of_numa.c 3.9 KB

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