blob: 6f972e6ec6639010a922427e5a9a0fdedcb087a6 [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 */
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100282 desc |= i915_ggtt_offset(ce->state) + LRC_PPHWSP_PN * 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 Wilsone2efd132016-05-24 14:53:34 +0100289uint64_t intel_lr_context_descriptor(struct i915_gem_context *ctx,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000290 struct intel_engine_cs *engine)
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000291{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000292 return ctx->engine[engine->id].lrc_desc;
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000293}
294
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100295static inline void
296execlists_context_status_change(struct drm_i915_gem_request *rq,
297 unsigned long status)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100298{
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100299 /*
300 * Only used when GVT-g is enabled now. When GVT-g is disabled,
301 * The compiler should eliminate this function as dead-code.
302 */
303 if (!IS_ENABLED(CONFIG_DRM_I915_GVT))
304 return;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100305
Changbin Du3fc03062017-03-13 10:47:11 +0800306 atomic_notifier_call_chain(&rq->engine->context_status_notifier,
307 status, rq);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100308}
309
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000310static void
311execlists_update_context_pdps(struct i915_hw_ppgtt *ppgtt, u32 *reg_state)
312{
313 ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
314 ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
315 ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
316 ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
317}
318
Chris Wilson70c2a242016-09-09 14:11:46 +0100319static u64 execlists_update_context(struct drm_i915_gem_request *rq)
Oscar Mateoae1250b2014-07-24 17:04:37 +0100320{
Chris Wilson70c2a242016-09-09 14:11:46 +0100321 struct intel_context *ce = &rq->ctx->engine[rq->engine->id];
Zhi Wang04da8112017-02-06 18:37:16 +0800322 struct i915_hw_ppgtt *ppgtt =
323 rq->ctx->ppgtt ?: rq->i915->mm.aliasing_ppgtt;
Chris Wilson70c2a242016-09-09 14:11:46 +0100324 u32 *reg_state = ce->lrc_reg_state;
Oscar Mateoae1250b2014-07-24 17:04:37 +0100325
Chris Wilsone6ba9992017-04-25 14:00:49 +0100326 reg_state[CTX_RING_TAIL+1] = intel_ring_set_tail(rq->ring, rq->tail);
Oscar Mateoae1250b2014-07-24 17:04:37 +0100327
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000328 /* True 32b PPGTT with dynamic page allocation: update PDP
329 * registers and point the unallocated PDPs to scratch page.
330 * PML4 is allocated during ppgtt init, so this is not needed
331 * in 48-bit mode.
332 */
Chris Wilson949e8ab2017-02-09 14:40:36 +0000333 if (ppgtt && !i915_vm_is_48bit(&ppgtt->base))
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000334 execlists_update_context_pdps(ppgtt, reg_state);
Chris Wilson70c2a242016-09-09 14:11:46 +0100335
336 return ce->lrc_desc;
Oscar Mateoae1250b2014-07-24 17:04:37 +0100337}
338
Chris Wilson70c2a242016-09-09 14:11:46 +0100339static void execlists_submit_ports(struct intel_engine_cs *engine)
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100340{
Chris Wilson70c2a242016-09-09 14:11:46 +0100341 struct execlist_port *port = engine->execlist_port;
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100342 u32 __iomem *elsp =
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100343 engine->i915->regs + i915_mmio_reg_offset(RING_ELSP(engine));
344 unsigned int n;
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100345
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100346 for (n = ARRAY_SIZE(engine->execlist_port); n--; ) {
347 struct drm_i915_gem_request *rq;
348 unsigned int count;
349 u64 desc;
Chris Wilson70c2a242016-09-09 14:11:46 +0100350
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100351 rq = port_unpack(&port[n], &count);
352 if (rq) {
353 GEM_BUG_ON(count > !n);
354 if (!count++)
355 execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_IN);
356 port_set(&port[n], port_pack(rq, count));
357 desc = execlists_update_context(rq);
358 GEM_DEBUG_EXEC(port[n].context_id = upper_32_bits(desc));
359 } else {
360 GEM_BUG_ON(!n);
361 desc = 0;
362 }
363
364 writel(upper_32_bits(desc), elsp);
365 writel(lower_32_bits(desc), elsp);
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100366 }
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100367}
368
Chris Wilson70c2a242016-09-09 14:11:46 +0100369static bool ctx_single_port_submission(const struct i915_gem_context *ctx)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100370{
Chris Wilson70c2a242016-09-09 14:11:46 +0100371 return (IS_ENABLED(CONFIG_DRM_I915_GVT) &&
Chris Wilson60958682016-12-31 11:20:11 +0000372 i915_gem_context_force_single_submission(ctx));
Ben Widawsky84b790f2014-07-24 17:04:36 +0100373}
374
Chris Wilson70c2a242016-09-09 14:11:46 +0100375static bool can_merge_ctx(const struct i915_gem_context *prev,
376 const struct i915_gem_context *next)
Michel Thierryacdd8842014-07-24 17:04:38 +0100377{
Chris Wilson70c2a242016-09-09 14:11:46 +0100378 if (prev != next)
379 return false;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100380
Chris Wilson70c2a242016-09-09 14:11:46 +0100381 if (ctx_single_port_submission(prev))
382 return false;
Michel Thierryacdd8842014-07-24 17:04:38 +0100383
Chris Wilson70c2a242016-09-09 14:11:46 +0100384 return true;
385}
Peter Antoine779949f2015-05-11 16:03:27 +0100386
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100387static void port_assign(struct execlist_port *port,
388 struct drm_i915_gem_request *rq)
389{
390 GEM_BUG_ON(rq == port_request(port));
391
392 if (port_isset(port))
393 i915_gem_request_put(port_request(port));
394
395 port_set(port, port_pack(i915_gem_request_get(rq), port_count(port)));
396}
397
Chris Wilson70c2a242016-09-09 14:11:46 +0100398static void execlists_dequeue(struct intel_engine_cs *engine)
399{
Chris Wilson20311bd2016-11-14 20:41:03 +0000400 struct drm_i915_gem_request *last;
Chris Wilson70c2a242016-09-09 14:11:46 +0100401 struct execlist_port *port = engine->execlist_port;
Chris Wilson20311bd2016-11-14 20:41:03 +0000402 struct rb_node *rb;
Chris Wilson70c2a242016-09-09 14:11:46 +0100403 bool submit = false;
Michel Thierryacdd8842014-07-24 17:04:38 +0100404
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100405 last = port_request(port);
Chris Wilson70c2a242016-09-09 14:11:46 +0100406 if (last)
407 /* WaIdleLiteRestore:bdw,skl
408 * Apply the wa NOOPs to prevent ring:HEAD == req:TAIL
Chris Wilson9b81d552016-10-28 13:58:50 +0100409 * as we resubmit the request. See gen8_emit_breadcrumb()
Chris Wilson70c2a242016-09-09 14:11:46 +0100410 * for where we prepare the padding after the end of the
411 * request.
Michel Thierry53292cd2015-04-15 18:11:33 +0100412 */
Chris Wilson70c2a242016-09-09 14:11:46 +0100413 last->tail = last->wa_tail;
414
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100415 GEM_BUG_ON(port_isset(&port[1]));
Chris Wilson70c2a242016-09-09 14:11:46 +0100416
417 /* Hardware submission is through 2 ports. Conceptually each port
418 * has a (RING_START, RING_HEAD, RING_TAIL) tuple. RING_START is
419 * static for a context, and unique to each, so we only execute
420 * requests belonging to a single context from each ring. RING_HEAD
421 * is maintained by the CS in the context image, it marks the place
422 * where it got up to last time, and through RING_TAIL we tell the CS
423 * where we want to execute up to this time.
424 *
425 * In this list the requests are in order of execution. Consecutive
426 * requests from the same context are adjacent in the ringbuffer. We
427 * can combine these requests into a single RING_TAIL update:
428 *
429 * RING_HEAD...req1...req2
430 * ^- RING_TAIL
431 * since to execute req2 the CS must first execute req1.
432 *
433 * Our goal then is to point each port to the end of a consecutive
434 * sequence of requests as being the most optimal (fewest wake ups
435 * and context switches) submission.
436 */
437
Tvrtko Ursulin9f7886d2017-03-21 10:55:11 +0000438 spin_lock_irq(&engine->timeline->lock);
Chris Wilson20311bd2016-11-14 20:41:03 +0000439 rb = engine->execlist_first;
Chris Wilson6c067572017-05-17 13:10:03 +0100440 GEM_BUG_ON(rb_first(&engine->execlist_queue) != rb);
Chris Wilson20311bd2016-11-14 20:41:03 +0000441 while (rb) {
Chris Wilson6c067572017-05-17 13:10:03 +0100442 struct i915_priolist *p = rb_entry(rb, typeof(*p), node);
443 struct drm_i915_gem_request *rq, *rn;
Chris Wilson20311bd2016-11-14 20:41:03 +0000444
Chris Wilson6c067572017-05-17 13:10:03 +0100445 list_for_each_entry_safe(rq, rn, &p->requests, priotree.link) {
446 /*
447 * Can we combine this request with the current port?
448 * It has to be the same context/ringbuffer and not
449 * have any exceptions (e.g. GVT saying never to
450 * combine contexts).
451 *
452 * If we can combine the requests, we can execute both
453 * by updating the RING_TAIL to point to the end of the
454 * second request, and so we never need to tell the
455 * hardware about the first.
Chris Wilson70c2a242016-09-09 14:11:46 +0100456 */
Chris Wilson6c067572017-05-17 13:10:03 +0100457 if (last && !can_merge_ctx(rq->ctx, last->ctx)) {
458 /*
459 * If we are on the second port and cannot
460 * combine this request with the last, then we
461 * are done.
462 */
463 if (port != engine->execlist_port) {
464 __list_del_many(&p->requests,
465 &rq->priotree.link);
466 goto done;
467 }
Chris Wilson70c2a242016-09-09 14:11:46 +0100468
Chris Wilson6c067572017-05-17 13:10:03 +0100469 /*
470 * If GVT overrides us we only ever submit
471 * port[0], leaving port[1] empty. Note that we
472 * also have to be careful that we don't queue
473 * the same context (even though a different
474 * request) to the second port.
475 */
476 if (ctx_single_port_submission(last->ctx) ||
477 ctx_single_port_submission(rq->ctx)) {
478 __list_del_many(&p->requests,
479 &rq->priotree.link);
480 goto done;
481 }
Chris Wilson70c2a242016-09-09 14:11:46 +0100482
Chris Wilson6c067572017-05-17 13:10:03 +0100483 GEM_BUG_ON(last->ctx == rq->ctx);
Chris Wilson70c2a242016-09-09 14:11:46 +0100484
Chris Wilson6c067572017-05-17 13:10:03 +0100485 if (submit)
486 port_assign(port, last);
487 port++;
488 }
489
490 INIT_LIST_HEAD(&rq->priotree.link);
491 rq->priotree.priority = INT_MAX;
492
493 __i915_gem_request_submit(rq);
494 trace_i915_gem_request_in(rq, port_index(port, engine));
495 last = rq;
496 submit = true;
Chris Wilson70c2a242016-09-09 14:11:46 +0100497 }
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000498
Chris Wilson20311bd2016-11-14 20:41:03 +0000499 rb = rb_next(rb);
Chris Wilson6c067572017-05-17 13:10:03 +0100500 rb_erase(&p->node, &engine->execlist_queue);
501 INIT_LIST_HEAD(&p->requests);
502 if (p->priority != I915_PRIORITY_NORMAL)
Chris Wilsonc5cf9a92017-05-17 13:10:04 +0100503 kmem_cache_free(engine->i915->priorities, p);
Michel Thierry53292cd2015-04-15 18:11:33 +0100504 }
Chris Wilson6c067572017-05-17 13:10:03 +0100505done:
506 engine->execlist_first = rb;
507 if (submit)
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100508 port_assign(port, last);
Tvrtko Ursulin9f7886d2017-03-21 10:55:11 +0000509 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson70c2a242016-09-09 14:11:46 +0100510
511 if (submit)
512 execlists_submit_ports(engine);
Michel Thierryacdd8842014-07-24 17:04:38 +0100513}
514
Chris Wilson816ee792017-01-24 11:00:03 +0000515static bool execlists_elsp_ready(const struct intel_engine_cs *engine)
Ben Widawsky91a41032016-01-05 10:30:07 -0800516{
Chris Wilson816ee792017-01-24 11:00:03 +0000517 const struct execlist_port *port = engine->execlist_port;
Ben Widawsky91a41032016-01-05 10:30:07 -0800518
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100519 return port_count(&port[0]) + port_count(&port[1]) < 2;
Ben Widawsky91a41032016-01-05 10:30:07 -0800520}
521
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200522/*
Oscar Mateo73e4d072014-07-24 17:04:48 +0100523 * Check the unread Context Status Buffers and manage the submission of new
524 * contexts to the ELSP accordingly.
525 */
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +0100526static void intel_lrc_irq_handler(unsigned long data)
Thomas Daniele981e7b2014-07-24 17:04:39 +0100527{
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +0100528 struct intel_engine_cs *engine = (struct intel_engine_cs *)data;
Chris Wilson70c2a242016-09-09 14:11:46 +0100529 struct execlist_port *port = engine->execlist_port;
Chris Wilsonc0336662016-05-06 15:40:21 +0100530 struct drm_i915_private *dev_priv = engine->i915;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100531
Chris Wilson48921262017-04-11 18:58:50 +0100532 /* We can skip acquiring intel_runtime_pm_get() here as it was taken
533 * on our behalf by the request (see i915_gem_mark_busy()) and it will
534 * not be relinquished until the device is idle (see
535 * i915_gem_idle_work_handler()). As a precaution, we make sure
536 * that all ELSP are drained i.e. we have processed the CSB,
537 * before allowing ourselves to idle and calling intel_runtime_pm_put().
538 */
539 GEM_BUG_ON(!dev_priv->gt.awake);
540
Tvrtko Ursulin37566852016-04-12 14:37:31 +0100541 intel_uncore_forcewake_get(dev_priv, engine->fw_domains);
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000542
Chris Wilson899f6202017-03-21 11:33:20 +0000543 /* Prefer doing test_and_clear_bit() as a two stage operation to avoid
544 * imposing the cost of a locked atomic transaction when submitting a
545 * new request (outside of the context-switch interrupt).
546 */
547 while (test_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted)) {
Chris Wilson70c2a242016-09-09 14:11:46 +0100548 u32 __iomem *csb_mmio =
549 dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine));
550 u32 __iomem *buf =
551 dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_BUF_LO(engine, 0));
Chris Wilson4af0d722017-03-25 20:10:53 +0000552 unsigned int head, tail;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100553
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 Wilson4af0d722017-03-25 20:10:53 +0000565 head = readl(csb_mmio);
566 tail = GEN8_CSB_WRITE_PTR(head);
567 head = GEN8_CSB_READ_PTR(head);
568 while (head != tail) {
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100569 struct drm_i915_gem_request *rq;
Chris Wilson4af0d722017-03-25 20:10:53 +0000570 unsigned int status;
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100571 unsigned int count;
Chris Wilsona37951a2017-01-24 11:00:06 +0000572
Chris Wilson4af0d722017-03-25 20:10:53 +0000573 if (++head == GEN8_CSB_ENTRIES)
574 head = 0;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100575
Chris Wilson2ffe80a2017-02-06 17:05:02 +0000576 /* We are flying near dragons again.
577 *
578 * We hold a reference to the request in execlist_port[]
579 * but no more than that. We are operating in softirq
580 * context and so cannot hold any mutex or sleep. That
581 * prevents us stopping the requests we are processing
582 * in port[] from being retired simultaneously (the
583 * breadcrumb will be complete before we see the
584 * context-switch). As we only hold the reference to the
585 * request, any pointer chasing underneath the request
586 * is subject to a potential use-after-free. Thus we
587 * store all of the bookkeeping within port[] as
588 * required, and avoid using unguarded pointers beneath
589 * request itself. The same applies to the atomic
590 * status notifier.
591 */
592
Chris Wilson4af0d722017-03-25 20:10:53 +0000593 status = readl(buf + 2 * head);
Chris Wilson70c2a242016-09-09 14:11:46 +0100594 if (!(status & GEN8_CTX_STATUS_COMPLETED_MASK))
595 continue;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100596
Chris Wilson86aa7e72017-01-23 11:31:32 +0000597 /* Check the context/desc id for this event matches */
Chris Wilson4af0d722017-03-25 20:10:53 +0000598 GEM_DEBUG_BUG_ON(readl(buf + 2 * head + 1) !=
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100599 port->context_id);
Chris Wilson86aa7e72017-01-23 11:31:32 +0000600
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100601 rq = port_unpack(port, &count);
602 GEM_BUG_ON(count == 0);
603 if (--count == 0) {
Chris Wilson70c2a242016-09-09 14:11:46 +0100604 GEM_BUG_ON(status & GEN8_CTX_STATUS_PREEMPTED);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100605 GEM_BUG_ON(!i915_gem_request_completed(rq));
606 execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_OUT);
Thomas Daniele981e7b2014-07-24 17:04:39 +0100607
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100608 trace_i915_gem_request_out(rq);
609 i915_gem_request_put(rq);
610
Chris Wilson70c2a242016-09-09 14:11:46 +0100611 port[0] = port[1];
612 memset(&port[1], 0, sizeof(port[1]));
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100613 } else {
614 port_set(port, port_pack(rq, count));
Chris Wilson70c2a242016-09-09 14:11:46 +0100615 }
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000616
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100617 /* After the final element, the hw should be idle */
618 GEM_BUG_ON(port_count(port) == 0 &&
Chris Wilson70c2a242016-09-09 14:11:46 +0100619 !(status & GEN8_CTX_STATUS_ACTIVE_IDLE));
Chris Wilson4af0d722017-03-25 20:10:53 +0000620 }
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000621
Chris Wilson4af0d722017-03-25 20:10:53 +0000622 writel(_MASKED_FIELD(GEN8_CSB_READ_PTR_MASK, head << 8),
Chris Wilson70c2a242016-09-09 14:11:46 +0100623 csb_mmio);
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000624 }
625
Chris Wilson70c2a242016-09-09 14:11:46 +0100626 if (execlists_elsp_ready(engine))
627 execlists_dequeue(engine);
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000628
Chris Wilson70c2a242016-09-09 14:11:46 +0100629 intel_uncore_forcewake_put(dev_priv, engine->fw_domains);
Thomas Daniele981e7b2014-07-24 17:04:39 +0100630}
631
Chris Wilson6c067572017-05-17 13:10:03 +0100632static bool
633insert_request(struct intel_engine_cs *engine,
634 struct i915_priotree *pt,
635 int prio)
Chris Wilson20311bd2016-11-14 20:41:03 +0000636{
Chris Wilson6c067572017-05-17 13:10:03 +0100637 struct i915_priolist *p;
638 struct rb_node **parent, *rb;
Chris Wilson20311bd2016-11-14 20:41:03 +0000639 bool first = true;
640
Chris Wilson6c067572017-05-17 13:10:03 +0100641 if (unlikely(engine->no_priolist))
642 prio = I915_PRIORITY_NORMAL;
643
644find_priolist:
Chris Wilson20311bd2016-11-14 20:41:03 +0000645 /* most positive priority is scheduled first, equal priorities fifo */
646 rb = NULL;
Chris Wilson6c067572017-05-17 13:10:03 +0100647 parent = &engine->execlist_queue.rb_node;
648 while (*parent) {
649 rb = *parent;
650 p = rb_entry(rb, typeof(*p), node);
651 if (prio > p->priority) {
652 parent = &rb->rb_left;
653 } else if (prio < p->priority) {
654 parent = &rb->rb_right;
Chris Wilson20311bd2016-11-14 20:41:03 +0000655 first = false;
Chris Wilson6c067572017-05-17 13:10:03 +0100656 } else {
657 list_add_tail(&pt->link, &p->requests);
658 return false;
Chris Wilson20311bd2016-11-14 20:41:03 +0000659 }
660 }
Chris Wilson6c067572017-05-17 13:10:03 +0100661
662 if (prio == I915_PRIORITY_NORMAL) {
663 p = &engine->default_priolist;
664 } else {
Chris Wilsonc5cf9a92017-05-17 13:10:04 +0100665 p = kmem_cache_alloc(engine->i915->priorities, GFP_ATOMIC);
Chris Wilson6c067572017-05-17 13:10:03 +0100666 /* Convert an allocation failure to a priority bump */
667 if (unlikely(!p)) {
668 prio = I915_PRIORITY_NORMAL; /* recurses just once */
669
670 /* To maintain ordering with all rendering, after an
671 * allocation failure we have to disable all scheduling.
672 * Requests will then be executed in fifo, and schedule
673 * will ensure that dependencies are emitted in fifo.
674 * There will be still some reordering with existing
675 * requests, so if userspace lied about their
676 * dependencies that reordering may be visible.
677 */
678 engine->no_priolist = true;
679 goto find_priolist;
680 }
681 }
682
683 p->priority = prio;
684 rb_link_node(&p->node, rb, parent);
685 rb_insert_color(&p->node, &engine->execlist_queue);
686
687 INIT_LIST_HEAD(&p->requests);
688 list_add_tail(&pt->link, &p->requests);
689
690 if (first)
691 engine->execlist_first = &p->node;
Chris Wilson20311bd2016-11-14 20:41:03 +0000692
693 return first;
694}
695
Chris Wilsonf4ea6bd2016-08-02 22:50:32 +0100696static void execlists_submit_request(struct drm_i915_gem_request *request)
Michel Thierryacdd8842014-07-24 17:04:38 +0100697{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000698 struct intel_engine_cs *engine = request->engine;
Chris Wilson5590af32016-09-09 14:11:54 +0100699 unsigned long flags;
Michel Thierryacdd8842014-07-24 17:04:38 +0100700
Chris Wilson663f71e2016-11-14 20:41:00 +0000701 /* Will be called from irq-context when using foreign fences. */
702 spin_lock_irqsave(&engine->timeline->lock, flags);
Michel Thierryacdd8842014-07-24 17:04:38 +0100703
Chris Wilson6c067572017-05-17 13:10:03 +0100704 if (insert_request(engine,
705 &request->priotree,
706 request->priotree.priority)) {
Chris Wilson48ea2552017-01-24 11:00:08 +0000707 if (execlists_elsp_ready(engine))
Chris Wilson38332812017-01-24 11:00:07 +0000708 tasklet_hi_schedule(&engine->irq_tasklet);
709 }
Michel Thierryacdd8842014-07-24 17:04:38 +0100710
Chris Wilson6c067572017-05-17 13:10:03 +0100711 GEM_BUG_ON(!engine->execlist_first);
712 GEM_BUG_ON(list_empty(&request->priotree.link));
713
Chris Wilson663f71e2016-11-14 20:41:00 +0000714 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Michel Thierryacdd8842014-07-24 17:04:38 +0100715}
716
Chris Wilson20311bd2016-11-14 20:41:03 +0000717static struct intel_engine_cs *
718pt_lock_engine(struct i915_priotree *pt, struct intel_engine_cs *locked)
719{
Chris Wilsona79a5242017-03-27 21:21:43 +0100720 struct intel_engine_cs *engine =
721 container_of(pt, struct drm_i915_gem_request, priotree)->engine;
Chris Wilson20311bd2016-11-14 20:41:03 +0000722
Chris Wilsona79a5242017-03-27 21:21:43 +0100723 GEM_BUG_ON(!locked);
724
Chris Wilson20311bd2016-11-14 20:41:03 +0000725 if (engine != locked) {
Chris Wilsona79a5242017-03-27 21:21:43 +0100726 spin_unlock(&locked->timeline->lock);
727 spin_lock(&engine->timeline->lock);
Chris Wilson20311bd2016-11-14 20:41:03 +0000728 }
729
730 return engine;
731}
732
733static void execlists_schedule(struct drm_i915_gem_request *request, int prio)
734{
Chris Wilsona79a5242017-03-27 21:21:43 +0100735 struct intel_engine_cs *engine;
Chris Wilson20311bd2016-11-14 20:41:03 +0000736 struct i915_dependency *dep, *p;
737 struct i915_dependency stack;
738 LIST_HEAD(dfs);
739
740 if (prio <= READ_ONCE(request->priotree.priority))
741 return;
742
Chris Wilson70cd1472016-11-28 14:36:49 +0000743 /* Need BKL in order to use the temporary link inside i915_dependency */
744 lockdep_assert_held(&request->i915->drm.struct_mutex);
Chris Wilson20311bd2016-11-14 20:41:03 +0000745
746 stack.signaler = &request->priotree;
747 list_add(&stack.dfs_link, &dfs);
748
749 /* Recursively bump all dependent priorities to match the new request.
750 *
751 * A naive approach would be to use recursion:
752 * static void update_priorities(struct i915_priotree *pt, prio) {
753 * list_for_each_entry(dep, &pt->signalers_list, signal_link)
754 * update_priorities(dep->signal, prio)
755 * insert_request(pt);
756 * }
757 * but that may have unlimited recursion depth and so runs a very
758 * real risk of overunning the kernel stack. Instead, we build
759 * a flat list of all dependencies starting with the current request.
760 * As we walk the list of dependencies, we add all of its dependencies
761 * to the end of the list (this may include an already visited
762 * request) and continue to walk onwards onto the new dependencies. The
763 * end result is a topological list of requests in reverse order, the
764 * last element in the list is the request we must execute first.
765 */
766 list_for_each_entry_safe(dep, p, &dfs, dfs_link) {
767 struct i915_priotree *pt = dep->signaler;
768
Chris Wilsona79a5242017-03-27 21:21:43 +0100769 /* Within an engine, there can be no cycle, but we may
770 * refer to the same dependency chain multiple times
771 * (redundant dependencies are not eliminated) and across
772 * engines.
773 */
774 list_for_each_entry(p, &pt->signalers_list, signal_link) {
775 GEM_BUG_ON(p->signaler->priority < pt->priority);
Chris Wilson20311bd2016-11-14 20:41:03 +0000776 if (prio > READ_ONCE(p->signaler->priority))
777 list_move_tail(&p->dfs_link, &dfs);
Chris Wilsona79a5242017-03-27 21:21:43 +0100778 }
Chris Wilson20311bd2016-11-14 20:41:03 +0000779
Chris Wilson0798cff2016-12-05 14:29:41 +0000780 list_safe_reset_next(dep, p, dfs_link);
Chris Wilson20311bd2016-11-14 20:41:03 +0000781 }
782
Chris Wilson349bdb62017-05-17 13:10:05 +0100783 /* If we didn't need to bump any existing priorities, and we haven't
784 * yet submitted this request (i.e. there is no potential race with
785 * execlists_submit_request()), we can set our own priority and skip
786 * acquiring the engine locks.
787 */
788 if (request->priotree.priority == INT_MIN) {
789 GEM_BUG_ON(!list_empty(&request->priotree.link));
790 request->priotree.priority = prio;
791 if (stack.dfs_link.next == stack.dfs_link.prev)
792 return;
793 __list_del_entry(&stack.dfs_link);
794 }
795
Chris Wilsona79a5242017-03-27 21:21:43 +0100796 engine = request->engine;
797 spin_lock_irq(&engine->timeline->lock);
798
Chris Wilson20311bd2016-11-14 20:41:03 +0000799 /* Fifo and depth-first replacement ensure our deps execute before us */
800 list_for_each_entry_safe_reverse(dep, p, &dfs, dfs_link) {
801 struct i915_priotree *pt = dep->signaler;
802
803 INIT_LIST_HEAD(&dep->dfs_link);
804
805 engine = pt_lock_engine(pt, engine);
806
807 if (prio <= pt->priority)
808 continue;
809
Chris Wilson20311bd2016-11-14 20:41:03 +0000810 pt->priority = prio;
Chris Wilson6c067572017-05-17 13:10:03 +0100811 if (!list_empty(&pt->link)) {
812 __list_del_entry(&pt->link);
813 insert_request(engine, pt, prio);
Chris Wilsona79a5242017-03-27 21:21:43 +0100814 }
Chris Wilson20311bd2016-11-14 20:41:03 +0000815 }
816
Chris Wilsona79a5242017-03-27 21:21:43 +0100817 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson20311bd2016-11-14 20:41:03 +0000818
819 /* XXX Do we need to preempt to make room for us and our deps? */
820}
821
Chris Wilson266a2402017-05-04 10:33:08 +0100822static struct intel_ring *
823execlists_context_pin(struct intel_engine_cs *engine,
824 struct i915_gem_context *ctx)
Oscar Mateodcb4c122014-11-13 10:28:10 +0000825{
Chris Wilson9021ad02016-05-24 14:53:37 +0100826 struct intel_context *ce = &ctx->engine[engine->id];
Chris Wilson2947e402016-12-18 15:37:23 +0000827 unsigned int flags;
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +0100828 void *vaddr;
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000829 int ret;
Oscar Mateodcb4c122014-11-13 10:28:10 +0000830
Chris Wilson91c8a322016-07-05 10:40:23 +0100831 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000832
Chris Wilson266a2402017-05-04 10:33:08 +0100833 if (likely(ce->pin_count++))
834 goto out;
Chris Wilsona533b4b2017-03-16 17:16:28 +0000835 GEM_BUG_ON(!ce->pin_count); /* no overflow please! */
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100836
Chris Wilsone8a9c582016-12-18 15:37:20 +0000837 if (!ce->state) {
838 ret = execlists_context_deferred_alloc(ctx, engine);
839 if (ret)
840 goto err;
841 }
Chris Wilson56f6e0a2017-01-05 15:30:20 +0000842 GEM_BUG_ON(!ce->state);
Chris Wilsone8a9c582016-12-18 15:37:20 +0000843
Chris Wilson72b72ae2017-02-10 10:14:22 +0000844 flags = PIN_GLOBAL | PIN_HIGH;
Daniele Ceraolo Spuriofeef2a72016-12-23 15:56:22 -0800845 if (ctx->ggtt_offset_bias)
846 flags |= PIN_OFFSET_BIAS | ctx->ggtt_offset_bias;
Chris Wilson2947e402016-12-18 15:37:23 +0000847
848 ret = i915_vma_pin(ce->state, 0, GEN8_LR_CONTEXT_ALIGN, flags);
Nick Hoathe84fe802015-09-11 12:53:46 +0100849 if (ret)
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100850 goto err;
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000851
Chris Wilsonbf3783e2016-08-15 10:48:54 +0100852 vaddr = i915_gem_object_pin_map(ce->state->obj, I915_MAP_WB);
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +0100853 if (IS_ERR(vaddr)) {
854 ret = PTR_ERR(vaddr);
Chris Wilsonbf3783e2016-08-15 10:48:54 +0100855 goto unpin_vma;
Tvrtko Ursulin82352e92016-01-15 17:12:45 +0000856 }
857
Chris Wilsond822bb12017-04-03 12:34:25 +0100858 ret = intel_ring_pin(ce->ring, ctx->i915, ctx->ggtt_offset_bias);
Nick Hoathe84fe802015-09-11 12:53:46 +0100859 if (ret)
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +0100860 goto unpin_map;
Alex Daid1675192015-08-12 15:43:43 +0100861
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000862 intel_lr_context_descriptor_update(ctx, engine);
Chris Wilson9021ad02016-05-24 14:53:37 +0100863
Chris Wilsona3aabe82016-10-04 21:11:26 +0100864 ce->lrc_reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
865 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100866 i915_ggtt_offset(ce->ring->vma);
Chris Wilsona3aabe82016-10-04 21:11:26 +0100867
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100868 ce->state->obj->mm.dirty = true;
Daniel Vettere93c28f2015-09-02 14:33:42 +0200869
Chris Wilson9a6feaf2016-07-20 13:31:50 +0100870 i915_gem_context_get(ctx);
Chris Wilson266a2402017-05-04 10:33:08 +0100871out:
872 return ce->ring;
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000873
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +0100874unpin_map:
Chris Wilsonbf3783e2016-08-15 10:48:54 +0100875 i915_gem_object_unpin_map(ce->state->obj);
876unpin_vma:
877 __i915_vma_unpin(ce->state);
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100878err:
Chris Wilson9021ad02016-05-24 14:53:37 +0100879 ce->pin_count = 0;
Chris Wilson266a2402017-05-04 10:33:08 +0100880 return ERR_PTR(ret);
Oscar Mateodcb4c122014-11-13 10:28:10 +0000881}
882
Chris Wilsone8a9c582016-12-18 15:37:20 +0000883static void execlists_context_unpin(struct intel_engine_cs *engine,
884 struct i915_gem_context *ctx)
Oscar Mateodcb4c122014-11-13 10:28:10 +0000885{
Chris Wilson9021ad02016-05-24 14:53:37 +0100886 struct intel_context *ce = &ctx->engine[engine->id];
Daniel Vetteraf3302b2015-12-04 17:27:15 +0100887
Chris Wilson91c8a322016-07-05 10:40:23 +0100888 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilson9021ad02016-05-24 14:53:37 +0100889 GEM_BUG_ON(ce->pin_count == 0);
Tvrtko Ursulin321fe302016-01-28 10:29:55 +0000890
Chris Wilson9021ad02016-05-24 14:53:37 +0100891 if (--ce->pin_count)
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100892 return;
893
Chris Wilsonaad29fb2016-08-02 22:50:23 +0100894 intel_ring_unpin(ce->ring);
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100895
Chris Wilsonbf3783e2016-08-15 10:48:54 +0100896 i915_gem_object_unpin_map(ce->state->obj);
897 i915_vma_unpin(ce->state);
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100898
Chris Wilson9a6feaf2016-07-20 13:31:50 +0100899 i915_gem_context_put(ctx);
Oscar Mateodcb4c122014-11-13 10:28:10 +0000900}
901
Chris Wilsonf73e7392016-12-18 15:37:24 +0000902static int execlists_request_alloc(struct drm_i915_gem_request *request)
Chris Wilsonef11c012016-12-18 15:37:19 +0000903{
904 struct intel_engine_cs *engine = request->engine;
905 struct intel_context *ce = &request->ctx->engine[engine->id];
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000906 u32 *cs;
Chris Wilsonef11c012016-12-18 15:37:19 +0000907 int ret;
908
Chris Wilsone8a9c582016-12-18 15:37:20 +0000909 GEM_BUG_ON(!ce->pin_count);
910
Chris Wilsonef11c012016-12-18 15:37:19 +0000911 /* Flush enough space to reduce the likelihood of waiting after
912 * we start building the request - in which case we will just
913 * have to repeat work.
914 */
915 request->reserved_space += EXECLISTS_REQUEST_SIZE;
916
Chris Wilsonef11c012016-12-18 15:37:19 +0000917 if (i915.enable_guc_submission) {
918 /*
919 * Check that the GuC has space for the request before
920 * going any further, as the i915_add_request() call
921 * later on mustn't fail ...
922 */
923 ret = i915_guc_wq_reserve(request);
924 if (ret)
Chris Wilsone8a9c582016-12-18 15:37:20 +0000925 goto err;
Chris Wilsonef11c012016-12-18 15:37:19 +0000926 }
927
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000928 cs = intel_ring_begin(request, 0);
929 if (IS_ERR(cs)) {
930 ret = PTR_ERR(cs);
Chris Wilsonef11c012016-12-18 15:37:19 +0000931 goto err_unreserve;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000932 }
Chris Wilsonef11c012016-12-18 15:37:19 +0000933
934 if (!ce->initialised) {
935 ret = engine->init_context(request);
936 if (ret)
937 goto err_unreserve;
938
939 ce->initialised = true;
940 }
941
942 /* Note that after this point, we have committed to using
943 * this request as it is being used to both track the
944 * state of engine initialisation and liveness of the
945 * golden renderstate above. Think twice before you try
946 * to cancel/unwind this request now.
947 */
948
949 request->reserved_space -= EXECLISTS_REQUEST_SIZE;
950 return 0;
951
952err_unreserve:
953 if (i915.enable_guc_submission)
954 i915_guc_wq_unreserve(request);
Chris Wilsone8a9c582016-12-18 15:37:20 +0000955err:
Chris Wilsonef11c012016-12-18 15:37:19 +0000956 return ret;
957}
958
Arun Siluvery9e000842015-07-03 14:27:31 +0100959/*
960 * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
961 * PIPE_CONTROL instruction. This is required for the flush to happen correctly
962 * but there is a slight complication as this is applied in WA batch where the
963 * values are only initialized once so we cannot take register value at the
964 * beginning and reuse it further; hence we save its value to memory, upload a
965 * constant value with bit21 set and then we restore it back with the saved value.
966 * To simplify the WA, a constant value is formed by using the default value
967 * of this register. This shouldn't be a problem because we are only modifying
968 * it for a short period and this batch in non-premptible. We can ofcourse
969 * use additional instructions that read the actual value of the register
970 * at that time and set our bit of interest but it makes the WA complicated.
971 *
972 * This WA is also required for Gen9 so extracting as a function avoids
973 * code duplication.
974 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000975static u32 *
976gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery9e000842015-07-03 14:27:31 +0100977{
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000978 *batch++ = MI_STORE_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
979 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
980 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
981 *batch++ = 0;
Arun Siluvery9e000842015-07-03 14:27:31 +0100982
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000983 *batch++ = MI_LOAD_REGISTER_IMM(1);
984 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
985 *batch++ = 0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES;
Arun Siluvery9e000842015-07-03 14:27:31 +0100986
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000987 batch = gen8_emit_pipe_control(batch,
988 PIPE_CONTROL_CS_STALL |
989 PIPE_CONTROL_DC_FLUSH_ENABLE,
990 0);
Arun Siluvery9e000842015-07-03 14:27:31 +0100991
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000992 *batch++ = MI_LOAD_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
993 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
994 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
995 *batch++ = 0;
Arun Siluvery9e000842015-07-03 14:27:31 +0100996
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000997 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +0100998}
999
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001000/*
1001 * Typically we only have one indirect_ctx and per_ctx batch buffer which are
1002 * initialized at the beginning and shared across all contexts but this field
1003 * helps us to have multiple batches at different offsets and select them based
1004 * on a criteria. At the moment this batch always start at the beginning of the page
1005 * and at this point we don't have multiple wa_ctx batch buffers.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001006 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001007 * The number of WA applied are not known at the beginning; we use this field
1008 * to return the no of DWORDS written.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001009 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001010 * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
1011 * so it adds NOOPs as padding to make it cacheline aligned.
1012 * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
1013 * makes a complete batch buffer.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001014 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001015static u32 *gen8_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001016{
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001017 /* WaDisableCtxRestoreArbitration:bdw,chv */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001018 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001019
Arun Siluveryc82435b2015-06-19 18:37:13 +01001020 /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001021 if (IS_BROADWELL(engine->i915))
1022 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
Arun Siluveryc82435b2015-06-19 18:37:13 +01001023
Arun Siluvery0160f052015-06-23 15:46:57 +01001024 /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
1025 /* Actual scratch location is at 128 bytes offset */
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001026 batch = gen8_emit_pipe_control(batch,
1027 PIPE_CONTROL_FLUSH_L3 |
1028 PIPE_CONTROL_GLOBAL_GTT_IVB |
1029 PIPE_CONTROL_CS_STALL |
1030 PIPE_CONTROL_QW_WRITE,
1031 i915_ggtt_offset(engine->scratch) +
1032 2 * CACHELINE_BYTES);
Arun Siluvery0160f052015-06-23 15:46:57 +01001033
Arun Siluvery17ee9502015-06-19 19:07:01 +01001034 /* Pad to end of cacheline */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001035 while ((unsigned long)batch % CACHELINE_BYTES)
1036 *batch++ = MI_NOOP;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001037
1038 /*
1039 * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
1040 * execution depends on the length specified in terms of cache lines
1041 * in the register CTX_RCS_INDIRECT_CTX
1042 */
1043
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001044 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001045}
1046
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001047/*
1048 * This batch is started immediately after indirect_ctx batch. Since we ensure
1049 * that indirect_ctx ends on a cacheline this batch is aligned automatically.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001050 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001051 * The number of DWORDS written are returned using this field.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001052 *
1053 * This batch is terminated with MI_BATCH_BUFFER_END and so we need not add padding
1054 * to align it with cacheline as padding after MI_BATCH_BUFFER_END is redundant.
1055 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001056static u32 *gen8_init_perctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001057{
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001058 /* WaDisableCtxRestoreArbitration:bdw,chv */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001059 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1060 *batch++ = MI_BATCH_BUFFER_END;
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001061
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001062 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001063}
1064
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001065static u32 *gen9_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery0504cff2015-07-14 15:01:27 +01001066{
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001067 /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt,glk */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001068 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
Arun Siluverya4106a72015-07-14 15:01:29 +01001069
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001070 /* WaDisableGatherAtSetShaderCommonSlice:skl,bxt,kbl,glk */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001071 *batch++ = MI_LOAD_REGISTER_IMM(1);
1072 *batch++ = i915_mmio_reg_offset(COMMON_SLICE_CHICKEN2);
1073 *batch++ = _MASKED_BIT_DISABLE(
1074 GEN9_DISABLE_GATHER_AT_SET_SHADER_COMMON_SLICE);
1075 *batch++ = MI_NOOP;
Mika Kuoppala873e8172016-07-20 14:26:13 +03001076
Mika Kuoppala066d4622016-06-07 17:19:15 +03001077 /* WaClearSlmSpaceAtContextSwitch:kbl */
1078 /* Actual scratch location is at 128 bytes offset */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001079 if (IS_KBL_REVID(engine->i915, 0, KBL_REVID_A0)) {
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001080 batch = gen8_emit_pipe_control(batch,
1081 PIPE_CONTROL_FLUSH_L3 |
1082 PIPE_CONTROL_GLOBAL_GTT_IVB |
1083 PIPE_CONTROL_CS_STALL |
1084 PIPE_CONTROL_QW_WRITE,
1085 i915_ggtt_offset(engine->scratch)
1086 + 2 * CACHELINE_BYTES);
Mika Kuoppala066d4622016-06-07 17:19:15 +03001087 }
Tim Gore3485d992016-07-05 10:01:30 +01001088
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001089 /* WaMediaPoolStateCmdInWABB:bxt,glk */
Tim Gore3485d992016-07-05 10:01:30 +01001090 if (HAS_POOLED_EU(engine->i915)) {
1091 /*
1092 * EU pool configuration is setup along with golden context
1093 * during context initialization. This value depends on
1094 * device type (2x6 or 3x6) and needs to be updated based
1095 * on which subslice is disabled especially for 2x6
1096 * devices, however it is safe to load default
1097 * configuration of 3x6 device instead of masking off
1098 * corresponding bits because HW ignores bits of a disabled
1099 * subslice and drops down to appropriate config. Please
1100 * see render_state_setup() in i915_gem_render_state.c for
1101 * possible configurations, to avoid duplication they are
1102 * not shown here again.
1103 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001104 *batch++ = GEN9_MEDIA_POOL_STATE;
1105 *batch++ = GEN9_MEDIA_POOL_ENABLE;
1106 *batch++ = 0x00777000;
1107 *batch++ = 0;
1108 *batch++ = 0;
1109 *batch++ = 0;
Tim Gore3485d992016-07-05 10:01:30 +01001110 }
1111
Arun Siluvery0504cff2015-07-14 15:01:27 +01001112 /* Pad to end of cacheline */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001113 while ((unsigned long)batch % CACHELINE_BYTES)
1114 *batch++ = MI_NOOP;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001115
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001116 return batch;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001117}
1118
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001119static u32 *gen9_init_perctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery0504cff2015-07-14 15:01:27 +01001120{
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001121 *batch++ = MI_BATCH_BUFFER_END;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001122
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001123 return batch;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001124}
1125
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001126#define CTX_WA_BB_OBJ_SIZE (PAGE_SIZE)
1127
1128static int lrc_setup_wa_ctx(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001129{
Chris Wilson48bb74e2016-08-15 10:49:04 +01001130 struct drm_i915_gem_object *obj;
1131 struct i915_vma *vma;
1132 int err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001133
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001134 obj = i915_gem_object_create(engine->i915, CTX_WA_BB_OBJ_SIZE);
Chris Wilson48bb74e2016-08-15 10:49:04 +01001135 if (IS_ERR(obj))
1136 return PTR_ERR(obj);
1137
Chris Wilsona01cb372017-01-16 15:21:30 +00001138 vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
Chris Wilson48bb74e2016-08-15 10:49:04 +01001139 if (IS_ERR(vma)) {
1140 err = PTR_ERR(vma);
1141 goto err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001142 }
1143
Chris Wilson48bb74e2016-08-15 10:49:04 +01001144 err = i915_vma_pin(vma, 0, PAGE_SIZE, PIN_GLOBAL | PIN_HIGH);
1145 if (err)
1146 goto err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001147
Chris Wilson48bb74e2016-08-15 10:49:04 +01001148 engine->wa_ctx.vma = vma;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001149 return 0;
Chris Wilson48bb74e2016-08-15 10:49:04 +01001150
1151err:
1152 i915_gem_object_put(obj);
1153 return err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001154}
1155
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001156static void lrc_destroy_wa_ctx(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001157{
Chris Wilson19880c42016-08-15 10:49:05 +01001158 i915_vma_unpin_and_release(&engine->wa_ctx.vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001159}
1160
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001161typedef u32 *(*wa_bb_func_t)(struct intel_engine_cs *engine, u32 *batch);
1162
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001163static int intel_init_workaround_bb(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001164{
Chris Wilson48bb74e2016-08-15 10:49:04 +01001165 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001166 struct i915_wa_ctx_bb *wa_bb[2] = { &wa_ctx->indirect_ctx,
1167 &wa_ctx->per_ctx };
1168 wa_bb_func_t wa_bb_fn[2];
Arun Siluvery17ee9502015-06-19 19:07:01 +01001169 struct page *page;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001170 void *batch, *batch_ptr;
1171 unsigned int i;
Chris Wilson48bb74e2016-08-15 10:49:04 +01001172 int ret;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001173
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001174 if (WARN_ON(engine->id != RCS || !engine->scratch))
1175 return -EINVAL;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001176
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001177 switch (INTEL_GEN(engine->i915)) {
1178 case 9:
1179 wa_bb_fn[0] = gen9_init_indirectctx_bb;
1180 wa_bb_fn[1] = gen9_init_perctx_bb;
1181 break;
1182 case 8:
1183 wa_bb_fn[0] = gen8_init_indirectctx_bb;
1184 wa_bb_fn[1] = gen8_init_perctx_bb;
1185 break;
1186 default:
1187 MISSING_CASE(INTEL_GEN(engine->i915));
Arun Siluvery5e60d792015-06-23 15:50:44 +01001188 return 0;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001189 }
Arun Siluvery5e60d792015-06-23 15:50:44 +01001190
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001191 ret = lrc_setup_wa_ctx(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001192 if (ret) {
1193 DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
1194 return ret;
1195 }
1196
Chris Wilson48bb74e2016-08-15 10:49:04 +01001197 page = i915_gem_object_get_dirty_page(wa_ctx->vma->obj, 0);
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001198 batch = batch_ptr = kmap_atomic(page);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001199
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001200 /*
1201 * Emit the two workaround batch buffers, recording the offset from the
1202 * start of the workaround batch buffer object for each and their
1203 * respective sizes.
1204 */
1205 for (i = 0; i < ARRAY_SIZE(wa_bb_fn); i++) {
1206 wa_bb[i]->offset = batch_ptr - batch;
1207 if (WARN_ON(!IS_ALIGNED(wa_bb[i]->offset, CACHELINE_BYTES))) {
1208 ret = -EINVAL;
1209 break;
1210 }
1211 batch_ptr = wa_bb_fn[i](engine, batch_ptr);
1212 wa_bb[i]->size = batch_ptr - (batch + wa_bb[i]->offset);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001213 }
1214
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001215 BUG_ON(batch_ptr - batch > CTX_WA_BB_OBJ_SIZE);
1216
Arun Siluvery17ee9502015-06-19 19:07:01 +01001217 kunmap_atomic(batch);
1218 if (ret)
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001219 lrc_destroy_wa_ctx(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001220
1221 return ret;
1222}
1223
Chris Wilsond41a3c22017-08-07 13:19:19 +01001224static u8 gtiir[] = {
1225 [RCS] = 0,
1226 [BCS] = 0,
1227 [VCS] = 1,
1228 [VCS2] = 1,
1229 [VECS] = 3,
1230};
1231
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001232static int gen8_init_common_ring(struct intel_engine_cs *engine)
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001233{
Chris Wilsonc0336662016-05-06 15:40:21 +01001234 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson6b764a52017-04-25 11:38:35 +01001235 struct execlist_port *port = engine->execlist_port;
1236 unsigned int n;
Chris Wilson77f0d0e2017-05-17 13:10:00 +01001237 bool submit;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001238 int ret;
1239
1240 ret = intel_mocs_init_engine(engine);
1241 if (ret)
1242 return ret;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001243
Chris Wilsonad07dfc2016-10-07 07:53:26 +01001244 intel_engine_reset_breadcrumbs(engine);
Chris Wilsonf3b8f912017-01-05 15:30:21 +00001245 intel_engine_init_hangcheck(engine);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001246
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001247 I915_WRITE(RING_HWSTAM(engine->mmio_base), 0xffffffff);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001248 I915_WRITE(RING_MODE_GEN7(engine),
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001249 _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
Chris Wilsonf3b8f912017-01-05 15:30:21 +00001250 I915_WRITE(RING_HWS_PGA(engine->mmio_base),
1251 engine->status_page.ggtt_offset);
1252 POSTING_READ(RING_HWS_PGA(engine->mmio_base));
Michel Thierrydfc53c52015-09-28 13:25:12 +01001253
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001254 DRM_DEBUG_DRIVER("Execlists enabled for %s\n", engine->name);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001255
Chris Wilsond41a3c22017-08-07 13:19:19 +01001256 GEM_BUG_ON(engine->id >= ARRAY_SIZE(gtiir));
1257
1258 /*
1259 * Clear any pending interrupt state.
1260 *
1261 * We do it twice out of paranoia that some of the IIR are double
1262 * buffered, and if we only reset it once there may still be
1263 * an interrupt pending.
1264 */
1265 I915_WRITE(GEN8_GT_IIR(gtiir[engine->id]),
1266 GT_CONTEXT_SWITCH_INTERRUPT << engine->irq_shift);
1267 I915_WRITE(GEN8_GT_IIR(gtiir[engine->id]),
1268 GT_CONTEXT_SWITCH_INTERRUPT << engine->irq_shift);
Chris Wilsonf7470262017-01-24 15:20:21 +00001269 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
Chris Wilson6b764a52017-04-25 11:38:35 +01001270
Chris Wilsond41a3c22017-08-07 13:19:19 +01001271 /* After a GPU reset, we may have requests to replay */
Chris Wilson77f0d0e2017-05-17 13:10:00 +01001272 submit = false;
Chris Wilson6b764a52017-04-25 11:38:35 +01001273 for (n = 0; n < ARRAY_SIZE(engine->execlist_port); n++) {
Chris Wilson77f0d0e2017-05-17 13:10:00 +01001274 if (!port_isset(&port[n]))
Chris Wilson6b764a52017-04-25 11:38:35 +01001275 break;
1276
1277 DRM_DEBUG_DRIVER("Restarting %s:%d from 0x%x\n",
1278 engine->name, n,
Chris Wilson77f0d0e2017-05-17 13:10:00 +01001279 port_request(&port[n])->global_seqno);
Chris Wilson6b764a52017-04-25 11:38:35 +01001280
1281 /* Discard the current inflight count */
Chris Wilson77f0d0e2017-05-17 13:10:00 +01001282 port_set(&port[n], port_request(&port[n]));
1283 submit = true;
Chris Wilsonc87d50c2016-10-04 21:11:27 +01001284 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01001285
Chris Wilson77f0d0e2017-05-17 13:10:00 +01001286 if (submit && !i915.enable_guc_submission)
Chris Wilson6b764a52017-04-25 11:38:35 +01001287 execlists_submit_ports(engine);
1288
Chris Wilson821ed7d2016-09-09 14:11:53 +01001289 return 0;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001290}
1291
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001292static int gen8_init_render_ring(struct intel_engine_cs *engine)
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001293{
Chris Wilsonc0336662016-05-06 15:40:21 +01001294 struct drm_i915_private *dev_priv = engine->i915;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001295 int ret;
1296
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001297 ret = gen8_init_common_ring(engine);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001298 if (ret)
1299 return ret;
1300
1301 /* We need to disable the AsyncFlip performance optimisations in order
1302 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1303 * programmed to '1' on all products.
1304 *
1305 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
1306 */
1307 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1308
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001309 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1310
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001311 return init_workarounds_ring(engine);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001312}
1313
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001314static int gen9_init_render_ring(struct intel_engine_cs *engine)
Damien Lespiau82ef8222015-02-09 19:33:08 +00001315{
1316 int ret;
1317
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001318 ret = gen8_init_common_ring(engine);
Damien Lespiau82ef8222015-02-09 19:33:08 +00001319 if (ret)
1320 return ret;
1321
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001322 return init_workarounds_ring(engine);
Damien Lespiau82ef8222015-02-09 19:33:08 +00001323}
1324
Chris Wilson821ed7d2016-09-09 14:11:53 +01001325static void reset_common_ring(struct intel_engine_cs *engine,
1326 struct drm_i915_gem_request *request)
1327{
Chris Wilson821ed7d2016-09-09 14:11:53 +01001328 struct execlist_port *port = engine->execlist_port;
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001329 struct intel_context *ce;
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001330 unsigned int n;
1331
1332 /*
1333 * Catch up with any missed context-switch interrupts.
1334 *
1335 * Ideally we would just read the remaining CSB entries now that we
1336 * know the gpu is idle. However, the CSB registers are sometimes^W
1337 * often trashed across a GPU reset! Instead we have to rely on
1338 * guessing the missed context-switch events by looking at what
1339 * requests were completed.
1340 */
1341 if (!request) {
1342 for (n = 0; n < ARRAY_SIZE(engine->execlist_port); n++)
1343 i915_gem_request_put(port_request(&port[n]));
1344 memset(engine->execlist_port, 0, sizeof(engine->execlist_port));
1345 return;
1346 }
1347
1348 if (request->ctx != port_request(port)->ctx) {
1349 i915_gem_request_put(port_request(port));
1350 port[0] = port[1];
1351 memset(&port[1], 0, sizeof(port[1]));
1352 }
1353
1354 GEM_BUG_ON(request->ctx != port_request(port)->ctx);
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001355
1356 /* If the request was innocent, we leave the request in the ELSP
1357 * and will try to replay it on restarting. The context image may
1358 * have been corrupted by the reset, in which case we may have
1359 * to service a new GPU hang, but more likely we can continue on
1360 * without impact.
1361 *
1362 * If the request was guilty, we presume the context is corrupt
1363 * and have to at least restore the RING register in the context
1364 * image back to the expected values to skip over the guilty request.
1365 */
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001366 if (request->fence.error != -EIO)
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001367 return;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001368
Chris Wilsona3aabe82016-10-04 21:11:26 +01001369 /* We want a simple context + ring to execute the breadcrumb update.
1370 * We cannot rely on the context being intact across the GPU hang,
1371 * so clear it and rebuild just what we need for the breadcrumb.
1372 * All pending requests for this context will be zapped, and any
1373 * future request will be after userspace has had the opportunity
1374 * to recreate its own state.
1375 */
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001376 ce = &request->ctx->engine[engine->id];
Chris Wilsona3aabe82016-10-04 21:11:26 +01001377 execlists_init_reg_state(ce->lrc_reg_state,
1378 request->ctx, engine, ce->ring);
1379
Chris Wilson821ed7d2016-09-09 14:11:53 +01001380 /* Move the RING_HEAD onto the breadcrumb, past the hanging batch */
Chris Wilsona3aabe82016-10-04 21:11:26 +01001381 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
1382 i915_ggtt_offset(ce->ring->vma);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001383 ce->lrc_reg_state[CTX_RING_HEAD+1] = request->postfix;
Chris Wilsona3aabe82016-10-04 21:11:26 +01001384
Chris Wilson821ed7d2016-09-09 14:11:53 +01001385 request->ring->head = request->postfix;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001386 intel_ring_update_space(request->ring);
1387
Chris Wilsona3aabe82016-10-04 21:11:26 +01001388 /* Reset WaIdleLiteRestore:bdw,skl as well */
Chris Wilson450362d2017-03-27 14:00:07 +01001389 request->tail =
1390 intel_ring_wrap(request->ring,
1391 request->wa_tail - WA_TAIL_DWORDS*sizeof(u32));
Chris Wilsoned1501d2017-03-27 14:14:12 +01001392 assert_ring_tail_valid(request->ring, request->tail);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001393}
1394
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001395static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request *req)
1396{
1397 struct i915_hw_ppgtt *ppgtt = req->ctx->ppgtt;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001398 struct intel_engine_cs *engine = req->engine;
Mika Kuoppalae7167762017-02-28 17:28:10 +02001399 const int num_lri_cmds = GEN8_3LVL_PDPES * 2;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001400 u32 *cs;
1401 int i;
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001402
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001403 cs = intel_ring_begin(req, num_lri_cmds * 2 + 2);
1404 if (IS_ERR(cs))
1405 return PTR_ERR(cs);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001406
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001407 *cs++ = MI_LOAD_REGISTER_IMM(num_lri_cmds);
Mika Kuoppalae7167762017-02-28 17:28:10 +02001408 for (i = GEN8_3LVL_PDPES - 1; i >= 0; i--) {
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001409 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
1410
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001411 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, i));
1412 *cs++ = upper_32_bits(pd_daddr);
1413 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, i));
1414 *cs++ = lower_32_bits(pd_daddr);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001415 }
1416
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001417 *cs++ = MI_NOOP;
1418 intel_ring_advance(req, cs);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001419
1420 return 0;
1421}
1422
John Harrisonbe795fc2015-05-29 17:44:03 +01001423static int gen8_emit_bb_start(struct drm_i915_gem_request *req,
Chris Wilson803688b2016-08-02 22:50:27 +01001424 u64 offset, u32 len,
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001425 const unsigned int flags)
Oscar Mateo15648582014-07-24 17:04:32 +01001426{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001427 u32 *cs;
Oscar Mateo15648582014-07-24 17:04:32 +01001428 int ret;
1429
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001430 /* Don't rely in hw updating PDPs, specially in lite-restore.
1431 * Ideally, we should set Force PD Restore in ctx descriptor,
1432 * but we can't. Force Restore would be a second option, but
1433 * it is unsafe in case of lite-restore (because the ctx is
Michel Thierry2dba3232015-07-30 11:06:23 +01001434 * not idle). PML4 is allocated during ppgtt init so this is
1435 * not needed in 48-bit.*/
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001436 if (req->ctx->ppgtt &&
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001437 (intel_engine_flag(req->engine) & req->ctx->ppgtt->pd_dirty_rings) &&
1438 !i915_vm_is_48bit(&req->ctx->ppgtt->base) &&
1439 !intel_vgpu_active(req->i915)) {
1440 ret = intel_logical_ring_emit_pdps(req);
1441 if (ret)
1442 return ret;
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001443
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00001444 req->ctx->ppgtt->pd_dirty_rings &= ~intel_engine_flag(req->engine);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001445 }
1446
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001447 cs = intel_ring_begin(req, 4);
1448 if (IS_ERR(cs))
1449 return PTR_ERR(cs);
Oscar Mateo15648582014-07-24 17:04:32 +01001450
1451 /* FIXME(BDW): Address space and security selectors. */
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001452 *cs++ = MI_BATCH_BUFFER_START_GEN8 |
1453 (flags & I915_DISPATCH_SECURE ? 0 : BIT(8)) |
1454 (flags & I915_DISPATCH_RS ? MI_BATCH_RESOURCE_STREAMER : 0);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001455 *cs++ = lower_32_bits(offset);
1456 *cs++ = upper_32_bits(offset);
1457 *cs++ = MI_NOOP;
1458 intel_ring_advance(req, cs);
Oscar Mateo15648582014-07-24 17:04:32 +01001459
1460 return 0;
1461}
1462
Chris Wilson31bb59c2016-07-01 17:23:27 +01001463static void gen8_logical_ring_enable_irq(struct intel_engine_cs *engine)
Oscar Mateo73d477f2014-07-24 17:04:31 +01001464{
Chris Wilsonc0336662016-05-06 15:40:21 +01001465 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson31bb59c2016-07-01 17:23:27 +01001466 I915_WRITE_IMR(engine,
1467 ~(engine->irq_enable_mask | engine->irq_keep_mask));
1468 POSTING_READ_FW(RING_IMR(engine->mmio_base));
Oscar Mateo73d477f2014-07-24 17:04:31 +01001469}
1470
Chris Wilson31bb59c2016-07-01 17:23:27 +01001471static void gen8_logical_ring_disable_irq(struct intel_engine_cs *engine)
Oscar Mateo73d477f2014-07-24 17:04:31 +01001472{
Chris Wilsonc0336662016-05-06 15:40:21 +01001473 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson31bb59c2016-07-01 17:23:27 +01001474 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Oscar Mateo73d477f2014-07-24 17:04:31 +01001475}
1476
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001477static int gen8_emit_flush(struct drm_i915_gem_request *request, u32 mode)
Oscar Mateo47122742014-07-24 17:04:28 +01001478{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001479 u32 cmd, *cs;
Oscar Mateo47122742014-07-24 17:04:28 +01001480
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001481 cs = intel_ring_begin(request, 4);
1482 if (IS_ERR(cs))
1483 return PTR_ERR(cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001484
1485 cmd = MI_FLUSH_DW + 1;
1486
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001487 /* We always require a command barrier so that subsequent
1488 * commands, such as breadcrumb interrupts, are strictly ordered
1489 * wrt the contents of the write cache being flushed to memory
1490 * (and thus being coherent from the CPU).
1491 */
1492 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1493
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001494 if (mode & EMIT_INVALIDATE) {
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001495 cmd |= MI_INVALIDATE_TLB;
Chris Wilson1dae2df2016-08-02 22:50:19 +01001496 if (request->engine->id == VCS)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001497 cmd |= MI_INVALIDATE_BSD;
Oscar Mateo47122742014-07-24 17:04:28 +01001498 }
1499
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001500 *cs++ = cmd;
1501 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
1502 *cs++ = 0; /* upper addr */
1503 *cs++ = 0; /* value */
1504 intel_ring_advance(request, cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001505
1506 return 0;
1507}
1508
John Harrison7deb4d32015-05-29 17:43:59 +01001509static int gen8_emit_flush_render(struct drm_i915_gem_request *request,
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001510 u32 mode)
Oscar Mateo47122742014-07-24 17:04:28 +01001511{
Chris Wilsonb5321f32016-08-02 22:50:18 +01001512 struct intel_engine_cs *engine = request->engine;
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001513 u32 scratch_addr =
1514 i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001515 bool vf_flush_wa = false, dc_flush_wa = false;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001516 u32 *cs, flags = 0;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001517 int len;
Oscar Mateo47122742014-07-24 17:04:28 +01001518
1519 flags |= PIPE_CONTROL_CS_STALL;
1520
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001521 if (mode & EMIT_FLUSH) {
Oscar Mateo47122742014-07-24 17:04:28 +01001522 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
1523 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -08001524 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +01001525 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Oscar Mateo47122742014-07-24 17:04:28 +01001526 }
1527
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001528 if (mode & EMIT_INVALIDATE) {
Oscar Mateo47122742014-07-24 17:04:28 +01001529 flags |= PIPE_CONTROL_TLB_INVALIDATE;
1530 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
1531 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
1532 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
1533 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
1534 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
1535 flags |= PIPE_CONTROL_QW_WRITE;
1536 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Oscar Mateo47122742014-07-24 17:04:28 +01001537
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001538 /*
1539 * On GEN9: before VF_CACHE_INVALIDATE we need to emit a NULL
1540 * pipe control.
1541 */
Chris Wilsonc0336662016-05-06 15:40:21 +01001542 if (IS_GEN9(request->i915))
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001543 vf_flush_wa = true;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001544
1545 /* WaForGAMHang:kbl */
1546 if (IS_KBL_REVID(request->i915, 0, KBL_REVID_B0))
1547 dc_flush_wa = true;
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001548 }
Imre Deak9647ff32015-01-25 13:27:11 -08001549
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001550 len = 6;
1551
1552 if (vf_flush_wa)
1553 len += 6;
1554
1555 if (dc_flush_wa)
1556 len += 12;
1557
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001558 cs = intel_ring_begin(request, len);
1559 if (IS_ERR(cs))
1560 return PTR_ERR(cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001561
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001562 if (vf_flush_wa)
1563 cs = gen8_emit_pipe_control(cs, 0, 0);
Imre Deak9647ff32015-01-25 13:27:11 -08001564
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001565 if (dc_flush_wa)
1566 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_DC_FLUSH_ENABLE,
1567 0);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001568
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001569 cs = gen8_emit_pipe_control(cs, flags, scratch_addr);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001570
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001571 if (dc_flush_wa)
1572 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_CS_STALL, 0);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001573
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001574 intel_ring_advance(request, cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001575
1576 return 0;
1577}
1578
Chris Wilson7c17d372016-01-20 15:43:35 +02001579/*
1580 * Reserve space for 2 NOOPs at the end of each request to be
1581 * used as a workaround for not being allowed to do lite
1582 * restore with HEAD==TAIL (WaIdleLiteRestore).
1583 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001584static void gen8_emit_wa_tail(struct drm_i915_gem_request *request, u32 *cs)
Oscar Mateo4da46e12014-07-24 17:04:27 +01001585{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001586 *cs++ = MI_NOOP;
1587 *cs++ = MI_NOOP;
1588 request->wa_tail = intel_ring_offset(request, cs);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001589}
Oscar Mateo4da46e12014-07-24 17:04:27 +01001590
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001591static void gen8_emit_breadcrumb(struct drm_i915_gem_request *request, u32 *cs)
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001592{
Chris Wilson7c17d372016-01-20 15:43:35 +02001593 /* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */
1594 BUILD_BUG_ON(I915_GEM_HWS_INDEX_ADDR & (1 << 5));
Oscar Mateo4da46e12014-07-24 17:04:27 +01001595
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001596 *cs++ = (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW;
1597 *cs++ = intel_hws_seqno_address(request->engine) | MI_FLUSH_DW_USE_GTT;
1598 *cs++ = 0;
1599 *cs++ = request->global_seqno;
1600 *cs++ = MI_USER_INTERRUPT;
1601 *cs++ = MI_NOOP;
1602 request->tail = intel_ring_offset(request, cs);
Chris Wilsoned1501d2017-03-27 14:14:12 +01001603 assert_ring_tail_valid(request->ring, request->tail);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001604
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001605 gen8_emit_wa_tail(request, cs);
Chris Wilson7c17d372016-01-20 15:43:35 +02001606}
Oscar Mateo4da46e12014-07-24 17:04:27 +01001607
Chris Wilson98f29e82016-10-28 13:58:51 +01001608static const int gen8_emit_breadcrumb_sz = 6 + WA_TAIL_DWORDS;
1609
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001610static void gen8_emit_breadcrumb_render(struct drm_i915_gem_request *request,
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001611 u32 *cs)
Chris Wilson7c17d372016-01-20 15:43:35 +02001612{
Michał Winiarskice81a652016-04-12 15:51:55 +02001613 /* We're using qword write, seqno should be aligned to 8 bytes. */
1614 BUILD_BUG_ON(I915_GEM_HWS_INDEX & 1);
1615
Chris Wilson7c17d372016-01-20 15:43:35 +02001616 /* w/a for post sync ops following a GPGPU operation we
1617 * need a prior CS_STALL, which is emitted by the flush
1618 * following the batch.
Michel Thierry53292cd2015-04-15 18:11:33 +01001619 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001620 *cs++ = GFX_OP_PIPE_CONTROL(6);
1621 *cs++ = PIPE_CONTROL_GLOBAL_GTT_IVB | PIPE_CONTROL_CS_STALL |
1622 PIPE_CONTROL_QW_WRITE;
1623 *cs++ = intel_hws_seqno_address(request->engine);
1624 *cs++ = 0;
1625 *cs++ = request->global_seqno;
Michał Winiarskice81a652016-04-12 15:51:55 +02001626 /* We're thrashing one dword of HWS. */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001627 *cs++ = 0;
1628 *cs++ = MI_USER_INTERRUPT;
1629 *cs++ = MI_NOOP;
1630 request->tail = intel_ring_offset(request, cs);
Chris Wilsoned1501d2017-03-27 14:14:12 +01001631 assert_ring_tail_valid(request->ring, request->tail);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001632
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001633 gen8_emit_wa_tail(request, cs);
Oscar Mateo4da46e12014-07-24 17:04:27 +01001634}
1635
Chris Wilson98f29e82016-10-28 13:58:51 +01001636static const int gen8_emit_breadcrumb_render_sz = 8 + WA_TAIL_DWORDS;
1637
John Harrison87531812015-05-29 17:43:44 +01001638static int gen8_init_rcs_context(struct drm_i915_gem_request *req)
Thomas Daniele7778be2014-12-02 12:50:48 +00001639{
1640 int ret;
1641
Tvrtko Ursulin4ac96592017-02-14 15:00:17 +00001642 ret = intel_ring_workarounds_emit(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001643 if (ret)
1644 return ret;
1645
Peter Antoine3bbaba02015-07-10 20:13:11 +03001646 ret = intel_rcs_context_init_mocs(req);
1647 /*
1648 * Failing to program the MOCS is non-fatal.The system will not
1649 * run at peak performance. So generate an error and carry on.
1650 */
1651 if (ret)
1652 DRM_ERROR("MOCS failed to program: expect performance issues.\n");
1653
Chris Wilson4e50f082016-10-28 13:58:31 +01001654 return i915_gem_render_state_emit(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001655}
1656
Oscar Mateo73e4d072014-07-24 17:04:48 +01001657/**
1658 * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001659 * @engine: Engine Command Streamer.
Oscar Mateo73e4d072014-07-24 17:04:48 +01001660 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001661void intel_logical_ring_cleanup(struct intel_engine_cs *engine)
Oscar Mateo454afeb2014-07-24 17:04:22 +01001662{
John Harrison6402c332014-10-31 12:00:26 +00001663 struct drm_i915_private *dev_priv;
Oscar Mateo9832b9d2014-07-24 17:04:30 +01001664
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01001665 /*
1666 * Tasklet cannot be active at this point due intel_mark_active/idle
1667 * so this is just for documentation.
1668 */
1669 if (WARN_ON(test_bit(TASKLET_STATE_SCHED, &engine->irq_tasklet.state)))
1670 tasklet_kill(&engine->irq_tasklet);
1671
Chris Wilsonc0336662016-05-06 15:40:21 +01001672 dev_priv = engine->i915;
John Harrison6402c332014-10-31 12:00:26 +00001673
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001674 if (engine->buffer) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001675 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
Dave Gordonb0366a52015-12-08 15:02:36 +00001676 }
Oscar Mateo48d82382014-07-24 17:04:23 +01001677
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001678 if (engine->cleanup)
1679 engine->cleanup(engine);
Oscar Mateo48d82382014-07-24 17:04:23 +01001680
Chris Wilson57e88532016-08-15 10:48:57 +01001681 if (engine->status_page.vma) {
1682 i915_gem_object_unpin_map(engine->status_page.vma->obj);
1683 engine->status_page.vma = NULL;
Oscar Mateo48d82382014-07-24 17:04:23 +01001684 }
Chris Wilsone8a9c582016-12-18 15:37:20 +00001685
1686 intel_engine_cleanup_common(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001687
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001688 lrc_destroy_wa_ctx(engine);
Chris Wilsonc0336662016-05-06 15:40:21 +01001689 engine->i915 = NULL;
Akash Goel3b3f1652016-10-13 22:44:48 +05301690 dev_priv->engine[engine->id] = NULL;
1691 kfree(engine);
Oscar Mateo454afeb2014-07-24 17:04:22 +01001692}
1693
Chris Wilsonff44ad52017-03-16 17:13:03 +00001694static void execlists_set_default_submission(struct intel_engine_cs *engine)
Chris Wilsonddd66c52016-08-02 22:50:31 +01001695{
Chris Wilsonff44ad52017-03-16 17:13:03 +00001696 engine->submit_request = execlists_submit_request;
1697 engine->schedule = execlists_schedule;
Chris Wilsonc9203e82017-03-18 10:28:59 +00001698 engine->irq_tasklet.func = intel_lrc_irq_handler;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001699}
1700
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001701static void
Chris Wilsone1382ef2016-05-06 15:40:20 +01001702logical_ring_default_vfuncs(struct intel_engine_cs *engine)
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001703{
1704 /* Default vfuncs which can be overriden by each engine. */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001705 engine->init_hw = gen8_init_common_ring;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001706 engine->reset_hw = reset_common_ring;
Chris Wilsone8a9c582016-12-18 15:37:20 +00001707
1708 engine->context_pin = execlists_context_pin;
1709 engine->context_unpin = execlists_context_unpin;
1710
Chris Wilsonf73e7392016-12-18 15:37:24 +00001711 engine->request_alloc = execlists_request_alloc;
1712
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001713 engine->emit_flush = gen8_emit_flush;
Chris Wilson9b81d552016-10-28 13:58:50 +01001714 engine->emit_breadcrumb = gen8_emit_breadcrumb;
Chris Wilson98f29e82016-10-28 13:58:51 +01001715 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_sz;
Chris Wilsonff44ad52017-03-16 17:13:03 +00001716
1717 engine->set_default_submission = execlists_set_default_submission;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001718
Chris Wilson31bb59c2016-07-01 17:23:27 +01001719 engine->irq_enable = gen8_logical_ring_enable_irq;
1720 engine->irq_disable = gen8_logical_ring_disable_irq;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001721 engine->emit_bb_start = gen8_emit_bb_start;
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001722}
1723
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001724static inline void
Dave Gordonc2c7f242016-07-13 16:03:35 +01001725logical_ring_default_irqs(struct intel_engine_cs *engine)
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001726{
Dave Gordonc2c7f242016-07-13 16:03:35 +01001727 unsigned shift = engine->irq_shift;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001728 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << shift;
1729 engine->irq_keep_mask = GT_CONTEXT_SWITCH_INTERRUPT << shift;
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001730}
1731
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001732static int
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001733lrc_setup_hws(struct intel_engine_cs *engine, struct i915_vma *vma)
Tvrtko Ursulin04794ad2016-04-12 15:40:41 +01001734{
Chris Wilson57e88532016-08-15 10:48:57 +01001735 const int hws_offset = LRC_PPHWSP_PN * PAGE_SIZE;
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001736 void *hws;
Tvrtko Ursulin04794ad2016-04-12 15:40:41 +01001737
1738 /* The HWSP is part of the default context object in LRC mode. */
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001739 hws = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001740 if (IS_ERR(hws))
1741 return PTR_ERR(hws);
Chris Wilson57e88532016-08-15 10:48:57 +01001742
1743 engine->status_page.page_addr = hws + hws_offset;
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001744 engine->status_page.ggtt_offset = i915_ggtt_offset(vma) + hws_offset;
Chris Wilson57e88532016-08-15 10:48:57 +01001745 engine->status_page.vma = vma;
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001746
1747 return 0;
Tvrtko Ursulin04794ad2016-04-12 15:40:41 +01001748}
1749
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001750static void
1751logical_ring_setup(struct intel_engine_cs *engine)
1752{
1753 struct drm_i915_private *dev_priv = engine->i915;
1754 enum forcewake_domains fw_domains;
1755
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01001756 intel_engine_setup_common(engine);
1757
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001758 /* Intentionally left blank. */
1759 engine->buffer = NULL;
1760
1761 fw_domains = intel_uncore_forcewake_for_reg(dev_priv,
1762 RING_ELSP(engine),
1763 FW_REG_WRITE);
1764
1765 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
1766 RING_CONTEXT_STATUS_PTR(engine),
1767 FW_REG_READ | FW_REG_WRITE);
1768
1769 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
1770 RING_CONTEXT_STATUS_BUF_BASE(engine),
1771 FW_REG_READ);
1772
1773 engine->fw_domains = fw_domains;
1774
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001775 tasklet_init(&engine->irq_tasklet,
1776 intel_lrc_irq_handler, (unsigned long)engine);
1777
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001778 logical_ring_default_vfuncs(engine);
1779 logical_ring_default_irqs(engine);
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001780}
1781
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001782static int
1783logical_ring_init(struct intel_engine_cs *engine)
1784{
1785 struct i915_gem_context *dctx = engine->i915->kernel_context;
1786 int ret;
1787
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01001788 ret = intel_engine_init_common(engine);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001789 if (ret)
1790 goto error;
1791
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001792 /* And setup the hardware status page. */
1793 ret = lrc_setup_hws(engine, dctx->engine[engine->id].state);
1794 if (ret) {
1795 DRM_ERROR("Failed to set up hws %s: %d\n", engine->name, ret);
1796 goto error;
1797 }
1798
1799 return 0;
1800
1801error:
1802 intel_logical_ring_cleanup(engine);
1803 return ret;
1804}
1805
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +01001806int logical_render_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001807{
1808 struct drm_i915_private *dev_priv = engine->i915;
1809 int ret;
1810
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001811 logical_ring_setup(engine);
1812
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001813 if (HAS_L3_DPF(dev_priv))
1814 engine->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
1815
1816 /* Override some for render ring. */
1817 if (INTEL_GEN(dev_priv) >= 9)
1818 engine->init_hw = gen9_init_render_ring;
1819 else
1820 engine->init_hw = gen8_init_render_ring;
1821 engine->init_context = gen8_init_rcs_context;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001822 engine->emit_flush = gen8_emit_flush_render;
Chris Wilson9b81d552016-10-28 13:58:50 +01001823 engine->emit_breadcrumb = gen8_emit_breadcrumb_render;
Chris Wilson98f29e82016-10-28 13:58:51 +01001824 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_render_sz;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001825
Chris Wilsonf51455d2017-01-10 14:47:34 +00001826 ret = intel_engine_create_scratch(engine, PAGE_SIZE);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001827 if (ret)
1828 return ret;
1829
1830 ret = intel_init_workaround_bb(engine);
1831 if (ret) {
1832 /*
1833 * We continue even if we fail to initialize WA batch
1834 * because we only expect rare glitches but nothing
1835 * critical to prevent us from using GPU
1836 */
1837 DRM_ERROR("WA batch buffer initialization failed: %d\n",
1838 ret);
1839 }
1840
Tvrtko Ursulind038fc72016-12-16 13:18:42 +00001841 return logical_ring_init(engine);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001842}
1843
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +01001844int logical_xcs_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001845{
1846 logical_ring_setup(engine);
1847
1848 return logical_ring_init(engine);
1849}
1850
Jeff McGee0cea6502015-02-13 10:27:56 -06001851static u32
Chris Wilsonc0336662016-05-06 15:40:21 +01001852make_rpcs(struct drm_i915_private *dev_priv)
Jeff McGee0cea6502015-02-13 10:27:56 -06001853{
1854 u32 rpcs = 0;
1855
1856 /*
1857 * No explicit RPCS request is needed to ensure full
1858 * slice/subslice/EU enablement prior to Gen9.
1859 */
Chris Wilsonc0336662016-05-06 15:40:21 +01001860 if (INTEL_GEN(dev_priv) < 9)
Jeff McGee0cea6502015-02-13 10:27:56 -06001861 return 0;
1862
1863 /*
1864 * Starting in Gen9, render power gating can leave
1865 * slice/subslice/EU in a partially enabled state. We
1866 * must make an explicit request through RPCS for full
1867 * enablement.
1868 */
Imre Deak43b67992016-08-31 19:13:02 +03001869 if (INTEL_INFO(dev_priv)->sseu.has_slice_pg) {
Jeff McGee0cea6502015-02-13 10:27:56 -06001870 rpcs |= GEN8_RPCS_S_CNT_ENABLE;
Imre Deakf08a0c92016-08-31 19:13:04 +03001871 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.slice_mask) <<
Jeff McGee0cea6502015-02-13 10:27:56 -06001872 GEN8_RPCS_S_CNT_SHIFT;
1873 rpcs |= GEN8_RPCS_ENABLE;
1874 }
1875
Imre Deak43b67992016-08-31 19:13:02 +03001876 if (INTEL_INFO(dev_priv)->sseu.has_subslice_pg) {
Jeff McGee0cea6502015-02-13 10:27:56 -06001877 rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
Imre Deak57ec1712016-08-31 19:13:05 +03001878 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.subslice_mask) <<
Jeff McGee0cea6502015-02-13 10:27:56 -06001879 GEN8_RPCS_SS_CNT_SHIFT;
1880 rpcs |= GEN8_RPCS_ENABLE;
1881 }
1882
Imre Deak43b67992016-08-31 19:13:02 +03001883 if (INTEL_INFO(dev_priv)->sseu.has_eu_pg) {
1884 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
Jeff McGee0cea6502015-02-13 10:27:56 -06001885 GEN8_RPCS_EU_MIN_SHIFT;
Imre Deak43b67992016-08-31 19:13:02 +03001886 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
Jeff McGee0cea6502015-02-13 10:27:56 -06001887 GEN8_RPCS_EU_MAX_SHIFT;
1888 rpcs |= GEN8_RPCS_ENABLE;
1889 }
1890
1891 return rpcs;
1892}
1893
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001894static u32 intel_lr_indirect_ctx_offset(struct intel_engine_cs *engine)
Michel Thierry71562912016-02-23 10:31:49 +00001895{
1896 u32 indirect_ctx_offset;
1897
Chris Wilsonc0336662016-05-06 15:40:21 +01001898 switch (INTEL_GEN(engine->i915)) {
Michel Thierry71562912016-02-23 10:31:49 +00001899 default:
Chris Wilsonc0336662016-05-06 15:40:21 +01001900 MISSING_CASE(INTEL_GEN(engine->i915));
Michel Thierry71562912016-02-23 10:31:49 +00001901 /* fall through */
Michel Thierry7bd0a2c2017-06-06 13:30:38 -07001902 case 10:
1903 indirect_ctx_offset =
1904 GEN10_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
1905 break;
Michel Thierry71562912016-02-23 10:31:49 +00001906 case 9:
1907 indirect_ctx_offset =
1908 GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
1909 break;
1910 case 8:
1911 indirect_ctx_offset =
1912 GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
1913 break;
1914 }
1915
1916 return indirect_ctx_offset;
1917}
1918
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001919static void execlists_init_reg_state(u32 *regs,
Chris Wilsona3aabe82016-10-04 21:11:26 +01001920 struct i915_gem_context *ctx,
1921 struct intel_engine_cs *engine,
1922 struct intel_ring *ring)
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001923{
Chris Wilsona3aabe82016-10-04 21:11:26 +01001924 struct drm_i915_private *dev_priv = engine->i915;
1925 struct i915_hw_ppgtt *ppgtt = ctx->ppgtt ?: dev_priv->mm.aliasing_ppgtt;
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001926 u32 base = engine->mmio_base;
1927 bool rcs = engine->id == RCS;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001928
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001929 /* A context is actually a big batch buffer with several
1930 * MI_LOAD_REGISTER_IMM commands followed by (reg, value) pairs. The
1931 * values we are setting here are only for the first context restore:
1932 * on a subsequent save, the GPU will recreate this batchbuffer with new
1933 * values (including all the missing MI_LOAD_REGISTER_IMM commands that
1934 * we are not initializing here).
1935 */
1936 regs[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(rcs ? 14 : 11) |
1937 MI_LRI_FORCE_POSTED;
1938
1939 CTX_REG(regs, CTX_CONTEXT_CONTROL, RING_CONTEXT_CONTROL(engine),
1940 _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
1941 CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
1942 (HAS_RESOURCE_STREAMER(dev_priv) ?
1943 CTX_CTRL_RS_CTX_ENABLE : 0)));
1944 CTX_REG(regs, CTX_RING_HEAD, RING_HEAD(base), 0);
1945 CTX_REG(regs, CTX_RING_TAIL, RING_TAIL(base), 0);
1946 CTX_REG(regs, CTX_RING_BUFFER_START, RING_START(base), 0);
1947 CTX_REG(regs, CTX_RING_BUFFER_CONTROL, RING_CTL(base),
1948 RING_CTL_SIZE(ring->size) | RING_VALID);
1949 CTX_REG(regs, CTX_BB_HEAD_U, RING_BBADDR_UDW(base), 0);
1950 CTX_REG(regs, CTX_BB_HEAD_L, RING_BBADDR(base), 0);
1951 CTX_REG(regs, CTX_BB_STATE, RING_BBSTATE(base), RING_BB_PPGTT);
1952 CTX_REG(regs, CTX_SECOND_BB_HEAD_U, RING_SBBADDR_UDW(base), 0);
1953 CTX_REG(regs, CTX_SECOND_BB_HEAD_L, RING_SBBADDR(base), 0);
1954 CTX_REG(regs, CTX_SECOND_BB_STATE, RING_SBBSTATE(base), 0);
1955 if (rcs) {
1956 CTX_REG(regs, CTX_BB_PER_CTX_PTR, RING_BB_PER_CTX_PTR(base), 0);
1957 CTX_REG(regs, CTX_RCS_INDIRECT_CTX, RING_INDIRECT_CTX(base), 0);
1958 CTX_REG(regs, CTX_RCS_INDIRECT_CTX_OFFSET,
1959 RING_INDIRECT_CTX_OFFSET(base), 0);
1960
Chris Wilson48bb74e2016-08-15 10:49:04 +01001961 if (engine->wa_ctx.vma) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001962 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001963 u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001964
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001965 regs[CTX_RCS_INDIRECT_CTX + 1] =
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001966 (ggtt_offset + wa_ctx->indirect_ctx.offset) |
1967 (wa_ctx->indirect_ctx.size / CACHELINE_BYTES);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001968
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001969 regs[CTX_RCS_INDIRECT_CTX_OFFSET + 1] =
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001970 intel_lr_indirect_ctx_offset(engine) << 6;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001971
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001972 regs[CTX_BB_PER_CTX_PTR + 1] =
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001973 (ggtt_offset + wa_ctx->per_ctx.offset) | 0x01;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001974 }
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001975 }
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001976
1977 regs[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9) | MI_LRI_FORCE_POSTED;
1978
1979 CTX_REG(regs, CTX_CTX_TIMESTAMP, RING_CTX_TIMESTAMP(base), 0);
Ville Syrjälä0d925ea2015-11-04 23:20:11 +02001980 /* PDP values well be assigned later if needed */
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001981 CTX_REG(regs, CTX_PDP3_UDW, GEN8_RING_PDP_UDW(engine, 3), 0);
1982 CTX_REG(regs, CTX_PDP3_LDW, GEN8_RING_PDP_LDW(engine, 3), 0);
1983 CTX_REG(regs, CTX_PDP2_UDW, GEN8_RING_PDP_UDW(engine, 2), 0);
1984 CTX_REG(regs, CTX_PDP2_LDW, GEN8_RING_PDP_LDW(engine, 2), 0);
1985 CTX_REG(regs, CTX_PDP1_UDW, GEN8_RING_PDP_UDW(engine, 1), 0);
1986 CTX_REG(regs, CTX_PDP1_LDW, GEN8_RING_PDP_LDW(engine, 1), 0);
1987 CTX_REG(regs, CTX_PDP0_UDW, GEN8_RING_PDP_UDW(engine, 0), 0);
1988 CTX_REG(regs, CTX_PDP0_LDW, GEN8_RING_PDP_LDW(engine, 0), 0);
Michel Thierryd7b26332015-04-08 12:13:34 +01001989
Chris Wilson949e8ab2017-02-09 14:40:36 +00001990 if (ppgtt && i915_vm_is_48bit(&ppgtt->base)) {
Michel Thierry2dba3232015-07-30 11:06:23 +01001991 /* 64b PPGTT (48bit canonical)
1992 * PDP0_DESCRIPTOR contains the base address to PML4 and
1993 * other PDP Descriptors are ignored.
1994 */
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001995 ASSIGN_CTX_PML4(ppgtt, regs);
Michel Thierry2dba3232015-07-30 11:06:23 +01001996 }
1997
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001998 if (rcs) {
1999 regs[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
2000 CTX_REG(regs, CTX_R_PWR_CLK_STATE, GEN8_R_PWR_CLK_STATE,
2001 make_rpcs(dev_priv));
Robert Bragg19f81df2017-06-13 12:23:03 +01002002
2003 i915_oa_init_reg_state(engine, ctx, regs);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002004 }
Chris Wilsona3aabe82016-10-04 21:11:26 +01002005}
2006
2007static int
2008populate_lr_context(struct i915_gem_context *ctx,
2009 struct drm_i915_gem_object *ctx_obj,
2010 struct intel_engine_cs *engine,
2011 struct intel_ring *ring)
2012{
2013 void *vaddr;
2014 int ret;
2015
2016 ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
2017 if (ret) {
2018 DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
2019 return ret;
2020 }
2021
2022 vaddr = i915_gem_object_pin_map(ctx_obj, I915_MAP_WB);
2023 if (IS_ERR(vaddr)) {
2024 ret = PTR_ERR(vaddr);
2025 DRM_DEBUG_DRIVER("Could not map object pages! (%d)\n", ret);
2026 return ret;
2027 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002028 ctx_obj->mm.dirty = true;
Chris Wilsona3aabe82016-10-04 21:11:26 +01002029
2030 /* The second page of the context object contains some fields which must
2031 * be set up prior to the first execution. */
2032
2033 execlists_init_reg_state(vaddr + LRC_STATE_PN * PAGE_SIZE,
2034 ctx, engine, ring);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002035
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01002036 i915_gem_object_unpin_map(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002037
2038 return 0;
2039}
2040
Chris Wilsone2efd132016-05-24 14:53:34 +01002041static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
Chris Wilson978f1e02016-04-28 09:56:54 +01002042 struct intel_engine_cs *engine)
Oscar Mateoede7d422014-07-24 17:04:12 +01002043{
Oscar Mateo8c8579172014-07-24 17:04:14 +01002044 struct drm_i915_gem_object *ctx_obj;
Chris Wilson9021ad02016-05-24 14:53:37 +01002045 struct intel_context *ce = &ctx->engine[engine->id];
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002046 struct i915_vma *vma;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002047 uint32_t context_size;
Chris Wilson7e37f882016-08-02 22:50:21 +01002048 struct intel_ring *ring;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002049 int ret;
2050
Chris Wilson9021ad02016-05-24 14:53:37 +01002051 WARN_ON(ce->state);
Oscar Mateoede7d422014-07-24 17:04:12 +01002052
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +03002053 context_size = round_up(engine->context_size, I915_GTT_PAGE_SIZE);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002054
Alex Daid1675192015-08-12 15:43:43 +01002055 /* One extra page as the sharing data between driver and GuC */
2056 context_size += PAGE_SIZE * LRC_PPHWSP_PN;
2057
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00002058 ctx_obj = i915_gem_object_create(ctx->i915, context_size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01002059 if (IS_ERR(ctx_obj)) {
Dan Carpenter3126a662015-04-30 17:30:50 +03002060 DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
Chris Wilsonfe3db792016-04-25 13:32:13 +01002061 return PTR_ERR(ctx_obj);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002062 }
2063
Chris Wilsona01cb372017-01-16 15:21:30 +00002064 vma = i915_vma_instance(ctx_obj, &ctx->i915->ggtt.base, NULL);
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002065 if (IS_ERR(vma)) {
2066 ret = PTR_ERR(vma);
2067 goto error_deref_obj;
2068 }
2069
Chris Wilson7e37f882016-08-02 22:50:21 +01002070 ring = intel_engine_create_ring(engine, ctx->ring_size);
Chris Wilsondca33ec2016-08-02 22:50:20 +01002071 if (IS_ERR(ring)) {
2072 ret = PTR_ERR(ring);
Nick Hoathe84fe802015-09-11 12:53:46 +01002073 goto error_deref_obj;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002074 }
2075
Chris Wilsondca33ec2016-08-02 22:50:20 +01002076 ret = populate_lr_context(ctx, ctx_obj, engine, ring);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002077 if (ret) {
2078 DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
Chris Wilsondca33ec2016-08-02 22:50:20 +01002079 goto error_ring_free;
Oscar Mateo84c23772014-07-24 17:04:15 +01002080 }
2081
Chris Wilsondca33ec2016-08-02 22:50:20 +01002082 ce->ring = ring;
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002083 ce->state = vma;
Chuanxiao Dong0d402a22017-05-11 18:07:42 +08002084 ce->initialised |= engine->init_context == NULL;
Oscar Mateoede7d422014-07-24 17:04:12 +01002085
2086 return 0;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002087
Chris Wilsondca33ec2016-08-02 22:50:20 +01002088error_ring_free:
Chris Wilson7e37f882016-08-02 22:50:21 +01002089 intel_ring_free(ring);
Nick Hoathe84fe802015-09-11 12:53:46 +01002090error_deref_obj:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002091 i915_gem_object_put(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002092 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01002093}
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002094
Chris Wilson821ed7d2016-09-09 14:11:53 +01002095void intel_lr_context_resume(struct drm_i915_private *dev_priv)
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002096{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002097 struct intel_engine_cs *engine;
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002098 struct i915_gem_context *ctx;
Akash Goel3b3f1652016-10-13 22:44:48 +05302099 enum intel_engine_id id;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002100
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002101 /* Because we emit WA_TAIL_DWORDS there may be a disparity
2102 * between our bookkeeping in ce->ring->head and ce->ring->tail and
2103 * that stored in context. As we only write new commands from
2104 * ce->ring->tail onwards, everything before that is junk. If the GPU
2105 * starts reading from its RING_HEAD from the context, it may try to
2106 * execute that junk and die.
2107 *
2108 * So to avoid that we reset the context images upon resume. For
2109 * simplicity, we just zero everything out.
2110 */
Chris Wilson829a0af2017-06-20 12:05:45 +01002111 list_for_each_entry(ctx, &dev_priv->contexts.list, link) {
Akash Goel3b3f1652016-10-13 22:44:48 +05302112 for_each_engine(engine, dev_priv, id) {
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002113 struct intel_context *ce = &ctx->engine[engine->id];
2114 u32 *reg;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002115
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002116 if (!ce->state)
2117 continue;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002118
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002119 reg = i915_gem_object_pin_map(ce->state->obj,
2120 I915_MAP_WB);
2121 if (WARN_ON(IS_ERR(reg)))
2122 continue;
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01002123
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002124 reg += LRC_STATE_PN * PAGE_SIZE / sizeof(*reg);
2125 reg[CTX_RING_HEAD+1] = 0;
2126 reg[CTX_RING_TAIL+1] = 0;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002127
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002128 ce->state->obj->mm.dirty = true;
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002129 i915_gem_object_unpin_map(ce->state->obj);
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002130
Chris Wilsone6ba9992017-04-25 14:00:49 +01002131 intel_ring_reset(ce->ring, 0);
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002132 }
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002133 }
2134}