blob: dd300c491f1f5e06d9d21d152cc9bffb313adebb [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
travis@sgi.com43238382008-01-30 13:33:25 +010029s16 apicid_to_node[MAX_LOCAL_APIC] __cpuinitdata = {
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010030 [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
Andi Kleen3f098c22005-09-12 18:49:24 +020031};
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033int numa_off __initdata;
Thomas Gleixner864fc312008-05-12 15:43:36 +020034static unsigned long __initdata nodemap_addr;
35static unsigned long __initdata nodemap_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Brian Gerst6470aff2009-01-27 12:56:47 +090037/*
38 * Map cpu index to node index
39 */
40DEFINE_EARLY_PER_CPU(int, x86_cpu_to_node_map, NUMA_NO_NODE);
41EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_node_map);
42
43/*
Eric Dumazet529a3402005-11-05 17:25:54 +010044 * Given a shift value, try to populate memnodemap[]
45 * Returns :
46 * 1 if OK
47 * 0 if memnodmap[] too small (of shift too small)
48 * -1 if node overlap or lost ram (shift too big)
49 */
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010050static int __init populate_memnodemap(const struct bootnode *nodes,
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -070051 int numnodes, int shift, int *nodeids)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Eric Dumazet529a3402005-11-05 17:25:54 +010053 unsigned long addr, end;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010054 int i, res = -1;
Keith Manntheyb6846642005-07-28 21:15:38 -070055
travis@sgi.com43238382008-01-30 13:33:25 +010056 memset(memnodemap, 0xff, sizeof(s16)*memnodemapsize);
Eric Dumazet529a3402005-11-05 17:25:54 +010057 for (i = 0; i < numnodes; i++) {
58 addr = nodes[i].start;
59 end = nodes[i].end;
60 if (addr >= end)
61 continue;
Amul Shah076422d2007-02-13 13:26:19 +010062 if ((end >> shift) >= memnodemapsize)
Eric Dumazet529a3402005-11-05 17:25:54 +010063 return 0;
64 do {
travis@sgi.com43238382008-01-30 13:33:25 +010065 if (memnodemap[addr >> shift] != NUMA_NO_NODE)
Eric Dumazet529a3402005-11-05 17:25:54 +010066 return -1;
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -070067
68 if (!nodeids)
69 memnodemap[addr >> shift] = i;
70 else
71 memnodemap[addr >> shift] = nodeids[i];
72
Amul Shah076422d2007-02-13 13:26:19 +010073 addr += (1UL << shift);
Eric Dumazet529a3402005-11-05 17:25:54 +010074 } while (addr < end);
75 res = 1;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010076 }
Eric Dumazet529a3402005-11-05 17:25:54 +010077 return res;
78}
79
Amul Shah076422d2007-02-13 13:26:19 +010080static int __init allocate_cachealigned_memnodemap(void)
81{
Yinghai Lu24a5da72008-02-01 17:49:41 +010082 unsigned long addr;
Amul Shah076422d2007-02-13 13:26:19 +010083
84 memnodemap = memnode.embedded_map;
travis@sgi.com316390b2008-01-30 13:33:15 +010085 if (memnodemapsize <= ARRAY_SIZE(memnode.embedded_map))
Amul Shah076422d2007-02-13 13:26:19 +010086 return 0;
Amul Shah076422d2007-02-13 13:26:19 +010087
Yinghai Lu24a5da72008-02-01 17:49:41 +010088 addr = 0x8000;
Joerg Roedelbe3e89e2008-07-25 16:48:58 +020089 nodemap_size = roundup(sizeof(s16) * memnodemapsize, L1_CACHE_BYTES);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070090 nodemap_addr = memblock_find_in_range(addr, max_pfn<<PAGE_SHIFT,
Yinghai Lu24a5da72008-02-01 17:49:41 +010091 nodemap_size, L1_CACHE_BYTES);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070092 if (nodemap_addr == MEMBLOCK_ERROR) {
Amul Shah076422d2007-02-13 13:26:19 +010093 printk(KERN_ERR
94 "NUMA: Unable to allocate Memory to Node hash map\n");
95 nodemap_addr = nodemap_size = 0;
96 return -1;
97 }
Yinghai Lu24a5da72008-02-01 17:49:41 +010098 memnodemap = phys_to_virt(nodemap_addr);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070099 memblock_x86_reserve_range(nodemap_addr, nodemap_addr + nodemap_size, "MEMNODEMAP");
Amul Shah076422d2007-02-13 13:26:19 +0100100
101 printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n",
102 nodemap_addr, nodemap_addr + nodemap_size);
103 return 0;
104}
105
106/*
107 * The LSB of all start and end addresses in the node map is the value of the
108 * maximum possible shift.
109 */
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100110static int __init extract_lsb_from_nodes(const struct bootnode *nodes,
111 int numnodes)
Amul Shah076422d2007-02-13 13:26:19 +0100112{
Amul Shah54413922007-02-13 13:26:20 +0100113 int i, nodes_used = 0;
Amul Shah076422d2007-02-13 13:26:19 +0100114 unsigned long start, end;
115 unsigned long bitfield = 0, memtop = 0;
116
117 for (i = 0; i < numnodes; i++) {
118 start = nodes[i].start;
119 end = nodes[i].end;
120 if (start >= end)
121 continue;
Amul Shah54413922007-02-13 13:26:20 +0100122 bitfield |= start;
123 nodes_used++;
Amul Shah076422d2007-02-13 13:26:19 +0100124 if (end > memtop)
125 memtop = end;
126 }
Amul Shah54413922007-02-13 13:26:20 +0100127 if (nodes_used <= 1)
128 i = 63;
129 else
130 i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
Amul Shah076422d2007-02-13 13:26:19 +0100131 memnodemapsize = (memtop >> i)+1;
132 return i;
133}
134
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700135int __init compute_hash_shift(struct bootnode *nodes, int numnodes,
136 int *nodeids)
Eric Dumazet529a3402005-11-05 17:25:54 +0100137{
Amul Shah076422d2007-02-13 13:26:19 +0100138 int shift;
Eric Dumazet529a3402005-11-05 17:25:54 +0100139
Amul Shah076422d2007-02-13 13:26:19 +0100140 shift = extract_lsb_from_nodes(nodes, numnodes);
141 if (allocate_cachealigned_memnodemap())
142 return -1;
Andi Kleen6b050f82006-01-11 22:44:33 +0100143 printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
Eric Dumazet529a3402005-11-05 17:25:54 +0100144 shift);
145
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700146 if (populate_memnodemap(nodes, numnodes, shift, nodeids) != 1) {
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100147 printk(KERN_INFO "Your memory is not aligned you need to "
148 "rebuild your kernel with a bigger NODEMAPSIZE "
149 "shift=%d\n", shift);
Eric Dumazet529a3402005-11-05 17:25:54 +0100150 return -1;
151 }
Keith Manntheyb6846642005-07-28 21:15:38 -0700152 return shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -0800155int __meminit __early_pfn_to_nid(unsigned long pfn)
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700156{
157 return phys_to_nid(pfn << PAGE_SHIFT);
158}
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700159
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100160static void * __init early_node_mem(int nodeid, unsigned long start,
Yinghai Lu24a5da72008-02-01 17:49:41 +0100161 unsigned long end, unsigned long size,
162 unsigned long align)
Andi Kleena8062232006-04-07 19:49:21 +0200163{
Yinghai Lucef625e2010-02-10 01:20:18 -0800164 unsigned long mem;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100165
Yinghai Lucef625e2010-02-10 01:20:18 -0800166 /*
167 * put it on high as possible
168 * something will go with NODE_DATA
169 */
170 if (start < (MAX_DMA_PFN<<PAGE_SHIFT))
171 start = MAX_DMA_PFN<<PAGE_SHIFT;
172 if (start < (MAX_DMA32_PFN<<PAGE_SHIFT) &&
173 end > (MAX_DMA32_PFN<<PAGE_SHIFT))
174 start = MAX_DMA32_PFN<<PAGE_SHIFT;
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700175 mem = memblock_x86_find_in_range_node(nodeid, start, end, size, align);
176 if (mem != MEMBLOCK_ERROR)
Andi Kleena8062232006-04-07 19:49:21 +0200177 return __va(mem);
Yinghai Lu9347e0b2008-02-01 17:49:42 +0100178
Yinghai Lucef625e2010-02-10 01:20:18 -0800179 /* extend the search scope */
180 end = max_pfn_mapped << PAGE_SHIFT;
Yinghai Lu419db272010-10-28 09:50:17 -0700181 start = MAX_DMA_PFN << PAGE_SHIFT;
182 mem = memblock_find_in_range(start, end, size, align);
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700183 if (mem != MEMBLOCK_ERROR)
Yinghai Lu1842f902010-02-10 01:20:15 -0800184 return __va(mem);
185
186 printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100187 size, nodeid);
Yinghai Lu1842f902010-02-10 01:20:15 -0800188
189 return NULL;
Andi Kleena8062232006-04-07 19:49:21 +0200190}
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/* Initialize bootmem allocator for a node */
Yinghai Lu7c437692009-05-15 13:59:37 -0700193void __init
194setup_node_bootmem(int nodeid, unsigned long start, unsigned long end)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100195{
Yinghai Lu08677212010-02-10 01:20:20 -0800196 unsigned long start_pfn, last_pfn, nodedata_phys;
Yinghai Lu7c437692009-05-15 13:59:37 -0700197 const int pgdat_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
Yinghai Lu1a27fc02008-03-18 12:52:37 -0700198 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Yinghai Lu4c31e922009-04-22 14:19:27 -0700200 if (!end)
201 return;
202
Yinghai Lu7c437692009-05-15 13:59:37 -0700203 /*
204 * Don't confuse VM with a node that doesn't have the
205 * minimum amount of memory:
206 */
207 if (end && (end - start) < NODE_MIN_SIZE)
208 return;
209
Joerg Roedelbe3e89e2008-07-25 16:48:58 +0200210 start = roundup(start, ZONE_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Yinghai Lu08677212010-02-10 01:20:20 -0800212 printk(KERN_INFO "Initmem setup node %d %016lx-%016lx\n", nodeid,
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100213 start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 start_pfn = start >> PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200216 last_pfn = end >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Yinghai Lu24a5da72008-02-01 17:49:41 +0100218 node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
219 SMP_CACHE_BYTES);
Andi Kleena8062232006-04-07 19:49:21 +0200220 if (node_data[nodeid] == NULL)
221 return;
222 nodedata_phys = __pa(node_data[nodeid]);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700223 memblock_x86_reserve_range(nodedata_phys, nodedata_phys + pgdat_size, "NODE_DATA");
Yinghai Lu6118f762008-02-04 16:47:56 +0100224 printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
225 nodedata_phys + pgdat_size - 1);
Yinghai Lu1842f902010-02-10 01:20:15 -0800226 nid = phys_to_nid(nodedata_phys);
227 if (nid != nodeid)
228 printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nodeid, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
Yinghai Lu08677212010-02-10 01:20:20 -0800231 NODE_DATA(nodeid)->node_id = nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 NODE_DATA(nodeid)->node_start_pfn = start_pfn;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200233 NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 node_set_online(nodeid);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100236}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100238/*
239 * There are unfortunately some poorly designed mainboards around that
240 * only connect memory to a single CPU. This breaks the 1:1 cpu->node
241 * mapping. To avoid this fill in the mapping for all possible CPUs,
242 * as the number of CPUs is not known yet. We round robin the existing
243 * nodes.
244 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245void __init numa_init_array(void)
246{
247 int rr, i;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100248
Ravikiran G Thirumalai85cc5132005-09-30 11:59:22 -0700249 rr = first_node(node_online_map);
Mike Travis168ef542008-12-16 17:34:01 -0800250 for (i = 0; i < nr_cpu_ids; i++) {
travis@sgi.com1ce35712008-01-30 13:33:33 +0100251 if (early_cpu_to_node(i) != NUMA_NO_NODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 continue;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100253 numa_set_node(i, rr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 rr = next_node(rr, node_online_map);
255 if (rr == MAX_NUMNODES)
256 rr = first_node(node_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260#ifdef CONFIG_NUMA_EMU
Rohit Seth53fee042007-02-13 13:26:22 +0100261/* Numa emulation */
David Rientjesadc19382009-09-25 15:20:09 -0700262static struct bootnode nodes[MAX_NUMNODES] __initdata;
David Rientjesc1c34432010-12-22 17:23:54 -0800263static struct bootnode physnodes[MAX_NUMNODES] __cpuinitdata;
Thomas Gleixner864fc312008-05-12 15:43:36 +0200264static char *cmdline __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
David Rientjesadc19382009-09-25 15:20:09 -0700266static int __init setup_physnodes(unsigned long start, unsigned long end,
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200267 int acpi, int amd)
David Rientjesadc19382009-09-25 15:20:09 -0700268{
269 int nr_nodes = 0;
270 int ret = 0;
271 int i;
272
David Rientjesc1c34432010-12-22 17:23:54 -0800273 memset(physnodes, 0, sizeof(physnodes));
David Rientjesadc19382009-09-25 15:20:09 -0700274#ifdef CONFIG_ACPI_NUMA
275 if (acpi)
276 nr_nodes = acpi_get_nodes(physnodes);
277#endif
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200278#ifdef CONFIG_AMD_NUMA
279 if (amd)
280 nr_nodes = amd_get_nodes(physnodes);
David Rientjesadc19382009-09-25 15:20:09 -0700281#endif
282 /*
283 * Basic sanity checking on the physical node map: there may be errors
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200284 * if the SRAT or AMD code incorrectly reported the topology or the mem=
David Rientjesadc19382009-09-25 15:20:09 -0700285 * kernel parameter is used.
286 */
287 for (i = 0; i < nr_nodes; i++) {
288 if (physnodes[i].start == physnodes[i].end)
289 continue;
290 if (physnodes[i].start > end) {
291 physnodes[i].end = physnodes[i].start;
292 continue;
293 }
294 if (physnodes[i].end < start) {
295 physnodes[i].start = physnodes[i].end;
296 continue;
297 }
298 if (physnodes[i].start < start)
299 physnodes[i].start = start;
300 if (physnodes[i].end > end)
301 physnodes[i].end = end;
302 }
303
304 /*
305 * Remove all nodes that have no memory or were truncated because of the
306 * limited address range.
307 */
308 for (i = 0; i < nr_nodes; i++) {
309 if (physnodes[i].start == physnodes[i].end)
310 continue;
311 physnodes[ret].start = physnodes[i].start;
312 physnodes[ret].end = physnodes[i].end;
313 ret++;
314 }
315
316 /*
317 * If no physical topology was detected, a single node is faked to cover
318 * the entire address space.
319 */
320 if (!ret) {
321 physnodes[ret].start = start;
322 physnodes[ret].end = end;
323 ret = 1;
324 }
325 return ret;
326}
327
David Rientjesf51bf302010-12-22 17:23:51 -0800328static void __init fake_physnodes(int acpi, int amd, int nr_nodes)
329{
330 int i;
331
332 BUG_ON(acpi && amd);
333#ifdef CONFIG_ACPI_NUMA
334 if (acpi)
335 acpi_fake_nodes(nodes, nr_nodes);
336#endif
337#ifdef CONFIG_AMD_NUMA
338 if (amd)
339 amd_fake_nodes(nodes, nr_nodes);
340#endif
341 if (!acpi && !amd)
342 for (i = 0; i < nr_cpu_ids; i++)
343 numa_set_node(i, 0);
344}
345
Rohit Seth53fee042007-02-13 13:26:22 +0100346/*
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100347 * Setups up nid to range from addr to addr + size. If the end
348 * boundary is greater than max_addr, then max_addr is used instead.
349 * The return value is 0 if there is additional memory left for
350 * allocation past addr and -1 otherwise. addr is adjusted to be at
351 * the end of the node.
Rohit Seth53fee042007-02-13 13:26:22 +0100352 */
David Rientjesadc19382009-09-25 15:20:09 -0700353static int __init setup_node_range(int nid, u64 *addr, u64 size, u64 max_addr)
Rohit Seth53fee042007-02-13 13:26:22 +0100354{
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200355 int ret = 0;
356 nodes[nid].start = *addr;
357 *addr += size;
358 if (*addr >= max_addr) {
359 *addr = max_addr;
360 ret = -1;
361 }
362 nodes[nid].end = *addr;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200363 node_set(nid, node_possible_map);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200364 printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
365 nodes[nid].start, nodes[nid].end,
366 (nodes[nid].end - nodes[nid].start) >> 20);
367 return ret;
Rohit Seth53fee042007-02-13 13:26:22 +0100368}
369
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200370/*
David Rientjesadc19382009-09-25 15:20:09 -0700371 * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
372 * to max_addr. The return value is the number of nodes allocated.
373 */
David Rientjesc1c34432010-12-22 17:23:54 -0800374static int __init split_nodes_interleave(u64 addr, u64 max_addr, int nr_nodes)
David Rientjesadc19382009-09-25 15:20:09 -0700375{
376 nodemask_t physnode_mask = NODE_MASK_NONE;
377 u64 size;
378 int big;
379 int ret = 0;
380 int i;
381
382 if (nr_nodes <= 0)
383 return -1;
384 if (nr_nodes > MAX_NUMNODES) {
385 pr_info("numa=fake=%d too large, reducing to %d\n",
386 nr_nodes, MAX_NUMNODES);
387 nr_nodes = MAX_NUMNODES;
388 }
389
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700390 size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) / nr_nodes;
David Rientjesadc19382009-09-25 15:20:09 -0700391 /*
392 * Calculate the number of big nodes that can be allocated as a result
393 * of consolidating the remainder.
394 */
David Rientjes68fd1112010-02-15 13:43:25 -0800395 big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
David Rientjesadc19382009-09-25 15:20:09 -0700396 FAKE_NODE_MIN_SIZE;
397
398 size &= FAKE_NODE_MIN_HASH_MASK;
399 if (!size) {
400 pr_err("Not enough memory for each node. "
401 "NUMA emulation disabled.\n");
402 return -1;
403 }
404
David Rientjesc1c34432010-12-22 17:23:54 -0800405 for (i = 0; i < MAX_NUMNODES; i++)
David Rientjesadc19382009-09-25 15:20:09 -0700406 if (physnodes[i].start != physnodes[i].end)
407 node_set(i, physnode_mask);
408
409 /*
410 * Continue to fill physical nodes with fake nodes until there is no
411 * memory left on any of them.
412 */
413 while (nodes_weight(physnode_mask)) {
414 for_each_node_mask(i, physnode_mask) {
415 u64 end = physnodes[i].start + size;
416 u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
417
418 if (ret < big)
419 end += FAKE_NODE_MIN_SIZE;
420
421 /*
422 * Continue to add memory to this fake node if its
423 * non-reserved memory is less than the per-node size.
424 */
425 while (end - physnodes[i].start -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700426 memblock_x86_hole_size(physnodes[i].start, end) < size) {
David Rientjesadc19382009-09-25 15:20:09 -0700427 end += FAKE_NODE_MIN_SIZE;
428 if (end > physnodes[i].end) {
429 end = physnodes[i].end;
430 break;
431 }
432 }
433
434 /*
435 * If there won't be at least FAKE_NODE_MIN_SIZE of
436 * non-reserved memory in ZONE_DMA32 for the next node,
437 * this one must extend to the boundary.
438 */
439 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700440 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjesadc19382009-09-25 15:20:09 -0700441 end = dma32_end;
442
443 /*
444 * If there won't be enough non-reserved memory for the
445 * next node, this one must extend to the end of the
446 * physical node.
447 */
448 if (physnodes[i].end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700449 memblock_x86_hole_size(end, physnodes[i].end) < size)
David Rientjesadc19382009-09-25 15:20:09 -0700450 end = physnodes[i].end;
451
452 /*
453 * Avoid allocating more nodes than requested, which can
454 * happen as a result of rounding down each node's size
455 * to FAKE_NODE_MIN_SIZE.
456 */
457 if (nodes_weight(physnode_mask) + ret >= nr_nodes)
458 end = physnodes[i].end;
459
460 if (setup_node_range(ret++, &physnodes[i].start,
461 end - physnodes[i].start,
462 physnodes[i].end) < 0)
463 node_clear(i, physnode_mask);
464 }
465 }
466 return ret;
467}
468
469/*
David Rientjes8df5bb342010-02-15 13:43:30 -0800470 * Returns the end address of a node so that there is at least `size' amount of
471 * non-reserved memory or `max_addr' is reached.
472 */
473static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
474{
475 u64 end = start + size;
476
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700477 while (end - start - memblock_x86_hole_size(start, end) < size) {
David Rientjes8df5bb342010-02-15 13:43:30 -0800478 end += FAKE_NODE_MIN_SIZE;
479 if (end > max_addr) {
480 end = max_addr;
481 break;
482 }
483 }
484 return end;
485}
486
487/*
488 * Sets up fake nodes of `size' interleaved over physical nodes ranging from
489 * `addr' to `max_addr'. The return value is the number of nodes allocated.
490 */
491static int __init split_nodes_size_interleave(u64 addr, u64 max_addr, u64 size)
492{
493 nodemask_t physnode_mask = NODE_MASK_NONE;
494 u64 min_size;
495 int ret = 0;
496 int i;
497
498 if (!size)
499 return -1;
500 /*
501 * The limit on emulated nodes is MAX_NUMNODES, so the size per node is
502 * increased accordingly if the requested size is too small. This
503 * creates a uniform distribution of node sizes across the entire
504 * machine (but not necessarily over physical nodes).
505 */
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700506 min_size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) /
David Rientjes8df5bb342010-02-15 13:43:30 -0800507 MAX_NUMNODES;
508 min_size = max(min_size, FAKE_NODE_MIN_SIZE);
509 if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size)
510 min_size = (min_size + FAKE_NODE_MIN_SIZE) &
511 FAKE_NODE_MIN_HASH_MASK;
512 if (size < min_size) {
513 pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
514 size >> 20, min_size >> 20);
515 size = min_size;
516 }
517 size &= FAKE_NODE_MIN_HASH_MASK;
518
519 for (i = 0; i < MAX_NUMNODES; i++)
520 if (physnodes[i].start != physnodes[i].end)
521 node_set(i, physnode_mask);
522 /*
523 * Fill physical nodes with fake nodes of size until there is no memory
524 * left on any of them.
525 */
526 while (nodes_weight(physnode_mask)) {
527 for_each_node_mask(i, physnode_mask) {
528 u64 dma32_end = MAX_DMA32_PFN << PAGE_SHIFT;
529 u64 end;
530
531 end = find_end_of_node(physnodes[i].start,
532 physnodes[i].end, size);
533 /*
534 * If there won't be at least FAKE_NODE_MIN_SIZE of
535 * non-reserved memory in ZONE_DMA32 for the next node,
536 * this one must extend to the boundary.
537 */
538 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700539 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjes8df5bb342010-02-15 13:43:30 -0800540 end = dma32_end;
541
542 /*
543 * If there won't be enough non-reserved memory for the
544 * next node, this one must extend to the end of the
545 * physical node.
546 */
547 if (physnodes[i].end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700548 memblock_x86_hole_size(end, physnodes[i].end) < size)
David Rientjes8df5bb342010-02-15 13:43:30 -0800549 end = physnodes[i].end;
550
551 /*
552 * Setup the fake node that will be allocated as bootmem
553 * later. If setup_node_range() returns non-zero, there
554 * is no more memory available on this physical node.
555 */
556 if (setup_node_range(ret++, &physnodes[i].start,
557 end - physnodes[i].start,
558 physnodes[i].end) < 0)
559 node_clear(i, physnode_mask);
560 }
561 }
562 return ret;
563}
564
565/*
Thomas Gleixner886533a2008-05-12 15:43:36 +0200566 * Sets up the system RAM area from start_pfn to last_pfn according to the
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200567 * numa=fake command-line option.
568 */
David Rientjesadc19382009-09-25 15:20:09 -0700569static int __init numa_emulation(unsigned long start_pfn,
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200570 unsigned long last_pfn, int acpi, int amd)
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200571{
David Rientjesca2107c2010-02-15 13:43:33 -0800572 u64 addr = start_pfn << PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200573 u64 max_addr = last_pfn << PAGE_SHIFT;
David Rientjesca2107c2010-02-15 13:43:33 -0800574 int num_nodes;
575 int i;
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200576
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200577 /*
David Rientjes8df5bb342010-02-15 13:43:30 -0800578 * If the numa=fake command-line contains a 'M' or 'G', it represents
David Rientjesca2107c2010-02-15 13:43:33 -0800579 * the fixed node size. Otherwise, if it is just a single number N,
580 * split the system RAM into N fake nodes.
David Rientjes8df5bb342010-02-15 13:43:30 -0800581 */
582 if (strchr(cmdline, 'M') || strchr(cmdline, 'G')) {
David Rientjesca2107c2010-02-15 13:43:33 -0800583 u64 size;
584
David Rientjes8df5bb342010-02-15 13:43:30 -0800585 size = memparse(cmdline, &cmdline);
586 num_nodes = split_nodes_size_interleave(addr, max_addr, size);
David Rientjesca2107c2010-02-15 13:43:33 -0800587 } else {
588 unsigned long n;
589
590 n = simple_strtoul(cmdline, NULL, 0);
David Rientjesc1c34432010-12-22 17:23:54 -0800591 num_nodes = split_nodes_interleave(addr, max_addr, n);
David Rientjes8df5bb342010-02-15 13:43:30 -0800592 }
593
David Rientjesca2107c2010-02-15 13:43:33 -0800594 if (num_nodes < 0)
595 return num_nodes;
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700596 memnode_shift = compute_hash_shift(nodes, num_nodes, NULL);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200597 if (memnode_shift < 0) {
598 memnode_shift = 0;
599 printk(KERN_ERR "No NUMA hash function found. NUMA emulation "
600 "disabled.\n");
601 return -1;
602 }
603
604 /*
David Rientjesadc19382009-09-25 15:20:09 -0700605 * We need to vacate all active ranges that may have been registered for
606 * the e820 memory map.
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200607 */
608 remove_all_active_ranges();
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200609 for_each_node_mask(i, node_possible_map) {
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700610 memblock_x86_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
Mel Gorman5cb248a2006-09-27 01:49:52 -0700611 nodes[i].end >> PAGE_SHIFT);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100612 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
Mel Gorman5cb248a2006-09-27 01:49:52 -0700613 }
David Rientjesc1c34432010-12-22 17:23:54 -0800614 setup_physnodes(addr, max_addr, acpi, amd);
David Rientjesf51bf302010-12-22 17:23:51 -0800615 fake_physnodes(acpi, amd, num_nodes);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100616 numa_init_array();
617 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200619#endif /* CONFIG_NUMA_EMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
David Rientjes8ee2deb2009-09-25 15:20:00 -0700621void __init initmem_init(unsigned long start_pfn, unsigned long last_pfn,
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200622 int acpi, int amd)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100623{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 int i;
625
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200626 nodes_clear(node_possible_map);
Yinghai Lub7ad1492008-02-17 02:02:21 -0800627 nodes_clear(node_online_map);
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629#ifdef CONFIG_NUMA_EMU
David Rientjesc1c34432010-12-22 17:23:54 -0800630 setup_physnodes(start_pfn << PAGE_SHIFT, last_pfn << PAGE_SHIFT,
631 acpi, amd);
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200632 if (cmdline && !numa_emulation(start_pfn, last_pfn, acpi, amd))
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100633 return;
David Rientjesc1c34432010-12-22 17:23:54 -0800634 setup_physnodes(start_pfn << PAGE_SHIFT, last_pfn << PAGE_SHIFT,
635 acpi, amd);
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200636 nodes_clear(node_possible_map);
Yinghai Lub7ad1492008-02-17 02:02:21 -0800637 nodes_clear(node_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638#endif
639
640#ifdef CONFIG_ACPI_NUMA
David Rientjes87162732009-09-25 15:20:04 -0700641 if (!numa_off && acpi && !acpi_scan_nodes(start_pfn << PAGE_SHIFT,
642 last_pfn << PAGE_SHIFT))
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100643 return;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200644 nodes_clear(node_possible_map);
Yinghai Lub7ad1492008-02-17 02:02:21 -0800645 nodes_clear(node_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646#endif
647
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200648#ifdef CONFIG_AMD_NUMA
649 if (!numa_off && amd && !amd_scan_nodes())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200651 nodes_clear(node_possible_map);
Yinghai Lub7ad1492008-02-17 02:02:21 -0800652 nodes_clear(node_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653#endif
654 printk(KERN_INFO "%s\n",
655 numa_off ? "NUMA turned off" : "No NUMA configuration found");
656
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100657 printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 start_pfn << PAGE_SHIFT,
Thomas Gleixner886533a2008-05-12 15:43:36 +0200659 last_pfn << PAGE_SHIFT);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100660 /* setup dummy node covering all memory */
661 memnode_shift = 63;
Amul Shah076422d2007-02-13 13:26:19 +0100662 memnodemap = memnode.embedded_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 memnodemap[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 node_set_online(0);
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200665 node_set(0, node_possible_map);
Mike Travis168ef542008-12-16 17:34:01 -0800666 for (i = 0; i < nr_cpu_ids; i++)
Andi Kleen69d81fc2005-11-05 17:25:53 +0100667 numa_set_node(i, 0);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700668 memblock_x86_register_active_regions(0, start_pfn, last_pfn);
Thomas Gleixner886533a2008-05-12 15:43:36 +0200669 setup_node_bootmem(0, start_pfn << PAGE_SHIFT, last_pfn << PAGE_SHIFT);
Andi Kleen69d81fc2005-11-05 17:25:53 +0100670}
671
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100672unsigned long __init numa_free_all_bootmem(void)
673{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 unsigned long pages = 0;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100675 int i;
676
677 for_each_online_node(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 pages += free_all_bootmem_node(NODE_DATA(i));
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100679
Yinghai Lu08677212010-02-10 01:20:20 -0800680 pages += free_all_memory_core_early(MAX_NUMNODES);
Yinghai Lu08677212010-02-10 01:20:20 -0800681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return pages;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100683}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200685static __init int numa_setup(char *opt)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100686{
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200687 if (!opt)
688 return -EINVAL;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100689 if (!strncmp(opt, "off", 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 numa_off = 1;
691#ifdef CONFIG_NUMA_EMU
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200692 if (!strncmp(opt, "fake=", 5))
693 cmdline = opt + 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694#endif
695#ifdef CONFIG_ACPI_NUMA
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100696 if (!strncmp(opt, "noacpi", 6))
697 acpi_numa = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698#endif
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200699 return 0;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100700}
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200701early_param("numa", numa_setup);
702
Mike Travis23ca4bb2008-05-12 21:21:12 +0200703#ifdef CONFIG_NUMA
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800704
705static __init int find_near_online_node(int node)
706{
707 int n, val;
708 int min_val = INT_MAX;
709 int best_node = -1;
710
711 for_each_online_node(n) {
712 val = node_distance(node, n);
713
714 if (val < min_val) {
715 min_val = val;
716 best_node = n;
717 }
718 }
719
720 return best_node;
721}
722
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100723/*
724 * Setup early cpu_to_node.
725 *
726 * Populate cpu_to_node[] only if x86_cpu_to_apicid[],
727 * and apicid_to_node[] tables have valid entries for a CPU.
728 * This means we skip cpu_to_node[] initialisation for NUMA
729 * emulation and faking node case (when running a kernel compiled
730 * for NUMA on a non NUMA box), which is OK as cpu_to_node[]
731 * is already initialized in a round robin manner at numa_init_array,
732 * prior to this call, and this initialization is good enough
733 * for the fake NUMA cases.
Mike Travis23ca4bb2008-05-12 21:21:12 +0200734 *
735 * Called before the per_cpu areas are setup.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100736 */
737void __init init_cpu_to_node(void)
738{
Mike Travis23ca4bb2008-05-12 21:21:12 +0200739 int cpu;
740 u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100741
Mike Travis23ca4bb2008-05-12 21:21:12 +0200742 BUG_ON(cpu_to_apicid == NULL);
743
744 for_each_possible_cpu(cpu) {
Yinghai Lu7c9e92b62008-02-19 15:35:54 -0800745 int node;
Mike Travis23ca4bb2008-05-12 21:21:12 +0200746 u16 apicid = cpu_to_apicid[cpu];
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100747
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100748 if (apicid == BAD_APICID)
749 continue;
Yinghai Lu7c9e92b62008-02-19 15:35:54 -0800750 node = apicid_to_node[apicid];
751 if (node == NUMA_NO_NODE)
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100752 continue;
Yinghai Lu7c9e92b62008-02-19 15:35:54 -0800753 if (!node_online(node))
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800754 node = find_near_online_node(node);
Mike Travis23ca4bb2008-05-12 21:21:12 +0200755 numa_set_node(cpu, node);
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100756 }
757}
Mike Travis23ca4bb2008-05-12 21:21:12 +0200758#endif
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100759
Andi Kleencf050132006-01-11 22:46:27 +0100760
Brian Gerst6470aff2009-01-27 12:56:47 +0900761void __cpuinit numa_set_node(int cpu, int node)
762{
763 int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
764
765 /* early setting, no percpu area yet */
766 if (cpu_to_node_map) {
767 cpu_to_node_map[cpu] = node;
768 return;
769 }
770
771#ifdef CONFIG_DEBUG_PER_CPU_MAPS
Brian Gerst44581a22009-02-08 09:58:40 -0500772 if (cpu >= nr_cpu_ids || !cpu_possible(cpu)) {
Brian Gerst6470aff2009-01-27 12:56:47 +0900773 printk(KERN_ERR "numa_set_node: invalid cpu# (%d)\n", cpu);
774 dump_stack();
775 return;
776 }
777#endif
778 per_cpu(x86_cpu_to_node_map, cpu) = node;
779
780 if (node != NUMA_NO_NODE)
Lee Schermerhorne534c7c2010-05-26 14:44:58 -0700781 set_cpu_numa_node(cpu, node);
Brian Gerst6470aff2009-01-27 12:56:47 +0900782}
783
784void __cpuinit numa_clear_node(int cpu)
785{
786 numa_set_node(cpu, NUMA_NO_NODE);
787}
788
789#ifndef CONFIG_DEBUG_PER_CPU_MAPS
790
David Rientjesc1c34432010-12-22 17:23:54 -0800791#ifndef CONFIG_NUMA_EMU
Brian Gerst6470aff2009-01-27 12:56:47 +0900792void __cpuinit numa_add_cpu(int cpu)
793{
Rusty Russellc032ef602009-03-13 14:49:53 +1030794 cpumask_set_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
Brian Gerst6470aff2009-01-27 12:56:47 +0900795}
796
797void __cpuinit numa_remove_cpu(int cpu)
798{
Rusty Russellc032ef602009-03-13 14:49:53 +1030799 cpumask_clear_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
Brian Gerst6470aff2009-01-27 12:56:47 +0900800}
David Rientjesc1c34432010-12-22 17:23:54 -0800801#else
802void __cpuinit numa_add_cpu(int cpu)
803{
804 unsigned long addr;
805 u16 apicid;
806 int physnid;
807 int nid = NUMA_NO_NODE;
808
809 apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
810 if (apicid != BAD_APICID)
811 nid = apicid_to_node[apicid];
812 if (nid == NUMA_NO_NODE)
813 nid = early_cpu_to_node(cpu);
814 BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
815
816 /*
817 * Use the starting address of the emulated node to find which physical
818 * node it is allocated on.
819 */
820 addr = node_start_pfn(nid) << PAGE_SHIFT;
821 for (physnid = 0; physnid < MAX_NUMNODES; physnid++)
822 if (addr >= physnodes[physnid].start &&
823 addr < physnodes[physnid].end)
824 break;
825
826 /*
827 * Map the cpu to each emulated node that is allocated on the physical
828 * node of the cpu's apic id.
829 */
830 for_each_online_node(nid) {
831 addr = node_start_pfn(nid) << PAGE_SHIFT;
832 if (addr >= physnodes[physnid].start &&
833 addr < physnodes[physnid].end)
834 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
835 }
836}
837
838void __cpuinit numa_remove_cpu(int cpu)
839{
840 int i;
841
842 for_each_online_node(i)
843 cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
844}
845#endif /* !CONFIG_NUMA_EMU */
Brian Gerst6470aff2009-01-27 12:56:47 +0900846
847#else /* CONFIG_DEBUG_PER_CPU_MAPS */
848
849/*
850 * --------- debug versions of the numa functions ---------
851 */
852static void __cpuinit numa_set_cpumask(int cpu, int enable)
853{
854 int node = early_cpu_to_node(cpu);
Rusty Russell73e907d2009-03-13 14:49:57 +1030855 struct cpumask *mask;
Brian Gerst6470aff2009-01-27 12:56:47 +0900856 char buf[64];
David Rientjesc1c34432010-12-22 17:23:54 -0800857 int i;
Brian Gerst6470aff2009-01-27 12:56:47 +0900858
David Rientjesc1c34432010-12-22 17:23:54 -0800859 for_each_online_node(i) {
860 unsigned long addr;
861
862 addr = node_start_pfn(i) << PAGE_SHIFT;
863 if (addr < physnodes[node].start ||
864 addr >= physnodes[node].end)
865 continue;
866 mask = node_to_cpumask_map[node];
867 if (mask == NULL) {
868 pr_err("node_to_cpumask_map[%i] NULL\n", i);
869 dump_stack();
870 return;
871 }
872
873 if (enable)
874 cpumask_set_cpu(cpu, mask);
875 else
876 cpumask_clear_cpu(cpu, mask);
877
878 cpulist_scnprintf(buf, sizeof(buf), mask);
879 printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n",
880 enable ? "numa_add_cpu" : "numa_remove_cpu",
881 cpu, node, buf);
Brian Gerst6470aff2009-01-27 12:56:47 +0900882 }
Brian Gerst6470aff2009-01-27 12:56:47 +0900883}
884
885void __cpuinit numa_add_cpu(int cpu)
886{
887 numa_set_cpumask(cpu, 1);
888}
889
890void __cpuinit numa_remove_cpu(int cpu)
891{
892 numa_set_cpumask(cpu, 0);
893}
894
Lee Schermerhorne534c7c2010-05-26 14:44:58 -0700895int __cpu_to_node(int cpu)
Brian Gerst6470aff2009-01-27 12:56:47 +0900896{
897 if (early_per_cpu_ptr(x86_cpu_to_node_map)) {
898 printk(KERN_WARNING
899 "cpu_to_node(%d): usage too early!\n", cpu);
900 dump_stack();
901 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
902 }
903 return per_cpu(x86_cpu_to_node_map, cpu);
904}
Lee Schermerhorne534c7c2010-05-26 14:44:58 -0700905EXPORT_SYMBOL(__cpu_to_node);
Brian Gerst6470aff2009-01-27 12:56:47 +0900906
907/*
908 * Same function as cpu_to_node() but used if called before the
909 * per_cpu areas are setup.
910 */
911int early_cpu_to_node(int cpu)
912{
913 if (early_per_cpu_ptr(x86_cpu_to_node_map))
914 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
915
Brian Gerst44581a22009-02-08 09:58:40 -0500916 if (!cpu_possible(cpu)) {
Brian Gerst6470aff2009-01-27 12:56:47 +0900917 printk(KERN_WARNING
918 "early_cpu_to_node(%d): no per_cpu area!\n", cpu);
919 dump_stack();
920 return NUMA_NO_NODE;
921 }
922 return per_cpu(x86_cpu_to_node_map, cpu);
923}
924
Brian Gerst6470aff2009-01-27 12:56:47 +0900925/*
Brian Gerst6470aff2009-01-27 12:56:47 +0900926 * --------- end of debug versions of the numa functions ---------
927 */
928
929#endif /* CONFIG_DEBUG_PER_CPU_MAPS */