of_numa.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * OF NUMA Parsing support.
  3. *
  4. * Copyright (C) 2015 - 2016 Cavium Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/nodemask.h>
  21. #include <asm/numa.h>
  22. /* define default numa node to 0 */
  23. #define DEFAULT_NODE 0
  24. /*
  25. * Even though we connect cpus to numa domains later in SMP
  26. * init, we need to know the node ids now for all cpus.
  27. */
  28. static void __init of_numa_parse_cpu_nodes(void)
  29. {
  30. u32 nid;
  31. int r;
  32. struct device_node *cpus;
  33. struct device_node *np = NULL;
  34. cpus = of_find_node_by_path("/cpus");
  35. if (!cpus)
  36. return;
  37. for_each_child_of_node(cpus, np) {
  38. /* Skip things that are not CPUs */
  39. if (of_node_cmp(np->type, "cpu") != 0)
  40. continue;
  41. r = of_property_read_u32(np, "numa-node-id", &nid);
  42. if (r)
  43. continue;
  44. pr_debug("NUMA: CPU on %u\n", nid);
  45. if (nid >= MAX_NUMNODES)
  46. pr_warn("NUMA: Node id %u exceeds maximum value\n",
  47. nid);
  48. else
  49. node_set(nid, numa_nodes_parsed);
  50. }
  51. }
  52. static int __init of_numa_parse_memory_nodes(void)
  53. {
  54. struct device_node *np = NULL;
  55. struct resource rsrc;
  56. u32 nid;
  57. int r = 0;
  58. for (;;) {
  59. np = of_find_node_by_type(np, "memory");
  60. if (!np)
  61. break;
  62. r = of_property_read_u32(np, "numa-node-id", &nid);
  63. if (r == -EINVAL)
  64. /*
  65. * property doesn't exist if -EINVAL, continue
  66. * looking for more memory nodes with
  67. * "numa-node-id" property
  68. */
  69. continue;
  70. else if (r)
  71. /* some other error */
  72. break;
  73. r = of_address_to_resource(np, 0, &rsrc);
  74. if (r) {
  75. pr_err("NUMA: bad reg property in memory node\n");
  76. break;
  77. }
  78. pr_debug("NUMA: base = %llx len = %llx, node = %u\n",
  79. rsrc.start, rsrc.end - rsrc.start + 1, nid);
  80. r = numa_add_memblk(nid, rsrc.start, rsrc.end + 1);
  81. if (r)
  82. break;
  83. }
  84. of_node_put(np);
  85. return r;
  86. }
  87. static int __init of_numa_parse_distance_map_v1(struct device_node *map)
  88. {
  89. const __be32 *matrix;
  90. int entry_count;
  91. int i;
  92. pr_info("NUMA: parsing numa-distance-map-v1\n");
  93. matrix = of_get_property(map, "distance-matrix", NULL);
  94. if (!matrix) {
  95. pr_err("NUMA: No distance-matrix property in distance-map\n");
  96. return -EINVAL;
  97. }
  98. entry_count = of_property_count_u32_elems(map, "distance-matrix");
  99. if (entry_count <= 0) {
  100. pr_err("NUMA: Invalid distance-matrix\n");
  101. return -EINVAL;
  102. }
  103. for (i = 0; i + 2 < entry_count; i += 3) {
  104. u32 nodea, nodeb, distance;
  105. nodea = of_read_number(matrix, 1);
  106. matrix++;
  107. nodeb = of_read_number(matrix, 1);
  108. matrix++;
  109. distance = of_read_number(matrix, 1);
  110. matrix++;
  111. numa_set_distance(nodea, nodeb, distance);
  112. pr_debug("NUMA: distance[node%d -> node%d] = %d\n",
  113. nodea, nodeb, distance);
  114. /* Set default distance of node B->A same as A->B */
  115. if (nodeb > nodea)
  116. numa_set_distance(nodeb, nodea, distance);
  117. }
  118. return 0;
  119. }
  120. static int __init of_numa_parse_distance_map(void)
  121. {
  122. int ret = 0;
  123. struct device_node *np;
  124. np = of_find_compatible_node(NULL, NULL,
  125. "numa-distance-map-v1");
  126. if (np)
  127. ret = of_numa_parse_distance_map_v1(np);
  128. of_node_put(np);
  129. return ret;
  130. }
  131. int of_node_to_nid(struct device_node *device)
  132. {
  133. struct device_node *np;
  134. u32 nid;
  135. int r = -ENODATA;
  136. np = of_node_get(device);
  137. while (np) {
  138. struct device_node *parent;
  139. r = of_property_read_u32(np, "numa-node-id", &nid);
  140. /*
  141. * -EINVAL indicates the property was not found, and
  142. * we walk up the tree trying to find a parent with a
  143. * "numa-node-id". Any other type of error indicates
  144. * a bad device tree and we give up.
  145. */
  146. if (r != -EINVAL)
  147. break;
  148. parent = of_get_parent(np);
  149. of_node_put(np);
  150. np = parent;
  151. }
  152. if (np && r)
  153. pr_warn("NUMA: Invalid \"numa-node-id\" property in node %s\n",
  154. np->name);
  155. of_node_put(np);
  156. if (!r) {
  157. if (nid >= MAX_NUMNODES)
  158. pr_warn("NUMA: Node id %u exceeds maximum value\n",
  159. nid);
  160. else
  161. return nid;
  162. }
  163. return NUMA_NO_NODE;
  164. }
  165. EXPORT_SYMBOL(of_node_to_nid);
  166. int __init of_numa_init(void)
  167. {
  168. int r;
  169. of_numa_parse_cpu_nodes();
  170. r = of_numa_parse_memory_nodes();
  171. if (r)
  172. return r;
  173. return of_numa_parse_distance_map();
  174. }