blob: 03b6ba54460e2e67afc91fe66c2cdb65ccce5c99 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002/*
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,
Michael Ellerman0f369102014-07-10 12:29:24 +10006 * and 8260 implementations but excludes the 8xx and 4xx.
Paul Mackerras14cf11a2005-09-26 16:04:21 +10007 * -- 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
Paul Mackerras14cf11a2005-09-26 16:04:21 +100015 *
16 * Derived from "arch/i386/mm/init.c"
17 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
Paul Mackerras14cf11a2005-09-26 16:04:21 +100018 */
19
Paul Mackerras14cf11a2005-09-26 16:04:21 +100020#include <linux/kernel.h>
21#include <linux/mm.h>
22#include <linux/init.h>
23#include <linux/highmem.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100024#include <linux/memblock.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100025
26#include <asm/prom.h>
27#include <asm/mmu.h>
28#include <asm/machdep.h>
Christophe Leroy9efc74f2018-11-09 17:33:24 +000029#include <asm/code-patching.h>
Christophe Leroy63b2bc62019-02-21 19:08:49 +000030#include <asm/sections.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100031
Christophe Leroy9d9f2cc2019-03-29 09:59:59 +000032#include <mm/mmu_decl.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100033
Christophe Leroy57e04912019-04-26 16:36:36 +000034struct hash_pte *Hash;
Christophe Leroye4dccf92019-04-26 16:36:39 +000035static unsigned long Hash_size, Hash_mask;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100036unsigned long _SDR1;
Christophe Leroy72f208c2019-04-26 16:23:35 +000037static unsigned int hash_mb, hash_mb2;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100038
Becky Bruce316a4052008-06-14 09:41:43 +100039struct ppc_bat BATS[8][2]; /* 8 pairs of IBAT, DBAT */
Paul Mackerras14cf11a2005-09-26 16:04:21 +100040
41struct batrange { /* stores address ranges mapped by BATs */
42 unsigned long start;
43 unsigned long limit;
Becky Bruce7c5c4322008-06-14 09:41:42 +100044 phys_addr_t phys;
Jon Loeligeree0339f2006-06-17 17:52:44 -050045} bat_addrs[8];
Paul Mackerras14cf11a2005-09-26 16:04:21 +100046
47/*
48 * Return PA for this VA if it is mapped by a BAT, or 0
49 */
Christophe Leroy3084cdb2016-02-09 17:07:58 +010050phys_addr_t v_block_mapped(unsigned long va)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100051{
52 int b;
Christophe Leroye93ba1b2018-11-16 17:27:42 +000053 for (b = 0; b < ARRAY_SIZE(bat_addrs); ++b)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100054 if (va >= bat_addrs[b].start && va < bat_addrs[b].limit)
55 return bat_addrs[b].phys + (va - bat_addrs[b].start);
56 return 0;
57}
58
59/*
60 * Return VA for a given PA or 0 if not mapped
61 */
Christophe Leroy3084cdb2016-02-09 17:07:58 +010062unsigned long p_block_mapped(phys_addr_t pa)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100063{
64 int b;
Christophe Leroye93ba1b2018-11-16 17:27:42 +000065 for (b = 0; b < ARRAY_SIZE(bat_addrs); ++b)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100066 if (pa >= bat_addrs[b].phys
67 && pa < (bat_addrs[b].limit-bat_addrs[b].start)
68 +bat_addrs[b].phys)
69 return bat_addrs[b].start+(pa-bat_addrs[b].phys);
70 return 0;
71}
72
Christophe Leroye4d66542019-02-21 19:08:39 +000073static int find_free_bat(void)
74{
75 int b;
76
Christophe Leroy12c3f1f2019-08-26 15:52:14 +000077 if (IS_ENABLED(CONFIG_PPC_BOOK3S_601)) {
Christophe Leroye4d66542019-02-21 19:08:39 +000078 for (b = 0; b < 4; b++) {
79 struct ppc_bat *bat = BATS[b];
80
81 if (!(bat[0].batl & 0x40))
82 return b;
83 }
84 } else {
85 int n = mmu_has_feature(MMU_FTR_USE_HIGH_BATS) ? 8 : 4;
86
87 for (b = 0; b < n; b++) {
88 struct ppc_bat *bat = BATS[b];
89
90 if (!(bat[1].batu & 3))
91 return b;
92 }
93 }
94 return -1;
95}
96
Christophe Leroy12f36352019-04-30 16:11:59 +000097/*
98 * This function calculates the size of the larger block usable to map the
99 * beginning of an area based on the start address and size of that area:
100 * - max block size is 8M on 601 and 256 on other 6xx.
101 * - base address must be aligned to the block size. So the maximum block size
102 * is identified by the lowest bit set to 1 in the base address (for instance
103 * if base is 0x16000000, max size is 0x02000000).
104 * - block size has to be a power of two. This is calculated by finding the
105 * highest bit set to 1.
106 */
Christophe Leroye4d66542019-02-21 19:08:39 +0000107static unsigned int block_size(unsigned long base, unsigned long top)
108{
Christophe Leroy12c3f1f2019-08-26 15:52:14 +0000109 unsigned int max_size = IS_ENABLED(CONFIG_PPC_BOOK3S_601) ? SZ_8M : SZ_256M;
Christophe Leroy12f36352019-04-30 16:11:59 +0000110 unsigned int base_shift = (ffs(base) - 1) & 31;
Christophe Leroye4d66542019-02-21 19:08:39 +0000111 unsigned int block_shift = (fls(top - base) - 1) & 31;
112
113 return min3(max_size, 1U << base_shift, 1U << block_shift);
114}
115
Christophe Leroy5e04ae82019-02-21 19:08:48 +0000116/*
117 * Set up one of the IBAT (block address translation) register pairs.
118 * The parameters are not checked; in particular size must be a power
119 * of 2 between 128k and 256M.
120 * Only for 603+ ...
121 */
122static void setibat(int index, unsigned long virt, phys_addr_t phys,
123 unsigned int size, pgprot_t prot)
124{
125 unsigned int bl = (size >> 17) - 1;
126 int wimgxpp;
127 struct ppc_bat *bat = BATS[index];
128 unsigned long flags = pgprot_val(prot);
129
130 if (!cpu_has_feature(CPU_FTR_NEED_COHERENT))
131 flags &= ~_PAGE_COHERENT;
132
133 wimgxpp = (flags & _PAGE_COHERENT) | (_PAGE_EXEC ? BPP_RX : BPP_XX);
134 bat[0].batu = virt | (bl << 2) | 2; /* Vs=1, Vp=0 */
135 bat[0].batl = BAT_PHYS_ADDR(phys) | wimgxpp;
136 if (flags & _PAGE_USER)
137 bat[0].batu |= 1; /* Vp = 1 */
138}
139
140static void clearibat(int index)
141{
142 struct ppc_bat *bat = BATS[index];
143
144 bat[0].batu = 0;
145 bat[0].batl = 0;
146}
147
Christophe Leroy63b2bc62019-02-21 19:08:49 +0000148static unsigned long __init __mmu_mapin_ram(unsigned long base, unsigned long top)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000149{
Christophe Leroye4d66542019-02-21 19:08:39 +0000150 int idx;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000151
Christophe Leroye4d66542019-02-21 19:08:39 +0000152 while ((idx = find_free_bat()) != -1 && base != top) {
153 unsigned int size = block_size(base, top);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000154
Christophe Leroye4d66542019-02-21 19:08:39 +0000155 if (size < 128 << 10)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000156 break;
Christophe Leroye4d66542019-02-21 19:08:39 +0000157 setbat(idx, PAGE_OFFSET + base, base, size, PAGE_KERNEL_X);
158 base += size;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000159 }
160
Christophe Leroye4d66542019-02-21 19:08:39 +0000161 return base;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000162}
163
Christophe Leroy63b2bc62019-02-21 19:08:49 +0000164unsigned long __init mmu_mapin_ram(unsigned long base, unsigned long top)
165{
Christophe Leroy12f36352019-04-30 16:11:59 +0000166 unsigned long done;
Christophe Leroy63b2bc62019-02-21 19:08:49 +0000167 unsigned long border = (unsigned long)__init_begin - PAGE_OFFSET;
168
169 if (__map_without_bats) {
170 pr_debug("RAM mapped without BATs\n");
171 return base;
172 }
Christophe Leroy2b279c02020-05-19 05:49:28 +0000173 if (debug_pagealloc_enabled()) {
174 if (base >= border)
175 return base;
176 if (top >= border)
177 top = border;
178 }
Christophe Leroy63b2bc62019-02-21 19:08:49 +0000179
180 if (!strict_kernel_rwx_enabled() || base >= border || top <= border)
181 return __mmu_mapin_ram(base, top);
182
183 done = __mmu_mapin_ram(base, border);
Christophe Leroy12f36352019-04-30 16:11:59 +0000184 if (done != border)
Christophe Leroy63b2bc62019-02-21 19:08:49 +0000185 return done;
186
Christophe Leroy12f36352019-04-30 16:11:59 +0000187 return __mmu_mapin_ram(border, top);
Christophe Leroy63b2bc62019-02-21 19:08:49 +0000188}
189
190void mmu_mark_initmem_nx(void)
191{
192 int nb = mmu_has_feature(MMU_FTR_USE_HIGH_BATS) ? 8 : 4;
193 int i;
194 unsigned long base = (unsigned long)_stext - PAGE_OFFSET;
195 unsigned long top = (unsigned long)_etext - PAGE_OFFSET;
Christophe Leroy4b19f962020-05-19 05:48:56 +0000196 unsigned long border = (unsigned long)__init_begin - PAGE_OFFSET;
Christophe Leroy63b2bc62019-02-21 19:08:49 +0000197 unsigned long size;
198
Christophe Leroy12c3f1f2019-08-26 15:52:14 +0000199 if (IS_ENABLED(CONFIG_PPC_BOOK3S_601))
Christophe Leroy63b2bc62019-02-21 19:08:49 +0000200 return;
201
202 for (i = 0; i < nb - 1 && base < top && top - base > (128 << 10);) {
203 size = block_size(base, top);
204 setibat(i++, PAGE_OFFSET + base, base, size, PAGE_KERNEL_TEXT);
205 base += size;
206 }
207 if (base < top) {
208 size = block_size(base, top);
209 size = max(size, 128UL << 10);
210 if ((top - base) > size) {
Christophe Leroy63b2bc62019-02-21 19:08:49 +0000211 size <<= 1;
Christophe Leroy4b19f962020-05-19 05:48:56 +0000212 if (strict_kernel_rwx_enabled() && base + size > border)
213 pr_warn("Some RW data is getting mapped X. "
214 "Adjust CONFIG_DATA_SHIFT to avoid that.\n");
Christophe Leroy63b2bc62019-02-21 19:08:49 +0000215 }
216 setibat(i++, PAGE_OFFSET + base, base, size, PAGE_KERNEL_TEXT);
217 base += size;
218 }
219 for (; i < nb; i++)
220 clearibat(i);
221
222 update_bats();
223
224 for (i = TASK_SIZE >> 28; i < 16; i++) {
225 /* Do not set NX on VM space for modules */
226 if (IS_ENABLED(CONFIG_MODULES) &&
227 (VMALLOC_START & 0xf0000000) == i << 28)
228 break;
229 mtsrin(mfsrin(i << 28) | 0x10000000, i << 28);
230 }
231}
232
233void mmu_mark_rodata_ro(void)
234{
235 int nb = mmu_has_feature(MMU_FTR_USE_HIGH_BATS) ? 8 : 4;
236 int i;
237
Christophe Leroy12c3f1f2019-08-26 15:52:14 +0000238 if (IS_ENABLED(CONFIG_PPC_BOOK3S_601))
Christophe Leroy63b2bc62019-02-21 19:08:49 +0000239 return;
240
241 for (i = 0; i < nb; i++) {
242 struct ppc_bat *bat = BATS[i];
243
244 if (bat_addrs[i].start < (unsigned long)__init_begin)
245 bat[1].batl = (bat[1].batl & ~BPP_RW) | BPP_RX;
246 }
247
248 update_bats();
249}
250
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000251/*
252 * Set up one of the I/D BAT (block address translation) register pairs.
253 * The parameters are not checked; in particular size must be a power
254 * of 2 between 128k and 256M.
Christophe Leroydf25f862019-02-21 19:08:43 +0000255 * On 603+, only set IBAT when _PAGE_EXEC is set
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000256 */
Becky Bruce7c5c4322008-06-14 09:41:42 +1000257void __init setbat(int index, unsigned long virt, phys_addr_t phys,
Michael Ellerman5dd4e4f2015-03-25 20:11:55 +1100258 unsigned int size, pgprot_t prot)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000259{
260 unsigned int bl;
261 int wimgxpp;
Christophe Leroycbcaff72019-09-16 20:25:39 +0000262 struct ppc_bat *bat;
Michael Ellerman5dd4e4f2015-03-25 20:11:55 +1100263 unsigned long flags = pgprot_val(prot);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000264
Christophe Leroycbcaff72019-09-16 20:25:39 +0000265 if (index == -1)
266 index = find_free_bat();
267 if (index == -1) {
268 pr_err("%s: no BAT available for mapping 0x%llx\n", __func__,
269 (unsigned long long)phys);
270 return;
271 }
272 bat = BATS[index];
273
Gerhard Pircher4c456a62009-01-23 06:51:28 +0000274 if ((flags & _PAGE_NO_CACHE) ||
275 (cpu_has_feature(CPU_FTR_NEED_COHERENT) == 0))
276 flags &= ~_PAGE_COHERENT;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000277
278 bl = (size >> 17) - 1;
Christophe Leroy12c3f1f2019-08-26 15:52:14 +0000279 if (!IS_ENABLED(CONFIG_PPC_BOOK3S_601)) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000280 /* 603, 604, etc. */
281 /* Do DBAT first */
282 wimgxpp = flags & (_PAGE_WRITETHRU | _PAGE_NO_CACHE
283 | _PAGE_COHERENT | _PAGE_GUARDED);
284 wimgxpp |= (flags & _PAGE_RW)? BPP_RW: BPP_RX;
Becky Bruce316a4052008-06-14 09:41:43 +1000285 bat[1].batu = virt | (bl << 2) | 2; /* Vs=1, Vp=0 */
286 bat[1].batl = BAT_PHYS_ADDR(phys) | wimgxpp;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000287 if (flags & _PAGE_USER)
Becky Bruce316a4052008-06-14 09:41:43 +1000288 bat[1].batu |= 1; /* Vp = 1 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000289 if (flags & _PAGE_GUARDED) {
290 /* G bit must be zero in IBATs */
Christophe Leroydf25f862019-02-21 19:08:43 +0000291 flags &= ~_PAGE_EXEC;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000292 }
Christophe Leroydf25f862019-02-21 19:08:43 +0000293 if (flags & _PAGE_EXEC)
294 bat[0] = bat[1];
295 else
296 bat[0].batu = bat[0].batl = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000297 } else {
298 /* 601 cpu */
299 if (bl > BL_8M)
300 bl = BL_8M;
301 wimgxpp = flags & (_PAGE_WRITETHRU | _PAGE_NO_CACHE
302 | _PAGE_COHERENT);
303 wimgxpp |= (flags & _PAGE_RW)?
304 ((flags & _PAGE_USER)? PP_RWRW: PP_RWXX): PP_RXRX;
Becky Bruce316a4052008-06-14 09:41:43 +1000305 bat->batu = virt | wimgxpp | 4; /* Ks=0, Ku=1 */
306 bat->batl = phys | bl | 0x40; /* V=1 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000307 }
308
309 bat_addrs[index].start = virt;
310 bat_addrs[index].limit = virt + ((bl + 1) << 17) - 1;
311 bat_addrs[index].phys = phys;
312}
313
314/*
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100315 * Preload a translation in the hash table
316 */
Christophe Leroyf49f4e22019-08-16 05:41:43 +0000317void hash_preload(struct mm_struct *mm, unsigned long ea)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100318{
319 pmd_t *pmd;
320
Mathieu Malaterred8731522018-04-13 20:41:43 +0200321 if (!Hash)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100322 return;
Mike Rapoporte05c7b12020-06-08 21:33:05 -0700323 pmd = pmd_off(mm, ea);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100324 if (!pmd_none(*pmd))
Paul Mackerras6218a762006-06-11 14:15:17 +1000325 add_hash_page(mm->context.id, ea, pmd_val(*pmd));
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100326}
327
328/*
Christophe Leroye5a1edb2019-08-16 05:41:42 +0000329 * This is called at the end of handling a user page fault, when the
330 * fault has been handled by updating a PTE in the linux page tables.
331 * We use it to preload an HPTE into the hash table corresponding to
332 * the updated linux PTE.
333 *
334 * This must always be called with the pte lock held.
335 */
336void update_mmu_cache(struct vm_area_struct *vma, unsigned long address,
337 pte_t *ptep)
338{
Christophe Leroyf2043382019-08-16 05:41:44 +0000339 if (!mmu_has_feature(MMU_FTR_HPTE_TABLE))
340 return;
Christophe Leroye5a1edb2019-08-16 05:41:42 +0000341 /*
342 * We don't need to worry about _PAGE_PRESENT here because we are
343 * called with either mm->page_table_lock held or ptl lock held
344 */
Christophe Leroye5a1edb2019-08-16 05:41:42 +0000345
346 /* We only want HPTEs for linux PTEs that have _PAGE_ACCESSED set */
347 if (!pte_young(*ptep) || address >= TASK_SIZE)
348 return;
349
Christophe Leroyf49f4e22019-08-16 05:41:43 +0000350 /* We have to test for regs NULL since init will get here first thing at boot */
351 if (!current->thread.regs)
Christophe Leroye5a1edb2019-08-16 05:41:42 +0000352 return;
Christophe Leroye5a1edb2019-08-16 05:41:42 +0000353
Christophe Leroyf49f4e22019-08-16 05:41:43 +0000354 /* We also avoid filling the hash if not coming from a fault */
355 if (TRAP(current->thread.regs) != 0x300 && TRAP(current->thread.regs) != 0x400)
356 return;
357
358 hash_preload(vma->vm_mm, address);
Christophe Leroye5a1edb2019-08-16 05:41:42 +0000359}
360
361/*
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000362 * Initialize the hash table and patch the instructions in hashtable.S.
363 */
364void __init MMU_init_hw(void)
365{
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000366 unsigned int n_hpteg, lg_n_hpteg;
367
Christophe Leroy4a3a2242018-11-09 17:33:22 +0000368 if (!mmu_has_feature(MMU_FTR_HPTE_TABLE))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000369 return;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000370
371 if ( ppc_md.progress ) ppc_md.progress("hash:enter", 0x105);
372
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000373#define LG_HPTEG_SIZE 6 /* 64 bytes per HPTEG */
374#define SDR1_LOW_BITS ((n_hpteg - 1) >> 10)
375#define MIN_N_HPTEG 1024 /* min 64kB hash table */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000376
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000377 /*
378 * Allow 1 HPTE (1/8 HPTEG) for each page of memory.
379 * This is less than the recommended amount, but then
380 * Linux ain't AIX.
381 */
382 n_hpteg = total_memory / (PAGE_SIZE * 8);
383 if (n_hpteg < MIN_N_HPTEG)
384 n_hpteg = MIN_N_HPTEG;
385 lg_n_hpteg = __ilog2(n_hpteg);
386 if (n_hpteg & (n_hpteg - 1)) {
387 ++lg_n_hpteg; /* round up if not power of 2 */
388 n_hpteg = 1 << lg_n_hpteg;
389 }
390 Hash_size = n_hpteg << LG_HPTEG_SIZE;
391
392 /*
393 * Find some memory for the hash table.
394 */
395 if ( ppc_md.progress ) ppc_md.progress("hash:find piece", 0x322);
Mike Rapoportb63a07d2019-03-07 16:31:06 -0800396 Hash = memblock_alloc(Hash_size, Hash_size);
Mike Rapoport8a7f97b2019-03-11 23:30:31 -0700397 if (!Hash)
398 panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
399 __func__, Hash_size, Hash_size);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000400 _SDR1 = __pa(Hash) | SDR1_LOW_BITS;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000401
Christophe Leroy8f156c22019-04-26 16:36:37 +0000402 pr_info("Total memory = %lldMB; using %ldkB for hash table\n",
403 (unsigned long long)(total_memory >> 20), Hash_size >> 10);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000404
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000405
Christophe Leroy72f208c2019-04-26 16:23:35 +0000406 Hash_mask = n_hpteg - 1;
407 hash_mb2 = hash_mb = 32 - LG_HPTEG_SIZE - lg_n_hpteg;
408 if (lg_n_hpteg > 16)
409 hash_mb2 = 16 - LG_HPTEG_SIZE;
Christophe Leroy9d6d7122019-08-14 10:02:20 +0000410
411 /*
412 * When KASAN is selected, there is already an early temporary hash
413 * table and the switch to the final hash table is done later.
414 */
415 if (IS_ENABLED(CONFIG_KASAN))
416 return;
417
418 MMU_init_hw_patch();
Christophe Leroy72f208c2019-04-26 16:23:35 +0000419}
420
421void __init MMU_init_hw_patch(void)
422{
423 unsigned int hmask = Hash_mask >> (16 - LG_HPTEG_SIZE);
Christophe Leroy232ca1e2020-02-15 10:14:25 +0000424 unsigned int hash = (unsigned int)Hash - PAGE_OFFSET;
Christophe Leroy72f208c2019-04-26 16:23:35 +0000425
426 if (ppc_md.progress)
427 ppc_md.progress("hash:patch", 0x345);
428 if (ppc_md.progress)
429 ppc_md.progress("hash:done", 0x205);
430
431 /* WARNING: Make sure nothing can trigger a KASAN check past this point */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000432
433 /*
434 * Patch up the instructions in hashtable.S:create_hpte
435 */
Christophe Leroycd08f102019-12-21 08:32:38 +0000436 modify_instruction_site(&patch__hash_page_A0, 0xffff, hash >> 16);
Christophe Leroy72f208c2019-04-26 16:23:35 +0000437 modify_instruction_site(&patch__hash_page_A1, 0x7c0, hash_mb << 6);
438 modify_instruction_site(&patch__hash_page_A2, 0x7c0, hash_mb2 << 6);
Christophe Leroy9efc74f2018-11-09 17:33:24 +0000439 modify_instruction_site(&patch__hash_page_B, 0xffff, hmask);
440 modify_instruction_site(&patch__hash_page_C, 0xffff, hmask);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000441
442 /*
443 * Patch up the instructions in hashtable.S:flush_hash_page
444 */
Christophe Leroy232ca1e2020-02-15 10:14:25 +0000445 modify_instruction_site(&patch__flush_hash_A0, 0xffff, hash >> 16);
Christophe Leroy72f208c2019-04-26 16:23:35 +0000446 modify_instruction_site(&patch__flush_hash_A1, 0x7c0, hash_mb << 6);
447 modify_instruction_site(&patch__flush_hash_A2, 0x7c0, hash_mb2 << 6);
Christophe Leroy9efc74f2018-11-09 17:33:24 +0000448 modify_instruction_site(&patch__flush_hash_B, 0xffff, hmask);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000449}
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -0700450
451void setup_initial_memory_limit(phys_addr_t first_memblock_base,
452 phys_addr_t first_memblock_size)
453{
454 /* We don't currently support the first MEMBLOCK not mapping 0
455 * physical on those processors
456 */
457 BUG_ON(first_memblock_base != 0);
458
459 /* 601 can only access 16MB at the moment */
Christophe Leroy12c3f1f2019-08-26 15:52:14 +0000460 if (IS_ENABLED(CONFIG_PPC_BOOK3S_601))
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -0700461 memblock_set_current_limit(min_t(u64, first_memblock_size, 0x01000000));
462 else /* Anything else has 256M mapped */
463 memblock_set_current_limit(min_t(u64, first_memblock_size, 0x10000000));
464}
Christophe Leroy31ed2b12019-03-11 08:30:35 +0000465
Christophe Leroye4dccf92019-04-26 16:36:39 +0000466void __init print_system_hash_info(void)
467{
468 pr_info("Hash_size = 0x%lx\n", Hash_size);
469 if (Hash_mask)
470 pr_info("Hash_mask = 0x%lx\n", Hash_mask);
471}
472
Christophe Leroy31ed2b12019-03-11 08:30:35 +0000473#ifdef CONFIG_PPC_KUEP
474void __init setup_kuep(bool disabled)
475{
476 pr_info("Activating Kernel Userspace Execution Prevention\n");
477
Christophe Leroy31ed2b12019-03-11 08:30:35 +0000478 if (disabled)
479 pr_warn("KUEP cannot be disabled yet on 6xx when compiled in\n");
480}
481#endif
Christophe Leroya68c31f2019-03-11 08:30:38 +0000482
483#ifdef CONFIG_PPC_KUAP
484void __init setup_kuap(bool disabled)
485{
486 pr_info("Activating Kernel Userspace Access Protection\n");
487
488 if (disabled)
489 pr_warn("KUAP cannot be disabled yet on 6xx when compiled in\n");
490}
491#endif