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