gtt.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. *
  23. * Authors:
  24. * Zhi Wang <zhi.a.wang@intel.com>
  25. * Zhenyu Wang <zhenyuw@linux.intel.com>
  26. * Xiao Zheng <xiao.zheng@intel.com>
  27. *
  28. * Contributors:
  29. * Min He <min.he@intel.com>
  30. * Bing Niu <bing.niu@intel.com>
  31. *
  32. */
  33. #ifndef _GVT_GTT_H_
  34. #define _GVT_GTT_H_
  35. #define I915_GTT_PAGE_SHIFT 12
  36. #define I915_GTT_PAGE_MASK (~(I915_GTT_PAGE_SIZE - 1))
  37. struct intel_vgpu_mm;
  38. #define INTEL_GVT_INVALID_ADDR (~0UL)
  39. struct intel_gvt_gtt_entry {
  40. u64 val64;
  41. int type;
  42. };
  43. struct intel_gvt_gtt_pte_ops {
  44. int (*get_entry)(void *pt,
  45. struct intel_gvt_gtt_entry *e,
  46. unsigned long index,
  47. bool hypervisor_access,
  48. unsigned long gpa,
  49. struct intel_vgpu *vgpu);
  50. int (*set_entry)(void *pt,
  51. struct intel_gvt_gtt_entry *e,
  52. unsigned long index,
  53. bool hypervisor_access,
  54. unsigned long gpa,
  55. struct intel_vgpu *vgpu);
  56. bool (*test_present)(struct intel_gvt_gtt_entry *e);
  57. void (*clear_present)(struct intel_gvt_gtt_entry *e);
  58. void (*set_present)(struct intel_gvt_gtt_entry *e);
  59. bool (*test_pse)(struct intel_gvt_gtt_entry *e);
  60. void (*set_pfn)(struct intel_gvt_gtt_entry *e, unsigned long pfn);
  61. unsigned long (*get_pfn)(struct intel_gvt_gtt_entry *e);
  62. };
  63. struct intel_gvt_gtt_gma_ops {
  64. unsigned long (*gma_to_ggtt_pte_index)(unsigned long gma);
  65. unsigned long (*gma_to_pte_index)(unsigned long gma);
  66. unsigned long (*gma_to_pde_index)(unsigned long gma);
  67. unsigned long (*gma_to_l3_pdp_index)(unsigned long gma);
  68. unsigned long (*gma_to_l4_pdp_index)(unsigned long gma);
  69. unsigned long (*gma_to_pml4_index)(unsigned long gma);
  70. };
  71. struct intel_gvt_gtt {
  72. struct intel_gvt_gtt_pte_ops *pte_ops;
  73. struct intel_gvt_gtt_gma_ops *gma_ops;
  74. int (*mm_alloc_page_table)(struct intel_vgpu_mm *mm);
  75. void (*mm_free_page_table)(struct intel_vgpu_mm *mm);
  76. struct list_head oos_page_use_list_head;
  77. struct list_head oos_page_free_list_head;
  78. struct list_head ppgtt_mm_lru_list_head;
  79. struct page *scratch_page;
  80. unsigned long scratch_mfn;
  81. };
  82. typedef enum {
  83. GTT_TYPE_INVALID = -1,
  84. GTT_TYPE_GGTT_PTE,
  85. GTT_TYPE_PPGTT_PTE_4K_ENTRY,
  86. GTT_TYPE_PPGTT_PTE_2M_ENTRY,
  87. GTT_TYPE_PPGTT_PTE_1G_ENTRY,
  88. GTT_TYPE_PPGTT_PTE_ENTRY,
  89. GTT_TYPE_PPGTT_PDE_ENTRY,
  90. GTT_TYPE_PPGTT_PDP_ENTRY,
  91. GTT_TYPE_PPGTT_PML4_ENTRY,
  92. GTT_TYPE_PPGTT_ROOT_ENTRY,
  93. GTT_TYPE_PPGTT_ROOT_L3_ENTRY,
  94. GTT_TYPE_PPGTT_ROOT_L4_ENTRY,
  95. GTT_TYPE_PPGTT_ENTRY,
  96. GTT_TYPE_PPGTT_PTE_PT,
  97. GTT_TYPE_PPGTT_PDE_PT,
  98. GTT_TYPE_PPGTT_PDP_PT,
  99. GTT_TYPE_PPGTT_PML4_PT,
  100. GTT_TYPE_MAX,
  101. } intel_gvt_gtt_type_t;
  102. enum intel_gvt_mm_type {
  103. INTEL_GVT_MM_GGTT,
  104. INTEL_GVT_MM_PPGTT,
  105. };
  106. #define GVT_RING_CTX_NR_PDPS GEN8_3LVL_PDPES
  107. struct intel_vgpu_mm {
  108. enum intel_gvt_mm_type type;
  109. struct intel_vgpu *vgpu;
  110. struct kref ref;
  111. atomic_t pincount;
  112. union {
  113. struct {
  114. intel_gvt_gtt_type_t root_entry_type;
  115. /*
  116. * The 4 PDPs in ring context. For 48bit addressing,
  117. * only PDP0 is valid and point to PML4. For 32it
  118. * addressing, all 4 are used as true PDPs.
  119. */
  120. u64 guest_pdps[GVT_RING_CTX_NR_PDPS];
  121. u64 shadow_pdps[GVT_RING_CTX_NR_PDPS];
  122. bool shadowed;
  123. struct list_head list;
  124. struct list_head lru_list;
  125. } ppgtt_mm;
  126. struct {
  127. void *virtual_ggtt;
  128. } ggtt_mm;
  129. };
  130. };
  131. struct intel_vgpu_mm *intel_vgpu_create_ppgtt_mm(struct intel_vgpu *vgpu,
  132. intel_gvt_gtt_type_t root_entry_type, u64 pdps[]);
  133. static inline void intel_vgpu_mm_get(struct intel_vgpu_mm *mm)
  134. {
  135. kref_get(&mm->ref);
  136. }
  137. void _intel_vgpu_mm_release(struct kref *mm_ref);
  138. static inline void intel_vgpu_mm_put(struct intel_vgpu_mm *mm)
  139. {
  140. kref_put(&mm->ref, _intel_vgpu_mm_release);
  141. }
  142. static inline void intel_vgpu_destroy_mm(struct intel_vgpu_mm *mm)
  143. {
  144. intel_vgpu_mm_put(mm);
  145. }
  146. struct intel_vgpu_guest_page;
  147. struct intel_vgpu_scratch_pt {
  148. struct page *page;
  149. unsigned long page_mfn;
  150. };
  151. struct intel_vgpu_gtt {
  152. struct intel_vgpu_mm *ggtt_mm;
  153. unsigned long active_ppgtt_mm_bitmap;
  154. struct list_head ppgtt_mm_list_head;
  155. struct radix_tree_root spt_tree;
  156. struct list_head oos_page_list_head;
  157. struct list_head post_shadow_list_head;
  158. struct intel_vgpu_scratch_pt scratch_pt[GTT_TYPE_MAX];
  159. };
  160. extern int intel_vgpu_init_gtt(struct intel_vgpu *vgpu);
  161. extern void intel_vgpu_clean_gtt(struct intel_vgpu *vgpu);
  162. void intel_vgpu_reset_ggtt(struct intel_vgpu *vgpu, bool invalidate_old);
  163. void intel_vgpu_invalidate_ppgtt(struct intel_vgpu *vgpu);
  164. extern int intel_gvt_init_gtt(struct intel_gvt *gvt);
  165. void intel_vgpu_reset_gtt(struct intel_vgpu *vgpu);
  166. extern void intel_gvt_clean_gtt(struct intel_gvt *gvt);
  167. extern struct intel_vgpu_mm *intel_gvt_find_ppgtt_mm(struct intel_vgpu *vgpu,
  168. int page_table_level, void *root_entry);
  169. struct intel_vgpu_oos_page {
  170. struct intel_vgpu_ppgtt_spt *spt;
  171. struct list_head list;
  172. struct list_head vm_list;
  173. int id;
  174. unsigned char mem[I915_GTT_PAGE_SIZE];
  175. };
  176. #define GTT_ENTRY_NUM_IN_ONE_PAGE 512
  177. /* Represent a vgpu shadow page table. */
  178. struct intel_vgpu_ppgtt_spt {
  179. atomic_t refcount;
  180. struct intel_vgpu *vgpu;
  181. struct {
  182. intel_gvt_gtt_type_t type;
  183. void *vaddr;
  184. struct page *page;
  185. unsigned long mfn;
  186. } shadow_page;
  187. struct {
  188. intel_gvt_gtt_type_t type;
  189. unsigned long gfn;
  190. unsigned long write_cnt;
  191. struct intel_vgpu_oos_page *oos_page;
  192. } guest_page;
  193. DECLARE_BITMAP(post_shadow_bitmap, GTT_ENTRY_NUM_IN_ONE_PAGE);
  194. struct list_head post_shadow_list;
  195. };
  196. int intel_vgpu_sync_oos_pages(struct intel_vgpu *vgpu);
  197. int intel_vgpu_flush_post_shadow(struct intel_vgpu *vgpu);
  198. int intel_vgpu_pin_mm(struct intel_vgpu_mm *mm);
  199. void intel_vgpu_unpin_mm(struct intel_vgpu_mm *mm);
  200. unsigned long intel_vgpu_gma_to_gpa(struct intel_vgpu_mm *mm,
  201. unsigned long gma);
  202. struct intel_vgpu_mm *intel_vgpu_find_ppgtt_mm(struct intel_vgpu *vgpu,
  203. u64 pdps[]);
  204. struct intel_vgpu_mm *intel_vgpu_get_ppgtt_mm(struct intel_vgpu *vgpu,
  205. intel_gvt_gtt_type_t root_entry_type, u64 pdps[]);
  206. int intel_vgpu_put_ppgtt_mm(struct intel_vgpu *vgpu, u64 pdps[]);
  207. int intel_vgpu_emulate_ggtt_mmio_read(struct intel_vgpu *vgpu,
  208. unsigned int off, void *p_data, unsigned int bytes);
  209. int intel_vgpu_emulate_ggtt_mmio_write(struct intel_vgpu *vgpu,
  210. unsigned int off, void *p_data, unsigned int bytes);
  211. #endif /* _GVT_GTT_H_ */