iommu.c 45 KB

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