srat.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * ACPI 3.0 based NUMA setup
  3. * Copyright 2004 Andi Kleen, SuSE Labs.
  4. *
  5. * Reads the ACPI SRAT table to figure out what memory belongs to which CPUs.
  6. *
  7. * Called from acpi_numa_init while reading the SRAT and SLIT tables.
  8. * Assumes all memory regions belonging to a single proximity domain
  9. * are in one chunk. Holes between them will be included in the node.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/acpi.h>
  13. #include <linux/mmzone.h>
  14. #include <linux/bitmap.h>
  15. #include <linux/module.h>
  16. #include <linux/topology.h>
  17. #include <asm/proto.h>
  18. #include <asm/numa.h>
  19. static struct acpi_table_slit *acpi_slit;
  20. static nodemask_t nodes_parsed __initdata;
  21. static nodemask_t nodes_found __initdata;
  22. static struct node nodes[MAX_NUMNODES] __initdata;
  23. static __u8 pxm2node[256] = { [0 ... 255] = 0xff };
  24. static int node_to_pxm(int n);
  25. int pxm_to_node(int pxm)
  26. {
  27. if ((unsigned)pxm >= 256)
  28. return 0;
  29. return pxm2node[pxm];
  30. }
  31. static __init int setup_node(int pxm)
  32. {
  33. unsigned node = pxm2node[pxm];
  34. if (node == 0xff) {
  35. if (nodes_weight(nodes_found) >= MAX_NUMNODES)
  36. return -1;
  37. node = first_unset_node(nodes_found);
  38. node_set(node, nodes_found);
  39. pxm2node[pxm] = node;
  40. }
  41. return pxm2node[pxm];
  42. }
  43. static __init int conflicting_nodes(unsigned long start, unsigned long end)
  44. {
  45. int i;
  46. for_each_node_mask(i, nodes_parsed) {
  47. struct node *nd = &nodes[i];
  48. if (nd->start == nd->end)
  49. continue;
  50. if (nd->end > start && nd->start < end)
  51. return i;
  52. if (nd->end == end && nd->start == start)
  53. return i;
  54. }
  55. return -1;
  56. }
  57. static __init void cutoff_node(int i, unsigned long start, unsigned long end)
  58. {
  59. struct node *nd = &nodes[i];
  60. if (nd->start < start) {
  61. nd->start = start;
  62. if (nd->end < nd->start)
  63. nd->start = nd->end;
  64. }
  65. if (nd->end > end) {
  66. nd->end = end;
  67. if (nd->start > nd->end)
  68. nd->start = nd->end;
  69. }
  70. }
  71. static __init void bad_srat(void)
  72. {
  73. int i;
  74. printk(KERN_ERR "SRAT: SRAT not used.\n");
  75. acpi_numa = -1;
  76. for (i = 0; i < MAX_LOCAL_APIC; i++)
  77. apicid_to_node[i] = NUMA_NO_NODE;
  78. }
  79. static __init inline int srat_disabled(void)
  80. {
  81. return numa_off || acpi_numa < 0;
  82. }
  83. /*
  84. * A lot of BIOS fill in 10 (= no distance) everywhere. This messes
  85. * up the NUMA heuristics which wants the local node to have a smaller
  86. * distance than the others.
  87. * Do some quick checks here and only use the SLIT if it passes.
  88. */
  89. static __init int slit_valid(struct acpi_table_slit *slit)
  90. {
  91. int i, j;
  92. int d = slit->localities;
  93. for (i = 0; i < d; i++) {
  94. for (j = 0; j < d; j++) {
  95. u8 val = slit->entry[d*i + j];
  96. if (i == j) {
  97. if (val != 10)
  98. return 0;
  99. } else if (val <= 10)
  100. return 0;
  101. }
  102. }
  103. return 1;
  104. }
  105. /* Callback for SLIT parsing */
  106. void __init acpi_numa_slit_init(struct acpi_table_slit *slit)
  107. {
  108. if (!slit_valid(slit)) {
  109. printk(KERN_INFO "ACPI: SLIT table looks invalid. Not used.\n");
  110. return;
  111. }
  112. acpi_slit = slit;
  113. }
  114. /* Callback for Proximity Domain -> LAPIC mapping */
  115. void __init
  116. acpi_numa_processor_affinity_init(struct acpi_table_processor_affinity *pa)
  117. {
  118. int pxm, node;
  119. if (srat_disabled() || pa->flags.enabled == 0)
  120. return;
  121. pxm = pa->proximity_domain;
  122. node = setup_node(pxm);
  123. if (node < 0) {
  124. printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm);
  125. bad_srat();
  126. return;
  127. }
  128. apicid_to_node[pa->apic_id] = node;
  129. acpi_numa = 1;
  130. printk(KERN_INFO "SRAT: PXM %u -> APIC %u -> Node %u\n",
  131. pxm, pa->apic_id, node);
  132. }
  133. /* Callback for parsing of the Proximity Domain <-> Memory Area mappings */
  134. void __init
  135. acpi_numa_memory_affinity_init(struct acpi_table_memory_affinity *ma)
  136. {
  137. struct node *nd;
  138. unsigned long start, end;
  139. int node, pxm;
  140. int i;
  141. if (srat_disabled() || ma->flags.enabled == 0)
  142. return;
  143. pxm = ma->proximity_domain;
  144. node = setup_node(pxm);
  145. if (node < 0) {
  146. printk(KERN_ERR "SRAT: Too many proximity domains.\n");
  147. bad_srat();
  148. return;
  149. }
  150. start = ma->base_addr_lo | ((u64)ma->base_addr_hi << 32);
  151. end = start + (ma->length_lo | ((u64)ma->length_hi << 32));
  152. /* It is fine to add this area to the nodes data it will be used later*/
  153. if (ma->flags.hot_pluggable == 1)
  154. printk(KERN_INFO "SRAT: hot plug zone found %lx - %lx \n",
  155. start, end);
  156. i = conflicting_nodes(start, end);
  157. if (i == node) {
  158. printk(KERN_WARNING
  159. "SRAT: Warning: PXM %d (%lx-%lx) overlaps with itself (%Lx-%Lx)\n",
  160. pxm, start, end, nodes[i].start, nodes[i].end);
  161. } else if (i >= 0) {
  162. printk(KERN_ERR
  163. "SRAT: PXM %d (%lx-%lx) overlaps with PXM %d (%Lx-%Lx)\n",
  164. pxm, start, end, node_to_pxm(i),
  165. nodes[i].start, nodes[i].end);
  166. bad_srat();
  167. return;
  168. }
  169. nd = &nodes[node];
  170. if (!node_test_and_set(node, nodes_parsed)) {
  171. nd->start = start;
  172. nd->end = end;
  173. } else {
  174. if (start < nd->start)
  175. nd->start = start;
  176. if (nd->end < end)
  177. nd->end = end;
  178. }
  179. printk(KERN_INFO "SRAT: Node %u PXM %u %Lx-%Lx\n", node, pxm,
  180. nd->start, nd->end);
  181. }
  182. void __init acpi_numa_arch_fixup(void) {}
  183. /* Use the information discovered above to actually set up the nodes. */
  184. int __init acpi_scan_nodes(unsigned long start, unsigned long end)
  185. {
  186. int i;
  187. if (acpi_numa <= 0)
  188. return -1;
  189. /* First clean up the node list */
  190. for_each_node_mask(i, nodes_parsed) {
  191. cutoff_node(i, start, end);
  192. if (nodes[i].start == nodes[i].end)
  193. node_clear(i, nodes_parsed);
  194. }
  195. memnode_shift = compute_hash_shift(nodes, nodes_weight(nodes_parsed));
  196. if (memnode_shift < 0) {
  197. printk(KERN_ERR
  198. "SRAT: No NUMA node hash function found. Contact maintainer\n");
  199. bad_srat();
  200. return -1;
  201. }
  202. /* Finally register nodes */
  203. for_each_node_mask(i, nodes_parsed)
  204. setup_node_bootmem(i, nodes[i].start, nodes[i].end);
  205. for (i = 0; i < NR_CPUS; i++) {
  206. if (cpu_to_node[i] == NUMA_NO_NODE)
  207. continue;
  208. if (!node_isset(cpu_to_node[i], nodes_parsed))
  209. numa_set_node(i, NUMA_NO_NODE);
  210. }
  211. numa_init_array();
  212. return 0;
  213. }
  214. static int node_to_pxm(int n)
  215. {
  216. int i;
  217. if (pxm2node[n] == n)
  218. return n;
  219. for (i = 0; i < 256; i++)
  220. if (pxm2node[i] == n)
  221. return i;
  222. return 0;
  223. }
  224. int __node_distance(int a, int b)
  225. {
  226. int index;
  227. if (!acpi_slit)
  228. return a == b ? 10 : 20;
  229. index = acpi_slit->localities * node_to_pxm(a);
  230. return acpi_slit->entry[index + node_to_pxm(b)];
  231. }
  232. EXPORT_SYMBOL(__node_distance);