blob: 086f98a66d80a3291fda61afe2933779ec462877 [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>
10#include <linux/mmzone.h>
11#include <linux/ctype.h>
12#include <linux/module.h>
13#include <linux/nodemask.h>
travis@sgi.com3cc87e32008-01-30 13:33:11 +010014#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#include <asm/e820.h>
17#include <asm/proto.h>
18#include <asm/dma.h>
19#include <asm/numa.h>
20#include <asm/acpi.h>
Thomas Gleixnerc9ff0342008-01-30 13:30:16 +010021#include <asm/k8.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -070023struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010024EXPORT_SYMBOL(node_data);
25
Eric Dumazetdcf36bf2006-03-25 16:31:46 +010026struct memnode memnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
travis@sgi.com43238382008-01-30 13:33:25 +010028s16 apicid_to_node[MAX_LOCAL_APIC] __cpuinitdata = {
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010029 [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
Andi Kleen3f098c22005-09-12 18:49:24 +020030};
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032int numa_off __initdata;
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 +090036DEFINE_PER_CPU(int, node_number) = 0;
37EXPORT_PER_CPU_SYMBOL(node_number);
38
39/*
40 * Map cpu index to node index
41 */
42DEFINE_EARLY_PER_CPU(int, x86_cpu_to_node_map, NUMA_NO_NODE);
43EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_node_map);
44
45/*
Eric Dumazet529a3402005-11-05 17:25:54 +010046 * Given a shift value, try to populate memnodemap[]
47 * Returns :
48 * 1 if OK
49 * 0 if memnodmap[] too small (of shift too small)
50 * -1 if node overlap or lost ram (shift too big)
51 */
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010052static int __init populate_memnodemap(const struct bootnode *nodes,
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -070053 int numnodes, int shift, int *nodeids)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Eric Dumazet529a3402005-11-05 17:25:54 +010055 unsigned long addr, end;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010056 int i, res = -1;
Keith Manntheyb6846642005-07-28 21:15:38 -070057
travis@sgi.com43238382008-01-30 13:33:25 +010058 memset(memnodemap, 0xff, sizeof(s16)*memnodemapsize);
Eric Dumazet529a3402005-11-05 17:25:54 +010059 for (i = 0; i < numnodes; i++) {
60 addr = nodes[i].start;
61 end = nodes[i].end;
62 if (addr >= end)
63 continue;
Amul Shah076422d2007-02-13 13:26:19 +010064 if ((end >> shift) >= memnodemapsize)
Eric Dumazet529a3402005-11-05 17:25:54 +010065 return 0;
66 do {
travis@sgi.com43238382008-01-30 13:33:25 +010067 if (memnodemap[addr >> shift] != NUMA_NO_NODE)
Eric Dumazet529a3402005-11-05 17:25:54 +010068 return -1;
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -070069
70 if (!nodeids)
71 memnodemap[addr >> shift] = i;
72 else
73 memnodemap[addr >> shift] = nodeids[i];
74
Amul Shah076422d2007-02-13 13:26:19 +010075 addr += (1UL << shift);
Eric Dumazet529a3402005-11-05 17:25:54 +010076 } while (addr < end);
77 res = 1;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010078 }
Eric Dumazet529a3402005-11-05 17:25:54 +010079 return res;
80}
81
Amul Shah076422d2007-02-13 13:26:19 +010082static int __init allocate_cachealigned_memnodemap(void)
83{
Yinghai Lu24a5da72008-02-01 17:49:41 +010084 unsigned long addr;
Amul Shah076422d2007-02-13 13:26:19 +010085
86 memnodemap = memnode.embedded_map;
travis@sgi.com316390b2008-01-30 13:33:15 +010087 if (memnodemapsize <= ARRAY_SIZE(memnode.embedded_map))
Amul Shah076422d2007-02-13 13:26:19 +010088 return 0;
Amul Shah076422d2007-02-13 13:26:19 +010089
Yinghai Lu24a5da72008-02-01 17:49:41 +010090 addr = 0x8000;
Joerg Roedelbe3e89e2008-07-25 16:48:58 +020091 nodemap_size = roundup(sizeof(s16) * memnodemapsize, L1_CACHE_BYTES);
Yinghai Luc987d122008-06-24 22:14:09 -070092 nodemap_addr = find_e820_area(addr, max_pfn<<PAGE_SHIFT,
Yinghai Lu24a5da72008-02-01 17:49:41 +010093 nodemap_size, L1_CACHE_BYTES);
Amul Shah076422d2007-02-13 13:26:19 +010094 if (nodemap_addr == -1UL) {
95 printk(KERN_ERR
96 "NUMA: Unable to allocate Memory to Node hash map\n");
97 nodemap_addr = nodemap_size = 0;
98 return -1;
99 }
Yinghai Lu24a5da72008-02-01 17:49:41 +0100100 memnodemap = phys_to_virt(nodemap_addr);
Yinghai Lu25eff8d2008-02-01 17:49:41 +0100101 reserve_early(nodemap_addr, nodemap_addr + nodemap_size, "MEMNODEMAP");
Amul Shah076422d2007-02-13 13:26:19 +0100102
103 printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n",
104 nodemap_addr, nodemap_addr + nodemap_size);
105 return 0;
106}
107
108/*
109 * The LSB of all start and end addresses in the node map is the value of the
110 * maximum possible shift.
111 */
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100112static int __init extract_lsb_from_nodes(const struct bootnode *nodes,
113 int numnodes)
Amul Shah076422d2007-02-13 13:26:19 +0100114{
Amul Shah54413922007-02-13 13:26:20 +0100115 int i, nodes_used = 0;
Amul Shah076422d2007-02-13 13:26:19 +0100116 unsigned long start, end;
117 unsigned long bitfield = 0, memtop = 0;
118
119 for (i = 0; i < numnodes; i++) {
120 start = nodes[i].start;
121 end = nodes[i].end;
122 if (start >= end)
123 continue;
Amul Shah54413922007-02-13 13:26:20 +0100124 bitfield |= start;
125 nodes_used++;
Amul Shah076422d2007-02-13 13:26:19 +0100126 if (end > memtop)
127 memtop = end;
128 }
Amul Shah54413922007-02-13 13:26:20 +0100129 if (nodes_used <= 1)
130 i = 63;
131 else
132 i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
Amul Shah076422d2007-02-13 13:26:19 +0100133 memnodemapsize = (memtop >> i)+1;
134 return i;
135}
136
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700137int __init compute_hash_shift(struct bootnode *nodes, int numnodes,
138 int *nodeids)
Eric Dumazet529a3402005-11-05 17:25:54 +0100139{
Amul Shah076422d2007-02-13 13:26:19 +0100140 int shift;
Eric Dumazet529a3402005-11-05 17:25:54 +0100141
Amul Shah076422d2007-02-13 13:26:19 +0100142 shift = extract_lsb_from_nodes(nodes, numnodes);
143 if (allocate_cachealigned_memnodemap())
144 return -1;
Andi Kleen6b050f82006-01-11 22:44:33 +0100145 printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
Eric Dumazet529a3402005-11-05 17:25:54 +0100146 shift);
147
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700148 if (populate_memnodemap(nodes, numnodes, shift, nodeids) != 1) {
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100149 printk(KERN_INFO "Your memory is not aligned you need to "
150 "rebuild your kernel with a bigger NODEMAPSIZE "
151 "shift=%d\n", shift);
Eric Dumazet529a3402005-11-05 17:25:54 +0100152 return -1;
153 }
Keith Manntheyb6846642005-07-28 21:15:38 -0700154 return shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -0800157int __meminit __early_pfn_to_nid(unsigned long pfn)
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700158{
159 return phys_to_nid(pfn << PAGE_SHIFT);
160}
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700161
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100162static void * __init early_node_mem(int nodeid, unsigned long start,
Yinghai Lu24a5da72008-02-01 17:49:41 +0100163 unsigned long end, unsigned long size,
164 unsigned long align)
Andi Kleena8062232006-04-07 19:49:21 +0200165{
Yinghai Lu24a5da72008-02-01 17:49:41 +0100166 unsigned long mem = find_e820_area(start, end, size, align);
Andi Kleena8062232006-04-07 19:49:21 +0200167 void *ptr;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100168
Yinghai Lu9347e0b2008-02-01 17:49:42 +0100169 if (mem != -1L)
Andi Kleena8062232006-04-07 19:49:21 +0200170 return __va(mem);
Yinghai Lu9347e0b2008-02-01 17:49:42 +0100171
Yinghai Lu24a5da72008-02-01 17:49:41 +0100172 ptr = __alloc_bootmem_nopanic(size, align, __pa(MAX_DMA_ADDRESS));
Yoann Padioleau83e83d52007-10-17 18:04:35 +0200173 if (ptr == NULL) {
Andi Kleena8062232006-04-07 19:49:21 +0200174 printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100175 size, nodeid);
Andi Kleena8062232006-04-07 19:49:21 +0200176 return NULL;
177 }
178 return ptr;
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/* Initialize bootmem allocator for a node */
Yinghai Lu7c437692009-05-15 13:59:37 -0700182void __init
183setup_node_bootmem(int nodeid, unsigned long start, unsigned long end)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100184{
Thomas Gleixner886533a2008-05-12 15:43:36 +0200185 unsigned long start_pfn, last_pfn, bootmap_pages, bootmap_size;
Yinghai Lu7c437692009-05-15 13:59:37 -0700186 const int pgdat_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100187 unsigned long bootmap_start, nodedata_phys;
Andi Kleena8062232006-04-07 19:49:21 +0200188 void *bootmap;
Yinghai Lu1a27fc02008-03-18 12:52:37 -0700189 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Yinghai Lu4c31e922009-04-22 14:19:27 -0700191 if (!end)
192 return;
193
Yinghai Lu7c437692009-05-15 13:59:37 -0700194 /*
195 * Don't confuse VM with a node that doesn't have the
196 * minimum amount of memory:
197 */
198 if (end && (end - start) < NODE_MIN_SIZE)
199 return;
200
Joerg Roedelbe3e89e2008-07-25 16:48:58 +0200201 start = roundup(start, ZONE_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100203 printk(KERN_INFO "Bootmem setup node %d %016lx-%016lx\n", nodeid,
204 start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 start_pfn = start >> PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200207 last_pfn = end >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Yinghai Lu24a5da72008-02-01 17:49:41 +0100209 node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
210 SMP_CACHE_BYTES);
Andi Kleena8062232006-04-07 19:49:21 +0200211 if (node_data[nodeid] == NULL)
212 return;
213 nodedata_phys = __pa(node_data[nodeid]);
Yinghai Lu6118f762008-02-04 16:47:56 +0100214 printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
215 nodedata_phys + pgdat_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
Johannes Weinerb61bfa32008-07-23 21:26:55 -0700218 NODE_DATA(nodeid)->bdata = &bootmem_node_data[nodeid];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 NODE_DATA(nodeid)->node_start_pfn = start_pfn;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200220 NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Yinghai Lu1a27fc02008-03-18 12:52:37 -0700222 /*
223 * Find a place for the bootmem map
224 * nodedata_phys could be on other nodes by alloc_bootmem,
225 * so need to sure bootmap_start not to be small, otherwise
226 * early_node_mem will get that with find_e820_area instead
227 * of alloc_bootmem, that could clash with reserved range
228 */
Thomas Gleixner886533a2008-05-12 15:43:36 +0200229 bootmap_pages = bootmem_bootmap_pages(last_pfn - start_pfn);
Yinghai Lu1a27fc02008-03-18 12:52:37 -0700230 nid = phys_to_nid(nodedata_phys);
231 if (nid == nodeid)
Joerg Roedelbe3e89e2008-07-25 16:48:58 +0200232 bootmap_start = roundup(nodedata_phys + pgdat_size, PAGE_SIZE);
Yinghai Lu1a27fc02008-03-18 12:52:37 -0700233 else
Joerg Roedelbe3e89e2008-07-25 16:48:58 +0200234 bootmap_start = roundup(start, PAGE_SIZE);
Yinghai Lu24a5da72008-02-01 17:49:41 +0100235 /*
Paul Jacksone9197bf2008-05-14 08:15:10 -0700236 * SMP_CACHE_BYTES could be enough, but init_bootmem_node like
Yinghai Lu24a5da72008-02-01 17:49:41 +0100237 * to use that to align to PAGE_SIZE
238 */
Andi Kleena8062232006-04-07 19:49:21 +0200239 bootmap = early_node_mem(nodeid, bootmap_start, end,
Yinghai Lu24a5da72008-02-01 17:49:41 +0100240 bootmap_pages<<PAGE_SHIFT, PAGE_SIZE);
Andi Kleena8062232006-04-07 19:49:21 +0200241 if (bootmap == NULL) {
242 if (nodedata_phys < start || nodedata_phys >= end)
Yinghai Lu37bff622008-03-18 12:40:04 -0700243 free_bootmem(nodedata_phys, pgdat_size);
Andi Kleena8062232006-04-07 19:49:21 +0200244 node_data[nodeid] = NULL;
245 return;
246 }
247 bootmap_start = __pa(bootmap);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 bootmap_size = init_bootmem_node(NODE_DATA(nodeid),
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100250 bootmap_start >> PAGE_SHIFT,
Thomas Gleixner886533a2008-05-12 15:43:36 +0200251 start_pfn, last_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Yinghai Lu6118f762008-02-04 16:47:56 +0100253 printk(KERN_INFO " bootmap [%016lx - %016lx] pages %lx\n",
254 bootmap_start, bootmap_start + bootmap_size - 1,
255 bootmap_pages);
256
Mel Gorman5cb248a2006-09-27 01:49:52 -0700257 free_bootmem_with_active_regions(nodeid, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Yinghai Lu1a27fc02008-03-18 12:52:37 -0700259 /*
260 * convert early reserve to bootmem reserve earlier
261 * otherwise early_node_mem could use early reserved mem
262 * on previous node
263 */
264 early_res_to_bootmem(start, end);
265
266 /*
267 * in some case early_node_mem could use alloc_bootmem
268 * to get range on other node, don't reserve that again
269 */
270 if (nid != nodeid)
271 printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nodeid, nid);
272 else
273 reserve_bootmem_node(NODE_DATA(nodeid), nodedata_phys,
274 pgdat_size, BOOTMEM_DEFAULT);
275 nid = phys_to_nid(bootmap_start);
276 if (nid != nodeid)
277 printk(KERN_INFO " bootmap(%d) on node %d\n", nodeid, nid);
278 else
279 reserve_bootmem_node(NODE_DATA(nodeid), bootmap_start,
280 bootmap_pages<<PAGE_SHIFT, BOOTMEM_DEFAULT);
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 node_set_online(nodeid);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100283}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100285/*
286 * There are unfortunately some poorly designed mainboards around that
287 * only connect memory to a single CPU. This breaks the 1:1 cpu->node
288 * mapping. To avoid this fill in the mapping for all possible CPUs,
289 * as the number of CPUs is not known yet. We round robin the existing
290 * nodes.
291 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292void __init numa_init_array(void)
293{
294 int rr, i;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100295
Ravikiran G Thirumalai85cc5132005-09-30 11:59:22 -0700296 rr = first_node(node_online_map);
Mike Travis168ef542008-12-16 17:34:01 -0800297 for (i = 0; i < nr_cpu_ids; i++) {
travis@sgi.com1ce35712008-01-30 13:33:33 +0100298 if (early_cpu_to_node(i) != NUMA_NO_NODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 continue;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100300 numa_set_node(i, rr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 rr = next_node(rr, node_online_map);
302 if (rr == MAX_NUMNODES)
303 rr = first_node(node_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307#ifdef CONFIG_NUMA_EMU
Rohit Seth53fee042007-02-13 13:26:22 +0100308/* Numa emulation */
David Rientjesadc19382009-09-25 15:20:09 -0700309static struct bootnode nodes[MAX_NUMNODES] __initdata;
310static struct bootnode physnodes[MAX_NUMNODES] __initdata;
Thomas Gleixner864fc312008-05-12 15:43:36 +0200311static char *cmdline __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
David Rientjesadc19382009-09-25 15:20:09 -0700313static int __init setup_physnodes(unsigned long start, unsigned long end,
314 int acpi, int k8)
315{
316 int nr_nodes = 0;
317 int ret = 0;
318 int i;
319
320#ifdef CONFIG_ACPI_NUMA
321 if (acpi)
322 nr_nodes = acpi_get_nodes(physnodes);
323#endif
324#ifdef CONFIG_K8_NUMA
325 if (k8)
326 nr_nodes = k8_get_nodes(physnodes);
327#endif
328 /*
329 * Basic sanity checking on the physical node map: there may be errors
330 * if the SRAT or K8 incorrectly reported the topology or the mem=
331 * kernel parameter is used.
332 */
333 for (i = 0; i < nr_nodes; i++) {
334 if (physnodes[i].start == physnodes[i].end)
335 continue;
336 if (physnodes[i].start > end) {
337 physnodes[i].end = physnodes[i].start;
338 continue;
339 }
340 if (physnodes[i].end < start) {
341 physnodes[i].start = physnodes[i].end;
342 continue;
343 }
344 if (physnodes[i].start < start)
345 physnodes[i].start = start;
346 if (physnodes[i].end > end)
347 physnodes[i].end = end;
348 }
349
350 /*
351 * Remove all nodes that have no memory or were truncated because of the
352 * limited address range.
353 */
354 for (i = 0; i < nr_nodes; i++) {
355 if (physnodes[i].start == physnodes[i].end)
356 continue;
357 physnodes[ret].start = physnodes[i].start;
358 physnodes[ret].end = physnodes[i].end;
359 ret++;
360 }
361
362 /*
363 * If no physical topology was detected, a single node is faked to cover
364 * the entire address space.
365 */
366 if (!ret) {
367 physnodes[ret].start = start;
368 physnodes[ret].end = end;
369 ret = 1;
370 }
371 return ret;
372}
373
Rohit Seth53fee042007-02-13 13:26:22 +0100374/*
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100375 * Setups up nid to range from addr to addr + size. If the end
376 * boundary is greater than max_addr, then max_addr is used instead.
377 * The return value is 0 if there is additional memory left for
378 * allocation past addr and -1 otherwise. addr is adjusted to be at
379 * the end of the node.
Rohit Seth53fee042007-02-13 13:26:22 +0100380 */
David Rientjesadc19382009-09-25 15:20:09 -0700381static int __init setup_node_range(int nid, u64 *addr, u64 size, u64 max_addr)
Rohit Seth53fee042007-02-13 13:26:22 +0100382{
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200383 int ret = 0;
384 nodes[nid].start = *addr;
385 *addr += size;
386 if (*addr >= max_addr) {
387 *addr = max_addr;
388 ret = -1;
389 }
390 nodes[nid].end = *addr;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200391 node_set(nid, node_possible_map);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200392 printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
393 nodes[nid].start, nodes[nid].end,
394 (nodes[nid].end - nodes[nid].start) >> 20);
395 return ret;
Rohit Seth53fee042007-02-13 13:26:22 +0100396}
397
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200398/*
David Rientjesadc19382009-09-25 15:20:09 -0700399 * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
400 * to max_addr. The return value is the number of nodes allocated.
401 */
402static int __init split_nodes_interleave(u64 addr, u64 max_addr,
403 int nr_phys_nodes, int nr_nodes)
404{
405 nodemask_t physnode_mask = NODE_MASK_NONE;
406 u64 size;
407 int big;
408 int ret = 0;
409 int i;
410
411 if (nr_nodes <= 0)
412 return -1;
413 if (nr_nodes > MAX_NUMNODES) {
414 pr_info("numa=fake=%d too large, reducing to %d\n",
415 nr_nodes, MAX_NUMNODES);
416 nr_nodes = MAX_NUMNODES;
417 }
418
419 size = (max_addr - addr - e820_hole_size(addr, max_addr)) / nr_nodes;
420 /*
421 * Calculate the number of big nodes that can be allocated as a result
422 * of consolidating the remainder.
423 */
424 big = ((size & ~FAKE_NODE_MIN_HASH_MASK) & nr_nodes) /
425 FAKE_NODE_MIN_SIZE;
426
427 size &= FAKE_NODE_MIN_HASH_MASK;
428 if (!size) {
429 pr_err("Not enough memory for each node. "
430 "NUMA emulation disabled.\n");
431 return -1;
432 }
433
434 for (i = 0; i < nr_phys_nodes; i++)
435 if (physnodes[i].start != physnodes[i].end)
436 node_set(i, physnode_mask);
437
438 /*
439 * Continue to fill physical nodes with fake nodes until there is no
440 * memory left on any of them.
441 */
442 while (nodes_weight(physnode_mask)) {
443 for_each_node_mask(i, physnode_mask) {
444 u64 end = physnodes[i].start + size;
445 u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
446
447 if (ret < big)
448 end += FAKE_NODE_MIN_SIZE;
449
450 /*
451 * Continue to add memory to this fake node if its
452 * non-reserved memory is less than the per-node size.
453 */
454 while (end - physnodes[i].start -
455 e820_hole_size(physnodes[i].start, end) < size) {
456 end += FAKE_NODE_MIN_SIZE;
457 if (end > physnodes[i].end) {
458 end = physnodes[i].end;
459 break;
460 }
461 }
462
463 /*
464 * If there won't be at least FAKE_NODE_MIN_SIZE of
465 * non-reserved memory in ZONE_DMA32 for the next node,
466 * this one must extend to the boundary.
467 */
468 if (end < dma32_end && dma32_end - end -
469 e820_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
470 end = dma32_end;
471
472 /*
473 * If there won't be enough non-reserved memory for the
474 * next node, this one must extend to the end of the
475 * physical node.
476 */
477 if (physnodes[i].end - end -
478 e820_hole_size(end, physnodes[i].end) < size)
479 end = physnodes[i].end;
480
481 /*
482 * Avoid allocating more nodes than requested, which can
483 * happen as a result of rounding down each node's size
484 * to FAKE_NODE_MIN_SIZE.
485 */
486 if (nodes_weight(physnode_mask) + ret >= nr_nodes)
487 end = physnodes[i].end;
488
489 if (setup_node_range(ret++, &physnodes[i].start,
490 end - physnodes[i].start,
491 physnodes[i].end) < 0)
492 node_clear(i, physnode_mask);
493 }
494 }
495 return ret;
496}
497
498/*
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200499 * Splits num_nodes nodes up equally starting at node_start. The return value
500 * is the number of nodes split up and addr is adjusted to be at the end of the
501 * last node allocated.
502 */
David Rientjesadc19382009-09-25 15:20:09 -0700503static int __init split_nodes_equally(u64 *addr, u64 max_addr, int node_start,
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200504 int num_nodes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200506 unsigned int big;
507 u64 size;
508 int i;
Rohit Seth53fee042007-02-13 13:26:22 +0100509
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200510 if (num_nodes <= 0)
511 return -1;
512 if (num_nodes > MAX_NUMNODES)
513 num_nodes = MAX_NUMNODES;
David Rientjesa7e96622007-07-21 17:11:29 +0200514 size = (max_addr - *addr - e820_hole_size(*addr, max_addr)) /
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200515 num_nodes;
Rohit Seth53fee042007-02-13 13:26:22 +0100516 /*
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200517 * Calculate the number of big nodes that can be allocated as a result
518 * of consolidating the leftovers.
Rohit Seth53fee042007-02-13 13:26:22 +0100519 */
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200520 big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * num_nodes) /
521 FAKE_NODE_MIN_SIZE;
Rohit Seth53fee042007-02-13 13:26:22 +0100522
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200523 /* Round down to nearest FAKE_NODE_MIN_SIZE. */
524 size &= FAKE_NODE_MIN_HASH_MASK;
525 if (!size) {
526 printk(KERN_ERR "Not enough memory for each node. "
527 "NUMA emulation disabled.\n");
528 return -1;
Rohit Seth53fee042007-02-13 13:26:22 +0100529 }
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200530
531 for (i = node_start; i < num_nodes + node_start; i++) {
532 u64 end = *addr + size;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100533
Rohit Seth53fee042007-02-13 13:26:22 +0100534 if (i < big)
535 end += FAKE_NODE_MIN_SIZE;
536 /*
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200537 * The final node can have the remaining system RAM. Other
538 * nodes receive roughly the same amount of available pages.
Rohit Seth53fee042007-02-13 13:26:22 +0100539 */
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200540 if (i == num_nodes + node_start - 1)
Rohit Seth53fee042007-02-13 13:26:22 +0100541 end = max_addr;
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200542 else
David Rientjesa7e96622007-07-21 17:11:29 +0200543 while (end - *addr - e820_hole_size(*addr, end) <
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200544 size) {
545 end += FAKE_NODE_MIN_SIZE;
546 if (end > max_addr) {
547 end = max_addr;
548 break;
549 }
550 }
David Rientjesadc19382009-09-25 15:20:09 -0700551 if (setup_node_range(i, addr, end - *addr, max_addr) < 0)
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200552 break;
553 }
554 return i - node_start + 1;
555}
556
557/*
David Rientjes382591d2007-05-02 19:27:09 +0200558 * Splits the remaining system RAM into chunks of size. The remaining memory is
559 * always assigned to a final node and can be asymmetric. Returns the number of
560 * nodes split.
561 */
David Rientjesadc19382009-09-25 15:20:09 -0700562static int __init split_nodes_by_size(u64 *addr, u64 max_addr, int node_start,
563 u64 size)
David Rientjes382591d2007-05-02 19:27:09 +0200564{
565 int i = node_start;
566 size = (size << 20) & FAKE_NODE_MIN_HASH_MASK;
David Rientjesadc19382009-09-25 15:20:09 -0700567 while (!setup_node_range(i++, addr, size, max_addr))
David Rientjes382591d2007-05-02 19:27:09 +0200568 ;
569 return i - node_start;
570}
571
572/*
Thomas Gleixner886533a2008-05-12 15:43:36 +0200573 * Sets up the system RAM area from start_pfn to last_pfn according to the
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200574 * numa=fake command-line option.
575 */
David Rientjesadc19382009-09-25 15:20:09 -0700576static int __init numa_emulation(unsigned long start_pfn,
577 unsigned long last_pfn, int acpi, int k8)
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200578{
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100579 u64 size, addr = start_pfn << PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200580 u64 max_addr = last_pfn << PAGE_SHIFT;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100581 int num_nodes = 0, num = 0, coeff_flag, coeff = -1, i;
David Rientjesadc19382009-09-25 15:20:09 -0700582 int num_phys_nodes;
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200583
David Rientjesadc19382009-09-25 15:20:09 -0700584 num_phys_nodes = setup_physnodes(addr, max_addr, acpi, k8);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200585 /*
586 * If the numa=fake command-line is just a single number N, split the
587 * system RAM into N fake nodes.
588 */
589 if (!strchr(cmdline, '*') && !strchr(cmdline, ',')) {
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100590 long n = simple_strtol(cmdline, NULL, 0);
591
David Rientjesadc19382009-09-25 15:20:09 -0700592 num_nodes = split_nodes_interleave(addr, max_addr,
593 num_phys_nodes, n);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200594 if (num_nodes < 0)
595 return num_nodes;
596 goto out;
597 }
598
599 /* Parse the command line. */
David Rientjes382591d2007-05-02 19:27:09 +0200600 for (coeff_flag = 0; ; cmdline++) {
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200601 if (*cmdline && isdigit(*cmdline)) {
602 num = num * 10 + *cmdline - '0';
603 continue;
604 }
David Rientjes382591d2007-05-02 19:27:09 +0200605 if (*cmdline == '*') {
606 if (num > 0)
607 coeff = num;
608 coeff_flag = 1;
609 }
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200610 if (!*cmdline || *cmdline == ',') {
David Rientjes382591d2007-05-02 19:27:09 +0200611 if (!coeff_flag)
612 coeff = 1;
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200613 /*
614 * Round down to the nearest FAKE_NODE_MIN_SIZE.
615 * Command-line coefficients are in megabytes.
616 */
617 size = ((u64)num << 20) & FAKE_NODE_MIN_HASH_MASK;
David Rientjes382591d2007-05-02 19:27:09 +0200618 if (size)
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200619 for (i = 0; i < coeff; i++, num_nodes++)
David Rientjesadc19382009-09-25 15:20:09 -0700620 if (setup_node_range(num_nodes, &addr,
621 size, max_addr) < 0)
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200622 goto done;
David Rientjes382591d2007-05-02 19:27:09 +0200623 if (!*cmdline)
624 break;
625 coeff_flag = 0;
626 coeff = -1;
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200627 }
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200628 num = 0;
629 }
630done:
631 if (!num_nodes)
632 return -1;
David Rientjes14694d72007-05-02 19:27:09 +0200633 /* Fill remainder of system RAM, if appropriate. */
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200634 if (addr < max_addr) {
David Rientjes382591d2007-05-02 19:27:09 +0200635 if (coeff_flag && coeff < 0) {
636 /* Split remaining nodes into num-sized chunks */
David Rientjesadc19382009-09-25 15:20:09 -0700637 num_nodes += split_nodes_by_size(&addr, max_addr,
David Rientjes382591d2007-05-02 19:27:09 +0200638 num_nodes, num);
639 goto out;
640 }
David Rientjes14694d72007-05-02 19:27:09 +0200641 switch (*(cmdline - 1)) {
642 case '*':
643 /* Split remaining nodes into coeff chunks */
644 if (coeff <= 0)
645 break;
David Rientjesadc19382009-09-25 15:20:09 -0700646 num_nodes += split_nodes_equally(&addr, max_addr,
David Rientjes14694d72007-05-02 19:27:09 +0200647 num_nodes, coeff);
648 break;
649 case ',':
650 /* Do not allocate remaining system RAM */
651 break;
652 default:
653 /* Give one final node */
David Rientjesadc19382009-09-25 15:20:09 -0700654 setup_node_range(num_nodes, &addr, max_addr - addr,
655 max_addr);
David Rientjes14694d72007-05-02 19:27:09 +0200656 num_nodes++;
657 }
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200658 }
659out:
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700660 memnode_shift = compute_hash_shift(nodes, num_nodes, NULL);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200661 if (memnode_shift < 0) {
662 memnode_shift = 0;
663 printk(KERN_ERR "No NUMA hash function found. NUMA emulation "
664 "disabled.\n");
665 return -1;
666 }
667
668 /*
David Rientjesadc19382009-09-25 15:20:09 -0700669 * We need to vacate all active ranges that may have been registered for
670 * the e820 memory map.
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200671 */
672 remove_all_active_ranges();
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200673 for_each_node_mask(i, node_possible_map) {
Mel Gorman5cb248a2006-09-27 01:49:52 -0700674 e820_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
675 nodes[i].end >> PAGE_SHIFT);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100676 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
Mel Gorman5cb248a2006-09-27 01:49:52 -0700677 }
David Rientjes3484d792007-07-21 17:10:32 +0200678 acpi_fake_nodes(nodes, num_nodes);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100679 numa_init_array();
680 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200682#endif /* CONFIG_NUMA_EMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
David Rientjes8ee2deb2009-09-25 15:20:00 -0700684void __init initmem_init(unsigned long start_pfn, unsigned long last_pfn,
685 int acpi, int k8)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100686{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 int i;
688
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200689 nodes_clear(node_possible_map);
Yinghai Lub7ad1492008-02-17 02:02:21 -0800690 nodes_clear(node_online_map);
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692#ifdef CONFIG_NUMA_EMU
David Rientjesadc19382009-09-25 15:20:09 -0700693 if (cmdline && !numa_emulation(start_pfn, last_pfn, acpi, k8))
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100694 return;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200695 nodes_clear(node_possible_map);
Yinghai Lub7ad1492008-02-17 02:02:21 -0800696 nodes_clear(node_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697#endif
698
699#ifdef CONFIG_ACPI_NUMA
David Rientjes87162732009-09-25 15:20:04 -0700700 if (!numa_off && acpi && !acpi_scan_nodes(start_pfn << PAGE_SHIFT,
701 last_pfn << PAGE_SHIFT))
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100702 return;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200703 nodes_clear(node_possible_map);
Yinghai Lub7ad1492008-02-17 02:02:21 -0800704 nodes_clear(node_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705#endif
706
707#ifdef CONFIG_K8_NUMA
David Rientjes8ee2deb2009-09-25 15:20:00 -0700708 if (!numa_off && k8 && !k8_scan_nodes())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200710 nodes_clear(node_possible_map);
Yinghai Lub7ad1492008-02-17 02:02:21 -0800711 nodes_clear(node_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712#endif
713 printk(KERN_INFO "%s\n",
714 numa_off ? "NUMA turned off" : "No NUMA configuration found");
715
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100716 printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 start_pfn << PAGE_SHIFT,
Thomas Gleixner886533a2008-05-12 15:43:36 +0200718 last_pfn << PAGE_SHIFT);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100719 /* setup dummy node covering all memory */
720 memnode_shift = 63;
Amul Shah076422d2007-02-13 13:26:19 +0100721 memnodemap = memnode.embedded_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 memnodemap[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 node_set_online(0);
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200724 node_set(0, node_possible_map);
Mike Travis168ef542008-12-16 17:34:01 -0800725 for (i = 0; i < nr_cpu_ids; i++)
Andi Kleen69d81fc2005-11-05 17:25:53 +0100726 numa_set_node(i, 0);
Thomas Gleixner886533a2008-05-12 15:43:36 +0200727 e820_register_active_regions(0, start_pfn, last_pfn);
728 setup_node_bootmem(0, start_pfn << PAGE_SHIFT, last_pfn << PAGE_SHIFT);
Andi Kleen69d81fc2005-11-05 17:25:53 +0100729}
730
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100731unsigned long __init numa_free_all_bootmem(void)
732{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 unsigned long pages = 0;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100734 int i;
735
736 for_each_online_node(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 pages += free_all_bootmem_node(NODE_DATA(i));
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return pages;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100740}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200742static __init int numa_setup(char *opt)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100743{
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200744 if (!opt)
745 return -EINVAL;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100746 if (!strncmp(opt, "off", 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 numa_off = 1;
748#ifdef CONFIG_NUMA_EMU
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200749 if (!strncmp(opt, "fake=", 5))
750 cmdline = opt + 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751#endif
752#ifdef CONFIG_ACPI_NUMA
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100753 if (!strncmp(opt, "noacpi", 6))
754 acpi_numa = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755#endif
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200756 return 0;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100757}
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200758early_param("numa", numa_setup);
759
Mike Travis23ca4bb2008-05-12 21:21:12 +0200760#ifdef CONFIG_NUMA
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100761/*
762 * Setup early cpu_to_node.
763 *
764 * Populate cpu_to_node[] only if x86_cpu_to_apicid[],
765 * and apicid_to_node[] tables have valid entries for a CPU.
766 * This means we skip cpu_to_node[] initialisation for NUMA
767 * emulation and faking node case (when running a kernel compiled
768 * for NUMA on a non NUMA box), which is OK as cpu_to_node[]
769 * is already initialized in a round robin manner at numa_init_array,
770 * prior to this call, and this initialization is good enough
771 * for the fake NUMA cases.
Mike Travis23ca4bb2008-05-12 21:21:12 +0200772 *
773 * Called before the per_cpu areas are setup.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100774 */
775void __init init_cpu_to_node(void)
776{
Mike Travis23ca4bb2008-05-12 21:21:12 +0200777 int cpu;
778 u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100779
Mike Travis23ca4bb2008-05-12 21:21:12 +0200780 BUG_ON(cpu_to_apicid == NULL);
781
782 for_each_possible_cpu(cpu) {
Yinghai Lu7c9e92b62008-02-19 15:35:54 -0800783 int node;
Mike Travis23ca4bb2008-05-12 21:21:12 +0200784 u16 apicid = cpu_to_apicid[cpu];
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100785
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100786 if (apicid == BAD_APICID)
787 continue;
Yinghai Lu7c9e92b62008-02-19 15:35:54 -0800788 node = apicid_to_node[apicid];
789 if (node == NUMA_NO_NODE)
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100790 continue;
Yinghai Lu7c9e92b62008-02-19 15:35:54 -0800791 if (!node_online(node))
792 continue;
Mike Travis23ca4bb2008-05-12 21:21:12 +0200793 numa_set_node(cpu, node);
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100794 }
795}
Mike Travis23ca4bb2008-05-12 21:21:12 +0200796#endif
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100797
Andi Kleencf050132006-01-11 22:46:27 +0100798
Brian Gerst6470aff2009-01-27 12:56:47 +0900799void __cpuinit numa_set_node(int cpu, int node)
800{
801 int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
802
803 /* early setting, no percpu area yet */
804 if (cpu_to_node_map) {
805 cpu_to_node_map[cpu] = node;
806 return;
807 }
808
809#ifdef CONFIG_DEBUG_PER_CPU_MAPS
Brian Gerst44581a22009-02-08 09:58:40 -0500810 if (cpu >= nr_cpu_ids || !cpu_possible(cpu)) {
Brian Gerst6470aff2009-01-27 12:56:47 +0900811 printk(KERN_ERR "numa_set_node: invalid cpu# (%d)\n", cpu);
812 dump_stack();
813 return;
814 }
815#endif
816 per_cpu(x86_cpu_to_node_map, cpu) = node;
817
818 if (node != NUMA_NO_NODE)
819 per_cpu(node_number, cpu) = node;
820}
821
822void __cpuinit numa_clear_node(int cpu)
823{
824 numa_set_node(cpu, NUMA_NO_NODE);
825}
826
827#ifndef CONFIG_DEBUG_PER_CPU_MAPS
828
829void __cpuinit numa_add_cpu(int cpu)
830{
Rusty Russellc032ef602009-03-13 14:49:53 +1030831 cpumask_set_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
Brian Gerst6470aff2009-01-27 12:56:47 +0900832}
833
834void __cpuinit numa_remove_cpu(int cpu)
835{
Rusty Russellc032ef602009-03-13 14:49:53 +1030836 cpumask_clear_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
Brian Gerst6470aff2009-01-27 12:56:47 +0900837}
838
839#else /* CONFIG_DEBUG_PER_CPU_MAPS */
840
841/*
842 * --------- debug versions of the numa functions ---------
843 */
844static void __cpuinit numa_set_cpumask(int cpu, int enable)
845{
846 int node = early_cpu_to_node(cpu);
Rusty Russell73e907d2009-03-13 14:49:57 +1030847 struct cpumask *mask;
Brian Gerst6470aff2009-01-27 12:56:47 +0900848 char buf[64];
849
Rusty Russellc032ef602009-03-13 14:49:53 +1030850 mask = node_to_cpumask_map[node];
851 if (mask == NULL) {
852 printk(KERN_ERR "node_to_cpumask_map[%i] NULL\n", node);
Brian Gerst6470aff2009-01-27 12:56:47 +0900853 dump_stack();
854 return;
855 }
856
Brian Gerst6470aff2009-01-27 12:56:47 +0900857 if (enable)
Rusty Russellc032ef602009-03-13 14:49:53 +1030858 cpumask_set_cpu(cpu, mask);
Brian Gerst6470aff2009-01-27 12:56:47 +0900859 else
Rusty Russellc032ef602009-03-13 14:49:53 +1030860 cpumask_clear_cpu(cpu, mask);
Brian Gerst6470aff2009-01-27 12:56:47 +0900861
862 cpulist_scnprintf(buf, sizeof(buf), mask);
863 printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n",
864 enable ? "numa_add_cpu" : "numa_remove_cpu", cpu, node, buf);
865}
866
867void __cpuinit numa_add_cpu(int cpu)
868{
869 numa_set_cpumask(cpu, 1);
870}
871
872void __cpuinit numa_remove_cpu(int cpu)
873{
874 numa_set_cpumask(cpu, 0);
875}
876
877int cpu_to_node(int cpu)
878{
879 if (early_per_cpu_ptr(x86_cpu_to_node_map)) {
880 printk(KERN_WARNING
881 "cpu_to_node(%d): usage too early!\n", cpu);
882 dump_stack();
883 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
884 }
885 return per_cpu(x86_cpu_to_node_map, cpu);
886}
887EXPORT_SYMBOL(cpu_to_node);
888
889/*
890 * Same function as cpu_to_node() but used if called before the
891 * per_cpu areas are setup.
892 */
893int early_cpu_to_node(int cpu)
894{
895 if (early_per_cpu_ptr(x86_cpu_to_node_map))
896 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
897
Brian Gerst44581a22009-02-08 09:58:40 -0500898 if (!cpu_possible(cpu)) {
Brian Gerst6470aff2009-01-27 12:56:47 +0900899 printk(KERN_WARNING
900 "early_cpu_to_node(%d): no per_cpu area!\n", cpu);
901 dump_stack();
902 return NUMA_NO_NODE;
903 }
904 return per_cpu(x86_cpu_to_node_map, cpu);
905}
906
Brian Gerst6470aff2009-01-27 12:56:47 +0900907/*
Brian Gerst6470aff2009-01-27 12:56:47 +0900908 * --------- end of debug versions of the numa functions ---------
909 */
910
911#endif /* CONFIG_DEBUG_PER_CPU_MAPS */