blob: 386e000bcb737845f17fe9935d54c4292365fddf [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71extern char _end[];
72extern char etext[], _stext[];
73extern char __init_begin, __init_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75#ifdef CONFIG_HIGHMEM
76pte_t *kmap_pte;
77pgprot_t kmap_prot;
78
79EXPORT_SYMBOL(kmap_prot);
80EXPORT_SYMBOL(kmap_pte);
81#endif
82
83void MMU_init(void);
84void set_phys_avail(unsigned long total_ram);
85
86/* XXX should be in current.h -- paulus */
87extern struct task_struct *current_set[NR_CPUS];
88
89char *klimit = _end;
90struct mem_pieces phys_avail;
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
93 * this tells the system to map all of ram with the segregs
94 * (i.e. page tables) instead of the bats.
95 * -- Cort
96 */
97int __map_without_bats;
98int __map_without_ltlbs;
99
100/* max amount of RAM to use */
101unsigned long __max_memory;
102/* max amount of low RAM to map in */
103unsigned long __max_low_memory = MAX_LOW_MEM;
104
105void show_mem(void)
106{
107 int i,free = 0,total = 0,reserved = 0;
108 int shared = 0, cached = 0;
109 int highmem = 0;
110
111 printk("Mem-info:\n");
112 show_free_areas();
113 printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
114 i = max_mapnr;
115 while (i-- > 0) {
116 total++;
117 if (PageHighMem(mem_map+i))
118 highmem++;
119 if (PageReserved(mem_map+i))
120 reserved++;
121 else if (PageSwapCache(mem_map+i))
122 cached++;
123 else if (!page_count(mem_map+i))
124 free++;
125 else
126 shared += page_count(mem_map+i) - 1;
127 }
128 printk("%d pages of RAM\n",total);
129 printk("%d pages of HIGHMEM\n", highmem);
130 printk("%d free pages\n",free);
131 printk("%d reserved pages\n",reserved);
132 printk("%d pages shared\n",shared);
133 printk("%d pages swap cached\n",cached);
134}
135
136/* Free up now-unused memory */
137static void free_sec(unsigned long start, unsigned long end, const char *name)
138{
139 unsigned long cnt = 0;
140
141 while (start < end) {
142 ClearPageReserved(virt_to_page(start));
Nick Piggin7835e982006-03-22 00:08:40 -0800143 init_page_count(virt_to_page(start));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 free_page(start);
145 cnt++;
146 start += PAGE_SIZE;
147 }
148 if (cnt) {
149 printk(" %ldk %s", cnt << (PAGE_SHIFT - 10), name);
150 totalram_pages += cnt;
151 }
152}
153
154void free_initmem(void)
155{
156#define FREESEC(TYPE) \
157 free_sec((unsigned long)(&__ ## TYPE ## _begin), \
158 (unsigned long)(&__ ## TYPE ## _end), \
159 #TYPE);
160
161 printk ("Freeing unused kernel memory:");
162 FREESEC(init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 printk("\n");
Paul Mackerras6c37a882005-05-20 16:57:22 +1000164 ppc_md.progress = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#undef FREESEC
166}
167
168#ifdef CONFIG_BLK_DEV_INITRD
169void free_initrd_mem(unsigned long start, unsigned long end)
170{
171 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
172
173 for (; start < end; start += PAGE_SIZE) {
174 ClearPageReserved(virt_to_page(start));
Nick Piggin7835e982006-03-22 00:08:40 -0800175 init_page_count(virt_to_page(start));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 free_page(start);
177 totalram_pages++;
178 }
179}
180#endif
181
182/*
183 * Check for command-line options that affect what MMU_init will do.
184 */
185void MMU_setup(void)
186{
187 /* Check for nobats option (used in mapin_ram). */
188 if (strstr(cmd_line, "nobats")) {
189 __map_without_bats = 1;
190 }
191
192 if (strstr(cmd_line, "noltlbs")) {
193 __map_without_ltlbs = 1;
194 }
195
196 /* Look for mem= option on command line */
197 if (strstr(cmd_line, "mem=")) {
198 char *p, *q;
199 unsigned long maxmem = 0;
200
201 for (q = cmd_line; (p = strstr(q, "mem=")) != 0; ) {
202 q = p + 4;
203 if (p > cmd_line && p[-1] != ' ')
204 continue;
205 maxmem = simple_strtoul(q, &q, 0);
206 if (*q == 'k' || *q == 'K') {
207 maxmem <<= 10;
208 ++q;
209 } else if (*q == 'm' || *q == 'M') {
210 maxmem <<= 20;
211 ++q;
212 }
213 }
214 __max_memory = maxmem;
215 }
216}
217
218/*
219 * MMU_init sets up the basic memory mappings for the kernel,
220 * including both RAM and possibly some I/O regions,
221 * and sets up the page tables and the MMU hardware ready to go.
222 */
223void __init MMU_init(void)
224{
225 if (ppc_md.progress)
226 ppc_md.progress("MMU:enter", 0x111);
227
228 /* parse args from command line */
229 MMU_setup();
230
231 /*
232 * Figure out how much memory we have, how much
233 * is lowmem, and how much is highmem. If we were
234 * passed the total memory size from the bootloader,
235 * just use it.
236 */
237 if (boot_mem_size)
238 total_memory = boot_mem_size;
239 else
240 total_memory = ppc_md.find_end_of_memory();
241
242 if (__max_memory && total_memory > __max_memory)
243 total_memory = __max_memory;
244 total_lowmem = total_memory;
245#ifdef CONFIG_FSL_BOOKE
246 /* Freescale Book-E parts expect lowmem to be mapped by fixed TLB
247 * entries, so we need to adjust lowmem to match the amount we can map
248 * in the fixed entries */
249 adjust_total_lowmem();
250#endif /* CONFIG_FSL_BOOKE */
251 if (total_lowmem > __max_low_memory) {
252 total_lowmem = __max_low_memory;
253#ifndef CONFIG_HIGHMEM
254 total_memory = total_lowmem;
255#endif /* CONFIG_HIGHMEM */
256 }
257 set_phys_avail(total_lowmem);
258
259 /* Initialize the MMU hardware */
260 if (ppc_md.progress)
261 ppc_md.progress("MMU:hw init", 0x300);
262 MMU_init_hw();
263
264 /* Map in all of RAM starting at KERNELBASE */
265 if (ppc_md.progress)
266 ppc_md.progress("MMU:mapin", 0x301);
267 mapin_ram();
268
269#ifdef CONFIG_HIGHMEM
270 ioremap_base = PKMAP_BASE;
271#else
272 ioremap_base = 0xfe000000UL; /* for now, could be 0xfffff000 */
273#endif /* CONFIG_HIGHMEM */
274 ioremap_bot = ioremap_base;
275
276 /* Map in I/O resources */
277 if (ppc_md.progress)
278 ppc_md.progress("MMU:setio", 0x302);
279 if (ppc_md.setup_io_mappings)
280 ppc_md.setup_io_mappings();
281
282 /* Initialize the context management stuff */
283 mmu_context_init();
284
285 if (ppc_md.progress)
286 ppc_md.progress("MMU:exit", 0x211);
287
288#ifdef CONFIG_BOOTX_TEXT
289 /* By default, we are no longer mapped */
290 boot_text_mapped = 0;
291 /* Must be done last, or ppc_md.progress will die. */
292 map_boot_text();
293#endif
294}
295
296/* This is only called until mem_init is done. */
297void __init *early_get_page(void)
298{
299 void *p;
300
301 if (init_bootmem_done) {
302 p = alloc_bootmem_pages(PAGE_SIZE);
303 } else {
304 p = mem_pieces_find(PAGE_SIZE, PAGE_SIZE);
305 }
306 return p;
307}
308
309/*
310 * Initialize the bootmem system and give it all the memory we
311 * have available.
312 */
313void __init do_init_bootmem(void)
314{
315 unsigned long start, size;
316 int i;
317
318 /*
319 * Find an area to use for the bootmem bitmap.
320 * We look for the first area which is at least
321 * 128kB in length (128kB is enough for a bitmap
322 * for 4GB of memory, using 4kB pages), plus 1 page
323 * (in case the address isn't page-aligned).
324 */
325 start = 0;
326 size = 0;
327 for (i = 0; i < phys_avail.n_regions; ++i) {
328 unsigned long a = phys_avail.regions[i].address;
329 unsigned long s = phys_avail.regions[i].size;
330 if (s <= size)
331 continue;
332 start = a;
333 size = s;
334 if (s >= 33 * PAGE_SIZE)
335 break;
336 }
337 start = PAGE_ALIGN(start);
338
339 min_low_pfn = start >> PAGE_SHIFT;
340 max_low_pfn = (PPC_MEMSTART + total_lowmem) >> PAGE_SHIFT;
341 max_pfn = (PPC_MEMSTART + total_memory) >> PAGE_SHIFT;
342 boot_mapsize = init_bootmem_node(&contig_page_data, min_low_pfn,
343 PPC_MEMSTART >> PAGE_SHIFT,
344 max_low_pfn);
345
346 /* remove the bootmem bitmap from the available memory */
347 mem_pieces_remove(&phys_avail, start, boot_mapsize, 1);
348
349 /* add everything in phys_avail into the bootmem map */
350 for (i = 0; i < phys_avail.n_regions; ++i)
351 free_bootmem(phys_avail.regions[i].address,
352 phys_avail.regions[i].size);
353
354 init_bootmem_done = 1;
355}
356
357/*
358 * paging_init() sets up the page tables - in fact we've already done this.
359 */
360void __init paging_init(void)
361{
362 unsigned long zones_size[MAX_NR_ZONES], i;
363
364#ifdef CONFIG_HIGHMEM
365 map_page(PKMAP_BASE, 0, 0); /* XXX gross */
366 pkmap_page_table = pte_offset_kernel(pmd_offset(pgd_offset_k
367 (PKMAP_BASE), PKMAP_BASE), PKMAP_BASE);
368 map_page(KMAP_FIX_BEGIN, 0, 0); /* XXX gross */
369 kmap_pte = pte_offset_kernel(pmd_offset(pgd_offset_k
370 (KMAP_FIX_BEGIN), KMAP_FIX_BEGIN), KMAP_FIX_BEGIN);
371 kmap_prot = PAGE_KERNEL;
372#endif /* CONFIG_HIGHMEM */
373
374 /*
375 * All pages are DMA-able so we put them all in the DMA zone.
376 */
377 zones_size[ZONE_DMA] = total_lowmem >> PAGE_SHIFT;
378 for (i = 1; i < MAX_NR_ZONES; i++)
379 zones_size[i] = 0;
380
381#ifdef CONFIG_HIGHMEM
382 zones_size[ZONE_HIGHMEM] = (total_memory - total_lowmem) >> PAGE_SHIFT;
383#endif /* CONFIG_HIGHMEM */
384
385 free_area_init(zones_size);
386}
387
388void __init mem_init(void)
389{
390 unsigned long addr;
391 int codepages = 0;
392 int datapages = 0;
393 int initpages = 0;
394#ifdef CONFIG_HIGHMEM
395 unsigned long highmem_mapnr;
396
397 highmem_mapnr = total_lowmem >> PAGE_SHIFT;
398#endif /* CONFIG_HIGHMEM */
399 max_mapnr = total_memory >> PAGE_SHIFT;
400
401 high_memory = (void *) __va(PPC_MEMSTART + total_lowmem);
402 num_physpages = max_mapnr; /* RAM is assumed contiguous */
403
404 totalram_pages += free_all_bootmem();
405
406#ifdef CONFIG_BLK_DEV_INITRD
407 /* if we are booted from BootX with an initial ramdisk,
408 make sure the ramdisk pages aren't reserved. */
409 if (initrd_start) {
410 for (addr = initrd_start; addr < initrd_end; addr += PAGE_SIZE)
411 ClearPageReserved(virt_to_page(addr));
412 }
413#endif /* CONFIG_BLK_DEV_INITRD */
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 for (addr = PAGE_OFFSET; addr < (unsigned long)high_memory;
416 addr += PAGE_SIZE) {
417 if (!PageReserved(virt_to_page(addr)))
418 continue;
419 if (addr < (ulong) etext)
420 codepages++;
421 else if (addr >= (unsigned long)&__init_begin
422 && addr < (unsigned long)&__init_end)
423 initpages++;
424 else if (addr < (ulong) klimit)
425 datapages++;
426 }
427
428#ifdef CONFIG_HIGHMEM
429 {
430 unsigned long pfn;
431
432 for (pfn = highmem_mapnr; pfn < max_mapnr; ++pfn) {
433 struct page *page = mem_map + pfn;
434
435 ClearPageReserved(page);
Nick Piggin7835e982006-03-22 00:08:40 -0800436 init_page_count(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 __free_page(page);
438 totalhigh_pages++;
439 }
440 totalram_pages += totalhigh_pages;
441 }
442#endif /* CONFIG_HIGHMEM */
443
444 printk("Memory: %luk available (%dk kernel code, %dk data, %dk init, %ldk highmem)\n",
445 (unsigned long)nr_free_pages()<< (PAGE_SHIFT-10),
446 codepages<< (PAGE_SHIFT-10), datapages<< (PAGE_SHIFT-10),
447 initpages<< (PAGE_SHIFT-10),
448 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)));
Benjamin Herrenschmidt6879dc12005-06-21 17:15:30 -0700449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 mem_init_done = 1;
451}
452
453/*
454 * Set phys_avail to the amount of physical memory,
455 * less the kernel text/data/bss.
456 */
457void __init
458set_phys_avail(unsigned long total_memory)
459{
460 unsigned long kstart, ksize;
461
462 /*
463 * Initially, available physical memory is equivalent to all
464 * physical memory.
465 */
466
467 phys_avail.regions[0].address = PPC_MEMSTART;
468 phys_avail.regions[0].size = total_memory;
469 phys_avail.n_regions = 1;
470
471 /*
472 * Map out the kernel text/data/bss from the available physical
473 * memory.
474 */
475
476 kstart = __pa(_stext); /* should be 0 */
477 ksize = PAGE_ALIGN(klimit - _stext);
478
479 mem_pieces_remove(&phys_avail, kstart, ksize, 0);
480 mem_pieces_remove(&phys_avail, 0, 0x4000, 0);
481
482#if defined(CONFIG_BLK_DEV_INITRD)
483 /* Remove the init RAM disk from the available memory. */
484 if (initrd_start) {
485 mem_pieces_remove(&phys_avail, __pa(initrd_start),
486 initrd_end - initrd_start, 1);
487 }
488#endif /* CONFIG_BLK_DEV_INITRD */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
491/* Mark some memory as reserved by removing it from phys_avail. */
492void __init reserve_phys_mem(unsigned long start, unsigned long size)
493{
494 mem_pieces_remove(&phys_avail, start, size, 1);
495}
496
497/*
498 * This is called when a page has been modified by the kernel.
499 * It just marks the page as not i-cache clean. We do the i-cache
500 * flush later when the page is given to a user process, if necessary.
501 */
502void flush_dcache_page(struct page *page)
503{
504 clear_bit(PG_arch_1, &page->flags);
505}
506
507void flush_dcache_icache_page(struct page *page)
508{
509#ifdef CONFIG_BOOKE
Roland Dreier5a6a4d42005-09-03 15:55:43 -0700510 void *start = kmap_atomic(page, KM_PPC_SYNC_ICACHE);
511 __flush_dcache_icache(start);
512 kunmap_atomic(start, KM_PPC_SYNC_ICACHE);
Lee Nickscc9c5402005-09-03 15:55:49 -0700513#elif defined(CONFIG_8xx)
Anton Wöllertbf85fa62005-07-27 04:45:17 -0300514 /* On 8xx there is no need to kmap since highmem is not supported */
515 __flush_dcache_icache(page_address(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516#else
517 __flush_dcache_icache_phys(page_to_pfn(page) << PAGE_SHIFT);
518#endif
519
520}
521void clear_user_page(void *page, unsigned long vaddr, struct page *pg)
522{
523 clear_page(page);
524 clear_bit(PG_arch_1, &pg->flags);
525}
526
527void copy_user_page(void *vto, void *vfrom, unsigned long vaddr,
528 struct page *pg)
529{
530 copy_page(vto, vfrom);
531 clear_bit(PG_arch_1, &pg->flags);
532}
533
534void flush_icache_user_range(struct vm_area_struct *vma, struct page *page,
535 unsigned long addr, int len)
536{
537 unsigned long maddr;
538
539 maddr = (unsigned long) kmap(page) + (addr & ~PAGE_MASK);
540 flush_icache_range(maddr, maddr + len);
541 kunmap(page);
542}
543
544/*
545 * This is called at the end of handling a user page fault, when the
546 * fault has been handled by updating a PTE in the linux page tables.
547 * We use it to preload an HPTE into the hash table corresponding to
548 * the updated linux PTE.
549 */
550void update_mmu_cache(struct vm_area_struct *vma, unsigned long address,
551 pte_t pte)
552{
553 /* handle i-cache coherency */
554 unsigned long pfn = pte_pfn(pte);
555
556 if (pfn_valid(pfn)) {
557 struct page *page = pfn_to_page(pfn);
Marcelo Tosattieb07d962005-11-14 05:38:31 -0200558#ifdef CONFIG_8xx
559 /* On 8xx, the TLB handlers work in 2 stages:
560 * First, a zeroed entry is loaded by TLBMiss handler,
561 * which causes the TLBError handler to be triggered.
562 * That means the zeroed TLB has to be invalidated
563 * whenever a page miss occurs.
564 */
565 _tlbie(address);
566#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (!PageReserved(page)
568 && !test_bit(PG_arch_1, &page->flags)) {
Marcelo Tosattieb07d962005-11-14 05:38:31 -0200569 if (vma->vm_mm == current->active_mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 __flush_dcache_icache((void *) address);
Marcelo Tosattieb07d962005-11-14 05:38:31 -0200571 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 flush_dcache_icache_page(page);
573 set_bit(PG_arch_1, &page->flags);
574 }
575 }
576
577#ifdef CONFIG_PPC_STD_MMU
578 /* We only want HPTEs for linux PTEs that have _PAGE_ACCESSED set */
579 if (Hash != 0 && pte_young(pte)) {
580 struct mm_struct *mm;
581 pmd_t *pmd;
582
583 mm = (address < TASK_SIZE)? vma->vm_mm: &init_mm;
584 pmd = pmd_offset(pgd_offset(mm, address), address);
585 if (!pmd_none(*pmd))
586 add_hash_page(mm->context, address, pmd_val(*pmd));
587 }
588#endif
589}
590
591/*
592 * This is called by /dev/mem to know if a given address has to
593 * be mapped non-cacheable or not
594 */
595int page_is_ram(unsigned long pfn)
596{
Roland Dreier8b150472005-10-28 17:46:18 -0700597 return pfn < max_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
Roland Dreier8b150472005-10-28 17:46:18 -0700600pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 unsigned long size, pgprot_t vma_prot)
602{
603 if (ppc_md.phys_mem_access_prot)
Roland Dreier8b150472005-10-28 17:46:18 -0700604 return ppc_md.phys_mem_access_prot(file, pfn, size, vma_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Roland Dreier8b150472005-10-28 17:46:18 -0700606 if (!page_is_ram(pfn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 vma_prot = __pgprot(pgprot_val(vma_prot)
608 | _PAGE_GUARDED | _PAGE_NO_CACHE);
609 return vma_prot;
610}
611EXPORT_SYMBOL(phys_mem_access_prot);