drm/i915: Give the invalid priority a magic name
[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
7d1ea609
CW
866 GEM_BUG_ON(prio == I915_PRIORITY_INVALID);
867
20311bd3
CW
868 if (prio <= READ_ONCE(request->priotree.priority))
869 return;
870
70cd1476
CW
871 /* Need BKL in order to use the temporary link inside i915_dependency */
872 lockdep_assert_held(&request->i915->drm.struct_mutex);
20311bd3
CW
873
874 stack.signaler = &request->priotree;
875 list_add(&stack.dfs_link, &dfs);
876
877 /* Recursively bump all dependent priorities to match the new request.
878 *
879 * A naive approach would be to use recursion:
880 * static void update_priorities(struct i915_priotree *pt, prio) {
881 * list_for_each_entry(dep, &pt->signalers_list, signal_link)
882 * update_priorities(dep->signal, prio)
883 * insert_request(pt);
884 * }
885 * but that may have unlimited recursion depth and so runs a very
886 * real risk of overunning the kernel stack. Instead, we build
887 * a flat list of all dependencies starting with the current request.
888 * As we walk the list of dependencies, we add all of its dependencies
889 * to the end of the list (this may include an already visited
890 * request) and continue to walk onwards onto the new dependencies. The
891 * end result is a topological list of requests in reverse order, the
892 * last element in the list is the request we must execute first.
893 */
894 list_for_each_entry_safe(dep, p, &dfs, dfs_link) {
895 struct i915_priotree *pt = dep->signaler;
896
a79a524e
CW
897 /* Within an engine, there can be no cycle, but we may
898 * refer to the same dependency chain multiple times
899 * (redundant dependencies are not eliminated) and across
900 * engines.
901 */
902 list_for_each_entry(p, &pt->signalers_list, signal_link) {
903 GEM_BUG_ON(p->signaler->priority < pt->priority);
20311bd3
CW
904 if (prio > READ_ONCE(p->signaler->priority))
905 list_move_tail(&p->dfs_link, &dfs);
a79a524e 906 }
20311bd3 907
0798cff4 908 list_safe_reset_next(dep, p, dfs_link);
20311bd3
CW
909 }
910
349bdb68
CW
911 /* If we didn't need to bump any existing priorities, and we haven't
912 * yet submitted this request (i.e. there is no potential race with
913 * execlists_submit_request()), we can set our own priority and skip
914 * acquiring the engine locks.
915 */
7d1ea609 916 if (request->priotree.priority == I915_PRIORITY_INVALID) {
349bdb68
CW
917 GEM_BUG_ON(!list_empty(&request->priotree.link));
918 request->priotree.priority = prio;
919 if (stack.dfs_link.next == stack.dfs_link.prev)
920 return;
921 __list_del_entry(&stack.dfs_link);
922 }
923
a79a524e
CW
924 engine = request->engine;
925 spin_lock_irq(&engine->timeline->lock);
926
20311bd3
CW
927 /* Fifo and depth-first replacement ensure our deps execute before us */
928 list_for_each_entry_safe_reverse(dep, p, &dfs, dfs_link) {
929 struct i915_priotree *pt = dep->signaler;
930
931 INIT_LIST_HEAD(&dep->dfs_link);
932
933 engine = pt_lock_engine(pt, engine);
934
935 if (prio <= pt->priority)
936 continue;
937
20311bd3 938 pt->priority = prio;
6c067579
CW
939 if (!list_empty(&pt->link)) {
940 __list_del_entry(&pt->link);
941 insert_request(engine, pt, prio);
a79a524e 942 }
20311bd3
CW
943 }
944
a79a524e 945 spin_unlock_irq(&engine->timeline->lock);
20311bd3
CW
946
947 /* XXX Do we need to preempt to make room for us and our deps? */
948}
949
266a240b
CW
950static struct intel_ring *
951execlists_context_pin(struct intel_engine_cs *engine,
952 struct i915_gem_context *ctx)
dcb4c12a 953{
9021ad03 954 struct intel_context *ce = &ctx->engine[engine->id];
2947e408 955 unsigned int flags;
7d774cac 956 void *vaddr;
ca82580c 957 int ret;
dcb4c12a 958
91c8a326 959 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
ca82580c 960
266a240b
CW
961 if (likely(ce->pin_count++))
962 goto out;
a533b4ba 963 GEM_BUG_ON(!ce->pin_count); /* no overflow please! */
24f1d3cc 964
e8a9c58f
CW
965 if (!ce->state) {
966 ret = execlists_context_deferred_alloc(ctx, engine);
967 if (ret)
968 goto err;
969 }
56f6e0a7 970 GEM_BUG_ON(!ce->state);
e8a9c58f 971
72b72ae4 972 flags = PIN_GLOBAL | PIN_HIGH;
feef2a7c
DCS
973 if (ctx->ggtt_offset_bias)
974 flags |= PIN_OFFSET_BIAS | ctx->ggtt_offset_bias;
2947e408
CW
975
976 ret = i915_vma_pin(ce->state, 0, GEN8_LR_CONTEXT_ALIGN, flags);
e84fe803 977 if (ret)
24f1d3cc 978 goto err;
7ba717cf 979
bf3783e5 980 vaddr = i915_gem_object_pin_map(ce->state->obj, I915_MAP_WB);
7d774cac
TU
981 if (IS_ERR(vaddr)) {
982 ret = PTR_ERR(vaddr);
bf3783e5 983 goto unpin_vma;
82352e90
TU
984 }
985
d822bb18 986 ret = intel_ring_pin(ce->ring, ctx->i915, ctx->ggtt_offset_bias);
e84fe803 987 if (ret)
7d774cac 988 goto unpin_map;
d1675198 989
0bc40be8 990 intel_lr_context_descriptor_update(ctx, engine);
9021ad03 991
a3aabe86
CW
992 ce->lrc_reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
993 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
bde13ebd 994 i915_ggtt_offset(ce->ring->vma);
a3aabe86 995
a4f5ea64 996 ce->state->obj->mm.dirty = true;
e93c28f3 997
9a6feaf0 998 i915_gem_context_get(ctx);
266a240b
CW
999out:
1000 return ce->ring;
7ba717cf 1001
7d774cac 1002unpin_map:
bf3783e5
CW
1003 i915_gem_object_unpin_map(ce->state->obj);
1004unpin_vma:
1005 __i915_vma_unpin(ce->state);
24f1d3cc 1006err:
9021ad03 1007 ce->pin_count = 0;
266a240b 1008 return ERR_PTR(ret);
e84fe803
NH
1009}
1010
e8a9c58f
CW
1011static void execlists_context_unpin(struct intel_engine_cs *engine,
1012 struct i915_gem_context *ctx)
e84fe803 1013{
9021ad03 1014 struct intel_context *ce = &ctx->engine[engine->id];
e84fe803 1015
91c8a326 1016 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
9021ad03 1017 GEM_BUG_ON(ce->pin_count == 0);
321fe304 1018
9021ad03 1019 if (--ce->pin_count)
24f1d3cc 1020 return;
e84fe803 1021
aad29fbb 1022 intel_ring_unpin(ce->ring);
dcb4c12a 1023
bf3783e5
CW
1024 i915_gem_object_unpin_map(ce->state->obj);
1025 i915_vma_unpin(ce->state);
321fe304 1026
9a6feaf0 1027 i915_gem_context_put(ctx);
dcb4c12a
OM
1028}
1029
f73e7399 1030static int execlists_request_alloc(struct drm_i915_gem_request *request)
ef11c01d
CW
1031{
1032 struct intel_engine_cs *engine = request->engine;
1033 struct intel_context *ce = &request->ctx->engine[engine->id];
73dec95e 1034 u32 *cs;
ef11c01d
CW
1035 int ret;
1036
e8a9c58f
CW
1037 GEM_BUG_ON(!ce->pin_count);
1038
ef11c01d
CW
1039 /* Flush enough space to reduce the likelihood of waiting after
1040 * we start building the request - in which case we will just
1041 * have to repeat work.
1042 */
1043 request->reserved_space += EXECLISTS_REQUEST_SIZE;
1044
73dec95e 1045 cs = intel_ring_begin(request, 0);
85e2fe67
MW
1046 if (IS_ERR(cs))
1047 return PTR_ERR(cs);
ef11c01d
CW
1048
1049 if (!ce->initialised) {
1050 ret = engine->init_context(request);
1051 if (ret)
85e2fe67 1052 return ret;
ef11c01d
CW
1053
1054 ce->initialised = true;
1055 }
1056
1057 /* Note that after this point, we have committed to using
1058 * this request as it is being used to both track the
1059 * state of engine initialisation and liveness of the
1060 * golden renderstate above. Think twice before you try
1061 * to cancel/unwind this request now.
1062 */
1063
1064 request->reserved_space -= EXECLISTS_REQUEST_SIZE;
1065 return 0;
ef11c01d
CW
1066}
1067
9e000847
AS
1068/*
1069 * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
1070 * PIPE_CONTROL instruction. This is required for the flush to happen correctly
1071 * but there is a slight complication as this is applied in WA batch where the
1072 * values are only initialized once so we cannot take register value at the
1073 * beginning and reuse it further; hence we save its value to memory, upload a
1074 * constant value with bit21 set and then we restore it back with the saved value.
1075 * To simplify the WA, a constant value is formed by using the default value
1076 * of this register. This shouldn't be a problem because we are only modifying
1077 * it for a short period and this batch in non-premptible. We can ofcourse
1078 * use additional instructions that read the actual value of the register
1079 * at that time and set our bit of interest but it makes the WA complicated.
1080 *
1081 * This WA is also required for Gen9 so extracting as a function avoids
1082 * code duplication.
1083 */
097d4f1c
TU
1084static u32 *
1085gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine, u32 *batch)
17ee950d 1086{
097d4f1c
TU
1087 *batch++ = MI_STORE_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1088 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1089 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
1090 *batch++ = 0;
1091
1092 *batch++ = MI_LOAD_REGISTER_IMM(1);
1093 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1094 *batch++ = 0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES;
1095
9f235dfa
TU
1096 batch = gen8_emit_pipe_control(batch,
1097 PIPE_CONTROL_CS_STALL |
1098 PIPE_CONTROL_DC_FLUSH_ENABLE,
1099 0);
097d4f1c
TU
1100
1101 *batch++ = MI_LOAD_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1102 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1103 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
1104 *batch++ = 0;
1105
1106 return batch;
17ee950d
AS
1107}
1108
6e5248b5
DV
1109/*
1110 * Typically we only have one indirect_ctx and per_ctx batch buffer which are
1111 * initialized at the beginning and shared across all contexts but this field
1112 * helps us to have multiple batches at different offsets and select them based
1113 * on a criteria. At the moment this batch always start at the beginning of the page
1114 * and at this point we don't have multiple wa_ctx batch buffers.
4d78c8dc 1115 *
6e5248b5
DV
1116 * The number of WA applied are not known at the beginning; we use this field
1117 * to return the no of DWORDS written.
17ee950d 1118 *
6e5248b5
DV
1119 * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
1120 * so it adds NOOPs as padding to make it cacheline aligned.
1121 * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
1122 * makes a complete batch buffer.
17ee950d 1123 */
097d4f1c 1124static u32 *gen8_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
17ee950d 1125{
7ad00d1a 1126 /* WaDisableCtxRestoreArbitration:bdw,chv */
097d4f1c 1127 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
17ee950d 1128
c82435bb 1129 /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
097d4f1c
TU
1130 if (IS_BROADWELL(engine->i915))
1131 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
c82435bb 1132
0160f055
AS
1133 /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
1134 /* Actual scratch location is at 128 bytes offset */
9f235dfa
TU
1135 batch = gen8_emit_pipe_control(batch,
1136 PIPE_CONTROL_FLUSH_L3 |
1137 PIPE_CONTROL_GLOBAL_GTT_IVB |
1138 PIPE_CONTROL_CS_STALL |
1139 PIPE_CONTROL_QW_WRITE,
1140 i915_ggtt_offset(engine->scratch) +
1141 2 * CACHELINE_BYTES);
0160f055 1142
17ee950d 1143 /* Pad to end of cacheline */
097d4f1c
TU
1144 while ((unsigned long)batch % CACHELINE_BYTES)
1145 *batch++ = MI_NOOP;
17ee950d
AS
1146
1147 /*
1148 * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
1149 * execution depends on the length specified in terms of cache lines
1150 * in the register CTX_RCS_INDIRECT_CTX
1151 */
1152
097d4f1c 1153 return batch;
17ee950d
AS
1154}
1155
6e5248b5
DV
1156/*
1157 * This batch is started immediately after indirect_ctx batch. Since we ensure
1158 * that indirect_ctx ends on a cacheline this batch is aligned automatically.
17ee950d 1159 *
6e5248b5 1160 * The number of DWORDS written are returned using this field.
17ee950d
AS
1161 *
1162 * This batch is terminated with MI_BATCH_BUFFER_END and so we need not add padding
1163 * to align it with cacheline as padding after MI_BATCH_BUFFER_END is redundant.
1164 */
097d4f1c 1165static u32 *gen8_init_perctx_bb(struct intel_engine_cs *engine, u32 *batch)
17ee950d 1166{
7ad00d1a 1167 /* WaDisableCtxRestoreArbitration:bdw,chv */
097d4f1c
TU
1168 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1169 *batch++ = MI_BATCH_BUFFER_END;
17ee950d 1170
097d4f1c 1171 return batch;
17ee950d
AS
1172}
1173
097d4f1c 1174static u32 *gen9_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
0504cffc 1175{
9fb5026f 1176 /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt,glk */
097d4f1c 1177 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
a4106a78 1178
9fb5026f 1179 /* WaDisableGatherAtSetShaderCommonSlice:skl,bxt,kbl,glk */
097d4f1c
TU
1180 *batch++ = MI_LOAD_REGISTER_IMM(1);
1181 *batch++ = i915_mmio_reg_offset(COMMON_SLICE_CHICKEN2);
1182 *batch++ = _MASKED_BIT_DISABLE(
1183 GEN9_DISABLE_GATHER_AT_SET_SHADER_COMMON_SLICE);
1184 *batch++ = MI_NOOP;
873e8171 1185
066d4628
MK
1186 /* WaClearSlmSpaceAtContextSwitch:kbl */
1187 /* Actual scratch location is at 128 bytes offset */
097d4f1c 1188 if (IS_KBL_REVID(engine->i915, 0, KBL_REVID_A0)) {
9f235dfa
TU
1189 batch = gen8_emit_pipe_control(batch,
1190 PIPE_CONTROL_FLUSH_L3 |
1191 PIPE_CONTROL_GLOBAL_GTT_IVB |
1192 PIPE_CONTROL_CS_STALL |
1193 PIPE_CONTROL_QW_WRITE,
1194 i915_ggtt_offset(engine->scratch)
1195 + 2 * CACHELINE_BYTES);
066d4628 1196 }
3485d99e 1197
9fb5026f 1198 /* WaMediaPoolStateCmdInWABB:bxt,glk */
3485d99e
TG
1199 if (HAS_POOLED_EU(engine->i915)) {
1200 /*
1201 * EU pool configuration is setup along with golden context
1202 * during context initialization. This value depends on
1203 * device type (2x6 or 3x6) and needs to be updated based
1204 * on which subslice is disabled especially for 2x6
1205 * devices, however it is safe to load default
1206 * configuration of 3x6 device instead of masking off
1207 * corresponding bits because HW ignores bits of a disabled
1208 * subslice and drops down to appropriate config. Please
1209 * see render_state_setup() in i915_gem_render_state.c for
1210 * possible configurations, to avoid duplication they are
1211 * not shown here again.
1212 */
097d4f1c
TU
1213 *batch++ = GEN9_MEDIA_POOL_STATE;
1214 *batch++ = GEN9_MEDIA_POOL_ENABLE;
1215 *batch++ = 0x00777000;
1216 *batch++ = 0;
1217 *batch++ = 0;
1218 *batch++ = 0;
3485d99e
TG
1219 }
1220
0504cffc 1221 /* Pad to end of cacheline */
097d4f1c
TU
1222 while ((unsigned long)batch % CACHELINE_BYTES)
1223 *batch++ = MI_NOOP;
0504cffc 1224
097d4f1c 1225 return batch;
0504cffc
AS
1226}
1227
097d4f1c
TU
1228#define CTX_WA_BB_OBJ_SIZE (PAGE_SIZE)
1229
1230static int lrc_setup_wa_ctx(struct intel_engine_cs *engine)
17ee950d 1231{
48bb74e4
CW
1232 struct drm_i915_gem_object *obj;
1233 struct i915_vma *vma;
1234 int err;
17ee950d 1235
097d4f1c 1236 obj = i915_gem_object_create(engine->i915, CTX_WA_BB_OBJ_SIZE);
48bb74e4
CW
1237 if (IS_ERR(obj))
1238 return PTR_ERR(obj);
17ee950d 1239
a01cb37a 1240 vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
48bb74e4
CW
1241 if (IS_ERR(vma)) {
1242 err = PTR_ERR(vma);
1243 goto err;
17ee950d
AS
1244 }
1245
48bb74e4
CW
1246 err = i915_vma_pin(vma, 0, PAGE_SIZE, PIN_GLOBAL | PIN_HIGH);
1247 if (err)
1248 goto err;
1249
1250 engine->wa_ctx.vma = vma;
17ee950d 1251 return 0;
48bb74e4
CW
1252
1253err:
1254 i915_gem_object_put(obj);
1255 return err;
17ee950d
AS
1256}
1257
097d4f1c 1258static void lrc_destroy_wa_ctx(struct intel_engine_cs *engine)
17ee950d 1259{
19880c4a 1260 i915_vma_unpin_and_release(&engine->wa_ctx.vma);
17ee950d
AS
1261}
1262
097d4f1c
TU
1263typedef u32 *(*wa_bb_func_t)(struct intel_engine_cs *engine, u32 *batch);
1264
0bc40be8 1265static int intel_init_workaround_bb(struct intel_engine_cs *engine)
17ee950d 1266{
48bb74e4 1267 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
097d4f1c
TU
1268 struct i915_wa_ctx_bb *wa_bb[2] = { &wa_ctx->indirect_ctx,
1269 &wa_ctx->per_ctx };
1270 wa_bb_func_t wa_bb_fn[2];
17ee950d 1271 struct page *page;
097d4f1c
TU
1272 void *batch, *batch_ptr;
1273 unsigned int i;
48bb74e4 1274 int ret;
17ee950d 1275
097d4f1c
TU
1276 if (WARN_ON(engine->id != RCS || !engine->scratch))
1277 return -EINVAL;
17ee950d 1278
097d4f1c 1279 switch (INTEL_GEN(engine->i915)) {
90007bca
RV
1280 case 10:
1281 return 0;
097d4f1c
TU
1282 case 9:
1283 wa_bb_fn[0] = gen9_init_indirectctx_bb;
b8aa2233 1284 wa_bb_fn[1] = NULL;
097d4f1c
TU
1285 break;
1286 case 8:
1287 wa_bb_fn[0] = gen8_init_indirectctx_bb;
1288 wa_bb_fn[1] = gen8_init_perctx_bb;
1289 break;
1290 default:
1291 MISSING_CASE(INTEL_GEN(engine->i915));
5e60d790 1292 return 0;
0504cffc 1293 }
5e60d790 1294
097d4f1c 1295 ret = lrc_setup_wa_ctx(engine);
17ee950d
AS
1296 if (ret) {
1297 DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
1298 return ret;
1299 }
1300
48bb74e4 1301 page = i915_gem_object_get_dirty_page(wa_ctx->vma->obj, 0);
097d4f1c 1302 batch = batch_ptr = kmap_atomic(page);
17ee950d 1303
097d4f1c
TU
1304 /*
1305 * Emit the two workaround batch buffers, recording the offset from the
1306 * start of the workaround batch buffer object for each and their
1307 * respective sizes.
1308 */
1309 for (i = 0; i < ARRAY_SIZE(wa_bb_fn); i++) {
1310 wa_bb[i]->offset = batch_ptr - batch;
1311 if (WARN_ON(!IS_ALIGNED(wa_bb[i]->offset, CACHELINE_BYTES))) {
1312 ret = -EINVAL;
1313 break;
1314 }
604a8f6f
CW
1315 if (wa_bb_fn[i])
1316 batch_ptr = wa_bb_fn[i](engine, batch_ptr);
097d4f1c 1317 wa_bb[i]->size = batch_ptr - (batch + wa_bb[i]->offset);
17ee950d
AS
1318 }
1319
097d4f1c
TU
1320 BUG_ON(batch_ptr - batch > CTX_WA_BB_OBJ_SIZE);
1321
17ee950d
AS
1322 kunmap_atomic(batch);
1323 if (ret)
097d4f1c 1324 lrc_destroy_wa_ctx(engine);
17ee950d
AS
1325
1326 return ret;
1327}
1328
64f09f00
CW
1329static u8 gtiir[] = {
1330 [RCS] = 0,
1331 [BCS] = 0,
1332 [VCS] = 1,
1333 [VCS2] = 1,
1334 [VECS] = 3,
1335};
1336
0bc40be8 1337static int gen8_init_common_ring(struct intel_engine_cs *engine)
9b1136d5 1338{
c033666a 1339 struct drm_i915_private *dev_priv = engine->i915;
b620e870 1340 struct intel_engine_execlists * const execlists = &engine->execlists;
821ed7df
CW
1341 int ret;
1342
1343 ret = intel_mocs_init_engine(engine);
1344 if (ret)
1345 return ret;
9b1136d5 1346
ad07dfcd 1347 intel_engine_reset_breadcrumbs(engine);
f3b8f912 1348 intel_engine_init_hangcheck(engine);
821ed7df 1349
0bc40be8 1350 I915_WRITE(RING_HWSTAM(engine->mmio_base), 0xffffffff);
0bc40be8 1351 I915_WRITE(RING_MODE_GEN7(engine),
9b1136d5 1352 _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
f3b8f912
CW
1353 I915_WRITE(RING_HWS_PGA(engine->mmio_base),
1354 engine->status_page.ggtt_offset);
1355 POSTING_READ(RING_HWS_PGA(engine->mmio_base));
dfc53c5e 1356
0bc40be8 1357 DRM_DEBUG_DRIVER("Execlists enabled for %s\n", engine->name);
9b1136d5 1358
64f09f00
CW
1359 GEM_BUG_ON(engine->id >= ARRAY_SIZE(gtiir));
1360
1361 /*
1362 * Clear any pending interrupt state.
1363 *
1364 * We do it twice out of paranoia that some of the IIR are double
1365 * buffered, and if we only reset it once there may still be
1366 * an interrupt pending.
1367 */
1368 I915_WRITE(GEN8_GT_IIR(gtiir[engine->id]),
1369 GT_CONTEXT_SWITCH_INTERRUPT << engine->irq_shift);
1370 I915_WRITE(GEN8_GT_IIR(gtiir[engine->id]),
1371 GT_CONTEXT_SWITCH_INTERRUPT << engine->irq_shift);
f747026c 1372 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
b620e870 1373 execlists->csb_head = -1;
6b764a59 1374
64f09f00 1375 /* After a GPU reset, we may have requests to replay */
b620e870
MK
1376 if (!i915_modparams.enable_guc_submission && execlists->first)
1377 tasklet_schedule(&execlists->irq_tasklet);
6b764a59 1378
821ed7df 1379 return 0;
9b1136d5
OM
1380}
1381
0bc40be8 1382static int gen8_init_render_ring(struct intel_engine_cs *engine)
9b1136d5 1383{
c033666a 1384 struct drm_i915_private *dev_priv = engine->i915;
9b1136d5
OM
1385 int ret;
1386
0bc40be8 1387 ret = gen8_init_common_ring(engine);
9b1136d5
OM
1388 if (ret)
1389 return ret;
1390
1391 /* We need to disable the AsyncFlip performance optimisations in order
1392 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1393 * programmed to '1' on all products.
1394 *
1395 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
1396 */
1397 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1398
9b1136d5
OM
1399 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1400
0bc40be8 1401 return init_workarounds_ring(engine);
9b1136d5
OM
1402}
1403
0bc40be8 1404static int gen9_init_render_ring(struct intel_engine_cs *engine)
82ef822e
DL
1405{
1406 int ret;
1407
0bc40be8 1408 ret = gen8_init_common_ring(engine);
82ef822e
DL
1409 if (ret)
1410 return ret;
1411
0bc40be8 1412 return init_workarounds_ring(engine);
82ef822e
DL
1413}
1414
821ed7df
CW
1415static void reset_common_ring(struct intel_engine_cs *engine,
1416 struct drm_i915_gem_request *request)
1417{
b620e870 1418 struct intel_engine_execlists * const execlists = &engine->execlists;
c0dcb203 1419 struct intel_context *ce;
221ab971 1420 unsigned long flags;
cdb6ded4 1421
221ab971
CW
1422 spin_lock_irqsave(&engine->timeline->lock, flags);
1423
cdb6ded4
CW
1424 /*
1425 * Catch up with any missed context-switch interrupts.
1426 *
1427 * Ideally we would just read the remaining CSB entries now that we
1428 * know the gpu is idle. However, the CSB registers are sometimes^W
1429 * often trashed across a GPU reset! Instead we have to rely on
1430 * guessing the missed context-switch events by looking at what
1431 * requests were completed.
1432 */
cf4591d1 1433 execlist_cancel_port_requests(execlists);
cdb6ded4 1434
221ab971 1435 /* Push back any incomplete requests for replay after the reset. */
7e4992ac 1436 unwind_incomplete_requests(engine);
cdb6ded4 1437
221ab971 1438 spin_unlock_irqrestore(&engine->timeline->lock, flags);
c0dcb203
CW
1439
1440 /* If the request was innocent, we leave the request in the ELSP
1441 * and will try to replay it on restarting. The context image may
1442 * have been corrupted by the reset, in which case we may have
1443 * to service a new GPU hang, but more likely we can continue on
1444 * without impact.
1445 *
1446 * If the request was guilty, we presume the context is corrupt
1447 * and have to at least restore the RING register in the context
1448 * image back to the expected values to skip over the guilty request.
1449 */
221ab971 1450 if (!request || request->fence.error != -EIO)
c0dcb203 1451 return;
821ed7df 1452
a3aabe86
CW
1453 /* We want a simple context + ring to execute the breadcrumb update.
1454 * We cannot rely on the context being intact across the GPU hang,
1455 * so clear it and rebuild just what we need for the breadcrumb.
1456 * All pending requests for this context will be zapped, and any
1457 * future request will be after userspace has had the opportunity
1458 * to recreate its own state.
1459 */
c0dcb203 1460 ce = &request->ctx->engine[engine->id];
a3aabe86
CW
1461 execlists_init_reg_state(ce->lrc_reg_state,
1462 request->ctx, engine, ce->ring);
1463
821ed7df 1464 /* Move the RING_HEAD onto the breadcrumb, past the hanging batch */
a3aabe86
CW
1465 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
1466 i915_ggtt_offset(ce->ring->vma);
821ed7df 1467 ce->lrc_reg_state[CTX_RING_HEAD+1] = request->postfix;
a3aabe86 1468
821ed7df 1469 request->ring->head = request->postfix;
821ed7df
CW
1470 intel_ring_update_space(request->ring);
1471
a3aabe86 1472 /* Reset WaIdleLiteRestore:bdw,skl as well */
7e4992ac 1473 unwind_wa_tail(request);
821ed7df
CW
1474}
1475
7a01a0a2
MT
1476static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request *req)
1477{
1478 struct i915_hw_ppgtt *ppgtt = req->ctx->ppgtt;
4a570db5 1479 struct intel_engine_cs *engine = req->engine;
e7167769 1480 const int num_lri_cmds = GEN8_3LVL_PDPES * 2;
73dec95e
TU
1481 u32 *cs;
1482 int i;
7a01a0a2 1483
73dec95e
TU
1484 cs = intel_ring_begin(req, num_lri_cmds * 2 + 2);
1485 if (IS_ERR(cs))
1486 return PTR_ERR(cs);
7a01a0a2 1487
73dec95e 1488 *cs++ = MI_LOAD_REGISTER_IMM(num_lri_cmds);
e7167769 1489 for (i = GEN8_3LVL_PDPES - 1; i >= 0; i--) {
7a01a0a2
MT
1490 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
1491
73dec95e
TU
1492 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, i));
1493 *cs++ = upper_32_bits(pd_daddr);
1494 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, i));
1495 *cs++ = lower_32_bits(pd_daddr);
7a01a0a2
MT
1496 }
1497
73dec95e
TU
1498 *cs++ = MI_NOOP;
1499 intel_ring_advance(req, cs);
7a01a0a2
MT
1500
1501 return 0;
1502}
1503
be795fc1 1504static int gen8_emit_bb_start(struct drm_i915_gem_request *req,
803688ba 1505 u64 offset, u32 len,
54af56db 1506 const unsigned int flags)
15648585 1507{
73dec95e 1508 u32 *cs;
15648585
OM
1509 int ret;
1510
7a01a0a2
MT
1511 /* Don't rely in hw updating PDPs, specially in lite-restore.
1512 * Ideally, we should set Force PD Restore in ctx descriptor,
1513 * but we can't. Force Restore would be a second option, but
1514 * it is unsafe in case of lite-restore (because the ctx is
2dba3239
MT
1515 * not idle). PML4 is allocated during ppgtt init so this is
1516 * not needed in 48-bit.*/
7a01a0a2 1517 if (req->ctx->ppgtt &&
54af56db
MK
1518 (intel_engine_flag(req->engine) & req->ctx->ppgtt->pd_dirty_rings) &&
1519 !i915_vm_is_48bit(&req->ctx->ppgtt->base) &&
1520 !intel_vgpu_active(req->i915)) {
1521 ret = intel_logical_ring_emit_pdps(req);
1522 if (ret)
1523 return ret;
7a01a0a2 1524
666796da 1525 req->ctx->ppgtt->pd_dirty_rings &= ~intel_engine_flag(req->engine);
7a01a0a2
MT
1526 }
1527
73dec95e
TU
1528 cs = intel_ring_begin(req, 4);
1529 if (IS_ERR(cs))
1530 return PTR_ERR(cs);
15648585
OM
1531
1532 /* FIXME(BDW): Address space and security selectors. */
54af56db
MK
1533 *cs++ = MI_BATCH_BUFFER_START_GEN8 |
1534 (flags & I915_DISPATCH_SECURE ? 0 : BIT(8)) |
1535 (flags & I915_DISPATCH_RS ? MI_BATCH_RESOURCE_STREAMER : 0);
73dec95e
TU
1536 *cs++ = lower_32_bits(offset);
1537 *cs++ = upper_32_bits(offset);
1538 *cs++ = MI_NOOP;
1539 intel_ring_advance(req, cs);
15648585
OM
1540
1541 return 0;
1542}
1543
31bb59cc 1544static void gen8_logical_ring_enable_irq(struct intel_engine_cs *engine)
73d477f6 1545{
c033666a 1546 struct drm_i915_private *dev_priv = engine->i915;
31bb59cc
CW
1547 I915_WRITE_IMR(engine,
1548 ~(engine->irq_enable_mask | engine->irq_keep_mask));
1549 POSTING_READ_FW(RING_IMR(engine->mmio_base));
73d477f6
OM
1550}
1551
31bb59cc 1552static void gen8_logical_ring_disable_irq(struct intel_engine_cs *engine)
73d477f6 1553{
c033666a 1554 struct drm_i915_private *dev_priv = engine->i915;
31bb59cc 1555 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
73d477f6
OM
1556}
1557
7c9cf4e3 1558static int gen8_emit_flush(struct drm_i915_gem_request *request, u32 mode)
4712274c 1559{
73dec95e 1560 u32 cmd, *cs;
4712274c 1561
73dec95e
TU
1562 cs = intel_ring_begin(request, 4);
1563 if (IS_ERR(cs))
1564 return PTR_ERR(cs);
4712274c
OM
1565
1566 cmd = MI_FLUSH_DW + 1;
1567
f0a1fb10
CW
1568 /* We always require a command barrier so that subsequent
1569 * commands, such as breadcrumb interrupts, are strictly ordered
1570 * wrt the contents of the write cache being flushed to memory
1571 * (and thus being coherent from the CPU).
1572 */
1573 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1574
7c9cf4e3 1575 if (mode & EMIT_INVALIDATE) {
f0a1fb10 1576 cmd |= MI_INVALIDATE_TLB;
1dae2dfb 1577 if (request->engine->id == VCS)
f0a1fb10 1578 cmd |= MI_INVALIDATE_BSD;
4712274c
OM
1579 }
1580
73dec95e
TU
1581 *cs++ = cmd;
1582 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
1583 *cs++ = 0; /* upper addr */
1584 *cs++ = 0; /* value */
1585 intel_ring_advance(request, cs);
4712274c
OM
1586
1587 return 0;
1588}
1589
7deb4d39 1590static int gen8_emit_flush_render(struct drm_i915_gem_request *request,
7c9cf4e3 1591 u32 mode)
4712274c 1592{
b5321f30 1593 struct intel_engine_cs *engine = request->engine;
bde13ebd
CW
1594 u32 scratch_addr =
1595 i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES;
0b2d0934 1596 bool vf_flush_wa = false, dc_flush_wa = false;
73dec95e 1597 u32 *cs, flags = 0;
0b2d0934 1598 int len;
4712274c
OM
1599
1600 flags |= PIPE_CONTROL_CS_STALL;
1601
7c9cf4e3 1602 if (mode & EMIT_FLUSH) {
4712274c
OM
1603 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
1604 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
965fd602 1605 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
40a24488 1606 flags |= PIPE_CONTROL_FLUSH_ENABLE;
4712274c
OM
1607 }
1608
7c9cf4e3 1609 if (mode & EMIT_INVALIDATE) {
4712274c
OM
1610 flags |= PIPE_CONTROL_TLB_INVALIDATE;
1611 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
1612 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
1613 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
1614 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
1615 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
1616 flags |= PIPE_CONTROL_QW_WRITE;
1617 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
4712274c 1618
1a5a9ce7
BW
1619 /*
1620 * On GEN9: before VF_CACHE_INVALIDATE we need to emit a NULL
1621 * pipe control.
1622 */
c033666a 1623 if (IS_GEN9(request->i915))
1a5a9ce7 1624 vf_flush_wa = true;
0b2d0934
MK
1625
1626 /* WaForGAMHang:kbl */
1627 if (IS_KBL_REVID(request->i915, 0, KBL_REVID_B0))
1628 dc_flush_wa = true;
1a5a9ce7 1629 }
9647ff36 1630
0b2d0934
MK
1631 len = 6;
1632
1633 if (vf_flush_wa)
1634 len += 6;
1635
1636 if (dc_flush_wa)
1637 len += 12;
1638
73dec95e
TU
1639 cs = intel_ring_begin(request, len);
1640 if (IS_ERR(cs))
1641 return PTR_ERR(cs);
4712274c 1642
9f235dfa
TU
1643 if (vf_flush_wa)
1644 cs = gen8_emit_pipe_control(cs, 0, 0);
9647ff36 1645
9f235dfa
TU
1646 if (dc_flush_wa)
1647 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_DC_FLUSH_ENABLE,
1648 0);
0b2d0934 1649
9f235dfa 1650 cs = gen8_emit_pipe_control(cs, flags, scratch_addr);
0b2d0934 1651
9f235dfa
TU
1652 if (dc_flush_wa)
1653 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_CS_STALL, 0);
0b2d0934 1654
73dec95e 1655 intel_ring_advance(request, cs);
4712274c
OM
1656
1657 return 0;
1658}
1659
7c17d377
CW
1660/*
1661 * Reserve space for 2 NOOPs at the end of each request to be
1662 * used as a workaround for not being allowed to do lite
1663 * restore with HEAD==TAIL (WaIdleLiteRestore).
1664 */
73dec95e 1665static void gen8_emit_wa_tail(struct drm_i915_gem_request *request, u32 *cs)
4da46e1e 1666{
73dec95e
TU
1667 *cs++ = MI_NOOP;
1668 *cs++ = MI_NOOP;
1669 request->wa_tail = intel_ring_offset(request, cs);
caddfe71 1670}
4da46e1e 1671
73dec95e 1672static void gen8_emit_breadcrumb(struct drm_i915_gem_request *request, u32 *cs)
caddfe71 1673{
7c17d377
CW
1674 /* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */
1675 BUILD_BUG_ON(I915_GEM_HWS_INDEX_ADDR & (1 << 5));
4da46e1e 1676
73dec95e
TU
1677 *cs++ = (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW;
1678 *cs++ = intel_hws_seqno_address(request->engine) | MI_FLUSH_DW_USE_GTT;
1679 *cs++ = 0;
1680 *cs++ = request->global_seqno;
1681 *cs++ = MI_USER_INTERRUPT;
1682 *cs++ = MI_NOOP;
1683 request->tail = intel_ring_offset(request, cs);
ed1501d4 1684 assert_ring_tail_valid(request->ring, request->tail);
caddfe71 1685
73dec95e 1686 gen8_emit_wa_tail(request, cs);
7c17d377 1687}
4da46e1e 1688
98f29e8d
CW
1689static const int gen8_emit_breadcrumb_sz = 6 + WA_TAIL_DWORDS;
1690
caddfe71 1691static void gen8_emit_breadcrumb_render(struct drm_i915_gem_request *request,
73dec95e 1692 u32 *cs)
7c17d377 1693{
ce81a65c
MW
1694 /* We're using qword write, seqno should be aligned to 8 bytes. */
1695 BUILD_BUG_ON(I915_GEM_HWS_INDEX & 1);
1696
7c17d377
CW
1697 /* w/a for post sync ops following a GPGPU operation we
1698 * need a prior CS_STALL, which is emitted by the flush
1699 * following the batch.
1700 */
73dec95e
TU
1701 *cs++ = GFX_OP_PIPE_CONTROL(6);
1702 *cs++ = PIPE_CONTROL_GLOBAL_GTT_IVB | PIPE_CONTROL_CS_STALL |
1703 PIPE_CONTROL_QW_WRITE;
1704 *cs++ = intel_hws_seqno_address(request->engine);
1705 *cs++ = 0;
1706 *cs++ = request->global_seqno;
ce81a65c 1707 /* We're thrashing one dword of HWS. */
73dec95e
TU
1708 *cs++ = 0;
1709 *cs++ = MI_USER_INTERRUPT;
1710 *cs++ = MI_NOOP;
1711 request->tail = intel_ring_offset(request, cs);
ed1501d4 1712 assert_ring_tail_valid(request->ring, request->tail);
caddfe71 1713
73dec95e 1714 gen8_emit_wa_tail(request, cs);
4da46e1e
OM
1715}
1716
98f29e8d
CW
1717static const int gen8_emit_breadcrumb_render_sz = 8 + WA_TAIL_DWORDS;
1718
8753181e 1719static int gen8_init_rcs_context(struct drm_i915_gem_request *req)
e7778be1
TD
1720{
1721 int ret;
1722
4ac9659e 1723 ret = intel_ring_workarounds_emit(req);
e7778be1
TD
1724 if (ret)
1725 return ret;
1726
3bbaba0c
PA
1727 ret = intel_rcs_context_init_mocs(req);
1728 /*
1729 * Failing to program the MOCS is non-fatal.The system will not
1730 * run at peak performance. So generate an error and carry on.
1731 */
1732 if (ret)
1733 DRM_ERROR("MOCS failed to program: expect performance issues.\n");
1734
4e50f082 1735 return i915_gem_render_state_emit(req);
e7778be1
TD
1736}
1737
73e4d07f
OM
1738/**
1739 * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
14bb2c11 1740 * @engine: Engine Command Streamer.
73e4d07f 1741 */
0bc40be8 1742void intel_logical_ring_cleanup(struct intel_engine_cs *engine)
454afebd 1743{
6402c330 1744 struct drm_i915_private *dev_priv;
9832b9da 1745
27af5eea
TU
1746 /*
1747 * Tasklet cannot be active at this point due intel_mark_active/idle
1748 * so this is just for documentation.
1749 */
b620e870
MK
1750 if (WARN_ON(test_bit(TASKLET_STATE_SCHED, &engine->execlists.irq_tasklet.state)))
1751 tasklet_kill(&engine->execlists.irq_tasklet);
27af5eea 1752
c033666a 1753 dev_priv = engine->i915;
6402c330 1754
0bc40be8 1755 if (engine->buffer) {
0bc40be8 1756 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
b0366a54 1757 }
48d82387 1758
0bc40be8
TU
1759 if (engine->cleanup)
1760 engine->cleanup(engine);
48d82387 1761
e8a9c58f 1762 intel_engine_cleanup_common(engine);
17ee950d 1763
097d4f1c 1764 lrc_destroy_wa_ctx(engine);
c033666a 1765 engine->i915 = NULL;
3b3f1650
AG
1766 dev_priv->engine[engine->id] = NULL;
1767 kfree(engine);
454afebd
OM
1768}
1769
ff44ad51 1770static void execlists_set_default_submission(struct intel_engine_cs *engine)
ddd66c51 1771{
ff44ad51 1772 engine->submit_request = execlists_submit_request;
27a5f61b 1773 engine->cancel_requests = execlists_cancel_requests;
ff44ad51 1774 engine->schedule = execlists_schedule;
b620e870 1775 engine->execlists.irq_tasklet.func = intel_lrc_irq_handler;
ddd66c51
CW
1776}
1777
c9cacf93 1778static void
e1382efb 1779logical_ring_default_vfuncs(struct intel_engine_cs *engine)
c9cacf93
TU
1780{
1781 /* Default vfuncs which can be overriden by each engine. */
0bc40be8 1782 engine->init_hw = gen8_init_common_ring;
821ed7df 1783 engine->reset_hw = reset_common_ring;
e8a9c58f
CW
1784
1785 engine->context_pin = execlists_context_pin;
1786 engine->context_unpin = execlists_context_unpin;
1787
f73e7399
CW
1788 engine->request_alloc = execlists_request_alloc;
1789
0bc40be8 1790 engine->emit_flush = gen8_emit_flush;
9b81d556 1791 engine->emit_breadcrumb = gen8_emit_breadcrumb;
98f29e8d 1792 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_sz;
ff44ad51
CW
1793
1794 engine->set_default_submission = execlists_set_default_submission;
ddd66c51 1795
31bb59cc
CW
1796 engine->irq_enable = gen8_logical_ring_enable_irq;
1797 engine->irq_disable = gen8_logical_ring_disable_irq;
0bc40be8 1798 engine->emit_bb_start = gen8_emit_bb_start;
c9cacf93
TU
1799}
1800
d9f3af96 1801static inline void
c2c7f240 1802logical_ring_default_irqs(struct intel_engine_cs *engine)
d9f3af96 1803{
c2c7f240 1804 unsigned shift = engine->irq_shift;
0bc40be8
TU
1805 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << shift;
1806 engine->irq_keep_mask = GT_CONTEXT_SWITCH_INTERRUPT << shift;
d9f3af96
TU
1807}
1808
bb45438f
TU
1809static void
1810logical_ring_setup(struct intel_engine_cs *engine)
1811{
1812 struct drm_i915_private *dev_priv = engine->i915;
1813 enum forcewake_domains fw_domains;
1814
019bf277
TU
1815 intel_engine_setup_common(engine);
1816
bb45438f
TU
1817 /* Intentionally left blank. */
1818 engine->buffer = NULL;
1819
1820 fw_domains = intel_uncore_forcewake_for_reg(dev_priv,
1821 RING_ELSP(engine),
1822 FW_REG_WRITE);
1823
1824 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
1825 RING_CONTEXT_STATUS_PTR(engine),
1826 FW_REG_READ | FW_REG_WRITE);
1827
1828 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
1829 RING_CONTEXT_STATUS_BUF_BASE(engine),
1830 FW_REG_READ);
1831
b620e870 1832 engine->execlists.fw_domains = fw_domains;
bb45438f 1833
b620e870 1834 tasklet_init(&engine->execlists.irq_tasklet,
bb45438f
TU
1835 intel_lrc_irq_handler, (unsigned long)engine);
1836
bb45438f
TU
1837 logical_ring_default_vfuncs(engine);
1838 logical_ring_default_irqs(engine);
bb45438f
TU
1839}
1840
486e93f7 1841static int logical_ring_init(struct intel_engine_cs *engine)
a19d6ff2 1842{
a19d6ff2
TU
1843 int ret;
1844
019bf277 1845 ret = intel_engine_init_common(engine);
a19d6ff2
TU
1846 if (ret)
1847 goto error;
1848
a19d6ff2
TU
1849 return 0;
1850
1851error:
1852 intel_logical_ring_cleanup(engine);
1853 return ret;
1854}
1855
88d2ba2e 1856int logical_render_ring_init(struct intel_engine_cs *engine)
a19d6ff2
TU
1857{
1858 struct drm_i915_private *dev_priv = engine->i915;
1859 int ret;
1860
bb45438f
TU
1861 logical_ring_setup(engine);
1862
a19d6ff2
TU
1863 if (HAS_L3_DPF(dev_priv))
1864 engine->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
1865
1866 /* Override some for render ring. */
1867 if (INTEL_GEN(dev_priv) >= 9)
1868 engine->init_hw = gen9_init_render_ring;
1869 else
1870 engine->init_hw = gen8_init_render_ring;
1871 engine->init_context = gen8_init_rcs_context;
a19d6ff2 1872 engine->emit_flush = gen8_emit_flush_render;
9b81d556 1873 engine->emit_breadcrumb = gen8_emit_breadcrumb_render;
98f29e8d 1874 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_render_sz;
a19d6ff2 1875
f51455d4 1876 ret = intel_engine_create_scratch(engine, PAGE_SIZE);
a19d6ff2
TU
1877 if (ret)
1878 return ret;
1879
1880 ret = intel_init_workaround_bb(engine);
1881 if (ret) {
1882 /*
1883 * We continue even if we fail to initialize WA batch
1884 * because we only expect rare glitches but nothing
1885 * critical to prevent us from using GPU
1886 */
1887 DRM_ERROR("WA batch buffer initialization failed: %d\n",
1888 ret);
1889 }
1890
d038fc7e 1891 return logical_ring_init(engine);
a19d6ff2
TU
1892}
1893
88d2ba2e 1894int logical_xcs_ring_init(struct intel_engine_cs *engine)
bb45438f
TU
1895{
1896 logical_ring_setup(engine);
1897
1898 return logical_ring_init(engine);
454afebd
OM
1899}
1900
0cea6502 1901static u32
c033666a 1902make_rpcs(struct drm_i915_private *dev_priv)
0cea6502
JM
1903{
1904 u32 rpcs = 0;
1905
1906 /*
1907 * No explicit RPCS request is needed to ensure full
1908 * slice/subslice/EU enablement prior to Gen9.
1909 */
c033666a 1910 if (INTEL_GEN(dev_priv) < 9)
0cea6502
JM
1911 return 0;
1912
1913 /*
1914 * Starting in Gen9, render power gating can leave
1915 * slice/subslice/EU in a partially enabled state. We
1916 * must make an explicit request through RPCS for full
1917 * enablement.
1918 */
43b67998 1919 if (INTEL_INFO(dev_priv)->sseu.has_slice_pg) {
0cea6502 1920 rpcs |= GEN8_RPCS_S_CNT_ENABLE;
f08a0c92 1921 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.slice_mask) <<
0cea6502
JM
1922 GEN8_RPCS_S_CNT_SHIFT;
1923 rpcs |= GEN8_RPCS_ENABLE;
1924 }
1925
43b67998 1926 if (INTEL_INFO(dev_priv)->sseu.has_subslice_pg) {
0cea6502 1927 rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
57ec171e 1928 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.subslice_mask) <<
0cea6502
JM
1929 GEN8_RPCS_SS_CNT_SHIFT;
1930 rpcs |= GEN8_RPCS_ENABLE;
1931 }
1932
43b67998
ID
1933 if (INTEL_INFO(dev_priv)->sseu.has_eu_pg) {
1934 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
0cea6502 1935 GEN8_RPCS_EU_MIN_SHIFT;
43b67998 1936 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
0cea6502
JM
1937 GEN8_RPCS_EU_MAX_SHIFT;
1938 rpcs |= GEN8_RPCS_ENABLE;
1939 }
1940
1941 return rpcs;
1942}
1943
0bc40be8 1944static u32 intel_lr_indirect_ctx_offset(struct intel_engine_cs *engine)
71562919
MT
1945{
1946 u32 indirect_ctx_offset;
1947
c033666a 1948 switch (INTEL_GEN(engine->i915)) {
71562919 1949 default:
c033666a 1950 MISSING_CASE(INTEL_GEN(engine->i915));
71562919 1951 /* fall through */
7bd0a2c6
MT
1952 case 10:
1953 indirect_ctx_offset =
1954 GEN10_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
1955 break;
71562919
MT
1956 case 9:
1957 indirect_ctx_offset =
1958 GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
1959 break;
1960 case 8:
1961 indirect_ctx_offset =
1962 GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
1963 break;
1964 }
1965
1966 return indirect_ctx_offset;
1967}
1968
56e51bf0 1969static void execlists_init_reg_state(u32 *regs,
a3aabe86
CW
1970 struct i915_gem_context *ctx,
1971 struct intel_engine_cs *engine,
1972 struct intel_ring *ring)
8670d6f9 1973{
a3aabe86
CW
1974 struct drm_i915_private *dev_priv = engine->i915;
1975 struct i915_hw_ppgtt *ppgtt = ctx->ppgtt ?: dev_priv->mm.aliasing_ppgtt;
56e51bf0
TU
1976 u32 base = engine->mmio_base;
1977 bool rcs = engine->id == RCS;
1978
1979 /* A context is actually a big batch buffer with several
1980 * MI_LOAD_REGISTER_IMM commands followed by (reg, value) pairs. The
1981 * values we are setting here are only for the first context restore:
1982 * on a subsequent save, the GPU will recreate this batchbuffer with new
1983 * values (including all the missing MI_LOAD_REGISTER_IMM commands that
1984 * we are not initializing here).
1985 */
1986 regs[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(rcs ? 14 : 11) |
1987 MI_LRI_FORCE_POSTED;
1988
1989 CTX_REG(regs, CTX_CONTEXT_CONTROL, RING_CONTEXT_CONTROL(engine),
1990 _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
1991 CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
1992 (HAS_RESOURCE_STREAMER(dev_priv) ?
1993 CTX_CTRL_RS_CTX_ENABLE : 0)));
1994 CTX_REG(regs, CTX_RING_HEAD, RING_HEAD(base), 0);
1995 CTX_REG(regs, CTX_RING_TAIL, RING_TAIL(base), 0);
1996 CTX_REG(regs, CTX_RING_BUFFER_START, RING_START(base), 0);
1997 CTX_REG(regs, CTX_RING_BUFFER_CONTROL, RING_CTL(base),
1998 RING_CTL_SIZE(ring->size) | RING_VALID);
1999 CTX_REG(regs, CTX_BB_HEAD_U, RING_BBADDR_UDW(base), 0);
2000 CTX_REG(regs, CTX_BB_HEAD_L, RING_BBADDR(base), 0);
2001 CTX_REG(regs, CTX_BB_STATE, RING_BBSTATE(base), RING_BB_PPGTT);
2002 CTX_REG(regs, CTX_SECOND_BB_HEAD_U, RING_SBBADDR_UDW(base), 0);
2003 CTX_REG(regs, CTX_SECOND_BB_HEAD_L, RING_SBBADDR(base), 0);
2004 CTX_REG(regs, CTX_SECOND_BB_STATE, RING_SBBSTATE(base), 0);
2005 if (rcs) {
604a8f6f
CW
2006 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
2007
56e51bf0
TU
2008 CTX_REG(regs, CTX_RCS_INDIRECT_CTX, RING_INDIRECT_CTX(base), 0);
2009 CTX_REG(regs, CTX_RCS_INDIRECT_CTX_OFFSET,
2010 RING_INDIRECT_CTX_OFFSET(base), 0);
604a8f6f 2011 if (wa_ctx->indirect_ctx.size) {
bde13ebd 2012 u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
17ee950d 2013
56e51bf0 2014 regs[CTX_RCS_INDIRECT_CTX + 1] =
097d4f1c
TU
2015 (ggtt_offset + wa_ctx->indirect_ctx.offset) |
2016 (wa_ctx->indirect_ctx.size / CACHELINE_BYTES);
17ee950d 2017
56e51bf0 2018 regs[CTX_RCS_INDIRECT_CTX_OFFSET + 1] =
0bc40be8 2019 intel_lr_indirect_ctx_offset(engine) << 6;
604a8f6f
CW
2020 }
2021
2022 CTX_REG(regs, CTX_BB_PER_CTX_PTR, RING_BB_PER_CTX_PTR(base), 0);
2023 if (wa_ctx->per_ctx.size) {
2024 u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
17ee950d 2025
56e51bf0 2026 regs[CTX_BB_PER_CTX_PTR + 1] =
097d4f1c 2027 (ggtt_offset + wa_ctx->per_ctx.offset) | 0x01;
17ee950d 2028 }
8670d6f9 2029 }
56e51bf0
TU
2030
2031 regs[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9) | MI_LRI_FORCE_POSTED;
2032
2033 CTX_REG(regs, CTX_CTX_TIMESTAMP, RING_CTX_TIMESTAMP(base), 0);
0d925ea0 2034 /* PDP values well be assigned later if needed */
56e51bf0
TU
2035 CTX_REG(regs, CTX_PDP3_UDW, GEN8_RING_PDP_UDW(engine, 3), 0);
2036 CTX_REG(regs, CTX_PDP3_LDW, GEN8_RING_PDP_LDW(engine, 3), 0);
2037 CTX_REG(regs, CTX_PDP2_UDW, GEN8_RING_PDP_UDW(engine, 2), 0);
2038 CTX_REG(regs, CTX_PDP2_LDW, GEN8_RING_PDP_LDW(engine, 2), 0);
2039 CTX_REG(regs, CTX_PDP1_UDW, GEN8_RING_PDP_UDW(engine, 1), 0);
2040 CTX_REG(regs, CTX_PDP1_LDW, GEN8_RING_PDP_LDW(engine, 1), 0);
2041 CTX_REG(regs, CTX_PDP0_UDW, GEN8_RING_PDP_UDW(engine, 0), 0);
2042 CTX_REG(regs, CTX_PDP0_LDW, GEN8_RING_PDP_LDW(engine, 0), 0);
d7b2633d 2043
949e8ab3 2044 if (ppgtt && i915_vm_is_48bit(&ppgtt->base)) {
2dba3239
MT
2045 /* 64b PPGTT (48bit canonical)
2046 * PDP0_DESCRIPTOR contains the base address to PML4 and
2047 * other PDP Descriptors are ignored.
2048 */
56e51bf0 2049 ASSIGN_CTX_PML4(ppgtt, regs);
2dba3239
MT
2050 }
2051
56e51bf0
TU
2052 if (rcs) {
2053 regs[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
2054 CTX_REG(regs, CTX_R_PWR_CLK_STATE, GEN8_R_PWR_CLK_STATE,
2055 make_rpcs(dev_priv));
19f81df2
RB
2056
2057 i915_oa_init_reg_state(engine, ctx, regs);
8670d6f9 2058 }
a3aabe86
CW
2059}
2060
2061static int
2062populate_lr_context(struct i915_gem_context *ctx,
2063 struct drm_i915_gem_object *ctx_obj,
2064 struct intel_engine_cs *engine,
2065 struct intel_ring *ring)
2066{
2067 void *vaddr;
2068 int ret;
2069
2070 ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
2071 if (ret) {
2072 DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
2073 return ret;
2074 }
2075
2076 vaddr = i915_gem_object_pin_map(ctx_obj, I915_MAP_WB);
2077 if (IS_ERR(vaddr)) {
2078 ret = PTR_ERR(vaddr);
2079 DRM_DEBUG_DRIVER("Could not map object pages! (%d)\n", ret);
2080 return ret;
2081 }
a4f5ea64 2082 ctx_obj->mm.dirty = true;
a3aabe86
CW
2083
2084 /* The second page of the context object contains some fields which must
2085 * be set up prior to the first execution. */
2086
2087 execlists_init_reg_state(vaddr + LRC_STATE_PN * PAGE_SIZE,
2088 ctx, engine, ring);
8670d6f9 2089
7d774cac 2090 i915_gem_object_unpin_map(ctx_obj);
8670d6f9
OM
2091
2092 return 0;
2093}
2094
e2efd130 2095static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
978f1e09 2096 struct intel_engine_cs *engine)
ede7d42b 2097{
8c857917 2098 struct drm_i915_gem_object *ctx_obj;
9021ad03 2099 struct intel_context *ce = &ctx->engine[engine->id];
bf3783e5 2100 struct i915_vma *vma;
8c857917 2101 uint32_t context_size;
7e37f889 2102 struct intel_ring *ring;
8c857917
OM
2103 int ret;
2104
9021ad03 2105 WARN_ON(ce->state);
ede7d42b 2106
63ffbcda 2107 context_size = round_up(engine->context_size, I915_GTT_PAGE_SIZE);
8c857917 2108
0b29c75a
MT
2109 /*
2110 * Before the actual start of the context image, we insert a few pages
2111 * for our own use and for sharing with the GuC.
2112 */
2113 context_size += LRC_HEADER_PAGES * PAGE_SIZE;
d1675198 2114
12d79d78 2115 ctx_obj = i915_gem_object_create(ctx->i915, context_size);
fe3db79b 2116 if (IS_ERR(ctx_obj)) {
3126a660 2117 DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
fe3db79b 2118 return PTR_ERR(ctx_obj);
8c857917
OM
2119 }
2120
a01cb37a 2121 vma = i915_vma_instance(ctx_obj, &ctx->i915->ggtt.base, NULL);
bf3783e5
CW
2122 if (IS_ERR(vma)) {
2123 ret = PTR_ERR(vma);
2124 goto error_deref_obj;
2125 }
2126
7e37f889 2127 ring = intel_engine_create_ring(engine, ctx->ring_size);
dca33ecc
CW
2128 if (IS_ERR(ring)) {
2129 ret = PTR_ERR(ring);
e84fe803 2130 goto error_deref_obj;
8670d6f9
OM
2131 }
2132
dca33ecc 2133 ret = populate_lr_context(ctx, ctx_obj, engine, ring);
8670d6f9
OM
2134 if (ret) {
2135 DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
dca33ecc 2136 goto error_ring_free;
84c2377f
OM
2137 }
2138
dca33ecc 2139 ce->ring = ring;
bf3783e5 2140 ce->state = vma;
0d402a24 2141 ce->initialised |= engine->init_context == NULL;
ede7d42b
OM
2142
2143 return 0;
8670d6f9 2144
dca33ecc 2145error_ring_free:
7e37f889 2146 intel_ring_free(ring);
e84fe803 2147error_deref_obj:
f8c417cd 2148 i915_gem_object_put(ctx_obj);
8670d6f9 2149 return ret;
ede7d42b 2150}
3e5b6f05 2151
821ed7df 2152void intel_lr_context_resume(struct drm_i915_private *dev_priv)
3e5b6f05 2153{
e2f80391 2154 struct intel_engine_cs *engine;
bafb2f7d 2155 struct i915_gem_context *ctx;
3b3f1650 2156 enum intel_engine_id id;
bafb2f7d
CW
2157
2158 /* Because we emit WA_TAIL_DWORDS there may be a disparity
2159 * between our bookkeeping in ce->ring->head and ce->ring->tail and
2160 * that stored in context. As we only write new commands from
2161 * ce->ring->tail onwards, everything before that is junk. If the GPU
2162 * starts reading from its RING_HEAD from the context, it may try to
2163 * execute that junk and die.
2164 *
2165 * So to avoid that we reset the context images upon resume. For
2166 * simplicity, we just zero everything out.
2167 */
829a0af2 2168 list_for_each_entry(ctx, &dev_priv->contexts.list, link) {
3b3f1650 2169 for_each_engine(engine, dev_priv, id) {
bafb2f7d
CW
2170 struct intel_context *ce = &ctx->engine[engine->id];
2171 u32 *reg;
3e5b6f05 2172
bafb2f7d
CW
2173 if (!ce->state)
2174 continue;
7d774cac 2175
bafb2f7d
CW
2176 reg = i915_gem_object_pin_map(ce->state->obj,
2177 I915_MAP_WB);
2178 if (WARN_ON(IS_ERR(reg)))
2179 continue;
3e5b6f05 2180
bafb2f7d
CW
2181 reg += LRC_STATE_PN * PAGE_SIZE / sizeof(*reg);
2182 reg[CTX_RING_HEAD+1] = 0;
2183 reg[CTX_RING_TAIL+1] = 0;
3e5b6f05 2184
a4f5ea64 2185 ce->state->obj->mm.dirty = true;
bafb2f7d 2186 i915_gem_object_unpin_map(ce->state->obj);
3e5b6f05 2187
e6ba9992 2188 intel_ring_reset(ce->ring, 0);
bafb2f7d 2189 }
3e5b6f05
TD
2190 }
2191}