iommu.c 50 KB

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