Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2020 - Google LLC
0004  * Author: Quentin Perret <qperret@google.com>
0005  */
0006 #ifndef __ARM64_KVM_PKVM_H__
0007 #define __ARM64_KVM_PKVM_H__
0008 
0009 #include <linux/memblock.h>
0010 #include <asm/kvm_pgtable.h>
0011 
0012 #define HYP_MEMBLOCK_REGIONS 128
0013 
0014 extern struct memblock_region kvm_nvhe_sym(hyp_memory)[];
0015 extern unsigned int kvm_nvhe_sym(hyp_memblock_nr);
0016 
0017 static inline unsigned long __hyp_pgtable_max_pages(unsigned long nr_pages)
0018 {
0019     unsigned long total = 0, i;
0020 
0021     /* Provision the worst case scenario */
0022     for (i = 0; i < KVM_PGTABLE_MAX_LEVELS; i++) {
0023         nr_pages = DIV_ROUND_UP(nr_pages, PTRS_PER_PTE);
0024         total += nr_pages;
0025     }
0026 
0027     return total;
0028 }
0029 
0030 static inline unsigned long __hyp_pgtable_total_pages(void)
0031 {
0032     unsigned long res = 0, i;
0033 
0034     /* Cover all of memory with page-granularity */
0035     for (i = 0; i < kvm_nvhe_sym(hyp_memblock_nr); i++) {
0036         struct memblock_region *reg = &kvm_nvhe_sym(hyp_memory)[i];
0037         res += __hyp_pgtable_max_pages(reg->size >> PAGE_SHIFT);
0038     }
0039 
0040     return res;
0041 }
0042 
0043 static inline unsigned long hyp_s1_pgtable_pages(void)
0044 {
0045     unsigned long res;
0046 
0047     res = __hyp_pgtable_total_pages();
0048 
0049     /* Allow 1 GiB for private mappings */
0050     res += __hyp_pgtable_max_pages(SZ_1G >> PAGE_SHIFT);
0051 
0052     return res;
0053 }
0054 
0055 static inline unsigned long host_s2_pgtable_pages(void)
0056 {
0057     unsigned long res;
0058 
0059     /*
0060      * Include an extra 16 pages to safely upper-bound the worst case of
0061      * concatenated pgds.
0062      */
0063     res = __hyp_pgtable_total_pages() + 16;
0064 
0065     /* Allow 1 GiB for MMIO mappings */
0066     res += __hyp_pgtable_max_pages(SZ_1G >> PAGE_SHIFT);
0067 
0068     return res;
0069 }
0070 
0071 #endif  /* __ARM64_KVM_PKVM_H__ */