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