blob: 719d476e71ba74e1a24dcb481ed456b923be424f [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
Bob Piccoe44e41d2006-06-28 12:55:43 -040029static unsigned long max_gap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#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 */
37void
38show_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: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
47 i = max_mapnr;
Bob Piccoe44e41d2006-06-28 12:55:43 -040048 for (i = 0; i < max_mapnr; i++) {
49 if (!pfn_valid(i)) {
50#ifdef CONFIG_VIRTUAL_MEM_MAP
51 if (max_gap < LARGE_GAP)
52 continue;
53 i = vmemmap_find_next_valid_pfn(0, i) - 1;
54#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 continue;
Bob Piccoe44e41d2006-06-28 12:55:43 -040056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 total++;
58 if (PageReserved(mem_map+i))
59 reserved++;
60 else if (PageSwapCache(mem_map+i))
61 cached++;
62 else if (page_count(mem_map + i))
63 shared += page_count(mem_map + i) - 1;
64 }
65 printk("%d pages of RAM\n", total);
66 printk("%d reserved pages\n", reserved);
67 printk("%d pages shared\n", shared);
68 printk("%d pages swap cached\n", cached);
Robin Holtfde740e2005-04-25 13:13:16 -070069 printk("%ld pages in page table cache\n",
70 pgtable_quicklist_total_size());
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
73/* physical address where the bootmem map is located */
74unsigned long bootmap_start;
75
76/**
77 * find_max_pfn - adjust the maximum page number callback
78 * @start: start of range
79 * @end: end of range
80 * @arg: address of pointer to global max_pfn variable
81 *
82 * Passed as a callback function to efi_memmap_walk() to determine the highest
83 * available page frame number in the system.
84 */
85int
86find_max_pfn (unsigned long start, unsigned long end, void *arg)
87{
88 unsigned long *max_pfnp = arg, pfn;
89
90 pfn = (PAGE_ALIGN(end - 1) - PAGE_OFFSET) >> PAGE_SHIFT;
91 if (pfn > *max_pfnp)
92 *max_pfnp = pfn;
93 return 0;
94}
95
96/**
97 * find_bootmap_location - callback to find a memory area for the bootmap
98 * @start: start of region
99 * @end: end of region
100 * @arg: unused callback data
101 *
102 * Find a place to put the bootmap and return its starting address in
103 * bootmap_start. This address must be page-aligned.
104 */
Chen, Kenneth Wdae28062006-03-22 16:54:15 -0800105static int __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106find_bootmap_location (unsigned long start, unsigned long end, void *arg)
107{
108 unsigned long needed = *(unsigned long *)arg;
109 unsigned long range_start, range_end, free_start;
110 int i;
111
112#if IGNORE_PFN0
113 if (start == PAGE_OFFSET) {
114 start += PAGE_SIZE;
115 if (start >= end)
116 return 0;
117 }
118#endif
119
120 free_start = PAGE_OFFSET;
121
122 for (i = 0; i < num_rsvd_regions; i++) {
123 range_start = max(start, free_start);
124 range_end = min(end, rsvd_region[i].start & PAGE_MASK);
125
126 free_start = PAGE_ALIGN(rsvd_region[i].end);
127
128 if (range_end <= range_start)
129 continue; /* skip over empty range */
130
131 if (range_end - range_start >= needed) {
132 bootmap_start = __pa(range_start);
133 return -1; /* done */
134 }
135
136 /* nothing more available in this segment */
137 if (range_end == end)
138 return 0;
139 }
140 return 0;
141}
142
143/**
144 * find_memory - setup memory map
145 *
146 * Walk the EFI memory map and find usable memory for the system, taking
147 * into account reserved areas.
148 */
Chen, Kenneth Wdae28062006-03-22 16:54:15 -0800149void __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150find_memory (void)
151{
152 unsigned long bootmap_size;
153
154 reserve_memory();
155
156 /* first find highest page frame number */
157 max_pfn = 0;
158 efi_memmap_walk(find_max_pfn, &max_pfn);
159
160 /* how many bytes to cover all the pages */
161 bootmap_size = bootmem_bootmap_pages(max_pfn) << PAGE_SHIFT;
162
163 /* look for a location to hold the bootmap */
164 bootmap_start = ~0UL;
165 efi_memmap_walk(find_bootmap_location, &bootmap_size);
166 if (bootmap_start == ~0UL)
167 panic("Cannot find %ld bytes for bootmap\n", bootmap_size);
168
169 bootmap_size = init_bootmem(bootmap_start >> PAGE_SHIFT, max_pfn);
170
171 /* Free all available memory, then mark bootmem-map as being in use. */
172 efi_memmap_walk(filter_rsvd_memory, free_bootmem);
173 reserve_bootmem(bootmap_start, bootmap_size);
174
175 find_initrd();
176}
177
178#ifdef CONFIG_SMP
179/**
180 * per_cpu_init - setup per-cpu variables
181 *
182 * Allocate and setup per-cpu data areas.
183 */
Chen, Kenneth W244fd542006-03-12 09:00:13 -0800184void * __cpuinit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185per_cpu_init (void)
186{
187 void *cpu_data;
188 int cpu;
Ashok Rajff741902005-11-11 14:32:40 -0800189 static int first_time=1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 /*
192 * get_free_pages() cannot be used before cpu_init() done. BSP
193 * allocates "NR_CPUS" pages for all CPUs to avoid that AP calls
194 * get_zeroed_page().
195 */
Ashok Rajff741902005-11-11 14:32:40 -0800196 if (first_time) {
197 first_time=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 cpu_data = __alloc_bootmem(PERCPU_PAGE_SIZE * NR_CPUS,
199 PERCPU_PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
200 for (cpu = 0; cpu < NR_CPUS; cpu++) {
201 memcpy(cpu_data, __phys_per_cpu_start, __per_cpu_end - __per_cpu_start);
202 __per_cpu_offset[cpu] = (char *) cpu_data - __per_cpu_start;
203 cpu_data += PERCPU_PAGE_SIZE;
204 per_cpu(local_per_cpu_offset, cpu) = __per_cpu_offset[cpu];
205 }
206 }
207 return __per_cpu_start + __per_cpu_offset[smp_processor_id()];
208}
209#endif /* CONFIG_SMP */
210
211static int
212count_pages (u64 start, u64 end, void *arg)
213{
214 unsigned long *count = arg;
215
216 *count += (end - start) >> PAGE_SHIFT;
217 return 0;
218}
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220/*
221 * Set up the page tables.
222 */
223
Chen, Kenneth Wdae28062006-03-22 16:54:15 -0800224void __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225paging_init (void)
226{
227 unsigned long max_dma;
Mel Gorman05e0caa2006-09-27 01:49:54 -0700228 unsigned long nid = 0;
229 unsigned long max_zone_pfns[MAX_NR_ZONES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 num_physpages = 0;
232 efi_memmap_walk(count_pages, &num_physpages);
233
234 max_dma = virt_to_phys((void *) MAX_DMA_ADDRESS) >> PAGE_SHIFT;
Mel Gorman05e0caa2006-09-27 01:49:54 -0700235 max_zone_pfns[ZONE_DMA] = max_dma;
236 max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238#ifdef CONFIG_VIRTUAL_MEM_MAP
Mel Gorman05e0caa2006-09-27 01:49:54 -0700239 efi_memmap_walk(register_active_ranges, &nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 efi_memmap_walk(find_largest_hole, (u64 *)&max_gap);
241 if (max_gap < LARGE_GAP) {
242 vmem_map = (struct page *) 0;
Mel Gorman05e0caa2006-09-27 01:49:54 -0700243 free_area_init_nodes(max_zone_pfns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 } else {
245 unsigned long map_size;
246
247 /* allocate virtual_mem_map */
248
Bob Picco921eea12006-06-28 12:54:55 -0400249 map_size = PAGE_ALIGN(ALIGN(max_low_pfn, MAX_ORDER_NR_PAGES) *
250 sizeof(struct page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 vmalloc_end -= map_size;
252 vmem_map = (struct page *) vmalloc_end;
253 efi_memmap_walk(create_mem_map_page_table, NULL);
254
Mel Gorman05e0caa2006-09-27 01:49:54 -0700255 /*
256 * alloc_node_mem_map makes an adjustment for mem_map
257 * which isn't compatible with vmem_map.
258 */
259 NODE_DATA(0)->node_mem_map = vmem_map +
260 find_min_pfn_with_active_regions();
261 free_area_init_nodes(max_zone_pfns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 printk("Virtual mem_map starts at 0x%p\n", mem_map);
264 }
265#else /* !CONFIG_VIRTUAL_MEM_MAP */
Mel Gorman05e0caa2006-09-27 01:49:54 -0700266 add_active_range(0, 0, max_low_pfn);
267 free_area_init_nodes(max_zone_pfns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268#endif /* !CONFIG_VIRTUAL_MEM_MAP */
269 zero_page_memmap_ptr = virt_to_page(ia64_imva(empty_zero_page));
270}