iommu.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628
  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 <trace/events/iommu.h>
  33. static struct kset *iommu_group_kset;
  34. static struct ida iommu_group_ida;
  35. static struct mutex iommu_group_mutex;
  36. struct iommu_callback_data {
  37. const struct iommu_ops *ops;
  38. };
  39. struct iommu_group {
  40. struct kobject kobj;
  41. struct kobject *devices_kobj;
  42. struct list_head devices;
  43. struct mutex mutex;
  44. struct blocking_notifier_head notifier;
  45. void *iommu_data;
  46. void (*iommu_data_release)(void *iommu_data);
  47. char *name;
  48. int id;
  49. struct iommu_domain *default_domain;
  50. struct iommu_domain *domain;
  51. };
  52. struct iommu_device {
  53. struct list_head list;
  54. struct device *dev;
  55. char *name;
  56. };
  57. struct iommu_group_attribute {
  58. struct attribute attr;
  59. ssize_t (*show)(struct iommu_group *group, char *buf);
  60. ssize_t (*store)(struct iommu_group *group,
  61. const char *buf, size_t count);
  62. };
  63. #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
  64. struct iommu_group_attribute iommu_group_attr_##_name = \
  65. __ATTR(_name, _mode, _show, _store)
  66. #define to_iommu_group_attr(_attr) \
  67. container_of(_attr, struct iommu_group_attribute, attr)
  68. #define to_iommu_group(_kobj) \
  69. container_of(_kobj, struct iommu_group, kobj)
  70. static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
  71. unsigned type);
  72. static int __iommu_attach_device(struct iommu_domain *domain,
  73. struct device *dev);
  74. static int __iommu_attach_group(struct iommu_domain *domain,
  75. struct iommu_group *group);
  76. static void __iommu_detach_group(struct iommu_domain *domain,
  77. struct iommu_group *group);
  78. static ssize_t iommu_group_attr_show(struct kobject *kobj,
  79. struct attribute *__attr, char *buf)
  80. {
  81. struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
  82. struct iommu_group *group = to_iommu_group(kobj);
  83. ssize_t ret = -EIO;
  84. if (attr->show)
  85. ret = attr->show(group, buf);
  86. return ret;
  87. }
  88. static ssize_t iommu_group_attr_store(struct kobject *kobj,
  89. struct attribute *__attr,
  90. const char *buf, size_t count)
  91. {
  92. struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
  93. struct iommu_group *group = to_iommu_group(kobj);
  94. ssize_t ret = -EIO;
  95. if (attr->store)
  96. ret = attr->store(group, buf, count);
  97. return ret;
  98. }
  99. static const struct sysfs_ops iommu_group_sysfs_ops = {
  100. .show = iommu_group_attr_show,
  101. .store = iommu_group_attr_store,
  102. };
  103. static int iommu_group_create_file(struct iommu_group *group,
  104. struct iommu_group_attribute *attr)
  105. {
  106. return sysfs_create_file(&group->kobj, &attr->attr);
  107. }
  108. static void iommu_group_remove_file(struct iommu_group *group,
  109. struct iommu_group_attribute *attr)
  110. {
  111. sysfs_remove_file(&group->kobj, &attr->attr);
  112. }
  113. static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
  114. {
  115. return sprintf(buf, "%s\n", group->name);
  116. }
  117. static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
  118. static void iommu_group_release(struct kobject *kobj)
  119. {
  120. struct iommu_group *group = to_iommu_group(kobj);
  121. pr_debug("Releasing group %d\n", group->id);
  122. if (group->iommu_data_release)
  123. group->iommu_data_release(group->iommu_data);
  124. mutex_lock(&iommu_group_mutex);
  125. ida_remove(&iommu_group_ida, group->id);
  126. mutex_unlock(&iommu_group_mutex);
  127. if (group->default_domain)
  128. iommu_domain_free(group->default_domain);
  129. kfree(group->name);
  130. kfree(group);
  131. }
  132. static struct kobj_type iommu_group_ktype = {
  133. .sysfs_ops = &iommu_group_sysfs_ops,
  134. .release = iommu_group_release,
  135. };
  136. /**
  137. * iommu_group_alloc - Allocate a new group
  138. * @name: Optional name to associate with group, visible in sysfs
  139. *
  140. * This function is called by an iommu driver to allocate a new iommu
  141. * group. The iommu group represents the minimum granularity of the iommu.
  142. * Upon successful return, the caller holds a reference to the supplied
  143. * group in order to hold the group until devices are added. Use
  144. * iommu_group_put() to release this extra reference count, allowing the
  145. * group to be automatically reclaimed once it has no devices or external
  146. * references.
  147. */
  148. struct iommu_group *iommu_group_alloc(void)
  149. {
  150. struct iommu_group *group;
  151. int ret;
  152. group = kzalloc(sizeof(*group), GFP_KERNEL);
  153. if (!group)
  154. return ERR_PTR(-ENOMEM);
  155. group->kobj.kset = iommu_group_kset;
  156. mutex_init(&group->mutex);
  157. INIT_LIST_HEAD(&group->devices);
  158. BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
  159. mutex_lock(&iommu_group_mutex);
  160. again:
  161. if (unlikely(0 == ida_pre_get(&iommu_group_ida, GFP_KERNEL))) {
  162. kfree(group);
  163. mutex_unlock(&iommu_group_mutex);
  164. return ERR_PTR(-ENOMEM);
  165. }
  166. if (-EAGAIN == ida_get_new(&iommu_group_ida, &group->id))
  167. goto again;
  168. mutex_unlock(&iommu_group_mutex);
  169. ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
  170. NULL, "%d", group->id);
  171. if (ret) {
  172. mutex_lock(&iommu_group_mutex);
  173. ida_remove(&iommu_group_ida, group->id);
  174. mutex_unlock(&iommu_group_mutex);
  175. kfree(group);
  176. return ERR_PTR(ret);
  177. }
  178. group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
  179. if (!group->devices_kobj) {
  180. kobject_put(&group->kobj); /* triggers .release & free */
  181. return ERR_PTR(-ENOMEM);
  182. }
  183. /*
  184. * The devices_kobj holds a reference on the group kobject, so
  185. * as long as that exists so will the group. We can therefore
  186. * use the devices_kobj for reference counting.
  187. */
  188. kobject_put(&group->kobj);
  189. pr_debug("Allocated group %d\n", group->id);
  190. return group;
  191. }
  192. EXPORT_SYMBOL_GPL(iommu_group_alloc);
  193. struct iommu_group *iommu_group_get_by_id(int id)
  194. {
  195. struct kobject *group_kobj;
  196. struct iommu_group *group;
  197. const char *name;
  198. if (!iommu_group_kset)
  199. return NULL;
  200. name = kasprintf(GFP_KERNEL, "%d", id);
  201. if (!name)
  202. return NULL;
  203. group_kobj = kset_find_obj(iommu_group_kset, name);
  204. kfree(name);
  205. if (!group_kobj)
  206. return NULL;
  207. group = container_of(group_kobj, struct iommu_group, kobj);
  208. BUG_ON(group->id != id);
  209. kobject_get(group->devices_kobj);
  210. kobject_put(&group->kobj);
  211. return group;
  212. }
  213. EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
  214. /**
  215. * iommu_group_get_iommudata - retrieve iommu_data registered for a group
  216. * @group: the group
  217. *
  218. * iommu drivers can store data in the group for use when doing iommu
  219. * operations. This function provides a way to retrieve it. Caller
  220. * should hold a group reference.
  221. */
  222. void *iommu_group_get_iommudata(struct iommu_group *group)
  223. {
  224. return group->iommu_data;
  225. }
  226. EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
  227. /**
  228. * iommu_group_set_iommudata - set iommu_data for a group
  229. * @group: the group
  230. * @iommu_data: new data
  231. * @release: release function for iommu_data
  232. *
  233. * iommu drivers can store data in the group for use when doing iommu
  234. * operations. This function provides a way to set the data after
  235. * the group has been allocated. Caller should hold a group reference.
  236. */
  237. void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
  238. void (*release)(void *iommu_data))
  239. {
  240. group->iommu_data = iommu_data;
  241. group->iommu_data_release = release;
  242. }
  243. EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
  244. /**
  245. * iommu_group_set_name - set name for a group
  246. * @group: the group
  247. * @name: name
  248. *
  249. * Allow iommu driver to set a name for a group. When set it will
  250. * appear in a name attribute file under the group in sysfs.
  251. */
  252. int iommu_group_set_name(struct iommu_group *group, const char *name)
  253. {
  254. int ret;
  255. if (group->name) {
  256. iommu_group_remove_file(group, &iommu_group_attr_name);
  257. kfree(group->name);
  258. group->name = NULL;
  259. if (!name)
  260. return 0;
  261. }
  262. group->name = kstrdup(name, GFP_KERNEL);
  263. if (!group->name)
  264. return -ENOMEM;
  265. ret = iommu_group_create_file(group, &iommu_group_attr_name);
  266. if (ret) {
  267. kfree(group->name);
  268. group->name = NULL;
  269. return ret;
  270. }
  271. return 0;
  272. }
  273. EXPORT_SYMBOL_GPL(iommu_group_set_name);
  274. static int iommu_group_create_direct_mappings(struct iommu_group *group,
  275. struct device *dev)
  276. {
  277. struct iommu_domain *domain = group->default_domain;
  278. struct iommu_dm_region *entry;
  279. struct list_head mappings;
  280. unsigned long pg_size;
  281. int ret = 0;
  282. if (!domain || domain->type != IOMMU_DOMAIN_DMA)
  283. return 0;
  284. BUG_ON(!domain->ops->pgsize_bitmap);
  285. pg_size = 1UL << __ffs(domain->ops->pgsize_bitmap);
  286. INIT_LIST_HEAD(&mappings);
  287. iommu_get_dm_regions(dev, &mappings);
  288. /* We need to consider overlapping regions for different devices */
  289. list_for_each_entry(entry, &mappings, list) {
  290. dma_addr_t start, end, addr;
  291. start = ALIGN(entry->start, pg_size);
  292. end = ALIGN(entry->start + entry->length, pg_size);
  293. for (addr = start; addr < end; addr += pg_size) {
  294. phys_addr_t phys_addr;
  295. phys_addr = iommu_iova_to_phys(domain, addr);
  296. if (phys_addr)
  297. continue;
  298. ret = iommu_map(domain, addr, addr, pg_size, entry->prot);
  299. if (ret)
  300. goto out;
  301. }
  302. }
  303. out:
  304. iommu_put_dm_regions(dev, &mappings);
  305. return ret;
  306. }
  307. /**
  308. * iommu_group_add_device - add a device to an iommu group
  309. * @group: the group into which to add the device (reference should be held)
  310. * @dev: the device
  311. *
  312. * This function is called by an iommu driver to add a device into a
  313. * group. Adding a device increments the group reference count.
  314. */
  315. int iommu_group_add_device(struct iommu_group *group, struct device *dev)
  316. {
  317. int ret, i = 0;
  318. struct iommu_device *device;
  319. device = kzalloc(sizeof(*device), GFP_KERNEL);
  320. if (!device)
  321. return -ENOMEM;
  322. device->dev = dev;
  323. ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
  324. if (ret) {
  325. kfree(device);
  326. return ret;
  327. }
  328. device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
  329. rename:
  330. if (!device->name) {
  331. sysfs_remove_link(&dev->kobj, "iommu_group");
  332. kfree(device);
  333. return -ENOMEM;
  334. }
  335. ret = sysfs_create_link_nowarn(group->devices_kobj,
  336. &dev->kobj, device->name);
  337. if (ret) {
  338. kfree(device->name);
  339. if (ret == -EEXIST && i >= 0) {
  340. /*
  341. * Account for the slim chance of collision
  342. * and append an instance to the name.
  343. */
  344. device->name = kasprintf(GFP_KERNEL, "%s.%d",
  345. kobject_name(&dev->kobj), i++);
  346. goto rename;
  347. }
  348. sysfs_remove_link(&dev->kobj, "iommu_group");
  349. kfree(device);
  350. return ret;
  351. }
  352. kobject_get(group->devices_kobj);
  353. dev->iommu_group = group;
  354. iommu_group_create_direct_mappings(group, dev);
  355. mutex_lock(&group->mutex);
  356. list_add_tail(&device->list, &group->devices);
  357. if (group->domain)
  358. __iommu_attach_device(group->domain, dev);
  359. mutex_unlock(&group->mutex);
  360. /* Notify any listeners about change to group. */
  361. blocking_notifier_call_chain(&group->notifier,
  362. IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
  363. trace_add_device_to_group(group->id, dev);
  364. pr_info("Adding device %s to group %d\n", dev_name(dev), group->id);
  365. return 0;
  366. }
  367. EXPORT_SYMBOL_GPL(iommu_group_add_device);
  368. /**
  369. * iommu_group_remove_device - remove a device from it's current group
  370. * @dev: device to be removed
  371. *
  372. * This function is called by an iommu driver to remove the device from
  373. * it's current group. This decrements the iommu group reference count.
  374. */
  375. void iommu_group_remove_device(struct device *dev)
  376. {
  377. struct iommu_group *group = dev->iommu_group;
  378. struct iommu_device *tmp_device, *device = NULL;
  379. pr_info("Removing device %s from group %d\n", dev_name(dev), group->id);
  380. /* Pre-notify listeners that a device is being removed. */
  381. blocking_notifier_call_chain(&group->notifier,
  382. IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
  383. mutex_lock(&group->mutex);
  384. list_for_each_entry(tmp_device, &group->devices, list) {
  385. if (tmp_device->dev == dev) {
  386. device = tmp_device;
  387. list_del(&device->list);
  388. break;
  389. }
  390. }
  391. mutex_unlock(&group->mutex);
  392. if (!device)
  393. return;
  394. sysfs_remove_link(group->devices_kobj, device->name);
  395. sysfs_remove_link(&dev->kobj, "iommu_group");
  396. trace_remove_device_from_group(group->id, dev);
  397. kfree(device->name);
  398. kfree(device);
  399. dev->iommu_group = NULL;
  400. kobject_put(group->devices_kobj);
  401. }
  402. EXPORT_SYMBOL_GPL(iommu_group_remove_device);
  403. static int iommu_group_device_count(struct iommu_group *group)
  404. {
  405. struct iommu_device *entry;
  406. int ret = 0;
  407. list_for_each_entry(entry, &group->devices, list)
  408. ret++;
  409. return ret;
  410. }
  411. /**
  412. * iommu_group_for_each_dev - iterate over each device in the group
  413. * @group: the group
  414. * @data: caller opaque data to be passed to callback function
  415. * @fn: caller supplied callback function
  416. *
  417. * This function is called by group users to iterate over group devices.
  418. * Callers should hold a reference count to the group during callback.
  419. * The group->mutex is held across callbacks, which will block calls to
  420. * iommu_group_add/remove_device.
  421. */
  422. static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
  423. int (*fn)(struct device *, void *))
  424. {
  425. struct iommu_device *device;
  426. int ret = 0;
  427. list_for_each_entry(device, &group->devices, list) {
  428. ret = fn(device->dev, data);
  429. if (ret)
  430. break;
  431. }
  432. return ret;
  433. }
  434. int iommu_group_for_each_dev(struct iommu_group *group, void *data,
  435. int (*fn)(struct device *, void *))
  436. {
  437. int ret;
  438. mutex_lock(&group->mutex);
  439. ret = __iommu_group_for_each_dev(group, data, fn);
  440. mutex_unlock(&group->mutex);
  441. return ret;
  442. }
  443. EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
  444. /**
  445. * iommu_group_get - Return the group for a device and increment reference
  446. * @dev: get the group that this device belongs to
  447. *
  448. * This function is called by iommu drivers and users to get the group
  449. * for the specified device. If found, the group is returned and the group
  450. * reference in incremented, else NULL.
  451. */
  452. struct iommu_group *iommu_group_get(struct device *dev)
  453. {
  454. struct iommu_group *group = dev->iommu_group;
  455. if (group)
  456. kobject_get(group->devices_kobj);
  457. return group;
  458. }
  459. EXPORT_SYMBOL_GPL(iommu_group_get);
  460. /**
  461. * iommu_group_put - Decrement group reference
  462. * @group: the group to use
  463. *
  464. * This function is called by iommu drivers and users to release the
  465. * iommu group. Once the reference count is zero, the group is released.
  466. */
  467. void iommu_group_put(struct iommu_group *group)
  468. {
  469. if (group)
  470. kobject_put(group->devices_kobj);
  471. }
  472. EXPORT_SYMBOL_GPL(iommu_group_put);
  473. /**
  474. * iommu_group_register_notifier - Register a notifier for group changes
  475. * @group: the group to watch
  476. * @nb: notifier block to signal
  477. *
  478. * This function allows iommu group users to track changes in a group.
  479. * See include/linux/iommu.h for actions sent via this notifier. Caller
  480. * should hold a reference to the group throughout notifier registration.
  481. */
  482. int iommu_group_register_notifier(struct iommu_group *group,
  483. struct notifier_block *nb)
  484. {
  485. return blocking_notifier_chain_register(&group->notifier, nb);
  486. }
  487. EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
  488. /**
  489. * iommu_group_unregister_notifier - Unregister a notifier
  490. * @group: the group to watch
  491. * @nb: notifier block to signal
  492. *
  493. * Unregister a previously registered group notifier block.
  494. */
  495. int iommu_group_unregister_notifier(struct iommu_group *group,
  496. struct notifier_block *nb)
  497. {
  498. return blocking_notifier_chain_unregister(&group->notifier, nb);
  499. }
  500. EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
  501. /**
  502. * iommu_group_id - Return ID for a group
  503. * @group: the group to ID
  504. *
  505. * Return the unique ID for the group matching the sysfs group number.
  506. */
  507. int iommu_group_id(struct iommu_group *group)
  508. {
  509. return group->id;
  510. }
  511. EXPORT_SYMBOL_GPL(iommu_group_id);
  512. static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
  513. unsigned long *devfns);
  514. /*
  515. * To consider a PCI device isolated, we require ACS to support Source
  516. * Validation, Request Redirection, Completer Redirection, and Upstream
  517. * Forwarding. This effectively means that devices cannot spoof their
  518. * requester ID, requests and completions cannot be redirected, and all
  519. * transactions are forwarded upstream, even as it passes through a
  520. * bridge where the target device is downstream.
  521. */
  522. #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
  523. /*
  524. * For multifunction devices which are not isolated from each other, find
  525. * all the other non-isolated functions and look for existing groups. For
  526. * each function, we also need to look for aliases to or from other devices
  527. * that may already have a group.
  528. */
  529. static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
  530. unsigned long *devfns)
  531. {
  532. struct pci_dev *tmp = NULL;
  533. struct iommu_group *group;
  534. if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
  535. return NULL;
  536. for_each_pci_dev(tmp) {
  537. if (tmp == pdev || tmp->bus != pdev->bus ||
  538. PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
  539. pci_acs_enabled(tmp, REQ_ACS_FLAGS))
  540. continue;
  541. group = get_pci_alias_group(tmp, devfns);
  542. if (group) {
  543. pci_dev_put(tmp);
  544. return group;
  545. }
  546. }
  547. return NULL;
  548. }
  549. /*
  550. * Look for aliases to or from the given device for exisiting groups. The
  551. * dma_alias_devfn only supports aliases on the same bus, therefore the search
  552. * space is quite small (especially since we're really only looking at pcie
  553. * device, and therefore only expect multiple slots on the root complex or
  554. * downstream switch ports). It's conceivable though that a pair of
  555. * multifunction devices could have aliases between them that would cause a
  556. * loop. To prevent this, we use a bitmap to track where we've been.
  557. */
  558. static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
  559. unsigned long *devfns)
  560. {
  561. struct pci_dev *tmp = NULL;
  562. struct iommu_group *group;
  563. if (test_and_set_bit(pdev->devfn & 0xff, devfns))
  564. return NULL;
  565. group = iommu_group_get(&pdev->dev);
  566. if (group)
  567. return group;
  568. for_each_pci_dev(tmp) {
  569. if (tmp == pdev || tmp->bus != pdev->bus)
  570. continue;
  571. /* We alias them or they alias us */
  572. if (((pdev->dev_flags & PCI_DEV_FLAGS_DMA_ALIAS_DEVFN) &&
  573. pdev->dma_alias_devfn == tmp->devfn) ||
  574. ((tmp->dev_flags & PCI_DEV_FLAGS_DMA_ALIAS_DEVFN) &&
  575. tmp->dma_alias_devfn == pdev->devfn)) {
  576. group = get_pci_alias_group(tmp, devfns);
  577. if (group) {
  578. pci_dev_put(tmp);
  579. return group;
  580. }
  581. group = get_pci_function_alias_group(tmp, devfns);
  582. if (group) {
  583. pci_dev_put(tmp);
  584. return group;
  585. }
  586. }
  587. }
  588. return NULL;
  589. }
  590. struct group_for_pci_data {
  591. struct pci_dev *pdev;
  592. struct iommu_group *group;
  593. };
  594. /*
  595. * DMA alias iterator callback, return the last seen device. Stop and return
  596. * the IOMMU group if we find one along the way.
  597. */
  598. static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
  599. {
  600. struct group_for_pci_data *data = opaque;
  601. data->pdev = pdev;
  602. data->group = iommu_group_get(&pdev->dev);
  603. return data->group != NULL;
  604. }
  605. /*
  606. * Generic device_group call-back function. It just allocates one
  607. * iommu-group per device.
  608. */
  609. struct iommu_group *generic_device_group(struct device *dev)
  610. {
  611. struct iommu_group *group;
  612. group = iommu_group_alloc();
  613. if (IS_ERR(group))
  614. return NULL;
  615. return group;
  616. }
  617. /*
  618. * Use standard PCI bus topology, isolation features, and DMA alias quirks
  619. * to find or create an IOMMU group for a device.
  620. */
  621. struct iommu_group *pci_device_group(struct device *dev)
  622. {
  623. struct pci_dev *pdev = to_pci_dev(dev);
  624. struct group_for_pci_data data;
  625. struct pci_bus *bus;
  626. struct iommu_group *group = NULL;
  627. u64 devfns[4] = { 0 };
  628. if (WARN_ON(!dev_is_pci(dev)))
  629. return ERR_PTR(-EINVAL);
  630. /*
  631. * Find the upstream DMA alias for the device. A device must not
  632. * be aliased due to topology in order to have its own IOMMU group.
  633. * If we find an alias along the way that already belongs to a
  634. * group, use it.
  635. */
  636. if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
  637. return data.group;
  638. pdev = data.pdev;
  639. /*
  640. * Continue upstream from the point of minimum IOMMU granularity
  641. * due to aliases to the point where devices are protected from
  642. * peer-to-peer DMA by PCI ACS. Again, if we find an existing
  643. * group, use it.
  644. */
  645. for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
  646. if (!bus->self)
  647. continue;
  648. if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
  649. break;
  650. pdev = bus->self;
  651. group = iommu_group_get(&pdev->dev);
  652. if (group)
  653. return group;
  654. }
  655. /*
  656. * Look for existing groups on device aliases. If we alias another
  657. * device or another device aliases us, use the same group.
  658. */
  659. group = get_pci_alias_group(pdev, (unsigned long *)devfns);
  660. if (group)
  661. return group;
  662. /*
  663. * Look for existing groups on non-isolated functions on the same
  664. * slot and aliases of those funcions, if any. No need to clear
  665. * the search bitmap, the tested devfns are still valid.
  666. */
  667. group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
  668. if (group)
  669. return group;
  670. /* No shared group found, allocate new */
  671. group = iommu_group_alloc();
  672. if (IS_ERR(group))
  673. return NULL;
  674. return group;
  675. }
  676. /**
  677. * iommu_group_get_for_dev - Find or create the IOMMU group for a device
  678. * @dev: target device
  679. *
  680. * This function is intended to be called by IOMMU drivers and extended to
  681. * support common, bus-defined algorithms when determining or creating the
  682. * IOMMU group for a device. On success, the caller will hold a reference
  683. * to the returned IOMMU group, which will already include the provided
  684. * device. The reference should be released with iommu_group_put().
  685. */
  686. struct iommu_group *iommu_group_get_for_dev(struct device *dev)
  687. {
  688. const struct iommu_ops *ops = dev->bus->iommu_ops;
  689. struct iommu_group *group;
  690. int ret;
  691. group = iommu_group_get(dev);
  692. if (group)
  693. return group;
  694. group = ERR_PTR(-EINVAL);
  695. if (ops && ops->device_group)
  696. group = ops->device_group(dev);
  697. if (IS_ERR(group))
  698. return group;
  699. /*
  700. * Try to allocate a default domain - needs support from the
  701. * IOMMU driver.
  702. */
  703. if (!group->default_domain) {
  704. group->default_domain = __iommu_domain_alloc(dev->bus,
  705. IOMMU_DOMAIN_DMA);
  706. group->domain = group->default_domain;
  707. }
  708. ret = iommu_group_add_device(group, dev);
  709. if (ret) {
  710. iommu_group_put(group);
  711. return ERR_PTR(ret);
  712. }
  713. return group;
  714. }
  715. struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
  716. {
  717. return group->default_domain;
  718. }
  719. static int add_iommu_group(struct device *dev, void *data)
  720. {
  721. struct iommu_callback_data *cb = data;
  722. const struct iommu_ops *ops = cb->ops;
  723. int ret;
  724. if (!ops->add_device)
  725. return 0;
  726. WARN_ON(dev->iommu_group);
  727. ret = ops->add_device(dev);
  728. /*
  729. * We ignore -ENODEV errors for now, as they just mean that the
  730. * device is not translated by an IOMMU. We still care about
  731. * other errors and fail to initialize when they happen.
  732. */
  733. if (ret == -ENODEV)
  734. ret = 0;
  735. return ret;
  736. }
  737. static int remove_iommu_group(struct device *dev, void *data)
  738. {
  739. struct iommu_callback_data *cb = data;
  740. const struct iommu_ops *ops = cb->ops;
  741. if (ops->remove_device && dev->iommu_group)
  742. ops->remove_device(dev);
  743. return 0;
  744. }
  745. static int iommu_bus_notifier(struct notifier_block *nb,
  746. unsigned long action, void *data)
  747. {
  748. struct device *dev = data;
  749. const struct iommu_ops *ops = dev->bus->iommu_ops;
  750. struct iommu_group *group;
  751. unsigned long group_action = 0;
  752. /*
  753. * ADD/DEL call into iommu driver ops if provided, which may
  754. * result in ADD/DEL notifiers to group->notifier
  755. */
  756. if (action == BUS_NOTIFY_ADD_DEVICE) {
  757. if (ops->add_device)
  758. return ops->add_device(dev);
  759. } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
  760. if (ops->remove_device && dev->iommu_group) {
  761. ops->remove_device(dev);
  762. return 0;
  763. }
  764. }
  765. /*
  766. * Remaining BUS_NOTIFYs get filtered and republished to the
  767. * group, if anyone is listening
  768. */
  769. group = iommu_group_get(dev);
  770. if (!group)
  771. return 0;
  772. switch (action) {
  773. case BUS_NOTIFY_BIND_DRIVER:
  774. group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
  775. break;
  776. case BUS_NOTIFY_BOUND_DRIVER:
  777. group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
  778. break;
  779. case BUS_NOTIFY_UNBIND_DRIVER:
  780. group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
  781. break;
  782. case BUS_NOTIFY_UNBOUND_DRIVER:
  783. group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
  784. break;
  785. }
  786. if (group_action)
  787. blocking_notifier_call_chain(&group->notifier,
  788. group_action, dev);
  789. iommu_group_put(group);
  790. return 0;
  791. }
  792. static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
  793. {
  794. int err;
  795. struct notifier_block *nb;
  796. struct iommu_callback_data cb = {
  797. .ops = ops,
  798. };
  799. nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
  800. if (!nb)
  801. return -ENOMEM;
  802. nb->notifier_call = iommu_bus_notifier;
  803. err = bus_register_notifier(bus, nb);
  804. if (err)
  805. goto out_free;
  806. err = bus_for_each_dev(bus, NULL, &cb, add_iommu_group);
  807. if (err)
  808. goto out_err;
  809. return 0;
  810. out_err:
  811. /* Clean up */
  812. bus_for_each_dev(bus, NULL, &cb, remove_iommu_group);
  813. bus_unregister_notifier(bus, nb);
  814. out_free:
  815. kfree(nb);
  816. return err;
  817. }
  818. /**
  819. * bus_set_iommu - set iommu-callbacks for the bus
  820. * @bus: bus.
  821. * @ops: the callbacks provided by the iommu-driver
  822. *
  823. * This function is called by an iommu driver to set the iommu methods
  824. * used for a particular bus. Drivers for devices on that bus can use
  825. * the iommu-api after these ops are registered.
  826. * This special function is needed because IOMMUs are usually devices on
  827. * the bus itself, so the iommu drivers are not initialized when the bus
  828. * is set up. With this function the iommu-driver can set the iommu-ops
  829. * afterwards.
  830. */
  831. int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
  832. {
  833. int err;
  834. if (bus->iommu_ops != NULL)
  835. return -EBUSY;
  836. bus->iommu_ops = ops;
  837. /* Do IOMMU specific setup for this bus-type */
  838. err = iommu_bus_init(bus, ops);
  839. if (err)
  840. bus->iommu_ops = NULL;
  841. return err;
  842. }
  843. EXPORT_SYMBOL_GPL(bus_set_iommu);
  844. bool iommu_present(struct bus_type *bus)
  845. {
  846. return bus->iommu_ops != NULL;
  847. }
  848. EXPORT_SYMBOL_GPL(iommu_present);
  849. bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
  850. {
  851. if (!bus->iommu_ops || !bus->iommu_ops->capable)
  852. return false;
  853. return bus->iommu_ops->capable(cap);
  854. }
  855. EXPORT_SYMBOL_GPL(iommu_capable);
  856. /**
  857. * iommu_set_fault_handler() - set a fault handler for an iommu domain
  858. * @domain: iommu domain
  859. * @handler: fault handler
  860. * @token: user data, will be passed back to the fault handler
  861. *
  862. * This function should be used by IOMMU users which want to be notified
  863. * whenever an IOMMU fault happens.
  864. *
  865. * The fault handler itself should return 0 on success, and an appropriate
  866. * error code otherwise.
  867. */
  868. void iommu_set_fault_handler(struct iommu_domain *domain,
  869. iommu_fault_handler_t handler,
  870. void *token)
  871. {
  872. BUG_ON(!domain);
  873. domain->handler = handler;
  874. domain->handler_token = token;
  875. }
  876. EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
  877. static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
  878. unsigned type)
  879. {
  880. struct iommu_domain *domain;
  881. if (bus == NULL || bus->iommu_ops == NULL)
  882. return NULL;
  883. domain = bus->iommu_ops->domain_alloc(type);
  884. if (!domain)
  885. return NULL;
  886. domain->ops = bus->iommu_ops;
  887. domain->type = type;
  888. return domain;
  889. }
  890. struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
  891. {
  892. return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
  893. }
  894. EXPORT_SYMBOL_GPL(iommu_domain_alloc);
  895. void iommu_domain_free(struct iommu_domain *domain)
  896. {
  897. domain->ops->domain_free(domain);
  898. }
  899. EXPORT_SYMBOL_GPL(iommu_domain_free);
  900. static int __iommu_attach_device(struct iommu_domain *domain,
  901. struct device *dev)
  902. {
  903. int ret;
  904. if (unlikely(domain->ops->attach_dev == NULL))
  905. return -ENODEV;
  906. ret = domain->ops->attach_dev(domain, dev);
  907. if (!ret)
  908. trace_attach_device_to_domain(dev);
  909. return ret;
  910. }
  911. int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
  912. {
  913. struct iommu_group *group;
  914. int ret;
  915. group = iommu_group_get(dev);
  916. /* FIXME: Remove this when groups a mandatory for iommu drivers */
  917. if (group == NULL)
  918. return __iommu_attach_device(domain, dev);
  919. /*
  920. * We have a group - lock it to make sure the device-count doesn't
  921. * change while we are attaching
  922. */
  923. mutex_lock(&group->mutex);
  924. ret = -EINVAL;
  925. if (iommu_group_device_count(group) != 1)
  926. goto out_unlock;
  927. ret = __iommu_attach_group(domain, group);
  928. out_unlock:
  929. mutex_unlock(&group->mutex);
  930. iommu_group_put(group);
  931. return ret;
  932. }
  933. EXPORT_SYMBOL_GPL(iommu_attach_device);
  934. static void __iommu_detach_device(struct iommu_domain *domain,
  935. struct device *dev)
  936. {
  937. if (unlikely(domain->ops->detach_dev == NULL))
  938. return;
  939. domain->ops->detach_dev(domain, dev);
  940. trace_detach_device_from_domain(dev);
  941. }
  942. void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
  943. {
  944. struct iommu_group *group;
  945. group = iommu_group_get(dev);
  946. /* FIXME: Remove this when groups a mandatory for iommu drivers */
  947. if (group == NULL)
  948. return __iommu_detach_device(domain, dev);
  949. mutex_lock(&group->mutex);
  950. if (iommu_group_device_count(group) != 1) {
  951. WARN_ON(1);
  952. goto out_unlock;
  953. }
  954. __iommu_detach_group(domain, group);
  955. out_unlock:
  956. mutex_unlock(&group->mutex);
  957. iommu_group_put(group);
  958. }
  959. EXPORT_SYMBOL_GPL(iommu_detach_device);
  960. struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
  961. {
  962. struct iommu_domain *domain;
  963. struct iommu_group *group;
  964. group = iommu_group_get(dev);
  965. /* FIXME: Remove this when groups a mandatory for iommu drivers */
  966. if (group == NULL)
  967. return NULL;
  968. domain = group->domain;
  969. iommu_group_put(group);
  970. return domain;
  971. }
  972. EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
  973. /*
  974. * IOMMU groups are really the natrual working unit of the IOMMU, but
  975. * the IOMMU API works on domains and devices. Bridge that gap by
  976. * iterating over the devices in a group. Ideally we'd have a single
  977. * device which represents the requestor ID of the group, but we also
  978. * allow IOMMU drivers to create policy defined minimum sets, where
  979. * the physical hardware may be able to distiguish members, but we
  980. * wish to group them at a higher level (ex. untrusted multi-function
  981. * PCI devices). Thus we attach each device.
  982. */
  983. static int iommu_group_do_attach_device(struct device *dev, void *data)
  984. {
  985. struct iommu_domain *domain = data;
  986. return __iommu_attach_device(domain, dev);
  987. }
  988. static int __iommu_attach_group(struct iommu_domain *domain,
  989. struct iommu_group *group)
  990. {
  991. int ret;
  992. if (group->default_domain && group->domain != group->default_domain)
  993. return -EBUSY;
  994. ret = __iommu_group_for_each_dev(group, domain,
  995. iommu_group_do_attach_device);
  996. if (ret == 0)
  997. group->domain = domain;
  998. return ret;
  999. }
  1000. int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
  1001. {
  1002. int ret;
  1003. mutex_lock(&group->mutex);
  1004. ret = __iommu_attach_group(domain, group);
  1005. mutex_unlock(&group->mutex);
  1006. return ret;
  1007. }
  1008. EXPORT_SYMBOL_GPL(iommu_attach_group);
  1009. static int iommu_group_do_detach_device(struct device *dev, void *data)
  1010. {
  1011. struct iommu_domain *domain = data;
  1012. __iommu_detach_device(domain, dev);
  1013. return 0;
  1014. }
  1015. static void __iommu_detach_group(struct iommu_domain *domain,
  1016. struct iommu_group *group)
  1017. {
  1018. int ret;
  1019. if (!group->default_domain) {
  1020. __iommu_group_for_each_dev(group, domain,
  1021. iommu_group_do_detach_device);
  1022. group->domain = NULL;
  1023. return;
  1024. }
  1025. if (group->domain == group->default_domain)
  1026. return;
  1027. /* Detach by re-attaching to the default domain */
  1028. ret = __iommu_group_for_each_dev(group, group->default_domain,
  1029. iommu_group_do_attach_device);
  1030. if (ret != 0)
  1031. WARN_ON(1);
  1032. else
  1033. group->domain = group->default_domain;
  1034. }
  1035. void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
  1036. {
  1037. mutex_lock(&group->mutex);
  1038. __iommu_detach_group(domain, group);
  1039. mutex_unlock(&group->mutex);
  1040. }
  1041. EXPORT_SYMBOL_GPL(iommu_detach_group);
  1042. phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
  1043. {
  1044. if (unlikely(domain->ops->iova_to_phys == NULL))
  1045. return 0;
  1046. return domain->ops->iova_to_phys(domain, iova);
  1047. }
  1048. EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
  1049. static size_t iommu_pgsize(struct iommu_domain *domain,
  1050. unsigned long addr_merge, size_t size)
  1051. {
  1052. unsigned int pgsize_idx;
  1053. size_t pgsize;
  1054. /* Max page size that still fits into 'size' */
  1055. pgsize_idx = __fls(size);
  1056. /* need to consider alignment requirements ? */
  1057. if (likely(addr_merge)) {
  1058. /* Max page size allowed by address */
  1059. unsigned int align_pgsize_idx = __ffs(addr_merge);
  1060. pgsize_idx = min(pgsize_idx, align_pgsize_idx);
  1061. }
  1062. /* build a mask of acceptable page sizes */
  1063. pgsize = (1UL << (pgsize_idx + 1)) - 1;
  1064. /* throw away page sizes not supported by the hardware */
  1065. pgsize &= domain->ops->pgsize_bitmap;
  1066. /* make sure we're still sane */
  1067. BUG_ON(!pgsize);
  1068. /* pick the biggest page */
  1069. pgsize_idx = __fls(pgsize);
  1070. pgsize = 1UL << pgsize_idx;
  1071. return pgsize;
  1072. }
  1073. int iommu_map(struct iommu_domain *domain, unsigned long iova,
  1074. phys_addr_t paddr, size_t size, int prot)
  1075. {
  1076. unsigned long orig_iova = iova;
  1077. unsigned int min_pagesz;
  1078. size_t orig_size = size;
  1079. phys_addr_t orig_paddr = paddr;
  1080. int ret = 0;
  1081. if (unlikely(domain->ops->map == NULL ||
  1082. domain->ops->pgsize_bitmap == 0UL))
  1083. return -ENODEV;
  1084. if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
  1085. return -EINVAL;
  1086. /* find out the minimum page size supported */
  1087. min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap);
  1088. /*
  1089. * both the virtual address and the physical one, as well as
  1090. * the size of the mapping, must be aligned (at least) to the
  1091. * size of the smallest page supported by the hardware
  1092. */
  1093. if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
  1094. pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
  1095. iova, &paddr, size, min_pagesz);
  1096. return -EINVAL;
  1097. }
  1098. pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
  1099. while (size) {
  1100. size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
  1101. pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
  1102. iova, &paddr, pgsize);
  1103. ret = domain->ops->map(domain, iova, paddr, pgsize, prot);
  1104. if (ret)
  1105. break;
  1106. iova += pgsize;
  1107. paddr += pgsize;
  1108. size -= pgsize;
  1109. }
  1110. /* unroll mapping in case something went wrong */
  1111. if (ret)
  1112. iommu_unmap(domain, orig_iova, orig_size - size);
  1113. else
  1114. trace_map(orig_iova, orig_paddr, orig_size);
  1115. return ret;
  1116. }
  1117. EXPORT_SYMBOL_GPL(iommu_map);
  1118. size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size)
  1119. {
  1120. size_t unmapped_page, unmapped = 0;
  1121. unsigned int min_pagesz;
  1122. unsigned long orig_iova = iova;
  1123. if (unlikely(domain->ops->unmap == NULL ||
  1124. domain->ops->pgsize_bitmap == 0UL))
  1125. return -ENODEV;
  1126. if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
  1127. return -EINVAL;
  1128. /* find out the minimum page size supported */
  1129. min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap);
  1130. /*
  1131. * The virtual address, as well as the size of the mapping, must be
  1132. * aligned (at least) to the size of the smallest page supported
  1133. * by the hardware
  1134. */
  1135. if (!IS_ALIGNED(iova | size, min_pagesz)) {
  1136. pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
  1137. iova, size, min_pagesz);
  1138. return -EINVAL;
  1139. }
  1140. pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
  1141. /*
  1142. * Keep iterating until we either unmap 'size' bytes (or more)
  1143. * or we hit an area that isn't mapped.
  1144. */
  1145. while (unmapped < size) {
  1146. size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
  1147. unmapped_page = domain->ops->unmap(domain, iova, pgsize);
  1148. if (!unmapped_page)
  1149. break;
  1150. pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
  1151. iova, unmapped_page);
  1152. iova += unmapped_page;
  1153. unmapped += unmapped_page;
  1154. }
  1155. trace_unmap(orig_iova, size, unmapped);
  1156. return unmapped;
  1157. }
  1158. EXPORT_SYMBOL_GPL(iommu_unmap);
  1159. size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
  1160. struct scatterlist *sg, unsigned int nents, int prot)
  1161. {
  1162. struct scatterlist *s;
  1163. size_t mapped = 0;
  1164. unsigned int i, min_pagesz;
  1165. int ret;
  1166. if (unlikely(domain->ops->pgsize_bitmap == 0UL))
  1167. return 0;
  1168. min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap);
  1169. for_each_sg(sg, s, nents, i) {
  1170. phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset;
  1171. /*
  1172. * We are mapping on IOMMU page boundaries, so offset within
  1173. * the page must be 0. However, the IOMMU may support pages
  1174. * smaller than PAGE_SIZE, so s->offset may still represent
  1175. * an offset of that boundary within the CPU page.
  1176. */
  1177. if (!IS_ALIGNED(s->offset, min_pagesz))
  1178. goto out_err;
  1179. ret = iommu_map(domain, iova + mapped, phys, s->length, prot);
  1180. if (ret)
  1181. goto out_err;
  1182. mapped += s->length;
  1183. }
  1184. return mapped;
  1185. out_err:
  1186. /* undo mappings already done */
  1187. iommu_unmap(domain, iova, mapped);
  1188. return 0;
  1189. }
  1190. EXPORT_SYMBOL_GPL(default_iommu_map_sg);
  1191. int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
  1192. phys_addr_t paddr, u64 size, int prot)
  1193. {
  1194. if (unlikely(domain->ops->domain_window_enable == NULL))
  1195. return -ENODEV;
  1196. return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
  1197. prot);
  1198. }
  1199. EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
  1200. void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
  1201. {
  1202. if (unlikely(domain->ops->domain_window_disable == NULL))
  1203. return;
  1204. return domain->ops->domain_window_disable(domain, wnd_nr);
  1205. }
  1206. EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
  1207. static int __init iommu_init(void)
  1208. {
  1209. iommu_group_kset = kset_create_and_add("iommu_groups",
  1210. NULL, kernel_kobj);
  1211. ida_init(&iommu_group_ida);
  1212. mutex_init(&iommu_group_mutex);
  1213. BUG_ON(!iommu_group_kset);
  1214. return 0;
  1215. }
  1216. core_initcall(iommu_init);
  1217. int iommu_domain_get_attr(struct iommu_domain *domain,
  1218. enum iommu_attr attr, void *data)
  1219. {
  1220. struct iommu_domain_geometry *geometry;
  1221. bool *paging;
  1222. int ret = 0;
  1223. u32 *count;
  1224. switch (attr) {
  1225. case DOMAIN_ATTR_GEOMETRY:
  1226. geometry = data;
  1227. *geometry = domain->geometry;
  1228. break;
  1229. case DOMAIN_ATTR_PAGING:
  1230. paging = data;
  1231. *paging = (domain->ops->pgsize_bitmap != 0UL);
  1232. break;
  1233. case DOMAIN_ATTR_WINDOWS:
  1234. count = data;
  1235. if (domain->ops->domain_get_windows != NULL)
  1236. *count = domain->ops->domain_get_windows(domain);
  1237. else
  1238. ret = -ENODEV;
  1239. break;
  1240. default:
  1241. if (!domain->ops->domain_get_attr)
  1242. return -EINVAL;
  1243. ret = domain->ops->domain_get_attr(domain, attr, data);
  1244. }
  1245. return ret;
  1246. }
  1247. EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
  1248. int iommu_domain_set_attr(struct iommu_domain *domain,
  1249. enum iommu_attr attr, void *data)
  1250. {
  1251. int ret = 0;
  1252. u32 *count;
  1253. switch (attr) {
  1254. case DOMAIN_ATTR_WINDOWS:
  1255. count = data;
  1256. if (domain->ops->domain_set_windows != NULL)
  1257. ret = domain->ops->domain_set_windows(domain, *count);
  1258. else
  1259. ret = -ENODEV;
  1260. break;
  1261. default:
  1262. if (domain->ops->domain_set_attr == NULL)
  1263. return -EINVAL;
  1264. ret = domain->ops->domain_set_attr(domain, attr, data);
  1265. }
  1266. return ret;
  1267. }
  1268. EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
  1269. void iommu_get_dm_regions(struct device *dev, struct list_head *list)
  1270. {
  1271. const struct iommu_ops *ops = dev->bus->iommu_ops;
  1272. if (ops && ops->get_dm_regions)
  1273. ops->get_dm_regions(dev, list);
  1274. }
  1275. void iommu_put_dm_regions(struct device *dev, struct list_head *list)
  1276. {
  1277. const struct iommu_ops *ops = dev->bus->iommu_ops;
  1278. if (ops && ops->put_dm_regions)
  1279. ops->put_dm_regions(dev, list);
  1280. }
  1281. /* Request that a device is direct mapped by the IOMMU */
  1282. int iommu_request_dm_for_dev(struct device *dev)
  1283. {
  1284. struct iommu_domain *dm_domain;
  1285. struct iommu_group *group;
  1286. int ret;
  1287. /* Device must already be in a group before calling this function */
  1288. group = iommu_group_get_for_dev(dev);
  1289. if (IS_ERR(group))
  1290. return PTR_ERR(group);
  1291. mutex_lock(&group->mutex);
  1292. /* Check if the default domain is already direct mapped */
  1293. ret = 0;
  1294. if (group->default_domain &&
  1295. group->default_domain->type == IOMMU_DOMAIN_IDENTITY)
  1296. goto out;
  1297. /* Don't change mappings of existing devices */
  1298. ret = -EBUSY;
  1299. if (iommu_group_device_count(group) != 1)
  1300. goto out;
  1301. /* Allocate a direct mapped domain */
  1302. ret = -ENOMEM;
  1303. dm_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_IDENTITY);
  1304. if (!dm_domain)
  1305. goto out;
  1306. /* Attach the device to the domain */
  1307. ret = __iommu_attach_group(dm_domain, group);
  1308. if (ret) {
  1309. iommu_domain_free(dm_domain);
  1310. goto out;
  1311. }
  1312. /* Make the direct mapped domain the default for this group */
  1313. if (group->default_domain)
  1314. iommu_domain_free(group->default_domain);
  1315. group->default_domain = dm_domain;
  1316. pr_info("Using direct mapping for device %s\n", dev_name(dev));
  1317. ret = 0;
  1318. out:
  1319. mutex_unlock(&group->mutex);
  1320. iommu_group_put(group);
  1321. return ret;
  1322. }