xref: /openbmc/linux/arch/powerpc/mm/book3s32/mmu.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
217312f25SChristophe Leroy /*
317312f25SChristophe Leroy  * This file contains the routines for handling the MMU on those
417312f25SChristophe Leroy  * PowerPC implementations where the MMU substantially follows the
517312f25SChristophe Leroy  * architecture specification.  This includes the 6xx, 7xx, 7xxx,
617312f25SChristophe Leroy  * and 8260 implementations but excludes the 8xx and 4xx.
717312f25SChristophe Leroy  *  -- paulus
817312f25SChristophe Leroy  *
917312f25SChristophe Leroy  *  Derived from arch/ppc/mm/init.c:
1017312f25SChristophe Leroy  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
1117312f25SChristophe Leroy  *
1217312f25SChristophe Leroy  *  Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
1317312f25SChristophe Leroy  *  and Cort Dougan (PReP) (cort@cs.nmt.edu)
1417312f25SChristophe Leroy  *    Copyright (C) 1996 Paul Mackerras
1517312f25SChristophe Leroy  *
1617312f25SChristophe Leroy  *  Derived from "arch/i386/mm/init.c"
1717312f25SChristophe Leroy  *    Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
1817312f25SChristophe Leroy  */
1917312f25SChristophe Leroy 
2017312f25SChristophe Leroy #include <linux/kernel.h>
2117312f25SChristophe Leroy #include <linux/mm.h>
2217312f25SChristophe Leroy #include <linux/init.h>
2317312f25SChristophe Leroy #include <linux/highmem.h>
2417312f25SChristophe Leroy #include <linux/memblock.h>
2517312f25SChristophe Leroy 
2617312f25SChristophe Leroy #include <asm/mmu.h>
2717312f25SChristophe Leroy #include <asm/machdep.h>
2817312f25SChristophe Leroy #include <asm/code-patching.h>
2917312f25SChristophe Leroy #include <asm/sections.h>
3017312f25SChristophe Leroy 
3117312f25SChristophe Leroy #include <mm/mmu_decl.h>
3217312f25SChristophe Leroy 
3369a1593aSChristophe Leroy u8 __initdata early_hash[SZ_256K] __aligned(SZ_256K) = {0};
3469a1593aSChristophe Leroy 
356e980b5cSChristophe Leroy static struct hash_pte __initdata *Hash = (struct hash_pte *)early_hash;
366e980b5cSChristophe Leroy static unsigned long __initdata Hash_size, Hash_mask;
376e980b5cSChristophe Leroy static unsigned int __initdata hash_mb, hash_mb2;
386e980b5cSChristophe Leroy unsigned long __initdata _SDR1;
3917312f25SChristophe Leroy 
4017312f25SChristophe Leroy struct ppc_bat BATS[8][2];	/* 8 pairs of IBAT, DBAT */
4117312f25SChristophe Leroy 
4203d5b19cSChristophe Leroy static struct batrange {	/* stores address ranges mapped by BATs */
4317312f25SChristophe Leroy 	unsigned long start;
4417312f25SChristophe Leroy 	unsigned long limit;
4517312f25SChristophe Leroy 	phys_addr_t phys;
4617312f25SChristophe Leroy } bat_addrs[8];
4717312f25SChristophe Leroy 
48f2655125SChristophe Leroy #ifdef CONFIG_SMP
49f2655125SChristophe Leroy unsigned long mmu_hash_lock;
50f2655125SChristophe Leroy #endif
51f2655125SChristophe Leroy 
5217312f25SChristophe Leroy /*
5317312f25SChristophe Leroy  * Return PA for this VA if it is mapped by a BAT, or 0
5417312f25SChristophe Leroy  */
v_block_mapped(unsigned long va)5517312f25SChristophe Leroy phys_addr_t v_block_mapped(unsigned long va)
5617312f25SChristophe Leroy {
5717312f25SChristophe Leroy 	int b;
5817312f25SChristophe Leroy 	for (b = 0; b < ARRAY_SIZE(bat_addrs); ++b)
5917312f25SChristophe Leroy 		if (va >= bat_addrs[b].start && va < bat_addrs[b].limit)
6017312f25SChristophe Leroy 			return bat_addrs[b].phys + (va - bat_addrs[b].start);
6117312f25SChristophe Leroy 	return 0;
6217312f25SChristophe Leroy }
6317312f25SChristophe Leroy 
6417312f25SChristophe Leroy /*
6517312f25SChristophe Leroy  * Return VA for a given PA or 0 if not mapped
6617312f25SChristophe Leroy  */
p_block_mapped(phys_addr_t pa)6717312f25SChristophe Leroy unsigned long p_block_mapped(phys_addr_t pa)
6817312f25SChristophe Leroy {
6917312f25SChristophe Leroy 	int b;
7017312f25SChristophe Leroy 	for (b = 0; b < ARRAY_SIZE(bat_addrs); ++b)
7117312f25SChristophe Leroy 		if (pa >= bat_addrs[b].phys
7217312f25SChristophe Leroy 	    	    && pa < (bat_addrs[b].limit-bat_addrs[b].start)
7317312f25SChristophe Leroy 		              +bat_addrs[b].phys)
7417312f25SChristophe Leroy 			return bat_addrs[b].start+(pa-bat_addrs[b].phys);
7517312f25SChristophe Leroy 	return 0;
7617312f25SChristophe Leroy }
7717312f25SChristophe Leroy 
find_free_bat(void)78d37823c3SChristophe Leroy int __init find_free_bat(void)
7917312f25SChristophe Leroy {
8017312f25SChristophe Leroy 	int b;
8117312f25SChristophe Leroy 	int n = mmu_has_feature(MMU_FTR_USE_HIGH_BATS) ? 8 : 4;
8217312f25SChristophe Leroy 
8317312f25SChristophe Leroy 	for (b = 0; b < n; b++) {
8417312f25SChristophe Leroy 		struct ppc_bat *bat = BATS[b];
8517312f25SChristophe Leroy 
8617312f25SChristophe Leroy 		if (!(bat[1].batu & 3))
8717312f25SChristophe Leroy 			return b;
8817312f25SChristophe Leroy 	}
8917312f25SChristophe Leroy 	return -1;
9017312f25SChristophe Leroy }
9117312f25SChristophe Leroy 
92b970afcfSLinus Torvalds /*
93b970afcfSLinus Torvalds  * This function calculates the size of the larger block usable to map the
94b970afcfSLinus Torvalds  * beginning of an area based on the start address and size of that area:
958b14e1dfSChristophe Leroy  * - max block size is 256 on 6xx.
96b970afcfSLinus Torvalds  * - base address must be aligned to the block size. So the maximum block size
97b970afcfSLinus Torvalds  *   is identified by the lowest bit set to 1 in the base address (for instance
98b970afcfSLinus Torvalds  *   if base is 0x16000000, max size is 0x02000000).
99b970afcfSLinus Torvalds  * - block size has to be a power of two. This is calculated by finding the
100b970afcfSLinus Torvalds  *   highest bit set to 1.
101b970afcfSLinus Torvalds  */
bat_block_size(unsigned long base,unsigned long top)102d37823c3SChristophe Leroy unsigned int bat_block_size(unsigned long base, unsigned long top)
10317312f25SChristophe Leroy {
1048b14e1dfSChristophe Leroy 	unsigned int max_size = SZ_256M;
105b970afcfSLinus Torvalds 	unsigned int base_shift = (ffs(base) - 1) & 31;
10617312f25SChristophe Leroy 	unsigned int block_shift = (fls(top - base) - 1) & 31;
10717312f25SChristophe Leroy 
10817312f25SChristophe Leroy 	return min3(max_size, 1U << base_shift, 1U << block_shift);
10917312f25SChristophe Leroy }
11017312f25SChristophe Leroy 
11117312f25SChristophe Leroy /*
11217312f25SChristophe Leroy  * Set up one of the IBAT (block address translation) register pairs.
11317312f25SChristophe Leroy  * The parameters are not checked; in particular size must be a power
11417312f25SChristophe Leroy  * of 2 between 128k and 256M.
11517312f25SChristophe Leroy  */
setibat(int index,unsigned long virt,phys_addr_t phys,unsigned int size,pgprot_t prot)11617312f25SChristophe Leroy static void setibat(int index, unsigned long virt, phys_addr_t phys,
11717312f25SChristophe Leroy 		    unsigned int size, pgprot_t prot)
11817312f25SChristophe Leroy {
11917312f25SChristophe Leroy 	unsigned int bl = (size >> 17) - 1;
12017312f25SChristophe Leroy 	int wimgxpp;
12117312f25SChristophe Leroy 	struct ppc_bat *bat = BATS[index];
12217312f25SChristophe Leroy 	unsigned long flags = pgprot_val(prot);
12317312f25SChristophe Leroy 
12417312f25SChristophe Leroy 	if (!cpu_has_feature(CPU_FTR_NEED_COHERENT))
12517312f25SChristophe Leroy 		flags &= ~_PAGE_COHERENT;
12617312f25SChristophe Leroy 
12717312f25SChristophe Leroy 	wimgxpp = (flags & _PAGE_COHERENT) | (_PAGE_EXEC ? BPP_RX : BPP_XX);
12817312f25SChristophe Leroy 	bat[0].batu = virt | (bl << 2) | 2; /* Vs=1, Vp=0 */
12917312f25SChristophe Leroy 	bat[0].batl = BAT_PHYS_ADDR(phys) | wimgxpp;
13017312f25SChristophe Leroy 	if (flags & _PAGE_USER)
13117312f25SChristophe Leroy 		bat[0].batu |= 1;	/* Vp = 1 */
13217312f25SChristophe Leroy }
13317312f25SChristophe Leroy 
clearibat(int index)13417312f25SChristophe Leroy static void clearibat(int index)
13517312f25SChristophe Leroy {
13617312f25SChristophe Leroy 	struct ppc_bat *bat = BATS[index];
13717312f25SChristophe Leroy 
13817312f25SChristophe Leroy 	bat[0].batu = 0;
13917312f25SChristophe Leroy 	bat[0].batl = 0;
14017312f25SChristophe Leroy }
14117312f25SChristophe Leroy 
__mmu_mapin_ram(unsigned long base,unsigned long top)14217312f25SChristophe Leroy static unsigned long __init __mmu_mapin_ram(unsigned long base, unsigned long top)
14317312f25SChristophe Leroy {
14417312f25SChristophe Leroy 	int idx;
14517312f25SChristophe Leroy 
14617312f25SChristophe Leroy 	while ((idx = find_free_bat()) != -1 && base != top) {
147d37823c3SChristophe Leroy 		unsigned int size = bat_block_size(base, top);
14817312f25SChristophe Leroy 
14917312f25SChristophe Leroy 		if (size < 128 << 10)
15017312f25SChristophe Leroy 			break;
15117312f25SChristophe Leroy 		setbat(idx, PAGE_OFFSET + base, base, size, PAGE_KERNEL_X);
15217312f25SChristophe Leroy 		base += size;
15317312f25SChristophe Leroy 	}
15417312f25SChristophe Leroy 
15517312f25SChristophe Leroy 	return base;
15617312f25SChristophe Leroy }
15717312f25SChristophe Leroy 
mmu_mapin_ram(unsigned long base,unsigned long top)15817312f25SChristophe Leroy unsigned long __init mmu_mapin_ram(unsigned long base, unsigned long top)
15917312f25SChristophe Leroy {
160b970afcfSLinus Torvalds 	unsigned long done;
161b150a4d1SMichael Ellerman 	unsigned long border = (unsigned long)__srwx_boundary - PAGE_OFFSET;
1622a0fb3c1SChristophe Leroy 	unsigned long size;
16317312f25SChristophe Leroy 
1642a0fb3c1SChristophe Leroy 	size = roundup_pow_of_two((unsigned long)_einittext - PAGE_OFFSET);
1652a0fb3c1SChristophe Leroy 	setibat(0, PAGE_OFFSET, 0, size, PAGE_KERNEL_X);
166035b19a1SChristophe Leroy 
1671ce84497SChristophe Leroy 	if (debug_pagealloc_enabled_or_kfence()) {
168035b19a1SChristophe Leroy 		pr_debug_once("Read-Write memory mapped without BATs\n");
1692b279c03SChristophe Leroy 		if (base >= border)
1702b279c03SChristophe Leroy 			return base;
1712b279c03SChristophe Leroy 		if (top >= border)
1722b279c03SChristophe Leroy 			top = border;
1732b279c03SChristophe Leroy 	}
17417312f25SChristophe Leroy 
17517312f25SChristophe Leroy 	if (!strict_kernel_rwx_enabled() || base >= border || top <= border)
17617312f25SChristophe Leroy 		return __mmu_mapin_ram(base, top);
17717312f25SChristophe Leroy 
17817312f25SChristophe Leroy 	done = __mmu_mapin_ram(base, border);
179b970afcfSLinus Torvalds 	if (done != border)
18017312f25SChristophe Leroy 		return done;
18117312f25SChristophe Leroy 
182b970afcfSLinus Torvalds 	return __mmu_mapin_ram(border, top);
18317312f25SChristophe Leroy }
18417312f25SChristophe Leroy 
is_module_segment(unsigned long addr)185c4964331SChristophe Leroy static bool is_module_segment(unsigned long addr)
186c4964331SChristophe Leroy {
187c4964331SChristophe Leroy 	if (!IS_ENABLED(CONFIG_MODULES))
188c4964331SChristophe Leroy 		return false;
1897bee31adSChristophe Leroy 	if (addr < ALIGN_DOWN(MODULES_VADDR, SZ_256M))
1907bee31adSChristophe Leroy 		return false;
191541cebb5SChristophe Leroy 	if (addr > ALIGN(MODULES_END, SZ_256M) - 1)
1927bee31adSChristophe Leroy 		return false;
193c4964331SChristophe Leroy 	return true;
194c4964331SChristophe Leroy }
195c4964331SChristophe Leroy 
mmu_mark_initmem_nx(void)19617312f25SChristophe Leroy void mmu_mark_initmem_nx(void)
19717312f25SChristophe Leroy {
19817312f25SChristophe Leroy 	int nb = mmu_has_feature(MMU_FTR_USE_HIGH_BATS) ? 8 : 4;
19917312f25SChristophe Leroy 	int i;
20017312f25SChristophe Leroy 	unsigned long base = (unsigned long)_stext - PAGE_OFFSET;
20137eb7ca9SChristophe Leroy 	unsigned long top = ALIGN((unsigned long)_etext - PAGE_OFFSET, SZ_128K);
2024b19f96aSChristophe Leroy 	unsigned long border = (unsigned long)__init_begin - PAGE_OFFSET;
20317312f25SChristophe Leroy 	unsigned long size;
20417312f25SChristophe Leroy 
20537eb7ca9SChristophe Leroy 	for (i = 0; i < nb - 1 && base < top;) {
206d37823c3SChristophe Leroy 		size = bat_block_size(base, top);
20717312f25SChristophe Leroy 		setibat(i++, PAGE_OFFSET + base, base, size, PAGE_KERNEL_TEXT);
20817312f25SChristophe Leroy 		base += size;
20917312f25SChristophe Leroy 	}
21017312f25SChristophe Leroy 	if (base < top) {
211d37823c3SChristophe Leroy 		size = bat_block_size(base, top);
21217312f25SChristophe Leroy 		if ((top - base) > size) {
21317312f25SChristophe Leroy 			size <<= 1;
2144b19f96aSChristophe Leroy 			if (strict_kernel_rwx_enabled() && base + size > border)
2154b19f96aSChristophe Leroy 				pr_warn("Some RW data is getting mapped X. "
2164b19f96aSChristophe Leroy 					"Adjust CONFIG_DATA_SHIFT to avoid that.\n");
21717312f25SChristophe Leroy 		}
21817312f25SChristophe Leroy 		setibat(i++, PAGE_OFFSET + base, base, size, PAGE_KERNEL_TEXT);
21917312f25SChristophe Leroy 		base += size;
22017312f25SChristophe Leroy 	}
22117312f25SChristophe Leroy 	for (; i < nb; i++)
22217312f25SChristophe Leroy 		clearibat(i);
22317312f25SChristophe Leroy 
22417312f25SChristophe Leroy 	update_bats();
22517312f25SChristophe Leroy 
22617312f25SChristophe Leroy 	for (i = TASK_SIZE >> 28; i < 16; i++) {
22717312f25SChristophe Leroy 		/* Do not set NX on VM space for modules */
228c4964331SChristophe Leroy 		if (is_module_segment(i << 28))
229c4964331SChristophe Leroy 			continue;
230c4964331SChristophe Leroy 
231179ae57dSChristophe Leroy 		mtsr(mfsr(i << 28) | 0x10000000, i << 28);
23217312f25SChristophe Leroy 	}
23317312f25SChristophe Leroy }
23417312f25SChristophe Leroy 
mmu_mark_rodata_ro(void)23517312f25SChristophe Leroy void mmu_mark_rodata_ro(void)
23617312f25SChristophe Leroy {
23717312f25SChristophe Leroy 	int nb = mmu_has_feature(MMU_FTR_USE_HIGH_BATS) ? 8 : 4;
23817312f25SChristophe Leroy 	int i;
23917312f25SChristophe Leroy 
24017312f25SChristophe Leroy 	for (i = 0; i < nb; i++) {
24117312f25SChristophe Leroy 		struct ppc_bat *bat = BATS[i];
24217312f25SChristophe Leroy 
2437082f8e7SNicholas Piggin 		if (bat_addrs[i].start < (unsigned long)__end_rodata)
24417312f25SChristophe Leroy 			bat[1].batl = (bat[1].batl & ~BPP_RW) | BPP_RX;
24517312f25SChristophe Leroy 	}
24617312f25SChristophe Leroy 
24717312f25SChristophe Leroy 	update_bats();
24817312f25SChristophe Leroy }
24917312f25SChristophe Leroy 
25017312f25SChristophe Leroy /*
2512a0fb3c1SChristophe Leroy  * Set up one of the D BAT (block address translation) register pairs.
25217312f25SChristophe Leroy  * The parameters are not checked; in particular size must be a power
25317312f25SChristophe Leroy  * of 2 between 128k and 256M.
25417312f25SChristophe Leroy  */
setbat(int index,unsigned long virt,phys_addr_t phys,unsigned int size,pgprot_t prot)25517312f25SChristophe Leroy void __init setbat(int index, unsigned long virt, phys_addr_t phys,
25617312f25SChristophe Leroy 		   unsigned int size, pgprot_t prot)
25717312f25SChristophe Leroy {
25817312f25SChristophe Leroy 	unsigned int bl;
25917312f25SChristophe Leroy 	int wimgxpp;
260cbcaff7dSChristophe Leroy 	struct ppc_bat *bat;
26117312f25SChristophe Leroy 	unsigned long flags = pgprot_val(prot);
26217312f25SChristophe Leroy 
263cbcaff7dSChristophe Leroy 	if (index == -1)
264cbcaff7dSChristophe Leroy 		index = find_free_bat();
265cbcaff7dSChristophe Leroy 	if (index == -1) {
266cbcaff7dSChristophe Leroy 		pr_err("%s: no BAT available for mapping 0x%llx\n", __func__,
267cbcaff7dSChristophe Leroy 		       (unsigned long long)phys);
268cbcaff7dSChristophe Leroy 		return;
269cbcaff7dSChristophe Leroy 	}
270cbcaff7dSChristophe Leroy 	bat = BATS[index];
271cbcaff7dSChristophe Leroy 
27217312f25SChristophe Leroy 	if ((flags & _PAGE_NO_CACHE) ||
27317312f25SChristophe Leroy 	    (cpu_has_feature(CPU_FTR_NEED_COHERENT) == 0))
27417312f25SChristophe Leroy 		flags &= ~_PAGE_COHERENT;
27517312f25SChristophe Leroy 
27617312f25SChristophe Leroy 	bl = (size >> 17) - 1;
27717312f25SChristophe Leroy 	/* Do DBAT first */
27817312f25SChristophe Leroy 	wimgxpp = flags & (_PAGE_WRITETHRU | _PAGE_NO_CACHE
27917312f25SChristophe Leroy 			   | _PAGE_COHERENT | _PAGE_GUARDED);
28017312f25SChristophe Leroy 	wimgxpp |= (flags & _PAGE_RW)? BPP_RW: BPP_RX;
28117312f25SChristophe Leroy 	bat[1].batu = virt | (bl << 2) | 2; /* Vs=1, Vp=0 */
28217312f25SChristophe Leroy 	bat[1].batl = BAT_PHYS_ADDR(phys) | wimgxpp;
28317312f25SChristophe Leroy 	if (flags & _PAGE_USER)
28417312f25SChristophe Leroy 		bat[1].batu |= 1; 	/* Vp = 1 */
28517312f25SChristophe Leroy 	if (flags & _PAGE_GUARDED) {
28617312f25SChristophe Leroy 		/* G bit must be zero in IBATs */
28717312f25SChristophe Leroy 		flags &= ~_PAGE_EXEC;
28817312f25SChristophe Leroy 	}
28917312f25SChristophe Leroy 
29017312f25SChristophe Leroy 	bat_addrs[index].start = virt;
29117312f25SChristophe Leroy 	bat_addrs[index].limit = virt + ((bl + 1) << 17) - 1;
29217312f25SChristophe Leroy 	bat_addrs[index].phys = phys;
29317312f25SChristophe Leroy }
29417312f25SChristophe Leroy 
29517312f25SChristophe Leroy /*
29617312f25SChristophe Leroy  * Preload a translation in the hash table
29717312f25SChristophe Leroy  */
hash_preload(struct mm_struct * mm,unsigned long ea)29879d1befeSChristophe Leroy static void hash_preload(struct mm_struct *mm, unsigned long ea)
29917312f25SChristophe Leroy {
30017312f25SChristophe Leroy 	pmd_t *pmd;
30117312f25SChristophe Leroy 
3024cc445b4SChristophe Leroy 	if (!mmu_has_feature(MMU_FTR_HPTE_TABLE))
30317312f25SChristophe Leroy 		return;
304e05c7b1fSMike Rapoport 	pmd = pmd_off(mm, ea);
30517312f25SChristophe Leroy 	if (!pmd_none(*pmd))
30617312f25SChristophe Leroy 		add_hash_page(mm->context.id, ea, pmd_val(*pmd));
30717312f25SChristophe Leroy }
30817312f25SChristophe Leroy 
30917312f25SChristophe Leroy /*
310e5a1edb9SChristophe Leroy  * This is called at the end of handling a user page fault, when the
311e5a1edb9SChristophe Leroy  * fault has been handled by updating a PTE in the linux page tables.
312e5a1edb9SChristophe Leroy  * We use it to preload an HPTE into the hash table corresponding to
313e5a1edb9SChristophe Leroy  * the updated linux PTE.
314e5a1edb9SChristophe Leroy  *
315e5a1edb9SChristophe Leroy  * This must always be called with the pte lock held.
316e5a1edb9SChristophe Leroy  */
__update_mmu_cache(struct vm_area_struct * vma,unsigned long address,pte_t * ptep)317*73ea68adSChristophe Leroy void __update_mmu_cache(struct vm_area_struct *vma, unsigned long address,
318e5a1edb9SChristophe Leroy 		      pte_t *ptep)
319e5a1edb9SChristophe Leroy {
320e5a1edb9SChristophe Leroy 	/*
321e5a1edb9SChristophe Leroy 	 * We don't need to worry about _PAGE_PRESENT here because we are
322e5a1edb9SChristophe Leroy 	 * called with either mm->page_table_lock held or ptl lock held
323e5a1edb9SChristophe Leroy 	 */
324e5a1edb9SChristophe Leroy 
325e5a1edb9SChristophe Leroy 	/* We only want HPTEs for linux PTEs that have _PAGE_ACCESSED set */
326e5a1edb9SChristophe Leroy 	if (!pte_young(*ptep) || address >= TASK_SIZE)
327e5a1edb9SChristophe Leroy 		return;
328e5a1edb9SChristophe Leroy 
329f49f4e2bSChristophe Leroy 	/* We have to test for regs NULL since init will get here first thing at boot */
330f49f4e2bSChristophe Leroy 	if (!current->thread.regs)
331e5a1edb9SChristophe Leroy 		return;
332e5a1edb9SChristophe Leroy 
333f49f4e2bSChristophe Leroy 	/* We also avoid filling the hash if not coming from a fault */
334f49f4e2bSChristophe Leroy 	if (TRAP(current->thread.regs) != 0x300 && TRAP(current->thread.regs) != 0x400)
335f49f4e2bSChristophe Leroy 		return;
336f49f4e2bSChristophe Leroy 
337f49f4e2bSChristophe Leroy 	hash_preload(vma->vm_mm, address);
338e5a1edb9SChristophe Leroy }
339e5a1edb9SChristophe Leroy 
340e5a1edb9SChristophe Leroy /*
34117312f25SChristophe Leroy  * Initialize the hash table and patch the instructions in hashtable.S.
34217312f25SChristophe Leroy  */
MMU_init_hw(void)34317312f25SChristophe Leroy void __init MMU_init_hw(void)
34417312f25SChristophe Leroy {
34517312f25SChristophe Leroy 	unsigned int n_hpteg, lg_n_hpteg;
34617312f25SChristophe Leroy 
34717312f25SChristophe Leroy 	if (!mmu_has_feature(MMU_FTR_HPTE_TABLE))
34817312f25SChristophe Leroy 		return;
34917312f25SChristophe Leroy 
35017312f25SChristophe Leroy 	if ( ppc_md.progress ) ppc_md.progress("hash:enter", 0x105);
35117312f25SChristophe Leroy 
35217312f25SChristophe Leroy #define LG_HPTEG_SIZE	6		/* 64 bytes per HPTEG */
35317312f25SChristophe Leroy #define SDR1_LOW_BITS	((n_hpteg - 1) >> 10)
35417312f25SChristophe Leroy #define MIN_N_HPTEG	1024		/* min 64kB hash table */
35517312f25SChristophe Leroy 
35617312f25SChristophe Leroy 	/*
35717312f25SChristophe Leroy 	 * Allow 1 HPTE (1/8 HPTEG) for each page of memory.
35817312f25SChristophe Leroy 	 * This is less than the recommended amount, but then
35917312f25SChristophe Leroy 	 * Linux ain't AIX.
36017312f25SChristophe Leroy 	 */
36117312f25SChristophe Leroy 	n_hpteg = total_memory / (PAGE_SIZE * 8);
36217312f25SChristophe Leroy 	if (n_hpteg < MIN_N_HPTEG)
36317312f25SChristophe Leroy 		n_hpteg = MIN_N_HPTEG;
36417312f25SChristophe Leroy 	lg_n_hpteg = __ilog2(n_hpteg);
36517312f25SChristophe Leroy 	if (n_hpteg & (n_hpteg - 1)) {
36617312f25SChristophe Leroy 		++lg_n_hpteg;		/* round up if not power of 2 */
36717312f25SChristophe Leroy 		n_hpteg = 1 << lg_n_hpteg;
36817312f25SChristophe Leroy 	}
36917312f25SChristophe Leroy 	Hash_size = n_hpteg << LG_HPTEG_SIZE;
37017312f25SChristophe Leroy 
37117312f25SChristophe Leroy 	/*
37217312f25SChristophe Leroy 	 * Find some memory for the hash table.
37317312f25SChristophe Leroy 	 */
37417312f25SChristophe Leroy 	if ( ppc_md.progress ) ppc_md.progress("hash:find piece", 0x322);
37517312f25SChristophe Leroy 	Hash = memblock_alloc(Hash_size, Hash_size);
37617312f25SChristophe Leroy 	if (!Hash)
37717312f25SChristophe Leroy 		panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
37817312f25SChristophe Leroy 		      __func__, Hash_size, Hash_size);
37917312f25SChristophe Leroy 	_SDR1 = __pa(Hash) | SDR1_LOW_BITS;
38017312f25SChristophe Leroy 
3818f156c23SChristophe Leroy 	pr_info("Total memory = %lldMB; using %ldkB for hash table\n",
3828f156c23SChristophe Leroy 		(unsigned long long)(total_memory >> 20), Hash_size >> 10);
38317312f25SChristophe Leroy 
38417312f25SChristophe Leroy 
38572f208c6SChristophe Leroy 	Hash_mask = n_hpteg - 1;
38672f208c6SChristophe Leroy 	hash_mb2 = hash_mb = 32 - LG_HPTEG_SIZE - lg_n_hpteg;
38772f208c6SChristophe Leroy 	if (lg_n_hpteg > 16)
38872f208c6SChristophe Leroy 		hash_mb2 = 16 - LG_HPTEG_SIZE;
38972f208c6SChristophe Leroy }
39072f208c6SChristophe Leroy 
MMU_init_hw_patch(void)39172f208c6SChristophe Leroy void __init MMU_init_hw_patch(void)
39272f208c6SChristophe Leroy {
39372f208c6SChristophe Leroy 	unsigned int hmask = Hash_mask >> (16 - LG_HPTEG_SIZE);
394232ca1eeSChristophe Leroy 	unsigned int hash = (unsigned int)Hash - PAGE_OFFSET;
39572f208c6SChristophe Leroy 
39669a1593aSChristophe Leroy 	if (!mmu_has_feature(MMU_FTR_HPTE_TABLE))
39769a1593aSChristophe Leroy 		return;
39869a1593aSChristophe Leroy 
39972f208c6SChristophe Leroy 	if (ppc_md.progress)
40072f208c6SChristophe Leroy 		ppc_md.progress("hash:patch", 0x345);
40172f208c6SChristophe Leroy 	if (ppc_md.progress)
40272f208c6SChristophe Leroy 		ppc_md.progress("hash:done", 0x205);
40372f208c6SChristophe Leroy 
40472f208c6SChristophe Leroy 	/* WARNING: Make sure nothing can trigger a KASAN check past this point */
40572f208c6SChristophe Leroy 
40617312f25SChristophe Leroy 	/*
40717312f25SChristophe Leroy 	 * Patch up the instructions in hashtable.S:create_hpte
40817312f25SChristophe Leroy 	 */
409cd08f109SChristophe Leroy 	modify_instruction_site(&patch__hash_page_A0, 0xffff, hash >> 16);
41072f208c6SChristophe Leroy 	modify_instruction_site(&patch__hash_page_A1, 0x7c0, hash_mb << 6);
41172f208c6SChristophe Leroy 	modify_instruction_site(&patch__hash_page_A2, 0x7c0, hash_mb2 << 6);
41217312f25SChristophe Leroy 	modify_instruction_site(&patch__hash_page_B, 0xffff, hmask);
41317312f25SChristophe Leroy 	modify_instruction_site(&patch__hash_page_C, 0xffff, hmask);
41417312f25SChristophe Leroy 
41517312f25SChristophe Leroy 	/*
41617312f25SChristophe Leroy 	 * Patch up the instructions in hashtable.S:flush_hash_page
41717312f25SChristophe Leroy 	 */
418232ca1eeSChristophe Leroy 	modify_instruction_site(&patch__flush_hash_A0, 0xffff, hash >> 16);
41972f208c6SChristophe Leroy 	modify_instruction_site(&patch__flush_hash_A1, 0x7c0, hash_mb << 6);
42072f208c6SChristophe Leroy 	modify_instruction_site(&patch__flush_hash_A2, 0x7c0, hash_mb2 << 6);
42117312f25SChristophe Leroy 	modify_instruction_site(&patch__flush_hash_B, 0xffff, hmask);
42217312f25SChristophe Leroy }
42317312f25SChristophe Leroy 
setup_initial_memory_limit(phys_addr_t first_memblock_base,phys_addr_t first_memblock_size)42417312f25SChristophe Leroy void setup_initial_memory_limit(phys_addr_t first_memblock_base,
42517312f25SChristophe Leroy 				phys_addr_t first_memblock_size)
42617312f25SChristophe Leroy {
42717312f25SChristophe Leroy 	/* We don't currently support the first MEMBLOCK not mapping 0
42817312f25SChristophe Leroy 	 * physical on those processors
42917312f25SChristophe Leroy 	 */
43017312f25SChristophe Leroy 	BUG_ON(first_memblock_base != 0);
43117312f25SChristophe Leroy 
4328b14e1dfSChristophe Leroy 	memblock_set_current_limit(min_t(u64, first_memblock_size, SZ_256M));
43317312f25SChristophe Leroy }
43417312f25SChristophe Leroy 
print_system_hash_info(void)435e4dccf90SChristophe Leroy void __init print_system_hash_info(void)
436e4dccf90SChristophe Leroy {
437e4dccf90SChristophe Leroy 	pr_info("Hash_size         = 0x%lx\n", Hash_size);
438e4dccf90SChristophe Leroy 	if (Hash_mask)
439e4dccf90SChristophe Leroy 		pr_info("Hash_mask         = 0x%lx\n", Hash_mask);
440e4dccf90SChristophe Leroy }
441e4dccf90SChristophe Leroy 
early_init_mmu(void)442068fdba1SChristophe Leroy void __init early_init_mmu(void)
443068fdba1SChristophe Leroy {
444068fdba1SChristophe Leroy }
445