vfio.c 36 KB

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