blob: 5e9ef23b467193a177f8212413eb2add64f56460 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PowerPC version
3 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
4 *
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)
10 *
11 * Derived from "arch/i386/mm/init.c"
12 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
13 *
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.
18 *
19 */
20
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>
28#include <linux/mm.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>
35
36#include <asm/pgalloc.h>
37#include <asm/prom.h>
38#include <asm/io.h>
39#include <asm/mmu_context.h>
40#include <asm/pgtable.h>
41#include <asm/mmu.h>
42#include <asm/smp.h>
43#include <asm/machdep.h>
44#include <asm/btext.h>
45#include <asm/tlb.h>
46#include <asm/bootinfo.h>
47
48#include "mem_pieces.h"
49#include "mmu_decl.h"
50
51#if defined(CONFIG_KERNEL_START_BOOL) || defined(CONFIG_LOWMEM_SIZE_BOOL)
52/* The ammount of lowmem must be within 0xF0000000 - KERNELBASE. */
53#if (CONFIG_LOWMEM_SIZE > (0xF0000000 - KERNELBASE))
54#error "You must adjust CONFIG_LOWMEM_SIZE or CONFIG_START_KERNEL"
55#endif
56#endif
57#define MAX_LOW_MEM CONFIG_LOWMEM_SIZE
58
59DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
60
61unsigned long total_memory;
62unsigned long total_lowmem;
63
64unsigned long ppc_memstart;
65unsigned long ppc_memoffset = PAGE_OFFSET;
66
67int mem_init_done;
68int init_bootmem_done;
69int boot_mapsize;
70#ifdef CONFIG_PPC_PMAC
71unsigned long agp_special_page;
72#endif
73
74extern char _end[];
75extern char etext[], _stext[];
76extern char __init_begin, __init_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78#ifdef CONFIG_HIGHMEM
79pte_t *kmap_pte;
80pgprot_t kmap_prot;
81
82EXPORT_SYMBOL(kmap_prot);
83EXPORT_SYMBOL(kmap_pte);
84#endif
85
86void MMU_init(void);
87void set_phys_avail(unsigned long total_ram);
88
89/* XXX should be in current.h -- paulus */
90extern struct task_struct *current_set[NR_CPUS];
91
92char *klimit = _end;
93struct mem_pieces phys_avail;
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/*
96 * this tells the system to map all of ram with the segregs
97 * (i.e. page tables) instead of the bats.
98 * -- Cort
99 */
100int __map_without_bats;
101int __map_without_ltlbs;
102
103/* max amount of RAM to use */
104unsigned long __max_memory;
105/* max amount of low RAM to map in */
106unsigned long __max_low_memory = MAX_LOW_MEM;
107
108void show_mem(void)
109{
110 int i,free = 0,total = 0,reserved = 0;
111 int shared = 0, cached = 0;
112 int highmem = 0;
113
114 printk("Mem-info:\n");
115 show_free_areas();
116 printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
117 i = max_mapnr;
118 while (i-- > 0) {
119 total++;
120 if (PageHighMem(mem_map+i))
121 highmem++;
122 if (PageReserved(mem_map+i))
123 reserved++;
124 else if (PageSwapCache(mem_map+i))
125 cached++;
126 else if (!page_count(mem_map+i))
127 free++;
128 else
129 shared += page_count(mem_map+i) - 1;
130 }
131 printk("%d pages of RAM\n",total);
132 printk("%d pages of HIGHMEM\n", highmem);
133 printk("%d free pages\n",free);
134 printk("%d reserved pages\n",reserved);
135 printk("%d pages shared\n",shared);
136 printk("%d pages swap cached\n",cached);
137}
138
139/* Free up now-unused memory */
140static void free_sec(unsigned long start, unsigned long end, const char *name)
141{
142 unsigned long cnt = 0;
143
144 while (start < end) {
145 ClearPageReserved(virt_to_page(start));
146 set_page_count(virt_to_page(start), 1);
147 free_page(start);
148 cnt++;
149 start += PAGE_SIZE;
150 }
151 if (cnt) {
152 printk(" %ldk %s", cnt << (PAGE_SHIFT - 10), name);
153 totalram_pages += cnt;
154 }
155}
156
157void free_initmem(void)
158{
159#define FREESEC(TYPE) \
160 free_sec((unsigned long)(&__ ## TYPE ## _begin), \
161 (unsigned long)(&__ ## TYPE ## _end), \
162 #TYPE);
163
164 printk ("Freeing unused kernel memory:");
165 FREESEC(init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 printk("\n");
Paul Mackerras6c37a882005-05-20 16:57:22 +1000167 ppc_md.progress = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#undef FREESEC
169}
170
171#ifdef CONFIG_BLK_DEV_INITRD
172void free_initrd_mem(unsigned long start, unsigned long end)
173{
174 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
175
176 for (; start < end; start += PAGE_SIZE) {
177 ClearPageReserved(virt_to_page(start));
178 set_page_count(virt_to_page(start), 1);
179 free_page(start);
180 totalram_pages++;
181 }
182}
183#endif
184
185/*
186 * Check for command-line options that affect what MMU_init will do.
187 */
188void MMU_setup(void)
189{
190 /* Check for nobats option (used in mapin_ram). */
191 if (strstr(cmd_line, "nobats")) {
192 __map_without_bats = 1;
193 }
194
195 if (strstr(cmd_line, "noltlbs")) {
196 __map_without_ltlbs = 1;
197 }
198
199 /* Look for mem= option on command line */
200 if (strstr(cmd_line, "mem=")) {
201 char *p, *q;
202 unsigned long maxmem = 0;
203
204 for (q = cmd_line; (p = strstr(q, "mem=")) != 0; ) {
205 q = p + 4;
206 if (p > cmd_line && p[-1] != ' ')
207 continue;
208 maxmem = simple_strtoul(q, &q, 0);
209 if (*q == 'k' || *q == 'K') {
210 maxmem <<= 10;
211 ++q;
212 } else if (*q == 'm' || *q == 'M') {
213 maxmem <<= 20;
214 ++q;
215 }
216 }
217 __max_memory = maxmem;
218 }
219}
220
221/*
222 * MMU_init sets up the basic memory mappings for the kernel,
223 * including both RAM and possibly some I/O regions,
224 * and sets up the page tables and the MMU hardware ready to go.
225 */
226void __init MMU_init(void)
227{
228 if (ppc_md.progress)
229 ppc_md.progress("MMU:enter", 0x111);
230
231 /* parse args from command line */
232 MMU_setup();
233
234 /*
235 * Figure out how much memory we have, how much
236 * is lowmem, and how much is highmem. If we were
237 * passed the total memory size from the bootloader,
238 * just use it.
239 */
240 if (boot_mem_size)
241 total_memory = boot_mem_size;
242 else
243 total_memory = ppc_md.find_end_of_memory();
244
245 if (__max_memory && total_memory > __max_memory)
246 total_memory = __max_memory;
247 total_lowmem = total_memory;
248#ifdef CONFIG_FSL_BOOKE
249 /* Freescale Book-E parts expect lowmem to be mapped by fixed TLB
250 * entries, so we need to adjust lowmem to match the amount we can map
251 * in the fixed entries */
252 adjust_total_lowmem();
253#endif /* CONFIG_FSL_BOOKE */
254 if (total_lowmem > __max_low_memory) {
255 total_lowmem = __max_low_memory;
256#ifndef CONFIG_HIGHMEM
257 total_memory = total_lowmem;
258#endif /* CONFIG_HIGHMEM */
259 }
260 set_phys_avail(total_lowmem);
261
262 /* Initialize the MMU hardware */
263 if (ppc_md.progress)
264 ppc_md.progress("MMU:hw init", 0x300);
265 MMU_init_hw();
266
267 /* Map in all of RAM starting at KERNELBASE */
268 if (ppc_md.progress)
269 ppc_md.progress("MMU:mapin", 0x301);
270 mapin_ram();
271
272#ifdef CONFIG_HIGHMEM
273 ioremap_base = PKMAP_BASE;
274#else
275 ioremap_base = 0xfe000000UL; /* for now, could be 0xfffff000 */
276#endif /* CONFIG_HIGHMEM */
277 ioremap_bot = ioremap_base;
278
279 /* Map in I/O resources */
280 if (ppc_md.progress)
281 ppc_md.progress("MMU:setio", 0x302);
282 if (ppc_md.setup_io_mappings)
283 ppc_md.setup_io_mappings();
284
285 /* Initialize the context management stuff */
286 mmu_context_init();
287
288 if (ppc_md.progress)
289 ppc_md.progress("MMU:exit", 0x211);
290
291#ifdef CONFIG_BOOTX_TEXT
292 /* By default, we are no longer mapped */
293 boot_text_mapped = 0;
294 /* Must be done last, or ppc_md.progress will die. */
295 map_boot_text();
296#endif
297}
298
299/* This is only called until mem_init is done. */
300void __init *early_get_page(void)
301{
302 void *p;
303
304 if (init_bootmem_done) {
305 p = alloc_bootmem_pages(PAGE_SIZE);
306 } else {
307 p = mem_pieces_find(PAGE_SIZE, PAGE_SIZE);
308 }
309 return p;
310}
311
312/*
313 * Initialize the bootmem system and give it all the memory we
314 * have available.
315 */
316void __init do_init_bootmem(void)
317{
318 unsigned long start, size;
319 int i;
320
321 /*
322 * Find an area to use for the bootmem bitmap.
323 * We look for the first area which is at least
324 * 128kB in length (128kB is enough for a bitmap
325 * for 4GB of memory, using 4kB pages), plus 1 page
326 * (in case the address isn't page-aligned).
327 */
328 start = 0;
329 size = 0;
330 for (i = 0; i < phys_avail.n_regions; ++i) {
331 unsigned long a = phys_avail.regions[i].address;
332 unsigned long s = phys_avail.regions[i].size;
333 if (s <= size)
334 continue;
335 start = a;
336 size = s;
337 if (s >= 33 * PAGE_SIZE)
338 break;
339 }
340 start = PAGE_ALIGN(start);
341
342 min_low_pfn = start >> PAGE_SHIFT;
343 max_low_pfn = (PPC_MEMSTART + total_lowmem) >> PAGE_SHIFT;
344 max_pfn = (PPC_MEMSTART + total_memory) >> PAGE_SHIFT;
345 boot_mapsize = init_bootmem_node(&contig_page_data, min_low_pfn,
346 PPC_MEMSTART >> PAGE_SHIFT,
347 max_low_pfn);
348
349 /* remove the bootmem bitmap from the available memory */
350 mem_pieces_remove(&phys_avail, start, boot_mapsize, 1);
351
352 /* add everything in phys_avail into the bootmem map */
353 for (i = 0; i < phys_avail.n_regions; ++i)
354 free_bootmem(phys_avail.regions[i].address,
355 phys_avail.regions[i].size);
356
357 init_bootmem_done = 1;
358}
359
360/*
361 * paging_init() sets up the page tables - in fact we've already done this.
362 */
363void __init paging_init(void)
364{
365 unsigned long zones_size[MAX_NR_ZONES], i;
366
367#ifdef CONFIG_HIGHMEM
368 map_page(PKMAP_BASE, 0, 0); /* XXX gross */
369 pkmap_page_table = pte_offset_kernel(pmd_offset(pgd_offset_k
370 (PKMAP_BASE), PKMAP_BASE), PKMAP_BASE);
371 map_page(KMAP_FIX_BEGIN, 0, 0); /* XXX gross */
372 kmap_pte = pte_offset_kernel(pmd_offset(pgd_offset_k
373 (KMAP_FIX_BEGIN), KMAP_FIX_BEGIN), KMAP_FIX_BEGIN);
374 kmap_prot = PAGE_KERNEL;
375#endif /* CONFIG_HIGHMEM */
376
377 /*
378 * All pages are DMA-able so we put them all in the DMA zone.
379 */
380 zones_size[ZONE_DMA] = total_lowmem >> PAGE_SHIFT;
381 for (i = 1; i < MAX_NR_ZONES; i++)
382 zones_size[i] = 0;
383
384#ifdef CONFIG_HIGHMEM
385 zones_size[ZONE_HIGHMEM] = (total_memory - total_lowmem) >> PAGE_SHIFT;
386#endif /* CONFIG_HIGHMEM */
387
388 free_area_init(zones_size);
389}
390
391void __init mem_init(void)
392{
393 unsigned long addr;
394 int codepages = 0;
395 int datapages = 0;
396 int initpages = 0;
397#ifdef CONFIG_HIGHMEM
398 unsigned long highmem_mapnr;
399
400 highmem_mapnr = total_lowmem >> PAGE_SHIFT;
401#endif /* CONFIG_HIGHMEM */
402 max_mapnr = total_memory >> PAGE_SHIFT;
403
404 high_memory = (void *) __va(PPC_MEMSTART + total_lowmem);
405 num_physpages = max_mapnr; /* RAM is assumed contiguous */
406
407 totalram_pages += free_all_bootmem();
408
409#ifdef CONFIG_BLK_DEV_INITRD
410 /* if we are booted from BootX with an initial ramdisk,
411 make sure the ramdisk pages aren't reserved. */
412 if (initrd_start) {
413 for (addr = initrd_start; addr < initrd_end; addr += PAGE_SIZE)
414 ClearPageReserved(virt_to_page(addr));
415 }
416#endif /* CONFIG_BLK_DEV_INITRD */
417
418#ifdef CONFIG_PPC_OF
419 /* mark the RTAS pages as reserved */
420 if ( rtas_data )
421 for (addr = (ulong)__va(rtas_data);
422 addr < PAGE_ALIGN((ulong)__va(rtas_data)+rtas_size) ;
423 addr += PAGE_SIZE)
424 SetPageReserved(virt_to_page(addr));
425#endif
426#ifdef CONFIG_PPC_PMAC
427 if (agp_special_page)
428 SetPageReserved(virt_to_page(agp_special_page));
429#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 for (addr = PAGE_OFFSET; addr < (unsigned long)high_memory;
431 addr += PAGE_SIZE) {
432 if (!PageReserved(virt_to_page(addr)))
433 continue;
434 if (addr < (ulong) etext)
435 codepages++;
436 else if (addr >= (unsigned long)&__init_begin
437 && addr < (unsigned long)&__init_end)
438 initpages++;
439 else if (addr < (ulong) klimit)
440 datapages++;
441 }
442
443#ifdef CONFIG_HIGHMEM
444 {
445 unsigned long pfn;
446
447 for (pfn = highmem_mapnr; pfn < max_mapnr; ++pfn) {
448 struct page *page = mem_map + pfn;
449
450 ClearPageReserved(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 set_page_count(page, 1);
452 __free_page(page);
453 totalhigh_pages++;
454 }
455 totalram_pages += totalhigh_pages;
456 }
457#endif /* CONFIG_HIGHMEM */
458
459 printk("Memory: %luk available (%dk kernel code, %dk data, %dk init, %ldk highmem)\n",
460 (unsigned long)nr_free_pages()<< (PAGE_SHIFT-10),
461 codepages<< (PAGE_SHIFT-10), datapages<< (PAGE_SHIFT-10),
462 initpages<< (PAGE_SHIFT-10),
463 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)));
Benjamin Herrenschmidt6879dc12005-06-21 17:15:30 -0700464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465#ifdef CONFIG_PPC_PMAC
466 if (agp_special_page)
467 printk(KERN_INFO "AGP special page: 0x%08lx\n", agp_special_page);
468#endif
469
470 mem_init_done = 1;
471}
472
473/*
474 * Set phys_avail to the amount of physical memory,
475 * less the kernel text/data/bss.
476 */
477void __init
478set_phys_avail(unsigned long total_memory)
479{
480 unsigned long kstart, ksize;
481
482 /*
483 * Initially, available physical memory is equivalent to all
484 * physical memory.
485 */
486
487 phys_avail.regions[0].address = PPC_MEMSTART;
488 phys_avail.regions[0].size = total_memory;
489 phys_avail.n_regions = 1;
490
491 /*
492 * Map out the kernel text/data/bss from the available physical
493 * memory.
494 */
495
496 kstart = __pa(_stext); /* should be 0 */
497 ksize = PAGE_ALIGN(klimit - _stext);
498
499 mem_pieces_remove(&phys_avail, kstart, ksize, 0);
500 mem_pieces_remove(&phys_avail, 0, 0x4000, 0);
501
502#if defined(CONFIG_BLK_DEV_INITRD)
503 /* Remove the init RAM disk from the available memory. */
504 if (initrd_start) {
505 mem_pieces_remove(&phys_avail, __pa(initrd_start),
506 initrd_end - initrd_start, 1);
507 }
508#endif /* CONFIG_BLK_DEV_INITRD */
509#ifdef CONFIG_PPC_OF
510 /* remove the RTAS pages from the available memory */
511 if (rtas_data)
512 mem_pieces_remove(&phys_avail, rtas_data, rtas_size, 1);
513#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514#ifdef CONFIG_PPC_PMAC
515 /* Because of some uninorth weirdness, we need a page of
516 * memory as high as possible (it must be outside of the
517 * bus address seen as the AGP aperture). It will be used
518 * by the r128 DRM driver
519 *
520 * FIXME: We need to make sure that page doesn't overlap any of the\
521 * above. This could be done by improving mem_pieces_find to be able
522 * to do a backward search from the end of the list.
523 */
524 if (_machine == _MACH_Pmac && find_devices("uni-north-agp")) {
525 agp_special_page = (total_memory - PAGE_SIZE);
526 mem_pieces_remove(&phys_avail, agp_special_page, PAGE_SIZE, 0);
527 agp_special_page = (unsigned long)__va(agp_special_page);
528 }
529#endif /* CONFIG_PPC_PMAC */
530}
531
532/* Mark some memory as reserved by removing it from phys_avail. */
533void __init reserve_phys_mem(unsigned long start, unsigned long size)
534{
535 mem_pieces_remove(&phys_avail, start, size, 1);
536}
537
538/*
539 * This is called when a page has been modified by the kernel.
540 * It just marks the page as not i-cache clean. We do the i-cache
541 * flush later when the page is given to a user process, if necessary.
542 */
543void flush_dcache_page(struct page *page)
544{
545 clear_bit(PG_arch_1, &page->flags);
546}
547
548void flush_dcache_icache_page(struct page *page)
549{
550#ifdef CONFIG_BOOKE
Roland Dreier5a6a4d42005-09-03 15:55:43 -0700551 void *start = kmap_atomic(page, KM_PPC_SYNC_ICACHE);
552 __flush_dcache_icache(start);
553 kunmap_atomic(start, KM_PPC_SYNC_ICACHE);
Lee Nickscc9c5402005-09-03 15:55:49 -0700554#elif defined(CONFIG_8xx)
Anton Wöllertbf85fa62005-07-27 04:45:17 -0300555 /* On 8xx there is no need to kmap since highmem is not supported */
556 __flush_dcache_icache(page_address(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557#else
558 __flush_dcache_icache_phys(page_to_pfn(page) << PAGE_SHIFT);
559#endif
560
561}
562void clear_user_page(void *page, unsigned long vaddr, struct page *pg)
563{
564 clear_page(page);
565 clear_bit(PG_arch_1, &pg->flags);
566}
567
568void copy_user_page(void *vto, void *vfrom, unsigned long vaddr,
569 struct page *pg)
570{
571 copy_page(vto, vfrom);
572 clear_bit(PG_arch_1, &pg->flags);
573}
574
575void flush_icache_user_range(struct vm_area_struct *vma, struct page *page,
576 unsigned long addr, int len)
577{
578 unsigned long maddr;
579
580 maddr = (unsigned long) kmap(page) + (addr & ~PAGE_MASK);
581 flush_icache_range(maddr, maddr + len);
582 kunmap(page);
583}
584
585/*
586 * This is called at the end of handling a user page fault, when the
587 * fault has been handled by updating a PTE in the linux page tables.
588 * We use it to preload an HPTE into the hash table corresponding to
589 * the updated linux PTE.
590 */
591void update_mmu_cache(struct vm_area_struct *vma, unsigned long address,
592 pte_t pte)
593{
594 /* handle i-cache coherency */
595 unsigned long pfn = pte_pfn(pte);
596
597 if (pfn_valid(pfn)) {
598 struct page *page = pfn_to_page(pfn);
599 if (!PageReserved(page)
600 && !test_bit(PG_arch_1, &page->flags)) {
Marcelo Tosattibb165742005-06-27 13:09:00 -0300601 if (vma->vm_mm == current->active_mm) {
602#ifdef CONFIG_8xx
603 /* On 8xx, cache control instructions (particularly
604 * "dcbst" from flush_dcache_icache) fault as write
605 * operation if there is an unpopulated TLB entry
606 * for the address in question. To workaround that,
607 * we invalidate the TLB here, thus avoiding dcbst
608 * misbehaviour.
609 */
610 _tlbie(address);
611#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 __flush_dcache_icache((void *) address);
Marcelo Tosattibb165742005-06-27 13:09:00 -0300613 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 flush_dcache_icache_page(page);
615 set_bit(PG_arch_1, &page->flags);
616 }
617 }
618
619#ifdef CONFIG_PPC_STD_MMU
620 /* We only want HPTEs for linux PTEs that have _PAGE_ACCESSED set */
621 if (Hash != 0 && pte_young(pte)) {
622 struct mm_struct *mm;
623 pmd_t *pmd;
624
625 mm = (address < TASK_SIZE)? vma->vm_mm: &init_mm;
626 pmd = pmd_offset(pgd_offset(mm, address), address);
627 if (!pmd_none(*pmd))
628 add_hash_page(mm->context, address, pmd_val(*pmd));
629 }
630#endif
631}
632
633/*
634 * This is called by /dev/mem to know if a given address has to
635 * be mapped non-cacheable or not
636 */
637int page_is_ram(unsigned long pfn)
638{
639 unsigned long paddr = (pfn << PAGE_SHIFT);
640
641 return paddr < __pa(high_memory);
642}
643
644pgprot_t phys_mem_access_prot(struct file *file, unsigned long addr,
645 unsigned long size, pgprot_t vma_prot)
646{
647 if (ppc_md.phys_mem_access_prot)
648 return ppc_md.phys_mem_access_prot(file, addr, size, vma_prot);
649
650 if (!page_is_ram(addr >> PAGE_SHIFT))
651 vma_prot = __pgprot(pgprot_val(vma_prot)
652 | _PAGE_GUARDED | _PAGE_NO_CACHE);
653 return vma_prot;
654}
655EXPORT_SYMBOL(phys_mem_access_prot);