1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020 - Google LLC
6 #ifndef __ARM64_KVM_PKVM_H__
7 #define __ARM64_KVM_PKVM_H__
9 #include <linux/memblock.h>
10 #include <asm/kvm_pgtable.h>
12 /* Maximum number of VMs that can co-exist under pKVM. */
13 #define KVM_MAX_PVMS 255
15 #define HYP_MEMBLOCK_REGIONS 128
17 int pkvm_init_host_vm(struct kvm *kvm);
18 int pkvm_create_hyp_vm(struct kvm *kvm);
19 void pkvm_destroy_hyp_vm(struct kvm *kvm);
21 extern struct memblock_region kvm_nvhe_sym(hyp_memory)[];
22 extern unsigned int kvm_nvhe_sym(hyp_memblock_nr);
24 static inline unsigned long
25 hyp_vmemmap_memblock_size(struct memblock_region *reg, size_t vmemmap_entry_size)
27 unsigned long nr_pages = reg->size >> PAGE_SHIFT;
28 unsigned long start, end;
30 start = (reg->base >> PAGE_SHIFT) * vmemmap_entry_size;
31 end = start + nr_pages * vmemmap_entry_size;
32 start = ALIGN_DOWN(start, PAGE_SIZE);
33 end = ALIGN(end, PAGE_SIZE);
38 static inline unsigned long hyp_vmemmap_pages(size_t vmemmap_entry_size)
40 unsigned long res = 0, i;
42 for (i = 0; i < kvm_nvhe_sym(hyp_memblock_nr); i++) {
43 res += hyp_vmemmap_memblock_size(&kvm_nvhe_sym(hyp_memory)[i],
47 return res >> PAGE_SHIFT;
50 static inline unsigned long hyp_vm_table_pages(void)
52 return PAGE_ALIGN(KVM_MAX_PVMS * sizeof(void *)) >> PAGE_SHIFT;
55 static inline unsigned long __hyp_pgtable_max_pages(unsigned long nr_pages)
57 unsigned long total = 0, i;
59 /* Provision the worst case scenario */
60 for (i = 0; i < KVM_PGTABLE_MAX_LEVELS; i++) {
61 nr_pages = DIV_ROUND_UP(nr_pages, PTRS_PER_PTE);
68 static inline unsigned long __hyp_pgtable_total_pages(void)
70 unsigned long res = 0, i;
72 /* Cover all of memory with page-granularity */
73 for (i = 0; i < kvm_nvhe_sym(hyp_memblock_nr); i++) {
74 struct memblock_region *reg = &kvm_nvhe_sym(hyp_memory)[i];
75 res += __hyp_pgtable_max_pages(reg->size >> PAGE_SHIFT);
81 static inline unsigned long hyp_s1_pgtable_pages(void)
85 res = __hyp_pgtable_total_pages();
87 /* Allow 1 GiB for private mappings */
88 res += __hyp_pgtable_max_pages(SZ_1G >> PAGE_SHIFT);
93 static inline unsigned long host_s2_pgtable_pages(void)
98 * Include an extra 16 pages to safely upper-bound the worst case of
101 res = __hyp_pgtable_total_pages() + 16;
103 /* Allow 1 GiB for MMIO mappings */
104 res += __hyp_pgtable_max_pages(SZ_1G >> PAGE_SHIFT);
109 #endif /* __ARM64_KVM_PKVM_H__ */