3 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
5 * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
6 * and Cort Dougan (PReP) (cort@cs.nmt.edu)
7 * Copyright (C) 1996 Paul Mackerras
8 * Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk).
9 * PPC44x/36-bit changes by Matt Porter (mporter@mvista.com)
11 * Derived from "arch/i386/mm/init.c"
12 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/sched.h>
24 #include <linux/kernel.h>
25 #include <linux/errno.h>
26 #include <linux/string.h>
27 #include <linux/types.h>
29 #include <linux/stddef.h>
30 #include <linux/init.h>
31 #include <linux/bootmem.h>
32 #include <linux/highmem.h>
33 #include <linux/initrd.h>
34 #include <linux/pagemap.h>
36 #include <asm/pgalloc.h>
39 #include <asm/mmu_context.h>
40 #include <asm/pgtable.h>
43 #include <asm/machdep.h>
44 #include <asm/btext.h>
46 #include <asm/bootinfo.h>
49 #include "mem_pieces.h"
52 #if defined(CONFIG_KERNEL_START_BOOL) || defined(CONFIG_LOWMEM_SIZE_BOOL)
53 /* The ammount of lowmem must be within 0xF0000000 - KERNELBASE. */
54 #if (CONFIG_LOWMEM_SIZE > (0xF0000000 - KERNELBASE))
55 #error "You must adjust CONFIG_LOWMEM_SIZE or CONFIG_START_KERNEL"
58 #define MAX_LOW_MEM CONFIG_LOWMEM_SIZE
60 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
62 unsigned long total_memory;
63 unsigned long total_lowmem;
65 unsigned long ppc_memstart;
66 unsigned long ppc_memoffset = PAGE_OFFSET;
69 int init_bootmem_done;
71 #ifdef CONFIG_PPC_PMAC
72 unsigned long agp_special_page;
76 extern char etext[], _stext[];
77 extern char __init_begin, __init_end;
83 EXPORT_SYMBOL(kmap_prot);
84 EXPORT_SYMBOL(kmap_pte);
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;
95 struct device_node *memory_node;
98 * this tells the system to map all of ram with the segregs
99 * (i.e. page tables) instead of the bats.
102 int __map_without_bats;
103 int __map_without_ltlbs;
105 /* max amount of RAM to use */
106 unsigned long __max_memory;
107 /* max amount of low RAM to map in */
108 unsigned long __max_low_memory = MAX_LOW_MEM;
111 * Read in a property describing some pieces of memory.
113 static int __init get_mem_prop(char *name, struct mem_pieces *mp)
115 struct reg_property *rp;
118 int nac = prom_n_addr_cells(memory_node);
119 int nsc = prom_n_size_cells(memory_node);
121 ip = (unsigned int *) get_property(memory_node, name, &s);
123 printk(KERN_ERR "error: couldn't get %s property on /memory\n",
127 s /= (nsc + nac) * 4;
129 for (i = 0; i < s; ++i, ip += nac+nsc) {
130 if (nac >= 2 && ip[nac-2] != 0)
132 rp->address = ip[nac-1];
133 if (nsc >= 2 && ip[nac+nsc-2] != 0)
136 rp->size = ip[nac+nsc-1];
139 mp->n_regions = rp - mp->regions;
141 /* Make sure the pieces are sorted. */
143 mem_pieces_coalesce(mp);
148 * Collect information about physical RAM and which pieces are
149 * already in use from the device tree.
151 unsigned long __init find_end_of_memory(void)
153 unsigned long a, total;
154 struct mem_pieces phys_mem;
157 * Find out where physical memory is, and check that it
158 * starts at 0 and is contiguous. It seems that RAM is
159 * always physically contiguous on Power Macintoshes.
161 * Supporting discontiguous physical memory isn't hard,
162 * it just makes the virtual <-> physical mapping functions
163 * more complicated (or else you end up wasting space
166 memory_node = find_devices("memory");
167 if (memory_node == NULL || !get_mem_prop("reg", &phys_mem)
168 || phys_mem.n_regions == 0)
170 a = phys_mem.regions[0].address;
172 panic("RAM doesn't start at physical address 0");
173 total = phys_mem.regions[0].size;
175 if (phys_mem.n_regions > 1) {
176 printk("RAM starting at 0x%x is not contiguous\n",
177 phys_mem.regions[1].address);
178 printk("Using RAM from 0 to 0x%lx\n", total-1);
185 * Check for command-line options that affect what MMU_init will do.
189 /* Check for nobats option (used in mapin_ram). */
190 if (strstr(cmd_line, "nobats")) {
191 __map_without_bats = 1;
194 if (strstr(cmd_line, "noltlbs")) {
195 __map_without_ltlbs = 1;
198 /* Look for mem= option on command line */
199 if (strstr(cmd_line, "mem=")) {
201 unsigned long maxmem = 0;
203 for (q = cmd_line; (p = strstr(q, "mem=")) != 0; ) {
205 if (p > cmd_line && p[-1] != ' ')
207 maxmem = simple_strtoul(q, &q, 0);
208 if (*q == 'k' || *q == 'K') {
211 } else if (*q == 'm' || *q == 'M') {
216 __max_memory = maxmem;
221 * MMU_init sets up the basic memory mappings for the kernel,
222 * including both RAM and possibly some I/O regions,
223 * and sets up the page tables and the MMU hardware ready to go.
225 void __init MMU_init(void)
228 ppc_md.progress("MMU:enter", 0x111);
230 /* parse args from command line */
234 * Figure out how much memory we have, how much
235 * is lowmem, and how much is highmem. If we were
236 * passed the total memory size from the bootloader,
240 total_memory = boot_mem_size;
242 total_memory = ppc_md.find_end_of_memory();
244 if (__max_memory && total_memory > __max_memory)
245 total_memory = __max_memory;
246 total_lowmem = total_memory;
247 #ifdef CONFIG_FSL_BOOKE
248 /* Freescale Book-E parts expect lowmem to be mapped by fixed TLB
249 * entries, so we need to adjust lowmem to match the amount we can map
250 * in the fixed entries */
251 adjust_total_lowmem();
252 #endif /* CONFIG_FSL_BOOKE */
253 if (total_lowmem > __max_low_memory) {
254 total_lowmem = __max_low_memory;
255 #ifndef CONFIG_HIGHMEM
256 total_memory = total_lowmem;
257 #endif /* CONFIG_HIGHMEM */
259 set_phys_avail(total_lowmem);
261 /* Initialize the MMU hardware */
263 ppc_md.progress("MMU:hw init", 0x300);
266 /* Map in all of RAM starting at KERNELBASE */
268 ppc_md.progress("MMU:mapin", 0x301);
271 #ifdef CONFIG_HIGHMEM
272 ioremap_base = PKMAP_BASE;
274 ioremap_base = 0xfe000000UL; /* for now, could be 0xfffff000 */
275 #endif /* CONFIG_HIGHMEM */
276 ioremap_bot = ioremap_base;
278 /* Map in I/O resources */
280 ppc_md.progress("MMU:setio", 0x302);
281 if (ppc_md.setup_io_mappings)
282 ppc_md.setup_io_mappings();
284 /* Initialize the context management stuff */
288 ppc_md.progress("MMU:exit", 0x211);
290 #ifdef CONFIG_BOOTX_TEXT
291 /* By default, we are no longer mapped */
292 boot_text_mapped = 0;
293 /* Must be done last, or ppc_md.progress will die. */
298 /* This is only called until mem_init is done. */
299 void __init *early_get_page(void)
303 if (init_bootmem_done) {
304 p = alloc_bootmem_pages(PAGE_SIZE);
306 p = mem_pieces_find(PAGE_SIZE, PAGE_SIZE);
311 /* Free up now-unused memory */
312 static void free_sec(unsigned long start, unsigned long end, const char *name)
314 unsigned long cnt = 0;
316 while (start < end) {
317 ClearPageReserved(virt_to_page(start));
318 set_page_count(virt_to_page(start), 1);
324 printk(" %ldk %s", cnt << (PAGE_SHIFT - 10), name);
325 totalram_pages += cnt;
329 void free_initmem(void)
331 #define FREESEC(TYPE) \
332 free_sec((unsigned long)(&__ ## TYPE ## _begin), \
333 (unsigned long)(&__ ## TYPE ## _end), \
336 printk ("Freeing unused kernel memory:");
339 ppc_md.progress = NULL;
343 #ifdef CONFIG_BLK_DEV_INITRD
344 void free_initrd_mem(unsigned long start, unsigned long end)
347 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
348 for (; start < end; start += PAGE_SIZE) {
349 ClearPageReserved(virt_to_page(start));
350 set_page_count(virt_to_page(start), 1);
358 * Initialize the bootmem system and give it all the memory we
361 void __init do_init_bootmem(void)
363 unsigned long start, size;
367 * Find an area to use for the bootmem bitmap.
368 * We look for the first area which is at least
369 * 128kB in length (128kB is enough for a bitmap
370 * for 4GB of memory, using 4kB pages), plus 1 page
371 * (in case the address isn't page-aligned).
375 for (i = 0; i < phys_avail.n_regions; ++i) {
376 unsigned long a = phys_avail.regions[i].address;
377 unsigned long s = phys_avail.regions[i].size;
382 if (s >= 33 * PAGE_SIZE)
385 start = PAGE_ALIGN(start);
387 min_low_pfn = start >> PAGE_SHIFT;
388 max_low_pfn = (PPC_MEMSTART + total_lowmem) >> PAGE_SHIFT;
389 max_pfn = (PPC_MEMSTART + total_memory) >> PAGE_SHIFT;
390 boot_mapsize = init_bootmem_node(&contig_page_data, min_low_pfn,
391 PPC_MEMSTART >> PAGE_SHIFT,
394 /* remove the bootmem bitmap from the available memory */
395 mem_pieces_remove(&phys_avail, start, boot_mapsize, 1);
397 /* add everything in phys_avail into the bootmem map */
398 for (i = 0; i < phys_avail.n_regions; ++i)
399 free_bootmem(phys_avail.regions[i].address,
400 phys_avail.regions[i].size);
402 init_bootmem_done = 1;
406 * paging_init() sets up the page tables - in fact we've already done this.
408 void __init paging_init(void)
410 unsigned long zones_size[MAX_NR_ZONES], i;
412 #ifdef CONFIG_HIGHMEM
413 map_page(PKMAP_BASE, 0, 0); /* XXX gross */
414 pkmap_page_table = pte_offset_kernel(pmd_offset(pgd_offset_k
415 (PKMAP_BASE), PKMAP_BASE), PKMAP_BASE);
416 map_page(KMAP_FIX_BEGIN, 0, 0); /* XXX gross */
417 kmap_pte = pte_offset_kernel(pmd_offset(pgd_offset_k
418 (KMAP_FIX_BEGIN), KMAP_FIX_BEGIN), KMAP_FIX_BEGIN);
419 kmap_prot = PAGE_KERNEL;
420 #endif /* CONFIG_HIGHMEM */
423 * All pages are DMA-able so we put them all in the DMA zone.
425 zones_size[ZONE_DMA] = total_lowmem >> PAGE_SHIFT;
426 for (i = 1; i < MAX_NR_ZONES; i++)
429 #ifdef CONFIG_HIGHMEM
430 zones_size[ZONE_HIGHMEM] = (total_memory - total_lowmem) >> PAGE_SHIFT;
431 #endif /* CONFIG_HIGHMEM */
433 free_area_init(zones_size);
436 void __init mem_init(void)
442 #ifdef CONFIG_HIGHMEM
443 unsigned long highmem_mapnr;
445 highmem_mapnr = total_lowmem >> PAGE_SHIFT;
446 #endif /* CONFIG_HIGHMEM */
447 max_mapnr = total_memory >> PAGE_SHIFT;
449 high_memory = (void *) __va(PPC_MEMSTART + total_lowmem);
450 num_physpages = max_mapnr; /* RAM is assumed contiguous */
452 totalram_pages += free_all_bootmem();
454 #ifdef CONFIG_BLK_DEV_INITRD
455 /* if we are booted from BootX with an initial ramdisk,
456 make sure the ramdisk pages aren't reserved. */
458 for (addr = initrd_start; addr < initrd_end; addr += PAGE_SIZE)
459 ClearPageReserved(virt_to_page(addr));
461 #endif /* CONFIG_BLK_DEV_INITRD */
464 /* mark the RTAS pages as reserved */
466 for (addr = (ulong)__va(rtas_data);
467 addr < PAGE_ALIGN((ulong)__va(rtas_data)+rtas_size) ;
469 SetPageReserved(virt_to_page(addr));
471 #ifdef CONFIG_PPC_PMAC
472 if (agp_special_page)
473 SetPageReserved(virt_to_page(agp_special_page));
475 for (addr = PAGE_OFFSET; addr < (unsigned long)high_memory;
477 if (!PageReserved(virt_to_page(addr)))
479 if (addr < (ulong) etext)
481 else if (addr >= (unsigned long)&__init_begin
482 && addr < (unsigned long)&__init_end)
484 else if (addr < (ulong) klimit)
488 #ifdef CONFIG_HIGHMEM
492 for (pfn = highmem_mapnr; pfn < max_mapnr; ++pfn) {
493 struct page *page = mem_map + pfn;
495 ClearPageReserved(page);
496 set_page_count(page, 1);
500 totalram_pages += totalhigh_pages;
502 #endif /* CONFIG_HIGHMEM */
504 printk("Memory: %luk available (%dk kernel code, %dk data, %dk init, %ldk highmem)\n",
505 (unsigned long)nr_free_pages()<< (PAGE_SHIFT-10),
506 codepages<< (PAGE_SHIFT-10), datapages<< (PAGE_SHIFT-10),
507 initpages<< (PAGE_SHIFT-10),
508 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)));
510 #ifdef CONFIG_PPC_PMAC
511 if (agp_special_page)
512 printk(KERN_INFO "AGP special page: 0x%08lx\n", agp_special_page);
519 * Set phys_avail to the amount of physical memory,
520 * less the kernel text/data/bss.
523 set_phys_avail(unsigned long total_memory)
525 unsigned long kstart, ksize;
528 * Initially, available physical memory is equivalent to all
532 phys_avail.regions[0].address = PPC_MEMSTART;
533 phys_avail.regions[0].size = total_memory;
534 phys_avail.n_regions = 1;
537 * Map out the kernel text/data/bss from the available physical
541 kstart = __pa(_stext); /* should be 0 */
542 ksize = PAGE_ALIGN(klimit - _stext);
544 mem_pieces_remove(&phys_avail, kstart, ksize, 0);
545 mem_pieces_remove(&phys_avail, 0, 0x4000, 0);
547 #if defined(CONFIG_BLK_DEV_INITRD)
548 /* Remove the init RAM disk from the available memory. */
550 mem_pieces_remove(&phys_avail, __pa(initrd_start),
551 initrd_end - initrd_start, 1);
553 #endif /* CONFIG_BLK_DEV_INITRD */
555 /* remove the RTAS pages from the available memory */
557 mem_pieces_remove(&phys_avail, rtas_data, rtas_size, 1);
559 #ifdef CONFIG_PPC_PMAC
560 /* Because of some uninorth weirdness, we need a page of
561 * memory as high as possible (it must be outside of the
562 * bus address seen as the AGP aperture). It will be used
563 * by the r128 DRM driver
565 * FIXME: We need to make sure that page doesn't overlap any of the\
566 * above. This could be done by improving mem_pieces_find to be able
567 * to do a backward search from the end of the list.
569 if (_machine == _MACH_Pmac && find_devices("uni-north-agp")) {
570 agp_special_page = (total_memory - PAGE_SIZE);
571 mem_pieces_remove(&phys_avail, agp_special_page, PAGE_SIZE, 0);
572 agp_special_page = (unsigned long)__va(agp_special_page);
574 #endif /* CONFIG_PPC_PMAC */
577 /* Mark some memory as reserved by removing it from phys_avail. */
578 void __init reserve_phys_mem(unsigned long start, unsigned long size)
580 mem_pieces_remove(&phys_avail, start, size, 1);