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