]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - arch/powerpc/mm/numa.c
powerpc: Sync RPA note in zImage with kernel's RPA note
[mirror_ubuntu-zesty-kernel.git] / arch / powerpc / mm / numa.c
CommitLineData
1da177e4
LT
1/*
2 * pSeries NUMA support
3 *
4 * Copyright (C) 2002 Anton Blanchard <anton@au.ibm.com>, IBM
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11#include <linux/threads.h>
12#include <linux/bootmem.h>
13#include <linux/init.h>
14#include <linux/mm.h>
15#include <linux/mmzone.h>
16#include <linux/module.h>
17#include <linux/nodemask.h>
18#include <linux/cpu.h>
19#include <linux/notifier.h>
d9b2b2a2 20#include <linux/lmb.h>
6df1646e 21#include <linux/of.h>
45fb6cea 22#include <asm/sparsemem.h>
d9b2b2a2 23#include <asm/prom.h>
cf00a8d1 24#include <asm/system.h>
2249ca9d 25#include <asm/smp.h>
1da177e4
LT
26
27static int numa_enabled = 1;
28
1daa6d08
BS
29static char *cmdline __initdata;
30
1da177e4
LT
31static int numa_debug;
32#define dbg(args...) if (numa_debug) { printk(KERN_INFO args); }
33
45fb6cea 34int numa_cpu_lookup_table[NR_CPUS];
1da177e4 35cpumask_t numa_cpumask_lookup_table[MAX_NUMNODES];
1da177e4 36struct pglist_data *node_data[MAX_NUMNODES];
45fb6cea
AB
37
38EXPORT_SYMBOL(numa_cpu_lookup_table);
39EXPORT_SYMBOL(numa_cpumask_lookup_table);
40EXPORT_SYMBOL(node_data);
41
1da177e4 42static int min_common_depth;
237a0989 43static int n_mem_addr_cells, n_mem_size_cells;
1da177e4 44
1daa6d08
BS
45static int __cpuinit fake_numa_create_new_node(unsigned long end_pfn,
46 unsigned int *nid)
47{
48 unsigned long long mem;
49 char *p = cmdline;
50 static unsigned int fake_nid;
51 static unsigned long long curr_boundary;
52
53 /*
54 * Modify node id, iff we started creating NUMA nodes
55 * We want to continue from where we left of the last time
56 */
57 if (fake_nid)
58 *nid = fake_nid;
59 /*
60 * In case there are no more arguments to parse, the
61 * node_id should be the same as the last fake node id
62 * (we've handled this above).
63 */
64 if (!p)
65 return 0;
66
67 mem = memparse(p, &p);
68 if (!mem)
69 return 0;
70
71 if (mem < curr_boundary)
72 return 0;
73
74 curr_boundary = mem;
75
76 if ((end_pfn << PAGE_SHIFT) > mem) {
77 /*
78 * Skip commas and spaces
79 */
80 while (*p == ',' || *p == ' ' || *p == '\t')
81 p++;
82
83 cmdline = p;
84 fake_nid++;
85 *nid = fake_nid;
86 dbg("created new fake_node with id %d\n", fake_nid);
87 return 1;
88 }
89 return 0;
90}
91
2e5ce39d 92static void __cpuinit map_cpu_to_node(int cpu, int node)
1da177e4
LT
93{
94 numa_cpu_lookup_table[cpu] = node;
45fb6cea 95
bf4b85b0
NL
96 dbg("adding cpu %d to node %d\n", cpu, node);
97
45fb6cea 98 if (!(cpu_isset(cpu, numa_cpumask_lookup_table[node])))
1da177e4 99 cpu_set(cpu, numa_cpumask_lookup_table[node]);
1da177e4
LT
100}
101
102#ifdef CONFIG_HOTPLUG_CPU
103static void unmap_cpu_from_node(unsigned long cpu)
104{
105 int node = numa_cpu_lookup_table[cpu];
106
107 dbg("removing cpu %lu from node %d\n", cpu, node);
108
109 if (cpu_isset(cpu, numa_cpumask_lookup_table[node])) {
110 cpu_clear(cpu, numa_cpumask_lookup_table[node]);
1da177e4
LT
111 } else {
112 printk(KERN_ERR "WARNING: cpu %lu not found in node %d\n",
113 cpu, node);
114 }
115}
116#endif /* CONFIG_HOTPLUG_CPU */
117
2e5ce39d 118static struct device_node * __cpuinit find_cpu_node(unsigned int cpu)
1da177e4
LT
119{
120 unsigned int hw_cpuid = get_hard_smp_processor_id(cpu);
121 struct device_node *cpu_node = NULL;
a7f67bdf 122 const unsigned int *interrupt_server, *reg;
1da177e4
LT
123 int len;
124
125 while ((cpu_node = of_find_node_by_type(cpu_node, "cpu")) != NULL) {
126 /* Try interrupt server first */
e2eb6392 127 interrupt_server = of_get_property(cpu_node,
1da177e4
LT
128 "ibm,ppc-interrupt-server#s", &len);
129
130 len = len / sizeof(u32);
131
132 if (interrupt_server && (len > 0)) {
133 while (len--) {
134 if (interrupt_server[len] == hw_cpuid)
135 return cpu_node;
136 }
137 } else {
e2eb6392 138 reg = of_get_property(cpu_node, "reg", &len);
1da177e4
LT
139 if (reg && (len > 0) && (reg[0] == hw_cpuid))
140 return cpu_node;
141 }
142 }
143
144 return NULL;
145}
146
147/* must hold reference to node during call */
a7f67bdf 148static const int *of_get_associativity(struct device_node *dev)
1da177e4 149{
e2eb6392 150 return of_get_property(dev, "ibm,associativity", NULL);
1da177e4
LT
151}
152
cf00085d
C
153/*
154 * Returns the property linux,drconf-usable-memory if
155 * it exists (the property exists only in kexec/kdump kernels,
156 * added by kexec-tools)
157 */
158static const u32 *of_get_usable_memory(struct device_node *memory)
159{
160 const u32 *prop;
161 u32 len;
162 prop = of_get_property(memory, "linux,drconf-usable-memory", &len);
163 if (!prop || len < sizeof(unsigned int))
164 return 0;
165 return prop;
166}
167
482ec7c4
NL
168/* Returns nid in the range [0..MAX_NUMNODES-1], or -1 if no useful numa
169 * info is found.
170 */
953039c8 171static int of_node_to_nid_single(struct device_node *device)
1da177e4 172{
482ec7c4 173 int nid = -1;
a7f67bdf 174 const unsigned int *tmp;
1da177e4
LT
175
176 if (min_common_depth == -1)
482ec7c4 177 goto out;
1da177e4
LT
178
179 tmp = of_get_associativity(device);
482ec7c4
NL
180 if (!tmp)
181 goto out;
182
183 if (tmp[0] >= min_common_depth)
cf950b7a 184 nid = tmp[min_common_depth];
bc16a759
NL
185
186 /* POWER4 LPAR uses 0xffff as invalid node */
482ec7c4
NL
187 if (nid == 0xffff || nid >= MAX_NUMNODES)
188 nid = -1;
189out:
cf950b7a 190 return nid;
1da177e4
LT
191}
192
953039c8
JK
193/* Walk the device tree upwards, looking for an associativity id */
194int of_node_to_nid(struct device_node *device)
195{
196 struct device_node *tmp;
197 int nid = -1;
198
199 of_node_get(device);
200 while (device) {
201 nid = of_node_to_nid_single(device);
202 if (nid != -1)
203 break;
204
205 tmp = device;
206 device = of_get_parent(tmp);
207 of_node_put(tmp);
208 }
209 of_node_put(device);
210
211 return nid;
212}
213EXPORT_SYMBOL_GPL(of_node_to_nid);
214
1da177e4
LT
215/*
216 * In theory, the "ibm,associativity" property may contain multiple
217 * associativity lists because a resource may be multiply connected
218 * into the machine. This resource then has different associativity
219 * characteristics relative to its multiple connections. We ignore
220 * this for now. We also assume that all cpu and memory sets have
221 * their distances represented at a common level. This won't be
1b3c3714 222 * true for hierarchical NUMA.
1da177e4
LT
223 *
224 * In any case the ibm,associativity-reference-points should give
225 * the correct depth for a normal NUMA system.
226 *
227 * - Dave Hansen <haveblue@us.ibm.com>
228 */
229static int __init find_min_common_depth(void)
230{
231 int depth;
a7f67bdf 232 const unsigned int *ref_points;
1da177e4
LT
233 struct device_node *rtas_root;
234 unsigned int len;
235
236 rtas_root = of_find_node_by_path("/rtas");
237
238 if (!rtas_root)
239 return -1;
240
241 /*
242 * this property is 2 32-bit integers, each representing a level of
243 * depth in the associativity nodes. The first is for an SMP
244 * configuration (should be all 0's) and the second is for a normal
245 * NUMA configuration.
246 */
e2eb6392 247 ref_points = of_get_property(rtas_root,
1da177e4
LT
248 "ibm,associativity-reference-points", &len);
249
250 if ((len >= 1) && ref_points) {
251 depth = ref_points[1];
252 } else {
bf4b85b0 253 dbg("NUMA: ibm,associativity-reference-points not found.\n");
1da177e4
LT
254 depth = -1;
255 }
256 of_node_put(rtas_root);
257
258 return depth;
259}
260
84c9fdd1 261static void __init get_n_mem_cells(int *n_addr_cells, int *n_size_cells)
1da177e4
LT
262{
263 struct device_node *memory = NULL;
1da177e4
LT
264
265 memory = of_find_node_by_type(memory, "memory");
54c23310 266 if (!memory)
84c9fdd1 267 panic("numa.c: No memory nodes found!");
54c23310 268
a8bda5dd 269 *n_addr_cells = of_n_addr_cells(memory);
9213feea 270 *n_size_cells = of_n_size_cells(memory);
84c9fdd1 271 of_node_put(memory);
1da177e4
LT
272}
273
a7f67bdf 274static unsigned long __devinit read_n_cells(int n, const unsigned int **buf)
1da177e4
LT
275{
276 unsigned long result = 0;
277
278 while (n--) {
279 result = (result << 32) | **buf;
280 (*buf)++;
281 }
282 return result;
283}
284
8342681d
NF
285struct of_drconf_cell {
286 u64 base_addr;
287 u32 drc_index;
288 u32 reserved;
289 u32 aa_index;
290 u32 flags;
291};
292
293#define DRCONF_MEM_ASSIGNED 0x00000008
294#define DRCONF_MEM_AI_INVALID 0x00000040
295#define DRCONF_MEM_RESERVED 0x00000080
296
297/*
298 * Read the next lmb list entry from the ibm,dynamic-memory property
299 * and return the information in the provided of_drconf_cell structure.
300 */
301static void read_drconf_cell(struct of_drconf_cell *drmem, const u32 **cellp)
302{
303 const u32 *cp;
304
305 drmem->base_addr = read_n_cells(n_mem_addr_cells, cellp);
306
307 cp = *cellp;
308 drmem->drc_index = cp[0];
309 drmem->reserved = cp[1];
310 drmem->aa_index = cp[2];
311 drmem->flags = cp[3];
312
313 *cellp = cp + 4;
314}
315
316/*
317 * Retreive and validate the ibm,dynamic-memory property of the device tree.
318 *
319 * The layout of the ibm,dynamic-memory property is a number N of lmb
320 * list entries followed by N lmb list entries. Each lmb list entry
321 * contains information as layed out in the of_drconf_cell struct above.
322 */
323static int of_get_drconf_memory(struct device_node *memory, const u32 **dm)
324{
325 const u32 *prop;
326 u32 len, entries;
327
328 prop = of_get_property(memory, "ibm,dynamic-memory", &len);
329 if (!prop || len < sizeof(unsigned int))
330 return 0;
331
332 entries = *prop++;
333
334 /* Now that we know the number of entries, revalidate the size
335 * of the property read in to ensure we have everything
336 */
337 if (len < (entries * (n_mem_addr_cells + 4) + 1) * sizeof(unsigned int))
338 return 0;
339
340 *dm = prop;
341 return entries;
342}
343
344/*
345 * Retreive and validate the ibm,lmb-size property for drconf memory
346 * from the device tree.
347 */
348static u64 of_get_lmb_size(struct device_node *memory)
349{
350 const u32 *prop;
351 u32 len;
352
353 prop = of_get_property(memory, "ibm,lmb-size", &len);
354 if (!prop || len < sizeof(unsigned int))
355 return 0;
356
357 return read_n_cells(n_mem_size_cells, &prop);
358}
359
360struct assoc_arrays {
361 u32 n_arrays;
362 u32 array_sz;
363 const u32 *arrays;
364};
365
366/*
367 * Retreive and validate the list of associativity arrays for drconf
368 * memory from the ibm,associativity-lookup-arrays property of the
369 * device tree..
370 *
371 * The layout of the ibm,associativity-lookup-arrays property is a number N
372 * indicating the number of associativity arrays, followed by a number M
373 * indicating the size of each associativity array, followed by a list
374 * of N associativity arrays.
375 */
376static int of_get_assoc_arrays(struct device_node *memory,
377 struct assoc_arrays *aa)
378{
379 const u32 *prop;
380 u32 len;
381
382 prop = of_get_property(memory, "ibm,associativity-lookup-arrays", &len);
383 if (!prop || len < 2 * sizeof(unsigned int))
384 return -1;
385
386 aa->n_arrays = *prop++;
387 aa->array_sz = *prop++;
388
389 /* Now that we know the number of arrrays and size of each array,
390 * revalidate the size of the property read in.
391 */
392 if (len < (aa->n_arrays * aa->array_sz + 2) * sizeof(unsigned int))
393 return -1;
394
395 aa->arrays = prop;
396 return 0;
397}
398
399/*
400 * This is like of_node_to_nid_single() for memory represented in the
401 * ibm,dynamic-reconfiguration-memory node.
402 */
403static int of_drconf_to_nid_single(struct of_drconf_cell *drmem,
404 struct assoc_arrays *aa)
405{
406 int default_nid = 0;
407 int nid = default_nid;
408 int index;
409
410 if (min_common_depth > 0 && min_common_depth <= aa->array_sz &&
411 !(drmem->flags & DRCONF_MEM_AI_INVALID) &&
412 drmem->aa_index < aa->n_arrays) {
413 index = drmem->aa_index * aa->array_sz + min_common_depth - 1;
414 nid = aa->arrays[index];
415
416 if (nid == 0xffff || nid >= MAX_NUMNODES)
417 nid = default_nid;
418 }
419
420 return nid;
421}
422
1da177e4
LT
423/*
424 * Figure out to which domain a cpu belongs and stick it there.
425 * Return the id of the domain used.
426 */
2e5ce39d 427static int __cpuinit numa_setup_cpu(unsigned long lcpu)
1da177e4 428{
cf950b7a 429 int nid = 0;
1da177e4
LT
430 struct device_node *cpu = find_cpu_node(lcpu);
431
432 if (!cpu) {
433 WARN_ON(1);
434 goto out;
435 }
436
953039c8 437 nid = of_node_to_nid_single(cpu);
1da177e4 438
482ec7c4
NL
439 if (nid < 0 || !node_online(nid))
440 nid = any_online_node(NODE_MASK_ALL);
1da177e4 441out:
cf950b7a 442 map_cpu_to_node(lcpu, nid);
1da177e4
LT
443
444 of_node_put(cpu);
445
cf950b7a 446 return nid;
1da177e4
LT
447}
448
74b85f37 449static int __cpuinit cpu_numa_callback(struct notifier_block *nfb,
1da177e4
LT
450 unsigned long action,
451 void *hcpu)
452{
453 unsigned long lcpu = (unsigned long)hcpu;
454 int ret = NOTIFY_DONE;
455
456 switch (action) {
457 case CPU_UP_PREPARE:
8bb78442 458 case CPU_UP_PREPARE_FROZEN:
2b261227 459 numa_setup_cpu(lcpu);
1da177e4
LT
460 ret = NOTIFY_OK;
461 break;
462#ifdef CONFIG_HOTPLUG_CPU
463 case CPU_DEAD:
8bb78442 464 case CPU_DEAD_FROZEN:
1da177e4 465 case CPU_UP_CANCELED:
8bb78442 466 case CPU_UP_CANCELED_FROZEN:
1da177e4
LT
467 unmap_cpu_from_node(lcpu);
468 break;
469 ret = NOTIFY_OK;
470#endif
471 }
472 return ret;
473}
474
475/*
476 * Check and possibly modify a memory region to enforce the memory limit.
477 *
478 * Returns the size the region should have to enforce the memory limit.
479 * This will either be the original value of size, a truncated value,
480 * or zero. If the returned value of size is 0 the region should be
481 * discarded as it lies wholy above the memory limit.
482 */
45fb6cea
AB
483static unsigned long __init numa_enforce_memory_limit(unsigned long start,
484 unsigned long size)
1da177e4
LT
485{
486 /*
487 * We use lmb_end_of_DRAM() in here instead of memory_limit because
488 * we've already adjusted it for the limit and it takes care of
489 * having memory holes below the limit.
490 */
1da177e4
LT
491
492 if (! memory_limit)
493 return size;
494
495 if (start + size <= lmb_end_of_DRAM())
496 return size;
497
498 if (start >= lmb_end_of_DRAM())
499 return 0;
500
501 return lmb_end_of_DRAM() - start;
502}
503
cf00085d
C
504/*
505 * Reads the counter for a given entry in
506 * linux,drconf-usable-memory property
507 */
508static inline int __init read_usm_ranges(const u32 **usm)
509{
510 /*
511 * For each lmb in ibm,dynamic-memory a corresponding
512 * entry in linux,drconf-usable-memory property contains
513 * a counter followed by that many (base, size) duple.
514 * read the counter from linux,drconf-usable-memory
515 */
516 return read_n_cells(n_mem_size_cells, usm);
517}
518
0204568a
PM
519/*
520 * Extract NUMA information from the ibm,dynamic-reconfiguration-memory
521 * node. This assumes n_mem_{addr,size}_cells have been set.
522 */
523static void __init parse_drconf_memory(struct device_node *memory)
524{
cf00085d
C
525 const u32 *dm, *usm;
526 unsigned int n, rc, ranges, is_kexec_kdump = 0;
527 unsigned long lmb_size, base, size, sz;
8342681d
NF
528 int nid;
529 struct assoc_arrays aa;
530
531 n = of_get_drconf_memory(memory, &dm);
532 if (!n)
0204568a
PM
533 return;
534
8342681d
NF
535 lmb_size = of_get_lmb_size(memory);
536 if (!lmb_size)
537 return;
538
539 rc = of_get_assoc_arrays(memory, &aa);
540 if (rc)
0204568a
PM
541 return;
542
cf00085d
C
543 /* check if this is a kexec/kdump kernel */
544 usm = of_get_usable_memory(memory);
545 if (usm != NULL)
546 is_kexec_kdump = 1;
547
0204568a 548 for (; n != 0; --n) {
8342681d
NF
549 struct of_drconf_cell drmem;
550
551 read_drconf_cell(&drmem, &dm);
552
553 /* skip this block if the reserved bit is set in flags (0x80)
554 or if the block is not assigned to this partition (0x8) */
555 if ((drmem.flags & DRCONF_MEM_RESERVED)
556 || !(drmem.flags & DRCONF_MEM_ASSIGNED))
0204568a 557 continue;
1daa6d08 558
cf00085d
C
559 base = drmem.base_addr;
560 size = lmb_size;
561 ranges = 1;
8342681d 562
cf00085d
C
563 if (is_kexec_kdump) {
564 ranges = read_usm_ranges(&usm);
565 if (!ranges) /* there are no (base, size) duple */
566 continue;
567 }
568 do {
569 if (is_kexec_kdump) {
570 base = read_n_cells(n_mem_addr_cells, &usm);
571 size = read_n_cells(n_mem_size_cells, &usm);
572 }
573 nid = of_drconf_to_nid_single(&drmem, &aa);
574 fake_numa_create_new_node(
575 ((base + size) >> PAGE_SHIFT),
8342681d 576 &nid);
cf00085d
C
577 node_set_online(nid);
578 sz = numa_enforce_memory_limit(base, size);
579 if (sz)
580 add_active_range(nid, base >> PAGE_SHIFT,
581 (base >> PAGE_SHIFT)
582 + (sz >> PAGE_SHIFT));
583 } while (--ranges);
0204568a
PM
584 }
585}
586
1da177e4
LT
587static int __init parse_numa_properties(void)
588{
589 struct device_node *cpu = NULL;
590 struct device_node *memory = NULL;
482ec7c4 591 int default_nid = 0;
1da177e4
LT
592 unsigned long i;
593
594 if (numa_enabled == 0) {
595 printk(KERN_WARNING "NUMA disabled by user\n");
596 return -1;
597 }
598
1da177e4
LT
599 min_common_depth = find_min_common_depth();
600
1da177e4
LT
601 if (min_common_depth < 0)
602 return min_common_depth;
603
bf4b85b0
NL
604 dbg("NUMA associativity depth for CPU/Memory: %d\n", min_common_depth);
605
1da177e4 606 /*
482ec7c4
NL
607 * Even though we connect cpus to numa domains later in SMP
608 * init, we need to know the node ids now. This is because
609 * each node to be onlined must have NODE_DATA etc backing it.
1da177e4 610 */
482ec7c4 611 for_each_present_cpu(i) {
cf950b7a 612 int nid;
1da177e4
LT
613
614 cpu = find_cpu_node(i);
482ec7c4 615 BUG_ON(!cpu);
953039c8 616 nid = of_node_to_nid_single(cpu);
482ec7c4 617 of_node_put(cpu);
1da177e4 618
482ec7c4
NL
619 /*
620 * Don't fall back to default_nid yet -- we will plug
621 * cpus into nodes once the memory scan has discovered
622 * the topology.
623 */
624 if (nid < 0)
625 continue;
626 node_set_online(nid);
1da177e4
LT
627 }
628
237a0989 629 get_n_mem_cells(&n_mem_addr_cells, &n_mem_size_cells);
1da177e4
LT
630 memory = NULL;
631 while ((memory = of_find_node_by_type(memory, "memory")) != NULL) {
632 unsigned long start;
633 unsigned long size;
cf950b7a 634 int nid;
1da177e4 635 int ranges;
a7f67bdf 636 const unsigned int *memcell_buf;
1da177e4
LT
637 unsigned int len;
638
e2eb6392 639 memcell_buf = of_get_property(memory,
ba759485
ME
640 "linux,usable-memory", &len);
641 if (!memcell_buf || len <= 0)
e2eb6392 642 memcell_buf = of_get_property(memory, "reg", &len);
1da177e4
LT
643 if (!memcell_buf || len <= 0)
644 continue;
645
cc5d0189
BH
646 /* ranges in cell */
647 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
1da177e4
LT
648new_range:
649 /* these are order-sensitive, and modify the buffer pointer */
237a0989
MK
650 start = read_n_cells(n_mem_addr_cells, &memcell_buf);
651 size = read_n_cells(n_mem_size_cells, &memcell_buf);
1da177e4 652
482ec7c4
NL
653 /*
654 * Assumption: either all memory nodes or none will
655 * have associativity properties. If none, then
656 * everything goes to default_nid.
657 */
953039c8 658 nid = of_node_to_nid_single(memory);
482ec7c4
NL
659 if (nid < 0)
660 nid = default_nid;
1daa6d08
BS
661
662 fake_numa_create_new_node(((start + size) >> PAGE_SHIFT), &nid);
482ec7c4 663 node_set_online(nid);
1da177e4 664
45fb6cea 665 if (!(size = numa_enforce_memory_limit(start, size))) {
1da177e4
LT
666 if (--ranges)
667 goto new_range;
668 else
669 continue;
670 }
671
c67c3cb4
MG
672 add_active_range(nid, start >> PAGE_SHIFT,
673 (start >> PAGE_SHIFT) + (size >> PAGE_SHIFT));
1da177e4
LT
674
675 if (--ranges)
676 goto new_range;
677 }
678
0204568a
PM
679 /*
680 * Now do the same thing for each LMB listed in the ibm,dynamic-memory
681 * property in the ibm,dynamic-reconfiguration-memory node.
682 */
683 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
684 if (memory)
685 parse_drconf_memory(memory);
686
1da177e4
LT
687 return 0;
688}
689
690static void __init setup_nonnuma(void)
691{
692 unsigned long top_of_ram = lmb_end_of_DRAM();
693 unsigned long total_ram = lmb_phys_mem_size();
c67c3cb4 694 unsigned long start_pfn, end_pfn;
1daa6d08 695 unsigned int i, nid = 0;
1da177e4 696
e110b281 697 printk(KERN_DEBUG "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
1da177e4 698 top_of_ram, total_ram);
e110b281 699 printk(KERN_DEBUG "Memory hole size: %ldMB\n",
1da177e4
LT
700 (top_of_ram - total_ram) >> 20);
701
c67c3cb4
MG
702 for (i = 0; i < lmb.memory.cnt; ++i) {
703 start_pfn = lmb.memory.region[i].base >> PAGE_SHIFT;
704 end_pfn = start_pfn + lmb_size_pages(&lmb.memory, i);
1daa6d08
BS
705
706 fake_numa_create_new_node(end_pfn, &nid);
707 add_active_range(nid, start_pfn, end_pfn);
708 node_set_online(nid);
c67c3cb4 709 }
1da177e4
LT
710}
711
4b703a23
AB
712void __init dump_numa_cpu_topology(void)
713{
714 unsigned int node;
715 unsigned int cpu, count;
716
717 if (min_common_depth == -1 || !numa_enabled)
718 return;
719
720 for_each_online_node(node) {
e110b281 721 printk(KERN_DEBUG "Node %d CPUs:", node);
4b703a23
AB
722
723 count = 0;
724 /*
725 * If we used a CPU iterator here we would miss printing
726 * the holes in the cpumap.
727 */
728 for (cpu = 0; cpu < NR_CPUS; cpu++) {
729 if (cpu_isset(cpu, numa_cpumask_lookup_table[node])) {
730 if (count == 0)
731 printk(" %u", cpu);
732 ++count;
733 } else {
734 if (count > 1)
735 printk("-%u", cpu - 1);
736 count = 0;
737 }
738 }
739
740 if (count > 1)
741 printk("-%u", NR_CPUS - 1);
742 printk("\n");
743 }
744}
745
746static void __init dump_numa_memory_topology(void)
1da177e4
LT
747{
748 unsigned int node;
749 unsigned int count;
750
751 if (min_common_depth == -1 || !numa_enabled)
752 return;
753
754 for_each_online_node(node) {
755 unsigned long i;
756
e110b281 757 printk(KERN_DEBUG "Node %d Memory:", node);
1da177e4
LT
758
759 count = 0;
760
45fb6cea
AB
761 for (i = 0; i < lmb_end_of_DRAM();
762 i += (1 << SECTION_SIZE_BITS)) {
763 if (early_pfn_to_nid(i >> PAGE_SHIFT) == node) {
1da177e4
LT
764 if (count == 0)
765 printk(" 0x%lx", i);
766 ++count;
767 } else {
768 if (count > 0)
769 printk("-0x%lx", i);
770 count = 0;
771 }
772 }
773
774 if (count > 0)
775 printk("-0x%lx", i);
776 printk("\n");
777 }
1da177e4
LT
778}
779
780/*
781 * Allocate some memory, satisfying the lmb or bootmem allocator where
782 * required. nid is the preferred node and end is the physical address of
783 * the highest address in the node.
784 *
785 * Returns the physical address of the memory.
786 */
45fb6cea
AB
787static void __init *careful_allocation(int nid, unsigned long size,
788 unsigned long align,
789 unsigned long end_pfn)
1da177e4 790{
45fb6cea 791 int new_nid;
d7a5b2ff 792 unsigned long ret = __lmb_alloc_base(size, align, end_pfn << PAGE_SHIFT);
1da177e4
LT
793
794 /* retry over all memory */
795 if (!ret)
d7a5b2ff 796 ret = __lmb_alloc_base(size, align, lmb_end_of_DRAM());
1da177e4
LT
797
798 if (!ret)
799 panic("numa.c: cannot allocate %lu bytes on node %d",
800 size, nid);
801
802 /*
803 * If the memory came from a previously allocated node, we must
804 * retry with the bootmem allocator.
805 */
45fb6cea
AB
806 new_nid = early_pfn_to_nid(ret >> PAGE_SHIFT);
807 if (new_nid < nid) {
808 ret = (unsigned long)__alloc_bootmem_node(NODE_DATA(new_nid),
1da177e4
LT
809 size, align, 0);
810
811 if (!ret)
812 panic("numa.c: cannot allocate %lu bytes on node %d",
45fb6cea 813 size, new_nid);
1da177e4 814
45fb6cea 815 ret = __pa(ret);
1da177e4
LT
816
817 dbg("alloc_bootmem %lx %lx\n", ret, size);
818 }
819
45fb6cea 820 return (void *)ret;
1da177e4
LT
821}
822
74b85f37
CS
823static struct notifier_block __cpuinitdata ppc64_numa_nb = {
824 .notifier_call = cpu_numa_callback,
825 .priority = 1 /* Must run before sched domains notifier. */
826};
827
1da177e4
LT
828void __init do_init_bootmem(void)
829{
830 int nid;
45fb6cea 831 unsigned int i;
1da177e4
LT
832
833 min_low_pfn = 0;
834 max_low_pfn = lmb_end_of_DRAM() >> PAGE_SHIFT;
835 max_pfn = max_low_pfn;
836
837 if (parse_numa_properties())
838 setup_nonnuma();
839 else
4b703a23 840 dump_numa_memory_topology();
1da177e4
LT
841
842 register_cpu_notifier(&ppc64_numa_nb);
2b261227
NL
843 cpu_numa_callback(&ppc64_numa_nb, CPU_UP_PREPARE,
844 (void *)(unsigned long)boot_cpuid);
1da177e4
LT
845
846 for_each_online_node(nid) {
c67c3cb4 847 unsigned long start_pfn, end_pfn;
1da177e4
LT
848 unsigned long bootmem_paddr;
849 unsigned long bootmap_pages;
850
c67c3cb4 851 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
1da177e4
LT
852
853 /* Allocate the node structure node local if possible */
45fb6cea 854 NODE_DATA(nid) = careful_allocation(nid,
1da177e4 855 sizeof(struct pglist_data),
45fb6cea
AB
856 SMP_CACHE_BYTES, end_pfn);
857 NODE_DATA(nid) = __va(NODE_DATA(nid));
1da177e4
LT
858 memset(NODE_DATA(nid), 0, sizeof(struct pglist_data));
859
860 dbg("node %d\n", nid);
861 dbg("NODE_DATA() = %p\n", NODE_DATA(nid));
862
b61bfa3c 863 NODE_DATA(nid)->bdata = &bootmem_node_data[nid];
45fb6cea
AB
864 NODE_DATA(nid)->node_start_pfn = start_pfn;
865 NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
1da177e4
LT
866
867 if (NODE_DATA(nid)->node_spanned_pages == 0)
868 continue;
869
45fb6cea
AB
870 dbg("start_paddr = %lx\n", start_pfn << PAGE_SHIFT);
871 dbg("end_paddr = %lx\n", end_pfn << PAGE_SHIFT);
1da177e4 872
45fb6cea
AB
873 bootmap_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
874 bootmem_paddr = (unsigned long)careful_allocation(nid,
875 bootmap_pages << PAGE_SHIFT,
876 PAGE_SIZE, end_pfn);
877 memset(__va(bootmem_paddr), 0, bootmap_pages << PAGE_SHIFT);
1da177e4 878
1da177e4
LT
879 dbg("bootmap_paddr = %lx\n", bootmem_paddr);
880
881 init_bootmem_node(NODE_DATA(nid), bootmem_paddr >> PAGE_SHIFT,
45fb6cea 882 start_pfn, end_pfn);
1da177e4 883
c67c3cb4 884 free_bootmem_with_active_regions(nid, end_pfn);
1da177e4 885
45fb6cea 886 /* Mark reserved regions on this node */
1da177e4 887 for (i = 0; i < lmb.reserved.cnt; i++) {
180379dc 888 unsigned long physbase = lmb.reserved.region[i].base;
1da177e4 889 unsigned long size = lmb.reserved.region[i].size;
45fb6cea
AB
890 unsigned long start_paddr = start_pfn << PAGE_SHIFT;
891 unsigned long end_paddr = end_pfn << PAGE_SHIFT;
1da177e4 892
45fb6cea
AB
893 if (early_pfn_to_nid(physbase >> PAGE_SHIFT) != nid &&
894 early_pfn_to_nid((physbase+size-1) >> PAGE_SHIFT) != nid)
1da177e4
LT
895 continue;
896
897 if (physbase < end_paddr &&
898 (physbase+size) > start_paddr) {
899 /* overlaps */
900 if (physbase < start_paddr) {
901 size -= start_paddr - physbase;
902 physbase = start_paddr;
903 }
904
905 if (size > end_paddr - physbase)
906 size = end_paddr - physbase;
907
908 dbg("reserve_bootmem %lx %lx\n", physbase,
909 size);
910 reserve_bootmem_node(NODE_DATA(nid), physbase,
72a7fe39 911 size, BOOTMEM_DEFAULT);
1da177e4
LT
912 }
913 }
802f192e 914
c67c3cb4 915 sparse_memory_present_with_active_regions(nid);
1da177e4
LT
916 }
917}
918
919void __init paging_init(void)
920{
6391af17
MG
921 unsigned long max_zone_pfns[MAX_NR_ZONES];
922 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
923 max_zone_pfns[ZONE_DMA] = lmb_end_of_DRAM() >> PAGE_SHIFT;
c67c3cb4 924 free_area_init_nodes(max_zone_pfns);
1da177e4
LT
925}
926
927static int __init early_numa(char *p)
928{
929 if (!p)
930 return 0;
931
932 if (strstr(p, "off"))
933 numa_enabled = 0;
934
935 if (strstr(p, "debug"))
936 numa_debug = 1;
937
1daa6d08
BS
938 p = strstr(p, "fake=");
939 if (p)
940 cmdline = p + strlen("fake=");
941
1da177e4
LT
942 return 0;
943}
944early_param("numa", early_numa);
237a0989
MK
945
946#ifdef CONFIG_MEMORY_HOTPLUG
0db9360a
NF
947/*
948 * Validate the node associated with the memory section we are
949 * trying to add.
950 */
951int valid_hot_add_scn(int *nid, unsigned long start, u32 lmb_size,
952 unsigned long scn_addr)
953{
954 nodemask_t nodes;
955
956 if (*nid < 0 || !node_online(*nid))
957 *nid = any_online_node(NODE_MASK_ALL);
958
959 if ((scn_addr >= start) && (scn_addr < (start + lmb_size))) {
960 nodes_setall(nodes);
961 while (NODE_DATA(*nid)->node_spanned_pages == 0) {
962 node_clear(*nid, nodes);
963 *nid = any_online_node(nodes);
964 }
965
966 return 1;
967 }
968
969 return 0;
970}
971
972/*
973 * Find the node associated with a hot added memory section represented
974 * by the ibm,dynamic-reconfiguration-memory node.
975 */
976static int hot_add_drconf_scn_to_nid(struct device_node *memory,
977 unsigned long scn_addr)
978{
979 const u32 *dm;
980 unsigned int n, rc;
981 unsigned long lmb_size;
982 int default_nid = any_online_node(NODE_MASK_ALL);
983 int nid;
984 struct assoc_arrays aa;
985
986 n = of_get_drconf_memory(memory, &dm);
987 if (!n)
988 return default_nid;;
989
990 lmb_size = of_get_lmb_size(memory);
991 if (!lmb_size)
992 return default_nid;
993
994 rc = of_get_assoc_arrays(memory, &aa);
995 if (rc)
996 return default_nid;
997
998 for (; n != 0; --n) {
999 struct of_drconf_cell drmem;
1000
1001 read_drconf_cell(&drmem, &dm);
1002
1003 /* skip this block if it is reserved or not assigned to
1004 * this partition */
1005 if ((drmem.flags & DRCONF_MEM_RESERVED)
1006 || !(drmem.flags & DRCONF_MEM_ASSIGNED))
1007 continue;
1008
1009 nid = of_drconf_to_nid_single(&drmem, &aa);
1010
1011 if (valid_hot_add_scn(&nid, drmem.base_addr, lmb_size,
1012 scn_addr))
1013 return nid;
1014 }
1015
1016 BUG(); /* section address should be found above */
1017 return 0;
1018}
1019
237a0989
MK
1020/*
1021 * Find the node associated with a hot added memory section. Section
1022 * corresponds to a SPARSEMEM section, not an LMB. It is assumed that
1023 * sections are fully contained within a single LMB.
1024 */
1025int hot_add_scn_to_nid(unsigned long scn_addr)
1026{
1027 struct device_node *memory = NULL;
069007ae 1028 int nid;
237a0989
MK
1029
1030 if (!numa_enabled || (min_common_depth < 0))
0db9360a
NF
1031 return any_online_node(NODE_MASK_ALL);
1032
1033 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
1034 if (memory) {
1035 nid = hot_add_drconf_scn_to_nid(memory, scn_addr);
1036 of_node_put(memory);
1037 return nid;
1038 }
237a0989
MK
1039
1040 while ((memory = of_find_node_by_type(memory, "memory")) != NULL) {
1041 unsigned long start, size;
b226e462 1042 int ranges;
a7f67bdf 1043 const unsigned int *memcell_buf;
237a0989
MK
1044 unsigned int len;
1045
e2eb6392 1046 memcell_buf = of_get_property(memory, "reg", &len);
237a0989
MK
1047 if (!memcell_buf || len <= 0)
1048 continue;
1049
cc5d0189
BH
1050 /* ranges in cell */
1051 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
237a0989
MK
1052ha_new_range:
1053 start = read_n_cells(n_mem_addr_cells, &memcell_buf);
1054 size = read_n_cells(n_mem_size_cells, &memcell_buf);
953039c8 1055 nid = of_node_to_nid_single(memory);
237a0989 1056
0db9360a 1057 if (valid_hot_add_scn(&nid, start, size, scn_addr)) {
237a0989 1058 of_node_put(memory);
0db9360a 1059 return nid;
237a0989
MK
1060 }
1061
1062 if (--ranges) /* process all ranges in cell */
1063 goto ha_new_range;
1064 }
237a0989 1065 BUG(); /* section address should be found above */
069007ae 1066 return 0;
237a0989
MK
1067}
1068#endif /* CONFIG_MEMORY_HOTPLUG */