iommu.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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. struct iommu_group *iommu_group_get_by_id(int id)
  174. {
  175. struct kobject *group_kobj;
  176. struct iommu_group *group;
  177. const char *name;
  178. if (!iommu_group_kset)
  179. return NULL;
  180. name = kasprintf(GFP_KERNEL, "%d", id);
  181. if (!name)
  182. return NULL;
  183. group_kobj = kset_find_obj(iommu_group_kset, name);
  184. kfree(name);
  185. if (!group_kobj)
  186. return NULL;
  187. group = container_of(group_kobj, struct iommu_group, kobj);
  188. BUG_ON(group->id != id);
  189. kobject_get(group->devices_kobj);
  190. kobject_put(&group->kobj);
  191. return group;
  192. }
  193. EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
  194. /**
  195. * iommu_group_get_iommudata - retrieve iommu_data registered for a group
  196. * @group: the group
  197. *
  198. * iommu drivers can store data in the group for use when doing iommu
  199. * operations. This function provides a way to retrieve it. Caller
  200. * should hold a group reference.
  201. */
  202. void *iommu_group_get_iommudata(struct iommu_group *group)
  203. {
  204. return group->iommu_data;
  205. }
  206. EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
  207. /**
  208. * iommu_group_set_iommudata - set iommu_data for a group
  209. * @group: the group
  210. * @iommu_data: new data
  211. * @release: release function for iommu_data
  212. *
  213. * iommu drivers can store data in the group for use when doing iommu
  214. * operations. This function provides a way to set the data after
  215. * the group has been allocated. Caller should hold a group reference.
  216. */
  217. void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
  218. void (*release)(void *iommu_data))
  219. {
  220. group->iommu_data = iommu_data;
  221. group->iommu_data_release = release;
  222. }
  223. EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
  224. /**
  225. * iommu_group_set_name - set name for a group
  226. * @group: the group
  227. * @name: name
  228. *
  229. * Allow iommu driver to set a name for a group. When set it will
  230. * appear in a name attribute file under the group in sysfs.
  231. */
  232. int iommu_group_set_name(struct iommu_group *group, const char *name)
  233. {
  234. int ret;
  235. if (group->name) {
  236. iommu_group_remove_file(group, &iommu_group_attr_name);
  237. kfree(group->name);
  238. group->name = NULL;
  239. if (!name)
  240. return 0;
  241. }
  242. group->name = kstrdup(name, GFP_KERNEL);
  243. if (!group->name)
  244. return -ENOMEM;
  245. ret = iommu_group_create_file(group, &iommu_group_attr_name);
  246. if (ret) {
  247. kfree(group->name);
  248. group->name = NULL;
  249. return ret;
  250. }
  251. return 0;
  252. }
  253. EXPORT_SYMBOL_GPL(iommu_group_set_name);
  254. /**
  255. * iommu_group_add_device - add a device to an iommu group
  256. * @group: the group into which to add the device (reference should be held)
  257. * @dev: the device
  258. *
  259. * This function is called by an iommu driver to add a device into a
  260. * group. Adding a device increments the group reference count.
  261. */
  262. int iommu_group_add_device(struct iommu_group *group, struct device *dev)
  263. {
  264. int ret, i = 0;
  265. struct iommu_device *device;
  266. device = kzalloc(sizeof(*device), GFP_KERNEL);
  267. if (!device)
  268. return -ENOMEM;
  269. device->dev = dev;
  270. ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
  271. if (ret) {
  272. kfree(device);
  273. return ret;
  274. }
  275. device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
  276. rename:
  277. if (!device->name) {
  278. sysfs_remove_link(&dev->kobj, "iommu_group");
  279. kfree(device);
  280. return -ENOMEM;
  281. }
  282. ret = sysfs_create_link_nowarn(group->devices_kobj,
  283. &dev->kobj, device->name);
  284. if (ret) {
  285. kfree(device->name);
  286. if (ret == -EEXIST && i >= 0) {
  287. /*
  288. * Account for the slim chance of collision
  289. * and append an instance to the name.
  290. */
  291. device->name = kasprintf(GFP_KERNEL, "%s.%d",
  292. kobject_name(&dev->kobj), i++);
  293. goto rename;
  294. }
  295. sysfs_remove_link(&dev->kobj, "iommu_group");
  296. kfree(device);
  297. return ret;
  298. }
  299. kobject_get(group->devices_kobj);
  300. dev->iommu_group = group;
  301. mutex_lock(&group->mutex);
  302. list_add_tail(&device->list, &group->devices);
  303. mutex_unlock(&group->mutex);
  304. /* Notify any listeners about change to group. */
  305. blocking_notifier_call_chain(&group->notifier,
  306. IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
  307. return 0;
  308. }
  309. EXPORT_SYMBOL_GPL(iommu_group_add_device);
  310. /**
  311. * iommu_group_remove_device - remove a device from it's current group
  312. * @dev: device to be removed
  313. *
  314. * This function is called by an iommu driver to remove the device from
  315. * it's current group. This decrements the iommu group reference count.
  316. */
  317. void iommu_group_remove_device(struct device *dev)
  318. {
  319. struct iommu_group *group = dev->iommu_group;
  320. struct iommu_device *tmp_device, *device = NULL;
  321. /* Pre-notify listeners that a device is being removed. */
  322. blocking_notifier_call_chain(&group->notifier,
  323. IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
  324. mutex_lock(&group->mutex);
  325. list_for_each_entry(tmp_device, &group->devices, list) {
  326. if (tmp_device->dev == dev) {
  327. device = tmp_device;
  328. list_del(&device->list);
  329. break;
  330. }
  331. }
  332. mutex_unlock(&group->mutex);
  333. if (!device)
  334. return;
  335. sysfs_remove_link(group->devices_kobj, device->name);
  336. sysfs_remove_link(&dev->kobj, "iommu_group");
  337. kfree(device->name);
  338. kfree(device);
  339. dev->iommu_group = NULL;
  340. kobject_put(group->devices_kobj);
  341. }
  342. EXPORT_SYMBOL_GPL(iommu_group_remove_device);
  343. /**
  344. * iommu_group_for_each_dev - iterate over each device in the group
  345. * @group: the group
  346. * @data: caller opaque data to be passed to callback function
  347. * @fn: caller supplied callback function
  348. *
  349. * This function is called by group users to iterate over group devices.
  350. * Callers should hold a reference count to the group during callback.
  351. * The group->mutex is held across callbacks, which will block calls to
  352. * iommu_group_add/remove_device.
  353. */
  354. int iommu_group_for_each_dev(struct iommu_group *group, void *data,
  355. int (*fn)(struct device *, void *))
  356. {
  357. struct iommu_device *device;
  358. int ret = 0;
  359. mutex_lock(&group->mutex);
  360. list_for_each_entry(device, &group->devices, list) {
  361. ret = fn(device->dev, data);
  362. if (ret)
  363. break;
  364. }
  365. mutex_unlock(&group->mutex);
  366. return ret;
  367. }
  368. EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
  369. /**
  370. * iommu_group_get - Return the group for a device and increment reference
  371. * @dev: get the group that this device belongs to
  372. *
  373. * This function is called by iommu drivers and users to get the group
  374. * for the specified device. If found, the group is returned and the group
  375. * reference in incremented, else NULL.
  376. */
  377. struct iommu_group *iommu_group_get(struct device *dev)
  378. {
  379. struct iommu_group *group = dev->iommu_group;
  380. if (group)
  381. kobject_get(group->devices_kobj);
  382. return group;
  383. }
  384. EXPORT_SYMBOL_GPL(iommu_group_get);
  385. /**
  386. * iommu_group_put - Decrement group reference
  387. * @group: the group to use
  388. *
  389. * This function is called by iommu drivers and users to release the
  390. * iommu group. Once the reference count is zero, the group is released.
  391. */
  392. void iommu_group_put(struct iommu_group *group)
  393. {
  394. if (group)
  395. kobject_put(group->devices_kobj);
  396. }
  397. EXPORT_SYMBOL_GPL(iommu_group_put);
  398. /**
  399. * iommu_group_register_notifier - Register a notifier for group changes
  400. * @group: the group to watch
  401. * @nb: notifier block to signal
  402. *
  403. * This function allows iommu group users to track changes in a group.
  404. * See include/linux/iommu.h for actions sent via this notifier. Caller
  405. * should hold a reference to the group throughout notifier registration.
  406. */
  407. int iommu_group_register_notifier(struct iommu_group *group,
  408. struct notifier_block *nb)
  409. {
  410. return blocking_notifier_chain_register(&group->notifier, nb);
  411. }
  412. EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
  413. /**
  414. * iommu_group_unregister_notifier - Unregister a notifier
  415. * @group: the group to watch
  416. * @nb: notifier block to signal
  417. *
  418. * Unregister a previously registered group notifier block.
  419. */
  420. int iommu_group_unregister_notifier(struct iommu_group *group,
  421. struct notifier_block *nb)
  422. {
  423. return blocking_notifier_chain_unregister(&group->notifier, nb);
  424. }
  425. EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
  426. /**
  427. * iommu_group_id - Return ID for a group
  428. * @group: the group to ID
  429. *
  430. * Return the unique ID for the group matching the sysfs group number.
  431. */
  432. int iommu_group_id(struct iommu_group *group)
  433. {
  434. return group->id;
  435. }
  436. EXPORT_SYMBOL_GPL(iommu_group_id);
  437. static int add_iommu_group(struct device *dev, void *data)
  438. {
  439. struct iommu_ops *ops = data;
  440. if (!ops->add_device)
  441. return -ENODEV;
  442. WARN_ON(dev->iommu_group);
  443. ops->add_device(dev);
  444. return 0;
  445. }
  446. static int iommu_bus_notifier(struct notifier_block *nb,
  447. unsigned long action, void *data)
  448. {
  449. struct device *dev = data;
  450. struct iommu_ops *ops = dev->bus->iommu_ops;
  451. struct iommu_group *group;
  452. unsigned long group_action = 0;
  453. /*
  454. * ADD/DEL call into iommu driver ops if provided, which may
  455. * result in ADD/DEL notifiers to group->notifier
  456. */
  457. if (action == BUS_NOTIFY_ADD_DEVICE) {
  458. if (ops->add_device)
  459. return ops->add_device(dev);
  460. } else if (action == BUS_NOTIFY_DEL_DEVICE) {
  461. if (ops->remove_device && dev->iommu_group) {
  462. ops->remove_device(dev);
  463. return 0;
  464. }
  465. }
  466. /*
  467. * Remaining BUS_NOTIFYs get filtered and republished to the
  468. * group, if anyone is listening
  469. */
  470. group = iommu_group_get(dev);
  471. if (!group)
  472. return 0;
  473. switch (action) {
  474. case BUS_NOTIFY_BIND_DRIVER:
  475. group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
  476. break;
  477. case BUS_NOTIFY_BOUND_DRIVER:
  478. group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
  479. break;
  480. case BUS_NOTIFY_UNBIND_DRIVER:
  481. group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
  482. break;
  483. case BUS_NOTIFY_UNBOUND_DRIVER:
  484. group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
  485. break;
  486. }
  487. if (group_action)
  488. blocking_notifier_call_chain(&group->notifier,
  489. group_action, dev);
  490. iommu_group_put(group);
  491. return 0;
  492. }
  493. static struct notifier_block iommu_bus_nb = {
  494. .notifier_call = iommu_bus_notifier,
  495. };
  496. static void iommu_bus_init(struct bus_type *bus, struct iommu_ops *ops)
  497. {
  498. bus_register_notifier(bus, &iommu_bus_nb);
  499. bus_for_each_dev(bus, NULL, ops, add_iommu_group);
  500. }
  501. /**
  502. * bus_set_iommu - set iommu-callbacks for the bus
  503. * @bus: bus.
  504. * @ops: the callbacks provided by the iommu-driver
  505. *
  506. * This function is called by an iommu driver to set the iommu methods
  507. * used for a particular bus. Drivers for devices on that bus can use
  508. * the iommu-api after these ops are registered.
  509. * This special function is needed because IOMMUs are usually devices on
  510. * the bus itself, so the iommu drivers are not initialized when the bus
  511. * is set up. With this function the iommu-driver can set the iommu-ops
  512. * afterwards.
  513. */
  514. int bus_set_iommu(struct bus_type *bus, struct iommu_ops *ops)
  515. {
  516. if (bus->iommu_ops != NULL)
  517. return -EBUSY;
  518. bus->iommu_ops = ops;
  519. /* Do IOMMU specific setup for this bus-type */
  520. iommu_bus_init(bus, ops);
  521. return 0;
  522. }
  523. EXPORT_SYMBOL_GPL(bus_set_iommu);
  524. bool iommu_present(struct bus_type *bus)
  525. {
  526. return bus->iommu_ops != NULL;
  527. }
  528. EXPORT_SYMBOL_GPL(iommu_present);
  529. /**
  530. * iommu_set_fault_handler() - set a fault handler for an iommu domain
  531. * @domain: iommu domain
  532. * @handler: fault handler
  533. * @token: user data, will be passed back to the fault handler
  534. *
  535. * This function should be used by IOMMU users which want to be notified
  536. * whenever an IOMMU fault happens.
  537. *
  538. * The fault handler itself should return 0 on success, and an appropriate
  539. * error code otherwise.
  540. */
  541. void iommu_set_fault_handler(struct iommu_domain *domain,
  542. iommu_fault_handler_t handler,
  543. void *token)
  544. {
  545. BUG_ON(!domain);
  546. domain->handler = handler;
  547. domain->handler_token = token;
  548. }
  549. EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
  550. struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
  551. {
  552. struct iommu_domain *domain;
  553. int ret;
  554. if (bus == NULL || bus->iommu_ops == NULL)
  555. return NULL;
  556. domain = kzalloc(sizeof(*domain), GFP_KERNEL);
  557. if (!domain)
  558. return NULL;
  559. domain->ops = bus->iommu_ops;
  560. ret = domain->ops->domain_init(domain);
  561. if (ret)
  562. goto out_free;
  563. return domain;
  564. out_free:
  565. kfree(domain);
  566. return NULL;
  567. }
  568. EXPORT_SYMBOL_GPL(iommu_domain_alloc);
  569. void iommu_domain_free(struct iommu_domain *domain)
  570. {
  571. if (likely(domain->ops->domain_destroy != NULL))
  572. domain->ops->domain_destroy(domain);
  573. kfree(domain);
  574. }
  575. EXPORT_SYMBOL_GPL(iommu_domain_free);
  576. int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
  577. {
  578. if (unlikely(domain->ops->attach_dev == NULL))
  579. return -ENODEV;
  580. return domain->ops->attach_dev(domain, dev);
  581. }
  582. EXPORT_SYMBOL_GPL(iommu_attach_device);
  583. void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
  584. {
  585. if (unlikely(domain->ops->detach_dev == NULL))
  586. return;
  587. domain->ops->detach_dev(domain, dev);
  588. }
  589. EXPORT_SYMBOL_GPL(iommu_detach_device);
  590. /*
  591. * IOMMU groups are really the natrual working unit of the IOMMU, but
  592. * the IOMMU API works on domains and devices. Bridge that gap by
  593. * iterating over the devices in a group. Ideally we'd have a single
  594. * device which represents the requestor ID of the group, but we also
  595. * allow IOMMU drivers to create policy defined minimum sets, where
  596. * the physical hardware may be able to distiguish members, but we
  597. * wish to group them at a higher level (ex. untrusted multi-function
  598. * PCI devices). Thus we attach each device.
  599. */
  600. static int iommu_group_do_attach_device(struct device *dev, void *data)
  601. {
  602. struct iommu_domain *domain = data;
  603. return iommu_attach_device(domain, dev);
  604. }
  605. int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
  606. {
  607. return iommu_group_for_each_dev(group, domain,
  608. iommu_group_do_attach_device);
  609. }
  610. EXPORT_SYMBOL_GPL(iommu_attach_group);
  611. static int iommu_group_do_detach_device(struct device *dev, void *data)
  612. {
  613. struct iommu_domain *domain = data;
  614. iommu_detach_device(domain, dev);
  615. return 0;
  616. }
  617. void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
  618. {
  619. iommu_group_for_each_dev(group, domain, iommu_group_do_detach_device);
  620. }
  621. EXPORT_SYMBOL_GPL(iommu_detach_group);
  622. phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
  623. {
  624. if (unlikely(domain->ops->iova_to_phys == NULL))
  625. return 0;
  626. return domain->ops->iova_to_phys(domain, iova);
  627. }
  628. EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
  629. int iommu_domain_has_cap(struct iommu_domain *domain,
  630. unsigned long cap)
  631. {
  632. if (unlikely(domain->ops->domain_has_cap == NULL))
  633. return 0;
  634. return domain->ops->domain_has_cap(domain, cap);
  635. }
  636. EXPORT_SYMBOL_GPL(iommu_domain_has_cap);
  637. static size_t iommu_pgsize(struct iommu_domain *domain,
  638. unsigned long addr_merge, size_t size)
  639. {
  640. unsigned int pgsize_idx;
  641. size_t pgsize;
  642. /* Max page size that still fits into 'size' */
  643. pgsize_idx = __fls(size);
  644. /* need to consider alignment requirements ? */
  645. if (likely(addr_merge)) {
  646. /* Max page size allowed by address */
  647. unsigned int align_pgsize_idx = __ffs(addr_merge);
  648. pgsize_idx = min(pgsize_idx, align_pgsize_idx);
  649. }
  650. /* build a mask of acceptable page sizes */
  651. pgsize = (1UL << (pgsize_idx + 1)) - 1;
  652. /* throw away page sizes not supported by the hardware */
  653. pgsize &= domain->ops->pgsize_bitmap;
  654. /* make sure we're still sane */
  655. BUG_ON(!pgsize);
  656. /* pick the biggest page */
  657. pgsize_idx = __fls(pgsize);
  658. pgsize = 1UL << pgsize_idx;
  659. return pgsize;
  660. }
  661. int iommu_map(struct iommu_domain *domain, unsigned long iova,
  662. phys_addr_t paddr, size_t size, int prot)
  663. {
  664. unsigned long orig_iova = iova;
  665. unsigned int min_pagesz;
  666. size_t orig_size = size;
  667. int ret = 0;
  668. if (unlikely(domain->ops->unmap == NULL ||
  669. domain->ops->pgsize_bitmap == 0UL))
  670. return -ENODEV;
  671. /* find out the minimum page size supported */
  672. min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap);
  673. /*
  674. * both the virtual address and the physical one, as well as
  675. * the size of the mapping, must be aligned (at least) to the
  676. * size of the smallest page supported by the hardware
  677. */
  678. if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
  679. pr_err("unaligned: iova 0x%lx pa 0x%pa size 0x%zx min_pagesz 0x%x\n",
  680. iova, &paddr, size, min_pagesz);
  681. return -EINVAL;
  682. }
  683. pr_debug("map: iova 0x%lx pa 0x%pa size 0x%zx\n", iova, &paddr, size);
  684. while (size) {
  685. size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
  686. pr_debug("mapping: iova 0x%lx pa 0x%pa pgsize 0x%zx\n",
  687. iova, &paddr, pgsize);
  688. ret = domain->ops->map(domain, iova, paddr, pgsize, prot);
  689. if (ret)
  690. break;
  691. iova += pgsize;
  692. paddr += pgsize;
  693. size -= pgsize;
  694. }
  695. /* unroll mapping in case something went wrong */
  696. if (ret)
  697. iommu_unmap(domain, orig_iova, orig_size - size);
  698. return ret;
  699. }
  700. EXPORT_SYMBOL_GPL(iommu_map);
  701. size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size)
  702. {
  703. size_t unmapped_page, unmapped = 0;
  704. unsigned int min_pagesz;
  705. if (unlikely(domain->ops->unmap == NULL ||
  706. domain->ops->pgsize_bitmap == 0UL))
  707. return -ENODEV;
  708. /* find out the minimum page size supported */
  709. min_pagesz = 1 << __ffs(domain->ops->pgsize_bitmap);
  710. /*
  711. * The virtual address, as well as the size of the mapping, must be
  712. * aligned (at least) to the size of the smallest page supported
  713. * by the hardware
  714. */
  715. if (!IS_ALIGNED(iova | size, min_pagesz)) {
  716. pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
  717. iova, size, min_pagesz);
  718. return -EINVAL;
  719. }
  720. pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
  721. /*
  722. * Keep iterating until we either unmap 'size' bytes (or more)
  723. * or we hit an area that isn't mapped.
  724. */
  725. while (unmapped < size) {
  726. size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
  727. unmapped_page = domain->ops->unmap(domain, iova, pgsize);
  728. if (!unmapped_page)
  729. break;
  730. pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
  731. iova, unmapped_page);
  732. iova += unmapped_page;
  733. unmapped += unmapped_page;
  734. }
  735. return unmapped;
  736. }
  737. EXPORT_SYMBOL_GPL(iommu_unmap);
  738. int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
  739. phys_addr_t paddr, u64 size, int prot)
  740. {
  741. if (unlikely(domain->ops->domain_window_enable == NULL))
  742. return -ENODEV;
  743. return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
  744. prot);
  745. }
  746. EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
  747. void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
  748. {
  749. if (unlikely(domain->ops->domain_window_disable == NULL))
  750. return;
  751. return domain->ops->domain_window_disable(domain, wnd_nr);
  752. }
  753. EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
  754. static int __init iommu_init(void)
  755. {
  756. iommu_group_kset = kset_create_and_add("iommu_groups",
  757. NULL, kernel_kobj);
  758. ida_init(&iommu_group_ida);
  759. mutex_init(&iommu_group_mutex);
  760. BUG_ON(!iommu_group_kset);
  761. return 0;
  762. }
  763. arch_initcall(iommu_init);
  764. int iommu_domain_get_attr(struct iommu_domain *domain,
  765. enum iommu_attr attr, void *data)
  766. {
  767. struct iommu_domain_geometry *geometry;
  768. bool *paging;
  769. int ret = 0;
  770. u32 *count;
  771. switch (attr) {
  772. case DOMAIN_ATTR_GEOMETRY:
  773. geometry = data;
  774. *geometry = domain->geometry;
  775. break;
  776. case DOMAIN_ATTR_PAGING:
  777. paging = data;
  778. *paging = (domain->ops->pgsize_bitmap != 0UL);
  779. break;
  780. case DOMAIN_ATTR_WINDOWS:
  781. count = data;
  782. if (domain->ops->domain_get_windows != NULL)
  783. *count = domain->ops->domain_get_windows(domain);
  784. else
  785. ret = -ENODEV;
  786. break;
  787. default:
  788. if (!domain->ops->domain_get_attr)
  789. return -EINVAL;
  790. ret = domain->ops->domain_get_attr(domain, attr, data);
  791. }
  792. return ret;
  793. }
  794. EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
  795. int iommu_domain_set_attr(struct iommu_domain *domain,
  796. enum iommu_attr attr, void *data)
  797. {
  798. int ret = 0;
  799. u32 *count;
  800. switch (attr) {
  801. case DOMAIN_ATTR_WINDOWS:
  802. count = data;
  803. if (domain->ops->domain_set_windows != NULL)
  804. ret = domain->ops->domain_set_windows(domain, *count);
  805. else
  806. ret = -ENODEV;
  807. break;
  808. default:
  809. if (domain->ops->domain_set_attr == NULL)
  810. return -EINVAL;
  811. ret = domain->ops->domain_set_attr(domain, attr, data);
  812. }
  813. return ret;
  814. }
  815. EXPORT_SYMBOL_GPL(iommu_domain_set_attr);