scheduler.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. * Zhi Wang <zhi.a.wang@intel.com>
  25. *
  26. * Contributors:
  27. * Ping Gao <ping.a.gao@intel.com>
  28. * Tina Zhang <tina.zhang@intel.com>
  29. * Chanbin Du <changbin.du@intel.com>
  30. * Min He <min.he@intel.com>
  31. * Bing Niu <bing.niu@intel.com>
  32. * Zhenyu Wang <zhenyuw@linux.intel.com>
  33. *
  34. */
  35. #include <linux/kthread.h>
  36. #include "i915_drv.h"
  37. #include "gvt.h"
  38. #define RING_CTX_OFF(x) \
  39. offsetof(struct execlist_ring_context, x)
  40. static void set_context_pdp_root_pointer(
  41. struct execlist_ring_context *ring_context,
  42. u32 pdp[8])
  43. {
  44. struct execlist_mmio_pair *pdp_pair = &ring_context->pdp3_UDW;
  45. int i;
  46. for (i = 0; i < 8; i++)
  47. pdp_pair[i].val = pdp[7 - i];
  48. }
  49. static int populate_shadow_context(struct intel_vgpu_workload *workload)
  50. {
  51. struct intel_vgpu *vgpu = workload->vgpu;
  52. struct intel_gvt *gvt = vgpu->gvt;
  53. int ring_id = workload->ring_id;
  54. struct i915_gem_context *shadow_ctx = workload->vgpu->shadow_ctx;
  55. struct drm_i915_gem_object *ctx_obj =
  56. shadow_ctx->engine[ring_id].state->obj;
  57. struct execlist_ring_context *shadow_ring_context;
  58. struct page *page;
  59. void *dst;
  60. unsigned long context_gpa, context_page_num;
  61. int i;
  62. gvt_dbg_sched("ring id %d workload lrca %x", ring_id,
  63. workload->ctx_desc.lrca);
  64. context_page_num = intel_lr_context_size(
  65. gvt->dev_priv->engine[ring_id]);
  66. context_page_num = context_page_num >> PAGE_SHIFT;
  67. if (IS_BROADWELL(gvt->dev_priv) && ring_id == RCS)
  68. context_page_num = 19;
  69. i = 2;
  70. while (i < context_page_num) {
  71. context_gpa = intel_vgpu_gma_to_gpa(vgpu->gtt.ggtt_mm,
  72. (u32)((workload->ctx_desc.lrca + i) <<
  73. GTT_PAGE_SHIFT));
  74. if (context_gpa == INTEL_GVT_INVALID_ADDR) {
  75. gvt_err("Invalid guest context descriptor\n");
  76. return -EINVAL;
  77. }
  78. page = i915_gem_object_get_page(ctx_obj, LRC_PPHWSP_PN + i);
  79. dst = kmap_atomic(page);
  80. intel_gvt_hypervisor_read_gpa(vgpu, context_gpa, dst,
  81. GTT_PAGE_SIZE);
  82. kunmap_atomic(dst);
  83. i++;
  84. }
  85. page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN);
  86. shadow_ring_context = kmap_atomic(page);
  87. #define COPY_REG(name) \
  88. intel_gvt_hypervisor_read_gpa(vgpu, workload->ring_context_gpa \
  89. + RING_CTX_OFF(name.val), &shadow_ring_context->name.val, 4)
  90. COPY_REG(ctx_ctrl);
  91. COPY_REG(ctx_timestamp);
  92. if (ring_id == RCS) {
  93. COPY_REG(bb_per_ctx_ptr);
  94. COPY_REG(rcs_indirect_ctx);
  95. COPY_REG(rcs_indirect_ctx_offset);
  96. }
  97. #undef COPY_REG
  98. set_context_pdp_root_pointer(shadow_ring_context,
  99. workload->shadow_mm->shadow_page_table);
  100. intel_gvt_hypervisor_read_gpa(vgpu,
  101. workload->ring_context_gpa +
  102. sizeof(*shadow_ring_context),
  103. (void *)shadow_ring_context +
  104. sizeof(*shadow_ring_context),
  105. GTT_PAGE_SIZE - sizeof(*shadow_ring_context));
  106. kunmap_atomic(shadow_ring_context);
  107. return 0;
  108. }
  109. static int shadow_context_status_change(struct notifier_block *nb,
  110. unsigned long action, void *data)
  111. {
  112. struct intel_vgpu *vgpu = container_of(nb,
  113. struct intel_vgpu, shadow_ctx_notifier_block);
  114. struct drm_i915_gem_request *req =
  115. (struct drm_i915_gem_request *)data;
  116. struct intel_gvt_workload_scheduler *scheduler =
  117. &vgpu->gvt->scheduler;
  118. struct intel_vgpu_workload *workload =
  119. scheduler->current_workload[req->engine->id];
  120. switch (action) {
  121. case INTEL_CONTEXT_SCHEDULE_IN:
  122. intel_gvt_load_render_mmio(workload->vgpu,
  123. workload->ring_id);
  124. atomic_set(&workload->shadow_ctx_active, 1);
  125. break;
  126. case INTEL_CONTEXT_SCHEDULE_OUT:
  127. intel_gvt_restore_render_mmio(workload->vgpu,
  128. workload->ring_id);
  129. atomic_set(&workload->shadow_ctx_active, 0);
  130. break;
  131. default:
  132. WARN_ON(1);
  133. return NOTIFY_OK;
  134. }
  135. wake_up(&workload->shadow_ctx_status_wq);
  136. return NOTIFY_OK;
  137. }
  138. static int dispatch_workload(struct intel_vgpu_workload *workload)
  139. {
  140. struct intel_vgpu *vgpu = workload->vgpu;
  141. struct intel_gvt *gvt = vgpu->gvt;
  142. int ring_id = workload->ring_id;
  143. struct i915_gem_context *shadow_ctx = workload->vgpu->shadow_ctx;
  144. struct drm_i915_private *dev_priv = workload->vgpu->gvt->dev_priv;
  145. struct drm_i915_gem_request *rq;
  146. int ret;
  147. gvt_dbg_sched("ring id %d prepare to dispatch workload %p\n",
  148. ring_id, workload);
  149. shadow_ctx->desc_template = workload->ctx_desc.addressing_mode <<
  150. GEN8_CTX_ADDRESSING_MODE_SHIFT;
  151. rq = i915_gem_request_alloc(dev_priv->engine[ring_id], shadow_ctx);
  152. if (IS_ERR(rq)) {
  153. gvt_err("fail to allocate gem request\n");
  154. workload->status = PTR_ERR(rq);
  155. return workload->status;
  156. }
  157. gvt_dbg_sched("ring id %d get i915 gem request %p\n", ring_id, rq);
  158. workload->req = i915_gem_request_get(rq);
  159. mutex_lock(&gvt->lock);
  160. ret = intel_gvt_scan_and_shadow_workload(workload);
  161. if (ret)
  162. goto err;
  163. ret = intel_gvt_scan_and_shadow_wa_ctx(&workload->wa_ctx);
  164. if (ret)
  165. goto err;
  166. ret = populate_shadow_context(workload);
  167. if (ret)
  168. goto err;
  169. if (workload->prepare) {
  170. ret = workload->prepare(workload);
  171. if (ret)
  172. goto err;
  173. }
  174. mutex_unlock(&gvt->lock);
  175. gvt_dbg_sched("ring id %d submit workload to i915 %p\n",
  176. ring_id, workload->req);
  177. i915_add_request_no_flush(rq);
  178. workload->dispatched = true;
  179. return 0;
  180. err:
  181. workload->status = ret;
  182. mutex_unlock(&gvt->lock);
  183. i915_add_request_no_flush(rq);
  184. return ret;
  185. }
  186. static struct intel_vgpu_workload *pick_next_workload(
  187. struct intel_gvt *gvt, int ring_id)
  188. {
  189. struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
  190. struct intel_vgpu_workload *workload = NULL;
  191. mutex_lock(&gvt->lock);
  192. /*
  193. * no current vgpu / will be scheduled out / no workload
  194. * bail out
  195. */
  196. if (!scheduler->current_vgpu) {
  197. gvt_dbg_sched("ring id %d stop - no current vgpu\n", ring_id);
  198. goto out;
  199. }
  200. if (scheduler->need_reschedule) {
  201. gvt_dbg_sched("ring id %d stop - will reschedule\n", ring_id);
  202. goto out;
  203. }
  204. if (list_empty(workload_q_head(scheduler->current_vgpu, ring_id))) {
  205. gvt_dbg_sched("ring id %d stop - no available workload\n",
  206. ring_id);
  207. goto out;
  208. }
  209. /*
  210. * still have current workload, maybe the workload disptacher
  211. * fail to submit it for some reason, resubmit it.
  212. */
  213. if (scheduler->current_workload[ring_id]) {
  214. workload = scheduler->current_workload[ring_id];
  215. gvt_dbg_sched("ring id %d still have current workload %p\n",
  216. ring_id, workload);
  217. goto out;
  218. }
  219. /*
  220. * pick a workload as current workload
  221. * once current workload is set, schedule policy routines
  222. * will wait the current workload is finished when trying to
  223. * schedule out a vgpu.
  224. */
  225. scheduler->current_workload[ring_id] = container_of(
  226. workload_q_head(scheduler->current_vgpu, ring_id)->next,
  227. struct intel_vgpu_workload, list);
  228. workload = scheduler->current_workload[ring_id];
  229. gvt_dbg_sched("ring id %d pick new workload %p\n", ring_id, workload);
  230. atomic_inc(&workload->vgpu->running_workload_num);
  231. out:
  232. mutex_unlock(&gvt->lock);
  233. return workload;
  234. }
  235. static void update_guest_context(struct intel_vgpu_workload *workload)
  236. {
  237. struct intel_vgpu *vgpu = workload->vgpu;
  238. struct intel_gvt *gvt = vgpu->gvt;
  239. int ring_id = workload->ring_id;
  240. struct i915_gem_context *shadow_ctx = workload->vgpu->shadow_ctx;
  241. struct drm_i915_gem_object *ctx_obj =
  242. shadow_ctx->engine[ring_id].state->obj;
  243. struct execlist_ring_context *shadow_ring_context;
  244. struct page *page;
  245. void *src;
  246. unsigned long context_gpa, context_page_num;
  247. int i;
  248. gvt_dbg_sched("ring id %d workload lrca %x\n", ring_id,
  249. workload->ctx_desc.lrca);
  250. context_page_num = intel_lr_context_size(
  251. gvt->dev_priv->engine[ring_id]);
  252. context_page_num = context_page_num >> PAGE_SHIFT;
  253. if (IS_BROADWELL(gvt->dev_priv) && ring_id == RCS)
  254. context_page_num = 19;
  255. i = 2;
  256. while (i < context_page_num) {
  257. context_gpa = intel_vgpu_gma_to_gpa(vgpu->gtt.ggtt_mm,
  258. (u32)((workload->ctx_desc.lrca + i) <<
  259. GTT_PAGE_SHIFT));
  260. if (context_gpa == INTEL_GVT_INVALID_ADDR) {
  261. gvt_err("invalid guest context descriptor\n");
  262. return;
  263. }
  264. page = i915_gem_object_get_page(ctx_obj, LRC_PPHWSP_PN + i);
  265. src = kmap_atomic(page);
  266. intel_gvt_hypervisor_write_gpa(vgpu, context_gpa, src,
  267. GTT_PAGE_SIZE);
  268. kunmap_atomic(src);
  269. i++;
  270. }
  271. intel_gvt_hypervisor_write_gpa(vgpu, workload->ring_context_gpa +
  272. RING_CTX_OFF(ring_header.val), &workload->rb_tail, 4);
  273. page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN);
  274. shadow_ring_context = kmap_atomic(page);
  275. #define COPY_REG(name) \
  276. intel_gvt_hypervisor_write_gpa(vgpu, workload->ring_context_gpa + \
  277. RING_CTX_OFF(name.val), &shadow_ring_context->name.val, 4)
  278. COPY_REG(ctx_ctrl);
  279. COPY_REG(ctx_timestamp);
  280. #undef COPY_REG
  281. intel_gvt_hypervisor_write_gpa(vgpu,
  282. workload->ring_context_gpa +
  283. sizeof(*shadow_ring_context),
  284. (void *)shadow_ring_context +
  285. sizeof(*shadow_ring_context),
  286. GTT_PAGE_SIZE - sizeof(*shadow_ring_context));
  287. kunmap_atomic(shadow_ring_context);
  288. }
  289. static void complete_current_workload(struct intel_gvt *gvt, int ring_id)
  290. {
  291. struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
  292. struct intel_vgpu_workload *workload;
  293. int event;
  294. mutex_lock(&gvt->lock);
  295. workload = scheduler->current_workload[ring_id];
  296. if (!workload->status && !workload->vgpu->resetting) {
  297. wait_event(workload->shadow_ctx_status_wq,
  298. !atomic_read(&workload->shadow_ctx_active));
  299. update_guest_context(workload);
  300. for_each_set_bit(event, workload->pending_events,
  301. INTEL_GVT_EVENT_MAX)
  302. intel_vgpu_trigger_virtual_event(workload->vgpu,
  303. event);
  304. }
  305. gvt_dbg_sched("ring id %d complete workload %p status %d\n",
  306. ring_id, workload, workload->status);
  307. scheduler->current_workload[ring_id] = NULL;
  308. atomic_dec(&workload->vgpu->running_workload_num);
  309. list_del_init(&workload->list);
  310. workload->complete(workload);
  311. wake_up(&scheduler->workload_complete_wq);
  312. mutex_unlock(&gvt->lock);
  313. }
  314. struct workload_thread_param {
  315. struct intel_gvt *gvt;
  316. int ring_id;
  317. };
  318. static DEFINE_MUTEX(scheduler_mutex);
  319. static int workload_thread(void *priv)
  320. {
  321. struct workload_thread_param *p = (struct workload_thread_param *)priv;
  322. struct intel_gvt *gvt = p->gvt;
  323. int ring_id = p->ring_id;
  324. struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
  325. struct intel_vgpu_workload *workload = NULL;
  326. long lret;
  327. int ret;
  328. bool need_force_wake = IS_SKYLAKE(gvt->dev_priv);
  329. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  330. kfree(p);
  331. gvt_dbg_core("workload thread for ring %d started\n", ring_id);
  332. while (!kthread_should_stop()) {
  333. add_wait_queue(&scheduler->waitq[ring_id], &wait);
  334. do {
  335. workload = pick_next_workload(gvt, ring_id);
  336. if (workload)
  337. break;
  338. wait_woken(&wait, TASK_INTERRUPTIBLE,
  339. MAX_SCHEDULE_TIMEOUT);
  340. } while (!kthread_should_stop());
  341. remove_wait_queue(&scheduler->waitq[ring_id], &wait);
  342. if (!workload)
  343. break;
  344. mutex_lock(&scheduler_mutex);
  345. gvt_dbg_sched("ring id %d next workload %p vgpu %d\n",
  346. workload->ring_id, workload,
  347. workload->vgpu->id);
  348. intel_runtime_pm_get(gvt->dev_priv);
  349. gvt_dbg_sched("ring id %d will dispatch workload %p\n",
  350. workload->ring_id, workload);
  351. if (need_force_wake)
  352. intel_uncore_forcewake_get(gvt->dev_priv,
  353. FORCEWAKE_ALL);
  354. mutex_lock(&gvt->dev_priv->drm.struct_mutex);
  355. ret = dispatch_workload(workload);
  356. mutex_unlock(&gvt->dev_priv->drm.struct_mutex);
  357. if (ret) {
  358. gvt_err("fail to dispatch workload, skip\n");
  359. goto complete;
  360. }
  361. gvt_dbg_sched("ring id %d wait workload %p\n",
  362. workload->ring_id, workload);
  363. lret = i915_wait_request(workload->req,
  364. 0, MAX_SCHEDULE_TIMEOUT);
  365. if (lret < 0) {
  366. workload->status = lret;
  367. gvt_err("fail to wait workload, skip\n");
  368. }
  369. complete:
  370. gvt_dbg_sched("will complete workload %p\n, status: %d\n",
  371. workload, workload->status);
  372. mutex_lock(&gvt->dev_priv->drm.struct_mutex);
  373. complete_current_workload(gvt, ring_id);
  374. mutex_unlock(&gvt->dev_priv->drm.struct_mutex);
  375. i915_gem_request_put(fetch_and_zero(&workload->req));
  376. if (need_force_wake)
  377. intel_uncore_forcewake_put(gvt->dev_priv,
  378. FORCEWAKE_ALL);
  379. intel_runtime_pm_put(gvt->dev_priv);
  380. mutex_unlock(&scheduler_mutex);
  381. }
  382. return 0;
  383. }
  384. void intel_gvt_wait_vgpu_idle(struct intel_vgpu *vgpu)
  385. {
  386. struct intel_gvt *gvt = vgpu->gvt;
  387. struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
  388. if (atomic_read(&vgpu->running_workload_num)) {
  389. gvt_dbg_sched("wait vgpu idle\n");
  390. wait_event(scheduler->workload_complete_wq,
  391. !atomic_read(&vgpu->running_workload_num));
  392. }
  393. }
  394. void intel_gvt_clean_workload_scheduler(struct intel_gvt *gvt)
  395. {
  396. struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
  397. int i;
  398. gvt_dbg_core("clean workload scheduler\n");
  399. for (i = 0; i < I915_NUM_ENGINES; i++) {
  400. if (scheduler->thread[i]) {
  401. kthread_stop(scheduler->thread[i]);
  402. scheduler->thread[i] = NULL;
  403. }
  404. }
  405. }
  406. int intel_gvt_init_workload_scheduler(struct intel_gvt *gvt)
  407. {
  408. struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
  409. struct workload_thread_param *param = NULL;
  410. int ret;
  411. int i;
  412. gvt_dbg_core("init workload scheduler\n");
  413. init_waitqueue_head(&scheduler->workload_complete_wq);
  414. for (i = 0; i < I915_NUM_ENGINES; i++) {
  415. /* check ring mask at init time */
  416. if (!HAS_ENGINE(gvt->dev_priv, i))
  417. continue;
  418. init_waitqueue_head(&scheduler->waitq[i]);
  419. param = kzalloc(sizeof(*param), GFP_KERNEL);
  420. if (!param) {
  421. ret = -ENOMEM;
  422. goto err;
  423. }
  424. param->gvt = gvt;
  425. param->ring_id = i;
  426. scheduler->thread[i] = kthread_run(workload_thread, param,
  427. "gvt workload %d", i);
  428. if (IS_ERR(scheduler->thread[i])) {
  429. gvt_err("fail to create workload thread\n");
  430. ret = PTR_ERR(scheduler->thread[i]);
  431. goto err;
  432. }
  433. }
  434. return 0;
  435. err:
  436. intel_gvt_clean_workload_scheduler(gvt);
  437. kfree(param);
  438. param = NULL;
  439. return ret;
  440. }
  441. void intel_vgpu_clean_gvt_context(struct intel_vgpu *vgpu)
  442. {
  443. struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
  444. atomic_notifier_chain_unregister(&vgpu->shadow_ctx->status_notifier,
  445. &vgpu->shadow_ctx_notifier_block);
  446. mutex_lock(&dev_priv->drm.struct_mutex);
  447. /* a little hacky to mark as ctx closed */
  448. vgpu->shadow_ctx->closed = true;
  449. i915_gem_context_put(vgpu->shadow_ctx);
  450. mutex_unlock(&dev_priv->drm.struct_mutex);
  451. }
  452. int intel_vgpu_init_gvt_context(struct intel_vgpu *vgpu)
  453. {
  454. atomic_set(&vgpu->running_workload_num, 0);
  455. vgpu->shadow_ctx = i915_gem_context_create_gvt(
  456. &vgpu->gvt->dev_priv->drm);
  457. if (IS_ERR(vgpu->shadow_ctx))
  458. return PTR_ERR(vgpu->shadow_ctx);
  459. vgpu->shadow_ctx->engine[RCS].initialised = true;
  460. vgpu->shadow_ctx_notifier_block.notifier_call =
  461. shadow_context_status_change;
  462. atomic_notifier_chain_register(&vgpu->shadow_ctx->status_notifier,
  463. &vgpu->shadow_ctx_notifier_block);
  464. return 0;
  465. }