xref: /kernel/linux/linux-5.10/arch/mips/mm/tlb-r4k.c (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public
38c2ecf20Sopenharmony_ci * License.  See the file "COPYING" in the main directory of this archive
48c2ecf20Sopenharmony_ci * for more details.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 1996 David S. Miller (davem@davemloft.net)
78c2ecf20Sopenharmony_ci * Copyright (C) 1997, 1998, 1999, 2000 Ralf Baechle ralf@gnu.org
88c2ecf20Sopenharmony_ci * Carsten Langgaard, carstenl@mips.com
98c2ecf20Sopenharmony_ci * Copyright (C) 2002 MIPS Technologies, Inc.  All rights reserved.
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci#include <linux/cpu_pm.h>
128c2ecf20Sopenharmony_ci#include <linux/init.h>
138c2ecf20Sopenharmony_ci#include <linux/sched.h>
148c2ecf20Sopenharmony_ci#include <linux/smp.h>
158c2ecf20Sopenharmony_ci#include <linux/mm.h>
168c2ecf20Sopenharmony_ci#include <linux/hugetlb.h>
178c2ecf20Sopenharmony_ci#include <linux/export.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <asm/cpu.h>
208c2ecf20Sopenharmony_ci#include <asm/cpu-type.h>
218c2ecf20Sopenharmony_ci#include <asm/bootinfo.h>
228c2ecf20Sopenharmony_ci#include <asm/hazards.h>
238c2ecf20Sopenharmony_ci#include <asm/mmu_context.h>
248c2ecf20Sopenharmony_ci#include <asm/tlb.h>
258c2ecf20Sopenharmony_ci#include <asm/tlbmisc.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ciextern void build_tlb_refill_handler(void);
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/*
308c2ecf20Sopenharmony_ci * LOONGSON-2 has a 4 entry itlb which is a subset of jtlb, LOONGSON-3 has
318c2ecf20Sopenharmony_ci * a 4 entry itlb and a 4 entry dtlb which are subsets of jtlb. Unfortunately,
328c2ecf20Sopenharmony_ci * itlb/dtlb are not totally transparent to software.
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_cistatic inline void flush_micro_tlb(void)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	switch (current_cpu_type()) {
378c2ecf20Sopenharmony_ci	case CPU_LOONGSON2EF:
388c2ecf20Sopenharmony_ci		write_c0_diag(LOONGSON_DIAG_ITLB);
398c2ecf20Sopenharmony_ci		break;
408c2ecf20Sopenharmony_ci	case CPU_LOONGSON64:
418c2ecf20Sopenharmony_ci		write_c0_diag(LOONGSON_DIAG_ITLB | LOONGSON_DIAG_DTLB);
428c2ecf20Sopenharmony_ci		break;
438c2ecf20Sopenharmony_ci	default:
448c2ecf20Sopenharmony_ci		break;
458c2ecf20Sopenharmony_ci	}
468c2ecf20Sopenharmony_ci}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic inline void flush_micro_tlb_vm(struct vm_area_struct *vma)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	if (vma->vm_flags & VM_EXEC)
518c2ecf20Sopenharmony_ci		flush_micro_tlb();
528c2ecf20Sopenharmony_ci}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_civoid local_flush_tlb_all(void)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	unsigned long flags;
578c2ecf20Sopenharmony_ci	unsigned long old_ctx;
588c2ecf20Sopenharmony_ci	int entry, ftlbhighset;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	local_irq_save(flags);
618c2ecf20Sopenharmony_ci	/* Save old context and create impossible VPN2 value */
628c2ecf20Sopenharmony_ci	old_ctx = read_c0_entryhi();
638c2ecf20Sopenharmony_ci	htw_stop();
648c2ecf20Sopenharmony_ci	write_c0_entrylo0(0);
658c2ecf20Sopenharmony_ci	write_c0_entrylo1(0);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	entry = num_wired_entries();
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	/*
708c2ecf20Sopenharmony_ci	 * Blast 'em all away.
718c2ecf20Sopenharmony_ci	 * If there are any wired entries, fall back to iterating
728c2ecf20Sopenharmony_ci	 */
738c2ecf20Sopenharmony_ci	if (cpu_has_tlbinv && !entry) {
748c2ecf20Sopenharmony_ci		if (current_cpu_data.tlbsizevtlb) {
758c2ecf20Sopenharmony_ci			write_c0_index(0);
768c2ecf20Sopenharmony_ci			mtc0_tlbw_hazard();
778c2ecf20Sopenharmony_ci			tlbinvf();  /* invalidate VTLB */
788c2ecf20Sopenharmony_ci		}
798c2ecf20Sopenharmony_ci		ftlbhighset = current_cpu_data.tlbsizevtlb +
808c2ecf20Sopenharmony_ci			current_cpu_data.tlbsizeftlbsets;
818c2ecf20Sopenharmony_ci		for (entry = current_cpu_data.tlbsizevtlb;
828c2ecf20Sopenharmony_ci		     entry < ftlbhighset;
838c2ecf20Sopenharmony_ci		     entry++) {
848c2ecf20Sopenharmony_ci			write_c0_index(entry);
858c2ecf20Sopenharmony_ci			mtc0_tlbw_hazard();
868c2ecf20Sopenharmony_ci			tlbinvf();  /* invalidate one FTLB set */
878c2ecf20Sopenharmony_ci		}
888c2ecf20Sopenharmony_ci	} else {
898c2ecf20Sopenharmony_ci		while (entry < current_cpu_data.tlbsize) {
908c2ecf20Sopenharmony_ci			/* Make sure all entries differ. */
918c2ecf20Sopenharmony_ci			write_c0_entryhi(UNIQUE_ENTRYHI(entry));
928c2ecf20Sopenharmony_ci			write_c0_index(entry);
938c2ecf20Sopenharmony_ci			mtc0_tlbw_hazard();
948c2ecf20Sopenharmony_ci			tlb_write_indexed();
958c2ecf20Sopenharmony_ci			entry++;
968c2ecf20Sopenharmony_ci		}
978c2ecf20Sopenharmony_ci	}
988c2ecf20Sopenharmony_ci	tlbw_use_hazard();
998c2ecf20Sopenharmony_ci	write_c0_entryhi(old_ctx);
1008c2ecf20Sopenharmony_ci	htw_start();
1018c2ecf20Sopenharmony_ci	flush_micro_tlb();
1028c2ecf20Sopenharmony_ci	local_irq_restore(flags);
1038c2ecf20Sopenharmony_ci}
1048c2ecf20Sopenharmony_ciEXPORT_SYMBOL(local_flush_tlb_all);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_civoid local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
1078c2ecf20Sopenharmony_ci	unsigned long end)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	struct mm_struct *mm = vma->vm_mm;
1108c2ecf20Sopenharmony_ci	int cpu = smp_processor_id();
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	if (cpu_context(cpu, mm) != 0) {
1138c2ecf20Sopenharmony_ci		unsigned long size, flags;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci		local_irq_save(flags);
1168c2ecf20Sopenharmony_ci		start = round_down(start, PAGE_SIZE << 1);
1178c2ecf20Sopenharmony_ci		end = round_up(end, PAGE_SIZE << 1);
1188c2ecf20Sopenharmony_ci		size = (end - start) >> (PAGE_SHIFT + 1);
1198c2ecf20Sopenharmony_ci		if (size <= (current_cpu_data.tlbsizeftlbsets ?
1208c2ecf20Sopenharmony_ci			     current_cpu_data.tlbsize / 8 :
1218c2ecf20Sopenharmony_ci			     current_cpu_data.tlbsize / 2)) {
1228c2ecf20Sopenharmony_ci			unsigned long old_entryhi, old_mmid;
1238c2ecf20Sopenharmony_ci			int newpid = cpu_asid(cpu, mm);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci			old_entryhi = read_c0_entryhi();
1268c2ecf20Sopenharmony_ci			if (cpu_has_mmid) {
1278c2ecf20Sopenharmony_ci				old_mmid = read_c0_memorymapid();
1288c2ecf20Sopenharmony_ci				write_c0_memorymapid(newpid);
1298c2ecf20Sopenharmony_ci			}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci			htw_stop();
1328c2ecf20Sopenharmony_ci			while (start < end) {
1338c2ecf20Sopenharmony_ci				int idx;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci				if (cpu_has_mmid)
1368c2ecf20Sopenharmony_ci					write_c0_entryhi(start);
1378c2ecf20Sopenharmony_ci				else
1388c2ecf20Sopenharmony_ci					write_c0_entryhi(start | newpid);
1398c2ecf20Sopenharmony_ci				start += (PAGE_SIZE << 1);
1408c2ecf20Sopenharmony_ci				mtc0_tlbw_hazard();
1418c2ecf20Sopenharmony_ci				tlb_probe();
1428c2ecf20Sopenharmony_ci				tlb_probe_hazard();
1438c2ecf20Sopenharmony_ci				idx = read_c0_index();
1448c2ecf20Sopenharmony_ci				write_c0_entrylo0(0);
1458c2ecf20Sopenharmony_ci				write_c0_entrylo1(0);
1468c2ecf20Sopenharmony_ci				if (idx < 0)
1478c2ecf20Sopenharmony_ci					continue;
1488c2ecf20Sopenharmony_ci				/* Make sure all entries differ. */
1498c2ecf20Sopenharmony_ci				write_c0_entryhi(UNIQUE_ENTRYHI(idx));
1508c2ecf20Sopenharmony_ci				mtc0_tlbw_hazard();
1518c2ecf20Sopenharmony_ci				tlb_write_indexed();
1528c2ecf20Sopenharmony_ci			}
1538c2ecf20Sopenharmony_ci			tlbw_use_hazard();
1548c2ecf20Sopenharmony_ci			write_c0_entryhi(old_entryhi);
1558c2ecf20Sopenharmony_ci			if (cpu_has_mmid)
1568c2ecf20Sopenharmony_ci				write_c0_memorymapid(old_mmid);
1578c2ecf20Sopenharmony_ci			htw_start();
1588c2ecf20Sopenharmony_ci		} else {
1598c2ecf20Sopenharmony_ci			drop_mmu_context(mm);
1608c2ecf20Sopenharmony_ci		}
1618c2ecf20Sopenharmony_ci		flush_micro_tlb();
1628c2ecf20Sopenharmony_ci		local_irq_restore(flags);
1638c2ecf20Sopenharmony_ci	}
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_civoid local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	unsigned long size, flags;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	local_irq_save(flags);
1718c2ecf20Sopenharmony_ci	size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
1728c2ecf20Sopenharmony_ci	size = (size + 1) >> 1;
1738c2ecf20Sopenharmony_ci	if (size <= (current_cpu_data.tlbsizeftlbsets ?
1748c2ecf20Sopenharmony_ci		     current_cpu_data.tlbsize / 8 :
1758c2ecf20Sopenharmony_ci		     current_cpu_data.tlbsize / 2)) {
1768c2ecf20Sopenharmony_ci		int pid = read_c0_entryhi();
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci		start &= (PAGE_MASK << 1);
1798c2ecf20Sopenharmony_ci		end += ((PAGE_SIZE << 1) - 1);
1808c2ecf20Sopenharmony_ci		end &= (PAGE_MASK << 1);
1818c2ecf20Sopenharmony_ci		htw_stop();
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci		while (start < end) {
1848c2ecf20Sopenharmony_ci			int idx;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci			write_c0_entryhi(start);
1878c2ecf20Sopenharmony_ci			start += (PAGE_SIZE << 1);
1888c2ecf20Sopenharmony_ci			mtc0_tlbw_hazard();
1898c2ecf20Sopenharmony_ci			tlb_probe();
1908c2ecf20Sopenharmony_ci			tlb_probe_hazard();
1918c2ecf20Sopenharmony_ci			idx = read_c0_index();
1928c2ecf20Sopenharmony_ci			write_c0_entrylo0(0);
1938c2ecf20Sopenharmony_ci			write_c0_entrylo1(0);
1948c2ecf20Sopenharmony_ci			if (idx < 0)
1958c2ecf20Sopenharmony_ci				continue;
1968c2ecf20Sopenharmony_ci			/* Make sure all entries differ. */
1978c2ecf20Sopenharmony_ci			write_c0_entryhi(UNIQUE_ENTRYHI(idx));
1988c2ecf20Sopenharmony_ci			mtc0_tlbw_hazard();
1998c2ecf20Sopenharmony_ci			tlb_write_indexed();
2008c2ecf20Sopenharmony_ci		}
2018c2ecf20Sopenharmony_ci		tlbw_use_hazard();
2028c2ecf20Sopenharmony_ci		write_c0_entryhi(pid);
2038c2ecf20Sopenharmony_ci		htw_start();
2048c2ecf20Sopenharmony_ci	} else {
2058c2ecf20Sopenharmony_ci		local_flush_tlb_all();
2068c2ecf20Sopenharmony_ci	}
2078c2ecf20Sopenharmony_ci	flush_micro_tlb();
2088c2ecf20Sopenharmony_ci	local_irq_restore(flags);
2098c2ecf20Sopenharmony_ci}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_civoid local_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
2128c2ecf20Sopenharmony_ci{
2138c2ecf20Sopenharmony_ci	int cpu = smp_processor_id();
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	if (cpu_context(cpu, vma->vm_mm) != 0) {
2168c2ecf20Sopenharmony_ci		unsigned long old_mmid;
2178c2ecf20Sopenharmony_ci		unsigned long flags, old_entryhi;
2188c2ecf20Sopenharmony_ci		int idx;
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci		page &= (PAGE_MASK << 1);
2218c2ecf20Sopenharmony_ci		local_irq_save(flags);
2228c2ecf20Sopenharmony_ci		old_entryhi = read_c0_entryhi();
2238c2ecf20Sopenharmony_ci		htw_stop();
2248c2ecf20Sopenharmony_ci		if (cpu_has_mmid) {
2258c2ecf20Sopenharmony_ci			old_mmid = read_c0_memorymapid();
2268c2ecf20Sopenharmony_ci			write_c0_entryhi(page);
2278c2ecf20Sopenharmony_ci			write_c0_memorymapid(cpu_asid(cpu, vma->vm_mm));
2288c2ecf20Sopenharmony_ci		} else {
2298c2ecf20Sopenharmony_ci			write_c0_entryhi(page | cpu_asid(cpu, vma->vm_mm));
2308c2ecf20Sopenharmony_ci		}
2318c2ecf20Sopenharmony_ci		mtc0_tlbw_hazard();
2328c2ecf20Sopenharmony_ci		tlb_probe();
2338c2ecf20Sopenharmony_ci		tlb_probe_hazard();
2348c2ecf20Sopenharmony_ci		idx = read_c0_index();
2358c2ecf20Sopenharmony_ci		write_c0_entrylo0(0);
2368c2ecf20Sopenharmony_ci		write_c0_entrylo1(0);
2378c2ecf20Sopenharmony_ci		if (idx < 0)
2388c2ecf20Sopenharmony_ci			goto finish;
2398c2ecf20Sopenharmony_ci		/* Make sure all entries differ. */
2408c2ecf20Sopenharmony_ci		write_c0_entryhi(UNIQUE_ENTRYHI(idx));
2418c2ecf20Sopenharmony_ci		mtc0_tlbw_hazard();
2428c2ecf20Sopenharmony_ci		tlb_write_indexed();
2438c2ecf20Sopenharmony_ci		tlbw_use_hazard();
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	finish:
2468c2ecf20Sopenharmony_ci		write_c0_entryhi(old_entryhi);
2478c2ecf20Sopenharmony_ci		if (cpu_has_mmid)
2488c2ecf20Sopenharmony_ci			write_c0_memorymapid(old_mmid);
2498c2ecf20Sopenharmony_ci		htw_start();
2508c2ecf20Sopenharmony_ci		flush_micro_tlb_vm(vma);
2518c2ecf20Sopenharmony_ci		local_irq_restore(flags);
2528c2ecf20Sopenharmony_ci	}
2538c2ecf20Sopenharmony_ci}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci/*
2568c2ecf20Sopenharmony_ci * This one is only used for pages with the global bit set so we don't care
2578c2ecf20Sopenharmony_ci * much about the ASID.
2588c2ecf20Sopenharmony_ci */
2598c2ecf20Sopenharmony_civoid local_flush_tlb_one(unsigned long page)
2608c2ecf20Sopenharmony_ci{
2618c2ecf20Sopenharmony_ci	unsigned long flags;
2628c2ecf20Sopenharmony_ci	int oldpid, idx;
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	local_irq_save(flags);
2658c2ecf20Sopenharmony_ci	oldpid = read_c0_entryhi();
2668c2ecf20Sopenharmony_ci	htw_stop();
2678c2ecf20Sopenharmony_ci	page &= (PAGE_MASK << 1);
2688c2ecf20Sopenharmony_ci	write_c0_entryhi(page);
2698c2ecf20Sopenharmony_ci	mtc0_tlbw_hazard();
2708c2ecf20Sopenharmony_ci	tlb_probe();
2718c2ecf20Sopenharmony_ci	tlb_probe_hazard();
2728c2ecf20Sopenharmony_ci	idx = read_c0_index();
2738c2ecf20Sopenharmony_ci	write_c0_entrylo0(0);
2748c2ecf20Sopenharmony_ci	write_c0_entrylo1(0);
2758c2ecf20Sopenharmony_ci	if (idx >= 0) {
2768c2ecf20Sopenharmony_ci		/* Make sure all entries differ. */
2778c2ecf20Sopenharmony_ci		write_c0_entryhi(UNIQUE_ENTRYHI(idx));
2788c2ecf20Sopenharmony_ci		mtc0_tlbw_hazard();
2798c2ecf20Sopenharmony_ci		tlb_write_indexed();
2808c2ecf20Sopenharmony_ci		tlbw_use_hazard();
2818c2ecf20Sopenharmony_ci	}
2828c2ecf20Sopenharmony_ci	write_c0_entryhi(oldpid);
2838c2ecf20Sopenharmony_ci	htw_start();
2848c2ecf20Sopenharmony_ci	flush_micro_tlb();
2858c2ecf20Sopenharmony_ci	local_irq_restore(flags);
2868c2ecf20Sopenharmony_ci}
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci/*
2898c2ecf20Sopenharmony_ci * We will need multiple versions of update_mmu_cache(), one that just
2908c2ecf20Sopenharmony_ci * updates the TLB with the new pte(s), and another which also checks
2918c2ecf20Sopenharmony_ci * for the R4k "end of page" hardware bug and does the needy.
2928c2ecf20Sopenharmony_ci */
2938c2ecf20Sopenharmony_civoid __update_tlb(struct vm_area_struct * vma, unsigned long address, pte_t pte)
2948c2ecf20Sopenharmony_ci{
2958c2ecf20Sopenharmony_ci	unsigned long flags;
2968c2ecf20Sopenharmony_ci	pgd_t *pgdp;
2978c2ecf20Sopenharmony_ci	p4d_t *p4dp;
2988c2ecf20Sopenharmony_ci	pud_t *pudp;
2998c2ecf20Sopenharmony_ci	pmd_t *pmdp;
3008c2ecf20Sopenharmony_ci	pte_t *ptep;
3018c2ecf20Sopenharmony_ci	int idx, pid;
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	/*
3048c2ecf20Sopenharmony_ci	 * Handle debugger faulting in for debugee.
3058c2ecf20Sopenharmony_ci	 */
3068c2ecf20Sopenharmony_ci	if (current->active_mm != vma->vm_mm)
3078c2ecf20Sopenharmony_ci		return;
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	local_irq_save(flags);
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	htw_stop();
3128c2ecf20Sopenharmony_ci	address &= (PAGE_MASK << 1);
3138c2ecf20Sopenharmony_ci	if (cpu_has_mmid) {
3148c2ecf20Sopenharmony_ci		write_c0_entryhi(address);
3158c2ecf20Sopenharmony_ci	} else {
3168c2ecf20Sopenharmony_ci		pid = read_c0_entryhi() & cpu_asid_mask(&current_cpu_data);
3178c2ecf20Sopenharmony_ci		write_c0_entryhi(address | pid);
3188c2ecf20Sopenharmony_ci	}
3198c2ecf20Sopenharmony_ci	pgdp = pgd_offset(vma->vm_mm, address);
3208c2ecf20Sopenharmony_ci	mtc0_tlbw_hazard();
3218c2ecf20Sopenharmony_ci	tlb_probe();
3228c2ecf20Sopenharmony_ci	tlb_probe_hazard();
3238c2ecf20Sopenharmony_ci	p4dp = p4d_offset(pgdp, address);
3248c2ecf20Sopenharmony_ci	pudp = pud_offset(p4dp, address);
3258c2ecf20Sopenharmony_ci	pmdp = pmd_offset(pudp, address);
3268c2ecf20Sopenharmony_ci	idx = read_c0_index();
3278c2ecf20Sopenharmony_ci#ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
3288c2ecf20Sopenharmony_ci	/* this could be a huge page  */
3298c2ecf20Sopenharmony_ci	if (pmd_huge(*pmdp)) {
3308c2ecf20Sopenharmony_ci		unsigned long lo;
3318c2ecf20Sopenharmony_ci		write_c0_pagemask(PM_HUGE_MASK);
3328c2ecf20Sopenharmony_ci		ptep = (pte_t *)pmdp;
3338c2ecf20Sopenharmony_ci		lo = pte_to_entrylo(pte_val(*ptep));
3348c2ecf20Sopenharmony_ci		write_c0_entrylo0(lo);
3358c2ecf20Sopenharmony_ci		write_c0_entrylo1(lo + (HPAGE_SIZE >> 7));
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci		mtc0_tlbw_hazard();
3388c2ecf20Sopenharmony_ci		if (idx < 0)
3398c2ecf20Sopenharmony_ci			tlb_write_random();
3408c2ecf20Sopenharmony_ci		else
3418c2ecf20Sopenharmony_ci			tlb_write_indexed();
3428c2ecf20Sopenharmony_ci		tlbw_use_hazard();
3438c2ecf20Sopenharmony_ci		write_c0_pagemask(PM_DEFAULT_MASK);
3448c2ecf20Sopenharmony_ci	} else
3458c2ecf20Sopenharmony_ci#endif
3468c2ecf20Sopenharmony_ci	{
3478c2ecf20Sopenharmony_ci		ptep = pte_offset_map(pmdp, address);
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci#if defined(CONFIG_PHYS_ADDR_T_64BIT) && defined(CONFIG_CPU_MIPS32)
3508c2ecf20Sopenharmony_ci#ifdef CONFIG_XPA
3518c2ecf20Sopenharmony_ci		write_c0_entrylo0(pte_to_entrylo(ptep->pte_high));
3528c2ecf20Sopenharmony_ci		if (cpu_has_xpa)
3538c2ecf20Sopenharmony_ci			writex_c0_entrylo0(ptep->pte_low & _PFNX_MASK);
3548c2ecf20Sopenharmony_ci		ptep++;
3558c2ecf20Sopenharmony_ci		write_c0_entrylo1(pte_to_entrylo(ptep->pte_high));
3568c2ecf20Sopenharmony_ci		if (cpu_has_xpa)
3578c2ecf20Sopenharmony_ci			writex_c0_entrylo1(ptep->pte_low & _PFNX_MASK);
3588c2ecf20Sopenharmony_ci#else
3598c2ecf20Sopenharmony_ci		write_c0_entrylo0(ptep->pte_high);
3608c2ecf20Sopenharmony_ci		ptep++;
3618c2ecf20Sopenharmony_ci		write_c0_entrylo1(ptep->pte_high);
3628c2ecf20Sopenharmony_ci#endif
3638c2ecf20Sopenharmony_ci#else
3648c2ecf20Sopenharmony_ci		write_c0_entrylo0(pte_to_entrylo(pte_val(*ptep++)));
3658c2ecf20Sopenharmony_ci		write_c0_entrylo1(pte_to_entrylo(pte_val(*ptep)));
3668c2ecf20Sopenharmony_ci#endif
3678c2ecf20Sopenharmony_ci		mtc0_tlbw_hazard();
3688c2ecf20Sopenharmony_ci		if (idx < 0)
3698c2ecf20Sopenharmony_ci			tlb_write_random();
3708c2ecf20Sopenharmony_ci		else
3718c2ecf20Sopenharmony_ci			tlb_write_indexed();
3728c2ecf20Sopenharmony_ci	}
3738c2ecf20Sopenharmony_ci	tlbw_use_hazard();
3748c2ecf20Sopenharmony_ci	htw_start();
3758c2ecf20Sopenharmony_ci	flush_micro_tlb_vm(vma);
3768c2ecf20Sopenharmony_ci	local_irq_restore(flags);
3778c2ecf20Sopenharmony_ci}
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_civoid add_wired_entry(unsigned long entrylo0, unsigned long entrylo1,
3808c2ecf20Sopenharmony_ci		     unsigned long entryhi, unsigned long pagemask)
3818c2ecf20Sopenharmony_ci{
3828c2ecf20Sopenharmony_ci#ifdef CONFIG_XPA
3838c2ecf20Sopenharmony_ci	panic("Broken for XPA kernels");
3848c2ecf20Sopenharmony_ci#else
3858c2ecf20Sopenharmony_ci	unsigned int old_mmid;
3868c2ecf20Sopenharmony_ci	unsigned long flags;
3878c2ecf20Sopenharmony_ci	unsigned long wired;
3888c2ecf20Sopenharmony_ci	unsigned long old_pagemask;
3898c2ecf20Sopenharmony_ci	unsigned long old_ctx;
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	local_irq_save(flags);
3928c2ecf20Sopenharmony_ci	if (cpu_has_mmid) {
3938c2ecf20Sopenharmony_ci		old_mmid = read_c0_memorymapid();
3948c2ecf20Sopenharmony_ci		write_c0_memorymapid(MMID_KERNEL_WIRED);
3958c2ecf20Sopenharmony_ci	}
3968c2ecf20Sopenharmony_ci	/* Save old context and create impossible VPN2 value */
3978c2ecf20Sopenharmony_ci	old_ctx = read_c0_entryhi();
3988c2ecf20Sopenharmony_ci	htw_stop();
3998c2ecf20Sopenharmony_ci	old_pagemask = read_c0_pagemask();
4008c2ecf20Sopenharmony_ci	wired = num_wired_entries();
4018c2ecf20Sopenharmony_ci	write_c0_wired(wired + 1);
4028c2ecf20Sopenharmony_ci	write_c0_index(wired);
4038c2ecf20Sopenharmony_ci	tlbw_use_hazard();	/* What is the hazard here? */
4048c2ecf20Sopenharmony_ci	write_c0_pagemask(pagemask);
4058c2ecf20Sopenharmony_ci	write_c0_entryhi(entryhi);
4068c2ecf20Sopenharmony_ci	write_c0_entrylo0(entrylo0);
4078c2ecf20Sopenharmony_ci	write_c0_entrylo1(entrylo1);
4088c2ecf20Sopenharmony_ci	mtc0_tlbw_hazard();
4098c2ecf20Sopenharmony_ci	tlb_write_indexed();
4108c2ecf20Sopenharmony_ci	tlbw_use_hazard();
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	write_c0_entryhi(old_ctx);
4138c2ecf20Sopenharmony_ci	if (cpu_has_mmid)
4148c2ecf20Sopenharmony_ci		write_c0_memorymapid(old_mmid);
4158c2ecf20Sopenharmony_ci	tlbw_use_hazard();	/* What is the hazard here? */
4168c2ecf20Sopenharmony_ci	htw_start();
4178c2ecf20Sopenharmony_ci	write_c0_pagemask(old_pagemask);
4188c2ecf20Sopenharmony_ci	local_flush_tlb_all();
4198c2ecf20Sopenharmony_ci	local_irq_restore(flags);
4208c2ecf20Sopenharmony_ci#endif
4218c2ecf20Sopenharmony_ci}
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci#ifdef CONFIG_TRANSPARENT_HUGEPAGE
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ciint has_transparent_hugepage(void)
4268c2ecf20Sopenharmony_ci{
4278c2ecf20Sopenharmony_ci	static unsigned int mask = -1;
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	if (mask == -1) {	/* first call comes during __init */
4308c2ecf20Sopenharmony_ci		unsigned long flags;
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci		local_irq_save(flags);
4338c2ecf20Sopenharmony_ci		write_c0_pagemask(PM_HUGE_MASK);
4348c2ecf20Sopenharmony_ci		back_to_back_c0_hazard();
4358c2ecf20Sopenharmony_ci		mask = read_c0_pagemask();
4368c2ecf20Sopenharmony_ci		write_c0_pagemask(PM_DEFAULT_MASK);
4378c2ecf20Sopenharmony_ci		local_irq_restore(flags);
4388c2ecf20Sopenharmony_ci	}
4398c2ecf20Sopenharmony_ci	return mask == PM_HUGE_MASK;
4408c2ecf20Sopenharmony_ci}
4418c2ecf20Sopenharmony_ciEXPORT_SYMBOL(has_transparent_hugepage);
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci#endif /* CONFIG_TRANSPARENT_HUGEPAGE  */
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci/*
4468c2ecf20Sopenharmony_ci * Used for loading TLB entries before trap_init() has started, when we
4478c2ecf20Sopenharmony_ci * don't actually want to add a wired entry which remains throughout the
4488c2ecf20Sopenharmony_ci * lifetime of the system
4498c2ecf20Sopenharmony_ci */
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ciint temp_tlb_entry;
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci__init int add_temporary_entry(unsigned long entrylo0, unsigned long entrylo1,
4548c2ecf20Sopenharmony_ci			       unsigned long entryhi, unsigned long pagemask)
4558c2ecf20Sopenharmony_ci{
4568c2ecf20Sopenharmony_ci	int ret = 0;
4578c2ecf20Sopenharmony_ci	unsigned long flags;
4588c2ecf20Sopenharmony_ci	unsigned long wired;
4598c2ecf20Sopenharmony_ci	unsigned long old_pagemask;
4608c2ecf20Sopenharmony_ci	unsigned long old_ctx;
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	local_irq_save(flags);
4638c2ecf20Sopenharmony_ci	/* Save old context and create impossible VPN2 value */
4648c2ecf20Sopenharmony_ci	htw_stop();
4658c2ecf20Sopenharmony_ci	old_ctx = read_c0_entryhi();
4668c2ecf20Sopenharmony_ci	old_pagemask = read_c0_pagemask();
4678c2ecf20Sopenharmony_ci	wired = num_wired_entries();
4688c2ecf20Sopenharmony_ci	if (--temp_tlb_entry < wired) {
4698c2ecf20Sopenharmony_ci		printk(KERN_WARNING
4708c2ecf20Sopenharmony_ci		       "No TLB space left for add_temporary_entry\n");
4718c2ecf20Sopenharmony_ci		ret = -ENOSPC;
4728c2ecf20Sopenharmony_ci		goto out;
4738c2ecf20Sopenharmony_ci	}
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	write_c0_index(temp_tlb_entry);
4768c2ecf20Sopenharmony_ci	write_c0_pagemask(pagemask);
4778c2ecf20Sopenharmony_ci	write_c0_entryhi(entryhi);
4788c2ecf20Sopenharmony_ci	write_c0_entrylo0(entrylo0);
4798c2ecf20Sopenharmony_ci	write_c0_entrylo1(entrylo1);
4808c2ecf20Sopenharmony_ci	mtc0_tlbw_hazard();
4818c2ecf20Sopenharmony_ci	tlb_write_indexed();
4828c2ecf20Sopenharmony_ci	tlbw_use_hazard();
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	write_c0_entryhi(old_ctx);
4858c2ecf20Sopenharmony_ci	write_c0_pagemask(old_pagemask);
4868c2ecf20Sopenharmony_ci	htw_start();
4878c2ecf20Sopenharmony_ciout:
4888c2ecf20Sopenharmony_ci	local_irq_restore(flags);
4898c2ecf20Sopenharmony_ci	return ret;
4908c2ecf20Sopenharmony_ci}
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_cistatic int ntlb;
4938c2ecf20Sopenharmony_cistatic int __init set_ntlb(char *str)
4948c2ecf20Sopenharmony_ci{
4958c2ecf20Sopenharmony_ci	get_option(&str, &ntlb);
4968c2ecf20Sopenharmony_ci	return 1;
4978c2ecf20Sopenharmony_ci}
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci__setup("ntlb=", set_ntlb);
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci/*
5028c2ecf20Sopenharmony_ci * Configure TLB (for init or after a CPU has been powered off).
5038c2ecf20Sopenharmony_ci */
5048c2ecf20Sopenharmony_cistatic void r4k_tlb_configure(void)
5058c2ecf20Sopenharmony_ci{
5068c2ecf20Sopenharmony_ci	/*
5078c2ecf20Sopenharmony_ci	 * You should never change this register:
5088c2ecf20Sopenharmony_ci	 *   - On R4600 1.7 the tlbp never hits for pages smaller than
5098c2ecf20Sopenharmony_ci	 *     the value in the c0_pagemask register.
5108c2ecf20Sopenharmony_ci	 *   - The entire mm handling assumes the c0_pagemask register to
5118c2ecf20Sopenharmony_ci	 *     be set to fixed-size pages.
5128c2ecf20Sopenharmony_ci	 */
5138c2ecf20Sopenharmony_ci	write_c0_pagemask(PM_DEFAULT_MASK);
5148c2ecf20Sopenharmony_ci	back_to_back_c0_hazard();
5158c2ecf20Sopenharmony_ci	if (read_c0_pagemask() != PM_DEFAULT_MASK)
5168c2ecf20Sopenharmony_ci		panic("MMU doesn't support PAGE_SIZE=0x%lx", PAGE_SIZE);
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	write_c0_wired(0);
5198c2ecf20Sopenharmony_ci	if (current_cpu_type() == CPU_R10000 ||
5208c2ecf20Sopenharmony_ci	    current_cpu_type() == CPU_R12000 ||
5218c2ecf20Sopenharmony_ci	    current_cpu_type() == CPU_R14000 ||
5228c2ecf20Sopenharmony_ci	    current_cpu_type() == CPU_R16000)
5238c2ecf20Sopenharmony_ci		write_c0_framemask(0);
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci	if (cpu_has_rixi) {
5268c2ecf20Sopenharmony_ci		/*
5278c2ecf20Sopenharmony_ci		 * Enable the no read, no exec bits, and enable large physical
5288c2ecf20Sopenharmony_ci		 * address.
5298c2ecf20Sopenharmony_ci		 */
5308c2ecf20Sopenharmony_ci#ifdef CONFIG_64BIT
5318c2ecf20Sopenharmony_ci		set_c0_pagegrain(PG_RIE | PG_XIE | PG_ELPA);
5328c2ecf20Sopenharmony_ci#else
5338c2ecf20Sopenharmony_ci		set_c0_pagegrain(PG_RIE | PG_XIE);
5348c2ecf20Sopenharmony_ci#endif
5358c2ecf20Sopenharmony_ci	}
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	temp_tlb_entry = current_cpu_data.tlbsize - 1;
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	/* From this point on the ARC firmware is dead.	 */
5408c2ecf20Sopenharmony_ci	local_flush_tlb_all();
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci	/* Did I tell you that ARC SUCKS?  */
5438c2ecf20Sopenharmony_ci}
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_civoid tlb_init(void)
5468c2ecf20Sopenharmony_ci{
5478c2ecf20Sopenharmony_ci	r4k_tlb_configure();
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	if (ntlb) {
5508c2ecf20Sopenharmony_ci		if (ntlb > 1 && ntlb <= current_cpu_data.tlbsize) {
5518c2ecf20Sopenharmony_ci			int wired = current_cpu_data.tlbsize - ntlb;
5528c2ecf20Sopenharmony_ci			write_c0_wired(wired);
5538c2ecf20Sopenharmony_ci			write_c0_index(wired-1);
5548c2ecf20Sopenharmony_ci			printk("Restricting TLB to %d entries\n", ntlb);
5558c2ecf20Sopenharmony_ci		} else
5568c2ecf20Sopenharmony_ci			printk("Ignoring invalid argument ntlb=%d\n", ntlb);
5578c2ecf20Sopenharmony_ci	}
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	build_tlb_refill_handler();
5608c2ecf20Sopenharmony_ci}
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_cistatic int r4k_tlb_pm_notifier(struct notifier_block *self, unsigned long cmd,
5638c2ecf20Sopenharmony_ci			       void *v)
5648c2ecf20Sopenharmony_ci{
5658c2ecf20Sopenharmony_ci	switch (cmd) {
5668c2ecf20Sopenharmony_ci	case CPU_PM_ENTER_FAILED:
5678c2ecf20Sopenharmony_ci	case CPU_PM_EXIT:
5688c2ecf20Sopenharmony_ci		r4k_tlb_configure();
5698c2ecf20Sopenharmony_ci		break;
5708c2ecf20Sopenharmony_ci	}
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci	return NOTIFY_OK;
5738c2ecf20Sopenharmony_ci}
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_cistatic struct notifier_block r4k_tlb_pm_notifier_block = {
5768c2ecf20Sopenharmony_ci	.notifier_call = r4k_tlb_pm_notifier,
5778c2ecf20Sopenharmony_ci};
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_cistatic int __init r4k_tlb_init_pm(void)
5808c2ecf20Sopenharmony_ci{
5818c2ecf20Sopenharmony_ci	return cpu_pm_register_notifier(&r4k_tlb_pm_notifier_block);
5828c2ecf20Sopenharmony_ci}
5838c2ecf20Sopenharmony_ciarch_initcall(r4k_tlb_init_pm);
584