2 * Written by: Patricia Gaughen <gone@us.ibm.com>, IBM Corporation
3 * August 2002: added remote node KVA remap - Martin J. Bligh
5 * Copyright (C) 2002, IBM Corp.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
17 * NON INFRINGEMENT. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <linux/config.h>
27 #include <linux/bootmem.h>
28 #include <linux/mmzone.h>
29 #include <linux/highmem.h>
30 #include <linux/initrd.h>
31 #include <linux/nodemask.h>
33 #include <asm/setup.h>
34 #include <asm/mmzone.h>
35 #include <bios_ebda.h>
37 struct pglist_data *node_data[MAX_NUMNODES];
38 bootmem_data_t node0_bdata;
41 * numa interface - we expect the numa architecture specfic code to have
42 * populated the following initialisation.
44 * 1) node_online_map - the map of all nodes configured (online) in the system
45 * 2) physnode_map - the mapping between a pfn and owning node
46 * 3) node_start_pfn - the starting page frame number for a node
47 * 3) node_end_pfn - the ending page fram number for a node
51 * physnode_map keeps track of the physical memory layout of a generic
52 * numa node on a 256Mb break (each element of the array will
53 * represent 256Mb of memory and will be marked by the node id. so,
54 * if the first gig is on node 0, and the second gig is on node 1
55 * physnode_map will contain:
57 * physnode_map[0-3] = 0;
58 * physnode_map[4-7] = 1;
59 * physnode_map[8- ] = -1;
61 s8 physnode_map[MAX_ELEMENTS] = { [0 ... (MAX_ELEMENTS - 1)] = -1};
63 unsigned long node_start_pfn[MAX_NUMNODES];
64 unsigned long node_end_pfn[MAX_NUMNODES];
66 extern unsigned long find_max_low_pfn(void);
67 extern void find_max_pfn(void);
68 extern void one_highpage_init(struct page *, int, int);
70 extern struct e820map e820;
71 extern unsigned long init_pg_tables_end;
72 extern unsigned long highend_pfn, highstart_pfn;
73 extern unsigned long max_low_pfn;
74 extern unsigned long totalram_pages;
75 extern unsigned long totalhigh_pages;
77 #define LARGE_PAGE_BYTES (PTRS_PER_PTE * PAGE_SIZE)
79 unsigned long node_remap_start_pfn[MAX_NUMNODES];
80 unsigned long node_remap_size[MAX_NUMNODES];
81 unsigned long node_remap_offset[MAX_NUMNODES];
82 void *node_remap_start_vaddr[MAX_NUMNODES];
83 void set_pmd_pfn(unsigned long vaddr, unsigned long pfn, pgprot_t flags);
86 * FLAT - support for basic PC memory model with discontig enabled, essentially
87 * a single node with all available processors in it with a flat
90 int __init get_memcfg_numa_flat(void)
92 printk("NUMA - single node, flat memory mode\n");
94 /* Run the memory configuration and find the top of memory. */
96 node_start_pfn[0] = 0;
97 node_end_pfn[0] = max_pfn;
99 /* Indicate there is one node available. */
100 nodes_clear(node_online_map);
106 * Find the highest page frame number we have available for the node
108 static void __init find_max_pfn_node(int nid)
110 if (node_end_pfn[nid] > max_pfn)
111 node_end_pfn[nid] = max_pfn;
113 * if a user has given mem=XXXX, then we need to make sure
114 * that the node _starts_ before that, too, not just ends
116 if (node_start_pfn[nid] > max_pfn)
117 node_start_pfn[nid] = max_pfn;
118 if (node_start_pfn[nid] > node_end_pfn[nid])
123 * Allocate memory for the pg_data_t for this node via a crude pre-bootmem
124 * method. For node zero take this from the bottom of memory, for
125 * subsequent nodes place them at node_remap_start_vaddr which contains
126 * node local data in physically node local memory. See setup_memory()
129 static void __init allocate_pgdat(int nid)
132 NODE_DATA(nid) = (pg_data_t *)node_remap_start_vaddr[nid];
134 NODE_DATA(nid) = (pg_data_t *)(__va(min_low_pfn << PAGE_SHIFT));
135 min_low_pfn += PFN_UP(sizeof(pg_data_t));
136 memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
141 * Register fully available low RAM pages with the bootmem allocator.
143 static void __init register_bootmem_low_pages(unsigned long system_max_low_pfn)
147 for (i = 0; i < e820.nr_map; i++) {
148 unsigned long curr_pfn, last_pfn, size;
150 * Reserve usable low memory
152 if (e820.map[i].type != E820_RAM)
155 * We are rounding up the start address of usable memory:
157 curr_pfn = PFN_UP(e820.map[i].addr);
158 if (curr_pfn >= system_max_low_pfn)
161 * ... and at the end of the usable range downwards:
163 last_pfn = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
165 if (last_pfn > system_max_low_pfn)
166 last_pfn = system_max_low_pfn;
169 * .. finally, did all the rounding and playing
170 * around just make the area go away?
172 if (last_pfn <= curr_pfn)
175 size = last_pfn - curr_pfn;
176 free_bootmem_node(NODE_DATA(0), PFN_PHYS(curr_pfn), PFN_PHYS(size));
180 void __init remap_numa_kva(void)
186 for_each_online_node(node) {
189 for (pfn=0; pfn < node_remap_size[node]; pfn += PTRS_PER_PTE) {
190 vaddr = node_remap_start_vaddr[node]+(pfn<<PAGE_SHIFT);
191 set_pmd_pfn((ulong) vaddr,
192 node_remap_start_pfn[node] + pfn,
198 static unsigned long calculate_numa_remap_pages(void)
201 unsigned long size, reserve_pages = 0;
203 for_each_online_node(nid) {
206 /* calculate the size of the mem_map needed in bytes */
207 size = (node_end_pfn[nid] - node_start_pfn[nid] + 1)
208 * sizeof(struct page) + sizeof(pg_data_t);
209 /* convert size to large (pmd size) pages, rounding up */
210 size = (size + LARGE_PAGE_BYTES - 1) / LARGE_PAGE_BYTES;
211 /* now the roundup is correct, convert to PAGE_SIZE pages */
212 size = size * PTRS_PER_PTE;
213 printk("Reserving %ld pages of KVA for lmem_map of node %d\n",
215 node_remap_size[nid] = size;
216 reserve_pages += size;
217 node_remap_offset[nid] = reserve_pages;
218 printk("Shrinking node %d from %ld pages to %ld pages\n",
219 nid, node_end_pfn[nid], node_end_pfn[nid] - size);
220 node_end_pfn[nid] -= size;
221 node_remap_start_pfn[nid] = node_end_pfn[nid];
223 printk("Reserving total of %ld pages for numa KVA remap\n",
225 return reserve_pages;
229 * workaround for Dell systems that neglect to reserve EBDA
231 static void __init reserve_ebda_region_node(void)
234 addr = get_bios_ebda();
236 reserve_bootmem_node(NODE_DATA(0), addr, PAGE_SIZE);
239 unsigned long __init setup_memory(void)
242 unsigned long bootmap_size, system_start_pfn, system_max_low_pfn;
243 unsigned long reserve_pages, pfn;
246 * When mapping a NUMA machine we allocate the node_mem_map arrays
247 * from node local memory. They are then mapped directly into KVA
248 * between zone normal and vmalloc space. Calculate the size of
249 * this space and use it to adjust the boundry between ZONE_NORMAL
254 /* Fill in the physnode_map */
255 for_each_online_node(nid) {
256 printk("Node: %d, start_pfn: %ld, end_pfn: %ld\n",
257 nid, node_start_pfn[nid], node_end_pfn[nid]);
258 printk(" Setting physnode_map array to node %d for pfns:\n ",
260 for (pfn = node_start_pfn[nid]; pfn < node_end_pfn[nid];
261 pfn += PAGES_PER_ELEMENT) {
262 physnode_map[pfn / PAGES_PER_ELEMENT] = nid;
268 reserve_pages = calculate_numa_remap_pages();
270 /* partially used pages are not usable - thus round upwards */
271 system_start_pfn = min_low_pfn = PFN_UP(init_pg_tables_end);
274 system_max_low_pfn = max_low_pfn = find_max_low_pfn() - reserve_pages;
275 printk("reserve_pages = %ld find_max_low_pfn() ~ %ld\n",
276 reserve_pages, max_low_pfn + reserve_pages);
277 printk("max_pfn = %ld\n", max_pfn);
278 #ifdef CONFIG_HIGHMEM
279 highstart_pfn = highend_pfn = max_pfn;
280 if (max_pfn > system_max_low_pfn)
281 highstart_pfn = system_max_low_pfn;
282 printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
283 pages_to_mb(highend_pfn - highstart_pfn));
285 printk(KERN_NOTICE "%ldMB LOWMEM available.\n",
286 pages_to_mb(system_max_low_pfn));
287 printk("min_low_pfn = %ld, max_low_pfn = %ld, highstart_pfn = %ld\n",
288 min_low_pfn, max_low_pfn, highstart_pfn);
290 printk("Low memory ends at vaddr %08lx\n",
291 (ulong) pfn_to_kaddr(max_low_pfn));
292 for_each_online_node(nid) {
293 node_remap_start_vaddr[nid] = pfn_to_kaddr(
294 (highstart_pfn + reserve_pages) - node_remap_offset[nid]);
296 printk ("node %d will remap to vaddr %08lx - %08lx\n", nid,
297 (ulong) node_remap_start_vaddr[nid],
298 (ulong) pfn_to_kaddr(highstart_pfn + reserve_pages
299 - node_remap_offset[nid] + node_remap_size[nid]));
301 printk("High memory starts at vaddr %08lx\n",
302 (ulong) pfn_to_kaddr(highstart_pfn));
303 vmalloc_earlyreserve = reserve_pages * PAGE_SIZE;
304 for_each_online_node(nid)
305 find_max_pfn_node(nid);
307 NODE_DATA(0)->bdata = &node0_bdata;
310 * Initialize the boot-time allocator (with low memory only):
312 bootmap_size = init_bootmem_node(NODE_DATA(0), min_low_pfn, 0, system_max_low_pfn);
314 register_bootmem_low_pages(system_max_low_pfn);
317 * Reserve the bootmem bitmap itself as well. We do this in two
318 * steps (first step was init_bootmem()) because this catches
319 * the (very unlikely) case of us accidentally initializing the
320 * bootmem allocator with an invalid RAM area.
322 reserve_bootmem_node(NODE_DATA(0), HIGH_MEMORY, (PFN_PHYS(min_low_pfn) +
323 bootmap_size + PAGE_SIZE-1) - (HIGH_MEMORY));
326 * reserve physical page 0 - it's a special BIOS page on many boxes,
327 * enabling clean reboots, SMP operation, laptop functions.
329 reserve_bootmem_node(NODE_DATA(0), 0, PAGE_SIZE);
332 * But first pinch a few for the stack/trampoline stuff
333 * FIXME: Don't need the extra page at 4K, but need to fix
334 * trampoline before removing it. (see the GDT stuff)
336 reserve_bootmem_node(NODE_DATA(0), PAGE_SIZE, PAGE_SIZE);
338 /* reserve EBDA region, it's a 4K region */
339 reserve_ebda_region_node();
341 #ifdef CONFIG_ACPI_SLEEP
343 * Reserve low memory region for sleep support.
345 acpi_reserve_bootmem();
349 * Find and reserve possible boot-time SMP configuration:
353 #ifdef CONFIG_BLK_DEV_INITRD
354 if (LOADER_TYPE && INITRD_START) {
355 if (INITRD_START + INITRD_SIZE <= (system_max_low_pfn << PAGE_SHIFT)) {
356 reserve_bootmem_node(NODE_DATA(0), INITRD_START, INITRD_SIZE);
358 INITRD_START ? INITRD_START + PAGE_OFFSET : 0;
359 initrd_end = initrd_start+INITRD_SIZE;
362 printk(KERN_ERR "initrd extends beyond end of memory "
363 "(0x%08lx > 0x%08lx)\ndisabling initrd\n",
364 INITRD_START + INITRD_SIZE,
365 system_max_low_pfn << PAGE_SHIFT);
370 return system_max_low_pfn;
373 void __init zone_sizes_init(void)
378 * Insert nodes into pgdat_list backward so they appear in order.
379 * Clobber node 0's links and NULL out pgdat_list before starting.
382 for (nid = MAX_NUMNODES - 1; nid >= 0; nid--) {
383 if (!node_online(nid))
386 memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
387 NODE_DATA(nid)->pgdat_next = pgdat_list;
388 pgdat_list = NODE_DATA(nid);
391 for_each_online_node(nid) {
392 unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0};
393 unsigned long *zholes_size;
394 unsigned int max_dma;
396 unsigned long low = max_low_pfn;
397 unsigned long start = node_start_pfn[nid];
398 unsigned long high = node_end_pfn[nid];
400 max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
403 #ifdef CONFIG_HIGHMEM
404 BUG_ON(start > high);
405 zones_size[ZONE_HIGHMEM] = high - start;
409 zones_size[ZONE_DMA] = low;
411 BUG_ON(max_dma > low);
413 zones_size[ZONE_DMA] = max_dma;
414 zones_size[ZONE_NORMAL] = low - max_dma;
415 #ifdef CONFIG_HIGHMEM
416 zones_size[ZONE_HIGHMEM] = high - low;
420 zholes_size = get_zholes_size(nid);
422 * We let the lmem_map for node 0 be allocated from the
423 * normal bootmem allocator, but other nodes come from the
424 * remapped KVA area - mbligh
427 free_area_init_node(nid, NODE_DATA(nid),
428 zones_size, start, zholes_size);
430 unsigned long lmem_map;
431 lmem_map = (unsigned long)node_remap_start_vaddr[nid];
432 lmem_map += sizeof(pg_data_t) + PAGE_SIZE - 1;
433 lmem_map &= PAGE_MASK;
434 NODE_DATA(nid)->node_mem_map = (struct page *)lmem_map;
435 free_area_init_node(nid, NODE_DATA(nid), zones_size,
442 void __init set_highmem_pages_init(int bad_ppro)
444 #ifdef CONFIG_HIGHMEM
447 for_each_zone(zone) {
448 unsigned long node_pfn, node_high_size, zone_start_pfn;
449 struct page * zone_mem_map;
451 if (!is_highmem(zone))
454 printk("Initializing %s for node %d\n", zone->name,
455 zone->zone_pgdat->node_id);
457 node_high_size = zone->spanned_pages;
458 zone_mem_map = zone->zone_mem_map;
459 zone_start_pfn = zone->zone_start_pfn;
461 for (node_pfn = 0; node_pfn < node_high_size; node_pfn++) {
462 one_highpage_init((struct page *)(zone_mem_map + node_pfn),
463 zone_start_pfn + node_pfn, bad_ppro);
466 totalram_pages += totalhigh_pages;
470 void __init set_max_mapnr_init(void)
472 #ifdef CONFIG_HIGHMEM
473 num_physpages = highend_pfn;
475 num_physpages = max_low_pfn;