blob: 70bd8221f928da6e688b81e95f50e8aceb5392ac [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 Heo96886782011-05-02 14:18:51 +020029static struct numa_meminfo numa_meminfo
30#ifndef CONFIG_MEMORY_HOTPLUG
31__initdata
32#endif
33;
34
Tejun Heoac7136b2011-02-16 17:11:09 +010035static int numa_distance_cnt;
36static u8 *numa_distance;
37
Tejun Heod9c515e2011-02-16 17:11:10 +010038static int __init numa_add_memblk_to(int nid, u64 start, u64 end,
39 struct numa_meminfo *mi)
Tejun Heoef396ec2011-02-16 17:11:07 +010040{
Tejun Heo56e827f2011-02-16 17:11:09 +010041 /* ignore zero length blks */
42 if (start == end)
43 return 0;
44
45 /* whine about and ignore invalid blks */
46 if (start > end || nid < 0 || nid >= MAX_NUMNODES) {
47 pr_warning("NUMA: Warning: invalid memblk node %d (%Lx-%Lx)\n",
48 nid, start, end);
49 return 0;
50 }
51
52 if (mi->nr_blks >= NR_NODE_MEMBLKS) {
53 pr_err("NUMA: too many memblk ranges\n");
Tejun Heoef396ec2011-02-16 17:11:07 +010054 return -EINVAL;
55 }
56
Tejun Heo97e7b782011-02-16 17:11:08 +010057 mi->blk[mi->nr_blks].start = start;
58 mi->blk[mi->nr_blks].end = end;
59 mi->blk[mi->nr_blks].nid = nid;
60 mi->nr_blks++;
Tejun Heoef396ec2011-02-16 17:11:07 +010061 return 0;
62}
63
Tejun Heo90e6b672011-02-22 11:10:08 +010064/**
65 * numa_remove_memblk_from - Remove one numa_memblk from a numa_meminfo
66 * @idx: Index of memblk to remove
67 * @mi: numa_meminfo to remove memblk from
68 *
69 * Remove @idx'th numa_memblk from @mi by shifting @mi->blk[] and
70 * decrementing @mi->nr_blks.
71 */
Tejun Heob8ef9172011-02-22 11:10:08 +010072void __init numa_remove_memblk_from(int idx, struct numa_meminfo *mi)
Tejun Heo2e756be2011-02-16 17:11:09 +010073{
74 mi->nr_blks--;
75 memmove(&mi->blk[idx], &mi->blk[idx + 1],
76 (mi->nr_blks - idx) * sizeof(mi->blk[0]));
77}
78
Tejun Heo90e6b672011-02-22 11:10:08 +010079/**
80 * numa_add_memblk - Add one numa_memblk to numa_meminfo
81 * @nid: NUMA node ID of the new memblk
82 * @start: Start address of the new memblk
83 * @end: End address of the new memblk
84 *
85 * Add a new memblk to the default numa_meminfo.
86 *
87 * RETURNS:
88 * 0 on success, -errno on failure.
89 */
Tejun Heod9c515e2011-02-16 17:11:10 +010090int __init numa_add_memblk(int nid, u64 start, u64 end)
91{
92 return numa_add_memblk_to(nid, start, end, &numa_meminfo);
93}
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/* Initialize bootmem allocator for a node */
Tejun Heo1201e102011-05-02 14:18:52 +020096static void __init
Tejun Heoebe685f2011-05-02 14:18:51 +020097setup_node_bootmem(int nid, unsigned long start, unsigned long end)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +010098{
Tejun Heoacd26d62011-05-02 14:18:51 +020099 const u64 nd_low = (u64)MAX_DMA_PFN << PAGE_SHIFT;
100 const u64 nd_high = (u64)max_pfn_mapped << PAGE_SHIFT;
Tejun Heoebe685f2011-05-02 14:18:51 +0200101 const size_t nd_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
102 unsigned long nd_pa;
103 int tnid;
Yinghai Lu4c31e922009-04-22 14:19:27 -0700104
Yinghai Lu7c437692009-05-15 13:59:37 -0700105 /*
106 * Don't confuse VM with a node that doesn't have the
107 * minimum amount of memory:
108 */
109 if (end && (end - start) < NODE_MIN_SIZE)
110 return;
111
Joerg Roedelbe3e89e2008-07-25 16:48:58 +0200112 start = roundup(start, ZONE_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Tejun Heoebe685f2011-05-02 14:18:51 +0200114 printk(KERN_INFO "Initmem setup node %d %016lx-%016lx\n",
115 nid, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Tejun Heoacd26d62011-05-02 14:18:51 +0200117 /*
118 * Try to allocate node data on local node and then fall back to
119 * all nodes. Never allocate in DMA zone.
120 */
121 nd_pa = memblock_x86_find_in_range_node(nid, nd_low, nd_high,
122 nd_size, SMP_CACHE_BYTES);
123 if (nd_pa == MEMBLOCK_ERROR)
124 nd_pa = memblock_find_in_range(nd_low, nd_high,
125 nd_size, SMP_CACHE_BYTES);
126 if (nd_pa == MEMBLOCK_ERROR) {
127 pr_err("Cannot find %lu bytes in node %d\n", nd_size, nid);
Andi Kleena8062232006-04-07 19:49:21 +0200128 return;
Tejun Heoacd26d62011-05-02 14:18:51 +0200129 }
Tejun Heoebe685f2011-05-02 14:18:51 +0200130 memblock_x86_reserve_range(nd_pa, nd_pa + nd_size, "NODE_DATA");
Tejun Heoacd26d62011-05-02 14:18:51 +0200131
132 /* report and initialize */
Tejun Heoebe685f2011-05-02 14:18:51 +0200133 printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n",
134 nd_pa, nd_pa + nd_size - 1);
135 tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
136 if (tnid != nid)
137 printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nid, tnid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Tejun Heoacd26d62011-05-02 14:18:51 +0200139 node_data[nid] = __va(nd_pa);
Tejun Heoebe685f2011-05-02 14:18:51 +0200140 memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
141 NODE_DATA(nid)->node_id = nid;
142 NODE_DATA(nid)->node_start_pfn = start >> PAGE_SHIFT;
143 NODE_DATA(nid)->node_spanned_pages = (end - start) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Tejun Heoebe685f2011-05-02 14:18:51 +0200145 node_set_online(nid);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100146}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Tejun Heo90e6b672011-02-22 11:10:08 +0100148/**
149 * numa_cleanup_meminfo - Cleanup a numa_meminfo
150 * @mi: numa_meminfo to clean up
151 *
152 * Sanitize @mi by merging and removing unncessary memblks. Also check for
153 * conflicts and clear unused memblks.
154 *
155 * RETURNS:
156 * 0 on success, -errno on failure.
157 */
Tejun Heob8ef9172011-02-22 11:10:08 +0100158int __init numa_cleanup_meminfo(struct numa_meminfo *mi)
Tejun Heofd0435d2011-02-16 17:11:08 +0100159{
Tejun Heo56e827f2011-02-16 17:11:09 +0100160 const u64 low = 0;
161 const u64 high = (u64)max_pfn << PAGE_SHIFT;
Tejun Heo2e756be2011-02-16 17:11:09 +0100162 int i, j, k;
Tejun Heoef396ec2011-02-16 17:11:07 +0100163
Tejun Heo2e756be2011-02-16 17:11:09 +0100164 for (i = 0; i < mi->nr_blks; i++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100165 struct numa_memblk *bi = &mi->blk[i];
Tejun Heoef396ec2011-02-16 17:11:07 +0100166
Tejun Heo56e827f2011-02-16 17:11:09 +0100167 /* make sure all blocks are inside the limits */
168 bi->start = max(bi->start, low);
169 bi->end = min(bi->end, high);
170
171 /* and there's no empty block */
Yinghai Lu2be19102011-05-01 19:12:04 +0200172 if (bi->start >= bi->end) {
Tejun Heo56e827f2011-02-16 17:11:09 +0100173 numa_remove_memblk_from(i--, mi);
174 continue;
175 }
176
Tejun Heo2e756be2011-02-16 17:11:09 +0100177 for (j = i + 1; j < mi->nr_blks; j++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100178 struct numa_memblk *bj = &mi->blk[j];
Tejun Heoef396ec2011-02-16 17:11:07 +0100179 unsigned long start, end;
180
Tejun Heo2e756be2011-02-16 17:11:09 +0100181 /*
Tejun Heo56e827f2011-02-16 17:11:09 +0100182 * See whether there are overlapping blocks. Whine
183 * about but allow overlaps of the same nid. They
184 * will be merged below.
185 */
186 if (bi->end > bj->start && bi->start < bj->end) {
187 if (bi->nid != bj->nid) {
188 pr_err("NUMA: node %d (%Lx-%Lx) overlaps with node %d (%Lx-%Lx)\n",
189 bi->nid, bi->start, bi->end,
190 bj->nid, bj->start, bj->end);
191 return -EINVAL;
192 }
193 pr_warning("NUMA: Warning: node %d (%Lx-%Lx) overlaps with itself (%Lx-%Lx)\n",
194 bi->nid, bi->start, bi->end,
195 bj->start, bj->end);
196 }
197
198 /*
Tejun Heo2e756be2011-02-16 17:11:09 +0100199 * Join together blocks on the same node, holes
200 * between which don't overlap with memory on other
201 * nodes.
202 */
Tejun Heo97e7b782011-02-16 17:11:08 +0100203 if (bi->nid != bj->nid)
Tejun Heoef396ec2011-02-16 17:11:07 +0100204 continue;
Tejun Heo56e827f2011-02-16 17:11:09 +0100205 start = max(min(bi->start, bj->start), low);
206 end = min(max(bi->end, bj->end), high);
Tejun Heo2e756be2011-02-16 17:11:09 +0100207 for (k = 0; k < mi->nr_blks; k++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100208 struct numa_memblk *bk = &mi->blk[k];
209
210 if (bi->nid == bk->nid)
Tejun Heoef396ec2011-02-16 17:11:07 +0100211 continue;
Tejun Heo97e7b782011-02-16 17:11:08 +0100212 if (start < bk->end && end > bk->start)
Tejun Heoef396ec2011-02-16 17:11:07 +0100213 break;
214 }
Tejun Heo97e7b782011-02-16 17:11:08 +0100215 if (k < mi->nr_blks)
Tejun Heoef396ec2011-02-16 17:11:07 +0100216 continue;
Tejun Heoef396ec2011-02-16 17:11:07 +0100217 printk(KERN_INFO "NUMA: Node %d [%Lx,%Lx) + [%Lx,%Lx) -> [%lx,%lx)\n",
Tejun Heo97e7b782011-02-16 17:11:08 +0100218 bi->nid, bi->start, bi->end, bj->start, bj->end,
Tejun Heoef396ec2011-02-16 17:11:07 +0100219 start, end);
Tejun Heo97e7b782011-02-16 17:11:08 +0100220 bi->start = start;
221 bi->end = end;
Tejun Heo2e756be2011-02-16 17:11:09 +0100222 numa_remove_memblk_from(j--, mi);
Tejun Heoef396ec2011-02-16 17:11:07 +0100223 }
224 }
225
Tejun Heo56e827f2011-02-16 17:11:09 +0100226 for (i = mi->nr_blks; i < ARRAY_SIZE(mi->blk); i++) {
227 mi->blk[i].start = mi->blk[i].end = 0;
228 mi->blk[i].nid = NUMA_NO_NODE;
229 }
230
Tejun Heof9c60252011-02-16 17:11:09 +0100231 return 0;
232}
233
234/*
Tejun Heo4697bdc2011-02-16 17:11:09 +0100235 * Set nodes, which have memory in @mi, in *@nodemask.
236 */
237static void __init numa_nodemask_from_meminfo(nodemask_t *nodemask,
238 const struct numa_meminfo *mi)
239{
240 int i;
241
242 for (i = 0; i < ARRAY_SIZE(mi->blk); i++)
243 if (mi->blk[i].start != mi->blk[i].end &&
244 mi->blk[i].nid != NUMA_NO_NODE)
245 node_set(mi->blk[i].nid, *nodemask);
246}
247
Tejun Heo90e6b672011-02-22 11:10:08 +0100248/**
249 * numa_reset_distance - Reset NUMA distance table
250 *
251 * The current table is freed. The next numa_set_distance() call will
252 * create a new one.
Tejun Heoac7136b2011-02-16 17:11:09 +0100253 */
Tejun Heob8ef9172011-02-22 11:10:08 +0100254void __init numa_reset_distance(void)
Tejun Heoac7136b2011-02-16 17:11:09 +0100255{
Yinghai Luce003332011-03-02 11:22:14 +0100256 size_t size = numa_distance_cnt * numa_distance_cnt * sizeof(numa_distance[0]);
Tejun Heoac7136b2011-02-16 17:11:09 +0100257
Tejun Heoeb8c1e22011-03-02 11:32:47 +0100258 /* numa_distance could be 1LU marking allocation failure, test cnt */
Yinghai Luce003332011-03-02 11:22:14 +0100259 if (numa_distance_cnt)
Yinghai Lu2ca230b2011-02-17 14:46:37 +0100260 memblock_x86_free_range(__pa(numa_distance),
261 __pa(numa_distance) + size);
Yinghai Luce003332011-03-02 11:22:14 +0100262 numa_distance_cnt = 0;
Tejun Heoeb8c1e22011-03-02 11:32:47 +0100263 numa_distance = NULL; /* enable table creation */
Tejun Heoac7136b2011-02-16 17:11:09 +0100264}
265
Yinghai Lu2bf50552011-02-22 11:18:49 +0100266static int __init numa_alloc_distance(void)
267{
268 nodemask_t nodes_parsed;
269 size_t size;
270 int i, j, cnt = 0;
271 u64 phys;
272
273 /* size the new table and allocate it */
274 nodes_parsed = numa_nodes_parsed;
275 numa_nodemask_from_meminfo(&nodes_parsed, &numa_meminfo);
276
277 for_each_node_mask(i, nodes_parsed)
278 cnt = i;
David Rientjes1f565a82011-02-25 10:06:39 +0100279 cnt++;
280 size = cnt * cnt * sizeof(numa_distance[0]);
Yinghai Lu2bf50552011-02-22 11:18:49 +0100281
282 phys = memblock_find_in_range(0, (u64)max_pfn_mapped << PAGE_SHIFT,
283 size, PAGE_SIZE);
284 if (phys == MEMBLOCK_ERROR) {
285 pr_warning("NUMA: Warning: can't allocate distance table!\n");
286 /* don't retry until explicitly reset */
287 numa_distance = (void *)1LU;
288 return -ENOMEM;
289 }
290 memblock_x86_reserve_range(phys, phys + size, "NUMA DIST");
291
292 numa_distance = __va(phys);
293 numa_distance_cnt = cnt;
294
295 /* fill with the default distances */
296 for (i = 0; i < cnt; i++)
297 for (j = 0; j < cnt; j++)
298 numa_distance[i * cnt + j] = i == j ?
299 LOCAL_DISTANCE : REMOTE_DISTANCE;
300 printk(KERN_DEBUG "NUMA: Initialized distance table, cnt=%d\n", cnt);
301
302 return 0;
303}
304
Tejun Heo90e6b672011-02-22 11:10:08 +0100305/**
306 * numa_set_distance - Set NUMA distance from one NUMA to another
307 * @from: the 'from' node to set distance
308 * @to: the 'to' node to set distance
309 * @distance: NUMA distance
310 *
311 * Set the distance from node @from to @to to @distance. If distance table
Lucas De Marchi0d2eb442011-03-17 16:24:16 -0300312 * doesn't exist, one which is large enough to accommodate all the currently
Tejun Heo90e6b672011-02-22 11:10:08 +0100313 * known nodes will be created.
Tejun Heoeb8c1e22011-03-02 11:32:47 +0100314 *
315 * If such table cannot be allocated, a warning is printed and further
316 * calls are ignored until the distance table is reset with
317 * numa_reset_distance().
318 *
319 * If @from or @to is higher than the highest known node at the time of
320 * table creation or @distance doesn't make sense, the call is ignored.
321 * This is to allow simplification of specific NUMA config implementations.
Tejun Heoac7136b2011-02-16 17:11:09 +0100322 */
323void __init numa_set_distance(int from, int to, int distance)
324{
Yinghai Lu2bf50552011-02-22 11:18:49 +0100325 if (!numa_distance && numa_alloc_distance() < 0)
326 return;
Tejun Heoac7136b2011-02-16 17:11:09 +0100327
328 if (from >= numa_distance_cnt || to >= numa_distance_cnt) {
329 printk_once(KERN_DEBUG "NUMA: Debug: distance out of bound, from=%d to=%d distance=%d\n",
330 from, to, distance);
331 return;
332 }
333
334 if ((u8)distance != distance ||
335 (from == to && distance != LOCAL_DISTANCE)) {
336 pr_warn_once("NUMA: Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
337 from, to, distance);
338 return;
339 }
340
341 numa_distance[from * numa_distance_cnt + to] = distance;
342}
343
344int __node_distance(int from, int to)
345{
Tejun Heoac7136b2011-02-16 17:11:09 +0100346 if (from >= numa_distance_cnt || to >= numa_distance_cnt)
347 return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
348 return numa_distance[from * numa_distance_cnt + to];
349}
350EXPORT_SYMBOL(__node_distance);
351
352/*
Tejun Heof9c60252011-02-16 17:11:09 +0100353 * Sanity check to catch more bad NUMA configurations (they are amazingly
354 * common). Make sure the nodes cover all memory.
355 */
Tejun Heo91556232011-02-16 17:11:09 +0100356static bool __init numa_meminfo_cover_memory(const struct numa_meminfo *mi)
Tejun Heof9c60252011-02-16 17:11:09 +0100357{
358 unsigned long numaram, e820ram;
359 int i;
360
361 numaram = 0;
Tejun Heo91556232011-02-16 17:11:09 +0100362 for (i = 0; i < mi->nr_blks; i++) {
363 unsigned long s = mi->blk[i].start >> PAGE_SHIFT;
364 unsigned long e = mi->blk[i].end >> PAGE_SHIFT;
Tejun Heof9c60252011-02-16 17:11:09 +0100365 numaram += e - s;
Tejun Heo91556232011-02-16 17:11:09 +0100366 numaram -= __absent_pages_in_range(mi->blk[i].nid, s, e);
Tejun Heof9c60252011-02-16 17:11:09 +0100367 if ((long)numaram < 0)
368 numaram = 0;
369 }
370
371 e820ram = max_pfn - (memblock_x86_hole_size(0,
372 max_pfn << PAGE_SHIFT) >> PAGE_SHIFT);
373 /* We seem to lose 3 pages somewhere. Allow 1M of slack. */
374 if ((long)(e820ram - numaram) >= (1 << (20 - PAGE_SHIFT))) {
375 printk(KERN_ERR "NUMA: nodes only cover %luMB of your %luMB e820 RAM. Not used.\n",
376 (numaram << PAGE_SHIFT) >> 20,
377 (e820ram << PAGE_SHIFT) >> 20);
Tejun Heo91556232011-02-16 17:11:09 +0100378 return false;
Tejun Heof9c60252011-02-16 17:11:09 +0100379 }
Tejun Heo91556232011-02-16 17:11:09 +0100380 return true;
Tejun Heof9c60252011-02-16 17:11:09 +0100381}
382
383static int __init numa_register_memblks(struct numa_meminfo *mi)
384{
Yinghai Lu69efcc62011-02-21 10:58:13 +0100385 int i, nid;
Tejun Heof9c60252011-02-16 17:11:09 +0100386
387 /* Account for nodes with cpus and no memory */
Tejun Heo4697bdc2011-02-16 17:11:09 +0100388 node_possible_map = numa_nodes_parsed;
389 numa_nodemask_from_meminfo(&node_possible_map, mi);
Tejun Heof9c60252011-02-16 17:11:09 +0100390 if (WARN_ON(nodes_empty(node_possible_map)))
391 return -EINVAL;
392
Tejun Heo97e7b782011-02-16 17:11:08 +0100393 for (i = 0; i < mi->nr_blks; i++)
394 memblock_x86_register_active_regions(mi->blk[i].nid,
395 mi->blk[i].start >> PAGE_SHIFT,
396 mi->blk[i].end >> PAGE_SHIFT);
Tejun Heofd0435d2011-02-16 17:11:08 +0100397
398 /* for out of order entries */
399 sort_node_map();
Tejun Heo91556232011-02-16 17:11:09 +0100400 if (!numa_meminfo_cover_memory(mi))
Tejun Heofd0435d2011-02-16 17:11:08 +0100401 return -EINVAL;
402
Yinghai Lu69efcc62011-02-21 10:58:13 +0100403 /* Finally register nodes. */
404 for_each_node_mask(nid, node_possible_map) {
405 u64 start = (u64)max_pfn << PAGE_SHIFT;
406 u64 end = 0;
Tejun Heo91556232011-02-16 17:11:09 +0100407
Yinghai Lu69efcc62011-02-21 10:58:13 +0100408 for (i = 0; i < mi->nr_blks; i++) {
409 if (nid != mi->blk[i].nid)
Tejun Heo91556232011-02-16 17:11:09 +0100410 continue;
Yinghai Lu69efcc62011-02-21 10:58:13 +0100411 start = min(mi->blk[i].start, start);
412 end = max(mi->blk[i].end, end);
Tejun Heo91556232011-02-16 17:11:09 +0100413 }
Yinghai Lu69efcc62011-02-21 10:58:13 +0100414
415 if (start < end)
416 setup_node_bootmem(nid, start, end);
Tejun Heo91556232011-02-16 17:11:09 +0100417 }
Tejun Heofd0435d2011-02-16 17:11:08 +0100418
Tejun Heoef396ec2011-02-16 17:11:07 +0100419 return 0;
420}
421
David Rientjesc09cedf2011-03-04 15:17:21 +0100422/**
423 * dummy_numma_init - Fallback dummy NUMA init
424 *
425 * Used if there's no underlying NUMA architecture, NUMA initialization
426 * fails, or NUMA is disabled on the command line.
427 *
428 * Must online at least one node and add memory blocks that cover all
429 * allowed memory. This function must not fail.
430 */
Yinghai Lu6d496f92011-02-17 14:53:20 +0100431static int __init dummy_numa_init(void)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100432{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 printk(KERN_INFO "%s\n",
434 numa_off ? "NUMA turned off" : "No NUMA configuration found");
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100435 printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
Tejun Heo86ef4db2011-02-16 12:13:06 +0100436 0LU, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100437
Tejun Heo92d4a432011-02-16 17:11:09 +0100438 node_set(0, numa_nodes_parsed);
Tejun Heo43a662f2011-02-16 17:11:08 +0100439 numa_add_memblk(0, 0, (u64)max_pfn << PAGE_SHIFT);
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100440
441 return 0;
442}
443
David Rientjesc09cedf2011-03-04 15:17:21 +0100444static int __init numa_init(int (*init_func)(void))
445{
446 int i;
447 int ret;
448
449 for (i = 0; i < MAX_LOCAL_APIC; i++)
450 set_apicid_to_node(i, NUMA_NO_NODE);
451
452 nodes_clear(numa_nodes_parsed);
453 nodes_clear(node_possible_map);
454 nodes_clear(node_online_map);
455 memset(&numa_meminfo, 0, sizeof(numa_meminfo));
456 remove_all_active_ranges();
457 numa_reset_distance();
458
459 ret = init_func();
460 if (ret < 0)
461 return ret;
462 ret = numa_cleanup_meminfo(&numa_meminfo);
463 if (ret < 0)
464 return ret;
465
466 numa_emulation(&numa_meminfo, numa_distance_cnt);
467
468 ret = numa_register_memblks(&numa_meminfo);
469 if (ret < 0)
470 return ret;
471
472 for (i = 0; i < nr_cpu_ids; i++) {
473 int nid = early_cpu_to_node(i);
474
475 if (nid == NUMA_NO_NODE)
476 continue;
477 if (!node_online(nid))
478 numa_clear_node(i);
479 }
480 numa_init_array();
481 return 0;
482}
483
Tejun Heoffe77a42011-02-16 12:13:06 +0100484void __init initmem_init(void)
485{
Tejun Heoffe77a42011-02-16 12:13:06 +0100486 if (!numa_off) {
487#ifdef CONFIG_ACPI_NUMA
Florian Mickler711b8c82011-04-04 01:17:40 +0200488 if (!numa_init(x86_acpi_numa_init))
David Rientjesc09cedf2011-03-04 15:17:21 +0100489 return;
Tejun Heoffe77a42011-02-16 12:13:06 +0100490#endif
491#ifdef CONFIG_AMD_NUMA
Florian Mickler711b8c82011-04-04 01:17:40 +0200492 if (!numa_init(amd_numa_init))
David Rientjesc09cedf2011-03-04 15:17:21 +0100493 return;
Tejun Heoffe77a42011-02-16 12:13:06 +0100494#endif
495 }
496
David Rientjesc09cedf2011-03-04 15:17:21 +0100497 numa_init(dummy_numa_init);
Andi Kleen69d81fc2005-11-05 17:25:53 +0100498}
499
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100500unsigned long __init numa_free_all_bootmem(void)
501{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 unsigned long pages = 0;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100503 int i;
504
505 for_each_online_node(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 pages += free_all_bootmem_node(NODE_DATA(i));
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100507
Yinghai Lu08677212010-02-10 01:20:20 -0800508 pages += free_all_memory_core_early(MAX_NUMNODES);
Yinghai Lu08677212010-02-10 01:20:20 -0800509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return pages;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100511}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Tejun Heo96886782011-05-02 14:18:51 +0200513#ifdef CONFIG_MEMORY_HOTPLUG
514int memory_add_physaddr_to_nid(u64 start)
515{
516 struct numa_meminfo *mi = &numa_meminfo;
517 int nid = mi->blk[0].nid;
518 int i;
519
520 for (i = 0; i < mi->nr_blks; i++)
521 if (mi->blk[i].start <= start && mi->blk[i].end > start)
522 nid = mi->blk[i].nid;
523 return nid;
524}
525EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
526#endif