blob: 8b1f178a866e7270fd3eb9dd3853aec4e61d8de1 [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 Heo92d4a432011-02-16 17:11:09 +010039nodemask_t numa_nodes_parsed __initdata;
Tejun Heoec8cf29b2011-02-16 12:13:07 +010040
Eric Dumazetdcf36bf2006-03-25 16:31:46 +010041struct memnode memnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Thomas Gleixner864fc312008-05-12 15:43:36 +020043static unsigned long __initdata nodemap_addr;
44static unsigned long __initdata nodemap_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Tejun Heo97e7b782011-02-16 17:11:08 +010046static struct numa_meminfo numa_meminfo __initdata;
Tejun Heoef396ec2011-02-16 17:11:07 +010047
Brian Gerst6470aff2009-01-27 12:56:47 +090048/*
Eric Dumazet529a3402005-11-05 17:25:54 +010049 * Given a shift value, try to populate memnodemap[]
50 * Returns :
51 * 1 if OK
52 * 0 if memnodmap[] too small (of shift too small)
53 * -1 if node overlap or lost ram (shift too big)
54 */
Tejun Heo97e7b782011-02-16 17:11:08 +010055static int __init populate_memnodemap(const struct numa_meminfo *mi, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Eric Dumazet529a3402005-11-05 17:25:54 +010057 unsigned long addr, end;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010058 int i, res = -1;
Keith Manntheyb6846642005-07-28 21:15:38 -070059
travis@sgi.com43238382008-01-30 13:33:25 +010060 memset(memnodemap, 0xff, sizeof(s16)*memnodemapsize);
Tejun Heo97e7b782011-02-16 17:11:08 +010061 for (i = 0; i < mi->nr_blks; i++) {
62 addr = mi->blk[i].start;
63 end = mi->blk[i].end;
Eric Dumazet529a3402005-11-05 17:25:54 +010064 if (addr >= end)
65 continue;
Amul Shah076422d2007-02-13 13:26:19 +010066 if ((end >> shift) >= memnodemapsize)
Eric Dumazet529a3402005-11-05 17:25:54 +010067 return 0;
68 do {
travis@sgi.com43238382008-01-30 13:33:25 +010069 if (memnodemap[addr >> shift] != NUMA_NO_NODE)
Eric Dumazet529a3402005-11-05 17:25:54 +010070 return -1;
Tejun Heo97e7b782011-02-16 17:11:08 +010071 memnodemap[addr >> shift] = mi->blk[i].nid;
Amul Shah076422d2007-02-13 13:26:19 +010072 addr += (1UL << shift);
Eric Dumazet529a3402005-11-05 17:25:54 +010073 } while (addr < end);
74 res = 1;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010075 }
Eric Dumazet529a3402005-11-05 17:25:54 +010076 return res;
77}
78
Amul Shah076422d2007-02-13 13:26:19 +010079static int __init allocate_cachealigned_memnodemap(void)
80{
Yinghai Lu24a5da72008-02-01 17:49:41 +010081 unsigned long addr;
Amul Shah076422d2007-02-13 13:26:19 +010082
83 memnodemap = memnode.embedded_map;
travis@sgi.com316390b2008-01-30 13:33:15 +010084 if (memnodemapsize <= ARRAY_SIZE(memnode.embedded_map))
Amul Shah076422d2007-02-13 13:26:19 +010085 return 0;
Amul Shah076422d2007-02-13 13:26:19 +010086
Yinghai Lu24a5da72008-02-01 17:49:41 +010087 addr = 0x8000;
Joerg Roedelbe3e89e2008-07-25 16:48:58 +020088 nodemap_size = roundup(sizeof(s16) * memnodemapsize, L1_CACHE_BYTES);
Yinghai Ludbef7b52010-12-27 16:48:08 -080089 nodemap_addr = memblock_find_in_range(addr, get_max_mapped(),
Yinghai Lu24a5da72008-02-01 17:49:41 +010090 nodemap_size, L1_CACHE_BYTES);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070091 if (nodemap_addr == MEMBLOCK_ERROR) {
Amul Shah076422d2007-02-13 13:26:19 +010092 printk(KERN_ERR
93 "NUMA: Unable to allocate Memory to Node hash map\n");
94 nodemap_addr = nodemap_size = 0;
95 return -1;
96 }
Yinghai Lu24a5da72008-02-01 17:49:41 +010097 memnodemap = phys_to_virt(nodemap_addr);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070098 memblock_x86_reserve_range(nodemap_addr, nodemap_addr + nodemap_size, "MEMNODEMAP");
Amul Shah076422d2007-02-13 13:26:19 +010099
100 printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n",
101 nodemap_addr, nodemap_addr + nodemap_size);
102 return 0;
103}
104
105/*
106 * The LSB of all start and end addresses in the node map is the value of the
107 * maximum possible shift.
108 */
Tejun Heo97e7b782011-02-16 17:11:08 +0100109static int __init extract_lsb_from_nodes(const struct numa_meminfo *mi)
Amul Shah076422d2007-02-13 13:26:19 +0100110{
Amul Shah54413922007-02-13 13:26:20 +0100111 int i, nodes_used = 0;
Amul Shah076422d2007-02-13 13:26:19 +0100112 unsigned long start, end;
113 unsigned long bitfield = 0, memtop = 0;
114
Tejun Heo97e7b782011-02-16 17:11:08 +0100115 for (i = 0; i < mi->nr_blks; i++) {
116 start = mi->blk[i].start;
117 end = mi->blk[i].end;
Amul Shah076422d2007-02-13 13:26:19 +0100118 if (start >= end)
119 continue;
Amul Shah54413922007-02-13 13:26:20 +0100120 bitfield |= start;
121 nodes_used++;
Amul Shah076422d2007-02-13 13:26:19 +0100122 if (end > memtop)
123 memtop = end;
124 }
Amul Shah54413922007-02-13 13:26:20 +0100125 if (nodes_used <= 1)
126 i = 63;
127 else
128 i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
Amul Shah076422d2007-02-13 13:26:19 +0100129 memnodemapsize = (memtop >> i)+1;
130 return i;
131}
132
Tejun Heo97e7b782011-02-16 17:11:08 +0100133static int __init compute_hash_shift(const struct numa_meminfo *mi)
Eric Dumazet529a3402005-11-05 17:25:54 +0100134{
Amul Shah076422d2007-02-13 13:26:19 +0100135 int shift;
Eric Dumazet529a3402005-11-05 17:25:54 +0100136
Tejun Heo97e7b782011-02-16 17:11:08 +0100137 shift = extract_lsb_from_nodes(mi);
Amul Shah076422d2007-02-13 13:26:19 +0100138 if (allocate_cachealigned_memnodemap())
139 return -1;
Andi Kleen6b050f82006-01-11 22:44:33 +0100140 printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
Eric Dumazet529a3402005-11-05 17:25:54 +0100141 shift);
142
Tejun Heo97e7b782011-02-16 17:11:08 +0100143 if (populate_memnodemap(mi, shift) != 1) {
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100144 printk(KERN_INFO "Your memory is not aligned you need to "
145 "rebuild your kernel with a bigger NODEMAPSIZE "
146 "shift=%d\n", shift);
Eric Dumazet529a3402005-11-05 17:25:54 +0100147 return -1;
148 }
Keith Manntheyb6846642005-07-28 21:15:38 -0700149 return shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -0800152int __meminit __early_pfn_to_nid(unsigned long pfn)
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700153{
154 return phys_to_nid(pfn << PAGE_SHIFT);
155}
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700156
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100157static void * __init early_node_mem(int nodeid, unsigned long start,
Yinghai Lu24a5da72008-02-01 17:49:41 +0100158 unsigned long end, unsigned long size,
159 unsigned long align)
Andi Kleena8062232006-04-07 19:49:21 +0200160{
Yinghai Lucef625e2010-02-10 01:20:18 -0800161 unsigned long mem;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100162
Yinghai Lucef625e2010-02-10 01:20:18 -0800163 /*
164 * put it on high as possible
165 * something will go with NODE_DATA
166 */
167 if (start < (MAX_DMA_PFN<<PAGE_SHIFT))
168 start = MAX_DMA_PFN<<PAGE_SHIFT;
169 if (start < (MAX_DMA32_PFN<<PAGE_SHIFT) &&
170 end > (MAX_DMA32_PFN<<PAGE_SHIFT))
171 start = MAX_DMA32_PFN<<PAGE_SHIFT;
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700172 mem = memblock_x86_find_in_range_node(nodeid, start, end, size, align);
173 if (mem != MEMBLOCK_ERROR)
Andi Kleena8062232006-04-07 19:49:21 +0200174 return __va(mem);
Yinghai Lu9347e0b2008-02-01 17:49:42 +0100175
Yinghai Lucef625e2010-02-10 01:20:18 -0800176 /* extend the search scope */
177 end = max_pfn_mapped << PAGE_SHIFT;
Yinghai Lu419db272010-10-28 09:50:17 -0700178 start = MAX_DMA_PFN << PAGE_SHIFT;
179 mem = memblock_find_in_range(start, end, size, align);
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700180 if (mem != MEMBLOCK_ERROR)
Yinghai Lu1842f902010-02-10 01:20:15 -0800181 return __va(mem);
182
183 printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100184 size, nodeid);
Yinghai Lu1842f902010-02-10 01:20:15 -0800185
186 return NULL;
Andi Kleena8062232006-04-07 19:49:21 +0200187}
188
Tejun Heoef396ec2011-02-16 17:11:07 +0100189int __init numa_add_memblk(int nid, u64 start, u64 end)
190{
Tejun Heo97e7b782011-02-16 17:11:08 +0100191 struct numa_meminfo *mi = &numa_meminfo;
Tejun Heoef396ec2011-02-16 17:11:07 +0100192
Tejun Heo56e827f2011-02-16 17:11:09 +0100193 /* ignore zero length blks */
194 if (start == end)
195 return 0;
196
197 /* whine about and ignore invalid blks */
198 if (start > end || nid < 0 || nid >= MAX_NUMNODES) {
199 pr_warning("NUMA: Warning: invalid memblk node %d (%Lx-%Lx)\n",
200 nid, start, end);
201 return 0;
202 }
203
204 if (mi->nr_blks >= NR_NODE_MEMBLKS) {
205 pr_err("NUMA: too many memblk ranges\n");
Tejun Heoef396ec2011-02-16 17:11:07 +0100206 return -EINVAL;
207 }
208
Tejun Heo97e7b782011-02-16 17:11:08 +0100209 mi->blk[mi->nr_blks].start = start;
210 mi->blk[mi->nr_blks].end = end;
211 mi->blk[mi->nr_blks].nid = nid;
212 mi->nr_blks++;
Tejun Heoef396ec2011-02-16 17:11:07 +0100213 return 0;
214}
215
Tejun Heo2e756be2011-02-16 17:11:09 +0100216static void __init numa_remove_memblk_from(int idx, struct numa_meminfo *mi)
217{
218 mi->nr_blks--;
219 memmove(&mi->blk[idx], &mi->blk[idx + 1],
220 (mi->nr_blks - idx) * sizeof(mi->blk[0]));
221}
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223/* Initialize bootmem allocator for a node */
Yinghai Lu7c437692009-05-15 13:59:37 -0700224void __init
225setup_node_bootmem(int nodeid, unsigned long start, unsigned long end)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100226{
Yinghai Lu08677212010-02-10 01:20:20 -0800227 unsigned long start_pfn, last_pfn, nodedata_phys;
Yinghai Lu7c437692009-05-15 13:59:37 -0700228 const int pgdat_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
Yinghai Lu1a27fc02008-03-18 12:52:37 -0700229 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Yinghai Lu4c31e922009-04-22 14:19:27 -0700231 if (!end)
232 return;
233
Yinghai Lu7c437692009-05-15 13:59:37 -0700234 /*
235 * Don't confuse VM with a node that doesn't have the
236 * minimum amount of memory:
237 */
238 if (end && (end - start) < NODE_MIN_SIZE)
239 return;
240
Joerg Roedelbe3e89e2008-07-25 16:48:58 +0200241 start = roundup(start, ZONE_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Yinghai Lu08677212010-02-10 01:20:20 -0800243 printk(KERN_INFO "Initmem setup node %d %016lx-%016lx\n", nodeid,
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100244 start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 start_pfn = start >> PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200247 last_pfn = end >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Yinghai Lu24a5da72008-02-01 17:49:41 +0100249 node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
250 SMP_CACHE_BYTES);
Andi Kleena8062232006-04-07 19:49:21 +0200251 if (node_data[nodeid] == NULL)
252 return;
253 nodedata_phys = __pa(node_data[nodeid]);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700254 memblock_x86_reserve_range(nodedata_phys, nodedata_phys + pgdat_size, "NODE_DATA");
Yinghai Lu6118f762008-02-04 16:47:56 +0100255 printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
256 nodedata_phys + pgdat_size - 1);
Yinghai Lu1842f902010-02-10 01:20:15 -0800257 nid = phys_to_nid(nodedata_phys);
258 if (nid != nodeid)
259 printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nodeid, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
Yinghai Lu08677212010-02-10 01:20:20 -0800262 NODE_DATA(nodeid)->node_id = nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 NODE_DATA(nodeid)->node_start_pfn = start_pfn;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200264 NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 node_set_online(nodeid);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100267}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Tejun Heof9c60252011-02-16 17:11:09 +0100269static int __init numa_cleanup_meminfo(struct numa_meminfo *mi)
Tejun Heofd0435d2011-02-16 17:11:08 +0100270{
Tejun Heo56e827f2011-02-16 17:11:09 +0100271 const u64 low = 0;
272 const u64 high = (u64)max_pfn << PAGE_SHIFT;
Tejun Heo2e756be2011-02-16 17:11:09 +0100273 int i, j, k;
Tejun Heoef396ec2011-02-16 17:11:07 +0100274
Tejun Heo2e756be2011-02-16 17:11:09 +0100275 for (i = 0; i < mi->nr_blks; i++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100276 struct numa_memblk *bi = &mi->blk[i];
Tejun Heoef396ec2011-02-16 17:11:07 +0100277
Tejun Heo56e827f2011-02-16 17:11:09 +0100278 /* make sure all blocks are inside the limits */
279 bi->start = max(bi->start, low);
280 bi->end = min(bi->end, high);
281
282 /* and there's no empty block */
283 if (bi->start == bi->end) {
284 numa_remove_memblk_from(i--, mi);
285 continue;
286 }
287
Tejun Heo2e756be2011-02-16 17:11:09 +0100288 for (j = i + 1; j < mi->nr_blks; j++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100289 struct numa_memblk *bj = &mi->blk[j];
Tejun Heoef396ec2011-02-16 17:11:07 +0100290 unsigned long start, end;
291
Tejun Heo2e756be2011-02-16 17:11:09 +0100292 /*
Tejun Heo56e827f2011-02-16 17:11:09 +0100293 * See whether there are overlapping blocks. Whine
294 * about but allow overlaps of the same nid. They
295 * will be merged below.
296 */
297 if (bi->end > bj->start && bi->start < bj->end) {
298 if (bi->nid != bj->nid) {
299 pr_err("NUMA: node %d (%Lx-%Lx) overlaps with node %d (%Lx-%Lx)\n",
300 bi->nid, bi->start, bi->end,
301 bj->nid, bj->start, bj->end);
302 return -EINVAL;
303 }
304 pr_warning("NUMA: Warning: node %d (%Lx-%Lx) overlaps with itself (%Lx-%Lx)\n",
305 bi->nid, bi->start, bi->end,
306 bj->start, bj->end);
307 }
308
309 /*
Tejun Heo2e756be2011-02-16 17:11:09 +0100310 * Join together blocks on the same node, holes
311 * between which don't overlap with memory on other
312 * nodes.
313 */
Tejun Heo97e7b782011-02-16 17:11:08 +0100314 if (bi->nid != bj->nid)
Tejun Heoef396ec2011-02-16 17:11:07 +0100315 continue;
Tejun Heo56e827f2011-02-16 17:11:09 +0100316 start = max(min(bi->start, bj->start), low);
317 end = min(max(bi->end, bj->end), high);
Tejun Heo2e756be2011-02-16 17:11:09 +0100318 for (k = 0; k < mi->nr_blks; k++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100319 struct numa_memblk *bk = &mi->blk[k];
320
321 if (bi->nid == bk->nid)
Tejun Heoef396ec2011-02-16 17:11:07 +0100322 continue;
Tejun Heo97e7b782011-02-16 17:11:08 +0100323 if (start < bk->end && end > bk->start)
Tejun Heoef396ec2011-02-16 17:11:07 +0100324 break;
325 }
Tejun Heo97e7b782011-02-16 17:11:08 +0100326 if (k < mi->nr_blks)
Tejun Heoef396ec2011-02-16 17:11:07 +0100327 continue;
Tejun Heoef396ec2011-02-16 17:11:07 +0100328 printk(KERN_INFO "NUMA: Node %d [%Lx,%Lx) + [%Lx,%Lx) -> [%lx,%lx)\n",
Tejun Heo97e7b782011-02-16 17:11:08 +0100329 bi->nid, bi->start, bi->end, bj->start, bj->end,
Tejun Heoef396ec2011-02-16 17:11:07 +0100330 start, end);
Tejun Heo97e7b782011-02-16 17:11:08 +0100331 bi->start = start;
332 bi->end = end;
Tejun Heo2e756be2011-02-16 17:11:09 +0100333 numa_remove_memblk_from(j--, mi);
Tejun Heoef396ec2011-02-16 17:11:07 +0100334 }
335 }
336
Tejun Heo56e827f2011-02-16 17:11:09 +0100337 for (i = mi->nr_blks; i < ARRAY_SIZE(mi->blk); i++) {
338 mi->blk[i].start = mi->blk[i].end = 0;
339 mi->blk[i].nid = NUMA_NO_NODE;
340 }
341
Tejun Heof9c60252011-02-16 17:11:09 +0100342 return 0;
343}
344
345/*
Tejun Heo4697bdc2011-02-16 17:11:09 +0100346 * Set nodes, which have memory in @mi, in *@nodemask.
347 */
348static void __init numa_nodemask_from_meminfo(nodemask_t *nodemask,
349 const struct numa_meminfo *mi)
350{
351 int i;
352
353 for (i = 0; i < ARRAY_SIZE(mi->blk); i++)
354 if (mi->blk[i].start != mi->blk[i].end &&
355 mi->blk[i].nid != NUMA_NO_NODE)
356 node_set(mi->blk[i].nid, *nodemask);
357}
358
359/*
Tejun Heof9c60252011-02-16 17:11:09 +0100360 * Sanity check to catch more bad NUMA configurations (they are amazingly
361 * common). Make sure the nodes cover all memory.
362 */
Tejun Heo91556232011-02-16 17:11:09 +0100363static bool __init numa_meminfo_cover_memory(const struct numa_meminfo *mi)
Tejun Heof9c60252011-02-16 17:11:09 +0100364{
365 unsigned long numaram, e820ram;
366 int i;
367
368 numaram = 0;
Tejun Heo91556232011-02-16 17:11:09 +0100369 for (i = 0; i < mi->nr_blks; i++) {
370 unsigned long s = mi->blk[i].start >> PAGE_SHIFT;
371 unsigned long e = mi->blk[i].end >> PAGE_SHIFT;
Tejun Heof9c60252011-02-16 17:11:09 +0100372 numaram += e - s;
Tejun Heo91556232011-02-16 17:11:09 +0100373 numaram -= __absent_pages_in_range(mi->blk[i].nid, s, e);
Tejun Heof9c60252011-02-16 17:11:09 +0100374 if ((long)numaram < 0)
375 numaram = 0;
376 }
377
378 e820ram = max_pfn - (memblock_x86_hole_size(0,
379 max_pfn << PAGE_SHIFT) >> PAGE_SHIFT);
380 /* We seem to lose 3 pages somewhere. Allow 1M of slack. */
381 if ((long)(e820ram - numaram) >= (1 << (20 - PAGE_SHIFT))) {
382 printk(KERN_ERR "NUMA: nodes only cover %luMB of your %luMB e820 RAM. Not used.\n",
383 (numaram << PAGE_SHIFT) >> 20,
384 (e820ram << PAGE_SHIFT) >> 20);
Tejun Heo91556232011-02-16 17:11:09 +0100385 return false;
Tejun Heof9c60252011-02-16 17:11:09 +0100386 }
Tejun Heo91556232011-02-16 17:11:09 +0100387 return true;
Tejun Heof9c60252011-02-16 17:11:09 +0100388}
389
390static int __init numa_register_memblks(struct numa_meminfo *mi)
391{
Tejun Heo91556232011-02-16 17:11:09 +0100392 int i, j, nid;
Tejun Heof9c60252011-02-16 17:11:09 +0100393
394 /* Account for nodes with cpus and no memory */
Tejun Heo4697bdc2011-02-16 17:11:09 +0100395 node_possible_map = numa_nodes_parsed;
396 numa_nodemask_from_meminfo(&node_possible_map, mi);
Tejun Heof9c60252011-02-16 17:11:09 +0100397 if (WARN_ON(nodes_empty(node_possible_map)))
398 return -EINVAL;
399
Tejun Heo97e7b782011-02-16 17:11:08 +0100400 memnode_shift = compute_hash_shift(mi);
Tejun Heoef396ec2011-02-16 17:11:07 +0100401 if (memnode_shift < 0) {
402 printk(KERN_ERR "NUMA: No NUMA node hash function found. Contact maintainer\n");
403 return -EINVAL;
404 }
405
Tejun Heo97e7b782011-02-16 17:11:08 +0100406 for (i = 0; i < mi->nr_blks; i++)
407 memblock_x86_register_active_regions(mi->blk[i].nid,
408 mi->blk[i].start >> PAGE_SHIFT,
409 mi->blk[i].end >> PAGE_SHIFT);
Tejun Heofd0435d2011-02-16 17:11:08 +0100410
411 /* for out of order entries */
412 sort_node_map();
Tejun Heo91556232011-02-16 17:11:09 +0100413 if (!numa_meminfo_cover_memory(mi))
Tejun Heofd0435d2011-02-16 17:11:08 +0100414 return -EINVAL;
415
416 init_memory_mapping_high();
417
Tejun Heofd0435d2011-02-16 17:11:08 +0100418 /*
Tejun Heo91556232011-02-16 17:11:09 +0100419 * Finally register nodes. Do it twice in case setup_node_bootmem
420 * missed one due to missing bootmem.
Tejun Heofd0435d2011-02-16 17:11:08 +0100421 */
Tejun Heo91556232011-02-16 17:11:09 +0100422 for (i = 0; i < 2; i++) {
423 for_each_node_mask(nid, node_possible_map) {
424 u64 start = (u64)max_pfn << PAGE_SHIFT;
425 u64 end = 0;
426
427 if (node_online(nid))
428 continue;
429
430 for (j = 0; j < mi->nr_blks; j++) {
431 if (nid != mi->blk[j].nid)
432 continue;
433 start = min(mi->blk[j].start, start);
434 end = max(mi->blk[j].end, end);
435 }
436
437 if (start < end)
438 setup_node_bootmem(nid, start, end);
439 }
440 }
Tejun Heofd0435d2011-02-16 17:11:08 +0100441
Tejun Heoef396ec2011-02-16 17:11:07 +0100442 return 0;
443}
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445#ifdef CONFIG_NUMA_EMU
Rohit Seth53fee042007-02-13 13:26:22 +0100446/* Numa emulation */
David Rientjesadc19382009-09-25 15:20:09 -0700447static struct bootnode nodes[MAX_NUMNODES] __initdata;
David Rientjesc1c34432010-12-22 17:23:54 -0800448static struct bootnode physnodes[MAX_NUMNODES] __cpuinitdata;
Thomas Gleixner864fc312008-05-12 15:43:36 +0200449static char *cmdline __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Jan Beulich90321602011-01-19 08:57:21 +0000451void __init numa_emu_cmdline(char *str)
452{
453 cmdline = str;
454}
455
Tejun Heoa844ef42011-02-16 17:11:09 +0100456int __init find_node_by_addr(unsigned long addr)
457{
Tejun Heo91556232011-02-16 17:11:09 +0100458 const struct numa_meminfo *mi = &numa_meminfo;
Tejun Heoa844ef42011-02-16 17:11:09 +0100459 int i;
460
Tejun Heo91556232011-02-16 17:11:09 +0100461 for (i = 0; i < mi->nr_blks; i++) {
Tejun Heoa844ef42011-02-16 17:11:09 +0100462 /*
463 * Find the real node that this emulated node appears on. For
464 * the sake of simplicity, we only use a real node's starting
465 * address to determine which emulated node it appears on.
466 */
Tejun Heo91556232011-02-16 17:11:09 +0100467 if (addr >= mi->blk[i].start && addr < mi->blk[i].end)
468 return mi->blk[i].nid;
Tejun Heoa844ef42011-02-16 17:11:09 +0100469 }
Tejun Heo91556232011-02-16 17:11:09 +0100470 return NUMA_NO_NODE;
Tejun Heoa844ef42011-02-16 17:11:09 +0100471}
472
Tejun Heo190955482011-02-16 12:13:07 +0100473static int __init setup_physnodes(unsigned long start, unsigned long end)
David Rientjesadc19382009-09-25 15:20:09 -0700474{
Tejun Heo91556232011-02-16 17:11:09 +0100475 const struct numa_meminfo *mi = &numa_meminfo;
David Rientjesadc19382009-09-25 15:20:09 -0700476 int ret = 0;
477 int i;
478
David Rientjesc1c34432010-12-22 17:23:54 -0800479 memset(physnodes, 0, sizeof(physnodes));
Tejun Heo190955482011-02-16 12:13:07 +0100480
Tejun Heo91556232011-02-16 17:11:09 +0100481 for (i = 0; i < mi->nr_blks; i++) {
482 int nid = mi->blk[i].nid;
483
484 if (physnodes[nid].start == physnodes[nid].end) {
485 physnodes[nid].start = mi->blk[i].start;
486 physnodes[nid].end = mi->blk[i].end;
487 } else {
488 physnodes[nid].start = min(physnodes[nid].start,
489 mi->blk[i].start);
490 physnodes[nid].end = max(physnodes[nid].end,
491 mi->blk[i].end);
492 }
Tejun Heo190955482011-02-16 12:13:07 +0100493 }
494
David Rientjesadc19382009-09-25 15:20:09 -0700495 /*
496 * Basic sanity checking on the physical node map: there may be errors
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200497 * if the SRAT or AMD code incorrectly reported the topology or the mem=
David Rientjesadc19382009-09-25 15:20:09 -0700498 * kernel parameter is used.
499 */
David Rientjesa387e952010-12-22 17:23:56 -0800500 for (i = 0; i < MAX_NUMNODES; i++) {
David Rientjesadc19382009-09-25 15:20:09 -0700501 if (physnodes[i].start == physnodes[i].end)
502 continue;
503 if (physnodes[i].start > end) {
504 physnodes[i].end = physnodes[i].start;
505 continue;
506 }
507 if (physnodes[i].end < start) {
508 physnodes[i].start = physnodes[i].end;
509 continue;
510 }
511 if (physnodes[i].start < start)
512 physnodes[i].start = start;
513 if (physnodes[i].end > end)
514 physnodes[i].end = end;
David Rientjesadc19382009-09-25 15:20:09 -0700515 ret++;
516 }
517
518 /*
519 * If no physical topology was detected, a single node is faked to cover
520 * the entire address space.
521 */
522 if (!ret) {
523 physnodes[ret].start = start;
524 physnodes[ret].end = end;
525 ret = 1;
526 }
527 return ret;
528}
529
David Rientjesf51bf302010-12-22 17:23:51 -0800530static void __init fake_physnodes(int acpi, int amd, int nr_nodes)
531{
532 int i;
533
534 BUG_ON(acpi && amd);
535#ifdef CONFIG_ACPI_NUMA
536 if (acpi)
537 acpi_fake_nodes(nodes, nr_nodes);
538#endif
539#ifdef CONFIG_AMD_NUMA
540 if (amd)
541 amd_fake_nodes(nodes, nr_nodes);
542#endif
543 if (!acpi && !amd)
544 for (i = 0; i < nr_cpu_ids; i++)
545 numa_set_node(i, 0);
546}
547
Rohit Seth53fee042007-02-13 13:26:22 +0100548/*
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100549 * Setups up nid to range from addr to addr + size. If the end
550 * boundary is greater than max_addr, then max_addr is used instead.
551 * The return value is 0 if there is additional memory left for
552 * allocation past addr and -1 otherwise. addr is adjusted to be at
553 * the end of the node.
Rohit Seth53fee042007-02-13 13:26:22 +0100554 */
David Rientjesadc19382009-09-25 15:20:09 -0700555static int __init setup_node_range(int nid, u64 *addr, u64 size, u64 max_addr)
Rohit Seth53fee042007-02-13 13:26:22 +0100556{
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200557 int ret = 0;
558 nodes[nid].start = *addr;
559 *addr += size;
560 if (*addr >= max_addr) {
561 *addr = max_addr;
562 ret = -1;
563 }
564 nodes[nid].end = *addr;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200565 node_set(nid, node_possible_map);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200566 printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
567 nodes[nid].start, nodes[nid].end,
568 (nodes[nid].end - nodes[nid].start) >> 20);
569 return ret;
Rohit Seth53fee042007-02-13 13:26:22 +0100570}
571
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200572/*
David Rientjesadc19382009-09-25 15:20:09 -0700573 * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
574 * to max_addr. The return value is the number of nodes allocated.
575 */
David Rientjesc1c34432010-12-22 17:23:54 -0800576static int __init split_nodes_interleave(u64 addr, u64 max_addr, int nr_nodes)
David Rientjesadc19382009-09-25 15:20:09 -0700577{
578 nodemask_t physnode_mask = NODE_MASK_NONE;
579 u64 size;
580 int big;
581 int ret = 0;
582 int i;
583
584 if (nr_nodes <= 0)
585 return -1;
586 if (nr_nodes > MAX_NUMNODES) {
587 pr_info("numa=fake=%d too large, reducing to %d\n",
588 nr_nodes, MAX_NUMNODES);
589 nr_nodes = MAX_NUMNODES;
590 }
591
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700592 size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) / nr_nodes;
David Rientjesadc19382009-09-25 15:20:09 -0700593 /*
594 * Calculate the number of big nodes that can be allocated as a result
595 * of consolidating the remainder.
596 */
David Rientjes68fd1112010-02-15 13:43:25 -0800597 big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
David Rientjesadc19382009-09-25 15:20:09 -0700598 FAKE_NODE_MIN_SIZE;
599
600 size &= FAKE_NODE_MIN_HASH_MASK;
601 if (!size) {
602 pr_err("Not enough memory for each node. "
603 "NUMA emulation disabled.\n");
604 return -1;
605 }
606
David Rientjesc1c34432010-12-22 17:23:54 -0800607 for (i = 0; i < MAX_NUMNODES; i++)
David Rientjesadc19382009-09-25 15:20:09 -0700608 if (physnodes[i].start != physnodes[i].end)
609 node_set(i, physnode_mask);
610
611 /*
612 * Continue to fill physical nodes with fake nodes until there is no
613 * memory left on any of them.
614 */
615 while (nodes_weight(physnode_mask)) {
616 for_each_node_mask(i, physnode_mask) {
617 u64 end = physnodes[i].start + size;
618 u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
619
620 if (ret < big)
621 end += FAKE_NODE_MIN_SIZE;
622
623 /*
624 * Continue to add memory to this fake node if its
625 * non-reserved memory is less than the per-node size.
626 */
627 while (end - physnodes[i].start -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700628 memblock_x86_hole_size(physnodes[i].start, end) < size) {
David Rientjesadc19382009-09-25 15:20:09 -0700629 end += FAKE_NODE_MIN_SIZE;
630 if (end > physnodes[i].end) {
631 end = physnodes[i].end;
632 break;
633 }
634 }
635
636 /*
637 * If there won't be at least FAKE_NODE_MIN_SIZE of
638 * non-reserved memory in ZONE_DMA32 for the next node,
639 * this one must extend to the boundary.
640 */
641 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700642 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjesadc19382009-09-25 15:20:09 -0700643 end = dma32_end;
644
645 /*
646 * If there won't be enough non-reserved memory for the
647 * next node, this one must extend to the end of the
648 * physical node.
649 */
650 if (physnodes[i].end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700651 memblock_x86_hole_size(end, physnodes[i].end) < size)
David Rientjesadc19382009-09-25 15:20:09 -0700652 end = physnodes[i].end;
653
654 /*
655 * Avoid allocating more nodes than requested, which can
656 * happen as a result of rounding down each node's size
657 * to FAKE_NODE_MIN_SIZE.
658 */
659 if (nodes_weight(physnode_mask) + ret >= nr_nodes)
660 end = physnodes[i].end;
661
662 if (setup_node_range(ret++, &physnodes[i].start,
663 end - physnodes[i].start,
664 physnodes[i].end) < 0)
665 node_clear(i, physnode_mask);
666 }
667 }
668 return ret;
669}
670
671/*
David Rientjes8df5bb342010-02-15 13:43:30 -0800672 * Returns the end address of a node so that there is at least `size' amount of
673 * non-reserved memory or `max_addr' is reached.
674 */
675static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
676{
677 u64 end = start + size;
678
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700679 while (end - start - memblock_x86_hole_size(start, end) < size) {
David Rientjes8df5bb342010-02-15 13:43:30 -0800680 end += FAKE_NODE_MIN_SIZE;
681 if (end > max_addr) {
682 end = max_addr;
683 break;
684 }
685 }
686 return end;
687}
688
689/*
690 * Sets up fake nodes of `size' interleaved over physical nodes ranging from
691 * `addr' to `max_addr'. The return value is the number of nodes allocated.
692 */
693static int __init split_nodes_size_interleave(u64 addr, u64 max_addr, u64 size)
694{
695 nodemask_t physnode_mask = NODE_MASK_NONE;
696 u64 min_size;
697 int ret = 0;
698 int i;
699
700 if (!size)
701 return -1;
702 /*
703 * The limit on emulated nodes is MAX_NUMNODES, so the size per node is
704 * increased accordingly if the requested size is too small. This
705 * creates a uniform distribution of node sizes across the entire
706 * machine (but not necessarily over physical nodes).
707 */
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700708 min_size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) /
David Rientjes8df5bb342010-02-15 13:43:30 -0800709 MAX_NUMNODES;
710 min_size = max(min_size, FAKE_NODE_MIN_SIZE);
711 if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size)
712 min_size = (min_size + FAKE_NODE_MIN_SIZE) &
713 FAKE_NODE_MIN_HASH_MASK;
714 if (size < min_size) {
715 pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
716 size >> 20, min_size >> 20);
717 size = min_size;
718 }
719 size &= FAKE_NODE_MIN_HASH_MASK;
720
721 for (i = 0; i < MAX_NUMNODES; i++)
722 if (physnodes[i].start != physnodes[i].end)
723 node_set(i, physnode_mask);
724 /*
725 * Fill physical nodes with fake nodes of size until there is no memory
726 * left on any of them.
727 */
728 while (nodes_weight(physnode_mask)) {
729 for_each_node_mask(i, physnode_mask) {
730 u64 dma32_end = MAX_DMA32_PFN << PAGE_SHIFT;
731 u64 end;
732
733 end = find_end_of_node(physnodes[i].start,
734 physnodes[i].end, size);
735 /*
736 * If there won't be at least FAKE_NODE_MIN_SIZE of
737 * non-reserved memory in ZONE_DMA32 for the next node,
738 * this one must extend to the boundary.
739 */
740 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700741 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjes8df5bb342010-02-15 13:43:30 -0800742 end = dma32_end;
743
744 /*
745 * If there won't be enough non-reserved memory for the
746 * next node, this one must extend to the end of the
747 * physical node.
748 */
749 if (physnodes[i].end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700750 memblock_x86_hole_size(end, physnodes[i].end) < size)
David Rientjes8df5bb342010-02-15 13:43:30 -0800751 end = physnodes[i].end;
752
753 /*
754 * Setup the fake node that will be allocated as bootmem
755 * later. If setup_node_range() returns non-zero, there
756 * is no more memory available on this physical node.
757 */
758 if (setup_node_range(ret++, &physnodes[i].start,
759 end - physnodes[i].start,
760 physnodes[i].end) < 0)
761 node_clear(i, physnode_mask);
762 }
763 }
764 return ret;
765}
766
767/*
Thomas Gleixner886533a2008-05-12 15:43:36 +0200768 * Sets up the system RAM area from start_pfn to last_pfn according to the
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200769 * numa=fake command-line option.
770 */
David Rientjesadc19382009-09-25 15:20:09 -0700771static int __init numa_emulation(unsigned long start_pfn,
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200772 unsigned long last_pfn, int acpi, int amd)
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200773{
Tejun Heo97e7b782011-02-16 17:11:08 +0100774 static struct numa_meminfo ei __initdata;
David Rientjesca2107c2010-02-15 13:43:33 -0800775 u64 addr = start_pfn << PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200776 u64 max_addr = last_pfn << PAGE_SHIFT;
David Rientjesca2107c2010-02-15 13:43:33 -0800777 int num_nodes;
778 int i;
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200779
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200780 /*
David Rientjes8df5bb342010-02-15 13:43:30 -0800781 * If the numa=fake command-line contains a 'M' or 'G', it represents
David Rientjesca2107c2010-02-15 13:43:33 -0800782 * the fixed node size. Otherwise, if it is just a single number N,
783 * split the system RAM into N fake nodes.
David Rientjes8df5bb342010-02-15 13:43:30 -0800784 */
785 if (strchr(cmdline, 'M') || strchr(cmdline, 'G')) {
David Rientjesca2107c2010-02-15 13:43:33 -0800786 u64 size;
787
David Rientjes8df5bb342010-02-15 13:43:30 -0800788 size = memparse(cmdline, &cmdline);
789 num_nodes = split_nodes_size_interleave(addr, max_addr, size);
David Rientjesca2107c2010-02-15 13:43:33 -0800790 } else {
791 unsigned long n;
792
793 n = simple_strtoul(cmdline, NULL, 0);
David Rientjesc1c34432010-12-22 17:23:54 -0800794 num_nodes = split_nodes_interleave(addr, max_addr, n);
David Rientjes8df5bb342010-02-15 13:43:30 -0800795 }
796
David Rientjesca2107c2010-02-15 13:43:33 -0800797 if (num_nodes < 0)
798 return num_nodes;
Tejun Heo8968dab2011-02-16 17:11:08 +0100799
Tejun Heo97e7b782011-02-16 17:11:08 +0100800 ei.nr_blks = num_nodes;
801 for (i = 0; i < ei.nr_blks; i++) {
802 ei.blk[i].start = nodes[i].start;
803 ei.blk[i].end = nodes[i].end;
804 ei.blk[i].nid = i;
805 }
Tejun Heo8968dab2011-02-16 17:11:08 +0100806
Tejun Heo97e7b782011-02-16 17:11:08 +0100807 memnode_shift = compute_hash_shift(&ei);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200808 if (memnode_shift < 0) {
809 memnode_shift = 0;
810 printk(KERN_ERR "No NUMA hash function found. NUMA emulation "
811 "disabled.\n");
812 return -1;
813 }
814
815 /*
David Rientjesadc19382009-09-25 15:20:09 -0700816 * We need to vacate all active ranges that may have been registered for
817 * the e820 memory map.
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200818 */
819 remove_all_active_ranges();
Yinghai Lu1411e0e2010-12-27 16:48:17 -0800820 for_each_node_mask(i, node_possible_map)
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700821 memblock_x86_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
Mel Gorman5cb248a2006-09-27 01:49:52 -0700822 nodes[i].end >> PAGE_SHIFT);
Yinghai Lu1411e0e2010-12-27 16:48:17 -0800823 init_memory_mapping_high();
824 for_each_node_mask(i, node_possible_map)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100825 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
Tejun Heo190955482011-02-16 12:13:07 +0100826 setup_physnodes(addr, max_addr);
David Rientjesf51bf302010-12-22 17:23:51 -0800827 fake_physnodes(acpi, amd, num_nodes);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100828 numa_init_array();
829 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200831#endif /* CONFIG_NUMA_EMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Tejun Heoffe77a42011-02-16 12:13:06 +0100833static int dummy_numa_init(void)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100834{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 printk(KERN_INFO "%s\n",
836 numa_off ? "NUMA turned off" : "No NUMA configuration found");
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100837 printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
Tejun Heo86ef4db2011-02-16 12:13:06 +0100838 0LU, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100839
Tejun Heo92d4a432011-02-16 17:11:09 +0100840 node_set(0, numa_nodes_parsed);
Tejun Heo43a662f2011-02-16 17:11:08 +0100841 numa_add_memblk(0, 0, (u64)max_pfn << PAGE_SHIFT);
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100842
843 return 0;
844}
845
Tejun Heoffe77a42011-02-16 12:13:06 +0100846void __init initmem_init(void)
847{
848 int (*numa_init[])(void) = { [2] = dummy_numa_init };
Tejun Heoffe77a42011-02-16 12:13:06 +0100849 int i, j;
850
851 if (!numa_off) {
852#ifdef CONFIG_ACPI_NUMA
853 numa_init[0] = x86_acpi_numa_init;
Tejun Heoffe77a42011-02-16 12:13:06 +0100854#endif
855#ifdef CONFIG_AMD_NUMA
856 numa_init[1] = amd_numa_init;
Tejun Heoffe77a42011-02-16 12:13:06 +0100857#endif
858 }
859
860 for (i = 0; i < ARRAY_SIZE(numa_init); i++) {
861 if (!numa_init[i])
862 continue;
863
864 for (j = 0; j < MAX_LOCAL_APIC; j++)
865 set_apicid_to_node(j, NUMA_NO_NODE);
866
Tejun Heo92d4a432011-02-16 17:11:09 +0100867 nodes_clear(numa_nodes_parsed);
Tejun Heoffe77a42011-02-16 12:13:06 +0100868 nodes_clear(node_possible_map);
869 nodes_clear(node_online_map);
Tejun Heo97e7b782011-02-16 17:11:08 +0100870 memset(&numa_meminfo, 0, sizeof(numa_meminfo));
Tejun Heofd0435d2011-02-16 17:11:08 +0100871 remove_all_active_ranges();
Tejun Heoffe77a42011-02-16 12:13:06 +0100872
873 if (numa_init[i]() < 0)
874 continue;
Tejun Heo206e4202011-02-16 12:13:07 +0100875
Tejun Heo56e827f2011-02-16 17:11:09 +0100876 if (numa_cleanup_meminfo(&numa_meminfo) < 0)
877 continue;
Tejun Heoffe77a42011-02-16 12:13:06 +0100878#ifdef CONFIG_NUMA_EMU
Tejun Heo190955482011-02-16 12:13:07 +0100879 setup_physnodes(0, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100880 if (cmdline && !numa_emulation(0, max_pfn, i == 0, i == 1))
881 return;
Tejun Heo190955482011-02-16 12:13:07 +0100882 setup_physnodes(0, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100883 nodes_clear(node_possible_map);
884 nodes_clear(node_online_map);
885#endif
Tejun Heof9c60252011-02-16 17:11:09 +0100886 if (numa_register_memblks(&numa_meminfo) < 0)
Tejun Heo43a662f2011-02-16 17:11:08 +0100887 continue;
888
Tejun Heofd0435d2011-02-16 17:11:08 +0100889 for (j = 0; j < nr_cpu_ids; j++) {
890 int nid = early_cpu_to_node(j);
891
892 if (nid == NUMA_NO_NODE)
893 continue;
894 if (!node_online(nid))
895 numa_clear_node(j);
896 }
897 numa_init_array();
898 return;
Tejun Heoffe77a42011-02-16 12:13:06 +0100899 }
900 BUG();
Andi Kleen69d81fc2005-11-05 17:25:53 +0100901}
902
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100903unsigned long __init numa_free_all_bootmem(void)
904{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 unsigned long pages = 0;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100906 int i;
907
908 for_each_online_node(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 pages += free_all_bootmem_node(NODE_DATA(i));
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100910
Yinghai Lu08677212010-02-10 01:20:20 -0800911 pages += free_all_memory_core_early(MAX_NUMNODES);
Yinghai Lu08677212010-02-10 01:20:20 -0800912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return pages;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100914}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100916int __cpuinit numa_cpu_node(int cpu)
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800917{
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100918 int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800919
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100920 if (apicid != BAD_APICID)
921 return __apicid_to_node[apicid];
922 return NUMA_NO_NODE;
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800923}
924
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100925/*
Tejun Heode2d9442011-01-23 14:37:41 +0100926 * UGLINESS AHEAD: Currently, CONFIG_NUMA_EMU is 64bit only and makes use
927 * of 64bit specific data structures. The distinction is artificial and
928 * should be removed. numa_{add|remove}_cpu() are implemented in numa.c
929 * for both 32 and 64bit when CONFIG_NUMA_EMU is disabled but here when
930 * enabled.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100931 *
Tejun Heode2d9442011-01-23 14:37:41 +0100932 * NUMA emulation is planned to be made generic and the following and other
933 * related code should be moved to numa.c.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100934 */
Tejun Heode2d9442011-01-23 14:37:41 +0100935#ifdef CONFIG_NUMA_EMU
936# ifndef CONFIG_DEBUG_PER_CPU_MAPS
David Rientjesc1c34432010-12-22 17:23:54 -0800937void __cpuinit numa_add_cpu(int cpu)
938{
939 unsigned long addr;
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100940 int physnid, nid;
David Rientjesc1c34432010-12-22 17:23:54 -0800941
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100942 nid = numa_cpu_node(cpu);
David Rientjesc1c34432010-12-22 17:23:54 -0800943 if (nid == NUMA_NO_NODE)
944 nid = early_cpu_to_node(cpu);
945 BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
946
947 /*
948 * Use the starting address of the emulated node to find which physical
949 * node it is allocated on.
950 */
951 addr = node_start_pfn(nid) << PAGE_SHIFT;
952 for (physnid = 0; physnid < MAX_NUMNODES; physnid++)
953 if (addr >= physnodes[physnid].start &&
954 addr < physnodes[physnid].end)
955 break;
956
957 /*
958 * Map the cpu to each emulated node that is allocated on the physical
959 * node of the cpu's apic id.
960 */
961 for_each_online_node(nid) {
962 addr = node_start_pfn(nid) << PAGE_SHIFT;
963 if (addr >= physnodes[physnid].start &&
964 addr < physnodes[physnid].end)
965 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
966 }
967}
968
969void __cpuinit numa_remove_cpu(int cpu)
970{
971 int i;
972
973 for_each_online_node(i)
974 cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
975}
Tejun Heode2d9442011-01-23 14:37:41 +0100976# else /* !CONFIG_DEBUG_PER_CPU_MAPS */
David Rientjesd906f0e2010-12-30 10:54:16 -0800977static void __cpuinit numa_set_cpumask(int cpu, int enable)
978{
979 int node = early_cpu_to_node(cpu);
980 struct cpumask *mask;
David Rientjesc1c34432010-12-22 17:23:54 -0800981 int i;
Brian Gerst6470aff2009-01-27 12:56:47 +0900982
David Rientjes14392fd2011-02-07 14:08:53 -0800983 if (node == NUMA_NO_NODE) {
984 /* early_cpu_to_node() already emits a warning and trace */
985 return;
986 }
David Rientjesc1c34432010-12-22 17:23:54 -0800987 for_each_online_node(i) {
988 unsigned long addr;
989
990 addr = node_start_pfn(i) << PAGE_SHIFT;
991 if (addr < physnodes[node].start ||
992 addr >= physnodes[node].end)
993 continue;
David Rientjesd906f0e2010-12-30 10:54:16 -0800994 mask = debug_cpumask_set_cpu(cpu, enable);
995 if (!mask)
David Rientjesc1c34432010-12-22 17:23:54 -0800996 return;
David Rientjesc1c34432010-12-22 17:23:54 -0800997
998 if (enable)
999 cpumask_set_cpu(cpu, mask);
1000 else
1001 cpumask_clear_cpu(cpu, mask);
Brian Gerst6470aff2009-01-27 12:56:47 +09001002 }
Brian Gerst6470aff2009-01-27 12:56:47 +09001003}
1004
1005void __cpuinit numa_add_cpu(int cpu)
1006{
1007 numa_set_cpumask(cpu, 1);
1008}
1009
1010void __cpuinit numa_remove_cpu(int cpu)
1011{
1012 numa_set_cpumask(cpu, 0);
1013}
Tejun Heode2d9442011-01-23 14:37:41 +01001014# endif /* !CONFIG_DEBUG_PER_CPU_MAPS */
1015#endif /* CONFIG_NUMA_EMU */