blob: 08d140fbc31b760b419c721e6c174b6d679e4f79 [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
Brian Gerst6470aff2009-01-27 12:56:47 +090023#ifdef CONFIG_DEBUG_PER_CPU_MAPS
24# define DBG(x...) printk(KERN_DEBUG x)
25#else
26# define DBG(x...)
27#endif
28
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -070029struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010030EXPORT_SYMBOL(node_data);
31
Eric Dumazetdcf36bf2006-03-25 16:31:46 +010032struct memnode memnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
travis@sgi.com43238382008-01-30 13:33:25 +010034s16 apicid_to_node[MAX_LOCAL_APIC] __cpuinitdata = {
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010035 [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
Andi Kleen3f098c22005-09-12 18:49:24 +020036};
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038int numa_off __initdata;
Thomas Gleixner864fc312008-05-12 15:43:36 +020039static unsigned long __initdata nodemap_addr;
40static unsigned long __initdata nodemap_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Brian Gerst6470aff2009-01-27 12:56:47 +090042DEFINE_PER_CPU(int, node_number) = 0;
43EXPORT_PER_CPU_SYMBOL(node_number);
44
45/*
46 * Map cpu index to node index
47 */
48DEFINE_EARLY_PER_CPU(int, x86_cpu_to_node_map, NUMA_NO_NODE);
49EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_node_map);
50
51/*
52 * Which logical CPUs are on which nodes
53 */
54cpumask_t *node_to_cpumask_map;
55EXPORT_SYMBOL(node_to_cpumask_map);
56
Eric Dumazet529a3402005-11-05 17:25:54 +010057/*
58 * Given a shift value, try to populate memnodemap[]
59 * Returns :
60 * 1 if OK
61 * 0 if memnodmap[] too small (of shift too small)
62 * -1 if node overlap or lost ram (shift too big)
63 */
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010064static int __init populate_memnodemap(const struct bootnode *nodes,
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -070065 int numnodes, int shift, int *nodeids)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Eric Dumazet529a3402005-11-05 17:25:54 +010067 unsigned long addr, end;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010068 int i, res = -1;
Keith Manntheyb6846642005-07-28 21:15:38 -070069
travis@sgi.com43238382008-01-30 13:33:25 +010070 memset(memnodemap, 0xff, sizeof(s16)*memnodemapsize);
Eric Dumazet529a3402005-11-05 17:25:54 +010071 for (i = 0; i < numnodes; i++) {
72 addr = nodes[i].start;
73 end = nodes[i].end;
74 if (addr >= end)
75 continue;
Amul Shah076422d22007-02-13 13:26:19 +010076 if ((end >> shift) >= memnodemapsize)
Eric Dumazet529a3402005-11-05 17:25:54 +010077 return 0;
78 do {
travis@sgi.com43238382008-01-30 13:33:25 +010079 if (memnodemap[addr >> shift] != NUMA_NO_NODE)
Eric Dumazet529a3402005-11-05 17:25:54 +010080 return -1;
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -070081
82 if (!nodeids)
83 memnodemap[addr >> shift] = i;
84 else
85 memnodemap[addr >> shift] = nodeids[i];
86
Amul Shah076422d22007-02-13 13:26:19 +010087 addr += (1UL << shift);
Eric Dumazet529a3402005-11-05 17:25:54 +010088 } while (addr < end);
89 res = 1;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010090 }
Eric Dumazet529a3402005-11-05 17:25:54 +010091 return res;
92}
93
Amul Shah076422d22007-02-13 13:26:19 +010094static int __init allocate_cachealigned_memnodemap(void)
95{
Yinghai Lu24a5da72008-02-01 17:49:41 +010096 unsigned long addr;
Amul Shah076422d22007-02-13 13:26:19 +010097
98 memnodemap = memnode.embedded_map;
travis@sgi.com316390b2008-01-30 13:33:15 +010099 if (memnodemapsize <= ARRAY_SIZE(memnode.embedded_map))
Amul Shah076422d22007-02-13 13:26:19 +0100100 return 0;
Amul Shah076422d22007-02-13 13:26:19 +0100101
Yinghai Lu24a5da72008-02-01 17:49:41 +0100102 addr = 0x8000;
Joerg Roedelbe3e89e2008-07-25 16:48:58 +0200103 nodemap_size = roundup(sizeof(s16) * memnodemapsize, L1_CACHE_BYTES);
Yinghai Luc987d122008-06-24 22:14:09 -0700104 nodemap_addr = find_e820_area(addr, max_pfn<<PAGE_SHIFT,
Yinghai Lu24a5da72008-02-01 17:49:41 +0100105 nodemap_size, L1_CACHE_BYTES);
Amul Shah076422d22007-02-13 13:26:19 +0100106 if (nodemap_addr == -1UL) {
107 printk(KERN_ERR
108 "NUMA: Unable to allocate Memory to Node hash map\n");
109 nodemap_addr = nodemap_size = 0;
110 return -1;
111 }
Yinghai Lu24a5da72008-02-01 17:49:41 +0100112 memnodemap = phys_to_virt(nodemap_addr);
Yinghai Lu25eff8d2008-02-01 17:49:41 +0100113 reserve_early(nodemap_addr, nodemap_addr + nodemap_size, "MEMNODEMAP");
Amul Shah076422d22007-02-13 13:26:19 +0100114
115 printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n",
116 nodemap_addr, nodemap_addr + nodemap_size);
117 return 0;
118}
119
120/*
121 * The LSB of all start and end addresses in the node map is the value of the
122 * maximum possible shift.
123 */
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100124static int __init extract_lsb_from_nodes(const struct bootnode *nodes,
125 int numnodes)
Amul Shah076422d22007-02-13 13:26:19 +0100126{
Amul Shah54413922007-02-13 13:26:20 +0100127 int i, nodes_used = 0;
Amul Shah076422d22007-02-13 13:26:19 +0100128 unsigned long start, end;
129 unsigned long bitfield = 0, memtop = 0;
130
131 for (i = 0; i < numnodes; i++) {
132 start = nodes[i].start;
133 end = nodes[i].end;
134 if (start >= end)
135 continue;
Amul Shah54413922007-02-13 13:26:20 +0100136 bitfield |= start;
137 nodes_used++;
Amul Shah076422d22007-02-13 13:26:19 +0100138 if (end > memtop)
139 memtop = end;
140 }
Amul Shah54413922007-02-13 13:26:20 +0100141 if (nodes_used <= 1)
142 i = 63;
143 else
144 i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
Amul Shah076422d22007-02-13 13:26:19 +0100145 memnodemapsize = (memtop >> i)+1;
146 return i;
147}
148
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700149int __init compute_hash_shift(struct bootnode *nodes, int numnodes,
150 int *nodeids)
Eric Dumazet529a3402005-11-05 17:25:54 +0100151{
Amul Shah076422d22007-02-13 13:26:19 +0100152 int shift;
Eric Dumazet529a3402005-11-05 17:25:54 +0100153
Amul Shah076422d22007-02-13 13:26:19 +0100154 shift = extract_lsb_from_nodes(nodes, numnodes);
155 if (allocate_cachealigned_memnodemap())
156 return -1;
Andi Kleen6b050f82006-01-11 22:44:33 +0100157 printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
Eric Dumazet529a3402005-11-05 17:25:54 +0100158 shift);
159
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700160 if (populate_memnodemap(nodes, numnodes, shift, nodeids) != 1) {
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100161 printk(KERN_INFO "Your memory is not aligned you need to "
162 "rebuild your kernel with a bigger NODEMAPSIZE "
163 "shift=%d\n", shift);
Eric Dumazet529a3402005-11-05 17:25:54 +0100164 return -1;
165 }
Keith Manntheyb6846642005-07-28 21:15:38 -0700166 return shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700169int early_pfn_to_nid(unsigned long pfn)
170{
171 return phys_to_nid(pfn << PAGE_SHIFT);
172}
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700173
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100174static void * __init early_node_mem(int nodeid, unsigned long start,
Yinghai Lu24a5da72008-02-01 17:49:41 +0100175 unsigned long end, unsigned long size,
176 unsigned long align)
Andi Kleena8062232006-04-07 19:49:21 +0200177{
Yinghai Lu24a5da72008-02-01 17:49:41 +0100178 unsigned long mem = find_e820_area(start, end, size, align);
Andi Kleena8062232006-04-07 19:49:21 +0200179 void *ptr;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100180
Yinghai Lu9347e0b2008-02-01 17:49:42 +0100181 if (mem != -1L)
Andi Kleena8062232006-04-07 19:49:21 +0200182 return __va(mem);
Yinghai Lu9347e0b2008-02-01 17:49:42 +0100183
Yinghai Lu24a5da72008-02-01 17:49:41 +0100184 ptr = __alloc_bootmem_nopanic(size, align, __pa(MAX_DMA_ADDRESS));
Yoann Padioleau83e83d52007-10-17 18:04:35 +0200185 if (ptr == NULL) {
Andi Kleena8062232006-04-07 19:49:21 +0200186 printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100187 size, nodeid);
Andi Kleena8062232006-04-07 19:49:21 +0200188 return NULL;
189 }
190 return ptr;
191}
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/* Initialize bootmem allocator for a node */
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100194void __init setup_node_bootmem(int nodeid, unsigned long start,
195 unsigned long end)
196{
Thomas Gleixner886533a2008-05-12 15:43:36 +0200197 unsigned long start_pfn, last_pfn, bootmap_pages, bootmap_size;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100198 unsigned long bootmap_start, nodedata_phys;
Andi Kleena8062232006-04-07 19:49:21 +0200199 void *bootmap;
Joerg Roedelbe3e89e2008-07-25 16:48:58 +0200200 const int pgdat_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
Yinghai Lu1a27fc02008-03-18 12:52:37 -0700201 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Joerg Roedelbe3e89e2008-07-25 16:48:58 +0200203 start = roundup(start, ZONE_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100205 printk(KERN_INFO "Bootmem setup node %d %016lx-%016lx\n", nodeid,
206 start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 start_pfn = start >> PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200209 last_pfn = end >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Yinghai Lu24a5da72008-02-01 17:49:41 +0100211 node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
212 SMP_CACHE_BYTES);
Andi Kleena8062232006-04-07 19:49:21 +0200213 if (node_data[nodeid] == NULL)
214 return;
215 nodedata_phys = __pa(node_data[nodeid]);
Yinghai Lu6118f762008-02-04 16:47:56 +0100216 printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
217 nodedata_phys + pgdat_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
Johannes Weinerb61bfa32008-07-23 21:26:55 -0700220 NODE_DATA(nodeid)->bdata = &bootmem_node_data[nodeid];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 NODE_DATA(nodeid)->node_start_pfn = start_pfn;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200222 NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Yinghai Lu1a27fc02008-03-18 12:52:37 -0700224 /*
225 * Find a place for the bootmem map
226 * nodedata_phys could be on other nodes by alloc_bootmem,
227 * so need to sure bootmap_start not to be small, otherwise
228 * early_node_mem will get that with find_e820_area instead
229 * of alloc_bootmem, that could clash with reserved range
230 */
Thomas Gleixner886533a2008-05-12 15:43:36 +0200231 bootmap_pages = bootmem_bootmap_pages(last_pfn - start_pfn);
Yinghai Lu1a27fc02008-03-18 12:52:37 -0700232 nid = phys_to_nid(nodedata_phys);
233 if (nid == nodeid)
Joerg Roedelbe3e89e2008-07-25 16:48:58 +0200234 bootmap_start = roundup(nodedata_phys + pgdat_size, PAGE_SIZE);
Yinghai Lu1a27fc02008-03-18 12:52:37 -0700235 else
Joerg Roedelbe3e89e2008-07-25 16:48:58 +0200236 bootmap_start = roundup(start, PAGE_SIZE);
Yinghai Lu24a5da72008-02-01 17:49:41 +0100237 /*
Paul Jacksone9197bf2008-05-14 08:15:10 -0700238 * SMP_CACHE_BYTES could be enough, but init_bootmem_node like
Yinghai Lu24a5da72008-02-01 17:49:41 +0100239 * to use that to align to PAGE_SIZE
240 */
Andi Kleena8062232006-04-07 19:49:21 +0200241 bootmap = early_node_mem(nodeid, bootmap_start, end,
Yinghai Lu24a5da72008-02-01 17:49:41 +0100242 bootmap_pages<<PAGE_SHIFT, PAGE_SIZE);
Andi Kleena8062232006-04-07 19:49:21 +0200243 if (bootmap == NULL) {
244 if (nodedata_phys < start || nodedata_phys >= end)
Yinghai Lu37bff622008-03-18 12:40:04 -0700245 free_bootmem(nodedata_phys, pgdat_size);
Andi Kleena8062232006-04-07 19:49:21 +0200246 node_data[nodeid] = NULL;
247 return;
248 }
249 bootmap_start = __pa(bootmap);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 bootmap_size = init_bootmem_node(NODE_DATA(nodeid),
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100252 bootmap_start >> PAGE_SHIFT,
Thomas Gleixner886533a2008-05-12 15:43:36 +0200253 start_pfn, last_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Yinghai Lu6118f762008-02-04 16:47:56 +0100255 printk(KERN_INFO " bootmap [%016lx - %016lx] pages %lx\n",
256 bootmap_start, bootmap_start + bootmap_size - 1,
257 bootmap_pages);
258
Mel Gorman5cb248a2006-09-27 01:49:52 -0700259 free_bootmem_with_active_regions(nodeid, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Yinghai Lu1a27fc02008-03-18 12:52:37 -0700261 /*
262 * convert early reserve to bootmem reserve earlier
263 * otherwise early_node_mem could use early reserved mem
264 * on previous node
265 */
266 early_res_to_bootmem(start, end);
267
268 /*
269 * in some case early_node_mem could use alloc_bootmem
270 * to get range on other node, don't reserve that again
271 */
272 if (nid != nodeid)
273 printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nodeid, nid);
274 else
275 reserve_bootmem_node(NODE_DATA(nodeid), nodedata_phys,
276 pgdat_size, BOOTMEM_DEFAULT);
277 nid = phys_to_nid(bootmap_start);
278 if (nid != nodeid)
279 printk(KERN_INFO " bootmap(%d) on node %d\n", nodeid, nid);
280 else
281 reserve_bootmem_node(NODE_DATA(nodeid), bootmap_start,
282 bootmap_pages<<PAGE_SHIFT, BOOTMEM_DEFAULT);
283
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200284#ifdef CONFIG_ACPI_NUMA
285 srat_reserve_add_area(nodeid);
286#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 node_set_online(nodeid);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100288}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100290/*
291 * There are unfortunately some poorly designed mainboards around that
292 * only connect memory to a single CPU. This breaks the 1:1 cpu->node
293 * mapping. To avoid this fill in the mapping for all possible CPUs,
294 * as the number of CPUs is not known yet. We round robin the existing
295 * nodes.
296 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297void __init numa_init_array(void)
298{
299 int rr, i;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100300
Ravikiran G Thirumalai85cc5132005-09-30 11:59:22 -0700301 rr = first_node(node_online_map);
Mike Travis168ef542008-12-16 17:34:01 -0800302 for (i = 0; i < nr_cpu_ids; i++) {
travis@sgi.com1ce35712008-01-30 13:33:33 +0100303 if (early_cpu_to_node(i) != NUMA_NO_NODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 continue;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100305 numa_set_node(i, rr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 rr = next_node(rr, node_online_map);
307 if (rr == MAX_NUMNODES)
308 rr = first_node(node_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
312#ifdef CONFIG_NUMA_EMU
Rohit Seth53fee042007-02-13 13:26:22 +0100313/* Numa emulation */
Thomas Gleixner864fc312008-05-12 15:43:36 +0200314static char *cmdline __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Rohit Seth53fee042007-02-13 13:26:22 +0100316/*
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100317 * Setups up nid to range from addr to addr + size. If the end
318 * boundary is greater than max_addr, then max_addr is used instead.
319 * The return value is 0 if there is additional memory left for
320 * allocation past addr and -1 otherwise. addr is adjusted to be at
321 * the end of the node.
Rohit Seth53fee042007-02-13 13:26:22 +0100322 */
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200323static int __init setup_node_range(int nid, struct bootnode *nodes, u64 *addr,
324 u64 size, u64 max_addr)
Rohit Seth53fee042007-02-13 13:26:22 +0100325{
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200326 int ret = 0;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100327
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200328 nodes[nid].start = *addr;
329 *addr += size;
330 if (*addr >= max_addr) {
331 *addr = max_addr;
332 ret = -1;
333 }
334 nodes[nid].end = *addr;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200335 node_set(nid, node_possible_map);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200336 printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
337 nodes[nid].start, nodes[nid].end,
338 (nodes[nid].end - nodes[nid].start) >> 20);
339 return ret;
Rohit Seth53fee042007-02-13 13:26:22 +0100340}
341
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200342/*
343 * Splits num_nodes nodes up equally starting at node_start. The return value
344 * is the number of nodes split up and addr is adjusted to be at the end of the
345 * last node allocated.
346 */
347static int __init split_nodes_equally(struct bootnode *nodes, u64 *addr,
348 u64 max_addr, int node_start,
349 int num_nodes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200351 unsigned int big;
352 u64 size;
353 int i;
Rohit Seth53fee042007-02-13 13:26:22 +0100354
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200355 if (num_nodes <= 0)
356 return -1;
357 if (num_nodes > MAX_NUMNODES)
358 num_nodes = MAX_NUMNODES;
David Rientjesa7e96622007-07-21 17:11:29 +0200359 size = (max_addr - *addr - e820_hole_size(*addr, max_addr)) /
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200360 num_nodes;
Rohit Seth53fee042007-02-13 13:26:22 +0100361 /*
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200362 * Calculate the number of big nodes that can be allocated as a result
363 * of consolidating the leftovers.
Rohit Seth53fee042007-02-13 13:26:22 +0100364 */
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200365 big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * num_nodes) /
366 FAKE_NODE_MIN_SIZE;
Rohit Seth53fee042007-02-13 13:26:22 +0100367
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200368 /* Round down to nearest FAKE_NODE_MIN_SIZE. */
369 size &= FAKE_NODE_MIN_HASH_MASK;
370 if (!size) {
371 printk(KERN_ERR "Not enough memory for each node. "
372 "NUMA emulation disabled.\n");
373 return -1;
Rohit Seth53fee042007-02-13 13:26:22 +0100374 }
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200375
376 for (i = node_start; i < num_nodes + node_start; i++) {
377 u64 end = *addr + size;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100378
Rohit Seth53fee042007-02-13 13:26:22 +0100379 if (i < big)
380 end += FAKE_NODE_MIN_SIZE;
381 /*
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200382 * The final node can have the remaining system RAM. Other
383 * nodes receive roughly the same amount of available pages.
Rohit Seth53fee042007-02-13 13:26:22 +0100384 */
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200385 if (i == num_nodes + node_start - 1)
Rohit Seth53fee042007-02-13 13:26:22 +0100386 end = max_addr;
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200387 else
David Rientjesa7e96622007-07-21 17:11:29 +0200388 while (end - *addr - e820_hole_size(*addr, end) <
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200389 size) {
390 end += FAKE_NODE_MIN_SIZE;
391 if (end > max_addr) {
392 end = max_addr;
393 break;
394 }
395 }
396 if (setup_node_range(i, nodes, addr, end - *addr, max_addr) < 0)
397 break;
398 }
399 return i - node_start + 1;
400}
401
402/*
David Rientjes382591d2007-05-02 19:27:09 +0200403 * Splits the remaining system RAM into chunks of size. The remaining memory is
404 * always assigned to a final node and can be asymmetric. Returns the number of
405 * nodes split.
406 */
407static int __init split_nodes_by_size(struct bootnode *nodes, u64 *addr,
408 u64 max_addr, int node_start, u64 size)
409{
410 int i = node_start;
411 size = (size << 20) & FAKE_NODE_MIN_HASH_MASK;
412 while (!setup_node_range(i++, nodes, addr, size, max_addr))
413 ;
414 return i - node_start;
415}
416
417/*
Thomas Gleixner886533a2008-05-12 15:43:36 +0200418 * Sets up the system RAM area from start_pfn to last_pfn according to the
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200419 * numa=fake command-line option.
420 */
Mike Travisf46bdf22008-04-04 18:11:09 -0700421static struct bootnode nodes[MAX_NUMNODES] __initdata;
422
Thomas Gleixner886533a2008-05-12 15:43:36 +0200423static int __init numa_emulation(unsigned long start_pfn, unsigned long last_pfn)
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200424{
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100425 u64 size, addr = start_pfn << PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200426 u64 max_addr = last_pfn << PAGE_SHIFT;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100427 int num_nodes = 0, num = 0, coeff_flag, coeff = -1, i;
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200428
429 memset(&nodes, 0, sizeof(nodes));
430 /*
431 * If the numa=fake command-line is just a single number N, split the
432 * system RAM into N fake nodes.
433 */
434 if (!strchr(cmdline, '*') && !strchr(cmdline, ',')) {
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100435 long n = simple_strtol(cmdline, NULL, 0);
436
437 num_nodes = split_nodes_equally(nodes, &addr, max_addr, 0, n);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200438 if (num_nodes < 0)
439 return num_nodes;
440 goto out;
441 }
442
443 /* Parse the command line. */
David Rientjes382591d2007-05-02 19:27:09 +0200444 for (coeff_flag = 0; ; cmdline++) {
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200445 if (*cmdline && isdigit(*cmdline)) {
446 num = num * 10 + *cmdline - '0';
447 continue;
448 }
David Rientjes382591d2007-05-02 19:27:09 +0200449 if (*cmdline == '*') {
450 if (num > 0)
451 coeff = num;
452 coeff_flag = 1;
453 }
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200454 if (!*cmdline || *cmdline == ',') {
David Rientjes382591d2007-05-02 19:27:09 +0200455 if (!coeff_flag)
456 coeff = 1;
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200457 /*
458 * Round down to the nearest FAKE_NODE_MIN_SIZE.
459 * Command-line coefficients are in megabytes.
460 */
461 size = ((u64)num << 20) & FAKE_NODE_MIN_HASH_MASK;
David Rientjes382591d2007-05-02 19:27:09 +0200462 if (size)
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200463 for (i = 0; i < coeff; i++, num_nodes++)
464 if (setup_node_range(num_nodes, nodes,
465 &addr, size, max_addr) < 0)
466 goto done;
David Rientjes382591d2007-05-02 19:27:09 +0200467 if (!*cmdline)
468 break;
469 coeff_flag = 0;
470 coeff = -1;
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200471 }
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200472 num = 0;
473 }
474done:
475 if (!num_nodes)
476 return -1;
David Rientjes14694d72007-05-02 19:27:09 +0200477 /* Fill remainder of system RAM, if appropriate. */
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200478 if (addr < max_addr) {
David Rientjes382591d2007-05-02 19:27:09 +0200479 if (coeff_flag && coeff < 0) {
480 /* Split remaining nodes into num-sized chunks */
481 num_nodes += split_nodes_by_size(nodes, &addr, max_addr,
482 num_nodes, num);
483 goto out;
484 }
David Rientjes14694d72007-05-02 19:27:09 +0200485 switch (*(cmdline - 1)) {
486 case '*':
487 /* Split remaining nodes into coeff chunks */
488 if (coeff <= 0)
489 break;
490 num_nodes += split_nodes_equally(nodes, &addr, max_addr,
491 num_nodes, coeff);
492 break;
493 case ',':
494 /* Do not allocate remaining system RAM */
495 break;
496 default:
497 /* Give one final node */
498 setup_node_range(num_nodes, nodes, &addr,
499 max_addr - addr, max_addr);
500 num_nodes++;
501 }
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200502 }
503out:
Suresh Siddha6ec6e0d2008-03-25 10:14:35 -0700504 memnode_shift = compute_hash_shift(nodes, num_nodes, NULL);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200505 if (memnode_shift < 0) {
506 memnode_shift = 0;
507 printk(KERN_ERR "No NUMA hash function found. NUMA emulation "
508 "disabled.\n");
509 return -1;
510 }
511
512 /*
513 * We need to vacate all active ranges that may have been registered by
David Rientjes1c05f092007-07-21 17:11:30 +0200514 * SRAT and set acpi_numa to -1 so that srat_disabled() always returns
515 * true. NUMA emulation has succeeded so we will not scan ACPI nodes.
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200516 */
517 remove_all_active_ranges();
David Rientjes1c05f092007-07-21 17:11:30 +0200518#ifdef CONFIG_ACPI_NUMA
519 acpi_numa = -1;
520#endif
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200521 for_each_node_mask(i, node_possible_map) {
Mel Gorman5cb248a2006-09-27 01:49:52 -0700522 e820_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
523 nodes[i].end >> PAGE_SHIFT);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100524 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
Mel Gorman5cb248a2006-09-27 01:49:52 -0700525 }
David Rientjes3484d792007-07-21 17:10:32 +0200526 acpi_fake_nodes(nodes, num_nodes);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100527 numa_init_array();
528 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200530#endif /* CONFIG_NUMA_EMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Yinghai Lu1f75d7e2008-06-22 02:44:49 -0700532void __init initmem_init(unsigned long start_pfn, unsigned long last_pfn)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100533{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 int i;
535
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200536 nodes_clear(node_possible_map);
Yinghai Lub7ad1492008-02-17 02:02:21 -0800537 nodes_clear(node_online_map);
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539#ifdef CONFIG_NUMA_EMU
Thomas Gleixner886533a2008-05-12 15:43:36 +0200540 if (cmdline && !numa_emulation(start_pfn, last_pfn))
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100541 return;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200542 nodes_clear(node_possible_map);
Yinghai Lub7ad1492008-02-17 02:02:21 -0800543 nodes_clear(node_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544#endif
545
546#ifdef CONFIG_ACPI_NUMA
547 if (!numa_off && !acpi_scan_nodes(start_pfn << PAGE_SHIFT,
Thomas Gleixner886533a2008-05-12 15:43:36 +0200548 last_pfn << PAGE_SHIFT))
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100549 return;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200550 nodes_clear(node_possible_map);
Yinghai Lub7ad1492008-02-17 02:02:21 -0800551 nodes_clear(node_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552#endif
553
554#ifdef CONFIG_K8_NUMA
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100555 if (!numa_off && !k8_scan_nodes(start_pfn<<PAGE_SHIFT,
Thomas Gleixner886533a2008-05-12 15:43:36 +0200556 last_pfn<<PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200558 nodes_clear(node_possible_map);
Yinghai Lub7ad1492008-02-17 02:02:21 -0800559 nodes_clear(node_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560#endif
561 printk(KERN_INFO "%s\n",
562 numa_off ? "NUMA turned off" : "No NUMA configuration found");
563
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100564 printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 start_pfn << PAGE_SHIFT,
Thomas Gleixner886533a2008-05-12 15:43:36 +0200566 last_pfn << PAGE_SHIFT);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100567 /* setup dummy node covering all memory */
568 memnode_shift = 63;
Amul Shah076422d22007-02-13 13:26:19 +0100569 memnodemap = memnode.embedded_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 memnodemap[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 node_set_online(0);
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200572 node_set(0, node_possible_map);
Mike Travis168ef542008-12-16 17:34:01 -0800573 for (i = 0; i < nr_cpu_ids; i++)
Andi Kleen69d81fc2005-11-05 17:25:53 +0100574 numa_set_node(i, 0);
Thomas Gleixner886533a2008-05-12 15:43:36 +0200575 e820_register_active_regions(0, start_pfn, last_pfn);
576 setup_node_bootmem(0, start_pfn << PAGE_SHIFT, last_pfn << PAGE_SHIFT);
Andi Kleen69d81fc2005-11-05 17:25:53 +0100577}
578
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100579unsigned long __init numa_free_all_bootmem(void)
580{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 unsigned long pages = 0;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100582 int i;
583
584 for_each_online_node(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 pages += free_all_bootmem_node(NODE_DATA(i));
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return pages;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100588}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590void __init paging_init(void)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100591{
Mel Gorman6391af12006-10-11 01:20:39 -0700592 unsigned long max_zone_pfns[MAX_NR_ZONES];
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100593
Mel Gorman6391af12006-10-11 01:20:39 -0700594 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
595 max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
596 max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
Yinghai Luc987d122008-06-24 22:14:09 -0700597 max_zone_pfns[ZONE_NORMAL] = max_pfn;
Bob Piccod3ee8712005-11-05 17:25:54 +0100598
Bob Piccof0a5a582007-02-13 13:26:25 +0100599 sparse_memory_present_with_active_regions(MAX_NUMNODES);
600 sparse_init();
Bob Piccod3ee8712005-11-05 17:25:54 +0100601
Mel Gorman5cb248a2006-09-27 01:49:52 -0700602 free_area_init_nodes(max_zone_pfns);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100603}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200605static __init int numa_setup(char *opt)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100606{
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200607 if (!opt)
608 return -EINVAL;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100609 if (!strncmp(opt, "off", 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 numa_off = 1;
611#ifdef CONFIG_NUMA_EMU
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200612 if (!strncmp(opt, "fake=", 5))
613 cmdline = opt + 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614#endif
615#ifdef CONFIG_ACPI_NUMA
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100616 if (!strncmp(opt, "noacpi", 6))
617 acpi_numa = -1;
618 if (!strncmp(opt, "hotadd=", 7))
Andi Kleen68a3a7f2006-04-07 19:49:18 +0200619 hotadd_percent = simple_strtoul(opt+7, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620#endif
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200621 return 0;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100622}
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200623early_param("numa", numa_setup);
624
Mike Travis23ca4bb2008-05-12 21:21:12 +0200625#ifdef CONFIG_NUMA
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100626/*
627 * Setup early cpu_to_node.
628 *
629 * Populate cpu_to_node[] only if x86_cpu_to_apicid[],
630 * and apicid_to_node[] tables have valid entries for a CPU.
631 * This means we skip cpu_to_node[] initialisation for NUMA
632 * emulation and faking node case (when running a kernel compiled
633 * for NUMA on a non NUMA box), which is OK as cpu_to_node[]
634 * is already initialized in a round robin manner at numa_init_array,
635 * prior to this call, and this initialization is good enough
636 * for the fake NUMA cases.
Mike Travis23ca4bb2008-05-12 21:21:12 +0200637 *
638 * Called before the per_cpu areas are setup.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100639 */
640void __init init_cpu_to_node(void)
641{
Mike Travis23ca4bb2008-05-12 21:21:12 +0200642 int cpu;
643 u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100644
Mike Travis23ca4bb2008-05-12 21:21:12 +0200645 BUG_ON(cpu_to_apicid == NULL);
646
647 for_each_possible_cpu(cpu) {
Yinghai Lu7c9e92b2008-02-19 15:35:54 -0800648 int node;
Mike Travis23ca4bb2008-05-12 21:21:12 +0200649 u16 apicid = cpu_to_apicid[cpu];
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100650
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100651 if (apicid == BAD_APICID)
652 continue;
Yinghai Lu7c9e92b2008-02-19 15:35:54 -0800653 node = apicid_to_node[apicid];
654 if (node == NUMA_NO_NODE)
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100655 continue;
Yinghai Lu7c9e92b2008-02-19 15:35:54 -0800656 if (!node_online(node))
657 continue;
Mike Travis23ca4bb2008-05-12 21:21:12 +0200658 numa_set_node(cpu, node);
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100659 }
660}
Mike Travis23ca4bb2008-05-12 21:21:12 +0200661#endif
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100662
Andi Kleencf050132006-01-11 22:46:27 +0100663
Brian Gerst6470aff2009-01-27 12:56:47 +0900664/*
665 * Allocate node_to_cpumask_map based on number of available nodes
666 * Requires node_possible_map to be valid.
667 *
668 * Note: node_to_cpumask() is not valid until after this is done.
669 * (Use CONFIG_DEBUG_PER_CPU_MAPS to check this.)
670 */
671void __init setup_node_to_cpumask_map(void)
672{
673 unsigned int node, num = 0;
674 cpumask_t *map;
675
676 /* setup nr_node_ids if not done yet */
677 if (nr_node_ids == MAX_NUMNODES) {
678 for_each_node_mask(node, node_possible_map)
679 num = node;
680 nr_node_ids = num + 1;
681 }
682
683 /* allocate the map */
684 map = alloc_bootmem_low(nr_node_ids * sizeof(cpumask_t));
685 DBG("node_to_cpumask_map at %p for %d nodes\n", map, nr_node_ids);
686
687 pr_debug("Node to cpumask map at %p for %d nodes\n",
688 map, nr_node_ids);
689
690 /* node_to_cpumask() will now work */
691 node_to_cpumask_map = map;
692}
693
694void __cpuinit numa_set_node(int cpu, int node)
695{
696 int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
697
698 /* early setting, no percpu area yet */
699 if (cpu_to_node_map) {
700 cpu_to_node_map[cpu] = node;
701 return;
702 }
703
704#ifdef CONFIG_DEBUG_PER_CPU_MAPS
705 if (cpu >= nr_cpu_ids || !per_cpu_offset(cpu)) {
706 printk(KERN_ERR "numa_set_node: invalid cpu# (%d)\n", cpu);
707 dump_stack();
708 return;
709 }
710#endif
711 per_cpu(x86_cpu_to_node_map, cpu) = node;
712
713 if (node != NUMA_NO_NODE)
714 per_cpu(node_number, cpu) = node;
715}
716
717void __cpuinit numa_clear_node(int cpu)
718{
719 numa_set_node(cpu, NUMA_NO_NODE);
720}
721
722#ifndef CONFIG_DEBUG_PER_CPU_MAPS
723
724void __cpuinit numa_add_cpu(int cpu)
725{
726 cpu_set(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
727}
728
729void __cpuinit numa_remove_cpu(int cpu)
730{
731 cpu_clear(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
732}
733
734#else /* CONFIG_DEBUG_PER_CPU_MAPS */
735
736/*
737 * --------- debug versions of the numa functions ---------
738 */
739static void __cpuinit numa_set_cpumask(int cpu, int enable)
740{
741 int node = early_cpu_to_node(cpu);
742 cpumask_t *mask;
743 char buf[64];
744
745 if (node_to_cpumask_map == NULL) {
746 printk(KERN_ERR "node_to_cpumask_map NULL\n");
747 dump_stack();
748 return;
749 }
750
751 mask = &node_to_cpumask_map[node];
752 if (enable)
753 cpu_set(cpu, *mask);
754 else
755 cpu_clear(cpu, *mask);
756
757 cpulist_scnprintf(buf, sizeof(buf), mask);
758 printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n",
759 enable ? "numa_add_cpu" : "numa_remove_cpu", cpu, node, buf);
760}
761
762void __cpuinit numa_add_cpu(int cpu)
763{
764 numa_set_cpumask(cpu, 1);
765}
766
767void __cpuinit numa_remove_cpu(int cpu)
768{
769 numa_set_cpumask(cpu, 0);
770}
771
772int cpu_to_node(int cpu)
773{
774 if (early_per_cpu_ptr(x86_cpu_to_node_map)) {
775 printk(KERN_WARNING
776 "cpu_to_node(%d): usage too early!\n", cpu);
777 dump_stack();
778 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
779 }
780 return per_cpu(x86_cpu_to_node_map, cpu);
781}
782EXPORT_SYMBOL(cpu_to_node);
783
784/*
785 * Same function as cpu_to_node() but used if called before the
786 * per_cpu areas are setup.
787 */
788int early_cpu_to_node(int cpu)
789{
790 if (early_per_cpu_ptr(x86_cpu_to_node_map))
791 return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
792
793 if (!per_cpu_offset(cpu)) {
794 printk(KERN_WARNING
795 "early_cpu_to_node(%d): no per_cpu area!\n", cpu);
796 dump_stack();
797 return NUMA_NO_NODE;
798 }
799 return per_cpu(x86_cpu_to_node_map, cpu);
800}
801
802
803/* empty cpumask */
804static const cpumask_t cpu_mask_none;
805
806/*
807 * Returns a pointer to the bitmask of CPUs on Node 'node'.
808 */
809const cpumask_t *cpumask_of_node(int node)
810{
811 if (node_to_cpumask_map == NULL) {
812 printk(KERN_WARNING
813 "cpumask_of_node(%d): no node_to_cpumask_map!\n",
814 node);
815 dump_stack();
816 return (const cpumask_t *)&cpu_online_map;
817 }
818 if (node >= nr_node_ids) {
819 printk(KERN_WARNING
820 "cpumask_of_node(%d): node > nr_node_ids(%d)\n",
821 node, nr_node_ids);
822 dump_stack();
823 return &cpu_mask_none;
824 }
825 return &node_to_cpumask_map[node];
826}
827EXPORT_SYMBOL(cpumask_of_node);
828
829/*
830 * Returns a bitmask of CPUs on Node 'node'.
831 *
832 * Side note: this function creates the returned cpumask on the stack
833 * so with a high NR_CPUS count, excessive stack space is used. The
834 * node_to_cpumask_ptr function should be used whenever possible.
835 */
836cpumask_t node_to_cpumask(int node)
837{
838 if (node_to_cpumask_map == NULL) {
839 printk(KERN_WARNING
840 "node_to_cpumask(%d): no node_to_cpumask_map!\n", node);
841 dump_stack();
842 return cpu_online_map;
843 }
844 if (node >= nr_node_ids) {
845 printk(KERN_WARNING
846 "node_to_cpumask(%d): node > nr_node_ids(%d)\n",
847 node, nr_node_ids);
848 dump_stack();
849 return cpu_mask_none;
850 }
851 return node_to_cpumask_map[node];
852}
853EXPORT_SYMBOL(node_to_cpumask);
854
855/*
856 * --------- end of debug versions of the numa functions ---------
857 */
858
859#endif /* CONFIG_DEBUG_PER_CPU_MAPS */