iommu.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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. static struct kset *iommu_group_kset;
  31. static struct ida iommu_group_ida;
  32. static struct mutex iommu_group_mutex;
  33. struct iommu_group {
  34. struct kobject kobj;
  35. struct kobject *devices_kobj;
  36. struct list_head devices;
  37. struct mutex mutex;
  38. struct blocking_notifier_head notifier;
  39. void *iommu_data;
  40. void (*iommu_data_release)(void *iommu_data);
  41. char *name;
  42. int id;
  43. };
  44. struct iommu_device {
  45. struct list_head list;
  46. struct device *dev;
  47. char *name;
  48. };
  49. struct iommu_group_attribute {
  50. struct attribute attr;
  51. ssize_t (*show)(struct iommu_group *group, char *buf);
  52. ssize_t (*store)(struct iommu_group *group,
  53. const char *buf, size_t count);
  54. };
  55. #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
  56. struct iommu_group_attribute iommu_group_attr_##_name = \
  57. __ATTR(_name, _mode, _show, _store)
  58. #define to_iommu_group_attr(_attr) \
  59. container_of(_attr, struct iommu_group_attribute, attr)
  60. #define to_iommu_group(_kobj) \
  61. container_of(_kobj, struct iommu_group, kobj)
  62. static ssize_t iommu_group_attr_show(struct kobject *kobj,
  63. struct attribute *__attr, char *buf)
  64. {
  65. struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
  66. struct iommu_group *group = to_iommu_group(kobj);
  67. ssize_t ret = -EIO;
  68. if (attr->show)
  69. ret = attr->show(group, buf);
  70. return ret;
  71. }
  72. static ssize_t iommu_group_attr_store(struct kobject *kobj,
  73. struct attribute *__attr,
  74. const char *buf, size_t count)
  75. {
  76. struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
  77. struct iommu_group *group = to_iommu_group(kobj);
  78. ssize_t ret = -EIO;
  79. if (attr->store)
  80. ret = attr->store(group, buf, count);
  81. return ret;
  82. }
  83. static const struct sysfs_ops iommu_group_sysfs_ops = {
  84. .show = iommu_group_attr_show,
  85. .store = iommu_group_attr_store,
  86. };
  87. static int iommu_group_create_file(struct iommu_group *group,
  88. struct iommu_group_attribute *attr)
  89. {
  90. return sysfs_create_file(&group->kobj, &attr->attr);
  91. }
  92. static void iommu_group_remove_file(struct iommu_group *group,
  93. struct iommu_group_attribute *attr)
  94. {
  95. sysfs_remove_file(&group->kobj, &attr->attr);
  96. }
  97. static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
  98. {
  99. return sprintf(buf, "%s\n", group->name);
  100. }
  101. static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
  102. static void iommu_group_release(struct kobject *kobj)
  103. {
  104. struct iommu_group *group = to_iommu_group(kobj);
  105. if (group->iommu_data_release)
  106. group->iommu_data_release(group->iommu_data);
  107. mutex_lock(&iommu_group_mutex);
  108. ida_remove(&iommu_group_ida, group->id);
  109. mutex_unlock(&iommu_group_mutex);
  110. kfree(group->name);
  111. kfree(group);
  112. }
  113. static struct kobj_type iommu_group_ktype = {
  114. .sysfs_ops = &iommu_group_sysfs_ops,
  115. .release = iommu_group_release,
  116. };
  117. /**
  118. * iommu_group_alloc - Allocate a new group
  119. * @name: Optional name to associate with group, visible in sysfs
  120. *
  121. * This function is called by an iommu driver to allocate a new iommu
  122. * group. The iommu group represents the minimum granularity of the iommu.
  123. * Upon successful return, the caller holds a reference to the supplied
  124. * group in order to hold the group until devices are added. Use
  125. * iommu_group_put() to release this extra reference count, allowing the
  126. * group to be automatically reclaimed once it has no devices or external
  127. * references.
  128. */
  129. struct iommu_group *iommu_group_alloc(void)
  130. {
  131. struct iommu_group *group;
  132. int ret;
  133. group = kzalloc(sizeof(*group), GFP_KERNEL);
  134. if (!group)
  135. return ERR_PTR(-ENOMEM);
  136. group->kobj.kset = iommu_group_kset;
  137. mutex_init(&group->mutex);
  138. INIT_LIST_HEAD(&group->devices);
  139. BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
  140. mutex_lock(&iommu_group_mutex);
  141. again:
  142. if (unlikely(0 == ida_pre_get(&iommu_group_ida, GFP_KERNEL))) {
  143. kfree(group);
  144. mutex_unlock(&iommu_group_mutex);
  145. return ERR_PTR(-ENOMEM);
  146. }
  147. if (-EAGAIN == ida_get_new(&iommu_group_ida, &group->id))
  148. goto again;
  149. mutex_unlock(&iommu_group_mutex);
  150. ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
  151. NULL, "%d", group->id);
  152. if (ret) {
  153. mutex_lock(&iommu_group_mutex);
  154. ida_remove(&iommu_group_ida, group->id);
  155. mutex_unlock(&iommu_group_mutex);
  156. kfree(group);
  157. return ERR_PTR(ret);
  158. }
  159. group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
  160. if (!group->devices_kobj) {
  161. kobject_put(&group->kobj); /* triggers .release & free */
  162. return ERR_PTR(-ENOMEM);
  163. }
  164. /*
  165. * The devices_kobj holds a reference on the group kobject, so
  166. * as long as that exists so will the group. We can therefore
  167. * use the devices_kobj for reference counting.
  168. */
  169. kobject_put(&group->kobj);
  170. return group;
  171. }
  172. EXPORT_SYMBOL_GPL(iommu_group_alloc);
  173. /**
  174. * iommu_group_get_iommudata - retrieve iommu_data registered for a group
  175. * @group: the group
  176. *
  177. * iommu drivers can store data in the group for use when doing iommu
  178. * operations. This function provides a way to retrieve it. Caller
  179. * should hold a group reference.
  180. */
  181. void *iommu_group_get_iommudata(struct iommu_group *group)
  182. {
  183. return group->iommu_data;
  184. }
  185. EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
  186. /**
  187. * iommu_group_set_iommudata - set iommu_data for a group
  188. * @group: the group
  189. * @iommu_data: new data
  190. * @release: release function for iommu_data
  191. *
  192. * iommu drivers can store data in the group for use when doing iommu
  193. * operations. This function provides a way to set the data after
  194. * the group has been allocated. Caller should hold a group reference.
  195. */
  196. void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
  197. void (*release)(void *iommu_data))
  198. {
  199. group->iommu_data = iommu_data;
  200. group->iommu_data_release = release;
  201. }
  202. EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
  203. /**
  204. * iommu_group_set_name - set name for a group
  205. * @group: the group
  206. * @name: name
  207. *
  208. * Allow iommu driver to set a name for a group. When set it will
  209. * appear in a name attribute file under the group in sysfs.
  210. */
  211. int iommu_group_set_name(struct iommu_group *group, const char *name)
  212. {
  213. int ret;
  214. if (group->name) {
  215. iommu_group_remove_file(group, &iommu_group_attr_name);
  216. kfree(group->name);
  217. group->name = NULL;
  218. if (!name)
  219. return 0;
  220. }
  221. group->name = kstrdup(name, GFP_KERNEL);
  222. if (!group->name)
  223. return -ENOMEM;
  224. ret = iommu_group_create_file(group, &iommu_group_attr_name);
  225. if (ret) {
  226. kfree(group->name);
  227. group->name = NULL;
  228. return ret;
  229. }
  230. return 0;
  231. }
  232. EXPORT_SYMBOL_GPL(iommu_group_set_name);
  233. /**
  234. * iommu_group_add_device - add a device to an iommu group
  235. * @group: the group into which to add the device (reference should be held)
  236. * @dev: the device
  237. *
  238. * This function is called by an iommu driver to add a device into a
  239. * group. Adding a device increments the group reference count.
  240. */
  241. int iommu_group_add_device(struct iommu_group *group, struct device *dev)
  242. {
  243. int ret, i = 0;
  244. struct iommu_device *device;
  245. device = kzalloc(sizeof(*device), GFP_KERNEL);
  246. if (!device)
  247. return -ENOMEM;
  248. device->dev = dev;
  249. ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
  250. if (ret) {
  251. kfree(device);
  252. return ret;
  253. }
  254. device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
  255. rename:
  256. if (!device->name) {
  257. sysfs_remove_link(&dev->kobj, "iommu_group");
  258. kfree(device);
  259. return -ENOMEM;
  260. }
  261. ret = sysfs_create_link_nowarn(group->devices_kobj,
  262. &dev->kobj, device->name);
  263. if (ret) {
  264. kfree(device->name);
  265. if (ret == -EEXIST && i >= 0) {
  266. /*
  267. * Account for the slim chance of collision
  268. * and append an instance to the name.
  269. */
  270. device->name = kasprintf(GFP_KERNEL, "%s.%d",
  271. kobject_name(&dev->kobj), i++);
  272. goto rename;
  273. }
  274. sysfs_remove_link(&dev->kobj, "iommu_group");
  275. kfree(device);
  276. return ret;
  277. }
  278. kobject_get(group->devices_kobj);
  279. dev->iommu_group = group;
  280. mutex_lock(&group->mutex);
  281. list_add_tail(&device->list, &group->devices);
  282. mutex_unlock(&group->mutex);
  283. /* Notify any listeners about change to group. */
  284. blocking_notifier_call_chain(&group->notifier,
  285. IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
  286. return 0;
  287. }
  288. EXPORT_SYMBOL_GPL(iommu_group_add_device);
  289. /**
  290. * iommu_group_remove_device - remove a device from it's current group
  291. * @dev: device to be removed
  292. *
  293. * This function is called by an iommu driver to remove the device from
  294. * it's current group. This decrements the iommu group reference count.
  295. */
  296. void iommu_group_remove_device(struct device *dev)
  297. {
  298. struct iommu_group *group = dev->iommu_group;
  299. struct iommu_device *tmp_device, *device = NULL;
  300. /* Pre-notify listeners that a device is being removed. */
  301. blocking_notifier_call_chain(&group->notifier,
  302. IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
  303. mutex_lock(&group->mutex);
  304. list_for_each_entry(tmp_device, &group->devices, list) {
  305. if (tmp_device->dev == dev) {
  306. device = tmp_device;
  307. list_del(&device->list);
  308. break;
  309. }
  310. }
  311. mutex_unlock(&group->mutex);
  312. if (!device)
  313. return;
  314. sysfs_remove_link(group->devices_kobj, device->name);
  315. sysfs_remove_link(&dev->kobj, "iommu_group");
  316. kfree(device->name);
  317. kfree(device);
  318. dev->iommu_group = NULL;
  319. kobject_put(group->devices_kobj);
  320. }
  321. EXPORT_SYMBOL_GPL(iommu_group_remove_device);
  322. /**
  323. * iommu_group_for_each_dev - iterate over each device in the group
  324. * @group: the group
  325. * @data: caller opaque data to be passed to callback function
  326. * @fn: caller supplied callback function
  327. *
  328. * This function is called by group users to iterate over group devices.
  329. * Callers should hold a reference count to the group during callback.
  330. * The group->mutex is held across callbacks, which will block calls to
  331. * iommu_group_add/remove_device.
  332. */
  333. int iommu_group_for_each_dev(struct iommu_group *group, void *data,
  334. int (*fn)(struct device *, void *))
  335. {
  336. struct iommu_device *device;
  337. int ret = 0;
  338. mutex_lock(&group->mutex);
  339. list_for_each_entry(device, &group->devices, list) {
  340. ret = fn(device->dev, data);
  341. if (ret)
  342. break;
  343. }
  344. mutex_unlock(&group->mutex);
  345. return ret;
  346. }
  347. EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
  348. /**
  349. * iommu_group_get - Return the group for a device and increment reference
  350. * @dev: get the group that this device belongs to
  351. *
  352. * This function is called by iommu drivers and users to get the group
  353. * for the specified device. If found, the group is returned and the group
  354. * reference in incremented, else NULL.
  355. */
  356. struct iommu_group *iommu_group_get(struct device *dev)
  357. {
  358. struct iommu_group *group = dev->iommu_group;
  359. if (group)
  360. kobject_get(group->devices_kobj);
  361. return group;
  362. }
  363. EXPORT_SYMBOL_GPL(iommu_group_get);
  364. /**
  365. * iommu_group_put - Decrement group reference
  366. * @group: the group to use
  367. *
  368. * This function is called by iommu drivers and users to release the
  369. * iommu group. Once the reference count is zero, the group is released.
  370. */
  371. void iommu_group_put(struct iommu_group *group)
  372. {
  373. if (group)
  374. kobject_put(group->devices_kobj);
  375. }
  376. EXPORT_SYMBOL_GPL(iommu_group_put);
  377. /**
  378. * iommu_group_register_notifier - Register a notifier for group changes
  379. * @group: the group to watch
  380. * @nb: notifier block to signal
  381. *
  382. * This function allows iommu group users to track changes in a group.
  383. * See include/linux/iommu.h for actions sent via this notifier. Caller
  384. * should hold a reference to the group throughout notifier registration.
  385. */
  386. int iommu_group_register_notifier(struct iommu_group *group,
  387. struct notifier_block *nb)
  388. {
  389. return blocking_notifier_chain_register(&group->notifier, nb);
  390. }
  391. EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
  392. /**
  393. * iommu_group_unregister_notifier - Unregister a notifier
  394. * @group: the group to watch
  395. * @nb: notifier block to signal
  396. *
  397. * Unregister a previously registered group notifier block.
  398. */
  399. int iommu_group_unregister_notifier(struct iommu_group *group,
  400. struct notifier_block *nb)
  401. {
  402. return blocking_notifier_chain_unregister(&group->notifier, nb);
  403. }
  404. EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
  405. /**
  406. * iommu_group_id - Return ID for a group
  407. * @group: the group to ID
  408. *
  409. * Return the unique ID for the group matching the sysfs group number.
  410. */
  411. int iommu_group_id(struct iommu_group *group)
  412. {
  413. return group->id;
  414. }
  415. EXPORT_SYMBOL_GPL(iommu_group_id);
  416. static int add_iommu_group(struct device *dev, void *data)
  417. {
  418. struct iommu_ops *ops = data;
  419. if (!ops->add_device)
  420. return -ENODEV;
  421. WARN_ON(dev->iommu_group);
  422. ops->add_device(dev);
  423. return 0;
  424. }
  425. static int iommu_bus_notifier(struct notifier_block *nb,
  426. unsigned long action, void *data)
  427. {
  428. struct device *dev = data;
  429. struct iommu_ops *ops = dev->bus->iommu_ops;
  430. struct iommu_group *group;
  431. unsigned long group_action = 0;
  432. /*
  433. * ADD/DEL call into iommu driver ops if provided, which may
  434. * result in ADD/DEL notifiers to group->notifier
  435. */
  436. if (action == BUS_NOTIFY_ADD_DEVICE) {
  437. if (ops->add_device)
  438. return ops->add_device(dev);
  439. } else if (action == BUS_NOTIFY_DEL_DEVICE) {
  440. if (ops->remove_device && dev->iommu_group) {
  441. ops->remove_device(dev);
  442. return 0;
  443. }
  444. }
  445. /*
  446. * Remaining BUS_NOTIFYs get filtered and republished to the
  447. * group, if anyone is listening
  448. */
  449. group = iommu_group_get(dev);
  450. if (!group)
  451. return 0;
  452. switch (action) {
  453. case BUS_NOTIFY_BIND_DRIVER:
  454. group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
  455. break;
  456. case BUS_NOTIFY_BOUND_DRIVER:
  457. group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
  458. break;
  459. case BUS_NOTIFY_UNBIND_DRIVER:
  460. group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
  461. break;
  462. case BUS_NOTIFY_UNBOUND_DRIVER:
  463. group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
  464. break;
  465. }
  466. if (group_action)
  467. blocking_notifier_call_chain(&group->notifier,
  468. group_action, dev);
  469. iommu_group_put(group);
  470. return 0;
  471. }
  472. static struct notifier_block iommu_bus_nb = {
  473. .notifier_call = iommu_bus_notifier,
  474. };
  475. static void iommu_bus_init(struct bus_type *bus, struct iommu_ops *ops)
  476. {
  477. bus_register_notifier(bus, &iommu_bus_nb);
  478. bus_for_each_dev(bus, NULL, ops, add_iommu_group);
  479. }
  480. /**
  481. * bus_set_iommu - set iommu-callbacks for the bus
  482. * @bus: bus.
  483. * @ops: the callbacks provided by the iommu-driver
  484. *
  485. * This function is called by an iommu driver to set the iommu methods
  486. * used for a particular bus. Drivers for devices on that bus can use
  487. * the iommu-api after these ops are registered.
  488. * This special function is needed because IOMMUs are usually devices on
  489. * the bus itself, so the iommu drivers are not initialized when the bus
  490. * is set up. With this function the iommu-driver can set the iommu-ops
  491. * afterwards.
  492. */
  493. int bus_set_iommu(struct bus_type *bus, struct iommu_ops *ops)
  494. {
  495. if (bus->iommu_ops != NULL)
  496. return -EBUSY;
  497. bus->iommu_ops = ops;
  498. /* Do IOMMU specific setup for this bus-type */
  499. iommu_bus_init(bus, ops);
  500. return 0;
  501. }
  502. EXPORT_SYMBOL_GPL(bus_set_iommu);
  503. bool iommu_present(struct bus_type *bus)
  504. {
  505. return bus->iommu_ops != NULL;
  506. }
  507. EXPORT_SYMBOL_GPL(iommu_present);
  508. /**
  509. * iommu_set_fault_handler() - set a fault handler for an iommu domain
  510. * @domain: iommu domain
  511. * @handler: fault handler
  512. * @token: user data, will be passed back to the fault handler
  513. *
  514. * This function should be used by IOMMU users which want to be notified
  515. * whenever an IOMMU fault happens.
  516. *
  517. * The fault handler itself should return 0 on success, and an appropriate
  518. * error code otherwise.
  519. */
  520. void iommu_set_fault_handler(struct iommu_domain *domain,
  521. iommu_fault_handler_t handler,
  522. void *token)
  523. {
  524. BUG_ON(!domain);
  525. domain->handler = handler;
  526. domain->handler_token = token;
  527. }
  528. EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
  529. struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
  530. {
  531. struct iommu_domain *domain;
  532. int ret;
  533. if (bus == NULL || bus->iommu_ops == NULL)
  534. return NULL;
  535. domain = kzalloc(sizeof(*domain), GFP_KERNEL);
  536. if (!domain)
  537. return NULL;
  538. domain->ops = bus->iommu_ops;
  539. ret = domain->ops->domain_init(domain);
  540. if (ret)
  541. goto out_free;
  542. return domain;
  543. out_free:
  544. kfree(domain);
  545. return NULL;
  546. }
  547. EXPORT_SYMBOL_GPL(iommu_domain_alloc);
  548. void iommu_domain_free(struct iommu_domain *domain)
  549. {
  550. if (likely(domain->ops->domain_destroy != NULL))
  551. domain->ops->domain_destroy(domain);
  552. kfree(domain);
  553. }
  554. EXPORT_SYMBOL_GPL(iommu_domain_free);
  555. int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
  556. {
  557. if (unlikely(domain->ops->attach_dev == NULL))
  558. return -ENODEV;
  559. return domain->ops->attach_dev(domain, dev);
  560. }
  561. EXPORT_SYMBOL_GPL(iommu_attach_device);
  562. void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
  563. {
  564. if (unlikely(domain->ops->detach_dev == NULL))
  565. return;
  566. domain->ops->detach_dev(domain, dev);
  567. }
  568. EXPORT_SYMBOL_GPL(iommu_detach_device);
  569. /*
  570. * IOMMU groups are really the natrual working unit of the IOMMU, but
  571. * the IOMMU API works on domains and devices. Bridge that gap by
  572. * iterating over the devices in a group. Ideally we'd have a single
  573. * device which represents the requestor ID of the group, but we also
  574. * allow IOMMU drivers to create policy defined minimum sets, where
  575. * the physical hardware may be able to distiguish members, but we
  576. * wish to group them at a higher level (ex. untrusted multi-function
  577. * PCI devices). Thus we attach each device.
  578. */
  579. static int iommu_group_do_attach_device(struct device *dev, void *data)
  580. {
  581. struct iommu_domain *domain = data;
  582. return iommu_attach_device(domain, dev);
  583. }
  584. int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
  585. {
  586. return iommu_group_for_each_dev(group, domain,
  587. iommu_group_do_attach_device);
  588. }
  589. EXPORT_SYMBOL_GPL(iommu_attach_group);
  590. static int iommu_group_do_detach_device(struct device *dev, void *data)
  591. {
  592. struct iommu_domain *domain = data;
  593. iommu_detach_device(domain, dev);
  594. return 0;
  595. }
  596. void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
  597. {
  598. iommu_group_for_each_dev(group, domain, iommu_group_do_detach_device);
  599. }
  600. EXPORT_SYMBOL_GPL(iommu_detach_group);
  601. phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
  602. unsigned long iova)
  603. {
  604. if (unlikely(domain->ops->iova_to_phys == NULL))
  605. return 0;
  606. return domain->ops->iova_to_phys(domain, iova);
  607. }
  608. EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
  609. int iommu_domain_has_cap(struct iommu_domain *domain,
  610. unsigned long cap)
  611. {
  612. if (unlikely(domain->ops->domain_has_cap == NULL))
  613. return 0;
  614. return domain->ops->domain_has_cap(domain, cap);
  615. }
  616. EXPORT_SYMBOL_GPL(iommu_domain_has_cap);
  617. int iommu_map(struct iommu_domain *domain, unsigned long iova,
  618. phys_addr_t paddr, size_t size, int prot)
  619. {
  620. unsigned long orig_iova = iova;
  621. unsigned int min_pagesz;
  622. size_t orig_size = size;
  623. int ret = 0;
  624. if (unlikely(domain->ops->map == NULL))
  625. return -ENODEV;
  626. /* find out the minimum page size supported */
  627. min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap);
  628. /*
  629. * both the virtual address and the physical one, as well as
  630. * the size of the mapping, must be aligned (at least) to the
  631. * size of the smallest page supported by the hardware
  632. */
  633. if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
  634. pr_err("unaligned: iova 0x%lx pa 0x%lx size 0x%lx min_pagesz "
  635. "0x%x\n", iova, (unsigned long)paddr,
  636. (unsigned long)size, min_pagesz);
  637. return -EINVAL;
  638. }
  639. pr_debug("map: iova 0x%lx pa 0x%lx size 0x%lx\n", iova,
  640. (unsigned long)paddr, (unsigned long)size);
  641. while (size) {
  642. unsigned long pgsize, addr_merge = iova | paddr;
  643. unsigned int pgsize_idx;
  644. /* Max page size that still fits into 'size' */
  645. pgsize_idx = __fls(size);
  646. /* need to consider alignment requirements ? */
  647. if (likely(addr_merge)) {
  648. /* Max page size allowed by both iova and paddr */
  649. unsigned int align_pgsize_idx = __ffs(addr_merge);
  650. pgsize_idx = min(pgsize_idx, align_pgsize_idx);
  651. }
  652. /* build a mask of acceptable page sizes */
  653. pgsize = (1UL << (pgsize_idx + 1)) - 1;
  654. /* throw away page sizes not supported by the hardware */
  655. pgsize &= domain->ops->pgsize_bitmap;
  656. /* make sure we're still sane */
  657. BUG_ON(!pgsize);
  658. /* pick the biggest page */
  659. pgsize_idx = __fls(pgsize);
  660. pgsize = 1UL << pgsize_idx;
  661. pr_debug("mapping: iova 0x%lx pa 0x%lx pgsize %lu\n", iova,
  662. (unsigned long)paddr, pgsize);
  663. ret = domain->ops->map(domain, iova, paddr, pgsize, prot);
  664. if (ret)
  665. break;
  666. iova += pgsize;
  667. paddr += pgsize;
  668. size -= pgsize;
  669. }
  670. /* unroll mapping in case something went wrong */
  671. if (ret)
  672. iommu_unmap(domain, orig_iova, orig_size - size);
  673. return ret;
  674. }
  675. EXPORT_SYMBOL_GPL(iommu_map);
  676. size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size)
  677. {
  678. size_t unmapped_page, unmapped = 0;
  679. unsigned int min_pagesz;
  680. if (unlikely(domain->ops->unmap == NULL))
  681. return -ENODEV;
  682. /* find out the minimum page size supported */
  683. min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap);
  684. /*
  685. * The virtual address, as well as the size of the mapping, must be
  686. * aligned (at least) to the size of the smallest page supported
  687. * by the hardware
  688. */
  689. if (!IS_ALIGNED(iova | size, min_pagesz)) {
  690. pr_err("unaligned: iova 0x%lx size 0x%lx min_pagesz 0x%x\n",
  691. iova, (unsigned long)size, min_pagesz);
  692. return -EINVAL;
  693. }
  694. pr_debug("unmap this: iova 0x%lx size 0x%lx\n", iova,
  695. (unsigned long)size);
  696. /*
  697. * Keep iterating until we either unmap 'size' bytes (or more)
  698. * or we hit an area that isn't mapped.
  699. */
  700. while (unmapped < size) {
  701. size_t left = size - unmapped;
  702. unmapped_page = domain->ops->unmap(domain, iova, left);
  703. if (!unmapped_page)
  704. break;
  705. pr_debug("unmapped: iova 0x%lx size %lx\n", iova,
  706. (unsigned long)unmapped_page);
  707. iova += unmapped_page;
  708. unmapped += unmapped_page;
  709. }
  710. return unmapped;
  711. }
  712. EXPORT_SYMBOL_GPL(iommu_unmap);
  713. static int __init iommu_init(void)
  714. {
  715. iommu_group_kset = kset_create_and_add("iommu_groups",
  716. NULL, kernel_kobj);
  717. ida_init(&iommu_group_ida);
  718. mutex_init(&iommu_group_mutex);
  719. BUG_ON(!iommu_group_kset);
  720. return 0;
  721. }
  722. subsys_initcall(iommu_init);
  723. int iommu_domain_get_attr(struct iommu_domain *domain,
  724. enum iommu_attr attr, void *data)
  725. {
  726. struct iommu_domain_geometry *geometry;
  727. int ret = 0;
  728. switch (attr) {
  729. case DOMAIN_ATTR_GEOMETRY:
  730. geometry = data;
  731. *geometry = domain->geometry;
  732. break;
  733. default:
  734. if (!domain->ops->domain_get_attr)
  735. return -EINVAL;
  736. ret = domain->ops->domain_get_attr(domain, attr, data);
  737. }
  738. return ret;
  739. }
  740. EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
  741. int iommu_domain_set_attr(struct iommu_domain *domain,
  742. enum iommu_attr attr, void *data)
  743. {
  744. if (!domain->ops->domain_set_attr)
  745. return -EINVAL;
  746. return domain->ops->domain_set_attr(domain, attr, data);
  747. }
  748. EXPORT_SYMBOL_GPL(iommu_domain_set_attr);