blob: a3621f2953d6357b4b9a9228b73359b6bec34eb7 [file] [log] [blame]
Thomas Gleixnere3cfe522008-01-30 13:30:37 +01001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Generic VM initialization for x86-64 NUMA setups.
3 * Copyright 2002,2003 Andi Kleen, SuSE Labs.
Thomas Gleixnere3cfe522008-01-30 13:30:37 +01004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/kernel.h>
6#include <linux/mm.h>
7#include <linux/string.h>
8#include <linux/init.h>
9#include <linux/bootmem.h>
Yinghai Lu72d7c3b2010-08-25 13:39:17 -070010#include <linux/memblock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/mmzone.h>
12#include <linux/ctype.h>
13#include <linux/module.h>
14#include <linux/nodemask.h>
travis@sgi.com3cc87e32008-01-30 13:33:11 +010015#include <linux/sched.h>
Tejun Heod8fc3af2011-02-16 12:13:06 +010016#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <asm/e820.h>
19#include <asm/proto.h>
20#include <asm/dma.h>
21#include <asm/numa.h>
22#include <asm/acpi.h>
Andreas Herrmann23ac4ae2010-09-17 18:03:43 +020023#include <asm/amd_nb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Tejun Heo97e7b782011-02-16 17:11:08 +010025struct numa_memblk {
26 u64 start;
27 u64 end;
28 int nid;
29};
30
31struct numa_meminfo {
32 int nr_blks;
33 struct numa_memblk blk[NR_NODE_MEMBLKS];
34};
35
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -070036struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010037EXPORT_SYMBOL(node_data);
38
Tejun Heo92d4a432011-02-16 17:11:09 +010039nodemask_t numa_nodes_parsed __initdata;
Tejun Heoec8cf29b2011-02-16 12:13:07 +010040
Eric Dumazetdcf36bf2006-03-25 16:31:46 +010041struct memnode memnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Thomas Gleixner864fc312008-05-12 15:43:36 +020043static unsigned long __initdata nodemap_addr;
44static unsigned long __initdata nodemap_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Tejun Heo97e7b782011-02-16 17:11:08 +010046static struct numa_meminfo numa_meminfo __initdata;
Tejun Heoef396ec2011-02-16 17:11:07 +010047
Tejun Heoac7136b2011-02-16 17:11:09 +010048static int numa_distance_cnt;
49static u8 *numa_distance;
50
51#ifdef CONFIG_NUMA_EMU
52static bool numa_emu_dist;
53#endif
54
Brian Gerst6470aff2009-01-27 12:56:47 +090055/*
Eric Dumazet529a3402005-11-05 17:25:54 +010056 * Given a shift value, try to populate memnodemap[]
57 * Returns :
58 * 1 if OK
59 * 0 if memnodmap[] too small (of shift too small)
60 * -1 if node overlap or lost ram (shift too big)
61 */
Tejun Heo97e7b782011-02-16 17:11:08 +010062static int __init populate_memnodemap(const struct numa_meminfo *mi, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Eric Dumazet529a3402005-11-05 17:25:54 +010064 unsigned long addr, end;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010065 int i, res = -1;
Keith Manntheyb6846642005-07-28 21:15:38 -070066
travis@sgi.com43238382008-01-30 13:33:25 +010067 memset(memnodemap, 0xff, sizeof(s16)*memnodemapsize);
Tejun Heo97e7b782011-02-16 17:11:08 +010068 for (i = 0; i < mi->nr_blks; i++) {
69 addr = mi->blk[i].start;
70 end = mi->blk[i].end;
Eric Dumazet529a3402005-11-05 17:25:54 +010071 if (addr >= end)
72 continue;
Amul Shah076422d2007-02-13 13:26:19 +010073 if ((end >> shift) >= memnodemapsize)
Eric Dumazet529a3402005-11-05 17:25:54 +010074 return 0;
75 do {
travis@sgi.com43238382008-01-30 13:33:25 +010076 if (memnodemap[addr >> shift] != NUMA_NO_NODE)
Eric Dumazet529a3402005-11-05 17:25:54 +010077 return -1;
Tejun Heo97e7b782011-02-16 17:11:08 +010078 memnodemap[addr >> shift] = mi->blk[i].nid;
Amul Shah076422d2007-02-13 13:26:19 +010079 addr += (1UL << shift);
Eric Dumazet529a3402005-11-05 17:25:54 +010080 } while (addr < end);
81 res = 1;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010082 }
Eric Dumazet529a3402005-11-05 17:25:54 +010083 return res;
84}
85
Amul Shah076422d2007-02-13 13:26:19 +010086static int __init allocate_cachealigned_memnodemap(void)
87{
Yinghai Lu24a5da72008-02-01 17:49:41 +010088 unsigned long addr;
Amul Shah076422d2007-02-13 13:26:19 +010089
90 memnodemap = memnode.embedded_map;
travis@sgi.com316390b2008-01-30 13:33:15 +010091 if (memnodemapsize <= ARRAY_SIZE(memnode.embedded_map))
Amul Shah076422d2007-02-13 13:26:19 +010092 return 0;
Amul Shah076422d2007-02-13 13:26:19 +010093
Yinghai Lu24a5da72008-02-01 17:49:41 +010094 addr = 0x8000;
Joerg Roedelbe3e89e2008-07-25 16:48:58 +020095 nodemap_size = roundup(sizeof(s16) * memnodemapsize, L1_CACHE_BYTES);
Yinghai Ludbef7b52010-12-27 16:48:08 -080096 nodemap_addr = memblock_find_in_range(addr, get_max_mapped(),
Yinghai Lu24a5da72008-02-01 17:49:41 +010097 nodemap_size, L1_CACHE_BYTES);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070098 if (nodemap_addr == MEMBLOCK_ERROR) {
Amul Shah076422d2007-02-13 13:26:19 +010099 printk(KERN_ERR
100 "NUMA: Unable to allocate Memory to Node hash map\n");
101 nodemap_addr = nodemap_size = 0;
102 return -1;
103 }
Yinghai Lu24a5da72008-02-01 17:49:41 +0100104 memnodemap = phys_to_virt(nodemap_addr);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700105 memblock_x86_reserve_range(nodemap_addr, nodemap_addr + nodemap_size, "MEMNODEMAP");
Amul Shah076422d2007-02-13 13:26:19 +0100106
107 printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n",
108 nodemap_addr, nodemap_addr + nodemap_size);
109 return 0;
110}
111
112/*
113 * The LSB of all start and end addresses in the node map is the value of the
114 * maximum possible shift.
115 */
Tejun Heo97e7b782011-02-16 17:11:08 +0100116static int __init extract_lsb_from_nodes(const struct numa_meminfo *mi)
Amul Shah076422d2007-02-13 13:26:19 +0100117{
Amul Shah54413922007-02-13 13:26:20 +0100118 int i, nodes_used = 0;
Amul Shah076422d2007-02-13 13:26:19 +0100119 unsigned long start, end;
120 unsigned long bitfield = 0, memtop = 0;
121
Tejun Heo97e7b782011-02-16 17:11:08 +0100122 for (i = 0; i < mi->nr_blks; i++) {
123 start = mi->blk[i].start;
124 end = mi->blk[i].end;
Amul Shah076422d2007-02-13 13:26:19 +0100125 if (start >= end)
126 continue;
Amul Shah54413922007-02-13 13:26:20 +0100127 bitfield |= start;
128 nodes_used++;
Amul Shah076422d2007-02-13 13:26:19 +0100129 if (end > memtop)
130 memtop = end;
131 }
Amul Shah54413922007-02-13 13:26:20 +0100132 if (nodes_used <= 1)
133 i = 63;
134 else
135 i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
Amul Shah076422d2007-02-13 13:26:19 +0100136 memnodemapsize = (memtop >> i)+1;
137 return i;
138}
139
Tejun Heo97e7b782011-02-16 17:11:08 +0100140static int __init compute_hash_shift(const struct numa_meminfo *mi)
Eric Dumazet529a3402005-11-05 17:25:54 +0100141{
Amul Shah076422d2007-02-13 13:26:19 +0100142 int shift;
Eric Dumazet529a3402005-11-05 17:25:54 +0100143
Tejun Heo97e7b782011-02-16 17:11:08 +0100144 shift = extract_lsb_from_nodes(mi);
Amul Shah076422d2007-02-13 13:26:19 +0100145 if (allocate_cachealigned_memnodemap())
146 return -1;
Andi Kleen6b050f82006-01-11 22:44:33 +0100147 printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
Eric Dumazet529a3402005-11-05 17:25:54 +0100148 shift);
149
Tejun Heo97e7b782011-02-16 17:11:08 +0100150 if (populate_memnodemap(mi, shift) != 1) {
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100151 printk(KERN_INFO "Your memory is not aligned you need to "
152 "rebuild your kernel with a bigger NODEMAPSIZE "
153 "shift=%d\n", shift);
Eric Dumazet529a3402005-11-05 17:25:54 +0100154 return -1;
155 }
Keith Manntheyb6846642005-07-28 21:15:38 -0700156 return shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -0800159int __meminit __early_pfn_to_nid(unsigned long pfn)
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700160{
161 return phys_to_nid(pfn << PAGE_SHIFT);
162}
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700163
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100164static void * __init early_node_mem(int nodeid, unsigned long start,
Yinghai Lu24a5da72008-02-01 17:49:41 +0100165 unsigned long end, unsigned long size,
166 unsigned long align)
Andi Kleena8062232006-04-07 19:49:21 +0200167{
Yinghai Lucef625e2010-02-10 01:20:18 -0800168 unsigned long mem;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100169
Yinghai Lucef625e2010-02-10 01:20:18 -0800170 /*
171 * put it on high as possible
172 * something will go with NODE_DATA
173 */
174 if (start < (MAX_DMA_PFN<<PAGE_SHIFT))
175 start = MAX_DMA_PFN<<PAGE_SHIFT;
176 if (start < (MAX_DMA32_PFN<<PAGE_SHIFT) &&
177 end > (MAX_DMA32_PFN<<PAGE_SHIFT))
178 start = MAX_DMA32_PFN<<PAGE_SHIFT;
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700179 mem = memblock_x86_find_in_range_node(nodeid, start, end, size, align);
180 if (mem != MEMBLOCK_ERROR)
Andi Kleena8062232006-04-07 19:49:21 +0200181 return __va(mem);
Yinghai Lu9347e0b2008-02-01 17:49:42 +0100182
Yinghai Lucef625e2010-02-10 01:20:18 -0800183 /* extend the search scope */
184 end = max_pfn_mapped << PAGE_SHIFT;
Yinghai Lu419db272010-10-28 09:50:17 -0700185 start = MAX_DMA_PFN << PAGE_SHIFT;
186 mem = memblock_find_in_range(start, end, size, align);
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700187 if (mem != MEMBLOCK_ERROR)
Yinghai Lu1842f902010-02-10 01:20:15 -0800188 return __va(mem);
189
190 printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100191 size, nodeid);
Yinghai Lu1842f902010-02-10 01:20:15 -0800192
193 return NULL;
Andi Kleena8062232006-04-07 19:49:21 +0200194}
195
Tejun Heoef396ec2011-02-16 17:11:07 +0100196int __init numa_add_memblk(int nid, u64 start, u64 end)
197{
Tejun Heo97e7b782011-02-16 17:11:08 +0100198 struct numa_meminfo *mi = &numa_meminfo;
Tejun Heoef396ec2011-02-16 17:11:07 +0100199
Tejun Heo56e827f2011-02-16 17:11:09 +0100200 /* ignore zero length blks */
201 if (start == end)
202 return 0;
203
204 /* whine about and ignore invalid blks */
205 if (start > end || nid < 0 || nid >= MAX_NUMNODES) {
206 pr_warning("NUMA: Warning: invalid memblk node %d (%Lx-%Lx)\n",
207 nid, start, end);
208 return 0;
209 }
210
211 if (mi->nr_blks >= NR_NODE_MEMBLKS) {
212 pr_err("NUMA: too many memblk ranges\n");
Tejun Heoef396ec2011-02-16 17:11:07 +0100213 return -EINVAL;
214 }
215
Tejun Heo97e7b782011-02-16 17:11:08 +0100216 mi->blk[mi->nr_blks].start = start;
217 mi->blk[mi->nr_blks].end = end;
218 mi->blk[mi->nr_blks].nid = nid;
219 mi->nr_blks++;
Tejun Heoef396ec2011-02-16 17:11:07 +0100220 return 0;
221}
222
Tejun Heo2e756be2011-02-16 17:11:09 +0100223static void __init numa_remove_memblk_from(int idx, struct numa_meminfo *mi)
224{
225 mi->nr_blks--;
226 memmove(&mi->blk[idx], &mi->blk[idx + 1],
227 (mi->nr_blks - idx) * sizeof(mi->blk[0]));
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{
442#if defined(CONFIG_ACPI_NUMA) && defined(CONFIG_NUMA_EMU)
443 if (numa_emu_dist)
444 return acpi_emu_node_distance(from, to);
445#endif
446 if (from >= numa_distance_cnt || to >= numa_distance_cnt)
447 return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
448 return numa_distance[from * numa_distance_cnt + to];
449}
450EXPORT_SYMBOL(__node_distance);
451
452/*
Tejun Heof9c60252011-02-16 17:11:09 +0100453 * Sanity check to catch more bad NUMA configurations (they are amazingly
454 * common). Make sure the nodes cover all memory.
455 */
Tejun Heo91556232011-02-16 17:11:09 +0100456static bool __init numa_meminfo_cover_memory(const struct numa_meminfo *mi)
Tejun Heof9c60252011-02-16 17:11:09 +0100457{
458 unsigned long numaram, e820ram;
459 int i;
460
461 numaram = 0;
Tejun Heo91556232011-02-16 17:11:09 +0100462 for (i = 0; i < mi->nr_blks; i++) {
463 unsigned long s = mi->blk[i].start >> PAGE_SHIFT;
464 unsigned long e = mi->blk[i].end >> PAGE_SHIFT;
Tejun Heof9c60252011-02-16 17:11:09 +0100465 numaram += e - s;
Tejun Heo91556232011-02-16 17:11:09 +0100466 numaram -= __absent_pages_in_range(mi->blk[i].nid, s, e);
Tejun Heof9c60252011-02-16 17:11:09 +0100467 if ((long)numaram < 0)
468 numaram = 0;
469 }
470
471 e820ram = max_pfn - (memblock_x86_hole_size(0,
472 max_pfn << PAGE_SHIFT) >> PAGE_SHIFT);
473 /* We seem to lose 3 pages somewhere. Allow 1M of slack. */
474 if ((long)(e820ram - numaram) >= (1 << (20 - PAGE_SHIFT))) {
475 printk(KERN_ERR "NUMA: nodes only cover %luMB of your %luMB e820 RAM. Not used.\n",
476 (numaram << PAGE_SHIFT) >> 20,
477 (e820ram << PAGE_SHIFT) >> 20);
Tejun Heo91556232011-02-16 17:11:09 +0100478 return false;
Tejun Heof9c60252011-02-16 17:11:09 +0100479 }
Tejun Heo91556232011-02-16 17:11:09 +0100480 return true;
Tejun Heof9c60252011-02-16 17:11:09 +0100481}
482
483static int __init numa_register_memblks(struct numa_meminfo *mi)
484{
Tejun Heo91556232011-02-16 17:11:09 +0100485 int i, j, nid;
Tejun Heof9c60252011-02-16 17:11:09 +0100486
487 /* Account for nodes with cpus and no memory */
Tejun Heo4697bdc2011-02-16 17:11:09 +0100488 node_possible_map = numa_nodes_parsed;
489 numa_nodemask_from_meminfo(&node_possible_map, mi);
Tejun Heof9c60252011-02-16 17:11:09 +0100490 if (WARN_ON(nodes_empty(node_possible_map)))
491 return -EINVAL;
492
Tejun Heo97e7b782011-02-16 17:11:08 +0100493 memnode_shift = compute_hash_shift(mi);
Tejun Heoef396ec2011-02-16 17:11:07 +0100494 if (memnode_shift < 0) {
495 printk(KERN_ERR "NUMA: No NUMA node hash function found. Contact maintainer\n");
496 return -EINVAL;
497 }
498
Tejun Heo97e7b782011-02-16 17:11:08 +0100499 for (i = 0; i < mi->nr_blks; i++)
500 memblock_x86_register_active_regions(mi->blk[i].nid,
501 mi->blk[i].start >> PAGE_SHIFT,
502 mi->blk[i].end >> PAGE_SHIFT);
Tejun Heofd0435d2011-02-16 17:11:08 +0100503
504 /* for out of order entries */
505 sort_node_map();
Tejun Heo91556232011-02-16 17:11:09 +0100506 if (!numa_meminfo_cover_memory(mi))
Tejun Heofd0435d2011-02-16 17:11:08 +0100507 return -EINVAL;
508
509 init_memory_mapping_high();
510
Tejun Heofd0435d2011-02-16 17:11:08 +0100511 /*
Tejun Heo91556232011-02-16 17:11:09 +0100512 * Finally register nodes. Do it twice in case setup_node_bootmem
513 * missed one due to missing bootmem.
Tejun Heofd0435d2011-02-16 17:11:08 +0100514 */
Tejun Heo91556232011-02-16 17:11:09 +0100515 for (i = 0; i < 2; i++) {
516 for_each_node_mask(nid, node_possible_map) {
517 u64 start = (u64)max_pfn << PAGE_SHIFT;
518 u64 end = 0;
519
520 if (node_online(nid))
521 continue;
522
523 for (j = 0; j < mi->nr_blks; j++) {
524 if (nid != mi->blk[j].nid)
525 continue;
526 start = min(mi->blk[j].start, start);
527 end = max(mi->blk[j].end, end);
528 }
529
530 if (start < end)
531 setup_node_bootmem(nid, start, end);
532 }
533 }
Tejun Heofd0435d2011-02-16 17:11:08 +0100534
Tejun Heoef396ec2011-02-16 17:11:07 +0100535 return 0;
536}
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538#ifdef CONFIG_NUMA_EMU
Rohit Seth53fee042007-02-13 13:26:22 +0100539/* Numa emulation */
David Rientjesadc19382009-09-25 15:20:09 -0700540static struct bootnode nodes[MAX_NUMNODES] __initdata;
David Rientjesc1c34432010-12-22 17:23:54 -0800541static struct bootnode physnodes[MAX_NUMNODES] __cpuinitdata;
Thomas Gleixner864fc312008-05-12 15:43:36 +0200542static char *cmdline __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Jan Beulich90321602011-01-19 08:57:21 +0000544void __init numa_emu_cmdline(char *str)
545{
546 cmdline = str;
547}
548
Tejun Heoa844ef42011-02-16 17:11:09 +0100549int __init find_node_by_addr(unsigned long addr)
550{
Tejun Heo91556232011-02-16 17:11:09 +0100551 const struct numa_meminfo *mi = &numa_meminfo;
Tejun Heoa844ef42011-02-16 17:11:09 +0100552 int i;
553
Tejun Heo91556232011-02-16 17:11:09 +0100554 for (i = 0; i < mi->nr_blks; i++) {
Tejun Heoa844ef42011-02-16 17:11:09 +0100555 /*
556 * Find the real node that this emulated node appears on. For
557 * the sake of simplicity, we only use a real node's starting
558 * address to determine which emulated node it appears on.
559 */
Tejun Heo91556232011-02-16 17:11:09 +0100560 if (addr >= mi->blk[i].start && addr < mi->blk[i].end)
561 return mi->blk[i].nid;
Tejun Heoa844ef42011-02-16 17:11:09 +0100562 }
Tejun Heo91556232011-02-16 17:11:09 +0100563 return NUMA_NO_NODE;
Tejun Heoa844ef42011-02-16 17:11:09 +0100564}
565
Tejun Heo19095542011-02-16 12:13:07 +0100566static int __init setup_physnodes(unsigned long start, unsigned long end)
David Rientjesadc19382009-09-25 15:20:09 -0700567{
Tejun Heo91556232011-02-16 17:11:09 +0100568 const struct numa_meminfo *mi = &numa_meminfo;
David Rientjesadc19382009-09-25 15:20:09 -0700569 int ret = 0;
570 int i;
571
David Rientjesc1c34432010-12-22 17:23:54 -0800572 memset(physnodes, 0, sizeof(physnodes));
Tejun Heo19095542011-02-16 12:13:07 +0100573
Tejun Heo91556232011-02-16 17:11:09 +0100574 for (i = 0; i < mi->nr_blks; i++) {
575 int nid = mi->blk[i].nid;
576
577 if (physnodes[nid].start == physnodes[nid].end) {
578 physnodes[nid].start = mi->blk[i].start;
579 physnodes[nid].end = mi->blk[i].end;
580 } else {
581 physnodes[nid].start = min(physnodes[nid].start,
582 mi->blk[i].start);
583 physnodes[nid].end = max(physnodes[nid].end,
584 mi->blk[i].end);
585 }
Tejun Heo19095542011-02-16 12:13:07 +0100586 }
587
David Rientjesadc19382009-09-25 15:20:09 -0700588 /*
589 * Basic sanity checking on the physical node map: there may be errors
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200590 * if the SRAT or AMD code incorrectly reported the topology or the mem=
David Rientjesadc19382009-09-25 15:20:09 -0700591 * kernel parameter is used.
592 */
David Rientjesa387e952010-12-22 17:23:56 -0800593 for (i = 0; i < MAX_NUMNODES; i++) {
David Rientjesadc19382009-09-25 15:20:09 -0700594 if (physnodes[i].start == physnodes[i].end)
595 continue;
596 if (physnodes[i].start > end) {
597 physnodes[i].end = physnodes[i].start;
598 continue;
599 }
600 if (physnodes[i].end < start) {
601 physnodes[i].start = physnodes[i].end;
602 continue;
603 }
604 if (physnodes[i].start < start)
605 physnodes[i].start = start;
606 if (physnodes[i].end > end)
607 physnodes[i].end = end;
David Rientjesadc19382009-09-25 15:20:09 -0700608 ret++;
609 }
610
611 /*
612 * If no physical topology was detected, a single node is faked to cover
613 * the entire address space.
614 */
615 if (!ret) {
616 physnodes[ret].start = start;
617 physnodes[ret].end = end;
618 ret = 1;
619 }
620 return ret;
621}
622
David Rientjesf51bf302010-12-22 17:23:51 -0800623static void __init fake_physnodes(int acpi, int amd, int nr_nodes)
624{
625 int i;
626
627 BUG_ON(acpi && amd);
628#ifdef CONFIG_ACPI_NUMA
629 if (acpi)
630 acpi_fake_nodes(nodes, nr_nodes);
631#endif
632#ifdef CONFIG_AMD_NUMA
633 if (amd)
634 amd_fake_nodes(nodes, nr_nodes);
635#endif
636 if (!acpi && !amd)
637 for (i = 0; i < nr_cpu_ids; i++)
638 numa_set_node(i, 0);
639}
640
Rohit Seth53fee042007-02-13 13:26:22 +0100641/*
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100642 * Setups up nid to range from addr to addr + size. If the end
643 * boundary is greater than max_addr, then max_addr is used instead.
644 * The return value is 0 if there is additional memory left for
645 * allocation past addr and -1 otherwise. addr is adjusted to be at
646 * the end of the node.
Rohit Seth53fee042007-02-13 13:26:22 +0100647 */
David Rientjesadc19382009-09-25 15:20:09 -0700648static int __init setup_node_range(int nid, u64 *addr, u64 size, u64 max_addr)
Rohit Seth53fee042007-02-13 13:26:22 +0100649{
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200650 int ret = 0;
651 nodes[nid].start = *addr;
652 *addr += size;
653 if (*addr >= max_addr) {
654 *addr = max_addr;
655 ret = -1;
656 }
657 nodes[nid].end = *addr;
Suresh Siddhae3f1cae2007-05-02 19:27:20 +0200658 node_set(nid, node_possible_map);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200659 printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
660 nodes[nid].start, nodes[nid].end,
661 (nodes[nid].end - nodes[nid].start) >> 20);
662 return ret;
Rohit Seth53fee042007-02-13 13:26:22 +0100663}
664
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200665/*
David Rientjesadc19382009-09-25 15:20:09 -0700666 * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
667 * to max_addr. The return value is the number of nodes allocated.
668 */
David Rientjesc1c34432010-12-22 17:23:54 -0800669static int __init split_nodes_interleave(u64 addr, u64 max_addr, int nr_nodes)
David Rientjesadc19382009-09-25 15:20:09 -0700670{
671 nodemask_t physnode_mask = NODE_MASK_NONE;
672 u64 size;
673 int big;
674 int ret = 0;
675 int i;
676
677 if (nr_nodes <= 0)
678 return -1;
679 if (nr_nodes > MAX_NUMNODES) {
680 pr_info("numa=fake=%d too large, reducing to %d\n",
681 nr_nodes, MAX_NUMNODES);
682 nr_nodes = MAX_NUMNODES;
683 }
684
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700685 size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) / nr_nodes;
David Rientjesadc19382009-09-25 15:20:09 -0700686 /*
687 * Calculate the number of big nodes that can be allocated as a result
688 * of consolidating the remainder.
689 */
David Rientjes68fd1112010-02-15 13:43:25 -0800690 big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
David Rientjesadc19382009-09-25 15:20:09 -0700691 FAKE_NODE_MIN_SIZE;
692
693 size &= FAKE_NODE_MIN_HASH_MASK;
694 if (!size) {
695 pr_err("Not enough memory for each node. "
696 "NUMA emulation disabled.\n");
697 return -1;
698 }
699
David Rientjesc1c34432010-12-22 17:23:54 -0800700 for (i = 0; i < MAX_NUMNODES; i++)
David Rientjesadc19382009-09-25 15:20:09 -0700701 if (physnodes[i].start != physnodes[i].end)
702 node_set(i, physnode_mask);
703
704 /*
705 * Continue to fill physical nodes with fake nodes until there is no
706 * memory left on any of them.
707 */
708 while (nodes_weight(physnode_mask)) {
709 for_each_node_mask(i, physnode_mask) {
710 u64 end = physnodes[i].start + size;
711 u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
712
713 if (ret < big)
714 end += FAKE_NODE_MIN_SIZE;
715
716 /*
717 * Continue to add memory to this fake node if its
718 * non-reserved memory is less than the per-node size.
719 */
720 while (end - physnodes[i].start -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700721 memblock_x86_hole_size(physnodes[i].start, end) < size) {
David Rientjesadc19382009-09-25 15:20:09 -0700722 end += FAKE_NODE_MIN_SIZE;
723 if (end > physnodes[i].end) {
724 end = physnodes[i].end;
725 break;
726 }
727 }
728
729 /*
730 * If there won't be at least FAKE_NODE_MIN_SIZE of
731 * non-reserved memory in ZONE_DMA32 for the next node,
732 * this one must extend to the boundary.
733 */
734 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700735 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjesadc19382009-09-25 15:20:09 -0700736 end = dma32_end;
737
738 /*
739 * If there won't be enough non-reserved memory for the
740 * next node, this one must extend to the end of the
741 * physical node.
742 */
743 if (physnodes[i].end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700744 memblock_x86_hole_size(end, physnodes[i].end) < size)
David Rientjesadc19382009-09-25 15:20:09 -0700745 end = physnodes[i].end;
746
747 /*
748 * Avoid allocating more nodes than requested, which can
749 * happen as a result of rounding down each node's size
750 * to FAKE_NODE_MIN_SIZE.
751 */
752 if (nodes_weight(physnode_mask) + ret >= nr_nodes)
753 end = physnodes[i].end;
754
755 if (setup_node_range(ret++, &physnodes[i].start,
756 end - physnodes[i].start,
757 physnodes[i].end) < 0)
758 node_clear(i, physnode_mask);
759 }
760 }
761 return ret;
762}
763
764/*
David Rientjes8df5bb32010-02-15 13:43:30 -0800765 * Returns the end address of a node so that there is at least `size' amount of
766 * non-reserved memory or `max_addr' is reached.
767 */
768static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
769{
770 u64 end = start + size;
771
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700772 while (end - start - memblock_x86_hole_size(start, end) < size) {
David Rientjes8df5bb32010-02-15 13:43:30 -0800773 end += FAKE_NODE_MIN_SIZE;
774 if (end > max_addr) {
775 end = max_addr;
776 break;
777 }
778 }
779 return end;
780}
781
782/*
783 * Sets up fake nodes of `size' interleaved over physical nodes ranging from
784 * `addr' to `max_addr'. The return value is the number of nodes allocated.
785 */
786static int __init split_nodes_size_interleave(u64 addr, u64 max_addr, u64 size)
787{
788 nodemask_t physnode_mask = NODE_MASK_NONE;
789 u64 min_size;
790 int ret = 0;
791 int i;
792
793 if (!size)
794 return -1;
795 /*
796 * The limit on emulated nodes is MAX_NUMNODES, so the size per node is
797 * increased accordingly if the requested size is too small. This
798 * creates a uniform distribution of node sizes across the entire
799 * machine (but not necessarily over physical nodes).
800 */
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700801 min_size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) /
David Rientjes8df5bb32010-02-15 13:43:30 -0800802 MAX_NUMNODES;
803 min_size = max(min_size, FAKE_NODE_MIN_SIZE);
804 if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size)
805 min_size = (min_size + FAKE_NODE_MIN_SIZE) &
806 FAKE_NODE_MIN_HASH_MASK;
807 if (size < min_size) {
808 pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
809 size >> 20, min_size >> 20);
810 size = min_size;
811 }
812 size &= FAKE_NODE_MIN_HASH_MASK;
813
814 for (i = 0; i < MAX_NUMNODES; i++)
815 if (physnodes[i].start != physnodes[i].end)
816 node_set(i, physnode_mask);
817 /*
818 * Fill physical nodes with fake nodes of size until there is no memory
819 * left on any of them.
820 */
821 while (nodes_weight(physnode_mask)) {
822 for_each_node_mask(i, physnode_mask) {
823 u64 dma32_end = MAX_DMA32_PFN << PAGE_SHIFT;
824 u64 end;
825
826 end = find_end_of_node(physnodes[i].start,
827 physnodes[i].end, size);
828 /*
829 * If there won't be at least FAKE_NODE_MIN_SIZE of
830 * non-reserved memory in ZONE_DMA32 for the next node,
831 * this one must extend to the boundary.
832 */
833 if (end < dma32_end && dma32_end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700834 memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
David Rientjes8df5bb32010-02-15 13:43:30 -0800835 end = dma32_end;
836
837 /*
838 * If there won't be enough non-reserved memory for the
839 * next node, this one must extend to the end of the
840 * physical node.
841 */
842 if (physnodes[i].end - end -
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700843 memblock_x86_hole_size(end, physnodes[i].end) < size)
David Rientjes8df5bb32010-02-15 13:43:30 -0800844 end = physnodes[i].end;
845
846 /*
847 * Setup the fake node that will be allocated as bootmem
848 * later. If setup_node_range() returns non-zero, there
849 * is no more memory available on this physical node.
850 */
851 if (setup_node_range(ret++, &physnodes[i].start,
852 end - physnodes[i].start,
853 physnodes[i].end) < 0)
854 node_clear(i, physnode_mask);
855 }
856 }
857 return ret;
858}
859
860/*
Thomas Gleixner886533a2008-05-12 15:43:36 +0200861 * Sets up the system RAM area from start_pfn to last_pfn according to the
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200862 * numa=fake command-line option.
863 */
David Rientjesadc19382009-09-25 15:20:09 -0700864static int __init numa_emulation(unsigned long start_pfn,
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200865 unsigned long last_pfn, int acpi, int amd)
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200866{
Tejun Heo97e7b782011-02-16 17:11:08 +0100867 static struct numa_meminfo ei __initdata;
David Rientjesca2107c2010-02-15 13:43:33 -0800868 u64 addr = start_pfn << PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200869 u64 max_addr = last_pfn << PAGE_SHIFT;
David Rientjesca2107c2010-02-15 13:43:33 -0800870 int num_nodes;
871 int i;
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200872
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200873 /*
David Rientjes8df5bb32010-02-15 13:43:30 -0800874 * If the numa=fake command-line contains a 'M' or 'G', it represents
David Rientjesca2107c2010-02-15 13:43:33 -0800875 * the fixed node size. Otherwise, if it is just a single number N,
876 * split the system RAM into N fake nodes.
David Rientjes8df5bb32010-02-15 13:43:30 -0800877 */
878 if (strchr(cmdline, 'M') || strchr(cmdline, 'G')) {
David Rientjesca2107c2010-02-15 13:43:33 -0800879 u64 size;
880
David Rientjes8df5bb32010-02-15 13:43:30 -0800881 size = memparse(cmdline, &cmdline);
882 num_nodes = split_nodes_size_interleave(addr, max_addr, size);
David Rientjesca2107c2010-02-15 13:43:33 -0800883 } else {
884 unsigned long n;
885
886 n = simple_strtoul(cmdline, NULL, 0);
David Rientjesc1c34432010-12-22 17:23:54 -0800887 num_nodes = split_nodes_interleave(addr, max_addr, n);
David Rientjes8df5bb32010-02-15 13:43:30 -0800888 }
889
David Rientjesca2107c2010-02-15 13:43:33 -0800890 if (num_nodes < 0)
891 return num_nodes;
Tejun Heo8968dab2011-02-16 17:11:08 +0100892
Tejun Heo97e7b782011-02-16 17:11:08 +0100893 ei.nr_blks = num_nodes;
894 for (i = 0; i < ei.nr_blks; i++) {
895 ei.blk[i].start = nodes[i].start;
896 ei.blk[i].end = nodes[i].end;
897 ei.blk[i].nid = i;
898 }
Tejun Heo8968dab2011-02-16 17:11:08 +0100899
Tejun Heo97e7b782011-02-16 17:11:08 +0100900 memnode_shift = compute_hash_shift(&ei);
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200901 if (memnode_shift < 0) {
902 memnode_shift = 0;
903 printk(KERN_ERR "No NUMA hash function found. NUMA emulation "
904 "disabled.\n");
905 return -1;
906 }
907
908 /*
David Rientjesadc19382009-09-25 15:20:09 -0700909 * We need to vacate all active ranges that may have been registered for
910 * the e820 memory map.
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200911 */
912 remove_all_active_ranges();
Yinghai Lu1411e0e2010-12-27 16:48:17 -0800913 for_each_node_mask(i, node_possible_map)
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700914 memblock_x86_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
Mel Gorman5cb248a2006-09-27 01:49:52 -0700915 nodes[i].end >> PAGE_SHIFT);
Yinghai Lu1411e0e2010-12-27 16:48:17 -0800916 init_memory_mapping_high();
917 for_each_node_mask(i, node_possible_map)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100918 setup_node_bootmem(i, nodes[i].start, nodes[i].end);
Tejun Heo19095542011-02-16 12:13:07 +0100919 setup_physnodes(addr, max_addr);
David Rientjesf51bf302010-12-22 17:23:51 -0800920 fake_physnodes(acpi, amd, num_nodes);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100921 numa_init_array();
Tejun Heoac7136b2011-02-16 17:11:09 +0100922 numa_emu_dist = true;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100923 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
David Rientjes8b8ca80e2007-05-02 19:27:09 +0200925#endif /* CONFIG_NUMA_EMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Tejun Heoffe77a42011-02-16 12:13:06 +0100927static int dummy_numa_init(void)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100928{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 printk(KERN_INFO "%s\n",
930 numa_off ? "NUMA turned off" : "No NUMA configuration found");
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100931 printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
Tejun Heo86ef4db2011-02-16 12:13:06 +0100932 0LU, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100933
Tejun Heo92d4a432011-02-16 17:11:09 +0100934 node_set(0, numa_nodes_parsed);
Tejun Heo43a662f2011-02-16 17:11:08 +0100935 numa_add_memblk(0, 0, (u64)max_pfn << PAGE_SHIFT);
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100936
937 return 0;
938}
939
Tejun Heoffe77a42011-02-16 12:13:06 +0100940void __init initmem_init(void)
941{
942 int (*numa_init[])(void) = { [2] = dummy_numa_init };
Tejun Heoffe77a42011-02-16 12:13:06 +0100943 int i, j;
944
945 if (!numa_off) {
946#ifdef CONFIG_ACPI_NUMA
947 numa_init[0] = x86_acpi_numa_init;
Tejun Heoffe77a42011-02-16 12:13:06 +0100948#endif
949#ifdef CONFIG_AMD_NUMA
950 numa_init[1] = amd_numa_init;
Tejun Heoffe77a42011-02-16 12:13:06 +0100951#endif
952 }
953
954 for (i = 0; i < ARRAY_SIZE(numa_init); i++) {
955 if (!numa_init[i])
956 continue;
957
958 for (j = 0; j < MAX_LOCAL_APIC; j++)
959 set_apicid_to_node(j, NUMA_NO_NODE);
960
Tejun Heo92d4a432011-02-16 17:11:09 +0100961 nodes_clear(numa_nodes_parsed);
Tejun Heoffe77a42011-02-16 12:13:06 +0100962 nodes_clear(node_possible_map);
963 nodes_clear(node_online_map);
Tejun Heo97e7b782011-02-16 17:11:08 +0100964 memset(&numa_meminfo, 0, sizeof(numa_meminfo));
Tejun Heofd0435d2011-02-16 17:11:08 +0100965 remove_all_active_ranges();
Tejun Heoac7136b2011-02-16 17:11:09 +0100966 numa_reset_distance();
Tejun Heoffe77a42011-02-16 12:13:06 +0100967
968 if (numa_init[i]() < 0)
969 continue;
Tejun Heo206e4202011-02-16 12:13:07 +0100970
Tejun Heo56e827f2011-02-16 17:11:09 +0100971 if (numa_cleanup_meminfo(&numa_meminfo) < 0)
972 continue;
Tejun Heoffe77a42011-02-16 12:13:06 +0100973#ifdef CONFIG_NUMA_EMU
Tejun Heo19095542011-02-16 12:13:07 +0100974 setup_physnodes(0, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100975 if (cmdline && !numa_emulation(0, max_pfn, i == 0, i == 1))
976 return;
Tejun Heo19095542011-02-16 12:13:07 +0100977 setup_physnodes(0, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100978 nodes_clear(node_possible_map);
979 nodes_clear(node_online_map);
980#endif
Tejun Heof9c60252011-02-16 17:11:09 +0100981 if (numa_register_memblks(&numa_meminfo) < 0)
Tejun Heo43a662f2011-02-16 17:11:08 +0100982 continue;
983
Tejun Heofd0435d2011-02-16 17:11:08 +0100984 for (j = 0; j < nr_cpu_ids; j++) {
985 int nid = early_cpu_to_node(j);
986
987 if (nid == NUMA_NO_NODE)
988 continue;
989 if (!node_online(nid))
990 numa_clear_node(j);
991 }
992 numa_init_array();
993 return;
Tejun Heoffe77a42011-02-16 12:13:06 +0100994 }
995 BUG();
Andi Kleen69d81fc2005-11-05 17:25:53 +0100996}
997
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100998unsigned long __init numa_free_all_bootmem(void)
999{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 unsigned long pages = 0;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +01001001 int i;
1002
1003 for_each_online_node(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 pages += free_all_bootmem_node(NODE_DATA(i));
Thomas Gleixnere3cfe522008-01-30 13:30:37 +01001005
Yinghai Lu08677212010-02-10 01:20:20 -08001006 pages += free_all_memory_core_early(MAX_NUMNODES);
Yinghai Lu08677212010-02-10 01:20:20 -08001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return pages;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +01001009}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Tejun Heobbc9e2f2011-01-23 14:37:39 +01001011int __cpuinit numa_cpu_node(int cpu)
Yinghai Lud9c2d5a2009-11-21 00:23:37 -08001012{
Tejun Heobbc9e2f2011-01-23 14:37:39 +01001013 int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
Yinghai Lud9c2d5a2009-11-21 00:23:37 -08001014
Tejun Heobbc9e2f2011-01-23 14:37:39 +01001015 if (apicid != BAD_APICID)
1016 return __apicid_to_node[apicid];
1017 return NUMA_NO_NODE;
Yinghai Lud9c2d5a2009-11-21 00:23:37 -08001018}
1019
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +01001020/*
Tejun Heode2d9442011-01-23 14:37:41 +01001021 * UGLINESS AHEAD: Currently, CONFIG_NUMA_EMU is 64bit only and makes use
1022 * of 64bit specific data structures. The distinction is artificial and
1023 * should be removed. numa_{add|remove}_cpu() are implemented in numa.c
1024 * for both 32 and 64bit when CONFIG_NUMA_EMU is disabled but here when
1025 * enabled.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +01001026 *
Tejun Heode2d9442011-01-23 14:37:41 +01001027 * NUMA emulation is planned to be made generic and the following and other
1028 * related code should be moved to numa.c.
Ravikiran Thirumalai05b3cbd2006-01-11 22:45:36 +01001029 */
Tejun Heode2d9442011-01-23 14:37:41 +01001030#ifdef CONFIG_NUMA_EMU
1031# ifndef CONFIG_DEBUG_PER_CPU_MAPS
David Rientjesc1c34432010-12-22 17:23:54 -08001032void __cpuinit numa_add_cpu(int cpu)
1033{
1034 unsigned long addr;
Tejun Heobbc9e2f2011-01-23 14:37:39 +01001035 int physnid, nid;
David Rientjesc1c34432010-12-22 17:23:54 -08001036
Tejun Heobbc9e2f2011-01-23 14:37:39 +01001037 nid = numa_cpu_node(cpu);
David Rientjesc1c34432010-12-22 17:23:54 -08001038 if (nid == NUMA_NO_NODE)
1039 nid = early_cpu_to_node(cpu);
1040 BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
1041
1042 /*
1043 * Use the starting address of the emulated node to find which physical
1044 * node it is allocated on.
1045 */
1046 addr = node_start_pfn(nid) << PAGE_SHIFT;
1047 for (physnid = 0; physnid < MAX_NUMNODES; physnid++)
1048 if (addr >= physnodes[physnid].start &&
1049 addr < physnodes[physnid].end)
1050 break;
1051
1052 /*
1053 * Map the cpu to each emulated node that is allocated on the physical
1054 * node of the cpu's apic id.
1055 */
1056 for_each_online_node(nid) {
1057 addr = node_start_pfn(nid) << PAGE_SHIFT;
1058 if (addr >= physnodes[physnid].start &&
1059 addr < physnodes[physnid].end)
1060 cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
1061 }
1062}
1063
1064void __cpuinit numa_remove_cpu(int cpu)
1065{
1066 int i;
1067
1068 for_each_online_node(i)
1069 cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
1070}
Tejun Heode2d9442011-01-23 14:37:41 +01001071# else /* !CONFIG_DEBUG_PER_CPU_MAPS */
David Rientjesd906f0e2010-12-30 10:54:16 -08001072static void __cpuinit numa_set_cpumask(int cpu, int enable)
1073{
1074 int node = early_cpu_to_node(cpu);
1075 struct cpumask *mask;
David Rientjesc1c34432010-12-22 17:23:54 -08001076 int i;
Brian Gerst6470aff2009-01-27 12:56:47 +09001077
David Rientjes14392fd2011-02-07 14:08:53 -08001078 if (node == NUMA_NO_NODE) {
1079 /* early_cpu_to_node() already emits a warning and trace */
1080 return;
1081 }
David Rientjesc1c34432010-12-22 17:23:54 -08001082 for_each_online_node(i) {
1083 unsigned long addr;
1084
1085 addr = node_start_pfn(i) << PAGE_SHIFT;
1086 if (addr < physnodes[node].start ||
1087 addr >= physnodes[node].end)
1088 continue;
David Rientjesd906f0e2010-12-30 10:54:16 -08001089 mask = debug_cpumask_set_cpu(cpu, enable);
1090 if (!mask)
David Rientjesc1c34432010-12-22 17:23:54 -08001091 return;
David Rientjesc1c34432010-12-22 17:23:54 -08001092
1093 if (enable)
1094 cpumask_set_cpu(cpu, mask);
1095 else
1096 cpumask_clear_cpu(cpu, mask);
Brian Gerst6470aff2009-01-27 12:56:47 +09001097 }
Brian Gerst6470aff2009-01-27 12:56:47 +09001098}
1099
1100void __cpuinit numa_add_cpu(int cpu)
1101{
1102 numa_set_cpumask(cpu, 1);
1103}
1104
1105void __cpuinit numa_remove_cpu(int cpu)
1106{
1107 numa_set_cpumask(cpu, 0);
1108}
Tejun Heode2d9442011-01-23 14:37:41 +01001109# endif /* !CONFIG_DEBUG_PER_CPU_MAPS */
1110#endif /* CONFIG_NUMA_EMU */