xref: /openbmc/qemu/backends/iommufd.c (revision c5614ee3)
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 "qemu/module.h"
17 #include "qom/object_interfaces.h"
18 #include "qemu/error-report.h"
19 #include "monitor/monitor.h"
20 #include "trace.h"
21 #include <sys/ioctl.h>
22 #include <linux/iommufd.h>
23 
24 static void iommufd_backend_init(Object *obj)
25 {
26     IOMMUFDBackend *be = IOMMUFD_BACKEND(obj);
27 
28     be->fd = -1;
29     be->users = 0;
30     be->owned = true;
31 }
32 
33 static void iommufd_backend_finalize(Object *obj)
34 {
35     IOMMUFDBackend *be = IOMMUFD_BACKEND(obj);
36 
37     if (be->owned) {
38         close(be->fd);
39         be->fd = -1;
40     }
41 }
42 
43 static void iommufd_backend_set_fd(Object *obj, const char *str, Error **errp)
44 {
45     ERRP_GUARD();
46     IOMMUFDBackend *be = IOMMUFD_BACKEND(obj);
47     int fd = -1;
48 
49     fd = monitor_fd_param(monitor_cur(), str, errp);
50     if (fd == -1) {
51         error_prepend(errp, "Could not parse remote object fd %s:", str);
52         return;
53     }
54     be->fd = fd;
55     be->owned = false;
56     trace_iommu_backend_set_fd(be->fd);
57 }
58 
59 static bool iommufd_backend_can_be_deleted(UserCreatable *uc)
60 {
61     IOMMUFDBackend *be = IOMMUFD_BACKEND(uc);
62 
63     return !be->users;
64 }
65 
66 static void iommufd_backend_class_init(ObjectClass *oc, void *data)
67 {
68     UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
69 
70     ucc->can_be_deleted = iommufd_backend_can_be_deleted;
71 
72     object_class_property_add_str(oc, "fd", NULL, iommufd_backend_set_fd);
73 }
74 
75 bool iommufd_backend_connect(IOMMUFDBackend *be, Error **errp)
76 {
77     int fd;
78 
79     if (be->owned && !be->users) {
80         fd = qemu_open_old("/dev/iommu", O_RDWR);
81         if (fd < 0) {
82             error_setg_errno(errp, errno, "/dev/iommu opening failed");
83             return false;
84         }
85         be->fd = fd;
86     }
87     be->users++;
88 
89     trace_iommufd_backend_connect(be->fd, be->owned, be->users);
90     return true;
91 }
92 
93 void iommufd_backend_disconnect(IOMMUFDBackend *be)
94 {
95     if (!be->users) {
96         goto out;
97     }
98     be->users--;
99     if (!be->users && be->owned) {
100         close(be->fd);
101         be->fd = -1;
102     }
103 out:
104     trace_iommufd_backend_disconnect(be->fd, be->users);
105 }
106 
107 bool iommufd_backend_alloc_ioas(IOMMUFDBackend *be, uint32_t *ioas_id,
108                                 Error **errp)
109 {
110     int fd = be->fd;
111     struct iommu_ioas_alloc alloc_data  = {
112         .size = sizeof(alloc_data),
113         .flags = 0,
114     };
115 
116     if (ioctl(fd, IOMMU_IOAS_ALLOC, &alloc_data)) {
117         error_setg_errno(errp, errno, "Failed to allocate ioas");
118         return false;
119     }
120 
121     *ioas_id = alloc_data.out_ioas_id;
122     trace_iommufd_backend_alloc_ioas(fd, *ioas_id);
123 
124     return true;
125 }
126 
127 void iommufd_backend_free_id(IOMMUFDBackend *be, uint32_t id)
128 {
129     int ret, fd = be->fd;
130     struct iommu_destroy des = {
131         .size = sizeof(des),
132         .id = id,
133     };
134 
135     ret = ioctl(fd, IOMMU_DESTROY, &des);
136     trace_iommufd_backend_free_id(fd, id, ret);
137     if (ret) {
138         error_report("Failed to free id: %u %m", id);
139     }
140 }
141 
142 int iommufd_backend_map_dma(IOMMUFDBackend *be, uint32_t ioas_id, hwaddr iova,
143                             ram_addr_t size, void *vaddr, bool readonly)
144 {
145     int ret, fd = be->fd;
146     struct iommu_ioas_map map = {
147         .size = sizeof(map),
148         .flags = IOMMU_IOAS_MAP_READABLE |
149                  IOMMU_IOAS_MAP_FIXED_IOVA,
150         .ioas_id = ioas_id,
151         .__reserved = 0,
152         .user_va = (uintptr_t)vaddr,
153         .iova = iova,
154         .length = size,
155     };
156 
157     if (!readonly) {
158         map.flags |= IOMMU_IOAS_MAP_WRITEABLE;
159     }
160 
161     ret = ioctl(fd, IOMMU_IOAS_MAP, &map);
162     trace_iommufd_backend_map_dma(fd, ioas_id, iova, size,
163                                   vaddr, readonly, ret);
164     if (ret) {
165         ret = -errno;
166 
167         /* TODO: Not support mapping hardware PCI BAR region for now. */
168         if (errno == EFAULT) {
169             warn_report("IOMMU_IOAS_MAP failed: %m, PCI BAR?");
170         } else {
171             error_report("IOMMU_IOAS_MAP failed: %m");
172         }
173     }
174     return ret;
175 }
176 
177 int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id,
178                               hwaddr iova, ram_addr_t size)
179 {
180     int ret, fd = be->fd;
181     struct iommu_ioas_unmap unmap = {
182         .size = sizeof(unmap),
183         .ioas_id = ioas_id,
184         .iova = iova,
185         .length = size,
186     };
187 
188     ret = ioctl(fd, IOMMU_IOAS_UNMAP, &unmap);
189     /*
190      * IOMMUFD takes mapping as some kind of object, unmapping
191      * nonexistent mapping is treated as deleting a nonexistent
192      * object and return ENOENT. This is different from legacy
193      * backend which allows it. vIOMMU may trigger a lot of
194      * redundant unmapping, to avoid flush the log, treat them
195      * as succeess for IOMMUFD just like legacy backend.
196      */
197     if (ret && errno == ENOENT) {
198         trace_iommufd_backend_unmap_dma_non_exist(fd, ioas_id, iova, size, ret);
199         ret = 0;
200     } else {
201         trace_iommufd_backend_unmap_dma(fd, ioas_id, iova, size, ret);
202     }
203 
204     if (ret) {
205         ret = -errno;
206         error_report("IOMMU_IOAS_UNMAP failed: %m");
207     }
208     return ret;
209 }
210 
211 bool iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid,
212                                      uint32_t *type, void *data, uint32_t len,
213                                      Error **errp)
214 {
215     struct iommu_hw_info info = {
216         .size = sizeof(info),
217         .dev_id = devid,
218         .data_len = len,
219         .data_uptr = (uintptr_t)data,
220     };
221 
222     if (ioctl(be->fd, IOMMU_GET_HW_INFO, &info)) {
223         error_setg_errno(errp, errno, "Failed to get hardware info");
224         return false;
225     }
226 
227     g_assert(type);
228     *type = info.out_data_type;
229 
230     return true;
231 }
232 
233 static int hiod_iommufd_get_cap(HostIOMMUDevice *hiod, int cap, Error **errp)
234 {
235     HostIOMMUDeviceCaps *caps = &hiod->caps;
236 
237     switch (cap) {
238     case HOST_IOMMU_DEVICE_CAP_IOMMU_TYPE:
239         return caps->type;
240     case HOST_IOMMU_DEVICE_CAP_AW_BITS:
241         return caps->aw_bits;
242     default:
243         error_setg(errp, "%s: unsupported capability %x", hiod->name, cap);
244         return -EINVAL;
245     }
246 }
247 
248 static void hiod_iommufd_class_init(ObjectClass *oc, void *data)
249 {
250     HostIOMMUDeviceClass *hioc = HOST_IOMMU_DEVICE_CLASS(oc);
251 
252     hioc->get_cap = hiod_iommufd_get_cap;
253 };
254 
255 static const TypeInfo types[] = {
256     {
257         .name = TYPE_IOMMUFD_BACKEND,
258         .parent = TYPE_OBJECT,
259         .instance_size = sizeof(IOMMUFDBackend),
260         .instance_init = iommufd_backend_init,
261         .instance_finalize = iommufd_backend_finalize,
262         .class_size = sizeof(IOMMUFDBackendClass),
263         .class_init = iommufd_backend_class_init,
264         .interfaces = (InterfaceInfo[]) {
265             { TYPE_USER_CREATABLE },
266             { }
267         }
268     }, {
269         .name = TYPE_HOST_IOMMU_DEVICE_IOMMUFD,
270         .parent = TYPE_HOST_IOMMU_DEVICE,
271         .class_init = hiod_iommufd_class_init,
272         .abstract = true,
273     }
274 };
275 
276 DEFINE_TYPES(types)
277