of_iommu.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * OF helpers for IOMMU
  4. *
  5. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  6. */
  7. #include <linux/export.h>
  8. #include <linux/iommu.h>
  9. #include <linux/limits.h>
  10. #include <linux/module.h>
  11. #include <linux/msi.h>
  12. #include <linux/of.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of_iommu.h>
  15. #include <linux/of_pci.h>
  16. #include <linux/pci.h>
  17. #include <linux/slab.h>
  18. #include <linux/fsl/mc.h>
  19. #define NO_IOMMU 1
  20. static int of_iommu_xlate(struct device *dev,
  21. struct of_phandle_args *iommu_spec)
  22. {
  23. const struct iommu_ops *ops;
  24. struct fwnode_handle *fwnode = &iommu_spec->np->fwnode;
  25. int ret;
  26. ops = iommu_ops_from_fwnode(fwnode);
  27. if ((ops && !ops->of_xlate) ||
  28. !of_device_is_available(iommu_spec->np))
  29. return NO_IOMMU;
  30. ret = iommu_fwspec_init(dev, &iommu_spec->np->fwnode, ops);
  31. if (ret)
  32. return ret;
  33. /*
  34. * The otherwise-empty fwspec handily serves to indicate the specific
  35. * IOMMU device we're waiting for, which will be useful if we ever get
  36. * a proper probe-ordering dependency mechanism in future.
  37. */
  38. if (!ops)
  39. return driver_deferred_probe_check_state(dev);
  40. if (!try_module_get(ops->owner))
  41. return -ENODEV;
  42. ret = ops->of_xlate(dev, iommu_spec);
  43. module_put(ops->owner);
  44. return ret;
  45. }
  46. static int of_iommu_configure_dev_id(struct device_node *master_np,
  47. struct device *dev,
  48. const u32 *id)
  49. {
  50. struct of_phandle_args iommu_spec = { .args_count = 1 };
  51. int err;
  52. err = of_map_id(master_np, *id, "iommu-map",
  53. "iommu-map-mask", &iommu_spec.np,
  54. iommu_spec.args);
  55. if (err)
  56. return err == -ENODEV ? NO_IOMMU : err;
  57. err = of_iommu_xlate(dev, &iommu_spec);
  58. of_node_put(iommu_spec.np);
  59. return err;
  60. }
  61. static int of_iommu_configure_dev(struct device_node *master_np,
  62. struct device *dev)
  63. {
  64. struct of_phandle_args iommu_spec;
  65. int err = NO_IOMMU, idx = 0;
  66. while (!of_parse_phandle_with_args(master_np, "iommus",
  67. "#iommu-cells",
  68. idx, &iommu_spec)) {
  69. err = of_iommu_xlate(dev, &iommu_spec);
  70. of_node_put(iommu_spec.np);
  71. idx++;
  72. if (err)
  73. break;
  74. }
  75. return err;
  76. }
  77. struct of_pci_iommu_alias_info {
  78. struct device *dev;
  79. struct device_node *np;
  80. };
  81. static int of_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
  82. {
  83. struct of_pci_iommu_alias_info *info = data;
  84. u32 input_id = alias;
  85. return of_iommu_configure_dev_id(info->np, info->dev, &input_id);
  86. }
  87. static int of_iommu_configure_device(struct device_node *master_np,
  88. struct device *dev, const u32 *id)
  89. {
  90. return (id) ? of_iommu_configure_dev_id(master_np, dev, id) :
  91. of_iommu_configure_dev(master_np, dev);
  92. }
  93. const struct iommu_ops *of_iommu_configure(struct device *dev,
  94. struct device_node *master_np,
  95. const u32 *id)
  96. {
  97. const struct iommu_ops *ops = NULL;
  98. struct iommu_fwspec *fwspec;
  99. int err = NO_IOMMU;
  100. if (!master_np)
  101. return NULL;
  102. /* Serialise to make dev->iommu stable under our potential fwspec */
  103. mutex_lock(&iommu_probe_device_lock);
  104. fwspec = dev_iommu_fwspec_get(dev);
  105. if (fwspec) {
  106. if (fwspec->ops) {
  107. mutex_unlock(&iommu_probe_device_lock);
  108. return fwspec->ops;
  109. }
  110. /* In the deferred case, start again from scratch */
  111. iommu_fwspec_free(dev);
  112. }
  113. /*
  114. * We don't currently walk up the tree looking for a parent IOMMU.
  115. * See the `Notes:' section of
  116. * Documentation/devicetree/bindings/iommu/iommu.txt
  117. */
  118. if (dev_is_pci(dev)) {
  119. struct of_pci_iommu_alias_info info = {
  120. .dev = dev,
  121. .np = master_np,
  122. };
  123. pci_request_acs();
  124. err = pci_for_each_dma_alias(to_pci_dev(dev),
  125. of_pci_iommu_init, &info);
  126. } else {
  127. err = of_iommu_configure_device(master_np, dev, id);
  128. }
  129. /*
  130. * Two success conditions can be represented by non-negative err here:
  131. * >0 : there is no IOMMU, or one was unavailable for non-fatal reasons
  132. * 0 : we found an IOMMU, and dev->fwspec is initialised appropriately
  133. * <0 : any actual error
  134. */
  135. if (!err) {
  136. /* The fwspec pointer changed, read it again */
  137. fwspec = dev_iommu_fwspec_get(dev);
  138. ops = fwspec->ops;
  139. }
  140. mutex_unlock(&iommu_probe_device_lock);
  141. /*
  142. * If we have reason to believe the IOMMU driver missed the initial
  143. * probe for dev, replay it to get things in order.
  144. */
  145. if (!err && dev->bus && !device_iommu_mapped(dev))
  146. err = iommu_probe_device(dev);
  147. /* Ignore all other errors apart from EPROBE_DEFER */
  148. if (err == -EPROBE_DEFER) {
  149. ops = ERR_PTR(err);
  150. } else if (err < 0) {
  151. dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
  152. ops = NULL;
  153. }
  154. return ops;
  155. }
  156. static enum iommu_resv_type __maybe_unused
  157. iommu_resv_region_get_type(struct device *dev,
  158. struct resource *phys,
  159. phys_addr_t start, size_t length)
  160. {
  161. phys_addr_t end = start + length - 1;
  162. /*
  163. * IOMMU regions without an associated physical region cannot be
  164. * mapped and are simply reservations.
  165. */
  166. if (phys->start >= phys->end)
  167. return IOMMU_RESV_RESERVED;
  168. /* may be IOMMU_RESV_DIRECT_RELAXABLE for certain cases */
  169. if (start == phys->start && end == phys->end)
  170. return IOMMU_RESV_DIRECT;
  171. dev_warn(dev, "treating non-direct mapping [%pr] -> [%pap-%pap] as reservation\n", &phys,
  172. &start, &end);
  173. return IOMMU_RESV_RESERVED;
  174. }
  175. /**
  176. * of_iommu_get_resv_regions - reserved region driver helper for device tree
  177. * @dev: device for which to get reserved regions
  178. * @list: reserved region list
  179. *
  180. * IOMMU drivers can use this to implement their .get_resv_regions() callback
  181. * for memory regions attached to a device tree node. See the reserved-memory
  182. * device tree bindings on how to use these:
  183. *
  184. * Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt
  185. */
  186. void of_iommu_get_resv_regions(struct device *dev, struct list_head *list)
  187. {
  188. #if IS_ENABLED(CONFIG_OF_ADDRESS)
  189. struct of_phandle_iterator it;
  190. int err;
  191. of_for_each_phandle(&it, err, dev->of_node, "memory-region", NULL, 0) {
  192. const __be32 *maps, *end;
  193. struct resource phys;
  194. int size;
  195. memset(&phys, 0, sizeof(phys));
  196. /*
  197. * The "reg" property is optional and can be omitted by reserved-memory regions
  198. * that represent reservations in the IOVA space, which are regions that should
  199. * not be mapped.
  200. */
  201. if (of_find_property(it.node, "reg", NULL)) {
  202. err = of_address_to_resource(it.node, 0, &phys);
  203. if (err < 0) {
  204. dev_err(dev, "failed to parse memory region %pOF: %d\n",
  205. it.node, err);
  206. continue;
  207. }
  208. }
  209. maps = of_get_property(it.node, "iommu-addresses", &size);
  210. if (!maps)
  211. continue;
  212. end = maps + size / sizeof(__be32);
  213. while (maps < end) {
  214. struct device_node *np;
  215. u32 phandle;
  216. phandle = be32_to_cpup(maps++);
  217. np = of_find_node_by_phandle(phandle);
  218. if (np == dev->of_node) {
  219. int prot = IOMMU_READ | IOMMU_WRITE;
  220. struct iommu_resv_region *region;
  221. enum iommu_resv_type type;
  222. phys_addr_t iova;
  223. size_t length;
  224. maps = of_translate_dma_region(np, maps, &iova, &length);
  225. type = iommu_resv_region_get_type(dev, &phys, iova, length);
  226. region = iommu_alloc_resv_region(iova, length, prot, type,
  227. GFP_KERNEL);
  228. if (region)
  229. list_add_tail(&region->list, list);
  230. }
  231. }
  232. }
  233. #endif
  234. }
  235. EXPORT_SYMBOL(of_iommu_get_resv_regions);