mmio.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. *
  23. * Authors:
  24. * Ke Yu
  25. * Kevin Tian <[email protected]>
  26. * Dexuan Cui
  27. *
  28. * Contributors:
  29. * Tina Zhang <[email protected]>
  30. * Min He <[email protected]>
  31. * Niu Bing <[email protected]>
  32. * Zhi Wang <[email protected]>
  33. *
  34. */
  35. #include "i915_drv.h"
  36. #include "i915_reg.h"
  37. #include "gvt.h"
  38. #include "gt/intel_gt_regs.h"
  39. /**
  40. * intel_vgpu_gpa_to_mmio_offset - translate a GPA to MMIO offset
  41. * @vgpu: a vGPU
  42. * @gpa: guest physical address
  43. *
  44. * Returns:
  45. * Zero on success, negative error code if failed
  46. */
  47. int intel_vgpu_gpa_to_mmio_offset(struct intel_vgpu *vgpu, u64 gpa)
  48. {
  49. u64 gttmmio_gpa = intel_vgpu_get_bar_gpa(vgpu, PCI_BASE_ADDRESS_0);
  50. return gpa - gttmmio_gpa;
  51. }
  52. #define reg_is_mmio(gvt, reg) \
  53. (reg >= 0 && reg < gvt->device_info.mmio_size)
  54. #define reg_is_gtt(gvt, reg) \
  55. (reg >= gvt->device_info.gtt_start_offset \
  56. && reg < gvt->device_info.gtt_start_offset + gvt_ggtt_sz(gvt))
  57. static void failsafe_emulate_mmio_rw(struct intel_vgpu *vgpu, u64 pa,
  58. void *p_data, unsigned int bytes, bool read)
  59. {
  60. struct intel_gvt *gvt = NULL;
  61. void *pt = NULL;
  62. unsigned int offset = 0;
  63. if (!vgpu || !p_data)
  64. return;
  65. gvt = vgpu->gvt;
  66. mutex_lock(&vgpu->vgpu_lock);
  67. offset = intel_vgpu_gpa_to_mmio_offset(vgpu, pa);
  68. if (reg_is_mmio(gvt, offset)) {
  69. if (read)
  70. intel_vgpu_default_mmio_read(vgpu, offset, p_data,
  71. bytes);
  72. else
  73. intel_vgpu_default_mmio_write(vgpu, offset, p_data,
  74. bytes);
  75. } else if (reg_is_gtt(gvt, offset)) {
  76. offset -= gvt->device_info.gtt_start_offset;
  77. pt = vgpu->gtt.ggtt_mm->ggtt_mm.virtual_ggtt + offset;
  78. if (read)
  79. memcpy(p_data, pt, bytes);
  80. else
  81. memcpy(pt, p_data, bytes);
  82. }
  83. mutex_unlock(&vgpu->vgpu_lock);
  84. }
  85. /**
  86. * intel_vgpu_emulate_mmio_read - emulate MMIO read
  87. * @vgpu: a vGPU
  88. * @pa: guest physical address
  89. * @p_data: data return buffer
  90. * @bytes: access data length
  91. *
  92. * Returns:
  93. * Zero on success, negative error code if failed
  94. */
  95. int intel_vgpu_emulate_mmio_read(struct intel_vgpu *vgpu, u64 pa,
  96. void *p_data, unsigned int bytes)
  97. {
  98. struct intel_gvt *gvt = vgpu->gvt;
  99. struct drm_i915_private *i915 = gvt->gt->i915;
  100. unsigned int offset = 0;
  101. int ret = -EINVAL;
  102. if (vgpu->failsafe) {
  103. failsafe_emulate_mmio_rw(vgpu, pa, p_data, bytes, true);
  104. return 0;
  105. }
  106. mutex_lock(&vgpu->vgpu_lock);
  107. offset = intel_vgpu_gpa_to_mmio_offset(vgpu, pa);
  108. if (drm_WARN_ON(&i915->drm, bytes > 8))
  109. goto err;
  110. if (reg_is_gtt(gvt, offset)) {
  111. if (drm_WARN_ON(&i915->drm, !IS_ALIGNED(offset, 4) &&
  112. !IS_ALIGNED(offset, 8)))
  113. goto err;
  114. if (drm_WARN_ON(&i915->drm, bytes != 4 && bytes != 8))
  115. goto err;
  116. if (drm_WARN_ON(&i915->drm,
  117. !reg_is_gtt(gvt, offset + bytes - 1)))
  118. goto err;
  119. ret = intel_vgpu_emulate_ggtt_mmio_read(vgpu, offset,
  120. p_data, bytes);
  121. if (ret)
  122. goto err;
  123. goto out;
  124. }
  125. if (drm_WARN_ON_ONCE(&i915->drm, !reg_is_mmio(gvt, offset))) {
  126. ret = intel_gvt_read_gpa(vgpu, pa, p_data, bytes);
  127. goto out;
  128. }
  129. if (drm_WARN_ON(&i915->drm, !reg_is_mmio(gvt, offset + bytes - 1)))
  130. goto err;
  131. if (!intel_gvt_mmio_is_unalign(gvt, offset)) {
  132. if (drm_WARN_ON(&i915->drm, !IS_ALIGNED(offset, bytes)))
  133. goto err;
  134. }
  135. ret = intel_vgpu_mmio_reg_rw(vgpu, offset, p_data, bytes, true);
  136. if (ret < 0)
  137. goto err;
  138. intel_gvt_mmio_set_accessed(gvt, offset);
  139. ret = 0;
  140. goto out;
  141. err:
  142. gvt_vgpu_err("fail to emulate MMIO read %08x len %d\n",
  143. offset, bytes);
  144. out:
  145. mutex_unlock(&vgpu->vgpu_lock);
  146. return ret;
  147. }
  148. /**
  149. * intel_vgpu_emulate_mmio_write - emulate MMIO write
  150. * @vgpu: a vGPU
  151. * @pa: guest physical address
  152. * @p_data: write data buffer
  153. * @bytes: access data length
  154. *
  155. * Returns:
  156. * Zero on success, negative error code if failed
  157. */
  158. int intel_vgpu_emulate_mmio_write(struct intel_vgpu *vgpu, u64 pa,
  159. void *p_data, unsigned int bytes)
  160. {
  161. struct intel_gvt *gvt = vgpu->gvt;
  162. struct drm_i915_private *i915 = gvt->gt->i915;
  163. unsigned int offset = 0;
  164. int ret = -EINVAL;
  165. if (vgpu->failsafe) {
  166. failsafe_emulate_mmio_rw(vgpu, pa, p_data, bytes, false);
  167. return 0;
  168. }
  169. mutex_lock(&vgpu->vgpu_lock);
  170. offset = intel_vgpu_gpa_to_mmio_offset(vgpu, pa);
  171. if (drm_WARN_ON(&i915->drm, bytes > 8))
  172. goto err;
  173. if (reg_is_gtt(gvt, offset)) {
  174. if (drm_WARN_ON(&i915->drm, !IS_ALIGNED(offset, 4) &&
  175. !IS_ALIGNED(offset, 8)))
  176. goto err;
  177. if (drm_WARN_ON(&i915->drm, bytes != 4 && bytes != 8))
  178. goto err;
  179. if (drm_WARN_ON(&i915->drm,
  180. !reg_is_gtt(gvt, offset + bytes - 1)))
  181. goto err;
  182. ret = intel_vgpu_emulate_ggtt_mmio_write(vgpu, offset,
  183. p_data, bytes);
  184. if (ret)
  185. goto err;
  186. goto out;
  187. }
  188. if (drm_WARN_ON_ONCE(&i915->drm, !reg_is_mmio(gvt, offset))) {
  189. ret = intel_gvt_write_gpa(vgpu, pa, p_data, bytes);
  190. goto out;
  191. }
  192. ret = intel_vgpu_mmio_reg_rw(vgpu, offset, p_data, bytes, false);
  193. if (ret < 0)
  194. goto err;
  195. intel_gvt_mmio_set_accessed(gvt, offset);
  196. ret = 0;
  197. goto out;
  198. err:
  199. gvt_vgpu_err("fail to emulate MMIO write %08x len %d\n", offset,
  200. bytes);
  201. out:
  202. mutex_unlock(&vgpu->vgpu_lock);
  203. return ret;
  204. }
  205. /**
  206. * intel_vgpu_reset_mmio - reset virtual MMIO space
  207. * @vgpu: a vGPU
  208. * @dmlr: whether this is device model level reset
  209. */
  210. void intel_vgpu_reset_mmio(struct intel_vgpu *vgpu, bool dmlr)
  211. {
  212. struct intel_gvt *gvt = vgpu->gvt;
  213. const struct intel_gvt_device_info *info = &gvt->device_info;
  214. void *mmio = gvt->firmware.mmio;
  215. if (dmlr) {
  216. memcpy(vgpu->mmio.vreg, mmio, info->mmio_size);
  217. vgpu_vreg_t(vgpu, GEN6_GT_THREAD_STATUS_REG) = 0;
  218. /* set the bit 0:2(Core C-State ) to C0 */
  219. vgpu_vreg_t(vgpu, GEN6_GT_CORE_STATUS) = 0;
  220. /* uc reset hw expect GS_MIA_IN_RESET */
  221. vgpu_vreg_t(vgpu, GUC_STATUS) |= GS_MIA_IN_RESET;
  222. if (IS_BROXTON(vgpu->gvt->gt->i915)) {
  223. vgpu_vreg_t(vgpu, BXT_P_CR_GT_DISP_PWRON) &=
  224. ~(BIT(0) | BIT(1));
  225. vgpu_vreg_t(vgpu, BXT_PORT_CL1CM_DW0(DPIO_PHY0)) &=
  226. ~PHY_POWER_GOOD;
  227. vgpu_vreg_t(vgpu, BXT_PORT_CL1CM_DW0(DPIO_PHY1)) &=
  228. ~PHY_POWER_GOOD;
  229. vgpu_vreg_t(vgpu, BXT_PHY_CTL_FAMILY(DPIO_PHY0)) &=
  230. ~BIT(30);
  231. vgpu_vreg_t(vgpu, BXT_PHY_CTL_FAMILY(DPIO_PHY1)) &=
  232. ~BIT(30);
  233. vgpu_vreg_t(vgpu, BXT_PHY_CTL(PORT_A)) &=
  234. ~BXT_PHY_LANE_ENABLED;
  235. vgpu_vreg_t(vgpu, BXT_PHY_CTL(PORT_A)) |=
  236. BXT_PHY_CMNLANE_POWERDOWN_ACK |
  237. BXT_PHY_LANE_POWERDOWN_ACK;
  238. vgpu_vreg_t(vgpu, BXT_PHY_CTL(PORT_B)) &=
  239. ~BXT_PHY_LANE_ENABLED;
  240. vgpu_vreg_t(vgpu, BXT_PHY_CTL(PORT_B)) |=
  241. BXT_PHY_CMNLANE_POWERDOWN_ACK |
  242. BXT_PHY_LANE_POWERDOWN_ACK;
  243. vgpu_vreg_t(vgpu, BXT_PHY_CTL(PORT_C)) &=
  244. ~BXT_PHY_LANE_ENABLED;
  245. vgpu_vreg_t(vgpu, BXT_PHY_CTL(PORT_C)) |=
  246. BXT_PHY_CMNLANE_POWERDOWN_ACK |
  247. BXT_PHY_LANE_POWERDOWN_ACK;
  248. vgpu_vreg_t(vgpu, SKL_FUSE_STATUS) |=
  249. SKL_FUSE_DOWNLOAD_STATUS |
  250. SKL_FUSE_PG_DIST_STATUS(SKL_PG0) |
  251. SKL_FUSE_PG_DIST_STATUS(SKL_PG1) |
  252. SKL_FUSE_PG_DIST_STATUS(SKL_PG2);
  253. }
  254. } else {
  255. #define GVT_GEN8_MMIO_RESET_OFFSET (0x44200)
  256. /* only reset the engine related, so starting with 0x44200
  257. * interrupt include DE,display mmio related will not be
  258. * touched
  259. */
  260. memcpy(vgpu->mmio.vreg, mmio, GVT_GEN8_MMIO_RESET_OFFSET);
  261. }
  262. }
  263. /**
  264. * intel_vgpu_init_mmio - init MMIO space
  265. * @vgpu: a vGPU
  266. *
  267. * Returns:
  268. * Zero on success, negative error code if failed
  269. */
  270. int intel_vgpu_init_mmio(struct intel_vgpu *vgpu)
  271. {
  272. const struct intel_gvt_device_info *info = &vgpu->gvt->device_info;
  273. vgpu->mmio.vreg = vzalloc(info->mmio_size);
  274. if (!vgpu->mmio.vreg)
  275. return -ENOMEM;
  276. intel_vgpu_reset_mmio(vgpu, true);
  277. return 0;
  278. }
  279. /**
  280. * intel_vgpu_clean_mmio - clean MMIO space
  281. * @vgpu: a vGPU
  282. *
  283. */
  284. void intel_vgpu_clean_mmio(struct intel_vgpu *vgpu)
  285. {
  286. vfree(vgpu->mmio.vreg);
  287. vgpu->mmio.vreg = NULL;
  288. }