iommu.c 40 KB

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