Rusty Russell | 71ee73e | 2009-03-13 14:49:52 +1030 | [diff] [blame] | 1 | /* Common code for 32 and 64-bit NUMA */ |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 2 | #include <linux/kernel.h> |
| 3 | #include <linux/mm.h> |
| 4 | #include <linux/string.h> |
| 5 | #include <linux/init.h> |
Rusty Russell | 71ee73e | 2009-03-13 14:49:52 +1030 | [diff] [blame] | 6 | #include <linux/bootmem.h> |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 7 | #include <linux/memblock.h> |
| 8 | #include <linux/mmzone.h> |
| 9 | #include <linux/ctype.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/nodemask.h> |
| 12 | #include <linux/sched.h> |
| 13 | #include <linux/topology.h> |
| 14 | |
| 15 | #include <asm/e820.h> |
| 16 | #include <asm/proto.h> |
| 17 | #include <asm/dma.h> |
Jan Beulich | 9032160 | 2011-01-19 08:57:21 +0000 | [diff] [blame] | 18 | #include <asm/acpi.h> |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 19 | #include <asm/amd_nb.h> |
| 20 | |
| 21 | #include "numa_internal.h" |
Jan Beulich | 9032160 | 2011-01-19 08:57:21 +0000 | [diff] [blame] | 22 | |
| 23 | int __initdata numa_off; |
Tejun Heo | e6df595 | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 24 | nodemask_t numa_nodes_parsed __initdata; |
Jan Beulich | 9032160 | 2011-01-19 08:57:21 +0000 | [diff] [blame] | 25 | |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 26 | struct pglist_data *node_data[MAX_NUMNODES] __read_mostly; |
| 27 | EXPORT_SYMBOL(node_data); |
| 28 | |
| 29 | static struct numa_meminfo numa_meminfo |
| 30 | #ifndef CONFIG_MEMORY_HOTPLUG |
| 31 | __initdata |
| 32 | #endif |
| 33 | ; |
| 34 | |
| 35 | static int numa_distance_cnt; |
| 36 | static u8 *numa_distance; |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 37 | |
Jan Beulich | 9032160 | 2011-01-19 08:57:21 +0000 | [diff] [blame] | 38 | static __init int numa_setup(char *opt) |
| 39 | { |
| 40 | if (!opt) |
| 41 | return -EINVAL; |
| 42 | if (!strncmp(opt, "off", 3)) |
| 43 | numa_off = 1; |
| 44 | #ifdef CONFIG_NUMA_EMU |
| 45 | if (!strncmp(opt, "fake=", 5)) |
| 46 | numa_emu_cmdline(opt + 5); |
| 47 | #endif |
| 48 | #ifdef CONFIG_ACPI_NUMA |
| 49 | if (!strncmp(opt, "noacpi", 6)) |
| 50 | acpi_numa = -1; |
| 51 | #endif |
| 52 | return 0; |
| 53 | } |
| 54 | early_param("numa", numa_setup); |
Rusty Russell | 71ee73e | 2009-03-13 14:49:52 +1030 | [diff] [blame] | 55 | |
Rusty Russell | 71ee73e | 2009-03-13 14:49:52 +1030 | [diff] [blame] | 56 | /* |
Tejun Heo | bbc9e2f | 2011-01-23 14:37:39 +0100 | [diff] [blame] | 57 | * apicid, cpu, node mappings |
Rusty Russell | 71ee73e | 2009-03-13 14:49:52 +1030 | [diff] [blame] | 58 | */ |
Tejun Heo | bbc9e2f | 2011-01-23 14:37:39 +0100 | [diff] [blame] | 59 | s16 __apicid_to_node[MAX_LOCAL_APIC] __cpuinitdata = { |
| 60 | [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE |
| 61 | }; |
| 62 | |
Tejun Heo | 6bd2627 | 2011-05-02 14:18:52 +0200 | [diff] [blame] | 63 | int __cpuinit numa_cpu_node(int cpu) |
| 64 | { |
| 65 | int apicid = early_per_cpu(x86_cpu_to_apicid, cpu); |
| 66 | |
| 67 | if (apicid != BAD_APICID) |
| 68 | return __apicid_to_node[apicid]; |
| 69 | return NUMA_NO_NODE; |
| 70 | } |
| 71 | |
Rusty Russell | c032ef60 | 2009-03-13 14:49:53 +1030 | [diff] [blame] | 72 | cpumask_var_t node_to_cpumask_map[MAX_NUMNODES]; |
Rusty Russell | 71ee73e | 2009-03-13 14:49:52 +1030 | [diff] [blame] | 73 | EXPORT_SYMBOL(node_to_cpumask_map); |
| 74 | |
| 75 | /* |
Tejun Heo | 645a791 | 2011-01-23 14:37:40 +0100 | [diff] [blame] | 76 | * Map cpu index to node index |
| 77 | */ |
Tejun Heo | 645a791 | 2011-01-23 14:37:40 +0100 | [diff] [blame] | 78 | DEFINE_EARLY_PER_CPU(int, x86_cpu_to_node_map, NUMA_NO_NODE); |
Tejun Heo | 645a791 | 2011-01-23 14:37:40 +0100 | [diff] [blame] | 79 | EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_node_map); |
| 80 | |
| 81 | void __cpuinit numa_set_node(int cpu, int node) |
| 82 | { |
| 83 | int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map); |
| 84 | |
| 85 | /* early setting, no percpu area yet */ |
| 86 | if (cpu_to_node_map) { |
| 87 | cpu_to_node_map[cpu] = node; |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | #ifdef CONFIG_DEBUG_PER_CPU_MAPS |
| 92 | if (cpu >= nr_cpu_ids || !cpu_possible(cpu)) { |
| 93 | printk(KERN_ERR "numa_set_node: invalid cpu# (%d)\n", cpu); |
| 94 | dump_stack(); |
| 95 | return; |
| 96 | } |
| 97 | #endif |
| 98 | per_cpu(x86_cpu_to_node_map, cpu) = node; |
| 99 | |
| 100 | if (node != NUMA_NO_NODE) |
| 101 | set_cpu_numa_node(cpu, node); |
| 102 | } |
| 103 | |
| 104 | void __cpuinit numa_clear_node(int cpu) |
| 105 | { |
| 106 | numa_set_node(cpu, NUMA_NO_NODE); |
| 107 | } |
| 108 | |
| 109 | /* |
Rusty Russell | 71ee73e | 2009-03-13 14:49:52 +1030 | [diff] [blame] | 110 | * Allocate node_to_cpumask_map based on number of available nodes |
| 111 | * Requires node_possible_map to be valid. |
| 112 | * |
| 113 | * Note: node_to_cpumask() is not valid until after this is done. |
| 114 | * (Use CONFIG_DEBUG_PER_CPU_MAPS to check this.) |
| 115 | */ |
| 116 | void __init setup_node_to_cpumask_map(void) |
| 117 | { |
| 118 | unsigned int node, num = 0; |
Rusty Russell | 71ee73e | 2009-03-13 14:49:52 +1030 | [diff] [blame] | 119 | |
| 120 | /* setup nr_node_ids if not done yet */ |
| 121 | if (nr_node_ids == MAX_NUMNODES) { |
| 122 | for_each_node_mask(node, node_possible_map) |
| 123 | num = node; |
| 124 | nr_node_ids = num + 1; |
| 125 | } |
| 126 | |
| 127 | /* allocate the map */ |
Rusty Russell | c032ef60 | 2009-03-13 14:49:53 +1030 | [diff] [blame] | 128 | for (node = 0; node < nr_node_ids; node++) |
| 129 | alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]); |
Rusty Russell | 71ee73e | 2009-03-13 14:49:52 +1030 | [diff] [blame] | 130 | |
Rusty Russell | c032ef60 | 2009-03-13 14:49:53 +1030 | [diff] [blame] | 131 | /* cpumask_of_node() will now work */ |
| 132 | pr_debug("Node to cpumask map for %d nodes\n", nr_node_ids); |
Rusty Russell | 71ee73e | 2009-03-13 14:49:52 +1030 | [diff] [blame] | 133 | } |
| 134 | |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 135 | static int __init numa_add_memblk_to(int nid, u64 start, u64 end, |
| 136 | struct numa_meminfo *mi) |
| 137 | { |
| 138 | /* ignore zero length blks */ |
| 139 | if (start == end) |
| 140 | return 0; |
| 141 | |
| 142 | /* whine about and ignore invalid blks */ |
| 143 | if (start > end || nid < 0 || nid >= MAX_NUMNODES) { |
| 144 | pr_warning("NUMA: Warning: invalid memblk node %d (%Lx-%Lx)\n", |
| 145 | nid, start, end); |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | if (mi->nr_blks >= NR_NODE_MEMBLKS) { |
| 150 | pr_err("NUMA: too many memblk ranges\n"); |
| 151 | return -EINVAL; |
| 152 | } |
| 153 | |
| 154 | mi->blk[mi->nr_blks].start = start; |
| 155 | mi->blk[mi->nr_blks].end = end; |
| 156 | mi->blk[mi->nr_blks].nid = nid; |
| 157 | mi->nr_blks++; |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * numa_remove_memblk_from - Remove one numa_memblk from a numa_meminfo |
| 163 | * @idx: Index of memblk to remove |
| 164 | * @mi: numa_meminfo to remove memblk from |
| 165 | * |
| 166 | * Remove @idx'th numa_memblk from @mi by shifting @mi->blk[] and |
| 167 | * decrementing @mi->nr_blks. |
| 168 | */ |
| 169 | void __init numa_remove_memblk_from(int idx, struct numa_meminfo *mi) |
| 170 | { |
| 171 | mi->nr_blks--; |
| 172 | memmove(&mi->blk[idx], &mi->blk[idx + 1], |
| 173 | (mi->nr_blks - idx) * sizeof(mi->blk[0])); |
| 174 | } |
| 175 | |
Tejun Heo | 744baba | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 176 | #ifdef CONFIG_X86_64 |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 177 | /** |
| 178 | * numa_add_memblk - Add one numa_memblk to numa_meminfo |
| 179 | * @nid: NUMA node ID of the new memblk |
| 180 | * @start: Start address of the new memblk |
| 181 | * @end: End address of the new memblk |
| 182 | * |
| 183 | * Add a new memblk to the default numa_meminfo. |
| 184 | * |
| 185 | * RETURNS: |
| 186 | * 0 on success, -errno on failure. |
| 187 | */ |
| 188 | int __init numa_add_memblk(int nid, u64 start, u64 end) |
| 189 | { |
| 190 | return numa_add_memblk_to(nid, start, end, &numa_meminfo); |
| 191 | } |
Tejun Heo | 744baba | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 192 | #endif |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 193 | |
| 194 | /* Initialize bootmem allocator for a node */ |
Tejun Heo | 38f3e1c | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 195 | static void __init setup_node_bootmem(int nid, u64 start, u64 end) |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 196 | { |
Tejun Heo | 38f3e1c | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 197 | const u64 nd_low = PFN_PHYS(MAX_DMA_PFN); |
| 198 | const u64 nd_high = PFN_PHYS(max_pfn_mapped); |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 199 | const size_t nd_size = roundup(sizeof(pg_data_t), PAGE_SIZE); |
Tejun Heo | 7888e96 | 2011-05-02 14:18:54 +0200 | [diff] [blame^] | 200 | bool remapped = false; |
Tejun Heo | 38f3e1c | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 201 | u64 nd_pa; |
Tejun Heo | 7888e96 | 2011-05-02 14:18:54 +0200 | [diff] [blame^] | 202 | void *nd; |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 203 | int tnid; |
| 204 | |
| 205 | /* |
| 206 | * Don't confuse VM with a node that doesn't have the |
| 207 | * minimum amount of memory: |
| 208 | */ |
| 209 | if (end && (end - start) < NODE_MIN_SIZE) |
| 210 | return; |
| 211 | |
Tejun Heo | 7888e96 | 2011-05-02 14:18:54 +0200 | [diff] [blame^] | 212 | /* initialize remap allocator before aligning to ZONE_ALIGN */ |
| 213 | init_alloc_remap(nid, start, end); |
| 214 | |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 215 | start = roundup(start, ZONE_ALIGN); |
| 216 | |
Tejun Heo | 38f3e1c | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 217 | printk(KERN_INFO "Initmem setup node %d %016Lx-%016Lx\n", |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 218 | nid, start, end); |
| 219 | |
| 220 | /* |
Tejun Heo | 7888e96 | 2011-05-02 14:18:54 +0200 | [diff] [blame^] | 221 | * Allocate node data. Try remap allocator first, node-local |
| 222 | * memory and then any node. Never allocate in DMA zone. |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 223 | */ |
Tejun Heo | 7888e96 | 2011-05-02 14:18:54 +0200 | [diff] [blame^] | 224 | nd = alloc_remap(nid, nd_size); |
| 225 | if (nd) { |
| 226 | nd_pa = __pa(nd); |
| 227 | remapped = true; |
| 228 | } else { |
| 229 | nd_pa = memblock_x86_find_in_range_node(nid, nd_low, nd_high, |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 230 | nd_size, SMP_CACHE_BYTES); |
Tejun Heo | 7888e96 | 2011-05-02 14:18:54 +0200 | [diff] [blame^] | 231 | if (nd_pa == MEMBLOCK_ERROR) |
| 232 | nd_pa = memblock_find_in_range(nd_low, nd_high, |
| 233 | nd_size, SMP_CACHE_BYTES); |
| 234 | if (nd_pa == MEMBLOCK_ERROR) { |
| 235 | pr_err("Cannot find %zu bytes in node %d\n", |
| 236 | nd_size, nid); |
| 237 | return; |
| 238 | } |
| 239 | memblock_x86_reserve_range(nd_pa, nd_pa + nd_size, "NODE_DATA"); |
| 240 | nd = __va(nd_pa); |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 241 | } |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 242 | |
| 243 | /* report and initialize */ |
Tejun Heo | 7888e96 | 2011-05-02 14:18:54 +0200 | [diff] [blame^] | 244 | printk(KERN_INFO " NODE_DATA [%016Lx - %016Lx]%s\n", |
| 245 | nd_pa, nd_pa + nd_size - 1, remapped ? " (remapped)" : ""); |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 246 | tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT); |
Tejun Heo | 7888e96 | 2011-05-02 14:18:54 +0200 | [diff] [blame^] | 247 | if (!remapped && tnid != nid) |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 248 | printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nid, tnid); |
| 249 | |
Tejun Heo | 7888e96 | 2011-05-02 14:18:54 +0200 | [diff] [blame^] | 250 | node_data[nid] = nd; |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 251 | memset(NODE_DATA(nid), 0, sizeof(pg_data_t)); |
| 252 | NODE_DATA(nid)->node_id = nid; |
| 253 | NODE_DATA(nid)->node_start_pfn = start >> PAGE_SHIFT; |
| 254 | NODE_DATA(nid)->node_spanned_pages = (end - start) >> PAGE_SHIFT; |
| 255 | |
| 256 | node_set_online(nid); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * numa_cleanup_meminfo - Cleanup a numa_meminfo |
| 261 | * @mi: numa_meminfo to clean up |
| 262 | * |
| 263 | * Sanitize @mi by merging and removing unncessary memblks. Also check for |
| 264 | * conflicts and clear unused memblks. |
| 265 | * |
| 266 | * RETURNS: |
| 267 | * 0 on success, -errno on failure. |
| 268 | */ |
| 269 | int __init numa_cleanup_meminfo(struct numa_meminfo *mi) |
| 270 | { |
| 271 | const u64 low = 0; |
Tejun Heo | 38f3e1c | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 272 | const u64 high = PFN_PHYS(max_pfn); |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 273 | int i, j, k; |
| 274 | |
| 275 | for (i = 0; i < mi->nr_blks; i++) { |
| 276 | struct numa_memblk *bi = &mi->blk[i]; |
| 277 | |
| 278 | /* make sure all blocks are inside the limits */ |
| 279 | bi->start = max(bi->start, low); |
| 280 | bi->end = min(bi->end, high); |
| 281 | |
| 282 | /* and there's no empty block */ |
| 283 | if (bi->start >= bi->end) { |
| 284 | numa_remove_memblk_from(i--, mi); |
| 285 | continue; |
| 286 | } |
| 287 | |
| 288 | for (j = i + 1; j < mi->nr_blks; j++) { |
| 289 | struct numa_memblk *bj = &mi->blk[j]; |
Tejun Heo | 38f3e1c | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 290 | u64 start, end; |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 291 | |
| 292 | /* |
| 293 | * See whether there are overlapping blocks. Whine |
| 294 | * about but allow overlaps of the same nid. They |
| 295 | * will be merged below. |
| 296 | */ |
| 297 | if (bi->end > bj->start && bi->start < bj->end) { |
| 298 | if (bi->nid != bj->nid) { |
| 299 | pr_err("NUMA: node %d (%Lx-%Lx) overlaps with node %d (%Lx-%Lx)\n", |
| 300 | bi->nid, bi->start, bi->end, |
| 301 | bj->nid, bj->start, bj->end); |
| 302 | return -EINVAL; |
| 303 | } |
| 304 | pr_warning("NUMA: Warning: node %d (%Lx-%Lx) overlaps with itself (%Lx-%Lx)\n", |
| 305 | bi->nid, bi->start, bi->end, |
| 306 | bj->start, bj->end); |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * Join together blocks on the same node, holes |
| 311 | * between which don't overlap with memory on other |
| 312 | * nodes. |
| 313 | */ |
| 314 | if (bi->nid != bj->nid) |
| 315 | continue; |
| 316 | start = max(min(bi->start, bj->start), low); |
| 317 | end = min(max(bi->end, bj->end), high); |
| 318 | for (k = 0; k < mi->nr_blks; k++) { |
| 319 | struct numa_memblk *bk = &mi->blk[k]; |
| 320 | |
| 321 | if (bi->nid == bk->nid) |
| 322 | continue; |
| 323 | if (start < bk->end && end > bk->start) |
| 324 | break; |
| 325 | } |
| 326 | if (k < mi->nr_blks) |
| 327 | continue; |
Tejun Heo | 38f3e1c | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 328 | printk(KERN_INFO "NUMA: Node %d [%Lx,%Lx) + [%Lx,%Lx) -> [%Lx,%Lx)\n", |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 329 | bi->nid, bi->start, bi->end, bj->start, bj->end, |
| 330 | start, end); |
| 331 | bi->start = start; |
| 332 | bi->end = end; |
| 333 | numa_remove_memblk_from(j--, mi); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | for (i = mi->nr_blks; i < ARRAY_SIZE(mi->blk); i++) { |
| 338 | mi->blk[i].start = mi->blk[i].end = 0; |
| 339 | mi->blk[i].nid = NUMA_NO_NODE; |
| 340 | } |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | * Set nodes, which have memory in @mi, in *@nodemask. |
| 347 | */ |
| 348 | static void __init numa_nodemask_from_meminfo(nodemask_t *nodemask, |
| 349 | const struct numa_meminfo *mi) |
| 350 | { |
| 351 | int i; |
| 352 | |
| 353 | for (i = 0; i < ARRAY_SIZE(mi->blk); i++) |
| 354 | if (mi->blk[i].start != mi->blk[i].end && |
| 355 | mi->blk[i].nid != NUMA_NO_NODE) |
| 356 | node_set(mi->blk[i].nid, *nodemask); |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * numa_reset_distance - Reset NUMA distance table |
| 361 | * |
| 362 | * The current table is freed. The next numa_set_distance() call will |
| 363 | * create a new one. |
| 364 | */ |
| 365 | void __init numa_reset_distance(void) |
| 366 | { |
| 367 | size_t size = numa_distance_cnt * numa_distance_cnt * sizeof(numa_distance[0]); |
| 368 | |
| 369 | /* numa_distance could be 1LU marking allocation failure, test cnt */ |
| 370 | if (numa_distance_cnt) |
| 371 | memblock_x86_free_range(__pa(numa_distance), |
| 372 | __pa(numa_distance) + size); |
| 373 | numa_distance_cnt = 0; |
| 374 | numa_distance = NULL; /* enable table creation */ |
| 375 | } |
| 376 | |
| 377 | static int __init numa_alloc_distance(void) |
| 378 | { |
| 379 | nodemask_t nodes_parsed; |
| 380 | size_t size; |
| 381 | int i, j, cnt = 0; |
| 382 | u64 phys; |
| 383 | |
| 384 | /* size the new table and allocate it */ |
| 385 | nodes_parsed = numa_nodes_parsed; |
| 386 | numa_nodemask_from_meminfo(&nodes_parsed, &numa_meminfo); |
| 387 | |
| 388 | for_each_node_mask(i, nodes_parsed) |
| 389 | cnt = i; |
| 390 | cnt++; |
| 391 | size = cnt * cnt * sizeof(numa_distance[0]); |
| 392 | |
Tejun Heo | 38f3e1c | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 393 | phys = memblock_find_in_range(0, PFN_PHYS(max_pfn_mapped), |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 394 | size, PAGE_SIZE); |
| 395 | if (phys == MEMBLOCK_ERROR) { |
| 396 | pr_warning("NUMA: Warning: can't allocate distance table!\n"); |
| 397 | /* don't retry until explicitly reset */ |
| 398 | numa_distance = (void *)1LU; |
| 399 | return -ENOMEM; |
| 400 | } |
| 401 | memblock_x86_reserve_range(phys, phys + size, "NUMA DIST"); |
| 402 | |
| 403 | numa_distance = __va(phys); |
| 404 | numa_distance_cnt = cnt; |
| 405 | |
| 406 | /* fill with the default distances */ |
| 407 | for (i = 0; i < cnt; i++) |
| 408 | for (j = 0; j < cnt; j++) |
| 409 | numa_distance[i * cnt + j] = i == j ? |
| 410 | LOCAL_DISTANCE : REMOTE_DISTANCE; |
| 411 | printk(KERN_DEBUG "NUMA: Initialized distance table, cnt=%d\n", cnt); |
| 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
Tejun Heo | 744baba | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 416 | #ifdef CONFIG_X86_64 |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 417 | /** |
| 418 | * numa_set_distance - Set NUMA distance from one NUMA to another |
| 419 | * @from: the 'from' node to set distance |
| 420 | * @to: the 'to' node to set distance |
| 421 | * @distance: NUMA distance |
| 422 | * |
| 423 | * Set the distance from node @from to @to to @distance. If distance table |
| 424 | * doesn't exist, one which is large enough to accommodate all the currently |
| 425 | * known nodes will be created. |
| 426 | * |
| 427 | * If such table cannot be allocated, a warning is printed and further |
| 428 | * calls are ignored until the distance table is reset with |
| 429 | * numa_reset_distance(). |
| 430 | * |
| 431 | * If @from or @to is higher than the highest known node at the time of |
| 432 | * table creation or @distance doesn't make sense, the call is ignored. |
| 433 | * This is to allow simplification of specific NUMA config implementations. |
| 434 | */ |
| 435 | void __init numa_set_distance(int from, int to, int distance) |
| 436 | { |
| 437 | if (!numa_distance && numa_alloc_distance() < 0) |
| 438 | return; |
| 439 | |
| 440 | if (from >= numa_distance_cnt || to >= numa_distance_cnt) { |
| 441 | printk_once(KERN_DEBUG "NUMA: Debug: distance out of bound, from=%d to=%d distance=%d\n", |
| 442 | from, to, distance); |
| 443 | return; |
| 444 | } |
| 445 | |
| 446 | if ((u8)distance != distance || |
| 447 | (from == to && distance != LOCAL_DISTANCE)) { |
| 448 | pr_warn_once("NUMA: Warning: invalid distance parameter, from=%d to=%d distance=%d\n", |
| 449 | from, to, distance); |
| 450 | return; |
| 451 | } |
| 452 | |
| 453 | numa_distance[from * numa_distance_cnt + to] = distance; |
| 454 | } |
Tejun Heo | 744baba | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 455 | #endif |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 456 | |
| 457 | int __node_distance(int from, int to) |
| 458 | { |
| 459 | if (from >= numa_distance_cnt || to >= numa_distance_cnt) |
| 460 | return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE; |
| 461 | return numa_distance[from * numa_distance_cnt + to]; |
| 462 | } |
| 463 | EXPORT_SYMBOL(__node_distance); |
| 464 | |
| 465 | /* |
| 466 | * Sanity check to catch more bad NUMA configurations (they are amazingly |
| 467 | * common). Make sure the nodes cover all memory. |
| 468 | */ |
| 469 | static bool __init numa_meminfo_cover_memory(const struct numa_meminfo *mi) |
| 470 | { |
Tejun Heo | 38f3e1c | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 471 | u64 numaram, e820ram; |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 472 | int i; |
| 473 | |
| 474 | numaram = 0; |
| 475 | for (i = 0; i < mi->nr_blks; i++) { |
Tejun Heo | 38f3e1c | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 476 | u64 s = mi->blk[i].start >> PAGE_SHIFT; |
| 477 | u64 e = mi->blk[i].end >> PAGE_SHIFT; |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 478 | numaram += e - s; |
| 479 | numaram -= __absent_pages_in_range(mi->blk[i].nid, s, e); |
Tejun Heo | 38f3e1c | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 480 | if ((s64)numaram < 0) |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 481 | numaram = 0; |
| 482 | } |
| 483 | |
| 484 | e820ram = max_pfn - (memblock_x86_hole_size(0, |
Tejun Heo | 38f3e1c | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 485 | PFN_PHYS(max_pfn)) >> PAGE_SHIFT); |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 486 | /* We seem to lose 3 pages somewhere. Allow 1M of slack. */ |
Tejun Heo | 38f3e1c | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 487 | if ((s64)(e820ram - numaram) >= (1 << (20 - PAGE_SHIFT))) { |
| 488 | printk(KERN_ERR "NUMA: nodes only cover %LuMB of your %LuMB e820 RAM. Not used.\n", |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 489 | (numaram << PAGE_SHIFT) >> 20, |
| 490 | (e820ram << PAGE_SHIFT) >> 20); |
| 491 | return false; |
| 492 | } |
| 493 | return true; |
| 494 | } |
| 495 | |
| 496 | static int __init numa_register_memblks(struct numa_meminfo *mi) |
| 497 | { |
| 498 | int i, nid; |
| 499 | |
| 500 | /* Account for nodes with cpus and no memory */ |
| 501 | node_possible_map = numa_nodes_parsed; |
| 502 | numa_nodemask_from_meminfo(&node_possible_map, mi); |
| 503 | if (WARN_ON(nodes_empty(node_possible_map))) |
| 504 | return -EINVAL; |
| 505 | |
| 506 | for (i = 0; i < mi->nr_blks; i++) |
| 507 | memblock_x86_register_active_regions(mi->blk[i].nid, |
| 508 | mi->blk[i].start >> PAGE_SHIFT, |
| 509 | mi->blk[i].end >> PAGE_SHIFT); |
| 510 | |
| 511 | /* for out of order entries */ |
| 512 | sort_node_map(); |
| 513 | if (!numa_meminfo_cover_memory(mi)) |
| 514 | return -EINVAL; |
| 515 | |
| 516 | /* Finally register nodes. */ |
| 517 | for_each_node_mask(nid, node_possible_map) { |
Tejun Heo | 38f3e1c | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 518 | u64 start = PFN_PHYS(max_pfn); |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 519 | u64 end = 0; |
| 520 | |
| 521 | for (i = 0; i < mi->nr_blks; i++) { |
| 522 | if (nid != mi->blk[i].nid) |
| 523 | continue; |
| 524 | start = min(mi->blk[i].start, start); |
| 525 | end = max(mi->blk[i].end, end); |
| 526 | } |
| 527 | |
| 528 | if (start < end) |
| 529 | setup_node_bootmem(nid, start, end); |
| 530 | } |
| 531 | |
| 532 | return 0; |
| 533 | } |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 534 | |
Tejun Heo | 8db78cc | 2011-01-23 14:37:42 +0100 | [diff] [blame] | 535 | /* |
| 536 | * There are unfortunately some poorly designed mainboards around that |
| 537 | * only connect memory to a single CPU. This breaks the 1:1 cpu->node |
| 538 | * mapping. To avoid this fill in the mapping for all possible CPUs, |
| 539 | * as the number of CPUs is not known yet. We round robin the existing |
| 540 | * nodes. |
| 541 | */ |
| 542 | void __init numa_init_array(void) |
| 543 | { |
| 544 | int rr, i; |
| 545 | |
| 546 | rr = first_node(node_online_map); |
| 547 | for (i = 0; i < nr_cpu_ids; i++) { |
| 548 | if (early_cpu_to_node(i) != NUMA_NO_NODE) |
| 549 | continue; |
| 550 | numa_set_node(i, rr); |
| 551 | rr = next_node(rr, node_online_map); |
| 552 | if (rr == MAX_NUMNODES) |
| 553 | rr = first_node(node_online_map); |
| 554 | } |
| 555 | } |
| 556 | |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 557 | static int __init numa_init(int (*init_func)(void)) |
| 558 | { |
| 559 | int i; |
| 560 | int ret; |
| 561 | |
| 562 | for (i = 0; i < MAX_LOCAL_APIC; i++) |
| 563 | set_apicid_to_node(i, NUMA_NO_NODE); |
| 564 | |
| 565 | nodes_clear(numa_nodes_parsed); |
| 566 | nodes_clear(node_possible_map); |
| 567 | nodes_clear(node_online_map); |
| 568 | memset(&numa_meminfo, 0, sizeof(numa_meminfo)); |
| 569 | remove_all_active_ranges(); |
| 570 | numa_reset_distance(); |
| 571 | |
| 572 | ret = init_func(); |
| 573 | if (ret < 0) |
| 574 | return ret; |
| 575 | ret = numa_cleanup_meminfo(&numa_meminfo); |
| 576 | if (ret < 0) |
| 577 | return ret; |
| 578 | |
| 579 | numa_emulation(&numa_meminfo, numa_distance_cnt); |
| 580 | |
| 581 | ret = numa_register_memblks(&numa_meminfo); |
| 582 | if (ret < 0) |
| 583 | return ret; |
| 584 | |
| 585 | for (i = 0; i < nr_cpu_ids; i++) { |
| 586 | int nid = early_cpu_to_node(i); |
| 587 | |
| 588 | if (nid == NUMA_NO_NODE) |
| 589 | continue; |
| 590 | if (!node_online(nid)) |
| 591 | numa_clear_node(i); |
| 592 | } |
| 593 | numa_init_array(); |
| 594 | return 0; |
| 595 | } |
| 596 | |
| 597 | /** |
| 598 | * dummy_numa_init - Fallback dummy NUMA init |
| 599 | * |
| 600 | * Used if there's no underlying NUMA architecture, NUMA initialization |
| 601 | * fails, or NUMA is disabled on the command line. |
| 602 | * |
| 603 | * Must online at least one node and add memory blocks that cover all |
| 604 | * allowed memory. This function must not fail. |
| 605 | */ |
| 606 | static int __init dummy_numa_init(void) |
| 607 | { |
| 608 | printk(KERN_INFO "%s\n", |
| 609 | numa_off ? "NUMA turned off" : "No NUMA configuration found"); |
Tejun Heo | 38f3e1c | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 610 | printk(KERN_INFO "Faking a node at %016Lx-%016Lx\n", |
| 611 | 0LLU, PFN_PHYS(max_pfn)); |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 612 | |
| 613 | node_set(0, numa_nodes_parsed); |
Tejun Heo | 38f3e1c | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 614 | numa_add_memblk(0, 0, PFN_PHYS(max_pfn)); |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 615 | |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | /** |
| 620 | * x86_numa_init - Initialize NUMA |
| 621 | * |
| 622 | * Try each configured NUMA initialization method until one succeeds. The |
| 623 | * last fallback is dummy single node config encomapssing whole memory and |
| 624 | * never fails. |
| 625 | */ |
| 626 | void __init x86_numa_init(void) |
| 627 | { |
| 628 | if (!numa_off) { |
| 629 | #ifdef CONFIG_ACPI_NUMA |
| 630 | if (!numa_init(x86_acpi_numa_init)) |
| 631 | return; |
| 632 | #endif |
| 633 | #ifdef CONFIG_AMD_NUMA |
| 634 | if (!numa_init(amd_numa_init)) |
| 635 | return; |
| 636 | #endif |
| 637 | } |
| 638 | |
| 639 | numa_init(dummy_numa_init); |
| 640 | } |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 641 | |
Tejun Heo | 8db78cc | 2011-01-23 14:37:42 +0100 | [diff] [blame] | 642 | static __init int find_near_online_node(int node) |
| 643 | { |
| 644 | int n, val; |
| 645 | int min_val = INT_MAX; |
| 646 | int best_node = -1; |
| 647 | |
| 648 | for_each_online_node(n) { |
| 649 | val = node_distance(node, n); |
| 650 | |
| 651 | if (val < min_val) { |
| 652 | min_val = val; |
| 653 | best_node = n; |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | return best_node; |
| 658 | } |
| 659 | |
| 660 | /* |
| 661 | * Setup early cpu_to_node. |
| 662 | * |
| 663 | * Populate cpu_to_node[] only if x86_cpu_to_apicid[], |
| 664 | * and apicid_to_node[] tables have valid entries for a CPU. |
| 665 | * This means we skip cpu_to_node[] initialisation for NUMA |
| 666 | * emulation and faking node case (when running a kernel compiled |
| 667 | * for NUMA on a non NUMA box), which is OK as cpu_to_node[] |
| 668 | * is already initialized in a round robin manner at numa_init_array, |
| 669 | * prior to this call, and this initialization is good enough |
| 670 | * for the fake NUMA cases. |
| 671 | * |
| 672 | * Called before the per_cpu areas are setup. |
| 673 | */ |
| 674 | void __init init_cpu_to_node(void) |
| 675 | { |
| 676 | int cpu; |
| 677 | u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid); |
| 678 | |
| 679 | BUG_ON(cpu_to_apicid == NULL); |
| 680 | |
| 681 | for_each_possible_cpu(cpu) { |
| 682 | int node = numa_cpu_node(cpu); |
| 683 | |
| 684 | if (node == NUMA_NO_NODE) |
| 685 | continue; |
| 686 | if (!node_online(node)) |
| 687 | node = find_near_online_node(node); |
| 688 | numa_set_node(cpu, node); |
| 689 | } |
| 690 | } |
| 691 | |
Tejun Heo | de2d944 | 2011-01-23 14:37:41 +0100 | [diff] [blame] | 692 | #ifndef CONFIG_DEBUG_PER_CPU_MAPS |
| 693 | |
| 694 | # ifndef CONFIG_NUMA_EMU |
| 695 | void __cpuinit numa_add_cpu(int cpu) |
| 696 | { |
| 697 | cpumask_set_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]); |
| 698 | } |
| 699 | |
| 700 | void __cpuinit numa_remove_cpu(int cpu) |
| 701 | { |
| 702 | cpumask_clear_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]); |
| 703 | } |
| 704 | # endif /* !CONFIG_NUMA_EMU */ |
| 705 | |
| 706 | #else /* !CONFIG_DEBUG_PER_CPU_MAPS */ |
Tejun Heo | 645a791 | 2011-01-23 14:37:40 +0100 | [diff] [blame] | 707 | |
| 708 | int __cpu_to_node(int cpu) |
| 709 | { |
| 710 | if (early_per_cpu_ptr(x86_cpu_to_node_map)) { |
| 711 | printk(KERN_WARNING |
| 712 | "cpu_to_node(%d): usage too early!\n", cpu); |
| 713 | dump_stack(); |
| 714 | return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu]; |
| 715 | } |
| 716 | return per_cpu(x86_cpu_to_node_map, cpu); |
| 717 | } |
| 718 | EXPORT_SYMBOL(__cpu_to_node); |
| 719 | |
| 720 | /* |
| 721 | * Same function as cpu_to_node() but used if called before the |
| 722 | * per_cpu areas are setup. |
| 723 | */ |
| 724 | int early_cpu_to_node(int cpu) |
| 725 | { |
| 726 | if (early_per_cpu_ptr(x86_cpu_to_node_map)) |
| 727 | return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu]; |
| 728 | |
| 729 | if (!cpu_possible(cpu)) { |
| 730 | printk(KERN_WARNING |
| 731 | "early_cpu_to_node(%d): no per_cpu area!\n", cpu); |
| 732 | dump_stack(); |
| 733 | return NUMA_NO_NODE; |
| 734 | } |
| 735 | return per_cpu(x86_cpu_to_node_map, cpu); |
| 736 | } |
| 737 | |
David Rientjes | 7a6c654 | 2011-04-20 19:19:13 -0700 | [diff] [blame] | 738 | void debug_cpumask_set_cpu(int cpu, int node, bool enable) |
Tejun Heo | de2d944 | 2011-01-23 14:37:41 +0100 | [diff] [blame] | 739 | { |
Tejun Heo | de2d944 | 2011-01-23 14:37:41 +0100 | [diff] [blame] | 740 | struct cpumask *mask; |
| 741 | char buf[64]; |
| 742 | |
David Rientjes | 14392fd | 2011-02-07 14:08:53 -0800 | [diff] [blame] | 743 | if (node == NUMA_NO_NODE) { |
| 744 | /* early_cpu_to_node() already emits a warning and trace */ |
David Rientjes | 7a6c654 | 2011-04-20 19:19:13 -0700 | [diff] [blame] | 745 | return; |
David Rientjes | 14392fd | 2011-02-07 14:08:53 -0800 | [diff] [blame] | 746 | } |
Tejun Heo | de2d944 | 2011-01-23 14:37:41 +0100 | [diff] [blame] | 747 | mask = node_to_cpumask_map[node]; |
| 748 | if (!mask) { |
| 749 | pr_err("node_to_cpumask_map[%i] NULL\n", node); |
| 750 | dump_stack(); |
Tejun Heo | de2d944 | 2011-01-23 14:37:41 +0100 | [diff] [blame] | 751 | return; |
David Rientjes | 7a6c654 | 2011-04-20 19:19:13 -0700 | [diff] [blame] | 752 | } |
Tejun Heo | de2d944 | 2011-01-23 14:37:41 +0100 | [diff] [blame] | 753 | |
| 754 | if (enable) |
| 755 | cpumask_set_cpu(cpu, mask); |
| 756 | else |
| 757 | cpumask_clear_cpu(cpu, mask); |
David Rientjes | 7a6c654 | 2011-04-20 19:19:13 -0700 | [diff] [blame] | 758 | |
| 759 | cpulist_scnprintf(buf, sizeof(buf), mask); |
| 760 | printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n", |
| 761 | enable ? "numa_add_cpu" : "numa_remove_cpu", |
| 762 | cpu, node, buf); |
| 763 | return; |
| 764 | } |
| 765 | |
| 766 | # ifndef CONFIG_NUMA_EMU |
| 767 | static void __cpuinit numa_set_cpumask(int cpu, bool enable) |
| 768 | { |
| 769 | debug_cpumask_set_cpu(cpu, early_cpu_to_node(cpu), enable); |
Tejun Heo | de2d944 | 2011-01-23 14:37:41 +0100 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | void __cpuinit numa_add_cpu(int cpu) |
| 773 | { |
David Rientjes | 7a6c654 | 2011-04-20 19:19:13 -0700 | [diff] [blame] | 774 | numa_set_cpumask(cpu, true); |
Tejun Heo | de2d944 | 2011-01-23 14:37:41 +0100 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | void __cpuinit numa_remove_cpu(int cpu) |
| 778 | { |
David Rientjes | 7a6c654 | 2011-04-20 19:19:13 -0700 | [diff] [blame] | 779 | numa_set_cpumask(cpu, false); |
Tejun Heo | de2d944 | 2011-01-23 14:37:41 +0100 | [diff] [blame] | 780 | } |
| 781 | # endif /* !CONFIG_NUMA_EMU */ |
| 782 | |
Rusty Russell | 71ee73e | 2009-03-13 14:49:52 +1030 | [diff] [blame] | 783 | /* |
| 784 | * Returns a pointer to the bitmask of CPUs on Node 'node'. |
| 785 | */ |
Rusty Russell | 73e907d | 2009-03-13 14:49:57 +1030 | [diff] [blame] | 786 | const struct cpumask *cpumask_of_node(int node) |
Rusty Russell | 71ee73e | 2009-03-13 14:49:52 +1030 | [diff] [blame] | 787 | { |
Rusty Russell | 71ee73e | 2009-03-13 14:49:52 +1030 | [diff] [blame] | 788 | if (node >= nr_node_ids) { |
| 789 | printk(KERN_WARNING |
| 790 | "cpumask_of_node(%d): node > nr_node_ids(%d)\n", |
| 791 | node, nr_node_ids); |
| 792 | dump_stack(); |
| 793 | return cpu_none_mask; |
| 794 | } |
Rusty Russell | c032ef60 | 2009-03-13 14:49:53 +1030 | [diff] [blame] | 795 | if (node_to_cpumask_map[node] == NULL) { |
| 796 | printk(KERN_WARNING |
| 797 | "cpumask_of_node(%d): no node_to_cpumask_map!\n", |
| 798 | node); |
| 799 | dump_stack(); |
| 800 | return cpu_online_mask; |
| 801 | } |
Rusty Russell | 0b96625 | 2009-03-13 23:42:42 +1030 | [diff] [blame] | 802 | return node_to_cpumask_map[node]; |
Rusty Russell | 71ee73e | 2009-03-13 14:49:52 +1030 | [diff] [blame] | 803 | } |
| 804 | EXPORT_SYMBOL(cpumask_of_node); |
Tejun Heo | 645a791 | 2011-01-23 14:37:40 +0100 | [diff] [blame] | 805 | |
Tejun Heo | de2d944 | 2011-01-23 14:37:41 +0100 | [diff] [blame] | 806 | #endif /* !CONFIG_DEBUG_PER_CPU_MAPS */ |
Tejun Heo | a4106ea | 2011-05-02 14:18:53 +0200 | [diff] [blame] | 807 | |
| 808 | #if defined(CONFIG_X86_64) && defined(CONFIG_MEMORY_HOTPLUG) |
| 809 | int memory_add_physaddr_to_nid(u64 start) |
| 810 | { |
| 811 | struct numa_meminfo *mi = &numa_meminfo; |
| 812 | int nid = mi->blk[0].nid; |
| 813 | int i; |
| 814 | |
| 815 | for (i = 0; i < mi->nr_blks; i++) |
| 816 | if (mi->blk[i].start <= start && mi->blk[i].end > start) |
| 817 | nid = mi->blk[i].nid; |
| 818 | return nid; |
| 819 | } |
| 820 | EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid); |
| 821 | #endif |