2 * ACPI 3.0 based NUMA setup
3 * Copyright 2004 Andi Kleen, SuSE Labs.
5 * Reads the ACPI SRAT table to figure out what memory belongs to which CPUs.
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.
12 #include <linux/kernel.h>
13 #include <linux/acpi.h>
14 #include <linux/mmzone.h>
15 #include <linux/bitmap.h>
16 #include <linux/module.h>
17 #include <linux/topology.h>
18 #include <linux/bootmem.h>
20 #include <asm/proto.h>
24 #if (defined(CONFIG_ACPI_HOTPLUG_MEMORY) || \
25 defined(CONFIG_ACPI_HOTPLUG_MEMORY_MODULE)) \
26 && !defined(CONFIG_MEMORY_HOTPLUG)
27 #define RESERVE_HOTADD 1
30 static struct acpi_table_slit *acpi_slit;
32 static nodemask_t nodes_parsed __initdata;
33 static nodemask_t nodes_found __initdata;
34 static struct bootnode nodes[MAX_NUMNODES] __initdata;
35 static struct bootnode nodes_add[MAX_NUMNODES] __initdata;
36 static int found_add_area __initdata;
37 int hotadd_percent __initdata = 0;
38 #ifndef RESERVE_HOTADD
39 #define hotadd_percent 0 /* Ignore all settings */
41 static u8 pxm2node[256] = { [0 ... 255] = 0xff };
43 /* Too small nodes confuse the VM badly. Usually they result
45 #define NODE_MIN_SIZE (4*1024*1024)
47 static int node_to_pxm(int n);
49 int pxm_to_node(int pxm)
51 if ((unsigned)pxm >= 256)
53 /* Extend 0xff to (int)-1 */
54 return (signed char)pxm2node[pxm];
57 static __init int setup_node(int pxm)
59 unsigned node = pxm2node[pxm];
61 if (nodes_weight(nodes_found) >= MAX_NUMNODES)
63 node = first_unset_node(nodes_found);
64 node_set(node, nodes_found);
70 static __init int conflicting_nodes(unsigned long start, unsigned long end)
73 for_each_node_mask(i, nodes_parsed) {
74 struct bootnode *nd = &nodes[i];
75 if (nd->start == nd->end)
77 if (nd->end > start && nd->start < end)
79 if (nd->end == end && nd->start == start)
85 static __init void cutoff_node(int i, unsigned long start, unsigned long end)
87 struct bootnode *nd = &nodes[i];
92 if (nd->start < start) {
94 if (nd->end < nd->start)
99 if (nd->start > nd->end)
104 static __init void bad_srat(void)
107 printk(KERN_ERR "SRAT: SRAT not used.\n");
110 for (i = 0; i < MAX_LOCAL_APIC; i++)
111 apicid_to_node[i] = NUMA_NO_NODE;
112 for (i = 0; i < MAX_NUMNODES; i++)
113 nodes_add[i].start = nodes[i].end = 0;
116 static __init inline int srat_disabled(void)
118 return numa_off || acpi_numa < 0;
122 * A lot of BIOS fill in 10 (= no distance) everywhere. This messes
123 * up the NUMA heuristics which wants the local node to have a smaller
124 * distance than the others.
125 * Do some quick checks here and only use the SLIT if it passes.
127 static __init int slit_valid(struct acpi_table_slit *slit)
130 int d = slit->localities;
131 for (i = 0; i < d; i++) {
132 for (j = 0; j < d; j++) {
133 u8 val = slit->entry[d*i + j];
137 } else if (val <= 10)
144 /* Callback for SLIT parsing */
145 void __init acpi_numa_slit_init(struct acpi_table_slit *slit)
147 if (!slit_valid(slit)) {
148 printk(KERN_INFO "ACPI: SLIT table looks invalid. Not used.\n");
154 /* Callback for Proximity Domain -> LAPIC mapping */
156 acpi_numa_processor_affinity_init(struct acpi_table_processor_affinity *pa)
161 if (pa->header.length != sizeof(struct acpi_table_processor_affinity)) {
165 if (pa->flags.enabled == 0)
167 pxm = pa->proximity_domain;
168 node = setup_node(pxm);
170 printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm);
174 apicid_to_node[pa->apic_id] = node;
176 printk(KERN_INFO "SRAT: PXM %u -> APIC %u -> Node %u\n",
177 pxm, pa->apic_id, node);
180 #ifdef RESERVE_HOTADD
182 * Protect against too large hotadd areas that would fill up memory.
184 static int hotadd_enough_memory(struct bootnode *nd)
186 static unsigned long allocated;
187 static unsigned long last_area_end;
188 unsigned long pages = (nd->end - nd->start) >> PAGE_SHIFT;
189 long mem = pages * sizeof(struct page);
191 unsigned long allowed;
192 unsigned long oldpages = pages;
196 allowed = (end_pfn - e820_hole_size(0, end_pfn)) * PAGE_SIZE;
197 allowed = (allowed / 100) * hotadd_percent;
198 if (allocated + mem > allowed) {
200 /* Give them at least part of their hotadd memory upto hotadd_percent
201 It would be better to spread the limit out
202 over multiple hotplug areas, but that is too complicated
204 if (allocated >= allowed)
206 range = allowed - allocated;
207 pages = (range / PAGE_SIZE);
208 mem = pages * sizeof(struct page);
209 nd->end = nd->start + range;
211 /* Not completely fool proof, but a good sanity check */
212 addr = find_e820_area(last_area_end, end_pfn<<PAGE_SHIFT, mem);
215 if (pages != oldpages)
216 printk(KERN_NOTICE "SRAT: Hotadd area limited to %lu bytes\n",
217 pages << PAGE_SHIFT);
218 last_area_end = addr + mem;
224 * It is fine to add this area to the nodes data it will be used later
225 * This code supports one contigious hot add area per node.
227 static int reserve_hotadd(int node, unsigned long start, unsigned long end)
229 unsigned long s_pfn = start >> PAGE_SHIFT;
230 unsigned long e_pfn = end >> PAGE_SHIFT;
232 struct bootnode *nd = &nodes_add[node];
234 /* I had some trouble with strange memory hotadd regions breaking
235 the boot. Be very strict here and reject anything unexpected.
236 If you want working memory hotadd write correct SRATs.
238 The node size check is a basic sanity check to guard against
240 if ((signed long)(end - start) < NODE_MIN_SIZE) {
241 printk(KERN_ERR "SRAT: Hotplug area too small\n");
245 /* This check might be a bit too strict, but I'm keeping it for now. */
246 if (e820_hole_size(s_pfn, e_pfn) != e_pfn - s_pfn) {
247 printk(KERN_ERR "SRAT: Hotplug area has existing memory\n");
251 if (!hotadd_enough_memory(&nodes_add[node])) {
252 printk(KERN_ERR "SRAT: Hotplug area too large\n");
259 if (nd->start == nd->end) {
264 if (nd->start == end) {
268 if (nd->end == start) {
273 printk(KERN_ERR "SRAT: Hotplug zone not continuous. Partly ignored\n");
276 if ((nd->end >> PAGE_SHIFT) > end_pfn)
277 end_pfn = nd->end >> PAGE_SHIFT;
280 printk(KERN_INFO "SRAT: hot plug zone found %Lx - %Lx\n", nd->start, nd->end);
285 /* Callback for parsing of the Proximity Domain <-> Memory Area mappings */
287 acpi_numa_memory_affinity_init(struct acpi_table_memory_affinity *ma)
289 struct bootnode *nd, oldnode;
290 unsigned long start, end;
296 if (ma->header.length != sizeof(struct acpi_table_memory_affinity)) {
300 if (ma->flags.enabled == 0)
302 if (ma->flags.hot_pluggable && hotadd_percent == 0)
304 start = ma->base_addr_lo | ((u64)ma->base_addr_hi << 32);
305 end = start + (ma->length_lo | ((u64)ma->length_hi << 32));
306 pxm = ma->proximity_domain;
307 node = setup_node(pxm);
309 printk(KERN_ERR "SRAT: Too many proximity domains.\n");
313 i = conflicting_nodes(start, end);
316 "SRAT: Warning: PXM %d (%lx-%lx) overlaps with itself (%Lx-%Lx)\n",
317 pxm, start, end, nodes[i].start, nodes[i].end);
320 "SRAT: PXM %d (%lx-%lx) overlaps with PXM %d (%Lx-%Lx)\n",
321 pxm, start, end, node_to_pxm(i),
322 nodes[i].start, nodes[i].end);
328 if (!node_test_and_set(node, nodes_parsed)) {
332 if (start < nd->start)
338 printk(KERN_INFO "SRAT: Node %u PXM %u %Lx-%Lx\n", node, pxm,
341 #ifdef RESERVE_HOTADD
342 if (ma->flags.hot_pluggable && reserve_hotadd(node, start, end) < 0) {
343 /* Ignore hotadd region. Undo damage */
344 printk(KERN_NOTICE "SRAT: Hotplug region ignored\n");
346 if ((nd->start | nd->end) == 0)
347 node_clear(node, nodes_parsed);
352 /* Sanity check to catch more bad SRATs (they are amazingly common).
353 Make sure the PXMs cover all memory. */
354 static int nodes_cover_memory(void)
357 unsigned long pxmram, e820ram;
360 for_each_node_mask(i, nodes_parsed) {
361 unsigned long s = nodes[i].start >> PAGE_SHIFT;
362 unsigned long e = nodes[i].end >> PAGE_SHIFT;
364 pxmram -= e820_hole_size(s, e);
365 pxmram -= nodes_add[i].end - nodes_add[i].start;
366 if ((long)pxmram < 0)
370 e820ram = end_pfn - e820_hole_size(0, end_pfn);
371 /* We seem to lose 3 pages somewhere. Allow a bit of slack. */
372 if ((long)(e820ram - pxmram) >= 1*1024*1024) {
374 "SRAT: PXMs only cover %luMB of your %luMB e820 RAM. Not used.\n",
375 (pxmram << PAGE_SHIFT) >> 20,
376 (e820ram << PAGE_SHIFT) >> 20);
382 static void unparse_node(int node)
385 node_clear(node, nodes_parsed);
386 for (i = 0; i < MAX_LOCAL_APIC; i++) {
387 if (apicid_to_node[i] == node)
388 apicid_to_node[i] = NUMA_NO_NODE;
392 void __init acpi_numa_arch_fixup(void) {}
394 /* Use the information discovered above to actually set up the nodes. */
395 int __init acpi_scan_nodes(unsigned long start, unsigned long end)
399 /* First clean up the node list */
400 for (i = 0; i < MAX_NUMNODES; i++) {
401 cutoff_node(i, start, end);
402 if ((nodes[i].end - nodes[i].start) < NODE_MIN_SIZE)
409 if (!nodes_cover_memory()) {
414 memnode_shift = compute_hash_shift(nodes, MAX_NUMNODES);
415 if (memnode_shift < 0) {
417 "SRAT: No NUMA node hash function found. Contact maintainer\n");
422 /* Finally register nodes */
423 for_each_node_mask(i, nodes_parsed)
424 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
425 /* Try again in case setup_node_bootmem missed one due
426 to missing bootmem */
427 for_each_node_mask(i, nodes_parsed)
429 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
431 for (i = 0; i < NR_CPUS; i++) {
432 if (cpu_to_node[i] == NUMA_NO_NODE)
434 if (!node_isset(cpu_to_node[i], nodes_parsed))
435 numa_set_node(i, NUMA_NO_NODE);
441 static int node_to_pxm(int n)
444 if (pxm2node[n] == n)
446 for (i = 0; i < 256; i++)
447 if (pxm2node[i] == n)
452 void __init srat_reserve_add_area(int nodeid)
454 if (found_add_area && nodes_add[nodeid].end) {
457 printk(KERN_INFO "SRAT: Reserving hot-add memory space "
458 "for node %d at %Lx-%Lx\n",
459 nodeid, nodes_add[nodeid].start, nodes_add[nodeid].end);
460 total_mb = (nodes_add[nodeid].end - nodes_add[nodeid].start)
462 total_mb *= sizeof(struct page);
464 printk(KERN_INFO "SRAT: This will cost you %Lu MB of "
465 "pre-allocated memory.\n", (unsigned long long)total_mb);
466 reserve_bootmem_node(NODE_DATA(nodeid), nodes_add[nodeid].start,
467 nodes_add[nodeid].end - nodes_add[nodeid].start);
471 int __node_distance(int a, int b)
476 return a == b ? 10 : 20;
477 index = acpi_slit->localities * node_to_pxm(a);
478 return acpi_slit->entry[index + node_to_pxm(b)];
481 EXPORT_SYMBOL(__node_distance);