Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/sh/mm/init.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 1999 Niibe Yutaka |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 5 | * Copyright (C) 2002 - 2007 Paul Mundt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * Based on linux/arch/i386/mm/init.c: |
| 8 | * Copyright (C) 1995 Linus Torvalds |
| 9 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/mm.h> |
| 11 | #include <linux/swap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/bootmem.h> |
Paul Mundt | 2cb7ce3 | 2006-09-27 18:20:58 +0900 | [diff] [blame] | 14 | #include <linux/proc_fs.h> |
Paul Mundt | 27641de | 2007-05-14 10:48:01 +0900 | [diff] [blame] | 15 | #include <linux/pagemap.h> |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 16 | #include <linux/percpu.h> |
| 17 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <asm/mmu_context.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/tlb.h> |
| 20 | #include <asm/cacheflush.h> |
| 21 | #include <asm/cache.h> |
| 22 | |
| 23 | DEFINE_PER_CPU(struct mmu_gather, mmu_gathers); |
| 24 | pgd_t swapper_pg_dir[PTRS_PER_PGD]; |
| 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | void (*copy_page)(void *from, void *to); |
| 27 | void (*clear_page)(void *to); |
| 28 | |
| 29 | void show_mem(void) |
| 30 | { |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 31 | int total = 0, reserved = 0, free = 0; |
| 32 | int shared = 0, cached = 0, slab = 0; |
| 33 | pg_data_t *pgdat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | printk("Mem-info:\n"); |
| 36 | show_free_areas(); |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 37 | |
| 38 | for_each_online_pgdat(pgdat) { |
Paul Mundt | dfbb904 | 2007-05-23 17:48:36 +0900 | [diff] [blame] | 39 | unsigned long flags, i; |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 40 | |
| 41 | pgdat_resize_lock(pgdat, &flags); |
Paul Mundt | dfbb904 | 2007-05-23 17:48:36 +0900 | [diff] [blame] | 42 | for (i = 0; i < pgdat->node_spanned_pages; i++) { |
| 43 | struct page *page = pgdat_page_nr(pgdat, i); |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 44 | total++; |
| 45 | if (PageReserved(page)) |
| 46 | reserved++; |
| 47 | else if (PageSwapCache(page)) |
| 48 | cached++; |
| 49 | else if (PageSlab(page)) |
| 50 | slab++; |
| 51 | else if (!page_count(page)) |
| 52 | free++; |
| 53 | else |
| 54 | shared += page_count(page) - 1; |
Paul Mundt | dfbb904 | 2007-05-23 17:48:36 +0900 | [diff] [blame] | 55 | } |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 56 | pgdat_resize_unlock(pgdat, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 58 | |
| 59 | printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10)); |
| 60 | printk("%d pages of RAM\n", total); |
| 61 | printk("%d free pages\n", free); |
| 62 | printk("%d reserved pages\n", reserved); |
| 63 | printk("%d slab pages\n", slab); |
| 64 | printk("%d pages shared\n", shared); |
| 65 | printk("%d pages swap cached\n", cached); |
Paul Mundt | 5f8c990 | 2007-05-08 11:55:21 +0900 | [diff] [blame] | 66 | printk(KERN_INFO "Total of %ld pages in page table cache\n", |
| 67 | quicklist_total_size()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Yoshinori Sato | 11cbb70 | 2006-12-07 18:07:27 +0900 | [diff] [blame] | 70 | #ifdef CONFIG_MMU |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | static void set_pte_phys(unsigned long addr, unsigned long phys, pgprot_t prot) |
| 72 | { |
| 73 | pgd_t *pgd; |
Paul Mundt | 26ff6c1 | 2006-09-27 15:13:36 +0900 | [diff] [blame] | 74 | pud_t *pud; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | pmd_t *pmd; |
| 76 | pte_t *pte; |
| 77 | |
Stuart Menefy | 99a596f | 2006-11-21 15:38:05 +0900 | [diff] [blame] | 78 | pgd = pgd_offset_k(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | if (pgd_none(*pgd)) { |
| 80 | pgd_ERROR(*pgd); |
| 81 | return; |
| 82 | } |
| 83 | |
Stuart Menefy | 99a596f | 2006-11-21 15:38:05 +0900 | [diff] [blame] | 84 | pud = pud_alloc(NULL, pgd, addr); |
| 85 | if (unlikely(!pud)) { |
| 86 | pud_ERROR(*pud); |
| 87 | return; |
Paul Mundt | 26ff6c1 | 2006-09-27 15:13:36 +0900 | [diff] [blame] | 88 | } |
| 89 | |
Stuart Menefy | 99a596f | 2006-11-21 15:38:05 +0900 | [diff] [blame] | 90 | pmd = pmd_alloc(NULL, pud, addr); |
| 91 | if (unlikely(!pmd)) { |
| 92 | pmd_ERROR(*pmd); |
| 93 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | pte = pte_offset_kernel(pmd, addr); |
| 97 | if (!pte_none(*pte)) { |
| 98 | pte_ERROR(*pte); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, prot)); |
| 103 | |
Paul Mundt | ea9af69 | 2006-12-25 19:28:54 +0900 | [diff] [blame] | 104 | flush_tlb_one(get_asid(), addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* |
| 108 | * As a performance optimization, other platforms preserve the fixmap mapping |
| 109 | * across a context switch, we don't presently do this, but this could be done |
| 110 | * in a similar fashion as to the wired TLB interface that sh64 uses (by way |
Simon Arlott | e868d61 | 2007-05-14 08:15:10 +0900 | [diff] [blame] | 111 | * of the memory mapped UTLB configuration) -- this unfortunately forces us to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | * give up a TLB entry for each mapping we want to preserve. While this may be |
| 113 | * viable for a small number of fixmaps, it's not particularly useful for |
| 114 | * everything and needs to be carefully evaluated. (ie, we may want this for |
| 115 | * the vsyscall page). |
| 116 | * |
| 117 | * XXX: Perhaps add a _PAGE_WIRED flag or something similar that we can pass |
| 118 | * in at __set_fixmap() time to determine the appropriate behavior to follow. |
| 119 | * |
| 120 | * -- PFM. |
| 121 | */ |
| 122 | void __set_fixmap(enum fixed_addresses idx, unsigned long phys, pgprot_t prot) |
| 123 | { |
| 124 | unsigned long address = __fix_to_virt(idx); |
| 125 | |
| 126 | if (idx >= __end_of_fixed_addresses) { |
| 127 | BUG(); |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | set_pte_phys(address, phys, prot); |
| 132 | } |
Yoshinori Sato | 11cbb70 | 2006-12-07 18:07:27 +0900 | [diff] [blame] | 133 | #endif /* CONFIG_MMU */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
| 135 | /* References to section boundaries */ |
| 136 | |
| 137 | extern char _text, _etext, _edata, __bss_start, _end; |
| 138 | extern char __init_begin, __init_end; |
| 139 | |
| 140 | /* |
| 141 | * paging_init() sets up the page tables |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | */ |
| 143 | void __init paging_init(void) |
| 144 | { |
Paul Mundt | 2de212e | 2007-06-06 12:09:54 +0900 | [diff] [blame^] | 145 | unsigned long max_zone_pfns[MAX_NR_ZONES]; |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 146 | int nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 148 | /* We don't need to map the kernel through the TLB, as |
| 149 | * it is permanatly mapped using P1. So clear the |
| 150 | * entire pgd. */ |
| 151 | memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
Stuart Menefy | 6e4662f | 2006-11-21 13:53:44 +0900 | [diff] [blame] | 153 | /* Set an initial value for the MMU.TTB so we don't have to |
| 154 | * check for a null value. */ |
| 155 | set_TTB(swapper_pg_dir); |
| 156 | |
Paul Mundt | 2de212e | 2007-06-06 12:09:54 +0900 | [diff] [blame^] | 157 | memset(max_zone_pfns, 0, sizeof(max_zone_pfns)); |
| 158 | |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 159 | for_each_online_node(nid) { |
| 160 | pg_data_t *pgdat = NODE_DATA(nid); |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 161 | unsigned long low, start_pfn; |
| 162 | |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 163 | start_pfn = pgdat->bdata->node_boot_start >> PAGE_SHIFT; |
| 164 | low = pgdat->bdata->node_low_pfn; |
| 165 | |
Paul Mundt | 2de212e | 2007-06-06 12:09:54 +0900 | [diff] [blame^] | 166 | if (max_zone_pfns[ZONE_NORMAL] < low) |
| 167 | max_zone_pfns[ZONE_NORMAL] = low; |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 168 | |
| 169 | printk("Node %u: start_pfn = 0x%lx, low = 0x%lx\n", |
| 170 | nid, start_pfn, low); |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 171 | } |
Paul Mundt | 2de212e | 2007-06-06 12:09:54 +0900 | [diff] [blame^] | 172 | |
| 173 | free_area_init_nodes(max_zone_pfns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Paul Mundt | 2cb7ce3 | 2006-09-27 18:20:58 +0900 | [diff] [blame] | 176 | static struct kcore_list kcore_mem, kcore_vmalloc; |
| 177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | void __init mem_init(void) |
| 179 | { |
Paul Mundt | dfbb904 | 2007-05-23 17:48:36 +0900 | [diff] [blame] | 180 | int codesize, datasize, initsize; |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 181 | int nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
Paul Mundt | 2de212e | 2007-06-06 12:09:54 +0900 | [diff] [blame^] | 183 | num_physpages = 0; |
| 184 | high_memory = NULL; |
| 185 | |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 186 | for_each_online_node(nid) { |
| 187 | pg_data_t *pgdat = NODE_DATA(nid); |
| 188 | unsigned long node_pages = 0; |
| 189 | void *node_high_memory; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 191 | num_physpages += pgdat->node_present_pages; |
| 192 | |
| 193 | if (pgdat->node_spanned_pages) |
| 194 | node_pages = free_all_bootmem_node(pgdat); |
| 195 | |
| 196 | totalram_pages += node_pages; |
| 197 | |
Paul Mundt | 2de212e | 2007-06-06 12:09:54 +0900 | [diff] [blame^] | 198 | node_high_memory = (void *)__va((pgdat->node_start_pfn + |
| 199 | pgdat->node_spanned_pages) << |
| 200 | PAGE_SHIFT); |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 201 | if (node_high_memory > high_memory) |
| 202 | high_memory = node_high_memory; |
| 203 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
| 205 | /* clear the zero-page */ |
| 206 | memset(empty_zero_page, 0, PAGE_SIZE); |
| 207 | __flush_wback_region(empty_zero_page, PAGE_SIZE); |
| 208 | |
Paul Mundt | 65463b7 | 2005-11-07 00:58:24 -0800 | [diff] [blame] | 209 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | * Setup wrappers for copy/clear_page(), these will get overridden |
| 211 | * later in the boot process if a better method is available. |
| 212 | */ |
Yoshinori Sato | e96636c | 2006-09-27 17:21:02 +0900 | [diff] [blame] | 213 | #ifdef CONFIG_MMU |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | copy_page = copy_page_slow; |
| 215 | clear_page = clear_page_slow; |
Yoshinori Sato | e96636c | 2006-09-27 17:21:02 +0900 | [diff] [blame] | 216 | #else |
| 217 | copy_page = copy_page_nommu; |
| 218 | clear_page = clear_page_nommu; |
| 219 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | codesize = (unsigned long) &_etext - (unsigned long) &_text; |
| 222 | datasize = (unsigned long) &_edata - (unsigned long) &_etext; |
| 223 | initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin; |
| 224 | |
Paul Mundt | 2cb7ce3 | 2006-09-27 18:20:58 +0900 | [diff] [blame] | 225 | kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT); |
| 226 | kclist_add(&kcore_vmalloc, (void *)VMALLOC_START, |
| 227 | VMALLOC_END - VMALLOC_START); |
| 228 | |
| 229 | printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, " |
Paul Mundt | dfbb904 | 2007-05-23 17:48:36 +0900 | [diff] [blame] | 230 | "%dk data, %dk init)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | (unsigned long) nr_free_pages() << (PAGE_SHIFT-10), |
Paul Mundt | 2de212e | 2007-06-06 12:09:54 +0900 | [diff] [blame^] | 232 | num_physpages << (PAGE_SHIFT-10), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | codesize >> 10, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | datasize >> 10, |
| 235 | initsize >> 10); |
| 236 | |
| 237 | p3_cache_init(); |
Paul Mundt | 19f9a34 | 2006-09-27 18:33:49 +0900 | [diff] [blame] | 238 | |
| 239 | /* Initialize the vDSO */ |
| 240 | vsyscall_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | void free_initmem(void) |
| 244 | { |
| 245 | unsigned long addr; |
Paul Mundt | 65463b7 | 2005-11-07 00:58:24 -0800 | [diff] [blame] | 246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | addr = (unsigned long)(&__init_begin); |
| 248 | for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) { |
| 249 | ClearPageReserved(virt_to_page(addr)); |
Nick Piggin | 7835e98 | 2006-03-22 00:08:40 -0800 | [diff] [blame] | 250 | init_page_count(virt_to_page(addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | free_page(addr); |
| 252 | totalram_pages++; |
| 253 | } |
Paul Mundt | 2de212e | 2007-06-06 12:09:54 +0900 | [diff] [blame^] | 254 | printk("Freeing unused kernel memory: %dk freed\n", |
| 255 | (&__init_end - &__init_begin) >> 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | #ifdef CONFIG_BLK_DEV_INITRD |
| 259 | void free_initrd_mem(unsigned long start, unsigned long end) |
| 260 | { |
| 261 | unsigned long p; |
| 262 | for (p = start; p < end; p += PAGE_SIZE) { |
| 263 | ClearPageReserved(virt_to_page(p)); |
Nick Piggin | 7835e98 | 2006-03-22 00:08:40 -0800 | [diff] [blame] | 264 | init_page_count(virt_to_page(p)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | free_page(p); |
| 266 | totalram_pages++; |
| 267 | } |
Paul Mundt | 2de212e | 2007-06-06 12:09:54 +0900 | [diff] [blame^] | 268 | printk("Freeing initrd memory: %ldk freed\n", (end - start) >> 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | } |
| 270 | #endif |