iommu.c 50 KB

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