1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 #ifndef __KVM_HYP_MM_H 3 #define __KVM_HYP_MM_H 4 5 #include <asm/kvm_pgtable.h> 6 #include <asm/spectre.h> 7 #include <linux/memblock.h> 8 #include <linux/types.h> 9 10 #include <nvhe/memory.h> 11 #include <nvhe/spinlock.h> 12 13 #define HYP_MEMBLOCK_REGIONS 128 14 extern struct memblock_region kvm_nvhe_sym(hyp_memory)[]; 15 extern unsigned int kvm_nvhe_sym(hyp_memblock_nr); 16 extern struct kvm_pgtable pkvm_pgtable; 17 extern hyp_spinlock_t pkvm_pgd_lock; 18 extern struct hyp_pool hpool; 19 extern u64 __io_map_base; 20 21 int hyp_create_idmap(u32 hyp_va_bits); 22 int hyp_map_vectors(void); 23 int hyp_back_vmemmap(phys_addr_t phys, unsigned long size, phys_addr_t back); 24 int pkvm_cpu_set_vector(enum arm64_hyp_spectre_vector slot); 25 int pkvm_create_mappings(void *from, void *to, enum kvm_pgtable_prot prot); 26 int pkvm_create_mappings_locked(void *from, void *to, enum kvm_pgtable_prot prot); 27 unsigned long __pkvm_create_private_mapping(phys_addr_t phys, size_t size, 28 enum kvm_pgtable_prot prot); 29 30 static inline void hyp_vmemmap_range(phys_addr_t phys, unsigned long size, 31 unsigned long *start, unsigned long *end) 32 { 33 unsigned long nr_pages = size >> PAGE_SHIFT; 34 struct hyp_page *p = hyp_phys_to_page(phys); 35 36 *start = (unsigned long)p; 37 *end = *start + nr_pages * sizeof(struct hyp_page); 38 *start = ALIGN_DOWN(*start, PAGE_SIZE); 39 *end = ALIGN(*end, PAGE_SIZE); 40 } 41 42 static inline unsigned long __hyp_pgtable_max_pages(unsigned long nr_pages) 43 { 44 unsigned long total = 0, i; 45 46 /* Provision the worst case scenario */ 47 for (i = 0; i < KVM_PGTABLE_MAX_LEVELS; i++) { 48 nr_pages = DIV_ROUND_UP(nr_pages, PTRS_PER_PTE); 49 total += nr_pages; 50 } 51 52 return total; 53 } 54 55 static inline unsigned long __hyp_pgtable_total_pages(void) 56 { 57 unsigned long res = 0, i; 58 59 /* Cover all of memory with page-granularity */ 60 for (i = 0; i < kvm_nvhe_sym(hyp_memblock_nr); i++) { 61 struct memblock_region *reg = &kvm_nvhe_sym(hyp_memory)[i]; 62 res += __hyp_pgtable_max_pages(reg->size >> PAGE_SHIFT); 63 } 64 65 return res; 66 } 67 68 static inline unsigned long hyp_s1_pgtable_pages(void) 69 { 70 unsigned long res; 71 72 res = __hyp_pgtable_total_pages(); 73 74 /* Allow 1 GiB for private mappings */ 75 res += __hyp_pgtable_max_pages(SZ_1G >> PAGE_SHIFT); 76 77 return res; 78 } 79 80 static inline unsigned long host_s2_pgtable_pages(void) 81 { 82 unsigned long res; 83 84 /* 85 * Include an extra 16 pages to safely upper-bound the worst case of 86 * concatenated pgds. 87 */ 88 res = __hyp_pgtable_total_pages() + 16; 89 90 /* Allow 1 GiB for MMIO mappings */ 91 res += __hyp_pgtable_max_pages(SZ_1G >> PAGE_SHIFT); 92 93 return res; 94 } 95 96 #endif /* __KVM_HYP_MM_H */ 97