Merge branch 'linux-2.6'
[pandora-kernel.git] / arch / powerpc / mm / numa.c
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>
20 #include <asm/sparsemem.h>
21 #include <asm/lmb.h>
22 #include <asm/system.h>
23 #include <asm/smp.h>
24
25 static int numa_enabled = 1;
26
27 static char *cmdline __initdata;
28
29 static int numa_debug;
30 #define dbg(args...) if (numa_debug) { printk(KERN_INFO args); }
31
32 int numa_cpu_lookup_table[NR_CPUS];
33 cpumask_t numa_cpumask_lookup_table[MAX_NUMNODES];
34 struct pglist_data *node_data[MAX_NUMNODES];
35
36 EXPORT_SYMBOL(numa_cpu_lookup_table);
37 EXPORT_SYMBOL(numa_cpumask_lookup_table);
38 EXPORT_SYMBOL(node_data);
39
40 static bootmem_data_t __initdata plat_node_bdata[MAX_NUMNODES];
41 static int min_common_depth;
42 static int n_mem_addr_cells, n_mem_size_cells;
43
44 static int __cpuinit fake_numa_create_new_node(unsigned long end_pfn,
45                                                 unsigned int *nid)
46 {
47         unsigned long long mem;
48         char *p = cmdline;
49         static unsigned int fake_nid = 0;
50         static unsigned long long curr_boundary = 0;
51
52         *nid = fake_nid;
53         if (!p)
54                 return 0;
55
56         mem = memparse(p, &p);
57         if (!mem)
58                 return 0;
59
60         if (mem < curr_boundary)
61                 return 0;
62
63         curr_boundary = mem;
64
65         if ((end_pfn << PAGE_SHIFT) > mem) {
66                 /*
67                  * Skip commas and spaces
68                  */
69                 while (*p == ',' || *p == ' ' || *p == '\t')
70                         p++;
71
72                 cmdline = p;
73                 fake_nid++;
74                 *nid = fake_nid;
75                 dbg("created new fake_node with id %d\n", fake_nid);
76                 return 1;
77         }
78         return 0;
79 }
80
81 static void __cpuinit map_cpu_to_node(int cpu, int node)
82 {
83         numa_cpu_lookup_table[cpu] = node;
84
85         dbg("adding cpu %d to node %d\n", cpu, node);
86
87         if (!(cpu_isset(cpu, numa_cpumask_lookup_table[node])))
88                 cpu_set(cpu, numa_cpumask_lookup_table[node]);
89 }
90
91 #ifdef CONFIG_HOTPLUG_CPU
92 static void unmap_cpu_from_node(unsigned long cpu)
93 {
94         int node = numa_cpu_lookup_table[cpu];
95
96         dbg("removing cpu %lu from node %d\n", cpu, node);
97
98         if (cpu_isset(cpu, numa_cpumask_lookup_table[node])) {
99                 cpu_clear(cpu, numa_cpumask_lookup_table[node]);
100         } else {
101                 printk(KERN_ERR "WARNING: cpu %lu not found in node %d\n",
102                        cpu, node);
103         }
104 }
105 #endif /* CONFIG_HOTPLUG_CPU */
106
107 static struct device_node * __cpuinit find_cpu_node(unsigned int cpu)
108 {
109         unsigned int hw_cpuid = get_hard_smp_processor_id(cpu);
110         struct device_node *cpu_node = NULL;
111         const unsigned int *interrupt_server, *reg;
112         int len;
113
114         while ((cpu_node = of_find_node_by_type(cpu_node, "cpu")) != NULL) {
115                 /* Try interrupt server first */
116                 interrupt_server = of_get_property(cpu_node,
117                                         "ibm,ppc-interrupt-server#s", &len);
118
119                 len = len / sizeof(u32);
120
121                 if (interrupt_server && (len > 0)) {
122                         while (len--) {
123                                 if (interrupt_server[len] == hw_cpuid)
124                                         return cpu_node;
125                         }
126                 } else {
127                         reg = of_get_property(cpu_node, "reg", &len);
128                         if (reg && (len > 0) && (reg[0] == hw_cpuid))
129                                 return cpu_node;
130                 }
131         }
132
133         return NULL;
134 }
135
136 /* must hold reference to node during call */
137 static const int *of_get_associativity(struct device_node *dev)
138 {
139         return of_get_property(dev, "ibm,associativity", NULL);
140 }
141
142 /* Returns nid in the range [0..MAX_NUMNODES-1], or -1 if no useful numa
143  * info is found.
144  */
145 static int of_node_to_nid_single(struct device_node *device)
146 {
147         int nid = -1;
148         const unsigned int *tmp;
149
150         if (min_common_depth == -1)
151                 goto out;
152
153         tmp = of_get_associativity(device);
154         if (!tmp)
155                 goto out;
156
157         if (tmp[0] >= min_common_depth)
158                 nid = tmp[min_common_depth];
159
160         /* POWER4 LPAR uses 0xffff as invalid node */
161         if (nid == 0xffff || nid >= MAX_NUMNODES)
162                 nid = -1;
163 out:
164         return nid;
165 }
166
167 /* Walk the device tree upwards, looking for an associativity id */
168 int of_node_to_nid(struct device_node *device)
169 {
170         struct device_node *tmp;
171         int nid = -1;
172
173         of_node_get(device);
174         while (device) {
175                 nid = of_node_to_nid_single(device);
176                 if (nid != -1)
177                         break;
178
179                 tmp = device;
180                 device = of_get_parent(tmp);
181                 of_node_put(tmp);
182         }
183         of_node_put(device);
184
185         return nid;
186 }
187 EXPORT_SYMBOL_GPL(of_node_to_nid);
188
189 /*
190  * In theory, the "ibm,associativity" property may contain multiple
191  * associativity lists because a resource may be multiply connected
192  * into the machine.  This resource then has different associativity
193  * characteristics relative to its multiple connections.  We ignore
194  * this for now.  We also assume that all cpu and memory sets have
195  * their distances represented at a common level.  This won't be
196  * true for hierarchical NUMA.
197  *
198  * In any case the ibm,associativity-reference-points should give
199  * the correct depth for a normal NUMA system.
200  *
201  * - Dave Hansen <haveblue@us.ibm.com>
202  */
203 static int __init find_min_common_depth(void)
204 {
205         int depth;
206         const unsigned int *ref_points;
207         struct device_node *rtas_root;
208         unsigned int len;
209
210         rtas_root = of_find_node_by_path("/rtas");
211
212         if (!rtas_root)
213                 return -1;
214
215         /*
216          * this property is 2 32-bit integers, each representing a level of
217          * depth in the associativity nodes.  The first is for an SMP
218          * configuration (should be all 0's) and the second is for a normal
219          * NUMA configuration.
220          */
221         ref_points = of_get_property(rtas_root,
222                         "ibm,associativity-reference-points", &len);
223
224         if ((len >= 1) && ref_points) {
225                 depth = ref_points[1];
226         } else {
227                 dbg("NUMA: ibm,associativity-reference-points not found.\n");
228                 depth = -1;
229         }
230         of_node_put(rtas_root);
231
232         return depth;
233 }
234
235 static void __init get_n_mem_cells(int *n_addr_cells, int *n_size_cells)
236 {
237         struct device_node *memory = NULL;
238
239         memory = of_find_node_by_type(memory, "memory");
240         if (!memory)
241                 panic("numa.c: No memory nodes found!");
242
243         *n_addr_cells = of_n_addr_cells(memory);
244         *n_size_cells = of_n_size_cells(memory);
245         of_node_put(memory);
246 }
247
248 static unsigned long __devinit read_n_cells(int n, const unsigned int **buf)
249 {
250         unsigned long result = 0;
251
252         while (n--) {
253                 result = (result << 32) | **buf;
254                 (*buf)++;
255         }
256         return result;
257 }
258
259 /*
260  * Figure out to which domain a cpu belongs and stick it there.
261  * Return the id of the domain used.
262  */
263 static int __cpuinit numa_setup_cpu(unsigned long lcpu)
264 {
265         int nid = 0;
266         struct device_node *cpu = find_cpu_node(lcpu);
267
268         if (!cpu) {
269                 WARN_ON(1);
270                 goto out;
271         }
272
273         nid = of_node_to_nid_single(cpu);
274
275         if (nid < 0 || !node_online(nid))
276                 nid = any_online_node(NODE_MASK_ALL);
277 out:
278         map_cpu_to_node(lcpu, nid);
279
280         of_node_put(cpu);
281
282         return nid;
283 }
284
285 static int __cpuinit cpu_numa_callback(struct notifier_block *nfb,
286                              unsigned long action,
287                              void *hcpu)
288 {
289         unsigned long lcpu = (unsigned long)hcpu;
290         int ret = NOTIFY_DONE;
291
292         switch (action) {
293         case CPU_UP_PREPARE:
294         case CPU_UP_PREPARE_FROZEN:
295                 numa_setup_cpu(lcpu);
296                 ret = NOTIFY_OK;
297                 break;
298 #ifdef CONFIG_HOTPLUG_CPU
299         case CPU_DEAD:
300         case CPU_DEAD_FROZEN:
301         case CPU_UP_CANCELED:
302         case CPU_UP_CANCELED_FROZEN:
303                 unmap_cpu_from_node(lcpu);
304                 break;
305                 ret = NOTIFY_OK;
306 #endif
307         }
308         return ret;
309 }
310
311 /*
312  * Check and possibly modify a memory region to enforce the memory limit.
313  *
314  * Returns the size the region should have to enforce the memory limit.
315  * This will either be the original value of size, a truncated value,
316  * or zero. If the returned value of size is 0 the region should be
317  * discarded as it lies wholy above the memory limit.
318  */
319 static unsigned long __init numa_enforce_memory_limit(unsigned long start,
320                                                       unsigned long size)
321 {
322         /*
323          * We use lmb_end_of_DRAM() in here instead of memory_limit because
324          * we've already adjusted it for the limit and it takes care of
325          * having memory holes below the limit.
326          */
327
328         if (! memory_limit)
329                 return size;
330
331         if (start + size <= lmb_end_of_DRAM())
332                 return size;
333
334         if (start >= lmb_end_of_DRAM())
335                 return 0;
336
337         return lmb_end_of_DRAM() - start;
338 }
339
340 /*
341  * Extract NUMA information from the ibm,dynamic-reconfiguration-memory
342  * node.  This assumes n_mem_{addr,size}_cells have been set.
343  */
344 static void __init parse_drconf_memory(struct device_node *memory)
345 {
346         const unsigned int *lm, *dm, *aa;
347         unsigned int ls, ld, la;
348         unsigned int n, aam, aalen;
349         unsigned long lmb_size, size, start;
350         int nid, default_nid = 0;
351         unsigned int ai, flags;
352
353         lm = of_get_property(memory, "ibm,lmb-size", &ls);
354         dm = of_get_property(memory, "ibm,dynamic-memory", &ld);
355         aa = of_get_property(memory, "ibm,associativity-lookup-arrays", &la);
356         if (!lm || !dm || !aa ||
357             ls < sizeof(unsigned int) || ld < sizeof(unsigned int) ||
358             la < 2 * sizeof(unsigned int))
359                 return;
360
361         lmb_size = read_n_cells(n_mem_size_cells, &lm);
362         n = *dm++;              /* number of LMBs */
363         aam = *aa++;            /* number of associativity lists */
364         aalen = *aa++;          /* length of each associativity list */
365         if (ld < (n * (n_mem_addr_cells + 4) + 1) * sizeof(unsigned int) ||
366             la < (aam * aalen + 2) * sizeof(unsigned int))
367                 return;
368
369         for (; n != 0; --n) {
370                 start = read_n_cells(n_mem_addr_cells, &dm);
371                 ai = dm[2];
372                 flags = dm[3];
373                 dm += 4;
374                 /* 0x80 == reserved, 0x8 = assigned to us */
375                 if ((flags & 0x80) || !(flags & 0x8))
376                         continue;
377                 nid = default_nid;
378                 /* flags & 0x40 means associativity index is invalid */
379                 if (min_common_depth > 0 && min_common_depth <= aalen &&
380                     (flags & 0x40) == 0 && ai < aam) {
381                         /* this is like of_node_to_nid_single */
382                         nid = aa[ai * aalen + min_common_depth - 1];
383                         if (nid == 0xffff || nid >= MAX_NUMNODES)
384                                 nid = default_nid;
385                 }
386
387                 size = numa_enforce_memory_limit(start, lmb_size);
388                 if (!size)
389                         continue;
390
391                 fake_numa_create_new_node(((start + size) >> PAGE_SHIFT), &nid);
392                 node_set_online(nid);
393
394                 add_active_range(nid, start >> PAGE_SHIFT,
395                                  (start >> PAGE_SHIFT) + (size >> PAGE_SHIFT));
396         }
397 }
398
399 static int __init parse_numa_properties(void)
400 {
401         struct device_node *cpu = NULL;
402         struct device_node *memory = NULL;
403         int default_nid = 0;
404         unsigned long i;
405
406         if (numa_enabled == 0) {
407                 printk(KERN_WARNING "NUMA disabled by user\n");
408                 return -1;
409         }
410
411         min_common_depth = find_min_common_depth();
412
413         if (min_common_depth < 0)
414                 return min_common_depth;
415
416         dbg("NUMA associativity depth for CPU/Memory: %d\n", min_common_depth);
417
418         /*
419          * Even though we connect cpus to numa domains later in SMP
420          * init, we need to know the node ids now. This is because
421          * each node to be onlined must have NODE_DATA etc backing it.
422          */
423         for_each_present_cpu(i) {
424                 int nid;
425
426                 cpu = find_cpu_node(i);
427                 BUG_ON(!cpu);
428                 nid = of_node_to_nid_single(cpu);
429                 of_node_put(cpu);
430
431                 /*
432                  * Don't fall back to default_nid yet -- we will plug
433                  * cpus into nodes once the memory scan has discovered
434                  * the topology.
435                  */
436                 if (nid < 0)
437                         continue;
438                 node_set_online(nid);
439         }
440
441         get_n_mem_cells(&n_mem_addr_cells, &n_mem_size_cells);
442         memory = NULL;
443         while ((memory = of_find_node_by_type(memory, "memory")) != NULL) {
444                 unsigned long start;
445                 unsigned long size;
446                 int nid;
447                 int ranges;
448                 const unsigned int *memcell_buf;
449                 unsigned int len;
450
451                 memcell_buf = of_get_property(memory,
452                         "linux,usable-memory", &len);
453                 if (!memcell_buf || len <= 0)
454                         memcell_buf = of_get_property(memory, "reg", &len);
455                 if (!memcell_buf || len <= 0)
456                         continue;
457
458                 /* ranges in cell */
459                 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
460 new_range:
461                 /* these are order-sensitive, and modify the buffer pointer */
462                 start = read_n_cells(n_mem_addr_cells, &memcell_buf);
463                 size = read_n_cells(n_mem_size_cells, &memcell_buf);
464
465                 /*
466                  * Assumption: either all memory nodes or none will
467                  * have associativity properties.  If none, then
468                  * everything goes to default_nid.
469                  */
470                 nid = of_node_to_nid_single(memory);
471                 if (nid < 0)
472                         nid = default_nid;
473
474                 if (!(size = numa_enforce_memory_limit(start, size))) {
475                         if (--ranges)
476                                 goto new_range;
477                         else
478                                 continue;
479                 }
480
481                 fake_numa_create_new_node(((start + size) >> PAGE_SHIFT), &nid);
482                 node_set_online(nid);
483
484                 add_active_range(nid, start >> PAGE_SHIFT,
485                                 (start >> PAGE_SHIFT) + (size >> PAGE_SHIFT));
486
487                 if (--ranges)
488                         goto new_range;
489         }
490
491         /*
492          * Now do the same thing for each LMB listed in the ibm,dynamic-memory
493          * property in the ibm,dynamic-reconfiguration-memory node.
494          */
495         memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
496         if (memory)
497                 parse_drconf_memory(memory);
498
499         return 0;
500 }
501
502 static void __init setup_nonnuma(void)
503 {
504         unsigned long top_of_ram = lmb_end_of_DRAM();
505         unsigned long total_ram = lmb_phys_mem_size();
506         unsigned long start_pfn, end_pfn;
507         unsigned int i, nid = 0;
508
509         printk(KERN_DEBUG "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
510                top_of_ram, total_ram);
511         printk(KERN_DEBUG "Memory hole size: %ldMB\n",
512                (top_of_ram - total_ram) >> 20);
513
514         for (i = 0; i < lmb.memory.cnt; ++i) {
515                 start_pfn = lmb.memory.region[i].base >> PAGE_SHIFT;
516                 end_pfn = start_pfn + lmb_size_pages(&lmb.memory, i);
517
518                 fake_numa_create_new_node(end_pfn, &nid);
519                 add_active_range(nid, start_pfn, end_pfn);
520                 node_set_online(nid);
521         }
522 }
523
524 void __init dump_numa_cpu_topology(void)
525 {
526         unsigned int node;
527         unsigned int cpu, count;
528
529         if (min_common_depth == -1 || !numa_enabled)
530                 return;
531
532         for_each_online_node(node) {
533                 printk(KERN_DEBUG "Node %d CPUs:", node);
534
535                 count = 0;
536                 /*
537                  * If we used a CPU iterator here we would miss printing
538                  * the holes in the cpumap.
539                  */
540                 for (cpu = 0; cpu < NR_CPUS; cpu++) {
541                         if (cpu_isset(cpu, numa_cpumask_lookup_table[node])) {
542                                 if (count == 0)
543                                         printk(" %u", cpu);
544                                 ++count;
545                         } else {
546                                 if (count > 1)
547                                         printk("-%u", cpu - 1);
548                                 count = 0;
549                         }
550                 }
551
552                 if (count > 1)
553                         printk("-%u", NR_CPUS - 1);
554                 printk("\n");
555         }
556 }
557
558 static void __init dump_numa_memory_topology(void)
559 {
560         unsigned int node;
561         unsigned int count;
562
563         if (min_common_depth == -1 || !numa_enabled)
564                 return;
565
566         for_each_online_node(node) {
567                 unsigned long i;
568
569                 printk(KERN_DEBUG "Node %d Memory:", node);
570
571                 count = 0;
572
573                 for (i = 0; i < lmb_end_of_DRAM();
574                      i += (1 << SECTION_SIZE_BITS)) {
575                         if (early_pfn_to_nid(i >> PAGE_SHIFT) == node) {
576                                 if (count == 0)
577                                         printk(" 0x%lx", i);
578                                 ++count;
579                         } else {
580                                 if (count > 0)
581                                         printk("-0x%lx", i);
582                                 count = 0;
583                         }
584                 }
585
586                 if (count > 0)
587                         printk("-0x%lx", i);
588                 printk("\n");
589         }
590 }
591
592 /*
593  * Allocate some memory, satisfying the lmb or bootmem allocator where
594  * required. nid is the preferred node and end is the physical address of
595  * the highest address in the node.
596  *
597  * Returns the physical address of the memory.
598  */
599 static void __init *careful_allocation(int nid, unsigned long size,
600                                        unsigned long align,
601                                        unsigned long end_pfn)
602 {
603         int new_nid;
604         unsigned long ret = __lmb_alloc_base(size, align, end_pfn << PAGE_SHIFT);
605
606         /* retry over all memory */
607         if (!ret)
608                 ret = __lmb_alloc_base(size, align, lmb_end_of_DRAM());
609
610         if (!ret)
611                 panic("numa.c: cannot allocate %lu bytes on node %d",
612                       size, nid);
613
614         /*
615          * If the memory came from a previously allocated node, we must
616          * retry with the bootmem allocator.
617          */
618         new_nid = early_pfn_to_nid(ret >> PAGE_SHIFT);
619         if (new_nid < nid) {
620                 ret = (unsigned long)__alloc_bootmem_node(NODE_DATA(new_nid),
621                                 size, align, 0);
622
623                 if (!ret)
624                         panic("numa.c: cannot allocate %lu bytes on node %d",
625                               size, new_nid);
626
627                 ret = __pa(ret);
628
629                 dbg("alloc_bootmem %lx %lx\n", ret, size);
630         }
631
632         return (void *)ret;
633 }
634
635 static struct notifier_block __cpuinitdata ppc64_numa_nb = {
636         .notifier_call = cpu_numa_callback,
637         .priority = 1 /* Must run before sched domains notifier. */
638 };
639
640 void __init do_init_bootmem(void)
641 {
642         int nid;
643         unsigned int i;
644
645         min_low_pfn = 0;
646         max_low_pfn = lmb_end_of_DRAM() >> PAGE_SHIFT;
647         max_pfn = max_low_pfn;
648
649         if (parse_numa_properties())
650                 setup_nonnuma();
651         else
652                 dump_numa_memory_topology();
653
654         register_cpu_notifier(&ppc64_numa_nb);
655         cpu_numa_callback(&ppc64_numa_nb, CPU_UP_PREPARE,
656                           (void *)(unsigned long)boot_cpuid);
657
658         for_each_online_node(nid) {
659                 unsigned long start_pfn, end_pfn;
660                 unsigned long bootmem_paddr;
661                 unsigned long bootmap_pages;
662
663                 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
664
665                 /* Allocate the node structure node local if possible */
666                 NODE_DATA(nid) = careful_allocation(nid,
667                                         sizeof(struct pglist_data),
668                                         SMP_CACHE_BYTES, end_pfn);
669                 NODE_DATA(nid) = __va(NODE_DATA(nid));
670                 memset(NODE_DATA(nid), 0, sizeof(struct pglist_data));
671
672                 dbg("node %d\n", nid);
673                 dbg("NODE_DATA() = %p\n", NODE_DATA(nid));
674
675                 NODE_DATA(nid)->bdata = &plat_node_bdata[nid];
676                 NODE_DATA(nid)->node_start_pfn = start_pfn;
677                 NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
678
679                 if (NODE_DATA(nid)->node_spanned_pages == 0)
680                         continue;
681
682                 dbg("start_paddr = %lx\n", start_pfn << PAGE_SHIFT);
683                 dbg("end_paddr = %lx\n", end_pfn << PAGE_SHIFT);
684
685                 bootmap_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
686                 bootmem_paddr = (unsigned long)careful_allocation(nid,
687                                         bootmap_pages << PAGE_SHIFT,
688                                         PAGE_SIZE, end_pfn);
689                 memset(__va(bootmem_paddr), 0, bootmap_pages << PAGE_SHIFT);
690
691                 dbg("bootmap_paddr = %lx\n", bootmem_paddr);
692
693                 init_bootmem_node(NODE_DATA(nid), bootmem_paddr >> PAGE_SHIFT,
694                                   start_pfn, end_pfn);
695
696                 free_bootmem_with_active_regions(nid, end_pfn);
697
698                 /* Mark reserved regions on this node */
699                 for (i = 0; i < lmb.reserved.cnt; i++) {
700                         unsigned long physbase = lmb.reserved.region[i].base;
701                         unsigned long size = lmb.reserved.region[i].size;
702                         unsigned long start_paddr = start_pfn << PAGE_SHIFT;
703                         unsigned long end_paddr = end_pfn << PAGE_SHIFT;
704
705                         if (early_pfn_to_nid(physbase >> PAGE_SHIFT) != nid &&
706                             early_pfn_to_nid((physbase+size-1) >> PAGE_SHIFT) != nid)
707                                 continue;
708
709                         if (physbase < end_paddr &&
710                             (physbase+size) > start_paddr) {
711                                 /* overlaps */
712                                 if (physbase < start_paddr) {
713                                         size -= start_paddr - physbase;
714                                         physbase = start_paddr;
715                                 }
716
717                                 if (size > end_paddr - physbase)
718                                         size = end_paddr - physbase;
719
720                                 dbg("reserve_bootmem %lx %lx\n", physbase,
721                                     size);
722                                 reserve_bootmem_node(NODE_DATA(nid), physbase,
723                                                      size);
724                         }
725                 }
726
727                 sparse_memory_present_with_active_regions(nid);
728         }
729 }
730
731 void __init paging_init(void)
732 {
733         unsigned long max_zone_pfns[MAX_NR_ZONES];
734         memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
735         max_zone_pfns[ZONE_DMA] = lmb_end_of_DRAM() >> PAGE_SHIFT;
736         free_area_init_nodes(max_zone_pfns);
737 }
738
739 static int __init early_numa(char *p)
740 {
741         if (!p)
742                 return 0;
743
744         if (strstr(p, "off"))
745                 numa_enabled = 0;
746
747         if (strstr(p, "debug"))
748                 numa_debug = 1;
749
750         p = strstr(p, "fake=");
751         if (p)
752                 cmdline = p + strlen("fake=");
753
754         return 0;
755 }
756 early_param("numa", early_numa);
757
758 #ifdef CONFIG_MEMORY_HOTPLUG
759 /*
760  * Find the node associated with a hot added memory section.  Section
761  * corresponds to a SPARSEMEM section, not an LMB.  It is assumed that
762  * sections are fully contained within a single LMB.
763  */
764 int hot_add_scn_to_nid(unsigned long scn_addr)
765 {
766         struct device_node *memory = NULL;
767         nodemask_t nodes;
768         int default_nid = any_online_node(NODE_MASK_ALL);
769         int nid;
770
771         if (!numa_enabled || (min_common_depth < 0))
772                 return default_nid;
773
774         while ((memory = of_find_node_by_type(memory, "memory")) != NULL) {
775                 unsigned long start, size;
776                 int ranges;
777                 const unsigned int *memcell_buf;
778                 unsigned int len;
779
780                 memcell_buf = of_get_property(memory, "reg", &len);
781                 if (!memcell_buf || len <= 0)
782                         continue;
783
784                 /* ranges in cell */
785                 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
786 ha_new_range:
787                 start = read_n_cells(n_mem_addr_cells, &memcell_buf);
788                 size = read_n_cells(n_mem_size_cells, &memcell_buf);
789                 nid = of_node_to_nid_single(memory);
790
791                 /* Domains not present at boot default to 0 */
792                 if (nid < 0 || !node_online(nid))
793                         nid = default_nid;
794
795                 if ((scn_addr >= start) && (scn_addr < (start + size))) {
796                         of_node_put(memory);
797                         goto got_nid;
798                 }
799
800                 if (--ranges)           /* process all ranges in cell */
801                         goto ha_new_range;
802         }
803         BUG();  /* section address should be found above */
804         return 0;
805
806         /* Temporary code to ensure that returned node is not empty */
807 got_nid:
808         nodes_setall(nodes);
809         while (NODE_DATA(nid)->node_spanned_pages == 0) {
810                 node_clear(nid, nodes);
811                 nid = any_online_node(nodes);
812         }
813         return nid;
814 }
815 #endif /* CONFIG_MEMORY_HOTPLUG */