mmio_context.c 17 KB

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