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