blob: c984e3431cc7d27181418848d28bce054ca1abff [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
Eric Dumazetdcf36bf2006-03-25 16:31:46 +010028struct memnode memnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Thomas Gleixner864fc312008-05-12 15:43:36 +020030static unsigned long __initdata nodemap_addr;
31static unsigned long __initdata nodemap_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Brian Gerst6470aff2009-01-27 12:56:47 +090033/*
Eric Dumazet529a3402005-11-05 17:25:54 +010034 * Given a shift value, try to populate memnodemap[]
35 * Returns :
36 * 1 if OK
37 * 0 if memnodmap[] too small (of shift too small)
38 * -1 if node overlap or lost ram (shift too big)
39 */
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010040static int __init populate_memnodemap(const struct bootnode *nodes,
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -070041 int numnodes, int shift, int *nodeids)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Eric Dumazet529a3402005-11-05 17:25:54 +010043 unsigned long addr, end;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010044 int i, res = -1;
Keith Manntheyb6846642005-07-28 21:15:38 -070045
travis@sgi.com43238382008-01-30 13:33:25 +010046 memset(memnodemap, 0xff, sizeof(s16)*memnodemapsize);
Eric Dumazet529a3402005-11-05 17:25:54 +010047 for (i = 0; i < numnodes; i++) {
48 addr = nodes[i].start;
49 end = nodes[i].end;
50 if (addr >= end)
51 continue;
Amul Shah076422d2007-02-13 13:26:19 +010052 if ((end >> shift) >= memnodemapsize)
Eric Dumazet529a3402005-11-05 17:25:54 +010053 return 0;
54 do {
travis@sgi.com43238382008-01-30 13:33:25 +010055 if (memnodemap[addr >> shift] != NUMA_NO_NODE)
Eric Dumazet529a3402005-11-05 17:25:54 +010056 return -1;
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -070057
58 if (!nodeids)
59 memnodemap[addr >> shift] = i;
60 else
61 memnodemap[addr >> shift] = nodeids[i];
62
Amul Shah076422d2007-02-13 13:26:19 +010063 addr += (1UL << shift);
Eric Dumazet529a3402005-11-05 17:25:54 +010064 } while (addr < end);
65 res = 1;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010066 }
Eric Dumazet529a3402005-11-05 17:25:54 +010067 return res;
68}
69
Amul Shah076422d2007-02-13 13:26:19 +010070static int __init allocate_cachealigned_memnodemap(void)
71{
Yinghai Lu24a5da72008-02-01 17:49:41 +010072 unsigned long addr;
Amul Shah076422d2007-02-13 13:26:19 +010073
74 memnodemap = memnode.embedded_map;
travis@sgi.com316390b2008-01-30 13:33:15 +010075 if (memnodemapsize <= ARRAY_SIZE(memnode.embedded_map))
Amul Shah076422d2007-02-13 13:26:19 +010076 return 0;
Amul Shah076422d2007-02-13 13:26:19 +010077
Yinghai Lu24a5da72008-02-01 17:49:41 +010078 addr = 0x8000;
Joerg Roedelbe3e89e2008-07-25 16:48:58 +020079 nodemap_size = roundup(sizeof(s16) * memnodemapsize, L1_CACHE_BYTES);
Yinghai Ludbef7b52010-12-27 16:48:08 -080080 nodemap_addr = memblock_find_in_range(addr, get_max_mapped(),
Yinghai Lu24a5da72008-02-01 17:49:41 +010081 nodemap_size, L1_CACHE_BYTES);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070082 if (nodemap_addr == MEMBLOCK_ERROR) {
Amul Shah076422d2007-02-13 13:26:19 +010083 printk(KERN_ERR
84 "NUMA: Unable to allocate Memory to Node hash map\n");
85 nodemap_addr = nodemap_size = 0;
86 return -1;
87 }
Yinghai Lu24a5da72008-02-01 17:49:41 +010088 memnodemap = phys_to_virt(nodemap_addr);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070089 memblock_x86_reserve_range(nodemap_addr, nodemap_addr + nodemap_size, "MEMNODEMAP");
Amul Shah076422d2007-02-13 13:26:19 +010090
91 printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n",
92 nodemap_addr, nodemap_addr + nodemap_size);
93 return 0;
94}
95
96/*
97 * The LSB of all start and end addresses in the node map is the value of the
98 * maximum possible shift.
99 */
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100100static int __init extract_lsb_from_nodes(const struct bootnode *nodes,
101 int numnodes)
Amul Shah076422d2007-02-13 13:26:19 +0100102{
Amul Shah54413922007-02-13 13:26:20 +0100103 int i, nodes_used = 0;
Amul Shah076422d2007-02-13 13:26:19 +0100104 unsigned long start, end;
105 unsigned long bitfield = 0, memtop = 0;
106
107 for (i = 0; i < numnodes; i++) {
108 start = nodes[i].start;
109 end = nodes[i].end;
110 if (start >= end)
111 continue;
Amul Shah54413922007-02-13 13:26:20 +0100112 bitfield |= start;
113 nodes_used++;
Amul Shah076422d2007-02-13 13:26:19 +0100114 if (end > memtop)
115 memtop = end;
116 }
Amul Shah54413922007-02-13 13:26:20 +0100117 if (nodes_used <= 1)
118 i = 63;
119 else
120 i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
Amul Shah076422d2007-02-13 13:26:19 +0100121 memnodemapsize = (memtop >> i)+1;
122 return i;
123}
124
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700125int __init compute_hash_shift(struct bootnode *nodes, int numnodes,
126 int *nodeids)
Eric Dumazet529a3402005-11-05 17:25:54 +0100127{
Amul Shah076422d2007-02-13 13:26:19 +0100128 int shift;
Eric Dumazet529a3402005-11-05 17:25:54 +0100129
Amul Shah076422d2007-02-13 13:26:19 +0100130 shift = extract_lsb_from_nodes(nodes, numnodes);
131 if (allocate_cachealigned_memnodemap())
132 return -1;
Andi Kleen6b050f82006-01-11 22:44:33 +0100133 printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
Eric Dumazet529a3402005-11-05 17:25:54 +0100134 shift);
135
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700136 if (populate_memnodemap(nodes, numnodes, shift, nodeids) != 1) {
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100137 printk(KERN_INFO "Your memory is not aligned you need to "
138 "rebuild your kernel with a bigger NODEMAPSIZE "
139 "shift=%d\n", shift);
Eric Dumazet529a3402005-11-05 17:25:54 +0100140 return -1;
141 }
Keith Manntheyb6846642005-07-28 21:15:38 -0700142 return shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -0800145int __meminit __early_pfn_to_nid(unsigned long pfn)
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700146{
147 return phys_to_nid(pfn << PAGE_SHIFT);
148}
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700149
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100150static void * __init early_node_mem(int nodeid, unsigned long start,
Yinghai Lu24a5da72008-02-01 17:49:41 +0100151 unsigned long end, unsigned long size,
152 unsigned long align)
Andi Kleena8062232006-04-07 19:49:21 +0200153{
Yinghai Lucef625e2010-02-10 01:20:18 -0800154 unsigned long mem;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100155
Yinghai Lucef625e2010-02-10 01:20:18 -0800156 /*
157 * put it on high as possible
158 * something will go with NODE_DATA
159 */
160 if (start < (MAX_DMA_PFN<<PAGE_SHIFT))
161 start = MAX_DMA_PFN<<PAGE_SHIFT;
162 if (start < (MAX_DMA32_PFN<<PAGE_SHIFT) &&
163 end > (MAX_DMA32_PFN<<PAGE_SHIFT))
164 start = MAX_DMA32_PFN<<PAGE_SHIFT;
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700165 mem = memblock_x86_find_in_range_node(nodeid, start, end, size, align);
166 if (mem != MEMBLOCK_ERROR)
Andi Kleena8062232006-04-07 19:49:21 +0200167 return __va(mem);
Yinghai Lu9347e0b2008-02-01 17:49:42 +0100168
Yinghai Lucef625e2010-02-10 01:20:18 -0800169 /* extend the search scope */
170 end = max_pfn_mapped << PAGE_SHIFT;
Yinghai Lu419db272010-10-28 09:50:17 -0700171 start = MAX_DMA_PFN << PAGE_SHIFT;
172 mem = memblock_find_in_range(start, end, size, align);
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700173 if (mem != MEMBLOCK_ERROR)
Yinghai Lu1842f902010-02-10 01:20:15 -0800174 return __va(mem);
175
176 printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100177 size, nodeid);
Yinghai Lu1842f902010-02-10 01:20:15 -0800178
179 return NULL;
Andi Kleena8062232006-04-07 19:49:21 +0200180}
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/* Initialize bootmem allocator for a node */
Yinghai Lu7c437692009-05-15 13:59:37 -0700183void __init
184setup_node_bootmem(int nodeid, unsigned long start, unsigned long end)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100185{
Yinghai Lu08677212010-02-10 01:20:20 -0800186 unsigned long start_pfn, last_pfn, nodedata_phys;
Yinghai Lu7c437692009-05-15 13:59:37 -0700187 const int pgdat_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
Yinghai Lu1a27fc02008-03-18 12:52:37 -0700188 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Yinghai Lu4c31e922009-04-22 14:19:27 -0700190 if (!end)
191 return;
192
Yinghai Lu7c437692009-05-15 13:59:37 -0700193 /*
194 * Don't confuse VM with a node that doesn't have the
195 * minimum amount of memory:
196 */
197 if (end && (end - start) < NODE_MIN_SIZE)
198 return;
199
Joerg Roedelbe3e89e2008-07-25 16:48:58 +0200200 start = roundup(start, ZONE_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Yinghai Lu08677212010-02-10 01:20:20 -0800202 printk(KERN_INFO "Initmem setup node %d %016lx-%016lx\n", nodeid,
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100203 start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 start_pfn = start >> PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200206 last_pfn = end >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Yinghai Lu24a5da72008-02-01 17:49:41 +0100208 node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
209 SMP_CACHE_BYTES);
Andi Kleena8062232006-04-07 19:49:21 +0200210 if (node_data[nodeid] == NULL)
211 return;
212 nodedata_phys = __pa(node_data[nodeid]);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700213 memblock_x86_reserve_range(nodedata_phys, nodedata_phys + pgdat_size, "NODE_DATA");
Yinghai Lu6118f762008-02-04 16:47:56 +0100214 printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
215 nodedata_phys + pgdat_size - 1);
Yinghai Lu1842f902010-02-10 01:20:15 -0800216 nid = phys_to_nid(nodedata_phys);
217 if (nid != nodeid)
218 printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nodeid, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
Yinghai Lu08677212010-02-10 01:20:20 -0800221 NODE_DATA(nodeid)->node_id = nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 NODE_DATA(nodeid)->node_start_pfn = start_pfn;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200223 NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 node_set_online(nodeid);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100226}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228#ifdef CONFIG_NUMA_EMU
Rohit Seth53fee042007-02-13 13:26:22 +0100229/* Numa emulation */
David Rientjesadc19382009-09-25 15:20:09 -0700230static struct bootnode nodes[MAX_NUMNODES] __initdata;
David Rientjesc1c34432010-12-22 17:23:54 -0800231static struct bootnode physnodes[MAX_NUMNODES] __cpuinitdata;
Thomas Gleixner864fc312008-05-12 15:43:36 +0200232static char *cmdline __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Jan Beulich90321602011-01-19 08:57:21 +0000234void __init numa_emu_cmdline(char *str)
235{
236 cmdline = str;
237}
238
David Rientjesadc19382009-09-25 15:20:09 -0700239static int __init setup_physnodes(unsigned long start, unsigned long end,
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200240 int acpi, int amd)
David Rientjesadc19382009-09-25 15:20:09 -0700241{
David Rientjesadc19382009-09-25 15:20:09 -0700242 int ret = 0;
243 int i;
244
David Rientjesc1c34432010-12-22 17:23:54 -0800245 memset(physnodes, 0, sizeof(physnodes));
David Rientjesadc19382009-09-25 15:20:09 -0700246#ifdef CONFIG_ACPI_NUMA
247 if (acpi)
David Rientjesa387e952010-12-22 17:23:56 -0800248 acpi_get_nodes(physnodes, start, end);
David Rientjesadc19382009-09-25 15:20:09 -0700249#endif
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200250#ifdef CONFIG_AMD_NUMA
251 if (amd)
David Rientjesa387e952010-12-22 17:23:56 -0800252 amd_get_nodes(physnodes);
David Rientjesadc19382009-09-25 15:20:09 -0700253#endif
254 /*
255 * Basic sanity checking on the physical node map: there may be errors
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200256 * if the SRAT or AMD code incorrectly reported the topology or the mem=
David Rientjesadc19382009-09-25 15:20:09 -0700257 * kernel parameter is used.
258 */
David Rientjesa387e952010-12-22 17:23:56 -0800259 for (i = 0; i < MAX_NUMNODES; i++) {
David Rientjesadc19382009-09-25 15:20:09 -0700260 if (physnodes[i].start == physnodes[i].end)
261 continue;
262 if (physnodes[i].start > end) {
263 physnodes[i].end = physnodes[i].start;
264 continue;
265 }
266 if (physnodes[i].end < start) {
267 physnodes[i].start = physnodes[i].end;
268 continue;
269 }
270 if (physnodes[i].start < start)
271 physnodes[i].start = start;
272 if (physnodes[i].end > end)
273 physnodes[i].end = end;
David Rientjesadc19382009-09-25 15:20:09 -0700274 ret++;
275 }
276
277 /*
278 * If no physical topology was detected, a single node is faked to cover
279 * the entire address space.
280 */
281 if (!ret) {
282 physnodes[ret].start = start;
283 physnodes[ret].end = end;
284 ret = 1;
285 }
286 return ret;
287}
288
David Rientjesf51bf302010-12-22 17:23:51 -0800289static void __init fake_physnodes(int acpi, int amd, int nr_nodes)
290{
291 int i;
292
293 BUG_ON(acpi && amd);
294#ifdef CONFIG_ACPI_NUMA
295 if (acpi)
296 acpi_fake_nodes(nodes, nr_nodes);
297#endif
298#ifdef CONFIG_AMD_NUMA
299 if (amd)
300 amd_fake_nodes(nodes, nr_nodes);
301#endif
302 if (!acpi && !amd)
303 for (i = 0; i < nr_cpu_ids; i++)
304 numa_set_node(i, 0);
305}
306
Rohit Seth53fee042007-02-13 13:26:22 +0100307/*
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100308 * Setups up nid to range from addr to addr + size. If the end
309 * boundary is greater than max_addr, then max_addr is used instead.
310 * The return value is 0 if there is additional memory left for
311 * allocation past addr and -1 otherwise. addr is adjusted to be at
312 * the end of the node.
Rohit Seth53fee042007-02-13 13:26:22 +0100313 */
David Rientjesadc19382009-09-25 15:20:09 -0700314static int __init setup_node_range(int nid, u64 *addr, u64 size, u64 max_addr)
Rohit Seth53fee042007-02-13 13:26:22 +0100315{
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200316 int ret = 0;
317 nodes[nid].start = *addr;
318 *addr += size;
319 if (*addr >= max_addr) {
320 *addr = max_addr;
321 ret = -1;
322 }
323 nodes[nid].end = *addr;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200324 node_set(nid, node_possible_map);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200325 printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
326 nodes[nid].start, nodes[nid].end,
327 (nodes[nid].end - nodes[nid].start) >> 20);
328 return ret;
Rohit Seth53fee042007-02-13 13:26:22 +0100329}
330
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200331/*
David Rientjesadc19382009-09-25 15:20:09 -0700332 * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
333 * to max_addr. The return value is the number of nodes allocated.
334 */
David Rientjesc1c34432010-12-22 17:23:54 -0800335static int __init split_nodes_interleave(u64 addr, u64 max_addr, int nr_nodes)
David Rientjesadc19382009-09-25 15:20:09 -0700336{
337 nodemask_t physnode_mask = NODE_MASK_NONE;
338 u64 size;
339 int big;
340 int ret = 0;
341 int i;
342
343 if (nr_nodes <= 0)
344 return -1;
345 if (nr_nodes > MAX_NUMNODES) {
346 pr_info("numa=fake=%d too large, reducing to %d\n",
347 nr_nodes, MAX_NUMNODES);
348 nr_nodes = MAX_NUMNODES;
349 }
350
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700351 size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) / nr_nodes;
David Rientjesadc19382009-09-25 15:20:09 -0700352 /*
353 * Calculate the number of big nodes that can be allocated as a result
354 * of consolidating the remainder.
355 */
David Rientjes68fd1112010-02-15 13:43:25 -0800356 big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
David Rientjesadc19382009-09-25 15:20:09 -0700357 FAKE_NODE_MIN_SIZE;
358
359 size &= FAKE_NODE_MIN_HASH_MASK;
360 if (!size) {
361 pr_err("Not enough memory for each node. "
362 "NUMA emulation disabled.\n");
363 return -1;
364 }
365
David Rientjesc1c34432010-12-22 17:23:54 -0800366 for (i = 0; i < MAX_NUMNODES; i++)
David Rientjesadc19382009-09-25 15:20:09 -0700367 if (physnodes[i].start != physnodes[i].end)
368 node_set(i, physnode_mask);
369
370 /*
371 * Continue to fill physical nodes with fake nodes until there is no
372 * memory left on any of them.
373 */
374 while (nodes_weight(physnode_mask)) {
375 for_each_node_mask(i, physnode_mask) {
376 u64 end = physnodes[i].start + size;
377 u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
378
379 if (ret < big)
380 end += FAKE_NODE_MIN_SIZE;
381
382 /*
383 * Continue to add memory to this fake node if its
384 * non-reserved memory is less than the per-node size.
385 */
386 while (end - physnodes[i].start -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700387 memblock_x86_hole_size(physnodes[i].start, end) < size) {
David Rientjesadc19382009-09-25 15:20:09 -0700388 end += FAKE_NODE_MIN_SIZE;
389 if (end > physnodes[i].end) {
390 end = physnodes[i].end;
391 break;
392 }
393 }
394
395 /*
396 * If there won't be at least FAKE_NODE_MIN_SIZE of
397 * non-reserved memory in ZONE_DMA32 for the next node,
398 * this one must extend to the boundary.
399 */
400 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700401 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjesadc19382009-09-25 15:20:09 -0700402 end = dma32_end;
403
404 /*
405 * If there won't be enough non-reserved memory for the
406 * next node, this one must extend to the end of the
407 * physical node.
408 */
409 if (physnodes[i].end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700410 memblock_x86_hole_size(end, physnodes[i].end) < size)
David Rientjesadc19382009-09-25 15:20:09 -0700411 end = physnodes[i].end;
412
413 /*
414 * Avoid allocating more nodes than requested, which can
415 * happen as a result of rounding down each node's size
416 * to FAKE_NODE_MIN_SIZE.
417 */
418 if (nodes_weight(physnode_mask) + ret >= nr_nodes)
419 end = physnodes[i].end;
420
421 if (setup_node_range(ret++, &physnodes[i].start,
422 end - physnodes[i].start,
423 physnodes[i].end) < 0)
424 node_clear(i, physnode_mask);
425 }
426 }
427 return ret;
428}
429
430/*
David Rientjes8df5bb32010-02-15 13:43:30 -0800431 * Returns the end address of a node so that there is at least `size' amount of
432 * non-reserved memory or `max_addr' is reached.
433 */
434static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
435{
436 u64 end = start + size;
437
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700438 while (end - start - memblock_x86_hole_size(start, end) < size) {
David Rientjes8df5bb32010-02-15 13:43:30 -0800439 end += FAKE_NODE_MIN_SIZE;
440 if (end > max_addr) {
441 end = max_addr;
442 break;
443 }
444 }
445 return end;
446}
447
448/*
449 * Sets up fake nodes of `size' interleaved over physical nodes ranging from
450 * `addr' to `max_addr'. The return value is the number of nodes allocated.
451 */
452static int __init split_nodes_size_interleave(u64 addr, u64 max_addr, u64 size)
453{
454 nodemask_t physnode_mask = NODE_MASK_NONE;
455 u64 min_size;
456 int ret = 0;
457 int i;
458
459 if (!size)
460 return -1;
461 /*
462 * The limit on emulated nodes is MAX_NUMNODES, so the size per node is
463 * increased accordingly if the requested size is too small. This
464 * creates a uniform distribution of node sizes across the entire
465 * machine (but not necessarily over physical nodes).
466 */
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700467 min_size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) /
David Rientjes8df5bb32010-02-15 13:43:30 -0800468 MAX_NUMNODES;
469 min_size = max(min_size, FAKE_NODE_MIN_SIZE);
470 if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size)
471 min_size = (min_size + FAKE_NODE_MIN_SIZE) &
472 FAKE_NODE_MIN_HASH_MASK;
473 if (size < min_size) {
474 pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
475 size >> 20, min_size >> 20);
476 size = min_size;
477 }
478 size &= FAKE_NODE_MIN_HASH_MASK;
479
480 for (i = 0; i < MAX_NUMNODES; i++)
481 if (physnodes[i].start != physnodes[i].end)
482 node_set(i, physnode_mask);
483 /*
484 * Fill physical nodes with fake nodes of size until there is no memory
485 * left on any of them.
486 */
487 while (nodes_weight(physnode_mask)) {
488 for_each_node_mask(i, physnode_mask) {
489 u64 dma32_end = MAX_DMA32_PFN << PAGE_SHIFT;
490 u64 end;
491
492 end = find_end_of_node(physnodes[i].start,
493 physnodes[i].end, size);
494 /*
495 * If there won't be at least FAKE_NODE_MIN_SIZE of
496 * non-reserved memory in ZONE_DMA32 for the next node,
497 * this one must extend to the boundary.
498 */
499 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700500 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjes8df5bb32010-02-15 13:43:30 -0800501 end = dma32_end;
502
503 /*
504 * If there won't be enough non-reserved memory for the
505 * next node, this one must extend to the end of the
506 * physical node.
507 */
508 if (physnodes[i].end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700509 memblock_x86_hole_size(end, physnodes[i].end) < size)
David Rientjes8df5bb32010-02-15 13:43:30 -0800510 end = physnodes[i].end;
511
512 /*
513 * Setup the fake node that will be allocated as bootmem
514 * later. If setup_node_range() returns non-zero, there
515 * is no more memory available on this physical node.
516 */
517 if (setup_node_range(ret++, &physnodes[i].start,
518 end - physnodes[i].start,
519 physnodes[i].end) < 0)
520 node_clear(i, physnode_mask);
521 }
522 }
523 return ret;
524}
525
526/*
Thomas Gleixner886533a2008-05-12 15:43:36 +0200527 * Sets up the system RAM area from start_pfn to last_pfn according to the
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200528 * numa=fake command-line option.
529 */
David Rientjesadc19382009-09-25 15:20:09 -0700530static int __init numa_emulation(unsigned long start_pfn,
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200531 unsigned long last_pfn, int acpi, int amd)
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200532{
David Rientjesca2107c2010-02-15 13:43:33 -0800533 u64 addr = start_pfn << PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200534 u64 max_addr = last_pfn << PAGE_SHIFT;
David Rientjesca2107c2010-02-15 13:43:33 -0800535 int num_nodes;
536 int i;
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200537
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200538 /*
David Rientjes8df5bb32010-02-15 13:43:30 -0800539 * If the numa=fake command-line contains a 'M' or 'G', it represents
David Rientjesca2107c2010-02-15 13:43:33 -0800540 * the fixed node size. Otherwise, if it is just a single number N,
541 * split the system RAM into N fake nodes.
David Rientjes8df5bb32010-02-15 13:43:30 -0800542 */
543 if (strchr(cmdline, 'M') || strchr(cmdline, 'G')) {
David Rientjesca2107c2010-02-15 13:43:33 -0800544 u64 size;
545
David Rientjes8df5bb32010-02-15 13:43:30 -0800546 size = memparse(cmdline, &cmdline);
547 num_nodes = split_nodes_size_interleave(addr, max_addr, size);
David Rientjesca2107c2010-02-15 13:43:33 -0800548 } else {
549 unsigned long n;
550
551 n = simple_strtoul(cmdline, NULL, 0);
David Rientjesc1c34432010-12-22 17:23:54 -0800552 num_nodes = split_nodes_interleave(addr, max_addr, n);
David Rientjes8df5bb32010-02-15 13:43:30 -0800553 }
554
David Rientjesca2107c2010-02-15 13:43:33 -0800555 if (num_nodes < 0)
556 return num_nodes;
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700557 memnode_shift = compute_hash_shift(nodes, num_nodes, NULL);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200558 if (memnode_shift < 0) {
559 memnode_shift = 0;
560 printk(KERN_ERR "No NUMA hash function found. NUMA emulation "
561 "disabled.\n");
562 return -1;
563 }
564
565 /*
David Rientjesadc19382009-09-25 15:20:09 -0700566 * We need to vacate all active ranges that may have been registered for
567 * the e820 memory map.
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200568 */
569 remove_all_active_ranges();
Yinghai Lu1411e0e2010-12-27 16:48:17 -0800570 for_each_node_mask(i, node_possible_map)
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700571 memblock_x86_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
Mel Gorman5cb248a2006-09-27 01:49:52 -0700572 nodes[i].end >> PAGE_SHIFT);
Yinghai Lu1411e0e2010-12-27 16:48:17 -0800573 init_memory_mapping_high();
574 for_each_node_mask(i, node_possible_map)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100575 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
David Rientjesc1c34432010-12-22 17:23:54 -0800576 setup_physnodes(addr, max_addr, acpi, amd);
David Rientjesf51bf302010-12-22 17:23:51 -0800577 fake_physnodes(acpi, amd, num_nodes);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100578 numa_init_array();
579 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200581#endif /* CONFIG_NUMA_EMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Tejun Heoffe77a42011-02-16 12:13:06 +0100583static int dummy_numa_init(void)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100584{
Tejun Heoffe77a42011-02-16 12:13:06 +0100585 return 0;
586}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Tejun Heoffe77a42011-02-16 12:13:06 +0100588static int dummy_scan_nodes(void)
589{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 printk(KERN_INFO "%s\n",
591 numa_off ? "NUMA turned off" : "No NUMA configuration found");
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100592 printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
Tejun Heo86ef4db2011-02-16 12:13:06 +0100593 0LU, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100594
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100595 /* setup dummy node covering all memory */
596 memnode_shift = 63;
Amul Shah076422d2007-02-13 13:26:19 +0100597 memnodemap = memnode.embedded_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 memnodemap[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 node_set_online(0);
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200600 node_set(0, node_possible_map);
Tejun Heo86ef4db2011-02-16 12:13:06 +0100601 memblock_x86_register_active_regions(0, 0, max_pfn);
Yinghai Lu1411e0e2010-12-27 16:48:17 -0800602 init_memory_mapping_high();
Tejun Heo86ef4db2011-02-16 12:13:06 +0100603 setup_node_bootmem(0, 0, max_pfn << PAGE_SHIFT);
Tejun Heo7d36b7b2011-02-16 12:13:06 +0100604 numa_init_array();
Tejun Heoffe77a42011-02-16 12:13:06 +0100605
606 return 0;
607}
608
609void __init initmem_init(void)
610{
611 int (*numa_init[])(void) = { [2] = dummy_numa_init };
612 int (*scan_nodes[])(void) = { [2] = dummy_scan_nodes };
613 int i, j;
614
615 if (!numa_off) {
616#ifdef CONFIG_ACPI_NUMA
617 numa_init[0] = x86_acpi_numa_init;
618 scan_nodes[0] = acpi_scan_nodes;
619#endif
620#ifdef CONFIG_AMD_NUMA
621 numa_init[1] = amd_numa_init;
622 scan_nodes[1] = amd_scan_nodes;
623#endif
624 }
625
626 for (i = 0; i < ARRAY_SIZE(numa_init); i++) {
627 if (!numa_init[i])
628 continue;
629
630 for (j = 0; j < MAX_LOCAL_APIC; j++)
631 set_apicid_to_node(j, NUMA_NO_NODE);
632
633 nodes_clear(node_possible_map);
634 nodes_clear(node_online_map);
635
636 if (numa_init[i]() < 0)
637 continue;
638#ifdef CONFIG_NUMA_EMU
639 setup_physnodes(0, max_pfn << PAGE_SHIFT, i == 0, i == 1);
640 if (cmdline && !numa_emulation(0, max_pfn, i == 0, i == 1))
641 return;
642 setup_physnodes(0, max_pfn << PAGE_SHIFT, i == 0, i == 1);
643 nodes_clear(node_possible_map);
644 nodes_clear(node_online_map);
645#endif
646 if (!scan_nodes[i]())
647 return;
648 }
649 BUG();
Andi Kleen69d81fc2005-11-05 17:25:53 +0100650}
651
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100652unsigned long __init numa_free_all_bootmem(void)
653{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 unsigned long pages = 0;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100655 int i;
656
657 for_each_online_node(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 pages += free_all_bootmem_node(NODE_DATA(i));
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100659
Yinghai Lu08677212010-02-10 01:20:20 -0800660 pages += free_all_memory_core_early(MAX_NUMNODES);
Yinghai Lu08677212010-02-10 01:20:20 -0800661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return pages;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100663}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100665int __cpuinit numa_cpu_node(int cpu)
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800666{
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100667 int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800668
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100669 if (apicid != BAD_APICID)
670 return __apicid_to_node[apicid];
671 return NUMA_NO_NODE;
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800672}
673
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100674/*
Tejun Heode2d9442011-01-23 14:37:41 +0100675 * UGLINESS AHEAD: Currently, CONFIG_NUMA_EMU is 64bit only and makes use
676 * of 64bit specific data structures. The distinction is artificial and
677 * should be removed. numa_{add|remove}_cpu() are implemented in numa.c
678 * for both 32 and 64bit when CONFIG_NUMA_EMU is disabled but here when
679 * enabled.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100680 *
Tejun Heode2d9442011-01-23 14:37:41 +0100681 * NUMA emulation is planned to be made generic and the following and other
682 * related code should be moved to numa.c.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100683 */
Tejun Heode2d9442011-01-23 14:37:41 +0100684#ifdef CONFIG_NUMA_EMU
685# ifndef CONFIG_DEBUG_PER_CPU_MAPS
David Rientjesc1c34432010-12-22 17:23:54 -0800686void __cpuinit numa_add_cpu(int cpu)
687{
688 unsigned long addr;
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100689 int physnid, nid;
David Rientjesc1c34432010-12-22 17:23:54 -0800690
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100691 nid = numa_cpu_node(cpu);
David Rientjesc1c34432010-12-22 17:23:54 -0800692 if (nid == NUMA_NO_NODE)
693 nid = early_cpu_to_node(cpu);
694 BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
695
696 /*
697 * Use the starting address of the emulated node to find which physical
698 * node it is allocated on.
699 */
700 addr = node_start_pfn(nid) << PAGE_SHIFT;
701 for (physnid = 0; physnid < MAX_NUMNODES; physnid++)
702 if (addr >= physnodes[physnid].start &&
703 addr < physnodes[physnid].end)
704 break;
705
706 /*
707 * Map the cpu to each emulated node that is allocated on the physical
708 * node of the cpu's apic id.
709 */
710 for_each_online_node(nid) {
711 addr = node_start_pfn(nid) << PAGE_SHIFT;
712 if (addr >= physnodes[physnid].start &&
713 addr < physnodes[physnid].end)
714 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
715 }
716}
717
718void __cpuinit numa_remove_cpu(int cpu)
719{
720 int i;
721
722 for_each_online_node(i)
723 cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
724}
Tejun Heode2d9442011-01-23 14:37:41 +0100725# else /* !CONFIG_DEBUG_PER_CPU_MAPS */
David Rientjesd906f0e2010-12-30 10:54:16 -0800726static void __cpuinit numa_set_cpumask(int cpu, int enable)
727{
728 int node = early_cpu_to_node(cpu);
729 struct cpumask *mask;
David Rientjesc1c34432010-12-22 17:23:54 -0800730 int i;
Brian Gerst6470aff2009-01-27 12:56:47 +0900731
David Rientjes14392fd2011-02-07 14:08:53 -0800732 if (node == NUMA_NO_NODE) {
733 /* early_cpu_to_node() already emits a warning and trace */
734 return;
735 }
David Rientjesc1c34432010-12-22 17:23:54 -0800736 for_each_online_node(i) {
737 unsigned long addr;
738
739 addr = node_start_pfn(i) << PAGE_SHIFT;
740 if (addr < physnodes[node].start ||
741 addr >= physnodes[node].end)
742 continue;
David Rientjesd906f0e2010-12-30 10:54:16 -0800743 mask = debug_cpumask_set_cpu(cpu, enable);
744 if (!mask)
David Rientjesc1c34432010-12-22 17:23:54 -0800745 return;
David Rientjesc1c34432010-12-22 17:23:54 -0800746
747 if (enable)
748 cpumask_set_cpu(cpu, mask);
749 else
750 cpumask_clear_cpu(cpu, mask);
Brian Gerst6470aff2009-01-27 12:56:47 +0900751 }
Brian Gerst6470aff2009-01-27 12:56:47 +0900752}
753
754void __cpuinit numa_add_cpu(int cpu)
755{
756 numa_set_cpumask(cpu, 1);
757}
758
759void __cpuinit numa_remove_cpu(int cpu)
760{
761 numa_set_cpumask(cpu, 0);
762}
Tejun Heode2d9442011-01-23 14:37:41 +0100763# endif /* !CONFIG_DEBUG_PER_CPU_MAPS */
764#endif /* CONFIG_NUMA_EMU */