of_numa.c 4.1 KB

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