blob: 1e1026f61a5a2447d308e897ab21497d37e4f9d7 [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/*
33 * Map cpu index to node index
34 */
35DEFINE_EARLY_PER_CPU(int, x86_cpu_to_node_map, NUMA_NO_NODE);
36EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_node_map);
37
38/*
Eric Dumazet529a3402005-11-05 17:25:54 +010039 * Given a shift value, try to populate memnodemap[]
40 * Returns :
41 * 1 if OK
42 * 0 if memnodmap[] too small (of shift too small)
43 * -1 if node overlap or lost ram (shift too big)
44 */
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010045static int __init populate_memnodemap(const struct bootnode *nodes,
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -070046 int numnodes, int shift, int *nodeids)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Eric Dumazet529a3402005-11-05 17:25:54 +010048 unsigned long addr, end;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010049 int i, res = -1;
Keith Manntheyb6846642005-07-28 21:15:38 -070050
travis@sgi.com43238382008-01-30 13:33:25 +010051 memset(memnodemap, 0xff, sizeof(s16)*memnodemapsize);
Eric Dumazet529a3402005-11-05 17:25:54 +010052 for (i = 0; i < numnodes; i++) {
53 addr = nodes[i].start;
54 end = nodes[i].end;
55 if (addr >= end)
56 continue;
Amul Shah076422d2007-02-13 13:26:19 +010057 if ((end >> shift) >= memnodemapsize)
Eric Dumazet529a3402005-11-05 17:25:54 +010058 return 0;
59 do {
travis@sgi.com43238382008-01-30 13:33:25 +010060 if (memnodemap[addr >> shift] != NUMA_NO_NODE)
Eric Dumazet529a3402005-11-05 17:25:54 +010061 return -1;
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -070062
63 if (!nodeids)
64 memnodemap[addr >> shift] = i;
65 else
66 memnodemap[addr >> shift] = nodeids[i];
67
Amul Shah076422d2007-02-13 13:26:19 +010068 addr += (1UL << shift);
Eric Dumazet529a3402005-11-05 17:25:54 +010069 } while (addr < end);
70 res = 1;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010071 }
Eric Dumazet529a3402005-11-05 17:25:54 +010072 return res;
73}
74
Amul Shah076422d2007-02-13 13:26:19 +010075static int __init allocate_cachealigned_memnodemap(void)
76{
Yinghai Lu24a5da72008-02-01 17:49:41 +010077 unsigned long addr;
Amul Shah076422d2007-02-13 13:26:19 +010078
79 memnodemap = memnode.embedded_map;
travis@sgi.com316390b2008-01-30 13:33:15 +010080 if (memnodemapsize <= ARRAY_SIZE(memnode.embedded_map))
Amul Shah076422d2007-02-13 13:26:19 +010081 return 0;
Amul Shah076422d2007-02-13 13:26:19 +010082
Yinghai Lu24a5da72008-02-01 17:49:41 +010083 addr = 0x8000;
Joerg Roedelbe3e89e2008-07-25 16:48:58 +020084 nodemap_size = roundup(sizeof(s16) * memnodemapsize, L1_CACHE_BYTES);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070085 nodemap_addr = memblock_find_in_range(addr, max_pfn<<PAGE_SHIFT,
Yinghai Lu24a5da72008-02-01 17:49:41 +010086 nodemap_size, L1_CACHE_BYTES);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070087 if (nodemap_addr == MEMBLOCK_ERROR) {
Amul Shah076422d2007-02-13 13:26:19 +010088 printk(KERN_ERR
89 "NUMA: Unable to allocate Memory to Node hash map\n");
90 nodemap_addr = nodemap_size = 0;
91 return -1;
92 }
Yinghai Lu24a5da72008-02-01 17:49:41 +010093 memnodemap = phys_to_virt(nodemap_addr);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070094 memblock_x86_reserve_range(nodemap_addr, nodemap_addr + nodemap_size, "MEMNODEMAP");
Amul Shah076422d2007-02-13 13:26:19 +010095
96 printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n",
97 nodemap_addr, nodemap_addr + nodemap_size);
98 return 0;
99}
100
101/*
102 * The LSB of all start and end addresses in the node map is the value of the
103 * maximum possible shift.
104 */
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100105static int __init extract_lsb_from_nodes(const struct bootnode *nodes,
106 int numnodes)
Amul Shah076422d2007-02-13 13:26:19 +0100107{
Amul Shah54413922007-02-13 13:26:20 +0100108 int i, nodes_used = 0;
Amul Shah076422d2007-02-13 13:26:19 +0100109 unsigned long start, end;
110 unsigned long bitfield = 0, memtop = 0;
111
112 for (i = 0; i < numnodes; i++) {
113 start = nodes[i].start;
114 end = nodes[i].end;
115 if (start >= end)
116 continue;
Amul Shah54413922007-02-13 13:26:20 +0100117 bitfield |= start;
118 nodes_used++;
Amul Shah076422d2007-02-13 13:26:19 +0100119 if (end > memtop)
120 memtop = end;
121 }
Amul Shah54413922007-02-13 13:26:20 +0100122 if (nodes_used <= 1)
123 i = 63;
124 else
125 i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
Amul Shah076422d2007-02-13 13:26:19 +0100126 memnodemapsize = (memtop >> i)+1;
127 return i;
128}
129
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700130int __init compute_hash_shift(struct bootnode *nodes, int numnodes,
131 int *nodeids)
Eric Dumazet529a3402005-11-05 17:25:54 +0100132{
Amul Shah076422d2007-02-13 13:26:19 +0100133 int shift;
Eric Dumazet529a3402005-11-05 17:25:54 +0100134
Amul Shah076422d2007-02-13 13:26:19 +0100135 shift = extract_lsb_from_nodes(nodes, numnodes);
136 if (allocate_cachealigned_memnodemap())
137 return -1;
Andi Kleen6b050f82006-01-11 22:44:33 +0100138 printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
Eric Dumazet529a3402005-11-05 17:25:54 +0100139 shift);
140
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700141 if (populate_memnodemap(nodes, numnodes, shift, nodeids) != 1) {
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100142 printk(KERN_INFO "Your memory is not aligned you need to "
143 "rebuild your kernel with a bigger NODEMAPSIZE "
144 "shift=%d\n", shift);
Eric Dumazet529a3402005-11-05 17:25:54 +0100145 return -1;
146 }
Keith Manntheyb6846642005-07-28 21:15:38 -0700147 return shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -0800150int __meminit __early_pfn_to_nid(unsigned long pfn)
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700151{
152 return phys_to_nid(pfn << PAGE_SHIFT);
153}
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700154
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100155static void * __init early_node_mem(int nodeid, unsigned long start,
Yinghai Lu24a5da72008-02-01 17:49:41 +0100156 unsigned long end, unsigned long size,
157 unsigned long align)
Andi Kleena8062232006-04-07 19:49:21 +0200158{
Yinghai Lucef625e2010-02-10 01:20:18 -0800159 unsigned long mem;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100160
Yinghai Lucef625e2010-02-10 01:20:18 -0800161 /*
162 * put it on high as possible
163 * something will go with NODE_DATA
164 */
165 if (start < (MAX_DMA_PFN<<PAGE_SHIFT))
166 start = MAX_DMA_PFN<<PAGE_SHIFT;
167 if (start < (MAX_DMA32_PFN<<PAGE_SHIFT) &&
168 end > (MAX_DMA32_PFN<<PAGE_SHIFT))
169 start = MAX_DMA32_PFN<<PAGE_SHIFT;
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700170 mem = memblock_x86_find_in_range_node(nodeid, start, end, size, align);
171 if (mem != MEMBLOCK_ERROR)
Andi Kleena8062232006-04-07 19:49:21 +0200172 return __va(mem);
Yinghai Lu9347e0b2008-02-01 17:49:42 +0100173
Yinghai Lucef625e2010-02-10 01:20:18 -0800174 /* extend the search scope */
175 end = max_pfn_mapped << PAGE_SHIFT;
Yinghai Lu419db272010-10-28 09:50:17 -0700176 start = MAX_DMA_PFN << PAGE_SHIFT;
177 mem = memblock_find_in_range(start, end, size, align);
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700178 if (mem != MEMBLOCK_ERROR)
Yinghai Lu1842f902010-02-10 01:20:15 -0800179 return __va(mem);
180
181 printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100182 size, nodeid);
Yinghai Lu1842f902010-02-10 01:20:15 -0800183
184 return NULL;
Andi Kleena8062232006-04-07 19:49:21 +0200185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/* Initialize bootmem allocator for a node */
Yinghai Lu7c437692009-05-15 13:59:37 -0700188void __init
189setup_node_bootmem(int nodeid, unsigned long start, unsigned long end)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100190{
Yinghai Lu08677212010-02-10 01:20:20 -0800191 unsigned long start_pfn, last_pfn, nodedata_phys;
Yinghai Lu7c437692009-05-15 13:59:37 -0700192 const int pgdat_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
Yinghai Lu1a27fc02008-03-18 12:52:37 -0700193 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Yinghai Lu4c31e922009-04-22 14:19:27 -0700195 if (!end)
196 return;
197
Yinghai Lu7c437692009-05-15 13:59:37 -0700198 /*
199 * Don't confuse VM with a node that doesn't have the
200 * minimum amount of memory:
201 */
202 if (end && (end - start) < NODE_MIN_SIZE)
203 return;
204
Joerg Roedelbe3e89e2008-07-25 16:48:58 +0200205 start = roundup(start, ZONE_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Yinghai Lu08677212010-02-10 01:20:20 -0800207 printk(KERN_INFO "Initmem setup node %d %016lx-%016lx\n", nodeid,
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100208 start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 start_pfn = start >> PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200211 last_pfn = end >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Yinghai Lu24a5da72008-02-01 17:49:41 +0100213 node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
214 SMP_CACHE_BYTES);
Andi Kleena8062232006-04-07 19:49:21 +0200215 if (node_data[nodeid] == NULL)
216 return;
217 nodedata_phys = __pa(node_data[nodeid]);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700218 memblock_x86_reserve_range(nodedata_phys, nodedata_phys + pgdat_size, "NODE_DATA");
Yinghai Lu6118f762008-02-04 16:47:56 +0100219 printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
220 nodedata_phys + pgdat_size - 1);
Yinghai Lu1842f902010-02-10 01:20:15 -0800221 nid = phys_to_nid(nodedata_phys);
222 if (nid != nodeid)
223 printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nodeid, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
Yinghai Lu08677212010-02-10 01:20:20 -0800226 NODE_DATA(nodeid)->node_id = nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 NODE_DATA(nodeid)->node_start_pfn = start_pfn;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200228 NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 node_set_online(nodeid);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100231}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100233/*
234 * There are unfortunately some poorly designed mainboards around that
235 * only connect memory to a single CPU. This breaks the 1:1 cpu->node
236 * mapping. To avoid this fill in the mapping for all possible CPUs,
237 * as the number of CPUs is not known yet. We round robin the existing
238 * nodes.
239 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240void __init numa_init_array(void)
241{
242 int rr, i;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100243
Ravikiran G Thirumalai85cc5132005-09-30 11:59:22 -0700244 rr = first_node(node_online_map);
Mike Travis168ef542008-12-16 17:34:01 -0800245 for (i = 0; i < nr_cpu_ids; i++) {
travis@sgi.com1ce35712008-01-30 13:33:33 +0100246 if (early_cpu_to_node(i) != NUMA_NO_NODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 continue;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100248 numa_set_node(i, rr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 rr = next_node(rr, node_online_map);
250 if (rr == MAX_NUMNODES)
251 rr = first_node(node_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255#ifdef CONFIG_NUMA_EMU
Rohit Seth53fee042007-02-13 13:26:22 +0100256/* Numa emulation */
David Rientjesadc19382009-09-25 15:20:09 -0700257static struct bootnode nodes[MAX_NUMNODES] __initdata;
David Rientjesc1c34432010-12-22 17:23:54 -0800258static struct bootnode physnodes[MAX_NUMNODES] __cpuinitdata;
Thomas Gleixner864fc312008-05-12 15:43:36 +0200259static char *cmdline __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Jan Beulich90321602011-01-19 08:57:21 +0000261void __init numa_emu_cmdline(char *str)
262{
263 cmdline = str;
264}
265
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{
David Rientjesadc19382009-09-25 15:20:09 -0700269 int ret = 0;
270 int i;
271
David Rientjesc1c34432010-12-22 17:23:54 -0800272 memset(physnodes, 0, sizeof(physnodes));
David Rientjesadc19382009-09-25 15:20:09 -0700273#ifdef CONFIG_ACPI_NUMA
274 if (acpi)
David Rientjesa387e952010-12-22 17:23:56 -0800275 acpi_get_nodes(physnodes, start, end);
David Rientjesadc19382009-09-25 15:20:09 -0700276#endif
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200277#ifdef CONFIG_AMD_NUMA
278 if (amd)
David Rientjesa387e952010-12-22 17:23:56 -0800279 amd_get_nodes(physnodes);
David Rientjesadc19382009-09-25 15:20:09 -0700280#endif
281 /*
282 * Basic sanity checking on the physical node map: there may be errors
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200283 * if the SRAT or AMD code incorrectly reported the topology or the mem=
David Rientjesadc19382009-09-25 15:20:09 -0700284 * kernel parameter is used.
285 */
David Rientjesa387e952010-12-22 17:23:56 -0800286 for (i = 0; i < MAX_NUMNODES; i++) {
David Rientjesadc19382009-09-25 15:20:09 -0700287 if (physnodes[i].start == physnodes[i].end)
288 continue;
289 if (physnodes[i].start > end) {
290 physnodes[i].end = physnodes[i].start;
291 continue;
292 }
293 if (physnodes[i].end < start) {
294 physnodes[i].start = physnodes[i].end;
295 continue;
296 }
297 if (physnodes[i].start < start)
298 physnodes[i].start = start;
299 if (physnodes[i].end > end)
300 physnodes[i].end = end;
David Rientjesadc19382009-09-25 15:20:09 -0700301 ret++;
302 }
303
304 /*
305 * If no physical topology was detected, a single node is faked to cover
306 * the entire address space.
307 */
308 if (!ret) {
309 physnodes[ret].start = start;
310 physnodes[ret].end = end;
311 ret = 1;
312 }
313 return ret;
314}
315
David Rientjesf51bf302010-12-22 17:23:51 -0800316static void __init fake_physnodes(int acpi, int amd, int nr_nodes)
317{
318 int i;
319
320 BUG_ON(acpi && amd);
321#ifdef CONFIG_ACPI_NUMA
322 if (acpi)
323 acpi_fake_nodes(nodes, nr_nodes);
324#endif
325#ifdef CONFIG_AMD_NUMA
326 if (amd)
327 amd_fake_nodes(nodes, nr_nodes);
328#endif
329 if (!acpi && !amd)
330 for (i = 0; i < nr_cpu_ids; i++)
331 numa_set_node(i, 0);
332}
333
Rohit Seth53fee042007-02-13 13:26:22 +0100334/*
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100335 * Setups up nid to range from addr to addr + size. If the end
336 * boundary is greater than max_addr, then max_addr is used instead.
337 * The return value is 0 if there is additional memory left for
338 * allocation past addr and -1 otherwise. addr is adjusted to be at
339 * the end of the node.
Rohit Seth53fee042007-02-13 13:26:22 +0100340 */
David Rientjesadc19382009-09-25 15:20:09 -0700341static int __init setup_node_range(int nid, u64 *addr, u64 size, u64 max_addr)
Rohit Seth53fee042007-02-13 13:26:22 +0100342{
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200343 int ret = 0;
344 nodes[nid].start = *addr;
345 *addr += size;
346 if (*addr >= max_addr) {
347 *addr = max_addr;
348 ret = -1;
349 }
350 nodes[nid].end = *addr;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200351 node_set(nid, node_possible_map);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200352 printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
353 nodes[nid].start, nodes[nid].end,
354 (nodes[nid].end - nodes[nid].start) >> 20);
355 return ret;
Rohit Seth53fee042007-02-13 13:26:22 +0100356}
357
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200358/*
David Rientjesadc19382009-09-25 15:20:09 -0700359 * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
360 * to max_addr. The return value is the number of nodes allocated.
361 */
David Rientjesc1c34432010-12-22 17:23:54 -0800362static int __init split_nodes_interleave(u64 addr, u64 max_addr, int nr_nodes)
David Rientjesadc19382009-09-25 15:20:09 -0700363{
364 nodemask_t physnode_mask = NODE_MASK_NONE;
365 u64 size;
366 int big;
367 int ret = 0;
368 int i;
369
370 if (nr_nodes <= 0)
371 return -1;
372 if (nr_nodes > MAX_NUMNODES) {
373 pr_info("numa=fake=%d too large, reducing to %d\n",
374 nr_nodes, MAX_NUMNODES);
375 nr_nodes = MAX_NUMNODES;
376 }
377
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700378 size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) / nr_nodes;
David Rientjesadc19382009-09-25 15:20:09 -0700379 /*
380 * Calculate the number of big nodes that can be allocated as a result
381 * of consolidating the remainder.
382 */
David Rientjes68fd1112010-02-15 13:43:25 -0800383 big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
David Rientjesadc19382009-09-25 15:20:09 -0700384 FAKE_NODE_MIN_SIZE;
385
386 size &= FAKE_NODE_MIN_HASH_MASK;
387 if (!size) {
388 pr_err("Not enough memory for each node. "
389 "NUMA emulation disabled.\n");
390 return -1;
391 }
392
David Rientjesc1c34432010-12-22 17:23:54 -0800393 for (i = 0; i < MAX_NUMNODES; i++)
David Rientjesadc19382009-09-25 15:20:09 -0700394 if (physnodes[i].start != physnodes[i].end)
395 node_set(i, physnode_mask);
396
397 /*
398 * Continue to fill physical nodes with fake nodes until there is no
399 * memory left on any of them.
400 */
401 while (nodes_weight(physnode_mask)) {
402 for_each_node_mask(i, physnode_mask) {
403 u64 end = physnodes[i].start + size;
404 u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
405
406 if (ret < big)
407 end += FAKE_NODE_MIN_SIZE;
408
409 /*
410 * Continue to add memory to this fake node if its
411 * non-reserved memory is less than the per-node size.
412 */
413 while (end - physnodes[i].start -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700414 memblock_x86_hole_size(physnodes[i].start, end) < size) {
David Rientjesadc19382009-09-25 15:20:09 -0700415 end += FAKE_NODE_MIN_SIZE;
416 if (end > physnodes[i].end) {
417 end = physnodes[i].end;
418 break;
419 }
420 }
421
422 /*
423 * If there won't be at least FAKE_NODE_MIN_SIZE of
424 * non-reserved memory in ZONE_DMA32 for the next node,
425 * this one must extend to the boundary.
426 */
427 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700428 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjesadc19382009-09-25 15:20:09 -0700429 end = dma32_end;
430
431 /*
432 * If there won't be enough non-reserved memory for the
433 * next node, this one must extend to the end of the
434 * physical node.
435 */
436 if (physnodes[i].end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700437 memblock_x86_hole_size(end, physnodes[i].end) < size)
David Rientjesadc19382009-09-25 15:20:09 -0700438 end = physnodes[i].end;
439
440 /*
441 * Avoid allocating more nodes than requested, which can
442 * happen as a result of rounding down each node's size
443 * to FAKE_NODE_MIN_SIZE.
444 */
445 if (nodes_weight(physnode_mask) + ret >= nr_nodes)
446 end = physnodes[i].end;
447
448 if (setup_node_range(ret++, &physnodes[i].start,
449 end - physnodes[i].start,
450 physnodes[i].end) < 0)
451 node_clear(i, physnode_mask);
452 }
453 }
454 return ret;
455}
456
457/*
David Rientjes8df5bb32010-02-15 13:43:30 -0800458 * Returns the end address of a node so that there is at least `size' amount of
459 * non-reserved memory or `max_addr' is reached.
460 */
461static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
462{
463 u64 end = start + size;
464
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700465 while (end - start - memblock_x86_hole_size(start, end) < size) {
David Rientjes8df5bb32010-02-15 13:43:30 -0800466 end += FAKE_NODE_MIN_SIZE;
467 if (end > max_addr) {
468 end = max_addr;
469 break;
470 }
471 }
472 return end;
473}
474
475/*
476 * Sets up fake nodes of `size' interleaved over physical nodes ranging from
477 * `addr' to `max_addr'. The return value is the number of nodes allocated.
478 */
479static int __init split_nodes_size_interleave(u64 addr, u64 max_addr, u64 size)
480{
481 nodemask_t physnode_mask = NODE_MASK_NONE;
482 u64 min_size;
483 int ret = 0;
484 int i;
485
486 if (!size)
487 return -1;
488 /*
489 * The limit on emulated nodes is MAX_NUMNODES, so the size per node is
490 * increased accordingly if the requested size is too small. This
491 * creates a uniform distribution of node sizes across the entire
492 * machine (but not necessarily over physical nodes).
493 */
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700494 min_size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) /
David Rientjes8df5bb32010-02-15 13:43:30 -0800495 MAX_NUMNODES;
496 min_size = max(min_size, FAKE_NODE_MIN_SIZE);
497 if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size)
498 min_size = (min_size + FAKE_NODE_MIN_SIZE) &
499 FAKE_NODE_MIN_HASH_MASK;
500 if (size < min_size) {
501 pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
502 size >> 20, min_size >> 20);
503 size = min_size;
504 }
505 size &= FAKE_NODE_MIN_HASH_MASK;
506
507 for (i = 0; i < MAX_NUMNODES; i++)
508 if (physnodes[i].start != physnodes[i].end)
509 node_set(i, physnode_mask);
510 /*
511 * Fill physical nodes with fake nodes of size until there is no memory
512 * left on any of them.
513 */
514 while (nodes_weight(physnode_mask)) {
515 for_each_node_mask(i, physnode_mask) {
516 u64 dma32_end = MAX_DMA32_PFN << PAGE_SHIFT;
517 u64 end;
518
519 end = find_end_of_node(physnodes[i].start,
520 physnodes[i].end, size);
521 /*
522 * If there won't be at least FAKE_NODE_MIN_SIZE of
523 * non-reserved memory in ZONE_DMA32 for the next node,
524 * this one must extend to the boundary.
525 */
526 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700527 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjes8df5bb32010-02-15 13:43:30 -0800528 end = dma32_end;
529
530 /*
531 * If there won't be enough non-reserved memory for the
532 * next node, this one must extend to the end of the
533 * physical node.
534 */
535 if (physnodes[i].end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700536 memblock_x86_hole_size(end, physnodes[i].end) < size)
David Rientjes8df5bb32010-02-15 13:43:30 -0800537 end = physnodes[i].end;
538
539 /*
540 * Setup the fake node that will be allocated as bootmem
541 * later. If setup_node_range() returns non-zero, there
542 * is no more memory available on this physical node.
543 */
544 if (setup_node_range(ret++, &physnodes[i].start,
545 end - physnodes[i].start,
546 physnodes[i].end) < 0)
547 node_clear(i, physnode_mask);
548 }
549 }
550 return ret;
551}
552
553/*
Thomas Gleixner886533a2008-05-12 15:43:36 +0200554 * Sets up the system RAM area from start_pfn to last_pfn according to the
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200555 * numa=fake command-line option.
556 */
David Rientjesadc19382009-09-25 15:20:09 -0700557static int __init numa_emulation(unsigned long start_pfn,
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200558 unsigned long last_pfn, int acpi, int amd)
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200559{
David Rientjesca2107c2010-02-15 13:43:33 -0800560 u64 addr = start_pfn << PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200561 u64 max_addr = last_pfn << PAGE_SHIFT;
David Rientjesca2107c2010-02-15 13:43:33 -0800562 int num_nodes;
563 int i;
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200564
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200565 /*
David Rientjes8df5bb32010-02-15 13:43:30 -0800566 * If the numa=fake command-line contains a 'M' or 'G', it represents
David Rientjesca2107c2010-02-15 13:43:33 -0800567 * the fixed node size. Otherwise, if it is just a single number N,
568 * split the system RAM into N fake nodes.
David Rientjes8df5bb32010-02-15 13:43:30 -0800569 */
570 if (strchr(cmdline, 'M') || strchr(cmdline, 'G')) {
David Rientjesca2107c2010-02-15 13:43:33 -0800571 u64 size;
572
David Rientjes8df5bb32010-02-15 13:43:30 -0800573 size = memparse(cmdline, &cmdline);
574 num_nodes = split_nodes_size_interleave(addr, max_addr, size);
David Rientjesca2107c2010-02-15 13:43:33 -0800575 } else {
576 unsigned long n;
577
578 n = simple_strtoul(cmdline, NULL, 0);
David Rientjesc1c34432010-12-22 17:23:54 -0800579 num_nodes = split_nodes_interleave(addr, max_addr, n);
David Rientjes8df5bb32010-02-15 13:43:30 -0800580 }
581
David Rientjesca2107c2010-02-15 13:43:33 -0800582 if (num_nodes < 0)
583 return num_nodes;
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700584 memnode_shift = compute_hash_shift(nodes, num_nodes, NULL);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200585 if (memnode_shift < 0) {
586 memnode_shift = 0;
587 printk(KERN_ERR "No NUMA hash function found. NUMA emulation "
588 "disabled.\n");
589 return -1;
590 }
591
592 /*
David Rientjesadc19382009-09-25 15:20:09 -0700593 * We need to vacate all active ranges that may have been registered for
594 * the e820 memory map.
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200595 */
596 remove_all_active_ranges();
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200597 for_each_node_mask(i, node_possible_map) {
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700598 memblock_x86_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
Mel Gorman5cb248a2006-09-27 01:49:52 -0700599 nodes[i].end >> PAGE_SHIFT);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100600 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
Mel Gorman5cb248a2006-09-27 01:49:52 -0700601 }
David Rientjesc1c34432010-12-22 17:23:54 -0800602 setup_physnodes(addr, max_addr, acpi, amd);
David Rientjesf51bf302010-12-22 17:23:51 -0800603 fake_physnodes(acpi, amd, num_nodes);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100604 numa_init_array();
605 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200607#endif /* CONFIG_NUMA_EMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
David Rientjes8ee2deb2009-09-25 15:20:00 -0700609void __init initmem_init(unsigned long start_pfn, unsigned long last_pfn,
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200610 int acpi, int amd)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100611{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 int i;
613
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200614 nodes_clear(node_possible_map);
Yinghai Lub7ad1492008-02-17 02:02:21 -0800615 nodes_clear(node_online_map);
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617#ifdef CONFIG_NUMA_EMU
David Rientjesc1c34432010-12-22 17:23:54 -0800618 setup_physnodes(start_pfn << PAGE_SHIFT, last_pfn << PAGE_SHIFT,
619 acpi, amd);
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200620 if (cmdline && !numa_emulation(start_pfn, last_pfn, acpi, amd))
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100621 return;
David Rientjesc1c34432010-12-22 17:23:54 -0800622 setup_physnodes(start_pfn << PAGE_SHIFT, last_pfn << PAGE_SHIFT,
623 acpi, amd);
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200624 nodes_clear(node_possible_map);
Yinghai Lub7ad1492008-02-17 02:02:21 -0800625 nodes_clear(node_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626#endif
627
628#ifdef CONFIG_ACPI_NUMA
David Rientjes87162732009-09-25 15:20:04 -0700629 if (!numa_off && acpi && !acpi_scan_nodes(start_pfn << PAGE_SHIFT,
630 last_pfn << PAGE_SHIFT))
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100631 return;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200632 nodes_clear(node_possible_map);
Yinghai Lub7ad1492008-02-17 02:02:21 -0800633 nodes_clear(node_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634#endif
635
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200636#ifdef CONFIG_AMD_NUMA
637 if (!numa_off && amd && !amd_scan_nodes())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200639 nodes_clear(node_possible_map);
Yinghai Lub7ad1492008-02-17 02:02:21 -0800640 nodes_clear(node_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641#endif
642 printk(KERN_INFO "%s\n",
643 numa_off ? "NUMA turned off" : "No NUMA configuration found");
644
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100645 printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 start_pfn << PAGE_SHIFT,
Thomas Gleixner886533a2008-05-12 15:43:36 +0200647 last_pfn << PAGE_SHIFT);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100648 /* setup dummy node covering all memory */
649 memnode_shift = 63;
Amul Shah076422d2007-02-13 13:26:19 +0100650 memnodemap = memnode.embedded_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 memnodemap[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 node_set_online(0);
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200653 node_set(0, node_possible_map);
Mike Travis168ef542008-12-16 17:34:01 -0800654 for (i = 0; i < nr_cpu_ids; i++)
Andi Kleen69d81fc2005-11-05 17:25:53 +0100655 numa_set_node(i, 0);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700656 memblock_x86_register_active_regions(0, start_pfn, last_pfn);
Thomas Gleixner886533a2008-05-12 15:43:36 +0200657 setup_node_bootmem(0, start_pfn << PAGE_SHIFT, last_pfn << PAGE_SHIFT);
Andi Kleen69d81fc2005-11-05 17:25:53 +0100658}
659
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100660unsigned long __init numa_free_all_bootmem(void)
661{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 unsigned long pages = 0;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100663 int i;
664
665 for_each_online_node(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 pages += free_all_bootmem_node(NODE_DATA(i));
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100667
Yinghai Lu08677212010-02-10 01:20:20 -0800668 pages += free_all_memory_core_early(MAX_NUMNODES);
Yinghai Lu08677212010-02-10 01:20:20 -0800669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return pages;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100671}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Mike Travis23ca4bb2008-05-12 21:21:12 +0200673#ifdef CONFIG_NUMA
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800674
675static __init int find_near_online_node(int node)
676{
677 int n, val;
678 int min_val = INT_MAX;
679 int best_node = -1;
680
681 for_each_online_node(n) {
682 val = node_distance(node, n);
683
684 if (val < min_val) {
685 min_val = val;
686 best_node = n;
687 }
688 }
689
690 return best_node;
691}
692
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100693/*
694 * Setup early cpu_to_node.
695 *
696 * Populate cpu_to_node[] only if x86_cpu_to_apicid[],
697 * and apicid_to_node[] tables have valid entries for a CPU.
698 * This means we skip cpu_to_node[] initialisation for NUMA
699 * emulation and faking node case (when running a kernel compiled
700 * for NUMA on a non NUMA box), which is OK as cpu_to_node[]
701 * is already initialized in a round robin manner at numa_init_array,
702 * prior to this call, and this initialization is good enough
703 * for the fake NUMA cases.
Mike Travis23ca4bb2008-05-12 21:21:12 +0200704 *
705 * Called before the per_cpu areas are setup.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100706 */
707void __init init_cpu_to_node(void)
708{
Mike Travis23ca4bb2008-05-12 21:21:12 +0200709 int cpu;
710 u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100711
Mike Travis23ca4bb2008-05-12 21:21:12 +0200712 BUG_ON(cpu_to_apicid == NULL);
713
714 for_each_possible_cpu(cpu) {
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100715 int node = numa_cpu_node(cpu);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100716
Yinghai Lu7c9e92b2008-02-19 15:35:54 -0800717 if (node == NUMA_NO_NODE)
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100718 continue;
Yinghai Lu7c9e92b2008-02-19 15:35:54 -0800719 if (!node_online(node))
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800720 node = find_near_online_node(node);
Mike Travis23ca4bb2008-05-12 21:21:12 +0200721 numa_set_node(cpu, node);
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100722 }
723}
Mike Travis23ca4bb2008-05-12 21:21:12 +0200724#endif
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100725
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100726int __cpuinit numa_cpu_node(int cpu)
727{
728 int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
729
730 if (apicid != BAD_APICID)
731 return __apicid_to_node[apicid];
732 return NUMA_NO_NODE;
733}
Andi Kleencf050132006-01-11 22:46:27 +0100734
Brian Gerst6470aff2009-01-27 12:56:47 +0900735void __cpuinit numa_set_node(int cpu, int node)
736{
737 int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
738
739 /* early setting, no percpu area yet */
740 if (cpu_to_node_map) {
741 cpu_to_node_map[cpu] = node;
742 return;
743 }
744
745#ifdef CONFIG_DEBUG_PER_CPU_MAPS
Brian Gerst44581a22009-02-08 09:58:40 -0500746 if (cpu >= nr_cpu_ids || !cpu_possible(cpu)) {
Brian Gerst6470aff2009-01-27 12:56:47 +0900747 printk(KERN_ERR "numa_set_node: invalid cpu# (%d)\n", cpu);
748 dump_stack();
749 return;
750 }
751#endif
752 per_cpu(x86_cpu_to_node_map, cpu) = node;
753
754 if (node != NUMA_NO_NODE)
Lee Schermerhorne534c7c2010-05-26 14:44:58 -0700755 set_cpu_numa_node(cpu, node);
Brian Gerst6470aff2009-01-27 12:56:47 +0900756}
757
758void __cpuinit numa_clear_node(int cpu)
759{
760 numa_set_node(cpu, NUMA_NO_NODE);
761}
762
763#ifndef CONFIG_DEBUG_PER_CPU_MAPS
764
David Rientjesc1c34432010-12-22 17:23:54 -0800765#ifndef CONFIG_NUMA_EMU
Brian Gerst6470aff2009-01-27 12:56:47 +0900766void __cpuinit numa_add_cpu(int cpu)
767{
Rusty Russellc032ef602009-03-13 14:49:53 +1030768 cpumask_set_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
Brian Gerst6470aff2009-01-27 12:56:47 +0900769}
770
771void __cpuinit numa_remove_cpu(int cpu)
772{
Rusty Russellc032ef602009-03-13 14:49:53 +1030773 cpumask_clear_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
Brian Gerst6470aff2009-01-27 12:56:47 +0900774}
David Rientjesc1c34432010-12-22 17:23:54 -0800775#else
776void __cpuinit numa_add_cpu(int cpu)
777{
778 unsigned long addr;
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100779 int physnid, nid;
David Rientjesc1c34432010-12-22 17:23:54 -0800780
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100781 nid = numa_cpu_node(cpu);
David Rientjesc1c34432010-12-22 17:23:54 -0800782 if (nid == NUMA_NO_NODE)
783 nid = early_cpu_to_node(cpu);
784 BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
785
786 /*
787 * Use the starting address of the emulated node to find which physical
788 * node it is allocated on.
789 */
790 addr = node_start_pfn(nid) << PAGE_SHIFT;
791 for (physnid = 0; physnid < MAX_NUMNODES; physnid++)
792 if (addr >= physnodes[physnid].start &&
793 addr < physnodes[physnid].end)
794 break;
795
796 /*
797 * Map the cpu to each emulated node that is allocated on the physical
798 * node of the cpu's apic id.
799 */
800 for_each_online_node(nid) {
801 addr = node_start_pfn(nid) << PAGE_SHIFT;
802 if (addr >= physnodes[physnid].start &&
803 addr < physnodes[physnid].end)
804 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
805 }
806}
807
808void __cpuinit numa_remove_cpu(int cpu)
809{
810 int i;
811
812 for_each_online_node(i)
813 cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
814}
815#endif /* !CONFIG_NUMA_EMU */
Brian Gerst6470aff2009-01-27 12:56:47 +0900816
817#else /* CONFIG_DEBUG_PER_CPU_MAPS */
David Rientjesd906f0e2010-12-30 10:54:16 -0800818static struct cpumask __cpuinit *debug_cpumask_set_cpu(int cpu, int enable)
Brian Gerst6470aff2009-01-27 12:56:47 +0900819{
820 int node = early_cpu_to_node(cpu);
Rusty Russell73e907d2009-03-13 14:49:57 +1030821 struct cpumask *mask;
Brian Gerst6470aff2009-01-27 12:56:47 +0900822 char buf[64];
David Rientjesd906f0e2010-12-30 10:54:16 -0800823
824 mask = node_to_cpumask_map[node];
825 if (!mask) {
826 pr_err("node_to_cpumask_map[%i] NULL\n", node);
827 dump_stack();
828 return NULL;
829 }
830
831 cpulist_scnprintf(buf, sizeof(buf), mask);
832 printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n",
833 enable ? "numa_add_cpu" : "numa_remove_cpu",
834 cpu, node, buf);
835 return mask;
836}
837
838/*
839 * --------- debug versions of the numa functions ---------
840 */
841#ifndef CONFIG_NUMA_EMU
842static void __cpuinit numa_set_cpumask(int cpu, int enable)
843{
844 struct cpumask *mask;
845
846 mask = debug_cpumask_set_cpu(cpu, enable);
847 if (!mask)
848 return;
849
850 if (enable)
851 cpumask_set_cpu(cpu, mask);
852 else
853 cpumask_clear_cpu(cpu, mask);
854}
855#else
856static void __cpuinit numa_set_cpumask(int cpu, int enable)
857{
858 int node = early_cpu_to_node(cpu);
859 struct cpumask *mask;
David Rientjesc1c34432010-12-22 17:23:54 -0800860 int i;
Brian Gerst6470aff2009-01-27 12:56:47 +0900861
David Rientjesc1c34432010-12-22 17:23:54 -0800862 for_each_online_node(i) {
863 unsigned long addr;
864
865 addr = node_start_pfn(i) << PAGE_SHIFT;
866 if (addr < physnodes[node].start ||
867 addr >= physnodes[node].end)
868 continue;
David Rientjesd906f0e2010-12-30 10:54:16 -0800869 mask = debug_cpumask_set_cpu(cpu, enable);
870 if (!mask)
David Rientjesc1c34432010-12-22 17:23:54 -0800871 return;
David Rientjesc1c34432010-12-22 17:23:54 -0800872
873 if (enable)
874 cpumask_set_cpu(cpu, mask);
875 else
876 cpumask_clear_cpu(cpu, mask);
Brian Gerst6470aff2009-01-27 12:56:47 +0900877 }
Brian Gerst6470aff2009-01-27 12:56:47 +0900878}
David Rientjesd906f0e2010-12-30 10:54:16 -0800879#endif /* CONFIG_NUMA_EMU */
Brian Gerst6470aff2009-01-27 12:56:47 +0900880
881void __cpuinit numa_add_cpu(int cpu)
882{
883 numa_set_cpumask(cpu, 1);
884}
885
886void __cpuinit numa_remove_cpu(int cpu)
887{
888 numa_set_cpumask(cpu, 0);
889}
890
Lee Schermerhorne534c7c2010-05-26 14:44:58 -0700891int __cpu_to_node(int cpu)
Brian Gerst6470aff2009-01-27 12:56:47 +0900892{
893 if (early_per_cpu_ptr(x86_cpu_to_node_map)) {
894 printk(KERN_WARNING
895 "cpu_to_node(%d): usage too early!\n", cpu);
896 dump_stack();
897 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
898 }
899 return per_cpu(x86_cpu_to_node_map, cpu);
900}
Lee Schermerhorne534c7c2010-05-26 14:44:58 -0700901EXPORT_SYMBOL(__cpu_to_node);
Brian Gerst6470aff2009-01-27 12:56:47 +0900902
903/*
904 * Same function as cpu_to_node() but used if called before the
905 * per_cpu areas are setup.
906 */
907int early_cpu_to_node(int cpu)
908{
909 if (early_per_cpu_ptr(x86_cpu_to_node_map))
910 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
911
Brian Gerst44581a22009-02-08 09:58:40 -0500912 if (!cpu_possible(cpu)) {
Brian Gerst6470aff2009-01-27 12:56:47 +0900913 printk(KERN_WARNING
914 "early_cpu_to_node(%d): no per_cpu area!\n", cpu);
915 dump_stack();
916 return NUMA_NO_NODE;
917 }
918 return per_cpu(x86_cpu_to_node_map, cpu);
919}
920
Brian Gerst6470aff2009-01-27 12:56:47 +0900921/*
Brian Gerst6470aff2009-01-27 12:56:47 +0900922 * --------- end of debug versions of the numa functions ---------
923 */
924
925#endif /* CONFIG_DEBUG_PER_CPU_MAPS */