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