2 * BK Id: SCCS/s.init.c 1.78 08/14/02 22:02:54 mporter
6 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
8 * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
9 * and Cort Dougan (PReP) (cort@cs.nmt.edu)
10 * Copyright (C) 1996 Paul Mackerras
11 * Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk).
12 * PPC440/36-bit changes by Matt Porter (mporter@mvista.com)
14 * Derived from "arch/i386/mm/init.c"
15 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version
20 * 2 of the License, or (at your option) any later version.
24 #include <linux/config.h>
25 #include <linux/sched.h>
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/string.h>
29 #include <linux/types.h>
31 #include <linux/stddef.h>
32 #include <linux/init.h>
33 #include <linux/bootmem.h>
34 #include <linux/highmem.h>
35 #ifdef CONFIG_BLK_DEV_INITRD
36 #include <linux/blk.h> /* for initrd_* */
39 #include <asm/pgalloc.h>
42 #include <asm/mmu_context.h>
43 #include <asm/pgtable.h>
46 #include <asm/machdep.h>
47 #include <asm/btext.h>
50 #include "mem_pieces.h"
53 #ifdef CONFIG_PPC_ISERIES
54 extern void create_virtual_bus_tce_table(void);
57 mmu_gather_t mmu_gathers[NR_CPUS];
59 unsigned long total_memory;
60 unsigned long total_lowmem;
62 unsigned long ppc_memstart;
63 unsigned long ppc_memoffset = PAGE_OFFSET;
66 int init_bootmem_done;
68 unsigned long totalram_pages;
69 unsigned long totalhigh_pages;
71 unsigned long agp_special_page;
75 extern char etext[], _stext[];
76 extern char __init_begin, __init_end;
77 extern char __prep_begin, __prep_end;
78 extern char __chrp_begin, __chrp_end;
79 extern char __pmac_begin, __pmac_end;
80 extern char __openfirmware_begin, __openfirmware_end;
88 void set_phys_avail(unsigned long total_ram);
90 /* XXX should be in current.h -- paulus */
91 extern struct task_struct *current_set[NR_CPUS];
94 struct mem_pieces phys_avail;
97 extern unsigned long sysmap_size;
99 * this tells the system to map all of ram with the segregs
100 * (i.e. page tables) instead of the bats.
103 int __map_without_bats;
105 /* max amount of RAM to use */
106 unsigned long __max_memory;
108 int do_check_pgt_cache(int low, int high)
111 if (pgtable_cache_size > high) {
114 free_pgd_slow(get_pgd_fast());
118 pte_free_slow(pte_alloc_one_fast(NULL, 0));
121 } while (pgtable_cache_size > low);
128 int i,free = 0,total = 0,reserved = 0;
129 int shared = 0, cached = 0;
130 struct task_struct *p;
133 printk("Mem-info:\n");
135 printk("Free swap: %6dkB\n",nr_swap_pages<<(PAGE_SHIFT-10));
139 if (PageHighMem(mem_map+i))
141 if (PageReserved(mem_map+i))
143 else if (PageSwapCache(mem_map+i))
145 else if (!page_count(mem_map+i))
148 shared += atomic_read(&mem_map[i].count) - 1;
150 printk("%d pages of RAM\n",total);
151 printk("%d pages of HIGHMEM\n", highmem);
152 printk("%d free pages\n",free);
153 printk("%d reserved pages\n",reserved);
154 printk("%d pages shared\n",shared);
155 printk("%d pages swap cached\n",cached);
156 printk("%d pages in page table cache\n",(int)pgtable_cache_size);
158 printk("%-8s %3s %8s %8s %8s %9s %8s", "Process", "Pid",
159 "Ctx", "Ctx<<4", "Last Sys", "pc", "task");
161 printk(" %3s", "CPU");
162 #endif /* CONFIG_SMP */
166 printk("%-8.8s %3d %8ld %8ld %8ld %c%08lx %08lx ",
168 (p->mm)?p->mm->context:0,
169 (p->mm)?(p->mm->context<<4):0,
170 p->thread.last_syscall,
171 (p->thread.regs)?user_mode(p->thread.regs) ? 'u' : 'k' : '?',
172 (p->thread.regs)?p->thread.regs->nip:0,
177 printk("%3d ", p->processor);
178 if ( (p->processor != NO_PROC_ID) &&
179 (p == current_set[p->processor]) )
191 if ( p == last_task_used_math )
197 #endif /* CONFIG_SMP */
203 void si_meminfo(struct sysinfo *val)
205 val->totalram = totalram_pages;
207 val->freeram = nr_free_pages();
208 val->bufferram = atomic_read(&buffermem_pages);
209 val->totalhigh = totalhigh_pages;
210 val->freehigh = nr_free_highpages();
211 val->mem_unit = PAGE_SIZE;
214 /* Free up now-unused memory */
215 static void free_sec(unsigned long start, unsigned long end, const char *name)
217 unsigned long cnt = 0;
219 while (start < end) {
220 ClearPageReserved(virt_to_page(start));
221 set_page_count(virt_to_page(start), 1);
227 printk(" %ldk %s", cnt << (PAGE_SHIFT - 10), name);
228 totalram_pages += cnt;
232 void free_initmem(void)
234 #define FREESEC(TYPE) \
235 free_sec((unsigned long)(&__ ## TYPE ## _begin), \
236 (unsigned long)(&__ ## TYPE ## _end), \
239 printk (KERN_INFO "Freeing unused kernel memory:");
241 if (_machine != _MACH_Pmac)
243 if (_machine != _MACH_chrp)
245 if (_machine != _MACH_prep)
248 FREESEC(openfirmware);
253 #ifdef CONFIG_BLK_DEV_INITRD
254 void free_initrd_mem(unsigned long start, unsigned long end)
256 printk (KERN_INFO "Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
258 for (; start < end; start += PAGE_SIZE) {
259 ClearPageReserved(virt_to_page(start));
260 set_page_count(virt_to_page(start), 1);
268 * Check for command-line options that affect what MMU_init will do.
272 /* Check for nobats option (used in mapin_ram). */
273 if (strstr(cmd_line, "nobats")) {
274 __map_without_bats = 1;
277 /* Look for mem= option on command line */
278 if (strstr(cmd_line, "mem=")) {
280 unsigned long maxmem = 0;
282 for (q = cmd_line; (p = strstr(q, "mem=")) != 0; ) {
284 if (p > cmd_line && p[-1] != ' ')
286 maxmem = simple_strtoul(q, &q, 0);
287 if (*q == 'k' || *q == 'K') {
290 } else if (*q == 'm' || *q == 'M') {
295 __max_memory = maxmem;
300 * MMU_init sets up the basic memory mappings for the kernel,
301 * including both RAM and possibly some I/O regions,
302 * and sets up the page tables and the MMU hardware ready to go.
304 void __init MMU_init(void)
307 ppc_md.progress("MMU:enter", 0x111);
309 /* parse args from command line */
313 * Figure out how much memory we have, how much
314 * is lowmem, and how much is highmem.
316 total_memory = ppc_md.find_end_of_memory();
318 if (__max_memory && total_memory > __max_memory)
319 total_memory = __max_memory;
320 total_lowmem = total_memory;
321 adjust_total_lowmem();
322 set_phys_avail(total_lowmem);
324 /* Initialize the MMU hardware */
326 ppc_md.progress("MMU:hw init", 0x300);
329 /* Map in all of RAM starting at KERNELBASE */
331 ppc_md.progress("MMU:mapin", 0x301);
334 #ifdef CONFIG_HIGHMEM
335 ioremap_base = PKMAP_BASE;
337 ioremap_base = 0xfe000000UL; /* for now, could be 0xfffff000 */
338 #endif /* CONFIG_HIGHMEM */
339 ioremap_bot = ioremap_base;
341 /* Map in I/O resources */
343 ppc_md.progress("MMU:setio", 0x302);
344 if (ppc_md.setup_io_mappings)
345 ppc_md.setup_io_mappings();
347 /* Initialize the context management stuff */
351 ppc_md.progress("MMU:exit", 0x211);
353 #ifdef CONFIG_BOOTX_TEXT
354 /* By default, we are no longer mapped */
355 boot_text_mapped = 0;
356 /* Must be done last, or ppc_md.progress will die. */
361 /* This is only called until mem_init is done. */
362 void __init *early_get_page(void)
366 if (init_bootmem_done) {
367 p = alloc_bootmem_pages(PAGE_SIZE);
369 p = mem_pieces_find(PAGE_SIZE, PAGE_SIZE);
375 * Initialize the bootmem system and give it all the memory we
378 void __init do_init_bootmem(void)
380 unsigned long start, size;
384 * Find an area to use for the bootmem bitmap.
385 * We look for the first area which is at least
386 * 128kB in length (128kB is enough for a bitmap
387 * for 4GB of memory, using 4kB pages), plus 1 page
388 * (in case the address isn't page-aligned).
392 for (i = 0; i < phys_avail.n_regions; ++i) {
393 unsigned long a = phys_avail.regions[i].address;
394 unsigned long s = phys_avail.regions[i].size;
399 if (s >= 33 * PAGE_SIZE)
402 start = PAGE_ALIGN(start);
404 min_low_pfn = start >> PAGE_SHIFT;
405 max_low_pfn = (PPC_MEMSTART + total_lowmem) >> PAGE_SHIFT;
406 boot_mapsize = init_bootmem_node(&contig_page_data, min_low_pfn,
407 PPC_MEMSTART >> PAGE_SHIFT,
410 /* remove the bootmem bitmap from the available memory */
411 mem_pieces_remove(&phys_avail, start, boot_mapsize, 1);
413 /* add everything in phys_avail into the bootmem map */
414 for (i = 0; i < phys_avail.n_regions; ++i)
415 free_bootmem(phys_avail.regions[i].address,
416 phys_avail.regions[i].size);
420 * Reserve space in the pinned TLB area (ZONE_DMA).
421 * Assume all 44x systems have >16MB RAM and any
422 * initrd is located outside of our pinned TLB area.
424 reserve_bootmem(0x00800000, 0x00800000);
427 init_bootmem_done = 1;
431 * paging_init() sets up the page tables - in fact we've already done this.
433 void __init paging_init(void)
435 unsigned long zones_size[MAX_NR_ZONES], i;
437 #ifdef CONFIG_HIGHMEM
438 map_page(PKMAP_BASE, 0, 0); /* XXX gross */
439 pkmap_page_table = pte_offset(pmd_offset(pgd_offset_k(PKMAP_BASE), PKMAP_BASE), PKMAP_BASE);
440 map_page(KMAP_FIX_BEGIN, 0, 0); /* XXX gross */
441 kmap_pte = pte_offset(pmd_offset(pgd_offset_k(KMAP_FIX_BEGIN), KMAP_FIX_BEGIN), KMAP_FIX_BEGIN);
442 kmap_prot = PAGE_KERNEL;
443 #endif /* CONFIG_HIGHMEM */
446 * All pages are DMA-able so we put them all in the DMA zone.
449 zones_size[ZONE_DMA] = total_lowmem >> PAGE_SHIFT;
451 for (i = 1; i < MAX_NR_ZONES; i++)
455 * On 440, we highjack the DMA zone to provide a zone
456 * which limits allocations to our pinned TLB region.
458 zones_size[ZONE_DMA] = PPC440_PIN_SIZE >> PAGE_SHIFT;
459 zones_size[ZONE_NORMAL] = (total_lowmem - PPC440_PIN_SIZE) >> PAGE_SHIFT;
461 for (i = 2; i < MAX_NR_ZONES; i++)
466 #ifdef CONFIG_HIGHMEM
467 zones_size[ZONE_HIGHMEM] = (total_memory - total_lowmem) >> PAGE_SHIFT;
468 #endif /* CONFIG_HIGHMEM */
470 free_area_init(zones_size);
473 void __init mem_init(void)
479 #ifdef CONFIG_HIGHMEM
480 unsigned long highmem_mapnr;
482 highmem_mapnr = total_lowmem >> PAGE_SHIFT;
483 highmem_start_page = mem_map + highmem_mapnr;
484 #endif /* CONFIG_HIGHMEM */
485 max_mapnr = total_memory >> PAGE_SHIFT;
487 high_memory = (void *) __va(PPC_MEMSTART + total_lowmem);
488 num_physpages = max_mapnr; /* RAM is assumed contiguous */
491 /* Free reserved space in the pinned TLB area (ZONE_DMA) */
492 if (total_lowmem > PPC440_PIN_SIZE)
493 free_bootmem(0x00800000, 0x00800000);
496 totalram_pages += free_all_bootmem();
498 #ifdef CONFIG_BLK_DEV_INITRD
499 /* if we are booted from BootX with an initial ramdisk,
500 make sure the ramdisk pages aren't reserved. */
502 for (addr = initrd_start; addr < initrd_end; addr += PAGE_SIZE)
503 ClearPageReserved(virt_to_page(addr));
505 #endif /* CONFIG_BLK_DEV_INITRD */
507 #if defined(CONFIG_ALL_PPC)
508 /* mark the RTAS pages as reserved */
510 for (addr = (ulong)__va(rtas_data);
511 addr < PAGE_ALIGN((ulong)__va(rtas_data)+rtas_size) ;
513 SetPageReserved(virt_to_page(addr));
514 if (agp_special_page)
515 SetPageReserved(virt_to_page(agp_special_page));
516 #endif /* defined(CONFIG_ALL_PPC) */
518 for (addr = (unsigned long)sysmap;
519 addr < PAGE_ALIGN((unsigned long)sysmap+sysmap_size) ;
521 SetPageReserved(virt_to_page(addr));
523 for (addr = PAGE_OFFSET; addr < (unsigned long)high_memory;
525 if (!PageReserved(virt_to_page(addr)))
527 if (addr < (ulong) etext)
529 else if (addr >= (unsigned long)&__init_begin
530 && addr < (unsigned long)&__init_end)
532 else if (addr < (ulong) klimit)
536 #ifdef CONFIG_HIGHMEM
540 for (pfn = highmem_mapnr; pfn < max_mapnr; ++pfn) {
541 struct page *page = mem_map + pfn;
543 ClearPageReserved(page);
544 set_bit(PG_highmem, &page->flags);
545 atomic_set(&page->count, 1);
549 totalram_pages += totalhigh_pages;
551 #endif /* CONFIG_HIGHMEM */
553 printk(KERN_INFO "Memory: %luk available (%dk kernel code, %dk data, %dk init, %ldk highmem)\n",
554 (unsigned long)nr_free_pages()<< (PAGE_SHIFT-10),
555 codepages<< (PAGE_SHIFT-10), datapages<< (PAGE_SHIFT-10),
556 initpages<< (PAGE_SHIFT-10),
557 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)));
559 printk("System.map loaded at 0x%08x for debugger, size: %ld bytes\n",
560 (unsigned int)sysmap, sysmap_size);
561 #if defined(CONFIG_ALL_PPC)
562 if (agp_special_page)
563 printk(KERN_INFO "AGP special page: 0x%08lx\n", agp_special_page);
564 #endif /* defined(CONFIG_ALL_PPC) */
565 #ifdef CONFIG_PPC_ISERIES
567 create_virtual_bus_tce_table();
569 #endif /* CONFIG_PPC_ISERIES */
574 * Set phys_avail to the amount of physical memory,
575 * less the kernel text/data/bss.
578 set_phys_avail(unsigned long total_memory)
580 unsigned long kstart, ksize;
583 * Initially, available physical memory is equivalent to all
587 phys_avail.regions[0].address = PPC_MEMSTART;
588 phys_avail.regions[0].size = total_memory;
589 phys_avail.n_regions = 1;
591 printk(KERN_INFO "Total memory is %d.\n", total_memory);
593 * Map out the kernel text/data/bss from the available physical
597 kstart = __pa(_stext); /* should be 0 */
598 ksize = PAGE_ALIGN(klimit - _stext);
600 mem_pieces_remove(&phys_avail, kstart, ksize, 0);
601 mem_pieces_remove(&phys_avail, 0, 0x4000, 0);
603 #if defined(CONFIG_BLK_DEV_INITRD)
604 /* Remove the init RAM disk from the available memory. */
606 mem_pieces_remove(&phys_avail, __pa(initrd_start),
607 initrd_end - initrd_start, 1);
609 #endif /* CONFIG_BLK_DEV_INITRD */
610 #ifdef CONFIG_ALL_PPC
611 /* remove the RTAS pages from the available memory */
613 mem_pieces_remove(&phys_avail, rtas_data, rtas_size, 1);
614 /* Because of some uninorth weirdness, we need a page of
615 * memory as high as possible (it must be outside of the
616 * bus address seen as the AGP aperture). It will be used
617 * by the r128 DRM driver
619 * FIXME: We need to make sure that page doesn't overlap any of the\
620 * above. This could be done by improving mem_pieces_find to be able
621 * to do a backward search from the end of the list.
623 if (_machine == _MACH_Pmac && find_devices("uni-north-agp")) {
624 agp_special_page = (total_memory - PAGE_SIZE);
625 mem_pieces_remove(&phys_avail, agp_special_page, PAGE_SIZE, 0);
626 agp_special_page = (unsigned long)__va(agp_special_page);
628 #endif /* CONFIG_ALL_PPC */
629 /* remove the sysmap pages from the available memory */
631 mem_pieces_remove(&phys_avail, __pa(sysmap), sysmap_size, 1);
634 /* Mark some memory as reserved by removing it from phys_avail. */
635 void __init reserve_phys_mem(unsigned long start, unsigned long size)
637 mem_pieces_remove(&phys_avail, start, size, 1);
641 * This is called when a page has been modified by the kernel.
642 * It just marks the page as not i-cache clean. We do the i-cache
643 * flush later when the page is given to a user process, if necessary.
645 void flush_dcache_page(struct page *page)
647 clear_bit(PG_arch_1, &page->flags);
650 void flush_icache_page(struct vm_area_struct *vma, struct page *page)
652 if (page->mapping && !PageReserved(page)
653 && !test_bit(PG_arch_1, &page->flags)) {
654 __flush_dcache_icache(kmap(page));
656 set_bit(PG_arch_1, &page->flags);
660 void clear_user_page(void *page, unsigned long vaddr)
663 __flush_dcache_icache(page);
666 void copy_user_page(void *vto, void *vfrom, unsigned long vaddr)
668 copy_page(vto, vfrom);
669 __flush_dcache_icache(vto);
672 void flush_icache_user_range(struct vm_area_struct *vma, struct page *page,
673 unsigned long addr, int len)
677 maddr = (unsigned long) kmap(page) + (addr & ~PAGE_MASK);
678 flush_icache_range(maddr, maddr + len);