blob: 82ee3083b0949889df4b2a630b43b6c61dc216e1 [file] [log] [blame]
Thomas Gleixnere3cfe522008-01-30 13:30:37 +01001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Generic VM initialization for x86-64 NUMA setups.
3 * Copyright 2002,2003 Andi Kleen, SuSE Labs.
Thomas Gleixnere3cfe522008-01-30 13:30:37 +01004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/kernel.h>
6#include <linux/mm.h>
7#include <linux/string.h>
8#include <linux/init.h>
9#include <linux/bootmem.h>
Yinghai Lu72d7c3b2010-08-25 13:39:17 -070010#include <linux/memblock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/mmzone.h>
12#include <linux/ctype.h>
13#include <linux/module.h>
14#include <linux/nodemask.h>
travis@sgi.com3cc87e32008-01-30 13:33:11 +010015#include <linux/sched.h>
Tejun Heod8fc3af2011-02-16 12:13:06 +010016#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <asm/e820.h>
19#include <asm/proto.h>
20#include <asm/dma.h>
21#include <asm/numa.h>
22#include <asm/acpi.h>
Andreas Herrmann23ac4ae2010-09-17 18:03:43 +020023#include <asm/amd_nb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -070025struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010026EXPORT_SYMBOL(node_data);
27
Tejun Heoec8cf29b2011-02-16 12:13:07 +010028nodemask_t cpu_nodes_parsed __initdata;
29nodemask_t mem_nodes_parsed __initdata;
30
Eric Dumazetdcf36bf2006-03-25 16:31:46 +010031struct memnode memnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Thomas Gleixner864fc312008-05-12 15:43:36 +020033static unsigned long __initdata nodemap_addr;
34static unsigned long __initdata nodemap_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Tejun Heo206e4202011-02-16 12:13:07 +010036struct bootnode numa_nodes[MAX_NUMNODES] __initdata;
37
Brian Gerst6470aff2009-01-27 12:56:47 +090038/*
Eric Dumazet529a3402005-11-05 17:25:54 +010039 * Given a shift value, try to populate memnodemap[]
40 * Returns :
41 * 1 if OK
42 * 0 if memnodmap[] too small (of shift too small)
43 * -1 if node overlap or lost ram (shift too big)
44 */
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010045static int __init populate_memnodemap(const struct bootnode *nodes,
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -070046 int numnodes, int shift, int *nodeids)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Eric Dumazet529a3402005-11-05 17:25:54 +010048 unsigned long addr, end;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010049 int i, res = -1;
Keith Manntheyb6846642005-07-28 21:15:38 -070050
travis@sgi.com43238382008-01-30 13:33:25 +010051 memset(memnodemap, 0xff, sizeof(s16)*memnodemapsize);
Eric Dumazet529a3402005-11-05 17:25:54 +010052 for (i = 0; i < numnodes; i++) {
53 addr = nodes[i].start;
54 end = nodes[i].end;
55 if (addr >= end)
56 continue;
Amul Shah076422d2007-02-13 13:26:19 +010057 if ((end >> shift) >= memnodemapsize)
Eric Dumazet529a3402005-11-05 17:25:54 +010058 return 0;
59 do {
travis@sgi.com43238382008-01-30 13:33:25 +010060 if (memnodemap[addr >> shift] != NUMA_NO_NODE)
Eric Dumazet529a3402005-11-05 17:25:54 +010061 return -1;
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -070062
63 if (!nodeids)
64 memnodemap[addr >> shift] = i;
65 else
66 memnodemap[addr >> shift] = nodeids[i];
67
Amul Shah076422d2007-02-13 13:26:19 +010068 addr += (1UL << shift);
Eric Dumazet529a3402005-11-05 17:25:54 +010069 } while (addr < end);
70 res = 1;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010071 }
Eric Dumazet529a3402005-11-05 17:25:54 +010072 return res;
73}
74
Amul Shah076422d2007-02-13 13:26:19 +010075static int __init allocate_cachealigned_memnodemap(void)
76{
Yinghai Lu24a5da72008-02-01 17:49:41 +010077 unsigned long addr;
Amul Shah076422d2007-02-13 13:26:19 +010078
79 memnodemap = memnode.embedded_map;
travis@sgi.com316390b2008-01-30 13:33:15 +010080 if (memnodemapsize <= ARRAY_SIZE(memnode.embedded_map))
Amul Shah076422d2007-02-13 13:26:19 +010081 return 0;
Amul Shah076422d2007-02-13 13:26:19 +010082
Yinghai Lu24a5da72008-02-01 17:49:41 +010083 addr = 0x8000;
Joerg Roedelbe3e89e2008-07-25 16:48:58 +020084 nodemap_size = roundup(sizeof(s16) * memnodemapsize, L1_CACHE_BYTES);
Yinghai Ludbef7b52010-12-27 16:48:08 -080085 nodemap_addr = memblock_find_in_range(addr, get_max_mapped(),
Yinghai Lu24a5da72008-02-01 17:49:41 +010086 nodemap_size, L1_CACHE_BYTES);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070087 if (nodemap_addr == MEMBLOCK_ERROR) {
Amul Shah076422d2007-02-13 13:26:19 +010088 printk(KERN_ERR
89 "NUMA: Unable to allocate Memory to Node hash map\n");
90 nodemap_addr = nodemap_size = 0;
91 return -1;
92 }
Yinghai Lu24a5da72008-02-01 17:49:41 +010093 memnodemap = phys_to_virt(nodemap_addr);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070094 memblock_x86_reserve_range(nodemap_addr, nodemap_addr + nodemap_size, "MEMNODEMAP");
Amul Shah076422d2007-02-13 13:26:19 +010095
96 printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n",
97 nodemap_addr, nodemap_addr + nodemap_size);
98 return 0;
99}
100
101/*
102 * The LSB of all start and end addresses in the node map is the value of the
103 * maximum possible shift.
104 */
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100105static int __init extract_lsb_from_nodes(const struct bootnode *nodes,
106 int numnodes)
Amul Shah076422d2007-02-13 13:26:19 +0100107{
Amul Shah54413922007-02-13 13:26:20 +0100108 int i, nodes_used = 0;
Amul Shah076422d2007-02-13 13:26:19 +0100109 unsigned long start, end;
110 unsigned long bitfield = 0, memtop = 0;
111
112 for (i = 0; i < numnodes; i++) {
113 start = nodes[i].start;
114 end = nodes[i].end;
115 if (start >= end)
116 continue;
Amul Shah54413922007-02-13 13:26:20 +0100117 bitfield |= start;
118 nodes_used++;
Amul Shah076422d2007-02-13 13:26:19 +0100119 if (end > memtop)
120 memtop = end;
121 }
Amul Shah54413922007-02-13 13:26:20 +0100122 if (nodes_used <= 1)
123 i = 63;
124 else
125 i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
Amul Shah076422d2007-02-13 13:26:19 +0100126 memnodemapsize = (memtop >> i)+1;
127 return i;
128}
129
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700130int __init compute_hash_shift(struct bootnode *nodes, int numnodes,
131 int *nodeids)
Eric Dumazet529a3402005-11-05 17:25:54 +0100132{
Amul Shah076422d2007-02-13 13:26:19 +0100133 int shift;
Eric Dumazet529a3402005-11-05 17:25:54 +0100134
Amul Shah076422d2007-02-13 13:26:19 +0100135 shift = extract_lsb_from_nodes(nodes, numnodes);
136 if (allocate_cachealigned_memnodemap())
137 return -1;
Andi Kleen6b050f82006-01-11 22:44:33 +0100138 printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
Eric Dumazet529a3402005-11-05 17:25:54 +0100139 shift);
140
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700141 if (populate_memnodemap(nodes, numnodes, shift, nodeids) != 1) {
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100142 printk(KERN_INFO "Your memory is not aligned you need to "
143 "rebuild your kernel with a bigger NODEMAPSIZE "
144 "shift=%d\n", shift);
Eric Dumazet529a3402005-11-05 17:25:54 +0100145 return -1;
146 }
Keith Manntheyb6846642005-07-28 21:15:38 -0700147 return shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -0800150int __meminit __early_pfn_to_nid(unsigned long pfn)
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700151{
152 return phys_to_nid(pfn << PAGE_SHIFT);
153}
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700154
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100155static void * __init early_node_mem(int nodeid, unsigned long start,
Yinghai Lu24a5da72008-02-01 17:49:41 +0100156 unsigned long end, unsigned long size,
157 unsigned long align)
Andi Kleena8062232006-04-07 19:49:21 +0200158{
Yinghai Lucef625e2010-02-10 01:20:18 -0800159 unsigned long mem;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100160
Yinghai Lucef625e2010-02-10 01:20:18 -0800161 /*
162 * put it on high as possible
163 * something will go with NODE_DATA
164 */
165 if (start < (MAX_DMA_PFN<<PAGE_SHIFT))
166 start = MAX_DMA_PFN<<PAGE_SHIFT;
167 if (start < (MAX_DMA32_PFN<<PAGE_SHIFT) &&
168 end > (MAX_DMA32_PFN<<PAGE_SHIFT))
169 start = MAX_DMA32_PFN<<PAGE_SHIFT;
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700170 mem = memblock_x86_find_in_range_node(nodeid, start, end, size, align);
171 if (mem != MEMBLOCK_ERROR)
Andi Kleena8062232006-04-07 19:49:21 +0200172 return __va(mem);
Yinghai Lu9347e0b2008-02-01 17:49:42 +0100173
Yinghai Lucef625e2010-02-10 01:20:18 -0800174 /* extend the search scope */
175 end = max_pfn_mapped << PAGE_SHIFT;
Yinghai Lu419db272010-10-28 09:50:17 -0700176 start = MAX_DMA_PFN << PAGE_SHIFT;
177 mem = memblock_find_in_range(start, end, size, align);
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700178 if (mem != MEMBLOCK_ERROR)
Yinghai Lu1842f902010-02-10 01:20:15 -0800179 return __va(mem);
180
181 printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100182 size, nodeid);
Yinghai Lu1842f902010-02-10 01:20:15 -0800183
184 return NULL;
Andi Kleena8062232006-04-07 19:49:21 +0200185}
186
Tejun Heo206e4202011-02-16 12:13:07 +0100187static __init void cutoff_node(int i, unsigned long start, unsigned long end)
188{
189 struct bootnode *nd = &numa_nodes[i];
190
191 if (nd->start < start) {
192 nd->start = start;
193 if (nd->end < nd->start)
194 nd->start = nd->end;
195 }
196 if (nd->end > end) {
197 nd->end = end;
198 if (nd->start > nd->end)
199 nd->start = nd->end;
200 }
201}
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/* Initialize bootmem allocator for a node */
Yinghai Lu7c437692009-05-15 13:59:37 -0700204void __init
205setup_node_bootmem(int nodeid, unsigned long start, unsigned long end)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100206{
Yinghai Lu08677212010-02-10 01:20:20 -0800207 unsigned long start_pfn, last_pfn, nodedata_phys;
Yinghai Lu7c437692009-05-15 13:59:37 -0700208 const int pgdat_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
Yinghai Lu1a27fc02008-03-18 12:52:37 -0700209 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Yinghai Lu4c31e922009-04-22 14:19:27 -0700211 if (!end)
212 return;
213
Yinghai Lu7c437692009-05-15 13:59:37 -0700214 /*
215 * Don't confuse VM with a node that doesn't have the
216 * minimum amount of memory:
217 */
218 if (end && (end - start) < NODE_MIN_SIZE)
219 return;
220
Joerg Roedelbe3e89e2008-07-25 16:48:58 +0200221 start = roundup(start, ZONE_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Yinghai Lu08677212010-02-10 01:20:20 -0800223 printk(KERN_INFO "Initmem setup node %d %016lx-%016lx\n", nodeid,
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100224 start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 start_pfn = start >> PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200227 last_pfn = end >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Yinghai Lu24a5da72008-02-01 17:49:41 +0100229 node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
230 SMP_CACHE_BYTES);
Andi Kleena8062232006-04-07 19:49:21 +0200231 if (node_data[nodeid] == NULL)
232 return;
233 nodedata_phys = __pa(node_data[nodeid]);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700234 memblock_x86_reserve_range(nodedata_phys, nodedata_phys + pgdat_size, "NODE_DATA");
Yinghai Lu6118f762008-02-04 16:47:56 +0100235 printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
236 nodedata_phys + pgdat_size - 1);
Yinghai Lu1842f902010-02-10 01:20:15 -0800237 nid = phys_to_nid(nodedata_phys);
238 if (nid != nodeid)
239 printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nodeid, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
Yinghai Lu08677212010-02-10 01:20:20 -0800242 NODE_DATA(nodeid)->node_id = nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 NODE_DATA(nodeid)->node_start_pfn = start_pfn;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200244 NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 node_set_online(nodeid);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100247}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249#ifdef CONFIG_NUMA_EMU
Rohit Seth53fee042007-02-13 13:26:22 +0100250/* Numa emulation */
David Rientjesadc19382009-09-25 15:20:09 -0700251static struct bootnode nodes[MAX_NUMNODES] __initdata;
David Rientjesc1c34432010-12-22 17:23:54 -0800252static struct bootnode physnodes[MAX_NUMNODES] __cpuinitdata;
Thomas Gleixner864fc312008-05-12 15:43:36 +0200253static char *cmdline __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Jan Beulich90321602011-01-19 08:57:21 +0000255void __init numa_emu_cmdline(char *str)
256{
257 cmdline = str;
258}
259
Tejun Heo19095542011-02-16 12:13:07 +0100260static int __init setup_physnodes(unsigned long start, unsigned long end)
David Rientjesadc19382009-09-25 15:20:09 -0700261{
David Rientjesadc19382009-09-25 15:20:09 -0700262 int ret = 0;
263 int i;
264
David Rientjesc1c34432010-12-22 17:23:54 -0800265 memset(physnodes, 0, sizeof(physnodes));
Tejun Heo19095542011-02-16 12:13:07 +0100266
267 for_each_node_mask(i, mem_nodes_parsed) {
268 physnodes[i].start = numa_nodes[i].start;
269 physnodes[i].end = numa_nodes[i].end;
270 }
271
David Rientjesadc19382009-09-25 15:20:09 -0700272 /*
273 * Basic sanity checking on the physical node map: there may be errors
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200274 * if the SRAT or AMD code incorrectly reported the topology or the mem=
David Rientjesadc19382009-09-25 15:20:09 -0700275 * kernel parameter is used.
276 */
David Rientjesa387e952010-12-22 17:23:56 -0800277 for (i = 0; i < MAX_NUMNODES; i++) {
David Rientjesadc19382009-09-25 15:20:09 -0700278 if (physnodes[i].start == physnodes[i].end)
279 continue;
280 if (physnodes[i].start > end) {
281 physnodes[i].end = physnodes[i].start;
282 continue;
283 }
284 if (physnodes[i].end < start) {
285 physnodes[i].start = physnodes[i].end;
286 continue;
287 }
288 if (physnodes[i].start < start)
289 physnodes[i].start = start;
290 if (physnodes[i].end > end)
291 physnodes[i].end = end;
David Rientjesadc19382009-09-25 15:20:09 -0700292 ret++;
293 }
294
295 /*
296 * If no physical topology was detected, a single node is faked to cover
297 * the entire address space.
298 */
299 if (!ret) {
300 physnodes[ret].start = start;
301 physnodes[ret].end = end;
302 ret = 1;
303 }
304 return ret;
305}
306
David Rientjesf51bf302010-12-22 17:23:51 -0800307static void __init fake_physnodes(int acpi, int amd, int nr_nodes)
308{
309 int i;
310
311 BUG_ON(acpi && amd);
312#ifdef CONFIG_ACPI_NUMA
313 if (acpi)
314 acpi_fake_nodes(nodes, nr_nodes);
315#endif
316#ifdef CONFIG_AMD_NUMA
317 if (amd)
318 amd_fake_nodes(nodes, nr_nodes);
319#endif
320 if (!acpi && !amd)
321 for (i = 0; i < nr_cpu_ids; i++)
322 numa_set_node(i, 0);
323}
324
Rohit Seth53fee042007-02-13 13:26:22 +0100325/*
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100326 * Setups up nid to range from addr to addr + size. If the end
327 * boundary is greater than max_addr, then max_addr is used instead.
328 * The return value is 0 if there is additional memory left for
329 * allocation past addr and -1 otherwise. addr is adjusted to be at
330 * the end of the node.
Rohit Seth53fee042007-02-13 13:26:22 +0100331 */
David Rientjesadc19382009-09-25 15:20:09 -0700332static int __init setup_node_range(int nid, u64 *addr, u64 size, u64 max_addr)
Rohit Seth53fee042007-02-13 13:26:22 +0100333{
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200334 int ret = 0;
335 nodes[nid].start = *addr;
336 *addr += size;
337 if (*addr >= max_addr) {
338 *addr = max_addr;
339 ret = -1;
340 }
341 nodes[nid].end = *addr;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200342 node_set(nid, node_possible_map);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200343 printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
344 nodes[nid].start, nodes[nid].end,
345 (nodes[nid].end - nodes[nid].start) >> 20);
346 return ret;
Rohit Seth53fee042007-02-13 13:26:22 +0100347}
348
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200349/*
David Rientjesadc19382009-09-25 15:20:09 -0700350 * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
351 * to max_addr. The return value is the number of nodes allocated.
352 */
David Rientjesc1c34432010-12-22 17:23:54 -0800353static int __init split_nodes_interleave(u64 addr, u64 max_addr, int nr_nodes)
David Rientjesadc19382009-09-25 15:20:09 -0700354{
355 nodemask_t physnode_mask = NODE_MASK_NONE;
356 u64 size;
357 int big;
358 int ret = 0;
359 int i;
360
361 if (nr_nodes <= 0)
362 return -1;
363 if (nr_nodes > MAX_NUMNODES) {
364 pr_info("numa=fake=%d too large, reducing to %d\n",
365 nr_nodes, MAX_NUMNODES);
366 nr_nodes = MAX_NUMNODES;
367 }
368
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700369 size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) / nr_nodes;
David Rientjesadc19382009-09-25 15:20:09 -0700370 /*
371 * Calculate the number of big nodes that can be allocated as a result
372 * of consolidating the remainder.
373 */
David Rientjes68fd1112010-02-15 13:43:25 -0800374 big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
David Rientjesadc19382009-09-25 15:20:09 -0700375 FAKE_NODE_MIN_SIZE;
376
377 size &= FAKE_NODE_MIN_HASH_MASK;
378 if (!size) {
379 pr_err("Not enough memory for each node. "
380 "NUMA emulation disabled.\n");
381 return -1;
382 }
383
David Rientjesc1c34432010-12-22 17:23:54 -0800384 for (i = 0; i < MAX_NUMNODES; i++)
David Rientjesadc19382009-09-25 15:20:09 -0700385 if (physnodes[i].start != physnodes[i].end)
386 node_set(i, physnode_mask);
387
388 /*
389 * Continue to fill physical nodes with fake nodes until there is no
390 * memory left on any of them.
391 */
392 while (nodes_weight(physnode_mask)) {
393 for_each_node_mask(i, physnode_mask) {
394 u64 end = physnodes[i].start + size;
395 u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
396
397 if (ret < big)
398 end += FAKE_NODE_MIN_SIZE;
399
400 /*
401 * Continue to add memory to this fake node if its
402 * non-reserved memory is less than the per-node size.
403 */
404 while (end - physnodes[i].start -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700405 memblock_x86_hole_size(physnodes[i].start, end) < size) {
David Rientjesadc19382009-09-25 15:20:09 -0700406 end += FAKE_NODE_MIN_SIZE;
407 if (end > physnodes[i].end) {
408 end = physnodes[i].end;
409 break;
410 }
411 }
412
413 /*
414 * If there won't be at least FAKE_NODE_MIN_SIZE of
415 * non-reserved memory in ZONE_DMA32 for the next node,
416 * this one must extend to the boundary.
417 */
418 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700419 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjesadc19382009-09-25 15:20:09 -0700420 end = dma32_end;
421
422 /*
423 * If there won't be enough non-reserved memory for the
424 * next node, this one must extend to the end of the
425 * physical node.
426 */
427 if (physnodes[i].end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700428 memblock_x86_hole_size(end, physnodes[i].end) < size)
David Rientjesadc19382009-09-25 15:20:09 -0700429 end = physnodes[i].end;
430
431 /*
432 * Avoid allocating more nodes than requested, which can
433 * happen as a result of rounding down each node's size
434 * to FAKE_NODE_MIN_SIZE.
435 */
436 if (nodes_weight(physnode_mask) + ret >= nr_nodes)
437 end = physnodes[i].end;
438
439 if (setup_node_range(ret++, &physnodes[i].start,
440 end - physnodes[i].start,
441 physnodes[i].end) < 0)
442 node_clear(i, physnode_mask);
443 }
444 }
445 return ret;
446}
447
448/*
David Rientjes8df5bb32010-02-15 13:43:30 -0800449 * Returns the end address of a node so that there is at least `size' amount of
450 * non-reserved memory or `max_addr' is reached.
451 */
452static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
453{
454 u64 end = start + size;
455
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700456 while (end - start - memblock_x86_hole_size(start, end) < size) {
David Rientjes8df5bb32010-02-15 13:43:30 -0800457 end += FAKE_NODE_MIN_SIZE;
458 if (end > max_addr) {
459 end = max_addr;
460 break;
461 }
462 }
463 return end;
464}
465
466/*
467 * Sets up fake nodes of `size' interleaved over physical nodes ranging from
468 * `addr' to `max_addr'. The return value is the number of nodes allocated.
469 */
470static int __init split_nodes_size_interleave(u64 addr, u64 max_addr, u64 size)
471{
472 nodemask_t physnode_mask = NODE_MASK_NONE;
473 u64 min_size;
474 int ret = 0;
475 int i;
476
477 if (!size)
478 return -1;
479 /*
480 * The limit on emulated nodes is MAX_NUMNODES, so the size per node is
481 * increased accordingly if the requested size is too small. This
482 * creates a uniform distribution of node sizes across the entire
483 * machine (but not necessarily over physical nodes).
484 */
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700485 min_size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) /
David Rientjes8df5bb32010-02-15 13:43:30 -0800486 MAX_NUMNODES;
487 min_size = max(min_size, FAKE_NODE_MIN_SIZE);
488 if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size)
489 min_size = (min_size + FAKE_NODE_MIN_SIZE) &
490 FAKE_NODE_MIN_HASH_MASK;
491 if (size < min_size) {
492 pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
493 size >> 20, min_size >> 20);
494 size = min_size;
495 }
496 size &= FAKE_NODE_MIN_HASH_MASK;
497
498 for (i = 0; i < MAX_NUMNODES; i++)
499 if (physnodes[i].start != physnodes[i].end)
500 node_set(i, physnode_mask);
501 /*
502 * Fill physical nodes with fake nodes of size until there is no memory
503 * left on any of them.
504 */
505 while (nodes_weight(physnode_mask)) {
506 for_each_node_mask(i, physnode_mask) {
507 u64 dma32_end = MAX_DMA32_PFN << PAGE_SHIFT;
508 u64 end;
509
510 end = find_end_of_node(physnodes[i].start,
511 physnodes[i].end, size);
512 /*
513 * If there won't be at least FAKE_NODE_MIN_SIZE of
514 * non-reserved memory in ZONE_DMA32 for the next node,
515 * this one must extend to the boundary.
516 */
517 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700518 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjes8df5bb32010-02-15 13:43:30 -0800519 end = dma32_end;
520
521 /*
522 * If there won't be enough non-reserved memory for the
523 * next node, this one must extend to the end of the
524 * physical node.
525 */
526 if (physnodes[i].end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700527 memblock_x86_hole_size(end, physnodes[i].end) < size)
David Rientjes8df5bb32010-02-15 13:43:30 -0800528 end = physnodes[i].end;
529
530 /*
531 * Setup the fake node that will be allocated as bootmem
532 * later. If setup_node_range() returns non-zero, there
533 * is no more memory available on this physical node.
534 */
535 if (setup_node_range(ret++, &physnodes[i].start,
536 end - physnodes[i].start,
537 physnodes[i].end) < 0)
538 node_clear(i, physnode_mask);
539 }
540 }
541 return ret;
542}
543
544/*
Thomas Gleixner886533a2008-05-12 15:43:36 +0200545 * Sets up the system RAM area from start_pfn to last_pfn according to the
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200546 * numa=fake command-line option.
547 */
David Rientjesadc19382009-09-25 15:20:09 -0700548static int __init numa_emulation(unsigned long start_pfn,
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200549 unsigned long last_pfn, int acpi, int amd)
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200550{
David Rientjesca2107c2010-02-15 13:43:33 -0800551 u64 addr = start_pfn << PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200552 u64 max_addr = last_pfn << PAGE_SHIFT;
David Rientjesca2107c2010-02-15 13:43:33 -0800553 int num_nodes;
554 int i;
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200555
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200556 /*
David Rientjes8df5bb32010-02-15 13:43:30 -0800557 * If the numa=fake command-line contains a 'M' or 'G', it represents
David Rientjesca2107c2010-02-15 13:43:33 -0800558 * the fixed node size. Otherwise, if it is just a single number N,
559 * split the system RAM into N fake nodes.
David Rientjes8df5bb32010-02-15 13:43:30 -0800560 */
561 if (strchr(cmdline, 'M') || strchr(cmdline, 'G')) {
David Rientjesca2107c2010-02-15 13:43:33 -0800562 u64 size;
563
David Rientjes8df5bb32010-02-15 13:43:30 -0800564 size = memparse(cmdline, &cmdline);
565 num_nodes = split_nodes_size_interleave(addr, max_addr, size);
David Rientjesca2107c2010-02-15 13:43:33 -0800566 } else {
567 unsigned long n;
568
569 n = simple_strtoul(cmdline, NULL, 0);
David Rientjesc1c34432010-12-22 17:23:54 -0800570 num_nodes = split_nodes_interleave(addr, max_addr, n);
David Rientjes8df5bb32010-02-15 13:43:30 -0800571 }
572
David Rientjesca2107c2010-02-15 13:43:33 -0800573 if (num_nodes < 0)
574 return num_nodes;
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700575 memnode_shift = compute_hash_shift(nodes, num_nodes, NULL);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200576 if (memnode_shift < 0) {
577 memnode_shift = 0;
578 printk(KERN_ERR "No NUMA hash function found. NUMA emulation "
579 "disabled.\n");
580 return -1;
581 }
582
583 /*
David Rientjesadc19382009-09-25 15:20:09 -0700584 * We need to vacate all active ranges that may have been registered for
585 * the e820 memory map.
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200586 */
587 remove_all_active_ranges();
Yinghai Lu1411e0e2010-12-27 16:48:17 -0800588 for_each_node_mask(i, node_possible_map)
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700589 memblock_x86_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
Mel Gorman5cb248a2006-09-27 01:49:52 -0700590 nodes[i].end >> PAGE_SHIFT);
Yinghai Lu1411e0e2010-12-27 16:48:17 -0800591 init_memory_mapping_high();
592 for_each_node_mask(i, node_possible_map)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100593 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
Tejun Heo19095542011-02-16 12:13:07 +0100594 setup_physnodes(addr, max_addr);
David Rientjesf51bf302010-12-22 17:23:51 -0800595 fake_physnodes(acpi, amd, num_nodes);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100596 numa_init_array();
597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200599#endif /* CONFIG_NUMA_EMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Tejun Heoffe77a42011-02-16 12:13:06 +0100601static int dummy_numa_init(void)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100602{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 printk(KERN_INFO "%s\n",
604 numa_off ? "NUMA turned off" : "No NUMA configuration found");
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100605 printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
Tejun Heo86ef4db2011-02-16 12:13:06 +0100606 0LU, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100607
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100608 node_set(0, cpu_nodes_parsed);
609 node_set(0, mem_nodes_parsed);
610
611 return 0;
612}
613
614static int dummy_scan_nodes(void)
615{
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100616 /* setup dummy node covering all memory */
617 memnode_shift = 63;
Amul Shah076422d2007-02-13 13:26:19 +0100618 memnodemap = memnode.embedded_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 memnodemap[0] = 0;
Tejun Heo86ef4db2011-02-16 12:13:06 +0100620 memblock_x86_register_active_regions(0, 0, max_pfn);
Yinghai Lu1411e0e2010-12-27 16:48:17 -0800621 init_memory_mapping_high();
Tejun Heo86ef4db2011-02-16 12:13:06 +0100622 setup_node_bootmem(0, 0, max_pfn << PAGE_SHIFT);
Tejun Heo7d36b7b2011-02-16 12:13:06 +0100623 numa_init_array();
Tejun Heoffe77a42011-02-16 12:13:06 +0100624
625 return 0;
626}
627
628void __init initmem_init(void)
629{
630 int (*numa_init[])(void) = { [2] = dummy_numa_init };
631 int (*scan_nodes[])(void) = { [2] = dummy_scan_nodes };
632 int i, j;
633
634 if (!numa_off) {
635#ifdef CONFIG_ACPI_NUMA
636 numa_init[0] = x86_acpi_numa_init;
637 scan_nodes[0] = acpi_scan_nodes;
638#endif
639#ifdef CONFIG_AMD_NUMA
640 numa_init[1] = amd_numa_init;
641 scan_nodes[1] = amd_scan_nodes;
642#endif
643 }
644
645 for (i = 0; i < ARRAY_SIZE(numa_init); i++) {
646 if (!numa_init[i])
647 continue;
648
649 for (j = 0; j < MAX_LOCAL_APIC; j++)
650 set_apicid_to_node(j, NUMA_NO_NODE);
651
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100652 nodes_clear(cpu_nodes_parsed);
653 nodes_clear(mem_nodes_parsed);
Tejun Heoffe77a42011-02-16 12:13:06 +0100654 nodes_clear(node_possible_map);
655 nodes_clear(node_online_map);
Tejun Heo206e4202011-02-16 12:13:07 +0100656 memset(numa_nodes, 0, sizeof(numa_nodes));
Tejun Heoffe77a42011-02-16 12:13:06 +0100657
658 if (numa_init[i]() < 0)
659 continue;
Tejun Heo206e4202011-02-16 12:13:07 +0100660
661 /* clean up the node list */
662 for (j = 0; j < MAX_NUMNODES; j++)
663 cutoff_node(j, 0, max_pfn << PAGE_SHIFT);
664
Tejun Heoffe77a42011-02-16 12:13:06 +0100665#ifdef CONFIG_NUMA_EMU
Tejun Heo19095542011-02-16 12:13:07 +0100666 setup_physnodes(0, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100667 if (cmdline && !numa_emulation(0, max_pfn, i == 0, i == 1))
668 return;
Tejun Heo19095542011-02-16 12:13:07 +0100669 setup_physnodes(0, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100670 nodes_clear(node_possible_map);
671 nodes_clear(node_online_map);
672#endif
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100673 /* Account for nodes with cpus and no memory */
674 nodes_or(node_possible_map, mem_nodes_parsed, cpu_nodes_parsed);
675 if (WARN_ON(nodes_empty(node_possible_map)))
676 continue;
677
Tejun Heoffe77a42011-02-16 12:13:06 +0100678 if (!scan_nodes[i]())
679 return;
680 }
681 BUG();
Andi Kleen69d81fc2005-11-05 17:25:53 +0100682}
683
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100684unsigned long __init numa_free_all_bootmem(void)
685{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 unsigned long pages = 0;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100687 int i;
688
689 for_each_online_node(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 pages += free_all_bootmem_node(NODE_DATA(i));
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100691
Yinghai Lu08677212010-02-10 01:20:20 -0800692 pages += free_all_memory_core_early(MAX_NUMNODES);
Yinghai Lu08677212010-02-10 01:20:20 -0800693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return pages;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100695}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100697int __cpuinit numa_cpu_node(int cpu)
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800698{
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100699 int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800700
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100701 if (apicid != BAD_APICID)
702 return __apicid_to_node[apicid];
703 return NUMA_NO_NODE;
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800704}
705
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100706/*
Tejun Heode2d9442011-01-23 14:37:41 +0100707 * UGLINESS AHEAD: Currently, CONFIG_NUMA_EMU is 64bit only and makes use
708 * of 64bit specific data structures. The distinction is artificial and
709 * should be removed. numa_{add|remove}_cpu() are implemented in numa.c
710 * for both 32 and 64bit when CONFIG_NUMA_EMU is disabled but here when
711 * enabled.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100712 *
Tejun Heode2d9442011-01-23 14:37:41 +0100713 * NUMA emulation is planned to be made generic and the following and other
714 * related code should be moved to numa.c.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100715 */
Tejun Heode2d9442011-01-23 14:37:41 +0100716#ifdef CONFIG_NUMA_EMU
717# ifndef CONFIG_DEBUG_PER_CPU_MAPS
David Rientjesc1c34432010-12-22 17:23:54 -0800718void __cpuinit numa_add_cpu(int cpu)
719{
720 unsigned long addr;
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100721 int physnid, nid;
David Rientjesc1c34432010-12-22 17:23:54 -0800722
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100723 nid = numa_cpu_node(cpu);
David Rientjesc1c34432010-12-22 17:23:54 -0800724 if (nid == NUMA_NO_NODE)
725 nid = early_cpu_to_node(cpu);
726 BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
727
728 /*
729 * Use the starting address of the emulated node to find which physical
730 * node it is allocated on.
731 */
732 addr = node_start_pfn(nid) << PAGE_SHIFT;
733 for (physnid = 0; physnid < MAX_NUMNODES; physnid++)
734 if (addr >= physnodes[physnid].start &&
735 addr < physnodes[physnid].end)
736 break;
737
738 /*
739 * Map the cpu to each emulated node that is allocated on the physical
740 * node of the cpu's apic id.
741 */
742 for_each_online_node(nid) {
743 addr = node_start_pfn(nid) << PAGE_SHIFT;
744 if (addr >= physnodes[physnid].start &&
745 addr < physnodes[physnid].end)
746 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
747 }
748}
749
750void __cpuinit numa_remove_cpu(int cpu)
751{
752 int i;
753
754 for_each_online_node(i)
755 cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
756}
Tejun Heode2d9442011-01-23 14:37:41 +0100757# else /* !CONFIG_DEBUG_PER_CPU_MAPS */
David Rientjesd906f0e2010-12-30 10:54:16 -0800758static void __cpuinit numa_set_cpumask(int cpu, int enable)
759{
760 int node = early_cpu_to_node(cpu);
761 struct cpumask *mask;
David Rientjesc1c34432010-12-22 17:23:54 -0800762 int i;
Brian Gerst6470aff2009-01-27 12:56:47 +0900763
David Rientjes14392fd2011-02-07 14:08:53 -0800764 if (node == NUMA_NO_NODE) {
765 /* early_cpu_to_node() already emits a warning and trace */
766 return;
767 }
David Rientjesc1c34432010-12-22 17:23:54 -0800768 for_each_online_node(i) {
769 unsigned long addr;
770
771 addr = node_start_pfn(i) << PAGE_SHIFT;
772 if (addr < physnodes[node].start ||
773 addr >= physnodes[node].end)
774 continue;
David Rientjesd906f0e2010-12-30 10:54:16 -0800775 mask = debug_cpumask_set_cpu(cpu, enable);
776 if (!mask)
David Rientjesc1c34432010-12-22 17:23:54 -0800777 return;
David Rientjesc1c34432010-12-22 17:23:54 -0800778
779 if (enable)
780 cpumask_set_cpu(cpu, mask);
781 else
782 cpumask_clear_cpu(cpu, mask);
Brian Gerst6470aff2009-01-27 12:56:47 +0900783 }
Brian Gerst6470aff2009-01-27 12:56:47 +0900784}
785
786void __cpuinit numa_add_cpu(int cpu)
787{
788 numa_set_cpumask(cpu, 1);
789}
790
791void __cpuinit numa_remove_cpu(int cpu)
792{
793 numa_set_cpumask(cpu, 0);
794}
Tejun Heode2d9442011-01-23 14:37:41 +0100795# endif /* !CONFIG_DEBUG_PER_CPU_MAPS */
796#endif /* CONFIG_NUMA_EMU */