iommu.c 41 KB

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