iommu.c 24 KB

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