1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * This file contains the routines for initializing the MMU
4 * on the 8xx series of chips.
5 * -- christophe
6 *
7 * Derived from arch/powerpc/mm/40x_mmu.c:
8 */
9
10 #include <linux/memblock.h>
11 #include <linux/hugetlb.h>
12
13 #include <mm/mmu_decl.h>
14
15 #define IMMR_SIZE (FIX_IMMR_SIZE << PAGE_SHIFT)
16
17 static unsigned long block_mapped_ram;
18
19 /*
20 * Return PA for this VA if it is in an area mapped with LTLBs or fixmap.
21 * Otherwise, returns 0
22 */
v_block_mapped(unsigned long va)23 phys_addr_t v_block_mapped(unsigned long va)
24 {
25 unsigned long p = PHYS_IMMR_BASE;
26
27 if (va >= VIRT_IMMR_BASE && va < VIRT_IMMR_BASE + IMMR_SIZE)
28 return p + va - VIRT_IMMR_BASE;
29 if (va >= PAGE_OFFSET && va < PAGE_OFFSET + block_mapped_ram)
30 return __pa(va);
31 return 0;
32 }
33
34 /*
35 * Return VA for a given PA mapped with LTLBs or fixmap
36 * Return 0 if not mapped
37 */
p_block_mapped(phys_addr_t pa)38 unsigned long p_block_mapped(phys_addr_t pa)
39 {
40 unsigned long p = PHYS_IMMR_BASE;
41
42 if (pa >= p && pa < p + IMMR_SIZE)
43 return VIRT_IMMR_BASE + pa - p;
44 if (pa < block_mapped_ram)
45 return (unsigned long)__va(pa);
46 return 0;
47 }
48
early_hugepd_alloc_kernel(hugepd_t * pmdp,unsigned long va)49 static pte_t __init *early_hugepd_alloc_kernel(hugepd_t *pmdp, unsigned long va)
50 {
51 if (hpd_val(*pmdp) == 0) {
52 pte_t *ptep = memblock_alloc(sizeof(pte_basic_t), SZ_4K);
53
54 if (!ptep)
55 return NULL;
56
57 hugepd_populate_kernel((hugepd_t *)pmdp, ptep, PAGE_SHIFT_8M);
58 hugepd_populate_kernel((hugepd_t *)pmdp + 1, ptep, PAGE_SHIFT_8M);
59 }
60 return hugepte_offset(*(hugepd_t *)pmdp, va, PGDIR_SHIFT);
61 }
62
__early_map_kernel_hugepage(unsigned long va,phys_addr_t pa,pgprot_t prot,int psize,bool new)63 static int __ref __early_map_kernel_hugepage(unsigned long va, phys_addr_t pa,
64 pgprot_t prot, int psize, bool new)
65 {
66 pmd_t *pmdp = pmd_off_k(va);
67 pte_t *ptep;
68
69 if (WARN_ON(psize != MMU_PAGE_512K && psize != MMU_PAGE_8M))
70 return -EINVAL;
71
72 if (new) {
73 if (WARN_ON(slab_is_available()))
74 return -EINVAL;
75
76 if (psize == MMU_PAGE_512K)
77 ptep = early_pte_alloc_kernel(pmdp, va);
78 else
79 ptep = early_hugepd_alloc_kernel((hugepd_t *)pmdp, va);
80 } else {
81 if (psize == MMU_PAGE_512K)
82 ptep = pte_offset_kernel(pmdp, va);
83 else
84 ptep = hugepte_offset(*(hugepd_t *)pmdp, va, PGDIR_SHIFT);
85 }
86
87 if (WARN_ON(!ptep))
88 return -ENOMEM;
89
90 /* The PTE should never be already present */
91 if (new && WARN_ON(pte_present(*ptep) && pgprot_val(prot)))
92 return -EINVAL;
93
94 set_huge_pte_at(&init_mm, va, ptep, pte_mkhuge(pfn_pte(pa >> PAGE_SHIFT, prot)));
95
96 return 0;
97 }
98
99 /*
100 * MMU_init_hw does the chip-specific initialization of the MMU hardware.
101 */
MMU_init_hw(void)102 void __init MMU_init_hw(void)
103 {
104 }
105
106 static bool immr_is_mapped __initdata;
107
mmu_mapin_immr(void)108 void __init mmu_mapin_immr(void)
109 {
110 if (immr_is_mapped)
111 return;
112
113 immr_is_mapped = true;
114
115 __early_map_kernel_hugepage(VIRT_IMMR_BASE, PHYS_IMMR_BASE,
116 PAGE_KERNEL_NCG, MMU_PAGE_512K, true);
117 }
118
mmu_mapin_ram_chunk(unsigned long offset,unsigned long top,pgprot_t prot,bool new)119 static void mmu_mapin_ram_chunk(unsigned long offset, unsigned long top,
120 pgprot_t prot, bool new)
121 {
122 unsigned long v = PAGE_OFFSET + offset;
123 unsigned long p = offset;
124
125 WARN_ON(!IS_ALIGNED(offset, SZ_512K) || !IS_ALIGNED(top, SZ_512K));
126
127 for (; p < ALIGN(p, SZ_8M) && p < top; p += SZ_512K, v += SZ_512K)
128 __early_map_kernel_hugepage(v, p, prot, MMU_PAGE_512K, new);
129 for (; p < ALIGN_DOWN(top, SZ_8M) && p < top; p += SZ_8M, v += SZ_8M)
130 __early_map_kernel_hugepage(v, p, prot, MMU_PAGE_8M, new);
131 for (; p < ALIGN_DOWN(top, SZ_512K) && p < top; p += SZ_512K, v += SZ_512K)
132 __early_map_kernel_hugepage(v, p, prot, MMU_PAGE_512K, new);
133
134 if (!new)
135 flush_tlb_kernel_range(PAGE_OFFSET + v, PAGE_OFFSET + top);
136 }
137
mmu_mapin_ram(unsigned long base,unsigned long top)138 unsigned long __init mmu_mapin_ram(unsigned long base, unsigned long top)
139 {
140 unsigned long etext8 = ALIGN(__pa(_etext), SZ_8M);
141 unsigned long sinittext = __pa(_sinittext);
142 bool strict_boundary = strict_kernel_rwx_enabled() || debug_pagealloc_enabled_or_kfence();
143 unsigned long boundary = strict_boundary ? sinittext : etext8;
144 unsigned long einittext8 = ALIGN(__pa(_einittext), SZ_8M);
145
146 WARN_ON(top < einittext8);
147
148 mmu_mapin_immr();
149
150 mmu_mapin_ram_chunk(0, boundary, PAGE_KERNEL_TEXT, true);
151 if (debug_pagealloc_enabled_or_kfence()) {
152 top = boundary;
153 } else {
154 mmu_mapin_ram_chunk(boundary, einittext8, PAGE_KERNEL_TEXT, true);
155 mmu_mapin_ram_chunk(einittext8, top, PAGE_KERNEL, true);
156 }
157
158 if (top > SZ_32M)
159 memblock_set_current_limit(top);
160
161 block_mapped_ram = top;
162
163 return top;
164 }
165
mmu_mark_initmem_nx(void)166 void mmu_mark_initmem_nx(void)
167 {
168 unsigned long etext8 = ALIGN(__pa(_etext), SZ_8M);
169 unsigned long sinittext = __pa(_sinittext);
170 unsigned long boundary = strict_kernel_rwx_enabled() ? sinittext : etext8;
171 unsigned long einittext8 = ALIGN(__pa(_einittext), SZ_8M);
172
173 if (!debug_pagealloc_enabled_or_kfence())
174 mmu_mapin_ram_chunk(boundary, einittext8, PAGE_KERNEL, false);
175
176 mmu_pin_tlb(block_mapped_ram, false);
177 }
178
179 #ifdef CONFIG_STRICT_KERNEL_RWX
mmu_mark_rodata_ro(void)180 void mmu_mark_rodata_ro(void)
181 {
182 unsigned long sinittext = __pa(_sinittext);
183
184 mmu_mapin_ram_chunk(0, sinittext, PAGE_KERNEL_ROX, false);
185 if (IS_ENABLED(CONFIG_PIN_TLB_DATA))
186 mmu_pin_tlb(block_mapped_ram, true);
187 }
188 #endif
189
setup_initial_memory_limit(phys_addr_t first_memblock_base,phys_addr_t first_memblock_size)190 void __init setup_initial_memory_limit(phys_addr_t first_memblock_base,
191 phys_addr_t first_memblock_size)
192 {
193 /* We don't currently support the first MEMBLOCK not mapping 0
194 * physical on those processors
195 */
196 BUG_ON(first_memblock_base != 0);
197
198 /* 8xx can only access 32MB at the moment */
199 memblock_set_current_limit(min_t(u64, first_memblock_size, SZ_32M));
200 }
201
pud_clear_huge(pud_t * pud)202 int pud_clear_huge(pud_t *pud)
203 {
204 return 0;
205 }
206
pmd_clear_huge(pmd_t * pmd)207 int pmd_clear_huge(pmd_t *pmd)
208 {
209 return 0;
210 }
211