of_numa.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. numa_set_distance(nodea, nodeb, distance);
  96. pr_debug("distance[node%d -> node%d] = %d\n",
  97. nodea, nodeb, distance);
  98. /* Set default distance of node B->A same as A->B */
  99. if (nodeb > nodea)
  100. numa_set_distance(nodeb, nodea, distance);
  101. }
  102. return 0;
  103. }
  104. static int __init of_numa_parse_distance_map(void)
  105. {
  106. int ret = 0;
  107. struct device_node *np;
  108. np = of_find_compatible_node(NULL, NULL,
  109. "numa-distance-map-v1");
  110. if (np)
  111. ret = of_numa_parse_distance_map_v1(np);
  112. of_node_put(np);
  113. return ret;
  114. }
  115. int of_node_to_nid(struct device_node *device)
  116. {
  117. struct device_node *np;
  118. u32 nid;
  119. int r = -ENODATA;
  120. np = of_node_get(device);
  121. while (np) {
  122. r = of_property_read_u32(np, "numa-node-id", &nid);
  123. /*
  124. * -EINVAL indicates the property was not found, and
  125. * we walk up the tree trying to find a parent with a
  126. * "numa-node-id". Any other type of error indicates
  127. * a bad device tree and we give up.
  128. */
  129. if (r != -EINVAL)
  130. break;
  131. np = of_get_next_parent(np);
  132. }
  133. if (np && r)
  134. pr_warn("Invalid \"numa-node-id\" property in node %s\n",
  135. np->name);
  136. of_node_put(np);
  137. /*
  138. * If numa=off passed on command line, or with a defective
  139. * device tree, the nid may not be in the set of possible
  140. * nodes. Check for this case and return NUMA_NO_NODE.
  141. */
  142. if (!r && nid < MAX_NUMNODES && node_possible(nid))
  143. return nid;
  144. return NUMA_NO_NODE;
  145. }
  146. int __init of_numa_init(void)
  147. {
  148. int r;
  149. of_numa_parse_cpu_nodes();
  150. r = of_numa_parse_memory_nodes();
  151. if (r)
  152. return r;
  153. return of_numa_parse_distance_map();
  154. }