2 * iommufd container backend
4 * Copyright (C) 2023 Intel Corporation.
5 * Copyright Red Hat, Inc. 2023
7 * Authors: Yi Liu <yi.l.liu@intel.com>
8 * Eric Auger <eric.auger@redhat.com>
10 * SPDX-License-Identifier: GPL-2.0-or-later
13 #include "qemu/osdep.h"
14 #include "sysemu/iommufd.h"
15 #include "qapi/error.h"
16 #include "qapi/qmp/qerror.h"
17 #include "qemu/module.h"
18 #include "qom/object_interfaces.h"
19 #include "qemu/error-report.h"
20 #include "monitor/monitor.h"
22 #include <sys/ioctl.h>
23 #include <linux/iommufd.h>
25 static void iommufd_backend_init(Object
*obj
)
27 IOMMUFDBackend
*be
= IOMMUFD_BACKEND(obj
);
34 static void iommufd_backend_finalize(Object
*obj
)
36 IOMMUFDBackend
*be
= IOMMUFD_BACKEND(obj
);
44 static void iommufd_backend_set_fd(Object
*obj
, const char *str
, Error
**errp
)
46 IOMMUFDBackend
*be
= IOMMUFD_BACKEND(obj
);
49 fd
= monitor_fd_param(monitor_cur(), str
, errp
);
51 error_prepend(errp
, "Could not parse remote object fd %s:", str
);
56 trace_iommu_backend_set_fd(be
->fd
);
59 static bool iommufd_backend_can_be_deleted(UserCreatable
*uc
)
61 IOMMUFDBackend
*be
= IOMMUFD_BACKEND(uc
);
66 static void iommufd_backend_class_init(ObjectClass
*oc
, void *data
)
68 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
70 ucc
->can_be_deleted
= iommufd_backend_can_be_deleted
;
72 object_class_property_add_str(oc
, "fd", NULL
, iommufd_backend_set_fd
);
75 int iommufd_backend_connect(IOMMUFDBackend
*be
, Error
**errp
)
79 if (be
->owned
&& !be
->users
) {
80 fd
= qemu_open_old("/dev/iommu", O_RDWR
);
82 error_setg_errno(errp
, errno
, "/dev/iommu opening failed");
90 trace_iommufd_backend_connect(be
->fd
, be
->owned
,
95 void iommufd_backend_disconnect(IOMMUFDBackend
*be
)
101 if (!be
->users
&& be
->owned
) {
106 trace_iommufd_backend_disconnect(be
->fd
, be
->users
);
109 int iommufd_backend_alloc_ioas(IOMMUFDBackend
*be
, uint32_t *ioas_id
,
112 int ret
, fd
= be
->fd
;
113 struct iommu_ioas_alloc alloc_data
= {
114 .size
= sizeof(alloc_data
),
118 ret
= ioctl(fd
, IOMMU_IOAS_ALLOC
, &alloc_data
);
120 error_setg_errno(errp
, errno
, "Failed to allocate ioas");
124 *ioas_id
= alloc_data
.out_ioas_id
;
125 trace_iommufd_backend_alloc_ioas(fd
, *ioas_id
, ret
);
130 void iommufd_backend_free_id(IOMMUFDBackend
*be
, uint32_t id
)
132 int ret
, fd
= be
->fd
;
133 struct iommu_destroy des
= {
138 ret
= ioctl(fd
, IOMMU_DESTROY
, &des
);
139 trace_iommufd_backend_free_id(fd
, id
, ret
);
141 error_report("Failed to free id: %u %m", id
);
145 int iommufd_backend_map_dma(IOMMUFDBackend
*be
, uint32_t ioas_id
, hwaddr iova
,
146 ram_addr_t size
, void *vaddr
, bool readonly
)
148 int ret
, fd
= be
->fd
;
149 struct iommu_ioas_map map
= {
151 .flags
= IOMMU_IOAS_MAP_READABLE
|
152 IOMMU_IOAS_MAP_FIXED_IOVA
,
155 .user_va
= (uintptr_t)vaddr
,
161 map
.flags
|= IOMMU_IOAS_MAP_WRITEABLE
;
164 ret
= ioctl(fd
, IOMMU_IOAS_MAP
, &map
);
165 trace_iommufd_backend_map_dma(fd
, ioas_id
, iova
, size
,
166 vaddr
, readonly
, ret
);
170 /* TODO: Not support mapping hardware PCI BAR region for now. */
171 if (errno
== EFAULT
) {
172 warn_report("IOMMU_IOAS_MAP failed: %m, PCI BAR?");
174 error_report("IOMMU_IOAS_MAP failed: %m");
180 int iommufd_backend_unmap_dma(IOMMUFDBackend
*be
, uint32_t ioas_id
,
181 hwaddr iova
, ram_addr_t size
)
183 int ret
, fd
= be
->fd
;
184 struct iommu_ioas_unmap unmap
= {
185 .size
= sizeof(unmap
),
191 ret
= ioctl(fd
, IOMMU_IOAS_UNMAP
, &unmap
);
193 * IOMMUFD takes mapping as some kind of object, unmapping
194 * nonexistent mapping is treated as deleting a nonexistent
195 * object and return ENOENT. This is different from legacy
196 * backend which allows it. vIOMMU may trigger a lot of
197 * redundant unmapping, to avoid flush the log, treat them
198 * as succeess for IOMMUFD just like legacy backend.
200 if (ret
&& errno
== ENOENT
) {
201 trace_iommufd_backend_unmap_dma_non_exist(fd
, ioas_id
, iova
, size
, ret
);
204 trace_iommufd_backend_unmap_dma(fd
, ioas_id
, iova
, size
, ret
);
209 error_report("IOMMU_IOAS_UNMAP failed: %m");
214 static const TypeInfo iommufd_backend_info
= {
215 .name
= TYPE_IOMMUFD_BACKEND
,
216 .parent
= TYPE_OBJECT
,
217 .instance_size
= sizeof(IOMMUFDBackend
),
218 .instance_init
= iommufd_backend_init
,
219 .instance_finalize
= iommufd_backend_finalize
,
220 .class_size
= sizeof(IOMMUFDBackendClass
),
221 .class_init
= iommufd_backend_class_init
,
222 .interfaces
= (InterfaceInfo
[]) {
223 { TYPE_USER_CREATABLE
},
228 static void register_types(void)
230 type_register_static(&iommufd_backend_info
);
233 type_init(register_types
);