Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #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 Picco | e44e41d | 2006-06-28 12:55:43 -0400 | [diff] [blame] | 29 | static unsigned long max_gap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #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 | */ |
| 37 | void |
| 38 | show_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 Picco | e44e41d | 2006-06-28 12:55:43 -0400 | [diff] [blame] | 48 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | continue; |
Bob Picco | e44e41d | 2006-06-28 12:55:43 -0400 | [diff] [blame] | 56 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | 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 Holt | fde740e | 2005-04-25 13:13:16 -0700 | [diff] [blame] | 69 | printk("%ld pages in page table cache\n", |
| 70 | pgtable_quicklist_total_size()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /* physical address where the bootmem map is located */ |
| 74 | unsigned 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 | */ |
| 85 | int |
| 86 | find_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 W | dae2806 | 2006-03-22 16:54:15 -0800 | [diff] [blame] | 105 | static int __init |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | find_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 W | dae2806 | 2006-03-22 16:54:15 -0800 | [diff] [blame] | 149 | void __init |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | find_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 W | 244fd54 | 2006-03-12 09:00:13 -0800 | [diff] [blame] | 184 | void * __cpuinit |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | per_cpu_init (void) |
| 186 | { |
| 187 | void *cpu_data; |
| 188 | int cpu; |
Ashok Raj | ff74190 | 2005-11-11 14:32:40 -0800 | [diff] [blame] | 189 | static int first_time=1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
| 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 Raj | ff74190 | 2005-11-11 14:32:40 -0800 | [diff] [blame] | 196 | if (first_time) { |
| 197 | first_time=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | 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 | |
| 211 | static int |
| 212 | count_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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | /* |
| 221 | * Set up the page tables. |
| 222 | */ |
| 223 | |
Chen, Kenneth W | dae2806 | 2006-03-22 16:54:15 -0800 | [diff] [blame] | 224 | void __init |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | paging_init (void) |
| 226 | { |
| 227 | unsigned long max_dma; |
Mel Gorman | 05e0caa | 2006-09-27 01:49:54 -0700 | [diff] [blame^] | 228 | unsigned long nid = 0; |
| 229 | unsigned long max_zone_pfns[MAX_NR_ZONES]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
| 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 Gorman | 05e0caa | 2006-09-27 01:49:54 -0700 | [diff] [blame^] | 235 | max_zone_pfns[ZONE_DMA] = max_dma; |
| 236 | max_zone_pfns[ZONE_NORMAL] = max_low_pfn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
| 238 | #ifdef CONFIG_VIRTUAL_MEM_MAP |
Mel Gorman | 05e0caa | 2006-09-27 01:49:54 -0700 | [diff] [blame^] | 239 | efi_memmap_walk(register_active_ranges, &nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | efi_memmap_walk(find_largest_hole, (u64 *)&max_gap); |
| 241 | if (max_gap < LARGE_GAP) { |
| 242 | vmem_map = (struct page *) 0; |
Mel Gorman | 05e0caa | 2006-09-27 01:49:54 -0700 | [diff] [blame^] | 243 | free_area_init_nodes(max_zone_pfns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | } else { |
| 245 | unsigned long map_size; |
| 246 | |
| 247 | /* allocate virtual_mem_map */ |
| 248 | |
Bob Picco | 921eea1 | 2006-06-28 12:54:55 -0400 | [diff] [blame] | 249 | map_size = PAGE_ALIGN(ALIGN(max_low_pfn, MAX_ORDER_NR_PAGES) * |
| 250 | sizeof(struct page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | vmalloc_end -= map_size; |
| 252 | vmem_map = (struct page *) vmalloc_end; |
| 253 | efi_memmap_walk(create_mem_map_page_table, NULL); |
| 254 | |
Mel Gorman | 05e0caa | 2006-09-27 01:49:54 -0700 | [diff] [blame^] | 255 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
| 263 | printk("Virtual mem_map starts at 0x%p\n", mem_map); |
| 264 | } |
| 265 | #else /* !CONFIG_VIRTUAL_MEM_MAP */ |
Mel Gorman | 05e0caa | 2006-09-27 01:49:54 -0700 | [diff] [blame^] | 266 | add_active_range(0, 0, max_low_pfn); |
| 267 | free_area_init_nodes(max_zone_pfns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | #endif /* !CONFIG_VIRTUAL_MEM_MAP */ |
| 269 | zero_page_memmap_ptr = virt_to_page(ia64_imva(empty_zero_page)); |
| 270 | } |