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