iommu.c 49 KB

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