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 <sys/ioctl.h>
15 #include <linux/vfio.h>
16 #include <linux/iommufd.h>
17
18 #include "hw/vfio/vfio-common.h"
19 #include "qemu/error-report.h"
20 #include "trace.h"
21 #include "qapi/error.h"
22 #include "system/iommufd.h"
23 #include "hw/qdev-core.h"
24 #include "system/reset.h"
25 #include "qemu/cutils.h"
26 #include "qemu/chardev_open.h"
27 #include "pci.h"
28
iommufd_cdev_map(const VFIOContainerBase * bcontainer,hwaddr iova,ram_addr_t size,void * vaddr,bool readonly)29 static int iommufd_cdev_map(const VFIOContainerBase *bcontainer, hwaddr iova,
30 ram_addr_t size, void *vaddr, bool readonly)
31 {
32 const VFIOIOMMUFDContainer *container =
33 container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
34
35 return iommufd_backend_map_dma(container->be,
36 container->ioas_id,
37 iova, size, vaddr, readonly);
38 }
39
iommufd_cdev_unmap(const VFIOContainerBase * bcontainer,hwaddr iova,ram_addr_t size,IOMMUTLBEntry * iotlb)40 static int iommufd_cdev_unmap(const VFIOContainerBase *bcontainer,
41 hwaddr iova, ram_addr_t size,
42 IOMMUTLBEntry *iotlb)
43 {
44 const VFIOIOMMUFDContainer *container =
45 container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
46
47 /* TODO: Handle dma_unmap_bitmap with iotlb args (migration) */
48 return iommufd_backend_unmap_dma(container->be,
49 container->ioas_id, iova, size);
50 }
51
iommufd_cdev_kvm_device_add(VFIODevice * vbasedev,Error ** errp)52 static bool iommufd_cdev_kvm_device_add(VFIODevice *vbasedev, Error **errp)
53 {
54 return !vfio_kvm_device_add_fd(vbasedev->fd, errp);
55 }
56
iommufd_cdev_kvm_device_del(VFIODevice * vbasedev)57 static void iommufd_cdev_kvm_device_del(VFIODevice *vbasedev)
58 {
59 Error *err = NULL;
60
61 if (vfio_kvm_device_del_fd(vbasedev->fd, &err)) {
62 error_report_err(err);
63 }
64 }
65
iommufd_cdev_connect_and_bind(VFIODevice * vbasedev,Error ** errp)66 static bool iommufd_cdev_connect_and_bind(VFIODevice *vbasedev, Error **errp)
67 {
68 IOMMUFDBackend *iommufd = vbasedev->iommufd;
69 struct vfio_device_bind_iommufd bind = {
70 .argsz = sizeof(bind),
71 .flags = 0,
72 };
73
74 if (!iommufd_backend_connect(iommufd, errp)) {
75 return false;
76 }
77
78 /*
79 * Add device to kvm-vfio to be prepared for the tracking
80 * in KVM. Especially for some emulated devices, it requires
81 * to have kvm information in the device open.
82 */
83 if (!iommufd_cdev_kvm_device_add(vbasedev, errp)) {
84 goto err_kvm_device_add;
85 }
86
87 /* Bind device to iommufd */
88 bind.iommufd = iommufd->fd;
89 if (ioctl(vbasedev->fd, VFIO_DEVICE_BIND_IOMMUFD, &bind)) {
90 error_setg_errno(errp, errno, "error bind device fd=%d to iommufd=%d",
91 vbasedev->fd, bind.iommufd);
92 goto err_bind;
93 }
94
95 vbasedev->devid = bind.out_devid;
96 trace_iommufd_cdev_connect_and_bind(bind.iommufd, vbasedev->name,
97 vbasedev->fd, vbasedev->devid);
98 return true;
99 err_bind:
100 iommufd_cdev_kvm_device_del(vbasedev);
101 err_kvm_device_add:
102 iommufd_backend_disconnect(iommufd);
103 return false;
104 }
105
iommufd_cdev_unbind_and_disconnect(VFIODevice * vbasedev)106 static void iommufd_cdev_unbind_and_disconnect(VFIODevice *vbasedev)
107 {
108 /* Unbind is automatically conducted when device fd is closed */
109 iommufd_cdev_kvm_device_del(vbasedev);
110 iommufd_backend_disconnect(vbasedev->iommufd);
111 }
112
iommufd_hwpt_dirty_tracking(VFIOIOASHwpt * hwpt)113 static bool iommufd_hwpt_dirty_tracking(VFIOIOASHwpt *hwpt)
114 {
115 return hwpt && hwpt->hwpt_flags & IOMMU_HWPT_ALLOC_DIRTY_TRACKING;
116 }
117
iommufd_set_dirty_page_tracking(const VFIOContainerBase * bcontainer,bool start,Error ** errp)118 static int iommufd_set_dirty_page_tracking(const VFIOContainerBase *bcontainer,
119 bool start, Error **errp)
120 {
121 const VFIOIOMMUFDContainer *container =
122 container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
123 VFIOIOASHwpt *hwpt;
124
125 QLIST_FOREACH(hwpt, &container->hwpt_list, next) {
126 if (!iommufd_hwpt_dirty_tracking(hwpt)) {
127 continue;
128 }
129
130 if (!iommufd_backend_set_dirty_tracking(container->be,
131 hwpt->hwpt_id, start, errp)) {
132 goto err;
133 }
134 }
135
136 return 0;
137
138 err:
139 QLIST_FOREACH(hwpt, &container->hwpt_list, next) {
140 if (!iommufd_hwpt_dirty_tracking(hwpt)) {
141 continue;
142 }
143 iommufd_backend_set_dirty_tracking(container->be,
144 hwpt->hwpt_id, !start, NULL);
145 }
146 return -EINVAL;
147 }
148
iommufd_query_dirty_bitmap(const VFIOContainerBase * bcontainer,VFIOBitmap * vbmap,hwaddr iova,hwaddr size,Error ** errp)149 static int iommufd_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
150 VFIOBitmap *vbmap, hwaddr iova,
151 hwaddr size, Error **errp)
152 {
153 VFIOIOMMUFDContainer *container = container_of(bcontainer,
154 VFIOIOMMUFDContainer,
155 bcontainer);
156 unsigned long page_size = qemu_real_host_page_size();
157 VFIOIOASHwpt *hwpt;
158
159 QLIST_FOREACH(hwpt, &container->hwpt_list, next) {
160 if (!iommufd_hwpt_dirty_tracking(hwpt)) {
161 continue;
162 }
163
164 if (!iommufd_backend_get_dirty_bitmap(container->be, hwpt->hwpt_id,
165 iova, size, page_size,
166 (uint64_t *)vbmap->bitmap,
167 errp)) {
168 return -EINVAL;
169 }
170 }
171
172 return 0;
173 }
174
iommufd_cdev_getfd(const char * sysfs_path,Error ** errp)175 static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp)
176 {
177 ERRP_GUARD();
178 long int ret = -ENOTTY;
179 g_autofree char *path = NULL;
180 g_autofree char *vfio_dev_path = NULL;
181 g_autofree char *vfio_path = NULL;
182 DIR *dir = NULL;
183 struct dirent *dent;
184 g_autofree gchar *contents = NULL;
185 gsize length;
186 int major, minor;
187 dev_t vfio_devt;
188
189 path = g_strdup_printf("%s/vfio-dev", sysfs_path);
190 dir = opendir(path);
191 if (!dir) {
192 error_setg_errno(errp, errno, "couldn't open directory %s", path);
193 goto out;
194 }
195
196 while ((dent = readdir(dir))) {
197 if (!strncmp(dent->d_name, "vfio", 4)) {
198 vfio_dev_path = g_strdup_printf("%s/%s/dev", path, dent->d_name);
199 break;
200 }
201 }
202
203 if (!vfio_dev_path) {
204 error_setg(errp, "failed to find vfio-dev/vfioX/dev");
205 goto out_close_dir;
206 }
207
208 if (!g_file_get_contents(vfio_dev_path, &contents, &length, NULL)) {
209 error_setg(errp, "failed to load \"%s\"", vfio_dev_path);
210 goto out_close_dir;
211 }
212
213 if (sscanf(contents, "%d:%d", &major, &minor) != 2) {
214 error_setg(errp, "failed to get major:minor for \"%s\"", vfio_dev_path);
215 goto out_close_dir;
216 }
217 vfio_devt = makedev(major, minor);
218
219 vfio_path = g_strdup_printf("/dev/vfio/devices/%s", dent->d_name);
220 ret = open_cdev(vfio_path, vfio_devt);
221 if (ret < 0) {
222 error_setg(errp, "Failed to open %s", vfio_path);
223 }
224
225 trace_iommufd_cdev_getfd(vfio_path, ret);
226
227 out_close_dir:
228 closedir(dir);
229 out:
230 if (*errp) {
231 error_prepend(errp, VFIO_MSG_PREFIX, path);
232 }
233
234 return ret;
235 }
236
iommufd_cdev_attach_ioas_hwpt(VFIODevice * vbasedev,uint32_t id,Error ** errp)237 static int iommufd_cdev_attach_ioas_hwpt(VFIODevice *vbasedev, uint32_t id,
238 Error **errp)
239 {
240 int iommufd = vbasedev->iommufd->fd;
241 struct vfio_device_attach_iommufd_pt attach_data = {
242 .argsz = sizeof(attach_data),
243 .flags = 0,
244 .pt_id = id,
245 };
246
247 /* Attach device to an IOAS or hwpt within iommufd */
248 if (ioctl(vbasedev->fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &attach_data)) {
249 error_setg_errno(errp, errno,
250 "[iommufd=%d] error attach %s (%d) to id=%d",
251 iommufd, vbasedev->name, vbasedev->fd, id);
252 return -errno;
253 }
254
255 trace_iommufd_cdev_attach_ioas_hwpt(iommufd, vbasedev->name,
256 vbasedev->fd, id);
257 return 0;
258 }
259
iommufd_cdev_detach_ioas_hwpt(VFIODevice * vbasedev,Error ** errp)260 static bool iommufd_cdev_detach_ioas_hwpt(VFIODevice *vbasedev, Error **errp)
261 {
262 int iommufd = vbasedev->iommufd->fd;
263 struct vfio_device_detach_iommufd_pt detach_data = {
264 .argsz = sizeof(detach_data),
265 .flags = 0,
266 };
267
268 if (ioctl(vbasedev->fd, VFIO_DEVICE_DETACH_IOMMUFD_PT, &detach_data)) {
269 error_setg_errno(errp, errno, "detach %s failed", vbasedev->name);
270 return false;
271 }
272
273 trace_iommufd_cdev_detach_ioas_hwpt(iommufd, vbasedev->name);
274 return true;
275 }
276
iommufd_cdev_autodomains_get(VFIODevice * vbasedev,VFIOIOMMUFDContainer * container,Error ** errp)277 static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
278 VFIOIOMMUFDContainer *container,
279 Error **errp)
280 {
281 ERRP_GUARD();
282 IOMMUFDBackend *iommufd = vbasedev->iommufd;
283 uint32_t flags = 0;
284 VFIOIOASHwpt *hwpt;
285 uint32_t hwpt_id;
286 int ret;
287
288 /* Try to find a domain */
289 QLIST_FOREACH(hwpt, &container->hwpt_list, next) {
290 ret = iommufd_cdev_attach_ioas_hwpt(vbasedev, hwpt->hwpt_id, errp);
291 if (ret) {
292 /* -EINVAL means the domain is incompatible with the device. */
293 if (ret == -EINVAL) {
294 /*
295 * It is an expected failure and it just means we will try
296 * another domain, or create one if no existing compatible
297 * domain is found. Hence why the error is discarded below.
298 */
299 error_free(*errp);
300 *errp = NULL;
301 continue;
302 }
303
304 return false;
305 } else {
306 vbasedev->hwpt = hwpt;
307 QLIST_INSERT_HEAD(&hwpt->device_list, vbasedev, hwpt_next);
308 vbasedev->iommu_dirty_tracking = iommufd_hwpt_dirty_tracking(hwpt);
309 return true;
310 }
311 }
312
313 /*
314 * This is quite early and VFIO Migration state isn't yet fully
315 * initialized, thus rely only on IOMMU hardware capabilities as to
316 * whether IOMMU dirty tracking is going to be requested. Later
317 * vfio_migration_realize() may decide to use VF dirty tracking
318 * instead.
319 */
320 if (vbasedev->hiod->caps.hw_caps & IOMMU_HW_CAP_DIRTY_TRACKING) {
321 flags = IOMMU_HWPT_ALLOC_DIRTY_TRACKING;
322 }
323
324 if (!iommufd_backend_alloc_hwpt(iommufd, vbasedev->devid,
325 container->ioas_id, flags,
326 IOMMU_HWPT_DATA_NONE, 0, NULL,
327 &hwpt_id, errp)) {
328 return false;
329 }
330
331 hwpt = g_malloc0(sizeof(*hwpt));
332 hwpt->hwpt_id = hwpt_id;
333 hwpt->hwpt_flags = flags;
334 QLIST_INIT(&hwpt->device_list);
335
336 ret = iommufd_cdev_attach_ioas_hwpt(vbasedev, hwpt->hwpt_id, errp);
337 if (ret) {
338 iommufd_backend_free_id(container->be, hwpt->hwpt_id);
339 g_free(hwpt);
340 return false;
341 }
342
343 vbasedev->hwpt = hwpt;
344 vbasedev->iommu_dirty_tracking = iommufd_hwpt_dirty_tracking(hwpt);
345 QLIST_INSERT_HEAD(&hwpt->device_list, vbasedev, hwpt_next);
346 QLIST_INSERT_HEAD(&container->hwpt_list, hwpt, next);
347 container->bcontainer.dirty_pages_supported |=
348 vbasedev->iommu_dirty_tracking;
349 if (container->bcontainer.dirty_pages_supported &&
350 !vbasedev->iommu_dirty_tracking) {
351 warn_report("IOMMU instance for device %s doesn't support dirty tracking",
352 vbasedev->name);
353 }
354 return true;
355 }
356
iommufd_cdev_autodomains_put(VFIODevice * vbasedev,VFIOIOMMUFDContainer * container)357 static void iommufd_cdev_autodomains_put(VFIODevice *vbasedev,
358 VFIOIOMMUFDContainer *container)
359 {
360 VFIOIOASHwpt *hwpt = vbasedev->hwpt;
361
362 QLIST_REMOVE(vbasedev, hwpt_next);
363 vbasedev->hwpt = NULL;
364
365 if (QLIST_EMPTY(&hwpt->device_list)) {
366 QLIST_REMOVE(hwpt, next);
367 iommufd_backend_free_id(container->be, hwpt->hwpt_id);
368 g_free(hwpt);
369 }
370 }
371
iommufd_cdev_attach_container(VFIODevice * vbasedev,VFIOIOMMUFDContainer * container,Error ** errp)372 static bool iommufd_cdev_attach_container(VFIODevice *vbasedev,
373 VFIOIOMMUFDContainer *container,
374 Error **errp)
375 {
376 /* mdevs aren't physical devices and will fail with auto domains */
377 if (!vbasedev->mdev) {
378 return iommufd_cdev_autodomains_get(vbasedev, container, errp);
379 }
380
381 return !iommufd_cdev_attach_ioas_hwpt(vbasedev, container->ioas_id, errp);
382 }
383
iommufd_cdev_detach_container(VFIODevice * vbasedev,VFIOIOMMUFDContainer * container)384 static void iommufd_cdev_detach_container(VFIODevice *vbasedev,
385 VFIOIOMMUFDContainer *container)
386 {
387 Error *err = NULL;
388
389 if (!iommufd_cdev_detach_ioas_hwpt(vbasedev, &err)) {
390 error_report_err(err);
391 }
392
393 if (vbasedev->hwpt) {
394 iommufd_cdev_autodomains_put(vbasedev, container);
395 }
396
397 }
398
iommufd_cdev_container_destroy(VFIOIOMMUFDContainer * container)399 static void iommufd_cdev_container_destroy(VFIOIOMMUFDContainer *container)
400 {
401 VFIOContainerBase *bcontainer = &container->bcontainer;
402
403 if (!QLIST_EMPTY(&bcontainer->device_list)) {
404 return;
405 }
406 memory_listener_unregister(&bcontainer->listener);
407 iommufd_backend_free_id(container->be, container->ioas_id);
408 object_unref(container);
409 }
410
iommufd_cdev_ram_block_discard_disable(bool state)411 static int iommufd_cdev_ram_block_discard_disable(bool state)
412 {
413 /*
414 * We support coordinated discarding of RAM via the RamDiscardManager.
415 */
416 return ram_block_uncoordinated_discard_disable(state);
417 }
418
iommufd_cdev_get_info_iova_range(VFIOIOMMUFDContainer * container,uint32_t ioas_id,Error ** errp)419 static bool iommufd_cdev_get_info_iova_range(VFIOIOMMUFDContainer *container,
420 uint32_t ioas_id, Error **errp)
421 {
422 VFIOContainerBase *bcontainer = &container->bcontainer;
423 g_autofree struct iommu_ioas_iova_ranges *info = NULL;
424 struct iommu_iova_range *iova_ranges;
425 int sz, fd = container->be->fd;
426
427 info = g_malloc0(sizeof(*info));
428 info->size = sizeof(*info);
429 info->ioas_id = ioas_id;
430
431 if (ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info) && errno != EMSGSIZE) {
432 goto error;
433 }
434
435 sz = info->num_iovas * sizeof(struct iommu_iova_range);
436 info = g_realloc(info, sizeof(*info) + sz);
437 info->allowed_iovas = (uintptr_t)(info + 1);
438
439 if (ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info)) {
440 goto error;
441 }
442
443 iova_ranges = (struct iommu_iova_range *)(uintptr_t)info->allowed_iovas;
444
445 for (int i = 0; i < info->num_iovas; i++) {
446 Range *range = g_new(Range, 1);
447
448 range_set_bounds(range, iova_ranges[i].start, iova_ranges[i].last);
449 bcontainer->iova_ranges =
450 range_list_insert(bcontainer->iova_ranges, range);
451 }
452 bcontainer->pgsizes = info->out_iova_alignment;
453
454 return true;
455
456 error:
457 error_setg_errno(errp, errno, "Cannot get IOVA ranges");
458 return false;
459 }
460
iommufd_cdev_attach(const char * name,VFIODevice * vbasedev,AddressSpace * as,Error ** errp)461 static bool iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
462 AddressSpace *as, Error **errp)
463 {
464 VFIOContainerBase *bcontainer;
465 VFIOIOMMUFDContainer *container;
466 VFIOAddressSpace *space;
467 struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };
468 int ret, devfd;
469 uint32_t ioas_id;
470 Error *err = NULL;
471 const VFIOIOMMUClass *iommufd_vioc =
472 VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD));
473
474 if (vbasedev->fd < 0) {
475 devfd = iommufd_cdev_getfd(vbasedev->sysfsdev, errp);
476 if (devfd < 0) {
477 return false;
478 }
479 vbasedev->fd = devfd;
480 } else {
481 devfd = vbasedev->fd;
482 }
483
484 if (!iommufd_cdev_connect_and_bind(vbasedev, errp)) {
485 goto err_connect_bind;
486 }
487
488 space = vfio_get_address_space(as);
489
490 /*
491 * The HostIOMMUDevice data from legacy backend is static and doesn't need
492 * any information from the (type1-iommu) backend to be initialized. In
493 * contrast however, the IOMMUFD HostIOMMUDevice data requires the iommufd
494 * FD to be connected and having a devid to be able to successfully call
495 * iommufd_backend_get_device_info().
496 */
497 if (!vfio_device_hiod_realize(vbasedev, errp)) {
498 goto err_alloc_ioas;
499 }
500
501 /* try to attach to an existing container in this space */
502 QLIST_FOREACH(bcontainer, &space->containers, next) {
503 container = container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
504 if (VFIO_IOMMU_GET_CLASS(bcontainer) != iommufd_vioc ||
505 vbasedev->iommufd != container->be) {
506 continue;
507 }
508 if (!iommufd_cdev_attach_container(vbasedev, container, &err)) {
509 const char *msg = error_get_pretty(err);
510
511 trace_iommufd_cdev_fail_attach_existing_container(msg);
512 error_free(err);
513 err = NULL;
514 } else {
515 ret = iommufd_cdev_ram_block_discard_disable(true);
516 if (ret) {
517 error_setg_errno(errp, -ret,
518 "Cannot set discarding of RAM broken");
519 goto err_discard_disable;
520 }
521 goto found_container;
522 }
523 }
524
525 /* Need to allocate a new dedicated container */
526 if (!iommufd_backend_alloc_ioas(vbasedev->iommufd, &ioas_id, errp)) {
527 goto err_alloc_ioas;
528 }
529
530 trace_iommufd_cdev_alloc_ioas(vbasedev->iommufd->fd, ioas_id);
531
532 container = VFIO_IOMMU_IOMMUFD(object_new(TYPE_VFIO_IOMMU_IOMMUFD));
533 container->be = vbasedev->iommufd;
534 container->ioas_id = ioas_id;
535 QLIST_INIT(&container->hwpt_list);
536
537 bcontainer = &container->bcontainer;
538 vfio_address_space_insert(space, bcontainer);
539
540 if (!iommufd_cdev_attach_container(vbasedev, container, errp)) {
541 goto err_attach_container;
542 }
543
544 ret = iommufd_cdev_ram_block_discard_disable(true);
545 if (ret) {
546 error_setg_errno(errp, -ret, "Cannot set discarding of RAM broken");
547 goto err_discard_disable;
548 }
549
550 if (!iommufd_cdev_get_info_iova_range(container, ioas_id, &err)) {
551 error_append_hint(&err,
552 "Fallback to default 64bit IOVA range and 4K page size\n");
553 warn_report_err(err);
554 err = NULL;
555 bcontainer->pgsizes = qemu_real_host_page_size();
556 }
557
558 bcontainer->listener = vfio_memory_listener;
559 memory_listener_register(&bcontainer->listener, bcontainer->space->as);
560
561 if (bcontainer->error) {
562 error_propagate_prepend(errp, bcontainer->error,
563 "memory listener initialization failed: ");
564 goto err_listener_register;
565 }
566
567 bcontainer->initialized = true;
568
569 found_container:
570 ret = ioctl(devfd, VFIO_DEVICE_GET_INFO, &dev_info);
571 if (ret) {
572 error_setg_errno(errp, errno, "error getting device info");
573 goto err_listener_register;
574 }
575
576 if (!vfio_cpr_register_container(bcontainer, errp)) {
577 goto err_listener_register;
578 }
579
580 /*
581 * TODO: examine RAM_BLOCK_DISCARD stuff, should we do group level
582 * for discarding incompatibility check as well?
583 */
584 if (vbasedev->ram_block_discard_allowed) {
585 iommufd_cdev_ram_block_discard_disable(false);
586 }
587
588 vbasedev->group = 0;
589 vbasedev->num_irqs = dev_info.num_irqs;
590 vbasedev->num_regions = dev_info.num_regions;
591 vbasedev->flags = dev_info.flags;
592 vbasedev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET);
593 vbasedev->bcontainer = bcontainer;
594 QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev, container_next);
595 QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next);
596
597 trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev->num_irqs,
598 vbasedev->num_regions, vbasedev->flags);
599 return true;
600
601 err_listener_register:
602 iommufd_cdev_ram_block_discard_disable(false);
603 err_discard_disable:
604 iommufd_cdev_detach_container(vbasedev, container);
605 err_attach_container:
606 iommufd_cdev_container_destroy(container);
607 err_alloc_ioas:
608 vfio_put_address_space(space);
609 iommufd_cdev_unbind_and_disconnect(vbasedev);
610 err_connect_bind:
611 close(vbasedev->fd);
612 return false;
613 }
614
iommufd_cdev_detach(VFIODevice * vbasedev)615 static void iommufd_cdev_detach(VFIODevice *vbasedev)
616 {
617 VFIOContainerBase *bcontainer = vbasedev->bcontainer;
618 VFIOAddressSpace *space = bcontainer->space;
619 VFIOIOMMUFDContainer *container = container_of(bcontainer,
620 VFIOIOMMUFDContainer,
621 bcontainer);
622 QLIST_REMOVE(vbasedev, global_next);
623 QLIST_REMOVE(vbasedev, container_next);
624 vbasedev->bcontainer = NULL;
625
626 if (!vbasedev->ram_block_discard_allowed) {
627 iommufd_cdev_ram_block_discard_disable(false);
628 }
629
630 vfio_cpr_unregister_container(bcontainer);
631 iommufd_cdev_detach_container(vbasedev, container);
632 iommufd_cdev_container_destroy(container);
633 vfio_put_address_space(space);
634
635 iommufd_cdev_unbind_and_disconnect(vbasedev);
636 close(vbasedev->fd);
637 }
638
iommufd_cdev_pci_find_by_devid(__u32 devid)639 static VFIODevice *iommufd_cdev_pci_find_by_devid(__u32 devid)
640 {
641 VFIODevice *vbasedev_iter;
642 const VFIOIOMMUClass *iommufd_vioc =
643 VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD));
644
645 QLIST_FOREACH(vbasedev_iter, &vfio_device_list, global_next) {
646 if (VFIO_IOMMU_GET_CLASS(vbasedev_iter->bcontainer) != iommufd_vioc) {
647 continue;
648 }
649 if (devid == vbasedev_iter->devid) {
650 return vbasedev_iter;
651 }
652 }
653 return NULL;
654 }
655
656 static VFIOPCIDevice *
iommufd_cdev_dep_get_realized_vpdev(struct vfio_pci_dependent_device * dep_dev,VFIODevice * reset_dev)657 iommufd_cdev_dep_get_realized_vpdev(struct vfio_pci_dependent_device *dep_dev,
658 VFIODevice *reset_dev)
659 {
660 VFIODevice *vbasedev_tmp;
661
662 if (dep_dev->devid == reset_dev->devid ||
663 dep_dev->devid == VFIO_PCI_DEVID_OWNED) {
664 return NULL;
665 }
666
667 vbasedev_tmp = iommufd_cdev_pci_find_by_devid(dep_dev->devid);
668 if (!vbasedev_tmp || !vbasedev_tmp->dev->realized ||
669 vbasedev_tmp->type != VFIO_DEVICE_TYPE_PCI) {
670 return NULL;
671 }
672
673 return container_of(vbasedev_tmp, VFIOPCIDevice, vbasedev);
674 }
675
iommufd_cdev_pci_hot_reset(VFIODevice * vbasedev,bool single)676 static int iommufd_cdev_pci_hot_reset(VFIODevice *vbasedev, bool single)
677 {
678 VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
679 struct vfio_pci_hot_reset_info *info = NULL;
680 struct vfio_pci_dependent_device *devices;
681 struct vfio_pci_hot_reset *reset;
682 int ret, i;
683 bool multi = false;
684
685 trace_vfio_pci_hot_reset(vdev->vbasedev.name, single ? "one" : "multi");
686
687 if (!single) {
688 vfio_pci_pre_reset(vdev);
689 }
690 vdev->vbasedev.needs_reset = false;
691
692 ret = vfio_pci_get_pci_hot_reset_info(vdev, &info);
693
694 if (ret) {
695 goto out_single;
696 }
697
698 assert(info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID);
699
700 devices = &info->devices[0];
701
702 if (!(info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID_OWNED)) {
703 if (!vdev->has_pm_reset) {
704 for (i = 0; i < info->count; i++) {
705 if (devices[i].devid == VFIO_PCI_DEVID_NOT_OWNED) {
706 error_report("vfio: Cannot reset device %s, "
707 "depends on device %04x:%02x:%02x.%x "
708 "which is not owned.",
709 vdev->vbasedev.name, devices[i].segment,
710 devices[i].bus, PCI_SLOT(devices[i].devfn),
711 PCI_FUNC(devices[i].devfn));
712 }
713 }
714 }
715 ret = -EPERM;
716 goto out_single;
717 }
718
719 trace_vfio_pci_hot_reset_has_dep_devices(vdev->vbasedev.name);
720
721 for (i = 0; i < info->count; i++) {
722 VFIOPCIDevice *tmp;
723
724 trace_iommufd_cdev_pci_hot_reset_dep_devices(devices[i].segment,
725 devices[i].bus,
726 PCI_SLOT(devices[i].devfn),
727 PCI_FUNC(devices[i].devfn),
728 devices[i].devid);
729
730 /*
731 * If a VFIO cdev device is resettable, all the dependent devices
732 * are either bound to same iommufd or within same iommu_groups as
733 * one of the iommufd bound devices.
734 */
735 assert(devices[i].devid != VFIO_PCI_DEVID_NOT_OWNED);
736
737 tmp = iommufd_cdev_dep_get_realized_vpdev(&devices[i], &vdev->vbasedev);
738 if (!tmp) {
739 continue;
740 }
741
742 if (single) {
743 ret = -EINVAL;
744 goto out_single;
745 }
746 vfio_pci_pre_reset(tmp);
747 tmp->vbasedev.needs_reset = false;
748 multi = true;
749 }
750
751 if (!single && !multi) {
752 ret = -EINVAL;
753 goto out_single;
754 }
755
756 /* Use zero length array for hot reset with iommufd backend */
757 reset = g_malloc0(sizeof(*reset));
758 reset->argsz = sizeof(*reset);
759
760 /* Bus reset! */
761 ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_PCI_HOT_RESET, reset);
762 g_free(reset);
763 if (ret) {
764 ret = -errno;
765 }
766
767 trace_vfio_pci_hot_reset_result(vdev->vbasedev.name,
768 ret ? strerror(errno) : "Success");
769
770 /* Re-enable INTx on affected devices */
771 for (i = 0; i < info->count; i++) {
772 VFIOPCIDevice *tmp;
773
774 tmp = iommufd_cdev_dep_get_realized_vpdev(&devices[i], &vdev->vbasedev);
775 if (!tmp) {
776 continue;
777 }
778 vfio_pci_post_reset(tmp);
779 }
780 out_single:
781 if (!single) {
782 vfio_pci_post_reset(vdev);
783 }
784 g_free(info);
785
786 return ret;
787 }
788
vfio_iommu_iommufd_class_init(ObjectClass * klass,void * data)789 static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data)
790 {
791 VFIOIOMMUClass *vioc = VFIO_IOMMU_CLASS(klass);
792
793 vioc->hiod_typename = TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO;
794
795 vioc->dma_map = iommufd_cdev_map;
796 vioc->dma_unmap = iommufd_cdev_unmap;
797 vioc->attach_device = iommufd_cdev_attach;
798 vioc->detach_device = iommufd_cdev_detach;
799 vioc->pci_hot_reset = iommufd_cdev_pci_hot_reset;
800 vioc->set_dirty_page_tracking = iommufd_set_dirty_page_tracking;
801 vioc->query_dirty_bitmap = iommufd_query_dirty_bitmap;
802 };
803
hiod_iommufd_vfio_realize(HostIOMMUDevice * hiod,void * opaque,Error ** errp)804 static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,
805 Error **errp)
806 {
807 VFIODevice *vdev = opaque;
808 HostIOMMUDeviceCaps *caps = &hiod->caps;
809 enum iommu_hw_info_type type;
810 union {
811 struct iommu_hw_info_vtd vtd;
812 } data;
813 uint64_t hw_caps;
814
815 hiod->agent = opaque;
816
817 if (!iommufd_backend_get_device_info(vdev->iommufd, vdev->devid,
818 &type, &data, sizeof(data),
819 &hw_caps, errp)) {
820 return false;
821 }
822
823 hiod->name = g_strdup(vdev->name);
824 caps->type = type;
825 caps->hw_caps = hw_caps;
826
827 return true;
828 }
829
830 static GList *
hiod_iommufd_vfio_get_iova_ranges(HostIOMMUDevice * hiod)831 hiod_iommufd_vfio_get_iova_ranges(HostIOMMUDevice *hiod)
832 {
833 VFIODevice *vdev = hiod->agent;
834
835 g_assert(vdev);
836 return vfio_container_get_iova_ranges(vdev->bcontainer);
837 }
838
839 static uint64_t
hiod_iommufd_vfio_get_page_size_mask(HostIOMMUDevice * hiod)840 hiod_iommufd_vfio_get_page_size_mask(HostIOMMUDevice *hiod)
841 {
842 VFIODevice *vdev = hiod->agent;
843
844 g_assert(vdev);
845 return vfio_container_get_page_size_mask(vdev->bcontainer);
846 }
847
848
hiod_iommufd_vfio_class_init(ObjectClass * oc,void * data)849 static void hiod_iommufd_vfio_class_init(ObjectClass *oc, void *data)
850 {
851 HostIOMMUDeviceClass *hiodc = HOST_IOMMU_DEVICE_CLASS(oc);
852
853 hiodc->realize = hiod_iommufd_vfio_realize;
854 hiodc->get_iova_ranges = hiod_iommufd_vfio_get_iova_ranges;
855 hiodc->get_page_size_mask = hiod_iommufd_vfio_get_page_size_mask;
856 };
857
858 static const TypeInfo types[] = {
859 {
860 .name = TYPE_VFIO_IOMMU_IOMMUFD,
861 .parent = TYPE_VFIO_IOMMU,
862 .instance_size = sizeof(VFIOIOMMUFDContainer),
863 .class_init = vfio_iommu_iommufd_class_init,
864 }, {
865 .name = TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO,
866 .parent = TYPE_HOST_IOMMU_DEVICE_IOMMUFD,
867 .class_init = hiod_iommufd_vfio_class_init,
868 }
869 };
870
871 DEFINE_TYPES(types)
872