pdt.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* pdt.c: OF PROM device tree support code.
  3. *
  4. * Paul Mackerras August 1996.
  5. * Copyright (C) 1996-2005 Paul Mackerras.
  6. *
  7. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  8. * {engebret|bergner}@us.ibm.com
  9. *
  10. * Adapted for sparc by David S. Miller davem@davemloft.net
  11. * Adapted for multiple architectures by Andres Salomon <dilinger@queued.net>
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/errno.h>
  16. #include <linux/mutex.h>
  17. #include <linux/slab.h>
  18. #include <linux/of.h>
  19. #include <linux/of_pdt.h>
  20. static struct of_pdt_ops *of_pdt_prom_ops __initdata;
  21. #if defined(CONFIG_SPARC)
  22. unsigned int of_pdt_unique_id __initdata;
  23. #define of_pdt_incr_unique_id(p) do { \
  24. (p)->unique_id = of_pdt_unique_id++; \
  25. } while (0)
  26. static char * __init of_pdt_build_full_name(struct device_node *dp)
  27. {
  28. return build_path_component(dp);
  29. }
  30. #else /* CONFIG_SPARC */
  31. static inline void of_pdt_incr_unique_id(void *p) { }
  32. static inline void irq_trans_init(struct device_node *dp) { }
  33. static char * __init of_pdt_build_full_name(struct device_node *dp)
  34. {
  35. static int failsafe_id = 0; /* for generating unique names on failure */
  36. const char *name;
  37. char path[256];
  38. char *buf;
  39. int len;
  40. if (!of_pdt_prom_ops->pkg2path(dp->phandle, path, sizeof(path), &len)) {
  41. name = kbasename(path);
  42. buf = prom_early_alloc(strlen(name) + 1);
  43. strcpy(buf, name);
  44. return buf;
  45. }
  46. name = of_get_property(dp, "name", &len);
  47. buf = prom_early_alloc(len + 16);
  48. sprintf(buf, "%s@unknown%i", name, failsafe_id++);
  49. pr_err("%s: pkg2path failed; assigning %s\n", __func__, buf);
  50. return buf;
  51. }
  52. #endif /* !CONFIG_SPARC */
  53. static struct property * __init of_pdt_build_one_prop(phandle node, char *prev,
  54. char *special_name,
  55. void *special_val,
  56. int special_len)
  57. {
  58. static struct property *tmp = NULL;
  59. struct property *p;
  60. int err;
  61. if (tmp) {
  62. p = tmp;
  63. memset(p, 0, sizeof(*p) + 32);
  64. tmp = NULL;
  65. } else {
  66. p = prom_early_alloc(sizeof(struct property) + 32);
  67. of_pdt_incr_unique_id(p);
  68. }
  69. p->name = (char *) (p + 1);
  70. if (special_name) {
  71. strcpy(p->name, special_name);
  72. p->length = special_len;
  73. p->value = prom_early_alloc(special_len);
  74. memcpy(p->value, special_val, special_len);
  75. } else {
  76. err = of_pdt_prom_ops->nextprop(node, prev, p->name);
  77. if (err) {
  78. tmp = p;
  79. return NULL;
  80. }
  81. p->length = of_pdt_prom_ops->getproplen(node, p->name);
  82. if (p->length <= 0) {
  83. p->length = 0;
  84. } else {
  85. int len;
  86. p->value = prom_early_alloc(p->length + 1);
  87. len = of_pdt_prom_ops->getproperty(node, p->name,
  88. p->value, p->length);
  89. if (len <= 0)
  90. p->length = 0;
  91. ((unsigned char *)p->value)[p->length] = '\0';
  92. }
  93. }
  94. return p;
  95. }
  96. static struct property * __init of_pdt_build_prop_list(phandle node)
  97. {
  98. struct property *head, *tail;
  99. head = tail = of_pdt_build_one_prop(node, NULL,
  100. ".node", &node, sizeof(node));
  101. tail->next = of_pdt_build_one_prop(node, NULL, NULL, NULL, 0);
  102. tail = tail->next;
  103. while(tail) {
  104. tail->next = of_pdt_build_one_prop(node, tail->name,
  105. NULL, NULL, 0);
  106. tail = tail->next;
  107. }
  108. return head;
  109. }
  110. static char * __init of_pdt_get_one_property(phandle node, const char *name)
  111. {
  112. char *buf = "<NULL>";
  113. int len;
  114. len = of_pdt_prom_ops->getproplen(node, name);
  115. if (len > 0) {
  116. buf = prom_early_alloc(len);
  117. len = of_pdt_prom_ops->getproperty(node, name, buf, len);
  118. }
  119. return buf;
  120. }
  121. static struct device_node * __init of_pdt_create_node(phandle node,
  122. struct device_node *parent)
  123. {
  124. struct device_node *dp;
  125. if (!node)
  126. return NULL;
  127. dp = prom_early_alloc(sizeof(*dp));
  128. of_node_init(dp);
  129. of_pdt_incr_unique_id(dp);
  130. dp->parent = parent;
  131. dp->name = of_pdt_get_one_property(node, "name");
  132. dp->phandle = node;
  133. dp->properties = of_pdt_build_prop_list(node);
  134. dp->full_name = of_pdt_build_full_name(dp);
  135. irq_trans_init(dp);
  136. return dp;
  137. }
  138. static struct device_node * __init of_pdt_build_tree(struct device_node *parent,
  139. phandle node)
  140. {
  141. struct device_node *ret = NULL, *prev_sibling = NULL;
  142. struct device_node *dp;
  143. while (1) {
  144. dp = of_pdt_create_node(node, parent);
  145. if (!dp)
  146. break;
  147. if (prev_sibling)
  148. prev_sibling->sibling = dp;
  149. if (!ret)
  150. ret = dp;
  151. prev_sibling = dp;
  152. dp->child = of_pdt_build_tree(dp, of_pdt_prom_ops->getchild(node));
  153. node = of_pdt_prom_ops->getsibling(node);
  154. }
  155. return ret;
  156. }
  157. static void * __init kernel_tree_alloc(u64 size, u64 align)
  158. {
  159. return prom_early_alloc(size);
  160. }
  161. void __init of_pdt_build_devicetree(phandle root_node, struct of_pdt_ops *ops)
  162. {
  163. BUG_ON(!ops);
  164. of_pdt_prom_ops = ops;
  165. of_root = of_pdt_create_node(root_node, NULL);
  166. of_root->full_name = "/";
  167. of_root->child = of_pdt_build_tree(of_root,
  168. of_pdt_prom_ops->getchild(of_root->phandle));
  169. /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
  170. of_alias_scan(kernel_tree_alloc);
  171. }