mmio.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 <kevin.tian@intel.com>
  26. * Dexuan Cui
  27. *
  28. * Contributors:
  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>
  33. *
  34. */
  35. #include "i915_drv.h"
  36. #include "gvt.h"
  37. /**
  38. * intel_vgpu_gpa_to_mmio_offset - translate a GPA to MMIO offset
  39. * @vgpu: a vGPU
  40. *
  41. * Returns:
  42. * Zero on success, negative error code if failed
  43. */
  44. int intel_vgpu_gpa_to_mmio_offset(struct intel_vgpu *vgpu, u64 gpa)
  45. {
  46. u64 gttmmio_gpa = intel_vgpu_get_bar_gpa(vgpu, PCI_BASE_ADDRESS_0);
  47. return gpa - gttmmio_gpa;
  48. }
  49. #define reg_is_mmio(gvt, reg) \
  50. (reg >= 0 && reg < gvt->device_info.mmio_size)
  51. #define reg_is_gtt(gvt, reg) \
  52. (reg >= gvt->device_info.gtt_start_offset \
  53. && reg < gvt->device_info.gtt_start_offset + gvt_ggtt_sz(gvt))
  54. static void failsafe_emulate_mmio_rw(struct intel_vgpu *vgpu, uint64_t pa,
  55. void *p_data, unsigned int bytes, bool read)
  56. {
  57. struct intel_gvt *gvt = NULL;
  58. void *pt = NULL;
  59. unsigned int offset = 0;
  60. if (!vgpu || !p_data)
  61. return;
  62. gvt = vgpu->gvt;
  63. mutex_lock(&gvt->lock);
  64. offset = intel_vgpu_gpa_to_mmio_offset(vgpu, pa);
  65. if (reg_is_mmio(gvt, offset)) {
  66. if (read)
  67. intel_vgpu_default_mmio_read(vgpu, offset, p_data,
  68. bytes);
  69. else
  70. intel_vgpu_default_mmio_write(vgpu, offset, p_data,
  71. bytes);
  72. } else if (reg_is_gtt(gvt, offset)) {
  73. offset -= gvt->device_info.gtt_start_offset;
  74. pt = vgpu->gtt.ggtt_mm->ggtt_mm.virtual_ggtt + offset;
  75. if (read)
  76. memcpy(p_data, pt, bytes);
  77. else
  78. memcpy(pt, p_data, bytes);
  79. }
  80. mutex_unlock(&gvt->lock);
  81. }
  82. /**
  83. * intel_vgpu_emulate_mmio_read - emulate MMIO read
  84. * @vgpu: a vGPU
  85. * @pa: guest physical address
  86. * @p_data: data return buffer
  87. * @bytes: access data length
  88. *
  89. * Returns:
  90. * Zero on success, negative error code if failed
  91. */
  92. int intel_vgpu_emulate_mmio_read(struct intel_vgpu *vgpu, uint64_t pa,
  93. void *p_data, unsigned int bytes)
  94. {
  95. struct intel_gvt *gvt = vgpu->gvt;
  96. unsigned int offset = 0;
  97. int ret = -EINVAL;
  98. if (vgpu->failsafe) {
  99. failsafe_emulate_mmio_rw(vgpu, pa, p_data, bytes, true);
  100. return 0;
  101. }
  102. mutex_lock(&gvt->lock);
  103. offset = intel_vgpu_gpa_to_mmio_offset(vgpu, pa);
  104. if (WARN_ON(bytes > 8))
  105. goto err;
  106. if (reg_is_gtt(gvt, offset)) {
  107. if (WARN_ON(!IS_ALIGNED(offset, 4) && !IS_ALIGNED(offset, 8)))
  108. goto err;
  109. if (WARN_ON(bytes != 4 && bytes != 8))
  110. goto err;
  111. if (WARN_ON(!reg_is_gtt(gvt, offset + bytes - 1)))
  112. goto err;
  113. ret = intel_vgpu_emulate_ggtt_mmio_read(vgpu, offset,
  114. p_data, bytes);
  115. if (ret)
  116. goto err;
  117. goto out;
  118. }
  119. if (WARN_ON_ONCE(!reg_is_mmio(gvt, offset))) {
  120. ret = intel_gvt_hypervisor_read_gpa(vgpu, pa, p_data, bytes);
  121. goto out;
  122. }
  123. if (WARN_ON(!reg_is_mmio(gvt, offset + bytes - 1)))
  124. goto err;
  125. if (!intel_gvt_mmio_is_unalign(gvt, offset)) {
  126. if (WARN_ON(!IS_ALIGNED(offset, bytes)))
  127. goto err;
  128. }
  129. ret = intel_vgpu_mmio_reg_rw(vgpu, offset, p_data, bytes, true);
  130. if (ret < 0)
  131. goto err;
  132. intel_gvt_mmio_set_accessed(gvt, offset);
  133. ret = 0;
  134. goto out;
  135. err:
  136. gvt_vgpu_err("fail to emulate MMIO read %08x len %d\n",
  137. offset, bytes);
  138. out:
  139. mutex_unlock(&gvt->lock);
  140. return ret;
  141. }
  142. /**
  143. * intel_vgpu_emulate_mmio_write - emulate MMIO write
  144. * @vgpu: a vGPU
  145. * @pa: guest physical address
  146. * @p_data: write data buffer
  147. * @bytes: access data length
  148. *
  149. * Returns:
  150. * Zero on success, negative error code if failed
  151. */
  152. int intel_vgpu_emulate_mmio_write(struct intel_vgpu *vgpu, uint64_t pa,
  153. void *p_data, unsigned int bytes)
  154. {
  155. struct intel_gvt *gvt = vgpu->gvt;
  156. unsigned int offset = 0;
  157. int ret = -EINVAL;
  158. if (vgpu->failsafe) {
  159. failsafe_emulate_mmio_rw(vgpu, pa, p_data, bytes, false);
  160. return 0;
  161. }
  162. mutex_lock(&gvt->lock);
  163. offset = intel_vgpu_gpa_to_mmio_offset(vgpu, pa);
  164. if (WARN_ON(bytes > 8))
  165. goto err;
  166. if (reg_is_gtt(gvt, offset)) {
  167. if (WARN_ON(!IS_ALIGNED(offset, 4) && !IS_ALIGNED(offset, 8)))
  168. goto err;
  169. if (WARN_ON(bytes != 4 && bytes != 8))
  170. goto err;
  171. if (WARN_ON(!reg_is_gtt(gvt, offset + bytes - 1)))
  172. goto err;
  173. ret = intel_vgpu_emulate_ggtt_mmio_write(vgpu, offset,
  174. p_data, bytes);
  175. if (ret)
  176. goto err;
  177. goto out;
  178. }
  179. if (WARN_ON_ONCE(!reg_is_mmio(gvt, offset))) {
  180. ret = intel_gvt_hypervisor_write_gpa(vgpu, pa, p_data, bytes);
  181. goto out;
  182. }
  183. ret = intel_vgpu_mmio_reg_rw(vgpu, offset, p_data, bytes, false);
  184. if (ret < 0)
  185. goto err;
  186. intel_gvt_mmio_set_accessed(gvt, offset);
  187. ret = 0;
  188. goto out;
  189. err:
  190. gvt_vgpu_err("fail to emulate MMIO write %08x len %d\n", offset,
  191. bytes);
  192. out:
  193. mutex_unlock(&gvt->lock);
  194. return ret;
  195. }
  196. /**
  197. * intel_vgpu_reset_mmio - reset virtual MMIO space
  198. * @vgpu: a vGPU
  199. *
  200. */
  201. void intel_vgpu_reset_mmio(struct intel_vgpu *vgpu, bool dmlr)
  202. {
  203. struct intel_gvt *gvt = vgpu->gvt;
  204. const struct intel_gvt_device_info *info = &gvt->device_info;
  205. void *mmio = gvt->firmware.mmio;
  206. if (dmlr) {
  207. memcpy(vgpu->mmio.vreg, mmio, info->mmio_size);
  208. memcpy(vgpu->mmio.sreg, mmio, info->mmio_size);
  209. vgpu_vreg_t(vgpu, GEN6_GT_THREAD_STATUS_REG) = 0;
  210. /* set the bit 0:2(Core C-State ) to C0 */
  211. vgpu_vreg_t(vgpu, GEN6_GT_CORE_STATUS) = 0;
  212. } else {
  213. #define GVT_GEN8_MMIO_RESET_OFFSET (0x44200)
  214. /* only reset the engine related, so starting with 0x44200
  215. * interrupt include DE,display mmio related will not be
  216. * touched
  217. */
  218. memcpy(vgpu->mmio.vreg, mmio, GVT_GEN8_MMIO_RESET_OFFSET);
  219. memcpy(vgpu->mmio.sreg, mmio, GVT_GEN8_MMIO_RESET_OFFSET);
  220. }
  221. }
  222. /**
  223. * intel_vgpu_init_mmio - init MMIO space
  224. * @vgpu: a vGPU
  225. *
  226. * Returns:
  227. * Zero on success, negative error code if failed
  228. */
  229. int intel_vgpu_init_mmio(struct intel_vgpu *vgpu)
  230. {
  231. const struct intel_gvt_device_info *info = &vgpu->gvt->device_info;
  232. vgpu->mmio.vreg = vzalloc(array_size(info->mmio_size, 2));
  233. if (!vgpu->mmio.vreg)
  234. return -ENOMEM;
  235. vgpu->mmio.sreg = vgpu->mmio.vreg + info->mmio_size;
  236. intel_vgpu_reset_mmio(vgpu, true);
  237. return 0;
  238. }
  239. /**
  240. * intel_vgpu_clean_mmio - clean MMIO space
  241. * @vgpu: a vGPU
  242. *
  243. */
  244. void intel_vgpu_clean_mmio(struct intel_vgpu *vgpu)
  245. {
  246. vfree(vgpu->mmio.vreg);
  247. vgpu->mmio.vreg = vgpu->mmio.sreg = NULL;
  248. }