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