blob: bd086ebc0ffccf6d1453c68164de5f52d36c5c50 [file] [log] [blame]
Thomas Gleixnere3cfe522008-01-30 13:30:37 +01001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Generic VM initialization for x86-64 NUMA setups.
3 * Copyright 2002,2003 Andi Kleen, SuSE Labs.
Thomas Gleixnere3cfe522008-01-30 13:30:37 +01004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/kernel.h>
6#include <linux/mm.h>
7#include <linux/string.h>
8#include <linux/init.h>
9#include <linux/bootmem.h>
Yinghai Lu72d7c3b2010-08-25 13:39:17 -070010#include <linux/memblock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/mmzone.h>
12#include <linux/ctype.h>
13#include <linux/module.h>
14#include <linux/nodemask.h>
travis@sgi.com3cc87e32008-01-30 13:33:11 +010015#include <linux/sched.h>
Tejun Heod8fc3af2011-02-16 12:13:06 +010016#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <asm/e820.h>
19#include <asm/proto.h>
20#include <asm/dma.h>
21#include <asm/numa.h>
22#include <asm/acpi.h>
Andreas Herrmann23ac4ae2010-09-17 18:03:43 +020023#include <asm/amd_nb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Tejun Heo97e7b782011-02-16 17:11:08 +010025struct numa_memblk {
26 u64 start;
27 u64 end;
28 int nid;
29};
30
31struct numa_meminfo {
32 int nr_blks;
33 struct numa_memblk blk[NR_NODE_MEMBLKS];
34};
35
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -070036struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010037EXPORT_SYMBOL(node_data);
38
Tejun Heo92d4a432011-02-16 17:11:09 +010039nodemask_t numa_nodes_parsed __initdata;
Tejun Heoec8cf29b2011-02-16 12:13:07 +010040
Eric Dumazetdcf36bf2006-03-25 16:31:46 +010041struct memnode memnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Thomas Gleixner864fc312008-05-12 15:43:36 +020043static unsigned long __initdata nodemap_addr;
44static unsigned long __initdata nodemap_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Tejun Heo97e7b782011-02-16 17:11:08 +010046static struct numa_meminfo numa_meminfo __initdata;
Tejun Heoef396ec2011-02-16 17:11:07 +010047
Tejun Heoac7136b2011-02-16 17:11:09 +010048static int numa_distance_cnt;
49static u8 *numa_distance;
50
51#ifdef CONFIG_NUMA_EMU
52static bool numa_emu_dist;
53#endif
54
Brian Gerst6470aff2009-01-27 12:56:47 +090055/*
Eric Dumazet529a3402005-11-05 17:25:54 +010056 * Given a shift value, try to populate memnodemap[]
57 * Returns :
58 * 1 if OK
59 * 0 if memnodmap[] too small (of shift too small)
60 * -1 if node overlap or lost ram (shift too big)
61 */
Tejun Heo97e7b782011-02-16 17:11:08 +010062static int __init populate_memnodemap(const struct numa_meminfo *mi, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Eric Dumazet529a3402005-11-05 17:25:54 +010064 unsigned long addr, end;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010065 int i, res = -1;
Keith Manntheyb6846642005-07-28 21:15:38 -070066
travis@sgi.com43238382008-01-30 13:33:25 +010067 memset(memnodemap, 0xff, sizeof(s16)*memnodemapsize);
Tejun Heo97e7b782011-02-16 17:11:08 +010068 for (i = 0; i < mi->nr_blks; i++) {
69 addr = mi->blk[i].start;
70 end = mi->blk[i].end;
Eric Dumazet529a3402005-11-05 17:25:54 +010071 if (addr >= end)
72 continue;
Amul Shah076422d2007-02-13 13:26:19 +010073 if ((end >> shift) >= memnodemapsize)
Eric Dumazet529a3402005-11-05 17:25:54 +010074 return 0;
75 do {
travis@sgi.com43238382008-01-30 13:33:25 +010076 if (memnodemap[addr >> shift] != NUMA_NO_NODE)
Eric Dumazet529a3402005-11-05 17:25:54 +010077 return -1;
Tejun Heo97e7b782011-02-16 17:11:08 +010078 memnodemap[addr >> shift] = mi->blk[i].nid;
Amul Shah076422d2007-02-13 13:26:19 +010079 addr += (1UL << shift);
Eric Dumazet529a3402005-11-05 17:25:54 +010080 } while (addr < end);
81 res = 1;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010082 }
Eric Dumazet529a3402005-11-05 17:25:54 +010083 return res;
84}
85
Amul Shah076422d2007-02-13 13:26:19 +010086static int __init allocate_cachealigned_memnodemap(void)
87{
Yinghai Lu24a5da72008-02-01 17:49:41 +010088 unsigned long addr;
Amul Shah076422d2007-02-13 13:26:19 +010089
90 memnodemap = memnode.embedded_map;
travis@sgi.com316390b2008-01-30 13:33:15 +010091 if (memnodemapsize <= ARRAY_SIZE(memnode.embedded_map))
Amul Shah076422d2007-02-13 13:26:19 +010092 return 0;
Amul Shah076422d2007-02-13 13:26:19 +010093
Yinghai Lu24a5da72008-02-01 17:49:41 +010094 addr = 0x8000;
Joerg Roedelbe3e89e2008-07-25 16:48:58 +020095 nodemap_size = roundup(sizeof(s16) * memnodemapsize, L1_CACHE_BYTES);
Yinghai Ludbef7b52010-12-27 16:48:08 -080096 nodemap_addr = memblock_find_in_range(addr, get_max_mapped(),
Yinghai Lu24a5da72008-02-01 17:49:41 +010097 nodemap_size, L1_CACHE_BYTES);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070098 if (nodemap_addr == MEMBLOCK_ERROR) {
Amul Shah076422d2007-02-13 13:26:19 +010099 printk(KERN_ERR
100 "NUMA: Unable to allocate Memory to Node hash map\n");
101 nodemap_addr = nodemap_size = 0;
102 return -1;
103 }
Yinghai Lu24a5da72008-02-01 17:49:41 +0100104 memnodemap = phys_to_virt(nodemap_addr);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700105 memblock_x86_reserve_range(nodemap_addr, nodemap_addr + nodemap_size, "MEMNODEMAP");
Amul Shah076422d2007-02-13 13:26:19 +0100106
107 printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n",
108 nodemap_addr, nodemap_addr + nodemap_size);
109 return 0;
110}
111
112/*
113 * The LSB of all start and end addresses in the node map is the value of the
114 * maximum possible shift.
115 */
Tejun Heo97e7b782011-02-16 17:11:08 +0100116static int __init extract_lsb_from_nodes(const struct numa_meminfo *mi)
Amul Shah076422d2007-02-13 13:26:19 +0100117{
Amul Shah54413922007-02-13 13:26:20 +0100118 int i, nodes_used = 0;
Amul Shah076422d2007-02-13 13:26:19 +0100119 unsigned long start, end;
120 unsigned long bitfield = 0, memtop = 0;
121
Tejun Heo97e7b782011-02-16 17:11:08 +0100122 for (i = 0; i < mi->nr_blks; i++) {
123 start = mi->blk[i].start;
124 end = mi->blk[i].end;
Amul Shah076422d2007-02-13 13:26:19 +0100125 if (start >= end)
126 continue;
Amul Shah54413922007-02-13 13:26:20 +0100127 bitfield |= start;
128 nodes_used++;
Amul Shah076422d2007-02-13 13:26:19 +0100129 if (end > memtop)
130 memtop = end;
131 }
Amul Shah54413922007-02-13 13:26:20 +0100132 if (nodes_used <= 1)
133 i = 63;
134 else
135 i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
Amul Shah076422d2007-02-13 13:26:19 +0100136 memnodemapsize = (memtop >> i)+1;
137 return i;
138}
139
Tejun Heo97e7b782011-02-16 17:11:08 +0100140static int __init compute_hash_shift(const struct numa_meminfo *mi)
Eric Dumazet529a3402005-11-05 17:25:54 +0100141{
Amul Shah076422d2007-02-13 13:26:19 +0100142 int shift;
Eric Dumazet529a3402005-11-05 17:25:54 +0100143
Tejun Heo97e7b782011-02-16 17:11:08 +0100144 shift = extract_lsb_from_nodes(mi);
Amul Shah076422d2007-02-13 13:26:19 +0100145 if (allocate_cachealigned_memnodemap())
146 return -1;
Andi Kleen6b050f82006-01-11 22:44:33 +0100147 printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
Eric Dumazet529a3402005-11-05 17:25:54 +0100148 shift);
149
Tejun Heo97e7b782011-02-16 17:11:08 +0100150 if (populate_memnodemap(mi, shift) != 1) {
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100151 printk(KERN_INFO "Your memory is not aligned you need to "
152 "rebuild your kernel with a bigger NODEMAPSIZE "
153 "shift=%d\n", shift);
Eric Dumazet529a3402005-11-05 17:25:54 +0100154 return -1;
155 }
Keith Manntheyb6846642005-07-28 21:15:38 -0700156 return shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -0800159int __meminit __early_pfn_to_nid(unsigned long pfn)
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700160{
161 return phys_to_nid(pfn << PAGE_SHIFT);
162}
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700163
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100164static void * __init early_node_mem(int nodeid, unsigned long start,
Yinghai Lu24a5da72008-02-01 17:49:41 +0100165 unsigned long end, unsigned long size,
166 unsigned long align)
Andi Kleena8062232006-04-07 19:49:21 +0200167{
Yinghai Lucef625e2010-02-10 01:20:18 -0800168 unsigned long mem;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100169
Yinghai Lucef625e2010-02-10 01:20:18 -0800170 /*
171 * put it on high as possible
172 * something will go with NODE_DATA
173 */
174 if (start < (MAX_DMA_PFN<<PAGE_SHIFT))
175 start = MAX_DMA_PFN<<PAGE_SHIFT;
176 if (start < (MAX_DMA32_PFN<<PAGE_SHIFT) &&
177 end > (MAX_DMA32_PFN<<PAGE_SHIFT))
178 start = MAX_DMA32_PFN<<PAGE_SHIFT;
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700179 mem = memblock_x86_find_in_range_node(nodeid, start, end, size, align);
180 if (mem != MEMBLOCK_ERROR)
Andi Kleena8062232006-04-07 19:49:21 +0200181 return __va(mem);
Yinghai Lu9347e0b2008-02-01 17:49:42 +0100182
Yinghai Lucef625e2010-02-10 01:20:18 -0800183 /* extend the search scope */
184 end = max_pfn_mapped << PAGE_SHIFT;
Yinghai Lu419db272010-10-28 09:50:17 -0700185 start = MAX_DMA_PFN << PAGE_SHIFT;
186 mem = memblock_find_in_range(start, end, size, align);
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700187 if (mem != MEMBLOCK_ERROR)
Yinghai Lu1842f902010-02-10 01:20:15 -0800188 return __va(mem);
189
190 printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100191 size, nodeid);
Yinghai Lu1842f902010-02-10 01:20:15 -0800192
193 return NULL;
Andi Kleena8062232006-04-07 19:49:21 +0200194}
195
Tejun Heod9c515e2011-02-16 17:11:10 +0100196static int __init numa_add_memblk_to(int nid, u64 start, u64 end,
197 struct numa_meminfo *mi)
Tejun Heoef396ec2011-02-16 17:11:07 +0100198{
Tejun Heo56e827f2011-02-16 17:11:09 +0100199 /* ignore zero length blks */
200 if (start == end)
201 return 0;
202
203 /* whine about and ignore invalid blks */
204 if (start > end || nid < 0 || nid >= MAX_NUMNODES) {
205 pr_warning("NUMA: Warning: invalid memblk node %d (%Lx-%Lx)\n",
206 nid, start, end);
207 return 0;
208 }
209
210 if (mi->nr_blks >= NR_NODE_MEMBLKS) {
211 pr_err("NUMA: too many memblk ranges\n");
Tejun Heoef396ec2011-02-16 17:11:07 +0100212 return -EINVAL;
213 }
214
Tejun Heo97e7b782011-02-16 17:11:08 +0100215 mi->blk[mi->nr_blks].start = start;
216 mi->blk[mi->nr_blks].end = end;
217 mi->blk[mi->nr_blks].nid = nid;
218 mi->nr_blks++;
Tejun Heoef396ec2011-02-16 17:11:07 +0100219 return 0;
220}
221
Tejun Heo2e756be2011-02-16 17:11:09 +0100222static void __init numa_remove_memblk_from(int idx, struct numa_meminfo *mi)
223{
224 mi->nr_blks--;
225 memmove(&mi->blk[idx], &mi->blk[idx + 1],
226 (mi->nr_blks - idx) * sizeof(mi->blk[0]));
227}
228
Tejun Heod9c515e2011-02-16 17:11:10 +0100229int __init numa_add_memblk(int nid, u64 start, u64 end)
230{
231 return numa_add_memblk_to(nid, start, end, &numa_meminfo);
232}
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234/* Initialize bootmem allocator for a node */
Yinghai Lu7c437692009-05-15 13:59:37 -0700235void __init
236setup_node_bootmem(int nodeid, unsigned long start, unsigned long end)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100237{
Yinghai Lu08677212010-02-10 01:20:20 -0800238 unsigned long start_pfn, last_pfn, nodedata_phys;
Yinghai Lu7c437692009-05-15 13:59:37 -0700239 const int pgdat_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
Yinghai Lu1a27fc02008-03-18 12:52:37 -0700240 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Yinghai Lu4c31e922009-04-22 14:19:27 -0700242 if (!end)
243 return;
244
Yinghai Lu7c437692009-05-15 13:59:37 -0700245 /*
246 * Don't confuse VM with a node that doesn't have the
247 * minimum amount of memory:
248 */
249 if (end && (end - start) < NODE_MIN_SIZE)
250 return;
251
Joerg Roedelbe3e89e2008-07-25 16:48:58 +0200252 start = roundup(start, ZONE_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Yinghai Lu08677212010-02-10 01:20:20 -0800254 printk(KERN_INFO "Initmem setup node %d %016lx-%016lx\n", nodeid,
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100255 start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 start_pfn = start >> PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200258 last_pfn = end >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Yinghai Lu24a5da72008-02-01 17:49:41 +0100260 node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
261 SMP_CACHE_BYTES);
Andi Kleena8062232006-04-07 19:49:21 +0200262 if (node_data[nodeid] == NULL)
263 return;
264 nodedata_phys = __pa(node_data[nodeid]);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700265 memblock_x86_reserve_range(nodedata_phys, nodedata_phys + pgdat_size, "NODE_DATA");
Yinghai Lu6118f762008-02-04 16:47:56 +0100266 printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
267 nodedata_phys + pgdat_size - 1);
Yinghai Lu1842f902010-02-10 01:20:15 -0800268 nid = phys_to_nid(nodedata_phys);
269 if (nid != nodeid)
270 printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nodeid, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
Yinghai Lu08677212010-02-10 01:20:20 -0800273 NODE_DATA(nodeid)->node_id = nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 NODE_DATA(nodeid)->node_start_pfn = start_pfn;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200275 NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 node_set_online(nodeid);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100278}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Tejun Heof9c60252011-02-16 17:11:09 +0100280static int __init numa_cleanup_meminfo(struct numa_meminfo *mi)
Tejun Heofd0435d2011-02-16 17:11:08 +0100281{
Tejun Heo56e827f2011-02-16 17:11:09 +0100282 const u64 low = 0;
283 const u64 high = (u64)max_pfn << PAGE_SHIFT;
Tejun Heo2e756be2011-02-16 17:11:09 +0100284 int i, j, k;
Tejun Heoef396ec2011-02-16 17:11:07 +0100285
Tejun Heo2e756be2011-02-16 17:11:09 +0100286 for (i = 0; i < mi->nr_blks; i++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100287 struct numa_memblk *bi = &mi->blk[i];
Tejun Heoef396ec2011-02-16 17:11:07 +0100288
Tejun Heo56e827f2011-02-16 17:11:09 +0100289 /* make sure all blocks are inside the limits */
290 bi->start = max(bi->start, low);
291 bi->end = min(bi->end, high);
292
293 /* and there's no empty block */
294 if (bi->start == bi->end) {
295 numa_remove_memblk_from(i--, mi);
296 continue;
297 }
298
Tejun Heo2e756be2011-02-16 17:11:09 +0100299 for (j = i + 1; j < mi->nr_blks; j++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100300 struct numa_memblk *bj = &mi->blk[j];
Tejun Heoef396ec2011-02-16 17:11:07 +0100301 unsigned long start, end;
302
Tejun Heo2e756be2011-02-16 17:11:09 +0100303 /*
Tejun Heo56e827f2011-02-16 17:11:09 +0100304 * See whether there are overlapping blocks. Whine
305 * about but allow overlaps of the same nid. They
306 * will be merged below.
307 */
308 if (bi->end > bj->start && bi->start < bj->end) {
309 if (bi->nid != bj->nid) {
310 pr_err("NUMA: node %d (%Lx-%Lx) overlaps with node %d (%Lx-%Lx)\n",
311 bi->nid, bi->start, bi->end,
312 bj->nid, bj->start, bj->end);
313 return -EINVAL;
314 }
315 pr_warning("NUMA: Warning: node %d (%Lx-%Lx) overlaps with itself (%Lx-%Lx)\n",
316 bi->nid, bi->start, bi->end,
317 bj->start, bj->end);
318 }
319
320 /*
Tejun Heo2e756be2011-02-16 17:11:09 +0100321 * Join together blocks on the same node, holes
322 * between which don't overlap with memory on other
323 * nodes.
324 */
Tejun Heo97e7b782011-02-16 17:11:08 +0100325 if (bi->nid != bj->nid)
Tejun Heoef396ec2011-02-16 17:11:07 +0100326 continue;
Tejun Heo56e827f2011-02-16 17:11:09 +0100327 start = max(min(bi->start, bj->start), low);
328 end = min(max(bi->end, bj->end), high);
Tejun Heo2e756be2011-02-16 17:11:09 +0100329 for (k = 0; k < mi->nr_blks; k++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100330 struct numa_memblk *bk = &mi->blk[k];
331
332 if (bi->nid == bk->nid)
Tejun Heoef396ec2011-02-16 17:11:07 +0100333 continue;
Tejun Heo97e7b782011-02-16 17:11:08 +0100334 if (start < bk->end && end > bk->start)
Tejun Heoef396ec2011-02-16 17:11:07 +0100335 break;
336 }
Tejun Heo97e7b782011-02-16 17:11:08 +0100337 if (k < mi->nr_blks)
Tejun Heoef396ec2011-02-16 17:11:07 +0100338 continue;
Tejun Heoef396ec2011-02-16 17:11:07 +0100339 printk(KERN_INFO "NUMA: Node %d [%Lx,%Lx) + [%Lx,%Lx) -> [%lx,%lx)\n",
Tejun Heo97e7b782011-02-16 17:11:08 +0100340 bi->nid, bi->start, bi->end, bj->start, bj->end,
Tejun Heoef396ec2011-02-16 17:11:07 +0100341 start, end);
Tejun Heo97e7b782011-02-16 17:11:08 +0100342 bi->start = start;
343 bi->end = end;
Tejun Heo2e756be2011-02-16 17:11:09 +0100344 numa_remove_memblk_from(j--, mi);
Tejun Heoef396ec2011-02-16 17:11:07 +0100345 }
346 }
347
Tejun Heo56e827f2011-02-16 17:11:09 +0100348 for (i = mi->nr_blks; i < ARRAY_SIZE(mi->blk); i++) {
349 mi->blk[i].start = mi->blk[i].end = 0;
350 mi->blk[i].nid = NUMA_NO_NODE;
351 }
352
Tejun Heof9c60252011-02-16 17:11:09 +0100353 return 0;
354}
355
356/*
Tejun Heo4697bdc2011-02-16 17:11:09 +0100357 * Set nodes, which have memory in @mi, in *@nodemask.
358 */
359static void __init numa_nodemask_from_meminfo(nodemask_t *nodemask,
360 const struct numa_meminfo *mi)
361{
362 int i;
363
364 for (i = 0; i < ARRAY_SIZE(mi->blk); i++)
365 if (mi->blk[i].start != mi->blk[i].end &&
366 mi->blk[i].nid != NUMA_NO_NODE)
367 node_set(mi->blk[i].nid, *nodemask);
368}
369
370/*
Tejun Heoac7136b2011-02-16 17:11:09 +0100371 * Reset distance table. The current table is freed. The next
372 * numa_set_distance() call will create a new one.
373 */
374static void __init numa_reset_distance(void)
375{
376 size_t size;
377
378 size = numa_distance_cnt * sizeof(numa_distance[0]);
379 memblock_x86_free_range(__pa(numa_distance),
380 __pa(numa_distance) + size);
381 numa_distance = NULL;
382 numa_distance_cnt = 0;
383}
384
385/*
386 * Set the distance between node @from to @to to @distance. If distance
387 * table doesn't exist, one which is large enough to accomodate all the
388 * currently known nodes will be created.
389 */
390void __init numa_set_distance(int from, int to, int distance)
391{
392 if (!numa_distance) {
393 nodemask_t nodes_parsed;
394 size_t size;
395 int i, j, cnt = 0;
396 u64 phys;
397
398 /* size the new table and allocate it */
399 nodes_parsed = numa_nodes_parsed;
400 numa_nodemask_from_meminfo(&nodes_parsed, &numa_meminfo);
401
402 for_each_node_mask(i, nodes_parsed)
403 cnt = i;
404 size = ++cnt * sizeof(numa_distance[0]);
405
406 phys = memblock_find_in_range(0,
407 (u64)max_pfn_mapped << PAGE_SHIFT,
408 size, PAGE_SIZE);
409 if (phys == MEMBLOCK_ERROR) {
410 pr_warning("NUMA: Warning: can't allocate distance table!\n");
411 /* don't retry until explicitly reset */
412 numa_distance = (void *)1LU;
413 return;
414 }
415 memblock_x86_reserve_range(phys, phys + size, "NUMA DIST");
416
417 numa_distance = __va(phys);
418 numa_distance_cnt = cnt;
419
420 /* fill with the default distances */
421 for (i = 0; i < cnt; i++)
422 for (j = 0; j < cnt; j++)
423 numa_distance[i * cnt + j] = i == j ?
424 LOCAL_DISTANCE : REMOTE_DISTANCE;
425 printk(KERN_DEBUG "NUMA: Initialized distance table, cnt=%d\n", cnt);
426 }
427
428 if (from >= numa_distance_cnt || to >= numa_distance_cnt) {
429 printk_once(KERN_DEBUG "NUMA: Debug: distance out of bound, from=%d to=%d distance=%d\n",
430 from, to, distance);
431 return;
432 }
433
434 if ((u8)distance != distance ||
435 (from == to && distance != LOCAL_DISTANCE)) {
436 pr_warn_once("NUMA: Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
437 from, to, distance);
438 return;
439 }
440
441 numa_distance[from * numa_distance_cnt + to] = distance;
442}
443
444int __node_distance(int from, int to)
445{
446#if defined(CONFIG_ACPI_NUMA) && defined(CONFIG_NUMA_EMU)
447 if (numa_emu_dist)
448 return acpi_emu_node_distance(from, to);
449#endif
450 if (from >= numa_distance_cnt || to >= numa_distance_cnt)
451 return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
452 return numa_distance[from * numa_distance_cnt + to];
453}
454EXPORT_SYMBOL(__node_distance);
455
456/*
Tejun Heof9c60252011-02-16 17:11:09 +0100457 * Sanity check to catch more bad NUMA configurations (they are amazingly
458 * common). Make sure the nodes cover all memory.
459 */
Tejun Heo91556232011-02-16 17:11:09 +0100460static bool __init numa_meminfo_cover_memory(const struct numa_meminfo *mi)
Tejun Heof9c60252011-02-16 17:11:09 +0100461{
462 unsigned long numaram, e820ram;
463 int i;
464
465 numaram = 0;
Tejun Heo91556232011-02-16 17:11:09 +0100466 for (i = 0; i < mi->nr_blks; i++) {
467 unsigned long s = mi->blk[i].start >> PAGE_SHIFT;
468 unsigned long e = mi->blk[i].end >> PAGE_SHIFT;
Tejun Heof9c60252011-02-16 17:11:09 +0100469 numaram += e - s;
Tejun Heo91556232011-02-16 17:11:09 +0100470 numaram -= __absent_pages_in_range(mi->blk[i].nid, s, e);
Tejun Heof9c60252011-02-16 17:11:09 +0100471 if ((long)numaram < 0)
472 numaram = 0;
473 }
474
475 e820ram = max_pfn - (memblock_x86_hole_size(0,
476 max_pfn << PAGE_SHIFT) >> PAGE_SHIFT);
477 /* We seem to lose 3 pages somewhere. Allow 1M of slack. */
478 if ((long)(e820ram - numaram) >= (1 << (20 - PAGE_SHIFT))) {
479 printk(KERN_ERR "NUMA: nodes only cover %luMB of your %luMB e820 RAM. Not used.\n",
480 (numaram << PAGE_SHIFT) >> 20,
481 (e820ram << PAGE_SHIFT) >> 20);
Tejun Heo91556232011-02-16 17:11:09 +0100482 return false;
Tejun Heof9c60252011-02-16 17:11:09 +0100483 }
Tejun Heo91556232011-02-16 17:11:09 +0100484 return true;
Tejun Heof9c60252011-02-16 17:11:09 +0100485}
486
487static int __init numa_register_memblks(struct numa_meminfo *mi)
488{
Tejun Heo91556232011-02-16 17:11:09 +0100489 int i, j, nid;
Tejun Heof9c60252011-02-16 17:11:09 +0100490
491 /* Account for nodes with cpus and no memory */
Tejun Heo4697bdc2011-02-16 17:11:09 +0100492 node_possible_map = numa_nodes_parsed;
493 numa_nodemask_from_meminfo(&node_possible_map, mi);
Tejun Heof9c60252011-02-16 17:11:09 +0100494 if (WARN_ON(nodes_empty(node_possible_map)))
495 return -EINVAL;
496
Tejun Heo97e7b782011-02-16 17:11:08 +0100497 memnode_shift = compute_hash_shift(mi);
Tejun Heoef396ec2011-02-16 17:11:07 +0100498 if (memnode_shift < 0) {
499 printk(KERN_ERR "NUMA: No NUMA node hash function found. Contact maintainer\n");
500 return -EINVAL;
501 }
502
Tejun Heo97e7b782011-02-16 17:11:08 +0100503 for (i = 0; i < mi->nr_blks; i++)
504 memblock_x86_register_active_regions(mi->blk[i].nid,
505 mi->blk[i].start >> PAGE_SHIFT,
506 mi->blk[i].end >> PAGE_SHIFT);
Tejun Heofd0435d2011-02-16 17:11:08 +0100507
508 /* for out of order entries */
509 sort_node_map();
Tejun Heo91556232011-02-16 17:11:09 +0100510 if (!numa_meminfo_cover_memory(mi))
Tejun Heofd0435d2011-02-16 17:11:08 +0100511 return -EINVAL;
512
513 init_memory_mapping_high();
514
Tejun Heofd0435d2011-02-16 17:11:08 +0100515 /*
Tejun Heo91556232011-02-16 17:11:09 +0100516 * Finally register nodes. Do it twice in case setup_node_bootmem
517 * missed one due to missing bootmem.
Tejun Heofd0435d2011-02-16 17:11:08 +0100518 */
Tejun Heo91556232011-02-16 17:11:09 +0100519 for (i = 0; i < 2; i++) {
520 for_each_node_mask(nid, node_possible_map) {
521 u64 start = (u64)max_pfn << PAGE_SHIFT;
522 u64 end = 0;
523
524 if (node_online(nid))
525 continue;
526
527 for (j = 0; j < mi->nr_blks; j++) {
528 if (nid != mi->blk[j].nid)
529 continue;
530 start = min(mi->blk[j].start, start);
531 end = max(mi->blk[j].end, end);
532 }
533
534 if (start < end)
535 setup_node_bootmem(nid, start, end);
536 }
537 }
Tejun Heofd0435d2011-02-16 17:11:08 +0100538
Tejun Heoef396ec2011-02-16 17:11:07 +0100539 return 0;
540}
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542#ifdef CONFIG_NUMA_EMU
Rohit Seth53fee042007-02-13 13:26:22 +0100543/* Numa emulation */
Tejun Heo9d073ca2011-02-16 17:11:10 +0100544static int emu_nid_to_phys[MAX_NUMNODES] __cpuinitdata;
Tejun Heod9c515e2011-02-16 17:11:10 +0100545static char *emu_cmdline __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Jan Beulich90321602011-01-19 08:57:21 +0000547void __init numa_emu_cmdline(char *str)
548{
Tejun Heod9c515e2011-02-16 17:11:10 +0100549 emu_cmdline = str;
Jan Beulich90321602011-01-19 08:57:21 +0000550}
551
Tejun Heo1cca5342011-02-16 17:11:10 +0100552static int __init emu_find_memblk_by_nid(int nid, const struct numa_meminfo *mi)
553{
554 int i;
555
556 for (i = 0; i < mi->nr_blks; i++)
557 if (mi->blk[i].nid == nid)
558 return i;
559 return -ENOENT;
560}
561
Tejun Heoa844ef42011-02-16 17:11:09 +0100562int __init find_node_by_addr(unsigned long addr)
563{
Tejun Heo91556232011-02-16 17:11:09 +0100564 const struct numa_meminfo *mi = &numa_meminfo;
Tejun Heoa844ef42011-02-16 17:11:09 +0100565 int i;
566
Tejun Heo91556232011-02-16 17:11:09 +0100567 for (i = 0; i < mi->nr_blks; i++) {
Tejun Heoa844ef42011-02-16 17:11:09 +0100568 /*
569 * Find the real node that this emulated node appears on. For
570 * the sake of simplicity, we only use a real node's starting
571 * address to determine which emulated node it appears on.
572 */
Tejun Heo91556232011-02-16 17:11:09 +0100573 if (addr >= mi->blk[i].start && addr < mi->blk[i].end)
574 return mi->blk[i].nid;
Tejun Heoa844ef42011-02-16 17:11:09 +0100575 }
Tejun Heo91556232011-02-16 17:11:09 +0100576 return NUMA_NO_NODE;
Tejun Heoa844ef42011-02-16 17:11:09 +0100577}
578
Tejun Heoc88aea72011-02-16 17:11:10 +0100579static void __init fake_physnodes(int acpi, int amd,
580 const struct numa_meminfo *ei)
David Rientjesf51bf302010-12-22 17:23:51 -0800581{
Tejun Heoc88aea72011-02-16 17:11:10 +0100582 static struct bootnode nodes[MAX_NUMNODES] __initdata;
583 int i, nr_nodes = 0;
584
585 for (i = 0; i < ei->nr_blks; i++) {
586 int nid = ei->blk[i].nid;
587
588 if (nodes[nid].start == nodes[nid].end) {
589 nodes[nid].start = ei->blk[i].start;
590 nodes[nid].end = ei->blk[i].end;
591 nr_nodes++;
592 } else {
593 nodes[nid].start = min(ei->blk[i].start, nodes[nid].start);
594 nodes[nid].end = max(ei->blk[i].end, nodes[nid].end);
595 }
596 }
David Rientjesf51bf302010-12-22 17:23:51 -0800597
598 BUG_ON(acpi && amd);
599#ifdef CONFIG_ACPI_NUMA
600 if (acpi)
601 acpi_fake_nodes(nodes, nr_nodes);
602#endif
603#ifdef CONFIG_AMD_NUMA
604 if (amd)
605 amd_fake_nodes(nodes, nr_nodes);
606#endif
607 if (!acpi && !amd)
608 for (i = 0; i < nr_cpu_ids; i++)
609 numa_set_node(i, 0);
610}
611
Rohit Seth53fee042007-02-13 13:26:22 +0100612/*
Tejun Heoc88aea72011-02-16 17:11:10 +0100613 * Sets up nid to range from @start to @end. The return value is -errno if
614 * something went wrong, 0 otherwise.
Rohit Seth53fee042007-02-13 13:26:22 +0100615 */
Tejun Heoc88aea72011-02-16 17:11:10 +0100616static int __init emu_setup_memblk(struct numa_meminfo *ei,
Tejun Heo1cca5342011-02-16 17:11:10 +0100617 struct numa_meminfo *pi,
618 int nid, int phys_blk, u64 size)
Rohit Seth53fee042007-02-13 13:26:22 +0100619{
Tejun Heoc88aea72011-02-16 17:11:10 +0100620 struct numa_memblk *eb = &ei->blk[ei->nr_blks];
Tejun Heo1cca5342011-02-16 17:11:10 +0100621 struct numa_memblk *pb = &pi->blk[phys_blk];
Tejun Heoc88aea72011-02-16 17:11:10 +0100622
623 if (ei->nr_blks >= NR_NODE_MEMBLKS) {
624 pr_err("NUMA: Too many emulated memblks, failing emulation\n");
625 return -EINVAL;
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200626 }
Tejun Heoc88aea72011-02-16 17:11:10 +0100627
628 ei->nr_blks++;
Tejun Heo1cca5342011-02-16 17:11:10 +0100629 eb->start = pb->start;
630 eb->end = pb->start + size;
Tejun Heoc88aea72011-02-16 17:11:10 +0100631 eb->nid = nid;
Tejun Heo9d073ca2011-02-16 17:11:10 +0100632
633 if (emu_nid_to_phys[nid] == NUMA_NO_NODE)
Tejun Heo1cca5342011-02-16 17:11:10 +0100634 emu_nid_to_phys[nid] = pb->nid;
635
636 pb->start += size;
637 if (pb->start >= pb->end) {
638 WARN_ON_ONCE(pb->start > pb->end);
639 numa_remove_memblk_from(phys_blk, pi);
640 }
Tejun Heo9d073ca2011-02-16 17:11:10 +0100641
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200642 printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
Tejun Heoc88aea72011-02-16 17:11:10 +0100643 eb->start, eb->end, (eb->end - eb->start) >> 20);
644 return 0;
Rohit Seth53fee042007-02-13 13:26:22 +0100645}
646
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200647/*
David Rientjesadc19382009-09-25 15:20:09 -0700648 * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
649 * to max_addr. The return value is the number of nodes allocated.
650 */
Tejun Heoc88aea72011-02-16 17:11:10 +0100651static int __init split_nodes_interleave(struct numa_meminfo *ei,
Tejun Heo1cca5342011-02-16 17:11:10 +0100652 struct numa_meminfo *pi,
Tejun Heoc88aea72011-02-16 17:11:10 +0100653 u64 addr, u64 max_addr, int nr_nodes)
David Rientjesadc19382009-09-25 15:20:09 -0700654{
655 nodemask_t physnode_mask = NODE_MASK_NONE;
656 u64 size;
657 int big;
Tejun Heoc88aea72011-02-16 17:11:10 +0100658 int nid = 0;
659 int i, ret;
David Rientjesadc19382009-09-25 15:20:09 -0700660
661 if (nr_nodes <= 0)
662 return -1;
663 if (nr_nodes > MAX_NUMNODES) {
664 pr_info("numa=fake=%d too large, reducing to %d\n",
665 nr_nodes, MAX_NUMNODES);
666 nr_nodes = MAX_NUMNODES;
667 }
668
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700669 size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) / nr_nodes;
David Rientjesadc19382009-09-25 15:20:09 -0700670 /*
671 * Calculate the number of big nodes that can be allocated as a result
672 * of consolidating the remainder.
673 */
David Rientjes68fd1112010-02-15 13:43:25 -0800674 big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
David Rientjesadc19382009-09-25 15:20:09 -0700675 FAKE_NODE_MIN_SIZE;
676
677 size &= FAKE_NODE_MIN_HASH_MASK;
678 if (!size) {
679 pr_err("Not enough memory for each node. "
680 "NUMA emulation disabled.\n");
681 return -1;
682 }
683
Tejun Heo1cca5342011-02-16 17:11:10 +0100684 for (i = 0; i < pi->nr_blks; i++)
685 node_set(pi->blk[i].nid, physnode_mask);
David Rientjesadc19382009-09-25 15:20:09 -0700686
687 /*
688 * Continue to fill physical nodes with fake nodes until there is no
689 * memory left on any of them.
690 */
691 while (nodes_weight(physnode_mask)) {
692 for_each_node_mask(i, physnode_mask) {
David Rientjesadc19382009-09-25 15:20:09 -0700693 u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
Tejun Heo1cca5342011-02-16 17:11:10 +0100694 u64 start, limit, end;
695 int phys_blk;
696
697 phys_blk = emu_find_memblk_by_nid(i, pi);
698 if (phys_blk < 0) {
699 node_clear(i, physnode_mask);
700 continue;
701 }
702 start = pi->blk[phys_blk].start;
703 limit = pi->blk[phys_blk].end;
704 end = start + size;
David Rientjesadc19382009-09-25 15:20:09 -0700705
Tejun Heoc88aea72011-02-16 17:11:10 +0100706 if (nid < big)
David Rientjesadc19382009-09-25 15:20:09 -0700707 end += FAKE_NODE_MIN_SIZE;
708
709 /*
710 * Continue to add memory to this fake node if its
711 * non-reserved memory is less than the per-node size.
712 */
Tejun Heo1cca5342011-02-16 17:11:10 +0100713 while (end - start -
714 memblock_x86_hole_size(start, end) < size) {
David Rientjesadc19382009-09-25 15:20:09 -0700715 end += FAKE_NODE_MIN_SIZE;
Tejun Heo1cca5342011-02-16 17:11:10 +0100716 if (end > limit) {
717 end = limit;
David Rientjesadc19382009-09-25 15:20:09 -0700718 break;
719 }
720 }
721
722 /*
723 * If there won't be at least FAKE_NODE_MIN_SIZE of
724 * non-reserved memory in ZONE_DMA32 for the next node,
725 * this one must extend to the boundary.
726 */
727 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700728 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjesadc19382009-09-25 15:20:09 -0700729 end = dma32_end;
730
731 /*
732 * If there won't be enough non-reserved memory for the
733 * next node, this one must extend to the end of the
734 * physical node.
735 */
Tejun Heo1cca5342011-02-16 17:11:10 +0100736 if (limit - end -
737 memblock_x86_hole_size(end, limit) < size)
738 end = limit;
David Rientjesadc19382009-09-25 15:20:09 -0700739
Tejun Heo1cca5342011-02-16 17:11:10 +0100740 ret = emu_setup_memblk(ei, pi, nid++ % nr_nodes,
741 phys_blk,
742 min(end, limit) - start);
Tejun Heoc88aea72011-02-16 17:11:10 +0100743 if (ret < 0)
744 return ret;
David Rientjesadc19382009-09-25 15:20:09 -0700745 }
746 }
Tejun Heoc88aea72011-02-16 17:11:10 +0100747 return 0;
David Rientjesadc19382009-09-25 15:20:09 -0700748}
749
750/*
David Rientjes8df5bb342010-02-15 13:43:30 -0800751 * Returns the end address of a node so that there is at least `size' amount of
752 * non-reserved memory or `max_addr' is reached.
753 */
754static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
755{
756 u64 end = start + size;
757
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700758 while (end - start - memblock_x86_hole_size(start, end) < size) {
David Rientjes8df5bb342010-02-15 13:43:30 -0800759 end += FAKE_NODE_MIN_SIZE;
760 if (end > max_addr) {
761 end = max_addr;
762 break;
763 }
764 }
765 return end;
766}
767
768/*
769 * Sets up fake nodes of `size' interleaved over physical nodes ranging from
770 * `addr' to `max_addr'. The return value is the number of nodes allocated.
771 */
Tejun Heoc88aea72011-02-16 17:11:10 +0100772static int __init split_nodes_size_interleave(struct numa_meminfo *ei,
Tejun Heo1cca5342011-02-16 17:11:10 +0100773 struct numa_meminfo *pi,
Tejun Heoc88aea72011-02-16 17:11:10 +0100774 u64 addr, u64 max_addr, u64 size)
David Rientjes8df5bb342010-02-15 13:43:30 -0800775{
776 nodemask_t physnode_mask = NODE_MASK_NONE;
777 u64 min_size;
Tejun Heoc88aea72011-02-16 17:11:10 +0100778 int nid = 0;
779 int i, ret;
David Rientjes8df5bb342010-02-15 13:43:30 -0800780
781 if (!size)
782 return -1;
783 /*
784 * The limit on emulated nodes is MAX_NUMNODES, so the size per node is
785 * increased accordingly if the requested size is too small. This
786 * creates a uniform distribution of node sizes across the entire
787 * machine (but not necessarily over physical nodes).
788 */
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700789 min_size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) /
David Rientjes8df5bb342010-02-15 13:43:30 -0800790 MAX_NUMNODES;
791 min_size = max(min_size, FAKE_NODE_MIN_SIZE);
792 if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size)
793 min_size = (min_size + FAKE_NODE_MIN_SIZE) &
794 FAKE_NODE_MIN_HASH_MASK;
795 if (size < min_size) {
796 pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
797 size >> 20, min_size >> 20);
798 size = min_size;
799 }
800 size &= FAKE_NODE_MIN_HASH_MASK;
801
Tejun Heo1cca5342011-02-16 17:11:10 +0100802 for (i = 0; i < pi->nr_blks; i++)
803 node_set(pi->blk[i].nid, physnode_mask);
804
David Rientjes8df5bb342010-02-15 13:43:30 -0800805 /*
806 * Fill physical nodes with fake nodes of size until there is no memory
807 * left on any of them.
808 */
809 while (nodes_weight(physnode_mask)) {
810 for_each_node_mask(i, physnode_mask) {
811 u64 dma32_end = MAX_DMA32_PFN << PAGE_SHIFT;
Tejun Heo1cca5342011-02-16 17:11:10 +0100812 u64 start, limit, end;
813 int phys_blk;
David Rientjes8df5bb342010-02-15 13:43:30 -0800814
Tejun Heo1cca5342011-02-16 17:11:10 +0100815 phys_blk = emu_find_memblk_by_nid(i, pi);
816 if (phys_blk < 0) {
817 node_clear(i, physnode_mask);
818 continue;
819 }
820 start = pi->blk[phys_blk].start;
821 limit = pi->blk[phys_blk].end;
822
823 end = find_end_of_node(start, limit, size);
David Rientjes8df5bb342010-02-15 13:43:30 -0800824 /*
825 * If there won't be at least FAKE_NODE_MIN_SIZE of
826 * non-reserved memory in ZONE_DMA32 for the next node,
827 * this one must extend to the boundary.
828 */
829 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700830 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjes8df5bb342010-02-15 13:43:30 -0800831 end = dma32_end;
832
833 /*
834 * If there won't be enough non-reserved memory for the
835 * next node, this one must extend to the end of the
836 * physical node.
837 */
Tejun Heo1cca5342011-02-16 17:11:10 +0100838 if (limit - end -
839 memblock_x86_hole_size(end, limit) < size)
840 end = limit;
David Rientjes8df5bb342010-02-15 13:43:30 -0800841
Tejun Heo1cca5342011-02-16 17:11:10 +0100842 ret = emu_setup_memblk(ei, pi, nid++ % MAX_NUMNODES,
843 phys_blk,
844 min(end, limit) - start);
Tejun Heoc88aea72011-02-16 17:11:10 +0100845 if (ret < 0)
846 return ret;
David Rientjes8df5bb342010-02-15 13:43:30 -0800847 }
848 }
Tejun Heoc88aea72011-02-16 17:11:10 +0100849 return 0;
David Rientjes8df5bb342010-02-15 13:43:30 -0800850}
851
852/*
Thomas Gleixner886533a2008-05-12 15:43:36 +0200853 * Sets up the system RAM area from start_pfn to last_pfn according to the
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200854 * numa=fake command-line option.
855 */
Tejun Heoc88aea72011-02-16 17:11:10 +0100856static bool __init numa_emulation(int acpi, int amd)
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200857{
Tejun Heo97e7b782011-02-16 17:11:08 +0100858 static struct numa_meminfo ei __initdata;
Tejun Heo1cca5342011-02-16 17:11:10 +0100859 static struct numa_meminfo pi __initdata;
Tejun Heod9c515e2011-02-16 17:11:10 +0100860 const u64 max_addr = max_pfn << PAGE_SHIFT;
Tejun Heoc88aea72011-02-16 17:11:10 +0100861 int i, ret;
862
863 memset(&ei, 0, sizeof(ei));
Tejun Heo1cca5342011-02-16 17:11:10 +0100864 pi = numa_meminfo;
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200865
Tejun Heo9d073ca2011-02-16 17:11:10 +0100866 for (i = 0; i < MAX_NUMNODES; i++)
867 emu_nid_to_phys[i] = NUMA_NO_NODE;
868
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200869 /*
David Rientjes8df5bb342010-02-15 13:43:30 -0800870 * If the numa=fake command-line contains a 'M' or 'G', it represents
David Rientjesca2107c2010-02-15 13:43:33 -0800871 * the fixed node size. Otherwise, if it is just a single number N,
872 * split the system RAM into N fake nodes.
David Rientjes8df5bb342010-02-15 13:43:30 -0800873 */
Tejun Heod9c515e2011-02-16 17:11:10 +0100874 if (strchr(emu_cmdline, 'M') || strchr(emu_cmdline, 'G')) {
David Rientjesca2107c2010-02-15 13:43:33 -0800875 u64 size;
876
Tejun Heod9c515e2011-02-16 17:11:10 +0100877 size = memparse(emu_cmdline, &emu_cmdline);
Tejun Heo1cca5342011-02-16 17:11:10 +0100878 ret = split_nodes_size_interleave(&ei, &pi, 0, max_addr, size);
David Rientjesca2107c2010-02-15 13:43:33 -0800879 } else {
880 unsigned long n;
881
Tejun Heod9c515e2011-02-16 17:11:10 +0100882 n = simple_strtoul(emu_cmdline, NULL, 0);
Tejun Heo1cca5342011-02-16 17:11:10 +0100883 ret = split_nodes_interleave(&ei, &pi, 0, max_addr, n);
David Rientjes8df5bb342010-02-15 13:43:30 -0800884 }
885
Tejun Heoc88aea72011-02-16 17:11:10 +0100886 if (ret < 0)
887 return false;
888
889 if (numa_cleanup_meminfo(&ei) < 0) {
890 pr_warning("NUMA: Warning: constructed meminfo invalid, disabling emulation\n");
891 return false;
892 }
893
894 /* commit */
895 numa_meminfo = ei;
Tejun Heo8968dab2011-02-16 17:11:08 +0100896
Tejun Heo9d073ca2011-02-16 17:11:10 +0100897 /* make sure all emulated nodes are mapped to a physical node */
898 for (i = 0; i < ARRAY_SIZE(emu_nid_to_phys); i++)
899 if (emu_nid_to_phys[i] == NUMA_NO_NODE)
900 emu_nid_to_phys[i] = 0;
901
Tejun Heoc88aea72011-02-16 17:11:10 +0100902 fake_physnodes(acpi, amd, &ei);
Tejun Heoac7136b2011-02-16 17:11:09 +0100903 numa_emu_dist = true;
Tejun Heoc88aea72011-02-16 17:11:10 +0100904 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200906#endif /* CONFIG_NUMA_EMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Tejun Heoffe77a42011-02-16 12:13:06 +0100908static int dummy_numa_init(void)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100909{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 printk(KERN_INFO "%s\n",
911 numa_off ? "NUMA turned off" : "No NUMA configuration found");
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100912 printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
Tejun Heo86ef4db2011-02-16 12:13:06 +0100913 0LU, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100914
Tejun Heo92d4a432011-02-16 17:11:09 +0100915 node_set(0, numa_nodes_parsed);
Tejun Heo43a662f2011-02-16 17:11:08 +0100916 numa_add_memblk(0, 0, (u64)max_pfn << PAGE_SHIFT);
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100917
918 return 0;
919}
920
Tejun Heoffe77a42011-02-16 12:13:06 +0100921void __init initmem_init(void)
922{
923 int (*numa_init[])(void) = { [2] = dummy_numa_init };
Tejun Heoffe77a42011-02-16 12:13:06 +0100924 int i, j;
925
926 if (!numa_off) {
927#ifdef CONFIG_ACPI_NUMA
928 numa_init[0] = x86_acpi_numa_init;
Tejun Heoffe77a42011-02-16 12:13:06 +0100929#endif
930#ifdef CONFIG_AMD_NUMA
931 numa_init[1] = amd_numa_init;
Tejun Heoffe77a42011-02-16 12:13:06 +0100932#endif
933 }
934
935 for (i = 0; i < ARRAY_SIZE(numa_init); i++) {
936 if (!numa_init[i])
937 continue;
938
939 for (j = 0; j < MAX_LOCAL_APIC; j++)
940 set_apicid_to_node(j, NUMA_NO_NODE);
941
Tejun Heo92d4a432011-02-16 17:11:09 +0100942 nodes_clear(numa_nodes_parsed);
Tejun Heoffe77a42011-02-16 12:13:06 +0100943 nodes_clear(node_possible_map);
944 nodes_clear(node_online_map);
Tejun Heo97e7b782011-02-16 17:11:08 +0100945 memset(&numa_meminfo, 0, sizeof(numa_meminfo));
Tejun Heofd0435d2011-02-16 17:11:08 +0100946 remove_all_active_ranges();
Tejun Heoac7136b2011-02-16 17:11:09 +0100947 numa_reset_distance();
Tejun Heoffe77a42011-02-16 12:13:06 +0100948
949 if (numa_init[i]() < 0)
950 continue;
Tejun Heo206e4202011-02-16 12:13:07 +0100951
Tejun Heo56e827f2011-02-16 17:11:09 +0100952 if (numa_cleanup_meminfo(&numa_meminfo) < 0)
953 continue;
Tejun Heoffe77a42011-02-16 12:13:06 +0100954#ifdef CONFIG_NUMA_EMU
Tejun Heoc88aea72011-02-16 17:11:10 +0100955 /*
956 * If requested, try emulation. If emulation is not used,
957 * build identity emu_nid_to_phys[] for numa_add_cpu()
958 */
959 if (!emu_cmdline || !numa_emulation(i == 0, i == 1))
960 for (j = 0; j < ARRAY_SIZE(emu_nid_to_phys); j++)
961 emu_nid_to_phys[j] = j;
Tejun Heoffe77a42011-02-16 12:13:06 +0100962#endif
Tejun Heof9c60252011-02-16 17:11:09 +0100963 if (numa_register_memblks(&numa_meminfo) < 0)
Tejun Heo43a662f2011-02-16 17:11:08 +0100964 continue;
965
Tejun Heofd0435d2011-02-16 17:11:08 +0100966 for (j = 0; j < nr_cpu_ids; j++) {
967 int nid = early_cpu_to_node(j);
968
969 if (nid == NUMA_NO_NODE)
970 continue;
971 if (!node_online(nid))
972 numa_clear_node(j);
973 }
974 numa_init_array();
975 return;
Tejun Heoffe77a42011-02-16 12:13:06 +0100976 }
977 BUG();
Andi Kleen69d81fc2005-11-05 17:25:53 +0100978}
979
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100980unsigned long __init numa_free_all_bootmem(void)
981{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 unsigned long pages = 0;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100983 int i;
984
985 for_each_online_node(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 pages += free_all_bootmem_node(NODE_DATA(i));
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100987
Yinghai Lu08677212010-02-10 01:20:20 -0800988 pages += free_all_memory_core_early(MAX_NUMNODES);
Yinghai Lu08677212010-02-10 01:20:20 -0800989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return pages;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100991}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100993int __cpuinit numa_cpu_node(int cpu)
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800994{
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100995 int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800996
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100997 if (apicid != BAD_APICID)
998 return __apicid_to_node[apicid];
999 return NUMA_NO_NODE;
Yinghai Lud9c2d5a2009-11-21 00:23:37 -08001000}
1001
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +01001002/*
Tejun Heode2d9442011-01-23 14:37:41 +01001003 * UGLINESS AHEAD: Currently, CONFIG_NUMA_EMU is 64bit only and makes use
1004 * of 64bit specific data structures. The distinction is artificial and
1005 * should be removed. numa_{add|remove}_cpu() are implemented in numa.c
1006 * for both 32 and 64bit when CONFIG_NUMA_EMU is disabled but here when
1007 * enabled.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +01001008 *
Tejun Heode2d9442011-01-23 14:37:41 +01001009 * NUMA emulation is planned to be made generic and the following and other
1010 * related code should be moved to numa.c.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +01001011 */
Tejun Heode2d9442011-01-23 14:37:41 +01001012#ifdef CONFIG_NUMA_EMU
1013# ifndef CONFIG_DEBUG_PER_CPU_MAPS
David Rientjesc1c34432010-12-22 17:23:54 -08001014void __cpuinit numa_add_cpu(int cpu)
1015{
Tejun Heobbc9e2f2011-01-23 14:37:39 +01001016 int physnid, nid;
David Rientjesc1c34432010-12-22 17:23:54 -08001017
Tejun Heobbc9e2f2011-01-23 14:37:39 +01001018 nid = numa_cpu_node(cpu);
David Rientjesc1c34432010-12-22 17:23:54 -08001019 if (nid == NUMA_NO_NODE)
1020 nid = early_cpu_to_node(cpu);
1021 BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
1022
Tejun Heo9d073ca2011-02-16 17:11:10 +01001023 physnid = emu_nid_to_phys[nid];
David Rientjesc1c34432010-12-22 17:23:54 -08001024
1025 /*
1026 * Map the cpu to each emulated node that is allocated on the physical
1027 * node of the cpu's apic id.
1028 */
Tejun Heo9d073ca2011-02-16 17:11:10 +01001029 for_each_online_node(nid)
1030 if (emu_nid_to_phys[nid] == physnid)
David Rientjesc1c34432010-12-22 17:23:54 -08001031 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
David Rientjesc1c34432010-12-22 17:23:54 -08001032}
1033
1034void __cpuinit numa_remove_cpu(int cpu)
1035{
1036 int i;
1037
1038 for_each_online_node(i)
1039 cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
1040}
Tejun Heode2d9442011-01-23 14:37:41 +01001041# else /* !CONFIG_DEBUG_PER_CPU_MAPS */
David Rientjesd906f0e2010-12-30 10:54:16 -08001042static void __cpuinit numa_set_cpumask(int cpu, int enable)
1043{
David Rientjesd906f0e2010-12-30 10:54:16 -08001044 struct cpumask *mask;
Tejun Heo9d073ca2011-02-16 17:11:10 +01001045 int nid, physnid, i;
Brian Gerst6470aff2009-01-27 12:56:47 +09001046
Tejun Heo9d073ca2011-02-16 17:11:10 +01001047 nid = early_cpu_to_node(cpu);
1048 if (nid == NUMA_NO_NODE) {
David Rientjes14392fd2011-02-07 14:08:53 -08001049 /* early_cpu_to_node() already emits a warning and trace */
1050 return;
1051 }
David Rientjesc1c34432010-12-22 17:23:54 -08001052
Tejun Heo9d073ca2011-02-16 17:11:10 +01001053 physnid = emu_nid_to_phys[nid];
1054
1055 for_each_online_node(i) {
1056 if (emu_nid_to_phys[nid] != physnid)
David Rientjesc1c34432010-12-22 17:23:54 -08001057 continue;
Tejun Heo9d073ca2011-02-16 17:11:10 +01001058
David Rientjesd906f0e2010-12-30 10:54:16 -08001059 mask = debug_cpumask_set_cpu(cpu, enable);
1060 if (!mask)
David Rientjesc1c34432010-12-22 17:23:54 -08001061 return;
David Rientjesc1c34432010-12-22 17:23:54 -08001062
1063 if (enable)
1064 cpumask_set_cpu(cpu, mask);
1065 else
1066 cpumask_clear_cpu(cpu, mask);
Brian Gerst6470aff2009-01-27 12:56:47 +09001067 }
Brian Gerst6470aff2009-01-27 12:56:47 +09001068}
1069
1070void __cpuinit numa_add_cpu(int cpu)
1071{
1072 numa_set_cpumask(cpu, 1);
1073}
1074
1075void __cpuinit numa_remove_cpu(int cpu)
1076{
1077 numa_set_cpumask(cpu, 0);
1078}
Tejun Heode2d9442011-01-23 14:37:41 +01001079# endif /* !CONFIG_DEBUG_PER_CPU_MAPS */
1080#endif /* CONFIG_NUMA_EMU */