18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Modifications by Matt Porter (mporter@mvista.com) to support
48c2ecf20Sopenharmony_ci * PPC44x Book E processors.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * This file contains the routines for initializing the MMU
78c2ecf20Sopenharmony_ci * on the 4xx series of chips.
88c2ecf20Sopenharmony_ci *  -- paulus
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci *  Derived from arch/ppc/mm/init.c:
118c2ecf20Sopenharmony_ci *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci *  Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
148c2ecf20Sopenharmony_ci *  and Cort Dougan (PReP) (cort@cs.nmt.edu)
158c2ecf20Sopenharmony_ci *    Copyright (C) 1996 Paul Mackerras
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci *  Derived from "arch/i386/mm/init.c"
188c2ecf20Sopenharmony_ci *    Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
198c2ecf20Sopenharmony_ci */
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include <linux/init.h>
228c2ecf20Sopenharmony_ci#include <linux/memblock.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include <asm/mmu.h>
258c2ecf20Sopenharmony_ci#include <asm/page.h>
268c2ecf20Sopenharmony_ci#include <asm/cacheflush.h>
278c2ecf20Sopenharmony_ci#include <asm/code-patching.h>
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#include <mm/mmu_decl.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/* Used by the 44x TLB replacement exception handler.
328c2ecf20Sopenharmony_ci * Just needed it declared someplace.
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_ciunsigned int tlb_44x_index; /* = 0 */
358c2ecf20Sopenharmony_ciunsigned int tlb_44x_hwater = PPC44x_TLB_SIZE - 1 - PPC44x_EARLY_TLBS;
368c2ecf20Sopenharmony_ciint icache_44x_need_flush;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ciunsigned long tlb_47x_boltmap[1024/8];
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic void ppc44x_update_tlb_hwater(void)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	/* The TLB miss handlers hard codes the watermark in a cmpli
438c2ecf20Sopenharmony_ci	 * instruction to improve performances rather than loading it
448c2ecf20Sopenharmony_ci	 * from the global variable. Thus, we patch the instructions
458c2ecf20Sopenharmony_ci	 * in the 2 TLB miss handlers when updating the value
468c2ecf20Sopenharmony_ci	 */
478c2ecf20Sopenharmony_ci	modify_instruction_site(&patch__tlb_44x_hwater_D, 0xffff, tlb_44x_hwater);
488c2ecf20Sopenharmony_ci	modify_instruction_site(&patch__tlb_44x_hwater_I, 0xffff, tlb_44x_hwater);
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/*
528c2ecf20Sopenharmony_ci * "Pins" a 256MB TLB entry in AS0 for kernel lowmem for 44x type MMU
538c2ecf20Sopenharmony_ci */
548c2ecf20Sopenharmony_cistatic void __init ppc44x_pin_tlb(unsigned int virt, unsigned int phys)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	unsigned int entry = tlb_44x_hwater--;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	ppc44x_update_tlb_hwater();
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	mtspr(SPRN_MMUCR, 0);
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	__asm__ __volatile__(
638c2ecf20Sopenharmony_ci		"tlbwe	%2,%3,%4\n"
648c2ecf20Sopenharmony_ci		"tlbwe	%1,%3,%5\n"
658c2ecf20Sopenharmony_ci		"tlbwe	%0,%3,%6\n"
668c2ecf20Sopenharmony_ci	:
678c2ecf20Sopenharmony_ci	: "r" (PPC44x_TLB_SW | PPC44x_TLB_SR | PPC44x_TLB_SX | PPC44x_TLB_G),
688c2ecf20Sopenharmony_ci	  "r" (phys),
698c2ecf20Sopenharmony_ci	  "r" (virt | PPC44x_TLB_VALID | PPC44x_TLB_256M),
708c2ecf20Sopenharmony_ci	  "r" (entry),
718c2ecf20Sopenharmony_ci	  "i" (PPC44x_TLB_PAGEID),
728c2ecf20Sopenharmony_ci	  "i" (PPC44x_TLB_XLAT),
738c2ecf20Sopenharmony_ci	  "i" (PPC44x_TLB_ATTRIB));
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic int __init ppc47x_find_free_bolted(void)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	unsigned int mmube0 = mfspr(SPRN_MMUBE0);
798c2ecf20Sopenharmony_ci	unsigned int mmube1 = mfspr(SPRN_MMUBE1);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	if (!(mmube0 & MMUBE0_VBE0))
828c2ecf20Sopenharmony_ci		return 0;
838c2ecf20Sopenharmony_ci	if (!(mmube0 & MMUBE0_VBE1))
848c2ecf20Sopenharmony_ci		return 1;
858c2ecf20Sopenharmony_ci	if (!(mmube0 & MMUBE0_VBE2))
868c2ecf20Sopenharmony_ci		return 2;
878c2ecf20Sopenharmony_ci	if (!(mmube1 & MMUBE1_VBE3))
888c2ecf20Sopenharmony_ci		return 3;
898c2ecf20Sopenharmony_ci	if (!(mmube1 & MMUBE1_VBE4))
908c2ecf20Sopenharmony_ci		return 4;
918c2ecf20Sopenharmony_ci	if (!(mmube1 & MMUBE1_VBE5))
928c2ecf20Sopenharmony_ci		return 5;
938c2ecf20Sopenharmony_ci	return -1;
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistatic void __init ppc47x_update_boltmap(void)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	unsigned int mmube0 = mfspr(SPRN_MMUBE0);
998c2ecf20Sopenharmony_ci	unsigned int mmube1 = mfspr(SPRN_MMUBE1);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	if (mmube0 & MMUBE0_VBE0)
1028c2ecf20Sopenharmony_ci		__set_bit((mmube0 >> MMUBE0_IBE0_SHIFT) & 0xff,
1038c2ecf20Sopenharmony_ci			  tlb_47x_boltmap);
1048c2ecf20Sopenharmony_ci	if (mmube0 & MMUBE0_VBE1)
1058c2ecf20Sopenharmony_ci		__set_bit((mmube0 >> MMUBE0_IBE1_SHIFT) & 0xff,
1068c2ecf20Sopenharmony_ci			  tlb_47x_boltmap);
1078c2ecf20Sopenharmony_ci	if (mmube0 & MMUBE0_VBE2)
1088c2ecf20Sopenharmony_ci		__set_bit((mmube0 >> MMUBE0_IBE2_SHIFT) & 0xff,
1098c2ecf20Sopenharmony_ci			  tlb_47x_boltmap);
1108c2ecf20Sopenharmony_ci	if (mmube1 & MMUBE1_VBE3)
1118c2ecf20Sopenharmony_ci		__set_bit((mmube1 >> MMUBE1_IBE3_SHIFT) & 0xff,
1128c2ecf20Sopenharmony_ci			  tlb_47x_boltmap);
1138c2ecf20Sopenharmony_ci	if (mmube1 & MMUBE1_VBE4)
1148c2ecf20Sopenharmony_ci		__set_bit((mmube1 >> MMUBE1_IBE4_SHIFT) & 0xff,
1158c2ecf20Sopenharmony_ci			  tlb_47x_boltmap);
1168c2ecf20Sopenharmony_ci	if (mmube1 & MMUBE1_VBE5)
1178c2ecf20Sopenharmony_ci		__set_bit((mmube1 >> MMUBE1_IBE5_SHIFT) & 0xff,
1188c2ecf20Sopenharmony_ci			  tlb_47x_boltmap);
1198c2ecf20Sopenharmony_ci}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci/*
1228c2ecf20Sopenharmony_ci * "Pins" a 256MB TLB entry in AS0 for kernel lowmem for 47x type MMU
1238c2ecf20Sopenharmony_ci */
1248c2ecf20Sopenharmony_cistatic void ppc47x_pin_tlb(unsigned int virt, unsigned int phys)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	unsigned int rA;
1278c2ecf20Sopenharmony_ci	int bolted;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	/* Base rA is HW way select, way 0, bolted bit set */
1308c2ecf20Sopenharmony_ci	rA = 0x88000000;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	/* Look for a bolted entry slot */
1338c2ecf20Sopenharmony_ci	bolted = ppc47x_find_free_bolted();
1348c2ecf20Sopenharmony_ci	BUG_ON(bolted < 0);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	/* Insert bolted slot number */
1378c2ecf20Sopenharmony_ci	rA |= bolted << 24;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	pr_debug("256M TLB entry for 0x%08x->0x%08x in bolt slot %d\n",
1408c2ecf20Sopenharmony_ci		 virt, phys, bolted);
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	mtspr(SPRN_MMUCR, 0);
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	__asm__ __volatile__(
1458c2ecf20Sopenharmony_ci		"tlbwe	%2,%3,0\n"
1468c2ecf20Sopenharmony_ci		"tlbwe	%1,%3,1\n"
1478c2ecf20Sopenharmony_ci		"tlbwe	%0,%3,2\n"
1488c2ecf20Sopenharmony_ci		:
1498c2ecf20Sopenharmony_ci		: "r" (PPC47x_TLB2_SW | PPC47x_TLB2_SR |
1508c2ecf20Sopenharmony_ci		       PPC47x_TLB2_SX
1518c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
1528c2ecf20Sopenharmony_ci		       | PPC47x_TLB2_M
1538c2ecf20Sopenharmony_ci#endif
1548c2ecf20Sopenharmony_ci		       ),
1558c2ecf20Sopenharmony_ci		  "r" (phys),
1568c2ecf20Sopenharmony_ci		  "r" (virt | PPC47x_TLB0_VALID | PPC47x_TLB0_256M),
1578c2ecf20Sopenharmony_ci		  "r" (rA));
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_civoid __init MMU_init_hw(void)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	/* This is not useful on 47x but won't hurt either */
1638c2ecf20Sopenharmony_ci	ppc44x_update_tlb_hwater();
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	flush_instruction_cache();
1668c2ecf20Sopenharmony_ci}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ciunsigned long __init mmu_mapin_ram(unsigned long base, unsigned long top)
1698c2ecf20Sopenharmony_ci{
1708c2ecf20Sopenharmony_ci	unsigned long addr;
1718c2ecf20Sopenharmony_ci	unsigned long memstart = memstart_addr & ~(PPC_PIN_SIZE - 1);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	/* Pin in enough TLBs to cover any lowmem not covered by the
1748c2ecf20Sopenharmony_ci	 * initial 256M mapping established in head_44x.S */
1758c2ecf20Sopenharmony_ci	for (addr = memstart + PPC_PIN_SIZE; addr < lowmem_end_addr;
1768c2ecf20Sopenharmony_ci	     addr += PPC_PIN_SIZE) {
1778c2ecf20Sopenharmony_ci		if (mmu_has_feature(MMU_FTR_TYPE_47x))
1788c2ecf20Sopenharmony_ci			ppc47x_pin_tlb(addr + PAGE_OFFSET, addr);
1798c2ecf20Sopenharmony_ci		else
1808c2ecf20Sopenharmony_ci			ppc44x_pin_tlb(addr + PAGE_OFFSET, addr);
1818c2ecf20Sopenharmony_ci	}
1828c2ecf20Sopenharmony_ci	if (mmu_has_feature(MMU_FTR_TYPE_47x)) {
1838c2ecf20Sopenharmony_ci		ppc47x_update_boltmap();
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci#ifdef DEBUG
1868c2ecf20Sopenharmony_ci		{
1878c2ecf20Sopenharmony_ci			int i;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci			printk(KERN_DEBUG "bolted entries: ");
1908c2ecf20Sopenharmony_ci			for (i = 0; i < 255; i++) {
1918c2ecf20Sopenharmony_ci				if (test_bit(i, tlb_47x_boltmap))
1928c2ecf20Sopenharmony_ci					printk("%d ", i);
1938c2ecf20Sopenharmony_ci			}
1948c2ecf20Sopenharmony_ci			printk("\n");
1958c2ecf20Sopenharmony_ci		}
1968c2ecf20Sopenharmony_ci#endif /* DEBUG */
1978c2ecf20Sopenharmony_ci	}
1988c2ecf20Sopenharmony_ci	return total_lowmem;
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_civoid setup_initial_memory_limit(phys_addr_t first_memblock_base,
2028c2ecf20Sopenharmony_ci				phys_addr_t first_memblock_size)
2038c2ecf20Sopenharmony_ci{
2048c2ecf20Sopenharmony_ci	u64 size;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci#ifndef CONFIG_NONSTATIC_KERNEL
2078c2ecf20Sopenharmony_ci	/* We don't currently support the first MEMBLOCK not mapping 0
2088c2ecf20Sopenharmony_ci	 * physical on those processors
2098c2ecf20Sopenharmony_ci	 */
2108c2ecf20Sopenharmony_ci	BUG_ON(first_memblock_base != 0);
2118c2ecf20Sopenharmony_ci#endif
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	/* 44x has a 256M TLB entry pinned at boot */
2148c2ecf20Sopenharmony_ci	size = (min_t(u64, first_memblock_size, PPC_PIN_SIZE));
2158c2ecf20Sopenharmony_ci	memblock_set_current_limit(first_memblock_base + size);
2168c2ecf20Sopenharmony_ci}
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
2198c2ecf20Sopenharmony_civoid __init mmu_init_secondary(int cpu)
2208c2ecf20Sopenharmony_ci{
2218c2ecf20Sopenharmony_ci	unsigned long addr;
2228c2ecf20Sopenharmony_ci	unsigned long memstart = memstart_addr & ~(PPC_PIN_SIZE - 1);
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	/* Pin in enough TLBs to cover any lowmem not covered by the
2258c2ecf20Sopenharmony_ci	 * initial 256M mapping established in head_44x.S
2268c2ecf20Sopenharmony_ci	 *
2278c2ecf20Sopenharmony_ci	 * WARNING: This is called with only the first 256M of the
2288c2ecf20Sopenharmony_ci	 * linear mapping in the TLB and we can't take faults yet
2298c2ecf20Sopenharmony_ci	 * so beware of what this code uses. It runs off a temporary
2308c2ecf20Sopenharmony_ci	 * stack. current (r2) isn't initialized, smp_processor_id()
2318c2ecf20Sopenharmony_ci	 * will not work, current thread info isn't accessible, ...
2328c2ecf20Sopenharmony_ci	 */
2338c2ecf20Sopenharmony_ci	for (addr = memstart + PPC_PIN_SIZE; addr < lowmem_end_addr;
2348c2ecf20Sopenharmony_ci	     addr += PPC_PIN_SIZE) {
2358c2ecf20Sopenharmony_ci		if (mmu_has_feature(MMU_FTR_TYPE_47x))
2368c2ecf20Sopenharmony_ci			ppc47x_pin_tlb(addr + PAGE_OFFSET, addr);
2378c2ecf20Sopenharmony_ci		else
2388c2ecf20Sopenharmony_ci			ppc44x_pin_tlb(addr + PAGE_OFFSET, addr);
2398c2ecf20Sopenharmony_ci	}
2408c2ecf20Sopenharmony_ci}
2418c2ecf20Sopenharmony_ci#endif /* CONFIG_SMP */
242