iommu.c 50 KB

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