blob: bfa4a6af5cfe5574d3de5135ac516bdf68cefa7c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
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>
Andi Kleen68a3a7f2006-04-07 19:49:18 +020018#include <linux/bootmem.h>
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070019#include <linux/memblock.h>
Andi Kleen68a3a7f2006-04-07 19:49:18 +020020#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/proto.h>
22#include <asm/numa.h>
Andi Kleen8a6fdd32006-01-11 22:44:39 +010023#include <asm/e820.h>
Ingo Molnar7b6aa332009-02-17 13:58:15 +010024#include <asm/apic.h>
Ingo Molnar4ec71fa2009-01-21 10:24:27 +010025#include <asm/uv/uv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Andi Kleenc31fbb12006-09-26 10:52:33 +020027int acpi_numa __initdata;
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static struct acpi_table_slit *acpi_slit;
30
Keith Mannthey4942e992006-09-30 23:27:06 -070031static struct bootnode nodes_add[MAX_NUMNODES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -070033static int num_node_memblks __initdata;
34static struct bootnode node_memblk_range[NR_NODE_MEMBLKS] __initdata;
35static int memblk_nodeid[NR_NODE_MEMBLKS] __initdata;
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037static __init int setup_node(int pxm)
38{
Yasunori Goto762834e2006-06-23 02:03:19 -070039 return acpi_map_pxm_to_node(pxm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -070042static __init int conflicting_memblks(unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 int i;
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -070045 for (i = 0; i < num_node_memblks; i++) {
46 struct bootnode *nd = &node_memblk_range[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 if (nd->start == nd->end)
48 continue;
49 if (nd->end > start && nd->start < end)
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -070050 return memblk_nodeid[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 if (nd->end == end && nd->start == start)
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -070052 return memblk_nodeid[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 }
54 return -1;
55}
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static __init void bad_srat(void)
58{
Andi Kleen2bce2b52005-09-12 18:49:25 +020059 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 printk(KERN_ERR "SRAT: SRAT not used.\n");
61 acpi_numa = -1;
Andi Kleen429b2b32009-07-18 08:56:57 +020062 for (i = 0; i < MAX_NUMNODES; i++) {
Tejun Heo206e4202011-02-16 12:13:07 +010063 numa_nodes[i].start = numa_nodes[i].end = 0;
Andi Kleen429b2b32009-07-18 08:56:57 +020064 nodes_add[i].start = nodes_add[i].end = 0;
65 }
Mel Gorman5cb248a2006-09-27 01:49:52 -070066 remove_all_active_ranges();
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
69static __init inline int srat_disabled(void)
70{
Tejun Heoffe77a42011-02-16 12:13:06 +010071 return acpi_numa < 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
74/* Callback for SLIT parsing */
75void __init acpi_numa_slit_init(struct acpi_table_slit *slit)
76{
Yinghai Luf302a5bb2008-07-10 20:36:37 -070077 unsigned length;
78 unsigned long phys;
79
80 length = slit->header.length;
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070081 phys = memblock_find_in_range(0, max_pfn_mapped<<PAGE_SHIFT, length,
Yinghai Luf302a5bb2008-07-10 20:36:37 -070082 PAGE_SIZE);
83
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070084 if (phys == MEMBLOCK_ERROR)
Yinghai Luf302a5bb2008-07-10 20:36:37 -070085 panic(" Can not save slit!\n");
86
87 acpi_slit = __va(phys);
88 memcpy(acpi_slit, slit, length);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070089 memblock_x86_reserve_range(phys, phys + length, "ACPI SLIT");
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
Suresh Siddha7237d3d2009-03-30 13:55:30 -080092/* Callback for Proximity Domain -> x2APIC mapping */
93void __init
94acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity *pa)
95{
96 int pxm, node;
97 int apic_id;
98
99 if (srat_disabled())
100 return;
101 if (pa->header.length < sizeof(struct acpi_srat_x2apic_cpu_affinity)) {
102 bad_srat();
103 return;
104 }
105 if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0)
106 return;
107 pxm = pa->proximity_domain;
108 node = setup_node(pxm);
109 if (node < 0) {
110 printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm);
111 bad_srat();
112 return;
113 }
114
115 apic_id = pa->apic_id;
Yinghai Lud3bd0582010-12-16 19:09:58 -0800116 if (apic_id >= MAX_LOCAL_APIC) {
117 printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%04x -> Node %u skipped apicid that is too big\n", pxm, apic_id, node);
118 return;
119 }
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100120 set_apicid_to_node(apic_id, node);
Jack Steinerdc098552009-04-17 09:22:42 -0500121 node_set(node, cpu_nodes_parsed);
Suresh Siddha7237d3d2009-03-30 13:55:30 -0800122 acpi_numa = 1;
Yinghai Lu163d3862009-11-21 00:23:37 -0800123 printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%04x -> Node %u\n",
Suresh Siddha7237d3d2009-03-30 13:55:30 -0800124 pxm, apic_id, node);
125}
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/* Callback for Proximity Domain -> LAPIC mapping */
128void __init
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300129acpi_numa_processor_affinity_init(struct acpi_srat_cpu_affinity *pa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 int pxm, node;
travis@sgi.comef970012008-01-30 13:33:10 +0100132 int apic_id;
133
Andi Kleend22fe802006-02-03 21:51:26 +0100134 if (srat_disabled())
135 return;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300136 if (pa->header.length != sizeof(struct acpi_srat_cpu_affinity)) {
Andi Kleenfad79062006-05-15 18:19:44 +0200137 bad_srat();
Andi Kleend22fe802006-02-03 21:51:26 +0100138 return;
139 }
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300140 if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300142 pxm = pa->proximity_domain_lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 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 }
Yinghai Lubeafe912008-02-16 23:00:22 -0800149
Jack Steiner2e420602008-09-23 15:37:13 -0500150 if (get_uv_system_type() >= UV_X2APIC)
Jack Steinera65d1d62008-03-28 14:12:08 -0500151 apic_id = (pa->apic_id << 8) | pa->local_sapic_eid;
152 else
153 apic_id = pa->apic_id;
Yinghai Lud3bd0582010-12-16 19:09:58 -0800154
155 if (apic_id >= MAX_LOCAL_APIC) {
156 printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%02x -> Node %u skipped apicid that is too big\n", pxm, apic_id, node);
157 return;
158 }
159
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100160 set_apicid_to_node(apic_id, node);
Jack Steinerdc098552009-04-17 09:22:42 -0500161 node_set(node, cpu_nodes_parsed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 acpi_numa = 1;
Yinghai Lu163d3862009-11-21 00:23:37 -0800163 printk(KERN_INFO "SRAT: PXM %u -> APIC 0x%02x -> Node %u\n",
travis@sgi.comef970012008-01-30 13:33:10 +0100164 pxm, apic_id, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Keith Mannthey71efa8f2006-09-30 23:27:05 -0700167#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
168static inline int save_add_info(void) {return 1;}
169#else
170static inline int save_add_info(void) {return 0;}
171#endif
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200172/*
Yinghai Lu888a5892009-05-15 13:59:37 -0700173 * Update nodes_add[]
174 * This code supports one contiguous hot add area per node
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200175 */
Yinghai Lu888a5892009-05-15 13:59:37 -0700176static void __init
177update_nodes_add(int node, unsigned long start, unsigned long end)
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200178{
179 unsigned long s_pfn = start >> PAGE_SHIFT;
180 unsigned long e_pfn = end >> PAGE_SHIFT;
Yinghai Lu888a5892009-05-15 13:59:37 -0700181 int changed = 0;
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200182 struct bootnode *nd = &nodes_add[node];
183
184 /* I had some trouble with strange memory hotadd regions breaking
185 the boot. Be very strict here and reject anything unexpected.
186 If you want working memory hotadd write correct SRATs.
187
188 The node size check is a basic sanity check to guard against
189 mistakes */
190 if ((signed long)(end - start) < NODE_MIN_SIZE) {
191 printk(KERN_ERR "SRAT: Hotplug area too small\n");
Yinghai Lu888a5892009-05-15 13:59:37 -0700192 return;
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200193 }
194
195 /* This check might be a bit too strict, but I'm keeping it for now. */
Mel Gorman5cb248a2006-09-27 01:49:52 -0700196 if (absent_pages_in_range(s_pfn, e_pfn) != e_pfn - s_pfn) {
Mel Gorman9c7cd682006-09-27 01:49:58 -0700197 printk(KERN_ERR
198 "SRAT: Hotplug area %lu -> %lu has existing memory\n",
199 s_pfn, e_pfn);
Yinghai Lu888a5892009-05-15 13:59:37 -0700200 return;
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200201 }
202
203 /* Looks good */
204
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200205 if (nd->start == nd->end) {
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300206 nd->start = start;
207 nd->end = end;
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200208 changed = 1;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300209 } else {
210 if (nd->start == end) {
211 nd->start = start;
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200212 changed = 1;
213 }
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300214 if (nd->end == start) {
215 nd->end = end;
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200216 changed = 1;
217 }
218 if (!changed)
219 printk(KERN_ERR "SRAT: Hotplug zone not continuous. Partly ignored\n");
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300220 }
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200221
David Rientjes3a5fc0e2010-01-20 12:10:47 -0800222 if (changed) {
223 node_set(node, cpu_nodes_parsed);
Yinghai Lu888a5892009-05-15 13:59:37 -0700224 printk(KERN_INFO "SRAT: hot plug zone found %Lx - %Lx\n",
225 nd->start, nd->end);
David Rientjes3a5fc0e2010-01-20 12:10:47 -0800226 }
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200227}
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229/* Callback for parsing of the Proximity Domain <-> Memory Area mappings */
230void __init
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300231acpi_numa_memory_affinity_init(struct acpi_srat_mem_affinity *ma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Tejun Heo13081df2011-02-16 12:13:06 +0100233 struct bootnode *nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 unsigned long start, end;
235 int node, pxm;
236 int i;
237
Andi Kleend22fe802006-02-03 21:51:26 +0100238 if (srat_disabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300240 if (ma->header.length != sizeof(struct acpi_srat_mem_affinity)) {
Andi Kleend22fe802006-02-03 21:51:26 +0100241 bad_srat();
242 return;
243 }
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300244 if ((ma->flags & ACPI_SRAT_MEM_ENABLED) == 0)
Andi Kleend22fe802006-02-03 21:51:26 +0100245 return;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300246
247 if ((ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) && !save_add_info())
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200248 return;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300249 start = ma->base_address;
250 end = start + ma->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 pxm = ma->proximity_domain;
252 node = setup_node(pxm);
253 if (node < 0) {
254 printk(KERN_ERR "SRAT: Too many proximity domains.\n");
255 bad_srat();
256 return;
257 }
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700258 i = conflicting_memblks(start, end);
Andi Kleen05d1fa42005-09-12 18:49:24 +0200259 if (i == node) {
260 printk(KERN_WARNING
261 "SRAT: Warning: PXM %d (%lx-%lx) overlaps with itself (%Lx-%Lx)\n",
Tejun Heo206e4202011-02-16 12:13:07 +0100262 pxm, start, end, numa_nodes[i].start, numa_nodes[i].end);
Andi Kleen05d1fa42005-09-12 18:49:24 +0200263 } else if (i >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 printk(KERN_ERR
Andi Kleen05d1fa42005-09-12 18:49:24 +0200265 "SRAT: PXM %d (%lx-%lx) overlaps with PXM %d (%Lx-%Lx)\n",
266 pxm, start, end, node_to_pxm(i),
Tejun Heo206e4202011-02-16 12:13:07 +0100267 numa_nodes[i].start, numa_nodes[i].end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 bad_srat();
269 return;
270 }
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200271
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700272 printk(KERN_INFO "SRAT: Node %u PXM %u %lx-%lx\n", node, pxm,
273 start, end);
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200274
Tejun Heo13081df2011-02-16 12:13:06 +0100275 if (!(ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE)) {
Tejun Heo206e4202011-02-16 12:13:07 +0100276 nd = &numa_nodes[node];
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100277 if (!node_test_and_set(node, mem_nodes_parsed)) {
Tejun Heo13081df2011-02-16 12:13:06 +0100278 nd->start = start;
279 nd->end = end;
280 } else {
281 if (start < nd->start)
282 nd->start = start;
283 if (nd->end < end)
284 nd->end = end;
285 }
286 } else
Yinghai Lu888a5892009-05-15 13:59:37 -0700287 update_nodes_add(node, start, end);
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700288
289 node_memblk_range[num_node_memblks].start = start;
290 node_memblk_range[num_node_memblks].end = end;
291 memblk_nodeid[num_node_memblks] = node;
292 num_node_memblks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
Andi Kleen8a6fdd32006-01-11 22:44:39 +0100295/* Sanity check to catch more bad SRATs (they are amazingly common).
296 Make sure the PXMs cover all memory. */
David Rientjes3484d792007-07-21 17:10:32 +0200297static int __init nodes_cover_memory(const struct bootnode *nodes)
Andi Kleen8a6fdd32006-01-11 22:44:39 +0100298{
299 int i;
300 unsigned long pxmram, e820ram;
301
302 pxmram = 0;
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100303 for_each_node_mask(i, mem_nodes_parsed) {
Andi Kleen8a6fdd32006-01-11 22:44:39 +0100304 unsigned long s = nodes[i].start >> PAGE_SHIFT;
305 unsigned long e = nodes[i].end >> PAGE_SHIFT;
306 pxmram += e - s;
Yinghai Lu32996252009-12-15 17:59:02 -0800307 pxmram -= __absent_pages_in_range(i, s, e);
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200308 if ((long)pxmram < 0)
309 pxmram = 0;
Andi Kleen8a6fdd32006-01-11 22:44:39 +0100310 }
311
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700312 e820ram = max_pfn - (memblock_x86_hole_size(0, max_pfn<<PAGE_SHIFT)>>PAGE_SHIFT);
Yinghai Lu0964b052009-05-08 00:37:34 -0700313 /* We seem to lose 3 pages somewhere. Allow 1M of slack. */
314 if ((long)(e820ram - pxmram) >= (1<<(20 - PAGE_SHIFT))) {
Andi Kleen8a6fdd32006-01-11 22:44:39 +0100315 printk(KERN_ERR
316 "SRAT: PXMs only cover %luMB of your %luMB e820 RAM. Not used.\n",
317 (pxmram << PAGE_SHIFT) >> 20,
318 (e820ram << PAGE_SHIFT) >> 20);
319 return 0;
320 }
321 return 1;
322}
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324void __init acpi_numa_arch_fixup(void) {}
325
David Rientjes4e76f4e2010-12-22 17:23:47 -0800326#ifdef CONFIG_NUMA_EMU
David Rientjesa387e952010-12-22 17:23:56 -0800327void __init acpi_get_nodes(struct bootnode *physnodes, unsigned long start,
328 unsigned long end)
David Rientjes87162732009-09-25 15:20:04 -0700329{
330 int i;
David Rientjes87162732009-09-25 15:20:04 -0700331
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100332 for_each_node_mask(i, mem_nodes_parsed) {
Tejun Heo206e4202011-02-16 12:13:07 +0100333 physnodes[i].start = numa_nodes[i].start;
334 physnodes[i].end = numa_nodes[i].end;
David Rientjes87162732009-09-25 15:20:04 -0700335 }
David Rientjes87162732009-09-25 15:20:04 -0700336}
David Rientjes4e76f4e2010-12-22 17:23:47 -0800337#endif /* CONFIG_NUMA_EMU */
David Rientjes87162732009-09-25 15:20:04 -0700338
Tejun Heoa9aec562011-02-16 12:13:06 +0100339int __init x86_acpi_numa_init(void)
340{
341 int ret;
342
343 ret = acpi_numa_init();
344 if (ret < 0)
345 return ret;
346 return srat_disabled() ? -EINVAL : 0;
347}
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349/* Use the information discovered above to actually set up the nodes. */
Tejun Heo940fed22011-02-16 12:13:06 +0100350int __init acpi_scan_nodes(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 int i;
Andi Kleen8a6fdd32006-01-11 22:44:39 +0100353
David Rientjesae2c6dc2007-07-21 17:09:56 +0200354 if (acpi_numa <= 0)
355 return -1;
356
Jan Beulich2e618782010-04-21 16:13:20 +0100357 /*
358 * Join together blocks on the same node, holes between
359 * which don't overlap with memory on other nodes.
360 */
361 for (i = 0; i < num_node_memblks; ++i) {
362 int j, k;
363
364 for (j = i + 1; j < num_node_memblks; ++j) {
365 unsigned long start, end;
366
367 if (memblk_nodeid[i] != memblk_nodeid[j])
368 continue;
369 start = min(node_memblk_range[i].end,
370 node_memblk_range[j].end);
371 end = max(node_memblk_range[i].start,
372 node_memblk_range[j].start);
373 for (k = 0; k < num_node_memblks; ++k) {
374 if (memblk_nodeid[i] == memblk_nodeid[k])
375 continue;
376 if (start < node_memblk_range[k].end &&
377 end > node_memblk_range[k].start)
378 break;
379 }
380 if (k < num_node_memblks)
381 continue;
382 start = min(node_memblk_range[i].start,
383 node_memblk_range[j].start);
384 end = max(node_memblk_range[i].end,
385 node_memblk_range[j].end);
386 printk(KERN_INFO "SRAT: Node %d "
387 "[%Lx,%Lx) + [%Lx,%Lx) -> [%lx,%lx)\n",
388 memblk_nodeid[i],
389 node_memblk_range[i].start,
390 node_memblk_range[i].end,
391 node_memblk_range[j].start,
392 node_memblk_range[j].end,
393 start, end);
394 node_memblk_range[i].start = start;
395 node_memblk_range[i].end = end;
396 k = --num_node_memblks - j;
397 memmove(memblk_nodeid + j, memblk_nodeid + j+1,
398 k * sizeof(*memblk_nodeid));
399 memmove(node_memblk_range + j, node_memblk_range + j+1,
400 k * sizeof(*node_memblk_range));
401 --j;
402 }
403 }
404
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700405 memnode_shift = compute_hash_shift(node_memblk_range, num_node_memblks,
406 memblk_nodeid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (memnode_shift < 0) {
408 printk(KERN_ERR
409 "SRAT: No NUMA node hash function found. Contact maintainer\n");
410 bad_srat();
411 return -1;
412 }
Andi Kleene58e0d02005-09-12 18:49:25 +0200413
Yinghai Lu73cf6242010-10-10 19:52:15 -0700414 for (i = 0; i < num_node_memblks; i++)
H. Peter Anvin8e4029e2010-10-11 17:05:11 -0700415 memblock_x86_register_active_regions(memblk_nodeid[i],
Yinghai Lu73cf6242010-10-10 19:52:15 -0700416 node_memblk_range[i].start >> PAGE_SHIFT,
417 node_memblk_range[i].end >> PAGE_SHIFT);
418
Yinghai Lu32996252009-12-15 17:59:02 -0800419 /* for out of order entries in SRAT */
420 sort_node_map();
Tejun Heo206e4202011-02-16 12:13:07 +0100421 if (!nodes_cover_memory(numa_nodes)) {
David Rientjes87162732009-09-25 15:20:04 -0700422 bad_srat();
423 return -1;
424 }
425
Yinghai Lu1411e0e2010-12-27 16:48:17 -0800426 init_memory_mapping_high();
427
Andi Kleene58e0d02005-09-12 18:49:25 +0200428 /* Finally register nodes */
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200429 for_each_node_mask(i, node_possible_map)
Tejun Heo206e4202011-02-16 12:13:07 +0100430 setup_node_bootmem(i, numa_nodes[i].start, numa_nodes[i].end);
Andi Kleena8062232006-04-07 19:49:21 +0200431 /* Try again in case setup_node_bootmem missed one due
432 to missing bootmem */
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200433 for_each_node_mask(i, node_possible_map)
Andi Kleena8062232006-04-07 19:49:21 +0200434 if (!node_online(i))
Tejun Heo206e4202011-02-16 12:13:07 +0100435 setup_node_bootmem(i, numa_nodes[i].start,
436 numa_nodes[i].end);
Andi Kleena8062232006-04-07 19:49:21 +0200437
Mike Travis168ef542008-12-16 17:34:01 -0800438 for (i = 0; i < nr_cpu_ids; i++) {
Mike Travis0164fe12008-01-30 13:33:21 +0100439 int node = early_cpu_to_node(i);
440
travis@sgi.com834beda12008-01-30 13:33:21 +0100441 if (node == NUMA_NO_NODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 continue;
Yinghai Lu7c437692009-05-15 13:59:37 -0700443 if (!node_online(node))
Mike Travis23ca4bb2008-05-12 21:21:12 +0200444 numa_clear_node(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446 numa_init_array();
447 return 0;
448}
449
David Rientjes3484d792007-07-21 17:10:32 +0200450#ifdef CONFIG_NUMA_EMU
travis@sgi.comef970012008-01-30 13:33:10 +0100451static int fake_node_to_pxm_map[MAX_NUMNODES] __initdata = {
452 [0 ... MAX_NUMNODES-1] = PXM_INVAL
453};
travis@sgi.com602a54a2008-01-30 13:33:21 +0100454static s16 fake_apicid_to_node[MAX_LOCAL_APIC] __initdata = {
travis@sgi.comef970012008-01-30 13:33:10 +0100455 [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
456};
David Rientjes3484d792007-07-21 17:10:32 +0200457static int __init find_node_by_addr(unsigned long addr)
458{
459 int ret = NUMA_NO_NODE;
460 int i;
461
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100462 for_each_node_mask(i, mem_nodes_parsed) {
David Rientjes3484d792007-07-21 17:10:32 +0200463 /*
464 * Find the real node that this emulated node appears on. For
465 * the sake of simplicity, we only use a real node's starting
466 * address to determine which emulated node it appears on.
467 */
Tejun Heo206e4202011-02-16 12:13:07 +0100468 if (addr >= numa_nodes[i].start && addr < numa_nodes[i].end) {
David Rientjes3484d792007-07-21 17:10:32 +0200469 ret = i;
470 break;
471 }
472 }
Minoru Usui9a1b62f2008-01-30 13:33:35 +0100473 return ret;
David Rientjes3484d792007-07-21 17:10:32 +0200474}
475
476/*
477 * In NUMA emulation, we need to setup proximity domain (_PXM) to node ID
478 * mappings that respect the real ACPI topology but reflect our emulated
479 * environment. For each emulated node, we find which real node it appears on
480 * and create PXM to NID mappings for those fake nodes which mirror that
481 * locality. SLIT will now represent the correct distances between emulated
482 * nodes as a result of the real topology.
483 */
484void __init acpi_fake_nodes(const struct bootnode *fake_nodes, int num_nodes)
485{
David Rientjes08705b82007-07-21 17:10:33 +0200486 int i, j;
David Rientjes3484d792007-07-21 17:10:32 +0200487
David Rientjes3484d792007-07-21 17:10:32 +0200488 for (i = 0; i < num_nodes; i++) {
489 int nid, pxm;
490
491 nid = find_node_by_addr(fake_nodes[i].start);
492 if (nid == NUMA_NO_NODE)
493 continue;
494 pxm = node_to_pxm(nid);
495 if (pxm == PXM_INVAL)
496 continue;
497 fake_node_to_pxm_map[i] = pxm;
David Rientjes08705b82007-07-21 17:10:33 +0200498 /*
499 * For each apicid_to_node mapping that exists for this real
500 * node, it must now point to the fake node ID.
501 */
502 for (j = 0; j < MAX_LOCAL_APIC; j++)
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100503 if (__apicid_to_node[j] == nid &&
David Rientjesb0c4d952010-05-06 02:24:34 -0700504 fake_apicid_to_node[j] == NUMA_NO_NODE)
David Rientjes08705b82007-07-21 17:10:33 +0200505 fake_apicid_to_node[j] = i;
David Rientjes3484d792007-07-21 17:10:32 +0200506 }
David Rientjesa387e952010-12-22 17:23:56 -0800507
508 /*
509 * If there are apicid-to-node mappings for physical nodes that do not
510 * have a corresponding emulated node, it should default to a guaranteed
511 * value.
512 */
513 for (i = 0; i < MAX_LOCAL_APIC; i++)
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100514 if (__apicid_to_node[i] != NUMA_NO_NODE &&
David Rientjesa387e952010-12-22 17:23:56 -0800515 fake_apicid_to_node[i] == NUMA_NO_NODE)
516 fake_apicid_to_node[i] = 0;
517
David Rientjes3484d792007-07-21 17:10:32 +0200518 for (i = 0; i < num_nodes; i++)
519 __acpi_map_pxm_to_node(fake_node_to_pxm_map[i], i);
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100520 memcpy(__apicid_to_node, fake_apicid_to_node, sizeof(__apicid_to_node));
David Rientjes3484d792007-07-21 17:10:32 +0200521
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100522 nodes_clear(mem_nodes_parsed);
David Rientjes3484d792007-07-21 17:10:32 +0200523 for (i = 0; i < num_nodes; i++)
524 if (fake_nodes[i].start != fake_nodes[i].end)
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100525 node_set(i, mem_nodes_parsed);
David Rientjes3484d792007-07-21 17:10:32 +0200526}
527
528static int null_slit_node_compare(int a, int b)
529{
530 return node_to_pxm(a) == node_to_pxm(b);
531}
532#else
533static int null_slit_node_compare(int a, int b)
534{
535 return a == b;
536}
537#endif /* CONFIG_NUMA_EMU */
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539int __node_distance(int a, int b)
540{
541 int index;
542
543 if (!acpi_slit)
David Rientjes3484d792007-07-21 17:10:32 +0200544 return null_slit_node_compare(a, b) ? LOCAL_DISTANCE :
545 REMOTE_DISTANCE;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300546 index = acpi_slit->locality_count * node_to_pxm(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return acpi_slit->entry[index + node_to_pxm(b)];
548}
549
550EXPORT_SYMBOL(__node_distance);
Keith Mannthey4942e992006-09-30 23:27:06 -0700551
Thomas Gleixner6a1673a2008-05-12 15:43:38 +0200552#if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) || defined(CONFIG_ACPI_HOTPLUG_MEMORY)
Keith Mannthey4942e992006-09-30 23:27:06 -0700553int memory_add_physaddr_to_nid(u64 start)
554{
555 int i, ret = 0;
556
557 for_each_node(i)
558 if (nodes_add[i].start <= start && nodes_add[i].end > start)
559 ret = i;
560
561 return ret;
562}
Keith Mannthey8c2676a2006-09-30 23:27:07 -0700563EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
Thomas Gleixner6a1673a2008-05-12 15:43:38 +0200564#endif