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