x86-64, NUMA: Fix distance table handling
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86 / mm / numa_emulation.c
blob0afa25d967baeedf002f71ea0f16f65a6ded6b99
1 /*
2 * NUMA emulation
3 */
4 #include <linux/kernel.h>
5 #include <linux/errno.h>
6 #include <linux/topology.h>
7 #include <linux/memblock.h>
8 #include <asm/dma.h>
10 #include "numa_internal.h"
12 static int emu_nid_to_phys[MAX_NUMNODES] __cpuinitdata;
13 static char *emu_cmdline __initdata;
15 void __init numa_emu_cmdline(char *str)
17 emu_cmdline = str;
20 static int __init emu_find_memblk_by_nid(int nid, const struct numa_meminfo *mi)
22 int i;
24 for (i = 0; i < mi->nr_blks; i++)
25 if (mi->blk[i].nid == nid)
26 return i;
27 return -ENOENT;
31 * Sets up nid to range from @start to @end. The return value is -errno if
32 * something went wrong, 0 otherwise.
34 static int __init emu_setup_memblk(struct numa_meminfo *ei,
35 struct numa_meminfo *pi,
36 int nid, int phys_blk, u64 size)
38 struct numa_memblk *eb = &ei->blk[ei->nr_blks];
39 struct numa_memblk *pb = &pi->blk[phys_blk];
41 if (ei->nr_blks >= NR_NODE_MEMBLKS) {
42 pr_err("NUMA: Too many emulated memblks, failing emulation\n");
43 return -EINVAL;
46 ei->nr_blks++;
47 eb->start = pb->start;
48 eb->end = pb->start + size;
49 eb->nid = nid;
51 if (emu_nid_to_phys[nid] == NUMA_NO_NODE)
52 emu_nid_to_phys[nid] = pb->nid;
54 pb->start += size;
55 if (pb->start >= pb->end) {
56 WARN_ON_ONCE(pb->start > pb->end);
57 numa_remove_memblk_from(phys_blk, pi);
60 printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
61 eb->start, eb->end, (eb->end - eb->start) >> 20);
62 return 0;
66 * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
67 * to max_addr. The return value is the number of nodes allocated.
69 static int __init split_nodes_interleave(struct numa_meminfo *ei,
70 struct numa_meminfo *pi,
71 u64 addr, u64 max_addr, int nr_nodes)
73 nodemask_t physnode_mask = NODE_MASK_NONE;
74 u64 size;
75 int big;
76 int nid = 0;
77 int i, ret;
79 if (nr_nodes <= 0)
80 return -1;
81 if (nr_nodes > MAX_NUMNODES) {
82 pr_info("numa=fake=%d too large, reducing to %d\n",
83 nr_nodes, MAX_NUMNODES);
84 nr_nodes = MAX_NUMNODES;
87 size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) / nr_nodes;
89 * Calculate the number of big nodes that can be allocated as a result
90 * of consolidating the remainder.
92 big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
93 FAKE_NODE_MIN_SIZE;
95 size &= FAKE_NODE_MIN_HASH_MASK;
96 if (!size) {
97 pr_err("Not enough memory for each node. "
98 "NUMA emulation disabled.\n");
99 return -1;
102 for (i = 0; i < pi->nr_blks; i++)
103 node_set(pi->blk[i].nid, physnode_mask);
106 * Continue to fill physical nodes with fake nodes until there is no
107 * memory left on any of them.
109 while (nodes_weight(physnode_mask)) {
110 for_each_node_mask(i, physnode_mask) {
111 u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
112 u64 start, limit, end;
113 int phys_blk;
115 phys_blk = emu_find_memblk_by_nid(i, pi);
116 if (phys_blk < 0) {
117 node_clear(i, physnode_mask);
118 continue;
120 start = pi->blk[phys_blk].start;
121 limit = pi->blk[phys_blk].end;
122 end = start + size;
124 if (nid < big)
125 end += FAKE_NODE_MIN_SIZE;
128 * Continue to add memory to this fake node if its
129 * non-reserved memory is less than the per-node size.
131 while (end - start -
132 memblock_x86_hole_size(start, end) < size) {
133 end += FAKE_NODE_MIN_SIZE;
134 if (end > limit) {
135 end = limit;
136 break;
141 * If there won't be at least FAKE_NODE_MIN_SIZE of
142 * non-reserved memory in ZONE_DMA32 for the next node,
143 * this one must extend to the boundary.
145 if (end < dma32_end && dma32_end - end -
146 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
147 end = dma32_end;
150 * If there won't be enough non-reserved memory for the
151 * next node, this one must extend to the end of the
152 * physical node.
154 if (limit - end -
155 memblock_x86_hole_size(end, limit) < size)
156 end = limit;
158 ret = emu_setup_memblk(ei, pi, nid++ % nr_nodes,
159 phys_blk,
160 min(end, limit) - start);
161 if (ret < 0)
162 return ret;
165 return 0;
169 * Returns the end address of a node so that there is at least `size' amount of
170 * non-reserved memory or `max_addr' is reached.
172 static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
174 u64 end = start + size;
176 while (end - start - memblock_x86_hole_size(start, end) < size) {
177 end += FAKE_NODE_MIN_SIZE;
178 if (end > max_addr) {
179 end = max_addr;
180 break;
183 return end;
187 * Sets up fake nodes of `size' interleaved over physical nodes ranging from
188 * `addr' to `max_addr'. The return value is the number of nodes allocated.
190 static int __init split_nodes_size_interleave(struct numa_meminfo *ei,
191 struct numa_meminfo *pi,
192 u64 addr, u64 max_addr, u64 size)
194 nodemask_t physnode_mask = NODE_MASK_NONE;
195 u64 min_size;
196 int nid = 0;
197 int i, ret;
199 if (!size)
200 return -1;
202 * The limit on emulated nodes is MAX_NUMNODES, so the size per node is
203 * increased accordingly if the requested size is too small. This
204 * creates a uniform distribution of node sizes across the entire
205 * machine (but not necessarily over physical nodes).
207 min_size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) /
208 MAX_NUMNODES;
209 min_size = max(min_size, FAKE_NODE_MIN_SIZE);
210 if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size)
211 min_size = (min_size + FAKE_NODE_MIN_SIZE) &
212 FAKE_NODE_MIN_HASH_MASK;
213 if (size < min_size) {
214 pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
215 size >> 20, min_size >> 20);
216 size = min_size;
218 size &= FAKE_NODE_MIN_HASH_MASK;
220 for (i = 0; i < pi->nr_blks; i++)
221 node_set(pi->blk[i].nid, physnode_mask);
224 * Fill physical nodes with fake nodes of size until there is no memory
225 * left on any of them.
227 while (nodes_weight(physnode_mask)) {
228 for_each_node_mask(i, physnode_mask) {
229 u64 dma32_end = MAX_DMA32_PFN << PAGE_SHIFT;
230 u64 start, limit, end;
231 int phys_blk;
233 phys_blk = emu_find_memblk_by_nid(i, pi);
234 if (phys_blk < 0) {
235 node_clear(i, physnode_mask);
236 continue;
238 start = pi->blk[phys_blk].start;
239 limit = pi->blk[phys_blk].end;
241 end = find_end_of_node(start, limit, size);
243 * If there won't be at least FAKE_NODE_MIN_SIZE of
244 * non-reserved memory in ZONE_DMA32 for the next node,
245 * this one must extend to the boundary.
247 if (end < dma32_end && dma32_end - end -
248 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
249 end = dma32_end;
252 * If there won't be enough non-reserved memory for the
253 * next node, this one must extend to the end of the
254 * physical node.
256 if (limit - end -
257 memblock_x86_hole_size(end, limit) < size)
258 end = limit;
260 ret = emu_setup_memblk(ei, pi, nid++ % MAX_NUMNODES,
261 phys_blk,
262 min(end, limit) - start);
263 if (ret < 0)
264 return ret;
267 return 0;
271 * numa_emulation - Emulate NUMA nodes
272 * @numa_meminfo: NUMA configuration to massage
273 * @numa_dist_cnt: The size of the physical NUMA distance table
275 * Emulate NUMA nodes according to the numa=fake kernel parameter.
276 * @numa_meminfo contains the physical memory configuration and is modified
277 * to reflect the emulated configuration on success. @numa_dist_cnt is
278 * used to determine the size of the physical distance table.
280 * On success, the following modifications are made.
282 * - @numa_meminfo is updated to reflect the emulated nodes.
284 * - __apicid_to_node[] is updated such that APIC IDs are mapped to the
285 * emulated nodes.
287 * - NUMA distance table is rebuilt to represent distances between emulated
288 * nodes. The distances are determined considering how emulated nodes
289 * are mapped to physical nodes and match the actual distances.
291 * - emu_nid_to_phys[] reflects how emulated nodes are mapped to physical
292 * nodes. This is used by numa_add_cpu() and numa_remove_cpu().
294 * If emulation is not enabled or fails, emu_nid_to_phys[] is filled with
295 * identity mapping and no other modification is made.
297 void __init numa_emulation(struct numa_meminfo *numa_meminfo, int numa_dist_cnt)
299 static struct numa_meminfo ei __initdata;
300 static struct numa_meminfo pi __initdata;
301 const u64 max_addr = max_pfn << PAGE_SHIFT;
302 u8 *phys_dist = NULL;
303 size_t phys_size = numa_dist_cnt * numa_dist_cnt * sizeof(phys_dist[0]);
304 int i, j, ret;
306 if (!emu_cmdline)
307 goto no_emu;
309 memset(&ei, 0, sizeof(ei));
310 pi = *numa_meminfo;
312 for (i = 0; i < MAX_NUMNODES; i++)
313 emu_nid_to_phys[i] = NUMA_NO_NODE;
316 * If the numa=fake command-line contains a 'M' or 'G', it represents
317 * the fixed node size. Otherwise, if it is just a single number N,
318 * split the system RAM into N fake nodes.
320 if (strchr(emu_cmdline, 'M') || strchr(emu_cmdline, 'G')) {
321 u64 size;
323 size = memparse(emu_cmdline, &emu_cmdline);
324 ret = split_nodes_size_interleave(&ei, &pi, 0, max_addr, size);
325 } else {
326 unsigned long n;
328 n = simple_strtoul(emu_cmdline, NULL, 0);
329 ret = split_nodes_interleave(&ei, &pi, 0, max_addr, n);
332 if (ret < 0)
333 goto no_emu;
335 if (numa_cleanup_meminfo(&ei) < 0) {
336 pr_warning("NUMA: Warning: constructed meminfo invalid, disabling emulation\n");
337 goto no_emu;
340 /* copy the physical distance table */
341 if (numa_dist_cnt) {
342 u64 phys;
344 phys = memblock_find_in_range(0,
345 (u64)max_pfn_mapped << PAGE_SHIFT,
346 phys_size, PAGE_SIZE);
347 if (phys == MEMBLOCK_ERROR) {
348 pr_warning("NUMA: Warning: can't allocate copy of distance table, disabling emulation\n");
349 goto no_emu;
351 memblock_x86_reserve_range(phys, phys + phys_size, "TMP NUMA DIST");
352 phys_dist = __va(phys);
354 for (i = 0; i < numa_dist_cnt; i++)
355 for (j = 0; j < numa_dist_cnt; j++)
356 phys_dist[i * numa_dist_cnt + j] =
357 node_distance(i, j);
360 /* commit */
361 *numa_meminfo = ei;
364 * Transform __apicid_to_node table to use emulated nids by
365 * reverse-mapping phys_nid. The maps should always exist but fall
366 * back to zero just in case.
368 for (i = 0; i < ARRAY_SIZE(__apicid_to_node); i++) {
369 if (__apicid_to_node[i] == NUMA_NO_NODE)
370 continue;
371 for (j = 0; j < ARRAY_SIZE(emu_nid_to_phys); j++)
372 if (__apicid_to_node[i] == emu_nid_to_phys[j])
373 break;
374 __apicid_to_node[i] = j < ARRAY_SIZE(emu_nid_to_phys) ? j : 0;
377 /* make sure all emulated nodes are mapped to a physical node */
378 for (i = 0; i < ARRAY_SIZE(emu_nid_to_phys); i++)
379 if (emu_nid_to_phys[i] == NUMA_NO_NODE)
380 emu_nid_to_phys[i] = 0;
382 /* transform distance table */
383 numa_reset_distance();
384 for (i = 0; i < MAX_NUMNODES; i++) {
385 for (j = 0; j < MAX_NUMNODES; j++) {
386 int physi = emu_nid_to_phys[i];
387 int physj = emu_nid_to_phys[j];
388 int dist;
390 if (physi >= numa_dist_cnt || physj >= numa_dist_cnt)
391 dist = physi == physj ?
392 LOCAL_DISTANCE : REMOTE_DISTANCE;
393 else
394 dist = phys_dist[physi * numa_dist_cnt + physj];
396 numa_set_distance(i, j, dist);
400 /* free the copied physical distance table */
401 if (phys_dist)
402 memblock_x86_free_range(__pa(phys_dist), __pa(phys_dist) + phys_size);
403 return;
405 no_emu:
406 /* No emulation. Build identity emu_nid_to_phys[] for numa_add_cpu() */
407 for (i = 0; i < ARRAY_SIZE(emu_nid_to_phys); i++)
408 emu_nid_to_phys[i] = i;
411 #ifndef CONFIG_DEBUG_PER_CPU_MAPS
412 void __cpuinit numa_add_cpu(int cpu)
414 int physnid, nid;
416 nid = numa_cpu_node(cpu);
417 if (nid == NUMA_NO_NODE)
418 nid = early_cpu_to_node(cpu);
419 BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
421 physnid = emu_nid_to_phys[nid];
424 * Map the cpu to each emulated node that is allocated on the physical
425 * node of the cpu's apic id.
427 for_each_online_node(nid)
428 if (emu_nid_to_phys[nid] == physnid)
429 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
432 void __cpuinit numa_remove_cpu(int cpu)
434 int i;
436 for_each_online_node(i)
437 cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
439 #else /* !CONFIG_DEBUG_PER_CPU_MAPS */
440 static void __cpuinit numa_set_cpumask(int cpu, int enable)
442 struct cpumask *mask;
443 int nid, physnid, i;
445 nid = early_cpu_to_node(cpu);
446 if (nid == NUMA_NO_NODE) {
447 /* early_cpu_to_node() already emits a warning and trace */
448 return;
451 physnid = emu_nid_to_phys[nid];
453 for_each_online_node(i) {
454 if (emu_nid_to_phys[nid] != physnid)
455 continue;
457 mask = debug_cpumask_set_cpu(cpu, enable);
458 if (!mask)
459 return;
461 if (enable)
462 cpumask_set_cpu(cpu, mask);
463 else
464 cpumask_clear_cpu(cpu, mask);
468 void __cpuinit numa_add_cpu(int cpu)
470 numa_set_cpumask(cpu, 1);
473 void __cpuinit numa_remove_cpu(int cpu)
475 numa_set_cpumask(cpu, 0);
477 #endif /* !CONFIG_DEBUG_PER_CPU_MAPS */