iommu.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  1. /*
  2. * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
  3. * Author: Joerg Roedel <jroedel@suse.de>
  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 0;
  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. err = bus_for_each_dev(bus, NULL, &cb, add_iommu_group);
  694. if (err) {
  695. bus_unregister_notifier(bus, nb);
  696. kfree(nb);
  697. return err;
  698. }
  699. return 0;
  700. }
  701. /**
  702. * bus_set_iommu - set iommu-callbacks for the bus
  703. * @bus: bus.
  704. * @ops: the callbacks provided by the iommu-driver
  705. *
  706. * This function is called by an iommu driver to set the iommu methods
  707. * used for a particular bus. Drivers for devices on that bus can use
  708. * the iommu-api after these ops are registered.
  709. * This special function is needed because IOMMUs are usually devices on
  710. * the bus itself, so the iommu drivers are not initialized when the bus
  711. * is set up. With this function the iommu-driver can set the iommu-ops
  712. * afterwards.
  713. */
  714. int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
  715. {
  716. int err;
  717. if (bus->iommu_ops != NULL)
  718. return -EBUSY;
  719. bus->iommu_ops = ops;
  720. /* Do IOMMU specific setup for this bus-type */
  721. err = iommu_bus_init(bus, ops);
  722. if (err)
  723. bus->iommu_ops = NULL;
  724. return err;
  725. }
  726. EXPORT_SYMBOL_GPL(bus_set_iommu);
  727. bool iommu_present(struct bus_type *bus)
  728. {
  729. return bus->iommu_ops != NULL;
  730. }
  731. EXPORT_SYMBOL_GPL(iommu_present);
  732. bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
  733. {
  734. if (!bus->iommu_ops || !bus->iommu_ops->capable)
  735. return false;
  736. return bus->iommu_ops->capable(cap);
  737. }
  738. EXPORT_SYMBOL_GPL(iommu_capable);
  739. /**
  740. * iommu_set_fault_handler() - set a fault handler for an iommu domain
  741. * @domain: iommu domain
  742. * @handler: fault handler
  743. * @token: user data, will be passed back to the fault handler
  744. *
  745. * This function should be used by IOMMU users which want to be notified
  746. * whenever an IOMMU fault happens.
  747. *
  748. * The fault handler itself should return 0 on success, and an appropriate
  749. * error code otherwise.
  750. */
  751. void iommu_set_fault_handler(struct iommu_domain *domain,
  752. iommu_fault_handler_t handler,
  753. void *token)
  754. {
  755. BUG_ON(!domain);
  756. domain->handler = handler;
  757. domain->handler_token = token;
  758. }
  759. EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
  760. struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
  761. {
  762. struct iommu_domain *domain;
  763. if (bus == NULL || bus->iommu_ops == NULL)
  764. return NULL;
  765. domain = bus->iommu_ops->domain_alloc(IOMMU_DOMAIN_UNMANAGED);
  766. if (!domain)
  767. return NULL;
  768. domain->ops = bus->iommu_ops;
  769. domain->type = IOMMU_DOMAIN_UNMANAGED;
  770. return domain;
  771. }
  772. EXPORT_SYMBOL_GPL(iommu_domain_alloc);
  773. void iommu_domain_free(struct iommu_domain *domain)
  774. {
  775. domain->ops->domain_free(domain);
  776. }
  777. EXPORT_SYMBOL_GPL(iommu_domain_free);
  778. int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
  779. {
  780. int ret;
  781. if (unlikely(domain->ops->attach_dev == NULL))
  782. return -ENODEV;
  783. ret = domain->ops->attach_dev(domain, dev);
  784. if (!ret)
  785. trace_attach_device_to_domain(dev);
  786. return ret;
  787. }
  788. EXPORT_SYMBOL_GPL(iommu_attach_device);
  789. void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
  790. {
  791. if (unlikely(domain->ops->detach_dev == NULL))
  792. return;
  793. domain->ops->detach_dev(domain, dev);
  794. trace_detach_device_from_domain(dev);
  795. }
  796. EXPORT_SYMBOL_GPL(iommu_detach_device);
  797. /*
  798. * IOMMU groups are really the natrual working unit of the IOMMU, but
  799. * the IOMMU API works on domains and devices. Bridge that gap by
  800. * iterating over the devices in a group. Ideally we'd have a single
  801. * device which represents the requestor ID of the group, but we also
  802. * allow IOMMU drivers to create policy defined minimum sets, where
  803. * the physical hardware may be able to distiguish members, but we
  804. * wish to group them at a higher level (ex. untrusted multi-function
  805. * PCI devices). Thus we attach each device.
  806. */
  807. static int iommu_group_do_attach_device(struct device *dev, void *data)
  808. {
  809. struct iommu_domain *domain = data;
  810. return iommu_attach_device(domain, dev);
  811. }
  812. int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
  813. {
  814. return iommu_group_for_each_dev(group, domain,
  815. iommu_group_do_attach_device);
  816. }
  817. EXPORT_SYMBOL_GPL(iommu_attach_group);
  818. static int iommu_group_do_detach_device(struct device *dev, void *data)
  819. {
  820. struct iommu_domain *domain = data;
  821. iommu_detach_device(domain, dev);
  822. return 0;
  823. }
  824. void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
  825. {
  826. iommu_group_for_each_dev(group, domain, iommu_group_do_detach_device);
  827. }
  828. EXPORT_SYMBOL_GPL(iommu_detach_group);
  829. phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
  830. {
  831. if (unlikely(domain->ops->iova_to_phys == NULL))
  832. return 0;
  833. return domain->ops->iova_to_phys(domain, iova);
  834. }
  835. EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
  836. static size_t iommu_pgsize(struct iommu_domain *domain,
  837. unsigned long addr_merge, size_t size)
  838. {
  839. unsigned int pgsize_idx;
  840. size_t pgsize;
  841. /* Max page size that still fits into 'size' */
  842. pgsize_idx = __fls(size);
  843. /* need to consider alignment requirements ? */
  844. if (likely(addr_merge)) {
  845. /* Max page size allowed by address */
  846. unsigned int align_pgsize_idx = __ffs(addr_merge);
  847. pgsize_idx = min(pgsize_idx, align_pgsize_idx);
  848. }
  849. /* build a mask of acceptable page sizes */
  850. pgsize = (1UL << (pgsize_idx + 1)) - 1;
  851. /* throw away page sizes not supported by the hardware */
  852. pgsize &= domain->ops->pgsize_bitmap;
  853. /* make sure we're still sane */
  854. BUG_ON(!pgsize);
  855. /* pick the biggest page */
  856. pgsize_idx = __fls(pgsize);
  857. pgsize = 1UL << pgsize_idx;
  858. return pgsize;
  859. }
  860. int iommu_map(struct iommu_domain *domain, unsigned long iova,
  861. phys_addr_t paddr, size_t size, int prot)
  862. {
  863. unsigned long orig_iova = iova;
  864. unsigned int min_pagesz;
  865. size_t orig_size = size;
  866. int ret = 0;
  867. if (unlikely(domain->ops->map == NULL ||
  868. domain->ops->pgsize_bitmap == 0UL))
  869. return -ENODEV;
  870. if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
  871. return -EINVAL;
  872. /* find out the minimum page size supported */
  873. min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap);
  874. /*
  875. * both the virtual address and the physical one, as well as
  876. * the size of the mapping, must be aligned (at least) to the
  877. * size of the smallest page supported by the hardware
  878. */
  879. if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
  880. pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
  881. iova, &paddr, size, min_pagesz);
  882. return -EINVAL;
  883. }
  884. pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
  885. while (size) {
  886. size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
  887. pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
  888. iova, &paddr, pgsize);
  889. ret = domain->ops->map(domain, iova, paddr, pgsize, prot);
  890. if (ret)
  891. break;
  892. iova += pgsize;
  893. paddr += pgsize;
  894. size -= pgsize;
  895. }
  896. /* unroll mapping in case something went wrong */
  897. if (ret)
  898. iommu_unmap(domain, orig_iova, orig_size - size);
  899. else
  900. trace_map(orig_iova, paddr, orig_size);
  901. return ret;
  902. }
  903. EXPORT_SYMBOL_GPL(iommu_map);
  904. size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size)
  905. {
  906. size_t unmapped_page, unmapped = 0;
  907. unsigned int min_pagesz;
  908. unsigned long orig_iova = iova;
  909. if (unlikely(domain->ops->unmap == NULL ||
  910. domain->ops->pgsize_bitmap == 0UL))
  911. return -ENODEV;
  912. if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
  913. return -EINVAL;
  914. /* find out the minimum page size supported */
  915. min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap);
  916. /*
  917. * The virtual address, as well as the size of the mapping, must be
  918. * aligned (at least) to the size of the smallest page supported
  919. * by the hardware
  920. */
  921. if (!IS_ALIGNED(iova | size, min_pagesz)) {
  922. pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
  923. iova, size, min_pagesz);
  924. return -EINVAL;
  925. }
  926. pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
  927. /*
  928. * Keep iterating until we either unmap 'size' bytes (or more)
  929. * or we hit an area that isn't mapped.
  930. */
  931. while (unmapped < size) {
  932. size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
  933. unmapped_page = domain->ops->unmap(domain, iova, pgsize);
  934. if (!unmapped_page)
  935. break;
  936. pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
  937. iova, unmapped_page);
  938. iova += unmapped_page;
  939. unmapped += unmapped_page;
  940. }
  941. trace_unmap(orig_iova, size, unmapped);
  942. return unmapped;
  943. }
  944. EXPORT_SYMBOL_GPL(iommu_unmap);
  945. size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
  946. struct scatterlist *sg, unsigned int nents, int prot)
  947. {
  948. struct scatterlist *s;
  949. size_t mapped = 0;
  950. unsigned int i, min_pagesz;
  951. int ret;
  952. if (unlikely(domain->ops->pgsize_bitmap == 0UL))
  953. return 0;
  954. min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap);
  955. for_each_sg(sg, s, nents, i) {
  956. phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset;
  957. /*
  958. * We are mapping on IOMMU page boundaries, so offset within
  959. * the page must be 0. However, the IOMMU may support pages
  960. * smaller than PAGE_SIZE, so s->offset may still represent
  961. * an offset of that boundary within the CPU page.
  962. */
  963. if (!IS_ALIGNED(s->offset, min_pagesz))
  964. goto out_err;
  965. ret = iommu_map(domain, iova + mapped, phys, s->length, prot);
  966. if (ret)
  967. goto out_err;
  968. mapped += s->length;
  969. }
  970. return mapped;
  971. out_err:
  972. /* undo mappings already done */
  973. iommu_unmap(domain, iova, mapped);
  974. return 0;
  975. }
  976. EXPORT_SYMBOL_GPL(default_iommu_map_sg);
  977. int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
  978. phys_addr_t paddr, u64 size, int prot)
  979. {
  980. if (unlikely(domain->ops->domain_window_enable == NULL))
  981. return -ENODEV;
  982. return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
  983. prot);
  984. }
  985. EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
  986. void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
  987. {
  988. if (unlikely(domain->ops->domain_window_disable == NULL))
  989. return;
  990. return domain->ops->domain_window_disable(domain, wnd_nr);
  991. }
  992. EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
  993. static int __init iommu_init(void)
  994. {
  995. iommu_group_kset = kset_create_and_add("iommu_groups",
  996. NULL, kernel_kobj);
  997. ida_init(&iommu_group_ida);
  998. mutex_init(&iommu_group_mutex);
  999. BUG_ON(!iommu_group_kset);
  1000. return 0;
  1001. }
  1002. arch_initcall(iommu_init);
  1003. int iommu_domain_get_attr(struct iommu_domain *domain,
  1004. enum iommu_attr attr, void *data)
  1005. {
  1006. struct iommu_domain_geometry *geometry;
  1007. bool *paging;
  1008. int ret = 0;
  1009. u32 *count;
  1010. switch (attr) {
  1011. case DOMAIN_ATTR_GEOMETRY:
  1012. geometry = data;
  1013. *geometry = domain->geometry;
  1014. break;
  1015. case DOMAIN_ATTR_PAGING:
  1016. paging = data;
  1017. *paging = (domain->ops->pgsize_bitmap != 0UL);
  1018. break;
  1019. case DOMAIN_ATTR_WINDOWS:
  1020. count = data;
  1021. if (domain->ops->domain_get_windows != NULL)
  1022. *count = domain->ops->domain_get_windows(domain);
  1023. else
  1024. ret = -ENODEV;
  1025. break;
  1026. default:
  1027. if (!domain->ops->domain_get_attr)
  1028. return -EINVAL;
  1029. ret = domain->ops->domain_get_attr(domain, attr, data);
  1030. }
  1031. return ret;
  1032. }
  1033. EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
  1034. int iommu_domain_set_attr(struct iommu_domain *domain,
  1035. enum iommu_attr attr, void *data)
  1036. {
  1037. int ret = 0;
  1038. u32 *count;
  1039. switch (attr) {
  1040. case DOMAIN_ATTR_WINDOWS:
  1041. count = data;
  1042. if (domain->ops->domain_set_windows != NULL)
  1043. ret = domain->ops->domain_set_windows(domain, *count);
  1044. else
  1045. ret = -ENODEV;
  1046. break;
  1047. default:
  1048. if (domain->ops->domain_set_attr == NULL)
  1049. return -EINVAL;
  1050. ret = domain->ops->domain_set_attr(domain, attr, data);
  1051. }
  1052. return ret;
  1053. }
  1054. EXPORT_SYMBOL_GPL(iommu_domain_set_attr);