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