powerpc/32s: Change mfsrin() into a static inline function
[linux-block.git] / arch / powerpc / mm / book3s32 / mmu.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
14cf11af
PM
2/*
3 * This file contains the routines for handling the MMU on those
4 * PowerPC implementations where the MMU substantially follows the
5 * architecture specification. This includes the 6xx, 7xx, 7xxx,
0f369103 6 * and 8260 implementations but excludes the 8xx and 4xx.
14cf11af
PM
7 * -- paulus
8 *
9 * Derived from arch/ppc/mm/init.c:
10 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
11 *
12 * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
13 * and Cort Dougan (PReP) (cort@cs.nmt.edu)
14 * Copyright (C) 1996 Paul Mackerras
14cf11af
PM
15 *
16 * Derived from "arch/i386/mm/init.c"
17 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
14cf11af
PM
18 */
19
14cf11af
PM
20#include <linux/kernel.h>
21#include <linux/mm.h>
22#include <linux/init.h>
23#include <linux/highmem.h>
95f72d1e 24#include <linux/memblock.h>
14cf11af
PM
25
26#include <asm/prom.h>
27#include <asm/mmu.h>
28#include <asm/machdep.h>
9efc74ff 29#include <asm/code-patching.h>
63b2bc61 30#include <asm/sections.h>
14cf11af 31
9d9f2ccc 32#include <mm/mmu_decl.h>
14cf11af 33
69a1593a
CL
34u8 __initdata early_hash[SZ_256K] __aligned(SZ_256K) = {0};
35
6e980b5c
CL
36static struct hash_pte __initdata *Hash = (struct hash_pte *)early_hash;
37static unsigned long __initdata Hash_size, Hash_mask;
38static unsigned int __initdata hash_mb, hash_mb2;
39unsigned long __initdata _SDR1;
14cf11af 40
316a4058 41struct ppc_bat BATS[8][2]; /* 8 pairs of IBAT, DBAT */
14cf11af 42
03d5b19c 43static struct batrange { /* stores address ranges mapped by BATs */
14cf11af
PM
44 unsigned long start;
45 unsigned long limit;
7c5c4325 46 phys_addr_t phys;
ee0339f2 47} bat_addrs[8];
14cf11af 48
f2655125
CL
49#ifdef CONFIG_SMP
50unsigned long mmu_hash_lock;
51#endif
52
14cf11af
PM
53/*
54 * Return PA for this VA if it is mapped by a BAT, or 0
55 */
3084cdb7 56phys_addr_t v_block_mapped(unsigned long va)
14cf11af
PM
57{
58 int b;
e93ba1b7 59 for (b = 0; b < ARRAY_SIZE(bat_addrs); ++b)
14cf11af
PM
60 if (va >= bat_addrs[b].start && va < bat_addrs[b].limit)
61 return bat_addrs[b].phys + (va - bat_addrs[b].start);
62 return 0;
63}
64
65/*
66 * Return VA for a given PA or 0 if not mapped
67 */
3084cdb7 68unsigned long p_block_mapped(phys_addr_t pa)
14cf11af
PM
69{
70 int b;
e93ba1b7 71 for (b = 0; b < ARRAY_SIZE(bat_addrs); ++b)
14cf11af
PM
72 if (pa >= bat_addrs[b].phys
73 && pa < (bat_addrs[b].limit-bat_addrs[b].start)
74 +bat_addrs[b].phys)
75 return bat_addrs[b].start+(pa-bat_addrs[b].phys);
76 return 0;
77}
78
e4d6654e
CL
79static int find_free_bat(void)
80{
81 int b;
2e38ea48 82 int n = mmu_has_feature(MMU_FTR_USE_HIGH_BATS) ? 8 : 4;
e4d6654e 83
2e38ea48
CL
84 for (b = 0; b < n; b++) {
85 struct ppc_bat *bat = BATS[b];
e4d6654e 86
2e38ea48
CL
87 if (!(bat[1].batu & 3))
88 return b;
e4d6654e
CL
89 }
90 return -1;
91}
92
12f36351
CL
93/*
94 * This function calculates the size of the larger block usable to map the
95 * beginning of an area based on the start address and size of that area:
8b14e1df 96 * - max block size is 256 on 6xx.
12f36351
CL
97 * - base address must be aligned to the block size. So the maximum block size
98 * is identified by the lowest bit set to 1 in the base address (for instance
99 * if base is 0x16000000, max size is 0x02000000).
100 * - block size has to be a power of two. This is calculated by finding the
101 * highest bit set to 1.
102 */
e4d6654e
CL
103static unsigned int block_size(unsigned long base, unsigned long top)
104{
8b14e1df 105 unsigned int max_size = SZ_256M;
12f36351 106 unsigned int base_shift = (ffs(base) - 1) & 31;
e4d6654e
CL
107 unsigned int block_shift = (fls(top - base) - 1) & 31;
108
109 return min3(max_size, 1U << base_shift, 1U << block_shift);
110}
111
5e04ae85
CL
112/*
113 * Set up one of the IBAT (block address translation) register pairs.
114 * The parameters are not checked; in particular size must be a power
115 * of 2 between 128k and 256M.
5e04ae85
CL
116 */
117static void setibat(int index, unsigned long virt, phys_addr_t phys,
118 unsigned int size, pgprot_t prot)
119{
120 unsigned int bl = (size >> 17) - 1;
121 int wimgxpp;
122 struct ppc_bat *bat = BATS[index];
123 unsigned long flags = pgprot_val(prot);
124
125 if (!cpu_has_feature(CPU_FTR_NEED_COHERENT))
126 flags &= ~_PAGE_COHERENT;
127
128 wimgxpp = (flags & _PAGE_COHERENT) | (_PAGE_EXEC ? BPP_RX : BPP_XX);
129 bat[0].batu = virt | (bl << 2) | 2; /* Vs=1, Vp=0 */
130 bat[0].batl = BAT_PHYS_ADDR(phys) | wimgxpp;
131 if (flags & _PAGE_USER)
132 bat[0].batu |= 1; /* Vp = 1 */
133}
134
135static void clearibat(int index)
136{
137 struct ppc_bat *bat = BATS[index];
138
139 bat[0].batu = 0;
140 bat[0].batl = 0;
141}
142
63b2bc61 143static unsigned long __init __mmu_mapin_ram(unsigned long base, unsigned long top)
14cf11af 144{
e4d6654e 145 int idx;
14cf11af 146
e4d6654e
CL
147 while ((idx = find_free_bat()) != -1 && base != top) {
148 unsigned int size = block_size(base, top);
14cf11af 149
e4d6654e 150 if (size < 128 << 10)
14cf11af 151 break;
e4d6654e
CL
152 setbat(idx, PAGE_OFFSET + base, base, size, PAGE_KERNEL_X);
153 base += size;
14cf11af
PM
154 }
155
e4d6654e 156 return base;
14cf11af
PM
157}
158
63b2bc61
CL
159unsigned long __init mmu_mapin_ram(unsigned long base, unsigned long top)
160{
12f36351 161 unsigned long done;
63b2bc61
CL
162 unsigned long border = (unsigned long)__init_begin - PAGE_OFFSET;
163
035b19a1
CL
164
165 if (debug_pagealloc_enabled() || __map_without_bats) {
166 pr_debug_once("Read-Write memory mapped without BATs\n");
2b279c03
CL
167 if (base >= border)
168 return base;
169 if (top >= border)
170 top = border;
171 }
63b2bc61
CL
172
173 if (!strict_kernel_rwx_enabled() || base >= border || top <= border)
174 return __mmu_mapin_ram(base, top);
175
176 done = __mmu_mapin_ram(base, border);
12f36351 177 if (done != border)
63b2bc61
CL
178 return done;
179
12f36351 180 return __mmu_mapin_ram(border, top);
63b2bc61
CL
181}
182
c4964331
CL
183static bool is_module_segment(unsigned long addr)
184{
185 if (!IS_ENABLED(CONFIG_MODULES))
186 return false;
7bee31ad
CL
187#ifdef MODULES_VADDR
188 if (addr < ALIGN_DOWN(MODULES_VADDR, SZ_256M))
189 return false;
541cebb5 190 if (addr > ALIGN(MODULES_END, SZ_256M) - 1)
7bee31ad
CL
191 return false;
192#else
c4964331
CL
193 if (addr < ALIGN_DOWN(VMALLOC_START, SZ_256M))
194 return false;
541cebb5 195 if (addr > ALIGN(VMALLOC_END, SZ_256M) - 1)
c4964331 196 return false;
7bee31ad 197#endif
c4964331
CL
198 return true;
199}
200
63b2bc61
CL
201void mmu_mark_initmem_nx(void)
202{
203 int nb = mmu_has_feature(MMU_FTR_USE_HIGH_BATS) ? 8 : 4;
204 int i;
205 unsigned long base = (unsigned long)_stext - PAGE_OFFSET;
206 unsigned long top = (unsigned long)_etext - PAGE_OFFSET;
4b19f96a 207 unsigned long border = (unsigned long)__init_begin - PAGE_OFFSET;
63b2bc61
CL
208 unsigned long size;
209
63b2bc61
CL
210 for (i = 0; i < nb - 1 && base < top && top - base > (128 << 10);) {
211 size = block_size(base, top);
212 setibat(i++, PAGE_OFFSET + base, base, size, PAGE_KERNEL_TEXT);
213 base += size;
214 }
215 if (base < top) {
216 size = block_size(base, top);
217 size = max(size, 128UL << 10);
218 if ((top - base) > size) {
63b2bc61 219 size <<= 1;
4b19f96a
CL
220 if (strict_kernel_rwx_enabled() && base + size > border)
221 pr_warn("Some RW data is getting mapped X. "
222 "Adjust CONFIG_DATA_SHIFT to avoid that.\n");
63b2bc61
CL
223 }
224 setibat(i++, PAGE_OFFSET + base, base, size, PAGE_KERNEL_TEXT);
225 base += size;
226 }
227 for (; i < nb; i++)
228 clearibat(i);
229
230 update_bats();
231
232 for (i = TASK_SIZE >> 28; i < 16; i++) {
233 /* Do not set NX on VM space for modules */
c4964331
CL
234 if (is_module_segment(i << 28))
235 continue;
236
63b2bc61
CL
237 mtsrin(mfsrin(i << 28) | 0x10000000, i << 28);
238 }
239}
240
241void mmu_mark_rodata_ro(void)
242{
243 int nb = mmu_has_feature(MMU_FTR_USE_HIGH_BATS) ? 8 : 4;
244 int i;
245
63b2bc61
CL
246 for (i = 0; i < nb; i++) {
247 struct ppc_bat *bat = BATS[i];
248
249 if (bat_addrs[i].start < (unsigned long)__init_begin)
250 bat[1].batl = (bat[1].batl & ~BPP_RW) | BPP_RX;
251 }
252
253 update_bats();
254}
255
14cf11af
PM
256/*
257 * Set up one of the I/D BAT (block address translation) register pairs.
258 * The parameters are not checked; in particular size must be a power
259 * of 2 between 128k and 256M.
df25f863 260 * On 603+, only set IBAT when _PAGE_EXEC is set
14cf11af 261 */
7c5c4325 262void __init setbat(int index, unsigned long virt, phys_addr_t phys,
5dd4e4f6 263 unsigned int size, pgprot_t prot)
14cf11af
PM
264{
265 unsigned int bl;
266 int wimgxpp;
cbcaff7d 267 struct ppc_bat *bat;
5dd4e4f6 268 unsigned long flags = pgprot_val(prot);
14cf11af 269
cbcaff7d
CL
270 if (index == -1)
271 index = find_free_bat();
272 if (index == -1) {
273 pr_err("%s: no BAT available for mapping 0x%llx\n", __func__,
274 (unsigned long long)phys);
275 return;
276 }
277 bat = BATS[index];
278
4c456a67
GP
279 if ((flags & _PAGE_NO_CACHE) ||
280 (cpu_has_feature(CPU_FTR_NEED_COHERENT) == 0))
281 flags &= ~_PAGE_COHERENT;
14cf11af
PM
282
283 bl = (size >> 17) - 1;
2e38ea48
CL
284 /* Do DBAT first */
285 wimgxpp = flags & (_PAGE_WRITETHRU | _PAGE_NO_CACHE
286 | _PAGE_COHERENT | _PAGE_GUARDED);
287 wimgxpp |= (flags & _PAGE_RW)? BPP_RW: BPP_RX;
288 bat[1].batu = virt | (bl << 2) | 2; /* Vs=1, Vp=0 */
289 bat[1].batl = BAT_PHYS_ADDR(phys) | wimgxpp;
290 if (flags & _PAGE_USER)
291 bat[1].batu |= 1; /* Vp = 1 */
292 if (flags & _PAGE_GUARDED) {
293 /* G bit must be zero in IBATs */
294 flags &= ~_PAGE_EXEC;
14cf11af 295 }
2e38ea48
CL
296 if (flags & _PAGE_EXEC)
297 bat[0] = bat[1];
298 else
299 bat[0].batu = bat[0].batl = 0;
14cf11af
PM
300
301 bat_addrs[index].start = virt;
302 bat_addrs[index].limit = virt + ((bl + 1) << 17) - 1;
303 bat_addrs[index].phys = phys;
304}
305
3c726f8d
BH
306/*
307 * Preload a translation in the hash table
308 */
79d1befe 309static void hash_preload(struct mm_struct *mm, unsigned long ea)
3c726f8d
BH
310{
311 pmd_t *pmd;
312
4cc445b4 313 if (!mmu_has_feature(MMU_FTR_HPTE_TABLE))
3c726f8d 314 return;
e05c7b1f 315 pmd = pmd_off(mm, ea);
3c726f8d 316 if (!pmd_none(*pmd))
6218a761 317 add_hash_page(mm->context.id, ea, pmd_val(*pmd));
3c726f8d
BH
318}
319
e5a1edb9
CL
320/*
321 * This is called at the end of handling a user page fault, when the
322 * fault has been handled by updating a PTE in the linux page tables.
323 * We use it to preload an HPTE into the hash table corresponding to
324 * the updated linux PTE.
325 *
326 * This must always be called with the pte lock held.
327 */
328void update_mmu_cache(struct vm_area_struct *vma, unsigned long address,
329 pte_t *ptep)
330{
f204338f
CL
331 if (!mmu_has_feature(MMU_FTR_HPTE_TABLE))
332 return;
e5a1edb9
CL
333 /*
334 * We don't need to worry about _PAGE_PRESENT here because we are
335 * called with either mm->page_table_lock held or ptl lock held
336 */
e5a1edb9
CL
337
338 /* We only want HPTEs for linux PTEs that have _PAGE_ACCESSED set */
339 if (!pte_young(*ptep) || address >= TASK_SIZE)
340 return;
341
f49f4e2b
CL
342 /* We have to test for regs NULL since init will get here first thing at boot */
343 if (!current->thread.regs)
344 return;
e5a1edb9 345
f49f4e2b
CL
346 /* We also avoid filling the hash if not coming from a fault */
347 if (TRAP(current->thread.regs) != 0x300 && TRAP(current->thread.regs) != 0x400)
e5a1edb9 348 return;
e5a1edb9 349
f49f4e2b 350 hash_preload(vma->vm_mm, address);
e5a1edb9
CL
351}
352
14cf11af
PM
353/*
354 * Initialize the hash table and patch the instructions in hashtable.S.
355 */
356void __init MMU_init_hw(void)
357{
14cf11af
PM
358 unsigned int n_hpteg, lg_n_hpteg;
359
4a3a224c 360 if (!mmu_has_feature(MMU_FTR_HPTE_TABLE))
14cf11af 361 return;
14cf11af
PM
362
363 if ( ppc_md.progress ) ppc_md.progress("hash:enter", 0x105);
364
14cf11af
PM
365#define LG_HPTEG_SIZE 6 /* 64 bytes per HPTEG */
366#define SDR1_LOW_BITS ((n_hpteg - 1) >> 10)
367#define MIN_N_HPTEG 1024 /* min 64kB hash table */
14cf11af 368
14cf11af
PM
369 /*
370 * Allow 1 HPTE (1/8 HPTEG) for each page of memory.
371 * This is less than the recommended amount, but then
372 * Linux ain't AIX.
373 */
374 n_hpteg = total_memory / (PAGE_SIZE * 8);
375 if (n_hpteg < MIN_N_HPTEG)
376 n_hpteg = MIN_N_HPTEG;
377 lg_n_hpteg = __ilog2(n_hpteg);
378 if (n_hpteg & (n_hpteg - 1)) {
379 ++lg_n_hpteg; /* round up if not power of 2 */
380 n_hpteg = 1 << lg_n_hpteg;
381 }
382 Hash_size = n_hpteg << LG_HPTEG_SIZE;
383
384 /*
385 * Find some memory for the hash table.
386 */
387 if ( ppc_md.progress ) ppc_md.progress("hash:find piece", 0x322);
b63a07d6 388 Hash = memblock_alloc(Hash_size, Hash_size);
8a7f97b9
MR
389 if (!Hash)
390 panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
391 __func__, Hash_size, Hash_size);
14cf11af 392 _SDR1 = __pa(Hash) | SDR1_LOW_BITS;
14cf11af 393
8f156c23
CL
394 pr_info("Total memory = %lldMB; using %ldkB for hash table\n",
395 (unsigned long long)(total_memory >> 20), Hash_size >> 10);
14cf11af 396
14cf11af 397
14cf11af 398 Hash_mask = n_hpteg - 1;
72f208c6 399 hash_mb2 = hash_mb = 32 - LG_HPTEG_SIZE - lg_n_hpteg;
14cf11af 400 if (lg_n_hpteg > 16)
72f208c6
CL
401 hash_mb2 = 16 - LG_HPTEG_SIZE;
402}
403
404void __init MMU_init_hw_patch(void)
405{
406 unsigned int hmask = Hash_mask >> (16 - LG_HPTEG_SIZE);
232ca1ee 407 unsigned int hash = (unsigned int)Hash - PAGE_OFFSET;
14cf11af 408
69a1593a
CL
409 if (!mmu_has_feature(MMU_FTR_HPTE_TABLE))
410 return;
411
72f208c6
CL
412 if (ppc_md.progress)
413 ppc_md.progress("hash:patch", 0x345);
414 if (ppc_md.progress)
415 ppc_md.progress("hash:done", 0x205);
416
417 /* WARNING: Make sure nothing can trigger a KASAN check past this point */
14cf11af
PM
418
419 /*
420 * Patch up the instructions in hashtable.S:create_hpte
421 */
cd08f109 422 modify_instruction_site(&patch__hash_page_A0, 0xffff, hash >> 16);
72f208c6
CL
423 modify_instruction_site(&patch__hash_page_A1, 0x7c0, hash_mb << 6);
424 modify_instruction_site(&patch__hash_page_A2, 0x7c0, hash_mb2 << 6);
9efc74ff
CL
425 modify_instruction_site(&patch__hash_page_B, 0xffff, hmask);
426 modify_instruction_site(&patch__hash_page_C, 0xffff, hmask);
14cf11af
PM
427
428 /*
429 * Patch up the instructions in hashtable.S:flush_hash_page
430 */
232ca1ee 431 modify_instruction_site(&patch__flush_hash_A0, 0xffff, hash >> 16);
72f208c6
CL
432 modify_instruction_site(&patch__flush_hash_A1, 0x7c0, hash_mb << 6);
433 modify_instruction_site(&patch__flush_hash_A2, 0x7c0, hash_mb2 << 6);
9efc74ff 434 modify_instruction_site(&patch__flush_hash_B, 0xffff, hmask);
14cf11af 435}
cd3db0c4
BH
436
437void setup_initial_memory_limit(phys_addr_t first_memblock_base,
438 phys_addr_t first_memblock_size)
439{
440 /* We don't currently support the first MEMBLOCK not mapping 0
441 * physical on those processors
442 */
443 BUG_ON(first_memblock_base != 0);
444
8b14e1df 445 memblock_set_current_limit(min_t(u64, first_memblock_size, SZ_256M));
cd3db0c4 446}
31ed2b13 447
e4dccf90
CL
448void __init print_system_hash_info(void)
449{
450 pr_info("Hash_size = 0x%lx\n", Hash_size);
451 if (Hash_mask)
452 pr_info("Hash_mask = 0x%lx\n", Hash_mask);
453}
454
31ed2b13
CL
455#ifdef CONFIG_PPC_KUEP
456void __init setup_kuep(bool disabled)
457{
458 pr_info("Activating Kernel Userspace Execution Prevention\n");
459
31ed2b13
CL
460 if (disabled)
461 pr_warn("KUEP cannot be disabled yet on 6xx when compiled in\n");
462}
463#endif
a68c31fc
CL
464
465#ifdef CONFIG_PPC_KUAP
466void __init setup_kuap(bool disabled)
467{
468 pr_info("Activating Kernel Userspace Access Protection\n");
469
470 if (disabled)
471 pr_warn("KUAP cannot be disabled yet on 6xx when compiled in\n");
472}
473#endif
068fdba1
CL
474
475void __init early_init_mmu(void)
476{
477}