blob: 7c8ff9792f7b4ad0190335c0bdb4e5ca688a2b5b [file] [log] [blame]
Michal Wajdeczko3ea58022019-08-12 09:29:35 +00001// SPDX-License-Identifier: MIT
Alex Daibac427f2015-08-12 15:43:39 +01002/*
3 * Copyright © 2014 Intel Corporation
Alex Daibac427f2015-08-12 15:43:39 +01004 */
Alex Daibac427f2015-08-12 15:43:39 +01005
Michal Wajdeczko9f436c42017-10-04 18:13:40 +00006#include <linux/circ_buf.h>
Chris Wilson31de73502017-03-16 12:56:18 +00007
Chris Wilson10be98a2019-05-28 10:29:49 +01008#include "gem/i915_gem_context.h"
Daniele Ceraolo Spurio43aaadc2021-01-12 18:12:35 -08009#include "gt/gen8_engine_cs.h"
10#include "gt/intel_breadcrumbs.h"
Daniele Ceraolo Spurio0f261b22019-07-13 11:00:11 +010011#include "gt/intel_context.h"
12#include "gt/intel_engine_pm.h"
Daniele Ceraolo Spurio84b1ca22019-07-13 11:00:14 +010013#include "gt/intel_gt.h"
Chris Wilson0669a6e2021-05-21 11:32:15 -070014#include "gt/intel_gt_irq.h"
Chris Wilsonc7302f22019-08-08 21:27:58 +010015#include "gt/intel_gt_pm.h"
Chris Wilsona0d3fdb2020-12-19 02:03:42 +000016#include "gt/intel_lrc.h"
Daniele Ceraolo Spurio43aaadc2021-01-12 18:12:35 -080017#include "gt/intel_mocs.h"
Chris Wilson2871ea82019-10-24 11:03:44 +010018#include "gt/intel_ring.h"
19
Sagar Arun Kamblea2695742017-11-16 19:02:41 +053020#include "intel_guc_submission.h"
Daniele Ceraolo Spurio0f261b22019-07-13 11:00:11 +010021
Michal Wajdeczko9f436c42017-10-04 18:13:40 +000022#include "i915_drv.h"
Jani Nikulaa09d9a82019-08-06 13:07:28 +030023#include "i915_trace.h"
Michal Wajdeczko9f436c42017-10-04 18:13:40 +000024
Alex Daibac427f2015-08-12 15:43:39 +010025/**
Alex Daifeda33e2015-10-19 16:10:54 -070026 * DOC: GuC-based command submission
Dave Gordon44a28b12015-08-12 15:43:41 +010027 *
Daniele Ceraolo Spurio218151e2019-10-14 11:36:01 -070028 * IMPORTANT NOTE: GuC submission is currently not supported in i915. The GuC
29 * firmware is moving to an updated submission interface and we plan to
30 * turn submission back on when that lands. The below documentation (and related
31 * code) matches the old submission model and will be updated as part of the
32 * upgrade to the new flow.
33 *
Oscar Mateob09935a2017-03-22 10:39:53 -070034 * GuC stage descriptor:
Oscar Mateo0d768122017-03-22 10:39:50 -070035 * During initialization, the driver allocates a static pool of 1024 such
Daniele Ceraolo Spurio3c9abe82019-12-05 14:02:42 -080036 * descriptors, and shares them with the GuC. Currently, we only use one
37 * descriptor. This stage descriptor lets the GuC know about the workqueue and
Daniele Ceraolo Spurioe9362e12019-12-05 14:02:41 -080038 * process descriptor. Theoretically, it also lets the GuC know about our HW
39 * contexts (context ID, etc...), but we actually employ a kind of submission
Daniele Ceraolo Spurio3c9abe82019-12-05 14:02:42 -080040 * where the GuC uses the LRCA sent via the work item instead. This is called
Daniele Ceraolo Spurioe9362e12019-12-05 14:02:41 -080041 * a "proxy" submission.
Dave Gordon44a28b12015-08-12 15:43:41 +010042 *
43 * The Scratch registers:
44 * There are 16 MMIO-based registers start from 0xC180. The kernel driver writes
45 * a value to the action register (SOFT_SCRATCH_0) along with any data. It then
46 * triggers an interrupt on the GuC via another register write (0xC4C8).
47 * Firmware writes a success/fail code back to the action register after
48 * processes the request. The kernel driver polls waiting for this update and
49 * then proceeds.
Dave Gordon44a28b12015-08-12 15:43:41 +010050 *
Dave Gordon44a28b12015-08-12 15:43:41 +010051 * Work Items:
52 * There are several types of work items that the host may place into a
53 * workqueue, each with its own requirements and limitations. Currently only
54 * WQ_TYPE_INORDER is needed to support legacy submission via GuC, which
55 * represents in-order queue. The kernel driver packs ring tail pointer and an
56 * ELSP context descriptor dword into Work Item.
Michał Winiarskia0991e12017-10-25 22:00:14 +020057 * See guc_add_request()
Dave Gordon44a28b12015-08-12 15:43:41 +010058 *
59 */
60
Daniele Ceraolo Spurio43aaadc2021-01-12 18:12:35 -080061#define GUC_REQUEST_SIZE 64 /* bytes */
62
Chris Wilsonf6322ed2018-02-22 14:22:29 +000063static inline struct i915_priolist *to_priolist(struct rb_node *rb)
64{
65 return rb_entry(rb, struct i915_priolist, node);
66}
67
Daniele Ceraolo Spurio3c9abe82019-12-05 14:02:42 -080068static struct guc_stage_desc *__get_stage_desc(struct intel_guc *guc, u32 id)
Joonas Lahtinenabddffd2017-03-22 10:39:44 -070069{
Daniele Ceraolo Spurio3c9abe82019-12-05 14:02:42 -080070 struct guc_stage_desc *base = guc->stage_desc_pool_vaddr;
71
72 return &base[id];
Joonas Lahtinenabddffd2017-03-22 10:39:44 -070073}
74
Michał Winiarski89922d02017-10-25 22:00:10 +020075static int guc_stage_desc_pool_create(struct intel_guc *guc)
76{
Daniele Ceraolo Spurio18c094b2019-12-05 14:02:40 -080077 u32 size = PAGE_ALIGN(sizeof(struct guc_stage_desc) *
78 GUC_MAX_STAGE_DESCRIPTORS);
Michał Winiarski89922d02017-10-25 22:00:10 +020079
Daniele Ceraolo Spurio3c9abe82019-12-05 14:02:42 -080080 return intel_guc_allocate_and_map_vma(guc, size, &guc->stage_desc_pool,
81 &guc->stage_desc_pool_vaddr);
Michał Winiarski89922d02017-10-25 22:00:10 +020082}
83
84static void guc_stage_desc_pool_destroy(struct intel_guc *guc)
85{
Chris Wilson6a2f59e2018-07-21 13:50:37 +010086 i915_vma_unpin_and_release(&guc->stage_desc_pool, I915_VMA_RELEASE_MAP);
Michał Winiarski89922d02017-10-25 22:00:10 +020087}
88
Dave Gordon44a28b12015-08-12 15:43:41 +010089/*
Oscar Mateob09935a2017-03-22 10:39:53 -070090 * Initialise/clear the stage descriptor shared with the GuC firmware.
Dave Gordon44a28b12015-08-12 15:43:41 +010091 *
92 * This descriptor tells the GuC where (in GGTT space) to find the important
Daniele Ceraolo Spurio3c9abe82019-12-05 14:02:42 -080093 * data structures related to work submission (process descriptor, write queue,
Daniele Ceraolo Spurioe9362e12019-12-05 14:02:41 -080094 * etc).
Dave Gordon44a28b12015-08-12 15:43:41 +010095 */
Daniele Ceraolo Spurio3c9abe82019-12-05 14:02:42 -080096static void guc_stage_desc_init(struct intel_guc *guc)
Dave Gordon44a28b12015-08-12 15:43:41 +010097{
Oscar Mateob09935a2017-03-22 10:39:53 -070098 struct guc_stage_desc *desc;
Dave Gordon44a28b12015-08-12 15:43:41 +010099
Daniele Ceraolo Spurio3c9abe82019-12-05 14:02:42 -0800100 /* we only use 1 stage desc, so hardcode it to 0 */
101 desc = __get_stage_desc(guc, 0);
Oscar Mateo73b05532017-03-22 10:39:45 -0700102 memset(desc, 0, sizeof(*desc));
Dave Gordon44a28b12015-08-12 15:43:41 +0100103
Sagar Arun Kamblea2695742017-11-16 19:02:41 +0530104 desc->attribute = GUC_STAGE_DESC_ATTR_ACTIVE |
105 GUC_STAGE_DESC_ATTR_KERNEL;
Dave Gordon44a28b12015-08-12 15:43:41 +0100106
Daniele Ceraolo Spurio3c9abe82019-12-05 14:02:42 -0800107 desc->stage_id = 0;
108 desc->priority = GUC_CLIENT_PRIORITY_KMD_NORMAL;
109
Michał Winiarskia529a1c2017-09-18 11:25:35 +0200110 desc->wq_size = GUC_WQ_SIZE;
Dave Gordon44a28b12015-08-12 15:43:41 +0100111}
112
Daniele Ceraolo Spurio3c9abe82019-12-05 14:02:42 -0800113static void guc_stage_desc_fini(struct intel_guc *guc)
Dave Gordon44a28b12015-08-12 15:43:41 +0100114{
Oscar Mateob09935a2017-03-22 10:39:53 -0700115 struct guc_stage_desc *desc;
Dave Gordon44a28b12015-08-12 15:43:41 +0100116
Daniele Ceraolo Spurio3c9abe82019-12-05 14:02:42 -0800117 desc = __get_stage_desc(guc, 0);
Oscar Mateo73b05532017-03-22 10:39:45 -0700118 memset(desc, 0, sizeof(*desc));
Dave Gordon44a28b12015-08-12 15:43:41 +0100119}
120
Chris Wilsone61e0f52018-02-21 09:56:36 +0000121static void guc_add_request(struct intel_guc *guc, struct i915_request *rq)
Michał Winiarskia0991e12017-10-25 22:00:14 +0200122{
Matthew Brost7e5299c2021-01-12 18:12:33 -0800123 /* Leaving stub as this function will be used in future patches */
Michał Winiarskia0991e12017-10-25 22:00:14 +0200124}
125
Michał Winiarskic41937fd2017-10-26 15:35:58 +0200126/*
127 * When we're doing submissions using regular execlists backend, writing to
128 * ELSP from CPU side is enough to make sure that writes to ringbuffer pages
129 * pinned in mappable aperture portion of GGTT are visible to command streamer.
130 * Writes done by GuC on our behalf are not guaranteeing such ordering,
131 * therefore, to ensure the flush, we're issuing a POSTING READ.
132 */
133static void flush_ggtt_writes(struct i915_vma *vma)
134{
Michał Winiarskic41937fd2017-10-26 15:35:58 +0200135 if (i915_vma_is_map_and_fenceable(vma))
Matthew Brosta22198a2019-12-06 17:00:33 -0800136 intel_uncore_posting_read_fw(vma->vm->gt->uncore,
137 GUC_STATUS);
Michał Winiarskic41937fd2017-10-26 15:35:58 +0200138}
139
Chris Wilson22b7a422019-06-20 15:20:51 +0100140static void guc_submit(struct intel_engine_cs *engine,
141 struct i915_request **out,
142 struct i915_request **end)
Dave Gordon44a28b12015-08-12 15:43:41 +0100143{
Daniele Ceraolo Spurio8b5689d2019-07-13 11:00:12 +0100144 struct intel_guc *guc = &engine->gt->uc.guc;
Dave Gordon44a28b12015-08-12 15:43:41 +0100145
Chris Wilson22b7a422019-06-20 15:20:51 +0100146 do {
147 struct i915_request *rq = *out++;
Chris Wilson0c335182017-02-28 11:28:03 +0000148
Chris Wilson22b7a422019-06-20 15:20:51 +0100149 flush_ggtt_writes(rq->ring->vma);
150 guc_add_request(guc, rq);
151 } while (out != end);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100152}
153
Chris Wilson2a694fe2018-04-03 19:35:37 +0100154static inline int rq_prio(const struct i915_request *rq)
155{
Chris Wilsoneec39e42020-05-07 16:23:38 +0100156 return rq->sched.attr.priority;
Chris Wilson2a694fe2018-04-03 19:35:37 +0100157}
158
Chris Wilson22b7a422019-06-20 15:20:51 +0100159static struct i915_request *schedule_in(struct i915_request *rq, int idx)
Chris Wilson2a694fe2018-04-03 19:35:37 +0100160{
Chris Wilson22b7a422019-06-20 15:20:51 +0100161 trace_i915_request_in(rq, idx);
162
Chris Wilson5f15c1e62019-08-12 21:36:26 +0100163 /*
164 * Currently we are not tracking the rq->context being inflight
165 * (ce->inflight = rq->engine). It is only used by the execlists
166 * backend at the moment, a similar counting strategy would be
167 * required if we generalise the inflight tracking.
168 */
Chris Wilson22b7a422019-06-20 15:20:51 +0100169
Chris Wilson93b0e8f2019-11-21 13:05:28 +0000170 __intel_gt_pm_get(rq->engine->gt);
Chris Wilson22b7a422019-06-20 15:20:51 +0100171 return i915_request_get(rq);
Chris Wilson2a694fe2018-04-03 19:35:37 +0100172}
173
Chris Wilson22b7a422019-06-20 15:20:51 +0100174static void schedule_out(struct i915_request *rq)
175{
176 trace_i915_request_out(rq);
177
Stuart Summerse18417b2019-11-20 13:13:21 -0800178 intel_gt_pm_put_async(rq->engine->gt);
Chris Wilson22b7a422019-06-20 15:20:51 +0100179 i915_request_put(rq);
180}
181
182static void __guc_dequeue(struct intel_engine_cs *engine)
Chris Wilson31de73502017-03-16 12:56:18 +0000183{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300184 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilson22b7a422019-06-20 15:20:51 +0100185 struct i915_request **first = execlists->inflight;
186 struct i915_request ** const last_port = first + execlists->port_mask;
187 struct i915_request *last = first[0];
188 struct i915_request **port;
Chris Wilson31de73502017-03-16 12:56:18 +0000189 bool submit = false;
Michał Winiarski85e2fe62017-09-14 10:32:13 +0200190 struct rb_node *rb;
191
Chris Wilson422d7df2019-06-14 17:46:06 +0100192 lockdep_assert_held(&engine->active.lock);
Chris Wilson6486d842018-05-08 22:03:18 +0100193
Chris Wilson22b7a422019-06-20 15:20:51 +0100194 if (last) {
Chris Wilson22b7a422019-06-20 15:20:51 +0100195 if (*++first)
196 return;
Michał Winiarskic41937fd2017-10-26 15:35:58 +0200197
Chris Wilson22b7a422019-06-20 15:20:51 +0100198 last = NULL;
199 }
200
Chris Wilson5f15c1e62019-08-12 21:36:26 +0100201 /*
202 * We write directly into the execlists->inflight queue and don't use
203 * the execlists->pending queue, as we don't have a distinct switch
204 * event.
205 */
Chris Wilson22b7a422019-06-20 15:20:51 +0100206 port = first;
Chris Wilson655250a2018-06-29 08:53:20 +0100207 while ((rb = rb_first_cached(&execlists->queue))) {
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000208 struct i915_priolist *p = to_priolist(rb);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000209 struct i915_request *rq, *rn;
Chris Wilson31de73502017-03-16 12:56:18 +0000210
Chris Wilson2867ff62021-01-20 12:14:38 +0000211 priolist_for_each_request_consume(rq, rn, p) {
Chris Wilson9f3ccd42019-12-20 10:12:29 +0000212 if (last && rq->context != last->context) {
Chris Wilson85f5e1f2018-10-01 13:32:04 +0100213 if (port == last_port)
Chris Wilson6c067572017-05-17 13:10:03 +0100214 goto done;
Chris Wilson31de73502017-03-16 12:56:18 +0000215
Chris Wilson22b7a422019-06-20 15:20:51 +0100216 *port = schedule_in(last,
217 port - execlists->inflight);
Chris Wilson6c067572017-05-17 13:10:03 +0100218 port++;
219 }
220
Chris Wilson85f5e1f2018-10-01 13:32:04 +0100221 list_del_init(&rq->sched.link);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000222 __i915_request_submit(rq);
Chris Wilson6c067572017-05-17 13:10:03 +0100223 submit = true;
Chris Wilson22b7a422019-06-20 15:20:51 +0100224 last = rq;
Chris Wilson31de73502017-03-16 12:56:18 +0000225 }
226
Chris Wilson655250a2018-06-29 08:53:20 +0100227 rb_erase_cached(&p->node, &execlists->queue);
Chris Wilson32eb6bc2019-02-28 10:20:33 +0000228 i915_priolist_free(p);
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000229 }
Chris Wilson6c067572017-05-17 13:10:03 +0100230done:
Chris Wilson4d97cbe02019-01-29 18:54:51 +0000231 execlists->queue_priority_hint =
232 rb ? to_priolist(rb)->priority : INT_MIN;
Chris Wilson22b7a422019-06-20 15:20:51 +0100233 if (submit) {
234 *port = schedule_in(last, port - execlists->inflight);
235 *++port = NULL;
236 guc_submit(engine, first, port);
237 }
238 execlists->active = execlists->inflight;
Chris Wilson31de73502017-03-16 12:56:18 +0000239}
240
Emil Renner Berthing2913fa42021-01-26 16:01:55 +0100241static void guc_submission_tasklet(struct tasklet_struct *t)
Chris Wilson31de73502017-03-16 12:56:18 +0000242{
Emil Renner Berthing2913fa42021-01-26 16:01:55 +0100243 struct intel_engine_cs * const engine =
244 from_tasklet(engine, t, execlists.tasklet);
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300245 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilson22b7a422019-06-20 15:20:51 +0100246 struct i915_request **port, *rq;
Chris Wilsona2bf92e2018-09-25 09:31:59 +0100247 unsigned long flags;
248
Chris Wilson422d7df2019-06-14 17:46:06 +0100249 spin_lock_irqsave(&engine->active.lock, flags);
Chris Wilson31de73502017-03-16 12:56:18 +0000250
Chris Wilson22b7a422019-06-20 15:20:51 +0100251 for (port = execlists->inflight; (rq = *port); port++) {
252 if (!i915_request_completed(rq))
253 break;
Michał Winiarski85e2fe62017-09-14 10:32:13 +0200254
Chris Wilson22b7a422019-06-20 15:20:51 +0100255 schedule_out(rq);
256 }
257 if (port != execlists->inflight) {
258 int idx = port - execlists->inflight;
259 int rem = ARRAY_SIZE(execlists->inflight) - idx;
260 memmove(execlists->inflight, port, rem * sizeof(*port));
Michał Winiarski85e2fe62017-09-14 10:32:13 +0200261 }
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100262
Chris Wilson71b08462019-07-09 17:54:26 -0700263 __guc_dequeue(engine);
Chris Wilsona2bf92e2018-09-25 09:31:59 +0100264
Chris Wilson422d7df2019-06-14 17:46:06 +0100265 spin_unlock_irqrestore(&engine->active.lock, flags);
Chris Wilson31de73502017-03-16 12:56:18 +0000266}
267
Chris Wilson0669a6e2021-05-21 11:32:15 -0700268static void cs_irq_handler(struct intel_engine_cs *engine, u16 iir)
269{
270 if (iir & GT_RENDER_USER_INTERRUPT) {
271 intel_engine_signal_breadcrumbs(engine);
272 tasklet_hi_schedule(&engine->execlists.tasklet);
273 }
274}
275
Chris Wilsoneb8d0f52019-01-25 13:22:28 +0000276static void guc_reset_prepare(struct intel_engine_cs *engine)
Chris Wilson13291152018-05-16 19:33:52 +0100277{
278 struct intel_engine_execlists * const execlists = &engine->execlists;
279
Venkata Sandeep Dhanalakota639f2f22019-12-13 07:51:52 -0800280 ENGINE_TRACE(engine, "\n");
Chris Wilson13291152018-05-16 19:33:52 +0100281
282 /*
283 * Prevent request submission to the hardware until we have
284 * completed the reset in i915_gem_reset_finish(). If a request
285 * is completed by one engine, it may then queue a request
286 * to a second via its execlists->tasklet *just* as we are
287 * calling engine->init_hw() and also writing the ELSP.
288 * Turning off the execlists->tasklet until the reset is over
289 * prevents the race.
290 */
291 __tasklet_disable_sync_once(&execlists->tasklet);
Chris Wilson13291152018-05-16 19:33:52 +0100292}
293
Chris Wilsona0d3fdb2020-12-19 02:03:42 +0000294static void guc_reset_state(struct intel_context *ce,
295 struct intel_engine_cs *engine,
296 u32 head,
297 bool scrub)
298{
299 GEM_BUG_ON(!intel_context_is_pinned(ce));
300
301 /*
302 * We want a simple context + ring to execute the breadcrumb update.
303 * We cannot rely on the context being intact across the GPU hang,
304 * so clear it and rebuild just what we need for the breadcrumb.
305 * All pending requests for this context will be zapped, and any
306 * future request will be after userspace has had the opportunity
307 * to recreate its own state.
308 */
309 if (scrub)
310 lrc_init_regs(ce, engine, true);
311
312 /* Rerun the request; its payload has been neutered (if guilty). */
313 lrc_update_regs(ce, engine, head);
314}
315
Chris Wilsone26b6d42019-12-22 12:07:52 +0000316static void guc_reset_rewind(struct intel_engine_cs *engine, bool stalled)
Chris Wilson292ad252019-04-11 14:05:14 +0100317{
318 struct intel_engine_execlists * const execlists = &engine->execlists;
319 struct i915_request *rq;
320 unsigned long flags;
321
Chris Wilson422d7df2019-06-14 17:46:06 +0100322 spin_lock_irqsave(&engine->active.lock, flags);
Chris Wilson292ad252019-04-11 14:05:14 +0100323
Chris Wilson292ad252019-04-11 14:05:14 +0100324 /* Push back any incomplete requests for replay after the reset. */
325 rq = execlists_unwind_incomplete_requests(execlists);
326 if (!rq)
327 goto out_unlock;
328
329 if (!i915_request_started(rq))
330 stalled = false;
331
Chris Wilsoncb823ed2019-07-12 20:29:53 +0100332 __i915_request_reset(rq, stalled);
Chris Wilsona0d3fdb2020-12-19 02:03:42 +0000333 guc_reset_state(rq->context, engine, rq->head, stalled);
Chris Wilson292ad252019-04-11 14:05:14 +0100334
335out_unlock:
Chris Wilson422d7df2019-06-14 17:46:06 +0100336 spin_unlock_irqrestore(&engine->active.lock, flags);
Chris Wilson292ad252019-04-11 14:05:14 +0100337}
338
Chris Wilsone26b6d42019-12-22 12:07:52 +0000339static void guc_reset_cancel(struct intel_engine_cs *engine)
Chris Wilson292ad252019-04-11 14:05:14 +0100340{
341 struct intel_engine_execlists * const execlists = &engine->execlists;
342 struct i915_request *rq, *rn;
343 struct rb_node *rb;
344 unsigned long flags;
345
Venkata Sandeep Dhanalakota639f2f22019-12-13 07:51:52 -0800346 ENGINE_TRACE(engine, "\n");
Chris Wilson292ad252019-04-11 14:05:14 +0100347
348 /*
349 * Before we call engine->cancel_requests(), we should have exclusive
350 * access to the submission state. This is arranged for us by the
351 * caller disabling the interrupt generation, the tasklet and other
352 * threads that may then access the same state, giving us a free hand
353 * to reset state. However, we still need to let lockdep be aware that
354 * we know this state may be accessed in hardirq context, so we
355 * disable the irq around this manipulation and we want to keep
356 * the spinlock focused on its duties and not accidentally conflate
357 * coverage to the submission's irq state. (Similarly, although we
358 * shouldn't need to disable irq around the manipulation of the
359 * submission's irq state, we also wish to remind ourselves that
360 * it is irq state.)
361 */
Chris Wilson422d7df2019-06-14 17:46:06 +0100362 spin_lock_irqsave(&engine->active.lock, flags);
Chris Wilson292ad252019-04-11 14:05:14 +0100363
Chris Wilson292ad252019-04-11 14:05:14 +0100364 /* Mark all executing requests as skipped. */
Chris Wilson422d7df2019-06-14 17:46:06 +0100365 list_for_each_entry(rq, &engine->active.requests, sched.link) {
Chris Wilson36e191f2020-03-04 12:18:48 +0000366 i915_request_set_error_once(rq, -EIO);
Chris Wilson292ad252019-04-11 14:05:14 +0100367 i915_request_mark_complete(rq);
368 }
369
370 /* Flush the queued requests to the timeline list (for retiring). */
371 while ((rb = rb_first_cached(&execlists->queue))) {
372 struct i915_priolist *p = to_priolist(rb);
Chris Wilson292ad252019-04-11 14:05:14 +0100373
Chris Wilson2867ff62021-01-20 12:14:38 +0000374 priolist_for_each_request_consume(rq, rn, p) {
Chris Wilson292ad252019-04-11 14:05:14 +0100375 list_del_init(&rq->sched.link);
376 __i915_request_submit(rq);
377 dma_fence_set_error(&rq->fence, -EIO);
378 i915_request_mark_complete(rq);
379 }
380
381 rb_erase_cached(&p->node, &execlists->queue);
382 i915_priolist_free(p);
383 }
384
385 /* Remaining _unready_ requests will be nop'ed when submitted */
386
387 execlists->queue_priority_hint = INT_MIN;
388 execlists->queue = RB_ROOT_CACHED;
Chris Wilson292ad252019-04-11 14:05:14 +0100389
Chris Wilson422d7df2019-06-14 17:46:06 +0100390 spin_unlock_irqrestore(&engine->active.lock, flags);
Chris Wilson292ad252019-04-11 14:05:14 +0100391}
392
393static void guc_reset_finish(struct intel_engine_cs *engine)
394{
395 struct intel_engine_execlists * const execlists = &engine->execlists;
396
397 if (__tasklet_enable(&execlists->tasklet))
398 /* And kick in case we missed a new request submission. */
399 tasklet_hi_schedule(&execlists->tasklet);
400
Venkata Sandeep Dhanalakota639f2f22019-12-13 07:51:52 -0800401 ENGINE_TRACE(engine, "depth->%d\n",
402 atomic_read(&execlists->tasklet.count));
Chris Wilson292ad252019-04-11 14:05:14 +0100403}
404
Dave Gordon44a28b12015-08-12 15:43:41 +0100405/*
Oscar Mateo397fce82017-03-22 10:39:52 -0700406 * Set up the memory resources to be shared with the GuC (via the GGTT)
407 * at firmware loading time.
Alex Daibac427f2015-08-12 15:43:39 +0100408 */
Sagar Arun Kambledb14d0c52017-11-16 19:02:39 +0530409int intel_guc_submission_init(struct intel_guc *guc)
Alex Daibac427f2015-08-12 15:43:39 +0100410{
Oscar Mateo3950bf32017-03-22 10:39:46 -0700411 int ret;
Alex Daibac427f2015-08-12 15:43:39 +0100412
Oscar Mateob09935a2017-03-22 10:39:53 -0700413 if (guc->stage_desc_pool)
Oscar Mateo3950bf32017-03-22 10:39:46 -0700414 return 0;
Alex Daibac427f2015-08-12 15:43:39 +0100415
Michał Winiarski89922d02017-10-25 22:00:10 +0200416 ret = guc_stage_desc_pool_create(guc);
417 if (ret)
418 return ret;
Chris Wilson856efd22017-11-06 11:48:33 +0000419 /*
420 * Keep static analysers happy, let them know that we allocated the
421 * vma after testing that it didn't exist earlier.
422 */
423 GEM_BUG_ON(!guc->stage_desc_pool);
Oscar Mateo73b05532017-03-22 10:39:45 -0700424
Alex Daibac427f2015-08-12 15:43:39 +0100425 return 0;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700426}
427
Sagar Arun Kambledb14d0c52017-11-16 19:02:39 +0530428void intel_guc_submission_fini(struct intel_guc *guc)
Oscar Mateo3950bf32017-03-22 10:39:46 -0700429{
Daniele Ceraolo Spurio3c9abe82019-12-05 14:02:42 -0800430 if (guc->stage_desc_pool) {
Chris Wilson6710fcfc2018-07-13 18:26:58 +0100431 guc_stage_desc_pool_destroy(guc);
Daniele Ceraolo Spurio3c9abe82019-12-05 14:02:42 -0800432 }
Chris Wilson4d357af2016-11-29 12:10:23 +0000433}
434
Daniele Ceraolo Spurio43aaadc2021-01-12 18:12:35 -0800435static int guc_context_alloc(struct intel_context *ce)
436{
437 return lrc_alloc(ce, ce->engine);
438}
439
440static int guc_context_pre_pin(struct intel_context *ce,
441 struct i915_gem_ww_ctx *ww,
442 void **vaddr)
443{
444 return lrc_pre_pin(ce, ce->engine, ww, vaddr);
445}
446
447static int guc_context_pin(struct intel_context *ce, void *vaddr)
448{
449 return lrc_pin(ce, ce->engine, vaddr);
450}
451
452static const struct intel_context_ops guc_context_ops = {
453 .alloc = guc_context_alloc,
454
455 .pre_pin = guc_context_pre_pin,
456 .pin = guc_context_pin,
457 .unpin = lrc_unpin,
458 .post_unpin = lrc_post_unpin,
459
460 .enter = intel_context_enter_engine,
461 .exit = intel_context_exit_engine,
462
463 .reset = lrc_reset,
464 .destroy = lrc_destroy,
465};
466
467static int guc_request_alloc(struct i915_request *request)
468{
469 int ret;
470
471 GEM_BUG_ON(!intel_context_is_pinned(request->context));
472
473 /*
474 * Flush enough space to reduce the likelihood of waiting after
475 * we start building the request - in which case we will just
476 * have to repeat work.
477 */
478 request->reserved_space += GUC_REQUEST_SIZE;
479
480 /*
481 * Note that after this point, we have committed to using
482 * this request as it is being used to both track the
483 * state of engine initialisation and liveness of the
484 * golden renderstate above. Think twice before you try
485 * to cancel/unwind this request now.
486 */
487
488 /* Unconditionally invalidate GPU caches and TLBs. */
489 ret = request->engine->emit_flush(request, EMIT_INVALIDATE);
490 if (ret)
491 return ret;
492
493 request->reserved_space -= GUC_REQUEST_SIZE;
494 return 0;
495}
496
Daniele Ceraolo Spurio007c4572021-01-12 18:12:36 -0800497static inline void queue_request(struct intel_engine_cs *engine,
498 struct i915_request *rq,
499 int prio)
500{
501 GEM_BUG_ON(!list_empty(&rq->sched.link));
502 list_add_tail(&rq->sched.link,
503 i915_sched_lookup_priolist(engine, prio));
504 set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
505}
506
507static void guc_submit_request(struct i915_request *rq)
508{
509 struct intel_engine_cs *engine = rq->engine;
510 unsigned long flags;
511
512 /* Will be called from irq-context when using foreign fences. */
513 spin_lock_irqsave(&engine->active.lock, flags);
514
515 queue_request(engine, rq, rq_prio(rq));
516
517 GEM_BUG_ON(RB_EMPTY_ROOT(&engine->execlists.queue.rb_root));
518 GEM_BUG_ON(list_empty(&rq->sched.link));
519
520 tasklet_hi_schedule(&engine->execlists.tasklet);
521
522 spin_unlock_irqrestore(&engine->active.lock, flags);
523}
524
Daniele Ceraolo Spurio43aaadc2021-01-12 18:12:35 -0800525static void sanitize_hwsp(struct intel_engine_cs *engine)
526{
527 struct intel_timeline *tl;
528
529 list_for_each_entry(tl, &engine->status_page.timelines, engine_link)
530 intel_timeline_reset_seqno(tl);
531}
532
533static void guc_sanitize(struct intel_engine_cs *engine)
534{
535 /*
536 * Poison residual state on resume, in case the suspend didn't!
537 *
538 * We have to assume that across suspend/resume (or other loss
539 * of control) that the contents of our pinned buffers has been
540 * lost, replaced by garbage. Since this doesn't always happen,
541 * let's poison such state so that we more quickly spot when
542 * we falsely assume it has been preserved.
543 */
544 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
545 memset(engine->status_page.addr, POISON_INUSE, PAGE_SIZE);
546
547 /*
548 * The kernel_context HWSP is stored in the status_page. As above,
549 * that may be lost on resume/initialisation, and so we need to
550 * reset the value in the HWSP.
551 */
552 sanitize_hwsp(engine);
553
554 /* And scrub the dirty cachelines for the HWSP */
555 clflush_cache_range(engine->status_page.addr, PAGE_SIZE);
556}
557
558static void setup_hwsp(struct intel_engine_cs *engine)
559{
560 intel_engine_set_hwsp_writemask(engine, ~0u); /* HWSTAM */
561
562 ENGINE_WRITE_FW(engine,
563 RING_HWS_PGA,
564 i915_ggtt_offset(engine->status_page.vma));
565}
566
567static void start_engine(struct intel_engine_cs *engine)
568{
569 ENGINE_WRITE_FW(engine,
570 RING_MODE_GEN7,
571 _MASKED_BIT_ENABLE(GEN11_GFX_DISABLE_LEGACY_MODE));
572
573 ENGINE_WRITE_FW(engine, RING_MI_MODE, _MASKED_BIT_DISABLE(STOP_RING));
574 ENGINE_POSTING_READ(engine, RING_MI_MODE);
575}
576
577static int guc_resume(struct intel_engine_cs *engine)
578{
579 assert_forcewakes_active(engine->uncore, FORCEWAKE_ALL);
580
581 intel_mocs_init_engine(engine);
582
583 intel_breadcrumbs_reset(engine->breadcrumbs);
584
585 setup_hwsp(engine);
586 start_engine(engine);
587
588 return 0;
589}
590
Chris Wilson209b7952018-07-17 21:29:32 +0100591static void guc_set_default_submission(struct intel_engine_cs *engine)
592{
Daniele Ceraolo Spurio007c4572021-01-12 18:12:36 -0800593 engine->submit_request = guc_submit_request;
Chris Wilson209b7952018-07-17 21:29:32 +0100594}
595
Daniele Ceraolo Spurio43aaadc2021-01-12 18:12:35 -0800596static void guc_release(struct intel_engine_cs *engine)
597{
598 engine->sanitize = NULL; /* no longer in control, nothing to sanitize */
599
600 tasklet_kill(&engine->execlists.tasklet);
601
602 intel_engine_cleanup_common(engine);
603 lrc_fini_wa_ctx(engine);
604}
605
606static void guc_default_vfuncs(struct intel_engine_cs *engine)
607{
608 /* Default vfuncs which can be overridden by each engine. */
609
610 engine->resume = guc_resume;
611
612 engine->cops = &guc_context_ops;
613 engine->request_alloc = guc_request_alloc;
614
Chris Wilson0db36332021-05-21 11:32:13 -0700615 engine->schedule = i915_schedule;
616
617 engine->reset.prepare = guc_reset_prepare;
618 engine->reset.rewind = guc_reset_rewind;
619 engine->reset.cancel = guc_reset_cancel;
620 engine->reset.finish = guc_reset_finish;
621
Daniele Ceraolo Spurio43aaadc2021-01-12 18:12:35 -0800622 engine->emit_flush = gen8_emit_flush_xcs;
623 engine->emit_init_breadcrumb = gen8_emit_init_breadcrumb;
624 engine->emit_fini_breadcrumb = gen8_emit_fini_breadcrumb_xcs;
Lucas De Marchic8167232021-06-05 08:53:52 -0700625 if (GRAPHICS_VER(engine->i915) >= 12) {
Daniele Ceraolo Spurio43aaadc2021-01-12 18:12:35 -0800626 engine->emit_fini_breadcrumb = gen12_emit_fini_breadcrumb_xcs;
627 engine->emit_flush = gen12_emit_flush_xcs;
628 }
629 engine->set_default_submission = guc_set_default_submission;
Chris Wilson0db36332021-05-21 11:32:13 -0700630
Chris Wilson0db36332021-05-21 11:32:13 -0700631 engine->flags |= I915_ENGINE_HAS_PREEMPTION;
632
633 /*
634 * TODO: GuC supports timeslicing and semaphores as well, but they're
635 * handled by the firmware so some minor tweaks are required before
636 * enabling.
637 *
638 * engine->flags |= I915_ENGINE_HAS_TIMESLICES;
639 * engine->flags |= I915_ENGINE_HAS_SEMAPHORES;
640 */
641
642 engine->emit_bb_start = gen8_emit_bb_start;
Daniele Ceraolo Spurio43aaadc2021-01-12 18:12:35 -0800643}
644
645static void rcs_submission_override(struct intel_engine_cs *engine)
646{
Lucas De Marchic8167232021-06-05 08:53:52 -0700647 switch (GRAPHICS_VER(engine->i915)) {
Daniele Ceraolo Spurio43aaadc2021-01-12 18:12:35 -0800648 case 12:
649 engine->emit_flush = gen12_emit_flush_rcs;
650 engine->emit_fini_breadcrumb = gen12_emit_fini_breadcrumb_rcs;
651 break;
652 case 11:
653 engine->emit_flush = gen11_emit_flush_rcs;
654 engine->emit_fini_breadcrumb = gen11_emit_fini_breadcrumb_rcs;
655 break;
656 default:
657 engine->emit_flush = gen8_emit_flush_rcs;
658 engine->emit_fini_breadcrumb = gen8_emit_fini_breadcrumb_rcs;
659 break;
660 }
661}
662
663static inline void guc_default_irqs(struct intel_engine_cs *engine)
664{
665 engine->irq_keep_mask = GT_RENDER_USER_INTERRUPT;
Chris Wilson0669a6e2021-05-21 11:32:15 -0700666 intel_engine_set_irq_handler(engine, cs_irq_handler);
Daniele Ceraolo Spurio43aaadc2021-01-12 18:12:35 -0800667}
668
669int intel_guc_submission_setup(struct intel_engine_cs *engine)
670{
671 struct drm_i915_private *i915 = engine->i915;
672
673 /*
674 * The setup relies on several assumptions (e.g. irqs always enabled)
675 * that are only valid on gen11+
676 */
Lucas De Marchic8167232021-06-05 08:53:52 -0700677 GEM_BUG_ON(GRAPHICS_VER(i915) < 11);
Daniele Ceraolo Spurio43aaadc2021-01-12 18:12:35 -0800678
Emil Renner Berthing2913fa42021-01-26 16:01:55 +0100679 tasklet_setup(&engine->execlists.tasklet, guc_submission_tasklet);
Daniele Ceraolo Spurio43aaadc2021-01-12 18:12:35 -0800680
681 guc_default_vfuncs(engine);
682 guc_default_irqs(engine);
683
684 if (engine->class == RENDER_CLASS)
685 rcs_submission_override(engine);
686
687 lrc_init_wa_ctx(engine);
688
689 /* Finally, take ownership and responsibility for cleanup! */
690 engine->sanitize = guc_sanitize;
691 engine->release = guc_release;
692
693 return 0;
694}
695
Daniele Ceraolo Spurioe9362e12019-12-05 14:02:41 -0800696void intel_guc_submission_enable(struct intel_guc *guc)
Dave Gordon44a28b12015-08-12 15:43:41 +0100697{
Daniele Ceraolo Spurio3c9abe82019-12-05 14:02:42 -0800698 guc_stage_desc_init(guc);
Dave Gordon44a28b12015-08-12 15:43:41 +0100699}
700
Sagar Arun Kambledb14d0c52017-11-16 19:02:39 +0530701void intel_guc_submission_disable(struct intel_guc *guc)
Dave Gordon44a28b12015-08-12 15:43:41 +0100702{
Daniele Ceraolo Spurio84b1ca22019-07-13 11:00:14 +0100703 struct intel_gt *gt = guc_to_gt(guc);
Dave Gordon44a28b12015-08-12 15:43:41 +0100704
Daniele Ceraolo Spurio84b1ca22019-07-13 11:00:14 +0100705 GEM_BUG_ON(gt->awake); /* GT should be parked first */
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100706
Daniele Ceraolo Spurio3c9abe82019-12-05 14:02:42 -0800707 /* Note: By the time we're here, GuC may have already been reset */
708
Daniele Ceraolo Spurio3c9abe82019-12-05 14:02:42 -0800709 guc_stage_desc_fini(guc);
Dave Gordon44a28b12015-08-12 15:43:41 +0100710}
Michel Thierry55bd6bd2017-11-16 14:06:31 -0800711
Daniele Ceraolo Spurio202c98e2020-02-18 14:33:24 -0800712static bool __guc_submission_selected(struct intel_guc *guc)
Michal Wajdeczko724df642019-07-31 22:33:20 +0000713{
Jani Nikula8a25c4b2020-06-18 18:04:02 +0300714 struct drm_i915_private *i915 = guc_to_gt(guc)->i915;
715
Daniele Ceraolo Spurio202c98e2020-02-18 14:33:24 -0800716 if (!intel_guc_submission_is_supported(guc))
Michal Wajdeczko724df642019-07-31 22:33:20 +0000717 return false;
718
Jani Nikula8a25c4b2020-06-18 18:04:02 +0300719 return i915->params.enable_guc & ENABLE_GUC_SUBMISSION;
Michal Wajdeczko724df642019-07-31 22:33:20 +0000720}
721
722void intel_guc_submission_init_early(struct intel_guc *guc)
723{
Daniele Ceraolo Spurio202c98e2020-02-18 14:33:24 -0800724 guc->submission_selected = __guc_submission_selected(guc);
Michal Wajdeczko724df642019-07-31 22:33:20 +0000725}