]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/x86/mm/numa_64.c
x86: cleanup numa_64.c
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / mm / numa_64.c
CommitLineData
e3cfe529 1/*
1da177e4
LT
2 * Generic VM initialization for x86-64 NUMA setups.
3 * Copyright 2002,2003 Andi Kleen, SuSE Labs.
e3cfe529 4 */
1da177e4
LT
5#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>
14
15#include <asm/e820.h>
16#include <asm/proto.h>
17#include <asm/dma.h>
18#include <asm/numa.h>
19#include <asm/acpi.h>
c9ff0342 20#include <asm/k8.h>
1da177e4
LT
21
22#ifndef Dprintk
23#define Dprintk(x...)
24#endif
25
6c231b7b 26struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
e3cfe529
TG
27EXPORT_SYMBOL(node_data);
28
1da177e4
LT
29bootmem_data_t plat_node_bdata[MAX_NUMNODES];
30
dcf36bfa 31struct memnode memnode;
1da177e4 32
3f098c26
AK
33unsigned char cpu_to_node[NR_CPUS] __read_mostly = {
34 [0 ... NR_CPUS-1] = NUMA_NO_NODE
0b07e984 35};
e3cfe529
TG
36EXPORT_SYMBOL(cpu_to_node);
37
3f098c26 38unsigned char apicid_to_node[MAX_LOCAL_APIC] __cpuinitdata = {
e3cfe529 39 [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
3f098c26 40};
e3cfe529 41
3f098c26 42cpumask_t node_to_cpumask[MAX_NUMNODES] __read_mostly;
e3cfe529 43EXPORT_SYMBOL(node_to_cpumask);
1da177e4
LT
44
45int numa_off __initdata;
076422d2
AS
46unsigned long __initdata nodemap_addr;
47unsigned long __initdata nodemap_size;
1da177e4 48
529a3404
ED
49/*
50 * Given a shift value, try to populate memnodemap[]
51 * Returns :
52 * 1 if OK
53 * 0 if memnodmap[] too small (of shift too small)
54 * -1 if node overlap or lost ram (shift too big)
55 */
e3cfe529
TG
56static int __init populate_memnodemap(const struct bootnode *nodes,
57 int numnodes, int shift)
1da177e4 58{
529a3404 59 unsigned long addr, end;
e3cfe529 60 int i, res = -1;
b684664f 61
076422d2 62 memset(memnodemap, 0xff, memnodemapsize);
b684664f 63 for (i = 0; i < numnodes; i++) {
529a3404
ED
64 addr = nodes[i].start;
65 end = nodes[i].end;
66 if (addr >= end)
b684664f 67 continue;
076422d2 68 if ((end >> shift) >= memnodemapsize)
529a3404
ED
69 return 0;
70 do {
71 if (memnodemap[addr >> shift] != 0xff)
b684664f 72 return -1;
b684664f 73 memnodemap[addr >> shift] = i;
076422d2 74 addr += (1UL << shift);
529a3404
ED
75 } while (addr < end);
76 res = 1;
e3cfe529 77 }
529a3404
ED
78 return res;
79}
80
076422d2
AS
81static int __init allocate_cachealigned_memnodemap(void)
82{
83 unsigned long pad, pad_addr;
84
85 memnodemap = memnode.embedded_map;
54413927 86 if (memnodemapsize <= 48)
076422d2 87 return 0;
076422d2
AS
88
89 pad = L1_CACHE_BYTES - 1;
90 pad_addr = 0x8000;
91 nodemap_size = pad + memnodemapsize;
92 nodemap_addr = find_e820_area(pad_addr, end_pfn<<PAGE_SHIFT,
93 nodemap_size);
94 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 }
100 pad_addr = (nodemap_addr + pad) & ~pad;
101 memnodemap = phys_to_virt(pad_addr);
102
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 */
e3cfe529
TG
112static int __init extract_lsb_from_nodes(const struct bootnode *nodes,
113 int numnodes)
529a3404 114{
54413927 115 int i, nodes_used = 0;
076422d2
AS
116 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;
54413927
AS
124 bitfield |= start;
125 nodes_used++;
076422d2
AS
126 if (end > memtop)
127 memtop = end;
128 }
54413927
AS
129 if (nodes_used <= 1)
130 i = 63;
131 else
132 i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
076422d2
AS
133 memnodemapsize = (memtop >> i)+1;
134 return i;
135}
529a3404 136
076422d2
AS
137int __init compute_hash_shift(struct bootnode *nodes, int numnodes)
138{
139 int shift;
529a3404 140
076422d2
AS
141 shift = extract_lsb_from_nodes(nodes, numnodes);
142 if (allocate_cachealigned_memnodemap())
143 return -1;
6b050f80 144 printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
529a3404
ED
145 shift);
146
147 if (populate_memnodemap(nodes, numnodes, shift) != 1) {
e3cfe529
TG
148 printk(KERN_INFO "Your memory is not aligned you need to "
149 "rebuild your kernel with a bigger NODEMAPSIZE "
150 "shift=%d\n", shift);
529a3404
ED
151 return -1;
152 }
b684664f 153 return shift;
1da177e4
LT
154}
155
bbfceef4
MT
156#ifdef CONFIG_SPARSEMEM
157int early_pfn_to_nid(unsigned long pfn)
158{
159 return phys_to_nid(pfn << PAGE_SHIFT);
160}
161#endif
162
e3cfe529
TG
163static void * __init early_node_mem(int nodeid, unsigned long start,
164 unsigned long end, unsigned long size)
a8062231
AK
165{
166 unsigned long mem = find_e820_area(start, end, size);
167 void *ptr;
e3cfe529 168
a8062231
AK
169 if (mem != -1L)
170 return __va(mem);
171 ptr = __alloc_bootmem_nopanic(size,
172 SMP_CACHE_BYTES, __pa(MAX_DMA_ADDRESS));
83e83d54 173 if (ptr == NULL) {
a8062231 174 printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
e3cfe529 175 size, nodeid);
a8062231
AK
176 return NULL;
177 }
178 return ptr;
179}
180
1da177e4 181/* Initialize bootmem allocator for a node */
e3cfe529
TG
182void __init setup_node_bootmem(int nodeid, unsigned long start,
183 unsigned long end)
184{
185 unsigned long start_pfn, end_pfn, bootmap_pages, bootmap_size;
186 unsigned long bootmap_start, nodedata_phys;
a8062231 187 void *bootmap;
1da177e4
LT
188 const int pgdat_size = round_up(sizeof(pg_data_t), PAGE_SIZE);
189
e3cfe529 190 start = round_up(start, ZONE_ALIGN);
1da177e4 191
e3cfe529
TG
192 printk(KERN_INFO "Bootmem setup node %d %016lx-%016lx\n", nodeid,
193 start, end);
1da177e4
LT
194
195 start_pfn = start >> PAGE_SHIFT;
196 end_pfn = end >> PAGE_SHIFT;
197
a8062231
AK
198 node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size);
199 if (node_data[nodeid] == NULL)
200 return;
201 nodedata_phys = __pa(node_data[nodeid]);
1da177e4 202
1da177e4
LT
203 memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
204 NODE_DATA(nodeid)->bdata = &plat_node_bdata[nodeid];
205 NODE_DATA(nodeid)->node_start_pfn = start_pfn;
206 NODE_DATA(nodeid)->node_spanned_pages = end_pfn - start_pfn;
207
208 /* Find a place for the bootmem map */
e3cfe529 209 bootmap_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
1da177e4 210 bootmap_start = round_up(nodedata_phys + pgdat_size, PAGE_SIZE);
a8062231
AK
211 bootmap = early_node_mem(nodeid, bootmap_start, end,
212 bootmap_pages<<PAGE_SHIFT);
213 if (bootmap == NULL) {
214 if (nodedata_phys < start || nodedata_phys >= end)
e3cfe529
TG
215 free_bootmem((unsigned long)node_data[nodeid],
216 pgdat_size);
a8062231
AK
217 node_data[nodeid] = NULL;
218 return;
219 }
220 bootmap_start = __pa(bootmap);
e3cfe529
TG
221 Dprintk("bootmap start %lu pages %lu\n", bootmap_start, bootmap_pages);
222
1da177e4 223 bootmap_size = init_bootmem_node(NODE_DATA(nodeid),
e3cfe529
TG
224 bootmap_start >> PAGE_SHIFT,
225 start_pfn, end_pfn);
1da177e4 226
5cb248ab 227 free_bootmem_with_active_regions(nodeid, end);
1da177e4 228
e3cfe529
TG
229 reserve_bootmem_node(NODE_DATA(nodeid), nodedata_phys, pgdat_size);
230 reserve_bootmem_node(NODE_DATA(nodeid), bootmap_start,
231 bootmap_pages<<PAGE_SHIFT);
68a3a7fe
AK
232#ifdef CONFIG_ACPI_NUMA
233 srat_reserve_add_area(nodeid);
234#endif
1da177e4 235 node_set_online(nodeid);
e3cfe529 236}
1da177e4
LT
237
238/* Initialize final allocator for a zone */
239void __init setup_node_zones(int nodeid)
e3cfe529 240{
267b4801 241 unsigned long start_pfn, end_pfn, memmapsize, limit;
1da177e4 242
e3cfe529
TG
243 start_pfn = node_start_pfn(nodeid);
244 end_pfn = node_end_pfn(nodeid);
1da177e4 245
5cb248ab 246 Dprintk(KERN_INFO "Setting up memmap for node %d %lx-%lx\n",
a2f1b424 247 nodeid, start_pfn, end_pfn);
1da177e4 248
e3cfe529
TG
249 /*
250 * Try to allocate mem_map at end to not fill up precious <4GB
251 * memory.
252 */
267b4801
AK
253 memmapsize = sizeof(struct page) * (end_pfn-start_pfn);
254 limit = end_pfn << PAGE_SHIFT;
3b5fd59f 255#ifdef CONFIG_FLAT_NODE_MEM_MAP
e3cfe529
TG
256 NODE_DATA(nodeid)->node_mem_map =
257 __alloc_bootmem_core(NODE_DATA(nodeid)->bdata,
258 memmapsize, SMP_CACHE_BYTES,
259 round_down(limit - memmapsize, PAGE_SIZE),
260 limit);
3b5fd59f 261#endif
e3cfe529 262}
1da177e4 263
e3cfe529
TG
264/*
265 * There are unfortunately some poorly designed mainboards around that
266 * only connect memory to a single CPU. This breaks the 1:1 cpu->node
267 * mapping. To avoid this fill in the mapping for all possible CPUs,
268 * as the number of CPUs is not known yet. We round robin the existing
269 * nodes.
270 */
1da177e4
LT
271void __init numa_init_array(void)
272{
273 int rr, i;
e3cfe529 274
85cc5135 275 rr = first_node(node_online_map);
1da177e4 276 for (i = 0; i < NR_CPUS; i++) {
98c9e27a 277 if (cpu_to_node(i) != NUMA_NO_NODE)
1da177e4 278 continue;
e3cfe529 279 numa_set_node(i, rr);
1da177e4
LT
280 rr = next_node(rr, node_online_map);
281 if (rr == MAX_NUMNODES)
282 rr = first_node(node_online_map);
1da177e4 283 }
1da177e4
LT
284}
285
286#ifdef CONFIG_NUMA_EMU
53fee04f 287/* Numa emulation */
8b8ca80e 288char *cmdline __initdata;
1da177e4 289
53fee04f 290/*
e3cfe529
TG
291 * Setups up nid to range from addr to addr + size. If the end
292 * boundary is greater than max_addr, then max_addr is used instead.
293 * The return value is 0 if there is additional memory left for
294 * allocation past addr and -1 otherwise. addr is adjusted to be at
295 * the end of the node.
53fee04f 296 */
8b8ca80e
DR
297static int __init setup_node_range(int nid, struct bootnode *nodes, u64 *addr,
298 u64 size, u64 max_addr)
53fee04f 299{
8b8ca80e 300 int ret = 0;
e3cfe529 301
8b8ca80e
DR
302 nodes[nid].start = *addr;
303 *addr += size;
304 if (*addr >= max_addr) {
305 *addr = max_addr;
306 ret = -1;
307 }
308 nodes[nid].end = *addr;
e3f1caee 309 node_set(nid, node_possible_map);
8b8ca80e
DR
310 printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
311 nodes[nid].start, nodes[nid].end,
312 (nodes[nid].end - nodes[nid].start) >> 20);
313 return ret;
53fee04f
RS
314}
315
8b8ca80e
DR
316/*
317 * Splits num_nodes nodes up equally starting at node_start. The return value
318 * is the number of nodes split up and addr is adjusted to be at the end of the
319 * last node allocated.
320 */
321static int __init split_nodes_equally(struct bootnode *nodes, u64 *addr,
322 u64 max_addr, int node_start,
323 int num_nodes)
1da177e4 324{
8b8ca80e
DR
325 unsigned int big;
326 u64 size;
327 int i;
53fee04f 328
8b8ca80e
DR
329 if (num_nodes <= 0)
330 return -1;
331 if (num_nodes > MAX_NUMNODES)
332 num_nodes = MAX_NUMNODES;
a7e96629 333 size = (max_addr - *addr - e820_hole_size(*addr, max_addr)) /
8b8ca80e 334 num_nodes;
53fee04f 335 /*
8b8ca80e
DR
336 * Calculate the number of big nodes that can be allocated as a result
337 * of consolidating the leftovers.
53fee04f 338 */
8b8ca80e
DR
339 big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * num_nodes) /
340 FAKE_NODE_MIN_SIZE;
341
342 /* Round down to nearest FAKE_NODE_MIN_SIZE. */
343 size &= FAKE_NODE_MIN_HASH_MASK;
344 if (!size) {
345 printk(KERN_ERR "Not enough memory for each node. "
346 "NUMA emulation disabled.\n");
347 return -1;
53fee04f 348 }
8b8ca80e
DR
349
350 for (i = node_start; i < num_nodes + node_start; i++) {
351 u64 end = *addr + size;
e3cfe529 352
53fee04f
RS
353 if (i < big)
354 end += FAKE_NODE_MIN_SIZE;
355 /*
8b8ca80e
DR
356 * The final node can have the remaining system RAM. Other
357 * nodes receive roughly the same amount of available pages.
53fee04f 358 */
8b8ca80e
DR
359 if (i == num_nodes + node_start - 1)
360 end = max_addr;
361 else
a7e96629 362 while (end - *addr - e820_hole_size(*addr, end) <
8b8ca80e
DR
363 size) {
364 end += FAKE_NODE_MIN_SIZE;
365 if (end > max_addr) {
366 end = max_addr;
367 break;
368 }
369 }
370 if (setup_node_range(i, nodes, addr, end - *addr, max_addr) < 0)
371 break;
372 }
373 return i - node_start + 1;
374}
375
382591d5
DR
376/*
377 * Splits the remaining system RAM into chunks of size. The remaining memory is
378 * always assigned to a final node and can be asymmetric. Returns the number of
379 * nodes split.
380 */
381static int __init split_nodes_by_size(struct bootnode *nodes, u64 *addr,
382 u64 max_addr, int node_start, u64 size)
383{
384 int i = node_start;
385 size = (size << 20) & FAKE_NODE_MIN_HASH_MASK;
386 while (!setup_node_range(i++, nodes, addr, size, max_addr))
387 ;
388 return i - node_start;
389}
390
8b8ca80e
DR
391/*
392 * Sets up the system RAM area from start_pfn to end_pfn according to the
393 * numa=fake command-line option.
394 */
395static int __init numa_emulation(unsigned long start_pfn, unsigned long end_pfn)
396{
397 struct bootnode nodes[MAX_NUMNODES];
e3cfe529 398 u64 size, addr = start_pfn << PAGE_SHIFT;
8b8ca80e 399 u64 max_addr = end_pfn << PAGE_SHIFT;
e3cfe529 400 int num_nodes = 0, num = 0, coeff_flag, coeff = -1, i;
8b8ca80e
DR
401
402 memset(&nodes, 0, sizeof(nodes));
403 /*
404 * If the numa=fake command-line is just a single number N, split the
405 * system RAM into N fake nodes.
406 */
407 if (!strchr(cmdline, '*') && !strchr(cmdline, ',')) {
e3cfe529
TG
408 long n = simple_strtol(cmdline, NULL, 0);
409
410 num_nodes = split_nodes_equally(nodes, &addr, max_addr, 0, n);
8b8ca80e
DR
411 if (num_nodes < 0)
412 return num_nodes;
413 goto out;
414 }
415
416 /* Parse the command line. */
382591d5 417 for (coeff_flag = 0; ; cmdline++) {
8b8ca80e
DR
418 if (*cmdline && isdigit(*cmdline)) {
419 num = num * 10 + *cmdline - '0';
420 continue;
53fee04f 421 }
382591d5
DR
422 if (*cmdline == '*') {
423 if (num > 0)
424 coeff = num;
425 coeff_flag = 1;
426 }
8b8ca80e 427 if (!*cmdline || *cmdline == ',') {
382591d5
DR
428 if (!coeff_flag)
429 coeff = 1;
8b8ca80e
DR
430 /*
431 * Round down to the nearest FAKE_NODE_MIN_SIZE.
432 * Command-line coefficients are in megabytes.
433 */
434 size = ((u64)num << 20) & FAKE_NODE_MIN_HASH_MASK;
382591d5 435 if (size)
8b8ca80e
DR
436 for (i = 0; i < coeff; i++, num_nodes++)
437 if (setup_node_range(num_nodes, nodes,
438 &addr, size, max_addr) < 0)
439 goto done;
382591d5
DR
440 if (!*cmdline)
441 break;
442 coeff_flag = 0;
443 coeff = -1;
53fee04f 444 }
8b8ca80e
DR
445 num = 0;
446 }
447done:
448 if (!num_nodes)
449 return -1;
14694d73 450 /* Fill remainder of system RAM, if appropriate. */
8b8ca80e 451 if (addr < max_addr) {
382591d5
DR
452 if (coeff_flag && coeff < 0) {
453 /* Split remaining nodes into num-sized chunks */
454 num_nodes += split_nodes_by_size(nodes, &addr, max_addr,
455 num_nodes, num);
456 goto out;
457 }
14694d73
DR
458 switch (*(cmdline - 1)) {
459 case '*':
460 /* Split remaining nodes into coeff chunks */
461 if (coeff <= 0)
462 break;
463 num_nodes += split_nodes_equally(nodes, &addr, max_addr,
464 num_nodes, coeff);
465 break;
466 case ',':
467 /* Do not allocate remaining system RAM */
468 break;
469 default:
470 /* Give one final node */
471 setup_node_range(num_nodes, nodes, &addr,
472 max_addr - addr, max_addr);
473 num_nodes++;
474 }
8b8ca80e
DR
475 }
476out:
477 memnode_shift = compute_hash_shift(nodes, num_nodes);
478 if (memnode_shift < 0) {
479 memnode_shift = 0;
480 printk(KERN_ERR "No NUMA hash function found. NUMA emulation "
481 "disabled.\n");
482 return -1;
483 }
484
485 /*
486 * We need to vacate all active ranges that may have been registered by
1c05f093
DR
487 * SRAT and set acpi_numa to -1 so that srat_disabled() always returns
488 * true. NUMA emulation has succeeded so we will not scan ACPI nodes.
8b8ca80e
DR
489 */
490 remove_all_active_ranges();
1c05f093
DR
491#ifdef CONFIG_ACPI_NUMA
492 acpi_numa = -1;
493#endif
e3f1caee 494 for_each_node_mask(i, node_possible_map) {
5cb248ab
MG
495 e820_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
496 nodes[i].end >> PAGE_SHIFT);
e3cfe529 497 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
5cb248ab 498 }
3484d798 499 acpi_fake_nodes(nodes, num_nodes);
e3cfe529
TG
500 numa_init_array();
501 return 0;
1da177e4 502}
8b8ca80e 503#endif /* CONFIG_NUMA_EMU */
1da177e4
LT
504
505void __init numa_initmem_init(unsigned long start_pfn, unsigned long end_pfn)
e3cfe529 506{
1da177e4
LT
507 int i;
508
e3f1caee
SS
509 nodes_clear(node_possible_map);
510
1da177e4 511#ifdef CONFIG_NUMA_EMU
8b8ca80e 512 if (cmdline && !numa_emulation(start_pfn, end_pfn))
e3cfe529 513 return;
e3f1caee 514 nodes_clear(node_possible_map);
1da177e4
LT
515#endif
516
517#ifdef CONFIG_ACPI_NUMA
518 if (!numa_off && !acpi_scan_nodes(start_pfn << PAGE_SHIFT,
519 end_pfn << PAGE_SHIFT))
e3cfe529 520 return;
e3f1caee 521 nodes_clear(node_possible_map);
1da177e4
LT
522#endif
523
524#ifdef CONFIG_K8_NUMA
e3cfe529
TG
525 if (!numa_off && !k8_scan_nodes(start_pfn<<PAGE_SHIFT,
526 end_pfn<<PAGE_SHIFT))
1da177e4 527 return;
e3f1caee 528 nodes_clear(node_possible_map);
1da177e4
LT
529#endif
530 printk(KERN_INFO "%s\n",
531 numa_off ? "NUMA turned off" : "No NUMA configuration found");
532
e3cfe529 533 printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
1da177e4 534 start_pfn << PAGE_SHIFT,
e3cfe529
TG
535 end_pfn << PAGE_SHIFT);
536 /* setup dummy node covering all memory */
537 memnode_shift = 63;
076422d2 538 memnodemap = memnode.embedded_map;
1da177e4
LT
539 memnodemap[0] = 0;
540 nodes_clear(node_online_map);
541 node_set_online(0);
e3f1caee 542 node_set(0, node_possible_map);
1da177e4 543 for (i = 0; i < NR_CPUS; i++)
69d81fcd 544 numa_set_node(i, 0);
1da177e4 545 node_to_cpumask[0] = cpumask_of_cpu(0);
5cb248ab 546 e820_register_active_regions(0, start_pfn, end_pfn);
1da177e4
LT
547 setup_node_bootmem(0, start_pfn << PAGE_SHIFT, end_pfn << PAGE_SHIFT);
548}
549
e6982c67 550__cpuinit void numa_add_cpu(int cpu)
1da177e4 551{
e6a045a5 552 set_bit(cpu, &node_to_cpumask[cpu_to_node(cpu)]);
e3cfe529 553}
1da177e4 554
69d81fcd
AK
555void __cpuinit numa_set_node(int cpu, int node)
556{
df79efde 557 cpu_pda(cpu)->nodenumber = node;
98c9e27a 558 cpu_to_node(cpu) = node;
69d81fcd
AK
559}
560
e3cfe529
TG
561unsigned long __init numa_free_all_bootmem(void)
562{
1da177e4 563 unsigned long pages = 0;
e3cfe529
TG
564 int i;
565
566 for_each_online_node(i)
1da177e4 567 pages += free_all_bootmem_node(NODE_DATA(i));
e3cfe529 568
1da177e4 569 return pages;
e3cfe529 570}
1da177e4
LT
571
572void __init paging_init(void)
e3cfe529 573{
6391af17 574 unsigned long max_zone_pfns[MAX_NR_ZONES];
e3cfe529
TG
575 int i;
576
6391af17
MG
577 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
578 max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
579 max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
580 max_zone_pfns[ZONE_NORMAL] = end_pfn;
d3ee871e 581
f0a5a58a
BP
582 sparse_memory_present_with_active_regions(MAX_NUMNODES);
583 sparse_init();
d3ee871e 584
e3cfe529
TG
585 for_each_online_node(i)
586 setup_node_zones(i);
5cb248ab
MG
587
588 free_area_init_nodes(max_zone_pfns);
e3cfe529 589}
1da177e4 590
2c8c0e6b 591static __init int numa_setup(char *opt)
e3cfe529 592{
2c8c0e6b
AK
593 if (!opt)
594 return -EINVAL;
e3cfe529 595 if (!strncmp(opt, "off", 3))
1da177e4
LT
596 numa_off = 1;
597#ifdef CONFIG_NUMA_EMU
8b8ca80e
DR
598 if (!strncmp(opt, "fake=", 5))
599 cmdline = opt + 5;
1da177e4
LT
600#endif
601#ifdef CONFIG_ACPI_NUMA
e3cfe529
TG
602 if (!strncmp(opt, "noacpi", 6))
603 acpi_numa = -1;
604 if (!strncmp(opt, "hotadd=", 7))
68a3a7fe 605 hotadd_percent = simple_strtoul(opt+7, NULL, 10);
1da177e4 606#endif
2c8c0e6b 607 return 0;
e3cfe529 608}
2c8c0e6b
AK
609early_param("numa", numa_setup);
610
05b3cbd8
RT
611/*
612 * Setup early cpu_to_node.
613 *
614 * Populate cpu_to_node[] only if x86_cpu_to_apicid[],
615 * and apicid_to_node[] tables have valid entries for a CPU.
616 * This means we skip cpu_to_node[] initialisation for NUMA
617 * emulation and faking node case (when running a kernel compiled
618 * for NUMA on a non NUMA box), which is OK as cpu_to_node[]
619 * is already initialized in a round robin manner at numa_init_array,
620 * prior to this call, and this initialization is good enough
621 * for the fake NUMA cases.
622 */
623void __init init_cpu_to_node(void)
624{
625 int i;
e3cfe529
TG
626
627 for (i = 0; i < NR_CPUS; i++) {
71fff5e6 628 u8 apicid = x86_cpu_to_apicid_init[i];
e3cfe529 629
05b3cbd8
RT
630 if (apicid == BAD_APICID)
631 continue;
632 if (apicid_to_node[apicid] == NUMA_NO_NODE)
633 continue;
e3cfe529 634 numa_set_node(i, apicid_to_node[apicid]);
05b3cbd8
RT
635 }
636}
637
cf050132
AK
638#ifdef CONFIG_DISCONTIGMEM
639/*
640 * Functions to convert PFNs from/to per node page addresses.
641 * These are out of line because they are quite big.
642 * They could be all tuned by pre caching more state.
643 * Should do that.
644 */
645
cf050132
AK
646int pfn_valid(unsigned long pfn)
647{
648 unsigned nid;
649 if (pfn >= num_physpages)
650 return 0;
651 nid = pfn_to_nid(pfn);
652 if (nid == 0xff)
653 return 0;
654 return pfn >= node_start_pfn(nid) && (pfn) < node_end_pfn(nid);
655}
656EXPORT_SYMBOL(pfn_valid);
657#endif