pdt.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. #include "of_private.h"
  21. static struct of_pdt_ops *of_pdt_prom_ops __initdata;
  22. #if defined(CONFIG_SPARC)
  23. unsigned int of_pdt_unique_id __initdata;
  24. #define of_pdt_incr_unique_id(p) do { \
  25. (p)->unique_id = of_pdt_unique_id++; \
  26. } while (0)
  27. static char * __init of_pdt_build_full_name(struct device_node *dp)
  28. {
  29. return build_path_component(dp);
  30. }
  31. #else /* CONFIG_SPARC */
  32. static inline void of_pdt_incr_unique_id(void *p) { }
  33. static inline void irq_trans_init(struct device_node *dp) { }
  34. static char * __init of_pdt_build_full_name(struct device_node *dp)
  35. {
  36. static int failsafe_id = 0; /* for generating unique names on failure */
  37. const char *name;
  38. char path[256];
  39. char *buf;
  40. int len;
  41. if (!of_pdt_prom_ops->pkg2path(dp->phandle, path, sizeof(path), &len)) {
  42. name = kbasename(path);
  43. buf = prom_early_alloc(strlen(name) + 1);
  44. strcpy(buf, name);
  45. return buf;
  46. }
  47. name = of_get_property(dp, "name", &len);
  48. buf = prom_early_alloc(len + 16);
  49. sprintf(buf, "%s@unknown%i", name, failsafe_id++);
  50. pr_err("%s: pkg2path failed; assigning %s\n", __func__, buf);
  51. return buf;
  52. }
  53. #endif /* !CONFIG_SPARC */
  54. static struct property * __init of_pdt_build_one_prop(phandle node, char *prev,
  55. char *special_name,
  56. void *special_val,
  57. int special_len)
  58. {
  59. static struct property *tmp = NULL;
  60. struct property *p;
  61. int err;
  62. if (tmp) {
  63. p = tmp;
  64. memset(p, 0, sizeof(*p) + 32);
  65. tmp = NULL;
  66. } else {
  67. p = prom_early_alloc(sizeof(struct property) + 32);
  68. of_pdt_incr_unique_id(p);
  69. }
  70. p->name = (char *) (p + 1);
  71. if (special_name) {
  72. strcpy(p->name, special_name);
  73. p->length = special_len;
  74. p->value = prom_early_alloc(special_len);
  75. memcpy(p->value, special_val, special_len);
  76. } else {
  77. err = of_pdt_prom_ops->nextprop(node, prev, p->name);
  78. if (err) {
  79. tmp = p;
  80. return NULL;
  81. }
  82. p->length = of_pdt_prom_ops->getproplen(node, p->name);
  83. if (p->length <= 0) {
  84. p->length = 0;
  85. } else {
  86. int len;
  87. p->value = prom_early_alloc(p->length + 1);
  88. len = of_pdt_prom_ops->getproperty(node, p->name,
  89. p->value, p->length);
  90. if (len <= 0)
  91. p->length = 0;
  92. ((unsigned char *)p->value)[p->length] = '\0';
  93. }
  94. }
  95. return p;
  96. }
  97. static struct property * __init of_pdt_build_prop_list(phandle node)
  98. {
  99. struct property *head, *tail;
  100. head = tail = of_pdt_build_one_prop(node, NULL,
  101. ".node", &node, sizeof(node));
  102. tail->next = of_pdt_build_one_prop(node, NULL, NULL, NULL, 0);
  103. tail = tail->next;
  104. while(tail) {
  105. tail->next = of_pdt_build_one_prop(node, tail->name,
  106. NULL, NULL, 0);
  107. tail = tail->next;
  108. }
  109. return head;
  110. }
  111. static char * __init of_pdt_get_one_property(phandle node, const char *name)
  112. {
  113. char *buf = "<NULL>";
  114. int len;
  115. len = of_pdt_prom_ops->getproplen(node, name);
  116. if (len > 0) {
  117. buf = prom_early_alloc(len);
  118. len = of_pdt_prom_ops->getproperty(node, name, buf, len);
  119. }
  120. return buf;
  121. }
  122. static struct device_node * __init of_pdt_create_node(phandle node,
  123. struct device_node *parent)
  124. {
  125. struct device_node *dp;
  126. if (!node)
  127. return NULL;
  128. dp = prom_early_alloc(sizeof(*dp));
  129. of_node_init(dp);
  130. of_pdt_incr_unique_id(dp);
  131. dp->parent = parent;
  132. dp->name = of_pdt_get_one_property(node, "name");
  133. dp->phandle = node;
  134. dp->properties = of_pdt_build_prop_list(node);
  135. dp->full_name = of_pdt_build_full_name(dp);
  136. irq_trans_init(dp);
  137. return dp;
  138. }
  139. static struct device_node * __init of_pdt_build_tree(struct device_node *parent,
  140. phandle node)
  141. {
  142. struct device_node *ret = NULL, *prev_sibling = NULL;
  143. struct device_node *dp;
  144. while (1) {
  145. dp = of_pdt_create_node(node, parent);
  146. if (!dp)
  147. break;
  148. if (prev_sibling)
  149. prev_sibling->sibling = dp;
  150. if (!ret)
  151. ret = dp;
  152. prev_sibling = dp;
  153. dp->child = of_pdt_build_tree(dp, of_pdt_prom_ops->getchild(node));
  154. node = of_pdt_prom_ops->getsibling(node);
  155. }
  156. return ret;
  157. }
  158. static void * __init kernel_tree_alloc(u64 size, u64 align)
  159. {
  160. return prom_early_alloc(size);
  161. }
  162. void __init of_pdt_build_devicetree(phandle root_node, struct of_pdt_ops *ops)
  163. {
  164. BUG_ON(!ops);
  165. of_pdt_prom_ops = ops;
  166. of_root = of_pdt_create_node(root_node, NULL);
  167. of_root->full_name = "/";
  168. of_root->child = of_pdt_build_tree(of_root,
  169. of_pdt_prom_ops->getchild(of_root->phandle));
  170. /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
  171. of_alias_scan(kernel_tree_alloc);
  172. }