iommu.c 50 KB

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