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