drm/i915/execlists: Move request unwinding to a separate function
[linux-block.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"
3bbaba0c 139#include "intel_mocs.h"
127f1003 140
e981e7b1
TD
141#define RING_EXECLIST_QFULL (1 << 0x2)
142#define RING_EXECLIST1_VALID (1 << 0x3)
143#define RING_EXECLIST0_VALID (1 << 0x4)
144#define RING_EXECLIST_ACTIVE_STATUS (3 << 0xE)
145#define RING_EXECLIST1_ACTIVE (1 << 0x11)
146#define RING_EXECLIST0_ACTIVE (1 << 0x12)
147
148#define GEN8_CTX_STATUS_IDLE_ACTIVE (1 << 0)
149#define GEN8_CTX_STATUS_PREEMPTED (1 << 1)
150#define GEN8_CTX_STATUS_ELEMENT_SWITCH (1 << 2)
151#define GEN8_CTX_STATUS_ACTIVE_IDLE (1 << 3)
152#define GEN8_CTX_STATUS_COMPLETE (1 << 4)
153#define GEN8_CTX_STATUS_LITE_RESTORE (1 << 15)
8670d6f9 154
70c2a24d
CW
155#define GEN8_CTX_STATUS_COMPLETED_MASK \
156 (GEN8_CTX_STATUS_ACTIVE_IDLE | \
157 GEN8_CTX_STATUS_PREEMPTED | \
158 GEN8_CTX_STATUS_ELEMENT_SWITCH)
159
8670d6f9
OM
160#define CTX_LRI_HEADER_0 0x01
161#define CTX_CONTEXT_CONTROL 0x02
162#define CTX_RING_HEAD 0x04
163#define CTX_RING_TAIL 0x06
164#define CTX_RING_BUFFER_START 0x08
165#define CTX_RING_BUFFER_CONTROL 0x0a
166#define CTX_BB_HEAD_U 0x0c
167#define CTX_BB_HEAD_L 0x0e
168#define CTX_BB_STATE 0x10
169#define CTX_SECOND_BB_HEAD_U 0x12
170#define CTX_SECOND_BB_HEAD_L 0x14
171#define CTX_SECOND_BB_STATE 0x16
172#define CTX_BB_PER_CTX_PTR 0x18
173#define CTX_RCS_INDIRECT_CTX 0x1a
174#define CTX_RCS_INDIRECT_CTX_OFFSET 0x1c
175#define CTX_LRI_HEADER_1 0x21
176#define CTX_CTX_TIMESTAMP 0x22
177#define CTX_PDP3_UDW 0x24
178#define CTX_PDP3_LDW 0x26
179#define CTX_PDP2_UDW 0x28
180#define CTX_PDP2_LDW 0x2a
181#define CTX_PDP1_UDW 0x2c
182#define CTX_PDP1_LDW 0x2e
183#define CTX_PDP0_UDW 0x30
184#define CTX_PDP0_LDW 0x32
185#define CTX_LRI_HEADER_2 0x41
186#define CTX_R_PWR_CLK_STATE 0x42
187#define CTX_GPGPU_CSR_BASE_ADDRESS 0x44
188
56e51bf0 189#define CTX_REG(reg_state, pos, reg, val) do { \
f0f59a00 190 (reg_state)[(pos)+0] = i915_mmio_reg_offset(reg); \
0d925ea0
VS
191 (reg_state)[(pos)+1] = (val); \
192} while (0)
193
194#define ASSIGN_CTX_PDP(ppgtt, reg_state, n) do { \
d852c7bf 195 const u64 _addr = i915_page_dir_dma_addr((ppgtt), (n)); \
e5815a2e
MT
196 reg_state[CTX_PDP ## n ## _UDW+1] = upper_32_bits(_addr); \
197 reg_state[CTX_PDP ## n ## _LDW+1] = lower_32_bits(_addr); \
9244a817 198} while (0)
e5815a2e 199
9244a817 200#define ASSIGN_CTX_PML4(ppgtt, reg_state) do { \
2dba3239
MT
201 reg_state[CTX_PDP0_UDW + 1] = upper_32_bits(px_dma(&ppgtt->pml4)); \
202 reg_state[CTX_PDP0_LDW + 1] = lower_32_bits(px_dma(&ppgtt->pml4)); \
9244a817 203} while (0)
2dba3239 204
71562919
MT
205#define GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x17
206#define GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x26
7bd0a2c6 207#define GEN10_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x19
84b790f8 208
0e93cdd4
CW
209/* Typical size of the average request (2 pipecontrols and a MI_BB) */
210#define EXECLISTS_REQUEST_SIZE 64 /* bytes */
211
a3aabe86 212#define WA_TAIL_DWORDS 2
7e4992ac 213#define WA_TAIL_BYTES (sizeof(u32) * WA_TAIL_DWORDS)
a3aabe86 214
e2efd130 215static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
978f1e09 216 struct intel_engine_cs *engine);
a3aabe86
CW
217static void execlists_init_reg_state(u32 *reg_state,
218 struct i915_gem_context *ctx,
219 struct intel_engine_cs *engine,
220 struct intel_ring *ring);
7ba717cf 221
73e4d07f
OM
222/**
223 * intel_sanitize_enable_execlists() - sanitize i915.enable_execlists
14bb2c11 224 * @dev_priv: i915 device private
73e4d07f
OM
225 * @enable_execlists: value of i915.enable_execlists module parameter.
226 *
227 * Only certain platforms support Execlists (the prerequisites being
27401d12 228 * support for Logical Ring Contexts and Aliasing PPGTT or better).
73e4d07f
OM
229 *
230 * Return: 1 if Execlists is supported and has to be enabled.
231 */
c033666a 232int intel_sanitize_enable_execlists(struct drm_i915_private *dev_priv, int enable_execlists)
127f1003 233{
a0bd6c31
ZL
234 /* On platforms with execlist available, vGPU will only
235 * support execlist mode, no ring buffer mode.
236 */
c033666a 237 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv) && intel_vgpu_active(dev_priv))
a0bd6c31
ZL
238 return 1;
239
c033666a 240 if (INTEL_GEN(dev_priv) >= 9)
70ee45e1
DL
241 return 1;
242
127f1003
OM
243 if (enable_execlists == 0)
244 return 0;
245
5a21b665
DV
246 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv) &&
247 USES_PPGTT(dev_priv) &&
4f044a88 248 i915_modparams.use_mmio_flip >= 0)
127f1003
OM
249 return 1;
250
251 return 0;
252}
ede7d42b 253
73e4d07f 254/**
ca82580c
TU
255 * intel_lr_context_descriptor_update() - calculate & cache the descriptor
256 * descriptor for a pinned context
ca82580c 257 * @ctx: Context to work on
9021ad03 258 * @engine: Engine the descriptor will be used with
73e4d07f 259 *
ca82580c
TU
260 * The context descriptor encodes various attributes of a context,
261 * including its GTT address and some flags. Because it's fairly
262 * expensive to calculate, we'll just do it once and cache the result,
263 * which remains valid until the context is unpinned.
264 *
6e5248b5
DV
265 * This is what a descriptor looks like, from LSB to MSB::
266 *
2355cf08 267 * bits 0-11: flags, GEN8_CTX_* (cached in ctx->desc_template)
6e5248b5
DV
268 * bits 12-31: LRCA, GTT address of (the HWSP of) this context
269 * bits 32-52: ctx ID, a globally unique tag
270 * bits 53-54: mbz, reserved for use by hardware
271 * bits 55-63: group ID, currently unused and set to 0
73e4d07f 272 */
ca82580c 273static void
e2efd130 274intel_lr_context_descriptor_update(struct i915_gem_context *ctx,
0bc40be8 275 struct intel_engine_cs *engine)
84b790f8 276{
9021ad03 277 struct intel_context *ce = &ctx->engine[engine->id];
7069b144 278 u64 desc;
84b790f8 279
7069b144 280 BUILD_BUG_ON(MAX_CONTEXT_HW_ID > (1<<GEN8_CTX_ID_WIDTH));
84b790f8 281
2355cf08 282 desc = ctx->desc_template; /* bits 0-11 */
0b29c75a 283 desc |= i915_ggtt_offset(ce->state) + LRC_HEADER_PAGES * PAGE_SIZE;
9021ad03 284 /* bits 12-31 */
7069b144 285 desc |= (u64)ctx->hw_id << GEN8_CTX_ID_SHIFT; /* bits 32-52 */
5af05fef 286
9021ad03 287 ce->lrc_desc = desc;
5af05fef
MT
288}
289
27606fd8
CW
290static struct i915_priolist *
291lookup_priolist(struct intel_engine_cs *engine,
292 struct i915_priotree *pt,
293 int prio)
08dd3e1a 294{
b620e870 295 struct intel_engine_execlists * const execlists = &engine->execlists;
08dd3e1a
CW
296 struct i915_priolist *p;
297 struct rb_node **parent, *rb;
298 bool first = true;
299
b620e870 300 if (unlikely(execlists->no_priolist))
08dd3e1a
CW
301 prio = I915_PRIORITY_NORMAL;
302
303find_priolist:
304 /* most positive priority is scheduled first, equal priorities fifo */
305 rb = NULL;
b620e870 306 parent = &execlists->queue.rb_node;
08dd3e1a
CW
307 while (*parent) {
308 rb = *parent;
309 p = rb_entry(rb, typeof(*p), node);
310 if (prio > p->priority) {
311 parent = &rb->rb_left;
312 } else if (prio < p->priority) {
313 parent = &rb->rb_right;
314 first = false;
315 } else {
27606fd8 316 return p;
08dd3e1a
CW
317 }
318 }
319
320 if (prio == I915_PRIORITY_NORMAL) {
b620e870 321 p = &execlists->default_priolist;
08dd3e1a
CW
322 } else {
323 p = kmem_cache_alloc(engine->i915->priorities, GFP_ATOMIC);
324 /* Convert an allocation failure to a priority bump */
325 if (unlikely(!p)) {
326 prio = I915_PRIORITY_NORMAL; /* recurses just once */
327
328 /* To maintain ordering with all rendering, after an
329 * allocation failure we have to disable all scheduling.
330 * Requests will then be executed in fifo, and schedule
331 * will ensure that dependencies are emitted in fifo.
332 * There will be still some reordering with existing
333 * requests, so if userspace lied about their
334 * dependencies that reordering may be visible.
335 */
b620e870 336 execlists->no_priolist = true;
08dd3e1a
CW
337 goto find_priolist;
338 }
339 }
340
341 p->priority = prio;
27606fd8 342 INIT_LIST_HEAD(&p->requests);
08dd3e1a 343 rb_link_node(&p->node, rb, parent);
b620e870 344 rb_insert_color(&p->node, &execlists->queue);
08dd3e1a 345
08dd3e1a 346 if (first)
b620e870 347 execlists->first = &p->node;
08dd3e1a 348
27606fd8 349 return ptr_pack_bits(p, first, 1);
08dd3e1a
CW
350}
351
7e4992ac
CW
352static void unwind_wa_tail(struct drm_i915_gem_request *rq)
353{
354 rq->tail = intel_ring_wrap(rq->ring, rq->wa_tail - WA_TAIL_BYTES);
355 assert_ring_tail_valid(rq->ring, rq->tail);
356}
357
358static void unwind_incomplete_requests(struct intel_engine_cs *engine)
359{
360 struct drm_i915_gem_request *rq, *rn;
361
362 lockdep_assert_held(&engine->timeline->lock);
363
364 list_for_each_entry_safe_reverse(rq, rn,
365 &engine->timeline->requests,
366 link) {
367 struct i915_priolist *p;
368
369 if (i915_gem_request_completed(rq))
370 return;
371
372 __i915_gem_request_unsubmit(rq);
373 unwind_wa_tail(rq);
374
375 p = lookup_priolist(engine,
376 &rq->priotree,
377 rq->priotree.priority);
378 list_add(&rq->priotree.link,
379 &ptr_mask_bits(p, 1)->requests);
380 }
381}
382
bbd6c47e
CW
383static inline void
384execlists_context_status_change(struct drm_i915_gem_request *rq,
385 unsigned long status)
84b790f8 386{
bbd6c47e
CW
387 /*
388 * Only used when GVT-g is enabled now. When GVT-g is disabled,
389 * The compiler should eliminate this function as dead-code.
390 */
391 if (!IS_ENABLED(CONFIG_DRM_I915_GVT))
392 return;
6daccb0b 393
3fc03069
CD
394 atomic_notifier_call_chain(&rq->engine->context_status_notifier,
395 status, rq);
84b790f8
BW
396}
397
c6a2ac71
TU
398static void
399execlists_update_context_pdps(struct i915_hw_ppgtt *ppgtt, u32 *reg_state)
400{
401 ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
402 ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
403 ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
404 ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
405}
406
70c2a24d 407static u64 execlists_update_context(struct drm_i915_gem_request *rq)
ae1250b9 408{
70c2a24d 409 struct intel_context *ce = &rq->ctx->engine[rq->engine->id];
04da811b
ZW
410 struct i915_hw_ppgtt *ppgtt =
411 rq->ctx->ppgtt ?: rq->i915->mm.aliasing_ppgtt;
70c2a24d 412 u32 *reg_state = ce->lrc_reg_state;
ae1250b9 413
e6ba9992 414 reg_state[CTX_RING_TAIL+1] = intel_ring_set_tail(rq->ring, rq->tail);
ae1250b9 415
c6a2ac71
TU
416 /* True 32b PPGTT with dynamic page allocation: update PDP
417 * registers and point the unallocated PDPs to scratch page.
418 * PML4 is allocated during ppgtt init, so this is not needed
419 * in 48-bit mode.
420 */
949e8ab3 421 if (ppgtt && !i915_vm_is_48bit(&ppgtt->base))
c6a2ac71 422 execlists_update_context_pdps(ppgtt, reg_state);
70c2a24d
CW
423
424 return ce->lrc_desc;
ae1250b9
OM
425}
426
70c2a24d 427static void execlists_submit_ports(struct intel_engine_cs *engine)
bbd6c47e 428{
b620e870 429 struct execlist_port *port = engine->execlists.port;
bbd6c47e 430 u32 __iomem *elsp =
77f0d0e9
CW
431 engine->i915->regs + i915_mmio_reg_offset(RING_ELSP(engine));
432 unsigned int n;
bbd6c47e 433
76e70087 434 for (n = execlists_num_ports(&engine->execlists); n--; ) {
77f0d0e9
CW
435 struct drm_i915_gem_request *rq;
436 unsigned int count;
437 u64 desc;
438
439 rq = port_unpack(&port[n], &count);
440 if (rq) {
441 GEM_BUG_ON(count > !n);
442 if (!count++)
443 execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_IN);
444 port_set(&port[n], port_pack(rq, count));
445 desc = execlists_update_context(rq);
446 GEM_DEBUG_EXEC(port[n].context_id = upper_32_bits(desc));
447 } else {
448 GEM_BUG_ON(!n);
449 desc = 0;
450 }
bbd6c47e 451
77f0d0e9
CW
452 writel(upper_32_bits(desc), elsp);
453 writel(lower_32_bits(desc), elsp);
454 }
bbd6c47e
CW
455}
456
70c2a24d 457static bool ctx_single_port_submission(const struct i915_gem_context *ctx)
84b790f8 458{
70c2a24d 459 return (IS_ENABLED(CONFIG_DRM_I915_GVT) &&
6095868a 460 i915_gem_context_force_single_submission(ctx));
70c2a24d 461}
84b790f8 462
70c2a24d
CW
463static bool can_merge_ctx(const struct i915_gem_context *prev,
464 const struct i915_gem_context *next)
465{
466 if (prev != next)
467 return false;
26720ab9 468
70c2a24d
CW
469 if (ctx_single_port_submission(prev))
470 return false;
26720ab9 471
70c2a24d 472 return true;
84b790f8
BW
473}
474
77f0d0e9
CW
475static void port_assign(struct execlist_port *port,
476 struct drm_i915_gem_request *rq)
477{
478 GEM_BUG_ON(rq == port_request(port));
479
480 if (port_isset(port))
481 i915_gem_request_put(port_request(port));
482
483 port_set(port, port_pack(i915_gem_request_get(rq), port_count(port)));
484}
485
70c2a24d 486static void execlists_dequeue(struct intel_engine_cs *engine)
acdd884a 487{
20311bd3 488 struct drm_i915_gem_request *last;
7a62cc61
MK
489 struct intel_engine_execlists * const execlists = &engine->execlists;
490 struct execlist_port *port = execlists->port;
76e70087
MK
491 const struct execlist_port * const last_port =
492 &execlists->port[execlists->port_mask];
20311bd3 493 struct rb_node *rb;
70c2a24d
CW
494 bool submit = false;
495
77f0d0e9 496 last = port_request(port);
70c2a24d
CW
497 if (last)
498 /* WaIdleLiteRestore:bdw,skl
499 * Apply the wa NOOPs to prevent ring:HEAD == req:TAIL
9b81d556 500 * as we resubmit the request. See gen8_emit_breadcrumb()
70c2a24d
CW
501 * for where we prepare the padding after the end of the
502 * request.
503 */
504 last->tail = last->wa_tail;
e981e7b1 505
70c2a24d
CW
506 /* Hardware submission is through 2 ports. Conceptually each port
507 * has a (RING_START, RING_HEAD, RING_TAIL) tuple. RING_START is
508 * static for a context, and unique to each, so we only execute
509 * requests belonging to a single context from each ring. RING_HEAD
510 * is maintained by the CS in the context image, it marks the place
511 * where it got up to last time, and through RING_TAIL we tell the CS
512 * where we want to execute up to this time.
513 *
514 * In this list the requests are in order of execution. Consecutive
515 * requests from the same context are adjacent in the ringbuffer. We
516 * can combine these requests into a single RING_TAIL update:
517 *
518 * RING_HEAD...req1...req2
519 * ^- RING_TAIL
520 * since to execute req2 the CS must first execute req1.
521 *
522 * Our goal then is to point each port to the end of a consecutive
523 * sequence of requests as being the most optimal (fewest wake ups
524 * and context switches) submission.
779949f4 525 */
acdd884a 526
9f7886d0 527 spin_lock_irq(&engine->timeline->lock);
7a62cc61
MK
528 rb = execlists->first;
529 GEM_BUG_ON(rb_first(&execlists->queue) != rb);
20311bd3 530 while (rb) {
6c067579
CW
531 struct i915_priolist *p = rb_entry(rb, typeof(*p), node);
532 struct drm_i915_gem_request *rq, *rn;
533
534 list_for_each_entry_safe(rq, rn, &p->requests, priotree.link) {
535 /*
536 * Can we combine this request with the current port?
537 * It has to be the same context/ringbuffer and not
538 * have any exceptions (e.g. GVT saying never to
539 * combine contexts).
540 *
541 * If we can combine the requests, we can execute both
542 * by updating the RING_TAIL to point to the end of the
543 * second request, and so we never need to tell the
544 * hardware about the first.
70c2a24d 545 */
6c067579
CW
546 if (last && !can_merge_ctx(rq->ctx, last->ctx)) {
547 /*
548 * If we are on the second port and cannot
549 * combine this request with the last, then we
550 * are done.
551 */
76e70087 552 if (port == last_port) {
6c067579
CW
553 __list_del_many(&p->requests,
554 &rq->priotree.link);
555 goto done;
556 }
557
558 /*
559 * If GVT overrides us we only ever submit
560 * port[0], leaving port[1] empty. Note that we
561 * also have to be careful that we don't queue
562 * the same context (even though a different
563 * request) to the second port.
564 */
565 if (ctx_single_port_submission(last->ctx) ||
566 ctx_single_port_submission(rq->ctx)) {
567 __list_del_many(&p->requests,
568 &rq->priotree.link);
569 goto done;
570 }
571
572 GEM_BUG_ON(last->ctx == rq->ctx);
573
574 if (submit)
575 port_assign(port, last);
576 port++;
7a62cc61
MK
577
578 GEM_BUG_ON(port_isset(port));
6c067579 579 }
70c2a24d 580
6c067579
CW
581 INIT_LIST_HEAD(&rq->priotree.link);
582 rq->priotree.priority = INT_MAX;
70c2a24d 583
6c067579 584 __i915_gem_request_submit(rq);
7a62cc61 585 trace_i915_gem_request_in(rq, port_index(port, execlists));
6c067579
CW
586 last = rq;
587 submit = true;
70c2a24d 588 }
d55ac5bf 589
20311bd3 590 rb = rb_next(rb);
7a62cc61 591 rb_erase(&p->node, &execlists->queue);
6c067579
CW
592 INIT_LIST_HEAD(&p->requests);
593 if (p->priority != I915_PRIORITY_NORMAL)
c5cf9a91 594 kmem_cache_free(engine->i915->priorities, p);
70c2a24d 595 }
6c067579 596done:
7a62cc61 597 execlists->first = rb;
6c067579 598 if (submit)
77f0d0e9 599 port_assign(port, last);
9f7886d0 600 spin_unlock_irq(&engine->timeline->lock);
53292cdb 601
70c2a24d
CW
602 if (submit)
603 execlists_submit_ports(engine);
acdd884a
MT
604}
605
3f9e6cd8
CW
606static void
607execlist_cancel_port_requests(struct intel_engine_execlists *execlists)
cf4591d1 608{
3f9e6cd8
CW
609 struct execlist_port *port = execlists->port;
610 unsigned int num_ports = ARRAY_SIZE(execlists->port);
cf4591d1 611
3f9e6cd8 612 while (num_ports-- && port_isset(port)) {
7e44fc28
CW
613 struct drm_i915_gem_request *rq = port_request(port);
614
615 execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_OUT);
616 i915_gem_request_put(rq);
617
3f9e6cd8
CW
618 memset(port, 0, sizeof(*port));
619 port++;
620 }
cf4591d1
MK
621}
622
27a5f61b
CW
623static void execlists_cancel_requests(struct intel_engine_cs *engine)
624{
b620e870 625 struct intel_engine_execlists * const execlists = &engine->execlists;
27a5f61b
CW
626 struct drm_i915_gem_request *rq, *rn;
627 struct rb_node *rb;
628 unsigned long flags;
27a5f61b
CW
629
630 spin_lock_irqsave(&engine->timeline->lock, flags);
631
632 /* Cancel the requests on the HW and clear the ELSP tracker. */
cf4591d1 633 execlist_cancel_port_requests(execlists);
27a5f61b
CW
634
635 /* Mark all executing requests as skipped. */
636 list_for_each_entry(rq, &engine->timeline->requests, link) {
637 GEM_BUG_ON(!rq->global_seqno);
638 if (!i915_gem_request_completed(rq))
639 dma_fence_set_error(&rq->fence, -EIO);
640 }
641
642 /* Flush the queued requests to the timeline list (for retiring). */
b620e870 643 rb = execlists->first;
27a5f61b
CW
644 while (rb) {
645 struct i915_priolist *p = rb_entry(rb, typeof(*p), node);
646
647 list_for_each_entry_safe(rq, rn, &p->requests, priotree.link) {
648 INIT_LIST_HEAD(&rq->priotree.link);
649 rq->priotree.priority = INT_MAX;
650
651 dma_fence_set_error(&rq->fence, -EIO);
652 __i915_gem_request_submit(rq);
653 }
654
655 rb = rb_next(rb);
b620e870 656 rb_erase(&p->node, &execlists->queue);
27a5f61b
CW
657 INIT_LIST_HEAD(&p->requests);
658 if (p->priority != I915_PRIORITY_NORMAL)
659 kmem_cache_free(engine->i915->priorities, p);
660 }
661
662 /* Remaining _unready_ requests will be nop'ed when submitted */
663
cf4591d1 664
b620e870
MK
665 execlists->queue = RB_ROOT;
666 execlists->first = NULL;
3f9e6cd8 667 GEM_BUG_ON(port_isset(execlists->port));
27a5f61b
CW
668
669 /*
670 * The port is checked prior to scheduling a tasklet, but
671 * just in case we have suspended the tasklet to do the
672 * wedging make sure that when it wakes, it decides there
673 * is no work to do by clearing the irq_posted bit.
674 */
675 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
676
677 spin_unlock_irqrestore(&engine->timeline->lock, flags);
678}
679
816ee798 680static bool execlists_elsp_ready(const struct intel_engine_cs *engine)
91a41032 681{
b620e870 682 const struct execlist_port *port = engine->execlists.port;
91a41032 683
77f0d0e9 684 return port_count(&port[0]) + port_count(&port[1]) < 2;
91a41032
BW
685}
686
6e5248b5 687/*
73e4d07f
OM
688 * Check the unread Context Status Buffers and manage the submission of new
689 * contexts to the ELSP accordingly.
690 */
27af5eea 691static void intel_lrc_irq_handler(unsigned long data)
e981e7b1 692{
b620e870
MK
693 struct intel_engine_cs * const engine = (struct intel_engine_cs *)data;
694 struct intel_engine_execlists * const execlists = &engine->execlists;
695 struct execlist_port *port = execlists->port;
c033666a 696 struct drm_i915_private *dev_priv = engine->i915;
c6a2ac71 697
48921260
CW
698 /* We can skip acquiring intel_runtime_pm_get() here as it was taken
699 * on our behalf by the request (see i915_gem_mark_busy()) and it will
700 * not be relinquished until the device is idle (see
701 * i915_gem_idle_work_handler()). As a precaution, we make sure
702 * that all ELSP are drained i.e. we have processed the CSB,
703 * before allowing ourselves to idle and calling intel_runtime_pm_put().
704 */
705 GEM_BUG_ON(!dev_priv->gt.awake);
706
b620e870 707 intel_uncore_forcewake_get(dev_priv, execlists->fw_domains);
c6a2ac71 708
899f6204
CW
709 /* Prefer doing test_and_clear_bit() as a two stage operation to avoid
710 * imposing the cost of a locked atomic transaction when submitting a
711 * new request (outside of the context-switch interrupt).
712 */
713 while (test_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted)) {
6d2cb5aa
CW
714 /* The HWSP contains a (cacheable) mirror of the CSB */
715 const u32 *buf =
716 &engine->status_page.page_addr[I915_HWS_CSB_BUF0_INDEX];
4af0d727 717 unsigned int head, tail;
70c2a24d 718
6d2cb5aa 719 /* However GVT emulation depends upon intercepting CSB mmio */
b620e870 720 if (unlikely(execlists->csb_use_mmio)) {
6d2cb5aa
CW
721 buf = (u32 * __force)
722 (dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_BUF_LO(engine, 0)));
b620e870 723 execlists->csb_head = -1; /* force mmio read of CSB ptrs */
6d2cb5aa
CW
724 }
725
2e70b8c6
CW
726 /* The write will be ordered by the uncached read (itself
727 * a memory barrier), so we do not need another in the form
728 * of a locked instruction. The race between the interrupt
729 * handler and the split test/clear is harmless as we order
730 * our clear before the CSB read. If the interrupt arrived
731 * first between the test and the clear, we read the updated
732 * CSB and clear the bit. If the interrupt arrives as we read
733 * the CSB or later (i.e. after we had cleared the bit) the bit
734 * is set and we do a new loop.
735 */
736 __clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
b620e870 737 if (unlikely(execlists->csb_head == -1)) { /* following a reset */
767a983a
CW
738 head = readl(dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)));
739 tail = GEN8_CSB_WRITE_PTR(head);
740 head = GEN8_CSB_READ_PTR(head);
b620e870 741 execlists->csb_head = head;
767a983a
CW
742 } else {
743 const int write_idx =
744 intel_hws_csb_write_index(dev_priv) -
745 I915_HWS_CSB_BUF0_INDEX;
746
b620e870 747 head = execlists->csb_head;
767a983a
CW
748 tail = READ_ONCE(buf[write_idx]);
749 }
b620e870 750
4af0d727 751 while (head != tail) {
77f0d0e9 752 struct drm_i915_gem_request *rq;
4af0d727 753 unsigned int status;
77f0d0e9 754 unsigned int count;
4af0d727
CW
755
756 if (++head == GEN8_CSB_ENTRIES)
757 head = 0;
70c2a24d 758
2ffe80aa
CW
759 /* We are flying near dragons again.
760 *
761 * We hold a reference to the request in execlist_port[]
762 * but no more than that. We are operating in softirq
763 * context and so cannot hold any mutex or sleep. That
764 * prevents us stopping the requests we are processing
765 * in port[] from being retired simultaneously (the
766 * breadcrumb will be complete before we see the
767 * context-switch). As we only hold the reference to the
768 * request, any pointer chasing underneath the request
769 * is subject to a potential use-after-free. Thus we
770 * store all of the bookkeeping within port[] as
771 * required, and avoid using unguarded pointers beneath
772 * request itself. The same applies to the atomic
773 * status notifier.
774 */
775
6d2cb5aa 776 status = READ_ONCE(buf[2 * head]); /* maybe mmio! */
70c2a24d
CW
777 if (!(status & GEN8_CTX_STATUS_COMPLETED_MASK))
778 continue;
779
86aa7e76 780 /* Check the context/desc id for this event matches */
6d2cb5aa 781 GEM_DEBUG_BUG_ON(buf[2 * head + 1] != port->context_id);
86aa7e76 782
77f0d0e9
CW
783 rq = port_unpack(port, &count);
784 GEM_BUG_ON(count == 0);
785 if (--count == 0) {
70c2a24d 786 GEM_BUG_ON(status & GEN8_CTX_STATUS_PREEMPTED);
77f0d0e9
CW
787 GEM_BUG_ON(!i915_gem_request_completed(rq));
788 execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_OUT);
789
790 trace_i915_gem_request_out(rq);
791 i915_gem_request_put(rq);
70c2a24d 792
7a62cc61 793 execlists_port_complete(execlists, port);
77f0d0e9
CW
794 } else {
795 port_set(port, port_pack(rq, count));
70c2a24d 796 }
26720ab9 797
77f0d0e9
CW
798 /* After the final element, the hw should be idle */
799 GEM_BUG_ON(port_count(port) == 0 &&
70c2a24d 800 !(status & GEN8_CTX_STATUS_ACTIVE_IDLE));
4af0d727 801 }
e1fee72c 802
b620e870
MK
803 if (head != execlists->csb_head) {
804 execlists->csb_head = head;
767a983a
CW
805 writel(_MASKED_FIELD(GEN8_CSB_READ_PTR_MASK, head << 8),
806 dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)));
807 }
e981e7b1
TD
808 }
809
70c2a24d
CW
810 if (execlists_elsp_ready(engine))
811 execlists_dequeue(engine);
c6a2ac71 812
b620e870 813 intel_uncore_forcewake_put(dev_priv, execlists->fw_domains);
e981e7b1
TD
814}
815
27606fd8
CW
816static void insert_request(struct intel_engine_cs *engine,
817 struct i915_priotree *pt,
818 int prio)
819{
820 struct i915_priolist *p = lookup_priolist(engine, pt, prio);
821
822 list_add_tail(&pt->link, &ptr_mask_bits(p, 1)->requests);
823 if (ptr_unmask_bits(p, 1) && execlists_elsp_ready(engine))
b620e870 824 tasklet_hi_schedule(&engine->execlists.irq_tasklet);
27606fd8
CW
825}
826
f4ea6bdd 827static void execlists_submit_request(struct drm_i915_gem_request *request)
acdd884a 828{
4a570db5 829 struct intel_engine_cs *engine = request->engine;
5590af3e 830 unsigned long flags;
acdd884a 831
663f71e7
CW
832 /* Will be called from irq-context when using foreign fences. */
833 spin_lock_irqsave(&engine->timeline->lock, flags);
acdd884a 834
27606fd8 835 insert_request(engine, &request->priotree, request->priotree.priority);
acdd884a 836
b620e870 837 GEM_BUG_ON(!engine->execlists.first);
6c067579
CW
838 GEM_BUG_ON(list_empty(&request->priotree.link));
839
663f71e7 840 spin_unlock_irqrestore(&engine->timeline->lock, flags);
acdd884a
MT
841}
842
20311bd3
CW
843static struct intel_engine_cs *
844pt_lock_engine(struct i915_priotree *pt, struct intel_engine_cs *locked)
845{
a79a524e
CW
846 struct intel_engine_cs *engine =
847 container_of(pt, struct drm_i915_gem_request, priotree)->engine;
848
849 GEM_BUG_ON(!locked);
20311bd3 850
20311bd3 851 if (engine != locked) {
a79a524e
CW
852 spin_unlock(&locked->timeline->lock);
853 spin_lock(&engine->timeline->lock);
20311bd3
CW
854 }
855
856 return engine;
857}
858
859static void execlists_schedule(struct drm_i915_gem_request *request, int prio)
860{
a79a524e 861 struct intel_engine_cs *engine;
20311bd3
CW
862 struct i915_dependency *dep, *p;
863 struct i915_dependency stack;
864 LIST_HEAD(dfs);
865
866 if (prio <= READ_ONCE(request->priotree.priority))
867 return;
868
70cd1476
CW
869 /* Need BKL in order to use the temporary link inside i915_dependency */
870 lockdep_assert_held(&request->i915->drm.struct_mutex);
20311bd3
CW
871
872 stack.signaler = &request->priotree;
873 list_add(&stack.dfs_link, &dfs);
874
875 /* Recursively bump all dependent priorities to match the new request.
876 *
877 * A naive approach would be to use recursion:
878 * static void update_priorities(struct i915_priotree *pt, prio) {
879 * list_for_each_entry(dep, &pt->signalers_list, signal_link)
880 * update_priorities(dep->signal, prio)
881 * insert_request(pt);
882 * }
883 * but that may have unlimited recursion depth and so runs a very
884 * real risk of overunning the kernel stack. Instead, we build
885 * a flat list of all dependencies starting with the current request.
886 * As we walk the list of dependencies, we add all of its dependencies
887 * to the end of the list (this may include an already visited
888 * request) and continue to walk onwards onto the new dependencies. The
889 * end result is a topological list of requests in reverse order, the
890 * last element in the list is the request we must execute first.
891 */
892 list_for_each_entry_safe(dep, p, &dfs, dfs_link) {
893 struct i915_priotree *pt = dep->signaler;
894
a79a524e
CW
895 /* Within an engine, there can be no cycle, but we may
896 * refer to the same dependency chain multiple times
897 * (redundant dependencies are not eliminated) and across
898 * engines.
899 */
900 list_for_each_entry(p, &pt->signalers_list, signal_link) {
901 GEM_BUG_ON(p->signaler->priority < pt->priority);
20311bd3
CW
902 if (prio > READ_ONCE(p->signaler->priority))
903 list_move_tail(&p->dfs_link, &dfs);
a79a524e 904 }
20311bd3 905
0798cff4 906 list_safe_reset_next(dep, p, dfs_link);
20311bd3
CW
907 }
908
349bdb68
CW
909 /* If we didn't need to bump any existing priorities, and we haven't
910 * yet submitted this request (i.e. there is no potential race with
911 * execlists_submit_request()), we can set our own priority and skip
912 * acquiring the engine locks.
913 */
914 if (request->priotree.priority == INT_MIN) {
915 GEM_BUG_ON(!list_empty(&request->priotree.link));
916 request->priotree.priority = prio;
917 if (stack.dfs_link.next == stack.dfs_link.prev)
918 return;
919 __list_del_entry(&stack.dfs_link);
920 }
921
a79a524e
CW
922 engine = request->engine;
923 spin_lock_irq(&engine->timeline->lock);
924
20311bd3
CW
925 /* Fifo and depth-first replacement ensure our deps execute before us */
926 list_for_each_entry_safe_reverse(dep, p, &dfs, dfs_link) {
927 struct i915_priotree *pt = dep->signaler;
928
929 INIT_LIST_HEAD(&dep->dfs_link);
930
931 engine = pt_lock_engine(pt, engine);
932
933 if (prio <= pt->priority)
934 continue;
935
20311bd3 936 pt->priority = prio;
6c067579
CW
937 if (!list_empty(&pt->link)) {
938 __list_del_entry(&pt->link);
939 insert_request(engine, pt, prio);
a79a524e 940 }
20311bd3
CW
941 }
942
a79a524e 943 spin_unlock_irq(&engine->timeline->lock);
20311bd3
CW
944
945 /* XXX Do we need to preempt to make room for us and our deps? */
946}
947
266a240b
CW
948static struct intel_ring *
949execlists_context_pin(struct intel_engine_cs *engine,
950 struct i915_gem_context *ctx)
dcb4c12a 951{
9021ad03 952 struct intel_context *ce = &ctx->engine[engine->id];
2947e408 953 unsigned int flags;
7d774cac 954 void *vaddr;
ca82580c 955 int ret;
dcb4c12a 956
91c8a326 957 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
ca82580c 958
266a240b
CW
959 if (likely(ce->pin_count++))
960 goto out;
a533b4ba 961 GEM_BUG_ON(!ce->pin_count); /* no overflow please! */
24f1d3cc 962
e8a9c58f
CW
963 if (!ce->state) {
964 ret = execlists_context_deferred_alloc(ctx, engine);
965 if (ret)
966 goto err;
967 }
56f6e0a7 968 GEM_BUG_ON(!ce->state);
e8a9c58f 969
72b72ae4 970 flags = PIN_GLOBAL | PIN_HIGH;
feef2a7c
DCS
971 if (ctx->ggtt_offset_bias)
972 flags |= PIN_OFFSET_BIAS | ctx->ggtt_offset_bias;
2947e408
CW
973
974 ret = i915_vma_pin(ce->state, 0, GEN8_LR_CONTEXT_ALIGN, flags);
e84fe803 975 if (ret)
24f1d3cc 976 goto err;
7ba717cf 977
bf3783e5 978 vaddr = i915_gem_object_pin_map(ce->state->obj, I915_MAP_WB);
7d774cac
TU
979 if (IS_ERR(vaddr)) {
980 ret = PTR_ERR(vaddr);
bf3783e5 981 goto unpin_vma;
82352e90
TU
982 }
983
d822bb18 984 ret = intel_ring_pin(ce->ring, ctx->i915, ctx->ggtt_offset_bias);
e84fe803 985 if (ret)
7d774cac 986 goto unpin_map;
d1675198 987
0bc40be8 988 intel_lr_context_descriptor_update(ctx, engine);
9021ad03 989
a3aabe86
CW
990 ce->lrc_reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
991 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
bde13ebd 992 i915_ggtt_offset(ce->ring->vma);
a3aabe86 993
a4f5ea64 994 ce->state->obj->mm.dirty = true;
e93c28f3 995
9a6feaf0 996 i915_gem_context_get(ctx);
266a240b
CW
997out:
998 return ce->ring;
7ba717cf 999
7d774cac 1000unpin_map:
bf3783e5
CW
1001 i915_gem_object_unpin_map(ce->state->obj);
1002unpin_vma:
1003 __i915_vma_unpin(ce->state);
24f1d3cc 1004err:
9021ad03 1005 ce->pin_count = 0;
266a240b 1006 return ERR_PTR(ret);
e84fe803
NH
1007}
1008
e8a9c58f
CW
1009static void execlists_context_unpin(struct intel_engine_cs *engine,
1010 struct i915_gem_context *ctx)
e84fe803 1011{
9021ad03 1012 struct intel_context *ce = &ctx->engine[engine->id];
e84fe803 1013
91c8a326 1014 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
9021ad03 1015 GEM_BUG_ON(ce->pin_count == 0);
321fe304 1016
9021ad03 1017 if (--ce->pin_count)
24f1d3cc 1018 return;
e84fe803 1019
aad29fbb 1020 intel_ring_unpin(ce->ring);
dcb4c12a 1021
bf3783e5
CW
1022 i915_gem_object_unpin_map(ce->state->obj);
1023 i915_vma_unpin(ce->state);
321fe304 1024
9a6feaf0 1025 i915_gem_context_put(ctx);
dcb4c12a
OM
1026}
1027
f73e7399 1028static int execlists_request_alloc(struct drm_i915_gem_request *request)
ef11c01d
CW
1029{
1030 struct intel_engine_cs *engine = request->engine;
1031 struct intel_context *ce = &request->ctx->engine[engine->id];
73dec95e 1032 u32 *cs;
ef11c01d
CW
1033 int ret;
1034
e8a9c58f
CW
1035 GEM_BUG_ON(!ce->pin_count);
1036
ef11c01d
CW
1037 /* Flush enough space to reduce the likelihood of waiting after
1038 * we start building the request - in which case we will just
1039 * have to repeat work.
1040 */
1041 request->reserved_space += EXECLISTS_REQUEST_SIZE;
1042
73dec95e 1043 cs = intel_ring_begin(request, 0);
85e2fe67
MW
1044 if (IS_ERR(cs))
1045 return PTR_ERR(cs);
ef11c01d
CW
1046
1047 if (!ce->initialised) {
1048 ret = engine->init_context(request);
1049 if (ret)
85e2fe67 1050 return ret;
ef11c01d
CW
1051
1052 ce->initialised = true;
1053 }
1054
1055 /* Note that after this point, we have committed to using
1056 * this request as it is being used to both track the
1057 * state of engine initialisation and liveness of the
1058 * golden renderstate above. Think twice before you try
1059 * to cancel/unwind this request now.
1060 */
1061
1062 request->reserved_space -= EXECLISTS_REQUEST_SIZE;
1063 return 0;
ef11c01d
CW
1064}
1065
9e000847
AS
1066/*
1067 * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
1068 * PIPE_CONTROL instruction. This is required for the flush to happen correctly
1069 * but there is a slight complication as this is applied in WA batch where the
1070 * values are only initialized once so we cannot take register value at the
1071 * beginning and reuse it further; hence we save its value to memory, upload a
1072 * constant value with bit21 set and then we restore it back with the saved value.
1073 * To simplify the WA, a constant value is formed by using the default value
1074 * of this register. This shouldn't be a problem because we are only modifying
1075 * it for a short period and this batch in non-premptible. We can ofcourse
1076 * use additional instructions that read the actual value of the register
1077 * at that time and set our bit of interest but it makes the WA complicated.
1078 *
1079 * This WA is also required for Gen9 so extracting as a function avoids
1080 * code duplication.
1081 */
097d4f1c
TU
1082static u32 *
1083gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine, u32 *batch)
17ee950d 1084{
097d4f1c
TU
1085 *batch++ = MI_STORE_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1086 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1087 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
1088 *batch++ = 0;
1089
1090 *batch++ = MI_LOAD_REGISTER_IMM(1);
1091 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1092 *batch++ = 0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES;
1093
9f235dfa
TU
1094 batch = gen8_emit_pipe_control(batch,
1095 PIPE_CONTROL_CS_STALL |
1096 PIPE_CONTROL_DC_FLUSH_ENABLE,
1097 0);
097d4f1c
TU
1098
1099 *batch++ = MI_LOAD_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1100 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1101 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
1102 *batch++ = 0;
1103
1104 return batch;
17ee950d
AS
1105}
1106
6e5248b5
DV
1107/*
1108 * Typically we only have one indirect_ctx and per_ctx batch buffer which are
1109 * initialized at the beginning and shared across all contexts but this field
1110 * helps us to have multiple batches at different offsets and select them based
1111 * on a criteria. At the moment this batch always start at the beginning of the page
1112 * and at this point we don't have multiple wa_ctx batch buffers.
4d78c8dc 1113 *
6e5248b5
DV
1114 * The number of WA applied are not known at the beginning; we use this field
1115 * to return the no of DWORDS written.
17ee950d 1116 *
6e5248b5
DV
1117 * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
1118 * so it adds NOOPs as padding to make it cacheline aligned.
1119 * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
1120 * makes a complete batch buffer.
17ee950d 1121 */
097d4f1c 1122static u32 *gen8_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
17ee950d 1123{
7ad00d1a 1124 /* WaDisableCtxRestoreArbitration:bdw,chv */
097d4f1c 1125 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
17ee950d 1126
c82435bb 1127 /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
097d4f1c
TU
1128 if (IS_BROADWELL(engine->i915))
1129 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
c82435bb 1130
0160f055
AS
1131 /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
1132 /* Actual scratch location is at 128 bytes offset */
9f235dfa
TU
1133 batch = gen8_emit_pipe_control(batch,
1134 PIPE_CONTROL_FLUSH_L3 |
1135 PIPE_CONTROL_GLOBAL_GTT_IVB |
1136 PIPE_CONTROL_CS_STALL |
1137 PIPE_CONTROL_QW_WRITE,
1138 i915_ggtt_offset(engine->scratch) +
1139 2 * CACHELINE_BYTES);
0160f055 1140
17ee950d 1141 /* Pad to end of cacheline */
097d4f1c
TU
1142 while ((unsigned long)batch % CACHELINE_BYTES)
1143 *batch++ = MI_NOOP;
17ee950d
AS
1144
1145 /*
1146 * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
1147 * execution depends on the length specified in terms of cache lines
1148 * in the register CTX_RCS_INDIRECT_CTX
1149 */
1150
097d4f1c 1151 return batch;
17ee950d
AS
1152}
1153
6e5248b5
DV
1154/*
1155 * This batch is started immediately after indirect_ctx batch. Since we ensure
1156 * that indirect_ctx ends on a cacheline this batch is aligned automatically.
17ee950d 1157 *
6e5248b5 1158 * The number of DWORDS written are returned using this field.
17ee950d
AS
1159 *
1160 * This batch is terminated with MI_BATCH_BUFFER_END and so we need not add padding
1161 * to align it with cacheline as padding after MI_BATCH_BUFFER_END is redundant.
1162 */
097d4f1c 1163static u32 *gen8_init_perctx_bb(struct intel_engine_cs *engine, u32 *batch)
17ee950d 1164{
7ad00d1a 1165 /* WaDisableCtxRestoreArbitration:bdw,chv */
097d4f1c
TU
1166 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1167 *batch++ = MI_BATCH_BUFFER_END;
17ee950d 1168
097d4f1c 1169 return batch;
17ee950d
AS
1170}
1171
097d4f1c 1172static u32 *gen9_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
0504cffc 1173{
9fb5026f 1174 /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt,glk */
097d4f1c 1175 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
a4106a78 1176
9fb5026f 1177 /* WaDisableGatherAtSetShaderCommonSlice:skl,bxt,kbl,glk */
097d4f1c
TU
1178 *batch++ = MI_LOAD_REGISTER_IMM(1);
1179 *batch++ = i915_mmio_reg_offset(COMMON_SLICE_CHICKEN2);
1180 *batch++ = _MASKED_BIT_DISABLE(
1181 GEN9_DISABLE_GATHER_AT_SET_SHADER_COMMON_SLICE);
1182 *batch++ = MI_NOOP;
873e8171 1183
066d4628
MK
1184 /* WaClearSlmSpaceAtContextSwitch:kbl */
1185 /* Actual scratch location is at 128 bytes offset */
097d4f1c 1186 if (IS_KBL_REVID(engine->i915, 0, KBL_REVID_A0)) {
9f235dfa
TU
1187 batch = gen8_emit_pipe_control(batch,
1188 PIPE_CONTROL_FLUSH_L3 |
1189 PIPE_CONTROL_GLOBAL_GTT_IVB |
1190 PIPE_CONTROL_CS_STALL |
1191 PIPE_CONTROL_QW_WRITE,
1192 i915_ggtt_offset(engine->scratch)
1193 + 2 * CACHELINE_BYTES);
066d4628 1194 }
3485d99e 1195
9fb5026f 1196 /* WaMediaPoolStateCmdInWABB:bxt,glk */
3485d99e
TG
1197 if (HAS_POOLED_EU(engine->i915)) {
1198 /*
1199 * EU pool configuration is setup along with golden context
1200 * during context initialization. This value depends on
1201 * device type (2x6 or 3x6) and needs to be updated based
1202 * on which subslice is disabled especially for 2x6
1203 * devices, however it is safe to load default
1204 * configuration of 3x6 device instead of masking off
1205 * corresponding bits because HW ignores bits of a disabled
1206 * subslice and drops down to appropriate config. Please
1207 * see render_state_setup() in i915_gem_render_state.c for
1208 * possible configurations, to avoid duplication they are
1209 * not shown here again.
1210 */
097d4f1c
TU
1211 *batch++ = GEN9_MEDIA_POOL_STATE;
1212 *batch++ = GEN9_MEDIA_POOL_ENABLE;
1213 *batch++ = 0x00777000;
1214 *batch++ = 0;
1215 *batch++ = 0;
1216 *batch++ = 0;
3485d99e
TG
1217 }
1218
0504cffc 1219 /* Pad to end of cacheline */
097d4f1c
TU
1220 while ((unsigned long)batch % CACHELINE_BYTES)
1221 *batch++ = MI_NOOP;
0504cffc 1222
097d4f1c 1223 return batch;
0504cffc
AS
1224}
1225
097d4f1c
TU
1226#define CTX_WA_BB_OBJ_SIZE (PAGE_SIZE)
1227
1228static int lrc_setup_wa_ctx(struct intel_engine_cs *engine)
17ee950d 1229{
48bb74e4
CW
1230 struct drm_i915_gem_object *obj;
1231 struct i915_vma *vma;
1232 int err;
17ee950d 1233
097d4f1c 1234 obj = i915_gem_object_create(engine->i915, CTX_WA_BB_OBJ_SIZE);
48bb74e4
CW
1235 if (IS_ERR(obj))
1236 return PTR_ERR(obj);
17ee950d 1237
a01cb37a 1238 vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
48bb74e4
CW
1239 if (IS_ERR(vma)) {
1240 err = PTR_ERR(vma);
1241 goto err;
17ee950d
AS
1242 }
1243
48bb74e4
CW
1244 err = i915_vma_pin(vma, 0, PAGE_SIZE, PIN_GLOBAL | PIN_HIGH);
1245 if (err)
1246 goto err;
1247
1248 engine->wa_ctx.vma = vma;
17ee950d 1249 return 0;
48bb74e4
CW
1250
1251err:
1252 i915_gem_object_put(obj);
1253 return err;
17ee950d
AS
1254}
1255
097d4f1c 1256static void lrc_destroy_wa_ctx(struct intel_engine_cs *engine)
17ee950d 1257{
19880c4a 1258 i915_vma_unpin_and_release(&engine->wa_ctx.vma);
17ee950d
AS
1259}
1260
097d4f1c
TU
1261typedef u32 *(*wa_bb_func_t)(struct intel_engine_cs *engine, u32 *batch);
1262
0bc40be8 1263static int intel_init_workaround_bb(struct intel_engine_cs *engine)
17ee950d 1264{
48bb74e4 1265 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
097d4f1c
TU
1266 struct i915_wa_ctx_bb *wa_bb[2] = { &wa_ctx->indirect_ctx,
1267 &wa_ctx->per_ctx };
1268 wa_bb_func_t wa_bb_fn[2];
17ee950d 1269 struct page *page;
097d4f1c
TU
1270 void *batch, *batch_ptr;
1271 unsigned int i;
48bb74e4 1272 int ret;
17ee950d 1273
097d4f1c
TU
1274 if (WARN_ON(engine->id != RCS || !engine->scratch))
1275 return -EINVAL;
17ee950d 1276
097d4f1c 1277 switch (INTEL_GEN(engine->i915)) {
90007bca
RV
1278 case 10:
1279 return 0;
097d4f1c
TU
1280 case 9:
1281 wa_bb_fn[0] = gen9_init_indirectctx_bb;
b8aa2233 1282 wa_bb_fn[1] = NULL;
097d4f1c
TU
1283 break;
1284 case 8:
1285 wa_bb_fn[0] = gen8_init_indirectctx_bb;
1286 wa_bb_fn[1] = gen8_init_perctx_bb;
1287 break;
1288 default:
1289 MISSING_CASE(INTEL_GEN(engine->i915));
5e60d790 1290 return 0;
0504cffc 1291 }
5e60d790 1292
097d4f1c 1293 ret = lrc_setup_wa_ctx(engine);
17ee950d
AS
1294 if (ret) {
1295 DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
1296 return ret;
1297 }
1298
48bb74e4 1299 page = i915_gem_object_get_dirty_page(wa_ctx->vma->obj, 0);
097d4f1c 1300 batch = batch_ptr = kmap_atomic(page);
17ee950d 1301
097d4f1c
TU
1302 /*
1303 * Emit the two workaround batch buffers, recording the offset from the
1304 * start of the workaround batch buffer object for each and their
1305 * respective sizes.
1306 */
1307 for (i = 0; i < ARRAY_SIZE(wa_bb_fn); i++) {
1308 wa_bb[i]->offset = batch_ptr - batch;
1309 if (WARN_ON(!IS_ALIGNED(wa_bb[i]->offset, CACHELINE_BYTES))) {
1310 ret = -EINVAL;
1311 break;
1312 }
604a8f6f
CW
1313 if (wa_bb_fn[i])
1314 batch_ptr = wa_bb_fn[i](engine, batch_ptr);
097d4f1c 1315 wa_bb[i]->size = batch_ptr - (batch + wa_bb[i]->offset);
17ee950d
AS
1316 }
1317
097d4f1c
TU
1318 BUG_ON(batch_ptr - batch > CTX_WA_BB_OBJ_SIZE);
1319
17ee950d
AS
1320 kunmap_atomic(batch);
1321 if (ret)
097d4f1c 1322 lrc_destroy_wa_ctx(engine);
17ee950d
AS
1323
1324 return ret;
1325}
1326
64f09f00
CW
1327static u8 gtiir[] = {
1328 [RCS] = 0,
1329 [BCS] = 0,
1330 [VCS] = 1,
1331 [VCS2] = 1,
1332 [VECS] = 3,
1333};
1334
0bc40be8 1335static int gen8_init_common_ring(struct intel_engine_cs *engine)
9b1136d5 1336{
c033666a 1337 struct drm_i915_private *dev_priv = engine->i915;
b620e870 1338 struct intel_engine_execlists * const execlists = &engine->execlists;
821ed7df
CW
1339 int ret;
1340
1341 ret = intel_mocs_init_engine(engine);
1342 if (ret)
1343 return ret;
9b1136d5 1344
ad07dfcd 1345 intel_engine_reset_breadcrumbs(engine);
f3b8f912 1346 intel_engine_init_hangcheck(engine);
821ed7df 1347
0bc40be8 1348 I915_WRITE(RING_HWSTAM(engine->mmio_base), 0xffffffff);
0bc40be8 1349 I915_WRITE(RING_MODE_GEN7(engine),
9b1136d5 1350 _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
f3b8f912
CW
1351 I915_WRITE(RING_HWS_PGA(engine->mmio_base),
1352 engine->status_page.ggtt_offset);
1353 POSTING_READ(RING_HWS_PGA(engine->mmio_base));
dfc53c5e 1354
0bc40be8 1355 DRM_DEBUG_DRIVER("Execlists enabled for %s\n", engine->name);
9b1136d5 1356
64f09f00
CW
1357 GEM_BUG_ON(engine->id >= ARRAY_SIZE(gtiir));
1358
1359 /*
1360 * Clear any pending interrupt state.
1361 *
1362 * We do it twice out of paranoia that some of the IIR are double
1363 * buffered, and if we only reset it once there may still be
1364 * an interrupt pending.
1365 */
1366 I915_WRITE(GEN8_GT_IIR(gtiir[engine->id]),
1367 GT_CONTEXT_SWITCH_INTERRUPT << engine->irq_shift);
1368 I915_WRITE(GEN8_GT_IIR(gtiir[engine->id]),
1369 GT_CONTEXT_SWITCH_INTERRUPT << engine->irq_shift);
f747026c 1370 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
b620e870 1371 execlists->csb_head = -1;
6b764a59 1372
64f09f00 1373 /* After a GPU reset, we may have requests to replay */
b620e870
MK
1374 if (!i915_modparams.enable_guc_submission && execlists->first)
1375 tasklet_schedule(&execlists->irq_tasklet);
6b764a59 1376
821ed7df 1377 return 0;
9b1136d5
OM
1378}
1379
0bc40be8 1380static int gen8_init_render_ring(struct intel_engine_cs *engine)
9b1136d5 1381{
c033666a 1382 struct drm_i915_private *dev_priv = engine->i915;
9b1136d5
OM
1383 int ret;
1384
0bc40be8 1385 ret = gen8_init_common_ring(engine);
9b1136d5
OM
1386 if (ret)
1387 return ret;
1388
1389 /* We need to disable the AsyncFlip performance optimisations in order
1390 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1391 * programmed to '1' on all products.
1392 *
1393 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
1394 */
1395 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1396
9b1136d5
OM
1397 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1398
0bc40be8 1399 return init_workarounds_ring(engine);
9b1136d5
OM
1400}
1401
0bc40be8 1402static int gen9_init_render_ring(struct intel_engine_cs *engine)
82ef822e
DL
1403{
1404 int ret;
1405
0bc40be8 1406 ret = gen8_init_common_ring(engine);
82ef822e
DL
1407 if (ret)
1408 return ret;
1409
0bc40be8 1410 return init_workarounds_ring(engine);
82ef822e
DL
1411}
1412
821ed7df
CW
1413static void reset_common_ring(struct intel_engine_cs *engine,
1414 struct drm_i915_gem_request *request)
1415{
b620e870 1416 struct intel_engine_execlists * const execlists = &engine->execlists;
c0dcb203 1417 struct intel_context *ce;
221ab971 1418 unsigned long flags;
cdb6ded4 1419
221ab971
CW
1420 spin_lock_irqsave(&engine->timeline->lock, flags);
1421
cdb6ded4
CW
1422 /*
1423 * Catch up with any missed context-switch interrupts.
1424 *
1425 * Ideally we would just read the remaining CSB entries now that we
1426 * know the gpu is idle. However, the CSB registers are sometimes^W
1427 * often trashed across a GPU reset! Instead we have to rely on
1428 * guessing the missed context-switch events by looking at what
1429 * requests were completed.
1430 */
cf4591d1 1431 execlist_cancel_port_requests(execlists);
cdb6ded4 1432
221ab971 1433 /* Push back any incomplete requests for replay after the reset. */
7e4992ac 1434 unwind_incomplete_requests(engine);
cdb6ded4 1435
221ab971 1436 spin_unlock_irqrestore(&engine->timeline->lock, flags);
c0dcb203
CW
1437
1438 /* If the request was innocent, we leave the request in the ELSP
1439 * and will try to replay it on restarting. The context image may
1440 * have been corrupted by the reset, in which case we may have
1441 * to service a new GPU hang, but more likely we can continue on
1442 * without impact.
1443 *
1444 * If the request was guilty, we presume the context is corrupt
1445 * and have to at least restore the RING register in the context
1446 * image back to the expected values to skip over the guilty request.
1447 */
221ab971 1448 if (!request || request->fence.error != -EIO)
c0dcb203 1449 return;
821ed7df 1450
a3aabe86
CW
1451 /* We want a simple context + ring to execute the breadcrumb update.
1452 * We cannot rely on the context being intact across the GPU hang,
1453 * so clear it and rebuild just what we need for the breadcrumb.
1454 * All pending requests for this context will be zapped, and any
1455 * future request will be after userspace has had the opportunity
1456 * to recreate its own state.
1457 */
c0dcb203 1458 ce = &request->ctx->engine[engine->id];
a3aabe86
CW
1459 execlists_init_reg_state(ce->lrc_reg_state,
1460 request->ctx, engine, ce->ring);
1461
821ed7df 1462 /* Move the RING_HEAD onto the breadcrumb, past the hanging batch */
a3aabe86
CW
1463 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
1464 i915_ggtt_offset(ce->ring->vma);
821ed7df 1465 ce->lrc_reg_state[CTX_RING_HEAD+1] = request->postfix;
a3aabe86 1466
821ed7df 1467 request->ring->head = request->postfix;
821ed7df
CW
1468 intel_ring_update_space(request->ring);
1469
a3aabe86 1470 /* Reset WaIdleLiteRestore:bdw,skl as well */
7e4992ac 1471 unwind_wa_tail(request);
821ed7df
CW
1472}
1473
7a01a0a2
MT
1474static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request *req)
1475{
1476 struct i915_hw_ppgtt *ppgtt = req->ctx->ppgtt;
4a570db5 1477 struct intel_engine_cs *engine = req->engine;
e7167769 1478 const int num_lri_cmds = GEN8_3LVL_PDPES * 2;
73dec95e
TU
1479 u32 *cs;
1480 int i;
7a01a0a2 1481
73dec95e
TU
1482 cs = intel_ring_begin(req, num_lri_cmds * 2 + 2);
1483 if (IS_ERR(cs))
1484 return PTR_ERR(cs);
7a01a0a2 1485
73dec95e 1486 *cs++ = MI_LOAD_REGISTER_IMM(num_lri_cmds);
e7167769 1487 for (i = GEN8_3LVL_PDPES - 1; i >= 0; i--) {
7a01a0a2
MT
1488 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
1489
73dec95e
TU
1490 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, i));
1491 *cs++ = upper_32_bits(pd_daddr);
1492 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, i));
1493 *cs++ = lower_32_bits(pd_daddr);
7a01a0a2
MT
1494 }
1495
73dec95e
TU
1496 *cs++ = MI_NOOP;
1497 intel_ring_advance(req, cs);
7a01a0a2
MT
1498
1499 return 0;
1500}
1501
be795fc1 1502static int gen8_emit_bb_start(struct drm_i915_gem_request *req,
803688ba 1503 u64 offset, u32 len,
54af56db 1504 const unsigned int flags)
15648585 1505{
73dec95e 1506 u32 *cs;
15648585
OM
1507 int ret;
1508
7a01a0a2
MT
1509 /* Don't rely in hw updating PDPs, specially in lite-restore.
1510 * Ideally, we should set Force PD Restore in ctx descriptor,
1511 * but we can't. Force Restore would be a second option, but
1512 * it is unsafe in case of lite-restore (because the ctx is
2dba3239
MT
1513 * not idle). PML4 is allocated during ppgtt init so this is
1514 * not needed in 48-bit.*/
7a01a0a2 1515 if (req->ctx->ppgtt &&
54af56db
MK
1516 (intel_engine_flag(req->engine) & req->ctx->ppgtt->pd_dirty_rings) &&
1517 !i915_vm_is_48bit(&req->ctx->ppgtt->base) &&
1518 !intel_vgpu_active(req->i915)) {
1519 ret = intel_logical_ring_emit_pdps(req);
1520 if (ret)
1521 return ret;
7a01a0a2 1522
666796da 1523 req->ctx->ppgtt->pd_dirty_rings &= ~intel_engine_flag(req->engine);
7a01a0a2
MT
1524 }
1525
73dec95e
TU
1526 cs = intel_ring_begin(req, 4);
1527 if (IS_ERR(cs))
1528 return PTR_ERR(cs);
15648585
OM
1529
1530 /* FIXME(BDW): Address space and security selectors. */
54af56db
MK
1531 *cs++ = MI_BATCH_BUFFER_START_GEN8 |
1532 (flags & I915_DISPATCH_SECURE ? 0 : BIT(8)) |
1533 (flags & I915_DISPATCH_RS ? MI_BATCH_RESOURCE_STREAMER : 0);
73dec95e
TU
1534 *cs++ = lower_32_bits(offset);
1535 *cs++ = upper_32_bits(offset);
1536 *cs++ = MI_NOOP;
1537 intel_ring_advance(req, cs);
15648585
OM
1538
1539 return 0;
1540}
1541
31bb59cc 1542static void gen8_logical_ring_enable_irq(struct intel_engine_cs *engine)
73d477f6 1543{
c033666a 1544 struct drm_i915_private *dev_priv = engine->i915;
31bb59cc
CW
1545 I915_WRITE_IMR(engine,
1546 ~(engine->irq_enable_mask | engine->irq_keep_mask));
1547 POSTING_READ_FW(RING_IMR(engine->mmio_base));
73d477f6
OM
1548}
1549
31bb59cc 1550static void gen8_logical_ring_disable_irq(struct intel_engine_cs *engine)
73d477f6 1551{
c033666a 1552 struct drm_i915_private *dev_priv = engine->i915;
31bb59cc 1553 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
73d477f6
OM
1554}
1555
7c9cf4e3 1556static int gen8_emit_flush(struct drm_i915_gem_request *request, u32 mode)
4712274c 1557{
73dec95e 1558 u32 cmd, *cs;
4712274c 1559
73dec95e
TU
1560 cs = intel_ring_begin(request, 4);
1561 if (IS_ERR(cs))
1562 return PTR_ERR(cs);
4712274c
OM
1563
1564 cmd = MI_FLUSH_DW + 1;
1565
f0a1fb10
CW
1566 /* We always require a command barrier so that subsequent
1567 * commands, such as breadcrumb interrupts, are strictly ordered
1568 * wrt the contents of the write cache being flushed to memory
1569 * (and thus being coherent from the CPU).
1570 */
1571 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1572
7c9cf4e3 1573 if (mode & EMIT_INVALIDATE) {
f0a1fb10 1574 cmd |= MI_INVALIDATE_TLB;
1dae2dfb 1575 if (request->engine->id == VCS)
f0a1fb10 1576 cmd |= MI_INVALIDATE_BSD;
4712274c
OM
1577 }
1578
73dec95e
TU
1579 *cs++ = cmd;
1580 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
1581 *cs++ = 0; /* upper addr */
1582 *cs++ = 0; /* value */
1583 intel_ring_advance(request, cs);
4712274c
OM
1584
1585 return 0;
1586}
1587
7deb4d39 1588static int gen8_emit_flush_render(struct drm_i915_gem_request *request,
7c9cf4e3 1589 u32 mode)
4712274c 1590{
b5321f30 1591 struct intel_engine_cs *engine = request->engine;
bde13ebd
CW
1592 u32 scratch_addr =
1593 i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES;
0b2d0934 1594 bool vf_flush_wa = false, dc_flush_wa = false;
73dec95e 1595 u32 *cs, flags = 0;
0b2d0934 1596 int len;
4712274c
OM
1597
1598 flags |= PIPE_CONTROL_CS_STALL;
1599
7c9cf4e3 1600 if (mode & EMIT_FLUSH) {
4712274c
OM
1601 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
1602 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
965fd602 1603 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
40a24488 1604 flags |= PIPE_CONTROL_FLUSH_ENABLE;
4712274c
OM
1605 }
1606
7c9cf4e3 1607 if (mode & EMIT_INVALIDATE) {
4712274c
OM
1608 flags |= PIPE_CONTROL_TLB_INVALIDATE;
1609 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
1610 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
1611 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
1612 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
1613 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
1614 flags |= PIPE_CONTROL_QW_WRITE;
1615 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
4712274c 1616
1a5a9ce7
BW
1617 /*
1618 * On GEN9: before VF_CACHE_INVALIDATE we need to emit a NULL
1619 * pipe control.
1620 */
c033666a 1621 if (IS_GEN9(request->i915))
1a5a9ce7 1622 vf_flush_wa = true;
0b2d0934
MK
1623
1624 /* WaForGAMHang:kbl */
1625 if (IS_KBL_REVID(request->i915, 0, KBL_REVID_B0))
1626 dc_flush_wa = true;
1a5a9ce7 1627 }
9647ff36 1628
0b2d0934
MK
1629 len = 6;
1630
1631 if (vf_flush_wa)
1632 len += 6;
1633
1634 if (dc_flush_wa)
1635 len += 12;
1636
73dec95e
TU
1637 cs = intel_ring_begin(request, len);
1638 if (IS_ERR(cs))
1639 return PTR_ERR(cs);
4712274c 1640
9f235dfa
TU
1641 if (vf_flush_wa)
1642 cs = gen8_emit_pipe_control(cs, 0, 0);
9647ff36 1643
9f235dfa
TU
1644 if (dc_flush_wa)
1645 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_DC_FLUSH_ENABLE,
1646 0);
0b2d0934 1647
9f235dfa 1648 cs = gen8_emit_pipe_control(cs, flags, scratch_addr);
0b2d0934 1649
9f235dfa
TU
1650 if (dc_flush_wa)
1651 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_CS_STALL, 0);
0b2d0934 1652
73dec95e 1653 intel_ring_advance(request, cs);
4712274c
OM
1654
1655 return 0;
1656}
1657
7c17d377
CW
1658/*
1659 * Reserve space for 2 NOOPs at the end of each request to be
1660 * used as a workaround for not being allowed to do lite
1661 * restore with HEAD==TAIL (WaIdleLiteRestore).
1662 */
73dec95e 1663static void gen8_emit_wa_tail(struct drm_i915_gem_request *request, u32 *cs)
4da46e1e 1664{
73dec95e
TU
1665 *cs++ = MI_NOOP;
1666 *cs++ = MI_NOOP;
1667 request->wa_tail = intel_ring_offset(request, cs);
caddfe71 1668}
4da46e1e 1669
73dec95e 1670static void gen8_emit_breadcrumb(struct drm_i915_gem_request *request, u32 *cs)
caddfe71 1671{
7c17d377
CW
1672 /* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */
1673 BUILD_BUG_ON(I915_GEM_HWS_INDEX_ADDR & (1 << 5));
4da46e1e 1674
73dec95e
TU
1675 *cs++ = (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW;
1676 *cs++ = intel_hws_seqno_address(request->engine) | MI_FLUSH_DW_USE_GTT;
1677 *cs++ = 0;
1678 *cs++ = request->global_seqno;
1679 *cs++ = MI_USER_INTERRUPT;
1680 *cs++ = MI_NOOP;
1681 request->tail = intel_ring_offset(request, cs);
ed1501d4 1682 assert_ring_tail_valid(request->ring, request->tail);
caddfe71 1683
73dec95e 1684 gen8_emit_wa_tail(request, cs);
7c17d377 1685}
4da46e1e 1686
98f29e8d
CW
1687static const int gen8_emit_breadcrumb_sz = 6 + WA_TAIL_DWORDS;
1688
caddfe71 1689static void gen8_emit_breadcrumb_render(struct drm_i915_gem_request *request,
73dec95e 1690 u32 *cs)
7c17d377 1691{
ce81a65c
MW
1692 /* We're using qword write, seqno should be aligned to 8 bytes. */
1693 BUILD_BUG_ON(I915_GEM_HWS_INDEX & 1);
1694
7c17d377
CW
1695 /* w/a for post sync ops following a GPGPU operation we
1696 * need a prior CS_STALL, which is emitted by the flush
1697 * following the batch.
1698 */
73dec95e
TU
1699 *cs++ = GFX_OP_PIPE_CONTROL(6);
1700 *cs++ = PIPE_CONTROL_GLOBAL_GTT_IVB | PIPE_CONTROL_CS_STALL |
1701 PIPE_CONTROL_QW_WRITE;
1702 *cs++ = intel_hws_seqno_address(request->engine);
1703 *cs++ = 0;
1704 *cs++ = request->global_seqno;
ce81a65c 1705 /* We're thrashing one dword of HWS. */
73dec95e
TU
1706 *cs++ = 0;
1707 *cs++ = MI_USER_INTERRUPT;
1708 *cs++ = MI_NOOP;
1709 request->tail = intel_ring_offset(request, cs);
ed1501d4 1710 assert_ring_tail_valid(request->ring, request->tail);
caddfe71 1711
73dec95e 1712 gen8_emit_wa_tail(request, cs);
4da46e1e
OM
1713}
1714
98f29e8d
CW
1715static const int gen8_emit_breadcrumb_render_sz = 8 + WA_TAIL_DWORDS;
1716
8753181e 1717static int gen8_init_rcs_context(struct drm_i915_gem_request *req)
e7778be1
TD
1718{
1719 int ret;
1720
4ac9659e 1721 ret = intel_ring_workarounds_emit(req);
e7778be1
TD
1722 if (ret)
1723 return ret;
1724
3bbaba0c
PA
1725 ret = intel_rcs_context_init_mocs(req);
1726 /*
1727 * Failing to program the MOCS is non-fatal.The system will not
1728 * run at peak performance. So generate an error and carry on.
1729 */
1730 if (ret)
1731 DRM_ERROR("MOCS failed to program: expect performance issues.\n");
1732
4e50f082 1733 return i915_gem_render_state_emit(req);
e7778be1
TD
1734}
1735
73e4d07f
OM
1736/**
1737 * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
14bb2c11 1738 * @engine: Engine Command Streamer.
73e4d07f 1739 */
0bc40be8 1740void intel_logical_ring_cleanup(struct intel_engine_cs *engine)
454afebd 1741{
6402c330 1742 struct drm_i915_private *dev_priv;
9832b9da 1743
27af5eea
TU
1744 /*
1745 * Tasklet cannot be active at this point due intel_mark_active/idle
1746 * so this is just for documentation.
1747 */
b620e870
MK
1748 if (WARN_ON(test_bit(TASKLET_STATE_SCHED, &engine->execlists.irq_tasklet.state)))
1749 tasklet_kill(&engine->execlists.irq_tasklet);
27af5eea 1750
c033666a 1751 dev_priv = engine->i915;
6402c330 1752
0bc40be8 1753 if (engine->buffer) {
0bc40be8 1754 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
b0366a54 1755 }
48d82387 1756
0bc40be8
TU
1757 if (engine->cleanup)
1758 engine->cleanup(engine);
48d82387 1759
e8a9c58f 1760 intel_engine_cleanup_common(engine);
17ee950d 1761
097d4f1c 1762 lrc_destroy_wa_ctx(engine);
c033666a 1763 engine->i915 = NULL;
3b3f1650
AG
1764 dev_priv->engine[engine->id] = NULL;
1765 kfree(engine);
454afebd
OM
1766}
1767
ff44ad51 1768static void execlists_set_default_submission(struct intel_engine_cs *engine)
ddd66c51 1769{
ff44ad51 1770 engine->submit_request = execlists_submit_request;
27a5f61b 1771 engine->cancel_requests = execlists_cancel_requests;
ff44ad51 1772 engine->schedule = execlists_schedule;
b620e870 1773 engine->execlists.irq_tasklet.func = intel_lrc_irq_handler;
ddd66c51
CW
1774}
1775
c9cacf93 1776static void
e1382efb 1777logical_ring_default_vfuncs(struct intel_engine_cs *engine)
c9cacf93
TU
1778{
1779 /* Default vfuncs which can be overriden by each engine. */
0bc40be8 1780 engine->init_hw = gen8_init_common_ring;
821ed7df 1781 engine->reset_hw = reset_common_ring;
e8a9c58f
CW
1782
1783 engine->context_pin = execlists_context_pin;
1784 engine->context_unpin = execlists_context_unpin;
1785
f73e7399
CW
1786 engine->request_alloc = execlists_request_alloc;
1787
0bc40be8 1788 engine->emit_flush = gen8_emit_flush;
9b81d556 1789 engine->emit_breadcrumb = gen8_emit_breadcrumb;
98f29e8d 1790 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_sz;
ff44ad51
CW
1791
1792 engine->set_default_submission = execlists_set_default_submission;
ddd66c51 1793
31bb59cc
CW
1794 engine->irq_enable = gen8_logical_ring_enable_irq;
1795 engine->irq_disable = gen8_logical_ring_disable_irq;
0bc40be8 1796 engine->emit_bb_start = gen8_emit_bb_start;
c9cacf93
TU
1797}
1798
d9f3af96 1799static inline void
c2c7f240 1800logical_ring_default_irqs(struct intel_engine_cs *engine)
d9f3af96 1801{
c2c7f240 1802 unsigned shift = engine->irq_shift;
0bc40be8
TU
1803 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << shift;
1804 engine->irq_keep_mask = GT_CONTEXT_SWITCH_INTERRUPT << shift;
d9f3af96
TU
1805}
1806
bb45438f
TU
1807static void
1808logical_ring_setup(struct intel_engine_cs *engine)
1809{
1810 struct drm_i915_private *dev_priv = engine->i915;
1811 enum forcewake_domains fw_domains;
1812
019bf277
TU
1813 intel_engine_setup_common(engine);
1814
bb45438f
TU
1815 /* Intentionally left blank. */
1816 engine->buffer = NULL;
1817
1818 fw_domains = intel_uncore_forcewake_for_reg(dev_priv,
1819 RING_ELSP(engine),
1820 FW_REG_WRITE);
1821
1822 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
1823 RING_CONTEXT_STATUS_PTR(engine),
1824 FW_REG_READ | FW_REG_WRITE);
1825
1826 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
1827 RING_CONTEXT_STATUS_BUF_BASE(engine),
1828 FW_REG_READ);
1829
b620e870 1830 engine->execlists.fw_domains = fw_domains;
bb45438f 1831
b620e870 1832 tasklet_init(&engine->execlists.irq_tasklet,
bb45438f
TU
1833 intel_lrc_irq_handler, (unsigned long)engine);
1834
bb45438f
TU
1835 logical_ring_default_vfuncs(engine);
1836 logical_ring_default_irqs(engine);
bb45438f
TU
1837}
1838
486e93f7 1839static int logical_ring_init(struct intel_engine_cs *engine)
a19d6ff2 1840{
a19d6ff2
TU
1841 int ret;
1842
019bf277 1843 ret = intel_engine_init_common(engine);
a19d6ff2
TU
1844 if (ret)
1845 goto error;
1846
a19d6ff2
TU
1847 return 0;
1848
1849error:
1850 intel_logical_ring_cleanup(engine);
1851 return ret;
1852}
1853
88d2ba2e 1854int logical_render_ring_init(struct intel_engine_cs *engine)
a19d6ff2
TU
1855{
1856 struct drm_i915_private *dev_priv = engine->i915;
1857 int ret;
1858
bb45438f
TU
1859 logical_ring_setup(engine);
1860
a19d6ff2
TU
1861 if (HAS_L3_DPF(dev_priv))
1862 engine->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
1863
1864 /* Override some for render ring. */
1865 if (INTEL_GEN(dev_priv) >= 9)
1866 engine->init_hw = gen9_init_render_ring;
1867 else
1868 engine->init_hw = gen8_init_render_ring;
1869 engine->init_context = gen8_init_rcs_context;
a19d6ff2 1870 engine->emit_flush = gen8_emit_flush_render;
9b81d556 1871 engine->emit_breadcrumb = gen8_emit_breadcrumb_render;
98f29e8d 1872 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_render_sz;
a19d6ff2 1873
f51455d4 1874 ret = intel_engine_create_scratch(engine, PAGE_SIZE);
a19d6ff2
TU
1875 if (ret)
1876 return ret;
1877
1878 ret = intel_init_workaround_bb(engine);
1879 if (ret) {
1880 /*
1881 * We continue even if we fail to initialize WA batch
1882 * because we only expect rare glitches but nothing
1883 * critical to prevent us from using GPU
1884 */
1885 DRM_ERROR("WA batch buffer initialization failed: %d\n",
1886 ret);
1887 }
1888
d038fc7e 1889 return logical_ring_init(engine);
a19d6ff2
TU
1890}
1891
88d2ba2e 1892int logical_xcs_ring_init(struct intel_engine_cs *engine)
bb45438f
TU
1893{
1894 logical_ring_setup(engine);
1895
1896 return logical_ring_init(engine);
454afebd
OM
1897}
1898
0cea6502 1899static u32
c033666a 1900make_rpcs(struct drm_i915_private *dev_priv)
0cea6502
JM
1901{
1902 u32 rpcs = 0;
1903
1904 /*
1905 * No explicit RPCS request is needed to ensure full
1906 * slice/subslice/EU enablement prior to Gen9.
1907 */
c033666a 1908 if (INTEL_GEN(dev_priv) < 9)
0cea6502
JM
1909 return 0;
1910
1911 /*
1912 * Starting in Gen9, render power gating can leave
1913 * slice/subslice/EU in a partially enabled state. We
1914 * must make an explicit request through RPCS for full
1915 * enablement.
1916 */
43b67998 1917 if (INTEL_INFO(dev_priv)->sseu.has_slice_pg) {
0cea6502 1918 rpcs |= GEN8_RPCS_S_CNT_ENABLE;
f08a0c92 1919 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.slice_mask) <<
0cea6502
JM
1920 GEN8_RPCS_S_CNT_SHIFT;
1921 rpcs |= GEN8_RPCS_ENABLE;
1922 }
1923
43b67998 1924 if (INTEL_INFO(dev_priv)->sseu.has_subslice_pg) {
0cea6502 1925 rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
57ec171e 1926 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.subslice_mask) <<
0cea6502
JM
1927 GEN8_RPCS_SS_CNT_SHIFT;
1928 rpcs |= GEN8_RPCS_ENABLE;
1929 }
1930
43b67998
ID
1931 if (INTEL_INFO(dev_priv)->sseu.has_eu_pg) {
1932 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
0cea6502 1933 GEN8_RPCS_EU_MIN_SHIFT;
43b67998 1934 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
0cea6502
JM
1935 GEN8_RPCS_EU_MAX_SHIFT;
1936 rpcs |= GEN8_RPCS_ENABLE;
1937 }
1938
1939 return rpcs;
1940}
1941
0bc40be8 1942static u32 intel_lr_indirect_ctx_offset(struct intel_engine_cs *engine)
71562919
MT
1943{
1944 u32 indirect_ctx_offset;
1945
c033666a 1946 switch (INTEL_GEN(engine->i915)) {
71562919 1947 default:
c033666a 1948 MISSING_CASE(INTEL_GEN(engine->i915));
71562919 1949 /* fall through */
7bd0a2c6
MT
1950 case 10:
1951 indirect_ctx_offset =
1952 GEN10_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
1953 break;
71562919
MT
1954 case 9:
1955 indirect_ctx_offset =
1956 GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
1957 break;
1958 case 8:
1959 indirect_ctx_offset =
1960 GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
1961 break;
1962 }
1963
1964 return indirect_ctx_offset;
1965}
1966
56e51bf0 1967static void execlists_init_reg_state(u32 *regs,
a3aabe86
CW
1968 struct i915_gem_context *ctx,
1969 struct intel_engine_cs *engine,
1970 struct intel_ring *ring)
8670d6f9 1971{
a3aabe86
CW
1972 struct drm_i915_private *dev_priv = engine->i915;
1973 struct i915_hw_ppgtt *ppgtt = ctx->ppgtt ?: dev_priv->mm.aliasing_ppgtt;
56e51bf0
TU
1974 u32 base = engine->mmio_base;
1975 bool rcs = engine->id == RCS;
1976
1977 /* A context is actually a big batch buffer with several
1978 * MI_LOAD_REGISTER_IMM commands followed by (reg, value) pairs. The
1979 * values we are setting here are only for the first context restore:
1980 * on a subsequent save, the GPU will recreate this batchbuffer with new
1981 * values (including all the missing MI_LOAD_REGISTER_IMM commands that
1982 * we are not initializing here).
1983 */
1984 regs[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(rcs ? 14 : 11) |
1985 MI_LRI_FORCE_POSTED;
1986
1987 CTX_REG(regs, CTX_CONTEXT_CONTROL, RING_CONTEXT_CONTROL(engine),
1988 _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
1989 CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
1990 (HAS_RESOURCE_STREAMER(dev_priv) ?
1991 CTX_CTRL_RS_CTX_ENABLE : 0)));
1992 CTX_REG(regs, CTX_RING_HEAD, RING_HEAD(base), 0);
1993 CTX_REG(regs, CTX_RING_TAIL, RING_TAIL(base), 0);
1994 CTX_REG(regs, CTX_RING_BUFFER_START, RING_START(base), 0);
1995 CTX_REG(regs, CTX_RING_BUFFER_CONTROL, RING_CTL(base),
1996 RING_CTL_SIZE(ring->size) | RING_VALID);
1997 CTX_REG(regs, CTX_BB_HEAD_U, RING_BBADDR_UDW(base), 0);
1998 CTX_REG(regs, CTX_BB_HEAD_L, RING_BBADDR(base), 0);
1999 CTX_REG(regs, CTX_BB_STATE, RING_BBSTATE(base), RING_BB_PPGTT);
2000 CTX_REG(regs, CTX_SECOND_BB_HEAD_U, RING_SBBADDR_UDW(base), 0);
2001 CTX_REG(regs, CTX_SECOND_BB_HEAD_L, RING_SBBADDR(base), 0);
2002 CTX_REG(regs, CTX_SECOND_BB_STATE, RING_SBBSTATE(base), 0);
2003 if (rcs) {
604a8f6f
CW
2004 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
2005
56e51bf0
TU
2006 CTX_REG(regs, CTX_RCS_INDIRECT_CTX, RING_INDIRECT_CTX(base), 0);
2007 CTX_REG(regs, CTX_RCS_INDIRECT_CTX_OFFSET,
2008 RING_INDIRECT_CTX_OFFSET(base), 0);
604a8f6f 2009 if (wa_ctx->indirect_ctx.size) {
bde13ebd 2010 u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
17ee950d 2011
56e51bf0 2012 regs[CTX_RCS_INDIRECT_CTX + 1] =
097d4f1c
TU
2013 (ggtt_offset + wa_ctx->indirect_ctx.offset) |
2014 (wa_ctx->indirect_ctx.size / CACHELINE_BYTES);
17ee950d 2015
56e51bf0 2016 regs[CTX_RCS_INDIRECT_CTX_OFFSET + 1] =
0bc40be8 2017 intel_lr_indirect_ctx_offset(engine) << 6;
604a8f6f
CW
2018 }
2019
2020 CTX_REG(regs, CTX_BB_PER_CTX_PTR, RING_BB_PER_CTX_PTR(base), 0);
2021 if (wa_ctx->per_ctx.size) {
2022 u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
17ee950d 2023
56e51bf0 2024 regs[CTX_BB_PER_CTX_PTR + 1] =
097d4f1c 2025 (ggtt_offset + wa_ctx->per_ctx.offset) | 0x01;
17ee950d 2026 }
8670d6f9 2027 }
56e51bf0
TU
2028
2029 regs[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9) | MI_LRI_FORCE_POSTED;
2030
2031 CTX_REG(regs, CTX_CTX_TIMESTAMP, RING_CTX_TIMESTAMP(base), 0);
0d925ea0 2032 /* PDP values well be assigned later if needed */
56e51bf0
TU
2033 CTX_REG(regs, CTX_PDP3_UDW, GEN8_RING_PDP_UDW(engine, 3), 0);
2034 CTX_REG(regs, CTX_PDP3_LDW, GEN8_RING_PDP_LDW(engine, 3), 0);
2035 CTX_REG(regs, CTX_PDP2_UDW, GEN8_RING_PDP_UDW(engine, 2), 0);
2036 CTX_REG(regs, CTX_PDP2_LDW, GEN8_RING_PDP_LDW(engine, 2), 0);
2037 CTX_REG(regs, CTX_PDP1_UDW, GEN8_RING_PDP_UDW(engine, 1), 0);
2038 CTX_REG(regs, CTX_PDP1_LDW, GEN8_RING_PDP_LDW(engine, 1), 0);
2039 CTX_REG(regs, CTX_PDP0_UDW, GEN8_RING_PDP_UDW(engine, 0), 0);
2040 CTX_REG(regs, CTX_PDP0_LDW, GEN8_RING_PDP_LDW(engine, 0), 0);
d7b2633d 2041
949e8ab3 2042 if (ppgtt && i915_vm_is_48bit(&ppgtt->base)) {
2dba3239
MT
2043 /* 64b PPGTT (48bit canonical)
2044 * PDP0_DESCRIPTOR contains the base address to PML4 and
2045 * other PDP Descriptors are ignored.
2046 */
56e51bf0 2047 ASSIGN_CTX_PML4(ppgtt, regs);
2dba3239
MT
2048 }
2049
56e51bf0
TU
2050 if (rcs) {
2051 regs[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
2052 CTX_REG(regs, CTX_R_PWR_CLK_STATE, GEN8_R_PWR_CLK_STATE,
2053 make_rpcs(dev_priv));
19f81df2
RB
2054
2055 i915_oa_init_reg_state(engine, ctx, regs);
8670d6f9 2056 }
a3aabe86
CW
2057}
2058
2059static int
2060populate_lr_context(struct i915_gem_context *ctx,
2061 struct drm_i915_gem_object *ctx_obj,
2062 struct intel_engine_cs *engine,
2063 struct intel_ring *ring)
2064{
2065 void *vaddr;
2066 int ret;
2067
2068 ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
2069 if (ret) {
2070 DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
2071 return ret;
2072 }
2073
2074 vaddr = i915_gem_object_pin_map(ctx_obj, I915_MAP_WB);
2075 if (IS_ERR(vaddr)) {
2076 ret = PTR_ERR(vaddr);
2077 DRM_DEBUG_DRIVER("Could not map object pages! (%d)\n", ret);
2078 return ret;
2079 }
a4f5ea64 2080 ctx_obj->mm.dirty = true;
a3aabe86
CW
2081
2082 /* The second page of the context object contains some fields which must
2083 * be set up prior to the first execution. */
2084
2085 execlists_init_reg_state(vaddr + LRC_STATE_PN * PAGE_SIZE,
2086 ctx, engine, ring);
8670d6f9 2087
7d774cac 2088 i915_gem_object_unpin_map(ctx_obj);
8670d6f9
OM
2089
2090 return 0;
2091}
2092
e2efd130 2093static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
978f1e09 2094 struct intel_engine_cs *engine)
ede7d42b 2095{
8c857917 2096 struct drm_i915_gem_object *ctx_obj;
9021ad03 2097 struct intel_context *ce = &ctx->engine[engine->id];
bf3783e5 2098 struct i915_vma *vma;
8c857917 2099 uint32_t context_size;
7e37f889 2100 struct intel_ring *ring;
8c857917
OM
2101 int ret;
2102
9021ad03 2103 WARN_ON(ce->state);
ede7d42b 2104
63ffbcda 2105 context_size = round_up(engine->context_size, I915_GTT_PAGE_SIZE);
8c857917 2106
0b29c75a
MT
2107 /*
2108 * Before the actual start of the context image, we insert a few pages
2109 * for our own use and for sharing with the GuC.
2110 */
2111 context_size += LRC_HEADER_PAGES * PAGE_SIZE;
d1675198 2112
12d79d78 2113 ctx_obj = i915_gem_object_create(ctx->i915, context_size);
fe3db79b 2114 if (IS_ERR(ctx_obj)) {
3126a660 2115 DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
fe3db79b 2116 return PTR_ERR(ctx_obj);
8c857917
OM
2117 }
2118
a01cb37a 2119 vma = i915_vma_instance(ctx_obj, &ctx->i915->ggtt.base, NULL);
bf3783e5
CW
2120 if (IS_ERR(vma)) {
2121 ret = PTR_ERR(vma);
2122 goto error_deref_obj;
2123 }
2124
7e37f889 2125 ring = intel_engine_create_ring(engine, ctx->ring_size);
dca33ecc
CW
2126 if (IS_ERR(ring)) {
2127 ret = PTR_ERR(ring);
e84fe803 2128 goto error_deref_obj;
8670d6f9
OM
2129 }
2130
dca33ecc 2131 ret = populate_lr_context(ctx, ctx_obj, engine, ring);
8670d6f9
OM
2132 if (ret) {
2133 DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
dca33ecc 2134 goto error_ring_free;
84c2377f
OM
2135 }
2136
dca33ecc 2137 ce->ring = ring;
bf3783e5 2138 ce->state = vma;
0d402a24 2139 ce->initialised |= engine->init_context == NULL;
ede7d42b
OM
2140
2141 return 0;
8670d6f9 2142
dca33ecc 2143error_ring_free:
7e37f889 2144 intel_ring_free(ring);
e84fe803 2145error_deref_obj:
f8c417cd 2146 i915_gem_object_put(ctx_obj);
8670d6f9 2147 return ret;
ede7d42b 2148}
3e5b6f05 2149
821ed7df 2150void intel_lr_context_resume(struct drm_i915_private *dev_priv)
3e5b6f05 2151{
e2f80391 2152 struct intel_engine_cs *engine;
bafb2f7d 2153 struct i915_gem_context *ctx;
3b3f1650 2154 enum intel_engine_id id;
bafb2f7d
CW
2155
2156 /* Because we emit WA_TAIL_DWORDS there may be a disparity
2157 * between our bookkeeping in ce->ring->head and ce->ring->tail and
2158 * that stored in context. As we only write new commands from
2159 * ce->ring->tail onwards, everything before that is junk. If the GPU
2160 * starts reading from its RING_HEAD from the context, it may try to
2161 * execute that junk and die.
2162 *
2163 * So to avoid that we reset the context images upon resume. For
2164 * simplicity, we just zero everything out.
2165 */
829a0af2 2166 list_for_each_entry(ctx, &dev_priv->contexts.list, link) {
3b3f1650 2167 for_each_engine(engine, dev_priv, id) {
bafb2f7d
CW
2168 struct intel_context *ce = &ctx->engine[engine->id];
2169 u32 *reg;
3e5b6f05 2170
bafb2f7d
CW
2171 if (!ce->state)
2172 continue;
7d774cac 2173
bafb2f7d
CW
2174 reg = i915_gem_object_pin_map(ce->state->obj,
2175 I915_MAP_WB);
2176 if (WARN_ON(IS_ERR(reg)))
2177 continue;
3e5b6f05 2178
bafb2f7d
CW
2179 reg += LRC_STATE_PN * PAGE_SIZE / sizeof(*reg);
2180 reg[CTX_RING_HEAD+1] = 0;
2181 reg[CTX_RING_TAIL+1] = 0;
3e5b6f05 2182
a4f5ea64 2183 ce->state->obj->mm.dirty = true;
bafb2f7d 2184 i915_gem_object_unpin_map(ce->state->obj);
3e5b6f05 2185
e6ba9992 2186 intel_ring_reset(ce->ring, 0);
bafb2f7d 2187 }
3e5b6f05
TD
2188 }
2189}