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