blob: ac7423bfaa7d06dc3c4eef4db1ad76b754b4990d [file] [log] [blame]
Alex Williamsoncba33452012-07-31 08:16:22 -06001/*
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>
25#include <linux/module.h>
26#include <linux/mutex.h>
Alex Williamson9587f442013-04-25 16:12:38 -060027#include <linux/rwsem.h>
Alex Williamsoncba33452012-07-31 08:16:22 -060028#include <linux/sched.h>
29#include <linux/slab.h>
30#include <linux/string.h>
31#include <linux/uaccess.h>
32#include <linux/vfio.h>
33#include <linux/wait.h>
34
35#define DRIVER_VERSION "0.3"
36#define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
37#define DRIVER_DESC "VFIO - User Level meta-driver"
38
39static struct vfio {
40 struct class *class;
41 struct list_head iommu_drivers_list;
42 struct mutex iommu_drivers_lock;
43 struct list_head group_list;
44 struct idr group_idr;
45 struct mutex group_lock;
46 struct cdev group_cdev;
47 struct device *dev;
48 dev_t devt;
49 struct cdev cdev;
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;
Alex Williamson9587f442013-04-25 16:12:38 -060061 struct rw_semaphore group_lock;
Alex Williamsoncba33452012-07-31 08:16:22 -060062 struct vfio_iommu_driver *iommu_driver;
63 void *iommu_data;
64};
65
66struct vfio_group {
67 struct kref kref;
68 int minor;
69 atomic_t container_users;
70 struct iommu_group *iommu_group;
71 struct vfio_container *container;
72 struct list_head device_list;
73 struct mutex device_lock;
74 struct device *dev;
75 struct notifier_block nb;
76 struct list_head vfio_next;
77 struct list_head container_next;
78};
79
80struct vfio_device {
81 struct kref kref;
82 struct device *dev;
83 const struct vfio_device_ops *ops;
84 struct vfio_group *group;
85 struct list_head group_next;
86 void *device_data;
87};
88
89/**
90 * IOMMU driver registration
91 */
92int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops)
93{
94 struct vfio_iommu_driver *driver, *tmp;
95
96 driver = kzalloc(sizeof(*driver), GFP_KERNEL);
97 if (!driver)
98 return -ENOMEM;
99
100 driver->ops = ops;
101
102 mutex_lock(&vfio.iommu_drivers_lock);
103
104 /* Check for duplicates */
105 list_for_each_entry(tmp, &vfio.iommu_drivers_list, vfio_next) {
106 if (tmp->ops == ops) {
107 mutex_unlock(&vfio.iommu_drivers_lock);
108 kfree(driver);
109 return -EINVAL;
110 }
111 }
112
113 list_add(&driver->vfio_next, &vfio.iommu_drivers_list);
114
115 mutex_unlock(&vfio.iommu_drivers_lock);
116
117 return 0;
118}
119EXPORT_SYMBOL_GPL(vfio_register_iommu_driver);
120
121void vfio_unregister_iommu_driver(const struct vfio_iommu_driver_ops *ops)
122{
123 struct vfio_iommu_driver *driver;
124
125 mutex_lock(&vfio.iommu_drivers_lock);
126 list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
127 if (driver->ops == ops) {
128 list_del(&driver->vfio_next);
129 mutex_unlock(&vfio.iommu_drivers_lock);
130 kfree(driver);
131 return;
132 }
133 }
134 mutex_unlock(&vfio.iommu_drivers_lock);
135}
136EXPORT_SYMBOL_GPL(vfio_unregister_iommu_driver);
137
138/**
139 * Group minor allocation/free - both called with vfio.group_lock held
140 */
141static int vfio_alloc_group_minor(struct vfio_group *group)
142{
Alex Williamsoncba33452012-07-31 08:16:22 -0600143 /* index 0 is used by /dev/vfio/vfio */
Tejun Heoa1c36b12013-02-27 17:04:48 -0800144 return idr_alloc(&vfio.group_idr, group, 1, MINORMASK + 1, GFP_KERNEL);
Alex Williamsoncba33452012-07-31 08:16:22 -0600145}
146
147static void vfio_free_group_minor(int minor)
148{
149 idr_remove(&vfio.group_idr, minor);
150}
151
152static int vfio_iommu_group_notifier(struct notifier_block *nb,
153 unsigned long action, void *data);
154static void vfio_group_get(struct vfio_group *group);
155
156/**
157 * Container objects - containers are created when /dev/vfio/vfio is
158 * opened, but their lifecycle extends until the last user is done, so
159 * it's freed via kref. Must support container/group/device being
160 * closed in any order.
161 */
162static void vfio_container_get(struct vfio_container *container)
163{
164 kref_get(&container->kref);
165}
166
167static void vfio_container_release(struct kref *kref)
168{
169 struct vfio_container *container;
170 container = container_of(kref, struct vfio_container, kref);
171
172 kfree(container);
173}
174
175static void vfio_container_put(struct vfio_container *container)
176{
177 kref_put(&container->kref, vfio_container_release);
178}
179
Jiang Liu9df7b252012-12-07 13:43:50 -0700180static void vfio_group_unlock_and_free(struct vfio_group *group)
181{
182 mutex_unlock(&vfio.group_lock);
183 /*
184 * Unregister outside of lock. A spurious callback is harmless now
185 * that the group is no longer in vfio.group_list.
186 */
187 iommu_group_unregister_notifier(group->iommu_group, &group->nb);
188 kfree(group);
189}
190
Alex Williamsoncba33452012-07-31 08:16:22 -0600191/**
192 * Group objects - create, release, get, put, search
193 */
194static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group)
195{
196 struct vfio_group *group, *tmp;
197 struct device *dev;
198 int ret, minor;
199
200 group = kzalloc(sizeof(*group), GFP_KERNEL);
201 if (!group)
202 return ERR_PTR(-ENOMEM);
203
204 kref_init(&group->kref);
205 INIT_LIST_HEAD(&group->device_list);
206 mutex_init(&group->device_lock);
207 atomic_set(&group->container_users, 0);
208 group->iommu_group = iommu_group;
209
210 group->nb.notifier_call = vfio_iommu_group_notifier;
211
212 /*
213 * blocking notifiers acquire a rwsem around registering and hold
214 * it around callback. Therefore, need to register outside of
215 * vfio.group_lock to avoid A-B/B-A contention. Our callback won't
216 * do anything unless it can find the group in vfio.group_list, so
217 * no harm in registering early.
218 */
219 ret = iommu_group_register_notifier(iommu_group, &group->nb);
220 if (ret) {
221 kfree(group);
222 return ERR_PTR(ret);
223 }
224
225 mutex_lock(&vfio.group_lock);
226
227 minor = vfio_alloc_group_minor(group);
228 if (minor < 0) {
Jiang Liu9df7b252012-12-07 13:43:50 -0700229 vfio_group_unlock_and_free(group);
Alex Williamsoncba33452012-07-31 08:16:22 -0600230 return ERR_PTR(minor);
231 }
232
233 /* Did we race creating this group? */
234 list_for_each_entry(tmp, &vfio.group_list, vfio_next) {
235 if (tmp->iommu_group == iommu_group) {
236 vfio_group_get(tmp);
237 vfio_free_group_minor(minor);
Jiang Liu9df7b252012-12-07 13:43:50 -0700238 vfio_group_unlock_and_free(group);
Alex Williamsoncba33452012-07-31 08:16:22 -0600239 return tmp;
240 }
241 }
242
243 dev = device_create(vfio.class, NULL, MKDEV(MAJOR(vfio.devt), minor),
244 group, "%d", iommu_group_id(iommu_group));
245 if (IS_ERR(dev)) {
246 vfio_free_group_minor(minor);
Jiang Liu9df7b252012-12-07 13:43:50 -0700247 vfio_group_unlock_and_free(group);
Alex Williamsoncba33452012-07-31 08:16:22 -0600248 return (struct vfio_group *)dev; /* ERR_PTR */
249 }
250
251 group->minor = minor;
252 group->dev = dev;
253
254 list_add(&group->vfio_next, &vfio.group_list);
255
256 mutex_unlock(&vfio.group_lock);
257
258 return group;
259}
260
Al Viro6d2cd3c2012-08-17 21:27:32 -0400261/* called with vfio.group_lock held */
Alex Williamsoncba33452012-07-31 08:16:22 -0600262static void vfio_group_release(struct kref *kref)
263{
264 struct vfio_group *group = container_of(kref, struct vfio_group, kref);
265
266 WARN_ON(!list_empty(&group->device_list));
267
268 device_destroy(vfio.class, MKDEV(MAJOR(vfio.devt), group->minor));
269 list_del(&group->vfio_next);
270 vfio_free_group_minor(group->minor);
Jiang Liu9df7b252012-12-07 13:43:50 -0700271 vfio_group_unlock_and_free(group);
Alex Williamsoncba33452012-07-31 08:16:22 -0600272}
273
274static void vfio_group_put(struct vfio_group *group)
275{
Al Viro6d2cd3c2012-08-17 21:27:32 -0400276 kref_put_mutex(&group->kref, vfio_group_release, &vfio.group_lock);
Alex Williamsoncba33452012-07-31 08:16:22 -0600277}
278
279/* Assume group_lock or group reference is held */
280static void vfio_group_get(struct vfio_group *group)
281{
282 kref_get(&group->kref);
283}
284
285/*
286 * Not really a try as we will sleep for mutex, but we need to make
287 * sure the group pointer is valid under lock and get a reference.
288 */
289static struct vfio_group *vfio_group_try_get(struct vfio_group *group)
290{
291 struct vfio_group *target = group;
292
293 mutex_lock(&vfio.group_lock);
294 list_for_each_entry(group, &vfio.group_list, vfio_next) {
295 if (group == target) {
296 vfio_group_get(group);
297 mutex_unlock(&vfio.group_lock);
298 return group;
299 }
300 }
301 mutex_unlock(&vfio.group_lock);
302
303 return NULL;
304}
305
306static
307struct vfio_group *vfio_group_get_from_iommu(struct iommu_group *iommu_group)
308{
309 struct vfio_group *group;
310
311 mutex_lock(&vfio.group_lock);
312 list_for_each_entry(group, &vfio.group_list, vfio_next) {
313 if (group->iommu_group == iommu_group) {
314 vfio_group_get(group);
315 mutex_unlock(&vfio.group_lock);
316 return group;
317 }
318 }
319 mutex_unlock(&vfio.group_lock);
320
321 return NULL;
322}
323
324static struct vfio_group *vfio_group_get_from_minor(int minor)
325{
326 struct vfio_group *group;
327
328 mutex_lock(&vfio.group_lock);
329 group = idr_find(&vfio.group_idr, minor);
330 if (!group) {
331 mutex_unlock(&vfio.group_lock);
332 return NULL;
333 }
334 vfio_group_get(group);
335 mutex_unlock(&vfio.group_lock);
336
337 return group;
338}
339
340/**
341 * Device objects - create, release, get, put, search
342 */
343static
344struct vfio_device *vfio_group_create_device(struct vfio_group *group,
345 struct device *dev,
346 const struct vfio_device_ops *ops,
347 void *device_data)
348{
349 struct vfio_device *device;
350 int ret;
351
352 device = kzalloc(sizeof(*device), GFP_KERNEL);
353 if (!device)
354 return ERR_PTR(-ENOMEM);
355
356 kref_init(&device->kref);
357 device->dev = dev;
358 device->group = group;
359 device->ops = ops;
360 device->device_data = device_data;
361
362 ret = dev_set_drvdata(dev, device);
363 if (ret) {
364 kfree(device);
365 return ERR_PTR(ret);
366 }
367
368 /* No need to get group_lock, caller has group reference */
369 vfio_group_get(group);
370
371 mutex_lock(&group->device_lock);
372 list_add(&device->group_next, &group->device_list);
373 mutex_unlock(&group->device_lock);
374
375 return device;
376}
377
378static void vfio_device_release(struct kref *kref)
379{
380 struct vfio_device *device = container_of(kref,
381 struct vfio_device, kref);
382 struct vfio_group *group = device->group;
383
Alex Williamsoncba33452012-07-31 08:16:22 -0600384 list_del(&device->group_next);
385 mutex_unlock(&group->device_lock);
386
387 dev_set_drvdata(device->dev, NULL);
388
389 kfree(device);
390
391 /* vfio_del_group_dev may be waiting for this device */
392 wake_up(&vfio.release_q);
393}
394
395/* Device reference always implies a group reference */
Vijay Mohan Pandarathil44f50712013-03-11 09:28:44 -0600396void vfio_device_put(struct vfio_device *device)
Alex Williamsoncba33452012-07-31 08:16:22 -0600397{
Al Viro934ad4c2012-08-17 19:49:09 -0400398 struct vfio_group *group = device->group;
Al Viro90b12532012-08-17 21:29:06 -0400399 kref_put_mutex(&device->kref, vfio_device_release, &group->device_lock);
Al Viro934ad4c2012-08-17 19:49:09 -0400400 vfio_group_put(group);
Alex Williamsoncba33452012-07-31 08:16:22 -0600401}
Vijay Mohan Pandarathil44f50712013-03-11 09:28:44 -0600402EXPORT_SYMBOL_GPL(vfio_device_put);
Alex Williamsoncba33452012-07-31 08:16:22 -0600403
404static void vfio_device_get(struct vfio_device *device)
405{
406 vfio_group_get(device->group);
407 kref_get(&device->kref);
408}
409
410static struct vfio_device *vfio_group_get_device(struct vfio_group *group,
411 struct device *dev)
412{
413 struct vfio_device *device;
414
415 mutex_lock(&group->device_lock);
416 list_for_each_entry(device, &group->device_list, group_next) {
417 if (device->dev == dev) {
418 vfio_device_get(device);
419 mutex_unlock(&group->device_lock);
420 return device;
421 }
422 }
423 mutex_unlock(&group->device_lock);
424 return NULL;
425}
426
427/*
428 * Whitelist some drivers that we know are safe (no dma) or just sit on
429 * a device. It's not always practical to leave a device within a group
430 * driverless as it could get re-bound to something unsafe.
431 */
Alex Williamson2b489a42013-02-14 14:02:13 -0700432static const char * const vfio_driver_whitelist[] = { "pci-stub", "pcieport" };
Alex Williamsoncba33452012-07-31 08:16:22 -0600433
434static bool vfio_whitelisted_driver(struct device_driver *drv)
435{
436 int i;
437
438 for (i = 0; i < ARRAY_SIZE(vfio_driver_whitelist); i++) {
439 if (!strcmp(drv->name, vfio_driver_whitelist[i]))
440 return true;
441 }
442
443 return false;
444}
445
446/*
447 * A vfio group is viable for use by userspace if all devices are either
448 * driver-less or bound to a vfio or whitelisted driver. We test the
449 * latter by the existence of a struct vfio_device matching the dev.
450 */
451static int vfio_dev_viable(struct device *dev, void *data)
452{
453 struct vfio_group *group = data;
454 struct vfio_device *device;
Jiang Liude2b3ee2012-12-07 13:43:50 -0700455 struct device_driver *drv = ACCESS_ONCE(dev->driver);
Alex Williamsoncba33452012-07-31 08:16:22 -0600456
Jiang Liude2b3ee2012-12-07 13:43:50 -0700457 if (!drv || vfio_whitelisted_driver(drv))
Alex Williamsoncba33452012-07-31 08:16:22 -0600458 return 0;
459
460 device = vfio_group_get_device(group, dev);
461 if (device) {
462 vfio_device_put(device);
463 return 0;
464 }
465
466 return -EINVAL;
467}
468
469/**
470 * Async device support
471 */
472static int vfio_group_nb_add_dev(struct vfio_group *group, struct device *dev)
473{
474 struct vfio_device *device;
475
476 /* Do we already know about it? We shouldn't */
477 device = vfio_group_get_device(group, dev);
478 if (WARN_ON_ONCE(device)) {
479 vfio_device_put(device);
480 return 0;
481 }
482
483 /* Nothing to do for idle groups */
484 if (!atomic_read(&group->container_users))
485 return 0;
486
487 /* TODO Prevent device auto probing */
488 WARN("Device %s added to live group %d!\n", dev_name(dev),
489 iommu_group_id(group->iommu_group));
490
491 return 0;
492}
493
494static int vfio_group_nb_del_dev(struct vfio_group *group, struct device *dev)
495{
496 struct vfio_device *device;
497
498 /*
499 * Expect to fall out here. If a device was in use, it would
500 * have been bound to a vfio sub-driver, which would have blocked
501 * in .remove at vfio_del_group_dev. Sanity check that we no
502 * longer track the device, so it's safe to remove.
503 */
504 device = vfio_group_get_device(group, dev);
505 if (likely(!device))
506 return 0;
507
508 WARN("Device %s removed from live group %d!\n", dev_name(dev),
509 iommu_group_id(group->iommu_group));
510
511 vfio_device_put(device);
512 return 0;
513}
514
515static int vfio_group_nb_verify(struct vfio_group *group, struct device *dev)
516{
517 /* We don't care what happens when the group isn't in use */
518 if (!atomic_read(&group->container_users))
519 return 0;
520
521 return vfio_dev_viable(dev, group);
522}
523
524static int vfio_iommu_group_notifier(struct notifier_block *nb,
525 unsigned long action, void *data)
526{
527 struct vfio_group *group = container_of(nb, struct vfio_group, nb);
528 struct device *dev = data;
529
530 /*
531 * Need to go through a group_lock lookup to get a reference or
532 * we risk racing a group being removed. Leave a WARN_ON for
533 * debuging, but if the group no longer exists, a spurious notify
534 * is harmless.
535 */
536 group = vfio_group_try_get(group);
537 if (WARN_ON(!group))
538 return NOTIFY_OK;
539
540 switch (action) {
541 case IOMMU_GROUP_NOTIFY_ADD_DEVICE:
542 vfio_group_nb_add_dev(group, dev);
543 break;
544 case IOMMU_GROUP_NOTIFY_DEL_DEVICE:
545 vfio_group_nb_del_dev(group, dev);
546 break;
547 case IOMMU_GROUP_NOTIFY_BIND_DRIVER:
548 pr_debug("%s: Device %s, group %d binding to driver\n",
549 __func__, dev_name(dev),
550 iommu_group_id(group->iommu_group));
551 break;
552 case IOMMU_GROUP_NOTIFY_BOUND_DRIVER:
553 pr_debug("%s: Device %s, group %d bound to driver %s\n",
554 __func__, dev_name(dev),
555 iommu_group_id(group->iommu_group), dev->driver->name);
556 BUG_ON(vfio_group_nb_verify(group, dev));
557 break;
558 case IOMMU_GROUP_NOTIFY_UNBIND_DRIVER:
559 pr_debug("%s: Device %s, group %d unbinding from driver %s\n",
560 __func__, dev_name(dev),
561 iommu_group_id(group->iommu_group), dev->driver->name);
562 break;
563 case IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER:
564 pr_debug("%s: Device %s, group %d unbound from driver\n",
565 __func__, dev_name(dev),
566 iommu_group_id(group->iommu_group));
567 /*
568 * XXX An unbound device in a live group is ok, but we'd
569 * really like to avoid the above BUG_ON by preventing other
570 * drivers from binding to it. Once that occurs, we have to
571 * stop the system to maintain isolation. At a minimum, we'd
572 * want a toggle to disable driver auto probe for this device.
573 */
574 break;
575 }
576
577 vfio_group_put(group);
578 return NOTIFY_OK;
579}
580
581/**
582 * VFIO driver API
583 */
584int vfio_add_group_dev(struct device *dev,
585 const struct vfio_device_ops *ops, void *device_data)
586{
587 struct iommu_group *iommu_group;
588 struct vfio_group *group;
589 struct vfio_device *device;
590
591 iommu_group = iommu_group_get(dev);
592 if (!iommu_group)
593 return -EINVAL;
594
595 group = vfio_group_get_from_iommu(iommu_group);
596 if (!group) {
597 group = vfio_create_group(iommu_group);
598 if (IS_ERR(group)) {
599 iommu_group_put(iommu_group);
600 return PTR_ERR(group);
601 }
602 }
603
604 device = vfio_group_get_device(group, dev);
605 if (device) {
606 WARN(1, "Device %s already exists on group %d\n",
607 dev_name(dev), iommu_group_id(iommu_group));
608 vfio_device_put(device);
609 vfio_group_put(group);
610 iommu_group_put(iommu_group);
611 return -EBUSY;
612 }
613
614 device = vfio_group_create_device(group, dev, ops, device_data);
615 if (IS_ERR(device)) {
616 vfio_group_put(group);
617 iommu_group_put(iommu_group);
618 return PTR_ERR(device);
619 }
620
621 /*
622 * Added device holds reference to iommu_group and vfio_device
623 * (which in turn holds reference to vfio_group). Drop extra
624 * group reference used while acquiring device.
625 */
626 vfio_group_put(group);
627
628 return 0;
629}
630EXPORT_SYMBOL_GPL(vfio_add_group_dev);
631
Vijay Mohan Pandarathil44f50712013-03-11 09:28:44 -0600632/**
633 * Get a reference to the vfio_device for a device that is known to
634 * be bound to a vfio driver. The driver implicitly holds a
635 * vfio_device reference between vfio_add_group_dev and
636 * vfio_del_group_dev. We can therefore use drvdata to increment
637 * that reference from the struct device. This additional
638 * reference must be released by calling vfio_device_put.
639 */
640struct vfio_device *vfio_device_get_from_dev(struct device *dev)
641{
642 struct vfio_device *device = dev_get_drvdata(dev);
643
644 vfio_device_get(device);
645
646 return device;
647}
648EXPORT_SYMBOL_GPL(vfio_device_get_from_dev);
649
650/*
651 * Caller must hold a reference to the vfio_device
652 */
653void *vfio_device_data(struct vfio_device *device)
654{
655 return device->device_data;
656}
657EXPORT_SYMBOL_GPL(vfio_device_data);
658
Alex Williamsone014e942013-02-14 14:02:13 -0700659/* Given a referenced group, check if it contains the device */
660static bool vfio_dev_present(struct vfio_group *group, struct device *dev)
Alex Williamsoncba33452012-07-31 08:16:22 -0600661{
Alex Williamsoncba33452012-07-31 08:16:22 -0600662 struct vfio_device *device;
663
Alex Williamsoncba33452012-07-31 08:16:22 -0600664 device = vfio_group_get_device(group, dev);
Alex Williamsone014e942013-02-14 14:02:13 -0700665 if (!device)
Alex Williamsoncba33452012-07-31 08:16:22 -0600666 return false;
Alex Williamsoncba33452012-07-31 08:16:22 -0600667
668 vfio_device_put(device);
Alex Williamsoncba33452012-07-31 08:16:22 -0600669 return true;
670}
671
672/*
673 * Decrement the device reference count and wait for the device to be
674 * removed. Open file descriptors for the device... */
675void *vfio_del_group_dev(struct device *dev)
676{
677 struct vfio_device *device = dev_get_drvdata(dev);
678 struct vfio_group *group = device->group;
679 struct iommu_group *iommu_group = group->iommu_group;
680 void *device_data = device->device_data;
681
Alex Williamsone014e942013-02-14 14:02:13 -0700682 /*
683 * The group exists so long as we have a device reference. Get
684 * a group reference and use it to scan for the device going away.
685 */
686 vfio_group_get(group);
687
Alex Williamsoncba33452012-07-31 08:16:22 -0600688 vfio_device_put(device);
689
690 /* TODO send a signal to encourage this to be released */
Alex Williamsone014e942013-02-14 14:02:13 -0700691 wait_event(vfio.release_q, !vfio_dev_present(group, dev));
692
693 vfio_group_put(group);
Alex Williamsoncba33452012-07-31 08:16:22 -0600694
695 iommu_group_put(iommu_group);
696
697 return device_data;
698}
699EXPORT_SYMBOL_GPL(vfio_del_group_dev);
700
701/**
702 * VFIO base fd, /dev/vfio/vfio
703 */
704static long vfio_ioctl_check_extension(struct vfio_container *container,
705 unsigned long arg)
706{
Alex Williamson0b43c082013-04-29 08:41:36 -0600707 struct vfio_iommu_driver *driver;
Alex Williamsoncba33452012-07-31 08:16:22 -0600708 long ret = 0;
709
Alex Williamson0b43c082013-04-29 08:41:36 -0600710 down_read(&container->group_lock);
711
712 driver = container->iommu_driver;
713
Alex Williamsoncba33452012-07-31 08:16:22 -0600714 switch (arg) {
715 /* No base extensions yet */
716 default:
717 /*
718 * If no driver is set, poll all registered drivers for
719 * extensions and return the first positive result. If
720 * a driver is already set, further queries will be passed
721 * only to that driver.
722 */
723 if (!driver) {
724 mutex_lock(&vfio.iommu_drivers_lock);
725 list_for_each_entry(driver, &vfio.iommu_drivers_list,
726 vfio_next) {
727 if (!try_module_get(driver->ops->owner))
728 continue;
729
730 ret = driver->ops->ioctl(NULL,
731 VFIO_CHECK_EXTENSION,
732 arg);
733 module_put(driver->ops->owner);
734 if (ret > 0)
735 break;
736 }
737 mutex_unlock(&vfio.iommu_drivers_lock);
738 } else
739 ret = driver->ops->ioctl(container->iommu_data,
740 VFIO_CHECK_EXTENSION, arg);
741 }
742
Alex Williamson0b43c082013-04-29 08:41:36 -0600743 up_read(&container->group_lock);
744
Alex Williamsoncba33452012-07-31 08:16:22 -0600745 return ret;
746}
747
Alex Williamson9587f442013-04-25 16:12:38 -0600748/* hold write lock on container->group_lock */
Alex Williamsoncba33452012-07-31 08:16:22 -0600749static int __vfio_container_attach_groups(struct vfio_container *container,
750 struct vfio_iommu_driver *driver,
751 void *data)
752{
753 struct vfio_group *group;
754 int ret = -ENODEV;
755
756 list_for_each_entry(group, &container->group_list, container_next) {
757 ret = driver->ops->attach_group(data, group->iommu_group);
758 if (ret)
759 goto unwind;
760 }
761
762 return ret;
763
764unwind:
765 list_for_each_entry_continue_reverse(group, &container->group_list,
766 container_next) {
767 driver->ops->detach_group(data, group->iommu_group);
768 }
769
770 return ret;
771}
772
773static long vfio_ioctl_set_iommu(struct vfio_container *container,
774 unsigned long arg)
775{
776 struct vfio_iommu_driver *driver;
777 long ret = -ENODEV;
778
Alex Williamson9587f442013-04-25 16:12:38 -0600779 down_write(&container->group_lock);
Alex Williamsoncba33452012-07-31 08:16:22 -0600780
781 /*
782 * The container is designed to be an unprivileged interface while
783 * the group can be assigned to specific users. Therefore, only by
784 * adding a group to a container does the user get the privilege of
785 * enabling the iommu, which may allocate finite resources. There
786 * is no unset_iommu, but by removing all the groups from a container,
787 * the container is deprivileged and returns to an unset state.
788 */
789 if (list_empty(&container->group_list) || container->iommu_driver) {
Alex Williamson9587f442013-04-25 16:12:38 -0600790 up_write(&container->group_lock);
Alex Williamsoncba33452012-07-31 08:16:22 -0600791 return -EINVAL;
792 }
793
794 mutex_lock(&vfio.iommu_drivers_lock);
795 list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
796 void *data;
797
798 if (!try_module_get(driver->ops->owner))
799 continue;
800
801 /*
802 * The arg magic for SET_IOMMU is the same as CHECK_EXTENSION,
803 * so test which iommu driver reported support for this
804 * extension and call open on them. We also pass them the
805 * magic, allowing a single driver to support multiple
806 * interfaces if they'd like.
807 */
808 if (driver->ops->ioctl(NULL, VFIO_CHECK_EXTENSION, arg) <= 0) {
809 module_put(driver->ops->owner);
810 continue;
811 }
812
813 /* module reference holds the driver we're working on */
814 mutex_unlock(&vfio.iommu_drivers_lock);
815
816 data = driver->ops->open(arg);
817 if (IS_ERR(data)) {
818 ret = PTR_ERR(data);
819 module_put(driver->ops->owner);
820 goto skip_drivers_unlock;
821 }
822
823 ret = __vfio_container_attach_groups(container, driver, data);
824 if (!ret) {
825 container->iommu_driver = driver;
826 container->iommu_data = data;
827 } else {
828 driver->ops->release(data);
829 module_put(driver->ops->owner);
830 }
831
832 goto skip_drivers_unlock;
833 }
834
835 mutex_unlock(&vfio.iommu_drivers_lock);
836skip_drivers_unlock:
Alex Williamson9587f442013-04-25 16:12:38 -0600837 up_write(&container->group_lock);
Alex Williamsoncba33452012-07-31 08:16:22 -0600838
839 return ret;
840}
841
842static long vfio_fops_unl_ioctl(struct file *filep,
843 unsigned int cmd, unsigned long arg)
844{
845 struct vfio_container *container = filep->private_data;
846 struct vfio_iommu_driver *driver;
847 void *data;
848 long ret = -EINVAL;
849
850 if (!container)
851 return ret;
852
Alex Williamsoncba33452012-07-31 08:16:22 -0600853 switch (cmd) {
854 case VFIO_GET_API_VERSION:
855 ret = VFIO_API_VERSION;
856 break;
857 case VFIO_CHECK_EXTENSION:
858 ret = vfio_ioctl_check_extension(container, arg);
859 break;
860 case VFIO_SET_IOMMU:
861 ret = vfio_ioctl_set_iommu(container, arg);
862 break;
863 default:
Alex Williamson0b43c082013-04-29 08:41:36 -0600864 down_read(&container->group_lock);
865
866 driver = container->iommu_driver;
867 data = container->iommu_data;
868
Alex Williamsoncba33452012-07-31 08:16:22 -0600869 if (driver) /* passthrough all unrecognized ioctls */
870 ret = driver->ops->ioctl(data, cmd, arg);
Alex Williamson0b43c082013-04-29 08:41:36 -0600871
872 up_read(&container->group_lock);
Alex Williamsoncba33452012-07-31 08:16:22 -0600873 }
874
875 return ret;
876}
877
878#ifdef CONFIG_COMPAT
879static long vfio_fops_compat_ioctl(struct file *filep,
880 unsigned int cmd, unsigned long arg)
881{
882 arg = (unsigned long)compat_ptr(arg);
883 return vfio_fops_unl_ioctl(filep, cmd, arg);
884}
885#endif /* CONFIG_COMPAT */
886
887static int vfio_fops_open(struct inode *inode, struct file *filep)
888{
889 struct vfio_container *container;
890
891 container = kzalloc(sizeof(*container), GFP_KERNEL);
892 if (!container)
893 return -ENOMEM;
894
895 INIT_LIST_HEAD(&container->group_list);
Alex Williamson9587f442013-04-25 16:12:38 -0600896 init_rwsem(&container->group_lock);
Alex Williamsoncba33452012-07-31 08:16:22 -0600897 kref_init(&container->kref);
898
899 filep->private_data = container;
900
901 return 0;
902}
903
904static int vfio_fops_release(struct inode *inode, struct file *filep)
905{
906 struct vfio_container *container = filep->private_data;
907
908 filep->private_data = NULL;
909
910 vfio_container_put(container);
911
912 return 0;
913}
914
915/*
916 * Once an iommu driver is set, we optionally pass read/write/mmap
917 * on to the driver, allowing management interfaces beyond ioctl.
918 */
919static ssize_t vfio_fops_read(struct file *filep, char __user *buf,
920 size_t count, loff_t *ppos)
921{
922 struct vfio_container *container = filep->private_data;
Alex Williamson0b43c082013-04-29 08:41:36 -0600923 struct vfio_iommu_driver *driver;
924 ssize_t ret = -EINVAL;
Alex Williamsoncba33452012-07-31 08:16:22 -0600925
Alex Williamson0b43c082013-04-29 08:41:36 -0600926 down_read(&container->group_lock);
Alex Williamsoncba33452012-07-31 08:16:22 -0600927
Alex Williamson0b43c082013-04-29 08:41:36 -0600928 driver = container->iommu_driver;
929 if (likely(driver && driver->ops->read))
930 ret = driver->ops->read(container->iommu_data,
931 buf, count, ppos);
932
933 up_read(&container->group_lock);
934
935 return ret;
Alex Williamsoncba33452012-07-31 08:16:22 -0600936}
937
938static ssize_t vfio_fops_write(struct file *filep, const char __user *buf,
939 size_t count, loff_t *ppos)
940{
941 struct vfio_container *container = filep->private_data;
Alex Williamson0b43c082013-04-29 08:41:36 -0600942 struct vfio_iommu_driver *driver;
943 ssize_t ret = -EINVAL;
Alex Williamsoncba33452012-07-31 08:16:22 -0600944
Alex Williamson0b43c082013-04-29 08:41:36 -0600945 down_read(&container->group_lock);
Alex Williamsoncba33452012-07-31 08:16:22 -0600946
Alex Williamson0b43c082013-04-29 08:41:36 -0600947 driver = container->iommu_driver;
948 if (likely(driver && driver->ops->write))
949 ret = driver->ops->write(container->iommu_data,
950 buf, count, ppos);
951
952 up_read(&container->group_lock);
953
954 return ret;
Alex Williamsoncba33452012-07-31 08:16:22 -0600955}
956
957static int vfio_fops_mmap(struct file *filep, struct vm_area_struct *vma)
958{
959 struct vfio_container *container = filep->private_data;
Alex Williamson0b43c082013-04-29 08:41:36 -0600960 struct vfio_iommu_driver *driver;
961 int ret = -EINVAL;
Alex Williamsoncba33452012-07-31 08:16:22 -0600962
Alex Williamson0b43c082013-04-29 08:41:36 -0600963 down_read(&container->group_lock);
Alex Williamsoncba33452012-07-31 08:16:22 -0600964
Alex Williamson0b43c082013-04-29 08:41:36 -0600965 driver = container->iommu_driver;
966 if (likely(driver && driver->ops->mmap))
967 ret = driver->ops->mmap(container->iommu_data, vma);
968
969 up_read(&container->group_lock);
970
971 return ret;
Alex Williamsoncba33452012-07-31 08:16:22 -0600972}
973
974static const struct file_operations vfio_fops = {
975 .owner = THIS_MODULE,
976 .open = vfio_fops_open,
977 .release = vfio_fops_release,
978 .read = vfio_fops_read,
979 .write = vfio_fops_write,
980 .unlocked_ioctl = vfio_fops_unl_ioctl,
981#ifdef CONFIG_COMPAT
982 .compat_ioctl = vfio_fops_compat_ioctl,
983#endif
984 .mmap = vfio_fops_mmap,
985};
986
987/**
988 * VFIO Group fd, /dev/vfio/$GROUP
989 */
990static void __vfio_group_unset_container(struct vfio_group *group)
991{
992 struct vfio_container *container = group->container;
993 struct vfio_iommu_driver *driver;
994
Alex Williamson9587f442013-04-25 16:12:38 -0600995 down_write(&container->group_lock);
Alex Williamsoncba33452012-07-31 08:16:22 -0600996
997 driver = container->iommu_driver;
998 if (driver)
999 driver->ops->detach_group(container->iommu_data,
1000 group->iommu_group);
1001
1002 group->container = NULL;
1003 list_del(&group->container_next);
1004
1005 /* Detaching the last group deprivileges a container, remove iommu */
1006 if (driver && list_empty(&container->group_list)) {
1007 driver->ops->release(container->iommu_data);
1008 module_put(driver->ops->owner);
1009 container->iommu_driver = NULL;
1010 container->iommu_data = NULL;
1011 }
1012
Alex Williamson9587f442013-04-25 16:12:38 -06001013 up_write(&container->group_lock);
Alex Williamsoncba33452012-07-31 08:16:22 -06001014
1015 vfio_container_put(container);
1016}
1017
1018/*
1019 * VFIO_GROUP_UNSET_CONTAINER should fail if there are other users or
1020 * if there was no container to unset. Since the ioctl is called on
1021 * the group, we know that still exists, therefore the only valid
1022 * transition here is 1->0.
1023 */
1024static int vfio_group_unset_container(struct vfio_group *group)
1025{
1026 int users = atomic_cmpxchg(&group->container_users, 1, 0);
1027
1028 if (!users)
1029 return -EINVAL;
1030 if (users != 1)
1031 return -EBUSY;
1032
1033 __vfio_group_unset_container(group);
1034
1035 return 0;
1036}
1037
1038/*
1039 * When removing container users, anything that removes the last user
1040 * implicitly removes the group from the container. That is, if the
1041 * group file descriptor is closed, as well as any device file descriptors,
1042 * the group is free.
1043 */
1044static void vfio_group_try_dissolve_container(struct vfio_group *group)
1045{
1046 if (0 == atomic_dec_if_positive(&group->container_users))
1047 __vfio_group_unset_container(group);
1048}
1049
1050static int vfio_group_set_container(struct vfio_group *group, int container_fd)
1051{
Al Viro2903ff02012-08-28 12:52:22 -04001052 struct fd f;
Alex Williamsoncba33452012-07-31 08:16:22 -06001053 struct vfio_container *container;
1054 struct vfio_iommu_driver *driver;
Al Viro2903ff02012-08-28 12:52:22 -04001055 int ret = 0;
Alex Williamsoncba33452012-07-31 08:16:22 -06001056
1057 if (atomic_read(&group->container_users))
1058 return -EINVAL;
1059
Al Viro2903ff02012-08-28 12:52:22 -04001060 f = fdget(container_fd);
1061 if (!f.file)
Alex Williamsoncba33452012-07-31 08:16:22 -06001062 return -EBADF;
1063
1064 /* Sanity check, is this really our fd? */
Al Viro2903ff02012-08-28 12:52:22 -04001065 if (f.file->f_op != &vfio_fops) {
1066 fdput(f);
Alex Williamsoncba33452012-07-31 08:16:22 -06001067 return -EINVAL;
1068 }
1069
Al Viro2903ff02012-08-28 12:52:22 -04001070 container = f.file->private_data;
Alex Williamsoncba33452012-07-31 08:16:22 -06001071 WARN_ON(!container); /* fget ensures we don't race vfio_release */
1072
Alex Williamson9587f442013-04-25 16:12:38 -06001073 down_write(&container->group_lock);
Alex Williamsoncba33452012-07-31 08:16:22 -06001074
1075 driver = container->iommu_driver;
1076 if (driver) {
1077 ret = driver->ops->attach_group(container->iommu_data,
1078 group->iommu_group);
1079 if (ret)
1080 goto unlock_out;
1081 }
1082
1083 group->container = container;
1084 list_add(&group->container_next, &container->group_list);
1085
1086 /* Get a reference on the container and mark a user within the group */
1087 vfio_container_get(container);
1088 atomic_inc(&group->container_users);
1089
1090unlock_out:
Alex Williamson9587f442013-04-25 16:12:38 -06001091 up_write(&container->group_lock);
Al Viro2903ff02012-08-28 12:52:22 -04001092 fdput(f);
Alex Williamsoncba33452012-07-31 08:16:22 -06001093 return ret;
1094}
1095
1096static bool vfio_group_viable(struct vfio_group *group)
1097{
1098 return (iommu_group_for_each_dev(group->iommu_group,
1099 group, vfio_dev_viable) == 0);
1100}
1101
1102static const struct file_operations vfio_device_fops;
1103
1104static int vfio_group_get_device_fd(struct vfio_group *group, char *buf)
1105{
1106 struct vfio_device *device;
1107 struct file *filep;
1108 int ret = -ENODEV;
1109
1110 if (0 == atomic_read(&group->container_users) ||
1111 !group->container->iommu_driver || !vfio_group_viable(group))
1112 return -EINVAL;
1113
1114 mutex_lock(&group->device_lock);
1115 list_for_each_entry(device, &group->device_list, group_next) {
1116 if (strcmp(dev_name(device->dev), buf))
1117 continue;
1118
1119 ret = device->ops->open(device->device_data);
1120 if (ret)
1121 break;
1122 /*
1123 * We can't use anon_inode_getfd() because we need to modify
1124 * the f_mode flags directly to allow more than just ioctls
1125 */
1126 ret = get_unused_fd();
1127 if (ret < 0) {
1128 device->ops->release(device->device_data);
1129 break;
1130 }
1131
1132 filep = anon_inode_getfile("[vfio-device]", &vfio_device_fops,
1133 device, O_RDWR);
1134 if (IS_ERR(filep)) {
1135 put_unused_fd(ret);
1136 ret = PTR_ERR(filep);
1137 device->ops->release(device->device_data);
1138 break;
1139 }
1140
1141 /*
1142 * TODO: add an anon_inode interface to do this.
1143 * Appears to be missing by lack of need rather than
1144 * explicitly prevented. Now there's need.
1145 */
1146 filep->f_mode |= (FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1147
Alex Williamsoncba33452012-07-31 08:16:22 -06001148 vfio_device_get(device);
1149 atomic_inc(&group->container_users);
Al Viro31605de2012-08-17 21:32:56 -04001150
1151 fd_install(ret, filep);
Alex Williamsoncba33452012-07-31 08:16:22 -06001152 break;
1153 }
1154 mutex_unlock(&group->device_lock);
1155
1156 return ret;
1157}
1158
1159static long vfio_group_fops_unl_ioctl(struct file *filep,
1160 unsigned int cmd, unsigned long arg)
1161{
1162 struct vfio_group *group = filep->private_data;
1163 long ret = -ENOTTY;
1164
1165 switch (cmd) {
1166 case VFIO_GROUP_GET_STATUS:
1167 {
1168 struct vfio_group_status status;
1169 unsigned long minsz;
1170
1171 minsz = offsetofend(struct vfio_group_status, flags);
1172
1173 if (copy_from_user(&status, (void __user *)arg, minsz))
1174 return -EFAULT;
1175
1176 if (status.argsz < minsz)
1177 return -EINVAL;
1178
1179 status.flags = 0;
1180
1181 if (vfio_group_viable(group))
1182 status.flags |= VFIO_GROUP_FLAGS_VIABLE;
1183
1184 if (group->container)
1185 status.flags |= VFIO_GROUP_FLAGS_CONTAINER_SET;
1186
1187 if (copy_to_user((void __user *)arg, &status, minsz))
1188 return -EFAULT;
1189
1190 ret = 0;
1191 break;
1192 }
1193 case VFIO_GROUP_SET_CONTAINER:
1194 {
1195 int fd;
1196
1197 if (get_user(fd, (int __user *)arg))
1198 return -EFAULT;
1199
1200 if (fd < 0)
1201 return -EINVAL;
1202
1203 ret = vfio_group_set_container(group, fd);
1204 break;
1205 }
1206 case VFIO_GROUP_UNSET_CONTAINER:
1207 ret = vfio_group_unset_container(group);
1208 break;
1209 case VFIO_GROUP_GET_DEVICE_FD:
1210 {
1211 char *buf;
1212
1213 buf = strndup_user((const char __user *)arg, PAGE_SIZE);
1214 if (IS_ERR(buf))
1215 return PTR_ERR(buf);
1216
1217 ret = vfio_group_get_device_fd(group, buf);
1218 kfree(buf);
1219 break;
1220 }
1221 }
1222
1223 return ret;
1224}
1225
1226#ifdef CONFIG_COMPAT
1227static long vfio_group_fops_compat_ioctl(struct file *filep,
1228 unsigned int cmd, unsigned long arg)
1229{
1230 arg = (unsigned long)compat_ptr(arg);
1231 return vfio_group_fops_unl_ioctl(filep, cmd, arg);
1232}
1233#endif /* CONFIG_COMPAT */
1234
1235static int vfio_group_fops_open(struct inode *inode, struct file *filep)
1236{
1237 struct vfio_group *group;
1238
1239 group = vfio_group_get_from_minor(iminor(inode));
1240 if (!group)
1241 return -ENODEV;
1242
1243 if (group->container) {
1244 vfio_group_put(group);
1245 return -EBUSY;
1246 }
1247
1248 filep->private_data = group;
1249
1250 return 0;
1251}
1252
1253static int vfio_group_fops_release(struct inode *inode, struct file *filep)
1254{
1255 struct vfio_group *group = filep->private_data;
1256
1257 filep->private_data = NULL;
1258
1259 vfio_group_try_dissolve_container(group);
1260
1261 vfio_group_put(group);
1262
1263 return 0;
1264}
1265
1266static const struct file_operations vfio_group_fops = {
1267 .owner = THIS_MODULE,
1268 .unlocked_ioctl = vfio_group_fops_unl_ioctl,
1269#ifdef CONFIG_COMPAT
1270 .compat_ioctl = vfio_group_fops_compat_ioctl,
1271#endif
1272 .open = vfio_group_fops_open,
1273 .release = vfio_group_fops_release,
1274};
1275
1276/**
1277 * VFIO Device fd
1278 */
1279static int vfio_device_fops_release(struct inode *inode, struct file *filep)
1280{
1281 struct vfio_device *device = filep->private_data;
1282
1283 device->ops->release(device->device_data);
1284
1285 vfio_group_try_dissolve_container(device->group);
1286
1287 vfio_device_put(device);
1288
1289 return 0;
1290}
1291
1292static long vfio_device_fops_unl_ioctl(struct file *filep,
1293 unsigned int cmd, unsigned long arg)
1294{
1295 struct vfio_device *device = filep->private_data;
1296
1297 if (unlikely(!device->ops->ioctl))
1298 return -EINVAL;
1299
1300 return device->ops->ioctl(device->device_data, cmd, arg);
1301}
1302
1303static ssize_t vfio_device_fops_read(struct file *filep, char __user *buf,
1304 size_t count, loff_t *ppos)
1305{
1306 struct vfio_device *device = filep->private_data;
1307
1308 if (unlikely(!device->ops->read))
1309 return -EINVAL;
1310
1311 return device->ops->read(device->device_data, buf, count, ppos);
1312}
1313
1314static ssize_t vfio_device_fops_write(struct file *filep,
1315 const char __user *buf,
1316 size_t count, loff_t *ppos)
1317{
1318 struct vfio_device *device = filep->private_data;
1319
1320 if (unlikely(!device->ops->write))
1321 return -EINVAL;
1322
1323 return device->ops->write(device->device_data, buf, count, ppos);
1324}
1325
1326static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
1327{
1328 struct vfio_device *device = filep->private_data;
1329
1330 if (unlikely(!device->ops->mmap))
1331 return -EINVAL;
1332
1333 return device->ops->mmap(device->device_data, vma);
1334}
1335
1336#ifdef CONFIG_COMPAT
1337static long vfio_device_fops_compat_ioctl(struct file *filep,
1338 unsigned int cmd, unsigned long arg)
1339{
1340 arg = (unsigned long)compat_ptr(arg);
1341 return vfio_device_fops_unl_ioctl(filep, cmd, arg);
1342}
1343#endif /* CONFIG_COMPAT */
1344
1345static const struct file_operations vfio_device_fops = {
1346 .owner = THIS_MODULE,
1347 .release = vfio_device_fops_release,
1348 .read = vfio_device_fops_read,
1349 .write = vfio_device_fops_write,
1350 .unlocked_ioctl = vfio_device_fops_unl_ioctl,
1351#ifdef CONFIG_COMPAT
1352 .compat_ioctl = vfio_device_fops_compat_ioctl,
1353#endif
1354 .mmap = vfio_device_fops_mmap,
1355};
1356
1357/**
1358 * Module/class support
1359 */
1360static char *vfio_devnode(struct device *dev, umode_t *mode)
1361{
1362 return kasprintf(GFP_KERNEL, "vfio/%s", dev_name(dev));
1363}
1364
1365static int __init vfio_init(void)
1366{
1367 int ret;
1368
1369 idr_init(&vfio.group_idr);
1370 mutex_init(&vfio.group_lock);
1371 mutex_init(&vfio.iommu_drivers_lock);
1372 INIT_LIST_HEAD(&vfio.group_list);
1373 INIT_LIST_HEAD(&vfio.iommu_drivers_list);
1374 init_waitqueue_head(&vfio.release_q);
1375
1376 vfio.class = class_create(THIS_MODULE, "vfio");
1377 if (IS_ERR(vfio.class)) {
1378 ret = PTR_ERR(vfio.class);
1379 goto err_class;
1380 }
1381
1382 vfio.class->devnode = vfio_devnode;
1383
1384 ret = alloc_chrdev_region(&vfio.devt, 0, MINORMASK, "vfio");
1385 if (ret)
1386 goto err_base_chrdev;
1387
1388 cdev_init(&vfio.cdev, &vfio_fops);
1389 ret = cdev_add(&vfio.cdev, vfio.devt, 1);
1390 if (ret)
1391 goto err_base_cdev;
1392
1393 vfio.dev = device_create(vfio.class, NULL, vfio.devt, NULL, "vfio");
1394 if (IS_ERR(vfio.dev)) {
1395 ret = PTR_ERR(vfio.dev);
1396 goto err_base_dev;
1397 }
1398
1399 /* /dev/vfio/$GROUP */
1400 cdev_init(&vfio.group_cdev, &vfio_group_fops);
1401 ret = cdev_add(&vfio.group_cdev,
1402 MKDEV(MAJOR(vfio.devt), 1), MINORMASK - 1);
1403 if (ret)
1404 goto err_groups_cdev;
1405
1406 pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
1407
Alex Williamson73fa0d12012-07-31 08:16:23 -06001408 /*
1409 * Attempt to load known iommu-drivers. This gives us a working
1410 * environment without the user needing to explicitly load iommu
1411 * drivers.
1412 */
1413 request_module_nowait("vfio_iommu_type1");
1414
Alex Williamsoncba33452012-07-31 08:16:22 -06001415 return 0;
1416
1417err_groups_cdev:
1418 device_destroy(vfio.class, vfio.devt);
1419err_base_dev:
1420 cdev_del(&vfio.cdev);
1421err_base_cdev:
1422 unregister_chrdev_region(vfio.devt, MINORMASK);
1423err_base_chrdev:
1424 class_destroy(vfio.class);
1425 vfio.class = NULL;
1426err_class:
1427 return ret;
1428}
1429
1430static void __exit vfio_cleanup(void)
1431{
1432 WARN_ON(!list_empty(&vfio.group_list));
1433
1434 idr_destroy(&vfio.group_idr);
1435 cdev_del(&vfio.group_cdev);
1436 device_destroy(vfio.class, vfio.devt);
1437 cdev_del(&vfio.cdev);
1438 unregister_chrdev_region(vfio.devt, MINORMASK);
1439 class_destroy(vfio.class);
1440 vfio.class = NULL;
1441}
1442
1443module_init(vfio_init);
1444module_exit(vfio_cleanup);
1445
1446MODULE_VERSION(DRIVER_VERSION);
1447MODULE_LICENSE("GPL v2");
1448MODULE_AUTHOR(DRIVER_AUTHOR);
1449MODULE_DESCRIPTION(DRIVER_DESC);