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]; |
Stuart Menefy | cbaa118 | 2007-11-30 17:06:36 +0900 | [diff] [blame^] | 26 | unsigned long cached_to_uncached = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | void show_mem(void) |
| 29 | { |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 30 | int total = 0, reserved = 0, free = 0; |
| 31 | int shared = 0, cached = 0, slab = 0; |
| 32 | pg_data_t *pgdat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | printk("Mem-info:\n"); |
| 35 | show_free_areas(); |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 36 | |
| 37 | for_each_online_pgdat(pgdat) { |
Paul Mundt | dfbb904 | 2007-05-23 17:48:36 +0900 | [diff] [blame] | 38 | unsigned long flags, i; |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 39 | |
| 40 | pgdat_resize_lock(pgdat, &flags); |
Paul Mundt | dfbb904 | 2007-05-23 17:48:36 +0900 | [diff] [blame] | 41 | for (i = 0; i < pgdat->node_spanned_pages; i++) { |
| 42 | struct page *page = pgdat_page_nr(pgdat, i); |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 43 | total++; |
| 44 | if (PageReserved(page)) |
| 45 | reserved++; |
| 46 | else if (PageSwapCache(page)) |
| 47 | cached++; |
| 48 | else if (PageSlab(page)) |
| 49 | slab++; |
| 50 | else if (!page_count(page)) |
| 51 | free++; |
| 52 | else |
| 53 | shared += page_count(page) - 1; |
Paul Mundt | dfbb904 | 2007-05-23 17:48:36 +0900 | [diff] [blame] | 54 | } |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 55 | pgdat_resize_unlock(pgdat, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | } |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 57 | |
| 58 | printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10)); |
| 59 | printk("%d pages of RAM\n", total); |
| 60 | printk("%d free pages\n", free); |
| 61 | printk("%d reserved pages\n", reserved); |
| 62 | printk("%d slab pages\n", slab); |
| 63 | printk("%d pages shared\n", shared); |
| 64 | printk("%d pages swap cached\n", cached); |
Paul Mundt | 5f8c990 | 2007-05-08 11:55:21 +0900 | [diff] [blame] | 65 | printk(KERN_INFO "Total of %ld pages in page table cache\n", |
| 66 | quicklist_total_size()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Yoshinori Sato | 11cbb70 | 2006-12-07 18:07:27 +0900 | [diff] [blame] | 69 | #ifdef CONFIG_MMU |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | static void set_pte_phys(unsigned long addr, unsigned long phys, pgprot_t prot) |
| 71 | { |
| 72 | pgd_t *pgd; |
Paul Mundt | 26ff6c1 | 2006-09-27 15:13:36 +0900 | [diff] [blame] | 73 | pud_t *pud; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | pmd_t *pmd; |
| 75 | pte_t *pte; |
| 76 | |
Stuart Menefy | 99a596f | 2006-11-21 15:38:05 +0900 | [diff] [blame] | 77 | pgd = pgd_offset_k(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | if (pgd_none(*pgd)) { |
| 79 | pgd_ERROR(*pgd); |
| 80 | return; |
| 81 | } |
| 82 | |
Stuart Menefy | 99a596f | 2006-11-21 15:38:05 +0900 | [diff] [blame] | 83 | pud = pud_alloc(NULL, pgd, addr); |
| 84 | if (unlikely(!pud)) { |
| 85 | pud_ERROR(*pud); |
| 86 | return; |
Paul Mundt | 26ff6c1 | 2006-09-27 15:13:36 +0900 | [diff] [blame] | 87 | } |
| 88 | |
Stuart Menefy | 99a596f | 2006-11-21 15:38:05 +0900 | [diff] [blame] | 89 | pmd = pmd_alloc(NULL, pud, addr); |
| 90 | if (unlikely(!pmd)) { |
| 91 | pmd_ERROR(*pmd); |
| 92 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | pte = pte_offset_kernel(pmd, addr); |
| 96 | if (!pte_none(*pte)) { |
| 97 | pte_ERROR(*pte); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, prot)); |
| 102 | |
Stuart Menefy | cbaa118 | 2007-11-30 17:06:36 +0900 | [diff] [blame^] | 103 | if (cached_to_uncached) |
| 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | /* |
| 136 | * paging_init() sets up the page tables |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | */ |
| 138 | void __init paging_init(void) |
| 139 | { |
Paul Mundt | 2de212e | 2007-06-06 12:09:54 +0900 | [diff] [blame] | 140 | unsigned long max_zone_pfns[MAX_NR_ZONES]; |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 141 | int nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 143 | /* We don't need to map the kernel through the TLB, as |
| 144 | * it is permanatly mapped using P1. So clear the |
| 145 | * entire pgd. */ |
| 146 | memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
Stuart Menefy | 6e4662f | 2006-11-21 13:53:44 +0900 | [diff] [blame] | 148 | /* Set an initial value for the MMU.TTB so we don't have to |
| 149 | * check for a null value. */ |
| 150 | set_TTB(swapper_pg_dir); |
| 151 | |
Paul Mundt | 2de212e | 2007-06-06 12:09:54 +0900 | [diff] [blame] | 152 | memset(max_zone_pfns, 0, sizeof(max_zone_pfns)); |
| 153 | |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 154 | for_each_online_node(nid) { |
| 155 | pg_data_t *pgdat = NODE_DATA(nid); |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 156 | unsigned long low, start_pfn; |
| 157 | |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 158 | start_pfn = pgdat->bdata->node_boot_start >> PAGE_SHIFT; |
| 159 | low = pgdat->bdata->node_low_pfn; |
| 160 | |
Paul Mundt | 2de212e | 2007-06-06 12:09:54 +0900 | [diff] [blame] | 161 | if (max_zone_pfns[ZONE_NORMAL] < low) |
| 162 | max_zone_pfns[ZONE_NORMAL] = low; |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 163 | |
| 164 | printk("Node %u: start_pfn = 0x%lx, low = 0x%lx\n", |
| 165 | nid, start_pfn, low); |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 166 | } |
Paul Mundt | 2de212e | 2007-06-06 12:09:54 +0900 | [diff] [blame] | 167 | |
| 168 | free_area_init_nodes(max_zone_pfns); |
Stuart Menefy | cbaa118 | 2007-11-30 17:06:36 +0900 | [diff] [blame^] | 169 | |
| 170 | /* Set up the uncached fixmap */ |
| 171 | set_fixmap_nocache(FIX_UNCACHED, __pa(&__uncached_start)); |
| 172 | |
| 173 | #ifdef CONFIG_29BIT |
| 174 | /* |
| 175 | * Handle trivial transitions between cached and uncached |
| 176 | * segments, making use of the 1:1 mapping relationship in |
| 177 | * 512MB lowmem. |
| 178 | */ |
| 179 | cached_to_uncached = P2SEG - P1SEG; |
| 180 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Paul Mundt | 2cb7ce3 | 2006-09-27 18:20:58 +0900 | [diff] [blame] | 183 | static struct kcore_list kcore_mem, kcore_vmalloc; |
Paul Mundt | ba2727b | 2007-11-20 15:14:48 +0900 | [diff] [blame] | 184 | int after_bootmem = 0; |
Paul Mundt | 2cb7ce3 | 2006-09-27 18:20:58 +0900 | [diff] [blame] | 185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | void __init mem_init(void) |
| 187 | { |
Paul Mundt | dfbb904 | 2007-05-23 17:48:36 +0900 | [diff] [blame] | 188 | int codesize, datasize, initsize; |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 189 | int nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Paul Mundt | 2de212e | 2007-06-06 12:09:54 +0900 | [diff] [blame] | 191 | num_physpages = 0; |
| 192 | high_memory = NULL; |
| 193 | |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 194 | for_each_online_node(nid) { |
| 195 | pg_data_t *pgdat = NODE_DATA(nid); |
| 196 | unsigned long node_pages = 0; |
| 197 | void *node_high_memory; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 199 | num_physpages += pgdat->node_present_pages; |
| 200 | |
| 201 | if (pgdat->node_spanned_pages) |
| 202 | node_pages = free_all_bootmem_node(pgdat); |
| 203 | |
| 204 | totalram_pages += node_pages; |
| 205 | |
Paul Mundt | 2de212e | 2007-06-06 12:09:54 +0900 | [diff] [blame] | 206 | node_high_memory = (void *)__va((pgdat->node_start_pfn + |
| 207 | pgdat->node_spanned_pages) << |
| 208 | PAGE_SHIFT); |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 209 | if (node_high_memory > high_memory) |
| 210 | high_memory = node_high_memory; |
| 211 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
| 213 | /* clear the zero-page */ |
| 214 | memset(empty_zero_page, 0, PAGE_SIZE); |
| 215 | __flush_wback_region(empty_zero_page, PAGE_SIZE); |
| 216 | |
Paul Mundt | ba2727b | 2007-11-20 15:14:48 +0900 | [diff] [blame] | 217 | after_bootmem = 1; |
| 218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | codesize = (unsigned long) &_etext - (unsigned long) &_text; |
| 220 | datasize = (unsigned long) &_edata - (unsigned long) &_etext; |
| 221 | initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin; |
| 222 | |
Paul Mundt | 2cb7ce3 | 2006-09-27 18:20:58 +0900 | [diff] [blame] | 223 | kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT); |
| 224 | kclist_add(&kcore_vmalloc, (void *)VMALLOC_START, |
| 225 | VMALLOC_END - VMALLOC_START); |
| 226 | |
| 227 | printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, " |
Paul Mundt | dfbb904 | 2007-05-23 17:48:36 +0900 | [diff] [blame] | 228 | "%dk data, %dk init)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | (unsigned long) nr_free_pages() << (PAGE_SHIFT-10), |
Paul Mundt | 2de212e | 2007-06-06 12:09:54 +0900 | [diff] [blame] | 230 | num_physpages << (PAGE_SHIFT-10), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | codesize >> 10, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | datasize >> 10, |
| 233 | initsize >> 10); |
| 234 | |
| 235 | p3_cache_init(); |
Paul Mundt | 19f9a34 | 2006-09-27 18:33:49 +0900 | [diff] [blame] | 236 | |
| 237 | /* Initialize the vDSO */ |
| 238 | vsyscall_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | void free_initmem(void) |
| 242 | { |
| 243 | unsigned long addr; |
Paul Mundt | 65463b7 | 2005-11-07 00:58:24 -0800 | [diff] [blame] | 244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | addr = (unsigned long)(&__init_begin); |
| 246 | for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) { |
| 247 | ClearPageReserved(virt_to_page(addr)); |
Nick Piggin | 7835e98 | 2006-03-22 00:08:40 -0800 | [diff] [blame] | 248 | init_page_count(virt_to_page(addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | free_page(addr); |
| 250 | totalram_pages++; |
| 251 | } |
Paul Mundt | 07cbb41 | 2007-06-06 12:23:06 +0900 | [diff] [blame] | 252 | printk("Freeing unused kernel memory: %ldk freed\n", |
| 253 | ((unsigned long)&__init_end - |
| 254 | (unsigned long)&__init_begin) >> 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | #ifdef CONFIG_BLK_DEV_INITRD |
| 258 | void free_initrd_mem(unsigned long start, unsigned long end) |
| 259 | { |
| 260 | unsigned long p; |
| 261 | for (p = start; p < end; p += PAGE_SIZE) { |
| 262 | ClearPageReserved(virt_to_page(p)); |
Nick Piggin | 7835e98 | 2006-03-22 00:08:40 -0800 | [diff] [blame] | 263 | init_page_count(virt_to_page(p)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | free_page(p); |
| 265 | totalram_pages++; |
| 266 | } |
Paul Mundt | 2de212e | 2007-06-06 12:09:54 +0900 | [diff] [blame] | 267 | printk("Freeing initrd memory: %ldk freed\n", (end - start) >> 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
| 269 | #endif |
Paul Mundt | 33d63bd | 2007-06-07 11:32:52 +0900 | [diff] [blame] | 270 | |
| 271 | #ifdef CONFIG_MEMORY_HOTPLUG |
| 272 | void online_page(struct page *page) |
| 273 | { |
| 274 | ClearPageReserved(page); |
| 275 | init_page_count(page); |
| 276 | __free_page(page); |
| 277 | totalram_pages++; |
| 278 | num_physpages++; |
| 279 | } |
| 280 | |
| 281 | int arch_add_memory(int nid, u64 start, u64 size) |
| 282 | { |
| 283 | pg_data_t *pgdat; |
| 284 | unsigned long start_pfn = start >> PAGE_SHIFT; |
| 285 | unsigned long nr_pages = size >> PAGE_SHIFT; |
| 286 | int ret; |
| 287 | |
| 288 | pgdat = NODE_DATA(nid); |
| 289 | |
| 290 | /* We only have ZONE_NORMAL, so this is easy.. */ |
| 291 | ret = __add_pages(pgdat->node_zones + ZONE_NORMAL, start_pfn, nr_pages); |
| 292 | if (unlikely(ret)) |
| 293 | printk("%s: Failed, __add_pages() == %d\n", __FUNCTION__, ret); |
| 294 | |
| 295 | return ret; |
| 296 | } |
| 297 | EXPORT_SYMBOL_GPL(arch_add_memory); |
| 298 | |
Paul Mundt | 357d594 | 2007-06-11 15:32:07 +0900 | [diff] [blame] | 299 | #ifdef CONFIG_NUMA |
Paul Mundt | 33d63bd | 2007-06-07 11:32:52 +0900 | [diff] [blame] | 300 | int memory_add_physaddr_to_nid(u64 addr) |
| 301 | { |
| 302 | /* Node 0 for now.. */ |
| 303 | return 0; |
| 304 | } |
| 305 | EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid); |
| 306 | #endif |
Paul Mundt | 357d594 | 2007-06-11 15:32:07 +0900 | [diff] [blame] | 307 | #endif |