]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/vfio/vfio.c
vfio: Tie IOMMU group reference to vfio group
[mirror_ubuntu-bionic-kernel.git] / drivers / vfio / vfio.c
CommitLineData
cba3345c
AW
1/*
2 * VFIO core
3 *
4 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
5 * Author: Alex Williamson <alex.williamson@redhat.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * Derived from original vfio:
12 * Copyright 2010 Cisco Systems, Inc. All rights reserved.
13 * Author: Tom Lyon, pugs@cisco.com
14 */
15
16#include <linux/cdev.h>
17#include <linux/compat.h>
18#include <linux/device.h>
19#include <linux/file.h>
20#include <linux/anon_inodes.h>
21#include <linux/fs.h>
22#include <linux/idr.h>
23#include <linux/iommu.h>
24#include <linux/list.h>
d1099901 25#include <linux/miscdevice.h>
cba3345c
AW
26#include <linux/module.h>
27#include <linux/mutex.h>
9587f44a 28#include <linux/rwsem.h>
cba3345c
AW
29#include <linux/sched.h>
30#include <linux/slab.h>
664e9386 31#include <linux/stat.h>
cba3345c
AW
32#include <linux/string.h>
33#include <linux/uaccess.h>
34#include <linux/vfio.h>
35#include <linux/wait.h>
36
37#define DRIVER_VERSION "0.3"
38#define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
39#define DRIVER_DESC "VFIO - User Level meta-driver"
40
41static struct vfio {
42 struct class *class;
43 struct list_head iommu_drivers_list;
44 struct mutex iommu_drivers_lock;
45 struct list_head group_list;
46 struct idr group_idr;
47 struct mutex group_lock;
48 struct cdev group_cdev;
d1099901 49 dev_t group_devt;
cba3345c
AW
50 wait_queue_head_t release_q;
51} vfio;
52
53struct vfio_iommu_driver {
54 const struct vfio_iommu_driver_ops *ops;
55 struct list_head vfio_next;
56};
57
58struct vfio_container {
59 struct kref kref;
60 struct list_head group_list;
9587f44a 61 struct rw_semaphore group_lock;
cba3345c
AW
62 struct vfio_iommu_driver *iommu_driver;
63 void *iommu_data;
64};
65
60720a0f
AW
66struct vfio_unbound_dev {
67 struct device *dev;
68 struct list_head unbound_next;
69};
70
cba3345c
AW
71struct vfio_group {
72 struct kref kref;
73 int minor;
74 atomic_t container_users;
75 struct iommu_group *iommu_group;
76 struct vfio_container *container;
77 struct list_head device_list;
78 struct mutex device_lock;
79 struct device *dev;
80 struct notifier_block nb;
81 struct list_head vfio_next;
82 struct list_head container_next;
60720a0f
AW
83 struct list_head unbound_list;
84 struct mutex unbound_lock;
6d6768c6 85 atomic_t opened;
cba3345c
AW
86};
87
88struct vfio_device {
89 struct kref kref;
90 struct device *dev;
91 const struct vfio_device_ops *ops;
92 struct vfio_group *group;
93 struct list_head group_next;
94 void *device_data;
95};
96
97/**
98 * IOMMU driver registration
99 */
100int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops)
101{
102 struct vfio_iommu_driver *driver, *tmp;
103
104 driver = kzalloc(sizeof(*driver), GFP_KERNEL);
105 if (!driver)
106 return -ENOMEM;
107
108 driver->ops = ops;
109
110 mutex_lock(&vfio.iommu_drivers_lock);
111
112 /* Check for duplicates */
113 list_for_each_entry(tmp, &vfio.iommu_drivers_list, vfio_next) {
114 if (tmp->ops == ops) {
115 mutex_unlock(&vfio.iommu_drivers_lock);
116 kfree(driver);
117 return -EINVAL;
118 }
119 }
120
121 list_add(&driver->vfio_next, &vfio.iommu_drivers_list);
122
123 mutex_unlock(&vfio.iommu_drivers_lock);
124
125 return 0;
126}
127EXPORT_SYMBOL_GPL(vfio_register_iommu_driver);
128
129void vfio_unregister_iommu_driver(const struct vfio_iommu_driver_ops *ops)
130{
131 struct vfio_iommu_driver *driver;
132
133 mutex_lock(&vfio.iommu_drivers_lock);
134 list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
135 if (driver->ops == ops) {
136 list_del(&driver->vfio_next);
137 mutex_unlock(&vfio.iommu_drivers_lock);
138 kfree(driver);
139 return;
140 }
141 }
142 mutex_unlock(&vfio.iommu_drivers_lock);
143}
144EXPORT_SYMBOL_GPL(vfio_unregister_iommu_driver);
145
146/**
147 * Group minor allocation/free - both called with vfio.group_lock held
148 */
149static int vfio_alloc_group_minor(struct vfio_group *group)
150{
d1099901 151 return idr_alloc(&vfio.group_idr, group, 0, MINORMASK + 1, GFP_KERNEL);
cba3345c
AW
152}
153
154static void vfio_free_group_minor(int minor)
155{
156 idr_remove(&vfio.group_idr, minor);
157}
158
159static int vfio_iommu_group_notifier(struct notifier_block *nb,
160 unsigned long action, void *data);
161static void vfio_group_get(struct vfio_group *group);
162
163/**
164 * Container objects - containers are created when /dev/vfio/vfio is
165 * opened, but their lifecycle extends until the last user is done, so
166 * it's freed via kref. Must support container/group/device being
167 * closed in any order.
168 */
169static void vfio_container_get(struct vfio_container *container)
170{
171 kref_get(&container->kref);
172}
173
174static void vfio_container_release(struct kref *kref)
175{
176 struct vfio_container *container;
177 container = container_of(kref, struct vfio_container, kref);
178
179 kfree(container);
180}
181
182static void vfio_container_put(struct vfio_container *container)
183{
184 kref_put(&container->kref, vfio_container_release);
185}
186
9df7b25a
JL
187static void vfio_group_unlock_and_free(struct vfio_group *group)
188{
189 mutex_unlock(&vfio.group_lock);
190 /*
191 * Unregister outside of lock. A spurious callback is harmless now
192 * that the group is no longer in vfio.group_list.
193 */
194 iommu_group_unregister_notifier(group->iommu_group, &group->nb);
195 kfree(group);
196}
197
cba3345c
AW
198/**
199 * Group objects - create, release, get, put, search
200 */
201static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group)
202{
203 struct vfio_group *group, *tmp;
204 struct device *dev;
205 int ret, minor;
206
207 group = kzalloc(sizeof(*group), GFP_KERNEL);
208 if (!group)
209 return ERR_PTR(-ENOMEM);
210
211 kref_init(&group->kref);
212 INIT_LIST_HEAD(&group->device_list);
213 mutex_init(&group->device_lock);
60720a0f
AW
214 INIT_LIST_HEAD(&group->unbound_list);
215 mutex_init(&group->unbound_lock);
cba3345c 216 atomic_set(&group->container_users, 0);
6d6768c6 217 atomic_set(&group->opened, 0);
cba3345c
AW
218 group->iommu_group = iommu_group;
219
220 group->nb.notifier_call = vfio_iommu_group_notifier;
221
222 /*
223 * blocking notifiers acquire a rwsem around registering and hold
224 * it around callback. Therefore, need to register outside of
225 * vfio.group_lock to avoid A-B/B-A contention. Our callback won't
226 * do anything unless it can find the group in vfio.group_list, so
227 * no harm in registering early.
228 */
229 ret = iommu_group_register_notifier(iommu_group, &group->nb);
230 if (ret) {
231 kfree(group);
232 return ERR_PTR(ret);
233 }
234
235 mutex_lock(&vfio.group_lock);
236
237 minor = vfio_alloc_group_minor(group);
238 if (minor < 0) {
9df7b25a 239 vfio_group_unlock_and_free(group);
cba3345c
AW
240 return ERR_PTR(minor);
241 }
242
243 /* Did we race creating this group? */
244 list_for_each_entry(tmp, &vfio.group_list, vfio_next) {
245 if (tmp->iommu_group == iommu_group) {
246 vfio_group_get(tmp);
247 vfio_free_group_minor(minor);
9df7b25a 248 vfio_group_unlock_and_free(group);
cba3345c
AW
249 return tmp;
250 }
251 }
252
d1099901
AW
253 dev = device_create(vfio.class, NULL,
254 MKDEV(MAJOR(vfio.group_devt), minor),
cba3345c
AW
255 group, "%d", iommu_group_id(iommu_group));
256 if (IS_ERR(dev)) {
257 vfio_free_group_minor(minor);
9df7b25a 258 vfio_group_unlock_and_free(group);
cba3345c
AW
259 return (struct vfio_group *)dev; /* ERR_PTR */
260 }
261
262 group->minor = minor;
263 group->dev = dev;
264
265 list_add(&group->vfio_next, &vfio.group_list);
266
267 mutex_unlock(&vfio.group_lock);
268
269 return group;
270}
271
6d2cd3ce 272/* called with vfio.group_lock held */
cba3345c
AW
273static void vfio_group_release(struct kref *kref)
274{
275 struct vfio_group *group = container_of(kref, struct vfio_group, kref);
60720a0f 276 struct vfio_unbound_dev *unbound, *tmp;
4a68810d 277 struct iommu_group *iommu_group = group->iommu_group;
cba3345c
AW
278
279 WARN_ON(!list_empty(&group->device_list));
280
60720a0f
AW
281 list_for_each_entry_safe(unbound, tmp,
282 &group->unbound_list, unbound_next) {
283 list_del(&unbound->unbound_next);
284 kfree(unbound);
285 }
286
d1099901 287 device_destroy(vfio.class, MKDEV(MAJOR(vfio.group_devt), group->minor));
cba3345c
AW
288 list_del(&group->vfio_next);
289 vfio_free_group_minor(group->minor);
9df7b25a 290 vfio_group_unlock_and_free(group);
4a68810d 291 iommu_group_put(iommu_group);
cba3345c
AW
292}
293
294static void vfio_group_put(struct vfio_group *group)
295{
6d2cd3ce 296 kref_put_mutex(&group->kref, vfio_group_release, &vfio.group_lock);
cba3345c
AW
297}
298
299/* Assume group_lock or group reference is held */
300static void vfio_group_get(struct vfio_group *group)
301{
302 kref_get(&group->kref);
303}
304
305/*
306 * Not really a try as we will sleep for mutex, but we need to make
307 * sure the group pointer is valid under lock and get a reference.
308 */
309static struct vfio_group *vfio_group_try_get(struct vfio_group *group)
310{
311 struct vfio_group *target = group;
312
313 mutex_lock(&vfio.group_lock);
314 list_for_each_entry(group, &vfio.group_list, vfio_next) {
315 if (group == target) {
316 vfio_group_get(group);
317 mutex_unlock(&vfio.group_lock);
318 return group;
319 }
320 }
321 mutex_unlock(&vfio.group_lock);
322
323 return NULL;
324}
325
326static
327struct vfio_group *vfio_group_get_from_iommu(struct iommu_group *iommu_group)
328{
329 struct vfio_group *group;
330
331 mutex_lock(&vfio.group_lock);
332 list_for_each_entry(group, &vfio.group_list, vfio_next) {
333 if (group->iommu_group == iommu_group) {
334 vfio_group_get(group);
335 mutex_unlock(&vfio.group_lock);
336 return group;
337 }
338 }
339 mutex_unlock(&vfio.group_lock);
340
341 return NULL;
342}
343
344static struct vfio_group *vfio_group_get_from_minor(int minor)
345{
346 struct vfio_group *group;
347
348 mutex_lock(&vfio.group_lock);
349 group = idr_find(&vfio.group_idr, minor);
350 if (!group) {
351 mutex_unlock(&vfio.group_lock);
352 return NULL;
353 }
354 vfio_group_get(group);
355 mutex_unlock(&vfio.group_lock);
356
357 return group;
358}
359
360/**
361 * Device objects - create, release, get, put, search
362 */
363static
364struct vfio_device *vfio_group_create_device(struct vfio_group *group,
365 struct device *dev,
366 const struct vfio_device_ops *ops,
367 void *device_data)
368{
369 struct vfio_device *device;
cba3345c
AW
370
371 device = kzalloc(sizeof(*device), GFP_KERNEL);
372 if (!device)
373 return ERR_PTR(-ENOMEM);
374
375 kref_init(&device->kref);
376 device->dev = dev;
377 device->group = group;
378 device->ops = ops;
379 device->device_data = device_data;
8283b491 380 dev_set_drvdata(dev, device);
cba3345c
AW
381
382 /* No need to get group_lock, caller has group reference */
383 vfio_group_get(group);
384
385 mutex_lock(&group->device_lock);
386 list_add(&device->group_next, &group->device_list);
387 mutex_unlock(&group->device_lock);
388
389 return device;
390}
391
392static void vfio_device_release(struct kref *kref)
393{
394 struct vfio_device *device = container_of(kref,
395 struct vfio_device, kref);
396 struct vfio_group *group = device->group;
397
cba3345c
AW
398 list_del(&device->group_next);
399 mutex_unlock(&group->device_lock);
400
401 dev_set_drvdata(device->dev, NULL);
402
403 kfree(device);
404
405 /* vfio_del_group_dev may be waiting for this device */
406 wake_up(&vfio.release_q);
407}
408
409/* Device reference always implies a group reference */
44f50716 410void vfio_device_put(struct vfio_device *device)
cba3345c 411{
934ad4c2 412 struct vfio_group *group = device->group;
90b1253e 413 kref_put_mutex(&device->kref, vfio_device_release, &group->device_lock);
934ad4c2 414 vfio_group_put(group);
cba3345c 415}
44f50716 416EXPORT_SYMBOL_GPL(vfio_device_put);
cba3345c
AW
417
418static void vfio_device_get(struct vfio_device *device)
419{
420 vfio_group_get(device->group);
421 kref_get(&device->kref);
422}
423
424static struct vfio_device *vfio_group_get_device(struct vfio_group *group,
425 struct device *dev)
426{
427 struct vfio_device *device;
428
429 mutex_lock(&group->device_lock);
430 list_for_each_entry(device, &group->device_list, group_next) {
431 if (device->dev == dev) {
432 vfio_device_get(device);
433 mutex_unlock(&group->device_lock);
434 return device;
435 }
436 }
437 mutex_unlock(&group->device_lock);
438 return NULL;
439}
440
441/*
442 * Whitelist some drivers that we know are safe (no dma) or just sit on
443 * a device. It's not always practical to leave a device within a group
444 * driverless as it could get re-bound to something unsafe.
445 */
2b489a45 446static const char * const vfio_driver_whitelist[] = { "pci-stub", "pcieport" };
cba3345c
AW
447
448static bool vfio_whitelisted_driver(struct device_driver *drv)
449{
450 int i;
451
452 for (i = 0; i < ARRAY_SIZE(vfio_driver_whitelist); i++) {
453 if (!strcmp(drv->name, vfio_driver_whitelist[i]))
454 return true;
455 }
456
457 return false;
458}
459
460/*
60720a0f
AW
461 * A vfio group is viable for use by userspace if all devices are in
462 * one of the following states:
463 * - driver-less
464 * - bound to a vfio driver
465 * - bound to a whitelisted driver
466 *
467 * We use two methods to determine whether a device is bound to a vfio
468 * driver. The first is to test whether the device exists in the vfio
469 * group. The second is to test if the device exists on the group
470 * unbound_list, indicating it's in the middle of transitioning from
471 * a vfio driver to driver-less.
cba3345c
AW
472 */
473static int vfio_dev_viable(struct device *dev, void *data)
474{
475 struct vfio_group *group = data;
476 struct vfio_device *device;
de2b3eea 477 struct device_driver *drv = ACCESS_ONCE(dev->driver);
60720a0f
AW
478 struct vfio_unbound_dev *unbound;
479 int ret = -EINVAL;
480
481 mutex_lock(&group->unbound_lock);
482 list_for_each_entry(unbound, &group->unbound_list, unbound_next) {
483 if (dev == unbound->dev) {
484 ret = 0;
485 break;
486 }
487 }
488 mutex_unlock(&group->unbound_lock);
cba3345c 489
60720a0f 490 if (!ret || !drv || vfio_whitelisted_driver(drv))
cba3345c
AW
491 return 0;
492
493 device = vfio_group_get_device(group, dev);
494 if (device) {
495 vfio_device_put(device);
496 return 0;
497 }
498
60720a0f 499 return ret;
cba3345c
AW
500}
501
502/**
503 * Async device support
504 */
505static int vfio_group_nb_add_dev(struct vfio_group *group, struct device *dev)
506{
507 struct vfio_device *device;
508
509 /* Do we already know about it? We shouldn't */
510 device = vfio_group_get_device(group, dev);
511 if (WARN_ON_ONCE(device)) {
512 vfio_device_put(device);
513 return 0;
514 }
515
516 /* Nothing to do for idle groups */
517 if (!atomic_read(&group->container_users))
518 return 0;
519
520 /* TODO Prevent device auto probing */
521 WARN("Device %s added to live group %d!\n", dev_name(dev),
522 iommu_group_id(group->iommu_group));
523
524 return 0;
525}
526
cba3345c
AW
527static int vfio_group_nb_verify(struct vfio_group *group, struct device *dev)
528{
529 /* We don't care what happens when the group isn't in use */
530 if (!atomic_read(&group->container_users))
531 return 0;
532
533 return vfio_dev_viable(dev, group);
534}
535
536static int vfio_iommu_group_notifier(struct notifier_block *nb,
537 unsigned long action, void *data)
538{
539 struct vfio_group *group = container_of(nb, struct vfio_group, nb);
540 struct device *dev = data;
60720a0f 541 struct vfio_unbound_dev *unbound;
cba3345c
AW
542
543 /*
c6401930
AW
544 * Need to go through a group_lock lookup to get a reference or we
545 * risk racing a group being removed. Ignore spurious notifies.
cba3345c
AW
546 */
547 group = vfio_group_try_get(group);
c6401930 548 if (!group)
cba3345c
AW
549 return NOTIFY_OK;
550
551 switch (action) {
552 case IOMMU_GROUP_NOTIFY_ADD_DEVICE:
553 vfio_group_nb_add_dev(group, dev);
554 break;
555 case IOMMU_GROUP_NOTIFY_DEL_DEVICE:
de9c7602
AW
556 /*
557 * Nothing to do here. If the device is in use, then the
558 * vfio sub-driver should block the remove callback until
559 * it is unused. If the device is unused or attached to a
560 * stub driver, then it should be released and we don't
561 * care that it will be going away.
562 */
cba3345c
AW
563 break;
564 case IOMMU_GROUP_NOTIFY_BIND_DRIVER:
565 pr_debug("%s: Device %s, group %d binding to driver\n",
566 __func__, dev_name(dev),
567 iommu_group_id(group->iommu_group));
568 break;
569 case IOMMU_GROUP_NOTIFY_BOUND_DRIVER:
570 pr_debug("%s: Device %s, group %d bound to driver %s\n",
571 __func__, dev_name(dev),
572 iommu_group_id(group->iommu_group), dev->driver->name);
573 BUG_ON(vfio_group_nb_verify(group, dev));
574 break;
575 case IOMMU_GROUP_NOTIFY_UNBIND_DRIVER:
576 pr_debug("%s: Device %s, group %d unbinding from driver %s\n",
577 __func__, dev_name(dev),
578 iommu_group_id(group->iommu_group), dev->driver->name);
579 break;
580 case IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER:
581 pr_debug("%s: Device %s, group %d unbound from driver\n",
582 __func__, dev_name(dev),
583 iommu_group_id(group->iommu_group));
584 /*
585 * XXX An unbound device in a live group is ok, but we'd
586 * really like to avoid the above BUG_ON by preventing other
587 * drivers from binding to it. Once that occurs, we have to
588 * stop the system to maintain isolation. At a minimum, we'd
589 * want a toggle to disable driver auto probe for this device.
590 */
60720a0f
AW
591
592 mutex_lock(&group->unbound_lock);
593 list_for_each_entry(unbound,
594 &group->unbound_list, unbound_next) {
595 if (dev == unbound->dev) {
596 list_del(&unbound->unbound_next);
597 kfree(unbound);
598 break;
599 }
600 }
601 mutex_unlock(&group->unbound_lock);
cba3345c
AW
602 break;
603 }
604
605 vfio_group_put(group);
606 return NOTIFY_OK;
607}
608
609/**
610 * VFIO driver API
611 */
612int vfio_add_group_dev(struct device *dev,
613 const struct vfio_device_ops *ops, void *device_data)
614{
615 struct iommu_group *iommu_group;
616 struct vfio_group *group;
617 struct vfio_device *device;
618
619 iommu_group = iommu_group_get(dev);
620 if (!iommu_group)
621 return -EINVAL;
622
623 group = vfio_group_get_from_iommu(iommu_group);
624 if (!group) {
625 group = vfio_create_group(iommu_group);
626 if (IS_ERR(group)) {
627 iommu_group_put(iommu_group);
628 return PTR_ERR(group);
629 }
4a68810d
AW
630 } else {
631 /*
632 * A found vfio_group already holds a reference to the
633 * iommu_group. A created vfio_group keeps the reference.
634 */
635 iommu_group_put(iommu_group);
cba3345c
AW
636 }
637
638 device = vfio_group_get_device(group, dev);
639 if (device) {
640 WARN(1, "Device %s already exists on group %d\n",
641 dev_name(dev), iommu_group_id(iommu_group));
642 vfio_device_put(device);
643 vfio_group_put(group);
cba3345c
AW
644 return -EBUSY;
645 }
646
647 device = vfio_group_create_device(group, dev, ops, device_data);
648 if (IS_ERR(device)) {
649 vfio_group_put(group);
cba3345c
AW
650 return PTR_ERR(device);
651 }
652
653 /*
4a68810d
AW
654 * Drop all but the vfio_device reference. The vfio_device holds
655 * a reference to the vfio_group, which holds a reference to the
656 * iommu_group.
cba3345c
AW
657 */
658 vfio_group_put(group);
659
660 return 0;
661}
662EXPORT_SYMBOL_GPL(vfio_add_group_dev);
663
44f50716
VMP
664/**
665 * Get a reference to the vfio_device for a device that is known to
666 * be bound to a vfio driver. The driver implicitly holds a
667 * vfio_device reference between vfio_add_group_dev and
668 * vfio_del_group_dev. We can therefore use drvdata to increment
669 * that reference from the struct device. This additional
670 * reference must be released by calling vfio_device_put.
671 */
672struct vfio_device *vfio_device_get_from_dev(struct device *dev)
673{
674 struct vfio_device *device = dev_get_drvdata(dev);
675
676 vfio_device_get(device);
677
678 return device;
679}
680EXPORT_SYMBOL_GPL(vfio_device_get_from_dev);
681
682/*
683 * Caller must hold a reference to the vfio_device
684 */
685void *vfio_device_data(struct vfio_device *device)
686{
687 return device->device_data;
688}
689EXPORT_SYMBOL_GPL(vfio_device_data);
690
e014e944
AW
691/* Given a referenced group, check if it contains the device */
692static bool vfio_dev_present(struct vfio_group *group, struct device *dev)
cba3345c 693{
cba3345c
AW
694 struct vfio_device *device;
695
cba3345c 696 device = vfio_group_get_device(group, dev);
e014e944 697 if (!device)
cba3345c 698 return false;
cba3345c
AW
699
700 vfio_device_put(device);
cba3345c
AW
701 return true;
702}
703
704/*
705 * Decrement the device reference count and wait for the device to be
706 * removed. Open file descriptors for the device... */
707void *vfio_del_group_dev(struct device *dev)
708{
709 struct vfio_device *device = dev_get_drvdata(dev);
710 struct vfio_group *group = device->group;
cba3345c 711 void *device_data = device->device_data;
60720a0f 712 struct vfio_unbound_dev *unbound;
cba3345c 713
e014e944
AW
714 /*
715 * The group exists so long as we have a device reference. Get
716 * a group reference and use it to scan for the device going away.
717 */
718 vfio_group_get(group);
719
60720a0f
AW
720 /*
721 * When the device is removed from the group, the group suddenly
722 * becomes non-viable; the device has a driver (until the unbind
723 * completes), but it's not present in the group. This is bad news
724 * for any external users that need to re-acquire a group reference
725 * in order to match and release their existing reference. To
726 * solve this, we track such devices on the unbound_list to bridge
727 * the gap until they're fully unbound.
728 */
729 unbound = kzalloc(sizeof(*unbound), GFP_KERNEL);
730 if (unbound) {
731 unbound->dev = dev;
732 mutex_lock(&group->unbound_lock);
733 list_add(&unbound->unbound_next, &group->unbound_list);
734 mutex_unlock(&group->unbound_lock);
735 }
736 WARN_ON(!unbound);
737
cba3345c
AW
738 vfio_device_put(device);
739
740 /* TODO send a signal to encourage this to be released */
e014e944
AW
741 wait_event(vfio.release_q, !vfio_dev_present(group, dev));
742
743 vfio_group_put(group);
cba3345c 744
cba3345c
AW
745 return device_data;
746}
747EXPORT_SYMBOL_GPL(vfio_del_group_dev);
748
749/**
750 * VFIO base fd, /dev/vfio/vfio
751 */
752static long vfio_ioctl_check_extension(struct vfio_container *container,
753 unsigned long arg)
754{
0b43c082 755 struct vfio_iommu_driver *driver;
cba3345c
AW
756 long ret = 0;
757
0b43c082
AW
758 down_read(&container->group_lock);
759
760 driver = container->iommu_driver;
761
cba3345c
AW
762 switch (arg) {
763 /* No base extensions yet */
764 default:
765 /*
766 * If no driver is set, poll all registered drivers for
767 * extensions and return the first positive result. If
768 * a driver is already set, further queries will be passed
769 * only to that driver.
770 */
771 if (!driver) {
772 mutex_lock(&vfio.iommu_drivers_lock);
773 list_for_each_entry(driver, &vfio.iommu_drivers_list,
774 vfio_next) {
775 if (!try_module_get(driver->ops->owner))
776 continue;
777
778 ret = driver->ops->ioctl(NULL,
779 VFIO_CHECK_EXTENSION,
780 arg);
781 module_put(driver->ops->owner);
782 if (ret > 0)
783 break;
784 }
785 mutex_unlock(&vfio.iommu_drivers_lock);
786 } else
787 ret = driver->ops->ioctl(container->iommu_data,
788 VFIO_CHECK_EXTENSION, arg);
789 }
790
0b43c082
AW
791 up_read(&container->group_lock);
792
cba3345c
AW
793 return ret;
794}
795
9587f44a 796/* hold write lock on container->group_lock */
cba3345c
AW
797static int __vfio_container_attach_groups(struct vfio_container *container,
798 struct vfio_iommu_driver *driver,
799 void *data)
800{
801 struct vfio_group *group;
802 int ret = -ENODEV;
803
804 list_for_each_entry(group, &container->group_list, container_next) {
805 ret = driver->ops->attach_group(data, group->iommu_group);
806 if (ret)
807 goto unwind;
808 }
809
810 return ret;
811
812unwind:
813 list_for_each_entry_continue_reverse(group, &container->group_list,
814 container_next) {
815 driver->ops->detach_group(data, group->iommu_group);
816 }
817
818 return ret;
819}
820
821static long vfio_ioctl_set_iommu(struct vfio_container *container,
822 unsigned long arg)
823{
824 struct vfio_iommu_driver *driver;
825 long ret = -ENODEV;
826
9587f44a 827 down_write(&container->group_lock);
cba3345c
AW
828
829 /*
830 * The container is designed to be an unprivileged interface while
831 * the group can be assigned to specific users. Therefore, only by
832 * adding a group to a container does the user get the privilege of
833 * enabling the iommu, which may allocate finite resources. There
834 * is no unset_iommu, but by removing all the groups from a container,
835 * the container is deprivileged and returns to an unset state.
836 */
837 if (list_empty(&container->group_list) || container->iommu_driver) {
9587f44a 838 up_write(&container->group_lock);
cba3345c
AW
839 return -EINVAL;
840 }
841
842 mutex_lock(&vfio.iommu_drivers_lock);
843 list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
844 void *data;
845
846 if (!try_module_get(driver->ops->owner))
847 continue;
848
849 /*
850 * The arg magic for SET_IOMMU is the same as CHECK_EXTENSION,
851 * so test which iommu driver reported support for this
852 * extension and call open on them. We also pass them the
853 * magic, allowing a single driver to support multiple
854 * interfaces if they'd like.
855 */
856 if (driver->ops->ioctl(NULL, VFIO_CHECK_EXTENSION, arg) <= 0) {
857 module_put(driver->ops->owner);
858 continue;
859 }
860
861 /* module reference holds the driver we're working on */
862 mutex_unlock(&vfio.iommu_drivers_lock);
863
864 data = driver->ops->open(arg);
865 if (IS_ERR(data)) {
866 ret = PTR_ERR(data);
867 module_put(driver->ops->owner);
868 goto skip_drivers_unlock;
869 }
870
871 ret = __vfio_container_attach_groups(container, driver, data);
872 if (!ret) {
873 container->iommu_driver = driver;
874 container->iommu_data = data;
875 } else {
876 driver->ops->release(data);
877 module_put(driver->ops->owner);
878 }
879
880 goto skip_drivers_unlock;
881 }
882
883 mutex_unlock(&vfio.iommu_drivers_lock);
884skip_drivers_unlock:
9587f44a 885 up_write(&container->group_lock);
cba3345c
AW
886
887 return ret;
888}
889
890static long vfio_fops_unl_ioctl(struct file *filep,
891 unsigned int cmd, unsigned long arg)
892{
893 struct vfio_container *container = filep->private_data;
894 struct vfio_iommu_driver *driver;
895 void *data;
896 long ret = -EINVAL;
897
898 if (!container)
899 return ret;
900
cba3345c
AW
901 switch (cmd) {
902 case VFIO_GET_API_VERSION:
903 ret = VFIO_API_VERSION;
904 break;
905 case VFIO_CHECK_EXTENSION:
906 ret = vfio_ioctl_check_extension(container, arg);
907 break;
908 case VFIO_SET_IOMMU:
909 ret = vfio_ioctl_set_iommu(container, arg);
910 break;
911 default:
0b43c082
AW
912 down_read(&container->group_lock);
913
914 driver = container->iommu_driver;
915 data = container->iommu_data;
916
cba3345c
AW
917 if (driver) /* passthrough all unrecognized ioctls */
918 ret = driver->ops->ioctl(data, cmd, arg);
0b43c082
AW
919
920 up_read(&container->group_lock);
cba3345c
AW
921 }
922
923 return ret;
924}
925
926#ifdef CONFIG_COMPAT
927static long vfio_fops_compat_ioctl(struct file *filep,
928 unsigned int cmd, unsigned long arg)
929{
930 arg = (unsigned long)compat_ptr(arg);
931 return vfio_fops_unl_ioctl(filep, cmd, arg);
932}
933#endif /* CONFIG_COMPAT */
934
935static int vfio_fops_open(struct inode *inode, struct file *filep)
936{
937 struct vfio_container *container;
938
939 container = kzalloc(sizeof(*container), GFP_KERNEL);
940 if (!container)
941 return -ENOMEM;
942
943 INIT_LIST_HEAD(&container->group_list);
9587f44a 944 init_rwsem(&container->group_lock);
cba3345c
AW
945 kref_init(&container->kref);
946
947 filep->private_data = container;
948
949 return 0;
950}
951
952static int vfio_fops_release(struct inode *inode, struct file *filep)
953{
954 struct vfio_container *container = filep->private_data;
955
956 filep->private_data = NULL;
957
958 vfio_container_put(container);
959
960 return 0;
961}
962
963/*
964 * Once an iommu driver is set, we optionally pass read/write/mmap
965 * on to the driver, allowing management interfaces beyond ioctl.
966 */
967static ssize_t vfio_fops_read(struct file *filep, char __user *buf,
968 size_t count, loff_t *ppos)
969{
970 struct vfio_container *container = filep->private_data;
0b43c082
AW
971 struct vfio_iommu_driver *driver;
972 ssize_t ret = -EINVAL;
cba3345c 973
0b43c082
AW
974 down_read(&container->group_lock);
975
976 driver = container->iommu_driver;
977 if (likely(driver && driver->ops->read))
978 ret = driver->ops->read(container->iommu_data,
979 buf, count, ppos);
cba3345c 980
0b43c082
AW
981 up_read(&container->group_lock);
982
983 return ret;
cba3345c
AW
984}
985
986static ssize_t vfio_fops_write(struct file *filep, const char __user *buf,
987 size_t count, loff_t *ppos)
988{
989 struct vfio_container *container = filep->private_data;
0b43c082
AW
990 struct vfio_iommu_driver *driver;
991 ssize_t ret = -EINVAL;
cba3345c 992
0b43c082
AW
993 down_read(&container->group_lock);
994
995 driver = container->iommu_driver;
996 if (likely(driver && driver->ops->write))
997 ret = driver->ops->write(container->iommu_data,
998 buf, count, ppos);
999
1000 up_read(&container->group_lock);
cba3345c 1001
0b43c082 1002 return ret;
cba3345c
AW
1003}
1004
1005static int vfio_fops_mmap(struct file *filep, struct vm_area_struct *vma)
1006{
1007 struct vfio_container *container = filep->private_data;
0b43c082
AW
1008 struct vfio_iommu_driver *driver;
1009 int ret = -EINVAL;
cba3345c 1010
0b43c082 1011 down_read(&container->group_lock);
cba3345c 1012
0b43c082
AW
1013 driver = container->iommu_driver;
1014 if (likely(driver && driver->ops->mmap))
1015 ret = driver->ops->mmap(container->iommu_data, vma);
1016
1017 up_read(&container->group_lock);
1018
1019 return ret;
cba3345c
AW
1020}
1021
1022static const struct file_operations vfio_fops = {
1023 .owner = THIS_MODULE,
1024 .open = vfio_fops_open,
1025 .release = vfio_fops_release,
1026 .read = vfio_fops_read,
1027 .write = vfio_fops_write,
1028 .unlocked_ioctl = vfio_fops_unl_ioctl,
1029#ifdef CONFIG_COMPAT
1030 .compat_ioctl = vfio_fops_compat_ioctl,
1031#endif
1032 .mmap = vfio_fops_mmap,
1033};
1034
1035/**
1036 * VFIO Group fd, /dev/vfio/$GROUP
1037 */
1038static void __vfio_group_unset_container(struct vfio_group *group)
1039{
1040 struct vfio_container *container = group->container;
1041 struct vfio_iommu_driver *driver;
1042
9587f44a 1043 down_write(&container->group_lock);
cba3345c
AW
1044
1045 driver = container->iommu_driver;
1046 if (driver)
1047 driver->ops->detach_group(container->iommu_data,
1048 group->iommu_group);
1049
1050 group->container = NULL;
1051 list_del(&group->container_next);
1052
1053 /* Detaching the last group deprivileges a container, remove iommu */
1054 if (driver && list_empty(&container->group_list)) {
1055 driver->ops->release(container->iommu_data);
1056 module_put(driver->ops->owner);
1057 container->iommu_driver = NULL;
1058 container->iommu_data = NULL;
1059 }
1060
9587f44a 1061 up_write(&container->group_lock);
cba3345c
AW
1062
1063 vfio_container_put(container);
1064}
1065
1066/*
1067 * VFIO_GROUP_UNSET_CONTAINER should fail if there are other users or
1068 * if there was no container to unset. Since the ioctl is called on
1069 * the group, we know that still exists, therefore the only valid
1070 * transition here is 1->0.
1071 */
1072static int vfio_group_unset_container(struct vfio_group *group)
1073{
1074 int users = atomic_cmpxchg(&group->container_users, 1, 0);
1075
1076 if (!users)
1077 return -EINVAL;
1078 if (users != 1)
1079 return -EBUSY;
1080
1081 __vfio_group_unset_container(group);
1082
1083 return 0;
1084}
1085
1086/*
1087 * When removing container users, anything that removes the last user
1088 * implicitly removes the group from the container. That is, if the
1089 * group file descriptor is closed, as well as any device file descriptors,
1090 * the group is free.
1091 */
1092static void vfio_group_try_dissolve_container(struct vfio_group *group)
1093{
1094 if (0 == atomic_dec_if_positive(&group->container_users))
1095 __vfio_group_unset_container(group);
1096}
1097
1098static int vfio_group_set_container(struct vfio_group *group, int container_fd)
1099{
2903ff01 1100 struct fd f;
cba3345c
AW
1101 struct vfio_container *container;
1102 struct vfio_iommu_driver *driver;
2903ff01 1103 int ret = 0;
cba3345c
AW
1104
1105 if (atomic_read(&group->container_users))
1106 return -EINVAL;
1107
2903ff01
AV
1108 f = fdget(container_fd);
1109 if (!f.file)
cba3345c
AW
1110 return -EBADF;
1111
1112 /* Sanity check, is this really our fd? */
2903ff01
AV
1113 if (f.file->f_op != &vfio_fops) {
1114 fdput(f);
cba3345c
AW
1115 return -EINVAL;
1116 }
1117
2903ff01 1118 container = f.file->private_data;
cba3345c
AW
1119 WARN_ON(!container); /* fget ensures we don't race vfio_release */
1120
9587f44a 1121 down_write(&container->group_lock);
cba3345c
AW
1122
1123 driver = container->iommu_driver;
1124 if (driver) {
1125 ret = driver->ops->attach_group(container->iommu_data,
1126 group->iommu_group);
1127 if (ret)
1128 goto unlock_out;
1129 }
1130
1131 group->container = container;
1132 list_add(&group->container_next, &container->group_list);
1133
1134 /* Get a reference on the container and mark a user within the group */
1135 vfio_container_get(container);
1136 atomic_inc(&group->container_users);
1137
1138unlock_out:
9587f44a 1139 up_write(&container->group_lock);
2903ff01 1140 fdput(f);
cba3345c
AW
1141 return ret;
1142}
1143
1144static bool vfio_group_viable(struct vfio_group *group)
1145{
1146 return (iommu_group_for_each_dev(group->iommu_group,
1147 group, vfio_dev_viable) == 0);
1148}
1149
1150static const struct file_operations vfio_device_fops;
1151
1152static int vfio_group_get_device_fd(struct vfio_group *group, char *buf)
1153{
1154 struct vfio_device *device;
1155 struct file *filep;
1156 int ret = -ENODEV;
1157
1158 if (0 == atomic_read(&group->container_users) ||
1159 !group->container->iommu_driver || !vfio_group_viable(group))
1160 return -EINVAL;
1161
1162 mutex_lock(&group->device_lock);
1163 list_for_each_entry(device, &group->device_list, group_next) {
1164 if (strcmp(dev_name(device->dev), buf))
1165 continue;
1166
1167 ret = device->ops->open(device->device_data);
1168 if (ret)
1169 break;
1170 /*
1171 * We can't use anon_inode_getfd() because we need to modify
1172 * the f_mode flags directly to allow more than just ioctls
1173 */
5d042fbd 1174 ret = get_unused_fd_flags(O_CLOEXEC);
cba3345c
AW
1175 if (ret < 0) {
1176 device->ops->release(device->device_data);
1177 break;
1178 }
1179
1180 filep = anon_inode_getfile("[vfio-device]", &vfio_device_fops,
1181 device, O_RDWR);
1182 if (IS_ERR(filep)) {
1183 put_unused_fd(ret);
1184 ret = PTR_ERR(filep);
1185 device->ops->release(device->device_data);
1186 break;
1187 }
1188
1189 /*
1190 * TODO: add an anon_inode interface to do this.
1191 * Appears to be missing by lack of need rather than
1192 * explicitly prevented. Now there's need.
1193 */
1194 filep->f_mode |= (FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1195
cba3345c
AW
1196 vfio_device_get(device);
1197 atomic_inc(&group->container_users);
31605deb
AV
1198
1199 fd_install(ret, filep);
cba3345c
AW
1200 break;
1201 }
1202 mutex_unlock(&group->device_lock);
1203
1204 return ret;
1205}
1206
1207static long vfio_group_fops_unl_ioctl(struct file *filep,
1208 unsigned int cmd, unsigned long arg)
1209{
1210 struct vfio_group *group = filep->private_data;
1211 long ret = -ENOTTY;
1212
1213 switch (cmd) {
1214 case VFIO_GROUP_GET_STATUS:
1215 {
1216 struct vfio_group_status status;
1217 unsigned long minsz;
1218
1219 minsz = offsetofend(struct vfio_group_status, flags);
1220
1221 if (copy_from_user(&status, (void __user *)arg, minsz))
1222 return -EFAULT;
1223
1224 if (status.argsz < minsz)
1225 return -EINVAL;
1226
1227 status.flags = 0;
1228
1229 if (vfio_group_viable(group))
1230 status.flags |= VFIO_GROUP_FLAGS_VIABLE;
1231
1232 if (group->container)
1233 status.flags |= VFIO_GROUP_FLAGS_CONTAINER_SET;
1234
1235 if (copy_to_user((void __user *)arg, &status, minsz))
1236 return -EFAULT;
1237
1238 ret = 0;
1239 break;
1240 }
1241 case VFIO_GROUP_SET_CONTAINER:
1242 {
1243 int fd;
1244
1245 if (get_user(fd, (int __user *)arg))
1246 return -EFAULT;
1247
1248 if (fd < 0)
1249 return -EINVAL;
1250
1251 ret = vfio_group_set_container(group, fd);
1252 break;
1253 }
1254 case VFIO_GROUP_UNSET_CONTAINER:
1255 ret = vfio_group_unset_container(group);
1256 break;
1257 case VFIO_GROUP_GET_DEVICE_FD:
1258 {
1259 char *buf;
1260
1261 buf = strndup_user((const char __user *)arg, PAGE_SIZE);
1262 if (IS_ERR(buf))
1263 return PTR_ERR(buf);
1264
1265 ret = vfio_group_get_device_fd(group, buf);
1266 kfree(buf);
1267 break;
1268 }
1269 }
1270
1271 return ret;
1272}
1273
1274#ifdef CONFIG_COMPAT
1275static long vfio_group_fops_compat_ioctl(struct file *filep,
1276 unsigned int cmd, unsigned long arg)
1277{
1278 arg = (unsigned long)compat_ptr(arg);
1279 return vfio_group_fops_unl_ioctl(filep, cmd, arg);
1280}
1281#endif /* CONFIG_COMPAT */
1282
1283static int vfio_group_fops_open(struct inode *inode, struct file *filep)
1284{
1285 struct vfio_group *group;
6d6768c6 1286 int opened;
cba3345c
AW
1287
1288 group = vfio_group_get_from_minor(iminor(inode));
1289 if (!group)
1290 return -ENODEV;
1291
6d6768c6
AW
1292 /* Do we need multiple instances of the group open? Seems not. */
1293 opened = atomic_cmpxchg(&group->opened, 0, 1);
1294 if (opened) {
1295 vfio_group_put(group);
1296 return -EBUSY;
1297 }
1298
1299 /* Is something still in use from a previous open? */
cba3345c 1300 if (group->container) {
6d6768c6 1301 atomic_dec(&group->opened);
cba3345c
AW
1302 vfio_group_put(group);
1303 return -EBUSY;
1304 }
1305
1306 filep->private_data = group;
1307
1308 return 0;
1309}
1310
1311static int vfio_group_fops_release(struct inode *inode, struct file *filep)
1312{
1313 struct vfio_group *group = filep->private_data;
1314
1315 filep->private_data = NULL;
1316
1317 vfio_group_try_dissolve_container(group);
1318
6d6768c6
AW
1319 atomic_dec(&group->opened);
1320
cba3345c
AW
1321 vfio_group_put(group);
1322
1323 return 0;
1324}
1325
1326static const struct file_operations vfio_group_fops = {
1327 .owner = THIS_MODULE,
1328 .unlocked_ioctl = vfio_group_fops_unl_ioctl,
1329#ifdef CONFIG_COMPAT
1330 .compat_ioctl = vfio_group_fops_compat_ioctl,
1331#endif
1332 .open = vfio_group_fops_open,
1333 .release = vfio_group_fops_release,
1334};
1335
1336/**
1337 * VFIO Device fd
1338 */
1339static int vfio_device_fops_release(struct inode *inode, struct file *filep)
1340{
1341 struct vfio_device *device = filep->private_data;
1342
1343 device->ops->release(device->device_data);
1344
1345 vfio_group_try_dissolve_container(device->group);
1346
1347 vfio_device_put(device);
1348
1349 return 0;
1350}
1351
1352static long vfio_device_fops_unl_ioctl(struct file *filep,
1353 unsigned int cmd, unsigned long arg)
1354{
1355 struct vfio_device *device = filep->private_data;
1356
1357 if (unlikely(!device->ops->ioctl))
1358 return -EINVAL;
1359
1360 return device->ops->ioctl(device->device_data, cmd, arg);
1361}
1362
1363static ssize_t vfio_device_fops_read(struct file *filep, char __user *buf,
1364 size_t count, loff_t *ppos)
1365{
1366 struct vfio_device *device = filep->private_data;
1367
1368 if (unlikely(!device->ops->read))
1369 return -EINVAL;
1370
1371 return device->ops->read(device->device_data, buf, count, ppos);
1372}
1373
1374static ssize_t vfio_device_fops_write(struct file *filep,
1375 const char __user *buf,
1376 size_t count, loff_t *ppos)
1377{
1378 struct vfio_device *device = filep->private_data;
1379
1380 if (unlikely(!device->ops->write))
1381 return -EINVAL;
1382
1383 return device->ops->write(device->device_data, buf, count, ppos);
1384}
1385
1386static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
1387{
1388 struct vfio_device *device = filep->private_data;
1389
1390 if (unlikely(!device->ops->mmap))
1391 return -EINVAL;
1392
1393 return device->ops->mmap(device->device_data, vma);
1394}
1395
1396#ifdef CONFIG_COMPAT
1397static long vfio_device_fops_compat_ioctl(struct file *filep,
1398 unsigned int cmd, unsigned long arg)
1399{
1400 arg = (unsigned long)compat_ptr(arg);
1401 return vfio_device_fops_unl_ioctl(filep, cmd, arg);
1402}
1403#endif /* CONFIG_COMPAT */
1404
1405static const struct file_operations vfio_device_fops = {
1406 .owner = THIS_MODULE,
1407 .release = vfio_device_fops_release,
1408 .read = vfio_device_fops_read,
1409 .write = vfio_device_fops_write,
1410 .unlocked_ioctl = vfio_device_fops_unl_ioctl,
1411#ifdef CONFIG_COMPAT
1412 .compat_ioctl = vfio_device_fops_compat_ioctl,
1413#endif
1414 .mmap = vfio_device_fops_mmap,
1415};
1416
6cdd9782
AK
1417/**
1418 * External user API, exported by symbols to be linked dynamically.
1419 *
1420 * The protocol includes:
1421 * 1. do normal VFIO init operation:
1422 * - opening a new container;
1423 * - attaching group(s) to it;
1424 * - setting an IOMMU driver for a container.
1425 * When IOMMU is set for a container, all groups in it are
1426 * considered ready to use by an external user.
1427 *
1428 * 2. User space passes a group fd to an external user.
1429 * The external user calls vfio_group_get_external_user()
1430 * to verify that:
1431 * - the group is initialized;
1432 * - IOMMU is set for it.
1433 * If both checks passed, vfio_group_get_external_user()
1434 * increments the container user counter to prevent
1435 * the VFIO group from disposal before KVM exits.
1436 *
1437 * 3. The external user calls vfio_external_user_iommu_id()
1438 * to know an IOMMU ID.
1439 *
1440 * 4. When the external KVM finishes, it calls
1441 * vfio_group_put_external_user() to release the VFIO group.
1442 * This call decrements the container user counter.
1443 */
1444struct vfio_group *vfio_group_get_external_user(struct file *filep)
1445{
1446 struct vfio_group *group = filep->private_data;
1447
1448 if (filep->f_op != &vfio_group_fops)
1449 return ERR_PTR(-EINVAL);
1450
1451 if (!atomic_inc_not_zero(&group->container_users))
1452 return ERR_PTR(-EINVAL);
1453
1454 if (!group->container->iommu_driver ||
1455 !vfio_group_viable(group)) {
1456 atomic_dec(&group->container_users);
1457 return ERR_PTR(-EINVAL);
1458 }
1459
1460 vfio_group_get(group);
1461
1462 return group;
1463}
1464EXPORT_SYMBOL_GPL(vfio_group_get_external_user);
1465
1466void vfio_group_put_external_user(struct vfio_group *group)
1467{
1468 vfio_group_put(group);
1469 vfio_group_try_dissolve_container(group);
1470}
1471EXPORT_SYMBOL_GPL(vfio_group_put_external_user);
1472
1473int vfio_external_user_iommu_id(struct vfio_group *group)
1474{
1475 return iommu_group_id(group->iommu_group);
1476}
1477EXPORT_SYMBOL_GPL(vfio_external_user_iommu_id);
1478
88d7ab89
AW
1479long vfio_external_check_extension(struct vfio_group *group, unsigned long arg)
1480{
1481 return vfio_ioctl_check_extension(group->container, arg);
1482}
1483EXPORT_SYMBOL_GPL(vfio_external_check_extension);
1484
cba3345c
AW
1485/**
1486 * Module/class support
1487 */
1488static char *vfio_devnode(struct device *dev, umode_t *mode)
1489{
1490 return kasprintf(GFP_KERNEL, "vfio/%s", dev_name(dev));
1491}
1492
d1099901
AW
1493static struct miscdevice vfio_dev = {
1494 .minor = VFIO_MINOR,
1495 .name = "vfio",
1496 .fops = &vfio_fops,
1497 .nodename = "vfio/vfio",
1498 .mode = S_IRUGO | S_IWUGO,
1499};
1500
cba3345c
AW
1501static int __init vfio_init(void)
1502{
1503 int ret;
1504
1505 idr_init(&vfio.group_idr);
1506 mutex_init(&vfio.group_lock);
1507 mutex_init(&vfio.iommu_drivers_lock);
1508 INIT_LIST_HEAD(&vfio.group_list);
1509 INIT_LIST_HEAD(&vfio.iommu_drivers_list);
1510 init_waitqueue_head(&vfio.release_q);
1511
d1099901
AW
1512 ret = misc_register(&vfio_dev);
1513 if (ret) {
1514 pr_err("vfio: misc device register failed\n");
1515 return ret;
1516 }
1517
1518 /* /dev/vfio/$GROUP */
cba3345c
AW
1519 vfio.class = class_create(THIS_MODULE, "vfio");
1520 if (IS_ERR(vfio.class)) {
1521 ret = PTR_ERR(vfio.class);
1522 goto err_class;
1523 }
1524
1525 vfio.class->devnode = vfio_devnode;
1526
d1099901 1527 ret = alloc_chrdev_region(&vfio.group_devt, 0, MINORMASK, "vfio");
cba3345c 1528 if (ret)
d1099901 1529 goto err_alloc_chrdev;
cba3345c 1530
cba3345c 1531 cdev_init(&vfio.group_cdev, &vfio_group_fops);
d1099901 1532 ret = cdev_add(&vfio.group_cdev, vfio.group_devt, MINORMASK);
cba3345c 1533 if (ret)
d1099901 1534 goto err_cdev_add;
cba3345c
AW
1535
1536 pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
1537
73fa0d10
AW
1538 /*
1539 * Attempt to load known iommu-drivers. This gives us a working
1540 * environment without the user needing to explicitly load iommu
1541 * drivers.
1542 */
1543 request_module_nowait("vfio_iommu_type1");
5ffd229c 1544 request_module_nowait("vfio_iommu_spapr_tce");
73fa0d10 1545
cba3345c
AW
1546 return 0;
1547
d1099901
AW
1548err_cdev_add:
1549 unregister_chrdev_region(vfio.group_devt, MINORMASK);
1550err_alloc_chrdev:
cba3345c
AW
1551 class_destroy(vfio.class);
1552 vfio.class = NULL;
1553err_class:
d1099901 1554 misc_deregister(&vfio_dev);
cba3345c
AW
1555 return ret;
1556}
1557
1558static void __exit vfio_cleanup(void)
1559{
1560 WARN_ON(!list_empty(&vfio.group_list));
1561
1562 idr_destroy(&vfio.group_idr);
1563 cdev_del(&vfio.group_cdev);
d1099901 1564 unregister_chrdev_region(vfio.group_devt, MINORMASK);
cba3345c
AW
1565 class_destroy(vfio.class);
1566 vfio.class = NULL;
d1099901 1567 misc_deregister(&vfio_dev);
cba3345c
AW
1568}
1569
1570module_init(vfio_init);
1571module_exit(vfio_cleanup);
1572
1573MODULE_VERSION(DRIVER_VERSION);
1574MODULE_LICENSE("GPL v2");
1575MODULE_AUTHOR(DRIVER_AUTHOR);
1576MODULE_DESCRIPTION(DRIVER_DESC);
d1099901
AW
1577MODULE_ALIAS_MISCDEV(VFIO_MINOR);
1578MODULE_ALIAS("devname:vfio/vfio");