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