blob: 1960ba5ff9e433c5ac39b2b61435462e4d06d37d [file] [log] [blame]
Oscar Mateob20385f2014-07-24 17:04:10 +01001/*
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
Oscar Mateo73e4d072014-07-24 17:04:48 +010031/**
32 * DOC: Logical Rings, Logical Ring Contexts and Execlists
33 *
34 * Motivation:
Oscar Mateob20385f2014-07-24 17:04:10 +010035 * 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 *
Oscar Mateo73e4d072014-07-24 17:04:48 +010039 * 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:
Oscar Mateob20385f2014-07-24 17:04:10 +010090 * Execlists are the new method by which, on gen8+ hardware, workloads are
91 * submitted for execution (as opposed to the legacy, ringbuffer-based, method).
Oscar Mateo73e4d072014-07-24 17:04:48 +010092 * 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 *
Oscar Mateob20385f2014-07-24 17:04:10 +0100133 */
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +0100134#include <linux/interrupt.h>
Oscar Mateob20385f2014-07-24 17:04:10 +0100135
136#include <drm/drmP.h>
137#include <drm/i915_drm.h>
138#include "i915_drv.h"
Peter Antoine3bbaba02015-07-10 20:13:11 +0300139#include "intel_mocs.h"
Oscar Mateo127f1002014-07-24 17:04:11 +0100140
Thomas Daniele981e7b2014-07-24 17:04:39 +0100141#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)
Oscar Mateo8670d6f2014-07-24 17:04:17 +0100154
Chris Wilson70c2a242016-09-09 14:11:46 +0100155#define GEN8_CTX_STATUS_COMPLETED_MASK \
156 (GEN8_CTX_STATUS_ACTIVE_IDLE | \
157 GEN8_CTX_STATUS_PREEMPTED | \
158 GEN8_CTX_STATUS_ELEMENT_SWITCH)
159
Oscar Mateo8670d6f2014-07-24 17:04:17 +0100160#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
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +0000189#define CTX_REG(reg_state, pos, reg, val) do { \
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200190 (reg_state)[(pos)+0] = i915_mmio_reg_offset(reg); \
Ville Syrjälä0d925ea2015-11-04 23:20:11 +0200191 (reg_state)[(pos)+1] = (val); \
192} while (0)
193
194#define ASSIGN_CTX_PDP(ppgtt, reg_state, n) do { \
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300195 const u64 _addr = i915_page_dir_dma_addr((ppgtt), (n)); \
Michel Thierrye5815a22015-04-08 12:13:32 +0100196 reg_state[CTX_PDP ## n ## _UDW+1] = upper_32_bits(_addr); \
197 reg_state[CTX_PDP ## n ## _LDW+1] = lower_32_bits(_addr); \
Ville Syrjälä9244a812015-11-04 23:20:09 +0200198} while (0)
Michel Thierrye5815a22015-04-08 12:13:32 +0100199
Ville Syrjälä9244a812015-11-04 23:20:09 +0200200#define ASSIGN_CTX_PML4(ppgtt, reg_state) do { \
Michel Thierry2dba3232015-07-30 11:06:23 +0100201 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)); \
Ville Syrjälä9244a812015-11-04 23:20:09 +0200203} while (0)
Michel Thierry2dba3232015-07-30 11:06:23 +0100204
Michel Thierry71562912016-02-23 10:31:49 +0000205#define GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x17
206#define GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x26
Michel Thierry7bd0a2c2017-06-06 13:30:38 -0700207#define GEN10_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x19
Ben Widawsky84b790f2014-07-24 17:04:36 +0100208
Chris Wilson0e93cdd2016-04-29 09:07:06 +0100209/* Typical size of the average request (2 pipecontrols and a MI_BB) */
210#define EXECLISTS_REQUEST_SIZE 64 /* bytes */
211
Chris Wilsona3aabe82016-10-04 21:11:26 +0100212#define WA_TAIL_DWORDS 2
213
Chris Wilsone2efd132016-05-24 14:53:34 +0100214static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
Chris Wilson978f1e02016-04-28 09:56:54 +0100215 struct intel_engine_cs *engine);
Chris Wilsona3aabe82016-10-04 21:11:26 +0100216static void execlists_init_reg_state(u32 *reg_state,
217 struct i915_gem_context *ctx,
218 struct intel_engine_cs *engine,
219 struct intel_ring *ring);
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000220
Oscar Mateo73e4d072014-07-24 17:04:48 +0100221/**
222 * intel_sanitize_enable_execlists() - sanitize i915.enable_execlists
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100223 * @dev_priv: i915 device private
Oscar Mateo73e4d072014-07-24 17:04:48 +0100224 * @enable_execlists: value of i915.enable_execlists module parameter.
225 *
226 * Only certain platforms support Execlists (the prerequisites being
Thomas Daniel27401d12014-12-11 12:48:35 +0000227 * support for Logical Ring Contexts and Aliasing PPGTT or better).
Oscar Mateo73e4d072014-07-24 17:04:48 +0100228 *
229 * Return: 1 if Execlists is supported and has to be enabled.
230 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100231int intel_sanitize_enable_execlists(struct drm_i915_private *dev_priv, int enable_execlists)
Oscar Mateo127f1002014-07-24 17:04:11 +0100232{
Zhiyuan Lva0bd6c32015-08-28 15:41:16 +0800233 /* On platforms with execlist available, vGPU will only
234 * support execlist mode, no ring buffer mode.
235 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100236 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv) && intel_vgpu_active(dev_priv))
Zhiyuan Lva0bd6c32015-08-28 15:41:16 +0800237 return 1;
238
Chris Wilsonc0336662016-05-06 15:40:21 +0100239 if (INTEL_GEN(dev_priv) >= 9)
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000240 return 1;
241
Oscar Mateo127f1002014-07-24 17:04:11 +0100242 if (enable_execlists == 0)
243 return 0;
244
Daniel Vetter5a21b662016-05-24 17:13:53 +0200245 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv) &&
246 USES_PPGTT(dev_priv) &&
247 i915.use_mmio_flip >= 0)
Oscar Mateo127f1002014-07-24 17:04:11 +0100248 return 1;
249
250 return 0;
251}
Oscar Mateoede7d422014-07-24 17:04:12 +0100252
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000253/**
254 * intel_lr_context_descriptor_update() - calculate & cache the descriptor
255 * descriptor for a pinned context
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000256 * @ctx: Context to work on
Chris Wilson9021ad02016-05-24 14:53:37 +0100257 * @engine: Engine the descriptor will be used with
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000258 *
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 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200264 * This is what a descriptor looks like, from LSB to MSB::
265 *
Mika Kuoppala2355cf02017-01-27 15:03:09 +0200266 * bits 0-11: flags, GEN8_CTX_* (cached in ctx->desc_template)
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200267 * 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
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000271 */
272static void
Chris Wilsone2efd132016-05-24 14:53:34 +0100273intel_lr_context_descriptor_update(struct i915_gem_context *ctx,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000274 struct intel_engine_cs *engine)
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000275{
Chris Wilson9021ad02016-05-24 14:53:37 +0100276 struct intel_context *ce = &ctx->engine[engine->id];
Chris Wilson7069b142016-04-28 09:56:52 +0100277 u64 desc;
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000278
Chris Wilson7069b142016-04-28 09:56:52 +0100279 BUILD_BUG_ON(MAX_CONTEXT_HW_ID > (1<<GEN8_CTX_ID_WIDTH));
280
Mika Kuoppala2355cf02017-01-27 15:03:09 +0200281 desc = ctx->desc_template; /* bits 0-11 */
Michel Thierry0b29c752017-09-13 09:56:00 +0100282 desc |= i915_ggtt_offset(ce->state) + LRC_HEADER_PAGES * PAGE_SIZE;
Chris Wilson9021ad02016-05-24 14:53:37 +0100283 /* bits 12-31 */
Chris Wilson7069b142016-04-28 09:56:52 +0100284 desc |= (u64)ctx->hw_id << GEN8_CTX_ID_SHIFT; /* bits 32-52 */
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000285
Chris Wilson9021ad02016-05-24 14:53:37 +0100286 ce->lrc_desc = desc;
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000287}
288
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100289static inline void
290execlists_context_status_change(struct drm_i915_gem_request *rq,
291 unsigned long status)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100292{
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100293 /*
294 * Only used when GVT-g is enabled now. When GVT-g is disabled,
295 * The compiler should eliminate this function as dead-code.
296 */
297 if (!IS_ENABLED(CONFIG_DRM_I915_GVT))
298 return;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100299
Changbin Du3fc03062017-03-13 10:47:11 +0800300 atomic_notifier_call_chain(&rq->engine->context_status_notifier,
301 status, rq);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100302}
303
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000304static void
305execlists_update_context_pdps(struct i915_hw_ppgtt *ppgtt, u32 *reg_state)
306{
307 ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
308 ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
309 ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
310 ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
311}
312
Chris Wilson70c2a242016-09-09 14:11:46 +0100313static u64 execlists_update_context(struct drm_i915_gem_request *rq)
Oscar Mateoae1250b2014-07-24 17:04:37 +0100314{
Chris Wilson70c2a242016-09-09 14:11:46 +0100315 struct intel_context *ce = &rq->ctx->engine[rq->engine->id];
Zhi Wang04da8112017-02-06 18:37:16 +0800316 struct i915_hw_ppgtt *ppgtt =
317 rq->ctx->ppgtt ?: rq->i915->mm.aliasing_ppgtt;
Chris Wilson70c2a242016-09-09 14:11:46 +0100318 u32 *reg_state = ce->lrc_reg_state;
Oscar Mateoae1250b2014-07-24 17:04:37 +0100319
Chris Wilsone6ba9992017-04-25 14:00:49 +0100320 reg_state[CTX_RING_TAIL+1] = intel_ring_set_tail(rq->ring, rq->tail);
Oscar Mateoae1250b2014-07-24 17:04:37 +0100321
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000322 /* True 32b PPGTT with dynamic page allocation: update PDP
323 * registers and point the unallocated PDPs to scratch page.
324 * PML4 is allocated during ppgtt init, so this is not needed
325 * in 48-bit mode.
326 */
Chris Wilson949e8ab2017-02-09 14:40:36 +0000327 if (ppgtt && !i915_vm_is_48bit(&ppgtt->base))
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000328 execlists_update_context_pdps(ppgtt, reg_state);
Chris Wilson70c2a242016-09-09 14:11:46 +0100329
330 return ce->lrc_desc;
Oscar Mateoae1250b2014-07-24 17:04:37 +0100331}
332
Chris Wilson70c2a242016-09-09 14:11:46 +0100333static void execlists_submit_ports(struct intel_engine_cs *engine)
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100334{
Chris Wilson70c2a242016-09-09 14:11:46 +0100335 struct execlist_port *port = engine->execlist_port;
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100336 u32 __iomem *elsp =
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100337 engine->i915->regs + i915_mmio_reg_offset(RING_ELSP(engine));
338 unsigned int n;
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100339
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100340 for (n = ARRAY_SIZE(engine->execlist_port); n--; ) {
341 struct drm_i915_gem_request *rq;
342 unsigned int count;
343 u64 desc;
Chris Wilson70c2a242016-09-09 14:11:46 +0100344
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100345 rq = port_unpack(&port[n], &count);
346 if (rq) {
347 GEM_BUG_ON(count > !n);
348 if (!count++)
349 execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_IN);
350 port_set(&port[n], port_pack(rq, count));
351 desc = execlists_update_context(rq);
352 GEM_DEBUG_EXEC(port[n].context_id = upper_32_bits(desc));
353 } else {
354 GEM_BUG_ON(!n);
355 desc = 0;
356 }
357
358 writel(upper_32_bits(desc), elsp);
359 writel(lower_32_bits(desc), elsp);
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100360 }
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100361}
362
Chris Wilson70c2a242016-09-09 14:11:46 +0100363static bool ctx_single_port_submission(const struct i915_gem_context *ctx)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100364{
Chris Wilson70c2a242016-09-09 14:11:46 +0100365 return (IS_ENABLED(CONFIG_DRM_I915_GVT) &&
Chris Wilson60958682016-12-31 11:20:11 +0000366 i915_gem_context_force_single_submission(ctx));
Ben Widawsky84b790f2014-07-24 17:04:36 +0100367}
368
Chris Wilson70c2a242016-09-09 14:11:46 +0100369static bool can_merge_ctx(const struct i915_gem_context *prev,
370 const struct i915_gem_context *next)
Michel Thierryacdd8842014-07-24 17:04:38 +0100371{
Chris Wilson70c2a242016-09-09 14:11:46 +0100372 if (prev != next)
373 return false;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100374
Chris Wilson70c2a242016-09-09 14:11:46 +0100375 if (ctx_single_port_submission(prev))
376 return false;
Michel Thierryacdd8842014-07-24 17:04:38 +0100377
Chris Wilson70c2a242016-09-09 14:11:46 +0100378 return true;
379}
Peter Antoine779949f2015-05-11 16:03:27 +0100380
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100381static void port_assign(struct execlist_port *port,
382 struct drm_i915_gem_request *rq)
383{
384 GEM_BUG_ON(rq == port_request(port));
385
386 if (port_isset(port))
387 i915_gem_request_put(port_request(port));
388
389 port_set(port, port_pack(i915_gem_request_get(rq), port_count(port)));
390}
391
Chris Wilson70c2a242016-09-09 14:11:46 +0100392static void execlists_dequeue(struct intel_engine_cs *engine)
393{
Chris Wilson20311bd2016-11-14 20:41:03 +0000394 struct drm_i915_gem_request *last;
Chris Wilson70c2a242016-09-09 14:11:46 +0100395 struct execlist_port *port = engine->execlist_port;
Chris Wilson20311bd2016-11-14 20:41:03 +0000396 struct rb_node *rb;
Chris Wilson70c2a242016-09-09 14:11:46 +0100397 bool submit = false;
Michel Thierryacdd8842014-07-24 17:04:38 +0100398
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100399 last = port_request(port);
Chris Wilson70c2a242016-09-09 14:11:46 +0100400 if (last)
401 /* WaIdleLiteRestore:bdw,skl
402 * Apply the wa NOOPs to prevent ring:HEAD == req:TAIL
Chris Wilson9b81d552016-10-28 13:58:50 +0100403 * as we resubmit the request. See gen8_emit_breadcrumb()
Chris Wilson70c2a242016-09-09 14:11:46 +0100404 * for where we prepare the padding after the end of the
405 * request.
Michel Thierry53292cd2015-04-15 18:11:33 +0100406 */
Chris Wilson70c2a242016-09-09 14:11:46 +0100407 last->tail = last->wa_tail;
408
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100409 GEM_BUG_ON(port_isset(&port[1]));
Chris Wilson70c2a242016-09-09 14:11:46 +0100410
411 /* Hardware submission is through 2 ports. Conceptually each port
412 * has a (RING_START, RING_HEAD, RING_TAIL) tuple. RING_START is
413 * static for a context, and unique to each, so we only execute
414 * requests belonging to a single context from each ring. RING_HEAD
415 * is maintained by the CS in the context image, it marks the place
416 * where it got up to last time, and through RING_TAIL we tell the CS
417 * where we want to execute up to this time.
418 *
419 * In this list the requests are in order of execution. Consecutive
420 * requests from the same context are adjacent in the ringbuffer. We
421 * can combine these requests into a single RING_TAIL update:
422 *
423 * RING_HEAD...req1...req2
424 * ^- RING_TAIL
425 * since to execute req2 the CS must first execute req1.
426 *
427 * Our goal then is to point each port to the end of a consecutive
428 * sequence of requests as being the most optimal (fewest wake ups
429 * and context switches) submission.
430 */
431
Tvrtko Ursulin9f7886d2017-03-21 10:55:11 +0000432 spin_lock_irq(&engine->timeline->lock);
Chris Wilson20311bd2016-11-14 20:41:03 +0000433 rb = engine->execlist_first;
Chris Wilson6c067572017-05-17 13:10:03 +0100434 GEM_BUG_ON(rb_first(&engine->execlist_queue) != rb);
Chris Wilson20311bd2016-11-14 20:41:03 +0000435 while (rb) {
Chris Wilson6c067572017-05-17 13:10:03 +0100436 struct i915_priolist *p = rb_entry(rb, typeof(*p), node);
437 struct drm_i915_gem_request *rq, *rn;
Chris Wilson20311bd2016-11-14 20:41:03 +0000438
Chris Wilson6c067572017-05-17 13:10:03 +0100439 list_for_each_entry_safe(rq, rn, &p->requests, priotree.link) {
440 /*
441 * Can we combine this request with the current port?
442 * It has to be the same context/ringbuffer and not
443 * have any exceptions (e.g. GVT saying never to
444 * combine contexts).
445 *
446 * If we can combine the requests, we can execute both
447 * by updating the RING_TAIL to point to the end of the
448 * second request, and so we never need to tell the
449 * hardware about the first.
Chris Wilson70c2a242016-09-09 14:11:46 +0100450 */
Chris Wilson6c067572017-05-17 13:10:03 +0100451 if (last && !can_merge_ctx(rq->ctx, last->ctx)) {
452 /*
453 * If we are on the second port and cannot
454 * combine this request with the last, then we
455 * are done.
456 */
457 if (port != engine->execlist_port) {
458 __list_del_many(&p->requests,
459 &rq->priotree.link);
460 goto done;
461 }
Chris Wilson70c2a242016-09-09 14:11:46 +0100462
Chris Wilson6c067572017-05-17 13:10:03 +0100463 /*
464 * If GVT overrides us we only ever submit
465 * port[0], leaving port[1] empty. Note that we
466 * also have to be careful that we don't queue
467 * the same context (even though a different
468 * request) to the second port.
469 */
470 if (ctx_single_port_submission(last->ctx) ||
471 ctx_single_port_submission(rq->ctx)) {
472 __list_del_many(&p->requests,
473 &rq->priotree.link);
474 goto done;
475 }
Chris Wilson70c2a242016-09-09 14:11:46 +0100476
Chris Wilson6c067572017-05-17 13:10:03 +0100477 GEM_BUG_ON(last->ctx == rq->ctx);
Chris Wilson70c2a242016-09-09 14:11:46 +0100478
Chris Wilson6c067572017-05-17 13:10:03 +0100479 if (submit)
480 port_assign(port, last);
481 port++;
482 }
483
484 INIT_LIST_HEAD(&rq->priotree.link);
485 rq->priotree.priority = INT_MAX;
486
487 __i915_gem_request_submit(rq);
488 trace_i915_gem_request_in(rq, port_index(port, engine));
489 last = rq;
490 submit = true;
Chris Wilson70c2a242016-09-09 14:11:46 +0100491 }
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000492
Chris Wilson20311bd2016-11-14 20:41:03 +0000493 rb = rb_next(rb);
Chris Wilson6c067572017-05-17 13:10:03 +0100494 rb_erase(&p->node, &engine->execlist_queue);
495 INIT_LIST_HEAD(&p->requests);
496 if (p->priority != I915_PRIORITY_NORMAL)
Chris Wilsonc5cf9a92017-05-17 13:10:04 +0100497 kmem_cache_free(engine->i915->priorities, p);
Michel Thierry53292cd2015-04-15 18:11:33 +0100498 }
Chris Wilson6c067572017-05-17 13:10:03 +0100499done:
500 engine->execlist_first = rb;
501 if (submit)
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100502 port_assign(port, last);
Tvrtko Ursulin9f7886d2017-03-21 10:55:11 +0000503 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson70c2a242016-09-09 14:11:46 +0100504
505 if (submit)
506 execlists_submit_ports(engine);
Michel Thierryacdd8842014-07-24 17:04:38 +0100507}
508
Chris Wilson816ee792017-01-24 11:00:03 +0000509static bool execlists_elsp_ready(const struct intel_engine_cs *engine)
Ben Widawsky91a41032016-01-05 10:30:07 -0800510{
Chris Wilson816ee792017-01-24 11:00:03 +0000511 const struct execlist_port *port = engine->execlist_port;
Ben Widawsky91a41032016-01-05 10:30:07 -0800512
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100513 return port_count(&port[0]) + port_count(&port[1]) < 2;
Ben Widawsky91a41032016-01-05 10:30:07 -0800514}
515
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200516/*
Oscar Mateo73e4d072014-07-24 17:04:48 +0100517 * Check the unread Context Status Buffers and manage the submission of new
518 * contexts to the ELSP accordingly.
519 */
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +0100520static void intel_lrc_irq_handler(unsigned long data)
Thomas Daniele981e7b2014-07-24 17:04:39 +0100521{
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +0100522 struct intel_engine_cs *engine = (struct intel_engine_cs *)data;
Chris Wilson70c2a242016-09-09 14:11:46 +0100523 struct execlist_port *port = engine->execlist_port;
Chris Wilsonc0336662016-05-06 15:40:21 +0100524 struct drm_i915_private *dev_priv = engine->i915;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100525
Chris Wilson48921262017-04-11 18:58:50 +0100526 /* We can skip acquiring intel_runtime_pm_get() here as it was taken
527 * on our behalf by the request (see i915_gem_mark_busy()) and it will
528 * not be relinquished until the device is idle (see
529 * i915_gem_idle_work_handler()). As a precaution, we make sure
530 * that all ELSP are drained i.e. we have processed the CSB,
531 * before allowing ourselves to idle and calling intel_runtime_pm_put().
532 */
533 GEM_BUG_ON(!dev_priv->gt.awake);
534
Tvrtko Ursulin37566852016-04-12 14:37:31 +0100535 intel_uncore_forcewake_get(dev_priv, engine->fw_domains);
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000536
Chris Wilson899f6202017-03-21 11:33:20 +0000537 /* Prefer doing test_and_clear_bit() as a two stage operation to avoid
538 * imposing the cost of a locked atomic transaction when submitting a
539 * new request (outside of the context-switch interrupt).
540 */
541 while (test_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted)) {
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100542 /* The HWSP contains a (cacheable) mirror of the CSB */
543 const u32 *buf =
544 &engine->status_page.page_addr[I915_HWS_CSB_BUF0_INDEX];
Chris Wilson4af0d722017-03-25 20:10:53 +0000545 unsigned int head, tail;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100546
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100547 /* However GVT emulation depends upon intercepting CSB mmio */
548 if (unlikely(engine->csb_use_mmio)) {
549 buf = (u32 * __force)
550 (dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_BUF_LO(engine, 0)));
Chris Wilson767a9832017-09-13 09:56:05 +0100551 engine->csb_head = -1; /* force mmio read of CSB ptrs */
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100552 }
553
Chris Wilson2e70b8c2017-03-23 13:48:03 +0000554 /* The write will be ordered by the uncached read (itself
555 * a memory barrier), so we do not need another in the form
556 * of a locked instruction. The race between the interrupt
557 * handler and the split test/clear is harmless as we order
558 * our clear before the CSB read. If the interrupt arrived
559 * first between the test and the clear, we read the updated
560 * CSB and clear the bit. If the interrupt arrives as we read
561 * the CSB or later (i.e. after we had cleared the bit) the bit
562 * is set and we do a new loop.
563 */
564 __clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
Chris Wilson767a9832017-09-13 09:56:05 +0100565 if (unlikely(engine->csb_head == -1)) { /* following a reset */
566 head = readl(dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)));
567 tail = GEN8_CSB_WRITE_PTR(head);
568 head = GEN8_CSB_READ_PTR(head);
569 engine->csb_head = head;
570 } else {
571 const int write_idx =
572 intel_hws_csb_write_index(dev_priv) -
573 I915_HWS_CSB_BUF0_INDEX;
574
575 head = engine->csb_head;
576 tail = READ_ONCE(buf[write_idx]);
577 }
Chris Wilson4af0d722017-03-25 20:10:53 +0000578 while (head != tail) {
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100579 struct drm_i915_gem_request *rq;
Chris Wilson4af0d722017-03-25 20:10:53 +0000580 unsigned int status;
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100581 unsigned int count;
Chris Wilsona37951a2017-01-24 11:00:06 +0000582
Chris Wilson4af0d722017-03-25 20:10:53 +0000583 if (++head == GEN8_CSB_ENTRIES)
584 head = 0;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100585
Chris Wilson2ffe80a2017-02-06 17:05:02 +0000586 /* We are flying near dragons again.
587 *
588 * We hold a reference to the request in execlist_port[]
589 * but no more than that. We are operating in softirq
590 * context and so cannot hold any mutex or sleep. That
591 * prevents us stopping the requests we are processing
592 * in port[] from being retired simultaneously (the
593 * breadcrumb will be complete before we see the
594 * context-switch). As we only hold the reference to the
595 * request, any pointer chasing underneath the request
596 * is subject to a potential use-after-free. Thus we
597 * store all of the bookkeeping within port[] as
598 * required, and avoid using unguarded pointers beneath
599 * request itself. The same applies to the atomic
600 * status notifier.
601 */
602
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100603 status = READ_ONCE(buf[2 * head]); /* maybe mmio! */
Chris Wilson70c2a242016-09-09 14:11:46 +0100604 if (!(status & GEN8_CTX_STATUS_COMPLETED_MASK))
605 continue;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100606
Chris Wilson86aa7e72017-01-23 11:31:32 +0000607 /* Check the context/desc id for this event matches */
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100608 GEM_DEBUG_BUG_ON(buf[2 * head + 1] != port->context_id);
Chris Wilson86aa7e72017-01-23 11:31:32 +0000609
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100610 rq = port_unpack(port, &count);
611 GEM_BUG_ON(count == 0);
612 if (--count == 0) {
Chris Wilson70c2a242016-09-09 14:11:46 +0100613 GEM_BUG_ON(status & GEN8_CTX_STATUS_PREEMPTED);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100614 GEM_BUG_ON(!i915_gem_request_completed(rq));
615 execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_OUT);
Thomas Daniele981e7b2014-07-24 17:04:39 +0100616
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100617 trace_i915_gem_request_out(rq);
618 i915_gem_request_put(rq);
619
Chris Wilson70c2a242016-09-09 14:11:46 +0100620 port[0] = port[1];
621 memset(&port[1], 0, sizeof(port[1]));
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100622 } else {
623 port_set(port, port_pack(rq, count));
Chris Wilson70c2a242016-09-09 14:11:46 +0100624 }
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000625
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100626 /* After the final element, the hw should be idle */
627 GEM_BUG_ON(port_count(port) == 0 &&
Chris Wilson70c2a242016-09-09 14:11:46 +0100628 !(status & GEN8_CTX_STATUS_ACTIVE_IDLE));
Chris Wilson4af0d722017-03-25 20:10:53 +0000629 }
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000630
Chris Wilson767a9832017-09-13 09:56:05 +0100631 if (head != engine->csb_head) {
632 engine->csb_head = head;
633 writel(_MASKED_FIELD(GEN8_CSB_READ_PTR_MASK, head << 8),
634 dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)));
635 }
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000636 }
637
Chris Wilson70c2a242016-09-09 14:11:46 +0100638 if (execlists_elsp_ready(engine))
639 execlists_dequeue(engine);
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000640
Chris Wilson70c2a242016-09-09 14:11:46 +0100641 intel_uncore_forcewake_put(dev_priv, engine->fw_domains);
Thomas Daniele981e7b2014-07-24 17:04:39 +0100642}
643
Chris Wilson6c067572017-05-17 13:10:03 +0100644static bool
645insert_request(struct intel_engine_cs *engine,
646 struct i915_priotree *pt,
647 int prio)
Chris Wilson20311bd2016-11-14 20:41:03 +0000648{
Chris Wilson6c067572017-05-17 13:10:03 +0100649 struct i915_priolist *p;
650 struct rb_node **parent, *rb;
Chris Wilson20311bd2016-11-14 20:41:03 +0000651 bool first = true;
652
Chris Wilson6c067572017-05-17 13:10:03 +0100653 if (unlikely(engine->no_priolist))
654 prio = I915_PRIORITY_NORMAL;
655
656find_priolist:
Chris Wilson20311bd2016-11-14 20:41:03 +0000657 /* most positive priority is scheduled first, equal priorities fifo */
658 rb = NULL;
Chris Wilson6c067572017-05-17 13:10:03 +0100659 parent = &engine->execlist_queue.rb_node;
660 while (*parent) {
661 rb = *parent;
662 p = rb_entry(rb, typeof(*p), node);
663 if (prio > p->priority) {
664 parent = &rb->rb_left;
665 } else if (prio < p->priority) {
666 parent = &rb->rb_right;
Chris Wilson20311bd2016-11-14 20:41:03 +0000667 first = false;
Chris Wilson6c067572017-05-17 13:10:03 +0100668 } else {
669 list_add_tail(&pt->link, &p->requests);
670 return false;
Chris Wilson20311bd2016-11-14 20:41:03 +0000671 }
672 }
Chris Wilson6c067572017-05-17 13:10:03 +0100673
674 if (prio == I915_PRIORITY_NORMAL) {
675 p = &engine->default_priolist;
676 } else {
Chris Wilsonc5cf9a92017-05-17 13:10:04 +0100677 p = kmem_cache_alloc(engine->i915->priorities, GFP_ATOMIC);
Chris Wilson6c067572017-05-17 13:10:03 +0100678 /* Convert an allocation failure to a priority bump */
679 if (unlikely(!p)) {
680 prio = I915_PRIORITY_NORMAL; /* recurses just once */
681
682 /* To maintain ordering with all rendering, after an
683 * allocation failure we have to disable all scheduling.
684 * Requests will then be executed in fifo, and schedule
685 * will ensure that dependencies are emitted in fifo.
686 * There will be still some reordering with existing
687 * requests, so if userspace lied about their
688 * dependencies that reordering may be visible.
689 */
690 engine->no_priolist = true;
691 goto find_priolist;
692 }
693 }
694
695 p->priority = prio;
696 rb_link_node(&p->node, rb, parent);
697 rb_insert_color(&p->node, &engine->execlist_queue);
698
699 INIT_LIST_HEAD(&p->requests);
700 list_add_tail(&pt->link, &p->requests);
701
702 if (first)
703 engine->execlist_first = &p->node;
Chris Wilson20311bd2016-11-14 20:41:03 +0000704
705 return first;
706}
707
Chris Wilsonf4ea6bd2016-08-02 22:50:32 +0100708static void execlists_submit_request(struct drm_i915_gem_request *request)
Michel Thierryacdd8842014-07-24 17:04:38 +0100709{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000710 struct intel_engine_cs *engine = request->engine;
Chris Wilson5590af32016-09-09 14:11:54 +0100711 unsigned long flags;
Michel Thierryacdd8842014-07-24 17:04:38 +0100712
Chris Wilson663f71e2016-11-14 20:41:00 +0000713 /* Will be called from irq-context when using foreign fences. */
714 spin_lock_irqsave(&engine->timeline->lock, flags);
Michel Thierryacdd8842014-07-24 17:04:38 +0100715
Chris Wilson6c067572017-05-17 13:10:03 +0100716 if (insert_request(engine,
717 &request->priotree,
718 request->priotree.priority)) {
Chris Wilson48ea2552017-01-24 11:00:08 +0000719 if (execlists_elsp_ready(engine))
Chris Wilson38332812017-01-24 11:00:07 +0000720 tasklet_hi_schedule(&engine->irq_tasklet);
721 }
Michel Thierryacdd8842014-07-24 17:04:38 +0100722
Chris Wilson6c067572017-05-17 13:10:03 +0100723 GEM_BUG_ON(!engine->execlist_first);
724 GEM_BUG_ON(list_empty(&request->priotree.link));
725
Chris Wilson663f71e2016-11-14 20:41:00 +0000726 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Michel Thierryacdd8842014-07-24 17:04:38 +0100727}
728
Chris Wilson20311bd2016-11-14 20:41:03 +0000729static struct intel_engine_cs *
730pt_lock_engine(struct i915_priotree *pt, struct intel_engine_cs *locked)
731{
Chris Wilsona79a5242017-03-27 21:21:43 +0100732 struct intel_engine_cs *engine =
733 container_of(pt, struct drm_i915_gem_request, priotree)->engine;
Chris Wilson20311bd2016-11-14 20:41:03 +0000734
Chris Wilsona79a5242017-03-27 21:21:43 +0100735 GEM_BUG_ON(!locked);
736
Chris Wilson20311bd2016-11-14 20:41:03 +0000737 if (engine != locked) {
Chris Wilsona79a5242017-03-27 21:21:43 +0100738 spin_unlock(&locked->timeline->lock);
739 spin_lock(&engine->timeline->lock);
Chris Wilson20311bd2016-11-14 20:41:03 +0000740 }
741
742 return engine;
743}
744
745static void execlists_schedule(struct drm_i915_gem_request *request, int prio)
746{
Chris Wilsona79a5242017-03-27 21:21:43 +0100747 struct intel_engine_cs *engine;
Chris Wilson20311bd2016-11-14 20:41:03 +0000748 struct i915_dependency *dep, *p;
749 struct i915_dependency stack;
750 LIST_HEAD(dfs);
751
752 if (prio <= READ_ONCE(request->priotree.priority))
753 return;
754
Chris Wilson70cd1472016-11-28 14:36:49 +0000755 /* Need BKL in order to use the temporary link inside i915_dependency */
756 lockdep_assert_held(&request->i915->drm.struct_mutex);
Chris Wilson20311bd2016-11-14 20:41:03 +0000757
758 stack.signaler = &request->priotree;
759 list_add(&stack.dfs_link, &dfs);
760
761 /* Recursively bump all dependent priorities to match the new request.
762 *
763 * A naive approach would be to use recursion:
764 * static void update_priorities(struct i915_priotree *pt, prio) {
765 * list_for_each_entry(dep, &pt->signalers_list, signal_link)
766 * update_priorities(dep->signal, prio)
767 * insert_request(pt);
768 * }
769 * but that may have unlimited recursion depth and so runs a very
770 * real risk of overunning the kernel stack. Instead, we build
771 * a flat list of all dependencies starting with the current request.
772 * As we walk the list of dependencies, we add all of its dependencies
773 * to the end of the list (this may include an already visited
774 * request) and continue to walk onwards onto the new dependencies. The
775 * end result is a topological list of requests in reverse order, the
776 * last element in the list is the request we must execute first.
777 */
778 list_for_each_entry_safe(dep, p, &dfs, dfs_link) {
779 struct i915_priotree *pt = dep->signaler;
780
Chris Wilsona79a5242017-03-27 21:21:43 +0100781 /* Within an engine, there can be no cycle, but we may
782 * refer to the same dependency chain multiple times
783 * (redundant dependencies are not eliminated) and across
784 * engines.
785 */
786 list_for_each_entry(p, &pt->signalers_list, signal_link) {
787 GEM_BUG_ON(p->signaler->priority < pt->priority);
Chris Wilson20311bd2016-11-14 20:41:03 +0000788 if (prio > READ_ONCE(p->signaler->priority))
789 list_move_tail(&p->dfs_link, &dfs);
Chris Wilsona79a5242017-03-27 21:21:43 +0100790 }
Chris Wilson20311bd2016-11-14 20:41:03 +0000791
Chris Wilson0798cff2016-12-05 14:29:41 +0000792 list_safe_reset_next(dep, p, dfs_link);
Chris Wilson20311bd2016-11-14 20:41:03 +0000793 }
794
Chris Wilson349bdb62017-05-17 13:10:05 +0100795 /* If we didn't need to bump any existing priorities, and we haven't
796 * yet submitted this request (i.e. there is no potential race with
797 * execlists_submit_request()), we can set our own priority and skip
798 * acquiring the engine locks.
799 */
800 if (request->priotree.priority == INT_MIN) {
801 GEM_BUG_ON(!list_empty(&request->priotree.link));
802 request->priotree.priority = prio;
803 if (stack.dfs_link.next == stack.dfs_link.prev)
804 return;
805 __list_del_entry(&stack.dfs_link);
806 }
807
Chris Wilsona79a5242017-03-27 21:21:43 +0100808 engine = request->engine;
809 spin_lock_irq(&engine->timeline->lock);
810
Chris Wilson20311bd2016-11-14 20:41:03 +0000811 /* Fifo and depth-first replacement ensure our deps execute before us */
812 list_for_each_entry_safe_reverse(dep, p, &dfs, dfs_link) {
813 struct i915_priotree *pt = dep->signaler;
814
815 INIT_LIST_HEAD(&dep->dfs_link);
816
817 engine = pt_lock_engine(pt, engine);
818
819 if (prio <= pt->priority)
820 continue;
821
Chris Wilson20311bd2016-11-14 20:41:03 +0000822 pt->priority = prio;
Chris Wilson6c067572017-05-17 13:10:03 +0100823 if (!list_empty(&pt->link)) {
824 __list_del_entry(&pt->link);
825 insert_request(engine, pt, prio);
Chris Wilsona79a5242017-03-27 21:21:43 +0100826 }
Chris Wilson20311bd2016-11-14 20:41:03 +0000827 }
828
Chris Wilsona79a5242017-03-27 21:21:43 +0100829 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson20311bd2016-11-14 20:41:03 +0000830
831 /* XXX Do we need to preempt to make room for us and our deps? */
832}
833
Chris Wilson266a2402017-05-04 10:33:08 +0100834static struct intel_ring *
835execlists_context_pin(struct intel_engine_cs *engine,
836 struct i915_gem_context *ctx)
Oscar Mateodcb4c122014-11-13 10:28:10 +0000837{
Chris Wilson9021ad02016-05-24 14:53:37 +0100838 struct intel_context *ce = &ctx->engine[engine->id];
Chris Wilson2947e402016-12-18 15:37:23 +0000839 unsigned int flags;
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +0100840 void *vaddr;
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000841 int ret;
Oscar Mateodcb4c122014-11-13 10:28:10 +0000842
Chris Wilson91c8a322016-07-05 10:40:23 +0100843 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000844
Chris Wilson266a2402017-05-04 10:33:08 +0100845 if (likely(ce->pin_count++))
846 goto out;
Chris Wilsona533b4b2017-03-16 17:16:28 +0000847 GEM_BUG_ON(!ce->pin_count); /* no overflow please! */
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100848
Chris Wilsone8a9c582016-12-18 15:37:20 +0000849 if (!ce->state) {
850 ret = execlists_context_deferred_alloc(ctx, engine);
851 if (ret)
852 goto err;
853 }
Chris Wilson56f6e0a2017-01-05 15:30:20 +0000854 GEM_BUG_ON(!ce->state);
Chris Wilsone8a9c582016-12-18 15:37:20 +0000855
Chris Wilson72b72ae2017-02-10 10:14:22 +0000856 flags = PIN_GLOBAL | PIN_HIGH;
Daniele Ceraolo Spuriofeef2a72016-12-23 15:56:22 -0800857 if (ctx->ggtt_offset_bias)
858 flags |= PIN_OFFSET_BIAS | ctx->ggtt_offset_bias;
Chris Wilson2947e402016-12-18 15:37:23 +0000859
860 ret = i915_vma_pin(ce->state, 0, GEN8_LR_CONTEXT_ALIGN, flags);
Nick Hoathe84fe802015-09-11 12:53:46 +0100861 if (ret)
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100862 goto err;
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000863
Chris Wilsonbf3783e2016-08-15 10:48:54 +0100864 vaddr = i915_gem_object_pin_map(ce->state->obj, I915_MAP_WB);
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +0100865 if (IS_ERR(vaddr)) {
866 ret = PTR_ERR(vaddr);
Chris Wilsonbf3783e2016-08-15 10:48:54 +0100867 goto unpin_vma;
Tvrtko Ursulin82352e92016-01-15 17:12:45 +0000868 }
869
Chris Wilsond822bb12017-04-03 12:34:25 +0100870 ret = intel_ring_pin(ce->ring, ctx->i915, ctx->ggtt_offset_bias);
Nick Hoathe84fe802015-09-11 12:53:46 +0100871 if (ret)
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +0100872 goto unpin_map;
Alex Daid1675192015-08-12 15:43:43 +0100873
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000874 intel_lr_context_descriptor_update(ctx, engine);
Chris Wilson9021ad02016-05-24 14:53:37 +0100875
Chris Wilsona3aabe82016-10-04 21:11:26 +0100876 ce->lrc_reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
877 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100878 i915_ggtt_offset(ce->ring->vma);
Chris Wilsona3aabe82016-10-04 21:11:26 +0100879
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100880 ce->state->obj->mm.dirty = true;
Daniel Vettere93c28f2015-09-02 14:33:42 +0200881
Chris Wilson9a6feaf2016-07-20 13:31:50 +0100882 i915_gem_context_get(ctx);
Chris Wilson266a2402017-05-04 10:33:08 +0100883out:
884 return ce->ring;
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000885
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +0100886unpin_map:
Chris Wilsonbf3783e2016-08-15 10:48:54 +0100887 i915_gem_object_unpin_map(ce->state->obj);
888unpin_vma:
889 __i915_vma_unpin(ce->state);
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100890err:
Chris Wilson9021ad02016-05-24 14:53:37 +0100891 ce->pin_count = 0;
Chris Wilson266a2402017-05-04 10:33:08 +0100892 return ERR_PTR(ret);
Oscar Mateodcb4c122014-11-13 10:28:10 +0000893}
894
Chris Wilsone8a9c582016-12-18 15:37:20 +0000895static void execlists_context_unpin(struct intel_engine_cs *engine,
896 struct i915_gem_context *ctx)
Oscar Mateodcb4c122014-11-13 10:28:10 +0000897{
Chris Wilson9021ad02016-05-24 14:53:37 +0100898 struct intel_context *ce = &ctx->engine[engine->id];
Daniel Vetteraf3302b2015-12-04 17:27:15 +0100899
Chris Wilson91c8a322016-07-05 10:40:23 +0100900 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilson9021ad02016-05-24 14:53:37 +0100901 GEM_BUG_ON(ce->pin_count == 0);
Tvrtko Ursulin321fe302016-01-28 10:29:55 +0000902
Chris Wilson9021ad02016-05-24 14:53:37 +0100903 if (--ce->pin_count)
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100904 return;
905
Chris Wilsonaad29fb2016-08-02 22:50:23 +0100906 intel_ring_unpin(ce->ring);
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100907
Chris Wilsonbf3783e2016-08-15 10:48:54 +0100908 i915_gem_object_unpin_map(ce->state->obj);
909 i915_vma_unpin(ce->state);
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100910
Chris Wilson9a6feaf2016-07-20 13:31:50 +0100911 i915_gem_context_put(ctx);
Oscar Mateodcb4c122014-11-13 10:28:10 +0000912}
913
Chris Wilsonf73e7392016-12-18 15:37:24 +0000914static int execlists_request_alloc(struct drm_i915_gem_request *request)
Chris Wilsonef11c012016-12-18 15:37:19 +0000915{
916 struct intel_engine_cs *engine = request->engine;
917 struct intel_context *ce = &request->ctx->engine[engine->id];
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000918 u32 *cs;
Chris Wilsonef11c012016-12-18 15:37:19 +0000919 int ret;
920
Chris Wilsone8a9c582016-12-18 15:37:20 +0000921 GEM_BUG_ON(!ce->pin_count);
922
Chris Wilsonef11c012016-12-18 15:37:19 +0000923 /* Flush enough space to reduce the likelihood of waiting after
924 * we start building the request - in which case we will just
925 * have to repeat work.
926 */
927 request->reserved_space += EXECLISTS_REQUEST_SIZE;
928
Chris Wilsonef11c012016-12-18 15:37:19 +0000929 if (i915.enable_guc_submission) {
930 /*
931 * Check that the GuC has space for the request before
932 * going any further, as the i915_add_request() call
933 * later on mustn't fail ...
934 */
935 ret = i915_guc_wq_reserve(request);
936 if (ret)
Chris Wilsone8a9c582016-12-18 15:37:20 +0000937 goto err;
Chris Wilsonef11c012016-12-18 15:37:19 +0000938 }
939
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000940 cs = intel_ring_begin(request, 0);
941 if (IS_ERR(cs)) {
942 ret = PTR_ERR(cs);
Chris Wilsonef11c012016-12-18 15:37:19 +0000943 goto err_unreserve;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000944 }
Chris Wilsonef11c012016-12-18 15:37:19 +0000945
946 if (!ce->initialised) {
947 ret = engine->init_context(request);
948 if (ret)
949 goto err_unreserve;
950
951 ce->initialised = true;
952 }
953
954 /* Note that after this point, we have committed to using
955 * this request as it is being used to both track the
956 * state of engine initialisation and liveness of the
957 * golden renderstate above. Think twice before you try
958 * to cancel/unwind this request now.
959 */
960
961 request->reserved_space -= EXECLISTS_REQUEST_SIZE;
962 return 0;
963
964err_unreserve:
965 if (i915.enable_guc_submission)
966 i915_guc_wq_unreserve(request);
Chris Wilsone8a9c582016-12-18 15:37:20 +0000967err:
Chris Wilsonef11c012016-12-18 15:37:19 +0000968 return ret;
969}
970
Arun Siluvery9e000842015-07-03 14:27:31 +0100971/*
972 * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
973 * PIPE_CONTROL instruction. This is required for the flush to happen correctly
974 * but there is a slight complication as this is applied in WA batch where the
975 * values are only initialized once so we cannot take register value at the
976 * beginning and reuse it further; hence we save its value to memory, upload a
977 * constant value with bit21 set and then we restore it back with the saved value.
978 * To simplify the WA, a constant value is formed by using the default value
979 * of this register. This shouldn't be a problem because we are only modifying
980 * it for a short period and this batch in non-premptible. We can ofcourse
981 * use additional instructions that read the actual value of the register
982 * at that time and set our bit of interest but it makes the WA complicated.
983 *
984 * This WA is also required for Gen9 so extracting as a function avoids
985 * code duplication.
986 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000987static u32 *
988gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery9e000842015-07-03 14:27:31 +0100989{
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000990 *batch++ = MI_STORE_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
991 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
992 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
993 *batch++ = 0;
Arun Siluvery9e000842015-07-03 14:27:31 +0100994
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000995 *batch++ = MI_LOAD_REGISTER_IMM(1);
996 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
997 *batch++ = 0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES;
Arun Siluvery9e000842015-07-03 14:27:31 +0100998
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000999 batch = gen8_emit_pipe_control(batch,
1000 PIPE_CONTROL_CS_STALL |
1001 PIPE_CONTROL_DC_FLUSH_ENABLE,
1002 0);
Arun Siluvery9e000842015-07-03 14:27:31 +01001003
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001004 *batch++ = MI_LOAD_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1005 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1006 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
1007 *batch++ = 0;
Arun Siluvery9e000842015-07-03 14:27:31 +01001008
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001009 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001010}
1011
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001012/*
1013 * Typically we only have one indirect_ctx and per_ctx batch buffer which are
1014 * initialized at the beginning and shared across all contexts but this field
1015 * helps us to have multiple batches at different offsets and select them based
1016 * on a criteria. At the moment this batch always start at the beginning of the page
1017 * and at this point we don't have multiple wa_ctx batch buffers.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001018 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001019 * The number of WA applied are not known at the beginning; we use this field
1020 * to return the no of DWORDS written.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001021 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001022 * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
1023 * so it adds NOOPs as padding to make it cacheline aligned.
1024 * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
1025 * makes a complete batch buffer.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001026 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001027static u32 *gen8_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001028{
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001029 /* WaDisableCtxRestoreArbitration:bdw,chv */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001030 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001031
Arun Siluveryc82435b2015-06-19 18:37:13 +01001032 /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001033 if (IS_BROADWELL(engine->i915))
1034 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
Arun Siluveryc82435b2015-06-19 18:37:13 +01001035
Arun Siluvery0160f052015-06-23 15:46:57 +01001036 /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
1037 /* Actual scratch location is at 128 bytes offset */
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001038 batch = gen8_emit_pipe_control(batch,
1039 PIPE_CONTROL_FLUSH_L3 |
1040 PIPE_CONTROL_GLOBAL_GTT_IVB |
1041 PIPE_CONTROL_CS_STALL |
1042 PIPE_CONTROL_QW_WRITE,
1043 i915_ggtt_offset(engine->scratch) +
1044 2 * CACHELINE_BYTES);
Arun Siluvery0160f052015-06-23 15:46:57 +01001045
Arun Siluvery17ee9502015-06-19 19:07:01 +01001046 /* Pad to end of cacheline */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001047 while ((unsigned long)batch % CACHELINE_BYTES)
1048 *batch++ = MI_NOOP;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001049
1050 /*
1051 * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
1052 * execution depends on the length specified in terms of cache lines
1053 * in the register CTX_RCS_INDIRECT_CTX
1054 */
1055
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001056 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001057}
1058
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001059/*
1060 * This batch is started immediately after indirect_ctx batch. Since we ensure
1061 * that indirect_ctx ends on a cacheline this batch is aligned automatically.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001062 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001063 * The number of DWORDS written are returned using this field.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001064 *
1065 * This batch is terminated with MI_BATCH_BUFFER_END and so we need not add padding
1066 * to align it with cacheline as padding after MI_BATCH_BUFFER_END is redundant.
1067 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001068static u32 *gen8_init_perctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001069{
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001070 /* WaDisableCtxRestoreArbitration:bdw,chv */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001071 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1072 *batch++ = MI_BATCH_BUFFER_END;
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001073
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001074 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001075}
1076
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001077static u32 *gen9_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery0504cff2015-07-14 15:01:27 +01001078{
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001079 /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt,glk */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001080 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
Arun Siluverya4106a72015-07-14 15:01:29 +01001081
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001082 /* WaDisableGatherAtSetShaderCommonSlice:skl,bxt,kbl,glk */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001083 *batch++ = MI_LOAD_REGISTER_IMM(1);
1084 *batch++ = i915_mmio_reg_offset(COMMON_SLICE_CHICKEN2);
1085 *batch++ = _MASKED_BIT_DISABLE(
1086 GEN9_DISABLE_GATHER_AT_SET_SHADER_COMMON_SLICE);
1087 *batch++ = MI_NOOP;
Mika Kuoppala873e8172016-07-20 14:26:13 +03001088
Mika Kuoppala066d4622016-06-07 17:19:15 +03001089 /* WaClearSlmSpaceAtContextSwitch:kbl */
1090 /* Actual scratch location is at 128 bytes offset */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001091 if (IS_KBL_REVID(engine->i915, 0, KBL_REVID_A0)) {
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001092 batch = gen8_emit_pipe_control(batch,
1093 PIPE_CONTROL_FLUSH_L3 |
1094 PIPE_CONTROL_GLOBAL_GTT_IVB |
1095 PIPE_CONTROL_CS_STALL |
1096 PIPE_CONTROL_QW_WRITE,
1097 i915_ggtt_offset(engine->scratch)
1098 + 2 * CACHELINE_BYTES);
Mika Kuoppala066d4622016-06-07 17:19:15 +03001099 }
Tim Gore3485d992016-07-05 10:01:30 +01001100
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001101 /* WaMediaPoolStateCmdInWABB:bxt,glk */
Tim Gore3485d992016-07-05 10:01:30 +01001102 if (HAS_POOLED_EU(engine->i915)) {
1103 /*
1104 * EU pool configuration is setup along with golden context
1105 * during context initialization. This value depends on
1106 * device type (2x6 or 3x6) and needs to be updated based
1107 * on which subslice is disabled especially for 2x6
1108 * devices, however it is safe to load default
1109 * configuration of 3x6 device instead of masking off
1110 * corresponding bits because HW ignores bits of a disabled
1111 * subslice and drops down to appropriate config. Please
1112 * see render_state_setup() in i915_gem_render_state.c for
1113 * possible configurations, to avoid duplication they are
1114 * not shown here again.
1115 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001116 *batch++ = GEN9_MEDIA_POOL_STATE;
1117 *batch++ = GEN9_MEDIA_POOL_ENABLE;
1118 *batch++ = 0x00777000;
1119 *batch++ = 0;
1120 *batch++ = 0;
1121 *batch++ = 0;
Tim Gore3485d992016-07-05 10:01:30 +01001122 }
1123
Arun Siluvery0504cff2015-07-14 15:01:27 +01001124 /* Pad to end of cacheline */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001125 while ((unsigned long)batch % CACHELINE_BYTES)
1126 *batch++ = MI_NOOP;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001127
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001128 return batch;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001129}
1130
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001131static u32 *gen9_init_perctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery0504cff2015-07-14 15:01:27 +01001132{
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001133 *batch++ = MI_BATCH_BUFFER_END;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001134
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001135 return batch;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001136}
1137
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001138#define CTX_WA_BB_OBJ_SIZE (PAGE_SIZE)
1139
1140static int lrc_setup_wa_ctx(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001141{
Chris Wilson48bb74e2016-08-15 10:49:04 +01001142 struct drm_i915_gem_object *obj;
1143 struct i915_vma *vma;
1144 int err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001145
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001146 obj = i915_gem_object_create(engine->i915, CTX_WA_BB_OBJ_SIZE);
Chris Wilson48bb74e2016-08-15 10:49:04 +01001147 if (IS_ERR(obj))
1148 return PTR_ERR(obj);
1149
Chris Wilsona01cb37a2017-01-16 15:21:30 +00001150 vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
Chris Wilson48bb74e2016-08-15 10:49:04 +01001151 if (IS_ERR(vma)) {
1152 err = PTR_ERR(vma);
1153 goto err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001154 }
1155
Chris Wilson48bb74e2016-08-15 10:49:04 +01001156 err = i915_vma_pin(vma, 0, PAGE_SIZE, PIN_GLOBAL | PIN_HIGH);
1157 if (err)
1158 goto err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001159
Chris Wilson48bb74e2016-08-15 10:49:04 +01001160 engine->wa_ctx.vma = vma;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001161 return 0;
Chris Wilson48bb74e2016-08-15 10:49:04 +01001162
1163err:
1164 i915_gem_object_put(obj);
1165 return err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001166}
1167
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001168static void lrc_destroy_wa_ctx(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001169{
Chris Wilson19880c42016-08-15 10:49:05 +01001170 i915_vma_unpin_and_release(&engine->wa_ctx.vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001171}
1172
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001173typedef u32 *(*wa_bb_func_t)(struct intel_engine_cs *engine, u32 *batch);
1174
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001175static int intel_init_workaround_bb(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001176{
Chris Wilson48bb74e2016-08-15 10:49:04 +01001177 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001178 struct i915_wa_ctx_bb *wa_bb[2] = { &wa_ctx->indirect_ctx,
1179 &wa_ctx->per_ctx };
1180 wa_bb_func_t wa_bb_fn[2];
Arun Siluvery17ee9502015-06-19 19:07:01 +01001181 struct page *page;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001182 void *batch, *batch_ptr;
1183 unsigned int i;
Chris Wilson48bb74e2016-08-15 10:49:04 +01001184 int ret;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001185
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001186 if (WARN_ON(engine->id != RCS || !engine->scratch))
1187 return -EINVAL;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001188
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001189 switch (INTEL_GEN(engine->i915)) {
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001190 case 10:
1191 return 0;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001192 case 9:
1193 wa_bb_fn[0] = gen9_init_indirectctx_bb;
1194 wa_bb_fn[1] = gen9_init_perctx_bb;
1195 break;
1196 case 8:
1197 wa_bb_fn[0] = gen8_init_indirectctx_bb;
1198 wa_bb_fn[1] = gen8_init_perctx_bb;
1199 break;
1200 default:
1201 MISSING_CASE(INTEL_GEN(engine->i915));
Arun Siluvery5e60d792015-06-23 15:50:44 +01001202 return 0;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001203 }
Arun Siluvery5e60d792015-06-23 15:50:44 +01001204
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001205 ret = lrc_setup_wa_ctx(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001206 if (ret) {
1207 DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
1208 return ret;
1209 }
1210
Chris Wilson48bb74e2016-08-15 10:49:04 +01001211 page = i915_gem_object_get_dirty_page(wa_ctx->vma->obj, 0);
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001212 batch = batch_ptr = kmap_atomic(page);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001213
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001214 /*
1215 * Emit the two workaround batch buffers, recording the offset from the
1216 * start of the workaround batch buffer object for each and their
1217 * respective sizes.
1218 */
1219 for (i = 0; i < ARRAY_SIZE(wa_bb_fn); i++) {
1220 wa_bb[i]->offset = batch_ptr - batch;
1221 if (WARN_ON(!IS_ALIGNED(wa_bb[i]->offset, CACHELINE_BYTES))) {
1222 ret = -EINVAL;
1223 break;
1224 }
1225 batch_ptr = wa_bb_fn[i](engine, batch_ptr);
1226 wa_bb[i]->size = batch_ptr - (batch + wa_bb[i]->offset);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001227 }
1228
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001229 BUG_ON(batch_ptr - batch > CTX_WA_BB_OBJ_SIZE);
1230
Arun Siluvery17ee9502015-06-19 19:07:01 +01001231 kunmap_atomic(batch);
1232 if (ret)
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001233 lrc_destroy_wa_ctx(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001234
1235 return ret;
1236}
1237
Chris Wilson64f09f02017-08-07 13:19:19 +01001238static u8 gtiir[] = {
1239 [RCS] = 0,
1240 [BCS] = 0,
1241 [VCS] = 1,
1242 [VCS2] = 1,
1243 [VECS] = 3,
1244};
1245
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001246static int gen8_init_common_ring(struct intel_engine_cs *engine)
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001247{
Chris Wilsonc0336662016-05-06 15:40:21 +01001248 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson6b764a52017-04-25 11:38:35 +01001249 struct execlist_port *port = engine->execlist_port;
1250 unsigned int n;
Chris Wilson77f0d0e2017-05-17 13:10:00 +01001251 bool submit;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001252 int ret;
1253
1254 ret = intel_mocs_init_engine(engine);
1255 if (ret)
1256 return ret;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001257
Chris Wilsonad07dfc2016-10-07 07:53:26 +01001258 intel_engine_reset_breadcrumbs(engine);
Chris Wilsonf3b8f912017-01-05 15:30:21 +00001259 intel_engine_init_hangcheck(engine);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001260
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001261 I915_WRITE(RING_HWSTAM(engine->mmio_base), 0xffffffff);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001262 I915_WRITE(RING_MODE_GEN7(engine),
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001263 _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
Chris Wilsonf3b8f912017-01-05 15:30:21 +00001264 I915_WRITE(RING_HWS_PGA(engine->mmio_base),
1265 engine->status_page.ggtt_offset);
1266 POSTING_READ(RING_HWS_PGA(engine->mmio_base));
Michel Thierrydfc53c52015-09-28 13:25:12 +01001267
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001268 DRM_DEBUG_DRIVER("Execlists enabled for %s\n", engine->name);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001269
Chris Wilson64f09f02017-08-07 13:19:19 +01001270 GEM_BUG_ON(engine->id >= ARRAY_SIZE(gtiir));
1271
1272 /*
1273 * Clear any pending interrupt state.
1274 *
1275 * We do it twice out of paranoia that some of the IIR are double
1276 * buffered, and if we only reset it once there may still be
1277 * an interrupt pending.
1278 */
1279 I915_WRITE(GEN8_GT_IIR(gtiir[engine->id]),
1280 GT_CONTEXT_SWITCH_INTERRUPT << engine->irq_shift);
1281 I915_WRITE(GEN8_GT_IIR(gtiir[engine->id]),
1282 GT_CONTEXT_SWITCH_INTERRUPT << engine->irq_shift);
Chris Wilsonf7470262017-01-24 15:20:21 +00001283 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
Chris Wilson767a9832017-09-13 09:56:05 +01001284 engine->csb_head = -1;
Chris Wilson6b764a52017-04-25 11:38:35 +01001285
Chris Wilson64f09f02017-08-07 13:19:19 +01001286 /* After a GPU reset, we may have requests to replay */
Chris Wilson77f0d0e2017-05-17 13:10:00 +01001287 submit = false;
Chris Wilson6b764a52017-04-25 11:38:35 +01001288 for (n = 0; n < ARRAY_SIZE(engine->execlist_port); n++) {
Chris Wilson77f0d0e2017-05-17 13:10:00 +01001289 if (!port_isset(&port[n]))
Chris Wilson6b764a52017-04-25 11:38:35 +01001290 break;
1291
1292 DRM_DEBUG_DRIVER("Restarting %s:%d from 0x%x\n",
1293 engine->name, n,
Chris Wilson77f0d0e2017-05-17 13:10:00 +01001294 port_request(&port[n])->global_seqno);
Chris Wilson6b764a52017-04-25 11:38:35 +01001295
1296 /* Discard the current inflight count */
Chris Wilson77f0d0e2017-05-17 13:10:00 +01001297 port_set(&port[n], port_request(&port[n]));
1298 submit = true;
Chris Wilsonc87d50c2016-10-04 21:11:27 +01001299 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01001300
Chris Wilson77f0d0e2017-05-17 13:10:00 +01001301 if (submit && !i915.enable_guc_submission)
Chris Wilson6b764a52017-04-25 11:38:35 +01001302 execlists_submit_ports(engine);
1303
Chris Wilson821ed7d2016-09-09 14:11:53 +01001304 return 0;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001305}
1306
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001307static int gen8_init_render_ring(struct intel_engine_cs *engine)
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001308{
Chris Wilsonc0336662016-05-06 15:40:21 +01001309 struct drm_i915_private *dev_priv = engine->i915;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001310 int ret;
1311
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001312 ret = gen8_init_common_ring(engine);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001313 if (ret)
1314 return ret;
1315
1316 /* We need to disable the AsyncFlip performance optimisations in order
1317 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1318 * programmed to '1' on all products.
1319 *
1320 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
1321 */
1322 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1323
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001324 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1325
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001326 return init_workarounds_ring(engine);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001327}
1328
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001329static int gen9_init_render_ring(struct intel_engine_cs *engine)
Damien Lespiau82ef8222015-02-09 19:33:08 +00001330{
1331 int ret;
1332
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001333 ret = gen8_init_common_ring(engine);
Damien Lespiau82ef8222015-02-09 19:33:08 +00001334 if (ret)
1335 return ret;
1336
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001337 return init_workarounds_ring(engine);
Damien Lespiau82ef8222015-02-09 19:33:08 +00001338}
1339
Chris Wilson821ed7d2016-09-09 14:11:53 +01001340static void reset_common_ring(struct intel_engine_cs *engine,
1341 struct drm_i915_gem_request *request)
1342{
Chris Wilson821ed7d2016-09-09 14:11:53 +01001343 struct execlist_port *port = engine->execlist_port;
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001344 struct intel_context *ce;
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001345 unsigned int n;
1346
1347 /*
1348 * Catch up with any missed context-switch interrupts.
1349 *
1350 * Ideally we would just read the remaining CSB entries now that we
1351 * know the gpu is idle. However, the CSB registers are sometimes^W
1352 * often trashed across a GPU reset! Instead we have to rely on
1353 * guessing the missed context-switch events by looking at what
1354 * requests were completed.
1355 */
1356 if (!request) {
1357 for (n = 0; n < ARRAY_SIZE(engine->execlist_port); n++)
1358 i915_gem_request_put(port_request(&port[n]));
1359 memset(engine->execlist_port, 0, sizeof(engine->execlist_port));
1360 return;
1361 }
1362
1363 if (request->ctx != port_request(port)->ctx) {
1364 i915_gem_request_put(port_request(port));
1365 port[0] = port[1];
1366 memset(&port[1], 0, sizeof(port[1]));
1367 }
1368
1369 GEM_BUG_ON(request->ctx != port_request(port)->ctx);
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001370
1371 /* If the request was innocent, we leave the request in the ELSP
1372 * and will try to replay it on restarting. The context image may
1373 * have been corrupted by the reset, in which case we may have
1374 * to service a new GPU hang, but more likely we can continue on
1375 * without impact.
1376 *
1377 * If the request was guilty, we presume the context is corrupt
1378 * and have to at least restore the RING register in the context
1379 * image back to the expected values to skip over the guilty request.
1380 */
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001381 if (request->fence.error != -EIO)
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001382 return;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001383
Chris Wilsona3aabe82016-10-04 21:11:26 +01001384 /* We want a simple context + ring to execute the breadcrumb update.
1385 * We cannot rely on the context being intact across the GPU hang,
1386 * so clear it and rebuild just what we need for the breadcrumb.
1387 * All pending requests for this context will be zapped, and any
1388 * future request will be after userspace has had the opportunity
1389 * to recreate its own state.
1390 */
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001391 ce = &request->ctx->engine[engine->id];
Chris Wilsona3aabe82016-10-04 21:11:26 +01001392 execlists_init_reg_state(ce->lrc_reg_state,
1393 request->ctx, engine, ce->ring);
1394
Chris Wilson821ed7d2016-09-09 14:11:53 +01001395 /* Move the RING_HEAD onto the breadcrumb, past the hanging batch */
Chris Wilsona3aabe82016-10-04 21:11:26 +01001396 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
1397 i915_ggtt_offset(ce->ring->vma);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001398 ce->lrc_reg_state[CTX_RING_HEAD+1] = request->postfix;
Chris Wilsona3aabe82016-10-04 21:11:26 +01001399
Chris Wilson821ed7d2016-09-09 14:11:53 +01001400 request->ring->head = request->postfix;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001401 intel_ring_update_space(request->ring);
1402
Chris Wilsona3aabe82016-10-04 21:11:26 +01001403 /* Reset WaIdleLiteRestore:bdw,skl as well */
Chris Wilson450362d2017-03-27 14:00:07 +01001404 request->tail =
1405 intel_ring_wrap(request->ring,
1406 request->wa_tail - WA_TAIL_DWORDS*sizeof(u32));
Chris Wilsoned1501d2017-03-27 14:14:12 +01001407 assert_ring_tail_valid(request->ring, request->tail);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001408}
1409
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001410static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request *req)
1411{
1412 struct i915_hw_ppgtt *ppgtt = req->ctx->ppgtt;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001413 struct intel_engine_cs *engine = req->engine;
Mika Kuoppalae7167762017-02-28 17:28:10 +02001414 const int num_lri_cmds = GEN8_3LVL_PDPES * 2;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001415 u32 *cs;
1416 int i;
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001417
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001418 cs = intel_ring_begin(req, num_lri_cmds * 2 + 2);
1419 if (IS_ERR(cs))
1420 return PTR_ERR(cs);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001421
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001422 *cs++ = MI_LOAD_REGISTER_IMM(num_lri_cmds);
Mika Kuoppalae7167762017-02-28 17:28:10 +02001423 for (i = GEN8_3LVL_PDPES - 1; i >= 0; i--) {
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001424 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
1425
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001426 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, i));
1427 *cs++ = upper_32_bits(pd_daddr);
1428 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, i));
1429 *cs++ = lower_32_bits(pd_daddr);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001430 }
1431
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001432 *cs++ = MI_NOOP;
1433 intel_ring_advance(req, cs);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001434
1435 return 0;
1436}
1437
John Harrisonbe795fc2015-05-29 17:44:03 +01001438static int gen8_emit_bb_start(struct drm_i915_gem_request *req,
Chris Wilson803688b2016-08-02 22:50:27 +01001439 u64 offset, u32 len,
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001440 const unsigned int flags)
Oscar Mateo15648582014-07-24 17:04:32 +01001441{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001442 u32 *cs;
Oscar Mateo15648582014-07-24 17:04:32 +01001443 int ret;
1444
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001445 /* Don't rely in hw updating PDPs, specially in lite-restore.
1446 * Ideally, we should set Force PD Restore in ctx descriptor,
1447 * but we can't. Force Restore would be a second option, but
1448 * it is unsafe in case of lite-restore (because the ctx is
Michel Thierry2dba3232015-07-30 11:06:23 +01001449 * not idle). PML4 is allocated during ppgtt init so this is
1450 * not needed in 48-bit.*/
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001451 if (req->ctx->ppgtt &&
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001452 (intel_engine_flag(req->engine) & req->ctx->ppgtt->pd_dirty_rings) &&
1453 !i915_vm_is_48bit(&req->ctx->ppgtt->base) &&
1454 !intel_vgpu_active(req->i915)) {
1455 ret = intel_logical_ring_emit_pdps(req);
1456 if (ret)
1457 return ret;
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001458
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00001459 req->ctx->ppgtt->pd_dirty_rings &= ~intel_engine_flag(req->engine);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001460 }
1461
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001462 cs = intel_ring_begin(req, 4);
1463 if (IS_ERR(cs))
1464 return PTR_ERR(cs);
Oscar Mateo15648582014-07-24 17:04:32 +01001465
1466 /* FIXME(BDW): Address space and security selectors. */
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001467 *cs++ = MI_BATCH_BUFFER_START_GEN8 |
1468 (flags & I915_DISPATCH_SECURE ? 0 : BIT(8)) |
1469 (flags & I915_DISPATCH_RS ? MI_BATCH_RESOURCE_STREAMER : 0);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001470 *cs++ = lower_32_bits(offset);
1471 *cs++ = upper_32_bits(offset);
1472 *cs++ = MI_NOOP;
1473 intel_ring_advance(req, cs);
Oscar Mateo15648582014-07-24 17:04:32 +01001474
1475 return 0;
1476}
1477
Chris Wilson31bb59c2016-07-01 17:23:27 +01001478static void gen8_logical_ring_enable_irq(struct intel_engine_cs *engine)
Oscar Mateo73d477f2014-07-24 17:04:31 +01001479{
Chris Wilsonc0336662016-05-06 15:40:21 +01001480 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson31bb59c2016-07-01 17:23:27 +01001481 I915_WRITE_IMR(engine,
1482 ~(engine->irq_enable_mask | engine->irq_keep_mask));
1483 POSTING_READ_FW(RING_IMR(engine->mmio_base));
Oscar Mateo73d477f2014-07-24 17:04:31 +01001484}
1485
Chris Wilson31bb59c2016-07-01 17:23:27 +01001486static void gen8_logical_ring_disable_irq(struct intel_engine_cs *engine)
Oscar Mateo73d477f2014-07-24 17:04:31 +01001487{
Chris Wilsonc0336662016-05-06 15:40:21 +01001488 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson31bb59c2016-07-01 17:23:27 +01001489 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Oscar Mateo73d477f2014-07-24 17:04:31 +01001490}
1491
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001492static int gen8_emit_flush(struct drm_i915_gem_request *request, u32 mode)
Oscar Mateo47122742014-07-24 17:04:28 +01001493{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001494 u32 cmd, *cs;
Oscar Mateo47122742014-07-24 17:04:28 +01001495
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001496 cs = intel_ring_begin(request, 4);
1497 if (IS_ERR(cs))
1498 return PTR_ERR(cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001499
1500 cmd = MI_FLUSH_DW + 1;
1501
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001502 /* We always require a command barrier so that subsequent
1503 * commands, such as breadcrumb interrupts, are strictly ordered
1504 * wrt the contents of the write cache being flushed to memory
1505 * (and thus being coherent from the CPU).
1506 */
1507 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1508
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001509 if (mode & EMIT_INVALIDATE) {
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001510 cmd |= MI_INVALIDATE_TLB;
Chris Wilson1dae2df2016-08-02 22:50:19 +01001511 if (request->engine->id == VCS)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001512 cmd |= MI_INVALIDATE_BSD;
Oscar Mateo47122742014-07-24 17:04:28 +01001513 }
1514
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001515 *cs++ = cmd;
1516 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
1517 *cs++ = 0; /* upper addr */
1518 *cs++ = 0; /* value */
1519 intel_ring_advance(request, cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001520
1521 return 0;
1522}
1523
John Harrison7deb4d32015-05-29 17:43:59 +01001524static int gen8_emit_flush_render(struct drm_i915_gem_request *request,
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001525 u32 mode)
Oscar Mateo47122742014-07-24 17:04:28 +01001526{
Chris Wilsonb5321f32016-08-02 22:50:18 +01001527 struct intel_engine_cs *engine = request->engine;
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001528 u32 scratch_addr =
1529 i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001530 bool vf_flush_wa = false, dc_flush_wa = false;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001531 u32 *cs, flags = 0;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001532 int len;
Oscar Mateo47122742014-07-24 17:04:28 +01001533
1534 flags |= PIPE_CONTROL_CS_STALL;
1535
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001536 if (mode & EMIT_FLUSH) {
Oscar Mateo47122742014-07-24 17:04:28 +01001537 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
1538 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -08001539 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +01001540 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Oscar Mateo47122742014-07-24 17:04:28 +01001541 }
1542
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001543 if (mode & EMIT_INVALIDATE) {
Oscar Mateo47122742014-07-24 17:04:28 +01001544 flags |= PIPE_CONTROL_TLB_INVALIDATE;
1545 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
1546 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
1547 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
1548 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
1549 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
1550 flags |= PIPE_CONTROL_QW_WRITE;
1551 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Oscar Mateo47122742014-07-24 17:04:28 +01001552
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001553 /*
1554 * On GEN9: before VF_CACHE_INVALIDATE we need to emit a NULL
1555 * pipe control.
1556 */
Chris Wilsonc0336662016-05-06 15:40:21 +01001557 if (IS_GEN9(request->i915))
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001558 vf_flush_wa = true;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001559
1560 /* WaForGAMHang:kbl */
1561 if (IS_KBL_REVID(request->i915, 0, KBL_REVID_B0))
1562 dc_flush_wa = true;
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001563 }
Imre Deak9647ff32015-01-25 13:27:11 -08001564
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001565 len = 6;
1566
1567 if (vf_flush_wa)
1568 len += 6;
1569
1570 if (dc_flush_wa)
1571 len += 12;
1572
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001573 cs = intel_ring_begin(request, len);
1574 if (IS_ERR(cs))
1575 return PTR_ERR(cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001576
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001577 if (vf_flush_wa)
1578 cs = gen8_emit_pipe_control(cs, 0, 0);
Imre Deak9647ff32015-01-25 13:27:11 -08001579
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001580 if (dc_flush_wa)
1581 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_DC_FLUSH_ENABLE,
1582 0);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001583
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001584 cs = gen8_emit_pipe_control(cs, flags, scratch_addr);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001585
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001586 if (dc_flush_wa)
1587 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_CS_STALL, 0);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001588
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001589 intel_ring_advance(request, cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001590
1591 return 0;
1592}
1593
Chris Wilson7c17d372016-01-20 15:43:35 +02001594/*
1595 * Reserve space for 2 NOOPs at the end of each request to be
1596 * used as a workaround for not being allowed to do lite
1597 * restore with HEAD==TAIL (WaIdleLiteRestore).
1598 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001599static void gen8_emit_wa_tail(struct drm_i915_gem_request *request, u32 *cs)
Oscar Mateo4da46e12014-07-24 17:04:27 +01001600{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001601 *cs++ = MI_NOOP;
1602 *cs++ = MI_NOOP;
1603 request->wa_tail = intel_ring_offset(request, cs);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001604}
Oscar Mateo4da46e12014-07-24 17:04:27 +01001605
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001606static void gen8_emit_breadcrumb(struct drm_i915_gem_request *request, u32 *cs)
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001607{
Chris Wilson7c17d372016-01-20 15:43:35 +02001608 /* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */
1609 BUILD_BUG_ON(I915_GEM_HWS_INDEX_ADDR & (1 << 5));
Oscar Mateo4da46e12014-07-24 17:04:27 +01001610
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001611 *cs++ = (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW;
1612 *cs++ = intel_hws_seqno_address(request->engine) | MI_FLUSH_DW_USE_GTT;
1613 *cs++ = 0;
1614 *cs++ = request->global_seqno;
1615 *cs++ = MI_USER_INTERRUPT;
1616 *cs++ = MI_NOOP;
1617 request->tail = intel_ring_offset(request, cs);
Chris Wilsoned1501d2017-03-27 14:14:12 +01001618 assert_ring_tail_valid(request->ring, request->tail);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001619
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001620 gen8_emit_wa_tail(request, cs);
Chris Wilson7c17d372016-01-20 15:43:35 +02001621}
Oscar Mateo4da46e12014-07-24 17:04:27 +01001622
Chris Wilson98f29e82016-10-28 13:58:51 +01001623static const int gen8_emit_breadcrumb_sz = 6 + WA_TAIL_DWORDS;
1624
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001625static void gen8_emit_breadcrumb_render(struct drm_i915_gem_request *request,
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001626 u32 *cs)
Chris Wilson7c17d372016-01-20 15:43:35 +02001627{
Michał Winiarskice81a652016-04-12 15:51:55 +02001628 /* We're using qword write, seqno should be aligned to 8 bytes. */
1629 BUILD_BUG_ON(I915_GEM_HWS_INDEX & 1);
1630
Chris Wilson7c17d372016-01-20 15:43:35 +02001631 /* w/a for post sync ops following a GPGPU operation we
1632 * need a prior CS_STALL, which is emitted by the flush
1633 * following the batch.
Michel Thierry53292cd2015-04-15 18:11:33 +01001634 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001635 *cs++ = GFX_OP_PIPE_CONTROL(6);
1636 *cs++ = PIPE_CONTROL_GLOBAL_GTT_IVB | PIPE_CONTROL_CS_STALL |
1637 PIPE_CONTROL_QW_WRITE;
1638 *cs++ = intel_hws_seqno_address(request->engine);
1639 *cs++ = 0;
1640 *cs++ = request->global_seqno;
Michał Winiarskice81a652016-04-12 15:51:55 +02001641 /* We're thrashing one dword of HWS. */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001642 *cs++ = 0;
1643 *cs++ = MI_USER_INTERRUPT;
1644 *cs++ = MI_NOOP;
1645 request->tail = intel_ring_offset(request, cs);
Chris Wilsoned1501d2017-03-27 14:14:12 +01001646 assert_ring_tail_valid(request->ring, request->tail);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001647
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001648 gen8_emit_wa_tail(request, cs);
Oscar Mateo4da46e12014-07-24 17:04:27 +01001649}
1650
Chris Wilson98f29e82016-10-28 13:58:51 +01001651static const int gen8_emit_breadcrumb_render_sz = 8 + WA_TAIL_DWORDS;
1652
John Harrison87531812015-05-29 17:43:44 +01001653static int gen8_init_rcs_context(struct drm_i915_gem_request *req)
Thomas Daniele7778be2014-12-02 12:50:48 +00001654{
1655 int ret;
1656
Tvrtko Ursulin4ac96592017-02-14 15:00:17 +00001657 ret = intel_ring_workarounds_emit(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001658 if (ret)
1659 return ret;
1660
Peter Antoine3bbaba02015-07-10 20:13:11 +03001661 ret = intel_rcs_context_init_mocs(req);
1662 /*
1663 * Failing to program the MOCS is non-fatal.The system will not
1664 * run at peak performance. So generate an error and carry on.
1665 */
1666 if (ret)
1667 DRM_ERROR("MOCS failed to program: expect performance issues.\n");
1668
Chris Wilson4e50f082016-10-28 13:58:31 +01001669 return i915_gem_render_state_emit(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001670}
1671
Oscar Mateo73e4d072014-07-24 17:04:48 +01001672/**
1673 * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001674 * @engine: Engine Command Streamer.
Oscar Mateo73e4d072014-07-24 17:04:48 +01001675 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001676void intel_logical_ring_cleanup(struct intel_engine_cs *engine)
Oscar Mateo454afeb2014-07-24 17:04:22 +01001677{
John Harrison6402c332014-10-31 12:00:26 +00001678 struct drm_i915_private *dev_priv;
Oscar Mateo9832b9d2014-07-24 17:04:30 +01001679
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01001680 /*
1681 * Tasklet cannot be active at this point due intel_mark_active/idle
1682 * so this is just for documentation.
1683 */
1684 if (WARN_ON(test_bit(TASKLET_STATE_SCHED, &engine->irq_tasklet.state)))
1685 tasklet_kill(&engine->irq_tasklet);
1686
Chris Wilsonc0336662016-05-06 15:40:21 +01001687 dev_priv = engine->i915;
John Harrison6402c332014-10-31 12:00:26 +00001688
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001689 if (engine->buffer) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001690 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
Dave Gordonb0366a52015-12-08 15:02:36 +00001691 }
Oscar Mateo48d82382014-07-24 17:04:23 +01001692
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001693 if (engine->cleanup)
1694 engine->cleanup(engine);
Oscar Mateo48d82382014-07-24 17:04:23 +01001695
Chris Wilsone8a9c582016-12-18 15:37:20 +00001696 intel_engine_cleanup_common(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001697
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001698 lrc_destroy_wa_ctx(engine);
Chris Wilsonc0336662016-05-06 15:40:21 +01001699 engine->i915 = NULL;
Akash Goel3b3f1652016-10-13 22:44:48 +05301700 dev_priv->engine[engine->id] = NULL;
1701 kfree(engine);
Oscar Mateo454afeb2014-07-24 17:04:22 +01001702}
1703
Chris Wilsonff44ad52017-03-16 17:13:03 +00001704static void execlists_set_default_submission(struct intel_engine_cs *engine)
Chris Wilsonddd66c52016-08-02 22:50:31 +01001705{
Chris Wilsonff44ad52017-03-16 17:13:03 +00001706 engine->submit_request = execlists_submit_request;
1707 engine->schedule = execlists_schedule;
Chris Wilsonc9203e82017-03-18 10:28:59 +00001708 engine->irq_tasklet.func = intel_lrc_irq_handler;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001709}
1710
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001711static void
Chris Wilsone1382ef2016-05-06 15:40:20 +01001712logical_ring_default_vfuncs(struct intel_engine_cs *engine)
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001713{
1714 /* Default vfuncs which can be overriden by each engine. */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001715 engine->init_hw = gen8_init_common_ring;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001716 engine->reset_hw = reset_common_ring;
Chris Wilsone8a9c582016-12-18 15:37:20 +00001717
1718 engine->context_pin = execlists_context_pin;
1719 engine->context_unpin = execlists_context_unpin;
1720
Chris Wilsonf73e7392016-12-18 15:37:24 +00001721 engine->request_alloc = execlists_request_alloc;
1722
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001723 engine->emit_flush = gen8_emit_flush;
Chris Wilson9b81d552016-10-28 13:58:50 +01001724 engine->emit_breadcrumb = gen8_emit_breadcrumb;
Chris Wilson98f29e82016-10-28 13:58:51 +01001725 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_sz;
Chris Wilsonff44ad52017-03-16 17:13:03 +00001726
1727 engine->set_default_submission = execlists_set_default_submission;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001728
Chris Wilson31bb59c2016-07-01 17:23:27 +01001729 engine->irq_enable = gen8_logical_ring_enable_irq;
1730 engine->irq_disable = gen8_logical_ring_disable_irq;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001731 engine->emit_bb_start = gen8_emit_bb_start;
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001732}
1733
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001734static inline void
Dave Gordonc2c7f242016-07-13 16:03:35 +01001735logical_ring_default_irqs(struct intel_engine_cs *engine)
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001736{
Dave Gordonc2c7f242016-07-13 16:03:35 +01001737 unsigned shift = engine->irq_shift;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001738 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << shift;
1739 engine->irq_keep_mask = GT_CONTEXT_SWITCH_INTERRUPT << shift;
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001740}
1741
Chris Wilson6d2cb5a2017-09-13 14:35:34 +01001742static bool irq_handler_force_mmio(struct drm_i915_private *i915)
1743{
1744 /* GVT emulation depends upon intercepting CSB mmio */
1745 if (intel_vgpu_active(i915))
1746 return true;
1747
1748 /*
1749 * IOMMU adds unpredictable latency causing the CSB write (from the
1750 * GPU into the HWSP) to only be visible some time after the interrupt
1751 * (missed breadcrumb syndrome).
1752 */
1753 if (intel_vtd_active())
1754 return true;
1755
1756 return false;
1757}
1758
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001759static void
1760logical_ring_setup(struct intel_engine_cs *engine)
1761{
1762 struct drm_i915_private *dev_priv = engine->i915;
1763 enum forcewake_domains fw_domains;
1764
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01001765 intel_engine_setup_common(engine);
1766
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001767 /* Intentionally left blank. */
1768 engine->buffer = NULL;
1769
Chris Wilson6d2cb5a2017-09-13 14:35:34 +01001770 engine->csb_use_mmio = irq_handler_force_mmio(dev_priv);
1771
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001772 fw_domains = intel_uncore_forcewake_for_reg(dev_priv,
1773 RING_ELSP(engine),
1774 FW_REG_WRITE);
1775
1776 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
1777 RING_CONTEXT_STATUS_PTR(engine),
1778 FW_REG_READ | FW_REG_WRITE);
1779
1780 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
1781 RING_CONTEXT_STATUS_BUF_BASE(engine),
1782 FW_REG_READ);
1783
1784 engine->fw_domains = fw_domains;
1785
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001786 tasklet_init(&engine->irq_tasklet,
1787 intel_lrc_irq_handler, (unsigned long)engine);
1788
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001789 logical_ring_default_vfuncs(engine);
1790 logical_ring_default_irqs(engine);
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001791}
1792
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +01001793static int logical_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001794{
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001795 int ret;
1796
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01001797 ret = intel_engine_init_common(engine);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001798 if (ret)
1799 goto error;
1800
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001801 return 0;
1802
1803error:
1804 intel_logical_ring_cleanup(engine);
1805 return ret;
1806}
1807
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +01001808int logical_render_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001809{
1810 struct drm_i915_private *dev_priv = engine->i915;
1811 int ret;
1812
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001813 logical_ring_setup(engine);
1814
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001815 if (HAS_L3_DPF(dev_priv))
1816 engine->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
1817
1818 /* Override some for render ring. */
1819 if (INTEL_GEN(dev_priv) >= 9)
1820 engine->init_hw = gen9_init_render_ring;
1821 else
1822 engine->init_hw = gen8_init_render_ring;
1823 engine->init_context = gen8_init_rcs_context;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001824 engine->emit_flush = gen8_emit_flush_render;
Chris Wilson9b81d552016-10-28 13:58:50 +01001825 engine->emit_breadcrumb = gen8_emit_breadcrumb_render;
Chris Wilson98f29e82016-10-28 13:58:51 +01001826 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_render_sz;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001827
Chris Wilsonf51455d2017-01-10 14:47:34 +00001828 ret = intel_engine_create_scratch(engine, PAGE_SIZE);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001829 if (ret)
1830 return ret;
1831
1832 ret = intel_init_workaround_bb(engine);
1833 if (ret) {
1834 /*
1835 * We continue even if we fail to initialize WA batch
1836 * because we only expect rare glitches but nothing
1837 * critical to prevent us from using GPU
1838 */
1839 DRM_ERROR("WA batch buffer initialization failed: %d\n",
1840 ret);
1841 }
1842
Tvrtko Ursulind038fc72016-12-16 13:18:42 +00001843 return logical_ring_init(engine);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001844}
1845
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +01001846int logical_xcs_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001847{
1848 logical_ring_setup(engine);
1849
1850 return logical_ring_init(engine);
1851}
1852
Jeff McGee0cea6502015-02-13 10:27:56 -06001853static u32
Chris Wilsonc0336662016-05-06 15:40:21 +01001854make_rpcs(struct drm_i915_private *dev_priv)
Jeff McGee0cea6502015-02-13 10:27:56 -06001855{
1856 u32 rpcs = 0;
1857
1858 /*
1859 * No explicit RPCS request is needed to ensure full
1860 * slice/subslice/EU enablement prior to Gen9.
1861 */
Chris Wilsonc0336662016-05-06 15:40:21 +01001862 if (INTEL_GEN(dev_priv) < 9)
Jeff McGee0cea6502015-02-13 10:27:56 -06001863 return 0;
1864
1865 /*
1866 * Starting in Gen9, render power gating can leave
1867 * slice/subslice/EU in a partially enabled state. We
1868 * must make an explicit request through RPCS for full
1869 * enablement.
1870 */
Imre Deak43b67992016-08-31 19:13:02 +03001871 if (INTEL_INFO(dev_priv)->sseu.has_slice_pg) {
Jeff McGee0cea6502015-02-13 10:27:56 -06001872 rpcs |= GEN8_RPCS_S_CNT_ENABLE;
Imre Deakf08a0c92016-08-31 19:13:04 +03001873 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.slice_mask) <<
Jeff McGee0cea6502015-02-13 10:27:56 -06001874 GEN8_RPCS_S_CNT_SHIFT;
1875 rpcs |= GEN8_RPCS_ENABLE;
1876 }
1877
Imre Deak43b67992016-08-31 19:13:02 +03001878 if (INTEL_INFO(dev_priv)->sseu.has_subslice_pg) {
Jeff McGee0cea6502015-02-13 10:27:56 -06001879 rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
Imre Deak57ec1712016-08-31 19:13:05 +03001880 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.subslice_mask) <<
Jeff McGee0cea6502015-02-13 10:27:56 -06001881 GEN8_RPCS_SS_CNT_SHIFT;
1882 rpcs |= GEN8_RPCS_ENABLE;
1883 }
1884
Imre Deak43b67992016-08-31 19:13:02 +03001885 if (INTEL_INFO(dev_priv)->sseu.has_eu_pg) {
1886 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
Jeff McGee0cea6502015-02-13 10:27:56 -06001887 GEN8_RPCS_EU_MIN_SHIFT;
Imre Deak43b67992016-08-31 19:13:02 +03001888 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
Jeff McGee0cea6502015-02-13 10:27:56 -06001889 GEN8_RPCS_EU_MAX_SHIFT;
1890 rpcs |= GEN8_RPCS_ENABLE;
1891 }
1892
1893 return rpcs;
1894}
1895
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001896static u32 intel_lr_indirect_ctx_offset(struct intel_engine_cs *engine)
Michel Thierry71562912016-02-23 10:31:49 +00001897{
1898 u32 indirect_ctx_offset;
1899
Chris Wilsonc0336662016-05-06 15:40:21 +01001900 switch (INTEL_GEN(engine->i915)) {
Michel Thierry71562912016-02-23 10:31:49 +00001901 default:
Chris Wilsonc0336662016-05-06 15:40:21 +01001902 MISSING_CASE(INTEL_GEN(engine->i915));
Michel Thierry71562912016-02-23 10:31:49 +00001903 /* fall through */
Michel Thierry7bd0a2c2017-06-06 13:30:38 -07001904 case 10:
1905 indirect_ctx_offset =
1906 GEN10_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
1907 break;
Michel Thierry71562912016-02-23 10:31:49 +00001908 case 9:
1909 indirect_ctx_offset =
1910 GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
1911 break;
1912 case 8:
1913 indirect_ctx_offset =
1914 GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
1915 break;
1916 }
1917
1918 return indirect_ctx_offset;
1919}
1920
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001921static void execlists_init_reg_state(u32 *regs,
Chris Wilsona3aabe82016-10-04 21:11:26 +01001922 struct i915_gem_context *ctx,
1923 struct intel_engine_cs *engine,
1924 struct intel_ring *ring)
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001925{
Chris Wilsona3aabe82016-10-04 21:11:26 +01001926 struct drm_i915_private *dev_priv = engine->i915;
1927 struct i915_hw_ppgtt *ppgtt = ctx->ppgtt ?: dev_priv->mm.aliasing_ppgtt;
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001928 u32 base = engine->mmio_base;
1929 bool rcs = engine->id == RCS;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001930
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001931 /* A context is actually a big batch buffer with several
1932 * MI_LOAD_REGISTER_IMM commands followed by (reg, value) pairs. The
1933 * values we are setting here are only for the first context restore:
1934 * on a subsequent save, the GPU will recreate this batchbuffer with new
1935 * values (including all the missing MI_LOAD_REGISTER_IMM commands that
1936 * we are not initializing here).
1937 */
1938 regs[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(rcs ? 14 : 11) |
1939 MI_LRI_FORCE_POSTED;
1940
1941 CTX_REG(regs, CTX_CONTEXT_CONTROL, RING_CONTEXT_CONTROL(engine),
1942 _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
1943 CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
1944 (HAS_RESOURCE_STREAMER(dev_priv) ?
1945 CTX_CTRL_RS_CTX_ENABLE : 0)));
1946 CTX_REG(regs, CTX_RING_HEAD, RING_HEAD(base), 0);
1947 CTX_REG(regs, CTX_RING_TAIL, RING_TAIL(base), 0);
1948 CTX_REG(regs, CTX_RING_BUFFER_START, RING_START(base), 0);
1949 CTX_REG(regs, CTX_RING_BUFFER_CONTROL, RING_CTL(base),
1950 RING_CTL_SIZE(ring->size) | RING_VALID);
1951 CTX_REG(regs, CTX_BB_HEAD_U, RING_BBADDR_UDW(base), 0);
1952 CTX_REG(regs, CTX_BB_HEAD_L, RING_BBADDR(base), 0);
1953 CTX_REG(regs, CTX_BB_STATE, RING_BBSTATE(base), RING_BB_PPGTT);
1954 CTX_REG(regs, CTX_SECOND_BB_HEAD_U, RING_SBBADDR_UDW(base), 0);
1955 CTX_REG(regs, CTX_SECOND_BB_HEAD_L, RING_SBBADDR(base), 0);
1956 CTX_REG(regs, CTX_SECOND_BB_STATE, RING_SBBSTATE(base), 0);
1957 if (rcs) {
1958 CTX_REG(regs, CTX_BB_PER_CTX_PTR, RING_BB_PER_CTX_PTR(base), 0);
1959 CTX_REG(regs, CTX_RCS_INDIRECT_CTX, RING_INDIRECT_CTX(base), 0);
1960 CTX_REG(regs, CTX_RCS_INDIRECT_CTX_OFFSET,
1961 RING_INDIRECT_CTX_OFFSET(base), 0);
1962
Chris Wilson48bb74e2016-08-15 10:49:04 +01001963 if (engine->wa_ctx.vma) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001964 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001965 u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001966
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001967 regs[CTX_RCS_INDIRECT_CTX + 1] =
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001968 (ggtt_offset + wa_ctx->indirect_ctx.offset) |
1969 (wa_ctx->indirect_ctx.size / CACHELINE_BYTES);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001970
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001971 regs[CTX_RCS_INDIRECT_CTX_OFFSET + 1] =
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001972 intel_lr_indirect_ctx_offset(engine) << 6;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001973
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001974 regs[CTX_BB_PER_CTX_PTR + 1] =
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001975 (ggtt_offset + wa_ctx->per_ctx.offset) | 0x01;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001976 }
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001977 }
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001978
1979 regs[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9) | MI_LRI_FORCE_POSTED;
1980
1981 CTX_REG(regs, CTX_CTX_TIMESTAMP, RING_CTX_TIMESTAMP(base), 0);
Ville Syrjälä0d925ea2015-11-04 23:20:11 +02001982 /* PDP values well be assigned later if needed */
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001983 CTX_REG(regs, CTX_PDP3_UDW, GEN8_RING_PDP_UDW(engine, 3), 0);
1984 CTX_REG(regs, CTX_PDP3_LDW, GEN8_RING_PDP_LDW(engine, 3), 0);
1985 CTX_REG(regs, CTX_PDP2_UDW, GEN8_RING_PDP_UDW(engine, 2), 0);
1986 CTX_REG(regs, CTX_PDP2_LDW, GEN8_RING_PDP_LDW(engine, 2), 0);
1987 CTX_REG(regs, CTX_PDP1_UDW, GEN8_RING_PDP_UDW(engine, 1), 0);
1988 CTX_REG(regs, CTX_PDP1_LDW, GEN8_RING_PDP_LDW(engine, 1), 0);
1989 CTX_REG(regs, CTX_PDP0_UDW, GEN8_RING_PDP_UDW(engine, 0), 0);
1990 CTX_REG(regs, CTX_PDP0_LDW, GEN8_RING_PDP_LDW(engine, 0), 0);
Michel Thierryd7b26332015-04-08 12:13:34 +01001991
Chris Wilson949e8ab2017-02-09 14:40:36 +00001992 if (ppgtt && i915_vm_is_48bit(&ppgtt->base)) {
Michel Thierry2dba3232015-07-30 11:06:23 +01001993 /* 64b PPGTT (48bit canonical)
1994 * PDP0_DESCRIPTOR contains the base address to PML4 and
1995 * other PDP Descriptors are ignored.
1996 */
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001997 ASSIGN_CTX_PML4(ppgtt, regs);
Michel Thierry2dba3232015-07-30 11:06:23 +01001998 }
1999
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002000 if (rcs) {
2001 regs[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
2002 CTX_REG(regs, CTX_R_PWR_CLK_STATE, GEN8_R_PWR_CLK_STATE,
2003 make_rpcs(dev_priv));
Robert Bragg19f81df2017-06-13 12:23:03 +01002004
2005 i915_oa_init_reg_state(engine, ctx, regs);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002006 }
Chris Wilsona3aabe82016-10-04 21:11:26 +01002007}
2008
2009static int
2010populate_lr_context(struct i915_gem_context *ctx,
2011 struct drm_i915_gem_object *ctx_obj,
2012 struct intel_engine_cs *engine,
2013 struct intel_ring *ring)
2014{
2015 void *vaddr;
2016 int ret;
2017
2018 ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
2019 if (ret) {
2020 DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
2021 return ret;
2022 }
2023
2024 vaddr = i915_gem_object_pin_map(ctx_obj, I915_MAP_WB);
2025 if (IS_ERR(vaddr)) {
2026 ret = PTR_ERR(vaddr);
2027 DRM_DEBUG_DRIVER("Could not map object pages! (%d)\n", ret);
2028 return ret;
2029 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002030 ctx_obj->mm.dirty = true;
Chris Wilsona3aabe82016-10-04 21:11:26 +01002031
2032 /* The second page of the context object contains some fields which must
2033 * be set up prior to the first execution. */
2034
2035 execlists_init_reg_state(vaddr + LRC_STATE_PN * PAGE_SIZE,
2036 ctx, engine, ring);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002037
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01002038 i915_gem_object_unpin_map(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002039
2040 return 0;
2041}
2042
Chris Wilsone2efd132016-05-24 14:53:34 +01002043static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
Chris Wilson978f1e02016-04-28 09:56:54 +01002044 struct intel_engine_cs *engine)
Oscar Mateoede7d422014-07-24 17:04:12 +01002045{
Oscar Mateo8c8579172014-07-24 17:04:14 +01002046 struct drm_i915_gem_object *ctx_obj;
Chris Wilson9021ad02016-05-24 14:53:37 +01002047 struct intel_context *ce = &ctx->engine[engine->id];
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002048 struct i915_vma *vma;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002049 uint32_t context_size;
Chris Wilson7e37f882016-08-02 22:50:21 +01002050 struct intel_ring *ring;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002051 int ret;
2052
Chris Wilson9021ad02016-05-24 14:53:37 +01002053 WARN_ON(ce->state);
Oscar Mateoede7d422014-07-24 17:04:12 +01002054
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +03002055 context_size = round_up(engine->context_size, I915_GTT_PAGE_SIZE);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002056
Michel Thierry0b29c752017-09-13 09:56:00 +01002057 /*
2058 * Before the actual start of the context image, we insert a few pages
2059 * for our own use and for sharing with the GuC.
2060 */
2061 context_size += LRC_HEADER_PAGES * PAGE_SIZE;
Alex Daid1675192015-08-12 15:43:43 +01002062
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00002063 ctx_obj = i915_gem_object_create(ctx->i915, context_size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01002064 if (IS_ERR(ctx_obj)) {
Dan Carpenter3126a662015-04-30 17:30:50 +03002065 DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
Chris Wilsonfe3db792016-04-25 13:32:13 +01002066 return PTR_ERR(ctx_obj);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002067 }
2068
Chris Wilsona01cb37a2017-01-16 15:21:30 +00002069 vma = i915_vma_instance(ctx_obj, &ctx->i915->ggtt.base, NULL);
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002070 if (IS_ERR(vma)) {
2071 ret = PTR_ERR(vma);
2072 goto error_deref_obj;
2073 }
2074
Chris Wilson7e37f882016-08-02 22:50:21 +01002075 ring = intel_engine_create_ring(engine, ctx->ring_size);
Chris Wilsondca33ec2016-08-02 22:50:20 +01002076 if (IS_ERR(ring)) {
2077 ret = PTR_ERR(ring);
Nick Hoathe84fe802015-09-11 12:53:46 +01002078 goto error_deref_obj;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002079 }
2080
Chris Wilsondca33ec2016-08-02 22:50:20 +01002081 ret = populate_lr_context(ctx, ctx_obj, engine, ring);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002082 if (ret) {
2083 DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
Chris Wilsondca33ec2016-08-02 22:50:20 +01002084 goto error_ring_free;
Oscar Mateo84c23772014-07-24 17:04:15 +01002085 }
2086
Chris Wilsondca33ec2016-08-02 22:50:20 +01002087 ce->ring = ring;
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002088 ce->state = vma;
Chuanxiao Dong0d402a22017-05-11 18:07:42 +08002089 ce->initialised |= engine->init_context == NULL;
Oscar Mateoede7d422014-07-24 17:04:12 +01002090
2091 return 0;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002092
Chris Wilsondca33ec2016-08-02 22:50:20 +01002093error_ring_free:
Chris Wilson7e37f882016-08-02 22:50:21 +01002094 intel_ring_free(ring);
Nick Hoathe84fe802015-09-11 12:53:46 +01002095error_deref_obj:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002096 i915_gem_object_put(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002097 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01002098}
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002099
Chris Wilson821ed7d2016-09-09 14:11:53 +01002100void intel_lr_context_resume(struct drm_i915_private *dev_priv)
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002101{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002102 struct intel_engine_cs *engine;
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002103 struct i915_gem_context *ctx;
Akash Goel3b3f1652016-10-13 22:44:48 +05302104 enum intel_engine_id id;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002105
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002106 /* Because we emit WA_TAIL_DWORDS there may be a disparity
2107 * between our bookkeeping in ce->ring->head and ce->ring->tail and
2108 * that stored in context. As we only write new commands from
2109 * ce->ring->tail onwards, everything before that is junk. If the GPU
2110 * starts reading from its RING_HEAD from the context, it may try to
2111 * execute that junk and die.
2112 *
2113 * So to avoid that we reset the context images upon resume. For
2114 * simplicity, we just zero everything out.
2115 */
Chris Wilson829a0af2017-06-20 12:05:45 +01002116 list_for_each_entry(ctx, &dev_priv->contexts.list, link) {
Akash Goel3b3f1652016-10-13 22:44:48 +05302117 for_each_engine(engine, dev_priv, id) {
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002118 struct intel_context *ce = &ctx->engine[engine->id];
2119 u32 *reg;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002120
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002121 if (!ce->state)
2122 continue;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002123
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002124 reg = i915_gem_object_pin_map(ce->state->obj,
2125 I915_MAP_WB);
2126 if (WARN_ON(IS_ERR(reg)))
2127 continue;
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01002128
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002129 reg += LRC_STATE_PN * PAGE_SIZE / sizeof(*reg);
2130 reg[CTX_RING_HEAD+1] = 0;
2131 reg[CTX_RING_TAIL+1] = 0;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002132
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002133 ce->state->obj->mm.dirty = true;
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002134 i915_gem_object_unpin_map(ce->state->obj);
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002135
Chris Wilsone6ba9992017-04-25 14:00:49 +01002136 intel_ring_reset(ce->ring, 0);
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002137 }
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002138 }
2139}