Christophe Leroy | a372acf | 2016-02-09 17:07:50 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file contains the routines for initializing the MMU |
| 3 | * on the 8xx series of chips. |
| 4 | * -- christophe |
| 5 | * |
| 6 | * Derived from arch/powerpc/mm/40x_mmu.c: |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version |
| 11 | * 2 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include <linux/memblock.h> |
Christophe Leroy | 4badd43 | 2016-05-17 09:02:45 +0200 | [diff] [blame] | 16 | #include <asm/fixmap.h> |
| 17 | #include <asm/code-patching.h> |
Christophe Leroy | a372acf | 2016-02-09 17:07:50 +0100 | [diff] [blame] | 18 | |
| 19 | #include "mmu_decl.h" |
| 20 | |
Christophe Leroy | 4badd43 | 2016-05-17 09:02:45 +0200 | [diff] [blame] | 21 | #define IMMR_SIZE (FIX_IMMR_SIZE << PAGE_SHIFT) |
| 22 | |
Christophe Leroy | a372acf | 2016-02-09 17:07:50 +0100 | [diff] [blame] | 23 | extern int __map_without_ltlbs; |
Christophe Leroy | 4badd43 | 2016-05-17 09:02:45 +0200 | [diff] [blame] | 24 | |
Christophe Leroy | eef784b | 2017-07-12 12:08:45 +0200 | [diff] [blame] | 25 | static unsigned long block_mapped_ram; |
| 26 | |
Christophe Leroy | 4badd43 | 2016-05-17 09:02:45 +0200 | [diff] [blame] | 27 | /* |
Christophe Leroy | eef784b | 2017-07-12 12:08:45 +0200 | [diff] [blame] | 28 | * Return PA for this VA if it is in an area mapped with LTLBs. |
| 29 | * Otherwise, returns 0 |
Christophe Leroy | 4badd43 | 2016-05-17 09:02:45 +0200 | [diff] [blame] | 30 | */ |
| 31 | phys_addr_t v_block_mapped(unsigned long va) |
| 32 | { |
| 33 | unsigned long p = PHYS_IMMR_BASE; |
| 34 | |
| 35 | if (__map_without_ltlbs) |
| 36 | return 0; |
| 37 | if (va >= VIRT_IMMR_BASE && va < VIRT_IMMR_BASE + IMMR_SIZE) |
| 38 | return p + va - VIRT_IMMR_BASE; |
Christophe Leroy | eef784b | 2017-07-12 12:08:45 +0200 | [diff] [blame] | 39 | if (va >= PAGE_OFFSET && va < PAGE_OFFSET + block_mapped_ram) |
| 40 | return __pa(va); |
Christophe Leroy | 4badd43 | 2016-05-17 09:02:45 +0200 | [diff] [blame] | 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | /* |
Christophe Leroy | eef784b | 2017-07-12 12:08:45 +0200 | [diff] [blame] | 45 | * Return VA for a given PA mapped with LTLBs or 0 if not mapped |
Christophe Leroy | 4badd43 | 2016-05-17 09:02:45 +0200 | [diff] [blame] | 46 | */ |
| 47 | unsigned long p_block_mapped(phys_addr_t pa) |
| 48 | { |
| 49 | unsigned long p = PHYS_IMMR_BASE; |
| 50 | |
| 51 | if (__map_without_ltlbs) |
| 52 | return 0; |
| 53 | if (pa >= p && pa < p + IMMR_SIZE) |
| 54 | return VIRT_IMMR_BASE + pa - p; |
Christophe Leroy | eef784b | 2017-07-12 12:08:45 +0200 | [diff] [blame] | 55 | if (pa < block_mapped_ram) |
| 56 | return (unsigned long)__va(pa); |
Christophe Leroy | 4badd43 | 2016-05-17 09:02:45 +0200 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | |
Christophe Leroy | 4ad2745 | 2016-05-17 09:02:54 +0200 | [diff] [blame] | 60 | #define LARGE_PAGE_SIZE_8M (1<<23) |
| 61 | |
Christophe Leroy | a372acf | 2016-02-09 17:07:50 +0100 | [diff] [blame] | 62 | /* |
| 63 | * MMU_init_hw does the chip-specific initialization of the MMU hardware. |
| 64 | */ |
| 65 | void __init MMU_init_hw(void) |
| 66 | { |
Christophe Leroy | 4ad2745 | 2016-05-17 09:02:54 +0200 | [diff] [blame] | 67 | /* PIN up to the 3 first 8Mb after IMMR in DTLB table */ |
Christophe Leroy | a3059b0 | 2017-07-12 12:08:51 +0200 | [diff] [blame] | 68 | #ifdef CONFIG_PIN_TLB_DATA |
Christophe Leroy | 4ad2745 | 2016-05-17 09:02:54 +0200 | [diff] [blame] | 69 | unsigned long ctr = mfspr(SPRN_MD_CTR) & 0xfe000000; |
| 70 | unsigned long flags = 0xf0 | MD_SPS16K | _PAGE_SHARED | _PAGE_DIRTY; |
Christophe Leroy | 62f64b4 | 2016-05-17 09:02:56 +0200 | [diff] [blame] | 71 | #ifdef CONFIG_PIN_TLB_IMMR |
| 72 | int i = 29; |
| 73 | #else |
| 74 | int i = 28; |
| 75 | #endif |
Christophe Leroy | 4ad2745 | 2016-05-17 09:02:54 +0200 | [diff] [blame] | 76 | unsigned long addr = 0; |
| 77 | unsigned long mem = total_lowmem; |
Christophe Leroy | a372acf | 2016-02-09 17:07:50 +0100 | [diff] [blame] | 78 | |
Christophe Leroy | 62f64b4 | 2016-05-17 09:02:56 +0200 | [diff] [blame] | 79 | for (; i < 32 && mem >= LARGE_PAGE_SIZE_8M; i++) { |
Christophe Leroy | 4ad2745 | 2016-05-17 09:02:54 +0200 | [diff] [blame] | 80 | mtspr(SPRN_MD_CTR, ctr | (i << 8)); |
| 81 | mtspr(SPRN_MD_EPN, (unsigned long)__va(addr) | MD_EVALID); |
| 82 | mtspr(SPRN_MD_TWC, MD_PS8MEG | MD_SVALID); |
| 83 | mtspr(SPRN_MD_RPN, addr | flags | _PAGE_PRESENT); |
| 84 | addr += LARGE_PAGE_SIZE_8M; |
| 85 | mem -= LARGE_PAGE_SIZE_8M; |
| 86 | } |
| 87 | #endif |
| 88 | } |
Christophe Leroy | a372acf | 2016-02-09 17:07:50 +0100 | [diff] [blame] | 89 | |
Christophe Leroy | 346bcc4 | 2017-07-12 12:08:55 +0200 | [diff] [blame] | 90 | static void __init mmu_mapin_immr(void) |
Christophe Leroy | 4badd43 | 2016-05-17 09:02:45 +0200 | [diff] [blame] | 91 | { |
| 92 | unsigned long p = PHYS_IMMR_BASE; |
| 93 | unsigned long v = VIRT_IMMR_BASE; |
| 94 | unsigned long f = pgprot_val(PAGE_KERNEL_NCG); |
| 95 | int offset; |
| 96 | |
| 97 | for (offset = 0; offset < IMMR_SIZE; offset += PAGE_SIZE) |
Christophe Leroy | 4386c09 | 2017-05-29 17:31:56 +0200 | [diff] [blame] | 98 | map_kernel_page(v + offset, p + offset, f); |
Christophe Leroy | 4badd43 | 2016-05-17 09:02:45 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /* Address of instructions to patch */ |
Christophe Leroy | 62f64b4 | 2016-05-17 09:02:56 +0200 | [diff] [blame] | 102 | #ifndef CONFIG_PIN_TLB_IMMR |
Christophe Leroy | 4badd43 | 2016-05-17 09:02:45 +0200 | [diff] [blame] | 103 | extern unsigned int DTLBMiss_jmp; |
| 104 | #endif |
Christophe Leroy | bb7f380 | 2016-05-17 09:02:51 +0200 | [diff] [blame] | 105 | extern unsigned int DTLBMiss_cmp, FixupDAR_cmp; |
Christophe Leroy | a3059b0 | 2017-07-12 12:08:51 +0200 | [diff] [blame] | 106 | #ifndef CONFIG_PIN_TLB_TEXT |
| 107 | extern unsigned int ITLBMiss_cmp; |
| 108 | #endif |
Christophe Leroy | bb7f380 | 2016-05-17 09:02:51 +0200 | [diff] [blame] | 109 | |
Christophe Leroy | 346bcc4 | 2017-07-12 12:08:55 +0200 | [diff] [blame] | 110 | static void __init mmu_patch_cmp_limit(unsigned int *addr, unsigned long mapped) |
Christophe Leroy | bb7f380 | 2016-05-17 09:02:51 +0200 | [diff] [blame] | 111 | { |
| 112 | unsigned int instr = *addr; |
| 113 | |
| 114 | instr &= 0xffff0000; |
| 115 | instr |= (unsigned long)__va(mapped) >> 16; |
| 116 | patch_instruction(addr, instr); |
| 117 | } |
Christophe Leroy | 4badd43 | 2016-05-17 09:02:45 +0200 | [diff] [blame] | 118 | |
Christophe Leroy | a372acf | 2016-02-09 17:07:50 +0100 | [diff] [blame] | 119 | unsigned long __init mmu_mapin_ram(unsigned long top) |
| 120 | { |
Christophe Leroy | bb7f380 | 2016-05-17 09:02:51 +0200 | [diff] [blame] | 121 | unsigned long mapped; |
Christophe Leroy | a372acf | 2016-02-09 17:07:50 +0100 | [diff] [blame] | 122 | |
Christophe Leroy | 4badd43 | 2016-05-17 09:02:45 +0200 | [diff] [blame] | 123 | if (__map_without_ltlbs) { |
Christophe Leroy | bb7f380 | 2016-05-17 09:02:51 +0200 | [diff] [blame] | 124 | mapped = 0; |
Christophe Leroy | 4badd43 | 2016-05-17 09:02:45 +0200 | [diff] [blame] | 125 | mmu_mapin_immr(); |
Christophe Leroy | 62f64b4 | 2016-05-17 09:02:56 +0200 | [diff] [blame] | 126 | #ifndef CONFIG_PIN_TLB_IMMR |
Christophe Leroy | 4badd43 | 2016-05-17 09:02:45 +0200 | [diff] [blame] | 127 | patch_instruction(&DTLBMiss_jmp, PPC_INST_NOP); |
| 128 | #endif |
Christophe Leroy | a3059b0 | 2017-07-12 12:08:51 +0200 | [diff] [blame] | 129 | #ifndef CONFIG_PIN_TLB_TEXT |
| 130 | mmu_patch_cmp_limit(&ITLBMiss_cmp, 0); |
| 131 | #endif |
Christophe Leroy | bb7f380 | 2016-05-17 09:02:51 +0200 | [diff] [blame] | 132 | } else { |
| 133 | mapped = top & ~(LARGE_PAGE_SIZE_8M - 1); |
Christophe Leroy | 4badd43 | 2016-05-17 09:02:45 +0200 | [diff] [blame] | 134 | } |
Christophe Leroy | a372acf | 2016-02-09 17:07:50 +0100 | [diff] [blame] | 135 | |
Christophe Leroy | bb7f380 | 2016-05-17 09:02:51 +0200 | [diff] [blame] | 136 | mmu_patch_cmp_limit(&DTLBMiss_cmp, mapped); |
| 137 | mmu_patch_cmp_limit(&FixupDAR_cmp, mapped); |
Christophe Leroy | a372acf | 2016-02-09 17:07:50 +0100 | [diff] [blame] | 138 | |
| 139 | /* If the size of RAM is not an exact power of two, we may not |
| 140 | * have covered RAM in its entirety with 8 MiB |
| 141 | * pages. Consequently, restrict the top end of RAM currently |
| 142 | * allocable so that calls to the MEMBLOCK to allocate PTEs for "tail" |
| 143 | * coverage with normal-sized pages (or other reasons) do not |
| 144 | * attempt to allocate outside the allowed range. |
| 145 | */ |
Christophe Leroy | bb7f380 | 2016-05-17 09:02:51 +0200 | [diff] [blame] | 146 | if (mapped) |
| 147 | memblock_set_current_limit(mapped); |
Christophe Leroy | a372acf | 2016-02-09 17:07:50 +0100 | [diff] [blame] | 148 | |
Christophe Leroy | eef784b | 2017-07-12 12:08:45 +0200 | [diff] [blame] | 149 | block_mapped_ram = mapped; |
| 150 | |
Christophe Leroy | a372acf | 2016-02-09 17:07:50 +0100 | [diff] [blame] | 151 | return mapped; |
| 152 | } |
Christophe Leroy | 516d918 | 2016-02-09 17:07:54 +0100 | [diff] [blame] | 153 | |
Christophe Leroy | 346bcc4 | 2017-07-12 12:08:55 +0200 | [diff] [blame] | 154 | void __init setup_initial_memory_limit(phys_addr_t first_memblock_base, |
| 155 | phys_addr_t first_memblock_size) |
Christophe Leroy | 516d918 | 2016-02-09 17:07:54 +0100 | [diff] [blame] | 156 | { |
| 157 | /* We don't currently support the first MEMBLOCK not mapping 0 |
| 158 | * physical on those processors |
| 159 | */ |
| 160 | BUG_ON(first_memblock_base != 0); |
| 161 | |
Christophe Leroy | 516d918 | 2016-02-09 17:07:54 +0100 | [diff] [blame] | 162 | /* 8xx can only access 24MB at the moment */ |
| 163 | memblock_set_current_limit(min_t(u64, first_memblock_size, 0x01800000)); |
Christophe Leroy | 516d918 | 2016-02-09 17:07:54 +0100 | [diff] [blame] | 164 | } |
Christophe Leroy | a7761fe | 2016-02-09 17:08:18 +0100 | [diff] [blame] | 165 | |
| 166 | /* |
| 167 | * Set up to use a given MMU context. |
| 168 | * id is context number, pgd is PGD pointer. |
| 169 | * |
| 170 | * We place the physical address of the new task page directory loaded |
| 171 | * into the MMU base register, and set the ASID compare register with |
| 172 | * the new "context." |
| 173 | */ |
| 174 | void set_context(unsigned long id, pgd_t *pgd) |
| 175 | { |
| 176 | s16 offset = (s16)(__pa(swapper_pg_dir)); |
| 177 | |
| 178 | #ifdef CONFIG_BDI_SWITCH |
| 179 | pgd_t **ptr = *(pgd_t ***)(KERNELBASE + 0xf0); |
| 180 | |
| 181 | /* Context switch the PTE pointer for the Abatron BDI2000. |
| 182 | * The PGDIR is passed as second argument. |
| 183 | */ |
| 184 | *(ptr + 1) = pgd; |
| 185 | #endif |
| 186 | |
| 187 | /* Register M_TW will contain base address of level 1 table minus the |
| 188 | * lower part of the kernel PGDIR base address, so that all accesses to |
| 189 | * level 1 table are done relative to lower part of kernel PGDIR base |
| 190 | * address. |
| 191 | */ |
| 192 | mtspr(SPRN_M_TW, __pa(pgd) - offset); |
| 193 | |
| 194 | /* Update context */ |
| 195 | mtspr(SPRN_M_CASID, id); |
| 196 | /* sync */ |
| 197 | mb(); |
| 198 | } |
Christophe Leroy | 766d45c | 2016-02-09 17:08:21 +0100 | [diff] [blame] | 199 | |
| 200 | void flush_instruction_cache(void) |
| 201 | { |
| 202 | isync(); |
| 203 | mtspr(SPRN_IC_CST, IDC_INVALL); |
| 204 | isync(); |
| 205 | } |