blob: e1195b975ef62e646729947cba6b82f14a65ff2e [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
Michael H. Nguyen468c6812014-11-13 17:51:49 +0000141#define GEN9_LR_CONTEXT_RENDER_SIZE (22 * PAGE_SIZE)
Oscar Mateo8c8579172014-07-24 17:04:14 +0100142#define GEN8_LR_CONTEXT_RENDER_SIZE (20 * PAGE_SIZE)
143#define GEN8_LR_CONTEXT_OTHER_SIZE (2 * PAGE_SIZE)
144
Thomas Daniele981e7b2014-07-24 17:04:39 +0100145#define RING_EXECLIST_QFULL (1 << 0x2)
146#define RING_EXECLIST1_VALID (1 << 0x3)
147#define RING_EXECLIST0_VALID (1 << 0x4)
148#define RING_EXECLIST_ACTIVE_STATUS (3 << 0xE)
149#define RING_EXECLIST1_ACTIVE (1 << 0x11)
150#define RING_EXECLIST0_ACTIVE (1 << 0x12)
151
152#define GEN8_CTX_STATUS_IDLE_ACTIVE (1 << 0)
153#define GEN8_CTX_STATUS_PREEMPTED (1 << 1)
154#define GEN8_CTX_STATUS_ELEMENT_SWITCH (1 << 2)
155#define GEN8_CTX_STATUS_ACTIVE_IDLE (1 << 3)
156#define GEN8_CTX_STATUS_COMPLETE (1 << 4)
157#define GEN8_CTX_STATUS_LITE_RESTORE (1 << 15)
Oscar Mateo8670d6f2014-07-24 17:04:17 +0100158
Chris Wilson70c2a242016-09-09 14:11:46 +0100159#define GEN8_CTX_STATUS_COMPLETED_MASK \
160 (GEN8_CTX_STATUS_ACTIVE_IDLE | \
161 GEN8_CTX_STATUS_PREEMPTED | \
162 GEN8_CTX_STATUS_ELEMENT_SWITCH)
163
Oscar Mateo8670d6f2014-07-24 17:04:17 +0100164#define CTX_LRI_HEADER_0 0x01
165#define CTX_CONTEXT_CONTROL 0x02
166#define CTX_RING_HEAD 0x04
167#define CTX_RING_TAIL 0x06
168#define CTX_RING_BUFFER_START 0x08
169#define CTX_RING_BUFFER_CONTROL 0x0a
170#define CTX_BB_HEAD_U 0x0c
171#define CTX_BB_HEAD_L 0x0e
172#define CTX_BB_STATE 0x10
173#define CTX_SECOND_BB_HEAD_U 0x12
174#define CTX_SECOND_BB_HEAD_L 0x14
175#define CTX_SECOND_BB_STATE 0x16
176#define CTX_BB_PER_CTX_PTR 0x18
177#define CTX_RCS_INDIRECT_CTX 0x1a
178#define CTX_RCS_INDIRECT_CTX_OFFSET 0x1c
179#define CTX_LRI_HEADER_1 0x21
180#define CTX_CTX_TIMESTAMP 0x22
181#define CTX_PDP3_UDW 0x24
182#define CTX_PDP3_LDW 0x26
183#define CTX_PDP2_UDW 0x28
184#define CTX_PDP2_LDW 0x2a
185#define CTX_PDP1_UDW 0x2c
186#define CTX_PDP1_LDW 0x2e
187#define CTX_PDP0_UDW 0x30
188#define CTX_PDP0_LDW 0x32
189#define CTX_LRI_HEADER_2 0x41
190#define CTX_R_PWR_CLK_STATE 0x42
191#define CTX_GPGPU_CSR_BASE_ADDRESS 0x44
192
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +0000193#define CTX_REG(reg_state, pos, reg, val) do { \
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200194 (reg_state)[(pos)+0] = i915_mmio_reg_offset(reg); \
Ville Syrjälä0d925ea2015-11-04 23:20:11 +0200195 (reg_state)[(pos)+1] = (val); \
196} while (0)
197
198#define ASSIGN_CTX_PDP(ppgtt, reg_state, n) do { \
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300199 const u64 _addr = i915_page_dir_dma_addr((ppgtt), (n)); \
Michel Thierrye5815a22015-04-08 12:13:32 +0100200 reg_state[CTX_PDP ## n ## _UDW+1] = upper_32_bits(_addr); \
201 reg_state[CTX_PDP ## n ## _LDW+1] = lower_32_bits(_addr); \
Ville Syrjälä9244a812015-11-04 23:20:09 +0200202} while (0)
Michel Thierrye5815a22015-04-08 12:13:32 +0100203
Ville Syrjälä9244a812015-11-04 23:20:09 +0200204#define ASSIGN_CTX_PML4(ppgtt, reg_state) do { \
Michel Thierry2dba3232015-07-30 11:06:23 +0100205 reg_state[CTX_PDP0_UDW + 1] = upper_32_bits(px_dma(&ppgtt->pml4)); \
206 reg_state[CTX_PDP0_LDW + 1] = lower_32_bits(px_dma(&ppgtt->pml4)); \
Ville Syrjälä9244a812015-11-04 23:20:09 +0200207} while (0)
Michel Thierry2dba3232015-07-30 11:06:23 +0100208
Michel Thierry71562912016-02-23 10:31:49 +0000209#define GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x17
210#define GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x26
Ben Widawsky84b790f2014-07-24 17:04:36 +0100211
Chris Wilson0e93cdd2016-04-29 09:07:06 +0100212/* Typical size of the average request (2 pipecontrols and a MI_BB) */
213#define EXECLISTS_REQUEST_SIZE 64 /* bytes */
214
Chris Wilsona3aabe82016-10-04 21:11:26 +0100215#define WA_TAIL_DWORDS 2
216
Chris Wilsone2efd132016-05-24 14:53:34 +0100217static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
Chris Wilson978f1e02016-04-28 09:56:54 +0100218 struct intel_engine_cs *engine);
Chris Wilsona3aabe82016-10-04 21:11:26 +0100219static void execlists_init_reg_state(u32 *reg_state,
220 struct i915_gem_context *ctx,
221 struct intel_engine_cs *engine,
222 struct intel_ring *ring);
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000223
Oscar Mateo73e4d072014-07-24 17:04:48 +0100224/**
225 * intel_sanitize_enable_execlists() - sanitize i915.enable_execlists
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100226 * @dev_priv: i915 device private
Oscar Mateo73e4d072014-07-24 17:04:48 +0100227 * @enable_execlists: value of i915.enable_execlists module parameter.
228 *
229 * Only certain platforms support Execlists (the prerequisites being
Thomas Daniel27401d12014-12-11 12:48:35 +0000230 * support for Logical Ring Contexts and Aliasing PPGTT or better).
Oscar Mateo73e4d072014-07-24 17:04:48 +0100231 *
232 * Return: 1 if Execlists is supported and has to be enabled.
233 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100234int intel_sanitize_enable_execlists(struct drm_i915_private *dev_priv, int enable_execlists)
Oscar Mateo127f1002014-07-24 17:04:11 +0100235{
Zhiyuan Lva0bd6c32015-08-28 15:41:16 +0800236 /* On platforms with execlist available, vGPU will only
237 * support execlist mode, no ring buffer mode.
238 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100239 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv) && intel_vgpu_active(dev_priv))
Zhiyuan Lva0bd6c32015-08-28 15:41:16 +0800240 return 1;
241
Chris Wilsonc0336662016-05-06 15:40:21 +0100242 if (INTEL_GEN(dev_priv) >= 9)
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000243 return 1;
244
Oscar Mateo127f1002014-07-24 17:04:11 +0100245 if (enable_execlists == 0)
246 return 0;
247
Daniel Vetter5a21b662016-05-24 17:13:53 +0200248 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv) &&
249 USES_PPGTT(dev_priv) &&
250 i915.use_mmio_flip >= 0)
Oscar Mateo127f1002014-07-24 17:04:11 +0100251 return 1;
252
253 return 0;
254}
Oscar Mateoede7d422014-07-24 17:04:12 +0100255
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000256/**
257 * intel_lr_context_descriptor_update() - calculate & cache the descriptor
258 * descriptor for a pinned context
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000259 * @ctx: Context to work on
Chris Wilson9021ad02016-05-24 14:53:37 +0100260 * @engine: Engine the descriptor will be used with
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000261 *
262 * The context descriptor encodes various attributes of a context,
263 * including its GTT address and some flags. Because it's fairly
264 * expensive to calculate, we'll just do it once and cache the result,
265 * which remains valid until the context is unpinned.
266 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200267 * This is what a descriptor looks like, from LSB to MSB::
268 *
Mika Kuoppala2355cf02017-01-27 15:03:09 +0200269 * bits 0-11: flags, GEN8_CTX_* (cached in ctx->desc_template)
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200270 * bits 12-31: LRCA, GTT address of (the HWSP of) this context
271 * bits 32-52: ctx ID, a globally unique tag
272 * bits 53-54: mbz, reserved for use by hardware
273 * bits 55-63: group ID, currently unused and set to 0
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000274 */
275static void
Chris Wilsone2efd132016-05-24 14:53:34 +0100276intel_lr_context_descriptor_update(struct i915_gem_context *ctx,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000277 struct intel_engine_cs *engine)
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000278{
Chris Wilson9021ad02016-05-24 14:53:37 +0100279 struct intel_context *ce = &ctx->engine[engine->id];
Chris Wilson7069b142016-04-28 09:56:52 +0100280 u64 desc;
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000281
Chris Wilson7069b142016-04-28 09:56:52 +0100282 BUILD_BUG_ON(MAX_CONTEXT_HW_ID > (1<<GEN8_CTX_ID_WIDTH));
283
Mika Kuoppala2355cf02017-01-27 15:03:09 +0200284 desc = ctx->desc_template; /* bits 0-11 */
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100285 desc |= i915_ggtt_offset(ce->state) + LRC_PPHWSP_PN * PAGE_SIZE;
Chris Wilson9021ad02016-05-24 14:53:37 +0100286 /* bits 12-31 */
Chris Wilson7069b142016-04-28 09:56:52 +0100287 desc |= (u64)ctx->hw_id << GEN8_CTX_ID_SHIFT; /* bits 32-52 */
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000288
Chris Wilson9021ad02016-05-24 14:53:37 +0100289 ce->lrc_desc = desc;
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000290}
291
Chris Wilsone2efd132016-05-24 14:53:34 +0100292uint64_t intel_lr_context_descriptor(struct i915_gem_context *ctx,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000293 struct intel_engine_cs *engine)
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000294{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000295 return ctx->engine[engine->id].lrc_desc;
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000296}
297
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100298static inline void
299execlists_context_status_change(struct drm_i915_gem_request *rq,
300 unsigned long status)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100301{
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100302 /*
303 * Only used when GVT-g is enabled now. When GVT-g is disabled,
304 * The compiler should eliminate this function as dead-code.
305 */
306 if (!IS_ENABLED(CONFIG_DRM_I915_GVT))
307 return;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100308
Changbin Du3fc03062017-03-13 10:47:11 +0800309 atomic_notifier_call_chain(&rq->engine->context_status_notifier,
310 status, rq);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100311}
312
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000313static void
314execlists_update_context_pdps(struct i915_hw_ppgtt *ppgtt, u32 *reg_state)
315{
316 ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
317 ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
318 ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
319 ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
320}
321
Chris Wilson70c2a242016-09-09 14:11:46 +0100322static u64 execlists_update_context(struct drm_i915_gem_request *rq)
Oscar Mateoae1250b2014-07-24 17:04:37 +0100323{
Chris Wilson70c2a242016-09-09 14:11:46 +0100324 struct intel_context *ce = &rq->ctx->engine[rq->engine->id];
Zhi Wang04da8112017-02-06 18:37:16 +0800325 struct i915_hw_ppgtt *ppgtt =
326 rq->ctx->ppgtt ?: rq->i915->mm.aliasing_ppgtt;
Chris Wilson70c2a242016-09-09 14:11:46 +0100327 u32 *reg_state = ce->lrc_reg_state;
Oscar Mateoae1250b2014-07-24 17:04:37 +0100328
Chris Wilson944a36d2017-02-17 16:38:33 +0000329 GEM_BUG_ON(!IS_ALIGNED(rq->tail, 8));
Chris Wilsoncaddfe72016-10-28 13:58:52 +0100330 reg_state[CTX_RING_TAIL+1] = rq->tail;
Oscar Mateoae1250b2014-07-24 17:04:37 +0100331
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000332 /* True 32b PPGTT with dynamic page allocation: update PDP
333 * registers and point the unallocated PDPs to scratch page.
334 * PML4 is allocated during ppgtt init, so this is not needed
335 * in 48-bit mode.
336 */
Chris Wilson949e8ab2017-02-09 14:40:36 +0000337 if (ppgtt && !i915_vm_is_48bit(&ppgtt->base))
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000338 execlists_update_context_pdps(ppgtt, reg_state);
Chris Wilson70c2a242016-09-09 14:11:46 +0100339
340 return ce->lrc_desc;
Oscar Mateoae1250b2014-07-24 17:04:37 +0100341}
342
Chris Wilson70c2a242016-09-09 14:11:46 +0100343static void execlists_submit_ports(struct intel_engine_cs *engine)
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100344{
Chris Wilson70c2a242016-09-09 14:11:46 +0100345 struct drm_i915_private *dev_priv = engine->i915;
346 struct execlist_port *port = engine->execlist_port;
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100347 u32 __iomem *elsp =
348 dev_priv->regs + i915_mmio_reg_offset(RING_ELSP(engine));
349 u64 desc[2];
350
Chris Wilsonc816e602017-01-24 11:00:02 +0000351 GEM_BUG_ON(port[0].count > 1);
Chris Wilson70c2a242016-09-09 14:11:46 +0100352 if (!port[0].count)
353 execlists_context_status_change(port[0].request,
354 INTEL_CONTEXT_SCHEDULE_IN);
355 desc[0] = execlists_update_context(port[0].request);
Chris Wilsonae9a0432017-02-07 10:23:19 +0000356 GEM_DEBUG_EXEC(port[0].context_id = upper_32_bits(desc[0]));
Chris Wilson816ee792017-01-24 11:00:03 +0000357 port[0].count++;
Chris Wilson70c2a242016-09-09 14:11:46 +0100358
359 if (port[1].request) {
360 GEM_BUG_ON(port[1].count);
361 execlists_context_status_change(port[1].request,
362 INTEL_CONTEXT_SCHEDULE_IN);
363 desc[1] = execlists_update_context(port[1].request);
Chris Wilsonae9a0432017-02-07 10:23:19 +0000364 GEM_DEBUG_EXEC(port[1].context_id = upper_32_bits(desc[1]));
Chris Wilson70c2a242016-09-09 14:11:46 +0100365 port[1].count = 1;
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100366 } else {
367 desc[1] = 0;
368 }
Chris Wilson70c2a242016-09-09 14:11:46 +0100369 GEM_BUG_ON(desc[0] == desc[1]);
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100370
371 /* You must always write both descriptors in the order below. */
372 writel(upper_32_bits(desc[1]), elsp);
373 writel(lower_32_bits(desc[1]), elsp);
374
375 writel(upper_32_bits(desc[0]), elsp);
376 /* The context is automatically loaded after the following */
377 writel(lower_32_bits(desc[0]), elsp);
378}
379
Chris Wilson70c2a242016-09-09 14:11:46 +0100380static bool ctx_single_port_submission(const struct i915_gem_context *ctx)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100381{
Chris Wilson70c2a242016-09-09 14:11:46 +0100382 return (IS_ENABLED(CONFIG_DRM_I915_GVT) &&
Chris Wilson60958682016-12-31 11:20:11 +0000383 i915_gem_context_force_single_submission(ctx));
Ben Widawsky84b790f2014-07-24 17:04:36 +0100384}
385
Chris Wilson70c2a242016-09-09 14:11:46 +0100386static bool can_merge_ctx(const struct i915_gem_context *prev,
387 const struct i915_gem_context *next)
Michel Thierryacdd8842014-07-24 17:04:38 +0100388{
Chris Wilson70c2a242016-09-09 14:11:46 +0100389 if (prev != next)
390 return false;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100391
Chris Wilson70c2a242016-09-09 14:11:46 +0100392 if (ctx_single_port_submission(prev))
393 return false;
Michel Thierryacdd8842014-07-24 17:04:38 +0100394
Chris Wilson70c2a242016-09-09 14:11:46 +0100395 return true;
396}
Peter Antoine779949f2015-05-11 16:03:27 +0100397
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 Wilsond55ac5b2016-11-14 20:40:59 +0000402 unsigned long flags;
Chris Wilson20311bd2016-11-14 20:41:03 +0000403 struct rb_node *rb;
Chris Wilson70c2a242016-09-09 14:11:46 +0100404 bool submit = false;
Michel Thierryacdd8842014-07-24 17:04:38 +0100405
Chris Wilson70c2a242016-09-09 14:11:46 +0100406 last = port->request;
407 if (last)
408 /* WaIdleLiteRestore:bdw,skl
409 * Apply the wa NOOPs to prevent ring:HEAD == req:TAIL
Chris Wilson9b81d552016-10-28 13:58:50 +0100410 * as we resubmit the request. See gen8_emit_breadcrumb()
Chris Wilson70c2a242016-09-09 14:11:46 +0100411 * for where we prepare the padding after the end of the
412 * request.
Michel Thierry53292cd2015-04-15 18:11:33 +0100413 */
Chris Wilson70c2a242016-09-09 14:11:46 +0100414 last->tail = last->wa_tail;
415
416 GEM_BUG_ON(port[1].request);
417
418 /* Hardware submission is through 2 ports. Conceptually each port
419 * has a (RING_START, RING_HEAD, RING_TAIL) tuple. RING_START is
420 * static for a context, and unique to each, so we only execute
421 * requests belonging to a single context from each ring. RING_HEAD
422 * is maintained by the CS in the context image, it marks the place
423 * where it got up to last time, and through RING_TAIL we tell the CS
424 * where we want to execute up to this time.
425 *
426 * In this list the requests are in order of execution. Consecutive
427 * requests from the same context are adjacent in the ringbuffer. We
428 * can combine these requests into a single RING_TAIL update:
429 *
430 * RING_HEAD...req1...req2
431 * ^- RING_TAIL
432 * since to execute req2 the CS must first execute req1.
433 *
434 * Our goal then is to point each port to the end of a consecutive
435 * sequence of requests as being the most optimal (fewest wake ups
436 * and context switches) submission.
437 */
438
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000439 spin_lock_irqsave(&engine->timeline->lock, flags);
Chris Wilson20311bd2016-11-14 20:41:03 +0000440 rb = engine->execlist_first;
441 while (rb) {
442 struct drm_i915_gem_request *cursor =
443 rb_entry(rb, typeof(*cursor), priotree.node);
444
Chris Wilson70c2a242016-09-09 14:11:46 +0100445 /* Can we combine this request with the current port? It has to
446 * be the same context/ringbuffer and not have any exceptions
447 * (e.g. GVT saying never to combine contexts).
448 *
449 * If we can combine the requests, we can execute both by
450 * updating the RING_TAIL to point to the end of the second
451 * request, and so we never need to tell the hardware about
452 * the first.
453 */
454 if (last && !can_merge_ctx(cursor->ctx, last->ctx)) {
455 /* If we are on the second port and cannot combine
456 * this request with the last, then we are done.
457 */
458 if (port != engine->execlist_port)
459 break;
460
461 /* If GVT overrides us we only ever submit port[0],
462 * leaving port[1] empty. Note that we also have
463 * to be careful that we don't queue the same
464 * context (even though a different request) to
465 * the second port.
466 */
Min Hed7ab9922016-11-16 22:05:04 +0800467 if (ctx_single_port_submission(last->ctx) ||
468 ctx_single_port_submission(cursor->ctx))
Chris Wilson70c2a242016-09-09 14:11:46 +0100469 break;
470
471 GEM_BUG_ON(last->ctx == cursor->ctx);
472
473 i915_gem_request_assign(&port->request, last);
474 port++;
475 }
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000476
Chris Wilson20311bd2016-11-14 20:41:03 +0000477 rb = rb_next(rb);
478 rb_erase(&cursor->priotree.node, &engine->execlist_queue);
479 RB_CLEAR_NODE(&cursor->priotree.node);
480 cursor->priotree.priority = INT_MAX;
481
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000482 __i915_gem_request_submit(cursor);
Tvrtko Ursulind7d96832017-02-21 11:03:00 +0000483 trace_i915_gem_request_in(cursor, port - engine->execlist_port);
Chris Wilson70c2a242016-09-09 14:11:46 +0100484 last = cursor;
485 submit = true;
Michel Thierry53292cd2015-04-15 18:11:33 +0100486 }
Chris Wilson70c2a242016-09-09 14:11:46 +0100487 if (submit) {
Chris Wilson70c2a242016-09-09 14:11:46 +0100488 i915_gem_request_assign(&port->request, last);
Chris Wilson20311bd2016-11-14 20:41:03 +0000489 engine->execlist_first = rb;
Chris Wilson70c2a242016-09-09 14:11:46 +0100490 }
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000491 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Chris Wilson70c2a242016-09-09 14:11:46 +0100492
493 if (submit)
494 execlists_submit_ports(engine);
Michel Thierryacdd8842014-07-24 17:04:38 +0100495}
496
Chris Wilson70c2a242016-09-09 14:11:46 +0100497static bool execlists_elsp_idle(struct intel_engine_cs *engine)
Thomas Daniele981e7b2014-07-24 17:04:39 +0100498{
Chris Wilson70c2a242016-09-09 14:11:46 +0100499 return !engine->execlist_port[0].request;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100500}
501
Chris Wilson816ee792017-01-24 11:00:03 +0000502static bool execlists_elsp_ready(const struct intel_engine_cs *engine)
Ben Widawsky91a41032016-01-05 10:30:07 -0800503{
Chris Wilson816ee792017-01-24 11:00:03 +0000504 const struct execlist_port *port = engine->execlist_port;
Ben Widawsky91a41032016-01-05 10:30:07 -0800505
Chris Wilson816ee792017-01-24 11:00:03 +0000506 return port[0].count + port[1].count < 2;
Ben Widawsky91a41032016-01-05 10:30:07 -0800507}
508
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200509/*
Oscar Mateo73e4d072014-07-24 17:04:48 +0100510 * Check the unread Context Status Buffers and manage the submission of new
511 * contexts to the ELSP accordingly.
512 */
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +0100513static void intel_lrc_irq_handler(unsigned long data)
Thomas Daniele981e7b2014-07-24 17:04:39 +0100514{
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +0100515 struct intel_engine_cs *engine = (struct intel_engine_cs *)data;
Chris Wilson70c2a242016-09-09 14:11:46 +0100516 struct execlist_port *port = engine->execlist_port;
Chris Wilsonc0336662016-05-06 15:40:21 +0100517 struct drm_i915_private *dev_priv = engine->i915;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100518
Tvrtko Ursulin37566852016-04-12 14:37:31 +0100519 intel_uncore_forcewake_get(dev_priv, engine->fw_domains);
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000520
Chris Wilsonf7470262017-01-24 15:20:21 +0000521 while (test_and_clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted)) {
Chris Wilson70c2a242016-09-09 14:11:46 +0100522 u32 __iomem *csb_mmio =
523 dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine));
524 u32 __iomem *buf =
525 dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_BUF_LO(engine, 0));
526 unsigned int csb, head, tail;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100527
Chris Wilson70c2a242016-09-09 14:11:46 +0100528 csb = readl(csb_mmio);
529 head = GEN8_CSB_READ_PTR(csb);
530 tail = GEN8_CSB_WRITE_PTR(csb);
Chris Wilsona37951a2017-01-24 11:00:06 +0000531 if (head == tail)
532 break;
533
Chris Wilson70c2a242016-09-09 14:11:46 +0100534 if (tail < head)
535 tail += GEN8_CSB_ENTRIES;
Chris Wilsona37951a2017-01-24 11:00:06 +0000536 do {
Chris Wilson70c2a242016-09-09 14:11:46 +0100537 unsigned int idx = ++head % GEN8_CSB_ENTRIES;
538 unsigned int status = readl(buf + 2 * idx);
Thomas Daniele981e7b2014-07-24 17:04:39 +0100539
Chris Wilson2ffe80a2017-02-06 17:05:02 +0000540 /* We are flying near dragons again.
541 *
542 * We hold a reference to the request in execlist_port[]
543 * but no more than that. We are operating in softirq
544 * context and so cannot hold any mutex or sleep. That
545 * prevents us stopping the requests we are processing
546 * in port[] from being retired simultaneously (the
547 * breadcrumb will be complete before we see the
548 * context-switch). As we only hold the reference to the
549 * request, any pointer chasing underneath the request
550 * is subject to a potential use-after-free. Thus we
551 * store all of the bookkeeping within port[] as
552 * required, and avoid using unguarded pointers beneath
553 * request itself. The same applies to the atomic
554 * status notifier.
555 */
556
Chris Wilson70c2a242016-09-09 14:11:46 +0100557 if (!(status & GEN8_CTX_STATUS_COMPLETED_MASK))
558 continue;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100559
Chris Wilson86aa7e72017-01-23 11:31:32 +0000560 /* Check the context/desc id for this event matches */
Chris Wilsonae9a0432017-02-07 10:23:19 +0000561 GEM_DEBUG_BUG_ON(readl(buf + 2 * idx + 1) !=
562 port[0].context_id);
Chris Wilson86aa7e72017-01-23 11:31:32 +0000563
Chris Wilson70c2a242016-09-09 14:11:46 +0100564 GEM_BUG_ON(port[0].count == 0);
565 if (--port[0].count == 0) {
566 GEM_BUG_ON(status & GEN8_CTX_STATUS_PREEMPTED);
Chris Wilsonfe9ae7a2017-02-23 14:50:31 +0000567 GEM_BUG_ON(!i915_gem_request_completed(port[0].request));
Chris Wilson70c2a242016-09-09 14:11:46 +0100568 execlists_context_status_change(port[0].request,
569 INTEL_CONTEXT_SCHEDULE_OUT);
Thomas Daniele981e7b2014-07-24 17:04:39 +0100570
Tvrtko Ursulind7d96832017-02-21 11:03:00 +0000571 trace_i915_gem_request_out(port[0].request);
Chris Wilson70c2a242016-09-09 14:11:46 +0100572 i915_gem_request_put(port[0].request);
573 port[0] = port[1];
574 memset(&port[1], 0, sizeof(port[1]));
Chris Wilson70c2a242016-09-09 14:11:46 +0100575 }
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000576
Chris Wilson70c2a242016-09-09 14:11:46 +0100577 GEM_BUG_ON(port[0].count == 0 &&
578 !(status & GEN8_CTX_STATUS_ACTIVE_IDLE));
Chris Wilsona37951a2017-01-24 11:00:06 +0000579 } while (head < tail);
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000580
Chris Wilson70c2a242016-09-09 14:11:46 +0100581 writel(_MASKED_FIELD(GEN8_CSB_READ_PTR_MASK,
582 GEN8_CSB_WRITE_PTR(csb) << 8),
583 csb_mmio);
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000584 }
585
Chris Wilson70c2a242016-09-09 14:11:46 +0100586 if (execlists_elsp_ready(engine))
587 execlists_dequeue(engine);
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000588
Chris Wilson70c2a242016-09-09 14:11:46 +0100589 intel_uncore_forcewake_put(dev_priv, engine->fw_domains);
Thomas Daniele981e7b2014-07-24 17:04:39 +0100590}
591
Chris Wilson20311bd2016-11-14 20:41:03 +0000592static bool insert_request(struct i915_priotree *pt, struct rb_root *root)
593{
594 struct rb_node **p, *rb;
595 bool first = true;
596
597 /* most positive priority is scheduled first, equal priorities fifo */
598 rb = NULL;
599 p = &root->rb_node;
600 while (*p) {
601 struct i915_priotree *pos;
602
603 rb = *p;
604 pos = rb_entry(rb, typeof(*pos), node);
605 if (pt->priority > pos->priority) {
606 p = &rb->rb_left;
607 } else {
608 p = &rb->rb_right;
609 first = false;
610 }
611 }
612 rb_link_node(&pt->node, rb, p);
613 rb_insert_color(&pt->node, root);
614
615 return first;
616}
617
Chris Wilsonf4ea6bd2016-08-02 22:50:32 +0100618static void execlists_submit_request(struct drm_i915_gem_request *request)
Michel Thierryacdd8842014-07-24 17:04:38 +0100619{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000620 struct intel_engine_cs *engine = request->engine;
Chris Wilson5590af32016-09-09 14:11:54 +0100621 unsigned long flags;
Michel Thierryacdd8842014-07-24 17:04:38 +0100622
Chris Wilson663f71e2016-11-14 20:41:00 +0000623 /* Will be called from irq-context when using foreign fences. */
624 spin_lock_irqsave(&engine->timeline->lock, flags);
Michel Thierryacdd8842014-07-24 17:04:38 +0100625
Chris Wilson38332812017-01-24 11:00:07 +0000626 if (insert_request(&request->priotree, &engine->execlist_queue)) {
Chris Wilson20311bd2016-11-14 20:41:03 +0000627 engine->execlist_first = &request->priotree.node;
Chris Wilson48ea2552017-01-24 11:00:08 +0000628 if (execlists_elsp_ready(engine))
Chris Wilson38332812017-01-24 11:00:07 +0000629 tasklet_hi_schedule(&engine->irq_tasklet);
630 }
Michel Thierryacdd8842014-07-24 17:04:38 +0100631
Chris Wilson663f71e2016-11-14 20:41:00 +0000632 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Michel Thierryacdd8842014-07-24 17:04:38 +0100633}
634
Chris Wilson20311bd2016-11-14 20:41:03 +0000635static struct intel_engine_cs *
636pt_lock_engine(struct i915_priotree *pt, struct intel_engine_cs *locked)
637{
638 struct intel_engine_cs *engine;
639
640 engine = container_of(pt,
641 struct drm_i915_gem_request,
642 priotree)->engine;
643 if (engine != locked) {
644 if (locked)
645 spin_unlock_irq(&locked->timeline->lock);
646 spin_lock_irq(&engine->timeline->lock);
647 }
648
649 return engine;
650}
651
652static void execlists_schedule(struct drm_i915_gem_request *request, int prio)
653{
654 struct intel_engine_cs *engine = NULL;
655 struct i915_dependency *dep, *p;
656 struct i915_dependency stack;
657 LIST_HEAD(dfs);
658
659 if (prio <= READ_ONCE(request->priotree.priority))
660 return;
661
Chris Wilson70cd1472016-11-28 14:36:49 +0000662 /* Need BKL in order to use the temporary link inside i915_dependency */
663 lockdep_assert_held(&request->i915->drm.struct_mutex);
Chris Wilson20311bd2016-11-14 20:41:03 +0000664
665 stack.signaler = &request->priotree;
666 list_add(&stack.dfs_link, &dfs);
667
668 /* Recursively bump all dependent priorities to match the new request.
669 *
670 * A naive approach would be to use recursion:
671 * static void update_priorities(struct i915_priotree *pt, prio) {
672 * list_for_each_entry(dep, &pt->signalers_list, signal_link)
673 * update_priorities(dep->signal, prio)
674 * insert_request(pt);
675 * }
676 * but that may have unlimited recursion depth and so runs a very
677 * real risk of overunning the kernel stack. Instead, we build
678 * a flat list of all dependencies starting with the current request.
679 * As we walk the list of dependencies, we add all of its dependencies
680 * to the end of the list (this may include an already visited
681 * request) and continue to walk onwards onto the new dependencies. The
682 * end result is a topological list of requests in reverse order, the
683 * last element in the list is the request we must execute first.
684 */
685 list_for_each_entry_safe(dep, p, &dfs, dfs_link) {
686 struct i915_priotree *pt = dep->signaler;
687
688 list_for_each_entry(p, &pt->signalers_list, signal_link)
689 if (prio > READ_ONCE(p->signaler->priority))
690 list_move_tail(&p->dfs_link, &dfs);
691
Chris Wilson0798cff2016-12-05 14:29:41 +0000692 list_safe_reset_next(dep, p, dfs_link);
Chris Wilson20311bd2016-11-14 20:41:03 +0000693 if (!RB_EMPTY_NODE(&pt->node))
694 continue;
695
696 engine = pt_lock_engine(pt, engine);
697
698 /* If it is not already in the rbtree, we can update the
699 * priority inplace and skip over it (and its dependencies)
700 * if it is referenced *again* as we descend the dfs.
701 */
702 if (prio > pt->priority && RB_EMPTY_NODE(&pt->node)) {
703 pt->priority = prio;
704 list_del_init(&dep->dfs_link);
705 }
706 }
707
708 /* Fifo and depth-first replacement ensure our deps execute before us */
709 list_for_each_entry_safe_reverse(dep, p, &dfs, dfs_link) {
710 struct i915_priotree *pt = dep->signaler;
711
712 INIT_LIST_HEAD(&dep->dfs_link);
713
714 engine = pt_lock_engine(pt, engine);
715
716 if (prio <= pt->priority)
717 continue;
718
719 GEM_BUG_ON(RB_EMPTY_NODE(&pt->node));
720
721 pt->priority = prio;
722 rb_erase(&pt->node, &engine->execlist_queue);
723 if (insert_request(pt, &engine->execlist_queue))
724 engine->execlist_first = &pt->node;
725 }
726
727 if (engine)
728 spin_unlock_irq(&engine->timeline->lock);
729
730 /* XXX Do we need to preempt to make room for us and our deps? */
731}
732
Chris Wilsone8a9c582016-12-18 15:37:20 +0000733static int execlists_context_pin(struct intel_engine_cs *engine,
734 struct i915_gem_context *ctx)
Oscar Mateodcb4c122014-11-13 10:28:10 +0000735{
Chris Wilson9021ad02016-05-24 14:53:37 +0100736 struct intel_context *ce = &ctx->engine[engine->id];
Chris Wilson2947e402016-12-18 15:37:23 +0000737 unsigned int flags;
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +0100738 void *vaddr;
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000739 int ret;
Oscar Mateodcb4c122014-11-13 10:28:10 +0000740
Chris Wilson91c8a322016-07-05 10:40:23 +0100741 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000742
Chris Wilson9021ad02016-05-24 14:53:37 +0100743 if (ce->pin_count++)
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100744 return 0;
745
Chris Wilsone8a9c582016-12-18 15:37:20 +0000746 if (!ce->state) {
747 ret = execlists_context_deferred_alloc(ctx, engine);
748 if (ret)
749 goto err;
750 }
Chris Wilson56f6e0a2017-01-05 15:30:20 +0000751 GEM_BUG_ON(!ce->state);
Chris Wilsone8a9c582016-12-18 15:37:20 +0000752
Chris Wilson72b72ae2017-02-10 10:14:22 +0000753 flags = PIN_GLOBAL | PIN_HIGH;
Daniele Ceraolo Spuriofeef2a72016-12-23 15:56:22 -0800754 if (ctx->ggtt_offset_bias)
755 flags |= PIN_OFFSET_BIAS | ctx->ggtt_offset_bias;
Chris Wilson2947e402016-12-18 15:37:23 +0000756
757 ret = i915_vma_pin(ce->state, 0, GEN8_LR_CONTEXT_ALIGN, flags);
Nick Hoathe84fe802015-09-11 12:53:46 +0100758 if (ret)
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100759 goto err;
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000760
Chris Wilsonbf3783e2016-08-15 10:48:54 +0100761 vaddr = i915_gem_object_pin_map(ce->state->obj, I915_MAP_WB);
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +0100762 if (IS_ERR(vaddr)) {
763 ret = PTR_ERR(vaddr);
Chris Wilsonbf3783e2016-08-15 10:48:54 +0100764 goto unpin_vma;
Tvrtko Ursulin82352e92016-01-15 17:12:45 +0000765 }
766
Daniele Ceraolo Spuriod3ef1af2016-12-23 15:56:21 -0800767 ret = intel_ring_pin(ce->ring, ctx->ggtt_offset_bias);
Nick Hoathe84fe802015-09-11 12:53:46 +0100768 if (ret)
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +0100769 goto unpin_map;
Alex Daid1675192015-08-12 15:43:43 +0100770
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000771 intel_lr_context_descriptor_update(ctx, engine);
Chris Wilson9021ad02016-05-24 14:53:37 +0100772
Chris Wilsona3aabe82016-10-04 21:11:26 +0100773 ce->lrc_reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
774 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100775 i915_ggtt_offset(ce->ring->vma);
Chris Wilsona3aabe82016-10-04 21:11:26 +0100776
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100777 ce->state->obj->mm.dirty = true;
Daniel Vettere93c28f2015-09-02 14:33:42 +0200778
Chris Wilson9a6feaf2016-07-20 13:31:50 +0100779 i915_gem_context_get(ctx);
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100780 return 0;
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000781
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +0100782unpin_map:
Chris Wilsonbf3783e2016-08-15 10:48:54 +0100783 i915_gem_object_unpin_map(ce->state->obj);
784unpin_vma:
785 __i915_vma_unpin(ce->state);
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100786err:
Chris Wilson9021ad02016-05-24 14:53:37 +0100787 ce->pin_count = 0;
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000788 return ret;
Oscar Mateodcb4c122014-11-13 10:28:10 +0000789}
790
Chris Wilsone8a9c582016-12-18 15:37:20 +0000791static void execlists_context_unpin(struct intel_engine_cs *engine,
792 struct i915_gem_context *ctx)
Oscar Mateodcb4c122014-11-13 10:28:10 +0000793{
Chris Wilson9021ad02016-05-24 14:53:37 +0100794 struct intel_context *ce = &ctx->engine[engine->id];
Daniel Vetteraf3302b2015-12-04 17:27:15 +0100795
Chris Wilson91c8a322016-07-05 10:40:23 +0100796 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilson9021ad02016-05-24 14:53:37 +0100797 GEM_BUG_ON(ce->pin_count == 0);
Tvrtko Ursulin321fe302016-01-28 10:29:55 +0000798
Chris Wilson9021ad02016-05-24 14:53:37 +0100799 if (--ce->pin_count)
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100800 return;
801
Chris Wilsonaad29fb2016-08-02 22:50:23 +0100802 intel_ring_unpin(ce->ring);
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100803
Chris Wilsonbf3783e2016-08-15 10:48:54 +0100804 i915_gem_object_unpin_map(ce->state->obj);
805 i915_vma_unpin(ce->state);
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100806
Chris Wilson9a6feaf2016-07-20 13:31:50 +0100807 i915_gem_context_put(ctx);
Oscar Mateodcb4c122014-11-13 10:28:10 +0000808}
809
Chris Wilsonf73e7392016-12-18 15:37:24 +0000810static int execlists_request_alloc(struct drm_i915_gem_request *request)
Chris Wilsonef11c012016-12-18 15:37:19 +0000811{
812 struct intel_engine_cs *engine = request->engine;
813 struct intel_context *ce = &request->ctx->engine[engine->id];
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000814 u32 *cs;
Chris Wilsonef11c012016-12-18 15:37:19 +0000815 int ret;
816
Chris Wilsone8a9c582016-12-18 15:37:20 +0000817 GEM_BUG_ON(!ce->pin_count);
818
Chris Wilsonef11c012016-12-18 15:37:19 +0000819 /* Flush enough space to reduce the likelihood of waiting after
820 * we start building the request - in which case we will just
821 * have to repeat work.
822 */
823 request->reserved_space += EXECLISTS_REQUEST_SIZE;
824
Chris Wilsone8a9c582016-12-18 15:37:20 +0000825 GEM_BUG_ON(!ce->ring);
Chris Wilsonef11c012016-12-18 15:37:19 +0000826 request->ring = ce->ring;
827
Chris Wilsonef11c012016-12-18 15:37:19 +0000828 if (i915.enable_guc_submission) {
829 /*
830 * Check that the GuC has space for the request before
831 * going any further, as the i915_add_request() call
832 * later on mustn't fail ...
833 */
834 ret = i915_guc_wq_reserve(request);
835 if (ret)
Chris Wilsone8a9c582016-12-18 15:37:20 +0000836 goto err;
Chris Wilsonef11c012016-12-18 15:37:19 +0000837 }
838
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000839 cs = intel_ring_begin(request, 0);
840 if (IS_ERR(cs)) {
841 ret = PTR_ERR(cs);
Chris Wilsonef11c012016-12-18 15:37:19 +0000842 goto err_unreserve;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000843 }
Chris Wilsonef11c012016-12-18 15:37:19 +0000844
845 if (!ce->initialised) {
846 ret = engine->init_context(request);
847 if (ret)
848 goto err_unreserve;
849
850 ce->initialised = true;
851 }
852
853 /* Note that after this point, we have committed to using
854 * this request as it is being used to both track the
855 * state of engine initialisation and liveness of the
856 * golden renderstate above. Think twice before you try
857 * to cancel/unwind this request now.
858 */
859
860 request->reserved_space -= EXECLISTS_REQUEST_SIZE;
861 return 0;
862
863err_unreserve:
864 if (i915.enable_guc_submission)
865 i915_guc_wq_unreserve(request);
Chris Wilsone8a9c582016-12-18 15:37:20 +0000866err:
Chris Wilsonef11c012016-12-18 15:37:19 +0000867 return ret;
868}
869
Arun Siluvery9e000842015-07-03 14:27:31 +0100870/*
871 * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
872 * PIPE_CONTROL instruction. This is required for the flush to happen correctly
873 * but there is a slight complication as this is applied in WA batch where the
874 * values are only initialized once so we cannot take register value at the
875 * beginning and reuse it further; hence we save its value to memory, upload a
876 * constant value with bit21 set and then we restore it back with the saved value.
877 * To simplify the WA, a constant value is formed by using the default value
878 * of this register. This shouldn't be a problem because we are only modifying
879 * it for a short period and this batch in non-premptible. We can ofcourse
880 * use additional instructions that read the actual value of the register
881 * at that time and set our bit of interest but it makes the WA complicated.
882 *
883 * This WA is also required for Gen9 so extracting as a function avoids
884 * code duplication.
885 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000886static u32 *
887gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery9e000842015-07-03 14:27:31 +0100888{
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000889 *batch++ = MI_STORE_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
890 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
891 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
892 *batch++ = 0;
Arun Siluvery9e000842015-07-03 14:27:31 +0100893
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000894 *batch++ = MI_LOAD_REGISTER_IMM(1);
895 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
896 *batch++ = 0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES;
Arun Siluvery9e000842015-07-03 14:27:31 +0100897
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000898 batch = gen8_emit_pipe_control(batch,
899 PIPE_CONTROL_CS_STALL |
900 PIPE_CONTROL_DC_FLUSH_ENABLE,
901 0);
Arun Siluvery9e000842015-07-03 14:27:31 +0100902
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000903 *batch++ = MI_LOAD_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
904 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
905 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
906 *batch++ = 0;
Arun Siluvery9e000842015-07-03 14:27:31 +0100907
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000908 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +0100909}
910
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200911/*
912 * Typically we only have one indirect_ctx and per_ctx batch buffer which are
913 * initialized at the beginning and shared across all contexts but this field
914 * helps us to have multiple batches at different offsets and select them based
915 * on a criteria. At the moment this batch always start at the beginning of the page
916 * and at this point we don't have multiple wa_ctx batch buffers.
Arun Siluvery17ee9502015-06-19 19:07:01 +0100917 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200918 * The number of WA applied are not known at the beginning; we use this field
919 * to return the no of DWORDS written.
Arun Siluvery17ee9502015-06-19 19:07:01 +0100920 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200921 * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
922 * so it adds NOOPs as padding to make it cacheline aligned.
923 * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
924 * makes a complete batch buffer.
Arun Siluvery17ee9502015-06-19 19:07:01 +0100925 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000926static u32 *gen8_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery17ee9502015-06-19 19:07:01 +0100927{
Arun Siluvery7ad00d12015-06-19 18:37:12 +0100928 /* WaDisableCtxRestoreArbitration:bdw,chv */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000929 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
Arun Siluvery17ee9502015-06-19 19:07:01 +0100930
Arun Siluveryc82435b2015-06-19 18:37:13 +0100931 /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000932 if (IS_BROADWELL(engine->i915))
933 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
Arun Siluveryc82435b2015-06-19 18:37:13 +0100934
Arun Siluvery0160f052015-06-23 15:46:57 +0100935 /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
936 /* Actual scratch location is at 128 bytes offset */
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000937 batch = gen8_emit_pipe_control(batch,
938 PIPE_CONTROL_FLUSH_L3 |
939 PIPE_CONTROL_GLOBAL_GTT_IVB |
940 PIPE_CONTROL_CS_STALL |
941 PIPE_CONTROL_QW_WRITE,
942 i915_ggtt_offset(engine->scratch) +
943 2 * CACHELINE_BYTES);
Arun Siluvery0160f052015-06-23 15:46:57 +0100944
Arun Siluvery17ee9502015-06-19 19:07:01 +0100945 /* Pad to end of cacheline */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000946 while ((unsigned long)batch % CACHELINE_BYTES)
947 *batch++ = MI_NOOP;
Arun Siluvery17ee9502015-06-19 19:07:01 +0100948
949 /*
950 * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
951 * execution depends on the length specified in terms of cache lines
952 * in the register CTX_RCS_INDIRECT_CTX
953 */
954
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000955 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +0100956}
957
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200958/*
959 * This batch is started immediately after indirect_ctx batch. Since we ensure
960 * that indirect_ctx ends on a cacheline this batch is aligned automatically.
Arun Siluvery17ee9502015-06-19 19:07:01 +0100961 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200962 * The number of DWORDS written are returned using this field.
Arun Siluvery17ee9502015-06-19 19:07:01 +0100963 *
964 * This batch is terminated with MI_BATCH_BUFFER_END and so we need not add padding
965 * to align it with cacheline as padding after MI_BATCH_BUFFER_END is redundant.
966 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000967static u32 *gen8_init_perctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery17ee9502015-06-19 19:07:01 +0100968{
Arun Siluvery7ad00d12015-06-19 18:37:12 +0100969 /* WaDisableCtxRestoreArbitration:bdw,chv */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000970 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
971 *batch++ = MI_BATCH_BUFFER_END;
Arun Siluvery7ad00d12015-06-19 18:37:12 +0100972
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000973 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +0100974}
975
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000976static u32 *gen9_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery0504cff2015-07-14 15:01:27 +0100977{
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200978 /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt,glk */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000979 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
Arun Siluverya4106a72015-07-14 15:01:29 +0100980
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200981 /* WaDisableGatherAtSetShaderCommonSlice:skl,bxt,kbl,glk */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000982 *batch++ = MI_LOAD_REGISTER_IMM(1);
983 *batch++ = i915_mmio_reg_offset(COMMON_SLICE_CHICKEN2);
984 *batch++ = _MASKED_BIT_DISABLE(
985 GEN9_DISABLE_GATHER_AT_SET_SHADER_COMMON_SLICE);
986 *batch++ = MI_NOOP;
Mika Kuoppala873e8172016-07-20 14:26:13 +0300987
Mika Kuoppala066d4622016-06-07 17:19:15 +0300988 /* WaClearSlmSpaceAtContextSwitch:kbl */
989 /* Actual scratch location is at 128 bytes offset */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000990 if (IS_KBL_REVID(engine->i915, 0, KBL_REVID_A0)) {
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000991 batch = gen8_emit_pipe_control(batch,
992 PIPE_CONTROL_FLUSH_L3 |
993 PIPE_CONTROL_GLOBAL_GTT_IVB |
994 PIPE_CONTROL_CS_STALL |
995 PIPE_CONTROL_QW_WRITE,
996 i915_ggtt_offset(engine->scratch)
997 + 2 * CACHELINE_BYTES);
Mika Kuoppala066d4622016-06-07 17:19:15 +0300998 }
Tim Gore3485d992016-07-05 10:01:30 +0100999
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001000 /* WaMediaPoolStateCmdInWABB:bxt,glk */
Tim Gore3485d992016-07-05 10:01:30 +01001001 if (HAS_POOLED_EU(engine->i915)) {
1002 /*
1003 * EU pool configuration is setup along with golden context
1004 * during context initialization. This value depends on
1005 * device type (2x6 or 3x6) and needs to be updated based
1006 * on which subslice is disabled especially for 2x6
1007 * devices, however it is safe to load default
1008 * configuration of 3x6 device instead of masking off
1009 * corresponding bits because HW ignores bits of a disabled
1010 * subslice and drops down to appropriate config. Please
1011 * see render_state_setup() in i915_gem_render_state.c for
1012 * possible configurations, to avoid duplication they are
1013 * not shown here again.
1014 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001015 *batch++ = GEN9_MEDIA_POOL_STATE;
1016 *batch++ = GEN9_MEDIA_POOL_ENABLE;
1017 *batch++ = 0x00777000;
1018 *batch++ = 0;
1019 *batch++ = 0;
1020 *batch++ = 0;
Tim Gore3485d992016-07-05 10:01:30 +01001021 }
1022
Arun Siluvery0504cff2015-07-14 15:01:27 +01001023 /* Pad to end of cacheline */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001024 while ((unsigned long)batch % CACHELINE_BYTES)
1025 *batch++ = MI_NOOP;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001026
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001027 return batch;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001028}
1029
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001030static u32 *gen9_init_perctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery0504cff2015-07-14 15:01:27 +01001031{
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001032 *batch++ = MI_BATCH_BUFFER_END;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001033
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001034 return batch;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001035}
1036
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001037#define CTX_WA_BB_OBJ_SIZE (PAGE_SIZE)
1038
1039static int lrc_setup_wa_ctx(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001040{
Chris Wilson48bb74e2016-08-15 10:49:04 +01001041 struct drm_i915_gem_object *obj;
1042 struct i915_vma *vma;
1043 int err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001044
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001045 obj = i915_gem_object_create(engine->i915, CTX_WA_BB_OBJ_SIZE);
Chris Wilson48bb74e2016-08-15 10:49:04 +01001046 if (IS_ERR(obj))
1047 return PTR_ERR(obj);
1048
Chris Wilsona01cb372017-01-16 15:21:30 +00001049 vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
Chris Wilson48bb74e2016-08-15 10:49:04 +01001050 if (IS_ERR(vma)) {
1051 err = PTR_ERR(vma);
1052 goto err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001053 }
1054
Chris Wilson48bb74e2016-08-15 10:49:04 +01001055 err = i915_vma_pin(vma, 0, PAGE_SIZE, PIN_GLOBAL | PIN_HIGH);
1056 if (err)
1057 goto err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001058
Chris Wilson48bb74e2016-08-15 10:49:04 +01001059 engine->wa_ctx.vma = vma;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001060 return 0;
Chris Wilson48bb74e2016-08-15 10:49:04 +01001061
1062err:
1063 i915_gem_object_put(obj);
1064 return err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001065}
1066
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001067static void lrc_destroy_wa_ctx(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001068{
Chris Wilson19880c42016-08-15 10:49:05 +01001069 i915_vma_unpin_and_release(&engine->wa_ctx.vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001070}
1071
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001072typedef u32 *(*wa_bb_func_t)(struct intel_engine_cs *engine, u32 *batch);
1073
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001074static int intel_init_workaround_bb(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001075{
Chris Wilson48bb74e2016-08-15 10:49:04 +01001076 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001077 struct i915_wa_ctx_bb *wa_bb[2] = { &wa_ctx->indirect_ctx,
1078 &wa_ctx->per_ctx };
1079 wa_bb_func_t wa_bb_fn[2];
Arun Siluvery17ee9502015-06-19 19:07:01 +01001080 struct page *page;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001081 void *batch, *batch_ptr;
1082 unsigned int i;
Chris Wilson48bb74e2016-08-15 10:49:04 +01001083 int ret;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001084
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001085 if (WARN_ON(engine->id != RCS || !engine->scratch))
1086 return -EINVAL;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001087
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001088 switch (INTEL_GEN(engine->i915)) {
1089 case 9:
1090 wa_bb_fn[0] = gen9_init_indirectctx_bb;
1091 wa_bb_fn[1] = gen9_init_perctx_bb;
1092 break;
1093 case 8:
1094 wa_bb_fn[0] = gen8_init_indirectctx_bb;
1095 wa_bb_fn[1] = gen8_init_perctx_bb;
1096 break;
1097 default:
1098 MISSING_CASE(INTEL_GEN(engine->i915));
Arun Siluvery5e60d792015-06-23 15:50:44 +01001099 return 0;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001100 }
Arun Siluvery5e60d792015-06-23 15:50:44 +01001101
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001102 ret = lrc_setup_wa_ctx(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001103 if (ret) {
1104 DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
1105 return ret;
1106 }
1107
Chris Wilson48bb74e2016-08-15 10:49:04 +01001108 page = i915_gem_object_get_dirty_page(wa_ctx->vma->obj, 0);
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001109 batch = batch_ptr = kmap_atomic(page);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001110
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001111 /*
1112 * Emit the two workaround batch buffers, recording the offset from the
1113 * start of the workaround batch buffer object for each and their
1114 * respective sizes.
1115 */
1116 for (i = 0; i < ARRAY_SIZE(wa_bb_fn); i++) {
1117 wa_bb[i]->offset = batch_ptr - batch;
1118 if (WARN_ON(!IS_ALIGNED(wa_bb[i]->offset, CACHELINE_BYTES))) {
1119 ret = -EINVAL;
1120 break;
1121 }
1122 batch_ptr = wa_bb_fn[i](engine, batch_ptr);
1123 wa_bb[i]->size = batch_ptr - (batch + wa_bb[i]->offset);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001124 }
1125
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001126 BUG_ON(batch_ptr - batch > CTX_WA_BB_OBJ_SIZE);
1127
Arun Siluvery17ee9502015-06-19 19:07:01 +01001128 kunmap_atomic(batch);
1129 if (ret)
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001130 lrc_destroy_wa_ctx(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001131
1132 return ret;
1133}
1134
Chris Wilson22cc4402017-02-04 11:05:19 +00001135static u32 port_seqno(struct execlist_port *port)
1136{
1137 return port->request ? port->request->global_seqno : 0;
1138}
1139
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001140static int gen8_init_common_ring(struct intel_engine_cs *engine)
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001141{
Chris Wilsonc0336662016-05-06 15:40:21 +01001142 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001143 int ret;
1144
1145 ret = intel_mocs_init_engine(engine);
1146 if (ret)
1147 return ret;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001148
Chris Wilsonad07dfc2016-10-07 07:53:26 +01001149 intel_engine_reset_breadcrumbs(engine);
Chris Wilsonf3b8f912017-01-05 15:30:21 +00001150 intel_engine_init_hangcheck(engine);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001151
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001152 I915_WRITE(RING_HWSTAM(engine->mmio_base), 0xffffffff);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001153 I915_WRITE(RING_MODE_GEN7(engine),
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001154 _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
Chris Wilsonf3b8f912017-01-05 15:30:21 +00001155 I915_WRITE(RING_HWS_PGA(engine->mmio_base),
1156 engine->status_page.ggtt_offset);
1157 POSTING_READ(RING_HWS_PGA(engine->mmio_base));
Michel Thierrydfc53c52015-09-28 13:25:12 +01001158
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001159 DRM_DEBUG_DRIVER("Execlists enabled for %s\n", engine->name);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001160
Chris Wilsonc87d50c2016-10-04 21:11:27 +01001161 /* After a GPU reset, we may have requests to replay */
Chris Wilsonf7470262017-01-24 15:20:21 +00001162 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
Chris Wilson31de7352017-03-16 12:56:18 +00001163 if (!i915.enable_guc_submission && !execlists_elsp_idle(engine)) {
Chris Wilson22cc4402017-02-04 11:05:19 +00001164 DRM_DEBUG_DRIVER("Restarting %s from requests [0x%x, 0x%x]\n",
1165 engine->name,
1166 port_seqno(&engine->execlist_port[0]),
1167 port_seqno(&engine->execlist_port[1]));
Chris Wilsonc87d50c2016-10-04 21:11:27 +01001168 engine->execlist_port[0].count = 0;
1169 engine->execlist_port[1].count = 0;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001170 execlists_submit_ports(engine);
Chris Wilsonc87d50c2016-10-04 21:11:27 +01001171 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01001172
1173 return 0;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001174}
1175
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001176static int gen8_init_render_ring(struct intel_engine_cs *engine)
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001177{
Chris Wilsonc0336662016-05-06 15:40:21 +01001178 struct drm_i915_private *dev_priv = engine->i915;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001179 int ret;
1180
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001181 ret = gen8_init_common_ring(engine);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001182 if (ret)
1183 return ret;
1184
1185 /* We need to disable the AsyncFlip performance optimisations in order
1186 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1187 * programmed to '1' on all products.
1188 *
1189 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
1190 */
1191 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1192
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001193 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1194
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001195 return init_workarounds_ring(engine);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001196}
1197
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001198static int gen9_init_render_ring(struct intel_engine_cs *engine)
Damien Lespiau82ef8222015-02-09 19:33:08 +00001199{
1200 int ret;
1201
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001202 ret = gen8_init_common_ring(engine);
Damien Lespiau82ef8222015-02-09 19:33:08 +00001203 if (ret)
1204 return ret;
1205
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001206 return init_workarounds_ring(engine);
Damien Lespiau82ef8222015-02-09 19:33:08 +00001207}
1208
Chris Wilson821ed7d2016-09-09 14:11:53 +01001209static void reset_common_ring(struct intel_engine_cs *engine,
1210 struct drm_i915_gem_request *request)
1211{
Chris Wilson821ed7d2016-09-09 14:11:53 +01001212 struct execlist_port *port = engine->execlist_port;
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001213 struct intel_context *ce;
1214
1215 /* If the request was innocent, we leave the request in the ELSP
1216 * and will try to replay it on restarting. The context image may
1217 * have been corrupted by the reset, in which case we may have
1218 * to service a new GPU hang, but more likely we can continue on
1219 * without impact.
1220 *
1221 * If the request was guilty, we presume the context is corrupt
1222 * and have to at least restore the RING register in the context
1223 * image back to the expected values to skip over the guilty request.
1224 */
1225 if (!request || request->fence.error != -EIO)
1226 return;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001227
Chris Wilsona3aabe82016-10-04 21:11:26 +01001228 /* We want a simple context + ring to execute the breadcrumb update.
1229 * We cannot rely on the context being intact across the GPU hang,
1230 * so clear it and rebuild just what we need for the breadcrumb.
1231 * All pending requests for this context will be zapped, and any
1232 * future request will be after userspace has had the opportunity
1233 * to recreate its own state.
1234 */
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001235 ce = &request->ctx->engine[engine->id];
Chris Wilsona3aabe82016-10-04 21:11:26 +01001236 execlists_init_reg_state(ce->lrc_reg_state,
1237 request->ctx, engine, ce->ring);
1238
Chris Wilson821ed7d2016-09-09 14:11:53 +01001239 /* Move the RING_HEAD onto the breadcrumb, past the hanging batch */
Chris Wilsona3aabe82016-10-04 21:11:26 +01001240 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
1241 i915_ggtt_offset(ce->ring->vma);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001242 ce->lrc_reg_state[CTX_RING_HEAD+1] = request->postfix;
Chris Wilsona3aabe82016-10-04 21:11:26 +01001243
Chris Wilson821ed7d2016-09-09 14:11:53 +01001244 request->ring->head = request->postfix;
1245 request->ring->last_retired_head = -1;
1246 intel_ring_update_space(request->ring);
1247
Chris Wilson821ed7d2016-09-09 14:11:53 +01001248 /* Catch up with any missed context-switch interrupts */
Chris Wilson821ed7d2016-09-09 14:11:53 +01001249 if (request->ctx != port[0].request->ctx) {
1250 i915_gem_request_put(port[0].request);
1251 port[0] = port[1];
1252 memset(&port[1], 0, sizeof(port[1]));
1253 }
1254
Chris Wilson821ed7d2016-09-09 14:11:53 +01001255 GEM_BUG_ON(request->ctx != port[0].request->ctx);
Chris Wilsona3aabe82016-10-04 21:11:26 +01001256
1257 /* Reset WaIdleLiteRestore:bdw,skl as well */
1258 request->tail = request->wa_tail - WA_TAIL_DWORDS * sizeof(u32);
Chris Wilson944a36d2017-02-17 16:38:33 +00001259 GEM_BUG_ON(!IS_ALIGNED(request->tail, 8));
Chris Wilson821ed7d2016-09-09 14:11:53 +01001260}
1261
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001262static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request *req)
1263{
1264 struct i915_hw_ppgtt *ppgtt = req->ctx->ppgtt;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001265 struct intel_engine_cs *engine = req->engine;
Mika Kuoppalae7167762017-02-28 17:28:10 +02001266 const int num_lri_cmds = GEN8_3LVL_PDPES * 2;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001267 u32 *cs;
1268 int i;
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001269
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001270 cs = intel_ring_begin(req, num_lri_cmds * 2 + 2);
1271 if (IS_ERR(cs))
1272 return PTR_ERR(cs);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001273
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001274 *cs++ = MI_LOAD_REGISTER_IMM(num_lri_cmds);
Mika Kuoppalae7167762017-02-28 17:28:10 +02001275 for (i = GEN8_3LVL_PDPES - 1; i >= 0; i--) {
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001276 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
1277
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001278 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, i));
1279 *cs++ = upper_32_bits(pd_daddr);
1280 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, i));
1281 *cs++ = lower_32_bits(pd_daddr);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001282 }
1283
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001284 *cs++ = MI_NOOP;
1285 intel_ring_advance(req, cs);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001286
1287 return 0;
1288}
1289
John Harrisonbe795fc2015-05-29 17:44:03 +01001290static int gen8_emit_bb_start(struct drm_i915_gem_request *req,
Chris Wilson803688b2016-08-02 22:50:27 +01001291 u64 offset, u32 len,
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001292 const unsigned int flags)
Oscar Mateo15648582014-07-24 17:04:32 +01001293{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001294 u32 *cs;
Oscar Mateo15648582014-07-24 17:04:32 +01001295 int ret;
1296
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001297 /* Don't rely in hw updating PDPs, specially in lite-restore.
1298 * Ideally, we should set Force PD Restore in ctx descriptor,
1299 * but we can't. Force Restore would be a second option, but
1300 * it is unsafe in case of lite-restore (because the ctx is
Michel Thierry2dba3232015-07-30 11:06:23 +01001301 * not idle). PML4 is allocated during ppgtt init so this is
1302 * not needed in 48-bit.*/
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001303 if (req->ctx->ppgtt &&
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001304 (intel_engine_flag(req->engine) & req->ctx->ppgtt->pd_dirty_rings) &&
1305 !i915_vm_is_48bit(&req->ctx->ppgtt->base) &&
1306 !intel_vgpu_active(req->i915)) {
1307 ret = intel_logical_ring_emit_pdps(req);
1308 if (ret)
1309 return ret;
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001310
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00001311 req->ctx->ppgtt->pd_dirty_rings &= ~intel_engine_flag(req->engine);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001312 }
1313
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001314 cs = intel_ring_begin(req, 4);
1315 if (IS_ERR(cs))
1316 return PTR_ERR(cs);
Oscar Mateo15648582014-07-24 17:04:32 +01001317
1318 /* FIXME(BDW): Address space and security selectors. */
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001319 *cs++ = MI_BATCH_BUFFER_START_GEN8 |
1320 (flags & I915_DISPATCH_SECURE ? 0 : BIT(8)) |
1321 (flags & I915_DISPATCH_RS ? MI_BATCH_RESOURCE_STREAMER : 0);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001322 *cs++ = lower_32_bits(offset);
1323 *cs++ = upper_32_bits(offset);
1324 *cs++ = MI_NOOP;
1325 intel_ring_advance(req, cs);
Oscar Mateo15648582014-07-24 17:04:32 +01001326
1327 return 0;
1328}
1329
Chris Wilson31bb59c2016-07-01 17:23:27 +01001330static void gen8_logical_ring_enable_irq(struct intel_engine_cs *engine)
Oscar Mateo73d477f2014-07-24 17:04:31 +01001331{
Chris Wilsonc0336662016-05-06 15:40:21 +01001332 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson31bb59c2016-07-01 17:23:27 +01001333 I915_WRITE_IMR(engine,
1334 ~(engine->irq_enable_mask | engine->irq_keep_mask));
1335 POSTING_READ_FW(RING_IMR(engine->mmio_base));
Oscar Mateo73d477f2014-07-24 17:04:31 +01001336}
1337
Chris Wilson31bb59c2016-07-01 17:23:27 +01001338static void gen8_logical_ring_disable_irq(struct intel_engine_cs *engine)
Oscar Mateo73d477f2014-07-24 17:04:31 +01001339{
Chris Wilsonc0336662016-05-06 15:40:21 +01001340 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson31bb59c2016-07-01 17:23:27 +01001341 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Oscar Mateo73d477f2014-07-24 17:04:31 +01001342}
1343
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001344static int gen8_emit_flush(struct drm_i915_gem_request *request, u32 mode)
Oscar Mateo47122742014-07-24 17:04:28 +01001345{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001346 u32 cmd, *cs;
Oscar Mateo47122742014-07-24 17:04:28 +01001347
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001348 cs = intel_ring_begin(request, 4);
1349 if (IS_ERR(cs))
1350 return PTR_ERR(cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001351
1352 cmd = MI_FLUSH_DW + 1;
1353
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001354 /* We always require a command barrier so that subsequent
1355 * commands, such as breadcrumb interrupts, are strictly ordered
1356 * wrt the contents of the write cache being flushed to memory
1357 * (and thus being coherent from the CPU).
1358 */
1359 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1360
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001361 if (mode & EMIT_INVALIDATE) {
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001362 cmd |= MI_INVALIDATE_TLB;
Chris Wilson1dae2df2016-08-02 22:50:19 +01001363 if (request->engine->id == VCS)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001364 cmd |= MI_INVALIDATE_BSD;
Oscar Mateo47122742014-07-24 17:04:28 +01001365 }
1366
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001367 *cs++ = cmd;
1368 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
1369 *cs++ = 0; /* upper addr */
1370 *cs++ = 0; /* value */
1371 intel_ring_advance(request, cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001372
1373 return 0;
1374}
1375
John Harrison7deb4d32015-05-29 17:43:59 +01001376static int gen8_emit_flush_render(struct drm_i915_gem_request *request,
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001377 u32 mode)
Oscar Mateo47122742014-07-24 17:04:28 +01001378{
Chris Wilsonb5321f32016-08-02 22:50:18 +01001379 struct intel_engine_cs *engine = request->engine;
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001380 u32 scratch_addr =
1381 i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001382 bool vf_flush_wa = false, dc_flush_wa = false;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001383 u32 *cs, flags = 0;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001384 int len;
Oscar Mateo47122742014-07-24 17:04:28 +01001385
1386 flags |= PIPE_CONTROL_CS_STALL;
1387
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001388 if (mode & EMIT_FLUSH) {
Oscar Mateo47122742014-07-24 17:04:28 +01001389 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
1390 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -08001391 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +01001392 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Oscar Mateo47122742014-07-24 17:04:28 +01001393 }
1394
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001395 if (mode & EMIT_INVALIDATE) {
Oscar Mateo47122742014-07-24 17:04:28 +01001396 flags |= PIPE_CONTROL_TLB_INVALIDATE;
1397 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
1398 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
1399 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
1400 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
1401 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
1402 flags |= PIPE_CONTROL_QW_WRITE;
1403 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Oscar Mateo47122742014-07-24 17:04:28 +01001404
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001405 /*
1406 * On GEN9: before VF_CACHE_INVALIDATE we need to emit a NULL
1407 * pipe control.
1408 */
Chris Wilsonc0336662016-05-06 15:40:21 +01001409 if (IS_GEN9(request->i915))
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001410 vf_flush_wa = true;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001411
1412 /* WaForGAMHang:kbl */
1413 if (IS_KBL_REVID(request->i915, 0, KBL_REVID_B0))
1414 dc_flush_wa = true;
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001415 }
Imre Deak9647ff32015-01-25 13:27:11 -08001416
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001417 len = 6;
1418
1419 if (vf_flush_wa)
1420 len += 6;
1421
1422 if (dc_flush_wa)
1423 len += 12;
1424
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001425 cs = intel_ring_begin(request, len);
1426 if (IS_ERR(cs))
1427 return PTR_ERR(cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001428
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001429 if (vf_flush_wa)
1430 cs = gen8_emit_pipe_control(cs, 0, 0);
Imre Deak9647ff32015-01-25 13:27:11 -08001431
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001432 if (dc_flush_wa)
1433 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_DC_FLUSH_ENABLE,
1434 0);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001435
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001436 cs = gen8_emit_pipe_control(cs, flags, scratch_addr);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001437
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001438 if (dc_flush_wa)
1439 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_CS_STALL, 0);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001440
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001441 intel_ring_advance(request, cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001442
1443 return 0;
1444}
1445
Chris Wilson7c17d372016-01-20 15:43:35 +02001446/*
1447 * Reserve space for 2 NOOPs at the end of each request to be
1448 * used as a workaround for not being allowed to do lite
1449 * restore with HEAD==TAIL (WaIdleLiteRestore).
1450 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001451static void gen8_emit_wa_tail(struct drm_i915_gem_request *request, u32 *cs)
Oscar Mateo4da46e12014-07-24 17:04:27 +01001452{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001453 *cs++ = MI_NOOP;
1454 *cs++ = MI_NOOP;
1455 request->wa_tail = intel_ring_offset(request, cs);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001456}
Oscar Mateo4da46e12014-07-24 17:04:27 +01001457
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001458static void gen8_emit_breadcrumb(struct drm_i915_gem_request *request, u32 *cs)
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001459{
Chris Wilson7c17d372016-01-20 15:43:35 +02001460 /* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */
1461 BUILD_BUG_ON(I915_GEM_HWS_INDEX_ADDR & (1 << 5));
Oscar Mateo4da46e12014-07-24 17:04:27 +01001462
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001463 *cs++ = (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW;
1464 *cs++ = intel_hws_seqno_address(request->engine) | MI_FLUSH_DW_USE_GTT;
1465 *cs++ = 0;
1466 *cs++ = request->global_seqno;
1467 *cs++ = MI_USER_INTERRUPT;
1468 *cs++ = MI_NOOP;
1469 request->tail = intel_ring_offset(request, cs);
Chris Wilson944a36d2017-02-17 16:38:33 +00001470 GEM_BUG_ON(!IS_ALIGNED(request->tail, 8));
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001471
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001472 gen8_emit_wa_tail(request, cs);
Chris Wilson7c17d372016-01-20 15:43:35 +02001473}
Oscar Mateo4da46e12014-07-24 17:04:27 +01001474
Chris Wilson98f29e82016-10-28 13:58:51 +01001475static const int gen8_emit_breadcrumb_sz = 6 + WA_TAIL_DWORDS;
1476
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001477static void gen8_emit_breadcrumb_render(struct drm_i915_gem_request *request,
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001478 u32 *cs)
Chris Wilson7c17d372016-01-20 15:43:35 +02001479{
Michał Winiarskice81a652016-04-12 15:51:55 +02001480 /* We're using qword write, seqno should be aligned to 8 bytes. */
1481 BUILD_BUG_ON(I915_GEM_HWS_INDEX & 1);
1482
Chris Wilson7c17d372016-01-20 15:43:35 +02001483 /* w/a for post sync ops following a GPGPU operation we
1484 * need a prior CS_STALL, which is emitted by the flush
1485 * following the batch.
Michel Thierry53292cd2015-04-15 18:11:33 +01001486 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001487 *cs++ = GFX_OP_PIPE_CONTROL(6);
1488 *cs++ = PIPE_CONTROL_GLOBAL_GTT_IVB | PIPE_CONTROL_CS_STALL |
1489 PIPE_CONTROL_QW_WRITE;
1490 *cs++ = intel_hws_seqno_address(request->engine);
1491 *cs++ = 0;
1492 *cs++ = request->global_seqno;
Michał Winiarskice81a652016-04-12 15:51:55 +02001493 /* We're thrashing one dword of HWS. */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001494 *cs++ = 0;
1495 *cs++ = MI_USER_INTERRUPT;
1496 *cs++ = MI_NOOP;
1497 request->tail = intel_ring_offset(request, cs);
Chris Wilson944a36d2017-02-17 16:38:33 +00001498 GEM_BUG_ON(!IS_ALIGNED(request->tail, 8));
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001499
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001500 gen8_emit_wa_tail(request, cs);
Oscar Mateo4da46e12014-07-24 17:04:27 +01001501}
1502
Chris Wilson98f29e82016-10-28 13:58:51 +01001503static const int gen8_emit_breadcrumb_render_sz = 8 + WA_TAIL_DWORDS;
1504
John Harrison87531812015-05-29 17:43:44 +01001505static int gen8_init_rcs_context(struct drm_i915_gem_request *req)
Thomas Daniele7778be2014-12-02 12:50:48 +00001506{
1507 int ret;
1508
Tvrtko Ursulin4ac96592017-02-14 15:00:17 +00001509 ret = intel_ring_workarounds_emit(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001510 if (ret)
1511 return ret;
1512
Peter Antoine3bbaba02015-07-10 20:13:11 +03001513 ret = intel_rcs_context_init_mocs(req);
1514 /*
1515 * Failing to program the MOCS is non-fatal.The system will not
1516 * run at peak performance. So generate an error and carry on.
1517 */
1518 if (ret)
1519 DRM_ERROR("MOCS failed to program: expect performance issues.\n");
1520
Chris Wilson4e50f082016-10-28 13:58:31 +01001521 return i915_gem_render_state_emit(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001522}
1523
Oscar Mateo73e4d072014-07-24 17:04:48 +01001524/**
1525 * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001526 * @engine: Engine Command Streamer.
Oscar Mateo73e4d072014-07-24 17:04:48 +01001527 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001528void intel_logical_ring_cleanup(struct intel_engine_cs *engine)
Oscar Mateo454afeb2014-07-24 17:04:22 +01001529{
John Harrison6402c332014-10-31 12:00:26 +00001530 struct drm_i915_private *dev_priv;
Oscar Mateo9832b9d2014-07-24 17:04:30 +01001531
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01001532 /*
1533 * Tasklet cannot be active at this point due intel_mark_active/idle
1534 * so this is just for documentation.
1535 */
1536 if (WARN_ON(test_bit(TASKLET_STATE_SCHED, &engine->irq_tasklet.state)))
1537 tasklet_kill(&engine->irq_tasklet);
1538
Chris Wilsonc0336662016-05-06 15:40:21 +01001539 dev_priv = engine->i915;
John Harrison6402c332014-10-31 12:00:26 +00001540
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001541 if (engine->buffer) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001542 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
Dave Gordonb0366a52015-12-08 15:02:36 +00001543 }
Oscar Mateo48d82382014-07-24 17:04:23 +01001544
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001545 if (engine->cleanup)
1546 engine->cleanup(engine);
Oscar Mateo48d82382014-07-24 17:04:23 +01001547
Chris Wilson57e88532016-08-15 10:48:57 +01001548 if (engine->status_page.vma) {
1549 i915_gem_object_unpin_map(engine->status_page.vma->obj);
1550 engine->status_page.vma = NULL;
Oscar Mateo48d82382014-07-24 17:04:23 +01001551 }
Chris Wilsone8a9c582016-12-18 15:37:20 +00001552
1553 intel_engine_cleanup_common(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001554
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001555 lrc_destroy_wa_ctx(engine);
Chris Wilsonc0336662016-05-06 15:40:21 +01001556 engine->i915 = NULL;
Akash Goel3b3f1652016-10-13 22:44:48 +05301557 dev_priv->engine[engine->id] = NULL;
1558 kfree(engine);
Oscar Mateo454afeb2014-07-24 17:04:22 +01001559}
1560
Chris Wilsonddd66c52016-08-02 22:50:31 +01001561void intel_execlists_enable_submission(struct drm_i915_private *dev_priv)
1562{
1563 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301564 enum intel_engine_id id;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001565
Chris Wilson20311bd2016-11-14 20:41:03 +00001566 for_each_engine(engine, dev_priv, id) {
Chris Wilsonf4ea6bd2016-08-02 22:50:32 +01001567 engine->submit_request = execlists_submit_request;
Chris Wilson20311bd2016-11-14 20:41:03 +00001568 engine->schedule = execlists_schedule;
1569 }
Chris Wilsonddd66c52016-08-02 22:50:31 +01001570}
1571
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001572static void
Chris Wilsone1382ef2016-05-06 15:40:20 +01001573logical_ring_default_vfuncs(struct intel_engine_cs *engine)
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001574{
1575 /* Default vfuncs which can be overriden by each engine. */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001576 engine->init_hw = gen8_init_common_ring;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001577 engine->reset_hw = reset_common_ring;
Chris Wilsone8a9c582016-12-18 15:37:20 +00001578
1579 engine->context_pin = execlists_context_pin;
1580 engine->context_unpin = execlists_context_unpin;
1581
Chris Wilsonf73e7392016-12-18 15:37:24 +00001582 engine->request_alloc = execlists_request_alloc;
1583
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001584 engine->emit_flush = gen8_emit_flush;
Chris Wilson9b81d552016-10-28 13:58:50 +01001585 engine->emit_breadcrumb = gen8_emit_breadcrumb;
Chris Wilson98f29e82016-10-28 13:58:51 +01001586 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_sz;
Chris Wilsonf4ea6bd2016-08-02 22:50:32 +01001587 engine->submit_request = execlists_submit_request;
Chris Wilson20311bd2016-11-14 20:41:03 +00001588 engine->schedule = execlists_schedule;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001589
Chris Wilson31bb59c2016-07-01 17:23:27 +01001590 engine->irq_enable = gen8_logical_ring_enable_irq;
1591 engine->irq_disable = gen8_logical_ring_disable_irq;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001592 engine->emit_bb_start = gen8_emit_bb_start;
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001593}
1594
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001595static inline void
Dave Gordonc2c7f242016-07-13 16:03:35 +01001596logical_ring_default_irqs(struct intel_engine_cs *engine)
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001597{
Dave Gordonc2c7f242016-07-13 16:03:35 +01001598 unsigned shift = engine->irq_shift;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001599 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << shift;
1600 engine->irq_keep_mask = GT_CONTEXT_SWITCH_INTERRUPT << shift;
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001601}
1602
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001603static int
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001604lrc_setup_hws(struct intel_engine_cs *engine, struct i915_vma *vma)
Tvrtko Ursulin04794ad2016-04-12 15:40:41 +01001605{
Chris Wilson57e88532016-08-15 10:48:57 +01001606 const int hws_offset = LRC_PPHWSP_PN * PAGE_SIZE;
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001607 void *hws;
Tvrtko Ursulin04794ad2016-04-12 15:40:41 +01001608
1609 /* The HWSP is part of the default context object in LRC mode. */
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001610 hws = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001611 if (IS_ERR(hws))
1612 return PTR_ERR(hws);
Chris Wilson57e88532016-08-15 10:48:57 +01001613
1614 engine->status_page.page_addr = hws + hws_offset;
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001615 engine->status_page.ggtt_offset = i915_ggtt_offset(vma) + hws_offset;
Chris Wilson57e88532016-08-15 10:48:57 +01001616 engine->status_page.vma = vma;
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001617
1618 return 0;
Tvrtko Ursulin04794ad2016-04-12 15:40:41 +01001619}
1620
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001621static void
1622logical_ring_setup(struct intel_engine_cs *engine)
1623{
1624 struct drm_i915_private *dev_priv = engine->i915;
1625 enum forcewake_domains fw_domains;
1626
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01001627 intel_engine_setup_common(engine);
1628
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001629 /* Intentionally left blank. */
1630 engine->buffer = NULL;
1631
1632 fw_domains = intel_uncore_forcewake_for_reg(dev_priv,
1633 RING_ELSP(engine),
1634 FW_REG_WRITE);
1635
1636 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
1637 RING_CONTEXT_STATUS_PTR(engine),
1638 FW_REG_READ | FW_REG_WRITE);
1639
1640 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
1641 RING_CONTEXT_STATUS_BUF_BASE(engine),
1642 FW_REG_READ);
1643
1644 engine->fw_domains = fw_domains;
1645
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001646 tasklet_init(&engine->irq_tasklet,
1647 intel_lrc_irq_handler, (unsigned long)engine);
1648
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001649 logical_ring_default_vfuncs(engine);
1650 logical_ring_default_irqs(engine);
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001651}
1652
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001653static int
1654logical_ring_init(struct intel_engine_cs *engine)
1655{
1656 struct i915_gem_context *dctx = engine->i915->kernel_context;
1657 int ret;
1658
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01001659 ret = intel_engine_init_common(engine);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001660 if (ret)
1661 goto error;
1662
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001663 /* And setup the hardware status page. */
1664 ret = lrc_setup_hws(engine, dctx->engine[engine->id].state);
1665 if (ret) {
1666 DRM_ERROR("Failed to set up hws %s: %d\n", engine->name, ret);
1667 goto error;
1668 }
1669
1670 return 0;
1671
1672error:
1673 intel_logical_ring_cleanup(engine);
1674 return ret;
1675}
1676
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +01001677int logical_render_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001678{
1679 struct drm_i915_private *dev_priv = engine->i915;
1680 int ret;
1681
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001682 logical_ring_setup(engine);
1683
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001684 if (HAS_L3_DPF(dev_priv))
1685 engine->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
1686
1687 /* Override some for render ring. */
1688 if (INTEL_GEN(dev_priv) >= 9)
1689 engine->init_hw = gen9_init_render_ring;
1690 else
1691 engine->init_hw = gen8_init_render_ring;
1692 engine->init_context = gen8_init_rcs_context;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001693 engine->emit_flush = gen8_emit_flush_render;
Chris Wilson9b81d552016-10-28 13:58:50 +01001694 engine->emit_breadcrumb = gen8_emit_breadcrumb_render;
Chris Wilson98f29e82016-10-28 13:58:51 +01001695 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_render_sz;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001696
Chris Wilsonf51455d2017-01-10 14:47:34 +00001697 ret = intel_engine_create_scratch(engine, PAGE_SIZE);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001698 if (ret)
1699 return ret;
1700
1701 ret = intel_init_workaround_bb(engine);
1702 if (ret) {
1703 /*
1704 * We continue even if we fail to initialize WA batch
1705 * because we only expect rare glitches but nothing
1706 * critical to prevent us from using GPU
1707 */
1708 DRM_ERROR("WA batch buffer initialization failed: %d\n",
1709 ret);
1710 }
1711
Tvrtko Ursulind038fc72016-12-16 13:18:42 +00001712 return logical_ring_init(engine);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001713}
1714
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +01001715int logical_xcs_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001716{
1717 logical_ring_setup(engine);
1718
1719 return logical_ring_init(engine);
1720}
1721
Jeff McGee0cea6502015-02-13 10:27:56 -06001722static u32
Chris Wilsonc0336662016-05-06 15:40:21 +01001723make_rpcs(struct drm_i915_private *dev_priv)
Jeff McGee0cea6502015-02-13 10:27:56 -06001724{
1725 u32 rpcs = 0;
1726
1727 /*
1728 * No explicit RPCS request is needed to ensure full
1729 * slice/subslice/EU enablement prior to Gen9.
1730 */
Chris Wilsonc0336662016-05-06 15:40:21 +01001731 if (INTEL_GEN(dev_priv) < 9)
Jeff McGee0cea6502015-02-13 10:27:56 -06001732 return 0;
1733
1734 /*
1735 * Starting in Gen9, render power gating can leave
1736 * slice/subslice/EU in a partially enabled state. We
1737 * must make an explicit request through RPCS for full
1738 * enablement.
1739 */
Imre Deak43b67992016-08-31 19:13:02 +03001740 if (INTEL_INFO(dev_priv)->sseu.has_slice_pg) {
Jeff McGee0cea6502015-02-13 10:27:56 -06001741 rpcs |= GEN8_RPCS_S_CNT_ENABLE;
Imre Deakf08a0c92016-08-31 19:13:04 +03001742 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.slice_mask) <<
Jeff McGee0cea6502015-02-13 10:27:56 -06001743 GEN8_RPCS_S_CNT_SHIFT;
1744 rpcs |= GEN8_RPCS_ENABLE;
1745 }
1746
Imre Deak43b67992016-08-31 19:13:02 +03001747 if (INTEL_INFO(dev_priv)->sseu.has_subslice_pg) {
Jeff McGee0cea6502015-02-13 10:27:56 -06001748 rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
Imre Deak57ec1712016-08-31 19:13:05 +03001749 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.subslice_mask) <<
Jeff McGee0cea6502015-02-13 10:27:56 -06001750 GEN8_RPCS_SS_CNT_SHIFT;
1751 rpcs |= GEN8_RPCS_ENABLE;
1752 }
1753
Imre Deak43b67992016-08-31 19:13:02 +03001754 if (INTEL_INFO(dev_priv)->sseu.has_eu_pg) {
1755 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
Jeff McGee0cea6502015-02-13 10:27:56 -06001756 GEN8_RPCS_EU_MIN_SHIFT;
Imre Deak43b67992016-08-31 19:13:02 +03001757 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
Jeff McGee0cea6502015-02-13 10:27:56 -06001758 GEN8_RPCS_EU_MAX_SHIFT;
1759 rpcs |= GEN8_RPCS_ENABLE;
1760 }
1761
1762 return rpcs;
1763}
1764
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001765static u32 intel_lr_indirect_ctx_offset(struct intel_engine_cs *engine)
Michel Thierry71562912016-02-23 10:31:49 +00001766{
1767 u32 indirect_ctx_offset;
1768
Chris Wilsonc0336662016-05-06 15:40:21 +01001769 switch (INTEL_GEN(engine->i915)) {
Michel Thierry71562912016-02-23 10:31:49 +00001770 default:
Chris Wilsonc0336662016-05-06 15:40:21 +01001771 MISSING_CASE(INTEL_GEN(engine->i915));
Michel Thierry71562912016-02-23 10:31:49 +00001772 /* fall through */
1773 case 9:
1774 indirect_ctx_offset =
1775 GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
1776 break;
1777 case 8:
1778 indirect_ctx_offset =
1779 GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
1780 break;
1781 }
1782
1783 return indirect_ctx_offset;
1784}
1785
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001786static void execlists_init_reg_state(u32 *regs,
Chris Wilsona3aabe82016-10-04 21:11:26 +01001787 struct i915_gem_context *ctx,
1788 struct intel_engine_cs *engine,
1789 struct intel_ring *ring)
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001790{
Chris Wilsona3aabe82016-10-04 21:11:26 +01001791 struct drm_i915_private *dev_priv = engine->i915;
1792 struct i915_hw_ppgtt *ppgtt = ctx->ppgtt ?: dev_priv->mm.aliasing_ppgtt;
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001793 u32 base = engine->mmio_base;
1794 bool rcs = engine->id == RCS;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001795
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001796 /* A context is actually a big batch buffer with several
1797 * MI_LOAD_REGISTER_IMM commands followed by (reg, value) pairs. The
1798 * values we are setting here are only for the first context restore:
1799 * on a subsequent save, the GPU will recreate this batchbuffer with new
1800 * values (including all the missing MI_LOAD_REGISTER_IMM commands that
1801 * we are not initializing here).
1802 */
1803 regs[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(rcs ? 14 : 11) |
1804 MI_LRI_FORCE_POSTED;
1805
1806 CTX_REG(regs, CTX_CONTEXT_CONTROL, RING_CONTEXT_CONTROL(engine),
1807 _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
1808 CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
1809 (HAS_RESOURCE_STREAMER(dev_priv) ?
1810 CTX_CTRL_RS_CTX_ENABLE : 0)));
1811 CTX_REG(regs, CTX_RING_HEAD, RING_HEAD(base), 0);
1812 CTX_REG(regs, CTX_RING_TAIL, RING_TAIL(base), 0);
1813 CTX_REG(regs, CTX_RING_BUFFER_START, RING_START(base), 0);
1814 CTX_REG(regs, CTX_RING_BUFFER_CONTROL, RING_CTL(base),
1815 RING_CTL_SIZE(ring->size) | RING_VALID);
1816 CTX_REG(regs, CTX_BB_HEAD_U, RING_BBADDR_UDW(base), 0);
1817 CTX_REG(regs, CTX_BB_HEAD_L, RING_BBADDR(base), 0);
1818 CTX_REG(regs, CTX_BB_STATE, RING_BBSTATE(base), RING_BB_PPGTT);
1819 CTX_REG(regs, CTX_SECOND_BB_HEAD_U, RING_SBBADDR_UDW(base), 0);
1820 CTX_REG(regs, CTX_SECOND_BB_HEAD_L, RING_SBBADDR(base), 0);
1821 CTX_REG(regs, CTX_SECOND_BB_STATE, RING_SBBSTATE(base), 0);
1822 if (rcs) {
1823 CTX_REG(regs, CTX_BB_PER_CTX_PTR, RING_BB_PER_CTX_PTR(base), 0);
1824 CTX_REG(regs, CTX_RCS_INDIRECT_CTX, RING_INDIRECT_CTX(base), 0);
1825 CTX_REG(regs, CTX_RCS_INDIRECT_CTX_OFFSET,
1826 RING_INDIRECT_CTX_OFFSET(base), 0);
1827
Chris Wilson48bb74e2016-08-15 10:49:04 +01001828 if (engine->wa_ctx.vma) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001829 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001830 u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001831
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001832 regs[CTX_RCS_INDIRECT_CTX + 1] =
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001833 (ggtt_offset + wa_ctx->indirect_ctx.offset) |
1834 (wa_ctx->indirect_ctx.size / CACHELINE_BYTES);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001835
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001836 regs[CTX_RCS_INDIRECT_CTX_OFFSET + 1] =
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001837 intel_lr_indirect_ctx_offset(engine) << 6;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001838
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001839 regs[CTX_BB_PER_CTX_PTR + 1] =
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001840 (ggtt_offset + wa_ctx->per_ctx.offset) | 0x01;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001841 }
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001842 }
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001843
1844 regs[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9) | MI_LRI_FORCE_POSTED;
1845
1846 CTX_REG(regs, CTX_CTX_TIMESTAMP, RING_CTX_TIMESTAMP(base), 0);
Ville Syrjälä0d925ea2015-11-04 23:20:11 +02001847 /* PDP values well be assigned later if needed */
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001848 CTX_REG(regs, CTX_PDP3_UDW, GEN8_RING_PDP_UDW(engine, 3), 0);
1849 CTX_REG(regs, CTX_PDP3_LDW, GEN8_RING_PDP_LDW(engine, 3), 0);
1850 CTX_REG(regs, CTX_PDP2_UDW, GEN8_RING_PDP_UDW(engine, 2), 0);
1851 CTX_REG(regs, CTX_PDP2_LDW, GEN8_RING_PDP_LDW(engine, 2), 0);
1852 CTX_REG(regs, CTX_PDP1_UDW, GEN8_RING_PDP_UDW(engine, 1), 0);
1853 CTX_REG(regs, CTX_PDP1_LDW, GEN8_RING_PDP_LDW(engine, 1), 0);
1854 CTX_REG(regs, CTX_PDP0_UDW, GEN8_RING_PDP_UDW(engine, 0), 0);
1855 CTX_REG(regs, CTX_PDP0_LDW, GEN8_RING_PDP_LDW(engine, 0), 0);
Michel Thierryd7b26332015-04-08 12:13:34 +01001856
Chris Wilson949e8ab2017-02-09 14:40:36 +00001857 if (ppgtt && i915_vm_is_48bit(&ppgtt->base)) {
Michel Thierry2dba3232015-07-30 11:06:23 +01001858 /* 64b PPGTT (48bit canonical)
1859 * PDP0_DESCRIPTOR contains the base address to PML4 and
1860 * other PDP Descriptors are ignored.
1861 */
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001862 ASSIGN_CTX_PML4(ppgtt, regs);
Michel Thierry2dba3232015-07-30 11:06:23 +01001863 }
1864
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001865 if (rcs) {
1866 regs[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
1867 CTX_REG(regs, CTX_R_PWR_CLK_STATE, GEN8_R_PWR_CLK_STATE,
1868 make_rpcs(dev_priv));
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001869 }
Chris Wilsona3aabe82016-10-04 21:11:26 +01001870}
1871
1872static int
1873populate_lr_context(struct i915_gem_context *ctx,
1874 struct drm_i915_gem_object *ctx_obj,
1875 struct intel_engine_cs *engine,
1876 struct intel_ring *ring)
1877{
1878 void *vaddr;
1879 int ret;
1880
1881 ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
1882 if (ret) {
1883 DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
1884 return ret;
1885 }
1886
1887 vaddr = i915_gem_object_pin_map(ctx_obj, I915_MAP_WB);
1888 if (IS_ERR(vaddr)) {
1889 ret = PTR_ERR(vaddr);
1890 DRM_DEBUG_DRIVER("Could not map object pages! (%d)\n", ret);
1891 return ret;
1892 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01001893 ctx_obj->mm.dirty = true;
Chris Wilsona3aabe82016-10-04 21:11:26 +01001894
1895 /* The second page of the context object contains some fields which must
1896 * be set up prior to the first execution. */
1897
1898 execlists_init_reg_state(vaddr + LRC_STATE_PN * PAGE_SIZE,
1899 ctx, engine, ring);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001900
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001901 i915_gem_object_unpin_map(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001902
1903 return 0;
1904}
1905
Oscar Mateo73e4d072014-07-24 17:04:48 +01001906/**
Dave Gordonc5d46ee2016-01-05 12:21:33 +00001907 * intel_lr_context_size() - return the size of the context for an engine
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001908 * @engine: which engine to find the context size for
Dave Gordonc5d46ee2016-01-05 12:21:33 +00001909 *
1910 * Each engine may require a different amount of space for a context image,
1911 * so when allocating (or copying) an image, this function can be used to
1912 * find the right size for the specific engine.
1913 *
1914 * Return: size (in bytes) of an engine-specific context image
1915 *
1916 * Note: this size includes the HWSP, which is part of the context image
1917 * in LRC mode, but does not include the "shared data page" used with
1918 * GuC submission. The caller should account for this if using the GuC.
1919 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001920uint32_t intel_lr_context_size(struct intel_engine_cs *engine)
Oscar Mateo8c8579172014-07-24 17:04:14 +01001921{
1922 int ret = 0;
1923
Chris Wilsonc0336662016-05-06 15:40:21 +01001924 WARN_ON(INTEL_GEN(engine->i915) < 8);
Oscar Mateo8c8579172014-07-24 17:04:14 +01001925
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001926 switch (engine->id) {
Oscar Mateo8c8579172014-07-24 17:04:14 +01001927 case RCS:
Chris Wilsonc0336662016-05-06 15:40:21 +01001928 if (INTEL_GEN(engine->i915) >= 9)
Michael H. Nguyen468c6812014-11-13 17:51:49 +00001929 ret = GEN9_LR_CONTEXT_RENDER_SIZE;
1930 else
1931 ret = GEN8_LR_CONTEXT_RENDER_SIZE;
Oscar Mateo8c8579172014-07-24 17:04:14 +01001932 break;
1933 case VCS:
1934 case BCS:
1935 case VECS:
1936 case VCS2:
1937 ret = GEN8_LR_CONTEXT_OTHER_SIZE;
1938 break;
1939 }
1940
1941 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01001942}
1943
Chris Wilsone2efd132016-05-24 14:53:34 +01001944static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
Chris Wilson978f1e02016-04-28 09:56:54 +01001945 struct intel_engine_cs *engine)
Oscar Mateoede7d422014-07-24 17:04:12 +01001946{
Oscar Mateo8c8579172014-07-24 17:04:14 +01001947 struct drm_i915_gem_object *ctx_obj;
Chris Wilson9021ad02016-05-24 14:53:37 +01001948 struct intel_context *ce = &ctx->engine[engine->id];
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001949 struct i915_vma *vma;
Oscar Mateo8c8579172014-07-24 17:04:14 +01001950 uint32_t context_size;
Chris Wilson7e37f882016-08-02 22:50:21 +01001951 struct intel_ring *ring;
Oscar Mateo8c8579172014-07-24 17:04:14 +01001952 int ret;
1953
Chris Wilson9021ad02016-05-24 14:53:37 +01001954 WARN_ON(ce->state);
Oscar Mateoede7d422014-07-24 17:04:12 +01001955
Chris Wilsonf51455d2017-01-10 14:47:34 +00001956 context_size = round_up(intel_lr_context_size(engine),
1957 I915_GTT_PAGE_SIZE);
Oscar Mateo8c8579172014-07-24 17:04:14 +01001958
Alex Daid1675192015-08-12 15:43:43 +01001959 /* One extra page as the sharing data between driver and GuC */
1960 context_size += PAGE_SIZE * LRC_PPHWSP_PN;
1961
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00001962 ctx_obj = i915_gem_object_create(ctx->i915, context_size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01001963 if (IS_ERR(ctx_obj)) {
Dan Carpenter3126a662015-04-30 17:30:50 +03001964 DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
Chris Wilsonfe3db792016-04-25 13:32:13 +01001965 return PTR_ERR(ctx_obj);
Oscar Mateo8c8579172014-07-24 17:04:14 +01001966 }
1967
Chris Wilsona01cb372017-01-16 15:21:30 +00001968 vma = i915_vma_instance(ctx_obj, &ctx->i915->ggtt.base, NULL);
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001969 if (IS_ERR(vma)) {
1970 ret = PTR_ERR(vma);
1971 goto error_deref_obj;
1972 }
1973
Chris Wilson7e37f882016-08-02 22:50:21 +01001974 ring = intel_engine_create_ring(engine, ctx->ring_size);
Chris Wilsondca33ec2016-08-02 22:50:20 +01001975 if (IS_ERR(ring)) {
1976 ret = PTR_ERR(ring);
Nick Hoathe84fe802015-09-11 12:53:46 +01001977 goto error_deref_obj;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001978 }
1979
Chris Wilsondca33ec2016-08-02 22:50:20 +01001980 ret = populate_lr_context(ctx, ctx_obj, engine, ring);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001981 if (ret) {
1982 DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
Chris Wilsondca33ec2016-08-02 22:50:20 +01001983 goto error_ring_free;
Oscar Mateo84c23772014-07-24 17:04:15 +01001984 }
1985
Chris Wilsondca33ec2016-08-02 22:50:20 +01001986 ce->ring = ring;
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001987 ce->state = vma;
Chris Wilson9021ad02016-05-24 14:53:37 +01001988 ce->initialised = engine->init_context == NULL;
Oscar Mateoede7d422014-07-24 17:04:12 +01001989
1990 return 0;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001991
Chris Wilsondca33ec2016-08-02 22:50:20 +01001992error_ring_free:
Chris Wilson7e37f882016-08-02 22:50:21 +01001993 intel_ring_free(ring);
Nick Hoathe84fe802015-09-11 12:53:46 +01001994error_deref_obj:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01001995 i915_gem_object_put(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001996 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01001997}
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00001998
Chris Wilson821ed7d2016-09-09 14:11:53 +01001999void intel_lr_context_resume(struct drm_i915_private *dev_priv)
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002000{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002001 struct intel_engine_cs *engine;
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002002 struct i915_gem_context *ctx;
Akash Goel3b3f1652016-10-13 22:44:48 +05302003 enum intel_engine_id id;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002004
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002005 /* Because we emit WA_TAIL_DWORDS there may be a disparity
2006 * between our bookkeeping in ce->ring->head and ce->ring->tail and
2007 * that stored in context. As we only write new commands from
2008 * ce->ring->tail onwards, everything before that is junk. If the GPU
2009 * starts reading from its RING_HEAD from the context, it may try to
2010 * execute that junk and die.
2011 *
2012 * So to avoid that we reset the context images upon resume. For
2013 * simplicity, we just zero everything out.
2014 */
2015 list_for_each_entry(ctx, &dev_priv->context_list, link) {
Akash Goel3b3f1652016-10-13 22:44:48 +05302016 for_each_engine(engine, dev_priv, id) {
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002017 struct intel_context *ce = &ctx->engine[engine->id];
2018 u32 *reg;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002019
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002020 if (!ce->state)
2021 continue;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002022
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002023 reg = i915_gem_object_pin_map(ce->state->obj,
2024 I915_MAP_WB);
2025 if (WARN_ON(IS_ERR(reg)))
2026 continue;
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01002027
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002028 reg += LRC_STATE_PN * PAGE_SIZE / sizeof(*reg);
2029 reg[CTX_RING_HEAD+1] = 0;
2030 reg[CTX_RING_TAIL+1] = 0;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002031
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002032 ce->state->obj->mm.dirty = true;
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002033 i915_gem_object_unpin_map(ce->state->obj);
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002034
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002035 ce->ring->head = ce->ring->tail = 0;
2036 ce->ring->last_retired_head = -1;
2037 intel_ring_update_space(ce->ring);
2038 }
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002039 }
2040}