iommu.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. /*
  2. * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
  3. * Author: Joerg Roedel <joerg.roedel@amd.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published
  7. * by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #define pr_fmt(fmt) "%s: " fmt, __func__
  19. #include <linux/device.h>
  20. #include <linux/kernel.h>
  21. #include <linux/bug.h>
  22. #include <linux/types.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/errno.h>
  26. #include <linux/iommu.h>
  27. #include <linux/idr.h>
  28. #include <linux/notifier.h>
  29. #include <linux/err.h>
  30. #include <linux/pci.h>
  31. #include <linux/bitops.h>
  32. #include <trace/events/iommu.h>
  33. static struct kset *iommu_group_kset;
  34. static struct ida iommu_group_ida;
  35. static struct mutex iommu_group_mutex;
  36. struct iommu_callback_data {
  37. const struct iommu_ops *ops;
  38. };
  39. struct iommu_group {
  40. struct kobject kobj;
  41. struct kobject *devices_kobj;
  42. struct list_head devices;
  43. struct mutex mutex;
  44. struct blocking_notifier_head notifier;
  45. void *iommu_data;
  46. void (*iommu_data_release)(void *iommu_data);
  47. char *name;
  48. int id;
  49. };
  50. struct iommu_device {
  51. struct list_head list;
  52. struct device *dev;
  53. char *name;
  54. };
  55. struct iommu_group_attribute {
  56. struct attribute attr;
  57. ssize_t (*show)(struct iommu_group *group, char *buf);
  58. ssize_t (*store)(struct iommu_group *group,
  59. const char *buf, size_t count);
  60. };
  61. #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
  62. struct iommu_group_attribute iommu_group_attr_##_name = \
  63. __ATTR(_name, _mode, _show, _store)
  64. #define to_iommu_group_attr(_attr) \
  65. container_of(_attr, struct iommu_group_attribute, attr)
  66. #define to_iommu_group(_kobj) \
  67. container_of(_kobj, struct iommu_group, kobj)
  68. static ssize_t iommu_group_attr_show(struct kobject *kobj,
  69. struct attribute *__attr, char *buf)
  70. {
  71. struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
  72. struct iommu_group *group = to_iommu_group(kobj);
  73. ssize_t ret = -EIO;
  74. if (attr->show)
  75. ret = attr->show(group, buf);
  76. return ret;
  77. }
  78. static ssize_t iommu_group_attr_store(struct kobject *kobj,
  79. struct attribute *__attr,
  80. const char *buf, size_t count)
  81. {
  82. struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
  83. struct iommu_group *group = to_iommu_group(kobj);
  84. ssize_t ret = -EIO;
  85. if (attr->store)
  86. ret = attr->store(group, buf, count);
  87. return ret;
  88. }
  89. static const struct sysfs_ops iommu_group_sysfs_ops = {
  90. .show = iommu_group_attr_show,
  91. .store = iommu_group_attr_store,
  92. };
  93. static int iommu_group_create_file(struct iommu_group *group,
  94. struct iommu_group_attribute *attr)
  95. {
  96. return sysfs_create_file(&group->kobj, &attr->attr);
  97. }
  98. static void iommu_group_remove_file(struct iommu_group *group,
  99. struct iommu_group_attribute *attr)
  100. {
  101. sysfs_remove_file(&group->kobj, &attr->attr);
  102. }
  103. static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
  104. {
  105. return sprintf(buf, "%s\n", group->name);
  106. }
  107. static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
  108. static void iommu_group_release(struct kobject *kobj)
  109. {
  110. struct iommu_group *group = to_iommu_group(kobj);
  111. if (group->iommu_data_release)
  112. group->iommu_data_release(group->iommu_data);
  113. mutex_lock(&iommu_group_mutex);
  114. ida_remove(&iommu_group_ida, group->id);
  115. mutex_unlock(&iommu_group_mutex);
  116. kfree(group->name);
  117. kfree(group);
  118. }
  119. static struct kobj_type iommu_group_ktype = {
  120. .sysfs_ops = &iommu_group_sysfs_ops,
  121. .release = iommu_group_release,
  122. };
  123. /**
  124. * iommu_group_alloc - Allocate a new group
  125. * @name: Optional name to associate with group, visible in sysfs
  126. *
  127. * This function is called by an iommu driver to allocate a new iommu
  128. * group. The iommu group represents the minimum granularity of the iommu.
  129. * Upon successful return, the caller holds a reference to the supplied
  130. * group in order to hold the group until devices are added. Use
  131. * iommu_group_put() to release this extra reference count, allowing the
  132. * group to be automatically reclaimed once it has no devices or external
  133. * references.
  134. */
  135. struct iommu_group *iommu_group_alloc(void)
  136. {
  137. struct iommu_group *group;
  138. int ret;
  139. group = kzalloc(sizeof(*group), GFP_KERNEL);
  140. if (!group)
  141. return ERR_PTR(-ENOMEM);
  142. group->kobj.kset = iommu_group_kset;
  143. mutex_init(&group->mutex);
  144. INIT_LIST_HEAD(&group->devices);
  145. BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
  146. mutex_lock(&iommu_group_mutex);
  147. again:
  148. if (unlikely(0 == ida_pre_get(&iommu_group_ida, GFP_KERNEL))) {
  149. kfree(group);
  150. mutex_unlock(&iommu_group_mutex);
  151. return ERR_PTR(-ENOMEM);
  152. }
  153. if (-EAGAIN == ida_get_new(&iommu_group_ida, &group->id))
  154. goto again;
  155. mutex_unlock(&iommu_group_mutex);
  156. ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
  157. NULL, "%d", group->id);
  158. if (ret) {
  159. mutex_lock(&iommu_group_mutex);
  160. ida_remove(&iommu_group_ida, group->id);
  161. mutex_unlock(&iommu_group_mutex);
  162. kfree(group);
  163. return ERR_PTR(ret);
  164. }
  165. group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
  166. if (!group->devices_kobj) {
  167. kobject_put(&group->kobj); /* triggers .release & free */
  168. return ERR_PTR(-ENOMEM);
  169. }
  170. /*
  171. * The devices_kobj holds a reference on the group kobject, so
  172. * as long as that exists so will the group. We can therefore
  173. * use the devices_kobj for reference counting.
  174. */
  175. kobject_put(&group->kobj);
  176. return group;
  177. }
  178. EXPORT_SYMBOL_GPL(iommu_group_alloc);
  179. struct iommu_group *iommu_group_get_by_id(int id)
  180. {
  181. struct kobject *group_kobj;
  182. struct iommu_group *group;
  183. const char *name;
  184. if (!iommu_group_kset)
  185. return NULL;
  186. name = kasprintf(GFP_KERNEL, "%d", id);
  187. if (!name)
  188. return NULL;
  189. group_kobj = kset_find_obj(iommu_group_kset, name);
  190. kfree(name);
  191. if (!group_kobj)
  192. return NULL;
  193. group = container_of(group_kobj, struct iommu_group, kobj);
  194. BUG_ON(group->id != id);
  195. kobject_get(group->devices_kobj);
  196. kobject_put(&group->kobj);
  197. return group;
  198. }
  199. EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
  200. /**
  201. * iommu_group_get_iommudata - retrieve iommu_data registered for a group
  202. * @group: the group
  203. *
  204. * iommu drivers can store data in the group for use when doing iommu
  205. * operations. This function provides a way to retrieve it. Caller
  206. * should hold a group reference.
  207. */
  208. void *iommu_group_get_iommudata(struct iommu_group *group)
  209. {
  210. return group->iommu_data;
  211. }
  212. EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
  213. /**
  214. * iommu_group_set_iommudata - set iommu_data for a group
  215. * @group: the group
  216. * @iommu_data: new data
  217. * @release: release function for iommu_data
  218. *
  219. * iommu drivers can store data in the group for use when doing iommu
  220. * operations. This function provides a way to set the data after
  221. * the group has been allocated. Caller should hold a group reference.
  222. */
  223. void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
  224. void (*release)(void *iommu_data))
  225. {
  226. group->iommu_data = iommu_data;
  227. group->iommu_data_release = release;
  228. }
  229. EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
  230. /**
  231. * iommu_group_set_name - set name for a group
  232. * @group: the group
  233. * @name: name
  234. *
  235. * Allow iommu driver to set a name for a group. When set it will
  236. * appear in a name attribute file under the group in sysfs.
  237. */
  238. int iommu_group_set_name(struct iommu_group *group, const char *name)
  239. {
  240. int ret;
  241. if (group->name) {
  242. iommu_group_remove_file(group, &iommu_group_attr_name);
  243. kfree(group->name);
  244. group->name = NULL;
  245. if (!name)
  246. return 0;
  247. }
  248. group->name = kstrdup(name, GFP_KERNEL);
  249. if (!group->name)
  250. return -ENOMEM;
  251. ret = iommu_group_create_file(group, &iommu_group_attr_name);
  252. if (ret) {
  253. kfree(group->name);
  254. group->name = NULL;
  255. return ret;
  256. }
  257. return 0;
  258. }
  259. EXPORT_SYMBOL_GPL(iommu_group_set_name);
  260. /**
  261. * iommu_group_add_device - add a device to an iommu group
  262. * @group: the group into which to add the device (reference should be held)
  263. * @dev: the device
  264. *
  265. * This function is called by an iommu driver to add a device into a
  266. * group. Adding a device increments the group reference count.
  267. */
  268. int iommu_group_add_device(struct iommu_group *group, struct device *dev)
  269. {
  270. int ret, i = 0;
  271. struct iommu_device *device;
  272. device = kzalloc(sizeof(*device), GFP_KERNEL);
  273. if (!device)
  274. return -ENOMEM;
  275. device->dev = dev;
  276. ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
  277. if (ret) {
  278. kfree(device);
  279. return ret;
  280. }
  281. device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
  282. rename:
  283. if (!device->name) {
  284. sysfs_remove_link(&dev->kobj, "iommu_group");
  285. kfree(device);
  286. return -ENOMEM;
  287. }
  288. ret = sysfs_create_link_nowarn(group->devices_kobj,
  289. &dev->kobj, device->name);
  290. if (ret) {
  291. kfree(device->name);
  292. if (ret == -EEXIST && i >= 0) {
  293. /*
  294. * Account for the slim chance of collision
  295. * and append an instance to the name.
  296. */
  297. device->name = kasprintf(GFP_KERNEL, "%s.%d",
  298. kobject_name(&dev->kobj), i++);
  299. goto rename;
  300. }
  301. sysfs_remove_link(&dev->kobj, "iommu_group");
  302. kfree(device);
  303. return ret;
  304. }
  305. kobject_get(group->devices_kobj);
  306. dev->iommu_group = group;
  307. mutex_lock(&group->mutex);
  308. list_add_tail(&device->list, &group->devices);
  309. mutex_unlock(&group->mutex);
  310. /* Notify any listeners about change to group. */
  311. blocking_notifier_call_chain(&group->notifier,
  312. IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
  313. trace_add_device_to_group(group->id, dev);
  314. return 0;
  315. }
  316. EXPORT_SYMBOL_GPL(iommu_group_add_device);
  317. /**
  318. * iommu_group_remove_device - remove a device from it's current group
  319. * @dev: device to be removed
  320. *
  321. * This function is called by an iommu driver to remove the device from
  322. * it's current group. This decrements the iommu group reference count.
  323. */
  324. void iommu_group_remove_device(struct device *dev)
  325. {
  326. struct iommu_group *group = dev->iommu_group;
  327. struct iommu_device *tmp_device, *device = NULL;
  328. /* Pre-notify listeners that a device is being removed. */
  329. blocking_notifier_call_chain(&group->notifier,
  330. IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
  331. mutex_lock(&group->mutex);
  332. list_for_each_entry(tmp_device, &group->devices, list) {
  333. if (tmp_device->dev == dev) {
  334. device = tmp_device;
  335. list_del(&device->list);
  336. break;
  337. }
  338. }
  339. mutex_unlock(&group->mutex);
  340. if (!device)
  341. return;
  342. sysfs_remove_link(group->devices_kobj, device->name);
  343. sysfs_remove_link(&dev->kobj, "iommu_group");
  344. trace_remove_device_from_group(group->id, dev);
  345. kfree(device->name);
  346. kfree(device);
  347. dev->iommu_group = NULL;
  348. kobject_put(group->devices_kobj);
  349. }
  350. EXPORT_SYMBOL_GPL(iommu_group_remove_device);
  351. /**
  352. * iommu_group_for_each_dev - iterate over each device in the group
  353. * @group: the group
  354. * @data: caller opaque data to be passed to callback function
  355. * @fn: caller supplied callback function
  356. *
  357. * This function is called by group users to iterate over group devices.
  358. * Callers should hold a reference count to the group during callback.
  359. * The group->mutex is held across callbacks, which will block calls to
  360. * iommu_group_add/remove_device.
  361. */
  362. int iommu_group_for_each_dev(struct iommu_group *group, void *data,
  363. int (*fn)(struct device *, void *))
  364. {
  365. struct iommu_device *device;
  366. int ret = 0;
  367. mutex_lock(&group->mutex);
  368. list_for_each_entry(device, &group->devices, list) {
  369. ret = fn(device->dev, data);
  370. if (ret)
  371. break;
  372. }
  373. mutex_unlock(&group->mutex);
  374. return ret;
  375. }
  376. EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
  377. /**
  378. * iommu_group_get - Return the group for a device and increment reference
  379. * @dev: get the group that this device belongs to
  380. *
  381. * This function is called by iommu drivers and users to get the group
  382. * for the specified device. If found, the group is returned and the group
  383. * reference in incremented, else NULL.
  384. */
  385. struct iommu_group *iommu_group_get(struct device *dev)
  386. {
  387. struct iommu_group *group = dev->iommu_group;
  388. if (group)
  389. kobject_get(group->devices_kobj);
  390. return group;
  391. }
  392. EXPORT_SYMBOL_GPL(iommu_group_get);
  393. /**
  394. * iommu_group_put - Decrement group reference
  395. * @group: the group to use
  396. *
  397. * This function is called by iommu drivers and users to release the
  398. * iommu group. Once the reference count is zero, the group is released.
  399. */
  400. void iommu_group_put(struct iommu_group *group)
  401. {
  402. if (group)
  403. kobject_put(group->devices_kobj);
  404. }
  405. EXPORT_SYMBOL_GPL(iommu_group_put);
  406. /**
  407. * iommu_group_register_notifier - Register a notifier for group changes
  408. * @group: the group to watch
  409. * @nb: notifier block to signal
  410. *
  411. * This function allows iommu group users to track changes in a group.
  412. * See include/linux/iommu.h for actions sent via this notifier. Caller
  413. * should hold a reference to the group throughout notifier registration.
  414. */
  415. int iommu_group_register_notifier(struct iommu_group *group,
  416. struct notifier_block *nb)
  417. {
  418. return blocking_notifier_chain_register(&group->notifier, nb);
  419. }
  420. EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
  421. /**
  422. * iommu_group_unregister_notifier - Unregister a notifier
  423. * @group: the group to watch
  424. * @nb: notifier block to signal
  425. *
  426. * Unregister a previously registered group notifier block.
  427. */
  428. int iommu_group_unregister_notifier(struct iommu_group *group,
  429. struct notifier_block *nb)
  430. {
  431. return blocking_notifier_chain_unregister(&group->notifier, nb);
  432. }
  433. EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
  434. /**
  435. * iommu_group_id - Return ID for a group
  436. * @group: the group to ID
  437. *
  438. * Return the unique ID for the group matching the sysfs group number.
  439. */
  440. int iommu_group_id(struct iommu_group *group)
  441. {
  442. return group->id;
  443. }
  444. EXPORT_SYMBOL_GPL(iommu_group_id);
  445. static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
  446. unsigned long *devfns);
  447. /*
  448. * To consider a PCI device isolated, we require ACS to support Source
  449. * Validation, Request Redirection, Completer Redirection, and Upstream
  450. * Forwarding. This effectively means that devices cannot spoof their
  451. * requester ID, requests and completions cannot be redirected, and all
  452. * transactions are forwarded upstream, even as it passes through a
  453. * bridge where the target device is downstream.
  454. */
  455. #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
  456. /*
  457. * For multifunction devices which are not isolated from each other, find
  458. * all the other non-isolated functions and look for existing groups. For
  459. * each function, we also need to look for aliases to or from other devices
  460. * that may already have a group.
  461. */
  462. static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
  463. unsigned long *devfns)
  464. {
  465. struct pci_dev *tmp = NULL;
  466. struct iommu_group *group;
  467. if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
  468. return NULL;
  469. for_each_pci_dev(tmp) {
  470. if (tmp == pdev || tmp->bus != pdev->bus ||
  471. PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
  472. pci_acs_enabled(tmp, REQ_ACS_FLAGS))
  473. continue;
  474. group = get_pci_alias_group(tmp, devfns);
  475. if (group) {
  476. pci_dev_put(tmp);
  477. return group;
  478. }
  479. }
  480. return NULL;
  481. }
  482. /*
  483. * Look for aliases to or from the given device for exisiting groups. The
  484. * dma_alias_devfn only supports aliases on the same bus, therefore the search
  485. * space is quite small (especially since we're really only looking at pcie
  486. * device, and therefore only expect multiple slots on the root complex or
  487. * downstream switch ports). It's conceivable though that a pair of
  488. * multifunction devices could have aliases between them that would cause a
  489. * loop. To prevent this, we use a bitmap to track where we've been.
  490. */
  491. static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
  492. unsigned long *devfns)
  493. {
  494. struct pci_dev *tmp = NULL;
  495. struct iommu_group *group;
  496. if (test_and_set_bit(pdev->devfn & 0xff, devfns))
  497. return NULL;
  498. group = iommu_group_get(&pdev->dev);
  499. if (group)
  500. return group;
  501. for_each_pci_dev(tmp) {
  502. if (tmp == pdev || tmp->bus != pdev->bus)
  503. continue;
  504. /* We alias them or they alias us */
  505. if (((pdev->dev_flags & PCI_DEV_FLAGS_DMA_ALIAS_DEVFN) &&
  506. pdev->dma_alias_devfn == tmp->devfn) ||
  507. ((tmp->dev_flags & PCI_DEV_FLAGS_DMA_ALIAS_DEVFN) &&
  508. tmp->dma_alias_devfn == pdev->devfn)) {
  509. group = get_pci_alias_group(tmp, devfns);
  510. if (group) {
  511. pci_dev_put(tmp);
  512. return group;
  513. }
  514. group = get_pci_function_alias_group(tmp, devfns);
  515. if (group) {
  516. pci_dev_put(tmp);
  517. return group;
  518. }
  519. }
  520. }
  521. return NULL;
  522. }
  523. struct group_for_pci_data {
  524. struct pci_dev *pdev;
  525. struct iommu_group *group;
  526. };
  527. /*
  528. * DMA alias iterator callback, return the last seen device. Stop and return
  529. * the IOMMU group if we find one along the way.
  530. */
  531. static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
  532. {
  533. struct group_for_pci_data *data = opaque;
  534. data->pdev = pdev;
  535. data->group = iommu_group_get(&pdev->dev);
  536. return data->group != NULL;
  537. }
  538. /*
  539. * Use standard PCI bus topology, isolation features, and DMA alias quirks
  540. * to find or create an IOMMU group for a device.
  541. */
  542. static struct iommu_group *iommu_group_get_for_pci_dev(struct pci_dev *pdev)
  543. {
  544. struct group_for_pci_data data;
  545. struct pci_bus *bus;
  546. struct iommu_group *group = NULL;
  547. u64 devfns[4] = { 0 };
  548. /*
  549. * Find the upstream DMA alias for the device. A device must not
  550. * be aliased due to topology in order to have its own IOMMU group.
  551. * If we find an alias along the way that already belongs to a
  552. * group, use it.
  553. */
  554. if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
  555. return data.group;
  556. pdev = data.pdev;
  557. /*
  558. * Continue upstream from the point of minimum IOMMU granularity
  559. * due to aliases to the point where devices are protected from
  560. * peer-to-peer DMA by PCI ACS. Again, if we find an existing
  561. * group, use it.
  562. */
  563. for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
  564. if (!bus->self)
  565. continue;
  566. if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
  567. break;
  568. pdev = bus->self;
  569. group = iommu_group_get(&pdev->dev);
  570. if (group)
  571. return group;
  572. }
  573. /*
  574. * Look for existing groups on device aliases. If we alias another
  575. * device or another device aliases us, use the same group.
  576. */
  577. group = get_pci_alias_group(pdev, (unsigned long *)devfns);
  578. if (group)
  579. return group;
  580. /*
  581. * Look for existing groups on non-isolated functions on the same
  582. * slot and aliases of those funcions, if any. No need to clear
  583. * the search bitmap, the tested devfns are still valid.
  584. */
  585. group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
  586. if (group)
  587. return group;
  588. /* No shared group found, allocate new */
  589. return iommu_group_alloc();
  590. }
  591. /**
  592. * iommu_group_get_for_dev - Find or create the IOMMU group for a device
  593. * @dev: target device
  594. *
  595. * This function is intended to be called by IOMMU drivers and extended to
  596. * support common, bus-defined algorithms when determining or creating the
  597. * IOMMU group for a device. On success, the caller will hold a reference
  598. * to the returned IOMMU group, which will already include the provided
  599. * device. The reference should be released with iommu_group_put().
  600. */
  601. struct iommu_group *iommu_group_get_for_dev(struct device *dev)
  602. {
  603. struct iommu_group *group;
  604. int ret;
  605. group = iommu_group_get(dev);
  606. if (group)
  607. return group;
  608. if (!dev_is_pci(dev))
  609. return ERR_PTR(-EINVAL);
  610. group = iommu_group_get_for_pci_dev(to_pci_dev(dev));
  611. if (IS_ERR(group))
  612. return group;
  613. ret = iommu_group_add_device(group, dev);
  614. if (ret) {
  615. iommu_group_put(group);
  616. return ERR_PTR(ret);
  617. }
  618. return group;
  619. }
  620. static int add_iommu_group(struct device *dev, void *data)
  621. {
  622. struct iommu_callback_data *cb = data;
  623. const struct iommu_ops *ops = cb->ops;
  624. if (!ops->add_device)
  625. return -ENODEV;
  626. WARN_ON(dev->iommu_group);
  627. ops->add_device(dev);
  628. return 0;
  629. }
  630. static int iommu_bus_notifier(struct notifier_block *nb,
  631. unsigned long action, void *data)
  632. {
  633. struct device *dev = data;
  634. const struct iommu_ops *ops = dev->bus->iommu_ops;
  635. struct iommu_group *group;
  636. unsigned long group_action = 0;
  637. /*
  638. * ADD/DEL call into iommu driver ops if provided, which may
  639. * result in ADD/DEL notifiers to group->notifier
  640. */
  641. if (action == BUS_NOTIFY_ADD_DEVICE) {
  642. if (ops->add_device)
  643. return ops->add_device(dev);
  644. } else if (action == BUS_NOTIFY_DEL_DEVICE) {
  645. if (ops->remove_device && dev->iommu_group) {
  646. ops->remove_device(dev);
  647. return 0;
  648. }
  649. }
  650. /*
  651. * Remaining BUS_NOTIFYs get filtered and republished to the
  652. * group, if anyone is listening
  653. */
  654. group = iommu_group_get(dev);
  655. if (!group)
  656. return 0;
  657. switch (action) {
  658. case BUS_NOTIFY_BIND_DRIVER:
  659. group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
  660. break;
  661. case BUS_NOTIFY_BOUND_DRIVER:
  662. group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
  663. break;
  664. case BUS_NOTIFY_UNBIND_DRIVER:
  665. group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
  666. break;
  667. case BUS_NOTIFY_UNBOUND_DRIVER:
  668. group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
  669. break;
  670. }
  671. if (group_action)
  672. blocking_notifier_call_chain(&group->notifier,
  673. group_action, dev);
  674. iommu_group_put(group);
  675. return 0;
  676. }
  677. static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
  678. {
  679. int err;
  680. struct notifier_block *nb;
  681. struct iommu_callback_data cb = {
  682. .ops = ops,
  683. };
  684. nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
  685. if (!nb)
  686. return -ENOMEM;
  687. nb->notifier_call = iommu_bus_notifier;
  688. err = bus_register_notifier(bus, nb);
  689. if (err) {
  690. kfree(nb);
  691. return err;
  692. }
  693. return bus_for_each_dev(bus, NULL, &cb, add_iommu_group);
  694. }
  695. /**
  696. * bus_set_iommu - set iommu-callbacks for the bus
  697. * @bus: bus.
  698. * @ops: the callbacks provided by the iommu-driver
  699. *
  700. * This function is called by an iommu driver to set the iommu methods
  701. * used for a particular bus. Drivers for devices on that bus can use
  702. * the iommu-api after these ops are registered.
  703. * This special function is needed because IOMMUs are usually devices on
  704. * the bus itself, so the iommu drivers are not initialized when the bus
  705. * is set up. With this function the iommu-driver can set the iommu-ops
  706. * afterwards.
  707. */
  708. int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
  709. {
  710. if (bus->iommu_ops != NULL)
  711. return -EBUSY;
  712. bus->iommu_ops = ops;
  713. /* Do IOMMU specific setup for this bus-type */
  714. return iommu_bus_init(bus, ops);
  715. }
  716. EXPORT_SYMBOL_GPL(bus_set_iommu);
  717. bool iommu_present(struct bus_type *bus)
  718. {
  719. return bus->iommu_ops != NULL;
  720. }
  721. EXPORT_SYMBOL_GPL(iommu_present);
  722. bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
  723. {
  724. if (!bus->iommu_ops || !bus->iommu_ops->capable)
  725. return false;
  726. return bus->iommu_ops->capable(cap);
  727. }
  728. EXPORT_SYMBOL_GPL(iommu_capable);
  729. /**
  730. * iommu_set_fault_handler() - set a fault handler for an iommu domain
  731. * @domain: iommu domain
  732. * @handler: fault handler
  733. * @token: user data, will be passed back to the fault handler
  734. *
  735. * This function should be used by IOMMU users which want to be notified
  736. * whenever an IOMMU fault happens.
  737. *
  738. * The fault handler itself should return 0 on success, and an appropriate
  739. * error code otherwise.
  740. */
  741. void iommu_set_fault_handler(struct iommu_domain *domain,
  742. iommu_fault_handler_t handler,
  743. void *token)
  744. {
  745. BUG_ON(!domain);
  746. domain->handler = handler;
  747. domain->handler_token = token;
  748. }
  749. EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
  750. struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
  751. {
  752. struct iommu_domain *domain;
  753. int ret;
  754. if (bus == NULL || bus->iommu_ops == NULL)
  755. return NULL;
  756. domain = kzalloc(sizeof(*domain), GFP_KERNEL);
  757. if (!domain)
  758. return NULL;
  759. domain->ops = bus->iommu_ops;
  760. ret = domain->ops->domain_init(domain);
  761. if (ret)
  762. goto out_free;
  763. return domain;
  764. out_free:
  765. kfree(domain);
  766. return NULL;
  767. }
  768. EXPORT_SYMBOL_GPL(iommu_domain_alloc);
  769. void iommu_domain_free(struct iommu_domain *domain)
  770. {
  771. if (likely(domain->ops->domain_destroy != NULL))
  772. domain->ops->domain_destroy(domain);
  773. kfree(domain);
  774. }
  775. EXPORT_SYMBOL_GPL(iommu_domain_free);
  776. int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
  777. {
  778. int ret;
  779. if (unlikely(domain->ops->attach_dev == NULL))
  780. return -ENODEV;
  781. ret = domain->ops->attach_dev(domain, dev);
  782. if (!ret)
  783. trace_attach_device_to_domain(dev);
  784. return ret;
  785. }
  786. EXPORT_SYMBOL_GPL(iommu_attach_device);
  787. void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
  788. {
  789. if (unlikely(domain->ops->detach_dev == NULL))
  790. return;
  791. domain->ops->detach_dev(domain, dev);
  792. trace_detach_device_from_domain(dev);
  793. }
  794. EXPORT_SYMBOL_GPL(iommu_detach_device);
  795. /*
  796. * IOMMU groups are really the natrual working unit of the IOMMU, but
  797. * the IOMMU API works on domains and devices. Bridge that gap by
  798. * iterating over the devices in a group. Ideally we'd have a single
  799. * device which represents the requestor ID of the group, but we also
  800. * allow IOMMU drivers to create policy defined minimum sets, where
  801. * the physical hardware may be able to distiguish members, but we
  802. * wish to group them at a higher level (ex. untrusted multi-function
  803. * PCI devices). Thus we attach each device.
  804. */
  805. static int iommu_group_do_attach_device(struct device *dev, void *data)
  806. {
  807. struct iommu_domain *domain = data;
  808. return iommu_attach_device(domain, dev);
  809. }
  810. int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
  811. {
  812. return iommu_group_for_each_dev(group, domain,
  813. iommu_group_do_attach_device);
  814. }
  815. EXPORT_SYMBOL_GPL(iommu_attach_group);
  816. static int iommu_group_do_detach_device(struct device *dev, void *data)
  817. {
  818. struct iommu_domain *domain = data;
  819. iommu_detach_device(domain, dev);
  820. return 0;
  821. }
  822. void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
  823. {
  824. iommu_group_for_each_dev(group, domain, iommu_group_do_detach_device);
  825. }
  826. EXPORT_SYMBOL_GPL(iommu_detach_group);
  827. phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
  828. {
  829. if (unlikely(domain->ops->iova_to_phys == NULL))
  830. return 0;
  831. return domain->ops->iova_to_phys(domain, iova);
  832. }
  833. EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
  834. static size_t iommu_pgsize(struct iommu_domain *domain,
  835. unsigned long addr_merge, size_t size)
  836. {
  837. unsigned int pgsize_idx;
  838. size_t pgsize;
  839. /* Max page size that still fits into 'size' */
  840. pgsize_idx = __fls(size);
  841. /* need to consider alignment requirements ? */
  842. if (likely(addr_merge)) {
  843. /* Max page size allowed by address */
  844. unsigned int align_pgsize_idx = __ffs(addr_merge);
  845. pgsize_idx = min(pgsize_idx, align_pgsize_idx);
  846. }
  847. /* build a mask of acceptable page sizes */
  848. pgsize = (1UL << (pgsize_idx + 1)) - 1;
  849. /* throw away page sizes not supported by the hardware */
  850. pgsize &= domain->ops->pgsize_bitmap;
  851. /* make sure we're still sane */
  852. BUG_ON(!pgsize);
  853. /* pick the biggest page */
  854. pgsize_idx = __fls(pgsize);
  855. pgsize = 1UL << pgsize_idx;
  856. return pgsize;
  857. }
  858. int iommu_map(struct iommu_domain *domain, unsigned long iova,
  859. phys_addr_t paddr, size_t size, int prot)
  860. {
  861. unsigned long orig_iova = iova;
  862. unsigned int min_pagesz;
  863. size_t orig_size = size;
  864. int ret = 0;
  865. if (unlikely(domain->ops->map == NULL ||
  866. domain->ops->pgsize_bitmap == 0UL))
  867. return -ENODEV;
  868. /* find out the minimum page size supported */
  869. min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap);
  870. /*
  871. * both the virtual address and the physical one, as well as
  872. * the size of the mapping, must be aligned (at least) to the
  873. * size of the smallest page supported by the hardware
  874. */
  875. if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
  876. pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
  877. iova, &paddr, size, min_pagesz);
  878. return -EINVAL;
  879. }
  880. pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
  881. while (size) {
  882. size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
  883. pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
  884. iova, &paddr, pgsize);
  885. ret = domain->ops->map(domain, iova, paddr, pgsize, prot);
  886. if (ret)
  887. break;
  888. iova += pgsize;
  889. paddr += pgsize;
  890. size -= pgsize;
  891. }
  892. /* unroll mapping in case something went wrong */
  893. if (ret)
  894. iommu_unmap(domain, orig_iova, orig_size - size);
  895. else
  896. trace_map(iova, paddr, size);
  897. return ret;
  898. }
  899. EXPORT_SYMBOL_GPL(iommu_map);
  900. size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size)
  901. {
  902. size_t unmapped_page, unmapped = 0;
  903. unsigned int min_pagesz;
  904. if (unlikely(domain->ops->unmap == NULL ||
  905. domain->ops->pgsize_bitmap == 0UL))
  906. return -ENODEV;
  907. /* find out the minimum page size supported */
  908. min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap);
  909. /*
  910. * The virtual address, as well as the size of the mapping, must be
  911. * aligned (at least) to the size of the smallest page supported
  912. * by the hardware
  913. */
  914. if (!IS_ALIGNED(iova | size, min_pagesz)) {
  915. pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
  916. iova, size, min_pagesz);
  917. return -EINVAL;
  918. }
  919. pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
  920. /*
  921. * Keep iterating until we either unmap 'size' bytes (or more)
  922. * or we hit an area that isn't mapped.
  923. */
  924. while (unmapped < size) {
  925. size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
  926. unmapped_page = domain->ops->unmap(domain, iova, pgsize);
  927. if (!unmapped_page)
  928. break;
  929. pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
  930. iova, unmapped_page);
  931. iova += unmapped_page;
  932. unmapped += unmapped_page;
  933. }
  934. trace_unmap(iova, 0, size);
  935. return unmapped;
  936. }
  937. EXPORT_SYMBOL_GPL(iommu_unmap);
  938. int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
  939. phys_addr_t paddr, u64 size, int prot)
  940. {
  941. if (unlikely(domain->ops->domain_window_enable == NULL))
  942. return -ENODEV;
  943. return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
  944. prot);
  945. }
  946. EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
  947. void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
  948. {
  949. if (unlikely(domain->ops->domain_window_disable == NULL))
  950. return;
  951. return domain->ops->domain_window_disable(domain, wnd_nr);
  952. }
  953. EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
  954. static int __init iommu_init(void)
  955. {
  956. iommu_group_kset = kset_create_and_add("iommu_groups",
  957. NULL, kernel_kobj);
  958. ida_init(&iommu_group_ida);
  959. mutex_init(&iommu_group_mutex);
  960. BUG_ON(!iommu_group_kset);
  961. return 0;
  962. }
  963. arch_initcall(iommu_init);
  964. int iommu_domain_get_attr(struct iommu_domain *domain,
  965. enum iommu_attr attr, void *data)
  966. {
  967. struct iommu_domain_geometry *geometry;
  968. bool *paging;
  969. int ret = 0;
  970. u32 *count;
  971. switch (attr) {
  972. case DOMAIN_ATTR_GEOMETRY:
  973. geometry = data;
  974. *geometry = domain->geometry;
  975. break;
  976. case DOMAIN_ATTR_PAGING:
  977. paging = data;
  978. *paging = (domain->ops->pgsize_bitmap != 0UL);
  979. break;
  980. case DOMAIN_ATTR_WINDOWS:
  981. count = data;
  982. if (domain->ops->domain_get_windows != NULL)
  983. *count = domain->ops->domain_get_windows(domain);
  984. else
  985. ret = -ENODEV;
  986. break;
  987. default:
  988. if (!domain->ops->domain_get_attr)
  989. return -EINVAL;
  990. ret = domain->ops->domain_get_attr(domain, attr, data);
  991. }
  992. return ret;
  993. }
  994. EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
  995. int iommu_domain_set_attr(struct iommu_domain *domain,
  996. enum iommu_attr attr, void *data)
  997. {
  998. int ret = 0;
  999. u32 *count;
  1000. switch (attr) {
  1001. case DOMAIN_ATTR_WINDOWS:
  1002. count = data;
  1003. if (domain->ops->domain_set_windows != NULL)
  1004. ret = domain->ops->domain_set_windows(domain, *count);
  1005. else
  1006. ret = -ENODEV;
  1007. break;
  1008. default:
  1009. if (domain->ops->domain_set_attr == NULL)
  1010. return -EINVAL;
  1011. ret = domain->ops->domain_set_attr(domain, attr, data);
  1012. }
  1013. return ret;
  1014. }
  1015. EXPORT_SYMBOL_GPL(iommu_domain_set_attr);