162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * This file contains the routines for handling the MMU on those
462306a36Sopenharmony_ci * PowerPC implementations where the MMU substantially follows the
562306a36Sopenharmony_ci * architecture specification.  This includes the 6xx, 7xx, 7xxx,
662306a36Sopenharmony_ci * and 8260 implementations but excludes the 8xx and 4xx.
762306a36Sopenharmony_ci *  -- paulus
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci *  Derived from arch/ppc/mm/init.c:
1062306a36Sopenharmony_ci *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
1162306a36Sopenharmony_ci *
1262306a36Sopenharmony_ci *  Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
1362306a36Sopenharmony_ci *  and Cort Dougan (PReP) (cort@cs.nmt.edu)
1462306a36Sopenharmony_ci *    Copyright (C) 1996 Paul Mackerras
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci *  Derived from "arch/i386/mm/init.c"
1762306a36Sopenharmony_ci *    Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
1862306a36Sopenharmony_ci */
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#include <linux/kernel.h>
2162306a36Sopenharmony_ci#include <linux/mm.h>
2262306a36Sopenharmony_ci#include <linux/init.h>
2362306a36Sopenharmony_ci#include <linux/highmem.h>
2462306a36Sopenharmony_ci#include <linux/memblock.h>
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#include <asm/mmu.h>
2762306a36Sopenharmony_ci#include <asm/machdep.h>
2862306a36Sopenharmony_ci#include <asm/code-patching.h>
2962306a36Sopenharmony_ci#include <asm/sections.h>
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci#include <mm/mmu_decl.h>
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ciu8 __initdata early_hash[SZ_256K] __aligned(SZ_256K) = {0};
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_cistatic struct hash_pte __initdata *Hash = (struct hash_pte *)early_hash;
3662306a36Sopenharmony_cistatic unsigned long __initdata Hash_size, Hash_mask;
3762306a36Sopenharmony_cistatic unsigned int __initdata hash_mb, hash_mb2;
3862306a36Sopenharmony_ciunsigned long __initdata _SDR1;
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_cistruct ppc_bat BATS[8][2];	/* 8 pairs of IBAT, DBAT */
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_cistatic struct batrange {	/* stores address ranges mapped by BATs */
4362306a36Sopenharmony_ci	unsigned long start;
4462306a36Sopenharmony_ci	unsigned long limit;
4562306a36Sopenharmony_ci	phys_addr_t phys;
4662306a36Sopenharmony_ci} bat_addrs[8];
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci#ifdef CONFIG_SMP
4962306a36Sopenharmony_ciunsigned long mmu_hash_lock;
5062306a36Sopenharmony_ci#endif
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci/*
5362306a36Sopenharmony_ci * Return PA for this VA if it is mapped by a BAT, or 0
5462306a36Sopenharmony_ci */
5562306a36Sopenharmony_ciphys_addr_t v_block_mapped(unsigned long va)
5662306a36Sopenharmony_ci{
5762306a36Sopenharmony_ci	int b;
5862306a36Sopenharmony_ci	for (b = 0; b < ARRAY_SIZE(bat_addrs); ++b)
5962306a36Sopenharmony_ci		if (va >= bat_addrs[b].start && va < bat_addrs[b].limit)
6062306a36Sopenharmony_ci			return bat_addrs[b].phys + (va - bat_addrs[b].start);
6162306a36Sopenharmony_ci	return 0;
6262306a36Sopenharmony_ci}
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci/*
6562306a36Sopenharmony_ci * Return VA for a given PA or 0 if not mapped
6662306a36Sopenharmony_ci */
6762306a36Sopenharmony_ciunsigned long p_block_mapped(phys_addr_t pa)
6862306a36Sopenharmony_ci{
6962306a36Sopenharmony_ci	int b;
7062306a36Sopenharmony_ci	for (b = 0; b < ARRAY_SIZE(bat_addrs); ++b)
7162306a36Sopenharmony_ci		if (pa >= bat_addrs[b].phys
7262306a36Sopenharmony_ci	    	    && pa < (bat_addrs[b].limit-bat_addrs[b].start)
7362306a36Sopenharmony_ci		              +bat_addrs[b].phys)
7462306a36Sopenharmony_ci			return bat_addrs[b].start+(pa-bat_addrs[b].phys);
7562306a36Sopenharmony_ci	return 0;
7662306a36Sopenharmony_ci}
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ciint __init find_free_bat(void)
7962306a36Sopenharmony_ci{
8062306a36Sopenharmony_ci	int b;
8162306a36Sopenharmony_ci	int n = mmu_has_feature(MMU_FTR_USE_HIGH_BATS) ? 8 : 4;
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci	for (b = 0; b < n; b++) {
8462306a36Sopenharmony_ci		struct ppc_bat *bat = BATS[b];
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci		if (!(bat[1].batu & 3))
8762306a36Sopenharmony_ci			return b;
8862306a36Sopenharmony_ci	}
8962306a36Sopenharmony_ci	return -1;
9062306a36Sopenharmony_ci}
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci/*
9362306a36Sopenharmony_ci * This function calculates the size of the larger block usable to map the
9462306a36Sopenharmony_ci * beginning of an area based on the start address and size of that area:
9562306a36Sopenharmony_ci * - max block size is 256 on 6xx.
9662306a36Sopenharmony_ci * - base address must be aligned to the block size. So the maximum block size
9762306a36Sopenharmony_ci *   is identified by the lowest bit set to 1 in the base address (for instance
9862306a36Sopenharmony_ci *   if base is 0x16000000, max size is 0x02000000).
9962306a36Sopenharmony_ci * - block size has to be a power of two. This is calculated by finding the
10062306a36Sopenharmony_ci *   highest bit set to 1.
10162306a36Sopenharmony_ci */
10262306a36Sopenharmony_ciunsigned int bat_block_size(unsigned long base, unsigned long top)
10362306a36Sopenharmony_ci{
10462306a36Sopenharmony_ci	unsigned int max_size = SZ_256M;
10562306a36Sopenharmony_ci	unsigned int base_shift = (ffs(base) - 1) & 31;
10662306a36Sopenharmony_ci	unsigned int block_shift = (fls(top - base) - 1) & 31;
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci	return min3(max_size, 1U << base_shift, 1U << block_shift);
10962306a36Sopenharmony_ci}
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci/*
11262306a36Sopenharmony_ci * Set up one of the IBAT (block address translation) register pairs.
11362306a36Sopenharmony_ci * The parameters are not checked; in particular size must be a power
11462306a36Sopenharmony_ci * of 2 between 128k and 256M.
11562306a36Sopenharmony_ci */
11662306a36Sopenharmony_cistatic void setibat(int index, unsigned long virt, phys_addr_t phys,
11762306a36Sopenharmony_ci		    unsigned int size, pgprot_t prot)
11862306a36Sopenharmony_ci{
11962306a36Sopenharmony_ci	unsigned int bl = (size >> 17) - 1;
12062306a36Sopenharmony_ci	int wimgxpp;
12162306a36Sopenharmony_ci	struct ppc_bat *bat = BATS[index];
12262306a36Sopenharmony_ci	unsigned long flags = pgprot_val(prot);
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci	if (!cpu_has_feature(CPU_FTR_NEED_COHERENT))
12562306a36Sopenharmony_ci		flags &= ~_PAGE_COHERENT;
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci	wimgxpp = (flags & _PAGE_COHERENT) | (_PAGE_EXEC ? BPP_RX : BPP_XX);
12862306a36Sopenharmony_ci	bat[0].batu = virt | (bl << 2) | 2; /* Vs=1, Vp=0 */
12962306a36Sopenharmony_ci	bat[0].batl = BAT_PHYS_ADDR(phys) | wimgxpp;
13062306a36Sopenharmony_ci	if (flags & _PAGE_USER)
13162306a36Sopenharmony_ci		bat[0].batu |= 1;	/* Vp = 1 */
13262306a36Sopenharmony_ci}
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_cistatic void clearibat(int index)
13562306a36Sopenharmony_ci{
13662306a36Sopenharmony_ci	struct ppc_bat *bat = BATS[index];
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci	bat[0].batu = 0;
13962306a36Sopenharmony_ci	bat[0].batl = 0;
14062306a36Sopenharmony_ci}
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_cistatic unsigned long __init __mmu_mapin_ram(unsigned long base, unsigned long top)
14362306a36Sopenharmony_ci{
14462306a36Sopenharmony_ci	int idx;
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci	while ((idx = find_free_bat()) != -1 && base != top) {
14762306a36Sopenharmony_ci		unsigned int size = bat_block_size(base, top);
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci		if (size < 128 << 10)
15062306a36Sopenharmony_ci			break;
15162306a36Sopenharmony_ci		setbat(idx, PAGE_OFFSET + base, base, size, PAGE_KERNEL_X);
15262306a36Sopenharmony_ci		base += size;
15362306a36Sopenharmony_ci	}
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci	return base;
15662306a36Sopenharmony_ci}
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ciunsigned long __init mmu_mapin_ram(unsigned long base, unsigned long top)
15962306a36Sopenharmony_ci{
16062306a36Sopenharmony_ci	unsigned long done;
16162306a36Sopenharmony_ci	unsigned long border = (unsigned long)__srwx_boundary - PAGE_OFFSET;
16262306a36Sopenharmony_ci	unsigned long size;
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci	size = roundup_pow_of_two((unsigned long)_einittext - PAGE_OFFSET);
16562306a36Sopenharmony_ci	setibat(0, PAGE_OFFSET, 0, size, PAGE_KERNEL_X);
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci	if (debug_pagealloc_enabled_or_kfence()) {
16862306a36Sopenharmony_ci		pr_debug_once("Read-Write memory mapped without BATs\n");
16962306a36Sopenharmony_ci		if (base >= border)
17062306a36Sopenharmony_ci			return base;
17162306a36Sopenharmony_ci		if (top >= border)
17262306a36Sopenharmony_ci			top = border;
17362306a36Sopenharmony_ci	}
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci	if (!strict_kernel_rwx_enabled() || base >= border || top <= border)
17662306a36Sopenharmony_ci		return __mmu_mapin_ram(base, top);
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci	done = __mmu_mapin_ram(base, border);
17962306a36Sopenharmony_ci	if (done != border)
18062306a36Sopenharmony_ci		return done;
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci	return __mmu_mapin_ram(border, top);
18362306a36Sopenharmony_ci}
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_cistatic bool is_module_segment(unsigned long addr)
18662306a36Sopenharmony_ci{
18762306a36Sopenharmony_ci	if (!IS_ENABLED(CONFIG_MODULES))
18862306a36Sopenharmony_ci		return false;
18962306a36Sopenharmony_ci	if (addr < ALIGN_DOWN(MODULES_VADDR, SZ_256M))
19062306a36Sopenharmony_ci		return false;
19162306a36Sopenharmony_ci	if (addr > ALIGN(MODULES_END, SZ_256M) - 1)
19262306a36Sopenharmony_ci		return false;
19362306a36Sopenharmony_ci	return true;
19462306a36Sopenharmony_ci}
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_civoid mmu_mark_initmem_nx(void)
19762306a36Sopenharmony_ci{
19862306a36Sopenharmony_ci	int nb = mmu_has_feature(MMU_FTR_USE_HIGH_BATS) ? 8 : 4;
19962306a36Sopenharmony_ci	int i;
20062306a36Sopenharmony_ci	unsigned long base = (unsigned long)_stext - PAGE_OFFSET;
20162306a36Sopenharmony_ci	unsigned long top = ALIGN((unsigned long)_etext - PAGE_OFFSET, SZ_128K);
20262306a36Sopenharmony_ci	unsigned long border = (unsigned long)__init_begin - PAGE_OFFSET;
20362306a36Sopenharmony_ci	unsigned long size;
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci	for (i = 0; i < nb - 1 && base < top;) {
20662306a36Sopenharmony_ci		size = bat_block_size(base, top);
20762306a36Sopenharmony_ci		setibat(i++, PAGE_OFFSET + base, base, size, PAGE_KERNEL_TEXT);
20862306a36Sopenharmony_ci		base += size;
20962306a36Sopenharmony_ci	}
21062306a36Sopenharmony_ci	if (base < top) {
21162306a36Sopenharmony_ci		size = bat_block_size(base, top);
21262306a36Sopenharmony_ci		if ((top - base) > size) {
21362306a36Sopenharmony_ci			size <<= 1;
21462306a36Sopenharmony_ci			if (strict_kernel_rwx_enabled() && base + size > border)
21562306a36Sopenharmony_ci				pr_warn("Some RW data is getting mapped X. "
21662306a36Sopenharmony_ci					"Adjust CONFIG_DATA_SHIFT to avoid that.\n");
21762306a36Sopenharmony_ci		}
21862306a36Sopenharmony_ci		setibat(i++, PAGE_OFFSET + base, base, size, PAGE_KERNEL_TEXT);
21962306a36Sopenharmony_ci		base += size;
22062306a36Sopenharmony_ci	}
22162306a36Sopenharmony_ci	for (; i < nb; i++)
22262306a36Sopenharmony_ci		clearibat(i);
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ci	update_bats();
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ci	for (i = TASK_SIZE >> 28; i < 16; i++) {
22762306a36Sopenharmony_ci		/* Do not set NX on VM space for modules */
22862306a36Sopenharmony_ci		if (is_module_segment(i << 28))
22962306a36Sopenharmony_ci			continue;
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci		mtsr(mfsr(i << 28) | 0x10000000, i << 28);
23262306a36Sopenharmony_ci	}
23362306a36Sopenharmony_ci}
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_civoid mmu_mark_rodata_ro(void)
23662306a36Sopenharmony_ci{
23762306a36Sopenharmony_ci	int nb = mmu_has_feature(MMU_FTR_USE_HIGH_BATS) ? 8 : 4;
23862306a36Sopenharmony_ci	int i;
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci	for (i = 0; i < nb; i++) {
24162306a36Sopenharmony_ci		struct ppc_bat *bat = BATS[i];
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_ci		if (bat_addrs[i].start < (unsigned long)__end_rodata)
24462306a36Sopenharmony_ci			bat[1].batl = (bat[1].batl & ~BPP_RW) | BPP_RX;
24562306a36Sopenharmony_ci	}
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ci	update_bats();
24862306a36Sopenharmony_ci}
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci/*
25162306a36Sopenharmony_ci * Set up one of the D BAT (block address translation) register pairs.
25262306a36Sopenharmony_ci * The parameters are not checked; in particular size must be a power
25362306a36Sopenharmony_ci * of 2 between 128k and 256M.
25462306a36Sopenharmony_ci */
25562306a36Sopenharmony_civoid __init setbat(int index, unsigned long virt, phys_addr_t phys,
25662306a36Sopenharmony_ci		   unsigned int size, pgprot_t prot)
25762306a36Sopenharmony_ci{
25862306a36Sopenharmony_ci	unsigned int bl;
25962306a36Sopenharmony_ci	int wimgxpp;
26062306a36Sopenharmony_ci	struct ppc_bat *bat;
26162306a36Sopenharmony_ci	unsigned long flags = pgprot_val(prot);
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_ci	if (index == -1)
26462306a36Sopenharmony_ci		index = find_free_bat();
26562306a36Sopenharmony_ci	if (index == -1) {
26662306a36Sopenharmony_ci		pr_err("%s: no BAT available for mapping 0x%llx\n", __func__,
26762306a36Sopenharmony_ci		       (unsigned long long)phys);
26862306a36Sopenharmony_ci		return;
26962306a36Sopenharmony_ci	}
27062306a36Sopenharmony_ci	bat = BATS[index];
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ci	if ((flags & _PAGE_NO_CACHE) ||
27362306a36Sopenharmony_ci	    (cpu_has_feature(CPU_FTR_NEED_COHERENT) == 0))
27462306a36Sopenharmony_ci		flags &= ~_PAGE_COHERENT;
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ci	bl = (size >> 17) - 1;
27762306a36Sopenharmony_ci	/* Do DBAT first */
27862306a36Sopenharmony_ci	wimgxpp = flags & (_PAGE_WRITETHRU | _PAGE_NO_CACHE
27962306a36Sopenharmony_ci			   | _PAGE_COHERENT | _PAGE_GUARDED);
28062306a36Sopenharmony_ci	wimgxpp |= (flags & _PAGE_RW)? BPP_RW: BPP_RX;
28162306a36Sopenharmony_ci	bat[1].batu = virt | (bl << 2) | 2; /* Vs=1, Vp=0 */
28262306a36Sopenharmony_ci	bat[1].batl = BAT_PHYS_ADDR(phys) | wimgxpp;
28362306a36Sopenharmony_ci	if (flags & _PAGE_USER)
28462306a36Sopenharmony_ci		bat[1].batu |= 1; 	/* Vp = 1 */
28562306a36Sopenharmony_ci	if (flags & _PAGE_GUARDED) {
28662306a36Sopenharmony_ci		/* G bit must be zero in IBATs */
28762306a36Sopenharmony_ci		flags &= ~_PAGE_EXEC;
28862306a36Sopenharmony_ci	}
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ci	bat_addrs[index].start = virt;
29162306a36Sopenharmony_ci	bat_addrs[index].limit = virt + ((bl + 1) << 17) - 1;
29262306a36Sopenharmony_ci	bat_addrs[index].phys = phys;
29362306a36Sopenharmony_ci}
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ci/*
29662306a36Sopenharmony_ci * Preload a translation in the hash table
29762306a36Sopenharmony_ci */
29862306a36Sopenharmony_cistatic void hash_preload(struct mm_struct *mm, unsigned long ea)
29962306a36Sopenharmony_ci{
30062306a36Sopenharmony_ci	pmd_t *pmd;
30162306a36Sopenharmony_ci
30262306a36Sopenharmony_ci	if (!mmu_has_feature(MMU_FTR_HPTE_TABLE))
30362306a36Sopenharmony_ci		return;
30462306a36Sopenharmony_ci	pmd = pmd_off(mm, ea);
30562306a36Sopenharmony_ci	if (!pmd_none(*pmd))
30662306a36Sopenharmony_ci		add_hash_page(mm->context.id, ea, pmd_val(*pmd));
30762306a36Sopenharmony_ci}
30862306a36Sopenharmony_ci
30962306a36Sopenharmony_ci/*
31062306a36Sopenharmony_ci * This is called at the end of handling a user page fault, when the
31162306a36Sopenharmony_ci * fault has been handled by updating a PTE in the linux page tables.
31262306a36Sopenharmony_ci * We use it to preload an HPTE into the hash table corresponding to
31362306a36Sopenharmony_ci * the updated linux PTE.
31462306a36Sopenharmony_ci *
31562306a36Sopenharmony_ci * This must always be called with the pte lock held.
31662306a36Sopenharmony_ci */
31762306a36Sopenharmony_civoid __update_mmu_cache(struct vm_area_struct *vma, unsigned long address,
31862306a36Sopenharmony_ci		      pte_t *ptep)
31962306a36Sopenharmony_ci{
32062306a36Sopenharmony_ci	/*
32162306a36Sopenharmony_ci	 * We don't need to worry about _PAGE_PRESENT here because we are
32262306a36Sopenharmony_ci	 * called with either mm->page_table_lock held or ptl lock held
32362306a36Sopenharmony_ci	 */
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_ci	/* We only want HPTEs for linux PTEs that have _PAGE_ACCESSED set */
32662306a36Sopenharmony_ci	if (!pte_young(*ptep) || address >= TASK_SIZE)
32762306a36Sopenharmony_ci		return;
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_ci	/* We have to test for regs NULL since init will get here first thing at boot */
33062306a36Sopenharmony_ci	if (!current->thread.regs)
33162306a36Sopenharmony_ci		return;
33262306a36Sopenharmony_ci
33362306a36Sopenharmony_ci	/* We also avoid filling the hash if not coming from a fault */
33462306a36Sopenharmony_ci	if (TRAP(current->thread.regs) != 0x300 && TRAP(current->thread.regs) != 0x400)
33562306a36Sopenharmony_ci		return;
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_ci	hash_preload(vma->vm_mm, address);
33862306a36Sopenharmony_ci}
33962306a36Sopenharmony_ci
34062306a36Sopenharmony_ci/*
34162306a36Sopenharmony_ci * Initialize the hash table and patch the instructions in hashtable.S.
34262306a36Sopenharmony_ci */
34362306a36Sopenharmony_civoid __init MMU_init_hw(void)
34462306a36Sopenharmony_ci{
34562306a36Sopenharmony_ci	unsigned int n_hpteg, lg_n_hpteg;
34662306a36Sopenharmony_ci
34762306a36Sopenharmony_ci	if (!mmu_has_feature(MMU_FTR_HPTE_TABLE))
34862306a36Sopenharmony_ci		return;
34962306a36Sopenharmony_ci
35062306a36Sopenharmony_ci	if ( ppc_md.progress ) ppc_md.progress("hash:enter", 0x105);
35162306a36Sopenharmony_ci
35262306a36Sopenharmony_ci#define LG_HPTEG_SIZE	6		/* 64 bytes per HPTEG */
35362306a36Sopenharmony_ci#define SDR1_LOW_BITS	((n_hpteg - 1) >> 10)
35462306a36Sopenharmony_ci#define MIN_N_HPTEG	1024		/* min 64kB hash table */
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_ci	/*
35762306a36Sopenharmony_ci	 * Allow 1 HPTE (1/8 HPTEG) for each page of memory.
35862306a36Sopenharmony_ci	 * This is less than the recommended amount, but then
35962306a36Sopenharmony_ci	 * Linux ain't AIX.
36062306a36Sopenharmony_ci	 */
36162306a36Sopenharmony_ci	n_hpteg = total_memory / (PAGE_SIZE * 8);
36262306a36Sopenharmony_ci	if (n_hpteg < MIN_N_HPTEG)
36362306a36Sopenharmony_ci		n_hpteg = MIN_N_HPTEG;
36462306a36Sopenharmony_ci	lg_n_hpteg = __ilog2(n_hpteg);
36562306a36Sopenharmony_ci	if (n_hpteg & (n_hpteg - 1)) {
36662306a36Sopenharmony_ci		++lg_n_hpteg;		/* round up if not power of 2 */
36762306a36Sopenharmony_ci		n_hpteg = 1 << lg_n_hpteg;
36862306a36Sopenharmony_ci	}
36962306a36Sopenharmony_ci	Hash_size = n_hpteg << LG_HPTEG_SIZE;
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_ci	/*
37262306a36Sopenharmony_ci	 * Find some memory for the hash table.
37362306a36Sopenharmony_ci	 */
37462306a36Sopenharmony_ci	if ( ppc_md.progress ) ppc_md.progress("hash:find piece", 0x322);
37562306a36Sopenharmony_ci	Hash = memblock_alloc(Hash_size, Hash_size);
37662306a36Sopenharmony_ci	if (!Hash)
37762306a36Sopenharmony_ci		panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
37862306a36Sopenharmony_ci		      __func__, Hash_size, Hash_size);
37962306a36Sopenharmony_ci	_SDR1 = __pa(Hash) | SDR1_LOW_BITS;
38062306a36Sopenharmony_ci
38162306a36Sopenharmony_ci	pr_info("Total memory = %lldMB; using %ldkB for hash table\n",
38262306a36Sopenharmony_ci		(unsigned long long)(total_memory >> 20), Hash_size >> 10);
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_ci
38562306a36Sopenharmony_ci	Hash_mask = n_hpteg - 1;
38662306a36Sopenharmony_ci	hash_mb2 = hash_mb = 32 - LG_HPTEG_SIZE - lg_n_hpteg;
38762306a36Sopenharmony_ci	if (lg_n_hpteg > 16)
38862306a36Sopenharmony_ci		hash_mb2 = 16 - LG_HPTEG_SIZE;
38962306a36Sopenharmony_ci}
39062306a36Sopenharmony_ci
39162306a36Sopenharmony_civoid __init MMU_init_hw_patch(void)
39262306a36Sopenharmony_ci{
39362306a36Sopenharmony_ci	unsigned int hmask = Hash_mask >> (16 - LG_HPTEG_SIZE);
39462306a36Sopenharmony_ci	unsigned int hash = (unsigned int)Hash - PAGE_OFFSET;
39562306a36Sopenharmony_ci
39662306a36Sopenharmony_ci	if (!mmu_has_feature(MMU_FTR_HPTE_TABLE))
39762306a36Sopenharmony_ci		return;
39862306a36Sopenharmony_ci
39962306a36Sopenharmony_ci	if (ppc_md.progress)
40062306a36Sopenharmony_ci		ppc_md.progress("hash:patch", 0x345);
40162306a36Sopenharmony_ci	if (ppc_md.progress)
40262306a36Sopenharmony_ci		ppc_md.progress("hash:done", 0x205);
40362306a36Sopenharmony_ci
40462306a36Sopenharmony_ci	/* WARNING: Make sure nothing can trigger a KASAN check past this point */
40562306a36Sopenharmony_ci
40662306a36Sopenharmony_ci	/*
40762306a36Sopenharmony_ci	 * Patch up the instructions in hashtable.S:create_hpte
40862306a36Sopenharmony_ci	 */
40962306a36Sopenharmony_ci	modify_instruction_site(&patch__hash_page_A0, 0xffff, hash >> 16);
41062306a36Sopenharmony_ci	modify_instruction_site(&patch__hash_page_A1, 0x7c0, hash_mb << 6);
41162306a36Sopenharmony_ci	modify_instruction_site(&patch__hash_page_A2, 0x7c0, hash_mb2 << 6);
41262306a36Sopenharmony_ci	modify_instruction_site(&patch__hash_page_B, 0xffff, hmask);
41362306a36Sopenharmony_ci	modify_instruction_site(&patch__hash_page_C, 0xffff, hmask);
41462306a36Sopenharmony_ci
41562306a36Sopenharmony_ci	/*
41662306a36Sopenharmony_ci	 * Patch up the instructions in hashtable.S:flush_hash_page
41762306a36Sopenharmony_ci	 */
41862306a36Sopenharmony_ci	modify_instruction_site(&patch__flush_hash_A0, 0xffff, hash >> 16);
41962306a36Sopenharmony_ci	modify_instruction_site(&patch__flush_hash_A1, 0x7c0, hash_mb << 6);
42062306a36Sopenharmony_ci	modify_instruction_site(&patch__flush_hash_A2, 0x7c0, hash_mb2 << 6);
42162306a36Sopenharmony_ci	modify_instruction_site(&patch__flush_hash_B, 0xffff, hmask);
42262306a36Sopenharmony_ci}
42362306a36Sopenharmony_ci
42462306a36Sopenharmony_civoid setup_initial_memory_limit(phys_addr_t first_memblock_base,
42562306a36Sopenharmony_ci				phys_addr_t first_memblock_size)
42662306a36Sopenharmony_ci{
42762306a36Sopenharmony_ci	/* We don't currently support the first MEMBLOCK not mapping 0
42862306a36Sopenharmony_ci	 * physical on those processors
42962306a36Sopenharmony_ci	 */
43062306a36Sopenharmony_ci	BUG_ON(first_memblock_base != 0);
43162306a36Sopenharmony_ci
43262306a36Sopenharmony_ci	memblock_set_current_limit(min_t(u64, first_memblock_size, SZ_256M));
43362306a36Sopenharmony_ci}
43462306a36Sopenharmony_ci
43562306a36Sopenharmony_civoid __init print_system_hash_info(void)
43662306a36Sopenharmony_ci{
43762306a36Sopenharmony_ci	pr_info("Hash_size         = 0x%lx\n", Hash_size);
43862306a36Sopenharmony_ci	if (Hash_mask)
43962306a36Sopenharmony_ci		pr_info("Hash_mask         = 0x%lx\n", Hash_mask);
44062306a36Sopenharmony_ci}
44162306a36Sopenharmony_ci
44262306a36Sopenharmony_civoid __init early_init_mmu(void)
44362306a36Sopenharmony_ci{
44462306a36Sopenharmony_ci}
445