ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ia64 / mm / contig.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1998-2003 Hewlett-Packard Co
7  *      David Mosberger-Tang <davidm@hpl.hp.com>
8  *      Stephane Eranian <eranian@hpl.hp.com>
9  * Copyright (C) 2000, Rohit Seth <rohit.seth@intel.com>
10  * Copyright (C) 1999 VA Linux Systems
11  * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
12  * Copyright (C) 2003 Silicon Graphics, Inc. All rights reserved.
13  *
14  * Routines used by ia64 machines with contiguous (or virtually contiguous)
15  * memory.
16  */
17 #include <linux/config.h>
18 #include <linux/bootmem.h>
19 #include <linux/efi.h>
20 #include <linux/mm.h>
21 #include <linux/swap.h>
22
23 #include <asm/meminit.h>
24 #include <asm/pgalloc.h>
25 #include <asm/pgtable.h>
26 #include <asm/sections.h>
27
28 #ifdef CONFIG_VIRTUAL_MEM_MAP
29 static unsigned long num_dma_physpages;
30 #endif
31
32 /**
33  * show_mem - display a memory statistics summary
34  *
35  * Just walks the pages in the system and describes where they're allocated.
36  */
37 void
38 show_mem (void)
39 {
40         int i, total = 0, reserved = 0;
41         int shared = 0, cached = 0;
42
43         printk("Mem-info:\n");
44         show_free_areas();
45
46         printk("Free swap:       %6dkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
47         i = max_mapnr;
48         while (i-- > 0) {
49                 if (!pfn_valid(i))
50                         continue;
51                 total++;
52                 if (PageReserved(mem_map+i))
53                         reserved++;
54                 else if (PageSwapCache(mem_map+i))
55                         cached++;
56                 else if (page_count(mem_map + i))
57                         shared += page_count(mem_map + i) - 1;
58         }
59         printk("%d pages of RAM\n", total);
60         printk("%d reserved pages\n", reserved);
61         printk("%d pages shared\n", shared);
62         printk("%d pages swap cached\n", cached);
63         printk("%ld pages in page table cache\n", pgtable_cache_size);
64 }
65
66 /* physical address where the bootmem map is located */
67 unsigned long bootmap_start;
68
69 /**
70  * find_max_pfn - adjust the maximum page number callback
71  * @start: start of range
72  * @end: end of range
73  * @arg: address of pointer to global max_pfn variable
74  *
75  * Passed as a callback function to efi_memmap_walk() to determine the highest
76  * available page frame number in the system.
77  */
78 int
79 find_max_pfn (unsigned long start, unsigned long end, void *arg)
80 {
81         unsigned long *max_pfnp = arg, pfn;
82
83         pfn = (PAGE_ALIGN(end - 1) - PAGE_OFFSET) >> PAGE_SHIFT;
84         if (pfn > *max_pfnp)
85                 *max_pfnp = pfn;
86         return 0;
87 }
88
89 /**
90  * find_bootmap_location - callback to find a memory area for the bootmap
91  * @start: start of region
92  * @end: end of region
93  * @arg: unused callback data
94  *
95  * Find a place to put the bootmap and return its starting address in
96  * bootmap_start.  This address must be page-aligned.
97  */
98 int
99 find_bootmap_location (unsigned long start, unsigned long end, void *arg)
100 {
101         unsigned long needed = *(unsigned long *)arg;
102         unsigned long range_start, range_end, free_start;
103         int i;
104
105 #if IGNORE_PFN0
106         if (start == PAGE_OFFSET) {
107                 start += PAGE_SIZE;
108                 if (start >= end)
109                         return 0;
110         }
111 #endif
112
113         free_start = PAGE_OFFSET;
114
115         for (i = 0; i < num_rsvd_regions; i++) {
116                 range_start = max(start, free_start);
117                 range_end   = min(end, rsvd_region[i].start & PAGE_MASK);
118
119                 if (range_end <= range_start)
120                         continue; /* skip over empty range */
121
122                 if (range_end - range_start >= needed) {
123                         bootmap_start = __pa(range_start);
124                         return 1;       /* done */
125                 }
126
127                 /* nothing more available in this segment */
128                 if (range_end == end)
129                         return 0;
130
131                 free_start = PAGE_ALIGN(rsvd_region[i].end);
132         }
133         return 0;
134 }
135
136 /**
137  * find_memory - setup memory map
138  *
139  * Walk the EFI memory map and find usable memory for the system, taking
140  * into account reserved areas.
141  */
142 void
143 find_memory (void)
144 {
145         unsigned long bootmap_size;
146
147         reserve_memory();
148
149         /* first find highest page frame number */
150         max_pfn = 0;
151         efi_memmap_walk(find_max_pfn, &max_pfn);
152
153         /* how many bytes to cover all the pages */
154         bootmap_size = bootmem_bootmap_pages(max_pfn) << PAGE_SHIFT;
155
156         /* look for a location to hold the bootmap */
157         bootmap_start = ~0UL;
158         efi_memmap_walk(find_bootmap_location, &bootmap_size);
159         if (bootmap_start == ~0UL)
160                 panic("Cannot find %ld bytes for bootmap\n", bootmap_size);
161
162         bootmap_size = init_bootmem(bootmap_start >> PAGE_SHIFT, max_pfn);
163
164         /* Free all available memory, then mark bootmem-map as being in use. */
165         efi_memmap_walk(filter_rsvd_memory, free_bootmem);
166         reserve_bootmem(bootmap_start, bootmap_size);
167
168         find_initrd();
169 }
170
171 #ifdef CONFIG_SMP
172 /**
173  * per_cpu_init - setup per-cpu variables
174  *
175  * Allocate and setup per-cpu data areas.
176  */
177 void *
178 per_cpu_init (void)
179 {
180         void *cpu_data;
181         int cpu;
182
183         /*
184          * get_free_pages() cannot be used before cpu_init() done.  BSP
185          * allocates "NR_CPUS" pages for all CPUs to avoid that AP calls
186          * get_zeroed_page().
187          */
188         if (smp_processor_id() == 0) {
189                 cpu_data = __alloc_bootmem(PERCPU_PAGE_SIZE * NR_CPUS,
190                                            PERCPU_PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
191                 for (cpu = 0; cpu < NR_CPUS; cpu++) {
192                         memcpy(cpu_data, __phys_per_cpu_start, __per_cpu_end - __per_cpu_start);
193                         __per_cpu_offset[cpu] = (char *) cpu_data - __per_cpu_start;
194                         cpu_data += PERCPU_PAGE_SIZE;
195                         per_cpu(local_per_cpu_offset, cpu) = __per_cpu_offset[cpu];
196                 }
197         }
198         return __per_cpu_start + __per_cpu_offset[smp_processor_id()];
199 }
200 #endif /* CONFIG_SMP */
201
202 static int
203 count_pages (u64 start, u64 end, void *arg)
204 {
205         unsigned long *count = arg;
206
207         *count += (end - start) >> PAGE_SHIFT;
208         return 0;
209 }
210
211 #ifdef CONFIG_VIRTUAL_MEM_MAP
212 static int
213 count_dma_pages (u64 start, u64 end, void *arg)
214 {
215         unsigned long *count = arg;
216
217         if (end <= MAX_DMA_ADDRESS)
218                 *count += (end - start) >> PAGE_SHIFT;
219         return 0;
220 }
221 #endif
222
223 /*
224  * Set up the page tables.
225  */
226
227 void
228 paging_init (void)
229 {
230         unsigned long max_dma;
231         unsigned long zones_size[MAX_NR_ZONES];
232 #ifdef CONFIG_VIRTUAL_MEM_MAP
233         unsigned long zholes_size[MAX_NR_ZONES];
234         unsigned long max_gap;
235 #endif
236
237         /* initialize mem_map[] */
238
239         memset(zones_size, 0, sizeof(zones_size));
240
241         num_physpages = 0;
242         efi_memmap_walk(count_pages, &num_physpages);
243
244         max_dma = virt_to_phys((void *) MAX_DMA_ADDRESS) >> PAGE_SHIFT;
245
246 #ifdef CONFIG_VIRTUAL_MEM_MAP
247         memset(zholes_size, 0, sizeof(zholes_size));
248
249         num_dma_physpages = 0;
250         efi_memmap_walk(count_dma_pages, &num_dma_physpages);
251
252         if (max_low_pfn < max_dma) {
253                 zones_size[ZONE_DMA] = max_low_pfn;
254                 zholes_size[ZONE_DMA] = max_low_pfn - num_dma_physpages;
255         } else {
256                 zones_size[ZONE_DMA] = max_dma;
257                 zholes_size[ZONE_DMA] = max_dma - num_dma_physpages;
258                 if (num_physpages > num_dma_physpages) {
259                         zones_size[ZONE_NORMAL] = max_low_pfn - max_dma;
260                         zholes_size[ZONE_NORMAL] =
261                                 ((max_low_pfn - max_dma) -
262                                  (num_physpages - num_dma_physpages));
263                 }
264         }
265
266         max_gap = 0;
267         efi_memmap_walk(find_largest_hole, (u64 *)&max_gap);
268         if (max_gap < LARGE_GAP) {
269                 vmem_map = (struct page *) 0;
270                 free_area_init_node(0, &contig_page_data, NULL, zones_size, 0,
271                                     zholes_size);
272                 mem_map = contig_page_data.node_mem_map;
273         } else {
274                 unsigned long map_size;
275
276                 /* allocate virtual_mem_map */
277
278                 map_size = PAGE_ALIGN(max_low_pfn * sizeof(struct page));
279                 vmalloc_end -= map_size;
280                 vmem_map = (struct page *) vmalloc_end;
281                 efi_memmap_walk(create_mem_map_page_table, 0);
282
283                 free_area_init_node(0, &contig_page_data, vmem_map, zones_size,
284                                     0, zholes_size);
285
286                 mem_map = contig_page_data.node_mem_map;
287                 printk("Virtual mem_map starts at 0x%p\n", mem_map);
288         }
289 #else /* !CONFIG_VIRTUAL_MEM_MAP */
290         if (max_low_pfn < max_dma)
291                 zones_size[ZONE_DMA] = max_low_pfn;
292         else {
293                 zones_size[ZONE_DMA] = max_dma;
294                 zones_size[ZONE_NORMAL] = max_low_pfn - max_dma;
295         }
296         free_area_init(zones_size);
297 #endif /* !CONFIG_VIRTUAL_MEM_MAP */
298         zero_page_memmap_ptr = virt_to_page(ia64_imva(empty_zero_page));
299 }