execlist.c 24 KB

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