blob: 1bae9c855ceb8b56cce821129a21194a693f7c81 [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>
19#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/proto.h>
21#include <asm/numa.h>
Andi Kleen8a6fdd32006-01-11 22:44:39 +010022#include <asm/e820.h>
Jack Steinera65d1d62008-03-28 14:12:08 -050023#include <asm/genapic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Andi Kleenc31fbb12006-09-26 10:52:33 +020025int acpi_numa __initdata;
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static struct acpi_table_slit *acpi_slit;
28
29static nodemask_t nodes_parsed __initdata;
Andi Kleenabe059e2006-03-25 16:29:12 +010030static struct bootnode nodes[MAX_NUMNODES] __initdata;
Keith Mannthey4942e992006-09-30 23:27:06 -070031static struct bootnode nodes_add[MAX_NUMNODES];
Andi Kleen68a3a7f2006-04-07 19:49:18 +020032static int found_add_area __initdata;
Andi Kleenfad79062006-05-15 18:19:44 +020033int hotadd_percent __initdata = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Andi Kleen9391a3f2006-02-03 21:51:17 +010035/* Too small nodes confuse the VM badly. Usually they result
36 from BIOS bugs. */
37#define NODE_MIN_SIZE (4*1024*1024)
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static __init int setup_node(int pxm)
40{
Yasunori Goto762834e2006-06-23 02:03:19 -070041 return acpi_map_pxm_to_node(pxm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
43
44static __init int conflicting_nodes(unsigned long start, unsigned long end)
45{
46 int i;
Andi Kleen4b6a4552005-09-12 18:49:25 +020047 for_each_node_mask(i, nodes_parsed) {
Andi Kleenabe059e2006-03-25 16:29:12 +010048 struct bootnode *nd = &nodes[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 if (nd->start == nd->end)
50 continue;
51 if (nd->end > start && nd->start < end)
Andi Kleen05d1fa42005-09-12 18:49:24 +020052 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 if (nd->end == end && nd->start == start)
Andi Kleen05d1fa42005-09-12 18:49:24 +020054 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 }
56 return -1;
57}
58
59static __init void cutoff_node(int i, unsigned long start, unsigned long end)
60{
Andi Kleenabe059e2006-03-25 16:29:12 +010061 struct bootnode *nd = &nodes[i];
Andi Kleen68a3a7f2006-04-07 19:49:18 +020062
63 if (found_add_area)
64 return;
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 if (nd->start < start) {
67 nd->start = start;
68 if (nd->end < nd->start)
69 nd->start = nd->end;
70 }
71 if (nd->end > end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 nd->end = end;
73 if (nd->start > nd->end)
74 nd->start = nd->end;
75 }
76}
77
78static __init void bad_srat(void)
79{
Andi Kleen2bce2b52005-09-12 18:49:25 +020080 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 printk(KERN_ERR "SRAT: SRAT not used.\n");
82 acpi_numa = -1;
Andi Kleenfad79062006-05-15 18:19:44 +020083 found_add_area = 0;
Andi Kleen2bce2b52005-09-12 18:49:25 +020084 for (i = 0; i < MAX_LOCAL_APIC; i++)
85 apicid_to_node[i] = NUMA_NO_NODE;
Andi Kleen68a3a7f2006-04-07 19:49:18 +020086 for (i = 0; i < MAX_NUMNODES; i++)
87 nodes_add[i].start = nodes[i].end = 0;
Mel Gorman5cb248a2006-09-27 01:49:52 -070088 remove_all_active_ranges();
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
91static __init inline int srat_disabled(void)
92{
93 return numa_off || acpi_numa < 0;
94}
95
Andi Kleen1584b892006-01-11 22:43:42 +010096/*
97 * A lot of BIOS fill in 10 (= no distance) everywhere. This messes
98 * up the NUMA heuristics which wants the local node to have a smaller
99 * distance than the others.
100 * Do some quick checks here and only use the SLIT if it passes.
101 */
102static __init int slit_valid(struct acpi_table_slit *slit)
103{
104 int i, j;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300105 int d = slit->locality_count;
Andi Kleen1584b892006-01-11 22:43:42 +0100106 for (i = 0; i < d; i++) {
107 for (j = 0; j < d; j++) {
108 u8 val = slit->entry[d*i + j];
109 if (i == j) {
David Rientjesa2e212d2007-07-21 17:09:55 +0200110 if (val != LOCAL_DISTANCE)
Andi Kleen1584b892006-01-11 22:43:42 +0100111 return 0;
David Rientjesa2e212d2007-07-21 17:09:55 +0200112 } else if (val <= LOCAL_DISTANCE)
Andi Kleen1584b892006-01-11 22:43:42 +0100113 return 0;
114 }
115 }
116 return 1;
117}
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/* Callback for SLIT parsing */
120void __init acpi_numa_slit_init(struct acpi_table_slit *slit)
121{
Andi Kleen1584b892006-01-11 22:43:42 +0100122 if (!slit_valid(slit)) {
123 printk(KERN_INFO "ACPI: SLIT table looks invalid. Not used.\n");
124 return;
125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 acpi_slit = slit;
127}
128
129/* Callback for Proximity Domain -> LAPIC mapping */
130void __init
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300131acpi_numa_processor_affinity_init(struct acpi_srat_cpu_affinity *pa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 int pxm, node;
travis@sgi.comef970012008-01-30 13:33:10 +0100134 int apic_id;
135
Andi Kleend22fe802006-02-03 21:51:26 +0100136 if (srat_disabled())
137 return;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300138 if (pa->header.length != sizeof(struct acpi_srat_cpu_affinity)) {
Andi Kleenfad79062006-05-15 18:19:44 +0200139 bad_srat();
Andi Kleend22fe802006-02-03 21:51:26 +0100140 return;
141 }
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300142 if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300144 pxm = pa->proximity_domain_lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 node = setup_node(pxm);
146 if (node < 0) {
147 printk(KERN_ERR "SRAT: Too many proximity domains %x\n", pxm);
148 bad_srat();
149 return;
150 }
Yinghai Lubeafe912008-02-16 23:00:22 -0800151
Jack Steinera65d1d62008-03-28 14:12:08 -0500152 if (is_uv_system())
153 apic_id = (pa->apic_id << 8) | pa->local_sapic_eid;
154 else
155 apic_id = pa->apic_id;
travis@sgi.comef970012008-01-30 13:33:10 +0100156 apicid_to_node[apic_id] = node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 acpi_numa = 1;
Andi Kleen0b07e982005-09-12 18:49:24 +0200158 printk(KERN_INFO "SRAT: PXM %u -> APIC %u -> Node %u\n",
travis@sgi.comef970012008-01-30 13:33:10 +0100159 pxm, apic_id, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
keith mannthey926fafe2006-10-21 18:37:01 +0200162int update_end_of_memory(unsigned long end) {return -1;}
Keith Mannthey71efa8f2006-09-30 23:27:05 -0700163static int hotadd_enough_memory(struct bootnode *nd) {return 1;}
164#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
165static inline int save_add_info(void) {return 1;}
166#else
167static inline int save_add_info(void) {return 0;}
168#endif
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200169/*
Keith Mannthey71efa8f2006-09-30 23:27:05 -0700170 * Update nodes_add and decide if to include add are in the zone.
Joe Perchesab4a5742008-01-30 13:31:42 +0100171 * Both SPARSE and RESERVE need nodes_add information.
Simon Arlott676b1852007-10-20 01:25:36 +0200172 * This code supports one contiguous hot add area per node.
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200173 */
Sam Ravnborgd01b9ad2008-02-17 13:22:58 +0100174static int __init
175reserve_hotadd(int node, unsigned long start, unsigned long end)
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200176{
177 unsigned long s_pfn = start >> PAGE_SHIFT;
178 unsigned long e_pfn = end >> PAGE_SHIFT;
Keith Mannthey71efa8f2006-09-30 23:27:05 -0700179 int ret = 0, changed = 0;
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200180 struct bootnode *nd = &nodes_add[node];
181
182 /* I had some trouble with strange memory hotadd regions breaking
183 the boot. Be very strict here and reject anything unexpected.
184 If you want working memory hotadd write correct SRATs.
185
186 The node size check is a basic sanity check to guard against
187 mistakes */
188 if ((signed long)(end - start) < NODE_MIN_SIZE) {
189 printk(KERN_ERR "SRAT: Hotplug area too small\n");
190 return -1;
191 }
192
193 /* This check might be a bit too strict, but I'm keeping it for now. */
Mel Gorman5cb248a2006-09-27 01:49:52 -0700194 if (absent_pages_in_range(s_pfn, e_pfn) != e_pfn - s_pfn) {
Mel Gorman9c7cd682006-09-27 01:49:58 -0700195 printk(KERN_ERR
196 "SRAT: Hotplug area %lu -> %lu has existing memory\n",
197 s_pfn, e_pfn);
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200198 return -1;
199 }
200
201 if (!hotadd_enough_memory(&nodes_add[node])) {
202 printk(KERN_ERR "SRAT: Hotplug area too large\n");
203 return -1;
204 }
205
206 /* Looks good */
207
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200208 if (nd->start == nd->end) {
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300209 nd->start = start;
210 nd->end = end;
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200211 changed = 1;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300212 } else {
213 if (nd->start == end) {
214 nd->start = start;
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200215 changed = 1;
216 }
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300217 if (nd->end == start) {
218 nd->end = end;
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200219 changed = 1;
220 }
221 if (!changed)
222 printk(KERN_ERR "SRAT: Hotplug zone not continuous. Partly ignored\n");
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300223 }
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200224
Keith Mannthey71efa8f2006-09-30 23:27:05 -0700225 ret = update_end_of_memory(nd->end);
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200226
227 if (changed)
228 printk(KERN_INFO "SRAT: hot plug zone found %Lx - %Lx\n", nd->start, nd->end);
Keith Mannthey71efa8f2006-09-30 23:27:05 -0700229 return ret;
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200230}
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232/* Callback for parsing of the Proximity Domain <-> Memory Area mappings */
233void __init
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300234acpi_numa_memory_affinity_init(struct acpi_srat_mem_affinity *ma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200236 struct bootnode *nd, oldnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 unsigned long start, end;
238 int node, pxm;
239 int i;
240
Andi Kleend22fe802006-02-03 21:51:26 +0100241 if (srat_disabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300243 if (ma->header.length != sizeof(struct acpi_srat_mem_affinity)) {
Andi Kleend22fe802006-02-03 21:51:26 +0100244 bad_srat();
245 return;
246 }
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300247 if ((ma->flags & ACPI_SRAT_MEM_ENABLED) == 0)
Andi Kleend22fe802006-02-03 21:51:26 +0100248 return;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300249
250 if ((ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) && !save_add_info())
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200251 return;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300252 start = ma->base_address;
253 end = start + ma->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 pxm = ma->proximity_domain;
255 node = setup_node(pxm);
256 if (node < 0) {
257 printk(KERN_ERR "SRAT: Too many proximity domains.\n");
258 bad_srat();
259 return;
260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 i = conflicting_nodes(start, end);
Andi Kleen05d1fa42005-09-12 18:49:24 +0200262 if (i == node) {
263 printk(KERN_WARNING
264 "SRAT: Warning: PXM %d (%lx-%lx) overlaps with itself (%Lx-%Lx)\n",
265 pxm, start, end, nodes[i].start, nodes[i].end);
266 } else if (i >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 printk(KERN_ERR
Andi Kleen05d1fa42005-09-12 18:49:24 +0200268 "SRAT: PXM %d (%lx-%lx) overlaps with PXM %d (%Lx-%Lx)\n",
269 pxm, start, end, node_to_pxm(i),
270 nodes[i].start, nodes[i].end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 bad_srat();
272 return;
273 }
274 nd = &nodes[node];
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200275 oldnode = *nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (!node_test_and_set(node, nodes_parsed)) {
277 nd->start = start;
278 nd->end = end;
279 } else {
280 if (start < nd->start)
281 nd->start = start;
282 if (nd->end < end)
283 nd->end = end;
284 }
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 printk(KERN_INFO "SRAT: Node %u PXM %u %Lx-%Lx\n", node, pxm,
287 nd->start, nd->end);
Mel Gorman5cb248a2006-09-27 01:49:52 -0700288 e820_register_active_regions(node, nd->start >> PAGE_SHIFT,
289 nd->end >> PAGE_SHIFT);
Mel Gormanfb014392006-09-27 01:49:59 -0700290 push_node_boundaries(node, nd->start >> PAGE_SHIFT,
291 nd->end >> PAGE_SHIFT);
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200292
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300293 if ((ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) &&
294 (reserve_hotadd(node, start, end) < 0)) {
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200295 /* Ignore hotadd region. Undo damage */
296 printk(KERN_NOTICE "SRAT: Hotplug region ignored\n");
297 *nd = oldnode;
298 if ((nd->start | nd->end) == 0)
299 node_clear(node, nodes_parsed);
300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
Andi Kleen8a6fdd32006-01-11 22:44:39 +0100303/* Sanity check to catch more bad SRATs (they are amazingly common).
304 Make sure the PXMs cover all memory. */
David Rientjes3484d792007-07-21 17:10:32 +0200305static int __init nodes_cover_memory(const struct bootnode *nodes)
Andi Kleen8a6fdd32006-01-11 22:44:39 +0100306{
307 int i;
308 unsigned long pxmram, e820ram;
309
310 pxmram = 0;
311 for_each_node_mask(i, nodes_parsed) {
312 unsigned long s = nodes[i].start >> PAGE_SHIFT;
313 unsigned long e = nodes[i].end >> PAGE_SHIFT;
314 pxmram += e - s;
Mel Gorman5cb248a2006-09-27 01:49:52 -0700315 pxmram -= absent_pages_in_range(s, e);
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200316 if ((long)pxmram < 0)
317 pxmram = 0;
Andi Kleen8a6fdd32006-01-11 22:44:39 +0100318 }
319
Mel Gorman5cb248a2006-09-27 01:49:52 -0700320 e820ram = end_pfn - absent_pages_in_range(0, end_pfn);
Andi Kleenfdb9df92006-02-16 23:42:13 +0100321 /* We seem to lose 3 pages somewhere. Allow a bit of slack. */
322 if ((long)(e820ram - pxmram) >= 1*1024*1024) {
Andi Kleen8a6fdd32006-01-11 22:44:39 +0100323 printk(KERN_ERR
324 "SRAT: PXMs only cover %luMB of your %luMB e820 RAM. Not used.\n",
325 (pxmram << PAGE_SHIFT) >> 20,
326 (e820ram << PAGE_SHIFT) >> 20);
327 return 0;
328 }
329 return 1;
330}
331
Sam Ravnborg1e296f52008-01-30 13:33:37 +0100332static void __init unparse_node(int node)
Andi Kleen9391a3f2006-02-03 21:51:17 +0100333{
334 int i;
335 node_clear(node, nodes_parsed);
336 for (i = 0; i < MAX_LOCAL_APIC; i++) {
337 if (apicid_to_node[i] == node)
338 apicid_to_node[i] = NUMA_NO_NODE;
339 }
340}
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342void __init acpi_numa_arch_fixup(void) {}
343
344/* Use the information discovered above to actually set up the nodes. */
345int __init acpi_scan_nodes(unsigned long start, unsigned long end)
346{
347 int i;
Andi Kleen8a6fdd32006-01-11 22:44:39 +0100348
David Rientjesae2c6dc2007-07-21 17:09:56 +0200349 if (acpi_numa <= 0)
350 return -1;
351
Andi Kleen9391a3f2006-02-03 21:51:17 +0100352 /* First clean up the node list */
353 for (i = 0; i < MAX_NUMNODES; i++) {
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300354 cutoff_node(i, start, end);
Mike Travis693e3c52008-01-30 13:33:14 +0100355 /*
356 * don't confuse VM with a node that doesn't have the
357 * minimum memory.
358 */
359 if (nodes[i].end &&
360 (nodes[i].end - nodes[i].start) < NODE_MIN_SIZE) {
Andi Kleen9391a3f2006-02-03 21:51:17 +0100361 unparse_node(i);
Daniel Yeisley0d015322006-05-30 22:47:57 +0200362 node_set_offline(i);
363 }
Andi Kleen9391a3f2006-02-03 21:51:17 +0100364 }
365
David Rientjes3484d792007-07-21 17:10:32 +0200366 if (!nodes_cover_memory(nodes)) {
Andi Kleen8a6fdd32006-01-11 22:44:39 +0100367 bad_srat();
368 return -1;
369 }
370
Andi Kleen2aed7112006-02-16 23:42:16 +0100371 memnode_shift = compute_hash_shift(nodes, MAX_NUMNODES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (memnode_shift < 0) {
373 printk(KERN_ERR
374 "SRAT: No NUMA node hash function found. Contact maintainer\n");
375 bad_srat();
376 return -1;
377 }
Andi Kleene58e0d02005-09-12 18:49:25 +0200378
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200379 node_possible_map = nodes_parsed;
380
Andi Kleene58e0d02005-09-12 18:49:25 +0200381 /* Finally register nodes */
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200382 for_each_node_mask(i, node_possible_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
Andi Kleena8062232006-04-07 19:49:21 +0200384 /* Try again in case setup_node_bootmem missed one due
385 to missing bootmem */
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200386 for_each_node_mask(i, node_possible_map)
Andi Kleena8062232006-04-07 19:49:21 +0200387 if (!node_online(i))
388 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
389
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300390 for (i = 0; i < NR_CPUS; i++) {
Mike Travis0164fe12008-01-30 13:33:21 +0100391 int node = early_cpu_to_node(i);
392
travis@sgi.com834beda12008-01-30 13:33:21 +0100393 if (node == NUMA_NO_NODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 continue;
travis@sgi.com834beda12008-01-30 13:33:21 +0100395 if (!node_isset(node, node_possible_map))
Andi Kleen69d81fc2005-11-05 17:25:53 +0100396 numa_set_node(i, NUMA_NO_NODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398 numa_init_array();
399 return 0;
400}
401
David Rientjes3484d792007-07-21 17:10:32 +0200402#ifdef CONFIG_NUMA_EMU
travis@sgi.comef970012008-01-30 13:33:10 +0100403static int fake_node_to_pxm_map[MAX_NUMNODES] __initdata = {
404 [0 ... MAX_NUMNODES-1] = PXM_INVAL
405};
travis@sgi.com602a54a2008-01-30 13:33:21 +0100406static s16 fake_apicid_to_node[MAX_LOCAL_APIC] __initdata = {
travis@sgi.comef970012008-01-30 13:33:10 +0100407 [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
408};
David Rientjes3484d792007-07-21 17:10:32 +0200409static int __init find_node_by_addr(unsigned long addr)
410{
411 int ret = NUMA_NO_NODE;
412 int i;
413
414 for_each_node_mask(i, nodes_parsed) {
415 /*
416 * Find the real node that this emulated node appears on. For
417 * the sake of simplicity, we only use a real node's starting
418 * address to determine which emulated node it appears on.
419 */
420 if (addr >= nodes[i].start && addr < nodes[i].end) {
421 ret = i;
422 break;
423 }
424 }
Minoru Usui9a1b62f2008-01-30 13:33:35 +0100425 return ret;
David Rientjes3484d792007-07-21 17:10:32 +0200426}
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 */
436void __init acpi_fake_nodes(const struct bootnode *fake_nodes, int num_nodes)
437{
David Rientjes08705b82007-07-21 17:10:33 +0200438 int i, j;
David Rientjes3484d792007-07-21 17:10:32 +0200439
440 printk(KERN_INFO "Faking PXM affinity for fake nodes on real "
441 "topology.\n");
442 for (i = 0; i < num_nodes; i++) {
443 int nid, pxm;
444
445 nid = find_node_by_addr(fake_nodes[i].start);
446 if (nid == NUMA_NO_NODE)
447 continue;
448 pxm = node_to_pxm(nid);
449 if (pxm == PXM_INVAL)
450 continue;
451 fake_node_to_pxm_map[i] = pxm;
David Rientjes08705b82007-07-21 17:10:33 +0200452 /*
453 * For each apicid_to_node mapping that exists for this real
454 * node, it must now point to the fake node ID.
455 */
456 for (j = 0; j < MAX_LOCAL_APIC; j++)
457 if (apicid_to_node[j] == nid)
458 fake_apicid_to_node[j] = i;
David Rientjes3484d792007-07-21 17:10:32 +0200459 }
460 for (i = 0; i < num_nodes; i++)
461 __acpi_map_pxm_to_node(fake_node_to_pxm_map[i], i);
David Rientjes08705b82007-07-21 17:10:33 +0200462 memcpy(apicid_to_node, fake_apicid_to_node, sizeof(apicid_to_node));
David Rientjes3484d792007-07-21 17:10:32 +0200463
464 nodes_clear(nodes_parsed);
465 for (i = 0; i < num_nodes; i++)
466 if (fake_nodes[i].start != fake_nodes[i].end)
467 node_set(i, nodes_parsed);
468 WARN_ON(!nodes_cover_memory(fake_nodes));
469}
470
471static int null_slit_node_compare(int a, int b)
472{
473 return node_to_pxm(a) == node_to_pxm(b);
474}
475#else
476static int null_slit_node_compare(int a, int b)
477{
478 return a == b;
479}
480#endif /* CONFIG_NUMA_EMU */
481
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200482void __init srat_reserve_add_area(int nodeid)
483{
484 if (found_add_area && nodes_add[nodeid].end) {
485 u64 total_mb;
486
487 printk(KERN_INFO "SRAT: Reserving hot-add memory space "
488 "for node %d at %Lx-%Lx\n",
489 nodeid, nodes_add[nodeid].start, nodes_add[nodeid].end);
490 total_mb = (nodes_add[nodeid].end - nodes_add[nodeid].start)
491 >> PAGE_SHIFT;
492 total_mb *= sizeof(struct page);
493 total_mb >>= 20;
494 printk(KERN_INFO "SRAT: This will cost you %Lu MB of "
495 "pre-allocated memory.\n", (unsigned long long)total_mb);
496 reserve_bootmem_node(NODE_DATA(nodeid), nodes_add[nodeid].start,
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800497 nodes_add[nodeid].end - nodes_add[nodeid].start,
498 BOOTMEM_DEFAULT);
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200499 }
500}
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502int __node_distance(int a, int b)
503{
504 int index;
505
506 if (!acpi_slit)
David Rientjes3484d792007-07-21 17:10:32 +0200507 return null_slit_node_compare(a, b) ? LOCAL_DISTANCE :
508 REMOTE_DISTANCE;
Alexey Starikovskiy15a58ed2007-02-02 19:48:22 +0300509 index = acpi_slit->locality_count * node_to_pxm(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return acpi_slit->entry[index + node_to_pxm(b)];
511}
512
513EXPORT_SYMBOL(__node_distance);
Keith Mannthey4942e992006-09-30 23:27:06 -0700514
515int memory_add_physaddr_to_nid(u64 start)
516{
517 int i, ret = 0;
518
519 for_each_node(i)
520 if (nodes_add[i].start <= start && nodes_add[i].end > start)
521 ret = i;
522
523 return ret;
524}
Keith Mannthey8c2676a2006-09-30 23:27:07 -0700525EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
526