of_numa.c 3.9 KB

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