iommu.c 49 KB

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