blob: 4fd3368adc8f0d521e057566bf09314129c53d0e [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
Tejun Heo97e7b782011-02-16 17:11:08 +010025struct numa_memblk {
26 u64 start;
27 u64 end;
28 int nid;
29};
30
31struct numa_meminfo {
32 int nr_blks;
33 struct numa_memblk blk[NR_NODE_MEMBLKS];
34};
35
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -070036struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010037EXPORT_SYMBOL(node_data);
38
Tejun Heoec8cf29b2011-02-16 12:13:07 +010039nodemask_t cpu_nodes_parsed __initdata;
40nodemask_t mem_nodes_parsed __initdata;
41
Eric Dumazetdcf36bf2006-03-25 16:31:46 +010042struct memnode memnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Thomas Gleixner864fc312008-05-12 15:43:36 +020044static unsigned long __initdata nodemap_addr;
45static unsigned long __initdata nodemap_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Tejun Heo97e7b782011-02-16 17:11:08 +010047static struct numa_meminfo numa_meminfo __initdata;
Tejun Heoef396ec2011-02-16 17:11:07 +010048
Tejun Heo206e4202011-02-16 12:13:07 +010049struct bootnode numa_nodes[MAX_NUMNODES] __initdata;
50
Brian Gerst6470aff2009-01-27 12:56:47 +090051/*
Eric Dumazet529a3402005-11-05 17:25:54 +010052 * Given a shift value, try to populate memnodemap[]
53 * Returns :
54 * 1 if OK
55 * 0 if memnodmap[] too small (of shift too small)
56 * -1 if node overlap or lost ram (shift too big)
57 */
Tejun Heo97e7b782011-02-16 17:11:08 +010058static int __init populate_memnodemap(const struct numa_meminfo *mi, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Eric Dumazet529a3402005-11-05 17:25:54 +010060 unsigned long addr, end;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010061 int i, res = -1;
Keith Manntheyb6846642005-07-28 21:15:38 -070062
travis@sgi.com43238382008-01-30 13:33:25 +010063 memset(memnodemap, 0xff, sizeof(s16)*memnodemapsize);
Tejun Heo97e7b782011-02-16 17:11:08 +010064 for (i = 0; i < mi->nr_blks; i++) {
65 addr = mi->blk[i].start;
66 end = mi->blk[i].end;
Eric Dumazet529a3402005-11-05 17:25:54 +010067 if (addr >= end)
68 continue;
Amul Shah076422d2007-02-13 13:26:19 +010069 if ((end >> shift) >= memnodemapsize)
Eric Dumazet529a3402005-11-05 17:25:54 +010070 return 0;
71 do {
travis@sgi.com43238382008-01-30 13:33:25 +010072 if (memnodemap[addr >> shift] != NUMA_NO_NODE)
Eric Dumazet529a3402005-11-05 17:25:54 +010073 return -1;
Tejun Heo97e7b782011-02-16 17:11:08 +010074 memnodemap[addr >> shift] = mi->blk[i].nid;
Amul Shah076422d2007-02-13 13:26:19 +010075 addr += (1UL << shift);
Eric Dumazet529a3402005-11-05 17:25:54 +010076 } while (addr < end);
77 res = 1;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010078 }
Eric Dumazet529a3402005-11-05 17:25:54 +010079 return res;
80}
81
Amul Shah076422d2007-02-13 13:26:19 +010082static int __init allocate_cachealigned_memnodemap(void)
83{
Yinghai Lu24a5da72008-02-01 17:49:41 +010084 unsigned long addr;
Amul Shah076422d2007-02-13 13:26:19 +010085
86 memnodemap = memnode.embedded_map;
travis@sgi.com316390b2008-01-30 13:33:15 +010087 if (memnodemapsize <= ARRAY_SIZE(memnode.embedded_map))
Amul Shah076422d2007-02-13 13:26:19 +010088 return 0;
Amul Shah076422d2007-02-13 13:26:19 +010089
Yinghai Lu24a5da72008-02-01 17:49:41 +010090 addr = 0x8000;
Joerg Roedelbe3e89e2008-07-25 16:48:58 +020091 nodemap_size = roundup(sizeof(s16) * memnodemapsize, L1_CACHE_BYTES);
Yinghai Ludbef7b52010-12-27 16:48:08 -080092 nodemap_addr = memblock_find_in_range(addr, get_max_mapped(),
Yinghai Lu24a5da72008-02-01 17:49:41 +010093 nodemap_size, L1_CACHE_BYTES);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070094 if (nodemap_addr == MEMBLOCK_ERROR) {
Amul Shah076422d2007-02-13 13:26:19 +010095 printk(KERN_ERR
96 "NUMA: Unable to allocate Memory to Node hash map\n");
97 nodemap_addr = nodemap_size = 0;
98 return -1;
99 }
Yinghai Lu24a5da72008-02-01 17:49:41 +0100100 memnodemap = phys_to_virt(nodemap_addr);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700101 memblock_x86_reserve_range(nodemap_addr, nodemap_addr + nodemap_size, "MEMNODEMAP");
Amul Shah076422d2007-02-13 13:26:19 +0100102
103 printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n",
104 nodemap_addr, nodemap_addr + nodemap_size);
105 return 0;
106}
107
108/*
109 * The LSB of all start and end addresses in the node map is the value of the
110 * maximum possible shift.
111 */
Tejun Heo97e7b782011-02-16 17:11:08 +0100112static int __init extract_lsb_from_nodes(const struct numa_meminfo *mi)
Amul Shah076422d2007-02-13 13:26:19 +0100113{
Amul Shah54413922007-02-13 13:26:20 +0100114 int i, nodes_used = 0;
Amul Shah076422d2007-02-13 13:26:19 +0100115 unsigned long start, end;
116 unsigned long bitfield = 0, memtop = 0;
117
Tejun Heo97e7b782011-02-16 17:11:08 +0100118 for (i = 0; i < mi->nr_blks; i++) {
119 start = mi->blk[i].start;
120 end = mi->blk[i].end;
Amul Shah076422d2007-02-13 13:26:19 +0100121 if (start >= end)
122 continue;
Amul Shah54413922007-02-13 13:26:20 +0100123 bitfield |= start;
124 nodes_used++;
Amul Shah076422d2007-02-13 13:26:19 +0100125 if (end > memtop)
126 memtop = end;
127 }
Amul Shah54413922007-02-13 13:26:20 +0100128 if (nodes_used <= 1)
129 i = 63;
130 else
131 i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
Amul Shah076422d2007-02-13 13:26:19 +0100132 memnodemapsize = (memtop >> i)+1;
133 return i;
134}
135
Tejun Heo97e7b782011-02-16 17:11:08 +0100136static int __init compute_hash_shift(const struct numa_meminfo *mi)
Eric Dumazet529a3402005-11-05 17:25:54 +0100137{
Amul Shah076422d2007-02-13 13:26:19 +0100138 int shift;
Eric Dumazet529a3402005-11-05 17:25:54 +0100139
Tejun Heo97e7b782011-02-16 17:11:08 +0100140 shift = extract_lsb_from_nodes(mi);
Amul Shah076422d2007-02-13 13:26:19 +0100141 if (allocate_cachealigned_memnodemap())
142 return -1;
Andi Kleen6b050f82006-01-11 22:44:33 +0100143 printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
Eric Dumazet529a3402005-11-05 17:25:54 +0100144 shift);
145
Tejun Heo97e7b782011-02-16 17:11:08 +0100146 if (populate_memnodemap(mi, shift) != 1) {
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100147 printk(KERN_INFO "Your memory is not aligned you need to "
148 "rebuild your kernel with a bigger NODEMAPSIZE "
149 "shift=%d\n", shift);
Eric Dumazet529a3402005-11-05 17:25:54 +0100150 return -1;
151 }
Keith Manntheyb6846642005-07-28 21:15:38 -0700152 return shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -0800155int __meminit __early_pfn_to_nid(unsigned long pfn)
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700156{
157 return phys_to_nid(pfn << PAGE_SHIFT);
158}
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700159
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100160static void * __init early_node_mem(int nodeid, unsigned long start,
Yinghai Lu24a5da72008-02-01 17:49:41 +0100161 unsigned long end, unsigned long size,
162 unsigned long align)
Andi Kleena8062232006-04-07 19:49:21 +0200163{
Yinghai Lucef625e2010-02-10 01:20:18 -0800164 unsigned long mem;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100165
Yinghai Lucef625e2010-02-10 01:20:18 -0800166 /*
167 * put it on high as possible
168 * something will go with NODE_DATA
169 */
170 if (start < (MAX_DMA_PFN<<PAGE_SHIFT))
171 start = MAX_DMA_PFN<<PAGE_SHIFT;
172 if (start < (MAX_DMA32_PFN<<PAGE_SHIFT) &&
173 end > (MAX_DMA32_PFN<<PAGE_SHIFT))
174 start = MAX_DMA32_PFN<<PAGE_SHIFT;
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700175 mem = memblock_x86_find_in_range_node(nodeid, start, end, size, align);
176 if (mem != MEMBLOCK_ERROR)
Andi Kleena8062232006-04-07 19:49:21 +0200177 return __va(mem);
Yinghai Lu9347e0b2008-02-01 17:49:42 +0100178
Yinghai Lucef625e2010-02-10 01:20:18 -0800179 /* extend the search scope */
180 end = max_pfn_mapped << PAGE_SHIFT;
Yinghai Lu419db272010-10-28 09:50:17 -0700181 start = MAX_DMA_PFN << PAGE_SHIFT;
182 mem = memblock_find_in_range(start, end, size, align);
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700183 if (mem != MEMBLOCK_ERROR)
Yinghai Lu1842f902010-02-10 01:20:15 -0800184 return __va(mem);
185
186 printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100187 size, nodeid);
Yinghai Lu1842f902010-02-10 01:20:15 -0800188
189 return NULL;
Andi Kleena8062232006-04-07 19:49:21 +0200190}
191
Tejun Heoef396ec2011-02-16 17:11:07 +0100192static __init int conflicting_memblks(unsigned long start, unsigned long end)
193{
Tejun Heo97e7b782011-02-16 17:11:08 +0100194 struct numa_meminfo *mi = &numa_meminfo;
Tejun Heoef396ec2011-02-16 17:11:07 +0100195 int i;
Tejun Heo97e7b782011-02-16 17:11:08 +0100196
197 for (i = 0; i < mi->nr_blks; i++) {
198 struct numa_memblk *blk = &mi->blk[i];
199
200 if (blk->start == blk->end)
Tejun Heoef396ec2011-02-16 17:11:07 +0100201 continue;
Tejun Heo97e7b782011-02-16 17:11:08 +0100202 if (blk->end > start && blk->start < end)
203 return blk->nid;
204 if (blk->end == end && blk->start == start)
205 return blk->nid;
Tejun Heoef396ec2011-02-16 17:11:07 +0100206 }
207 return -1;
208}
209
210int __init numa_add_memblk(int nid, u64 start, u64 end)
211{
Tejun Heo97e7b782011-02-16 17:11:08 +0100212 struct numa_meminfo *mi = &numa_meminfo;
Tejun Heoef396ec2011-02-16 17:11:07 +0100213 int i;
214
215 i = conflicting_memblks(start, end);
216 if (i == nid) {
217 printk(KERN_WARNING "NUMA: Warning: node %d (%Lx-%Lx) overlaps with itself (%Lx-%Lx)\n",
218 nid, start, end, numa_nodes[i].start, numa_nodes[i].end);
219 } else if (i >= 0) {
220 printk(KERN_ERR "NUMA: node %d (%Lx-%Lx) overlaps with node %d (%Lx-%Lx)\n",
221 nid, start, end, i,
222 numa_nodes[i].start, numa_nodes[i].end);
223 return -EINVAL;
224 }
225
Tejun Heo97e7b782011-02-16 17:11:08 +0100226 mi->blk[mi->nr_blks].start = start;
227 mi->blk[mi->nr_blks].end = end;
228 mi->blk[mi->nr_blks].nid = nid;
229 mi->nr_blks++;
Tejun Heoef396ec2011-02-16 17:11:07 +0100230 return 0;
231}
232
Tejun Heo2e756be2011-02-16 17:11:09 +0100233static void __init numa_remove_memblk_from(int idx, struct numa_meminfo *mi)
234{
235 mi->nr_blks--;
236 memmove(&mi->blk[idx], &mi->blk[idx + 1],
237 (mi->nr_blks - idx) * sizeof(mi->blk[0]));
238}
239
Tejun Heo206e4202011-02-16 12:13:07 +0100240static __init void cutoff_node(int i, unsigned long start, unsigned long end)
241{
242 struct bootnode *nd = &numa_nodes[i];
243
244 if (nd->start < start) {
245 nd->start = start;
246 if (nd->end < nd->start)
247 nd->start = nd->end;
248 }
249 if (nd->end > end) {
250 nd->end = end;
251 if (nd->start > nd->end)
252 nd->start = nd->end;
253 }
254}
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256/* Initialize bootmem allocator for a node */
Yinghai Lu7c437692009-05-15 13:59:37 -0700257void __init
258setup_node_bootmem(int nodeid, unsigned long start, unsigned long end)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100259{
Yinghai Lu08677212010-02-10 01:20:20 -0800260 unsigned long start_pfn, last_pfn, nodedata_phys;
Yinghai Lu7c437692009-05-15 13:59:37 -0700261 const int pgdat_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
Yinghai Lu1a27fc02008-03-18 12:52:37 -0700262 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Yinghai Lu4c31e922009-04-22 14:19:27 -0700264 if (!end)
265 return;
266
Yinghai Lu7c437692009-05-15 13:59:37 -0700267 /*
268 * Don't confuse VM with a node that doesn't have the
269 * minimum amount of memory:
270 */
271 if (end && (end - start) < NODE_MIN_SIZE)
272 return;
273
Joerg Roedelbe3e89e2008-07-25 16:48:58 +0200274 start = roundup(start, ZONE_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Yinghai Lu08677212010-02-10 01:20:20 -0800276 printk(KERN_INFO "Initmem setup node %d %016lx-%016lx\n", nodeid,
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100277 start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 start_pfn = start >> PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200280 last_pfn = end >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Yinghai Lu24a5da72008-02-01 17:49:41 +0100282 node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
283 SMP_CACHE_BYTES);
Andi Kleena8062232006-04-07 19:49:21 +0200284 if (node_data[nodeid] == NULL)
285 return;
286 nodedata_phys = __pa(node_data[nodeid]);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700287 memblock_x86_reserve_range(nodedata_phys, nodedata_phys + pgdat_size, "NODE_DATA");
Yinghai Lu6118f762008-02-04 16:47:56 +0100288 printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
289 nodedata_phys + pgdat_size - 1);
Yinghai Lu1842f902010-02-10 01:20:15 -0800290 nid = phys_to_nid(nodedata_phys);
291 if (nid != nodeid)
292 printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nodeid, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
Yinghai Lu08677212010-02-10 01:20:20 -0800295 NODE_DATA(nodeid)->node_id = nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 NODE_DATA(nodeid)->node_start_pfn = start_pfn;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200297 NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 node_set_online(nodeid);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100300}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Tejun Heof9c60252011-02-16 17:11:09 +0100302static int __init numa_cleanup_meminfo(struct numa_meminfo *mi)
Tejun Heofd0435d2011-02-16 17:11:08 +0100303{
Tejun Heo2e756be2011-02-16 17:11:09 +0100304 int i, j, k;
Tejun Heoef396ec2011-02-16 17:11:07 +0100305
Tejun Heo2e756be2011-02-16 17:11:09 +0100306 for (i = 0; i < mi->nr_blks; i++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100307 struct numa_memblk *bi = &mi->blk[i];
Tejun Heoef396ec2011-02-16 17:11:07 +0100308
Tejun Heo2e756be2011-02-16 17:11:09 +0100309 for (j = i + 1; j < mi->nr_blks; j++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100310 struct numa_memblk *bj = &mi->blk[j];
Tejun Heoef396ec2011-02-16 17:11:07 +0100311 unsigned long start, end;
312
Tejun Heo2e756be2011-02-16 17:11:09 +0100313 /*
314 * Join together blocks on the same node, holes
315 * between which don't overlap with memory on other
316 * nodes.
317 */
Tejun Heo97e7b782011-02-16 17:11:08 +0100318 if (bi->nid != bj->nid)
Tejun Heoef396ec2011-02-16 17:11:07 +0100319 continue;
Tejun Heo2e756be2011-02-16 17:11:09 +0100320 start = min(bi->start, bj->start);
321 end = max(bi->end, bj->end);
322 for (k = 0; k < mi->nr_blks; k++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100323 struct numa_memblk *bk = &mi->blk[k];
324
325 if (bi->nid == bk->nid)
Tejun Heoef396ec2011-02-16 17:11:07 +0100326 continue;
Tejun Heo97e7b782011-02-16 17:11:08 +0100327 if (start < bk->end && end > bk->start)
Tejun Heoef396ec2011-02-16 17:11:07 +0100328 break;
329 }
Tejun Heo97e7b782011-02-16 17:11:08 +0100330 if (k < mi->nr_blks)
Tejun Heoef396ec2011-02-16 17:11:07 +0100331 continue;
Tejun Heoef396ec2011-02-16 17:11:07 +0100332 printk(KERN_INFO "NUMA: Node %d [%Lx,%Lx) + [%Lx,%Lx) -> [%lx,%lx)\n",
Tejun Heo97e7b782011-02-16 17:11:08 +0100333 bi->nid, bi->start, bi->end, bj->start, bj->end,
Tejun Heoef396ec2011-02-16 17:11:07 +0100334 start, end);
Tejun Heo97e7b782011-02-16 17:11:08 +0100335 bi->start = start;
336 bi->end = end;
Tejun Heo2e756be2011-02-16 17:11:09 +0100337 numa_remove_memblk_from(j--, mi);
Tejun Heoef396ec2011-02-16 17:11:07 +0100338 }
339 }
340
Tejun Heof9c60252011-02-16 17:11:09 +0100341 return 0;
342}
343
344/*
345 * Sanity check to catch more bad NUMA configurations (they are amazingly
346 * common). Make sure the nodes cover all memory.
347 */
348static int __init nodes_cover_memory(const struct bootnode *nodes)
349{
350 unsigned long numaram, e820ram;
351 int i;
352
353 numaram = 0;
354 for_each_node_mask(i, mem_nodes_parsed) {
355 unsigned long s = nodes[i].start >> PAGE_SHIFT;
356 unsigned long e = nodes[i].end >> PAGE_SHIFT;
357 numaram += e - s;
358 numaram -= __absent_pages_in_range(i, s, e);
359 if ((long)numaram < 0)
360 numaram = 0;
361 }
362
363 e820ram = max_pfn - (memblock_x86_hole_size(0,
364 max_pfn << PAGE_SHIFT) >> PAGE_SHIFT);
365 /* We seem to lose 3 pages somewhere. Allow 1M of slack. */
366 if ((long)(e820ram - numaram) >= (1 << (20 - PAGE_SHIFT))) {
367 printk(KERN_ERR "NUMA: nodes only cover %luMB of your %luMB e820 RAM. Not used.\n",
368 (numaram << PAGE_SHIFT) >> 20,
369 (e820ram << PAGE_SHIFT) >> 20);
370 return 0;
371 }
372 return 1;
373}
374
375static int __init numa_register_memblks(struct numa_meminfo *mi)
376{
377 int i;
378
379 /* Account for nodes with cpus and no memory */
380 nodes_or(node_possible_map, mem_nodes_parsed, cpu_nodes_parsed);
381 if (WARN_ON(nodes_empty(node_possible_map)))
382 return -EINVAL;
383
Tejun Heo97e7b782011-02-16 17:11:08 +0100384 memnode_shift = compute_hash_shift(mi);
Tejun Heoef396ec2011-02-16 17:11:07 +0100385 if (memnode_shift < 0) {
386 printk(KERN_ERR "NUMA: No NUMA node hash function found. Contact maintainer\n");
387 return -EINVAL;
388 }
389
Tejun Heo97e7b782011-02-16 17:11:08 +0100390 for (i = 0; i < mi->nr_blks; i++)
391 memblock_x86_register_active_regions(mi->blk[i].nid,
392 mi->blk[i].start >> PAGE_SHIFT,
393 mi->blk[i].end >> PAGE_SHIFT);
Tejun Heofd0435d2011-02-16 17:11:08 +0100394
395 /* for out of order entries */
396 sort_node_map();
397 if (!nodes_cover_memory(numa_nodes))
398 return -EINVAL;
399
400 init_memory_mapping_high();
401
402 /* Finally register nodes. */
403 for_each_node_mask(i, node_possible_map)
404 setup_node_bootmem(i, numa_nodes[i].start, numa_nodes[i].end);
405
406 /*
407 * Try again in case setup_node_bootmem missed one due to missing
408 * bootmem.
409 */
410 for_each_node_mask(i, node_possible_map)
411 if (!node_online(i))
412 setup_node_bootmem(i, numa_nodes[i].start,
413 numa_nodes[i].end);
414
Tejun Heoef396ec2011-02-16 17:11:07 +0100415 return 0;
416}
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418#ifdef CONFIG_NUMA_EMU
Rohit Seth53fee042007-02-13 13:26:22 +0100419/* Numa emulation */
David Rientjesadc19382009-09-25 15:20:09 -0700420static struct bootnode nodes[MAX_NUMNODES] __initdata;
David Rientjesc1c34432010-12-22 17:23:54 -0800421static struct bootnode physnodes[MAX_NUMNODES] __cpuinitdata;
Thomas Gleixner864fc312008-05-12 15:43:36 +0200422static char *cmdline __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Jan Beulich90321602011-01-19 08:57:21 +0000424void __init numa_emu_cmdline(char *str)
425{
426 cmdline = str;
427}
428
Tejun Heo19095542011-02-16 12:13:07 +0100429static int __init setup_physnodes(unsigned long start, unsigned long end)
David Rientjesadc19382009-09-25 15:20:09 -0700430{
David Rientjesadc19382009-09-25 15:20:09 -0700431 int ret = 0;
432 int i;
433
David Rientjesc1c34432010-12-22 17:23:54 -0800434 memset(physnodes, 0, sizeof(physnodes));
Tejun Heo19095542011-02-16 12:13:07 +0100435
436 for_each_node_mask(i, mem_nodes_parsed) {
437 physnodes[i].start = numa_nodes[i].start;
438 physnodes[i].end = numa_nodes[i].end;
439 }
440
David Rientjesadc19382009-09-25 15:20:09 -0700441 /*
442 * Basic sanity checking on the physical node map: there may be errors
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200443 * if the SRAT or AMD code incorrectly reported the topology or the mem=
David Rientjesadc19382009-09-25 15:20:09 -0700444 * kernel parameter is used.
445 */
David Rientjesa387e952010-12-22 17:23:56 -0800446 for (i = 0; i < MAX_NUMNODES; i++) {
David Rientjesadc19382009-09-25 15:20:09 -0700447 if (physnodes[i].start == physnodes[i].end)
448 continue;
449 if (physnodes[i].start > end) {
450 physnodes[i].end = physnodes[i].start;
451 continue;
452 }
453 if (physnodes[i].end < start) {
454 physnodes[i].start = physnodes[i].end;
455 continue;
456 }
457 if (physnodes[i].start < start)
458 physnodes[i].start = start;
459 if (physnodes[i].end > end)
460 physnodes[i].end = end;
David Rientjesadc19382009-09-25 15:20:09 -0700461 ret++;
462 }
463
464 /*
465 * If no physical topology was detected, a single node is faked to cover
466 * the entire address space.
467 */
468 if (!ret) {
469 physnodes[ret].start = start;
470 physnodes[ret].end = end;
471 ret = 1;
472 }
473 return ret;
474}
475
David Rientjesf51bf302010-12-22 17:23:51 -0800476static void __init fake_physnodes(int acpi, int amd, int nr_nodes)
477{
478 int i;
479
480 BUG_ON(acpi && amd);
481#ifdef CONFIG_ACPI_NUMA
482 if (acpi)
483 acpi_fake_nodes(nodes, nr_nodes);
484#endif
485#ifdef CONFIG_AMD_NUMA
486 if (amd)
487 amd_fake_nodes(nodes, nr_nodes);
488#endif
489 if (!acpi && !amd)
490 for (i = 0; i < nr_cpu_ids; i++)
491 numa_set_node(i, 0);
492}
493
Rohit Seth53fee042007-02-13 13:26:22 +0100494/*
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100495 * Setups up nid to range from addr to addr + size. If the end
496 * boundary is greater than max_addr, then max_addr is used instead.
497 * The return value is 0 if there is additional memory left for
498 * allocation past addr and -1 otherwise. addr is adjusted to be at
499 * the end of the node.
Rohit Seth53fee042007-02-13 13:26:22 +0100500 */
David Rientjesadc19382009-09-25 15:20:09 -0700501static int __init setup_node_range(int nid, u64 *addr, u64 size, u64 max_addr)
Rohit Seth53fee042007-02-13 13:26:22 +0100502{
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200503 int ret = 0;
504 nodes[nid].start = *addr;
505 *addr += size;
506 if (*addr >= max_addr) {
507 *addr = max_addr;
508 ret = -1;
509 }
510 nodes[nid].end = *addr;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200511 node_set(nid, node_possible_map);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200512 printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
513 nodes[nid].start, nodes[nid].end,
514 (nodes[nid].end - nodes[nid].start) >> 20);
515 return ret;
Rohit Seth53fee042007-02-13 13:26:22 +0100516}
517
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200518/*
David Rientjesadc19382009-09-25 15:20:09 -0700519 * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
520 * to max_addr. The return value is the number of nodes allocated.
521 */
David Rientjesc1c34432010-12-22 17:23:54 -0800522static int __init split_nodes_interleave(u64 addr, u64 max_addr, int nr_nodes)
David Rientjesadc19382009-09-25 15:20:09 -0700523{
524 nodemask_t physnode_mask = NODE_MASK_NONE;
525 u64 size;
526 int big;
527 int ret = 0;
528 int i;
529
530 if (nr_nodes <= 0)
531 return -1;
532 if (nr_nodes > MAX_NUMNODES) {
533 pr_info("numa=fake=%d too large, reducing to %d\n",
534 nr_nodes, MAX_NUMNODES);
535 nr_nodes = MAX_NUMNODES;
536 }
537
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700538 size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) / nr_nodes;
David Rientjesadc19382009-09-25 15:20:09 -0700539 /*
540 * Calculate the number of big nodes that can be allocated as a result
541 * of consolidating the remainder.
542 */
David Rientjes68fd1112010-02-15 13:43:25 -0800543 big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
David Rientjesadc19382009-09-25 15:20:09 -0700544 FAKE_NODE_MIN_SIZE;
545
546 size &= FAKE_NODE_MIN_HASH_MASK;
547 if (!size) {
548 pr_err("Not enough memory for each node. "
549 "NUMA emulation disabled.\n");
550 return -1;
551 }
552
David Rientjesc1c34432010-12-22 17:23:54 -0800553 for (i = 0; i < MAX_NUMNODES; i++)
David Rientjesadc19382009-09-25 15:20:09 -0700554 if (physnodes[i].start != physnodes[i].end)
555 node_set(i, physnode_mask);
556
557 /*
558 * Continue to fill physical nodes with fake nodes until there is no
559 * memory left on any of them.
560 */
561 while (nodes_weight(physnode_mask)) {
562 for_each_node_mask(i, physnode_mask) {
563 u64 end = physnodes[i].start + size;
564 u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
565
566 if (ret < big)
567 end += FAKE_NODE_MIN_SIZE;
568
569 /*
570 * Continue to add memory to this fake node if its
571 * non-reserved memory is less than the per-node size.
572 */
573 while (end - physnodes[i].start -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700574 memblock_x86_hole_size(physnodes[i].start, end) < size) {
David Rientjesadc19382009-09-25 15:20:09 -0700575 end += FAKE_NODE_MIN_SIZE;
576 if (end > physnodes[i].end) {
577 end = physnodes[i].end;
578 break;
579 }
580 }
581
582 /*
583 * If there won't be at least FAKE_NODE_MIN_SIZE of
584 * non-reserved memory in ZONE_DMA32 for the next node,
585 * this one must extend to the boundary.
586 */
587 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700588 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjesadc19382009-09-25 15:20:09 -0700589 end = dma32_end;
590
591 /*
592 * If there won't be enough non-reserved memory for the
593 * next node, this one must extend to the end of the
594 * physical node.
595 */
596 if (physnodes[i].end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700597 memblock_x86_hole_size(end, physnodes[i].end) < size)
David Rientjesadc19382009-09-25 15:20:09 -0700598 end = physnodes[i].end;
599
600 /*
601 * Avoid allocating more nodes than requested, which can
602 * happen as a result of rounding down each node's size
603 * to FAKE_NODE_MIN_SIZE.
604 */
605 if (nodes_weight(physnode_mask) + ret >= nr_nodes)
606 end = physnodes[i].end;
607
608 if (setup_node_range(ret++, &physnodes[i].start,
609 end - physnodes[i].start,
610 physnodes[i].end) < 0)
611 node_clear(i, physnode_mask);
612 }
613 }
614 return ret;
615}
616
617/*
David Rientjes8df5bb32010-02-15 13:43:30 -0800618 * Returns the end address of a node so that there is at least `size' amount of
619 * non-reserved memory or `max_addr' is reached.
620 */
621static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
622{
623 u64 end = start + size;
624
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700625 while (end - start - memblock_x86_hole_size(start, end) < size) {
David Rientjes8df5bb32010-02-15 13:43:30 -0800626 end += FAKE_NODE_MIN_SIZE;
627 if (end > max_addr) {
628 end = max_addr;
629 break;
630 }
631 }
632 return end;
633}
634
635/*
636 * Sets up fake nodes of `size' interleaved over physical nodes ranging from
637 * `addr' to `max_addr'. The return value is the number of nodes allocated.
638 */
639static int __init split_nodes_size_interleave(u64 addr, u64 max_addr, u64 size)
640{
641 nodemask_t physnode_mask = NODE_MASK_NONE;
642 u64 min_size;
643 int ret = 0;
644 int i;
645
646 if (!size)
647 return -1;
648 /*
649 * The limit on emulated nodes is MAX_NUMNODES, so the size per node is
650 * increased accordingly if the requested size is too small. This
651 * creates a uniform distribution of node sizes across the entire
652 * machine (but not necessarily over physical nodes).
653 */
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700654 min_size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) /
David Rientjes8df5bb32010-02-15 13:43:30 -0800655 MAX_NUMNODES;
656 min_size = max(min_size, FAKE_NODE_MIN_SIZE);
657 if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size)
658 min_size = (min_size + FAKE_NODE_MIN_SIZE) &
659 FAKE_NODE_MIN_HASH_MASK;
660 if (size < min_size) {
661 pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
662 size >> 20, min_size >> 20);
663 size = min_size;
664 }
665 size &= FAKE_NODE_MIN_HASH_MASK;
666
667 for (i = 0; i < MAX_NUMNODES; i++)
668 if (physnodes[i].start != physnodes[i].end)
669 node_set(i, physnode_mask);
670 /*
671 * Fill physical nodes with fake nodes of size until there is no memory
672 * left on any of them.
673 */
674 while (nodes_weight(physnode_mask)) {
675 for_each_node_mask(i, physnode_mask) {
676 u64 dma32_end = MAX_DMA32_PFN << PAGE_SHIFT;
677 u64 end;
678
679 end = find_end_of_node(physnodes[i].start,
680 physnodes[i].end, size);
681 /*
682 * If there won't be at least FAKE_NODE_MIN_SIZE of
683 * non-reserved memory in ZONE_DMA32 for the next node,
684 * this one must extend to the boundary.
685 */
686 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700687 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjes8df5bb32010-02-15 13:43:30 -0800688 end = dma32_end;
689
690 /*
691 * If there won't be enough non-reserved memory for the
692 * next node, this one must extend to the end of the
693 * physical node.
694 */
695 if (physnodes[i].end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700696 memblock_x86_hole_size(end, physnodes[i].end) < size)
David Rientjes8df5bb32010-02-15 13:43:30 -0800697 end = physnodes[i].end;
698
699 /*
700 * Setup the fake node that will be allocated as bootmem
701 * later. If setup_node_range() returns non-zero, there
702 * is no more memory available on this physical node.
703 */
704 if (setup_node_range(ret++, &physnodes[i].start,
705 end - physnodes[i].start,
706 physnodes[i].end) < 0)
707 node_clear(i, physnode_mask);
708 }
709 }
710 return ret;
711}
712
713/*
Thomas Gleixner886533a2008-05-12 15:43:36 +0200714 * Sets up the system RAM area from start_pfn to last_pfn according to the
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200715 * numa=fake command-line option.
716 */
David Rientjesadc19382009-09-25 15:20:09 -0700717static int __init numa_emulation(unsigned long start_pfn,
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200718 unsigned long last_pfn, int acpi, int amd)
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200719{
Tejun Heo97e7b782011-02-16 17:11:08 +0100720 static struct numa_meminfo ei __initdata;
David Rientjesca2107c2010-02-15 13:43:33 -0800721 u64 addr = start_pfn << PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200722 u64 max_addr = last_pfn << PAGE_SHIFT;
David Rientjesca2107c2010-02-15 13:43:33 -0800723 int num_nodes;
724 int i;
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200725
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200726 /*
David Rientjes8df5bb32010-02-15 13:43:30 -0800727 * If the numa=fake command-line contains a 'M' or 'G', it represents
David Rientjesca2107c2010-02-15 13:43:33 -0800728 * the fixed node size. Otherwise, if it is just a single number N,
729 * split the system RAM into N fake nodes.
David Rientjes8df5bb32010-02-15 13:43:30 -0800730 */
731 if (strchr(cmdline, 'M') || strchr(cmdline, 'G')) {
David Rientjesca2107c2010-02-15 13:43:33 -0800732 u64 size;
733
David Rientjes8df5bb32010-02-15 13:43:30 -0800734 size = memparse(cmdline, &cmdline);
735 num_nodes = split_nodes_size_interleave(addr, max_addr, size);
David Rientjesca2107c2010-02-15 13:43:33 -0800736 } else {
737 unsigned long n;
738
739 n = simple_strtoul(cmdline, NULL, 0);
David Rientjesc1c34432010-12-22 17:23:54 -0800740 num_nodes = split_nodes_interleave(addr, max_addr, n);
David Rientjes8df5bb32010-02-15 13:43:30 -0800741 }
742
David Rientjesca2107c2010-02-15 13:43:33 -0800743 if (num_nodes < 0)
744 return num_nodes;
Tejun Heo8968dab2011-02-16 17:11:08 +0100745
Tejun Heo97e7b782011-02-16 17:11:08 +0100746 ei.nr_blks = num_nodes;
747 for (i = 0; i < ei.nr_blks; i++) {
748 ei.blk[i].start = nodes[i].start;
749 ei.blk[i].end = nodes[i].end;
750 ei.blk[i].nid = i;
751 }
Tejun Heo8968dab2011-02-16 17:11:08 +0100752
Tejun Heo97e7b782011-02-16 17:11:08 +0100753 memnode_shift = compute_hash_shift(&ei);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200754 if (memnode_shift < 0) {
755 memnode_shift = 0;
756 printk(KERN_ERR "No NUMA hash function found. NUMA emulation "
757 "disabled.\n");
758 return -1;
759 }
760
761 /*
David Rientjesadc19382009-09-25 15:20:09 -0700762 * We need to vacate all active ranges that may have been registered for
763 * the e820 memory map.
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200764 */
765 remove_all_active_ranges();
Yinghai Lu1411e0e2010-12-27 16:48:17 -0800766 for_each_node_mask(i, node_possible_map)
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700767 memblock_x86_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
Mel Gorman5cb248a2006-09-27 01:49:52 -0700768 nodes[i].end >> PAGE_SHIFT);
Yinghai Lu1411e0e2010-12-27 16:48:17 -0800769 init_memory_mapping_high();
770 for_each_node_mask(i, node_possible_map)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100771 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
Tejun Heo19095542011-02-16 12:13:07 +0100772 setup_physnodes(addr, max_addr);
David Rientjesf51bf302010-12-22 17:23:51 -0800773 fake_physnodes(acpi, amd, num_nodes);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100774 numa_init_array();
775 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200777#endif /* CONFIG_NUMA_EMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Tejun Heoffe77a42011-02-16 12:13:06 +0100779static int dummy_numa_init(void)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100780{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 printk(KERN_INFO "%s\n",
782 numa_off ? "NUMA turned off" : "No NUMA configuration found");
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100783 printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
Tejun Heo86ef4db2011-02-16 12:13:06 +0100784 0LU, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100785
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100786 node_set(0, cpu_nodes_parsed);
787 node_set(0, mem_nodes_parsed);
Tejun Heo43a662f2011-02-16 17:11:08 +0100788 numa_add_memblk(0, 0, (u64)max_pfn << PAGE_SHIFT);
Tejun Heofd0435d2011-02-16 17:11:08 +0100789 numa_nodes[0].start = 0;
790 numa_nodes[0].end = (u64)max_pfn << PAGE_SHIFT;
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100791
792 return 0;
793}
794
Tejun Heoffe77a42011-02-16 12:13:06 +0100795void __init initmem_init(void)
796{
797 int (*numa_init[])(void) = { [2] = dummy_numa_init };
Tejun Heoffe77a42011-02-16 12:13:06 +0100798 int i, j;
799
800 if (!numa_off) {
801#ifdef CONFIG_ACPI_NUMA
802 numa_init[0] = x86_acpi_numa_init;
Tejun Heoffe77a42011-02-16 12:13:06 +0100803#endif
804#ifdef CONFIG_AMD_NUMA
805 numa_init[1] = amd_numa_init;
Tejun Heoffe77a42011-02-16 12:13:06 +0100806#endif
807 }
808
809 for (i = 0; i < ARRAY_SIZE(numa_init); i++) {
810 if (!numa_init[i])
811 continue;
812
813 for (j = 0; j < MAX_LOCAL_APIC; j++)
814 set_apicid_to_node(j, NUMA_NO_NODE);
815
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100816 nodes_clear(cpu_nodes_parsed);
817 nodes_clear(mem_nodes_parsed);
Tejun Heoffe77a42011-02-16 12:13:06 +0100818 nodes_clear(node_possible_map);
819 nodes_clear(node_online_map);
Tejun Heo97e7b782011-02-16 17:11:08 +0100820 memset(&numa_meminfo, 0, sizeof(numa_meminfo));
Tejun Heo206e4202011-02-16 12:13:07 +0100821 memset(numa_nodes, 0, sizeof(numa_nodes));
Tejun Heofd0435d2011-02-16 17:11:08 +0100822 remove_all_active_ranges();
Tejun Heoffe77a42011-02-16 12:13:06 +0100823
824 if (numa_init[i]() < 0)
825 continue;
Tejun Heo206e4202011-02-16 12:13:07 +0100826
827 /* clean up the node list */
828 for (j = 0; j < MAX_NUMNODES; j++)
829 cutoff_node(j, 0, max_pfn << PAGE_SHIFT);
830
Tejun Heoffe77a42011-02-16 12:13:06 +0100831#ifdef CONFIG_NUMA_EMU
Tejun Heo19095542011-02-16 12:13:07 +0100832 setup_physnodes(0, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100833 if (cmdline && !numa_emulation(0, max_pfn, i == 0, i == 1))
834 return;
Tejun Heo19095542011-02-16 12:13:07 +0100835 setup_physnodes(0, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100836 nodes_clear(node_possible_map);
837 nodes_clear(node_online_map);
838#endif
Tejun Heof9c60252011-02-16 17:11:09 +0100839 if (numa_cleanup_meminfo(&numa_meminfo) < 0)
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100840 continue;
841
Tejun Heof9c60252011-02-16 17:11:09 +0100842 if (numa_register_memblks(&numa_meminfo) < 0)
Tejun Heo43a662f2011-02-16 17:11:08 +0100843 continue;
844
Tejun Heofd0435d2011-02-16 17:11:08 +0100845 for (j = 0; j < nr_cpu_ids; j++) {
846 int nid = early_cpu_to_node(j);
847
848 if (nid == NUMA_NO_NODE)
849 continue;
850 if (!node_online(nid))
851 numa_clear_node(j);
852 }
853 numa_init_array();
854 return;
Tejun Heoffe77a42011-02-16 12:13:06 +0100855 }
856 BUG();
Andi Kleen69d81fc2005-11-05 17:25:53 +0100857}
858
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100859unsigned long __init numa_free_all_bootmem(void)
860{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 unsigned long pages = 0;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100862 int i;
863
864 for_each_online_node(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 pages += free_all_bootmem_node(NODE_DATA(i));
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100866
Yinghai Lu08677212010-02-10 01:20:20 -0800867 pages += free_all_memory_core_early(MAX_NUMNODES);
Yinghai Lu08677212010-02-10 01:20:20 -0800868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return pages;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100870}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100872int __cpuinit numa_cpu_node(int cpu)
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800873{
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100874 int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800875
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100876 if (apicid != BAD_APICID)
877 return __apicid_to_node[apicid];
878 return NUMA_NO_NODE;
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800879}
880
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100881/*
Tejun Heode2d9442011-01-23 14:37:41 +0100882 * UGLINESS AHEAD: Currently, CONFIG_NUMA_EMU is 64bit only and makes use
883 * of 64bit specific data structures. The distinction is artificial and
884 * should be removed. numa_{add|remove}_cpu() are implemented in numa.c
885 * for both 32 and 64bit when CONFIG_NUMA_EMU is disabled but here when
886 * enabled.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100887 *
Tejun Heode2d9442011-01-23 14:37:41 +0100888 * NUMA emulation is planned to be made generic and the following and other
889 * related code should be moved to numa.c.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100890 */
Tejun Heode2d9442011-01-23 14:37:41 +0100891#ifdef CONFIG_NUMA_EMU
892# ifndef CONFIG_DEBUG_PER_CPU_MAPS
David Rientjesc1c34432010-12-22 17:23:54 -0800893void __cpuinit numa_add_cpu(int cpu)
894{
895 unsigned long addr;
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100896 int physnid, nid;
David Rientjesc1c34432010-12-22 17:23:54 -0800897
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100898 nid = numa_cpu_node(cpu);
David Rientjesc1c34432010-12-22 17:23:54 -0800899 if (nid == NUMA_NO_NODE)
900 nid = early_cpu_to_node(cpu);
901 BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
902
903 /*
904 * Use the starting address of the emulated node to find which physical
905 * node it is allocated on.
906 */
907 addr = node_start_pfn(nid) << PAGE_SHIFT;
908 for (physnid = 0; physnid < MAX_NUMNODES; physnid++)
909 if (addr >= physnodes[physnid].start &&
910 addr < physnodes[physnid].end)
911 break;
912
913 /*
914 * Map the cpu to each emulated node that is allocated on the physical
915 * node of the cpu's apic id.
916 */
917 for_each_online_node(nid) {
918 addr = node_start_pfn(nid) << PAGE_SHIFT;
919 if (addr >= physnodes[physnid].start &&
920 addr < physnodes[physnid].end)
921 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
922 }
923}
924
925void __cpuinit numa_remove_cpu(int cpu)
926{
927 int i;
928
929 for_each_online_node(i)
930 cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
931}
Tejun Heode2d9442011-01-23 14:37:41 +0100932# else /* !CONFIG_DEBUG_PER_CPU_MAPS */
David Rientjesd906f0e2010-12-30 10:54:16 -0800933static void __cpuinit numa_set_cpumask(int cpu, int enable)
934{
935 int node = early_cpu_to_node(cpu);
936 struct cpumask *mask;
David Rientjesc1c34432010-12-22 17:23:54 -0800937 int i;
Brian Gerst6470aff2009-01-27 12:56:47 +0900938
David Rientjes14392fd2011-02-07 14:08:53 -0800939 if (node == NUMA_NO_NODE) {
940 /* early_cpu_to_node() already emits a warning and trace */
941 return;
942 }
David Rientjesc1c34432010-12-22 17:23:54 -0800943 for_each_online_node(i) {
944 unsigned long addr;
945
946 addr = node_start_pfn(i) << PAGE_SHIFT;
947 if (addr < physnodes[node].start ||
948 addr >= physnodes[node].end)
949 continue;
David Rientjesd906f0e2010-12-30 10:54:16 -0800950 mask = debug_cpumask_set_cpu(cpu, enable);
951 if (!mask)
David Rientjesc1c34432010-12-22 17:23:54 -0800952 return;
David Rientjesc1c34432010-12-22 17:23:54 -0800953
954 if (enable)
955 cpumask_set_cpu(cpu, mask);
956 else
957 cpumask_clear_cpu(cpu, mask);
Brian Gerst6470aff2009-01-27 12:56:47 +0900958 }
Brian Gerst6470aff2009-01-27 12:56:47 +0900959}
960
961void __cpuinit numa_add_cpu(int cpu)
962{
963 numa_set_cpumask(cpu, 1);
964}
965
966void __cpuinit numa_remove_cpu(int cpu)
967{
968 numa_set_cpumask(cpu, 0);
969}
Tejun Heode2d9442011-01-23 14:37:41 +0100970# endif /* !CONFIG_DEBUG_PER_CPU_MAPS */
971#endif /* CONFIG_NUMA_EMU */