execlist.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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. * Zhiyuan Lv <zhiyuan.lv@intel.com>
  25. * Zhi Wang <zhi.a.wang@intel.com>
  26. *
  27. * Contributors:
  28. * Min He <min.he@intel.com>
  29. * Bing Niu <bing.niu@intel.com>
  30. * Ping Gao <ping.a.gao@intel.com>
  31. * Tina Zhang <tina.zhang@intel.com>
  32. *
  33. */
  34. #include "i915_drv.h"
  35. #include "gvt.h"
  36. #define _EL_OFFSET_STATUS 0x234
  37. #define _EL_OFFSET_STATUS_BUF 0x370
  38. #define _EL_OFFSET_STATUS_PTR 0x3A0
  39. #define execlist_ring_mmio(gvt, ring_id, offset) \
  40. (gvt->dev_priv->engine[ring_id]->mmio_base + (offset))
  41. #define valid_context(ctx) ((ctx)->valid)
  42. #define same_context(a, b) (((a)->context_id == (b)->context_id) && \
  43. ((a)->lrca == (b)->lrca))
  44. static void clean_workloads(struct intel_vgpu *vgpu, unsigned long engine_mask);
  45. static int context_switch_events[] = {
  46. [RCS] = RCS_AS_CONTEXT_SWITCH,
  47. [BCS] = BCS_AS_CONTEXT_SWITCH,
  48. [VCS] = VCS_AS_CONTEXT_SWITCH,
  49. [VCS2] = VCS2_AS_CONTEXT_SWITCH,
  50. [VECS] = VECS_AS_CONTEXT_SWITCH,
  51. };
  52. static int ring_id_to_context_switch_event(int ring_id)
  53. {
  54. if (WARN_ON(ring_id < RCS ||
  55. ring_id >= ARRAY_SIZE(context_switch_events)))
  56. return -EINVAL;
  57. return context_switch_events[ring_id];
  58. }
  59. static void switch_virtual_execlist_slot(struct intel_vgpu_execlist *execlist)
  60. {
  61. gvt_dbg_el("[before] running slot %d/context %x pending slot %d\n",
  62. execlist->running_slot ?
  63. execlist->running_slot->index : -1,
  64. execlist->running_context ?
  65. execlist->running_context->context_id : 0,
  66. execlist->pending_slot ?
  67. execlist->pending_slot->index : -1);
  68. execlist->running_slot = execlist->pending_slot;
  69. execlist->pending_slot = NULL;
  70. execlist->running_context = execlist->running_context ?
  71. &execlist->running_slot->ctx[0] : NULL;
  72. gvt_dbg_el("[after] running slot %d/context %x pending slot %d\n",
  73. execlist->running_slot ?
  74. execlist->running_slot->index : -1,
  75. execlist->running_context ?
  76. execlist->running_context->context_id : 0,
  77. execlist->pending_slot ?
  78. execlist->pending_slot->index : -1);
  79. }
  80. static void emulate_execlist_status(struct intel_vgpu_execlist *execlist)
  81. {
  82. struct intel_vgpu_execlist_slot *running = execlist->running_slot;
  83. struct intel_vgpu_execlist_slot *pending = execlist->pending_slot;
  84. struct execlist_ctx_descriptor_format *desc = execlist->running_context;
  85. struct intel_vgpu *vgpu = execlist->vgpu;
  86. struct execlist_status_format status;
  87. int ring_id = execlist->ring_id;
  88. u32 status_reg = execlist_ring_mmio(vgpu->gvt,
  89. ring_id, _EL_OFFSET_STATUS);
  90. status.ldw = vgpu_vreg(vgpu, status_reg);
  91. status.udw = vgpu_vreg(vgpu, status_reg + 4);
  92. if (running) {
  93. status.current_execlist_pointer = !!running->index;
  94. status.execlist_write_pointer = !!!running->index;
  95. status.execlist_0_active = status.execlist_0_valid =
  96. !!!(running->index);
  97. status.execlist_1_active = status.execlist_1_valid =
  98. !!(running->index);
  99. } else {
  100. status.context_id = 0;
  101. status.execlist_0_active = status.execlist_0_valid = 0;
  102. status.execlist_1_active = status.execlist_1_valid = 0;
  103. }
  104. status.context_id = desc ? desc->context_id : 0;
  105. status.execlist_queue_full = !!(pending);
  106. vgpu_vreg(vgpu, status_reg) = status.ldw;
  107. vgpu_vreg(vgpu, status_reg + 4) = status.udw;
  108. gvt_dbg_el("vgpu%d: status reg offset %x ldw %x udw %x\n",
  109. vgpu->id, status_reg, status.ldw, status.udw);
  110. }
  111. static void emulate_csb_update(struct intel_vgpu_execlist *execlist,
  112. struct execlist_context_status_format *status,
  113. bool trigger_interrupt_later)
  114. {
  115. struct intel_vgpu *vgpu = execlist->vgpu;
  116. int ring_id = execlist->ring_id;
  117. struct execlist_context_status_pointer_format ctx_status_ptr;
  118. u32 write_pointer;
  119. u32 ctx_status_ptr_reg, ctx_status_buf_reg, offset;
  120. ctx_status_ptr_reg = execlist_ring_mmio(vgpu->gvt, ring_id,
  121. _EL_OFFSET_STATUS_PTR);
  122. ctx_status_buf_reg = execlist_ring_mmio(vgpu->gvt, ring_id,
  123. _EL_OFFSET_STATUS_BUF);
  124. ctx_status_ptr.dw = vgpu_vreg(vgpu, ctx_status_ptr_reg);
  125. write_pointer = ctx_status_ptr.write_ptr;
  126. if (write_pointer == 0x7)
  127. write_pointer = 0;
  128. else {
  129. ++write_pointer;
  130. write_pointer %= 0x6;
  131. }
  132. offset = ctx_status_buf_reg + write_pointer * 8;
  133. vgpu_vreg(vgpu, offset) = status->ldw;
  134. vgpu_vreg(vgpu, offset + 4) = status->udw;
  135. ctx_status_ptr.write_ptr = write_pointer;
  136. vgpu_vreg(vgpu, ctx_status_ptr_reg) = ctx_status_ptr.dw;
  137. gvt_dbg_el("vgpu%d: w pointer %u reg %x csb l %x csb h %x\n",
  138. vgpu->id, write_pointer, offset, status->ldw, status->udw);
  139. if (trigger_interrupt_later)
  140. return;
  141. intel_vgpu_trigger_virtual_event(vgpu,
  142. ring_id_to_context_switch_event(execlist->ring_id));
  143. }
  144. static int emulate_execlist_ctx_schedule_out(
  145. struct intel_vgpu_execlist *execlist,
  146. struct execlist_ctx_descriptor_format *ctx)
  147. {
  148. struct intel_vgpu *vgpu = execlist->vgpu;
  149. struct intel_vgpu_execlist_slot *running = execlist->running_slot;
  150. struct intel_vgpu_execlist_slot *pending = execlist->pending_slot;
  151. struct execlist_ctx_descriptor_format *ctx0 = &running->ctx[0];
  152. struct execlist_ctx_descriptor_format *ctx1 = &running->ctx[1];
  153. struct execlist_context_status_format status;
  154. memset(&status, 0, sizeof(status));
  155. gvt_dbg_el("schedule out context id %x\n", ctx->context_id);
  156. if (WARN_ON(!same_context(ctx, execlist->running_context))) {
  157. gvt_vgpu_err("schedule out context is not running context,"
  158. "ctx id %x running ctx id %x\n",
  159. ctx->context_id,
  160. execlist->running_context->context_id);
  161. return -EINVAL;
  162. }
  163. /* ctx1 is valid, ctx0/ctx is scheduled-out -> element switch */
  164. if (valid_context(ctx1) && same_context(ctx0, ctx)) {
  165. gvt_dbg_el("ctx 1 valid, ctx/ctx 0 is scheduled-out\n");
  166. execlist->running_context = ctx1;
  167. emulate_execlist_status(execlist);
  168. status.context_complete = status.element_switch = 1;
  169. status.context_id = ctx->context_id;
  170. emulate_csb_update(execlist, &status, false);
  171. /*
  172. * ctx1 is not valid, ctx == ctx0
  173. * ctx1 is valid, ctx1 == ctx
  174. * --> last element is finished
  175. * emulate:
  176. * active-to-idle if there is *no* pending execlist
  177. * context-complete if there *is* pending execlist
  178. */
  179. } else if ((!valid_context(ctx1) && same_context(ctx0, ctx))
  180. || (valid_context(ctx1) && same_context(ctx1, ctx))) {
  181. gvt_dbg_el("need to switch virtual execlist slot\n");
  182. switch_virtual_execlist_slot(execlist);
  183. emulate_execlist_status(execlist);
  184. status.context_complete = status.active_to_idle = 1;
  185. status.context_id = ctx->context_id;
  186. if (!pending) {
  187. emulate_csb_update(execlist, &status, false);
  188. } else {
  189. emulate_csb_update(execlist, &status, true);
  190. memset(&status, 0, sizeof(status));
  191. status.idle_to_active = 1;
  192. status.context_id = 0;
  193. emulate_csb_update(execlist, &status, false);
  194. }
  195. } else {
  196. WARN_ON(1);
  197. return -EINVAL;
  198. }
  199. return 0;
  200. }
  201. static struct intel_vgpu_execlist_slot *get_next_execlist_slot(
  202. struct intel_vgpu_execlist *execlist)
  203. {
  204. struct intel_vgpu *vgpu = execlist->vgpu;
  205. int ring_id = execlist->ring_id;
  206. u32 status_reg = execlist_ring_mmio(vgpu->gvt, ring_id,
  207. _EL_OFFSET_STATUS);
  208. struct execlist_status_format status;
  209. status.ldw = vgpu_vreg(vgpu, status_reg);
  210. status.udw = vgpu_vreg(vgpu, status_reg + 4);
  211. if (status.execlist_queue_full) {
  212. gvt_vgpu_err("virtual execlist slots are full\n");
  213. return NULL;
  214. }
  215. return &execlist->slot[status.execlist_write_pointer];
  216. }
  217. static int emulate_execlist_schedule_in(struct intel_vgpu_execlist *execlist,
  218. struct execlist_ctx_descriptor_format ctx[2])
  219. {
  220. struct intel_vgpu_execlist_slot *running = execlist->running_slot;
  221. struct intel_vgpu_execlist_slot *slot =
  222. get_next_execlist_slot(execlist);
  223. struct execlist_ctx_descriptor_format *ctx0, *ctx1;
  224. struct execlist_context_status_format status;
  225. struct intel_vgpu *vgpu = execlist->vgpu;
  226. gvt_dbg_el("emulate schedule-in\n");
  227. if (!slot) {
  228. gvt_vgpu_err("no available execlist slot\n");
  229. return -EINVAL;
  230. }
  231. memset(&status, 0, sizeof(status));
  232. memset(slot->ctx, 0, sizeof(slot->ctx));
  233. slot->ctx[0] = ctx[0];
  234. slot->ctx[1] = ctx[1];
  235. gvt_dbg_el("alloc slot index %d ctx 0 %x ctx 1 %x\n",
  236. slot->index, ctx[0].context_id,
  237. ctx[1].context_id);
  238. /*
  239. * no running execlist, make this write bundle as running execlist
  240. * -> idle-to-active
  241. */
  242. if (!running) {
  243. gvt_dbg_el("no current running execlist\n");
  244. execlist->running_slot = slot;
  245. execlist->pending_slot = NULL;
  246. execlist->running_context = &slot->ctx[0];
  247. gvt_dbg_el("running slot index %d running context %x\n",
  248. execlist->running_slot->index,
  249. execlist->running_context->context_id);
  250. emulate_execlist_status(execlist);
  251. status.idle_to_active = 1;
  252. status.context_id = 0;
  253. emulate_csb_update(execlist, &status, false);
  254. return 0;
  255. }
  256. ctx0 = &running->ctx[0];
  257. ctx1 = &running->ctx[1];
  258. gvt_dbg_el("current running slot index %d ctx 0 %x ctx 1 %x\n",
  259. running->index, ctx0->context_id, ctx1->context_id);
  260. /*
  261. * already has an running execlist
  262. * a. running ctx1 is valid,
  263. * ctx0 is finished, and running ctx1 == new execlist ctx[0]
  264. * b. running ctx1 is not valid,
  265. * ctx0 == new execlist ctx[0]
  266. * ----> lite-restore + preempted
  267. */
  268. if ((valid_context(ctx1) && same_context(ctx1, &slot->ctx[0]) &&
  269. /* condition a */
  270. (!same_context(ctx0, execlist->running_context))) ||
  271. (!valid_context(ctx1) &&
  272. same_context(ctx0, &slot->ctx[0]))) { /* condition b */
  273. gvt_dbg_el("need to switch virtual execlist slot\n");
  274. execlist->pending_slot = slot;
  275. switch_virtual_execlist_slot(execlist);
  276. emulate_execlist_status(execlist);
  277. status.lite_restore = status.preempted = 1;
  278. status.context_id = ctx[0].context_id;
  279. emulate_csb_update(execlist, &status, false);
  280. } else {
  281. gvt_dbg_el("emulate as pending slot\n");
  282. /*
  283. * otherwise
  284. * --> emulate pending execlist exist + but no preemption case
  285. */
  286. execlist->pending_slot = slot;
  287. emulate_execlist_status(execlist);
  288. }
  289. return 0;
  290. }
  291. static void free_workload(struct intel_vgpu_workload *workload)
  292. {
  293. intel_vgpu_unpin_mm(workload->shadow_mm);
  294. intel_gvt_mm_unreference(workload->shadow_mm);
  295. kmem_cache_free(workload->vgpu->workloads, workload);
  296. }
  297. #define get_desc_from_elsp_dwords(ed, i) \
  298. ((struct execlist_ctx_descriptor_format *)&((ed)->data[i * 2]))
  299. static void prepare_shadow_batch_buffer(struct intel_vgpu_workload *workload)
  300. {
  301. const int gmadr_bytes = workload->vgpu->gvt->device_info.gmadr_bytes_in_cmd;
  302. struct intel_shadow_bb_entry *entry_obj;
  303. /* pin the gem object to ggtt */
  304. list_for_each_entry(entry_obj, &workload->shadow_bb, list) {
  305. struct i915_vma *vma;
  306. vma = i915_gem_object_ggtt_pin(entry_obj->obj, NULL, 0, 4, 0);
  307. if (IS_ERR(vma)) {
  308. return;
  309. }
  310. /* FIXME: we are not tracking our pinned VMA leaving it
  311. * up to the core to fix up the stray pin_count upon
  312. * free.
  313. */
  314. /* update the relocate gma with shadow batch buffer*/
  315. entry_obj->bb_start_cmd_va[1] = i915_ggtt_offset(vma);
  316. if (gmadr_bytes == 8)
  317. entry_obj->bb_start_cmd_va[2] = 0;
  318. }
  319. }
  320. static int update_wa_ctx_2_shadow_ctx(struct intel_shadow_wa_ctx *wa_ctx)
  321. {
  322. struct intel_vgpu_workload *workload = container_of(wa_ctx,
  323. struct intel_vgpu_workload,
  324. wa_ctx);
  325. int ring_id = workload->ring_id;
  326. struct i915_gem_context *shadow_ctx = workload->vgpu->shadow_ctx;
  327. struct drm_i915_gem_object *ctx_obj =
  328. shadow_ctx->engine[ring_id].state->obj;
  329. struct execlist_ring_context *shadow_ring_context;
  330. struct page *page;
  331. page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN);
  332. shadow_ring_context = kmap_atomic(page);
  333. shadow_ring_context->bb_per_ctx_ptr.val =
  334. (shadow_ring_context->bb_per_ctx_ptr.val &
  335. (~PER_CTX_ADDR_MASK)) | wa_ctx->per_ctx.shadow_gma;
  336. shadow_ring_context->rcs_indirect_ctx.val =
  337. (shadow_ring_context->rcs_indirect_ctx.val &
  338. (~INDIRECT_CTX_ADDR_MASK)) | wa_ctx->indirect_ctx.shadow_gma;
  339. kunmap_atomic(shadow_ring_context);
  340. return 0;
  341. }
  342. static void prepare_shadow_wa_ctx(struct intel_shadow_wa_ctx *wa_ctx)
  343. {
  344. struct i915_vma *vma;
  345. unsigned char *per_ctx_va =
  346. (unsigned char *)wa_ctx->indirect_ctx.shadow_va +
  347. wa_ctx->indirect_ctx.size;
  348. if (wa_ctx->indirect_ctx.size == 0)
  349. return;
  350. vma = i915_gem_object_ggtt_pin(wa_ctx->indirect_ctx.obj, NULL,
  351. 0, CACHELINE_BYTES, 0);
  352. if (IS_ERR(vma)) {
  353. return;
  354. }
  355. /* FIXME: we are not tracking our pinned VMA leaving it
  356. * up to the core to fix up the stray pin_count upon
  357. * free.
  358. */
  359. wa_ctx->indirect_ctx.shadow_gma = i915_ggtt_offset(vma);
  360. wa_ctx->per_ctx.shadow_gma = *((unsigned int *)per_ctx_va + 1);
  361. memset(per_ctx_va, 0, CACHELINE_BYTES);
  362. update_wa_ctx_2_shadow_ctx(wa_ctx);
  363. }
  364. static int prepare_execlist_workload(struct intel_vgpu_workload *workload)
  365. {
  366. struct intel_vgpu *vgpu = workload->vgpu;
  367. struct execlist_ctx_descriptor_format ctx[2];
  368. int ring_id = workload->ring_id;
  369. intel_vgpu_pin_mm(workload->shadow_mm);
  370. intel_vgpu_sync_oos_pages(workload->vgpu);
  371. intel_vgpu_flush_post_shadow(workload->vgpu);
  372. prepare_shadow_batch_buffer(workload);
  373. prepare_shadow_wa_ctx(&workload->wa_ctx);
  374. if (!workload->emulate_schedule_in)
  375. return 0;
  376. ctx[0] = *get_desc_from_elsp_dwords(&workload->elsp_dwords, 1);
  377. ctx[1] = *get_desc_from_elsp_dwords(&workload->elsp_dwords, 0);
  378. return emulate_execlist_schedule_in(&vgpu->execlist[ring_id], ctx);
  379. }
  380. static void release_shadow_batch_buffer(struct intel_vgpu_workload *workload)
  381. {
  382. /* release all the shadow batch buffer */
  383. if (!list_empty(&workload->shadow_bb)) {
  384. struct intel_shadow_bb_entry *entry_obj =
  385. list_first_entry(&workload->shadow_bb,
  386. struct intel_shadow_bb_entry,
  387. list);
  388. struct intel_shadow_bb_entry *temp;
  389. list_for_each_entry_safe(entry_obj, temp, &workload->shadow_bb,
  390. list) {
  391. i915_gem_object_unpin_map(entry_obj->obj);
  392. i915_gem_object_put(entry_obj->obj);
  393. list_del(&entry_obj->list);
  394. kfree(entry_obj);
  395. }
  396. }
  397. }
  398. static void release_shadow_wa_ctx(struct intel_shadow_wa_ctx *wa_ctx)
  399. {
  400. if (!wa_ctx->indirect_ctx.obj)
  401. return;
  402. i915_gem_object_unpin_map(wa_ctx->indirect_ctx.obj);
  403. i915_gem_object_put(wa_ctx->indirect_ctx.obj);
  404. }
  405. static int complete_execlist_workload(struct intel_vgpu_workload *workload)
  406. {
  407. struct intel_vgpu *vgpu = workload->vgpu;
  408. int ring_id = workload->ring_id;
  409. struct intel_vgpu_execlist *execlist = &vgpu->execlist[ring_id];
  410. struct intel_vgpu_workload *next_workload;
  411. struct list_head *next = workload_q_head(vgpu, ring_id)->next;
  412. bool lite_restore = false;
  413. int ret;
  414. gvt_dbg_el("complete workload %p status %d\n", workload,
  415. workload->status);
  416. release_shadow_batch_buffer(workload);
  417. release_shadow_wa_ctx(&workload->wa_ctx);
  418. if (workload->status || (vgpu->resetting_eng & ENGINE_MASK(ring_id))) {
  419. /* if workload->status is not successful means HW GPU
  420. * has occurred GPU hang or something wrong with i915/GVT,
  421. * and GVT won't inject context switch interrupt to guest.
  422. * So this error is a vGPU hang actually to the guest.
  423. * According to this we should emunlate a vGPU hang. If
  424. * there are pending workloads which are already submitted
  425. * from guest, we should clean them up like HW GPU does.
  426. *
  427. * if it is in middle of engine resetting, the pending
  428. * workloads won't be submitted to HW GPU and will be
  429. * cleaned up during the resetting process later, so doing
  430. * the workload clean up here doesn't have any impact.
  431. **/
  432. clean_workloads(vgpu, ENGINE_MASK(ring_id));
  433. goto out;
  434. }
  435. if (!list_empty(workload_q_head(vgpu, ring_id))) {
  436. struct execlist_ctx_descriptor_format *this_desc, *next_desc;
  437. next_workload = container_of(next,
  438. struct intel_vgpu_workload, list);
  439. this_desc = &workload->ctx_desc;
  440. next_desc = &next_workload->ctx_desc;
  441. lite_restore = same_context(this_desc, next_desc);
  442. }
  443. if (lite_restore) {
  444. gvt_dbg_el("next context == current - no schedule-out\n");
  445. free_workload(workload);
  446. return 0;
  447. }
  448. ret = emulate_execlist_ctx_schedule_out(execlist, &workload->ctx_desc);
  449. if (ret)
  450. goto err;
  451. out:
  452. free_workload(workload);
  453. return 0;
  454. err:
  455. free_workload(workload);
  456. return ret;
  457. }
  458. #define RING_CTX_OFF(x) \
  459. offsetof(struct execlist_ring_context, x)
  460. static void read_guest_pdps(struct intel_vgpu *vgpu,
  461. u64 ring_context_gpa, u32 pdp[8])
  462. {
  463. u64 gpa;
  464. int i;
  465. gpa = ring_context_gpa + RING_CTX_OFF(pdp3_UDW.val);
  466. for (i = 0; i < 8; i++)
  467. intel_gvt_hypervisor_read_gpa(vgpu,
  468. gpa + i * 8, &pdp[7 - i], 4);
  469. }
  470. static int prepare_mm(struct intel_vgpu_workload *workload)
  471. {
  472. struct execlist_ctx_descriptor_format *desc = &workload->ctx_desc;
  473. struct intel_vgpu_mm *mm;
  474. struct intel_vgpu *vgpu = workload->vgpu;
  475. int page_table_level;
  476. u32 pdp[8];
  477. if (desc->addressing_mode == 1) { /* legacy 32-bit */
  478. page_table_level = 3;
  479. } else if (desc->addressing_mode == 3) { /* legacy 64 bit */
  480. page_table_level = 4;
  481. } else {
  482. gvt_vgpu_err("Advanced Context mode(SVM) is not supported!\n");
  483. return -EINVAL;
  484. }
  485. read_guest_pdps(workload->vgpu, workload->ring_context_gpa, pdp);
  486. mm = intel_vgpu_find_ppgtt_mm(workload->vgpu, page_table_level, pdp);
  487. if (mm) {
  488. intel_gvt_mm_reference(mm);
  489. } else {
  490. mm = intel_vgpu_create_mm(workload->vgpu, INTEL_GVT_MM_PPGTT,
  491. pdp, page_table_level, 0);
  492. if (IS_ERR(mm)) {
  493. gvt_vgpu_err("fail to create mm object.\n");
  494. return PTR_ERR(mm);
  495. }
  496. }
  497. workload->shadow_mm = mm;
  498. return 0;
  499. }
  500. #define get_last_workload(q) \
  501. (list_empty(q) ? NULL : container_of(q->prev, \
  502. struct intel_vgpu_workload, list))
  503. static int submit_context(struct intel_vgpu *vgpu, int ring_id,
  504. struct execlist_ctx_descriptor_format *desc,
  505. bool emulate_schedule_in)
  506. {
  507. struct list_head *q = workload_q_head(vgpu, ring_id);
  508. struct intel_vgpu_workload *last_workload = get_last_workload(q);
  509. struct intel_vgpu_workload *workload = NULL;
  510. struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
  511. u64 ring_context_gpa;
  512. u32 head, tail, start, ctl, ctx_ctl, per_ctx, indirect_ctx;
  513. int ret;
  514. ring_context_gpa = intel_vgpu_gma_to_gpa(vgpu->gtt.ggtt_mm,
  515. (u32)((desc->lrca + 1) << GTT_PAGE_SHIFT));
  516. if (ring_context_gpa == INTEL_GVT_INVALID_ADDR) {
  517. gvt_vgpu_err("invalid guest context LRCA: %x\n", desc->lrca);
  518. return -EINVAL;
  519. }
  520. intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
  521. RING_CTX_OFF(ring_header.val), &head, 4);
  522. intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
  523. RING_CTX_OFF(ring_tail.val), &tail, 4);
  524. head &= RB_HEAD_OFF_MASK;
  525. tail &= RB_TAIL_OFF_MASK;
  526. if (last_workload && same_context(&last_workload->ctx_desc, desc)) {
  527. gvt_dbg_el("ring id %d cur workload == last\n", ring_id);
  528. gvt_dbg_el("ctx head %x real head %lx\n", head,
  529. last_workload->rb_tail);
  530. /*
  531. * cannot use guest context head pointer here,
  532. * as it might not be updated at this time
  533. */
  534. head = last_workload->rb_tail;
  535. }
  536. gvt_dbg_el("ring id %d begin a new workload\n", ring_id);
  537. workload = kmem_cache_zalloc(vgpu->workloads, GFP_KERNEL);
  538. if (!workload)
  539. return -ENOMEM;
  540. /* record some ring buffer register values for scan and shadow */
  541. intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
  542. RING_CTX_OFF(rb_start.val), &start, 4);
  543. intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
  544. RING_CTX_OFF(rb_ctrl.val), &ctl, 4);
  545. intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
  546. RING_CTX_OFF(ctx_ctrl.val), &ctx_ctl, 4);
  547. INIT_LIST_HEAD(&workload->list);
  548. INIT_LIST_HEAD(&workload->shadow_bb);
  549. init_waitqueue_head(&workload->shadow_ctx_status_wq);
  550. atomic_set(&workload->shadow_ctx_active, 0);
  551. workload->vgpu = vgpu;
  552. workload->ring_id = ring_id;
  553. workload->ctx_desc = *desc;
  554. workload->ring_context_gpa = ring_context_gpa;
  555. workload->rb_head = head;
  556. workload->rb_tail = tail;
  557. workload->rb_start = start;
  558. workload->rb_ctl = ctl;
  559. workload->prepare = prepare_execlist_workload;
  560. workload->complete = complete_execlist_workload;
  561. workload->status = -EINPROGRESS;
  562. workload->emulate_schedule_in = emulate_schedule_in;
  563. workload->shadowed = false;
  564. if (ring_id == RCS) {
  565. intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
  566. RING_CTX_OFF(bb_per_ctx_ptr.val), &per_ctx, 4);
  567. intel_gvt_hypervisor_read_gpa(vgpu, ring_context_gpa +
  568. RING_CTX_OFF(rcs_indirect_ctx.val), &indirect_ctx, 4);
  569. workload->wa_ctx.indirect_ctx.guest_gma =
  570. indirect_ctx & INDIRECT_CTX_ADDR_MASK;
  571. workload->wa_ctx.indirect_ctx.size =
  572. (indirect_ctx & INDIRECT_CTX_SIZE_MASK) *
  573. CACHELINE_BYTES;
  574. workload->wa_ctx.per_ctx.guest_gma =
  575. per_ctx & PER_CTX_ADDR_MASK;
  576. workload->wa_ctx.per_ctx.valid = per_ctx & 1;
  577. }
  578. if (emulate_schedule_in)
  579. workload->elsp_dwords = vgpu->execlist[ring_id].elsp_dwords;
  580. gvt_dbg_el("workload %p ring id %d head %x tail %x start %x ctl %x\n",
  581. workload, ring_id, head, tail, start, ctl);
  582. gvt_dbg_el("workload %p emulate schedule_in %d\n", workload,
  583. emulate_schedule_in);
  584. ret = prepare_mm(workload);
  585. if (ret) {
  586. kmem_cache_free(vgpu->workloads, workload);
  587. return ret;
  588. }
  589. /* Only scan and shadow the first workload in the queue
  590. * as there is only one pre-allocated buf-obj for shadow.
  591. */
  592. if (list_empty(workload_q_head(vgpu, ring_id))) {
  593. intel_runtime_pm_get(dev_priv);
  594. mutex_lock(&dev_priv->drm.struct_mutex);
  595. intel_gvt_scan_and_shadow_workload(workload);
  596. mutex_unlock(&dev_priv->drm.struct_mutex);
  597. intel_runtime_pm_put(dev_priv);
  598. }
  599. queue_workload(workload);
  600. return 0;
  601. }
  602. int intel_vgpu_submit_execlist(struct intel_vgpu *vgpu, int ring_id)
  603. {
  604. struct intel_vgpu_execlist *execlist = &vgpu->execlist[ring_id];
  605. struct execlist_ctx_descriptor_format desc[2];
  606. int i, ret;
  607. desc[0] = *get_desc_from_elsp_dwords(&execlist->elsp_dwords, 1);
  608. desc[1] = *get_desc_from_elsp_dwords(&execlist->elsp_dwords, 0);
  609. if (!desc[0].valid) {
  610. gvt_vgpu_err("invalid elsp submission, desc0 is invalid\n");
  611. goto inv_desc;
  612. }
  613. for (i = 0; i < ARRAY_SIZE(desc); i++) {
  614. if (!desc[i].valid)
  615. continue;
  616. if (!desc[i].privilege_access) {
  617. gvt_vgpu_err("unexpected GGTT elsp submission\n");
  618. goto inv_desc;
  619. }
  620. }
  621. /* submit workload */
  622. for (i = 0; i < ARRAY_SIZE(desc); i++) {
  623. if (!desc[i].valid)
  624. continue;
  625. ret = submit_context(vgpu, ring_id, &desc[i], i == 0);
  626. if (ret) {
  627. gvt_vgpu_err("failed to submit desc %d\n", i);
  628. return ret;
  629. }
  630. }
  631. return 0;
  632. inv_desc:
  633. gvt_vgpu_err("descriptors content: desc0 %08x %08x desc1 %08x %08x\n",
  634. desc[0].udw, desc[0].ldw, desc[1].udw, desc[1].ldw);
  635. return -EINVAL;
  636. }
  637. static void init_vgpu_execlist(struct intel_vgpu *vgpu, int ring_id)
  638. {
  639. struct intel_vgpu_execlist *execlist = &vgpu->execlist[ring_id];
  640. struct execlist_context_status_pointer_format ctx_status_ptr;
  641. u32 ctx_status_ptr_reg;
  642. memset(execlist, 0, sizeof(*execlist));
  643. execlist->vgpu = vgpu;
  644. execlist->ring_id = ring_id;
  645. execlist->slot[0].index = 0;
  646. execlist->slot[1].index = 1;
  647. ctx_status_ptr_reg = execlist_ring_mmio(vgpu->gvt, ring_id,
  648. _EL_OFFSET_STATUS_PTR);
  649. ctx_status_ptr.dw = vgpu_vreg(vgpu, ctx_status_ptr_reg);
  650. ctx_status_ptr.read_ptr = 0;
  651. ctx_status_ptr.write_ptr = 0x7;
  652. vgpu_vreg(vgpu, ctx_status_ptr_reg) = ctx_status_ptr.dw;
  653. }
  654. static void clean_workloads(struct intel_vgpu *vgpu, unsigned long engine_mask)
  655. {
  656. struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
  657. struct intel_engine_cs *engine;
  658. struct intel_vgpu_workload *pos, *n;
  659. unsigned int tmp;
  660. /* free the unsubmited workloads in the queues. */
  661. for_each_engine_masked(engine, dev_priv, engine_mask, tmp) {
  662. list_for_each_entry_safe(pos, n,
  663. &vgpu->workload_q_head[engine->id], list) {
  664. list_del_init(&pos->list);
  665. free_workload(pos);
  666. }
  667. clear_bit(engine->id, vgpu->shadow_ctx_desc_updated);
  668. }
  669. }
  670. void intel_vgpu_clean_execlist(struct intel_vgpu *vgpu)
  671. {
  672. clean_workloads(vgpu, ALL_ENGINES);
  673. kmem_cache_destroy(vgpu->workloads);
  674. }
  675. int intel_vgpu_init_execlist(struct intel_vgpu *vgpu)
  676. {
  677. enum intel_engine_id i;
  678. struct intel_engine_cs *engine;
  679. /* each ring has a virtual execlist engine */
  680. for_each_engine(engine, vgpu->gvt->dev_priv, i) {
  681. init_vgpu_execlist(vgpu, i);
  682. INIT_LIST_HEAD(&vgpu->workload_q_head[i]);
  683. }
  684. vgpu->workloads = kmem_cache_create("gvt-g_vgpu_workload",
  685. sizeof(struct intel_vgpu_workload), 0,
  686. SLAB_HWCACHE_ALIGN,
  687. NULL);
  688. if (!vgpu->workloads)
  689. return -ENOMEM;
  690. return 0;
  691. }
  692. void intel_vgpu_reset_execlist(struct intel_vgpu *vgpu,
  693. unsigned long engine_mask)
  694. {
  695. struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
  696. struct intel_engine_cs *engine;
  697. unsigned int tmp;
  698. clean_workloads(vgpu, engine_mask);
  699. for_each_engine_masked(engine, dev_priv, engine_mask, tmp)
  700. init_vgpu_execlist(vgpu, engine->id);
  701. }