162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2012,2013 - ARM Ltd
462306a36Sopenharmony_ci * Author: Marc Zyngier <marc.zyngier@arm.com>
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#ifndef __ARM64_KVM_ARM_H__
862306a36Sopenharmony_ci#define __ARM64_KVM_ARM_H__
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <asm/esr.h>
1162306a36Sopenharmony_ci#include <asm/memory.h>
1262306a36Sopenharmony_ci#include <asm/sysreg.h>
1362306a36Sopenharmony_ci#include <asm/types.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci/* Hyp Configuration Register (HCR) bits */
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#define HCR_TID5	(UL(1) << 58)
1862306a36Sopenharmony_ci#define HCR_DCT		(UL(1) << 57)
1962306a36Sopenharmony_ci#define HCR_ATA_SHIFT	56
2062306a36Sopenharmony_ci#define HCR_ATA		(UL(1) << HCR_ATA_SHIFT)
2162306a36Sopenharmony_ci#define HCR_TTLBOS	(UL(1) << 55)
2262306a36Sopenharmony_ci#define HCR_TTLBIS	(UL(1) << 54)
2362306a36Sopenharmony_ci#define HCR_ENSCXT	(UL(1) << 53)
2462306a36Sopenharmony_ci#define HCR_TOCU	(UL(1) << 52)
2562306a36Sopenharmony_ci#define HCR_AMVOFFEN	(UL(1) << 51)
2662306a36Sopenharmony_ci#define HCR_TICAB	(UL(1) << 50)
2762306a36Sopenharmony_ci#define HCR_TID4	(UL(1) << 49)
2862306a36Sopenharmony_ci#define HCR_FIEN	(UL(1) << 47)
2962306a36Sopenharmony_ci#define HCR_FWB		(UL(1) << 46)
3062306a36Sopenharmony_ci#define HCR_NV2		(UL(1) << 45)
3162306a36Sopenharmony_ci#define HCR_AT		(UL(1) << 44)
3262306a36Sopenharmony_ci#define HCR_NV1		(UL(1) << 43)
3362306a36Sopenharmony_ci#define HCR_NV		(UL(1) << 42)
3462306a36Sopenharmony_ci#define HCR_API		(UL(1) << 41)
3562306a36Sopenharmony_ci#define HCR_APK		(UL(1) << 40)
3662306a36Sopenharmony_ci#define HCR_TEA		(UL(1) << 37)
3762306a36Sopenharmony_ci#define HCR_TERR	(UL(1) << 36)
3862306a36Sopenharmony_ci#define HCR_TLOR	(UL(1) << 35)
3962306a36Sopenharmony_ci#define HCR_E2H		(UL(1) << 34)
4062306a36Sopenharmony_ci#define HCR_ID		(UL(1) << 33)
4162306a36Sopenharmony_ci#define HCR_CD		(UL(1) << 32)
4262306a36Sopenharmony_ci#define HCR_RW_SHIFT	31
4362306a36Sopenharmony_ci#define HCR_RW		(UL(1) << HCR_RW_SHIFT)
4462306a36Sopenharmony_ci#define HCR_TRVM	(UL(1) << 30)
4562306a36Sopenharmony_ci#define HCR_HCD		(UL(1) << 29)
4662306a36Sopenharmony_ci#define HCR_TDZ		(UL(1) << 28)
4762306a36Sopenharmony_ci#define HCR_TGE		(UL(1) << 27)
4862306a36Sopenharmony_ci#define HCR_TVM		(UL(1) << 26)
4962306a36Sopenharmony_ci#define HCR_TTLB	(UL(1) << 25)
5062306a36Sopenharmony_ci#define HCR_TPU		(UL(1) << 24)
5162306a36Sopenharmony_ci#define HCR_TPC		(UL(1) << 23) /* HCR_TPCP if FEAT_DPB */
5262306a36Sopenharmony_ci#define HCR_TSW		(UL(1) << 22)
5362306a36Sopenharmony_ci#define HCR_TACR	(UL(1) << 21)
5462306a36Sopenharmony_ci#define HCR_TIDCP	(UL(1) << 20)
5562306a36Sopenharmony_ci#define HCR_TSC		(UL(1) << 19)
5662306a36Sopenharmony_ci#define HCR_TID3	(UL(1) << 18)
5762306a36Sopenharmony_ci#define HCR_TID2	(UL(1) << 17)
5862306a36Sopenharmony_ci#define HCR_TID1	(UL(1) << 16)
5962306a36Sopenharmony_ci#define HCR_TID0	(UL(1) << 15)
6062306a36Sopenharmony_ci#define HCR_TWE		(UL(1) << 14)
6162306a36Sopenharmony_ci#define HCR_TWI		(UL(1) << 13)
6262306a36Sopenharmony_ci#define HCR_DC		(UL(1) << 12)
6362306a36Sopenharmony_ci#define HCR_BSU		(3 << 10)
6462306a36Sopenharmony_ci#define HCR_BSU_IS	(UL(1) << 10)
6562306a36Sopenharmony_ci#define HCR_FB		(UL(1) << 9)
6662306a36Sopenharmony_ci#define HCR_VSE		(UL(1) << 8)
6762306a36Sopenharmony_ci#define HCR_VI		(UL(1) << 7)
6862306a36Sopenharmony_ci#define HCR_VF		(UL(1) << 6)
6962306a36Sopenharmony_ci#define HCR_AMO		(UL(1) << 5)
7062306a36Sopenharmony_ci#define HCR_IMO		(UL(1) << 4)
7162306a36Sopenharmony_ci#define HCR_FMO		(UL(1) << 3)
7262306a36Sopenharmony_ci#define HCR_PTW		(UL(1) << 2)
7362306a36Sopenharmony_ci#define HCR_SWIO	(UL(1) << 1)
7462306a36Sopenharmony_ci#define HCR_VM		(UL(1) << 0)
7562306a36Sopenharmony_ci#define HCR_RES0	((UL(1) << 48) | (UL(1) << 39))
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci/*
7862306a36Sopenharmony_ci * The bits we set in HCR:
7962306a36Sopenharmony_ci * TLOR:	Trap LORegion register accesses
8062306a36Sopenharmony_ci * RW:		64bit by default, can be overridden for 32bit VMs
8162306a36Sopenharmony_ci * TACR:	Trap ACTLR
8262306a36Sopenharmony_ci * TSC:		Trap SMC
8362306a36Sopenharmony_ci * TSW:		Trap cache operations by set/way
8462306a36Sopenharmony_ci * TWE:		Trap WFE
8562306a36Sopenharmony_ci * TWI:		Trap WFI
8662306a36Sopenharmony_ci * TIDCP:	Trap L2CTLR/L2ECTLR
8762306a36Sopenharmony_ci * BSU_IS:	Upgrade barriers to the inner shareable domain
8862306a36Sopenharmony_ci * FB:		Force broadcast of all maintenance operations
8962306a36Sopenharmony_ci * AMO:		Override CPSR.A and enable signaling with VA
9062306a36Sopenharmony_ci * IMO:		Override CPSR.I and enable signaling with VI
9162306a36Sopenharmony_ci * FMO:		Override CPSR.F and enable signaling with VF
9262306a36Sopenharmony_ci * SWIO:	Turn set/way invalidates into set/way clean+invalidate
9362306a36Sopenharmony_ci * PTW:		Take a stage2 fault if a stage1 walk steps in device memory
9462306a36Sopenharmony_ci * TID3:	Trap EL1 reads of group 3 ID registers
9562306a36Sopenharmony_ci * TID2:	Trap CTR_EL0, CCSIDR2_EL1, CLIDR_EL1, and CSSELR_EL1
9662306a36Sopenharmony_ci */
9762306a36Sopenharmony_ci#define HCR_GUEST_FLAGS (HCR_TSC | HCR_TSW | HCR_TWE | HCR_TWI | HCR_VM | \
9862306a36Sopenharmony_ci			 HCR_BSU_IS | HCR_FB | HCR_TACR | \
9962306a36Sopenharmony_ci			 HCR_AMO | HCR_SWIO | HCR_TIDCP | HCR_RW | HCR_TLOR | \
10062306a36Sopenharmony_ci			 HCR_FMO | HCR_IMO | HCR_PTW | HCR_TID3)
10162306a36Sopenharmony_ci#define HCR_HOST_NVHE_FLAGS (HCR_RW | HCR_API | HCR_APK | HCR_ATA)
10262306a36Sopenharmony_ci#define HCR_HOST_NVHE_PROTECTED_FLAGS (HCR_HOST_NVHE_FLAGS | HCR_TSC)
10362306a36Sopenharmony_ci#define HCR_HOST_VHE_FLAGS (HCR_RW | HCR_TGE | HCR_E2H)
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci#define HCRX_GUEST_FLAGS (HCRX_EL2_SMPME | HCRX_EL2_TCR2En)
10662306a36Sopenharmony_ci#define HCRX_HOST_FLAGS (HCRX_EL2_MSCEn | HCRX_EL2_TCR2En)
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci/* TCR_EL2 Registers bits */
10962306a36Sopenharmony_ci#define TCR_EL2_RES1		((1U << 31) | (1 << 23))
11062306a36Sopenharmony_ci#define TCR_EL2_TBI		(1 << 20)
11162306a36Sopenharmony_ci#define TCR_EL2_PS_SHIFT	16
11262306a36Sopenharmony_ci#define TCR_EL2_PS_MASK		(7 << TCR_EL2_PS_SHIFT)
11362306a36Sopenharmony_ci#define TCR_EL2_PS_40B		(2 << TCR_EL2_PS_SHIFT)
11462306a36Sopenharmony_ci#define TCR_EL2_TG0_MASK	TCR_TG0_MASK
11562306a36Sopenharmony_ci#define TCR_EL2_SH0_MASK	TCR_SH0_MASK
11662306a36Sopenharmony_ci#define TCR_EL2_ORGN0_MASK	TCR_ORGN0_MASK
11762306a36Sopenharmony_ci#define TCR_EL2_IRGN0_MASK	TCR_IRGN0_MASK
11862306a36Sopenharmony_ci#define TCR_EL2_T0SZ_MASK	0x3f
11962306a36Sopenharmony_ci#define TCR_EL2_MASK	(TCR_EL2_TG0_MASK | TCR_EL2_SH0_MASK | \
12062306a36Sopenharmony_ci			 TCR_EL2_ORGN0_MASK | TCR_EL2_IRGN0_MASK | TCR_EL2_T0SZ_MASK)
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci/* VTCR_EL2 Registers bits */
12362306a36Sopenharmony_ci#define VTCR_EL2_RES1		(1U << 31)
12462306a36Sopenharmony_ci#define VTCR_EL2_HD		(1 << 22)
12562306a36Sopenharmony_ci#define VTCR_EL2_HA		(1 << 21)
12662306a36Sopenharmony_ci#define VTCR_EL2_PS_SHIFT	TCR_EL2_PS_SHIFT
12762306a36Sopenharmony_ci#define VTCR_EL2_PS_MASK	TCR_EL2_PS_MASK
12862306a36Sopenharmony_ci#define VTCR_EL2_TG0_MASK	TCR_TG0_MASK
12962306a36Sopenharmony_ci#define VTCR_EL2_TG0_4K		TCR_TG0_4K
13062306a36Sopenharmony_ci#define VTCR_EL2_TG0_16K	TCR_TG0_16K
13162306a36Sopenharmony_ci#define VTCR_EL2_TG0_64K	TCR_TG0_64K
13262306a36Sopenharmony_ci#define VTCR_EL2_SH0_MASK	TCR_SH0_MASK
13362306a36Sopenharmony_ci#define VTCR_EL2_SH0_INNER	TCR_SH0_INNER
13462306a36Sopenharmony_ci#define VTCR_EL2_ORGN0_MASK	TCR_ORGN0_MASK
13562306a36Sopenharmony_ci#define VTCR_EL2_ORGN0_WBWA	TCR_ORGN0_WBWA
13662306a36Sopenharmony_ci#define VTCR_EL2_IRGN0_MASK	TCR_IRGN0_MASK
13762306a36Sopenharmony_ci#define VTCR_EL2_IRGN0_WBWA	TCR_IRGN0_WBWA
13862306a36Sopenharmony_ci#define VTCR_EL2_SL0_SHIFT	6
13962306a36Sopenharmony_ci#define VTCR_EL2_SL0_MASK	(3 << VTCR_EL2_SL0_SHIFT)
14062306a36Sopenharmony_ci#define VTCR_EL2_T0SZ_MASK	0x3f
14162306a36Sopenharmony_ci#define VTCR_EL2_VS_SHIFT	19
14262306a36Sopenharmony_ci#define VTCR_EL2_VS_8BIT	(0 << VTCR_EL2_VS_SHIFT)
14362306a36Sopenharmony_ci#define VTCR_EL2_VS_16BIT	(1 << VTCR_EL2_VS_SHIFT)
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci#define VTCR_EL2_T0SZ(x)	TCR_T0SZ(x)
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci/*
14862306a36Sopenharmony_ci * We configure the Stage-2 page tables to always restrict the IPA space to be
14962306a36Sopenharmony_ci * 40 bits wide (T0SZ = 24).  Systems with a PARange smaller than 40 bits are
15062306a36Sopenharmony_ci * not known to exist and will break with this configuration.
15162306a36Sopenharmony_ci *
15262306a36Sopenharmony_ci * The VTCR_EL2 is configured per VM and is initialised in kvm_init_stage2_mmu.
15362306a36Sopenharmony_ci *
15462306a36Sopenharmony_ci * Note that when using 4K pages, we concatenate two first level page tables
15562306a36Sopenharmony_ci * together. With 16K pages, we concatenate 16 first level page tables.
15662306a36Sopenharmony_ci *
15762306a36Sopenharmony_ci */
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci#define VTCR_EL2_COMMON_BITS	(VTCR_EL2_SH0_INNER | VTCR_EL2_ORGN0_WBWA | \
16062306a36Sopenharmony_ci				 VTCR_EL2_IRGN0_WBWA | VTCR_EL2_RES1)
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci/*
16362306a36Sopenharmony_ci * VTCR_EL2:SL0 indicates the entry level for Stage2 translation.
16462306a36Sopenharmony_ci * Interestingly, it depends on the page size.
16562306a36Sopenharmony_ci * See D.10.2.121, VTCR_EL2, in ARM DDI 0487C.a
16662306a36Sopenharmony_ci *
16762306a36Sopenharmony_ci *	-----------------------------------------
16862306a36Sopenharmony_ci *	| Entry level		|  4K  | 16K/64K |
16962306a36Sopenharmony_ci *	------------------------------------------
17062306a36Sopenharmony_ci *	| Level: 0		|  2   |   -     |
17162306a36Sopenharmony_ci *	------------------------------------------
17262306a36Sopenharmony_ci *	| Level: 1		|  1   |   2     |
17362306a36Sopenharmony_ci *	------------------------------------------
17462306a36Sopenharmony_ci *	| Level: 2		|  0   |   1     |
17562306a36Sopenharmony_ci *	------------------------------------------
17662306a36Sopenharmony_ci *	| Level: 3		|  -   |   0     |
17762306a36Sopenharmony_ci *	------------------------------------------
17862306a36Sopenharmony_ci *
17962306a36Sopenharmony_ci * The table roughly translates to :
18062306a36Sopenharmony_ci *
18162306a36Sopenharmony_ci *	SL0(PAGE_SIZE, Entry_level) = TGRAN_SL0_BASE - Entry_Level
18262306a36Sopenharmony_ci *
18362306a36Sopenharmony_ci * Where TGRAN_SL0_BASE is a magic number depending on the page size:
18462306a36Sopenharmony_ci * 	TGRAN_SL0_BASE(4K) = 2
18562306a36Sopenharmony_ci *	TGRAN_SL0_BASE(16K) = 3
18662306a36Sopenharmony_ci *	TGRAN_SL0_BASE(64K) = 3
18762306a36Sopenharmony_ci * provided we take care of ruling out the unsupported cases and
18862306a36Sopenharmony_ci * Entry_Level = 4 - Number_of_levels.
18962306a36Sopenharmony_ci *
19062306a36Sopenharmony_ci */
19162306a36Sopenharmony_ci#ifdef CONFIG_ARM64_64K_PAGES
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci#define VTCR_EL2_TGRAN			VTCR_EL2_TG0_64K
19462306a36Sopenharmony_ci#define VTCR_EL2_TGRAN_SL0_BASE		3UL
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci#elif defined(CONFIG_ARM64_16K_PAGES)
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci#define VTCR_EL2_TGRAN			VTCR_EL2_TG0_16K
19962306a36Sopenharmony_ci#define VTCR_EL2_TGRAN_SL0_BASE		3UL
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ci#else	/* 4K */
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci#define VTCR_EL2_TGRAN			VTCR_EL2_TG0_4K
20462306a36Sopenharmony_ci#define VTCR_EL2_TGRAN_SL0_BASE		2UL
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci#endif
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci#define VTCR_EL2_LVLS_TO_SL0(levels)	\
20962306a36Sopenharmony_ci	((VTCR_EL2_TGRAN_SL0_BASE - (4 - (levels))) << VTCR_EL2_SL0_SHIFT)
21062306a36Sopenharmony_ci#define VTCR_EL2_SL0_TO_LVLS(sl0)	\
21162306a36Sopenharmony_ci	((sl0) + 4 - VTCR_EL2_TGRAN_SL0_BASE)
21262306a36Sopenharmony_ci#define VTCR_EL2_LVLS(vtcr)		\
21362306a36Sopenharmony_ci	VTCR_EL2_SL0_TO_LVLS(((vtcr) & VTCR_EL2_SL0_MASK) >> VTCR_EL2_SL0_SHIFT)
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci#define VTCR_EL2_FLAGS			(VTCR_EL2_COMMON_BITS | VTCR_EL2_TGRAN)
21662306a36Sopenharmony_ci#define VTCR_EL2_IPA(vtcr)		(64 - ((vtcr) & VTCR_EL2_T0SZ_MASK))
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci/*
21962306a36Sopenharmony_ci * ARM VMSAv8-64 defines an algorithm for finding the translation table
22062306a36Sopenharmony_ci * descriptors in section D4.2.8 in ARM DDI 0487C.a.
22162306a36Sopenharmony_ci *
22262306a36Sopenharmony_ci * The algorithm defines the expectations on the translation table
22362306a36Sopenharmony_ci * addresses for each level, based on PAGE_SIZE, entry level
22462306a36Sopenharmony_ci * and the translation table size (T0SZ). The variable "x" in the
22562306a36Sopenharmony_ci * algorithm determines the alignment of a table base address at a given
22662306a36Sopenharmony_ci * level and thus determines the alignment of VTTBR:BADDR for stage2
22762306a36Sopenharmony_ci * page table entry level.
22862306a36Sopenharmony_ci * Since the number of bits resolved at the entry level could vary
22962306a36Sopenharmony_ci * depending on the T0SZ, the value of "x" is defined based on a
23062306a36Sopenharmony_ci * Magic constant for a given PAGE_SIZE and Entry Level. The
23162306a36Sopenharmony_ci * intermediate levels must be always aligned to the PAGE_SIZE (i.e,
23262306a36Sopenharmony_ci * x = PAGE_SHIFT).
23362306a36Sopenharmony_ci *
23462306a36Sopenharmony_ci * The value of "x" for entry level is calculated as :
23562306a36Sopenharmony_ci *    x = Magic_N - T0SZ
23662306a36Sopenharmony_ci *
23762306a36Sopenharmony_ci * where Magic_N is an integer depending on the page size and the entry
23862306a36Sopenharmony_ci * level of the page table as below:
23962306a36Sopenharmony_ci *
24062306a36Sopenharmony_ci *	--------------------------------------------
24162306a36Sopenharmony_ci *	| Entry level		|  4K    16K   64K |
24262306a36Sopenharmony_ci *	--------------------------------------------
24362306a36Sopenharmony_ci *	| Level: 0 (4 levels)	| 28   |  -  |  -  |
24462306a36Sopenharmony_ci *	--------------------------------------------
24562306a36Sopenharmony_ci *	| Level: 1 (3 levels)	| 37   | 31  | 25  |
24662306a36Sopenharmony_ci *	--------------------------------------------
24762306a36Sopenharmony_ci *	| Level: 2 (2 levels)	| 46   | 42  | 38  |
24862306a36Sopenharmony_ci *	--------------------------------------------
24962306a36Sopenharmony_ci *	| Level: 3 (1 level)	| -    | 53  | 51  |
25062306a36Sopenharmony_ci *	--------------------------------------------
25162306a36Sopenharmony_ci *
25262306a36Sopenharmony_ci * We have a magic formula for the Magic_N below:
25362306a36Sopenharmony_ci *
25462306a36Sopenharmony_ci *  Magic_N(PAGE_SIZE, Level) = 64 - ((PAGE_SHIFT - 3) * Number_of_levels)
25562306a36Sopenharmony_ci *
25662306a36Sopenharmony_ci * where Number_of_levels = (4 - Level). We are only interested in the
25762306a36Sopenharmony_ci * value for Entry_Level for the stage2 page table.
25862306a36Sopenharmony_ci *
25962306a36Sopenharmony_ci * So, given that T0SZ = (64 - IPA_SHIFT), we can compute 'x' as follows:
26062306a36Sopenharmony_ci *
26162306a36Sopenharmony_ci *	x = (64 - ((PAGE_SHIFT - 3) * Number_of_levels)) - (64 - IPA_SHIFT)
26262306a36Sopenharmony_ci *	  = IPA_SHIFT - ((PAGE_SHIFT - 3) * Number of levels)
26362306a36Sopenharmony_ci *
26462306a36Sopenharmony_ci * Here is one way to explain the Magic Formula:
26562306a36Sopenharmony_ci *
26662306a36Sopenharmony_ci *  x = log2(Size_of_Entry_Level_Table)
26762306a36Sopenharmony_ci *
26862306a36Sopenharmony_ci * Since, we can resolve (PAGE_SHIFT - 3) bits at each level, and another
26962306a36Sopenharmony_ci * PAGE_SHIFT bits in the PTE, we have :
27062306a36Sopenharmony_ci *
27162306a36Sopenharmony_ci *  Bits_Entry_level = IPA_SHIFT - ((PAGE_SHIFT - 3) * (n - 1) + PAGE_SHIFT)
27262306a36Sopenharmony_ci *		     = IPA_SHIFT - (PAGE_SHIFT - 3) * n - 3
27362306a36Sopenharmony_ci *  where n = number of levels, and since each pointer is 8bytes, we have:
27462306a36Sopenharmony_ci *
27562306a36Sopenharmony_ci *  x = Bits_Entry_Level + 3
27662306a36Sopenharmony_ci *    = IPA_SHIFT - (PAGE_SHIFT - 3) * n
27762306a36Sopenharmony_ci *
27862306a36Sopenharmony_ci * The only constraint here is that, we have to find the number of page table
27962306a36Sopenharmony_ci * levels for a given IPA size (which we do, see stage2_pt_levels())
28062306a36Sopenharmony_ci */
28162306a36Sopenharmony_ci#define ARM64_VTTBR_X(ipa, levels)	((ipa) - ((levels) * (PAGE_SHIFT - 3)))
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_ci#define VTTBR_CNP_BIT     (UL(1))
28462306a36Sopenharmony_ci#define VTTBR_VMID_SHIFT  (UL(48))
28562306a36Sopenharmony_ci#define VTTBR_VMID_MASK(size) (_AT(u64, (1 << size) - 1) << VTTBR_VMID_SHIFT)
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_ci/* Hyp System Trap Register */
28862306a36Sopenharmony_ci#define HSTR_EL2_T(x)	(1 << x)
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ci/* Hyp Coprocessor Trap Register Shifts */
29162306a36Sopenharmony_ci#define CPTR_EL2_TFP_SHIFT 10
29262306a36Sopenharmony_ci
29362306a36Sopenharmony_ci/* Hyp Coprocessor Trap Register */
29462306a36Sopenharmony_ci#define CPTR_EL2_TCPAC	(1U << 31)
29562306a36Sopenharmony_ci#define CPTR_EL2_TAM	(1 << 30)
29662306a36Sopenharmony_ci#define CPTR_EL2_TTA	(1 << 20)
29762306a36Sopenharmony_ci#define CPTR_EL2_TSM	(1 << 12)
29862306a36Sopenharmony_ci#define CPTR_EL2_TFP	(1 << CPTR_EL2_TFP_SHIFT)
29962306a36Sopenharmony_ci#define CPTR_EL2_TZ	(1 << 8)
30062306a36Sopenharmony_ci#define CPTR_NVHE_EL2_RES1	0x000032ff /* known RES1 bits in CPTR_EL2 (nVHE) */
30162306a36Sopenharmony_ci#define CPTR_NVHE_EL2_RES0	(GENMASK(63, 32) |	\
30262306a36Sopenharmony_ci				 GENMASK(29, 21) |	\
30362306a36Sopenharmony_ci				 GENMASK(19, 14) |	\
30462306a36Sopenharmony_ci				 BIT(11))
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_ci/* Hyp Debug Configuration Register bits */
30762306a36Sopenharmony_ci#define MDCR_EL2_E2TB_MASK	(UL(0x3))
30862306a36Sopenharmony_ci#define MDCR_EL2_E2TB_SHIFT	(UL(24))
30962306a36Sopenharmony_ci#define MDCR_EL2_HPMFZS		(UL(1) << 36)
31062306a36Sopenharmony_ci#define MDCR_EL2_HPMFZO		(UL(1) << 29)
31162306a36Sopenharmony_ci#define MDCR_EL2_MTPME		(UL(1) << 28)
31262306a36Sopenharmony_ci#define MDCR_EL2_TDCC		(UL(1) << 27)
31362306a36Sopenharmony_ci#define MDCR_EL2_HLP		(UL(1) << 26)
31462306a36Sopenharmony_ci#define MDCR_EL2_HCCD		(UL(1) << 23)
31562306a36Sopenharmony_ci#define MDCR_EL2_TTRF		(UL(1) << 19)
31662306a36Sopenharmony_ci#define MDCR_EL2_HPMD		(UL(1) << 17)
31762306a36Sopenharmony_ci#define MDCR_EL2_TPMS		(UL(1) << 14)
31862306a36Sopenharmony_ci#define MDCR_EL2_E2PB_MASK	(UL(0x3))
31962306a36Sopenharmony_ci#define MDCR_EL2_E2PB_SHIFT	(UL(12))
32062306a36Sopenharmony_ci#define MDCR_EL2_TDRA		(UL(1) << 11)
32162306a36Sopenharmony_ci#define MDCR_EL2_TDOSA		(UL(1) << 10)
32262306a36Sopenharmony_ci#define MDCR_EL2_TDA		(UL(1) << 9)
32362306a36Sopenharmony_ci#define MDCR_EL2_TDE		(UL(1) << 8)
32462306a36Sopenharmony_ci#define MDCR_EL2_HPME		(UL(1) << 7)
32562306a36Sopenharmony_ci#define MDCR_EL2_TPM		(UL(1) << 6)
32662306a36Sopenharmony_ci#define MDCR_EL2_TPMCR		(UL(1) << 5)
32762306a36Sopenharmony_ci#define MDCR_EL2_HPMN_MASK	(UL(0x1F))
32862306a36Sopenharmony_ci#define MDCR_EL2_RES0		(GENMASK(63, 37) |	\
32962306a36Sopenharmony_ci				 GENMASK(35, 30) |	\
33062306a36Sopenharmony_ci				 GENMASK(25, 24) |	\
33162306a36Sopenharmony_ci				 GENMASK(22, 20) |	\
33262306a36Sopenharmony_ci				 BIT(18) |		\
33362306a36Sopenharmony_ci				 GENMASK(16, 15))
33462306a36Sopenharmony_ci
33562306a36Sopenharmony_ci/*
33662306a36Sopenharmony_ci * FGT register definitions
33762306a36Sopenharmony_ci *
33862306a36Sopenharmony_ci * RES0 and polarity masks as of DDI0487J.a, to be updated as needed.
33962306a36Sopenharmony_ci * We're not using the generated masks as they are usually ahead of
34062306a36Sopenharmony_ci * the published ARM ARM, which we use as a reference.
34162306a36Sopenharmony_ci *
34262306a36Sopenharmony_ci * Once we get to a point where the two describe the same thing, we'll
34362306a36Sopenharmony_ci * merge the definitions. One day.
34462306a36Sopenharmony_ci */
34562306a36Sopenharmony_ci#define __HFGRTR_EL2_RES0	(GENMASK(63, 56) | GENMASK(53, 51))
34662306a36Sopenharmony_ci#define __HFGRTR_EL2_MASK	GENMASK(49, 0)
34762306a36Sopenharmony_ci#define __HFGRTR_EL2_nMASK	(GENMASK(58, 57) | GENMASK(55, 54) | BIT(50))
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_ci#define __HFGWTR_EL2_RES0	(GENMASK(63, 56) | GENMASK(53, 51) |	\
35062306a36Sopenharmony_ci				 BIT(46) | BIT(42) | BIT(40) | BIT(28) | \
35162306a36Sopenharmony_ci				 GENMASK(26, 25) | BIT(21) | BIT(18) |	\
35262306a36Sopenharmony_ci				 GENMASK(15, 14) | GENMASK(10, 9) | BIT(2))
35362306a36Sopenharmony_ci#define __HFGWTR_EL2_MASK	GENMASK(49, 0)
35462306a36Sopenharmony_ci#define __HFGWTR_EL2_nMASK	(GENMASK(58, 57) | GENMASK(55, 54) | BIT(50))
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_ci#define __HFGITR_EL2_RES0	GENMASK(63, 57)
35762306a36Sopenharmony_ci#define __HFGITR_EL2_MASK	GENMASK(54, 0)
35862306a36Sopenharmony_ci#define __HFGITR_EL2_nMASK	GENMASK(56, 55)
35962306a36Sopenharmony_ci
36062306a36Sopenharmony_ci#define __HDFGRTR_EL2_RES0	(BIT(49) | BIT(42) | GENMASK(39, 38) |	\
36162306a36Sopenharmony_ci				 GENMASK(21, 20) | BIT(8))
36262306a36Sopenharmony_ci#define __HDFGRTR_EL2_MASK	~__HDFGRTR_EL2_nMASK
36362306a36Sopenharmony_ci#define __HDFGRTR_EL2_nMASK	GENMASK(62, 59)
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_ci#define __HDFGWTR_EL2_RES0	(BIT(63) | GENMASK(59, 58) | BIT(51) | BIT(47) | \
36662306a36Sopenharmony_ci				 BIT(43) | GENMASK(40, 38) | BIT(34) | BIT(30) | \
36762306a36Sopenharmony_ci				 BIT(22) | BIT(9) | BIT(6))
36862306a36Sopenharmony_ci#define __HDFGWTR_EL2_MASK	~__HDFGWTR_EL2_nMASK
36962306a36Sopenharmony_ci#define __HDFGWTR_EL2_nMASK	GENMASK(62, 60)
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_ci/* Similar definitions for HCRX_EL2 */
37262306a36Sopenharmony_ci#define __HCRX_EL2_RES0		(GENMASK(63, 16) | GENMASK(13, 12))
37362306a36Sopenharmony_ci#define __HCRX_EL2_MASK		(0)
37462306a36Sopenharmony_ci#define __HCRX_EL2_nMASK	(GENMASK(15, 14) | GENMASK(4, 0))
37562306a36Sopenharmony_ci
37662306a36Sopenharmony_ci/* Hyp Prefetch Fault Address Register (HPFAR/HDFAR) */
37762306a36Sopenharmony_ci#define HPFAR_MASK	(~UL(0xf))
37862306a36Sopenharmony_ci/*
37962306a36Sopenharmony_ci * We have
38062306a36Sopenharmony_ci *	PAR	[PA_Shift - 1	: 12] = PA	[PA_Shift - 1 : 12]
38162306a36Sopenharmony_ci *	HPFAR	[PA_Shift - 9	: 4]  = FIPA	[PA_Shift - 1 : 12]
38262306a36Sopenharmony_ci *
38362306a36Sopenharmony_ci * Always assume 52 bit PA since at this point, we don't know how many PA bits
38462306a36Sopenharmony_ci * the page table has been set up for. This should be safe since unused address
38562306a36Sopenharmony_ci * bits in PAR are res0.
38662306a36Sopenharmony_ci */
38762306a36Sopenharmony_ci#define PAR_TO_HPFAR(par)		\
38862306a36Sopenharmony_ci	(((par) & GENMASK_ULL(52 - 1, 12)) >> 8)
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_ci#define ECN(x) { ESR_ELx_EC_##x, #x }
39162306a36Sopenharmony_ci
39262306a36Sopenharmony_ci#define kvm_arm_exception_class \
39362306a36Sopenharmony_ci	ECN(UNKNOWN), ECN(WFx), ECN(CP15_32), ECN(CP15_64), ECN(CP14_MR), \
39462306a36Sopenharmony_ci	ECN(CP14_LS), ECN(FP_ASIMD), ECN(CP10_ID), ECN(PAC), ECN(CP14_64), \
39562306a36Sopenharmony_ci	ECN(SVC64), ECN(HVC64), ECN(SMC64), ECN(SYS64), ECN(SVE), \
39662306a36Sopenharmony_ci	ECN(IMP_DEF), ECN(IABT_LOW), ECN(IABT_CUR), \
39762306a36Sopenharmony_ci	ECN(PC_ALIGN), ECN(DABT_LOW), ECN(DABT_CUR), \
39862306a36Sopenharmony_ci	ECN(SP_ALIGN), ECN(FP_EXC32), ECN(FP_EXC64), ECN(SERROR), \
39962306a36Sopenharmony_ci	ECN(BREAKPT_LOW), ECN(BREAKPT_CUR), ECN(SOFTSTP_LOW), \
40062306a36Sopenharmony_ci	ECN(SOFTSTP_CUR), ECN(WATCHPT_LOW), ECN(WATCHPT_CUR), \
40162306a36Sopenharmony_ci	ECN(BKPT32), ECN(VECTOR32), ECN(BRK64), ECN(ERET)
40262306a36Sopenharmony_ci
40362306a36Sopenharmony_ci#define CPACR_EL1_TTA		(1 << 28)
40462306a36Sopenharmony_ci
40562306a36Sopenharmony_ci#define kvm_mode_names				\
40662306a36Sopenharmony_ci	{ PSR_MODE_EL0t,	"EL0t" },	\
40762306a36Sopenharmony_ci	{ PSR_MODE_EL1t,	"EL1t" },	\
40862306a36Sopenharmony_ci	{ PSR_MODE_EL1h,	"EL1h" },	\
40962306a36Sopenharmony_ci	{ PSR_MODE_EL2t,	"EL2t" },	\
41062306a36Sopenharmony_ci	{ PSR_MODE_EL2h,	"EL2h" },	\
41162306a36Sopenharmony_ci	{ PSR_MODE_EL3t,	"EL3t" },	\
41262306a36Sopenharmony_ci	{ PSR_MODE_EL3h,	"EL3h" },	\
41362306a36Sopenharmony_ci	{ PSR_AA32_MODE_USR,	"32-bit USR" },	\
41462306a36Sopenharmony_ci	{ PSR_AA32_MODE_FIQ,	"32-bit FIQ" },	\
41562306a36Sopenharmony_ci	{ PSR_AA32_MODE_IRQ,	"32-bit IRQ" },	\
41662306a36Sopenharmony_ci	{ PSR_AA32_MODE_SVC,	"32-bit SVC" },	\
41762306a36Sopenharmony_ci	{ PSR_AA32_MODE_ABT,	"32-bit ABT" },	\
41862306a36Sopenharmony_ci	{ PSR_AA32_MODE_HYP,	"32-bit HYP" },	\
41962306a36Sopenharmony_ci	{ PSR_AA32_MODE_UND,	"32-bit UND" },	\
42062306a36Sopenharmony_ci	{ PSR_AA32_MODE_SYS,	"32-bit SYS" }
42162306a36Sopenharmony_ci
42262306a36Sopenharmony_ci#endif /* __ARM64_KVM_ARM_H__ */
423