of_iommu.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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_iommu.h>
  14. #include <linux/of_pci.h>
  15. #include <linux/pci.h>
  16. #include <linux/slab.h>
  17. #include <linux/fsl/mc.h>
  18. #define NO_IOMMU 1
  19. /**
  20. * of_get_dma_window - Parse *dma-window property and returns 0 if found.
  21. *
  22. * @dn: device node
  23. * @prefix: prefix for property name if any
  24. * @index: index to start to parse
  25. * @busno: Returns busno if supported. Otherwise pass NULL
  26. * @addr: Returns address that DMA starts
  27. * @size: Returns the range that DMA can handle
  28. *
  29. * This supports different formats flexibly. "prefix" can be
  30. * configured if any. "busno" and "index" are optionally
  31. * specified. Set 0(or NULL) if not used.
  32. */
  33. int of_get_dma_window(struct device_node *dn, const char *prefix, int index,
  34. unsigned long *busno, dma_addr_t *addr, size_t *size)
  35. {
  36. const __be32 *dma_window, *end;
  37. int bytes, cur_index = 0;
  38. char propname[NAME_MAX], addrname[NAME_MAX], sizename[NAME_MAX];
  39. if (!dn || !addr || !size)
  40. return -EINVAL;
  41. if (!prefix)
  42. prefix = "";
  43. snprintf(propname, sizeof(propname), "%sdma-window", prefix);
  44. snprintf(addrname, sizeof(addrname), "%s#dma-address-cells", prefix);
  45. snprintf(sizename, sizeof(sizename), "%s#dma-size-cells", prefix);
  46. dma_window = of_get_property(dn, propname, &bytes);
  47. if (!dma_window)
  48. return -ENODEV;
  49. end = dma_window + bytes / sizeof(*dma_window);
  50. while (dma_window < end) {
  51. u32 cells;
  52. const void *prop;
  53. /* busno is one cell if supported */
  54. if (busno)
  55. *busno = be32_to_cpup(dma_window++);
  56. prop = of_get_property(dn, addrname, NULL);
  57. if (!prop)
  58. prop = of_get_property(dn, "#address-cells", NULL);
  59. cells = prop ? be32_to_cpup(prop) : of_n_addr_cells(dn);
  60. if (!cells)
  61. return -EINVAL;
  62. *addr = of_read_number(dma_window, cells);
  63. dma_window += cells;
  64. prop = of_get_property(dn, sizename, NULL);
  65. cells = prop ? be32_to_cpup(prop) : of_n_size_cells(dn);
  66. if (!cells)
  67. return -EINVAL;
  68. *size = of_read_number(dma_window, cells);
  69. dma_window += cells;
  70. if (cur_index++ == index)
  71. break;
  72. }
  73. return 0;
  74. }
  75. EXPORT_SYMBOL_GPL(of_get_dma_window);
  76. static int of_iommu_xlate(struct device *dev,
  77. struct of_phandle_args *iommu_spec)
  78. {
  79. const struct iommu_ops *ops;
  80. struct fwnode_handle *fwnode = &iommu_spec->np->fwnode;
  81. int ret;
  82. ops = iommu_ops_from_fwnode(fwnode);
  83. if ((ops && !ops->of_xlate) ||
  84. !of_device_is_available(iommu_spec->np))
  85. return NO_IOMMU;
  86. ret = iommu_fwspec_init(dev, &iommu_spec->np->fwnode, ops);
  87. if (ret)
  88. return ret;
  89. /*
  90. * The otherwise-empty fwspec handily serves to indicate the specific
  91. * IOMMU device we're waiting for, which will be useful if we ever get
  92. * a proper probe-ordering dependency mechanism in future.
  93. */
  94. if (!ops)
  95. return driver_deferred_probe_check_state(dev);
  96. if (!try_module_get(ops->owner))
  97. return -ENODEV;
  98. ret = ops->of_xlate(dev, iommu_spec);
  99. module_put(ops->owner);
  100. return ret;
  101. }
  102. static int of_iommu_configure_dev_id(struct device_node *master_np,
  103. struct device *dev,
  104. const u32 *id)
  105. {
  106. struct of_phandle_args iommu_spec = { .args_count = 1 };
  107. int err;
  108. err = of_map_id(master_np, *id, "iommu-map",
  109. "iommu-map-mask", &iommu_spec.np,
  110. iommu_spec.args);
  111. if (err)
  112. return err == -ENODEV ? NO_IOMMU : err;
  113. err = of_iommu_xlate(dev, &iommu_spec);
  114. of_node_put(iommu_spec.np);
  115. return err;
  116. }
  117. static int of_iommu_configure_dev(struct device_node *master_np,
  118. struct device *dev)
  119. {
  120. struct of_phandle_args iommu_spec;
  121. int err = NO_IOMMU, idx = 0;
  122. while (!of_parse_phandle_with_args(master_np, "iommus",
  123. "#iommu-cells",
  124. idx, &iommu_spec)) {
  125. err = of_iommu_xlate(dev, &iommu_spec);
  126. of_node_put(iommu_spec.np);
  127. idx++;
  128. if (err)
  129. break;
  130. }
  131. return err;
  132. }
  133. struct of_pci_iommu_alias_info {
  134. struct device *dev;
  135. struct device_node *np;
  136. };
  137. static int of_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
  138. {
  139. struct of_pci_iommu_alias_info *info = data;
  140. u32 input_id = alias;
  141. return of_iommu_configure_dev_id(info->np, info->dev, &input_id);
  142. }
  143. static int of_iommu_configure_device(struct device_node *master_np,
  144. struct device *dev, const u32 *id)
  145. {
  146. return (id) ? of_iommu_configure_dev_id(master_np, dev, id) :
  147. of_iommu_configure_dev(master_np, dev);
  148. }
  149. const struct iommu_ops *of_iommu_configure(struct device *dev,
  150. struct device_node *master_np,
  151. const u32 *id)
  152. {
  153. const struct iommu_ops *ops = NULL;
  154. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  155. int err = NO_IOMMU;
  156. if (!master_np)
  157. return NULL;
  158. if (fwspec) {
  159. if (fwspec->ops)
  160. return fwspec->ops;
  161. /* In the deferred case, start again from scratch */
  162. iommu_fwspec_free(dev);
  163. }
  164. /*
  165. * We don't currently walk up the tree looking for a parent IOMMU.
  166. * See the `Notes:' section of
  167. * Documentation/devicetree/bindings/iommu/iommu.txt
  168. */
  169. if (dev_is_pci(dev)) {
  170. struct of_pci_iommu_alias_info info = {
  171. .dev = dev,
  172. .np = master_np,
  173. };
  174. pci_request_acs();
  175. err = pci_for_each_dma_alias(to_pci_dev(dev),
  176. of_pci_iommu_init, &info);
  177. } else {
  178. err = of_iommu_configure_device(master_np, dev, id);
  179. fwspec = dev_iommu_fwspec_get(dev);
  180. if (!err && fwspec)
  181. of_property_read_u32(master_np, "pasid-num-bits",
  182. &fwspec->num_pasid_bits);
  183. }
  184. /*
  185. * Two success conditions can be represented by non-negative err here:
  186. * >0 : there is no IOMMU, or one was unavailable for non-fatal reasons
  187. * 0 : we found an IOMMU, and dev->fwspec is initialised appropriately
  188. * <0 : any actual error
  189. */
  190. if (!err) {
  191. /* The fwspec pointer changed, read it again */
  192. fwspec = dev_iommu_fwspec_get(dev);
  193. ops = fwspec->ops;
  194. }
  195. /*
  196. * If we have reason to believe the IOMMU driver missed the initial
  197. * probe for dev, replay it to get things in order.
  198. */
  199. if (!err && dev->bus && !device_iommu_mapped(dev))
  200. err = iommu_probe_device(dev);
  201. /* Ignore all other errors apart from EPROBE_DEFER */
  202. if (err == -EPROBE_DEFER) {
  203. ops = ERR_PTR(err);
  204. } else if (err < 0) {
  205. dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
  206. ops = NULL;
  207. }
  208. return ops;
  209. }