blob: 1efd631211ef3772532430129176286bb182fbbc [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>
Anton Blanchard45fb6ce2005-11-11 14:22:35 +110021#include <asm/sparsemem.h>
David S. Millerd9b2b2a2008-02-13 16:56:49 -080022#include <asm/prom.h>
Paul Mackerrascf00a8d2005-10-31 13:07:02 +110023#include <asm/system.h>
Paul Mackerras2249ca92005-11-07 13:18:13 +110024#include <asm/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26static int numa_enabled = 1;
27
Balbir Singh1daa6d02008-02-01 15:57:31 +110028static char *cmdline __initdata;
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030static int numa_debug;
31#define dbg(args...) if (numa_debug) { printk(KERN_INFO args); }
32
Anton Blanchard45fb6ce2005-11-11 14:22:35 +110033int numa_cpu_lookup_table[NR_CPUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070034cpumask_t numa_cpumask_lookup_table[MAX_NUMNODES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070035struct pglist_data *node_data[MAX_NUMNODES];
Anton Blanchard45fb6ce2005-11-11 14:22:35 +110036
37EXPORT_SYMBOL(numa_cpu_lookup_table);
38EXPORT_SYMBOL(numa_cpumask_lookup_table);
39EXPORT_SYMBOL(node_data);
40
41static bootmem_data_t __initdata plat_node_bdata[MAX_NUMNODES];
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
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600153/* Returns nid in the range [0..MAX_NUMNODES-1], or -1 if no useful numa
154 * info is found.
155 */
Jeremy Kerr953039c2006-05-01 12:16:12 -0700156static int of_node_to_nid_single(struct device_node *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600158 int nid = -1;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000159 const unsigned int *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 if (min_common_depth == -1)
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600162 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 tmp = of_get_associativity(device);
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600165 if (!tmp)
166 goto out;
167
168 if (tmp[0] >= min_common_depth)
Nathan Lynchcf950b72006-03-20 18:35:45 -0600169 nid = tmp[min_common_depth];
Nathan Lynchbc16a752006-03-20 18:36:15 -0600170
171 /* POWER4 LPAR uses 0xffff as invalid node */
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600172 if (nid == 0xffff || nid >= MAX_NUMNODES)
173 nid = -1;
174out:
Nathan Lynchcf950b72006-03-20 18:35:45 -0600175 return nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Jeremy Kerr953039c2006-05-01 12:16:12 -0700178/* Walk the device tree upwards, looking for an associativity id */
179int of_node_to_nid(struct device_node *device)
180{
181 struct device_node *tmp;
182 int nid = -1;
183
184 of_node_get(device);
185 while (device) {
186 nid = of_node_to_nid_single(device);
187 if (nid != -1)
188 break;
189
190 tmp = device;
191 device = of_get_parent(tmp);
192 of_node_put(tmp);
193 }
194 of_node_put(device);
195
196 return nid;
197}
198EXPORT_SYMBOL_GPL(of_node_to_nid);
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/*
201 * In theory, the "ibm,associativity" property may contain multiple
202 * associativity lists because a resource may be multiply connected
203 * into the machine. This resource then has different associativity
204 * characteristics relative to its multiple connections. We ignore
205 * this for now. We also assume that all cpu and memory sets have
206 * their distances represented at a common level. This won't be
Uwe Kleine-König1b3c3712007-02-17 19:23:03 +0100207 * true for hierarchical NUMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 *
209 * In any case the ibm,associativity-reference-points should give
210 * the correct depth for a normal NUMA system.
211 *
212 * - Dave Hansen <haveblue@us.ibm.com>
213 */
214static int __init find_min_common_depth(void)
215{
216 int depth;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000217 const unsigned int *ref_points;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 struct device_node *rtas_root;
219 unsigned int len;
220
221 rtas_root = of_find_node_by_path("/rtas");
222
223 if (!rtas_root)
224 return -1;
225
226 /*
227 * this property is 2 32-bit integers, each representing a level of
228 * depth in the associativity nodes. The first is for an SMP
229 * configuration (should be all 0's) and the second is for a normal
230 * NUMA configuration.
231 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000232 ref_points = of_get_property(rtas_root,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 "ibm,associativity-reference-points", &len);
234
235 if ((len >= 1) && ref_points) {
236 depth = ref_points[1];
237 } else {
Nathan Lynchbf4b85b2006-03-20 18:34:45 -0600238 dbg("NUMA: ibm,associativity-reference-points not found.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 depth = -1;
240 }
241 of_node_put(rtas_root);
242
243 return depth;
244}
245
Mike Kravetz84c9fdd2005-11-30 13:47:23 -0800246static void __init get_n_mem_cells(int *n_addr_cells, int *n_size_cells)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 struct device_node *memory = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 memory = of_find_node_by_type(memory, "memory");
Paul Mackerras54c23312005-12-05 15:50:39 +1100251 if (!memory)
Mike Kravetz84c9fdd2005-11-30 13:47:23 -0800252 panic("numa.c: No memory nodes found!");
Paul Mackerras54c23312005-12-05 15:50:39 +1100253
Stephen Rothwella8bda5d2007-04-03 10:56:50 +1000254 *n_addr_cells = of_n_addr_cells(memory);
Stephen Rothwell9213fee2007-04-03 10:57:48 +1000255 *n_size_cells = of_n_size_cells(memory);
Mike Kravetz84c9fdd2005-11-30 13:47:23 -0800256 of_node_put(memory);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000259static unsigned long __devinit read_n_cells(int n, const unsigned int **buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 unsigned long result = 0;
262
263 while (n--) {
264 result = (result << 32) | **buf;
265 (*buf)++;
266 }
267 return result;
268}
269
270/*
271 * Figure out to which domain a cpu belongs and stick it there.
272 * Return the id of the domain used.
273 */
Nathan Lynch2e5ce392006-03-20 18:35:15 -0600274static int __cpuinit numa_setup_cpu(unsigned long lcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Nathan Lynchcf950b72006-03-20 18:35:45 -0600276 int nid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 struct device_node *cpu = find_cpu_node(lcpu);
278
279 if (!cpu) {
280 WARN_ON(1);
281 goto out;
282 }
283
Jeremy Kerr953039c2006-05-01 12:16:12 -0700284 nid = of_node_to_nid_single(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600286 if (nid < 0 || !node_online(nid))
287 nid = any_online_node(NODE_MASK_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288out:
Nathan Lynchcf950b72006-03-20 18:35:45 -0600289 map_cpu_to_node(lcpu, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 of_node_put(cpu);
292
Nathan Lynchcf950b72006-03-20 18:35:45 -0600293 return nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Chandra Seetharaman74b85f32006-06-27 02:54:09 -0700296static int __cpuinit cpu_numa_callback(struct notifier_block *nfb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 unsigned long action,
298 void *hcpu)
299{
300 unsigned long lcpu = (unsigned long)hcpu;
301 int ret = NOTIFY_DONE;
302
303 switch (action) {
304 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700305 case CPU_UP_PREPARE_FROZEN:
Nathan Lynch2b261222006-03-20 18:37:15 -0600306 numa_setup_cpu(lcpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 ret = NOTIFY_OK;
308 break;
309#ifdef CONFIG_HOTPLUG_CPU
310 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700311 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700313 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 unmap_cpu_from_node(lcpu);
315 break;
316 ret = NOTIFY_OK;
317#endif
318 }
319 return ret;
320}
321
322/*
323 * Check and possibly modify a memory region to enforce the memory limit.
324 *
325 * Returns the size the region should have to enforce the memory limit.
326 * This will either be the original value of size, a truncated value,
327 * or zero. If the returned value of size is 0 the region should be
328 * discarded as it lies wholy above the memory limit.
329 */
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100330static unsigned long __init numa_enforce_memory_limit(unsigned long start,
331 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 /*
334 * We use lmb_end_of_DRAM() in here instead of memory_limit because
335 * we've already adjusted it for the limit and it takes care of
336 * having memory holes below the limit.
337 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 if (! memory_limit)
340 return size;
341
342 if (start + size <= lmb_end_of_DRAM())
343 return size;
344
345 if (start >= lmb_end_of_DRAM())
346 return 0;
347
348 return lmb_end_of_DRAM() - start;
349}
350
Paul Mackerras02045682006-11-29 22:27:42 +1100351/*
352 * Extract NUMA information from the ibm,dynamic-reconfiguration-memory
353 * node. This assumes n_mem_{addr,size}_cells have been set.
354 */
355static void __init parse_drconf_memory(struct device_node *memory)
356{
357 const unsigned int *lm, *dm, *aa;
358 unsigned int ls, ld, la;
359 unsigned int n, aam, aalen;
Michael Ellermanb9c3fdb2007-08-01 11:34:38 +1000360 unsigned long lmb_size, size, start;
Paul Mackerras02045682006-11-29 22:27:42 +1100361 int nid, default_nid = 0;
Michael Ellermanb9c3fdb2007-08-01 11:34:38 +1000362 unsigned int ai, flags;
Paul Mackerras02045682006-11-29 22:27:42 +1100363
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000364 lm = of_get_property(memory, "ibm,lmb-size", &ls);
365 dm = of_get_property(memory, "ibm,dynamic-memory", &ld);
366 aa = of_get_property(memory, "ibm,associativity-lookup-arrays", &la);
Paul Mackerras02045682006-11-29 22:27:42 +1100367 if (!lm || !dm || !aa ||
368 ls < sizeof(unsigned int) || ld < sizeof(unsigned int) ||
369 la < 2 * sizeof(unsigned int))
370 return;
371
372 lmb_size = read_n_cells(n_mem_size_cells, &lm);
373 n = *dm++; /* number of LMBs */
374 aam = *aa++; /* number of associativity lists */
375 aalen = *aa++; /* length of each associativity list */
376 if (ld < (n * (n_mem_addr_cells + 4) + 1) * sizeof(unsigned int) ||
377 la < (aam * aalen + 2) * sizeof(unsigned int))
378 return;
379
380 for (; n != 0; --n) {
381 start = read_n_cells(n_mem_addr_cells, &dm);
382 ai = dm[2];
383 flags = dm[3];
384 dm += 4;
385 /* 0x80 == reserved, 0x8 = assigned to us */
386 if ((flags & 0x80) || !(flags & 0x8))
387 continue;
388 nid = default_nid;
389 /* flags & 0x40 means associativity index is invalid */
390 if (min_common_depth > 0 && min_common_depth <= aalen &&
391 (flags & 0x40) == 0 && ai < aam) {
392 /* this is like of_node_to_nid_single */
393 nid = aa[ai * aalen + min_common_depth - 1];
394 if (nid == 0xffff || nid >= MAX_NUMNODES)
395 nid = default_nid;
396 }
Balbir Singh1daa6d02008-02-01 15:57:31 +1100397
398 fake_numa_create_new_node(((start + lmb_size) >> PAGE_SHIFT),
399 &nid);
Paul Mackerras02045682006-11-29 22:27:42 +1100400 node_set_online(nid);
401
402 size = numa_enforce_memory_limit(start, lmb_size);
403 if (!size)
404 continue;
405
406 add_active_range(nid, start >> PAGE_SHIFT,
407 (start >> PAGE_SHIFT) + (size >> PAGE_SHIFT));
408 }
409}
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411static int __init parse_numa_properties(void)
412{
413 struct device_node *cpu = NULL;
414 struct device_node *memory = NULL;
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600415 int default_nid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 unsigned long i;
417
418 if (numa_enabled == 0) {
419 printk(KERN_WARNING "NUMA disabled by user\n");
420 return -1;
421 }
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 min_common_depth = find_min_common_depth();
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (min_common_depth < 0)
426 return min_common_depth;
427
Nathan Lynchbf4b85b2006-03-20 18:34:45 -0600428 dbg("NUMA associativity depth for CPU/Memory: %d\n", min_common_depth);
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 /*
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600431 * Even though we connect cpus to numa domains later in SMP
432 * init, we need to know the node ids now. This is because
433 * each node to be onlined must have NODE_DATA etc backing it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 */
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600435 for_each_present_cpu(i) {
Nathan Lynchcf950b72006-03-20 18:35:45 -0600436 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 cpu = find_cpu_node(i);
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600439 BUG_ON(!cpu);
Jeremy Kerr953039c2006-05-01 12:16:12 -0700440 nid = of_node_to_nid_single(cpu);
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600441 of_node_put(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600443 /*
444 * Don't fall back to default_nid yet -- we will plug
445 * cpus into nodes once the memory scan has discovered
446 * the topology.
447 */
448 if (nid < 0)
449 continue;
450 node_set_online(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452
Mike Kravetz237a0982005-12-05 12:06:42 -0800453 get_n_mem_cells(&n_mem_addr_cells, &n_mem_size_cells);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 memory = NULL;
455 while ((memory = of_find_node_by_type(memory, "memory")) != NULL) {
456 unsigned long start;
457 unsigned long size;
Nathan Lynchcf950b72006-03-20 18:35:45 -0600458 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 int ranges;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000460 const unsigned int *memcell_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 unsigned int len;
462
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000463 memcell_buf = of_get_property(memory,
Michael Ellermanba759482005-12-04 18:39:55 +1100464 "linux,usable-memory", &len);
465 if (!memcell_buf || len <= 0)
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000466 memcell_buf = of_get_property(memory, "reg", &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (!memcell_buf || len <= 0)
468 continue;
469
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100470 /* ranges in cell */
471 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472new_range:
473 /* these are order-sensitive, and modify the buffer pointer */
Mike Kravetz237a0982005-12-05 12:06:42 -0800474 start = read_n_cells(n_mem_addr_cells, &memcell_buf);
475 size = read_n_cells(n_mem_size_cells, &memcell_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600477 /*
478 * Assumption: either all memory nodes or none will
479 * have associativity properties. If none, then
480 * everything goes to default_nid.
481 */
Jeremy Kerr953039c2006-05-01 12:16:12 -0700482 nid = of_node_to_nid_single(memory);
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600483 if (nid < 0)
484 nid = default_nid;
Balbir Singh1daa6d02008-02-01 15:57:31 +1100485
486 fake_numa_create_new_node(((start + size) >> PAGE_SHIFT), &nid);
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600487 node_set_online(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100489 if (!(size = numa_enforce_memory_limit(start, size))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (--ranges)
491 goto new_range;
492 else
493 continue;
494 }
495
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700496 add_active_range(nid, start >> PAGE_SHIFT,
497 (start >> PAGE_SHIFT) + (size >> PAGE_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 if (--ranges)
500 goto new_range;
501 }
502
Paul Mackerras02045682006-11-29 22:27:42 +1100503 /*
504 * Now do the same thing for each LMB listed in the ibm,dynamic-memory
505 * property in the ibm,dynamic-reconfiguration-memory node.
506 */
507 memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
508 if (memory)
509 parse_drconf_memory(memory);
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return 0;
512}
513
514static void __init setup_nonnuma(void)
515{
516 unsigned long top_of_ram = lmb_end_of_DRAM();
517 unsigned long total_ram = lmb_phys_mem_size();
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700518 unsigned long start_pfn, end_pfn;
Balbir Singh1daa6d02008-02-01 15:57:31 +1100519 unsigned int i, nid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Olof Johanssone110b282006-04-12 15:25:01 -0500521 printk(KERN_DEBUG "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 top_of_ram, total_ram);
Olof Johanssone110b282006-04-12 15:25:01 -0500523 printk(KERN_DEBUG "Memory hole size: %ldMB\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 (top_of_ram - total_ram) >> 20);
525
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700526 for (i = 0; i < lmb.memory.cnt; ++i) {
527 start_pfn = lmb.memory.region[i].base >> PAGE_SHIFT;
528 end_pfn = start_pfn + lmb_size_pages(&lmb.memory, i);
Balbir Singh1daa6d02008-02-01 15:57:31 +1100529
530 fake_numa_create_new_node(end_pfn, &nid);
531 add_active_range(nid, start_pfn, end_pfn);
532 node_set_online(nid);
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
Anton Blanchard4b703a22005-12-13 06:56:47 +1100536void __init dump_numa_cpu_topology(void)
537{
538 unsigned int node;
539 unsigned int cpu, count;
540
541 if (min_common_depth == -1 || !numa_enabled)
542 return;
543
544 for_each_online_node(node) {
Olof Johanssone110b282006-04-12 15:25:01 -0500545 printk(KERN_DEBUG "Node %d CPUs:", node);
Anton Blanchard4b703a22005-12-13 06:56:47 +1100546
547 count = 0;
548 /*
549 * If we used a CPU iterator here we would miss printing
550 * the holes in the cpumap.
551 */
552 for (cpu = 0; cpu < NR_CPUS; cpu++) {
553 if (cpu_isset(cpu, numa_cpumask_lookup_table[node])) {
554 if (count == 0)
555 printk(" %u", cpu);
556 ++count;
557 } else {
558 if (count > 1)
559 printk("-%u", cpu - 1);
560 count = 0;
561 }
562 }
563
564 if (count > 1)
565 printk("-%u", NR_CPUS - 1);
566 printk("\n");
567 }
568}
569
570static void __init dump_numa_memory_topology(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 unsigned int node;
573 unsigned int count;
574
575 if (min_common_depth == -1 || !numa_enabled)
576 return;
577
578 for_each_online_node(node) {
579 unsigned long i;
580
Olof Johanssone110b282006-04-12 15:25:01 -0500581 printk(KERN_DEBUG "Node %d Memory:", node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 count = 0;
584
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100585 for (i = 0; i < lmb_end_of_DRAM();
586 i += (1 << SECTION_SIZE_BITS)) {
587 if (early_pfn_to_nid(i >> PAGE_SHIFT) == node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (count == 0)
589 printk(" 0x%lx", i);
590 ++count;
591 } else {
592 if (count > 0)
593 printk("-0x%lx", i);
594 count = 0;
595 }
596 }
597
598 if (count > 0)
599 printk("-0x%lx", i);
600 printk("\n");
601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
604/*
605 * Allocate some memory, satisfying the lmb or bootmem allocator where
606 * required. nid is the preferred node and end is the physical address of
607 * the highest address in the node.
608 *
609 * Returns the physical address of the memory.
610 */
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100611static void __init *careful_allocation(int nid, unsigned long size,
612 unsigned long align,
613 unsigned long end_pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100615 int new_nid;
Michael Ellermand7a5b2f2006-01-25 21:31:28 +1300616 unsigned long ret = __lmb_alloc_base(size, align, end_pfn << PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 /* retry over all memory */
619 if (!ret)
Michael Ellermand7a5b2f2006-01-25 21:31:28 +1300620 ret = __lmb_alloc_base(size, align, lmb_end_of_DRAM());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 if (!ret)
623 panic("numa.c: cannot allocate %lu bytes on node %d",
624 size, nid);
625
626 /*
627 * If the memory came from a previously allocated node, we must
628 * retry with the bootmem allocator.
629 */
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100630 new_nid = early_pfn_to_nid(ret >> PAGE_SHIFT);
631 if (new_nid < nid) {
632 ret = (unsigned long)__alloc_bootmem_node(NODE_DATA(new_nid),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 size, align, 0);
634
635 if (!ret)
636 panic("numa.c: cannot allocate %lu bytes on node %d",
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100637 size, new_nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100639 ret = __pa(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 dbg("alloc_bootmem %lx %lx\n", ret, size);
642 }
643
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100644 return (void *)ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
Chandra Seetharaman74b85f32006-06-27 02:54:09 -0700647static struct notifier_block __cpuinitdata ppc64_numa_nb = {
648 .notifier_call = cpu_numa_callback,
649 .priority = 1 /* Must run before sched domains notifier. */
650};
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652void __init do_init_bootmem(void)
653{
654 int nid;
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100655 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 min_low_pfn = 0;
658 max_low_pfn = lmb_end_of_DRAM() >> PAGE_SHIFT;
659 max_pfn = max_low_pfn;
660
661 if (parse_numa_properties())
662 setup_nonnuma();
663 else
Anton Blanchard4b703a22005-12-13 06:56:47 +1100664 dump_numa_memory_topology();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 register_cpu_notifier(&ppc64_numa_nb);
Nathan Lynch2b261222006-03-20 18:37:15 -0600667 cpu_numa_callback(&ppc64_numa_nb, CPU_UP_PREPARE,
668 (void *)(unsigned long)boot_cpuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 for_each_online_node(nid) {
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700671 unsigned long start_pfn, end_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 unsigned long bootmem_paddr;
673 unsigned long bootmap_pages;
674
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700675 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 /* Allocate the node structure node local if possible */
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100678 NODE_DATA(nid) = careful_allocation(nid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 sizeof(struct pglist_data),
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100680 SMP_CACHE_BYTES, end_pfn);
681 NODE_DATA(nid) = __va(NODE_DATA(nid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 memset(NODE_DATA(nid), 0, sizeof(struct pglist_data));
683
684 dbg("node %d\n", nid);
685 dbg("NODE_DATA() = %p\n", NODE_DATA(nid));
686
687 NODE_DATA(nid)->bdata = &plat_node_bdata[nid];
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100688 NODE_DATA(nid)->node_start_pfn = start_pfn;
689 NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 if (NODE_DATA(nid)->node_spanned_pages == 0)
692 continue;
693
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100694 dbg("start_paddr = %lx\n", start_pfn << PAGE_SHIFT);
695 dbg("end_paddr = %lx\n", end_pfn << PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100697 bootmap_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
698 bootmem_paddr = (unsigned long)careful_allocation(nid,
699 bootmap_pages << PAGE_SHIFT,
700 PAGE_SIZE, end_pfn);
701 memset(__va(bootmem_paddr), 0, bootmap_pages << PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 dbg("bootmap_paddr = %lx\n", bootmem_paddr);
704
705 init_bootmem_node(NODE_DATA(nid), bootmem_paddr >> PAGE_SHIFT,
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100706 start_pfn, end_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700708 free_bootmem_with_active_regions(nid, end_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100710 /* Mark reserved regions on this node */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 for (i = 0; i < lmb.reserved.cnt; i++) {
Michael Ellerman180379d2005-08-03 20:21:26 +1000712 unsigned long physbase = lmb.reserved.region[i].base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 unsigned long size = lmb.reserved.region[i].size;
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100714 unsigned long start_paddr = start_pfn << PAGE_SHIFT;
715 unsigned long end_paddr = end_pfn << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Anton Blanchard45fb6ce2005-11-11 14:22:35 +1100717 if (early_pfn_to_nid(physbase >> PAGE_SHIFT) != nid &&
718 early_pfn_to_nid((physbase+size-1) >> PAGE_SHIFT) != nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 continue;
720
721 if (physbase < end_paddr &&
722 (physbase+size) > start_paddr) {
723 /* overlaps */
724 if (physbase < start_paddr) {
725 size -= start_paddr - physbase;
726 physbase = start_paddr;
727 }
728
729 if (size > end_paddr - physbase)
730 size = end_paddr - physbase;
731
732 dbg("reserve_bootmem %lx %lx\n", physbase,
733 size);
734 reserve_bootmem_node(NODE_DATA(nid), physbase,
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800735 size, BOOTMEM_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737 }
Bob Picco802f1922005-09-03 15:54:26 -0700738
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700739 sparse_memory_present_with_active_regions(nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
741}
742
743void __init paging_init(void)
744{
Mel Gorman6391af12006-10-11 01:20:39 -0700745 unsigned long max_zone_pfns[MAX_NR_ZONES];
746 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
747 max_zone_pfns[ZONE_DMA] = lmb_end_of_DRAM() >> PAGE_SHIFT;
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700748 free_area_init_nodes(max_zone_pfns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
751static int __init early_numa(char *p)
752{
753 if (!p)
754 return 0;
755
756 if (strstr(p, "off"))
757 numa_enabled = 0;
758
759 if (strstr(p, "debug"))
760 numa_debug = 1;
761
Balbir Singh1daa6d02008-02-01 15:57:31 +1100762 p = strstr(p, "fake=");
763 if (p)
764 cmdline = p + strlen("fake=");
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return 0;
767}
768early_param("numa", early_numa);
Mike Kravetz237a0982005-12-05 12:06:42 -0800769
770#ifdef CONFIG_MEMORY_HOTPLUG
771/*
772 * Find the node associated with a hot added memory section. Section
773 * corresponds to a SPARSEMEM section, not an LMB. It is assumed that
774 * sections are fully contained within a single LMB.
775 */
776int hot_add_scn_to_nid(unsigned long scn_addr)
777{
778 struct device_node *memory = NULL;
Mike Kravetzb226e462005-12-16 14:30:35 -0800779 nodemask_t nodes;
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600780 int default_nid = any_online_node(NODE_MASK_ALL);
Andrew Morton069007a2006-03-24 02:34:46 -0800781 int nid;
Mike Kravetz237a0982005-12-05 12:06:42 -0800782
783 if (!numa_enabled || (min_common_depth < 0))
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600784 return default_nid;
Mike Kravetz237a0982005-12-05 12:06:42 -0800785
786 while ((memory = of_find_node_by_type(memory, "memory")) != NULL) {
787 unsigned long start, size;
Mike Kravetzb226e462005-12-16 14:30:35 -0800788 int ranges;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000789 const unsigned int *memcell_buf;
Mike Kravetz237a0982005-12-05 12:06:42 -0800790 unsigned int len;
791
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000792 memcell_buf = of_get_property(memory, "reg", &len);
Mike Kravetz237a0982005-12-05 12:06:42 -0800793 if (!memcell_buf || len <= 0)
794 continue;
795
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100796 /* ranges in cell */
797 ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
Mike Kravetz237a0982005-12-05 12:06:42 -0800798ha_new_range:
799 start = read_n_cells(n_mem_addr_cells, &memcell_buf);
800 size = read_n_cells(n_mem_size_cells, &memcell_buf);
Jeremy Kerr953039c2006-05-01 12:16:12 -0700801 nid = of_node_to_nid_single(memory);
Mike Kravetz237a0982005-12-05 12:06:42 -0800802
803 /* Domains not present at boot default to 0 */
Nathan Lynch482ec7c2006-03-20 18:36:45 -0600804 if (nid < 0 || !node_online(nid))
805 nid = default_nid;
Mike Kravetz237a0982005-12-05 12:06:42 -0800806
807 if ((scn_addr >= start) && (scn_addr < (start + size))) {
808 of_node_put(memory);
Nathan Lynchcf950b72006-03-20 18:35:45 -0600809 goto got_nid;
Mike Kravetz237a0982005-12-05 12:06:42 -0800810 }
811
812 if (--ranges) /* process all ranges in cell */
813 goto ha_new_range;
814 }
Mike Kravetz237a0982005-12-05 12:06:42 -0800815 BUG(); /* section address should be found above */
Andrew Morton069007a2006-03-24 02:34:46 -0800816 return 0;
Mike Kravetzb226e462005-12-16 14:30:35 -0800817
818 /* Temporary code to ensure that returned node is not empty */
Nathan Lynchcf950b72006-03-20 18:35:45 -0600819got_nid:
Mike Kravetzb226e462005-12-16 14:30:35 -0800820 nodes_setall(nodes);
Nathan Lynchcf950b72006-03-20 18:35:45 -0600821 while (NODE_DATA(nid)->node_spanned_pages == 0) {
822 node_clear(nid, nodes);
823 nid = any_online_node(nodes);
Mike Kravetzb226e462005-12-16 14:30:35 -0800824 }
Nathan Lynchcf950b72006-03-20 18:35:45 -0600825 return nid;
Mike Kravetz237a0982005-12-05 12:06:42 -0800826}
827#endif /* CONFIG_MEMORY_HOTPLUG */