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,
  81. rsrc.end - rsrc.start + 1);
  82. if (r)
  83. break;
  84. }
  85. of_node_put(np);
  86. return r;
  87. }
  88. static int __init of_numa_parse_distance_map_v1(struct device_node *map)
  89. {
  90. const __be32 *matrix;
  91. int entry_count;
  92. int i;
  93. pr_info("NUMA: parsing numa-distance-map-v1\n");
  94. matrix = of_get_property(map, "distance-matrix", NULL);
  95. if (!matrix) {
  96. pr_err("NUMA: No distance-matrix property in distance-map\n");
  97. return -EINVAL;
  98. }
  99. entry_count = of_property_count_u32_elems(map, "distance-matrix");
  100. if (entry_count <= 0) {
  101. pr_err("NUMA: Invalid distance-matrix\n");
  102. return -EINVAL;
  103. }
  104. for (i = 0; i + 2 < entry_count; i += 3) {
  105. u32 nodea, nodeb, distance;
  106. nodea = of_read_number(matrix, 1);
  107. matrix++;
  108. nodeb = of_read_number(matrix, 1);
  109. matrix++;
  110. distance = of_read_number(matrix, 1);
  111. matrix++;
  112. numa_set_distance(nodea, nodeb, distance);
  113. pr_debug("NUMA: distance[node%d -> node%d] = %d\n",
  114. nodea, nodeb, distance);
  115. /* Set default distance of node B->A same as A->B */
  116. if (nodeb > nodea)
  117. numa_set_distance(nodeb, nodea, distance);
  118. }
  119. return 0;
  120. }
  121. static int __init of_numa_parse_distance_map(void)
  122. {
  123. int ret = 0;
  124. struct device_node *np;
  125. np = of_find_compatible_node(NULL, NULL,
  126. "numa-distance-map-v1");
  127. if (np)
  128. ret = of_numa_parse_distance_map_v1(np);
  129. of_node_put(np);
  130. return ret;
  131. }
  132. int of_node_to_nid(struct device_node *device)
  133. {
  134. struct device_node *np;
  135. u32 nid;
  136. int r = -ENODATA;
  137. np = of_node_get(device);
  138. while (np) {
  139. struct device_node *parent;
  140. r = of_property_read_u32(np, "numa-node-id", &nid);
  141. /*
  142. * -EINVAL indicates the property was not found, and
  143. * we walk up the tree trying to find a parent with a
  144. * "numa-node-id". Any other type of error indicates
  145. * a bad device tree and we give up.
  146. */
  147. if (r != -EINVAL)
  148. break;
  149. parent = of_get_parent(np);
  150. of_node_put(np);
  151. np = parent;
  152. }
  153. if (np && r)
  154. pr_warn("NUMA: Invalid \"numa-node-id\" property in node %s\n",
  155. np->name);
  156. of_node_put(np);
  157. if (!r) {
  158. if (nid >= MAX_NUMNODES)
  159. pr_warn("NUMA: Node id %u exceeds maximum value\n",
  160. nid);
  161. else
  162. return nid;
  163. }
  164. return NUMA_NO_NODE;
  165. }
  166. EXPORT_SYMBOL(of_node_to_nid);
  167. int __init of_numa_init(void)
  168. {
  169. int r;
  170. of_numa_parse_cpu_nodes();
  171. r = of_numa_parse_memory_nodes();
  172. if (r)
  173. return r;
  174. return of_numa_parse_distance_map();
  175. }