blob: f91e126a7f9764d24bde889a48209b00bd5fc30c [file] [log] [blame]
Oscar Mateob20385f2014-07-24 17:04:10 +01001/*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Ben Widawsky <ben@bwidawsk.net>
25 * Michel Thierry <michel.thierry@intel.com>
26 * Thomas Daniel <thomas.daniel@intel.com>
27 * Oscar Mateo <oscar.mateo@intel.com>
28 *
29 */
30
Oscar Mateo73e4d072014-07-24 17:04:48 +010031/**
32 * DOC: Logical Rings, Logical Ring Contexts and Execlists
33 *
34 * Motivation:
Oscar Mateob20385f2014-07-24 17:04:10 +010035 * GEN8 brings an expansion of the HW contexts: "Logical Ring Contexts".
36 * These expanded contexts enable a number of new abilities, especially
37 * "Execlists" (also implemented in this file).
38 *
Oscar Mateo73e4d072014-07-24 17:04:48 +010039 * One of the main differences with the legacy HW contexts is that logical
40 * ring contexts incorporate many more things to the context's state, like
41 * PDPs or ringbuffer control registers:
42 *
43 * The reason why PDPs are included in the context is straightforward: as
44 * PPGTTs (per-process GTTs) are actually per-context, having the PDPs
45 * contained there mean you don't need to do a ppgtt->switch_mm yourself,
46 * instead, the GPU will do it for you on the context switch.
47 *
48 * But, what about the ringbuffer control registers (head, tail, etc..)?
49 * shouldn't we just need a set of those per engine command streamer? This is
50 * where the name "Logical Rings" starts to make sense: by virtualizing the
51 * rings, the engine cs shifts to a new "ring buffer" with every context
52 * switch. When you want to submit a workload to the GPU you: A) choose your
53 * context, B) find its appropriate virtualized ring, C) write commands to it
54 * and then, finally, D) tell the GPU to switch to that context.
55 *
56 * Instead of the legacy MI_SET_CONTEXT, the way you tell the GPU to switch
57 * to a contexts is via a context execution list, ergo "Execlists".
58 *
59 * LRC implementation:
60 * Regarding the creation of contexts, we have:
61 *
62 * - One global default context.
63 * - One local default context for each opened fd.
64 * - One local extra context for each context create ioctl call.
65 *
66 * Now that ringbuffers belong per-context (and not per-engine, like before)
67 * and that contexts are uniquely tied to a given engine (and not reusable,
68 * like before) we need:
69 *
70 * - One ringbuffer per-engine inside each context.
71 * - One backing object per-engine inside each context.
72 *
73 * The global default context starts its life with these new objects fully
74 * allocated and populated. The local default context for each opened fd is
75 * more complex, because we don't know at creation time which engine is going
76 * to use them. To handle this, we have implemented a deferred creation of LR
77 * contexts:
78 *
79 * The local context starts its life as a hollow or blank holder, that only
80 * gets populated for a given engine once we receive an execbuffer. If later
81 * on we receive another execbuffer ioctl for the same context but a different
82 * engine, we allocate/populate a new ringbuffer and context backing object and
83 * so on.
84 *
85 * Finally, regarding local contexts created using the ioctl call: as they are
86 * only allowed with the render ring, we can allocate & populate them right
87 * away (no need to defer anything, at least for now).
88 *
89 * Execlists implementation:
Oscar Mateob20385f2014-07-24 17:04:10 +010090 * Execlists are the new method by which, on gen8+ hardware, workloads are
91 * submitted for execution (as opposed to the legacy, ringbuffer-based, method).
Oscar Mateo73e4d072014-07-24 17:04:48 +010092 * This method works as follows:
93 *
94 * When a request is committed, its commands (the BB start and any leading or
95 * trailing commands, like the seqno breadcrumbs) are placed in the ringbuffer
96 * for the appropriate context. The tail pointer in the hardware context is not
97 * updated at this time, but instead, kept by the driver in the ringbuffer
98 * structure. A structure representing this request is added to a request queue
99 * for the appropriate engine: this structure contains a copy of the context's
100 * tail after the request was written to the ring buffer and a pointer to the
101 * context itself.
102 *
103 * If the engine's request queue was empty before the request was added, the
104 * queue is processed immediately. Otherwise the queue will be processed during
105 * a context switch interrupt. In any case, elements on the queue will get sent
106 * (in pairs) to the GPU's ExecLists Submit Port (ELSP, for short) with a
107 * globally unique 20-bits submission ID.
108 *
109 * When execution of a request completes, the GPU updates the context status
110 * buffer with a context complete event and generates a context switch interrupt.
111 * During the interrupt handling, the driver examines the events in the buffer:
112 * for each context complete event, if the announced ID matches that on the head
113 * of the request queue, then that request is retired and removed from the queue.
114 *
115 * After processing, if any requests were retired and the queue is not empty
116 * then a new execution list can be submitted. The two requests at the front of
117 * the queue are next to be submitted but since a context may not occur twice in
118 * an execution list, if subsequent requests have the same ID as the first then
119 * the two requests must be combined. This is done simply by discarding requests
120 * at the head of the queue until either only one requests is left (in which case
121 * we use a NULL second context) or the first two requests have unique IDs.
122 *
123 * By always executing the first two requests in the queue the driver ensures
124 * that the GPU is kept as busy as possible. In the case where a single context
125 * completes but a second context is still executing, the request for this second
126 * context will be at the head of the queue when we remove the first one. This
127 * request will then be resubmitted along with a new request for a different context,
128 * which will cause the hardware to continue executing the second request and queue
129 * the new request (the GPU detects the condition of a context getting preempted
130 * with the same context and optimizes the context switch flow by not doing
131 * preemption, but just sampling the new tail pointer).
132 *
Oscar Mateob20385f2014-07-24 17:04:10 +0100133 */
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +0100134#include <linux/interrupt.h>
Oscar Mateob20385f2014-07-24 17:04:10 +0100135
136#include <drm/drmP.h>
137#include <drm/i915_drm.h>
138#include "i915_drv.h"
Peter Antoine3bbaba02015-07-10 20:13:11 +0300139#include "intel_mocs.h"
Oscar Mateo127f1002014-07-24 17:04:11 +0100140
Thomas Daniele981e7b2014-07-24 17:04:39 +0100141#define RING_EXECLIST_QFULL (1 << 0x2)
142#define RING_EXECLIST1_VALID (1 << 0x3)
143#define RING_EXECLIST0_VALID (1 << 0x4)
144#define RING_EXECLIST_ACTIVE_STATUS (3 << 0xE)
145#define RING_EXECLIST1_ACTIVE (1 << 0x11)
146#define RING_EXECLIST0_ACTIVE (1 << 0x12)
147
148#define GEN8_CTX_STATUS_IDLE_ACTIVE (1 << 0)
149#define GEN8_CTX_STATUS_PREEMPTED (1 << 1)
150#define GEN8_CTX_STATUS_ELEMENT_SWITCH (1 << 2)
151#define GEN8_CTX_STATUS_ACTIVE_IDLE (1 << 3)
152#define GEN8_CTX_STATUS_COMPLETE (1 << 4)
153#define GEN8_CTX_STATUS_LITE_RESTORE (1 << 15)
Oscar Mateo8670d6f2014-07-24 17:04:17 +0100154
Chris Wilson70c2a242016-09-09 14:11:46 +0100155#define GEN8_CTX_STATUS_COMPLETED_MASK \
156 (GEN8_CTX_STATUS_ACTIVE_IDLE | \
157 GEN8_CTX_STATUS_PREEMPTED | \
158 GEN8_CTX_STATUS_ELEMENT_SWITCH)
159
Oscar Mateo8670d6f2014-07-24 17:04:17 +0100160#define CTX_LRI_HEADER_0 0x01
161#define CTX_CONTEXT_CONTROL 0x02
162#define CTX_RING_HEAD 0x04
163#define CTX_RING_TAIL 0x06
164#define CTX_RING_BUFFER_START 0x08
165#define CTX_RING_BUFFER_CONTROL 0x0a
166#define CTX_BB_HEAD_U 0x0c
167#define CTX_BB_HEAD_L 0x0e
168#define CTX_BB_STATE 0x10
169#define CTX_SECOND_BB_HEAD_U 0x12
170#define CTX_SECOND_BB_HEAD_L 0x14
171#define CTX_SECOND_BB_STATE 0x16
172#define CTX_BB_PER_CTX_PTR 0x18
173#define CTX_RCS_INDIRECT_CTX 0x1a
174#define CTX_RCS_INDIRECT_CTX_OFFSET 0x1c
175#define CTX_LRI_HEADER_1 0x21
176#define CTX_CTX_TIMESTAMP 0x22
177#define CTX_PDP3_UDW 0x24
178#define CTX_PDP3_LDW 0x26
179#define CTX_PDP2_UDW 0x28
180#define CTX_PDP2_LDW 0x2a
181#define CTX_PDP1_UDW 0x2c
182#define CTX_PDP1_LDW 0x2e
183#define CTX_PDP0_UDW 0x30
184#define CTX_PDP0_LDW 0x32
185#define CTX_LRI_HEADER_2 0x41
186#define CTX_R_PWR_CLK_STATE 0x42
187#define CTX_GPGPU_CSR_BASE_ADDRESS 0x44
188
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +0000189#define CTX_REG(reg_state, pos, reg, val) do { \
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200190 (reg_state)[(pos)+0] = i915_mmio_reg_offset(reg); \
Ville Syrjälä0d925ea2015-11-04 23:20:11 +0200191 (reg_state)[(pos)+1] = (val); \
192} while (0)
193
194#define ASSIGN_CTX_PDP(ppgtt, reg_state, n) do { \
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300195 const u64 _addr = i915_page_dir_dma_addr((ppgtt), (n)); \
Michel Thierrye5815a22015-04-08 12:13:32 +0100196 reg_state[CTX_PDP ## n ## _UDW+1] = upper_32_bits(_addr); \
197 reg_state[CTX_PDP ## n ## _LDW+1] = lower_32_bits(_addr); \
Ville Syrjälä9244a812015-11-04 23:20:09 +0200198} while (0)
Michel Thierrye5815a22015-04-08 12:13:32 +0100199
Ville Syrjälä9244a812015-11-04 23:20:09 +0200200#define ASSIGN_CTX_PML4(ppgtt, reg_state) do { \
Michel Thierry2dba3232015-07-30 11:06:23 +0100201 reg_state[CTX_PDP0_UDW + 1] = upper_32_bits(px_dma(&ppgtt->pml4)); \
202 reg_state[CTX_PDP0_LDW + 1] = lower_32_bits(px_dma(&ppgtt->pml4)); \
Ville Syrjälä9244a812015-11-04 23:20:09 +0200203} while (0)
Michel Thierry2dba3232015-07-30 11:06:23 +0100204
Michel Thierry71562912016-02-23 10:31:49 +0000205#define GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x17
206#define GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x26
Michel Thierry7bd0a2c2017-06-06 13:30:38 -0700207#define GEN10_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x19
Ben Widawsky84b790f2014-07-24 17:04:36 +0100208
Chris Wilson0e93cdd2016-04-29 09:07:06 +0100209/* Typical size of the average request (2 pipecontrols and a MI_BB) */
210#define EXECLISTS_REQUEST_SIZE 64 /* bytes */
211
Chris Wilsona3aabe82016-10-04 21:11:26 +0100212#define WA_TAIL_DWORDS 2
213
Chris Wilsone2efd132016-05-24 14:53:34 +0100214static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
Chris Wilson978f1e02016-04-28 09:56:54 +0100215 struct intel_engine_cs *engine);
Chris Wilsona3aabe82016-10-04 21:11:26 +0100216static void execlists_init_reg_state(u32 *reg_state,
217 struct i915_gem_context *ctx,
218 struct intel_engine_cs *engine,
219 struct intel_ring *ring);
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000220
Oscar Mateo73e4d072014-07-24 17:04:48 +0100221/**
222 * intel_sanitize_enable_execlists() - sanitize i915.enable_execlists
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100223 * @dev_priv: i915 device private
Oscar Mateo73e4d072014-07-24 17:04:48 +0100224 * @enable_execlists: value of i915.enable_execlists module parameter.
225 *
226 * Only certain platforms support Execlists (the prerequisites being
Thomas Daniel27401d12014-12-11 12:48:35 +0000227 * support for Logical Ring Contexts and Aliasing PPGTT or better).
Oscar Mateo73e4d072014-07-24 17:04:48 +0100228 *
229 * Return: 1 if Execlists is supported and has to be enabled.
230 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100231int intel_sanitize_enable_execlists(struct drm_i915_private *dev_priv, int enable_execlists)
Oscar Mateo127f1002014-07-24 17:04:11 +0100232{
Zhiyuan Lva0bd6c32015-08-28 15:41:16 +0800233 /* On platforms with execlist available, vGPU will only
234 * support execlist mode, no ring buffer mode.
235 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100236 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv) && intel_vgpu_active(dev_priv))
Zhiyuan Lva0bd6c32015-08-28 15:41:16 +0800237 return 1;
238
Chris Wilsonc0336662016-05-06 15:40:21 +0100239 if (INTEL_GEN(dev_priv) >= 9)
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000240 return 1;
241
Oscar Mateo127f1002014-07-24 17:04:11 +0100242 if (enable_execlists == 0)
243 return 0;
244
Daniel Vetter5a21b662016-05-24 17:13:53 +0200245 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv) &&
246 USES_PPGTT(dev_priv) &&
Michal Wajdeczko4f044a82017-09-19 19:38:44 +0000247 i915_modparams.use_mmio_flip >= 0)
Oscar Mateo127f1002014-07-24 17:04:11 +0100248 return 1;
249
250 return 0;
251}
Oscar Mateoede7d422014-07-24 17:04:12 +0100252
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000253/**
254 * intel_lr_context_descriptor_update() - calculate & cache the descriptor
255 * descriptor for a pinned context
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000256 * @ctx: Context to work on
Chris Wilson9021ad02016-05-24 14:53:37 +0100257 * @engine: Engine the descriptor will be used with
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000258 *
259 * The context descriptor encodes various attributes of a context,
260 * including its GTT address and some flags. Because it's fairly
261 * expensive to calculate, we'll just do it once and cache the result,
262 * which remains valid until the context is unpinned.
263 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200264 * This is what a descriptor looks like, from LSB to MSB::
265 *
Mika Kuoppala2355cf02017-01-27 15:03:09 +0200266 * bits 0-11: flags, GEN8_CTX_* (cached in ctx->desc_template)
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200267 * bits 12-31: LRCA, GTT address of (the HWSP of) this context
268 * bits 32-52: ctx ID, a globally unique tag
269 * bits 53-54: mbz, reserved for use by hardware
270 * bits 55-63: group ID, currently unused and set to 0
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000271 */
272static void
Chris Wilsone2efd132016-05-24 14:53:34 +0100273intel_lr_context_descriptor_update(struct i915_gem_context *ctx,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000274 struct intel_engine_cs *engine)
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000275{
Chris Wilson9021ad02016-05-24 14:53:37 +0100276 struct intel_context *ce = &ctx->engine[engine->id];
Chris Wilson7069b142016-04-28 09:56:52 +0100277 u64 desc;
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000278
Chris Wilson7069b142016-04-28 09:56:52 +0100279 BUILD_BUG_ON(MAX_CONTEXT_HW_ID > (1<<GEN8_CTX_ID_WIDTH));
280
Mika Kuoppala2355cf02017-01-27 15:03:09 +0200281 desc = ctx->desc_template; /* bits 0-11 */
Michel Thierry0b29c752017-09-13 09:56:00 +0100282 desc |= i915_ggtt_offset(ce->state) + LRC_HEADER_PAGES * PAGE_SIZE;
Chris Wilson9021ad02016-05-24 14:53:37 +0100283 /* bits 12-31 */
Chris Wilson7069b142016-04-28 09:56:52 +0100284 desc |= (u64)ctx->hw_id << GEN8_CTX_ID_SHIFT; /* bits 32-52 */
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000285
Chris Wilson9021ad02016-05-24 14:53:37 +0100286 ce->lrc_desc = desc;
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000287}
288
Chris Wilson27606fd2017-09-16 21:44:13 +0100289static struct i915_priolist *
290lookup_priolist(struct intel_engine_cs *engine,
291 struct i915_priotree *pt,
292 int prio)
Chris Wilson08dd3e12017-09-16 21:44:12 +0100293{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300294 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100295 struct i915_priolist *p;
296 struct rb_node **parent, *rb;
297 bool first = true;
298
Mika Kuoppalab620e872017-09-22 15:43:03 +0300299 if (unlikely(execlists->no_priolist))
Chris Wilson08dd3e12017-09-16 21:44:12 +0100300 prio = I915_PRIORITY_NORMAL;
301
302find_priolist:
303 /* most positive priority is scheduled first, equal priorities fifo */
304 rb = NULL;
Mika Kuoppalab620e872017-09-22 15:43:03 +0300305 parent = &execlists->queue.rb_node;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100306 while (*parent) {
307 rb = *parent;
308 p = rb_entry(rb, typeof(*p), node);
309 if (prio > p->priority) {
310 parent = &rb->rb_left;
311 } else if (prio < p->priority) {
312 parent = &rb->rb_right;
313 first = false;
314 } else {
Chris Wilson27606fd2017-09-16 21:44:13 +0100315 return p;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100316 }
317 }
318
319 if (prio == I915_PRIORITY_NORMAL) {
Mika Kuoppalab620e872017-09-22 15:43:03 +0300320 p = &execlists->default_priolist;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100321 } else {
322 p = kmem_cache_alloc(engine->i915->priorities, GFP_ATOMIC);
323 /* Convert an allocation failure to a priority bump */
324 if (unlikely(!p)) {
325 prio = I915_PRIORITY_NORMAL; /* recurses just once */
326
327 /* To maintain ordering with all rendering, after an
328 * allocation failure we have to disable all scheduling.
329 * Requests will then be executed in fifo, and schedule
330 * will ensure that dependencies are emitted in fifo.
331 * There will be still some reordering with existing
332 * requests, so if userspace lied about their
333 * dependencies that reordering may be visible.
334 */
Mika Kuoppalab620e872017-09-22 15:43:03 +0300335 execlists->no_priolist = true;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100336 goto find_priolist;
337 }
338 }
339
340 p->priority = prio;
Chris Wilson27606fd2017-09-16 21:44:13 +0100341 INIT_LIST_HEAD(&p->requests);
Chris Wilson08dd3e12017-09-16 21:44:12 +0100342 rb_link_node(&p->node, rb, parent);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300343 rb_insert_color(&p->node, &execlists->queue);
Chris Wilson08dd3e12017-09-16 21:44:12 +0100344
Chris Wilson08dd3e12017-09-16 21:44:12 +0100345 if (first)
Mika Kuoppalab620e872017-09-22 15:43:03 +0300346 execlists->first = &p->node;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100347
Chris Wilson27606fd2017-09-16 21:44:13 +0100348 return ptr_pack_bits(p, first, 1);
Chris Wilson08dd3e12017-09-16 21:44:12 +0100349}
350
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100351static inline void
352execlists_context_status_change(struct drm_i915_gem_request *rq,
353 unsigned long status)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100354{
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100355 /*
356 * Only used when GVT-g is enabled now. When GVT-g is disabled,
357 * The compiler should eliminate this function as dead-code.
358 */
359 if (!IS_ENABLED(CONFIG_DRM_I915_GVT))
360 return;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100361
Changbin Du3fc03062017-03-13 10:47:11 +0800362 atomic_notifier_call_chain(&rq->engine->context_status_notifier,
363 status, rq);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100364}
365
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000366static void
367execlists_update_context_pdps(struct i915_hw_ppgtt *ppgtt, u32 *reg_state)
368{
369 ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
370 ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
371 ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
372 ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
373}
374
Chris Wilson70c2a242016-09-09 14:11:46 +0100375static u64 execlists_update_context(struct drm_i915_gem_request *rq)
Oscar Mateoae1250b2014-07-24 17:04:37 +0100376{
Chris Wilson70c2a242016-09-09 14:11:46 +0100377 struct intel_context *ce = &rq->ctx->engine[rq->engine->id];
Zhi Wang04da8112017-02-06 18:37:16 +0800378 struct i915_hw_ppgtt *ppgtt =
379 rq->ctx->ppgtt ?: rq->i915->mm.aliasing_ppgtt;
Chris Wilson70c2a242016-09-09 14:11:46 +0100380 u32 *reg_state = ce->lrc_reg_state;
Oscar Mateoae1250b2014-07-24 17:04:37 +0100381
Chris Wilsone6ba9992017-04-25 14:00:49 +0100382 reg_state[CTX_RING_TAIL+1] = intel_ring_set_tail(rq->ring, rq->tail);
Oscar Mateoae1250b2014-07-24 17:04:37 +0100383
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000384 /* True 32b PPGTT with dynamic page allocation: update PDP
385 * registers and point the unallocated PDPs to scratch page.
386 * PML4 is allocated during ppgtt init, so this is not needed
387 * in 48-bit mode.
388 */
Chris Wilson949e8ab2017-02-09 14:40:36 +0000389 if (ppgtt && !i915_vm_is_48bit(&ppgtt->base))
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000390 execlists_update_context_pdps(ppgtt, reg_state);
Chris Wilson70c2a242016-09-09 14:11:46 +0100391
392 return ce->lrc_desc;
Oscar Mateoae1250b2014-07-24 17:04:37 +0100393}
394
Chris Wilson70c2a242016-09-09 14:11:46 +0100395static void execlists_submit_ports(struct intel_engine_cs *engine)
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100396{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300397 struct execlist_port *port = engine->execlists.port;
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100398 u32 __iomem *elsp =
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100399 engine->i915->regs + i915_mmio_reg_offset(RING_ELSP(engine));
400 unsigned int n;
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100401
Mika Kuoppala76e70082017-09-22 15:43:07 +0300402 for (n = execlists_num_ports(&engine->execlists); n--; ) {
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100403 struct drm_i915_gem_request *rq;
404 unsigned int count;
405 u64 desc;
Chris Wilson70c2a242016-09-09 14:11:46 +0100406
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100407 rq = port_unpack(&port[n], &count);
408 if (rq) {
409 GEM_BUG_ON(count > !n);
410 if (!count++)
411 execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_IN);
412 port_set(&port[n], port_pack(rq, count));
413 desc = execlists_update_context(rq);
414 GEM_DEBUG_EXEC(port[n].context_id = upper_32_bits(desc));
415 } else {
416 GEM_BUG_ON(!n);
417 desc = 0;
418 }
419
420 writel(upper_32_bits(desc), elsp);
421 writel(lower_32_bits(desc), elsp);
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100422 }
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100423}
424
Chris Wilson70c2a242016-09-09 14:11:46 +0100425static bool ctx_single_port_submission(const struct i915_gem_context *ctx)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100426{
Chris Wilson70c2a242016-09-09 14:11:46 +0100427 return (IS_ENABLED(CONFIG_DRM_I915_GVT) &&
Chris Wilson60958682016-12-31 11:20:11 +0000428 i915_gem_context_force_single_submission(ctx));
Ben Widawsky84b790f2014-07-24 17:04:36 +0100429}
430
Chris Wilson70c2a242016-09-09 14:11:46 +0100431static bool can_merge_ctx(const struct i915_gem_context *prev,
432 const struct i915_gem_context *next)
Michel Thierryacdd8842014-07-24 17:04:38 +0100433{
Chris Wilson70c2a242016-09-09 14:11:46 +0100434 if (prev != next)
435 return false;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100436
Chris Wilson70c2a242016-09-09 14:11:46 +0100437 if (ctx_single_port_submission(prev))
438 return false;
Michel Thierryacdd8842014-07-24 17:04:38 +0100439
Chris Wilson70c2a242016-09-09 14:11:46 +0100440 return true;
441}
Peter Antoine779949f2015-05-11 16:03:27 +0100442
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100443static void port_assign(struct execlist_port *port,
444 struct drm_i915_gem_request *rq)
445{
446 GEM_BUG_ON(rq == port_request(port));
447
448 if (port_isset(port))
449 i915_gem_request_put(port_request(port));
450
451 port_set(port, port_pack(i915_gem_request_get(rq), port_count(port)));
452}
453
Chris Wilson70c2a242016-09-09 14:11:46 +0100454static void execlists_dequeue(struct intel_engine_cs *engine)
455{
Chris Wilson20311bd2016-11-14 20:41:03 +0000456 struct drm_i915_gem_request *last;
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300457 struct intel_engine_execlists * const execlists = &engine->execlists;
458 struct execlist_port *port = execlists->port;
Mika Kuoppala76e70082017-09-22 15:43:07 +0300459 const struct execlist_port * const last_port =
460 &execlists->port[execlists->port_mask];
Chris Wilson20311bd2016-11-14 20:41:03 +0000461 struct rb_node *rb;
Chris Wilson70c2a242016-09-09 14:11:46 +0100462 bool submit = false;
Michel Thierryacdd8842014-07-24 17:04:38 +0100463
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100464 last = port_request(port);
Chris Wilson70c2a242016-09-09 14:11:46 +0100465 if (last)
466 /* WaIdleLiteRestore:bdw,skl
467 * Apply the wa NOOPs to prevent ring:HEAD == req:TAIL
Chris Wilson9b81d552016-10-28 13:58:50 +0100468 * as we resubmit the request. See gen8_emit_breadcrumb()
Chris Wilson70c2a242016-09-09 14:11:46 +0100469 * for where we prepare the padding after the end of the
470 * request.
Michel Thierry53292cd2015-04-15 18:11:33 +0100471 */
Chris Wilson70c2a242016-09-09 14:11:46 +0100472 last->tail = last->wa_tail;
473
Chris Wilson70c2a242016-09-09 14:11:46 +0100474 /* Hardware submission is through 2 ports. Conceptually each port
475 * has a (RING_START, RING_HEAD, RING_TAIL) tuple. RING_START is
476 * static for a context, and unique to each, so we only execute
477 * requests belonging to a single context from each ring. RING_HEAD
478 * is maintained by the CS in the context image, it marks the place
479 * where it got up to last time, and through RING_TAIL we tell the CS
480 * where we want to execute up to this time.
481 *
482 * In this list the requests are in order of execution. Consecutive
483 * requests from the same context are adjacent in the ringbuffer. We
484 * can combine these requests into a single RING_TAIL update:
485 *
486 * RING_HEAD...req1...req2
487 * ^- RING_TAIL
488 * since to execute req2 the CS must first execute req1.
489 *
490 * Our goal then is to point each port to the end of a consecutive
491 * sequence of requests as being the most optimal (fewest wake ups
492 * and context switches) submission.
493 */
494
Tvrtko Ursulin9f7886d2017-03-21 10:55:11 +0000495 spin_lock_irq(&engine->timeline->lock);
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300496 rb = execlists->first;
497 GEM_BUG_ON(rb_first(&execlists->queue) != rb);
Chris Wilson20311bd2016-11-14 20:41:03 +0000498 while (rb) {
Chris Wilson6c067572017-05-17 13:10:03 +0100499 struct i915_priolist *p = rb_entry(rb, typeof(*p), node);
500 struct drm_i915_gem_request *rq, *rn;
Chris Wilson20311bd2016-11-14 20:41:03 +0000501
Chris Wilson6c067572017-05-17 13:10:03 +0100502 list_for_each_entry_safe(rq, rn, &p->requests, priotree.link) {
503 /*
504 * Can we combine this request with the current port?
505 * It has to be the same context/ringbuffer and not
506 * have any exceptions (e.g. GVT saying never to
507 * combine contexts).
508 *
509 * If we can combine the requests, we can execute both
510 * by updating the RING_TAIL to point to the end of the
511 * second request, and so we never need to tell the
512 * hardware about the first.
Chris Wilson70c2a242016-09-09 14:11:46 +0100513 */
Chris Wilson6c067572017-05-17 13:10:03 +0100514 if (last && !can_merge_ctx(rq->ctx, last->ctx)) {
515 /*
516 * If we are on the second port and cannot
517 * combine this request with the last, then we
518 * are done.
519 */
Mika Kuoppala76e70082017-09-22 15:43:07 +0300520 if (port == last_port) {
Chris Wilson6c067572017-05-17 13:10:03 +0100521 __list_del_many(&p->requests,
522 &rq->priotree.link);
523 goto done;
524 }
Chris Wilson70c2a242016-09-09 14:11:46 +0100525
Chris Wilson6c067572017-05-17 13:10:03 +0100526 /*
527 * If GVT overrides us we only ever submit
528 * port[0], leaving port[1] empty. Note that we
529 * also have to be careful that we don't queue
530 * the same context (even though a different
531 * request) to the second port.
532 */
533 if (ctx_single_port_submission(last->ctx) ||
534 ctx_single_port_submission(rq->ctx)) {
535 __list_del_many(&p->requests,
536 &rq->priotree.link);
537 goto done;
538 }
Chris Wilson70c2a242016-09-09 14:11:46 +0100539
Chris Wilson6c067572017-05-17 13:10:03 +0100540 GEM_BUG_ON(last->ctx == rq->ctx);
Chris Wilson70c2a242016-09-09 14:11:46 +0100541
Chris Wilson6c067572017-05-17 13:10:03 +0100542 if (submit)
543 port_assign(port, last);
544 port++;
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300545
546 GEM_BUG_ON(port_isset(port));
Chris Wilson6c067572017-05-17 13:10:03 +0100547 }
548
549 INIT_LIST_HEAD(&rq->priotree.link);
550 rq->priotree.priority = INT_MAX;
551
552 __i915_gem_request_submit(rq);
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300553 trace_i915_gem_request_in(rq, port_index(port, execlists));
Chris Wilson6c067572017-05-17 13:10:03 +0100554 last = rq;
555 submit = true;
Chris Wilson70c2a242016-09-09 14:11:46 +0100556 }
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000557
Chris Wilson20311bd2016-11-14 20:41:03 +0000558 rb = rb_next(rb);
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300559 rb_erase(&p->node, &execlists->queue);
Chris Wilson6c067572017-05-17 13:10:03 +0100560 INIT_LIST_HEAD(&p->requests);
561 if (p->priority != I915_PRIORITY_NORMAL)
Chris Wilsonc5cf9a92017-05-17 13:10:04 +0100562 kmem_cache_free(engine->i915->priorities, p);
Michel Thierry53292cd2015-04-15 18:11:33 +0100563 }
Chris Wilson6c067572017-05-17 13:10:03 +0100564done:
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300565 execlists->first = rb;
Chris Wilson6c067572017-05-17 13:10:03 +0100566 if (submit)
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100567 port_assign(port, last);
Tvrtko Ursulin9f7886d2017-03-21 10:55:11 +0000568 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson70c2a242016-09-09 14:11:46 +0100569
570 if (submit)
571 execlists_submit_ports(engine);
Michel Thierryacdd8842014-07-24 17:04:38 +0100572}
573
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300574static void execlist_cancel_port_requests(struct intel_engine_execlists *execlists)
575{
576 unsigned int i;
577
578 for (i = 0; i < ARRAY_SIZE(execlists->port); i++)
579 i915_gem_request_put(port_request(&execlists->port[i]));
580
581 memset(execlists->port, 0, sizeof(execlists->port));
582}
583
Chris Wilson27a5f612017-09-15 18:31:00 +0100584static void execlists_cancel_requests(struct intel_engine_cs *engine)
585{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300586 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilson27a5f612017-09-15 18:31:00 +0100587 struct drm_i915_gem_request *rq, *rn;
588 struct rb_node *rb;
589 unsigned long flags;
Chris Wilson27a5f612017-09-15 18:31:00 +0100590
591 spin_lock_irqsave(&engine->timeline->lock, flags);
592
593 /* Cancel the requests on the HW and clear the ELSP tracker. */
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300594 execlist_cancel_port_requests(execlists);
Chris Wilson27a5f612017-09-15 18:31:00 +0100595
596 /* Mark all executing requests as skipped. */
597 list_for_each_entry(rq, &engine->timeline->requests, link) {
598 GEM_BUG_ON(!rq->global_seqno);
599 if (!i915_gem_request_completed(rq))
600 dma_fence_set_error(&rq->fence, -EIO);
601 }
602
603 /* Flush the queued requests to the timeline list (for retiring). */
Mika Kuoppalab620e872017-09-22 15:43:03 +0300604 rb = execlists->first;
Chris Wilson27a5f612017-09-15 18:31:00 +0100605 while (rb) {
606 struct i915_priolist *p = rb_entry(rb, typeof(*p), node);
607
608 list_for_each_entry_safe(rq, rn, &p->requests, priotree.link) {
609 INIT_LIST_HEAD(&rq->priotree.link);
610 rq->priotree.priority = INT_MAX;
611
612 dma_fence_set_error(&rq->fence, -EIO);
613 __i915_gem_request_submit(rq);
614 }
615
616 rb = rb_next(rb);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300617 rb_erase(&p->node, &execlists->queue);
Chris Wilson27a5f612017-09-15 18:31:00 +0100618 INIT_LIST_HEAD(&p->requests);
619 if (p->priority != I915_PRIORITY_NORMAL)
620 kmem_cache_free(engine->i915->priorities, p);
621 }
622
623 /* Remaining _unready_ requests will be nop'ed when submitted */
624
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300625
Mika Kuoppalab620e872017-09-22 15:43:03 +0300626 execlists->queue = RB_ROOT;
627 execlists->first = NULL;
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300628 GEM_BUG_ON(port_isset(&execlists->port[0]));
Chris Wilson27a5f612017-09-15 18:31:00 +0100629
630 /*
631 * The port is checked prior to scheduling a tasklet, but
632 * just in case we have suspended the tasklet to do the
633 * wedging make sure that when it wakes, it decides there
634 * is no work to do by clearing the irq_posted bit.
635 */
636 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
637
638 spin_unlock_irqrestore(&engine->timeline->lock, flags);
639}
640
Chris Wilson816ee792017-01-24 11:00:03 +0000641static bool execlists_elsp_ready(const struct intel_engine_cs *engine)
Ben Widawsky91a41032016-01-05 10:30:07 -0800642{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300643 const struct execlist_port *port = engine->execlists.port;
Ben Widawsky91a41032016-01-05 10:30:07 -0800644
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100645 return port_count(&port[0]) + port_count(&port[1]) < 2;
Ben Widawsky91a41032016-01-05 10:30:07 -0800646}
647
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200648/*
Oscar Mateo73e4d072014-07-24 17:04:48 +0100649 * Check the unread Context Status Buffers and manage the submission of new
650 * contexts to the ELSP accordingly.
651 */
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +0100652static void intel_lrc_irq_handler(unsigned long data)
Thomas Daniele981e7b2014-07-24 17:04:39 +0100653{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300654 struct intel_engine_cs * const engine = (struct intel_engine_cs *)data;
655 struct intel_engine_execlists * const execlists = &engine->execlists;
656 struct execlist_port *port = execlists->port;
Chris Wilsonc0336662016-05-06 15:40:21 +0100657 struct drm_i915_private *dev_priv = engine->i915;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100658
Chris Wilson48921262017-04-11 18:58:50 +0100659 /* We can skip acquiring intel_runtime_pm_get() here as it was taken
660 * on our behalf by the request (see i915_gem_mark_busy()) and it will
661 * not be relinquished until the device is idle (see
662 * i915_gem_idle_work_handler()). As a precaution, we make sure
663 * that all ELSP are drained i.e. we have processed the CSB,
664 * before allowing ourselves to idle and calling intel_runtime_pm_put().
665 */
666 GEM_BUG_ON(!dev_priv->gt.awake);
667
Mika Kuoppalab620e872017-09-22 15:43:03 +0300668 intel_uncore_forcewake_get(dev_priv, execlists->fw_domains);
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000669
Chris Wilson899f6202017-03-21 11:33:20 +0000670 /* Prefer doing test_and_clear_bit() as a two stage operation to avoid
671 * imposing the cost of a locked atomic transaction when submitting a
672 * new request (outside of the context-switch interrupt).
673 */
674 while (test_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted)) {
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100675 /* The HWSP contains a (cacheable) mirror of the CSB */
676 const u32 *buf =
677 &engine->status_page.page_addr[I915_HWS_CSB_BUF0_INDEX];
Chris Wilson4af0d722017-03-25 20:10:53 +0000678 unsigned int head, tail;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100679
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100680 /* However GVT emulation depends upon intercepting CSB mmio */
Mika Kuoppalab620e872017-09-22 15:43:03 +0300681 if (unlikely(execlists->csb_use_mmio)) {
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100682 buf = (u32 * __force)
683 (dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_BUF_LO(engine, 0)));
Mika Kuoppalab620e872017-09-22 15:43:03 +0300684 execlists->csb_head = -1; /* force mmio read of CSB ptrs */
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100685 }
686
Chris Wilson2e70b8c2017-03-23 13:48:03 +0000687 /* The write will be ordered by the uncached read (itself
688 * a memory barrier), so we do not need another in the form
689 * of a locked instruction. The race between the interrupt
690 * handler and the split test/clear is harmless as we order
691 * our clear before the CSB read. If the interrupt arrived
692 * first between the test and the clear, we read the updated
693 * CSB and clear the bit. If the interrupt arrives as we read
694 * the CSB or later (i.e. after we had cleared the bit) the bit
695 * is set and we do a new loop.
696 */
697 __clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300698 if (unlikely(execlists->csb_head == -1)) { /* following a reset */
Chris Wilson767a9832017-09-13 09:56:05 +0100699 head = readl(dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)));
700 tail = GEN8_CSB_WRITE_PTR(head);
701 head = GEN8_CSB_READ_PTR(head);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300702 execlists->csb_head = head;
Chris Wilson767a9832017-09-13 09:56:05 +0100703 } else {
704 const int write_idx =
705 intel_hws_csb_write_index(dev_priv) -
706 I915_HWS_CSB_BUF0_INDEX;
707
Mika Kuoppalab620e872017-09-22 15:43:03 +0300708 head = execlists->csb_head;
Chris Wilson767a9832017-09-13 09:56:05 +0100709 tail = READ_ONCE(buf[write_idx]);
710 }
Mika Kuoppalab620e872017-09-22 15:43:03 +0300711
Chris Wilson4af0d722017-03-25 20:10:53 +0000712 while (head != tail) {
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100713 struct drm_i915_gem_request *rq;
Chris Wilson4af0d722017-03-25 20:10:53 +0000714 unsigned int status;
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100715 unsigned int count;
Chris Wilsona37951a2017-01-24 11:00:06 +0000716
Chris Wilson4af0d722017-03-25 20:10:53 +0000717 if (++head == GEN8_CSB_ENTRIES)
718 head = 0;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100719
Chris Wilson2ffe80a2017-02-06 17:05:02 +0000720 /* We are flying near dragons again.
721 *
722 * We hold a reference to the request in execlist_port[]
723 * but no more than that. We are operating in softirq
724 * context and so cannot hold any mutex or sleep. That
725 * prevents us stopping the requests we are processing
726 * in port[] from being retired simultaneously (the
727 * breadcrumb will be complete before we see the
728 * context-switch). As we only hold the reference to the
729 * request, any pointer chasing underneath the request
730 * is subject to a potential use-after-free. Thus we
731 * store all of the bookkeeping within port[] as
732 * required, and avoid using unguarded pointers beneath
733 * request itself. The same applies to the atomic
734 * status notifier.
735 */
736
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100737 status = READ_ONCE(buf[2 * head]); /* maybe mmio! */
Chris Wilson70c2a242016-09-09 14:11:46 +0100738 if (!(status & GEN8_CTX_STATUS_COMPLETED_MASK))
739 continue;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100740
Chris Wilson86aa7e72017-01-23 11:31:32 +0000741 /* Check the context/desc id for this event matches */
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100742 GEM_DEBUG_BUG_ON(buf[2 * head + 1] != port->context_id);
Chris Wilson86aa7e72017-01-23 11:31:32 +0000743
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100744 rq = port_unpack(port, &count);
745 GEM_BUG_ON(count == 0);
746 if (--count == 0) {
Chris Wilson70c2a242016-09-09 14:11:46 +0100747 GEM_BUG_ON(status & GEN8_CTX_STATUS_PREEMPTED);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100748 GEM_BUG_ON(!i915_gem_request_completed(rq));
749 execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_OUT);
Thomas Daniele981e7b2014-07-24 17:04:39 +0100750
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100751 trace_i915_gem_request_out(rq);
752 i915_gem_request_put(rq);
753
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300754 execlists_port_complete(execlists, port);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100755 } else {
756 port_set(port, port_pack(rq, count));
Chris Wilson70c2a242016-09-09 14:11:46 +0100757 }
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000758
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100759 /* After the final element, the hw should be idle */
760 GEM_BUG_ON(port_count(port) == 0 &&
Chris Wilson70c2a242016-09-09 14:11:46 +0100761 !(status & GEN8_CTX_STATUS_ACTIVE_IDLE));
Chris Wilson4af0d722017-03-25 20:10:53 +0000762 }
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000763
Mika Kuoppalab620e872017-09-22 15:43:03 +0300764 if (head != execlists->csb_head) {
765 execlists->csb_head = head;
Chris Wilson767a9832017-09-13 09:56:05 +0100766 writel(_MASKED_FIELD(GEN8_CSB_READ_PTR_MASK, head << 8),
767 dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)));
768 }
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000769 }
770
Chris Wilson70c2a242016-09-09 14:11:46 +0100771 if (execlists_elsp_ready(engine))
772 execlists_dequeue(engine);
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000773
Mika Kuoppalab620e872017-09-22 15:43:03 +0300774 intel_uncore_forcewake_put(dev_priv, execlists->fw_domains);
Thomas Daniele981e7b2014-07-24 17:04:39 +0100775}
776
Chris Wilson27606fd2017-09-16 21:44:13 +0100777static void insert_request(struct intel_engine_cs *engine,
778 struct i915_priotree *pt,
779 int prio)
780{
781 struct i915_priolist *p = lookup_priolist(engine, pt, prio);
782
783 list_add_tail(&pt->link, &ptr_mask_bits(p, 1)->requests);
784 if (ptr_unmask_bits(p, 1) && execlists_elsp_ready(engine))
Mika Kuoppalab620e872017-09-22 15:43:03 +0300785 tasklet_hi_schedule(&engine->execlists.irq_tasklet);
Chris Wilson27606fd2017-09-16 21:44:13 +0100786}
787
Chris Wilsonf4ea6bd2016-08-02 22:50:32 +0100788static void execlists_submit_request(struct drm_i915_gem_request *request)
Michel Thierryacdd8842014-07-24 17:04:38 +0100789{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000790 struct intel_engine_cs *engine = request->engine;
Chris Wilson5590af32016-09-09 14:11:54 +0100791 unsigned long flags;
Michel Thierryacdd8842014-07-24 17:04:38 +0100792
Chris Wilson663f71e2016-11-14 20:41:00 +0000793 /* Will be called from irq-context when using foreign fences. */
794 spin_lock_irqsave(&engine->timeline->lock, flags);
Michel Thierryacdd8842014-07-24 17:04:38 +0100795
Chris Wilson27606fd2017-09-16 21:44:13 +0100796 insert_request(engine, &request->priotree, request->priotree.priority);
Michel Thierryacdd8842014-07-24 17:04:38 +0100797
Mika Kuoppalab620e872017-09-22 15:43:03 +0300798 GEM_BUG_ON(!engine->execlists.first);
Chris Wilson6c067572017-05-17 13:10:03 +0100799 GEM_BUG_ON(list_empty(&request->priotree.link));
800
Chris Wilson663f71e2016-11-14 20:41:00 +0000801 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Michel Thierryacdd8842014-07-24 17:04:38 +0100802}
803
Chris Wilson20311bd2016-11-14 20:41:03 +0000804static struct intel_engine_cs *
805pt_lock_engine(struct i915_priotree *pt, struct intel_engine_cs *locked)
806{
Chris Wilsona79a5242017-03-27 21:21:43 +0100807 struct intel_engine_cs *engine =
808 container_of(pt, struct drm_i915_gem_request, priotree)->engine;
Chris Wilson20311bd2016-11-14 20:41:03 +0000809
Chris Wilsona79a5242017-03-27 21:21:43 +0100810 GEM_BUG_ON(!locked);
811
Chris Wilson20311bd2016-11-14 20:41:03 +0000812 if (engine != locked) {
Chris Wilsona79a5242017-03-27 21:21:43 +0100813 spin_unlock(&locked->timeline->lock);
814 spin_lock(&engine->timeline->lock);
Chris Wilson20311bd2016-11-14 20:41:03 +0000815 }
816
817 return engine;
818}
819
820static void execlists_schedule(struct drm_i915_gem_request *request, int prio)
821{
Chris Wilsona79a5242017-03-27 21:21:43 +0100822 struct intel_engine_cs *engine;
Chris Wilson20311bd2016-11-14 20:41:03 +0000823 struct i915_dependency *dep, *p;
824 struct i915_dependency stack;
825 LIST_HEAD(dfs);
826
827 if (prio <= READ_ONCE(request->priotree.priority))
828 return;
829
Chris Wilson70cd1472016-11-28 14:36:49 +0000830 /* Need BKL in order to use the temporary link inside i915_dependency */
831 lockdep_assert_held(&request->i915->drm.struct_mutex);
Chris Wilson20311bd2016-11-14 20:41:03 +0000832
833 stack.signaler = &request->priotree;
834 list_add(&stack.dfs_link, &dfs);
835
836 /* Recursively bump all dependent priorities to match the new request.
837 *
838 * A naive approach would be to use recursion:
839 * static void update_priorities(struct i915_priotree *pt, prio) {
840 * list_for_each_entry(dep, &pt->signalers_list, signal_link)
841 * update_priorities(dep->signal, prio)
842 * insert_request(pt);
843 * }
844 * but that may have unlimited recursion depth and so runs a very
845 * real risk of overunning the kernel stack. Instead, we build
846 * a flat list of all dependencies starting with the current request.
847 * As we walk the list of dependencies, we add all of its dependencies
848 * to the end of the list (this may include an already visited
849 * request) and continue to walk onwards onto the new dependencies. The
850 * end result is a topological list of requests in reverse order, the
851 * last element in the list is the request we must execute first.
852 */
853 list_for_each_entry_safe(dep, p, &dfs, dfs_link) {
854 struct i915_priotree *pt = dep->signaler;
855
Chris Wilsona79a5242017-03-27 21:21:43 +0100856 /* Within an engine, there can be no cycle, but we may
857 * refer to the same dependency chain multiple times
858 * (redundant dependencies are not eliminated) and across
859 * engines.
860 */
861 list_for_each_entry(p, &pt->signalers_list, signal_link) {
862 GEM_BUG_ON(p->signaler->priority < pt->priority);
Chris Wilson20311bd2016-11-14 20:41:03 +0000863 if (prio > READ_ONCE(p->signaler->priority))
864 list_move_tail(&p->dfs_link, &dfs);
Chris Wilsona79a5242017-03-27 21:21:43 +0100865 }
Chris Wilson20311bd2016-11-14 20:41:03 +0000866
Chris Wilson0798cff2016-12-05 14:29:41 +0000867 list_safe_reset_next(dep, p, dfs_link);
Chris Wilson20311bd2016-11-14 20:41:03 +0000868 }
869
Chris Wilson349bdb62017-05-17 13:10:05 +0100870 /* If we didn't need to bump any existing priorities, and we haven't
871 * yet submitted this request (i.e. there is no potential race with
872 * execlists_submit_request()), we can set our own priority and skip
873 * acquiring the engine locks.
874 */
875 if (request->priotree.priority == INT_MIN) {
876 GEM_BUG_ON(!list_empty(&request->priotree.link));
877 request->priotree.priority = prio;
878 if (stack.dfs_link.next == stack.dfs_link.prev)
879 return;
880 __list_del_entry(&stack.dfs_link);
881 }
882
Chris Wilsona79a5242017-03-27 21:21:43 +0100883 engine = request->engine;
884 spin_lock_irq(&engine->timeline->lock);
885
Chris Wilson20311bd2016-11-14 20:41:03 +0000886 /* Fifo and depth-first replacement ensure our deps execute before us */
887 list_for_each_entry_safe_reverse(dep, p, &dfs, dfs_link) {
888 struct i915_priotree *pt = dep->signaler;
889
890 INIT_LIST_HEAD(&dep->dfs_link);
891
892 engine = pt_lock_engine(pt, engine);
893
894 if (prio <= pt->priority)
895 continue;
896
Chris Wilson20311bd2016-11-14 20:41:03 +0000897 pt->priority = prio;
Chris Wilson6c067572017-05-17 13:10:03 +0100898 if (!list_empty(&pt->link)) {
899 __list_del_entry(&pt->link);
900 insert_request(engine, pt, prio);
Chris Wilsona79a5242017-03-27 21:21:43 +0100901 }
Chris Wilson20311bd2016-11-14 20:41:03 +0000902 }
903
Chris Wilsona79a5242017-03-27 21:21:43 +0100904 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson20311bd2016-11-14 20:41:03 +0000905
906 /* XXX Do we need to preempt to make room for us and our deps? */
907}
908
Chris Wilson266a2402017-05-04 10:33:08 +0100909static struct intel_ring *
910execlists_context_pin(struct intel_engine_cs *engine,
911 struct i915_gem_context *ctx)
Oscar Mateodcb4c122014-11-13 10:28:10 +0000912{
Chris Wilson9021ad02016-05-24 14:53:37 +0100913 struct intel_context *ce = &ctx->engine[engine->id];
Chris Wilson2947e402016-12-18 15:37:23 +0000914 unsigned int flags;
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +0100915 void *vaddr;
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000916 int ret;
Oscar Mateodcb4c122014-11-13 10:28:10 +0000917
Chris Wilson91c8a322016-07-05 10:40:23 +0100918 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000919
Chris Wilson266a2402017-05-04 10:33:08 +0100920 if (likely(ce->pin_count++))
921 goto out;
Chris Wilsona533b4b2017-03-16 17:16:28 +0000922 GEM_BUG_ON(!ce->pin_count); /* no overflow please! */
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100923
Chris Wilsone8a9c582016-12-18 15:37:20 +0000924 if (!ce->state) {
925 ret = execlists_context_deferred_alloc(ctx, engine);
926 if (ret)
927 goto err;
928 }
Chris Wilson56f6e0a2017-01-05 15:30:20 +0000929 GEM_BUG_ON(!ce->state);
Chris Wilsone8a9c582016-12-18 15:37:20 +0000930
Chris Wilson72b72ae2017-02-10 10:14:22 +0000931 flags = PIN_GLOBAL | PIN_HIGH;
Daniele Ceraolo Spuriofeef2a72016-12-23 15:56:22 -0800932 if (ctx->ggtt_offset_bias)
933 flags |= PIN_OFFSET_BIAS | ctx->ggtt_offset_bias;
Chris Wilson2947e402016-12-18 15:37:23 +0000934
935 ret = i915_vma_pin(ce->state, 0, GEN8_LR_CONTEXT_ALIGN, flags);
Nick Hoathe84fe802015-09-11 12:53:46 +0100936 if (ret)
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100937 goto err;
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000938
Chris Wilsonbf3783e2016-08-15 10:48:54 +0100939 vaddr = i915_gem_object_pin_map(ce->state->obj, I915_MAP_WB);
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +0100940 if (IS_ERR(vaddr)) {
941 ret = PTR_ERR(vaddr);
Chris Wilsonbf3783e2016-08-15 10:48:54 +0100942 goto unpin_vma;
Tvrtko Ursulin82352e92016-01-15 17:12:45 +0000943 }
944
Chris Wilsond822bb12017-04-03 12:34:25 +0100945 ret = intel_ring_pin(ce->ring, ctx->i915, ctx->ggtt_offset_bias);
Nick Hoathe84fe802015-09-11 12:53:46 +0100946 if (ret)
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +0100947 goto unpin_map;
Alex Daid1675192015-08-12 15:43:43 +0100948
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000949 intel_lr_context_descriptor_update(ctx, engine);
Chris Wilson9021ad02016-05-24 14:53:37 +0100950
Chris Wilsona3aabe82016-10-04 21:11:26 +0100951 ce->lrc_reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
952 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100953 i915_ggtt_offset(ce->ring->vma);
Chris Wilsona3aabe82016-10-04 21:11:26 +0100954
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100955 ce->state->obj->mm.dirty = true;
Daniel Vettere93c28f2015-09-02 14:33:42 +0200956
Chris Wilson9a6feaf2016-07-20 13:31:50 +0100957 i915_gem_context_get(ctx);
Chris Wilson266a2402017-05-04 10:33:08 +0100958out:
959 return ce->ring;
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000960
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +0100961unpin_map:
Chris Wilsonbf3783e2016-08-15 10:48:54 +0100962 i915_gem_object_unpin_map(ce->state->obj);
963unpin_vma:
964 __i915_vma_unpin(ce->state);
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100965err:
Chris Wilson9021ad02016-05-24 14:53:37 +0100966 ce->pin_count = 0;
Chris Wilson266a2402017-05-04 10:33:08 +0100967 return ERR_PTR(ret);
Oscar Mateodcb4c122014-11-13 10:28:10 +0000968}
969
Chris Wilsone8a9c582016-12-18 15:37:20 +0000970static void execlists_context_unpin(struct intel_engine_cs *engine,
971 struct i915_gem_context *ctx)
Oscar Mateodcb4c122014-11-13 10:28:10 +0000972{
Chris Wilson9021ad02016-05-24 14:53:37 +0100973 struct intel_context *ce = &ctx->engine[engine->id];
Daniel Vetteraf3302b2015-12-04 17:27:15 +0100974
Chris Wilson91c8a322016-07-05 10:40:23 +0100975 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilson9021ad02016-05-24 14:53:37 +0100976 GEM_BUG_ON(ce->pin_count == 0);
Tvrtko Ursulin321fe302016-01-28 10:29:55 +0000977
Chris Wilson9021ad02016-05-24 14:53:37 +0100978 if (--ce->pin_count)
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100979 return;
980
Chris Wilsonaad29fb2016-08-02 22:50:23 +0100981 intel_ring_unpin(ce->ring);
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100982
Chris Wilsonbf3783e2016-08-15 10:48:54 +0100983 i915_gem_object_unpin_map(ce->state->obj);
984 i915_vma_unpin(ce->state);
Chris Wilson24f1d3c2016-04-28 09:56:53 +0100985
Chris Wilson9a6feaf2016-07-20 13:31:50 +0100986 i915_gem_context_put(ctx);
Oscar Mateodcb4c122014-11-13 10:28:10 +0000987}
988
Chris Wilsonf73e7392016-12-18 15:37:24 +0000989static int execlists_request_alloc(struct drm_i915_gem_request *request)
Chris Wilsonef11c012016-12-18 15:37:19 +0000990{
991 struct intel_engine_cs *engine = request->engine;
992 struct intel_context *ce = &request->ctx->engine[engine->id];
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000993 u32 *cs;
Chris Wilsonef11c012016-12-18 15:37:19 +0000994 int ret;
995
Chris Wilsone8a9c582016-12-18 15:37:20 +0000996 GEM_BUG_ON(!ce->pin_count);
997
Chris Wilsonef11c012016-12-18 15:37:19 +0000998 /* Flush enough space to reduce the likelihood of waiting after
999 * we start building the request - in which case we will just
1000 * have to repeat work.
1001 */
1002 request->reserved_space += EXECLISTS_REQUEST_SIZE;
1003
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001004 cs = intel_ring_begin(request, 0);
Michał Winiarski85e2fe62017-09-14 10:32:13 +02001005 if (IS_ERR(cs))
1006 return PTR_ERR(cs);
Chris Wilsonef11c012016-12-18 15:37:19 +00001007
1008 if (!ce->initialised) {
1009 ret = engine->init_context(request);
1010 if (ret)
Michał Winiarski85e2fe62017-09-14 10:32:13 +02001011 return ret;
Chris Wilsonef11c012016-12-18 15:37:19 +00001012
1013 ce->initialised = true;
1014 }
1015
1016 /* Note that after this point, we have committed to using
1017 * this request as it is being used to both track the
1018 * state of engine initialisation and liveness of the
1019 * golden renderstate above. Think twice before you try
1020 * to cancel/unwind this request now.
1021 */
1022
1023 request->reserved_space -= EXECLISTS_REQUEST_SIZE;
1024 return 0;
Chris Wilsonef11c012016-12-18 15:37:19 +00001025}
1026
Arun Siluvery9e000842015-07-03 14:27:31 +01001027/*
1028 * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
1029 * PIPE_CONTROL instruction. This is required for the flush to happen correctly
1030 * but there is a slight complication as this is applied in WA batch where the
1031 * values are only initialized once so we cannot take register value at the
1032 * beginning and reuse it further; hence we save its value to memory, upload a
1033 * constant value with bit21 set and then we restore it back with the saved value.
1034 * To simplify the WA, a constant value is formed by using the default value
1035 * of this register. This shouldn't be a problem because we are only modifying
1036 * it for a short period and this batch in non-premptible. We can ofcourse
1037 * use additional instructions that read the actual value of the register
1038 * at that time and set our bit of interest but it makes the WA complicated.
1039 *
1040 * This WA is also required for Gen9 so extracting as a function avoids
1041 * code duplication.
1042 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001043static u32 *
1044gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery9e000842015-07-03 14:27:31 +01001045{
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001046 *batch++ = MI_STORE_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1047 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1048 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
1049 *batch++ = 0;
Arun Siluvery9e000842015-07-03 14:27:31 +01001050
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001051 *batch++ = MI_LOAD_REGISTER_IMM(1);
1052 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1053 *batch++ = 0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES;
Arun Siluvery9e000842015-07-03 14:27:31 +01001054
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001055 batch = gen8_emit_pipe_control(batch,
1056 PIPE_CONTROL_CS_STALL |
1057 PIPE_CONTROL_DC_FLUSH_ENABLE,
1058 0);
Arun Siluvery9e000842015-07-03 14:27:31 +01001059
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001060 *batch++ = MI_LOAD_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1061 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1062 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
1063 *batch++ = 0;
Arun Siluvery9e000842015-07-03 14:27:31 +01001064
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001065 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001066}
1067
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001068/*
1069 * Typically we only have one indirect_ctx and per_ctx batch buffer which are
1070 * initialized at the beginning and shared across all contexts but this field
1071 * helps us to have multiple batches at different offsets and select them based
1072 * on a criteria. At the moment this batch always start at the beginning of the page
1073 * and at this point we don't have multiple wa_ctx batch buffers.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001074 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001075 * The number of WA applied are not known at the beginning; we use this field
1076 * to return the no of DWORDS written.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001077 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001078 * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
1079 * so it adds NOOPs as padding to make it cacheline aligned.
1080 * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
1081 * makes a complete batch buffer.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001082 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001083static u32 *gen8_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001084{
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001085 /* WaDisableCtxRestoreArbitration:bdw,chv */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001086 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001087
Arun Siluveryc82435b2015-06-19 18:37:13 +01001088 /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001089 if (IS_BROADWELL(engine->i915))
1090 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
Arun Siluveryc82435b2015-06-19 18:37:13 +01001091
Arun Siluvery0160f052015-06-23 15:46:57 +01001092 /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
1093 /* Actual scratch location is at 128 bytes offset */
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001094 batch = gen8_emit_pipe_control(batch,
1095 PIPE_CONTROL_FLUSH_L3 |
1096 PIPE_CONTROL_GLOBAL_GTT_IVB |
1097 PIPE_CONTROL_CS_STALL |
1098 PIPE_CONTROL_QW_WRITE,
1099 i915_ggtt_offset(engine->scratch) +
1100 2 * CACHELINE_BYTES);
Arun Siluvery0160f052015-06-23 15:46:57 +01001101
Arun Siluvery17ee9502015-06-19 19:07:01 +01001102 /* Pad to end of cacheline */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001103 while ((unsigned long)batch % CACHELINE_BYTES)
1104 *batch++ = MI_NOOP;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001105
1106 /*
1107 * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
1108 * execution depends on the length specified in terms of cache lines
1109 * in the register CTX_RCS_INDIRECT_CTX
1110 */
1111
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001112 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001113}
1114
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001115/*
1116 * This batch is started immediately after indirect_ctx batch. Since we ensure
1117 * that indirect_ctx ends on a cacheline this batch is aligned automatically.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001118 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001119 * The number of DWORDS written are returned using this field.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001120 *
1121 * This batch is terminated with MI_BATCH_BUFFER_END and so we need not add padding
1122 * to align it with cacheline as padding after MI_BATCH_BUFFER_END is redundant.
1123 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001124static u32 *gen8_init_perctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001125{
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001126 /* WaDisableCtxRestoreArbitration:bdw,chv */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001127 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1128 *batch++ = MI_BATCH_BUFFER_END;
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001129
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001130 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001131}
1132
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001133static u32 *gen9_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery0504cff2015-07-14 15:01:27 +01001134{
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001135 /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt,glk */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001136 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
Arun Siluverya4106a72015-07-14 15:01:29 +01001137
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001138 /* WaDisableGatherAtSetShaderCommonSlice:skl,bxt,kbl,glk */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001139 *batch++ = MI_LOAD_REGISTER_IMM(1);
1140 *batch++ = i915_mmio_reg_offset(COMMON_SLICE_CHICKEN2);
1141 *batch++ = _MASKED_BIT_DISABLE(
1142 GEN9_DISABLE_GATHER_AT_SET_SHADER_COMMON_SLICE);
1143 *batch++ = MI_NOOP;
Mika Kuoppala873e8172016-07-20 14:26:13 +03001144
Mika Kuoppala066d4622016-06-07 17:19:15 +03001145 /* WaClearSlmSpaceAtContextSwitch:kbl */
1146 /* Actual scratch location is at 128 bytes offset */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001147 if (IS_KBL_REVID(engine->i915, 0, KBL_REVID_A0)) {
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001148 batch = gen8_emit_pipe_control(batch,
1149 PIPE_CONTROL_FLUSH_L3 |
1150 PIPE_CONTROL_GLOBAL_GTT_IVB |
1151 PIPE_CONTROL_CS_STALL |
1152 PIPE_CONTROL_QW_WRITE,
1153 i915_ggtt_offset(engine->scratch)
1154 + 2 * CACHELINE_BYTES);
Mika Kuoppala066d4622016-06-07 17:19:15 +03001155 }
Tim Gore3485d992016-07-05 10:01:30 +01001156
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001157 /* WaMediaPoolStateCmdInWABB:bxt,glk */
Tim Gore3485d992016-07-05 10:01:30 +01001158 if (HAS_POOLED_EU(engine->i915)) {
1159 /*
1160 * EU pool configuration is setup along with golden context
1161 * during context initialization. This value depends on
1162 * device type (2x6 or 3x6) and needs to be updated based
1163 * on which subslice is disabled especially for 2x6
1164 * devices, however it is safe to load default
1165 * configuration of 3x6 device instead of masking off
1166 * corresponding bits because HW ignores bits of a disabled
1167 * subslice and drops down to appropriate config. Please
1168 * see render_state_setup() in i915_gem_render_state.c for
1169 * possible configurations, to avoid duplication they are
1170 * not shown here again.
1171 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001172 *batch++ = GEN9_MEDIA_POOL_STATE;
1173 *batch++ = GEN9_MEDIA_POOL_ENABLE;
1174 *batch++ = 0x00777000;
1175 *batch++ = 0;
1176 *batch++ = 0;
1177 *batch++ = 0;
Tim Gore3485d992016-07-05 10:01:30 +01001178 }
1179
Arun Siluvery0504cff2015-07-14 15:01:27 +01001180 /* Pad to end of cacheline */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001181 while ((unsigned long)batch % CACHELINE_BYTES)
1182 *batch++ = MI_NOOP;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001183
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001184 return batch;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001185}
1186
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001187static u32 *gen9_init_perctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery0504cff2015-07-14 15:01:27 +01001188{
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001189 *batch++ = MI_BATCH_BUFFER_END;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001190
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001191 return batch;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001192}
1193
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001194#define CTX_WA_BB_OBJ_SIZE (PAGE_SIZE)
1195
1196static int lrc_setup_wa_ctx(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001197{
Chris Wilson48bb74e2016-08-15 10:49:04 +01001198 struct drm_i915_gem_object *obj;
1199 struct i915_vma *vma;
1200 int err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001201
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001202 obj = i915_gem_object_create(engine->i915, CTX_WA_BB_OBJ_SIZE);
Chris Wilson48bb74e2016-08-15 10:49:04 +01001203 if (IS_ERR(obj))
1204 return PTR_ERR(obj);
1205
Chris Wilsona01cb372017-01-16 15:21:30 +00001206 vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
Chris Wilson48bb74e2016-08-15 10:49:04 +01001207 if (IS_ERR(vma)) {
1208 err = PTR_ERR(vma);
1209 goto err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001210 }
1211
Chris Wilson48bb74e2016-08-15 10:49:04 +01001212 err = i915_vma_pin(vma, 0, PAGE_SIZE, PIN_GLOBAL | PIN_HIGH);
1213 if (err)
1214 goto err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001215
Chris Wilson48bb74e2016-08-15 10:49:04 +01001216 engine->wa_ctx.vma = vma;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001217 return 0;
Chris Wilson48bb74e2016-08-15 10:49:04 +01001218
1219err:
1220 i915_gem_object_put(obj);
1221 return err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001222}
1223
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001224static void lrc_destroy_wa_ctx(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001225{
Chris Wilson19880c42016-08-15 10:49:05 +01001226 i915_vma_unpin_and_release(&engine->wa_ctx.vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001227}
1228
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001229typedef u32 *(*wa_bb_func_t)(struct intel_engine_cs *engine, u32 *batch);
1230
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001231static int intel_init_workaround_bb(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001232{
Chris Wilson48bb74e2016-08-15 10:49:04 +01001233 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001234 struct i915_wa_ctx_bb *wa_bb[2] = { &wa_ctx->indirect_ctx,
1235 &wa_ctx->per_ctx };
1236 wa_bb_func_t wa_bb_fn[2];
Arun Siluvery17ee9502015-06-19 19:07:01 +01001237 struct page *page;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001238 void *batch, *batch_ptr;
1239 unsigned int i;
Chris Wilson48bb74e2016-08-15 10:49:04 +01001240 int ret;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001241
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001242 if (WARN_ON(engine->id != RCS || !engine->scratch))
1243 return -EINVAL;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001244
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001245 switch (INTEL_GEN(engine->i915)) {
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001246 case 10:
1247 return 0;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001248 case 9:
1249 wa_bb_fn[0] = gen9_init_indirectctx_bb;
1250 wa_bb_fn[1] = gen9_init_perctx_bb;
1251 break;
1252 case 8:
1253 wa_bb_fn[0] = gen8_init_indirectctx_bb;
1254 wa_bb_fn[1] = gen8_init_perctx_bb;
1255 break;
1256 default:
1257 MISSING_CASE(INTEL_GEN(engine->i915));
Arun Siluvery5e60d792015-06-23 15:50:44 +01001258 return 0;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001259 }
Arun Siluvery5e60d792015-06-23 15:50:44 +01001260
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001261 ret = lrc_setup_wa_ctx(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001262 if (ret) {
1263 DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
1264 return ret;
1265 }
1266
Chris Wilson48bb74e2016-08-15 10:49:04 +01001267 page = i915_gem_object_get_dirty_page(wa_ctx->vma->obj, 0);
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001268 batch = batch_ptr = kmap_atomic(page);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001269
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001270 /*
1271 * Emit the two workaround batch buffers, recording the offset from the
1272 * start of the workaround batch buffer object for each and their
1273 * respective sizes.
1274 */
1275 for (i = 0; i < ARRAY_SIZE(wa_bb_fn); i++) {
1276 wa_bb[i]->offset = batch_ptr - batch;
1277 if (WARN_ON(!IS_ALIGNED(wa_bb[i]->offset, CACHELINE_BYTES))) {
1278 ret = -EINVAL;
1279 break;
1280 }
1281 batch_ptr = wa_bb_fn[i](engine, batch_ptr);
1282 wa_bb[i]->size = batch_ptr - (batch + wa_bb[i]->offset);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001283 }
1284
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001285 BUG_ON(batch_ptr - batch > CTX_WA_BB_OBJ_SIZE);
1286
Arun Siluvery17ee9502015-06-19 19:07:01 +01001287 kunmap_atomic(batch);
1288 if (ret)
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001289 lrc_destroy_wa_ctx(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001290
1291 return ret;
1292}
1293
Chris Wilson64f09f02017-08-07 13:19:19 +01001294static u8 gtiir[] = {
1295 [RCS] = 0,
1296 [BCS] = 0,
1297 [VCS] = 1,
1298 [VCS2] = 1,
1299 [VECS] = 3,
1300};
1301
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001302static int gen8_init_common_ring(struct intel_engine_cs *engine)
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001303{
Chris Wilsonc0336662016-05-06 15:40:21 +01001304 struct drm_i915_private *dev_priv = engine->i915;
Mika Kuoppalab620e872017-09-22 15:43:03 +03001305 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001306 int ret;
1307
1308 ret = intel_mocs_init_engine(engine);
1309 if (ret)
1310 return ret;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001311
Chris Wilsonad07dfc2016-10-07 07:53:26 +01001312 intel_engine_reset_breadcrumbs(engine);
Chris Wilsonf3b8f912017-01-05 15:30:21 +00001313 intel_engine_init_hangcheck(engine);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001314
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001315 I915_WRITE(RING_HWSTAM(engine->mmio_base), 0xffffffff);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001316 I915_WRITE(RING_MODE_GEN7(engine),
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001317 _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
Chris Wilsonf3b8f912017-01-05 15:30:21 +00001318 I915_WRITE(RING_HWS_PGA(engine->mmio_base),
1319 engine->status_page.ggtt_offset);
1320 POSTING_READ(RING_HWS_PGA(engine->mmio_base));
Michel Thierrydfc53c52015-09-28 13:25:12 +01001321
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001322 DRM_DEBUG_DRIVER("Execlists enabled for %s\n", engine->name);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001323
Chris Wilson64f09f02017-08-07 13:19:19 +01001324 GEM_BUG_ON(engine->id >= ARRAY_SIZE(gtiir));
1325
1326 /*
1327 * Clear any pending interrupt state.
1328 *
1329 * We do it twice out of paranoia that some of the IIR are double
1330 * buffered, and if we only reset it once there may still be
1331 * an interrupt pending.
1332 */
1333 I915_WRITE(GEN8_GT_IIR(gtiir[engine->id]),
1334 GT_CONTEXT_SWITCH_INTERRUPT << engine->irq_shift);
1335 I915_WRITE(GEN8_GT_IIR(gtiir[engine->id]),
1336 GT_CONTEXT_SWITCH_INTERRUPT << engine->irq_shift);
Chris Wilsonf7470262017-01-24 15:20:21 +00001337 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
Mika Kuoppalab620e872017-09-22 15:43:03 +03001338 execlists->csb_head = -1;
Chris Wilson6b764a52017-04-25 11:38:35 +01001339
Chris Wilson64f09f02017-08-07 13:19:19 +01001340 /* After a GPU reset, we may have requests to replay */
Mika Kuoppalab620e872017-09-22 15:43:03 +03001341 if (!i915_modparams.enable_guc_submission && execlists->first)
1342 tasklet_schedule(&execlists->irq_tasklet);
Chris Wilson6b764a52017-04-25 11:38:35 +01001343
Chris Wilson821ed7d2016-09-09 14:11:53 +01001344 return 0;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001345}
1346
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001347static int gen8_init_render_ring(struct intel_engine_cs *engine)
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001348{
Chris Wilsonc0336662016-05-06 15:40:21 +01001349 struct drm_i915_private *dev_priv = engine->i915;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001350 int ret;
1351
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001352 ret = gen8_init_common_ring(engine);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001353 if (ret)
1354 return ret;
1355
1356 /* We need to disable the AsyncFlip performance optimisations in order
1357 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1358 * programmed to '1' on all products.
1359 *
1360 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
1361 */
1362 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1363
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001364 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1365
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001366 return init_workarounds_ring(engine);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001367}
1368
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001369static int gen9_init_render_ring(struct intel_engine_cs *engine)
Damien Lespiau82ef8222015-02-09 19:33:08 +00001370{
1371 int ret;
1372
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001373 ret = gen8_init_common_ring(engine);
Damien Lespiau82ef8222015-02-09 19:33:08 +00001374 if (ret)
1375 return ret;
1376
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001377 return init_workarounds_ring(engine);
Damien Lespiau82ef8222015-02-09 19:33:08 +00001378}
1379
Chris Wilson821ed7d2016-09-09 14:11:53 +01001380static void reset_common_ring(struct intel_engine_cs *engine,
1381 struct drm_i915_gem_request *request)
1382{
Mika Kuoppalab620e872017-09-22 15:43:03 +03001383 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilson221ab97192017-09-16 21:44:14 +01001384 struct drm_i915_gem_request *rq, *rn;
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001385 struct intel_context *ce;
Chris Wilson221ab97192017-09-16 21:44:14 +01001386 unsigned long flags;
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001387
Chris Wilson221ab97192017-09-16 21:44:14 +01001388 spin_lock_irqsave(&engine->timeline->lock, flags);
1389
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001390 /*
1391 * Catch up with any missed context-switch interrupts.
1392 *
1393 * Ideally we would just read the remaining CSB entries now that we
1394 * know the gpu is idle. However, the CSB registers are sometimes^W
1395 * often trashed across a GPU reset! Instead we have to rely on
1396 * guessing the missed context-switch events by looking at what
1397 * requests were completed.
1398 */
Mika Kuoppalacf4591d2017-09-22 15:43:05 +03001399 execlist_cancel_port_requests(execlists);
Chris Wilson221ab97192017-09-16 21:44:14 +01001400
1401 /* Push back any incomplete requests for replay after the reset. */
1402 list_for_each_entry_safe_reverse(rq, rn,
1403 &engine->timeline->requests, link) {
1404 struct i915_priolist *p;
1405
1406 if (i915_gem_request_completed(rq))
1407 break;
1408
1409 __i915_gem_request_unsubmit(rq);
1410
1411 p = lookup_priolist(engine,
1412 &rq->priotree,
1413 rq->priotree.priority);
1414 list_add(&rq->priotree.link,
1415 &ptr_mask_bits(p, 1)->requests);
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001416 }
1417
Chris Wilson221ab97192017-09-16 21:44:14 +01001418 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001419
1420 /* If the request was innocent, we leave the request in the ELSP
1421 * and will try to replay it on restarting. The context image may
1422 * have been corrupted by the reset, in which case we may have
1423 * to service a new GPU hang, but more likely we can continue on
1424 * without impact.
1425 *
1426 * If the request was guilty, we presume the context is corrupt
1427 * and have to at least restore the RING register in the context
1428 * image back to the expected values to skip over the guilty request.
1429 */
Chris Wilson221ab97192017-09-16 21:44:14 +01001430 if (!request || request->fence.error != -EIO)
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001431 return;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001432
Chris Wilsona3aabe82016-10-04 21:11:26 +01001433 /* We want a simple context + ring to execute the breadcrumb update.
1434 * We cannot rely on the context being intact across the GPU hang,
1435 * so clear it and rebuild just what we need for the breadcrumb.
1436 * All pending requests for this context will be zapped, and any
1437 * future request will be after userspace has had the opportunity
1438 * to recreate its own state.
1439 */
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001440 ce = &request->ctx->engine[engine->id];
Chris Wilsona3aabe82016-10-04 21:11:26 +01001441 execlists_init_reg_state(ce->lrc_reg_state,
1442 request->ctx, engine, ce->ring);
1443
Chris Wilson821ed7d2016-09-09 14:11:53 +01001444 /* Move the RING_HEAD onto the breadcrumb, past the hanging batch */
Chris Wilsona3aabe82016-10-04 21:11:26 +01001445 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
1446 i915_ggtt_offset(ce->ring->vma);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001447 ce->lrc_reg_state[CTX_RING_HEAD+1] = request->postfix;
Chris Wilsona3aabe82016-10-04 21:11:26 +01001448
Chris Wilson821ed7d2016-09-09 14:11:53 +01001449 request->ring->head = request->postfix;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001450 intel_ring_update_space(request->ring);
1451
Chris Wilsona3aabe82016-10-04 21:11:26 +01001452 /* Reset WaIdleLiteRestore:bdw,skl as well */
Chris Wilson450362d2017-03-27 14:00:07 +01001453 request->tail =
1454 intel_ring_wrap(request->ring,
1455 request->wa_tail - WA_TAIL_DWORDS*sizeof(u32));
Chris Wilsoned1501d2017-03-27 14:14:12 +01001456 assert_ring_tail_valid(request->ring, request->tail);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001457}
1458
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001459static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request *req)
1460{
1461 struct i915_hw_ppgtt *ppgtt = req->ctx->ppgtt;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001462 struct intel_engine_cs *engine = req->engine;
Mika Kuoppalae7167762017-02-28 17:28:10 +02001463 const int num_lri_cmds = GEN8_3LVL_PDPES * 2;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001464 u32 *cs;
1465 int i;
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001466
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001467 cs = intel_ring_begin(req, num_lri_cmds * 2 + 2);
1468 if (IS_ERR(cs))
1469 return PTR_ERR(cs);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001470
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001471 *cs++ = MI_LOAD_REGISTER_IMM(num_lri_cmds);
Mika Kuoppalae7167762017-02-28 17:28:10 +02001472 for (i = GEN8_3LVL_PDPES - 1; i >= 0; i--) {
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001473 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
1474
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001475 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, i));
1476 *cs++ = upper_32_bits(pd_daddr);
1477 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, i));
1478 *cs++ = lower_32_bits(pd_daddr);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001479 }
1480
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001481 *cs++ = MI_NOOP;
1482 intel_ring_advance(req, cs);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001483
1484 return 0;
1485}
1486
John Harrisonbe795fc2015-05-29 17:44:03 +01001487static int gen8_emit_bb_start(struct drm_i915_gem_request *req,
Chris Wilson803688b2016-08-02 22:50:27 +01001488 u64 offset, u32 len,
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001489 const unsigned int flags)
Oscar Mateo15648582014-07-24 17:04:32 +01001490{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001491 u32 *cs;
Oscar Mateo15648582014-07-24 17:04:32 +01001492 int ret;
1493
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001494 /* Don't rely in hw updating PDPs, specially in lite-restore.
1495 * Ideally, we should set Force PD Restore in ctx descriptor,
1496 * but we can't. Force Restore would be a second option, but
1497 * it is unsafe in case of lite-restore (because the ctx is
Michel Thierry2dba3232015-07-30 11:06:23 +01001498 * not idle). PML4 is allocated during ppgtt init so this is
1499 * not needed in 48-bit.*/
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001500 if (req->ctx->ppgtt &&
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001501 (intel_engine_flag(req->engine) & req->ctx->ppgtt->pd_dirty_rings) &&
1502 !i915_vm_is_48bit(&req->ctx->ppgtt->base) &&
1503 !intel_vgpu_active(req->i915)) {
1504 ret = intel_logical_ring_emit_pdps(req);
1505 if (ret)
1506 return ret;
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001507
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00001508 req->ctx->ppgtt->pd_dirty_rings &= ~intel_engine_flag(req->engine);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001509 }
1510
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001511 cs = intel_ring_begin(req, 4);
1512 if (IS_ERR(cs))
1513 return PTR_ERR(cs);
Oscar Mateo15648582014-07-24 17:04:32 +01001514
1515 /* FIXME(BDW): Address space and security selectors. */
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001516 *cs++ = MI_BATCH_BUFFER_START_GEN8 |
1517 (flags & I915_DISPATCH_SECURE ? 0 : BIT(8)) |
1518 (flags & I915_DISPATCH_RS ? MI_BATCH_RESOURCE_STREAMER : 0);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001519 *cs++ = lower_32_bits(offset);
1520 *cs++ = upper_32_bits(offset);
1521 *cs++ = MI_NOOP;
1522 intel_ring_advance(req, cs);
Oscar Mateo15648582014-07-24 17:04:32 +01001523
1524 return 0;
1525}
1526
Chris Wilson31bb59c2016-07-01 17:23:27 +01001527static void gen8_logical_ring_enable_irq(struct intel_engine_cs *engine)
Oscar Mateo73d477f2014-07-24 17:04:31 +01001528{
Chris Wilsonc0336662016-05-06 15:40:21 +01001529 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson31bb59c2016-07-01 17:23:27 +01001530 I915_WRITE_IMR(engine,
1531 ~(engine->irq_enable_mask | engine->irq_keep_mask));
1532 POSTING_READ_FW(RING_IMR(engine->mmio_base));
Oscar Mateo73d477f2014-07-24 17:04:31 +01001533}
1534
Chris Wilson31bb59c2016-07-01 17:23:27 +01001535static void gen8_logical_ring_disable_irq(struct intel_engine_cs *engine)
Oscar Mateo73d477f2014-07-24 17:04:31 +01001536{
Chris Wilsonc0336662016-05-06 15:40:21 +01001537 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson31bb59c2016-07-01 17:23:27 +01001538 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Oscar Mateo73d477f2014-07-24 17:04:31 +01001539}
1540
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001541static int gen8_emit_flush(struct drm_i915_gem_request *request, u32 mode)
Oscar Mateo47122742014-07-24 17:04:28 +01001542{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001543 u32 cmd, *cs;
Oscar Mateo47122742014-07-24 17:04:28 +01001544
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001545 cs = intel_ring_begin(request, 4);
1546 if (IS_ERR(cs))
1547 return PTR_ERR(cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001548
1549 cmd = MI_FLUSH_DW + 1;
1550
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001551 /* We always require a command barrier so that subsequent
1552 * commands, such as breadcrumb interrupts, are strictly ordered
1553 * wrt the contents of the write cache being flushed to memory
1554 * (and thus being coherent from the CPU).
1555 */
1556 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1557
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001558 if (mode & EMIT_INVALIDATE) {
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001559 cmd |= MI_INVALIDATE_TLB;
Chris Wilson1dae2df2016-08-02 22:50:19 +01001560 if (request->engine->id == VCS)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001561 cmd |= MI_INVALIDATE_BSD;
Oscar Mateo47122742014-07-24 17:04:28 +01001562 }
1563
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001564 *cs++ = cmd;
1565 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
1566 *cs++ = 0; /* upper addr */
1567 *cs++ = 0; /* value */
1568 intel_ring_advance(request, cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001569
1570 return 0;
1571}
1572
John Harrison7deb4d32015-05-29 17:43:59 +01001573static int gen8_emit_flush_render(struct drm_i915_gem_request *request,
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001574 u32 mode)
Oscar Mateo47122742014-07-24 17:04:28 +01001575{
Chris Wilsonb5321f32016-08-02 22:50:18 +01001576 struct intel_engine_cs *engine = request->engine;
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001577 u32 scratch_addr =
1578 i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001579 bool vf_flush_wa = false, dc_flush_wa = false;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001580 u32 *cs, flags = 0;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001581 int len;
Oscar Mateo47122742014-07-24 17:04:28 +01001582
1583 flags |= PIPE_CONTROL_CS_STALL;
1584
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001585 if (mode & EMIT_FLUSH) {
Oscar Mateo47122742014-07-24 17:04:28 +01001586 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
1587 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -08001588 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +01001589 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Oscar Mateo47122742014-07-24 17:04:28 +01001590 }
1591
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001592 if (mode & EMIT_INVALIDATE) {
Oscar Mateo47122742014-07-24 17:04:28 +01001593 flags |= PIPE_CONTROL_TLB_INVALIDATE;
1594 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
1595 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
1596 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
1597 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
1598 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
1599 flags |= PIPE_CONTROL_QW_WRITE;
1600 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Oscar Mateo47122742014-07-24 17:04:28 +01001601
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001602 /*
1603 * On GEN9: before VF_CACHE_INVALIDATE we need to emit a NULL
1604 * pipe control.
1605 */
Chris Wilsonc0336662016-05-06 15:40:21 +01001606 if (IS_GEN9(request->i915))
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001607 vf_flush_wa = true;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001608
1609 /* WaForGAMHang:kbl */
1610 if (IS_KBL_REVID(request->i915, 0, KBL_REVID_B0))
1611 dc_flush_wa = true;
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001612 }
Imre Deak9647ff32015-01-25 13:27:11 -08001613
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001614 len = 6;
1615
1616 if (vf_flush_wa)
1617 len += 6;
1618
1619 if (dc_flush_wa)
1620 len += 12;
1621
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001622 cs = intel_ring_begin(request, len);
1623 if (IS_ERR(cs))
1624 return PTR_ERR(cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001625
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001626 if (vf_flush_wa)
1627 cs = gen8_emit_pipe_control(cs, 0, 0);
Imre Deak9647ff32015-01-25 13:27:11 -08001628
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001629 if (dc_flush_wa)
1630 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_DC_FLUSH_ENABLE,
1631 0);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001632
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001633 cs = gen8_emit_pipe_control(cs, flags, scratch_addr);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001634
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001635 if (dc_flush_wa)
1636 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_CS_STALL, 0);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001637
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001638 intel_ring_advance(request, cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001639
1640 return 0;
1641}
1642
Chris Wilson7c17d372016-01-20 15:43:35 +02001643/*
1644 * Reserve space for 2 NOOPs at the end of each request to be
1645 * used as a workaround for not being allowed to do lite
1646 * restore with HEAD==TAIL (WaIdleLiteRestore).
1647 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001648static void gen8_emit_wa_tail(struct drm_i915_gem_request *request, u32 *cs)
Oscar Mateo4da46e12014-07-24 17:04:27 +01001649{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001650 *cs++ = MI_NOOP;
1651 *cs++ = MI_NOOP;
1652 request->wa_tail = intel_ring_offset(request, cs);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001653}
Oscar Mateo4da46e12014-07-24 17:04:27 +01001654
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001655static void gen8_emit_breadcrumb(struct drm_i915_gem_request *request, u32 *cs)
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001656{
Chris Wilson7c17d372016-01-20 15:43:35 +02001657 /* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */
1658 BUILD_BUG_ON(I915_GEM_HWS_INDEX_ADDR & (1 << 5));
Oscar Mateo4da46e12014-07-24 17:04:27 +01001659
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001660 *cs++ = (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW;
1661 *cs++ = intel_hws_seqno_address(request->engine) | MI_FLUSH_DW_USE_GTT;
1662 *cs++ = 0;
1663 *cs++ = request->global_seqno;
1664 *cs++ = MI_USER_INTERRUPT;
1665 *cs++ = MI_NOOP;
1666 request->tail = intel_ring_offset(request, cs);
Chris Wilsoned1501d2017-03-27 14:14:12 +01001667 assert_ring_tail_valid(request->ring, request->tail);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001668
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001669 gen8_emit_wa_tail(request, cs);
Chris Wilson7c17d372016-01-20 15:43:35 +02001670}
Oscar Mateo4da46e12014-07-24 17:04:27 +01001671
Chris Wilson98f29e82016-10-28 13:58:51 +01001672static const int gen8_emit_breadcrumb_sz = 6 + WA_TAIL_DWORDS;
1673
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001674static void gen8_emit_breadcrumb_render(struct drm_i915_gem_request *request,
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001675 u32 *cs)
Chris Wilson7c17d372016-01-20 15:43:35 +02001676{
Michał Winiarskice81a652016-04-12 15:51:55 +02001677 /* We're using qword write, seqno should be aligned to 8 bytes. */
1678 BUILD_BUG_ON(I915_GEM_HWS_INDEX & 1);
1679
Chris Wilson7c17d372016-01-20 15:43:35 +02001680 /* w/a for post sync ops following a GPGPU operation we
1681 * need a prior CS_STALL, which is emitted by the flush
1682 * following the batch.
Michel Thierry53292cd2015-04-15 18:11:33 +01001683 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001684 *cs++ = GFX_OP_PIPE_CONTROL(6);
1685 *cs++ = PIPE_CONTROL_GLOBAL_GTT_IVB | PIPE_CONTROL_CS_STALL |
1686 PIPE_CONTROL_QW_WRITE;
1687 *cs++ = intel_hws_seqno_address(request->engine);
1688 *cs++ = 0;
1689 *cs++ = request->global_seqno;
Michał Winiarskice81a652016-04-12 15:51:55 +02001690 /* We're thrashing one dword of HWS. */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001691 *cs++ = 0;
1692 *cs++ = MI_USER_INTERRUPT;
1693 *cs++ = MI_NOOP;
1694 request->tail = intel_ring_offset(request, cs);
Chris Wilsoned1501d2017-03-27 14:14:12 +01001695 assert_ring_tail_valid(request->ring, request->tail);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001696
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001697 gen8_emit_wa_tail(request, cs);
Oscar Mateo4da46e12014-07-24 17:04:27 +01001698}
1699
Chris Wilson98f29e82016-10-28 13:58:51 +01001700static const int gen8_emit_breadcrumb_render_sz = 8 + WA_TAIL_DWORDS;
1701
John Harrison87531812015-05-29 17:43:44 +01001702static int gen8_init_rcs_context(struct drm_i915_gem_request *req)
Thomas Daniele7778be2014-12-02 12:50:48 +00001703{
1704 int ret;
1705
Tvrtko Ursulin4ac96592017-02-14 15:00:17 +00001706 ret = intel_ring_workarounds_emit(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001707 if (ret)
1708 return ret;
1709
Peter Antoine3bbaba02015-07-10 20:13:11 +03001710 ret = intel_rcs_context_init_mocs(req);
1711 /*
1712 * Failing to program the MOCS is non-fatal.The system will not
1713 * run at peak performance. So generate an error and carry on.
1714 */
1715 if (ret)
1716 DRM_ERROR("MOCS failed to program: expect performance issues.\n");
1717
Chris Wilson4e50f082016-10-28 13:58:31 +01001718 return i915_gem_render_state_emit(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001719}
1720
Oscar Mateo73e4d072014-07-24 17:04:48 +01001721/**
1722 * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001723 * @engine: Engine Command Streamer.
Oscar Mateo73e4d072014-07-24 17:04:48 +01001724 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001725void intel_logical_ring_cleanup(struct intel_engine_cs *engine)
Oscar Mateo454afeb2014-07-24 17:04:22 +01001726{
John Harrison6402c332014-10-31 12:00:26 +00001727 struct drm_i915_private *dev_priv;
Oscar Mateo9832b9d2014-07-24 17:04:30 +01001728
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01001729 /*
1730 * Tasklet cannot be active at this point due intel_mark_active/idle
1731 * so this is just for documentation.
1732 */
Mika Kuoppalab620e872017-09-22 15:43:03 +03001733 if (WARN_ON(test_bit(TASKLET_STATE_SCHED, &engine->execlists.irq_tasklet.state)))
1734 tasklet_kill(&engine->execlists.irq_tasklet);
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01001735
Chris Wilsonc0336662016-05-06 15:40:21 +01001736 dev_priv = engine->i915;
John Harrison6402c332014-10-31 12:00:26 +00001737
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001738 if (engine->buffer) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001739 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
Dave Gordonb0366a52015-12-08 15:02:36 +00001740 }
Oscar Mateo48d82382014-07-24 17:04:23 +01001741
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001742 if (engine->cleanup)
1743 engine->cleanup(engine);
Oscar Mateo48d82382014-07-24 17:04:23 +01001744
Chris Wilsone8a9c582016-12-18 15:37:20 +00001745 intel_engine_cleanup_common(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001746
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001747 lrc_destroy_wa_ctx(engine);
Chris Wilsonc0336662016-05-06 15:40:21 +01001748 engine->i915 = NULL;
Akash Goel3b3f1652016-10-13 22:44:48 +05301749 dev_priv->engine[engine->id] = NULL;
1750 kfree(engine);
Oscar Mateo454afeb2014-07-24 17:04:22 +01001751}
1752
Chris Wilsonff44ad52017-03-16 17:13:03 +00001753static void execlists_set_default_submission(struct intel_engine_cs *engine)
Chris Wilsonddd66c52016-08-02 22:50:31 +01001754{
Chris Wilsonff44ad52017-03-16 17:13:03 +00001755 engine->submit_request = execlists_submit_request;
Chris Wilson27a5f612017-09-15 18:31:00 +01001756 engine->cancel_requests = execlists_cancel_requests;
Chris Wilsonff44ad52017-03-16 17:13:03 +00001757 engine->schedule = execlists_schedule;
Mika Kuoppalab620e872017-09-22 15:43:03 +03001758 engine->execlists.irq_tasklet.func = intel_lrc_irq_handler;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001759}
1760
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001761static void
Chris Wilsone1382ef2016-05-06 15:40:20 +01001762logical_ring_default_vfuncs(struct intel_engine_cs *engine)
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001763{
1764 /* Default vfuncs which can be overriden by each engine. */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001765 engine->init_hw = gen8_init_common_ring;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001766 engine->reset_hw = reset_common_ring;
Chris Wilsone8a9c582016-12-18 15:37:20 +00001767
1768 engine->context_pin = execlists_context_pin;
1769 engine->context_unpin = execlists_context_unpin;
1770
Chris Wilsonf73e7392016-12-18 15:37:24 +00001771 engine->request_alloc = execlists_request_alloc;
1772
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001773 engine->emit_flush = gen8_emit_flush;
Chris Wilson9b81d552016-10-28 13:58:50 +01001774 engine->emit_breadcrumb = gen8_emit_breadcrumb;
Chris Wilson98f29e82016-10-28 13:58:51 +01001775 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_sz;
Chris Wilsonff44ad52017-03-16 17:13:03 +00001776
1777 engine->set_default_submission = execlists_set_default_submission;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001778
Chris Wilson31bb59c2016-07-01 17:23:27 +01001779 engine->irq_enable = gen8_logical_ring_enable_irq;
1780 engine->irq_disable = gen8_logical_ring_disable_irq;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001781 engine->emit_bb_start = gen8_emit_bb_start;
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001782}
1783
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001784static inline void
Dave Gordonc2c7f242016-07-13 16:03:35 +01001785logical_ring_default_irqs(struct intel_engine_cs *engine)
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001786{
Dave Gordonc2c7f242016-07-13 16:03:35 +01001787 unsigned shift = engine->irq_shift;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001788 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << shift;
1789 engine->irq_keep_mask = GT_CONTEXT_SWITCH_INTERRUPT << shift;
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001790}
1791
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001792static void
1793logical_ring_setup(struct intel_engine_cs *engine)
1794{
1795 struct drm_i915_private *dev_priv = engine->i915;
1796 enum forcewake_domains fw_domains;
1797
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01001798 intel_engine_setup_common(engine);
1799
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001800 /* Intentionally left blank. */
1801 engine->buffer = NULL;
1802
1803 fw_domains = intel_uncore_forcewake_for_reg(dev_priv,
1804 RING_ELSP(engine),
1805 FW_REG_WRITE);
1806
1807 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
1808 RING_CONTEXT_STATUS_PTR(engine),
1809 FW_REG_READ | FW_REG_WRITE);
1810
1811 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
1812 RING_CONTEXT_STATUS_BUF_BASE(engine),
1813 FW_REG_READ);
1814
Mika Kuoppalab620e872017-09-22 15:43:03 +03001815 engine->execlists.fw_domains = fw_domains;
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001816
Mika Kuoppalab620e872017-09-22 15:43:03 +03001817 tasklet_init(&engine->execlists.irq_tasklet,
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001818 intel_lrc_irq_handler, (unsigned long)engine);
1819
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001820 logical_ring_default_vfuncs(engine);
1821 logical_ring_default_irqs(engine);
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001822}
1823
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +01001824static int logical_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001825{
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001826 int ret;
1827
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01001828 ret = intel_engine_init_common(engine);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001829 if (ret)
1830 goto error;
1831
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001832 return 0;
1833
1834error:
1835 intel_logical_ring_cleanup(engine);
1836 return ret;
1837}
1838
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +01001839int logical_render_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001840{
1841 struct drm_i915_private *dev_priv = engine->i915;
1842 int ret;
1843
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001844 logical_ring_setup(engine);
1845
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001846 if (HAS_L3_DPF(dev_priv))
1847 engine->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
1848
1849 /* Override some for render ring. */
1850 if (INTEL_GEN(dev_priv) >= 9)
1851 engine->init_hw = gen9_init_render_ring;
1852 else
1853 engine->init_hw = gen8_init_render_ring;
1854 engine->init_context = gen8_init_rcs_context;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001855 engine->emit_flush = gen8_emit_flush_render;
Chris Wilson9b81d552016-10-28 13:58:50 +01001856 engine->emit_breadcrumb = gen8_emit_breadcrumb_render;
Chris Wilson98f29e82016-10-28 13:58:51 +01001857 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_render_sz;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001858
Chris Wilsonf51455d2017-01-10 14:47:34 +00001859 ret = intel_engine_create_scratch(engine, PAGE_SIZE);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001860 if (ret)
1861 return ret;
1862
1863 ret = intel_init_workaround_bb(engine);
1864 if (ret) {
1865 /*
1866 * We continue even if we fail to initialize WA batch
1867 * because we only expect rare glitches but nothing
1868 * critical to prevent us from using GPU
1869 */
1870 DRM_ERROR("WA batch buffer initialization failed: %d\n",
1871 ret);
1872 }
1873
Tvrtko Ursulind038fc72016-12-16 13:18:42 +00001874 return logical_ring_init(engine);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001875}
1876
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +01001877int logical_xcs_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001878{
1879 logical_ring_setup(engine);
1880
1881 return logical_ring_init(engine);
1882}
1883
Jeff McGee0cea6502015-02-13 10:27:56 -06001884static u32
Chris Wilsonc0336662016-05-06 15:40:21 +01001885make_rpcs(struct drm_i915_private *dev_priv)
Jeff McGee0cea6502015-02-13 10:27:56 -06001886{
1887 u32 rpcs = 0;
1888
1889 /*
1890 * No explicit RPCS request is needed to ensure full
1891 * slice/subslice/EU enablement prior to Gen9.
1892 */
Chris Wilsonc0336662016-05-06 15:40:21 +01001893 if (INTEL_GEN(dev_priv) < 9)
Jeff McGee0cea6502015-02-13 10:27:56 -06001894 return 0;
1895
1896 /*
1897 * Starting in Gen9, render power gating can leave
1898 * slice/subslice/EU in a partially enabled state. We
1899 * must make an explicit request through RPCS for full
1900 * enablement.
1901 */
Imre Deak43b67992016-08-31 19:13:02 +03001902 if (INTEL_INFO(dev_priv)->sseu.has_slice_pg) {
Jeff McGee0cea6502015-02-13 10:27:56 -06001903 rpcs |= GEN8_RPCS_S_CNT_ENABLE;
Imre Deakf08a0c92016-08-31 19:13:04 +03001904 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.slice_mask) <<
Jeff McGee0cea6502015-02-13 10:27:56 -06001905 GEN8_RPCS_S_CNT_SHIFT;
1906 rpcs |= GEN8_RPCS_ENABLE;
1907 }
1908
Imre Deak43b67992016-08-31 19:13:02 +03001909 if (INTEL_INFO(dev_priv)->sseu.has_subslice_pg) {
Jeff McGee0cea6502015-02-13 10:27:56 -06001910 rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
Imre Deak57ec1712016-08-31 19:13:05 +03001911 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.subslice_mask) <<
Jeff McGee0cea6502015-02-13 10:27:56 -06001912 GEN8_RPCS_SS_CNT_SHIFT;
1913 rpcs |= GEN8_RPCS_ENABLE;
1914 }
1915
Imre Deak43b67992016-08-31 19:13:02 +03001916 if (INTEL_INFO(dev_priv)->sseu.has_eu_pg) {
1917 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
Jeff McGee0cea6502015-02-13 10:27:56 -06001918 GEN8_RPCS_EU_MIN_SHIFT;
Imre Deak43b67992016-08-31 19:13:02 +03001919 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
Jeff McGee0cea6502015-02-13 10:27:56 -06001920 GEN8_RPCS_EU_MAX_SHIFT;
1921 rpcs |= GEN8_RPCS_ENABLE;
1922 }
1923
1924 return rpcs;
1925}
1926
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001927static u32 intel_lr_indirect_ctx_offset(struct intel_engine_cs *engine)
Michel Thierry71562912016-02-23 10:31:49 +00001928{
1929 u32 indirect_ctx_offset;
1930
Chris Wilsonc0336662016-05-06 15:40:21 +01001931 switch (INTEL_GEN(engine->i915)) {
Michel Thierry71562912016-02-23 10:31:49 +00001932 default:
Chris Wilsonc0336662016-05-06 15:40:21 +01001933 MISSING_CASE(INTEL_GEN(engine->i915));
Michel Thierry71562912016-02-23 10:31:49 +00001934 /* fall through */
Michel Thierry7bd0a2c2017-06-06 13:30:38 -07001935 case 10:
1936 indirect_ctx_offset =
1937 GEN10_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
1938 break;
Michel Thierry71562912016-02-23 10:31:49 +00001939 case 9:
1940 indirect_ctx_offset =
1941 GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
1942 break;
1943 case 8:
1944 indirect_ctx_offset =
1945 GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
1946 break;
1947 }
1948
1949 return indirect_ctx_offset;
1950}
1951
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001952static void execlists_init_reg_state(u32 *regs,
Chris Wilsona3aabe82016-10-04 21:11:26 +01001953 struct i915_gem_context *ctx,
1954 struct intel_engine_cs *engine,
1955 struct intel_ring *ring)
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001956{
Chris Wilsona3aabe82016-10-04 21:11:26 +01001957 struct drm_i915_private *dev_priv = engine->i915;
1958 struct i915_hw_ppgtt *ppgtt = ctx->ppgtt ?: dev_priv->mm.aliasing_ppgtt;
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001959 u32 base = engine->mmio_base;
1960 bool rcs = engine->id == RCS;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001961
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001962 /* A context is actually a big batch buffer with several
1963 * MI_LOAD_REGISTER_IMM commands followed by (reg, value) pairs. The
1964 * values we are setting here are only for the first context restore:
1965 * on a subsequent save, the GPU will recreate this batchbuffer with new
1966 * values (including all the missing MI_LOAD_REGISTER_IMM commands that
1967 * we are not initializing here).
1968 */
1969 regs[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(rcs ? 14 : 11) |
1970 MI_LRI_FORCE_POSTED;
1971
1972 CTX_REG(regs, CTX_CONTEXT_CONTROL, RING_CONTEXT_CONTROL(engine),
1973 _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
1974 CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
1975 (HAS_RESOURCE_STREAMER(dev_priv) ?
1976 CTX_CTRL_RS_CTX_ENABLE : 0)));
1977 CTX_REG(regs, CTX_RING_HEAD, RING_HEAD(base), 0);
1978 CTX_REG(regs, CTX_RING_TAIL, RING_TAIL(base), 0);
1979 CTX_REG(regs, CTX_RING_BUFFER_START, RING_START(base), 0);
1980 CTX_REG(regs, CTX_RING_BUFFER_CONTROL, RING_CTL(base),
1981 RING_CTL_SIZE(ring->size) | RING_VALID);
1982 CTX_REG(regs, CTX_BB_HEAD_U, RING_BBADDR_UDW(base), 0);
1983 CTX_REG(regs, CTX_BB_HEAD_L, RING_BBADDR(base), 0);
1984 CTX_REG(regs, CTX_BB_STATE, RING_BBSTATE(base), RING_BB_PPGTT);
1985 CTX_REG(regs, CTX_SECOND_BB_HEAD_U, RING_SBBADDR_UDW(base), 0);
1986 CTX_REG(regs, CTX_SECOND_BB_HEAD_L, RING_SBBADDR(base), 0);
1987 CTX_REG(regs, CTX_SECOND_BB_STATE, RING_SBBSTATE(base), 0);
1988 if (rcs) {
1989 CTX_REG(regs, CTX_BB_PER_CTX_PTR, RING_BB_PER_CTX_PTR(base), 0);
1990 CTX_REG(regs, CTX_RCS_INDIRECT_CTX, RING_INDIRECT_CTX(base), 0);
1991 CTX_REG(regs, CTX_RCS_INDIRECT_CTX_OFFSET,
1992 RING_INDIRECT_CTX_OFFSET(base), 0);
1993
Chris Wilson48bb74e2016-08-15 10:49:04 +01001994 if (engine->wa_ctx.vma) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001995 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001996 u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001997
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00001998 regs[CTX_RCS_INDIRECT_CTX + 1] =
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001999 (ggtt_offset + wa_ctx->indirect_ctx.offset) |
2000 (wa_ctx->indirect_ctx.size / CACHELINE_BYTES);
Arun Siluvery17ee9502015-06-19 19:07:01 +01002001
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002002 regs[CTX_RCS_INDIRECT_CTX_OFFSET + 1] =
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002003 intel_lr_indirect_ctx_offset(engine) << 6;
Arun Siluvery17ee9502015-06-19 19:07:01 +01002004
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002005 regs[CTX_BB_PER_CTX_PTR + 1] =
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00002006 (ggtt_offset + wa_ctx->per_ctx.offset) | 0x01;
Arun Siluvery17ee9502015-06-19 19:07:01 +01002007 }
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002008 }
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002009
2010 regs[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9) | MI_LRI_FORCE_POSTED;
2011
2012 CTX_REG(regs, CTX_CTX_TIMESTAMP, RING_CTX_TIMESTAMP(base), 0);
Ville Syrjälä0d925ea2015-11-04 23:20:11 +02002013 /* PDP values well be assigned later if needed */
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002014 CTX_REG(regs, CTX_PDP3_UDW, GEN8_RING_PDP_UDW(engine, 3), 0);
2015 CTX_REG(regs, CTX_PDP3_LDW, GEN8_RING_PDP_LDW(engine, 3), 0);
2016 CTX_REG(regs, CTX_PDP2_UDW, GEN8_RING_PDP_UDW(engine, 2), 0);
2017 CTX_REG(regs, CTX_PDP2_LDW, GEN8_RING_PDP_LDW(engine, 2), 0);
2018 CTX_REG(regs, CTX_PDP1_UDW, GEN8_RING_PDP_UDW(engine, 1), 0);
2019 CTX_REG(regs, CTX_PDP1_LDW, GEN8_RING_PDP_LDW(engine, 1), 0);
2020 CTX_REG(regs, CTX_PDP0_UDW, GEN8_RING_PDP_UDW(engine, 0), 0);
2021 CTX_REG(regs, CTX_PDP0_LDW, GEN8_RING_PDP_LDW(engine, 0), 0);
Michel Thierryd7b26332015-04-08 12:13:34 +01002022
Chris Wilson949e8ab2017-02-09 14:40:36 +00002023 if (ppgtt && i915_vm_is_48bit(&ppgtt->base)) {
Michel Thierry2dba3232015-07-30 11:06:23 +01002024 /* 64b PPGTT (48bit canonical)
2025 * PDP0_DESCRIPTOR contains the base address to PML4 and
2026 * other PDP Descriptors are ignored.
2027 */
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002028 ASSIGN_CTX_PML4(ppgtt, regs);
Michel Thierry2dba3232015-07-30 11:06:23 +01002029 }
2030
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002031 if (rcs) {
2032 regs[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
2033 CTX_REG(regs, CTX_R_PWR_CLK_STATE, GEN8_R_PWR_CLK_STATE,
2034 make_rpcs(dev_priv));
Robert Bragg19f81df2017-06-13 12:23:03 +01002035
2036 i915_oa_init_reg_state(engine, ctx, regs);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002037 }
Chris Wilsona3aabe82016-10-04 21:11:26 +01002038}
2039
2040static int
2041populate_lr_context(struct i915_gem_context *ctx,
2042 struct drm_i915_gem_object *ctx_obj,
2043 struct intel_engine_cs *engine,
2044 struct intel_ring *ring)
2045{
2046 void *vaddr;
2047 int ret;
2048
2049 ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
2050 if (ret) {
2051 DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
2052 return ret;
2053 }
2054
2055 vaddr = i915_gem_object_pin_map(ctx_obj, I915_MAP_WB);
2056 if (IS_ERR(vaddr)) {
2057 ret = PTR_ERR(vaddr);
2058 DRM_DEBUG_DRIVER("Could not map object pages! (%d)\n", ret);
2059 return ret;
2060 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002061 ctx_obj->mm.dirty = true;
Chris Wilsona3aabe82016-10-04 21:11:26 +01002062
2063 /* The second page of the context object contains some fields which must
2064 * be set up prior to the first execution. */
2065
2066 execlists_init_reg_state(vaddr + LRC_STATE_PN * PAGE_SIZE,
2067 ctx, engine, ring);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002068
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01002069 i915_gem_object_unpin_map(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002070
2071 return 0;
2072}
2073
Chris Wilsone2efd132016-05-24 14:53:34 +01002074static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
Chris Wilson978f1e02016-04-28 09:56:54 +01002075 struct intel_engine_cs *engine)
Oscar Mateoede7d422014-07-24 17:04:12 +01002076{
Oscar Mateo8c8579172014-07-24 17:04:14 +01002077 struct drm_i915_gem_object *ctx_obj;
Chris Wilson9021ad02016-05-24 14:53:37 +01002078 struct intel_context *ce = &ctx->engine[engine->id];
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002079 struct i915_vma *vma;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002080 uint32_t context_size;
Chris Wilson7e37f882016-08-02 22:50:21 +01002081 struct intel_ring *ring;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002082 int ret;
2083
Chris Wilson9021ad02016-05-24 14:53:37 +01002084 WARN_ON(ce->state);
Oscar Mateoede7d422014-07-24 17:04:12 +01002085
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +03002086 context_size = round_up(engine->context_size, I915_GTT_PAGE_SIZE);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002087
Michel Thierry0b29c752017-09-13 09:56:00 +01002088 /*
2089 * Before the actual start of the context image, we insert a few pages
2090 * for our own use and for sharing with the GuC.
2091 */
2092 context_size += LRC_HEADER_PAGES * PAGE_SIZE;
Alex Daid1675192015-08-12 15:43:43 +01002093
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00002094 ctx_obj = i915_gem_object_create(ctx->i915, context_size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01002095 if (IS_ERR(ctx_obj)) {
Dan Carpenter3126a662015-04-30 17:30:50 +03002096 DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
Chris Wilsonfe3db792016-04-25 13:32:13 +01002097 return PTR_ERR(ctx_obj);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002098 }
2099
Chris Wilsona01cb372017-01-16 15:21:30 +00002100 vma = i915_vma_instance(ctx_obj, &ctx->i915->ggtt.base, NULL);
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002101 if (IS_ERR(vma)) {
2102 ret = PTR_ERR(vma);
2103 goto error_deref_obj;
2104 }
2105
Chris Wilson7e37f882016-08-02 22:50:21 +01002106 ring = intel_engine_create_ring(engine, ctx->ring_size);
Chris Wilsondca33ec2016-08-02 22:50:20 +01002107 if (IS_ERR(ring)) {
2108 ret = PTR_ERR(ring);
Nick Hoathe84fe802015-09-11 12:53:46 +01002109 goto error_deref_obj;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002110 }
2111
Chris Wilsondca33ec2016-08-02 22:50:20 +01002112 ret = populate_lr_context(ctx, ctx_obj, engine, ring);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002113 if (ret) {
2114 DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
Chris Wilsondca33ec2016-08-02 22:50:20 +01002115 goto error_ring_free;
Oscar Mateo84c23772014-07-24 17:04:15 +01002116 }
2117
Chris Wilsondca33ec2016-08-02 22:50:20 +01002118 ce->ring = ring;
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002119 ce->state = vma;
Chuanxiao Dong0d402a22017-05-11 18:07:42 +08002120 ce->initialised |= engine->init_context == NULL;
Oscar Mateoede7d422014-07-24 17:04:12 +01002121
2122 return 0;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002123
Chris Wilsondca33ec2016-08-02 22:50:20 +01002124error_ring_free:
Chris Wilson7e37f882016-08-02 22:50:21 +01002125 intel_ring_free(ring);
Nick Hoathe84fe802015-09-11 12:53:46 +01002126error_deref_obj:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002127 i915_gem_object_put(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002128 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01002129}
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002130
Chris Wilson821ed7d2016-09-09 14:11:53 +01002131void intel_lr_context_resume(struct drm_i915_private *dev_priv)
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002132{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002133 struct intel_engine_cs *engine;
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002134 struct i915_gem_context *ctx;
Akash Goel3b3f1652016-10-13 22:44:48 +05302135 enum intel_engine_id id;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002136
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002137 /* Because we emit WA_TAIL_DWORDS there may be a disparity
2138 * between our bookkeeping in ce->ring->head and ce->ring->tail and
2139 * that stored in context. As we only write new commands from
2140 * ce->ring->tail onwards, everything before that is junk. If the GPU
2141 * starts reading from its RING_HEAD from the context, it may try to
2142 * execute that junk and die.
2143 *
2144 * So to avoid that we reset the context images upon resume. For
2145 * simplicity, we just zero everything out.
2146 */
Chris Wilson829a0af2017-06-20 12:05:45 +01002147 list_for_each_entry(ctx, &dev_priv->contexts.list, link) {
Akash Goel3b3f1652016-10-13 22:44:48 +05302148 for_each_engine(engine, dev_priv, id) {
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002149 struct intel_context *ce = &ctx->engine[engine->id];
2150 u32 *reg;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002151
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002152 if (!ce->state)
2153 continue;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002154
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002155 reg = i915_gem_object_pin_map(ce->state->obj,
2156 I915_MAP_WB);
2157 if (WARN_ON(IS_ERR(reg)))
2158 continue;
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01002159
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002160 reg += LRC_STATE_PN * PAGE_SIZE / sizeof(*reg);
2161 reg[CTX_RING_HEAD+1] = 0;
2162 reg[CTX_RING_TAIL+1] = 0;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002163
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002164 ce->state->obj->mm.dirty = true;
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002165 i915_gem_object_unpin_map(ce->state->obj);
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002166
Chris Wilsone6ba9992017-04-25 14:00:49 +01002167 intel_ring_reset(ce->ring, 0);
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002168 }
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002169 }
2170}