of_iommu.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. INIT_LIST_HEAD(&iommu->list);
  97. iommu->np = np;
  98. iommu->ops = ops;
  99. spin_lock(&of_iommu_lock);
  100. list_add_tail(&iommu->list, &of_iommu_list);
  101. spin_unlock(&of_iommu_lock);
  102. }
  103. struct iommu_ops *of_iommu_get_ops(struct device_node *np)
  104. {
  105. struct of_iommu_node *node;
  106. struct iommu_ops *ops = NULL;
  107. spin_lock(&of_iommu_lock);
  108. list_for_each_entry(node, &of_iommu_list, list)
  109. if (node->np == np) {
  110. ops = node->ops;
  111. break;
  112. }
  113. spin_unlock(&of_iommu_lock);
  114. return ops;
  115. }
  116. struct iommu_ops *of_iommu_configure(struct device *dev,
  117. struct device_node *master_np)
  118. {
  119. struct of_phandle_args iommu_spec;
  120. struct device_node *np;
  121. struct iommu_ops *ops = NULL;
  122. int idx = 0;
  123. /*
  124. * We can't do much for PCI devices without knowing how
  125. * device IDs are wired up from the PCI bus to the IOMMU.
  126. */
  127. if (dev_is_pci(dev))
  128. return NULL;
  129. /*
  130. * We don't currently walk up the tree looking for a parent IOMMU.
  131. * See the `Notes:' section of
  132. * Documentation/devicetree/bindings/iommu/iommu.txt
  133. */
  134. while (!of_parse_phandle_with_args(master_np, "iommus",
  135. "#iommu-cells", idx,
  136. &iommu_spec)) {
  137. np = iommu_spec.np;
  138. ops = of_iommu_get_ops(np);
  139. if (!ops || !ops->of_xlate || ops->of_xlate(dev, &iommu_spec))
  140. goto err_put_node;
  141. of_node_put(np);
  142. idx++;
  143. }
  144. return ops;
  145. err_put_node:
  146. of_node_put(np);
  147. return NULL;
  148. }
  149. void __init of_iommu_init(void)
  150. {
  151. struct device_node *np;
  152. const struct of_device_id *match, *matches = &__iommu_of_table;
  153. for_each_matching_node_and_match(np, matches, &match) {
  154. const of_iommu_init_fn init_fn = match->data;
  155. if (init_fn(np))
  156. pr_err("Failed to initialise IOMMU %s\n",
  157. of_node_full_name(np));
  158. }
  159. }