srat.c 7.4 KB

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