]> git.proxmox.com Git - mirror_qemu.git/blob - backends/iommufd.c
393c0d9a3719e3de1a6b51a8ff2e75e184badc82
[mirror_qemu.git] / backends / iommufd.c
1 /*
2 * iommufd container backend
3 *
4 * Copyright (C) 2023 Intel Corporation.
5 * Copyright Red Hat, Inc. 2023
6 *
7 * Authors: Yi Liu <yi.l.liu@intel.com>
8 * Eric Auger <eric.auger@redhat.com>
9 *
10 * SPDX-License-Identifier: GPL-2.0-or-later
11 */
12
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"
21 #include "trace.h"
22 #include <sys/ioctl.h>
23 #include <linux/iommufd.h>
24
25 static void iommufd_backend_init(Object *obj)
26 {
27 IOMMUFDBackend *be = IOMMUFD_BACKEND(obj);
28
29 be->fd = -1;
30 be->users = 0;
31 be->owned = true;
32 qemu_mutex_init(&be->lock);
33 }
34
35 static void iommufd_backend_finalize(Object *obj)
36 {
37 IOMMUFDBackend *be = IOMMUFD_BACKEND(obj);
38
39 if (be->owned) {
40 close(be->fd);
41 be->fd = -1;
42 }
43 }
44
45 static void iommufd_backend_set_fd(Object *obj, const char *str, Error **errp)
46 {
47 IOMMUFDBackend *be = IOMMUFD_BACKEND(obj);
48 int fd = -1;
49
50 fd = monitor_fd_param(monitor_cur(), str, errp);
51 if (fd == -1) {
52 error_prepend(errp, "Could not parse remote object fd %s:", str);
53 return;
54 }
55 qemu_mutex_lock(&be->lock);
56 be->fd = fd;
57 be->owned = false;
58 qemu_mutex_unlock(&be->lock);
59 trace_iommu_backend_set_fd(be->fd);
60 }
61
62 static bool iommufd_backend_can_be_deleted(UserCreatable *uc)
63 {
64 IOMMUFDBackend *be = IOMMUFD_BACKEND(uc);
65
66 return !be->users;
67 }
68
69 static void iommufd_backend_class_init(ObjectClass *oc, void *data)
70 {
71 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
72
73 ucc->can_be_deleted = iommufd_backend_can_be_deleted;
74
75 object_class_property_add_str(oc, "fd", NULL, iommufd_backend_set_fd);
76 }
77
78 int iommufd_backend_connect(IOMMUFDBackend *be, Error **errp)
79 {
80 int fd, ret = 0;
81
82 qemu_mutex_lock(&be->lock);
83 if (be->owned && !be->users) {
84 fd = qemu_open_old("/dev/iommu", O_RDWR);
85 if (fd < 0) {
86 error_setg_errno(errp, errno, "/dev/iommu opening failed");
87 ret = fd;
88 goto out;
89 }
90 be->fd = fd;
91 }
92 be->users++;
93 out:
94 trace_iommufd_backend_connect(be->fd, be->owned,
95 be->users, ret);
96 qemu_mutex_unlock(&be->lock);
97 return ret;
98 }
99
100 void iommufd_backend_disconnect(IOMMUFDBackend *be)
101 {
102 qemu_mutex_lock(&be->lock);
103 if (!be->users) {
104 goto out;
105 }
106 be->users--;
107 if (!be->users && be->owned) {
108 close(be->fd);
109 be->fd = -1;
110 }
111 out:
112 trace_iommufd_backend_disconnect(be->fd, be->users);
113 qemu_mutex_unlock(&be->lock);
114 }
115
116 int iommufd_backend_alloc_ioas(IOMMUFDBackend *be, uint32_t *ioas_id,
117 Error **errp)
118 {
119 int ret, fd = be->fd;
120 struct iommu_ioas_alloc alloc_data = {
121 .size = sizeof(alloc_data),
122 .flags = 0,
123 };
124
125 ret = ioctl(fd, IOMMU_IOAS_ALLOC, &alloc_data);
126 if (ret) {
127 error_setg_errno(errp, errno, "Failed to allocate ioas");
128 return ret;
129 }
130
131 *ioas_id = alloc_data.out_ioas_id;
132 trace_iommufd_backend_alloc_ioas(fd, *ioas_id, ret);
133
134 return ret;
135 }
136
137 void iommufd_backend_free_id(IOMMUFDBackend *be, uint32_t id)
138 {
139 int ret, fd = be->fd;
140 struct iommu_destroy des = {
141 .size = sizeof(des),
142 .id = id,
143 };
144
145 ret = ioctl(fd, IOMMU_DESTROY, &des);
146 trace_iommufd_backend_free_id(fd, id, ret);
147 if (ret) {
148 error_report("Failed to free id: %u %m", id);
149 }
150 }
151
152 int iommufd_backend_map_dma(IOMMUFDBackend *be, uint32_t ioas_id, hwaddr iova,
153 ram_addr_t size, void *vaddr, bool readonly)
154 {
155 int ret, fd = be->fd;
156 struct iommu_ioas_map map = {
157 .size = sizeof(map),
158 .flags = IOMMU_IOAS_MAP_READABLE |
159 IOMMU_IOAS_MAP_FIXED_IOVA,
160 .ioas_id = ioas_id,
161 .__reserved = 0,
162 .user_va = (uintptr_t)vaddr,
163 .iova = iova,
164 .length = size,
165 };
166
167 if (!readonly) {
168 map.flags |= IOMMU_IOAS_MAP_WRITEABLE;
169 }
170
171 ret = ioctl(fd, IOMMU_IOAS_MAP, &map);
172 trace_iommufd_backend_map_dma(fd, ioas_id, iova, size,
173 vaddr, readonly, ret);
174 if (ret) {
175 ret = -errno;
176
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?");
180 } else {
181 error_report("IOMMU_IOAS_MAP failed: %m");
182 }
183 }
184 return ret;
185 }
186
187 int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id,
188 hwaddr iova, ram_addr_t size)
189 {
190 int ret, fd = be->fd;
191 struct iommu_ioas_unmap unmap = {
192 .size = sizeof(unmap),
193 .ioas_id = ioas_id,
194 .iova = iova,
195 .length = size,
196 };
197
198 ret = ioctl(fd, IOMMU_IOAS_UNMAP, &unmap);
199 /*
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.
206 */
207 if (ret && errno == ENOENT) {
208 trace_iommufd_backend_unmap_dma_non_exist(fd, ioas_id, iova, size, ret);
209 ret = 0;
210 } else {
211 trace_iommufd_backend_unmap_dma(fd, ioas_id, iova, size, ret);
212 }
213
214 if (ret) {
215 ret = -errno;
216 error_report("IOMMU_IOAS_UNMAP failed: %m");
217 }
218 return ret;
219 }
220
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 },
231 { }
232 }
233 };
234
235 static void register_types(void)
236 {
237 type_register_static(&iommufd_backend_info);
238 }
239
240 type_init(register_types);