iommu.c 25 KB

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