blob: b4fd25e753cb3a2473501da8c33dc41d98dcfd7f [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 Heo96886782011-05-02 14:18:51 +020031static struct numa_meminfo numa_meminfo
32#ifndef CONFIG_MEMORY_HOTPLUG
33__initdata
34#endif
35;
36
Tejun Heoac7136b2011-02-16 17:11:09 +010037static int numa_distance_cnt;
38static u8 *numa_distance;
39
Tejun Heod9c515e2011-02-16 17:11:10 +010040static int __init numa_add_memblk_to(int nid, u64 start, u64 end,
41 struct numa_meminfo *mi)
Tejun Heoef396ec2011-02-16 17:11:07 +010042{
Tejun Heo56e827f2011-02-16 17:11:09 +010043 /* ignore zero length blks */
44 if (start == end)
45 return 0;
46
47 /* whine about and ignore invalid blks */
48 if (start > end || nid < 0 || nid >= MAX_NUMNODES) {
49 pr_warning("NUMA: Warning: invalid memblk node %d (%Lx-%Lx)\n",
50 nid, start, end);
51 return 0;
52 }
53
54 if (mi->nr_blks >= NR_NODE_MEMBLKS) {
55 pr_err("NUMA: too many memblk ranges\n");
Tejun Heoef396ec2011-02-16 17:11:07 +010056 return -EINVAL;
57 }
58
Tejun Heo97e7b782011-02-16 17:11:08 +010059 mi->blk[mi->nr_blks].start = start;
60 mi->blk[mi->nr_blks].end = end;
61 mi->blk[mi->nr_blks].nid = nid;
62 mi->nr_blks++;
Tejun Heoef396ec2011-02-16 17:11:07 +010063 return 0;
64}
65
Tejun Heo90e6b672011-02-22 11:10:08 +010066/**
67 * numa_remove_memblk_from - Remove one numa_memblk from a numa_meminfo
68 * @idx: Index of memblk to remove
69 * @mi: numa_meminfo to remove memblk from
70 *
71 * Remove @idx'th numa_memblk from @mi by shifting @mi->blk[] and
72 * decrementing @mi->nr_blks.
73 */
Tejun Heob8ef9172011-02-22 11:10:08 +010074void __init numa_remove_memblk_from(int idx, struct numa_meminfo *mi)
Tejun Heo2e756be2011-02-16 17:11:09 +010075{
76 mi->nr_blks--;
77 memmove(&mi->blk[idx], &mi->blk[idx + 1],
78 (mi->nr_blks - idx) * sizeof(mi->blk[0]));
79}
80
Tejun Heo90e6b672011-02-22 11:10:08 +010081/**
82 * numa_add_memblk - Add one numa_memblk to numa_meminfo
83 * @nid: NUMA node ID of the new memblk
84 * @start: Start address of the new memblk
85 * @end: End address of the new memblk
86 *
87 * Add a new memblk to the default numa_meminfo.
88 *
89 * RETURNS:
90 * 0 on success, -errno on failure.
91 */
Tejun Heod9c515e2011-02-16 17:11:10 +010092int __init numa_add_memblk(int nid, u64 start, u64 end)
93{
94 return numa_add_memblk_to(nid, start, end, &numa_meminfo);
95}
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/* Initialize bootmem allocator for a node */
Yinghai Lu7c437692009-05-15 13:59:37 -070098void __init
Tejun Heoebe685f2011-05-02 14:18:51 +020099setup_node_bootmem(int nid, unsigned long start, unsigned long end)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100100{
Tejun Heoacd26d62011-05-02 14:18:51 +0200101 const u64 nd_low = (u64)MAX_DMA_PFN << PAGE_SHIFT;
102 const u64 nd_high = (u64)max_pfn_mapped << PAGE_SHIFT;
Tejun Heoebe685f2011-05-02 14:18:51 +0200103 const size_t nd_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
104 unsigned long nd_pa;
105 int tnid;
Yinghai Lu4c31e922009-04-22 14:19:27 -0700106
Yinghai Lu7c437692009-05-15 13:59:37 -0700107 /*
108 * Don't confuse VM with a node that doesn't have the
109 * minimum amount of memory:
110 */
111 if (end && (end - start) < NODE_MIN_SIZE)
112 return;
113
Joerg Roedelbe3e89e2008-07-25 16:48:58 +0200114 start = roundup(start, ZONE_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Tejun Heoebe685f2011-05-02 14:18:51 +0200116 printk(KERN_INFO "Initmem setup node %d %016lx-%016lx\n",
117 nid, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Tejun Heoacd26d62011-05-02 14:18:51 +0200119 /*
120 * Try to allocate node data on local node and then fall back to
121 * all nodes. Never allocate in DMA zone.
122 */
123 nd_pa = memblock_x86_find_in_range_node(nid, nd_low, nd_high,
124 nd_size, SMP_CACHE_BYTES);
125 if (nd_pa == MEMBLOCK_ERROR)
126 nd_pa = memblock_find_in_range(nd_low, nd_high,
127 nd_size, SMP_CACHE_BYTES);
128 if (nd_pa == MEMBLOCK_ERROR) {
129 pr_err("Cannot find %lu bytes in node %d\n", nd_size, nid);
Andi Kleena8062232006-04-07 19:49:21 +0200130 return;
Tejun Heoacd26d62011-05-02 14:18:51 +0200131 }
Tejun Heoebe685f2011-05-02 14:18:51 +0200132 memblock_x86_reserve_range(nd_pa, nd_pa + nd_size, "NODE_DATA");
Tejun Heoacd26d62011-05-02 14:18:51 +0200133
134 /* report and initialize */
Tejun Heoebe685f2011-05-02 14:18:51 +0200135 printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n",
136 nd_pa, nd_pa + nd_size - 1);
137 tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
138 if (tnid != nid)
139 printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nid, tnid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Tejun Heoacd26d62011-05-02 14:18:51 +0200141 node_data[nid] = __va(nd_pa);
Tejun Heoebe685f2011-05-02 14:18:51 +0200142 memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
143 NODE_DATA(nid)->node_id = nid;
144 NODE_DATA(nid)->node_start_pfn = start >> PAGE_SHIFT;
145 NODE_DATA(nid)->node_spanned_pages = (end - start) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Tejun Heoebe685f2011-05-02 14:18:51 +0200147 node_set_online(nid);
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100148}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Tejun Heo90e6b672011-02-22 11:10:08 +0100150/**
151 * numa_cleanup_meminfo - Cleanup a numa_meminfo
152 * @mi: numa_meminfo to clean up
153 *
154 * Sanitize @mi by merging and removing unncessary memblks. Also check for
155 * conflicts and clear unused memblks.
156 *
157 * RETURNS:
158 * 0 on success, -errno on failure.
159 */
Tejun Heob8ef9172011-02-22 11:10:08 +0100160int __init numa_cleanup_meminfo(struct numa_meminfo *mi)
Tejun Heofd0435d2011-02-16 17:11:08 +0100161{
Tejun Heo56e827f2011-02-16 17:11:09 +0100162 const u64 low = 0;
163 const u64 high = (u64)max_pfn << PAGE_SHIFT;
Tejun Heo2e756be2011-02-16 17:11:09 +0100164 int i, j, k;
Tejun Heoef396ec2011-02-16 17:11:07 +0100165
Tejun Heo2e756be2011-02-16 17:11:09 +0100166 for (i = 0; i < mi->nr_blks; i++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100167 struct numa_memblk *bi = &mi->blk[i];
Tejun Heoef396ec2011-02-16 17:11:07 +0100168
Tejun Heo56e827f2011-02-16 17:11:09 +0100169 /* make sure all blocks are inside the limits */
170 bi->start = max(bi->start, low);
171 bi->end = min(bi->end, high);
172
173 /* and there's no empty block */
Yinghai Lu2be19102011-05-01 19:12:04 +0200174 if (bi->start >= bi->end) {
Tejun Heo56e827f2011-02-16 17:11:09 +0100175 numa_remove_memblk_from(i--, mi);
176 continue;
177 }
178
Tejun Heo2e756be2011-02-16 17:11:09 +0100179 for (j = i + 1; j < mi->nr_blks; j++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100180 struct numa_memblk *bj = &mi->blk[j];
Tejun Heoef396ec2011-02-16 17:11:07 +0100181 unsigned long start, end;
182
Tejun Heo2e756be2011-02-16 17:11:09 +0100183 /*
Tejun Heo56e827f2011-02-16 17:11:09 +0100184 * See whether there are overlapping blocks. Whine
185 * about but allow overlaps of the same nid. They
186 * will be merged below.
187 */
188 if (bi->end > bj->start && bi->start < bj->end) {
189 if (bi->nid != bj->nid) {
190 pr_err("NUMA: node %d (%Lx-%Lx) overlaps with node %d (%Lx-%Lx)\n",
191 bi->nid, bi->start, bi->end,
192 bj->nid, bj->start, bj->end);
193 return -EINVAL;
194 }
195 pr_warning("NUMA: Warning: node %d (%Lx-%Lx) overlaps with itself (%Lx-%Lx)\n",
196 bi->nid, bi->start, bi->end,
197 bj->start, bj->end);
198 }
199
200 /*
Tejun Heo2e756be2011-02-16 17:11:09 +0100201 * Join together blocks on the same node, holes
202 * between which don't overlap with memory on other
203 * nodes.
204 */
Tejun Heo97e7b782011-02-16 17:11:08 +0100205 if (bi->nid != bj->nid)
Tejun Heoef396ec2011-02-16 17:11:07 +0100206 continue;
Tejun Heo56e827f2011-02-16 17:11:09 +0100207 start = max(min(bi->start, bj->start), low);
208 end = min(max(bi->end, bj->end), high);
Tejun Heo2e756be2011-02-16 17:11:09 +0100209 for (k = 0; k < mi->nr_blks; k++) {
Tejun Heo97e7b782011-02-16 17:11:08 +0100210 struct numa_memblk *bk = &mi->blk[k];
211
212 if (bi->nid == bk->nid)
Tejun Heoef396ec2011-02-16 17:11:07 +0100213 continue;
Tejun Heo97e7b782011-02-16 17:11:08 +0100214 if (start < bk->end && end > bk->start)
Tejun Heoef396ec2011-02-16 17:11:07 +0100215 break;
216 }
Tejun Heo97e7b782011-02-16 17:11:08 +0100217 if (k < mi->nr_blks)
Tejun Heoef396ec2011-02-16 17:11:07 +0100218 continue;
Tejun Heoef396ec2011-02-16 17:11:07 +0100219 printk(KERN_INFO "NUMA: Node %d [%Lx,%Lx) + [%Lx,%Lx) -> [%lx,%lx)\n",
Tejun Heo97e7b782011-02-16 17:11:08 +0100220 bi->nid, bi->start, bi->end, bj->start, bj->end,
Tejun Heoef396ec2011-02-16 17:11:07 +0100221 start, end);
Tejun Heo97e7b782011-02-16 17:11:08 +0100222 bi->start = start;
223 bi->end = end;
Tejun Heo2e756be2011-02-16 17:11:09 +0100224 numa_remove_memblk_from(j--, mi);
Tejun Heoef396ec2011-02-16 17:11:07 +0100225 }
226 }
227
Tejun Heo56e827f2011-02-16 17:11:09 +0100228 for (i = mi->nr_blks; i < ARRAY_SIZE(mi->blk); i++) {
229 mi->blk[i].start = mi->blk[i].end = 0;
230 mi->blk[i].nid = NUMA_NO_NODE;
231 }
232
Tejun Heof9c60252011-02-16 17:11:09 +0100233 return 0;
234}
235
236/*
Tejun Heo4697bdc2011-02-16 17:11:09 +0100237 * Set nodes, which have memory in @mi, in *@nodemask.
238 */
239static void __init numa_nodemask_from_meminfo(nodemask_t *nodemask,
240 const struct numa_meminfo *mi)
241{
242 int i;
243
244 for (i = 0; i < ARRAY_SIZE(mi->blk); i++)
245 if (mi->blk[i].start != mi->blk[i].end &&
246 mi->blk[i].nid != NUMA_NO_NODE)
247 node_set(mi->blk[i].nid, *nodemask);
248}
249
Tejun Heo90e6b672011-02-22 11:10:08 +0100250/**
251 * numa_reset_distance - Reset NUMA distance table
252 *
253 * The current table is freed. The next numa_set_distance() call will
254 * create a new one.
Tejun Heoac7136b2011-02-16 17:11:09 +0100255 */
Tejun Heob8ef9172011-02-22 11:10:08 +0100256void __init numa_reset_distance(void)
Tejun Heoac7136b2011-02-16 17:11:09 +0100257{
Yinghai Luce003332011-03-02 11:22:14 +0100258 size_t size = numa_distance_cnt * numa_distance_cnt * sizeof(numa_distance[0]);
Tejun Heoac7136b2011-02-16 17:11:09 +0100259
Tejun Heoeb8c1e22011-03-02 11:32:47 +0100260 /* numa_distance could be 1LU marking allocation failure, test cnt */
Yinghai Luce003332011-03-02 11:22:14 +0100261 if (numa_distance_cnt)
Yinghai Lu2ca230b2011-02-17 14:46:37 +0100262 memblock_x86_free_range(__pa(numa_distance),
263 __pa(numa_distance) + size);
Yinghai Luce003332011-03-02 11:22:14 +0100264 numa_distance_cnt = 0;
Tejun Heoeb8c1e22011-03-02 11:32:47 +0100265 numa_distance = NULL; /* enable table creation */
Tejun Heoac7136b2011-02-16 17:11:09 +0100266}
267
Yinghai Lu2bf50552011-02-22 11:18:49 +0100268static int __init numa_alloc_distance(void)
269{
270 nodemask_t nodes_parsed;
271 size_t size;
272 int i, j, cnt = 0;
273 u64 phys;
274
275 /* size the new table and allocate it */
276 nodes_parsed = numa_nodes_parsed;
277 numa_nodemask_from_meminfo(&nodes_parsed, &numa_meminfo);
278
279 for_each_node_mask(i, nodes_parsed)
280 cnt = i;
David Rientjes1f565a82011-02-25 10:06:39 +0100281 cnt++;
282 size = cnt * cnt * sizeof(numa_distance[0]);
Yinghai Lu2bf50552011-02-22 11:18:49 +0100283
284 phys = memblock_find_in_range(0, (u64)max_pfn_mapped << PAGE_SHIFT,
285 size, PAGE_SIZE);
286 if (phys == MEMBLOCK_ERROR) {
287 pr_warning("NUMA: Warning: can't allocate distance table!\n");
288 /* don't retry until explicitly reset */
289 numa_distance = (void *)1LU;
290 return -ENOMEM;
291 }
292 memblock_x86_reserve_range(phys, phys + size, "NUMA DIST");
293
294 numa_distance = __va(phys);
295 numa_distance_cnt = cnt;
296
297 /* fill with the default distances */
298 for (i = 0; i < cnt; i++)
299 for (j = 0; j < cnt; j++)
300 numa_distance[i * cnt + j] = i == j ?
301 LOCAL_DISTANCE : REMOTE_DISTANCE;
302 printk(KERN_DEBUG "NUMA: Initialized distance table, cnt=%d\n", cnt);
303
304 return 0;
305}
306
Tejun Heo90e6b672011-02-22 11:10:08 +0100307/**
308 * numa_set_distance - Set NUMA distance from one NUMA to another
309 * @from: the 'from' node to set distance
310 * @to: the 'to' node to set distance
311 * @distance: NUMA distance
312 *
313 * Set the distance from node @from to @to to @distance. If distance table
Lucas De Marchi0d2eb442011-03-17 16:24:16 -0300314 * doesn't exist, one which is large enough to accommodate all the currently
Tejun Heo90e6b672011-02-22 11:10:08 +0100315 * known nodes will be created.
Tejun Heoeb8c1e22011-03-02 11:32:47 +0100316 *
317 * If such table cannot be allocated, a warning is printed and further
318 * calls are ignored until the distance table is reset with
319 * numa_reset_distance().
320 *
321 * If @from or @to is higher than the highest known node at the time of
322 * table creation or @distance doesn't make sense, the call is ignored.
323 * This is to allow simplification of specific NUMA config implementations.
Tejun Heoac7136b2011-02-16 17:11:09 +0100324 */
325void __init numa_set_distance(int from, int to, int distance)
326{
Yinghai Lu2bf50552011-02-22 11:18:49 +0100327 if (!numa_distance && numa_alloc_distance() < 0)
328 return;
Tejun Heoac7136b2011-02-16 17:11:09 +0100329
330 if (from >= numa_distance_cnt || to >= numa_distance_cnt) {
331 printk_once(KERN_DEBUG "NUMA: Debug: distance out of bound, from=%d to=%d distance=%d\n",
332 from, to, distance);
333 return;
334 }
335
336 if ((u8)distance != distance ||
337 (from == to && distance != LOCAL_DISTANCE)) {
338 pr_warn_once("NUMA: Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
339 from, to, distance);
340 return;
341 }
342
343 numa_distance[from * numa_distance_cnt + to] = distance;
344}
345
346int __node_distance(int from, int to)
347{
Tejun Heoac7136b2011-02-16 17:11:09 +0100348 if (from >= numa_distance_cnt || to >= numa_distance_cnt)
349 return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
350 return numa_distance[from * numa_distance_cnt + to];
351}
352EXPORT_SYMBOL(__node_distance);
353
354/*
Tejun Heof9c60252011-02-16 17:11:09 +0100355 * Sanity check to catch more bad NUMA configurations (they are amazingly
356 * common). Make sure the nodes cover all memory.
357 */
Tejun Heo91556232011-02-16 17:11:09 +0100358static bool __init numa_meminfo_cover_memory(const struct numa_meminfo *mi)
Tejun Heof9c60252011-02-16 17:11:09 +0100359{
360 unsigned long numaram, e820ram;
361 int i;
362
363 numaram = 0;
Tejun Heo91556232011-02-16 17:11:09 +0100364 for (i = 0; i < mi->nr_blks; i++) {
365 unsigned long s = mi->blk[i].start >> PAGE_SHIFT;
366 unsigned long e = mi->blk[i].end >> PAGE_SHIFT;
Tejun Heof9c60252011-02-16 17:11:09 +0100367 numaram += e - s;
Tejun Heo91556232011-02-16 17:11:09 +0100368 numaram -= __absent_pages_in_range(mi->blk[i].nid, s, e);
Tejun Heof9c60252011-02-16 17:11:09 +0100369 if ((long)numaram < 0)
370 numaram = 0;
371 }
372
373 e820ram = max_pfn - (memblock_x86_hole_size(0,
374 max_pfn << PAGE_SHIFT) >> PAGE_SHIFT);
375 /* We seem to lose 3 pages somewhere. Allow 1M of slack. */
376 if ((long)(e820ram - numaram) >= (1 << (20 - PAGE_SHIFT))) {
377 printk(KERN_ERR "NUMA: nodes only cover %luMB of your %luMB e820 RAM. Not used.\n",
378 (numaram << PAGE_SHIFT) >> 20,
379 (e820ram << PAGE_SHIFT) >> 20);
Tejun Heo91556232011-02-16 17:11:09 +0100380 return false;
Tejun Heof9c60252011-02-16 17:11:09 +0100381 }
Tejun Heo91556232011-02-16 17:11:09 +0100382 return true;
Tejun Heof9c60252011-02-16 17:11:09 +0100383}
384
385static int __init numa_register_memblks(struct numa_meminfo *mi)
386{
Yinghai Lu69efcc62011-02-21 10:58:13 +0100387 int i, nid;
Tejun Heof9c60252011-02-16 17:11:09 +0100388
389 /* Account for nodes with cpus and no memory */
Tejun Heo4697bdc2011-02-16 17:11:09 +0100390 node_possible_map = numa_nodes_parsed;
391 numa_nodemask_from_meminfo(&node_possible_map, mi);
Tejun Heof9c60252011-02-16 17:11:09 +0100392 if (WARN_ON(nodes_empty(node_possible_map)))
393 return -EINVAL;
394
Tejun Heo97e7b782011-02-16 17:11:08 +0100395 for (i = 0; i < mi->nr_blks; i++)
396 memblock_x86_register_active_regions(mi->blk[i].nid,
397 mi->blk[i].start >> PAGE_SHIFT,
398 mi->blk[i].end >> PAGE_SHIFT);
Tejun Heofd0435d2011-02-16 17:11:08 +0100399
400 /* for out of order entries */
401 sort_node_map();
Tejun Heo91556232011-02-16 17:11:09 +0100402 if (!numa_meminfo_cover_memory(mi))
Tejun Heofd0435d2011-02-16 17:11:08 +0100403 return -EINVAL;
404
Yinghai Lu69efcc62011-02-21 10:58:13 +0100405 /* Finally register nodes. */
406 for_each_node_mask(nid, node_possible_map) {
407 u64 start = (u64)max_pfn << PAGE_SHIFT;
408 u64 end = 0;
Tejun Heo91556232011-02-16 17:11:09 +0100409
Yinghai Lu69efcc62011-02-21 10:58:13 +0100410 for (i = 0; i < mi->nr_blks; i++) {
411 if (nid != mi->blk[i].nid)
Tejun Heo91556232011-02-16 17:11:09 +0100412 continue;
Yinghai Lu69efcc62011-02-21 10:58:13 +0100413 start = min(mi->blk[i].start, start);
414 end = max(mi->blk[i].end, end);
Tejun Heo91556232011-02-16 17:11:09 +0100415 }
Yinghai Lu69efcc62011-02-21 10:58:13 +0100416
417 if (start < end)
418 setup_node_bootmem(nid, start, end);
Tejun Heo91556232011-02-16 17:11:09 +0100419 }
Tejun Heofd0435d2011-02-16 17:11:08 +0100420
Tejun Heoef396ec2011-02-16 17:11:07 +0100421 return 0;
422}
423
David Rientjesc09cedf2011-03-04 15:17:21 +0100424/**
425 * dummy_numma_init - Fallback dummy NUMA init
426 *
427 * Used if there's no underlying NUMA architecture, NUMA initialization
428 * fails, or NUMA is disabled on the command line.
429 *
430 * Must online at least one node and add memory blocks that cover all
431 * allowed memory. This function must not fail.
432 */
Yinghai Lu6d496f92011-02-17 14:53:20 +0100433static int __init dummy_numa_init(void)
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100434{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 printk(KERN_INFO "%s\n",
436 numa_off ? "NUMA turned off" : "No NUMA configuration found");
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100437 printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
Tejun Heo86ef4db2011-02-16 12:13:06 +0100438 0LU, max_pfn << PAGE_SHIFT);
Tejun Heoffe77a42011-02-16 12:13:06 +0100439
Tejun Heo92d4a432011-02-16 17:11:09 +0100440 node_set(0, numa_nodes_parsed);
Tejun Heo43a662f2011-02-16 17:11:08 +0100441 numa_add_memblk(0, 0, (u64)max_pfn << PAGE_SHIFT);
Tejun Heoec8cf29b2011-02-16 12:13:07 +0100442
443 return 0;
444}
445
David Rientjesc09cedf2011-03-04 15:17:21 +0100446static int __init numa_init(int (*init_func)(void))
447{
448 int i;
449 int ret;
450
451 for (i = 0; i < MAX_LOCAL_APIC; i++)
452 set_apicid_to_node(i, NUMA_NO_NODE);
453
454 nodes_clear(numa_nodes_parsed);
455 nodes_clear(node_possible_map);
456 nodes_clear(node_online_map);
457 memset(&numa_meminfo, 0, sizeof(numa_meminfo));
458 remove_all_active_ranges();
459 numa_reset_distance();
460
461 ret = init_func();
462 if (ret < 0)
463 return ret;
464 ret = numa_cleanup_meminfo(&numa_meminfo);
465 if (ret < 0)
466 return ret;
467
468 numa_emulation(&numa_meminfo, numa_distance_cnt);
469
470 ret = numa_register_memblks(&numa_meminfo);
471 if (ret < 0)
472 return ret;
473
474 for (i = 0; i < nr_cpu_ids; i++) {
475 int nid = early_cpu_to_node(i);
476
477 if (nid == NUMA_NO_NODE)
478 continue;
479 if (!node_online(nid))
480 numa_clear_node(i);
481 }
482 numa_init_array();
483 return 0;
484}
485
Tejun Heoffe77a42011-02-16 12:13:06 +0100486void __init initmem_init(void)
487{
Tejun Heoffe77a42011-02-16 12:13:06 +0100488 if (!numa_off) {
489#ifdef CONFIG_ACPI_NUMA
Florian Mickler711b8c82011-04-04 01:17:40 +0200490 if (!numa_init(x86_acpi_numa_init))
David Rientjesc09cedf2011-03-04 15:17:21 +0100491 return;
Tejun Heoffe77a42011-02-16 12:13:06 +0100492#endif
493#ifdef CONFIG_AMD_NUMA
Florian Mickler711b8c82011-04-04 01:17:40 +0200494 if (!numa_init(amd_numa_init))
David Rientjesc09cedf2011-03-04 15:17:21 +0100495 return;
Tejun Heoffe77a42011-02-16 12:13:06 +0100496#endif
497 }
498
David Rientjesc09cedf2011-03-04 15:17:21 +0100499 numa_init(dummy_numa_init);
Andi Kleen69d81fc2005-11-05 17:25:53 +0100500}
501
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100502unsigned long __init numa_free_all_bootmem(void)
503{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 unsigned long pages = 0;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100505 int i;
506
507 for_each_online_node(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 pages += free_all_bootmem_node(NODE_DATA(i));
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100509
Yinghai Lu08677212010-02-10 01:20:20 -0800510 pages += free_all_memory_core_early(MAX_NUMNODES);
Yinghai Lu08677212010-02-10 01:20:20 -0800511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return pages;
Thomas Gleixnere3cfe522008-01-30 13:30:37 +0100513}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100515int __cpuinit numa_cpu_node(int cpu)
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800516{
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100517 int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800518
Tejun Heobbc9e2f2011-01-23 14:37:39 +0100519 if (apicid != BAD_APICID)
520 return __apicid_to_node[apicid];
521 return NUMA_NO_NODE;
Yinghai Lud9c2d5a2009-11-21 00:23:37 -0800522}
Tejun Heo96886782011-05-02 14:18:51 +0200523
524#ifdef CONFIG_MEMORY_HOTPLUG
525int memory_add_physaddr_to_nid(u64 start)
526{
527 struct numa_meminfo *mi = &numa_meminfo;
528 int nid = mi->blk[0].nid;
529 int i;
530
531 for (i = 0; i < mi->nr_blks; i++)
532 if (mi->blk[i].start <= start && mi->blk[i].end > start)
533 nid = mi->blk[i].nid;
534 return nid;
535}
536EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
537#endif