1 1.1 riastrad /* $NetBSD: gtt.c,v 1.2 2021/12/18 23:45:31 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * GTT virtualization 5 1.1 riastrad * 6 1.1 riastrad * Copyright(c) 2011-2016 Intel Corporation. All rights reserved. 7 1.1 riastrad * 8 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 9 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 10 1.1 riastrad * to deal in the Software without restriction, including without limitation 11 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 12 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 13 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 14 1.1 riastrad * 15 1.1 riastrad * The above copyright notice and this permission notice (including the next 16 1.1 riastrad * paragraph) shall be included in all copies or substantial portions of the 17 1.1 riastrad * Software. 18 1.1 riastrad * 19 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 22 1.1 riastrad * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 1.1 riastrad * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 1.1 riastrad * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 1.1 riastrad * SOFTWARE. 26 1.1 riastrad * 27 1.1 riastrad * Authors: 28 1.1 riastrad * Zhi Wang <zhi.a.wang (at) intel.com> 29 1.1 riastrad * Zhenyu Wang <zhenyuw (at) linux.intel.com> 30 1.1 riastrad * Xiao Zheng <xiao.zheng (at) intel.com> 31 1.1 riastrad * 32 1.1 riastrad * Contributors: 33 1.1 riastrad * Min He <min.he (at) intel.com> 34 1.1 riastrad * Bing Niu <bing.niu (at) intel.com> 35 1.1 riastrad * 36 1.1 riastrad */ 37 1.1 riastrad 38 1.1 riastrad #include <sys/cdefs.h> 39 1.1 riastrad __KERNEL_RCSID(0, "$NetBSD: gtt.c,v 1.2 2021/12/18 23:45:31 riastradh Exp $"); 40 1.1 riastrad 41 1.1 riastrad #include "i915_drv.h" 42 1.1 riastrad #include "gvt.h" 43 1.1 riastrad #include "i915_pvinfo.h" 44 1.1 riastrad #include "trace.h" 45 1.1 riastrad 46 1.1 riastrad #if defined(VERBOSE_DEBUG) 47 1.1 riastrad #define gvt_vdbg_mm(fmt, args...) gvt_dbg_mm(fmt, ##args) 48 1.1 riastrad #else 49 1.1 riastrad #define gvt_vdbg_mm(fmt, args...) 50 1.1 riastrad #endif 51 1.1 riastrad 52 1.1 riastrad static bool enable_out_of_sync = false; 53 1.1 riastrad static int preallocated_oos_pages = 8192; 54 1.1 riastrad 55 1.1 riastrad /* 56 1.1 riastrad * validate a gm address and related range size, 57 1.1 riastrad * translate it to host gm address 58 1.1 riastrad */ 59 1.1 riastrad bool intel_gvt_ggtt_validate_range(struct intel_vgpu *vgpu, u64 addr, u32 size) 60 1.1 riastrad { 61 1.1 riastrad if (size == 0) 62 1.1 riastrad return vgpu_gmadr_is_valid(vgpu, addr); 63 1.1 riastrad 64 1.1 riastrad if (vgpu_gmadr_is_aperture(vgpu, addr) && 65 1.1 riastrad vgpu_gmadr_is_aperture(vgpu, addr + size - 1)) 66 1.1 riastrad return true; 67 1.1 riastrad else if (vgpu_gmadr_is_hidden(vgpu, addr) && 68 1.1 riastrad vgpu_gmadr_is_hidden(vgpu, addr + size - 1)) 69 1.1 riastrad return true; 70 1.1 riastrad 71 1.1 riastrad gvt_dbg_mm("Invalid ggtt range at 0x%llx, size: 0x%x\n", 72 1.1 riastrad addr, size); 73 1.1 riastrad return false; 74 1.1 riastrad } 75 1.1 riastrad 76 1.1 riastrad /* translate a guest gmadr to host gmadr */ 77 1.1 riastrad int intel_gvt_ggtt_gmadr_g2h(struct intel_vgpu *vgpu, u64 g_addr, u64 *h_addr) 78 1.1 riastrad { 79 1.1 riastrad if (WARN(!vgpu_gmadr_is_valid(vgpu, g_addr), 80 1.1 riastrad "invalid guest gmadr %llx\n", g_addr)) 81 1.1 riastrad return -EACCES; 82 1.1 riastrad 83 1.1 riastrad if (vgpu_gmadr_is_aperture(vgpu, g_addr)) 84 1.1 riastrad *h_addr = vgpu_aperture_gmadr_base(vgpu) 85 1.1 riastrad + (g_addr - vgpu_aperture_offset(vgpu)); 86 1.1 riastrad else 87 1.1 riastrad *h_addr = vgpu_hidden_gmadr_base(vgpu) 88 1.1 riastrad + (g_addr - vgpu_hidden_offset(vgpu)); 89 1.1 riastrad return 0; 90 1.1 riastrad } 91 1.1 riastrad 92 1.1 riastrad /* translate a host gmadr to guest gmadr */ 93 1.1 riastrad int intel_gvt_ggtt_gmadr_h2g(struct intel_vgpu *vgpu, u64 h_addr, u64 *g_addr) 94 1.1 riastrad { 95 1.1 riastrad if (WARN(!gvt_gmadr_is_valid(vgpu->gvt, h_addr), 96 1.1 riastrad "invalid host gmadr %llx\n", h_addr)) 97 1.1 riastrad return -EACCES; 98 1.1 riastrad 99 1.1 riastrad if (gvt_gmadr_is_aperture(vgpu->gvt, h_addr)) 100 1.1 riastrad *g_addr = vgpu_aperture_gmadr_base(vgpu) 101 1.1 riastrad + (h_addr - gvt_aperture_gmadr_base(vgpu->gvt)); 102 1.1 riastrad else 103 1.1 riastrad *g_addr = vgpu_hidden_gmadr_base(vgpu) 104 1.1 riastrad + (h_addr - gvt_hidden_gmadr_base(vgpu->gvt)); 105 1.1 riastrad return 0; 106 1.1 riastrad } 107 1.1 riastrad 108 1.1 riastrad int intel_gvt_ggtt_index_g2h(struct intel_vgpu *vgpu, unsigned long g_index, 109 1.1 riastrad unsigned long *h_index) 110 1.1 riastrad { 111 1.1 riastrad u64 h_addr; 112 1.1 riastrad int ret; 113 1.1 riastrad 114 1.1 riastrad ret = intel_gvt_ggtt_gmadr_g2h(vgpu, g_index << I915_GTT_PAGE_SHIFT, 115 1.1 riastrad &h_addr); 116 1.1 riastrad if (ret) 117 1.1 riastrad return ret; 118 1.1 riastrad 119 1.1 riastrad *h_index = h_addr >> I915_GTT_PAGE_SHIFT; 120 1.1 riastrad return 0; 121 1.1 riastrad } 122 1.1 riastrad 123 1.1 riastrad int intel_gvt_ggtt_h2g_index(struct intel_vgpu *vgpu, unsigned long h_index, 124 1.1 riastrad unsigned long *g_index) 125 1.1 riastrad { 126 1.1 riastrad u64 g_addr; 127 1.1 riastrad int ret; 128 1.1 riastrad 129 1.1 riastrad ret = intel_gvt_ggtt_gmadr_h2g(vgpu, h_index << I915_GTT_PAGE_SHIFT, 130 1.1 riastrad &g_addr); 131 1.1 riastrad if (ret) 132 1.1 riastrad return ret; 133 1.1 riastrad 134 1.1 riastrad *g_index = g_addr >> I915_GTT_PAGE_SHIFT; 135 1.1 riastrad return 0; 136 1.1 riastrad } 137 1.1 riastrad 138 1.1 riastrad #define gtt_type_is_entry(type) \ 139 1.1 riastrad (type > GTT_TYPE_INVALID && type < GTT_TYPE_PPGTT_ENTRY \ 140 1.1 riastrad && type != GTT_TYPE_PPGTT_PTE_ENTRY \ 141 1.1 riastrad && type != GTT_TYPE_PPGTT_ROOT_ENTRY) 142 1.1 riastrad 143 1.1 riastrad #define gtt_type_is_pt(type) \ 144 1.1 riastrad (type >= GTT_TYPE_PPGTT_PTE_PT && type < GTT_TYPE_MAX) 145 1.1 riastrad 146 1.1 riastrad #define gtt_type_is_pte_pt(type) \ 147 1.1 riastrad (type == GTT_TYPE_PPGTT_PTE_PT) 148 1.1 riastrad 149 1.1 riastrad #define gtt_type_is_root_pointer(type) \ 150 1.1 riastrad (gtt_type_is_entry(type) && type > GTT_TYPE_PPGTT_ROOT_ENTRY) 151 1.1 riastrad 152 1.1 riastrad #define gtt_init_entry(e, t, p, v) do { \ 153 1.1 riastrad (e)->type = t; \ 154 1.1 riastrad (e)->pdev = p; \ 155 1.1 riastrad memcpy(&(e)->val64, &v, sizeof(v)); \ 156 1.1 riastrad } while (0) 157 1.1 riastrad 158 1.1 riastrad /* 159 1.1 riastrad * Mappings between GTT_TYPE* enumerations. 160 1.1 riastrad * Following information can be found according to the given type: 161 1.1 riastrad * - type of next level page table 162 1.1 riastrad * - type of entry inside this level page table 163 1.1 riastrad * - type of entry with PSE set 164 1.1 riastrad * 165 1.1 riastrad * If the given type doesn't have such a kind of information, 166 1.1 riastrad * e.g. give a l4 root entry type, then request to get its PSE type, 167 1.1 riastrad * give a PTE page table type, then request to get its next level page 168 1.1 riastrad * table type, as we know l4 root entry doesn't have a PSE bit, 169 1.1 riastrad * and a PTE page table doesn't have a next level page table type, 170 1.1 riastrad * GTT_TYPE_INVALID will be returned. This is useful when traversing a 171 1.1 riastrad * page table. 172 1.1 riastrad */ 173 1.1 riastrad 174 1.1 riastrad struct gtt_type_table_entry { 175 1.1 riastrad int entry_type; 176 1.1 riastrad int pt_type; 177 1.1 riastrad int next_pt_type; 178 1.1 riastrad int pse_entry_type; 179 1.1 riastrad }; 180 1.1 riastrad 181 1.1 riastrad #define GTT_TYPE_TABLE_ENTRY(type, e_type, cpt_type, npt_type, pse_type) \ 182 1.1 riastrad [type] = { \ 183 1.1 riastrad .entry_type = e_type, \ 184 1.1 riastrad .pt_type = cpt_type, \ 185 1.1 riastrad .next_pt_type = npt_type, \ 186 1.1 riastrad .pse_entry_type = pse_type, \ 187 1.1 riastrad } 188 1.1 riastrad 189 1.1 riastrad static struct gtt_type_table_entry gtt_type_table[] = { 190 1.1 riastrad GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_ROOT_L4_ENTRY, 191 1.1 riastrad GTT_TYPE_PPGTT_ROOT_L4_ENTRY, 192 1.1 riastrad GTT_TYPE_INVALID, 193 1.1 riastrad GTT_TYPE_PPGTT_PML4_PT, 194 1.1 riastrad GTT_TYPE_INVALID), 195 1.1 riastrad GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PML4_PT, 196 1.1 riastrad GTT_TYPE_PPGTT_PML4_ENTRY, 197 1.1 riastrad GTT_TYPE_PPGTT_PML4_PT, 198 1.1 riastrad GTT_TYPE_PPGTT_PDP_PT, 199 1.1 riastrad GTT_TYPE_INVALID), 200 1.1 riastrad GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PML4_ENTRY, 201 1.1 riastrad GTT_TYPE_PPGTT_PML4_ENTRY, 202 1.1 riastrad GTT_TYPE_PPGTT_PML4_PT, 203 1.1 riastrad GTT_TYPE_PPGTT_PDP_PT, 204 1.1 riastrad GTT_TYPE_INVALID), 205 1.1 riastrad GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PDP_PT, 206 1.1 riastrad GTT_TYPE_PPGTT_PDP_ENTRY, 207 1.1 riastrad GTT_TYPE_PPGTT_PDP_PT, 208 1.1 riastrad GTT_TYPE_PPGTT_PDE_PT, 209 1.1 riastrad GTT_TYPE_PPGTT_PTE_1G_ENTRY), 210 1.1 riastrad GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_ROOT_L3_ENTRY, 211 1.1 riastrad GTT_TYPE_PPGTT_ROOT_L3_ENTRY, 212 1.1 riastrad GTT_TYPE_INVALID, 213 1.1 riastrad GTT_TYPE_PPGTT_PDE_PT, 214 1.1 riastrad GTT_TYPE_PPGTT_PTE_1G_ENTRY), 215 1.1 riastrad GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PDP_ENTRY, 216 1.1 riastrad GTT_TYPE_PPGTT_PDP_ENTRY, 217 1.1 riastrad GTT_TYPE_PPGTT_PDP_PT, 218 1.1 riastrad GTT_TYPE_PPGTT_PDE_PT, 219 1.1 riastrad GTT_TYPE_PPGTT_PTE_1G_ENTRY), 220 1.1 riastrad GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PDE_PT, 221 1.1 riastrad GTT_TYPE_PPGTT_PDE_ENTRY, 222 1.1 riastrad GTT_TYPE_PPGTT_PDE_PT, 223 1.1 riastrad GTT_TYPE_PPGTT_PTE_PT, 224 1.1 riastrad GTT_TYPE_PPGTT_PTE_2M_ENTRY), 225 1.1 riastrad GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PDE_ENTRY, 226 1.1 riastrad GTT_TYPE_PPGTT_PDE_ENTRY, 227 1.1 riastrad GTT_TYPE_PPGTT_PDE_PT, 228 1.1 riastrad GTT_TYPE_PPGTT_PTE_PT, 229 1.1 riastrad GTT_TYPE_PPGTT_PTE_2M_ENTRY), 230 1.1 riastrad /* We take IPS bit as 'PSE' for PTE level. */ 231 1.1 riastrad GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PTE_PT, 232 1.1 riastrad GTT_TYPE_PPGTT_PTE_4K_ENTRY, 233 1.1 riastrad GTT_TYPE_PPGTT_PTE_PT, 234 1.1 riastrad GTT_TYPE_INVALID, 235 1.1 riastrad GTT_TYPE_PPGTT_PTE_64K_ENTRY), 236 1.1 riastrad GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PTE_4K_ENTRY, 237 1.1 riastrad GTT_TYPE_PPGTT_PTE_4K_ENTRY, 238 1.1 riastrad GTT_TYPE_PPGTT_PTE_PT, 239 1.1 riastrad GTT_TYPE_INVALID, 240 1.1 riastrad GTT_TYPE_PPGTT_PTE_64K_ENTRY), 241 1.1 riastrad GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PTE_64K_ENTRY, 242 1.1 riastrad GTT_TYPE_PPGTT_PTE_4K_ENTRY, 243 1.1 riastrad GTT_TYPE_PPGTT_PTE_PT, 244 1.1 riastrad GTT_TYPE_INVALID, 245 1.1 riastrad GTT_TYPE_PPGTT_PTE_64K_ENTRY), 246 1.1 riastrad GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PTE_2M_ENTRY, 247 1.1 riastrad GTT_TYPE_PPGTT_PDE_ENTRY, 248 1.1 riastrad GTT_TYPE_PPGTT_PDE_PT, 249 1.1 riastrad GTT_TYPE_INVALID, 250 1.1 riastrad GTT_TYPE_PPGTT_PTE_2M_ENTRY), 251 1.1 riastrad GTT_TYPE_TABLE_ENTRY(GTT_TYPE_PPGTT_PTE_1G_ENTRY, 252 1.1 riastrad GTT_TYPE_PPGTT_PDP_ENTRY, 253 1.1 riastrad GTT_TYPE_PPGTT_PDP_PT, 254 1.1 riastrad GTT_TYPE_INVALID, 255 1.1 riastrad GTT_TYPE_PPGTT_PTE_1G_ENTRY), 256 1.1 riastrad GTT_TYPE_TABLE_ENTRY(GTT_TYPE_GGTT_PTE, 257 1.1 riastrad GTT_TYPE_GGTT_PTE, 258 1.1 riastrad GTT_TYPE_INVALID, 259 1.1 riastrad GTT_TYPE_INVALID, 260 1.1 riastrad GTT_TYPE_INVALID), 261 1.1 riastrad }; 262 1.1 riastrad 263 1.1 riastrad static inline int get_next_pt_type(int type) 264 1.1 riastrad { 265 1.1 riastrad return gtt_type_table[type].next_pt_type; 266 1.1 riastrad } 267 1.1 riastrad 268 1.1 riastrad static inline int get_pt_type(int type) 269 1.1 riastrad { 270 1.1 riastrad return gtt_type_table[type].pt_type; 271 1.1 riastrad } 272 1.1 riastrad 273 1.1 riastrad static inline int get_entry_type(int type) 274 1.1 riastrad { 275 1.1 riastrad return gtt_type_table[type].entry_type; 276 1.1 riastrad } 277 1.1 riastrad 278 1.1 riastrad static inline int get_pse_type(int type) 279 1.1 riastrad { 280 1.1 riastrad return gtt_type_table[type].pse_entry_type; 281 1.1 riastrad } 282 1.1 riastrad 283 1.1 riastrad static u64 read_pte64(struct drm_i915_private *dev_priv, unsigned long index) 284 1.1 riastrad { 285 1.1 riastrad void __iomem *addr = (gen8_pte_t __iomem *)dev_priv->ggtt.gsm + index; 286 1.1 riastrad 287 1.1 riastrad return readq(addr); 288 1.1 riastrad } 289 1.1 riastrad 290 1.1 riastrad static void ggtt_invalidate(struct drm_i915_private *dev_priv) 291 1.1 riastrad { 292 1.1 riastrad mmio_hw_access_pre(dev_priv); 293 1.1 riastrad I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN); 294 1.1 riastrad mmio_hw_access_post(dev_priv); 295 1.1 riastrad } 296 1.1 riastrad 297 1.1 riastrad static void write_pte64(struct drm_i915_private *dev_priv, 298 1.1 riastrad unsigned long index, u64 pte) 299 1.1 riastrad { 300 1.1 riastrad void __iomem *addr = (gen8_pte_t __iomem *)dev_priv->ggtt.gsm + index; 301 1.1 riastrad 302 1.1 riastrad writeq(pte, addr); 303 1.1 riastrad } 304 1.1 riastrad 305 1.1 riastrad static inline int gtt_get_entry64(void *pt, 306 1.1 riastrad struct intel_gvt_gtt_entry *e, 307 1.1 riastrad unsigned long index, bool hypervisor_access, unsigned long gpa, 308 1.1 riastrad struct intel_vgpu *vgpu) 309 1.1 riastrad { 310 1.1 riastrad const struct intel_gvt_device_info *info = &vgpu->gvt->device_info; 311 1.1 riastrad int ret; 312 1.1 riastrad 313 1.1 riastrad if (WARN_ON(info->gtt_entry_size != 8)) 314 1.1 riastrad return -EINVAL; 315 1.1 riastrad 316 1.1 riastrad if (hypervisor_access) { 317 1.1 riastrad ret = intel_gvt_hypervisor_read_gpa(vgpu, gpa + 318 1.1 riastrad (index << info->gtt_entry_size_shift), 319 1.1 riastrad &e->val64, 8); 320 1.1 riastrad if (WARN_ON(ret)) 321 1.1 riastrad return ret; 322 1.1 riastrad } else if (!pt) { 323 1.1 riastrad e->val64 = read_pte64(vgpu->gvt->dev_priv, index); 324 1.1 riastrad } else { 325 1.1 riastrad e->val64 = *((u64 *)pt + index); 326 1.1 riastrad } 327 1.1 riastrad return 0; 328 1.1 riastrad } 329 1.1 riastrad 330 1.1 riastrad static inline int gtt_set_entry64(void *pt, 331 1.1 riastrad struct intel_gvt_gtt_entry *e, 332 1.1 riastrad unsigned long index, bool hypervisor_access, unsigned long gpa, 333 1.1 riastrad struct intel_vgpu *vgpu) 334 1.1 riastrad { 335 1.1 riastrad const struct intel_gvt_device_info *info = &vgpu->gvt->device_info; 336 1.1 riastrad int ret; 337 1.1 riastrad 338 1.1 riastrad if (WARN_ON(info->gtt_entry_size != 8)) 339 1.1 riastrad return -EINVAL; 340 1.1 riastrad 341 1.1 riastrad if (hypervisor_access) { 342 1.1 riastrad ret = intel_gvt_hypervisor_write_gpa(vgpu, gpa + 343 1.1 riastrad (index << info->gtt_entry_size_shift), 344 1.1 riastrad &e->val64, 8); 345 1.1 riastrad if (WARN_ON(ret)) 346 1.1 riastrad return ret; 347 1.1 riastrad } else if (!pt) { 348 1.1 riastrad write_pte64(vgpu->gvt->dev_priv, index, e->val64); 349 1.1 riastrad } else { 350 1.1 riastrad *((u64 *)pt + index) = e->val64; 351 1.1 riastrad } 352 1.1 riastrad return 0; 353 1.1 riastrad } 354 1.1 riastrad 355 1.1 riastrad #define GTT_HAW 46 356 1.1 riastrad 357 1.1 riastrad #define ADDR_1G_MASK GENMASK_ULL(GTT_HAW - 1, 30) 358 1.1 riastrad #define ADDR_2M_MASK GENMASK_ULL(GTT_HAW - 1, 21) 359 1.1 riastrad #define ADDR_64K_MASK GENMASK_ULL(GTT_HAW - 1, 16) 360 1.1 riastrad #define ADDR_4K_MASK GENMASK_ULL(GTT_HAW - 1, 12) 361 1.1 riastrad 362 1.1 riastrad #define GTT_SPTE_FLAG_MASK GENMASK_ULL(62, 52) 363 1.1 riastrad #define GTT_SPTE_FLAG_64K_SPLITED BIT(52) /* splited 64K gtt entry */ 364 1.1 riastrad 365 1.1 riastrad #define GTT_64K_PTE_STRIDE 16 366 1.1 riastrad 367 1.1 riastrad static unsigned long gen8_gtt_get_pfn(struct intel_gvt_gtt_entry *e) 368 1.1 riastrad { 369 1.1 riastrad unsigned long pfn; 370 1.1 riastrad 371 1.1 riastrad if (e->type == GTT_TYPE_PPGTT_PTE_1G_ENTRY) 372 1.1 riastrad pfn = (e->val64 & ADDR_1G_MASK) >> PAGE_SHIFT; 373 1.1 riastrad else if (e->type == GTT_TYPE_PPGTT_PTE_2M_ENTRY) 374 1.1 riastrad pfn = (e->val64 & ADDR_2M_MASK) >> PAGE_SHIFT; 375 1.1 riastrad else if (e->type == GTT_TYPE_PPGTT_PTE_64K_ENTRY) 376 1.1 riastrad pfn = (e->val64 & ADDR_64K_MASK) >> PAGE_SHIFT; 377 1.1 riastrad else 378 1.1 riastrad pfn = (e->val64 & ADDR_4K_MASK) >> PAGE_SHIFT; 379 1.1 riastrad return pfn; 380 1.1 riastrad } 381 1.1 riastrad 382 1.1 riastrad static void gen8_gtt_set_pfn(struct intel_gvt_gtt_entry *e, unsigned long pfn) 383 1.1 riastrad { 384 1.1 riastrad if (e->type == GTT_TYPE_PPGTT_PTE_1G_ENTRY) { 385 1.1 riastrad e->val64 &= ~ADDR_1G_MASK; 386 1.1 riastrad pfn &= (ADDR_1G_MASK >> PAGE_SHIFT); 387 1.1 riastrad } else if (e->type == GTT_TYPE_PPGTT_PTE_2M_ENTRY) { 388 1.1 riastrad e->val64 &= ~ADDR_2M_MASK; 389 1.1 riastrad pfn &= (ADDR_2M_MASK >> PAGE_SHIFT); 390 1.1 riastrad } else if (e->type == GTT_TYPE_PPGTT_PTE_64K_ENTRY) { 391 1.1 riastrad e->val64 &= ~ADDR_64K_MASK; 392 1.1 riastrad pfn &= (ADDR_64K_MASK >> PAGE_SHIFT); 393 1.1 riastrad } else { 394 1.1 riastrad e->val64 &= ~ADDR_4K_MASK; 395 1.1 riastrad pfn &= (ADDR_4K_MASK >> PAGE_SHIFT); 396 1.1 riastrad } 397 1.1 riastrad 398 1.1 riastrad e->val64 |= (pfn << PAGE_SHIFT); 399 1.1 riastrad } 400 1.1 riastrad 401 1.1 riastrad static bool gen8_gtt_test_pse(struct intel_gvt_gtt_entry *e) 402 1.1 riastrad { 403 1.1 riastrad return !!(e->val64 & _PAGE_PSE); 404 1.1 riastrad } 405 1.1 riastrad 406 1.1 riastrad static void gen8_gtt_clear_pse(struct intel_gvt_gtt_entry *e) 407 1.1 riastrad { 408 1.1 riastrad if (gen8_gtt_test_pse(e)) { 409 1.1 riastrad switch (e->type) { 410 1.1 riastrad case GTT_TYPE_PPGTT_PTE_2M_ENTRY: 411 1.1 riastrad e->val64 &= ~_PAGE_PSE; 412 1.1 riastrad e->type = GTT_TYPE_PPGTT_PDE_ENTRY; 413 1.1 riastrad break; 414 1.1 riastrad case GTT_TYPE_PPGTT_PTE_1G_ENTRY: 415 1.1 riastrad e->type = GTT_TYPE_PPGTT_PDP_ENTRY; 416 1.1 riastrad e->val64 &= ~_PAGE_PSE; 417 1.1 riastrad break; 418 1.1 riastrad default: 419 1.1 riastrad WARN_ON(1); 420 1.1 riastrad } 421 1.1 riastrad } 422 1.1 riastrad } 423 1.1 riastrad 424 1.1 riastrad static bool gen8_gtt_test_ips(struct intel_gvt_gtt_entry *e) 425 1.1 riastrad { 426 1.1 riastrad if (GEM_WARN_ON(e->type != GTT_TYPE_PPGTT_PDE_ENTRY)) 427 1.1 riastrad return false; 428 1.1 riastrad 429 1.1 riastrad return !!(e->val64 & GEN8_PDE_IPS_64K); 430 1.1 riastrad } 431 1.1 riastrad 432 1.1 riastrad static void gen8_gtt_clear_ips(struct intel_gvt_gtt_entry *e) 433 1.1 riastrad { 434 1.1 riastrad if (GEM_WARN_ON(e->type != GTT_TYPE_PPGTT_PDE_ENTRY)) 435 1.1 riastrad return; 436 1.1 riastrad 437 1.1 riastrad e->val64 &= ~GEN8_PDE_IPS_64K; 438 1.1 riastrad } 439 1.1 riastrad 440 1.1 riastrad static bool gen8_gtt_test_present(struct intel_gvt_gtt_entry *e) 441 1.1 riastrad { 442 1.1 riastrad /* 443 1.1 riastrad * i915 writes PDP root pointer registers without present bit, 444 1.1 riastrad * it also works, so we need to treat root pointer entry 445 1.1 riastrad * specifically. 446 1.1 riastrad */ 447 1.1 riastrad if (e->type == GTT_TYPE_PPGTT_ROOT_L3_ENTRY 448 1.1 riastrad || e->type == GTT_TYPE_PPGTT_ROOT_L4_ENTRY) 449 1.1 riastrad return (e->val64 != 0); 450 1.1 riastrad else 451 1.1 riastrad return (e->val64 & _PAGE_PRESENT); 452 1.1 riastrad } 453 1.1 riastrad 454 1.1 riastrad static void gtt_entry_clear_present(struct intel_gvt_gtt_entry *e) 455 1.1 riastrad { 456 1.1 riastrad e->val64 &= ~_PAGE_PRESENT; 457 1.1 riastrad } 458 1.1 riastrad 459 1.1 riastrad static void gtt_entry_set_present(struct intel_gvt_gtt_entry *e) 460 1.1 riastrad { 461 1.1 riastrad e->val64 |= _PAGE_PRESENT; 462 1.1 riastrad } 463 1.1 riastrad 464 1.1 riastrad static bool gen8_gtt_test_64k_splited(struct intel_gvt_gtt_entry *e) 465 1.1 riastrad { 466 1.1 riastrad return !!(e->val64 & GTT_SPTE_FLAG_64K_SPLITED); 467 1.1 riastrad } 468 1.1 riastrad 469 1.1 riastrad static void gen8_gtt_set_64k_splited(struct intel_gvt_gtt_entry *e) 470 1.1 riastrad { 471 1.1 riastrad e->val64 |= GTT_SPTE_FLAG_64K_SPLITED; 472 1.1 riastrad } 473 1.1 riastrad 474 1.1 riastrad static void gen8_gtt_clear_64k_splited(struct intel_gvt_gtt_entry *e) 475 1.1 riastrad { 476 1.1 riastrad e->val64 &= ~GTT_SPTE_FLAG_64K_SPLITED; 477 1.1 riastrad } 478 1.1 riastrad 479 1.1 riastrad /* 480 1.1 riastrad * Per-platform GMA routines. 481 1.1 riastrad */ 482 1.1 riastrad static unsigned long gma_to_ggtt_pte_index(unsigned long gma) 483 1.1 riastrad { 484 1.1 riastrad unsigned long x = (gma >> I915_GTT_PAGE_SHIFT); 485 1.1 riastrad 486 1.1 riastrad trace_gma_index(__func__, gma, x); 487 1.1 riastrad return x; 488 1.1 riastrad } 489 1.1 riastrad 490 1.1 riastrad #define DEFINE_PPGTT_GMA_TO_INDEX(prefix, ename, exp) \ 491 1.1 riastrad static unsigned long prefix##_gma_to_##ename##_index(unsigned long gma) \ 492 1.1 riastrad { \ 493 1.1 riastrad unsigned long x = (exp); \ 494 1.1 riastrad trace_gma_index(__func__, gma, x); \ 495 1.1 riastrad return x; \ 496 1.1 riastrad } 497 1.1 riastrad 498 1.1 riastrad DEFINE_PPGTT_GMA_TO_INDEX(gen8, pte, (gma >> 12 & 0x1ff)); 499 1.1 riastrad DEFINE_PPGTT_GMA_TO_INDEX(gen8, pde, (gma >> 21 & 0x1ff)); 500 1.1 riastrad DEFINE_PPGTT_GMA_TO_INDEX(gen8, l3_pdp, (gma >> 30 & 0x3)); 501 1.1 riastrad DEFINE_PPGTT_GMA_TO_INDEX(gen8, l4_pdp, (gma >> 30 & 0x1ff)); 502 1.1 riastrad DEFINE_PPGTT_GMA_TO_INDEX(gen8, pml4, (gma >> 39 & 0x1ff)); 503 1.1 riastrad 504 1.1 riastrad static struct intel_gvt_gtt_pte_ops gen8_gtt_pte_ops = { 505 1.1 riastrad .get_entry = gtt_get_entry64, 506 1.1 riastrad .set_entry = gtt_set_entry64, 507 1.1 riastrad .clear_present = gtt_entry_clear_present, 508 1.1 riastrad .set_present = gtt_entry_set_present, 509 1.1 riastrad .test_present = gen8_gtt_test_present, 510 1.1 riastrad .test_pse = gen8_gtt_test_pse, 511 1.1 riastrad .clear_pse = gen8_gtt_clear_pse, 512 1.1 riastrad .clear_ips = gen8_gtt_clear_ips, 513 1.1 riastrad .test_ips = gen8_gtt_test_ips, 514 1.1 riastrad .clear_64k_splited = gen8_gtt_clear_64k_splited, 515 1.1 riastrad .set_64k_splited = gen8_gtt_set_64k_splited, 516 1.1 riastrad .test_64k_splited = gen8_gtt_test_64k_splited, 517 1.1 riastrad .get_pfn = gen8_gtt_get_pfn, 518 1.1 riastrad .set_pfn = gen8_gtt_set_pfn, 519 1.1 riastrad }; 520 1.1 riastrad 521 1.1 riastrad static struct intel_gvt_gtt_gma_ops gen8_gtt_gma_ops = { 522 1.1 riastrad .gma_to_ggtt_pte_index = gma_to_ggtt_pte_index, 523 1.1 riastrad .gma_to_pte_index = gen8_gma_to_pte_index, 524 1.1 riastrad .gma_to_pde_index = gen8_gma_to_pde_index, 525 1.1 riastrad .gma_to_l3_pdp_index = gen8_gma_to_l3_pdp_index, 526 1.1 riastrad .gma_to_l4_pdp_index = gen8_gma_to_l4_pdp_index, 527 1.1 riastrad .gma_to_pml4_index = gen8_gma_to_pml4_index, 528 1.1 riastrad }; 529 1.1 riastrad 530 1.1 riastrad /* Update entry type per pse and ips bit. */ 531 1.1 riastrad static void update_entry_type_for_real(struct intel_gvt_gtt_pte_ops *pte_ops, 532 1.1 riastrad struct intel_gvt_gtt_entry *entry, bool ips) 533 1.1 riastrad { 534 1.1 riastrad switch (entry->type) { 535 1.1 riastrad case GTT_TYPE_PPGTT_PDE_ENTRY: 536 1.1 riastrad case GTT_TYPE_PPGTT_PDP_ENTRY: 537 1.1 riastrad if (pte_ops->test_pse(entry)) 538 1.1 riastrad entry->type = get_pse_type(entry->type); 539 1.1 riastrad break; 540 1.1 riastrad case GTT_TYPE_PPGTT_PTE_4K_ENTRY: 541 1.1 riastrad if (ips) 542 1.1 riastrad entry->type = get_pse_type(entry->type); 543 1.1 riastrad break; 544 1.1 riastrad default: 545 1.1 riastrad GEM_BUG_ON(!gtt_type_is_entry(entry->type)); 546 1.1 riastrad } 547 1.1 riastrad 548 1.1 riastrad GEM_BUG_ON(entry->type == GTT_TYPE_INVALID); 549 1.1 riastrad } 550 1.1 riastrad 551 1.1 riastrad /* 552 1.1 riastrad * MM helpers. 553 1.1 riastrad */ 554 1.1 riastrad static void _ppgtt_get_root_entry(struct intel_vgpu_mm *mm, 555 1.1 riastrad struct intel_gvt_gtt_entry *entry, unsigned long index, 556 1.1 riastrad bool guest) 557 1.1 riastrad { 558 1.1 riastrad struct intel_gvt_gtt_pte_ops *pte_ops = mm->vgpu->gvt->gtt.pte_ops; 559 1.1 riastrad 560 1.1 riastrad GEM_BUG_ON(mm->type != INTEL_GVT_MM_PPGTT); 561 1.1 riastrad 562 1.1 riastrad entry->type = mm->ppgtt_mm.root_entry_type; 563 1.1 riastrad pte_ops->get_entry(guest ? mm->ppgtt_mm.guest_pdps : 564 1.1 riastrad mm->ppgtt_mm.shadow_pdps, 565 1.1 riastrad entry, index, false, 0, mm->vgpu); 566 1.1 riastrad update_entry_type_for_real(pte_ops, entry, false); 567 1.1 riastrad } 568 1.1 riastrad 569 1.1 riastrad static inline void ppgtt_get_guest_root_entry(struct intel_vgpu_mm *mm, 570 1.1 riastrad struct intel_gvt_gtt_entry *entry, unsigned long index) 571 1.1 riastrad { 572 1.1 riastrad _ppgtt_get_root_entry(mm, entry, index, true); 573 1.1 riastrad } 574 1.1 riastrad 575 1.1 riastrad static inline void ppgtt_get_shadow_root_entry(struct intel_vgpu_mm *mm, 576 1.1 riastrad struct intel_gvt_gtt_entry *entry, unsigned long index) 577 1.1 riastrad { 578 1.1 riastrad _ppgtt_get_root_entry(mm, entry, index, false); 579 1.1 riastrad } 580 1.1 riastrad 581 1.1 riastrad static void _ppgtt_set_root_entry(struct intel_vgpu_mm *mm, 582 1.1 riastrad struct intel_gvt_gtt_entry *entry, unsigned long index, 583 1.1 riastrad bool guest) 584 1.1 riastrad { 585 1.1 riastrad struct intel_gvt_gtt_pte_ops *pte_ops = mm->vgpu->gvt->gtt.pte_ops; 586 1.1 riastrad 587 1.1 riastrad pte_ops->set_entry(guest ? mm->ppgtt_mm.guest_pdps : 588 1.1 riastrad mm->ppgtt_mm.shadow_pdps, 589 1.1 riastrad entry, index, false, 0, mm->vgpu); 590 1.1 riastrad } 591 1.1 riastrad 592 1.1 riastrad static inline void ppgtt_set_guest_root_entry(struct intel_vgpu_mm *mm, 593 1.1 riastrad struct intel_gvt_gtt_entry *entry, unsigned long index) 594 1.1 riastrad { 595 1.1 riastrad _ppgtt_set_root_entry(mm, entry, index, true); 596 1.1 riastrad } 597 1.1 riastrad 598 1.1 riastrad static inline void ppgtt_set_shadow_root_entry(struct intel_vgpu_mm *mm, 599 1.1 riastrad struct intel_gvt_gtt_entry *entry, unsigned long index) 600 1.1 riastrad { 601 1.1 riastrad _ppgtt_set_root_entry(mm, entry, index, false); 602 1.1 riastrad } 603 1.1 riastrad 604 1.1 riastrad static void ggtt_get_guest_entry(struct intel_vgpu_mm *mm, 605 1.1 riastrad struct intel_gvt_gtt_entry *entry, unsigned long index) 606 1.1 riastrad { 607 1.1 riastrad struct intel_gvt_gtt_pte_ops *pte_ops = mm->vgpu->gvt->gtt.pte_ops; 608 1.1 riastrad 609 1.1 riastrad GEM_BUG_ON(mm->type != INTEL_GVT_MM_GGTT); 610 1.1 riastrad 611 1.1 riastrad entry->type = GTT_TYPE_GGTT_PTE; 612 1.1 riastrad pte_ops->get_entry(mm->ggtt_mm.virtual_ggtt, entry, index, 613 1.1 riastrad false, 0, mm->vgpu); 614 1.1 riastrad } 615 1.1 riastrad 616 1.1 riastrad static void ggtt_set_guest_entry(struct intel_vgpu_mm *mm, 617 1.1 riastrad struct intel_gvt_gtt_entry *entry, unsigned long index) 618 1.1 riastrad { 619 1.1 riastrad struct intel_gvt_gtt_pte_ops *pte_ops = mm->vgpu->gvt->gtt.pte_ops; 620 1.1 riastrad 621 1.1 riastrad GEM_BUG_ON(mm->type != INTEL_GVT_MM_GGTT); 622 1.1 riastrad 623 1.1 riastrad pte_ops->set_entry(mm->ggtt_mm.virtual_ggtt, entry, index, 624 1.1 riastrad false, 0, mm->vgpu); 625 1.1 riastrad } 626 1.1 riastrad 627 1.1 riastrad static void ggtt_get_host_entry(struct intel_vgpu_mm *mm, 628 1.1 riastrad struct intel_gvt_gtt_entry *entry, unsigned long index) 629 1.1 riastrad { 630 1.1 riastrad struct intel_gvt_gtt_pte_ops *pte_ops = mm->vgpu->gvt->gtt.pte_ops; 631 1.1 riastrad 632 1.1 riastrad GEM_BUG_ON(mm->type != INTEL_GVT_MM_GGTT); 633 1.1 riastrad 634 1.1 riastrad pte_ops->get_entry(NULL, entry, index, false, 0, mm->vgpu); 635 1.1 riastrad } 636 1.1 riastrad 637 1.1 riastrad static void ggtt_set_host_entry(struct intel_vgpu_mm *mm, 638 1.1 riastrad struct intel_gvt_gtt_entry *entry, unsigned long index) 639 1.1 riastrad { 640 1.1 riastrad struct intel_gvt_gtt_pte_ops *pte_ops = mm->vgpu->gvt->gtt.pte_ops; 641 1.1 riastrad 642 1.1 riastrad GEM_BUG_ON(mm->type != INTEL_GVT_MM_GGTT); 643 1.1 riastrad 644 1.1 riastrad pte_ops->set_entry(NULL, entry, index, false, 0, mm->vgpu); 645 1.1 riastrad } 646 1.1 riastrad 647 1.1 riastrad /* 648 1.1 riastrad * PPGTT shadow page table helpers. 649 1.1 riastrad */ 650 1.1 riastrad static inline int ppgtt_spt_get_entry( 651 1.1 riastrad struct intel_vgpu_ppgtt_spt *spt, 652 1.1 riastrad void *page_table, int type, 653 1.1 riastrad struct intel_gvt_gtt_entry *e, unsigned long index, 654 1.1 riastrad bool guest) 655 1.1 riastrad { 656 1.1 riastrad struct intel_gvt *gvt = spt->vgpu->gvt; 657 1.1 riastrad struct intel_gvt_gtt_pte_ops *ops = gvt->gtt.pte_ops; 658 1.1 riastrad int ret; 659 1.1 riastrad 660 1.1 riastrad e->type = get_entry_type(type); 661 1.1 riastrad 662 1.1 riastrad if (WARN(!gtt_type_is_entry(e->type), "invalid entry type\n")) 663 1.1 riastrad return -EINVAL; 664 1.1 riastrad 665 1.1 riastrad ret = ops->get_entry(page_table, e, index, guest, 666 1.1 riastrad spt->guest_page.gfn << I915_GTT_PAGE_SHIFT, 667 1.1 riastrad spt->vgpu); 668 1.1 riastrad if (ret) 669 1.1 riastrad return ret; 670 1.1 riastrad 671 1.1 riastrad update_entry_type_for_real(ops, e, guest ? 672 1.1 riastrad spt->guest_page.pde_ips : false); 673 1.1 riastrad 674 1.1 riastrad gvt_vdbg_mm("read ppgtt entry, spt type %d, entry type %d, index %lu, value %llx\n", 675 1.1 riastrad type, e->type, index, e->val64); 676 1.1 riastrad return 0; 677 1.1 riastrad } 678 1.1 riastrad 679 1.1 riastrad static inline int ppgtt_spt_set_entry( 680 1.1 riastrad struct intel_vgpu_ppgtt_spt *spt, 681 1.1 riastrad void *page_table, int type, 682 1.1 riastrad struct intel_gvt_gtt_entry *e, unsigned long index, 683 1.1 riastrad bool guest) 684 1.1 riastrad { 685 1.1 riastrad struct intel_gvt *gvt = spt->vgpu->gvt; 686 1.1 riastrad struct intel_gvt_gtt_pte_ops *ops = gvt->gtt.pte_ops; 687 1.1 riastrad 688 1.1 riastrad if (WARN(!gtt_type_is_entry(e->type), "invalid entry type\n")) 689 1.1 riastrad return -EINVAL; 690 1.1 riastrad 691 1.1 riastrad gvt_vdbg_mm("set ppgtt entry, spt type %d, entry type %d, index %lu, value %llx\n", 692 1.1 riastrad type, e->type, index, e->val64); 693 1.1 riastrad 694 1.1 riastrad return ops->set_entry(page_table, e, index, guest, 695 1.1 riastrad spt->guest_page.gfn << I915_GTT_PAGE_SHIFT, 696 1.1 riastrad spt->vgpu); 697 1.1 riastrad } 698 1.1 riastrad 699 1.1 riastrad #define ppgtt_get_guest_entry(spt, e, index) \ 700 1.1 riastrad ppgtt_spt_get_entry(spt, NULL, \ 701 1.1 riastrad spt->guest_page.type, e, index, true) 702 1.1 riastrad 703 1.1 riastrad #define ppgtt_set_guest_entry(spt, e, index) \ 704 1.1 riastrad ppgtt_spt_set_entry(spt, NULL, \ 705 1.1 riastrad spt->guest_page.type, e, index, true) 706 1.1 riastrad 707 1.1 riastrad #define ppgtt_get_shadow_entry(spt, e, index) \ 708 1.1 riastrad ppgtt_spt_get_entry(spt, spt->shadow_page.vaddr, \ 709 1.1 riastrad spt->shadow_page.type, e, index, false) 710 1.1 riastrad 711 1.1 riastrad #define ppgtt_set_shadow_entry(spt, e, index) \ 712 1.1 riastrad ppgtt_spt_set_entry(spt, spt->shadow_page.vaddr, \ 713 1.1 riastrad spt->shadow_page.type, e, index, false) 714 1.1 riastrad 715 1.1 riastrad static void *alloc_spt(gfp_t gfp_mask) 716 1.1 riastrad { 717 1.1 riastrad struct intel_vgpu_ppgtt_spt *spt; 718 1.1 riastrad 719 1.1 riastrad spt = kzalloc(sizeof(*spt), gfp_mask); 720 1.1 riastrad if (!spt) 721 1.1 riastrad return NULL; 722 1.1 riastrad 723 1.1 riastrad spt->shadow_page.page = alloc_page(gfp_mask); 724 1.1 riastrad if (!spt->shadow_page.page) { 725 1.1 riastrad kfree(spt); 726 1.1 riastrad return NULL; 727 1.1 riastrad } 728 1.1 riastrad return spt; 729 1.1 riastrad } 730 1.1 riastrad 731 1.1 riastrad static void free_spt(struct intel_vgpu_ppgtt_spt *spt) 732 1.1 riastrad { 733 1.1 riastrad __free_page(spt->shadow_page.page); 734 1.1 riastrad kfree(spt); 735 1.1 riastrad } 736 1.1 riastrad 737 1.1 riastrad static int detach_oos_page(struct intel_vgpu *vgpu, 738 1.1 riastrad struct intel_vgpu_oos_page *oos_page); 739 1.1 riastrad 740 1.1 riastrad static void ppgtt_free_spt(struct intel_vgpu_ppgtt_spt *spt) 741 1.1 riastrad { 742 1.1 riastrad struct device *kdev = &spt->vgpu->gvt->dev_priv->drm.pdev->dev; 743 1.1 riastrad 744 1.1 riastrad trace_spt_free(spt->vgpu->id, spt, spt->guest_page.type); 745 1.1 riastrad 746 1.1 riastrad dma_unmap_page(kdev, spt->shadow_page.mfn << I915_GTT_PAGE_SHIFT, 4096, 747 1.1 riastrad PCI_DMA_BIDIRECTIONAL); 748 1.1 riastrad 749 1.1 riastrad radix_tree_delete(&spt->vgpu->gtt.spt_tree, spt->shadow_page.mfn); 750 1.1 riastrad 751 1.1 riastrad if (spt->guest_page.gfn) { 752 1.1 riastrad if (spt->guest_page.oos_page) 753 1.1 riastrad detach_oos_page(spt->vgpu, spt->guest_page.oos_page); 754 1.1 riastrad 755 1.1 riastrad intel_vgpu_unregister_page_track(spt->vgpu, spt->guest_page.gfn); 756 1.1 riastrad } 757 1.1 riastrad 758 1.1 riastrad list_del_init(&spt->post_shadow_list); 759 1.1 riastrad free_spt(spt); 760 1.1 riastrad } 761 1.1 riastrad 762 1.1 riastrad static void ppgtt_free_all_spt(struct intel_vgpu *vgpu) 763 1.1 riastrad { 764 1.1 riastrad struct intel_vgpu_ppgtt_spt *spt, *spn; 765 1.1 riastrad struct radix_tree_iter iter; 766 1.1 riastrad LIST_HEAD(all_spt); 767 1.1 riastrad void __rcu **slot; 768 1.1 riastrad 769 1.1 riastrad rcu_read_lock(); 770 1.1 riastrad radix_tree_for_each_slot(slot, &vgpu->gtt.spt_tree, &iter, 0) { 771 1.1 riastrad spt = radix_tree_deref_slot(slot); 772 1.1 riastrad list_move(&spt->post_shadow_list, &all_spt); 773 1.1 riastrad } 774 1.1 riastrad rcu_read_unlock(); 775 1.1 riastrad 776 1.1 riastrad list_for_each_entry_safe(spt, spn, &all_spt, post_shadow_list) 777 1.1 riastrad ppgtt_free_spt(spt); 778 1.1 riastrad } 779 1.1 riastrad 780 1.1 riastrad static int ppgtt_handle_guest_write_page_table_bytes( 781 1.1 riastrad struct intel_vgpu_ppgtt_spt *spt, 782 1.1 riastrad u64 pa, void *p_data, int bytes); 783 1.1 riastrad 784 1.1 riastrad static int ppgtt_write_protection_handler( 785 1.1 riastrad struct intel_vgpu_page_track *page_track, 786 1.1 riastrad u64 gpa, void *data, int bytes) 787 1.1 riastrad { 788 1.1 riastrad struct intel_vgpu_ppgtt_spt *spt = page_track->priv_data; 789 1.1 riastrad 790 1.1 riastrad int ret; 791 1.1 riastrad 792 1.1 riastrad if (bytes != 4 && bytes != 8) 793 1.1 riastrad return -EINVAL; 794 1.1 riastrad 795 1.1 riastrad ret = ppgtt_handle_guest_write_page_table_bytes(spt, gpa, data, bytes); 796 1.1 riastrad if (ret) 797 1.1 riastrad return ret; 798 1.1 riastrad return ret; 799 1.1 riastrad } 800 1.1 riastrad 801 1.1 riastrad /* Find a spt by guest gfn. */ 802 1.1 riastrad static struct intel_vgpu_ppgtt_spt *intel_vgpu_find_spt_by_gfn( 803 1.1 riastrad struct intel_vgpu *vgpu, unsigned long gfn) 804 1.1 riastrad { 805 1.1 riastrad struct intel_vgpu_page_track *track; 806 1.1 riastrad 807 1.1 riastrad track = intel_vgpu_find_page_track(vgpu, gfn); 808 1.1 riastrad if (track && track->handler == ppgtt_write_protection_handler) 809 1.1 riastrad return track->priv_data; 810 1.1 riastrad 811 1.1 riastrad return NULL; 812 1.1 riastrad } 813 1.1 riastrad 814 1.1 riastrad /* Find the spt by shadow page mfn. */ 815 1.1 riastrad static inline struct intel_vgpu_ppgtt_spt *intel_vgpu_find_spt_by_mfn( 816 1.1 riastrad struct intel_vgpu *vgpu, unsigned long mfn) 817 1.1 riastrad { 818 1.1 riastrad return radix_tree_lookup(&vgpu->gtt.spt_tree, mfn); 819 1.1 riastrad } 820 1.1 riastrad 821 1.1 riastrad static int reclaim_one_ppgtt_mm(struct intel_gvt *gvt); 822 1.1 riastrad 823 1.1 riastrad /* Allocate shadow page table without guest page. */ 824 1.1 riastrad static struct intel_vgpu_ppgtt_spt *ppgtt_alloc_spt( 825 1.1 riastrad struct intel_vgpu *vgpu, enum intel_gvt_gtt_type type) 826 1.1 riastrad { 827 1.1 riastrad struct device *kdev = &vgpu->gvt->dev_priv->drm.pdev->dev; 828 1.1 riastrad struct intel_vgpu_ppgtt_spt *spt = NULL; 829 1.1 riastrad dma_addr_t daddr; 830 1.1 riastrad int ret; 831 1.1 riastrad 832 1.1 riastrad retry: 833 1.1 riastrad spt = alloc_spt(GFP_KERNEL | __GFP_ZERO); 834 1.1 riastrad if (!spt) { 835 1.1 riastrad if (reclaim_one_ppgtt_mm(vgpu->gvt)) 836 1.1 riastrad goto retry; 837 1.1 riastrad 838 1.1 riastrad gvt_vgpu_err("fail to allocate ppgtt shadow page\n"); 839 1.1 riastrad return ERR_PTR(-ENOMEM); 840 1.1 riastrad } 841 1.1 riastrad 842 1.1 riastrad spt->vgpu = vgpu; 843 1.1 riastrad atomic_set(&spt->refcount, 1); 844 1.1 riastrad INIT_LIST_HEAD(&spt->post_shadow_list); 845 1.1 riastrad 846 1.1 riastrad /* 847 1.1 riastrad * Init shadow_page. 848 1.1 riastrad */ 849 1.1 riastrad spt->shadow_page.type = type; 850 1.1 riastrad daddr = dma_map_page(kdev, spt->shadow_page.page, 851 1.1 riastrad 0, 4096, PCI_DMA_BIDIRECTIONAL); 852 1.1 riastrad if (dma_mapping_error(kdev, daddr)) { 853 1.1 riastrad gvt_vgpu_err("fail to map dma addr\n"); 854 1.1 riastrad ret = -EINVAL; 855 1.1 riastrad goto err_free_spt; 856 1.1 riastrad } 857 1.1 riastrad spt->shadow_page.vaddr = page_address(spt->shadow_page.page); 858 1.1 riastrad spt->shadow_page.mfn = daddr >> I915_GTT_PAGE_SHIFT; 859 1.1 riastrad 860 1.1 riastrad ret = radix_tree_insert(&vgpu->gtt.spt_tree, spt->shadow_page.mfn, spt); 861 1.1 riastrad if (ret) 862 1.1 riastrad goto err_unmap_dma; 863 1.1 riastrad 864 1.1 riastrad return spt; 865 1.1 riastrad 866 1.1 riastrad err_unmap_dma: 867 1.1 riastrad dma_unmap_page(kdev, daddr, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); 868 1.1 riastrad err_free_spt: 869 1.1 riastrad free_spt(spt); 870 1.1 riastrad return ERR_PTR(ret); 871 1.1 riastrad } 872 1.1 riastrad 873 1.1 riastrad /* Allocate shadow page table associated with specific gfn. */ 874 1.1 riastrad static struct intel_vgpu_ppgtt_spt *ppgtt_alloc_spt_gfn( 875 1.1 riastrad struct intel_vgpu *vgpu, enum intel_gvt_gtt_type type, 876 1.1 riastrad unsigned long gfn, bool guest_pde_ips) 877 1.1 riastrad { 878 1.1 riastrad struct intel_vgpu_ppgtt_spt *spt; 879 1.1 riastrad int ret; 880 1.1 riastrad 881 1.1 riastrad spt = ppgtt_alloc_spt(vgpu, type); 882 1.1 riastrad if (IS_ERR(spt)) 883 1.1 riastrad return spt; 884 1.1 riastrad 885 1.1 riastrad /* 886 1.1 riastrad * Init guest_page. 887 1.1 riastrad */ 888 1.1 riastrad ret = intel_vgpu_register_page_track(vgpu, gfn, 889 1.1 riastrad ppgtt_write_protection_handler, spt); 890 1.1 riastrad if (ret) { 891 1.1 riastrad ppgtt_free_spt(spt); 892 1.1 riastrad return ERR_PTR(ret); 893 1.1 riastrad } 894 1.1 riastrad 895 1.1 riastrad spt->guest_page.type = type; 896 1.1 riastrad spt->guest_page.gfn = gfn; 897 1.1 riastrad spt->guest_page.pde_ips = guest_pde_ips; 898 1.1 riastrad 899 1.1 riastrad trace_spt_alloc(vgpu->id, spt, type, spt->shadow_page.mfn, gfn); 900 1.1 riastrad 901 1.1 riastrad return spt; 902 1.1 riastrad } 903 1.1 riastrad 904 1.1 riastrad #define pt_entry_size_shift(spt) \ 905 1.1 riastrad ((spt)->vgpu->gvt->device_info.gtt_entry_size_shift) 906 1.1 riastrad 907 1.1 riastrad #define pt_entries(spt) \ 908 1.1 riastrad (I915_GTT_PAGE_SIZE >> pt_entry_size_shift(spt)) 909 1.1 riastrad 910 1.1 riastrad #define for_each_present_guest_entry(spt, e, i) \ 911 1.1 riastrad for (i = 0; i < pt_entries(spt); \ 912 1.1 riastrad i += spt->guest_page.pde_ips ? GTT_64K_PTE_STRIDE : 1) \ 913 1.1 riastrad if (!ppgtt_get_guest_entry(spt, e, i) && \ 914 1.1 riastrad spt->vgpu->gvt->gtt.pte_ops->test_present(e)) 915 1.1 riastrad 916 1.1 riastrad #define for_each_present_shadow_entry(spt, e, i) \ 917 1.1 riastrad for (i = 0; i < pt_entries(spt); \ 918 1.1 riastrad i += spt->shadow_page.pde_ips ? GTT_64K_PTE_STRIDE : 1) \ 919 1.1 riastrad if (!ppgtt_get_shadow_entry(spt, e, i) && \ 920 1.1 riastrad spt->vgpu->gvt->gtt.pte_ops->test_present(e)) 921 1.1 riastrad 922 1.1 riastrad #define for_each_shadow_entry(spt, e, i) \ 923 1.1 riastrad for (i = 0; i < pt_entries(spt); \ 924 1.1 riastrad i += (spt->shadow_page.pde_ips ? GTT_64K_PTE_STRIDE : 1)) \ 925 1.1 riastrad if (!ppgtt_get_shadow_entry(spt, e, i)) 926 1.1 riastrad 927 1.1 riastrad static inline void ppgtt_get_spt(struct intel_vgpu_ppgtt_spt *spt) 928 1.1 riastrad { 929 1.1 riastrad int v = atomic_read(&spt->refcount); 930 1.1 riastrad 931 1.1 riastrad trace_spt_refcount(spt->vgpu->id, "inc", spt, v, (v + 1)); 932 1.1 riastrad atomic_inc(&spt->refcount); 933 1.1 riastrad } 934 1.1 riastrad 935 1.1 riastrad static inline int ppgtt_put_spt(struct intel_vgpu_ppgtt_spt *spt) 936 1.1 riastrad { 937 1.1 riastrad int v = atomic_read(&spt->refcount); 938 1.1 riastrad 939 1.1 riastrad trace_spt_refcount(spt->vgpu->id, "dec", spt, v, (v - 1)); 940 1.1 riastrad return atomic_dec_return(&spt->refcount); 941 1.1 riastrad } 942 1.1 riastrad 943 1.1 riastrad static int ppgtt_invalidate_spt(struct intel_vgpu_ppgtt_spt *spt); 944 1.1 riastrad 945 1.1 riastrad static int ppgtt_invalidate_spt_by_shadow_entry(struct intel_vgpu *vgpu, 946 1.1 riastrad struct intel_gvt_gtt_entry *e) 947 1.1 riastrad { 948 1.1 riastrad struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops; 949 1.1 riastrad struct intel_vgpu_ppgtt_spt *s; 950 1.1 riastrad enum intel_gvt_gtt_type cur_pt_type; 951 1.1 riastrad 952 1.1 riastrad GEM_BUG_ON(!gtt_type_is_pt(get_next_pt_type(e->type))); 953 1.1 riastrad 954 1.1 riastrad if (e->type != GTT_TYPE_PPGTT_ROOT_L3_ENTRY 955 1.1 riastrad && e->type != GTT_TYPE_PPGTT_ROOT_L4_ENTRY) { 956 1.1 riastrad cur_pt_type = get_next_pt_type(e->type); 957 1.1 riastrad 958 1.1 riastrad if (!gtt_type_is_pt(cur_pt_type) || 959 1.1 riastrad !gtt_type_is_pt(cur_pt_type + 1)) { 960 1.1 riastrad WARN(1, "Invalid page table type, cur_pt_type is: %d\n", cur_pt_type); 961 1.1 riastrad return -EINVAL; 962 1.1 riastrad } 963 1.1 riastrad 964 1.1 riastrad cur_pt_type += 1; 965 1.1 riastrad 966 1.1 riastrad if (ops->get_pfn(e) == 967 1.1 riastrad vgpu->gtt.scratch_pt[cur_pt_type].page_mfn) 968 1.1 riastrad return 0; 969 1.1 riastrad } 970 1.1 riastrad s = intel_vgpu_find_spt_by_mfn(vgpu, ops->get_pfn(e)); 971 1.1 riastrad if (!s) { 972 1.1 riastrad gvt_vgpu_err("fail to find shadow page: mfn: 0x%lx\n", 973 1.1 riastrad ops->get_pfn(e)); 974 1.1 riastrad return -ENXIO; 975 1.1 riastrad } 976 1.1 riastrad return ppgtt_invalidate_spt(s); 977 1.1 riastrad } 978 1.1 riastrad 979 1.1 riastrad static inline void ppgtt_invalidate_pte(struct intel_vgpu_ppgtt_spt *spt, 980 1.1 riastrad struct intel_gvt_gtt_entry *entry) 981 1.1 riastrad { 982 1.1 riastrad struct intel_vgpu *vgpu = spt->vgpu; 983 1.1 riastrad struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops; 984 1.1 riastrad unsigned long pfn; 985 1.1 riastrad int type; 986 1.1 riastrad 987 1.1 riastrad pfn = ops->get_pfn(entry); 988 1.1 riastrad type = spt->shadow_page.type; 989 1.1 riastrad 990 1.1 riastrad /* Uninitialized spte or unshadowed spte. */ 991 1.1 riastrad if (!pfn || pfn == vgpu->gtt.scratch_pt[type].page_mfn) 992 1.1 riastrad return; 993 1.1 riastrad 994 1.1 riastrad intel_gvt_hypervisor_dma_unmap_guest_page(vgpu, pfn << PAGE_SHIFT); 995 1.1 riastrad } 996 1.1 riastrad 997 1.1 riastrad static int ppgtt_invalidate_spt(struct intel_vgpu_ppgtt_spt *spt) 998 1.1 riastrad { 999 1.1 riastrad struct intel_vgpu *vgpu = spt->vgpu; 1000 1.1 riastrad struct intel_gvt_gtt_entry e; 1001 1.1 riastrad unsigned long index; 1002 1.1 riastrad int ret; 1003 1.1 riastrad 1004 1.1 riastrad trace_spt_change(spt->vgpu->id, "die", spt, 1005 1.1 riastrad spt->guest_page.gfn, spt->shadow_page.type); 1006 1.1 riastrad 1007 1.1 riastrad if (ppgtt_put_spt(spt) > 0) 1008 1.1 riastrad return 0; 1009 1.1 riastrad 1010 1.1 riastrad for_each_present_shadow_entry(spt, &e, index) { 1011 1.1 riastrad switch (e.type) { 1012 1.1 riastrad case GTT_TYPE_PPGTT_PTE_4K_ENTRY: 1013 1.1 riastrad gvt_vdbg_mm("invalidate 4K entry\n"); 1014 1.1 riastrad ppgtt_invalidate_pte(spt, &e); 1015 1.1 riastrad break; 1016 1.1 riastrad case GTT_TYPE_PPGTT_PTE_64K_ENTRY: 1017 1.1 riastrad /* We don't setup 64K shadow entry so far. */ 1018 1.1 riastrad WARN(1, "suspicious 64K gtt entry\n"); 1019 1.1 riastrad continue; 1020 1.1 riastrad case GTT_TYPE_PPGTT_PTE_2M_ENTRY: 1021 1.1 riastrad gvt_vdbg_mm("invalidate 2M entry\n"); 1022 1.1 riastrad continue; 1023 1.1 riastrad case GTT_TYPE_PPGTT_PTE_1G_ENTRY: 1024 1.1 riastrad WARN(1, "GVT doesn't support 1GB page\n"); 1025 1.1 riastrad continue; 1026 1.1 riastrad case GTT_TYPE_PPGTT_PML4_ENTRY: 1027 1.1 riastrad case GTT_TYPE_PPGTT_PDP_ENTRY: 1028 1.1 riastrad case GTT_TYPE_PPGTT_PDE_ENTRY: 1029 1.1 riastrad gvt_vdbg_mm("invalidate PMUL4/PDP/PDE entry\n"); 1030 1.1 riastrad ret = ppgtt_invalidate_spt_by_shadow_entry( 1031 1.1 riastrad spt->vgpu, &e); 1032 1.1 riastrad if (ret) 1033 1.1 riastrad goto fail; 1034 1.1 riastrad break; 1035 1.1 riastrad default: 1036 1.1 riastrad GEM_BUG_ON(1); 1037 1.1 riastrad } 1038 1.1 riastrad } 1039 1.1 riastrad 1040 1.1 riastrad trace_spt_change(spt->vgpu->id, "release", spt, 1041 1.1 riastrad spt->guest_page.gfn, spt->shadow_page.type); 1042 1.1 riastrad ppgtt_free_spt(spt); 1043 1.1 riastrad return 0; 1044 1.1 riastrad fail: 1045 1.1 riastrad gvt_vgpu_err("fail: shadow page %p shadow entry 0x%llx type %d\n", 1046 1.1 riastrad spt, e.val64, e.type); 1047 1.1 riastrad return ret; 1048 1.1 riastrad } 1049 1.1 riastrad 1050 1.1 riastrad static bool vgpu_ips_enabled(struct intel_vgpu *vgpu) 1051 1.1 riastrad { 1052 1.1 riastrad struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv; 1053 1.1 riastrad 1054 1.1 riastrad if (INTEL_GEN(dev_priv) == 9 || INTEL_GEN(dev_priv) == 10) { 1055 1.1 riastrad u32 ips = vgpu_vreg_t(vgpu, GEN8_GAMW_ECO_DEV_RW_IA) & 1056 1.1 riastrad GAMW_ECO_ENABLE_64K_IPS_FIELD; 1057 1.1 riastrad 1058 1.1 riastrad return ips == GAMW_ECO_ENABLE_64K_IPS_FIELD; 1059 1.1 riastrad } else if (INTEL_GEN(dev_priv) >= 11) { 1060 1.1 riastrad /* 64K paging only controlled by IPS bit in PTE now. */ 1061 1.1 riastrad return true; 1062 1.1 riastrad } else 1063 1.1 riastrad return false; 1064 1.1 riastrad } 1065 1.1 riastrad 1066 1.1 riastrad static int ppgtt_populate_spt(struct intel_vgpu_ppgtt_spt *spt); 1067 1.1 riastrad 1068 1.1 riastrad static struct intel_vgpu_ppgtt_spt *ppgtt_populate_spt_by_guest_entry( 1069 1.1 riastrad struct intel_vgpu *vgpu, struct intel_gvt_gtt_entry *we) 1070 1.1 riastrad { 1071 1.1 riastrad struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops; 1072 1.1 riastrad struct intel_vgpu_ppgtt_spt *spt = NULL; 1073 1.1 riastrad bool ips = false; 1074 1.1 riastrad int ret; 1075 1.1 riastrad 1076 1.1 riastrad GEM_BUG_ON(!gtt_type_is_pt(get_next_pt_type(we->type))); 1077 1.1 riastrad 1078 1.1 riastrad if (we->type == GTT_TYPE_PPGTT_PDE_ENTRY) 1079 1.1 riastrad ips = vgpu_ips_enabled(vgpu) && ops->test_ips(we); 1080 1.1 riastrad 1081 1.1 riastrad spt = intel_vgpu_find_spt_by_gfn(vgpu, ops->get_pfn(we)); 1082 1.1 riastrad if (spt) { 1083 1.1 riastrad ppgtt_get_spt(spt); 1084 1.1 riastrad 1085 1.1 riastrad if (ips != spt->guest_page.pde_ips) { 1086 1.1 riastrad spt->guest_page.pde_ips = ips; 1087 1.1 riastrad 1088 1.1 riastrad gvt_dbg_mm("reshadow PDE since ips changed\n"); 1089 1.1 riastrad clear_page(spt->shadow_page.vaddr); 1090 1.1 riastrad ret = ppgtt_populate_spt(spt); 1091 1.1 riastrad if (ret) { 1092 1.1 riastrad ppgtt_put_spt(spt); 1093 1.1 riastrad goto err; 1094 1.1 riastrad } 1095 1.1 riastrad } 1096 1.1 riastrad } else { 1097 1.1 riastrad int type = get_next_pt_type(we->type); 1098 1.1 riastrad 1099 1.1 riastrad if (!gtt_type_is_pt(type)) { 1100 1.1 riastrad ret = -EINVAL; 1101 1.1 riastrad goto err; 1102 1.1 riastrad } 1103 1.1 riastrad 1104 1.1 riastrad spt = ppgtt_alloc_spt_gfn(vgpu, type, ops->get_pfn(we), ips); 1105 1.1 riastrad if (IS_ERR(spt)) { 1106 1.1 riastrad ret = PTR_ERR(spt); 1107 1.1 riastrad goto err; 1108 1.1 riastrad } 1109 1.1 riastrad 1110 1.1 riastrad ret = intel_vgpu_enable_page_track(vgpu, spt->guest_page.gfn); 1111 1.1 riastrad if (ret) 1112 1.1 riastrad goto err_free_spt; 1113 1.1 riastrad 1114 1.1 riastrad ret = ppgtt_populate_spt(spt); 1115 1.1 riastrad if (ret) 1116 1.1 riastrad goto err_free_spt; 1117 1.1 riastrad 1118 1.1 riastrad trace_spt_change(vgpu->id, "new", spt, spt->guest_page.gfn, 1119 1.1 riastrad spt->shadow_page.type); 1120 1.1 riastrad } 1121 1.1 riastrad return spt; 1122 1.1 riastrad 1123 1.1 riastrad err_free_spt: 1124 1.1 riastrad ppgtt_free_spt(spt); 1125 1.1 riastrad spt = NULL; 1126 1.1 riastrad err: 1127 1.1 riastrad gvt_vgpu_err("fail: shadow page %p guest entry 0x%llx type %d\n", 1128 1.1 riastrad spt, we->val64, we->type); 1129 1.1 riastrad return ERR_PTR(ret); 1130 1.1 riastrad } 1131 1.1 riastrad 1132 1.1 riastrad static inline void ppgtt_generate_shadow_entry(struct intel_gvt_gtt_entry *se, 1133 1.1 riastrad struct intel_vgpu_ppgtt_spt *s, struct intel_gvt_gtt_entry *ge) 1134 1.1 riastrad { 1135 1.1 riastrad struct intel_gvt_gtt_pte_ops *ops = s->vgpu->gvt->gtt.pte_ops; 1136 1.1 riastrad 1137 1.1 riastrad se->type = ge->type; 1138 1.1 riastrad se->val64 = ge->val64; 1139 1.1 riastrad 1140 1.1 riastrad /* Because we always split 64KB pages, so clear IPS in shadow PDE. */ 1141 1.1 riastrad if (se->type == GTT_TYPE_PPGTT_PDE_ENTRY) 1142 1.1 riastrad ops->clear_ips(se); 1143 1.1 riastrad 1144 1.1 riastrad ops->set_pfn(se, s->shadow_page.mfn); 1145 1.1 riastrad } 1146 1.1 riastrad 1147 1.1 riastrad /** 1148 1.1 riastrad * Check if can do 2M page 1149 1.1 riastrad * @vgpu: target vgpu 1150 1.1 riastrad * @entry: target pfn's gtt entry 1151 1.1 riastrad * 1152 1.1 riastrad * Return 1 if 2MB huge gtt shadowing is possilbe, 0 if miscondition, 1153 1.1 riastrad * negtive if found err. 1154 1.1 riastrad */ 1155 1.1 riastrad static int is_2MB_gtt_possible(struct intel_vgpu *vgpu, 1156 1.1 riastrad struct intel_gvt_gtt_entry *entry) 1157 1.1 riastrad { 1158 1.1 riastrad struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops; 1159 1.1 riastrad unsigned long pfn; 1160 1.1 riastrad 1161 1.1 riastrad if (!HAS_PAGE_SIZES(vgpu->gvt->dev_priv, I915_GTT_PAGE_SIZE_2M)) 1162 1.1 riastrad return 0; 1163 1.1 riastrad 1164 1.1 riastrad pfn = intel_gvt_hypervisor_gfn_to_mfn(vgpu, ops->get_pfn(entry)); 1165 1.1 riastrad if (pfn == INTEL_GVT_INVALID_ADDR) 1166 1.1 riastrad return -EINVAL; 1167 1.1 riastrad 1168 1.1 riastrad return PageTransHuge(pfn_to_page(pfn)); 1169 1.1 riastrad } 1170 1.1 riastrad 1171 1.1 riastrad static int split_2MB_gtt_entry(struct intel_vgpu *vgpu, 1172 1.1 riastrad struct intel_vgpu_ppgtt_spt *spt, unsigned long index, 1173 1.1 riastrad struct intel_gvt_gtt_entry *se) 1174 1.1 riastrad { 1175 1.1 riastrad struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops; 1176 1.1 riastrad struct intel_vgpu_ppgtt_spt *sub_spt; 1177 1.1 riastrad struct intel_gvt_gtt_entry sub_se; 1178 1.1 riastrad unsigned long start_gfn; 1179 1.1 riastrad dma_addr_t dma_addr; 1180 1.1 riastrad unsigned long sub_index; 1181 1.1 riastrad int ret; 1182 1.1 riastrad 1183 1.1 riastrad gvt_dbg_mm("Split 2M gtt entry, index %lu\n", index); 1184 1.1 riastrad 1185 1.1 riastrad start_gfn = ops->get_pfn(se); 1186 1.1 riastrad 1187 1.1 riastrad sub_spt = ppgtt_alloc_spt(vgpu, GTT_TYPE_PPGTT_PTE_PT); 1188 1.1 riastrad if (IS_ERR(sub_spt)) 1189 1.1 riastrad return PTR_ERR(sub_spt); 1190 1.1 riastrad 1191 1.1 riastrad for_each_shadow_entry(sub_spt, &sub_se, sub_index) { 1192 1.1 riastrad ret = intel_gvt_hypervisor_dma_map_guest_page(vgpu, 1193 1.1 riastrad start_gfn + sub_index, PAGE_SIZE, &dma_addr); 1194 1.1 riastrad if (ret) { 1195 1.1 riastrad ppgtt_invalidate_spt(spt); 1196 1.1 riastrad return ret; 1197 1.1 riastrad } 1198 1.1 riastrad sub_se.val64 = se->val64; 1199 1.1 riastrad 1200 1.1 riastrad /* Copy the PAT field from PDE. */ 1201 1.1 riastrad sub_se.val64 &= ~_PAGE_PAT; 1202 1.1 riastrad sub_se.val64 |= (se->val64 & _PAGE_PAT_LARGE) >> 5; 1203 1.1 riastrad 1204 1.1 riastrad ops->set_pfn(&sub_se, dma_addr >> PAGE_SHIFT); 1205 1.1 riastrad ppgtt_set_shadow_entry(sub_spt, &sub_se, sub_index); 1206 1.1 riastrad } 1207 1.1 riastrad 1208 1.1 riastrad /* Clear dirty field. */ 1209 1.1 riastrad se->val64 &= ~_PAGE_DIRTY; 1210 1.1 riastrad 1211 1.1 riastrad ops->clear_pse(se); 1212 1.1 riastrad ops->clear_ips(se); 1213 1.1 riastrad ops->set_pfn(se, sub_spt->shadow_page.mfn); 1214 1.1 riastrad ppgtt_set_shadow_entry(spt, se, index); 1215 1.1 riastrad return 0; 1216 1.1 riastrad } 1217 1.1 riastrad 1218 1.1 riastrad static int split_64KB_gtt_entry(struct intel_vgpu *vgpu, 1219 1.1 riastrad struct intel_vgpu_ppgtt_spt *spt, unsigned long index, 1220 1.1 riastrad struct intel_gvt_gtt_entry *se) 1221 1.1 riastrad { 1222 1.1 riastrad struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops; 1223 1.1 riastrad struct intel_gvt_gtt_entry entry = *se; 1224 1.1 riastrad unsigned long start_gfn; 1225 1.1 riastrad dma_addr_t dma_addr; 1226 1.1 riastrad int i, ret; 1227 1.1 riastrad 1228 1.1 riastrad gvt_vdbg_mm("Split 64K gtt entry, index %lu\n", index); 1229 1.1 riastrad 1230 1.1 riastrad GEM_BUG_ON(index % GTT_64K_PTE_STRIDE); 1231 1.1 riastrad 1232 1.1 riastrad start_gfn = ops->get_pfn(se); 1233 1.1 riastrad 1234 1.1 riastrad entry.type = GTT_TYPE_PPGTT_PTE_4K_ENTRY; 1235 1.1 riastrad ops->set_64k_splited(&entry); 1236 1.1 riastrad 1237 1.1 riastrad for (i = 0; i < GTT_64K_PTE_STRIDE; i++) { 1238 1.1 riastrad ret = intel_gvt_hypervisor_dma_map_guest_page(vgpu, 1239 1.1 riastrad start_gfn + i, PAGE_SIZE, &dma_addr); 1240 1.1 riastrad if (ret) 1241 1.1 riastrad return ret; 1242 1.1 riastrad 1243 1.1 riastrad ops->set_pfn(&entry, dma_addr >> PAGE_SHIFT); 1244 1.1 riastrad ppgtt_set_shadow_entry(spt, &entry, index + i); 1245 1.1 riastrad } 1246 1.1 riastrad return 0; 1247 1.1 riastrad } 1248 1.1 riastrad 1249 1.1 riastrad static int ppgtt_populate_shadow_entry(struct intel_vgpu *vgpu, 1250 1.1 riastrad struct intel_vgpu_ppgtt_spt *spt, unsigned long index, 1251 1.1 riastrad struct intel_gvt_gtt_entry *ge) 1252 1.1 riastrad { 1253 1.1 riastrad struct intel_gvt_gtt_pte_ops *pte_ops = vgpu->gvt->gtt.pte_ops; 1254 1.1 riastrad struct intel_gvt_gtt_entry se = *ge; 1255 1.1 riastrad unsigned long gfn, page_size = PAGE_SIZE; 1256 1.1 riastrad dma_addr_t dma_addr; 1257 1.1 riastrad int ret; 1258 1.1 riastrad 1259 1.1 riastrad if (!pte_ops->test_present(ge)) 1260 1.1 riastrad return 0; 1261 1.1 riastrad 1262 1.1 riastrad gfn = pte_ops->get_pfn(ge); 1263 1.1 riastrad 1264 1.1 riastrad switch (ge->type) { 1265 1.1 riastrad case GTT_TYPE_PPGTT_PTE_4K_ENTRY: 1266 1.1 riastrad gvt_vdbg_mm("shadow 4K gtt entry\n"); 1267 1.1 riastrad break; 1268 1.1 riastrad case GTT_TYPE_PPGTT_PTE_64K_ENTRY: 1269 1.1 riastrad gvt_vdbg_mm("shadow 64K gtt entry\n"); 1270 1.1 riastrad /* 1271 1.1 riastrad * The layout of 64K page is special, the page size is 1272 1.1 riastrad * controlled by uper PDE. To be simple, we always split 1273 1.1 riastrad * 64K page to smaller 4K pages in shadow PT. 1274 1.1 riastrad */ 1275 1.1 riastrad return split_64KB_gtt_entry(vgpu, spt, index, &se); 1276 1.1 riastrad case GTT_TYPE_PPGTT_PTE_2M_ENTRY: 1277 1.1 riastrad gvt_vdbg_mm("shadow 2M gtt entry\n"); 1278 1.1 riastrad ret = is_2MB_gtt_possible(vgpu, ge); 1279 1.1 riastrad if (ret == 0) 1280 1.1 riastrad return split_2MB_gtt_entry(vgpu, spt, index, &se); 1281 1.1 riastrad else if (ret < 0) 1282 1.1 riastrad return ret; 1283 1.1 riastrad page_size = I915_GTT_PAGE_SIZE_2M; 1284 1.1 riastrad break; 1285 1.1 riastrad case GTT_TYPE_PPGTT_PTE_1G_ENTRY: 1286 1.1 riastrad gvt_vgpu_err("GVT doesn't support 1GB entry\n"); 1287 1.1 riastrad return -EINVAL; 1288 1.1 riastrad default: 1289 1.1 riastrad GEM_BUG_ON(1); 1290 1.1 riastrad } 1291 1.1 riastrad 1292 1.1 riastrad /* direct shadow */ 1293 1.1 riastrad ret = intel_gvt_hypervisor_dma_map_guest_page(vgpu, gfn, page_size, 1294 1.1 riastrad &dma_addr); 1295 1.1 riastrad if (ret) 1296 1.1 riastrad return -ENXIO; 1297 1.1 riastrad 1298 1.1 riastrad pte_ops->set_pfn(&se, dma_addr >> PAGE_SHIFT); 1299 1.1 riastrad ppgtt_set_shadow_entry(spt, &se, index); 1300 1.1 riastrad return 0; 1301 1.1 riastrad } 1302 1.1 riastrad 1303 1.1 riastrad static int ppgtt_populate_spt(struct intel_vgpu_ppgtt_spt *spt) 1304 1.1 riastrad { 1305 1.1 riastrad struct intel_vgpu *vgpu = spt->vgpu; 1306 1.1 riastrad struct intel_gvt *gvt = vgpu->gvt; 1307 1.1 riastrad struct intel_gvt_gtt_pte_ops *ops = gvt->gtt.pte_ops; 1308 1.1 riastrad struct intel_vgpu_ppgtt_spt *s; 1309 1.1 riastrad struct intel_gvt_gtt_entry se, ge; 1310 1.1 riastrad unsigned long gfn, i; 1311 1.1 riastrad int ret; 1312 1.1 riastrad 1313 1.1 riastrad trace_spt_change(spt->vgpu->id, "born", spt, 1314 1.1 riastrad spt->guest_page.gfn, spt->shadow_page.type); 1315 1.1 riastrad 1316 1.1 riastrad for_each_present_guest_entry(spt, &ge, i) { 1317 1.1 riastrad if (gtt_type_is_pt(get_next_pt_type(ge.type))) { 1318 1.1 riastrad s = ppgtt_populate_spt_by_guest_entry(vgpu, &ge); 1319 1.1 riastrad if (IS_ERR(s)) { 1320 1.1 riastrad ret = PTR_ERR(s); 1321 1.1 riastrad goto fail; 1322 1.1 riastrad } 1323 1.1 riastrad ppgtt_get_shadow_entry(spt, &se, i); 1324 1.1 riastrad ppgtt_generate_shadow_entry(&se, s, &ge); 1325 1.1 riastrad ppgtt_set_shadow_entry(spt, &se, i); 1326 1.1 riastrad } else { 1327 1.1 riastrad gfn = ops->get_pfn(&ge); 1328 1.1 riastrad if (!intel_gvt_hypervisor_is_valid_gfn(vgpu, gfn)) { 1329 1.1 riastrad ops->set_pfn(&se, gvt->gtt.scratch_mfn); 1330 1.1 riastrad ppgtt_set_shadow_entry(spt, &se, i); 1331 1.1 riastrad continue; 1332 1.1 riastrad } 1333 1.1 riastrad 1334 1.1 riastrad ret = ppgtt_populate_shadow_entry(vgpu, spt, i, &ge); 1335 1.1 riastrad if (ret) 1336 1.1 riastrad goto fail; 1337 1.1 riastrad } 1338 1.1 riastrad } 1339 1.1 riastrad return 0; 1340 1.1 riastrad fail: 1341 1.1 riastrad gvt_vgpu_err("fail: shadow page %p guest entry 0x%llx type %d\n", 1342 1.1 riastrad spt, ge.val64, ge.type); 1343 1.1 riastrad return ret; 1344 1.1 riastrad } 1345 1.1 riastrad 1346 1.1 riastrad static int ppgtt_handle_guest_entry_removal(struct intel_vgpu_ppgtt_spt *spt, 1347 1.1 riastrad struct intel_gvt_gtt_entry *se, unsigned long index) 1348 1.1 riastrad { 1349 1.1 riastrad struct intel_vgpu *vgpu = spt->vgpu; 1350 1.1 riastrad struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops; 1351 1.1 riastrad int ret; 1352 1.1 riastrad 1353 1.1 riastrad trace_spt_guest_change(spt->vgpu->id, "remove", spt, 1354 1.1 riastrad spt->shadow_page.type, se->val64, index); 1355 1.1 riastrad 1356 1.1 riastrad gvt_vdbg_mm("destroy old shadow entry, type %d, index %lu, value %llx\n", 1357 1.1 riastrad se->type, index, se->val64); 1358 1.1 riastrad 1359 1.1 riastrad if (!ops->test_present(se)) 1360 1.1 riastrad return 0; 1361 1.1 riastrad 1362 1.1 riastrad if (ops->get_pfn(se) == 1363 1.1 riastrad vgpu->gtt.scratch_pt[spt->shadow_page.type].page_mfn) 1364 1.1 riastrad return 0; 1365 1.1 riastrad 1366 1.1 riastrad if (gtt_type_is_pt(get_next_pt_type(se->type))) { 1367 1.1 riastrad struct intel_vgpu_ppgtt_spt *s = 1368 1.1 riastrad intel_vgpu_find_spt_by_mfn(vgpu, ops->get_pfn(se)); 1369 1.1 riastrad if (!s) { 1370 1.1 riastrad gvt_vgpu_err("fail to find guest page\n"); 1371 1.1 riastrad ret = -ENXIO; 1372 1.1 riastrad goto fail; 1373 1.1 riastrad } 1374 1.1 riastrad ret = ppgtt_invalidate_spt(s); 1375 1.1 riastrad if (ret) 1376 1.1 riastrad goto fail; 1377 1.1 riastrad } else { 1378 1.1 riastrad /* We don't setup 64K shadow entry so far. */ 1379 1.1 riastrad WARN(se->type == GTT_TYPE_PPGTT_PTE_64K_ENTRY, 1380 1.1 riastrad "suspicious 64K entry\n"); 1381 1.1 riastrad ppgtt_invalidate_pte(spt, se); 1382 1.1 riastrad } 1383 1.1 riastrad 1384 1.1 riastrad return 0; 1385 1.1 riastrad fail: 1386 1.1 riastrad gvt_vgpu_err("fail: shadow page %p guest entry 0x%llx type %d\n", 1387 1.1 riastrad spt, se->val64, se->type); 1388 1.1 riastrad return ret; 1389 1.1 riastrad } 1390 1.1 riastrad 1391 1.1 riastrad static int ppgtt_handle_guest_entry_add(struct intel_vgpu_ppgtt_spt *spt, 1392 1.1 riastrad struct intel_gvt_gtt_entry *we, unsigned long index) 1393 1.1 riastrad { 1394 1.1 riastrad struct intel_vgpu *vgpu = spt->vgpu; 1395 1.1 riastrad struct intel_gvt_gtt_entry m; 1396 1.1 riastrad struct intel_vgpu_ppgtt_spt *s; 1397 1.1 riastrad int ret; 1398 1.1 riastrad 1399 1.1 riastrad trace_spt_guest_change(spt->vgpu->id, "add", spt, spt->shadow_page.type, 1400 1.1 riastrad we->val64, index); 1401 1.1 riastrad 1402 1.1 riastrad gvt_vdbg_mm("add shadow entry: type %d, index %lu, value %llx\n", 1403 1.1 riastrad we->type, index, we->val64); 1404 1.1 riastrad 1405 1.1 riastrad if (gtt_type_is_pt(get_next_pt_type(we->type))) { 1406 1.1 riastrad s = ppgtt_populate_spt_by_guest_entry(vgpu, we); 1407 1.1 riastrad if (IS_ERR(s)) { 1408 1.1 riastrad ret = PTR_ERR(s); 1409 1.1 riastrad goto fail; 1410 1.1 riastrad } 1411 1.1 riastrad ppgtt_get_shadow_entry(spt, &m, index); 1412 1.1 riastrad ppgtt_generate_shadow_entry(&m, s, we); 1413 1.1 riastrad ppgtt_set_shadow_entry(spt, &m, index); 1414 1.1 riastrad } else { 1415 1.1 riastrad ret = ppgtt_populate_shadow_entry(vgpu, spt, index, we); 1416 1.1 riastrad if (ret) 1417 1.1 riastrad goto fail; 1418 1.1 riastrad } 1419 1.1 riastrad return 0; 1420 1.1 riastrad fail: 1421 1.1 riastrad gvt_vgpu_err("fail: spt %p guest entry 0x%llx type %d\n", 1422 1.1 riastrad spt, we->val64, we->type); 1423 1.1 riastrad return ret; 1424 1.1 riastrad } 1425 1.1 riastrad 1426 1.1 riastrad static int sync_oos_page(struct intel_vgpu *vgpu, 1427 1.1 riastrad struct intel_vgpu_oos_page *oos_page) 1428 1.1 riastrad { 1429 1.1 riastrad const struct intel_gvt_device_info *info = &vgpu->gvt->device_info; 1430 1.1 riastrad struct intel_gvt *gvt = vgpu->gvt; 1431 1.1 riastrad struct intel_gvt_gtt_pte_ops *ops = gvt->gtt.pte_ops; 1432 1.1 riastrad struct intel_vgpu_ppgtt_spt *spt = oos_page->spt; 1433 1.1 riastrad struct intel_gvt_gtt_entry old, new; 1434 1.1 riastrad int index; 1435 1.1 riastrad int ret; 1436 1.1 riastrad 1437 1.1 riastrad trace_oos_change(vgpu->id, "sync", oos_page->id, 1438 1.1 riastrad spt, spt->guest_page.type); 1439 1.1 riastrad 1440 1.1 riastrad old.type = new.type = get_entry_type(spt->guest_page.type); 1441 1.1 riastrad old.val64 = new.val64 = 0; 1442 1.1 riastrad 1443 1.1 riastrad for (index = 0; index < (I915_GTT_PAGE_SIZE >> 1444 1.1 riastrad info->gtt_entry_size_shift); index++) { 1445 1.1 riastrad ops->get_entry(oos_page->mem, &old, index, false, 0, vgpu); 1446 1.1 riastrad ops->get_entry(NULL, &new, index, true, 1447 1.1 riastrad spt->guest_page.gfn << PAGE_SHIFT, vgpu); 1448 1.1 riastrad 1449 1.1 riastrad if (old.val64 == new.val64 1450 1.1 riastrad && !test_and_clear_bit(index, spt->post_shadow_bitmap)) 1451 1.1 riastrad continue; 1452 1.1 riastrad 1453 1.1 riastrad trace_oos_sync(vgpu->id, oos_page->id, 1454 1.1 riastrad spt, spt->guest_page.type, 1455 1.1 riastrad new.val64, index); 1456 1.1 riastrad 1457 1.1 riastrad ret = ppgtt_populate_shadow_entry(vgpu, spt, index, &new); 1458 1.1 riastrad if (ret) 1459 1.1 riastrad return ret; 1460 1.1 riastrad 1461 1.1 riastrad ops->set_entry(oos_page->mem, &new, index, false, 0, vgpu); 1462 1.1 riastrad } 1463 1.1 riastrad 1464 1.1 riastrad spt->guest_page.write_cnt = 0; 1465 1.1 riastrad list_del_init(&spt->post_shadow_list); 1466 1.1 riastrad return 0; 1467 1.1 riastrad } 1468 1.1 riastrad 1469 1.1 riastrad static int detach_oos_page(struct intel_vgpu *vgpu, 1470 1.1 riastrad struct intel_vgpu_oos_page *oos_page) 1471 1.1 riastrad { 1472 1.1 riastrad struct intel_gvt *gvt = vgpu->gvt; 1473 1.1 riastrad struct intel_vgpu_ppgtt_spt *spt = oos_page->spt; 1474 1.1 riastrad 1475 1.1 riastrad trace_oos_change(vgpu->id, "detach", oos_page->id, 1476 1.1 riastrad spt, spt->guest_page.type); 1477 1.1 riastrad 1478 1.1 riastrad spt->guest_page.write_cnt = 0; 1479 1.1 riastrad spt->guest_page.oos_page = NULL; 1480 1.1 riastrad oos_page->spt = NULL; 1481 1.1 riastrad 1482 1.1 riastrad list_del_init(&oos_page->vm_list); 1483 1.1 riastrad list_move_tail(&oos_page->list, &gvt->gtt.oos_page_free_list_head); 1484 1.1 riastrad 1485 1.1 riastrad return 0; 1486 1.1 riastrad } 1487 1.1 riastrad 1488 1.1 riastrad static int attach_oos_page(struct intel_vgpu_oos_page *oos_page, 1489 1.1 riastrad struct intel_vgpu_ppgtt_spt *spt) 1490 1.1 riastrad { 1491 1.1 riastrad struct intel_gvt *gvt = spt->vgpu->gvt; 1492 1.1 riastrad int ret; 1493 1.1 riastrad 1494 1.1 riastrad ret = intel_gvt_hypervisor_read_gpa(spt->vgpu, 1495 1.1 riastrad spt->guest_page.gfn << I915_GTT_PAGE_SHIFT, 1496 1.1 riastrad oos_page->mem, I915_GTT_PAGE_SIZE); 1497 1.1 riastrad if (ret) 1498 1.1 riastrad return ret; 1499 1.1 riastrad 1500 1.1 riastrad oos_page->spt = spt; 1501 1.1 riastrad spt->guest_page.oos_page = oos_page; 1502 1.1 riastrad 1503 1.1 riastrad list_move_tail(&oos_page->list, &gvt->gtt.oos_page_use_list_head); 1504 1.1 riastrad 1505 1.1 riastrad trace_oos_change(spt->vgpu->id, "attach", oos_page->id, 1506 1.1 riastrad spt, spt->guest_page.type); 1507 1.1 riastrad return 0; 1508 1.1 riastrad } 1509 1.1 riastrad 1510 1.1 riastrad static int ppgtt_set_guest_page_sync(struct intel_vgpu_ppgtt_spt *spt) 1511 1.1 riastrad { 1512 1.1 riastrad struct intel_vgpu_oos_page *oos_page = spt->guest_page.oos_page; 1513 1.1 riastrad int ret; 1514 1.1 riastrad 1515 1.1 riastrad ret = intel_vgpu_enable_page_track(spt->vgpu, spt->guest_page.gfn); 1516 1.1 riastrad if (ret) 1517 1.1 riastrad return ret; 1518 1.1 riastrad 1519 1.1 riastrad trace_oos_change(spt->vgpu->id, "set page sync", oos_page->id, 1520 1.1 riastrad spt, spt->guest_page.type); 1521 1.1 riastrad 1522 1.1 riastrad list_del_init(&oos_page->vm_list); 1523 1.1 riastrad return sync_oos_page(spt->vgpu, oos_page); 1524 1.1 riastrad } 1525 1.1 riastrad 1526 1.1 riastrad static int ppgtt_allocate_oos_page(struct intel_vgpu_ppgtt_spt *spt) 1527 1.1 riastrad { 1528 1.1 riastrad struct intel_gvt *gvt = spt->vgpu->gvt; 1529 1.1 riastrad struct intel_gvt_gtt *gtt = &gvt->gtt; 1530 1.1 riastrad struct intel_vgpu_oos_page *oos_page = spt->guest_page.oos_page; 1531 1.1 riastrad int ret; 1532 1.1 riastrad 1533 1.1 riastrad WARN(oos_page, "shadow PPGTT page has already has a oos page\n"); 1534 1.1 riastrad 1535 1.1 riastrad if (list_empty(>t->oos_page_free_list_head)) { 1536 1.1 riastrad oos_page = container_of(gtt->oos_page_use_list_head.next, 1537 1.1 riastrad struct intel_vgpu_oos_page, list); 1538 1.1 riastrad ret = ppgtt_set_guest_page_sync(oos_page->spt); 1539 1.1 riastrad if (ret) 1540 1.1 riastrad return ret; 1541 1.1 riastrad ret = detach_oos_page(spt->vgpu, oos_page); 1542 1.1 riastrad if (ret) 1543 1.1 riastrad return ret; 1544 1.1 riastrad } else 1545 1.1 riastrad oos_page = container_of(gtt->oos_page_free_list_head.next, 1546 1.1 riastrad struct intel_vgpu_oos_page, list); 1547 1.1 riastrad return attach_oos_page(oos_page, spt); 1548 1.1 riastrad } 1549 1.1 riastrad 1550 1.1 riastrad static int ppgtt_set_guest_page_oos(struct intel_vgpu_ppgtt_spt *spt) 1551 1.1 riastrad { 1552 1.1 riastrad struct intel_vgpu_oos_page *oos_page = spt->guest_page.oos_page; 1553 1.1 riastrad 1554 1.1 riastrad if (WARN(!oos_page, "shadow PPGTT page should have a oos page\n")) 1555 1.1 riastrad return -EINVAL; 1556 1.1 riastrad 1557 1.1 riastrad trace_oos_change(spt->vgpu->id, "set page out of sync", oos_page->id, 1558 1.1 riastrad spt, spt->guest_page.type); 1559 1.1 riastrad 1560 1.1 riastrad list_add_tail(&oos_page->vm_list, &spt->vgpu->gtt.oos_page_list_head); 1561 1.1 riastrad return intel_vgpu_disable_page_track(spt->vgpu, spt->guest_page.gfn); 1562 1.1 riastrad } 1563 1.1 riastrad 1564 1.1 riastrad /** 1565 1.1 riastrad * intel_vgpu_sync_oos_pages - sync all the out-of-synced shadow for vGPU 1566 1.1 riastrad * @vgpu: a vGPU 1567 1.1 riastrad * 1568 1.1 riastrad * This function is called before submitting a guest workload to host, 1569 1.1 riastrad * to sync all the out-of-synced shadow for vGPU 1570 1.1 riastrad * 1571 1.1 riastrad * Returns: 1572 1.1 riastrad * Zero on success, negative error code if failed. 1573 1.1 riastrad */ 1574 1.1 riastrad int intel_vgpu_sync_oos_pages(struct intel_vgpu *vgpu) 1575 1.1 riastrad { 1576 1.1 riastrad struct list_head *pos, *n; 1577 1.1 riastrad struct intel_vgpu_oos_page *oos_page; 1578 1.1 riastrad int ret; 1579 1.1 riastrad 1580 1.1 riastrad if (!enable_out_of_sync) 1581 1.1 riastrad return 0; 1582 1.1 riastrad 1583 1.1 riastrad list_for_each_safe(pos, n, &vgpu->gtt.oos_page_list_head) { 1584 1.1 riastrad oos_page = container_of(pos, 1585 1.1 riastrad struct intel_vgpu_oos_page, vm_list); 1586 1.1 riastrad ret = ppgtt_set_guest_page_sync(oos_page->spt); 1587 1.1 riastrad if (ret) 1588 1.1 riastrad return ret; 1589 1.1 riastrad } 1590 1.1 riastrad return 0; 1591 1.1 riastrad } 1592 1.1 riastrad 1593 1.1 riastrad /* 1594 1.1 riastrad * The heart of PPGTT shadow page table. 1595 1.1 riastrad */ 1596 1.1 riastrad static int ppgtt_handle_guest_write_page_table( 1597 1.1 riastrad struct intel_vgpu_ppgtt_spt *spt, 1598 1.1 riastrad struct intel_gvt_gtt_entry *we, unsigned long index) 1599 1.1 riastrad { 1600 1.1 riastrad struct intel_vgpu *vgpu = spt->vgpu; 1601 1.1 riastrad int type = spt->shadow_page.type; 1602 1.1 riastrad struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops; 1603 1.1 riastrad struct intel_gvt_gtt_entry old_se; 1604 1.1 riastrad int new_present; 1605 1.1 riastrad int i, ret; 1606 1.1 riastrad 1607 1.1 riastrad new_present = ops->test_present(we); 1608 1.1 riastrad 1609 1.1 riastrad /* 1610 1.1 riastrad * Adding the new entry first and then removing the old one, that can 1611 1.1 riastrad * guarantee the ppgtt table is validated during the window between 1612 1.1 riastrad * adding and removal. 1613 1.1 riastrad */ 1614 1.1 riastrad ppgtt_get_shadow_entry(spt, &old_se, index); 1615 1.1 riastrad 1616 1.1 riastrad if (new_present) { 1617 1.1 riastrad ret = ppgtt_handle_guest_entry_add(spt, we, index); 1618 1.1 riastrad if (ret) 1619 1.1 riastrad goto fail; 1620 1.1 riastrad } 1621 1.1 riastrad 1622 1.1 riastrad ret = ppgtt_handle_guest_entry_removal(spt, &old_se, index); 1623 1.1 riastrad if (ret) 1624 1.1 riastrad goto fail; 1625 1.1 riastrad 1626 1.1 riastrad if (!new_present) { 1627 1.1 riastrad /* For 64KB splited entries, we need clear them all. */ 1628 1.1 riastrad if (ops->test_64k_splited(&old_se) && 1629 1.1 riastrad !(index % GTT_64K_PTE_STRIDE)) { 1630 1.1 riastrad gvt_vdbg_mm("remove splited 64K shadow entries\n"); 1631 1.1 riastrad for (i = 0; i < GTT_64K_PTE_STRIDE; i++) { 1632 1.1 riastrad ops->clear_64k_splited(&old_se); 1633 1.1 riastrad ops->set_pfn(&old_se, 1634 1.1 riastrad vgpu->gtt.scratch_pt[type].page_mfn); 1635 1.1 riastrad ppgtt_set_shadow_entry(spt, &old_se, index + i); 1636 1.1 riastrad } 1637 1.1 riastrad } else if (old_se.type == GTT_TYPE_PPGTT_PTE_2M_ENTRY || 1638 1.1 riastrad old_se.type == GTT_TYPE_PPGTT_PTE_1G_ENTRY) { 1639 1.1 riastrad ops->clear_pse(&old_se); 1640 1.1 riastrad ops->set_pfn(&old_se, 1641 1.1 riastrad vgpu->gtt.scratch_pt[type].page_mfn); 1642 1.1 riastrad ppgtt_set_shadow_entry(spt, &old_se, index); 1643 1.1 riastrad } else { 1644 1.1 riastrad ops->set_pfn(&old_se, 1645 1.1 riastrad vgpu->gtt.scratch_pt[type].page_mfn); 1646 1.1 riastrad ppgtt_set_shadow_entry(spt, &old_se, index); 1647 1.1 riastrad } 1648 1.1 riastrad } 1649 1.1 riastrad 1650 1.1 riastrad return 0; 1651 1.1 riastrad fail: 1652 1.1 riastrad gvt_vgpu_err("fail: shadow page %p guest entry 0x%llx type %d.\n", 1653 1.1 riastrad spt, we->val64, we->type); 1654 1.1 riastrad return ret; 1655 1.1 riastrad } 1656 1.1 riastrad 1657 1.1 riastrad 1658 1.1 riastrad 1659 1.1 riastrad static inline bool can_do_out_of_sync(struct intel_vgpu_ppgtt_spt *spt) 1660 1.1 riastrad { 1661 1.1 riastrad return enable_out_of_sync 1662 1.1 riastrad && gtt_type_is_pte_pt(spt->guest_page.type) 1663 1.1 riastrad && spt->guest_page.write_cnt >= 2; 1664 1.1 riastrad } 1665 1.1 riastrad 1666 1.1 riastrad static void ppgtt_set_post_shadow(struct intel_vgpu_ppgtt_spt *spt, 1667 1.1 riastrad unsigned long index) 1668 1.1 riastrad { 1669 1.1 riastrad set_bit(index, spt->post_shadow_bitmap); 1670 1.1 riastrad if (!list_empty(&spt->post_shadow_list)) 1671 1.1 riastrad return; 1672 1.1 riastrad 1673 1.1 riastrad list_add_tail(&spt->post_shadow_list, 1674 1.1 riastrad &spt->vgpu->gtt.post_shadow_list_head); 1675 1.1 riastrad } 1676 1.1 riastrad 1677 1.1 riastrad /** 1678 1.1 riastrad * intel_vgpu_flush_post_shadow - flush the post shadow transactions 1679 1.1 riastrad * @vgpu: a vGPU 1680 1.1 riastrad * 1681 1.1 riastrad * This function is called before submitting a guest workload to host, 1682 1.1 riastrad * to flush all the post shadows for a vGPU. 1683 1.1 riastrad * 1684 1.1 riastrad * Returns: 1685 1.1 riastrad * Zero on success, negative error code if failed. 1686 1.1 riastrad */ 1687 1.1 riastrad int intel_vgpu_flush_post_shadow(struct intel_vgpu *vgpu) 1688 1.1 riastrad { 1689 1.1 riastrad struct list_head *pos, *n; 1690 1.1 riastrad struct intel_vgpu_ppgtt_spt *spt; 1691 1.1 riastrad struct intel_gvt_gtt_entry ge; 1692 1.1 riastrad unsigned long index; 1693 1.1 riastrad int ret; 1694 1.1 riastrad 1695 1.1 riastrad list_for_each_safe(pos, n, &vgpu->gtt.post_shadow_list_head) { 1696 1.1 riastrad spt = container_of(pos, struct intel_vgpu_ppgtt_spt, 1697 1.1 riastrad post_shadow_list); 1698 1.1 riastrad 1699 1.1 riastrad for_each_set_bit(index, spt->post_shadow_bitmap, 1700 1.1 riastrad GTT_ENTRY_NUM_IN_ONE_PAGE) { 1701 1.1 riastrad ppgtt_get_guest_entry(spt, &ge, index); 1702 1.1 riastrad 1703 1.1 riastrad ret = ppgtt_handle_guest_write_page_table(spt, 1704 1.1 riastrad &ge, index); 1705 1.1 riastrad if (ret) 1706 1.1 riastrad return ret; 1707 1.1 riastrad clear_bit(index, spt->post_shadow_bitmap); 1708 1.1 riastrad } 1709 1.1 riastrad list_del_init(&spt->post_shadow_list); 1710 1.1 riastrad } 1711 1.1 riastrad return 0; 1712 1.1 riastrad } 1713 1.1 riastrad 1714 1.1 riastrad static int ppgtt_handle_guest_write_page_table_bytes( 1715 1.1 riastrad struct intel_vgpu_ppgtt_spt *spt, 1716 1.1 riastrad u64 pa, void *p_data, int bytes) 1717 1.1 riastrad { 1718 1.1 riastrad struct intel_vgpu *vgpu = spt->vgpu; 1719 1.1 riastrad struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops; 1720 1.1 riastrad const struct intel_gvt_device_info *info = &vgpu->gvt->device_info; 1721 1.1 riastrad struct intel_gvt_gtt_entry we, se; 1722 1.1 riastrad unsigned long index; 1723 1.1 riastrad int ret; 1724 1.1 riastrad 1725 1.1 riastrad index = (pa & (PAGE_SIZE - 1)) >> info->gtt_entry_size_shift; 1726 1.1 riastrad 1727 1.1 riastrad ppgtt_get_guest_entry(spt, &we, index); 1728 1.1 riastrad 1729 1.1 riastrad /* 1730 1.1 riastrad * For page table which has 64K gtt entry, only PTE#0, PTE#16, 1731 1.1 riastrad * PTE#32, ... PTE#496 are used. Unused PTEs update should be 1732 1.1 riastrad * ignored. 1733 1.1 riastrad */ 1734 1.1 riastrad if (we.type == GTT_TYPE_PPGTT_PTE_64K_ENTRY && 1735 1.1 riastrad (index % GTT_64K_PTE_STRIDE)) { 1736 1.1 riastrad gvt_vdbg_mm("Ignore write to unused PTE entry, index %lu\n", 1737 1.1 riastrad index); 1738 1.1 riastrad return 0; 1739 1.1 riastrad } 1740 1.1 riastrad 1741 1.1 riastrad if (bytes == info->gtt_entry_size) { 1742 1.1 riastrad ret = ppgtt_handle_guest_write_page_table(spt, &we, index); 1743 1.1 riastrad if (ret) 1744 1.1 riastrad return ret; 1745 1.1 riastrad } else { 1746 1.1 riastrad if (!test_bit(index, spt->post_shadow_bitmap)) { 1747 1.1 riastrad int type = spt->shadow_page.type; 1748 1.1 riastrad 1749 1.1 riastrad ppgtt_get_shadow_entry(spt, &se, index); 1750 1.1 riastrad ret = ppgtt_handle_guest_entry_removal(spt, &se, index); 1751 1.1 riastrad if (ret) 1752 1.1 riastrad return ret; 1753 1.1 riastrad ops->set_pfn(&se, vgpu->gtt.scratch_pt[type].page_mfn); 1754 1.1 riastrad ppgtt_set_shadow_entry(spt, &se, index); 1755 1.1 riastrad } 1756 1.1 riastrad ppgtt_set_post_shadow(spt, index); 1757 1.1 riastrad } 1758 1.1 riastrad 1759 1.1 riastrad if (!enable_out_of_sync) 1760 1.1 riastrad return 0; 1761 1.1 riastrad 1762 1.1 riastrad spt->guest_page.write_cnt++; 1763 1.1 riastrad 1764 1.1 riastrad if (spt->guest_page.oos_page) 1765 1.1 riastrad ops->set_entry(spt->guest_page.oos_page->mem, &we, index, 1766 1.1 riastrad false, 0, vgpu); 1767 1.1 riastrad 1768 1.1 riastrad if (can_do_out_of_sync(spt)) { 1769 1.1 riastrad if (!spt->guest_page.oos_page) 1770 1.1 riastrad ppgtt_allocate_oos_page(spt); 1771 1.1 riastrad 1772 1.1 riastrad ret = ppgtt_set_guest_page_oos(spt); 1773 1.1 riastrad if (ret < 0) 1774 1.1 riastrad return ret; 1775 1.1 riastrad } 1776 1.1 riastrad return 0; 1777 1.1 riastrad } 1778 1.1 riastrad 1779 1.1 riastrad static void invalidate_ppgtt_mm(struct intel_vgpu_mm *mm) 1780 1.1 riastrad { 1781 1.1 riastrad struct intel_vgpu *vgpu = mm->vgpu; 1782 1.1 riastrad struct intel_gvt *gvt = vgpu->gvt; 1783 1.1 riastrad struct intel_gvt_gtt *gtt = &gvt->gtt; 1784 1.1 riastrad struct intel_gvt_gtt_pte_ops *ops = gtt->pte_ops; 1785 1.1 riastrad struct intel_gvt_gtt_entry se; 1786 1.1 riastrad int index; 1787 1.1 riastrad 1788 1.1 riastrad if (!mm->ppgtt_mm.shadowed) 1789 1.1 riastrad return; 1790 1.1 riastrad 1791 1.1 riastrad for (index = 0; index < ARRAY_SIZE(mm->ppgtt_mm.shadow_pdps); index++) { 1792 1.1 riastrad ppgtt_get_shadow_root_entry(mm, &se, index); 1793 1.1 riastrad 1794 1.1 riastrad if (!ops->test_present(&se)) 1795 1.1 riastrad continue; 1796 1.1 riastrad 1797 1.1 riastrad ppgtt_invalidate_spt_by_shadow_entry(vgpu, &se); 1798 1.1 riastrad se.val64 = 0; 1799 1.1 riastrad ppgtt_set_shadow_root_entry(mm, &se, index); 1800 1.1 riastrad 1801 1.1 riastrad trace_spt_guest_change(vgpu->id, "destroy root pointer", 1802 1.1 riastrad NULL, se.type, se.val64, index); 1803 1.1 riastrad } 1804 1.1 riastrad 1805 1.1 riastrad mm->ppgtt_mm.shadowed = false; 1806 1.1 riastrad } 1807 1.1 riastrad 1808 1.1 riastrad 1809 1.1 riastrad static int shadow_ppgtt_mm(struct intel_vgpu_mm *mm) 1810 1.1 riastrad { 1811 1.1 riastrad struct intel_vgpu *vgpu = mm->vgpu; 1812 1.1 riastrad struct intel_gvt *gvt = vgpu->gvt; 1813 1.1 riastrad struct intel_gvt_gtt *gtt = &gvt->gtt; 1814 1.1 riastrad struct intel_gvt_gtt_pte_ops *ops = gtt->pte_ops; 1815 1.1 riastrad struct intel_vgpu_ppgtt_spt *spt; 1816 1.1 riastrad struct intel_gvt_gtt_entry ge, se; 1817 1.1 riastrad int index, ret; 1818 1.1 riastrad 1819 1.1 riastrad if (mm->ppgtt_mm.shadowed) 1820 1.1 riastrad return 0; 1821 1.1 riastrad 1822 1.1 riastrad mm->ppgtt_mm.shadowed = true; 1823 1.1 riastrad 1824 1.1 riastrad for (index = 0; index < ARRAY_SIZE(mm->ppgtt_mm.guest_pdps); index++) { 1825 1.1 riastrad ppgtt_get_guest_root_entry(mm, &ge, index); 1826 1.1 riastrad 1827 1.1 riastrad if (!ops->test_present(&ge)) 1828 1.1 riastrad continue; 1829 1.1 riastrad 1830 1.1 riastrad trace_spt_guest_change(vgpu->id, __func__, NULL, 1831 1.1 riastrad ge.type, ge.val64, index); 1832 1.1 riastrad 1833 1.1 riastrad spt = ppgtt_populate_spt_by_guest_entry(vgpu, &ge); 1834 1.1 riastrad if (IS_ERR(spt)) { 1835 1.1 riastrad gvt_vgpu_err("fail to populate guest root pointer\n"); 1836 1.1 riastrad ret = PTR_ERR(spt); 1837 1.1 riastrad goto fail; 1838 1.1 riastrad } 1839 1.1 riastrad ppgtt_generate_shadow_entry(&se, spt, &ge); 1840 1.1 riastrad ppgtt_set_shadow_root_entry(mm, &se, index); 1841 1.1 riastrad 1842 1.1 riastrad trace_spt_guest_change(vgpu->id, "populate root pointer", 1843 1.1 riastrad NULL, se.type, se.val64, index); 1844 1.1 riastrad } 1845 1.1 riastrad 1846 1.1 riastrad return 0; 1847 1.1 riastrad fail: 1848 1.1 riastrad invalidate_ppgtt_mm(mm); 1849 1.1 riastrad return ret; 1850 1.1 riastrad } 1851 1.1 riastrad 1852 1.1 riastrad static struct intel_vgpu_mm *vgpu_alloc_mm(struct intel_vgpu *vgpu) 1853 1.1 riastrad { 1854 1.1 riastrad struct intel_vgpu_mm *mm; 1855 1.1 riastrad 1856 1.1 riastrad mm = kzalloc(sizeof(*mm), GFP_KERNEL); 1857 1.1 riastrad if (!mm) 1858 1.1 riastrad return NULL; 1859 1.1 riastrad 1860 1.1 riastrad mm->vgpu = vgpu; 1861 1.1 riastrad kref_init(&mm->ref); 1862 1.1 riastrad atomic_set(&mm->pincount, 0); 1863 1.1 riastrad 1864 1.1 riastrad return mm; 1865 1.1 riastrad } 1866 1.1 riastrad 1867 1.1 riastrad static void vgpu_free_mm(struct intel_vgpu_mm *mm) 1868 1.1 riastrad { 1869 1.1 riastrad kfree(mm); 1870 1.1 riastrad } 1871 1.1 riastrad 1872 1.1 riastrad /** 1873 1.1 riastrad * intel_vgpu_create_ppgtt_mm - create a ppgtt mm object for a vGPU 1874 1.1 riastrad * @vgpu: a vGPU 1875 1.1 riastrad * @root_entry_type: ppgtt root entry type 1876 1.1 riastrad * @pdps: guest pdps. 1877 1.1 riastrad * 1878 1.1 riastrad * This function is used to create a ppgtt mm object for a vGPU. 1879 1.1 riastrad * 1880 1.1 riastrad * Returns: 1881 1.1 riastrad * Zero on success, negative error code in pointer if failed. 1882 1.1 riastrad */ 1883 1.1 riastrad struct intel_vgpu_mm *intel_vgpu_create_ppgtt_mm(struct intel_vgpu *vgpu, 1884 1.1 riastrad enum intel_gvt_gtt_type root_entry_type, u64 pdps[]) 1885 1.1 riastrad { 1886 1.1 riastrad struct intel_gvt *gvt = vgpu->gvt; 1887 1.1 riastrad struct intel_vgpu_mm *mm; 1888 1.1 riastrad int ret; 1889 1.1 riastrad 1890 1.1 riastrad mm = vgpu_alloc_mm(vgpu); 1891 1.1 riastrad if (!mm) 1892 1.1 riastrad return ERR_PTR(-ENOMEM); 1893 1.1 riastrad 1894 1.1 riastrad mm->type = INTEL_GVT_MM_PPGTT; 1895 1.1 riastrad 1896 1.1 riastrad GEM_BUG_ON(root_entry_type != GTT_TYPE_PPGTT_ROOT_L3_ENTRY && 1897 1.1 riastrad root_entry_type != GTT_TYPE_PPGTT_ROOT_L4_ENTRY); 1898 1.1 riastrad mm->ppgtt_mm.root_entry_type = root_entry_type; 1899 1.1 riastrad 1900 1.1 riastrad INIT_LIST_HEAD(&mm->ppgtt_mm.list); 1901 1.1 riastrad INIT_LIST_HEAD(&mm->ppgtt_mm.lru_list); 1902 1.1 riastrad 1903 1.1 riastrad if (root_entry_type == GTT_TYPE_PPGTT_ROOT_L4_ENTRY) 1904 1.1 riastrad mm->ppgtt_mm.guest_pdps[0] = pdps[0]; 1905 1.1 riastrad else 1906 1.1 riastrad memcpy(mm->ppgtt_mm.guest_pdps, pdps, 1907 1.1 riastrad sizeof(mm->ppgtt_mm.guest_pdps)); 1908 1.1 riastrad 1909 1.1 riastrad ret = shadow_ppgtt_mm(mm); 1910 1.1 riastrad if (ret) { 1911 1.1 riastrad gvt_vgpu_err("failed to shadow ppgtt mm\n"); 1912 1.1 riastrad vgpu_free_mm(mm); 1913 1.1 riastrad return ERR_PTR(ret); 1914 1.1 riastrad } 1915 1.1 riastrad 1916 1.1 riastrad list_add_tail(&mm->ppgtt_mm.list, &vgpu->gtt.ppgtt_mm_list_head); 1917 1.1 riastrad 1918 1.1 riastrad mutex_lock(&gvt->gtt.ppgtt_mm_lock); 1919 1.1 riastrad list_add_tail(&mm->ppgtt_mm.lru_list, &gvt->gtt.ppgtt_mm_lru_list_head); 1920 1.1 riastrad mutex_unlock(&gvt->gtt.ppgtt_mm_lock); 1921 1.1 riastrad 1922 1.1 riastrad return mm; 1923 1.1 riastrad } 1924 1.1 riastrad 1925 1.1 riastrad static struct intel_vgpu_mm *intel_vgpu_create_ggtt_mm(struct intel_vgpu *vgpu) 1926 1.1 riastrad { 1927 1.1 riastrad struct intel_vgpu_mm *mm; 1928 1.1 riastrad unsigned long nr_entries; 1929 1.1 riastrad 1930 1.1 riastrad mm = vgpu_alloc_mm(vgpu); 1931 1.1 riastrad if (!mm) 1932 1.1 riastrad return ERR_PTR(-ENOMEM); 1933 1.1 riastrad 1934 1.1 riastrad mm->type = INTEL_GVT_MM_GGTT; 1935 1.1 riastrad 1936 1.1 riastrad nr_entries = gvt_ggtt_gm_sz(vgpu->gvt) >> I915_GTT_PAGE_SHIFT; 1937 1.1 riastrad mm->ggtt_mm.virtual_ggtt = 1938 1.1 riastrad vzalloc(array_size(nr_entries, 1939 1.1 riastrad vgpu->gvt->device_info.gtt_entry_size)); 1940 1.1 riastrad if (!mm->ggtt_mm.virtual_ggtt) { 1941 1.1 riastrad vgpu_free_mm(mm); 1942 1.1 riastrad return ERR_PTR(-ENOMEM); 1943 1.1 riastrad } 1944 1.1 riastrad 1945 1.1 riastrad return mm; 1946 1.1 riastrad } 1947 1.1 riastrad 1948 1.1 riastrad /** 1949 1.1 riastrad * _intel_vgpu_mm_release - destroy a mm object 1950 1.1 riastrad * @mm_ref: a kref object 1951 1.1 riastrad * 1952 1.1 riastrad * This function is used to destroy a mm object for vGPU 1953 1.1 riastrad * 1954 1.1 riastrad */ 1955 1.1 riastrad void _intel_vgpu_mm_release(struct kref *mm_ref) 1956 1.1 riastrad { 1957 1.1 riastrad struct intel_vgpu_mm *mm = container_of(mm_ref, typeof(*mm), ref); 1958 1.1 riastrad 1959 1.1 riastrad if (GEM_WARN_ON(atomic_read(&mm->pincount))) 1960 1.1 riastrad gvt_err("vgpu mm pin count bug detected\n"); 1961 1.1 riastrad 1962 1.1 riastrad if (mm->type == INTEL_GVT_MM_PPGTT) { 1963 1.1 riastrad list_del(&mm->ppgtt_mm.list); 1964 1.1 riastrad 1965 1.1 riastrad mutex_lock(&mm->vgpu->gvt->gtt.ppgtt_mm_lock); 1966 1.1 riastrad list_del(&mm->ppgtt_mm.lru_list); 1967 1.1 riastrad mutex_unlock(&mm->vgpu->gvt->gtt.ppgtt_mm_lock); 1968 1.1 riastrad 1969 1.1 riastrad invalidate_ppgtt_mm(mm); 1970 1.1 riastrad } else { 1971 1.1 riastrad vfree(mm->ggtt_mm.virtual_ggtt); 1972 1.1 riastrad } 1973 1.1 riastrad 1974 1.1 riastrad vgpu_free_mm(mm); 1975 1.1 riastrad } 1976 1.1 riastrad 1977 1.1 riastrad /** 1978 1.1 riastrad * intel_vgpu_unpin_mm - decrease the pin count of a vGPU mm object 1979 1.1 riastrad * @mm: a vGPU mm object 1980 1.1 riastrad * 1981 1.1 riastrad * This function is called when user doesn't want to use a vGPU mm object 1982 1.1 riastrad */ 1983 1.1 riastrad void intel_vgpu_unpin_mm(struct intel_vgpu_mm *mm) 1984 1.1 riastrad { 1985 1.1 riastrad atomic_dec_if_positive(&mm->pincount); 1986 1.1 riastrad } 1987 1.1 riastrad 1988 1.1 riastrad /** 1989 1.1 riastrad * intel_vgpu_pin_mm - increase the pin count of a vGPU mm object 1990 1.1 riastrad * @mm: target vgpu mm 1991 1.1 riastrad * 1992 1.1 riastrad * This function is called when user wants to use a vGPU mm object. If this 1993 1.1 riastrad * mm object hasn't been shadowed yet, the shadow will be populated at this 1994 1.1 riastrad * time. 1995 1.1 riastrad * 1996 1.1 riastrad * Returns: 1997 1.1 riastrad * Zero on success, negative error code if failed. 1998 1.1 riastrad */ 1999 1.1 riastrad int intel_vgpu_pin_mm(struct intel_vgpu_mm *mm) 2000 1.1 riastrad { 2001 1.1 riastrad int ret; 2002 1.1 riastrad 2003 1.1 riastrad atomic_inc(&mm->pincount); 2004 1.1 riastrad 2005 1.1 riastrad if (mm->type == INTEL_GVT_MM_PPGTT) { 2006 1.1 riastrad ret = shadow_ppgtt_mm(mm); 2007 1.1 riastrad if (ret) 2008 1.1 riastrad return ret; 2009 1.1 riastrad 2010 1.1 riastrad mutex_lock(&mm->vgpu->gvt->gtt.ppgtt_mm_lock); 2011 1.1 riastrad list_move_tail(&mm->ppgtt_mm.lru_list, 2012 1.1 riastrad &mm->vgpu->gvt->gtt.ppgtt_mm_lru_list_head); 2013 1.1 riastrad mutex_unlock(&mm->vgpu->gvt->gtt.ppgtt_mm_lock); 2014 1.1 riastrad } 2015 1.1 riastrad 2016 1.1 riastrad return 0; 2017 1.1 riastrad } 2018 1.1 riastrad 2019 1.1 riastrad static int reclaim_one_ppgtt_mm(struct intel_gvt *gvt) 2020 1.1 riastrad { 2021 1.1 riastrad struct intel_vgpu_mm *mm; 2022 1.1 riastrad struct list_head *pos, *n; 2023 1.1 riastrad 2024 1.1 riastrad mutex_lock(&gvt->gtt.ppgtt_mm_lock); 2025 1.1 riastrad 2026 1.1 riastrad list_for_each_safe(pos, n, &gvt->gtt.ppgtt_mm_lru_list_head) { 2027 1.1 riastrad mm = container_of(pos, struct intel_vgpu_mm, ppgtt_mm.lru_list); 2028 1.1 riastrad 2029 1.1 riastrad if (atomic_read(&mm->pincount)) 2030 1.1 riastrad continue; 2031 1.1 riastrad 2032 1.1 riastrad list_del_init(&mm->ppgtt_mm.lru_list); 2033 1.1 riastrad mutex_unlock(&gvt->gtt.ppgtt_mm_lock); 2034 1.1 riastrad invalidate_ppgtt_mm(mm); 2035 1.1 riastrad return 1; 2036 1.1 riastrad } 2037 1.1 riastrad mutex_unlock(&gvt->gtt.ppgtt_mm_lock); 2038 1.1 riastrad return 0; 2039 1.1 riastrad } 2040 1.1 riastrad 2041 1.1 riastrad /* 2042 1.1 riastrad * GMA translation APIs. 2043 1.1 riastrad */ 2044 1.1 riastrad static inline int ppgtt_get_next_level_entry(struct intel_vgpu_mm *mm, 2045 1.1 riastrad struct intel_gvt_gtt_entry *e, unsigned long index, bool guest) 2046 1.1 riastrad { 2047 1.1 riastrad struct intel_vgpu *vgpu = mm->vgpu; 2048 1.1 riastrad struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops; 2049 1.1 riastrad struct intel_vgpu_ppgtt_spt *s; 2050 1.1 riastrad 2051 1.1 riastrad s = intel_vgpu_find_spt_by_mfn(vgpu, ops->get_pfn(e)); 2052 1.1 riastrad if (!s) 2053 1.1 riastrad return -ENXIO; 2054 1.1 riastrad 2055 1.1 riastrad if (!guest) 2056 1.1 riastrad ppgtt_get_shadow_entry(s, e, index); 2057 1.1 riastrad else 2058 1.1 riastrad ppgtt_get_guest_entry(s, e, index); 2059 1.1 riastrad return 0; 2060 1.1 riastrad } 2061 1.1 riastrad 2062 1.1 riastrad /** 2063 1.1 riastrad * intel_vgpu_gma_to_gpa - translate a gma to GPA 2064 1.1 riastrad * @mm: mm object. could be a PPGTT or GGTT mm object 2065 1.1 riastrad * @gma: graphics memory address in this mm object 2066 1.1 riastrad * 2067 1.1 riastrad * This function is used to translate a graphics memory address in specific 2068 1.1 riastrad * graphics memory space to guest physical address. 2069 1.1 riastrad * 2070 1.1 riastrad * Returns: 2071 1.1 riastrad * Guest physical address on success, INTEL_GVT_INVALID_ADDR if failed. 2072 1.1 riastrad */ 2073 1.1 riastrad unsigned long intel_vgpu_gma_to_gpa(struct intel_vgpu_mm *mm, unsigned long gma) 2074 1.1 riastrad { 2075 1.1 riastrad struct intel_vgpu *vgpu = mm->vgpu; 2076 1.1 riastrad struct intel_gvt *gvt = vgpu->gvt; 2077 1.1 riastrad struct intel_gvt_gtt_pte_ops *pte_ops = gvt->gtt.pte_ops; 2078 1.1 riastrad struct intel_gvt_gtt_gma_ops *gma_ops = gvt->gtt.gma_ops; 2079 1.1 riastrad unsigned long gpa = INTEL_GVT_INVALID_ADDR; 2080 1.1 riastrad unsigned long gma_index[4]; 2081 1.1 riastrad struct intel_gvt_gtt_entry e; 2082 1.1 riastrad int i, levels = 0; 2083 1.1 riastrad int ret; 2084 1.1 riastrad 2085 1.1 riastrad GEM_BUG_ON(mm->type != INTEL_GVT_MM_GGTT && 2086 1.1 riastrad mm->type != INTEL_GVT_MM_PPGTT); 2087 1.1 riastrad 2088 1.1 riastrad if (mm->type == INTEL_GVT_MM_GGTT) { 2089 1.1 riastrad if (!vgpu_gmadr_is_valid(vgpu, gma)) 2090 1.1 riastrad goto err; 2091 1.1 riastrad 2092 1.1 riastrad ggtt_get_guest_entry(mm, &e, 2093 1.1 riastrad gma_ops->gma_to_ggtt_pte_index(gma)); 2094 1.1 riastrad 2095 1.1 riastrad gpa = (pte_ops->get_pfn(&e) << I915_GTT_PAGE_SHIFT) 2096 1.1 riastrad + (gma & ~I915_GTT_PAGE_MASK); 2097 1.1 riastrad 2098 1.1 riastrad trace_gma_translate(vgpu->id, "ggtt", 0, 0, gma, gpa); 2099 1.1 riastrad } else { 2100 1.1 riastrad switch (mm->ppgtt_mm.root_entry_type) { 2101 1.1 riastrad case GTT_TYPE_PPGTT_ROOT_L4_ENTRY: 2102 1.1 riastrad ppgtt_get_shadow_root_entry(mm, &e, 0); 2103 1.1 riastrad 2104 1.1 riastrad gma_index[0] = gma_ops->gma_to_pml4_index(gma); 2105 1.1 riastrad gma_index[1] = gma_ops->gma_to_l4_pdp_index(gma); 2106 1.1 riastrad gma_index[2] = gma_ops->gma_to_pde_index(gma); 2107 1.1 riastrad gma_index[3] = gma_ops->gma_to_pte_index(gma); 2108 1.1 riastrad levels = 4; 2109 1.1 riastrad break; 2110 1.1 riastrad case GTT_TYPE_PPGTT_ROOT_L3_ENTRY: 2111 1.1 riastrad ppgtt_get_shadow_root_entry(mm, &e, 2112 1.1 riastrad gma_ops->gma_to_l3_pdp_index(gma)); 2113 1.1 riastrad 2114 1.1 riastrad gma_index[0] = gma_ops->gma_to_pde_index(gma); 2115 1.1 riastrad gma_index[1] = gma_ops->gma_to_pte_index(gma); 2116 1.1 riastrad levels = 2; 2117 1.1 riastrad break; 2118 1.1 riastrad default: 2119 1.1 riastrad GEM_BUG_ON(1); 2120 1.1 riastrad } 2121 1.1 riastrad 2122 1.1 riastrad /* walk the shadow page table and get gpa from guest entry */ 2123 1.1 riastrad for (i = 0; i < levels; i++) { 2124 1.1 riastrad ret = ppgtt_get_next_level_entry(mm, &e, gma_index[i], 2125 1.1 riastrad (i == levels - 1)); 2126 1.1 riastrad if (ret) 2127 1.1 riastrad goto err; 2128 1.1 riastrad 2129 1.1 riastrad if (!pte_ops->test_present(&e)) { 2130 1.1 riastrad gvt_dbg_core("GMA 0x%lx is not present\n", gma); 2131 1.1 riastrad goto err; 2132 1.1 riastrad } 2133 1.1 riastrad } 2134 1.1 riastrad 2135 1.1 riastrad gpa = (pte_ops->get_pfn(&e) << I915_GTT_PAGE_SHIFT) + 2136 1.1 riastrad (gma & ~I915_GTT_PAGE_MASK); 2137 1.1 riastrad trace_gma_translate(vgpu->id, "ppgtt", 0, 2138 1.1 riastrad mm->ppgtt_mm.root_entry_type, gma, gpa); 2139 1.1 riastrad } 2140 1.1 riastrad 2141 1.1 riastrad return gpa; 2142 1.1 riastrad err: 2143 1.1 riastrad gvt_vgpu_err("invalid mm type: %d gma %lx\n", mm->type, gma); 2144 1.1 riastrad return INTEL_GVT_INVALID_ADDR; 2145 1.1 riastrad } 2146 1.1 riastrad 2147 1.1 riastrad static int emulate_ggtt_mmio_read(struct intel_vgpu *vgpu, 2148 1.1 riastrad unsigned int off, void *p_data, unsigned int bytes) 2149 1.1 riastrad { 2150 1.1 riastrad struct intel_vgpu_mm *ggtt_mm = vgpu->gtt.ggtt_mm; 2151 1.1 riastrad const struct intel_gvt_device_info *info = &vgpu->gvt->device_info; 2152 1.1 riastrad unsigned long index = off >> info->gtt_entry_size_shift; 2153 1.1 riastrad unsigned long gma; 2154 1.1 riastrad struct intel_gvt_gtt_entry e; 2155 1.1 riastrad 2156 1.1 riastrad if (bytes != 4 && bytes != 8) 2157 1.1 riastrad return -EINVAL; 2158 1.1 riastrad 2159 1.1 riastrad gma = index << I915_GTT_PAGE_SHIFT; 2160 1.1 riastrad if (!intel_gvt_ggtt_validate_range(vgpu, 2161 1.1 riastrad gma, 1 << I915_GTT_PAGE_SHIFT)) { 2162 1.1 riastrad gvt_dbg_mm("read invalid ggtt at 0x%lx\n", gma); 2163 1.1 riastrad memset(p_data, 0, bytes); 2164 1.1 riastrad return 0; 2165 1.1 riastrad } 2166 1.1 riastrad 2167 1.1 riastrad ggtt_get_guest_entry(ggtt_mm, &e, index); 2168 1.1 riastrad memcpy(p_data, (void *)&e.val64 + (off & (info->gtt_entry_size - 1)), 2169 1.1 riastrad bytes); 2170 1.1 riastrad return 0; 2171 1.1 riastrad } 2172 1.1 riastrad 2173 1.1 riastrad /** 2174 1.1 riastrad * intel_vgpu_emulate_gtt_mmio_read - emulate GTT MMIO register read 2175 1.1 riastrad * @vgpu: a vGPU 2176 1.1 riastrad * @off: register offset 2177 1.1 riastrad * @p_data: data will be returned to guest 2178 1.1 riastrad * @bytes: data length 2179 1.1 riastrad * 2180 1.1 riastrad * This function is used to emulate the GTT MMIO register read 2181 1.1 riastrad * 2182 1.1 riastrad * Returns: 2183 1.1 riastrad * Zero on success, error code if failed. 2184 1.1 riastrad */ 2185 1.1 riastrad int intel_vgpu_emulate_ggtt_mmio_read(struct intel_vgpu *vgpu, unsigned int off, 2186 1.1 riastrad void *p_data, unsigned int bytes) 2187 1.1 riastrad { 2188 1.1 riastrad const struct intel_gvt_device_info *info = &vgpu->gvt->device_info; 2189 1.1 riastrad int ret; 2190 1.1 riastrad 2191 1.1 riastrad if (bytes != 4 && bytes != 8) 2192 1.1 riastrad return -EINVAL; 2193 1.1 riastrad 2194 1.1 riastrad off -= info->gtt_start_offset; 2195 1.1 riastrad ret = emulate_ggtt_mmio_read(vgpu, off, p_data, bytes); 2196 1.1 riastrad return ret; 2197 1.1 riastrad } 2198 1.1 riastrad 2199 1.1 riastrad static void ggtt_invalidate_pte(struct intel_vgpu *vgpu, 2200 1.1 riastrad struct intel_gvt_gtt_entry *entry) 2201 1.1 riastrad { 2202 1.1 riastrad struct intel_gvt_gtt_pte_ops *pte_ops = vgpu->gvt->gtt.pte_ops; 2203 1.1 riastrad unsigned long pfn; 2204 1.1 riastrad 2205 1.1 riastrad pfn = pte_ops->get_pfn(entry); 2206 1.1 riastrad if (pfn != vgpu->gvt->gtt.scratch_mfn) 2207 1.1 riastrad intel_gvt_hypervisor_dma_unmap_guest_page(vgpu, 2208 1.1 riastrad pfn << PAGE_SHIFT); 2209 1.1 riastrad } 2210 1.1 riastrad 2211 1.1 riastrad static int emulate_ggtt_mmio_write(struct intel_vgpu *vgpu, unsigned int off, 2212 1.1 riastrad void *p_data, unsigned int bytes) 2213 1.1 riastrad { 2214 1.1 riastrad struct intel_gvt *gvt = vgpu->gvt; 2215 1.1 riastrad const struct intel_gvt_device_info *info = &gvt->device_info; 2216 1.1 riastrad struct intel_vgpu_mm *ggtt_mm = vgpu->gtt.ggtt_mm; 2217 1.1 riastrad struct intel_gvt_gtt_pte_ops *ops = gvt->gtt.pte_ops; 2218 1.1 riastrad unsigned long g_gtt_index = off >> info->gtt_entry_size_shift; 2219 1.1 riastrad unsigned long gma, gfn; 2220 1.1 riastrad struct intel_gvt_gtt_entry e = {.val64 = 0, .type = GTT_TYPE_GGTT_PTE}; 2221 1.1 riastrad struct intel_gvt_gtt_entry m = {.val64 = 0, .type = GTT_TYPE_GGTT_PTE}; 2222 1.1 riastrad dma_addr_t dma_addr; 2223 1.1 riastrad int ret; 2224 1.1 riastrad struct intel_gvt_partial_pte *partial_pte, *pos, *n; 2225 1.1 riastrad bool partial_update = false; 2226 1.1 riastrad 2227 1.1 riastrad if (bytes != 4 && bytes != 8) 2228 1.1 riastrad return -EINVAL; 2229 1.1 riastrad 2230 1.1 riastrad gma = g_gtt_index << I915_GTT_PAGE_SHIFT; 2231 1.1 riastrad 2232 1.1 riastrad /* the VM may configure the whole GM space when ballooning is used */ 2233 1.1 riastrad if (!vgpu_gmadr_is_valid(vgpu, gma)) 2234 1.1 riastrad return 0; 2235 1.1 riastrad 2236 1.1 riastrad e.type = GTT_TYPE_GGTT_PTE; 2237 1.1 riastrad memcpy((void *)&e.val64 + (off & (info->gtt_entry_size - 1)), p_data, 2238 1.1 riastrad bytes); 2239 1.1 riastrad 2240 1.1 riastrad /* If ggtt entry size is 8 bytes, and it's split into two 4 bytes 2241 1.1 riastrad * write, save the first 4 bytes in a list and update virtual 2242 1.1 riastrad * PTE. Only update shadow PTE when the second 4 bytes comes. 2243 1.1 riastrad */ 2244 1.1 riastrad if (bytes < info->gtt_entry_size) { 2245 1.1 riastrad bool found = false; 2246 1.1 riastrad 2247 1.1 riastrad list_for_each_entry_safe(pos, n, 2248 1.1 riastrad &ggtt_mm->ggtt_mm.partial_pte_list, list) { 2249 1.1 riastrad if (g_gtt_index == pos->offset >> 2250 1.1 riastrad info->gtt_entry_size_shift) { 2251 1.1 riastrad if (off != pos->offset) { 2252 1.1 riastrad /* the second partial part*/ 2253 1.1 riastrad int last_off = pos->offset & 2254 1.1 riastrad (info->gtt_entry_size - 1); 2255 1.1 riastrad 2256 1.1 riastrad memcpy((void *)&e.val64 + last_off, 2257 1.1 riastrad (void *)&pos->data + last_off, 2258 1.1 riastrad bytes); 2259 1.1 riastrad 2260 1.1 riastrad list_del(&pos->list); 2261 1.1 riastrad kfree(pos); 2262 1.1 riastrad found = true; 2263 1.1 riastrad break; 2264 1.1 riastrad } 2265 1.1 riastrad 2266 1.1 riastrad /* update of the first partial part */ 2267 1.1 riastrad pos->data = e.val64; 2268 1.1 riastrad ggtt_set_guest_entry(ggtt_mm, &e, g_gtt_index); 2269 1.1 riastrad return 0; 2270 1.1 riastrad } 2271 1.1 riastrad } 2272 1.1 riastrad 2273 1.1 riastrad if (!found) { 2274 1.1 riastrad /* the first partial part */ 2275 1.1 riastrad partial_pte = kzalloc(sizeof(*partial_pte), GFP_KERNEL); 2276 1.1 riastrad if (!partial_pte) 2277 1.1 riastrad return -ENOMEM; 2278 1.1 riastrad partial_pte->offset = off; 2279 1.1 riastrad partial_pte->data = e.val64; 2280 1.1 riastrad list_add_tail(&partial_pte->list, 2281 1.1 riastrad &ggtt_mm->ggtt_mm.partial_pte_list); 2282 1.1 riastrad partial_update = true; 2283 1.1 riastrad } 2284 1.1 riastrad } 2285 1.1 riastrad 2286 1.1 riastrad if (!partial_update && (ops->test_present(&e))) { 2287 1.1 riastrad gfn = ops->get_pfn(&e); 2288 1.1 riastrad m.val64 = e.val64; 2289 1.1 riastrad m.type = e.type; 2290 1.1 riastrad 2291 1.1 riastrad /* one PTE update may be issued in multiple writes and the 2292 1.1 riastrad * first write may not construct a valid gfn 2293 1.1 riastrad */ 2294 1.1 riastrad if (!intel_gvt_hypervisor_is_valid_gfn(vgpu, gfn)) { 2295 1.1 riastrad ops->set_pfn(&m, gvt->gtt.scratch_mfn); 2296 1.1 riastrad goto out; 2297 1.1 riastrad } 2298 1.1 riastrad 2299 1.1 riastrad ret = intel_gvt_hypervisor_dma_map_guest_page(vgpu, gfn, 2300 1.1 riastrad PAGE_SIZE, &dma_addr); 2301 1.1 riastrad if (ret) { 2302 1.1 riastrad gvt_vgpu_err("fail to populate guest ggtt entry\n"); 2303 1.1 riastrad /* guest driver may read/write the entry when partial 2304 1.1 riastrad * update the entry in this situation p2m will fail 2305 1.1 riastrad * settting the shadow entry to point to a scratch page 2306 1.1 riastrad */ 2307 1.1 riastrad ops->set_pfn(&m, gvt->gtt.scratch_mfn); 2308 1.1 riastrad } else 2309 1.1 riastrad ops->set_pfn(&m, dma_addr >> PAGE_SHIFT); 2310 1.1 riastrad } else { 2311 1.1 riastrad ops->set_pfn(&m, gvt->gtt.scratch_mfn); 2312 1.1 riastrad ops->clear_present(&m); 2313 1.1 riastrad } 2314 1.1 riastrad 2315 1.1 riastrad out: 2316 1.1 riastrad ggtt_set_guest_entry(ggtt_mm, &e, g_gtt_index); 2317 1.1 riastrad 2318 1.1 riastrad ggtt_get_host_entry(ggtt_mm, &e, g_gtt_index); 2319 1.1 riastrad ggtt_invalidate_pte(vgpu, &e); 2320 1.1 riastrad 2321 1.1 riastrad ggtt_set_host_entry(ggtt_mm, &m, g_gtt_index); 2322 1.1 riastrad ggtt_invalidate(gvt->dev_priv); 2323 1.1 riastrad return 0; 2324 1.1 riastrad } 2325 1.1 riastrad 2326 1.1 riastrad /* 2327 1.1 riastrad * intel_vgpu_emulate_ggtt_mmio_write - emulate GTT MMIO register write 2328 1.1 riastrad * @vgpu: a vGPU 2329 1.1 riastrad * @off: register offset 2330 1.1 riastrad * @p_data: data from guest write 2331 1.1 riastrad * @bytes: data length 2332 1.1 riastrad * 2333 1.1 riastrad * This function is used to emulate the GTT MMIO register write 2334 1.1 riastrad * 2335 1.1 riastrad * Returns: 2336 1.1 riastrad * Zero on success, error code if failed. 2337 1.1 riastrad */ 2338 1.1 riastrad int intel_vgpu_emulate_ggtt_mmio_write(struct intel_vgpu *vgpu, 2339 1.1 riastrad unsigned int off, void *p_data, unsigned int bytes) 2340 1.1 riastrad { 2341 1.1 riastrad const struct intel_gvt_device_info *info = &vgpu->gvt->device_info; 2342 1.1 riastrad int ret; 2343 1.1 riastrad 2344 1.1 riastrad if (bytes != 4 && bytes != 8) 2345 1.1 riastrad return -EINVAL; 2346 1.1 riastrad 2347 1.1 riastrad off -= info->gtt_start_offset; 2348 1.1 riastrad ret = emulate_ggtt_mmio_write(vgpu, off, p_data, bytes); 2349 1.1 riastrad return ret; 2350 1.1 riastrad } 2351 1.1 riastrad 2352 1.1 riastrad static int alloc_scratch_pages(struct intel_vgpu *vgpu, 2353 1.1 riastrad enum intel_gvt_gtt_type type) 2354 1.1 riastrad { 2355 1.1 riastrad struct intel_vgpu_gtt *gtt = &vgpu->gtt; 2356 1.1 riastrad struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops; 2357 1.1 riastrad int page_entry_num = I915_GTT_PAGE_SIZE >> 2358 1.1 riastrad vgpu->gvt->device_info.gtt_entry_size_shift; 2359 1.1 riastrad void *scratch_pt; 2360 1.1 riastrad int i; 2361 1.1 riastrad struct device *dev = &vgpu->gvt->dev_priv->drm.pdev->dev; 2362 1.1 riastrad dma_addr_t daddr; 2363 1.1 riastrad 2364 1.1 riastrad if (WARN_ON(type < GTT_TYPE_PPGTT_PTE_PT || type >= GTT_TYPE_MAX)) 2365 1.1 riastrad return -EINVAL; 2366 1.1 riastrad 2367 1.1 riastrad scratch_pt = (void *)get_zeroed_page(GFP_KERNEL); 2368 1.1 riastrad if (!scratch_pt) { 2369 1.1 riastrad gvt_vgpu_err("fail to allocate scratch page\n"); 2370 1.1 riastrad return -ENOMEM; 2371 1.1 riastrad } 2372 1.1 riastrad 2373 1.1 riastrad daddr = dma_map_page(dev, virt_to_page(scratch_pt), 0, 2374 1.1 riastrad 4096, PCI_DMA_BIDIRECTIONAL); 2375 1.1 riastrad if (dma_mapping_error(dev, daddr)) { 2376 1.1 riastrad gvt_vgpu_err("fail to dmamap scratch_pt\n"); 2377 1.1 riastrad __free_page(virt_to_page(scratch_pt)); 2378 1.1 riastrad return -ENOMEM; 2379 1.1 riastrad } 2380 1.1 riastrad gtt->scratch_pt[type].page_mfn = 2381 1.1 riastrad (unsigned long)(daddr >> I915_GTT_PAGE_SHIFT); 2382 1.1 riastrad gtt->scratch_pt[type].page = virt_to_page(scratch_pt); 2383 1.1 riastrad gvt_dbg_mm("vgpu%d create scratch_pt: type %d mfn=0x%lx\n", 2384 1.1 riastrad vgpu->id, type, gtt->scratch_pt[type].page_mfn); 2385 1.1 riastrad 2386 1.1 riastrad /* Build the tree by full filled the scratch pt with the entries which 2387 1.1 riastrad * point to the next level scratch pt or scratch page. The 2388 1.1 riastrad * scratch_pt[type] indicate the scratch pt/scratch page used by the 2389 1.1 riastrad * 'type' pt. 2390 1.1 riastrad * e.g. scratch_pt[GTT_TYPE_PPGTT_PDE_PT] is used by 2391 1.1 riastrad * GTT_TYPE_PPGTT_PDE_PT level pt, that means this scratch_pt it self 2392 1.1 riastrad * is GTT_TYPE_PPGTT_PTE_PT, and full filled by scratch page mfn. 2393 1.1 riastrad */ 2394 1.1 riastrad if (type > GTT_TYPE_PPGTT_PTE_PT) { 2395 1.1 riastrad struct intel_gvt_gtt_entry se; 2396 1.1 riastrad 2397 1.1 riastrad memset(&se, 0, sizeof(struct intel_gvt_gtt_entry)); 2398 1.1 riastrad se.type = get_entry_type(type - 1); 2399 1.1 riastrad ops->set_pfn(&se, gtt->scratch_pt[type - 1].page_mfn); 2400 1.1 riastrad 2401 1.1 riastrad /* The entry parameters like present/writeable/cache type 2402 1.1 riastrad * set to the same as i915's scratch page tree. 2403 1.1 riastrad */ 2404 1.1 riastrad se.val64 |= _PAGE_PRESENT | _PAGE_RW; 2405 1.1 riastrad if (type == GTT_TYPE_PPGTT_PDE_PT) 2406 1.1 riastrad se.val64 |= PPAT_CACHED; 2407 1.1 riastrad 2408 1.1 riastrad for (i = 0; i < page_entry_num; i++) 2409 1.1 riastrad ops->set_entry(scratch_pt, &se, i, false, 0, vgpu); 2410 1.1 riastrad } 2411 1.1 riastrad 2412 1.1 riastrad return 0; 2413 1.1 riastrad } 2414 1.1 riastrad 2415 1.1 riastrad static int release_scratch_page_tree(struct intel_vgpu *vgpu) 2416 1.1 riastrad { 2417 1.1 riastrad int i; 2418 1.1 riastrad struct device *dev = &vgpu->gvt->dev_priv->drm.pdev->dev; 2419 1.1 riastrad dma_addr_t daddr; 2420 1.1 riastrad 2421 1.1 riastrad for (i = GTT_TYPE_PPGTT_PTE_PT; i < GTT_TYPE_MAX; i++) { 2422 1.1 riastrad if (vgpu->gtt.scratch_pt[i].page != NULL) { 2423 1.1 riastrad daddr = (dma_addr_t)(vgpu->gtt.scratch_pt[i].page_mfn << 2424 1.1 riastrad I915_GTT_PAGE_SHIFT); 2425 1.1 riastrad dma_unmap_page(dev, daddr, 4096, PCI_DMA_BIDIRECTIONAL); 2426 1.1 riastrad __free_page(vgpu->gtt.scratch_pt[i].page); 2427 1.1 riastrad vgpu->gtt.scratch_pt[i].page = NULL; 2428 1.1 riastrad vgpu->gtt.scratch_pt[i].page_mfn = 0; 2429 1.1 riastrad } 2430 1.1 riastrad } 2431 1.1 riastrad 2432 1.1 riastrad return 0; 2433 1.1 riastrad } 2434 1.1 riastrad 2435 1.1 riastrad static int create_scratch_page_tree(struct intel_vgpu *vgpu) 2436 1.1 riastrad { 2437 1.1 riastrad int i, ret; 2438 1.1 riastrad 2439 1.1 riastrad for (i = GTT_TYPE_PPGTT_PTE_PT; i < GTT_TYPE_MAX; i++) { 2440 1.1 riastrad ret = alloc_scratch_pages(vgpu, i); 2441 1.1 riastrad if (ret) 2442 1.1 riastrad goto err; 2443 1.1 riastrad } 2444 1.1 riastrad 2445 1.1 riastrad return 0; 2446 1.1 riastrad 2447 1.1 riastrad err: 2448 1.1 riastrad release_scratch_page_tree(vgpu); 2449 1.1 riastrad return ret; 2450 1.1 riastrad } 2451 1.1 riastrad 2452 1.1 riastrad /** 2453 1.1 riastrad * intel_vgpu_init_gtt - initialize per-vGPU graphics memory virulization 2454 1.1 riastrad * @vgpu: a vGPU 2455 1.1 riastrad * 2456 1.1 riastrad * This function is used to initialize per-vGPU graphics memory virtualization 2457 1.1 riastrad * components. 2458 1.1 riastrad * 2459 1.1 riastrad * Returns: 2460 1.1 riastrad * Zero on success, error code if failed. 2461 1.1 riastrad */ 2462 1.1 riastrad int intel_vgpu_init_gtt(struct intel_vgpu *vgpu) 2463 1.1 riastrad { 2464 1.1 riastrad struct intel_vgpu_gtt *gtt = &vgpu->gtt; 2465 1.1 riastrad 2466 1.1 riastrad INIT_RADIX_TREE(>t->spt_tree, GFP_KERNEL); 2467 1.1 riastrad 2468 1.1 riastrad INIT_LIST_HEAD(>t->ppgtt_mm_list_head); 2469 1.1 riastrad INIT_LIST_HEAD(>t->oos_page_list_head); 2470 1.1 riastrad INIT_LIST_HEAD(>t->post_shadow_list_head); 2471 1.1 riastrad 2472 1.1 riastrad gtt->ggtt_mm = intel_vgpu_create_ggtt_mm(vgpu); 2473 1.1 riastrad if (IS_ERR(gtt->ggtt_mm)) { 2474 1.1 riastrad gvt_vgpu_err("fail to create mm for ggtt.\n"); 2475 1.1 riastrad return PTR_ERR(gtt->ggtt_mm); 2476 1.1 riastrad } 2477 1.1 riastrad 2478 1.1 riastrad intel_vgpu_reset_ggtt(vgpu, false); 2479 1.1 riastrad 2480 1.1 riastrad INIT_LIST_HEAD(>t->ggtt_mm->ggtt_mm.partial_pte_list); 2481 1.1 riastrad 2482 1.1 riastrad return create_scratch_page_tree(vgpu); 2483 1.1 riastrad } 2484 1.1 riastrad 2485 1.1 riastrad static void intel_vgpu_destroy_all_ppgtt_mm(struct intel_vgpu *vgpu) 2486 1.1 riastrad { 2487 1.1 riastrad struct list_head *pos, *n; 2488 1.1 riastrad struct intel_vgpu_mm *mm; 2489 1.1 riastrad 2490 1.1 riastrad list_for_each_safe(pos, n, &vgpu->gtt.ppgtt_mm_list_head) { 2491 1.1 riastrad mm = container_of(pos, struct intel_vgpu_mm, ppgtt_mm.list); 2492 1.1 riastrad intel_vgpu_destroy_mm(mm); 2493 1.1 riastrad } 2494 1.1 riastrad 2495 1.1 riastrad if (GEM_WARN_ON(!list_empty(&vgpu->gtt.ppgtt_mm_list_head))) 2496 1.1 riastrad gvt_err("vgpu ppgtt mm is not fully destroyed\n"); 2497 1.1 riastrad 2498 1.1 riastrad if (GEM_WARN_ON(!radix_tree_empty(&vgpu->gtt.spt_tree))) { 2499 1.1 riastrad gvt_err("Why we still has spt not freed?\n"); 2500 1.1 riastrad ppgtt_free_all_spt(vgpu); 2501 1.1 riastrad } 2502 1.1 riastrad } 2503 1.1 riastrad 2504 1.1 riastrad static void intel_vgpu_destroy_ggtt_mm(struct intel_vgpu *vgpu) 2505 1.1 riastrad { 2506 1.1 riastrad struct intel_gvt_partial_pte *pos, *next; 2507 1.1 riastrad 2508 1.1 riastrad list_for_each_entry_safe(pos, next, 2509 1.1 riastrad &vgpu->gtt.ggtt_mm->ggtt_mm.partial_pte_list, 2510 1.1 riastrad list) { 2511 1.1 riastrad gvt_dbg_mm("partial PTE update on hold 0x%lx : 0x%llx\n", 2512 1.1 riastrad pos->offset, pos->data); 2513 1.1 riastrad kfree(pos); 2514 1.1 riastrad } 2515 1.1 riastrad intel_vgpu_destroy_mm(vgpu->gtt.ggtt_mm); 2516 1.1 riastrad vgpu->gtt.ggtt_mm = NULL; 2517 1.1 riastrad } 2518 1.1 riastrad 2519 1.1 riastrad /** 2520 1.1 riastrad * intel_vgpu_clean_gtt - clean up per-vGPU graphics memory virulization 2521 1.1 riastrad * @vgpu: a vGPU 2522 1.1 riastrad * 2523 1.1 riastrad * This function is used to clean up per-vGPU graphics memory virtualization 2524 1.1 riastrad * components. 2525 1.1 riastrad * 2526 1.1 riastrad * Returns: 2527 1.1 riastrad * Zero on success, error code if failed. 2528 1.1 riastrad */ 2529 1.1 riastrad void intel_vgpu_clean_gtt(struct intel_vgpu *vgpu) 2530 1.1 riastrad { 2531 1.1 riastrad intel_vgpu_destroy_all_ppgtt_mm(vgpu); 2532 1.1 riastrad intel_vgpu_destroy_ggtt_mm(vgpu); 2533 1.1 riastrad release_scratch_page_tree(vgpu); 2534 1.1 riastrad } 2535 1.1 riastrad 2536 1.1 riastrad static void clean_spt_oos(struct intel_gvt *gvt) 2537 1.1 riastrad { 2538 1.1 riastrad struct intel_gvt_gtt *gtt = &gvt->gtt; 2539 1.1 riastrad struct list_head *pos, *n; 2540 1.1 riastrad struct intel_vgpu_oos_page *oos_page; 2541 1.1 riastrad 2542 1.1 riastrad WARN(!list_empty(>t->oos_page_use_list_head), 2543 1.1 riastrad "someone is still using oos page\n"); 2544 1.1 riastrad 2545 1.1 riastrad list_for_each_safe(pos, n, >t->oos_page_free_list_head) { 2546 1.1 riastrad oos_page = container_of(pos, struct intel_vgpu_oos_page, list); 2547 1.1 riastrad list_del(&oos_page->list); 2548 1.1 riastrad free_page((unsigned long)oos_page->mem); 2549 1.1 riastrad kfree(oos_page); 2550 1.1 riastrad } 2551 1.1 riastrad } 2552 1.1 riastrad 2553 1.1 riastrad static int setup_spt_oos(struct intel_gvt *gvt) 2554 1.1 riastrad { 2555 1.1 riastrad struct intel_gvt_gtt *gtt = &gvt->gtt; 2556 1.1 riastrad struct intel_vgpu_oos_page *oos_page; 2557 1.1 riastrad int i; 2558 1.1 riastrad int ret; 2559 1.1 riastrad 2560 1.1 riastrad INIT_LIST_HEAD(>t->oos_page_free_list_head); 2561 1.1 riastrad INIT_LIST_HEAD(>t->oos_page_use_list_head); 2562 1.1 riastrad 2563 1.1 riastrad for (i = 0; i < preallocated_oos_pages; i++) { 2564 1.1 riastrad oos_page = kzalloc(sizeof(*oos_page), GFP_KERNEL); 2565 1.1 riastrad if (!oos_page) { 2566 1.1 riastrad ret = -ENOMEM; 2567 1.1 riastrad goto fail; 2568 1.1 riastrad } 2569 1.1 riastrad oos_page->mem = (void *)__get_free_pages(GFP_KERNEL, 0); 2570 1.1 riastrad if (!oos_page->mem) { 2571 1.1 riastrad ret = -ENOMEM; 2572 1.1 riastrad kfree(oos_page); 2573 1.1 riastrad goto fail; 2574 1.1 riastrad } 2575 1.1 riastrad 2576 1.1 riastrad INIT_LIST_HEAD(&oos_page->list); 2577 1.1 riastrad INIT_LIST_HEAD(&oos_page->vm_list); 2578 1.1 riastrad oos_page->id = i; 2579 1.1 riastrad list_add_tail(&oos_page->list, >t->oos_page_free_list_head); 2580 1.1 riastrad } 2581 1.1 riastrad 2582 1.1 riastrad gvt_dbg_mm("%d oos pages preallocated\n", i); 2583 1.1 riastrad 2584 1.1 riastrad return 0; 2585 1.1 riastrad fail: 2586 1.1 riastrad clean_spt_oos(gvt); 2587 1.1 riastrad return ret; 2588 1.1 riastrad } 2589 1.1 riastrad 2590 1.1 riastrad /** 2591 1.1 riastrad * intel_vgpu_find_ppgtt_mm - find a PPGTT mm object 2592 1.1 riastrad * @vgpu: a vGPU 2593 1.1 riastrad * @pdps: pdp root array 2594 1.1 riastrad * 2595 1.1 riastrad * This function is used to find a PPGTT mm object from mm object pool 2596 1.1 riastrad * 2597 1.1 riastrad * Returns: 2598 1.1 riastrad * pointer to mm object on success, NULL if failed. 2599 1.1 riastrad */ 2600 1.1 riastrad struct intel_vgpu_mm *intel_vgpu_find_ppgtt_mm(struct intel_vgpu *vgpu, 2601 1.1 riastrad u64 pdps[]) 2602 1.1 riastrad { 2603 1.1 riastrad struct intel_vgpu_mm *mm; 2604 1.1 riastrad struct list_head *pos; 2605 1.1 riastrad 2606 1.1 riastrad list_for_each(pos, &vgpu->gtt.ppgtt_mm_list_head) { 2607 1.1 riastrad mm = container_of(pos, struct intel_vgpu_mm, ppgtt_mm.list); 2608 1.1 riastrad 2609 1.1 riastrad switch (mm->ppgtt_mm.root_entry_type) { 2610 1.1 riastrad case GTT_TYPE_PPGTT_ROOT_L4_ENTRY: 2611 1.1 riastrad if (pdps[0] == mm->ppgtt_mm.guest_pdps[0]) 2612 1.1 riastrad return mm; 2613 1.1 riastrad break; 2614 1.1 riastrad case GTT_TYPE_PPGTT_ROOT_L3_ENTRY: 2615 1.1 riastrad if (!memcmp(pdps, mm->ppgtt_mm.guest_pdps, 2616 1.1 riastrad sizeof(mm->ppgtt_mm.guest_pdps))) 2617 1.1 riastrad return mm; 2618 1.1 riastrad break; 2619 1.1 riastrad default: 2620 1.1 riastrad GEM_BUG_ON(1); 2621 1.1 riastrad } 2622 1.1 riastrad } 2623 1.1 riastrad return NULL; 2624 1.1 riastrad } 2625 1.1 riastrad 2626 1.1 riastrad /** 2627 1.1 riastrad * intel_vgpu_get_ppgtt_mm - get or create a PPGTT mm object. 2628 1.1 riastrad * @vgpu: a vGPU 2629 1.1 riastrad * @root_entry_type: ppgtt root entry type 2630 1.1 riastrad * @pdps: guest pdps 2631 1.1 riastrad * 2632 1.1 riastrad * This function is used to find or create a PPGTT mm object from a guest. 2633 1.1 riastrad * 2634 1.1 riastrad * Returns: 2635 1.1 riastrad * Zero on success, negative error code if failed. 2636 1.1 riastrad */ 2637 1.1 riastrad struct intel_vgpu_mm *intel_vgpu_get_ppgtt_mm(struct intel_vgpu *vgpu, 2638 1.1 riastrad enum intel_gvt_gtt_type root_entry_type, u64 pdps[]) 2639 1.1 riastrad { 2640 1.1 riastrad struct intel_vgpu_mm *mm; 2641 1.1 riastrad 2642 1.1 riastrad mm = intel_vgpu_find_ppgtt_mm(vgpu, pdps); 2643 1.1 riastrad if (mm) { 2644 1.1 riastrad intel_vgpu_mm_get(mm); 2645 1.1 riastrad } else { 2646 1.1 riastrad mm = intel_vgpu_create_ppgtt_mm(vgpu, root_entry_type, pdps); 2647 1.1 riastrad if (IS_ERR(mm)) 2648 1.1 riastrad gvt_vgpu_err("fail to create mm\n"); 2649 1.1 riastrad } 2650 1.1 riastrad return mm; 2651 1.1 riastrad } 2652 1.1 riastrad 2653 1.1 riastrad /** 2654 1.1 riastrad * intel_vgpu_put_ppgtt_mm - find and put a PPGTT mm object. 2655 1.1 riastrad * @vgpu: a vGPU 2656 1.1 riastrad * @pdps: guest pdps 2657 1.1 riastrad * 2658 1.1 riastrad * This function is used to find a PPGTT mm object from a guest and destroy it. 2659 1.1 riastrad * 2660 1.1 riastrad * Returns: 2661 1.1 riastrad * Zero on success, negative error code if failed. 2662 1.1 riastrad */ 2663 1.1 riastrad int intel_vgpu_put_ppgtt_mm(struct intel_vgpu *vgpu, u64 pdps[]) 2664 1.1 riastrad { 2665 1.1 riastrad struct intel_vgpu_mm *mm; 2666 1.1 riastrad 2667 1.1 riastrad mm = intel_vgpu_find_ppgtt_mm(vgpu, pdps); 2668 1.1 riastrad if (!mm) { 2669 1.1 riastrad gvt_vgpu_err("fail to find ppgtt instance.\n"); 2670 1.1 riastrad return -EINVAL; 2671 1.1 riastrad } 2672 1.1 riastrad intel_vgpu_mm_put(mm); 2673 1.1 riastrad return 0; 2674 1.1 riastrad } 2675 1.1 riastrad 2676 1.1 riastrad /** 2677 1.1 riastrad * intel_gvt_init_gtt - initialize mm components of a GVT device 2678 1.1 riastrad * @gvt: GVT device 2679 1.1 riastrad * 2680 1.1 riastrad * This function is called at the initialization stage, to initialize 2681 1.1 riastrad * the mm components of a GVT device. 2682 1.1 riastrad * 2683 1.1 riastrad * Returns: 2684 1.1 riastrad * zero on success, negative error code if failed. 2685 1.1 riastrad */ 2686 1.1 riastrad int intel_gvt_init_gtt(struct intel_gvt *gvt) 2687 1.1 riastrad { 2688 1.1 riastrad int ret; 2689 1.1 riastrad void *page; 2690 1.1 riastrad struct device *dev = &gvt->dev_priv->drm.pdev->dev; 2691 1.1 riastrad dma_addr_t daddr; 2692 1.1 riastrad 2693 1.1 riastrad gvt_dbg_core("init gtt\n"); 2694 1.1 riastrad 2695 1.1 riastrad gvt->gtt.pte_ops = &gen8_gtt_pte_ops; 2696 1.1 riastrad gvt->gtt.gma_ops = &gen8_gtt_gma_ops; 2697 1.1 riastrad 2698 1.1 riastrad page = (void *)get_zeroed_page(GFP_KERNEL); 2699 1.1 riastrad if (!page) { 2700 1.1 riastrad gvt_err("fail to allocate scratch ggtt page\n"); 2701 1.1 riastrad return -ENOMEM; 2702 1.1 riastrad } 2703 1.1 riastrad 2704 1.1 riastrad daddr = dma_map_page(dev, virt_to_page(page), 0, 2705 1.1 riastrad 4096, PCI_DMA_BIDIRECTIONAL); 2706 1.1 riastrad if (dma_mapping_error(dev, daddr)) { 2707 1.1 riastrad gvt_err("fail to dmamap scratch ggtt page\n"); 2708 1.1 riastrad __free_page(virt_to_page(page)); 2709 1.1 riastrad return -ENOMEM; 2710 1.1 riastrad } 2711 1.1 riastrad 2712 1.1 riastrad gvt->gtt.scratch_page = virt_to_page(page); 2713 1.1 riastrad gvt->gtt.scratch_mfn = (unsigned long)(daddr >> I915_GTT_PAGE_SHIFT); 2714 1.1 riastrad 2715 1.1 riastrad if (enable_out_of_sync) { 2716 1.1 riastrad ret = setup_spt_oos(gvt); 2717 1.1 riastrad if (ret) { 2718 1.1 riastrad gvt_err("fail to initialize SPT oos\n"); 2719 1.1 riastrad dma_unmap_page(dev, daddr, 4096, PCI_DMA_BIDIRECTIONAL); 2720 1.1 riastrad __free_page(gvt->gtt.scratch_page); 2721 1.1 riastrad return ret; 2722 1.1 riastrad } 2723 1.1 riastrad } 2724 1.1 riastrad INIT_LIST_HEAD(&gvt->gtt.ppgtt_mm_lru_list_head); 2725 1.1 riastrad mutex_init(&gvt->gtt.ppgtt_mm_lock); 2726 1.1 riastrad return 0; 2727 1.1 riastrad } 2728 1.1 riastrad 2729 1.1 riastrad /** 2730 1.1 riastrad * intel_gvt_clean_gtt - clean up mm components of a GVT device 2731 1.1 riastrad * @gvt: GVT device 2732 1.1 riastrad * 2733 1.1 riastrad * This function is called at the driver unloading stage, to clean up the 2734 1.1 riastrad * the mm components of a GVT device. 2735 1.1 riastrad * 2736 1.1 riastrad */ 2737 1.1 riastrad void intel_gvt_clean_gtt(struct intel_gvt *gvt) 2738 1.1 riastrad { 2739 1.1 riastrad struct device *dev = &gvt->dev_priv->drm.pdev->dev; 2740 1.1 riastrad dma_addr_t daddr = (dma_addr_t)(gvt->gtt.scratch_mfn << 2741 1.1 riastrad I915_GTT_PAGE_SHIFT); 2742 1.1 riastrad 2743 1.1 riastrad dma_unmap_page(dev, daddr, 4096, PCI_DMA_BIDIRECTIONAL); 2744 1.1 riastrad 2745 1.1 riastrad __free_page(gvt->gtt.scratch_page); 2746 1.1 riastrad 2747 1.1 riastrad if (enable_out_of_sync) 2748 1.1 riastrad clean_spt_oos(gvt); 2749 1.1 riastrad } 2750 1.1 riastrad 2751 1.1 riastrad /** 2752 1.1 riastrad * intel_vgpu_invalidate_ppgtt - invalidate PPGTT instances 2753 1.1 riastrad * @vgpu: a vGPU 2754 1.1 riastrad * 2755 1.1 riastrad * This function is called when invalidate all PPGTT instances of a vGPU. 2756 1.1 riastrad * 2757 1.1 riastrad */ 2758 1.1 riastrad void intel_vgpu_invalidate_ppgtt(struct intel_vgpu *vgpu) 2759 1.1 riastrad { 2760 1.1 riastrad struct list_head *pos, *n; 2761 1.1 riastrad struct intel_vgpu_mm *mm; 2762 1.1 riastrad 2763 1.1 riastrad list_for_each_safe(pos, n, &vgpu->gtt.ppgtt_mm_list_head) { 2764 1.1 riastrad mm = container_of(pos, struct intel_vgpu_mm, ppgtt_mm.list); 2765 1.1 riastrad if (mm->type == INTEL_GVT_MM_PPGTT) { 2766 1.1 riastrad mutex_lock(&vgpu->gvt->gtt.ppgtt_mm_lock); 2767 1.1 riastrad list_del_init(&mm->ppgtt_mm.lru_list); 2768 1.1 riastrad mutex_unlock(&vgpu->gvt->gtt.ppgtt_mm_lock); 2769 1.1 riastrad if (mm->ppgtt_mm.shadowed) 2770 1.1 riastrad invalidate_ppgtt_mm(mm); 2771 1.1 riastrad } 2772 1.1 riastrad } 2773 1.1 riastrad } 2774 1.1 riastrad 2775 1.1 riastrad /** 2776 1.1 riastrad * intel_vgpu_reset_ggtt - reset the GGTT entry 2777 1.1 riastrad * @vgpu: a vGPU 2778 1.1 riastrad * @invalidate_old: invalidate old entries 2779 1.1 riastrad * 2780 1.1 riastrad * This function is called at the vGPU create stage 2781 1.1 riastrad * to reset all the GGTT entries. 2782 1.1 riastrad * 2783 1.1 riastrad */ 2784 1.1 riastrad void intel_vgpu_reset_ggtt(struct intel_vgpu *vgpu, bool invalidate_old) 2785 1.1 riastrad { 2786 1.1 riastrad struct intel_gvt *gvt = vgpu->gvt; 2787 1.1 riastrad struct drm_i915_private *dev_priv = gvt->dev_priv; 2788 1.1 riastrad struct intel_gvt_gtt_pte_ops *pte_ops = vgpu->gvt->gtt.pte_ops; 2789 1.1 riastrad struct intel_gvt_gtt_entry entry = {.type = GTT_TYPE_GGTT_PTE}; 2790 1.1 riastrad struct intel_gvt_gtt_entry old_entry; 2791 1.1 riastrad u32 index; 2792 1.1 riastrad u32 num_entries; 2793 1.1 riastrad 2794 1.1 riastrad pte_ops->set_pfn(&entry, gvt->gtt.scratch_mfn); 2795 1.1 riastrad pte_ops->set_present(&entry); 2796 1.1 riastrad 2797 1.1 riastrad index = vgpu_aperture_gmadr_base(vgpu) >> PAGE_SHIFT; 2798 1.1 riastrad num_entries = vgpu_aperture_sz(vgpu) >> PAGE_SHIFT; 2799 1.1 riastrad while (num_entries--) { 2800 1.1 riastrad if (invalidate_old) { 2801 1.1 riastrad ggtt_get_host_entry(vgpu->gtt.ggtt_mm, &old_entry, index); 2802 1.1 riastrad ggtt_invalidate_pte(vgpu, &old_entry); 2803 1.1 riastrad } 2804 1.1 riastrad ggtt_set_host_entry(vgpu->gtt.ggtt_mm, &entry, index++); 2805 1.1 riastrad } 2806 1.1 riastrad 2807 1.1 riastrad index = vgpu_hidden_gmadr_base(vgpu) >> PAGE_SHIFT; 2808 1.1 riastrad num_entries = vgpu_hidden_sz(vgpu) >> PAGE_SHIFT; 2809 1.1 riastrad while (num_entries--) { 2810 1.1 riastrad if (invalidate_old) { 2811 1.1 riastrad ggtt_get_host_entry(vgpu->gtt.ggtt_mm, &old_entry, index); 2812 1.1 riastrad ggtt_invalidate_pte(vgpu, &old_entry); 2813 1.1 riastrad } 2814 1.1 riastrad ggtt_set_host_entry(vgpu->gtt.ggtt_mm, &entry, index++); 2815 1.1 riastrad } 2816 1.1 riastrad 2817 1.1 riastrad ggtt_invalidate(dev_priv); 2818 1.1 riastrad } 2819 1.1 riastrad 2820 1.1 riastrad /** 2821 1.1 riastrad * intel_vgpu_reset_gtt - reset the all GTT related status 2822 1.1 riastrad * @vgpu: a vGPU 2823 1.1 riastrad * 2824 1.1 riastrad * This function is called from vfio core to reset reset all 2825 1.1 riastrad * GTT related status, including GGTT, PPGTT, scratch page. 2826 1.1 riastrad * 2827 1.1 riastrad */ 2828 1.1 riastrad void intel_vgpu_reset_gtt(struct intel_vgpu *vgpu) 2829 1.1 riastrad { 2830 1.1 riastrad /* Shadow pages are only created when there is no page 2831 1.1 riastrad * table tracking data, so remove page tracking data after 2832 1.1 riastrad * removing the shadow pages. 2833 1.1 riastrad */ 2834 1.1 riastrad intel_vgpu_destroy_all_ppgtt_mm(vgpu); 2835 1.1 riastrad intel_vgpu_reset_ggtt(vgpu, true); 2836 1.1 riastrad } 2837