blob: becde55b02a3bfbabbc118e75ed0e60f53d68e65 [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;
Chris Wilsona533b4b2017-03-16 17:16:28 +0000745 GEM_BUG_ON(!ce->pin_count); /* no overflow please! */
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100746
Chris Wilsone8a9c582016-12-18 15:37:20 +0000747 if (!ce->state) {
748 ret = execlists_context_deferred_alloc(ctx, engine);
749 if (ret)
750 goto err;
751 }
Chris Wilson56f6e0a2017-01-05 15:30:20 +0000752 GEM_BUG_ON(!ce->state);
Chris Wilsone8a9c582016-12-18 15:37:20 +0000753
Chris Wilson72b72ae2017-02-10 10:14:22 +0000754 flags = PIN_GLOBAL | PIN_HIGH;
Daniele Ceraolo Spuriofeef2a72016-12-23 15:56:22 -0800755 if (ctx->ggtt_offset_bias)
756 flags |= PIN_OFFSET_BIAS | ctx->ggtt_offset_bias;
Chris Wilson2947e402016-12-18 15:37:23 +0000757
758 ret = i915_vma_pin(ce->state, 0, GEN8_LR_CONTEXT_ALIGN, flags);
Nick Hoathe84fe802015-09-11 12:53:46 +0100759 if (ret)
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100760 goto err;
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000761
Chris Wilsonbf3783e2016-08-15 10:48:54 +0100762 vaddr = i915_gem_object_pin_map(ce->state->obj, I915_MAP_WB);
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +0100763 if (IS_ERR(vaddr)) {
764 ret = PTR_ERR(vaddr);
Chris Wilsonbf3783e2016-08-15 10:48:54 +0100765 goto unpin_vma;
Tvrtko Ursulin82352e92016-01-15 17:12:45 +0000766 }
767
Daniele Ceraolo Spuriod3ef1af2016-12-23 15:56:21 -0800768 ret = intel_ring_pin(ce->ring, ctx->ggtt_offset_bias);
Nick Hoathe84fe802015-09-11 12:53:46 +0100769 if (ret)
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +0100770 goto unpin_map;
Alex Daid1675192015-08-12 15:43:43 +0100771
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000772 intel_lr_context_descriptor_update(ctx, engine);
Chris Wilson9021ad02016-05-24 14:53:37 +0100773
Chris Wilsona3aabe82016-10-04 21:11:26 +0100774 ce->lrc_reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
775 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100776 i915_ggtt_offset(ce->ring->vma);
Chris Wilsona3aabe82016-10-04 21:11:26 +0100777
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100778 ce->state->obj->mm.dirty = true;
Daniel Vettere93c28f2015-09-02 14:33:42 +0200779
Chris Wilson9a6feaf2016-07-20 13:31:50 +0100780 i915_gem_context_get(ctx);
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100781 return 0;
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000782
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +0100783unpin_map:
Chris Wilsonbf3783e2016-08-15 10:48:54 +0100784 i915_gem_object_unpin_map(ce->state->obj);
785unpin_vma:
786 __i915_vma_unpin(ce->state);
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100787err:
Chris Wilson9021ad02016-05-24 14:53:37 +0100788 ce->pin_count = 0;
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000789 return ret;
Oscar Mateodcb4c122014-11-13 10:28:10 +0000790}
791
Chris Wilsone8a9c582016-12-18 15:37:20 +0000792static void execlists_context_unpin(struct intel_engine_cs *engine,
793 struct i915_gem_context *ctx)
Oscar Mateodcb4c122014-11-13 10:28:10 +0000794{
Chris Wilson9021ad02016-05-24 14:53:37 +0100795 struct intel_context *ce = &ctx->engine[engine->id];
Daniel Vetteraf3302b2015-12-04 17:27:15 +0100796
Chris Wilson91c8a322016-07-05 10:40:23 +0100797 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilson9021ad02016-05-24 14:53:37 +0100798 GEM_BUG_ON(ce->pin_count == 0);
Tvrtko Ursulin321fe302016-01-28 10:29:55 +0000799
Chris Wilson9021ad02016-05-24 14:53:37 +0100800 if (--ce->pin_count)
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100801 return;
802
Chris Wilsonaad29fb2016-08-02 22:50:23 +0100803 intel_ring_unpin(ce->ring);
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100804
Chris Wilsonbf3783e2016-08-15 10:48:54 +0100805 i915_gem_object_unpin_map(ce->state->obj);
806 i915_vma_unpin(ce->state);
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100807
Chris Wilson9a6feaf2016-07-20 13:31:50 +0100808 i915_gem_context_put(ctx);
Oscar Mateodcb4c122014-11-13 10:28:10 +0000809}
810
Chris Wilsonf73e7392016-12-18 15:37:24 +0000811static int execlists_request_alloc(struct drm_i915_gem_request *request)
Chris Wilsonef11c012016-12-18 15:37:19 +0000812{
813 struct intel_engine_cs *engine = request->engine;
814 struct intel_context *ce = &request->ctx->engine[engine->id];
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000815 u32 *cs;
Chris Wilsonef11c012016-12-18 15:37:19 +0000816 int ret;
817
Chris Wilsone8a9c582016-12-18 15:37:20 +0000818 GEM_BUG_ON(!ce->pin_count);
819
Chris Wilsonef11c012016-12-18 15:37:19 +0000820 /* Flush enough space to reduce the likelihood of waiting after
821 * we start building the request - in which case we will just
822 * have to repeat work.
823 */
824 request->reserved_space += EXECLISTS_REQUEST_SIZE;
825
Chris Wilsone8a9c582016-12-18 15:37:20 +0000826 GEM_BUG_ON(!ce->ring);
Chris Wilsonef11c012016-12-18 15:37:19 +0000827 request->ring = ce->ring;
828
Chris Wilsonef11c012016-12-18 15:37:19 +0000829 if (i915.enable_guc_submission) {
830 /*
831 * Check that the GuC has space for the request before
832 * going any further, as the i915_add_request() call
833 * later on mustn't fail ...
834 */
835 ret = i915_guc_wq_reserve(request);
836 if (ret)
Chris Wilsone8a9c582016-12-18 15:37:20 +0000837 goto err;
Chris Wilsonef11c012016-12-18 15:37:19 +0000838 }
839
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000840 cs = intel_ring_begin(request, 0);
841 if (IS_ERR(cs)) {
842 ret = PTR_ERR(cs);
Chris Wilsonef11c012016-12-18 15:37:19 +0000843 goto err_unreserve;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000844 }
Chris Wilsonef11c012016-12-18 15:37:19 +0000845
846 if (!ce->initialised) {
847 ret = engine->init_context(request);
848 if (ret)
849 goto err_unreserve;
850
851 ce->initialised = true;
852 }
853
854 /* Note that after this point, we have committed to using
855 * this request as it is being used to both track the
856 * state of engine initialisation and liveness of the
857 * golden renderstate above. Think twice before you try
858 * to cancel/unwind this request now.
859 */
860
861 request->reserved_space -= EXECLISTS_REQUEST_SIZE;
862 return 0;
863
864err_unreserve:
865 if (i915.enable_guc_submission)
866 i915_guc_wq_unreserve(request);
Chris Wilsone8a9c582016-12-18 15:37:20 +0000867err:
Chris Wilsonef11c012016-12-18 15:37:19 +0000868 return ret;
869}
870
Arun Siluvery9e000842015-07-03 14:27:31 +0100871/*
872 * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
873 * PIPE_CONTROL instruction. This is required for the flush to happen correctly
874 * but there is a slight complication as this is applied in WA batch where the
875 * values are only initialized once so we cannot take register value at the
876 * beginning and reuse it further; hence we save its value to memory, upload a
877 * constant value with bit21 set and then we restore it back with the saved value.
878 * To simplify the WA, a constant value is formed by using the default value
879 * of this register. This shouldn't be a problem because we are only modifying
880 * it for a short period and this batch in non-premptible. We can ofcourse
881 * use additional instructions that read the actual value of the register
882 * at that time and set our bit of interest but it makes the WA complicated.
883 *
884 * This WA is also required for Gen9 so extracting as a function avoids
885 * code duplication.
886 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000887static u32 *
888gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery9e000842015-07-03 14:27:31 +0100889{
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000890 *batch++ = MI_STORE_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
891 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
892 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
893 *batch++ = 0;
Arun Siluvery9e000842015-07-03 14:27:31 +0100894
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000895 *batch++ = MI_LOAD_REGISTER_IMM(1);
896 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
897 *batch++ = 0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES;
Arun Siluvery9e000842015-07-03 14:27:31 +0100898
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000899 batch = gen8_emit_pipe_control(batch,
900 PIPE_CONTROL_CS_STALL |
901 PIPE_CONTROL_DC_FLUSH_ENABLE,
902 0);
Arun Siluvery9e000842015-07-03 14:27:31 +0100903
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000904 *batch++ = MI_LOAD_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
905 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
906 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
907 *batch++ = 0;
Arun Siluvery9e000842015-07-03 14:27:31 +0100908
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000909 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +0100910}
911
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200912/*
913 * Typically we only have one indirect_ctx and per_ctx batch buffer which are
914 * initialized at the beginning and shared across all contexts but this field
915 * helps us to have multiple batches at different offsets and select them based
916 * on a criteria. At the moment this batch always start at the beginning of the page
917 * and at this point we don't have multiple wa_ctx batch buffers.
Arun Siluvery17ee9502015-06-19 19:07:01 +0100918 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200919 * The number of WA applied are not known at the beginning; we use this field
920 * to return the no of DWORDS written.
Arun Siluvery17ee9502015-06-19 19:07:01 +0100921 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200922 * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
923 * so it adds NOOPs as padding to make it cacheline aligned.
924 * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
925 * makes a complete batch buffer.
Arun Siluvery17ee9502015-06-19 19:07:01 +0100926 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000927static u32 *gen8_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery17ee9502015-06-19 19:07:01 +0100928{
Arun Siluvery7ad00d12015-06-19 18:37:12 +0100929 /* WaDisableCtxRestoreArbitration:bdw,chv */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000930 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
Arun Siluvery17ee9502015-06-19 19:07:01 +0100931
Arun Siluveryc82435b2015-06-19 18:37:13 +0100932 /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000933 if (IS_BROADWELL(engine->i915))
934 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
Arun Siluveryc82435b2015-06-19 18:37:13 +0100935
Arun Siluvery0160f052015-06-23 15:46:57 +0100936 /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
937 /* Actual scratch location is at 128 bytes offset */
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000938 batch = gen8_emit_pipe_control(batch,
939 PIPE_CONTROL_FLUSH_L3 |
940 PIPE_CONTROL_GLOBAL_GTT_IVB |
941 PIPE_CONTROL_CS_STALL |
942 PIPE_CONTROL_QW_WRITE,
943 i915_ggtt_offset(engine->scratch) +
944 2 * CACHELINE_BYTES);
Arun Siluvery0160f052015-06-23 15:46:57 +0100945
Arun Siluvery17ee9502015-06-19 19:07:01 +0100946 /* Pad to end of cacheline */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000947 while ((unsigned long)batch % CACHELINE_BYTES)
948 *batch++ = MI_NOOP;
Arun Siluvery17ee9502015-06-19 19:07:01 +0100949
950 /*
951 * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
952 * execution depends on the length specified in terms of cache lines
953 * in the register CTX_RCS_INDIRECT_CTX
954 */
955
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000956 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +0100957}
958
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200959/*
960 * This batch is started immediately after indirect_ctx batch. Since we ensure
961 * that indirect_ctx ends on a cacheline this batch is aligned automatically.
Arun Siluvery17ee9502015-06-19 19:07:01 +0100962 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200963 * The number of DWORDS written are returned using this field.
Arun Siluvery17ee9502015-06-19 19:07:01 +0100964 *
965 * This batch is terminated with MI_BATCH_BUFFER_END and so we need not add padding
966 * to align it with cacheline as padding after MI_BATCH_BUFFER_END is redundant.
967 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000968static u32 *gen8_init_perctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery17ee9502015-06-19 19:07:01 +0100969{
Arun Siluvery7ad00d12015-06-19 18:37:12 +0100970 /* WaDisableCtxRestoreArbitration:bdw,chv */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000971 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
972 *batch++ = MI_BATCH_BUFFER_END;
Arun Siluvery7ad00d12015-06-19 18:37:12 +0100973
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000974 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +0100975}
976
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000977static u32 *gen9_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery0504cff2015-07-14 15:01:27 +0100978{
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200979 /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt,glk */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000980 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
Arun Siluverya4106a72015-07-14 15:01:29 +0100981
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200982 /* WaDisableGatherAtSetShaderCommonSlice:skl,bxt,kbl,glk */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000983 *batch++ = MI_LOAD_REGISTER_IMM(1);
984 *batch++ = i915_mmio_reg_offset(COMMON_SLICE_CHICKEN2);
985 *batch++ = _MASKED_BIT_DISABLE(
986 GEN9_DISABLE_GATHER_AT_SET_SHADER_COMMON_SLICE);
987 *batch++ = MI_NOOP;
Mika Kuoppala873e8172016-07-20 14:26:13 +0300988
Mika Kuoppala066d4622016-06-07 17:19:15 +0300989 /* WaClearSlmSpaceAtContextSwitch:kbl */
990 /* Actual scratch location is at 128 bytes offset */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +0000991 if (IS_KBL_REVID(engine->i915, 0, KBL_REVID_A0)) {
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +0000992 batch = gen8_emit_pipe_control(batch,
993 PIPE_CONTROL_FLUSH_L3 |
994 PIPE_CONTROL_GLOBAL_GTT_IVB |
995 PIPE_CONTROL_CS_STALL |
996 PIPE_CONTROL_QW_WRITE,
997 i915_ggtt_offset(engine->scratch)
998 + 2 * CACHELINE_BYTES);
Mika Kuoppala066d4622016-06-07 17:19:15 +0300999 }
Tim Gore3485d992016-07-05 10:01:30 +01001000
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001001 /* WaMediaPoolStateCmdInWABB:bxt,glk */
Tim Gore3485d992016-07-05 10:01:30 +01001002 if (HAS_POOLED_EU(engine->i915)) {
1003 /*
1004 * EU pool configuration is setup along with golden context
1005 * during context initialization. This value depends on
1006 * device type (2x6 or 3x6) and needs to be updated based
1007 * on which subslice is disabled especially for 2x6
1008 * devices, however it is safe to load default
1009 * configuration of 3x6 device instead of masking off
1010 * corresponding bits because HW ignores bits of a disabled
1011 * subslice and drops down to appropriate config. Please
1012 * see render_state_setup() in i915_gem_render_state.c for
1013 * possible configurations, to avoid duplication they are
1014 * not shown here again.
1015 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001016 *batch++ = GEN9_MEDIA_POOL_STATE;
1017 *batch++ = GEN9_MEDIA_POOL_ENABLE;
1018 *batch++ = 0x00777000;
1019 *batch++ = 0;
1020 *batch++ = 0;
1021 *batch++ = 0;
Tim Gore3485d992016-07-05 10:01:30 +01001022 }
1023
Arun Siluvery0504cff2015-07-14 15:01:27 +01001024 /* Pad to end of cacheline */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001025 while ((unsigned long)batch % CACHELINE_BYTES)
1026 *batch++ = MI_NOOP;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001027
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001028 return batch;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001029}
1030
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001031static u32 *gen9_init_perctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery0504cff2015-07-14 15:01:27 +01001032{
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001033 *batch++ = MI_BATCH_BUFFER_END;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001034
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001035 return batch;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001036}
1037
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001038#define CTX_WA_BB_OBJ_SIZE (PAGE_SIZE)
1039
1040static int lrc_setup_wa_ctx(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001041{
Chris Wilson48bb74e2016-08-15 10:49:04 +01001042 struct drm_i915_gem_object *obj;
1043 struct i915_vma *vma;
1044 int err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001045
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001046 obj = i915_gem_object_create(engine->i915, CTX_WA_BB_OBJ_SIZE);
Chris Wilson48bb74e2016-08-15 10:49:04 +01001047 if (IS_ERR(obj))
1048 return PTR_ERR(obj);
1049
Chris Wilsona01cb372017-01-16 15:21:30 +00001050 vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
Chris Wilson48bb74e2016-08-15 10:49:04 +01001051 if (IS_ERR(vma)) {
1052 err = PTR_ERR(vma);
1053 goto err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001054 }
1055
Chris Wilson48bb74e2016-08-15 10:49:04 +01001056 err = i915_vma_pin(vma, 0, PAGE_SIZE, PIN_GLOBAL | PIN_HIGH);
1057 if (err)
1058 goto err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001059
Chris Wilson48bb74e2016-08-15 10:49:04 +01001060 engine->wa_ctx.vma = vma;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001061 return 0;
Chris Wilson48bb74e2016-08-15 10:49:04 +01001062
1063err:
1064 i915_gem_object_put(obj);
1065 return err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001066}
1067
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001068static void lrc_destroy_wa_ctx(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001069{
Chris Wilson19880c42016-08-15 10:49:05 +01001070 i915_vma_unpin_and_release(&engine->wa_ctx.vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001071}
1072
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001073typedef u32 *(*wa_bb_func_t)(struct intel_engine_cs *engine, u32 *batch);
1074
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001075static int intel_init_workaround_bb(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001076{
Chris Wilson48bb74e2016-08-15 10:49:04 +01001077 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001078 struct i915_wa_ctx_bb *wa_bb[2] = { &wa_ctx->indirect_ctx,
1079 &wa_ctx->per_ctx };
1080 wa_bb_func_t wa_bb_fn[2];
Arun Siluvery17ee9502015-06-19 19:07:01 +01001081 struct page *page;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001082 void *batch, *batch_ptr;
1083 unsigned int i;
Chris Wilson48bb74e2016-08-15 10:49:04 +01001084 int ret;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001085
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001086 if (WARN_ON(engine->id != RCS || !engine->scratch))
1087 return -EINVAL;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001088
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001089 switch (INTEL_GEN(engine->i915)) {
1090 case 9:
1091 wa_bb_fn[0] = gen9_init_indirectctx_bb;
1092 wa_bb_fn[1] = gen9_init_perctx_bb;
1093 break;
1094 case 8:
1095 wa_bb_fn[0] = gen8_init_indirectctx_bb;
1096 wa_bb_fn[1] = gen8_init_perctx_bb;
1097 break;
1098 default:
1099 MISSING_CASE(INTEL_GEN(engine->i915));
Arun Siluvery5e60d792015-06-23 15:50:44 +01001100 return 0;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001101 }
Arun Siluvery5e60d792015-06-23 15:50:44 +01001102
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001103 ret = lrc_setup_wa_ctx(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001104 if (ret) {
1105 DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
1106 return ret;
1107 }
1108
Chris Wilson48bb74e2016-08-15 10:49:04 +01001109 page = i915_gem_object_get_dirty_page(wa_ctx->vma->obj, 0);
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001110 batch = batch_ptr = kmap_atomic(page);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001111
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001112 /*
1113 * Emit the two workaround batch buffers, recording the offset from the
1114 * start of the workaround batch buffer object for each and their
1115 * respective sizes.
1116 */
1117 for (i = 0; i < ARRAY_SIZE(wa_bb_fn); i++) {
1118 wa_bb[i]->offset = batch_ptr - batch;
1119 if (WARN_ON(!IS_ALIGNED(wa_bb[i]->offset, CACHELINE_BYTES))) {
1120 ret = -EINVAL;
1121 break;
1122 }
1123 batch_ptr = wa_bb_fn[i](engine, batch_ptr);
1124 wa_bb[i]->size = batch_ptr - (batch + wa_bb[i]->offset);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001125 }
1126
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001127 BUG_ON(batch_ptr - batch > CTX_WA_BB_OBJ_SIZE);
1128
Arun Siluvery17ee9502015-06-19 19:07:01 +01001129 kunmap_atomic(batch);
1130 if (ret)
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001131 lrc_destroy_wa_ctx(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001132
1133 return ret;
1134}
1135
Chris Wilson22cc4402017-02-04 11:05:19 +00001136static u32 port_seqno(struct execlist_port *port)
1137{
1138 return port->request ? port->request->global_seqno : 0;
1139}
1140
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001141static int gen8_init_common_ring(struct intel_engine_cs *engine)
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001142{
Chris Wilsonc0336662016-05-06 15:40:21 +01001143 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001144 int ret;
1145
1146 ret = intel_mocs_init_engine(engine);
1147 if (ret)
1148 return ret;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001149
Chris Wilsonad07dfc2016-10-07 07:53:26 +01001150 intel_engine_reset_breadcrumbs(engine);
Chris Wilsonf3b8f912017-01-05 15:30:21 +00001151 intel_engine_init_hangcheck(engine);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001152
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001153 I915_WRITE(RING_HWSTAM(engine->mmio_base), 0xffffffff);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001154 I915_WRITE(RING_MODE_GEN7(engine),
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001155 _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
Chris Wilsonf3b8f912017-01-05 15:30:21 +00001156 I915_WRITE(RING_HWS_PGA(engine->mmio_base),
1157 engine->status_page.ggtt_offset);
1158 POSTING_READ(RING_HWS_PGA(engine->mmio_base));
Michel Thierrydfc53c52015-09-28 13:25:12 +01001159
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001160 DRM_DEBUG_DRIVER("Execlists enabled for %s\n", engine->name);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001161
Chris Wilsonc87d50c2016-10-04 21:11:27 +01001162 /* After a GPU reset, we may have requests to replay */
Chris Wilsonf7470262017-01-24 15:20:21 +00001163 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
Chris Wilson31de7352017-03-16 12:56:18 +00001164 if (!i915.enable_guc_submission && !execlists_elsp_idle(engine)) {
Chris Wilson22cc4402017-02-04 11:05:19 +00001165 DRM_DEBUG_DRIVER("Restarting %s from requests [0x%x, 0x%x]\n",
1166 engine->name,
1167 port_seqno(&engine->execlist_port[0]),
1168 port_seqno(&engine->execlist_port[1]));
Chris Wilsonc87d50c2016-10-04 21:11:27 +01001169 engine->execlist_port[0].count = 0;
1170 engine->execlist_port[1].count = 0;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001171 execlists_submit_ports(engine);
Chris Wilsonc87d50c2016-10-04 21:11:27 +01001172 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01001173
1174 return 0;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001175}
1176
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001177static int gen8_init_render_ring(struct intel_engine_cs *engine)
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001178{
Chris Wilsonc0336662016-05-06 15:40:21 +01001179 struct drm_i915_private *dev_priv = engine->i915;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001180 int ret;
1181
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001182 ret = gen8_init_common_ring(engine);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001183 if (ret)
1184 return ret;
1185
1186 /* We need to disable the AsyncFlip performance optimisations in order
1187 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1188 * programmed to '1' on all products.
1189 *
1190 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
1191 */
1192 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1193
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001194 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1195
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001196 return init_workarounds_ring(engine);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001197}
1198
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001199static int gen9_init_render_ring(struct intel_engine_cs *engine)
Damien Lespiau82ef8222015-02-09 19:33:08 +00001200{
1201 int ret;
1202
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001203 ret = gen8_init_common_ring(engine);
Damien Lespiau82ef8222015-02-09 19:33:08 +00001204 if (ret)
1205 return ret;
1206
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001207 return init_workarounds_ring(engine);
Damien Lespiau82ef8222015-02-09 19:33:08 +00001208}
1209
Chris Wilson821ed7d2016-09-09 14:11:53 +01001210static void reset_common_ring(struct intel_engine_cs *engine,
1211 struct drm_i915_gem_request *request)
1212{
Chris Wilson821ed7d2016-09-09 14:11:53 +01001213 struct execlist_port *port = engine->execlist_port;
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001214 struct intel_context *ce;
1215
1216 /* If the request was innocent, we leave the request in the ELSP
1217 * and will try to replay it on restarting. The context image may
1218 * have been corrupted by the reset, in which case we may have
1219 * to service a new GPU hang, but more likely we can continue on
1220 * without impact.
1221 *
1222 * If the request was guilty, we presume the context is corrupt
1223 * and have to at least restore the RING register in the context
1224 * image back to the expected values to skip over the guilty request.
1225 */
1226 if (!request || request->fence.error != -EIO)
1227 return;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001228
Chris Wilsona3aabe82016-10-04 21:11:26 +01001229 /* We want a simple context + ring to execute the breadcrumb update.
1230 * We cannot rely on the context being intact across the GPU hang,
1231 * so clear it and rebuild just what we need for the breadcrumb.
1232 * All pending requests for this context will be zapped, and any
1233 * future request will be after userspace has had the opportunity
1234 * to recreate its own state.
1235 */
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001236 ce = &request->ctx->engine[engine->id];
Chris Wilsona3aabe82016-10-04 21:11:26 +01001237 execlists_init_reg_state(ce->lrc_reg_state,
1238 request->ctx, engine, ce->ring);
1239
Chris Wilson821ed7d2016-09-09 14:11:53 +01001240 /* Move the RING_HEAD onto the breadcrumb, past the hanging batch */
Chris Wilsona3aabe82016-10-04 21:11:26 +01001241 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
1242 i915_ggtt_offset(ce->ring->vma);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001243 ce->lrc_reg_state[CTX_RING_HEAD+1] = request->postfix;
Chris Wilsona3aabe82016-10-04 21:11:26 +01001244
Chris Wilson821ed7d2016-09-09 14:11:53 +01001245 request->ring->head = request->postfix;
1246 request->ring->last_retired_head = -1;
1247 intel_ring_update_space(request->ring);
1248
Chris Wilson821ed7d2016-09-09 14:11:53 +01001249 /* Catch up with any missed context-switch interrupts */
Chris Wilson821ed7d2016-09-09 14:11:53 +01001250 if (request->ctx != port[0].request->ctx) {
1251 i915_gem_request_put(port[0].request);
1252 port[0] = port[1];
1253 memset(&port[1], 0, sizeof(port[1]));
1254 }
1255
Chris Wilson821ed7d2016-09-09 14:11:53 +01001256 GEM_BUG_ON(request->ctx != port[0].request->ctx);
Chris Wilsona3aabe82016-10-04 21:11:26 +01001257
1258 /* Reset WaIdleLiteRestore:bdw,skl as well */
1259 request->tail = request->wa_tail - WA_TAIL_DWORDS * sizeof(u32);
Chris Wilson944a36d2017-02-17 16:38:33 +00001260 GEM_BUG_ON(!IS_ALIGNED(request->tail, 8));
Chris Wilson821ed7d2016-09-09 14:11:53 +01001261}
1262
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001263static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request *req)
1264{
1265 struct i915_hw_ppgtt *ppgtt = req->ctx->ppgtt;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001266 struct intel_engine_cs *engine = req->engine;
Mika Kuoppalae7167762017-02-28 17:28:10 +02001267 const int num_lri_cmds = GEN8_3LVL_PDPES * 2;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001268 u32 *cs;
1269 int i;
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001270
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001271 cs = intel_ring_begin(req, num_lri_cmds * 2 + 2);
1272 if (IS_ERR(cs))
1273 return PTR_ERR(cs);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001274
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001275 *cs++ = MI_LOAD_REGISTER_IMM(num_lri_cmds);
Mika Kuoppalae7167762017-02-28 17:28:10 +02001276 for (i = GEN8_3LVL_PDPES - 1; i >= 0; i--) {
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001277 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
1278
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001279 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, i));
1280 *cs++ = upper_32_bits(pd_daddr);
1281 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, i));
1282 *cs++ = lower_32_bits(pd_daddr);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001283 }
1284
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001285 *cs++ = MI_NOOP;
1286 intel_ring_advance(req, cs);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001287
1288 return 0;
1289}
1290
John Harrisonbe795fc2015-05-29 17:44:03 +01001291static int gen8_emit_bb_start(struct drm_i915_gem_request *req,
Chris Wilson803688b2016-08-02 22:50:27 +01001292 u64 offset, u32 len,
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001293 const unsigned int flags)
Oscar Mateo15648582014-07-24 17:04:32 +01001294{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001295 u32 *cs;
Oscar Mateo15648582014-07-24 17:04:32 +01001296 int ret;
1297
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001298 /* Don't rely in hw updating PDPs, specially in lite-restore.
1299 * Ideally, we should set Force PD Restore in ctx descriptor,
1300 * but we can't. Force Restore would be a second option, but
1301 * it is unsafe in case of lite-restore (because the ctx is
Michel Thierry2dba3232015-07-30 11:06:23 +01001302 * not idle). PML4 is allocated during ppgtt init so this is
1303 * not needed in 48-bit.*/
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001304 if (req->ctx->ppgtt &&
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001305 (intel_engine_flag(req->engine) & req->ctx->ppgtt->pd_dirty_rings) &&
1306 !i915_vm_is_48bit(&req->ctx->ppgtt->base) &&
1307 !intel_vgpu_active(req->i915)) {
1308 ret = intel_logical_ring_emit_pdps(req);
1309 if (ret)
1310 return ret;
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001311
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00001312 req->ctx->ppgtt->pd_dirty_rings &= ~intel_engine_flag(req->engine);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001313 }
1314
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001315 cs = intel_ring_begin(req, 4);
1316 if (IS_ERR(cs))
1317 return PTR_ERR(cs);
Oscar Mateo15648582014-07-24 17:04:32 +01001318
1319 /* FIXME(BDW): Address space and security selectors. */
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001320 *cs++ = MI_BATCH_BUFFER_START_GEN8 |
1321 (flags & I915_DISPATCH_SECURE ? 0 : BIT(8)) |
1322 (flags & I915_DISPATCH_RS ? MI_BATCH_RESOURCE_STREAMER : 0);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001323 *cs++ = lower_32_bits(offset);
1324 *cs++ = upper_32_bits(offset);
1325 *cs++ = MI_NOOP;
1326 intel_ring_advance(req, cs);
Oscar Mateo15648582014-07-24 17:04:32 +01001327
1328 return 0;
1329}
1330
Chris Wilson31bb59c2016-07-01 17:23:27 +01001331static void gen8_logical_ring_enable_irq(struct intel_engine_cs *engine)
Oscar Mateo73d477f2014-07-24 17:04:31 +01001332{
Chris Wilsonc0336662016-05-06 15:40:21 +01001333 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson31bb59c2016-07-01 17:23:27 +01001334 I915_WRITE_IMR(engine,
1335 ~(engine->irq_enable_mask | engine->irq_keep_mask));
1336 POSTING_READ_FW(RING_IMR(engine->mmio_base));
Oscar Mateo73d477f2014-07-24 17:04:31 +01001337}
1338
Chris Wilson31bb59c2016-07-01 17:23:27 +01001339static void gen8_logical_ring_disable_irq(struct intel_engine_cs *engine)
Oscar Mateo73d477f2014-07-24 17:04:31 +01001340{
Chris Wilsonc0336662016-05-06 15:40:21 +01001341 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson31bb59c2016-07-01 17:23:27 +01001342 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Oscar Mateo73d477f2014-07-24 17:04:31 +01001343}
1344
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001345static int gen8_emit_flush(struct drm_i915_gem_request *request, u32 mode)
Oscar Mateo47122742014-07-24 17:04:28 +01001346{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001347 u32 cmd, *cs;
Oscar Mateo47122742014-07-24 17:04:28 +01001348
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001349 cs = intel_ring_begin(request, 4);
1350 if (IS_ERR(cs))
1351 return PTR_ERR(cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001352
1353 cmd = MI_FLUSH_DW + 1;
1354
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001355 /* We always require a command barrier so that subsequent
1356 * commands, such as breadcrumb interrupts, are strictly ordered
1357 * wrt the contents of the write cache being flushed to memory
1358 * (and thus being coherent from the CPU).
1359 */
1360 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1361
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001362 if (mode & EMIT_INVALIDATE) {
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001363 cmd |= MI_INVALIDATE_TLB;
Chris Wilson1dae2df2016-08-02 22:50:19 +01001364 if (request->engine->id == VCS)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001365 cmd |= MI_INVALIDATE_BSD;
Oscar Mateo47122742014-07-24 17:04:28 +01001366 }
1367
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001368 *cs++ = cmd;
1369 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
1370 *cs++ = 0; /* upper addr */
1371 *cs++ = 0; /* value */
1372 intel_ring_advance(request, cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001373
1374 return 0;
1375}
1376
John Harrison7deb4d32015-05-29 17:43:59 +01001377static int gen8_emit_flush_render(struct drm_i915_gem_request *request,
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001378 u32 mode)
Oscar Mateo47122742014-07-24 17:04:28 +01001379{
Chris Wilsonb5321f32016-08-02 22:50:18 +01001380 struct intel_engine_cs *engine = request->engine;
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001381 u32 scratch_addr =
1382 i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001383 bool vf_flush_wa = false, dc_flush_wa = false;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001384 u32 *cs, flags = 0;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001385 int len;
Oscar Mateo47122742014-07-24 17:04:28 +01001386
1387 flags |= PIPE_CONTROL_CS_STALL;
1388
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001389 if (mode & EMIT_FLUSH) {
Oscar Mateo47122742014-07-24 17:04:28 +01001390 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
1391 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -08001392 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +01001393 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Oscar Mateo47122742014-07-24 17:04:28 +01001394 }
1395
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001396 if (mode & EMIT_INVALIDATE) {
Oscar Mateo47122742014-07-24 17:04:28 +01001397 flags |= PIPE_CONTROL_TLB_INVALIDATE;
1398 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
1399 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
1400 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
1401 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
1402 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
1403 flags |= PIPE_CONTROL_QW_WRITE;
1404 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Oscar Mateo47122742014-07-24 17:04:28 +01001405
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001406 /*
1407 * On GEN9: before VF_CACHE_INVALIDATE we need to emit a NULL
1408 * pipe control.
1409 */
Chris Wilsonc0336662016-05-06 15:40:21 +01001410 if (IS_GEN9(request->i915))
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001411 vf_flush_wa = true;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001412
1413 /* WaForGAMHang:kbl */
1414 if (IS_KBL_REVID(request->i915, 0, KBL_REVID_B0))
1415 dc_flush_wa = true;
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001416 }
Imre Deak9647ff32015-01-25 13:27:11 -08001417
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001418 len = 6;
1419
1420 if (vf_flush_wa)
1421 len += 6;
1422
1423 if (dc_flush_wa)
1424 len += 12;
1425
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001426 cs = intel_ring_begin(request, len);
1427 if (IS_ERR(cs))
1428 return PTR_ERR(cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001429
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001430 if (vf_flush_wa)
1431 cs = gen8_emit_pipe_control(cs, 0, 0);
Imre Deak9647ff32015-01-25 13:27:11 -08001432
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001433 if (dc_flush_wa)
1434 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_DC_FLUSH_ENABLE,
1435 0);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001436
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001437 cs = gen8_emit_pipe_control(cs, flags, scratch_addr);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001438
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001439 if (dc_flush_wa)
1440 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_CS_STALL, 0);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001441
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001442 intel_ring_advance(request, cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001443
1444 return 0;
1445}
1446
Chris Wilson7c17d372016-01-20 15:43:35 +02001447/*
1448 * Reserve space for 2 NOOPs at the end of each request to be
1449 * used as a workaround for not being allowed to do lite
1450 * restore with HEAD==TAIL (WaIdleLiteRestore).
1451 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001452static void gen8_emit_wa_tail(struct drm_i915_gem_request *request, u32 *cs)
Oscar Mateo4da46e12014-07-24 17:04:27 +01001453{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001454 *cs++ = MI_NOOP;
1455 *cs++ = MI_NOOP;
1456 request->wa_tail = intel_ring_offset(request, cs);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001457}
Oscar Mateo4da46e12014-07-24 17:04:27 +01001458
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001459static void gen8_emit_breadcrumb(struct drm_i915_gem_request *request, u32 *cs)
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001460{
Chris Wilson7c17d372016-01-20 15:43:35 +02001461 /* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */
1462 BUILD_BUG_ON(I915_GEM_HWS_INDEX_ADDR & (1 << 5));
Oscar Mateo4da46e12014-07-24 17:04:27 +01001463
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001464 *cs++ = (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW;
1465 *cs++ = intel_hws_seqno_address(request->engine) | MI_FLUSH_DW_USE_GTT;
1466 *cs++ = 0;
1467 *cs++ = request->global_seqno;
1468 *cs++ = MI_USER_INTERRUPT;
1469 *cs++ = MI_NOOP;
1470 request->tail = intel_ring_offset(request, cs);
Chris Wilson944a36d2017-02-17 16:38:33 +00001471 GEM_BUG_ON(!IS_ALIGNED(request->tail, 8));
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001472
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001473 gen8_emit_wa_tail(request, cs);
Chris Wilson7c17d372016-01-20 15:43:35 +02001474}
Oscar Mateo4da46e12014-07-24 17:04:27 +01001475
Chris Wilson98f29e82016-10-28 13:58:51 +01001476static const int gen8_emit_breadcrumb_sz = 6 + WA_TAIL_DWORDS;
1477
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001478static void gen8_emit_breadcrumb_render(struct drm_i915_gem_request *request,
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001479 u32 *cs)
Chris Wilson7c17d372016-01-20 15:43:35 +02001480{
Michał Winiarskice81a652016-04-12 15:51:55 +02001481 /* We're using qword write, seqno should be aligned to 8 bytes. */
1482 BUILD_BUG_ON(I915_GEM_HWS_INDEX & 1);
1483
Chris Wilson7c17d372016-01-20 15:43:35 +02001484 /* w/a for post sync ops following a GPGPU operation we
1485 * need a prior CS_STALL, which is emitted by the flush
1486 * following the batch.
Michel Thierry53292cd2015-04-15 18:11:33 +01001487 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001488 *cs++ = GFX_OP_PIPE_CONTROL(6);
1489 *cs++ = PIPE_CONTROL_GLOBAL_GTT_IVB | PIPE_CONTROL_CS_STALL |
1490 PIPE_CONTROL_QW_WRITE;
1491 *cs++ = intel_hws_seqno_address(request->engine);
1492 *cs++ = 0;
1493 *cs++ = request->global_seqno;
Michał Winiarskice81a652016-04-12 15:51:55 +02001494 /* We're thrashing one dword of HWS. */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001495 *cs++ = 0;
1496 *cs++ = MI_USER_INTERRUPT;
1497 *cs++ = MI_NOOP;
1498 request->tail = intel_ring_offset(request, cs);
Chris Wilson944a36d2017-02-17 16:38:33 +00001499 GEM_BUG_ON(!IS_ALIGNED(request->tail, 8));
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001500
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001501 gen8_emit_wa_tail(request, cs);
Oscar Mateo4da46e12014-07-24 17:04:27 +01001502}
1503
Chris Wilson98f29e82016-10-28 13:58:51 +01001504static const int gen8_emit_breadcrumb_render_sz = 8 + WA_TAIL_DWORDS;
1505
John Harrison87531812015-05-29 17:43:44 +01001506static int gen8_init_rcs_context(struct drm_i915_gem_request *req)
Thomas Daniele7778be2014-12-02 12:50:48 +00001507{
1508 int ret;
1509
Tvrtko Ursulin4ac96592017-02-14 15:00:17 +00001510 ret = intel_ring_workarounds_emit(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001511 if (ret)
1512 return ret;
1513
Peter Antoine3bbaba02015-07-10 20:13:11 +03001514 ret = intel_rcs_context_init_mocs(req);
1515 /*
1516 * Failing to program the MOCS is non-fatal.The system will not
1517 * run at peak performance. So generate an error and carry on.
1518 */
1519 if (ret)
1520 DRM_ERROR("MOCS failed to program: expect performance issues.\n");
1521
Chris Wilson4e50f082016-10-28 13:58:31 +01001522 return i915_gem_render_state_emit(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001523}
1524
Oscar Mateo73e4d072014-07-24 17:04:48 +01001525/**
1526 * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001527 * @engine: Engine Command Streamer.
Oscar Mateo73e4d072014-07-24 17:04:48 +01001528 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001529void intel_logical_ring_cleanup(struct intel_engine_cs *engine)
Oscar Mateo454afeb2014-07-24 17:04:22 +01001530{
John Harrison6402c332014-10-31 12:00:26 +00001531 struct drm_i915_private *dev_priv;
Oscar Mateo9832b9d2014-07-24 17:04:30 +01001532
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01001533 /*
1534 * Tasklet cannot be active at this point due intel_mark_active/idle
1535 * so this is just for documentation.
1536 */
1537 if (WARN_ON(test_bit(TASKLET_STATE_SCHED, &engine->irq_tasklet.state)))
1538 tasklet_kill(&engine->irq_tasklet);
1539
Chris Wilsonc0336662016-05-06 15:40:21 +01001540 dev_priv = engine->i915;
John Harrison6402c332014-10-31 12:00:26 +00001541
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001542 if (engine->buffer) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001543 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
Dave Gordonb0366a52015-12-08 15:02:36 +00001544 }
Oscar Mateo48d82382014-07-24 17:04:23 +01001545
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001546 if (engine->cleanup)
1547 engine->cleanup(engine);
Oscar Mateo48d82382014-07-24 17:04:23 +01001548
Chris Wilson57e88532016-08-15 10:48:57 +01001549 if (engine->status_page.vma) {
1550 i915_gem_object_unpin_map(engine->status_page.vma->obj);
1551 engine->status_page.vma = NULL;
Oscar Mateo48d82382014-07-24 17:04:23 +01001552 }
Chris Wilsone8a9c582016-12-18 15:37:20 +00001553
1554 intel_engine_cleanup_common(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001555
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001556 lrc_destroy_wa_ctx(engine);
Chris Wilsonc0336662016-05-06 15:40:21 +01001557 engine->i915 = NULL;
Akash Goel3b3f1652016-10-13 22:44:48 +05301558 dev_priv->engine[engine->id] = NULL;
1559 kfree(engine);
Oscar Mateo454afeb2014-07-24 17:04:22 +01001560}
1561
Chris Wilsonff44ad52017-03-16 17:13:03 +00001562static void execlists_set_default_submission(struct intel_engine_cs *engine)
Chris Wilsonddd66c52016-08-02 22:50:31 +01001563{
Chris Wilsonff44ad52017-03-16 17:13:03 +00001564 engine->submit_request = execlists_submit_request;
1565 engine->schedule = execlists_schedule;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001566}
1567
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001568static void
Chris Wilsone1382ef2016-05-06 15:40:20 +01001569logical_ring_default_vfuncs(struct intel_engine_cs *engine)
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001570{
1571 /* Default vfuncs which can be overriden by each engine. */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001572 engine->init_hw = gen8_init_common_ring;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001573 engine->reset_hw = reset_common_ring;
Chris Wilsone8a9c582016-12-18 15:37:20 +00001574
1575 engine->context_pin = execlists_context_pin;
1576 engine->context_unpin = execlists_context_unpin;
1577
Chris Wilsonf73e7392016-12-18 15:37:24 +00001578 engine->request_alloc = execlists_request_alloc;
1579
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001580 engine->emit_flush = gen8_emit_flush;
Chris Wilson9b81d552016-10-28 13:58:50 +01001581 engine->emit_breadcrumb = gen8_emit_breadcrumb;
Chris Wilson98f29e82016-10-28 13:58:51 +01001582 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_sz;
Chris Wilsonff44ad52017-03-16 17:13:03 +00001583
1584 engine->set_default_submission = execlists_set_default_submission;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001585
Chris Wilson31bb59c2016-07-01 17:23:27 +01001586 engine->irq_enable = gen8_logical_ring_enable_irq;
1587 engine->irq_disable = gen8_logical_ring_disable_irq;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001588 engine->emit_bb_start = gen8_emit_bb_start;
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001589}
1590
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001591static inline void
Dave Gordonc2c7f242016-07-13 16:03:35 +01001592logical_ring_default_irqs(struct intel_engine_cs *engine)
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001593{
Dave Gordonc2c7f242016-07-13 16:03:35 +01001594 unsigned shift = engine->irq_shift;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001595 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << shift;
1596 engine->irq_keep_mask = GT_CONTEXT_SWITCH_INTERRUPT << shift;
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001597}
1598
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001599static int
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001600lrc_setup_hws(struct intel_engine_cs *engine, struct i915_vma *vma)
Tvrtko Ursulin04794ad2016-04-12 15:40:41 +01001601{
Chris Wilson57e88532016-08-15 10:48:57 +01001602 const int hws_offset = LRC_PPHWSP_PN * PAGE_SIZE;
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001603 void *hws;
Tvrtko Ursulin04794ad2016-04-12 15:40:41 +01001604
1605 /* The HWSP is part of the default context object in LRC mode. */
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001606 hws = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001607 if (IS_ERR(hws))
1608 return PTR_ERR(hws);
Chris Wilson57e88532016-08-15 10:48:57 +01001609
1610 engine->status_page.page_addr = hws + hws_offset;
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001611 engine->status_page.ggtt_offset = i915_ggtt_offset(vma) + hws_offset;
Chris Wilson57e88532016-08-15 10:48:57 +01001612 engine->status_page.vma = vma;
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001613
1614 return 0;
Tvrtko Ursulin04794ad2016-04-12 15:40:41 +01001615}
1616
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001617static void
1618logical_ring_setup(struct intel_engine_cs *engine)
1619{
1620 struct drm_i915_private *dev_priv = engine->i915;
1621 enum forcewake_domains fw_domains;
1622
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01001623 intel_engine_setup_common(engine);
1624
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001625 /* Intentionally left blank. */
1626 engine->buffer = NULL;
1627
1628 fw_domains = intel_uncore_forcewake_for_reg(dev_priv,
1629 RING_ELSP(engine),
1630 FW_REG_WRITE);
1631
1632 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
1633 RING_CONTEXT_STATUS_PTR(engine),
1634 FW_REG_READ | FW_REG_WRITE);
1635
1636 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
1637 RING_CONTEXT_STATUS_BUF_BASE(engine),
1638 FW_REG_READ);
1639
1640 engine->fw_domains = fw_domains;
1641
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001642 tasklet_init(&engine->irq_tasklet,
1643 intel_lrc_irq_handler, (unsigned long)engine);
1644
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001645 logical_ring_default_vfuncs(engine);
1646 logical_ring_default_irqs(engine);
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001647}
1648
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001649static int
1650logical_ring_init(struct intel_engine_cs *engine)
1651{
1652 struct i915_gem_context *dctx = engine->i915->kernel_context;
1653 int ret;
1654
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01001655 ret = intel_engine_init_common(engine);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001656 if (ret)
1657 goto error;
1658
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001659 /* And setup the hardware status page. */
1660 ret = lrc_setup_hws(engine, dctx->engine[engine->id].state);
1661 if (ret) {
1662 DRM_ERROR("Failed to set up hws %s: %d\n", engine->name, ret);
1663 goto error;
1664 }
1665
1666 return 0;
1667
1668error:
1669 intel_logical_ring_cleanup(engine);
1670 return ret;
1671}
1672
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +01001673int logical_render_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001674{
1675 struct drm_i915_private *dev_priv = engine->i915;
1676 int ret;
1677
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001678 logical_ring_setup(engine);
1679
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001680 if (HAS_L3_DPF(dev_priv))
1681 engine->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
1682
1683 /* Override some for render ring. */
1684 if (INTEL_GEN(dev_priv) >= 9)
1685 engine->init_hw = gen9_init_render_ring;
1686 else
1687 engine->init_hw = gen8_init_render_ring;
1688 engine->init_context = gen8_init_rcs_context;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001689 engine->emit_flush = gen8_emit_flush_render;
Chris Wilson9b81d552016-10-28 13:58:50 +01001690 engine->emit_breadcrumb = gen8_emit_breadcrumb_render;
Chris Wilson98f29e82016-10-28 13:58:51 +01001691 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_render_sz;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001692
Chris Wilsonf51455d2017-01-10 14:47:34 +00001693 ret = intel_engine_create_scratch(engine, PAGE_SIZE);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001694 if (ret)
1695 return ret;
1696
1697 ret = intel_init_workaround_bb(engine);
1698 if (ret) {
1699 /*
1700 * We continue even if we fail to initialize WA batch
1701 * because we only expect rare glitches but nothing
1702 * critical to prevent us from using GPU
1703 */
1704 DRM_ERROR("WA batch buffer initialization failed: %d\n",
1705 ret);
1706 }
1707
Tvrtko Ursulind038fc72016-12-16 13:18:42 +00001708 return logical_ring_init(engine);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001709}
1710
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +01001711int logical_xcs_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001712{
1713 logical_ring_setup(engine);
1714
1715 return logical_ring_init(engine);
1716}
1717
Jeff McGee0cea6502015-02-13 10:27:56 -06001718static u32
Chris Wilsonc0336662016-05-06 15:40:21 +01001719make_rpcs(struct drm_i915_private *dev_priv)
Jeff McGee0cea6502015-02-13 10:27:56 -06001720{
1721 u32 rpcs = 0;
1722
1723 /*
1724 * No explicit RPCS request is needed to ensure full
1725 * slice/subslice/EU enablement prior to Gen9.
1726 */
Chris Wilsonc0336662016-05-06 15:40:21 +01001727 if (INTEL_GEN(dev_priv) < 9)
Jeff McGee0cea6502015-02-13 10:27:56 -06001728 return 0;
1729
1730 /*
1731 * Starting in Gen9, render power gating can leave
1732 * slice/subslice/EU in a partially enabled state. We
1733 * must make an explicit request through RPCS for full
1734 * enablement.
1735 */
Imre Deak43b67992016-08-31 19:13:02 +03001736 if (INTEL_INFO(dev_priv)->sseu.has_slice_pg) {
Jeff McGee0cea6502015-02-13 10:27:56 -06001737 rpcs |= GEN8_RPCS_S_CNT_ENABLE;
Imre Deakf08a0c92016-08-31 19:13:04 +03001738 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.slice_mask) <<
Jeff McGee0cea6502015-02-13 10:27:56 -06001739 GEN8_RPCS_S_CNT_SHIFT;
1740 rpcs |= GEN8_RPCS_ENABLE;
1741 }
1742
Imre Deak43b67992016-08-31 19:13:02 +03001743 if (INTEL_INFO(dev_priv)->sseu.has_subslice_pg) {
Jeff McGee0cea6502015-02-13 10:27:56 -06001744 rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
Imre Deak57ec1712016-08-31 19:13:05 +03001745 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.subslice_mask) <<
Jeff McGee0cea6502015-02-13 10:27:56 -06001746 GEN8_RPCS_SS_CNT_SHIFT;
1747 rpcs |= GEN8_RPCS_ENABLE;
1748 }
1749
Imre Deak43b67992016-08-31 19:13:02 +03001750 if (INTEL_INFO(dev_priv)->sseu.has_eu_pg) {
1751 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
Jeff McGee0cea6502015-02-13 10:27:56 -06001752 GEN8_RPCS_EU_MIN_SHIFT;
Imre Deak43b67992016-08-31 19:13:02 +03001753 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
Jeff McGee0cea6502015-02-13 10:27:56 -06001754 GEN8_RPCS_EU_MAX_SHIFT;
1755 rpcs |= GEN8_RPCS_ENABLE;
1756 }
1757
1758 return rpcs;
1759}
1760
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001761static u32 intel_lr_indirect_ctx_offset(struct intel_engine_cs *engine)
Michel Thierry71562912016-02-23 10:31:49 +00001762{
1763 u32 indirect_ctx_offset;
1764
Chris Wilsonc0336662016-05-06 15:40:21 +01001765 switch (INTEL_GEN(engine->i915)) {
Michel Thierry71562912016-02-23 10:31:49 +00001766 default:
Chris Wilsonc0336662016-05-06 15:40:21 +01001767 MISSING_CASE(INTEL_GEN(engine->i915));
Michel Thierry71562912016-02-23 10:31:49 +00001768 /* fall through */
1769 case 9:
1770 indirect_ctx_offset =
1771 GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
1772 break;
1773 case 8:
1774 indirect_ctx_offset =
1775 GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
1776 break;
1777 }
1778
1779 return indirect_ctx_offset;
1780}
1781
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001782static void execlists_init_reg_state(u32 *regs,
Chris Wilsona3aabe82016-10-04 21:11:26 +01001783 struct i915_gem_context *ctx,
1784 struct intel_engine_cs *engine,
1785 struct intel_ring *ring)
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001786{
Chris Wilsona3aabe82016-10-04 21:11:26 +01001787 struct drm_i915_private *dev_priv = engine->i915;
1788 struct i915_hw_ppgtt *ppgtt = ctx->ppgtt ?: dev_priv->mm.aliasing_ppgtt;
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001789 u32 base = engine->mmio_base;
1790 bool rcs = engine->id == RCS;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001791
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001792 /* A context is actually a big batch buffer with several
1793 * MI_LOAD_REGISTER_IMM commands followed by (reg, value) pairs. The
1794 * values we are setting here are only for the first context restore:
1795 * on a subsequent save, the GPU will recreate this batchbuffer with new
1796 * values (including all the missing MI_LOAD_REGISTER_IMM commands that
1797 * we are not initializing here).
1798 */
1799 regs[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(rcs ? 14 : 11) |
1800 MI_LRI_FORCE_POSTED;
1801
1802 CTX_REG(regs, CTX_CONTEXT_CONTROL, RING_CONTEXT_CONTROL(engine),
1803 _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
1804 CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
1805 (HAS_RESOURCE_STREAMER(dev_priv) ?
1806 CTX_CTRL_RS_CTX_ENABLE : 0)));
1807 CTX_REG(regs, CTX_RING_HEAD, RING_HEAD(base), 0);
1808 CTX_REG(regs, CTX_RING_TAIL, RING_TAIL(base), 0);
1809 CTX_REG(regs, CTX_RING_BUFFER_START, RING_START(base), 0);
1810 CTX_REG(regs, CTX_RING_BUFFER_CONTROL, RING_CTL(base),
1811 RING_CTL_SIZE(ring->size) | RING_VALID);
1812 CTX_REG(regs, CTX_BB_HEAD_U, RING_BBADDR_UDW(base), 0);
1813 CTX_REG(regs, CTX_BB_HEAD_L, RING_BBADDR(base), 0);
1814 CTX_REG(regs, CTX_BB_STATE, RING_BBSTATE(base), RING_BB_PPGTT);
1815 CTX_REG(regs, CTX_SECOND_BB_HEAD_U, RING_SBBADDR_UDW(base), 0);
1816 CTX_REG(regs, CTX_SECOND_BB_HEAD_L, RING_SBBADDR(base), 0);
1817 CTX_REG(regs, CTX_SECOND_BB_STATE, RING_SBBSTATE(base), 0);
1818 if (rcs) {
1819 CTX_REG(regs, CTX_BB_PER_CTX_PTR, RING_BB_PER_CTX_PTR(base), 0);
1820 CTX_REG(regs, CTX_RCS_INDIRECT_CTX, RING_INDIRECT_CTX(base), 0);
1821 CTX_REG(regs, CTX_RCS_INDIRECT_CTX_OFFSET,
1822 RING_INDIRECT_CTX_OFFSET(base), 0);
1823
Chris Wilson48bb74e2016-08-15 10:49:04 +01001824 if (engine->wa_ctx.vma) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001825 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001826 u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001827
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001828 regs[CTX_RCS_INDIRECT_CTX + 1] =
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001829 (ggtt_offset + wa_ctx->indirect_ctx.offset) |
1830 (wa_ctx->indirect_ctx.size / CACHELINE_BYTES);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001831
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001832 regs[CTX_RCS_INDIRECT_CTX_OFFSET + 1] =
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001833 intel_lr_indirect_ctx_offset(engine) << 6;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001834
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001835 regs[CTX_BB_PER_CTX_PTR + 1] =
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001836 (ggtt_offset + wa_ctx->per_ctx.offset) | 0x01;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001837 }
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001838 }
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001839
1840 regs[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9) | MI_LRI_FORCE_POSTED;
1841
1842 CTX_REG(regs, CTX_CTX_TIMESTAMP, RING_CTX_TIMESTAMP(base), 0);
Ville Syrjälä0d925ea2015-11-04 23:20:11 +02001843 /* PDP values well be assigned later if needed */
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001844 CTX_REG(regs, CTX_PDP3_UDW, GEN8_RING_PDP_UDW(engine, 3), 0);
1845 CTX_REG(regs, CTX_PDP3_LDW, GEN8_RING_PDP_LDW(engine, 3), 0);
1846 CTX_REG(regs, CTX_PDP2_UDW, GEN8_RING_PDP_UDW(engine, 2), 0);
1847 CTX_REG(regs, CTX_PDP2_LDW, GEN8_RING_PDP_LDW(engine, 2), 0);
1848 CTX_REG(regs, CTX_PDP1_UDW, GEN8_RING_PDP_UDW(engine, 1), 0);
1849 CTX_REG(regs, CTX_PDP1_LDW, GEN8_RING_PDP_LDW(engine, 1), 0);
1850 CTX_REG(regs, CTX_PDP0_UDW, GEN8_RING_PDP_UDW(engine, 0), 0);
1851 CTX_REG(regs, CTX_PDP0_LDW, GEN8_RING_PDP_LDW(engine, 0), 0);
Michel Thierryd7b26332015-04-08 12:13:34 +01001852
Chris Wilson949e8ab2017-02-09 14:40:36 +00001853 if (ppgtt && i915_vm_is_48bit(&ppgtt->base)) {
Michel Thierry2dba3232015-07-30 11:06:23 +01001854 /* 64b PPGTT (48bit canonical)
1855 * PDP0_DESCRIPTOR contains the base address to PML4 and
1856 * other PDP Descriptors are ignored.
1857 */
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001858 ASSIGN_CTX_PML4(ppgtt, regs);
Michel Thierry2dba3232015-07-30 11:06:23 +01001859 }
1860
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001861 if (rcs) {
1862 regs[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
1863 CTX_REG(regs, CTX_R_PWR_CLK_STATE, GEN8_R_PWR_CLK_STATE,
1864 make_rpcs(dev_priv));
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001865 }
Chris Wilsona3aabe82016-10-04 21:11:26 +01001866}
1867
1868static int
1869populate_lr_context(struct i915_gem_context *ctx,
1870 struct drm_i915_gem_object *ctx_obj,
1871 struct intel_engine_cs *engine,
1872 struct intel_ring *ring)
1873{
1874 void *vaddr;
1875 int ret;
1876
1877 ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
1878 if (ret) {
1879 DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
1880 return ret;
1881 }
1882
1883 vaddr = i915_gem_object_pin_map(ctx_obj, I915_MAP_WB);
1884 if (IS_ERR(vaddr)) {
1885 ret = PTR_ERR(vaddr);
1886 DRM_DEBUG_DRIVER("Could not map object pages! (%d)\n", ret);
1887 return ret;
1888 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01001889 ctx_obj->mm.dirty = true;
Chris Wilsona3aabe82016-10-04 21:11:26 +01001890
1891 /* The second page of the context object contains some fields which must
1892 * be set up prior to the first execution. */
1893
1894 execlists_init_reg_state(vaddr + LRC_STATE_PN * PAGE_SIZE,
1895 ctx, engine, ring);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001896
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001897 i915_gem_object_unpin_map(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001898
1899 return 0;
1900}
1901
Oscar Mateo73e4d072014-07-24 17:04:48 +01001902/**
Dave Gordonc5d46ee2016-01-05 12:21:33 +00001903 * intel_lr_context_size() - return the size of the context for an engine
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001904 * @engine: which engine to find the context size for
Dave Gordonc5d46ee2016-01-05 12:21:33 +00001905 *
1906 * Each engine may require a different amount of space for a context image,
1907 * so when allocating (or copying) an image, this function can be used to
1908 * find the right size for the specific engine.
1909 *
1910 * Return: size (in bytes) of an engine-specific context image
1911 *
1912 * Note: this size includes the HWSP, which is part of the context image
1913 * in LRC mode, but does not include the "shared data page" used with
1914 * GuC submission. The caller should account for this if using the GuC.
1915 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001916uint32_t intel_lr_context_size(struct intel_engine_cs *engine)
Oscar Mateo8c8579172014-07-24 17:04:14 +01001917{
1918 int ret = 0;
1919
Chris Wilsonc0336662016-05-06 15:40:21 +01001920 WARN_ON(INTEL_GEN(engine->i915) < 8);
Oscar Mateo8c8579172014-07-24 17:04:14 +01001921
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001922 switch (engine->id) {
Oscar Mateo8c8579172014-07-24 17:04:14 +01001923 case RCS:
Chris Wilsonc0336662016-05-06 15:40:21 +01001924 if (INTEL_GEN(engine->i915) >= 9)
Michael H. Nguyen468c6812014-11-13 17:51:49 +00001925 ret = GEN9_LR_CONTEXT_RENDER_SIZE;
1926 else
1927 ret = GEN8_LR_CONTEXT_RENDER_SIZE;
Oscar Mateo8c8579172014-07-24 17:04:14 +01001928 break;
1929 case VCS:
1930 case BCS:
1931 case VECS:
1932 case VCS2:
1933 ret = GEN8_LR_CONTEXT_OTHER_SIZE;
1934 break;
1935 }
1936
1937 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01001938}
1939
Chris Wilsone2efd132016-05-24 14:53:34 +01001940static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
Chris Wilson978f1e02016-04-28 09:56:54 +01001941 struct intel_engine_cs *engine)
Oscar Mateoede7d422014-07-24 17:04:12 +01001942{
Oscar Mateo8c8579172014-07-24 17:04:14 +01001943 struct drm_i915_gem_object *ctx_obj;
Chris Wilson9021ad02016-05-24 14:53:37 +01001944 struct intel_context *ce = &ctx->engine[engine->id];
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001945 struct i915_vma *vma;
Oscar Mateo8c8579172014-07-24 17:04:14 +01001946 uint32_t context_size;
Chris Wilson7e37f882016-08-02 22:50:21 +01001947 struct intel_ring *ring;
Oscar Mateo8c8579172014-07-24 17:04:14 +01001948 int ret;
1949
Chris Wilson9021ad02016-05-24 14:53:37 +01001950 WARN_ON(ce->state);
Oscar Mateoede7d422014-07-24 17:04:12 +01001951
Chris Wilsonf51455d2017-01-10 14:47:34 +00001952 context_size = round_up(intel_lr_context_size(engine),
1953 I915_GTT_PAGE_SIZE);
Oscar Mateo8c8579172014-07-24 17:04:14 +01001954
Alex Daid1675192015-08-12 15:43:43 +01001955 /* One extra page as the sharing data between driver and GuC */
1956 context_size += PAGE_SIZE * LRC_PPHWSP_PN;
1957
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00001958 ctx_obj = i915_gem_object_create(ctx->i915, context_size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01001959 if (IS_ERR(ctx_obj)) {
Dan Carpenter3126a662015-04-30 17:30:50 +03001960 DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
Chris Wilsonfe3db792016-04-25 13:32:13 +01001961 return PTR_ERR(ctx_obj);
Oscar Mateo8c8579172014-07-24 17:04:14 +01001962 }
1963
Chris Wilsona01cb372017-01-16 15:21:30 +00001964 vma = i915_vma_instance(ctx_obj, &ctx->i915->ggtt.base, NULL);
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001965 if (IS_ERR(vma)) {
1966 ret = PTR_ERR(vma);
1967 goto error_deref_obj;
1968 }
1969
Chris Wilson7e37f882016-08-02 22:50:21 +01001970 ring = intel_engine_create_ring(engine, ctx->ring_size);
Chris Wilsondca33ec2016-08-02 22:50:20 +01001971 if (IS_ERR(ring)) {
1972 ret = PTR_ERR(ring);
Nick Hoathe84fe802015-09-11 12:53:46 +01001973 goto error_deref_obj;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001974 }
1975
Chris Wilsondca33ec2016-08-02 22:50:20 +01001976 ret = populate_lr_context(ctx, ctx_obj, engine, ring);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001977 if (ret) {
1978 DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
Chris Wilsondca33ec2016-08-02 22:50:20 +01001979 goto error_ring_free;
Oscar Mateo84c23772014-07-24 17:04:15 +01001980 }
1981
Chris Wilsondca33ec2016-08-02 22:50:20 +01001982 ce->ring = ring;
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001983 ce->state = vma;
Chris Wilson9021ad02016-05-24 14:53:37 +01001984 ce->initialised = engine->init_context == NULL;
Oscar Mateoede7d422014-07-24 17:04:12 +01001985
1986 return 0;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001987
Chris Wilsondca33ec2016-08-02 22:50:20 +01001988error_ring_free:
Chris Wilson7e37f882016-08-02 22:50:21 +01001989 intel_ring_free(ring);
Nick Hoathe84fe802015-09-11 12:53:46 +01001990error_deref_obj:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01001991 i915_gem_object_put(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001992 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01001993}
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00001994
Chris Wilson821ed7d2016-09-09 14:11:53 +01001995void intel_lr_context_resume(struct drm_i915_private *dev_priv)
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00001996{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001997 struct intel_engine_cs *engine;
Chris Wilsonbafb2f72016-09-21 14:51:08 +01001998 struct i915_gem_context *ctx;
Akash Goel3b3f1652016-10-13 22:44:48 +05301999 enum intel_engine_id id;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002000
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002001 /* Because we emit WA_TAIL_DWORDS there may be a disparity
2002 * between our bookkeeping in ce->ring->head and ce->ring->tail and
2003 * that stored in context. As we only write new commands from
2004 * ce->ring->tail onwards, everything before that is junk. If the GPU
2005 * starts reading from its RING_HEAD from the context, it may try to
2006 * execute that junk and die.
2007 *
2008 * So to avoid that we reset the context images upon resume. For
2009 * simplicity, we just zero everything out.
2010 */
2011 list_for_each_entry(ctx, &dev_priv->context_list, link) {
Akash Goel3b3f1652016-10-13 22:44:48 +05302012 for_each_engine(engine, dev_priv, id) {
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002013 struct intel_context *ce = &ctx->engine[engine->id];
2014 u32 *reg;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002015
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002016 if (!ce->state)
2017 continue;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002018
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002019 reg = i915_gem_object_pin_map(ce->state->obj,
2020 I915_MAP_WB);
2021 if (WARN_ON(IS_ERR(reg)))
2022 continue;
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01002023
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002024 reg += LRC_STATE_PN * PAGE_SIZE / sizeof(*reg);
2025 reg[CTX_RING_HEAD+1] = 0;
2026 reg[CTX_RING_TAIL+1] = 0;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002027
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002028 ce->state->obj->mm.dirty = true;
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002029 i915_gem_object_unpin_map(ce->state->obj);
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002030
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002031 ce->ring->head = ce->ring->tail = 0;
2032 ce->ring->last_retired_head = -1;
2033 intel_ring_update_space(ce->ring);
2034 }
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002035 }
2036}