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