blob: 6e4ee96d1b11995c2cb069fa1cfa459330850868 [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 Heoac7136b62011-02-16 17:11:09 +010048static int numa_distance_cnt;
49static u8 *numa_distance;
50
Brian Gerst6470aff2009-01-27 12:56:47 +090051/*
Eric Dumazet529a3402005-11-05 17:25:54 +010052 * Given a shift value, try to populate memnodemap[]
53 * Returns :
54 * 1 if OK
55 * 0 if memnodmap[] too small (of shift too small)
56 * -1 if node overlap or lost ram (shift too big)
57 */
Tejun Heo97e7b782011-02-16 17:11:08 +010058static int __init populate_memnodemap(const struct numa_meminfo *mi, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Eric Dumazet529a3402005-11-05 17:25:54 +010060 unsigned long addr, end;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010061 int i, res = -1;
Keith Manntheyb6846642005-07-28 21:15:38 -070062
travis@sgi.com43238382008-01-30 13:33:25 +010063 memset(memnodemap, 0xff, sizeof(s16)*memnodemapsize);
Tejun Heo97e7b782011-02-16 17:11:08 +010064 for (i = 0; i < mi->nr_blks; i++) {
65 addr = mi->blk[i].start;
66 end = mi->blk[i].end;
Eric Dumazet529a3402005-11-05 17:25:54 +010067 if (addr >= end)
68 continue;
Amul Shah076422d22007-02-13 13:26:19 +010069 if ((end >> shift) >= memnodemapsize)
Eric Dumazet529a3402005-11-05 17:25:54 +010070 return 0;
71 do {
travis@sgi.com43238382008-01-30 13:33:25 +010072 if (memnodemap[addr >> shift] != NUMA_NO_NODE)
Eric Dumazet529a3402005-11-05 17:25:54 +010073 return -1;
Tejun Heo97e7b782011-02-16 17:11:08 +010074 memnodemap[addr >> shift] = mi->blk[i].nid;
Amul Shah076422d22007-02-13 13:26:19 +010075 addr += (1UL << shift);
Eric Dumazet529a3402005-11-05 17:25:54 +010076 } while (addr < end);
77 res = 1;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010078 }
Eric Dumazet529a3402005-11-05 17:25:54 +010079 return res;
80}
81
Amul Shah076422d22007-02-13 13:26:19 +010082static int __init allocate_cachealigned_memnodemap(void)
83{
Yinghai Lu24a5da72008-02-01 17:49:41 +010084 unsigned long addr;
Amul Shah076422d22007-02-13 13:26:19 +010085
86 memnodemap = memnode.embedded_map;
travis@sgi.com316390b2008-01-30 13:33:15 +010087 if (memnodemapsize <= ARRAY_SIZE(memnode.embedded_map))
Amul Shah076422d22007-02-13 13:26:19 +010088 return 0;
Amul Shah076422d22007-02-13 13:26:19 +010089
Yinghai Lu24a5da72008-02-01 17:49:41 +010090 addr = 0x8000;
Joerg Roedelbe3e89e2008-07-25 16:48:58 +020091 nodemap_size = roundup(sizeof(s16) * memnodemapsize, L1_CACHE_BYTES);
Yinghai Ludbef7b52010-12-27 16:48:08 -080092 nodemap_addr = memblock_find_in_range(addr, get_max_mapped(),
Yinghai Lu24a5da72008-02-01 17:49:41 +010093 nodemap_size, L1_CACHE_BYTES);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070094 if (nodemap_addr == MEMBLOCK_ERROR) {
Amul Shah076422d22007-02-13 13:26:19 +010095 printk(KERN_ERR
96 "NUMA: Unable to allocate Memory to Node hash map\n");
97 nodemap_addr = nodemap_size = 0;
98 return -1;
99 }
Yinghai Lu24a5da72008-02-01 17:49:41 +0100100 memnodemap = phys_to_virt(nodemap_addr);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700101 memblock_x86_reserve_range(nodemap_addr, nodemap_addr + nodemap_size, "MEMNODEMAP");
Amul Shah076422d22007-02-13 13:26:19 +0100102
103 printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n",
104 nodemap_addr, nodemap_addr + nodemap_size);
105 return 0;
106}
107
108/*
109 * The LSB of all start and end addresses in the node map is the value of the
110 * maximum possible shift.
111 */
Tejun Heo97e7b782011-02-16 17:11:08 +0100112static int __init extract_lsb_from_nodes(const struct numa_meminfo *mi)
Amul Shah076422d22007-02-13 13:26:19 +0100113{
Amul Shah54413922007-02-13 13:26:20 +0100114 int i, nodes_used = 0;
Amul Shah076422d22007-02-13 13:26:19 +0100115 unsigned long start, end;
116 unsigned long bitfield = 0, memtop = 0;
117
Tejun Heo97e7b782011-02-16 17:11:08 +0100118 for (i = 0; i < mi->nr_blks; i++) {
119 start = mi->blk[i].start;
120 end = mi->blk[i].end;
Amul Shah076422d22007-02-13 13:26:19 +0100121 if (start >= end)
122 continue;
Amul Shah54413922007-02-13 13:26:20 +0100123 bitfield |= start;
124 nodes_used++;
Amul Shah076422d22007-02-13 13:26:19 +0100125 if (end > memtop)
126 memtop = end;
127 }
Amul Shah54413922007-02-13 13:26:20 +0100128 if (nodes_used <= 1)
129 i = 63;
130 else
131 i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
Amul Shah076422d22007-02-13 13:26:19 +0100132 memnodemapsize = (memtop >> i)+1;
133 return i;
134}
135
Tejun Heo97e7b782011-02-16 17:11:08 +0100136static int __init compute_hash_shift(const struct numa_meminfo *mi)
Eric Dumazet529a3402005-11-05 17:25:54 +0100137{
Amul Shah076422d22007-02-13 13:26:19 +0100138 int shift;
Eric Dumazet529a3402005-11-05 17:25:54 +0100139
Tejun Heo97e7b782011-02-16 17:11:08 +0100140 shift = extract_lsb_from_nodes(mi);
Amul Shah076422d22007-02-13 13:26:19 +0100141 if (allocate_cachealigned_memnodemap())
142 return -1;
Andi Kleen6b050f82006-01-11 22:44:33 +0100143 printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
Eric Dumazet529a3402005-11-05 17:25:54 +0100144 shift);
145
Tejun Heo97e7b782011-02-16 17:11:08 +0100146 if (populate_memnodemap(mi, shift) != 1) {
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100147 printk(KERN_INFO "Your memory is not aligned you need to "
148 "rebuild your kernel with a bigger NODEMAPSIZE "
149 "shift=%d\n", shift);
Eric Dumazet529a3402005-11-05 17:25:54 +0100150 return -1;
151 }
Keith Manntheyb6846642005-07-28 21:15:38 -0700152 return shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -0800155int __meminit __early_pfn_to_nid(unsigned long pfn)
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700156{
157 return phys_to_nid(pfn << PAGE_SHIFT);
158}
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700159
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100160static void * __init early_node_mem(int nodeid, unsigned long start,
Yinghai Lu24a5da72008-02-01 17:49:41 +0100161 unsigned long end, unsigned long size,
162 unsigned long align)
Andi Kleena8062232006-04-07 19:49:21 +0200163{
Yinghai Lucef625e2010-02-10 01:20:18 -0800164 unsigned long mem;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100165
Yinghai Lucef625e2010-02-10 01:20:18 -0800166 /*
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;
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700175 mem = memblock_x86_find_in_range_node(nodeid, start, end, size, align);
176 if (mem != MEMBLOCK_ERROR)
Andi Kleena8062232006-04-07 19:49:21 +0200177 return __va(mem);
Yinghai Lu9347e0b2008-02-01 17:49:42 +0100178
Yinghai Lucef625e2010-02-10 01:20:18 -0800179 /* extend the search scope */
180 end = max_pfn_mapped << PAGE_SHIFT;
Yinghai Lu419db272010-10-28 09:50:17 -0700181 start = MAX_DMA_PFN << PAGE_SHIFT;
182 mem = memblock_find_in_range(start, end, size, align);
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700183 if (mem != MEMBLOCK_ERROR)
Yinghai Lu1842f902010-02-10 01:20:15 -0800184 return __va(mem);
185
186 printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100187 size, nodeid);
Yinghai Lu1842f902010-02-10 01:20:15 -0800188
189 return NULL;
Andi Kleena8062232006-04-07 19:49:21 +0200190}
191
Tejun Heod9c515e2011-02-16 17:11:10 +0100192static int __init numa_add_memblk_to(int nid, u64 start, u64 end,
193 struct numa_meminfo *mi)
Tejun Heoef396ec2011-02-16 17:11:07 +0100194{
Tejun Heo56e827f2011-02-16 17:11:09 +0100195 /* ignore zero length blks */
196 if (start == end)
197 return 0;
198
199 /* whine about and ignore invalid blks */
200 if (start > end || nid < 0 || nid >= MAX_NUMNODES) {
201 pr_warning("NUMA: Warning: invalid memblk node %d (%Lx-%Lx)\n",
202 nid, start, end);
203 return 0;
204 }
205
206 if (mi->nr_blks >= NR_NODE_MEMBLKS) {
207 pr_err("NUMA: too many memblk ranges\n");
Tejun Heoef396ec2011-02-16 17:11:07 +0100208 return -EINVAL;
209 }
210
Tejun Heo97e7b782011-02-16 17:11:08 +0100211 mi->blk[mi->nr_blks].start = start;
212 mi->blk[mi->nr_blks].end = end;
213 mi->blk[mi->nr_blks].nid = nid;
214 mi->nr_blks++;
Tejun Heoef396ec2011-02-16 17:11:07 +0100215 return 0;
216}
217
Tejun Heo2e756be2011-02-16 17:11:09 +0100218static void __init numa_remove_memblk_from(int idx, struct numa_meminfo *mi)
219{
220 mi->nr_blks--;
221 memmove(&mi->blk[idx], &mi->blk[idx + 1],
222 (mi->nr_blks - idx) * sizeof(mi->blk[0]));
223}
224
Tejun Heod9c515e2011-02-16 17:11:10 +0100225int __init numa_add_memblk(int nid, u64 start, u64 end)
226{
227 return numa_add_memblk_to(nid, start, end, &numa_meminfo);
228}
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230/* Initialize bootmem allocator for a node */
Yinghai Lu7c437692009-05-15 13:59:37 -0700231void __init
232setup_node_bootmem(int nodeid, unsigned long start, unsigned long end)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100233{
Yinghai Lu08677212010-02-10 01:20:20 -0800234 unsigned long start_pfn, last_pfn, nodedata_phys;
Yinghai Lu7c437692009-05-15 13:59:37 -0700235 const int pgdat_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
Yinghai Lu1a27fc02008-03-18 12:52:37 -0700236 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Yinghai Lu4c31e922009-04-22 14:19:27 -0700238 if (!end)
239 return;
240
Yinghai Lu7c437692009-05-15 13:59:37 -0700241 /*
242 * Don't confuse VM with a node that doesn't have the
243 * minimum amount of memory:
244 */
245 if (end && (end - start) < NODE_MIN_SIZE)
246 return;
247
Joerg Roedelbe3e89e2008-07-25 16:48:58 +0200248 start = roundup(start, ZONE_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Yinghai Lu08677212010-02-10 01:20:20 -0800250 printk(KERN_INFO "Initmem setup node %d %016lx-%016lx\n", nodeid,
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100251 start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 start_pfn = start >> PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200254 last_pfn = end >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Yinghai Lu24a5da72008-02-01 17:49:41 +0100256 node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
257 SMP_CACHE_BYTES);
Andi Kleena8062232006-04-07 19:49:21 +0200258 if (node_data[nodeid] == NULL)
259 return;
260 nodedata_phys = __pa(node_data[nodeid]);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700261 memblock_x86_reserve_range(nodedata_phys, nodedata_phys + pgdat_size, "NODE_DATA");
Yinghai Lu6118f762008-02-04 16:47:56 +0100262 printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
263 nodedata_phys + pgdat_size - 1);
Yinghai Lu1842f902010-02-10 01:20:15 -0800264 nid = phys_to_nid(nodedata_phys);
265 if (nid != nodeid)
266 printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nodeid, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
Yinghai Lu08677212010-02-10 01:20:20 -0800269 NODE_DATA(nodeid)->node_id = nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 NODE_DATA(nodeid)->node_start_pfn = start_pfn;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200271 NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 node_set_online(nodeid);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100274}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Tejun Heof9c60252011-02-16 17:11:09 +0100276static int __init numa_cleanup_meminfo(struct numa_meminfo *mi)
Tejun Heofd0435d2011-02-16 17:11:08 +0100277{
Tejun Heo56e827f2011-02-16 17:11:09 +0100278 const u64 low = 0;
279 const u64 high = (u64)max_pfn << PAGE_SHIFT;
Tejun Heo2e756be2011-02-16 17:11:09 +0100280 int i, j, k;
Tejun Heoef396ec2011-02-16 17:11:07 +0100281
Tejun Heo2e756be2011-02-16 17:11:09 +0100282 for (i = 0; i < mi->nr_blks; i++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100283 struct numa_memblk *bi = &mi->blk[i];
Tejun Heoef396ec2011-02-16 17:11:07 +0100284
Tejun Heo56e827f2011-02-16 17:11:09 +0100285 /* make sure all blocks are inside the limits */
286 bi->start = max(bi->start, low);
287 bi->end = min(bi->end, high);
288
289 /* and there's no empty block */
290 if (bi->start == bi->end) {
291 numa_remove_memblk_from(i--, mi);
292 continue;
293 }
294
Tejun Heo2e756be2011-02-16 17:11:09 +0100295 for (j = i + 1; j < mi->nr_blks; j++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100296 struct numa_memblk *bj = &mi->blk[j];
Tejun Heoef396ec2011-02-16 17:11:07 +0100297 unsigned long start, end;
298
Tejun Heo2e756be2011-02-16 17:11:09 +0100299 /*
Tejun Heo56e827f2011-02-16 17:11:09 +0100300 * See whether there are overlapping blocks. Whine
301 * about but allow overlaps of the same nid. They
302 * will be merged below.
303 */
304 if (bi->end > bj->start && bi->start < bj->end) {
305 if (bi->nid != bj->nid) {
306 pr_err("NUMA: node %d (%Lx-%Lx) overlaps with node %d (%Lx-%Lx)\n",
307 bi->nid, bi->start, bi->end,
308 bj->nid, bj->start, bj->end);
309 return -EINVAL;
310 }
311 pr_warning("NUMA: Warning: node %d (%Lx-%Lx) overlaps with itself (%Lx-%Lx)\n",
312 bi->nid, bi->start, bi->end,
313 bj->start, bj->end);
314 }
315
316 /*
Tejun Heo2e756be2011-02-16 17:11:09 +0100317 * Join together blocks on the same node, holes
318 * between which don't overlap with memory on other
319 * nodes.
320 */
Tejun Heo97e7b782011-02-16 17:11:08 +0100321 if (bi->nid != bj->nid)
Tejun Heoef396ec2011-02-16 17:11:07 +0100322 continue;
Tejun Heo56e827f2011-02-16 17:11:09 +0100323 start = max(min(bi->start, bj->start), low);
324 end = min(max(bi->end, bj->end), high);
Tejun Heo2e756be2011-02-16 17:11:09 +0100325 for (k = 0; k < mi->nr_blks; k++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100326 struct numa_memblk *bk = &mi->blk[k];
327
328 if (bi->nid == bk->nid)
Tejun Heoef396ec2011-02-16 17:11:07 +0100329 continue;
Tejun Heo97e7b782011-02-16 17:11:08 +0100330 if (start < bk->end && end > bk->start)
Tejun Heoef396ec2011-02-16 17:11:07 +0100331 break;
332 }
Tejun Heo97e7b782011-02-16 17:11:08 +0100333 if (k < mi->nr_blks)
Tejun Heoef396ec2011-02-16 17:11:07 +0100334 continue;
Tejun Heoef396ec2011-02-16 17:11:07 +0100335 printk(KERN_INFO "NUMA: Node %d [%Lx,%Lx) + [%Lx,%Lx) -> [%lx,%lx)\n",
Tejun Heo97e7b782011-02-16 17:11:08 +0100336 bi->nid, bi->start, bi->end, bj->start, bj->end,
Tejun Heoef396ec2011-02-16 17:11:07 +0100337 start, end);
Tejun Heo97e7b782011-02-16 17:11:08 +0100338 bi->start = start;
339 bi->end = end;
Tejun Heo2e756be2011-02-16 17:11:09 +0100340 numa_remove_memblk_from(j--, mi);
Tejun Heoef396ec2011-02-16 17:11:07 +0100341 }
342 }
343
Tejun Heo56e827f2011-02-16 17:11:09 +0100344 for (i = mi->nr_blks; i < ARRAY_SIZE(mi->blk); i++) {
345 mi->blk[i].start = mi->blk[i].end = 0;
346 mi->blk[i].nid = NUMA_NO_NODE;
347 }
348
Tejun Heof9c60252011-02-16 17:11:09 +0100349 return 0;
350}
351
352/*
Tejun Heo4697bdc2011-02-16 17:11:09 +0100353 * Set nodes, which have memory in @mi, in *@nodemask.
354 */
355static void __init numa_nodemask_from_meminfo(nodemask_t *nodemask,
356 const struct numa_meminfo *mi)
357{
358 int i;
359
360 for (i = 0; i < ARRAY_SIZE(mi->blk); i++)
361 if (mi->blk[i].start != mi->blk[i].end &&
362 mi->blk[i].nid != NUMA_NO_NODE)
363 node_set(mi->blk[i].nid, *nodemask);
364}
365
366/*
Tejun Heoac7136b62011-02-16 17:11:09 +0100367 * Reset distance table. The current table is freed. The next
368 * numa_set_distance() call will create a new one.
369 */
370static void __init numa_reset_distance(void)
371{
372 size_t size;
373
Yinghai Lu2ca230b2011-02-17 14:46:37 +0100374 if (numa_distance_cnt) {
375 size = numa_distance_cnt * sizeof(numa_distance[0]);
376 memblock_x86_free_range(__pa(numa_distance),
377 __pa(numa_distance) + size);
378 numa_distance_cnt = 0;
379 }
Tejun Heoac7136b62011-02-16 17:11:09 +0100380 numa_distance = NULL;
Tejun Heoac7136b62011-02-16 17:11:09 +0100381}
382
383/*
384 * Set the distance between node @from to @to to @distance. If distance
385 * table doesn't exist, one which is large enough to accomodate all the
386 * currently known nodes will be created.
387 */
388void __init numa_set_distance(int from, int to, int distance)
389{
390 if (!numa_distance) {
391 nodemask_t nodes_parsed;
392 size_t size;
393 int i, j, cnt = 0;
394 u64 phys;
395
396 /* size the new table and allocate it */
397 nodes_parsed = numa_nodes_parsed;
398 numa_nodemask_from_meminfo(&nodes_parsed, &numa_meminfo);
399
400 for_each_node_mask(i, nodes_parsed)
401 cnt = i;
402 size = ++cnt * sizeof(numa_distance[0]);
403
404 phys = memblock_find_in_range(0,
405 (u64)max_pfn_mapped << PAGE_SHIFT,
406 size, PAGE_SIZE);
407 if (phys == MEMBLOCK_ERROR) {
408 pr_warning("NUMA: Warning: can't allocate distance table!\n");
409 /* don't retry until explicitly reset */
410 numa_distance = (void *)1LU;
411 return;
412 }
413 memblock_x86_reserve_range(phys, phys + size, "NUMA DIST");
414
415 numa_distance = __va(phys);
416 numa_distance_cnt = cnt;
417
418 /* fill with the default distances */
419 for (i = 0; i < cnt; i++)
420 for (j = 0; j < cnt; j++)
421 numa_distance[i * cnt + j] = i == j ?
422 LOCAL_DISTANCE : REMOTE_DISTANCE;
423 printk(KERN_DEBUG "NUMA: Initialized distance table, cnt=%d\n", cnt);
424 }
425
426 if (from >= numa_distance_cnt || to >= numa_distance_cnt) {
427 printk_once(KERN_DEBUG "NUMA: Debug: distance out of bound, from=%d to=%d distance=%d\n",
428 from, to, distance);
429 return;
430 }
431
432 if ((u8)distance != distance ||
433 (from == to && distance != LOCAL_DISTANCE)) {
434 pr_warn_once("NUMA: Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
435 from, to, distance);
436 return;
437 }
438
439 numa_distance[from * numa_distance_cnt + to] = distance;
440}
441
442int __node_distance(int from, int to)
443{
Tejun Heoac7136b62011-02-16 17:11:09 +0100444 if (from >= numa_distance_cnt || to >= numa_distance_cnt)
445 return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
446 return numa_distance[from * numa_distance_cnt + to];
447}
448EXPORT_SYMBOL(__node_distance);
449
450/*
Tejun Heof9c60252011-02-16 17:11:09 +0100451 * Sanity check to catch more bad NUMA configurations (they are amazingly
452 * common). Make sure the nodes cover all memory.
453 */
Tejun Heo91556232011-02-16 17:11:09 +0100454static bool __init numa_meminfo_cover_memory(const struct numa_meminfo *mi)
Tejun Heof9c60252011-02-16 17:11:09 +0100455{
456 unsigned long numaram, e820ram;
457 int i;
458
459 numaram = 0;
Tejun Heo91556232011-02-16 17:11:09 +0100460 for (i = 0; i < mi->nr_blks; i++) {
461 unsigned long s = mi->blk[i].start >> PAGE_SHIFT;
462 unsigned long e = mi->blk[i].end >> PAGE_SHIFT;
Tejun Heof9c60252011-02-16 17:11:09 +0100463 numaram += e - s;
Tejun Heo91556232011-02-16 17:11:09 +0100464 numaram -= __absent_pages_in_range(mi->blk[i].nid, s, e);
Tejun Heof9c60252011-02-16 17:11:09 +0100465 if ((long)numaram < 0)
466 numaram = 0;
467 }
468
469 e820ram = max_pfn - (memblock_x86_hole_size(0,
470 max_pfn << PAGE_SHIFT) >> PAGE_SHIFT);
471 /* We seem to lose 3 pages somewhere. Allow 1M of slack. */
472 if ((long)(e820ram - numaram) >= (1 << (20 - PAGE_SHIFT))) {
473 printk(KERN_ERR "NUMA: nodes only cover %luMB of your %luMB e820 RAM. Not used.\n",
474 (numaram << PAGE_SHIFT) >> 20,
475 (e820ram << PAGE_SHIFT) >> 20);
Tejun Heo91556232011-02-16 17:11:09 +0100476 return false;
Tejun Heof9c60252011-02-16 17:11:09 +0100477 }
Tejun Heo91556232011-02-16 17:11:09 +0100478 return true;
Tejun Heof9c60252011-02-16 17:11:09 +0100479}
480
481static int __init numa_register_memblks(struct numa_meminfo *mi)
482{
Yinghai Lu69efcc62011-02-21 10:58:13 +0100483 int i, nid;
Tejun Heof9c60252011-02-16 17:11:09 +0100484
485 /* Account for nodes with cpus and no memory */
Tejun Heo4697bdc2011-02-16 17:11:09 +0100486 node_possible_map = numa_nodes_parsed;
487 numa_nodemask_from_meminfo(&node_possible_map, mi);
Tejun Heof9c60252011-02-16 17:11:09 +0100488 if (WARN_ON(nodes_empty(node_possible_map)))
489 return -EINVAL;
490
Tejun Heo97e7b782011-02-16 17:11:08 +0100491 memnode_shift = compute_hash_shift(mi);
Tejun Heoef396ec2011-02-16 17:11:07 +0100492 if (memnode_shift < 0) {
493 printk(KERN_ERR "NUMA: No NUMA node hash function found. Contact maintainer\n");
494 return -EINVAL;
495 }
496
Tejun Heo97e7b782011-02-16 17:11:08 +0100497 for (i = 0; i < mi->nr_blks; i++)
498 memblock_x86_register_active_regions(mi->blk[i].nid,
499 mi->blk[i].start >> PAGE_SHIFT,
500 mi->blk[i].end >> PAGE_SHIFT);
Tejun Heofd0435d2011-02-16 17:11:08 +0100501
502 /* for out of order entries */
503 sort_node_map();
Tejun Heo91556232011-02-16 17:11:09 +0100504 if (!numa_meminfo_cover_memory(mi))
Tejun Heofd0435d2011-02-16 17:11:08 +0100505 return -EINVAL;
506
507 init_memory_mapping_high();
508
Yinghai Lu69efcc62011-02-21 10:58:13 +0100509 /* Finally register nodes. */
510 for_each_node_mask(nid, node_possible_map) {
511 u64 start = (u64)max_pfn << PAGE_SHIFT;
512 u64 end = 0;
Tejun Heo91556232011-02-16 17:11:09 +0100513
Yinghai Lu69efcc62011-02-21 10:58:13 +0100514 for (i = 0; i < mi->nr_blks; i++) {
515 if (nid != mi->blk[i].nid)
Tejun Heo91556232011-02-16 17:11:09 +0100516 continue;
Yinghai Lu69efcc62011-02-21 10:58:13 +0100517 start = min(mi->blk[i].start, start);
518 end = max(mi->blk[i].end, end);
Tejun Heo91556232011-02-16 17:11:09 +0100519 }
Yinghai Lu69efcc62011-02-21 10:58:13 +0100520
521 if (start < end)
522 setup_node_bootmem(nid, start, end);
Tejun Heo91556232011-02-16 17:11:09 +0100523 }
Tejun Heofd0435d2011-02-16 17:11:08 +0100524
Tejun Heoef396ec2011-02-16 17:11:07 +0100525 return 0;
526}
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528#ifdef CONFIG_NUMA_EMU
Rohit Seth53fee042007-02-13 13:26:22 +0100529/* Numa emulation */
Tejun Heo9d073ca2011-02-16 17:11:10 +0100530static int emu_nid_to_phys[MAX_NUMNODES] __cpuinitdata;
Tejun Heod9c515e2011-02-16 17:11:10 +0100531static char *emu_cmdline __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Jan Beulich90321602011-01-19 08:57:21 +0000533void __init numa_emu_cmdline(char *str)
534{
Tejun Heod9c515e2011-02-16 17:11:10 +0100535 emu_cmdline = str;
Jan Beulich90321602011-01-19 08:57:21 +0000536}
537
Tejun Heo1cca5342011-02-16 17:11:10 +0100538static int __init emu_find_memblk_by_nid(int nid, const struct numa_meminfo *mi)
539{
540 int i;
541
542 for (i = 0; i < mi->nr_blks; i++)
543 if (mi->blk[i].nid == nid)
544 return i;
545 return -ENOENT;
546}
547
Rohit Seth53fee042007-02-13 13:26:22 +0100548/*
Tejun Heoc88aea72011-02-16 17:11:10 +0100549 * Sets up nid to range from @start to @end. The return value is -errno if
550 * something went wrong, 0 otherwise.
Rohit Seth53fee042007-02-13 13:26:22 +0100551 */
Tejun Heoc88aea72011-02-16 17:11:10 +0100552static int __init emu_setup_memblk(struct numa_meminfo *ei,
Tejun Heo1cca5342011-02-16 17:11:10 +0100553 struct numa_meminfo *pi,
554 int nid, int phys_blk, u64 size)
Rohit Seth53fee042007-02-13 13:26:22 +0100555{
Tejun Heoc88aea72011-02-16 17:11:10 +0100556 struct numa_memblk *eb = &ei->blk[ei->nr_blks];
Tejun Heo1cca5342011-02-16 17:11:10 +0100557 struct numa_memblk *pb = &pi->blk[phys_blk];
Tejun Heoc88aea72011-02-16 17:11:10 +0100558
559 if (ei->nr_blks >= NR_NODE_MEMBLKS) {
560 pr_err("NUMA: Too many emulated memblks, failing emulation\n");
561 return -EINVAL;
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200562 }
Tejun Heoc88aea72011-02-16 17:11:10 +0100563
564 ei->nr_blks++;
Tejun Heo1cca5342011-02-16 17:11:10 +0100565 eb->start = pb->start;
566 eb->end = pb->start + size;
Tejun Heoc88aea72011-02-16 17:11:10 +0100567 eb->nid = nid;
Tejun Heo9d073ca2011-02-16 17:11:10 +0100568
569 if (emu_nid_to_phys[nid] == NUMA_NO_NODE)
Tejun Heo1cca5342011-02-16 17:11:10 +0100570 emu_nid_to_phys[nid] = pb->nid;
571
572 pb->start += size;
573 if (pb->start >= pb->end) {
574 WARN_ON_ONCE(pb->start > pb->end);
575 numa_remove_memblk_from(phys_blk, pi);
576 }
Tejun Heo9d073ca2011-02-16 17:11:10 +0100577
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200578 printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
Tejun Heoc88aea72011-02-16 17:11:10 +0100579 eb->start, eb->end, (eb->end - eb->start) >> 20);
580 return 0;
Rohit Seth53fee042007-02-13 13:26:22 +0100581}
582
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200583/*
David Rientjesadc19382009-09-25 15:20:09 -0700584 * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
585 * to max_addr. The return value is the number of nodes allocated.
586 */
Tejun Heoc88aea72011-02-16 17:11:10 +0100587static int __init split_nodes_interleave(struct numa_meminfo *ei,
Tejun Heo1cca5342011-02-16 17:11:10 +0100588 struct numa_meminfo *pi,
Tejun Heoc88aea72011-02-16 17:11:10 +0100589 u64 addr, u64 max_addr, int nr_nodes)
David Rientjesadc19382009-09-25 15:20:09 -0700590{
591 nodemask_t physnode_mask = NODE_MASK_NONE;
592 u64 size;
593 int big;
Tejun Heoc88aea72011-02-16 17:11:10 +0100594 int nid = 0;
595 int i, ret;
David Rientjesadc19382009-09-25 15:20:09 -0700596
597 if (nr_nodes <= 0)
598 return -1;
599 if (nr_nodes > MAX_NUMNODES) {
600 pr_info("numa=fake=%d too large, reducing to %d\n",
601 nr_nodes, MAX_NUMNODES);
602 nr_nodes = MAX_NUMNODES;
603 }
604
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700605 size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) / nr_nodes;
David Rientjesadc19382009-09-25 15:20:09 -0700606 /*
607 * Calculate the number of big nodes that can be allocated as a result
608 * of consolidating the remainder.
609 */
David Rientjes68fd1112010-02-15 13:43:25 -0800610 big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
David Rientjesadc19382009-09-25 15:20:09 -0700611 FAKE_NODE_MIN_SIZE;
612
613 size &= FAKE_NODE_MIN_HASH_MASK;
614 if (!size) {
615 pr_err("Not enough memory for each node. "
616 "NUMA emulation disabled.\n");
617 return -1;
618 }
619
Tejun Heo1cca5342011-02-16 17:11:10 +0100620 for (i = 0; i < pi->nr_blks; i++)
621 node_set(pi->blk[i].nid, physnode_mask);
David Rientjesadc19382009-09-25 15:20:09 -0700622
623 /*
624 * Continue to fill physical nodes with fake nodes until there is no
625 * memory left on any of them.
626 */
627 while (nodes_weight(physnode_mask)) {
628 for_each_node_mask(i, physnode_mask) {
David Rientjesadc19382009-09-25 15:20:09 -0700629 u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
Tejun Heo1cca5342011-02-16 17:11:10 +0100630 u64 start, limit, end;
631 int phys_blk;
632
633 phys_blk = emu_find_memblk_by_nid(i, pi);
634 if (phys_blk < 0) {
635 node_clear(i, physnode_mask);
636 continue;
637 }
638 start = pi->blk[phys_blk].start;
639 limit = pi->blk[phys_blk].end;
640 end = start + size;
David Rientjesadc19382009-09-25 15:20:09 -0700641
Tejun Heoc88aea72011-02-16 17:11:10 +0100642 if (nid < big)
David Rientjesadc19382009-09-25 15:20:09 -0700643 end += FAKE_NODE_MIN_SIZE;
644
645 /*
646 * Continue to add memory to this fake node if its
647 * non-reserved memory is less than the per-node size.
648 */
Tejun Heo1cca5342011-02-16 17:11:10 +0100649 while (end - start -
650 memblock_x86_hole_size(start, end) < size) {
David Rientjesadc19382009-09-25 15:20:09 -0700651 end += FAKE_NODE_MIN_SIZE;
Tejun Heo1cca5342011-02-16 17:11:10 +0100652 if (end > limit) {
653 end = limit;
David Rientjesadc19382009-09-25 15:20:09 -0700654 break;
655 }
656 }
657
658 /*
659 * If there won't be at least FAKE_NODE_MIN_SIZE of
660 * non-reserved memory in ZONE_DMA32 for the next node,
661 * this one must extend to the boundary.
662 */
663 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700664 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjesadc19382009-09-25 15:20:09 -0700665 end = dma32_end;
666
667 /*
668 * If there won't be enough non-reserved memory for the
669 * next node, this one must extend to the end of the
670 * physical node.
671 */
Tejun Heo1cca5342011-02-16 17:11:10 +0100672 if (limit - end -
673 memblock_x86_hole_size(end, limit) < size)
674 end = limit;
David Rientjesadc19382009-09-25 15:20:09 -0700675
Tejun Heo1cca5342011-02-16 17:11:10 +0100676 ret = emu_setup_memblk(ei, pi, nid++ % nr_nodes,
677 phys_blk,
678 min(end, limit) - start);
Tejun Heoc88aea72011-02-16 17:11:10 +0100679 if (ret < 0)
680 return ret;
David Rientjesadc19382009-09-25 15:20:09 -0700681 }
682 }
Tejun Heoc88aea72011-02-16 17:11:10 +0100683 return 0;
David Rientjesadc19382009-09-25 15:20:09 -0700684}
685
686/*
David Rientjes8df5bb342010-02-15 13:43:30 -0800687 * Returns the end address of a node so that there is at least `size' amount of
688 * non-reserved memory or `max_addr' is reached.
689 */
690static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
691{
692 u64 end = start + size;
693
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700694 while (end - start - memblock_x86_hole_size(start, end) < size) {
David Rientjes8df5bb342010-02-15 13:43:30 -0800695 end += FAKE_NODE_MIN_SIZE;
696 if (end > max_addr) {
697 end = max_addr;
698 break;
699 }
700 }
701 return end;
702}
703
704/*
705 * Sets up fake nodes of `size' interleaved over physical nodes ranging from
706 * `addr' to `max_addr'. The return value is the number of nodes allocated.
707 */
Tejun Heoc88aea72011-02-16 17:11:10 +0100708static int __init split_nodes_size_interleave(struct numa_meminfo *ei,
Tejun Heo1cca5342011-02-16 17:11:10 +0100709 struct numa_meminfo *pi,
Tejun Heoc88aea72011-02-16 17:11:10 +0100710 u64 addr, u64 max_addr, u64 size)
David Rientjes8df5bb342010-02-15 13:43:30 -0800711{
712 nodemask_t physnode_mask = NODE_MASK_NONE;
713 u64 min_size;
Tejun Heoc88aea72011-02-16 17:11:10 +0100714 int nid = 0;
715 int i, ret;
David Rientjes8df5bb342010-02-15 13:43:30 -0800716
717 if (!size)
718 return -1;
719 /*
720 * The limit on emulated nodes is MAX_NUMNODES, so the size per node is
721 * increased accordingly if the requested size is too small. This
722 * creates a uniform distribution of node sizes across the entire
723 * machine (but not necessarily over physical nodes).
724 */
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700725 min_size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) /
David Rientjes8df5bb342010-02-15 13:43:30 -0800726 MAX_NUMNODES;
727 min_size = max(min_size, FAKE_NODE_MIN_SIZE);
728 if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size)
729 min_size = (min_size + FAKE_NODE_MIN_SIZE) &
730 FAKE_NODE_MIN_HASH_MASK;
731 if (size < min_size) {
732 pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
733 size >> 20, min_size >> 20);
734 size = min_size;
735 }
736 size &= FAKE_NODE_MIN_HASH_MASK;
737
Tejun Heo1cca5342011-02-16 17:11:10 +0100738 for (i = 0; i < pi->nr_blks; i++)
739 node_set(pi->blk[i].nid, physnode_mask);
740
David Rientjes8df5bb342010-02-15 13:43:30 -0800741 /*
742 * Fill physical nodes with fake nodes of size until there is no memory
743 * left on any of them.
744 */
745 while (nodes_weight(physnode_mask)) {
746 for_each_node_mask(i, physnode_mask) {
747 u64 dma32_end = MAX_DMA32_PFN << PAGE_SHIFT;
Tejun Heo1cca5342011-02-16 17:11:10 +0100748 u64 start, limit, end;
749 int phys_blk;
David Rientjes8df5bb342010-02-15 13:43:30 -0800750
Tejun Heo1cca5342011-02-16 17:11:10 +0100751 phys_blk = emu_find_memblk_by_nid(i, pi);
752 if (phys_blk < 0) {
753 node_clear(i, physnode_mask);
754 continue;
755 }
756 start = pi->blk[phys_blk].start;
757 limit = pi->blk[phys_blk].end;
758
759 end = find_end_of_node(start, limit, size);
David Rientjes8df5bb342010-02-15 13:43:30 -0800760 /*
761 * If there won't be at least FAKE_NODE_MIN_SIZE of
762 * non-reserved memory in ZONE_DMA32 for the next node,
763 * this one must extend to the boundary.
764 */
765 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700766 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjes8df5bb342010-02-15 13:43:30 -0800767 end = dma32_end;
768
769 /*
770 * If there won't be enough non-reserved memory for the
771 * next node, this one must extend to the end of the
772 * physical node.
773 */
Tejun Heo1cca5342011-02-16 17:11:10 +0100774 if (limit - end -
775 memblock_x86_hole_size(end, limit) < size)
776 end = limit;
David Rientjes8df5bb342010-02-15 13:43:30 -0800777
Tejun Heo1cca5342011-02-16 17:11:10 +0100778 ret = emu_setup_memblk(ei, pi, nid++ % MAX_NUMNODES,
779 phys_blk,
780 min(end, limit) - start);
Tejun Heoc88aea72011-02-16 17:11:10 +0100781 if (ret < 0)
782 return ret;
David Rientjes8df5bb342010-02-15 13:43:30 -0800783 }
784 }
Tejun Heoc88aea72011-02-16 17:11:10 +0100785 return 0;
David Rientjes8df5bb342010-02-15 13:43:30 -0800786}
787
788/*
Thomas Gleixner886533a2008-05-12 15:43:36 +0200789 * Sets up the system RAM area from start_pfn to last_pfn according to the
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200790 * numa=fake command-line option.
791 */
Tejun Heoe23bba62011-02-16 17:11:10 +0100792static bool __init numa_emulation(void)
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200793{
Tejun Heo97e7b782011-02-16 17:11:08 +0100794 static struct numa_meminfo ei __initdata;
Tejun Heo1cca5342011-02-16 17:11:10 +0100795 static struct numa_meminfo pi __initdata;
Tejun Heod9c515e2011-02-16 17:11:10 +0100796 const u64 max_addr = max_pfn << PAGE_SHIFT;
Tejun Heoe23bba62011-02-16 17:11:10 +0100797 int phys_dist_cnt = numa_distance_cnt;
798 u8 *phys_dist = NULL;
Tejun Heo6b78cb52011-02-16 17:11:10 +0100799 int i, j, ret;
Tejun Heoc88aea72011-02-16 17:11:10 +0100800
801 memset(&ei, 0, sizeof(ei));
Tejun Heo1cca5342011-02-16 17:11:10 +0100802 pi = numa_meminfo;
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200803
Tejun Heo9d073ca2011-02-16 17:11:10 +0100804 for (i = 0; i < MAX_NUMNODES; i++)
805 emu_nid_to_phys[i] = NUMA_NO_NODE;
806
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200807 /*
David Rientjes8df5bb342010-02-15 13:43:30 -0800808 * If the numa=fake command-line contains a 'M' or 'G', it represents
David Rientjesca2107c2010-02-15 13:43:33 -0800809 * the fixed node size. Otherwise, if it is just a single number N,
810 * split the system RAM into N fake nodes.
David Rientjes8df5bb342010-02-15 13:43:30 -0800811 */
Tejun Heod9c515e2011-02-16 17:11:10 +0100812 if (strchr(emu_cmdline, 'M') || strchr(emu_cmdline, 'G')) {
David Rientjesca2107c2010-02-15 13:43:33 -0800813 u64 size;
814
Tejun Heod9c515e2011-02-16 17:11:10 +0100815 size = memparse(emu_cmdline, &emu_cmdline);
Tejun Heo1cca5342011-02-16 17:11:10 +0100816 ret = split_nodes_size_interleave(&ei, &pi, 0, max_addr, size);
David Rientjesca2107c2010-02-15 13:43:33 -0800817 } else {
818 unsigned long n;
819
Tejun Heod9c515e2011-02-16 17:11:10 +0100820 n = simple_strtoul(emu_cmdline, NULL, 0);
Tejun Heo1cca5342011-02-16 17:11:10 +0100821 ret = split_nodes_interleave(&ei, &pi, 0, max_addr, n);
David Rientjes8df5bb342010-02-15 13:43:30 -0800822 }
823
Tejun Heoc88aea72011-02-16 17:11:10 +0100824 if (ret < 0)
825 return false;
826
827 if (numa_cleanup_meminfo(&ei) < 0) {
828 pr_warning("NUMA: Warning: constructed meminfo invalid, disabling emulation\n");
829 return false;
830 }
831
Tejun Heoe23bba62011-02-16 17:11:10 +0100832 /*
833 * Copy the original distance table. It's temporary so no need to
834 * reserve it.
835 */
836 if (phys_dist_cnt) {
837 size_t size = phys_dist_cnt * sizeof(numa_distance[0]);
838 u64 phys;
839
840 phys = memblock_find_in_range(0,
841 (u64)max_pfn_mapped << PAGE_SHIFT,
842 size, PAGE_SIZE);
843 if (phys == MEMBLOCK_ERROR) {
844 pr_warning("NUMA: Warning: can't allocate copy of distance table, disabling emulation\n");
845 return false;
846 }
847 phys_dist = __va(phys);
848 memcpy(phys_dist, numa_distance, size);
849 }
850
Tejun Heoc88aea72011-02-16 17:11:10 +0100851 /* commit */
852 numa_meminfo = ei;
Tejun Heo8968dab2011-02-16 17:11:08 +0100853
Tejun Heo6b78cb52011-02-16 17:11:10 +0100854 /*
855 * Transform __apicid_to_node table to use emulated nids by
856 * reverse-mapping phys_nid. The maps should always exist but fall
857 * back to zero just in case.
858 */
859 for (i = 0; i < ARRAY_SIZE(__apicid_to_node); i++) {
860 if (__apicid_to_node[i] == NUMA_NO_NODE)
861 continue;
862 for (j = 0; j < ARRAY_SIZE(emu_nid_to_phys); j++)
863 if (__apicid_to_node[i] == emu_nid_to_phys[j])
864 break;
865 __apicid_to_node[i] = j < ARRAY_SIZE(emu_nid_to_phys) ? j : 0;
866 }
867
Tejun Heo9d073ca2011-02-16 17:11:10 +0100868 /* make sure all emulated nodes are mapped to a physical node */
869 for (i = 0; i < ARRAY_SIZE(emu_nid_to_phys); i++)
870 if (emu_nid_to_phys[i] == NUMA_NO_NODE)
871 emu_nid_to_phys[i] = 0;
872
Tejun Heoe23bba62011-02-16 17:11:10 +0100873 /* transform distance table */
874 numa_reset_distance();
875 for (i = 0; i < MAX_NUMNODES; i++) {
876 for (j = 0; j < MAX_NUMNODES; j++) {
877 int physi = emu_nid_to_phys[i];
878 int physj = emu_nid_to_phys[j];
879 int dist;
880
881 if (physi >= phys_dist_cnt || physj >= phys_dist_cnt)
882 dist = physi == physj ?
883 LOCAL_DISTANCE : REMOTE_DISTANCE;
884 else
885 dist = phys_dist[physi * phys_dist_cnt + physj];
886
887 numa_set_distance(i, j, dist);
888 }
889 }
Tejun Heoc88aea72011-02-16 17:11:10 +0100890 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200892#endif /* CONFIG_NUMA_EMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Yinghai Lu6d496f92011-02-17 14:53:20 +0100894static int __init dummy_numa_init(void)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100895{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 printk(KERN_INFO "%s\n",
897 numa_off ? "NUMA turned off" : "No NUMA configuration found");
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100898 printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
Tejun Heo86ef4db2011-02-16 12:13:06 +0100899 0LU, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100900
Tejun Heo92d4a432011-02-16 17:11:09 +0100901 node_set(0, numa_nodes_parsed);
Tejun Heo43a662f2011-02-16 17:11:08 +0100902 numa_add_memblk(0, 0, (u64)max_pfn << PAGE_SHIFT);
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100903
904 return 0;
905}
906
Tejun Heoffe77a42011-02-16 12:13:06 +0100907void __init initmem_init(void)
908{
909 int (*numa_init[])(void) = { [2] = dummy_numa_init };
Tejun Heoffe77a42011-02-16 12:13:06 +0100910 int i, j;
911
912 if (!numa_off) {
913#ifdef CONFIG_ACPI_NUMA
914 numa_init[0] = x86_acpi_numa_init;
Tejun Heoffe77a42011-02-16 12:13:06 +0100915#endif
916#ifdef CONFIG_AMD_NUMA
917 numa_init[1] = amd_numa_init;
Tejun Heoffe77a42011-02-16 12:13:06 +0100918#endif
919 }
920
921 for (i = 0; i < ARRAY_SIZE(numa_init); i++) {
922 if (!numa_init[i])
923 continue;
924
925 for (j = 0; j < MAX_LOCAL_APIC; j++)
926 set_apicid_to_node(j, NUMA_NO_NODE);
927
Tejun Heo92d4a432011-02-16 17:11:09 +0100928 nodes_clear(numa_nodes_parsed);
Tejun Heoffe77a42011-02-16 12:13:06 +0100929 nodes_clear(node_possible_map);
930 nodes_clear(node_online_map);
Tejun Heo97e7b782011-02-16 17:11:08 +0100931 memset(&numa_meminfo, 0, sizeof(numa_meminfo));
Tejun Heofd0435d2011-02-16 17:11:08 +0100932 remove_all_active_ranges();
Tejun Heoac7136b62011-02-16 17:11:09 +0100933 numa_reset_distance();
Tejun Heoffe77a42011-02-16 12:13:06 +0100934
935 if (numa_init[i]() < 0)
936 continue;
Tejun Heo206e4202011-02-16 12:13:07 +0100937
Tejun Heo56e827f2011-02-16 17:11:09 +0100938 if (numa_cleanup_meminfo(&numa_meminfo) < 0)
939 continue;
Tejun Heoffe77a42011-02-16 12:13:06 +0100940#ifdef CONFIG_NUMA_EMU
Tejun Heoc88aea72011-02-16 17:11:10 +0100941 /*
942 * If requested, try emulation. If emulation is not used,
943 * build identity emu_nid_to_phys[] for numa_add_cpu()
944 */
Tejun Heoe23bba62011-02-16 17:11:10 +0100945 if (!emu_cmdline || !numa_emulation())
Tejun Heoc88aea72011-02-16 17:11:10 +0100946 for (j = 0; j < ARRAY_SIZE(emu_nid_to_phys); j++)
947 emu_nid_to_phys[j] = j;
Tejun Heoffe77a42011-02-16 12:13:06 +0100948#endif
Tejun Heof9c60252011-02-16 17:11:09 +0100949 if (numa_register_memblks(&numa_meminfo) < 0)
Tejun Heo43a662f2011-02-16 17:11:08 +0100950 continue;
951
Tejun Heofd0435d2011-02-16 17:11:08 +0100952 for (j = 0; j < nr_cpu_ids; j++) {
953 int nid = early_cpu_to_node(j);
954
955 if (nid == NUMA_NO_NODE)
956 continue;
957 if (!node_online(nid))
958 numa_clear_node(j);
959 }
960 numa_init_array();
961 return;
Tejun Heoffe77a42011-02-16 12:13:06 +0100962 }
963 BUG();
Andi Kleen69d81fc2005-11-05 17:25:53 +0100964}
965
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100966unsigned long __init numa_free_all_bootmem(void)
967{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 unsigned long pages = 0;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100969 int i;
970
971 for_each_online_node(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 pages += free_all_bootmem_node(NODE_DATA(i));
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100973
Yinghai Lu08677212010-02-10 01:20:20 -0800974 pages += free_all_memory_core_early(MAX_NUMNODES);
Yinghai Lu08677212010-02-10 01:20:20 -0800975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return pages;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100977}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100979int __cpuinit numa_cpu_node(int cpu)
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800980{
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100981 int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800982
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100983 if (apicid != BAD_APICID)
984 return __apicid_to_node[apicid];
985 return NUMA_NO_NODE;
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800986}
987
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100988/*
Tejun Heode2d9442011-01-23 14:37:41 +0100989 * UGLINESS AHEAD: Currently, CONFIG_NUMA_EMU is 64bit only and makes use
990 * of 64bit specific data structures. The distinction is artificial and
991 * should be removed. numa_{add|remove}_cpu() are implemented in numa.c
992 * for both 32 and 64bit when CONFIG_NUMA_EMU is disabled but here when
993 * enabled.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100994 *
Tejun Heode2d9442011-01-23 14:37:41 +0100995 * NUMA emulation is planned to be made generic and the following and other
996 * related code should be moved to numa.c.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100997 */
Tejun Heode2d9442011-01-23 14:37:41 +0100998#ifdef CONFIG_NUMA_EMU
999# ifndef CONFIG_DEBUG_PER_CPU_MAPS
David Rientjesc1c34432010-12-22 17:23:54 -08001000void __cpuinit numa_add_cpu(int cpu)
1001{
Tejun Heobbc9e2f2011-01-23 14:37:39 +01001002 int physnid, nid;
David Rientjesc1c34432010-12-22 17:23:54 -08001003
Tejun Heobbc9e2f2011-01-23 14:37:39 +01001004 nid = numa_cpu_node(cpu);
David Rientjesc1c34432010-12-22 17:23:54 -08001005 if (nid == NUMA_NO_NODE)
1006 nid = early_cpu_to_node(cpu);
1007 BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
1008
Tejun Heo9d073ca2011-02-16 17:11:10 +01001009 physnid = emu_nid_to_phys[nid];
David Rientjesc1c34432010-12-22 17:23:54 -08001010
1011 /*
1012 * Map the cpu to each emulated node that is allocated on the physical
1013 * node of the cpu's apic id.
1014 */
Tejun Heo9d073ca2011-02-16 17:11:10 +01001015 for_each_online_node(nid)
1016 if (emu_nid_to_phys[nid] == physnid)
David Rientjesc1c34432010-12-22 17:23:54 -08001017 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
David Rientjesc1c34432010-12-22 17:23:54 -08001018}
1019
1020void __cpuinit numa_remove_cpu(int cpu)
1021{
1022 int i;
1023
1024 for_each_online_node(i)
1025 cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
1026}
Tejun Heode2d9442011-01-23 14:37:41 +01001027# else /* !CONFIG_DEBUG_PER_CPU_MAPS */
David Rientjesd906f0e2010-12-30 10:54:16 -08001028static void __cpuinit numa_set_cpumask(int cpu, int enable)
1029{
David Rientjesd906f0e2010-12-30 10:54:16 -08001030 struct cpumask *mask;
Tejun Heo9d073ca2011-02-16 17:11:10 +01001031 int nid, physnid, i;
Brian Gerst6470aff2009-01-27 12:56:47 +09001032
Tejun Heo9d073ca2011-02-16 17:11:10 +01001033 nid = early_cpu_to_node(cpu);
1034 if (nid == NUMA_NO_NODE) {
David Rientjes14392fd2011-02-07 14:08:53 -08001035 /* early_cpu_to_node() already emits a warning and trace */
1036 return;
1037 }
David Rientjesc1c34432010-12-22 17:23:54 -08001038
Tejun Heo9d073ca2011-02-16 17:11:10 +01001039 physnid = emu_nid_to_phys[nid];
1040
1041 for_each_online_node(i) {
1042 if (emu_nid_to_phys[nid] != physnid)
David Rientjesc1c34432010-12-22 17:23:54 -08001043 continue;
Tejun Heo9d073ca2011-02-16 17:11:10 +01001044
David Rientjesd906f0e2010-12-30 10:54:16 -08001045 mask = debug_cpumask_set_cpu(cpu, enable);
1046 if (!mask)
David Rientjesc1c34432010-12-22 17:23:54 -08001047 return;
David Rientjesc1c34432010-12-22 17:23:54 -08001048
1049 if (enable)
1050 cpumask_set_cpu(cpu, mask);
1051 else
1052 cpumask_clear_cpu(cpu, mask);
Brian Gerst6470aff2009-01-27 12:56:47 +09001053 }
Brian Gerst6470aff2009-01-27 12:56:47 +09001054}
1055
1056void __cpuinit numa_add_cpu(int cpu)
1057{
1058 numa_set_cpumask(cpu, 1);
1059}
1060
1061void __cpuinit numa_remove_cpu(int cpu)
1062{
1063 numa_set_cpumask(cpu, 0);
1064}
Tejun Heode2d9442011-01-23 14:37:41 +01001065# endif /* !CONFIG_DEBUG_PER_CPU_MAPS */
1066#endif /* CONFIG_NUMA_EMU */