of_iommu.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/limits.h>
  21. #include <linux/of.h>
  22. #include <linux/of_iommu.h>
  23. /**
  24. * of_get_dma_window - Parse *dma-window property and returns 0 if found.
  25. *
  26. * @dn: device node
  27. * @prefix: prefix for property name if any
  28. * @index: index to start to parse
  29. * @busno: Returns busno if supported. Otherwise pass NULL
  30. * @addr: Returns address that DMA starts
  31. * @size: Returns the range that DMA can handle
  32. *
  33. * This supports different formats flexibly. "prefix" can be
  34. * configured if any. "busno" and "index" are optionally
  35. * specified. Set 0(or NULL) if not used.
  36. */
  37. int of_get_dma_window(struct device_node *dn, const char *prefix, int index,
  38. unsigned long *busno, dma_addr_t *addr, size_t *size)
  39. {
  40. const __be32 *dma_window, *end;
  41. int bytes, cur_index = 0;
  42. char propname[NAME_MAX], addrname[NAME_MAX], sizename[NAME_MAX];
  43. if (!dn || !addr || !size)
  44. return -EINVAL;
  45. if (!prefix)
  46. prefix = "";
  47. snprintf(propname, sizeof(propname), "%sdma-window", prefix);
  48. snprintf(addrname, sizeof(addrname), "%s#dma-address-cells", prefix);
  49. snprintf(sizename, sizeof(sizename), "%s#dma-size-cells", prefix);
  50. dma_window = of_get_property(dn, propname, &bytes);
  51. if (!dma_window)
  52. return -ENODEV;
  53. end = dma_window + bytes / sizeof(*dma_window);
  54. while (dma_window < end) {
  55. u32 cells;
  56. const void *prop;
  57. /* busno is one cell if supported */
  58. if (busno)
  59. *busno = be32_to_cpup(dma_window++);
  60. prop = of_get_property(dn, addrname, NULL);
  61. if (!prop)
  62. prop = of_get_property(dn, "#address-cells", NULL);
  63. cells = prop ? be32_to_cpup(prop) : of_n_addr_cells(dn);
  64. if (!cells)
  65. return -EINVAL;
  66. *addr = of_read_number(dma_window, cells);
  67. dma_window += cells;
  68. prop = of_get_property(dn, sizename, NULL);
  69. cells = prop ? be32_to_cpup(prop) : of_n_size_cells(dn);
  70. if (!cells)
  71. return -EINVAL;
  72. *size = of_read_number(dma_window, cells);
  73. dma_window += cells;
  74. if (cur_index++ == index)
  75. break;
  76. }
  77. return 0;
  78. }
  79. EXPORT_SYMBOL_GPL(of_get_dma_window);