iommu.c 46 KB

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