iommu.c 30 KB

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