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) 1994 - 2000 Ralf Baechle |
| 7 | * Copyright (C) 1999, 2000 Silicon Graphics, Inc. |
| 8 | * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com |
| 9 | * Copyright (C) 2000 MIPS Technologies, Inc. All rights reserved. |
| 10 | */ |
| 11 | #include <linux/config.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/signal.h> |
| 15 | #include <linux/sched.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/errno.h> |
| 18 | #include <linux/string.h> |
| 19 | #include <linux/types.h> |
| 20 | #include <linux/pagemap.h> |
| 21 | #include <linux/ptrace.h> |
| 22 | #include <linux/mman.h> |
| 23 | #include <linux/mm.h> |
| 24 | #include <linux/bootmem.h> |
| 25 | #include <linux/highmem.h> |
| 26 | #include <linux/swap.h> |
Daniel Jacobowitz | 3d50375 | 2005-01-20 19:59:54 -0500 | [diff] [blame] | 27 | #include <linux/proc_fs.h> |
Dave Hansen | 22a9835 | 2006-03-27 01:16:04 -0800 | [diff] [blame] | 28 | #include <linux/pfn.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | #include <asm/bootinfo.h> |
| 31 | #include <asm/cachectl.h> |
| 32 | #include <asm/cpu.h> |
| 33 | #include <asm/dma.h> |
| 34 | #include <asm/mmu_context.h> |
| 35 | #include <asm/sections.h> |
| 36 | #include <asm/pgtable.h> |
| 37 | #include <asm/pgalloc.h> |
| 38 | #include <asm/tlb.h> |
| 39 | |
| 40 | DEFINE_PER_CPU(struct mmu_gather, mmu_gathers); |
| 41 | |
| 42 | unsigned long highstart_pfn, highend_pfn; |
| 43 | |
| 44 | /* |
| 45 | * We have up to 8 empty zeroed pages so we can map one of the right colour |
| 46 | * when needed. This is necessary only on R4000 / R4400 SC and MC versions |
| 47 | * where we have to avoid VCED / VECI exceptions for good performance at |
| 48 | * any price. Since page is never written to after the initialization we |
| 49 | * don't have to care about aliases on other CPUs. |
| 50 | */ |
| 51 | unsigned long empty_zero_page, zero_page_mask; |
| 52 | |
| 53 | /* |
| 54 | * Not static inline because used by IP27 special magic initialization code |
| 55 | */ |
| 56 | unsigned long setup_zero_pages(void) |
| 57 | { |
Nick Piggin | 8dfcc9b | 2006-03-22 00:08:05 -0800 | [diff] [blame] | 58 | unsigned int order; |
| 59 | unsigned long size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | struct page *page; |
| 61 | |
| 62 | if (cpu_has_vce) |
| 63 | order = 3; |
| 64 | else |
| 65 | order = 0; |
| 66 | |
| 67 | empty_zero_page = __get_free_pages(GFP_KERNEL | __GFP_ZERO, order); |
| 68 | if (!empty_zero_page) |
| 69 | panic("Oh boy, that early out of memory?"); |
| 70 | |
| 71 | page = virt_to_page(empty_zero_page); |
Nick Piggin | 8dfcc9b | 2006-03-22 00:08:05 -0800 | [diff] [blame] | 72 | split_page(page, order); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | while (page < virt_to_page(empty_zero_page + (PAGE_SIZE << order))) { |
Hugh Dickins | 68352e6 | 2005-12-12 00:37:23 -0800 | [diff] [blame] | 74 | SetPageReserved(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | page++; |
| 76 | } |
| 77 | |
| 78 | size = PAGE_SIZE << order; |
| 79 | zero_page_mask = (size - 1) & PAGE_MASK; |
| 80 | |
| 81 | return 1UL << order; |
| 82 | } |
| 83 | |
| 84 | #ifdef CONFIG_HIGHMEM |
| 85 | pte_t *kmap_pte; |
| 86 | pgprot_t kmap_prot; |
| 87 | |
| 88 | #define kmap_get_fixmap_pte(vaddr) \ |
Ralf Baechle | c6e8b58 | 2005-02-10 12:19:59 +0000 | [diff] [blame] | 89 | pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr), (vaddr)), (vaddr)), (vaddr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
| 91 | static void __init kmap_init(void) |
| 92 | { |
| 93 | unsigned long kmap_vstart; |
| 94 | |
| 95 | /* cache the first kmap pte */ |
| 96 | kmap_vstart = __fix_to_virt(FIX_KMAP_BEGIN); |
| 97 | kmap_pte = kmap_get_fixmap_pte(kmap_vstart); |
| 98 | |
| 99 | kmap_prot = PAGE_KERNEL; |
| 100 | } |
| 101 | |
Ralf Baechle | 84fd089 | 2005-02-07 16:13:07 +0000 | [diff] [blame] | 102 | #ifdef CONFIG_32BIT |
| 103 | void __init fixrange_init(unsigned long start, unsigned long end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | pgd_t *pgd_base) |
| 105 | { |
| 106 | pgd_t *pgd; |
Ralf Baechle | c6e8b58 | 2005-02-10 12:19:59 +0000 | [diff] [blame] | 107 | pud_t *pud; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | pmd_t *pmd; |
| 109 | pte_t *pte; |
Ralf Baechle | c6e8b58 | 2005-02-10 12:19:59 +0000 | [diff] [blame] | 110 | int i, j, k; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | unsigned long vaddr; |
| 112 | |
| 113 | vaddr = start; |
| 114 | i = __pgd_offset(vaddr); |
Ralf Baechle | c6e8b58 | 2005-02-10 12:19:59 +0000 | [diff] [blame] | 115 | j = __pud_offset(vaddr); |
| 116 | k = __pmd_offset(vaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | pgd = pgd_base + i; |
| 118 | |
| 119 | for ( ; (i < PTRS_PER_PGD) && (vaddr != end); pgd++, i++) { |
Ralf Baechle | c6e8b58 | 2005-02-10 12:19:59 +0000 | [diff] [blame] | 120 | pud = (pud_t *)pgd; |
| 121 | for ( ; (j < PTRS_PER_PUD) && (vaddr != end); pud++, j++) { |
| 122 | pmd = (pmd_t *)pud; |
| 123 | for (; (k < PTRS_PER_PMD) && (vaddr != end); pmd++, k++) { |
| 124 | if (pmd_none(*pmd)) { |
| 125 | pte = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE); |
| 126 | set_pmd(pmd, __pmd(pte)); |
| 127 | if (pte != pte_offset_kernel(pmd, 0)) |
| 128 | BUG(); |
| 129 | } |
| 130 | vaddr += PMD_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } |
Ralf Baechle | c6e8b58 | 2005-02-10 12:19:59 +0000 | [diff] [blame] | 132 | k = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | j = 0; |
| 135 | } |
| 136 | } |
Ralf Baechle | dbc5716 | 2005-10-29 19:32:38 +0100 | [diff] [blame] | 137 | #endif /* CONFIG_32BIT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | #endif /* CONFIG_HIGHMEM */ |
| 139 | |
Yoichi Yuasa | b4819b5 | 2005-06-25 14:54:31 -0700 | [diff] [blame] | 140 | #ifndef CONFIG_NEED_MULTIPLE_NODES |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | extern void pagetable_init(void); |
| 142 | |
| 143 | void __init paging_init(void) |
| 144 | { |
| 145 | unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0}; |
| 146 | unsigned long max_dma, high, low; |
| 147 | |
| 148 | pagetable_init(); |
| 149 | |
| 150 | #ifdef CONFIG_HIGHMEM |
| 151 | kmap_init(); |
| 152 | #endif |
| 153 | |
| 154 | max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT; |
| 155 | low = max_low_pfn; |
| 156 | high = highend_pfn; |
| 157 | |
| 158 | #ifdef CONFIG_ISA |
| 159 | if (low < max_dma) |
| 160 | zones_size[ZONE_DMA] = low; |
| 161 | else { |
| 162 | zones_size[ZONE_DMA] = max_dma; |
| 163 | zones_size[ZONE_NORMAL] = low - max_dma; |
| 164 | } |
| 165 | #else |
| 166 | zones_size[ZONE_DMA] = low; |
| 167 | #endif |
| 168 | #ifdef CONFIG_HIGHMEM |
| 169 | if (cpu_has_dc_aliases) { |
| 170 | printk(KERN_WARNING "This processor doesn't support highmem."); |
| 171 | if (high - low) |
| 172 | printk(" %ldk highmem ignored", high - low); |
| 173 | printk("\n"); |
| 174 | } else |
| 175 | zones_size[ZONE_HIGHMEM] = high - low; |
| 176 | #endif |
| 177 | |
| 178 | free_area_init(zones_size); |
| 179 | } |
| 180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | static inline int page_is_ram(unsigned long pagenr) |
| 182 | { |
| 183 | int i; |
| 184 | |
| 185 | for (i = 0; i < boot_mem_map.nr_map; i++) { |
| 186 | unsigned long addr, end; |
| 187 | |
| 188 | if (boot_mem_map.map[i].type != BOOT_MEM_RAM) |
| 189 | /* not usable memory */ |
| 190 | continue; |
| 191 | |
| 192 | addr = PFN_UP(boot_mem_map.map[i].addr); |
| 193 | end = PFN_DOWN(boot_mem_map.map[i].addr + |
| 194 | boot_mem_map.map[i].size); |
| 195 | |
| 196 | if (pagenr >= addr && pagenr < end) |
| 197 | return 1; |
| 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
Daniel Jacobowitz | 3d50375 | 2005-01-20 19:59:54 -0500 | [diff] [blame] | 203 | static struct kcore_list kcore_mem, kcore_vmalloc; |
| 204 | #ifdef CONFIG_64BIT |
| 205 | static struct kcore_list kcore_kseg0; |
| 206 | #endif |
| 207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | void __init mem_init(void) |
| 209 | { |
| 210 | unsigned long codesize, reservedpages, datasize, initsize; |
| 211 | unsigned long tmp, ram; |
| 212 | |
| 213 | #ifdef CONFIG_HIGHMEM |
| 214 | #ifdef CONFIG_DISCONTIGMEM |
| 215 | #error "CONFIG_HIGHMEM and CONFIG_DISCONTIGMEM dont work together yet" |
| 216 | #endif |
| 217 | max_mapnr = num_physpages = highend_pfn; |
| 218 | #else |
| 219 | max_mapnr = num_physpages = max_low_pfn; |
| 220 | #endif |
| 221 | high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT); |
| 222 | |
| 223 | totalram_pages += free_all_bootmem(); |
| 224 | totalram_pages -= setup_zero_pages(); /* Setup zeroed pages. */ |
| 225 | |
| 226 | reservedpages = ram = 0; |
| 227 | for (tmp = 0; tmp < max_low_pfn; tmp++) |
| 228 | if (page_is_ram(tmp)) { |
| 229 | ram++; |
| 230 | if (PageReserved(mem_map+tmp)) |
| 231 | reservedpages++; |
| 232 | } |
| 233 | |
| 234 | #ifdef CONFIG_HIGHMEM |
| 235 | for (tmp = highstart_pfn; tmp < highend_pfn; tmp++) { |
| 236 | struct page *page = mem_map + tmp; |
| 237 | |
| 238 | if (!page_is_ram(tmp)) { |
| 239 | SetPageReserved(page); |
| 240 | continue; |
| 241 | } |
| 242 | ClearPageReserved(page); |
| 243 | #ifdef CONFIG_LIMITED_DMA |
| 244 | set_page_address(page, lowmem_page_address(page)); |
| 245 | #endif |
Nick Piggin | 7835e98 | 2006-03-22 00:08:40 -0800 | [diff] [blame] | 246 | init_page_count(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | __free_page(page); |
| 248 | totalhigh_pages++; |
| 249 | } |
| 250 | totalram_pages += totalhigh_pages; |
| 251 | #endif |
| 252 | |
| 253 | codesize = (unsigned long) &_etext - (unsigned long) &_text; |
| 254 | datasize = (unsigned long) &_edata - (unsigned long) &_etext; |
| 255 | initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin; |
| 256 | |
Daniel Jacobowitz | 3d50375 | 2005-01-20 19:59:54 -0500 | [diff] [blame] | 257 | #ifdef CONFIG_64BIT |
| 258 | if ((unsigned long) &_text > (unsigned long) CKSEG0) |
| 259 | /* The -4 is a hack so that user tools don't have to handle |
| 260 | the overflow. */ |
| 261 | kclist_add(&kcore_kseg0, (void *) CKSEG0, 0x80000000 - 4); |
| 262 | #endif |
| 263 | kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT); |
| 264 | kclist_add(&kcore_vmalloc, (void *)VMALLOC_START, |
| 265 | VMALLOC_END-VMALLOC_START); |
| 266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | printk(KERN_INFO "Memory: %luk/%luk available (%ldk kernel code, " |
| 268 | "%ldk reserved, %ldk data, %ldk init, %ldk highmem)\n", |
| 269 | (unsigned long) nr_free_pages() << (PAGE_SHIFT-10), |
| 270 | ram << (PAGE_SHIFT-10), |
| 271 | codesize >> 10, |
| 272 | reservedpages << (PAGE_SHIFT-10), |
| 273 | datasize >> 10, |
| 274 | initsize >> 10, |
| 275 | (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10))); |
| 276 | } |
Yoichi Yuasa | b4819b5 | 2005-06-25 14:54:31 -0700 | [diff] [blame] | 277 | #endif /* !CONFIG_NEED_MULTIPLE_NODES */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
| 279 | #ifdef CONFIG_BLK_DEV_INITRD |
| 280 | void free_initrd_mem(unsigned long start, unsigned long end) |
| 281 | { |
Ralf Baechle | 875d43e | 2005-09-03 15:56:16 -0700 | [diff] [blame] | 282 | #ifdef CONFIG_64BIT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | /* Switch from KSEG0 to XKPHYS addresses */ |
| 284 | start = (unsigned long)phys_to_virt(CPHYSADDR(start)); |
| 285 | end = (unsigned long)phys_to_virt(CPHYSADDR(end)); |
| 286 | #endif |
| 287 | if (start < end) |
| 288 | printk(KERN_INFO "Freeing initrd memory: %ldk freed\n", |
| 289 | (end - start) >> 10); |
| 290 | |
| 291 | for (; start < end; start += PAGE_SIZE) { |
| 292 | ClearPageReserved(virt_to_page(start)); |
Nick Piggin | 7835e98 | 2006-03-22 00:08:40 -0800 | [diff] [blame] | 293 | init_page_count(virt_to_page(start)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | free_page(start); |
| 295 | totalram_pages++; |
| 296 | } |
| 297 | } |
| 298 | #endif |
| 299 | |
| 300 | extern unsigned long prom_free_prom_memory(void); |
| 301 | |
| 302 | void free_initmem(void) |
| 303 | { |
| 304 | unsigned long addr, page, freed; |
| 305 | |
| 306 | freed = prom_free_prom_memory(); |
| 307 | |
| 308 | addr = (unsigned long) &__init_begin; |
| 309 | while (addr < (unsigned long) &__init_end) { |
Ralf Baechle | 875d43e | 2005-09-03 15:56:16 -0700 | [diff] [blame] | 310 | #ifdef CONFIG_64BIT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | page = PAGE_OFFSET | CPHYSADDR(addr); |
| 312 | #else |
| 313 | page = addr; |
| 314 | #endif |
| 315 | ClearPageReserved(virt_to_page(page)); |
Nick Piggin | 7835e98 | 2006-03-22 00:08:40 -0800 | [diff] [blame] | 316 | init_page_count(virt_to_page(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | free_page(page); |
| 318 | totalram_pages++; |
| 319 | freed += PAGE_SIZE; |
| 320 | addr += PAGE_SIZE; |
| 321 | } |
| 322 | printk(KERN_INFO "Freeing unused kernel memory: %ldk freed\n", |
| 323 | freed >> 10); |
| 324 | } |