iommu.c 49 KB

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