srat.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. #include <asm/e820.h>
  20. static struct acpi_table_slit *acpi_slit;
  21. static nodemask_t nodes_parsed __initdata;
  22. static nodemask_t nodes_found __initdata;
  23. static struct node nodes[MAX_NUMNODES] __initdata;
  24. static u8 pxm2node[256] = { [0 ... 255] = 0xff };
  25. static int node_to_pxm(int n);
  26. int pxm_to_node(int pxm)
  27. {
  28. if ((unsigned)pxm >= 256)
  29. return -1;
  30. /* Extend 0xff to (int)-1 */
  31. return (signed char)pxm2node[pxm];
  32. }
  33. static __init int setup_node(int pxm)
  34. {
  35. unsigned node = pxm2node[pxm];
  36. if (node == 0xff) {
  37. if (nodes_weight(nodes_found) >= MAX_NUMNODES)
  38. return -1;
  39. node = first_unset_node(nodes_found);
  40. node_set(node, nodes_found);
  41. pxm2node[pxm] = node;
  42. }
  43. return pxm2node[pxm];
  44. }
  45. static __init int conflicting_nodes(unsigned long start, unsigned long end)
  46. {
  47. int i;
  48. for_each_node_mask(i, nodes_parsed) {
  49. struct node *nd = &nodes[i];
  50. if (nd->start == nd->end)
  51. continue;
  52. if (nd->end > start && nd->start < end)
  53. return i;
  54. if (nd->end == end && nd->start == start)
  55. return i;
  56. }
  57. return -1;
  58. }
  59. static __init void cutoff_node(int i, unsigned long start, unsigned long end)
  60. {
  61. struct node *nd = &nodes[i];
  62. if (nd->start < start) {
  63. nd->start = start;
  64. if (nd->end < nd->start)
  65. nd->start = nd->end;
  66. }
  67. if (nd->end > end) {
  68. nd->end = end;
  69. if (nd->start > nd->end)
  70. nd->start = nd->end;
  71. }
  72. }
  73. static __init void bad_srat(void)
  74. {
  75. int i;
  76. printk(KERN_ERR "SRAT: SRAT not used.\n");
  77. acpi_numa = -1;
  78. for (i = 0; i < MAX_LOCAL_APIC; i++)
  79. apicid_to_node[i] = NUMA_NO_NODE;
  80. }
  81. static __init inline int srat_disabled(void)
  82. {
  83. return numa_off || acpi_numa < 0;
  84. }
  85. /*
  86. * A lot of BIOS fill in 10 (= no distance) everywhere. This messes
  87. * up the NUMA heuristics which wants the local node to have a smaller
  88. * distance than the others.
  89. * Do some quick checks here and only use the SLIT if it passes.
  90. */
  91. static __init int slit_valid(struct acpi_table_slit *slit)
  92. {
  93. int i, j;
  94. int d = slit->localities;
  95. for (i = 0; i < d; i++) {
  96. for (j = 0; j < d; j++) {
  97. u8 val = slit->entry[d*i + j];
  98. if (i == j) {
  99. if (val != 10)
  100. return 0;
  101. } else if (val <= 10)
  102. return 0;
  103. }
  104. }
  105. return 1;
  106. }
  107. /* Callback for SLIT parsing */
  108. void __init acpi_numa_slit_init(struct acpi_table_slit *slit)
  109. {
  110. if (!slit_valid(slit)) {
  111. printk(KERN_INFO "ACPI: SLIT table looks invalid. Not used.\n");
  112. return;
  113. }
  114. acpi_slit = slit;
  115. }
  116. /* Callback for Proximity Domain -> LAPIC mapping */
  117. void __init
  118. acpi_numa_processor_affinity_init(struct acpi_table_processor_affinity *pa)
  119. {
  120. int pxm, node;
  121. if (srat_disabled() || pa->flags.enabled == 0)
  122. return;
  123. pxm = pa->proximity_domain;
  124. node = setup_node(pxm);
  125. if (node < 0) {
  126. printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm);
  127. bad_srat();
  128. return;
  129. }
  130. apicid_to_node[pa->apic_id] = node;
  131. acpi_numa = 1;
  132. printk(KERN_INFO "SRAT: PXM %u -> APIC %u -> Node %u\n",
  133. pxm, pa->apic_id, node);
  134. }
  135. /* Callback for parsing of the Proximity Domain <-> Memory Area mappings */
  136. void __init
  137. acpi_numa_memory_affinity_init(struct acpi_table_memory_affinity *ma)
  138. {
  139. struct node *nd;
  140. unsigned long start, end;
  141. int node, pxm;
  142. int i;
  143. if (srat_disabled() || ma->flags.enabled == 0)
  144. return;
  145. pxm = ma->proximity_domain;
  146. node = setup_node(pxm);
  147. if (node < 0) {
  148. printk(KERN_ERR "SRAT: Too many proximity domains.\n");
  149. bad_srat();
  150. return;
  151. }
  152. start = ma->base_addr_lo | ((u64)ma->base_addr_hi << 32);
  153. end = start + (ma->length_lo | ((u64)ma->length_hi << 32));
  154. /* It is fine to add this area to the nodes data it will be used later*/
  155. if (ma->flags.hot_pluggable == 1)
  156. printk(KERN_INFO "SRAT: hot plug zone found %lx - %lx \n",
  157. start, end);
  158. i = conflicting_nodes(start, end);
  159. if (i == node) {
  160. printk(KERN_WARNING
  161. "SRAT: Warning: PXM %d (%lx-%lx) overlaps with itself (%Lx-%Lx)\n",
  162. pxm, start, end, nodes[i].start, nodes[i].end);
  163. } else if (i >= 0) {
  164. printk(KERN_ERR
  165. "SRAT: PXM %d (%lx-%lx) overlaps with PXM %d (%Lx-%Lx)\n",
  166. pxm, start, end, node_to_pxm(i),
  167. nodes[i].start, nodes[i].end);
  168. bad_srat();
  169. return;
  170. }
  171. nd = &nodes[node];
  172. if (!node_test_and_set(node, nodes_parsed)) {
  173. nd->start = start;
  174. nd->end = end;
  175. } else {
  176. if (start < nd->start)
  177. nd->start = start;
  178. if (nd->end < end)
  179. nd->end = end;
  180. }
  181. printk(KERN_INFO "SRAT: Node %u PXM %u %Lx-%Lx\n", node, pxm,
  182. nd->start, nd->end);
  183. }
  184. /* Sanity check to catch more bad SRATs (they are amazingly common).
  185. Make sure the PXMs cover all memory. */
  186. static int nodes_cover_memory(void)
  187. {
  188. int i;
  189. unsigned long pxmram, e820ram;
  190. pxmram = 0;
  191. for_each_node_mask(i, nodes_parsed) {
  192. unsigned long s = nodes[i].start >> PAGE_SHIFT;
  193. unsigned long e = nodes[i].end >> PAGE_SHIFT;
  194. pxmram += e - s;
  195. pxmram -= e820_hole_size(s, e);
  196. }
  197. e820ram = end_pfn - e820_hole_size(0, end_pfn);
  198. if (pxmram < e820ram) {
  199. printk(KERN_ERR
  200. "SRAT: PXMs only cover %luMB of your %luMB e820 RAM. Not used.\n",
  201. (pxmram << PAGE_SHIFT) >> 20,
  202. (e820ram << PAGE_SHIFT) >> 20);
  203. return 0;
  204. }
  205. return 1;
  206. }
  207. void __init acpi_numa_arch_fixup(void) {}
  208. /* Use the information discovered above to actually set up the nodes. */
  209. int __init acpi_scan_nodes(unsigned long start, unsigned long end)
  210. {
  211. int i;
  212. if (acpi_numa <= 0)
  213. return -1;
  214. /* First clean up the node list */
  215. for_each_node_mask(i, nodes_parsed) {
  216. cutoff_node(i, start, end);
  217. if (nodes[i].start == nodes[i].end)
  218. node_clear(i, nodes_parsed);
  219. }
  220. if (!nodes_cover_memory()) {
  221. bad_srat();
  222. return -1;
  223. }
  224. memnode_shift = compute_hash_shift(nodes, nodes_weight(nodes_parsed));
  225. if (memnode_shift < 0) {
  226. printk(KERN_ERR
  227. "SRAT: No NUMA node hash function found. Contact maintainer\n");
  228. bad_srat();
  229. return -1;
  230. }
  231. /* Finally register nodes */
  232. for_each_node_mask(i, nodes_parsed)
  233. setup_node_bootmem(i, nodes[i].start, nodes[i].end);
  234. for (i = 0; i < NR_CPUS; i++) {
  235. if (cpu_to_node[i] == NUMA_NO_NODE)
  236. continue;
  237. if (!node_isset(cpu_to_node[i], nodes_parsed))
  238. numa_set_node(i, NUMA_NO_NODE);
  239. }
  240. numa_init_array();
  241. return 0;
  242. }
  243. static int node_to_pxm(int n)
  244. {
  245. int i;
  246. if (pxm2node[n] == n)
  247. return n;
  248. for (i = 0; i < 256; i++)
  249. if (pxm2node[i] == n)
  250. return i;
  251. return 0;
  252. }
  253. int __node_distance(int a, int b)
  254. {
  255. int index;
  256. if (!acpi_slit)
  257. return a == b ? 10 : 20;
  258. index = acpi_slit->localities * node_to_pxm(a);
  259. return acpi_slit->entry[index + node_to_pxm(b)];
  260. }
  261. EXPORT_SYMBOL(__node_distance);