gtt.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 <[email protected]>
  25. * Zhenyu Wang <[email protected]>
  26. * Xiao Zheng <[email protected]>
  27. *
  28. * Contributors:
  29. * Min He <[email protected]>
  30. * Bing Niu <[email protected]>
  31. *
  32. */
  33. #ifndef _GVT_GTT_H_
  34. #define _GVT_GTT_H_
  35. #include <linux/kernel.h>
  36. #include <linux/kref.h>
  37. #include <linux/mutex.h>
  38. #include <linux/radix-tree.h>
  39. #include "gt/intel_gtt.h"
  40. struct intel_gvt;
  41. struct intel_vgpu;
  42. struct intel_vgpu_mm;
  43. #define I915_GTT_PAGE_SHIFT 12
  44. #define INTEL_GVT_INVALID_ADDR (~0UL)
  45. struct intel_gvt_gtt_entry {
  46. u64 val64;
  47. int type;
  48. };
  49. struct intel_gvt_gtt_pte_ops {
  50. int (*get_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. int (*set_entry)(void *pt,
  57. struct intel_gvt_gtt_entry *e,
  58. unsigned long index,
  59. bool hypervisor_access,
  60. unsigned long gpa,
  61. struct intel_vgpu *vgpu);
  62. bool (*test_present)(struct intel_gvt_gtt_entry *e);
  63. void (*clear_present)(struct intel_gvt_gtt_entry *e);
  64. void (*set_present)(struct intel_gvt_gtt_entry *e);
  65. bool (*test_pse)(struct intel_gvt_gtt_entry *e);
  66. void (*clear_pse)(struct intel_gvt_gtt_entry *e);
  67. bool (*test_ips)(struct intel_gvt_gtt_entry *e);
  68. void (*clear_ips)(struct intel_gvt_gtt_entry *e);
  69. bool (*test_64k_splited)(struct intel_gvt_gtt_entry *e);
  70. void (*clear_64k_splited)(struct intel_gvt_gtt_entry *e);
  71. void (*set_64k_splited)(struct intel_gvt_gtt_entry *e);
  72. void (*set_pfn)(struct intel_gvt_gtt_entry *e, unsigned long pfn);
  73. unsigned long (*get_pfn)(struct intel_gvt_gtt_entry *e);
  74. };
  75. struct intel_gvt_gtt_gma_ops {
  76. unsigned long (*gma_to_ggtt_pte_index)(unsigned long gma);
  77. unsigned long (*gma_to_pte_index)(unsigned long gma);
  78. unsigned long (*gma_to_pde_index)(unsigned long gma);
  79. unsigned long (*gma_to_l3_pdp_index)(unsigned long gma);
  80. unsigned long (*gma_to_l4_pdp_index)(unsigned long gma);
  81. unsigned long (*gma_to_pml4_index)(unsigned long gma);
  82. };
  83. struct intel_gvt_gtt {
  84. const struct intel_gvt_gtt_pte_ops *pte_ops;
  85. const struct intel_gvt_gtt_gma_ops *gma_ops;
  86. int (*mm_alloc_page_table)(struct intel_vgpu_mm *mm);
  87. void (*mm_free_page_table)(struct intel_vgpu_mm *mm);
  88. struct list_head oos_page_use_list_head;
  89. struct list_head oos_page_free_list_head;
  90. struct mutex ppgtt_mm_lock;
  91. struct list_head ppgtt_mm_lru_list_head;
  92. struct page *scratch_page;
  93. unsigned long scratch_mfn;
  94. };
  95. enum intel_gvt_gtt_type {
  96. GTT_TYPE_INVALID = 0,
  97. GTT_TYPE_GGTT_PTE,
  98. GTT_TYPE_PPGTT_PTE_4K_ENTRY,
  99. GTT_TYPE_PPGTT_PTE_64K_ENTRY,
  100. GTT_TYPE_PPGTT_PTE_2M_ENTRY,
  101. GTT_TYPE_PPGTT_PTE_1G_ENTRY,
  102. GTT_TYPE_PPGTT_PTE_ENTRY,
  103. GTT_TYPE_PPGTT_PDE_ENTRY,
  104. GTT_TYPE_PPGTT_PDP_ENTRY,
  105. GTT_TYPE_PPGTT_PML4_ENTRY,
  106. GTT_TYPE_PPGTT_ROOT_ENTRY,
  107. GTT_TYPE_PPGTT_ROOT_L3_ENTRY,
  108. GTT_TYPE_PPGTT_ROOT_L4_ENTRY,
  109. GTT_TYPE_PPGTT_ENTRY,
  110. GTT_TYPE_PPGTT_PTE_PT,
  111. GTT_TYPE_PPGTT_PDE_PT,
  112. GTT_TYPE_PPGTT_PDP_PT,
  113. GTT_TYPE_PPGTT_PML4_PT,
  114. GTT_TYPE_MAX,
  115. };
  116. enum intel_gvt_mm_type {
  117. INTEL_GVT_MM_GGTT,
  118. INTEL_GVT_MM_PPGTT,
  119. };
  120. #define GVT_RING_CTX_NR_PDPS GEN8_3LVL_PDPES
  121. struct intel_gvt_partial_pte {
  122. unsigned long offset;
  123. u64 data;
  124. struct list_head list;
  125. };
  126. struct intel_vgpu_mm {
  127. enum intel_gvt_mm_type type;
  128. struct intel_vgpu *vgpu;
  129. struct kref ref;
  130. atomic_t pincount;
  131. union {
  132. struct {
  133. enum intel_gvt_gtt_type root_entry_type;
  134. /*
  135. * The 4 PDPs in ring context. For 48bit addressing,
  136. * only PDP0 is valid and point to PML4. For 32it
  137. * addressing, all 4 are used as true PDPs.
  138. */
  139. u64 guest_pdps[GVT_RING_CTX_NR_PDPS];
  140. u64 shadow_pdps[GVT_RING_CTX_NR_PDPS];
  141. bool shadowed;
  142. struct list_head list;
  143. struct list_head lru_list;
  144. struct list_head link; /* possible LRI shadow mm list */
  145. } ppgtt_mm;
  146. struct {
  147. void *virtual_ggtt;
  148. /* Save/restore for PM */
  149. u64 *host_ggtt_aperture;
  150. u64 *host_ggtt_hidden;
  151. struct list_head partial_pte_list;
  152. } ggtt_mm;
  153. };
  154. };
  155. struct intel_vgpu_mm *intel_vgpu_create_ppgtt_mm(struct intel_vgpu *vgpu,
  156. enum intel_gvt_gtt_type root_entry_type, u64 pdps[]);
  157. static inline void intel_vgpu_mm_get(struct intel_vgpu_mm *mm)
  158. {
  159. kref_get(&mm->ref);
  160. }
  161. void _intel_vgpu_mm_release(struct kref *mm_ref);
  162. static inline void intel_vgpu_mm_put(struct intel_vgpu_mm *mm)
  163. {
  164. kref_put(&mm->ref, _intel_vgpu_mm_release);
  165. }
  166. static inline void intel_vgpu_destroy_mm(struct intel_vgpu_mm *mm)
  167. {
  168. intel_vgpu_mm_put(mm);
  169. }
  170. struct intel_vgpu_guest_page;
  171. struct intel_vgpu_scratch_pt {
  172. struct page *page;
  173. unsigned long page_mfn;
  174. };
  175. struct intel_vgpu_gtt {
  176. struct intel_vgpu_mm *ggtt_mm;
  177. unsigned long active_ppgtt_mm_bitmap;
  178. struct list_head ppgtt_mm_list_head;
  179. struct radix_tree_root spt_tree;
  180. struct list_head oos_page_list_head;
  181. struct list_head post_shadow_list_head;
  182. struct intel_vgpu_scratch_pt scratch_pt[GTT_TYPE_MAX];
  183. };
  184. int intel_vgpu_init_gtt(struct intel_vgpu *vgpu);
  185. void intel_vgpu_clean_gtt(struct intel_vgpu *vgpu);
  186. void intel_vgpu_reset_ggtt(struct intel_vgpu *vgpu, bool invalidate_old);
  187. void intel_vgpu_invalidate_ppgtt(struct intel_vgpu *vgpu);
  188. int intel_gvt_init_gtt(struct intel_gvt *gvt);
  189. void intel_gvt_clean_gtt(struct intel_gvt *gvt);
  190. struct intel_vgpu_mm *intel_gvt_find_ppgtt_mm(struct intel_vgpu *vgpu,
  191. int page_table_level,
  192. void *root_entry);
  193. struct intel_vgpu_oos_page {
  194. struct intel_vgpu_ppgtt_spt *spt;
  195. struct list_head list;
  196. struct list_head vm_list;
  197. int id;
  198. void *mem;
  199. };
  200. #define GTT_ENTRY_NUM_IN_ONE_PAGE 512
  201. /* Represent a vgpu shadow page table. */
  202. struct intel_vgpu_ppgtt_spt {
  203. atomic_t refcount;
  204. struct intel_vgpu *vgpu;
  205. struct {
  206. enum intel_gvt_gtt_type type;
  207. bool pde_ips; /* for 64KB PTEs */
  208. void *vaddr;
  209. struct page *page;
  210. unsigned long mfn;
  211. } shadow_page;
  212. struct {
  213. enum intel_gvt_gtt_type type;
  214. bool pde_ips; /* for 64KB PTEs */
  215. unsigned long gfn;
  216. unsigned long write_cnt;
  217. struct intel_vgpu_oos_page *oos_page;
  218. } guest_page;
  219. DECLARE_BITMAP(post_shadow_bitmap, GTT_ENTRY_NUM_IN_ONE_PAGE);
  220. struct list_head post_shadow_list;
  221. };
  222. int intel_vgpu_sync_oos_pages(struct intel_vgpu *vgpu);
  223. int intel_vgpu_flush_post_shadow(struct intel_vgpu *vgpu);
  224. int intel_vgpu_pin_mm(struct intel_vgpu_mm *mm);
  225. void intel_vgpu_unpin_mm(struct intel_vgpu_mm *mm);
  226. unsigned long intel_vgpu_gma_to_gpa(struct intel_vgpu_mm *mm,
  227. unsigned long gma);
  228. struct intel_vgpu_mm *intel_vgpu_find_ppgtt_mm(struct intel_vgpu *vgpu,
  229. u64 pdps[]);
  230. struct intel_vgpu_mm *intel_vgpu_get_ppgtt_mm(struct intel_vgpu *vgpu,
  231. enum intel_gvt_gtt_type root_entry_type, u64 pdps[]);
  232. int intel_vgpu_put_ppgtt_mm(struct intel_vgpu *vgpu, u64 pdps[]);
  233. int intel_vgpu_emulate_ggtt_mmio_read(struct intel_vgpu *vgpu,
  234. unsigned int off, void *p_data, unsigned int bytes);
  235. int intel_vgpu_emulate_ggtt_mmio_write(struct intel_vgpu *vgpu,
  236. unsigned int off, void *p_data, unsigned int bytes);
  237. void intel_vgpu_destroy_all_ppgtt_mm(struct intel_vgpu *vgpu);
  238. void intel_gvt_restore_ggtt(struct intel_gvt *gvt);
  239. #endif /* _GVT_GTT_H_ */