18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2020 Loongson Technology Corporation Limited
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci#ifndef __ASM_TLB_H
68c2ecf20Sopenharmony_ci#define __ASM_TLB_H
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/mm_types.h>
98c2ecf20Sopenharmony_ci#include <asm/cpu-features.h>
108c2ecf20Sopenharmony_ci#include <asm/loongarchregs.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci/*
138c2ecf20Sopenharmony_ci * TLB Invalidate Flush
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_cistatic inline void tlbclr(void)
168c2ecf20Sopenharmony_ci{
178c2ecf20Sopenharmony_ci	__asm__ __volatile__("tlbclr");
188c2ecf20Sopenharmony_ci}
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic inline void tlbflush(void)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	__asm__ __volatile__("tlbflush");
238c2ecf20Sopenharmony_ci}
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/*
268c2ecf20Sopenharmony_ci * TLB R/W operations.
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_cistatic inline void tlb_probe(void)
298c2ecf20Sopenharmony_ci{
308c2ecf20Sopenharmony_ci	__asm__ __volatile__("tlbsrch");
318c2ecf20Sopenharmony_ci}
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic inline void tlb_read(void)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	__asm__ __volatile__("tlbrd");
368c2ecf20Sopenharmony_ci}
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic inline void tlb_write_indexed(void)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	__asm__ __volatile__("tlbwr");
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic inline void tlb_write_random(void)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	__asm__ __volatile__("tlbfill");
468c2ecf20Sopenharmony_ci}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cienum invtlb_ops {
498c2ecf20Sopenharmony_ci	/* Invalid all tlb */
508c2ecf20Sopenharmony_ci	INVTLB_ALL = 0x0,
518c2ecf20Sopenharmony_ci	/* Invalid current tlb */
528c2ecf20Sopenharmony_ci	INVTLB_CURRENT_ALL = 0x1,
538c2ecf20Sopenharmony_ci	/* Invalid all global=1 lines in current tlb */
548c2ecf20Sopenharmony_ci	INVTLB_CURRENT_GTRUE = 0x2,
558c2ecf20Sopenharmony_ci	/* Invalid all global=0 lines in current tlb */
568c2ecf20Sopenharmony_ci	INVTLB_CURRENT_GFALSE = 0x3,
578c2ecf20Sopenharmony_ci	/* Invalid global=0 and matched asid lines in current tlb */
588c2ecf20Sopenharmony_ci	INVTLB_GFALSE_AND_ASID = 0x4,
598c2ecf20Sopenharmony_ci	/* Invalid addr with global=0 and matched asid in current tlb */
608c2ecf20Sopenharmony_ci	INVTLB_ADDR_GFALSE_AND_ASID = 0x5,
618c2ecf20Sopenharmony_ci	/* Invalid addr with global=1 or matched asid in current tlb */
628c2ecf20Sopenharmony_ci	INVTLB_ADDR_GTRUE_OR_ASID = 0x6,
638c2ecf20Sopenharmony_ci	/* Invalid matched gid in guest tlb */
648c2ecf20Sopenharmony_ci	INVGTLB_GID = 0x9,
658c2ecf20Sopenharmony_ci	/* Invalid global=1, matched gid in guest tlb */
668c2ecf20Sopenharmony_ci	INVGTLB_GID_GTRUE = 0xa,
678c2ecf20Sopenharmony_ci	/* Invalid global=0, matched gid in guest tlb */
688c2ecf20Sopenharmony_ci	INVGTLB_GID_GFALSE = 0xb,
698c2ecf20Sopenharmony_ci	/* Invalid global=0, matched gid and asid in guest tlb */
708c2ecf20Sopenharmony_ci	INVGTLB_GID_GFALSE_ASID = 0xc,
718c2ecf20Sopenharmony_ci	/* Invalid global=0 , matched gid, asid and addr in guest tlb */
728c2ecf20Sopenharmony_ci	INVGTLB_GID_GFALSE_ASID_ADDR = 0xd,
738c2ecf20Sopenharmony_ci	/* Invalid global=1 , matched gid, asid and addr in guest tlb */
748c2ecf20Sopenharmony_ci	INVGTLB_GID_GTRUE_ASID_ADDR = 0xe,
758c2ecf20Sopenharmony_ci	/* Invalid all gid gva-->gpa guest tlb */
768c2ecf20Sopenharmony_ci	INVGTLB_ALLGID_GVA_TO_GPA = 0x10,
778c2ecf20Sopenharmony_ci	/* Invalid all gid gpa-->hpa tlb */
788c2ecf20Sopenharmony_ci	INVTLB_ALLGID_GPA_TO_HPA = 0x11,
798c2ecf20Sopenharmony_ci	/* Invalid all gid tlb, including  gva-->gpa and gpa-->hpa */
808c2ecf20Sopenharmony_ci	INVTLB_ALLGID = 0x12,
818c2ecf20Sopenharmony_ci	/* Invalid matched gid gva-->gpa guest tlb */
828c2ecf20Sopenharmony_ci	INVGTLB_GID_GVA_TO_GPA = 0x13,
838c2ecf20Sopenharmony_ci	/* Invalid matched gid gpa-->hpa tlb */
848c2ecf20Sopenharmony_ci	INVTLB_GID_GPA_TO_HPA = 0x14,
858c2ecf20Sopenharmony_ci	/* Invalid matched gid tlb,including gva-->gpa and gpa-->hpa */
868c2ecf20Sopenharmony_ci	INVTLB_GID_ALL = 0x15,
878c2ecf20Sopenharmony_ci	/* Invalid matched gid and addr gpa-->hpa tlb */
888c2ecf20Sopenharmony_ci	INVTLB_GID_ADDR = 0x16,
898c2ecf20Sopenharmony_ci};
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic __always_inline void invtlb(u32 op, u32 info, u64 addr)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	__asm__ __volatile__(
948c2ecf20Sopenharmony_ci		"invtlb %0, %1, %2\n\t"
958c2ecf20Sopenharmony_ci		:
968c2ecf20Sopenharmony_ci		: "i"(op), "r"(info), "r"(addr)
978c2ecf20Sopenharmony_ci		: "memory"
988c2ecf20Sopenharmony_ci		);
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistatic __always_inline void invtlb_addr(u32 op, u32 info, u64 addr)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	BUILD_BUG_ON(!__builtin_constant_p(info) || info != 0);
1048c2ecf20Sopenharmony_ci	__asm__ __volatile__(
1058c2ecf20Sopenharmony_ci		"invtlb %0, $zero, %1\n\t"
1068c2ecf20Sopenharmony_ci		:
1078c2ecf20Sopenharmony_ci		: "i"(op), "r"(addr)
1088c2ecf20Sopenharmony_ci		: "memory"
1098c2ecf20Sopenharmony_ci		);
1108c2ecf20Sopenharmony_ci}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistatic __always_inline void invtlb_info(u32 op, u32 info, u64 addr)
1138c2ecf20Sopenharmony_ci{
1148c2ecf20Sopenharmony_ci	BUILD_BUG_ON(!__builtin_constant_p(addr) || addr != 0);
1158c2ecf20Sopenharmony_ci	__asm__ __volatile__(
1168c2ecf20Sopenharmony_ci		"invtlb %0, %1, $zero\n\t"
1178c2ecf20Sopenharmony_ci		:
1188c2ecf20Sopenharmony_ci		: "i"(op), "r"(info)
1198c2ecf20Sopenharmony_ci		: "memory"
1208c2ecf20Sopenharmony_ci		);
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic __always_inline void invtlb_all(u32 op, u32 info, u64 addr)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	BUILD_BUG_ON(!__builtin_constant_p(info) || info != 0);
1268c2ecf20Sopenharmony_ci	BUILD_BUG_ON(!__builtin_constant_p(addr) || addr != 0);
1278c2ecf20Sopenharmony_ci	__asm__ __volatile__(
1288c2ecf20Sopenharmony_ci		"invtlb %0, $zero, $zero\n\t"
1298c2ecf20Sopenharmony_ci		:
1308c2ecf20Sopenharmony_ci		: "i"(op)
1318c2ecf20Sopenharmony_ci		: "memory"
1328c2ecf20Sopenharmony_ci		);
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci/*
1368c2ecf20Sopenharmony_ci * LoongArch doesn't need any special per-pte or per-vma handling, except
1378c2ecf20Sopenharmony_ci * we need to flush cache for area to be unmapped.
1388c2ecf20Sopenharmony_ci */
1398c2ecf20Sopenharmony_ci#define tlb_start_vma(tlb, vma)					\
1408c2ecf20Sopenharmony_ci	do {							\
1418c2ecf20Sopenharmony_ci		if (!(tlb)->fullmm)				\
1428c2ecf20Sopenharmony_ci			flush_cache_range(vma, vma->vm_start, vma->vm_end); \
1438c2ecf20Sopenharmony_ci	}  while (0)
1448c2ecf20Sopenharmony_ci#define tlb_end_vma(tlb, vma) do { } while (0)
1458c2ecf20Sopenharmony_ci#define __tlb_remove_tlb_entry(tlb, ptep, address) do { } while (0)
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_cistatic void tlb_flush(struct mmu_gather *tlb);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci#define tlb_flush tlb_flush
1508c2ecf20Sopenharmony_ci#include <asm-generic/tlb.h>
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic inline void tlb_flush(struct mmu_gather *tlb)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	struct vm_area_struct vma;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	vma.vm_mm = tlb->mm;
1578c2ecf20Sopenharmony_ci	vma.vm_flags = 0;
1588c2ecf20Sopenharmony_ci	if (tlb->fullmm) {
1598c2ecf20Sopenharmony_ci		flush_tlb_mm(tlb->mm);
1608c2ecf20Sopenharmony_ci		return;
1618c2ecf20Sopenharmony_ci	}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	flush_tlb_range(&vma, tlb->start, tlb->end);
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ciextern void handle_tlb_load(void);
1678c2ecf20Sopenharmony_ciextern void handle_tlb_store(void);
1688c2ecf20Sopenharmony_ciextern void handle_tlb_modify(void);
1698c2ecf20Sopenharmony_ciextern void handle_tlb_refill(void);
1708c2ecf20Sopenharmony_ciextern void handle_tlb_protect(void);
1718c2ecf20Sopenharmony_ciextern void handle_tlb_load_ptw(void);
1728c2ecf20Sopenharmony_ciextern void handle_tlb_store_ptw(void);
1738c2ecf20Sopenharmony_ciextern void handle_tlb_modify_ptw(void);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ciextern void dump_tlb_all(void);
1768c2ecf20Sopenharmony_ciextern void dump_tlb_regs(void);
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci#endif /* __ASM_TLB_H */
179