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