blob: 537c2f3833ee47bcdfd29a214c61b404d8ac4f61 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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
29static unsigned long num_dma_physpages;
Bob Piccoe44e41d2006-06-28 12:55:43 -040030static unsigned long max_gap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#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 */
38void
39show_mem (void)
40{
41 int i, total = 0, reserved = 0;
42 int shared = 0, cached = 0;
43
Jes Sorensen709a6c12006-09-13 08:43:42 -040044 printk(KERN_INFO "Mem-info:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 show_free_areas();
46
Jes Sorensen709a6c12006-09-13 08:43:42 -040047 printk(KERN_INFO "Free swap: %6ldkB\n",
48 nr_swap_pages<<(PAGE_SHIFT-10));
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 i = max_mapnr;
Bob Piccoe44e41d2006-06-28 12:55:43 -040050 for (i = 0; i < max_mapnr; i++) {
51 if (!pfn_valid(i)) {
52#ifdef CONFIG_VIRTUAL_MEM_MAP
53 if (max_gap < LARGE_GAP)
54 continue;
55 i = vmemmap_find_next_valid_pfn(0, i) - 1;
56#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 continue;
Bob Piccoe44e41d2006-06-28 12:55:43 -040058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 total++;
60 if (PageReserved(mem_map+i))
61 reserved++;
62 else if (PageSwapCache(mem_map+i))
63 cached++;
64 else if (page_count(mem_map + i))
65 shared += page_count(mem_map + i) - 1;
66 }
Jes Sorensen709a6c12006-09-13 08:43:42 -040067 printk(KERN_INFO "%d pages of RAM\n", total);
68 printk(KERN_INFO "%d reserved pages\n", reserved);
69 printk(KERN_INFO "%d pages shared\n", shared);
70 printk(KERN_INFO "%d pages swap cached\n", cached);
71 printk(KERN_INFO "%ld pages in page table cache\n",
72 pgtable_quicklist_total_size());
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
75/* physical address where the bootmem map is located */
76unsigned long bootmap_start;
77
78/**
79 * find_max_pfn - adjust the maximum page number callback
80 * @start: start of range
81 * @end: end of range
82 * @arg: address of pointer to global max_pfn variable
83 *
84 * Passed as a callback function to efi_memmap_walk() to determine the highest
85 * available page frame number in the system.
86 */
87int
88find_max_pfn (unsigned long start, unsigned long end, void *arg)
89{
90 unsigned long *max_pfnp = arg, pfn;
91
92 pfn = (PAGE_ALIGN(end - 1) - PAGE_OFFSET) >> PAGE_SHIFT;
93 if (pfn > *max_pfnp)
94 *max_pfnp = pfn;
95 return 0;
96}
97
98/**
99 * find_bootmap_location - callback to find a memory area for the bootmap
100 * @start: start of region
101 * @end: end of region
102 * @arg: unused callback data
103 *
104 * Find a place to put the bootmap and return its starting address in
105 * bootmap_start. This address must be page-aligned.
106 */
Chen, Kenneth Wdae28062006-03-22 16:54:15 -0800107static int __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108find_bootmap_location (unsigned long start, unsigned long end, void *arg)
109{
110 unsigned long needed = *(unsigned long *)arg;
111 unsigned long range_start, range_end, free_start;
112 int i;
113
114#if IGNORE_PFN0
115 if (start == PAGE_OFFSET) {
116 start += PAGE_SIZE;
117 if (start >= end)
118 return 0;
119 }
120#endif
121
122 free_start = PAGE_OFFSET;
123
124 for (i = 0; i < num_rsvd_regions; i++) {
125 range_start = max(start, free_start);
126 range_end = min(end, rsvd_region[i].start & PAGE_MASK);
127
128 free_start = PAGE_ALIGN(rsvd_region[i].end);
129
130 if (range_end <= range_start)
131 continue; /* skip over empty range */
132
133 if (range_end - range_start >= needed) {
134 bootmap_start = __pa(range_start);
135 return -1; /* done */
136 }
137
138 /* nothing more available in this segment */
139 if (range_end == end)
140 return 0;
141 }
142 return 0;
143}
144
145/**
146 * find_memory - setup memory map
147 *
148 * Walk the EFI memory map and find usable memory for the system, taking
149 * into account reserved areas.
150 */
Chen, Kenneth Wdae28062006-03-22 16:54:15 -0800151void __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152find_memory (void)
153{
154 unsigned long bootmap_size;
155
156 reserve_memory();
157
158 /* first find highest page frame number */
159 max_pfn = 0;
160 efi_memmap_walk(find_max_pfn, &max_pfn);
161
162 /* how many bytes to cover all the pages */
163 bootmap_size = bootmem_bootmap_pages(max_pfn) << PAGE_SHIFT;
164
165 /* look for a location to hold the bootmap */
166 bootmap_start = ~0UL;
167 efi_memmap_walk(find_bootmap_location, &bootmap_size);
168 if (bootmap_start == ~0UL)
169 panic("Cannot find %ld bytes for bootmap\n", bootmap_size);
170
171 bootmap_size = init_bootmem(bootmap_start >> PAGE_SHIFT, max_pfn);
172
173 /* Free all available memory, then mark bootmem-map as being in use. */
174 efi_memmap_walk(filter_rsvd_memory, free_bootmem);
175 reserve_bootmem(bootmap_start, bootmap_size);
176
177 find_initrd();
178}
179
180#ifdef CONFIG_SMP
181/**
182 * per_cpu_init - setup per-cpu variables
183 *
184 * Allocate and setup per-cpu data areas.
185 */
Chen, Kenneth W244fd542006-03-12 09:00:13 -0800186void * __cpuinit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187per_cpu_init (void)
188{
189 void *cpu_data;
190 int cpu;
Ashok Rajff741902005-11-11 14:32:40 -0800191 static int first_time=1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 /*
194 * get_free_pages() cannot be used before cpu_init() done. BSP
195 * allocates "NR_CPUS" pages for all CPUs to avoid that AP calls
196 * get_zeroed_page().
197 */
Ashok Rajff741902005-11-11 14:32:40 -0800198 if (first_time) {
199 first_time=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 cpu_data = __alloc_bootmem(PERCPU_PAGE_SIZE * NR_CPUS,
201 PERCPU_PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
202 for (cpu = 0; cpu < NR_CPUS; cpu++) {
203 memcpy(cpu_data, __phys_per_cpu_start, __per_cpu_end - __per_cpu_start);
204 __per_cpu_offset[cpu] = (char *) cpu_data - __per_cpu_start;
205 cpu_data += PERCPU_PAGE_SIZE;
206 per_cpu(local_per_cpu_offset, cpu) = __per_cpu_offset[cpu];
207 }
208 }
209 return __per_cpu_start + __per_cpu_offset[smp_processor_id()];
210}
211#endif /* CONFIG_SMP */
212
213static int
214count_pages (u64 start, u64 end, void *arg)
215{
216 unsigned long *count = arg;
217
218 *count += (end - start) >> PAGE_SHIFT;
219 return 0;
220}
221
222#ifdef CONFIG_VIRTUAL_MEM_MAP
223static int
224count_dma_pages (u64 start, u64 end, void *arg)
225{
226 unsigned long *count = arg;
227
228 if (start < MAX_DMA_ADDRESS)
229 *count += (min(end, MAX_DMA_ADDRESS) - start) >> PAGE_SHIFT;
230 return 0;
231}
232#endif
233
234/*
235 * Set up the page tables.
236 */
237
Chen, Kenneth Wdae28062006-03-22 16:54:15 -0800238void __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239paging_init (void)
240{
241 unsigned long max_dma;
242 unsigned long zones_size[MAX_NR_ZONES];
243#ifdef CONFIG_VIRTUAL_MEM_MAP
244 unsigned long zholes_size[MAX_NR_ZONES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245#endif
246
247 /* initialize mem_map[] */
248
249 memset(zones_size, 0, sizeof(zones_size));
250
251 num_physpages = 0;
252 efi_memmap_walk(count_pages, &num_physpages);
253
254 max_dma = virt_to_phys((void *) MAX_DMA_ADDRESS) >> PAGE_SHIFT;
255
256#ifdef CONFIG_VIRTUAL_MEM_MAP
257 memset(zholes_size, 0, sizeof(zholes_size));
258
259 num_dma_physpages = 0;
260 efi_memmap_walk(count_dma_pages, &num_dma_physpages);
261
262 if (max_low_pfn < max_dma) {
263 zones_size[ZONE_DMA] = max_low_pfn;
264 zholes_size[ZONE_DMA] = max_low_pfn - num_dma_physpages;
265 } else {
266 zones_size[ZONE_DMA] = max_dma;
267 zholes_size[ZONE_DMA] = max_dma - num_dma_physpages;
268 if (num_physpages > num_dma_physpages) {
269 zones_size[ZONE_NORMAL] = max_low_pfn - max_dma;
270 zholes_size[ZONE_NORMAL] =
271 ((max_low_pfn - max_dma) -
272 (num_physpages - num_dma_physpages));
273 }
274 }
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 efi_memmap_walk(find_largest_hole, (u64 *)&max_gap);
277 if (max_gap < LARGE_GAP) {
278 vmem_map = (struct page *) 0;
Bob Piccoc6787962005-10-04 15:13:44 -0400279 free_area_init_node(0, NODE_DATA(0), zones_size, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 zholes_size);
281 } else {
282 unsigned long map_size;
283
284 /* allocate virtual_mem_map */
285
Bob Picco921eea12006-06-28 12:54:55 -0400286 map_size = PAGE_ALIGN(ALIGN(max_low_pfn, MAX_ORDER_NR_PAGES) *
287 sizeof(struct page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 vmalloc_end -= map_size;
289 vmem_map = (struct page *) vmalloc_end;
290 efi_memmap_walk(create_mem_map_page_table, NULL);
291
292 NODE_DATA(0)->node_mem_map = vmem_map;
Bob Piccoc6787962005-10-04 15:13:44 -0400293 free_area_init_node(0, NODE_DATA(0), zones_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 0, zholes_size);
295
296 printk("Virtual mem_map starts at 0x%p\n", mem_map);
297 }
298#else /* !CONFIG_VIRTUAL_MEM_MAP */
299 if (max_low_pfn < max_dma)
300 zones_size[ZONE_DMA] = max_low_pfn;
301 else {
302 zones_size[ZONE_DMA] = max_dma;
303 zones_size[ZONE_NORMAL] = max_low_pfn - max_dma;
304 }
305 free_area_init(zones_size);
306#endif /* !CONFIG_VIRTUAL_MEM_MAP */
307 zero_page_memmap_ptr = virt_to_page(ia64_imva(empty_zero_page));
308}