of_numa.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. }
  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 i, r;
  58. for_each_node_by_type(np, "memory") {
  59. r = of_property_read_u32(np, "numa-node-id", &nid);
  60. if (r == -EINVAL)
  61. /*
  62. * property doesn't exist if -EINVAL, continue
  63. * looking for more memory nodes with
  64. * "numa-node-id" property
  65. */
  66. continue;
  67. if (nid >= MAX_NUMNODES) {
  68. pr_warn("Node id %u exceeds maximum value\n", nid);
  69. r = -EINVAL;
  70. }
  71. for (i = 0; !r && !of_address_to_resource(np, i, &rsrc); i++)
  72. r = numa_add_memblk(nid, rsrc.start, rsrc.end + 1);
  73. if (!i || r) {
  74. of_node_put(np);
  75. pr_err("bad property in memory node\n");
  76. return r ? : -EINVAL;
  77. }
  78. }
  79. return 0;
  80. }
  81. static int __init of_numa_parse_distance_map_v1(struct device_node *map)
  82. {
  83. const __be32 *matrix;
  84. int entry_count;
  85. int i;
  86. pr_info("parsing numa-distance-map-v1\n");
  87. matrix = of_get_property(map, "distance-matrix", NULL);
  88. if (!matrix) {
  89. pr_err("No distance-matrix property in distance-map\n");
  90. return -EINVAL;
  91. }
  92. entry_count = of_property_count_u32_elems(map, "distance-matrix");
  93. if (entry_count <= 0) {
  94. pr_err("Invalid distance-matrix\n");
  95. return -EINVAL;
  96. }
  97. for (i = 0; i + 2 < entry_count; i += 3) {
  98. u32 nodea, nodeb, distance;
  99. nodea = of_read_number(matrix, 1);
  100. matrix++;
  101. nodeb = of_read_number(matrix, 1);
  102. matrix++;
  103. distance = of_read_number(matrix, 1);
  104. matrix++;
  105. numa_set_distance(nodea, nodeb, distance);
  106. pr_debug("distance[node%d -> node%d] = %d\n",
  107. nodea, nodeb, distance);
  108. /* Set default distance of node B->A same as A->B */
  109. if (nodeb > nodea)
  110. numa_set_distance(nodeb, nodea, distance);
  111. }
  112. return 0;
  113. }
  114. static int __init of_numa_parse_distance_map(void)
  115. {
  116. int ret = 0;
  117. struct device_node *np;
  118. np = of_find_compatible_node(NULL, NULL,
  119. "numa-distance-map-v1");
  120. if (np)
  121. ret = of_numa_parse_distance_map_v1(np);
  122. of_node_put(np);
  123. return ret;
  124. }
  125. int of_node_to_nid(struct device_node *device)
  126. {
  127. struct device_node *np;
  128. u32 nid;
  129. int r = -ENODATA;
  130. np = of_node_get(device);
  131. while (np) {
  132. r = of_property_read_u32(np, "numa-node-id", &nid);
  133. /*
  134. * -EINVAL indicates the property was not found, and
  135. * we walk up the tree trying to find a parent with a
  136. * "numa-node-id". Any other type of error indicates
  137. * a bad device tree and we give up.
  138. */
  139. if (r != -EINVAL)
  140. break;
  141. np = of_get_next_parent(np);
  142. }
  143. if (np && r)
  144. pr_warn("Invalid \"numa-node-id\" property in node %s\n",
  145. np->name);
  146. of_node_put(np);
  147. /*
  148. * If numa=off passed on command line, or with a defective
  149. * device tree, the nid may not be in the set of possible
  150. * nodes. Check for this case and return NUMA_NO_NODE.
  151. */
  152. if (!r && nid < MAX_NUMNODES && node_possible(nid))
  153. return nid;
  154. return NUMA_NO_NODE;
  155. }
  156. EXPORT_SYMBOL(of_node_to_nid);
  157. int __init of_numa_init(void)
  158. {
  159. int r;
  160. of_numa_parse_cpu_nodes();
  161. r = of_numa_parse_memory_nodes();
  162. if (r)
  163. return r;
  164. return of_numa_parse_distance_map();
  165. }