of_iommu.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * OF helpers for IOMMU
  3. *
  4. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #include <linux/export.h>
  20. #include <linux/iommu.h>
  21. #include <linux/limits.h>
  22. #include <linux/of.h>
  23. #include <linux/of_iommu.h>
  24. #include <linux/slab.h>
  25. static const struct of_device_id __iommu_of_table_sentinel
  26. __used __section(__iommu_of_table_end);
  27. /**
  28. * of_get_dma_window - Parse *dma-window property and returns 0 if found.
  29. *
  30. * @dn: device node
  31. * @prefix: prefix for property name if any
  32. * @index: index to start to parse
  33. * @busno: Returns busno if supported. Otherwise pass NULL
  34. * @addr: Returns address that DMA starts
  35. * @size: Returns the range that DMA can handle
  36. *
  37. * This supports different formats flexibly. "prefix" can be
  38. * configured if any. "busno" and "index" are optionally
  39. * specified. Set 0(or NULL) if not used.
  40. */
  41. int of_get_dma_window(struct device_node *dn, const char *prefix, int index,
  42. unsigned long *busno, dma_addr_t *addr, size_t *size)
  43. {
  44. const __be32 *dma_window, *end;
  45. int bytes, cur_index = 0;
  46. char propname[NAME_MAX], addrname[NAME_MAX], sizename[NAME_MAX];
  47. if (!dn || !addr || !size)
  48. return -EINVAL;
  49. if (!prefix)
  50. prefix = "";
  51. snprintf(propname, sizeof(propname), "%sdma-window", prefix);
  52. snprintf(addrname, sizeof(addrname), "%s#dma-address-cells", prefix);
  53. snprintf(sizename, sizeof(sizename), "%s#dma-size-cells", prefix);
  54. dma_window = of_get_property(dn, propname, &bytes);
  55. if (!dma_window)
  56. return -ENODEV;
  57. end = dma_window + bytes / sizeof(*dma_window);
  58. while (dma_window < end) {
  59. u32 cells;
  60. const void *prop;
  61. /* busno is one cell if supported */
  62. if (busno)
  63. *busno = be32_to_cpup(dma_window++);
  64. prop = of_get_property(dn, addrname, NULL);
  65. if (!prop)
  66. prop = of_get_property(dn, "#address-cells", NULL);
  67. cells = prop ? be32_to_cpup(prop) : of_n_addr_cells(dn);
  68. if (!cells)
  69. return -EINVAL;
  70. *addr = of_read_number(dma_window, cells);
  71. dma_window += cells;
  72. prop = of_get_property(dn, sizename, NULL);
  73. cells = prop ? be32_to_cpup(prop) : of_n_size_cells(dn);
  74. if (!cells)
  75. return -EINVAL;
  76. *size = of_read_number(dma_window, cells);
  77. dma_window += cells;
  78. if (cur_index++ == index)
  79. break;
  80. }
  81. return 0;
  82. }
  83. EXPORT_SYMBOL_GPL(of_get_dma_window);
  84. struct of_iommu_node {
  85. struct list_head list;
  86. struct device_node *np;
  87. struct iommu_ops *ops;
  88. };
  89. static LIST_HEAD(of_iommu_list);
  90. static DEFINE_SPINLOCK(of_iommu_lock);
  91. void of_iommu_set_ops(struct device_node *np, struct iommu_ops *ops)
  92. {
  93. struct of_iommu_node *iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
  94. if (WARN_ON(!iommu))
  95. return;
  96. of_node_get(np);
  97. INIT_LIST_HEAD(&iommu->list);
  98. iommu->np = np;
  99. iommu->ops = ops;
  100. spin_lock(&of_iommu_lock);
  101. list_add_tail(&iommu->list, &of_iommu_list);
  102. spin_unlock(&of_iommu_lock);
  103. }
  104. struct iommu_ops *of_iommu_get_ops(struct device_node *np)
  105. {
  106. struct of_iommu_node *node;
  107. struct iommu_ops *ops = NULL;
  108. spin_lock(&of_iommu_lock);
  109. list_for_each_entry(node, &of_iommu_list, list)
  110. if (node->np == np) {
  111. ops = node->ops;
  112. break;
  113. }
  114. spin_unlock(&of_iommu_lock);
  115. return ops;
  116. }
  117. struct iommu_ops *of_iommu_configure(struct device *dev,
  118. struct device_node *master_np)
  119. {
  120. struct of_phandle_args iommu_spec;
  121. struct device_node *np;
  122. struct iommu_ops *ops = NULL;
  123. int idx = 0;
  124. /*
  125. * We can't do much for PCI devices without knowing how
  126. * device IDs are wired up from the PCI bus to the IOMMU.
  127. */
  128. if (dev_is_pci(dev))
  129. return NULL;
  130. /*
  131. * We don't currently walk up the tree looking for a parent IOMMU.
  132. * See the `Notes:' section of
  133. * Documentation/devicetree/bindings/iommu/iommu.txt
  134. */
  135. while (!of_parse_phandle_with_args(master_np, "iommus",
  136. "#iommu-cells", idx,
  137. &iommu_spec)) {
  138. np = iommu_spec.np;
  139. ops = of_iommu_get_ops(np);
  140. if (!ops || !ops->of_xlate || ops->of_xlate(dev, &iommu_spec))
  141. goto err_put_node;
  142. of_node_put(np);
  143. idx++;
  144. }
  145. return ops;
  146. err_put_node:
  147. of_node_put(np);
  148. return NULL;
  149. }
  150. void __init of_iommu_init(void)
  151. {
  152. struct device_node *np;
  153. const struct of_device_id *match, *matches = &__iommu_of_table;
  154. for_each_matching_node_and_match(np, matches, &match) {
  155. const of_iommu_init_fn init_fn = match->data;
  156. if (init_fn(np))
  157. pr_err("Failed to initialise IOMMU %s\n",
  158. of_node_full_name(np));
  159. }
  160. }