]> git.ipfire.org Git - thirdparty/linux.git/blame - drivers/gpu/drm/i915/intel_lrc.c
drm/i915: Flush the ring stop bit after clearing RING_HEAD in reset
[thirdparty/linux.git] / drivers / gpu / drm / i915 / intel_lrc.c
CommitLineData
b20385f1
OM
1/*
2 * Copyright © 2014 Intel Corporation
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Ben Widawsky <ben@bwidawsk.net>
25 * Michel Thierry <michel.thierry@intel.com>
26 * Thomas Daniel <thomas.daniel@intel.com>
27 * Oscar Mateo <oscar.mateo@intel.com>
28 *
29 */
30
73e4d07f
OM
31/**
32 * DOC: Logical Rings, Logical Ring Contexts and Execlists
33 *
34 * Motivation:
b20385f1
OM
35 * GEN8 brings an expansion of the HW contexts: "Logical Ring Contexts".
36 * These expanded contexts enable a number of new abilities, especially
37 * "Execlists" (also implemented in this file).
38 *
73e4d07f
OM
39 * One of the main differences with the legacy HW contexts is that logical
40 * ring contexts incorporate many more things to the context's state, like
41 * PDPs or ringbuffer control registers:
42 *
43 * The reason why PDPs are included in the context is straightforward: as
44 * PPGTTs (per-process GTTs) are actually per-context, having the PDPs
45 * contained there mean you don't need to do a ppgtt->switch_mm yourself,
46 * instead, the GPU will do it for you on the context switch.
47 *
48 * But, what about the ringbuffer control registers (head, tail, etc..)?
49 * shouldn't we just need a set of those per engine command streamer? This is
50 * where the name "Logical Rings" starts to make sense: by virtualizing the
51 * rings, the engine cs shifts to a new "ring buffer" with every context
52 * switch. When you want to submit a workload to the GPU you: A) choose your
53 * context, B) find its appropriate virtualized ring, C) write commands to it
54 * and then, finally, D) tell the GPU to switch to that context.
55 *
56 * Instead of the legacy MI_SET_CONTEXT, the way you tell the GPU to switch
57 * to a contexts is via a context execution list, ergo "Execlists".
58 *
59 * LRC implementation:
60 * Regarding the creation of contexts, we have:
61 *
62 * - One global default context.
63 * - One local default context for each opened fd.
64 * - One local extra context for each context create ioctl call.
65 *
66 * Now that ringbuffers belong per-context (and not per-engine, like before)
67 * and that contexts are uniquely tied to a given engine (and not reusable,
68 * like before) we need:
69 *
70 * - One ringbuffer per-engine inside each context.
71 * - One backing object per-engine inside each context.
72 *
73 * The global default context starts its life with these new objects fully
74 * allocated and populated. The local default context for each opened fd is
75 * more complex, because we don't know at creation time which engine is going
76 * to use them. To handle this, we have implemented a deferred creation of LR
77 * contexts:
78 *
79 * The local context starts its life as a hollow or blank holder, that only
80 * gets populated for a given engine once we receive an execbuffer. If later
81 * on we receive another execbuffer ioctl for the same context but a different
82 * engine, we allocate/populate a new ringbuffer and context backing object and
83 * so on.
84 *
85 * Finally, regarding local contexts created using the ioctl call: as they are
86 * only allowed with the render ring, we can allocate & populate them right
87 * away (no need to defer anything, at least for now).
88 *
89 * Execlists implementation:
b20385f1
OM
90 * Execlists are the new method by which, on gen8+ hardware, workloads are
91 * submitted for execution (as opposed to the legacy, ringbuffer-based, method).
73e4d07f
OM
92 * This method works as follows:
93 *
94 * When a request is committed, its commands (the BB start and any leading or
95 * trailing commands, like the seqno breadcrumbs) are placed in the ringbuffer
96 * for the appropriate context. The tail pointer in the hardware context is not
97 * updated at this time, but instead, kept by the driver in the ringbuffer
98 * structure. A structure representing this request is added to a request queue
99 * for the appropriate engine: this structure contains a copy of the context's
100 * tail after the request was written to the ring buffer and a pointer to the
101 * context itself.
102 *
103 * If the engine's request queue was empty before the request was added, the
104 * queue is processed immediately. Otherwise the queue will be processed during
105 * a context switch interrupt. In any case, elements on the queue will get sent
106 * (in pairs) to the GPU's ExecLists Submit Port (ELSP, for short) with a
107 * globally unique 20-bits submission ID.
108 *
109 * When execution of a request completes, the GPU updates the context status
110 * buffer with a context complete event and generates a context switch interrupt.
111 * During the interrupt handling, the driver examines the events in the buffer:
112 * for each context complete event, if the announced ID matches that on the head
113 * of the request queue, then that request is retired and removed from the queue.
114 *
115 * After processing, if any requests were retired and the queue is not empty
116 * then a new execution list can be submitted. The two requests at the front of
117 * the queue are next to be submitted but since a context may not occur twice in
118 * an execution list, if subsequent requests have the same ID as the first then
119 * the two requests must be combined. This is done simply by discarding requests
120 * at the head of the queue until either only one requests is left (in which case
121 * we use a NULL second context) or the first two requests have unique IDs.
122 *
123 * By always executing the first two requests in the queue the driver ensures
124 * that the GPU is kept as busy as possible. In the case where a single context
125 * completes but a second context is still executing, the request for this second
126 * context will be at the head of the queue when we remove the first one. This
127 * request will then be resubmitted along with a new request for a different context,
128 * which will cause the hardware to continue executing the second request and queue
129 * the new request (the GPU detects the condition of a context getting preempted
130 * with the same context and optimizes the context switch flow by not doing
131 * preemption, but just sampling the new tail pointer).
132 *
b20385f1 133 */
27af5eea 134#include <linux/interrupt.h>
b20385f1
OM
135
136#include <drm/drmP.h>
137#include <drm/i915_drm.h>
138#include "i915_drv.h"
7c2fa7fa 139#include "i915_gem_render_state.h"
578f1ac6 140#include "intel_lrc_reg.h"
3bbaba0c 141#include "intel_mocs.h"
7d3c425f 142#include "intel_workarounds.h"
127f1003 143
e981e7b1
TD
144#define RING_EXECLIST_QFULL (1 << 0x2)
145#define RING_EXECLIST1_VALID (1 << 0x3)
146#define RING_EXECLIST0_VALID (1 << 0x4)
147#define RING_EXECLIST_ACTIVE_STATUS (3 << 0xE)
148#define RING_EXECLIST1_ACTIVE (1 << 0x11)
149#define RING_EXECLIST0_ACTIVE (1 << 0x12)
150
151#define GEN8_CTX_STATUS_IDLE_ACTIVE (1 << 0)
152#define GEN8_CTX_STATUS_PREEMPTED (1 << 1)
153#define GEN8_CTX_STATUS_ELEMENT_SWITCH (1 << 2)
154#define GEN8_CTX_STATUS_ACTIVE_IDLE (1 << 3)
155#define GEN8_CTX_STATUS_COMPLETE (1 << 4)
156#define GEN8_CTX_STATUS_LITE_RESTORE (1 << 15)
8670d6f9 157
70c2a24d 158#define GEN8_CTX_STATUS_COMPLETED_MASK \
d8747afb 159 (GEN8_CTX_STATUS_COMPLETE | GEN8_CTX_STATUS_PREEMPTED)
70c2a24d 160
0e93cdd4
CW
161/* Typical size of the average request (2 pipecontrols and a MI_BB) */
162#define EXECLISTS_REQUEST_SIZE 64 /* bytes */
a3aabe86 163#define WA_TAIL_DWORDS 2
7e4992ac 164#define WA_TAIL_BYTES (sizeof(u32) * WA_TAIL_DWORDS)
a3aabe86 165
e2efd130 166static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
1fc44d9b
CW
167 struct intel_engine_cs *engine,
168 struct intel_context *ce);
a3aabe86
CW
169static void execlists_init_reg_state(u32 *reg_state,
170 struct i915_gem_context *ctx,
171 struct intel_engine_cs *engine,
172 struct intel_ring *ring);
7ba717cf 173
f6322edd
CW
174static inline struct i915_priolist *to_priolist(struct rb_node *rb)
175{
176 return rb_entry(rb, struct i915_priolist, node);
177}
178
179static inline int rq_prio(const struct i915_request *rq)
180{
b7268c5e 181 return rq->sched.attr.priority;
f6322edd
CW
182}
183
184static inline bool need_preempt(const struct intel_engine_cs *engine,
185 const struct i915_request *last,
186 int prio)
187{
2a694feb 188 return (intel_engine_has_preemption(engine) &&
c5ce3b8d
CW
189 __execlists_need_preempt(prio, rq_prio(last)) &&
190 !i915_request_completed(last));
f6322edd
CW
191}
192
1fc44d9b 193/*
ca82580c
TU
194 * The context descriptor encodes various attributes of a context,
195 * including its GTT address and some flags. Because it's fairly
196 * expensive to calculate, we'll just do it once and cache the result,
197 * which remains valid until the context is unpinned.
198 *
6e5248b5
DV
199 * This is what a descriptor looks like, from LSB to MSB::
200 *
2355cf08 201 * bits 0-11: flags, GEN8_CTX_* (cached in ctx->desc_template)
6e5248b5
DV
202 * bits 12-31: LRCA, GTT address of (the HWSP of) this context
203 * bits 32-52: ctx ID, a globally unique tag
204 * bits 53-54: mbz, reserved for use by hardware
205 * bits 55-63: group ID, currently unused and set to 0
ac52da6a
DCS
206 *
207 * Starting from Gen11, the upper dword of the descriptor has a new format:
208 *
209 * bits 32-36: reserved
210 * bits 37-47: SW context ID
211 * bits 48:53: engine instance
212 * bit 54: mbz, reserved for use by hardware
213 * bits 55-60: SW counter
214 * bits 61-63: engine class
215 *
216 * engine info, SW context ID and SW counter need to form a unique number
217 * (Context ID) per lrc.
73e4d07f 218 */
ca82580c 219static void
e2efd130 220intel_lr_context_descriptor_update(struct i915_gem_context *ctx,
1fc44d9b
CW
221 struct intel_engine_cs *engine,
222 struct intel_context *ce)
84b790f8 223{
7069b144 224 u64 desc;
84b790f8 225
ac52da6a
DCS
226 BUILD_BUG_ON(MAX_CONTEXT_HW_ID > (BIT(GEN8_CTX_ID_WIDTH)));
227 BUILD_BUG_ON(GEN11_MAX_CONTEXT_HW_ID > (BIT(GEN11_SW_CTX_ID_WIDTH)));
84b790f8 228
2355cf08 229 desc = ctx->desc_template; /* bits 0-11 */
ac52da6a
DCS
230 GEM_BUG_ON(desc & GENMASK_ULL(63, 12));
231
0b29c75a 232 desc |= i915_ggtt_offset(ce->state) + LRC_HEADER_PAGES * PAGE_SIZE;
9021ad03 233 /* bits 12-31 */
ac52da6a
DCS
234 GEM_BUG_ON(desc & GENMASK_ULL(63, 32));
235
236 if (INTEL_GEN(ctx->i915) >= 11) {
237 GEM_BUG_ON(ctx->hw_id >= BIT(GEN11_SW_CTX_ID_WIDTH));
238 desc |= (u64)ctx->hw_id << GEN11_SW_CTX_ID_SHIFT;
239 /* bits 37-47 */
240
241 desc |= (u64)engine->instance << GEN11_ENGINE_INSTANCE_SHIFT;
242 /* bits 48-53 */
243
244 /* TODO: decide what to do with SW counter (bits 55-60) */
245
246 desc |= (u64)engine->class << GEN11_ENGINE_CLASS_SHIFT;
247 /* bits 61-63 */
248 } else {
249 GEM_BUG_ON(ctx->hw_id >= BIT(GEN8_CTX_ID_WIDTH));
250 desc |= (u64)ctx->hw_id << GEN8_CTX_ID_SHIFT; /* bits 32-52 */
251 }
5af05fef 252
9021ad03 253 ce->lrc_desc = desc;
5af05fef
MT
254}
255
27606fd8 256static struct i915_priolist *
87c7acf8 257lookup_priolist(struct intel_engine_cs *engine, int prio)
08dd3e1a 258{
b620e870 259 struct intel_engine_execlists * const execlists = &engine->execlists;
08dd3e1a
CW
260 struct i915_priolist *p;
261 struct rb_node **parent, *rb;
262 bool first = true;
263
b620e870 264 if (unlikely(execlists->no_priolist))
08dd3e1a
CW
265 prio = I915_PRIORITY_NORMAL;
266
267find_priolist:
268 /* most positive priority is scheduled first, equal priorities fifo */
269 rb = NULL;
b620e870 270 parent = &execlists->queue.rb_node;
08dd3e1a
CW
271 while (*parent) {
272 rb = *parent;
f6322edd 273 p = to_priolist(rb);
08dd3e1a
CW
274 if (prio > p->priority) {
275 parent = &rb->rb_left;
276 } else if (prio < p->priority) {
277 parent = &rb->rb_right;
278 first = false;
279 } else {
27606fd8 280 return p;
08dd3e1a
CW
281 }
282 }
283
284 if (prio == I915_PRIORITY_NORMAL) {
b620e870 285 p = &execlists->default_priolist;
08dd3e1a
CW
286 } else {
287 p = kmem_cache_alloc(engine->i915->priorities, GFP_ATOMIC);
288 /* Convert an allocation failure to a priority bump */
289 if (unlikely(!p)) {
290 prio = I915_PRIORITY_NORMAL; /* recurses just once */
291
292 /* To maintain ordering with all rendering, after an
293 * allocation failure we have to disable all scheduling.
294 * Requests will then be executed in fifo, and schedule
295 * will ensure that dependencies are emitted in fifo.
296 * There will be still some reordering with existing
297 * requests, so if userspace lied about their
298 * dependencies that reordering may be visible.
299 */
b620e870 300 execlists->no_priolist = true;
08dd3e1a
CW
301 goto find_priolist;
302 }
303 }
304
305 p->priority = prio;
27606fd8 306 INIT_LIST_HEAD(&p->requests);
08dd3e1a 307 rb_link_node(&p->node, rb, parent);
b620e870 308 rb_insert_color(&p->node, &execlists->queue);
08dd3e1a 309
08dd3e1a 310 if (first)
b620e870 311 execlists->first = &p->node;
08dd3e1a 312
f6322edd 313 return p;
08dd3e1a
CW
314}
315
e61e0f51 316static void unwind_wa_tail(struct i915_request *rq)
7e4992ac
CW
317{
318 rq->tail = intel_ring_wrap(rq->ring, rq->wa_tail - WA_TAIL_BYTES);
319 assert_ring_tail_valid(rq->ring, rq->tail);
320}
321
a4598d17 322static void __unwind_incomplete_requests(struct intel_engine_cs *engine)
7e4992ac 323{
e61e0f51 324 struct i915_request *rq, *rn;
097a9481
MW
325 struct i915_priolist *uninitialized_var(p);
326 int last_prio = I915_PRIORITY_INVALID;
7e4992ac 327
a89d1f92 328 lockdep_assert_held(&engine->timeline.lock);
7e4992ac
CW
329
330 list_for_each_entry_safe_reverse(rq, rn,
a89d1f92 331 &engine->timeline.requests,
7e4992ac 332 link) {
e61e0f51 333 if (i915_request_completed(rq))
7e4992ac
CW
334 return;
335
e61e0f51 336 __i915_request_unsubmit(rq);
7e4992ac
CW
337 unwind_wa_tail(rq);
338
f6322edd
CW
339 GEM_BUG_ON(rq_prio(rq) == I915_PRIORITY_INVALID);
340 if (rq_prio(rq) != last_prio) {
341 last_prio = rq_prio(rq);
87c7acf8 342 p = lookup_priolist(engine, last_prio);
097a9481
MW
343 }
344
a02eb975 345 GEM_BUG_ON(p->priority != rq_prio(rq));
0c7112a0 346 list_add(&rq->sched.link, &p->requests);
7e4992ac
CW
347 }
348}
349
c41937fd 350void
a4598d17
MW
351execlists_unwind_incomplete_requests(struct intel_engine_execlists *execlists)
352{
353 struct intel_engine_cs *engine =
354 container_of(execlists, typeof(*engine), execlists);
4413c474
CW
355 unsigned long flags;
356
357 spin_lock_irqsave(&engine->timeline.lock, flags);
a4598d17 358
a4598d17 359 __unwind_incomplete_requests(engine);
4413c474
CW
360
361 spin_unlock_irqrestore(&engine->timeline.lock, flags);
a4598d17
MW
362}
363
bbd6c47e 364static inline void
e61e0f51 365execlists_context_status_change(struct i915_request *rq, unsigned long status)
84b790f8 366{
bbd6c47e
CW
367 /*
368 * Only used when GVT-g is enabled now. When GVT-g is disabled,
369 * The compiler should eliminate this function as dead-code.
370 */
371 if (!IS_ENABLED(CONFIG_DRM_I915_GVT))
372 return;
6daccb0b 373
3fc03069
CD
374 atomic_notifier_call_chain(&rq->engine->context_status_notifier,
375 status, rq);
84b790f8
BW
376}
377
f2605207
CW
378inline void
379execlists_user_begin(struct intel_engine_execlists *execlists,
380 const struct execlist_port *port)
381{
382 execlists_set_active_once(execlists, EXECLISTS_ACTIVE_USER);
383}
384
385inline void
386execlists_user_end(struct intel_engine_execlists *execlists)
387{
388 execlists_clear_active(execlists, EXECLISTS_ACTIVE_USER);
389}
390
73fd9d38 391static inline void
e61e0f51 392execlists_context_schedule_in(struct i915_request *rq)
73fd9d38
TU
393{
394 execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_IN);
30e17b78 395 intel_engine_context_in(rq->engine);
73fd9d38
TU
396}
397
398static inline void
b9b77426 399execlists_context_schedule_out(struct i915_request *rq, unsigned long status)
73fd9d38 400{
30e17b78 401 intel_engine_context_out(rq->engine);
b9b77426
CW
402 execlists_context_status_change(rq, status);
403 trace_i915_request_out(rq);
73fd9d38
TU
404}
405
c6a2ac71
TU
406static void
407execlists_update_context_pdps(struct i915_hw_ppgtt *ppgtt, u32 *reg_state)
408{
409 ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
410 ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
411 ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
412 ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
413}
414
e61e0f51 415static u64 execlists_update_context(struct i915_request *rq)
ae1250b9 416{
1fc44d9b 417 struct intel_context *ce = rq->hw_context;
04da811b 418 struct i915_hw_ppgtt *ppgtt =
4e0d64db 419 rq->gem_context->ppgtt ?: rq->i915->mm.aliasing_ppgtt;
70c2a24d 420 u32 *reg_state = ce->lrc_reg_state;
ae1250b9 421
e6ba9992 422 reg_state[CTX_RING_TAIL+1] = intel_ring_set_tail(rq->ring, rq->tail);
ae1250b9 423
c6a2ac71
TU
424 /* True 32b PPGTT with dynamic page allocation: update PDP
425 * registers and point the unallocated PDPs to scratch page.
426 * PML4 is allocated during ppgtt init, so this is not needed
427 * in 48-bit mode.
428 */
949e8ab3 429 if (ppgtt && !i915_vm_is_48bit(&ppgtt->base))
c6a2ac71 430 execlists_update_context_pdps(ppgtt, reg_state);
70c2a24d
CW
431
432 return ce->lrc_desc;
ae1250b9
OM
433}
434
05f0addd 435static inline void write_desc(struct intel_engine_execlists *execlists, u64 desc, u32 port)
beecec90 436{
05f0addd
TD
437 if (execlists->ctrl_reg) {
438 writel(lower_32_bits(desc), execlists->submit_reg + port * 2);
439 writel(upper_32_bits(desc), execlists->submit_reg + port * 2 + 1);
440 } else {
441 writel(upper_32_bits(desc), execlists->submit_reg);
442 writel(lower_32_bits(desc), execlists->submit_reg);
443 }
beecec90
CW
444}
445
70c2a24d 446static void execlists_submit_ports(struct intel_engine_cs *engine)
bbd6c47e 447{
05f0addd
TD
448 struct intel_engine_execlists *execlists = &engine->execlists;
449 struct execlist_port *port = execlists->port;
77f0d0e9 450 unsigned int n;
bbd6c47e 451
05f0addd
TD
452 /*
453 * ELSQ note: the submit queue is not cleared after being submitted
454 * to the HW so we need to make sure we always clean it up. This is
455 * currently ensured by the fact that we always write the same number
456 * of elsq entries, keep this in mind before changing the loop below.
457 */
458 for (n = execlists_num_ports(execlists); n--; ) {
e61e0f51 459 struct i915_request *rq;
77f0d0e9
CW
460 unsigned int count;
461 u64 desc;
462
463 rq = port_unpack(&port[n], &count);
464 if (rq) {
465 GEM_BUG_ON(count > !n);
466 if (!count++)
73fd9d38 467 execlists_context_schedule_in(rq);
77f0d0e9
CW
468 port_set(&port[n], port_pack(rq, count));
469 desc = execlists_update_context(rq);
470 GEM_DEBUG_EXEC(port[n].context_id = upper_32_bits(desc));
bccd3b83 471
0c5c7df3 472 GEM_TRACE("%s in[%d]: ctx=%d.%d, global=%d (fence %llx:%d) (current %d), prio=%d\n",
bccd3b83 473 engine->name, n,
16c8619a 474 port[n].context_id, count,
f6322edd 475 rq->global_seqno,
0c5c7df3 476 rq->fence.context, rq->fence.seqno,
e7702760 477 intel_engine_get_seqno(engine),
f6322edd 478 rq_prio(rq));
77f0d0e9
CW
479 } else {
480 GEM_BUG_ON(!n);
481 desc = 0;
482 }
bbd6c47e 483
05f0addd 484 write_desc(execlists, desc, n);
77f0d0e9 485 }
05f0addd
TD
486
487 /* we need to manually load the submit queue */
488 if (execlists->ctrl_reg)
489 writel(EL_CTRL_LOAD, execlists->ctrl_reg);
490
491 execlists_clear_active(execlists, EXECLISTS_ACTIVE_HWACK);
bbd6c47e
CW
492}
493
1fc44d9b 494static bool ctx_single_port_submission(const struct intel_context *ce)
84b790f8 495{
70c2a24d 496 return (IS_ENABLED(CONFIG_DRM_I915_GVT) &&
1fc44d9b 497 i915_gem_context_force_single_submission(ce->gem_context));
70c2a24d 498}
84b790f8 499
1fc44d9b
CW
500static bool can_merge_ctx(const struct intel_context *prev,
501 const struct intel_context *next)
70c2a24d
CW
502{
503 if (prev != next)
504 return false;
26720ab9 505
70c2a24d
CW
506 if (ctx_single_port_submission(prev))
507 return false;
26720ab9 508
70c2a24d 509 return true;
84b790f8
BW
510}
511
e61e0f51 512static void port_assign(struct execlist_port *port, struct i915_request *rq)
77f0d0e9
CW
513{
514 GEM_BUG_ON(rq == port_request(port));
515
516 if (port_isset(port))
e61e0f51 517 i915_request_put(port_request(port));
77f0d0e9 518
e61e0f51 519 port_set(port, port_pack(i915_request_get(rq), port_count(port)));
77f0d0e9
CW
520}
521
beecec90
CW
522static void inject_preempt_context(struct intel_engine_cs *engine)
523{
05f0addd 524 struct intel_engine_execlists *execlists = &engine->execlists;
beecec90 525 struct intel_context *ce =
ab82a063 526 to_intel_context(engine->i915->preempt_context, engine);
beecec90
CW
527 unsigned int n;
528
05f0addd 529 GEM_BUG_ON(execlists->preempt_complete_status !=
d6376374 530 upper_32_bits(ce->lrc_desc));
09b1a4e4
CW
531 GEM_BUG_ON((ce->lrc_reg_state[CTX_CONTEXT_CONTROL + 1] &
532 _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
533 CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT)) !=
534 _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
535 CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT));
536
f6322edd
CW
537 /*
538 * Switch to our empty preempt context so
539 * the state of the GPU is known (idle).
540 */
16a87394 541 GEM_TRACE("%s\n", engine->name);
05f0addd
TD
542 for (n = execlists_num_ports(execlists); --n; )
543 write_desc(execlists, 0, n);
544
545 write_desc(execlists, ce->lrc_desc, n);
546
547 /* we need to manually load the submit queue */
548 if (execlists->ctrl_reg)
549 writel(EL_CTRL_LOAD, execlists->ctrl_reg);
beecec90 550
ef2fb720
CW
551 execlists_clear_active(execlists, EXECLISTS_ACTIVE_HWACK);
552 execlists_set_active(execlists, EXECLISTS_ACTIVE_PREEMPT);
553}
554
555static void complete_preempt_context(struct intel_engine_execlists *execlists)
556{
557 GEM_BUG_ON(!execlists_is_active(execlists, EXECLISTS_ACTIVE_PREEMPT));
558
559 execlists_cancel_port_requests(execlists);
560 execlists_unwind_incomplete_requests(execlists);
561
562 execlists_clear_active(execlists, EXECLISTS_ACTIVE_PREEMPT);
beecec90
CW
563}
564
4413c474 565static bool __execlists_dequeue(struct intel_engine_cs *engine)
acdd884a 566{
7a62cc61
MK
567 struct intel_engine_execlists * const execlists = &engine->execlists;
568 struct execlist_port *port = execlists->port;
76e70087
MK
569 const struct execlist_port * const last_port =
570 &execlists->port[execlists->port_mask];
e61e0f51 571 struct i915_request *last = port_request(port);
20311bd3 572 struct rb_node *rb;
70c2a24d
CW
573 bool submit = false;
574
4413c474
CW
575 lockdep_assert_held(&engine->timeline.lock);
576
70c2a24d
CW
577 /* Hardware submission is through 2 ports. Conceptually each port
578 * has a (RING_START, RING_HEAD, RING_TAIL) tuple. RING_START is
579 * static for a context, and unique to each, so we only execute
580 * requests belonging to a single context from each ring. RING_HEAD
581 * is maintained by the CS in the context image, it marks the place
582 * where it got up to last time, and through RING_TAIL we tell the CS
583 * where we want to execute up to this time.
584 *
585 * In this list the requests are in order of execution. Consecutive
586 * requests from the same context are adjacent in the ringbuffer. We
587 * can combine these requests into a single RING_TAIL update:
588 *
589 * RING_HEAD...req1...req2
590 * ^- RING_TAIL
591 * since to execute req2 the CS must first execute req1.
592 *
593 * Our goal then is to point each port to the end of a consecutive
594 * sequence of requests as being the most optimal (fewest wake ups
595 * and context switches) submission.
779949f4 596 */
acdd884a 597
7a62cc61
MK
598 rb = execlists->first;
599 GEM_BUG_ON(rb_first(&execlists->queue) != rb);
beecec90
CW
600
601 if (last) {
602 /*
603 * Don't resubmit or switch until all outstanding
604 * preemptions (lite-restore) are seen. Then we
605 * know the next preemption status we see corresponds
606 * to this ELSP update.
607 */
eed7ec52
CW
608 GEM_BUG_ON(!execlists_is_active(execlists,
609 EXECLISTS_ACTIVE_USER));
ba74cb10 610 GEM_BUG_ON(!port_count(&port[0]));
beecec90 611
ba74cb10
MT
612 /*
613 * If we write to ELSP a second time before the HW has had
614 * a chance to respond to the previous write, we can confuse
615 * the HW and hit "undefined behaviour". After writing to ELSP,
616 * we must then wait until we see a context-switch event from
617 * the HW to indicate that it has had a chance to respond.
618 */
619 if (!execlists_is_active(execlists, EXECLISTS_ACTIVE_HWACK))
4413c474 620 return false;
ba74cb10 621
f6322edd 622 if (need_preempt(engine, last, execlists->queue_priority)) {
beecec90 623 inject_preempt_context(engine);
4413c474 624 return false;
beecec90 625 }
f6322edd
CW
626
627 /*
628 * In theory, we could coalesce more requests onto
629 * the second port (the first port is active, with
630 * no preemptions pending). However, that means we
631 * then have to deal with the possible lite-restore
632 * of the second port (as we submit the ELSP, there
633 * may be a context-switch) but also we may complete
634 * the resubmission before the context-switch. Ergo,
635 * coalescing onto the second port will cause a
636 * preemption event, but we cannot predict whether
637 * that will affect port[0] or port[1].
638 *
639 * If the second port is already active, we can wait
640 * until the next context-switch before contemplating
641 * new requests. The GPU will be busy and we should be
642 * able to resubmit the new ELSP before it idles,
643 * avoiding pipeline bubbles (momentary pauses where
644 * the driver is unable to keep up the supply of new
645 * work). However, we have to double check that the
646 * priorities of the ports haven't been switch.
647 */
648 if (port_count(&port[1]))
4413c474 649 return false;
f6322edd
CW
650
651 /*
652 * WaIdleLiteRestore:bdw,skl
653 * Apply the wa NOOPs to prevent
654 * ring:HEAD == rq:TAIL as we resubmit the
655 * request. See gen8_emit_breadcrumb() for
656 * where we prepare the padding after the
657 * end of the request.
658 */
659 last->tail = last->wa_tail;
beecec90
CW
660 }
661
f6322edd
CW
662 while (rb) {
663 struct i915_priolist *p = to_priolist(rb);
e61e0f51 664 struct i915_request *rq, *rn;
6c067579 665
0c7112a0 666 list_for_each_entry_safe(rq, rn, &p->requests, sched.link) {
6c067579
CW
667 /*
668 * Can we combine this request with the current port?
669 * It has to be the same context/ringbuffer and not
670 * have any exceptions (e.g. GVT saying never to
671 * combine contexts).
672 *
673 * If we can combine the requests, we can execute both
674 * by updating the RING_TAIL to point to the end of the
675 * second request, and so we never need to tell the
676 * hardware about the first.
70c2a24d 677 */
1fc44d9b
CW
678 if (last &&
679 !can_merge_ctx(rq->hw_context, last->hw_context)) {
6c067579
CW
680 /*
681 * If we are on the second port and cannot
682 * combine this request with the last, then we
683 * are done.
684 */
76e70087 685 if (port == last_port) {
6c067579 686 __list_del_many(&p->requests,
0c7112a0 687 &rq->sched.link);
6c067579
CW
688 goto done;
689 }
690
691 /*
692 * If GVT overrides us we only ever submit
693 * port[0], leaving port[1] empty. Note that we
694 * also have to be careful that we don't queue
695 * the same context (even though a different
696 * request) to the second port.
697 */
1fc44d9b
CW
698 if (ctx_single_port_submission(last->hw_context) ||
699 ctx_single_port_submission(rq->hw_context)) {
6c067579 700 __list_del_many(&p->requests,
0c7112a0 701 &rq->sched.link);
6c067579
CW
702 goto done;
703 }
704
1fc44d9b 705 GEM_BUG_ON(last->hw_context == rq->hw_context);
6c067579
CW
706
707 if (submit)
708 port_assign(port, last);
709 port++;
7a62cc61
MK
710
711 GEM_BUG_ON(port_isset(port));
6c067579 712 }
70c2a24d 713
0c7112a0 714 INIT_LIST_HEAD(&rq->sched.link);
e61e0f51
CW
715 __i915_request_submit(rq);
716 trace_i915_request_in(rq, port_index(port, execlists));
6c067579
CW
717 last = rq;
718 submit = true;
70c2a24d 719 }
d55ac5bf 720
20311bd3 721 rb = rb_next(rb);
7a62cc61 722 rb_erase(&p->node, &execlists->queue);
6c067579
CW
723 INIT_LIST_HEAD(&p->requests);
724 if (p->priority != I915_PRIORITY_NORMAL)
c5cf9a91 725 kmem_cache_free(engine->i915->priorities, p);
f6322edd 726 }
15c83c43 727
6c067579 728done:
15c83c43
CW
729 /*
730 * Here be a bit of magic! Or sleight-of-hand, whichever you prefer.
731 *
732 * We choose queue_priority such that if we add a request of greater
733 * priority than this, we kick the submission tasklet to decide on
734 * the right order of submitting the requests to hardware. We must
735 * also be prepared to reorder requests as they are in-flight on the
736 * HW. We derive the queue_priority then as the first "hole" in
737 * the HW submission ports and if there are no available slots,
738 * the priority of the lowest executing request, i.e. last.
739 *
740 * When we do receive a higher priority request ready to run from the
741 * user, see queue_request(), the queue_priority is bumped to that
742 * request triggering preemption on the next dequeue (or subsequent
743 * interrupt for secondary ports).
744 */
745 execlists->queue_priority =
746 port != execlists->port ? rq_prio(last) : INT_MIN;
747
7a62cc61 748 execlists->first = rb;
6c067579 749 if (submit)
77f0d0e9 750 port_assign(port, last);
339ccd35
CW
751
752 /* We must always keep the beast fed if we have work piled up */
339ccd35
CW
753 GEM_BUG_ON(execlists->first && !port_isset(execlists->port));
754
4413c474
CW
755 /* Re-evaluate the executing context setup after each preemptive kick */
756 if (last)
f2605207 757 execlists_user_begin(execlists, execlists->port);
4413c474
CW
758
759 return submit;
760}
761
762static void execlists_dequeue(struct intel_engine_cs *engine)
763{
764 struct intel_engine_execlists * const execlists = &engine->execlists;
765 unsigned long flags;
766 bool submit;
767
768 spin_lock_irqsave(&engine->timeline.lock, flags);
769 submit = __execlists_dequeue(engine);
770 spin_unlock_irqrestore(&engine->timeline.lock, flags);
771
772 if (submit)
70c2a24d 773 execlists_submit_ports(engine);
d081e021
CW
774
775 GEM_BUG_ON(port_isset(execlists->port) &&
776 !execlists_is_active(execlists, EXECLISTS_ACTIVE_USER));
acdd884a
MT
777}
778
c41937fd 779void
a4598d17 780execlists_cancel_port_requests(struct intel_engine_execlists * const execlists)
cf4591d1 781{
3f9e6cd8 782 struct execlist_port *port = execlists->port;
dc2279e1 783 unsigned int num_ports = execlists_num_ports(execlists);
cf4591d1 784
3f9e6cd8 785 while (num_ports-- && port_isset(port)) {
e61e0f51 786 struct i915_request *rq = port_request(port);
7e44fc28 787
0c5c7df3
TU
788 GEM_TRACE("%s:port%u global=%d (fence %llx:%d), (current %d)\n",
789 rq->engine->name,
790 (unsigned int)(port - execlists->port),
791 rq->global_seqno,
792 rq->fence.context, rq->fence.seqno,
793 intel_engine_get_seqno(rq->engine));
794
4a118ecb 795 GEM_BUG_ON(!execlists->active);
b9b77426
CW
796 execlists_context_schedule_out(rq,
797 i915_request_completed(rq) ?
798 INTEL_CONTEXT_SCHEDULE_OUT :
799 INTEL_CONTEXT_SCHEDULE_PREEMPTED);
702791f7 800
e61e0f51 801 i915_request_put(rq);
7e44fc28 802
3f9e6cd8
CW
803 memset(port, 0, sizeof(*port));
804 port++;
805 }
eed7ec52 806
38057aa1 807 execlists_clear_active(execlists, EXECLISTS_ACTIVE_USER);
f2605207 808 execlists_user_end(execlists);
cf4591d1
MK
809}
810
46b3617d
CW
811static void clear_gtiir(struct intel_engine_cs *engine)
812{
46b3617d
CW
813 struct drm_i915_private *dev_priv = engine->i915;
814 int i;
815
46b3617d
CW
816 /*
817 * Clear any pending interrupt state.
818 *
819 * We do it twice out of paranoia that some of the IIR are
820 * double buffered, and so if we only reset it once there may
821 * still be an interrupt pending.
822 */
ff047a87
OM
823 if (INTEL_GEN(dev_priv) >= 11) {
824 static const struct {
825 u8 bank;
826 u8 bit;
827 } gen11_gtiir[] = {
828 [RCS] = {0, GEN11_RCS0},
829 [BCS] = {0, GEN11_BCS},
830 [_VCS(0)] = {1, GEN11_VCS(0)},
831 [_VCS(1)] = {1, GEN11_VCS(1)},
832 [_VCS(2)] = {1, GEN11_VCS(2)},
833 [_VCS(3)] = {1, GEN11_VCS(3)},
834 [_VECS(0)] = {1, GEN11_VECS(0)},
835 [_VECS(1)] = {1, GEN11_VECS(1)},
836 };
837 unsigned long irqflags;
838
839 GEM_BUG_ON(engine->id >= ARRAY_SIZE(gen11_gtiir));
840
841 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
842 for (i = 0; i < 2; i++) {
843 gen11_reset_one_iir(dev_priv,
844 gen11_gtiir[engine->id].bank,
845 gen11_gtiir[engine->id].bit);
846 }
847 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
848 } else {
849 static const u8 gtiir[] = {
850 [RCS] = 0,
851 [BCS] = 0,
852 [VCS] = 1,
853 [VCS2] = 1,
854 [VECS] = 3,
855 };
856
857 GEM_BUG_ON(engine->id >= ARRAY_SIZE(gtiir));
858
859 for (i = 0; i < 2; i++) {
860 I915_WRITE(GEN8_GT_IIR(gtiir[engine->id]),
861 engine->irq_keep_mask);
862 POSTING_READ(GEN8_GT_IIR(gtiir[engine->id]));
863 }
864 GEM_BUG_ON(I915_READ(GEN8_GT_IIR(gtiir[engine->id])) &
46b3617d 865 engine->irq_keep_mask);
46b3617d 866 }
46b3617d
CW
867}
868
869static void reset_irq(struct intel_engine_cs *engine)
870{
871 /* Mark all CS interrupts as complete */
872 smp_store_mb(engine->execlists.active, 0);
873 synchronize_hardirq(engine->i915->drm.irq);
874
875 clear_gtiir(engine);
876
877 /*
878 * The port is checked prior to scheduling a tasklet, but
879 * just in case we have suspended the tasklet to do the
880 * wedging make sure that when it wakes, it decides there
881 * is no work to do by clearing the irq_posted bit.
882 */
883 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
884}
885
27a5f61b
CW
886static void execlists_cancel_requests(struct intel_engine_cs *engine)
887{
b620e870 888 struct intel_engine_execlists * const execlists = &engine->execlists;
e61e0f51 889 struct i915_request *rq, *rn;
27a5f61b
CW
890 struct rb_node *rb;
891 unsigned long flags;
27a5f61b 892
0c5c7df3
TU
893 GEM_TRACE("%s current %d\n",
894 engine->name, intel_engine_get_seqno(engine));
963ddd63 895
a3e38836
CW
896 /*
897 * Before we call engine->cancel_requests(), we should have exclusive
898 * access to the submission state. This is arranged for us by the
899 * caller disabling the interrupt generation, the tasklet and other
900 * threads that may then access the same state, giving us a free hand
901 * to reset state. However, we still need to let lockdep be aware that
902 * we know this state may be accessed in hardirq context, so we
903 * disable the irq around this manipulation and we want to keep
904 * the spinlock focused on its duties and not accidentally conflate
905 * coverage to the submission's irq state. (Similarly, although we
906 * shouldn't need to disable irq around the manipulation of the
907 * submission's irq state, we also wish to remind ourselves that
908 * it is irq state.)
909 */
910 local_irq_save(flags);
27a5f61b
CW
911
912 /* Cancel the requests on the HW and clear the ELSP tracker. */
a4598d17 913 execlists_cancel_port_requests(execlists);
46b3617d 914 reset_irq(engine);
27a5f61b 915
a89d1f92 916 spin_lock(&engine->timeline.lock);
a3e38836 917
27a5f61b 918 /* Mark all executing requests as skipped. */
a89d1f92 919 list_for_each_entry(rq, &engine->timeline.requests, link) {
27a5f61b 920 GEM_BUG_ON(!rq->global_seqno);
e61e0f51 921 if (!i915_request_completed(rq))
27a5f61b
CW
922 dma_fence_set_error(&rq->fence, -EIO);
923 }
924
925 /* Flush the queued requests to the timeline list (for retiring). */
b620e870 926 rb = execlists->first;
27a5f61b 927 while (rb) {
f6322edd 928 struct i915_priolist *p = to_priolist(rb);
27a5f61b 929
0c7112a0
CW
930 list_for_each_entry_safe(rq, rn, &p->requests, sched.link) {
931 INIT_LIST_HEAD(&rq->sched.link);
27a5f61b
CW
932
933 dma_fence_set_error(&rq->fence, -EIO);
e61e0f51 934 __i915_request_submit(rq);
27a5f61b
CW
935 }
936
937 rb = rb_next(rb);
b620e870 938 rb_erase(&p->node, &execlists->queue);
27a5f61b
CW
939 INIT_LIST_HEAD(&p->requests);
940 if (p->priority != I915_PRIORITY_NORMAL)
941 kmem_cache_free(engine->i915->priorities, p);
942 }
943
944 /* Remaining _unready_ requests will be nop'ed when submitted */
945
f6322edd 946 execlists->queue_priority = INT_MIN;
b620e870
MK
947 execlists->queue = RB_ROOT;
948 execlists->first = NULL;
3f9e6cd8 949 GEM_BUG_ON(port_isset(execlists->port));
27a5f61b 950
a89d1f92 951 spin_unlock(&engine->timeline.lock);
a3e38836 952
a3e38836 953 local_irq_restore(flags);
27a5f61b
CW
954}
955
73377dbc 956static void process_csb(struct intel_engine_cs *engine)
e981e7b1 957{
b620e870 958 struct intel_engine_execlists * const execlists = &engine->execlists;
f2605207 959 struct execlist_port *port = execlists->port;
73377dbc 960 struct drm_i915_private *i915 = engine->i915;
bb5db7e1 961 bool fw = false;
c6a2ac71 962
73377dbc 963 do {
6d2cb5aa
CW
964 /* The HWSP contains a (cacheable) mirror of the CSB */
965 const u32 *buf =
966 &engine->status_page.page_addr[I915_HWS_CSB_BUF0_INDEX];
4af0d727 967 unsigned int head, tail;
70c2a24d 968
b620e870 969 if (unlikely(execlists->csb_use_mmio)) {
6d2cb5aa 970 buf = (u32 * __force)
73377dbc
CW
971 (i915->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_BUF_LO(engine, 0)));
972 execlists->csb_head = -1; /* force mmio read of CSB */
6d2cb5aa
CW
973 }
974
9153e6b7
CW
975 /* Clear before reading to catch new interrupts */
976 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
977 smp_mb__after_atomic();
978
73377dbc 979 if (unlikely(execlists->csb_head == -1)) { /* after a reset */
bb5db7e1 980 if (!fw) {
73377dbc 981 intel_uncore_forcewake_get(i915, execlists->fw_domains);
bb5db7e1
CW
982 fw = true;
983 }
984
73377dbc 985 head = readl(i915->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)));
767a983a
CW
986 tail = GEN8_CSB_WRITE_PTR(head);
987 head = GEN8_CSB_READ_PTR(head);
b620e870 988 execlists->csb_head = head;
767a983a
CW
989 } else {
990 const int write_idx =
73377dbc 991 intel_hws_csb_write_index(i915) -
767a983a
CW
992 I915_HWS_CSB_BUF0_INDEX;
993
b620e870 994 head = execlists->csb_head;
767a983a 995 tail = READ_ONCE(buf[write_idx]);
77dfedb5 996 rmb(); /* Hopefully paired with a wmb() in HW */
767a983a 997 }
bb5db7e1 998 GEM_TRACE("%s cs-irq head=%d [%d%s], tail=%d [%d%s]\n",
bccd3b83 999 engine->name,
73377dbc
CW
1000 head, GEN8_CSB_READ_PTR(readl(i915->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)))), fw ? "" : "?",
1001 tail, GEN8_CSB_WRITE_PTR(readl(i915->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)))), fw ? "" : "?");
b620e870 1002
4af0d727 1003 while (head != tail) {
e61e0f51 1004 struct i915_request *rq;
4af0d727 1005 unsigned int status;
77f0d0e9 1006 unsigned int count;
4af0d727
CW
1007
1008 if (++head == GEN8_CSB_ENTRIES)
1009 head = 0;
70c2a24d 1010
73377dbc
CW
1011 /*
1012 * We are flying near dragons again.
2ffe80aa
CW
1013 *
1014 * We hold a reference to the request in execlist_port[]
1015 * but no more than that. We are operating in softirq
1016 * context and so cannot hold any mutex or sleep. That
1017 * prevents us stopping the requests we are processing
1018 * in port[] from being retired simultaneously (the
1019 * breadcrumb will be complete before we see the
1020 * context-switch). As we only hold the reference to the
1021 * request, any pointer chasing underneath the request
1022 * is subject to a potential use-after-free. Thus we
1023 * store all of the bookkeeping within port[] as
1024 * required, and avoid using unguarded pointers beneath
1025 * request itself. The same applies to the atomic
1026 * status notifier.
1027 */
1028
6d2cb5aa 1029 status = READ_ONCE(buf[2 * head]); /* maybe mmio! */
193a98dc 1030 GEM_TRACE("%s csb[%d]: status=0x%08x:0x%08x, active=0x%x\n",
bccd3b83 1031 engine->name, head,
193a98dc
CW
1032 status, buf[2*head + 1],
1033 execlists->active);
ba74cb10
MT
1034
1035 if (status & (GEN8_CTX_STATUS_IDLE_ACTIVE |
1036 GEN8_CTX_STATUS_PREEMPTED))
1037 execlists_set_active(execlists,
1038 EXECLISTS_ACTIVE_HWACK);
1039 if (status & GEN8_CTX_STATUS_ACTIVE_IDLE)
1040 execlists_clear_active(execlists,
1041 EXECLISTS_ACTIVE_HWACK);
1042
70c2a24d
CW
1043 if (!(status & GEN8_CTX_STATUS_COMPLETED_MASK))
1044 continue;
1045
1f5f9edb
CW
1046 /* We should never get a COMPLETED | IDLE_ACTIVE! */
1047 GEM_BUG_ON(status & GEN8_CTX_STATUS_IDLE_ACTIVE);
1048
e40dd226 1049 if (status & GEN8_CTX_STATUS_COMPLETE &&
d6376374 1050 buf[2*head + 1] == execlists->preempt_complete_status) {
193a98dc 1051 GEM_TRACE("%s preempt-idle\n", engine->name);
ef2fb720 1052 complete_preempt_context(execlists);
beecec90
CW
1053 continue;
1054 }
1055
1056 if (status & GEN8_CTX_STATUS_PREEMPTED &&
4a118ecb
CW
1057 execlists_is_active(execlists,
1058 EXECLISTS_ACTIVE_PREEMPT))
beecec90
CW
1059 continue;
1060
4a118ecb
CW
1061 GEM_BUG_ON(!execlists_is_active(execlists,
1062 EXECLISTS_ACTIVE_USER));
1063
77f0d0e9 1064 rq = port_unpack(port, &count);
0c5c7df3 1065 GEM_TRACE("%s out[0]: ctx=%d.%d, global=%d (fence %llx:%d) (current %d), prio=%d\n",
bccd3b83 1066 engine->name,
16c8619a 1067 port->context_id, count,
f6322edd 1068 rq ? rq->global_seqno : 0,
0c5c7df3
TU
1069 rq ? rq->fence.context : 0,
1070 rq ? rq->fence.seqno : 0,
e7702760 1071 intel_engine_get_seqno(engine),
f6322edd 1072 rq ? rq_prio(rq) : 0);
e084039b
CW
1073
1074 /* Check the context/desc id for this event matches */
1075 GEM_DEBUG_BUG_ON(buf[2 * head + 1] != port->context_id);
1076
77f0d0e9
CW
1077 GEM_BUG_ON(count == 0);
1078 if (--count == 0) {
f2605207
CW
1079 /*
1080 * On the final event corresponding to the
1081 * submission of this context, we expect either
1082 * an element-switch event or a completion
1083 * event (and on completion, the active-idle
1084 * marker). No more preemptions, lite-restore
1085 * or otherwise.
1086 */
70c2a24d 1087 GEM_BUG_ON(status & GEN8_CTX_STATUS_PREEMPTED);
d8747afb
CW
1088 GEM_BUG_ON(port_isset(&port[1]) &&
1089 !(status & GEN8_CTX_STATUS_ELEMENT_SWITCH));
f2605207
CW
1090 GEM_BUG_ON(!port_isset(&port[1]) &&
1091 !(status & GEN8_CTX_STATUS_ACTIVE_IDLE));
1092
1093 /*
1094 * We rely on the hardware being strongly
1095 * ordered, that the breadcrumb write is
1096 * coherent (visible from the CPU) before the
1097 * user interrupt and CSB is processed.
1098 */
e61e0f51 1099 GEM_BUG_ON(!i915_request_completed(rq));
f2605207 1100
b9b77426
CW
1101 execlists_context_schedule_out(rq,
1102 INTEL_CONTEXT_SCHEDULE_OUT);
e61e0f51 1103 i915_request_put(rq);
70c2a24d 1104
65cb8c0f
CW
1105 GEM_TRACE("%s completed ctx=%d\n",
1106 engine->name, port->context_id);
1107
f2605207
CW
1108 port = execlists_port_complete(execlists, port);
1109 if (port_isset(port))
1110 execlists_user_begin(execlists, port);
1111 else
1112 execlists_user_end(execlists);
77f0d0e9
CW
1113 } else {
1114 port_set(port, port_pack(rq, count));
70c2a24d 1115 }
4af0d727 1116 }
e1fee72c 1117
b620e870
MK
1118 if (head != execlists->csb_head) {
1119 execlists->csb_head = head;
767a983a 1120 writel(_MASKED_FIELD(GEN8_CSB_READ_PTR_MASK, head << 8),
73377dbc 1121 i915->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)));
767a983a 1122 }
73377dbc 1123 } while (test_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted));
e981e7b1 1124
73377dbc
CW
1125 if (unlikely(fw))
1126 intel_uncore_forcewake_put(i915, execlists->fw_domains);
1127}
c6a2ac71 1128
73377dbc
CW
1129/*
1130 * Check the unread Context Status Buffers and manage the submission of new
1131 * contexts to the ELSP accordingly.
1132 */
1133static void execlists_submission_tasklet(unsigned long data)
1134{
1135 struct intel_engine_cs * const engine = (struct intel_engine_cs *)data;
1136
1137 GEM_TRACE("%s awake?=%d, active=%x, irq-posted?=%d\n",
1138 engine->name,
1139 engine->i915->gt.awake,
1140 engine->execlists.active,
1141 test_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted));
1142
1143 /*
1144 * We can skip acquiring intel_runtime_pm_get() here as it was taken
1145 * on our behalf by the request (see i915_gem_mark_busy()) and it will
1146 * not be relinquished until the device is idle (see
1147 * i915_gem_idle_work_handler()). As a precaution, we make sure
1148 * that all ELSP are drained i.e. we have processed the CSB,
1149 * before allowing ourselves to idle and calling intel_runtime_pm_put().
1150 */
1151 GEM_BUG_ON(!engine->i915->gt.awake);
1152
1153 /*
1154 * Prefer doing test_and_clear_bit() as a two stage operation to avoid
1155 * imposing the cost of a locked atomic transaction when submitting a
1156 * new request (outside of the context-switch interrupt).
1157 */
1158 if (test_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted))
1159 process_csb(engine);
1160
1161 if (!execlists_is_active(&engine->execlists, EXECLISTS_ACTIVE_PREEMPT))
1162 execlists_dequeue(engine);
eed7ec52
CW
1163
1164 /* If the engine is now idle, so should be the flag; and vice versa. */
1165 GEM_BUG_ON(execlists_is_active(&engine->execlists,
1166 EXECLISTS_ACTIVE_USER) ==
1167 !port_isset(engine->execlists.port));
e981e7b1
TD
1168}
1169
f6322edd 1170static void queue_request(struct intel_engine_cs *engine,
0c7112a0 1171 struct i915_sched_node *node,
f6322edd 1172 int prio)
27606fd8 1173{
0c7112a0 1174 list_add_tail(&node->link,
87c7acf8 1175 &lookup_priolist(engine, prio)->requests);
f6322edd 1176}
27606fd8 1177
ae2f5c00
CW
1178static void __submit_queue(struct intel_engine_cs *engine, int prio)
1179{
1180 engine->execlists.queue_priority = prio;
1181 tasklet_hi_schedule(&engine->execlists.tasklet);
1182}
1183
f6322edd
CW
1184static void submit_queue(struct intel_engine_cs *engine, int prio)
1185{
ae2f5c00
CW
1186 if (prio > engine->execlists.queue_priority)
1187 __submit_queue(engine, prio);
27606fd8
CW
1188}
1189
e61e0f51 1190static void execlists_submit_request(struct i915_request *request)
acdd884a 1191{
4a570db5 1192 struct intel_engine_cs *engine = request->engine;
5590af3e 1193 unsigned long flags;
acdd884a 1194
663f71e7 1195 /* Will be called from irq-context when using foreign fences. */
a89d1f92 1196 spin_lock_irqsave(&engine->timeline.lock, flags);
acdd884a 1197
0c7112a0 1198 queue_request(engine, &request->sched, rq_prio(request));
f6322edd 1199 submit_queue(engine, rq_prio(request));
acdd884a 1200
b620e870 1201 GEM_BUG_ON(!engine->execlists.first);
0c7112a0 1202 GEM_BUG_ON(list_empty(&request->sched.link));
6c067579 1203
a89d1f92 1204 spin_unlock_irqrestore(&engine->timeline.lock, flags);
acdd884a
MT
1205}
1206
0c7112a0 1207static struct i915_request *sched_to_request(struct i915_sched_node *node)
1f181225 1208{
0c7112a0 1209 return container_of(node, struct i915_request, sched);
1f181225
CW
1210}
1211
20311bd3 1212static struct intel_engine_cs *
0c7112a0 1213sched_lock_engine(struct i915_sched_node *node, struct intel_engine_cs *locked)
20311bd3 1214{
0c7112a0 1215 struct intel_engine_cs *engine = sched_to_request(node)->engine;
a79a524e
CW
1216
1217 GEM_BUG_ON(!locked);
20311bd3 1218
20311bd3 1219 if (engine != locked) {
a89d1f92
CW
1220 spin_unlock(&locked->timeline.lock);
1221 spin_lock(&engine->timeline.lock);
20311bd3
CW
1222 }
1223
1224 return engine;
1225}
1226
b7268c5e
CW
1227static void execlists_schedule(struct i915_request *request,
1228 const struct i915_sched_attr *attr)
20311bd3 1229{
a02eb975
CW
1230 struct i915_priolist *uninitialized_var(pl);
1231 struct intel_engine_cs *engine, *last;
20311bd3
CW
1232 struct i915_dependency *dep, *p;
1233 struct i915_dependency stack;
b7268c5e 1234 const int prio = attr->priority;
20311bd3
CW
1235 LIST_HEAD(dfs);
1236
7d1ea609
CW
1237 GEM_BUG_ON(prio == I915_PRIORITY_INVALID);
1238
e61e0f51 1239 if (i915_request_completed(request))
c218ee03
CW
1240 return;
1241
b7268c5e 1242 if (prio <= READ_ONCE(request->sched.attr.priority))
20311bd3
CW
1243 return;
1244
70cd1476
CW
1245 /* Need BKL in order to use the temporary link inside i915_dependency */
1246 lockdep_assert_held(&request->i915->drm.struct_mutex);
20311bd3 1247
0c7112a0 1248 stack.signaler = &request->sched;
20311bd3
CW
1249 list_add(&stack.dfs_link, &dfs);
1250
ce01b173
CW
1251 /*
1252 * Recursively bump all dependent priorities to match the new request.
20311bd3
CW
1253 *
1254 * A naive approach would be to use recursion:
0c7112a0
CW
1255 * static void update_priorities(struct i915_sched_node *node, prio) {
1256 * list_for_each_entry(dep, &node->signalers_list, signal_link)
20311bd3 1257 * update_priorities(dep->signal, prio)
0c7112a0 1258 * queue_request(node);
20311bd3
CW
1259 * }
1260 * but that may have unlimited recursion depth and so runs a very
1261 * real risk of overunning the kernel stack. Instead, we build
1262 * a flat list of all dependencies starting with the current request.
1263 * As we walk the list of dependencies, we add all of its dependencies
1264 * to the end of the list (this may include an already visited
1265 * request) and continue to walk onwards onto the new dependencies. The
1266 * end result is a topological list of requests in reverse order, the
1267 * last element in the list is the request we must execute first.
1268 */
2221c5b7 1269 list_for_each_entry(dep, &dfs, dfs_link) {
0c7112a0 1270 struct i915_sched_node *node = dep->signaler;
20311bd3 1271
ce01b173
CW
1272 /*
1273 * Within an engine, there can be no cycle, but we may
a79a524e
CW
1274 * refer to the same dependency chain multiple times
1275 * (redundant dependencies are not eliminated) and across
1276 * engines.
1277 */
0c7112a0 1278 list_for_each_entry(p, &node->signalers_list, signal_link) {
ce01b173
CW
1279 GEM_BUG_ON(p == dep); /* no cycles! */
1280
0c7112a0 1281 if (i915_sched_node_signaled(p->signaler))
1f181225
CW
1282 continue;
1283
b7268c5e
CW
1284 GEM_BUG_ON(p->signaler->attr.priority < node->attr.priority);
1285 if (prio > READ_ONCE(p->signaler->attr.priority))
20311bd3 1286 list_move_tail(&p->dfs_link, &dfs);
a79a524e 1287 }
20311bd3
CW
1288 }
1289
ce01b173
CW
1290 /*
1291 * If we didn't need to bump any existing priorities, and we haven't
349bdb68
CW
1292 * yet submitted this request (i.e. there is no potential race with
1293 * execlists_submit_request()), we can set our own priority and skip
1294 * acquiring the engine locks.
1295 */
b7268c5e 1296 if (request->sched.attr.priority == I915_PRIORITY_INVALID) {
0c7112a0 1297 GEM_BUG_ON(!list_empty(&request->sched.link));
b7268c5e 1298 request->sched.attr = *attr;
349bdb68
CW
1299 if (stack.dfs_link.next == stack.dfs_link.prev)
1300 return;
1301 __list_del_entry(&stack.dfs_link);
1302 }
1303
a02eb975 1304 last = NULL;
a79a524e 1305 engine = request->engine;
a89d1f92 1306 spin_lock_irq(&engine->timeline.lock);
a79a524e 1307
20311bd3
CW
1308 /* Fifo and depth-first replacement ensure our deps execute before us */
1309 list_for_each_entry_safe_reverse(dep, p, &dfs, dfs_link) {
0c7112a0 1310 struct i915_sched_node *node = dep->signaler;
20311bd3
CW
1311
1312 INIT_LIST_HEAD(&dep->dfs_link);
1313
0c7112a0 1314 engine = sched_lock_engine(node, engine);
20311bd3 1315
b7268c5e 1316 if (prio <= node->attr.priority)
20311bd3
CW
1317 continue;
1318
b7268c5e 1319 node->attr.priority = prio;
0c7112a0 1320 if (!list_empty(&node->link)) {
a02eb975
CW
1321 if (last != engine) {
1322 pl = lookup_priolist(engine, prio);
1323 last = engine;
1324 }
1325 GEM_BUG_ON(pl->priority != prio);
1326 list_move_tail(&node->link, &pl->requests);
a79a524e 1327 }
ae2f5c00
CW
1328
1329 if (prio > engine->execlists.queue_priority &&
0c7112a0 1330 i915_sw_fence_done(&sched_to_request(node)->submit))
ae2f5c00 1331 __submit_queue(engine, prio);
20311bd3
CW
1332 }
1333
a89d1f92 1334 spin_unlock_irq(&engine->timeline.lock);
20311bd3
CW
1335}
1336
1fc44d9b
CW
1337static void execlists_context_destroy(struct intel_context *ce)
1338{
1339 GEM_BUG_ON(!ce->state);
1340 GEM_BUG_ON(ce->pin_count);
1341
1342 intel_ring_free(ce->ring);
1343 __i915_gem_object_release_unless_active(ce->state->obj);
1344}
1345
867985d4 1346static void execlists_context_unpin(struct intel_context *ce)
1fc44d9b
CW
1347{
1348 intel_ring_unpin(ce->ring);
1349
1350 ce->state->obj->pin_global--;
1351 i915_gem_object_unpin_map(ce->state->obj);
1352 i915_vma_unpin(ce->state);
1353
1354 i915_gem_context_put(ce->gem_context);
1355}
1356
f4e15af7
CW
1357static int __context_pin(struct i915_gem_context *ctx, struct i915_vma *vma)
1358{
1359 unsigned int flags;
1360 int err;
1361
1362 /*
1363 * Clear this page out of any CPU caches for coherent swap-in/out.
1364 * We only want to do this on the first bind so that we do not stall
1365 * on an active context (which by nature is already on the GPU).
1366 */
1367 if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
1368 err = i915_gem_object_set_to_gtt_domain(vma->obj, true);
1369 if (err)
1370 return err;
1371 }
1372
1373 flags = PIN_GLOBAL | PIN_HIGH;
1374 if (ctx->ggtt_offset_bias)
1375 flags |= PIN_OFFSET_BIAS | ctx->ggtt_offset_bias;
1376
1377 return i915_vma_pin(vma, 0, GEN8_LR_CONTEXT_ALIGN, flags);
1378}
1379
1fc44d9b
CW
1380static struct intel_context *
1381__execlists_context_pin(struct intel_engine_cs *engine,
1382 struct i915_gem_context *ctx,
1383 struct intel_context *ce)
dcb4c12a 1384{
7d774cac 1385 void *vaddr;
ca82580c 1386 int ret;
dcb4c12a 1387
1fc44d9b 1388 ret = execlists_context_deferred_alloc(ctx, engine, ce);
1d2a19c2
CW
1389 if (ret)
1390 goto err;
56f6e0a7 1391 GEM_BUG_ON(!ce->state);
e8a9c58f 1392
f4e15af7 1393 ret = __context_pin(ctx, ce->state);
e84fe803 1394 if (ret)
24f1d3cc 1395 goto err;
7ba717cf 1396
bf3783e5 1397 vaddr = i915_gem_object_pin_map(ce->state->obj, I915_MAP_WB);
7d774cac
TU
1398 if (IS_ERR(vaddr)) {
1399 ret = PTR_ERR(vaddr);
bf3783e5 1400 goto unpin_vma;
82352e90
TU
1401 }
1402
d822bb18 1403 ret = intel_ring_pin(ce->ring, ctx->i915, ctx->ggtt_offset_bias);
e84fe803 1404 if (ret)
7d774cac 1405 goto unpin_map;
d1675198 1406
1fc44d9b 1407 intel_lr_context_descriptor_update(ctx, engine, ce);
9021ad03 1408
a3aabe86
CW
1409 ce->lrc_reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
1410 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
bde13ebd 1411 i915_ggtt_offset(ce->ring->vma);
c216e906 1412 ce->lrc_reg_state[CTX_RING_HEAD+1] = ce->ring->head;
a3aabe86 1413
3d574a6b 1414 ce->state->obj->pin_global++;
9a6feaf0 1415 i915_gem_context_get(ctx);
1fc44d9b 1416 return ce;
7ba717cf 1417
7d774cac 1418unpin_map:
bf3783e5
CW
1419 i915_gem_object_unpin_map(ce->state->obj);
1420unpin_vma:
1421 __i915_vma_unpin(ce->state);
24f1d3cc 1422err:
9021ad03 1423 ce->pin_count = 0;
266a240b 1424 return ERR_PTR(ret);
e84fe803
NH
1425}
1426
1fc44d9b
CW
1427static const struct intel_context_ops execlists_context_ops = {
1428 .unpin = execlists_context_unpin,
1429 .destroy = execlists_context_destroy,
1430};
1431
1432static struct intel_context *
1433execlists_context_pin(struct intel_engine_cs *engine,
1434 struct i915_gem_context *ctx)
e84fe803 1435{
ab82a063 1436 struct intel_context *ce = to_intel_context(ctx, engine);
e84fe803 1437
91c8a326 1438 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
321fe304 1439
1fc44d9b
CW
1440 if (likely(ce->pin_count++))
1441 return ce;
1442 GEM_BUG_ON(!ce->pin_count); /* no overflow please! */
dcb4c12a 1443
1fc44d9b 1444 ce->ops = &execlists_context_ops;
321fe304 1445
1fc44d9b 1446 return __execlists_context_pin(engine, ctx, ce);
dcb4c12a
OM
1447}
1448
e61e0f51 1449static int execlists_request_alloc(struct i915_request *request)
ef11c01d 1450{
fd138212 1451 int ret;
ef11c01d 1452
1fc44d9b 1453 GEM_BUG_ON(!request->hw_context->pin_count);
e8a9c58f 1454
ef11c01d
CW
1455 /* Flush enough space to reduce the likelihood of waiting after
1456 * we start building the request - in which case we will just
1457 * have to repeat work.
1458 */
1459 request->reserved_space += EXECLISTS_REQUEST_SIZE;
1460
fd138212
CW
1461 ret = intel_ring_wait_for_space(request->ring, request->reserved_space);
1462 if (ret)
1463 return ret;
ef11c01d 1464
ef11c01d
CW
1465 /* Note that after this point, we have committed to using
1466 * this request as it is being used to both track the
1467 * state of engine initialisation and liveness of the
1468 * golden renderstate above. Think twice before you try
1469 * to cancel/unwind this request now.
1470 */
1471
1472 request->reserved_space -= EXECLISTS_REQUEST_SIZE;
1473 return 0;
ef11c01d
CW
1474}
1475
9e000847
AS
1476/*
1477 * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
1478 * PIPE_CONTROL instruction. This is required for the flush to happen correctly
1479 * but there is a slight complication as this is applied in WA batch where the
1480 * values are only initialized once so we cannot take register value at the
1481 * beginning and reuse it further; hence we save its value to memory, upload a
1482 * constant value with bit21 set and then we restore it back with the saved value.
1483 * To simplify the WA, a constant value is formed by using the default value
1484 * of this register. This shouldn't be a problem because we are only modifying
1485 * it for a short period and this batch in non-premptible. We can ofcourse
1486 * use additional instructions that read the actual value of the register
1487 * at that time and set our bit of interest but it makes the WA complicated.
1488 *
1489 * This WA is also required for Gen9 so extracting as a function avoids
1490 * code duplication.
1491 */
097d4f1c
TU
1492static u32 *
1493gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine, u32 *batch)
17ee950d 1494{
097d4f1c
TU
1495 *batch++ = MI_STORE_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1496 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1497 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
1498 *batch++ = 0;
1499
1500 *batch++ = MI_LOAD_REGISTER_IMM(1);
1501 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1502 *batch++ = 0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES;
1503
9f235dfa
TU
1504 batch = gen8_emit_pipe_control(batch,
1505 PIPE_CONTROL_CS_STALL |
1506 PIPE_CONTROL_DC_FLUSH_ENABLE,
1507 0);
097d4f1c
TU
1508
1509 *batch++ = MI_LOAD_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1510 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1511 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
1512 *batch++ = 0;
1513
1514 return batch;
17ee950d
AS
1515}
1516
6e5248b5
DV
1517/*
1518 * Typically we only have one indirect_ctx and per_ctx batch buffer which are
1519 * initialized at the beginning and shared across all contexts but this field
1520 * helps us to have multiple batches at different offsets and select them based
1521 * on a criteria. At the moment this batch always start at the beginning of the page
1522 * and at this point we don't have multiple wa_ctx batch buffers.
4d78c8dc 1523 *
6e5248b5
DV
1524 * The number of WA applied are not known at the beginning; we use this field
1525 * to return the no of DWORDS written.
17ee950d 1526 *
6e5248b5
DV
1527 * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
1528 * so it adds NOOPs as padding to make it cacheline aligned.
1529 * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
1530 * makes a complete batch buffer.
17ee950d 1531 */
097d4f1c 1532static u32 *gen8_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
17ee950d 1533{
7ad00d1a 1534 /* WaDisableCtxRestoreArbitration:bdw,chv */
097d4f1c 1535 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
17ee950d 1536
c82435bb 1537 /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
097d4f1c
TU
1538 if (IS_BROADWELL(engine->i915))
1539 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
c82435bb 1540
0160f055
AS
1541 /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
1542 /* Actual scratch location is at 128 bytes offset */
9f235dfa
TU
1543 batch = gen8_emit_pipe_control(batch,
1544 PIPE_CONTROL_FLUSH_L3 |
1545 PIPE_CONTROL_GLOBAL_GTT_IVB |
1546 PIPE_CONTROL_CS_STALL |
1547 PIPE_CONTROL_QW_WRITE,
1548 i915_ggtt_offset(engine->scratch) +
1549 2 * CACHELINE_BYTES);
0160f055 1550
beecec90
CW
1551 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1552
17ee950d 1553 /* Pad to end of cacheline */
097d4f1c
TU
1554 while ((unsigned long)batch % CACHELINE_BYTES)
1555 *batch++ = MI_NOOP;
17ee950d
AS
1556
1557 /*
1558 * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
1559 * execution depends on the length specified in terms of cache lines
1560 * in the register CTX_RCS_INDIRECT_CTX
1561 */
1562
097d4f1c 1563 return batch;
17ee950d
AS
1564}
1565
097d4f1c 1566static u32 *gen9_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
0504cffc 1567{
beecec90
CW
1568 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
1569
9fb5026f 1570 /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt,glk */
097d4f1c 1571 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
a4106a78 1572
9fb5026f 1573 /* WaDisableGatherAtSetShaderCommonSlice:skl,bxt,kbl,glk */
097d4f1c
TU
1574 *batch++ = MI_LOAD_REGISTER_IMM(1);
1575 *batch++ = i915_mmio_reg_offset(COMMON_SLICE_CHICKEN2);
1576 *batch++ = _MASKED_BIT_DISABLE(
1577 GEN9_DISABLE_GATHER_AT_SET_SHADER_COMMON_SLICE);
1578 *batch++ = MI_NOOP;
873e8171 1579
066d4628
MK
1580 /* WaClearSlmSpaceAtContextSwitch:kbl */
1581 /* Actual scratch location is at 128 bytes offset */
097d4f1c 1582 if (IS_KBL_REVID(engine->i915, 0, KBL_REVID_A0)) {
9f235dfa
TU
1583 batch = gen8_emit_pipe_control(batch,
1584 PIPE_CONTROL_FLUSH_L3 |
1585 PIPE_CONTROL_GLOBAL_GTT_IVB |
1586 PIPE_CONTROL_CS_STALL |
1587 PIPE_CONTROL_QW_WRITE,
1588 i915_ggtt_offset(engine->scratch)
1589 + 2 * CACHELINE_BYTES);
066d4628 1590 }
3485d99e 1591
9fb5026f 1592 /* WaMediaPoolStateCmdInWABB:bxt,glk */
3485d99e
TG
1593 if (HAS_POOLED_EU(engine->i915)) {
1594 /*
1595 * EU pool configuration is setup along with golden context
1596 * during context initialization. This value depends on
1597 * device type (2x6 or 3x6) and needs to be updated based
1598 * on which subslice is disabled especially for 2x6
1599 * devices, however it is safe to load default
1600 * configuration of 3x6 device instead of masking off
1601 * corresponding bits because HW ignores bits of a disabled
1602 * subslice and drops down to appropriate config. Please
1603 * see render_state_setup() in i915_gem_render_state.c for
1604 * possible configurations, to avoid duplication they are
1605 * not shown here again.
1606 */
097d4f1c
TU
1607 *batch++ = GEN9_MEDIA_POOL_STATE;
1608 *batch++ = GEN9_MEDIA_POOL_ENABLE;
1609 *batch++ = 0x00777000;
1610 *batch++ = 0;
1611 *batch++ = 0;
1612 *batch++ = 0;
3485d99e
TG
1613 }
1614
beecec90
CW
1615 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1616
0504cffc 1617 /* Pad to end of cacheline */
097d4f1c
TU
1618 while ((unsigned long)batch % CACHELINE_BYTES)
1619 *batch++ = MI_NOOP;
0504cffc 1620
097d4f1c 1621 return batch;
0504cffc
AS
1622}
1623
4b6ce681
RA
1624static u32 *
1625gen10_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
1626{
1627 int i;
1628
1629 /*
1630 * WaPipeControlBefore3DStateSamplePattern: cnl
1631 *
1632 * Ensure the engine is idle prior to programming a
1633 * 3DSTATE_SAMPLE_PATTERN during a context restore.
1634 */
1635 batch = gen8_emit_pipe_control(batch,
1636 PIPE_CONTROL_CS_STALL,
1637 0);
1638 /*
1639 * WaPipeControlBefore3DStateSamplePattern says we need 4 dwords for
1640 * the PIPE_CONTROL followed by 12 dwords of 0x0, so 16 dwords in
1641 * total. However, a PIPE_CONTROL is 6 dwords long, not 4, which is
1642 * confusing. Since gen8_emit_pipe_control() already advances the
1643 * batch by 6 dwords, we advance the other 10 here, completing a
1644 * cacheline. It's not clear if the workaround requires this padding
1645 * before other commands, or if it's just the regular padding we would
1646 * already have for the workaround bb, so leave it here for now.
1647 */
1648 for (i = 0; i < 10; i++)
1649 *batch++ = MI_NOOP;
1650
1651 /* Pad to end of cacheline */
1652 while ((unsigned long)batch % CACHELINE_BYTES)
1653 *batch++ = MI_NOOP;
1654
1655 return batch;
1656}
1657
097d4f1c
TU
1658#define CTX_WA_BB_OBJ_SIZE (PAGE_SIZE)
1659
1660static int lrc_setup_wa_ctx(struct intel_engine_cs *engine)
17ee950d 1661{
48bb74e4
CW
1662 struct drm_i915_gem_object *obj;
1663 struct i915_vma *vma;
1664 int err;
17ee950d 1665
097d4f1c 1666 obj = i915_gem_object_create(engine->i915, CTX_WA_BB_OBJ_SIZE);
48bb74e4
CW
1667 if (IS_ERR(obj))
1668 return PTR_ERR(obj);
17ee950d 1669
a01cb37a 1670 vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
48bb74e4
CW
1671 if (IS_ERR(vma)) {
1672 err = PTR_ERR(vma);
1673 goto err;
17ee950d
AS
1674 }
1675
48bb74e4
CW
1676 err = i915_vma_pin(vma, 0, PAGE_SIZE, PIN_GLOBAL | PIN_HIGH);
1677 if (err)
1678 goto err;
1679
1680 engine->wa_ctx.vma = vma;
17ee950d 1681 return 0;
48bb74e4
CW
1682
1683err:
1684 i915_gem_object_put(obj);
1685 return err;
17ee950d
AS
1686}
1687
097d4f1c 1688static void lrc_destroy_wa_ctx(struct intel_engine_cs *engine)
17ee950d 1689{
19880c4a 1690 i915_vma_unpin_and_release(&engine->wa_ctx.vma);
17ee950d
AS
1691}
1692
097d4f1c
TU
1693typedef u32 *(*wa_bb_func_t)(struct intel_engine_cs *engine, u32 *batch);
1694
0bc40be8 1695static int intel_init_workaround_bb(struct intel_engine_cs *engine)
17ee950d 1696{
48bb74e4 1697 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
097d4f1c
TU
1698 struct i915_wa_ctx_bb *wa_bb[2] = { &wa_ctx->indirect_ctx,
1699 &wa_ctx->per_ctx };
1700 wa_bb_func_t wa_bb_fn[2];
17ee950d 1701 struct page *page;
097d4f1c
TU
1702 void *batch, *batch_ptr;
1703 unsigned int i;
48bb74e4 1704 int ret;
17ee950d 1705
10bde236 1706 if (GEM_WARN_ON(engine->id != RCS))
097d4f1c 1707 return -EINVAL;
17ee950d 1708
097d4f1c 1709 switch (INTEL_GEN(engine->i915)) {
cc38cae7
OM
1710 case 11:
1711 return 0;
90007bca 1712 case 10:
4b6ce681
RA
1713 wa_bb_fn[0] = gen10_init_indirectctx_bb;
1714 wa_bb_fn[1] = NULL;
1715 break;
097d4f1c
TU
1716 case 9:
1717 wa_bb_fn[0] = gen9_init_indirectctx_bb;
b8aa2233 1718 wa_bb_fn[1] = NULL;
097d4f1c
TU
1719 break;
1720 case 8:
1721 wa_bb_fn[0] = gen8_init_indirectctx_bb;
3ad7b52d 1722 wa_bb_fn[1] = NULL;
097d4f1c
TU
1723 break;
1724 default:
1725 MISSING_CASE(INTEL_GEN(engine->i915));
5e60d790 1726 return 0;
0504cffc 1727 }
5e60d790 1728
097d4f1c 1729 ret = lrc_setup_wa_ctx(engine);
17ee950d
AS
1730 if (ret) {
1731 DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
1732 return ret;
1733 }
1734
48bb74e4 1735 page = i915_gem_object_get_dirty_page(wa_ctx->vma->obj, 0);
097d4f1c 1736 batch = batch_ptr = kmap_atomic(page);
17ee950d 1737
097d4f1c
TU
1738 /*
1739 * Emit the two workaround batch buffers, recording the offset from the
1740 * start of the workaround batch buffer object for each and their
1741 * respective sizes.
1742 */
1743 for (i = 0; i < ARRAY_SIZE(wa_bb_fn); i++) {
1744 wa_bb[i]->offset = batch_ptr - batch;
1d2a19c2
CW
1745 if (GEM_WARN_ON(!IS_ALIGNED(wa_bb[i]->offset,
1746 CACHELINE_BYTES))) {
097d4f1c
TU
1747 ret = -EINVAL;
1748 break;
1749 }
604a8f6f
CW
1750 if (wa_bb_fn[i])
1751 batch_ptr = wa_bb_fn[i](engine, batch_ptr);
097d4f1c 1752 wa_bb[i]->size = batch_ptr - (batch + wa_bb[i]->offset);
17ee950d
AS
1753 }
1754
097d4f1c
TU
1755 BUG_ON(batch_ptr - batch > CTX_WA_BB_OBJ_SIZE);
1756
17ee950d
AS
1757 kunmap_atomic(batch);
1758 if (ret)
097d4f1c 1759 lrc_destroy_wa_ctx(engine);
17ee950d
AS
1760
1761 return ret;
1762}
1763
f3c9d407 1764static void enable_execlists(struct intel_engine_cs *engine)
9b1136d5 1765{
c033666a 1766 struct drm_i915_private *dev_priv = engine->i915;
f3c9d407
CW
1767
1768 I915_WRITE(RING_HWSTAM(engine->mmio_base), 0xffffffff);
225701fc
KG
1769
1770 /*
1771 * Make sure we're not enabling the new 12-deep CSB
1772 * FIFO as that requires a slightly updated handling
1773 * in the ctx switch irq. Since we're currently only
1774 * using only 2 elements of the enhanced execlists the
1775 * deeper FIFO it's not needed and it's not worth adding
1776 * more statements to the irq handler to support it.
1777 */
1778 if (INTEL_GEN(dev_priv) >= 11)
1779 I915_WRITE(RING_MODE_GEN7(engine),
1780 _MASKED_BIT_DISABLE(GEN11_GFX_DISABLE_LEGACY_MODE));
1781 else
1782 I915_WRITE(RING_MODE_GEN7(engine),
1783 _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
1784
9a4dc803
CW
1785 I915_WRITE(RING_MI_MODE(engine->mmio_base),
1786 _MASKED_BIT_DISABLE(STOP_RING));
1787
f3c9d407
CW
1788 I915_WRITE(RING_HWS_PGA(engine->mmio_base),
1789 engine->status_page.ggtt_offset);
1790 POSTING_READ(RING_HWS_PGA(engine->mmio_base));
e840130a
CW
1791
1792 /* Following the reset, we need to reload the CSB read/write pointers */
1793 engine->execlists.csb_head = -1;
f3c9d407
CW
1794}
1795
9a4dc803
CW
1796static bool unexpected_starting_state(struct intel_engine_cs *engine)
1797{
1798 struct drm_i915_private *dev_priv = engine->i915;
1799 bool unexpected = false;
1800
1801 if (I915_READ(RING_MI_MODE(engine->mmio_base)) & STOP_RING) {
1802 DRM_DEBUG_DRIVER("STOP_RING still set in RING_MI_MODE\n");
1803 unexpected = true;
1804 }
1805
1806 return unexpected;
1807}
1808
f3c9d407
CW
1809static int gen8_init_common_ring(struct intel_engine_cs *engine)
1810{
b620e870 1811 struct intel_engine_execlists * const execlists = &engine->execlists;
821ed7df
CW
1812 int ret;
1813
1814 ret = intel_mocs_init_engine(engine);
1815 if (ret)
1816 return ret;
9b1136d5 1817
ad07dfcd 1818 intel_engine_reset_breadcrumbs(engine);
f3b8f912 1819 intel_engine_init_hangcheck(engine);
821ed7df 1820
9a4dc803
CW
1821 if (GEM_SHOW_DEBUG() && unexpected_starting_state(engine)) {
1822 struct drm_printer p = drm_debug_printer(__func__);
1823
1824 intel_engine_dump(engine, &p, NULL);
1825 }
1826
f3c9d407 1827 enable_execlists(engine);
9b1136d5 1828
64f09f00 1829 /* After a GPU reset, we may have requests to replay */
9bdc3573 1830 if (execlists->first)
c6dce8f1 1831 tasklet_schedule(&execlists->tasklet);
6b764a59 1832
821ed7df 1833 return 0;
9b1136d5
OM
1834}
1835
0bc40be8 1836static int gen8_init_render_ring(struct intel_engine_cs *engine)
9b1136d5 1837{
c033666a 1838 struct drm_i915_private *dev_priv = engine->i915;
9b1136d5
OM
1839 int ret;
1840
0bc40be8 1841 ret = gen8_init_common_ring(engine);
9b1136d5
OM
1842 if (ret)
1843 return ret;
1844
f4ecfbfc 1845 intel_whitelist_workarounds_apply(engine);
59b449d5 1846
9b1136d5
OM
1847 /* We need to disable the AsyncFlip performance optimisations in order
1848 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1849 * programmed to '1' on all products.
1850 *
1851 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
1852 */
1853 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1854
9b1136d5
OM
1855 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1856
59b449d5 1857 return 0;
9b1136d5
OM
1858}
1859
0bc40be8 1860static int gen9_init_render_ring(struct intel_engine_cs *engine)
82ef822e
DL
1861{
1862 int ret;
1863
0bc40be8 1864 ret = gen8_init_common_ring(engine);
82ef822e
DL
1865 if (ret)
1866 return ret;
1867
f4ecfbfc 1868 intel_whitelist_workarounds_apply(engine);
59b449d5
OM
1869
1870 return 0;
82ef822e
DL
1871}
1872
5adfb772
CW
1873static struct i915_request *
1874execlists_reset_prepare(struct intel_engine_cs *engine)
1875{
1876 struct intel_engine_execlists * const execlists = &engine->execlists;
63572937 1877 struct i915_request *request, *active;
5adfb772
CW
1878
1879 GEM_TRACE("%s\n", engine->name);
1880
1881 /*
1882 * Prevent request submission to the hardware until we have
1883 * completed the reset in i915_gem_reset_finish(). If a request
1884 * is completed by one engine, it may then queue a request
1885 * to a second via its execlists->tasklet *just* as we are
1886 * calling engine->init_hw() and also writing the ELSP.
1887 * Turning off the execlists->tasklet until the reset is over
1888 * prevents the race.
1889 */
1890 __tasklet_disable_sync_once(&execlists->tasklet);
1891
63572937
CW
1892 /*
1893 * We want to flush the pending context switches, having disabled
1894 * the tasklet above, we can assume exclusive access to the execlists.
1895 * For this allows us to catch up with an inflight preemption event,
1896 * and avoid blaming an innocent request if the stall was due to the
1897 * preemption itself.
1898 */
1899 if (test_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted))
1900 process_csb(engine);
1901
1902 /*
1903 * The last active request can then be no later than the last request
1904 * now in ELSP[0]. So search backwards from there, so that if the GPU
1905 * has advanced beyond the last CSB update, it will be pardoned.
1906 */
1907 active = NULL;
1908 request = port_request(execlists->port);
1909 if (request) {
1910 unsigned long flags;
1911
3f6e9822
CW
1912 /*
1913 * Prevent the breadcrumb from advancing before we decide
1914 * which request is currently active.
1915 */
1916 intel_engine_stop_cs(engine);
1917
63572937
CW
1918 spin_lock_irqsave(&engine->timeline.lock, flags);
1919 list_for_each_entry_from_reverse(request,
1920 &engine->timeline.requests,
1921 link) {
1922 if (__i915_request_completed(request,
1923 request->global_seqno))
1924 break;
1925
1926 active = request;
1927 }
1928 spin_unlock_irqrestore(&engine->timeline.lock, flags);
1929 }
1930
1931 return active;
5adfb772
CW
1932}
1933
1934static void execlists_reset(struct intel_engine_cs *engine,
1935 struct i915_request *request)
821ed7df 1936{
b620e870 1937 struct intel_engine_execlists * const execlists = &engine->execlists;
221ab971 1938 unsigned long flags;
5692251c 1939 u32 *regs;
cdb6ded4 1940
0c5c7df3
TU
1941 GEM_TRACE("%s request global=%x, current=%d\n",
1942 engine->name, request ? request->global_seqno : 0,
1943 intel_engine_get_seqno(engine));
42232213 1944
a3e38836
CW
1945 /* See execlists_cancel_requests() for the irq/spinlock split. */
1946 local_irq_save(flags);
221ab971 1947
cdb6ded4
CW
1948 /*
1949 * Catch up with any missed context-switch interrupts.
1950 *
1951 * Ideally we would just read the remaining CSB entries now that we
1952 * know the gpu is idle. However, the CSB registers are sometimes^W
1953 * often trashed across a GPU reset! Instead we have to rely on
1954 * guessing the missed context-switch events by looking at what
1955 * requests were completed.
1956 */
a4598d17 1957 execlists_cancel_port_requests(execlists);
46b3617d 1958 reset_irq(engine);
cdb6ded4 1959
221ab971 1960 /* Push back any incomplete requests for replay after the reset. */
a89d1f92 1961 spin_lock(&engine->timeline.lock);
a4598d17 1962 __unwind_incomplete_requests(engine);
a89d1f92 1963 spin_unlock(&engine->timeline.lock);
cdb6ded4 1964
a3e38836 1965 local_irq_restore(flags);
aebbc2d7 1966
a3e38836
CW
1967 /*
1968 * If the request was innocent, we leave the request in the ELSP
c0dcb203
CW
1969 * and will try to replay it on restarting. The context image may
1970 * have been corrupted by the reset, in which case we may have
1971 * to service a new GPU hang, but more likely we can continue on
1972 * without impact.
1973 *
1974 * If the request was guilty, we presume the context is corrupt
1975 * and have to at least restore the RING register in the context
1976 * image back to the expected values to skip over the guilty request.
1977 */
221ab971 1978 if (!request || request->fence.error != -EIO)
c0dcb203 1979 return;
821ed7df 1980
a3e38836
CW
1981 /*
1982 * We want a simple context + ring to execute the breadcrumb update.
a3aabe86
CW
1983 * We cannot rely on the context being intact across the GPU hang,
1984 * so clear it and rebuild just what we need for the breadcrumb.
1985 * All pending requests for this context will be zapped, and any
1986 * future request will be after userspace has had the opportunity
1987 * to recreate its own state.
1988 */
1fc44d9b 1989 regs = request->hw_context->lrc_reg_state;
fe0c4935
CW
1990 if (engine->pinned_default_state) {
1991 memcpy(regs, /* skip restoring the vanilla PPHWSP */
1992 engine->pinned_default_state + LRC_STATE_PN * PAGE_SIZE,
1993 engine->context_size - PAGE_SIZE);
5692251c 1994 }
4e0d64db
CW
1995 execlists_init_reg_state(regs,
1996 request->gem_context, engine, request->ring);
a3aabe86 1997
821ed7df 1998 /* Move the RING_HEAD onto the breadcrumb, past the hanging batch */
5692251c
CW
1999 regs[CTX_RING_BUFFER_START + 1] = i915_ggtt_offset(request->ring->vma);
2000 regs[CTX_RING_HEAD + 1] = request->postfix;
a3aabe86 2001
821ed7df 2002 request->ring->head = request->postfix;
821ed7df
CW
2003 intel_ring_update_space(request->ring);
2004
a3aabe86 2005 /* Reset WaIdleLiteRestore:bdw,skl as well */
7e4992ac 2006 unwind_wa_tail(request);
821ed7df
CW
2007}
2008
5adfb772
CW
2009static void execlists_reset_finish(struct intel_engine_cs *engine)
2010{
2011 tasklet_enable(&engine->execlists.tasklet);
2012
2013 GEM_TRACE("%s\n", engine->name);
2014}
2015
e61e0f51 2016static int intel_logical_ring_emit_pdps(struct i915_request *rq)
7a01a0a2 2017{
4e0d64db 2018 struct i915_hw_ppgtt *ppgtt = rq->gem_context->ppgtt;
e61e0f51 2019 struct intel_engine_cs *engine = rq->engine;
e7167769 2020 const int num_lri_cmds = GEN8_3LVL_PDPES * 2;
73dec95e
TU
2021 u32 *cs;
2022 int i;
7a01a0a2 2023
e61e0f51 2024 cs = intel_ring_begin(rq, num_lri_cmds * 2 + 2);
73dec95e
TU
2025 if (IS_ERR(cs))
2026 return PTR_ERR(cs);
7a01a0a2 2027
73dec95e 2028 *cs++ = MI_LOAD_REGISTER_IMM(num_lri_cmds);
e7167769 2029 for (i = GEN8_3LVL_PDPES - 1; i >= 0; i--) {
7a01a0a2
MT
2030 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
2031
73dec95e
TU
2032 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, i));
2033 *cs++ = upper_32_bits(pd_daddr);
2034 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, i));
2035 *cs++ = lower_32_bits(pd_daddr);
7a01a0a2
MT
2036 }
2037
73dec95e 2038 *cs++ = MI_NOOP;
e61e0f51 2039 intel_ring_advance(rq, cs);
7a01a0a2
MT
2040
2041 return 0;
2042}
2043
e61e0f51 2044static int gen8_emit_bb_start(struct i915_request *rq,
803688ba 2045 u64 offset, u32 len,
54af56db 2046 const unsigned int flags)
15648585 2047{
73dec95e 2048 u32 *cs;
15648585
OM
2049 int ret;
2050
7a01a0a2
MT
2051 /* Don't rely in hw updating PDPs, specially in lite-restore.
2052 * Ideally, we should set Force PD Restore in ctx descriptor,
2053 * but we can't. Force Restore would be a second option, but
2054 * it is unsafe in case of lite-restore (because the ctx is
2dba3239
MT
2055 * not idle). PML4 is allocated during ppgtt init so this is
2056 * not needed in 48-bit.*/
4e0d64db
CW
2057 if (rq->gem_context->ppgtt &&
2058 (intel_engine_flag(rq->engine) & rq->gem_context->ppgtt->pd_dirty_rings) &&
2059 !i915_vm_is_48bit(&rq->gem_context->ppgtt->base) &&
e61e0f51
CW
2060 !intel_vgpu_active(rq->i915)) {
2061 ret = intel_logical_ring_emit_pdps(rq);
54af56db
MK
2062 if (ret)
2063 return ret;
7a01a0a2 2064
4e0d64db 2065 rq->gem_context->ppgtt->pd_dirty_rings &= ~intel_engine_flag(rq->engine);
7a01a0a2
MT
2066 }
2067
74f94741 2068 cs = intel_ring_begin(rq, 6);
73dec95e
TU
2069 if (IS_ERR(cs))
2070 return PTR_ERR(cs);
15648585 2071
279f5a00
CW
2072 /*
2073 * WaDisableCtxRestoreArbitration:bdw,chv
2074 *
2075 * We don't need to perform MI_ARB_ENABLE as often as we do (in
2076 * particular all the gen that do not need the w/a at all!), if we
2077 * took care to make sure that on every switch into this context
2078 * (both ordinary and for preemption) that arbitrartion was enabled
2079 * we would be fine. However, there doesn't seem to be a downside to
2080 * being paranoid and making sure it is set before each batch and
2081 * every context-switch.
2082 *
2083 * Note that if we fail to enable arbitration before the request
2084 * is complete, then we do not see the context-switch interrupt and
2085 * the engine hangs (with RING_HEAD == RING_TAIL).
2086 *
2087 * That satisfies both the GPGPU w/a and our heavy-handed paranoia.
2088 */
3ad7b52d
CW
2089 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
2090
15648585 2091 /* FIXME(BDW): Address space and security selectors. */
54af56db
MK
2092 *cs++ = MI_BATCH_BUFFER_START_GEN8 |
2093 (flags & I915_DISPATCH_SECURE ? 0 : BIT(8)) |
2094 (flags & I915_DISPATCH_RS ? MI_BATCH_RESOURCE_STREAMER : 0);
73dec95e
TU
2095 *cs++ = lower_32_bits(offset);
2096 *cs++ = upper_32_bits(offset);
74f94741
CW
2097
2098 *cs++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
2099 *cs++ = MI_NOOP;
e61e0f51 2100 intel_ring_advance(rq, cs);
15648585
OM
2101
2102 return 0;
2103}
2104
31bb59cc 2105static void gen8_logical_ring_enable_irq(struct intel_engine_cs *engine)
73d477f6 2106{
c033666a 2107 struct drm_i915_private *dev_priv = engine->i915;
31bb59cc
CW
2108 I915_WRITE_IMR(engine,
2109 ~(engine->irq_enable_mask | engine->irq_keep_mask));
2110 POSTING_READ_FW(RING_IMR(engine->mmio_base));
73d477f6
OM
2111}
2112
31bb59cc 2113static void gen8_logical_ring_disable_irq(struct intel_engine_cs *engine)
73d477f6 2114{
c033666a 2115 struct drm_i915_private *dev_priv = engine->i915;
31bb59cc 2116 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
73d477f6
OM
2117}
2118
e61e0f51 2119static int gen8_emit_flush(struct i915_request *request, u32 mode)
4712274c 2120{
73dec95e 2121 u32 cmd, *cs;
4712274c 2122
73dec95e
TU
2123 cs = intel_ring_begin(request, 4);
2124 if (IS_ERR(cs))
2125 return PTR_ERR(cs);
4712274c
OM
2126
2127 cmd = MI_FLUSH_DW + 1;
2128
f0a1fb10
CW
2129 /* We always require a command barrier so that subsequent
2130 * commands, such as breadcrumb interrupts, are strictly ordered
2131 * wrt the contents of the write cache being flushed to memory
2132 * (and thus being coherent from the CPU).
2133 */
2134 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
2135
7c9cf4e3 2136 if (mode & EMIT_INVALIDATE) {
f0a1fb10 2137 cmd |= MI_INVALIDATE_TLB;
1dae2dfb 2138 if (request->engine->id == VCS)
f0a1fb10 2139 cmd |= MI_INVALIDATE_BSD;
4712274c
OM
2140 }
2141
73dec95e
TU
2142 *cs++ = cmd;
2143 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
2144 *cs++ = 0; /* upper addr */
2145 *cs++ = 0; /* value */
2146 intel_ring_advance(request, cs);
4712274c
OM
2147
2148 return 0;
2149}
2150
e61e0f51 2151static int gen8_emit_flush_render(struct i915_request *request,
7c9cf4e3 2152 u32 mode)
4712274c 2153{
b5321f30 2154 struct intel_engine_cs *engine = request->engine;
bde13ebd
CW
2155 u32 scratch_addr =
2156 i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES;
0b2d0934 2157 bool vf_flush_wa = false, dc_flush_wa = false;
73dec95e 2158 u32 *cs, flags = 0;
0b2d0934 2159 int len;
4712274c
OM
2160
2161 flags |= PIPE_CONTROL_CS_STALL;
2162
7c9cf4e3 2163 if (mode & EMIT_FLUSH) {
4712274c
OM
2164 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
2165 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
965fd602 2166 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
40a24488 2167 flags |= PIPE_CONTROL_FLUSH_ENABLE;
4712274c
OM
2168 }
2169
7c9cf4e3 2170 if (mode & EMIT_INVALIDATE) {
4712274c
OM
2171 flags |= PIPE_CONTROL_TLB_INVALIDATE;
2172 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
2173 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
2174 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
2175 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
2176 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
2177 flags |= PIPE_CONTROL_QW_WRITE;
2178 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
4712274c 2179
1a5a9ce7
BW
2180 /*
2181 * On GEN9: before VF_CACHE_INVALIDATE we need to emit a NULL
2182 * pipe control.
2183 */
c033666a 2184 if (IS_GEN9(request->i915))
1a5a9ce7 2185 vf_flush_wa = true;
0b2d0934
MK
2186
2187 /* WaForGAMHang:kbl */
2188 if (IS_KBL_REVID(request->i915, 0, KBL_REVID_B0))
2189 dc_flush_wa = true;
1a5a9ce7 2190 }
9647ff36 2191
0b2d0934
MK
2192 len = 6;
2193
2194 if (vf_flush_wa)
2195 len += 6;
2196
2197 if (dc_flush_wa)
2198 len += 12;
2199
73dec95e
TU
2200 cs = intel_ring_begin(request, len);
2201 if (IS_ERR(cs))
2202 return PTR_ERR(cs);
4712274c 2203
9f235dfa
TU
2204 if (vf_flush_wa)
2205 cs = gen8_emit_pipe_control(cs, 0, 0);
9647ff36 2206
9f235dfa
TU
2207 if (dc_flush_wa)
2208 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_DC_FLUSH_ENABLE,
2209 0);
0b2d0934 2210
9f235dfa 2211 cs = gen8_emit_pipe_control(cs, flags, scratch_addr);
0b2d0934 2212
9f235dfa
TU
2213 if (dc_flush_wa)
2214 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_CS_STALL, 0);
0b2d0934 2215
73dec95e 2216 intel_ring_advance(request, cs);
4712274c
OM
2217
2218 return 0;
2219}
2220
7c17d377
CW
2221/*
2222 * Reserve space for 2 NOOPs at the end of each request to be
2223 * used as a workaround for not being allowed to do lite
2224 * restore with HEAD==TAIL (WaIdleLiteRestore).
2225 */
e61e0f51 2226static void gen8_emit_wa_tail(struct i915_request *request, u32 *cs)
4da46e1e 2227{
beecec90
CW
2228 /* Ensure there's always at least one preemption point per-request. */
2229 *cs++ = MI_ARB_CHECK;
73dec95e
TU
2230 *cs++ = MI_NOOP;
2231 request->wa_tail = intel_ring_offset(request, cs);
caddfe71 2232}
4da46e1e 2233
e61e0f51 2234static void gen8_emit_breadcrumb(struct i915_request *request, u32 *cs)
caddfe71 2235{
7c17d377
CW
2236 /* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */
2237 BUILD_BUG_ON(I915_GEM_HWS_INDEX_ADDR & (1 << 5));
4da46e1e 2238
df77cd83
MW
2239 cs = gen8_emit_ggtt_write(cs, request->global_seqno,
2240 intel_hws_seqno_address(request->engine));
73dec95e 2241 *cs++ = MI_USER_INTERRUPT;
74f94741 2242 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
73dec95e 2243 request->tail = intel_ring_offset(request, cs);
ed1501d4 2244 assert_ring_tail_valid(request->ring, request->tail);
caddfe71 2245
73dec95e 2246 gen8_emit_wa_tail(request, cs);
7c17d377 2247}
98f29e8d
CW
2248static const int gen8_emit_breadcrumb_sz = 6 + WA_TAIL_DWORDS;
2249
e61e0f51 2250static void gen8_emit_breadcrumb_rcs(struct i915_request *request, u32 *cs)
7c17d377 2251{
ce81a65c
MW
2252 /* We're using qword write, seqno should be aligned to 8 bytes. */
2253 BUILD_BUG_ON(I915_GEM_HWS_INDEX & 1);
2254
df77cd83
MW
2255 cs = gen8_emit_ggtt_write_rcs(cs, request->global_seqno,
2256 intel_hws_seqno_address(request->engine));
73dec95e 2257 *cs++ = MI_USER_INTERRUPT;
74f94741 2258 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
73dec95e 2259 request->tail = intel_ring_offset(request, cs);
ed1501d4 2260 assert_ring_tail_valid(request->ring, request->tail);
caddfe71 2261
73dec95e 2262 gen8_emit_wa_tail(request, cs);
4da46e1e 2263}
df77cd83 2264static const int gen8_emit_breadcrumb_rcs_sz = 8 + WA_TAIL_DWORDS;
98f29e8d 2265
e61e0f51 2266static int gen8_init_rcs_context(struct i915_request *rq)
e7778be1
TD
2267{
2268 int ret;
2269
59b449d5 2270 ret = intel_ctx_workarounds_emit(rq);
e7778be1
TD
2271 if (ret)
2272 return ret;
2273
e61e0f51 2274 ret = intel_rcs_context_init_mocs(rq);
3bbaba0c
PA
2275 /*
2276 * Failing to program the MOCS is non-fatal.The system will not
2277 * run at peak performance. So generate an error and carry on.
2278 */
2279 if (ret)
2280 DRM_ERROR("MOCS failed to program: expect performance issues.\n");
2281
e61e0f51 2282 return i915_gem_render_state_emit(rq);
e7778be1
TD
2283}
2284
73e4d07f
OM
2285/**
2286 * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
14bb2c11 2287 * @engine: Engine Command Streamer.
73e4d07f 2288 */
0bc40be8 2289void intel_logical_ring_cleanup(struct intel_engine_cs *engine)
454afebd 2290{
6402c330 2291 struct drm_i915_private *dev_priv;
9832b9da 2292
27af5eea
TU
2293 /*
2294 * Tasklet cannot be active at this point due intel_mark_active/idle
2295 * so this is just for documentation.
2296 */
c6dce8f1
SAK
2297 if (WARN_ON(test_bit(TASKLET_STATE_SCHED,
2298 &engine->execlists.tasklet.state)))
2299 tasklet_kill(&engine->execlists.tasklet);
27af5eea 2300
c033666a 2301 dev_priv = engine->i915;
6402c330 2302
0bc40be8 2303 if (engine->buffer) {
0bc40be8 2304 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
b0366a54 2305 }
48d82387 2306
0bc40be8
TU
2307 if (engine->cleanup)
2308 engine->cleanup(engine);
48d82387 2309
e8a9c58f 2310 intel_engine_cleanup_common(engine);
17ee950d 2311
097d4f1c 2312 lrc_destroy_wa_ctx(engine);
f3c9d407 2313
c033666a 2314 engine->i915 = NULL;
3b3f1650
AG
2315 dev_priv->engine[engine->id] = NULL;
2316 kfree(engine);
454afebd
OM
2317}
2318
ff44ad51 2319static void execlists_set_default_submission(struct intel_engine_cs *engine)
ddd66c51 2320{
ff44ad51 2321 engine->submit_request = execlists_submit_request;
27a5f61b 2322 engine->cancel_requests = execlists_cancel_requests;
ff44ad51 2323 engine->schedule = execlists_schedule;
c6dce8f1 2324 engine->execlists.tasklet.func = execlists_submission_tasklet;
aba5e278 2325
1329115c
CW
2326 engine->reset.prepare = execlists_reset_prepare;
2327
aba5e278
CW
2328 engine->park = NULL;
2329 engine->unpark = NULL;
cf669b4e
TU
2330
2331 engine->flags |= I915_ENGINE_SUPPORTS_STATS;
2a694feb
CW
2332 if (engine->i915->preempt_context)
2333 engine->flags |= I915_ENGINE_HAS_PREEMPTION;
3fed1808
CW
2334
2335 engine->i915->caps.scheduler =
2336 I915_SCHEDULER_CAP_ENABLED |
2337 I915_SCHEDULER_CAP_PRIORITY;
2a694feb 2338 if (intel_engine_has_preemption(engine))
3fed1808 2339 engine->i915->caps.scheduler |= I915_SCHEDULER_CAP_PREEMPTION;
ddd66c51
CW
2340}
2341
c9cacf93 2342static void
e1382efb 2343logical_ring_default_vfuncs(struct intel_engine_cs *engine)
c9cacf93
TU
2344{
2345 /* Default vfuncs which can be overriden by each engine. */
0bc40be8 2346 engine->init_hw = gen8_init_common_ring;
5adfb772
CW
2347
2348 engine->reset.prepare = execlists_reset_prepare;
2349 engine->reset.reset = execlists_reset;
2350 engine->reset.finish = execlists_reset_finish;
e8a9c58f
CW
2351
2352 engine->context_pin = execlists_context_pin;
f73e7399
CW
2353 engine->request_alloc = execlists_request_alloc;
2354
0bc40be8 2355 engine->emit_flush = gen8_emit_flush;
9b81d556 2356 engine->emit_breadcrumb = gen8_emit_breadcrumb;
98f29e8d 2357 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_sz;
ff44ad51
CW
2358
2359 engine->set_default_submission = execlists_set_default_submission;
ddd66c51 2360
d4ccceb0
TU
2361 if (INTEL_GEN(engine->i915) < 11) {
2362 engine->irq_enable = gen8_logical_ring_enable_irq;
2363 engine->irq_disable = gen8_logical_ring_disable_irq;
2364 } else {
2365 /*
2366 * TODO: On Gen11 interrupt masks need to be clear
2367 * to allow C6 entry. Keep interrupts enabled at
2368 * and take the hit of generating extra interrupts
2369 * until a more refined solution exists.
2370 */
2371 }
0bc40be8 2372 engine->emit_bb_start = gen8_emit_bb_start;
c9cacf93
TU
2373}
2374
d9f3af96 2375static inline void
c2c7f240 2376logical_ring_default_irqs(struct intel_engine_cs *engine)
d9f3af96 2377{
fa6f071d
DCS
2378 unsigned int shift = 0;
2379
2380 if (INTEL_GEN(engine->i915) < 11) {
2381 const u8 irq_shifts[] = {
2382 [RCS] = GEN8_RCS_IRQ_SHIFT,
2383 [BCS] = GEN8_BCS_IRQ_SHIFT,
2384 [VCS] = GEN8_VCS1_IRQ_SHIFT,
2385 [VCS2] = GEN8_VCS2_IRQ_SHIFT,
2386 [VECS] = GEN8_VECS_IRQ_SHIFT,
2387 };
2388
2389 shift = irq_shifts[engine->id];
2390 }
2391
0bc40be8
TU
2392 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << shift;
2393 engine->irq_keep_mask = GT_CONTEXT_SWITCH_INTERRUPT << shift;
d9f3af96
TU
2394}
2395
bb45438f
TU
2396static void
2397logical_ring_setup(struct intel_engine_cs *engine)
2398{
2399 struct drm_i915_private *dev_priv = engine->i915;
2400 enum forcewake_domains fw_domains;
2401
019bf277
TU
2402 intel_engine_setup_common(engine);
2403
bb45438f
TU
2404 /* Intentionally left blank. */
2405 engine->buffer = NULL;
2406
2407 fw_domains = intel_uncore_forcewake_for_reg(dev_priv,
2408 RING_ELSP(engine),
2409 FW_REG_WRITE);
2410
2411 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
2412 RING_CONTEXT_STATUS_PTR(engine),
2413 FW_REG_READ | FW_REG_WRITE);
2414
2415 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
2416 RING_CONTEXT_STATUS_BUF_BASE(engine),
2417 FW_REG_READ);
2418
b620e870 2419 engine->execlists.fw_domains = fw_domains;
bb45438f 2420
c6dce8f1
SAK
2421 tasklet_init(&engine->execlists.tasklet,
2422 execlists_submission_tasklet, (unsigned long)engine);
bb45438f 2423
bb45438f
TU
2424 logical_ring_default_vfuncs(engine);
2425 logical_ring_default_irqs(engine);
bb45438f
TU
2426}
2427
486e93f7 2428static int logical_ring_init(struct intel_engine_cs *engine)
a19d6ff2 2429{
a19d6ff2
TU
2430 int ret;
2431
019bf277 2432 ret = intel_engine_init_common(engine);
a19d6ff2
TU
2433 if (ret)
2434 goto error;
2435
05f0addd
TD
2436 if (HAS_LOGICAL_RING_ELSQ(engine->i915)) {
2437 engine->execlists.submit_reg = engine->i915->regs +
2438 i915_mmio_reg_offset(RING_EXECLIST_SQ_CONTENTS(engine));
2439 engine->execlists.ctrl_reg = engine->i915->regs +
2440 i915_mmio_reg_offset(RING_EXECLIST_CONTROL(engine));
2441 } else {
2442 engine->execlists.submit_reg = engine->i915->regs +
2443 i915_mmio_reg_offset(RING_ELSP(engine));
2444 }
693cfbf0 2445
d6376374 2446 engine->execlists.preempt_complete_status = ~0u;
ab82a063
CW
2447 if (engine->i915->preempt_context) {
2448 struct intel_context *ce =
2449 to_intel_context(engine->i915->preempt_context, engine);
2450
d6376374 2451 engine->execlists.preempt_complete_status =
ab82a063
CW
2452 upper_32_bits(ce->lrc_desc);
2453 }
d6376374 2454
a19d6ff2
TU
2455 return 0;
2456
2457error:
2458 intel_logical_ring_cleanup(engine);
2459 return ret;
2460}
2461
88d2ba2e 2462int logical_render_ring_init(struct intel_engine_cs *engine)
a19d6ff2
TU
2463{
2464 struct drm_i915_private *dev_priv = engine->i915;
2465 int ret;
2466
bb45438f
TU
2467 logical_ring_setup(engine);
2468
a19d6ff2
TU
2469 if (HAS_L3_DPF(dev_priv))
2470 engine->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
2471
2472 /* Override some for render ring. */
2473 if (INTEL_GEN(dev_priv) >= 9)
2474 engine->init_hw = gen9_init_render_ring;
2475 else
2476 engine->init_hw = gen8_init_render_ring;
2477 engine->init_context = gen8_init_rcs_context;
a19d6ff2 2478 engine->emit_flush = gen8_emit_flush_render;
df77cd83
MW
2479 engine->emit_breadcrumb = gen8_emit_breadcrumb_rcs;
2480 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_rcs_sz;
a19d6ff2 2481
f51455d4 2482 ret = intel_engine_create_scratch(engine, PAGE_SIZE);
a19d6ff2
TU
2483 if (ret)
2484 return ret;
2485
2486 ret = intel_init_workaround_bb(engine);
2487 if (ret) {
2488 /*
2489 * We continue even if we fail to initialize WA batch
2490 * because we only expect rare glitches but nothing
2491 * critical to prevent us from using GPU
2492 */
2493 DRM_ERROR("WA batch buffer initialization failed: %d\n",
2494 ret);
2495 }
2496
d038fc7e 2497 return logical_ring_init(engine);
a19d6ff2
TU
2498}
2499
88d2ba2e 2500int logical_xcs_ring_init(struct intel_engine_cs *engine)
bb45438f
TU
2501{
2502 logical_ring_setup(engine);
2503
2504 return logical_ring_init(engine);
454afebd
OM
2505}
2506
0cea6502 2507static u32
c033666a 2508make_rpcs(struct drm_i915_private *dev_priv)
0cea6502
JM
2509{
2510 u32 rpcs = 0;
2511
2512 /*
2513 * No explicit RPCS request is needed to ensure full
2514 * slice/subslice/EU enablement prior to Gen9.
2515 */
c033666a 2516 if (INTEL_GEN(dev_priv) < 9)
0cea6502
JM
2517 return 0;
2518
2519 /*
2520 * Starting in Gen9, render power gating can leave
2521 * slice/subslice/EU in a partially enabled state. We
2522 * must make an explicit request through RPCS for full
2523 * enablement.
2524 */
43b67998 2525 if (INTEL_INFO(dev_priv)->sseu.has_slice_pg) {
0cea6502 2526 rpcs |= GEN8_RPCS_S_CNT_ENABLE;
f08a0c92 2527 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.slice_mask) <<
0cea6502
JM
2528 GEN8_RPCS_S_CNT_SHIFT;
2529 rpcs |= GEN8_RPCS_ENABLE;
2530 }
2531
43b67998 2532 if (INTEL_INFO(dev_priv)->sseu.has_subslice_pg) {
0cea6502 2533 rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
8cc76693 2534 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.subslice_mask[0]) <<
0cea6502
JM
2535 GEN8_RPCS_SS_CNT_SHIFT;
2536 rpcs |= GEN8_RPCS_ENABLE;
2537 }
2538
43b67998
ID
2539 if (INTEL_INFO(dev_priv)->sseu.has_eu_pg) {
2540 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
0cea6502 2541 GEN8_RPCS_EU_MIN_SHIFT;
43b67998 2542 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
0cea6502
JM
2543 GEN8_RPCS_EU_MAX_SHIFT;
2544 rpcs |= GEN8_RPCS_ENABLE;
2545 }
2546
2547 return rpcs;
2548}
2549
0bc40be8 2550static u32 intel_lr_indirect_ctx_offset(struct intel_engine_cs *engine)
71562919
MT
2551{
2552 u32 indirect_ctx_offset;
2553
c033666a 2554 switch (INTEL_GEN(engine->i915)) {
71562919 2555 default:
c033666a 2556 MISSING_CASE(INTEL_GEN(engine->i915));
71562919 2557 /* fall through */
fd034c77
MT
2558 case 11:
2559 indirect_ctx_offset =
2560 GEN11_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2561 break;
7bd0a2c6
MT
2562 case 10:
2563 indirect_ctx_offset =
2564 GEN10_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2565 break;
71562919
MT
2566 case 9:
2567 indirect_ctx_offset =
2568 GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2569 break;
2570 case 8:
2571 indirect_ctx_offset =
2572 GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2573 break;
2574 }
2575
2576 return indirect_ctx_offset;
2577}
2578
56e51bf0 2579static void execlists_init_reg_state(u32 *regs,
a3aabe86
CW
2580 struct i915_gem_context *ctx,
2581 struct intel_engine_cs *engine,
2582 struct intel_ring *ring)
8670d6f9 2583{
a3aabe86
CW
2584 struct drm_i915_private *dev_priv = engine->i915;
2585 struct i915_hw_ppgtt *ppgtt = ctx->ppgtt ?: dev_priv->mm.aliasing_ppgtt;
56e51bf0 2586 u32 base = engine->mmio_base;
1fc44d9b 2587 bool rcs = engine->class == RENDER_CLASS;
56e51bf0
TU
2588
2589 /* A context is actually a big batch buffer with several
2590 * MI_LOAD_REGISTER_IMM commands followed by (reg, value) pairs. The
2591 * values we are setting here are only for the first context restore:
2592 * on a subsequent save, the GPU will recreate this batchbuffer with new
2593 * values (including all the missing MI_LOAD_REGISTER_IMM commands that
2594 * we are not initializing here).
2595 */
2596 regs[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(rcs ? 14 : 11) |
2597 MI_LRI_FORCE_POSTED;
2598
2599 CTX_REG(regs, CTX_CONTEXT_CONTROL, RING_CONTEXT_CONTROL(engine),
09b1a4e4
CW
2600 _MASKED_BIT_DISABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
2601 CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT) |
56e51bf0 2602 _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
56e51bf0
TU
2603 (HAS_RESOURCE_STREAMER(dev_priv) ?
2604 CTX_CTRL_RS_CTX_ENABLE : 0)));
2605 CTX_REG(regs, CTX_RING_HEAD, RING_HEAD(base), 0);
2606 CTX_REG(regs, CTX_RING_TAIL, RING_TAIL(base), 0);
2607 CTX_REG(regs, CTX_RING_BUFFER_START, RING_START(base), 0);
2608 CTX_REG(regs, CTX_RING_BUFFER_CONTROL, RING_CTL(base),
2609 RING_CTL_SIZE(ring->size) | RING_VALID);
2610 CTX_REG(regs, CTX_BB_HEAD_U, RING_BBADDR_UDW(base), 0);
2611 CTX_REG(regs, CTX_BB_HEAD_L, RING_BBADDR(base), 0);
2612 CTX_REG(regs, CTX_BB_STATE, RING_BBSTATE(base), RING_BB_PPGTT);
2613 CTX_REG(regs, CTX_SECOND_BB_HEAD_U, RING_SBBADDR_UDW(base), 0);
2614 CTX_REG(regs, CTX_SECOND_BB_HEAD_L, RING_SBBADDR(base), 0);
2615 CTX_REG(regs, CTX_SECOND_BB_STATE, RING_SBBSTATE(base), 0);
2616 if (rcs) {
604a8f6f
CW
2617 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
2618
56e51bf0
TU
2619 CTX_REG(regs, CTX_RCS_INDIRECT_CTX, RING_INDIRECT_CTX(base), 0);
2620 CTX_REG(regs, CTX_RCS_INDIRECT_CTX_OFFSET,
2621 RING_INDIRECT_CTX_OFFSET(base), 0);
604a8f6f 2622 if (wa_ctx->indirect_ctx.size) {
bde13ebd 2623 u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
17ee950d 2624
56e51bf0 2625 regs[CTX_RCS_INDIRECT_CTX + 1] =
097d4f1c
TU
2626 (ggtt_offset + wa_ctx->indirect_ctx.offset) |
2627 (wa_ctx->indirect_ctx.size / CACHELINE_BYTES);
17ee950d 2628
56e51bf0 2629 regs[CTX_RCS_INDIRECT_CTX_OFFSET + 1] =
0bc40be8 2630 intel_lr_indirect_ctx_offset(engine) << 6;
604a8f6f
CW
2631 }
2632
2633 CTX_REG(regs, CTX_BB_PER_CTX_PTR, RING_BB_PER_CTX_PTR(base), 0);
2634 if (wa_ctx->per_ctx.size) {
2635 u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
17ee950d 2636
56e51bf0 2637 regs[CTX_BB_PER_CTX_PTR + 1] =
097d4f1c 2638 (ggtt_offset + wa_ctx->per_ctx.offset) | 0x01;
17ee950d 2639 }
8670d6f9 2640 }
56e51bf0
TU
2641
2642 regs[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9) | MI_LRI_FORCE_POSTED;
2643
2644 CTX_REG(regs, CTX_CTX_TIMESTAMP, RING_CTX_TIMESTAMP(base), 0);
0d925ea0 2645 /* PDP values well be assigned later if needed */
56e51bf0
TU
2646 CTX_REG(regs, CTX_PDP3_UDW, GEN8_RING_PDP_UDW(engine, 3), 0);
2647 CTX_REG(regs, CTX_PDP3_LDW, GEN8_RING_PDP_LDW(engine, 3), 0);
2648 CTX_REG(regs, CTX_PDP2_UDW, GEN8_RING_PDP_UDW(engine, 2), 0);
2649 CTX_REG(regs, CTX_PDP2_LDW, GEN8_RING_PDP_LDW(engine, 2), 0);
2650 CTX_REG(regs, CTX_PDP1_UDW, GEN8_RING_PDP_UDW(engine, 1), 0);
2651 CTX_REG(regs, CTX_PDP1_LDW, GEN8_RING_PDP_LDW(engine, 1), 0);
2652 CTX_REG(regs, CTX_PDP0_UDW, GEN8_RING_PDP_UDW(engine, 0), 0);
2653 CTX_REG(regs, CTX_PDP0_LDW, GEN8_RING_PDP_LDW(engine, 0), 0);
d7b2633d 2654
949e8ab3 2655 if (ppgtt && i915_vm_is_48bit(&ppgtt->base)) {
2dba3239
MT
2656 /* 64b PPGTT (48bit canonical)
2657 * PDP0_DESCRIPTOR contains the base address to PML4 and
2658 * other PDP Descriptors are ignored.
2659 */
56e51bf0 2660 ASSIGN_CTX_PML4(ppgtt, regs);
2dba3239
MT
2661 }
2662
56e51bf0
TU
2663 if (rcs) {
2664 regs[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
2665 CTX_REG(regs, CTX_R_PWR_CLK_STATE, GEN8_R_PWR_CLK_STATE,
2666 make_rpcs(dev_priv));
19f81df2
RB
2667
2668 i915_oa_init_reg_state(engine, ctx, regs);
8670d6f9 2669 }
a3aabe86
CW
2670}
2671
2672static int
2673populate_lr_context(struct i915_gem_context *ctx,
2674 struct drm_i915_gem_object *ctx_obj,
2675 struct intel_engine_cs *engine,
2676 struct intel_ring *ring)
2677{
2678 void *vaddr;
d2b4b979 2679 u32 *regs;
a3aabe86
CW
2680 int ret;
2681
2682 ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
2683 if (ret) {
2684 DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
2685 return ret;
2686 }
2687
2688 vaddr = i915_gem_object_pin_map(ctx_obj, I915_MAP_WB);
2689 if (IS_ERR(vaddr)) {
2690 ret = PTR_ERR(vaddr);
2691 DRM_DEBUG_DRIVER("Could not map object pages! (%d)\n", ret);
2692 return ret;
2693 }
a4f5ea64 2694 ctx_obj->mm.dirty = true;
a3aabe86 2695
d2b4b979
CW
2696 if (engine->default_state) {
2697 /*
2698 * We only want to copy over the template context state;
2699 * skipping over the headers reserved for GuC communication,
2700 * leaving those as zero.
2701 */
2702 const unsigned long start = LRC_HEADER_PAGES * PAGE_SIZE;
2703 void *defaults;
2704
2705 defaults = i915_gem_object_pin_map(engine->default_state,
2706 I915_MAP_WB);
aaefa06a
MA
2707 if (IS_ERR(defaults)) {
2708 ret = PTR_ERR(defaults);
2709 goto err_unpin_ctx;
2710 }
d2b4b979
CW
2711
2712 memcpy(vaddr + start, defaults + start, engine->context_size);
2713 i915_gem_object_unpin_map(engine->default_state);
2714 }
2715
a3aabe86
CW
2716 /* The second page of the context object contains some fields which must
2717 * be set up prior to the first execution. */
d2b4b979
CW
2718 regs = vaddr + LRC_STATE_PN * PAGE_SIZE;
2719 execlists_init_reg_state(regs, ctx, engine, ring);
2720 if (!engine->default_state)
2721 regs[CTX_CONTEXT_CONTROL + 1] |=
2722 _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT);
05f0addd 2723 if (ctx == ctx->i915->preempt_context && INTEL_GEN(engine->i915) < 11)
517aaffe
CW
2724 regs[CTX_CONTEXT_CONTROL + 1] |=
2725 _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
2726 CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT);
8670d6f9 2727
aaefa06a 2728err_unpin_ctx:
7d774cac 2729 i915_gem_object_unpin_map(ctx_obj);
aaefa06a 2730 return ret;
8670d6f9
OM
2731}
2732
e2efd130 2733static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
1fc44d9b
CW
2734 struct intel_engine_cs *engine,
2735 struct intel_context *ce)
ede7d42b 2736{
8c857917 2737 struct drm_i915_gem_object *ctx_obj;
bf3783e5 2738 struct i915_vma *vma;
8c857917 2739 uint32_t context_size;
7e37f889 2740 struct intel_ring *ring;
a89d1f92 2741 struct i915_timeline *timeline;
8c857917
OM
2742 int ret;
2743
1d2a19c2
CW
2744 if (ce->state)
2745 return 0;
ede7d42b 2746
63ffbcda 2747 context_size = round_up(engine->context_size, I915_GTT_PAGE_SIZE);
8c857917 2748
0b29c75a
MT
2749 /*
2750 * Before the actual start of the context image, we insert a few pages
2751 * for our own use and for sharing with the GuC.
2752 */
2753 context_size += LRC_HEADER_PAGES * PAGE_SIZE;
d1675198 2754
12d79d78 2755 ctx_obj = i915_gem_object_create(ctx->i915, context_size);
fe3db79b 2756 if (IS_ERR(ctx_obj)) {
a89d1f92
CW
2757 ret = PTR_ERR(ctx_obj);
2758 goto error_deref_obj;
8c857917
OM
2759 }
2760
a01cb37a 2761 vma = i915_vma_instance(ctx_obj, &ctx->i915->ggtt.base, NULL);
bf3783e5
CW
2762 if (IS_ERR(vma)) {
2763 ret = PTR_ERR(vma);
2764 goto error_deref_obj;
2765 }
2766
a89d1f92
CW
2767 timeline = i915_timeline_create(ctx->i915, ctx->name);
2768 if (IS_ERR(timeline)) {
2769 ret = PTR_ERR(timeline);
2770 goto error_deref_obj;
2771 }
2772
2773 ring = intel_engine_create_ring(engine, timeline, ctx->ring_size);
2774 i915_timeline_put(timeline);
dca33ecc
CW
2775 if (IS_ERR(ring)) {
2776 ret = PTR_ERR(ring);
e84fe803 2777 goto error_deref_obj;
8670d6f9
OM
2778 }
2779
dca33ecc 2780 ret = populate_lr_context(ctx, ctx_obj, engine, ring);
8670d6f9
OM
2781 if (ret) {
2782 DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
dca33ecc 2783 goto error_ring_free;
84c2377f
OM
2784 }
2785
dca33ecc 2786 ce->ring = ring;
bf3783e5 2787 ce->state = vma;
ede7d42b
OM
2788
2789 return 0;
8670d6f9 2790
dca33ecc 2791error_ring_free:
7e37f889 2792 intel_ring_free(ring);
e84fe803 2793error_deref_obj:
f8c417cd 2794 i915_gem_object_put(ctx_obj);
8670d6f9 2795 return ret;
ede7d42b 2796}
3e5b6f05 2797
821ed7df 2798void intel_lr_context_resume(struct drm_i915_private *dev_priv)
3e5b6f05 2799{
e2f80391 2800 struct intel_engine_cs *engine;
bafb2f7d 2801 struct i915_gem_context *ctx;
3b3f1650 2802 enum intel_engine_id id;
bafb2f7d
CW
2803
2804 /* Because we emit WA_TAIL_DWORDS there may be a disparity
2805 * between our bookkeeping in ce->ring->head and ce->ring->tail and
2806 * that stored in context. As we only write new commands from
2807 * ce->ring->tail onwards, everything before that is junk. If the GPU
2808 * starts reading from its RING_HEAD from the context, it may try to
2809 * execute that junk and die.
2810 *
2811 * So to avoid that we reset the context images upon resume. For
2812 * simplicity, we just zero everything out.
2813 */
829a0af2 2814 list_for_each_entry(ctx, &dev_priv->contexts.list, link) {
3b3f1650 2815 for_each_engine(engine, dev_priv, id) {
ab82a063
CW
2816 struct intel_context *ce =
2817 to_intel_context(ctx, engine);
bafb2f7d 2818 u32 *reg;
3e5b6f05 2819
bafb2f7d
CW
2820 if (!ce->state)
2821 continue;
7d774cac 2822
bafb2f7d
CW
2823 reg = i915_gem_object_pin_map(ce->state->obj,
2824 I915_MAP_WB);
2825 if (WARN_ON(IS_ERR(reg)))
2826 continue;
3e5b6f05 2827
bafb2f7d
CW
2828 reg += LRC_STATE_PN * PAGE_SIZE / sizeof(*reg);
2829 reg[CTX_RING_HEAD+1] = 0;
2830 reg[CTX_RING_TAIL+1] = 0;
3e5b6f05 2831
a4f5ea64 2832 ce->state->obj->mm.dirty = true;
bafb2f7d 2833 i915_gem_object_unpin_map(ce->state->obj);
3e5b6f05 2834
e6ba9992 2835 intel_ring_reset(ce->ring, 0);
bafb2f7d 2836 }
3e5b6f05
TD
2837 }
2838}
2c66555e
CW
2839
2840#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
2841#include "selftests/intel_lrc.c"
2842#endif