blob: c490448d716a79851f2161c25fb09e843a5907b0 [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
Brian Gerst6470aff2009-01-27 12:56:47 +090049/*
Eric Dumazet529a3402005-11-05 17:25:54 +010050 * Given a shift value, try to populate memnodemap[]
51 * Returns :
52 * 1 if OK
53 * 0 if memnodmap[] too small (of shift too small)
54 * -1 if node overlap or lost ram (shift too big)
55 */
Tejun Heo97e7b782011-02-16 17:11:08 +010056static int __init populate_memnodemap(const struct numa_meminfo *mi, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Eric Dumazet529a3402005-11-05 17:25:54 +010058 unsigned long addr, end;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010059 int i, res = -1;
Keith Manntheyb6846642005-07-28 21:15:38 -070060
travis@sgi.com43238382008-01-30 13:33:25 +010061 memset(memnodemap, 0xff, sizeof(s16)*memnodemapsize);
Tejun Heo97e7b782011-02-16 17:11:08 +010062 for (i = 0; i < mi->nr_blks; i++) {
63 addr = mi->blk[i].start;
64 end = mi->blk[i].end;
Eric Dumazet529a3402005-11-05 17:25:54 +010065 if (addr >= end)
66 continue;
Amul Shah076422d2007-02-13 13:26:19 +010067 if ((end >> shift) >= memnodemapsize)
Eric Dumazet529a3402005-11-05 17:25:54 +010068 return 0;
69 do {
travis@sgi.com43238382008-01-30 13:33:25 +010070 if (memnodemap[addr >> shift] != NUMA_NO_NODE)
Eric Dumazet529a3402005-11-05 17:25:54 +010071 return -1;
Tejun Heo97e7b782011-02-16 17:11:08 +010072 memnodemap[addr >> shift] = mi->blk[i].nid;
Amul Shah076422d2007-02-13 13:26:19 +010073 addr += (1UL << shift);
Eric Dumazet529a3402005-11-05 17:25:54 +010074 } while (addr < end);
75 res = 1;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010076 }
Eric Dumazet529a3402005-11-05 17:25:54 +010077 return res;
78}
79
Amul Shah076422d2007-02-13 13:26:19 +010080static int __init allocate_cachealigned_memnodemap(void)
81{
Yinghai Lu24a5da72008-02-01 17:49:41 +010082 unsigned long addr;
Amul Shah076422d2007-02-13 13:26:19 +010083
84 memnodemap = memnode.embedded_map;
travis@sgi.com316390b2008-01-30 13:33:15 +010085 if (memnodemapsize <= ARRAY_SIZE(memnode.embedded_map))
Amul Shah076422d2007-02-13 13:26:19 +010086 return 0;
Amul Shah076422d2007-02-13 13:26:19 +010087
Yinghai Lu24a5da72008-02-01 17:49:41 +010088 addr = 0x8000;
Joerg Roedelbe3e89e2008-07-25 16:48:58 +020089 nodemap_size = roundup(sizeof(s16) * memnodemapsize, L1_CACHE_BYTES);
Yinghai Ludbef7b52010-12-27 16:48:08 -080090 nodemap_addr = memblock_find_in_range(addr, get_max_mapped(),
Yinghai Lu24a5da72008-02-01 17:49:41 +010091 nodemap_size, L1_CACHE_BYTES);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070092 if (nodemap_addr == MEMBLOCK_ERROR) {
Amul Shah076422d2007-02-13 13:26:19 +010093 printk(KERN_ERR
94 "NUMA: Unable to allocate Memory to Node hash map\n");
95 nodemap_addr = nodemap_size = 0;
96 return -1;
97 }
Yinghai Lu24a5da72008-02-01 17:49:41 +010098 memnodemap = phys_to_virt(nodemap_addr);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070099 memblock_x86_reserve_range(nodemap_addr, nodemap_addr + nodemap_size, "MEMNODEMAP");
Amul Shah076422d2007-02-13 13:26:19 +0100100
101 printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n",
102 nodemap_addr, nodemap_addr + nodemap_size);
103 return 0;
104}
105
106/*
107 * The LSB of all start and end addresses in the node map is the value of the
108 * maximum possible shift.
109 */
Tejun Heo97e7b782011-02-16 17:11:08 +0100110static int __init extract_lsb_from_nodes(const struct numa_meminfo *mi)
Amul Shah076422d2007-02-13 13:26:19 +0100111{
Amul Shah54413922007-02-13 13:26:20 +0100112 int i, nodes_used = 0;
Amul Shah076422d2007-02-13 13:26:19 +0100113 unsigned long start, end;
114 unsigned long bitfield = 0, memtop = 0;
115
Tejun Heo97e7b782011-02-16 17:11:08 +0100116 for (i = 0; i < mi->nr_blks; i++) {
117 start = mi->blk[i].start;
118 end = mi->blk[i].end;
Amul Shah076422d2007-02-13 13:26:19 +0100119 if (start >= end)
120 continue;
Amul Shah54413922007-02-13 13:26:20 +0100121 bitfield |= start;
122 nodes_used++;
Amul Shah076422d2007-02-13 13:26:19 +0100123 if (end > memtop)
124 memtop = end;
125 }
Amul Shah54413922007-02-13 13:26:20 +0100126 if (nodes_used <= 1)
127 i = 63;
128 else
129 i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
Amul Shah076422d2007-02-13 13:26:19 +0100130 memnodemapsize = (memtop >> i)+1;
131 return i;
132}
133
Tejun Heo97e7b782011-02-16 17:11:08 +0100134static int __init compute_hash_shift(const struct numa_meminfo *mi)
Eric Dumazet529a3402005-11-05 17:25:54 +0100135{
Amul Shah076422d2007-02-13 13:26:19 +0100136 int shift;
Eric Dumazet529a3402005-11-05 17:25:54 +0100137
Tejun Heo97e7b782011-02-16 17:11:08 +0100138 shift = extract_lsb_from_nodes(mi);
Amul Shah076422d2007-02-13 13:26:19 +0100139 if (allocate_cachealigned_memnodemap())
140 return -1;
Andi Kleen6b050f82006-01-11 22:44:33 +0100141 printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
Eric Dumazet529a3402005-11-05 17:25:54 +0100142 shift);
143
Tejun Heo97e7b782011-02-16 17:11:08 +0100144 if (populate_memnodemap(mi, shift) != 1) {
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100145 printk(KERN_INFO "Your memory is not aligned you need to "
146 "rebuild your kernel with a bigger NODEMAPSIZE "
147 "shift=%d\n", shift);
Eric Dumazet529a3402005-11-05 17:25:54 +0100148 return -1;
149 }
Keith Manntheyb6846642005-07-28 21:15:38 -0700150 return shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -0800153int __meminit __early_pfn_to_nid(unsigned long pfn)
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700154{
155 return phys_to_nid(pfn << PAGE_SHIFT);
156}
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700157
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100158static void * __init early_node_mem(int nodeid, unsigned long start,
Yinghai Lu24a5da72008-02-01 17:49:41 +0100159 unsigned long end, unsigned long size,
160 unsigned long align)
Andi Kleena8062232006-04-07 19:49:21 +0200161{
Yinghai Lucef625e2010-02-10 01:20:18 -0800162 unsigned long mem;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100163
Yinghai Lucef625e2010-02-10 01:20:18 -0800164 /*
165 * put it on high as possible
166 * something will go with NODE_DATA
167 */
168 if (start < (MAX_DMA_PFN<<PAGE_SHIFT))
169 start = MAX_DMA_PFN<<PAGE_SHIFT;
170 if (start < (MAX_DMA32_PFN<<PAGE_SHIFT) &&
171 end > (MAX_DMA32_PFN<<PAGE_SHIFT))
172 start = MAX_DMA32_PFN<<PAGE_SHIFT;
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700173 mem = memblock_x86_find_in_range_node(nodeid, start, end, size, align);
174 if (mem != MEMBLOCK_ERROR)
Andi Kleena8062232006-04-07 19:49:21 +0200175 return __va(mem);
Yinghai Lu9347e0b2008-02-01 17:49:42 +0100176
Yinghai Lucef625e2010-02-10 01:20:18 -0800177 /* extend the search scope */
178 end = max_pfn_mapped << PAGE_SHIFT;
Yinghai Lu419db272010-10-28 09:50:17 -0700179 start = MAX_DMA_PFN << PAGE_SHIFT;
180 mem = memblock_find_in_range(start, end, size, align);
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700181 if (mem != MEMBLOCK_ERROR)
Yinghai Lu1842f902010-02-10 01:20:15 -0800182 return __va(mem);
183
184 printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100185 size, nodeid);
Yinghai Lu1842f902010-02-10 01:20:15 -0800186
187 return NULL;
Andi Kleena8062232006-04-07 19:49:21 +0200188}
189
Tejun Heoef396ec2011-02-16 17:11:07 +0100190int __init numa_add_memblk(int nid, u64 start, u64 end)
191{
Tejun Heo97e7b782011-02-16 17:11:08 +0100192 struct numa_meminfo *mi = &numa_meminfo;
Tejun Heoef396ec2011-02-16 17:11:07 +0100193
Tejun Heo56e827f2011-02-16 17:11:09 +0100194 /* ignore zero length blks */
195 if (start == end)
196 return 0;
197
198 /* whine about and ignore invalid blks */
199 if (start > end || nid < 0 || nid >= MAX_NUMNODES) {
200 pr_warning("NUMA: Warning: invalid memblk node %d (%Lx-%Lx)\n",
201 nid, start, end);
202 return 0;
203 }
204
205 if (mi->nr_blks >= NR_NODE_MEMBLKS) {
206 pr_err("NUMA: too many memblk ranges\n");
Tejun Heoef396ec2011-02-16 17:11:07 +0100207 return -EINVAL;
208 }
209
Tejun Heo97e7b782011-02-16 17:11:08 +0100210 mi->blk[mi->nr_blks].start = start;
211 mi->blk[mi->nr_blks].end = end;
212 mi->blk[mi->nr_blks].nid = nid;
213 mi->nr_blks++;
Tejun Heoef396ec2011-02-16 17:11:07 +0100214 return 0;
215}
216
Tejun Heo2e756be2011-02-16 17:11:09 +0100217static void __init numa_remove_memblk_from(int idx, struct numa_meminfo *mi)
218{
219 mi->nr_blks--;
220 memmove(&mi->blk[idx], &mi->blk[idx + 1],
221 (mi->nr_blks - idx) * sizeof(mi->blk[0]));
222}
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224/* Initialize bootmem allocator for a node */
Yinghai Lu7c437692009-05-15 13:59:37 -0700225void __init
226setup_node_bootmem(int nodeid, unsigned long start, unsigned long end)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100227{
Yinghai Lu08677212010-02-10 01:20:20 -0800228 unsigned long start_pfn, last_pfn, nodedata_phys;
Yinghai Lu7c437692009-05-15 13:59:37 -0700229 const int pgdat_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
Yinghai Lu1a27fc02008-03-18 12:52:37 -0700230 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Yinghai Lu4c31e922009-04-22 14:19:27 -0700232 if (!end)
233 return;
234
Yinghai Lu7c437692009-05-15 13:59:37 -0700235 /*
236 * Don't confuse VM with a node that doesn't have the
237 * minimum amount of memory:
238 */
239 if (end && (end - start) < NODE_MIN_SIZE)
240 return;
241
Joerg Roedelbe3e89e2008-07-25 16:48:58 +0200242 start = roundup(start, ZONE_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Yinghai Lu08677212010-02-10 01:20:20 -0800244 printk(KERN_INFO "Initmem setup node %d %016lx-%016lx\n", nodeid,
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100245 start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 start_pfn = start >> PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200248 last_pfn = end >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Yinghai Lu24a5da72008-02-01 17:49:41 +0100250 node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
251 SMP_CACHE_BYTES);
Andi Kleena8062232006-04-07 19:49:21 +0200252 if (node_data[nodeid] == NULL)
253 return;
254 nodedata_phys = __pa(node_data[nodeid]);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700255 memblock_x86_reserve_range(nodedata_phys, nodedata_phys + pgdat_size, "NODE_DATA");
Yinghai Lu6118f762008-02-04 16:47:56 +0100256 printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
257 nodedata_phys + pgdat_size - 1);
Yinghai Lu1842f902010-02-10 01:20:15 -0800258 nid = phys_to_nid(nodedata_phys);
259 if (nid != nodeid)
260 printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nodeid, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
Yinghai Lu08677212010-02-10 01:20:20 -0800263 NODE_DATA(nodeid)->node_id = nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 NODE_DATA(nodeid)->node_start_pfn = start_pfn;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200265 NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 node_set_online(nodeid);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100268}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Tejun Heof9c60252011-02-16 17:11:09 +0100270static int __init numa_cleanup_meminfo(struct numa_meminfo *mi)
Tejun Heofd0435d2011-02-16 17:11:08 +0100271{
Tejun Heo56e827f2011-02-16 17:11:09 +0100272 const u64 low = 0;
273 const u64 high = (u64)max_pfn << PAGE_SHIFT;
Tejun Heo2e756be2011-02-16 17:11:09 +0100274 int i, j, k;
Tejun Heoef396ec2011-02-16 17:11:07 +0100275
Tejun Heo2e756be2011-02-16 17:11:09 +0100276 for (i = 0; i < mi->nr_blks; i++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100277 struct numa_memblk *bi = &mi->blk[i];
Tejun Heoef396ec2011-02-16 17:11:07 +0100278
Tejun Heo56e827f2011-02-16 17:11:09 +0100279 /* make sure all blocks are inside the limits */
280 bi->start = max(bi->start, low);
281 bi->end = min(bi->end, high);
282
283 /* and there's no empty block */
284 if (bi->start == bi->end) {
285 numa_remove_memblk_from(i--, mi);
286 continue;
287 }
288
Tejun Heo2e756be2011-02-16 17:11:09 +0100289 for (j = i + 1; j < mi->nr_blks; j++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100290 struct numa_memblk *bj = &mi->blk[j];
Tejun Heoef396ec2011-02-16 17:11:07 +0100291 unsigned long start, end;
292
Tejun Heo2e756be2011-02-16 17:11:09 +0100293 /*
Tejun Heo56e827f2011-02-16 17:11:09 +0100294 * See whether there are overlapping blocks. Whine
295 * about but allow overlaps of the same nid. They
296 * will be merged below.
297 */
298 if (bi->end > bj->start && bi->start < bj->end) {
299 if (bi->nid != bj->nid) {
300 pr_err("NUMA: node %d (%Lx-%Lx) overlaps with node %d (%Lx-%Lx)\n",
301 bi->nid, bi->start, bi->end,
302 bj->nid, bj->start, bj->end);
303 return -EINVAL;
304 }
305 pr_warning("NUMA: Warning: node %d (%Lx-%Lx) overlaps with itself (%Lx-%Lx)\n",
306 bi->nid, bi->start, bi->end,
307 bj->start, bj->end);
308 }
309
310 /*
Tejun Heo2e756be2011-02-16 17:11:09 +0100311 * Join together blocks on the same node, holes
312 * between which don't overlap with memory on other
313 * nodes.
314 */
Tejun Heo97e7b782011-02-16 17:11:08 +0100315 if (bi->nid != bj->nid)
Tejun Heoef396ec2011-02-16 17:11:07 +0100316 continue;
Tejun Heo56e827f2011-02-16 17:11:09 +0100317 start = max(min(bi->start, bj->start), low);
318 end = min(max(bi->end, bj->end), high);
Tejun Heo2e756be2011-02-16 17:11:09 +0100319 for (k = 0; k < mi->nr_blks; k++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100320 struct numa_memblk *bk = &mi->blk[k];
321
322 if (bi->nid == bk->nid)
Tejun Heoef396ec2011-02-16 17:11:07 +0100323 continue;
Tejun Heo97e7b782011-02-16 17:11:08 +0100324 if (start < bk->end && end > bk->start)
Tejun Heoef396ec2011-02-16 17:11:07 +0100325 break;
326 }
Tejun Heo97e7b782011-02-16 17:11:08 +0100327 if (k < mi->nr_blks)
Tejun Heoef396ec2011-02-16 17:11:07 +0100328 continue;
Tejun Heoef396ec2011-02-16 17:11:07 +0100329 printk(KERN_INFO "NUMA: Node %d [%Lx,%Lx) + [%Lx,%Lx) -> [%lx,%lx)\n",
Tejun Heo97e7b782011-02-16 17:11:08 +0100330 bi->nid, bi->start, bi->end, bj->start, bj->end,
Tejun Heoef396ec2011-02-16 17:11:07 +0100331 start, end);
Tejun Heo97e7b782011-02-16 17:11:08 +0100332 bi->start = start;
333 bi->end = end;
Tejun Heo2e756be2011-02-16 17:11:09 +0100334 numa_remove_memblk_from(j--, mi);
Tejun Heoef396ec2011-02-16 17:11:07 +0100335 }
336 }
337
Tejun Heo56e827f2011-02-16 17:11:09 +0100338 for (i = mi->nr_blks; i < ARRAY_SIZE(mi->blk); i++) {
339 mi->blk[i].start = mi->blk[i].end = 0;
340 mi->blk[i].nid = NUMA_NO_NODE;
341 }
342
Tejun Heof9c60252011-02-16 17:11:09 +0100343 return 0;
344}
345
346/*
347 * Sanity check to catch more bad NUMA configurations (they are amazingly
348 * common). Make sure the nodes cover all memory.
349 */
Tejun Heo91556232011-02-16 17:11:09 +0100350static bool __init numa_meminfo_cover_memory(const struct numa_meminfo *mi)
Tejun Heof9c60252011-02-16 17:11:09 +0100351{
352 unsigned long numaram, e820ram;
353 int i;
354
355 numaram = 0;
Tejun Heo91556232011-02-16 17:11:09 +0100356 for (i = 0; i < mi->nr_blks; i++) {
357 unsigned long s = mi->blk[i].start >> PAGE_SHIFT;
358 unsigned long e = mi->blk[i].end >> PAGE_SHIFT;
Tejun Heof9c60252011-02-16 17:11:09 +0100359 numaram += e - s;
Tejun Heo91556232011-02-16 17:11:09 +0100360 numaram -= __absent_pages_in_range(mi->blk[i].nid, s, e);
Tejun Heof9c60252011-02-16 17:11:09 +0100361 if ((long)numaram < 0)
362 numaram = 0;
363 }
364
365 e820ram = max_pfn - (memblock_x86_hole_size(0,
366 max_pfn << PAGE_SHIFT) >> PAGE_SHIFT);
367 /* We seem to lose 3 pages somewhere. Allow 1M of slack. */
368 if ((long)(e820ram - numaram) >= (1 << (20 - PAGE_SHIFT))) {
369 printk(KERN_ERR "NUMA: nodes only cover %luMB of your %luMB e820 RAM. Not used.\n",
370 (numaram << PAGE_SHIFT) >> 20,
371 (e820ram << PAGE_SHIFT) >> 20);
Tejun Heo91556232011-02-16 17:11:09 +0100372 return false;
Tejun Heof9c60252011-02-16 17:11:09 +0100373 }
Tejun Heo91556232011-02-16 17:11:09 +0100374 return true;
Tejun Heof9c60252011-02-16 17:11:09 +0100375}
376
377static int __init numa_register_memblks(struct numa_meminfo *mi)
378{
Tejun Heo91556232011-02-16 17:11:09 +0100379 int i, j, nid;
Tejun Heof9c60252011-02-16 17:11:09 +0100380
381 /* Account for nodes with cpus and no memory */
382 nodes_or(node_possible_map, mem_nodes_parsed, cpu_nodes_parsed);
383 if (WARN_ON(nodes_empty(node_possible_map)))
384 return -EINVAL;
385
Tejun Heo97e7b782011-02-16 17:11:08 +0100386 memnode_shift = compute_hash_shift(mi);
Tejun Heoef396ec2011-02-16 17:11:07 +0100387 if (memnode_shift < 0) {
388 printk(KERN_ERR "NUMA: No NUMA node hash function found. Contact maintainer\n");
389 return -EINVAL;
390 }
391
Tejun Heo97e7b782011-02-16 17:11:08 +0100392 for (i = 0; i < mi->nr_blks; i++)
393 memblock_x86_register_active_regions(mi->blk[i].nid,
394 mi->blk[i].start >> PAGE_SHIFT,
395 mi->blk[i].end >> PAGE_SHIFT);
Tejun Heofd0435d2011-02-16 17:11:08 +0100396
397 /* for out of order entries */
398 sort_node_map();
Tejun Heo91556232011-02-16 17:11:09 +0100399 if (!numa_meminfo_cover_memory(mi))
Tejun Heofd0435d2011-02-16 17:11:08 +0100400 return -EINVAL;
401
402 init_memory_mapping_high();
403
Tejun Heofd0435d2011-02-16 17:11:08 +0100404 /*
Tejun Heo91556232011-02-16 17:11:09 +0100405 * Finally register nodes. Do it twice in case setup_node_bootmem
406 * missed one due to missing bootmem.
Tejun Heofd0435d2011-02-16 17:11:08 +0100407 */
Tejun Heo91556232011-02-16 17:11:09 +0100408 for (i = 0; i < 2; i++) {
409 for_each_node_mask(nid, node_possible_map) {
410 u64 start = (u64)max_pfn << PAGE_SHIFT;
411 u64 end = 0;
412
413 if (node_online(nid))
414 continue;
415
416 for (j = 0; j < mi->nr_blks; j++) {
417 if (nid != mi->blk[j].nid)
418 continue;
419 start = min(mi->blk[j].start, start);
420 end = max(mi->blk[j].end, end);
421 }
422
423 if (start < end)
424 setup_node_bootmem(nid, start, end);
425 }
426 }
Tejun Heofd0435d2011-02-16 17:11:08 +0100427
Tejun Heoef396ec2011-02-16 17:11:07 +0100428 return 0;
429}
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431#ifdef CONFIG_NUMA_EMU
Rohit Seth53fee042007-02-13 13:26:22 +0100432/* Numa emulation */
David Rientjesadc19382009-09-25 15:20:09 -0700433static struct bootnode nodes[MAX_NUMNODES] __initdata;
David Rientjesc1c34432010-12-22 17:23:54 -0800434static struct bootnode physnodes[MAX_NUMNODES] __cpuinitdata;
Thomas Gleixner864fc312008-05-12 15:43:36 +0200435static char *cmdline __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Jan Beulich90321602011-01-19 08:57:21 +0000437void __init numa_emu_cmdline(char *str)
438{
439 cmdline = str;
440}
441
Tejun Heoa844ef42011-02-16 17:11:09 +0100442int __init find_node_by_addr(unsigned long addr)
443{
Tejun Heo91556232011-02-16 17:11:09 +0100444 const struct numa_meminfo *mi = &numa_meminfo;
Tejun Heoa844ef42011-02-16 17:11:09 +0100445 int i;
446
Tejun Heo91556232011-02-16 17:11:09 +0100447 for (i = 0; i < mi->nr_blks; i++) {
Tejun Heoa844ef42011-02-16 17:11:09 +0100448 /*
449 * Find the real node that this emulated node appears on. For
450 * the sake of simplicity, we only use a real node's starting
451 * address to determine which emulated node it appears on.
452 */
Tejun Heo91556232011-02-16 17:11:09 +0100453 if (addr >= mi->blk[i].start && addr < mi->blk[i].end)
454 return mi->blk[i].nid;
Tejun Heoa844ef42011-02-16 17:11:09 +0100455 }
Tejun Heo91556232011-02-16 17:11:09 +0100456 return NUMA_NO_NODE;
Tejun Heoa844ef42011-02-16 17:11:09 +0100457}
458
Tejun Heo190955482011-02-16 12:13:07 +0100459static int __init setup_physnodes(unsigned long start, unsigned long end)
David Rientjesadc19382009-09-25 15:20:09 -0700460{
Tejun Heo91556232011-02-16 17:11:09 +0100461 const struct numa_meminfo *mi = &numa_meminfo;
David Rientjesadc19382009-09-25 15:20:09 -0700462 int ret = 0;
463 int i;
464
David Rientjesc1c34432010-12-22 17:23:54 -0800465 memset(physnodes, 0, sizeof(physnodes));
Tejun Heo190955482011-02-16 12:13:07 +0100466
Tejun Heo91556232011-02-16 17:11:09 +0100467 for (i = 0; i < mi->nr_blks; i++) {
468 int nid = mi->blk[i].nid;
469
470 if (physnodes[nid].start == physnodes[nid].end) {
471 physnodes[nid].start = mi->blk[i].start;
472 physnodes[nid].end = mi->blk[i].end;
473 } else {
474 physnodes[nid].start = min(physnodes[nid].start,
475 mi->blk[i].start);
476 physnodes[nid].end = max(physnodes[nid].end,
477 mi->blk[i].end);
478 }
Tejun Heo190955482011-02-16 12:13:07 +0100479 }
480
David Rientjesadc19382009-09-25 15:20:09 -0700481 /*
482 * Basic sanity checking on the physical node map: there may be errors
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200483 * if the SRAT or AMD code incorrectly reported the topology or the mem=
David Rientjesadc19382009-09-25 15:20:09 -0700484 * kernel parameter is used.
485 */
David Rientjesa387e952010-12-22 17:23:56 -0800486 for (i = 0; i < MAX_NUMNODES; i++) {
David Rientjesadc19382009-09-25 15:20:09 -0700487 if (physnodes[i].start == physnodes[i].end)
488 continue;
489 if (physnodes[i].start > end) {
490 physnodes[i].end = physnodes[i].start;
491 continue;
492 }
493 if (physnodes[i].end < start) {
494 physnodes[i].start = physnodes[i].end;
495 continue;
496 }
497 if (physnodes[i].start < start)
498 physnodes[i].start = start;
499 if (physnodes[i].end > end)
500 physnodes[i].end = end;
David Rientjesadc19382009-09-25 15:20:09 -0700501 ret++;
502 }
503
504 /*
505 * If no physical topology was detected, a single node is faked to cover
506 * the entire address space.
507 */
508 if (!ret) {
509 physnodes[ret].start = start;
510 physnodes[ret].end = end;
511 ret = 1;
512 }
513 return ret;
514}
515
David Rientjesf51bf302010-12-22 17:23:51 -0800516static void __init fake_physnodes(int acpi, int amd, int nr_nodes)
517{
518 int i;
519
520 BUG_ON(acpi && amd);
521#ifdef CONFIG_ACPI_NUMA
522 if (acpi)
523 acpi_fake_nodes(nodes, nr_nodes);
524#endif
525#ifdef CONFIG_AMD_NUMA
526 if (amd)
527 amd_fake_nodes(nodes, nr_nodes);
528#endif
529 if (!acpi && !amd)
530 for (i = 0; i < nr_cpu_ids; i++)
531 numa_set_node(i, 0);
532}
533
Rohit Seth53fee042007-02-13 13:26:22 +0100534/*
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100535 * Setups up nid to range from addr to addr + size. If the end
536 * boundary is greater than max_addr, then max_addr is used instead.
537 * The return value is 0 if there is additional memory left for
538 * allocation past addr and -1 otherwise. addr is adjusted to be at
539 * the end of the node.
Rohit Seth53fee042007-02-13 13:26:22 +0100540 */
David Rientjesadc19382009-09-25 15:20:09 -0700541static int __init setup_node_range(int nid, u64 *addr, u64 size, u64 max_addr)
Rohit Seth53fee042007-02-13 13:26:22 +0100542{
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200543 int ret = 0;
544 nodes[nid].start = *addr;
545 *addr += size;
546 if (*addr >= max_addr) {
547 *addr = max_addr;
548 ret = -1;
549 }
550 nodes[nid].end = *addr;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200551 node_set(nid, node_possible_map);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200552 printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
553 nodes[nid].start, nodes[nid].end,
554 (nodes[nid].end - nodes[nid].start) >> 20);
555 return ret;
Rohit Seth53fee042007-02-13 13:26:22 +0100556}
557
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200558/*
David Rientjesadc19382009-09-25 15:20:09 -0700559 * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
560 * to max_addr. The return value is the number of nodes allocated.
561 */
David Rientjesc1c34432010-12-22 17:23:54 -0800562static int __init split_nodes_interleave(u64 addr, u64 max_addr, int nr_nodes)
David Rientjesadc19382009-09-25 15:20:09 -0700563{
564 nodemask_t physnode_mask = NODE_MASK_NONE;
565 u64 size;
566 int big;
567 int ret = 0;
568 int i;
569
570 if (nr_nodes <= 0)
571 return -1;
572 if (nr_nodes > MAX_NUMNODES) {
573 pr_info("numa=fake=%d too large, reducing to %d\n",
574 nr_nodes, MAX_NUMNODES);
575 nr_nodes = MAX_NUMNODES;
576 }
577
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700578 size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) / nr_nodes;
David Rientjesadc19382009-09-25 15:20:09 -0700579 /*
580 * Calculate the number of big nodes that can be allocated as a result
581 * of consolidating the remainder.
582 */
David Rientjes68fd1112010-02-15 13:43:25 -0800583 big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
David Rientjesadc19382009-09-25 15:20:09 -0700584 FAKE_NODE_MIN_SIZE;
585
586 size &= FAKE_NODE_MIN_HASH_MASK;
587 if (!size) {
588 pr_err("Not enough memory for each node. "
589 "NUMA emulation disabled.\n");
590 return -1;
591 }
592
David Rientjesc1c34432010-12-22 17:23:54 -0800593 for (i = 0; i < MAX_NUMNODES; i++)
David Rientjesadc19382009-09-25 15:20:09 -0700594 if (physnodes[i].start != physnodes[i].end)
595 node_set(i, physnode_mask);
596
597 /*
598 * Continue to fill physical nodes with fake nodes until there is no
599 * memory left on any of them.
600 */
601 while (nodes_weight(physnode_mask)) {
602 for_each_node_mask(i, physnode_mask) {
603 u64 end = physnodes[i].start + size;
604 u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
605
606 if (ret < big)
607 end += FAKE_NODE_MIN_SIZE;
608
609 /*
610 * Continue to add memory to this fake node if its
611 * non-reserved memory is less than the per-node size.
612 */
613 while (end - physnodes[i].start -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700614 memblock_x86_hole_size(physnodes[i].start, end) < size) {
David Rientjesadc19382009-09-25 15:20:09 -0700615 end += FAKE_NODE_MIN_SIZE;
616 if (end > physnodes[i].end) {
617 end = physnodes[i].end;
618 break;
619 }
620 }
621
622 /*
623 * If there won't be at least FAKE_NODE_MIN_SIZE of
624 * non-reserved memory in ZONE_DMA32 for the next node,
625 * this one must extend to the boundary.
626 */
627 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700628 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjesadc19382009-09-25 15:20:09 -0700629 end = dma32_end;
630
631 /*
632 * If there won't be enough non-reserved memory for the
633 * next node, this one must extend to the end of the
634 * physical node.
635 */
636 if (physnodes[i].end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700637 memblock_x86_hole_size(end, physnodes[i].end) < size)
David Rientjesadc19382009-09-25 15:20:09 -0700638 end = physnodes[i].end;
639
640 /*
641 * Avoid allocating more nodes than requested, which can
642 * happen as a result of rounding down each node's size
643 * to FAKE_NODE_MIN_SIZE.
644 */
645 if (nodes_weight(physnode_mask) + ret >= nr_nodes)
646 end = physnodes[i].end;
647
648 if (setup_node_range(ret++, &physnodes[i].start,
649 end - physnodes[i].start,
650 physnodes[i].end) < 0)
651 node_clear(i, physnode_mask);
652 }
653 }
654 return ret;
655}
656
657/*
David Rientjes8df5bb342010-02-15 13:43:30 -0800658 * Returns the end address of a node so that there is at least `size' amount of
659 * non-reserved memory or `max_addr' is reached.
660 */
661static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
662{
663 u64 end = start + size;
664
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700665 while (end - start - memblock_x86_hole_size(start, end) < size) {
David Rientjes8df5bb342010-02-15 13:43:30 -0800666 end += FAKE_NODE_MIN_SIZE;
667 if (end > max_addr) {
668 end = max_addr;
669 break;
670 }
671 }
672 return end;
673}
674
675/*
676 * Sets up fake nodes of `size' interleaved over physical nodes ranging from
677 * `addr' to `max_addr'. The return value is the number of nodes allocated.
678 */
679static int __init split_nodes_size_interleave(u64 addr, u64 max_addr, u64 size)
680{
681 nodemask_t physnode_mask = NODE_MASK_NONE;
682 u64 min_size;
683 int ret = 0;
684 int i;
685
686 if (!size)
687 return -1;
688 /*
689 * The limit on emulated nodes is MAX_NUMNODES, so the size per node is
690 * increased accordingly if the requested size is too small. This
691 * creates a uniform distribution of node sizes across the entire
692 * machine (but not necessarily over physical nodes).
693 */
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700694 min_size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) /
David Rientjes8df5bb342010-02-15 13:43:30 -0800695 MAX_NUMNODES;
696 min_size = max(min_size, FAKE_NODE_MIN_SIZE);
697 if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size)
698 min_size = (min_size + FAKE_NODE_MIN_SIZE) &
699 FAKE_NODE_MIN_HASH_MASK;
700 if (size < min_size) {
701 pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
702 size >> 20, min_size >> 20);
703 size = min_size;
704 }
705 size &= FAKE_NODE_MIN_HASH_MASK;
706
707 for (i = 0; i < MAX_NUMNODES; i++)
708 if (physnodes[i].start != physnodes[i].end)
709 node_set(i, physnode_mask);
710 /*
711 * Fill physical nodes with fake nodes of size until there is no memory
712 * left on any of them.
713 */
714 while (nodes_weight(physnode_mask)) {
715 for_each_node_mask(i, physnode_mask) {
716 u64 dma32_end = MAX_DMA32_PFN << PAGE_SHIFT;
717 u64 end;
718
719 end = find_end_of_node(physnodes[i].start,
720 physnodes[i].end, size);
721 /*
722 * If there won't be at least FAKE_NODE_MIN_SIZE of
723 * non-reserved memory in ZONE_DMA32 for the next node,
724 * this one must extend to the boundary.
725 */
726 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700727 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjes8df5bb342010-02-15 13:43:30 -0800728 end = dma32_end;
729
730 /*
731 * If there won't be enough non-reserved memory for the
732 * next node, this one must extend to the end of the
733 * physical node.
734 */
735 if (physnodes[i].end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700736 memblock_x86_hole_size(end, physnodes[i].end) < size)
David Rientjes8df5bb342010-02-15 13:43:30 -0800737 end = physnodes[i].end;
738
739 /*
740 * Setup the fake node that will be allocated as bootmem
741 * later. If setup_node_range() returns non-zero, there
742 * is no more memory available on this physical node.
743 */
744 if (setup_node_range(ret++, &physnodes[i].start,
745 end - physnodes[i].start,
746 physnodes[i].end) < 0)
747 node_clear(i, physnode_mask);
748 }
749 }
750 return ret;
751}
752
753/*
Thomas Gleixner886533a2008-05-12 15:43:36 +0200754 * Sets up the system RAM area from start_pfn to last_pfn according to the
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200755 * numa=fake command-line option.
756 */
David Rientjesadc19382009-09-25 15:20:09 -0700757static int __init numa_emulation(unsigned long start_pfn,
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200758 unsigned long last_pfn, int acpi, int amd)
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200759{
Tejun Heo97e7b782011-02-16 17:11:08 +0100760 static struct numa_meminfo ei __initdata;
David Rientjesca2107c2010-02-15 13:43:33 -0800761 u64 addr = start_pfn << PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200762 u64 max_addr = last_pfn << PAGE_SHIFT;
David Rientjesca2107c2010-02-15 13:43:33 -0800763 int num_nodes;
764 int i;
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200765
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200766 /*
David Rientjes8df5bb342010-02-15 13:43:30 -0800767 * If the numa=fake command-line contains a 'M' or 'G', it represents
David Rientjesca2107c2010-02-15 13:43:33 -0800768 * the fixed node size. Otherwise, if it is just a single number N,
769 * split the system RAM into N fake nodes.
David Rientjes8df5bb342010-02-15 13:43:30 -0800770 */
771 if (strchr(cmdline, 'M') || strchr(cmdline, 'G')) {
David Rientjesca2107c2010-02-15 13:43:33 -0800772 u64 size;
773
David Rientjes8df5bb342010-02-15 13:43:30 -0800774 size = memparse(cmdline, &cmdline);
775 num_nodes = split_nodes_size_interleave(addr, max_addr, size);
David Rientjesca2107c2010-02-15 13:43:33 -0800776 } else {
777 unsigned long n;
778
779 n = simple_strtoul(cmdline, NULL, 0);
David Rientjesc1c34432010-12-22 17:23:54 -0800780 num_nodes = split_nodes_interleave(addr, max_addr, n);
David Rientjes8df5bb342010-02-15 13:43:30 -0800781 }
782
David Rientjesca2107c2010-02-15 13:43:33 -0800783 if (num_nodes < 0)
784 return num_nodes;
Tejun Heo8968dab2011-02-16 17:11:08 +0100785
Tejun Heo97e7b782011-02-16 17:11:08 +0100786 ei.nr_blks = num_nodes;
787 for (i = 0; i < ei.nr_blks; i++) {
788 ei.blk[i].start = nodes[i].start;
789 ei.blk[i].end = nodes[i].end;
790 ei.blk[i].nid = i;
791 }
Tejun Heo8968dab2011-02-16 17:11:08 +0100792
Tejun Heo97e7b782011-02-16 17:11:08 +0100793 memnode_shift = compute_hash_shift(&ei);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200794 if (memnode_shift < 0) {
795 memnode_shift = 0;
796 printk(KERN_ERR "No NUMA hash function found. NUMA emulation "
797 "disabled.\n");
798 return -1;
799 }
800
801 /*
David Rientjesadc19382009-09-25 15:20:09 -0700802 * We need to vacate all active ranges that may have been registered for
803 * the e820 memory map.
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200804 */
805 remove_all_active_ranges();
Yinghai Lu1411e0e2010-12-27 16:48:17 -0800806 for_each_node_mask(i, node_possible_map)
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700807 memblock_x86_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
Mel Gorman5cb248a2006-09-27 01:49:52 -0700808 nodes[i].end >> PAGE_SHIFT);
Yinghai Lu1411e0e2010-12-27 16:48:17 -0800809 init_memory_mapping_high();
810 for_each_node_mask(i, node_possible_map)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100811 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
Tejun Heo190955482011-02-16 12:13:07 +0100812 setup_physnodes(addr, max_addr);
David Rientjesf51bf302010-12-22 17:23:51 -0800813 fake_physnodes(acpi, amd, num_nodes);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100814 numa_init_array();
815 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200817#endif /* CONFIG_NUMA_EMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Tejun Heoffe77a42011-02-16 12:13:06 +0100819static int dummy_numa_init(void)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100820{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 printk(KERN_INFO "%s\n",
822 numa_off ? "NUMA turned off" : "No NUMA configuration found");
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100823 printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
Tejun Heo86ef4db2011-02-16 12:13:06 +0100824 0LU, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100825
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100826 node_set(0, cpu_nodes_parsed);
827 node_set(0, mem_nodes_parsed);
Tejun Heo43a662f2011-02-16 17:11:08 +0100828 numa_add_memblk(0, 0, (u64)max_pfn << PAGE_SHIFT);
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100829
830 return 0;
831}
832
Tejun Heoffe77a42011-02-16 12:13:06 +0100833void __init initmem_init(void)
834{
835 int (*numa_init[])(void) = { [2] = dummy_numa_init };
Tejun Heoffe77a42011-02-16 12:13:06 +0100836 int i, j;
837
838 if (!numa_off) {
839#ifdef CONFIG_ACPI_NUMA
840 numa_init[0] = x86_acpi_numa_init;
Tejun Heoffe77a42011-02-16 12:13:06 +0100841#endif
842#ifdef CONFIG_AMD_NUMA
843 numa_init[1] = amd_numa_init;
Tejun Heoffe77a42011-02-16 12:13:06 +0100844#endif
845 }
846
847 for (i = 0; i < ARRAY_SIZE(numa_init); i++) {
848 if (!numa_init[i])
849 continue;
850
851 for (j = 0; j < MAX_LOCAL_APIC; j++)
852 set_apicid_to_node(j, NUMA_NO_NODE);
853
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100854 nodes_clear(cpu_nodes_parsed);
855 nodes_clear(mem_nodes_parsed);
Tejun Heoffe77a42011-02-16 12:13:06 +0100856 nodes_clear(node_possible_map);
857 nodes_clear(node_online_map);
Tejun Heo97e7b782011-02-16 17:11:08 +0100858 memset(&numa_meminfo, 0, sizeof(numa_meminfo));
Tejun Heofd0435d2011-02-16 17:11:08 +0100859 remove_all_active_ranges();
Tejun Heoffe77a42011-02-16 12:13:06 +0100860
861 if (numa_init[i]() < 0)
862 continue;
Tejun Heo206e4202011-02-16 12:13:07 +0100863
Tejun Heo56e827f2011-02-16 17:11:09 +0100864 if (numa_cleanup_meminfo(&numa_meminfo) < 0)
865 continue;
Tejun Heoffe77a42011-02-16 12:13:06 +0100866#ifdef CONFIG_NUMA_EMU
Tejun Heo190955482011-02-16 12:13:07 +0100867 setup_physnodes(0, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100868 if (cmdline && !numa_emulation(0, max_pfn, i == 0, i == 1))
869 return;
Tejun Heo190955482011-02-16 12:13:07 +0100870 setup_physnodes(0, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100871 nodes_clear(node_possible_map);
872 nodes_clear(node_online_map);
873#endif
Tejun Heof9c60252011-02-16 17:11:09 +0100874 if (numa_register_memblks(&numa_meminfo) < 0)
Tejun Heo43a662f2011-02-16 17:11:08 +0100875 continue;
876
Tejun Heofd0435d2011-02-16 17:11:08 +0100877 for (j = 0; j < nr_cpu_ids; j++) {
878 int nid = early_cpu_to_node(j);
879
880 if (nid == NUMA_NO_NODE)
881 continue;
882 if (!node_online(nid))
883 numa_clear_node(j);
884 }
885 numa_init_array();
886 return;
Tejun Heoffe77a42011-02-16 12:13:06 +0100887 }
888 BUG();
Andi Kleen69d81fc2005-11-05 17:25:53 +0100889}
890
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100891unsigned long __init numa_free_all_bootmem(void)
892{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 unsigned long pages = 0;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100894 int i;
895
896 for_each_online_node(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 pages += free_all_bootmem_node(NODE_DATA(i));
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100898
Yinghai Lu08677212010-02-10 01:20:20 -0800899 pages += free_all_memory_core_early(MAX_NUMNODES);
Yinghai Lu08677212010-02-10 01:20:20 -0800900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return pages;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100902}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100904int __cpuinit numa_cpu_node(int cpu)
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800905{
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100906 int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800907
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100908 if (apicid != BAD_APICID)
909 return __apicid_to_node[apicid];
910 return NUMA_NO_NODE;
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800911}
912
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100913/*
Tejun Heode2d9442011-01-23 14:37:41 +0100914 * UGLINESS AHEAD: Currently, CONFIG_NUMA_EMU is 64bit only and makes use
915 * of 64bit specific data structures. The distinction is artificial and
916 * should be removed. numa_{add|remove}_cpu() are implemented in numa.c
917 * for both 32 and 64bit when CONFIG_NUMA_EMU is disabled but here when
918 * enabled.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100919 *
Tejun Heode2d9442011-01-23 14:37:41 +0100920 * NUMA emulation is planned to be made generic and the following and other
921 * related code should be moved to numa.c.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100922 */
Tejun Heode2d9442011-01-23 14:37:41 +0100923#ifdef CONFIG_NUMA_EMU
924# ifndef CONFIG_DEBUG_PER_CPU_MAPS
David Rientjesc1c34432010-12-22 17:23:54 -0800925void __cpuinit numa_add_cpu(int cpu)
926{
927 unsigned long addr;
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100928 int physnid, nid;
David Rientjesc1c34432010-12-22 17:23:54 -0800929
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100930 nid = numa_cpu_node(cpu);
David Rientjesc1c34432010-12-22 17:23:54 -0800931 if (nid == NUMA_NO_NODE)
932 nid = early_cpu_to_node(cpu);
933 BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
934
935 /*
936 * Use the starting address of the emulated node to find which physical
937 * node it is allocated on.
938 */
939 addr = node_start_pfn(nid) << PAGE_SHIFT;
940 for (physnid = 0; physnid < MAX_NUMNODES; physnid++)
941 if (addr >= physnodes[physnid].start &&
942 addr < physnodes[physnid].end)
943 break;
944
945 /*
946 * Map the cpu to each emulated node that is allocated on the physical
947 * node of the cpu's apic id.
948 */
949 for_each_online_node(nid) {
950 addr = node_start_pfn(nid) << PAGE_SHIFT;
951 if (addr >= physnodes[physnid].start &&
952 addr < physnodes[physnid].end)
953 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
954 }
955}
956
957void __cpuinit numa_remove_cpu(int cpu)
958{
959 int i;
960
961 for_each_online_node(i)
962 cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
963}
Tejun Heode2d9442011-01-23 14:37:41 +0100964# else /* !CONFIG_DEBUG_PER_CPU_MAPS */
David Rientjesd906f0e2010-12-30 10:54:16 -0800965static void __cpuinit numa_set_cpumask(int cpu, int enable)
966{
967 int node = early_cpu_to_node(cpu);
968 struct cpumask *mask;
David Rientjesc1c34432010-12-22 17:23:54 -0800969 int i;
Brian Gerst6470aff2009-01-27 12:56:47 +0900970
David Rientjes14392fd2011-02-07 14:08:53 -0800971 if (node == NUMA_NO_NODE) {
972 /* early_cpu_to_node() already emits a warning and trace */
973 return;
974 }
David Rientjesc1c34432010-12-22 17:23:54 -0800975 for_each_online_node(i) {
976 unsigned long addr;
977
978 addr = node_start_pfn(i) << PAGE_SHIFT;
979 if (addr < physnodes[node].start ||
980 addr >= physnodes[node].end)
981 continue;
David Rientjesd906f0e2010-12-30 10:54:16 -0800982 mask = debug_cpumask_set_cpu(cpu, enable);
983 if (!mask)
David Rientjesc1c34432010-12-22 17:23:54 -0800984 return;
David Rientjesc1c34432010-12-22 17:23:54 -0800985
986 if (enable)
987 cpumask_set_cpu(cpu, mask);
988 else
989 cpumask_clear_cpu(cpu, mask);
Brian Gerst6470aff2009-01-27 12:56:47 +0900990 }
Brian Gerst6470aff2009-01-27 12:56:47 +0900991}
992
993void __cpuinit numa_add_cpu(int cpu)
994{
995 numa_set_cpumask(cpu, 1);
996}
997
998void __cpuinit numa_remove_cpu(int cpu)
999{
1000 numa_set_cpumask(cpu, 0);
1001}
Tejun Heode2d9442011-01-23 14:37:41 +01001002# endif /* !CONFIG_DEBUG_PER_CPU_MAPS */
1003#endif /* CONFIG_NUMA_EMU */