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