kvm_pkvm.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 - Google LLC
  4. * Author: Quentin Perret <qperret@google.com>
  5. */
  6. #ifndef __ARM64_KVM_PKVM_H__
  7. #define __ARM64_KVM_PKVM_H__
  8. #include <linux/arm_ffa.h>
  9. #include <linux/memblock.h>
  10. #include <linux/scatterlist.h>
  11. #include <asm/kvm_pgtable.h>
  12. /* Maximum number of VMs that can co-exist under pKVM. */
  13. #define KVM_MAX_PVMS 255
  14. #define HYP_MEMBLOCK_REGIONS 128
  15. int pkvm_init_host_vm(struct kvm *kvm);
  16. int pkvm_create_hyp_vm(struct kvm *kvm);
  17. void pkvm_destroy_hyp_vm(struct kvm *kvm);
  18. extern struct memblock_region kvm_nvhe_sym(hyp_memory)[];
  19. extern unsigned int kvm_nvhe_sym(hyp_memblock_nr);
  20. static inline unsigned long
  21. hyp_vmemmap_memblock_size(struct memblock_region *reg, size_t vmemmap_entry_size)
  22. {
  23. unsigned long nr_pages = reg->size >> PAGE_SHIFT;
  24. unsigned long start, end;
  25. start = (reg->base >> PAGE_SHIFT) * vmemmap_entry_size;
  26. end = start + nr_pages * vmemmap_entry_size;
  27. start = ALIGN_DOWN(start, PAGE_SIZE);
  28. end = ALIGN(end, PAGE_SIZE);
  29. return end - start;
  30. }
  31. static inline unsigned long hyp_vmemmap_pages(size_t vmemmap_entry_size)
  32. {
  33. unsigned long res = 0, i;
  34. for (i = 0; i < kvm_nvhe_sym(hyp_memblock_nr); i++) {
  35. res += hyp_vmemmap_memblock_size(&kvm_nvhe_sym(hyp_memory)[i],
  36. vmemmap_entry_size);
  37. }
  38. return res >> PAGE_SHIFT;
  39. }
  40. static inline unsigned long hyp_vm_table_pages(void)
  41. {
  42. return PAGE_ALIGN(KVM_MAX_PVMS * sizeof(void *)) >> PAGE_SHIFT;
  43. }
  44. static inline unsigned long __hyp_pgtable_max_pages(unsigned long nr_pages)
  45. {
  46. unsigned long total = 0;
  47. int i;
  48. /* Provision the worst case scenario */
  49. for (i = KVM_PGTABLE_FIRST_LEVEL; i <= KVM_PGTABLE_LAST_LEVEL; i++) {
  50. nr_pages = DIV_ROUND_UP(nr_pages, PTRS_PER_PTE);
  51. total += nr_pages;
  52. }
  53. return total;
  54. }
  55. static inline unsigned long __hyp_pgtable_total_pages(void)
  56. {
  57. unsigned long res = 0, i;
  58. /* Cover all of memory with page-granularity */
  59. for (i = 0; i < kvm_nvhe_sym(hyp_memblock_nr); i++) {
  60. struct memblock_region *reg = &kvm_nvhe_sym(hyp_memory)[i];
  61. res += __hyp_pgtable_max_pages(reg->size >> PAGE_SHIFT);
  62. }
  63. return res;
  64. }
  65. static inline unsigned long hyp_s1_pgtable_pages(void)
  66. {
  67. unsigned long res;
  68. res = __hyp_pgtable_total_pages();
  69. /* Allow 1 GiB for private mappings */
  70. res += __hyp_pgtable_max_pages(SZ_1G >> PAGE_SHIFT);
  71. return res;
  72. }
  73. static inline unsigned long host_s2_pgtable_pages(void)
  74. {
  75. unsigned long res;
  76. /*
  77. * Include an extra 16 pages to safely upper-bound the worst case of
  78. * concatenated pgds.
  79. */
  80. res = __hyp_pgtable_total_pages() + 16;
  81. /* Allow 1 GiB for MMIO mappings */
  82. res += __hyp_pgtable_max_pages(SZ_1G >> PAGE_SHIFT);
  83. return res;
  84. }
  85. #define KVM_FFA_MBOX_NR_PAGES 1
  86. static inline unsigned long hyp_ffa_proxy_pages(void)
  87. {
  88. size_t desc_max;
  89. /*
  90. * The hypervisor FFA proxy needs enough memory to buffer a fragmented
  91. * descriptor returned from EL3 in response to a RETRIEVE_REQ call.
  92. */
  93. desc_max = sizeof(struct ffa_mem_region) +
  94. sizeof(struct ffa_mem_region_attributes) +
  95. sizeof(struct ffa_composite_mem_region) +
  96. SG_MAX_SEGMENTS * sizeof(struct ffa_mem_region_addr_range);
  97. /* Plus a page each for the hypervisor's RX and TX mailboxes. */
  98. return (2 * KVM_FFA_MBOX_NR_PAGES) + DIV_ROUND_UP(desc_max, PAGE_SIZE);
  99. }
  100. static inline size_t pkvm_host_sve_state_size(void)
  101. {
  102. if (!system_supports_sve())
  103. return 0;
  104. return size_add(sizeof(struct cpu_sve_state),
  105. SVE_SIG_REGS_SIZE(sve_vq_from_vl(kvm_host_sve_max_vl)));
  106. }
  107. #endif /* __ARM64_KVM_PKVM_H__ */