2 * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * Kevin Tian <kevin.tian@intel.com>
29 * Tina Zhang <tina.zhang@intel.com>
30 * Min He <min.he@intel.com>
31 * Niu Bing <bing.niu@intel.com>
32 * Zhi Wang <zhi.a.wang@intel.com>
40 * intel_vgpu_gpa_to_mmio_offset - translate a GPA to MMIO offset
42 * @gpa: guest physical address
45 * Zero on success, negative error code if failed
47 int intel_vgpu_gpa_to_mmio_offset(struct intel_vgpu
*vgpu
, u64 gpa
)
49 u64 gttmmio_gpa
= intel_vgpu_get_bar_gpa(vgpu
, PCI_BASE_ADDRESS_0
);
50 return gpa
- gttmmio_gpa
;
53 #define reg_is_mmio(gvt, reg) \
54 (reg >= 0 && reg < gvt->device_info.mmio_size)
56 #define reg_is_gtt(gvt, reg) \
57 (reg >= gvt->device_info.gtt_start_offset \
58 && reg < gvt->device_info.gtt_start_offset + gvt_ggtt_sz(gvt))
60 static void failsafe_emulate_mmio_rw(struct intel_vgpu
*vgpu
, u64 pa
,
61 void *p_data
, unsigned int bytes
, bool read
)
63 struct intel_gvt
*gvt
= NULL
;
65 unsigned int offset
= 0;
71 mutex_lock(&vgpu
->vgpu_lock
);
72 offset
= intel_vgpu_gpa_to_mmio_offset(vgpu
, pa
);
73 if (reg_is_mmio(gvt
, offset
)) {
75 intel_vgpu_default_mmio_read(vgpu
, offset
, p_data
,
78 intel_vgpu_default_mmio_write(vgpu
, offset
, p_data
,
80 } else if (reg_is_gtt(gvt
, offset
)) {
81 offset
-= gvt
->device_info
.gtt_start_offset
;
82 pt
= vgpu
->gtt
.ggtt_mm
->ggtt_mm
.virtual_ggtt
+ offset
;
84 memcpy(p_data
, pt
, bytes
);
86 memcpy(pt
, p_data
, bytes
);
89 mutex_unlock(&vgpu
->vgpu_lock
);
93 * intel_vgpu_emulate_mmio_read - emulate MMIO read
95 * @pa: guest physical address
96 * @p_data: data return buffer
97 * @bytes: access data length
100 * Zero on success, negative error code if failed
102 int intel_vgpu_emulate_mmio_read(struct intel_vgpu
*vgpu
, u64 pa
,
103 void *p_data
, unsigned int bytes
)
105 struct intel_gvt
*gvt
= vgpu
->gvt
;
106 struct drm_i915_private
*i915
= gvt
->gt
->i915
;
107 unsigned int offset
= 0;
110 if (vgpu
->failsafe
) {
111 failsafe_emulate_mmio_rw(vgpu
, pa
, p_data
, bytes
, true);
114 mutex_lock(&vgpu
->vgpu_lock
);
116 offset
= intel_vgpu_gpa_to_mmio_offset(vgpu
, pa
);
118 if (drm_WARN_ON(&i915
->drm
, bytes
> 8))
121 if (reg_is_gtt(gvt
, offset
)) {
122 if (drm_WARN_ON(&i915
->drm
, !IS_ALIGNED(offset
, 4) &&
123 !IS_ALIGNED(offset
, 8)))
125 if (drm_WARN_ON(&i915
->drm
, bytes
!= 4 && bytes
!= 8))
127 if (drm_WARN_ON(&i915
->drm
,
128 !reg_is_gtt(gvt
, offset
+ bytes
- 1)))
131 ret
= intel_vgpu_emulate_ggtt_mmio_read(vgpu
, offset
,
138 if (drm_WARN_ON_ONCE(&i915
->drm
, !reg_is_mmio(gvt
, offset
))) {
139 ret
= intel_gvt_hypervisor_read_gpa(vgpu
, pa
, p_data
, bytes
);
143 if (drm_WARN_ON(&i915
->drm
, !reg_is_mmio(gvt
, offset
+ bytes
- 1)))
146 if (!intel_gvt_mmio_is_unalign(gvt
, offset
)) {
147 if (drm_WARN_ON(&i915
->drm
, !IS_ALIGNED(offset
, bytes
)))
151 ret
= intel_vgpu_mmio_reg_rw(vgpu
, offset
, p_data
, bytes
, true);
155 intel_gvt_mmio_set_accessed(gvt
, offset
);
160 gvt_vgpu_err("fail to emulate MMIO read %08x len %d\n",
163 mutex_unlock(&vgpu
->vgpu_lock
);
168 * intel_vgpu_emulate_mmio_write - emulate MMIO write
170 * @pa: guest physical address
171 * @p_data: write data buffer
172 * @bytes: access data length
175 * Zero on success, negative error code if failed
177 int intel_vgpu_emulate_mmio_write(struct intel_vgpu
*vgpu
, u64 pa
,
178 void *p_data
, unsigned int bytes
)
180 struct intel_gvt
*gvt
= vgpu
->gvt
;
181 struct drm_i915_private
*i915
= gvt
->gt
->i915
;
182 unsigned int offset
= 0;
185 if (vgpu
->failsafe
) {
186 failsafe_emulate_mmio_rw(vgpu
, pa
, p_data
, bytes
, false);
190 mutex_lock(&vgpu
->vgpu_lock
);
192 offset
= intel_vgpu_gpa_to_mmio_offset(vgpu
, pa
);
194 if (drm_WARN_ON(&i915
->drm
, bytes
> 8))
197 if (reg_is_gtt(gvt
, offset
)) {
198 if (drm_WARN_ON(&i915
->drm
, !IS_ALIGNED(offset
, 4) &&
199 !IS_ALIGNED(offset
, 8)))
201 if (drm_WARN_ON(&i915
->drm
, bytes
!= 4 && bytes
!= 8))
203 if (drm_WARN_ON(&i915
->drm
,
204 !reg_is_gtt(gvt
, offset
+ bytes
- 1)))
207 ret
= intel_vgpu_emulate_ggtt_mmio_write(vgpu
, offset
,
214 if (drm_WARN_ON_ONCE(&i915
->drm
, !reg_is_mmio(gvt
, offset
))) {
215 ret
= intel_gvt_hypervisor_write_gpa(vgpu
, pa
, p_data
, bytes
);
219 ret
= intel_vgpu_mmio_reg_rw(vgpu
, offset
, p_data
, bytes
, false);
223 intel_gvt_mmio_set_accessed(gvt
, offset
);
227 gvt_vgpu_err("fail to emulate MMIO write %08x len %d\n", offset
,
230 mutex_unlock(&vgpu
->vgpu_lock
);
236 * intel_vgpu_reset_mmio - reset virtual MMIO space
238 * @dmlr: whether this is device model level reset
240 void intel_vgpu_reset_mmio(struct intel_vgpu
*vgpu
, bool dmlr
)
242 struct intel_gvt
*gvt
= vgpu
->gvt
;
243 const struct intel_gvt_device_info
*info
= &gvt
->device_info
;
244 void *mmio
= gvt
->firmware
.mmio
;
247 memcpy(vgpu
->mmio
.vreg
, mmio
, info
->mmio_size
);
249 vgpu_vreg_t(vgpu
, GEN6_GT_THREAD_STATUS_REG
) = 0;
251 /* set the bit 0:2(Core C-State ) to C0 */
252 vgpu_vreg_t(vgpu
, GEN6_GT_CORE_STATUS
) = 0;
254 /* uc reset hw expect GS_MIA_IN_RESET */
255 vgpu_vreg_t(vgpu
, GUC_STATUS
) |= GS_MIA_IN_RESET
;
257 if (IS_BROXTON(vgpu
->gvt
->gt
->i915
)) {
258 vgpu_vreg_t(vgpu
, BXT_P_CR_GT_DISP_PWRON
) &=
260 vgpu_vreg_t(vgpu
, BXT_PORT_CL1CM_DW0(DPIO_PHY0
)) &=
262 vgpu_vreg_t(vgpu
, BXT_PORT_CL1CM_DW0(DPIO_PHY1
)) &=
264 vgpu_vreg_t(vgpu
, BXT_PHY_CTL_FAMILY(DPIO_PHY0
)) &=
266 vgpu_vreg_t(vgpu
, BXT_PHY_CTL_FAMILY(DPIO_PHY1
)) &=
268 vgpu_vreg_t(vgpu
, BXT_PHY_CTL(PORT_A
)) &=
269 ~BXT_PHY_LANE_ENABLED
;
270 vgpu_vreg_t(vgpu
, BXT_PHY_CTL(PORT_A
)) |=
271 BXT_PHY_CMNLANE_POWERDOWN_ACK
|
272 BXT_PHY_LANE_POWERDOWN_ACK
;
273 vgpu_vreg_t(vgpu
, BXT_PHY_CTL(PORT_B
)) &=
274 ~BXT_PHY_LANE_ENABLED
;
275 vgpu_vreg_t(vgpu
, BXT_PHY_CTL(PORT_B
)) |=
276 BXT_PHY_CMNLANE_POWERDOWN_ACK
|
277 BXT_PHY_LANE_POWERDOWN_ACK
;
278 vgpu_vreg_t(vgpu
, BXT_PHY_CTL(PORT_C
)) &=
279 ~BXT_PHY_LANE_ENABLED
;
280 vgpu_vreg_t(vgpu
, BXT_PHY_CTL(PORT_C
)) |=
281 BXT_PHY_CMNLANE_POWERDOWN_ACK
|
282 BXT_PHY_LANE_POWERDOWN_ACK
;
283 vgpu_vreg_t(vgpu
, SKL_FUSE_STATUS
) |=
284 SKL_FUSE_DOWNLOAD_STATUS
|
285 SKL_FUSE_PG_DIST_STATUS(SKL_PG0
) |
286 SKL_FUSE_PG_DIST_STATUS(SKL_PG1
) |
287 SKL_FUSE_PG_DIST_STATUS(SKL_PG2
);
290 #define GVT_GEN8_MMIO_RESET_OFFSET (0x44200)
291 /* only reset the engine related, so starting with 0x44200
292 * interrupt include DE,display mmio related will not be
295 memcpy(vgpu
->mmio
.vreg
, mmio
, GVT_GEN8_MMIO_RESET_OFFSET
);
301 * intel_vgpu_init_mmio - init MMIO space
305 * Zero on success, negative error code if failed
307 int intel_vgpu_init_mmio(struct intel_vgpu
*vgpu
)
309 const struct intel_gvt_device_info
*info
= &vgpu
->gvt
->device_info
;
311 vgpu
->mmio
.vreg
= vzalloc(info
->mmio_size
);
312 if (!vgpu
->mmio
.vreg
)
315 intel_vgpu_reset_mmio(vgpu
, true);
321 * intel_vgpu_clean_mmio - clean MMIO space
325 void intel_vgpu_clean_mmio(struct intel_vgpu
*vgpu
)
327 vfree(vgpu
->mmio
.vreg
);
328 vgpu
->mmio
.vreg
= NULL
;