]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * pSeries NUMA support | |
3 | * | |
4 | * Copyright (C) 2002 Anton Blanchard <anton@au.ibm.com>, IBM | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version | |
9 | * 2 of the License, or (at your option) any later version. | |
10 | */ | |
11 | #include <linux/threads.h> | |
12 | #include <linux/bootmem.h> | |
13 | #include <linux/init.h> | |
14 | #include <linux/mm.h> | |
15 | #include <linux/mmzone.h> | |
4b16f8e2 | 16 | #include <linux/export.h> |
1da177e4 LT |
17 | #include <linux/nodemask.h> |
18 | #include <linux/cpu.h> | |
19 | #include <linux/notifier.h> | |
95f72d1e | 20 | #include <linux/memblock.h> |
6df1646e | 21 | #include <linux/of.h> |
06eccea6 | 22 | #include <linux/pfn.h> |
9eff1a38 JL |
23 | #include <linux/cpuset.h> |
24 | #include <linux/node.h> | |
45fb6cea | 25 | #include <asm/sparsemem.h> |
d9b2b2a2 | 26 | #include <asm/prom.h> |
2249ca9d | 27 | #include <asm/smp.h> |
9eff1a38 JL |
28 | #include <asm/firmware.h> |
29 | #include <asm/paca.h> | |
39bf990e | 30 | #include <asm/hvcall.h> |
ae3a197e | 31 | #include <asm/setup.h> |
1da177e4 LT |
32 | |
33 | static int numa_enabled = 1; | |
34 | ||
1daa6d08 BS |
35 | static char *cmdline __initdata; |
36 | ||
1da177e4 LT |
37 | static int numa_debug; |
38 | #define dbg(args...) if (numa_debug) { printk(KERN_INFO args); } | |
39 | ||
45fb6cea | 40 | int numa_cpu_lookup_table[NR_CPUS]; |
25863de0 | 41 | cpumask_var_t node_to_cpumask_map[MAX_NUMNODES]; |
1da177e4 | 42 | struct pglist_data *node_data[MAX_NUMNODES]; |
45fb6cea AB |
43 | |
44 | EXPORT_SYMBOL(numa_cpu_lookup_table); | |
25863de0 | 45 | EXPORT_SYMBOL(node_to_cpumask_map); |
45fb6cea AB |
46 | EXPORT_SYMBOL(node_data); |
47 | ||
1da177e4 | 48 | static int min_common_depth; |
237a0989 | 49 | static int n_mem_addr_cells, n_mem_size_cells; |
41eab6f8 AB |
50 | static int form1_affinity; |
51 | ||
52 | #define MAX_DISTANCE_REF_POINTS 4 | |
53 | static int distance_ref_points_depth; | |
54 | static const unsigned int *distance_ref_points; | |
55 | static int distance_lookup_table[MAX_NUMNODES][MAX_DISTANCE_REF_POINTS]; | |
1da177e4 | 56 | |
25863de0 AB |
57 | /* |
58 | * Allocate node_to_cpumask_map based on number of available nodes | |
59 | * Requires node_possible_map to be valid. | |
60 | * | |
9512938b | 61 | * Note: cpumask_of_node() is not valid until after this is done. |
25863de0 AB |
62 | */ |
63 | static void __init setup_node_to_cpumask_map(void) | |
64 | { | |
65 | unsigned int node, num = 0; | |
66 | ||
67 | /* setup nr_node_ids if not done yet */ | |
68 | if (nr_node_ids == MAX_NUMNODES) { | |
69 | for_each_node_mask(node, node_possible_map) | |
70 | num = node; | |
71 | nr_node_ids = num + 1; | |
72 | } | |
73 | ||
74 | /* allocate the map */ | |
75 | for (node = 0; node < nr_node_ids; node++) | |
76 | alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]); | |
77 | ||
78 | /* cpumask_of_node() will now work */ | |
79 | dbg("Node to cpumask map for %d nodes\n", nr_node_ids); | |
80 | } | |
81 | ||
55671f3c | 82 | static int __init fake_numa_create_new_node(unsigned long end_pfn, |
1daa6d08 BS |
83 | unsigned int *nid) |
84 | { | |
85 | unsigned long long mem; | |
86 | char *p = cmdline; | |
87 | static unsigned int fake_nid; | |
88 | static unsigned long long curr_boundary; | |
89 | ||
90 | /* | |
91 | * Modify node id, iff we started creating NUMA nodes | |
92 | * We want to continue from where we left of the last time | |
93 | */ | |
94 | if (fake_nid) | |
95 | *nid = fake_nid; | |
96 | /* | |
97 | * In case there are no more arguments to parse, the | |
98 | * node_id should be the same as the last fake node id | |
99 | * (we've handled this above). | |
100 | */ | |
101 | if (!p) | |
102 | return 0; | |
103 | ||
104 | mem = memparse(p, &p); | |
105 | if (!mem) | |
106 | return 0; | |
107 | ||
108 | if (mem < curr_boundary) | |
109 | return 0; | |
110 | ||
111 | curr_boundary = mem; | |
112 | ||
113 | if ((end_pfn << PAGE_SHIFT) > mem) { | |
114 | /* | |
115 | * Skip commas and spaces | |
116 | */ | |
117 | while (*p == ',' || *p == ' ' || *p == '\t') | |
118 | p++; | |
119 | ||
120 | cmdline = p; | |
121 | fake_nid++; | |
122 | *nid = fake_nid; | |
123 | dbg("created new fake_node with id %d\n", fake_nid); | |
124 | return 1; | |
125 | } | |
126 | return 0; | |
127 | } | |
128 | ||
8f64e1f2 | 129 | /* |
5dfe8660 | 130 | * get_node_active_region - Return active region containing pfn |
e8170372 | 131 | * Active range returned is empty if none found. |
5dfe8660 TH |
132 | * @pfn: The page to return the region for |
133 | * @node_ar: Returned set to the active region containing @pfn | |
8f64e1f2 | 134 | */ |
5dfe8660 TH |
135 | static void __init get_node_active_region(unsigned long pfn, |
136 | struct node_active_region *node_ar) | |
8f64e1f2 | 137 | { |
5dfe8660 TH |
138 | unsigned long start_pfn, end_pfn; |
139 | int i, nid; | |
140 | ||
141 | for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) { | |
142 | if (pfn >= start_pfn && pfn < end_pfn) { | |
143 | node_ar->nid = nid; | |
144 | node_ar->start_pfn = start_pfn; | |
145 | node_ar->end_pfn = end_pfn; | |
146 | break; | |
147 | } | |
148 | } | |
8f64e1f2 JT |
149 | } |
150 | ||
39bf990e | 151 | static void map_cpu_to_node(int cpu, int node) |
1da177e4 LT |
152 | { |
153 | numa_cpu_lookup_table[cpu] = node; | |
45fb6cea | 154 | |
bf4b85b0 NL |
155 | dbg("adding cpu %d to node %d\n", cpu, node); |
156 | ||
25863de0 AB |
157 | if (!(cpumask_test_cpu(cpu, node_to_cpumask_map[node]))) |
158 | cpumask_set_cpu(cpu, node_to_cpumask_map[node]); | |
1da177e4 LT |
159 | } |
160 | ||
39bf990e | 161 | #if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_PPC_SPLPAR) |
1da177e4 LT |
162 | static void unmap_cpu_from_node(unsigned long cpu) |
163 | { | |
164 | int node = numa_cpu_lookup_table[cpu]; | |
165 | ||
166 | dbg("removing cpu %lu from node %d\n", cpu, node); | |
167 | ||
25863de0 | 168 | if (cpumask_test_cpu(cpu, node_to_cpumask_map[node])) { |
429f4d8d | 169 | cpumask_clear_cpu(cpu, node_to_cpumask_map[node]); |
1da177e4 LT |
170 | } else { |
171 | printk(KERN_ERR "WARNING: cpu %lu not found in node %d\n", | |
172 | cpu, node); | |
173 | } | |
174 | } | |
39bf990e | 175 | #endif /* CONFIG_HOTPLUG_CPU || CONFIG_PPC_SPLPAR */ |
1da177e4 | 176 | |
1da177e4 | 177 | /* must hold reference to node during call */ |
a7f67bdf | 178 | static const int *of_get_associativity(struct device_node *dev) |
1da177e4 | 179 | { |
e2eb6392 | 180 | return of_get_property(dev, "ibm,associativity", NULL); |
1da177e4 LT |
181 | } |
182 | ||
cf00085d C |
183 | /* |
184 | * Returns the property linux,drconf-usable-memory if | |
185 | * it exists (the property exists only in kexec/kdump kernels, | |
186 | * added by kexec-tools) | |
187 | */ | |
188 | static const u32 *of_get_usable_memory(struct device_node *memory) | |
189 | { | |
190 | const u32 *prop; | |
191 | u32 len; | |
192 | prop = of_get_property(memory, "linux,drconf-usable-memory", &len); | |
193 | if (!prop || len < sizeof(unsigned int)) | |
194 | return 0; | |
195 | return prop; | |
196 | } | |
197 | ||
41eab6f8 AB |
198 | int __node_distance(int a, int b) |
199 | { | |
200 | int i; | |
201 | int distance = LOCAL_DISTANCE; | |
202 | ||
203 | if (!form1_affinity) | |
7122beee | 204 | return ((a == b) ? LOCAL_DISTANCE : REMOTE_DISTANCE); |
41eab6f8 AB |
205 | |
206 | for (i = 0; i < distance_ref_points_depth; i++) { | |
207 | if (distance_lookup_table[a][i] == distance_lookup_table[b][i]) | |
208 | break; | |
209 | ||
210 | /* Double the distance for each NUMA level */ | |
211 | distance *= 2; | |
212 | } | |
213 | ||
214 | return distance; | |
215 | } | |
216 | ||
217 | static void initialize_distance_lookup_table(int nid, | |
218 | const unsigned int *associativity) | |
219 | { | |
220 | int i; | |
221 | ||
222 | if (!form1_affinity) | |
223 | return; | |
224 | ||
225 | for (i = 0; i < distance_ref_points_depth; i++) { | |
226 | distance_lookup_table[nid][i] = | |
227 | associativity[distance_ref_points[i]]; | |
228 | } | |
229 | } | |
230 | ||
482ec7c4 NL |
231 | /* Returns nid in the range [0..MAX_NUMNODES-1], or -1 if no useful numa |
232 | * info is found. | |
233 | */ | |
9eff1a38 | 234 | static int associativity_to_nid(const unsigned int *associativity) |
1da177e4 | 235 | { |
482ec7c4 | 236 | int nid = -1; |
1da177e4 LT |
237 | |
238 | if (min_common_depth == -1) | |
482ec7c4 | 239 | goto out; |
1da177e4 | 240 | |
9eff1a38 JL |
241 | if (associativity[0] >= min_common_depth) |
242 | nid = associativity[min_common_depth]; | |
bc16a759 NL |
243 | |
244 | /* POWER4 LPAR uses 0xffff as invalid node */ | |
482ec7c4 NL |
245 | if (nid == 0xffff || nid >= MAX_NUMNODES) |
246 | nid = -1; | |
41eab6f8 | 247 | |
9eff1a38 JL |
248 | if (nid > 0 && associativity[0] >= distance_ref_points_depth) |
249 | initialize_distance_lookup_table(nid, associativity); | |
41eab6f8 | 250 | |
482ec7c4 | 251 | out: |
cf950b7a | 252 | return nid; |
1da177e4 LT |
253 | } |
254 | ||
9eff1a38 JL |
255 | /* Returns the nid associated with the given device tree node, |
256 | * or -1 if not found. | |
257 | */ | |
258 | static int of_node_to_nid_single(struct device_node *device) | |
259 | { | |
260 | int nid = -1; | |
261 | const unsigned int *tmp; | |
262 | ||
263 | tmp = of_get_associativity(device); | |
264 | if (tmp) | |
265 | nid = associativity_to_nid(tmp); | |
266 | return nid; | |
267 | } | |
268 | ||
953039c8 JK |
269 | /* Walk the device tree upwards, looking for an associativity id */ |
270 | int of_node_to_nid(struct device_node *device) | |
271 | { | |
272 | struct device_node *tmp; | |
273 | int nid = -1; | |
274 | ||
275 | of_node_get(device); | |
276 | while (device) { | |
277 | nid = of_node_to_nid_single(device); | |
278 | if (nid != -1) | |
279 | break; | |
280 | ||
281 | tmp = device; | |
282 | device = of_get_parent(tmp); | |
283 | of_node_put(tmp); | |
284 | } | |
285 | of_node_put(device); | |
286 | ||
287 | return nid; | |
288 | } | |
289 | EXPORT_SYMBOL_GPL(of_node_to_nid); | |
290 | ||
1da177e4 LT |
291 | static int __init find_min_common_depth(void) |
292 | { | |
41eab6f8 | 293 | int depth; |
e70606eb | 294 | struct device_node *root; |
1da177e4 | 295 | |
1c8ee733 DS |
296 | if (firmware_has_feature(FW_FEATURE_OPAL)) |
297 | root = of_find_node_by_path("/ibm,opal"); | |
298 | else | |
299 | root = of_find_node_by_path("/rtas"); | |
e70606eb ME |
300 | if (!root) |
301 | root = of_find_node_by_path("/"); | |
1da177e4 LT |
302 | |
303 | /* | |
41eab6f8 AB |
304 | * This property is a set of 32-bit integers, each representing |
305 | * an index into the ibm,associativity nodes. | |
306 | * | |
307 | * With form 0 affinity the first integer is for an SMP configuration | |
308 | * (should be all 0's) and the second is for a normal NUMA | |
309 | * configuration. We have only one level of NUMA. | |
310 | * | |
311 | * With form 1 affinity the first integer is the most significant | |
312 | * NUMA boundary and the following are progressively less significant | |
313 | * boundaries. There can be more than one level of NUMA. | |
1da177e4 | 314 | */ |
e70606eb | 315 | distance_ref_points = of_get_property(root, |
41eab6f8 AB |
316 | "ibm,associativity-reference-points", |
317 | &distance_ref_points_depth); | |
318 | ||
319 | if (!distance_ref_points) { | |
320 | dbg("NUMA: ibm,associativity-reference-points not found.\n"); | |
321 | goto err; | |
322 | } | |
323 | ||
324 | distance_ref_points_depth /= sizeof(int); | |
1da177e4 | 325 | |
8002b0c5 NF |
326 | if (firmware_has_feature(FW_FEATURE_OPAL) || |
327 | firmware_has_feature(FW_FEATURE_TYPE1_AFFINITY)) { | |
328 | dbg("Using form 1 affinity\n"); | |
1c8ee733 | 329 | form1_affinity = 1; |
4b83c330 AB |
330 | } |
331 | ||
41eab6f8 AB |
332 | if (form1_affinity) { |
333 | depth = distance_ref_points[0]; | |
1da177e4 | 334 | } else { |
41eab6f8 AB |
335 | if (distance_ref_points_depth < 2) { |
336 | printk(KERN_WARNING "NUMA: " | |
337 | "short ibm,associativity-reference-points\n"); | |
338 | goto err; | |
339 | } | |
340 | ||
341 | depth = distance_ref_points[1]; | |
1da177e4 | 342 | } |
1da177e4 | 343 | |
41eab6f8 AB |
344 | /* |
345 | * Warn and cap if the hardware supports more than | |
346 | * MAX_DISTANCE_REF_POINTS domains. | |
347 | */ | |
348 | if (distance_ref_points_depth > MAX_DISTANCE_REF_POINTS) { | |
349 | printk(KERN_WARNING "NUMA: distance array capped at " | |
350 | "%d entries\n", MAX_DISTANCE_REF_POINTS); | |
351 | distance_ref_points_depth = MAX_DISTANCE_REF_POINTS; | |
352 | } | |
353 | ||
e70606eb | 354 | of_node_put(root); |
1da177e4 | 355 | return depth; |
41eab6f8 AB |
356 | |
357 | err: | |
e70606eb | 358 | of_node_put(root); |
41eab6f8 | 359 | return -1; |
1da177e4 LT |
360 | } |
361 | ||
84c9fdd1 | 362 | static void __init get_n_mem_cells(int *n_addr_cells, int *n_size_cells) |
1da177e4 LT |
363 | { |
364 | struct device_node *memory = NULL; | |
1da177e4 LT |
365 | |
366 | memory = of_find_node_by_type(memory, "memory"); | |
54c23310 | 367 | if (!memory) |
84c9fdd1 | 368 | panic("numa.c: No memory nodes found!"); |
54c23310 | 369 | |
a8bda5dd | 370 | *n_addr_cells = of_n_addr_cells(memory); |
9213feea | 371 | *n_size_cells = of_n_size_cells(memory); |
84c9fdd1 | 372 | of_node_put(memory); |
1da177e4 LT |
373 | } |
374 | ||
2011b1d0 | 375 | static unsigned long read_n_cells(int n, const unsigned int **buf) |
1da177e4 LT |
376 | { |
377 | unsigned long result = 0; | |
378 | ||
379 | while (n--) { | |
380 | result = (result << 32) | **buf; | |
381 | (*buf)++; | |
382 | } | |
383 | return result; | |
384 | } | |
385 | ||
8342681d | 386 | /* |
95f72d1e | 387 | * Read the next memblock list entry from the ibm,dynamic-memory property |
8342681d NF |
388 | * and return the information in the provided of_drconf_cell structure. |
389 | */ | |
390 | static void read_drconf_cell(struct of_drconf_cell *drmem, const u32 **cellp) | |
391 | { | |
392 | const u32 *cp; | |
393 | ||
394 | drmem->base_addr = read_n_cells(n_mem_addr_cells, cellp); | |
395 | ||
396 | cp = *cellp; | |
397 | drmem->drc_index = cp[0]; | |
398 | drmem->reserved = cp[1]; | |
399 | drmem->aa_index = cp[2]; | |
400 | drmem->flags = cp[3]; | |
401 | ||
402 | *cellp = cp + 4; | |
403 | } | |
404 | ||
405 | /* | |
25985edc | 406 | * Retrieve and validate the ibm,dynamic-memory property of the device tree. |
8342681d | 407 | * |
95f72d1e YL |
408 | * The layout of the ibm,dynamic-memory property is a number N of memblock |
409 | * list entries followed by N memblock list entries. Each memblock list entry | |
25985edc | 410 | * contains information as laid out in the of_drconf_cell struct above. |
8342681d NF |
411 | */ |
412 | static int of_get_drconf_memory(struct device_node *memory, const u32 **dm) | |
413 | { | |
414 | const u32 *prop; | |
415 | u32 len, entries; | |
416 | ||
417 | prop = of_get_property(memory, "ibm,dynamic-memory", &len); | |
418 | if (!prop || len < sizeof(unsigned int)) | |
419 | return 0; | |
420 | ||
421 | entries = *prop++; | |
422 | ||
423 | /* Now that we know the number of entries, revalidate the size | |
424 | * of the property read in to ensure we have everything | |
425 | */ | |
426 | if (len < (entries * (n_mem_addr_cells + 4) + 1) * sizeof(unsigned int)) | |
427 | return 0; | |
428 | ||
429 | *dm = prop; | |
430 | return entries; | |
431 | } | |
432 | ||
433 | /* | |
25985edc | 434 | * Retrieve and validate the ibm,lmb-size property for drconf memory |
8342681d NF |
435 | * from the device tree. |
436 | */ | |
3fdfd990 | 437 | static u64 of_get_lmb_size(struct device_node *memory) |
8342681d NF |
438 | { |
439 | const u32 *prop; | |
440 | u32 len; | |
441 | ||
3fdfd990 | 442 | prop = of_get_property(memory, "ibm,lmb-size", &len); |
8342681d NF |
443 | if (!prop || len < sizeof(unsigned int)) |
444 | return 0; | |
445 | ||
446 | return read_n_cells(n_mem_size_cells, &prop); | |
447 | } | |
448 | ||
449 | struct assoc_arrays { | |
450 | u32 n_arrays; | |
451 | u32 array_sz; | |
452 | const u32 *arrays; | |
453 | }; | |
454 | ||
455 | /* | |
25985edc | 456 | * Retrieve and validate the list of associativity arrays for drconf |
8342681d NF |
457 | * memory from the ibm,associativity-lookup-arrays property of the |
458 | * device tree.. | |
459 | * | |
460 | * The layout of the ibm,associativity-lookup-arrays property is a number N | |
461 | * indicating the number of associativity arrays, followed by a number M | |
462 | * indicating the size of each associativity array, followed by a list | |
463 | * of N associativity arrays. | |
464 | */ | |
465 | static int of_get_assoc_arrays(struct device_node *memory, | |
466 | struct assoc_arrays *aa) | |
467 | { | |
468 | const u32 *prop; | |
469 | u32 len; | |
470 | ||
471 | prop = of_get_property(memory, "ibm,associativity-lookup-arrays", &len); | |
472 | if (!prop || len < 2 * sizeof(unsigned int)) | |
473 | return -1; | |
474 | ||
475 | aa->n_arrays = *prop++; | |
476 | aa->array_sz = *prop++; | |
477 | ||
42b2aa86 | 478 | /* Now that we know the number of arrays and size of each array, |
8342681d NF |
479 | * revalidate the size of the property read in. |
480 | */ | |
481 | if (len < (aa->n_arrays * aa->array_sz + 2) * sizeof(unsigned int)) | |
482 | return -1; | |
483 | ||
484 | aa->arrays = prop; | |
485 | return 0; | |
486 | } | |
487 | ||
488 | /* | |
489 | * This is like of_node_to_nid_single() for memory represented in the | |
490 | * ibm,dynamic-reconfiguration-memory node. | |
491 | */ | |
492 | static int of_drconf_to_nid_single(struct of_drconf_cell *drmem, | |
493 | struct assoc_arrays *aa) | |
494 | { | |
495 | int default_nid = 0; | |
496 | int nid = default_nid; | |
497 | int index; | |
498 | ||
499 | if (min_common_depth > 0 && min_common_depth <= aa->array_sz && | |
500 | !(drmem->flags & DRCONF_MEM_AI_INVALID) && | |
501 | drmem->aa_index < aa->n_arrays) { | |
502 | index = drmem->aa_index * aa->array_sz + min_common_depth - 1; | |
503 | nid = aa->arrays[index]; | |
504 | ||
505 | if (nid == 0xffff || nid >= MAX_NUMNODES) | |
506 | nid = default_nid; | |
507 | } | |
508 | ||
509 | return nid; | |
510 | } | |
511 | ||
1da177e4 LT |
512 | /* |
513 | * Figure out to which domain a cpu belongs and stick it there. | |
514 | * Return the id of the domain used. | |
515 | */ | |
2e5ce39d | 516 | static int __cpuinit numa_setup_cpu(unsigned long lcpu) |
1da177e4 | 517 | { |
cf950b7a | 518 | int nid = 0; |
8b16cd23 | 519 | struct device_node *cpu = of_get_cpu_node(lcpu, NULL); |
1da177e4 LT |
520 | |
521 | if (!cpu) { | |
522 | WARN_ON(1); | |
523 | goto out; | |
524 | } | |
525 | ||
953039c8 | 526 | nid = of_node_to_nid_single(cpu); |
1da177e4 | 527 | |
482ec7c4 | 528 | if (nid < 0 || !node_online(nid)) |
72c33688 | 529 | nid = first_online_node; |
1da177e4 | 530 | out: |
cf950b7a | 531 | map_cpu_to_node(lcpu, nid); |
1da177e4 LT |
532 | |
533 | of_node_put(cpu); | |
534 | ||
cf950b7a | 535 | return nid; |
1da177e4 LT |
536 | } |
537 | ||
74b85f37 | 538 | static int __cpuinit cpu_numa_callback(struct notifier_block *nfb, |
1da177e4 LT |
539 | unsigned long action, |
540 | void *hcpu) | |
541 | { | |
542 | unsigned long lcpu = (unsigned long)hcpu; | |
543 | int ret = NOTIFY_DONE; | |
544 | ||
545 | switch (action) { | |
546 | case CPU_UP_PREPARE: | |
8bb78442 | 547 | case CPU_UP_PREPARE_FROZEN: |
2b261227 | 548 | numa_setup_cpu(lcpu); |
1da177e4 LT |
549 | ret = NOTIFY_OK; |
550 | break; | |
551 | #ifdef CONFIG_HOTPLUG_CPU | |
552 | case CPU_DEAD: | |
8bb78442 | 553 | case CPU_DEAD_FROZEN: |
1da177e4 | 554 | case CPU_UP_CANCELED: |
8bb78442 | 555 | case CPU_UP_CANCELED_FROZEN: |
1da177e4 LT |
556 | unmap_cpu_from_node(lcpu); |
557 | break; | |
558 | ret = NOTIFY_OK; | |
559 | #endif | |
560 | } | |
561 | return ret; | |
562 | } | |
563 | ||
564 | /* | |
565 | * Check and possibly modify a memory region to enforce the memory limit. | |
566 | * | |
567 | * Returns the size the region should have to enforce the memory limit. | |
568 | * This will either be the original value of size, a truncated value, | |
569 | * or zero. If the returned value of size is 0 the region should be | |
25985edc | 570 | * discarded as it lies wholly above the memory limit. |
1da177e4 | 571 | */ |
45fb6cea AB |
572 | static unsigned long __init numa_enforce_memory_limit(unsigned long start, |
573 | unsigned long size) | |
1da177e4 LT |
574 | { |
575 | /* | |
95f72d1e | 576 | * We use memblock_end_of_DRAM() in here instead of memory_limit because |
1da177e4 | 577 | * we've already adjusted it for the limit and it takes care of |
fe55249d MM |
578 | * having memory holes below the limit. Also, in the case of |
579 | * iommu_is_off, memory_limit is not set but is implicitly enforced. | |
1da177e4 | 580 | */ |
1da177e4 | 581 | |
95f72d1e | 582 | if (start + size <= memblock_end_of_DRAM()) |
1da177e4 LT |
583 | return size; |
584 | ||
95f72d1e | 585 | if (start >= memblock_end_of_DRAM()) |
1da177e4 LT |
586 | return 0; |
587 | ||
95f72d1e | 588 | return memblock_end_of_DRAM() - start; |
1da177e4 LT |
589 | } |
590 | ||
cf00085d C |
591 | /* |
592 | * Reads the counter for a given entry in | |
593 | * linux,drconf-usable-memory property | |
594 | */ | |
595 | static inline int __init read_usm_ranges(const u32 **usm) | |
596 | { | |
597 | /* | |
3fdfd990 | 598 | * For each lmb in ibm,dynamic-memory a corresponding |
cf00085d C |
599 | * entry in linux,drconf-usable-memory property contains |
600 | * a counter followed by that many (base, size) duple. | |
601 | * read the counter from linux,drconf-usable-memory | |
602 | */ | |
603 | return read_n_cells(n_mem_size_cells, usm); | |
604 | } | |
605 | ||
0204568a PM |
606 | /* |
607 | * Extract NUMA information from the ibm,dynamic-reconfiguration-memory | |
608 | * node. This assumes n_mem_{addr,size}_cells have been set. | |
609 | */ | |
610 | static void __init parse_drconf_memory(struct device_node *memory) | |
611 | { | |
82b2521d | 612 | const u32 *uninitialized_var(dm), *usm; |
cf00085d | 613 | unsigned int n, rc, ranges, is_kexec_kdump = 0; |
3fdfd990 | 614 | unsigned long lmb_size, base, size, sz; |
8342681d | 615 | int nid; |
aa709f3b | 616 | struct assoc_arrays aa = { .arrays = NULL }; |
8342681d NF |
617 | |
618 | n = of_get_drconf_memory(memory, &dm); | |
619 | if (!n) | |
0204568a PM |
620 | return; |
621 | ||
3fdfd990 BH |
622 | lmb_size = of_get_lmb_size(memory); |
623 | if (!lmb_size) | |
8342681d NF |
624 | return; |
625 | ||
626 | rc = of_get_assoc_arrays(memory, &aa); | |
627 | if (rc) | |
0204568a PM |
628 | return; |
629 | ||
cf00085d C |
630 | /* check if this is a kexec/kdump kernel */ |
631 | usm = of_get_usable_memory(memory); | |
632 | if (usm != NULL) | |
633 | is_kexec_kdump = 1; | |
634 | ||
0204568a | 635 | for (; n != 0; --n) { |
8342681d NF |
636 | struct of_drconf_cell drmem; |
637 | ||
638 | read_drconf_cell(&drmem, &dm); | |
639 | ||
640 | /* skip this block if the reserved bit is set in flags (0x80) | |
641 | or if the block is not assigned to this partition (0x8) */ | |
642 | if ((drmem.flags & DRCONF_MEM_RESERVED) | |
643 | || !(drmem.flags & DRCONF_MEM_ASSIGNED)) | |
0204568a | 644 | continue; |
1daa6d08 | 645 | |
cf00085d | 646 | base = drmem.base_addr; |
3fdfd990 | 647 | size = lmb_size; |
cf00085d | 648 | ranges = 1; |
8342681d | 649 | |
cf00085d C |
650 | if (is_kexec_kdump) { |
651 | ranges = read_usm_ranges(&usm); | |
652 | if (!ranges) /* there are no (base, size) duple */ | |
653 | continue; | |
654 | } | |
655 | do { | |
656 | if (is_kexec_kdump) { | |
657 | base = read_n_cells(n_mem_addr_cells, &usm); | |
658 | size = read_n_cells(n_mem_size_cells, &usm); | |
659 | } | |
660 | nid = of_drconf_to_nid_single(&drmem, &aa); | |
661 | fake_numa_create_new_node( | |
662 | ((base + size) >> PAGE_SHIFT), | |
8342681d | 663 | &nid); |
cf00085d C |
664 | node_set_online(nid); |
665 | sz = numa_enforce_memory_limit(base, size); | |
666 | if (sz) | |
1d7cfe18 | 667 | memblock_set_node(base, sz, nid); |
cf00085d | 668 | } while (--ranges); |
0204568a PM |
669 | } |
670 | } | |
671 | ||
1da177e4 LT |
672 | static int __init parse_numa_properties(void) |
673 | { | |
94db7c5e | 674 | struct device_node *memory; |
482ec7c4 | 675 | int default_nid = 0; |
1da177e4 LT |
676 | unsigned long i; |
677 | ||
678 | if (numa_enabled == 0) { | |
679 | printk(KERN_WARNING "NUMA disabled by user\n"); | |
680 | return -1; | |
681 | } | |
682 | ||
1da177e4 LT |
683 | min_common_depth = find_min_common_depth(); |
684 | ||
1da177e4 LT |
685 | if (min_common_depth < 0) |
686 | return min_common_depth; | |
687 | ||
bf4b85b0 NL |
688 | dbg("NUMA associativity depth for CPU/Memory: %d\n", min_common_depth); |
689 | ||
1da177e4 | 690 | /* |
482ec7c4 NL |
691 | * Even though we connect cpus to numa domains later in SMP |
692 | * init, we need to know the node ids now. This is because | |
693 | * each node to be onlined must have NODE_DATA etc backing it. | |
1da177e4 | 694 | */ |
482ec7c4 | 695 | for_each_present_cpu(i) { |
dfbe93a2 | 696 | struct device_node *cpu; |
cf950b7a | 697 | int nid; |
1da177e4 | 698 | |
8b16cd23 | 699 | cpu = of_get_cpu_node(i, NULL); |
482ec7c4 | 700 | BUG_ON(!cpu); |
953039c8 | 701 | nid = of_node_to_nid_single(cpu); |
482ec7c4 | 702 | of_node_put(cpu); |
1da177e4 | 703 | |
482ec7c4 NL |
704 | /* |
705 | * Don't fall back to default_nid yet -- we will plug | |
706 | * cpus into nodes once the memory scan has discovered | |
707 | * the topology. | |
708 | */ | |
709 | if (nid < 0) | |
710 | continue; | |
711 | node_set_online(nid); | |
1da177e4 LT |
712 | } |
713 | ||
237a0989 | 714 | get_n_mem_cells(&n_mem_addr_cells, &n_mem_size_cells); |
94db7c5e AB |
715 | |
716 | for_each_node_by_type(memory, "memory") { | |
1da177e4 LT |
717 | unsigned long start; |
718 | unsigned long size; | |
cf950b7a | 719 | int nid; |
1da177e4 | 720 | int ranges; |
a7f67bdf | 721 | const unsigned int *memcell_buf; |
1da177e4 LT |
722 | unsigned int len; |
723 | ||
e2eb6392 | 724 | memcell_buf = of_get_property(memory, |
ba759485 ME |
725 | "linux,usable-memory", &len); |
726 | if (!memcell_buf || len <= 0) | |
e2eb6392 | 727 | memcell_buf = of_get_property(memory, "reg", &len); |
1da177e4 LT |
728 | if (!memcell_buf || len <= 0) |
729 | continue; | |
730 | ||
cc5d0189 BH |
731 | /* ranges in cell */ |
732 | ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells); | |
1da177e4 LT |
733 | new_range: |
734 | /* these are order-sensitive, and modify the buffer pointer */ | |
237a0989 MK |
735 | start = read_n_cells(n_mem_addr_cells, &memcell_buf); |
736 | size = read_n_cells(n_mem_size_cells, &memcell_buf); | |
1da177e4 | 737 | |
482ec7c4 NL |
738 | /* |
739 | * Assumption: either all memory nodes or none will | |
740 | * have associativity properties. If none, then | |
741 | * everything goes to default_nid. | |
742 | */ | |
953039c8 | 743 | nid = of_node_to_nid_single(memory); |
482ec7c4 NL |
744 | if (nid < 0) |
745 | nid = default_nid; | |
1daa6d08 BS |
746 | |
747 | fake_numa_create_new_node(((start + size) >> PAGE_SHIFT), &nid); | |
482ec7c4 | 748 | node_set_online(nid); |
1da177e4 | 749 | |
45fb6cea | 750 | if (!(size = numa_enforce_memory_limit(start, size))) { |
1da177e4 LT |
751 | if (--ranges) |
752 | goto new_range; | |
753 | else | |
754 | continue; | |
755 | } | |
756 | ||
1d7cfe18 | 757 | memblock_set_node(start, size, nid); |
1da177e4 LT |
758 | |
759 | if (--ranges) | |
760 | goto new_range; | |
761 | } | |
762 | ||
0204568a | 763 | /* |
dfbe93a2 AB |
764 | * Now do the same thing for each MEMBLOCK listed in the |
765 | * ibm,dynamic-memory property in the | |
766 | * ibm,dynamic-reconfiguration-memory node. | |
0204568a PM |
767 | */ |
768 | memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory"); | |
769 | if (memory) | |
770 | parse_drconf_memory(memory); | |
771 | ||
1da177e4 LT |
772 | return 0; |
773 | } | |
774 | ||
775 | static void __init setup_nonnuma(void) | |
776 | { | |
95f72d1e YL |
777 | unsigned long top_of_ram = memblock_end_of_DRAM(); |
778 | unsigned long total_ram = memblock_phys_mem_size(); | |
c67c3cb4 | 779 | unsigned long start_pfn, end_pfn; |
28be7072 BH |
780 | unsigned int nid = 0; |
781 | struct memblock_region *reg; | |
1da177e4 | 782 | |
e110b281 | 783 | printk(KERN_DEBUG "Top of RAM: 0x%lx, Total RAM: 0x%lx\n", |
1da177e4 | 784 | top_of_ram, total_ram); |
e110b281 | 785 | printk(KERN_DEBUG "Memory hole size: %ldMB\n", |
1da177e4 LT |
786 | (top_of_ram - total_ram) >> 20); |
787 | ||
28be7072 | 788 | for_each_memblock(memory, reg) { |
c7fc2de0 YL |
789 | start_pfn = memblock_region_memory_base_pfn(reg); |
790 | end_pfn = memblock_region_memory_end_pfn(reg); | |
1daa6d08 BS |
791 | |
792 | fake_numa_create_new_node(end_pfn, &nid); | |
1d7cfe18 TH |
793 | memblock_set_node(PFN_PHYS(start_pfn), |
794 | PFN_PHYS(end_pfn - start_pfn), nid); | |
1daa6d08 | 795 | node_set_online(nid); |
c67c3cb4 | 796 | } |
1da177e4 LT |
797 | } |
798 | ||
4b703a23 AB |
799 | void __init dump_numa_cpu_topology(void) |
800 | { | |
801 | unsigned int node; | |
802 | unsigned int cpu, count; | |
803 | ||
804 | if (min_common_depth == -1 || !numa_enabled) | |
805 | return; | |
806 | ||
807 | for_each_online_node(node) { | |
e110b281 | 808 | printk(KERN_DEBUG "Node %d CPUs:", node); |
4b703a23 AB |
809 | |
810 | count = 0; | |
811 | /* | |
812 | * If we used a CPU iterator here we would miss printing | |
813 | * the holes in the cpumap. | |
814 | */ | |
25863de0 AB |
815 | for (cpu = 0; cpu < nr_cpu_ids; cpu++) { |
816 | if (cpumask_test_cpu(cpu, | |
817 | node_to_cpumask_map[node])) { | |
4b703a23 AB |
818 | if (count == 0) |
819 | printk(" %u", cpu); | |
820 | ++count; | |
821 | } else { | |
822 | if (count > 1) | |
823 | printk("-%u", cpu - 1); | |
824 | count = 0; | |
825 | } | |
826 | } | |
827 | ||
828 | if (count > 1) | |
25863de0 | 829 | printk("-%u", nr_cpu_ids - 1); |
4b703a23 AB |
830 | printk("\n"); |
831 | } | |
832 | } | |
833 | ||
834 | static void __init dump_numa_memory_topology(void) | |
1da177e4 LT |
835 | { |
836 | unsigned int node; | |
837 | unsigned int count; | |
838 | ||
839 | if (min_common_depth == -1 || !numa_enabled) | |
840 | return; | |
841 | ||
842 | for_each_online_node(node) { | |
843 | unsigned long i; | |
844 | ||
e110b281 | 845 | printk(KERN_DEBUG "Node %d Memory:", node); |
1da177e4 LT |
846 | |
847 | count = 0; | |
848 | ||
95f72d1e | 849 | for (i = 0; i < memblock_end_of_DRAM(); |
45fb6cea AB |
850 | i += (1 << SECTION_SIZE_BITS)) { |
851 | if (early_pfn_to_nid(i >> PAGE_SHIFT) == node) { | |
1da177e4 LT |
852 | if (count == 0) |
853 | printk(" 0x%lx", i); | |
854 | ++count; | |
855 | } else { | |
856 | if (count > 0) | |
857 | printk("-0x%lx", i); | |
858 | count = 0; | |
859 | } | |
860 | } | |
861 | ||
862 | if (count > 0) | |
863 | printk("-0x%lx", i); | |
864 | printk("\n"); | |
865 | } | |
1da177e4 LT |
866 | } |
867 | ||
868 | /* | |
95f72d1e | 869 | * Allocate some memory, satisfying the memblock or bootmem allocator where |
1da177e4 LT |
870 | * required. nid is the preferred node and end is the physical address of |
871 | * the highest address in the node. | |
872 | * | |
0be210fd | 873 | * Returns the virtual address of the memory. |
1da177e4 | 874 | */ |
893473df | 875 | static void __init *careful_zallocation(int nid, unsigned long size, |
45fb6cea AB |
876 | unsigned long align, |
877 | unsigned long end_pfn) | |
1da177e4 | 878 | { |
0be210fd | 879 | void *ret; |
45fb6cea | 880 | int new_nid; |
0be210fd DH |
881 | unsigned long ret_paddr; |
882 | ||
95f72d1e | 883 | ret_paddr = __memblock_alloc_base(size, align, end_pfn << PAGE_SHIFT); |
1da177e4 LT |
884 | |
885 | /* retry over all memory */ | |
0be210fd | 886 | if (!ret_paddr) |
95f72d1e | 887 | ret_paddr = __memblock_alloc_base(size, align, memblock_end_of_DRAM()); |
1da177e4 | 888 | |
0be210fd | 889 | if (!ret_paddr) |
5d21ea2b | 890 | panic("numa.c: cannot allocate %lu bytes for node %d", |
1da177e4 LT |
891 | size, nid); |
892 | ||
0be210fd DH |
893 | ret = __va(ret_paddr); |
894 | ||
1da177e4 | 895 | /* |
c555e520 | 896 | * We initialize the nodes in numeric order: 0, 1, 2... |
95f72d1e | 897 | * and hand over control from the MEMBLOCK allocator to the |
c555e520 DH |
898 | * bootmem allocator. If this function is called for |
899 | * node 5, then we know that all nodes <5 are using the | |
95f72d1e | 900 | * bootmem allocator instead of the MEMBLOCK allocator. |
c555e520 DH |
901 | * |
902 | * So, check the nid from which this allocation came | |
903 | * and double check to see if we need to use bootmem | |
95f72d1e | 904 | * instead of the MEMBLOCK. We don't free the MEMBLOCK memory |
c555e520 | 905 | * since it would be useless. |
1da177e4 | 906 | */ |
0be210fd | 907 | new_nid = early_pfn_to_nid(ret_paddr >> PAGE_SHIFT); |
45fb6cea | 908 | if (new_nid < nid) { |
0be210fd | 909 | ret = __alloc_bootmem_node(NODE_DATA(new_nid), |
1da177e4 LT |
910 | size, align, 0); |
911 | ||
0be210fd | 912 | dbg("alloc_bootmem %p %lx\n", ret, size); |
1da177e4 LT |
913 | } |
914 | ||
893473df | 915 | memset(ret, 0, size); |
0be210fd | 916 | return ret; |
1da177e4 LT |
917 | } |
918 | ||
74b85f37 CS |
919 | static struct notifier_block __cpuinitdata ppc64_numa_nb = { |
920 | .notifier_call = cpu_numa_callback, | |
921 | .priority = 1 /* Must run before sched domains notifier. */ | |
922 | }; | |
923 | ||
28e86bdb | 924 | static void __init mark_reserved_regions_for_nid(int nid) |
4a618669 DH |
925 | { |
926 | struct pglist_data *node = NODE_DATA(nid); | |
28be7072 | 927 | struct memblock_region *reg; |
4a618669 | 928 | |
28be7072 BH |
929 | for_each_memblock(reserved, reg) { |
930 | unsigned long physbase = reg->base; | |
931 | unsigned long size = reg->size; | |
4a618669 | 932 | unsigned long start_pfn = physbase >> PAGE_SHIFT; |
06eccea6 | 933 | unsigned long end_pfn = PFN_UP(physbase + size); |
4a618669 DH |
934 | struct node_active_region node_ar; |
935 | unsigned long node_end_pfn = node->node_start_pfn + | |
936 | node->node_spanned_pages; | |
937 | ||
938 | /* | |
95f72d1e | 939 | * Check to make sure that this memblock.reserved area is |
4a618669 DH |
940 | * within the bounds of the node that we care about. |
941 | * Checking the nid of the start and end points is not | |
942 | * sufficient because the reserved area could span the | |
943 | * entire node. | |
944 | */ | |
945 | if (end_pfn <= node->node_start_pfn || | |
946 | start_pfn >= node_end_pfn) | |
947 | continue; | |
948 | ||
949 | get_node_active_region(start_pfn, &node_ar); | |
950 | while (start_pfn < end_pfn && | |
951 | node_ar.start_pfn < node_ar.end_pfn) { | |
952 | unsigned long reserve_size = size; | |
953 | /* | |
954 | * if reserved region extends past active region | |
955 | * then trim size to active region | |
956 | */ | |
957 | if (end_pfn > node_ar.end_pfn) | |
958 | reserve_size = (node_ar.end_pfn << PAGE_SHIFT) | |
06eccea6 | 959 | - physbase; |
a4c74ddd DH |
960 | /* |
961 | * Only worry about *this* node, others may not | |
962 | * yet have valid NODE_DATA(). | |
963 | */ | |
964 | if (node_ar.nid == nid) { | |
965 | dbg("reserve_bootmem %lx %lx nid=%d\n", | |
966 | physbase, reserve_size, node_ar.nid); | |
967 | reserve_bootmem_node(NODE_DATA(node_ar.nid), | |
968 | physbase, reserve_size, | |
969 | BOOTMEM_DEFAULT); | |
970 | } | |
4a618669 DH |
971 | /* |
972 | * if reserved region is contained in the active region | |
973 | * then done. | |
974 | */ | |
975 | if (end_pfn <= node_ar.end_pfn) | |
976 | break; | |
977 | ||
978 | /* | |
979 | * reserved region extends past the active region | |
980 | * get next active region that contains this | |
981 | * reserved region | |
982 | */ | |
983 | start_pfn = node_ar.end_pfn; | |
984 | physbase = start_pfn << PAGE_SHIFT; | |
985 | size = size - reserve_size; | |
986 | get_node_active_region(start_pfn, &node_ar); | |
987 | } | |
988 | } | |
989 | } | |
990 | ||
991 | ||
1da177e4 LT |
992 | void __init do_init_bootmem(void) |
993 | { | |
994 | int nid; | |
1da177e4 LT |
995 | |
996 | min_low_pfn = 0; | |
95f72d1e | 997 | max_low_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT; |
1da177e4 LT |
998 | max_pfn = max_low_pfn; |
999 | ||
1000 | if (parse_numa_properties()) | |
1001 | setup_nonnuma(); | |
1002 | else | |
4b703a23 | 1003 | dump_numa_memory_topology(); |
1da177e4 | 1004 | |
1da177e4 | 1005 | for_each_online_node(nid) { |
c67c3cb4 | 1006 | unsigned long start_pfn, end_pfn; |
0be210fd | 1007 | void *bootmem_vaddr; |
1da177e4 LT |
1008 | unsigned long bootmap_pages; |
1009 | ||
c67c3cb4 | 1010 | get_pfn_range_for_nid(nid, &start_pfn, &end_pfn); |
1da177e4 | 1011 | |
4a618669 DH |
1012 | /* |
1013 | * Allocate the node structure node local if possible | |
1014 | * | |
1015 | * Be careful moving this around, as it relies on all | |
1016 | * previous nodes' bootmem to be initialized and have | |
1017 | * all reserved areas marked. | |
1018 | */ | |
893473df | 1019 | NODE_DATA(nid) = careful_zallocation(nid, |
1da177e4 | 1020 | sizeof(struct pglist_data), |
45fb6cea | 1021 | SMP_CACHE_BYTES, end_pfn); |
1da177e4 LT |
1022 | |
1023 | dbg("node %d\n", nid); | |
1024 | dbg("NODE_DATA() = %p\n", NODE_DATA(nid)); | |
1025 | ||
b61bfa3c | 1026 | NODE_DATA(nid)->bdata = &bootmem_node_data[nid]; |
45fb6cea AB |
1027 | NODE_DATA(nid)->node_start_pfn = start_pfn; |
1028 | NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn; | |
1da177e4 LT |
1029 | |
1030 | if (NODE_DATA(nid)->node_spanned_pages == 0) | |
1031 | continue; | |
1032 | ||
45fb6cea AB |
1033 | dbg("start_paddr = %lx\n", start_pfn << PAGE_SHIFT); |
1034 | dbg("end_paddr = %lx\n", end_pfn << PAGE_SHIFT); | |
1da177e4 | 1035 | |
45fb6cea | 1036 | bootmap_pages = bootmem_bootmap_pages(end_pfn - start_pfn); |
893473df | 1037 | bootmem_vaddr = careful_zallocation(nid, |
45fb6cea AB |
1038 | bootmap_pages << PAGE_SHIFT, |
1039 | PAGE_SIZE, end_pfn); | |
1da177e4 | 1040 | |
0be210fd | 1041 | dbg("bootmap_vaddr = %p\n", bootmem_vaddr); |
1da177e4 | 1042 | |
0be210fd DH |
1043 | init_bootmem_node(NODE_DATA(nid), |
1044 | __pa(bootmem_vaddr) >> PAGE_SHIFT, | |
45fb6cea | 1045 | start_pfn, end_pfn); |
1da177e4 | 1046 | |
c67c3cb4 | 1047 | free_bootmem_with_active_regions(nid, end_pfn); |
4a618669 DH |
1048 | /* |
1049 | * Be very careful about moving this around. Future | |
893473df | 1050 | * calls to careful_zallocation() depend on this getting |
4a618669 DH |
1051 | * done correctly. |
1052 | */ | |
1053 | mark_reserved_regions_for_nid(nid); | |
8f64e1f2 | 1054 | sparse_memory_present_with_active_regions(nid); |
4a618669 | 1055 | } |
d3f6204a BH |
1056 | |
1057 | init_bootmem_done = 1; | |
25863de0 AB |
1058 | |
1059 | /* | |
1060 | * Now bootmem is initialised we can create the node to cpumask | |
1061 | * lookup tables and setup the cpu callback to populate them. | |
1062 | */ | |
1063 | setup_node_to_cpumask_map(); | |
1064 | ||
1065 | register_cpu_notifier(&ppc64_numa_nb); | |
1066 | cpu_numa_callback(&ppc64_numa_nb, CPU_UP_PREPARE, | |
1067 | (void *)(unsigned long)boot_cpuid); | |
1da177e4 LT |
1068 | } |
1069 | ||
1070 | void __init paging_init(void) | |
1071 | { | |
6391af17 MG |
1072 | unsigned long max_zone_pfns[MAX_NR_ZONES]; |
1073 | memset(max_zone_pfns, 0, sizeof(max_zone_pfns)); | |
95f72d1e | 1074 | max_zone_pfns[ZONE_DMA] = memblock_end_of_DRAM() >> PAGE_SHIFT; |
c67c3cb4 | 1075 | free_area_init_nodes(max_zone_pfns); |
1da177e4 LT |
1076 | } |
1077 | ||
1078 | static int __init early_numa(char *p) | |
1079 | { | |
1080 | if (!p) | |
1081 | return 0; | |
1082 | ||
1083 | if (strstr(p, "off")) | |
1084 | numa_enabled = 0; | |
1085 | ||
1086 | if (strstr(p, "debug")) | |
1087 | numa_debug = 1; | |
1088 | ||
1daa6d08 BS |
1089 | p = strstr(p, "fake="); |
1090 | if (p) | |
1091 | cmdline = p + strlen("fake="); | |
1092 | ||
1da177e4 LT |
1093 | return 0; |
1094 | } | |
1095 | early_param("numa", early_numa); | |
237a0989 MK |
1096 | |
1097 | #ifdef CONFIG_MEMORY_HOTPLUG | |
0db9360a | 1098 | /* |
0f16ef7f NF |
1099 | * Find the node associated with a hot added memory section for |
1100 | * memory represented in the device tree by the property | |
1101 | * ibm,dynamic-reconfiguration-memory/ibm,dynamic-memory. | |
0db9360a NF |
1102 | */ |
1103 | static int hot_add_drconf_scn_to_nid(struct device_node *memory, | |
1104 | unsigned long scn_addr) | |
1105 | { | |
1106 | const u32 *dm; | |
0f16ef7f | 1107 | unsigned int drconf_cell_cnt, rc; |
3fdfd990 | 1108 | unsigned long lmb_size; |
0db9360a | 1109 | struct assoc_arrays aa; |
0f16ef7f | 1110 | int nid = -1; |
0db9360a | 1111 | |
0f16ef7f NF |
1112 | drconf_cell_cnt = of_get_drconf_memory(memory, &dm); |
1113 | if (!drconf_cell_cnt) | |
1114 | return -1; | |
0db9360a | 1115 | |
3fdfd990 BH |
1116 | lmb_size = of_get_lmb_size(memory); |
1117 | if (!lmb_size) | |
0f16ef7f | 1118 | return -1; |
0db9360a NF |
1119 | |
1120 | rc = of_get_assoc_arrays(memory, &aa); | |
1121 | if (rc) | |
0f16ef7f | 1122 | return -1; |
0db9360a | 1123 | |
0f16ef7f | 1124 | for (; drconf_cell_cnt != 0; --drconf_cell_cnt) { |
0db9360a NF |
1125 | struct of_drconf_cell drmem; |
1126 | ||
1127 | read_drconf_cell(&drmem, &dm); | |
1128 | ||
1129 | /* skip this block if it is reserved or not assigned to | |
1130 | * this partition */ | |
1131 | if ((drmem.flags & DRCONF_MEM_RESERVED) | |
1132 | || !(drmem.flags & DRCONF_MEM_ASSIGNED)) | |
1133 | continue; | |
1134 | ||
0f16ef7f | 1135 | if ((scn_addr < drmem.base_addr) |
3fdfd990 | 1136 | || (scn_addr >= (drmem.base_addr + lmb_size))) |
0f16ef7f NF |
1137 | continue; |
1138 | ||
0db9360a | 1139 | nid = of_drconf_to_nid_single(&drmem, &aa); |
0f16ef7f NF |
1140 | break; |
1141 | } | |
1142 | ||
1143 | return nid; | |
1144 | } | |
1145 | ||
1146 | /* | |
1147 | * Find the node associated with a hot added memory section for memory | |
1148 | * represented in the device tree as a node (i.e. memory@XXXX) for | |
95f72d1e | 1149 | * each memblock. |
0f16ef7f NF |
1150 | */ |
1151 | int hot_add_node_scn_to_nid(unsigned long scn_addr) | |
1152 | { | |
94db7c5e | 1153 | struct device_node *memory; |
0f16ef7f NF |
1154 | int nid = -1; |
1155 | ||
94db7c5e | 1156 | for_each_node_by_type(memory, "memory") { |
0f16ef7f NF |
1157 | unsigned long start, size; |
1158 | int ranges; | |
1159 | const unsigned int *memcell_buf; | |
1160 | unsigned int len; | |
1161 | ||
1162 | memcell_buf = of_get_property(memory, "reg", &len); | |
1163 | if (!memcell_buf || len <= 0) | |
1164 | continue; | |
1165 | ||
1166 | /* ranges in cell */ | |
1167 | ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells); | |
1168 | ||
1169 | while (ranges--) { | |
1170 | start = read_n_cells(n_mem_addr_cells, &memcell_buf); | |
1171 | size = read_n_cells(n_mem_size_cells, &memcell_buf); | |
1172 | ||
1173 | if ((scn_addr < start) || (scn_addr >= (start + size))) | |
1174 | continue; | |
1175 | ||
1176 | nid = of_node_to_nid_single(memory); | |
1177 | break; | |
1178 | } | |
0db9360a | 1179 | |
0f16ef7f NF |
1180 | if (nid >= 0) |
1181 | break; | |
0db9360a NF |
1182 | } |
1183 | ||
60831842 AB |
1184 | of_node_put(memory); |
1185 | ||
0f16ef7f | 1186 | return nid; |
0db9360a NF |
1187 | } |
1188 | ||
237a0989 MK |
1189 | /* |
1190 | * Find the node associated with a hot added memory section. Section | |
95f72d1e YL |
1191 | * corresponds to a SPARSEMEM section, not an MEMBLOCK. It is assumed that |
1192 | * sections are fully contained within a single MEMBLOCK. | |
237a0989 MK |
1193 | */ |
1194 | int hot_add_scn_to_nid(unsigned long scn_addr) | |
1195 | { | |
1196 | struct device_node *memory = NULL; | |
0f16ef7f | 1197 | int nid, found = 0; |
237a0989 MK |
1198 | |
1199 | if (!numa_enabled || (min_common_depth < 0)) | |
72c33688 | 1200 | return first_online_node; |
0db9360a NF |
1201 | |
1202 | memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory"); | |
1203 | if (memory) { | |
1204 | nid = hot_add_drconf_scn_to_nid(memory, scn_addr); | |
1205 | of_node_put(memory); | |
0f16ef7f NF |
1206 | } else { |
1207 | nid = hot_add_node_scn_to_nid(scn_addr); | |
0db9360a | 1208 | } |
237a0989 | 1209 | |
0f16ef7f | 1210 | if (nid < 0 || !node_online(nid)) |
72c33688 | 1211 | nid = first_online_node; |
237a0989 | 1212 | |
0f16ef7f NF |
1213 | if (NODE_DATA(nid)->node_spanned_pages) |
1214 | return nid; | |
237a0989 | 1215 | |
0f16ef7f NF |
1216 | for_each_online_node(nid) { |
1217 | if (NODE_DATA(nid)->node_spanned_pages) { | |
1218 | found = 1; | |
1219 | break; | |
237a0989 | 1220 | } |
237a0989 | 1221 | } |
0f16ef7f NF |
1222 | |
1223 | BUG_ON(!found); | |
1224 | return nid; | |
237a0989 | 1225 | } |
0f16ef7f | 1226 | |
cd34206e NA |
1227 | static u64 hot_add_drconf_memory_max(void) |
1228 | { | |
1229 | struct device_node *memory = NULL; | |
1230 | unsigned int drconf_cell_cnt = 0; | |
1231 | u64 lmb_size = 0; | |
1232 | const u32 *dm = 0; | |
1233 | ||
1234 | memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory"); | |
1235 | if (memory) { | |
1236 | drconf_cell_cnt = of_get_drconf_memory(memory, &dm); | |
1237 | lmb_size = of_get_lmb_size(memory); | |
1238 | of_node_put(memory); | |
1239 | } | |
1240 | return lmb_size * drconf_cell_cnt; | |
1241 | } | |
1242 | ||
1243 | /* | |
1244 | * memory_hotplug_max - return max address of memory that may be added | |
1245 | * | |
1246 | * This is currently only used on systems that support drconfig memory | |
1247 | * hotplug. | |
1248 | */ | |
1249 | u64 memory_hotplug_max(void) | |
1250 | { | |
1251 | return max(hot_add_drconf_memory_max(), memblock_end_of_DRAM()); | |
1252 | } | |
237a0989 | 1253 | #endif /* CONFIG_MEMORY_HOTPLUG */ |
9eff1a38 | 1254 | |
bd03403a | 1255 | /* Virtual Processor Home Node (VPHN) support */ |
39bf990e | 1256 | #ifdef CONFIG_PPC_SPLPAR |
5de16699 | 1257 | static u8 vphn_cpu_change_counts[NR_CPUS][MAX_DISTANCE_REF_POINTS]; |
9eff1a38 JL |
1258 | static cpumask_t cpu_associativity_changes_mask; |
1259 | static int vphn_enabled; | |
5d88aa85 JL |
1260 | static int prrn_enabled; |
1261 | static void reset_topology_timer(void); | |
9eff1a38 JL |
1262 | |
1263 | /* | |
1264 | * Store the current values of the associativity change counters in the | |
1265 | * hypervisor. | |
1266 | */ | |
1267 | static void setup_cpu_associativity_change_counters(void) | |
1268 | { | |
cd9d6cc7 | 1269 | int cpu; |
9eff1a38 | 1270 | |
5de16699 AB |
1271 | /* The VPHN feature supports a maximum of 8 reference points */ |
1272 | BUILD_BUG_ON(MAX_DISTANCE_REF_POINTS > 8); | |
1273 | ||
9eff1a38 | 1274 | for_each_possible_cpu(cpu) { |
cd9d6cc7 | 1275 | int i; |
9eff1a38 JL |
1276 | u8 *counts = vphn_cpu_change_counts[cpu]; |
1277 | volatile u8 *hypervisor_counts = lppaca[cpu].vphn_assoc_counts; | |
1278 | ||
5de16699 | 1279 | for (i = 0; i < distance_ref_points_depth; i++) |
9eff1a38 | 1280 | counts[i] = hypervisor_counts[i]; |
9eff1a38 JL |
1281 | } |
1282 | } | |
1283 | ||
1284 | /* | |
1285 | * The hypervisor maintains a set of 8 associativity change counters in | |
1286 | * the VPA of each cpu that correspond to the associativity levels in the | |
1287 | * ibm,associativity-reference-points property. When an associativity | |
1288 | * level changes, the corresponding counter is incremented. | |
1289 | * | |
1290 | * Set a bit in cpu_associativity_changes_mask for each cpu whose home | |
1291 | * node associativity levels have changed. | |
1292 | * | |
1293 | * Returns the number of cpus with unhandled associativity changes. | |
1294 | */ | |
1295 | static int update_cpu_associativity_changes_mask(void) | |
1296 | { | |
5d88aa85 | 1297 | int cpu; |
9eff1a38 JL |
1298 | cpumask_t *changes = &cpu_associativity_changes_mask; |
1299 | ||
9eff1a38 JL |
1300 | for_each_possible_cpu(cpu) { |
1301 | int i, changed = 0; | |
1302 | u8 *counts = vphn_cpu_change_counts[cpu]; | |
1303 | volatile u8 *hypervisor_counts = lppaca[cpu].vphn_assoc_counts; | |
1304 | ||
5de16699 | 1305 | for (i = 0; i < distance_ref_points_depth; i++) { |
d69043e8 | 1306 | if (hypervisor_counts[i] != counts[i]) { |
9eff1a38 JL |
1307 | counts[i] = hypervisor_counts[i]; |
1308 | changed = 1; | |
1309 | } | |
1310 | } | |
1311 | if (changed) { | |
1312 | cpumask_set_cpu(cpu, changes); | |
9eff1a38 JL |
1313 | } |
1314 | } | |
1315 | ||
5d88aa85 | 1316 | return cpumask_weight(changes); |
9eff1a38 JL |
1317 | } |
1318 | ||
c0e5e46f AB |
1319 | /* |
1320 | * 6 64-bit registers unpacked into 12 32-bit associativity values. To form | |
1321 | * the complete property we have to add the length in the first cell. | |
1322 | */ | |
1323 | #define VPHN_ASSOC_BUFSIZE (6*sizeof(u64)/sizeof(u32) + 1) | |
9eff1a38 JL |
1324 | |
1325 | /* | |
1326 | * Convert the associativity domain numbers returned from the hypervisor | |
1327 | * to the sequence they would appear in the ibm,associativity property. | |
1328 | */ | |
1329 | static int vphn_unpack_associativity(const long *packed, unsigned int *unpacked) | |
1330 | { | |
cd9d6cc7 | 1331 | int i, nr_assoc_doms = 0; |
9eff1a38 JL |
1332 | const u16 *field = (const u16*) packed; |
1333 | ||
1334 | #define VPHN_FIELD_UNUSED (0xffff) | |
1335 | #define VPHN_FIELD_MSB (0x8000) | |
1336 | #define VPHN_FIELD_MASK (~VPHN_FIELD_MSB) | |
1337 | ||
c0e5e46f | 1338 | for (i = 1; i < VPHN_ASSOC_BUFSIZE; i++) { |
9eff1a38 JL |
1339 | if (*field == VPHN_FIELD_UNUSED) { |
1340 | /* All significant fields processed, and remaining | |
1341 | * fields contain the reserved value of all 1's. | |
1342 | * Just store them. | |
1343 | */ | |
1344 | unpacked[i] = *((u32*)field); | |
1345 | field += 2; | |
7639adaa | 1346 | } else if (*field & VPHN_FIELD_MSB) { |
9eff1a38 JL |
1347 | /* Data is in the lower 15 bits of this field */ |
1348 | unpacked[i] = *field & VPHN_FIELD_MASK; | |
1349 | field++; | |
1350 | nr_assoc_doms++; | |
7639adaa | 1351 | } else { |
9eff1a38 JL |
1352 | /* Data is in the lower 15 bits of this field |
1353 | * concatenated with the next 16 bit field | |
1354 | */ | |
1355 | unpacked[i] = *((u32*)field); | |
1356 | field += 2; | |
1357 | nr_assoc_doms++; | |
1358 | } | |
1359 | } | |
1360 | ||
c0e5e46f AB |
1361 | /* The first cell contains the length of the property */ |
1362 | unpacked[0] = nr_assoc_doms; | |
1363 | ||
9eff1a38 JL |
1364 | return nr_assoc_doms; |
1365 | } | |
1366 | ||
1367 | /* | |
1368 | * Retrieve the new associativity information for a virtual processor's | |
1369 | * home node. | |
1370 | */ | |
1371 | static long hcall_vphn(unsigned long cpu, unsigned int *associativity) | |
1372 | { | |
cd9d6cc7 | 1373 | long rc; |
9eff1a38 JL |
1374 | long retbuf[PLPAR_HCALL9_BUFSIZE] = {0}; |
1375 | u64 flags = 1; | |
1376 | int hwcpu = get_hard_smp_processor_id(cpu); | |
1377 | ||
1378 | rc = plpar_hcall9(H_HOME_NODE_ASSOCIATIVITY, retbuf, flags, hwcpu); | |
1379 | vphn_unpack_associativity(retbuf, associativity); | |
1380 | ||
1381 | return rc; | |
1382 | } | |
1383 | ||
1384 | static long vphn_get_associativity(unsigned long cpu, | |
1385 | unsigned int *associativity) | |
1386 | { | |
cd9d6cc7 | 1387 | long rc; |
9eff1a38 JL |
1388 | |
1389 | rc = hcall_vphn(cpu, associativity); | |
1390 | ||
1391 | switch (rc) { | |
1392 | case H_FUNCTION: | |
1393 | printk(KERN_INFO | |
1394 | "VPHN is not supported. Disabling polling...\n"); | |
1395 | stop_topology_update(); | |
1396 | break; | |
1397 | case H_HARDWARE: | |
1398 | printk(KERN_ERR | |
1399 | "hcall_vphn() experienced a hardware fault " | |
1400 | "preventing VPHN. Disabling polling...\n"); | |
1401 | stop_topology_update(); | |
1402 | } | |
1403 | ||
1404 | return rc; | |
1405 | } | |
1406 | ||
1407 | /* | |
1408 | * Update the node maps and sysfs entries for each cpu whose home node | |
79c5fceb | 1409 | * has changed. Returns 1 when the topology has changed, and 0 otherwise. |
9eff1a38 JL |
1410 | */ |
1411 | int arch_update_cpu_topology(void) | |
1412 | { | |
79c5fceb | 1413 | int cpu, nid, old_nid, changed = 0; |
9eff1a38 | 1414 | unsigned int associativity[VPHN_ASSOC_BUFSIZE] = {0}; |
8a25a2fd | 1415 | struct device *dev; |
9eff1a38 | 1416 | |
5d88aa85 | 1417 | for_each_cpu(cpu, &cpu_associativity_changes_mask) { |
9eff1a38 JL |
1418 | vphn_get_associativity(cpu, associativity); |
1419 | nid = associativity_to_nid(associativity); | |
1420 | ||
1421 | if (nid < 0 || !node_online(nid)) | |
1422 | nid = first_online_node; | |
1423 | ||
1424 | old_nid = numa_cpu_lookup_table[cpu]; | |
1425 | ||
1426 | /* Disable hotplug while we update the cpu | |
1427 | * masks and sysfs. | |
1428 | */ | |
1429 | get_online_cpus(); | |
1430 | unregister_cpu_under_node(cpu, old_nid); | |
1431 | unmap_cpu_from_node(cpu); | |
1432 | map_cpu_to_node(cpu, nid); | |
1433 | register_cpu_under_node(cpu, nid); | |
1434 | put_online_cpus(); | |
1435 | ||
8a25a2fd KS |
1436 | dev = get_cpu_device(cpu); |
1437 | if (dev) | |
1438 | kobject_uevent(&dev->kobj, KOBJ_CHANGE); | |
5d88aa85 | 1439 | cpumask_clear_cpu(cpu, &cpu_associativity_changes_mask); |
79c5fceb | 1440 | changed = 1; |
9eff1a38 JL |
1441 | } |
1442 | ||
79c5fceb | 1443 | return changed; |
9eff1a38 JL |
1444 | } |
1445 | ||
1446 | static void topology_work_fn(struct work_struct *work) | |
1447 | { | |
1448 | rebuild_sched_domains(); | |
1449 | } | |
1450 | static DECLARE_WORK(topology_work, topology_work_fn); | |
1451 | ||
1452 | void topology_schedule_update(void) | |
1453 | { | |
1454 | schedule_work(&topology_work); | |
1455 | } | |
1456 | ||
1457 | static void topology_timer_fn(unsigned long ignored) | |
1458 | { | |
5d88aa85 | 1459 | if (prrn_enabled && cpumask_weight(&cpu_associativity_changes_mask)) |
9eff1a38 | 1460 | topology_schedule_update(); |
5d88aa85 JL |
1461 | else if (vphn_enabled) { |
1462 | if (update_cpu_associativity_changes_mask() > 0) | |
1463 | topology_schedule_update(); | |
1464 | reset_topology_timer(); | |
1465 | } | |
9eff1a38 JL |
1466 | } |
1467 | static struct timer_list topology_timer = | |
1468 | TIMER_INITIALIZER(topology_timer_fn, 0, 0); | |
1469 | ||
5d88aa85 | 1470 | static void reset_topology_timer(void) |
9eff1a38 JL |
1471 | { |
1472 | topology_timer.data = 0; | |
1473 | topology_timer.expires = jiffies + 60 * HZ; | |
5d88aa85 JL |
1474 | mod_timer(&topology_timer, topology_timer.expires); |
1475 | } | |
1476 | ||
1477 | static void stage_topology_update(int core_id) | |
1478 | { | |
1479 | cpumask_or(&cpu_associativity_changes_mask, | |
1480 | &cpu_associativity_changes_mask, cpu_sibling_mask(core_id)); | |
1481 | reset_topology_timer(); | |
1482 | } | |
1483 | ||
1484 | static int dt_update_callback(struct notifier_block *nb, | |
1485 | unsigned long action, void *data) | |
1486 | { | |
1487 | struct of_prop_reconfig *update; | |
1488 | int rc = NOTIFY_DONE; | |
1489 | ||
1490 | switch (action) { | |
1491 | case OF_RECONFIG_ADD_PROPERTY: | |
1492 | case OF_RECONFIG_UPDATE_PROPERTY: | |
1493 | update = (struct of_prop_reconfig *)data; | |
1494 | if (!of_prop_cmp(update->dn->type, "cpu")) { | |
1495 | u32 core_id; | |
1496 | of_property_read_u32(update->dn, "reg", &core_id); | |
1497 | stage_topology_update(core_id); | |
1498 | rc = NOTIFY_OK; | |
1499 | } | |
1500 | break; | |
1501 | } | |
1502 | ||
1503 | return rc; | |
9eff1a38 JL |
1504 | } |
1505 | ||
5d88aa85 JL |
1506 | static struct notifier_block dt_update_nb = { |
1507 | .notifier_call = dt_update_callback, | |
1508 | }; | |
1509 | ||
9eff1a38 | 1510 | /* |
5d88aa85 | 1511 | * Start polling for associativity changes. |
9eff1a38 JL |
1512 | */ |
1513 | int start_topology_update(void) | |
1514 | { | |
1515 | int rc = 0; | |
1516 | ||
5d88aa85 JL |
1517 | if (firmware_has_feature(FW_FEATURE_PRRN)) { |
1518 | if (!prrn_enabled) { | |
1519 | prrn_enabled = 1; | |
1520 | vphn_enabled = 0; | |
1521 | rc = of_reconfig_notifier_register(&dt_update_nb); | |
1522 | } | |
1523 | } else if (0 && firmware_has_feature(FW_FEATURE_VPHN) && | |
1524 | get_lppaca()->shared_proc) { | |
1525 | /* Disabled until races with load balancing are fixed */ | |
1526 | if (!vphn_enabled) { | |
1527 | prrn_enabled = 0; | |
1528 | vphn_enabled = 1; | |
1529 | setup_cpu_associativity_change_counters(); | |
1530 | init_timer_deferrable(&topology_timer); | |
1531 | reset_topology_timer(); | |
1532 | } | |
9eff1a38 JL |
1533 | } |
1534 | ||
1535 | return rc; | |
1536 | } | |
1537 | __initcall(start_topology_update); | |
1538 | ||
1539 | /* | |
1540 | * Disable polling for VPHN associativity changes. | |
1541 | */ | |
1542 | int stop_topology_update(void) | |
1543 | { | |
5d88aa85 JL |
1544 | int rc = 0; |
1545 | ||
1546 | if (prrn_enabled) { | |
1547 | prrn_enabled = 0; | |
1548 | rc = of_reconfig_notifier_unregister(&dt_update_nb); | |
1549 | } else if (vphn_enabled) { | |
1550 | vphn_enabled = 0; | |
1551 | rc = del_timer_sync(&topology_timer); | |
1552 | } | |
1553 | ||
1554 | return rc; | |
9eff1a38 | 1555 | } |
39bf990e | 1556 | #endif /* CONFIG_PPC_SPLPAR */ |