srat_64.c 14 KB

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