blob: 8ce6177359008223c1777635a9395e62c93de483 [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
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 Shah076422d2007-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 Shah076422d2007-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 Shah076422d2007-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 Shah076422d2007-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 Shah076422d2007-02-13 13:26:19 +010088 return 0;
Amul Shah076422d2007-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 Shah076422d2007-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 Shah076422d2007-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 Shah076422d2007-02-13 13:26:19 +0100113{
Amul Shah54413922007-02-13 13:26:20 +0100114 int i, nodes_used = 0;
Amul Shah076422d2007-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 Shah076422d2007-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 Shah076422d2007-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 Shah076422d2007-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 Shah076422d2007-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 Shah076422d2007-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 Heoac7136b2011-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
374 size = numa_distance_cnt * sizeof(numa_distance[0]);
375 memblock_x86_free_range(__pa(numa_distance),
376 __pa(numa_distance) + size);
377 numa_distance = NULL;
378 numa_distance_cnt = 0;
379}
380
381/*
382 * Set the distance between node @from to @to to @distance. If distance
383 * table doesn't exist, one which is large enough to accomodate all the
384 * currently known nodes will be created.
385 */
386void __init numa_set_distance(int from, int to, int distance)
387{
388 if (!numa_distance) {
389 nodemask_t nodes_parsed;
390 size_t size;
391 int i, j, cnt = 0;
392 u64 phys;
393
394 /* size the new table and allocate it */
395 nodes_parsed = numa_nodes_parsed;
396 numa_nodemask_from_meminfo(&nodes_parsed, &numa_meminfo);
397
398 for_each_node_mask(i, nodes_parsed)
399 cnt = i;
400 size = ++cnt * sizeof(numa_distance[0]);
401
402 phys = memblock_find_in_range(0,
403 (u64)max_pfn_mapped << PAGE_SHIFT,
404 size, PAGE_SIZE);
405 if (phys == MEMBLOCK_ERROR) {
406 pr_warning("NUMA: Warning: can't allocate distance table!\n");
407 /* don't retry until explicitly reset */
408 numa_distance = (void *)1LU;
409 return;
410 }
411 memblock_x86_reserve_range(phys, phys + size, "NUMA DIST");
412
413 numa_distance = __va(phys);
414 numa_distance_cnt = cnt;
415
416 /* fill with the default distances */
417 for (i = 0; i < cnt; i++)
418 for (j = 0; j < cnt; j++)
419 numa_distance[i * cnt + j] = i == j ?
420 LOCAL_DISTANCE : REMOTE_DISTANCE;
421 printk(KERN_DEBUG "NUMA: Initialized distance table, cnt=%d\n", cnt);
422 }
423
424 if (from >= numa_distance_cnt || to >= numa_distance_cnt) {
425 printk_once(KERN_DEBUG "NUMA: Debug: distance out of bound, from=%d to=%d distance=%d\n",
426 from, to, distance);
427 return;
428 }
429
430 if ((u8)distance != distance ||
431 (from == to && distance != LOCAL_DISTANCE)) {
432 pr_warn_once("NUMA: Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
433 from, to, distance);
434 return;
435 }
436
437 numa_distance[from * numa_distance_cnt + to] = distance;
438}
439
440int __node_distance(int from, int to)
441{
Tejun Heoac7136b2011-02-16 17:11:09 +0100442 if (from >= numa_distance_cnt || to >= numa_distance_cnt)
443 return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
444 return numa_distance[from * numa_distance_cnt + to];
445}
446EXPORT_SYMBOL(__node_distance);
447
448/*
Tejun Heof9c60252011-02-16 17:11:09 +0100449 * Sanity check to catch more bad NUMA configurations (they are amazingly
450 * common). Make sure the nodes cover all memory.
451 */
Tejun Heo91556232011-02-16 17:11:09 +0100452static bool __init numa_meminfo_cover_memory(const struct numa_meminfo *mi)
Tejun Heof9c60252011-02-16 17:11:09 +0100453{
454 unsigned long numaram, e820ram;
455 int i;
456
457 numaram = 0;
Tejun Heo91556232011-02-16 17:11:09 +0100458 for (i = 0; i < mi->nr_blks; i++) {
459 unsigned long s = mi->blk[i].start >> PAGE_SHIFT;
460 unsigned long e = mi->blk[i].end >> PAGE_SHIFT;
Tejun Heof9c60252011-02-16 17:11:09 +0100461 numaram += e - s;
Tejun Heo91556232011-02-16 17:11:09 +0100462 numaram -= __absent_pages_in_range(mi->blk[i].nid, s, e);
Tejun Heof9c60252011-02-16 17:11:09 +0100463 if ((long)numaram < 0)
464 numaram = 0;
465 }
466
467 e820ram = max_pfn - (memblock_x86_hole_size(0,
468 max_pfn << PAGE_SHIFT) >> PAGE_SHIFT);
469 /* We seem to lose 3 pages somewhere. Allow 1M of slack. */
470 if ((long)(e820ram - numaram) >= (1 << (20 - PAGE_SHIFT))) {
471 printk(KERN_ERR "NUMA: nodes only cover %luMB of your %luMB e820 RAM. Not used.\n",
472 (numaram << PAGE_SHIFT) >> 20,
473 (e820ram << PAGE_SHIFT) >> 20);
Tejun Heo91556232011-02-16 17:11:09 +0100474 return false;
Tejun Heof9c60252011-02-16 17:11:09 +0100475 }
Tejun Heo91556232011-02-16 17:11:09 +0100476 return true;
Tejun Heof9c60252011-02-16 17:11:09 +0100477}
478
479static int __init numa_register_memblks(struct numa_meminfo *mi)
480{
Tejun Heo91556232011-02-16 17:11:09 +0100481 int i, j, nid;
Tejun Heof9c60252011-02-16 17:11:09 +0100482
483 /* Account for nodes with cpus and no memory */
Tejun Heo4697bdc2011-02-16 17:11:09 +0100484 node_possible_map = numa_nodes_parsed;
485 numa_nodemask_from_meminfo(&node_possible_map, mi);
Tejun Heof9c60252011-02-16 17:11:09 +0100486 if (WARN_ON(nodes_empty(node_possible_map)))
487 return -EINVAL;
488
Tejun Heo97e7b782011-02-16 17:11:08 +0100489 memnode_shift = compute_hash_shift(mi);
Tejun Heoef396ec2011-02-16 17:11:07 +0100490 if (memnode_shift < 0) {
491 printk(KERN_ERR "NUMA: No NUMA node hash function found. Contact maintainer\n");
492 return -EINVAL;
493 }
494
Tejun Heo97e7b782011-02-16 17:11:08 +0100495 for (i = 0; i < mi->nr_blks; i++)
496 memblock_x86_register_active_regions(mi->blk[i].nid,
497 mi->blk[i].start >> PAGE_SHIFT,
498 mi->blk[i].end >> PAGE_SHIFT);
Tejun Heofd0435d2011-02-16 17:11:08 +0100499
500 /* for out of order entries */
501 sort_node_map();
Tejun Heo91556232011-02-16 17:11:09 +0100502 if (!numa_meminfo_cover_memory(mi))
Tejun Heofd0435d2011-02-16 17:11:08 +0100503 return -EINVAL;
504
505 init_memory_mapping_high();
506
Tejun Heofd0435d2011-02-16 17:11:08 +0100507 /*
Tejun Heo91556232011-02-16 17:11:09 +0100508 * Finally register nodes. Do it twice in case setup_node_bootmem
509 * missed one due to missing bootmem.
Tejun Heofd0435d2011-02-16 17:11:08 +0100510 */
Tejun Heo91556232011-02-16 17:11:09 +0100511 for (i = 0; i < 2; i++) {
512 for_each_node_mask(nid, node_possible_map) {
513 u64 start = (u64)max_pfn << PAGE_SHIFT;
514 u64 end = 0;
515
516 if (node_online(nid))
517 continue;
518
519 for (j = 0; j < mi->nr_blks; j++) {
520 if (nid != mi->blk[j].nid)
521 continue;
522 start = min(mi->blk[j].start, start);
523 end = max(mi->blk[j].end, end);
524 }
525
526 if (start < end)
527 setup_node_bootmem(nid, start, end);
528 }
529 }
Tejun Heofd0435d2011-02-16 17:11:08 +0100530
Tejun Heoef396ec2011-02-16 17:11:07 +0100531 return 0;
532}
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534#ifdef CONFIG_NUMA_EMU
Rohit Seth53fee042007-02-13 13:26:22 +0100535/* Numa emulation */
Tejun Heo9d073ca2011-02-16 17:11:10 +0100536static int emu_nid_to_phys[MAX_NUMNODES] __cpuinitdata;
Tejun Heod9c515e2011-02-16 17:11:10 +0100537static char *emu_cmdline __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Jan Beulich90321602011-01-19 08:57:21 +0000539void __init numa_emu_cmdline(char *str)
540{
Tejun Heod9c515e2011-02-16 17:11:10 +0100541 emu_cmdline = str;
Jan Beulich90321602011-01-19 08:57:21 +0000542}
543
Tejun Heo1cca5342011-02-16 17:11:10 +0100544static int __init emu_find_memblk_by_nid(int nid, const struct numa_meminfo *mi)
545{
546 int i;
547
548 for (i = 0; i < mi->nr_blks; i++)
549 if (mi->blk[i].nid == nid)
550 return i;
551 return -ENOENT;
552}
553
Rohit Seth53fee042007-02-13 13:26:22 +0100554/*
Tejun Heoc88aea72011-02-16 17:11:10 +0100555 * Sets up nid to range from @start to @end. The return value is -errno if
556 * something went wrong, 0 otherwise.
Rohit Seth53fee042007-02-13 13:26:22 +0100557 */
Tejun Heoc88aea72011-02-16 17:11:10 +0100558static int __init emu_setup_memblk(struct numa_meminfo *ei,
Tejun Heo1cca5342011-02-16 17:11:10 +0100559 struct numa_meminfo *pi,
560 int nid, int phys_blk, u64 size)
Rohit Seth53fee042007-02-13 13:26:22 +0100561{
Tejun Heoc88aea72011-02-16 17:11:10 +0100562 struct numa_memblk *eb = &ei->blk[ei->nr_blks];
Tejun Heo1cca5342011-02-16 17:11:10 +0100563 struct numa_memblk *pb = &pi->blk[phys_blk];
Tejun Heoc88aea72011-02-16 17:11:10 +0100564
565 if (ei->nr_blks >= NR_NODE_MEMBLKS) {
566 pr_err("NUMA: Too many emulated memblks, failing emulation\n");
567 return -EINVAL;
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200568 }
Tejun Heoc88aea72011-02-16 17:11:10 +0100569
570 ei->nr_blks++;
Tejun Heo1cca5342011-02-16 17:11:10 +0100571 eb->start = pb->start;
572 eb->end = pb->start + size;
Tejun Heoc88aea72011-02-16 17:11:10 +0100573 eb->nid = nid;
Tejun Heo9d073ca2011-02-16 17:11:10 +0100574
575 if (emu_nid_to_phys[nid] == NUMA_NO_NODE)
Tejun Heo1cca5342011-02-16 17:11:10 +0100576 emu_nid_to_phys[nid] = pb->nid;
577
578 pb->start += size;
579 if (pb->start >= pb->end) {
580 WARN_ON_ONCE(pb->start > pb->end);
581 numa_remove_memblk_from(phys_blk, pi);
582 }
Tejun Heo9d073ca2011-02-16 17:11:10 +0100583
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200584 printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
Tejun Heoc88aea72011-02-16 17:11:10 +0100585 eb->start, eb->end, (eb->end - eb->start) >> 20);
586 return 0;
Rohit Seth53fee042007-02-13 13:26:22 +0100587}
588
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200589/*
David Rientjesadc19382009-09-25 15:20:09 -0700590 * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
591 * to max_addr. The return value is the number of nodes allocated.
592 */
Tejun Heoc88aea72011-02-16 17:11:10 +0100593static int __init split_nodes_interleave(struct numa_meminfo *ei,
Tejun Heo1cca5342011-02-16 17:11:10 +0100594 struct numa_meminfo *pi,
Tejun Heoc88aea72011-02-16 17:11:10 +0100595 u64 addr, u64 max_addr, int nr_nodes)
David Rientjesadc19382009-09-25 15:20:09 -0700596{
597 nodemask_t physnode_mask = NODE_MASK_NONE;
598 u64 size;
599 int big;
Tejun Heoc88aea72011-02-16 17:11:10 +0100600 int nid = 0;
601 int i, ret;
David Rientjesadc19382009-09-25 15:20:09 -0700602
603 if (nr_nodes <= 0)
604 return -1;
605 if (nr_nodes > MAX_NUMNODES) {
606 pr_info("numa=fake=%d too large, reducing to %d\n",
607 nr_nodes, MAX_NUMNODES);
608 nr_nodes = MAX_NUMNODES;
609 }
610
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700611 size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) / nr_nodes;
David Rientjesadc19382009-09-25 15:20:09 -0700612 /*
613 * Calculate the number of big nodes that can be allocated as a result
614 * of consolidating the remainder.
615 */
David Rientjes68fd1112010-02-15 13:43:25 -0800616 big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
David Rientjesadc19382009-09-25 15:20:09 -0700617 FAKE_NODE_MIN_SIZE;
618
619 size &= FAKE_NODE_MIN_HASH_MASK;
620 if (!size) {
621 pr_err("Not enough memory for each node. "
622 "NUMA emulation disabled.\n");
623 return -1;
624 }
625
Tejun Heo1cca5342011-02-16 17:11:10 +0100626 for (i = 0; i < pi->nr_blks; i++)
627 node_set(pi->blk[i].nid, physnode_mask);
David Rientjesadc19382009-09-25 15:20:09 -0700628
629 /*
630 * Continue to fill physical nodes with fake nodes until there is no
631 * memory left on any of them.
632 */
633 while (nodes_weight(physnode_mask)) {
634 for_each_node_mask(i, physnode_mask) {
David Rientjesadc19382009-09-25 15:20:09 -0700635 u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
Tejun Heo1cca5342011-02-16 17:11:10 +0100636 u64 start, limit, end;
637 int phys_blk;
638
639 phys_blk = emu_find_memblk_by_nid(i, pi);
640 if (phys_blk < 0) {
641 node_clear(i, physnode_mask);
642 continue;
643 }
644 start = pi->blk[phys_blk].start;
645 limit = pi->blk[phys_blk].end;
646 end = start + size;
David Rientjesadc19382009-09-25 15:20:09 -0700647
Tejun Heoc88aea72011-02-16 17:11:10 +0100648 if (nid < big)
David Rientjesadc19382009-09-25 15:20:09 -0700649 end += FAKE_NODE_MIN_SIZE;
650
651 /*
652 * Continue to add memory to this fake node if its
653 * non-reserved memory is less than the per-node size.
654 */
Tejun Heo1cca5342011-02-16 17:11:10 +0100655 while (end - start -
656 memblock_x86_hole_size(start, end) < size) {
David Rientjesadc19382009-09-25 15:20:09 -0700657 end += FAKE_NODE_MIN_SIZE;
Tejun Heo1cca5342011-02-16 17:11:10 +0100658 if (end > limit) {
659 end = limit;
David Rientjesadc19382009-09-25 15:20:09 -0700660 break;
661 }
662 }
663
664 /*
665 * If there won't be at least FAKE_NODE_MIN_SIZE of
666 * non-reserved memory in ZONE_DMA32 for the next node,
667 * this one must extend to the boundary.
668 */
669 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700670 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjesadc19382009-09-25 15:20:09 -0700671 end = dma32_end;
672
673 /*
674 * If there won't be enough non-reserved memory for the
675 * next node, this one must extend to the end of the
676 * physical node.
677 */
Tejun Heo1cca5342011-02-16 17:11:10 +0100678 if (limit - end -
679 memblock_x86_hole_size(end, limit) < size)
680 end = limit;
David Rientjesadc19382009-09-25 15:20:09 -0700681
Tejun Heo1cca5342011-02-16 17:11:10 +0100682 ret = emu_setup_memblk(ei, pi, nid++ % nr_nodes,
683 phys_blk,
684 min(end, limit) - start);
Tejun Heoc88aea72011-02-16 17:11:10 +0100685 if (ret < 0)
686 return ret;
David Rientjesadc19382009-09-25 15:20:09 -0700687 }
688 }
Tejun Heoc88aea72011-02-16 17:11:10 +0100689 return 0;
David Rientjesadc19382009-09-25 15:20:09 -0700690}
691
692/*
David Rientjes8df5bb32010-02-15 13:43:30 -0800693 * Returns the end address of a node so that there is at least `size' amount of
694 * non-reserved memory or `max_addr' is reached.
695 */
696static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
697{
698 u64 end = start + size;
699
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700700 while (end - start - memblock_x86_hole_size(start, end) < size) {
David Rientjes8df5bb32010-02-15 13:43:30 -0800701 end += FAKE_NODE_MIN_SIZE;
702 if (end > max_addr) {
703 end = max_addr;
704 break;
705 }
706 }
707 return end;
708}
709
710/*
711 * Sets up fake nodes of `size' interleaved over physical nodes ranging from
712 * `addr' to `max_addr'. The return value is the number of nodes allocated.
713 */
Tejun Heoc88aea72011-02-16 17:11:10 +0100714static int __init split_nodes_size_interleave(struct numa_meminfo *ei,
Tejun Heo1cca5342011-02-16 17:11:10 +0100715 struct numa_meminfo *pi,
Tejun Heoc88aea72011-02-16 17:11:10 +0100716 u64 addr, u64 max_addr, u64 size)
David Rientjes8df5bb32010-02-15 13:43:30 -0800717{
718 nodemask_t physnode_mask = NODE_MASK_NONE;
719 u64 min_size;
Tejun Heoc88aea72011-02-16 17:11:10 +0100720 int nid = 0;
721 int i, ret;
David Rientjes8df5bb32010-02-15 13:43:30 -0800722
723 if (!size)
724 return -1;
725 /*
726 * The limit on emulated nodes is MAX_NUMNODES, so the size per node is
727 * increased accordingly if the requested size is too small. This
728 * creates a uniform distribution of node sizes across the entire
729 * machine (but not necessarily over physical nodes).
730 */
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700731 min_size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) /
David Rientjes8df5bb32010-02-15 13:43:30 -0800732 MAX_NUMNODES;
733 min_size = max(min_size, FAKE_NODE_MIN_SIZE);
734 if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size)
735 min_size = (min_size + FAKE_NODE_MIN_SIZE) &
736 FAKE_NODE_MIN_HASH_MASK;
737 if (size < min_size) {
738 pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
739 size >> 20, min_size >> 20);
740 size = min_size;
741 }
742 size &= FAKE_NODE_MIN_HASH_MASK;
743
Tejun Heo1cca5342011-02-16 17:11:10 +0100744 for (i = 0; i < pi->nr_blks; i++)
745 node_set(pi->blk[i].nid, physnode_mask);
746
David Rientjes8df5bb32010-02-15 13:43:30 -0800747 /*
748 * Fill physical nodes with fake nodes of size until there is no memory
749 * left on any of them.
750 */
751 while (nodes_weight(physnode_mask)) {
752 for_each_node_mask(i, physnode_mask) {
753 u64 dma32_end = MAX_DMA32_PFN << PAGE_SHIFT;
Tejun Heo1cca5342011-02-16 17:11:10 +0100754 u64 start, limit, end;
755 int phys_blk;
David Rientjes8df5bb32010-02-15 13:43:30 -0800756
Tejun Heo1cca5342011-02-16 17:11:10 +0100757 phys_blk = emu_find_memblk_by_nid(i, pi);
758 if (phys_blk < 0) {
759 node_clear(i, physnode_mask);
760 continue;
761 }
762 start = pi->blk[phys_blk].start;
763 limit = pi->blk[phys_blk].end;
764
765 end = find_end_of_node(start, limit, size);
David Rientjes8df5bb32010-02-15 13:43:30 -0800766 /*
767 * If there won't be at least FAKE_NODE_MIN_SIZE of
768 * non-reserved memory in ZONE_DMA32 for the next node,
769 * this one must extend to the boundary.
770 */
771 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700772 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjes8df5bb32010-02-15 13:43:30 -0800773 end = dma32_end;
774
775 /*
776 * If there won't be enough non-reserved memory for the
777 * next node, this one must extend to the end of the
778 * physical node.
779 */
Tejun Heo1cca5342011-02-16 17:11:10 +0100780 if (limit - end -
781 memblock_x86_hole_size(end, limit) < size)
782 end = limit;
David Rientjes8df5bb32010-02-15 13:43:30 -0800783
Tejun Heo1cca5342011-02-16 17:11:10 +0100784 ret = emu_setup_memblk(ei, pi, nid++ % MAX_NUMNODES,
785 phys_blk,
786 min(end, limit) - start);
Tejun Heoc88aea72011-02-16 17:11:10 +0100787 if (ret < 0)
788 return ret;
David Rientjes8df5bb32010-02-15 13:43:30 -0800789 }
790 }
Tejun Heoc88aea72011-02-16 17:11:10 +0100791 return 0;
David Rientjes8df5bb32010-02-15 13:43:30 -0800792}
793
794/*
Thomas Gleixner886533a2008-05-12 15:43:36 +0200795 * Sets up the system RAM area from start_pfn to last_pfn according to the
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200796 * numa=fake command-line option.
797 */
Tejun Heoe23bba62011-02-16 17:11:10 +0100798static bool __init numa_emulation(void)
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200799{
Tejun Heo97e7b782011-02-16 17:11:08 +0100800 static struct numa_meminfo ei __initdata;
Tejun Heo1cca5342011-02-16 17:11:10 +0100801 static struct numa_meminfo pi __initdata;
Tejun Heod9c515e2011-02-16 17:11:10 +0100802 const u64 max_addr = max_pfn << PAGE_SHIFT;
Tejun Heoe23bba62011-02-16 17:11:10 +0100803 int phys_dist_cnt = numa_distance_cnt;
804 u8 *phys_dist = NULL;
Tejun Heo6b78cb52011-02-16 17:11:10 +0100805 int i, j, ret;
Tejun Heoc88aea72011-02-16 17:11:10 +0100806
807 memset(&ei, 0, sizeof(ei));
Tejun Heo1cca5342011-02-16 17:11:10 +0100808 pi = numa_meminfo;
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200809
Tejun Heo9d073ca2011-02-16 17:11:10 +0100810 for (i = 0; i < MAX_NUMNODES; i++)
811 emu_nid_to_phys[i] = NUMA_NO_NODE;
812
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200813 /*
David Rientjes8df5bb32010-02-15 13:43:30 -0800814 * If the numa=fake command-line contains a 'M' or 'G', it represents
David Rientjesca2107c2010-02-15 13:43:33 -0800815 * the fixed node size. Otherwise, if it is just a single number N,
816 * split the system RAM into N fake nodes.
David Rientjes8df5bb32010-02-15 13:43:30 -0800817 */
Tejun Heod9c515e2011-02-16 17:11:10 +0100818 if (strchr(emu_cmdline, 'M') || strchr(emu_cmdline, 'G')) {
David Rientjesca2107c2010-02-15 13:43:33 -0800819 u64 size;
820
Tejun Heod9c515e2011-02-16 17:11:10 +0100821 size = memparse(emu_cmdline, &emu_cmdline);
Tejun Heo1cca5342011-02-16 17:11:10 +0100822 ret = split_nodes_size_interleave(&ei, &pi, 0, max_addr, size);
David Rientjesca2107c2010-02-15 13:43:33 -0800823 } else {
824 unsigned long n;
825
Tejun Heod9c515e2011-02-16 17:11:10 +0100826 n = simple_strtoul(emu_cmdline, NULL, 0);
Tejun Heo1cca5342011-02-16 17:11:10 +0100827 ret = split_nodes_interleave(&ei, &pi, 0, max_addr, n);
David Rientjes8df5bb32010-02-15 13:43:30 -0800828 }
829
Tejun Heoc88aea72011-02-16 17:11:10 +0100830 if (ret < 0)
831 return false;
832
833 if (numa_cleanup_meminfo(&ei) < 0) {
834 pr_warning("NUMA: Warning: constructed meminfo invalid, disabling emulation\n");
835 return false;
836 }
837
Tejun Heoe23bba62011-02-16 17:11:10 +0100838 /*
839 * Copy the original distance table. It's temporary so no need to
840 * reserve it.
841 */
842 if (phys_dist_cnt) {
843 size_t size = phys_dist_cnt * sizeof(numa_distance[0]);
844 u64 phys;
845
846 phys = memblock_find_in_range(0,
847 (u64)max_pfn_mapped << PAGE_SHIFT,
848 size, PAGE_SIZE);
849 if (phys == MEMBLOCK_ERROR) {
850 pr_warning("NUMA: Warning: can't allocate copy of distance table, disabling emulation\n");
851 return false;
852 }
853 phys_dist = __va(phys);
854 memcpy(phys_dist, numa_distance, size);
855 }
856
Tejun Heoc88aea72011-02-16 17:11:10 +0100857 /* commit */
858 numa_meminfo = ei;
Tejun Heo8968dab2011-02-16 17:11:08 +0100859
Tejun Heo6b78cb52011-02-16 17:11:10 +0100860 /*
861 * Transform __apicid_to_node table to use emulated nids by
862 * reverse-mapping phys_nid. The maps should always exist but fall
863 * back to zero just in case.
864 */
865 for (i = 0; i < ARRAY_SIZE(__apicid_to_node); i++) {
866 if (__apicid_to_node[i] == NUMA_NO_NODE)
867 continue;
868 for (j = 0; j < ARRAY_SIZE(emu_nid_to_phys); j++)
869 if (__apicid_to_node[i] == emu_nid_to_phys[j])
870 break;
871 __apicid_to_node[i] = j < ARRAY_SIZE(emu_nid_to_phys) ? j : 0;
872 }
873
Tejun Heo9d073ca2011-02-16 17:11:10 +0100874 /* make sure all emulated nodes are mapped to a physical node */
875 for (i = 0; i < ARRAY_SIZE(emu_nid_to_phys); i++)
876 if (emu_nid_to_phys[i] == NUMA_NO_NODE)
877 emu_nid_to_phys[i] = 0;
878
Tejun Heoe23bba62011-02-16 17:11:10 +0100879 /* transform distance table */
880 numa_reset_distance();
881 for (i = 0; i < MAX_NUMNODES; i++) {
882 for (j = 0; j < MAX_NUMNODES; j++) {
883 int physi = emu_nid_to_phys[i];
884 int physj = emu_nid_to_phys[j];
885 int dist;
886
887 if (physi >= phys_dist_cnt || physj >= phys_dist_cnt)
888 dist = physi == physj ?
889 LOCAL_DISTANCE : REMOTE_DISTANCE;
890 else
891 dist = phys_dist[physi * phys_dist_cnt + physj];
892
893 numa_set_distance(i, j, dist);
894 }
895 }
Tejun Heoc88aea72011-02-16 17:11:10 +0100896 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200898#endif /* CONFIG_NUMA_EMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Tejun Heoffe77a42011-02-16 12:13:06 +0100900static int dummy_numa_init(void)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100901{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 printk(KERN_INFO "%s\n",
903 numa_off ? "NUMA turned off" : "No NUMA configuration found");
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100904 printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
Tejun Heo86ef4db2011-02-16 12:13:06 +0100905 0LU, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100906
Tejun Heo92d4a432011-02-16 17:11:09 +0100907 node_set(0, numa_nodes_parsed);
Tejun Heo43a662f2011-02-16 17:11:08 +0100908 numa_add_memblk(0, 0, (u64)max_pfn << PAGE_SHIFT);
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100909
910 return 0;
911}
912
Tejun Heoffe77a42011-02-16 12:13:06 +0100913void __init initmem_init(void)
914{
915 int (*numa_init[])(void) = { [2] = dummy_numa_init };
Tejun Heoffe77a42011-02-16 12:13:06 +0100916 int i, j;
917
918 if (!numa_off) {
919#ifdef CONFIG_ACPI_NUMA
920 numa_init[0] = x86_acpi_numa_init;
Tejun Heoffe77a42011-02-16 12:13:06 +0100921#endif
922#ifdef CONFIG_AMD_NUMA
923 numa_init[1] = amd_numa_init;
Tejun Heoffe77a42011-02-16 12:13:06 +0100924#endif
925 }
926
927 for (i = 0; i < ARRAY_SIZE(numa_init); i++) {
928 if (!numa_init[i])
929 continue;
930
931 for (j = 0; j < MAX_LOCAL_APIC; j++)
932 set_apicid_to_node(j, NUMA_NO_NODE);
933
Tejun Heo92d4a432011-02-16 17:11:09 +0100934 nodes_clear(numa_nodes_parsed);
Tejun Heoffe77a42011-02-16 12:13:06 +0100935 nodes_clear(node_possible_map);
936 nodes_clear(node_online_map);
Tejun Heo97e7b782011-02-16 17:11:08 +0100937 memset(&numa_meminfo, 0, sizeof(numa_meminfo));
Tejun Heofd0435d2011-02-16 17:11:08 +0100938 remove_all_active_ranges();
Tejun Heoac7136b2011-02-16 17:11:09 +0100939 numa_reset_distance();
Tejun Heoffe77a42011-02-16 12:13:06 +0100940
941 if (numa_init[i]() < 0)
942 continue;
Tejun Heo206e4202011-02-16 12:13:07 +0100943
Tejun Heo56e827f2011-02-16 17:11:09 +0100944 if (numa_cleanup_meminfo(&numa_meminfo) < 0)
945 continue;
Tejun Heoffe77a42011-02-16 12:13:06 +0100946#ifdef CONFIG_NUMA_EMU
Tejun Heoc88aea72011-02-16 17:11:10 +0100947 /*
948 * If requested, try emulation. If emulation is not used,
949 * build identity emu_nid_to_phys[] for numa_add_cpu()
950 */
Tejun Heoe23bba62011-02-16 17:11:10 +0100951 if (!emu_cmdline || !numa_emulation())
Tejun Heoc88aea72011-02-16 17:11:10 +0100952 for (j = 0; j < ARRAY_SIZE(emu_nid_to_phys); j++)
953 emu_nid_to_phys[j] = j;
Tejun Heoffe77a42011-02-16 12:13:06 +0100954#endif
Tejun Heof9c60252011-02-16 17:11:09 +0100955 if (numa_register_memblks(&numa_meminfo) < 0)
Tejun Heo43a662f2011-02-16 17:11:08 +0100956 continue;
957
Tejun Heofd0435d2011-02-16 17:11:08 +0100958 for (j = 0; j < nr_cpu_ids; j++) {
959 int nid = early_cpu_to_node(j);
960
961 if (nid == NUMA_NO_NODE)
962 continue;
963 if (!node_online(nid))
964 numa_clear_node(j);
965 }
966 numa_init_array();
967 return;
Tejun Heoffe77a42011-02-16 12:13:06 +0100968 }
969 BUG();
Andi Kleen69d81fc2005-11-05 17:25:53 +0100970}
971
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100972unsigned long __init numa_free_all_bootmem(void)
973{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 unsigned long pages = 0;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100975 int i;
976
977 for_each_online_node(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 pages += free_all_bootmem_node(NODE_DATA(i));
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100979
Yinghai Lu08677212010-02-10 01:20:20 -0800980 pages += free_all_memory_core_early(MAX_NUMNODES);
Yinghai Lu08677212010-02-10 01:20:20 -0800981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return pages;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100983}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100985int __cpuinit numa_cpu_node(int cpu)
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800986{
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100987 int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800988
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100989 if (apicid != BAD_APICID)
990 return __apicid_to_node[apicid];
991 return NUMA_NO_NODE;
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800992}
993
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +0100994/*
Tejun Heode2d9442011-01-23 14:37:41 +0100995 * UGLINESS AHEAD: Currently, CONFIG_NUMA_EMU is 64bit only and makes use
996 * of 64bit specific data structures. The distinction is artificial and
997 * should be removed. numa_{add|remove}_cpu() are implemented in numa.c
998 * for both 32 and 64bit when CONFIG_NUMA_EMU is disabled but here when
999 * enabled.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +01001000 *
Tejun Heode2d9442011-01-23 14:37:41 +01001001 * NUMA emulation is planned to be made generic and the following and other
1002 * related code should be moved to numa.c.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +01001003 */
Tejun Heode2d9442011-01-23 14:37:41 +01001004#ifdef CONFIG_NUMA_EMU
1005# ifndef CONFIG_DEBUG_PER_CPU_MAPS
David Rientjesc1c34432010-12-22 17:23:54 -08001006void __cpuinit numa_add_cpu(int cpu)
1007{
Tejun Heobbc9e2f2011-01-23 14:37:39 +01001008 int physnid, nid;
David Rientjesc1c34432010-12-22 17:23:54 -08001009
Tejun Heobbc9e2f2011-01-23 14:37:39 +01001010 nid = numa_cpu_node(cpu);
David Rientjesc1c34432010-12-22 17:23:54 -08001011 if (nid == NUMA_NO_NODE)
1012 nid = early_cpu_to_node(cpu);
1013 BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
1014
Tejun Heo9d073ca2011-02-16 17:11:10 +01001015 physnid = emu_nid_to_phys[nid];
David Rientjesc1c34432010-12-22 17:23:54 -08001016
1017 /*
1018 * Map the cpu to each emulated node that is allocated on the physical
1019 * node of the cpu's apic id.
1020 */
Tejun Heo9d073ca2011-02-16 17:11:10 +01001021 for_each_online_node(nid)
1022 if (emu_nid_to_phys[nid] == physnid)
David Rientjesc1c34432010-12-22 17:23:54 -08001023 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
David Rientjesc1c34432010-12-22 17:23:54 -08001024}
1025
1026void __cpuinit numa_remove_cpu(int cpu)
1027{
1028 int i;
1029
1030 for_each_online_node(i)
1031 cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
1032}
Tejun Heode2d9442011-01-23 14:37:41 +01001033# else /* !CONFIG_DEBUG_PER_CPU_MAPS */
David Rientjesd906f0e2010-12-30 10:54:16 -08001034static void __cpuinit numa_set_cpumask(int cpu, int enable)
1035{
David Rientjesd906f0e2010-12-30 10:54:16 -08001036 struct cpumask *mask;
Tejun Heo9d073ca2011-02-16 17:11:10 +01001037 int nid, physnid, i;
Brian Gerst6470aff2009-01-27 12:56:47 +09001038
Tejun Heo9d073ca2011-02-16 17:11:10 +01001039 nid = early_cpu_to_node(cpu);
1040 if (nid == NUMA_NO_NODE) {
David Rientjes14392fd2011-02-07 14:08:53 -08001041 /* early_cpu_to_node() already emits a warning and trace */
1042 return;
1043 }
David Rientjesc1c34432010-12-22 17:23:54 -08001044
Tejun Heo9d073ca2011-02-16 17:11:10 +01001045 physnid = emu_nid_to_phys[nid];
1046
1047 for_each_online_node(i) {
1048 if (emu_nid_to_phys[nid] != physnid)
David Rientjesc1c34432010-12-22 17:23:54 -08001049 continue;
Tejun Heo9d073ca2011-02-16 17:11:10 +01001050
David Rientjesd906f0e2010-12-30 10:54:16 -08001051 mask = debug_cpumask_set_cpu(cpu, enable);
1052 if (!mask)
David Rientjesc1c34432010-12-22 17:23:54 -08001053 return;
David Rientjesc1c34432010-12-22 17:23:54 -08001054
1055 if (enable)
1056 cpumask_set_cpu(cpu, mask);
1057 else
1058 cpumask_clear_cpu(cpu, mask);
Brian Gerst6470aff2009-01-27 12:56:47 +09001059 }
Brian Gerst6470aff2009-01-27 12:56:47 +09001060}
1061
1062void __cpuinit numa_add_cpu(int cpu)
1063{
1064 numa_set_cpumask(cpu, 1);
1065}
1066
1067void __cpuinit numa_remove_cpu(int cpu)
1068{
1069 numa_set_cpumask(cpu, 0);
1070}
Tejun Heode2d9442011-01-23 14:37:41 +01001071# endif /* !CONFIG_DEBUG_PER_CPU_MAPS */
1072#endif /* CONFIG_NUMA_EMU */