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) numnodes - the total number of nodes configured 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. */
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 (node = 1; node < numnodes; ++node) {
187 for (pfn=0; pfn < node_remap_size[node]; pfn += PTRS_PER_PTE) {
188 vaddr = node_remap_start_vaddr[node]+(pfn<<PAGE_SHIFT);
189 set_pmd_pfn((ulong) vaddr,
190 node_remap_start_pfn[node] + pfn,
196 static unsigned long calculate_numa_remap_pages(void)
199 unsigned long size, reserve_pages = 0;
201 for (nid = 1; nid < numnodes; nid++) {
202 /* calculate the size of the mem_map needed in bytes */
203 size = (node_end_pfn[nid] - node_start_pfn[nid] + 1)
204 * sizeof(struct page) + sizeof(pg_data_t);
205 /* convert size to large (pmd size) pages, rounding up */
206 size = (size + LARGE_PAGE_BYTES - 1) / LARGE_PAGE_BYTES;
207 /* now the roundup is correct, convert to PAGE_SIZE pages */
208 size = size * PTRS_PER_PTE;
209 printk("Reserving %ld pages of KVA for lmem_map of node %d\n",
211 node_remap_size[nid] = size;
212 reserve_pages += size;
213 node_remap_offset[nid] = reserve_pages;
214 printk("Shrinking node %d from %ld pages to %ld pages\n",
215 nid, node_end_pfn[nid], node_end_pfn[nid] - size);
216 node_end_pfn[nid] -= size;
217 node_remap_start_pfn[nid] = node_end_pfn[nid];
219 printk("Reserving total of %ld pages for numa KVA remap\n",
221 return reserve_pages;
225 * workaround for Dell systems that neglect to reserve EBDA
227 static void __init reserve_ebda_region_node(void)
230 addr = get_bios_ebda();
232 reserve_bootmem_node(NODE_DATA(0), addr, PAGE_SIZE);
235 unsigned long __init setup_memory(void)
238 unsigned long bootmap_size, system_start_pfn, system_max_low_pfn;
239 unsigned long reserve_pages, pfn;
242 * When mapping a NUMA machine we allocate the node_mem_map arrays
243 * from node local memory. They are then mapped directly into KVA
244 * between zone normal and vmalloc space. Calculate the size of
245 * this space and use it to adjust the boundry between ZONE_NORMAL
250 /* Fill in the physnode_map */
251 for (nid = 0; nid < numnodes; nid++) {
252 printk("Node: %d, start_pfn: %ld, end_pfn: %ld\n",
253 nid, node_start_pfn[nid], node_end_pfn[nid]);
254 printk(" Setting physnode_map array to node %d for pfns:\n ",
256 for (pfn = node_start_pfn[nid]; pfn < node_end_pfn[nid];
257 pfn += PAGES_PER_ELEMENT) {
258 physnode_map[pfn / PAGES_PER_ELEMENT] = nid;
264 reserve_pages = calculate_numa_remap_pages();
266 /* partially used pages are not usable - thus round upwards */
267 system_start_pfn = min_low_pfn = PFN_UP(init_pg_tables_end);
270 system_max_low_pfn = max_low_pfn = find_max_low_pfn() - reserve_pages;
271 printk("reserve_pages = %ld find_max_low_pfn() ~ %ld\n",
272 reserve_pages, max_low_pfn + reserve_pages);
273 printk("max_pfn = %ld\n", max_pfn);
274 #ifdef CONFIG_HIGHMEM
275 highstart_pfn = highend_pfn = max_pfn;
276 if (max_pfn > system_max_low_pfn)
277 highstart_pfn = system_max_low_pfn;
278 printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
279 pages_to_mb(highend_pfn - highstart_pfn));
281 printk(KERN_NOTICE "%ldMB LOWMEM available.\n",
282 pages_to_mb(system_max_low_pfn));
283 printk("min_low_pfn = %ld, max_low_pfn = %ld, highstart_pfn = %ld\n",
284 min_low_pfn, max_low_pfn, highstart_pfn);
286 printk("Low memory ends at vaddr %08lx\n",
287 (ulong) pfn_to_kaddr(max_low_pfn));
288 for (nid = 0; nid < numnodes; nid++) {
289 node_remap_start_vaddr[nid] = pfn_to_kaddr(
290 (highstart_pfn + reserve_pages) - node_remap_offset[nid]);
292 printk ("node %d will remap to vaddr %08lx - %08lx\n", nid,
293 (ulong) node_remap_start_vaddr[nid],
294 (ulong) pfn_to_kaddr(highstart_pfn + reserve_pages
295 - node_remap_offset[nid] + node_remap_size[nid]));
297 printk("High memory starts at vaddr %08lx\n",
298 (ulong) pfn_to_kaddr(highstart_pfn));
299 vmalloc_earlyreserve = reserve_pages * PAGE_SIZE;
300 for (nid = 0; nid < numnodes; nid++)
301 find_max_pfn_node(nid);
303 NODE_DATA(0)->bdata = &node0_bdata;
306 * Initialize the boot-time allocator (with low memory only):
308 bootmap_size = init_bootmem_node(NODE_DATA(0), min_low_pfn, 0, system_max_low_pfn);
310 register_bootmem_low_pages(system_max_low_pfn);
313 * Reserve the bootmem bitmap itself as well. We do this in two
314 * steps (first step was init_bootmem()) because this catches
315 * the (very unlikely) case of us accidentally initializing the
316 * bootmem allocator with an invalid RAM area.
318 reserve_bootmem_node(NODE_DATA(0), HIGH_MEMORY, (PFN_PHYS(min_low_pfn) +
319 bootmap_size + PAGE_SIZE-1) - (HIGH_MEMORY));
322 * reserve physical page 0 - it's a special BIOS page on many boxes,
323 * enabling clean reboots, SMP operation, laptop functions.
325 reserve_bootmem_node(NODE_DATA(0), 0, PAGE_SIZE);
328 * But first pinch a few for the stack/trampoline stuff
329 * FIXME: Don't need the extra page at 4K, but need to fix
330 * trampoline before removing it. (see the GDT stuff)
332 reserve_bootmem_node(NODE_DATA(0), PAGE_SIZE, PAGE_SIZE);
334 /* reserve EBDA region, it's a 4K region */
335 reserve_ebda_region_node();
337 #ifdef CONFIG_ACPI_SLEEP
339 * Reserve low memory region for sleep support.
341 acpi_reserve_bootmem();
345 * Find and reserve possible boot-time SMP configuration:
349 #ifdef CONFIG_BLK_DEV_INITRD
350 if (LOADER_TYPE && INITRD_START) {
351 if (INITRD_START + INITRD_SIZE <= (system_max_low_pfn << PAGE_SHIFT)) {
352 reserve_bootmem_node(NODE_DATA(0), INITRD_START, INITRD_SIZE);
354 INITRD_START ? INITRD_START + PAGE_OFFSET : 0;
355 initrd_end = initrd_start+INITRD_SIZE;
358 printk(KERN_ERR "initrd extends beyond end of memory "
359 "(0x%08lx > 0x%08lx)\ndisabling initrd\n",
360 INITRD_START + INITRD_SIZE,
361 system_max_low_pfn << PAGE_SHIFT);
366 return system_max_low_pfn;
369 void __init zone_sizes_init(void)
374 * Insert nodes into pgdat_list backward so they appear in order.
375 * Clobber node 0's links and NULL out pgdat_list before starting.
378 for (nid = numnodes - 1; nid >= 0; nid--) {
380 memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
381 NODE_DATA(nid)->pgdat_next = pgdat_list;
382 pgdat_list = NODE_DATA(nid);
385 for (nid = 0; nid < numnodes; nid++) {
386 unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0};
387 unsigned long *zholes_size;
388 unsigned int max_dma;
390 unsigned long low = max_low_pfn;
391 unsigned long start = node_start_pfn[nid];
392 unsigned long high = node_end_pfn[nid];
394 max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
397 #ifdef CONFIG_HIGHMEM
398 BUG_ON(start > high);
399 zones_size[ZONE_HIGHMEM] = high - start;
403 zones_size[ZONE_DMA] = low;
405 BUG_ON(max_dma > low);
407 zones_size[ZONE_DMA] = max_dma;
408 zones_size[ZONE_NORMAL] = low - max_dma;
409 #ifdef CONFIG_HIGHMEM
410 zones_size[ZONE_HIGHMEM] = high - low;
414 zholes_size = get_zholes_size(nid);
416 * We let the lmem_map for node 0 be allocated from the
417 * normal bootmem allocator, but other nodes come from the
418 * remapped KVA area - mbligh
421 free_area_init_node(nid, NODE_DATA(nid),
422 zones_size, start, zholes_size);
424 unsigned long lmem_map;
425 lmem_map = (unsigned long)node_remap_start_vaddr[nid];
426 lmem_map += sizeof(pg_data_t) + PAGE_SIZE - 1;
427 lmem_map &= PAGE_MASK;
428 NODE_DATA(nid)->node_mem_map = (struct page *)lmem_map;
429 free_area_init_node(nid, NODE_DATA(nid), zones_size,
436 void __init set_highmem_pages_init(int bad_ppro)
438 #ifdef CONFIG_HIGHMEM
441 for_each_zone(zone) {
442 unsigned long node_pfn, node_high_size, zone_start_pfn;
443 struct page * zone_mem_map;
445 if (!is_highmem(zone))
448 printk("Initializing %s for node %d\n", zone->name,
449 zone->zone_pgdat->node_id);
451 node_high_size = zone->spanned_pages;
452 zone_mem_map = zone->zone_mem_map;
453 zone_start_pfn = zone->zone_start_pfn;
455 for (node_pfn = 0; node_pfn < node_high_size; node_pfn++) {
456 one_highpage_init((struct page *)(zone_mem_map + node_pfn),
457 zone_start_pfn + node_pfn, bad_ppro);
460 totalram_pages += totalhigh_pages;
464 void __init set_max_mapnr_init(void)
466 #ifdef CONFIG_HIGHMEM
467 struct zone *high0 = &NODE_DATA(0)->node_zones[ZONE_HIGHMEM];
468 if (high0->spanned_pages > 0)
469 highmem_start_page = high0->zone_mem_map;
471 highmem_start_page = pfn_to_page(max_low_pfn - 1) + 1;
472 num_physpages = highend_pfn;
474 num_physpages = max_low_pfn;