upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / arch / i386 / mm / discontig.c
1 /*
2  * Written by: Patricia Gaughen <gone@us.ibm.com>, IBM Corporation
3  * August 2002: added remote node KVA remap - Martin J. Bligh 
4  *
5  * Copyright (C) 2002, IBM Corp.
6  *
7  * All rights reserved.          
8  *
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.
13  *
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
18  * details.
19  *
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.
23  */
24
25 #include <linux/config.h>
26 #include <linux/mm.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>
32 #include <asm/e820.h>
33 #include <asm/setup.h>
34 #include <asm/mmzone.h>
35 #include <bios_ebda.h>
36
37 struct pglist_data *node_data[MAX_NUMNODES];
38 bootmem_data_t node0_bdata;
39
40 /*
41  * numa interface - we expect the numa architecture specfic code to have
42  *                  populated the following initialisation.
43  *
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
48  */
49
50 /*
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:
56  *
57  *     physnode_map[0-3] = 0;
58  *     physnode_map[4-7] = 1;
59  *     physnode_map[8- ] = -1;
60  */
61 s8 physnode_map[MAX_ELEMENTS] = { [0 ... (MAX_ELEMENTS - 1)] = -1};
62
63 unsigned long node_start_pfn[MAX_NUMNODES];
64 unsigned long node_end_pfn[MAX_NUMNODES];
65
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);
69
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;
76
77 #define LARGE_PAGE_BYTES (PTRS_PER_PTE * PAGE_SIZE)
78
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);
84
85 /*
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
88  *        memory map.
89  */
90 int __init get_memcfg_numa_flat(void)
91 {
92         printk("NUMA - single node, flat memory mode\n");
93
94         /* Run the memory configuration and find the top of memory. */
95         find_max_pfn();
96         node_start_pfn[0]  = 0;
97         node_end_pfn[0]   = max_pfn;
98
99         /* Indicate there is one node available. */
100         node_set_online(0);
101         numnodes = 1;
102         return 1;
103 }
104
105 /*
106  * Find the highest page frame number we have available for the node
107  */
108 static void __init find_max_pfn_node(int nid)
109 {
110         if (node_end_pfn[nid] > max_pfn)
111                 node_end_pfn[nid] = max_pfn;
112         /*
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
115          */
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])
119                 BUG();
120 }
121
122 /* 
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()
127  * for details.
128  */
129 static void __init allocate_pgdat(int nid)
130 {
131         if (nid)
132                 NODE_DATA(nid) = (pg_data_t *)node_remap_start_vaddr[nid];
133         else {
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));
137         }
138 }
139
140 /*
141  * Register fully available low RAM pages with the bootmem allocator.
142  */
143 static void __init register_bootmem_low_pages(unsigned long system_max_low_pfn)
144 {
145         int i;
146
147         for (i = 0; i < e820.nr_map; i++) {
148                 unsigned long curr_pfn, last_pfn, size;
149                 /*
150                  * Reserve usable low memory
151                  */
152                 if (e820.map[i].type != E820_RAM)
153                         continue;
154                 /*
155                  * We are rounding up the start address of usable memory:
156                  */
157                 curr_pfn = PFN_UP(e820.map[i].addr);
158                 if (curr_pfn >= system_max_low_pfn)
159                         continue;
160                 /*
161                  * ... and at the end of the usable range downwards:
162                  */
163                 last_pfn = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
164
165                 if (last_pfn > system_max_low_pfn)
166                         last_pfn = system_max_low_pfn;
167
168                 /*
169                  * .. finally, did all the rounding and playing
170                  * around just make the area go away?
171                  */
172                 if (last_pfn <= curr_pfn)
173                         continue;
174
175                 size = last_pfn - curr_pfn;
176                 free_bootmem_node(NODE_DATA(0), PFN_PHYS(curr_pfn), PFN_PHYS(size));
177         }
178 }
179
180 void __init remap_numa_kva(void)
181 {
182         void *vaddr;
183         unsigned long pfn;
184         int node;
185
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, 
191                                 PAGE_KERNEL_LARGE);
192                 }
193         }
194 }
195
196 static unsigned long calculate_numa_remap_pages(void)
197 {
198         int nid;
199         unsigned long size, reserve_pages = 0;
200
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",
210                                 size, nid);
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];
218         }
219         printk("Reserving total of %ld pages for numa KVA remap\n",
220                         reserve_pages);
221         return reserve_pages;
222 }
223
224 /*
225  * workaround for Dell systems that neglect to reserve EBDA
226  */
227 static void __init reserve_ebda_region_node(void)
228 {
229         unsigned int addr;
230         addr = get_bios_ebda();
231         if (addr)
232                 reserve_bootmem_node(NODE_DATA(0), addr, PAGE_SIZE);
233 }
234
235 unsigned long __init setup_memory(void)
236 {
237         int nid;
238         unsigned long bootmap_size, system_start_pfn, system_max_low_pfn;
239         unsigned long reserve_pages, pfn;
240
241         /*
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
246          * and ZONE_HIGHMEM.
247          */
248         get_memcfg_numa();
249
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  ",
255                                 nid);
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;
259                         printk("%ld ", pfn);
260                 }
261                 printk("\n");
262         }
263
264         reserve_pages = calculate_numa_remap_pages();
265
266         /* partially used pages are not usable - thus round upwards */
267         system_start_pfn = min_low_pfn = PFN_UP(init_pg_tables_end);
268
269         find_max_pfn();
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));
280 #endif
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);
285
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]);
291                 allocate_pgdat(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]));
296         }
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);
302
303         NODE_DATA(0)->bdata = &node0_bdata;
304
305         /*
306          * Initialize the boot-time allocator (with low memory only):
307          */
308         bootmap_size = init_bootmem_node(NODE_DATA(0), min_low_pfn, 0, system_max_low_pfn);
309
310         register_bootmem_low_pages(system_max_low_pfn);
311
312         /*
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.
317          */
318         reserve_bootmem_node(NODE_DATA(0), HIGH_MEMORY, (PFN_PHYS(min_low_pfn) +
319                  bootmap_size + PAGE_SIZE-1) - (HIGH_MEMORY));
320
321         /*
322          * reserve physical page 0 - it's a special BIOS page on many boxes,
323          * enabling clean reboots, SMP operation, laptop functions.
324          */
325         reserve_bootmem_node(NODE_DATA(0), 0, PAGE_SIZE);
326
327         /*
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)
331          */
332         reserve_bootmem_node(NODE_DATA(0), PAGE_SIZE, PAGE_SIZE);
333
334         /* reserve EBDA region, it's a 4K region */
335         reserve_ebda_region_node();
336
337 #ifdef CONFIG_ACPI_SLEEP
338         /*
339          * Reserve low memory region for sleep support.
340          */
341         acpi_reserve_bootmem();
342 #endif
343
344         /*
345          * Find and reserve possible boot-time SMP configuration:
346          */
347         find_smp_config();
348
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);
353                         initrd_start =
354                                 INITRD_START ? INITRD_START + PAGE_OFFSET : 0;
355                         initrd_end = initrd_start+INITRD_SIZE;
356                 }
357                 else {
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);
362                         initrd_start = 0;
363                 }
364         }
365 #endif
366         return system_max_low_pfn;
367 }
368
369 void __init zone_sizes_init(void)
370 {
371         int nid;
372
373         /*
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.
376          */
377         pgdat_list = NULL;
378         for (nid = numnodes - 1; nid >= 0; nid--) {       
379                 if (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);
383         }
384
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;
389
390                 unsigned long low = max_low_pfn;
391                 unsigned long start = node_start_pfn[nid];
392                 unsigned long high = node_end_pfn[nid];
393
394                 max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
395
396                 if (start > low) {
397 #ifdef CONFIG_HIGHMEM
398                         BUG_ON(start > high);
399                         zones_size[ZONE_HIGHMEM] = high - start;
400 #endif
401                 } else {
402                         if (low < max_dma)
403                                 zones_size[ZONE_DMA] = low;
404                         else {
405                                 BUG_ON(max_dma > low);
406                                 BUG_ON(low > high);
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;
411 #endif
412                         }
413                 }
414                 zholes_size = get_zholes_size(nid);
415                 /*
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
419                  */
420                 if (!nid)
421                         free_area_init_node(nid, NODE_DATA(nid),
422                                         zones_size, start, zholes_size);
423                 else {
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,
430                                 start, zholes_size);
431                 }
432         }
433         return;
434 }
435
436 void __init set_highmem_pages_init(int bad_ppro) 
437 {
438 #ifdef CONFIG_HIGHMEM
439         struct zone *zone;
440
441         for_each_zone(zone) {
442                 unsigned long node_pfn, node_high_size, zone_start_pfn;
443                 struct page * zone_mem_map;
444                 
445                 if (!is_highmem(zone))
446                         continue;
447
448                 printk("Initializing %s for node %d\n", zone->name,
449                         zone->zone_pgdat->node_id);
450
451                 node_high_size = zone->spanned_pages;
452                 zone_mem_map = zone->zone_mem_map;
453                 zone_start_pfn = zone->zone_start_pfn;
454
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);
458                 }
459         }
460         totalram_pages += totalhigh_pages;
461 #endif
462 }
463
464 void __init set_max_mapnr_init(void)
465 {
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;
470         else
471                 highmem_start_page = pfn_to_page(max_low_pfn - 1) + 1;
472         num_physpages = highend_pfn;
473 #else
474         num_physpages = max_low_pfn;
475 #endif
476 }