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