srat_64.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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 <linux/bootmem.h>
  18. #include <linux/mm.h>
  19. #include <asm/proto.h>
  20. #include <asm/numa.h>
  21. #include <asm/e820.h>
  22. #include <asm/apic.h>
  23. #include <asm/uv/uv.h>
  24. int acpi_numa __initdata;
  25. static struct acpi_table_slit *acpi_slit;
  26. static nodemask_t nodes_parsed __initdata;
  27. static nodemask_t cpu_nodes_parsed __initdata;
  28. static struct bootnode nodes[MAX_NUMNODES] __initdata;
  29. static struct bootnode nodes_add[MAX_NUMNODES];
  30. static int num_node_memblks __initdata;
  31. static struct bootnode node_memblk_range[NR_NODE_MEMBLKS] __initdata;
  32. static int memblk_nodeid[NR_NODE_MEMBLKS] __initdata;
  33. static __init int setup_node(int pxm)
  34. {
  35. return acpi_map_pxm_to_node(pxm);
  36. }
  37. static __init int conflicting_memblks(unsigned long start, unsigned long end)
  38. {
  39. int i;
  40. for (i = 0; i < num_node_memblks; i++) {
  41. struct bootnode *nd = &node_memblk_range[i];
  42. if (nd->start == nd->end)
  43. continue;
  44. if (nd->end > start && nd->start < end)
  45. return memblk_nodeid[i];
  46. if (nd->end == end && nd->start == start)
  47. return memblk_nodeid[i];
  48. }
  49. return -1;
  50. }
  51. static __init void cutoff_node(int i, unsigned long start, unsigned long end)
  52. {
  53. struct bootnode *nd = &nodes[i];
  54. if (nd->start < start) {
  55. nd->start = start;
  56. if (nd->end < nd->start)
  57. nd->start = nd->end;
  58. }
  59. if (nd->end > end) {
  60. nd->end = end;
  61. if (nd->start > nd->end)
  62. nd->start = nd->end;
  63. }
  64. }
  65. static __init void bad_srat(void)
  66. {
  67. int i;
  68. printk(KERN_ERR "SRAT: SRAT not used.\n");
  69. acpi_numa = -1;
  70. for (i = 0; i < MAX_LOCAL_APIC; i++)
  71. apicid_to_node[i] = NUMA_NO_NODE;
  72. for (i = 0; i < MAX_NUMNODES; i++) {
  73. nodes[i].start = nodes[i].end = 0;
  74. nodes_add[i].start = nodes_add[i].end = 0;
  75. }
  76. remove_all_active_ranges();
  77. }
  78. static __init inline int srat_disabled(void)
  79. {
  80. return numa_off || acpi_numa < 0;
  81. }
  82. /* Callback for SLIT parsing */
  83. void __init acpi_numa_slit_init(struct acpi_table_slit *slit)
  84. {
  85. unsigned length;
  86. unsigned long phys;
  87. length = slit->header.length;
  88. phys = find_e820_area(0, max_pfn_mapped<<PAGE_SHIFT, length,
  89. PAGE_SIZE);
  90. if (phys == -1L)
  91. panic(" Can not save slit!\n");
  92. acpi_slit = __va(phys);
  93. memcpy(acpi_slit, slit, length);
  94. reserve_early(phys, phys + length, "ACPI SLIT");
  95. }
  96. /* Callback for Proximity Domain -> x2APIC mapping */
  97. void __init
  98. acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity *pa)
  99. {
  100. int pxm, node;
  101. int apic_id;
  102. if (srat_disabled())
  103. return;
  104. if (pa->header.length < sizeof(struct acpi_srat_x2apic_cpu_affinity)) {
  105. bad_srat();
  106. return;
  107. }
  108. if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0)
  109. return;
  110. pxm = pa->proximity_domain;
  111. node = setup_node(pxm);
  112. if (node < 0) {
  113. printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm);
  114. bad_srat();
  115. return;
  116. }
  117. apic_id = pa->apic_id;
  118. apicid_to_node[apic_id] = node;
  119. node_set(node, cpu_nodes_parsed);
  120. acpi_numa = 1;
  121. printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%04x -> Node %u\n",
  122. pxm, apic_id, node);
  123. }
  124. /* Callback for Proximity Domain -> LAPIC mapping */
  125. void __init
  126. acpi_numa_processor_affinity_init(struct acpi_srat_cpu_affinity *pa)
  127. {
  128. int pxm, node;
  129. int apic_id;
  130. if (srat_disabled())
  131. return;
  132. if (pa->header.length != sizeof(struct acpi_srat_cpu_affinity)) {
  133. bad_srat();
  134. return;
  135. }
  136. if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0)
  137. return;
  138. pxm = pa->proximity_domain_lo;
  139. node = setup_node(pxm);
  140. if (node < 0) {
  141. printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm);
  142. bad_srat();
  143. return;
  144. }
  145. if (get_uv_system_type() >= UV_X2APIC)
  146. apic_id = (pa->apic_id << 8) | pa->local_sapic_eid;
  147. else
  148. apic_id = pa->apic_id;
  149. apicid_to_node[apic_id] = node;
  150. node_set(node, cpu_nodes_parsed);
  151. acpi_numa = 1;
  152. printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%02x -> Node %u\n",
  153. pxm, apic_id, node);
  154. }
  155. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  156. static inline int save_add_info(void) {return 1;}
  157. #else
  158. static inline int save_add_info(void) {return 0;}
  159. #endif
  160. /*
  161. * Update nodes_add[]
  162. * This code supports one contiguous hot add area per node
  163. */
  164. static void __init
  165. update_nodes_add(int node, unsigned long start, unsigned long end)
  166. {
  167. unsigned long s_pfn = start >> PAGE_SHIFT;
  168. unsigned long e_pfn = end >> PAGE_SHIFT;
  169. int changed = 0;
  170. struct bootnode *nd = &nodes_add[node];
  171. /* I had some trouble with strange memory hotadd regions breaking
  172. the boot. Be very strict here and reject anything unexpected.
  173. If you want working memory hotadd write correct SRATs.
  174. The node size check is a basic sanity check to guard against
  175. mistakes */
  176. if ((signed long)(end - start) < NODE_MIN_SIZE) {
  177. printk(KERN_ERR "SRAT: Hotplug area too small\n");
  178. return;
  179. }
  180. /* This check might be a bit too strict, but I'm keeping it for now. */
  181. if (absent_pages_in_range(s_pfn, e_pfn) != e_pfn - s_pfn) {
  182. printk(KERN_ERR
  183. "SRAT: Hotplug area %lu -> %lu has existing memory\n",
  184. s_pfn, e_pfn);
  185. return;
  186. }
  187. /* Looks good */
  188. if (nd->start == nd->end) {
  189. nd->start = start;
  190. nd->end = end;
  191. changed = 1;
  192. } else {
  193. if (nd->start == end) {
  194. nd->start = start;
  195. changed = 1;
  196. }
  197. if (nd->end == start) {
  198. nd->end = end;
  199. changed = 1;
  200. }
  201. if (!changed)
  202. printk(KERN_ERR "SRAT: Hotplug zone not continuous. Partly ignored\n");
  203. }
  204. if (changed)
  205. printk(KERN_INFO "SRAT: hot plug zone found %Lx - %Lx\n",
  206. nd->start, nd->end);
  207. }
  208. /* Callback for parsing of the Proximity Domain <-> Memory Area mappings */
  209. void __init
  210. acpi_numa_memory_affinity_init(struct acpi_srat_mem_affinity *ma)
  211. {
  212. struct bootnode *nd, oldnode;
  213. unsigned long start, end;
  214. int node, pxm;
  215. int i;
  216. if (srat_disabled())
  217. return;
  218. if (ma->header.length != sizeof(struct acpi_srat_mem_affinity)) {
  219. bad_srat();
  220. return;
  221. }
  222. if ((ma->flags & ACPI_SRAT_MEM_ENABLED) == 0)
  223. return;
  224. if ((ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) && !save_add_info())
  225. return;
  226. start = ma->base_address;
  227. end = start + ma->length;
  228. pxm = ma->proximity_domain;
  229. node = setup_node(pxm);
  230. if (node < 0) {
  231. printk(KERN_ERR "SRAT: Too many proximity domains.\n");
  232. bad_srat();
  233. return;
  234. }
  235. i = conflicting_memblks(start, end);
  236. if (i == node) {
  237. printk(KERN_WARNING
  238. "SRAT: Warning: PXM %d (%lx-%lx) overlaps with itself (%Lx-%Lx)\n",
  239. pxm, start, end, nodes[i].start, nodes[i].end);
  240. } else if (i >= 0) {
  241. printk(KERN_ERR
  242. "SRAT: PXM %d (%lx-%lx) overlaps with PXM %d (%Lx-%Lx)\n",
  243. pxm, start, end, node_to_pxm(i),
  244. nodes[i].start, nodes[i].end);
  245. bad_srat();
  246. return;
  247. }
  248. nd = &nodes[node];
  249. oldnode = *nd;
  250. if (!node_test_and_set(node, nodes_parsed)) {
  251. nd->start = start;
  252. nd->end = end;
  253. } else {
  254. if (start < nd->start)
  255. nd->start = start;
  256. if (nd->end < end)
  257. nd->end = end;
  258. }
  259. printk(KERN_INFO "SRAT: Node %u PXM %u %lx-%lx\n", node, pxm,
  260. start, end);
  261. if (ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) {
  262. update_nodes_add(node, start, end);
  263. /* restore nodes[node] */
  264. *nd = oldnode;
  265. if ((nd->start | nd->end) == 0)
  266. node_clear(node, nodes_parsed);
  267. }
  268. node_memblk_range[num_node_memblks].start = start;
  269. node_memblk_range[num_node_memblks].end = end;
  270. memblk_nodeid[num_node_memblks] = node;
  271. num_node_memblks++;
  272. }
  273. /* Sanity check to catch more bad SRATs (they are amazingly common).
  274. Make sure the PXMs cover all memory. */
  275. static int __init nodes_cover_memory(const struct bootnode *nodes)
  276. {
  277. int i;
  278. unsigned long pxmram, e820ram;
  279. pxmram = 0;
  280. for_each_node_mask(i, nodes_parsed) {
  281. unsigned long s = nodes[i].start >> PAGE_SHIFT;
  282. unsigned long e = nodes[i].end >> PAGE_SHIFT;
  283. pxmram += e - s;
  284. pxmram -= __absent_pages_in_range(i, s, e);
  285. if ((long)pxmram < 0)
  286. pxmram = 0;
  287. }
  288. e820ram = max_pfn - (e820_hole_size(0, max_pfn<<PAGE_SHIFT)>>PAGE_SHIFT);
  289. /* We seem to lose 3 pages somewhere. Allow 1M of slack. */
  290. if ((long)(e820ram - pxmram) >= (1<<(20 - PAGE_SHIFT))) {
  291. printk(KERN_ERR
  292. "SRAT: PXMs only cover %luMB of your %luMB e820 RAM. Not used.\n",
  293. (pxmram << PAGE_SHIFT) >> 20,
  294. (e820ram << PAGE_SHIFT) >> 20);
  295. return 0;
  296. }
  297. return 1;
  298. }
  299. void __init acpi_numa_arch_fixup(void) {}
  300. int __init acpi_get_nodes(struct bootnode *physnodes)
  301. {
  302. int i;
  303. int ret = 0;
  304. for_each_node_mask(i, nodes_parsed) {
  305. physnodes[ret].start = nodes[i].start;
  306. physnodes[ret].end = nodes[i].end;
  307. ret++;
  308. }
  309. return ret;
  310. }
  311. /* Use the information discovered above to actually set up the nodes. */
  312. int __init acpi_scan_nodes(unsigned long start, unsigned long end)
  313. {
  314. int i;
  315. if (acpi_numa <= 0)
  316. return -1;
  317. /* First clean up the node list */
  318. for (i = 0; i < MAX_NUMNODES; i++)
  319. cutoff_node(i, start, end);
  320. memnode_shift = compute_hash_shift(node_memblk_range, num_node_memblks,
  321. memblk_nodeid);
  322. if (memnode_shift < 0) {
  323. printk(KERN_ERR
  324. "SRAT: No NUMA node hash function found. Contact maintainer\n");
  325. bad_srat();
  326. return -1;
  327. }
  328. for_each_node_mask(i, nodes_parsed)
  329. e820_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
  330. nodes[i].end >> PAGE_SHIFT);
  331. /* for out of order entries in SRAT */
  332. sort_node_map();
  333. if (!nodes_cover_memory(nodes)) {
  334. bad_srat();
  335. return -1;
  336. }
  337. /* Account for nodes with cpus and no memory */
  338. nodes_or(node_possible_map, nodes_parsed, cpu_nodes_parsed);
  339. /* Finally register nodes */
  340. for_each_node_mask(i, node_possible_map)
  341. setup_node_bootmem(i, nodes[i].start, nodes[i].end);
  342. /* Try again in case setup_node_bootmem missed one due
  343. to missing bootmem */
  344. for_each_node_mask(i, node_possible_map)
  345. if (!node_online(i))
  346. setup_node_bootmem(i, nodes[i].start, nodes[i].end);
  347. for (i = 0; i < nr_cpu_ids; i++) {
  348. int node = early_cpu_to_node(i);
  349. if (node == NUMA_NO_NODE)
  350. continue;
  351. if (!node_online(node))
  352. numa_clear_node(i);
  353. }
  354. numa_init_array();
  355. return 0;
  356. }
  357. #ifdef CONFIG_NUMA_EMU
  358. static int fake_node_to_pxm_map[MAX_NUMNODES] __initdata = {
  359. [0 ... MAX_NUMNODES-1] = PXM_INVAL
  360. };
  361. static s16 fake_apicid_to_node[MAX_LOCAL_APIC] __initdata = {
  362. [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
  363. };
  364. static int __init find_node_by_addr(unsigned long addr)
  365. {
  366. int ret = NUMA_NO_NODE;
  367. int i;
  368. for_each_node_mask(i, nodes_parsed) {
  369. /*
  370. * Find the real node that this emulated node appears on. For
  371. * the sake of simplicity, we only use a real node's starting
  372. * address to determine which emulated node it appears on.
  373. */
  374. if (addr >= nodes[i].start && addr < nodes[i].end) {
  375. ret = i;
  376. break;
  377. }
  378. }
  379. return ret;
  380. }
  381. /*
  382. * In NUMA emulation, we need to setup proximity domain (_PXM) to node ID
  383. * mappings that respect the real ACPI topology but reflect our emulated
  384. * environment. For each emulated node, we find which real node it appears on
  385. * and create PXM to NID mappings for those fake nodes which mirror that
  386. * locality. SLIT will now represent the correct distances between emulated
  387. * nodes as a result of the real topology.
  388. */
  389. void __init acpi_fake_nodes(const struct bootnode *fake_nodes, int num_nodes)
  390. {
  391. int i, j;
  392. printk(KERN_INFO "Faking PXM affinity for fake nodes on real "
  393. "topology.\n");
  394. for (i = 0; i < num_nodes; i++) {
  395. int nid, pxm;
  396. nid = find_node_by_addr(fake_nodes[i].start);
  397. if (nid == NUMA_NO_NODE)
  398. continue;
  399. pxm = node_to_pxm(nid);
  400. if (pxm == PXM_INVAL)
  401. continue;
  402. fake_node_to_pxm_map[i] = pxm;
  403. /*
  404. * For each apicid_to_node mapping that exists for this real
  405. * node, it must now point to the fake node ID.
  406. */
  407. for (j = 0; j < MAX_LOCAL_APIC; j++)
  408. if (apicid_to_node[j] == nid)
  409. fake_apicid_to_node[j] = i;
  410. }
  411. for (i = 0; i < num_nodes; i++)
  412. __acpi_map_pxm_to_node(fake_node_to_pxm_map[i], i);
  413. memcpy(apicid_to_node, fake_apicid_to_node, sizeof(apicid_to_node));
  414. nodes_clear(nodes_parsed);
  415. for (i = 0; i < num_nodes; i++)
  416. if (fake_nodes[i].start != fake_nodes[i].end)
  417. node_set(i, nodes_parsed);
  418. }
  419. static int null_slit_node_compare(int a, int b)
  420. {
  421. return node_to_pxm(a) == node_to_pxm(b);
  422. }
  423. #else
  424. static int null_slit_node_compare(int a, int b)
  425. {
  426. return a == b;
  427. }
  428. #endif /* CONFIG_NUMA_EMU */
  429. int __node_distance(int a, int b)
  430. {
  431. int index;
  432. if (!acpi_slit)
  433. return null_slit_node_compare(a, b) ? LOCAL_DISTANCE :
  434. REMOTE_DISTANCE;
  435. index = acpi_slit->locality_count * node_to_pxm(a);
  436. return acpi_slit->entry[index + node_to_pxm(b)];
  437. }
  438. EXPORT_SYMBOL(__node_distance);
  439. #if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) || defined(CONFIG_ACPI_HOTPLUG_MEMORY)
  440. int memory_add_physaddr_to_nid(u64 start)
  441. {
  442. int i, ret = 0;
  443. for_each_node(i)
  444. if (nodes_add[i].start <= start && nodes_add[i].end > start)
  445. ret = i;
  446. return ret;
  447. }
  448. EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
  449. #endif