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