blob: 13f5b068e8c25242cc299883713e3bdb0bcb623f [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
Tejun Heo97e7b782011-02-16 17:11:08 +010031static struct numa_meminfo numa_meminfo __initdata;
Tejun Heoac7136b2011-02-16 17:11:09 +010032static int numa_distance_cnt;
33static u8 *numa_distance;
34
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010035static void * __init early_node_mem(int nodeid, unsigned long start,
Yinghai Lu24a5da72008-02-01 17:49:41 +010036 unsigned long end, unsigned long size,
37 unsigned long align)
Andi Kleena8062232006-04-07 19:49:21 +020038{
Yinghai Lucef625e2010-02-10 01:20:18 -080039 unsigned long mem;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010040
Yinghai Lucef625e2010-02-10 01:20:18 -080041 /*
42 * put it on high as possible
43 * something will go with NODE_DATA
44 */
45 if (start < (MAX_DMA_PFN<<PAGE_SHIFT))
46 start = MAX_DMA_PFN<<PAGE_SHIFT;
47 if (start < (MAX_DMA32_PFN<<PAGE_SHIFT) &&
48 end > (MAX_DMA32_PFN<<PAGE_SHIFT))
49 start = MAX_DMA32_PFN<<PAGE_SHIFT;
Yinghai Lu72d7c3b2010-08-25 13:39:17 -070050 mem = memblock_x86_find_in_range_node(nodeid, start, end, size, align);
51 if (mem != MEMBLOCK_ERROR)
Andi Kleena8062232006-04-07 19:49:21 +020052 return __va(mem);
Yinghai Lu9347e0b2008-02-01 17:49:42 +010053
Yinghai Lucef625e2010-02-10 01:20:18 -080054 /* extend the search scope */
55 end = max_pfn_mapped << PAGE_SHIFT;
Yinghai Lu419db272010-10-28 09:50:17 -070056 start = MAX_DMA_PFN << PAGE_SHIFT;
57 mem = memblock_find_in_range(start, end, size, align);
Yinghai Lu72d7c3b2010-08-25 13:39:17 -070058 if (mem != MEMBLOCK_ERROR)
Yinghai Lu1842f902010-02-10 01:20:15 -080059 return __va(mem);
60
61 printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010062 size, nodeid);
Yinghai Lu1842f902010-02-10 01:20:15 -080063
64 return NULL;
Andi Kleena8062232006-04-07 19:49:21 +020065}
66
Tejun Heod9c515e2011-02-16 17:11:10 +010067static int __init numa_add_memblk_to(int nid, u64 start, u64 end,
68 struct numa_meminfo *mi)
Tejun Heoef396ec2011-02-16 17:11:07 +010069{
Tejun Heo56e827f2011-02-16 17:11:09 +010070 /* ignore zero length blks */
71 if (start == end)
72 return 0;
73
74 /* whine about and ignore invalid blks */
75 if (start > end || nid < 0 || nid >= MAX_NUMNODES) {
76 pr_warning("NUMA: Warning: invalid memblk node %d (%Lx-%Lx)\n",
77 nid, start, end);
78 return 0;
79 }
80
81 if (mi->nr_blks >= NR_NODE_MEMBLKS) {
82 pr_err("NUMA: too many memblk ranges\n");
Tejun Heoef396ec2011-02-16 17:11:07 +010083 return -EINVAL;
84 }
85
Tejun Heo97e7b782011-02-16 17:11:08 +010086 mi->blk[mi->nr_blks].start = start;
87 mi->blk[mi->nr_blks].end = end;
88 mi->blk[mi->nr_blks].nid = nid;
89 mi->nr_blks++;
Tejun Heoef396ec2011-02-16 17:11:07 +010090 return 0;
91}
92
Tejun Heo90e6b672011-02-22 11:10:08 +010093/**
94 * numa_remove_memblk_from - Remove one numa_memblk from a numa_meminfo
95 * @idx: Index of memblk to remove
96 * @mi: numa_meminfo to remove memblk from
97 *
98 * Remove @idx'th numa_memblk from @mi by shifting @mi->blk[] and
99 * decrementing @mi->nr_blks.
100 */
Tejun Heob8ef9172011-02-22 11:10:08 +0100101void __init numa_remove_memblk_from(int idx, struct numa_meminfo *mi)
Tejun Heo2e756be2011-02-16 17:11:09 +0100102{
103 mi->nr_blks--;
104 memmove(&mi->blk[idx], &mi->blk[idx + 1],
105 (mi->nr_blks - idx) * sizeof(mi->blk[0]));
106}
107
Tejun Heo90e6b672011-02-22 11:10:08 +0100108/**
109 * numa_add_memblk - Add one numa_memblk to numa_meminfo
110 * @nid: NUMA node ID of the new memblk
111 * @start: Start address of the new memblk
112 * @end: End address of the new memblk
113 *
114 * Add a new memblk to the default numa_meminfo.
115 *
116 * RETURNS:
117 * 0 on success, -errno on failure.
118 */
Tejun Heod9c515e2011-02-16 17:11:10 +0100119int __init numa_add_memblk(int nid, u64 start, u64 end)
120{
121 return numa_add_memblk_to(nid, start, end, &numa_meminfo);
122}
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/* Initialize bootmem allocator for a node */
Yinghai Lu7c437692009-05-15 13:59:37 -0700125void __init
126setup_node_bootmem(int nodeid, unsigned long start, unsigned long end)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100127{
Yinghai Lu08677212010-02-10 01:20:20 -0800128 unsigned long start_pfn, last_pfn, nodedata_phys;
Yinghai Lu7c437692009-05-15 13:59:37 -0700129 const int pgdat_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
Yinghai Lu1a27fc02008-03-18 12:52:37 -0700130 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Yinghai Lu4c31e922009-04-22 14:19:27 -0700132 if (!end)
133 return;
134
Yinghai Lu7c437692009-05-15 13:59:37 -0700135 /*
136 * Don't confuse VM with a node that doesn't have the
137 * minimum amount of memory:
138 */
139 if (end && (end - start) < NODE_MIN_SIZE)
140 return;
141
Joerg Roedelbe3e89e2008-07-25 16:48:58 +0200142 start = roundup(start, ZONE_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Yinghai Lu08677212010-02-10 01:20:20 -0800144 printk(KERN_INFO "Initmem setup node %d %016lx-%016lx\n", nodeid,
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100145 start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 start_pfn = start >> PAGE_SHIFT;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200148 last_pfn = end >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Yinghai Lu24a5da72008-02-01 17:49:41 +0100150 node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
151 SMP_CACHE_BYTES);
Andi Kleena8062232006-04-07 19:49:21 +0200152 if (node_data[nodeid] == NULL)
153 return;
154 nodedata_phys = __pa(node_data[nodeid]);
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700155 memblock_x86_reserve_range(nodedata_phys, nodedata_phys + pgdat_size, "NODE_DATA");
Yinghai Lu6118f762008-02-04 16:47:56 +0100156 printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
157 nodedata_phys + pgdat_size - 1);
Tejun Heo05293602011-04-01 11:15:12 +0200158 nid = early_pfn_to_nid(nodedata_phys >> PAGE_SHIFT);
Yinghai Lu1842f902010-02-10 01:20:15 -0800159 if (nid != nodeid)
160 printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nodeid, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
Yinghai Lu08677212010-02-10 01:20:20 -0800163 NODE_DATA(nodeid)->node_id = nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 NODE_DATA(nodeid)->node_start_pfn = start_pfn;
Thomas Gleixner886533a2008-05-12 15:43:36 +0200165 NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 node_set_online(nodeid);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100168}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Tejun Heo90e6b672011-02-22 11:10:08 +0100170/**
171 * numa_cleanup_meminfo - Cleanup a numa_meminfo
172 * @mi: numa_meminfo to clean up
173 *
174 * Sanitize @mi by merging and removing unncessary memblks. Also check for
175 * conflicts and clear unused memblks.
176 *
177 * RETURNS:
178 * 0 on success, -errno on failure.
179 */
Tejun Heob8ef9172011-02-22 11:10:08 +0100180int __init numa_cleanup_meminfo(struct numa_meminfo *mi)
Tejun Heofd0435d2011-02-16 17:11:08 +0100181{
Tejun Heo56e827f2011-02-16 17:11:09 +0100182 const u64 low = 0;
183 const u64 high = (u64)max_pfn << PAGE_SHIFT;
Tejun Heo2e756be2011-02-16 17:11:09 +0100184 int i, j, k;
Tejun Heoef396ec2011-02-16 17:11:07 +0100185
Tejun Heo2e756be2011-02-16 17:11:09 +0100186 for (i = 0; i < mi->nr_blks; i++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100187 struct numa_memblk *bi = &mi->blk[i];
Tejun Heoef396ec2011-02-16 17:11:07 +0100188
Tejun Heo56e827f2011-02-16 17:11:09 +0100189 /* make sure all blocks are inside the limits */
190 bi->start = max(bi->start, low);
191 bi->end = min(bi->end, high);
192
193 /* and there's no empty block */
194 if (bi->start == bi->end) {
195 numa_remove_memblk_from(i--, mi);
196 continue;
197 }
198
Tejun Heo2e756be2011-02-16 17:11:09 +0100199 for (j = i + 1; j < mi->nr_blks; j++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100200 struct numa_memblk *bj = &mi->blk[j];
Tejun Heoef396ec2011-02-16 17:11:07 +0100201 unsigned long start, end;
202
Tejun Heo2e756be2011-02-16 17:11:09 +0100203 /*
Tejun Heo56e827f2011-02-16 17:11:09 +0100204 * See whether there are overlapping blocks. Whine
205 * about but allow overlaps of the same nid. They
206 * will be merged below.
207 */
208 if (bi->end > bj->start && bi->start < bj->end) {
209 if (bi->nid != bj->nid) {
210 pr_err("NUMA: node %d (%Lx-%Lx) overlaps with node %d (%Lx-%Lx)\n",
211 bi->nid, bi->start, bi->end,
212 bj->nid, bj->start, bj->end);
213 return -EINVAL;
214 }
215 pr_warning("NUMA: Warning: node %d (%Lx-%Lx) overlaps with itself (%Lx-%Lx)\n",
216 bi->nid, bi->start, bi->end,
217 bj->start, bj->end);
218 }
219
220 /*
Tejun Heo2e756be2011-02-16 17:11:09 +0100221 * Join together blocks on the same node, holes
222 * between which don't overlap with memory on other
223 * nodes.
224 */
Tejun Heo97e7b782011-02-16 17:11:08 +0100225 if (bi->nid != bj->nid)
Tejun Heoef396ec2011-02-16 17:11:07 +0100226 continue;
Tejun Heo56e827f2011-02-16 17:11:09 +0100227 start = max(min(bi->start, bj->start), low);
228 end = min(max(bi->end, bj->end), high);
Tejun Heo2e756be2011-02-16 17:11:09 +0100229 for (k = 0; k < mi->nr_blks; k++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100230 struct numa_memblk *bk = &mi->blk[k];
231
232 if (bi->nid == bk->nid)
Tejun Heoef396ec2011-02-16 17:11:07 +0100233 continue;
Tejun Heo97e7b782011-02-16 17:11:08 +0100234 if (start < bk->end && end > bk->start)
Tejun Heoef396ec2011-02-16 17:11:07 +0100235 break;
236 }
Tejun Heo97e7b782011-02-16 17:11:08 +0100237 if (k < mi->nr_blks)
Tejun Heoef396ec2011-02-16 17:11:07 +0100238 continue;
Tejun Heoef396ec2011-02-16 17:11:07 +0100239 printk(KERN_INFO "NUMA: Node %d [%Lx,%Lx) + [%Lx,%Lx) -> [%lx,%lx)\n",
Tejun Heo97e7b782011-02-16 17:11:08 +0100240 bi->nid, bi->start, bi->end, bj->start, bj->end,
Tejun Heoef396ec2011-02-16 17:11:07 +0100241 start, end);
Tejun Heo97e7b782011-02-16 17:11:08 +0100242 bi->start = start;
243 bi->end = end;
Tejun Heo2e756be2011-02-16 17:11:09 +0100244 numa_remove_memblk_from(j--, mi);
Tejun Heoef396ec2011-02-16 17:11:07 +0100245 }
246 }
247
Tejun Heo56e827f2011-02-16 17:11:09 +0100248 for (i = mi->nr_blks; i < ARRAY_SIZE(mi->blk); i++) {
249 mi->blk[i].start = mi->blk[i].end = 0;
250 mi->blk[i].nid = NUMA_NO_NODE;
251 }
252
Tejun Heof9c60252011-02-16 17:11:09 +0100253 return 0;
254}
255
256/*
Tejun Heo4697bdc2011-02-16 17:11:09 +0100257 * Set nodes, which have memory in @mi, in *@nodemask.
258 */
259static void __init numa_nodemask_from_meminfo(nodemask_t *nodemask,
260 const struct numa_meminfo *mi)
261{
262 int i;
263
264 for (i = 0; i < ARRAY_SIZE(mi->blk); i++)
265 if (mi->blk[i].start != mi->blk[i].end &&
266 mi->blk[i].nid != NUMA_NO_NODE)
267 node_set(mi->blk[i].nid, *nodemask);
268}
269
Tejun Heo90e6b672011-02-22 11:10:08 +0100270/**
271 * numa_reset_distance - Reset NUMA distance table
272 *
273 * The current table is freed. The next numa_set_distance() call will
274 * create a new one.
Tejun Heoac7136b2011-02-16 17:11:09 +0100275 */
Tejun Heob8ef9172011-02-22 11:10:08 +0100276void __init numa_reset_distance(void)
Tejun Heoac7136b2011-02-16 17:11:09 +0100277{
Yinghai Luce003332011-03-02 11:22:14 +0100278 size_t size = numa_distance_cnt * numa_distance_cnt * sizeof(numa_distance[0]);
Tejun Heoac7136b2011-02-16 17:11:09 +0100279
Tejun Heoeb8c1e22011-03-02 11:32:47 +0100280 /* numa_distance could be 1LU marking allocation failure, test cnt */
Yinghai Luce003332011-03-02 11:22:14 +0100281 if (numa_distance_cnt)
Yinghai Lu2ca230b2011-02-17 14:46:37 +0100282 memblock_x86_free_range(__pa(numa_distance),
283 __pa(numa_distance) + size);
Yinghai Luce003332011-03-02 11:22:14 +0100284 numa_distance_cnt = 0;
Tejun Heoeb8c1e22011-03-02 11:32:47 +0100285 numa_distance = NULL; /* enable table creation */
Tejun Heoac7136b2011-02-16 17:11:09 +0100286}
287
Yinghai Lu2bf50552011-02-22 11:18:49 +0100288static int __init numa_alloc_distance(void)
289{
290 nodemask_t nodes_parsed;
291 size_t size;
292 int i, j, cnt = 0;
293 u64 phys;
294
295 /* size the new table and allocate it */
296 nodes_parsed = numa_nodes_parsed;
297 numa_nodemask_from_meminfo(&nodes_parsed, &numa_meminfo);
298
299 for_each_node_mask(i, nodes_parsed)
300 cnt = i;
David Rientjes1f565a82011-02-25 10:06:39 +0100301 cnt++;
302 size = cnt * cnt * sizeof(numa_distance[0]);
Yinghai Lu2bf50552011-02-22 11:18:49 +0100303
304 phys = memblock_find_in_range(0, (u64)max_pfn_mapped << PAGE_SHIFT,
305 size, PAGE_SIZE);
306 if (phys == MEMBLOCK_ERROR) {
307 pr_warning("NUMA: Warning: can't allocate distance table!\n");
308 /* don't retry until explicitly reset */
309 numa_distance = (void *)1LU;
310 return -ENOMEM;
311 }
312 memblock_x86_reserve_range(phys, phys + size, "NUMA DIST");
313
314 numa_distance = __va(phys);
315 numa_distance_cnt = cnt;
316
317 /* fill with the default distances */
318 for (i = 0; i < cnt; i++)
319 for (j = 0; j < cnt; j++)
320 numa_distance[i * cnt + j] = i == j ?
321 LOCAL_DISTANCE : REMOTE_DISTANCE;
322 printk(KERN_DEBUG "NUMA: Initialized distance table, cnt=%d\n", cnt);
323
324 return 0;
325}
326
Tejun Heo90e6b672011-02-22 11:10:08 +0100327/**
328 * numa_set_distance - Set NUMA distance from one NUMA to another
329 * @from: the 'from' node to set distance
330 * @to: the 'to' node to set distance
331 * @distance: NUMA distance
332 *
333 * Set the distance from node @from to @to to @distance. If distance table
Lucas De Marchi0d2eb442011-03-17 16:24:16 -0300334 * doesn't exist, one which is large enough to accommodate all the currently
Tejun Heo90e6b672011-02-22 11:10:08 +0100335 * known nodes will be created.
Tejun Heoeb8c1e22011-03-02 11:32:47 +0100336 *
337 * If such table cannot be allocated, a warning is printed and further
338 * calls are ignored until the distance table is reset with
339 * numa_reset_distance().
340 *
341 * If @from or @to is higher than the highest known node at the time of
342 * table creation or @distance doesn't make sense, the call is ignored.
343 * This is to allow simplification of specific NUMA config implementations.
Tejun Heoac7136b2011-02-16 17:11:09 +0100344 */
345void __init numa_set_distance(int from, int to, int distance)
346{
Yinghai Lu2bf50552011-02-22 11:18:49 +0100347 if (!numa_distance && numa_alloc_distance() < 0)
348 return;
Tejun Heoac7136b2011-02-16 17:11:09 +0100349
350 if (from >= numa_distance_cnt || to >= numa_distance_cnt) {
351 printk_once(KERN_DEBUG "NUMA: Debug: distance out of bound, from=%d to=%d distance=%d\n",
352 from, to, distance);
353 return;
354 }
355
356 if ((u8)distance != distance ||
357 (from == to && distance != LOCAL_DISTANCE)) {
358 pr_warn_once("NUMA: Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
359 from, to, distance);
360 return;
361 }
362
363 numa_distance[from * numa_distance_cnt + to] = distance;
364}
365
366int __node_distance(int from, int to)
367{
Tejun Heoac7136b2011-02-16 17:11:09 +0100368 if (from >= numa_distance_cnt || to >= numa_distance_cnt)
369 return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
370 return numa_distance[from * numa_distance_cnt + to];
371}
372EXPORT_SYMBOL(__node_distance);
373
374/*
Tejun Heof9c60252011-02-16 17:11:09 +0100375 * Sanity check to catch more bad NUMA configurations (they are amazingly
376 * common). Make sure the nodes cover all memory.
377 */
Tejun Heo91556232011-02-16 17:11:09 +0100378static bool __init numa_meminfo_cover_memory(const struct numa_meminfo *mi)
Tejun Heof9c60252011-02-16 17:11:09 +0100379{
380 unsigned long numaram, e820ram;
381 int i;
382
383 numaram = 0;
Tejun Heo91556232011-02-16 17:11:09 +0100384 for (i = 0; i < mi->nr_blks; i++) {
385 unsigned long s = mi->blk[i].start >> PAGE_SHIFT;
386 unsigned long e = mi->blk[i].end >> PAGE_SHIFT;
Tejun Heof9c60252011-02-16 17:11:09 +0100387 numaram += e - s;
Tejun Heo91556232011-02-16 17:11:09 +0100388 numaram -= __absent_pages_in_range(mi->blk[i].nid, s, e);
Tejun Heof9c60252011-02-16 17:11:09 +0100389 if ((long)numaram < 0)
390 numaram = 0;
391 }
392
393 e820ram = max_pfn - (memblock_x86_hole_size(0,
394 max_pfn << PAGE_SHIFT) >> PAGE_SHIFT);
395 /* We seem to lose 3 pages somewhere. Allow 1M of slack. */
396 if ((long)(e820ram - numaram) >= (1 << (20 - PAGE_SHIFT))) {
397 printk(KERN_ERR "NUMA: nodes only cover %luMB of your %luMB e820 RAM. Not used.\n",
398 (numaram << PAGE_SHIFT) >> 20,
399 (e820ram << PAGE_SHIFT) >> 20);
Tejun Heo91556232011-02-16 17:11:09 +0100400 return false;
Tejun Heof9c60252011-02-16 17:11:09 +0100401 }
Tejun Heo91556232011-02-16 17:11:09 +0100402 return true;
Tejun Heof9c60252011-02-16 17:11:09 +0100403}
404
405static int __init numa_register_memblks(struct numa_meminfo *mi)
406{
Yinghai Lu69efcc62011-02-21 10:58:13 +0100407 int i, nid;
Tejun Heof9c60252011-02-16 17:11:09 +0100408
409 /* Account for nodes with cpus and no memory */
Tejun Heo4697bdc2011-02-16 17:11:09 +0100410 node_possible_map = numa_nodes_parsed;
411 numa_nodemask_from_meminfo(&node_possible_map, mi);
Tejun Heof9c60252011-02-16 17:11:09 +0100412 if (WARN_ON(nodes_empty(node_possible_map)))
413 return -EINVAL;
414
Tejun Heo97e7b782011-02-16 17:11:08 +0100415 for (i = 0; i < mi->nr_blks; i++)
416 memblock_x86_register_active_regions(mi->blk[i].nid,
417 mi->blk[i].start >> PAGE_SHIFT,
418 mi->blk[i].end >> PAGE_SHIFT);
Tejun Heofd0435d2011-02-16 17:11:08 +0100419
420 /* for out of order entries */
421 sort_node_map();
Tejun Heo91556232011-02-16 17:11:09 +0100422 if (!numa_meminfo_cover_memory(mi))
Tejun Heofd0435d2011-02-16 17:11:08 +0100423 return -EINVAL;
424
Yinghai Lu69efcc62011-02-21 10:58:13 +0100425 /* Finally register nodes. */
426 for_each_node_mask(nid, node_possible_map) {
427 u64 start = (u64)max_pfn << PAGE_SHIFT;
428 u64 end = 0;
Tejun Heo91556232011-02-16 17:11:09 +0100429
Yinghai Lu69efcc62011-02-21 10:58:13 +0100430 for (i = 0; i < mi->nr_blks; i++) {
431 if (nid != mi->blk[i].nid)
Tejun Heo91556232011-02-16 17:11:09 +0100432 continue;
Yinghai Lu69efcc62011-02-21 10:58:13 +0100433 start = min(mi->blk[i].start, start);
434 end = max(mi->blk[i].end, end);
Tejun Heo91556232011-02-16 17:11:09 +0100435 }
Yinghai Lu69efcc62011-02-21 10:58:13 +0100436
437 if (start < end)
438 setup_node_bootmem(nid, start, end);
Tejun Heo91556232011-02-16 17:11:09 +0100439 }
Tejun Heofd0435d2011-02-16 17:11:08 +0100440
Tejun Heoef396ec2011-02-16 17:11:07 +0100441 return 0;
442}
443
David Rientjesc09cedf2011-03-04 15:17:21 +0100444/**
445 * dummy_numma_init - Fallback dummy NUMA init
446 *
447 * Used if there's no underlying NUMA architecture, NUMA initialization
448 * fails, or NUMA is disabled on the command line.
449 *
450 * Must online at least one node and add memory blocks that cover all
451 * allowed memory. This function must not fail.
452 */
Yinghai Lu6d496f92011-02-17 14:53:20 +0100453static int __init dummy_numa_init(void)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100454{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 printk(KERN_INFO "%s\n",
456 numa_off ? "NUMA turned off" : "No NUMA configuration found");
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100457 printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
Tejun Heo86ef4db2011-02-16 12:13:06 +0100458 0LU, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100459
Tejun Heo92d4a432011-02-16 17:11:09 +0100460 node_set(0, numa_nodes_parsed);
Tejun Heo43a662f2011-02-16 17:11:08 +0100461 numa_add_memblk(0, 0, (u64)max_pfn << PAGE_SHIFT);
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100462
463 return 0;
464}
465
David Rientjesc09cedf2011-03-04 15:17:21 +0100466static int __init numa_init(int (*init_func)(void))
467{
468 int i;
469 int ret;
470
471 for (i = 0; i < MAX_LOCAL_APIC; i++)
472 set_apicid_to_node(i, NUMA_NO_NODE);
473
474 nodes_clear(numa_nodes_parsed);
475 nodes_clear(node_possible_map);
476 nodes_clear(node_online_map);
477 memset(&numa_meminfo, 0, sizeof(numa_meminfo));
478 remove_all_active_ranges();
479 numa_reset_distance();
480
481 ret = init_func();
482 if (ret < 0)
483 return ret;
484 ret = numa_cleanup_meminfo(&numa_meminfo);
485 if (ret < 0)
486 return ret;
487
488 numa_emulation(&numa_meminfo, numa_distance_cnt);
489
490 ret = numa_register_memblks(&numa_meminfo);
491 if (ret < 0)
492 return ret;
493
494 for (i = 0; i < nr_cpu_ids; i++) {
495 int nid = early_cpu_to_node(i);
496
497 if (nid == NUMA_NO_NODE)
498 continue;
499 if (!node_online(nid))
500 numa_clear_node(i);
501 }
502 numa_init_array();
503 return 0;
504}
505
Tejun Heoffe77a42011-02-16 12:13:06 +0100506void __init initmem_init(void)
507{
Tejun Heoffe77a42011-02-16 12:13:06 +0100508 if (!numa_off) {
509#ifdef CONFIG_ACPI_NUMA
Florian Mickler711b8c82011-04-04 01:17:40 +0200510 if (!numa_init(x86_acpi_numa_init))
David Rientjesc09cedf2011-03-04 15:17:21 +0100511 return;
Tejun Heoffe77a42011-02-16 12:13:06 +0100512#endif
513#ifdef CONFIG_AMD_NUMA
Florian Mickler711b8c82011-04-04 01:17:40 +0200514 if (!numa_init(amd_numa_init))
David Rientjesc09cedf2011-03-04 15:17:21 +0100515 return;
Tejun Heoffe77a42011-02-16 12:13:06 +0100516#endif
517 }
518
David Rientjesc09cedf2011-03-04 15:17:21 +0100519 numa_init(dummy_numa_init);
Andi Kleen69d81fc2005-11-05 17:25:53 +0100520}
521
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100522unsigned long __init numa_free_all_bootmem(void)
523{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 unsigned long pages = 0;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100525 int i;
526
527 for_each_online_node(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 pages += free_all_bootmem_node(NODE_DATA(i));
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100529
Yinghai Lu08677212010-02-10 01:20:20 -0800530 pages += free_all_memory_core_early(MAX_NUMNODES);
Yinghai Lu08677212010-02-10 01:20:20 -0800531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return pages;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100533}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100535int __cpuinit numa_cpu_node(int cpu)
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800536{
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100537 int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800538
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100539 if (apicid != BAD_APICID)
540 return __apicid_to_node[apicid];
541 return NUMA_NO_NODE;
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800542}