blob: 243d18d4cfde50e17696fcacbdabae0bb27eccec [file] [log] [blame]
Thomas Gleixnere3cfe522008-01-30 13:30:37 +01001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Generic VM initialization for x86-64 NUMA setups.
3 * Copyright 2002,2003 Andi Kleen, SuSE Labs.
Thomas Gleixnere3cfe522008-01-30 13:30:37 +01004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/kernel.h>
6#include <linux/mm.h>
7#include <linux/string.h>
8#include <linux/init.h>
9#include <linux/bootmem.h>
Yinghai Lu72d7c3b2010-08-25 13:39:17 -070010#include <linux/memblock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/mmzone.h>
12#include <linux/ctype.h>
13#include <linux/module.h>
14#include <linux/nodemask.h>
travis@sgi.com3cc87e32008-01-30 13:33:11 +010015#include <linux/sched.h>
Tejun Heod8fc3af2011-02-16 12:13:06 +010016#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <asm/e820.h>
19#include <asm/proto.h>
20#include <asm/dma.h>
21#include <asm/numa.h>
22#include <asm/acpi.h>
Andreas Herrmann23ac4ae2010-09-17 18:03:43 +020023#include <asm/amd_nb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -070025struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010026EXPORT_SYMBOL(node_data);
27
Tejun Heoec8cf29b2011-02-16 12:13:07 +010028nodemask_t cpu_nodes_parsed __initdata;
29nodemask_t mem_nodes_parsed __initdata;
30
Eric Dumazetdcf36bf2006-03-25 16:31:46 +010031struct memnode memnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Thomas Gleixner864fc312008-05-12 15:43:36 +020033static unsigned long __initdata nodemap_addr;
34static unsigned long __initdata nodemap_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Tejun Heoef396ec2011-02-16 17:11:07 +010036static int num_node_memblks __initdata;
37static struct bootnode node_memblk_range[NR_NODE_MEMBLKS] __initdata;
38static int memblk_nodeid[NR_NODE_MEMBLKS] __initdata;
39
Tejun Heo206e4202011-02-16 12:13:07 +010040struct bootnode numa_nodes[MAX_NUMNODES] __initdata;
41
Brian Gerst6470aff2009-01-27 12:56:47 +090042/*
Eric Dumazet529a3402005-11-05 17:25:54 +010043 * Given a shift value, try to populate memnodemap[]
44 * Returns :
45 * 1 if OK
46 * 0 if memnodmap[] too small (of shift too small)
47 * -1 if node overlap or lost ram (shift too big)
48 */
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010049static int __init populate_memnodemap(const struct bootnode *nodes,
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -070050 int numnodes, int shift, int *nodeids)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Eric Dumazet529a3402005-11-05 17:25:54 +010052 unsigned long addr, end;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010053 int i, res = -1;
Keith Manntheyb6846642005-07-28 21:15:38 -070054
travis@sgi.com43238382008-01-30 13:33:25 +010055 memset(memnodemap, 0xff, sizeof(s16)*memnodemapsize);
Eric Dumazet529a3402005-11-05 17:25:54 +010056 for (i = 0; i < numnodes; i++) {
57 addr = nodes[i].start;
58 end = nodes[i].end;
59 if (addr >= end)
60 continue;
Amul Shah076422d2007-02-13 13:26:19 +010061 if ((end >> shift) >= memnodemapsize)
Eric Dumazet529a3402005-11-05 17:25:54 +010062 return 0;
63 do {
travis@sgi.com43238382008-01-30 13:33:25 +010064 if (memnodemap[addr >> shift] != NUMA_NO_NODE)
Eric Dumazet529a3402005-11-05 17:25:54 +010065 return -1;
Tejun Heo8968dab2011-02-16 17:11:08 +010066 memnodemap[addr >> shift] = nodeids[i];
Amul Shah076422d2007-02-13 13:26:19 +010067 addr += (1UL << shift);
Eric Dumazet529a3402005-11-05 17:25:54 +010068 } while (addr < end);
69 res = 1;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010070 }
Eric Dumazet529a3402005-11-05 17:25:54 +010071 return res;
72}
73
Amul Shah076422d2007-02-13 13:26:19 +010074static int __init allocate_cachealigned_memnodemap(void)
75{
Yinghai Lu24a5da72008-02-01 17:49:41 +010076 unsigned long addr;
Amul Shah076422d2007-02-13 13:26:19 +010077
78 memnodemap = memnode.embedded_map;
travis@sgi.com316390b2008-01-30 13:33:15 +010079 if (memnodemapsize <= ARRAY_SIZE(memnode.embedded_map))
Amul Shah076422d2007-02-13 13:26:19 +010080 return 0;
Amul Shah076422d2007-02-13 13:26:19 +010081
Yinghai Lu24a5da72008-02-01 17:49:41 +010082 addr = 0x8000;
Joerg Roedelbe3e89e2008-07-25 16:48:58 +020083 nodemap_size = roundup(sizeof(s16) * memnodemapsize, L1_CACHE_BYTES);
Yinghai Ludbef7b52010-12-27 16:48:08 -080084 nodemap_addr = memblock_find_in_range(addr, get_max_mapped(),
Yinghai Lu24a5da72008-02-01 17:49:41 +010085 nodemap_size, L1_CACHE_BYTES);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070086 if (nodemap_addr == MEMBLOCK_ERROR) {
Amul Shah076422d2007-02-13 13:26:19 +010087 printk(KERN_ERR
88 "NUMA: Unable to allocate Memory to Node hash map\n");
89 nodemap_addr = nodemap_size = 0;
90 return -1;
91 }
Yinghai Lu24a5da72008-02-01 17:49:41 +010092 memnodemap = phys_to_virt(nodemap_addr);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070093 memblock_x86_reserve_range(nodemap_addr, nodemap_addr + nodemap_size, "MEMNODEMAP");
Amul Shah076422d2007-02-13 13:26:19 +010094
95 printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n",
96 nodemap_addr, nodemap_addr + nodemap_size);
97 return 0;
98}
99
100/*
101 * The LSB of all start and end addresses in the node map is the value of the
102 * maximum possible shift.
103 */
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100104static int __init extract_lsb_from_nodes(const struct bootnode *nodes,
105 int numnodes)
Amul Shah076422d2007-02-13 13:26:19 +0100106{
Amul Shah54413922007-02-13 13:26:20 +0100107 int i, nodes_used = 0;
Amul Shah076422d2007-02-13 13:26:19 +0100108 unsigned long start, end;
109 unsigned long bitfield = 0, memtop = 0;
110
111 for (i = 0; i < numnodes; i++) {
112 start = nodes[i].start;
113 end = nodes[i].end;
114 if (start >= end)
115 continue;
Amul Shah54413922007-02-13 13:26:20 +0100116 bitfield |= start;
117 nodes_used++;
Amul Shah076422d2007-02-13 13:26:19 +0100118 if (end > memtop)
119 memtop = end;
120 }
Amul Shah54413922007-02-13 13:26:20 +0100121 if (nodes_used <= 1)
122 i = 63;
123 else
124 i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
Amul Shah076422d2007-02-13 13:26:19 +0100125 memnodemapsize = (memtop >> i)+1;
126 return i;
127}
128
Tejun Heo43a662f2011-02-16 17:11:08 +0100129static int __init compute_hash_shift(struct bootnode *nodes, int numnodes,
130 int *nodeids)
Eric Dumazet529a3402005-11-05 17:25:54 +0100131{
Amul Shah076422d2007-02-13 13:26:19 +0100132 int shift;
Eric Dumazet529a3402005-11-05 17:25:54 +0100133
Amul Shah076422d2007-02-13 13:26:19 +0100134 shift = extract_lsb_from_nodes(nodes, numnodes);
135 if (allocate_cachealigned_memnodemap())
136 return -1;
Andi Kleen6b050f82006-01-11 22:44:33 +0100137 printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
Eric Dumazet529a3402005-11-05 17:25:54 +0100138 shift);
139
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700140 if (populate_memnodemap(nodes, numnodes, shift, nodeids) != 1) {
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100141 printk(KERN_INFO "Your memory is not aligned you need to "
142 "rebuild your kernel with a bigger NODEMAPSIZE "
143 "shift=%d\n", shift);
Eric Dumazet529a3402005-11-05 17:25:54 +0100144 return -1;
145 }
Keith Manntheyb6846642005-07-28 21:15:38 -0700146 return shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -0800149int __meminit __early_pfn_to_nid(unsigned long pfn)
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700150{
151 return phys_to_nid(pfn << PAGE_SHIFT);
152}
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700153
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100154static void * __init early_node_mem(int nodeid, unsigned long start,
Yinghai Lu24a5da72008-02-01 17:49:41 +0100155 unsigned long end, unsigned long size,
156 unsigned long align)
Andi Kleena8062232006-04-07 19:49:21 +0200157{
Yinghai Lucef625e2010-02-10 01:20:18 -0800158 unsigned long mem;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100159
Yinghai Lucef625e2010-02-10 01:20:18 -0800160 /*
161 * put it on high as possible
162 * something will go with NODE_DATA
163 */
164 if (start < (MAX_DMA_PFN<<PAGE_SHIFT))
165 start = MAX_DMA_PFN<<PAGE_SHIFT;
166 if (start < (MAX_DMA32_PFN<<PAGE_SHIFT) &&
167 end > (MAX_DMA32_PFN<<PAGE_SHIFT))
168 start = MAX_DMA32_PFN<<PAGE_SHIFT;
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700169 mem = memblock_x86_find_in_range_node(nodeid, start, end, size, align);
170 if (mem != MEMBLOCK_ERROR)
Andi Kleena8062232006-04-07 19:49:21 +0200171 return __va(mem);
Yinghai Lu9347e0b2008-02-01 17:49:42 +0100172
Yinghai Lucef625e2010-02-10 01:20:18 -0800173 /* extend the search scope */
174 end = max_pfn_mapped << PAGE_SHIFT;
Yinghai Lu419db272010-10-28 09:50:17 -0700175 start = MAX_DMA_PFN << PAGE_SHIFT;
176 mem = memblock_find_in_range(start, end, size, align);
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700177 if (mem != MEMBLOCK_ERROR)
Yinghai Lu1842f902010-02-10 01:20:15 -0800178 return __va(mem);
179
180 printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100181 size, nodeid);
Yinghai Lu1842f902010-02-10 01:20:15 -0800182
183 return NULL;
Andi Kleena8062232006-04-07 19:49:21 +0200184}
185
Tejun Heoef396ec2011-02-16 17:11:07 +0100186static __init int conflicting_memblks(unsigned long start, unsigned long end)
187{
188 int i;
189 for (i = 0; i < num_node_memblks; i++) {
190 struct bootnode *nd = &node_memblk_range[i];
191 if (nd->start == nd->end)
192 continue;
193 if (nd->end > start && nd->start < end)
194 return memblk_nodeid[i];
195 if (nd->end == end && nd->start == start)
196 return memblk_nodeid[i];
197 }
198 return -1;
199}
200
201int __init numa_add_memblk(int nid, u64 start, u64 end)
202{
203 int i;
204
205 i = conflicting_memblks(start, end);
206 if (i == nid) {
207 printk(KERN_WARNING "NUMA: Warning: node %d (%Lx-%Lx) overlaps with itself (%Lx-%Lx)\n",
208 nid, start, end, numa_nodes[i].start, numa_nodes[i].end);
209 } else if (i >= 0) {
210 printk(KERN_ERR "NUMA: node %d (%Lx-%Lx) overlaps with node %d (%Lx-%Lx)\n",
211 nid, start, end, i,
212 numa_nodes[i].start, numa_nodes[i].end);
213 return -EINVAL;
214 }
215
216 node_memblk_range[num_node_memblks].start = start;
217 node_memblk_range[num_node_memblks].end = end;
218 memblk_nodeid[num_node_memblks] = nid;
219 num_node_memblks++;
220 return 0;
221}
222
Tejun Heo206e4202011-02-16 12:13:07 +0100223static __init void cutoff_node(int i, unsigned long start, unsigned long end)
224{
225 struct bootnode *nd = &numa_nodes[i];
226
227 if (nd->start < start) {
228 nd->start = start;
229 if (nd->end < nd->start)
230 nd->start = nd->end;
231 }
232 if (nd->end > end) {
233 nd->end = end;
234 if (nd->start > nd->end)
235 nd->start = nd->end;
236 }
237}
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239/* Initialize bootmem allocator for a node */
Yinghai Lu7c437692009-05-15 13:59:37 -0700240void __init
241setup_node_bootmem(int nodeid, unsigned long start, unsigned long end)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100242{
Yinghai Lu08677212010-02-10 01:20:20 -0800243 unsigned long start_pfn, last_pfn, nodedata_phys;
Yinghai Lu7c437692009-05-15 13:59:37 -0700244 const int pgdat_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
Yinghai Lu1a27fc02008-03-18 12:52:37 -0700245 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Yinghai Lu4c31e922009-04-22 14:19:27 -0700247 if (!end)
248 return;
249
Yinghai Lu7c437692009-05-15 13:59:37 -0700250 /*
251 * Don't confuse VM with a node that doesn't have the
252 * minimum amount of memory:
253 */
254 if (end && (end - start) < NODE_MIN_SIZE)
255 return;
256
Joerg Roedelbe3e89e2008-07-25 16:48:58 +0200257 start = roundup(start, ZONE_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Yinghai Lu08677212010-02-10 01:20:20 -0800259 printk(KERN_INFO "Initmem setup node %d %016lx-%016lx\n", nodeid,
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100260 start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 start_pfn = start >> PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200263 last_pfn = end >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Yinghai Lu24a5da72008-02-01 17:49:41 +0100265 node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
266 SMP_CACHE_BYTES);
Andi Kleena8062232006-04-07 19:49:21 +0200267 if (node_data[nodeid] == NULL)
268 return;
269 nodedata_phys = __pa(node_data[nodeid]);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700270 memblock_x86_reserve_range(nodedata_phys, nodedata_phys + pgdat_size, "NODE_DATA");
Yinghai Lu6118f762008-02-04 16:47:56 +0100271 printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
272 nodedata_phys + pgdat_size - 1);
Yinghai Lu1842f902010-02-10 01:20:15 -0800273 nid = phys_to_nid(nodedata_phys);
274 if (nid != nodeid)
275 printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nodeid, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
Yinghai Lu08677212010-02-10 01:20:20 -0800278 NODE_DATA(nodeid)->node_id = nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 NODE_DATA(nodeid)->node_start_pfn = start_pfn;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200280 NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 node_set_online(nodeid);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100283}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Tejun Heofd0435d2011-02-16 17:11:08 +0100285/*
286 * Sanity check to catch more bad NUMA configurations (they are amazingly
287 * common). Make sure the nodes cover all memory.
288 */
289static int __init nodes_cover_memory(const struct bootnode *nodes)
290{
291 unsigned long numaram, e820ram;
292 int i;
293
294 numaram = 0;
295 for_each_node_mask(i, mem_nodes_parsed) {
296 unsigned long s = nodes[i].start >> PAGE_SHIFT;
297 unsigned long e = nodes[i].end >> PAGE_SHIFT;
298 numaram += e - s;
299 numaram -= __absent_pages_in_range(i, s, e);
300 if ((long)numaram < 0)
301 numaram = 0;
302 }
303
304 e820ram = max_pfn -
305 (memblock_x86_hole_size(0, max_pfn<<PAGE_SHIFT) >> PAGE_SHIFT);
306 /* We seem to lose 3 pages somewhere. Allow 1M of slack. */
307 if ((long)(e820ram - numaram) >= (1<<(20 - PAGE_SHIFT))) {
308 printk(KERN_ERR "NUMA: nodes only cover %luMB of your %luMB e820 RAM. Not used.\n",
309 (numaram << PAGE_SHIFT) >> 20,
310 (e820ram << PAGE_SHIFT) >> 20);
311 return 0;
312 }
313 return 1;
314}
315
Tejun Heo43a662f2011-02-16 17:11:08 +0100316static int __init numa_register_memblks(void)
Tejun Heoef396ec2011-02-16 17:11:07 +0100317{
318 int i;
319
320 /*
321 * Join together blocks on the same node, holes between
322 * which don't overlap with memory on other nodes.
323 */
324 for (i = 0; i < num_node_memblks; ++i) {
325 int j, k;
326
327 for (j = i + 1; j < num_node_memblks; ++j) {
328 unsigned long start, end;
329
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 "NUMA: Node %d [%Lx,%Lx) + [%Lx,%Lx) -> [%lx,%lx)\n",
350 memblk_nodeid[i],
351 node_memblk_range[i].start,
352 node_memblk_range[i].end,
353 node_memblk_range[j].start,
354 node_memblk_range[j].end,
355 start, end);
356 node_memblk_range[i].start = start;
357 node_memblk_range[i].end = end;
358 k = --num_node_memblks - j;
359 memmove(memblk_nodeid + j, memblk_nodeid + j+1,
360 k * sizeof(*memblk_nodeid));
361 memmove(node_memblk_range + j, node_memblk_range + j+1,
362 k * sizeof(*node_memblk_range));
363 --j;
364 }
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 "NUMA: No NUMA node hash function found. Contact maintainer\n");
371 return -EINVAL;
372 }
373
374 for (i = 0; i < num_node_memblks; i++)
375 memblock_x86_register_active_regions(memblk_nodeid[i],
376 node_memblk_range[i].start >> PAGE_SHIFT,
377 node_memblk_range[i].end >> PAGE_SHIFT);
Tejun Heofd0435d2011-02-16 17:11:08 +0100378
379 /* for out of order entries */
380 sort_node_map();
381 if (!nodes_cover_memory(numa_nodes))
382 return -EINVAL;
383
384 init_memory_mapping_high();
385
386 /* Finally register nodes. */
387 for_each_node_mask(i, node_possible_map)
388 setup_node_bootmem(i, numa_nodes[i].start, numa_nodes[i].end);
389
390 /*
391 * Try again in case setup_node_bootmem missed one due to missing
392 * bootmem.
393 */
394 for_each_node_mask(i, node_possible_map)
395 if (!node_online(i))
396 setup_node_bootmem(i, numa_nodes[i].start,
397 numa_nodes[i].end);
398
Tejun Heoef396ec2011-02-16 17:11:07 +0100399 return 0;
400}
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402#ifdef CONFIG_NUMA_EMU
Rohit Seth53fee042007-02-13 13:26:22 +0100403/* Numa emulation */
David Rientjesadc19382009-09-25 15:20:09 -0700404static struct bootnode nodes[MAX_NUMNODES] __initdata;
David Rientjesc1c34432010-12-22 17:23:54 -0800405static struct bootnode physnodes[MAX_NUMNODES] __cpuinitdata;
Thomas Gleixner864fc312008-05-12 15:43:36 +0200406static char *cmdline __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Jan Beulich90321602011-01-19 08:57:21 +0000408void __init numa_emu_cmdline(char *str)
409{
410 cmdline = str;
411}
412
Tejun Heo190955482011-02-16 12:13:07 +0100413static int __init setup_physnodes(unsigned long start, unsigned long end)
David Rientjesadc19382009-09-25 15:20:09 -0700414{
David Rientjesadc19382009-09-25 15:20:09 -0700415 int ret = 0;
416 int i;
417
David Rientjesc1c34432010-12-22 17:23:54 -0800418 memset(physnodes, 0, sizeof(physnodes));
Tejun Heo190955482011-02-16 12:13:07 +0100419
420 for_each_node_mask(i, mem_nodes_parsed) {
421 physnodes[i].start = numa_nodes[i].start;
422 physnodes[i].end = numa_nodes[i].end;
423 }
424
David Rientjesadc19382009-09-25 15:20:09 -0700425 /*
426 * Basic sanity checking on the physical node map: there may be errors
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200427 * if the SRAT or AMD code incorrectly reported the topology or the mem=
David Rientjesadc19382009-09-25 15:20:09 -0700428 * kernel parameter is used.
429 */
David Rientjesa387e952010-12-22 17:23:56 -0800430 for (i = 0; i < MAX_NUMNODES; i++) {
David Rientjesadc19382009-09-25 15:20:09 -0700431 if (physnodes[i].start == physnodes[i].end)
432 continue;
433 if (physnodes[i].start > end) {
434 physnodes[i].end = physnodes[i].start;
435 continue;
436 }
437 if (physnodes[i].end < start) {
438 physnodes[i].start = physnodes[i].end;
439 continue;
440 }
441 if (physnodes[i].start < start)
442 physnodes[i].start = start;
443 if (physnodes[i].end > end)
444 physnodes[i].end = end;
David Rientjesadc19382009-09-25 15:20:09 -0700445 ret++;
446 }
447
448 /*
449 * If no physical topology was detected, a single node is faked to cover
450 * the entire address space.
451 */
452 if (!ret) {
453 physnodes[ret].start = start;
454 physnodes[ret].end = end;
455 ret = 1;
456 }
457 return ret;
458}
459
David Rientjesf51bf302010-12-22 17:23:51 -0800460static void __init fake_physnodes(int acpi, int amd, int nr_nodes)
461{
462 int i;
463
464 BUG_ON(acpi && amd);
465#ifdef CONFIG_ACPI_NUMA
466 if (acpi)
467 acpi_fake_nodes(nodes, nr_nodes);
468#endif
469#ifdef CONFIG_AMD_NUMA
470 if (amd)
471 amd_fake_nodes(nodes, nr_nodes);
472#endif
473 if (!acpi && !amd)
474 for (i = 0; i < nr_cpu_ids; i++)
475 numa_set_node(i, 0);
476}
477
Rohit Seth53fee042007-02-13 13:26:22 +0100478/*
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100479 * Setups up nid to range from addr to addr + size. If the end
480 * boundary is greater than max_addr, then max_addr is used instead.
481 * The return value is 0 if there is additional memory left for
482 * allocation past addr and -1 otherwise. addr is adjusted to be at
483 * the end of the node.
Rohit Seth53fee042007-02-13 13:26:22 +0100484 */
David Rientjesadc19382009-09-25 15:20:09 -0700485static int __init setup_node_range(int nid, u64 *addr, u64 size, u64 max_addr)
Rohit Seth53fee042007-02-13 13:26:22 +0100486{
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200487 int ret = 0;
488 nodes[nid].start = *addr;
489 *addr += size;
490 if (*addr >= max_addr) {
491 *addr = max_addr;
492 ret = -1;
493 }
494 nodes[nid].end = *addr;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200495 node_set(nid, node_possible_map);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200496 printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
497 nodes[nid].start, nodes[nid].end,
498 (nodes[nid].end - nodes[nid].start) >> 20);
499 return ret;
Rohit Seth53fee042007-02-13 13:26:22 +0100500}
501
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200502/*
David Rientjesadc19382009-09-25 15:20:09 -0700503 * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
504 * to max_addr. The return value is the number of nodes allocated.
505 */
David Rientjesc1c34432010-12-22 17:23:54 -0800506static int __init split_nodes_interleave(u64 addr, u64 max_addr, int nr_nodes)
David Rientjesadc19382009-09-25 15:20:09 -0700507{
508 nodemask_t physnode_mask = NODE_MASK_NONE;
509 u64 size;
510 int big;
511 int ret = 0;
512 int i;
513
514 if (nr_nodes <= 0)
515 return -1;
516 if (nr_nodes > MAX_NUMNODES) {
517 pr_info("numa=fake=%d too large, reducing to %d\n",
518 nr_nodes, MAX_NUMNODES);
519 nr_nodes = MAX_NUMNODES;
520 }
521
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700522 size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) / nr_nodes;
David Rientjesadc19382009-09-25 15:20:09 -0700523 /*
524 * Calculate the number of big nodes that can be allocated as a result
525 * of consolidating the remainder.
526 */
David Rientjes68fd1112010-02-15 13:43:25 -0800527 big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
David Rientjesadc19382009-09-25 15:20:09 -0700528 FAKE_NODE_MIN_SIZE;
529
530 size &= FAKE_NODE_MIN_HASH_MASK;
531 if (!size) {
532 pr_err("Not enough memory for each node. "
533 "NUMA emulation disabled.\n");
534 return -1;
535 }
536
David Rientjesc1c34432010-12-22 17:23:54 -0800537 for (i = 0; i < MAX_NUMNODES; i++)
David Rientjesadc19382009-09-25 15:20:09 -0700538 if (physnodes[i].start != physnodes[i].end)
539 node_set(i, physnode_mask);
540
541 /*
542 * Continue to fill physical nodes with fake nodes until there is no
543 * memory left on any of them.
544 */
545 while (nodes_weight(physnode_mask)) {
546 for_each_node_mask(i, physnode_mask) {
547 u64 end = physnodes[i].start + size;
548 u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
549
550 if (ret < big)
551 end += FAKE_NODE_MIN_SIZE;
552
553 /*
554 * Continue to add memory to this fake node if its
555 * non-reserved memory is less than the per-node size.
556 */
557 while (end - physnodes[i].start -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700558 memblock_x86_hole_size(physnodes[i].start, end) < size) {
David Rientjesadc19382009-09-25 15:20:09 -0700559 end += FAKE_NODE_MIN_SIZE;
560 if (end > physnodes[i].end) {
561 end = physnodes[i].end;
562 break;
563 }
564 }
565
566 /*
567 * If there won't be at least FAKE_NODE_MIN_SIZE of
568 * non-reserved memory in ZONE_DMA32 for the next node,
569 * this one must extend to the boundary.
570 */
571 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700572 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjesadc19382009-09-25 15:20:09 -0700573 end = dma32_end;
574
575 /*
576 * If there won't be enough non-reserved memory for the
577 * next node, this one must extend to the end of the
578 * physical node.
579 */
580 if (physnodes[i].end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700581 memblock_x86_hole_size(end, physnodes[i].end) < size)
David Rientjesadc19382009-09-25 15:20:09 -0700582 end = physnodes[i].end;
583
584 /*
585 * Avoid allocating more nodes than requested, which can
586 * happen as a result of rounding down each node's size
587 * to FAKE_NODE_MIN_SIZE.
588 */
589 if (nodes_weight(physnode_mask) + ret >= nr_nodes)
590 end = physnodes[i].end;
591
592 if (setup_node_range(ret++, &physnodes[i].start,
593 end - physnodes[i].start,
594 physnodes[i].end) < 0)
595 node_clear(i, physnode_mask);
596 }
597 }
598 return ret;
599}
600
601/*
David Rientjes8df5bb342010-02-15 13:43:30 -0800602 * Returns the end address of a node so that there is at least `size' amount of
603 * non-reserved memory or `max_addr' is reached.
604 */
605static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
606{
607 u64 end = start + size;
608
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700609 while (end - start - memblock_x86_hole_size(start, end) < size) {
David Rientjes8df5bb342010-02-15 13:43:30 -0800610 end += FAKE_NODE_MIN_SIZE;
611 if (end > max_addr) {
612 end = max_addr;
613 break;
614 }
615 }
616 return end;
617}
618
619/*
620 * Sets up fake nodes of `size' interleaved over physical nodes ranging from
621 * `addr' to `max_addr'. The return value is the number of nodes allocated.
622 */
623static int __init split_nodes_size_interleave(u64 addr, u64 max_addr, u64 size)
624{
625 nodemask_t physnode_mask = NODE_MASK_NONE;
626 u64 min_size;
627 int ret = 0;
628 int i;
629
630 if (!size)
631 return -1;
632 /*
633 * The limit on emulated nodes is MAX_NUMNODES, so the size per node is
634 * increased accordingly if the requested size is too small. This
635 * creates a uniform distribution of node sizes across the entire
636 * machine (but not necessarily over physical nodes).
637 */
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700638 min_size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) /
David Rientjes8df5bb342010-02-15 13:43:30 -0800639 MAX_NUMNODES;
640 min_size = max(min_size, FAKE_NODE_MIN_SIZE);
641 if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size)
642 min_size = (min_size + FAKE_NODE_MIN_SIZE) &
643 FAKE_NODE_MIN_HASH_MASK;
644 if (size < min_size) {
645 pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
646 size >> 20, min_size >> 20);
647 size = min_size;
648 }
649 size &= FAKE_NODE_MIN_HASH_MASK;
650
651 for (i = 0; i < MAX_NUMNODES; i++)
652 if (physnodes[i].start != physnodes[i].end)
653 node_set(i, physnode_mask);
654 /*
655 * Fill physical nodes with fake nodes of size until there is no memory
656 * left on any of them.
657 */
658 while (nodes_weight(physnode_mask)) {
659 for_each_node_mask(i, physnode_mask) {
660 u64 dma32_end = MAX_DMA32_PFN << PAGE_SHIFT;
661 u64 end;
662
663 end = find_end_of_node(physnodes[i].start,
664 physnodes[i].end, size);
665 /*
666 * If there won't be at least FAKE_NODE_MIN_SIZE of
667 * non-reserved memory in ZONE_DMA32 for the next node,
668 * this one must extend to the boundary.
669 */
670 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700671 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjes8df5bb342010-02-15 13:43:30 -0800672 end = dma32_end;
673
674 /*
675 * If there won't be enough non-reserved memory for the
676 * next node, this one must extend to the end of the
677 * physical node.
678 */
679 if (physnodes[i].end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700680 memblock_x86_hole_size(end, physnodes[i].end) < size)
David Rientjes8df5bb342010-02-15 13:43:30 -0800681 end = physnodes[i].end;
682
683 /*
684 * Setup the fake node that will be allocated as bootmem
685 * later. If setup_node_range() returns non-zero, there
686 * is no more memory available on this physical node.
687 */
688 if (setup_node_range(ret++, &physnodes[i].start,
689 end - physnodes[i].start,
690 physnodes[i].end) < 0)
691 node_clear(i, physnode_mask);
692 }
693 }
694 return ret;
695}
696
697/*
Thomas Gleixner886533a2008-05-12 15:43:36 +0200698 * Sets up the system RAM area from start_pfn to last_pfn according to the
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200699 * numa=fake command-line option.
700 */
David Rientjesadc19382009-09-25 15:20:09 -0700701static int __init numa_emulation(unsigned long start_pfn,
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200702 unsigned long last_pfn, int acpi, int amd)
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200703{
Tejun Heo8968dab2011-02-16 17:11:08 +0100704 static int nodeid[NR_NODE_MEMBLKS] __initdata;
David Rientjesca2107c2010-02-15 13:43:33 -0800705 u64 addr = start_pfn << PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200706 u64 max_addr = last_pfn << PAGE_SHIFT;
David Rientjesca2107c2010-02-15 13:43:33 -0800707 int num_nodes;
708 int i;
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200709
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200710 /*
David Rientjes8df5bb342010-02-15 13:43:30 -0800711 * If the numa=fake command-line contains a 'M' or 'G', it represents
David Rientjesca2107c2010-02-15 13:43:33 -0800712 * the fixed node size. Otherwise, if it is just a single number N,
713 * split the system RAM into N fake nodes.
David Rientjes8df5bb342010-02-15 13:43:30 -0800714 */
715 if (strchr(cmdline, 'M') || strchr(cmdline, 'G')) {
David Rientjesca2107c2010-02-15 13:43:33 -0800716 u64 size;
717
David Rientjes8df5bb342010-02-15 13:43:30 -0800718 size = memparse(cmdline, &cmdline);
719 num_nodes = split_nodes_size_interleave(addr, max_addr, size);
David Rientjesca2107c2010-02-15 13:43:33 -0800720 } else {
721 unsigned long n;
722
723 n = simple_strtoul(cmdline, NULL, 0);
David Rientjesc1c34432010-12-22 17:23:54 -0800724 num_nodes = split_nodes_interleave(addr, max_addr, n);
David Rientjes8df5bb342010-02-15 13:43:30 -0800725 }
726
David Rientjesca2107c2010-02-15 13:43:33 -0800727 if (num_nodes < 0)
728 return num_nodes;
Tejun Heo8968dab2011-02-16 17:11:08 +0100729
730 for (i = 0; i < ARRAY_SIZE(nodeid); i++)
731 nodeid[i] = i;
732
733 memnode_shift = compute_hash_shift(nodes, num_nodes, nodeid);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200734 if (memnode_shift < 0) {
735 memnode_shift = 0;
736 printk(KERN_ERR "No NUMA hash function found. NUMA emulation "
737 "disabled.\n");
738 return -1;
739 }
740
741 /*
David Rientjesadc19382009-09-25 15:20:09 -0700742 * We need to vacate all active ranges that may have been registered for
743 * the e820 memory map.
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200744 */
745 remove_all_active_ranges();
Yinghai Lu1411e0e2010-12-27 16:48:17 -0800746 for_each_node_mask(i, node_possible_map)
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700747 memblock_x86_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
Mel Gorman5cb248a2006-09-27 01:49:52 -0700748 nodes[i].end >> PAGE_SHIFT);
Yinghai Lu1411e0e2010-12-27 16:48:17 -0800749 init_memory_mapping_high();
750 for_each_node_mask(i, node_possible_map)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100751 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
Tejun Heo190955482011-02-16 12:13:07 +0100752 setup_physnodes(addr, max_addr);
David Rientjesf51bf302010-12-22 17:23:51 -0800753 fake_physnodes(acpi, amd, num_nodes);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100754 numa_init_array();
755 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200757#endif /* CONFIG_NUMA_EMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Tejun Heoffe77a42011-02-16 12:13:06 +0100759static int dummy_numa_init(void)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100760{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 printk(KERN_INFO "%s\n",
762 numa_off ? "NUMA turned off" : "No NUMA configuration found");
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100763 printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
Tejun Heo86ef4db2011-02-16 12:13:06 +0100764 0LU, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100765
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100766 node_set(0, cpu_nodes_parsed);
767 node_set(0, mem_nodes_parsed);
Tejun Heo43a662f2011-02-16 17:11:08 +0100768 numa_add_memblk(0, 0, (u64)max_pfn << PAGE_SHIFT);
Tejun Heofd0435d2011-02-16 17:11:08 +0100769 numa_nodes[0].start = 0;
770 numa_nodes[0].end = (u64)max_pfn << PAGE_SHIFT;
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100771
772 return 0;
773}
774
Tejun Heoffe77a42011-02-16 12:13:06 +0100775void __init initmem_init(void)
776{
777 int (*numa_init[])(void) = { [2] = dummy_numa_init };
Tejun Heoffe77a42011-02-16 12:13:06 +0100778 int i, j;
779
780 if (!numa_off) {
781#ifdef CONFIG_ACPI_NUMA
782 numa_init[0] = x86_acpi_numa_init;
Tejun Heoffe77a42011-02-16 12:13:06 +0100783#endif
784#ifdef CONFIG_AMD_NUMA
785 numa_init[1] = amd_numa_init;
Tejun Heoffe77a42011-02-16 12:13:06 +0100786#endif
787 }
788
789 for (i = 0; i < ARRAY_SIZE(numa_init); i++) {
790 if (!numa_init[i])
791 continue;
792
793 for (j = 0; j < MAX_LOCAL_APIC; j++)
794 set_apicid_to_node(j, NUMA_NO_NODE);
795
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100796 nodes_clear(cpu_nodes_parsed);
797 nodes_clear(mem_nodes_parsed);
Tejun Heoffe77a42011-02-16 12:13:06 +0100798 nodes_clear(node_possible_map);
799 nodes_clear(node_online_map);
Tejun Heoef396ec2011-02-16 17:11:07 +0100800 num_node_memblks = 0;
801 memset(node_memblk_range, 0, sizeof(node_memblk_range));
802 memset(memblk_nodeid, 0, sizeof(memblk_nodeid));
Tejun Heo206e4202011-02-16 12:13:07 +0100803 memset(numa_nodes, 0, sizeof(numa_nodes));
Tejun Heofd0435d2011-02-16 17:11:08 +0100804 remove_all_active_ranges();
Tejun Heoffe77a42011-02-16 12:13:06 +0100805
806 if (numa_init[i]() < 0)
807 continue;
Tejun Heo206e4202011-02-16 12:13:07 +0100808
809 /* clean up the node list */
810 for (j = 0; j < MAX_NUMNODES; j++)
811 cutoff_node(j, 0, max_pfn << PAGE_SHIFT);
812
Tejun Heoffe77a42011-02-16 12:13:06 +0100813#ifdef CONFIG_NUMA_EMU
Tejun Heo190955482011-02-16 12:13:07 +0100814 setup_physnodes(0, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100815 if (cmdline && !numa_emulation(0, max_pfn, i == 0, i == 1))
816 return;
Tejun Heo190955482011-02-16 12:13:07 +0100817 setup_physnodes(0, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100818 nodes_clear(node_possible_map);
819 nodes_clear(node_online_map);
820#endif
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100821 /* Account for nodes with cpus and no memory */
822 nodes_or(node_possible_map, mem_nodes_parsed, cpu_nodes_parsed);
823 if (WARN_ON(nodes_empty(node_possible_map)))
824 continue;
825
Tejun Heo43a662f2011-02-16 17:11:08 +0100826 if (numa_register_memblks() < 0)
827 continue;
828
Tejun Heofd0435d2011-02-16 17:11:08 +0100829 for (j = 0; j < nr_cpu_ids; j++) {
830 int nid = early_cpu_to_node(j);
831
832 if (nid == NUMA_NO_NODE)
833 continue;
834 if (!node_online(nid))
835 numa_clear_node(j);
836 }
837 numa_init_array();
838 return;
Tejun Heoffe77a42011-02-16 12:13:06 +0100839 }
840 BUG();
Andi Kleen69d81fc2005-11-05 17:25:53 +0100841}
842
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100843unsigned long __init numa_free_all_bootmem(void)
844{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 unsigned long pages = 0;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100846 int i;
847
848 for_each_online_node(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 pages += free_all_bootmem_node(NODE_DATA(i));
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100850
Yinghai Lu08677212010-02-10 01:20:20 -0800851 pages += free_all_memory_core_early(MAX_NUMNODES);
Yinghai Lu08677212010-02-10 01:20:20 -0800852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return pages;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100854}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100856int __cpuinit numa_cpu_node(int cpu)
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800857{
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100858 int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800859
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100860 if (apicid != BAD_APICID)
861 return __apicid_to_node[apicid];
862 return NUMA_NO_NODE;
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800863}
864
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100865/*
Tejun Heode2d9442011-01-23 14:37:41 +0100866 * UGLINESS AHEAD: Currently, CONFIG_NUMA_EMU is 64bit only and makes use
867 * of 64bit specific data structures. The distinction is artificial and
868 * should be removed. numa_{add|remove}_cpu() are implemented in numa.c
869 * for both 32 and 64bit when CONFIG_NUMA_EMU is disabled but here when
870 * enabled.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100871 *
Tejun Heode2d9442011-01-23 14:37:41 +0100872 * NUMA emulation is planned to be made generic and the following and other
873 * related code should be moved to numa.c.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100874 */
Tejun Heode2d9442011-01-23 14:37:41 +0100875#ifdef CONFIG_NUMA_EMU
876# ifndef CONFIG_DEBUG_PER_CPU_MAPS
David Rientjesc1c34432010-12-22 17:23:54 -0800877void __cpuinit numa_add_cpu(int cpu)
878{
879 unsigned long addr;
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100880 int physnid, nid;
David Rientjesc1c34432010-12-22 17:23:54 -0800881
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100882 nid = numa_cpu_node(cpu);
David Rientjesc1c34432010-12-22 17:23:54 -0800883 if (nid == NUMA_NO_NODE)
884 nid = early_cpu_to_node(cpu);
885 BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
886
887 /*
888 * Use the starting address of the emulated node to find which physical
889 * node it is allocated on.
890 */
891 addr = node_start_pfn(nid) << PAGE_SHIFT;
892 for (physnid = 0; physnid < MAX_NUMNODES; physnid++)
893 if (addr >= physnodes[physnid].start &&
894 addr < physnodes[physnid].end)
895 break;
896
897 /*
898 * Map the cpu to each emulated node that is allocated on the physical
899 * node of the cpu's apic id.
900 */
901 for_each_online_node(nid) {
902 addr = node_start_pfn(nid) << PAGE_SHIFT;
903 if (addr >= physnodes[physnid].start &&
904 addr < physnodes[physnid].end)
905 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
906 }
907}
908
909void __cpuinit numa_remove_cpu(int cpu)
910{
911 int i;
912
913 for_each_online_node(i)
914 cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
915}
Tejun Heode2d9442011-01-23 14:37:41 +0100916# else /* !CONFIG_DEBUG_PER_CPU_MAPS */
David Rientjesd906f0e2010-12-30 10:54:16 -0800917static void __cpuinit numa_set_cpumask(int cpu, int enable)
918{
919 int node = early_cpu_to_node(cpu);
920 struct cpumask *mask;
David Rientjesc1c34432010-12-22 17:23:54 -0800921 int i;
Brian Gerst6470aff2009-01-27 12:56:47 +0900922
David Rientjes14392fd2011-02-07 14:08:53 -0800923 if (node == NUMA_NO_NODE) {
924 /* early_cpu_to_node() already emits a warning and trace */
925 return;
926 }
David Rientjesc1c34432010-12-22 17:23:54 -0800927 for_each_online_node(i) {
928 unsigned long addr;
929
930 addr = node_start_pfn(i) << PAGE_SHIFT;
931 if (addr < physnodes[node].start ||
932 addr >= physnodes[node].end)
933 continue;
David Rientjesd906f0e2010-12-30 10:54:16 -0800934 mask = debug_cpumask_set_cpu(cpu, enable);
935 if (!mask)
David Rientjesc1c34432010-12-22 17:23:54 -0800936 return;
David Rientjesc1c34432010-12-22 17:23:54 -0800937
938 if (enable)
939 cpumask_set_cpu(cpu, mask);
940 else
941 cpumask_clear_cpu(cpu, mask);
Brian Gerst6470aff2009-01-27 12:56:47 +0900942 }
Brian Gerst6470aff2009-01-27 12:56:47 +0900943}
944
945void __cpuinit numa_add_cpu(int cpu)
946{
947 numa_set_cpumask(cpu, 1);
948}
949
950void __cpuinit numa_remove_cpu(int cpu)
951{
952 numa_set_cpumask(cpu, 0);
953}
Tejun Heode2d9442011-01-23 14:37:41 +0100954# endif /* !CONFIG_DEBUG_PER_CPU_MAPS */
955#endif /* CONFIG_NUMA_EMU */