iommu.c 32 KB

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