iommu.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019
  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\n";
  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. kobject_put(&group->kobj);
  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. sysfs_remove_link(group->devices_kobj, device->name);
  544. err_free_name:
  545. kfree(device->name);
  546. err_remove_link:
  547. sysfs_remove_link(&dev->kobj, "iommu_group");
  548. err_free_device:
  549. kfree(device);
  550. pr_err("Failed to add device %s to group %d: %d\n", dev_name(dev), group->id, ret);
  551. return ret;
  552. }
  553. EXPORT_SYMBOL_GPL(iommu_group_add_device);
  554. /**
  555. * iommu_group_remove_device - remove a device from it's current group
  556. * @dev: device to be removed
  557. *
  558. * This function is called by an iommu driver to remove the device from
  559. * it's current group. This decrements the iommu group reference count.
  560. */
  561. void iommu_group_remove_device(struct device *dev)
  562. {
  563. struct iommu_group *group = dev->iommu_group;
  564. struct group_device *tmp_device, *device = NULL;
  565. pr_info("Removing device %s from group %d\n", dev_name(dev), group->id);
  566. /* Pre-notify listeners that a device is being removed. */
  567. blocking_notifier_call_chain(&group->notifier,
  568. IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
  569. mutex_lock(&group->mutex);
  570. list_for_each_entry(tmp_device, &group->devices, list) {
  571. if (tmp_device->dev == dev) {
  572. device = tmp_device;
  573. list_del(&device->list);
  574. break;
  575. }
  576. }
  577. mutex_unlock(&group->mutex);
  578. if (!device)
  579. return;
  580. sysfs_remove_link(group->devices_kobj, device->name);
  581. sysfs_remove_link(&dev->kobj, "iommu_group");
  582. trace_remove_device_from_group(group->id, dev);
  583. kfree(device->name);
  584. kfree(device);
  585. dev->iommu_group = NULL;
  586. kobject_put(group->devices_kobj);
  587. }
  588. EXPORT_SYMBOL_GPL(iommu_group_remove_device);
  589. static int iommu_group_device_count(struct iommu_group *group)
  590. {
  591. struct group_device *entry;
  592. int ret = 0;
  593. list_for_each_entry(entry, &group->devices, list)
  594. ret++;
  595. return ret;
  596. }
  597. /**
  598. * iommu_group_for_each_dev - iterate over each device in the group
  599. * @group: the group
  600. * @data: caller opaque data to be passed to callback function
  601. * @fn: caller supplied callback function
  602. *
  603. * This function is called by group users to iterate over group devices.
  604. * Callers should hold a reference count to the group during callback.
  605. * The group->mutex is held across callbacks, which will block calls to
  606. * iommu_group_add/remove_device.
  607. */
  608. static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
  609. int (*fn)(struct device *, void *))
  610. {
  611. struct group_device *device;
  612. int ret = 0;
  613. list_for_each_entry(device, &group->devices, list) {
  614. ret = fn(device->dev, data);
  615. if (ret)
  616. break;
  617. }
  618. return ret;
  619. }
  620. int iommu_group_for_each_dev(struct iommu_group *group, void *data,
  621. int (*fn)(struct device *, void *))
  622. {
  623. int ret;
  624. mutex_lock(&group->mutex);
  625. ret = __iommu_group_for_each_dev(group, data, fn);
  626. mutex_unlock(&group->mutex);
  627. return ret;
  628. }
  629. EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
  630. /**
  631. * iommu_group_get - Return the group for a device and increment reference
  632. * @dev: get the group that this device belongs to
  633. *
  634. * This function is called by iommu drivers and users to get the group
  635. * for the specified device. If found, the group is returned and the group
  636. * reference in incremented, else NULL.
  637. */
  638. struct iommu_group *iommu_group_get(struct device *dev)
  639. {
  640. struct iommu_group *group = dev->iommu_group;
  641. if (group)
  642. kobject_get(group->devices_kobj);
  643. return group;
  644. }
  645. EXPORT_SYMBOL_GPL(iommu_group_get);
  646. /**
  647. * iommu_group_ref_get - Increment reference on a group
  648. * @group: the group to use, must not be NULL
  649. *
  650. * This function is called by iommu drivers to take additional references on an
  651. * existing group. Returns the given group for convenience.
  652. */
  653. struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
  654. {
  655. kobject_get(group->devices_kobj);
  656. return group;
  657. }
  658. /**
  659. * iommu_group_put - Decrement group reference
  660. * @group: the group to use
  661. *
  662. * This function is called by iommu drivers and users to release the
  663. * iommu group. Once the reference count is zero, the group is released.
  664. */
  665. void iommu_group_put(struct iommu_group *group)
  666. {
  667. if (group)
  668. kobject_put(group->devices_kobj);
  669. }
  670. EXPORT_SYMBOL_GPL(iommu_group_put);
  671. /**
  672. * iommu_group_register_notifier - Register a notifier for group changes
  673. * @group: the group to watch
  674. * @nb: notifier block to signal
  675. *
  676. * This function allows iommu group users to track changes in a group.
  677. * See include/linux/iommu.h for actions sent via this notifier. Caller
  678. * should hold a reference to the group throughout notifier registration.
  679. */
  680. int iommu_group_register_notifier(struct iommu_group *group,
  681. struct notifier_block *nb)
  682. {
  683. return blocking_notifier_chain_register(&group->notifier, nb);
  684. }
  685. EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
  686. /**
  687. * iommu_group_unregister_notifier - Unregister a notifier
  688. * @group: the group to watch
  689. * @nb: notifier block to signal
  690. *
  691. * Unregister a previously registered group notifier block.
  692. */
  693. int iommu_group_unregister_notifier(struct iommu_group *group,
  694. struct notifier_block *nb)
  695. {
  696. return blocking_notifier_chain_unregister(&group->notifier, nb);
  697. }
  698. EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
  699. /**
  700. * iommu_group_id - Return ID for a group
  701. * @group: the group to ID
  702. *
  703. * Return the unique ID for the group matching the sysfs group number.
  704. */
  705. int iommu_group_id(struct iommu_group *group)
  706. {
  707. return group->id;
  708. }
  709. EXPORT_SYMBOL_GPL(iommu_group_id);
  710. static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
  711. unsigned long *devfns);
  712. /*
  713. * To consider a PCI device isolated, we require ACS to support Source
  714. * Validation, Request Redirection, Completer Redirection, and Upstream
  715. * Forwarding. This effectively means that devices cannot spoof their
  716. * requester ID, requests and completions cannot be redirected, and all
  717. * transactions are forwarded upstream, even as it passes through a
  718. * bridge where the target device is downstream.
  719. */
  720. #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
  721. /*
  722. * For multifunction devices which are not isolated from each other, find
  723. * all the other non-isolated functions and look for existing groups. For
  724. * each function, we also need to look for aliases to or from other devices
  725. * that may already have a group.
  726. */
  727. static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
  728. unsigned long *devfns)
  729. {
  730. struct pci_dev *tmp = NULL;
  731. struct iommu_group *group;
  732. if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
  733. return NULL;
  734. for_each_pci_dev(tmp) {
  735. if (tmp == pdev || tmp->bus != pdev->bus ||
  736. PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
  737. pci_acs_enabled(tmp, REQ_ACS_FLAGS))
  738. continue;
  739. group = get_pci_alias_group(tmp, devfns);
  740. if (group) {
  741. pci_dev_put(tmp);
  742. return group;
  743. }
  744. }
  745. return NULL;
  746. }
  747. /*
  748. * Look for aliases to or from the given device for existing groups. DMA
  749. * aliases are only supported on the same bus, therefore the search
  750. * space is quite small (especially since we're really only looking at pcie
  751. * device, and therefore only expect multiple slots on the root complex or
  752. * downstream switch ports). It's conceivable though that a pair of
  753. * multifunction devices could have aliases between them that would cause a
  754. * loop. To prevent this, we use a bitmap to track where we've been.
  755. */
  756. static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
  757. unsigned long *devfns)
  758. {
  759. struct pci_dev *tmp = NULL;
  760. struct iommu_group *group;
  761. if (test_and_set_bit(pdev->devfn & 0xff, devfns))
  762. return NULL;
  763. group = iommu_group_get(&pdev->dev);
  764. if (group)
  765. return group;
  766. for_each_pci_dev(tmp) {
  767. if (tmp == pdev || tmp->bus != pdev->bus)
  768. continue;
  769. /* We alias them or they alias us */
  770. if (pci_devs_are_dma_aliases(pdev, tmp)) {
  771. group = get_pci_alias_group(tmp, devfns);
  772. if (group) {
  773. pci_dev_put(tmp);
  774. return group;
  775. }
  776. group = get_pci_function_alias_group(tmp, devfns);
  777. if (group) {
  778. pci_dev_put(tmp);
  779. return group;
  780. }
  781. }
  782. }
  783. return NULL;
  784. }
  785. struct group_for_pci_data {
  786. struct pci_dev *pdev;
  787. struct iommu_group *group;
  788. };
  789. /*
  790. * DMA alias iterator callback, return the last seen device. Stop and return
  791. * the IOMMU group if we find one along the way.
  792. */
  793. static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
  794. {
  795. struct group_for_pci_data *data = opaque;
  796. data->pdev = pdev;
  797. data->group = iommu_group_get(&pdev->dev);
  798. return data->group != NULL;
  799. }
  800. /*
  801. * Generic device_group call-back function. It just allocates one
  802. * iommu-group per device.
  803. */
  804. struct iommu_group *generic_device_group(struct device *dev)
  805. {
  806. return iommu_group_alloc();
  807. }
  808. /*
  809. * Use standard PCI bus topology, isolation features, and DMA alias quirks
  810. * to find or create an IOMMU group for a device.
  811. */
  812. struct iommu_group *pci_device_group(struct device *dev)
  813. {
  814. struct pci_dev *pdev = to_pci_dev(dev);
  815. struct group_for_pci_data data;
  816. struct pci_bus *bus;
  817. struct iommu_group *group = NULL;
  818. u64 devfns[4] = { 0 };
  819. if (WARN_ON(!dev_is_pci(dev)))
  820. return ERR_PTR(-EINVAL);
  821. /*
  822. * Find the upstream DMA alias for the device. A device must not
  823. * be aliased due to topology in order to have its own IOMMU group.
  824. * If we find an alias along the way that already belongs to a
  825. * group, use it.
  826. */
  827. if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
  828. return data.group;
  829. pdev = data.pdev;
  830. /*
  831. * Continue upstream from the point of minimum IOMMU granularity
  832. * due to aliases to the point where devices are protected from
  833. * peer-to-peer DMA by PCI ACS. Again, if we find an existing
  834. * group, use it.
  835. */
  836. for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
  837. if (!bus->self)
  838. continue;
  839. if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
  840. break;
  841. pdev = bus->self;
  842. group = iommu_group_get(&pdev->dev);
  843. if (group)
  844. return group;
  845. }
  846. /*
  847. * Look for existing groups on device aliases. If we alias another
  848. * device or another device aliases us, use the same group.
  849. */
  850. group = get_pci_alias_group(pdev, (unsigned long *)devfns);
  851. if (group)
  852. return group;
  853. /*
  854. * Look for existing groups on non-isolated functions on the same
  855. * slot and aliases of those funcions, if any. No need to clear
  856. * the search bitmap, the tested devfns are still valid.
  857. */
  858. group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
  859. if (group)
  860. return group;
  861. /* No shared group found, allocate new */
  862. return iommu_group_alloc();
  863. }
  864. /**
  865. * iommu_group_get_for_dev - Find or create the IOMMU group for a device
  866. * @dev: target device
  867. *
  868. * This function is intended to be called by IOMMU drivers and extended to
  869. * support common, bus-defined algorithms when determining or creating the
  870. * IOMMU group for a device. On success, the caller will hold a reference
  871. * to the returned IOMMU group, which will already include the provided
  872. * device. The reference should be released with iommu_group_put().
  873. */
  874. struct iommu_group *iommu_group_get_for_dev(struct device *dev)
  875. {
  876. const struct iommu_ops *ops = dev->bus->iommu_ops;
  877. struct iommu_group *group;
  878. int ret;
  879. group = iommu_group_get(dev);
  880. if (group)
  881. return group;
  882. if (!ops)
  883. return ERR_PTR(-EINVAL);
  884. group = ops->device_group(dev);
  885. if (WARN_ON_ONCE(group == NULL))
  886. return ERR_PTR(-EINVAL);
  887. if (IS_ERR(group))
  888. return group;
  889. /*
  890. * Try to allocate a default domain - needs support from the
  891. * IOMMU driver.
  892. */
  893. if (!group->default_domain) {
  894. struct iommu_domain *dom;
  895. dom = __iommu_domain_alloc(dev->bus, iommu_def_domain_type);
  896. if (!dom && iommu_def_domain_type != IOMMU_DOMAIN_DMA) {
  897. dev_warn(dev,
  898. "failed to allocate default IOMMU domain of type %u; falling back to IOMMU_DOMAIN_DMA",
  899. iommu_def_domain_type);
  900. dom = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_DMA);
  901. }
  902. group->default_domain = dom;
  903. if (!group->domain)
  904. group->domain = dom;
  905. }
  906. ret = iommu_group_add_device(group, dev);
  907. if (ret) {
  908. iommu_group_put(group);
  909. return ERR_PTR(ret);
  910. }
  911. return group;
  912. }
  913. struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
  914. {
  915. return group->default_domain;
  916. }
  917. static int add_iommu_group(struct device *dev, void *data)
  918. {
  919. struct iommu_callback_data *cb = data;
  920. const struct iommu_ops *ops = cb->ops;
  921. int ret;
  922. if (!ops->add_device)
  923. return 0;
  924. WARN_ON(dev->iommu_group);
  925. ret = ops->add_device(dev);
  926. /*
  927. * We ignore -ENODEV errors for now, as they just mean that the
  928. * device is not translated by an IOMMU. We still care about
  929. * other errors and fail to initialize when they happen.
  930. */
  931. if (ret == -ENODEV)
  932. ret = 0;
  933. return ret;
  934. }
  935. static int remove_iommu_group(struct device *dev, void *data)
  936. {
  937. struct iommu_callback_data *cb = data;
  938. const struct iommu_ops *ops = cb->ops;
  939. if (ops->remove_device && dev->iommu_group)
  940. ops->remove_device(dev);
  941. return 0;
  942. }
  943. static int iommu_bus_notifier(struct notifier_block *nb,
  944. unsigned long action, void *data)
  945. {
  946. struct device *dev = data;
  947. const struct iommu_ops *ops = dev->bus->iommu_ops;
  948. struct iommu_group *group;
  949. unsigned long group_action = 0;
  950. /*
  951. * ADD/DEL call into iommu driver ops if provided, which may
  952. * result in ADD/DEL notifiers to group->notifier
  953. */
  954. if (action == BUS_NOTIFY_ADD_DEVICE) {
  955. if (ops->add_device) {
  956. int ret;
  957. ret = ops->add_device(dev);
  958. return (ret) ? NOTIFY_DONE : NOTIFY_OK;
  959. }
  960. } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
  961. if (ops->remove_device && dev->iommu_group) {
  962. ops->remove_device(dev);
  963. return 0;
  964. }
  965. }
  966. /*
  967. * Remaining BUS_NOTIFYs get filtered and republished to the
  968. * group, if anyone is listening
  969. */
  970. group = iommu_group_get(dev);
  971. if (!group)
  972. return 0;
  973. switch (action) {
  974. case BUS_NOTIFY_BIND_DRIVER:
  975. group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
  976. break;
  977. case BUS_NOTIFY_BOUND_DRIVER:
  978. group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
  979. break;
  980. case BUS_NOTIFY_UNBIND_DRIVER:
  981. group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
  982. break;
  983. case BUS_NOTIFY_UNBOUND_DRIVER:
  984. group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
  985. break;
  986. }
  987. if (group_action)
  988. blocking_notifier_call_chain(&group->notifier,
  989. group_action, dev);
  990. iommu_group_put(group);
  991. return 0;
  992. }
  993. static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
  994. {
  995. int err;
  996. struct notifier_block *nb;
  997. struct iommu_callback_data cb = {
  998. .ops = ops,
  999. };
  1000. nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
  1001. if (!nb)
  1002. return -ENOMEM;
  1003. nb->notifier_call = iommu_bus_notifier;
  1004. err = bus_register_notifier(bus, nb);
  1005. if (err)
  1006. goto out_free;
  1007. err = bus_for_each_dev(bus, NULL, &cb, add_iommu_group);
  1008. if (err)
  1009. goto out_err;
  1010. return 0;
  1011. out_err:
  1012. /* Clean up */
  1013. bus_for_each_dev(bus, NULL, &cb, remove_iommu_group);
  1014. bus_unregister_notifier(bus, nb);
  1015. out_free:
  1016. kfree(nb);
  1017. return err;
  1018. }
  1019. /**
  1020. * bus_set_iommu - set iommu-callbacks for the bus
  1021. * @bus: bus.
  1022. * @ops: the callbacks provided by the iommu-driver
  1023. *
  1024. * This function is called by an iommu driver to set the iommu methods
  1025. * used for a particular bus. Drivers for devices on that bus can use
  1026. * the iommu-api after these ops are registered.
  1027. * This special function is needed because IOMMUs are usually devices on
  1028. * the bus itself, so the iommu drivers are not initialized when the bus
  1029. * is set up. With this function the iommu-driver can set the iommu-ops
  1030. * afterwards.
  1031. */
  1032. int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
  1033. {
  1034. int err;
  1035. if (bus->iommu_ops != NULL)
  1036. return -EBUSY;
  1037. bus->iommu_ops = ops;
  1038. /* Do IOMMU specific setup for this bus-type */
  1039. err = iommu_bus_init(bus, ops);
  1040. if (err)
  1041. bus->iommu_ops = NULL;
  1042. return err;
  1043. }
  1044. EXPORT_SYMBOL_GPL(bus_set_iommu);
  1045. bool iommu_present(struct bus_type *bus)
  1046. {
  1047. return bus->iommu_ops != NULL;
  1048. }
  1049. EXPORT_SYMBOL_GPL(iommu_present);
  1050. bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
  1051. {
  1052. if (!bus->iommu_ops || !bus->iommu_ops->capable)
  1053. return false;
  1054. return bus->iommu_ops->capable(cap);
  1055. }
  1056. EXPORT_SYMBOL_GPL(iommu_capable);
  1057. /**
  1058. * iommu_set_fault_handler() - set a fault handler for an iommu domain
  1059. * @domain: iommu domain
  1060. * @handler: fault handler
  1061. * @token: user data, will be passed back to the fault handler
  1062. *
  1063. * This function should be used by IOMMU users which want to be notified
  1064. * whenever an IOMMU fault happens.
  1065. *
  1066. * The fault handler itself should return 0 on success, and an appropriate
  1067. * error code otherwise.
  1068. */
  1069. void iommu_set_fault_handler(struct iommu_domain *domain,
  1070. iommu_fault_handler_t handler,
  1071. void *token)
  1072. {
  1073. BUG_ON(!domain);
  1074. domain->handler = handler;
  1075. domain->handler_token = token;
  1076. }
  1077. EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
  1078. static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
  1079. unsigned type)
  1080. {
  1081. struct iommu_domain *domain;
  1082. if (bus == NULL || bus->iommu_ops == NULL)
  1083. return NULL;
  1084. domain = bus->iommu_ops->domain_alloc(type);
  1085. if (!domain)
  1086. return NULL;
  1087. domain->ops = bus->iommu_ops;
  1088. domain->type = type;
  1089. /* Assume all sizes by default; the driver may override this later */
  1090. domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
  1091. return domain;
  1092. }
  1093. struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
  1094. {
  1095. return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
  1096. }
  1097. EXPORT_SYMBOL_GPL(iommu_domain_alloc);
  1098. void iommu_domain_free(struct iommu_domain *domain)
  1099. {
  1100. domain->ops->domain_free(domain);
  1101. }
  1102. EXPORT_SYMBOL_GPL(iommu_domain_free);
  1103. static int __iommu_attach_device(struct iommu_domain *domain,
  1104. struct device *dev)
  1105. {
  1106. int ret;
  1107. if ((domain->ops->is_attach_deferred != NULL) &&
  1108. domain->ops->is_attach_deferred(domain, dev))
  1109. return 0;
  1110. if (unlikely(domain->ops->attach_dev == NULL))
  1111. return -ENODEV;
  1112. ret = domain->ops->attach_dev(domain, dev);
  1113. if (!ret)
  1114. trace_attach_device_to_domain(dev);
  1115. return ret;
  1116. }
  1117. int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
  1118. {
  1119. struct iommu_group *group;
  1120. int ret;
  1121. group = iommu_group_get(dev);
  1122. if (!group)
  1123. return -ENODEV;
  1124. /*
  1125. * Lock the group to make sure the device-count doesn't
  1126. * change while we are attaching
  1127. */
  1128. mutex_lock(&group->mutex);
  1129. ret = -EINVAL;
  1130. if (iommu_group_device_count(group) != 1)
  1131. goto out_unlock;
  1132. ret = __iommu_attach_group(domain, group);
  1133. out_unlock:
  1134. mutex_unlock(&group->mutex);
  1135. iommu_group_put(group);
  1136. return ret;
  1137. }
  1138. EXPORT_SYMBOL_GPL(iommu_attach_device);
  1139. static void __iommu_detach_device(struct iommu_domain *domain,
  1140. struct device *dev)
  1141. {
  1142. if ((domain->ops->is_attach_deferred != NULL) &&
  1143. domain->ops->is_attach_deferred(domain, dev))
  1144. return;
  1145. if (unlikely(domain->ops->detach_dev == NULL))
  1146. return;
  1147. domain->ops->detach_dev(domain, dev);
  1148. trace_detach_device_from_domain(dev);
  1149. }
  1150. void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
  1151. {
  1152. struct iommu_group *group;
  1153. group = iommu_group_get(dev);
  1154. if (!group)
  1155. return;
  1156. mutex_lock(&group->mutex);
  1157. if (iommu_group_device_count(group) != 1) {
  1158. WARN_ON(1);
  1159. goto out_unlock;
  1160. }
  1161. __iommu_detach_group(domain, group);
  1162. out_unlock:
  1163. mutex_unlock(&group->mutex);
  1164. iommu_group_put(group);
  1165. }
  1166. EXPORT_SYMBOL_GPL(iommu_detach_device);
  1167. struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
  1168. {
  1169. struct iommu_domain *domain;
  1170. struct iommu_group *group;
  1171. group = iommu_group_get(dev);
  1172. if (!group)
  1173. return NULL;
  1174. domain = group->domain;
  1175. iommu_group_put(group);
  1176. return domain;
  1177. }
  1178. EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
  1179. /*
  1180. * IOMMU groups are really the natrual working unit of the IOMMU, but
  1181. * the IOMMU API works on domains and devices. Bridge that gap by
  1182. * iterating over the devices in a group. Ideally we'd have a single
  1183. * device which represents the requestor ID of the group, but we also
  1184. * allow IOMMU drivers to create policy defined minimum sets, where
  1185. * the physical hardware may be able to distiguish members, but we
  1186. * wish to group them at a higher level (ex. untrusted multi-function
  1187. * PCI devices). Thus we attach each device.
  1188. */
  1189. static int iommu_group_do_attach_device(struct device *dev, void *data)
  1190. {
  1191. struct iommu_domain *domain = data;
  1192. return __iommu_attach_device(domain, dev);
  1193. }
  1194. static int __iommu_attach_group(struct iommu_domain *domain,
  1195. struct iommu_group *group)
  1196. {
  1197. int ret;
  1198. if (group->default_domain && group->domain != group->default_domain)
  1199. return -EBUSY;
  1200. ret = __iommu_group_for_each_dev(group, domain,
  1201. iommu_group_do_attach_device);
  1202. if (ret == 0)
  1203. group->domain = domain;
  1204. return ret;
  1205. }
  1206. int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
  1207. {
  1208. int ret;
  1209. mutex_lock(&group->mutex);
  1210. ret = __iommu_attach_group(domain, group);
  1211. mutex_unlock(&group->mutex);
  1212. return ret;
  1213. }
  1214. EXPORT_SYMBOL_GPL(iommu_attach_group);
  1215. static int iommu_group_do_detach_device(struct device *dev, void *data)
  1216. {
  1217. struct iommu_domain *domain = data;
  1218. __iommu_detach_device(domain, dev);
  1219. return 0;
  1220. }
  1221. static void __iommu_detach_group(struct iommu_domain *domain,
  1222. struct iommu_group *group)
  1223. {
  1224. int ret;
  1225. if (!group->default_domain) {
  1226. __iommu_group_for_each_dev(group, domain,
  1227. iommu_group_do_detach_device);
  1228. group->domain = NULL;
  1229. return;
  1230. }
  1231. if (group->domain == group->default_domain)
  1232. return;
  1233. /* Detach by re-attaching to the default domain */
  1234. ret = __iommu_group_for_each_dev(group, group->default_domain,
  1235. iommu_group_do_attach_device);
  1236. if (ret != 0)
  1237. WARN_ON(1);
  1238. else
  1239. group->domain = group->default_domain;
  1240. }
  1241. void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
  1242. {
  1243. mutex_lock(&group->mutex);
  1244. __iommu_detach_group(domain, group);
  1245. mutex_unlock(&group->mutex);
  1246. }
  1247. EXPORT_SYMBOL_GPL(iommu_detach_group);
  1248. phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
  1249. {
  1250. if (unlikely(domain->ops->iova_to_phys == NULL))
  1251. return 0;
  1252. return domain->ops->iova_to_phys(domain, iova);
  1253. }
  1254. EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
  1255. static size_t iommu_pgsize(struct iommu_domain *domain,
  1256. unsigned long addr_merge, size_t size)
  1257. {
  1258. unsigned int pgsize_idx;
  1259. size_t pgsize;
  1260. /* Max page size that still fits into 'size' */
  1261. pgsize_idx = __fls(size);
  1262. /* need to consider alignment requirements ? */
  1263. if (likely(addr_merge)) {
  1264. /* Max page size allowed by address */
  1265. unsigned int align_pgsize_idx = __ffs(addr_merge);
  1266. pgsize_idx = min(pgsize_idx, align_pgsize_idx);
  1267. }
  1268. /* build a mask of acceptable page sizes */
  1269. pgsize = (1UL << (pgsize_idx + 1)) - 1;
  1270. /* throw away page sizes not supported by the hardware */
  1271. pgsize &= domain->pgsize_bitmap;
  1272. /* make sure we're still sane */
  1273. BUG_ON(!pgsize);
  1274. /* pick the biggest page */
  1275. pgsize_idx = __fls(pgsize);
  1276. pgsize = 1UL << pgsize_idx;
  1277. return pgsize;
  1278. }
  1279. int iommu_map(struct iommu_domain *domain, unsigned long iova,
  1280. phys_addr_t paddr, size_t size, int prot)
  1281. {
  1282. unsigned long orig_iova = iova;
  1283. unsigned int min_pagesz;
  1284. size_t orig_size = size;
  1285. phys_addr_t orig_paddr = paddr;
  1286. int ret = 0;
  1287. if (unlikely(domain->ops->map == NULL ||
  1288. domain->pgsize_bitmap == 0UL))
  1289. return -ENODEV;
  1290. if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
  1291. return -EINVAL;
  1292. /* find out the minimum page size supported */
  1293. min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
  1294. /*
  1295. * both the virtual address and the physical one, as well as
  1296. * the size of the mapping, must be aligned (at least) to the
  1297. * size of the smallest page supported by the hardware
  1298. */
  1299. if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
  1300. pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
  1301. iova, &paddr, size, min_pagesz);
  1302. return -EINVAL;
  1303. }
  1304. pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
  1305. while (size) {
  1306. size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
  1307. pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
  1308. iova, &paddr, pgsize);
  1309. ret = domain->ops->map(domain, iova, paddr, pgsize, prot);
  1310. if (ret)
  1311. break;
  1312. iova += pgsize;
  1313. paddr += pgsize;
  1314. size -= pgsize;
  1315. }
  1316. /* unroll mapping in case something went wrong */
  1317. if (ret)
  1318. iommu_unmap(domain, orig_iova, orig_size - size);
  1319. else
  1320. trace_map(orig_iova, orig_paddr, orig_size);
  1321. return ret;
  1322. }
  1323. EXPORT_SYMBOL_GPL(iommu_map);
  1324. static size_t __iommu_unmap(struct iommu_domain *domain,
  1325. unsigned long iova, size_t size,
  1326. bool sync)
  1327. {
  1328. const struct iommu_ops *ops = domain->ops;
  1329. size_t unmapped_page, unmapped = 0;
  1330. unsigned long orig_iova = iova;
  1331. unsigned int min_pagesz;
  1332. if (unlikely(ops->unmap == NULL ||
  1333. domain->pgsize_bitmap == 0UL))
  1334. return 0;
  1335. if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
  1336. return 0;
  1337. /* find out the minimum page size supported */
  1338. min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
  1339. /*
  1340. * The virtual address, as well as the size of the mapping, must be
  1341. * aligned (at least) to the size of the smallest page supported
  1342. * by the hardware
  1343. */
  1344. if (!IS_ALIGNED(iova | size, min_pagesz)) {
  1345. pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
  1346. iova, size, min_pagesz);
  1347. return 0;
  1348. }
  1349. pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
  1350. /*
  1351. * Keep iterating until we either unmap 'size' bytes (or more)
  1352. * or we hit an area that isn't mapped.
  1353. */
  1354. while (unmapped < size) {
  1355. size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
  1356. unmapped_page = ops->unmap(domain, iova, pgsize);
  1357. if (!unmapped_page)
  1358. break;
  1359. if (sync && ops->iotlb_range_add)
  1360. ops->iotlb_range_add(domain, iova, pgsize);
  1361. pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
  1362. iova, unmapped_page);
  1363. iova += unmapped_page;
  1364. unmapped += unmapped_page;
  1365. }
  1366. if (sync && ops->iotlb_sync)
  1367. ops->iotlb_sync(domain);
  1368. trace_unmap(orig_iova, size, unmapped);
  1369. return unmapped;
  1370. }
  1371. size_t iommu_unmap(struct iommu_domain *domain,
  1372. unsigned long iova, size_t size)
  1373. {
  1374. return __iommu_unmap(domain, iova, size, true);
  1375. }
  1376. EXPORT_SYMBOL_GPL(iommu_unmap);
  1377. size_t iommu_unmap_fast(struct iommu_domain *domain,
  1378. unsigned long iova, size_t size)
  1379. {
  1380. return __iommu_unmap(domain, iova, size, false);
  1381. }
  1382. EXPORT_SYMBOL_GPL(iommu_unmap_fast);
  1383. size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
  1384. struct scatterlist *sg, unsigned int nents, int prot)
  1385. {
  1386. struct scatterlist *s;
  1387. size_t mapped = 0;
  1388. unsigned int i, min_pagesz;
  1389. int ret;
  1390. if (unlikely(domain->pgsize_bitmap == 0UL))
  1391. return 0;
  1392. min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
  1393. for_each_sg(sg, s, nents, i) {
  1394. phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset;
  1395. /*
  1396. * We are mapping on IOMMU page boundaries, so offset within
  1397. * the page must be 0. However, the IOMMU may support pages
  1398. * smaller than PAGE_SIZE, so s->offset may still represent
  1399. * an offset of that boundary within the CPU page.
  1400. */
  1401. if (!IS_ALIGNED(s->offset, min_pagesz))
  1402. goto out_err;
  1403. ret = iommu_map(domain, iova + mapped, phys, s->length, prot);
  1404. if (ret)
  1405. goto out_err;
  1406. mapped += s->length;
  1407. }
  1408. return mapped;
  1409. out_err:
  1410. /* undo mappings already done */
  1411. iommu_unmap(domain, iova, mapped);
  1412. return 0;
  1413. }
  1414. EXPORT_SYMBOL_GPL(iommu_map_sg);
  1415. int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
  1416. phys_addr_t paddr, u64 size, int prot)
  1417. {
  1418. if (unlikely(domain->ops->domain_window_enable == NULL))
  1419. return -ENODEV;
  1420. return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
  1421. prot);
  1422. }
  1423. EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
  1424. void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
  1425. {
  1426. if (unlikely(domain->ops->domain_window_disable == NULL))
  1427. return;
  1428. return domain->ops->domain_window_disable(domain, wnd_nr);
  1429. }
  1430. EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
  1431. /**
  1432. * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
  1433. * @domain: the iommu domain where the fault has happened
  1434. * @dev: the device where the fault has happened
  1435. * @iova: the faulting address
  1436. * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
  1437. *
  1438. * This function should be called by the low-level IOMMU implementations
  1439. * whenever IOMMU faults happen, to allow high-level users, that are
  1440. * interested in such events, to know about them.
  1441. *
  1442. * This event may be useful for several possible use cases:
  1443. * - mere logging of the event
  1444. * - dynamic TLB/PTE loading
  1445. * - if restarting of the faulting device is required
  1446. *
  1447. * Returns 0 on success and an appropriate error code otherwise (if dynamic
  1448. * PTE/TLB loading will one day be supported, implementations will be able
  1449. * to tell whether it succeeded or not according to this return value).
  1450. *
  1451. * Specifically, -ENOSYS is returned if a fault handler isn't installed
  1452. * (though fault handlers can also return -ENOSYS, in case they want to
  1453. * elicit the default behavior of the IOMMU drivers).
  1454. */
  1455. int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
  1456. unsigned long iova, int flags)
  1457. {
  1458. int ret = -ENOSYS;
  1459. /*
  1460. * if upper layers showed interest and installed a fault handler,
  1461. * invoke it.
  1462. */
  1463. if (domain->handler)
  1464. ret = domain->handler(domain, dev, iova, flags,
  1465. domain->handler_token);
  1466. trace_io_page_fault(dev, iova, flags);
  1467. return ret;
  1468. }
  1469. EXPORT_SYMBOL_GPL(report_iommu_fault);
  1470. static int __init iommu_init(void)
  1471. {
  1472. iommu_group_kset = kset_create_and_add("iommu_groups",
  1473. NULL, kernel_kobj);
  1474. BUG_ON(!iommu_group_kset);
  1475. iommu_debugfs_setup();
  1476. return 0;
  1477. }
  1478. core_initcall(iommu_init);
  1479. int iommu_domain_get_attr(struct iommu_domain *domain,
  1480. enum iommu_attr attr, void *data)
  1481. {
  1482. struct iommu_domain_geometry *geometry;
  1483. bool *paging;
  1484. int ret = 0;
  1485. u32 *count;
  1486. switch (attr) {
  1487. case DOMAIN_ATTR_GEOMETRY:
  1488. geometry = data;
  1489. *geometry = domain->geometry;
  1490. break;
  1491. case DOMAIN_ATTR_PAGING:
  1492. paging = data;
  1493. *paging = (domain->pgsize_bitmap != 0UL);
  1494. break;
  1495. case DOMAIN_ATTR_WINDOWS:
  1496. count = data;
  1497. if (domain->ops->domain_get_windows != NULL)
  1498. *count = domain->ops->domain_get_windows(domain);
  1499. else
  1500. ret = -ENODEV;
  1501. break;
  1502. default:
  1503. if (!domain->ops->domain_get_attr)
  1504. return -EINVAL;
  1505. ret = domain->ops->domain_get_attr(domain, attr, data);
  1506. }
  1507. return ret;
  1508. }
  1509. EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
  1510. int iommu_domain_set_attr(struct iommu_domain *domain,
  1511. enum iommu_attr attr, void *data)
  1512. {
  1513. int ret = 0;
  1514. u32 *count;
  1515. switch (attr) {
  1516. case DOMAIN_ATTR_WINDOWS:
  1517. count = data;
  1518. if (domain->ops->domain_set_windows != NULL)
  1519. ret = domain->ops->domain_set_windows(domain, *count);
  1520. else
  1521. ret = -ENODEV;
  1522. break;
  1523. default:
  1524. if (domain->ops->domain_set_attr == NULL)
  1525. return -EINVAL;
  1526. ret = domain->ops->domain_set_attr(domain, attr, data);
  1527. }
  1528. return ret;
  1529. }
  1530. EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
  1531. void iommu_get_resv_regions(struct device *dev, struct list_head *list)
  1532. {
  1533. const struct iommu_ops *ops = dev->bus->iommu_ops;
  1534. if (ops && ops->get_resv_regions)
  1535. ops->get_resv_regions(dev, list);
  1536. }
  1537. void iommu_put_resv_regions(struct device *dev, struct list_head *list)
  1538. {
  1539. const struct iommu_ops *ops = dev->bus->iommu_ops;
  1540. if (ops && ops->put_resv_regions)
  1541. ops->put_resv_regions(dev, list);
  1542. }
  1543. struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
  1544. size_t length, int prot,
  1545. enum iommu_resv_type type)
  1546. {
  1547. struct iommu_resv_region *region;
  1548. region = kzalloc(sizeof(*region), GFP_KERNEL);
  1549. if (!region)
  1550. return NULL;
  1551. INIT_LIST_HEAD(&region->list);
  1552. region->start = start;
  1553. region->length = length;
  1554. region->prot = prot;
  1555. region->type = type;
  1556. return region;
  1557. }
  1558. /* Request that a device is direct mapped by the IOMMU */
  1559. int iommu_request_dm_for_dev(struct device *dev)
  1560. {
  1561. struct iommu_domain *dm_domain;
  1562. struct iommu_group *group;
  1563. int ret;
  1564. /* Device must already be in a group before calling this function */
  1565. group = iommu_group_get(dev);
  1566. if (!group)
  1567. return -EINVAL;
  1568. mutex_lock(&group->mutex);
  1569. /* Check if the default domain is already direct mapped */
  1570. ret = 0;
  1571. if (group->default_domain &&
  1572. group->default_domain->type == IOMMU_DOMAIN_IDENTITY)
  1573. goto out;
  1574. /* Don't change mappings of existing devices */
  1575. ret = -EBUSY;
  1576. if (iommu_group_device_count(group) != 1)
  1577. goto out;
  1578. /* Allocate a direct mapped domain */
  1579. ret = -ENOMEM;
  1580. dm_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_IDENTITY);
  1581. if (!dm_domain)
  1582. goto out;
  1583. /* Attach the device to the domain */
  1584. ret = __iommu_attach_group(dm_domain, group);
  1585. if (ret) {
  1586. iommu_domain_free(dm_domain);
  1587. goto out;
  1588. }
  1589. /* Make the direct mapped domain the default for this group */
  1590. if (group->default_domain)
  1591. iommu_domain_free(group->default_domain);
  1592. group->default_domain = dm_domain;
  1593. pr_info("Using direct mapping for device %s\n", dev_name(dev));
  1594. ret = 0;
  1595. out:
  1596. mutex_unlock(&group->mutex);
  1597. iommu_group_put(group);
  1598. return ret;
  1599. }
  1600. const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
  1601. {
  1602. const struct iommu_ops *ops = NULL;
  1603. struct iommu_device *iommu;
  1604. spin_lock(&iommu_device_lock);
  1605. list_for_each_entry(iommu, &iommu_device_list, list)
  1606. if (iommu->fwnode == fwnode) {
  1607. ops = iommu->ops;
  1608. break;
  1609. }
  1610. spin_unlock(&iommu_device_lock);
  1611. return ops;
  1612. }
  1613. int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
  1614. const struct iommu_ops *ops)
  1615. {
  1616. struct iommu_fwspec *fwspec = dev->iommu_fwspec;
  1617. if (fwspec)
  1618. return ops == fwspec->ops ? 0 : -EINVAL;
  1619. fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL);
  1620. if (!fwspec)
  1621. return -ENOMEM;
  1622. of_node_get(to_of_node(iommu_fwnode));
  1623. fwspec->iommu_fwnode = iommu_fwnode;
  1624. fwspec->ops = ops;
  1625. dev->iommu_fwspec = fwspec;
  1626. return 0;
  1627. }
  1628. EXPORT_SYMBOL_GPL(iommu_fwspec_init);
  1629. void iommu_fwspec_free(struct device *dev)
  1630. {
  1631. struct iommu_fwspec *fwspec = dev->iommu_fwspec;
  1632. if (fwspec) {
  1633. fwnode_handle_put(fwspec->iommu_fwnode);
  1634. kfree(fwspec);
  1635. dev->iommu_fwspec = NULL;
  1636. }
  1637. }
  1638. EXPORT_SYMBOL_GPL(iommu_fwspec_free);
  1639. int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
  1640. {
  1641. struct iommu_fwspec *fwspec = dev->iommu_fwspec;
  1642. size_t size;
  1643. int i;
  1644. if (!fwspec)
  1645. return -EINVAL;
  1646. size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]);
  1647. if (size > sizeof(*fwspec)) {
  1648. fwspec = krealloc(dev->iommu_fwspec, size, GFP_KERNEL);
  1649. if (!fwspec)
  1650. return -ENOMEM;
  1651. dev->iommu_fwspec = fwspec;
  1652. }
  1653. for (i = 0; i < num_ids; i++)
  1654. fwspec->ids[fwspec->num_ids + i] = ids[i];
  1655. fwspec->num_ids += num_ids;
  1656. return 0;
  1657. }
  1658. EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);