iommu.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  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) "iommu: " fmt
  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 DEFINE_IDA(iommu_group_ida);
  35. struct iommu_callback_data {
  36. const struct iommu_ops *ops;
  37. };
  38. struct iommu_group {
  39. struct kobject kobj;
  40. struct kobject *devices_kobj;
  41. struct list_head devices;
  42. struct mutex mutex;
  43. struct blocking_notifier_head notifier;
  44. void *iommu_data;
  45. void (*iommu_data_release)(void *iommu_data);
  46. char *name;
  47. int id;
  48. struct iommu_domain *default_domain;
  49. struct iommu_domain *domain;
  50. };
  51. struct iommu_device {
  52. struct list_head list;
  53. struct device *dev;
  54. char *name;
  55. };
  56. struct iommu_group_attribute {
  57. struct attribute attr;
  58. ssize_t (*show)(struct iommu_group *group, char *buf);
  59. ssize_t (*store)(struct iommu_group *group,
  60. const char *buf, size_t count);
  61. };
  62. #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
  63. struct iommu_group_attribute iommu_group_attr_##_name = \
  64. __ATTR(_name, _mode, _show, _store)
  65. #define to_iommu_group_attr(_attr) \
  66. container_of(_attr, struct iommu_group_attribute, attr)
  67. #define to_iommu_group(_kobj) \
  68. container_of(_kobj, struct iommu_group, kobj)
  69. static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
  70. unsigned type);
  71. static int __iommu_attach_device(struct iommu_domain *domain,
  72. struct device *dev);
  73. static int __iommu_attach_group(struct iommu_domain *domain,
  74. struct iommu_group *group);
  75. static void __iommu_detach_group(struct iommu_domain *domain,
  76. struct iommu_group *group);
  77. static ssize_t iommu_group_attr_show(struct kobject *kobj,
  78. struct attribute *__attr, char *buf)
  79. {
  80. struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
  81. struct iommu_group *group = to_iommu_group(kobj);
  82. ssize_t ret = -EIO;
  83. if (attr->show)
  84. ret = attr->show(group, buf);
  85. return ret;
  86. }
  87. static ssize_t iommu_group_attr_store(struct kobject *kobj,
  88. struct attribute *__attr,
  89. const char *buf, size_t count)
  90. {
  91. struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
  92. struct iommu_group *group = to_iommu_group(kobj);
  93. ssize_t ret = -EIO;
  94. if (attr->store)
  95. ret = attr->store(group, buf, count);
  96. return ret;
  97. }
  98. static const struct sysfs_ops iommu_group_sysfs_ops = {
  99. .show = iommu_group_attr_show,
  100. .store = iommu_group_attr_store,
  101. };
  102. static int iommu_group_create_file(struct iommu_group *group,
  103. struct iommu_group_attribute *attr)
  104. {
  105. return sysfs_create_file(&group->kobj, &attr->attr);
  106. }
  107. static void iommu_group_remove_file(struct iommu_group *group,
  108. struct iommu_group_attribute *attr)
  109. {
  110. sysfs_remove_file(&group->kobj, &attr->attr);
  111. }
  112. static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
  113. {
  114. return sprintf(buf, "%s\n", group->name);
  115. }
  116. static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
  117. static void iommu_group_release(struct kobject *kobj)
  118. {
  119. struct iommu_group *group = to_iommu_group(kobj);
  120. pr_debug("Releasing group %d\n", group->id);
  121. if (group->iommu_data_release)
  122. group->iommu_data_release(group->iommu_data);
  123. ida_simple_remove(&iommu_group_ida, group->id);
  124. if (group->default_domain)
  125. iommu_domain_free(group->default_domain);
  126. kfree(group->name);
  127. kfree(group);
  128. }
  129. static struct kobj_type iommu_group_ktype = {
  130. .sysfs_ops = &iommu_group_sysfs_ops,
  131. .release = iommu_group_release,
  132. };
  133. /**
  134. * iommu_group_alloc - Allocate a new group
  135. * @name: Optional name to associate with group, visible in sysfs
  136. *
  137. * This function is called by an iommu driver to allocate a new iommu
  138. * group. The iommu group represents the minimum granularity of the iommu.
  139. * Upon successful return, the caller holds a reference to the supplied
  140. * group in order to hold the group until devices are added. Use
  141. * iommu_group_put() to release this extra reference count, allowing the
  142. * group to be automatically reclaimed once it has no devices or external
  143. * references.
  144. */
  145. struct iommu_group *iommu_group_alloc(void)
  146. {
  147. struct iommu_group *group;
  148. int ret;
  149. group = kzalloc(sizeof(*group), GFP_KERNEL);
  150. if (!group)
  151. return ERR_PTR(-ENOMEM);
  152. group->kobj.kset = iommu_group_kset;
  153. mutex_init(&group->mutex);
  154. INIT_LIST_HEAD(&group->devices);
  155. BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
  156. ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
  157. if (ret < 0) {
  158. kfree(group);
  159. return ERR_PTR(ret);
  160. }
  161. group->id = ret;
  162. ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
  163. NULL, "%d", group->id);
  164. if (ret) {
  165. ida_simple_remove(&iommu_group_ida, group->id);
  166. kfree(group);
  167. return ERR_PTR(ret);
  168. }
  169. group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
  170. if (!group->devices_kobj) {
  171. kobject_put(&group->kobj); /* triggers .release & free */
  172. return ERR_PTR(-ENOMEM);
  173. }
  174. /*
  175. * The devices_kobj holds a reference on the group kobject, so
  176. * as long as that exists so will the group. We can therefore
  177. * use the devices_kobj for reference counting.
  178. */
  179. kobject_put(&group->kobj);
  180. pr_debug("Allocated group %d\n", group->id);
  181. return group;
  182. }
  183. EXPORT_SYMBOL_GPL(iommu_group_alloc);
  184. struct iommu_group *iommu_group_get_by_id(int id)
  185. {
  186. struct kobject *group_kobj;
  187. struct iommu_group *group;
  188. const char *name;
  189. if (!iommu_group_kset)
  190. return NULL;
  191. name = kasprintf(GFP_KERNEL, "%d", id);
  192. if (!name)
  193. return NULL;
  194. group_kobj = kset_find_obj(iommu_group_kset, name);
  195. kfree(name);
  196. if (!group_kobj)
  197. return NULL;
  198. group = container_of(group_kobj, struct iommu_group, kobj);
  199. BUG_ON(group->id != id);
  200. kobject_get(group->devices_kobj);
  201. kobject_put(&group->kobj);
  202. return group;
  203. }
  204. EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
  205. /**
  206. * iommu_group_get_iommudata - retrieve iommu_data registered for a group
  207. * @group: the group
  208. *
  209. * iommu drivers can store data in the group for use when doing iommu
  210. * operations. This function provides a way to retrieve it. Caller
  211. * should hold a group reference.
  212. */
  213. void *iommu_group_get_iommudata(struct iommu_group *group)
  214. {
  215. return group->iommu_data;
  216. }
  217. EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
  218. /**
  219. * iommu_group_set_iommudata - set iommu_data for a group
  220. * @group: the group
  221. * @iommu_data: new data
  222. * @release: release function for iommu_data
  223. *
  224. * iommu drivers can store data in the group for use when doing iommu
  225. * operations. This function provides a way to set the data after
  226. * the group has been allocated. Caller should hold a group reference.
  227. */
  228. void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
  229. void (*release)(void *iommu_data))
  230. {
  231. group->iommu_data = iommu_data;
  232. group->iommu_data_release = release;
  233. }
  234. EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
  235. /**
  236. * iommu_group_set_name - set name for a group
  237. * @group: the group
  238. * @name: name
  239. *
  240. * Allow iommu driver to set a name for a group. When set it will
  241. * appear in a name attribute file under the group in sysfs.
  242. */
  243. int iommu_group_set_name(struct iommu_group *group, const char *name)
  244. {
  245. int ret;
  246. if (group->name) {
  247. iommu_group_remove_file(group, &iommu_group_attr_name);
  248. kfree(group->name);
  249. group->name = NULL;
  250. if (!name)
  251. return 0;
  252. }
  253. group->name = kstrdup(name, GFP_KERNEL);
  254. if (!group->name)
  255. return -ENOMEM;
  256. ret = iommu_group_create_file(group, &iommu_group_attr_name);
  257. if (ret) {
  258. kfree(group->name);
  259. group->name = NULL;
  260. return ret;
  261. }
  262. return 0;
  263. }
  264. EXPORT_SYMBOL_GPL(iommu_group_set_name);
  265. static int iommu_group_create_direct_mappings(struct iommu_group *group,
  266. struct device *dev)
  267. {
  268. struct iommu_domain *domain = group->default_domain;
  269. struct iommu_dm_region *entry;
  270. struct list_head mappings;
  271. unsigned long pg_size;
  272. int ret = 0;
  273. if (!domain || domain->type != IOMMU_DOMAIN_DMA)
  274. return 0;
  275. BUG_ON(!domain->pgsize_bitmap);
  276. pg_size = 1UL << __ffs(domain->pgsize_bitmap);
  277. INIT_LIST_HEAD(&mappings);
  278. iommu_get_dm_regions(dev, &mappings);
  279. /* We need to consider overlapping regions for different devices */
  280. list_for_each_entry(entry, &mappings, list) {
  281. dma_addr_t start, end, addr;
  282. if (domain->ops->apply_dm_region)
  283. domain->ops->apply_dm_region(dev, domain, entry);
  284. start = ALIGN(entry->start, pg_size);
  285. end = ALIGN(entry->start + entry->length, pg_size);
  286. for (addr = start; addr < end; addr += pg_size) {
  287. phys_addr_t phys_addr;
  288. phys_addr = iommu_iova_to_phys(domain, addr);
  289. if (phys_addr)
  290. continue;
  291. ret = iommu_map(domain, addr, addr, pg_size, entry->prot);
  292. if (ret)
  293. goto out;
  294. }
  295. }
  296. out:
  297. iommu_put_dm_regions(dev, &mappings);
  298. return ret;
  299. }
  300. /**
  301. * iommu_group_add_device - add a device to an iommu group
  302. * @group: the group into which to add the device (reference should be held)
  303. * @dev: the device
  304. *
  305. * This function is called by an iommu driver to add a device into a
  306. * group. Adding a device increments the group reference count.
  307. */
  308. int iommu_group_add_device(struct iommu_group *group, struct device *dev)
  309. {
  310. int ret, i = 0;
  311. struct iommu_device *device;
  312. device = kzalloc(sizeof(*device), GFP_KERNEL);
  313. if (!device)
  314. return -ENOMEM;
  315. device->dev = dev;
  316. ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
  317. if (ret) {
  318. kfree(device);
  319. return ret;
  320. }
  321. device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
  322. rename:
  323. if (!device->name) {
  324. sysfs_remove_link(&dev->kobj, "iommu_group");
  325. kfree(device);
  326. return -ENOMEM;
  327. }
  328. ret = sysfs_create_link_nowarn(group->devices_kobj,
  329. &dev->kobj, device->name);
  330. if (ret) {
  331. kfree(device->name);
  332. if (ret == -EEXIST && i >= 0) {
  333. /*
  334. * Account for the slim chance of collision
  335. * and append an instance to the name.
  336. */
  337. device->name = kasprintf(GFP_KERNEL, "%s.%d",
  338. kobject_name(&dev->kobj), i++);
  339. goto rename;
  340. }
  341. sysfs_remove_link(&dev->kobj, "iommu_group");
  342. kfree(device);
  343. return ret;
  344. }
  345. kobject_get(group->devices_kobj);
  346. dev->iommu_group = group;
  347. iommu_group_create_direct_mappings(group, dev);
  348. mutex_lock(&group->mutex);
  349. list_add_tail(&device->list, &group->devices);
  350. if (group->domain)
  351. __iommu_attach_device(group->domain, dev);
  352. mutex_unlock(&group->mutex);
  353. /* Notify any listeners about change to group. */
  354. blocking_notifier_call_chain(&group->notifier,
  355. IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
  356. trace_add_device_to_group(group->id, dev);
  357. pr_info("Adding device %s to group %d\n", dev_name(dev), group->id);
  358. return 0;
  359. }
  360. EXPORT_SYMBOL_GPL(iommu_group_add_device);
  361. /**
  362. * iommu_group_remove_device - remove a device from it's current group
  363. * @dev: device to be removed
  364. *
  365. * This function is called by an iommu driver to remove the device from
  366. * it's current group. This decrements the iommu group reference count.
  367. */
  368. void iommu_group_remove_device(struct device *dev)
  369. {
  370. struct iommu_group *group = dev->iommu_group;
  371. struct iommu_device *tmp_device, *device = NULL;
  372. pr_info("Removing device %s from group %d\n", dev_name(dev), group->id);
  373. /* Pre-notify listeners that a device is being removed. */
  374. blocking_notifier_call_chain(&group->notifier,
  375. IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
  376. mutex_lock(&group->mutex);
  377. list_for_each_entry(tmp_device, &group->devices, list) {
  378. if (tmp_device->dev == dev) {
  379. device = tmp_device;
  380. list_del(&device->list);
  381. break;
  382. }
  383. }
  384. mutex_unlock(&group->mutex);
  385. if (!device)
  386. return;
  387. sysfs_remove_link(group->devices_kobj, device->name);
  388. sysfs_remove_link(&dev->kobj, "iommu_group");
  389. trace_remove_device_from_group(group->id, dev);
  390. kfree(device->name);
  391. kfree(device);
  392. dev->iommu_group = NULL;
  393. kobject_put(group->devices_kobj);
  394. }
  395. EXPORT_SYMBOL_GPL(iommu_group_remove_device);
  396. static int iommu_group_device_count(struct iommu_group *group)
  397. {
  398. struct iommu_device *entry;
  399. int ret = 0;
  400. list_for_each_entry(entry, &group->devices, list)
  401. ret++;
  402. return ret;
  403. }
  404. /**
  405. * iommu_group_for_each_dev - iterate over each device in the group
  406. * @group: the group
  407. * @data: caller opaque data to be passed to callback function
  408. * @fn: caller supplied callback function
  409. *
  410. * This function is called by group users to iterate over group devices.
  411. * Callers should hold a reference count to the group during callback.
  412. * The group->mutex is held across callbacks, which will block calls to
  413. * iommu_group_add/remove_device.
  414. */
  415. static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
  416. int (*fn)(struct device *, void *))
  417. {
  418. struct iommu_device *device;
  419. int ret = 0;
  420. list_for_each_entry(device, &group->devices, list) {
  421. ret = fn(device->dev, data);
  422. if (ret)
  423. break;
  424. }
  425. return ret;
  426. }
  427. int iommu_group_for_each_dev(struct iommu_group *group, void *data,
  428. int (*fn)(struct device *, void *))
  429. {
  430. int ret;
  431. mutex_lock(&group->mutex);
  432. ret = __iommu_group_for_each_dev(group, data, fn);
  433. mutex_unlock(&group->mutex);
  434. return ret;
  435. }
  436. EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
  437. /**
  438. * iommu_group_get - Return the group for a device and increment reference
  439. * @dev: get the group that this device belongs to
  440. *
  441. * This function is called by iommu drivers and users to get the group
  442. * for the specified device. If found, the group is returned and the group
  443. * reference in incremented, else NULL.
  444. */
  445. struct iommu_group *iommu_group_get(struct device *dev)
  446. {
  447. struct iommu_group *group = dev->iommu_group;
  448. if (group)
  449. kobject_get(group->devices_kobj);
  450. return group;
  451. }
  452. EXPORT_SYMBOL_GPL(iommu_group_get);
  453. /**
  454. * iommu_group_put - Decrement group reference
  455. * @group: the group to use
  456. *
  457. * This function is called by iommu drivers and users to release the
  458. * iommu group. Once the reference count is zero, the group is released.
  459. */
  460. void iommu_group_put(struct iommu_group *group)
  461. {
  462. if (group)
  463. kobject_put(group->devices_kobj);
  464. }
  465. EXPORT_SYMBOL_GPL(iommu_group_put);
  466. /**
  467. * iommu_group_register_notifier - Register a notifier for group changes
  468. * @group: the group to watch
  469. * @nb: notifier block to signal
  470. *
  471. * This function allows iommu group users to track changes in a group.
  472. * See include/linux/iommu.h for actions sent via this notifier. Caller
  473. * should hold a reference to the group throughout notifier registration.
  474. */
  475. int iommu_group_register_notifier(struct iommu_group *group,
  476. struct notifier_block *nb)
  477. {
  478. return blocking_notifier_chain_register(&group->notifier, nb);
  479. }
  480. EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
  481. /**
  482. * iommu_group_unregister_notifier - Unregister a notifier
  483. * @group: the group to watch
  484. * @nb: notifier block to signal
  485. *
  486. * Unregister a previously registered group notifier block.
  487. */
  488. int iommu_group_unregister_notifier(struct iommu_group *group,
  489. struct notifier_block *nb)
  490. {
  491. return blocking_notifier_chain_unregister(&group->notifier, nb);
  492. }
  493. EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
  494. /**
  495. * iommu_group_id - Return ID for a group
  496. * @group: the group to ID
  497. *
  498. * Return the unique ID for the group matching the sysfs group number.
  499. */
  500. int iommu_group_id(struct iommu_group *group)
  501. {
  502. return group->id;
  503. }
  504. EXPORT_SYMBOL_GPL(iommu_group_id);
  505. static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
  506. unsigned long *devfns);
  507. /*
  508. * To consider a PCI device isolated, we require ACS to support Source
  509. * Validation, Request Redirection, Completer Redirection, and Upstream
  510. * Forwarding. This effectively means that devices cannot spoof their
  511. * requester ID, requests and completions cannot be redirected, and all
  512. * transactions are forwarded upstream, even as it passes through a
  513. * bridge where the target device is downstream.
  514. */
  515. #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
  516. /*
  517. * For multifunction devices which are not isolated from each other, find
  518. * all the other non-isolated functions and look for existing groups. For
  519. * each function, we also need to look for aliases to or from other devices
  520. * that may already have a group.
  521. */
  522. static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
  523. unsigned long *devfns)
  524. {
  525. struct pci_dev *tmp = NULL;
  526. struct iommu_group *group;
  527. if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
  528. return NULL;
  529. for_each_pci_dev(tmp) {
  530. if (tmp == pdev || tmp->bus != pdev->bus ||
  531. PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
  532. pci_acs_enabled(tmp, REQ_ACS_FLAGS))
  533. continue;
  534. group = get_pci_alias_group(tmp, devfns);
  535. if (group) {
  536. pci_dev_put(tmp);
  537. return group;
  538. }
  539. }
  540. return NULL;
  541. }
  542. /*
  543. * Look for aliases to or from the given device for existing groups. DMA
  544. * aliases are only supported on the same bus, therefore the search
  545. * space is quite small (especially since we're really only looking at pcie
  546. * device, and therefore only expect multiple slots on the root complex or
  547. * downstream switch ports). It's conceivable though that a pair of
  548. * multifunction devices could have aliases between them that would cause a
  549. * loop. To prevent this, we use a bitmap to track where we've been.
  550. */
  551. static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
  552. unsigned long *devfns)
  553. {
  554. struct pci_dev *tmp = NULL;
  555. struct iommu_group *group;
  556. if (test_and_set_bit(pdev->devfn & 0xff, devfns))
  557. return NULL;
  558. group = iommu_group_get(&pdev->dev);
  559. if (group)
  560. return group;
  561. for_each_pci_dev(tmp) {
  562. if (tmp == pdev || tmp->bus != pdev->bus)
  563. continue;
  564. /* We alias them or they alias us */
  565. if (pci_devs_are_dma_aliases(pdev, tmp)) {
  566. group = get_pci_alias_group(tmp, devfns);
  567. if (group) {
  568. pci_dev_put(tmp);
  569. return group;
  570. }
  571. group = get_pci_function_alias_group(tmp, devfns);
  572. if (group) {
  573. pci_dev_put(tmp);
  574. return group;
  575. }
  576. }
  577. }
  578. return NULL;
  579. }
  580. struct group_for_pci_data {
  581. struct pci_dev *pdev;
  582. struct iommu_group *group;
  583. };
  584. /*
  585. * DMA alias iterator callback, return the last seen device. Stop and return
  586. * the IOMMU group if we find one along the way.
  587. */
  588. static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
  589. {
  590. struct group_for_pci_data *data = opaque;
  591. data->pdev = pdev;
  592. data->group = iommu_group_get(&pdev->dev);
  593. return data->group != NULL;
  594. }
  595. /*
  596. * Generic device_group call-back function. It just allocates one
  597. * iommu-group per device.
  598. */
  599. struct iommu_group *generic_device_group(struct device *dev)
  600. {
  601. struct iommu_group *group;
  602. group = iommu_group_alloc();
  603. if (IS_ERR(group))
  604. return NULL;
  605. return group;
  606. }
  607. /*
  608. * Use standard PCI bus topology, isolation features, and DMA alias quirks
  609. * to find or create an IOMMU group for a device.
  610. */
  611. struct iommu_group *pci_device_group(struct device *dev)
  612. {
  613. struct pci_dev *pdev = to_pci_dev(dev);
  614. struct group_for_pci_data data;
  615. struct pci_bus *bus;
  616. struct iommu_group *group = NULL;
  617. u64 devfns[4] = { 0 };
  618. if (WARN_ON(!dev_is_pci(dev)))
  619. return ERR_PTR(-EINVAL);
  620. /*
  621. * Find the upstream DMA alias for the device. A device must not
  622. * be aliased due to topology in order to have its own IOMMU group.
  623. * If we find an alias along the way that already belongs to a
  624. * group, use it.
  625. */
  626. if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
  627. return data.group;
  628. pdev = data.pdev;
  629. /*
  630. * Continue upstream from the point of minimum IOMMU granularity
  631. * due to aliases to the point where devices are protected from
  632. * peer-to-peer DMA by PCI ACS. Again, if we find an existing
  633. * group, use it.
  634. */
  635. for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
  636. if (!bus->self)
  637. continue;
  638. if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
  639. break;
  640. pdev = bus->self;
  641. group = iommu_group_get(&pdev->dev);
  642. if (group)
  643. return group;
  644. }
  645. /*
  646. * Look for existing groups on device aliases. If we alias another
  647. * device or another device aliases us, use the same group.
  648. */
  649. group = get_pci_alias_group(pdev, (unsigned long *)devfns);
  650. if (group)
  651. return group;
  652. /*
  653. * Look for existing groups on non-isolated functions on the same
  654. * slot and aliases of those funcions, if any. No need to clear
  655. * the search bitmap, the tested devfns are still valid.
  656. */
  657. group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
  658. if (group)
  659. return group;
  660. /* No shared group found, allocate new */
  661. group = iommu_group_alloc();
  662. if (IS_ERR(group))
  663. return NULL;
  664. return group;
  665. }
  666. /**
  667. * iommu_group_get_for_dev - Find or create the IOMMU group for a device
  668. * @dev: target device
  669. *
  670. * This function is intended to be called by IOMMU drivers and extended to
  671. * support common, bus-defined algorithms when determining or creating the
  672. * IOMMU group for a device. On success, the caller will hold a reference
  673. * to the returned IOMMU group, which will already include the provided
  674. * device. The reference should be released with iommu_group_put().
  675. */
  676. struct iommu_group *iommu_group_get_for_dev(struct device *dev)
  677. {
  678. const struct iommu_ops *ops = dev->bus->iommu_ops;
  679. struct iommu_group *group;
  680. int ret;
  681. group = iommu_group_get(dev);
  682. if (group)
  683. return group;
  684. group = ERR_PTR(-EINVAL);
  685. if (ops && ops->device_group)
  686. group = ops->device_group(dev);
  687. if (IS_ERR(group))
  688. return group;
  689. /*
  690. * Try to allocate a default domain - needs support from the
  691. * IOMMU driver.
  692. */
  693. if (!group->default_domain) {
  694. group->default_domain = __iommu_domain_alloc(dev->bus,
  695. IOMMU_DOMAIN_DMA);
  696. if (!group->domain)
  697. group->domain = group->default_domain;
  698. }
  699. ret = iommu_group_add_device(group, dev);
  700. if (ret) {
  701. iommu_group_put(group);
  702. return ERR_PTR(ret);
  703. }
  704. return group;
  705. }
  706. struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
  707. {
  708. return group->default_domain;
  709. }
  710. static int add_iommu_group(struct device *dev, void *data)
  711. {
  712. struct iommu_callback_data *cb = data;
  713. const struct iommu_ops *ops = cb->ops;
  714. int ret;
  715. if (!ops->add_device)
  716. return 0;
  717. WARN_ON(dev->iommu_group);
  718. ret = ops->add_device(dev);
  719. /*
  720. * We ignore -ENODEV errors for now, as they just mean that the
  721. * device is not translated by an IOMMU. We still care about
  722. * other errors and fail to initialize when they happen.
  723. */
  724. if (ret == -ENODEV)
  725. ret = 0;
  726. return ret;
  727. }
  728. static int remove_iommu_group(struct device *dev, void *data)
  729. {
  730. struct iommu_callback_data *cb = data;
  731. const struct iommu_ops *ops = cb->ops;
  732. if (ops->remove_device && dev->iommu_group)
  733. ops->remove_device(dev);
  734. return 0;
  735. }
  736. static int iommu_bus_notifier(struct notifier_block *nb,
  737. unsigned long action, void *data)
  738. {
  739. struct device *dev = data;
  740. const struct iommu_ops *ops = dev->bus->iommu_ops;
  741. struct iommu_group *group;
  742. unsigned long group_action = 0;
  743. /*
  744. * ADD/DEL call into iommu driver ops if provided, which may
  745. * result in ADD/DEL notifiers to group->notifier
  746. */
  747. if (action == BUS_NOTIFY_ADD_DEVICE) {
  748. if (ops->add_device)
  749. return ops->add_device(dev);
  750. } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
  751. if (ops->remove_device && dev->iommu_group) {
  752. ops->remove_device(dev);
  753. return 0;
  754. }
  755. }
  756. /*
  757. * Remaining BUS_NOTIFYs get filtered and republished to the
  758. * group, if anyone is listening
  759. */
  760. group = iommu_group_get(dev);
  761. if (!group)
  762. return 0;
  763. switch (action) {
  764. case BUS_NOTIFY_BIND_DRIVER:
  765. group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
  766. break;
  767. case BUS_NOTIFY_BOUND_DRIVER:
  768. group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
  769. break;
  770. case BUS_NOTIFY_UNBIND_DRIVER:
  771. group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
  772. break;
  773. case BUS_NOTIFY_UNBOUND_DRIVER:
  774. group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
  775. break;
  776. }
  777. if (group_action)
  778. blocking_notifier_call_chain(&group->notifier,
  779. group_action, dev);
  780. iommu_group_put(group);
  781. return 0;
  782. }
  783. static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
  784. {
  785. int err;
  786. struct notifier_block *nb;
  787. struct iommu_callback_data cb = {
  788. .ops = ops,
  789. };
  790. nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
  791. if (!nb)
  792. return -ENOMEM;
  793. nb->notifier_call = iommu_bus_notifier;
  794. err = bus_register_notifier(bus, nb);
  795. if (err)
  796. goto out_free;
  797. err = bus_for_each_dev(bus, NULL, &cb, add_iommu_group);
  798. if (err)
  799. goto out_err;
  800. return 0;
  801. out_err:
  802. /* Clean up */
  803. bus_for_each_dev(bus, NULL, &cb, remove_iommu_group);
  804. bus_unregister_notifier(bus, nb);
  805. out_free:
  806. kfree(nb);
  807. return err;
  808. }
  809. /**
  810. * bus_set_iommu - set iommu-callbacks for the bus
  811. * @bus: bus.
  812. * @ops: the callbacks provided by the iommu-driver
  813. *
  814. * This function is called by an iommu driver to set the iommu methods
  815. * used for a particular bus. Drivers for devices on that bus can use
  816. * the iommu-api after these ops are registered.
  817. * This special function is needed because IOMMUs are usually devices on
  818. * the bus itself, so the iommu drivers are not initialized when the bus
  819. * is set up. With this function the iommu-driver can set the iommu-ops
  820. * afterwards.
  821. */
  822. int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
  823. {
  824. int err;
  825. if (bus->iommu_ops != NULL)
  826. return -EBUSY;
  827. bus->iommu_ops = ops;
  828. /* Do IOMMU specific setup for this bus-type */
  829. err = iommu_bus_init(bus, ops);
  830. if (err)
  831. bus->iommu_ops = NULL;
  832. return err;
  833. }
  834. EXPORT_SYMBOL_GPL(bus_set_iommu);
  835. bool iommu_present(struct bus_type *bus)
  836. {
  837. return bus->iommu_ops != NULL;
  838. }
  839. EXPORT_SYMBOL_GPL(iommu_present);
  840. bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
  841. {
  842. if (!bus->iommu_ops || !bus->iommu_ops->capable)
  843. return false;
  844. return bus->iommu_ops->capable(cap);
  845. }
  846. EXPORT_SYMBOL_GPL(iommu_capable);
  847. /**
  848. * iommu_set_fault_handler() - set a fault handler for an iommu domain
  849. * @domain: iommu domain
  850. * @handler: fault handler
  851. * @token: user data, will be passed back to the fault handler
  852. *
  853. * This function should be used by IOMMU users which want to be notified
  854. * whenever an IOMMU fault happens.
  855. *
  856. * The fault handler itself should return 0 on success, and an appropriate
  857. * error code otherwise.
  858. */
  859. void iommu_set_fault_handler(struct iommu_domain *domain,
  860. iommu_fault_handler_t handler,
  861. void *token)
  862. {
  863. BUG_ON(!domain);
  864. domain->handler = handler;
  865. domain->handler_token = token;
  866. }
  867. EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
  868. static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
  869. unsigned type)
  870. {
  871. struct iommu_domain *domain;
  872. if (bus == NULL || bus->iommu_ops == NULL)
  873. return NULL;
  874. domain = bus->iommu_ops->domain_alloc(type);
  875. if (!domain)
  876. return NULL;
  877. domain->ops = bus->iommu_ops;
  878. domain->type = type;
  879. /* Assume all sizes by default; the driver may override this later */
  880. domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
  881. return domain;
  882. }
  883. struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
  884. {
  885. return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
  886. }
  887. EXPORT_SYMBOL_GPL(iommu_domain_alloc);
  888. void iommu_domain_free(struct iommu_domain *domain)
  889. {
  890. domain->ops->domain_free(domain);
  891. }
  892. EXPORT_SYMBOL_GPL(iommu_domain_free);
  893. static int __iommu_attach_device(struct iommu_domain *domain,
  894. struct device *dev)
  895. {
  896. int ret;
  897. if (unlikely(domain->ops->attach_dev == NULL))
  898. return -ENODEV;
  899. ret = domain->ops->attach_dev(domain, dev);
  900. if (!ret)
  901. trace_attach_device_to_domain(dev);
  902. return ret;
  903. }
  904. int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
  905. {
  906. struct iommu_group *group;
  907. int ret;
  908. group = iommu_group_get(dev);
  909. /* FIXME: Remove this when groups a mandatory for iommu drivers */
  910. if (group == NULL)
  911. return __iommu_attach_device(domain, dev);
  912. /*
  913. * We have a group - lock it to make sure the device-count doesn't
  914. * change while we are attaching
  915. */
  916. mutex_lock(&group->mutex);
  917. ret = -EINVAL;
  918. if (iommu_group_device_count(group) != 1)
  919. goto out_unlock;
  920. ret = __iommu_attach_group(domain, group);
  921. out_unlock:
  922. mutex_unlock(&group->mutex);
  923. iommu_group_put(group);
  924. return ret;
  925. }
  926. EXPORT_SYMBOL_GPL(iommu_attach_device);
  927. static void __iommu_detach_device(struct iommu_domain *domain,
  928. struct device *dev)
  929. {
  930. if (unlikely(domain->ops->detach_dev == NULL))
  931. return;
  932. domain->ops->detach_dev(domain, dev);
  933. trace_detach_device_from_domain(dev);
  934. }
  935. void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
  936. {
  937. struct iommu_group *group;
  938. group = iommu_group_get(dev);
  939. /* FIXME: Remove this when groups a mandatory for iommu drivers */
  940. if (group == NULL)
  941. return __iommu_detach_device(domain, dev);
  942. mutex_lock(&group->mutex);
  943. if (iommu_group_device_count(group) != 1) {
  944. WARN_ON(1);
  945. goto out_unlock;
  946. }
  947. __iommu_detach_group(domain, group);
  948. out_unlock:
  949. mutex_unlock(&group->mutex);
  950. iommu_group_put(group);
  951. }
  952. EXPORT_SYMBOL_GPL(iommu_detach_device);
  953. struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
  954. {
  955. struct iommu_domain *domain;
  956. struct iommu_group *group;
  957. group = iommu_group_get(dev);
  958. /* FIXME: Remove this when groups a mandatory for iommu drivers */
  959. if (group == NULL)
  960. return NULL;
  961. domain = group->domain;
  962. iommu_group_put(group);
  963. return domain;
  964. }
  965. EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
  966. /*
  967. * IOMMU groups are really the natrual working unit of the IOMMU, but
  968. * the IOMMU API works on domains and devices. Bridge that gap by
  969. * iterating over the devices in a group. Ideally we'd have a single
  970. * device which represents the requestor ID of the group, but we also
  971. * allow IOMMU drivers to create policy defined minimum sets, where
  972. * the physical hardware may be able to distiguish members, but we
  973. * wish to group them at a higher level (ex. untrusted multi-function
  974. * PCI devices). Thus we attach each device.
  975. */
  976. static int iommu_group_do_attach_device(struct device *dev, void *data)
  977. {
  978. struct iommu_domain *domain = data;
  979. return __iommu_attach_device(domain, dev);
  980. }
  981. static int __iommu_attach_group(struct iommu_domain *domain,
  982. struct iommu_group *group)
  983. {
  984. int ret;
  985. if (group->default_domain && group->domain != group->default_domain)
  986. return -EBUSY;
  987. ret = __iommu_group_for_each_dev(group, domain,
  988. iommu_group_do_attach_device);
  989. if (ret == 0)
  990. group->domain = domain;
  991. return ret;
  992. }
  993. int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
  994. {
  995. int ret;
  996. mutex_lock(&group->mutex);
  997. ret = __iommu_attach_group(domain, group);
  998. mutex_unlock(&group->mutex);
  999. return ret;
  1000. }
  1001. EXPORT_SYMBOL_GPL(iommu_attach_group);
  1002. static int iommu_group_do_detach_device(struct device *dev, void *data)
  1003. {
  1004. struct iommu_domain *domain = data;
  1005. __iommu_detach_device(domain, dev);
  1006. return 0;
  1007. }
  1008. static void __iommu_detach_group(struct iommu_domain *domain,
  1009. struct iommu_group *group)
  1010. {
  1011. int ret;
  1012. if (!group->default_domain) {
  1013. __iommu_group_for_each_dev(group, domain,
  1014. iommu_group_do_detach_device);
  1015. group->domain = NULL;
  1016. return;
  1017. }
  1018. if (group->domain == group->default_domain)
  1019. return;
  1020. /* Detach by re-attaching to the default domain */
  1021. ret = __iommu_group_for_each_dev(group, group->default_domain,
  1022. iommu_group_do_attach_device);
  1023. if (ret != 0)
  1024. WARN_ON(1);
  1025. else
  1026. group->domain = group->default_domain;
  1027. }
  1028. void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
  1029. {
  1030. mutex_lock(&group->mutex);
  1031. __iommu_detach_group(domain, group);
  1032. mutex_unlock(&group->mutex);
  1033. }
  1034. EXPORT_SYMBOL_GPL(iommu_detach_group);
  1035. phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
  1036. {
  1037. if (unlikely(domain->ops->iova_to_phys == NULL))
  1038. return 0;
  1039. return domain->ops->iova_to_phys(domain, iova);
  1040. }
  1041. EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
  1042. static size_t iommu_pgsize(struct iommu_domain *domain,
  1043. unsigned long addr_merge, size_t size)
  1044. {
  1045. unsigned int pgsize_idx;
  1046. size_t pgsize;
  1047. /* Max page size that still fits into 'size' */
  1048. pgsize_idx = __fls(size);
  1049. /* need to consider alignment requirements ? */
  1050. if (likely(addr_merge)) {
  1051. /* Max page size allowed by address */
  1052. unsigned int align_pgsize_idx = __ffs(addr_merge);
  1053. pgsize_idx = min(pgsize_idx, align_pgsize_idx);
  1054. }
  1055. /* build a mask of acceptable page sizes */
  1056. pgsize = (1UL << (pgsize_idx + 1)) - 1;
  1057. /* throw away page sizes not supported by the hardware */
  1058. pgsize &= domain->pgsize_bitmap;
  1059. /* make sure we're still sane */
  1060. BUG_ON(!pgsize);
  1061. /* pick the biggest page */
  1062. pgsize_idx = __fls(pgsize);
  1063. pgsize = 1UL << pgsize_idx;
  1064. return pgsize;
  1065. }
  1066. int iommu_map(struct iommu_domain *domain, unsigned long iova,
  1067. phys_addr_t paddr, size_t size, int prot)
  1068. {
  1069. unsigned long orig_iova = iova;
  1070. unsigned int min_pagesz;
  1071. size_t orig_size = size;
  1072. phys_addr_t orig_paddr = paddr;
  1073. int ret = 0;
  1074. if (unlikely(domain->ops->map == NULL ||
  1075. domain->pgsize_bitmap == 0UL))
  1076. return -ENODEV;
  1077. if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
  1078. return -EINVAL;
  1079. /* find out the minimum page size supported */
  1080. min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
  1081. /*
  1082. * both the virtual address and the physical one, as well as
  1083. * the size of the mapping, must be aligned (at least) to the
  1084. * size of the smallest page supported by the hardware
  1085. */
  1086. if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
  1087. pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
  1088. iova, &paddr, size, min_pagesz);
  1089. return -EINVAL;
  1090. }
  1091. pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
  1092. while (size) {
  1093. size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
  1094. pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
  1095. iova, &paddr, pgsize);
  1096. ret = domain->ops->map(domain, iova, paddr, pgsize, prot);
  1097. if (ret)
  1098. break;
  1099. iova += pgsize;
  1100. paddr += pgsize;
  1101. size -= pgsize;
  1102. }
  1103. /* unroll mapping in case something went wrong */
  1104. if (ret)
  1105. iommu_unmap(domain, orig_iova, orig_size - size);
  1106. else
  1107. trace_map(orig_iova, orig_paddr, orig_size);
  1108. return ret;
  1109. }
  1110. EXPORT_SYMBOL_GPL(iommu_map);
  1111. size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size)
  1112. {
  1113. size_t unmapped_page, unmapped = 0;
  1114. unsigned int min_pagesz;
  1115. unsigned long orig_iova = iova;
  1116. if (unlikely(domain->ops->unmap == NULL ||
  1117. domain->pgsize_bitmap == 0UL))
  1118. return -ENODEV;
  1119. if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
  1120. return -EINVAL;
  1121. /* find out the minimum page size supported */
  1122. min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
  1123. /*
  1124. * The virtual address, as well as the size of the mapping, must be
  1125. * aligned (at least) to the size of the smallest page supported
  1126. * by the hardware
  1127. */
  1128. if (!IS_ALIGNED(iova | size, min_pagesz)) {
  1129. pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
  1130. iova, size, min_pagesz);
  1131. return -EINVAL;
  1132. }
  1133. pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
  1134. /*
  1135. * Keep iterating until we either unmap 'size' bytes (or more)
  1136. * or we hit an area that isn't mapped.
  1137. */
  1138. while (unmapped < size) {
  1139. size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
  1140. unmapped_page = domain->ops->unmap(domain, iova, pgsize);
  1141. if (!unmapped_page)
  1142. break;
  1143. pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
  1144. iova, unmapped_page);
  1145. iova += unmapped_page;
  1146. unmapped += unmapped_page;
  1147. }
  1148. trace_unmap(orig_iova, size, unmapped);
  1149. return unmapped;
  1150. }
  1151. EXPORT_SYMBOL_GPL(iommu_unmap);
  1152. size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
  1153. struct scatterlist *sg, unsigned int nents, int prot)
  1154. {
  1155. struct scatterlist *s;
  1156. size_t mapped = 0;
  1157. unsigned int i, min_pagesz;
  1158. int ret;
  1159. if (unlikely(domain->pgsize_bitmap == 0UL))
  1160. return 0;
  1161. min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
  1162. for_each_sg(sg, s, nents, i) {
  1163. phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset;
  1164. /*
  1165. * We are mapping on IOMMU page boundaries, so offset within
  1166. * the page must be 0. However, the IOMMU may support pages
  1167. * smaller than PAGE_SIZE, so s->offset may still represent
  1168. * an offset of that boundary within the CPU page.
  1169. */
  1170. if (!IS_ALIGNED(s->offset, min_pagesz))
  1171. goto out_err;
  1172. ret = iommu_map(domain, iova + mapped, phys, s->length, prot);
  1173. if (ret)
  1174. goto out_err;
  1175. mapped += s->length;
  1176. }
  1177. return mapped;
  1178. out_err:
  1179. /* undo mappings already done */
  1180. iommu_unmap(domain, iova, mapped);
  1181. return 0;
  1182. }
  1183. EXPORT_SYMBOL_GPL(default_iommu_map_sg);
  1184. int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
  1185. phys_addr_t paddr, u64 size, int prot)
  1186. {
  1187. if (unlikely(domain->ops->domain_window_enable == NULL))
  1188. return -ENODEV;
  1189. return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
  1190. prot);
  1191. }
  1192. EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
  1193. void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
  1194. {
  1195. if (unlikely(domain->ops->domain_window_disable == NULL))
  1196. return;
  1197. return domain->ops->domain_window_disable(domain, wnd_nr);
  1198. }
  1199. EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
  1200. static int __init iommu_init(void)
  1201. {
  1202. iommu_group_kset = kset_create_and_add("iommu_groups",
  1203. NULL, kernel_kobj);
  1204. BUG_ON(!iommu_group_kset);
  1205. return 0;
  1206. }
  1207. core_initcall(iommu_init);
  1208. int iommu_domain_get_attr(struct iommu_domain *domain,
  1209. enum iommu_attr attr, void *data)
  1210. {
  1211. struct iommu_domain_geometry *geometry;
  1212. bool *paging;
  1213. int ret = 0;
  1214. u32 *count;
  1215. switch (attr) {
  1216. case DOMAIN_ATTR_GEOMETRY:
  1217. geometry = data;
  1218. *geometry = domain->geometry;
  1219. break;
  1220. case DOMAIN_ATTR_PAGING:
  1221. paging = data;
  1222. *paging = (domain->pgsize_bitmap != 0UL);
  1223. break;
  1224. case DOMAIN_ATTR_WINDOWS:
  1225. count = data;
  1226. if (domain->ops->domain_get_windows != NULL)
  1227. *count = domain->ops->domain_get_windows(domain);
  1228. else
  1229. ret = -ENODEV;
  1230. break;
  1231. default:
  1232. if (!domain->ops->domain_get_attr)
  1233. return -EINVAL;
  1234. ret = domain->ops->domain_get_attr(domain, attr, data);
  1235. }
  1236. return ret;
  1237. }
  1238. EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
  1239. int iommu_domain_set_attr(struct iommu_domain *domain,
  1240. enum iommu_attr attr, void *data)
  1241. {
  1242. int ret = 0;
  1243. u32 *count;
  1244. switch (attr) {
  1245. case DOMAIN_ATTR_WINDOWS:
  1246. count = data;
  1247. if (domain->ops->domain_set_windows != NULL)
  1248. ret = domain->ops->domain_set_windows(domain, *count);
  1249. else
  1250. ret = -ENODEV;
  1251. break;
  1252. default:
  1253. if (domain->ops->domain_set_attr == NULL)
  1254. return -EINVAL;
  1255. ret = domain->ops->domain_set_attr(domain, attr, data);
  1256. }
  1257. return ret;
  1258. }
  1259. EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
  1260. void iommu_get_dm_regions(struct device *dev, struct list_head *list)
  1261. {
  1262. const struct iommu_ops *ops = dev->bus->iommu_ops;
  1263. if (ops && ops->get_dm_regions)
  1264. ops->get_dm_regions(dev, list);
  1265. }
  1266. void iommu_put_dm_regions(struct device *dev, struct list_head *list)
  1267. {
  1268. const struct iommu_ops *ops = dev->bus->iommu_ops;
  1269. if (ops && ops->put_dm_regions)
  1270. ops->put_dm_regions(dev, list);
  1271. }
  1272. /* Request that a device is direct mapped by the IOMMU */
  1273. int iommu_request_dm_for_dev(struct device *dev)
  1274. {
  1275. struct iommu_domain *dm_domain;
  1276. struct iommu_group *group;
  1277. int ret;
  1278. /* Device must already be in a group before calling this function */
  1279. group = iommu_group_get_for_dev(dev);
  1280. if (IS_ERR(group))
  1281. return PTR_ERR(group);
  1282. mutex_lock(&group->mutex);
  1283. /* Check if the default domain is already direct mapped */
  1284. ret = 0;
  1285. if (group->default_domain &&
  1286. group->default_domain->type == IOMMU_DOMAIN_IDENTITY)
  1287. goto out;
  1288. /* Don't change mappings of existing devices */
  1289. ret = -EBUSY;
  1290. if (iommu_group_device_count(group) != 1)
  1291. goto out;
  1292. /* Allocate a direct mapped domain */
  1293. ret = -ENOMEM;
  1294. dm_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_IDENTITY);
  1295. if (!dm_domain)
  1296. goto out;
  1297. /* Attach the device to the domain */
  1298. ret = __iommu_attach_group(dm_domain, group);
  1299. if (ret) {
  1300. iommu_domain_free(dm_domain);
  1301. goto out;
  1302. }
  1303. /* Make the direct mapped domain the default for this group */
  1304. if (group->default_domain)
  1305. iommu_domain_free(group->default_domain);
  1306. group->default_domain = dm_domain;
  1307. pr_info("Using direct mapping for device %s\n", dev_name(dev));
  1308. ret = 0;
  1309. out:
  1310. mutex_unlock(&group->mutex);
  1311. iommu_group_put(group);
  1312. return ret;
  1313. }