mmio_context.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. * Eddie Dong <eddie.dong@intel.com>
  25. * Kevin Tian <kevin.tian@intel.com>
  26. *
  27. * Contributors:
  28. * Zhi Wang <zhi.a.wang@intel.com>
  29. * Changbin Du <changbin.du@intel.com>
  30. * Zhenyu Wang <zhenyuw@linux.intel.com>
  31. * Tina Zhang <tina.zhang@intel.com>
  32. * Bing Niu <bing.niu@intel.com>
  33. *
  34. */
  35. #include "i915_drv.h"
  36. #include "gvt.h"
  37. #include "trace.h"
  38. #define GEN9_MOCS_SIZE 64
  39. /* Raw offset is appened to each line for convenience. */
  40. static struct engine_mmio gen8_engine_mmio_list[] __cacheline_aligned = {
  41. {RCS, GFX_MODE_GEN7, 0xffff, false}, /* 0x229c */
  42. {RCS, GEN9_CTX_PREEMPT_REG, 0x0, false}, /* 0x2248 */
  43. {RCS, HWSTAM, 0x0, false}, /* 0x2098 */
  44. {RCS, INSTPM, 0xffff, true}, /* 0x20c0 */
  45. {RCS, RING_FORCE_TO_NONPRIV(RENDER_RING_BASE, 0), 0, false}, /* 0x24d0 */
  46. {RCS, RING_FORCE_TO_NONPRIV(RENDER_RING_BASE, 1), 0, false}, /* 0x24d4 */
  47. {RCS, RING_FORCE_TO_NONPRIV(RENDER_RING_BASE, 2), 0, false}, /* 0x24d8 */
  48. {RCS, RING_FORCE_TO_NONPRIV(RENDER_RING_BASE, 3), 0, false}, /* 0x24dc */
  49. {RCS, RING_FORCE_TO_NONPRIV(RENDER_RING_BASE, 4), 0, false}, /* 0x24e0 */
  50. {RCS, RING_FORCE_TO_NONPRIV(RENDER_RING_BASE, 5), 0, false}, /* 0x24e4 */
  51. {RCS, RING_FORCE_TO_NONPRIV(RENDER_RING_BASE, 6), 0, false}, /* 0x24e8 */
  52. {RCS, RING_FORCE_TO_NONPRIV(RENDER_RING_BASE, 7), 0, false}, /* 0x24ec */
  53. {RCS, RING_FORCE_TO_NONPRIV(RENDER_RING_BASE, 8), 0, false}, /* 0x24f0 */
  54. {RCS, RING_FORCE_TO_NONPRIV(RENDER_RING_BASE, 9), 0, false}, /* 0x24f4 */
  55. {RCS, RING_FORCE_TO_NONPRIV(RENDER_RING_BASE, 10), 0, false}, /* 0x24f8 */
  56. {RCS, RING_FORCE_TO_NONPRIV(RENDER_RING_BASE, 11), 0, false}, /* 0x24fc */
  57. {RCS, CACHE_MODE_1, 0xffff, true}, /* 0x7004 */
  58. {RCS, GEN7_GT_MODE, 0xffff, true}, /* 0x7008 */
  59. {RCS, CACHE_MODE_0_GEN7, 0xffff, true}, /* 0x7000 */
  60. {RCS, GEN7_COMMON_SLICE_CHICKEN1, 0xffff, true}, /* 0x7010 */
  61. {RCS, HDC_CHICKEN0, 0xffff, true}, /* 0x7300 */
  62. {RCS, VF_GUARDBAND, 0xffff, true}, /* 0x83a4 */
  63. {BCS, RING_GFX_MODE(BLT_RING_BASE), 0xffff, false}, /* 0x2229c */
  64. {BCS, RING_MI_MODE(BLT_RING_BASE), 0xffff, false}, /* 0x2209c */
  65. {BCS, RING_INSTPM(BLT_RING_BASE), 0xffff, false}, /* 0x220c0 */
  66. {BCS, RING_HWSTAM(BLT_RING_BASE), 0x0, false}, /* 0x22098 */
  67. {BCS, RING_EXCC(BLT_RING_BASE), 0x0, false}, /* 0x22028 */
  68. {RCS, INVALID_MMIO_REG, 0, false } /* Terminated */
  69. };
  70. static struct engine_mmio gen9_engine_mmio_list[] __cacheline_aligned = {
  71. {RCS, GFX_MODE_GEN7, 0xffff, false}, /* 0x229c */
  72. {RCS, GEN9_CTX_PREEMPT_REG, 0x0, false}, /* 0x2248 */
  73. {RCS, HWSTAM, 0x0, false}, /* 0x2098 */
  74. {RCS, INSTPM, 0xffff, true}, /* 0x20c0 */
  75. {RCS, RING_FORCE_TO_NONPRIV(RENDER_RING_BASE, 0), 0, false}, /* 0x24d0 */
  76. {RCS, RING_FORCE_TO_NONPRIV(RENDER_RING_BASE, 1), 0, false}, /* 0x24d4 */
  77. {RCS, RING_FORCE_TO_NONPRIV(RENDER_RING_BASE, 2), 0, false}, /* 0x24d8 */
  78. {RCS, RING_FORCE_TO_NONPRIV(RENDER_RING_BASE, 3), 0, false}, /* 0x24dc */
  79. {RCS, RING_FORCE_TO_NONPRIV(RENDER_RING_BASE, 4), 0, false}, /* 0x24e0 */
  80. {RCS, RING_FORCE_TO_NONPRIV(RENDER_RING_BASE, 5), 0, false}, /* 0x24e4 */
  81. {RCS, RING_FORCE_TO_NONPRIV(RENDER_RING_BASE, 6), 0, false}, /* 0x24e8 */
  82. {RCS, RING_FORCE_TO_NONPRIV(RENDER_RING_BASE, 7), 0, false}, /* 0x24ec */
  83. {RCS, RING_FORCE_TO_NONPRIV(RENDER_RING_BASE, 8), 0, false}, /* 0x24f0 */
  84. {RCS, RING_FORCE_TO_NONPRIV(RENDER_RING_BASE, 9), 0, false}, /* 0x24f4 */
  85. {RCS, RING_FORCE_TO_NONPRIV(RENDER_RING_BASE, 10), 0, false}, /* 0x24f8 */
  86. {RCS, RING_FORCE_TO_NONPRIV(RENDER_RING_BASE, 11), 0, false}, /* 0x24fc */
  87. {RCS, CACHE_MODE_1, 0xffff, true}, /* 0x7004 */
  88. {RCS, GEN7_GT_MODE, 0xffff, true}, /* 0x7008 */
  89. {RCS, CACHE_MODE_0_GEN7, 0xffff, true}, /* 0x7000 */
  90. {RCS, GEN7_COMMON_SLICE_CHICKEN1, 0xffff, true}, /* 0x7010 */
  91. {RCS, HDC_CHICKEN0, 0xffff, true}, /* 0x7300 */
  92. {RCS, VF_GUARDBAND, 0xffff, true}, /* 0x83a4 */
  93. {RCS, GEN8_PRIVATE_PAT_LO, 0, false}, /* 0x40e0 */
  94. {RCS, GEN8_PRIVATE_PAT_HI, 0, false}, /* 0x40e4 */
  95. {RCS, GEN8_CS_CHICKEN1, 0xffff, true}, /* 0x2580 */
  96. {RCS, COMMON_SLICE_CHICKEN2, 0xffff, true}, /* 0x7014 */
  97. {RCS, GEN9_CS_DEBUG_MODE1, 0xffff, false}, /* 0x20ec */
  98. {RCS, GEN8_L3SQCREG4, 0, false}, /* 0xb118 */
  99. {RCS, GEN7_HALF_SLICE_CHICKEN1, 0xffff, true}, /* 0xe100 */
  100. {RCS, HALF_SLICE_CHICKEN2, 0xffff, true}, /* 0xe180 */
  101. {RCS, HALF_SLICE_CHICKEN3, 0xffff, true}, /* 0xe184 */
  102. {RCS, GEN9_HALF_SLICE_CHICKEN5, 0xffff, true}, /* 0xe188 */
  103. {RCS, GEN9_HALF_SLICE_CHICKEN7, 0xffff, true}, /* 0xe194 */
  104. {RCS, GEN8_ROW_CHICKEN, 0xffff, true}, /* 0xe4f0 */
  105. {RCS, TRVATTL3PTRDW(0), 0, false}, /* 0x4de0 */
  106. {RCS, TRVATTL3PTRDW(1), 0, false}, /* 0x4de4 */
  107. {RCS, TRNULLDETCT, 0, false}, /* 0x4de8 */
  108. {RCS, TRINVTILEDETCT, 0, false}, /* 0x4dec */
  109. {RCS, TRVADR, 0, false}, /* 0x4df0 */
  110. {RCS, TRTTE, 0, false}, /* 0x4df4 */
  111. {BCS, RING_GFX_MODE(BLT_RING_BASE), 0xffff, false}, /* 0x2229c */
  112. {BCS, RING_MI_MODE(BLT_RING_BASE), 0xffff, false}, /* 0x2209c */
  113. {BCS, RING_INSTPM(BLT_RING_BASE), 0xffff, false}, /* 0x220c0 */
  114. {BCS, RING_HWSTAM(BLT_RING_BASE), 0x0, false}, /* 0x22098 */
  115. {BCS, RING_EXCC(BLT_RING_BASE), 0x0, false}, /* 0x22028 */
  116. {VCS2, RING_EXCC(GEN8_BSD2_RING_BASE), 0xffff, false}, /* 0x1c028 */
  117. {VECS, RING_EXCC(VEBOX_RING_BASE), 0xffff, false}, /* 0x1a028 */
  118. {RCS, GEN8_HDC_CHICKEN1, 0xffff, true}, /* 0x7304 */
  119. {RCS, GEN9_CTX_PREEMPT_REG, 0x0, false}, /* 0x2248 */
  120. {RCS, GEN7_UCGCTL4, 0x0, false}, /* 0x940c */
  121. {RCS, GAMT_CHKN_BIT_REG, 0x0, false}, /* 0x4ab8 */
  122. {RCS, GEN9_GAMT_ECO_REG_RW_IA, 0x0, false}, /* 0x4ab0 */
  123. {RCS, GEN9_CSFE_CHICKEN1_RCS, 0xffff, false}, /* 0x20d4 */
  124. {RCS, GEN8_GARBCNTL, 0x0, false}, /* 0xb004 */
  125. {RCS, GEN7_FF_THREAD_MODE, 0x0, false}, /* 0x20a0 */
  126. {RCS, FF_SLICE_CS_CHICKEN2, 0xffff, false}, /* 0x20e4 */
  127. {RCS, INVALID_MMIO_REG, 0, false } /* Terminated */
  128. };
  129. static struct {
  130. bool initialized;
  131. u32 control_table[I915_NUM_ENGINES][GEN9_MOCS_SIZE];
  132. u32 l3cc_table[GEN9_MOCS_SIZE / 2];
  133. } gen9_render_mocs;
  134. static void load_render_mocs(struct drm_i915_private *dev_priv)
  135. {
  136. i915_reg_t offset;
  137. u32 regs[] = {
  138. [RCS] = 0xc800,
  139. [VCS] = 0xc900,
  140. [VCS2] = 0xca00,
  141. [BCS] = 0xcc00,
  142. [VECS] = 0xcb00,
  143. };
  144. int ring_id, i;
  145. for (ring_id = 0; ring_id < ARRAY_SIZE(regs); ring_id++) {
  146. if (!HAS_ENGINE(dev_priv, ring_id))
  147. continue;
  148. offset.reg = regs[ring_id];
  149. for (i = 0; i < GEN9_MOCS_SIZE; i++) {
  150. gen9_render_mocs.control_table[ring_id][i] =
  151. I915_READ_FW(offset);
  152. offset.reg += 4;
  153. }
  154. }
  155. offset.reg = 0xb020;
  156. for (i = 0; i < GEN9_MOCS_SIZE / 2; i++) {
  157. gen9_render_mocs.l3cc_table[i] =
  158. I915_READ_FW(offset);
  159. offset.reg += 4;
  160. }
  161. gen9_render_mocs.initialized = true;
  162. }
  163. static int
  164. restore_context_mmio_for_inhibit(struct intel_vgpu *vgpu,
  165. struct i915_request *req)
  166. {
  167. u32 *cs;
  168. int ret;
  169. struct engine_mmio *mmio;
  170. struct intel_gvt *gvt = vgpu->gvt;
  171. int ring_id = req->engine->id;
  172. int count = gvt->engine_mmio_list.ctx_mmio_count[ring_id];
  173. if (count == 0)
  174. return 0;
  175. ret = req->engine->emit_flush(req, EMIT_BARRIER);
  176. if (ret)
  177. return ret;
  178. cs = intel_ring_begin(req, count * 2 + 2);
  179. if (IS_ERR(cs))
  180. return PTR_ERR(cs);
  181. *cs++ = MI_LOAD_REGISTER_IMM(count);
  182. for (mmio = gvt->engine_mmio_list.mmio;
  183. i915_mmio_reg_valid(mmio->reg); mmio++) {
  184. if (mmio->ring_id != ring_id ||
  185. !mmio->in_context)
  186. continue;
  187. *cs++ = i915_mmio_reg_offset(mmio->reg);
  188. *cs++ = vgpu_vreg_t(vgpu, mmio->reg) |
  189. (mmio->mask << 16);
  190. gvt_dbg_core("add lri reg pair 0x%x:0x%x in inhibit ctx, vgpu:%d, rind_id:%d\n",
  191. *(cs-2), *(cs-1), vgpu->id, ring_id);
  192. }
  193. *cs++ = MI_NOOP;
  194. intel_ring_advance(req, cs);
  195. ret = req->engine->emit_flush(req, EMIT_BARRIER);
  196. if (ret)
  197. return ret;
  198. return 0;
  199. }
  200. static int
  201. restore_render_mocs_control_for_inhibit(struct intel_vgpu *vgpu,
  202. struct i915_request *req)
  203. {
  204. unsigned int index;
  205. u32 *cs;
  206. cs = intel_ring_begin(req, 2 * GEN9_MOCS_SIZE + 2);
  207. if (IS_ERR(cs))
  208. return PTR_ERR(cs);
  209. *cs++ = MI_LOAD_REGISTER_IMM(GEN9_MOCS_SIZE);
  210. for (index = 0; index < GEN9_MOCS_SIZE; index++) {
  211. *cs++ = i915_mmio_reg_offset(GEN9_GFX_MOCS(index));
  212. *cs++ = vgpu_vreg_t(vgpu, GEN9_GFX_MOCS(index));
  213. gvt_dbg_core("add lri reg pair 0x%x:0x%x in inhibit ctx, vgpu:%d, rind_id:%d\n",
  214. *(cs-2), *(cs-1), vgpu->id, req->engine->id);
  215. }
  216. *cs++ = MI_NOOP;
  217. intel_ring_advance(req, cs);
  218. return 0;
  219. }
  220. static int
  221. restore_render_mocs_l3cc_for_inhibit(struct intel_vgpu *vgpu,
  222. struct i915_request *req)
  223. {
  224. unsigned int index;
  225. u32 *cs;
  226. cs = intel_ring_begin(req, 2 * GEN9_MOCS_SIZE / 2 + 2);
  227. if (IS_ERR(cs))
  228. return PTR_ERR(cs);
  229. *cs++ = MI_LOAD_REGISTER_IMM(GEN9_MOCS_SIZE / 2);
  230. for (index = 0; index < GEN9_MOCS_SIZE / 2; index++) {
  231. *cs++ = i915_mmio_reg_offset(GEN9_LNCFCMOCS(index));
  232. *cs++ = vgpu_vreg_t(vgpu, GEN9_LNCFCMOCS(index));
  233. gvt_dbg_core("add lri reg pair 0x%x:0x%x in inhibit ctx, vgpu:%d, rind_id:%d\n",
  234. *(cs-2), *(cs-1), vgpu->id, req->engine->id);
  235. }
  236. *cs++ = MI_NOOP;
  237. intel_ring_advance(req, cs);
  238. return 0;
  239. }
  240. /*
  241. * Use lri command to initialize the mmio which is in context state image for
  242. * inhibit context, it contains tracked engine mmio, render_mocs and
  243. * render_mocs_l3cc.
  244. */
  245. int intel_vgpu_restore_inhibit_context(struct intel_vgpu *vgpu,
  246. struct i915_request *req)
  247. {
  248. int ret;
  249. u32 *cs;
  250. cs = intel_ring_begin(req, 2);
  251. if (IS_ERR(cs))
  252. return PTR_ERR(cs);
  253. *cs++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
  254. *cs++ = MI_NOOP;
  255. intel_ring_advance(req, cs);
  256. ret = restore_context_mmio_for_inhibit(vgpu, req);
  257. if (ret)
  258. goto out;
  259. /* no MOCS register in context except render engine */
  260. if (req->engine->id != RCS)
  261. goto out;
  262. ret = restore_render_mocs_control_for_inhibit(vgpu, req);
  263. if (ret)
  264. goto out;
  265. ret = restore_render_mocs_l3cc_for_inhibit(vgpu, req);
  266. if (ret)
  267. goto out;
  268. out:
  269. cs = intel_ring_begin(req, 2);
  270. if (IS_ERR(cs))
  271. return PTR_ERR(cs);
  272. *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
  273. *cs++ = MI_NOOP;
  274. intel_ring_advance(req, cs);
  275. return ret;
  276. }
  277. static void handle_tlb_pending_event(struct intel_vgpu *vgpu, int ring_id)
  278. {
  279. struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
  280. struct intel_vgpu_submission *s = &vgpu->submission;
  281. enum forcewake_domains fw;
  282. i915_reg_t reg;
  283. u32 regs[] = {
  284. [RCS] = 0x4260,
  285. [VCS] = 0x4264,
  286. [VCS2] = 0x4268,
  287. [BCS] = 0x426c,
  288. [VECS] = 0x4270,
  289. };
  290. if (WARN_ON(ring_id >= ARRAY_SIZE(regs)))
  291. return;
  292. if (!test_and_clear_bit(ring_id, (void *)s->tlb_handle_pending))
  293. return;
  294. reg = _MMIO(regs[ring_id]);
  295. /* WaForceWakeRenderDuringMmioTLBInvalidate:skl
  296. * we need to put a forcewake when invalidating RCS TLB caches,
  297. * otherwise device can go to RC6 state and interrupt invalidation
  298. * process
  299. */
  300. fw = intel_uncore_forcewake_for_reg(dev_priv, reg,
  301. FW_REG_READ | FW_REG_WRITE);
  302. if (ring_id == RCS && (IS_SKYLAKE(dev_priv) ||
  303. IS_KABYLAKE(dev_priv) || IS_BROXTON(dev_priv)))
  304. fw |= FORCEWAKE_RENDER;
  305. intel_uncore_forcewake_get(dev_priv, fw);
  306. I915_WRITE_FW(reg, 0x1);
  307. if (wait_for_atomic((I915_READ_FW(reg) == 0), 50))
  308. gvt_vgpu_err("timeout in invalidate ring (%d) tlb\n", ring_id);
  309. else
  310. vgpu_vreg_t(vgpu, reg) = 0;
  311. intel_uncore_forcewake_put(dev_priv, fw);
  312. gvt_dbg_core("invalidate TLB for ring %d\n", ring_id);
  313. }
  314. static void switch_mocs(struct intel_vgpu *pre, struct intel_vgpu *next,
  315. int ring_id)
  316. {
  317. struct drm_i915_private *dev_priv;
  318. i915_reg_t offset, l3_offset;
  319. u32 old_v, new_v;
  320. u32 regs[] = {
  321. [RCS] = 0xc800,
  322. [VCS] = 0xc900,
  323. [VCS2] = 0xca00,
  324. [BCS] = 0xcc00,
  325. [VECS] = 0xcb00,
  326. };
  327. int i;
  328. dev_priv = pre ? pre->gvt->dev_priv : next->gvt->dev_priv;
  329. if (WARN_ON(ring_id >= ARRAY_SIZE(regs)))
  330. return;
  331. if ((IS_KABYLAKE(dev_priv) || IS_BROXTON(dev_priv)) && ring_id == RCS)
  332. return;
  333. if (!pre && !gen9_render_mocs.initialized)
  334. load_render_mocs(dev_priv);
  335. offset.reg = regs[ring_id];
  336. for (i = 0; i < GEN9_MOCS_SIZE; i++) {
  337. if (pre)
  338. old_v = vgpu_vreg_t(pre, offset);
  339. else
  340. old_v = gen9_render_mocs.control_table[ring_id][i];
  341. if (next)
  342. new_v = vgpu_vreg_t(next, offset);
  343. else
  344. new_v = gen9_render_mocs.control_table[ring_id][i];
  345. if (old_v != new_v)
  346. I915_WRITE_FW(offset, new_v);
  347. offset.reg += 4;
  348. }
  349. if (ring_id == RCS) {
  350. l3_offset.reg = 0xb020;
  351. for (i = 0; i < GEN9_MOCS_SIZE / 2; i++) {
  352. if (pre)
  353. old_v = vgpu_vreg_t(pre, l3_offset);
  354. else
  355. old_v = gen9_render_mocs.l3cc_table[i];
  356. if (next)
  357. new_v = vgpu_vreg_t(next, l3_offset);
  358. else
  359. new_v = gen9_render_mocs.l3cc_table[i];
  360. if (old_v != new_v)
  361. I915_WRITE_FW(l3_offset, new_v);
  362. l3_offset.reg += 4;
  363. }
  364. }
  365. }
  366. #define CTX_CONTEXT_CONTROL_VAL 0x03
  367. bool is_inhibit_context(struct intel_context *ce)
  368. {
  369. const u32 *reg_state = ce->lrc_reg_state;
  370. u32 inhibit_mask =
  371. _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT);
  372. return inhibit_mask ==
  373. (reg_state[CTX_CONTEXT_CONTROL_VAL] & inhibit_mask);
  374. }
  375. /* Switch ring mmio values (context). */
  376. static void switch_mmio(struct intel_vgpu *pre,
  377. struct intel_vgpu *next,
  378. int ring_id)
  379. {
  380. struct drm_i915_private *dev_priv;
  381. struct intel_vgpu_submission *s;
  382. struct engine_mmio *mmio;
  383. u32 old_v, new_v;
  384. dev_priv = pre ? pre->gvt->dev_priv : next->gvt->dev_priv;
  385. if (IS_SKYLAKE(dev_priv)
  386. || IS_KABYLAKE(dev_priv)
  387. || IS_BROXTON(dev_priv))
  388. switch_mocs(pre, next, ring_id);
  389. for (mmio = dev_priv->gvt->engine_mmio_list.mmio;
  390. i915_mmio_reg_valid(mmio->reg); mmio++) {
  391. if (mmio->ring_id != ring_id)
  392. continue;
  393. /*
  394. * No need to do save or restore of the mmio which is in context
  395. * state image on kabylake, it's initialized by lri command and
  396. * save or restore with context together.
  397. */
  398. if ((IS_KABYLAKE(dev_priv) || IS_BROXTON(dev_priv))
  399. && mmio->in_context)
  400. continue;
  401. // save
  402. if (pre) {
  403. vgpu_vreg_t(pre, mmio->reg) = I915_READ_FW(mmio->reg);
  404. if (mmio->mask)
  405. vgpu_vreg_t(pre, mmio->reg) &=
  406. ~(mmio->mask << 16);
  407. old_v = vgpu_vreg_t(pre, mmio->reg);
  408. } else
  409. old_v = mmio->value = I915_READ_FW(mmio->reg);
  410. // restore
  411. if (next) {
  412. s = &next->submission;
  413. /*
  414. * No need to restore the mmio which is in context state
  415. * image if it's not inhibit context, it will restore
  416. * itself.
  417. */
  418. if (mmio->in_context &&
  419. !is_inhibit_context(&s->shadow_ctx->__engine[ring_id]))
  420. continue;
  421. if (mmio->mask)
  422. new_v = vgpu_vreg_t(next, mmio->reg) |
  423. (mmio->mask << 16);
  424. else
  425. new_v = vgpu_vreg_t(next, mmio->reg);
  426. } else {
  427. if (mmio->in_context)
  428. continue;
  429. if (mmio->mask)
  430. new_v = mmio->value | (mmio->mask << 16);
  431. else
  432. new_v = mmio->value;
  433. }
  434. I915_WRITE_FW(mmio->reg, new_v);
  435. trace_render_mmio(pre ? pre->id : 0,
  436. next ? next->id : 0,
  437. "switch",
  438. i915_mmio_reg_offset(mmio->reg),
  439. old_v, new_v);
  440. }
  441. if (next)
  442. handle_tlb_pending_event(next, ring_id);
  443. }
  444. /**
  445. * intel_gvt_switch_render_mmio - switch mmio context of specific engine
  446. * @pre: the last vGPU that own the engine
  447. * @next: the vGPU to switch to
  448. * @ring_id: specify the engine
  449. *
  450. * If pre is null indicates that host own the engine. If next is null
  451. * indicates that we are switching to host workload.
  452. */
  453. void intel_gvt_switch_mmio(struct intel_vgpu *pre,
  454. struct intel_vgpu *next, int ring_id)
  455. {
  456. struct drm_i915_private *dev_priv;
  457. if (WARN_ON(!pre && !next))
  458. return;
  459. gvt_dbg_render("switch ring %d from %s to %s\n", ring_id,
  460. pre ? "vGPU" : "host", next ? "vGPU" : "HOST");
  461. dev_priv = pre ? pre->gvt->dev_priv : next->gvt->dev_priv;
  462. /**
  463. * We are using raw mmio access wrapper to improve the
  464. * performace for batch mmio read/write, so we need
  465. * handle forcewake mannually.
  466. */
  467. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  468. switch_mmio(pre, next, ring_id);
  469. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  470. }
  471. /**
  472. * intel_gvt_init_engine_mmio_context - Initiate the engine mmio list
  473. * @gvt: GVT device
  474. *
  475. */
  476. void intel_gvt_init_engine_mmio_context(struct intel_gvt *gvt)
  477. {
  478. struct engine_mmio *mmio;
  479. if (IS_SKYLAKE(gvt->dev_priv) ||
  480. IS_KABYLAKE(gvt->dev_priv) ||
  481. IS_BROXTON(gvt->dev_priv))
  482. gvt->engine_mmio_list.mmio = gen9_engine_mmio_list;
  483. else
  484. gvt->engine_mmio_list.mmio = gen8_engine_mmio_list;
  485. for (mmio = gvt->engine_mmio_list.mmio;
  486. i915_mmio_reg_valid(mmio->reg); mmio++) {
  487. if (mmio->in_context) {
  488. gvt->engine_mmio_list.ctx_mmio_count[mmio->ring_id]++;
  489. intel_gvt_mmio_set_in_ctx(gvt, mmio->reg.reg);
  490. }
  491. }
  492. }