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
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.
14 * Routines used by ia64 machines with contiguous (or virtually contiguous)
17 #include <linux/config.h>
18 #include <linux/bootmem.h>
19 #include <linux/efi.h>
21 #include <linux/swap.h>
22 #include <linux/module.h>
24 #include <asm/meminit.h>
25 #include <asm/pgalloc.h>
26 #include <asm/pgtable.h>
27 #include <asm/sections.h>
29 #ifdef CONFIG_VIRTUAL_MEM_MAP
30 static unsigned long num_dma_physpages;
34 * show_mem - display a memory statistics summary
36 * Just walks the pages in the system and describes where they're allocated.
41 int i, total = 0, reserved = 0;
42 int shared = 0, cached = 0;
44 printk("Mem-info:\n");
47 printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
53 if (PageReserved(mem_map+i))
55 else if (PageSwapCache(mem_map+i))
57 else if (page_count(mem_map + i))
58 shared += page_count(mem_map + i) - 1;
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", pgtable_cache_size);
67 EXPORT_SYMBOL_GPL(show_mem);
69 /* physical address where the bootmem map is located */
70 unsigned long bootmap_start;
73 * find_max_pfn - adjust the maximum page number callback
74 * @start: start of range
76 * @arg: address of pointer to global max_pfn variable
78 * Passed as a callback function to efi_memmap_walk() to determine the highest
79 * available page frame number in the system.
82 find_max_pfn (unsigned long start, unsigned long end, void *arg)
84 unsigned long *max_pfnp = arg, pfn;
86 pfn = (PAGE_ALIGN(end - 1) - PAGE_OFFSET) >> PAGE_SHIFT;
93 * find_bootmap_location - callback to find a memory area for the bootmap
94 * @start: start of region
96 * @arg: unused callback data
98 * Find a place to put the bootmap and return its starting address in
99 * bootmap_start. This address must be page-aligned.
102 find_bootmap_location (unsigned long start, unsigned long end, void *arg)
104 unsigned long needed = *(unsigned long *)arg;
105 unsigned long range_start, range_end, free_start;
109 if (start == PAGE_OFFSET) {
116 free_start = PAGE_OFFSET;
118 for (i = 0; i < num_rsvd_regions; i++) {
119 range_start = max(start, free_start);
120 range_end = min(end, rsvd_region[i].start & PAGE_MASK);
122 free_start = PAGE_ALIGN(rsvd_region[i].end);
124 if (range_end <= range_start)
125 continue; /* skip over empty range */
127 if (range_end - range_start >= needed) {
128 bootmap_start = __pa(range_start);
129 return -1; /* done */
132 /* nothing more available in this segment */
133 if (range_end == end)
140 * find_memory - setup memory map
142 * Walk the EFI memory map and find usable memory for the system, taking
143 * into account reserved areas.
148 unsigned long bootmap_size;
152 /* first find highest page frame number */
154 efi_memmap_walk(find_max_pfn, &max_pfn);
156 /* how many bytes to cover all the pages */
157 bootmap_size = bootmem_bootmap_pages(max_pfn) << PAGE_SHIFT;
159 /* look for a location to hold the bootmap */
160 bootmap_start = ~0UL;
161 efi_memmap_walk(find_bootmap_location, &bootmap_size);
162 if (bootmap_start == ~0UL)
163 panic("Cannot find %ld bytes for bootmap\n", bootmap_size);
165 bootmap_size = init_bootmem(bootmap_start >> PAGE_SHIFT, max_pfn);
167 /* Free all available memory, then mark bootmem-map as being in use. */
168 efi_memmap_walk(filter_rsvd_memory, free_bootmem);
169 reserve_bootmem(bootmap_start, bootmap_size);
176 * per_cpu_init - setup per-cpu variables
178 * Allocate and setup per-cpu data areas.
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
191 if (smp_processor_id() == 0) {
192 cpu_data = __alloc_bootmem(PERCPU_PAGE_SIZE * NR_CPUS,
193 PERCPU_PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
194 for (cpu = 0; cpu < NR_CPUS; cpu++) {
195 memcpy(cpu_data, __phys_per_cpu_start, __per_cpu_end - __per_cpu_start);
196 __per_cpu_offset[cpu] = (char *) cpu_data - __per_cpu_start;
197 cpu_data += PERCPU_PAGE_SIZE;
198 per_cpu(local_per_cpu_offset, cpu) = __per_cpu_offset[cpu];
201 return __per_cpu_start + __per_cpu_offset[smp_processor_id()];
203 #endif /* CONFIG_SMP */
206 count_pages (u64 start, u64 end, void *arg)
208 unsigned long *count = arg;
210 *count += (end - start) >> PAGE_SHIFT;
214 #ifdef CONFIG_VIRTUAL_MEM_MAP
216 count_dma_pages (u64 start, u64 end, void *arg)
218 unsigned long *count = arg;
220 if (end <= MAX_DMA_ADDRESS)
221 *count += (end - start) >> PAGE_SHIFT;
227 * Set up the page tables.
233 unsigned long max_dma;
234 unsigned long zones_size[MAX_NR_ZONES];
235 #ifdef CONFIG_VIRTUAL_MEM_MAP
236 unsigned long zholes_size[MAX_NR_ZONES];
237 unsigned long max_gap;
240 /* initialize mem_map[] */
242 memset(zones_size, 0, sizeof(zones_size));
245 efi_memmap_walk(count_pages, &num_physpages);
247 max_dma = virt_to_phys((void *) MAX_DMA_ADDRESS) >> PAGE_SHIFT;
249 #ifdef CONFIG_VIRTUAL_MEM_MAP
250 memset(zholes_size, 0, sizeof(zholes_size));
252 num_dma_physpages = 0;
253 efi_memmap_walk(count_dma_pages, &num_dma_physpages);
255 if (max_low_pfn < max_dma) {
256 zones_size[ZONE_DMA] = max_low_pfn;
257 zholes_size[ZONE_DMA] = max_low_pfn - num_dma_physpages;
259 zones_size[ZONE_DMA] = max_dma;
260 zholes_size[ZONE_DMA] = max_dma - num_dma_physpages;
261 if (num_physpages > num_dma_physpages) {
262 zones_size[ZONE_NORMAL] = max_low_pfn - max_dma;
263 zholes_size[ZONE_NORMAL] =
264 ((max_low_pfn - max_dma) -
265 (num_physpages - num_dma_physpages));
270 efi_memmap_walk(find_largest_hole, (u64 *)&max_gap);
271 if (max_gap < LARGE_GAP) {
272 vmem_map = (struct page *) 0;
273 free_area_init_node(0, &contig_page_data, zones_size, 0,
276 unsigned long map_size;
278 /* allocate virtual_mem_map */
280 map_size = PAGE_ALIGN(max_low_pfn * sizeof(struct page));
281 vmalloc_end -= map_size;
282 vmem_map = (struct page *) vmalloc_end;
283 efi_memmap_walk(create_mem_map_page_table, NULL);
285 mem_map = contig_page_data.node_mem_map = vmem_map;
286 free_area_init_node(0, &contig_page_data, zones_size,
289 printk("Virtual mem_map starts at 0x%p\n", mem_map);
291 #else /* !CONFIG_VIRTUAL_MEM_MAP */
292 if (max_low_pfn < max_dma)
293 zones_size[ZONE_DMA] = max_low_pfn;
295 zones_size[ZONE_DMA] = max_dma;
296 zones_size[ZONE_NORMAL] = max_low_pfn - max_dma;
298 free_area_init(zones_size);
299 #endif /* !CONFIG_VIRTUAL_MEM_MAP */
300 zero_page_memmap_ptr = virt_to_page(ia64_imva(empty_zero_page));