srat_64.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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. node_set(node, cpu_nodes_parsed);
  206. printk(KERN_INFO "SRAT: hot plug zone found %Lx - %Lx\n",
  207. nd->start, nd->end);
  208. }
  209. }
  210. /* Callback for parsing of the Proximity Domain <-> Memory Area mappings */
  211. void __init
  212. acpi_numa_memory_affinity_init(struct acpi_srat_mem_affinity *ma)
  213. {
  214. struct bootnode *nd, oldnode;
  215. unsigned long start, end;
  216. int node, pxm;
  217. int i;
  218. if (srat_disabled())
  219. return;
  220. if (ma->header.length != sizeof(struct acpi_srat_mem_affinity)) {
  221. bad_srat();
  222. return;
  223. }
  224. if ((ma->flags & ACPI_SRAT_MEM_ENABLED) == 0)
  225. return;
  226. if ((ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) && !save_add_info())
  227. return;
  228. start = ma->base_address;
  229. end = start + ma->length;
  230. pxm = ma->proximity_domain;
  231. node = setup_node(pxm);
  232. if (node < 0) {
  233. printk(KERN_ERR "SRAT: Too many proximity domains.\n");
  234. bad_srat();
  235. return;
  236. }
  237. i = conflicting_memblks(start, end);
  238. if (i == node) {
  239. printk(KERN_WARNING
  240. "SRAT: Warning: PXM %d (%lx-%lx) overlaps with itself (%Lx-%Lx)\n",
  241. pxm, start, end, nodes[i].start, nodes[i].end);
  242. } else if (i >= 0) {
  243. printk(KERN_ERR
  244. "SRAT: PXM %d (%lx-%lx) overlaps with PXM %d (%Lx-%Lx)\n",
  245. pxm, start, end, node_to_pxm(i),
  246. nodes[i].start, nodes[i].end);
  247. bad_srat();
  248. return;
  249. }
  250. nd = &nodes[node];
  251. oldnode = *nd;
  252. if (!node_test_and_set(node, nodes_parsed)) {
  253. nd->start = start;
  254. nd->end = end;
  255. } else {
  256. if (start < nd->start)
  257. nd->start = start;
  258. if (nd->end < end)
  259. nd->end = end;
  260. }
  261. printk(KERN_INFO "SRAT: Node %u PXM %u %lx-%lx\n", node, pxm,
  262. start, end);
  263. if (ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) {
  264. update_nodes_add(node, start, end);
  265. /* restore nodes[node] */
  266. *nd = oldnode;
  267. if ((nd->start | nd->end) == 0)
  268. node_clear(node, nodes_parsed);
  269. }
  270. node_memblk_range[num_node_memblks].start = start;
  271. node_memblk_range[num_node_memblks].end = end;
  272. memblk_nodeid[num_node_memblks] = node;
  273. num_node_memblks++;
  274. }
  275. /* Sanity check to catch more bad SRATs (they are amazingly common).
  276. Make sure the PXMs cover all memory. */
  277. static int __init nodes_cover_memory(const struct bootnode *nodes)
  278. {
  279. int i;
  280. unsigned long pxmram, e820ram;
  281. pxmram = 0;
  282. for_each_node_mask(i, nodes_parsed) {
  283. unsigned long s = nodes[i].start >> PAGE_SHIFT;
  284. unsigned long e = nodes[i].end >> PAGE_SHIFT;
  285. pxmram += e - s;
  286. pxmram -= __absent_pages_in_range(i, s, e);
  287. if ((long)pxmram < 0)
  288. pxmram = 0;
  289. }
  290. e820ram = max_pfn - (e820_hole_size(0, max_pfn<<PAGE_SHIFT)>>PAGE_SHIFT);
  291. /* We seem to lose 3 pages somewhere. Allow 1M of slack. */
  292. if ((long)(e820ram - pxmram) >= (1<<(20 - PAGE_SHIFT))) {
  293. printk(KERN_ERR
  294. "SRAT: PXMs only cover %luMB of your %luMB e820 RAM. Not used.\n",
  295. (pxmram << PAGE_SHIFT) >> 20,
  296. (e820ram << PAGE_SHIFT) >> 20);
  297. return 0;
  298. }
  299. return 1;
  300. }
  301. void __init acpi_numa_arch_fixup(void) {}
  302. int __init acpi_get_nodes(struct bootnode *physnodes)
  303. {
  304. int i;
  305. int ret = 0;
  306. for_each_node_mask(i, nodes_parsed) {
  307. physnodes[ret].start = nodes[i].start;
  308. physnodes[ret].end = nodes[i].end;
  309. ret++;
  310. }
  311. return ret;
  312. }
  313. /* Use the information discovered above to actually set up the nodes. */
  314. int __init acpi_scan_nodes(unsigned long start, unsigned long end)
  315. {
  316. int i;
  317. if (acpi_numa <= 0)
  318. return -1;
  319. /* First clean up the node list */
  320. for (i = 0; i < MAX_NUMNODES; i++)
  321. cutoff_node(i, start, end);
  322. /*
  323. * Join together blocks on the same node, holes between
  324. * which don't overlap with memory on other nodes.
  325. */
  326. for (i = 0; i < num_node_memblks; ++i) {
  327. int j, k;
  328. for (j = i + 1; j < num_node_memblks; ++j) {
  329. unsigned long start, end;
  330. if (memblk_nodeid[i] != memblk_nodeid[j])
  331. continue;
  332. start = min(node_memblk_range[i].end,
  333. node_memblk_range[j].end);
  334. end = max(node_memblk_range[i].start,
  335. node_memblk_range[j].start);
  336. for (k = 0; k < num_node_memblks; ++k) {
  337. if (memblk_nodeid[i] == memblk_nodeid[k])
  338. continue;
  339. if (start < node_memblk_range[k].end &&
  340. end > node_memblk_range[k].start)
  341. break;
  342. }
  343. if (k < num_node_memblks)
  344. continue;
  345. start = min(node_memblk_range[i].start,
  346. node_memblk_range[j].start);
  347. end = max(node_memblk_range[i].end,
  348. node_memblk_range[j].end);
  349. printk(KERN_INFO "SRAT: Node %d "
  350. "[%Lx,%Lx) + [%Lx,%Lx) -> [%lx,%lx)\n",
  351. memblk_nodeid[i],
  352. node_memblk_range[i].start,
  353. node_memblk_range[i].end,
  354. node_memblk_range[j].start,
  355. node_memblk_range[j].end,
  356. start, end);
  357. node_memblk_range[i].start = start;
  358. node_memblk_range[i].end = end;
  359. k = --num_node_memblks - j;
  360. memmove(memblk_nodeid + j, memblk_nodeid + j+1,
  361. k * sizeof(*memblk_nodeid));
  362. memmove(node_memblk_range + j, node_memblk_range + j+1,
  363. k * sizeof(*node_memblk_range));
  364. --j;
  365. }
  366. }
  367. memnode_shift = compute_hash_shift(node_memblk_range, num_node_memblks,
  368. memblk_nodeid);
  369. if (memnode_shift < 0) {
  370. printk(KERN_ERR
  371. "SRAT: No NUMA node hash function found. Contact maintainer\n");
  372. bad_srat();
  373. return -1;
  374. }
  375. for_each_node_mask(i, nodes_parsed)
  376. e820_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
  377. nodes[i].end >> PAGE_SHIFT);
  378. /* for out of order entries in SRAT */
  379. sort_node_map();
  380. if (!nodes_cover_memory(nodes)) {
  381. bad_srat();
  382. return -1;
  383. }
  384. /* Account for nodes with cpus and no memory */
  385. nodes_or(node_possible_map, nodes_parsed, cpu_nodes_parsed);
  386. /* Finally register nodes */
  387. for_each_node_mask(i, node_possible_map)
  388. setup_node_bootmem(i, nodes[i].start, nodes[i].end);
  389. /* Try again in case setup_node_bootmem missed one due
  390. to missing bootmem */
  391. for_each_node_mask(i, node_possible_map)
  392. if (!node_online(i))
  393. setup_node_bootmem(i, nodes[i].start, nodes[i].end);
  394. for (i = 0; i < nr_cpu_ids; i++) {
  395. int node = early_cpu_to_node(i);
  396. if (node == NUMA_NO_NODE)
  397. continue;
  398. if (!node_online(node))
  399. numa_clear_node(i);
  400. }
  401. numa_init_array();
  402. return 0;
  403. }
  404. #ifdef CONFIG_NUMA_EMU
  405. static int fake_node_to_pxm_map[MAX_NUMNODES] __initdata = {
  406. [0 ... MAX_NUMNODES-1] = PXM_INVAL
  407. };
  408. static s16 fake_apicid_to_node[MAX_LOCAL_APIC] __initdata = {
  409. [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
  410. };
  411. static int __init find_node_by_addr(unsigned long addr)
  412. {
  413. int ret = NUMA_NO_NODE;
  414. int i;
  415. for_each_node_mask(i, nodes_parsed) {
  416. /*
  417. * Find the real node that this emulated node appears on. For
  418. * the sake of simplicity, we only use a real node's starting
  419. * address to determine which emulated node it appears on.
  420. */
  421. if (addr >= nodes[i].start && addr < nodes[i].end) {
  422. ret = i;
  423. break;
  424. }
  425. }
  426. return ret;
  427. }
  428. /*
  429. * In NUMA emulation, we need to setup proximity domain (_PXM) to node ID
  430. * mappings that respect the real ACPI topology but reflect our emulated
  431. * environment. For each emulated node, we find which real node it appears on
  432. * and create PXM to NID mappings for those fake nodes which mirror that
  433. * locality. SLIT will now represent the correct distances between emulated
  434. * nodes as a result of the real topology.
  435. */
  436. void __init acpi_fake_nodes(const struct bootnode *fake_nodes, int num_nodes)
  437. {
  438. int i, j;
  439. printk(KERN_INFO "Faking PXM affinity for fake nodes on real "
  440. "topology.\n");
  441. for (i = 0; i < num_nodes; i++) {
  442. int nid, pxm;
  443. nid = find_node_by_addr(fake_nodes[i].start);
  444. if (nid == NUMA_NO_NODE)
  445. continue;
  446. pxm = node_to_pxm(nid);
  447. if (pxm == PXM_INVAL)
  448. continue;
  449. fake_node_to_pxm_map[i] = pxm;
  450. /*
  451. * For each apicid_to_node mapping that exists for this real
  452. * node, it must now point to the fake node ID.
  453. */
  454. for (j = 0; j < MAX_LOCAL_APIC; j++)
  455. if (apicid_to_node[j] == nid &&
  456. fake_apicid_to_node[j] == NUMA_NO_NODE)
  457. fake_apicid_to_node[j] = i;
  458. }
  459. for (i = 0; i < num_nodes; i++)
  460. __acpi_map_pxm_to_node(fake_node_to_pxm_map[i], i);
  461. memcpy(apicid_to_node, fake_apicid_to_node, sizeof(apicid_to_node));
  462. nodes_clear(nodes_parsed);
  463. for (i = 0; i < num_nodes; i++)
  464. if (fake_nodes[i].start != fake_nodes[i].end)
  465. node_set(i, nodes_parsed);
  466. }
  467. static int null_slit_node_compare(int a, int b)
  468. {
  469. return node_to_pxm(a) == node_to_pxm(b);
  470. }
  471. #else
  472. static int null_slit_node_compare(int a, int b)
  473. {
  474. return a == b;
  475. }
  476. #endif /* CONFIG_NUMA_EMU */
  477. int __node_distance(int a, int b)
  478. {
  479. int index;
  480. if (!acpi_slit)
  481. return null_slit_node_compare(a, b) ? LOCAL_DISTANCE :
  482. REMOTE_DISTANCE;
  483. index = acpi_slit->locality_count * node_to_pxm(a);
  484. return acpi_slit->entry[index + node_to_pxm(b)];
  485. }
  486. EXPORT_SYMBOL(__node_distance);
  487. #if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) || defined(CONFIG_ACPI_HOTPLUG_MEMORY)
  488. int memory_add_physaddr_to_nid(u64 start)
  489. {
  490. int i, ret = 0;
  491. for_each_node(i)
  492. if (nodes_add[i].start <= start && nodes_add[i].end > start)
  493. ret = i;
  494. return ret;
  495. }
  496. EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
  497. #endif