of_iommu.c 2.7 KB

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