iommu.c 40 KB

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