162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2020 Google LLC
462306a36Sopenharmony_ci * Author: Will Deacon <will@kernel.org>
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#ifndef __ARM64_KVM_PGTABLE_H__
862306a36Sopenharmony_ci#define __ARM64_KVM_PGTABLE_H__
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <linux/bits.h>
1162306a36Sopenharmony_ci#include <linux/kvm_host.h>
1262306a36Sopenharmony_ci#include <linux/types.h>
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#define KVM_PGTABLE_MAX_LEVELS		4U
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci/*
1762306a36Sopenharmony_ci * The largest supported block sizes for KVM (no 52-bit PA support):
1862306a36Sopenharmony_ci *  - 4K (level 1):	1GB
1962306a36Sopenharmony_ci *  - 16K (level 2):	32MB
2062306a36Sopenharmony_ci *  - 64K (level 2):	512MB
2162306a36Sopenharmony_ci */
2262306a36Sopenharmony_ci#ifdef CONFIG_ARM64_4K_PAGES
2362306a36Sopenharmony_ci#define KVM_PGTABLE_MIN_BLOCK_LEVEL	1U
2462306a36Sopenharmony_ci#else
2562306a36Sopenharmony_ci#define KVM_PGTABLE_MIN_BLOCK_LEVEL	2U
2662306a36Sopenharmony_ci#endif
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_cistatic inline u64 kvm_get_parange(u64 mmfr0)
2962306a36Sopenharmony_ci{
3062306a36Sopenharmony_ci	u64 parange = cpuid_feature_extract_unsigned_field(mmfr0,
3162306a36Sopenharmony_ci				ID_AA64MMFR0_EL1_PARANGE_SHIFT);
3262306a36Sopenharmony_ci	if (parange > ID_AA64MMFR0_EL1_PARANGE_MAX)
3362306a36Sopenharmony_ci		parange = ID_AA64MMFR0_EL1_PARANGE_MAX;
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci	return parange;
3662306a36Sopenharmony_ci}
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_citypedef u64 kvm_pte_t;
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci#define KVM_PTE_VALID			BIT(0)
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci#define KVM_PTE_ADDR_MASK		GENMASK(47, PAGE_SHIFT)
4362306a36Sopenharmony_ci#define KVM_PTE_ADDR_51_48		GENMASK(15, 12)
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci#define KVM_PHYS_INVALID		(-1ULL)
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_cistatic inline bool kvm_pte_valid(kvm_pte_t pte)
4862306a36Sopenharmony_ci{
4962306a36Sopenharmony_ci	return pte & KVM_PTE_VALID;
5062306a36Sopenharmony_ci}
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_cistatic inline u64 kvm_pte_to_phys(kvm_pte_t pte)
5362306a36Sopenharmony_ci{
5462306a36Sopenharmony_ci	u64 pa = pte & KVM_PTE_ADDR_MASK;
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci	if (PAGE_SHIFT == 16)
5762306a36Sopenharmony_ci		pa |= FIELD_GET(KVM_PTE_ADDR_51_48, pte) << 48;
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci	return pa;
6062306a36Sopenharmony_ci}
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_cistatic inline kvm_pte_t kvm_phys_to_pte(u64 pa)
6362306a36Sopenharmony_ci{
6462306a36Sopenharmony_ci	kvm_pte_t pte = pa & KVM_PTE_ADDR_MASK;
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci	if (PAGE_SHIFT == 16) {
6762306a36Sopenharmony_ci		pa &= GENMASK(51, 48);
6862306a36Sopenharmony_ci		pte |= FIELD_PREP(KVM_PTE_ADDR_51_48, pa >> 48);
6962306a36Sopenharmony_ci	}
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci	return pte;
7262306a36Sopenharmony_ci}
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_cistatic inline kvm_pfn_t kvm_pte_to_pfn(kvm_pte_t pte)
7562306a36Sopenharmony_ci{
7662306a36Sopenharmony_ci	return __phys_to_pfn(kvm_pte_to_phys(pte));
7762306a36Sopenharmony_ci}
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_cistatic inline u64 kvm_granule_shift(u32 level)
8062306a36Sopenharmony_ci{
8162306a36Sopenharmony_ci	/* Assumes KVM_PGTABLE_MAX_LEVELS is 4 */
8262306a36Sopenharmony_ci	return ARM64_HW_PGTABLE_LEVEL_SHIFT(level);
8362306a36Sopenharmony_ci}
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_cistatic inline u64 kvm_granule_size(u32 level)
8662306a36Sopenharmony_ci{
8762306a36Sopenharmony_ci	return BIT(kvm_granule_shift(level));
8862306a36Sopenharmony_ci}
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_cistatic inline bool kvm_level_supports_block_mapping(u32 level)
9162306a36Sopenharmony_ci{
9262306a36Sopenharmony_ci	return level >= KVM_PGTABLE_MIN_BLOCK_LEVEL;
9362306a36Sopenharmony_ci}
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_cistatic inline u32 kvm_supported_block_sizes(void)
9662306a36Sopenharmony_ci{
9762306a36Sopenharmony_ci	u32 level = KVM_PGTABLE_MIN_BLOCK_LEVEL;
9862306a36Sopenharmony_ci	u32 r = 0;
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci	for (; level < KVM_PGTABLE_MAX_LEVELS; level++)
10162306a36Sopenharmony_ci		r |= BIT(kvm_granule_shift(level));
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	return r;
10462306a36Sopenharmony_ci}
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_cistatic inline bool kvm_is_block_size_supported(u64 size)
10762306a36Sopenharmony_ci{
10862306a36Sopenharmony_ci	bool is_power_of_two = IS_ALIGNED(size, size);
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci	return is_power_of_two && (size & kvm_supported_block_sizes());
11162306a36Sopenharmony_ci}
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci/**
11462306a36Sopenharmony_ci * struct kvm_pgtable_mm_ops - Memory management callbacks.
11562306a36Sopenharmony_ci * @zalloc_page:		Allocate a single zeroed memory page.
11662306a36Sopenharmony_ci *				The @arg parameter can be used by the walker
11762306a36Sopenharmony_ci *				to pass a memcache. The initial refcount of
11862306a36Sopenharmony_ci *				the page is 1.
11962306a36Sopenharmony_ci * @zalloc_pages_exact:		Allocate an exact number of zeroed memory pages.
12062306a36Sopenharmony_ci *				The @size parameter is in bytes, and is rounded
12162306a36Sopenharmony_ci *				up to the next page boundary. The resulting
12262306a36Sopenharmony_ci *				allocation is physically contiguous.
12362306a36Sopenharmony_ci * @free_pages_exact:		Free an exact number of memory pages previously
12462306a36Sopenharmony_ci *				allocated by zalloc_pages_exact.
12562306a36Sopenharmony_ci * @free_unlinked_table:	Free an unlinked paging structure by unlinking and
12662306a36Sopenharmony_ci *				dropping references.
12762306a36Sopenharmony_ci * @get_page:			Increment the refcount on a page.
12862306a36Sopenharmony_ci * @put_page:			Decrement the refcount on a page. When the
12962306a36Sopenharmony_ci *				refcount reaches 0 the page is automatically
13062306a36Sopenharmony_ci *				freed.
13162306a36Sopenharmony_ci * @page_count:			Return the refcount of a page.
13262306a36Sopenharmony_ci * @phys_to_virt:		Convert a physical address into a virtual
13362306a36Sopenharmony_ci *				address	mapped in the current context.
13462306a36Sopenharmony_ci * @virt_to_phys:		Convert a virtual address mapped in the current
13562306a36Sopenharmony_ci *				context into a physical address.
13662306a36Sopenharmony_ci * @dcache_clean_inval_poc:	Clean and invalidate the data cache to the PoC
13762306a36Sopenharmony_ci *				for the	specified memory address range.
13862306a36Sopenharmony_ci * @icache_inval_pou:		Invalidate the instruction cache to the PoU
13962306a36Sopenharmony_ci *				for the specified memory address range.
14062306a36Sopenharmony_ci */
14162306a36Sopenharmony_cistruct kvm_pgtable_mm_ops {
14262306a36Sopenharmony_ci	void*		(*zalloc_page)(void *arg);
14362306a36Sopenharmony_ci	void*		(*zalloc_pages_exact)(size_t size);
14462306a36Sopenharmony_ci	void		(*free_pages_exact)(void *addr, size_t size);
14562306a36Sopenharmony_ci	void		(*free_unlinked_table)(void *addr, u32 level);
14662306a36Sopenharmony_ci	void		(*get_page)(void *addr);
14762306a36Sopenharmony_ci	void		(*put_page)(void *addr);
14862306a36Sopenharmony_ci	int		(*page_count)(void *addr);
14962306a36Sopenharmony_ci	void*		(*phys_to_virt)(phys_addr_t phys);
15062306a36Sopenharmony_ci	phys_addr_t	(*virt_to_phys)(void *addr);
15162306a36Sopenharmony_ci	void		(*dcache_clean_inval_poc)(void *addr, size_t size);
15262306a36Sopenharmony_ci	void		(*icache_inval_pou)(void *addr, size_t size);
15362306a36Sopenharmony_ci};
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci/**
15662306a36Sopenharmony_ci * enum kvm_pgtable_stage2_flags - Stage-2 page-table flags.
15762306a36Sopenharmony_ci * @KVM_PGTABLE_S2_NOFWB:	Don't enforce Normal-WB even if the CPUs have
15862306a36Sopenharmony_ci *				ARM64_HAS_STAGE2_FWB.
15962306a36Sopenharmony_ci * @KVM_PGTABLE_S2_IDMAP:	Only use identity mappings.
16062306a36Sopenharmony_ci */
16162306a36Sopenharmony_cienum kvm_pgtable_stage2_flags {
16262306a36Sopenharmony_ci	KVM_PGTABLE_S2_NOFWB			= BIT(0),
16362306a36Sopenharmony_ci	KVM_PGTABLE_S2_IDMAP			= BIT(1),
16462306a36Sopenharmony_ci};
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci/**
16762306a36Sopenharmony_ci * enum kvm_pgtable_prot - Page-table permissions and attributes.
16862306a36Sopenharmony_ci * @KVM_PGTABLE_PROT_X:		Execute permission.
16962306a36Sopenharmony_ci * @KVM_PGTABLE_PROT_W:		Write permission.
17062306a36Sopenharmony_ci * @KVM_PGTABLE_PROT_R:		Read permission.
17162306a36Sopenharmony_ci * @KVM_PGTABLE_PROT_DEVICE:	Device attributes.
17262306a36Sopenharmony_ci * @KVM_PGTABLE_PROT_SW0:	Software bit 0.
17362306a36Sopenharmony_ci * @KVM_PGTABLE_PROT_SW1:	Software bit 1.
17462306a36Sopenharmony_ci * @KVM_PGTABLE_PROT_SW2:	Software bit 2.
17562306a36Sopenharmony_ci * @KVM_PGTABLE_PROT_SW3:	Software bit 3.
17662306a36Sopenharmony_ci */
17762306a36Sopenharmony_cienum kvm_pgtable_prot {
17862306a36Sopenharmony_ci	KVM_PGTABLE_PROT_X			= BIT(0),
17962306a36Sopenharmony_ci	KVM_PGTABLE_PROT_W			= BIT(1),
18062306a36Sopenharmony_ci	KVM_PGTABLE_PROT_R			= BIT(2),
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci	KVM_PGTABLE_PROT_DEVICE			= BIT(3),
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci	KVM_PGTABLE_PROT_SW0			= BIT(55),
18562306a36Sopenharmony_ci	KVM_PGTABLE_PROT_SW1			= BIT(56),
18662306a36Sopenharmony_ci	KVM_PGTABLE_PROT_SW2			= BIT(57),
18762306a36Sopenharmony_ci	KVM_PGTABLE_PROT_SW3			= BIT(58),
18862306a36Sopenharmony_ci};
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci#define KVM_PGTABLE_PROT_RW	(KVM_PGTABLE_PROT_R | KVM_PGTABLE_PROT_W)
19162306a36Sopenharmony_ci#define KVM_PGTABLE_PROT_RWX	(KVM_PGTABLE_PROT_RW | KVM_PGTABLE_PROT_X)
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci#define PKVM_HOST_MEM_PROT	KVM_PGTABLE_PROT_RWX
19462306a36Sopenharmony_ci#define PKVM_HOST_MMIO_PROT	KVM_PGTABLE_PROT_RW
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci#define PAGE_HYP		KVM_PGTABLE_PROT_RW
19762306a36Sopenharmony_ci#define PAGE_HYP_EXEC		(KVM_PGTABLE_PROT_R | KVM_PGTABLE_PROT_X)
19862306a36Sopenharmony_ci#define PAGE_HYP_RO		(KVM_PGTABLE_PROT_R)
19962306a36Sopenharmony_ci#define PAGE_HYP_DEVICE		(PAGE_HYP | KVM_PGTABLE_PROT_DEVICE)
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_citypedef bool (*kvm_pgtable_force_pte_cb_t)(u64 addr, u64 end,
20262306a36Sopenharmony_ci					   enum kvm_pgtable_prot prot);
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci/**
20562306a36Sopenharmony_ci * enum kvm_pgtable_walk_flags - Flags to control a depth-first page-table walk.
20662306a36Sopenharmony_ci * @KVM_PGTABLE_WALK_LEAF:		Visit leaf entries, including invalid
20762306a36Sopenharmony_ci *					entries.
20862306a36Sopenharmony_ci * @KVM_PGTABLE_WALK_TABLE_PRE:		Visit table entries before their
20962306a36Sopenharmony_ci *					children.
21062306a36Sopenharmony_ci * @KVM_PGTABLE_WALK_TABLE_POST:	Visit table entries after their
21162306a36Sopenharmony_ci *					children.
21262306a36Sopenharmony_ci * @KVM_PGTABLE_WALK_SHARED:		Indicates the page-tables may be shared
21362306a36Sopenharmony_ci *					with other software walkers.
21462306a36Sopenharmony_ci * @KVM_PGTABLE_WALK_HANDLE_FAULT:	Indicates the page-table walk was
21562306a36Sopenharmony_ci *					invoked from a fault handler.
21662306a36Sopenharmony_ci * @KVM_PGTABLE_WALK_SKIP_BBM_TLBI:	Visit and update table entries
21762306a36Sopenharmony_ci *					without Break-before-make's
21862306a36Sopenharmony_ci *					TLB invalidation.
21962306a36Sopenharmony_ci * @KVM_PGTABLE_WALK_SKIP_CMO:		Visit and update table entries
22062306a36Sopenharmony_ci *					without Cache maintenance
22162306a36Sopenharmony_ci *					operations required.
22262306a36Sopenharmony_ci */
22362306a36Sopenharmony_cienum kvm_pgtable_walk_flags {
22462306a36Sopenharmony_ci	KVM_PGTABLE_WALK_LEAF			= BIT(0),
22562306a36Sopenharmony_ci	KVM_PGTABLE_WALK_TABLE_PRE		= BIT(1),
22662306a36Sopenharmony_ci	KVM_PGTABLE_WALK_TABLE_POST		= BIT(2),
22762306a36Sopenharmony_ci	KVM_PGTABLE_WALK_SHARED			= BIT(3),
22862306a36Sopenharmony_ci	KVM_PGTABLE_WALK_HANDLE_FAULT		= BIT(4),
22962306a36Sopenharmony_ci	KVM_PGTABLE_WALK_SKIP_BBM_TLBI		= BIT(5),
23062306a36Sopenharmony_ci	KVM_PGTABLE_WALK_SKIP_CMO		= BIT(6),
23162306a36Sopenharmony_ci};
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_cistruct kvm_pgtable_visit_ctx {
23462306a36Sopenharmony_ci	kvm_pte_t				*ptep;
23562306a36Sopenharmony_ci	kvm_pte_t				old;
23662306a36Sopenharmony_ci	void					*arg;
23762306a36Sopenharmony_ci	struct kvm_pgtable_mm_ops		*mm_ops;
23862306a36Sopenharmony_ci	u64					start;
23962306a36Sopenharmony_ci	u64					addr;
24062306a36Sopenharmony_ci	u64					end;
24162306a36Sopenharmony_ci	u32					level;
24262306a36Sopenharmony_ci	enum kvm_pgtable_walk_flags		flags;
24362306a36Sopenharmony_ci};
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_citypedef int (*kvm_pgtable_visitor_fn_t)(const struct kvm_pgtable_visit_ctx *ctx,
24662306a36Sopenharmony_ci					enum kvm_pgtable_walk_flags visit);
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_cistatic inline bool kvm_pgtable_walk_shared(const struct kvm_pgtable_visit_ctx *ctx)
24962306a36Sopenharmony_ci{
25062306a36Sopenharmony_ci	return ctx->flags & KVM_PGTABLE_WALK_SHARED;
25162306a36Sopenharmony_ci}
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci/**
25462306a36Sopenharmony_ci * struct kvm_pgtable_walker - Hook into a page-table walk.
25562306a36Sopenharmony_ci * @cb:		Callback function to invoke during the walk.
25662306a36Sopenharmony_ci * @arg:	Argument passed to the callback function.
25762306a36Sopenharmony_ci * @flags:	Bitwise-OR of flags to identify the entry types on which to
25862306a36Sopenharmony_ci *		invoke the callback function.
25962306a36Sopenharmony_ci */
26062306a36Sopenharmony_cistruct kvm_pgtable_walker {
26162306a36Sopenharmony_ci	const kvm_pgtable_visitor_fn_t		cb;
26262306a36Sopenharmony_ci	void * const				arg;
26362306a36Sopenharmony_ci	const enum kvm_pgtable_walk_flags	flags;
26462306a36Sopenharmony_ci};
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci/*
26762306a36Sopenharmony_ci * RCU cannot be used in a non-kernel context such as the hyp. As such, page
26862306a36Sopenharmony_ci * table walkers used in hyp do not call into RCU and instead use other
26962306a36Sopenharmony_ci * synchronization mechanisms (such as a spinlock).
27062306a36Sopenharmony_ci */
27162306a36Sopenharmony_ci#if defined(__KVM_NVHE_HYPERVISOR__) || defined(__KVM_VHE_HYPERVISOR__)
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_citypedef kvm_pte_t *kvm_pteref_t;
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_cistatic inline kvm_pte_t *kvm_dereference_pteref(struct kvm_pgtable_walker *walker,
27662306a36Sopenharmony_ci						kvm_pteref_t pteref)
27762306a36Sopenharmony_ci{
27862306a36Sopenharmony_ci	return pteref;
27962306a36Sopenharmony_ci}
28062306a36Sopenharmony_ci
28162306a36Sopenharmony_cistatic inline int kvm_pgtable_walk_begin(struct kvm_pgtable_walker *walker)
28262306a36Sopenharmony_ci{
28362306a36Sopenharmony_ci	/*
28462306a36Sopenharmony_ci	 * Due to the lack of RCU (or a similar protection scheme), only
28562306a36Sopenharmony_ci	 * non-shared table walkers are allowed in the hypervisor.
28662306a36Sopenharmony_ci	 */
28762306a36Sopenharmony_ci	if (walker->flags & KVM_PGTABLE_WALK_SHARED)
28862306a36Sopenharmony_ci		return -EPERM;
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ci	return 0;
29162306a36Sopenharmony_ci}
29262306a36Sopenharmony_ci
29362306a36Sopenharmony_cistatic inline void kvm_pgtable_walk_end(struct kvm_pgtable_walker *walker) {}
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_cistatic inline bool kvm_pgtable_walk_lock_held(void)
29662306a36Sopenharmony_ci{
29762306a36Sopenharmony_ci	return true;
29862306a36Sopenharmony_ci}
29962306a36Sopenharmony_ci
30062306a36Sopenharmony_ci#else
30162306a36Sopenharmony_ci
30262306a36Sopenharmony_citypedef kvm_pte_t __rcu *kvm_pteref_t;
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_cistatic inline kvm_pte_t *kvm_dereference_pteref(struct kvm_pgtable_walker *walker,
30562306a36Sopenharmony_ci						kvm_pteref_t pteref)
30662306a36Sopenharmony_ci{
30762306a36Sopenharmony_ci	return rcu_dereference_check(pteref, !(walker->flags & KVM_PGTABLE_WALK_SHARED));
30862306a36Sopenharmony_ci}
30962306a36Sopenharmony_ci
31062306a36Sopenharmony_cistatic inline int kvm_pgtable_walk_begin(struct kvm_pgtable_walker *walker)
31162306a36Sopenharmony_ci{
31262306a36Sopenharmony_ci	if (walker->flags & KVM_PGTABLE_WALK_SHARED)
31362306a36Sopenharmony_ci		rcu_read_lock();
31462306a36Sopenharmony_ci
31562306a36Sopenharmony_ci	return 0;
31662306a36Sopenharmony_ci}
31762306a36Sopenharmony_ci
31862306a36Sopenharmony_cistatic inline void kvm_pgtable_walk_end(struct kvm_pgtable_walker *walker)
31962306a36Sopenharmony_ci{
32062306a36Sopenharmony_ci	if (walker->flags & KVM_PGTABLE_WALK_SHARED)
32162306a36Sopenharmony_ci		rcu_read_unlock();
32262306a36Sopenharmony_ci}
32362306a36Sopenharmony_ci
32462306a36Sopenharmony_cistatic inline bool kvm_pgtable_walk_lock_held(void)
32562306a36Sopenharmony_ci{
32662306a36Sopenharmony_ci	return rcu_read_lock_held();
32762306a36Sopenharmony_ci}
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_ci#endif
33062306a36Sopenharmony_ci
33162306a36Sopenharmony_ci/**
33262306a36Sopenharmony_ci * struct kvm_pgtable - KVM page-table.
33362306a36Sopenharmony_ci * @ia_bits:		Maximum input address size, in bits.
33462306a36Sopenharmony_ci * @start_level:	Level at which the page-table walk starts.
33562306a36Sopenharmony_ci * @pgd:		Pointer to the first top-level entry of the page-table.
33662306a36Sopenharmony_ci * @mm_ops:		Memory management callbacks.
33762306a36Sopenharmony_ci * @mmu:		Stage-2 KVM MMU struct. Unused for stage-1 page-tables.
33862306a36Sopenharmony_ci * @flags:		Stage-2 page-table flags.
33962306a36Sopenharmony_ci * @force_pte_cb:	Function that returns true if page level mappings must
34062306a36Sopenharmony_ci *			be used instead of block mappings.
34162306a36Sopenharmony_ci */
34262306a36Sopenharmony_cistruct kvm_pgtable {
34362306a36Sopenharmony_ci	u32					ia_bits;
34462306a36Sopenharmony_ci	u32					start_level;
34562306a36Sopenharmony_ci	kvm_pteref_t				pgd;
34662306a36Sopenharmony_ci	struct kvm_pgtable_mm_ops		*mm_ops;
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_ci	/* Stage-2 only */
34962306a36Sopenharmony_ci	struct kvm_s2_mmu			*mmu;
35062306a36Sopenharmony_ci	enum kvm_pgtable_stage2_flags		flags;
35162306a36Sopenharmony_ci	kvm_pgtable_force_pte_cb_t		force_pte_cb;
35262306a36Sopenharmony_ci};
35362306a36Sopenharmony_ci
35462306a36Sopenharmony_ci/**
35562306a36Sopenharmony_ci * kvm_pgtable_hyp_init() - Initialise a hypervisor stage-1 page-table.
35662306a36Sopenharmony_ci * @pgt:	Uninitialised page-table structure to initialise.
35762306a36Sopenharmony_ci * @va_bits:	Maximum virtual address bits.
35862306a36Sopenharmony_ci * @mm_ops:	Memory management callbacks.
35962306a36Sopenharmony_ci *
36062306a36Sopenharmony_ci * Return: 0 on success, negative error code on failure.
36162306a36Sopenharmony_ci */
36262306a36Sopenharmony_ciint kvm_pgtable_hyp_init(struct kvm_pgtable *pgt, u32 va_bits,
36362306a36Sopenharmony_ci			 struct kvm_pgtable_mm_ops *mm_ops);
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_ci/**
36662306a36Sopenharmony_ci * kvm_pgtable_hyp_destroy() - Destroy an unused hypervisor stage-1 page-table.
36762306a36Sopenharmony_ci * @pgt:	Page-table structure initialised by kvm_pgtable_hyp_init().
36862306a36Sopenharmony_ci *
36962306a36Sopenharmony_ci * The page-table is assumed to be unreachable by any hardware walkers prior
37062306a36Sopenharmony_ci * to freeing and therefore no TLB invalidation is performed.
37162306a36Sopenharmony_ci */
37262306a36Sopenharmony_civoid kvm_pgtable_hyp_destroy(struct kvm_pgtable *pgt);
37362306a36Sopenharmony_ci
37462306a36Sopenharmony_ci/**
37562306a36Sopenharmony_ci * kvm_pgtable_hyp_map() - Install a mapping in a hypervisor stage-1 page-table.
37662306a36Sopenharmony_ci * @pgt:	Page-table structure initialised by kvm_pgtable_hyp_init().
37762306a36Sopenharmony_ci * @addr:	Virtual address at which to place the mapping.
37862306a36Sopenharmony_ci * @size:	Size of the mapping.
37962306a36Sopenharmony_ci * @phys:	Physical address of the memory to map.
38062306a36Sopenharmony_ci * @prot:	Permissions and attributes for the mapping.
38162306a36Sopenharmony_ci *
38262306a36Sopenharmony_ci * The offset of @addr within a page is ignored, @size is rounded-up to
38362306a36Sopenharmony_ci * the next page boundary and @phys is rounded-down to the previous page
38462306a36Sopenharmony_ci * boundary.
38562306a36Sopenharmony_ci *
38662306a36Sopenharmony_ci * If device attributes are not explicitly requested in @prot, then the
38762306a36Sopenharmony_ci * mapping will be normal, cacheable. Attempts to install a new mapping
38862306a36Sopenharmony_ci * for a virtual address that is already mapped will be rejected with an
38962306a36Sopenharmony_ci * error and a WARN().
39062306a36Sopenharmony_ci *
39162306a36Sopenharmony_ci * Return: 0 on success, negative error code on failure.
39262306a36Sopenharmony_ci */
39362306a36Sopenharmony_ciint kvm_pgtable_hyp_map(struct kvm_pgtable *pgt, u64 addr, u64 size, u64 phys,
39462306a36Sopenharmony_ci			enum kvm_pgtable_prot prot);
39562306a36Sopenharmony_ci
39662306a36Sopenharmony_ci/**
39762306a36Sopenharmony_ci * kvm_pgtable_hyp_unmap() - Remove a mapping from a hypervisor stage-1 page-table.
39862306a36Sopenharmony_ci * @pgt:	Page-table structure initialised by kvm_pgtable_hyp_init().
39962306a36Sopenharmony_ci * @addr:	Virtual address from which to remove the mapping.
40062306a36Sopenharmony_ci * @size:	Size of the mapping.
40162306a36Sopenharmony_ci *
40262306a36Sopenharmony_ci * The offset of @addr within a page is ignored, @size is rounded-up to
40362306a36Sopenharmony_ci * the next page boundary and @phys is rounded-down to the previous page
40462306a36Sopenharmony_ci * boundary.
40562306a36Sopenharmony_ci *
40662306a36Sopenharmony_ci * TLB invalidation is performed for each page-table entry cleared during the
40762306a36Sopenharmony_ci * unmapping operation and the reference count for the page-table page
40862306a36Sopenharmony_ci * containing the cleared entry is decremented, with unreferenced pages being
40962306a36Sopenharmony_ci * freed. The unmapping operation will stop early if it encounters either an
41062306a36Sopenharmony_ci * invalid page-table entry or a valid block mapping which maps beyond the range
41162306a36Sopenharmony_ci * being unmapped.
41262306a36Sopenharmony_ci *
41362306a36Sopenharmony_ci * Return: Number of bytes unmapped, which may be 0.
41462306a36Sopenharmony_ci */
41562306a36Sopenharmony_ciu64 kvm_pgtable_hyp_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size);
41662306a36Sopenharmony_ci
41762306a36Sopenharmony_ci/**
41862306a36Sopenharmony_ci * kvm_get_vtcr() - Helper to construct VTCR_EL2
41962306a36Sopenharmony_ci * @mmfr0:	Sanitized value of SYS_ID_AA64MMFR0_EL1 register.
42062306a36Sopenharmony_ci * @mmfr1:	Sanitized value of SYS_ID_AA64MMFR1_EL1 register.
42162306a36Sopenharmony_ci * @phys_shfit:	Value to set in VTCR_EL2.T0SZ.
42262306a36Sopenharmony_ci *
42362306a36Sopenharmony_ci * The VTCR value is common across all the physical CPUs on the system.
42462306a36Sopenharmony_ci * We use system wide sanitised values to fill in different fields,
42562306a36Sopenharmony_ci * except for Hardware Management of Access Flags. HA Flag is set
42662306a36Sopenharmony_ci * unconditionally on all CPUs, as it is safe to run with or without
42762306a36Sopenharmony_ci * the feature and the bit is RES0 on CPUs that don't support it.
42862306a36Sopenharmony_ci *
42962306a36Sopenharmony_ci * Return: VTCR_EL2 value
43062306a36Sopenharmony_ci */
43162306a36Sopenharmony_ciu64 kvm_get_vtcr(u64 mmfr0, u64 mmfr1, u32 phys_shift);
43262306a36Sopenharmony_ci
43362306a36Sopenharmony_ci/**
43462306a36Sopenharmony_ci * kvm_pgtable_stage2_pgd_size() - Helper to compute size of a stage-2 PGD
43562306a36Sopenharmony_ci * @vtcr:	Content of the VTCR register.
43662306a36Sopenharmony_ci *
43762306a36Sopenharmony_ci * Return: the size (in bytes) of the stage-2 PGD
43862306a36Sopenharmony_ci */
43962306a36Sopenharmony_cisize_t kvm_pgtable_stage2_pgd_size(u64 vtcr);
44062306a36Sopenharmony_ci
44162306a36Sopenharmony_ci/**
44262306a36Sopenharmony_ci * __kvm_pgtable_stage2_init() - Initialise a guest stage-2 page-table.
44362306a36Sopenharmony_ci * @pgt:	Uninitialised page-table structure to initialise.
44462306a36Sopenharmony_ci * @mmu:	S2 MMU context for this S2 translation
44562306a36Sopenharmony_ci * @mm_ops:	Memory management callbacks.
44662306a36Sopenharmony_ci * @flags:	Stage-2 configuration flags.
44762306a36Sopenharmony_ci * @force_pte_cb: Function that returns true if page level mappings must
44862306a36Sopenharmony_ci *		be used instead of block mappings.
44962306a36Sopenharmony_ci *
45062306a36Sopenharmony_ci * Return: 0 on success, negative error code on failure.
45162306a36Sopenharmony_ci */
45262306a36Sopenharmony_ciint __kvm_pgtable_stage2_init(struct kvm_pgtable *pgt, struct kvm_s2_mmu *mmu,
45362306a36Sopenharmony_ci			      struct kvm_pgtable_mm_ops *mm_ops,
45462306a36Sopenharmony_ci			      enum kvm_pgtable_stage2_flags flags,
45562306a36Sopenharmony_ci			      kvm_pgtable_force_pte_cb_t force_pte_cb);
45662306a36Sopenharmony_ci
45762306a36Sopenharmony_ci#define kvm_pgtable_stage2_init(pgt, mmu, mm_ops) \
45862306a36Sopenharmony_ci	__kvm_pgtable_stage2_init(pgt, mmu, mm_ops, 0, NULL)
45962306a36Sopenharmony_ci
46062306a36Sopenharmony_ci/**
46162306a36Sopenharmony_ci * kvm_pgtable_stage2_destroy() - Destroy an unused guest stage-2 page-table.
46262306a36Sopenharmony_ci * @pgt:	Page-table structure initialised by kvm_pgtable_stage2_init*().
46362306a36Sopenharmony_ci *
46462306a36Sopenharmony_ci * The page-table is assumed to be unreachable by any hardware walkers prior
46562306a36Sopenharmony_ci * to freeing and therefore no TLB invalidation is performed.
46662306a36Sopenharmony_ci */
46762306a36Sopenharmony_civoid kvm_pgtable_stage2_destroy(struct kvm_pgtable *pgt);
46862306a36Sopenharmony_ci
46962306a36Sopenharmony_ci/**
47062306a36Sopenharmony_ci * kvm_pgtable_stage2_free_unlinked() - Free an unlinked stage-2 paging structure.
47162306a36Sopenharmony_ci * @mm_ops:	Memory management callbacks.
47262306a36Sopenharmony_ci * @pgtable:	Unlinked stage-2 paging structure to be freed.
47362306a36Sopenharmony_ci * @level:	Level of the stage-2 paging structure to be freed.
47462306a36Sopenharmony_ci *
47562306a36Sopenharmony_ci * The page-table is assumed to be unreachable by any hardware walkers prior to
47662306a36Sopenharmony_ci * freeing and therefore no TLB invalidation is performed.
47762306a36Sopenharmony_ci */
47862306a36Sopenharmony_civoid kvm_pgtable_stage2_free_unlinked(struct kvm_pgtable_mm_ops *mm_ops, void *pgtable, u32 level);
47962306a36Sopenharmony_ci
48062306a36Sopenharmony_ci/**
48162306a36Sopenharmony_ci * kvm_pgtable_stage2_create_unlinked() - Create an unlinked stage-2 paging structure.
48262306a36Sopenharmony_ci * @pgt:	Page-table structure initialised by kvm_pgtable_stage2_init*().
48362306a36Sopenharmony_ci * @phys:	Physical address of the memory to map.
48462306a36Sopenharmony_ci * @level:	Starting level of the stage-2 paging structure to be created.
48562306a36Sopenharmony_ci * @prot:	Permissions and attributes for the mapping.
48662306a36Sopenharmony_ci * @mc:		Cache of pre-allocated and zeroed memory from which to allocate
48762306a36Sopenharmony_ci *		page-table pages.
48862306a36Sopenharmony_ci * @force_pte:  Force mappings to PAGE_SIZE granularity.
48962306a36Sopenharmony_ci *
49062306a36Sopenharmony_ci * Returns an unlinked page-table tree.  This new page-table tree is
49162306a36Sopenharmony_ci * not reachable (i.e., it is unlinked) from the root pgd and it's
49262306a36Sopenharmony_ci * therefore unreachableby the hardware page-table walker. No TLB
49362306a36Sopenharmony_ci * invalidation or CMOs are performed.
49462306a36Sopenharmony_ci *
49562306a36Sopenharmony_ci * If device attributes are not explicitly requested in @prot, then the
49662306a36Sopenharmony_ci * mapping will be normal, cacheable.
49762306a36Sopenharmony_ci *
49862306a36Sopenharmony_ci * Return: The fully populated (unlinked) stage-2 paging structure, or
49962306a36Sopenharmony_ci * an ERR_PTR(error) on failure.
50062306a36Sopenharmony_ci */
50162306a36Sopenharmony_cikvm_pte_t *kvm_pgtable_stage2_create_unlinked(struct kvm_pgtable *pgt,
50262306a36Sopenharmony_ci					      u64 phys, u32 level,
50362306a36Sopenharmony_ci					      enum kvm_pgtable_prot prot,
50462306a36Sopenharmony_ci					      void *mc, bool force_pte);
50562306a36Sopenharmony_ci
50662306a36Sopenharmony_ci/**
50762306a36Sopenharmony_ci * kvm_pgtable_stage2_map() - Install a mapping in a guest stage-2 page-table.
50862306a36Sopenharmony_ci * @pgt:	Page-table structure initialised by kvm_pgtable_stage2_init*().
50962306a36Sopenharmony_ci * @addr:	Intermediate physical address at which to place the mapping.
51062306a36Sopenharmony_ci * @size:	Size of the mapping.
51162306a36Sopenharmony_ci * @phys:	Physical address of the memory to map.
51262306a36Sopenharmony_ci * @prot:	Permissions and attributes for the mapping.
51362306a36Sopenharmony_ci * @mc:		Cache of pre-allocated and zeroed memory from which to allocate
51462306a36Sopenharmony_ci *		page-table pages.
51562306a36Sopenharmony_ci * @flags:	Flags to control the page-table walk (ex. a shared walk)
51662306a36Sopenharmony_ci *
51762306a36Sopenharmony_ci * The offset of @addr within a page is ignored, @size is rounded-up to
51862306a36Sopenharmony_ci * the next page boundary and @phys is rounded-down to the previous page
51962306a36Sopenharmony_ci * boundary.
52062306a36Sopenharmony_ci *
52162306a36Sopenharmony_ci * If device attributes are not explicitly requested in @prot, then the
52262306a36Sopenharmony_ci * mapping will be normal, cacheable.
52362306a36Sopenharmony_ci *
52462306a36Sopenharmony_ci * Note that the update of a valid leaf PTE in this function will be aborted,
52562306a36Sopenharmony_ci * if it's trying to recreate the exact same mapping or only change the access
52662306a36Sopenharmony_ci * permissions. Instead, the vCPU will exit one more time from guest if still
52762306a36Sopenharmony_ci * needed and then go through the path of relaxing permissions.
52862306a36Sopenharmony_ci *
52962306a36Sopenharmony_ci * Note that this function will both coalesce existing table entries and split
53062306a36Sopenharmony_ci * existing block mappings, relying on page-faults to fault back areas outside
53162306a36Sopenharmony_ci * of the new mapping lazily.
53262306a36Sopenharmony_ci *
53362306a36Sopenharmony_ci * Return: 0 on success, negative error code on failure.
53462306a36Sopenharmony_ci */
53562306a36Sopenharmony_ciint kvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size,
53662306a36Sopenharmony_ci			   u64 phys, enum kvm_pgtable_prot prot,
53762306a36Sopenharmony_ci			   void *mc, enum kvm_pgtable_walk_flags flags);
53862306a36Sopenharmony_ci
53962306a36Sopenharmony_ci/**
54062306a36Sopenharmony_ci * kvm_pgtable_stage2_set_owner() - Unmap and annotate pages in the IPA space to
54162306a36Sopenharmony_ci *				    track ownership.
54262306a36Sopenharmony_ci * @pgt:	Page-table structure initialised by kvm_pgtable_stage2_init*().
54362306a36Sopenharmony_ci * @addr:	Base intermediate physical address to annotate.
54462306a36Sopenharmony_ci * @size:	Size of the annotated range.
54562306a36Sopenharmony_ci * @mc:		Cache of pre-allocated and zeroed memory from which to allocate
54662306a36Sopenharmony_ci *		page-table pages.
54762306a36Sopenharmony_ci * @owner_id:	Unique identifier for the owner of the page.
54862306a36Sopenharmony_ci *
54962306a36Sopenharmony_ci * By default, all page-tables are owned by identifier 0. This function can be
55062306a36Sopenharmony_ci * used to mark portions of the IPA space as owned by other entities. When a
55162306a36Sopenharmony_ci * stage 2 is used with identity-mappings, these annotations allow to use the
55262306a36Sopenharmony_ci * page-table data structure as a simple rmap.
55362306a36Sopenharmony_ci *
55462306a36Sopenharmony_ci * Return: 0 on success, negative error code on failure.
55562306a36Sopenharmony_ci */
55662306a36Sopenharmony_ciint kvm_pgtable_stage2_set_owner(struct kvm_pgtable *pgt, u64 addr, u64 size,
55762306a36Sopenharmony_ci				 void *mc, u8 owner_id);
55862306a36Sopenharmony_ci
55962306a36Sopenharmony_ci/**
56062306a36Sopenharmony_ci * kvm_pgtable_stage2_unmap() - Remove a mapping from a guest stage-2 page-table.
56162306a36Sopenharmony_ci * @pgt:	Page-table structure initialised by kvm_pgtable_stage2_init*().
56262306a36Sopenharmony_ci * @addr:	Intermediate physical address from which to remove the mapping.
56362306a36Sopenharmony_ci * @size:	Size of the mapping.
56462306a36Sopenharmony_ci *
56562306a36Sopenharmony_ci * The offset of @addr within a page is ignored and @size is rounded-up to
56662306a36Sopenharmony_ci * the next page boundary.
56762306a36Sopenharmony_ci *
56862306a36Sopenharmony_ci * TLB invalidation is performed for each page-table entry cleared during the
56962306a36Sopenharmony_ci * unmapping operation and the reference count for the page-table page
57062306a36Sopenharmony_ci * containing the cleared entry is decremented, with unreferenced pages being
57162306a36Sopenharmony_ci * freed. Unmapping a cacheable page will ensure that it is clean to the PoC if
57262306a36Sopenharmony_ci * FWB is not supported by the CPU.
57362306a36Sopenharmony_ci *
57462306a36Sopenharmony_ci * Return: 0 on success, negative error code on failure.
57562306a36Sopenharmony_ci */
57662306a36Sopenharmony_ciint kvm_pgtable_stage2_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size);
57762306a36Sopenharmony_ci
57862306a36Sopenharmony_ci/**
57962306a36Sopenharmony_ci * kvm_pgtable_stage2_wrprotect() - Write-protect guest stage-2 address range
58062306a36Sopenharmony_ci *                                  without TLB invalidation.
58162306a36Sopenharmony_ci * @pgt:	Page-table structure initialised by kvm_pgtable_stage2_init*().
58262306a36Sopenharmony_ci * @addr:	Intermediate physical address from which to write-protect,
58362306a36Sopenharmony_ci * @size:	Size of the range.
58462306a36Sopenharmony_ci *
58562306a36Sopenharmony_ci * The offset of @addr within a page is ignored and @size is rounded-up to
58662306a36Sopenharmony_ci * the next page boundary.
58762306a36Sopenharmony_ci *
58862306a36Sopenharmony_ci * Note that it is the caller's responsibility to invalidate the TLB after
58962306a36Sopenharmony_ci * calling this function to ensure that the updated permissions are visible
59062306a36Sopenharmony_ci * to the CPUs.
59162306a36Sopenharmony_ci *
59262306a36Sopenharmony_ci * Return: 0 on success, negative error code on failure.
59362306a36Sopenharmony_ci */
59462306a36Sopenharmony_ciint kvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size);
59562306a36Sopenharmony_ci
59662306a36Sopenharmony_ci/**
59762306a36Sopenharmony_ci * kvm_pgtable_stage2_mkyoung() - Set the access flag in a page-table entry.
59862306a36Sopenharmony_ci * @pgt:	Page-table structure initialised by kvm_pgtable_stage2_init*().
59962306a36Sopenharmony_ci * @addr:	Intermediate physical address to identify the page-table entry.
60062306a36Sopenharmony_ci *
60162306a36Sopenharmony_ci * The offset of @addr within a page is ignored.
60262306a36Sopenharmony_ci *
60362306a36Sopenharmony_ci * If there is a valid, leaf page-table entry used to translate @addr, then
60462306a36Sopenharmony_ci * set the access flag in that entry.
60562306a36Sopenharmony_ci *
60662306a36Sopenharmony_ci * Return: The old page-table entry prior to setting the flag, 0 on failure.
60762306a36Sopenharmony_ci */
60862306a36Sopenharmony_cikvm_pte_t kvm_pgtable_stage2_mkyoung(struct kvm_pgtable *pgt, u64 addr);
60962306a36Sopenharmony_ci
61062306a36Sopenharmony_ci/**
61162306a36Sopenharmony_ci * kvm_pgtable_stage2_test_clear_young() - Test and optionally clear the access
61262306a36Sopenharmony_ci *					   flag in a page-table entry.
61362306a36Sopenharmony_ci * @pgt:	Page-table structure initialised by kvm_pgtable_stage2_init*().
61462306a36Sopenharmony_ci * @addr:	Intermediate physical address to identify the page-table entry.
61562306a36Sopenharmony_ci * @size:	Size of the address range to visit.
61662306a36Sopenharmony_ci * @mkold:	True if the access flag should be cleared.
61762306a36Sopenharmony_ci *
61862306a36Sopenharmony_ci * The offset of @addr within a page is ignored.
61962306a36Sopenharmony_ci *
62062306a36Sopenharmony_ci * Tests and conditionally clears the access flag for every valid, leaf
62162306a36Sopenharmony_ci * page-table entry used to translate the range [@addr, @addr + @size).
62262306a36Sopenharmony_ci *
62362306a36Sopenharmony_ci * Note that it is the caller's responsibility to invalidate the TLB after
62462306a36Sopenharmony_ci * calling this function to ensure that the updated permissions are visible
62562306a36Sopenharmony_ci * to the CPUs.
62662306a36Sopenharmony_ci *
62762306a36Sopenharmony_ci * Return: True if any of the visited PTEs had the access flag set.
62862306a36Sopenharmony_ci */
62962306a36Sopenharmony_cibool kvm_pgtable_stage2_test_clear_young(struct kvm_pgtable *pgt, u64 addr,
63062306a36Sopenharmony_ci					 u64 size, bool mkold);
63162306a36Sopenharmony_ci
63262306a36Sopenharmony_ci/**
63362306a36Sopenharmony_ci * kvm_pgtable_stage2_relax_perms() - Relax the permissions enforced by a
63462306a36Sopenharmony_ci *				      page-table entry.
63562306a36Sopenharmony_ci * @pgt:	Page-table structure initialised by kvm_pgtable_stage2_init*().
63662306a36Sopenharmony_ci * @addr:	Intermediate physical address to identify the page-table entry.
63762306a36Sopenharmony_ci * @prot:	Additional permissions to grant for the mapping.
63862306a36Sopenharmony_ci *
63962306a36Sopenharmony_ci * The offset of @addr within a page is ignored.
64062306a36Sopenharmony_ci *
64162306a36Sopenharmony_ci * If there is a valid, leaf page-table entry used to translate @addr, then
64262306a36Sopenharmony_ci * relax the permissions in that entry according to the read, write and
64362306a36Sopenharmony_ci * execute permissions specified by @prot. No permissions are removed, and
64462306a36Sopenharmony_ci * TLB invalidation is performed after updating the entry. Software bits cannot
64562306a36Sopenharmony_ci * be set or cleared using kvm_pgtable_stage2_relax_perms().
64662306a36Sopenharmony_ci *
64762306a36Sopenharmony_ci * Return: 0 on success, negative error code on failure.
64862306a36Sopenharmony_ci */
64962306a36Sopenharmony_ciint kvm_pgtable_stage2_relax_perms(struct kvm_pgtable *pgt, u64 addr,
65062306a36Sopenharmony_ci				   enum kvm_pgtable_prot prot);
65162306a36Sopenharmony_ci
65262306a36Sopenharmony_ci/**
65362306a36Sopenharmony_ci * kvm_pgtable_stage2_flush_range() - Clean and invalidate data cache to Point
65462306a36Sopenharmony_ci * 				      of Coherency for guest stage-2 address
65562306a36Sopenharmony_ci *				      range.
65662306a36Sopenharmony_ci * @pgt:	Page-table structure initialised by kvm_pgtable_stage2_init*().
65762306a36Sopenharmony_ci * @addr:	Intermediate physical address from which to flush.
65862306a36Sopenharmony_ci * @size:	Size of the range.
65962306a36Sopenharmony_ci *
66062306a36Sopenharmony_ci * The offset of @addr within a page is ignored and @size is rounded-up to
66162306a36Sopenharmony_ci * the next page boundary.
66262306a36Sopenharmony_ci *
66362306a36Sopenharmony_ci * Return: 0 on success, negative error code on failure.
66462306a36Sopenharmony_ci */
66562306a36Sopenharmony_ciint kvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size);
66662306a36Sopenharmony_ci
66762306a36Sopenharmony_ci/**
66862306a36Sopenharmony_ci * kvm_pgtable_stage2_split() - Split a range of huge pages into leaf PTEs pointing
66962306a36Sopenharmony_ci *				to PAGE_SIZE guest pages.
67062306a36Sopenharmony_ci * @pgt:	 Page-table structure initialised by kvm_pgtable_stage2_init().
67162306a36Sopenharmony_ci * @addr:	 Intermediate physical address from which to split.
67262306a36Sopenharmony_ci * @size:	 Size of the range.
67362306a36Sopenharmony_ci * @mc:		 Cache of pre-allocated and zeroed memory from which to allocate
67462306a36Sopenharmony_ci *		 page-table pages.
67562306a36Sopenharmony_ci *
67662306a36Sopenharmony_ci * The function tries to split any level 1 or 2 entry that overlaps
67762306a36Sopenharmony_ci * with the input range (given by @addr and @size).
67862306a36Sopenharmony_ci *
67962306a36Sopenharmony_ci * Return: 0 on success, negative error code on failure. Note that
68062306a36Sopenharmony_ci * kvm_pgtable_stage2_split() is best effort: it tries to break as many
68162306a36Sopenharmony_ci * blocks in the input range as allowed by @mc_capacity.
68262306a36Sopenharmony_ci */
68362306a36Sopenharmony_ciint kvm_pgtable_stage2_split(struct kvm_pgtable *pgt, u64 addr, u64 size,
68462306a36Sopenharmony_ci			     struct kvm_mmu_memory_cache *mc);
68562306a36Sopenharmony_ci
68662306a36Sopenharmony_ci/**
68762306a36Sopenharmony_ci * kvm_pgtable_walk() - Walk a page-table.
68862306a36Sopenharmony_ci * @pgt:	Page-table structure initialised by kvm_pgtable_*_init().
68962306a36Sopenharmony_ci * @addr:	Input address for the start of the walk.
69062306a36Sopenharmony_ci * @size:	Size of the range to walk.
69162306a36Sopenharmony_ci * @walker:	Walker callback description.
69262306a36Sopenharmony_ci *
69362306a36Sopenharmony_ci * The offset of @addr within a page is ignored and @size is rounded-up to
69462306a36Sopenharmony_ci * the next page boundary.
69562306a36Sopenharmony_ci *
69662306a36Sopenharmony_ci * The walker will walk the page-table entries corresponding to the input
69762306a36Sopenharmony_ci * address range specified, visiting entries according to the walker flags.
69862306a36Sopenharmony_ci * Invalid entries are treated as leaf entries. The visited page table entry is
69962306a36Sopenharmony_ci * reloaded after invoking the walker callback, allowing the walker to descend
70062306a36Sopenharmony_ci * into a newly installed table.
70162306a36Sopenharmony_ci *
70262306a36Sopenharmony_ci * Returning a negative error code from the walker callback function will
70362306a36Sopenharmony_ci * terminate the walk immediately with the same error code.
70462306a36Sopenharmony_ci *
70562306a36Sopenharmony_ci * Return: 0 on success, negative error code on failure.
70662306a36Sopenharmony_ci */
70762306a36Sopenharmony_ciint kvm_pgtable_walk(struct kvm_pgtable *pgt, u64 addr, u64 size,
70862306a36Sopenharmony_ci		     struct kvm_pgtable_walker *walker);
70962306a36Sopenharmony_ci
71062306a36Sopenharmony_ci/**
71162306a36Sopenharmony_ci * kvm_pgtable_get_leaf() - Walk a page-table and retrieve the leaf entry
71262306a36Sopenharmony_ci *			    with its level.
71362306a36Sopenharmony_ci * @pgt:	Page-table structure initialised by kvm_pgtable_*_init()
71462306a36Sopenharmony_ci *		or a similar initialiser.
71562306a36Sopenharmony_ci * @addr:	Input address for the start of the walk.
71662306a36Sopenharmony_ci * @ptep:	Pointer to storage for the retrieved PTE.
71762306a36Sopenharmony_ci * @level:	Pointer to storage for the level of the retrieved PTE.
71862306a36Sopenharmony_ci *
71962306a36Sopenharmony_ci * The offset of @addr within a page is ignored.
72062306a36Sopenharmony_ci *
72162306a36Sopenharmony_ci * The walker will walk the page-table entries corresponding to the input
72262306a36Sopenharmony_ci * address specified, retrieving the leaf corresponding to this address.
72362306a36Sopenharmony_ci * Invalid entries are treated as leaf entries.
72462306a36Sopenharmony_ci *
72562306a36Sopenharmony_ci * Return: 0 on success, negative error code on failure.
72662306a36Sopenharmony_ci */
72762306a36Sopenharmony_ciint kvm_pgtable_get_leaf(struct kvm_pgtable *pgt, u64 addr,
72862306a36Sopenharmony_ci			 kvm_pte_t *ptep, u32 *level);
72962306a36Sopenharmony_ci
73062306a36Sopenharmony_ci/**
73162306a36Sopenharmony_ci * kvm_pgtable_stage2_pte_prot() - Retrieve the protection attributes of a
73262306a36Sopenharmony_ci *				   stage-2 Page-Table Entry.
73362306a36Sopenharmony_ci * @pte:	Page-table entry
73462306a36Sopenharmony_ci *
73562306a36Sopenharmony_ci * Return: protection attributes of the page-table entry in the enum
73662306a36Sopenharmony_ci *	   kvm_pgtable_prot format.
73762306a36Sopenharmony_ci */
73862306a36Sopenharmony_cienum kvm_pgtable_prot kvm_pgtable_stage2_pte_prot(kvm_pte_t pte);
73962306a36Sopenharmony_ci
74062306a36Sopenharmony_ci/**
74162306a36Sopenharmony_ci * kvm_pgtable_hyp_pte_prot() - Retrieve the protection attributes of a stage-1
74262306a36Sopenharmony_ci *				Page-Table Entry.
74362306a36Sopenharmony_ci * @pte:	Page-table entry
74462306a36Sopenharmony_ci *
74562306a36Sopenharmony_ci * Return: protection attributes of the page-table entry in the enum
74662306a36Sopenharmony_ci *	   kvm_pgtable_prot format.
74762306a36Sopenharmony_ci */
74862306a36Sopenharmony_cienum kvm_pgtable_prot kvm_pgtable_hyp_pte_prot(kvm_pte_t pte);
74962306a36Sopenharmony_ci
75062306a36Sopenharmony_ci/**
75162306a36Sopenharmony_ci * kvm_tlb_flush_vmid_range() - Invalidate/flush a range of TLB entries
75262306a36Sopenharmony_ci *
75362306a36Sopenharmony_ci * @mmu:	Stage-2 KVM MMU struct
75462306a36Sopenharmony_ci * @addr:	The base Intermediate physical address from which to invalidate
75562306a36Sopenharmony_ci * @size:	Size of the range from the base to invalidate
75662306a36Sopenharmony_ci */
75762306a36Sopenharmony_civoid kvm_tlb_flush_vmid_range(struct kvm_s2_mmu *mmu,
75862306a36Sopenharmony_ci				phys_addr_t addr, size_t size);
75962306a36Sopenharmony_ci#endif	/* __ARM64_KVM_PGTABLE_H__ */
760