gtt.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. #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. struct list_head oos_page_use_list_head;
  87. struct list_head oos_page_free_list_head;
  88. struct mutex ppgtt_mm_lock;
  89. struct list_head ppgtt_mm_lru_list_head;
  90. struct page *scratch_page;
  91. unsigned long scratch_mfn;
  92. };
  93. enum intel_gvt_gtt_type {
  94. GTT_TYPE_INVALID = 0,
  95. GTT_TYPE_GGTT_PTE,
  96. GTT_TYPE_PPGTT_PTE_4K_ENTRY,
  97. GTT_TYPE_PPGTT_PTE_64K_ENTRY,
  98. GTT_TYPE_PPGTT_PTE_2M_ENTRY,
  99. GTT_TYPE_PPGTT_PTE_1G_ENTRY,
  100. GTT_TYPE_PPGTT_PTE_ENTRY,
  101. GTT_TYPE_PPGTT_PDE_ENTRY,
  102. GTT_TYPE_PPGTT_PDP_ENTRY,
  103. GTT_TYPE_PPGTT_PML4_ENTRY,
  104. GTT_TYPE_PPGTT_ROOT_ENTRY,
  105. GTT_TYPE_PPGTT_ROOT_L3_ENTRY,
  106. GTT_TYPE_PPGTT_ROOT_L4_ENTRY,
  107. GTT_TYPE_PPGTT_ENTRY,
  108. GTT_TYPE_PPGTT_PTE_PT,
  109. GTT_TYPE_PPGTT_PDE_PT,
  110. GTT_TYPE_PPGTT_PDP_PT,
  111. GTT_TYPE_PPGTT_PML4_PT,
  112. GTT_TYPE_MAX,
  113. };
  114. enum intel_gvt_mm_type {
  115. INTEL_GVT_MM_GGTT,
  116. INTEL_GVT_MM_PPGTT,
  117. };
  118. #define GVT_RING_CTX_NR_PDPS GEN8_3LVL_PDPES
  119. struct intel_gvt_partial_pte {
  120. unsigned long offset;
  121. u64 data;
  122. struct list_head list;
  123. };
  124. struct intel_vgpu_mm {
  125. enum intel_gvt_mm_type type;
  126. struct intel_vgpu *vgpu;
  127. struct kref ref;
  128. atomic_t pincount;
  129. union {
  130. struct {
  131. enum intel_gvt_gtt_type root_entry_type;
  132. /*
  133. * The 4 PDPs in ring context. For 48bit addressing,
  134. * only PDP0 is valid and point to PML4. For 32it
  135. * addressing, all 4 are used as true PDPs.
  136. */
  137. u64 guest_pdps[GVT_RING_CTX_NR_PDPS];
  138. u64 shadow_pdps[GVT_RING_CTX_NR_PDPS];
  139. bool shadowed;
  140. struct list_head list;
  141. struct list_head lru_list;
  142. struct list_head link; /* possible LRI shadow mm list */
  143. } ppgtt_mm;
  144. struct {
  145. void *virtual_ggtt;
  146. /* Save/restore for PM */
  147. u64 *host_ggtt_aperture;
  148. u64 *host_ggtt_hidden;
  149. struct list_head partial_pte_list;
  150. } ggtt_mm;
  151. };
  152. };
  153. struct intel_vgpu_mm *intel_vgpu_create_ppgtt_mm(struct intel_vgpu *vgpu,
  154. enum intel_gvt_gtt_type root_entry_type, u64 pdps[]);
  155. static inline void intel_vgpu_mm_get(struct intel_vgpu_mm *mm)
  156. {
  157. kref_get(&mm->ref);
  158. }
  159. void _intel_vgpu_mm_release(struct kref *mm_ref);
  160. static inline void intel_vgpu_mm_put(struct intel_vgpu_mm *mm)
  161. {
  162. kref_put(&mm->ref, _intel_vgpu_mm_release);
  163. }
  164. static inline void intel_vgpu_destroy_mm(struct intel_vgpu_mm *mm)
  165. {
  166. intel_vgpu_mm_put(mm);
  167. }
  168. struct intel_vgpu_guest_page;
  169. struct intel_vgpu_scratch_pt {
  170. struct page *page;
  171. unsigned long page_mfn;
  172. };
  173. struct intel_vgpu_gtt {
  174. struct intel_vgpu_mm *ggtt_mm;
  175. struct list_head ppgtt_mm_list_head;
  176. struct radix_tree_root spt_tree;
  177. struct list_head oos_page_list_head;
  178. struct list_head post_shadow_list_head;
  179. struct intel_vgpu_scratch_pt scratch_pt[GTT_TYPE_MAX];
  180. };
  181. int intel_vgpu_init_gtt(struct intel_vgpu *vgpu);
  182. void intel_vgpu_clean_gtt(struct intel_vgpu *vgpu);
  183. void intel_vgpu_reset_ggtt(struct intel_vgpu *vgpu, bool invalidate_old);
  184. void intel_vgpu_invalidate_ppgtt(struct intel_vgpu *vgpu);
  185. int intel_gvt_init_gtt(struct intel_gvt *gvt);
  186. void intel_gvt_clean_gtt(struct intel_gvt *gvt);
  187. struct intel_vgpu_mm *intel_gvt_find_ppgtt_mm(struct intel_vgpu *vgpu,
  188. int page_table_level,
  189. void *root_entry);
  190. struct intel_vgpu_oos_page {
  191. struct intel_vgpu_ppgtt_spt *spt;
  192. struct list_head list;
  193. struct list_head vm_list;
  194. int id;
  195. void *mem;
  196. };
  197. #define GTT_ENTRY_NUM_IN_ONE_PAGE 512
  198. /* Represent a vgpu shadow page table. */
  199. struct intel_vgpu_ppgtt_spt {
  200. atomic_t refcount;
  201. struct intel_vgpu *vgpu;
  202. struct {
  203. enum intel_gvt_gtt_type type;
  204. bool pde_ips; /* for 64KB PTEs */
  205. void *vaddr;
  206. struct page *page;
  207. unsigned long mfn;
  208. } shadow_page;
  209. struct {
  210. enum intel_gvt_gtt_type type;
  211. bool pde_ips; /* for 64KB PTEs */
  212. unsigned long gfn;
  213. unsigned long write_cnt;
  214. struct intel_vgpu_oos_page *oos_page;
  215. } guest_page;
  216. DECLARE_BITMAP(post_shadow_bitmap, GTT_ENTRY_NUM_IN_ONE_PAGE);
  217. struct list_head post_shadow_list;
  218. };
  219. int intel_vgpu_sync_oos_pages(struct intel_vgpu *vgpu);
  220. int intel_vgpu_flush_post_shadow(struct intel_vgpu *vgpu);
  221. int intel_vgpu_pin_mm(struct intel_vgpu_mm *mm);
  222. void intel_vgpu_unpin_mm(struct intel_vgpu_mm *mm);
  223. unsigned long intel_vgpu_gma_to_gpa(struct intel_vgpu_mm *mm,
  224. unsigned long gma);
  225. struct intel_vgpu_mm *intel_vgpu_find_ppgtt_mm(struct intel_vgpu *vgpu,
  226. u64 pdps[]);
  227. struct intel_vgpu_mm *intel_vgpu_get_ppgtt_mm(struct intel_vgpu *vgpu,
  228. enum intel_gvt_gtt_type root_entry_type, u64 pdps[]);
  229. int intel_vgpu_put_ppgtt_mm(struct intel_vgpu *vgpu, u64 pdps[]);
  230. int intel_vgpu_emulate_ggtt_mmio_read(struct intel_vgpu *vgpu,
  231. unsigned int off, void *p_data, unsigned int bytes);
  232. int intel_vgpu_emulate_ggtt_mmio_write(struct intel_vgpu *vgpu,
  233. unsigned int off, void *p_data, unsigned int bytes);
  234. void intel_vgpu_destroy_all_ppgtt_mm(struct intel_vgpu *vgpu);
  235. void intel_gvt_restore_ggtt(struct intel_gvt *gvt);
  236. #endif /* _GVT_GTT_H_ */