blob: 681bc0d59db554ce3861f7571eaea8ca45dcc509 [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 +0100192int __init numa_add_memblk(int nid, u64 start, u64 end)
193{
Tejun Heo97e7b782011-02-16 17:11:08 +0100194 struct numa_meminfo *mi = &numa_meminfo;
Tejun Heoef396ec2011-02-16 17:11:07 +0100195
Tejun Heo56e827f2011-02-16 17:11:09 +0100196 /* ignore zero length blks */
197 if (start == end)
198 return 0;
199
200 /* whine about and ignore invalid blks */
201 if (start > end || nid < 0 || nid >= MAX_NUMNODES) {
202 pr_warning("NUMA: Warning: invalid memblk node %d (%Lx-%Lx)\n",
203 nid, start, end);
204 return 0;
205 }
206
207 if (mi->nr_blks >= NR_NODE_MEMBLKS) {
208 pr_err("NUMA: too many memblk ranges\n");
Tejun Heoef396ec2011-02-16 17:11:07 +0100209 return -EINVAL;
210 }
211
Tejun Heo97e7b782011-02-16 17:11:08 +0100212 mi->blk[mi->nr_blks].start = start;
213 mi->blk[mi->nr_blks].end = end;
214 mi->blk[mi->nr_blks].nid = nid;
215 mi->nr_blks++;
Tejun Heoef396ec2011-02-16 17:11:07 +0100216 return 0;
217}
218
Tejun Heo2e756be2011-02-16 17:11:09 +0100219static void __init numa_remove_memblk_from(int idx, struct numa_meminfo *mi)
220{
221 mi->nr_blks--;
222 memmove(&mi->blk[idx], &mi->blk[idx + 1],
223 (mi->nr_blks - idx) * sizeof(mi->blk[0]));
224}
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226/* Initialize bootmem allocator for a node */
Yinghai Lu7c437692009-05-15 13:59:37 -0700227void __init
228setup_node_bootmem(int nodeid, unsigned long start, unsigned long end)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100229{
Yinghai Lu08677212010-02-10 01:20:20 -0800230 unsigned long start_pfn, last_pfn, nodedata_phys;
Yinghai Lu7c437692009-05-15 13:59:37 -0700231 const int pgdat_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
Yinghai Lu1a27fc02008-03-18 12:52:37 -0700232 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Yinghai Lu4c31e922009-04-22 14:19:27 -0700234 if (!end)
235 return;
236
Yinghai Lu7c437692009-05-15 13:59:37 -0700237 /*
238 * Don't confuse VM with a node that doesn't have the
239 * minimum amount of memory:
240 */
241 if (end && (end - start) < NODE_MIN_SIZE)
242 return;
243
Joerg Roedelbe3e89e2008-07-25 16:48:58 +0200244 start = roundup(start, ZONE_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Yinghai Lu08677212010-02-10 01:20:20 -0800246 printk(KERN_INFO "Initmem setup node %d %016lx-%016lx\n", nodeid,
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100247 start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 start_pfn = start >> PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200250 last_pfn = end >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Yinghai Lu24a5da72008-02-01 17:49:41 +0100252 node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
253 SMP_CACHE_BYTES);
Andi Kleena8062232006-04-07 19:49:21 +0200254 if (node_data[nodeid] == NULL)
255 return;
256 nodedata_phys = __pa(node_data[nodeid]);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700257 memblock_x86_reserve_range(nodedata_phys, nodedata_phys + pgdat_size, "NODE_DATA");
Yinghai Lu6118f762008-02-04 16:47:56 +0100258 printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
259 nodedata_phys + pgdat_size - 1);
Yinghai Lu1842f902010-02-10 01:20:15 -0800260 nid = phys_to_nid(nodedata_phys);
261 if (nid != nodeid)
262 printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nodeid, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
Yinghai Lu08677212010-02-10 01:20:20 -0800265 NODE_DATA(nodeid)->node_id = nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 NODE_DATA(nodeid)->node_start_pfn = start_pfn;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200267 NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 node_set_online(nodeid);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100270}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Tejun Heof9c60252011-02-16 17:11:09 +0100272static int __init numa_cleanup_meminfo(struct numa_meminfo *mi)
Tejun Heofd0435d2011-02-16 17:11:08 +0100273{
Tejun Heo56e827f2011-02-16 17:11:09 +0100274 const u64 low = 0;
275 const u64 high = (u64)max_pfn << PAGE_SHIFT;
Tejun Heo2e756be2011-02-16 17:11:09 +0100276 int i, j, k;
Tejun Heoef396ec2011-02-16 17:11:07 +0100277
Tejun Heo2e756be2011-02-16 17:11:09 +0100278 for (i = 0; i < mi->nr_blks; i++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100279 struct numa_memblk *bi = &mi->blk[i];
Tejun Heoef396ec2011-02-16 17:11:07 +0100280
Tejun Heo56e827f2011-02-16 17:11:09 +0100281 /* make sure all blocks are inside the limits */
282 bi->start = max(bi->start, low);
283 bi->end = min(bi->end, high);
284
285 /* and there's no empty block */
286 if (bi->start == bi->end) {
287 numa_remove_memblk_from(i--, mi);
288 continue;
289 }
290
Tejun Heo2e756be2011-02-16 17:11:09 +0100291 for (j = i + 1; j < mi->nr_blks; j++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100292 struct numa_memblk *bj = &mi->blk[j];
Tejun Heoef396ec2011-02-16 17:11:07 +0100293 unsigned long start, end;
294
Tejun Heo2e756be2011-02-16 17:11:09 +0100295 /*
Tejun Heo56e827f2011-02-16 17:11:09 +0100296 * See whether there are overlapping blocks. Whine
297 * about but allow overlaps of the same nid. They
298 * will be merged below.
299 */
300 if (bi->end > bj->start && bi->start < bj->end) {
301 if (bi->nid != bj->nid) {
302 pr_err("NUMA: node %d (%Lx-%Lx) overlaps with node %d (%Lx-%Lx)\n",
303 bi->nid, bi->start, bi->end,
304 bj->nid, bj->start, bj->end);
305 return -EINVAL;
306 }
307 pr_warning("NUMA: Warning: node %d (%Lx-%Lx) overlaps with itself (%Lx-%Lx)\n",
308 bi->nid, bi->start, bi->end,
309 bj->start, bj->end);
310 }
311
312 /*
Tejun Heo2e756be2011-02-16 17:11:09 +0100313 * Join together blocks on the same node, holes
314 * between which don't overlap with memory on other
315 * nodes.
316 */
Tejun Heo97e7b782011-02-16 17:11:08 +0100317 if (bi->nid != bj->nid)
Tejun Heoef396ec2011-02-16 17:11:07 +0100318 continue;
Tejun Heo56e827f2011-02-16 17:11:09 +0100319 start = max(min(bi->start, bj->start), low);
320 end = min(max(bi->end, bj->end), high);
Tejun Heo2e756be2011-02-16 17:11:09 +0100321 for (k = 0; k < mi->nr_blks; k++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100322 struct numa_memblk *bk = &mi->blk[k];
323
324 if (bi->nid == bk->nid)
Tejun Heoef396ec2011-02-16 17:11:07 +0100325 continue;
Tejun Heo97e7b782011-02-16 17:11:08 +0100326 if (start < bk->end && end > bk->start)
Tejun Heoef396ec2011-02-16 17:11:07 +0100327 break;
328 }
Tejun Heo97e7b782011-02-16 17:11:08 +0100329 if (k < mi->nr_blks)
Tejun Heoef396ec2011-02-16 17:11:07 +0100330 continue;
Tejun Heoef396ec2011-02-16 17:11:07 +0100331 printk(KERN_INFO "NUMA: Node %d [%Lx,%Lx) + [%Lx,%Lx) -> [%lx,%lx)\n",
Tejun Heo97e7b782011-02-16 17:11:08 +0100332 bi->nid, bi->start, bi->end, bj->start, bj->end,
Tejun Heoef396ec2011-02-16 17:11:07 +0100333 start, end);
Tejun Heo97e7b782011-02-16 17:11:08 +0100334 bi->start = start;
335 bi->end = end;
Tejun Heo2e756be2011-02-16 17:11:09 +0100336 numa_remove_memblk_from(j--, mi);
Tejun Heoef396ec2011-02-16 17:11:07 +0100337 }
338 }
339
Tejun Heo56e827f2011-02-16 17:11:09 +0100340 for (i = mi->nr_blks; i < ARRAY_SIZE(mi->blk); i++) {
341 mi->blk[i].start = mi->blk[i].end = 0;
342 mi->blk[i].nid = NUMA_NO_NODE;
343 }
344
Tejun Heof9c60252011-02-16 17:11:09 +0100345 return 0;
346}
347
348/*
349 * Sanity check to catch more bad NUMA configurations (they are amazingly
350 * common). Make sure the nodes cover all memory.
351 */
352static int __init nodes_cover_memory(const struct bootnode *nodes)
353{
354 unsigned long numaram, e820ram;
355 int i;
356
357 numaram = 0;
358 for_each_node_mask(i, mem_nodes_parsed) {
359 unsigned long s = nodes[i].start >> PAGE_SHIFT;
360 unsigned long e = nodes[i].end >> PAGE_SHIFT;
361 numaram += e - s;
362 numaram -= __absent_pages_in_range(i, s, e);
363 if ((long)numaram < 0)
364 numaram = 0;
365 }
366
367 e820ram = max_pfn - (memblock_x86_hole_size(0,
368 max_pfn << PAGE_SHIFT) >> PAGE_SHIFT);
369 /* We seem to lose 3 pages somewhere. Allow 1M of slack. */
370 if ((long)(e820ram - numaram) >= (1 << (20 - PAGE_SHIFT))) {
371 printk(KERN_ERR "NUMA: nodes only cover %luMB of your %luMB e820 RAM. Not used.\n",
372 (numaram << PAGE_SHIFT) >> 20,
373 (e820ram << PAGE_SHIFT) >> 20);
374 return 0;
375 }
376 return 1;
377}
378
379static int __init numa_register_memblks(struct numa_meminfo *mi)
380{
381 int i;
382
383 /* Account for nodes with cpus and no memory */
384 nodes_or(node_possible_map, mem_nodes_parsed, cpu_nodes_parsed);
385 if (WARN_ON(nodes_empty(node_possible_map)))
386 return -EINVAL;
387
Tejun Heo97e7b782011-02-16 17:11:08 +0100388 memnode_shift = compute_hash_shift(mi);
Tejun Heoef396ec2011-02-16 17:11:07 +0100389 if (memnode_shift < 0) {
390 printk(KERN_ERR "NUMA: No NUMA node hash function found. Contact maintainer\n");
391 return -EINVAL;
392 }
393
Tejun Heo97e7b782011-02-16 17:11:08 +0100394 for (i = 0; i < mi->nr_blks; i++)
395 memblock_x86_register_active_regions(mi->blk[i].nid,
396 mi->blk[i].start >> PAGE_SHIFT,
397 mi->blk[i].end >> PAGE_SHIFT);
Tejun Heofd0435d2011-02-16 17:11:08 +0100398
399 /* for out of order entries */
400 sort_node_map();
401 if (!nodes_cover_memory(numa_nodes))
402 return -EINVAL;
403
404 init_memory_mapping_high();
405
406 /* Finally register nodes. */
407 for_each_node_mask(i, node_possible_map)
408 setup_node_bootmem(i, numa_nodes[i].start, numa_nodes[i].end);
409
410 /*
411 * Try again in case setup_node_bootmem missed one due to missing
412 * bootmem.
413 */
414 for_each_node_mask(i, node_possible_map)
415 if (!node_online(i))
416 setup_node_bootmem(i, numa_nodes[i].start,
417 numa_nodes[i].end);
418
Tejun Heoef396ec2011-02-16 17:11:07 +0100419 return 0;
420}
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422#ifdef CONFIG_NUMA_EMU
Rohit Seth53fee042007-02-13 13:26:22 +0100423/* Numa emulation */
David Rientjesadc19382009-09-25 15:20:09 -0700424static struct bootnode nodes[MAX_NUMNODES] __initdata;
David Rientjesc1c34432010-12-22 17:23:54 -0800425static struct bootnode physnodes[MAX_NUMNODES] __cpuinitdata;
Thomas Gleixner864fc312008-05-12 15:43:36 +0200426static char *cmdline __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Jan Beulich90321602011-01-19 08:57:21 +0000428void __init numa_emu_cmdline(char *str)
429{
430 cmdline = str;
431}
432
Tejun Heoa844ef42011-02-16 17:11:09 +0100433int __init find_node_by_addr(unsigned long addr)
434{
435 int ret = NUMA_NO_NODE;
436 int i;
437
438 for_each_node_mask(i, mem_nodes_parsed) {
439 /*
440 * Find the real node that this emulated node appears on. For
441 * the sake of simplicity, we only use a real node's starting
442 * address to determine which emulated node it appears on.
443 */
444 if (addr >= numa_nodes[i].start && addr < numa_nodes[i].end) {
445 ret = i;
446 break;
447 }
448 }
449 return ret;
450}
451
Tejun Heo190955482011-02-16 12:13:07 +0100452static int __init setup_physnodes(unsigned long start, unsigned long end)
David Rientjesadc19382009-09-25 15:20:09 -0700453{
David Rientjesadc19382009-09-25 15:20:09 -0700454 int ret = 0;
455 int i;
456
David Rientjesc1c34432010-12-22 17:23:54 -0800457 memset(physnodes, 0, sizeof(physnodes));
Tejun Heo190955482011-02-16 12:13:07 +0100458
459 for_each_node_mask(i, mem_nodes_parsed) {
460 physnodes[i].start = numa_nodes[i].start;
461 physnodes[i].end = numa_nodes[i].end;
462 }
463
David Rientjesadc19382009-09-25 15:20:09 -0700464 /*
465 * Basic sanity checking on the physical node map: there may be errors
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200466 * if the SRAT or AMD code incorrectly reported the topology or the mem=
David Rientjesadc19382009-09-25 15:20:09 -0700467 * kernel parameter is used.
468 */
David Rientjesa387e952010-12-22 17:23:56 -0800469 for (i = 0; i < MAX_NUMNODES; i++) {
David Rientjesadc19382009-09-25 15:20:09 -0700470 if (physnodes[i].start == physnodes[i].end)
471 continue;
472 if (physnodes[i].start > end) {
473 physnodes[i].end = physnodes[i].start;
474 continue;
475 }
476 if (physnodes[i].end < start) {
477 physnodes[i].start = physnodes[i].end;
478 continue;
479 }
480 if (physnodes[i].start < start)
481 physnodes[i].start = start;
482 if (physnodes[i].end > end)
483 physnodes[i].end = end;
David Rientjesadc19382009-09-25 15:20:09 -0700484 ret++;
485 }
486
487 /*
488 * If no physical topology was detected, a single node is faked to cover
489 * the entire address space.
490 */
491 if (!ret) {
492 physnodes[ret].start = start;
493 physnodes[ret].end = end;
494 ret = 1;
495 }
496 return ret;
497}
498
David Rientjesf51bf302010-12-22 17:23:51 -0800499static void __init fake_physnodes(int acpi, int amd, int nr_nodes)
500{
501 int i;
502
503 BUG_ON(acpi && amd);
504#ifdef CONFIG_ACPI_NUMA
505 if (acpi)
506 acpi_fake_nodes(nodes, nr_nodes);
507#endif
508#ifdef CONFIG_AMD_NUMA
509 if (amd)
510 amd_fake_nodes(nodes, nr_nodes);
511#endif
512 if (!acpi && !amd)
513 for (i = 0; i < nr_cpu_ids; i++)
514 numa_set_node(i, 0);
515}
516
Rohit Seth53fee042007-02-13 13:26:22 +0100517/*
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100518 * Setups up nid to range from addr to addr + size. If the end
519 * boundary is greater than max_addr, then max_addr is used instead.
520 * The return value is 0 if there is additional memory left for
521 * allocation past addr and -1 otherwise. addr is adjusted to be at
522 * the end of the node.
Rohit Seth53fee042007-02-13 13:26:22 +0100523 */
David Rientjesadc19382009-09-25 15:20:09 -0700524static int __init setup_node_range(int nid, u64 *addr, u64 size, u64 max_addr)
Rohit Seth53fee042007-02-13 13:26:22 +0100525{
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200526 int ret = 0;
527 nodes[nid].start = *addr;
528 *addr += size;
529 if (*addr >= max_addr) {
530 *addr = max_addr;
531 ret = -1;
532 }
533 nodes[nid].end = *addr;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200534 node_set(nid, node_possible_map);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200535 printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
536 nodes[nid].start, nodes[nid].end,
537 (nodes[nid].end - nodes[nid].start) >> 20);
538 return ret;
Rohit Seth53fee042007-02-13 13:26:22 +0100539}
540
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200541/*
David Rientjesadc19382009-09-25 15:20:09 -0700542 * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
543 * to max_addr. The return value is the number of nodes allocated.
544 */
David Rientjesc1c34432010-12-22 17:23:54 -0800545static int __init split_nodes_interleave(u64 addr, u64 max_addr, int nr_nodes)
David Rientjesadc19382009-09-25 15:20:09 -0700546{
547 nodemask_t physnode_mask = NODE_MASK_NONE;
548 u64 size;
549 int big;
550 int ret = 0;
551 int i;
552
553 if (nr_nodes <= 0)
554 return -1;
555 if (nr_nodes > MAX_NUMNODES) {
556 pr_info("numa=fake=%d too large, reducing to %d\n",
557 nr_nodes, MAX_NUMNODES);
558 nr_nodes = MAX_NUMNODES;
559 }
560
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700561 size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) / nr_nodes;
David Rientjesadc19382009-09-25 15:20:09 -0700562 /*
563 * Calculate the number of big nodes that can be allocated as a result
564 * of consolidating the remainder.
565 */
David Rientjes68fd1112010-02-15 13:43:25 -0800566 big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
David Rientjesadc19382009-09-25 15:20:09 -0700567 FAKE_NODE_MIN_SIZE;
568
569 size &= FAKE_NODE_MIN_HASH_MASK;
570 if (!size) {
571 pr_err("Not enough memory for each node. "
572 "NUMA emulation disabled.\n");
573 return -1;
574 }
575
David Rientjesc1c34432010-12-22 17:23:54 -0800576 for (i = 0; i < MAX_NUMNODES; i++)
David Rientjesadc19382009-09-25 15:20:09 -0700577 if (physnodes[i].start != physnodes[i].end)
578 node_set(i, physnode_mask);
579
580 /*
581 * Continue to fill physical nodes with fake nodes until there is no
582 * memory left on any of them.
583 */
584 while (nodes_weight(physnode_mask)) {
585 for_each_node_mask(i, physnode_mask) {
586 u64 end = physnodes[i].start + size;
587 u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
588
589 if (ret < big)
590 end += FAKE_NODE_MIN_SIZE;
591
592 /*
593 * Continue to add memory to this fake node if its
594 * non-reserved memory is less than the per-node size.
595 */
596 while (end - physnodes[i].start -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700597 memblock_x86_hole_size(physnodes[i].start, end) < size) {
David Rientjesadc19382009-09-25 15:20:09 -0700598 end += FAKE_NODE_MIN_SIZE;
599 if (end > physnodes[i].end) {
600 end = physnodes[i].end;
601 break;
602 }
603 }
604
605 /*
606 * If there won't be at least FAKE_NODE_MIN_SIZE of
607 * non-reserved memory in ZONE_DMA32 for the next node,
608 * this one must extend to the boundary.
609 */
610 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700611 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjesadc19382009-09-25 15:20:09 -0700612 end = dma32_end;
613
614 /*
615 * If there won't be enough non-reserved memory for the
616 * next node, this one must extend to the end of the
617 * physical node.
618 */
619 if (physnodes[i].end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700620 memblock_x86_hole_size(end, physnodes[i].end) < size)
David Rientjesadc19382009-09-25 15:20:09 -0700621 end = physnodes[i].end;
622
623 /*
624 * Avoid allocating more nodes than requested, which can
625 * happen as a result of rounding down each node's size
626 * to FAKE_NODE_MIN_SIZE.
627 */
628 if (nodes_weight(physnode_mask) + ret >= nr_nodes)
629 end = physnodes[i].end;
630
631 if (setup_node_range(ret++, &physnodes[i].start,
632 end - physnodes[i].start,
633 physnodes[i].end) < 0)
634 node_clear(i, physnode_mask);
635 }
636 }
637 return ret;
638}
639
640/*
David Rientjes8df5bb342010-02-15 13:43:30 -0800641 * Returns the end address of a node so that there is at least `size' amount of
642 * non-reserved memory or `max_addr' is reached.
643 */
644static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
645{
646 u64 end = start + size;
647
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700648 while (end - start - memblock_x86_hole_size(start, end) < size) {
David Rientjes8df5bb342010-02-15 13:43:30 -0800649 end += FAKE_NODE_MIN_SIZE;
650 if (end > max_addr) {
651 end = max_addr;
652 break;
653 }
654 }
655 return end;
656}
657
658/*
659 * Sets up fake nodes of `size' interleaved over physical nodes ranging from
660 * `addr' to `max_addr'. The return value is the number of nodes allocated.
661 */
662static int __init split_nodes_size_interleave(u64 addr, u64 max_addr, u64 size)
663{
664 nodemask_t physnode_mask = NODE_MASK_NONE;
665 u64 min_size;
666 int ret = 0;
667 int i;
668
669 if (!size)
670 return -1;
671 /*
672 * The limit on emulated nodes is MAX_NUMNODES, so the size per node is
673 * increased accordingly if the requested size is too small. This
674 * creates a uniform distribution of node sizes across the entire
675 * machine (but not necessarily over physical nodes).
676 */
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700677 min_size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) /
David Rientjes8df5bb342010-02-15 13:43:30 -0800678 MAX_NUMNODES;
679 min_size = max(min_size, FAKE_NODE_MIN_SIZE);
680 if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size)
681 min_size = (min_size + FAKE_NODE_MIN_SIZE) &
682 FAKE_NODE_MIN_HASH_MASK;
683 if (size < min_size) {
684 pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
685 size >> 20, min_size >> 20);
686 size = min_size;
687 }
688 size &= FAKE_NODE_MIN_HASH_MASK;
689
690 for (i = 0; i < MAX_NUMNODES; i++)
691 if (physnodes[i].start != physnodes[i].end)
692 node_set(i, physnode_mask);
693 /*
694 * Fill physical nodes with fake nodes of size until there is no memory
695 * left on any of them.
696 */
697 while (nodes_weight(physnode_mask)) {
698 for_each_node_mask(i, physnode_mask) {
699 u64 dma32_end = MAX_DMA32_PFN << PAGE_SHIFT;
700 u64 end;
701
702 end = find_end_of_node(physnodes[i].start,
703 physnodes[i].end, size);
704 /*
705 * If there won't be at least FAKE_NODE_MIN_SIZE of
706 * non-reserved memory in ZONE_DMA32 for the next node,
707 * this one must extend to the boundary.
708 */
709 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700710 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjes8df5bb342010-02-15 13:43:30 -0800711 end = dma32_end;
712
713 /*
714 * If there won't be enough non-reserved memory for the
715 * next node, this one must extend to the end of the
716 * physical node.
717 */
718 if (physnodes[i].end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700719 memblock_x86_hole_size(end, physnodes[i].end) < size)
David Rientjes8df5bb342010-02-15 13:43:30 -0800720 end = physnodes[i].end;
721
722 /*
723 * Setup the fake node that will be allocated as bootmem
724 * later. If setup_node_range() returns non-zero, there
725 * is no more memory available on this physical node.
726 */
727 if (setup_node_range(ret++, &physnodes[i].start,
728 end - physnodes[i].start,
729 physnodes[i].end) < 0)
730 node_clear(i, physnode_mask);
731 }
732 }
733 return ret;
734}
735
736/*
Thomas Gleixner886533a2008-05-12 15:43:36 +0200737 * Sets up the system RAM area from start_pfn to last_pfn according to the
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200738 * numa=fake command-line option.
739 */
David Rientjesadc19382009-09-25 15:20:09 -0700740static int __init numa_emulation(unsigned long start_pfn,
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200741 unsigned long last_pfn, int acpi, int amd)
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200742{
Tejun Heo97e7b782011-02-16 17:11:08 +0100743 static struct numa_meminfo ei __initdata;
David Rientjesca2107c2010-02-15 13:43:33 -0800744 u64 addr = start_pfn << PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200745 u64 max_addr = last_pfn << PAGE_SHIFT;
David Rientjesca2107c2010-02-15 13:43:33 -0800746 int num_nodes;
747 int i;
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200748
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200749 /*
David Rientjes8df5bb342010-02-15 13:43:30 -0800750 * If the numa=fake command-line contains a 'M' or 'G', it represents
David Rientjesca2107c2010-02-15 13:43:33 -0800751 * the fixed node size. Otherwise, if it is just a single number N,
752 * split the system RAM into N fake nodes.
David Rientjes8df5bb342010-02-15 13:43:30 -0800753 */
754 if (strchr(cmdline, 'M') || strchr(cmdline, 'G')) {
David Rientjesca2107c2010-02-15 13:43:33 -0800755 u64 size;
756
David Rientjes8df5bb342010-02-15 13:43:30 -0800757 size = memparse(cmdline, &cmdline);
758 num_nodes = split_nodes_size_interleave(addr, max_addr, size);
David Rientjesca2107c2010-02-15 13:43:33 -0800759 } else {
760 unsigned long n;
761
762 n = simple_strtoul(cmdline, NULL, 0);
David Rientjesc1c34432010-12-22 17:23:54 -0800763 num_nodes = split_nodes_interleave(addr, max_addr, n);
David Rientjes8df5bb342010-02-15 13:43:30 -0800764 }
765
David Rientjesca2107c2010-02-15 13:43:33 -0800766 if (num_nodes < 0)
767 return num_nodes;
Tejun Heo8968dab2011-02-16 17:11:08 +0100768
Tejun Heo97e7b782011-02-16 17:11:08 +0100769 ei.nr_blks = num_nodes;
770 for (i = 0; i < ei.nr_blks; i++) {
771 ei.blk[i].start = nodes[i].start;
772 ei.blk[i].end = nodes[i].end;
773 ei.blk[i].nid = i;
774 }
Tejun Heo8968dab2011-02-16 17:11:08 +0100775
Tejun Heo97e7b782011-02-16 17:11:08 +0100776 memnode_shift = compute_hash_shift(&ei);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200777 if (memnode_shift < 0) {
778 memnode_shift = 0;
779 printk(KERN_ERR "No NUMA hash function found. NUMA emulation "
780 "disabled.\n");
781 return -1;
782 }
783
784 /*
David Rientjesadc19382009-09-25 15:20:09 -0700785 * We need to vacate all active ranges that may have been registered for
786 * the e820 memory map.
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200787 */
788 remove_all_active_ranges();
Yinghai Lu1411e0e2010-12-27 16:48:17 -0800789 for_each_node_mask(i, node_possible_map)
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700790 memblock_x86_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
Mel Gorman5cb248a2006-09-27 01:49:52 -0700791 nodes[i].end >> PAGE_SHIFT);
Yinghai Lu1411e0e2010-12-27 16:48:17 -0800792 init_memory_mapping_high();
793 for_each_node_mask(i, node_possible_map)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100794 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
Tejun Heo190955482011-02-16 12:13:07 +0100795 setup_physnodes(addr, max_addr);
David Rientjesf51bf302010-12-22 17:23:51 -0800796 fake_physnodes(acpi, amd, num_nodes);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100797 numa_init_array();
798 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200800#endif /* CONFIG_NUMA_EMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Tejun Heoffe77a42011-02-16 12:13:06 +0100802static int dummy_numa_init(void)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100803{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 printk(KERN_INFO "%s\n",
805 numa_off ? "NUMA turned off" : "No NUMA configuration found");
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100806 printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
Tejun Heo86ef4db2011-02-16 12:13:06 +0100807 0LU, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100808
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100809 node_set(0, cpu_nodes_parsed);
810 node_set(0, mem_nodes_parsed);
Tejun Heo43a662f2011-02-16 17:11:08 +0100811 numa_add_memblk(0, 0, (u64)max_pfn << PAGE_SHIFT);
Tejun Heofd0435d2011-02-16 17:11:08 +0100812 numa_nodes[0].start = 0;
813 numa_nodes[0].end = (u64)max_pfn << PAGE_SHIFT;
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100814
815 return 0;
816}
817
Tejun Heoffe77a42011-02-16 12:13:06 +0100818void __init initmem_init(void)
819{
820 int (*numa_init[])(void) = { [2] = dummy_numa_init };
Tejun Heoffe77a42011-02-16 12:13:06 +0100821 int i, j;
822
823 if (!numa_off) {
824#ifdef CONFIG_ACPI_NUMA
825 numa_init[0] = x86_acpi_numa_init;
Tejun Heoffe77a42011-02-16 12:13:06 +0100826#endif
827#ifdef CONFIG_AMD_NUMA
828 numa_init[1] = amd_numa_init;
Tejun Heoffe77a42011-02-16 12:13:06 +0100829#endif
830 }
831
832 for (i = 0; i < ARRAY_SIZE(numa_init); i++) {
833 if (!numa_init[i])
834 continue;
835
836 for (j = 0; j < MAX_LOCAL_APIC; j++)
837 set_apicid_to_node(j, NUMA_NO_NODE);
838
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100839 nodes_clear(cpu_nodes_parsed);
840 nodes_clear(mem_nodes_parsed);
Tejun Heoffe77a42011-02-16 12:13:06 +0100841 nodes_clear(node_possible_map);
842 nodes_clear(node_online_map);
Tejun Heo97e7b782011-02-16 17:11:08 +0100843 memset(&numa_meminfo, 0, sizeof(numa_meminfo));
Tejun Heo206e4202011-02-16 12:13:07 +0100844 memset(numa_nodes, 0, sizeof(numa_nodes));
Tejun Heofd0435d2011-02-16 17:11:08 +0100845 remove_all_active_ranges();
Tejun Heoffe77a42011-02-16 12:13:06 +0100846
847 if (numa_init[i]() < 0)
848 continue;
Tejun Heo206e4202011-02-16 12:13:07 +0100849
Tejun Heo56e827f2011-02-16 17:11:09 +0100850 if (numa_cleanup_meminfo(&numa_meminfo) < 0)
851 continue;
Tejun Heoffe77a42011-02-16 12:13:06 +0100852#ifdef CONFIG_NUMA_EMU
Tejun Heo190955482011-02-16 12:13:07 +0100853 setup_physnodes(0, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100854 if (cmdline && !numa_emulation(0, max_pfn, i == 0, i == 1))
855 return;
Tejun Heo190955482011-02-16 12:13:07 +0100856 setup_physnodes(0, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100857 nodes_clear(node_possible_map);
858 nodes_clear(node_online_map);
859#endif
Tejun Heof9c60252011-02-16 17:11:09 +0100860 if (numa_register_memblks(&numa_meminfo) < 0)
Tejun Heo43a662f2011-02-16 17:11:08 +0100861 continue;
862
Tejun Heofd0435d2011-02-16 17:11:08 +0100863 for (j = 0; j < nr_cpu_ids; j++) {
864 int nid = early_cpu_to_node(j);
865
866 if (nid == NUMA_NO_NODE)
867 continue;
868 if (!node_online(nid))
869 numa_clear_node(j);
870 }
871 numa_init_array();
872 return;
Tejun Heoffe77a42011-02-16 12:13:06 +0100873 }
874 BUG();
Andi Kleen69d81fc2005-11-05 17:25:53 +0100875}
876
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100877unsigned long __init numa_free_all_bootmem(void)
878{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 unsigned long pages = 0;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100880 int i;
881
882 for_each_online_node(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 pages += free_all_bootmem_node(NODE_DATA(i));
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100884
Yinghai Lu08677212010-02-10 01:20:20 -0800885 pages += free_all_memory_core_early(MAX_NUMNODES);
Yinghai Lu08677212010-02-10 01:20:20 -0800886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return pages;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100888}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100890int __cpuinit numa_cpu_node(int cpu)
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800891{
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100892 int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800893
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100894 if (apicid != BAD_APICID)
895 return __apicid_to_node[apicid];
896 return NUMA_NO_NODE;
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800897}
898
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100899/*
Tejun Heode2d9442011-01-23 14:37:41 +0100900 * UGLINESS AHEAD: Currently, CONFIG_NUMA_EMU is 64bit only and makes use
901 * of 64bit specific data structures. The distinction is artificial and
902 * should be removed. numa_{add|remove}_cpu() are implemented in numa.c
903 * for both 32 and 64bit when CONFIG_NUMA_EMU is disabled but here when
904 * enabled.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100905 *
Tejun Heode2d9442011-01-23 14:37:41 +0100906 * NUMA emulation is planned to be made generic and the following and other
907 * related code should be moved to numa.c.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100908 */
Tejun Heode2d9442011-01-23 14:37:41 +0100909#ifdef CONFIG_NUMA_EMU
910# ifndef CONFIG_DEBUG_PER_CPU_MAPS
David Rientjesc1c34432010-12-22 17:23:54 -0800911void __cpuinit numa_add_cpu(int cpu)
912{
913 unsigned long addr;
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100914 int physnid, nid;
David Rientjesc1c34432010-12-22 17:23:54 -0800915
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100916 nid = numa_cpu_node(cpu);
David Rientjesc1c34432010-12-22 17:23:54 -0800917 if (nid == NUMA_NO_NODE)
918 nid = early_cpu_to_node(cpu);
919 BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
920
921 /*
922 * Use the starting address of the emulated node to find which physical
923 * node it is allocated on.
924 */
925 addr = node_start_pfn(nid) << PAGE_SHIFT;
926 for (physnid = 0; physnid < MAX_NUMNODES; physnid++)
927 if (addr >= physnodes[physnid].start &&
928 addr < physnodes[physnid].end)
929 break;
930
931 /*
932 * Map the cpu to each emulated node that is allocated on the physical
933 * node of the cpu's apic id.
934 */
935 for_each_online_node(nid) {
936 addr = node_start_pfn(nid) << PAGE_SHIFT;
937 if (addr >= physnodes[physnid].start &&
938 addr < physnodes[physnid].end)
939 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
940 }
941}
942
943void __cpuinit numa_remove_cpu(int cpu)
944{
945 int i;
946
947 for_each_online_node(i)
948 cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
949}
Tejun Heode2d9442011-01-23 14:37:41 +0100950# else /* !CONFIG_DEBUG_PER_CPU_MAPS */
David Rientjesd906f0e2010-12-30 10:54:16 -0800951static void __cpuinit numa_set_cpumask(int cpu, int enable)
952{
953 int node = early_cpu_to_node(cpu);
954 struct cpumask *mask;
David Rientjesc1c34432010-12-22 17:23:54 -0800955 int i;
Brian Gerst6470aff2009-01-27 12:56:47 +0900956
David Rientjes14392fd2011-02-07 14:08:53 -0800957 if (node == NUMA_NO_NODE) {
958 /* early_cpu_to_node() already emits a warning and trace */
959 return;
960 }
David Rientjesc1c34432010-12-22 17:23:54 -0800961 for_each_online_node(i) {
962 unsigned long addr;
963
964 addr = node_start_pfn(i) << PAGE_SHIFT;
965 if (addr < physnodes[node].start ||
966 addr >= physnodes[node].end)
967 continue;
David Rientjesd906f0e2010-12-30 10:54:16 -0800968 mask = debug_cpumask_set_cpu(cpu, enable);
969 if (!mask)
David Rientjesc1c34432010-12-22 17:23:54 -0800970 return;
David Rientjesc1c34432010-12-22 17:23:54 -0800971
972 if (enable)
973 cpumask_set_cpu(cpu, mask);
974 else
975 cpumask_clear_cpu(cpu, mask);
Brian Gerst6470aff2009-01-27 12:56:47 +0900976 }
Brian Gerst6470aff2009-01-27 12:56:47 +0900977}
978
979void __cpuinit numa_add_cpu(int cpu)
980{
981 numa_set_cpumask(cpu, 1);
982}
983
984void __cpuinit numa_remove_cpu(int cpu)
985{
986 numa_set_cpumask(cpu, 0);
987}
Tejun Heode2d9442011-01-23 14:37:41 +0100988# endif /* !CONFIG_DEBUG_PER_CPU_MAPS */
989#endif /* CONFIG_NUMA_EMU */