blob: 541746fdeb4be8ec3903dfec353e06bf7ab70210 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/acpi.h>
Andreas Herrmann23ac4ae2010-09-17 18:03:43 +020022#include <asm/amd_nb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Tejun Heob8ef9172011-02-22 11:10:08 +010024#include "numa_internal.h"
Tejun Heo97e7b782011-02-16 17:11:08 +010025
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -070026struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010027EXPORT_SYMBOL(node_data);
28
Tejun Heo92d4a432011-02-16 17:11:09 +010029nodemask_t numa_nodes_parsed __initdata;
Tejun Heoec8cf29b2011-02-16 12:13:07 +010030
Eric Dumazetdcf36bf2006-03-25 16:31:46 +010031struct memnode memnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Thomas Gleixner864fc312008-05-12 15:43:36 +020033static unsigned long __initdata nodemap_addr;
34static unsigned long __initdata nodemap_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Tejun Heo97e7b782011-02-16 17:11:08 +010036static struct numa_meminfo numa_meminfo __initdata;
Tejun Heoef396ec2011-02-16 17:11:07 +010037
Tejun Heoac7136b2011-02-16 17:11:09 +010038static int numa_distance_cnt;
39static u8 *numa_distance;
40
Brian Gerst6470aff2009-01-27 12:56:47 +090041/*
Eric Dumazet529a3402005-11-05 17:25:54 +010042 * Given a shift value, try to populate memnodemap[]
43 * Returns :
44 * 1 if OK
45 * 0 if memnodmap[] too small (of shift too small)
46 * -1 if node overlap or lost ram (shift too big)
47 */
Tejun Heo97e7b782011-02-16 17:11:08 +010048static int __init populate_memnodemap(const struct numa_meminfo *mi, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Eric Dumazet529a3402005-11-05 17:25:54 +010050 unsigned long addr, end;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010051 int i, res = -1;
Keith Manntheyb6846642005-07-28 21:15:38 -070052
travis@sgi.com43238382008-01-30 13:33:25 +010053 memset(memnodemap, 0xff, sizeof(s16)*memnodemapsize);
Tejun Heo97e7b782011-02-16 17:11:08 +010054 for (i = 0; i < mi->nr_blks; i++) {
55 addr = mi->blk[i].start;
56 end = mi->blk[i].end;
Eric Dumazet529a3402005-11-05 17:25:54 +010057 if (addr >= end)
58 continue;
Amul Shah076422d2007-02-13 13:26:19 +010059 if ((end >> shift) >= memnodemapsize)
Eric Dumazet529a3402005-11-05 17:25:54 +010060 return 0;
61 do {
travis@sgi.com43238382008-01-30 13:33:25 +010062 if (memnodemap[addr >> shift] != NUMA_NO_NODE)
Eric Dumazet529a3402005-11-05 17:25:54 +010063 return -1;
Tejun Heo97e7b782011-02-16 17:11:08 +010064 memnodemap[addr >> shift] = mi->blk[i].nid;
Amul Shah076422d2007-02-13 13:26:19 +010065 addr += (1UL << shift);
Eric Dumazet529a3402005-11-05 17:25:54 +010066 } while (addr < end);
67 res = 1;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010068 }
Eric Dumazet529a3402005-11-05 17:25:54 +010069 return res;
70}
71
Amul Shah076422d2007-02-13 13:26:19 +010072static int __init allocate_cachealigned_memnodemap(void)
73{
Yinghai Lu24a5da72008-02-01 17:49:41 +010074 unsigned long addr;
Amul Shah076422d2007-02-13 13:26:19 +010075
76 memnodemap = memnode.embedded_map;
travis@sgi.com316390b2008-01-30 13:33:15 +010077 if (memnodemapsize <= ARRAY_SIZE(memnode.embedded_map))
Amul Shah076422d2007-02-13 13:26:19 +010078 return 0;
Amul Shah076422d2007-02-13 13:26:19 +010079
Yinghai Lu24a5da72008-02-01 17:49:41 +010080 addr = 0x8000;
Joerg Roedelbe3e89e2008-07-25 16:48:58 +020081 nodemap_size = roundup(sizeof(s16) * memnodemapsize, L1_CACHE_BYTES);
Yinghai Ludbef7b52010-12-27 16:48:08 -080082 nodemap_addr = memblock_find_in_range(addr, get_max_mapped(),
Yinghai Lu24a5da72008-02-01 17:49:41 +010083 nodemap_size, L1_CACHE_BYTES);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070084 if (nodemap_addr == MEMBLOCK_ERROR) {
Amul Shah076422d2007-02-13 13:26:19 +010085 printk(KERN_ERR
86 "NUMA: Unable to allocate Memory to Node hash map\n");
87 nodemap_addr = nodemap_size = 0;
88 return -1;
89 }
Yinghai Lu24a5da72008-02-01 17:49:41 +010090 memnodemap = phys_to_virt(nodemap_addr);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070091 memblock_x86_reserve_range(nodemap_addr, nodemap_addr + nodemap_size, "MEMNODEMAP");
Amul Shah076422d2007-02-13 13:26:19 +010092
93 printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n",
94 nodemap_addr, nodemap_addr + nodemap_size);
95 return 0;
96}
97
98/*
99 * The LSB of all start and end addresses in the node map is the value of the
100 * maximum possible shift.
101 */
Tejun Heo97e7b782011-02-16 17:11:08 +0100102static int __init extract_lsb_from_nodes(const struct numa_meminfo *mi)
Amul Shah076422d2007-02-13 13:26:19 +0100103{
Amul Shah54413922007-02-13 13:26:20 +0100104 int i, nodes_used = 0;
Amul Shah076422d2007-02-13 13:26:19 +0100105 unsigned long start, end;
106 unsigned long bitfield = 0, memtop = 0;
107
Tejun Heo97e7b782011-02-16 17:11:08 +0100108 for (i = 0; i < mi->nr_blks; i++) {
109 start = mi->blk[i].start;
110 end = mi->blk[i].end;
Amul Shah076422d2007-02-13 13:26:19 +0100111 if (start >= end)
112 continue;
Amul Shah54413922007-02-13 13:26:20 +0100113 bitfield |= start;
114 nodes_used++;
Amul Shah076422d2007-02-13 13:26:19 +0100115 if (end > memtop)
116 memtop = end;
117 }
Amul Shah54413922007-02-13 13:26:20 +0100118 if (nodes_used <= 1)
119 i = 63;
120 else
121 i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
Amul Shah076422d2007-02-13 13:26:19 +0100122 memnodemapsize = (memtop >> i)+1;
123 return i;
124}
125
Tejun Heo97e7b782011-02-16 17:11:08 +0100126static int __init compute_hash_shift(const struct numa_meminfo *mi)
Eric Dumazet529a3402005-11-05 17:25:54 +0100127{
Amul Shah076422d2007-02-13 13:26:19 +0100128 int shift;
Eric Dumazet529a3402005-11-05 17:25:54 +0100129
Tejun Heo97e7b782011-02-16 17:11:08 +0100130 shift = extract_lsb_from_nodes(mi);
Amul Shah076422d2007-02-13 13:26:19 +0100131 if (allocate_cachealigned_memnodemap())
132 return -1;
Andi Kleen6b050f82006-01-11 22:44:33 +0100133 printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
Eric Dumazet529a3402005-11-05 17:25:54 +0100134 shift);
135
Tejun Heo97e7b782011-02-16 17:11:08 +0100136 if (populate_memnodemap(mi, shift) != 1) {
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100137 printk(KERN_INFO "Your memory is not aligned you need to "
138 "rebuild your kernel with a bigger NODEMAPSIZE "
139 "shift=%d\n", shift);
Eric Dumazet529a3402005-11-05 17:25:54 +0100140 return -1;
141 }
Keith Manntheyb6846642005-07-28 21:15:38 -0700142 return shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
KAMEZAWA Hiroyukif2dbcfa2009-02-18 14:48:32 -0800145int __meminit __early_pfn_to_nid(unsigned long pfn)
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700146{
147 return phys_to_nid(pfn << PAGE_SHIFT);
148}
Matt Tolentinobbfceef2005-06-23 00:08:07 -0700149
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100150static void * __init early_node_mem(int nodeid, unsigned long start,
Yinghai Lu24a5da72008-02-01 17:49:41 +0100151 unsigned long end, unsigned long size,
152 unsigned long align)
Andi Kleena8062232006-04-07 19:49:21 +0200153{
Yinghai Lucef625e2010-02-10 01:20:18 -0800154 unsigned long mem;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100155
Yinghai Lucef625e2010-02-10 01:20:18 -0800156 /*
157 * put it on high as possible
158 * something will go with NODE_DATA
159 */
160 if (start < (MAX_DMA_PFN<<PAGE_SHIFT))
161 start = MAX_DMA_PFN<<PAGE_SHIFT;
162 if (start < (MAX_DMA32_PFN<<PAGE_SHIFT) &&
163 end > (MAX_DMA32_PFN<<PAGE_SHIFT))
164 start = MAX_DMA32_PFN<<PAGE_SHIFT;
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700165 mem = memblock_x86_find_in_range_node(nodeid, start, end, size, align);
166 if (mem != MEMBLOCK_ERROR)
Andi Kleena8062232006-04-07 19:49:21 +0200167 return __va(mem);
Yinghai Lu9347e0b2008-02-01 17:49:42 +0100168
Yinghai Lucef625e2010-02-10 01:20:18 -0800169 /* extend the search scope */
170 end = max_pfn_mapped << PAGE_SHIFT;
Yinghai Lu419db272010-10-28 09:50:17 -0700171 start = MAX_DMA_PFN << PAGE_SHIFT;
172 mem = memblock_find_in_range(start, end, size, align);
Yinghai Lu72d7c3b2010-08-25 13:39:17 -0700173 if (mem != MEMBLOCK_ERROR)
Yinghai Lu1842f902010-02-10 01:20:15 -0800174 return __va(mem);
175
176 printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100177 size, nodeid);
Yinghai Lu1842f902010-02-10 01:20:15 -0800178
179 return NULL;
Andi Kleena8062232006-04-07 19:49:21 +0200180}
181
Tejun Heod9c515e2011-02-16 17:11:10 +0100182static int __init numa_add_memblk_to(int nid, u64 start, u64 end,
183 struct numa_meminfo *mi)
Tejun Heoef396ec2011-02-16 17:11:07 +0100184{
Tejun Heo56e827f2011-02-16 17:11:09 +0100185 /* ignore zero length blks */
186 if (start == end)
187 return 0;
188
189 /* whine about and ignore invalid blks */
190 if (start > end || nid < 0 || nid >= MAX_NUMNODES) {
191 pr_warning("NUMA: Warning: invalid memblk node %d (%Lx-%Lx)\n",
192 nid, start, end);
193 return 0;
194 }
195
196 if (mi->nr_blks >= NR_NODE_MEMBLKS) {
197 pr_err("NUMA: too many memblk ranges\n");
Tejun Heoef396ec2011-02-16 17:11:07 +0100198 return -EINVAL;
199 }
200
Tejun Heo97e7b782011-02-16 17:11:08 +0100201 mi->blk[mi->nr_blks].start = start;
202 mi->blk[mi->nr_blks].end = end;
203 mi->blk[mi->nr_blks].nid = nid;
204 mi->nr_blks++;
Tejun Heoef396ec2011-02-16 17:11:07 +0100205 return 0;
206}
207
Tejun Heo90e6b672011-02-22 11:10:08 +0100208/**
209 * numa_remove_memblk_from - Remove one numa_memblk from a numa_meminfo
210 * @idx: Index of memblk to remove
211 * @mi: numa_meminfo to remove memblk from
212 *
213 * Remove @idx'th numa_memblk from @mi by shifting @mi->blk[] and
214 * decrementing @mi->nr_blks.
215 */
Tejun Heob8ef9172011-02-22 11:10:08 +0100216void __init numa_remove_memblk_from(int idx, struct numa_meminfo *mi)
Tejun Heo2e756be2011-02-16 17:11:09 +0100217{
218 mi->nr_blks--;
219 memmove(&mi->blk[idx], &mi->blk[idx + 1],
220 (mi->nr_blks - idx) * sizeof(mi->blk[0]));
221}
222
Tejun Heo90e6b672011-02-22 11:10:08 +0100223/**
224 * numa_add_memblk - Add one numa_memblk to numa_meminfo
225 * @nid: NUMA node ID of the new memblk
226 * @start: Start address of the new memblk
227 * @end: End address of the new memblk
228 *
229 * Add a new memblk to the default numa_meminfo.
230 *
231 * RETURNS:
232 * 0 on success, -errno on failure.
233 */
Tejun Heod9c515e2011-02-16 17:11:10 +0100234int __init numa_add_memblk(int nid, u64 start, u64 end)
235{
236 return numa_add_memblk_to(nid, start, end, &numa_meminfo);
237}
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239/* Initialize bootmem allocator for a node */
Yinghai Lu7c437692009-05-15 13:59:37 -0700240void __init
241setup_node_bootmem(int nodeid, unsigned long start, unsigned long end)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100242{
Yinghai Lu08677212010-02-10 01:20:20 -0800243 unsigned long start_pfn, last_pfn, nodedata_phys;
Yinghai Lu7c437692009-05-15 13:59:37 -0700244 const int pgdat_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
Yinghai Lu1a27fc02008-03-18 12:52:37 -0700245 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Yinghai Lu4c31e922009-04-22 14:19:27 -0700247 if (!end)
248 return;
249
Yinghai Lu7c437692009-05-15 13:59:37 -0700250 /*
251 * Don't confuse VM with a node that doesn't have the
252 * minimum amount of memory:
253 */
254 if (end && (end - start) < NODE_MIN_SIZE)
255 return;
256
Joerg Roedelbe3e89e2008-07-25 16:48:58 +0200257 start = roundup(start, ZONE_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Yinghai Lu08677212010-02-10 01:20:20 -0800259 printk(KERN_INFO "Initmem setup node %d %016lx-%016lx\n", nodeid,
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100260 start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 start_pfn = start >> PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200263 last_pfn = end >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Yinghai Lu24a5da72008-02-01 17:49:41 +0100265 node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
266 SMP_CACHE_BYTES);
Andi Kleena8062232006-04-07 19:49:21 +0200267 if (node_data[nodeid] == NULL)
268 return;
269 nodedata_phys = __pa(node_data[nodeid]);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700270 memblock_x86_reserve_range(nodedata_phys, nodedata_phys + pgdat_size, "NODE_DATA");
Yinghai Lu6118f762008-02-04 16:47:56 +0100271 printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
272 nodedata_phys + pgdat_size - 1);
Yinghai Lu1842f902010-02-10 01:20:15 -0800273 nid = phys_to_nid(nodedata_phys);
274 if (nid != nodeid)
275 printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nodeid, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
Yinghai Lu08677212010-02-10 01:20:20 -0800278 NODE_DATA(nodeid)->node_id = nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 NODE_DATA(nodeid)->node_start_pfn = start_pfn;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200280 NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 node_set_online(nodeid);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100283}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Tejun Heo90e6b672011-02-22 11:10:08 +0100285/**
286 * numa_cleanup_meminfo - Cleanup a numa_meminfo
287 * @mi: numa_meminfo to clean up
288 *
289 * Sanitize @mi by merging and removing unncessary memblks. Also check for
290 * conflicts and clear unused memblks.
291 *
292 * RETURNS:
293 * 0 on success, -errno on failure.
294 */
Tejun Heob8ef9172011-02-22 11:10:08 +0100295int __init numa_cleanup_meminfo(struct numa_meminfo *mi)
Tejun Heofd0435d2011-02-16 17:11:08 +0100296{
Tejun Heo56e827f2011-02-16 17:11:09 +0100297 const u64 low = 0;
298 const u64 high = (u64)max_pfn << PAGE_SHIFT;
Tejun Heo2e756be2011-02-16 17:11:09 +0100299 int i, j, k;
Tejun Heoef396ec2011-02-16 17:11:07 +0100300
Tejun Heo2e756be2011-02-16 17:11:09 +0100301 for (i = 0; i < mi->nr_blks; i++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100302 struct numa_memblk *bi = &mi->blk[i];
Tejun Heoef396ec2011-02-16 17:11:07 +0100303
Tejun Heo56e827f2011-02-16 17:11:09 +0100304 /* make sure all blocks are inside the limits */
305 bi->start = max(bi->start, low);
306 bi->end = min(bi->end, high);
307
308 /* and there's no empty block */
309 if (bi->start == bi->end) {
310 numa_remove_memblk_from(i--, mi);
311 continue;
312 }
313
Tejun Heo2e756be2011-02-16 17:11:09 +0100314 for (j = i + 1; j < mi->nr_blks; j++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100315 struct numa_memblk *bj = &mi->blk[j];
Tejun Heoef396ec2011-02-16 17:11:07 +0100316 unsigned long start, end;
317
Tejun Heo2e756be2011-02-16 17:11:09 +0100318 /*
Tejun Heo56e827f2011-02-16 17:11:09 +0100319 * See whether there are overlapping blocks. Whine
320 * about but allow overlaps of the same nid. They
321 * will be merged below.
322 */
323 if (bi->end > bj->start && bi->start < bj->end) {
324 if (bi->nid != bj->nid) {
325 pr_err("NUMA: node %d (%Lx-%Lx) overlaps with node %d (%Lx-%Lx)\n",
326 bi->nid, bi->start, bi->end,
327 bj->nid, bj->start, bj->end);
328 return -EINVAL;
329 }
330 pr_warning("NUMA: Warning: node %d (%Lx-%Lx) overlaps with itself (%Lx-%Lx)\n",
331 bi->nid, bi->start, bi->end,
332 bj->start, bj->end);
333 }
334
335 /*
Tejun Heo2e756be2011-02-16 17:11:09 +0100336 * Join together blocks on the same node, holes
337 * between which don't overlap with memory on other
338 * nodes.
339 */
Tejun Heo97e7b782011-02-16 17:11:08 +0100340 if (bi->nid != bj->nid)
Tejun Heoef396ec2011-02-16 17:11:07 +0100341 continue;
Tejun Heo56e827f2011-02-16 17:11:09 +0100342 start = max(min(bi->start, bj->start), low);
343 end = min(max(bi->end, bj->end), high);
Tejun Heo2e756be2011-02-16 17:11:09 +0100344 for (k = 0; k < mi->nr_blks; k++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100345 struct numa_memblk *bk = &mi->blk[k];
346
347 if (bi->nid == bk->nid)
Tejun Heoef396ec2011-02-16 17:11:07 +0100348 continue;
Tejun Heo97e7b782011-02-16 17:11:08 +0100349 if (start < bk->end && end > bk->start)
Tejun Heoef396ec2011-02-16 17:11:07 +0100350 break;
351 }
Tejun Heo97e7b782011-02-16 17:11:08 +0100352 if (k < mi->nr_blks)
Tejun Heoef396ec2011-02-16 17:11:07 +0100353 continue;
Tejun Heoef396ec2011-02-16 17:11:07 +0100354 printk(KERN_INFO "NUMA: Node %d [%Lx,%Lx) + [%Lx,%Lx) -> [%lx,%lx)\n",
Tejun Heo97e7b782011-02-16 17:11:08 +0100355 bi->nid, bi->start, bi->end, bj->start, bj->end,
Tejun Heoef396ec2011-02-16 17:11:07 +0100356 start, end);
Tejun Heo97e7b782011-02-16 17:11:08 +0100357 bi->start = start;
358 bi->end = end;
Tejun Heo2e756be2011-02-16 17:11:09 +0100359 numa_remove_memblk_from(j--, mi);
Tejun Heoef396ec2011-02-16 17:11:07 +0100360 }
361 }
362
Tejun Heo56e827f2011-02-16 17:11:09 +0100363 for (i = mi->nr_blks; i < ARRAY_SIZE(mi->blk); i++) {
364 mi->blk[i].start = mi->blk[i].end = 0;
365 mi->blk[i].nid = NUMA_NO_NODE;
366 }
367
Tejun Heof9c60252011-02-16 17:11:09 +0100368 return 0;
369}
370
371/*
Tejun Heo4697bdc2011-02-16 17:11:09 +0100372 * Set nodes, which have memory in @mi, in *@nodemask.
373 */
374static void __init numa_nodemask_from_meminfo(nodemask_t *nodemask,
375 const struct numa_meminfo *mi)
376{
377 int i;
378
379 for (i = 0; i < ARRAY_SIZE(mi->blk); i++)
380 if (mi->blk[i].start != mi->blk[i].end &&
381 mi->blk[i].nid != NUMA_NO_NODE)
382 node_set(mi->blk[i].nid, *nodemask);
383}
384
Tejun Heo90e6b672011-02-22 11:10:08 +0100385/**
386 * numa_reset_distance - Reset NUMA distance table
387 *
388 * The current table is freed. The next numa_set_distance() call will
389 * create a new one.
Tejun Heoac7136b2011-02-16 17:11:09 +0100390 */
Tejun Heob8ef9172011-02-22 11:10:08 +0100391void __init numa_reset_distance(void)
Tejun Heoac7136b2011-02-16 17:11:09 +0100392{
Yinghai Luce003332011-03-02 11:22:14 +0100393 size_t size = numa_distance_cnt * numa_distance_cnt * sizeof(numa_distance[0]);
Tejun Heoac7136b2011-02-16 17:11:09 +0100394
Yinghai Luce003332011-03-02 11:22:14 +0100395 if (numa_distance_cnt)
Yinghai Lu2ca230b2011-02-17 14:46:37 +0100396 memblock_x86_free_range(__pa(numa_distance),
397 __pa(numa_distance) + size);
Yinghai Luce003332011-03-02 11:22:14 +0100398 numa_distance_cnt = 0;
Tejun Heoac7136b2011-02-16 17:11:09 +0100399 numa_distance = NULL;
Tejun Heoac7136b2011-02-16 17:11:09 +0100400}
401
Yinghai Lu2bf50552011-02-22 11:18:49 +0100402static int __init numa_alloc_distance(void)
403{
404 nodemask_t nodes_parsed;
405 size_t size;
406 int i, j, cnt = 0;
407 u64 phys;
408
409 /* size the new table and allocate it */
410 nodes_parsed = numa_nodes_parsed;
411 numa_nodemask_from_meminfo(&nodes_parsed, &numa_meminfo);
412
413 for_each_node_mask(i, nodes_parsed)
414 cnt = i;
David Rientjes1f565a82011-02-25 10:06:39 +0100415 cnt++;
416 size = cnt * cnt * sizeof(numa_distance[0]);
Yinghai Lu2bf50552011-02-22 11:18:49 +0100417
418 phys = memblock_find_in_range(0, (u64)max_pfn_mapped << PAGE_SHIFT,
419 size, PAGE_SIZE);
420 if (phys == MEMBLOCK_ERROR) {
421 pr_warning("NUMA: Warning: can't allocate distance table!\n");
422 /* don't retry until explicitly reset */
423 numa_distance = (void *)1LU;
424 return -ENOMEM;
425 }
426 memblock_x86_reserve_range(phys, phys + size, "NUMA DIST");
427
428 numa_distance = __va(phys);
429 numa_distance_cnt = cnt;
430
431 /* fill with the default distances */
432 for (i = 0; i < cnt; i++)
433 for (j = 0; j < cnt; j++)
434 numa_distance[i * cnt + j] = i == j ?
435 LOCAL_DISTANCE : REMOTE_DISTANCE;
436 printk(KERN_DEBUG "NUMA: Initialized distance table, cnt=%d\n", cnt);
437
438 return 0;
439}
440
Tejun Heo90e6b672011-02-22 11:10:08 +0100441/**
442 * numa_set_distance - Set NUMA distance from one NUMA to another
443 * @from: the 'from' node to set distance
444 * @to: the 'to' node to set distance
445 * @distance: NUMA distance
446 *
447 * Set the distance from node @from to @to to @distance. If distance table
448 * doesn't exist, one which is large enough to accomodate all the currently
449 * known nodes will be created.
Tejun Heoac7136b2011-02-16 17:11:09 +0100450 */
451void __init numa_set_distance(int from, int to, int distance)
452{
Yinghai Lu2bf50552011-02-22 11:18:49 +0100453 if (!numa_distance && numa_alloc_distance() < 0)
454 return;
Tejun Heoac7136b2011-02-16 17:11:09 +0100455
456 if (from >= numa_distance_cnt || to >= numa_distance_cnt) {
457 printk_once(KERN_DEBUG "NUMA: Debug: distance out of bound, from=%d to=%d distance=%d\n",
458 from, to, distance);
459 return;
460 }
461
462 if ((u8)distance != distance ||
463 (from == to && distance != LOCAL_DISTANCE)) {
464 pr_warn_once("NUMA: Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
465 from, to, distance);
466 return;
467 }
468
469 numa_distance[from * numa_distance_cnt + to] = distance;
470}
471
472int __node_distance(int from, int to)
473{
Tejun Heoac7136b2011-02-16 17:11:09 +0100474 if (from >= numa_distance_cnt || to >= numa_distance_cnt)
475 return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
476 return numa_distance[from * numa_distance_cnt + to];
477}
478EXPORT_SYMBOL(__node_distance);
479
480/*
Tejun Heof9c60252011-02-16 17:11:09 +0100481 * Sanity check to catch more bad NUMA configurations (they are amazingly
482 * common). Make sure the nodes cover all memory.
483 */
Tejun Heo91556232011-02-16 17:11:09 +0100484static bool __init numa_meminfo_cover_memory(const struct numa_meminfo *mi)
Tejun Heof9c60252011-02-16 17:11:09 +0100485{
486 unsigned long numaram, e820ram;
487 int i;
488
489 numaram = 0;
Tejun Heo91556232011-02-16 17:11:09 +0100490 for (i = 0; i < mi->nr_blks; i++) {
491 unsigned long s = mi->blk[i].start >> PAGE_SHIFT;
492 unsigned long e = mi->blk[i].end >> PAGE_SHIFT;
Tejun Heof9c60252011-02-16 17:11:09 +0100493 numaram += e - s;
Tejun Heo91556232011-02-16 17:11:09 +0100494 numaram -= __absent_pages_in_range(mi->blk[i].nid, s, e);
Tejun Heof9c60252011-02-16 17:11:09 +0100495 if ((long)numaram < 0)
496 numaram = 0;
497 }
498
499 e820ram = max_pfn - (memblock_x86_hole_size(0,
500 max_pfn << PAGE_SHIFT) >> PAGE_SHIFT);
501 /* We seem to lose 3 pages somewhere. Allow 1M of slack. */
502 if ((long)(e820ram - numaram) >= (1 << (20 - PAGE_SHIFT))) {
503 printk(KERN_ERR "NUMA: nodes only cover %luMB of your %luMB e820 RAM. Not used.\n",
504 (numaram << PAGE_SHIFT) >> 20,
505 (e820ram << PAGE_SHIFT) >> 20);
Tejun Heo91556232011-02-16 17:11:09 +0100506 return false;
Tejun Heof9c60252011-02-16 17:11:09 +0100507 }
Tejun Heo91556232011-02-16 17:11:09 +0100508 return true;
Tejun Heof9c60252011-02-16 17:11:09 +0100509}
510
511static int __init numa_register_memblks(struct numa_meminfo *mi)
512{
Yinghai Lu69efcc62011-02-21 10:58:13 +0100513 int i, nid;
Tejun Heof9c60252011-02-16 17:11:09 +0100514
515 /* Account for nodes with cpus and no memory */
Tejun Heo4697bdc2011-02-16 17:11:09 +0100516 node_possible_map = numa_nodes_parsed;
517 numa_nodemask_from_meminfo(&node_possible_map, mi);
Tejun Heof9c60252011-02-16 17:11:09 +0100518 if (WARN_ON(nodes_empty(node_possible_map)))
519 return -EINVAL;
520
Tejun Heo97e7b782011-02-16 17:11:08 +0100521 memnode_shift = compute_hash_shift(mi);
Tejun Heoef396ec2011-02-16 17:11:07 +0100522 if (memnode_shift < 0) {
523 printk(KERN_ERR "NUMA: No NUMA node hash function found. Contact maintainer\n");
524 return -EINVAL;
525 }
526
Tejun Heo97e7b782011-02-16 17:11:08 +0100527 for (i = 0; i < mi->nr_blks; i++)
528 memblock_x86_register_active_regions(mi->blk[i].nid,
529 mi->blk[i].start >> PAGE_SHIFT,
530 mi->blk[i].end >> PAGE_SHIFT);
Tejun Heofd0435d2011-02-16 17:11:08 +0100531
532 /* for out of order entries */
533 sort_node_map();
Tejun Heo91556232011-02-16 17:11:09 +0100534 if (!numa_meminfo_cover_memory(mi))
Tejun Heofd0435d2011-02-16 17:11:08 +0100535 return -EINVAL;
536
537 init_memory_mapping_high();
538
Yinghai Lu69efcc62011-02-21 10:58:13 +0100539 /* Finally register nodes. */
540 for_each_node_mask(nid, node_possible_map) {
541 u64 start = (u64)max_pfn << PAGE_SHIFT;
542 u64 end = 0;
Tejun Heo91556232011-02-16 17:11:09 +0100543
Yinghai Lu69efcc62011-02-21 10:58:13 +0100544 for (i = 0; i < mi->nr_blks; i++) {
545 if (nid != mi->blk[i].nid)
Tejun Heo91556232011-02-16 17:11:09 +0100546 continue;
Yinghai Lu69efcc62011-02-21 10:58:13 +0100547 start = min(mi->blk[i].start, start);
548 end = max(mi->blk[i].end, end);
Tejun Heo91556232011-02-16 17:11:09 +0100549 }
Yinghai Lu69efcc62011-02-21 10:58:13 +0100550
551 if (start < end)
552 setup_node_bootmem(nid, start, end);
Tejun Heo91556232011-02-16 17:11:09 +0100553 }
Tejun Heofd0435d2011-02-16 17:11:08 +0100554
Tejun Heoef396ec2011-02-16 17:11:07 +0100555 return 0;
556}
557
Yinghai Lu6d496f92011-02-17 14:53:20 +0100558static int __init dummy_numa_init(void)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100559{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 printk(KERN_INFO "%s\n",
561 numa_off ? "NUMA turned off" : "No NUMA configuration found");
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100562 printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
Tejun Heo86ef4db2011-02-16 12:13:06 +0100563 0LU, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100564
Tejun Heo92d4a432011-02-16 17:11:09 +0100565 node_set(0, numa_nodes_parsed);
Tejun Heo43a662f2011-02-16 17:11:08 +0100566 numa_add_memblk(0, 0, (u64)max_pfn << PAGE_SHIFT);
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100567
568 return 0;
569}
570
Tejun Heoffe77a42011-02-16 12:13:06 +0100571void __init initmem_init(void)
572{
573 int (*numa_init[])(void) = { [2] = dummy_numa_init };
Tejun Heoffe77a42011-02-16 12:13:06 +0100574 int i, j;
575
576 if (!numa_off) {
577#ifdef CONFIG_ACPI_NUMA
578 numa_init[0] = x86_acpi_numa_init;
Tejun Heoffe77a42011-02-16 12:13:06 +0100579#endif
580#ifdef CONFIG_AMD_NUMA
581 numa_init[1] = amd_numa_init;
Tejun Heoffe77a42011-02-16 12:13:06 +0100582#endif
583 }
584
585 for (i = 0; i < ARRAY_SIZE(numa_init); i++) {
586 if (!numa_init[i])
587 continue;
588
589 for (j = 0; j < MAX_LOCAL_APIC; j++)
590 set_apicid_to_node(j, NUMA_NO_NODE);
591
Tejun Heo92d4a432011-02-16 17:11:09 +0100592 nodes_clear(numa_nodes_parsed);
Tejun Heoffe77a42011-02-16 12:13:06 +0100593 nodes_clear(node_possible_map);
594 nodes_clear(node_online_map);
Tejun Heo97e7b782011-02-16 17:11:08 +0100595 memset(&numa_meminfo, 0, sizeof(numa_meminfo));
Tejun Heofd0435d2011-02-16 17:11:08 +0100596 remove_all_active_ranges();
Tejun Heoac7136b2011-02-16 17:11:09 +0100597 numa_reset_distance();
Tejun Heoffe77a42011-02-16 12:13:06 +0100598
599 if (numa_init[i]() < 0)
600 continue;
Tejun Heo206e4202011-02-16 12:13:07 +0100601
Tejun Heo56e827f2011-02-16 17:11:09 +0100602 if (numa_cleanup_meminfo(&numa_meminfo) < 0)
603 continue;
Tejun Heofbe99952011-02-22 11:10:08 +0100604
605 numa_emulation(&numa_meminfo, numa_distance_cnt);
606
Tejun Heof9c60252011-02-16 17:11:09 +0100607 if (numa_register_memblks(&numa_meminfo) < 0)
Tejun Heo43a662f2011-02-16 17:11:08 +0100608 continue;
609
Tejun Heofd0435d2011-02-16 17:11:08 +0100610 for (j = 0; j < nr_cpu_ids; j++) {
611 int nid = early_cpu_to_node(j);
612
613 if (nid == NUMA_NO_NODE)
614 continue;
615 if (!node_online(nid))
616 numa_clear_node(j);
617 }
618 numa_init_array();
619 return;
Tejun Heoffe77a42011-02-16 12:13:06 +0100620 }
621 BUG();
Andi Kleen69d81fc2005-11-05 17:25:53 +0100622}
623
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100624unsigned long __init numa_free_all_bootmem(void)
625{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 unsigned long pages = 0;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100627 int i;
628
629 for_each_online_node(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 pages += free_all_bootmem_node(NODE_DATA(i));
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100631
Yinghai Lu08677212010-02-10 01:20:20 -0800632 pages += free_all_memory_core_early(MAX_NUMNODES);
Yinghai Lu08677212010-02-10 01:20:20 -0800633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return pages;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100635}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100637int __cpuinit numa_cpu_node(int cpu)
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800638{
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100639 int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800640
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100641 if (apicid != BAD_APICID)
642 return __apicid_to_node[apicid];
643 return NUMA_NO_NODE;
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800644}