Commit | Line | Data |
---|---|---|
e3cfe529 | 1 | /* |
1da177e4 LT |
2 | * Generic VM initialization for x86-64 NUMA setups. |
3 | * Copyright 2002,2003 Andi Kleen, SuSE Labs. | |
e3cfe529 | 4 | */ |
1da177e4 LT |
5 | #include <linux/kernel.h> |
6 | #include <linux/mm.h> | |
7 | #include <linux/string.h> | |
8 | #include <linux/init.h> | |
9 | #include <linux/bootmem.h> | |
72d7c3b3 | 10 | #include <linux/memblock.h> |
1da177e4 LT |
11 | #include <linux/mmzone.h> |
12 | #include <linux/ctype.h> | |
13 | #include <linux/module.h> | |
14 | #include <linux/nodemask.h> | |
3cc87e3f | 15 | #include <linux/sched.h> |
1da177e4 LT |
16 | |
17 | #include <asm/e820.h> | |
18 | #include <asm/proto.h> | |
19 | #include <asm/dma.h> | |
20 | #include <asm/numa.h> | |
21 | #include <asm/acpi.h> | |
c9ff0342 | 22 | #include <asm/k8.h> |
1da177e4 | 23 | |
6c231b7b | 24 | struct pglist_data *node_data[MAX_NUMNODES] __read_mostly; |
e3cfe529 TG |
25 | EXPORT_SYMBOL(node_data); |
26 | ||
dcf36bfa | 27 | struct memnode memnode; |
1da177e4 | 28 | |
43238382 | 29 | s16 apicid_to_node[MAX_LOCAL_APIC] __cpuinitdata = { |
e3cfe529 | 30 | [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE |
3f098c26 | 31 | }; |
e3cfe529 | 32 | |
1da177e4 | 33 | int numa_off __initdata; |
864fc31e TG |
34 | static unsigned long __initdata nodemap_addr; |
35 | static unsigned long __initdata nodemap_size; | |
1da177e4 | 36 | |
6470aff6 BG |
37 | /* |
38 | * Map cpu index to node index | |
39 | */ | |
40 | DEFINE_EARLY_PER_CPU(int, x86_cpu_to_node_map, NUMA_NO_NODE); | |
41 | EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_node_map); | |
42 | ||
529a3404 ED |
43 | /* |
44 | * Given a shift value, try to populate memnodemap[] | |
45 | * Returns : | |
46 | * 1 if OK | |
47 | * 0 if memnodmap[] too small (of shift too small) | |
48 | * -1 if node overlap or lost ram (shift too big) | |
49 | */ | |
e3cfe529 | 50 | static int __init populate_memnodemap(const struct bootnode *nodes, |
6ec6e0d9 | 51 | int numnodes, int shift, int *nodeids) |
1da177e4 | 52 | { |
529a3404 | 53 | unsigned long addr, end; |
e3cfe529 | 54 | int i, res = -1; |
b684664f | 55 | |
43238382 | 56 | memset(memnodemap, 0xff, sizeof(s16)*memnodemapsize); |
b684664f | 57 | for (i = 0; i < numnodes; i++) { |
529a3404 ED |
58 | addr = nodes[i].start; |
59 | end = nodes[i].end; | |
60 | if (addr >= end) | |
b684664f | 61 | continue; |
076422d2 | 62 | if ((end >> shift) >= memnodemapsize) |
529a3404 ED |
63 | return 0; |
64 | do { | |
43238382 | 65 | if (memnodemap[addr >> shift] != NUMA_NO_NODE) |
b684664f | 66 | return -1; |
6ec6e0d9 SS |
67 | |
68 | if (!nodeids) | |
69 | memnodemap[addr >> shift] = i; | |
70 | else | |
71 | memnodemap[addr >> shift] = nodeids[i]; | |
72 | ||
076422d2 | 73 | addr += (1UL << shift); |
529a3404 ED |
74 | } while (addr < end); |
75 | res = 1; | |
e3cfe529 | 76 | } |
529a3404 ED |
77 | return res; |
78 | } | |
79 | ||
076422d2 AS |
80 | static int __init allocate_cachealigned_memnodemap(void) |
81 | { | |
24a5da73 | 82 | unsigned long addr; |
076422d2 AS |
83 | |
84 | memnodemap = memnode.embedded_map; | |
316390b0 | 85 | if (memnodemapsize <= ARRAY_SIZE(memnode.embedded_map)) |
076422d2 | 86 | return 0; |
076422d2 | 87 | |
24a5da73 | 88 | addr = 0x8000; |
be3e89ee | 89 | nodemap_size = roundup(sizeof(s16) * memnodemapsize, L1_CACHE_BYTES); |
c987d12f | 90 | nodemap_addr = find_e820_area(addr, max_pfn<<PAGE_SHIFT, |
24a5da73 | 91 | nodemap_size, L1_CACHE_BYTES); |
076422d2 AS |
92 | if (nodemap_addr == -1UL) { |
93 | printk(KERN_ERR | |
94 | "NUMA: Unable to allocate Memory to Node hash map\n"); | |
95 | nodemap_addr = nodemap_size = 0; | |
96 | return -1; | |
97 | } | |
24a5da73 | 98 | memnodemap = phys_to_virt(nodemap_addr); |
25eff8d4 | 99 | reserve_early(nodemap_addr, nodemap_addr + nodemap_size, "MEMNODEMAP"); |
076422d2 AS |
100 | |
101 | printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n", | |
102 | nodemap_addr, nodemap_addr + nodemap_size); | |
103 | return 0; | |
104 | } | |
105 | ||
106 | /* | |
107 | * The LSB of all start and end addresses in the node map is the value of the | |
108 | * maximum possible shift. | |
109 | */ | |
e3cfe529 TG |
110 | static int __init extract_lsb_from_nodes(const struct bootnode *nodes, |
111 | int numnodes) | |
529a3404 | 112 | { |
54413927 | 113 | int i, nodes_used = 0; |
076422d2 AS |
114 | unsigned long start, end; |
115 | unsigned long bitfield = 0, memtop = 0; | |
116 | ||
117 | for (i = 0; i < numnodes; i++) { | |
118 | start = nodes[i].start; | |
119 | end = nodes[i].end; | |
120 | if (start >= end) | |
121 | continue; | |
54413927 AS |
122 | bitfield |= start; |
123 | nodes_used++; | |
076422d2 AS |
124 | if (end > memtop) |
125 | memtop = end; | |
126 | } | |
54413927 AS |
127 | if (nodes_used <= 1) |
128 | i = 63; | |
129 | else | |
130 | i = find_first_bit(&bitfield, sizeof(unsigned long)*8); | |
076422d2 AS |
131 | memnodemapsize = (memtop >> i)+1; |
132 | return i; | |
133 | } | |
529a3404 | 134 | |
6ec6e0d9 SS |
135 | int __init compute_hash_shift(struct bootnode *nodes, int numnodes, |
136 | int *nodeids) | |
076422d2 AS |
137 | { |
138 | int shift; | |
529a3404 | 139 | |
076422d2 AS |
140 | shift = extract_lsb_from_nodes(nodes, numnodes); |
141 | if (allocate_cachealigned_memnodemap()) | |
142 | return -1; | |
6b050f80 | 143 | printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n", |
529a3404 ED |
144 | shift); |
145 | ||
6ec6e0d9 | 146 | if (populate_memnodemap(nodes, numnodes, shift, nodeids) != 1) { |
e3cfe529 TG |
147 | printk(KERN_INFO "Your memory is not aligned you need to " |
148 | "rebuild your kernel with a bigger NODEMAPSIZE " | |
149 | "shift=%d\n", shift); | |
529a3404 ED |
150 | return -1; |
151 | } | |
b684664f | 152 | return shift; |
1da177e4 LT |
153 | } |
154 | ||
f2dbcfa7 | 155 | int __meminit __early_pfn_to_nid(unsigned long pfn) |
bbfceef4 MT |
156 | { |
157 | return phys_to_nid(pfn << PAGE_SHIFT); | |
158 | } | |
bbfceef4 | 159 | |
e3cfe529 | 160 | static void * __init early_node_mem(int nodeid, unsigned long start, |
24a5da73 YL |
161 | unsigned long end, unsigned long size, |
162 | unsigned long align) | |
a8062231 | 163 | { |
cef625ee | 164 | unsigned long mem; |
e3cfe529 | 165 | |
cef625ee YL |
166 | /* |
167 | * put it on high as possible | |
168 | * something will go with NODE_DATA | |
169 | */ | |
170 | if (start < (MAX_DMA_PFN<<PAGE_SHIFT)) | |
171 | start = MAX_DMA_PFN<<PAGE_SHIFT; | |
172 | if (start < (MAX_DMA32_PFN<<PAGE_SHIFT) && | |
173 | end > (MAX_DMA32_PFN<<PAGE_SHIFT)) | |
174 | start = MAX_DMA32_PFN<<PAGE_SHIFT; | |
72d7c3b3 YL |
175 | mem = memblock_x86_find_in_range_node(nodeid, start, end, size, align); |
176 | if (mem != MEMBLOCK_ERROR) | |
a8062231 | 177 | return __va(mem); |
9347e0b0 | 178 | |
cef625ee YL |
179 | /* extend the search scope */ |
180 | end = max_pfn_mapped << PAGE_SHIFT; | |
181 | if (end > (MAX_DMA32_PFN<<PAGE_SHIFT)) | |
182 | start = MAX_DMA32_PFN<<PAGE_SHIFT; | |
183 | else | |
184 | start = MAX_DMA_PFN<<PAGE_SHIFT; | |
72d7c3b3 YL |
185 | mem = memblock_x86_find_in_range_node(nodeid, start, end, size, align); |
186 | if (mem != MEMBLOCK_ERROR) | |
a8062231 | 187 | return __va(mem); |
9347e0b0 | 188 | |
1842f90c | 189 | printk(KERN_ERR "Cannot find %lu bytes in node %d\n", |
e3cfe529 | 190 | size, nodeid); |
1842f90c YL |
191 | |
192 | return NULL; | |
a8062231 AK |
193 | } |
194 | ||
1da177e4 | 195 | /* Initialize bootmem allocator for a node */ |
7c43769a YL |
196 | void __init |
197 | setup_node_bootmem(int nodeid, unsigned long start, unsigned long end) | |
e3cfe529 | 198 | { |
08677214 | 199 | unsigned long start_pfn, last_pfn, nodedata_phys; |
7c43769a | 200 | const int pgdat_size = roundup(sizeof(pg_data_t), PAGE_SIZE); |
1a27fc0a | 201 | int nid; |
08677214 YL |
202 | #ifndef CONFIG_NO_BOOTMEM |
203 | unsigned long bootmap_start, bootmap_pages, bootmap_size; | |
204 | void *bootmap; | |
205 | #endif | |
1da177e4 | 206 | |
4c31e92b YL |
207 | if (!end) |
208 | return; | |
209 | ||
7c43769a YL |
210 | /* |
211 | * Don't confuse VM with a node that doesn't have the | |
212 | * minimum amount of memory: | |
213 | */ | |
214 | if (end && (end - start) < NODE_MIN_SIZE) | |
215 | return; | |
216 | ||
be3e89ee | 217 | start = roundup(start, ZONE_ALIGN); |
1da177e4 | 218 | |
08677214 | 219 | printk(KERN_INFO "Initmem setup node %d %016lx-%016lx\n", nodeid, |
e3cfe529 | 220 | start, end); |
1da177e4 LT |
221 | |
222 | start_pfn = start >> PAGE_SHIFT; | |
886533a3 | 223 | last_pfn = end >> PAGE_SHIFT; |
1da177e4 | 224 | |
24a5da73 YL |
225 | node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size, |
226 | SMP_CACHE_BYTES); | |
a8062231 AK |
227 | if (node_data[nodeid] == NULL) |
228 | return; | |
229 | nodedata_phys = __pa(node_data[nodeid]); | |
1842f90c | 230 | reserve_early(nodedata_phys, nodedata_phys + pgdat_size, "NODE_DATA"); |
6118f76f YL |
231 | printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n", nodedata_phys, |
232 | nodedata_phys + pgdat_size - 1); | |
1842f90c YL |
233 | nid = phys_to_nid(nodedata_phys); |
234 | if (nid != nodeid) | |
235 | printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nodeid, nid); | |
1da177e4 | 236 | |
1da177e4 | 237 | memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t)); |
08677214 | 238 | NODE_DATA(nodeid)->node_id = nodeid; |
1da177e4 | 239 | NODE_DATA(nodeid)->node_start_pfn = start_pfn; |
886533a3 | 240 | NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn; |
1da177e4 | 241 | |
08677214 YL |
242 | #ifndef CONFIG_NO_BOOTMEM |
243 | NODE_DATA(nodeid)->bdata = &bootmem_node_data[nodeid]; | |
244 | ||
1a27fc0a YL |
245 | /* |
246 | * Find a place for the bootmem map | |
247 | * nodedata_phys could be on other nodes by alloc_bootmem, | |
248 | * so need to sure bootmap_start not to be small, otherwise | |
249 | * early_node_mem will get that with find_e820_area instead | |
250 | * of alloc_bootmem, that could clash with reserved range | |
251 | */ | |
886533a3 | 252 | bootmap_pages = bootmem_bootmap_pages(last_pfn - start_pfn); |
1842f90c | 253 | bootmap_start = roundup(nodedata_phys + pgdat_size, PAGE_SIZE); |
24a5da73 | 254 | /* |
e9197bf0 | 255 | * SMP_CACHE_BYTES could be enough, but init_bootmem_node like |
24a5da73 YL |
256 | * to use that to align to PAGE_SIZE |
257 | */ | |
a8062231 | 258 | bootmap = early_node_mem(nodeid, bootmap_start, end, |
24a5da73 | 259 | bootmap_pages<<PAGE_SHIFT, PAGE_SIZE); |
a8062231 | 260 | if (bootmap == NULL) { |
1842f90c | 261 | free_early(nodedata_phys, nodedata_phys + pgdat_size); |
a8062231 AK |
262 | node_data[nodeid] = NULL; |
263 | return; | |
264 | } | |
265 | bootmap_start = __pa(bootmap); | |
1842f90c YL |
266 | reserve_early(bootmap_start, bootmap_start+(bootmap_pages<<PAGE_SHIFT), |
267 | "BOOTMAP"); | |
e3cfe529 | 268 | |
1da177e4 | 269 | bootmap_size = init_bootmem_node(NODE_DATA(nodeid), |
e3cfe529 | 270 | bootmap_start >> PAGE_SHIFT, |
886533a3 | 271 | start_pfn, last_pfn); |
1da177e4 | 272 | |
6118f76f YL |
273 | printk(KERN_INFO " bootmap [%016lx - %016lx] pages %lx\n", |
274 | bootmap_start, bootmap_start + bootmap_size - 1, | |
275 | bootmap_pages); | |
1a27fc0a YL |
276 | nid = phys_to_nid(bootmap_start); |
277 | if (nid != nodeid) | |
278 | printk(KERN_INFO " bootmap(%d) on node %d\n", nodeid, nid); | |
1842f90c YL |
279 | |
280 | free_bootmem_with_active_regions(nodeid, end); | |
08677214 | 281 | #endif |
1a27fc0a | 282 | |
1da177e4 | 283 | node_set_online(nodeid); |
e3cfe529 | 284 | } |
1da177e4 | 285 | |
e3cfe529 TG |
286 | /* |
287 | * There are unfortunately some poorly designed mainboards around that | |
288 | * only connect memory to a single CPU. This breaks the 1:1 cpu->node | |
289 | * mapping. To avoid this fill in the mapping for all possible CPUs, | |
290 | * as the number of CPUs is not known yet. We round robin the existing | |
291 | * nodes. | |
292 | */ | |
1da177e4 LT |
293 | void __init numa_init_array(void) |
294 | { | |
295 | int rr, i; | |
e3cfe529 | 296 | |
85cc5135 | 297 | rr = first_node(node_online_map); |
168ef543 | 298 | for (i = 0; i < nr_cpu_ids; i++) { |
1ce35712 | 299 | if (early_cpu_to_node(i) != NUMA_NO_NODE) |
1da177e4 | 300 | continue; |
e3cfe529 | 301 | numa_set_node(i, rr); |
1da177e4 LT |
302 | rr = next_node(rr, node_online_map); |
303 | if (rr == MAX_NUMNODES) | |
304 | rr = first_node(node_online_map); | |
1da177e4 | 305 | } |
1da177e4 LT |
306 | } |
307 | ||
308 | #ifdef CONFIG_NUMA_EMU | |
53fee04f | 309 | /* Numa emulation */ |
adc19389 DR |
310 | static struct bootnode nodes[MAX_NUMNODES] __initdata; |
311 | static struct bootnode physnodes[MAX_NUMNODES] __initdata; | |
864fc31e | 312 | static char *cmdline __initdata; |
1da177e4 | 313 | |
adc19389 DR |
314 | static int __init setup_physnodes(unsigned long start, unsigned long end, |
315 | int acpi, int k8) | |
316 | { | |
317 | int nr_nodes = 0; | |
318 | int ret = 0; | |
319 | int i; | |
320 | ||
321 | #ifdef CONFIG_ACPI_NUMA | |
322 | if (acpi) | |
323 | nr_nodes = acpi_get_nodes(physnodes); | |
324 | #endif | |
325 | #ifdef CONFIG_K8_NUMA | |
326 | if (k8) | |
327 | nr_nodes = k8_get_nodes(physnodes); | |
328 | #endif | |
329 | /* | |
330 | * Basic sanity checking on the physical node map: there may be errors | |
331 | * if the SRAT or K8 incorrectly reported the topology or the mem= | |
332 | * kernel parameter is used. | |
333 | */ | |
334 | for (i = 0; i < nr_nodes; i++) { | |
335 | if (physnodes[i].start == physnodes[i].end) | |
336 | continue; | |
337 | if (physnodes[i].start > end) { | |
338 | physnodes[i].end = physnodes[i].start; | |
339 | continue; | |
340 | } | |
341 | if (physnodes[i].end < start) { | |
342 | physnodes[i].start = physnodes[i].end; | |
343 | continue; | |
344 | } | |
345 | if (physnodes[i].start < start) | |
346 | physnodes[i].start = start; | |
347 | if (physnodes[i].end > end) | |
348 | physnodes[i].end = end; | |
349 | } | |
350 | ||
351 | /* | |
352 | * Remove all nodes that have no memory or were truncated because of the | |
353 | * limited address range. | |
354 | */ | |
355 | for (i = 0; i < nr_nodes; i++) { | |
356 | if (physnodes[i].start == physnodes[i].end) | |
357 | continue; | |
358 | physnodes[ret].start = physnodes[i].start; | |
359 | physnodes[ret].end = physnodes[i].end; | |
360 | ret++; | |
361 | } | |
362 | ||
363 | /* | |
364 | * If no physical topology was detected, a single node is faked to cover | |
365 | * the entire address space. | |
366 | */ | |
367 | if (!ret) { | |
368 | physnodes[ret].start = start; | |
369 | physnodes[ret].end = end; | |
370 | ret = 1; | |
371 | } | |
372 | return ret; | |
373 | } | |
374 | ||
53fee04f | 375 | /* |
e3cfe529 TG |
376 | * Setups up nid to range from addr to addr + size. If the end |
377 | * boundary is greater than max_addr, then max_addr is used instead. | |
378 | * The return value is 0 if there is additional memory left for | |
379 | * allocation past addr and -1 otherwise. addr is adjusted to be at | |
380 | * the end of the node. | |
53fee04f | 381 | */ |
adc19389 | 382 | static int __init setup_node_range(int nid, u64 *addr, u64 size, u64 max_addr) |
53fee04f | 383 | { |
8b8ca80e DR |
384 | int ret = 0; |
385 | nodes[nid].start = *addr; | |
386 | *addr += size; | |
387 | if (*addr >= max_addr) { | |
388 | *addr = max_addr; | |
389 | ret = -1; | |
390 | } | |
391 | nodes[nid].end = *addr; | |
e3f1caee | 392 | node_set(nid, node_possible_map); |
8b8ca80e DR |
393 | printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid, |
394 | nodes[nid].start, nodes[nid].end, | |
395 | (nodes[nid].end - nodes[nid].start) >> 20); | |
396 | return ret; | |
53fee04f RS |
397 | } |
398 | ||
adc19389 DR |
399 | /* |
400 | * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr | |
401 | * to max_addr. The return value is the number of nodes allocated. | |
402 | */ | |
403 | static int __init split_nodes_interleave(u64 addr, u64 max_addr, | |
404 | int nr_phys_nodes, int nr_nodes) | |
405 | { | |
406 | nodemask_t physnode_mask = NODE_MASK_NONE; | |
407 | u64 size; | |
408 | int big; | |
409 | int ret = 0; | |
410 | int i; | |
411 | ||
412 | if (nr_nodes <= 0) | |
413 | return -1; | |
414 | if (nr_nodes > MAX_NUMNODES) { | |
415 | pr_info("numa=fake=%d too large, reducing to %d\n", | |
416 | nr_nodes, MAX_NUMNODES); | |
417 | nr_nodes = MAX_NUMNODES; | |
418 | } | |
419 | ||
420 | size = (max_addr - addr - e820_hole_size(addr, max_addr)) / nr_nodes; | |
421 | /* | |
422 | * Calculate the number of big nodes that can be allocated as a result | |
423 | * of consolidating the remainder. | |
424 | */ | |
68fd111e | 425 | big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) / |
adc19389 DR |
426 | FAKE_NODE_MIN_SIZE; |
427 | ||
428 | size &= FAKE_NODE_MIN_HASH_MASK; | |
429 | if (!size) { | |
430 | pr_err("Not enough memory for each node. " | |
431 | "NUMA emulation disabled.\n"); | |
432 | return -1; | |
433 | } | |
434 | ||
435 | for (i = 0; i < nr_phys_nodes; i++) | |
436 | if (physnodes[i].start != physnodes[i].end) | |
437 | node_set(i, physnode_mask); | |
438 | ||
439 | /* | |
440 | * Continue to fill physical nodes with fake nodes until there is no | |
441 | * memory left on any of them. | |
442 | */ | |
443 | while (nodes_weight(physnode_mask)) { | |
444 | for_each_node_mask(i, physnode_mask) { | |
445 | u64 end = physnodes[i].start + size; | |
446 | u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN); | |
447 | ||
448 | if (ret < big) | |
449 | end += FAKE_NODE_MIN_SIZE; | |
450 | ||
451 | /* | |
452 | * Continue to add memory to this fake node if its | |
453 | * non-reserved memory is less than the per-node size. | |
454 | */ | |
455 | while (end - physnodes[i].start - | |
456 | e820_hole_size(physnodes[i].start, end) < size) { | |
457 | end += FAKE_NODE_MIN_SIZE; | |
458 | if (end > physnodes[i].end) { | |
459 | end = physnodes[i].end; | |
460 | break; | |
461 | } | |
462 | } | |
463 | ||
464 | /* | |
465 | * If there won't be at least FAKE_NODE_MIN_SIZE of | |
466 | * non-reserved memory in ZONE_DMA32 for the next node, | |
467 | * this one must extend to the boundary. | |
468 | */ | |
469 | if (end < dma32_end && dma32_end - end - | |
470 | e820_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE) | |
471 | end = dma32_end; | |
472 | ||
473 | /* | |
474 | * If there won't be enough non-reserved memory for the | |
475 | * next node, this one must extend to the end of the | |
476 | * physical node. | |
477 | */ | |
478 | if (physnodes[i].end - end - | |
479 | e820_hole_size(end, physnodes[i].end) < size) | |
480 | end = physnodes[i].end; | |
481 | ||
482 | /* | |
483 | * Avoid allocating more nodes than requested, which can | |
484 | * happen as a result of rounding down each node's size | |
485 | * to FAKE_NODE_MIN_SIZE. | |
486 | */ | |
487 | if (nodes_weight(physnode_mask) + ret >= nr_nodes) | |
488 | end = physnodes[i].end; | |
489 | ||
490 | if (setup_node_range(ret++, &physnodes[i].start, | |
491 | end - physnodes[i].start, | |
492 | physnodes[i].end) < 0) | |
493 | node_clear(i, physnode_mask); | |
494 | } | |
495 | } | |
496 | return ret; | |
497 | } | |
498 | ||
8df5bb34 DR |
499 | /* |
500 | * Returns the end address of a node so that there is at least `size' amount of | |
501 | * non-reserved memory or `max_addr' is reached. | |
502 | */ | |
503 | static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size) | |
504 | { | |
505 | u64 end = start + size; | |
506 | ||
507 | while (end - start - e820_hole_size(start, end) < size) { | |
508 | end += FAKE_NODE_MIN_SIZE; | |
509 | if (end > max_addr) { | |
510 | end = max_addr; | |
511 | break; | |
512 | } | |
513 | } | |
514 | return end; | |
515 | } | |
516 | ||
517 | /* | |
518 | * Sets up fake nodes of `size' interleaved over physical nodes ranging from | |
519 | * `addr' to `max_addr'. The return value is the number of nodes allocated. | |
520 | */ | |
521 | static int __init split_nodes_size_interleave(u64 addr, u64 max_addr, u64 size) | |
522 | { | |
523 | nodemask_t physnode_mask = NODE_MASK_NONE; | |
524 | u64 min_size; | |
525 | int ret = 0; | |
526 | int i; | |
527 | ||
528 | if (!size) | |
529 | return -1; | |
530 | /* | |
531 | * The limit on emulated nodes is MAX_NUMNODES, so the size per node is | |
532 | * increased accordingly if the requested size is too small. This | |
533 | * creates a uniform distribution of node sizes across the entire | |
534 | * machine (but not necessarily over physical nodes). | |
535 | */ | |
536 | min_size = (max_addr - addr - e820_hole_size(addr, max_addr)) / | |
537 | MAX_NUMNODES; | |
538 | min_size = max(min_size, FAKE_NODE_MIN_SIZE); | |
539 | if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size) | |
540 | min_size = (min_size + FAKE_NODE_MIN_SIZE) & | |
541 | FAKE_NODE_MIN_HASH_MASK; | |
542 | if (size < min_size) { | |
543 | pr_err("Fake node size %LuMB too small, increasing to %LuMB\n", | |
544 | size >> 20, min_size >> 20); | |
545 | size = min_size; | |
546 | } | |
547 | size &= FAKE_NODE_MIN_HASH_MASK; | |
548 | ||
549 | for (i = 0; i < MAX_NUMNODES; i++) | |
550 | if (physnodes[i].start != physnodes[i].end) | |
551 | node_set(i, physnode_mask); | |
552 | /* | |
553 | * Fill physical nodes with fake nodes of size until there is no memory | |
554 | * left on any of them. | |
555 | */ | |
556 | while (nodes_weight(physnode_mask)) { | |
557 | for_each_node_mask(i, physnode_mask) { | |
558 | u64 dma32_end = MAX_DMA32_PFN << PAGE_SHIFT; | |
559 | u64 end; | |
560 | ||
561 | end = find_end_of_node(physnodes[i].start, | |
562 | physnodes[i].end, size); | |
563 | /* | |
564 | * If there won't be at least FAKE_NODE_MIN_SIZE of | |
565 | * non-reserved memory in ZONE_DMA32 for the next node, | |
566 | * this one must extend to the boundary. | |
567 | */ | |
568 | if (end < dma32_end && dma32_end - end - | |
569 | e820_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE) | |
570 | end = dma32_end; | |
571 | ||
572 | /* | |
573 | * If there won't be enough non-reserved memory for the | |
574 | * next node, this one must extend to the end of the | |
575 | * physical node. | |
576 | */ | |
577 | if (physnodes[i].end - end - | |
578 | e820_hole_size(end, physnodes[i].end) < size) | |
579 | end = physnodes[i].end; | |
580 | ||
581 | /* | |
582 | * Setup the fake node that will be allocated as bootmem | |
583 | * later. If setup_node_range() returns non-zero, there | |
584 | * is no more memory available on this physical node. | |
585 | */ | |
586 | if (setup_node_range(ret++, &physnodes[i].start, | |
587 | end - physnodes[i].start, | |
588 | physnodes[i].end) < 0) | |
589 | node_clear(i, physnode_mask); | |
590 | } | |
591 | } | |
592 | return ret; | |
593 | } | |
594 | ||
8b8ca80e | 595 | /* |
886533a3 | 596 | * Sets up the system RAM area from start_pfn to last_pfn according to the |
8b8ca80e DR |
597 | * numa=fake command-line option. |
598 | */ | |
adc19389 DR |
599 | static int __init numa_emulation(unsigned long start_pfn, |
600 | unsigned long last_pfn, int acpi, int k8) | |
8b8ca80e | 601 | { |
ca2107c9 | 602 | u64 addr = start_pfn << PAGE_SHIFT; |
886533a3 | 603 | u64 max_addr = last_pfn << PAGE_SHIFT; |
adc19389 | 604 | int num_phys_nodes; |
ca2107c9 DR |
605 | int num_nodes; |
606 | int i; | |
8b8ca80e | 607 | |
adc19389 | 608 | num_phys_nodes = setup_physnodes(addr, max_addr, acpi, k8); |
8df5bb34 DR |
609 | /* |
610 | * If the numa=fake command-line contains a 'M' or 'G', it represents | |
ca2107c9 DR |
611 | * the fixed node size. Otherwise, if it is just a single number N, |
612 | * split the system RAM into N fake nodes. | |
8df5bb34 DR |
613 | */ |
614 | if (strchr(cmdline, 'M') || strchr(cmdline, 'G')) { | |
ca2107c9 DR |
615 | u64 size; |
616 | ||
8df5bb34 DR |
617 | size = memparse(cmdline, &cmdline); |
618 | num_nodes = split_nodes_size_interleave(addr, max_addr, size); | |
ca2107c9 DR |
619 | } else { |
620 | unsigned long n; | |
8df5bb34 | 621 | |
ca2107c9 DR |
622 | n = simple_strtoul(cmdline, NULL, 0); |
623 | num_nodes = split_nodes_interleave(addr, max_addr, num_phys_nodes, n); | |
8b8ca80e DR |
624 | } |
625 | ||
ca2107c9 DR |
626 | if (num_nodes < 0) |
627 | return num_nodes; | |
6ec6e0d9 | 628 | memnode_shift = compute_hash_shift(nodes, num_nodes, NULL); |
8b8ca80e DR |
629 | if (memnode_shift < 0) { |
630 | memnode_shift = 0; | |
631 | printk(KERN_ERR "No NUMA hash function found. NUMA emulation " | |
632 | "disabled.\n"); | |
633 | return -1; | |
634 | } | |
635 | ||
636 | /* | |
adc19389 DR |
637 | * We need to vacate all active ranges that may have been registered for |
638 | * the e820 memory map. | |
8b8ca80e DR |
639 | */ |
640 | remove_all_active_ranges(); | |
e3f1caee | 641 | for_each_node_mask(i, node_possible_map) { |
5cb248ab MG |
642 | e820_register_active_regions(i, nodes[i].start >> PAGE_SHIFT, |
643 | nodes[i].end >> PAGE_SHIFT); | |
e3cfe529 | 644 | setup_node_bootmem(i, nodes[i].start, nodes[i].end); |
5cb248ab | 645 | } |
3484d798 | 646 | acpi_fake_nodes(nodes, num_nodes); |
e3cfe529 TG |
647 | numa_init_array(); |
648 | return 0; | |
1da177e4 | 649 | } |
8b8ca80e | 650 | #endif /* CONFIG_NUMA_EMU */ |
1da177e4 | 651 | |
8ee2debc DR |
652 | void __init initmem_init(unsigned long start_pfn, unsigned long last_pfn, |
653 | int acpi, int k8) | |
e3cfe529 | 654 | { |
1da177e4 LT |
655 | int i; |
656 | ||
e3f1caee | 657 | nodes_clear(node_possible_map); |
b7ad149d | 658 | nodes_clear(node_online_map); |
e3f1caee | 659 | |
1da177e4 | 660 | #ifdef CONFIG_NUMA_EMU |
adc19389 | 661 | if (cmdline && !numa_emulation(start_pfn, last_pfn, acpi, k8)) |
e3cfe529 | 662 | return; |
e3f1caee | 663 | nodes_clear(node_possible_map); |
b7ad149d | 664 | nodes_clear(node_online_map); |
1da177e4 LT |
665 | #endif |
666 | ||
667 | #ifdef CONFIG_ACPI_NUMA | |
8716273c DR |
668 | if (!numa_off && acpi && !acpi_scan_nodes(start_pfn << PAGE_SHIFT, |
669 | last_pfn << PAGE_SHIFT)) | |
e3cfe529 | 670 | return; |
e3f1caee | 671 | nodes_clear(node_possible_map); |
b7ad149d | 672 | nodes_clear(node_online_map); |
1da177e4 LT |
673 | #endif |
674 | ||
675 | #ifdef CONFIG_K8_NUMA | |
8ee2debc | 676 | if (!numa_off && k8 && !k8_scan_nodes()) |
1da177e4 | 677 | return; |
e3f1caee | 678 | nodes_clear(node_possible_map); |
b7ad149d | 679 | nodes_clear(node_online_map); |
1da177e4 LT |
680 | #endif |
681 | printk(KERN_INFO "%s\n", | |
682 | numa_off ? "NUMA turned off" : "No NUMA configuration found"); | |
683 | ||
e3cfe529 | 684 | printk(KERN_INFO "Faking a node at %016lx-%016lx\n", |
1da177e4 | 685 | start_pfn << PAGE_SHIFT, |
886533a3 | 686 | last_pfn << PAGE_SHIFT); |
e3cfe529 TG |
687 | /* setup dummy node covering all memory */ |
688 | memnode_shift = 63; | |
076422d2 | 689 | memnodemap = memnode.embedded_map; |
1da177e4 | 690 | memnodemap[0] = 0; |
1da177e4 | 691 | node_set_online(0); |
e3f1caee | 692 | node_set(0, node_possible_map); |
168ef543 | 693 | for (i = 0; i < nr_cpu_ids; i++) |
69d81fcd | 694 | numa_set_node(i, 0); |
886533a3 TG |
695 | e820_register_active_regions(0, start_pfn, last_pfn); |
696 | setup_node_bootmem(0, start_pfn << PAGE_SHIFT, last_pfn << PAGE_SHIFT); | |
69d81fcd AK |
697 | } |
698 | ||
e3cfe529 TG |
699 | unsigned long __init numa_free_all_bootmem(void) |
700 | { | |
1da177e4 | 701 | unsigned long pages = 0; |
e3cfe529 TG |
702 | int i; |
703 | ||
704 | for_each_online_node(i) | |
1da177e4 | 705 | pages += free_all_bootmem_node(NODE_DATA(i)); |
e3cfe529 | 706 | |
08677214 YL |
707 | #ifdef CONFIG_NO_BOOTMEM |
708 | pages += free_all_memory_core_early(MAX_NUMNODES); | |
709 | #endif | |
710 | ||
1da177e4 | 711 | return pages; |
e3cfe529 | 712 | } |
1da177e4 | 713 | |
2c8c0e6b | 714 | static __init int numa_setup(char *opt) |
e3cfe529 | 715 | { |
2c8c0e6b AK |
716 | if (!opt) |
717 | return -EINVAL; | |
e3cfe529 | 718 | if (!strncmp(opt, "off", 3)) |
1da177e4 LT |
719 | numa_off = 1; |
720 | #ifdef CONFIG_NUMA_EMU | |
8b8ca80e DR |
721 | if (!strncmp(opt, "fake=", 5)) |
722 | cmdline = opt + 5; | |
1da177e4 LT |
723 | #endif |
724 | #ifdef CONFIG_ACPI_NUMA | |
e3cfe529 TG |
725 | if (!strncmp(opt, "noacpi", 6)) |
726 | acpi_numa = -1; | |
1da177e4 | 727 | #endif |
2c8c0e6b | 728 | return 0; |
e3cfe529 | 729 | } |
2c8c0e6b AK |
730 | early_param("numa", numa_setup); |
731 | ||
23ca4bba | 732 | #ifdef CONFIG_NUMA |
d9c2d5ac YL |
733 | |
734 | static __init int find_near_online_node(int node) | |
735 | { | |
736 | int n, val; | |
737 | int min_val = INT_MAX; | |
738 | int best_node = -1; | |
739 | ||
740 | for_each_online_node(n) { | |
741 | val = node_distance(node, n); | |
742 | ||
743 | if (val < min_val) { | |
744 | min_val = val; | |
745 | best_node = n; | |
746 | } | |
747 | } | |
748 | ||
749 | return best_node; | |
750 | } | |
751 | ||
05b3cbd8 RT |
752 | /* |
753 | * Setup early cpu_to_node. | |
754 | * | |
755 | * Populate cpu_to_node[] only if x86_cpu_to_apicid[], | |
756 | * and apicid_to_node[] tables have valid entries for a CPU. | |
757 | * This means we skip cpu_to_node[] initialisation for NUMA | |
758 | * emulation and faking node case (when running a kernel compiled | |
759 | * for NUMA on a non NUMA box), which is OK as cpu_to_node[] | |
760 | * is already initialized in a round robin manner at numa_init_array, | |
761 | * prior to this call, and this initialization is good enough | |
762 | * for the fake NUMA cases. | |
23ca4bba MT |
763 | * |
764 | * Called before the per_cpu areas are setup. | |
05b3cbd8 RT |
765 | */ |
766 | void __init init_cpu_to_node(void) | |
767 | { | |
23ca4bba MT |
768 | int cpu; |
769 | u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid); | |
e3cfe529 | 770 | |
23ca4bba MT |
771 | BUG_ON(cpu_to_apicid == NULL); |
772 | ||
773 | for_each_possible_cpu(cpu) { | |
7c9e92b6 | 774 | int node; |
23ca4bba | 775 | u16 apicid = cpu_to_apicid[cpu]; |
e3cfe529 | 776 | |
05b3cbd8 RT |
777 | if (apicid == BAD_APICID) |
778 | continue; | |
7c9e92b6 YL |
779 | node = apicid_to_node[apicid]; |
780 | if (node == NUMA_NO_NODE) | |
05b3cbd8 | 781 | continue; |
7c9e92b6 | 782 | if (!node_online(node)) |
d9c2d5ac | 783 | node = find_near_online_node(node); |
23ca4bba | 784 | numa_set_node(cpu, node); |
05b3cbd8 RT |
785 | } |
786 | } | |
23ca4bba | 787 | #endif |
05b3cbd8 | 788 | |
cf050132 | 789 | |
6470aff6 BG |
790 | void __cpuinit numa_set_node(int cpu, int node) |
791 | { | |
792 | int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map); | |
793 | ||
794 | /* early setting, no percpu area yet */ | |
795 | if (cpu_to_node_map) { | |
796 | cpu_to_node_map[cpu] = node; | |
797 | return; | |
798 | } | |
799 | ||
800 | #ifdef CONFIG_DEBUG_PER_CPU_MAPS | |
44581a28 | 801 | if (cpu >= nr_cpu_ids || !cpu_possible(cpu)) { |
6470aff6 BG |
802 | printk(KERN_ERR "numa_set_node: invalid cpu# (%d)\n", cpu); |
803 | dump_stack(); | |
804 | return; | |
805 | } | |
806 | #endif | |
807 | per_cpu(x86_cpu_to_node_map, cpu) = node; | |
808 | ||
809 | if (node != NUMA_NO_NODE) | |
e534c7c5 | 810 | set_cpu_numa_node(cpu, node); |
6470aff6 BG |
811 | } |
812 | ||
813 | void __cpuinit numa_clear_node(int cpu) | |
814 | { | |
815 | numa_set_node(cpu, NUMA_NO_NODE); | |
816 | } | |
817 | ||
818 | #ifndef CONFIG_DEBUG_PER_CPU_MAPS | |
819 | ||
820 | void __cpuinit numa_add_cpu(int cpu) | |
821 | { | |
c032ef60 | 822 | cpumask_set_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]); |
6470aff6 BG |
823 | } |
824 | ||
825 | void __cpuinit numa_remove_cpu(int cpu) | |
826 | { | |
c032ef60 | 827 | cpumask_clear_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]); |
6470aff6 BG |
828 | } |
829 | ||
830 | #else /* CONFIG_DEBUG_PER_CPU_MAPS */ | |
831 | ||
832 | /* | |
833 | * --------- debug versions of the numa functions --------- | |
834 | */ | |
835 | static void __cpuinit numa_set_cpumask(int cpu, int enable) | |
836 | { | |
837 | int node = early_cpu_to_node(cpu); | |
73e907de | 838 | struct cpumask *mask; |
6470aff6 BG |
839 | char buf[64]; |
840 | ||
c032ef60 RR |
841 | mask = node_to_cpumask_map[node]; |
842 | if (mask == NULL) { | |
843 | printk(KERN_ERR "node_to_cpumask_map[%i] NULL\n", node); | |
6470aff6 BG |
844 | dump_stack(); |
845 | return; | |
846 | } | |
847 | ||
6470aff6 | 848 | if (enable) |
c032ef60 | 849 | cpumask_set_cpu(cpu, mask); |
6470aff6 | 850 | else |
c032ef60 | 851 | cpumask_clear_cpu(cpu, mask); |
6470aff6 BG |
852 | |
853 | cpulist_scnprintf(buf, sizeof(buf), mask); | |
854 | printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n", | |
855 | enable ? "numa_add_cpu" : "numa_remove_cpu", cpu, node, buf); | |
856 | } | |
857 | ||
858 | void __cpuinit numa_add_cpu(int cpu) | |
859 | { | |
860 | numa_set_cpumask(cpu, 1); | |
861 | } | |
862 | ||
863 | void __cpuinit numa_remove_cpu(int cpu) | |
864 | { | |
865 | numa_set_cpumask(cpu, 0); | |
866 | } | |
867 | ||
e534c7c5 | 868 | int __cpu_to_node(int cpu) |
6470aff6 BG |
869 | { |
870 | if (early_per_cpu_ptr(x86_cpu_to_node_map)) { | |
871 | printk(KERN_WARNING | |
872 | "cpu_to_node(%d): usage too early!\n", cpu); | |
873 | dump_stack(); | |
874 | return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu]; | |
875 | } | |
876 | return per_cpu(x86_cpu_to_node_map, cpu); | |
877 | } | |
e534c7c5 | 878 | EXPORT_SYMBOL(__cpu_to_node); |
6470aff6 BG |
879 | |
880 | /* | |
881 | * Same function as cpu_to_node() but used if called before the | |
882 | * per_cpu areas are setup. | |
883 | */ | |
884 | int early_cpu_to_node(int cpu) | |
885 | { | |
886 | if (early_per_cpu_ptr(x86_cpu_to_node_map)) | |
887 | return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu]; | |
888 | ||
44581a28 | 889 | if (!cpu_possible(cpu)) { |
6470aff6 BG |
890 | printk(KERN_WARNING |
891 | "early_cpu_to_node(%d): no per_cpu area!\n", cpu); | |
892 | dump_stack(); | |
893 | return NUMA_NO_NODE; | |
894 | } | |
895 | return per_cpu(x86_cpu_to_node_map, cpu); | |
896 | } | |
897 | ||
6470aff6 BG |
898 | /* |
899 | * --------- end of debug versions of the numa functions --------- | |
900 | */ | |
901 | ||
902 | #endif /* CONFIG_DEBUG_PER_CPU_MAPS */ |