Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
at v2.6.21 406 lines 9.9 kB view raw
1/* 2 * linux/arch/arm26/mm/init.c 3 * 4 * Copyright (C) 1995-2002 Russell King 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 2 as 8 * published by the Free Software Foundation. 9 */ 10#include <linux/signal.h> 11#include <linux/sched.h> 12#include <linux/kernel.h> 13#include <linux/errno.h> 14#include <linux/string.h> 15#include <linux/types.h> 16#include <linux/ptrace.h> 17#include <linux/mman.h> 18#include <linux/mm.h> 19#include <linux/swap.h> 20#include <linux/smp.h> 21#include <linux/init.h> 22#include <linux/initrd.h> 23#include <linux/bootmem.h> 24#include <linux/blkdev.h> 25#include <linux/pfn.h> 26 27#include <asm/segment.h> 28#include <asm/mach-types.h> 29#include <asm/dma.h> 30#include <asm/hardware.h> 31#include <asm/setup.h> 32#include <asm/tlb.h> 33 34#include <asm/map.h> 35 36 37#define TABLE_SIZE PTRS_PER_PTE * sizeof(pte_t)) 38 39struct mmu_gather mmu_gathers[NR_CPUS]; 40 41extern pgd_t swapper_pg_dir[PTRS_PER_PGD]; 42extern char _stext, _text, _etext, _end, __init_begin, __init_end; 43#ifdef CONFIG_XIP_KERNEL 44extern char _endtext, _sdata; 45#endif 46extern unsigned long phys_initrd_start; 47extern unsigned long phys_initrd_size; 48 49/* 50 * The sole use of this is to pass memory configuration 51 * data from paging_init to mem_init. 52 */ 53static struct meminfo meminfo __initdata = { 0, }; 54 55/* 56 * empty_zero_page is a special page that is used for 57 * zero-initialized data and COW. 58 */ 59struct page *empty_zero_page; 60 61void show_mem(void) 62{ 63 int free = 0, total = 0, reserved = 0; 64 int shared = 0, cached = 0, slab = 0; 65 struct page *page, *end; 66 67 printk("Mem-info:\n"); 68 show_free_areas(); 69 printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10)); 70 71 72 page = NODE_MEM_MAP(0); 73 end = page + NODE_DATA(0)->node_spanned_pages; 74 75 do { 76 total++; 77 if (PageReserved(page)) 78 reserved++; 79 else if (PageSwapCache(page)) 80 cached++; 81 else if (PageSlab(page)) 82 slab++; 83 else if (!page_count(page)) 84 free++; 85 else 86 shared += page_count(page) - 1; 87 page++; 88 } while (page < end); 89 90 printk("%d pages of RAM\n", total); 91 printk("%d free pages\n", free); 92 printk("%d reserved pages\n", reserved); 93 printk("%d slab pages\n", slab); 94 printk("%d pages shared\n", shared); 95 printk("%d pages swap cached\n", cached); 96} 97 98struct node_info { 99 unsigned int start; 100 unsigned int end; 101 int bootmap_pages; 102}; 103 104/* 105 * FIXME: We really want to avoid allocating the bootmap bitmap 106 * over the top of the initrd. Hopefully, this is located towards 107 * the start of a bank, so if we allocate the bootmap bitmap at 108 * the end, we won't clash. 109 */ 110static unsigned int __init 111find_bootmap_pfn(struct meminfo *mi, unsigned int bootmap_pages) 112{ 113 unsigned int start_pfn, bootmap_pfn; 114 unsigned int start, end; 115 116 start_pfn = PFN_UP((unsigned long)&_end); 117 bootmap_pfn = 0; 118 119 /* ARM26 machines only have one node */ 120 if (mi->bank->node != 0) 121 BUG(); 122 123 start = PFN_UP(mi->bank->start); 124 end = PFN_DOWN(mi->bank->size + mi->bank->start); 125 126 if (start < start_pfn) 127 start = start_pfn; 128 129 if (end <= start) 130 BUG(); 131 132 if (end - start >= bootmap_pages) 133 bootmap_pfn = start; 134 else 135 BUG(); 136 137 return bootmap_pfn; 138} 139 140/* 141 * Scan the memory info structure and pull out: 142 * - the end of memory 143 * - the number of nodes 144 * - the pfn range of each node 145 * - the number of bootmem bitmap pages 146 */ 147static void __init 148find_memend_and_nodes(struct meminfo *mi, struct node_info *np) 149{ 150 unsigned int memend_pfn = 0; 151 152 nodes_clear(node_online_map); 153 node_set_online(0); 154 155 np->bootmap_pages = 0; 156 157 if (mi->bank->size == 0) { 158 BUG(); 159 } 160 161 /* 162 * Get the start and end pfns for this bank 163 */ 164 np->start = PFN_UP(mi->bank->start); 165 np->end = PFN_DOWN(mi->bank->start + mi->bank->size); 166 167 if (memend_pfn < np->end) 168 memend_pfn = np->end; 169 170 /* 171 * Calculate the number of pages we require to 172 * store the bootmem bitmaps. 173 */ 174 np->bootmap_pages = bootmem_bootmap_pages(np->end - np->start); 175 176 /* 177 * This doesn't seem to be used by the Linux memory 178 * manager any more. If we can get rid of it, we 179 * also get rid of some of the stuff above as well. 180 */ 181 max_low_pfn = memend_pfn - PFN_DOWN(PHYS_OFFSET); 182 max_pfn = memend_pfn - PFN_DOWN(PHYS_OFFSET); 183 mi->end = memend_pfn << PAGE_SHIFT; 184 185} 186 187/* 188 * Initialise the bootmem allocator for all nodes. This is called 189 * early during the architecture specific initialisation. 190 */ 191void __init bootmem_init(struct meminfo *mi) 192{ 193 struct node_info node_info; 194 unsigned int bootmap_pfn; 195 pg_data_t *pgdat = NODE_DATA(0); 196 197 find_memend_and_nodes(mi, &node_info); 198 199 bootmap_pfn = find_bootmap_pfn(mi, node_info.bootmap_pages); 200 201 /* 202 * Note that node 0 must always have some pages. 203 */ 204 if (node_info.end == 0) 205 BUG(); 206 207 /* 208 * Initialise the bootmem allocator. 209 */ 210 init_bootmem_node(pgdat, bootmap_pfn, node_info.start, node_info.end); 211 212 /* 213 * Register all available RAM in this node with the bootmem allocator. 214 */ 215 free_bootmem_node(pgdat, mi->bank->start, mi->bank->size); 216 217 /* 218 * Register the kernel text and data with bootmem. 219 * Note: with XIP we dont register .text since 220 * its in ROM. 221 */ 222#ifdef CONFIG_XIP_KERNEL 223 reserve_bootmem_node(pgdat, __pa(&_sdata), &_end - &_sdata); 224#else 225 reserve_bootmem_node(pgdat, __pa(&_stext), &_end - &_stext); 226#endif 227 228 /* 229 * And don't forget to reserve the allocator bitmap, 230 * which will be freed later. 231 */ 232 reserve_bootmem_node(pgdat, bootmap_pfn << PAGE_SHIFT, 233 node_info.bootmap_pages << PAGE_SHIFT); 234 235 /* 236 * These should likewise go elsewhere. They pre-reserve 237 * the screen memory region at the start of main system 238 * memory. FIXME - screen RAM is not 512K! 239 */ 240 reserve_bootmem_node(pgdat, 0x02000000, 0x00080000); 241 242#ifdef CONFIG_BLK_DEV_INITRD 243 initrd_start = phys_initrd_start; 244 initrd_end = initrd_start + phys_initrd_size; 245 246 /* Achimedes machines only have one node, so initrd is in node 0 */ 247#ifdef CONFIG_XIP_KERNEL 248 /* Only reserve initrd space if it is in RAM */ 249 if(initrd_start && initrd_start < 0x03000000){ 250#else 251 if(initrd_start){ 252#endif 253 reserve_bootmem_node(pgdat, __pa(initrd_start), 254 initrd_end - initrd_start); 255 } 256#endif /* CONFIG_BLK_DEV_INITRD */ 257 258 259} 260 261/* 262 * paging_init() sets up the page tables, initialises the zone memory 263 * maps, and sets up the zero page, bad page and bad page tables. 264 */ 265void __init paging_init(struct meminfo *mi) 266{ 267 void *zero_page; 268 unsigned long zone_size[MAX_NR_ZONES]; 269 unsigned long zhole_size[MAX_NR_ZONES]; 270 struct bootmem_data *bdata; 271 pg_data_t *pgdat; 272 int i; 273 274 memcpy(&meminfo, mi, sizeof(meminfo)); 275 276 /* 277 * allocate the zero page. Note that we count on this going ok. 278 */ 279 zero_page = alloc_bootmem_low_pages(PAGE_SIZE); 280 281 /* 282 * initialise the page tables. 283 */ 284 memtable_init(mi); 285 flush_tlb_all(); 286 287 /* 288 * initialise the zones in node 0 (archimedes have only 1 node) 289 */ 290 291 for (i = 0; i < MAX_NR_ZONES; i++) { 292 zone_size[i] = 0; 293 zhole_size[i] = 0; 294 } 295 296 pgdat = NODE_DATA(0); 297 bdata = pgdat->bdata; 298 zone_size[0] = bdata->node_low_pfn - 299 (bdata->node_boot_start >> PAGE_SHIFT); 300 if (!zone_size[0]) 301 BUG(); 302 pgdat->node_mem_map = NULL; 303 free_area_init_node(0, pgdat, zone_size, 304 bdata->node_boot_start >> PAGE_SHIFT, zhole_size); 305 306 /* 307 * finish off the bad pages once 308 * the mem_map is initialised 309 */ 310 memzero(zero_page, PAGE_SIZE); 311 empty_zero_page = virt_to_page(zero_page); 312} 313 314static inline void free_area(unsigned long addr, unsigned long end, char *s) 315{ 316 unsigned int size = (end - addr) >> 10; 317 318 for (; addr < end; addr += PAGE_SIZE) { 319 struct page *page = virt_to_page(addr); 320 ClearPageReserved(page); 321 init_page_count(page); 322 free_page(addr); 323 totalram_pages++; 324 } 325 326 if (size && s) 327 printk(KERN_INFO "Freeing %s memory: %dK\n", s, size); 328} 329 330/* 331 * mem_init() marks the free areas in the mem_map and tells us how much 332 * memory is free. This is done after various parts of the system have 333 * claimed their memory after the kernel image. 334 */ 335void __init mem_init(void) 336{ 337 unsigned int codepages, datapages, initpages; 338 pg_data_t *pgdat = NODE_DATA(0); 339 extern int sysctl_overcommit_memory; 340 341 342 /* Note: data pages includes BSS */ 343#ifdef CONFIG_XIP_KERNEL 344 codepages = &_endtext - &_text; 345 datapages = &_end - &_sdata; 346#else 347 codepages = &_etext - &_text; 348 datapages = &_end - &_etext; 349#endif 350 initpages = &__init_end - &__init_begin; 351 352 high_memory = (void *)__va(meminfo.end); 353 max_mapnr = virt_to_page(high_memory) - mem_map; 354 355 /* this will put all unused low memory onto the freelists */ 356 if (pgdat->node_spanned_pages != 0) 357 totalram_pages += free_all_bootmem_node(pgdat); 358 359 num_physpages = meminfo.bank[0].size >> PAGE_SHIFT; 360 361 printk(KERN_INFO "Memory: %luMB total\n", num_physpages >> (20 - PAGE_SHIFT)); 362 printk(KERN_NOTICE "Memory: %luKB available (%dK code, " 363 "%dK data, %dK init)\n", 364 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10), 365 codepages >> 10, datapages >> 10, initpages >> 10); 366 367 /* 368 * Turn on overcommit on tiny machines 369 */ 370 if (PAGE_SIZE >= 16384 && num_physpages <= 128) { 371 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS; 372 printk("Turning on overcommit\n"); 373 } 374} 375 376void free_initmem(void){ 377#ifndef CONFIG_XIP_KERNEL 378 free_area((unsigned long)(&__init_begin), 379 (unsigned long)(&__init_end), 380 "init"); 381#endif 382} 383 384#ifdef CONFIG_BLK_DEV_INITRD 385 386static int keep_initrd; 387 388void free_initrd_mem(unsigned long start, unsigned long end) 389{ 390#ifdef CONFIG_XIP_KERNEL 391 /* Only bin initrd if it is in RAM... */ 392 if(!keep_initrd && start < 0x03000000) 393#else 394 if (!keep_initrd) 395#endif 396 free_area(start, end, "initrd"); 397} 398 399static int __init keepinitrd_setup(char *__unused) 400{ 401 keep_initrd = 1; 402 return 1; 403} 404 405__setup("keepinitrd", keepinitrd_setup); 406#endif