1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * This file contains the routines for initializing the MMU 4 * on the 4xx series of chips. 5 * -- paulus 6 * 7 * Derived from arch/ppc/mm/init.c: 8 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org) 9 * 10 * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au) 11 * and Cort Dougan (PReP) (cort@cs.nmt.edu) 12 * Copyright (C) 1996 Paul Mackerras 13 * 14 * Derived from "arch/i386/mm/init.c" 15 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 16 */ 17 18 #include <linux/signal.h> 19 #include <linux/sched.h> 20 #include <linux/kernel.h> 21 #include <linux/errno.h> 22 #include <linux/string.h> 23 #include <linux/types.h> 24 #include <linux/ptrace.h> 25 #include <linux/mman.h> 26 #include <linux/mm.h> 27 #include <linux/swap.h> 28 #include <linux/stddef.h> 29 #include <linux/vmalloc.h> 30 #include <linux/init.h> 31 #include <linux/delay.h> 32 #include <linux/highmem.h> 33 #include <linux/memblock.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/mmu.h> 40 #include <linux/uaccess.h> 41 #include <asm/smp.h> 42 #include <asm/bootx.h> 43 #include <asm/machdep.h> 44 #include <asm/setup.h> 45 46 #include <mm/mmu_decl.h> 47 48 extern int __map_without_ltlbs; 49 /* 50 * MMU_init_hw does the chip-specific initialization of the MMU hardware. 51 */ 52 void __init MMU_init_hw(void) 53 { 54 /* 55 * The Zone Protection Register (ZPR) defines how protection will 56 * be applied to every page which is a member of a given zone. At 57 * present, we utilize only two of the 4xx's zones. 58 * The zone index bits (of ZSEL) in the PTE are used for software 59 * indicators, except the LSB. For user access, zone 1 is used, 60 * for kernel access, zone 0 is used. We set all but zone 1 61 * to zero, allowing only kernel access as indicated in the PTE. 62 * For zone 1, we set a 01 binary (a value of 10 will not work) 63 * to allow user access as indicated in the PTE. This also allows 64 * kernel access as indicated in the PTE. 65 */ 66 67 mtspr(SPRN_ZPR, 0x10000000); 68 69 flush_instruction_cache(); 70 71 /* 72 * Set up the real-mode cache parameters for the exception vector 73 * handlers (which are run in real-mode). 74 */ 75 76 mtspr(SPRN_DCWR, 0x00000000); /* All caching is write-back */ 77 78 /* 79 * Cache instruction and data space where the exception 80 * vectors and the kernel live in real-mode. 81 */ 82 83 mtspr(SPRN_DCCR, 0xFFFF0000); /* 2GByte of data space at 0x0. */ 84 mtspr(SPRN_ICCR, 0xFFFF0000); /* 2GByte of instr. space at 0x0. */ 85 } 86 87 #define LARGE_PAGE_SIZE_16M (1<<24) 88 #define LARGE_PAGE_SIZE_4M (1<<22) 89 90 unsigned long __init mmu_mapin_ram(unsigned long base, unsigned long top) 91 { 92 unsigned long v, s, mapped; 93 phys_addr_t p; 94 95 v = KERNELBASE; 96 p = 0; 97 s = total_lowmem; 98 99 if (__map_without_ltlbs) 100 return 0; 101 102 while (s >= LARGE_PAGE_SIZE_16M) { 103 pmd_t *pmdp; 104 unsigned long val = p | _PMD_SIZE_16M | _PAGE_EXEC | _PAGE_RW; 105 106 pmdp = pmd_off_k(v); 107 *pmdp++ = __pmd(val); 108 *pmdp++ = __pmd(val); 109 *pmdp++ = __pmd(val); 110 *pmdp++ = __pmd(val); 111 112 v += LARGE_PAGE_SIZE_16M; 113 p += LARGE_PAGE_SIZE_16M; 114 s -= LARGE_PAGE_SIZE_16M; 115 } 116 117 while (s >= LARGE_PAGE_SIZE_4M) { 118 pmd_t *pmdp; 119 unsigned long val = p | _PMD_SIZE_4M | _PAGE_EXEC | _PAGE_RW; 120 121 pmdp = pmd_off_k(v); 122 *pmdp = __pmd(val); 123 124 v += LARGE_PAGE_SIZE_4M; 125 p += LARGE_PAGE_SIZE_4M; 126 s -= LARGE_PAGE_SIZE_4M; 127 } 128 129 mapped = total_lowmem - s; 130 131 /* If the size of RAM is not an exact power of two, we may not 132 * have covered RAM in its entirety with 16 and 4 MiB 133 * pages. Consequently, restrict the top end of RAM currently 134 * allocable so that calls to the MEMBLOCK to allocate PTEs for "tail" 135 * coverage with normal-sized pages (or other reasons) do not 136 * attempt to allocate outside the allowed range. 137 */ 138 memblock_set_current_limit(mapped); 139 140 return mapped; 141 } 142 143 void setup_initial_memory_limit(phys_addr_t first_memblock_base, 144 phys_addr_t first_memblock_size) 145 { 146 /* We don't currently support the first MEMBLOCK not mapping 0 147 * physical on those processors 148 */ 149 BUG_ON(first_memblock_base != 0); 150 151 /* 40x can only access 16MB at the moment (see head_40x.S) */ 152 memblock_set_current_limit(min_t(u64, first_memblock_size, 0x00800000)); 153 } 154