blob: a6b899f7ddd2bf278c909843934931664f6050db [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
David Rientjesadc19382009-09-25 15:20:09 -0700260static int __init setup_physnodes(unsigned long start, unsigned long end,
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200261 int acpi, int amd)
David Rientjesadc19382009-09-25 15:20:09 -0700262{
David Rientjesadc19382009-09-25 15:20:09 -0700263 int ret = 0;
264 int i;
265
David Rientjesc1c34432010-12-22 17:23:54 -0800266 memset(physnodes, 0, sizeof(physnodes));
David Rientjesadc19382009-09-25 15:20:09 -0700267#ifdef CONFIG_ACPI_NUMA
268 if (acpi)
David Rientjesa387e952010-12-22 17:23:56 -0800269 acpi_get_nodes(physnodes, start, end);
David Rientjesadc19382009-09-25 15:20:09 -0700270#endif
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200271#ifdef CONFIG_AMD_NUMA
272 if (amd)
David Rientjesa387e952010-12-22 17:23:56 -0800273 amd_get_nodes(physnodes);
David Rientjesadc19382009-09-25 15:20:09 -0700274#endif
275 /*
276 * Basic sanity checking on the physical node map: there may be errors
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200277 * if the SRAT or AMD code incorrectly reported the topology or the mem=
David Rientjesadc19382009-09-25 15:20:09 -0700278 * kernel parameter is used.
279 */
David Rientjesa387e952010-12-22 17:23:56 -0800280 for (i = 0; i < MAX_NUMNODES; i++) {
David Rientjesadc19382009-09-25 15:20:09 -0700281 if (physnodes[i].start == physnodes[i].end)
282 continue;
283 if (physnodes[i].start > end) {
284 physnodes[i].end = physnodes[i].start;
285 continue;
286 }
287 if (physnodes[i].end < start) {
288 physnodes[i].start = physnodes[i].end;
289 continue;
290 }
291 if (physnodes[i].start < start)
292 physnodes[i].start = start;
293 if (physnodes[i].end > end)
294 physnodes[i].end = end;
David Rientjesadc19382009-09-25 15:20:09 -0700295 ret++;
296 }
297
298 /*
299 * If no physical topology was detected, a single node is faked to cover
300 * the entire address space.
301 */
302 if (!ret) {
303 physnodes[ret].start = start;
304 physnodes[ret].end = end;
305 ret = 1;
306 }
307 return ret;
308}
309
David Rientjesf51bf302010-12-22 17:23:51 -0800310static void __init fake_physnodes(int acpi, int amd, int nr_nodes)
311{
312 int i;
313
314 BUG_ON(acpi && amd);
315#ifdef CONFIG_ACPI_NUMA
316 if (acpi)
317 acpi_fake_nodes(nodes, nr_nodes);
318#endif
319#ifdef CONFIG_AMD_NUMA
320 if (amd)
321 amd_fake_nodes(nodes, nr_nodes);
322#endif
323 if (!acpi && !amd)
324 for (i = 0; i < nr_cpu_ids; i++)
325 numa_set_node(i, 0);
326}
327
Rohit Seth53fee042007-02-13 13:26:22 +0100328/*
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100329 * Setups up nid to range from addr to addr + size. If the end
330 * boundary is greater than max_addr, then max_addr is used instead.
331 * The return value is 0 if there is additional memory left for
332 * allocation past addr and -1 otherwise. addr is adjusted to be at
333 * the end of the node.
Rohit Seth53fee042007-02-13 13:26:22 +0100334 */
David Rientjesadc19382009-09-25 15:20:09 -0700335static int __init setup_node_range(int nid, u64 *addr, u64 size, u64 max_addr)
Rohit Seth53fee042007-02-13 13:26:22 +0100336{
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200337 int ret = 0;
338 nodes[nid].start = *addr;
339 *addr += size;
340 if (*addr >= max_addr) {
341 *addr = max_addr;
342 ret = -1;
343 }
344 nodes[nid].end = *addr;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200345 node_set(nid, node_possible_map);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200346 printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
347 nodes[nid].start, nodes[nid].end,
348 (nodes[nid].end - nodes[nid].start) >> 20);
349 return ret;
Rohit Seth53fee042007-02-13 13:26:22 +0100350}
351
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200352/*
David Rientjesadc19382009-09-25 15:20:09 -0700353 * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
354 * to max_addr. The return value is the number of nodes allocated.
355 */
David Rientjesc1c34432010-12-22 17:23:54 -0800356static int __init split_nodes_interleave(u64 addr, u64 max_addr, int nr_nodes)
David Rientjesadc19382009-09-25 15:20:09 -0700357{
358 nodemask_t physnode_mask = NODE_MASK_NONE;
359 u64 size;
360 int big;
361 int ret = 0;
362 int i;
363
364 if (nr_nodes <= 0)
365 return -1;
366 if (nr_nodes > MAX_NUMNODES) {
367 pr_info("numa=fake=%d too large, reducing to %d\n",
368 nr_nodes, MAX_NUMNODES);
369 nr_nodes = MAX_NUMNODES;
370 }
371
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700372 size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) / nr_nodes;
David Rientjesadc19382009-09-25 15:20:09 -0700373 /*
374 * Calculate the number of big nodes that can be allocated as a result
375 * of consolidating the remainder.
376 */
David Rientjes68fd1112010-02-15 13:43:25 -0800377 big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
David Rientjesadc19382009-09-25 15:20:09 -0700378 FAKE_NODE_MIN_SIZE;
379
380 size &= FAKE_NODE_MIN_HASH_MASK;
381 if (!size) {
382 pr_err("Not enough memory for each node. "
383 "NUMA emulation disabled.\n");
384 return -1;
385 }
386
David Rientjesc1c34432010-12-22 17:23:54 -0800387 for (i = 0; i < MAX_NUMNODES; i++)
David Rientjesadc19382009-09-25 15:20:09 -0700388 if (physnodes[i].start != physnodes[i].end)
389 node_set(i, physnode_mask);
390
391 /*
392 * Continue to fill physical nodes with fake nodes until there is no
393 * memory left on any of them.
394 */
395 while (nodes_weight(physnode_mask)) {
396 for_each_node_mask(i, physnode_mask) {
397 u64 end = physnodes[i].start + size;
398 u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
399
400 if (ret < big)
401 end += FAKE_NODE_MIN_SIZE;
402
403 /*
404 * Continue to add memory to this fake node if its
405 * non-reserved memory is less than the per-node size.
406 */
407 while (end - physnodes[i].start -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700408 memblock_x86_hole_size(physnodes[i].start, end) < size) {
David Rientjesadc19382009-09-25 15:20:09 -0700409 end += FAKE_NODE_MIN_SIZE;
410 if (end > physnodes[i].end) {
411 end = physnodes[i].end;
412 break;
413 }
414 }
415
416 /*
417 * If there won't be at least FAKE_NODE_MIN_SIZE of
418 * non-reserved memory in ZONE_DMA32 for the next node,
419 * this one must extend to the boundary.
420 */
421 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700422 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjesadc19382009-09-25 15:20:09 -0700423 end = dma32_end;
424
425 /*
426 * If there won't be enough non-reserved memory for the
427 * next node, this one must extend to the end of the
428 * physical node.
429 */
430 if (physnodes[i].end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700431 memblock_x86_hole_size(end, physnodes[i].end) < size)
David Rientjesadc19382009-09-25 15:20:09 -0700432 end = physnodes[i].end;
433
434 /*
435 * Avoid allocating more nodes than requested, which can
436 * happen as a result of rounding down each node's size
437 * to FAKE_NODE_MIN_SIZE.
438 */
439 if (nodes_weight(physnode_mask) + ret >= nr_nodes)
440 end = physnodes[i].end;
441
442 if (setup_node_range(ret++, &physnodes[i].start,
443 end - physnodes[i].start,
444 physnodes[i].end) < 0)
445 node_clear(i, physnode_mask);
446 }
447 }
448 return ret;
449}
450
451/*
David Rientjes8df5bb32010-02-15 13:43:30 -0800452 * Returns the end address of a node so that there is at least `size' amount of
453 * non-reserved memory or `max_addr' is reached.
454 */
455static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
456{
457 u64 end = start + size;
458
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700459 while (end - start - memblock_x86_hole_size(start, end) < size) {
David Rientjes8df5bb32010-02-15 13:43:30 -0800460 end += FAKE_NODE_MIN_SIZE;
461 if (end > max_addr) {
462 end = max_addr;
463 break;
464 }
465 }
466 return end;
467}
468
469/*
470 * Sets up fake nodes of `size' interleaved over physical nodes ranging from
471 * `addr' to `max_addr'. The return value is the number of nodes allocated.
472 */
473static int __init split_nodes_size_interleave(u64 addr, u64 max_addr, u64 size)
474{
475 nodemask_t physnode_mask = NODE_MASK_NONE;
476 u64 min_size;
477 int ret = 0;
478 int i;
479
480 if (!size)
481 return -1;
482 /*
483 * The limit on emulated nodes is MAX_NUMNODES, so the size per node is
484 * increased accordingly if the requested size is too small. This
485 * creates a uniform distribution of node sizes across the entire
486 * machine (but not necessarily over physical nodes).
487 */
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700488 min_size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) /
David Rientjes8df5bb32010-02-15 13:43:30 -0800489 MAX_NUMNODES;
490 min_size = max(min_size, FAKE_NODE_MIN_SIZE);
491 if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size)
492 min_size = (min_size + FAKE_NODE_MIN_SIZE) &
493 FAKE_NODE_MIN_HASH_MASK;
494 if (size < min_size) {
495 pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
496 size >> 20, min_size >> 20);
497 size = min_size;
498 }
499 size &= FAKE_NODE_MIN_HASH_MASK;
500
501 for (i = 0; i < MAX_NUMNODES; i++)
502 if (physnodes[i].start != physnodes[i].end)
503 node_set(i, physnode_mask);
504 /*
505 * Fill physical nodes with fake nodes of size until there is no memory
506 * left on any of them.
507 */
508 while (nodes_weight(physnode_mask)) {
509 for_each_node_mask(i, physnode_mask) {
510 u64 dma32_end = MAX_DMA32_PFN << PAGE_SHIFT;
511 u64 end;
512
513 end = find_end_of_node(physnodes[i].start,
514 physnodes[i].end, size);
515 /*
516 * If there won't be at least FAKE_NODE_MIN_SIZE of
517 * non-reserved memory in ZONE_DMA32 for the next node,
518 * this one must extend to the boundary.
519 */
520 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700521 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjes8df5bb32010-02-15 13:43:30 -0800522 end = dma32_end;
523
524 /*
525 * If there won't be enough non-reserved memory for the
526 * next node, this one must extend to the end of the
527 * physical node.
528 */
529 if (physnodes[i].end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700530 memblock_x86_hole_size(end, physnodes[i].end) < size)
David Rientjes8df5bb32010-02-15 13:43:30 -0800531 end = physnodes[i].end;
532
533 /*
534 * Setup the fake node that will be allocated as bootmem
535 * later. If setup_node_range() returns non-zero, there
536 * is no more memory available on this physical node.
537 */
538 if (setup_node_range(ret++, &physnodes[i].start,
539 end - physnodes[i].start,
540 physnodes[i].end) < 0)
541 node_clear(i, physnode_mask);
542 }
543 }
544 return ret;
545}
546
547/*
Thomas Gleixner886533a2008-05-12 15:43:36 +0200548 * Sets up the system RAM area from start_pfn to last_pfn according to the
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200549 * numa=fake command-line option.
550 */
David Rientjesadc19382009-09-25 15:20:09 -0700551static int __init numa_emulation(unsigned long start_pfn,
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200552 unsigned long last_pfn, int acpi, int amd)
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200553{
David Rientjesca2107c2010-02-15 13:43:33 -0800554 u64 addr = start_pfn << PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200555 u64 max_addr = last_pfn << PAGE_SHIFT;
David Rientjesca2107c2010-02-15 13:43:33 -0800556 int num_nodes;
557 int i;
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200558
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200559 /*
David Rientjes8df5bb32010-02-15 13:43:30 -0800560 * If the numa=fake command-line contains a 'M' or 'G', it represents
David Rientjesca2107c2010-02-15 13:43:33 -0800561 * the fixed node size. Otherwise, if it is just a single number N,
562 * split the system RAM into N fake nodes.
David Rientjes8df5bb32010-02-15 13:43:30 -0800563 */
564 if (strchr(cmdline, 'M') || strchr(cmdline, 'G')) {
David Rientjesca2107c2010-02-15 13:43:33 -0800565 u64 size;
566
David Rientjes8df5bb32010-02-15 13:43:30 -0800567 size = memparse(cmdline, &cmdline);
568 num_nodes = split_nodes_size_interleave(addr, max_addr, size);
David Rientjesca2107c2010-02-15 13:43:33 -0800569 } else {
570 unsigned long n;
571
572 n = simple_strtoul(cmdline, NULL, 0);
David Rientjesc1c34432010-12-22 17:23:54 -0800573 num_nodes = split_nodes_interleave(addr, max_addr, n);
David Rientjes8df5bb32010-02-15 13:43:30 -0800574 }
575
David Rientjesca2107c2010-02-15 13:43:33 -0800576 if (num_nodes < 0)
577 return num_nodes;
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700578 memnode_shift = compute_hash_shift(nodes, num_nodes, NULL);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200579 if (memnode_shift < 0) {
580 memnode_shift = 0;
581 printk(KERN_ERR "No NUMA hash function found. NUMA emulation "
582 "disabled.\n");
583 return -1;
584 }
585
586 /*
David Rientjesadc19382009-09-25 15:20:09 -0700587 * We need to vacate all active ranges that may have been registered for
588 * the e820 memory map.
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200589 */
590 remove_all_active_ranges();
Yinghai Lu1411e0e2010-12-27 16:48:17 -0800591 for_each_node_mask(i, node_possible_map)
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700592 memblock_x86_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
Mel Gorman5cb248a2006-09-27 01:49:52 -0700593 nodes[i].end >> PAGE_SHIFT);
Yinghai Lu1411e0e2010-12-27 16:48:17 -0800594 init_memory_mapping_high();
595 for_each_node_mask(i, node_possible_map)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100596 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
David Rientjesc1c34432010-12-22 17:23:54 -0800597 setup_physnodes(addr, max_addr, acpi, amd);
David Rientjesf51bf302010-12-22 17:23:51 -0800598 fake_physnodes(acpi, amd, num_nodes);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100599 numa_init_array();
600 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200602#endif /* CONFIG_NUMA_EMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Tejun Heoffe77a42011-02-16 12:13:06 +0100604static int dummy_numa_init(void)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100605{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 printk(KERN_INFO "%s\n",
607 numa_off ? "NUMA turned off" : "No NUMA configuration found");
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100608 printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
Tejun Heo86ef4db2011-02-16 12:13:06 +0100609 0LU, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100610
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100611 node_set(0, cpu_nodes_parsed);
612 node_set(0, mem_nodes_parsed);
613
614 return 0;
615}
616
617static int dummy_scan_nodes(void)
618{
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100619 /* setup dummy node covering all memory */
620 memnode_shift = 63;
Amul Shah076422d2007-02-13 13:26:19 +0100621 memnodemap = memnode.embedded_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 memnodemap[0] = 0;
Tejun Heo86ef4db2011-02-16 12:13:06 +0100623 memblock_x86_register_active_regions(0, 0, max_pfn);
Yinghai Lu1411e0e2010-12-27 16:48:17 -0800624 init_memory_mapping_high();
Tejun Heo86ef4db2011-02-16 12:13:06 +0100625 setup_node_bootmem(0, 0, max_pfn << PAGE_SHIFT);
Tejun Heo7d36b7b2011-02-16 12:13:06 +0100626 numa_init_array();
Tejun Heoffe77a42011-02-16 12:13:06 +0100627
628 return 0;
629}
630
631void __init initmem_init(void)
632{
633 int (*numa_init[])(void) = { [2] = dummy_numa_init };
634 int (*scan_nodes[])(void) = { [2] = dummy_scan_nodes };
635 int i, j;
636
637 if (!numa_off) {
638#ifdef CONFIG_ACPI_NUMA
639 numa_init[0] = x86_acpi_numa_init;
640 scan_nodes[0] = acpi_scan_nodes;
641#endif
642#ifdef CONFIG_AMD_NUMA
643 numa_init[1] = amd_numa_init;
644 scan_nodes[1] = amd_scan_nodes;
645#endif
646 }
647
648 for (i = 0; i < ARRAY_SIZE(numa_init); i++) {
649 if (!numa_init[i])
650 continue;
651
652 for (j = 0; j < MAX_LOCAL_APIC; j++)
653 set_apicid_to_node(j, NUMA_NO_NODE);
654
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100655 nodes_clear(cpu_nodes_parsed);
656 nodes_clear(mem_nodes_parsed);
Tejun Heoffe77a42011-02-16 12:13:06 +0100657 nodes_clear(node_possible_map);
658 nodes_clear(node_online_map);
Tejun Heo206e4202011-02-16 12:13:07 +0100659 memset(numa_nodes, 0, sizeof(numa_nodes));
Tejun Heoffe77a42011-02-16 12:13:06 +0100660
661 if (numa_init[i]() < 0)
662 continue;
Tejun Heo206e4202011-02-16 12:13:07 +0100663
664 /* clean up the node list */
665 for (j = 0; j < MAX_NUMNODES; j++)
666 cutoff_node(j, 0, max_pfn << PAGE_SHIFT);
667
Tejun Heoffe77a42011-02-16 12:13:06 +0100668#ifdef CONFIG_NUMA_EMU
669 setup_physnodes(0, max_pfn << PAGE_SHIFT, i == 0, i == 1);
670 if (cmdline && !numa_emulation(0, max_pfn, i == 0, i == 1))
671 return;
672 setup_physnodes(0, max_pfn << PAGE_SHIFT, i == 0, i == 1);
673 nodes_clear(node_possible_map);
674 nodes_clear(node_online_map);
675#endif
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100676 /* Account for nodes with cpus and no memory */
677 nodes_or(node_possible_map, mem_nodes_parsed, cpu_nodes_parsed);
678 if (WARN_ON(nodes_empty(node_possible_map)))
679 continue;
680
Tejun Heoffe77a42011-02-16 12:13:06 +0100681 if (!scan_nodes[i]())
682 return;
683 }
684 BUG();
Andi Kleen69d81fc2005-11-05 17:25:53 +0100685}
686
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100687unsigned long __init numa_free_all_bootmem(void)
688{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 unsigned long pages = 0;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100690 int i;
691
692 for_each_online_node(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 pages += free_all_bootmem_node(NODE_DATA(i));
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100694
Yinghai Lu08677212010-02-10 01:20:20 -0800695 pages += free_all_memory_core_early(MAX_NUMNODES);
Yinghai Lu08677212010-02-10 01:20:20 -0800696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return pages;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100698}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100700int __cpuinit numa_cpu_node(int cpu)
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800701{
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100702 int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800703
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100704 if (apicid != BAD_APICID)
705 return __apicid_to_node[apicid];
706 return NUMA_NO_NODE;
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800707}
708
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100709/*
Tejun Heode2d9442011-01-23 14:37:41 +0100710 * UGLINESS AHEAD: Currently, CONFIG_NUMA_EMU is 64bit only and makes use
711 * of 64bit specific data structures. The distinction is artificial and
712 * should be removed. numa_{add|remove}_cpu() are implemented in numa.c
713 * for both 32 and 64bit when CONFIG_NUMA_EMU is disabled but here when
714 * enabled.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100715 *
Tejun Heode2d9442011-01-23 14:37:41 +0100716 * NUMA emulation is planned to be made generic and the following and other
717 * related code should be moved to numa.c.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100718 */
Tejun Heode2d9442011-01-23 14:37:41 +0100719#ifdef CONFIG_NUMA_EMU
720# ifndef CONFIG_DEBUG_PER_CPU_MAPS
David Rientjesc1c34432010-12-22 17:23:54 -0800721void __cpuinit numa_add_cpu(int cpu)
722{
723 unsigned long addr;
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100724 int physnid, nid;
David Rientjesc1c34432010-12-22 17:23:54 -0800725
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100726 nid = numa_cpu_node(cpu);
David Rientjesc1c34432010-12-22 17:23:54 -0800727 if (nid == NUMA_NO_NODE)
728 nid = early_cpu_to_node(cpu);
729 BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
730
731 /*
732 * Use the starting address of the emulated node to find which physical
733 * node it is allocated on.
734 */
735 addr = node_start_pfn(nid) << PAGE_SHIFT;
736 for (physnid = 0; physnid < MAX_NUMNODES; physnid++)
737 if (addr >= physnodes[physnid].start &&
738 addr < physnodes[physnid].end)
739 break;
740
741 /*
742 * Map the cpu to each emulated node that is allocated on the physical
743 * node of the cpu's apic id.
744 */
745 for_each_online_node(nid) {
746 addr = node_start_pfn(nid) << PAGE_SHIFT;
747 if (addr >= physnodes[physnid].start &&
748 addr < physnodes[physnid].end)
749 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
750 }
751}
752
753void __cpuinit numa_remove_cpu(int cpu)
754{
755 int i;
756
757 for_each_online_node(i)
758 cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
759}
Tejun Heode2d9442011-01-23 14:37:41 +0100760# else /* !CONFIG_DEBUG_PER_CPU_MAPS */
David Rientjesd906f0e2010-12-30 10:54:16 -0800761static void __cpuinit numa_set_cpumask(int cpu, int enable)
762{
763 int node = early_cpu_to_node(cpu);
764 struct cpumask *mask;
David Rientjesc1c34432010-12-22 17:23:54 -0800765 int i;
Brian Gerst6470aff2009-01-27 12:56:47 +0900766
David Rientjes14392fd2011-02-07 14:08:53 -0800767 if (node == NUMA_NO_NODE) {
768 /* early_cpu_to_node() already emits a warning and trace */
769 return;
770 }
David Rientjesc1c34432010-12-22 17:23:54 -0800771 for_each_online_node(i) {
772 unsigned long addr;
773
774 addr = node_start_pfn(i) << PAGE_SHIFT;
775 if (addr < physnodes[node].start ||
776 addr >= physnodes[node].end)
777 continue;
David Rientjesd906f0e2010-12-30 10:54:16 -0800778 mask = debug_cpumask_set_cpu(cpu, enable);
779 if (!mask)
David Rientjesc1c34432010-12-22 17:23:54 -0800780 return;
David Rientjesc1c34432010-12-22 17:23:54 -0800781
782 if (enable)
783 cpumask_set_cpu(cpu, mask);
784 else
785 cpumask_clear_cpu(cpu, mask);
Brian Gerst6470aff2009-01-27 12:56:47 +0900786 }
Brian Gerst6470aff2009-01-27 12:56:47 +0900787}
788
789void __cpuinit numa_add_cpu(int cpu)
790{
791 numa_set_cpumask(cpu, 1);
792}
793
794void __cpuinit numa_remove_cpu(int cpu)
795{
796 numa_set_cpumask(cpu, 0);
797}
Tejun Heode2d9442011-01-23 14:37:41 +0100798# endif /* !CONFIG_DEBUG_PER_CPU_MAPS */
799#endif /* CONFIG_NUMA_EMU */