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
);
32 qemu_mutex_init(&be
->lock
);
35 static void iommufd_backend_finalize(Object
*obj
)
37 IOMMUFDBackend
*be
= IOMMUFD_BACKEND(obj
);
45 static void iommufd_backend_set_fd(Object
*obj
, const char *str
, Error
**errp
)
47 IOMMUFDBackend
*be
= IOMMUFD_BACKEND(obj
);
50 fd
= monitor_fd_param(monitor_cur(), str
, errp
);
52 error_prepend(errp
, "Could not parse remote object fd %s:", str
);
55 qemu_mutex_lock(&be
->lock
);
58 qemu_mutex_unlock(&be
->lock
);
59 trace_iommu_backend_set_fd(be
->fd
);
62 static bool iommufd_backend_can_be_deleted(UserCreatable
*uc
)
64 IOMMUFDBackend
*be
= IOMMUFD_BACKEND(uc
);
69 static void iommufd_backend_class_init(ObjectClass
*oc
, void *data
)
71 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
73 ucc
->can_be_deleted
= iommufd_backend_can_be_deleted
;
75 object_class_property_add_str(oc
, "fd", NULL
, iommufd_backend_set_fd
);
78 int iommufd_backend_connect(IOMMUFDBackend
*be
, Error
**errp
)
82 qemu_mutex_lock(&be
->lock
);
83 if (be
->users
== UINT32_MAX
) {
84 error_setg(errp
, "too many connections");
88 if (be
->owned
&& !be
->users
) {
89 fd
= qemu_open_old("/dev/iommu", O_RDWR
);
91 error_setg_errno(errp
, errno
, "/dev/iommu opening failed");
99 trace_iommufd_backend_connect(be
->fd
, be
->owned
,
101 qemu_mutex_unlock(&be
->lock
);
105 void iommufd_backend_disconnect(IOMMUFDBackend
*be
)
107 qemu_mutex_lock(&be
->lock
);
112 if (!be
->users
&& be
->owned
) {
117 trace_iommufd_backend_disconnect(be
->fd
, be
->users
);
118 qemu_mutex_unlock(&be
->lock
);
121 int iommufd_backend_alloc_ioas(IOMMUFDBackend
*be
, uint32_t *ioas_id
,
124 int ret
, fd
= be
->fd
;
125 struct iommu_ioas_alloc alloc_data
= {
126 .size
= sizeof(alloc_data
),
130 ret
= ioctl(fd
, IOMMU_IOAS_ALLOC
, &alloc_data
);
132 error_setg_errno(errp
, errno
, "Failed to allocate ioas");
136 *ioas_id
= alloc_data
.out_ioas_id
;
137 trace_iommufd_backend_alloc_ioas(fd
, *ioas_id
, ret
);
142 void iommufd_backend_free_id(IOMMUFDBackend
*be
, uint32_t id
)
144 int ret
, fd
= be
->fd
;
145 struct iommu_destroy des
= {
150 ret
= ioctl(fd
, IOMMU_DESTROY
, &des
);
151 trace_iommufd_backend_free_id(fd
, id
, ret
);
153 error_report("Failed to free id: %u %m", id
);
157 int iommufd_backend_map_dma(IOMMUFDBackend
*be
, uint32_t ioas_id
, hwaddr iova
,
158 ram_addr_t size
, void *vaddr
, bool readonly
)
160 int ret
, fd
= be
->fd
;
161 struct iommu_ioas_map map
= {
163 .flags
= IOMMU_IOAS_MAP_READABLE
|
164 IOMMU_IOAS_MAP_FIXED_IOVA
,
167 .user_va
= (uintptr_t)vaddr
,
173 map
.flags
|= IOMMU_IOAS_MAP_WRITEABLE
;
176 ret
= ioctl(fd
, IOMMU_IOAS_MAP
, &map
);
177 trace_iommufd_backend_map_dma(fd
, ioas_id
, iova
, size
,
178 vaddr
, readonly
, ret
);
182 /* TODO: Not support mapping hardware PCI BAR region for now. */
183 if (errno
== EFAULT
) {
184 warn_report("IOMMU_IOAS_MAP failed: %m, PCI BAR?");
186 error_report("IOMMU_IOAS_MAP failed: %m");
192 int iommufd_backend_unmap_dma(IOMMUFDBackend
*be
, uint32_t ioas_id
,
193 hwaddr iova
, ram_addr_t size
)
195 int ret
, fd
= be
->fd
;
196 struct iommu_ioas_unmap unmap
= {
197 .size
= sizeof(unmap
),
203 ret
= ioctl(fd
, IOMMU_IOAS_UNMAP
, &unmap
);
205 * IOMMUFD takes mapping as some kind of object, unmapping
206 * nonexistent mapping is treated as deleting a nonexistent
207 * object and return ENOENT. This is different from legacy
208 * backend which allows it. vIOMMU may trigger a lot of
209 * redundant unmapping, to avoid flush the log, treat them
210 * as succeess for IOMMUFD just like legacy backend.
212 if (ret
&& errno
== ENOENT
) {
213 trace_iommufd_backend_unmap_dma_non_exist(fd
, ioas_id
, iova
, size
, ret
);
216 trace_iommufd_backend_unmap_dma(fd
, ioas_id
, iova
, size
, ret
);
221 error_report("IOMMU_IOAS_UNMAP failed: %m");
226 static const TypeInfo iommufd_backend_info
= {
227 .name
= TYPE_IOMMUFD_BACKEND
,
228 .parent
= TYPE_OBJECT
,
229 .instance_size
= sizeof(IOMMUFDBackend
),
230 .instance_init
= iommufd_backend_init
,
231 .instance_finalize
= iommufd_backend_finalize
,
232 .class_size
= sizeof(IOMMUFDBackendClass
),
233 .class_init
= iommufd_backend_class_init
,
234 .interfaces
= (InterfaceInfo
[]) {
235 { TYPE_USER_CREATABLE
},
240 static void register_types(void)
242 type_register_static(&iommufd_backend_info
);
245 type_init(register_types
);