blob: 95ea1551eebca344bee38ebc160ba8f3057c1734 [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
Thomas Gleixner864fc312008-05-12 15:43:36 +020033static unsigned long __initdata nodemap_addr;
34static unsigned long __initdata nodemap_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Brian Gerst6470aff2009-01-27 12:56:47 +090036/*
37 * Map cpu index to node index
38 */
39DEFINE_EARLY_PER_CPU(int, x86_cpu_to_node_map, NUMA_NO_NODE);
40EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_node_map);
41
42/*
Eric Dumazet529a3402005-11-05 17:25:54 +010043 * Given a shift value, try to populate memnodemap[]
44 * Returns :
45 * 1 if OK
46 * 0 if memnodmap[] too small (of shift too small)
47 * -1 if node overlap or lost ram (shift too big)
48 */
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010049static int __init populate_memnodemap(const struct bootnode *nodes,
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -070050 int numnodes, int shift, int *nodeids)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Eric Dumazet529a3402005-11-05 17:25:54 +010052 unsigned long addr, end;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010053 int i, res = -1;
Keith Manntheyb6846642005-07-28 21:15:38 -070054
travis@sgi.com43238382008-01-30 13:33:25 +010055 memset(memnodemap, 0xff, sizeof(s16)*memnodemapsize);
Eric Dumazet529a3402005-11-05 17:25:54 +010056 for (i = 0; i < numnodes; i++) {
57 addr = nodes[i].start;
58 end = nodes[i].end;
59 if (addr >= end)
60 continue;
Amul Shah076422d2007-02-13 13:26:19 +010061 if ((end >> shift) >= memnodemapsize)
Eric Dumazet529a3402005-11-05 17:25:54 +010062 return 0;
63 do {
travis@sgi.com43238382008-01-30 13:33:25 +010064 if (memnodemap[addr >> shift] != NUMA_NO_NODE)
Eric Dumazet529a3402005-11-05 17:25:54 +010065 return -1;
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -070066
67 if (!nodeids)
68 memnodemap[addr >> shift] = i;
69 else
70 memnodemap[addr >> shift] = nodeids[i];
71
Amul Shah076422d2007-02-13 13:26:19 +010072 addr += (1UL << shift);
Eric Dumazet529a3402005-11-05 17:25:54 +010073 } while (addr < end);
74 res = 1;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010075 }
Eric Dumazet529a3402005-11-05 17:25:54 +010076 return res;
77}
78
Amul Shah076422d2007-02-13 13:26:19 +010079static int __init allocate_cachealigned_memnodemap(void)
80{
Yinghai Lu24a5da72008-02-01 17:49:41 +010081 unsigned long addr;
Amul Shah076422d2007-02-13 13:26:19 +010082
83 memnodemap = memnode.embedded_map;
travis@sgi.com316390b2008-01-30 13:33:15 +010084 if (memnodemapsize <= ARRAY_SIZE(memnode.embedded_map))
Amul Shah076422d2007-02-13 13:26:19 +010085 return 0;
Amul Shah076422d2007-02-13 13:26:19 +010086
Yinghai Lu24a5da72008-02-01 17:49:41 +010087 addr = 0x8000;
Joerg Roedelbe3e89e2008-07-25 16:48:58 +020088 nodemap_size = roundup(sizeof(s16) * memnodemapsize, L1_CACHE_BYTES);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070089 nodemap_addr = memblock_find_in_range(addr, max_pfn<<PAGE_SHIFT,
Yinghai Lu24a5da72008-02-01 17:49:41 +010090 nodemap_size, L1_CACHE_BYTES);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070091 if (nodemap_addr == MEMBLOCK_ERROR) {
Amul Shah076422d2007-02-13 13:26:19 +010092 printk(KERN_ERR
93 "NUMA: Unable to allocate Memory to Node hash map\n");
94 nodemap_addr = nodemap_size = 0;
95 return -1;
96 }
Yinghai Lu24a5da72008-02-01 17:49:41 +010097 memnodemap = phys_to_virt(nodemap_addr);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070098 memblock_x86_reserve_range(nodemap_addr, nodemap_addr + nodemap_size, "MEMNODEMAP");
Amul Shah076422d2007-02-13 13:26:19 +010099
100 printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n",
101 nodemap_addr, nodemap_addr + nodemap_size);
102 return 0;
103}
104
105/*
106 * The LSB of all start and end addresses in the node map is the value of the
107 * maximum possible shift.
108 */
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100109static int __init extract_lsb_from_nodes(const struct bootnode *nodes,
110 int numnodes)
Amul Shah076422d2007-02-13 13:26:19 +0100111{
Amul Shah54413922007-02-13 13:26:20 +0100112 int i, nodes_used = 0;
Amul Shah076422d2007-02-13 13:26:19 +0100113 unsigned long start, end;
114 unsigned long bitfield = 0, memtop = 0;
115
116 for (i = 0; i < numnodes; i++) {
117 start = nodes[i].start;
118 end = nodes[i].end;
119 if (start >= end)
120 continue;
Amul Shah54413922007-02-13 13:26:20 +0100121 bitfield |= start;
122 nodes_used++;
Amul Shah076422d2007-02-13 13:26:19 +0100123 if (end > memtop)
124 memtop = end;
125 }
Amul Shah54413922007-02-13 13:26:20 +0100126 if (nodes_used <= 1)
127 i = 63;
128 else
129 i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
Amul Shah076422d2007-02-13 13:26:19 +0100130 memnodemapsize = (memtop >> i)+1;
131 return i;
132}
133
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700134int __init compute_hash_shift(struct bootnode *nodes, int numnodes,
135 int *nodeids)
Eric Dumazet529a3402005-11-05 17:25:54 +0100136{
Amul Shah076422d2007-02-13 13:26:19 +0100137 int shift;
Eric Dumazet529a3402005-11-05 17:25:54 +0100138
Amul Shah076422d2007-02-13 13:26:19 +0100139 shift = extract_lsb_from_nodes(nodes, numnodes);
140 if (allocate_cachealigned_memnodemap())
141 return -1;
Andi Kleen6b050f82006-01-11 22:44:33 +0100142 printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
Eric Dumazet529a3402005-11-05 17:25:54 +0100143 shift);
144
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700145 if (populate_memnodemap(nodes, numnodes, shift, nodeids) != 1) {
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100146 printk(KERN_INFO "Your memory is not aligned you need to "
147 "rebuild your kernel with a bigger NODEMAPSIZE "
148 "shift=%d\n", shift);
Eric Dumazet529a3402005-11-05 17:25:54 +0100149 return -1;
150 }
Keith Manntheyb6846642005-07-28 21:15:38 -0700151 return shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -0800154int __meminit __early_pfn_to_nid(unsigned long pfn)
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700155{
156 return phys_to_nid(pfn << PAGE_SHIFT);
157}
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700158
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100159static void * __init early_node_mem(int nodeid, unsigned long start,
Yinghai Lu24a5da72008-02-01 17:49:41 +0100160 unsigned long end, unsigned long size,
161 unsigned long align)
Andi Kleena8062232006-04-07 19:49:21 +0200162{
Yinghai Lucef625e2010-02-10 01:20:18 -0800163 unsigned long mem;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100164
Yinghai Lucef625e2010-02-10 01:20:18 -0800165 /*
166 * put it on high as possible
167 * something will go with NODE_DATA
168 */
169 if (start < (MAX_DMA_PFN<<PAGE_SHIFT))
170 start = MAX_DMA_PFN<<PAGE_SHIFT;
171 if (start < (MAX_DMA32_PFN<<PAGE_SHIFT) &&
172 end > (MAX_DMA32_PFN<<PAGE_SHIFT))
173 start = MAX_DMA32_PFN<<PAGE_SHIFT;
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700174 mem = memblock_x86_find_in_range_node(nodeid, start, end, size, align);
175 if (mem != MEMBLOCK_ERROR)
Andi Kleena8062232006-04-07 19:49:21 +0200176 return __va(mem);
Yinghai Lu9347e0b2008-02-01 17:49:42 +0100177
Yinghai Lucef625e2010-02-10 01:20:18 -0800178 /* extend the search scope */
179 end = max_pfn_mapped << PAGE_SHIFT;
Yinghai Lu419db272010-10-28 09:50:17 -0700180 start = MAX_DMA_PFN << PAGE_SHIFT;
181 mem = memblock_find_in_range(start, end, size, align);
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700182 if (mem != MEMBLOCK_ERROR)
Yinghai Lu1842f902010-02-10 01:20:15 -0800183 return __va(mem);
184
185 printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100186 size, nodeid);
Yinghai Lu1842f902010-02-10 01:20:15 -0800187
188 return NULL;
Andi Kleena8062232006-04-07 19:49:21 +0200189}
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/* Initialize bootmem allocator for a node */
Yinghai Lu7c437692009-05-15 13:59:37 -0700192void __init
193setup_node_bootmem(int nodeid, unsigned long start, unsigned long end)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100194{
Yinghai Lu08677212010-02-10 01:20:20 -0800195 unsigned long start_pfn, last_pfn, nodedata_phys;
Yinghai Lu7c437692009-05-15 13:59:37 -0700196 const int pgdat_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
Yinghai Lu1a27fc02008-03-18 12:52:37 -0700197 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Yinghai Lu4c31e922009-04-22 14:19:27 -0700199 if (!end)
200 return;
201
Yinghai Lu7c437692009-05-15 13:59:37 -0700202 /*
203 * Don't confuse VM with a node that doesn't have the
204 * minimum amount of memory:
205 */
206 if (end && (end - start) < NODE_MIN_SIZE)
207 return;
208
Joerg Roedelbe3e89e2008-07-25 16:48:58 +0200209 start = roundup(start, ZONE_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Yinghai Lu08677212010-02-10 01:20:20 -0800211 printk(KERN_INFO "Initmem setup node %d %016lx-%016lx\n", nodeid,
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100212 start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 start_pfn = start >> PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200215 last_pfn = end >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Yinghai Lu24a5da72008-02-01 17:49:41 +0100217 node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
218 SMP_CACHE_BYTES);
Andi Kleena8062232006-04-07 19:49:21 +0200219 if (node_data[nodeid] == NULL)
220 return;
221 nodedata_phys = __pa(node_data[nodeid]);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700222 memblock_x86_reserve_range(nodedata_phys, nodedata_phys + pgdat_size, "NODE_DATA");
Yinghai Lu6118f762008-02-04 16:47:56 +0100223 printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
224 nodedata_phys + pgdat_size - 1);
Yinghai Lu1842f902010-02-10 01:20:15 -0800225 nid = phys_to_nid(nodedata_phys);
226 if (nid != nodeid)
227 printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nodeid, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
Yinghai Lu08677212010-02-10 01:20:20 -0800230 NODE_DATA(nodeid)->node_id = nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 NODE_DATA(nodeid)->node_start_pfn = start_pfn;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200232 NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 node_set_online(nodeid);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100235}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100237/*
238 * There are unfortunately some poorly designed mainboards around that
239 * only connect memory to a single CPU. This breaks the 1:1 cpu->node
240 * mapping. To avoid this fill in the mapping for all possible CPUs,
241 * as the number of CPUs is not known yet. We round robin the existing
242 * nodes.
243 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244void __init numa_init_array(void)
245{
246 int rr, i;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100247
Ravikiran G Thirumalai85cc5132005-09-30 11:59:22 -0700248 rr = first_node(node_online_map);
Mike Travis168ef542008-12-16 17:34:01 -0800249 for (i = 0; i < nr_cpu_ids; i++) {
travis@sgi.com1ce35712008-01-30 13:33:33 +0100250 if (early_cpu_to_node(i) != NUMA_NO_NODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 continue;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100252 numa_set_node(i, rr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 rr = next_node(rr, node_online_map);
254 if (rr == MAX_NUMNODES)
255 rr = first_node(node_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
259#ifdef CONFIG_NUMA_EMU
Rohit Seth53fee042007-02-13 13:26:22 +0100260/* Numa emulation */
David Rientjesadc19382009-09-25 15:20:09 -0700261static struct bootnode nodes[MAX_NUMNODES] __initdata;
David Rientjesc1c34432010-12-22 17:23:54 -0800262static struct bootnode physnodes[MAX_NUMNODES] __cpuinitdata;
Thomas Gleixner864fc312008-05-12 15:43:36 +0200263static char *cmdline __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Jan Beulich90321602011-01-19 08:57:21 +0000265void __init numa_emu_cmdline(char *str)
266{
267 cmdline = str;
268}
269
David Rientjesadc19382009-09-25 15:20:09 -0700270static int __init setup_physnodes(unsigned long start, unsigned long end,
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200271 int acpi, int amd)
David Rientjesadc19382009-09-25 15:20:09 -0700272{
David Rientjesadc19382009-09-25 15:20:09 -0700273 int ret = 0;
274 int i;
275
David Rientjesc1c34432010-12-22 17:23:54 -0800276 memset(physnodes, 0, sizeof(physnodes));
David Rientjesadc19382009-09-25 15:20:09 -0700277#ifdef CONFIG_ACPI_NUMA
278 if (acpi)
David Rientjesa387e952010-12-22 17:23:56 -0800279 acpi_get_nodes(physnodes, start, end);
David Rientjesadc19382009-09-25 15:20:09 -0700280#endif
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200281#ifdef CONFIG_AMD_NUMA
282 if (amd)
David Rientjesa387e952010-12-22 17:23:56 -0800283 amd_get_nodes(physnodes);
David Rientjesadc19382009-09-25 15:20:09 -0700284#endif
285 /*
286 * Basic sanity checking on the physical node map: there may be errors
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200287 * if the SRAT or AMD code incorrectly reported the topology or the mem=
David Rientjesadc19382009-09-25 15:20:09 -0700288 * kernel parameter is used.
289 */
David Rientjesa387e952010-12-22 17:23:56 -0800290 for (i = 0; i < MAX_NUMNODES; i++) {
David Rientjesadc19382009-09-25 15:20:09 -0700291 if (physnodes[i].start == physnodes[i].end)
292 continue;
293 if (physnodes[i].start > end) {
294 physnodes[i].end = physnodes[i].start;
295 continue;
296 }
297 if (physnodes[i].end < start) {
298 physnodes[i].start = physnodes[i].end;
299 continue;
300 }
301 if (physnodes[i].start < start)
302 physnodes[i].start = start;
303 if (physnodes[i].end > end)
304 physnodes[i].end = end;
David Rientjesadc19382009-09-25 15:20:09 -0700305 ret++;
306 }
307
308 /*
309 * If no physical topology was detected, a single node is faked to cover
310 * the entire address space.
311 */
312 if (!ret) {
313 physnodes[ret].start = start;
314 physnodes[ret].end = end;
315 ret = 1;
316 }
317 return ret;
318}
319
David Rientjesf51bf302010-12-22 17:23:51 -0800320static void __init fake_physnodes(int acpi, int amd, int nr_nodes)
321{
322 int i;
323
324 BUG_ON(acpi && amd);
325#ifdef CONFIG_ACPI_NUMA
326 if (acpi)
327 acpi_fake_nodes(nodes, nr_nodes);
328#endif
329#ifdef CONFIG_AMD_NUMA
330 if (amd)
331 amd_fake_nodes(nodes, nr_nodes);
332#endif
333 if (!acpi && !amd)
334 for (i = 0; i < nr_cpu_ids; i++)
335 numa_set_node(i, 0);
336}
337
Rohit Seth53fee042007-02-13 13:26:22 +0100338/*
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100339 * Setups up nid to range from addr to addr + size. If the end
340 * boundary is greater than max_addr, then max_addr is used instead.
341 * The return value is 0 if there is additional memory left for
342 * allocation past addr and -1 otherwise. addr is adjusted to be at
343 * the end of the node.
Rohit Seth53fee042007-02-13 13:26:22 +0100344 */
David Rientjesadc19382009-09-25 15:20:09 -0700345static int __init setup_node_range(int nid, u64 *addr, u64 size, u64 max_addr)
Rohit Seth53fee042007-02-13 13:26:22 +0100346{
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200347 int ret = 0;
348 nodes[nid].start = *addr;
349 *addr += size;
350 if (*addr >= max_addr) {
351 *addr = max_addr;
352 ret = -1;
353 }
354 nodes[nid].end = *addr;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200355 node_set(nid, node_possible_map);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200356 printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
357 nodes[nid].start, nodes[nid].end,
358 (nodes[nid].end - nodes[nid].start) >> 20);
359 return ret;
Rohit Seth53fee042007-02-13 13:26:22 +0100360}
361
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200362/*
David Rientjesadc19382009-09-25 15:20:09 -0700363 * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
364 * to max_addr. The return value is the number of nodes allocated.
365 */
David Rientjesc1c34432010-12-22 17:23:54 -0800366static int __init split_nodes_interleave(u64 addr, u64 max_addr, int nr_nodes)
David Rientjesadc19382009-09-25 15:20:09 -0700367{
368 nodemask_t physnode_mask = NODE_MASK_NONE;
369 u64 size;
370 int big;
371 int ret = 0;
372 int i;
373
374 if (nr_nodes <= 0)
375 return -1;
376 if (nr_nodes > MAX_NUMNODES) {
377 pr_info("numa=fake=%d too large, reducing to %d\n",
378 nr_nodes, MAX_NUMNODES);
379 nr_nodes = MAX_NUMNODES;
380 }
381
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700382 size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) / nr_nodes;
David Rientjesadc19382009-09-25 15:20:09 -0700383 /*
384 * Calculate the number of big nodes that can be allocated as a result
385 * of consolidating the remainder.
386 */
David Rientjes68fd1112010-02-15 13:43:25 -0800387 big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
David Rientjesadc19382009-09-25 15:20:09 -0700388 FAKE_NODE_MIN_SIZE;
389
390 size &= FAKE_NODE_MIN_HASH_MASK;
391 if (!size) {
392 pr_err("Not enough memory for each node. "
393 "NUMA emulation disabled.\n");
394 return -1;
395 }
396
David Rientjesc1c34432010-12-22 17:23:54 -0800397 for (i = 0; i < MAX_NUMNODES; i++)
David Rientjesadc19382009-09-25 15:20:09 -0700398 if (physnodes[i].start != physnodes[i].end)
399 node_set(i, physnode_mask);
400
401 /*
402 * Continue to fill physical nodes with fake nodes until there is no
403 * memory left on any of them.
404 */
405 while (nodes_weight(physnode_mask)) {
406 for_each_node_mask(i, physnode_mask) {
407 u64 end = physnodes[i].start + size;
408 u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
409
410 if (ret < big)
411 end += FAKE_NODE_MIN_SIZE;
412
413 /*
414 * Continue to add memory to this fake node if its
415 * non-reserved memory is less than the per-node size.
416 */
417 while (end - physnodes[i].start -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700418 memblock_x86_hole_size(physnodes[i].start, end) < size) {
David Rientjesadc19382009-09-25 15:20:09 -0700419 end += FAKE_NODE_MIN_SIZE;
420 if (end > physnodes[i].end) {
421 end = physnodes[i].end;
422 break;
423 }
424 }
425
426 /*
427 * If there won't be at least FAKE_NODE_MIN_SIZE of
428 * non-reserved memory in ZONE_DMA32 for the next node,
429 * this one must extend to the boundary.
430 */
431 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700432 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjesadc19382009-09-25 15:20:09 -0700433 end = dma32_end;
434
435 /*
436 * If there won't be enough non-reserved memory for the
437 * next node, this one must extend to the end of the
438 * physical node.
439 */
440 if (physnodes[i].end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700441 memblock_x86_hole_size(end, physnodes[i].end) < size)
David Rientjesadc19382009-09-25 15:20:09 -0700442 end = physnodes[i].end;
443
444 /*
445 * Avoid allocating more nodes than requested, which can
446 * happen as a result of rounding down each node's size
447 * to FAKE_NODE_MIN_SIZE.
448 */
449 if (nodes_weight(physnode_mask) + ret >= nr_nodes)
450 end = physnodes[i].end;
451
452 if (setup_node_range(ret++, &physnodes[i].start,
453 end - physnodes[i].start,
454 physnodes[i].end) < 0)
455 node_clear(i, physnode_mask);
456 }
457 }
458 return ret;
459}
460
461/*
David Rientjes8df5bb342010-02-15 13:43:30 -0800462 * Returns the end address of a node so that there is at least `size' amount of
463 * non-reserved memory or `max_addr' is reached.
464 */
465static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
466{
467 u64 end = start + size;
468
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700469 while (end - start - memblock_x86_hole_size(start, end) < size) {
David Rientjes8df5bb342010-02-15 13:43:30 -0800470 end += FAKE_NODE_MIN_SIZE;
471 if (end > max_addr) {
472 end = max_addr;
473 break;
474 }
475 }
476 return end;
477}
478
479/*
480 * Sets up fake nodes of `size' interleaved over physical nodes ranging from
481 * `addr' to `max_addr'. The return value is the number of nodes allocated.
482 */
483static int __init split_nodes_size_interleave(u64 addr, u64 max_addr, u64 size)
484{
485 nodemask_t physnode_mask = NODE_MASK_NONE;
486 u64 min_size;
487 int ret = 0;
488 int i;
489
490 if (!size)
491 return -1;
492 /*
493 * The limit on emulated nodes is MAX_NUMNODES, so the size per node is
494 * increased accordingly if the requested size is too small. This
495 * creates a uniform distribution of node sizes across the entire
496 * machine (but not necessarily over physical nodes).
497 */
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700498 min_size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) /
David Rientjes8df5bb342010-02-15 13:43:30 -0800499 MAX_NUMNODES;
500 min_size = max(min_size, FAKE_NODE_MIN_SIZE);
501 if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size)
502 min_size = (min_size + FAKE_NODE_MIN_SIZE) &
503 FAKE_NODE_MIN_HASH_MASK;
504 if (size < min_size) {
505 pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
506 size >> 20, min_size >> 20);
507 size = min_size;
508 }
509 size &= FAKE_NODE_MIN_HASH_MASK;
510
511 for (i = 0; i < MAX_NUMNODES; i++)
512 if (physnodes[i].start != physnodes[i].end)
513 node_set(i, physnode_mask);
514 /*
515 * Fill physical nodes with fake nodes of size until there is no memory
516 * left on any of them.
517 */
518 while (nodes_weight(physnode_mask)) {
519 for_each_node_mask(i, physnode_mask) {
520 u64 dma32_end = MAX_DMA32_PFN << PAGE_SHIFT;
521 u64 end;
522
523 end = find_end_of_node(physnodes[i].start,
524 physnodes[i].end, size);
525 /*
526 * If there won't be at least FAKE_NODE_MIN_SIZE of
527 * non-reserved memory in ZONE_DMA32 for the next node,
528 * this one must extend to the boundary.
529 */
530 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700531 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjes8df5bb342010-02-15 13:43:30 -0800532 end = dma32_end;
533
534 /*
535 * If there won't be enough non-reserved memory for the
536 * next node, this one must extend to the end of the
537 * physical node.
538 */
539 if (physnodes[i].end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700540 memblock_x86_hole_size(end, physnodes[i].end) < size)
David Rientjes8df5bb342010-02-15 13:43:30 -0800541 end = physnodes[i].end;
542
543 /*
544 * Setup the fake node that will be allocated as bootmem
545 * later. If setup_node_range() returns non-zero, there
546 * is no more memory available on this physical node.
547 */
548 if (setup_node_range(ret++, &physnodes[i].start,
549 end - physnodes[i].start,
550 physnodes[i].end) < 0)
551 node_clear(i, physnode_mask);
552 }
553 }
554 return ret;
555}
556
557/*
Thomas Gleixner886533a2008-05-12 15:43:36 +0200558 * Sets up the system RAM area from start_pfn to last_pfn according to the
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200559 * numa=fake command-line option.
560 */
David Rientjesadc19382009-09-25 15:20:09 -0700561static int __init numa_emulation(unsigned long start_pfn,
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200562 unsigned long last_pfn, int acpi, int amd)
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200563{
David Rientjesca2107c2010-02-15 13:43:33 -0800564 u64 addr = start_pfn << PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200565 u64 max_addr = last_pfn << PAGE_SHIFT;
David Rientjesca2107c2010-02-15 13:43:33 -0800566 int num_nodes;
567 int i;
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200568
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200569 /*
David Rientjes8df5bb342010-02-15 13:43:30 -0800570 * If the numa=fake command-line contains a 'M' or 'G', it represents
David Rientjesca2107c2010-02-15 13:43:33 -0800571 * the fixed node size. Otherwise, if it is just a single number N,
572 * split the system RAM into N fake nodes.
David Rientjes8df5bb342010-02-15 13:43:30 -0800573 */
574 if (strchr(cmdline, 'M') || strchr(cmdline, 'G')) {
David Rientjesca2107c2010-02-15 13:43:33 -0800575 u64 size;
576
David Rientjes8df5bb342010-02-15 13:43:30 -0800577 size = memparse(cmdline, &cmdline);
578 num_nodes = split_nodes_size_interleave(addr, max_addr, size);
David Rientjesca2107c2010-02-15 13:43:33 -0800579 } else {
580 unsigned long n;
581
582 n = simple_strtoul(cmdline, NULL, 0);
David Rientjesc1c34432010-12-22 17:23:54 -0800583 num_nodes = split_nodes_interleave(addr, max_addr, n);
David Rientjes8df5bb342010-02-15 13:43:30 -0800584 }
585
David Rientjesca2107c2010-02-15 13:43:33 -0800586 if (num_nodes < 0)
587 return num_nodes;
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700588 memnode_shift = compute_hash_shift(nodes, num_nodes, NULL);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200589 if (memnode_shift < 0) {
590 memnode_shift = 0;
591 printk(KERN_ERR "No NUMA hash function found. NUMA emulation "
592 "disabled.\n");
593 return -1;
594 }
595
596 /*
David Rientjesadc19382009-09-25 15:20:09 -0700597 * We need to vacate all active ranges that may have been registered for
598 * the e820 memory map.
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200599 */
600 remove_all_active_ranges();
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200601 for_each_node_mask(i, node_possible_map) {
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700602 memblock_x86_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
Mel Gorman5cb248a2006-09-27 01:49:52 -0700603 nodes[i].end >> PAGE_SHIFT);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100604 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
Mel Gorman5cb248a2006-09-27 01:49:52 -0700605 }
David Rientjesc1c34432010-12-22 17:23:54 -0800606 setup_physnodes(addr, max_addr, acpi, amd);
David Rientjesf51bf302010-12-22 17:23:51 -0800607 fake_physnodes(acpi, amd, num_nodes);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100608 numa_init_array();
609 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200611#endif /* CONFIG_NUMA_EMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
David Rientjes8ee2deb2009-09-25 15:20:00 -0700613void __init initmem_init(unsigned long start_pfn, unsigned long last_pfn,
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200614 int acpi, int amd)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100615{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 int i;
617
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200618 nodes_clear(node_possible_map);
Yinghai Lub7ad1492008-02-17 02:02:21 -0800619 nodes_clear(node_online_map);
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621#ifdef CONFIG_NUMA_EMU
David Rientjesc1c34432010-12-22 17:23:54 -0800622 setup_physnodes(start_pfn << PAGE_SHIFT, last_pfn << PAGE_SHIFT,
623 acpi, amd);
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200624 if (cmdline && !numa_emulation(start_pfn, last_pfn, acpi, amd))
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100625 return;
David Rientjesc1c34432010-12-22 17:23:54 -0800626 setup_physnodes(start_pfn << PAGE_SHIFT, last_pfn << PAGE_SHIFT,
627 acpi, amd);
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200628 nodes_clear(node_possible_map);
Yinghai Lub7ad1492008-02-17 02:02:21 -0800629 nodes_clear(node_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630#endif
631
632#ifdef CONFIG_ACPI_NUMA
David Rientjes87162732009-09-25 15:20:04 -0700633 if (!numa_off && acpi && !acpi_scan_nodes(start_pfn << PAGE_SHIFT,
634 last_pfn << PAGE_SHIFT))
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100635 return;
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
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200640#ifdef CONFIG_AMD_NUMA
641 if (!numa_off && amd && !amd_scan_nodes())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200643 nodes_clear(node_possible_map);
Yinghai Lub7ad1492008-02-17 02:02:21 -0800644 nodes_clear(node_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645#endif
646 printk(KERN_INFO "%s\n",
647 numa_off ? "NUMA turned off" : "No NUMA configuration found");
648
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100649 printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 start_pfn << PAGE_SHIFT,
Thomas Gleixner886533a2008-05-12 15:43:36 +0200651 last_pfn << PAGE_SHIFT);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100652 /* setup dummy node covering all memory */
653 memnode_shift = 63;
Amul Shah076422d2007-02-13 13:26:19 +0100654 memnodemap = memnode.embedded_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 memnodemap[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 node_set_online(0);
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200657 node_set(0, node_possible_map);
Mike Travis168ef542008-12-16 17:34:01 -0800658 for (i = 0; i < nr_cpu_ids; i++)
Andi Kleen69d81fc2005-11-05 17:25:53 +0100659 numa_set_node(i, 0);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700660 memblock_x86_register_active_regions(0, start_pfn, last_pfn);
Thomas Gleixner886533a2008-05-12 15:43:36 +0200661 setup_node_bootmem(0, start_pfn << PAGE_SHIFT, last_pfn << PAGE_SHIFT);
Andi Kleen69d81fc2005-11-05 17:25:53 +0100662}
663
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100664unsigned long __init numa_free_all_bootmem(void)
665{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 unsigned long pages = 0;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100667 int i;
668
669 for_each_online_node(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 pages += free_all_bootmem_node(NODE_DATA(i));
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100671
Yinghai Lu08677212010-02-10 01:20:20 -0800672 pages += free_all_memory_core_early(MAX_NUMNODES);
Yinghai Lu08677212010-02-10 01:20:20 -0800673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return pages;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100675}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Mike Travis23ca4bb2008-05-12 21:21:12 +0200677#ifdef CONFIG_NUMA
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800678
679static __init int find_near_online_node(int node)
680{
681 int n, val;
682 int min_val = INT_MAX;
683 int best_node = -1;
684
685 for_each_online_node(n) {
686 val = node_distance(node, n);
687
688 if (val < min_val) {
689 min_val = val;
690 best_node = n;
691 }
692 }
693
694 return best_node;
695}
696
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100697/*
698 * Setup early cpu_to_node.
699 *
700 * Populate cpu_to_node[] only if x86_cpu_to_apicid[],
701 * and apicid_to_node[] tables have valid entries for a CPU.
702 * This means we skip cpu_to_node[] initialisation for NUMA
703 * emulation and faking node case (when running a kernel compiled
704 * for NUMA on a non NUMA box), which is OK as cpu_to_node[]
705 * is already initialized in a round robin manner at numa_init_array,
706 * prior to this call, and this initialization is good enough
707 * for the fake NUMA cases.
Mike Travis23ca4bb2008-05-12 21:21:12 +0200708 *
709 * Called before the per_cpu areas are setup.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100710 */
711void __init init_cpu_to_node(void)
712{
Mike Travis23ca4bb2008-05-12 21:21:12 +0200713 int cpu;
714 u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100715
Mike Travis23ca4bb2008-05-12 21:21:12 +0200716 BUG_ON(cpu_to_apicid == NULL);
717
718 for_each_possible_cpu(cpu) {
Yinghai Lu7c9e92b62008-02-19 15:35:54 -0800719 int node;
Mike Travis23ca4bb2008-05-12 21:21:12 +0200720 u16 apicid = cpu_to_apicid[cpu];
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100721
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100722 if (apicid == BAD_APICID)
723 continue;
Yinghai Lu7c9e92b62008-02-19 15:35:54 -0800724 node = apicid_to_node[apicid];
725 if (node == NUMA_NO_NODE)
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100726 continue;
Yinghai Lu7c9e92b62008-02-19 15:35:54 -0800727 if (!node_online(node))
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800728 node = find_near_online_node(node);
Mike Travis23ca4bb2008-05-12 21:21:12 +0200729 numa_set_node(cpu, node);
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100730 }
731}
Mike Travis23ca4bb2008-05-12 21:21:12 +0200732#endif
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100733
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;
779 u16 apicid;
780 int physnid;
781 int nid = NUMA_NO_NODE;
782
783 apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
784 if (apicid != BAD_APICID)
785 nid = apicid_to_node[apicid];
786 if (nid == NUMA_NO_NODE)
787 nid = early_cpu_to_node(cpu);
788 BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
789
790 /*
791 * Use the starting address of the emulated node to find which physical
792 * node it is allocated on.
793 */
794 addr = node_start_pfn(nid) << PAGE_SHIFT;
795 for (physnid = 0; physnid < MAX_NUMNODES; physnid++)
796 if (addr >= physnodes[physnid].start &&
797 addr < physnodes[physnid].end)
798 break;
799
800 /*
801 * Map the cpu to each emulated node that is allocated on the physical
802 * node of the cpu's apic id.
803 */
804 for_each_online_node(nid) {
805 addr = node_start_pfn(nid) << PAGE_SHIFT;
806 if (addr >= physnodes[physnid].start &&
807 addr < physnodes[physnid].end)
808 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
809 }
810}
811
812void __cpuinit numa_remove_cpu(int cpu)
813{
814 int i;
815
816 for_each_online_node(i)
817 cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
818}
819#endif /* !CONFIG_NUMA_EMU */
Brian Gerst6470aff2009-01-27 12:56:47 +0900820
821#else /* CONFIG_DEBUG_PER_CPU_MAPS */
David Rientjesd906f0e2010-12-30 10:54:16 -0800822static struct cpumask __cpuinit *debug_cpumask_set_cpu(int cpu, int enable)
Brian Gerst6470aff2009-01-27 12:56:47 +0900823{
824 int node = early_cpu_to_node(cpu);
Rusty Russell73e907d2009-03-13 14:49:57 +1030825 struct cpumask *mask;
Brian Gerst6470aff2009-01-27 12:56:47 +0900826 char buf[64];
David Rientjesd906f0e2010-12-30 10:54:16 -0800827
828 mask = node_to_cpumask_map[node];
829 if (!mask) {
830 pr_err("node_to_cpumask_map[%i] NULL\n", node);
831 dump_stack();
832 return NULL;
833 }
834
835 cpulist_scnprintf(buf, sizeof(buf), mask);
836 printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n",
837 enable ? "numa_add_cpu" : "numa_remove_cpu",
838 cpu, node, buf);
839 return mask;
840}
841
842/*
843 * --------- debug versions of the numa functions ---------
844 */
845#ifndef CONFIG_NUMA_EMU
846static void __cpuinit numa_set_cpumask(int cpu, int enable)
847{
848 struct cpumask *mask;
849
850 mask = debug_cpumask_set_cpu(cpu, enable);
851 if (!mask)
852 return;
853
854 if (enable)
855 cpumask_set_cpu(cpu, mask);
856 else
857 cpumask_clear_cpu(cpu, mask);
858}
859#else
860static void __cpuinit numa_set_cpumask(int cpu, int enable)
861{
862 int node = early_cpu_to_node(cpu);
863 struct cpumask *mask;
David Rientjesc1c34432010-12-22 17:23:54 -0800864 int i;
Brian Gerst6470aff2009-01-27 12:56:47 +0900865
David Rientjesc1c34432010-12-22 17:23:54 -0800866 for_each_online_node(i) {
867 unsigned long addr;
868
869 addr = node_start_pfn(i) << PAGE_SHIFT;
870 if (addr < physnodes[node].start ||
871 addr >= physnodes[node].end)
872 continue;
David Rientjesd906f0e2010-12-30 10:54:16 -0800873 mask = debug_cpumask_set_cpu(cpu, enable);
874 if (!mask)
David Rientjesc1c34432010-12-22 17:23:54 -0800875 return;
David Rientjesc1c34432010-12-22 17:23:54 -0800876
877 if (enable)
878 cpumask_set_cpu(cpu, mask);
879 else
880 cpumask_clear_cpu(cpu, mask);
Brian Gerst6470aff2009-01-27 12:56:47 +0900881 }
Brian Gerst6470aff2009-01-27 12:56:47 +0900882}
David Rientjesd906f0e2010-12-30 10:54:16 -0800883#endif /* CONFIG_NUMA_EMU */
Brian Gerst6470aff2009-01-27 12:56:47 +0900884
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 */