of_numa.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #define pr_fmt(fmt) "OF: NUMA: " fmt
  19. #include <linux/of.h>
  20. #include <linux/of_address.h>
  21. #include <linux/nodemask.h>
  22. #include <asm/numa.h>
  23. /* define default numa node to 0 */
  24. #define DEFAULT_NODE 0
  25. /*
  26. * Even though we connect cpus to numa domains later in SMP
  27. * init, we need to know the node ids now for all cpus.
  28. */
  29. static void __init of_numa_parse_cpu_nodes(void)
  30. {
  31. u32 nid;
  32. int r;
  33. struct device_node *cpus;
  34. struct device_node *np = NULL;
  35. cpus = of_find_node_by_path("/cpus");
  36. if (!cpus)
  37. return;
  38. for_each_child_of_node(cpus, np) {
  39. /* Skip things that are not CPUs */
  40. if (of_node_cmp(np->type, "cpu") != 0)
  41. continue;
  42. r = of_property_read_u32(np, "numa-node-id", &nid);
  43. if (r)
  44. continue;
  45. pr_debug("CPU on %u\n", nid);
  46. if (nid >= MAX_NUMNODES)
  47. pr_warn("Node id %u exceeds maximum value\n", nid);
  48. else
  49. node_set(nid, numa_nodes_parsed);
  50. }
  51. of_node_put(cpus);
  52. }
  53. static int __init of_numa_parse_memory_nodes(void)
  54. {
  55. struct device_node *np = NULL;
  56. struct resource rsrc;
  57. u32 nid;
  58. int i, r;
  59. for_each_node_by_type(np, "memory") {
  60. r = of_property_read_u32(np, "numa-node-id", &nid);
  61. if (r == -EINVAL)
  62. /*
  63. * property doesn't exist if -EINVAL, continue
  64. * looking for more memory nodes with
  65. * "numa-node-id" property
  66. */
  67. continue;
  68. if (nid >= MAX_NUMNODES) {
  69. pr_warn("Node id %u exceeds maximum value\n", nid);
  70. r = -EINVAL;
  71. }
  72. for (i = 0; !r && !of_address_to_resource(np, i, &rsrc); i++)
  73. r = numa_add_memblk(nid, rsrc.start, rsrc.end + 1);
  74. if (!i || r) {
  75. of_node_put(np);
  76. pr_err("bad property in memory node\n");
  77. return r ? : -EINVAL;
  78. }
  79. }
  80. return 0;
  81. }
  82. static int __init of_numa_parse_distance_map_v1(struct device_node *map)
  83. {
  84. const __be32 *matrix;
  85. int entry_count;
  86. int i;
  87. pr_info("parsing numa-distance-map-v1\n");
  88. matrix = of_get_property(map, "distance-matrix", NULL);
  89. if (!matrix) {
  90. pr_err("No distance-matrix property in distance-map\n");
  91. return -EINVAL;
  92. }
  93. entry_count = of_property_count_u32_elems(map, "distance-matrix");
  94. if (entry_count <= 0) {
  95. pr_err("Invalid distance-matrix\n");
  96. return -EINVAL;
  97. }
  98. for (i = 0; i + 2 < entry_count; i += 3) {
  99. u32 nodea, nodeb, distance;
  100. nodea = of_read_number(matrix, 1);
  101. matrix++;
  102. nodeb = of_read_number(matrix, 1);
  103. matrix++;
  104. distance = of_read_number(matrix, 1);
  105. matrix++;
  106. if ((nodea == nodeb && distance != LOCAL_DISTANCE) ||
  107. (nodea != nodeb && distance <= LOCAL_DISTANCE)) {
  108. pr_err("Invalid distance[node%d -> node%d] = %d\n",
  109. nodea, nodeb, distance);
  110. return -EINVAL;
  111. }
  112. numa_set_distance(nodea, nodeb, distance);
  113. /* Set default distance of node B->A same as A->B */
  114. if (nodeb > nodea)
  115. numa_set_distance(nodeb, nodea, distance);
  116. }
  117. return 0;
  118. }
  119. static int __init of_numa_parse_distance_map(void)
  120. {
  121. int ret = 0;
  122. struct device_node *np;
  123. np = of_find_compatible_node(NULL, NULL,
  124. "numa-distance-map-v1");
  125. if (np)
  126. ret = of_numa_parse_distance_map_v1(np);
  127. of_node_put(np);
  128. return ret;
  129. }
  130. int of_node_to_nid(struct device_node *device)
  131. {
  132. struct device_node *np;
  133. u32 nid;
  134. int r = -ENODATA;
  135. np = of_node_get(device);
  136. while (np) {
  137. r = of_property_read_u32(np, "numa-node-id", &nid);
  138. /*
  139. * -EINVAL indicates the property was not found, and
  140. * we walk up the tree trying to find a parent with a
  141. * "numa-node-id". Any other type of error indicates
  142. * a bad device tree and we give up.
  143. */
  144. if (r != -EINVAL)
  145. break;
  146. np = of_get_next_parent(np);
  147. }
  148. if (np && r)
  149. pr_warn("Invalid \"numa-node-id\" property in node %s\n",
  150. np->name);
  151. of_node_put(np);
  152. /*
  153. * If numa=off passed on command line, or with a defective
  154. * device tree, the nid may not be in the set of possible
  155. * nodes. Check for this case and return NUMA_NO_NODE.
  156. */
  157. if (!r && nid < MAX_NUMNODES && node_possible(nid))
  158. return nid;
  159. return NUMA_NO_NODE;
  160. }
  161. EXPORT_SYMBOL(of_node_to_nid);
  162. int __init of_numa_init(void)
  163. {
  164. int r;
  165. of_numa_parse_cpu_nodes();
  166. r = of_numa_parse_memory_nodes();
  167. if (r)
  168. return r;
  169. return of_numa_parse_distance_map();
  170. }