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
->owned
&& !be
->users
) {
84 fd
= qemu_open_old("/dev/iommu", O_RDWR
);
86 error_setg_errno(errp
, errno
, "/dev/iommu opening failed");
94 trace_iommufd_backend_connect(be
->fd
, be
->owned
,
96 qemu_mutex_unlock(&be
->lock
);
100 void iommufd_backend_disconnect(IOMMUFDBackend
*be
)
102 qemu_mutex_lock(&be
->lock
);
107 if (!be
->users
&& be
->owned
) {
112 trace_iommufd_backend_disconnect(be
->fd
, be
->users
);
113 qemu_mutex_unlock(&be
->lock
);
116 int iommufd_backend_alloc_ioas(IOMMUFDBackend
*be
, uint32_t *ioas_id
,
119 int ret
, fd
= be
->fd
;
120 struct iommu_ioas_alloc alloc_data
= {
121 .size
= sizeof(alloc_data
),
125 ret
= ioctl(fd
, IOMMU_IOAS_ALLOC
, &alloc_data
);
127 error_setg_errno(errp
, errno
, "Failed to allocate ioas");
131 *ioas_id
= alloc_data
.out_ioas_id
;
132 trace_iommufd_backend_alloc_ioas(fd
, *ioas_id
, ret
);
137 void iommufd_backend_free_id(IOMMUFDBackend
*be
, uint32_t id
)
139 int ret
, fd
= be
->fd
;
140 struct iommu_destroy des
= {
145 ret
= ioctl(fd
, IOMMU_DESTROY
, &des
);
146 trace_iommufd_backend_free_id(fd
, id
, ret
);
148 error_report("Failed to free id: %u %m", id
);
152 int iommufd_backend_map_dma(IOMMUFDBackend
*be
, uint32_t ioas_id
, hwaddr iova
,
153 ram_addr_t size
, void *vaddr
, bool readonly
)
155 int ret
, fd
= be
->fd
;
156 struct iommu_ioas_map map
= {
158 .flags
= IOMMU_IOAS_MAP_READABLE
|
159 IOMMU_IOAS_MAP_FIXED_IOVA
,
162 .user_va
= (uintptr_t)vaddr
,
168 map
.flags
|= IOMMU_IOAS_MAP_WRITEABLE
;
171 ret
= ioctl(fd
, IOMMU_IOAS_MAP
, &map
);
172 trace_iommufd_backend_map_dma(fd
, ioas_id
, iova
, size
,
173 vaddr
, readonly
, ret
);
177 /* TODO: Not support mapping hardware PCI BAR region for now. */
178 if (errno
== EFAULT
) {
179 warn_report("IOMMU_IOAS_MAP failed: %m, PCI BAR?");
181 error_report("IOMMU_IOAS_MAP failed: %m");
187 int iommufd_backend_unmap_dma(IOMMUFDBackend
*be
, uint32_t ioas_id
,
188 hwaddr iova
, ram_addr_t size
)
190 int ret
, fd
= be
->fd
;
191 struct iommu_ioas_unmap unmap
= {
192 .size
= sizeof(unmap
),
198 ret
= ioctl(fd
, IOMMU_IOAS_UNMAP
, &unmap
);
200 * IOMMUFD takes mapping as some kind of object, unmapping
201 * nonexistent mapping is treated as deleting a nonexistent
202 * object and return ENOENT. This is different from legacy
203 * backend which allows it. vIOMMU may trigger a lot of
204 * redundant unmapping, to avoid flush the log, treat them
205 * as succeess for IOMMUFD just like legacy backend.
207 if (ret
&& errno
== ENOENT
) {
208 trace_iommufd_backend_unmap_dma_non_exist(fd
, ioas_id
, iova
, size
, ret
);
211 trace_iommufd_backend_unmap_dma(fd
, ioas_id
, iova
, size
, ret
);
216 error_report("IOMMU_IOAS_UNMAP failed: %m");
221 static const TypeInfo iommufd_backend_info
= {
222 .name
= TYPE_IOMMUFD_BACKEND
,
223 .parent
= TYPE_OBJECT
,
224 .instance_size
= sizeof(IOMMUFDBackend
),
225 .instance_init
= iommufd_backend_init
,
226 .instance_finalize
= iommufd_backend_finalize
,
227 .class_size
= sizeof(IOMMUFDBackendClass
),
228 .class_init
= iommufd_backend_class_init
,
229 .interfaces
= (InterfaceInfo
[]) {
230 { TYPE_USER_CREATABLE
},
235 static void register_types(void)
237 type_register_static(&iommufd_backend_info
);
240 type_init(register_types
);