iommu.c 49 KB

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