blob: be05457631d4edd6834073955df15b541ba26e90 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
David S. Millerd9b2b2a2008-02-13 16:56:49 -080020#include <linux/lmb.h>
Michael Ellerman6df16462008-02-14 11:37:49 +110021#include <linux/of.h>
Anton Blanchard45fb6ce2005-11-11 14:22:35 +110022#include <asm/sparsemem.h>
David S. Millerd9b2b2a2008-02-13 16:56:49 -080023#include <asm/prom.h>
Paul Mackerrascf00a8d2005-10-31 13:07:02 +110024#include <asm/system.h>
Paul Mackerras2249ca92005-11-07 13:18:13 +110025#include <asm/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27static int numa_enabled = 1;
28
Balbir Singh1daa6d02008-02-01 15:57:31 +110029static char *cmdline __initdata;
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static int numa_debug;
32#define dbg(args...) if (numa_debug) { printk(KERN_INFO args); }
33
Anton Blanchard45fb6ce2005-11-11 14:22:35 +110034int numa_cpu_lookup_table[NR_CPUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070035cpumask_t numa_cpumask_lookup_table[MAX_NUMNODES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070036struct pglist_data *node_data[MAX_NUMNODES];
Anton Blanchard45fb6ce2005-11-11 14:22:35 +110037
38EXPORT_SYMBOL(numa_cpu_lookup_table);
39EXPORT_SYMBOL(numa_cpumask_lookup_table);
40EXPORT_SYMBOL(node_data);
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static int min_common_depth;
Mike Kravetz237a0982005-12-05 12:06:42 -080043static int n_mem_addr_cells, n_mem_size_cells;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Balbir Singh1daa6d02008-02-01 15:57:31 +110045static 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
Nathan Lynch2e5ce392006-03-20 18:35:15 -060092static void __cpuinit map_cpu_to_node(int cpu, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 numa_cpu_lookup_table[cpu] = node;
Anton Blanchard45fb6ce2005-11-11 14:22:35 +110095
Nathan Lynchbf4b85b2006-03-20 18:34:45 -060096 dbg("adding cpu %d to node %d\n", cpu, node);
97
Anton Blanchard45fb6ce2005-11-11 14:22:35 +110098 if (!(cpu_isset(cpu, numa_cpumask_lookup_table[node])))
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 cpu_set(cpu, numa_cpumask_lookup_table[node]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
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]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 } 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
Nathan Lynch2e5ce392006-03-20 18:35:15 -0600118static struct device_node * __cpuinit find_cpu_node(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 unsigned int hw_cpuid = get_hard_smp_processor_id(cpu);
121 struct device_node *cpu_node = NULL;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000122 const unsigned int *interrupt_server, *reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 int len;
124
125 while ((cpu_node = of_find_node_by_type(cpu_node, "cpu")) != NULL) {
126 /* Try interrupt server first */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000127 interrupt_server = of_get_property(cpu_node,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 "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 {
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000138 reg = of_get_property(cpu_node, "reg", &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 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 */
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000148static const int *of_get_associativity(struct device_node *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000150 return of_get_property(dev, "ibm,associativity", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Chandrucf000852008-08-30 00:28:16 +1000153/*
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
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600168/* Returns nid in the range [0..MAX_NUMNODES-1], or -1 if no useful numa
169 * info is found.
170 */
Jeremy Kerr953039c2006-05-01 12:16:12 -0700171static int of_node_to_nid_single(struct device_node *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600173 int nid = -1;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000174 const unsigned int *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 if (min_common_depth == -1)
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600177 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 tmp = of_get_associativity(device);
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600180 if (!tmp)
181 goto out;
182
183 if (tmp[0] >= min_common_depth)
Nathan Lynchcf950b72006-03-20 18:35:45 -0600184 nid = tmp[min_common_depth];
Nathan Lynchbc16a752006-03-20 18:36:15 -0600185
186 /* POWER4 LPAR uses 0xffff as invalid node */
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600187 if (nid == 0xffff || nid >= MAX_NUMNODES)
188 nid = -1;
189out:
Nathan Lynchcf950b72006-03-20 18:35:45 -0600190 return nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Jeremy Kerr953039c2006-05-01 12:16:12 -0700193/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215/*
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
Uwe Kleine-König1b3c3712007-02-17 19:23:03 +0100222 * true for hierarchical NUMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 *
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;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000232 const unsigned int *ref_points;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 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 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000247 ref_points = of_get_property(rtas_root,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 "ibm,associativity-reference-points", &len);
249
250 if ((len >= 1) && ref_points) {
251 depth = ref_points[1];
252 } else {
Nathan Lynchbf4b85b2006-03-20 18:34:45 -0600253 dbg("NUMA: ibm,associativity-reference-points not found.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 depth = -1;
255 }
256 of_node_put(rtas_root);
257
258 return depth;
259}
260
Mike Kravetz84c9fdd2005-11-30 13:47:23 -0800261static void __init get_n_mem_cells(int *n_addr_cells, int *n_size_cells)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 struct device_node *memory = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 memory = of_find_node_by_type(memory, "memory");
Paul Mackerras54c23312005-12-05 15:50:39 +1100266 if (!memory)
Mike Kravetz84c9fdd2005-11-30 13:47:23 -0800267 panic("numa.c: No memory nodes found!");
Paul Mackerras54c23312005-12-05 15:50:39 +1100268
Stephen Rothwella8bda5d2007-04-03 10:56:50 +1000269 *n_addr_cells = of_n_addr_cells(memory);
Stephen Rothwell9213fee2007-04-03 10:57:48 +1000270 *n_size_cells = of_n_size_cells(memory);
Mike Kravetz84c9fdd2005-11-30 13:47:23 -0800271 of_node_put(memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000274static unsigned long __devinit read_n_cells(int n, const unsigned int **buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 unsigned long result = 0;
277
278 while (n--) {
279 result = (result << 32) | **buf;
280 (*buf)++;
281 }
282 return result;
283}
284
Nathan Fontenot83426812008-07-03 13:35:54 +1000285struct 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423/*
424 * Figure out to which domain a cpu belongs and stick it there.
425 * Return the id of the domain used.
426 */
Nathan Lynch2e5ce392006-03-20 18:35:15 -0600427static int __cpuinit numa_setup_cpu(unsigned long lcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Nathan Lynchcf950b72006-03-20 18:35:45 -0600429 int nid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct device_node *cpu = find_cpu_node(lcpu);
431
432 if (!cpu) {
433 WARN_ON(1);
434 goto out;
435 }
436
Jeremy Kerr953039c2006-05-01 12:16:12 -0700437 nid = of_node_to_nid_single(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600439 if (nid < 0 || !node_online(nid))
440 nid = any_online_node(NODE_MASK_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441out:
Nathan Lynchcf950b72006-03-20 18:35:45 -0600442 map_cpu_to_node(lcpu, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 of_node_put(cpu);
445
Nathan Lynchcf950b72006-03-20 18:35:45 -0600446 return nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
Chandra Seetharaman74b85f32006-06-27 02:54:09 -0700449static int __cpuinit cpu_numa_callback(struct notifier_block *nfb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 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:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700458 case CPU_UP_PREPARE_FROZEN:
Nathan Lynch2b261222006-03-20 18:37:15 -0600459 numa_setup_cpu(lcpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 ret = NOTIFY_OK;
461 break;
462#ifdef CONFIG_HOTPLUG_CPU
463 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700464 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700466 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 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 */
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100483static unsigned long __init numa_enforce_memory_limit(unsigned long start,
484 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
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
Paul Mackerras02045682006-11-29 22:27:42 +1100504/*
Chandrucf000852008-08-30 00:28:16 +1000505 * 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
519/*
Paul Mackerras02045682006-11-29 22:27:42 +1100520 * 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{
Chandrucf000852008-08-30 00:28:16 +1000525 const u32 *dm, *usm;
526 unsigned int n, rc, ranges, is_kexec_kdump = 0;
527 unsigned long lmb_size, base, size, sz;
Nathan Fontenot83426812008-07-03 13:35:54 +1000528 int nid;
529 struct assoc_arrays aa;
Paul Mackerras02045682006-11-29 22:27:42 +1100530
Nathan Fontenot83426812008-07-03 13:35:54 +1000531 n = of_get_drconf_memory(memory, &dm);
532 if (!n)
Paul Mackerras02045682006-11-29 22:27:42 +1100533 return;
534
Nathan Fontenot83426812008-07-03 13:35:54 +1000535 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)
Paul Mackerras02045682006-11-29 22:27:42 +1100541 return;
542
Chandrucf000852008-08-30 00:28:16 +1000543 /* 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
Paul Mackerras02045682006-11-29 22:27:42 +1100548 for (; n != 0; --n) {
Nathan Fontenot83426812008-07-03 13:35:54 +1000549 struct of_drconf_cell drmem;
Balbir Singh1daa6d02008-02-01 15:57:31 +1100550
Nathan Fontenot83426812008-07-03 13:35:54 +1000551 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))
557 continue;
558
Chandrucf000852008-08-30 00:28:16 +1000559 base = drmem.base_addr;
560 size = lmb_size;
561 ranges = 1;
Nathan Fontenot83426812008-07-03 13:35:54 +1000562
Chandrucf000852008-08-30 00:28:16 +1000563 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),
Nathan Fontenot83426812008-07-03 13:35:54 +1000576 &nid);
Chandrucf000852008-08-30 00:28:16 +1000577 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);
Paul Mackerras02045682006-11-29 22:27:42 +1100584 }
585}
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587static int __init parse_numa_properties(void)
588{
589 struct device_node *cpu = NULL;
590 struct device_node *memory = NULL;
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600591 int default_nid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 unsigned long i;
593
594 if (numa_enabled == 0) {
595 printk(KERN_WARNING "NUMA disabled by user\n");
596 return -1;
597 }
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 min_common_depth = find_min_common_depth();
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (min_common_depth < 0)
602 return min_common_depth;
603
Nathan Lynchbf4b85b2006-03-20 18:34:45 -0600604 dbg("NUMA associativity depth for CPU/Memory: %d\n", min_common_depth);
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /*
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600607 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 */
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600611 for_each_present_cpu(i) {
Nathan Lynchcf950b72006-03-20 18:35:45 -0600612 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 cpu = find_cpu_node(i);
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600615 BUG_ON(!cpu);
Jeremy Kerr953039c2006-05-01 12:16:12 -0700616 nid = of_node_to_nid_single(cpu);
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600617 of_node_put(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600619 /*
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628
Mike Kravetz237a0982005-12-05 12:06:42 -0800629 get_n_mem_cells(&n_mem_addr_cells, &n_mem_size_cells);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 memory = NULL;
631 while ((memory = of_find_node_by_type(memory, "memory")) != NULL) {
632 unsigned long start;
633 unsigned long size;
Nathan Lynchcf950b72006-03-20 18:35:45 -0600634 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 int ranges;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000636 const unsigned int *memcell_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 unsigned int len;
638
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000639 memcell_buf = of_get_property(memory,
Michael Ellermanba759482005-12-04 18:39:55 +1100640 "linux,usable-memory", &len);
641 if (!memcell_buf || len <= 0)
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000642 memcell_buf = of_get_property(memory, "reg", &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (!memcell_buf || len <= 0)
644 continue;
645
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100646 /* ranges in cell */
647 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648new_range:
649 /* these are order-sensitive, and modify the buffer pointer */
Mike Kravetz237a0982005-12-05 12:06:42 -0800650 start = read_n_cells(n_mem_addr_cells, &memcell_buf);
651 size = read_n_cells(n_mem_size_cells, &memcell_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600653 /*
654 * Assumption: either all memory nodes or none will
655 * have associativity properties. If none, then
656 * everything goes to default_nid.
657 */
Jeremy Kerr953039c2006-05-01 12:16:12 -0700658 nid = of_node_to_nid_single(memory);
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600659 if (nid < 0)
660 nid = default_nid;
Balbir Singh1daa6d02008-02-01 15:57:31 +1100661
662 fake_numa_create_new_node(((start + size) >> PAGE_SHIFT), &nid);
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600663 node_set_online(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100665 if (!(size = numa_enforce_memory_limit(start, size))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (--ranges)
667 goto new_range;
668 else
669 continue;
670 }
671
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700672 add_active_range(nid, start >> PAGE_SHIFT,
673 (start >> PAGE_SHIFT) + (size >> PAGE_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 if (--ranges)
676 goto new_range;
677 }
678
Paul Mackerras02045682006-11-29 22:27:42 +1100679 /*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 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();
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700694 unsigned long start_pfn, end_pfn;
Balbir Singh1daa6d02008-02-01 15:57:31 +1100695 unsigned int i, nid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Olof Johanssone110b282006-04-12 15:25:01 -0500697 printk(KERN_DEBUG "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 top_of_ram, total_ram);
Olof Johanssone110b282006-04-12 15:25:01 -0500699 printk(KERN_DEBUG "Memory hole size: %ldMB\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 (top_of_ram - total_ram) >> 20);
701
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700702 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);
Balbir Singh1daa6d02008-02-01 15:57:31 +1100705
706 fake_numa_create_new_node(end_pfn, &nid);
707 add_active_range(nid, start_pfn, end_pfn);
708 node_set_online(nid);
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
Anton Blanchard4b703a22005-12-13 06:56:47 +1100712void __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) {
Olof Johanssone110b282006-04-12 15:25:01 -0500721 printk(KERN_DEBUG "Node %d CPUs:", node);
Anton Blanchard4b703a22005-12-13 06:56:47 +1100722
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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
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
Olof Johanssone110b282006-04-12 15:25:01 -0500757 printk(KERN_DEBUG "Node %d Memory:", node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 count = 0;
760
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100761 for (i = 0; i < lmb_end_of_DRAM();
762 i += (1 << SECTION_SIZE_BITS)) {
763 if (early_pfn_to_nid(i >> PAGE_SHIFT) == node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
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 */
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100787static void __init *careful_allocation(int nid, unsigned long size,
788 unsigned long align,
789 unsigned long end_pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100791 int new_nid;
Michael Ellermand7a5b2f2006-01-25 21:31:28 +1300792 unsigned long ret = __lmb_alloc_base(size, align, end_pfn << PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 /* retry over all memory */
795 if (!ret)
Michael Ellermand7a5b2f2006-01-25 21:31:28 +1300796 ret = __lmb_alloc_base(size, align, lmb_end_of_DRAM());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
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 */
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100806 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),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 size, align, 0);
810
811 if (!ret)
812 panic("numa.c: cannot allocate %lu bytes on node %d",
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100813 size, new_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100815 ret = __pa(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 dbg("alloc_bootmem %lx %lx\n", ret, size);
818 }
819
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100820 return (void *)ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
Chandra Seetharaman74b85f32006-06-27 02:54:09 -0700823static struct notifier_block __cpuinitdata ppc64_numa_nb = {
824 .notifier_call = cpu_numa_callback,
825 .priority = 1 /* Must run before sched domains notifier. */
826};
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828void __init do_init_bootmem(void)
829{
830 int nid;
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100831 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
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
Anton Blanchard4b703a22005-12-13 06:56:47 +1100840 dump_numa_memory_topology();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 register_cpu_notifier(&ppc64_numa_nb);
Nathan Lynch2b261222006-03-20 18:37:15 -0600843 cpu_numa_callback(&ppc64_numa_nb, CPU_UP_PREPARE,
844 (void *)(unsigned long)boot_cpuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 for_each_online_node(nid) {
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700847 unsigned long start_pfn, end_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 unsigned long bootmem_paddr;
849 unsigned long bootmap_pages;
850
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700851 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 /* Allocate the node structure node local if possible */
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100854 NODE_DATA(nid) = careful_allocation(nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 sizeof(struct pglist_data),
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100856 SMP_CACHE_BYTES, end_pfn);
857 NODE_DATA(nid) = __va(NODE_DATA(nid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 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
Johannes Weinerb61bfa32008-07-23 21:26:55 -0700863 NODE_DATA(nid)->bdata = &bootmem_node_data[nid];
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100864 NODE_DATA(nid)->node_start_pfn = start_pfn;
865 NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 if (NODE_DATA(nid)->node_spanned_pages == 0)
868 continue;
869
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100870 dbg("start_paddr = %lx\n", start_pfn << PAGE_SHIFT);
871 dbg("end_paddr = %lx\n", end_pfn << PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100873 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 dbg("bootmap_paddr = %lx\n", bootmem_paddr);
880
881 init_bootmem_node(NODE_DATA(nid), bootmem_paddr >> PAGE_SHIFT,
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100882 start_pfn, end_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700884 free_bootmem_with_active_regions(nid, end_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100886 /* Mark reserved regions on this node */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 for (i = 0; i < lmb.reserved.cnt; i++) {
Michael Ellerman180379d2005-08-03 20:21:26 +1000888 unsigned long physbase = lmb.reserved.region[i].base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 unsigned long size = lmb.reserved.region[i].size;
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100890 unsigned long start_paddr = start_pfn << PAGE_SHIFT;
891 unsigned long end_paddr = end_pfn << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100893 if (early_pfn_to_nid(physbase >> PAGE_SHIFT) != nid &&
894 early_pfn_to_nid((physbase+size-1) >> PAGE_SHIFT) != nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 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,
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800911 size, BOOTMEM_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 }
913 }
Bob Picco802f1922005-09-03 15:54:26 -0700914
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700915 sparse_memory_present_with_active_regions(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917}
918
919void __init paging_init(void)
920{
Mel Gorman6391af12006-10-11 01:20:39 -0700921 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;
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700924 free_area_init_nodes(max_zone_pfns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
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
Balbir Singh1daa6d02008-02-01 15:57:31 +1100938 p = strstr(p, "fake=");
939 if (p)
940 cmdline = p + strlen("fake=");
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return 0;
943}
944early_param("numa", early_numa);
Mike Kravetz237a0982005-12-05 12:06:42 -0800945
946#ifdef CONFIG_MEMORY_HOTPLUG
947/*
Nathan Fontenot0db93602008-07-03 13:25:08 +1000948 * 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
1020/*
Mike Kravetz237a0982005-12-05 12:06:42 -08001021 * 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;
Andrew Morton069007a2006-03-24 02:34:46 -08001028 int nid;
Mike Kravetz237a0982005-12-05 12:06:42 -08001029
1030 if (!numa_enabled || (min_common_depth < 0))
Nathan Fontenot0db93602008-07-03 13:25:08 +10001031 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 }
Mike Kravetz237a0982005-12-05 12:06:42 -08001039
1040 while ((memory = of_find_node_by_type(memory, "memory")) != NULL) {
1041 unsigned long start, size;
Mike Kravetzb226e462005-12-16 14:30:35 -08001042 int ranges;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +10001043 const unsigned int *memcell_buf;
Mike Kravetz237a0982005-12-05 12:06:42 -08001044 unsigned int len;
1045
Stephen Rothwelle2eb6392007-04-03 22:26:41 +10001046 memcell_buf = of_get_property(memory, "reg", &len);
Mike Kravetz237a0982005-12-05 12:06:42 -08001047 if (!memcell_buf || len <= 0)
1048 continue;
1049
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001050 /* ranges in cell */
1051 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
Mike Kravetz237a0982005-12-05 12:06:42 -08001052ha_new_range:
1053 start = read_n_cells(n_mem_addr_cells, &memcell_buf);
1054 size = read_n_cells(n_mem_size_cells, &memcell_buf);
Jeremy Kerr953039c2006-05-01 12:16:12 -07001055 nid = of_node_to_nid_single(memory);
Mike Kravetz237a0982005-12-05 12:06:42 -08001056
Nathan Fontenot0db93602008-07-03 13:25:08 +10001057 if (valid_hot_add_scn(&nid, start, size, scn_addr)) {
Mike Kravetz237a0982005-12-05 12:06:42 -08001058 of_node_put(memory);
Nathan Fontenot0db93602008-07-03 13:25:08 +10001059 return nid;
Mike Kravetz237a0982005-12-05 12:06:42 -08001060 }
1061
1062 if (--ranges) /* process all ranges in cell */
1063 goto ha_new_range;
1064 }
Mike Kravetz237a0982005-12-05 12:06:42 -08001065 BUG(); /* section address should be found above */
Andrew Morton069007a2006-03-24 02:34:46 -08001066 return 0;
Mike Kravetz237a0982005-12-05 12:06:42 -08001067}
1068#endif /* CONFIG_MEMORY_HOTPLUG */