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>
32 #include <asm/setup.h>
33 #include <asm/mmzone.h>
35 struct pglist_data *node_data[MAX_NUMNODES];
36 bootmem_data_t node0_bdata;
39 * numa interface - we expect the numa architecture specfic code to have
40 * populated the following initialisation.
42 * 1) numnodes - the total number of nodes configured in the system
43 * 2) physnode_map - the mapping between a pfn and owning node
44 * 3) node_start_pfn - the starting page frame number for a node
45 * 3) node_end_pfn - the ending page fram number for a node
49 * physnode_map keeps track of the physical memory layout of a generic
50 * numa node on a 256Mb break (each element of the array will
51 * represent 256Mb of memory and will be marked by the node id. so,
52 * if the first gig is on node 0, and the second gig is on node 1
53 * physnode_map will contain:
55 * physnode_map[0-3] = 0;
56 * physnode_map[4-7] = 1;
57 * physnode_map[8- ] = -1;
59 s8 physnode_map[MAX_ELEMENTS] = { [0 ... (MAX_ELEMENTS - 1)] = -1};
61 unsigned long node_start_pfn[MAX_NUMNODES];
62 unsigned long node_end_pfn[MAX_NUMNODES];
64 extern unsigned long find_max_low_pfn(void);
65 extern void find_max_pfn(void);
66 extern void one_highpage_init(struct page *, int, int);
68 extern struct e820map e820;
69 extern unsigned long init_pg_tables_end;
70 extern unsigned long highend_pfn, highstart_pfn;
71 extern unsigned long max_low_pfn;
72 extern unsigned long totalram_pages;
73 extern unsigned long totalhigh_pages;
75 #define LARGE_PAGE_BYTES (PTRS_PER_PTE * PAGE_SIZE)
77 unsigned long node_remap_start_pfn[MAX_NUMNODES];
78 unsigned long node_remap_size[MAX_NUMNODES];
79 unsigned long node_remap_offset[MAX_NUMNODES];
80 void *node_remap_start_vaddr[MAX_NUMNODES];
81 void set_pmd_pfn(unsigned long vaddr, unsigned long pfn, pgprot_t flags);
84 * FLAT - support for basic PC memory model with discontig enabled, essentially
85 * a single node with all available processors in it with a flat
88 int __init get_memcfg_numa_flat(void)
90 printk("NUMA - single node, flat memory mode\n");
92 /* Run the memory configuration and find the top of memory. */
94 node_start_pfn[0] = 0;
95 node_end_pfn[0] = max_pfn;
97 /* Indicate there is one node available. */
104 * Find the highest page frame number we have available for the node
106 static void __init find_max_pfn_node(int nid)
108 if (node_end_pfn[nid] > max_pfn)
109 node_end_pfn[nid] = max_pfn;
111 * if a user has given mem=XXXX, then we need to make sure
112 * that the node _starts_ before that, too, not just ends
114 if (node_start_pfn[nid] > max_pfn)
115 node_start_pfn[nid] = max_pfn;
116 if (node_start_pfn[nid] > node_end_pfn[nid])
121 * Allocate memory for the pg_data_t for this node via a crude pre-bootmem
122 * method. For node zero take this from the bottom of memory, for
123 * subsequent nodes place them at node_remap_start_vaddr which contains
124 * node local data in physically node local memory. See setup_memory()
127 static void __init allocate_pgdat(int nid)
130 NODE_DATA(nid) = (pg_data_t *)node_remap_start_vaddr[nid];
132 NODE_DATA(nid) = (pg_data_t *)(__va(min_low_pfn << PAGE_SHIFT));
133 min_low_pfn += PFN_UP(sizeof(pg_data_t));
134 memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
139 * Register fully available low RAM pages with the bootmem allocator.
141 static void __init register_bootmem_low_pages(unsigned long system_max_low_pfn)
145 for (i = 0; i < e820.nr_map; i++) {
146 unsigned long curr_pfn, last_pfn, size;
148 * Reserve usable low memory
150 if (e820.map[i].type != E820_RAM)
153 * We are rounding up the start address of usable memory:
155 curr_pfn = PFN_UP(e820.map[i].addr);
156 if (curr_pfn >= system_max_low_pfn)
159 * ... and at the end of the usable range downwards:
161 last_pfn = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
163 if (last_pfn > system_max_low_pfn)
164 last_pfn = system_max_low_pfn;
167 * .. finally, did all the rounding and playing
168 * around just make the area go away?
170 if (last_pfn <= curr_pfn)
173 size = last_pfn - curr_pfn;
174 free_bootmem_node(NODE_DATA(0), PFN_PHYS(curr_pfn), PFN_PHYS(size));
178 void __init remap_numa_kva(void)
184 for (node = 1; node < numnodes; ++node) {
185 for (pfn=0; pfn < node_remap_size[node]; pfn += PTRS_PER_PTE) {
186 vaddr = node_remap_start_vaddr[node]+(pfn<<PAGE_SHIFT);
187 set_pmd_pfn((ulong) vaddr,
188 node_remap_start_pfn[node] + pfn,
194 static unsigned long calculate_numa_remap_pages(void)
197 unsigned long size, reserve_pages = 0;
199 for (nid = 1; nid < numnodes; nid++) {
200 /* calculate the size of the mem_map needed in bytes */
201 size = (node_end_pfn[nid] - node_start_pfn[nid] + 1)
202 * sizeof(struct page) + sizeof(pg_data_t);
203 /* convert size to large (pmd size) pages, rounding up */
204 size = (size + LARGE_PAGE_BYTES - 1) / LARGE_PAGE_BYTES;
205 /* now the roundup is correct, convert to PAGE_SIZE pages */
206 size = size * PTRS_PER_PTE;
207 printk("Reserving %ld pages of KVA for lmem_map of node %d\n",
209 node_remap_size[nid] = size;
210 reserve_pages += size;
211 node_remap_offset[nid] = reserve_pages;
212 printk("Shrinking node %d from %ld pages to %ld pages\n",
213 nid, node_end_pfn[nid], node_end_pfn[nid] - size);
214 node_end_pfn[nid] -= size;
215 node_remap_start_pfn[nid] = node_end_pfn[nid];
217 printk("Reserving total of %ld pages for numa KVA remap\n",
219 return reserve_pages;
222 unsigned long __init setup_memory(void)
225 unsigned long bootmap_size, system_start_pfn, system_max_low_pfn;
226 unsigned long reserve_pages, pfn;
229 * When mapping a NUMA machine we allocate the node_mem_map arrays
230 * from node local memory. They are then mapped directly into KVA
231 * between zone normal and vmalloc space. Calculate the size of
232 * this space and use it to adjust the boundry between ZONE_NORMAL
237 /* Fill in the physnode_map */
238 for (nid = 0; nid < numnodes; nid++) {
239 printk("Node: %d, start_pfn: %ld, end_pfn: %ld\n",
240 nid, node_start_pfn[nid], node_end_pfn[nid]);
241 printk(" Setting physnode_map array to node %d for pfns:\n ",
243 for (pfn = node_start_pfn[nid]; pfn < node_end_pfn[nid];
244 pfn += PAGES_PER_ELEMENT) {
245 physnode_map[pfn / PAGES_PER_ELEMENT] = nid;
251 reserve_pages = calculate_numa_remap_pages();
253 /* partially used pages are not usable - thus round upwards */
254 system_start_pfn = min_low_pfn = PFN_UP(init_pg_tables_end);
257 system_max_low_pfn = max_low_pfn = find_max_low_pfn() - reserve_pages;
258 printk("reserve_pages = %ld find_max_low_pfn() ~ %ld\n",
259 reserve_pages, max_low_pfn + reserve_pages);
260 printk("max_pfn = %ld\n", max_pfn);
261 #ifdef CONFIG_HIGHMEM
262 highstart_pfn = highend_pfn = max_pfn;
263 if (max_pfn > system_max_low_pfn)
264 highstart_pfn = system_max_low_pfn;
265 printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
266 pages_to_mb(highend_pfn - highstart_pfn));
268 printk(KERN_NOTICE "%ldMB LOWMEM available.\n",
269 pages_to_mb(system_max_low_pfn));
270 printk("min_low_pfn = %ld, max_low_pfn = %ld, highstart_pfn = %ld\n",
271 min_low_pfn, max_low_pfn, highstart_pfn);
273 printk("Low memory ends at vaddr %08lx\n",
274 (ulong) pfn_to_kaddr(max_low_pfn));
275 for (nid = 0; nid < numnodes; nid++) {
276 node_remap_start_vaddr[nid] = pfn_to_kaddr(
277 (highstart_pfn + reserve_pages) - node_remap_offset[nid]);
279 printk ("node %d will remap to vaddr %08lx - %08lx\n", nid,
280 (ulong) node_remap_start_vaddr[nid],
281 (ulong) pfn_to_kaddr(highstart_pfn + reserve_pages
282 - node_remap_offset[nid] + node_remap_size[nid]));
284 printk("High memory starts at vaddr %08lx\n",
285 (ulong) pfn_to_kaddr(highstart_pfn));
286 vmalloc_earlyreserve = reserve_pages * PAGE_SIZE;
287 for (nid = 0; nid < numnodes; nid++)
288 find_max_pfn_node(nid);
290 NODE_DATA(0)->bdata = &node0_bdata;
293 * Initialize the boot-time allocator (with low memory only):
295 bootmap_size = init_bootmem_node(NODE_DATA(0), min_low_pfn, 0, system_max_low_pfn);
297 register_bootmem_low_pages(system_max_low_pfn);
300 * Reserve the bootmem bitmap itself as well. We do this in two
301 * steps (first step was init_bootmem()) because this catches
302 * the (very unlikely) case of us accidentally initializing the
303 * bootmem allocator with an invalid RAM area.
305 reserve_bootmem_node(NODE_DATA(0), HIGH_MEMORY, (PFN_PHYS(min_low_pfn) +
306 bootmap_size + PAGE_SIZE-1) - (HIGH_MEMORY));
309 * reserve physical page 0 - it's a special BIOS page on many boxes,
310 * enabling clean reboots, SMP operation, laptop functions.
312 reserve_bootmem_node(NODE_DATA(0), 0, PAGE_SIZE);
315 * But first pinch a few for the stack/trampoline stuff
316 * FIXME: Don't need the extra page at 4K, but need to fix
317 * trampoline before removing it. (see the GDT stuff)
319 reserve_bootmem_node(NODE_DATA(0), PAGE_SIZE, PAGE_SIZE);
321 #ifdef CONFIG_ACPI_SLEEP
323 * Reserve low memory region for sleep support.
325 acpi_reserve_bootmem();
329 * Find and reserve possible boot-time SMP configuration:
333 #ifdef CONFIG_BLK_DEV_INITRD
334 if (LOADER_TYPE && INITRD_START) {
335 if (INITRD_START + INITRD_SIZE <= (system_max_low_pfn << PAGE_SHIFT)) {
336 reserve_bootmem_node(NODE_DATA(0), INITRD_START, INITRD_SIZE);
338 INITRD_START ? INITRD_START + PAGE_OFFSET : 0;
339 initrd_end = initrd_start+INITRD_SIZE;
342 printk(KERN_ERR "initrd extends beyond end of memory "
343 "(0x%08lx > 0x%08lx)\ndisabling initrd\n",
344 INITRD_START + INITRD_SIZE,
345 system_max_low_pfn << PAGE_SHIFT);
350 return system_max_low_pfn;
353 void __init zone_sizes_init(void)
358 * Insert nodes into pgdat_list backward so they appear in order.
359 * Clobber node 0's links and NULL out pgdat_list before starting.
362 for (nid = numnodes - 1; nid >= 0; nid--) {
364 memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
365 NODE_DATA(nid)->pgdat_next = pgdat_list;
366 pgdat_list = NODE_DATA(nid);
369 for (nid = 0; nid < numnodes; nid++) {
370 unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0};
371 unsigned long *zholes_size;
372 unsigned int max_dma;
374 unsigned long low = max_low_pfn;
375 unsigned long start = node_start_pfn[nid];
376 unsigned long high = node_end_pfn[nid];
378 max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
381 #ifdef CONFIG_HIGHMEM
382 BUG_ON(start > high);
383 zones_size[ZONE_HIGHMEM] = high - start;
387 zones_size[ZONE_DMA] = low;
389 BUG_ON(max_dma > low);
391 zones_size[ZONE_DMA] = max_dma;
392 zones_size[ZONE_NORMAL] = low - max_dma;
393 #ifdef CONFIG_HIGHMEM
394 zones_size[ZONE_HIGHMEM] = high - low;
398 zholes_size = get_zholes_size(nid);
400 * We let the lmem_map for node 0 be allocated from the
401 * normal bootmem allocator, but other nodes come from the
402 * remapped KVA area - mbligh
405 free_area_init_node(nid, NODE_DATA(nid), 0,
406 zones_size, start, zholes_size);
408 unsigned long lmem_map;
409 lmem_map = (unsigned long)node_remap_start_vaddr[nid];
410 lmem_map += sizeof(pg_data_t) + PAGE_SIZE - 1;
411 lmem_map &= PAGE_MASK;
412 free_area_init_node(nid, NODE_DATA(nid),
413 (struct page *)lmem_map, zones_size,
420 void __init set_highmem_pages_init(int bad_ppro)
422 #ifdef CONFIG_HIGHMEM
425 for_each_zone(zone) {
426 unsigned long node_pfn, node_high_size, zone_start_pfn;
427 struct page * zone_mem_map;
429 if (!is_highmem(zone))
432 printk("Initializing %s for node %d\n", zone->name,
433 zone->zone_pgdat->node_id);
435 node_high_size = zone->spanned_pages;
436 zone_mem_map = zone->zone_mem_map;
437 zone_start_pfn = zone->zone_start_pfn;
439 for (node_pfn = 0; node_pfn < node_high_size; node_pfn++) {
440 one_highpage_init((struct page *)(zone_mem_map + node_pfn),
441 zone_start_pfn + node_pfn, bad_ppro);
444 totalram_pages += totalhigh_pages;
448 void __init set_max_mapnr_init(void)
450 #ifdef CONFIG_HIGHMEM
451 struct zone *high0 = &NODE_DATA(0)->node_zones[ZONE_HIGHMEM];
452 if (high0->spanned_pages > 0)
453 highmem_start_page = high0->zone_mem_map;
455 highmem_start_page = pfn_to_page(max_low_pfn+1);
456 num_physpages = highend_pfn;
458 num_physpages = max_low_pfn;