srat.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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() || pa->flags.enabled == 0)
  125. return;
  126. pxm = pa->proximity_domain;
  127. node = setup_node(pxm);
  128. if (node < 0) {
  129. printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm);
  130. bad_srat();
  131. return;
  132. }
  133. apicid_to_node[pa->apic_id] = node;
  134. acpi_numa = 1;
  135. printk(KERN_INFO "SRAT: PXM %u -> APIC %u -> Node %u\n",
  136. pxm, pa->apic_id, node);
  137. }
  138. /* Callback for parsing of the Proximity Domain <-> Memory Area mappings */
  139. void __init
  140. acpi_numa_memory_affinity_init(struct acpi_table_memory_affinity *ma)
  141. {
  142. struct node *nd;
  143. unsigned long start, end;
  144. int node, pxm;
  145. int i;
  146. if (srat_disabled() || ma->flags.enabled == 0)
  147. return;
  148. pxm = ma->proximity_domain;
  149. node = setup_node(pxm);
  150. if (node < 0) {
  151. printk(KERN_ERR "SRAT: Too many proximity domains.\n");
  152. bad_srat();
  153. return;
  154. }
  155. start = ma->base_addr_lo | ((u64)ma->base_addr_hi << 32);
  156. end = start + (ma->length_lo | ((u64)ma->length_hi << 32));
  157. /* It is fine to add this area to the nodes data it will be used later*/
  158. if (ma->flags.hot_pluggable == 1)
  159. printk(KERN_INFO "SRAT: hot plug zone found %lx - %lx \n",
  160. start, end);
  161. i = conflicting_nodes(start, end);
  162. if (i == node) {
  163. printk(KERN_WARNING
  164. "SRAT: Warning: PXM %d (%lx-%lx) overlaps with itself (%Lx-%Lx)\n",
  165. pxm, start, end, nodes[i].start, nodes[i].end);
  166. } else if (i >= 0) {
  167. printk(KERN_ERR
  168. "SRAT: PXM %d (%lx-%lx) overlaps with PXM %d (%Lx-%Lx)\n",
  169. pxm, start, end, node_to_pxm(i),
  170. nodes[i].start, nodes[i].end);
  171. bad_srat();
  172. return;
  173. }
  174. nd = &nodes[node];
  175. if (!node_test_and_set(node, nodes_parsed)) {
  176. nd->start = start;
  177. nd->end = end;
  178. } else {
  179. if (start < nd->start)
  180. nd->start = start;
  181. if (nd->end < end)
  182. nd->end = end;
  183. }
  184. printk(KERN_INFO "SRAT: Node %u PXM %u %Lx-%Lx\n", node, pxm,
  185. nd->start, nd->end);
  186. }
  187. /* Sanity check to catch more bad SRATs (they are amazingly common).
  188. Make sure the PXMs cover all memory. */
  189. static int nodes_cover_memory(void)
  190. {
  191. int i;
  192. unsigned long pxmram, e820ram;
  193. pxmram = 0;
  194. for_each_node_mask(i, nodes_parsed) {
  195. unsigned long s = nodes[i].start >> PAGE_SHIFT;
  196. unsigned long e = nodes[i].end >> PAGE_SHIFT;
  197. pxmram += e - s;
  198. pxmram -= e820_hole_size(s, e);
  199. }
  200. e820ram = end_pfn - e820_hole_size(0, end_pfn);
  201. if (pxmram < e820ram) {
  202. printk(KERN_ERR
  203. "SRAT: PXMs only cover %luMB of your %luMB e820 RAM. Not used.\n",
  204. (pxmram << PAGE_SHIFT) >> 20,
  205. (e820ram << PAGE_SHIFT) >> 20);
  206. return 0;
  207. }
  208. return 1;
  209. }
  210. static void unparse_node(int node)
  211. {
  212. int i;
  213. node_clear(node, nodes_parsed);
  214. for (i = 0; i < MAX_LOCAL_APIC; i++) {
  215. if (apicid_to_node[i] == node)
  216. apicid_to_node[i] = NUMA_NO_NODE;
  217. }
  218. }
  219. void __init acpi_numa_arch_fixup(void) {}
  220. /* Use the information discovered above to actually set up the nodes. */
  221. int __init acpi_scan_nodes(unsigned long start, unsigned long end)
  222. {
  223. int i;
  224. /* First clean up the node list */
  225. for (i = 0; i < MAX_NUMNODES; i++) {
  226. cutoff_node(i, start, end);
  227. if ((nodes[i].end - nodes[i].start) < NODE_MIN_SIZE)
  228. unparse_node(i);
  229. }
  230. if (acpi_numa <= 0)
  231. return -1;
  232. if (!nodes_cover_memory()) {
  233. bad_srat();
  234. return -1;
  235. }
  236. memnode_shift = compute_hash_shift(nodes, nodes_weight(nodes_parsed));
  237. if (memnode_shift < 0) {
  238. printk(KERN_ERR
  239. "SRAT: No NUMA node hash function found. Contact maintainer\n");
  240. bad_srat();
  241. return -1;
  242. }
  243. /* Finally register nodes */
  244. for_each_node_mask(i, nodes_parsed)
  245. setup_node_bootmem(i, nodes[i].start, nodes[i].end);
  246. for (i = 0; i < NR_CPUS; i++) {
  247. if (cpu_to_node[i] == NUMA_NO_NODE)
  248. continue;
  249. if (!node_isset(cpu_to_node[i], nodes_parsed))
  250. numa_set_node(i, NUMA_NO_NODE);
  251. }
  252. numa_init_array();
  253. return 0;
  254. }
  255. static int node_to_pxm(int n)
  256. {
  257. int i;
  258. if (pxm2node[n] == n)
  259. return n;
  260. for (i = 0; i < 256; i++)
  261. if (pxm2node[i] == n)
  262. return i;
  263. return 0;
  264. }
  265. int __node_distance(int a, int b)
  266. {
  267. int index;
  268. if (!acpi_slit)
  269. return a == b ? 10 : 20;
  270. index = acpi_slit->localities * node_to_pxm(a);
  271. return acpi_slit->entry[index + node_to_pxm(b)];
  272. }
  273. EXPORT_SYMBOL(__node_distance);