blob: f548fbf75f44b75c297f5bfa777ed235b623f60d [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#include <asm/e820.h>
18#include <asm/proto.h>
19#include <asm/dma.h>
20#include <asm/numa.h>
21#include <asm/acpi.h>
Andreas Herrmann23ac4ae2010-09-17 18:03:43 +020022#include <asm/amd_nb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -070024struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010025EXPORT_SYMBOL(node_data);
26
Eric Dumazetdcf36bf2006-03-25 16:31:46 +010027struct memnode memnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Thomas Gleixner864fc312008-05-12 15:43:36 +020029static unsigned long __initdata nodemap_addr;
30static unsigned long __initdata nodemap_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Brian Gerst6470aff2009-01-27 12:56:47 +090032/*
Eric Dumazet529a3402005-11-05 17:25:54 +010033 * Given a shift value, try to populate memnodemap[]
34 * Returns :
35 * 1 if OK
36 * 0 if memnodmap[] too small (of shift too small)
37 * -1 if node overlap or lost ram (shift too big)
38 */
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010039static int __init populate_memnodemap(const struct bootnode *nodes,
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -070040 int numnodes, int shift, int *nodeids)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Eric Dumazet529a3402005-11-05 17:25:54 +010042 unsigned long addr, end;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010043 int i, res = -1;
Keith Manntheyb6846642005-07-28 21:15:38 -070044
travis@sgi.com43238382008-01-30 13:33:25 +010045 memset(memnodemap, 0xff, sizeof(s16)*memnodemapsize);
Eric Dumazet529a3402005-11-05 17:25:54 +010046 for (i = 0; i < numnodes; i++) {
47 addr = nodes[i].start;
48 end = nodes[i].end;
49 if (addr >= end)
50 continue;
Amul Shah076422d2007-02-13 13:26:19 +010051 if ((end >> shift) >= memnodemapsize)
Eric Dumazet529a3402005-11-05 17:25:54 +010052 return 0;
53 do {
travis@sgi.com43238382008-01-30 13:33:25 +010054 if (memnodemap[addr >> shift] != NUMA_NO_NODE)
Eric Dumazet529a3402005-11-05 17:25:54 +010055 return -1;
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -070056
57 if (!nodeids)
58 memnodemap[addr >> shift] = i;
59 else
60 memnodemap[addr >> shift] = nodeids[i];
61
Amul Shah076422d2007-02-13 13:26:19 +010062 addr += (1UL << shift);
Eric Dumazet529a3402005-11-05 17:25:54 +010063 } while (addr < end);
64 res = 1;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010065 }
Eric Dumazet529a3402005-11-05 17:25:54 +010066 return res;
67}
68
Amul Shah076422d2007-02-13 13:26:19 +010069static int __init allocate_cachealigned_memnodemap(void)
70{
Yinghai Lu24a5da72008-02-01 17:49:41 +010071 unsigned long addr;
Amul Shah076422d2007-02-13 13:26:19 +010072
73 memnodemap = memnode.embedded_map;
travis@sgi.com316390b2008-01-30 13:33:15 +010074 if (memnodemapsize <= ARRAY_SIZE(memnode.embedded_map))
Amul Shah076422d2007-02-13 13:26:19 +010075 return 0;
Amul Shah076422d2007-02-13 13:26:19 +010076
Yinghai Lu24a5da72008-02-01 17:49:41 +010077 addr = 0x8000;
Joerg Roedelbe3e89e2008-07-25 16:48:58 +020078 nodemap_size = roundup(sizeof(s16) * memnodemapsize, L1_CACHE_BYTES);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070079 nodemap_addr = memblock_find_in_range(addr, max_pfn<<PAGE_SHIFT,
Yinghai Lu24a5da72008-02-01 17:49:41 +010080 nodemap_size, L1_CACHE_BYTES);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070081 if (nodemap_addr == MEMBLOCK_ERROR) {
Amul Shah076422d2007-02-13 13:26:19 +010082 printk(KERN_ERR
83 "NUMA: Unable to allocate Memory to Node hash map\n");
84 nodemap_addr = nodemap_size = 0;
85 return -1;
86 }
Yinghai Lu24a5da72008-02-01 17:49:41 +010087 memnodemap = phys_to_virt(nodemap_addr);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070088 memblock_x86_reserve_range(nodemap_addr, nodemap_addr + nodemap_size, "MEMNODEMAP");
Amul Shah076422d2007-02-13 13:26:19 +010089
90 printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n",
91 nodemap_addr, nodemap_addr + nodemap_size);
92 return 0;
93}
94
95/*
96 * The LSB of all start and end addresses in the node map is the value of the
97 * maximum possible shift.
98 */
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010099static int __init extract_lsb_from_nodes(const struct bootnode *nodes,
100 int numnodes)
Amul Shah076422d2007-02-13 13:26:19 +0100101{
Amul Shah54413922007-02-13 13:26:20 +0100102 int i, nodes_used = 0;
Amul Shah076422d2007-02-13 13:26:19 +0100103 unsigned long start, end;
104 unsigned long bitfield = 0, memtop = 0;
105
106 for (i = 0; i < numnodes; i++) {
107 start = nodes[i].start;
108 end = nodes[i].end;
109 if (start >= end)
110 continue;
Amul Shah54413922007-02-13 13:26:20 +0100111 bitfield |= start;
112 nodes_used++;
Amul Shah076422d2007-02-13 13:26:19 +0100113 if (end > memtop)
114 memtop = end;
115 }
Amul Shah54413922007-02-13 13:26:20 +0100116 if (nodes_used <= 1)
117 i = 63;
118 else
119 i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
Amul Shah076422d2007-02-13 13:26:19 +0100120 memnodemapsize = (memtop >> i)+1;
121 return i;
122}
123
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700124int __init compute_hash_shift(struct bootnode *nodes, int numnodes,
125 int *nodeids)
Eric Dumazet529a3402005-11-05 17:25:54 +0100126{
Amul Shah076422d2007-02-13 13:26:19 +0100127 int shift;
Eric Dumazet529a3402005-11-05 17:25:54 +0100128
Amul Shah076422d2007-02-13 13:26:19 +0100129 shift = extract_lsb_from_nodes(nodes, numnodes);
130 if (allocate_cachealigned_memnodemap())
131 return -1;
Andi Kleen6b050f82006-01-11 22:44:33 +0100132 printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
Eric Dumazet529a3402005-11-05 17:25:54 +0100133 shift);
134
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700135 if (populate_memnodemap(nodes, numnodes, shift, nodeids) != 1) {
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100136 printk(KERN_INFO "Your memory is not aligned you need to "
137 "rebuild your kernel with a bigger NODEMAPSIZE "
138 "shift=%d\n", shift);
Eric Dumazet529a3402005-11-05 17:25:54 +0100139 return -1;
140 }
Keith Manntheyb6846642005-07-28 21:15:38 -0700141 return shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -0800144int __meminit __early_pfn_to_nid(unsigned long pfn)
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700145{
146 return phys_to_nid(pfn << PAGE_SHIFT);
147}
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700148
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100149static void * __init early_node_mem(int nodeid, unsigned long start,
Yinghai Lu24a5da72008-02-01 17:49:41 +0100150 unsigned long end, unsigned long size,
151 unsigned long align)
Andi Kleena8062232006-04-07 19:49:21 +0200152{
Yinghai Lucef625e2010-02-10 01:20:18 -0800153 unsigned long mem;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100154
Yinghai Lucef625e2010-02-10 01:20:18 -0800155 /*
156 * put it on high as possible
157 * something will go with NODE_DATA
158 */
159 if (start < (MAX_DMA_PFN<<PAGE_SHIFT))
160 start = MAX_DMA_PFN<<PAGE_SHIFT;
161 if (start < (MAX_DMA32_PFN<<PAGE_SHIFT) &&
162 end > (MAX_DMA32_PFN<<PAGE_SHIFT))
163 start = MAX_DMA32_PFN<<PAGE_SHIFT;
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700164 mem = memblock_x86_find_in_range_node(nodeid, start, end, size, align);
165 if (mem != MEMBLOCK_ERROR)
Andi Kleena8062232006-04-07 19:49:21 +0200166 return __va(mem);
Yinghai Lu9347e0b2008-02-01 17:49:42 +0100167
Yinghai Lucef625e2010-02-10 01:20:18 -0800168 /* extend the search scope */
169 end = max_pfn_mapped << PAGE_SHIFT;
Yinghai Lu419db272010-10-28 09:50:17 -0700170 start = MAX_DMA_PFN << PAGE_SHIFT;
171 mem = memblock_find_in_range(start, end, size, align);
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700172 if (mem != MEMBLOCK_ERROR)
Yinghai Lu1842f902010-02-10 01:20:15 -0800173 return __va(mem);
174
175 printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100176 size, nodeid);
Yinghai Lu1842f902010-02-10 01:20:15 -0800177
178 return NULL;
Andi Kleena8062232006-04-07 19:49:21 +0200179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/* Initialize bootmem allocator for a node */
Yinghai Lu7c437692009-05-15 13:59:37 -0700182void __init
183setup_node_bootmem(int nodeid, unsigned long start, unsigned long end)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100184{
Yinghai Lu08677212010-02-10 01:20:20 -0800185 unsigned long start_pfn, last_pfn, nodedata_phys;
Yinghai Lu7c437692009-05-15 13:59:37 -0700186 const int pgdat_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
Yinghai Lu1a27fc02008-03-18 12:52:37 -0700187 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Yinghai Lu4c31e922009-04-22 14:19:27 -0700189 if (!end)
190 return;
191
Yinghai Lu7c437692009-05-15 13:59:37 -0700192 /*
193 * Don't confuse VM with a node that doesn't have the
194 * minimum amount of memory:
195 */
196 if (end && (end - start) < NODE_MIN_SIZE)
197 return;
198
Joerg Roedelbe3e89e2008-07-25 16:48:58 +0200199 start = roundup(start, ZONE_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Yinghai Lu08677212010-02-10 01:20:20 -0800201 printk(KERN_INFO "Initmem setup node %d %016lx-%016lx\n", nodeid,
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100202 start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 start_pfn = start >> PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200205 last_pfn = end >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Yinghai Lu24a5da72008-02-01 17:49:41 +0100207 node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
208 SMP_CACHE_BYTES);
Andi Kleena8062232006-04-07 19:49:21 +0200209 if (node_data[nodeid] == NULL)
210 return;
211 nodedata_phys = __pa(node_data[nodeid]);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700212 memblock_x86_reserve_range(nodedata_phys, nodedata_phys + pgdat_size, "NODE_DATA");
Yinghai Lu6118f762008-02-04 16:47:56 +0100213 printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
214 nodedata_phys + pgdat_size - 1);
Yinghai Lu1842f902010-02-10 01:20:15 -0800215 nid = phys_to_nid(nodedata_phys);
216 if (nid != nodeid)
217 printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nodeid, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
Yinghai Lu08677212010-02-10 01:20:20 -0800220 NODE_DATA(nodeid)->node_id = nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 NODE_DATA(nodeid)->node_start_pfn = start_pfn;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200222 NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 node_set_online(nodeid);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100225}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227#ifdef CONFIG_NUMA_EMU
Rohit Seth53fee042007-02-13 13:26:22 +0100228/* Numa emulation */
David Rientjesadc19382009-09-25 15:20:09 -0700229static struct bootnode nodes[MAX_NUMNODES] __initdata;
David Rientjesc1c34432010-12-22 17:23:54 -0800230static struct bootnode physnodes[MAX_NUMNODES] __cpuinitdata;
Thomas Gleixner864fc312008-05-12 15:43:36 +0200231static char *cmdline __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Jan Beulich90321602011-01-19 08:57:21 +0000233void __init numa_emu_cmdline(char *str)
234{
235 cmdline = str;
236}
237
David Rientjesadc19382009-09-25 15:20:09 -0700238static int __init setup_physnodes(unsigned long start, unsigned long end,
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200239 int acpi, int amd)
David Rientjesadc19382009-09-25 15:20:09 -0700240{
David Rientjesadc19382009-09-25 15:20:09 -0700241 int ret = 0;
242 int i;
243
David Rientjesc1c34432010-12-22 17:23:54 -0800244 memset(physnodes, 0, sizeof(physnodes));
David Rientjesadc19382009-09-25 15:20:09 -0700245#ifdef CONFIG_ACPI_NUMA
246 if (acpi)
David Rientjesa387e952010-12-22 17:23:56 -0800247 acpi_get_nodes(physnodes, start, end);
David Rientjesadc19382009-09-25 15:20:09 -0700248#endif
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200249#ifdef CONFIG_AMD_NUMA
250 if (amd)
David Rientjesa387e952010-12-22 17:23:56 -0800251 amd_get_nodes(physnodes);
David Rientjesadc19382009-09-25 15:20:09 -0700252#endif
253 /*
254 * Basic sanity checking on the physical node map: there may be errors
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200255 * if the SRAT or AMD code incorrectly reported the topology or the mem=
David Rientjesadc19382009-09-25 15:20:09 -0700256 * kernel parameter is used.
257 */
David Rientjesa387e952010-12-22 17:23:56 -0800258 for (i = 0; i < MAX_NUMNODES; i++) {
David Rientjesadc19382009-09-25 15:20:09 -0700259 if (physnodes[i].start == physnodes[i].end)
260 continue;
261 if (physnodes[i].start > end) {
262 physnodes[i].end = physnodes[i].start;
263 continue;
264 }
265 if (physnodes[i].end < start) {
266 physnodes[i].start = physnodes[i].end;
267 continue;
268 }
269 if (physnodes[i].start < start)
270 physnodes[i].start = start;
271 if (physnodes[i].end > end)
272 physnodes[i].end = end;
David Rientjesadc19382009-09-25 15:20:09 -0700273 ret++;
274 }
275
276 /*
277 * If no physical topology was detected, a single node is faked to cover
278 * the entire address space.
279 */
280 if (!ret) {
281 physnodes[ret].start = start;
282 physnodes[ret].end = end;
283 ret = 1;
284 }
285 return ret;
286}
287
David Rientjesf51bf302010-12-22 17:23:51 -0800288static void __init fake_physnodes(int acpi, int amd, int nr_nodes)
289{
290 int i;
291
292 BUG_ON(acpi && amd);
293#ifdef CONFIG_ACPI_NUMA
294 if (acpi)
295 acpi_fake_nodes(nodes, nr_nodes);
296#endif
297#ifdef CONFIG_AMD_NUMA
298 if (amd)
299 amd_fake_nodes(nodes, nr_nodes);
300#endif
301 if (!acpi && !amd)
302 for (i = 0; i < nr_cpu_ids; i++)
303 numa_set_node(i, 0);
304}
305
Rohit Seth53fee042007-02-13 13:26:22 +0100306/*
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100307 * Setups up nid to range from addr to addr + size. If the end
308 * boundary is greater than max_addr, then max_addr is used instead.
309 * The return value is 0 if there is additional memory left for
310 * allocation past addr and -1 otherwise. addr is adjusted to be at
311 * the end of the node.
Rohit Seth53fee042007-02-13 13:26:22 +0100312 */
David Rientjesadc19382009-09-25 15:20:09 -0700313static int __init setup_node_range(int nid, u64 *addr, u64 size, u64 max_addr)
Rohit Seth53fee042007-02-13 13:26:22 +0100314{
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200315 int ret = 0;
316 nodes[nid].start = *addr;
317 *addr += size;
318 if (*addr >= max_addr) {
319 *addr = max_addr;
320 ret = -1;
321 }
322 nodes[nid].end = *addr;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200323 node_set(nid, node_possible_map);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200324 printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
325 nodes[nid].start, nodes[nid].end,
326 (nodes[nid].end - nodes[nid].start) >> 20);
327 return ret;
Rohit Seth53fee042007-02-13 13:26:22 +0100328}
329
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200330/*
David Rientjesadc19382009-09-25 15:20:09 -0700331 * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
332 * to max_addr. The return value is the number of nodes allocated.
333 */
David Rientjesc1c34432010-12-22 17:23:54 -0800334static int __init split_nodes_interleave(u64 addr, u64 max_addr, int nr_nodes)
David Rientjesadc19382009-09-25 15:20:09 -0700335{
336 nodemask_t physnode_mask = NODE_MASK_NONE;
337 u64 size;
338 int big;
339 int ret = 0;
340 int i;
341
342 if (nr_nodes <= 0)
343 return -1;
344 if (nr_nodes > MAX_NUMNODES) {
345 pr_info("numa=fake=%d too large, reducing to %d\n",
346 nr_nodes, MAX_NUMNODES);
347 nr_nodes = MAX_NUMNODES;
348 }
349
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700350 size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) / nr_nodes;
David Rientjesadc19382009-09-25 15:20:09 -0700351 /*
352 * Calculate the number of big nodes that can be allocated as a result
353 * of consolidating the remainder.
354 */
David Rientjes68fd1112010-02-15 13:43:25 -0800355 big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
David Rientjesadc19382009-09-25 15:20:09 -0700356 FAKE_NODE_MIN_SIZE;
357
358 size &= FAKE_NODE_MIN_HASH_MASK;
359 if (!size) {
360 pr_err("Not enough memory for each node. "
361 "NUMA emulation disabled.\n");
362 return -1;
363 }
364
David Rientjesc1c34432010-12-22 17:23:54 -0800365 for (i = 0; i < MAX_NUMNODES; i++)
David Rientjesadc19382009-09-25 15:20:09 -0700366 if (physnodes[i].start != physnodes[i].end)
367 node_set(i, physnode_mask);
368
369 /*
370 * Continue to fill physical nodes with fake nodes until there is no
371 * memory left on any of them.
372 */
373 while (nodes_weight(physnode_mask)) {
374 for_each_node_mask(i, physnode_mask) {
375 u64 end = physnodes[i].start + size;
376 u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
377
378 if (ret < big)
379 end += FAKE_NODE_MIN_SIZE;
380
381 /*
382 * Continue to add memory to this fake node if its
383 * non-reserved memory is less than the per-node size.
384 */
385 while (end - physnodes[i].start -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700386 memblock_x86_hole_size(physnodes[i].start, end) < size) {
David Rientjesadc19382009-09-25 15:20:09 -0700387 end += FAKE_NODE_MIN_SIZE;
388 if (end > physnodes[i].end) {
389 end = physnodes[i].end;
390 break;
391 }
392 }
393
394 /*
395 * If there won't be at least FAKE_NODE_MIN_SIZE of
396 * non-reserved memory in ZONE_DMA32 for the next node,
397 * this one must extend to the boundary.
398 */
399 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700400 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjesadc19382009-09-25 15:20:09 -0700401 end = dma32_end;
402
403 /*
404 * If there won't be enough non-reserved memory for the
405 * next node, this one must extend to the end of the
406 * physical node.
407 */
408 if (physnodes[i].end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700409 memblock_x86_hole_size(end, physnodes[i].end) < size)
David Rientjesadc19382009-09-25 15:20:09 -0700410 end = physnodes[i].end;
411
412 /*
413 * Avoid allocating more nodes than requested, which can
414 * happen as a result of rounding down each node's size
415 * to FAKE_NODE_MIN_SIZE.
416 */
417 if (nodes_weight(physnode_mask) + ret >= nr_nodes)
418 end = physnodes[i].end;
419
420 if (setup_node_range(ret++, &physnodes[i].start,
421 end - physnodes[i].start,
422 physnodes[i].end) < 0)
423 node_clear(i, physnode_mask);
424 }
425 }
426 return ret;
427}
428
429/*
David Rientjes8df5bb342010-02-15 13:43:30 -0800430 * Returns the end address of a node so that there is at least `size' amount of
431 * non-reserved memory or `max_addr' is reached.
432 */
433static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
434{
435 u64 end = start + size;
436
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700437 while (end - start - memblock_x86_hole_size(start, end) < size) {
David Rientjes8df5bb342010-02-15 13:43:30 -0800438 end += FAKE_NODE_MIN_SIZE;
439 if (end > max_addr) {
440 end = max_addr;
441 break;
442 }
443 }
444 return end;
445}
446
447/*
448 * Sets up fake nodes of `size' interleaved over physical nodes ranging from
449 * `addr' to `max_addr'. The return value is the number of nodes allocated.
450 */
451static int __init split_nodes_size_interleave(u64 addr, u64 max_addr, u64 size)
452{
453 nodemask_t physnode_mask = NODE_MASK_NONE;
454 u64 min_size;
455 int ret = 0;
456 int i;
457
458 if (!size)
459 return -1;
460 /*
461 * The limit on emulated nodes is MAX_NUMNODES, so the size per node is
462 * increased accordingly if the requested size is too small. This
463 * creates a uniform distribution of node sizes across the entire
464 * machine (but not necessarily over physical nodes).
465 */
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700466 min_size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) /
David Rientjes8df5bb342010-02-15 13:43:30 -0800467 MAX_NUMNODES;
468 min_size = max(min_size, FAKE_NODE_MIN_SIZE);
469 if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size)
470 min_size = (min_size + FAKE_NODE_MIN_SIZE) &
471 FAKE_NODE_MIN_HASH_MASK;
472 if (size < min_size) {
473 pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
474 size >> 20, min_size >> 20);
475 size = min_size;
476 }
477 size &= FAKE_NODE_MIN_HASH_MASK;
478
479 for (i = 0; i < MAX_NUMNODES; i++)
480 if (physnodes[i].start != physnodes[i].end)
481 node_set(i, physnode_mask);
482 /*
483 * Fill physical nodes with fake nodes of size until there is no memory
484 * left on any of them.
485 */
486 while (nodes_weight(physnode_mask)) {
487 for_each_node_mask(i, physnode_mask) {
488 u64 dma32_end = MAX_DMA32_PFN << PAGE_SHIFT;
489 u64 end;
490
491 end = find_end_of_node(physnodes[i].start,
492 physnodes[i].end, size);
493 /*
494 * If there won't be at least FAKE_NODE_MIN_SIZE of
495 * non-reserved memory in ZONE_DMA32 for the next node,
496 * this one must extend to the boundary.
497 */
498 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700499 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjes8df5bb342010-02-15 13:43:30 -0800500 end = dma32_end;
501
502 /*
503 * If there won't be enough non-reserved memory for the
504 * next node, this one must extend to the end of the
505 * physical node.
506 */
507 if (physnodes[i].end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700508 memblock_x86_hole_size(end, physnodes[i].end) < size)
David Rientjes8df5bb342010-02-15 13:43:30 -0800509 end = physnodes[i].end;
510
511 /*
512 * Setup the fake node that will be allocated as bootmem
513 * later. If setup_node_range() returns non-zero, there
514 * is no more memory available on this physical node.
515 */
516 if (setup_node_range(ret++, &physnodes[i].start,
517 end - physnodes[i].start,
518 physnodes[i].end) < 0)
519 node_clear(i, physnode_mask);
520 }
521 }
522 return ret;
523}
524
525/*
Thomas Gleixner886533a2008-05-12 15:43:36 +0200526 * Sets up the system RAM area from start_pfn to last_pfn according to the
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200527 * numa=fake command-line option.
528 */
David Rientjesadc19382009-09-25 15:20:09 -0700529static int __init numa_emulation(unsigned long start_pfn,
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200530 unsigned long last_pfn, int acpi, int amd)
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200531{
David Rientjesca2107c2010-02-15 13:43:33 -0800532 u64 addr = start_pfn << PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200533 u64 max_addr = last_pfn << PAGE_SHIFT;
David Rientjesca2107c2010-02-15 13:43:33 -0800534 int num_nodes;
535 int i;
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200536
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200537 /*
David Rientjes8df5bb342010-02-15 13:43:30 -0800538 * If the numa=fake command-line contains a 'M' or 'G', it represents
David Rientjesca2107c2010-02-15 13:43:33 -0800539 * the fixed node size. Otherwise, if it is just a single number N,
540 * split the system RAM into N fake nodes.
David Rientjes8df5bb342010-02-15 13:43:30 -0800541 */
542 if (strchr(cmdline, 'M') || strchr(cmdline, 'G')) {
David Rientjesca2107c2010-02-15 13:43:33 -0800543 u64 size;
544
David Rientjes8df5bb342010-02-15 13:43:30 -0800545 size = memparse(cmdline, &cmdline);
546 num_nodes = split_nodes_size_interleave(addr, max_addr, size);
David Rientjesca2107c2010-02-15 13:43:33 -0800547 } else {
548 unsigned long n;
549
550 n = simple_strtoul(cmdline, NULL, 0);
David Rientjesc1c34432010-12-22 17:23:54 -0800551 num_nodes = split_nodes_interleave(addr, max_addr, n);
David Rientjes8df5bb342010-02-15 13:43:30 -0800552 }
553
David Rientjesca2107c2010-02-15 13:43:33 -0800554 if (num_nodes < 0)
555 return num_nodes;
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700556 memnode_shift = compute_hash_shift(nodes, num_nodes, NULL);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200557 if (memnode_shift < 0) {
558 memnode_shift = 0;
559 printk(KERN_ERR "No NUMA hash function found. NUMA emulation "
560 "disabled.\n");
561 return -1;
562 }
563
564 /*
David Rientjesadc19382009-09-25 15:20:09 -0700565 * We need to vacate all active ranges that may have been registered for
566 * the e820 memory map.
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200567 */
568 remove_all_active_ranges();
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200569 for_each_node_mask(i, node_possible_map) {
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700570 memblock_x86_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
Mel Gorman5cb248a2006-09-27 01:49:52 -0700571 nodes[i].end >> PAGE_SHIFT);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100572 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
Mel Gorman5cb248a2006-09-27 01:49:52 -0700573 }
David Rientjesc1c34432010-12-22 17:23:54 -0800574 setup_physnodes(addr, max_addr, acpi, amd);
David Rientjesf51bf302010-12-22 17:23:51 -0800575 fake_physnodes(acpi, amd, num_nodes);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100576 numa_init_array();
577 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200579#endif /* CONFIG_NUMA_EMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
David Rientjes8ee2deb2009-09-25 15:20:00 -0700581void __init initmem_init(unsigned long start_pfn, unsigned long last_pfn,
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200582 int acpi, int amd)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100583{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 int i;
585
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200586 nodes_clear(node_possible_map);
Yinghai Lub7ad1492008-02-17 02:02:21 -0800587 nodes_clear(node_online_map);
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589#ifdef CONFIG_NUMA_EMU
David Rientjesc1c34432010-12-22 17:23:54 -0800590 setup_physnodes(start_pfn << PAGE_SHIFT, last_pfn << PAGE_SHIFT,
591 acpi, amd);
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200592 if (cmdline && !numa_emulation(start_pfn, last_pfn, acpi, amd))
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100593 return;
David Rientjesc1c34432010-12-22 17:23:54 -0800594 setup_physnodes(start_pfn << PAGE_SHIFT, last_pfn << PAGE_SHIFT,
595 acpi, amd);
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200596 nodes_clear(node_possible_map);
Yinghai Lub7ad1492008-02-17 02:02:21 -0800597 nodes_clear(node_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598#endif
599
600#ifdef CONFIG_ACPI_NUMA
David Rientjes87162732009-09-25 15:20:04 -0700601 if (!numa_off && acpi && !acpi_scan_nodes(start_pfn << PAGE_SHIFT,
602 last_pfn << PAGE_SHIFT))
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100603 return;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200604 nodes_clear(node_possible_map);
Yinghai Lub7ad1492008-02-17 02:02:21 -0800605 nodes_clear(node_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606#endif
607
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200608#ifdef CONFIG_AMD_NUMA
609 if (!numa_off && amd && !amd_scan_nodes())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200611 nodes_clear(node_possible_map);
Yinghai Lub7ad1492008-02-17 02:02:21 -0800612 nodes_clear(node_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613#endif
614 printk(KERN_INFO "%s\n",
615 numa_off ? "NUMA turned off" : "No NUMA configuration found");
616
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100617 printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 start_pfn << PAGE_SHIFT,
Thomas Gleixner886533a2008-05-12 15:43:36 +0200619 last_pfn << PAGE_SHIFT);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100620 /* setup dummy node covering all memory */
621 memnode_shift = 63;
Amul Shah076422d2007-02-13 13:26:19 +0100622 memnodemap = memnode.embedded_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 memnodemap[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 node_set_online(0);
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200625 node_set(0, node_possible_map);
Mike Travis168ef542008-12-16 17:34:01 -0800626 for (i = 0; i < nr_cpu_ids; i++)
Andi Kleen69d81fc2005-11-05 17:25:53 +0100627 numa_set_node(i, 0);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700628 memblock_x86_register_active_regions(0, start_pfn, last_pfn);
Thomas Gleixner886533a2008-05-12 15:43:36 +0200629 setup_node_bootmem(0, start_pfn << PAGE_SHIFT, last_pfn << PAGE_SHIFT);
Andi Kleen69d81fc2005-11-05 17:25:53 +0100630}
631
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100632unsigned long __init numa_free_all_bootmem(void)
633{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 unsigned long pages = 0;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100635 int i;
636
637 for_each_online_node(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 pages += free_all_bootmem_node(NODE_DATA(i));
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100639
Yinghai Lu08677212010-02-10 01:20:20 -0800640 pages += free_all_memory_core_early(MAX_NUMNODES);
Yinghai Lu08677212010-02-10 01:20:20 -0800641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return pages;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100643}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100645int __cpuinit numa_cpu_node(int cpu)
646{
647 int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
648
649 if (apicid != BAD_APICID)
650 return __apicid_to_node[apicid];
651 return NUMA_NO_NODE;
652}
Andi Kleencf050132006-01-11 22:46:27 +0100653
Tejun Heode2d9442011-01-23 14:37:41 +0100654/*
655 * UGLINESS AHEAD: Currently, CONFIG_NUMA_EMU is 64bit only and makes use
656 * of 64bit specific data structures. The distinction is artificial and
657 * should be removed. numa_{add|remove}_cpu() are implemented in numa.c
658 * for both 32 and 64bit when CONFIG_NUMA_EMU is disabled but here when
659 * enabled.
660 *
661 * NUMA emulation is planned to be made generic and the following and other
662 * related code should be moved to numa.c.
663 */
664#ifdef CONFIG_NUMA_EMU
665# ifndef CONFIG_DEBUG_PER_CPU_MAPS
David Rientjesc1c34432010-12-22 17:23:54 -0800666void __cpuinit numa_add_cpu(int cpu)
667{
668 unsigned long addr;
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100669 int physnid, nid;
David Rientjesc1c34432010-12-22 17:23:54 -0800670
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100671 nid = numa_cpu_node(cpu);
David Rientjesc1c34432010-12-22 17:23:54 -0800672 if (nid == NUMA_NO_NODE)
673 nid = early_cpu_to_node(cpu);
674 BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
675
676 /*
677 * Use the starting address of the emulated node to find which physical
678 * node it is allocated on.
679 */
680 addr = node_start_pfn(nid) << PAGE_SHIFT;
681 for (physnid = 0; physnid < MAX_NUMNODES; physnid++)
682 if (addr >= physnodes[physnid].start &&
683 addr < physnodes[physnid].end)
684 break;
685
686 /*
687 * Map the cpu to each emulated node that is allocated on the physical
688 * node of the cpu's apic id.
689 */
690 for_each_online_node(nid) {
691 addr = node_start_pfn(nid) << PAGE_SHIFT;
692 if (addr >= physnodes[physnid].start &&
693 addr < physnodes[physnid].end)
694 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
695 }
696}
697
698void __cpuinit numa_remove_cpu(int cpu)
699{
700 int i;
701
702 for_each_online_node(i)
703 cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
704}
Tejun Heode2d9442011-01-23 14:37:41 +0100705# else /* !CONFIG_DEBUG_PER_CPU_MAPS */
David Rientjesd906f0e2010-12-30 10:54:16 -0800706static void __cpuinit numa_set_cpumask(int cpu, int enable)
707{
708 int node = early_cpu_to_node(cpu);
709 struct cpumask *mask;
David Rientjesc1c34432010-12-22 17:23:54 -0800710 int i;
Brian Gerst6470aff2009-01-27 12:56:47 +0900711
David Rientjesc1c34432010-12-22 17:23:54 -0800712 for_each_online_node(i) {
713 unsigned long addr;
714
715 addr = node_start_pfn(i) << PAGE_SHIFT;
716 if (addr < physnodes[node].start ||
717 addr >= physnodes[node].end)
718 continue;
David Rientjesd906f0e2010-12-30 10:54:16 -0800719 mask = debug_cpumask_set_cpu(cpu, enable);
720 if (!mask)
David Rientjesc1c34432010-12-22 17:23:54 -0800721 return;
David Rientjesc1c34432010-12-22 17:23:54 -0800722
723 if (enable)
724 cpumask_set_cpu(cpu, mask);
725 else
726 cpumask_clear_cpu(cpu, mask);
Brian Gerst6470aff2009-01-27 12:56:47 +0900727 }
Brian Gerst6470aff2009-01-27 12:56:47 +0900728}
729
730void __cpuinit numa_add_cpu(int cpu)
731{
732 numa_set_cpumask(cpu, 1);
733}
734
735void __cpuinit numa_remove_cpu(int cpu)
736{
737 numa_set_cpumask(cpu, 0);
738}
Tejun Heode2d9442011-01-23 14:37:41 +0100739# endif /* !CONFIG_DEBUG_PER_CPU_MAPS */
740#endif /* CONFIG_NUMA_EMU */