blob: 69838f66886210d0007f581fae429e2eca9d8546 [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"
Chris Wilson7c2fa7f2017-11-10 14:26:34 +0000139#include "i915_gem_render_state.h"
Michel Thierry578f1ac2018-01-23 16:43:49 -0800140#include "intel_lrc_reg.h"
Peter Antoine3bbaba02015-07-10 20:13:11 +0300141#include "intel_mocs.h"
Oscar Mateo127f1002014-07-24 17:04:11 +0100142
Thomas Daniele981e7b2014-07-24 17:04:39 +0100143#define RING_EXECLIST_QFULL (1 << 0x2)
144#define RING_EXECLIST1_VALID (1 << 0x3)
145#define RING_EXECLIST0_VALID (1 << 0x4)
146#define RING_EXECLIST_ACTIVE_STATUS (3 << 0xE)
147#define RING_EXECLIST1_ACTIVE (1 << 0x11)
148#define RING_EXECLIST0_ACTIVE (1 << 0x12)
149
150#define GEN8_CTX_STATUS_IDLE_ACTIVE (1 << 0)
151#define GEN8_CTX_STATUS_PREEMPTED (1 << 1)
152#define GEN8_CTX_STATUS_ELEMENT_SWITCH (1 << 2)
153#define GEN8_CTX_STATUS_ACTIVE_IDLE (1 << 3)
154#define GEN8_CTX_STATUS_COMPLETE (1 << 4)
155#define GEN8_CTX_STATUS_LITE_RESTORE (1 << 15)
Oscar Mateo8670d6f2014-07-24 17:04:17 +0100156
Chris Wilson70c2a242016-09-09 14:11:46 +0100157#define GEN8_CTX_STATUS_COMPLETED_MASK \
Chris Wilsond8747af2017-11-20 12:34:56 +0000158 (GEN8_CTX_STATUS_COMPLETE | GEN8_CTX_STATUS_PREEMPTED)
Chris Wilson70c2a242016-09-09 14:11:46 +0100159
Chris Wilson0e93cdd2016-04-29 09:07:06 +0100160/* Typical size of the average request (2 pipecontrols and a MI_BB) */
161#define EXECLISTS_REQUEST_SIZE 64 /* bytes */
Chris Wilsona3aabe82016-10-04 21:11:26 +0100162#define WA_TAIL_DWORDS 2
Chris Wilson7e4992a2017-09-28 20:38:59 +0100163#define WA_TAIL_BYTES (sizeof(u32) * WA_TAIL_DWORDS)
Chris Wilsona3aabe82016-10-04 21:11:26 +0100164
Chris Wilsone2efd132016-05-24 14:53:34 +0100165static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
Chris Wilson978f1e02016-04-28 09:56:54 +0100166 struct intel_engine_cs *engine);
Chris Wilsona3aabe82016-10-04 21:11:26 +0100167static void execlists_init_reg_state(u32 *reg_state,
168 struct i915_gem_context *ctx,
169 struct intel_engine_cs *engine,
170 struct intel_ring *ring);
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000171
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000172static inline struct i915_priolist *to_priolist(struct rb_node *rb)
173{
174 return rb_entry(rb, struct i915_priolist, node);
175}
176
177static inline int rq_prio(const struct i915_request *rq)
178{
179 return rq->priotree.priority;
180}
181
182static inline bool need_preempt(const struct intel_engine_cs *engine,
183 const struct i915_request *last,
184 int prio)
185{
186 return engine->i915->preempt_context && prio > max(rq_prio(last), 0);
187}
188
Oscar Mateo73e4d072014-07-24 17:04:48 +0100189/**
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000190 * intel_lr_context_descriptor_update() - calculate & cache the descriptor
191 * descriptor for a pinned context
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000192 * @ctx: Context to work on
Chris Wilson9021ad02016-05-24 14:53:37 +0100193 * @engine: Engine the descriptor will be used with
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000194 *
195 * The context descriptor encodes various attributes of a context,
196 * including its GTT address and some flags. Because it's fairly
197 * expensive to calculate, we'll just do it once and cache the result,
198 * which remains valid until the context is unpinned.
199 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200200 * This is what a descriptor looks like, from LSB to MSB::
201 *
Mika Kuoppala2355cf02017-01-27 15:03:09 +0200202 * bits 0-11: flags, GEN8_CTX_* (cached in ctx->desc_template)
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200203 * bits 12-31: LRCA, GTT address of (the HWSP of) this context
204 * bits 32-52: ctx ID, a globally unique tag
205 * bits 53-54: mbz, reserved for use by hardware
206 * bits 55-63: group ID, currently unused and set to 0
Daniele Ceraolo Spurioac52da62018-03-02 18:14:58 +0200207 *
208 * Starting from Gen11, the upper dword of the descriptor has a new format:
209 *
210 * bits 32-36: reserved
211 * bits 37-47: SW context ID
212 * bits 48:53: engine instance
213 * bit 54: mbz, reserved for use by hardware
214 * bits 55-60: SW counter
215 * bits 61-63: engine class
216 *
217 * engine info, SW context ID and SW counter need to form a unique number
218 * (Context ID) per lrc.
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000219 */
220static void
Chris Wilsone2efd132016-05-24 14:53:34 +0100221intel_lr_context_descriptor_update(struct i915_gem_context *ctx,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000222 struct intel_engine_cs *engine)
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000223{
Chris Wilson9021ad02016-05-24 14:53:37 +0100224 struct intel_context *ce = &ctx->engine[engine->id];
Chris Wilson7069b142016-04-28 09:56:52 +0100225 u64 desc;
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000226
Daniele Ceraolo Spurioac52da62018-03-02 18:14:58 +0200227 BUILD_BUG_ON(MAX_CONTEXT_HW_ID > (BIT(GEN8_CTX_ID_WIDTH)));
228 BUILD_BUG_ON(GEN11_MAX_CONTEXT_HW_ID > (BIT(GEN11_SW_CTX_ID_WIDTH)));
Chris Wilson7069b142016-04-28 09:56:52 +0100229
Mika Kuoppala2355cf02017-01-27 15:03:09 +0200230 desc = ctx->desc_template; /* bits 0-11 */
Daniele Ceraolo Spurioac52da62018-03-02 18:14:58 +0200231 GEM_BUG_ON(desc & GENMASK_ULL(63, 12));
232
Michel Thierry0b29c752017-09-13 09:56:00 +0100233 desc |= i915_ggtt_offset(ce->state) + LRC_HEADER_PAGES * PAGE_SIZE;
Chris Wilson9021ad02016-05-24 14:53:37 +0100234 /* bits 12-31 */
Daniele Ceraolo Spurioac52da62018-03-02 18:14:58 +0200235 GEM_BUG_ON(desc & GENMASK_ULL(63, 32));
236
237 if (INTEL_GEN(ctx->i915) >= 11) {
238 GEM_BUG_ON(ctx->hw_id >= BIT(GEN11_SW_CTX_ID_WIDTH));
239 desc |= (u64)ctx->hw_id << GEN11_SW_CTX_ID_SHIFT;
240 /* bits 37-47 */
241
242 desc |= (u64)engine->instance << GEN11_ENGINE_INSTANCE_SHIFT;
243 /* bits 48-53 */
244
245 /* TODO: decide what to do with SW counter (bits 55-60) */
246
247 desc |= (u64)engine->class << GEN11_ENGINE_CLASS_SHIFT;
248 /* bits 61-63 */
249 } else {
250 GEM_BUG_ON(ctx->hw_id >= BIT(GEN8_CTX_ID_WIDTH));
251 desc |= (u64)ctx->hw_id << GEN8_CTX_ID_SHIFT; /* bits 32-52 */
252 }
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000253
Chris Wilson9021ad02016-05-24 14:53:37 +0100254 ce->lrc_desc = desc;
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000255}
256
Chris Wilson27606fd2017-09-16 21:44:13 +0100257static struct i915_priolist *
258lookup_priolist(struct intel_engine_cs *engine,
259 struct i915_priotree *pt,
260 int prio)
Chris Wilson08dd3e12017-09-16 21:44:12 +0100261{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300262 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100263 struct i915_priolist *p;
264 struct rb_node **parent, *rb;
265 bool first = true;
266
Mika Kuoppalab620e872017-09-22 15:43:03 +0300267 if (unlikely(execlists->no_priolist))
Chris Wilson08dd3e12017-09-16 21:44:12 +0100268 prio = I915_PRIORITY_NORMAL;
269
270find_priolist:
271 /* most positive priority is scheduled first, equal priorities fifo */
272 rb = NULL;
Mika Kuoppalab620e872017-09-22 15:43:03 +0300273 parent = &execlists->queue.rb_node;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100274 while (*parent) {
275 rb = *parent;
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000276 p = to_priolist(rb);
Chris Wilson08dd3e12017-09-16 21:44:12 +0100277 if (prio > p->priority) {
278 parent = &rb->rb_left;
279 } else if (prio < p->priority) {
280 parent = &rb->rb_right;
281 first = false;
282 } else {
Chris Wilson27606fd2017-09-16 21:44:13 +0100283 return p;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100284 }
285 }
286
287 if (prio == I915_PRIORITY_NORMAL) {
Mika Kuoppalab620e872017-09-22 15:43:03 +0300288 p = &execlists->default_priolist;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100289 } else {
290 p = kmem_cache_alloc(engine->i915->priorities, GFP_ATOMIC);
291 /* Convert an allocation failure to a priority bump */
292 if (unlikely(!p)) {
293 prio = I915_PRIORITY_NORMAL; /* recurses just once */
294
295 /* To maintain ordering with all rendering, after an
296 * allocation failure we have to disable all scheduling.
297 * Requests will then be executed in fifo, and schedule
298 * will ensure that dependencies are emitted in fifo.
299 * There will be still some reordering with existing
300 * requests, so if userspace lied about their
301 * dependencies that reordering may be visible.
302 */
Mika Kuoppalab620e872017-09-22 15:43:03 +0300303 execlists->no_priolist = true;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100304 goto find_priolist;
305 }
306 }
307
308 p->priority = prio;
Chris Wilson27606fd2017-09-16 21:44:13 +0100309 INIT_LIST_HEAD(&p->requests);
Chris Wilson08dd3e12017-09-16 21:44:12 +0100310 rb_link_node(&p->node, rb, parent);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300311 rb_insert_color(&p->node, &execlists->queue);
Chris Wilson08dd3e12017-09-16 21:44:12 +0100312
Chris Wilson08dd3e12017-09-16 21:44:12 +0100313 if (first)
Mika Kuoppalab620e872017-09-22 15:43:03 +0300314 execlists->first = &p->node;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100315
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000316 return p;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100317}
318
Chris Wilsone61e0f52018-02-21 09:56:36 +0000319static void unwind_wa_tail(struct i915_request *rq)
Chris Wilson7e4992a2017-09-28 20:38:59 +0100320{
321 rq->tail = intel_ring_wrap(rq->ring, rq->wa_tail - WA_TAIL_BYTES);
322 assert_ring_tail_valid(rq->ring, rq->tail);
323}
324
Michał Winiarskia4598d12017-10-25 22:00:18 +0200325static void __unwind_incomplete_requests(struct intel_engine_cs *engine)
Chris Wilson7e4992a2017-09-28 20:38:59 +0100326{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000327 struct i915_request *rq, *rn;
Michał Winiarski097a9482017-09-28 20:39:01 +0100328 struct i915_priolist *uninitialized_var(p);
329 int last_prio = I915_PRIORITY_INVALID;
Chris Wilson7e4992a2017-09-28 20:38:59 +0100330
331 lockdep_assert_held(&engine->timeline->lock);
332
333 list_for_each_entry_safe_reverse(rq, rn,
334 &engine->timeline->requests,
335 link) {
Chris Wilsone61e0f52018-02-21 09:56:36 +0000336 if (i915_request_completed(rq))
Chris Wilson7e4992a2017-09-28 20:38:59 +0100337 return;
338
Chris Wilsone61e0f52018-02-21 09:56:36 +0000339 __i915_request_unsubmit(rq);
Chris Wilson7e4992a2017-09-28 20:38:59 +0100340 unwind_wa_tail(rq);
341
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000342 GEM_BUG_ON(rq_prio(rq) == I915_PRIORITY_INVALID);
343 if (rq_prio(rq) != last_prio) {
344 last_prio = rq_prio(rq);
345 p = lookup_priolist(engine, &rq->priotree, last_prio);
Michał Winiarski097a9482017-09-28 20:39:01 +0100346 }
347
348 list_add(&rq->priotree.link, &p->requests);
Chris Wilson7e4992a2017-09-28 20:38:59 +0100349 }
350}
351
Michał Winiarskic41937f2017-10-26 15:35:58 +0200352void
Michał Winiarskia4598d12017-10-25 22:00:18 +0200353execlists_unwind_incomplete_requests(struct intel_engine_execlists *execlists)
354{
355 struct intel_engine_cs *engine =
356 container_of(execlists, typeof(*engine), execlists);
357
358 spin_lock_irq(&engine->timeline->lock);
359 __unwind_incomplete_requests(engine);
360 spin_unlock_irq(&engine->timeline->lock);
361}
362
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100363static inline void
Chris Wilsone61e0f52018-02-21 09:56:36 +0000364execlists_context_status_change(struct i915_request *rq, unsigned long status)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100365{
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100366 /*
367 * Only used when GVT-g is enabled now. When GVT-g is disabled,
368 * The compiler should eliminate this function as dead-code.
369 */
370 if (!IS_ENABLED(CONFIG_DRM_I915_GVT))
371 return;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100372
Changbin Du3fc03062017-03-13 10:47:11 +0800373 atomic_notifier_call_chain(&rq->engine->context_status_notifier,
374 status, rq);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100375}
376
Tvrtko Ursulin73fd9d32017-11-21 18:18:47 +0000377static inline void
Chris Wilsone61e0f52018-02-21 09:56:36 +0000378execlists_context_schedule_in(struct i915_request *rq)
Tvrtko Ursulin73fd9d32017-11-21 18:18:47 +0000379{
380 execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_IN);
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +0000381 intel_engine_context_in(rq->engine);
Tvrtko Ursulin73fd9d32017-11-21 18:18:47 +0000382}
383
384static inline void
Chris Wilsone61e0f52018-02-21 09:56:36 +0000385execlists_context_schedule_out(struct i915_request *rq)
Tvrtko Ursulin73fd9d32017-11-21 18:18:47 +0000386{
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +0000387 intel_engine_context_out(rq->engine);
Tvrtko Ursulin73fd9d32017-11-21 18:18:47 +0000388 execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_OUT);
389}
390
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000391static void
392execlists_update_context_pdps(struct i915_hw_ppgtt *ppgtt, u32 *reg_state)
393{
394 ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
395 ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
396 ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
397 ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
398}
399
Chris Wilsone61e0f52018-02-21 09:56:36 +0000400static u64 execlists_update_context(struct i915_request *rq)
Oscar Mateoae1250b2014-07-24 17:04:37 +0100401{
Chris Wilson70c2a242016-09-09 14:11:46 +0100402 struct intel_context *ce = &rq->ctx->engine[rq->engine->id];
Zhi Wang04da8112017-02-06 18:37:16 +0800403 struct i915_hw_ppgtt *ppgtt =
404 rq->ctx->ppgtt ?: rq->i915->mm.aliasing_ppgtt;
Chris Wilson70c2a242016-09-09 14:11:46 +0100405 u32 *reg_state = ce->lrc_reg_state;
Oscar Mateoae1250b2014-07-24 17:04:37 +0100406
Chris Wilsone6ba9992017-04-25 14:00:49 +0100407 reg_state[CTX_RING_TAIL+1] = intel_ring_set_tail(rq->ring, rq->tail);
Oscar Mateoae1250b2014-07-24 17:04:37 +0100408
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000409 /* True 32b PPGTT with dynamic page allocation: update PDP
410 * registers and point the unallocated PDPs to scratch page.
411 * PML4 is allocated during ppgtt init, so this is not needed
412 * in 48-bit mode.
413 */
Chris Wilson949e8ab2017-02-09 14:40:36 +0000414 if (ppgtt && !i915_vm_is_48bit(&ppgtt->base))
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000415 execlists_update_context_pdps(ppgtt, reg_state);
Chris Wilson70c2a242016-09-09 14:11:46 +0100416
417 return ce->lrc_desc;
Oscar Mateoae1250b2014-07-24 17:04:37 +0100418}
419
Chris Wilsonbeecec92017-10-03 21:34:52 +0100420static inline void elsp_write(u64 desc, u32 __iomem *elsp)
421{
422 writel(upper_32_bits(desc), elsp);
423 writel(lower_32_bits(desc), elsp);
424}
425
Chris Wilson70c2a242016-09-09 14:11:46 +0100426static void execlists_submit_ports(struct intel_engine_cs *engine)
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100427{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300428 struct execlist_port *port = engine->execlists.port;
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100429 unsigned int n;
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100430
Mika Kuoppala76e70082017-09-22 15:43:07 +0300431 for (n = execlists_num_ports(&engine->execlists); n--; ) {
Chris Wilsone61e0f52018-02-21 09:56:36 +0000432 struct i915_request *rq;
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100433 unsigned int count;
434 u64 desc;
Chris Wilson70c2a242016-09-09 14:11:46 +0100435
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100436 rq = port_unpack(&port[n], &count);
437 if (rq) {
438 GEM_BUG_ON(count > !n);
439 if (!count++)
Tvrtko Ursulin73fd9d32017-11-21 18:18:47 +0000440 execlists_context_schedule_in(rq);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100441 port_set(&port[n], port_pack(rq, count));
442 desc = execlists_update_context(rq);
443 GEM_DEBUG_EXEC(port[n].context_id = upper_32_bits(desc));
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000444
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000445 GEM_TRACE("%s in[%d]: ctx=%d.%d, seqno=%x, prio=%d\n",
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000446 engine->name, n,
Chris Wilson16c86192017-12-19 22:09:16 +0000447 port[n].context_id, count,
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000448 rq->global_seqno,
449 rq_prio(rq));
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100450 } else {
451 GEM_BUG_ON(!n);
452 desc = 0;
453 }
454
Chris Wilson2fc7a062017-12-07 22:24:34 +0000455 elsp_write(desc, engine->execlists.elsp);
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100456 }
Michel Thierryba74cb12017-11-20 12:34:58 +0000457 execlists_clear_active(&engine->execlists, EXECLISTS_ACTIVE_HWACK);
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100458}
459
Chris Wilson70c2a242016-09-09 14:11:46 +0100460static bool ctx_single_port_submission(const struct i915_gem_context *ctx)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100461{
Chris Wilson70c2a242016-09-09 14:11:46 +0100462 return (IS_ENABLED(CONFIG_DRM_I915_GVT) &&
Chris Wilson60958682016-12-31 11:20:11 +0000463 i915_gem_context_force_single_submission(ctx));
Ben Widawsky84b790f2014-07-24 17:04:36 +0100464}
465
Chris Wilson70c2a242016-09-09 14:11:46 +0100466static bool can_merge_ctx(const struct i915_gem_context *prev,
467 const struct i915_gem_context *next)
Michel Thierryacdd8842014-07-24 17:04:38 +0100468{
Chris Wilson70c2a242016-09-09 14:11:46 +0100469 if (prev != next)
470 return false;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100471
Chris Wilson70c2a242016-09-09 14:11:46 +0100472 if (ctx_single_port_submission(prev))
473 return false;
Michel Thierryacdd8842014-07-24 17:04:38 +0100474
Chris Wilson70c2a242016-09-09 14:11:46 +0100475 return true;
476}
Peter Antoine779949f2015-05-11 16:03:27 +0100477
Chris Wilsone61e0f52018-02-21 09:56:36 +0000478static void port_assign(struct execlist_port *port, struct i915_request *rq)
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100479{
480 GEM_BUG_ON(rq == port_request(port));
481
482 if (port_isset(port))
Chris Wilsone61e0f52018-02-21 09:56:36 +0000483 i915_request_put(port_request(port));
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100484
Chris Wilsone61e0f52018-02-21 09:56:36 +0000485 port_set(port, port_pack(i915_request_get(rq), port_count(port)));
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100486}
487
Chris Wilsonbeecec92017-10-03 21:34:52 +0100488static void inject_preempt_context(struct intel_engine_cs *engine)
489{
490 struct intel_context *ce =
491 &engine->i915->preempt_context->engine[engine->id];
Chris Wilsonbeecec92017-10-03 21:34:52 +0100492 unsigned int n;
493
Chris Wilsond6376372018-02-07 21:05:44 +0000494 GEM_BUG_ON(engine->execlists.preempt_complete_status !=
495 upper_32_bits(ce->lrc_desc));
Chris Wilson09b1a4e2018-01-25 11:24:42 +0000496 GEM_BUG_ON((ce->lrc_reg_state[CTX_CONTEXT_CONTROL + 1] &
497 _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
498 CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT)) !=
499 _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
500 CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT));
501
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000502 /*
503 * Switch to our empty preempt context so
504 * the state of the GPU is known (idle).
505 */
Chris Wilson16a87392017-12-20 09:06:26 +0000506 GEM_TRACE("%s\n", engine->name);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100507 for (n = execlists_num_ports(&engine->execlists); --n; )
Chris Wilson2fc7a062017-12-07 22:24:34 +0000508 elsp_write(0, engine->execlists.elsp);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100509
Chris Wilson2fc7a062017-12-07 22:24:34 +0000510 elsp_write(ce->lrc_desc, engine->execlists.elsp);
Michel Thierryba74cb12017-11-20 12:34:58 +0000511 execlists_clear_active(&engine->execlists, EXECLISTS_ACTIVE_HWACK);
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000512 execlists_set_active(&engine->execlists, EXECLISTS_ACTIVE_PREEMPT);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100513}
514
Chris Wilson70c2a242016-09-09 14:11:46 +0100515static void execlists_dequeue(struct intel_engine_cs *engine)
516{
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300517 struct intel_engine_execlists * const execlists = &engine->execlists;
518 struct execlist_port *port = execlists->port;
Mika Kuoppala76e70082017-09-22 15:43:07 +0300519 const struct execlist_port * const last_port =
520 &execlists->port[execlists->port_mask];
Chris Wilsone61e0f52018-02-21 09:56:36 +0000521 struct i915_request *last = port_request(port);
Chris Wilson20311bd2016-11-14 20:41:03 +0000522 struct rb_node *rb;
Chris Wilson70c2a242016-09-09 14:11:46 +0100523 bool submit = false;
Michel Thierryacdd8842014-07-24 17:04:38 +0100524
Chris Wilson70c2a242016-09-09 14:11:46 +0100525 /* Hardware submission is through 2 ports. Conceptually each port
526 * has a (RING_START, RING_HEAD, RING_TAIL) tuple. RING_START is
527 * static for a context, and unique to each, so we only execute
528 * requests belonging to a single context from each ring. RING_HEAD
529 * is maintained by the CS in the context image, it marks the place
530 * where it got up to last time, and through RING_TAIL we tell the CS
531 * where we want to execute up to this time.
532 *
533 * In this list the requests are in order of execution. Consecutive
534 * requests from the same context are adjacent in the ringbuffer. We
535 * can combine these requests into a single RING_TAIL update:
536 *
537 * RING_HEAD...req1...req2
538 * ^- RING_TAIL
539 * since to execute req2 the CS must first execute req1.
540 *
541 * Our goal then is to point each port to the end of a consecutive
542 * sequence of requests as being the most optimal (fewest wake ups
543 * and context switches) submission.
544 */
545
Tvrtko Ursulin9f7886d2017-03-21 10:55:11 +0000546 spin_lock_irq(&engine->timeline->lock);
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300547 rb = execlists->first;
548 GEM_BUG_ON(rb_first(&execlists->queue) != rb);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100549
550 if (last) {
551 /*
552 * Don't resubmit or switch until all outstanding
553 * preemptions (lite-restore) are seen. Then we
554 * know the next preemption status we see corresponds
555 * to this ELSP update.
556 */
Michel Thierryba74cb12017-11-20 12:34:58 +0000557 GEM_BUG_ON(!port_count(&port[0]));
Chris Wilsonbeecec92017-10-03 21:34:52 +0100558 if (port_count(&port[0]) > 1)
559 goto unlock;
560
Michel Thierryba74cb12017-11-20 12:34:58 +0000561 /*
562 * If we write to ELSP a second time before the HW has had
563 * a chance to respond to the previous write, we can confuse
564 * the HW and hit "undefined behaviour". After writing to ELSP,
565 * we must then wait until we see a context-switch event from
566 * the HW to indicate that it has had a chance to respond.
567 */
568 if (!execlists_is_active(execlists, EXECLISTS_ACTIVE_HWACK))
569 goto unlock;
570
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000571 if (need_preempt(engine, last, execlists->queue_priority)) {
Chris Wilsonbeecec92017-10-03 21:34:52 +0100572 inject_preempt_context(engine);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100573 goto unlock;
Chris Wilsonbeecec92017-10-03 21:34:52 +0100574 }
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000575
576 /*
577 * In theory, we could coalesce more requests onto
578 * the second port (the first port is active, with
579 * no preemptions pending). However, that means we
580 * then have to deal with the possible lite-restore
581 * of the second port (as we submit the ELSP, there
582 * may be a context-switch) but also we may complete
583 * the resubmission before the context-switch. Ergo,
584 * coalescing onto the second port will cause a
585 * preemption event, but we cannot predict whether
586 * that will affect port[0] or port[1].
587 *
588 * If the second port is already active, we can wait
589 * until the next context-switch before contemplating
590 * new requests. The GPU will be busy and we should be
591 * able to resubmit the new ELSP before it idles,
592 * avoiding pipeline bubbles (momentary pauses where
593 * the driver is unable to keep up the supply of new
594 * work). However, we have to double check that the
595 * priorities of the ports haven't been switch.
596 */
597 if (port_count(&port[1]))
598 goto unlock;
599
600 /*
601 * WaIdleLiteRestore:bdw,skl
602 * Apply the wa NOOPs to prevent
603 * ring:HEAD == rq:TAIL as we resubmit the
604 * request. See gen8_emit_breadcrumb() for
605 * where we prepare the padding after the
606 * end of the request.
607 */
608 last->tail = last->wa_tail;
Chris Wilsonbeecec92017-10-03 21:34:52 +0100609 }
610
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000611 while (rb) {
612 struct i915_priolist *p = to_priolist(rb);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000613 struct i915_request *rq, *rn;
Chris Wilson20311bd2016-11-14 20:41:03 +0000614
Chris Wilson6c067572017-05-17 13:10:03 +0100615 list_for_each_entry_safe(rq, rn, &p->requests, priotree.link) {
616 /*
617 * Can we combine this request with the current port?
618 * It has to be the same context/ringbuffer and not
619 * have any exceptions (e.g. GVT saying never to
620 * combine contexts).
621 *
622 * If we can combine the requests, we can execute both
623 * by updating the RING_TAIL to point to the end of the
624 * second request, and so we never need to tell the
625 * hardware about the first.
Chris Wilson70c2a242016-09-09 14:11:46 +0100626 */
Chris Wilson6c067572017-05-17 13:10:03 +0100627 if (last && !can_merge_ctx(rq->ctx, last->ctx)) {
628 /*
629 * If we are on the second port and cannot
630 * combine this request with the last, then we
631 * are done.
632 */
Mika Kuoppala76e70082017-09-22 15:43:07 +0300633 if (port == last_port) {
Chris Wilson6c067572017-05-17 13:10:03 +0100634 __list_del_many(&p->requests,
635 &rq->priotree.link);
636 goto done;
637 }
Chris Wilson70c2a242016-09-09 14:11:46 +0100638
Chris Wilson6c067572017-05-17 13:10:03 +0100639 /*
640 * If GVT overrides us we only ever submit
641 * port[0], leaving port[1] empty. Note that we
642 * also have to be careful that we don't queue
643 * the same context (even though a different
644 * request) to the second port.
645 */
646 if (ctx_single_port_submission(last->ctx) ||
647 ctx_single_port_submission(rq->ctx)) {
648 __list_del_many(&p->requests,
649 &rq->priotree.link);
650 goto done;
651 }
Chris Wilson70c2a242016-09-09 14:11:46 +0100652
Chris Wilson6c067572017-05-17 13:10:03 +0100653 GEM_BUG_ON(last->ctx == rq->ctx);
Chris Wilson70c2a242016-09-09 14:11:46 +0100654
Chris Wilson6c067572017-05-17 13:10:03 +0100655 if (submit)
656 port_assign(port, last);
657 port++;
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300658
659 GEM_BUG_ON(port_isset(port));
Chris Wilson6c067572017-05-17 13:10:03 +0100660 }
661
662 INIT_LIST_HEAD(&rq->priotree.link);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000663 __i915_request_submit(rq);
664 trace_i915_request_in(rq, port_index(port, execlists));
Chris Wilson6c067572017-05-17 13:10:03 +0100665 last = rq;
666 submit = true;
Chris Wilson70c2a242016-09-09 14:11:46 +0100667 }
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000668
Chris Wilson20311bd2016-11-14 20:41:03 +0000669 rb = rb_next(rb);
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300670 rb_erase(&p->node, &execlists->queue);
Chris Wilson6c067572017-05-17 13:10:03 +0100671 INIT_LIST_HEAD(&p->requests);
672 if (p->priority != I915_PRIORITY_NORMAL)
Chris Wilsonc5cf9a92017-05-17 13:10:04 +0100673 kmem_cache_free(engine->i915->priorities, p);
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000674 }
Chris Wilson6c067572017-05-17 13:10:03 +0100675done:
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000676 execlists->queue_priority = rb ? to_priolist(rb)->priority : INT_MIN;
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300677 execlists->first = rb;
Chris Wilson6c067572017-05-17 13:10:03 +0100678 if (submit)
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100679 port_assign(port, last);
Chris Wilson339ccd32018-02-15 16:25:53 +0000680
681 /* We must always keep the beast fed if we have work piled up */
Chris Wilson339ccd32018-02-15 16:25:53 +0000682 GEM_BUG_ON(execlists->first && !port_isset(execlists->port));
683
Chris Wilsonbeecec92017-10-03 21:34:52 +0100684unlock:
Tvrtko Ursulin9f7886d2017-03-21 10:55:11 +0000685 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson70c2a242016-09-09 14:11:46 +0100686
Chris Wilson4a118ec2017-10-23 22:32:36 +0100687 if (submit) {
688 execlists_set_active(execlists, EXECLISTS_ACTIVE_USER);
Chris Wilson70c2a242016-09-09 14:11:46 +0100689 execlists_submit_ports(engine);
Chris Wilson4a118ec2017-10-23 22:32:36 +0100690 }
Chris Wilsond081e022018-02-16 15:32:10 +0000691
692 GEM_BUG_ON(port_isset(execlists->port) &&
693 !execlists_is_active(execlists, EXECLISTS_ACTIVE_USER));
Michel Thierryacdd8842014-07-24 17:04:38 +0100694}
695
Michał Winiarskic41937f2017-10-26 15:35:58 +0200696void
Michał Winiarskia4598d12017-10-25 22:00:18 +0200697execlists_cancel_port_requests(struct intel_engine_execlists * const execlists)
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300698{
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100699 struct execlist_port *port = execlists->port;
Mika Kuoppaladc2279e2017-10-10 14:48:57 +0300700 unsigned int num_ports = execlists_num_ports(execlists);
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300701
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100702 while (num_ports-- && port_isset(port)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +0000703 struct i915_request *rq = port_request(port);
Chris Wilson7e44fc22017-09-26 11:17:19 +0100704
Chris Wilson4a118ec2017-10-23 22:32:36 +0100705 GEM_BUG_ON(!execlists->active);
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +0000706 intel_engine_context_out(rq->engine);
Chris Wilsond6c05112017-10-03 21:34:47 +0100707 execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_PREEMPTED);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000708 i915_request_put(rq);
Chris Wilson7e44fc22017-09-26 11:17:19 +0100709
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100710 memset(port, 0, sizeof(*port));
711 port++;
712 }
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300713}
714
Chris Wilson27a5f612017-09-15 18:31:00 +0100715static void execlists_cancel_requests(struct intel_engine_cs *engine)
716{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300717 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilsone61e0f52018-02-21 09:56:36 +0000718 struct i915_request *rq, *rn;
Chris Wilson27a5f612017-09-15 18:31:00 +0100719 struct rb_node *rb;
720 unsigned long flags;
Chris Wilson27a5f612017-09-15 18:31:00 +0100721
Chris Wilson963ddd62018-03-02 11:33:24 +0000722 GEM_TRACE("%s\n", engine->name);
723
Chris Wilsona3e38832018-03-02 14:32:45 +0000724 /*
725 * Before we call engine->cancel_requests(), we should have exclusive
726 * access to the submission state. This is arranged for us by the
727 * caller disabling the interrupt generation, the tasklet and other
728 * threads that may then access the same state, giving us a free hand
729 * to reset state. However, we still need to let lockdep be aware that
730 * we know this state may be accessed in hardirq context, so we
731 * disable the irq around this manipulation and we want to keep
732 * the spinlock focused on its duties and not accidentally conflate
733 * coverage to the submission's irq state. (Similarly, although we
734 * shouldn't need to disable irq around the manipulation of the
735 * submission's irq state, we also wish to remind ourselves that
736 * it is irq state.)
737 */
738 local_irq_save(flags);
Chris Wilson27a5f612017-09-15 18:31:00 +0100739
740 /* Cancel the requests on the HW and clear the ELSP tracker. */
Michał Winiarskia4598d12017-10-25 22:00:18 +0200741 execlists_cancel_port_requests(execlists);
Chris Wilson27a5f612017-09-15 18:31:00 +0100742
Chris Wilsona3e38832018-03-02 14:32:45 +0000743 spin_lock(&engine->timeline->lock);
744
Chris Wilson27a5f612017-09-15 18:31:00 +0100745 /* Mark all executing requests as skipped. */
746 list_for_each_entry(rq, &engine->timeline->requests, link) {
747 GEM_BUG_ON(!rq->global_seqno);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000748 if (!i915_request_completed(rq))
Chris Wilson27a5f612017-09-15 18:31:00 +0100749 dma_fence_set_error(&rq->fence, -EIO);
750 }
751
752 /* Flush the queued requests to the timeline list (for retiring). */
Mika Kuoppalab620e872017-09-22 15:43:03 +0300753 rb = execlists->first;
Chris Wilson27a5f612017-09-15 18:31:00 +0100754 while (rb) {
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000755 struct i915_priolist *p = to_priolist(rb);
Chris Wilson27a5f612017-09-15 18:31:00 +0100756
757 list_for_each_entry_safe(rq, rn, &p->requests, priotree.link) {
758 INIT_LIST_HEAD(&rq->priotree.link);
Chris Wilson27a5f612017-09-15 18:31:00 +0100759
760 dma_fence_set_error(&rq->fence, -EIO);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000761 __i915_request_submit(rq);
Chris Wilson27a5f612017-09-15 18:31:00 +0100762 }
763
764 rb = rb_next(rb);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300765 rb_erase(&p->node, &execlists->queue);
Chris Wilson27a5f612017-09-15 18:31:00 +0100766 INIT_LIST_HEAD(&p->requests);
767 if (p->priority != I915_PRIORITY_NORMAL)
768 kmem_cache_free(engine->i915->priorities, p);
769 }
770
771 /* Remaining _unready_ requests will be nop'ed when submitted */
772
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000773 execlists->queue_priority = INT_MIN;
Mika Kuoppalab620e872017-09-22 15:43:03 +0300774 execlists->queue = RB_ROOT;
775 execlists->first = NULL;
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100776 GEM_BUG_ON(port_isset(execlists->port));
Chris Wilson27a5f612017-09-15 18:31:00 +0100777
Chris Wilsona3e38832018-03-02 14:32:45 +0000778 spin_unlock(&engine->timeline->lock);
779
Chris Wilson27a5f612017-09-15 18:31:00 +0100780 /*
781 * The port is checked prior to scheduling a tasklet, but
782 * just in case we have suspended the tasklet to do the
783 * wedging make sure that when it wakes, it decides there
784 * is no work to do by clearing the irq_posted bit.
785 */
786 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
787
Chris Wilson963ddd62018-03-02 11:33:24 +0000788 /* Mark all CS interrupts as complete */
789 execlists->active = 0;
790
Chris Wilsona3e38832018-03-02 14:32:45 +0000791 local_irq_restore(flags);
Chris Wilson27a5f612017-09-15 18:31:00 +0100792}
793
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200794/*
Oscar Mateo73e4d072014-07-24 17:04:48 +0100795 * Check the unread Context Status Buffers and manage the submission of new
796 * contexts to the ELSP accordingly.
797 */
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +0530798static void execlists_submission_tasklet(unsigned long data)
Thomas Daniele981e7b2014-07-24 17:04:39 +0100799{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300800 struct intel_engine_cs * const engine = (struct intel_engine_cs *)data;
801 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilsonbeecec92017-10-03 21:34:52 +0100802 struct execlist_port * const port = execlists->port;
Chris Wilsonc0336662016-05-06 15:40:21 +0100803 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonbb5db7e2018-01-22 10:07:14 +0000804 bool fw = false;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100805
Chris Wilson48921262017-04-11 18:58:50 +0100806 /* We can skip acquiring intel_runtime_pm_get() here as it was taken
807 * on our behalf by the request (see i915_gem_mark_busy()) and it will
808 * not be relinquished until the device is idle (see
809 * i915_gem_idle_work_handler()). As a precaution, we make sure
810 * that all ELSP are drained i.e. we have processed the CSB,
811 * before allowing ourselves to idle and calling intel_runtime_pm_put().
812 */
813 GEM_BUG_ON(!dev_priv->gt.awake);
814
Chris Wilson899f6202017-03-21 11:33:20 +0000815 /* Prefer doing test_and_clear_bit() as a two stage operation to avoid
816 * imposing the cost of a locked atomic transaction when submitting a
817 * new request (outside of the context-switch interrupt).
818 */
819 while (test_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted)) {
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100820 /* The HWSP contains a (cacheable) mirror of the CSB */
821 const u32 *buf =
822 &engine->status_page.page_addr[I915_HWS_CSB_BUF0_INDEX];
Chris Wilson4af0d722017-03-25 20:10:53 +0000823 unsigned int head, tail;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100824
Mika Kuoppalab620e872017-09-22 15:43:03 +0300825 if (unlikely(execlists->csb_use_mmio)) {
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100826 buf = (u32 * __force)
827 (dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_BUF_LO(engine, 0)));
Mika Kuoppalab620e872017-09-22 15:43:03 +0300828 execlists->csb_head = -1; /* force mmio read of CSB ptrs */
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100829 }
830
Chris Wilson2e70b8c2017-03-23 13:48:03 +0000831 /* The write will be ordered by the uncached read (itself
832 * a memory barrier), so we do not need another in the form
833 * of a locked instruction. The race between the interrupt
834 * handler and the split test/clear is harmless as we order
835 * our clear before the CSB read. If the interrupt arrived
836 * first between the test and the clear, we read the updated
837 * CSB and clear the bit. If the interrupt arrives as we read
838 * the CSB or later (i.e. after we had cleared the bit) the bit
839 * is set and we do a new loop.
840 */
841 __clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300842 if (unlikely(execlists->csb_head == -1)) { /* following a reset */
Chris Wilsonbb5db7e2018-01-22 10:07:14 +0000843 if (!fw) {
844 intel_uncore_forcewake_get(dev_priv,
845 execlists->fw_domains);
846 fw = true;
847 }
848
Chris Wilson767a9832017-09-13 09:56:05 +0100849 head = readl(dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)));
850 tail = GEN8_CSB_WRITE_PTR(head);
851 head = GEN8_CSB_READ_PTR(head);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300852 execlists->csb_head = head;
Chris Wilson767a9832017-09-13 09:56:05 +0100853 } else {
854 const int write_idx =
855 intel_hws_csb_write_index(dev_priv) -
856 I915_HWS_CSB_BUF0_INDEX;
857
Mika Kuoppalab620e872017-09-22 15:43:03 +0300858 head = execlists->csb_head;
Chris Wilson767a9832017-09-13 09:56:05 +0100859 tail = READ_ONCE(buf[write_idx]);
860 }
Chris Wilsonbb5db7e2018-01-22 10:07:14 +0000861 GEM_TRACE("%s cs-irq head=%d [%d%s], tail=%d [%d%s]\n",
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000862 engine->name,
Chris Wilsonbb5db7e2018-01-22 10:07:14 +0000863 head, GEN8_CSB_READ_PTR(readl(dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)))), fw ? "" : "?",
864 tail, GEN8_CSB_WRITE_PTR(readl(dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)))), fw ? "" : "?");
Mika Kuoppalab620e872017-09-22 15:43:03 +0300865
Chris Wilson4af0d722017-03-25 20:10:53 +0000866 while (head != tail) {
Chris Wilsone61e0f52018-02-21 09:56:36 +0000867 struct i915_request *rq;
Chris Wilson4af0d722017-03-25 20:10:53 +0000868 unsigned int status;
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100869 unsigned int count;
Chris Wilsona37951a2017-01-24 11:00:06 +0000870
Chris Wilson4af0d722017-03-25 20:10:53 +0000871 if (++head == GEN8_CSB_ENTRIES)
872 head = 0;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100873
Chris Wilson2ffe80a2017-02-06 17:05:02 +0000874 /* We are flying near dragons again.
875 *
876 * We hold a reference to the request in execlist_port[]
877 * but no more than that. We are operating in softirq
878 * context and so cannot hold any mutex or sleep. That
879 * prevents us stopping the requests we are processing
880 * in port[] from being retired simultaneously (the
881 * breadcrumb will be complete before we see the
882 * context-switch). As we only hold the reference to the
883 * request, any pointer chasing underneath the request
884 * is subject to a potential use-after-free. Thus we
885 * store all of the bookkeeping within port[] as
886 * required, and avoid using unguarded pointers beneath
887 * request itself. The same applies to the atomic
888 * status notifier.
889 */
890
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100891 status = READ_ONCE(buf[2 * head]); /* maybe mmio! */
Chris Wilson193a98d2017-12-22 13:27:42 +0000892 GEM_TRACE("%s csb[%d]: status=0x%08x:0x%08x, active=0x%x\n",
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000893 engine->name, head,
Chris Wilson193a98d2017-12-22 13:27:42 +0000894 status, buf[2*head + 1],
895 execlists->active);
Michel Thierryba74cb12017-11-20 12:34:58 +0000896
897 if (status & (GEN8_CTX_STATUS_IDLE_ACTIVE |
898 GEN8_CTX_STATUS_PREEMPTED))
899 execlists_set_active(execlists,
900 EXECLISTS_ACTIVE_HWACK);
901 if (status & GEN8_CTX_STATUS_ACTIVE_IDLE)
902 execlists_clear_active(execlists,
903 EXECLISTS_ACTIVE_HWACK);
904
Chris Wilson70c2a242016-09-09 14:11:46 +0100905 if (!(status & GEN8_CTX_STATUS_COMPLETED_MASK))
906 continue;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100907
Chris Wilson1f5f9ed2017-11-20 12:34:57 +0000908 /* We should never get a COMPLETED | IDLE_ACTIVE! */
909 GEM_BUG_ON(status & GEN8_CTX_STATUS_IDLE_ACTIVE);
910
Chris Wilsone40dd222017-11-20 12:34:55 +0000911 if (status & GEN8_CTX_STATUS_COMPLETE &&
Chris Wilsond6376372018-02-07 21:05:44 +0000912 buf[2*head + 1] == execlists->preempt_complete_status) {
Chris Wilson193a98d2017-12-22 13:27:42 +0000913 GEM_TRACE("%s preempt-idle\n", engine->name);
914
Michał Winiarskia4598d12017-10-25 22:00:18 +0200915 execlists_cancel_port_requests(execlists);
916 execlists_unwind_incomplete_requests(execlists);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100917
Chris Wilson4a118ec2017-10-23 22:32:36 +0100918 GEM_BUG_ON(!execlists_is_active(execlists,
919 EXECLISTS_ACTIVE_PREEMPT));
920 execlists_clear_active(execlists,
921 EXECLISTS_ACTIVE_PREEMPT);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100922 continue;
923 }
924
925 if (status & GEN8_CTX_STATUS_PREEMPTED &&
Chris Wilson4a118ec2017-10-23 22:32:36 +0100926 execlists_is_active(execlists,
927 EXECLISTS_ACTIVE_PREEMPT))
Chris Wilsonbeecec92017-10-03 21:34:52 +0100928 continue;
929
Chris Wilson4a118ec2017-10-23 22:32:36 +0100930 GEM_BUG_ON(!execlists_is_active(execlists,
931 EXECLISTS_ACTIVE_USER));
932
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100933 rq = port_unpack(port, &count);
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000934 GEM_TRACE("%s out[0]: ctx=%d.%d, seqno=%x, prio=%d\n",
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000935 engine->name,
Chris Wilson16c86192017-12-19 22:09:16 +0000936 port->context_id, count,
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000937 rq ? rq->global_seqno : 0,
938 rq ? rq_prio(rq) : 0);
Chris Wilsone0840392018-02-21 15:23:01 +0000939
940 /* Check the context/desc id for this event matches */
941 GEM_DEBUG_BUG_ON(buf[2 * head + 1] != port->context_id);
942
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100943 GEM_BUG_ON(count == 0);
944 if (--count == 0) {
Chris Wilson70c2a242016-09-09 14:11:46 +0100945 GEM_BUG_ON(status & GEN8_CTX_STATUS_PREEMPTED);
Chris Wilsond8747af2017-11-20 12:34:56 +0000946 GEM_BUG_ON(port_isset(&port[1]) &&
947 !(status & GEN8_CTX_STATUS_ELEMENT_SWITCH));
Chris Wilsone61e0f52018-02-21 09:56:36 +0000948 GEM_BUG_ON(!i915_request_completed(rq));
Tvrtko Ursulin73fd9d32017-11-21 18:18:47 +0000949 execlists_context_schedule_out(rq);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000950 trace_i915_request_out(rq);
951 i915_request_put(rq);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100952
Chris Wilson65cb8c02018-02-21 15:15:53 +0000953 GEM_TRACE("%s completed ctx=%d\n",
954 engine->name, port->context_id);
955
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300956 execlists_port_complete(execlists, port);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100957 } else {
958 port_set(port, port_pack(rq, count));
Chris Wilson70c2a242016-09-09 14:11:46 +0100959 }
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000960
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100961 /* After the final element, the hw should be idle */
962 GEM_BUG_ON(port_count(port) == 0 &&
Chris Wilson70c2a242016-09-09 14:11:46 +0100963 !(status & GEN8_CTX_STATUS_ACTIVE_IDLE));
Chris Wilson4a118ec2017-10-23 22:32:36 +0100964 if (port_count(port) == 0)
965 execlists_clear_active(execlists,
966 EXECLISTS_ACTIVE_USER);
Chris Wilson4af0d722017-03-25 20:10:53 +0000967 }
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000968
Mika Kuoppalab620e872017-09-22 15:43:03 +0300969 if (head != execlists->csb_head) {
970 execlists->csb_head = head;
Chris Wilson767a9832017-09-13 09:56:05 +0100971 writel(_MASKED_FIELD(GEN8_CSB_READ_PTR_MASK, head << 8),
972 dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)));
973 }
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000974 }
975
Chris Wilson4a118ec2017-10-23 22:32:36 +0100976 if (!execlists_is_active(execlists, EXECLISTS_ACTIVE_PREEMPT))
Chris Wilson70c2a242016-09-09 14:11:46 +0100977 execlists_dequeue(engine);
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000978
Chris Wilsonbb5db7e2018-01-22 10:07:14 +0000979 if (fw)
980 intel_uncore_forcewake_put(dev_priv, execlists->fw_domains);
Thomas Daniele981e7b2014-07-24 17:04:39 +0100981}
982
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000983static void queue_request(struct intel_engine_cs *engine,
984 struct i915_priotree *pt,
985 int prio)
Chris Wilson27606fd2017-09-16 21:44:13 +0100986{
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000987 list_add_tail(&pt->link, &lookup_priolist(engine, pt, prio)->requests);
988}
Chris Wilson27606fd2017-09-16 21:44:13 +0100989
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000990static void submit_queue(struct intel_engine_cs *engine, int prio)
991{
992 if (prio > engine->execlists.queue_priority) {
993 engine->execlists.queue_priority = prio;
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +0530994 tasklet_hi_schedule(&engine->execlists.tasklet);
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000995 }
Chris Wilson27606fd2017-09-16 21:44:13 +0100996}
997
Chris Wilsone61e0f52018-02-21 09:56:36 +0000998static void execlists_submit_request(struct i915_request *request)
Michel Thierryacdd8842014-07-24 17:04:38 +0100999{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001000 struct intel_engine_cs *engine = request->engine;
Chris Wilson5590af32016-09-09 14:11:54 +01001001 unsigned long flags;
Michel Thierryacdd8842014-07-24 17:04:38 +01001002
Chris Wilson663f71e2016-11-14 20:41:00 +00001003 /* Will be called from irq-context when using foreign fences. */
1004 spin_lock_irqsave(&engine->timeline->lock, flags);
Michel Thierryacdd8842014-07-24 17:04:38 +01001005
Chris Wilsonf6322ed2018-02-22 14:22:29 +00001006 queue_request(engine, &request->priotree, rq_prio(request));
1007 submit_queue(engine, rq_prio(request));
Michel Thierryacdd8842014-07-24 17:04:38 +01001008
Mika Kuoppalab620e872017-09-22 15:43:03 +03001009 GEM_BUG_ON(!engine->execlists.first);
Chris Wilson6c067572017-05-17 13:10:03 +01001010 GEM_BUG_ON(list_empty(&request->priotree.link));
1011
Chris Wilson663f71e2016-11-14 20:41:00 +00001012 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Michel Thierryacdd8842014-07-24 17:04:38 +01001013}
1014
Chris Wilsone61e0f52018-02-21 09:56:36 +00001015static struct i915_request *pt_to_request(struct i915_priotree *pt)
Chris Wilson1f181222017-10-03 21:34:50 +01001016{
Chris Wilsone61e0f52018-02-21 09:56:36 +00001017 return container_of(pt, struct i915_request, priotree);
Chris Wilson1f181222017-10-03 21:34:50 +01001018}
1019
Chris Wilson20311bd2016-11-14 20:41:03 +00001020static struct intel_engine_cs *
1021pt_lock_engine(struct i915_priotree *pt, struct intel_engine_cs *locked)
1022{
Chris Wilson1f181222017-10-03 21:34:50 +01001023 struct intel_engine_cs *engine = pt_to_request(pt)->engine;
Chris Wilson20311bd2016-11-14 20:41:03 +00001024
Chris Wilsona79a5242017-03-27 21:21:43 +01001025 GEM_BUG_ON(!locked);
1026
Chris Wilson20311bd2016-11-14 20:41:03 +00001027 if (engine != locked) {
Chris Wilsona79a5242017-03-27 21:21:43 +01001028 spin_unlock(&locked->timeline->lock);
1029 spin_lock(&engine->timeline->lock);
Chris Wilson20311bd2016-11-14 20:41:03 +00001030 }
1031
1032 return engine;
1033}
1034
Chris Wilsone61e0f52018-02-21 09:56:36 +00001035static void execlists_schedule(struct i915_request *request, int prio)
Chris Wilson20311bd2016-11-14 20:41:03 +00001036{
Chris Wilsona79a5242017-03-27 21:21:43 +01001037 struct intel_engine_cs *engine;
Chris Wilson20311bd2016-11-14 20:41:03 +00001038 struct i915_dependency *dep, *p;
1039 struct i915_dependency stack;
1040 LIST_HEAD(dfs);
1041
Chris Wilson7d1ea602017-09-28 20:39:00 +01001042 GEM_BUG_ON(prio == I915_PRIORITY_INVALID);
1043
Chris Wilsone61e0f52018-02-21 09:56:36 +00001044 if (i915_request_completed(request))
Chris Wilsonc218ee02018-01-06 10:56:18 +00001045 return;
1046
Chris Wilson20311bd2016-11-14 20:41:03 +00001047 if (prio <= READ_ONCE(request->priotree.priority))
1048 return;
1049
Chris Wilson70cd1472016-11-28 14:36:49 +00001050 /* Need BKL in order to use the temporary link inside i915_dependency */
1051 lockdep_assert_held(&request->i915->drm.struct_mutex);
Chris Wilson20311bd2016-11-14 20:41:03 +00001052
1053 stack.signaler = &request->priotree;
1054 list_add(&stack.dfs_link, &dfs);
1055
Chris Wilsonce01b172018-01-02 15:12:26 +00001056 /*
1057 * Recursively bump all dependent priorities to match the new request.
Chris Wilson20311bd2016-11-14 20:41:03 +00001058 *
1059 * A naive approach would be to use recursion:
1060 * static void update_priorities(struct i915_priotree *pt, prio) {
1061 * list_for_each_entry(dep, &pt->signalers_list, signal_link)
1062 * update_priorities(dep->signal, prio)
Chris Wilsonf6322ed2018-02-22 14:22:29 +00001063 * queue_request(pt);
Chris Wilson20311bd2016-11-14 20:41:03 +00001064 * }
1065 * but that may have unlimited recursion depth and so runs a very
1066 * real risk of overunning the kernel stack. Instead, we build
1067 * a flat list of all dependencies starting with the current request.
1068 * As we walk the list of dependencies, we add all of its dependencies
1069 * to the end of the list (this may include an already visited
1070 * request) and continue to walk onwards onto the new dependencies. The
1071 * end result is a topological list of requests in reverse order, the
1072 * last element in the list is the request we must execute first.
1073 */
Chris Wilson2221c5b2018-01-02 15:12:27 +00001074 list_for_each_entry(dep, &dfs, dfs_link) {
Chris Wilson20311bd2016-11-14 20:41:03 +00001075 struct i915_priotree *pt = dep->signaler;
1076
Chris Wilsonce01b172018-01-02 15:12:26 +00001077 /*
1078 * Within an engine, there can be no cycle, but we may
Chris Wilsona79a5242017-03-27 21:21:43 +01001079 * refer to the same dependency chain multiple times
1080 * (redundant dependencies are not eliminated) and across
1081 * engines.
1082 */
1083 list_for_each_entry(p, &pt->signalers_list, signal_link) {
Chris Wilsonce01b172018-01-02 15:12:26 +00001084 GEM_BUG_ON(p == dep); /* no cycles! */
1085
Chris Wilson83cc84c2018-01-02 15:12:25 +00001086 if (i915_priotree_signaled(p->signaler))
Chris Wilson1f181222017-10-03 21:34:50 +01001087 continue;
1088
Chris Wilsona79a5242017-03-27 21:21:43 +01001089 GEM_BUG_ON(p->signaler->priority < pt->priority);
Chris Wilson20311bd2016-11-14 20:41:03 +00001090 if (prio > READ_ONCE(p->signaler->priority))
1091 list_move_tail(&p->dfs_link, &dfs);
Chris Wilsona79a5242017-03-27 21:21:43 +01001092 }
Chris Wilson20311bd2016-11-14 20:41:03 +00001093 }
1094
Chris Wilsonce01b172018-01-02 15:12:26 +00001095 /*
1096 * If we didn't need to bump any existing priorities, and we haven't
Chris Wilson349bdb62017-05-17 13:10:05 +01001097 * yet submitted this request (i.e. there is no potential race with
1098 * execlists_submit_request()), we can set our own priority and skip
1099 * acquiring the engine locks.
1100 */
Chris Wilson7d1ea602017-09-28 20:39:00 +01001101 if (request->priotree.priority == I915_PRIORITY_INVALID) {
Chris Wilson349bdb62017-05-17 13:10:05 +01001102 GEM_BUG_ON(!list_empty(&request->priotree.link));
1103 request->priotree.priority = prio;
1104 if (stack.dfs_link.next == stack.dfs_link.prev)
1105 return;
1106 __list_del_entry(&stack.dfs_link);
1107 }
1108
Chris Wilsona79a5242017-03-27 21:21:43 +01001109 engine = request->engine;
1110 spin_lock_irq(&engine->timeline->lock);
1111
Chris Wilson20311bd2016-11-14 20:41:03 +00001112 /* Fifo and depth-first replacement ensure our deps execute before us */
1113 list_for_each_entry_safe_reverse(dep, p, &dfs, dfs_link) {
1114 struct i915_priotree *pt = dep->signaler;
1115
1116 INIT_LIST_HEAD(&dep->dfs_link);
1117
1118 engine = pt_lock_engine(pt, engine);
1119
1120 if (prio <= pt->priority)
1121 continue;
1122
Chris Wilson20311bd2016-11-14 20:41:03 +00001123 pt->priority = prio;
Chris Wilson6c067572017-05-17 13:10:03 +01001124 if (!list_empty(&pt->link)) {
1125 __list_del_entry(&pt->link);
Chris Wilsonf6322ed2018-02-22 14:22:29 +00001126 queue_request(engine, pt, prio);
Chris Wilsona79a5242017-03-27 21:21:43 +01001127 }
Chris Wilsonf6322ed2018-02-22 14:22:29 +00001128 submit_queue(engine, prio);
Chris Wilson20311bd2016-11-14 20:41:03 +00001129 }
1130
Chris Wilsona79a5242017-03-27 21:21:43 +01001131 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson20311bd2016-11-14 20:41:03 +00001132}
1133
Chris Wilsonf4e15af2017-11-10 14:26:32 +00001134static int __context_pin(struct i915_gem_context *ctx, struct i915_vma *vma)
1135{
1136 unsigned int flags;
1137 int err;
1138
1139 /*
1140 * Clear this page out of any CPU caches for coherent swap-in/out.
1141 * We only want to do this on the first bind so that we do not stall
1142 * on an active context (which by nature is already on the GPU).
1143 */
1144 if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
1145 err = i915_gem_object_set_to_gtt_domain(vma->obj, true);
1146 if (err)
1147 return err;
1148 }
1149
1150 flags = PIN_GLOBAL | PIN_HIGH;
1151 if (ctx->ggtt_offset_bias)
1152 flags |= PIN_OFFSET_BIAS | ctx->ggtt_offset_bias;
1153
1154 return i915_vma_pin(vma, 0, GEN8_LR_CONTEXT_ALIGN, flags);
1155}
1156
Chris Wilson266a2402017-05-04 10:33:08 +01001157static struct intel_ring *
1158execlists_context_pin(struct intel_engine_cs *engine,
1159 struct i915_gem_context *ctx)
Oscar Mateodcb4c122014-11-13 10:28:10 +00001160{
Chris Wilson9021ad02016-05-24 14:53:37 +01001161 struct intel_context *ce = &ctx->engine[engine->id];
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001162 void *vaddr;
Tvrtko Ursulinca825802016-01-15 15:10:27 +00001163 int ret;
Oscar Mateodcb4c122014-11-13 10:28:10 +00001164
Chris Wilson91c8a322016-07-05 10:40:23 +01001165 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Tvrtko Ursulinca825802016-01-15 15:10:27 +00001166
Chris Wilson266a2402017-05-04 10:33:08 +01001167 if (likely(ce->pin_count++))
1168 goto out;
Chris Wilsona533b4b2017-03-16 17:16:28 +00001169 GEM_BUG_ON(!ce->pin_count); /* no overflow please! */
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001170
Chris Wilson1d2a19c2018-01-26 12:18:46 +00001171 ret = execlists_context_deferred_alloc(ctx, engine);
1172 if (ret)
1173 goto err;
Chris Wilson56f6e0a2017-01-05 15:30:20 +00001174 GEM_BUG_ON(!ce->state);
Chris Wilsone8a9c582016-12-18 15:37:20 +00001175
Chris Wilsonf4e15af2017-11-10 14:26:32 +00001176 ret = __context_pin(ctx, ce->state);
Nick Hoathe84fe802015-09-11 12:53:46 +01001177 if (ret)
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001178 goto err;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001179
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001180 vaddr = i915_gem_object_pin_map(ce->state->obj, I915_MAP_WB);
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001181 if (IS_ERR(vaddr)) {
1182 ret = PTR_ERR(vaddr);
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001183 goto unpin_vma;
Tvrtko Ursulin82352e92016-01-15 17:12:45 +00001184 }
1185
Chris Wilsond822bb12017-04-03 12:34:25 +01001186 ret = intel_ring_pin(ce->ring, ctx->i915, ctx->ggtt_offset_bias);
Nick Hoathe84fe802015-09-11 12:53:46 +01001187 if (ret)
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001188 goto unpin_map;
Alex Daid1675192015-08-12 15:43:43 +01001189
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001190 intel_lr_context_descriptor_update(ctx, engine);
Chris Wilson9021ad02016-05-24 14:53:37 +01001191
Chris Wilsona3aabe82016-10-04 21:11:26 +01001192 ce->lrc_reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
1193 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001194 i915_ggtt_offset(ce->ring->vma);
Chris Wilsona3aabe82016-10-04 21:11:26 +01001195
Chris Wilson3d574a62017-10-13 21:26:16 +01001196 ce->state->obj->pin_global++;
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001197 i915_gem_context_get(ctx);
Chris Wilson266a2402017-05-04 10:33:08 +01001198out:
1199 return ce->ring;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001200
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001201unpin_map:
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001202 i915_gem_object_unpin_map(ce->state->obj);
1203unpin_vma:
1204 __i915_vma_unpin(ce->state);
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001205err:
Chris Wilson9021ad02016-05-24 14:53:37 +01001206 ce->pin_count = 0;
Chris Wilson266a2402017-05-04 10:33:08 +01001207 return ERR_PTR(ret);
Oscar Mateodcb4c122014-11-13 10:28:10 +00001208}
1209
Chris Wilsone8a9c582016-12-18 15:37:20 +00001210static void execlists_context_unpin(struct intel_engine_cs *engine,
1211 struct i915_gem_context *ctx)
Oscar Mateodcb4c122014-11-13 10:28:10 +00001212{
Chris Wilson9021ad02016-05-24 14:53:37 +01001213 struct intel_context *ce = &ctx->engine[engine->id];
Daniel Vetteraf3302b2015-12-04 17:27:15 +01001214
Chris Wilson91c8a322016-07-05 10:40:23 +01001215 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilson9021ad02016-05-24 14:53:37 +01001216 GEM_BUG_ON(ce->pin_count == 0);
Tvrtko Ursulin321fe302016-01-28 10:29:55 +00001217
Chris Wilson9021ad02016-05-24 14:53:37 +01001218 if (--ce->pin_count)
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001219 return;
1220
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001221 intel_ring_unpin(ce->ring);
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001222
Chris Wilson3d574a62017-10-13 21:26:16 +01001223 ce->state->obj->pin_global--;
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001224 i915_gem_object_unpin_map(ce->state->obj);
1225 i915_vma_unpin(ce->state);
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001226
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001227 i915_gem_context_put(ctx);
Oscar Mateodcb4c122014-11-13 10:28:10 +00001228}
1229
Chris Wilsone61e0f52018-02-21 09:56:36 +00001230static int execlists_request_alloc(struct i915_request *request)
Chris Wilsonef11c012016-12-18 15:37:19 +00001231{
1232 struct intel_engine_cs *engine = request->engine;
1233 struct intel_context *ce = &request->ctx->engine[engine->id];
Chris Wilsonfd138212017-11-15 15:12:04 +00001234 int ret;
Chris Wilsonef11c012016-12-18 15:37:19 +00001235
Chris Wilsone8a9c582016-12-18 15:37:20 +00001236 GEM_BUG_ON(!ce->pin_count);
1237
Chris Wilsonef11c012016-12-18 15:37:19 +00001238 /* Flush enough space to reduce the likelihood of waiting after
1239 * we start building the request - in which case we will just
1240 * have to repeat work.
1241 */
1242 request->reserved_space += EXECLISTS_REQUEST_SIZE;
1243
Chris Wilsonfd138212017-11-15 15:12:04 +00001244 ret = intel_ring_wait_for_space(request->ring, request->reserved_space);
1245 if (ret)
1246 return ret;
Chris Wilsonef11c012016-12-18 15:37:19 +00001247
Chris Wilsonef11c012016-12-18 15:37:19 +00001248 /* Note that after this point, we have committed to using
1249 * this request as it is being used to both track the
1250 * state of engine initialisation and liveness of the
1251 * golden renderstate above. Think twice before you try
1252 * to cancel/unwind this request now.
1253 */
1254
1255 request->reserved_space -= EXECLISTS_REQUEST_SIZE;
1256 return 0;
Chris Wilsonef11c012016-12-18 15:37:19 +00001257}
1258
Arun Siluvery9e000842015-07-03 14:27:31 +01001259/*
1260 * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
1261 * PIPE_CONTROL instruction. This is required for the flush to happen correctly
1262 * but there is a slight complication as this is applied in WA batch where the
1263 * values are only initialized once so we cannot take register value at the
1264 * beginning and reuse it further; hence we save its value to memory, upload a
1265 * constant value with bit21 set and then we restore it back with the saved value.
1266 * To simplify the WA, a constant value is formed by using the default value
1267 * of this register. This shouldn't be a problem because we are only modifying
1268 * it for a short period and this batch in non-premptible. We can ofcourse
1269 * use additional instructions that read the actual value of the register
1270 * at that time and set our bit of interest but it makes the WA complicated.
1271 *
1272 * This WA is also required for Gen9 so extracting as a function avoids
1273 * code duplication.
1274 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001275static u32 *
1276gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery9e000842015-07-03 14:27:31 +01001277{
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001278 *batch++ = MI_STORE_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1279 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1280 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
1281 *batch++ = 0;
Arun Siluvery9e000842015-07-03 14:27:31 +01001282
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001283 *batch++ = MI_LOAD_REGISTER_IMM(1);
1284 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1285 *batch++ = 0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES;
Arun Siluvery9e000842015-07-03 14:27:31 +01001286
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001287 batch = gen8_emit_pipe_control(batch,
1288 PIPE_CONTROL_CS_STALL |
1289 PIPE_CONTROL_DC_FLUSH_ENABLE,
1290 0);
Arun Siluvery9e000842015-07-03 14:27:31 +01001291
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001292 *batch++ = MI_LOAD_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1293 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1294 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
1295 *batch++ = 0;
Arun Siluvery9e000842015-07-03 14:27:31 +01001296
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001297 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001298}
1299
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001300/*
1301 * Typically we only have one indirect_ctx and per_ctx batch buffer which are
1302 * initialized at the beginning and shared across all contexts but this field
1303 * helps us to have multiple batches at different offsets and select them based
1304 * on a criteria. At the moment this batch always start at the beginning of the page
1305 * and at this point we don't have multiple wa_ctx batch buffers.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001306 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001307 * The number of WA applied are not known at the beginning; we use this field
1308 * to return the no of DWORDS written.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001309 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001310 * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
1311 * so it adds NOOPs as padding to make it cacheline aligned.
1312 * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
1313 * makes a complete batch buffer.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001314 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001315static u32 *gen8_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001316{
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001317 /* WaDisableCtxRestoreArbitration:bdw,chv */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001318 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001319
Arun Siluveryc82435b2015-06-19 18:37:13 +01001320 /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001321 if (IS_BROADWELL(engine->i915))
1322 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
Arun Siluveryc82435b2015-06-19 18:37:13 +01001323
Arun Siluvery0160f052015-06-23 15:46:57 +01001324 /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
1325 /* Actual scratch location is at 128 bytes offset */
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001326 batch = gen8_emit_pipe_control(batch,
1327 PIPE_CONTROL_FLUSH_L3 |
1328 PIPE_CONTROL_GLOBAL_GTT_IVB |
1329 PIPE_CONTROL_CS_STALL |
1330 PIPE_CONTROL_QW_WRITE,
1331 i915_ggtt_offset(engine->scratch) +
1332 2 * CACHELINE_BYTES);
Arun Siluvery0160f052015-06-23 15:46:57 +01001333
Chris Wilsonbeecec92017-10-03 21:34:52 +01001334 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1335
Arun Siluvery17ee9502015-06-19 19:07:01 +01001336 /* Pad to end of cacheline */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001337 while ((unsigned long)batch % CACHELINE_BYTES)
1338 *batch++ = MI_NOOP;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001339
1340 /*
1341 * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
1342 * execution depends on the length specified in terms of cache lines
1343 * in the register CTX_RCS_INDIRECT_CTX
1344 */
1345
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001346 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001347}
1348
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001349static u32 *gen9_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery0504cff2015-07-14 15:01:27 +01001350{
Chris Wilsonbeecec92017-10-03 21:34:52 +01001351 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
1352
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001353 /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt,glk */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001354 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
Arun Siluverya4106a72015-07-14 15:01:29 +01001355
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001356 /* WaDisableGatherAtSetShaderCommonSlice:skl,bxt,kbl,glk */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001357 *batch++ = MI_LOAD_REGISTER_IMM(1);
1358 *batch++ = i915_mmio_reg_offset(COMMON_SLICE_CHICKEN2);
1359 *batch++ = _MASKED_BIT_DISABLE(
1360 GEN9_DISABLE_GATHER_AT_SET_SHADER_COMMON_SLICE);
1361 *batch++ = MI_NOOP;
Mika Kuoppala873e8172016-07-20 14:26:13 +03001362
Mika Kuoppala066d4622016-06-07 17:19:15 +03001363 /* WaClearSlmSpaceAtContextSwitch:kbl */
1364 /* Actual scratch location is at 128 bytes offset */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001365 if (IS_KBL_REVID(engine->i915, 0, KBL_REVID_A0)) {
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001366 batch = gen8_emit_pipe_control(batch,
1367 PIPE_CONTROL_FLUSH_L3 |
1368 PIPE_CONTROL_GLOBAL_GTT_IVB |
1369 PIPE_CONTROL_CS_STALL |
1370 PIPE_CONTROL_QW_WRITE,
1371 i915_ggtt_offset(engine->scratch)
1372 + 2 * CACHELINE_BYTES);
Mika Kuoppala066d4622016-06-07 17:19:15 +03001373 }
Tim Gore3485d992016-07-05 10:01:30 +01001374
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001375 /* WaMediaPoolStateCmdInWABB:bxt,glk */
Tim Gore3485d992016-07-05 10:01:30 +01001376 if (HAS_POOLED_EU(engine->i915)) {
1377 /*
1378 * EU pool configuration is setup along with golden context
1379 * during context initialization. This value depends on
1380 * device type (2x6 or 3x6) and needs to be updated based
1381 * on which subslice is disabled especially for 2x6
1382 * devices, however it is safe to load default
1383 * configuration of 3x6 device instead of masking off
1384 * corresponding bits because HW ignores bits of a disabled
1385 * subslice and drops down to appropriate config. Please
1386 * see render_state_setup() in i915_gem_render_state.c for
1387 * possible configurations, to avoid duplication they are
1388 * not shown here again.
1389 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001390 *batch++ = GEN9_MEDIA_POOL_STATE;
1391 *batch++ = GEN9_MEDIA_POOL_ENABLE;
1392 *batch++ = 0x00777000;
1393 *batch++ = 0;
1394 *batch++ = 0;
1395 *batch++ = 0;
Tim Gore3485d992016-07-05 10:01:30 +01001396 }
1397
Chris Wilsonbeecec92017-10-03 21:34:52 +01001398 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1399
Arun Siluvery0504cff2015-07-14 15:01:27 +01001400 /* Pad to end of cacheline */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001401 while ((unsigned long)batch % CACHELINE_BYTES)
1402 *batch++ = MI_NOOP;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001403
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001404 return batch;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001405}
1406
Rafael Antognolli4b6ce682018-02-05 15:33:30 -08001407static u32 *
1408gen10_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
1409{
1410 int i;
1411
1412 /*
1413 * WaPipeControlBefore3DStateSamplePattern: cnl
1414 *
1415 * Ensure the engine is idle prior to programming a
1416 * 3DSTATE_SAMPLE_PATTERN during a context restore.
1417 */
1418 batch = gen8_emit_pipe_control(batch,
1419 PIPE_CONTROL_CS_STALL,
1420 0);
1421 /*
1422 * WaPipeControlBefore3DStateSamplePattern says we need 4 dwords for
1423 * the PIPE_CONTROL followed by 12 dwords of 0x0, so 16 dwords in
1424 * total. However, a PIPE_CONTROL is 6 dwords long, not 4, which is
1425 * confusing. Since gen8_emit_pipe_control() already advances the
1426 * batch by 6 dwords, we advance the other 10 here, completing a
1427 * cacheline. It's not clear if the workaround requires this padding
1428 * before other commands, or if it's just the regular padding we would
1429 * already have for the workaround bb, so leave it here for now.
1430 */
1431 for (i = 0; i < 10; i++)
1432 *batch++ = MI_NOOP;
1433
1434 /* Pad to end of cacheline */
1435 while ((unsigned long)batch % CACHELINE_BYTES)
1436 *batch++ = MI_NOOP;
1437
1438 return batch;
1439}
1440
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001441#define CTX_WA_BB_OBJ_SIZE (PAGE_SIZE)
1442
1443static int lrc_setup_wa_ctx(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001444{
Chris Wilson48bb74e2016-08-15 10:49:04 +01001445 struct drm_i915_gem_object *obj;
1446 struct i915_vma *vma;
1447 int err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001448
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001449 obj = i915_gem_object_create(engine->i915, CTX_WA_BB_OBJ_SIZE);
Chris Wilson48bb74e2016-08-15 10:49:04 +01001450 if (IS_ERR(obj))
1451 return PTR_ERR(obj);
1452
Chris Wilsona01cb372017-01-16 15:21:30 +00001453 vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
Chris Wilson48bb74e2016-08-15 10:49:04 +01001454 if (IS_ERR(vma)) {
1455 err = PTR_ERR(vma);
1456 goto err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001457 }
1458
Chris Wilson48bb74e2016-08-15 10:49:04 +01001459 err = i915_vma_pin(vma, 0, PAGE_SIZE, PIN_GLOBAL | PIN_HIGH);
1460 if (err)
1461 goto err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001462
Chris Wilson48bb74e2016-08-15 10:49:04 +01001463 engine->wa_ctx.vma = vma;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001464 return 0;
Chris Wilson48bb74e2016-08-15 10:49:04 +01001465
1466err:
1467 i915_gem_object_put(obj);
1468 return err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001469}
1470
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001471static void lrc_destroy_wa_ctx(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001472{
Chris Wilson19880c42016-08-15 10:49:05 +01001473 i915_vma_unpin_and_release(&engine->wa_ctx.vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001474}
1475
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001476typedef u32 *(*wa_bb_func_t)(struct intel_engine_cs *engine, u32 *batch);
1477
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001478static int intel_init_workaround_bb(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001479{
Chris Wilson48bb74e2016-08-15 10:49:04 +01001480 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001481 struct i915_wa_ctx_bb *wa_bb[2] = { &wa_ctx->indirect_ctx,
1482 &wa_ctx->per_ctx };
1483 wa_bb_func_t wa_bb_fn[2];
Arun Siluvery17ee9502015-06-19 19:07:01 +01001484 struct page *page;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001485 void *batch, *batch_ptr;
1486 unsigned int i;
Chris Wilson48bb74e2016-08-15 10:49:04 +01001487 int ret;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001488
Tvrtko Ursulin10bde232018-01-19 10:00:04 +00001489 if (GEM_WARN_ON(engine->id != RCS))
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001490 return -EINVAL;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001491
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001492 switch (INTEL_GEN(engine->i915)) {
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001493 case 10:
Rafael Antognolli4b6ce682018-02-05 15:33:30 -08001494 wa_bb_fn[0] = gen10_init_indirectctx_bb;
1495 wa_bb_fn[1] = NULL;
1496 break;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001497 case 9:
1498 wa_bb_fn[0] = gen9_init_indirectctx_bb;
Chris Wilsonb8aa2232017-09-21 14:54:44 +01001499 wa_bb_fn[1] = NULL;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001500 break;
1501 case 8:
1502 wa_bb_fn[0] = gen8_init_indirectctx_bb;
Chris Wilson3ad7b522017-10-03 21:34:49 +01001503 wa_bb_fn[1] = NULL;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001504 break;
1505 default:
1506 MISSING_CASE(INTEL_GEN(engine->i915));
Arun Siluvery5e60d792015-06-23 15:50:44 +01001507 return 0;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001508 }
Arun Siluvery5e60d792015-06-23 15:50:44 +01001509
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001510 ret = lrc_setup_wa_ctx(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001511 if (ret) {
1512 DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
1513 return ret;
1514 }
1515
Chris Wilson48bb74e2016-08-15 10:49:04 +01001516 page = i915_gem_object_get_dirty_page(wa_ctx->vma->obj, 0);
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001517 batch = batch_ptr = kmap_atomic(page);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001518
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001519 /*
1520 * Emit the two workaround batch buffers, recording the offset from the
1521 * start of the workaround batch buffer object for each and their
1522 * respective sizes.
1523 */
1524 for (i = 0; i < ARRAY_SIZE(wa_bb_fn); i++) {
1525 wa_bb[i]->offset = batch_ptr - batch;
Chris Wilson1d2a19c2018-01-26 12:18:46 +00001526 if (GEM_WARN_ON(!IS_ALIGNED(wa_bb[i]->offset,
1527 CACHELINE_BYTES))) {
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001528 ret = -EINVAL;
1529 break;
1530 }
Chris Wilson604a8f62017-09-21 14:54:43 +01001531 if (wa_bb_fn[i])
1532 batch_ptr = wa_bb_fn[i](engine, batch_ptr);
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001533 wa_bb[i]->size = batch_ptr - (batch + wa_bb[i]->offset);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001534 }
1535
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001536 BUG_ON(batch_ptr - batch > CTX_WA_BB_OBJ_SIZE);
1537
Arun Siluvery17ee9502015-06-19 19:07:01 +01001538 kunmap_atomic(batch);
1539 if (ret)
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001540 lrc_destroy_wa_ctx(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001541
1542 return ret;
1543}
1544
Chris Wilson64f09f02017-08-07 13:19:19 +01001545static u8 gtiir[] = {
1546 [RCS] = 0,
1547 [BCS] = 0,
1548 [VCS] = 1,
1549 [VCS2] = 1,
1550 [VECS] = 3,
1551};
1552
Chris Wilsonf3c9d4072018-01-02 15:12:34 +00001553static void enable_execlists(struct intel_engine_cs *engine)
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001554{
Chris Wilsonc0336662016-05-06 15:40:21 +01001555 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonf3c9d4072018-01-02 15:12:34 +00001556
1557 I915_WRITE(RING_HWSTAM(engine->mmio_base), 0xffffffff);
Kelvin Gardiner225701f2018-01-30 11:49:17 -02001558
1559 /*
1560 * Make sure we're not enabling the new 12-deep CSB
1561 * FIFO as that requires a slightly updated handling
1562 * in the ctx switch irq. Since we're currently only
1563 * using only 2 elements of the enhanced execlists the
1564 * deeper FIFO it's not needed and it's not worth adding
1565 * more statements to the irq handler to support it.
1566 */
1567 if (INTEL_GEN(dev_priv) >= 11)
1568 I915_WRITE(RING_MODE_GEN7(engine),
1569 _MASKED_BIT_DISABLE(GEN11_GFX_DISABLE_LEGACY_MODE));
1570 else
1571 I915_WRITE(RING_MODE_GEN7(engine),
1572 _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
1573
Chris Wilsonf3c9d4072018-01-02 15:12:34 +00001574 I915_WRITE(RING_HWS_PGA(engine->mmio_base),
1575 engine->status_page.ggtt_offset);
1576 POSTING_READ(RING_HWS_PGA(engine->mmio_base));
Chris Wilsone8401302018-02-05 15:24:30 +00001577
1578 /* Following the reset, we need to reload the CSB read/write pointers */
1579 engine->execlists.csb_head = -1;
Chris Wilsonf3c9d4072018-01-02 15:12:34 +00001580}
1581
1582static int gen8_init_common_ring(struct intel_engine_cs *engine)
1583{
Mika Kuoppalab620e872017-09-22 15:43:03 +03001584 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001585 int ret;
1586
1587 ret = intel_mocs_init_engine(engine);
1588 if (ret)
1589 return ret;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001590
Chris Wilsonad07dfc2016-10-07 07:53:26 +01001591 intel_engine_reset_breadcrumbs(engine);
Chris Wilsonf3b8f912017-01-05 15:30:21 +00001592 intel_engine_init_hangcheck(engine);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001593
Chris Wilsonf3c9d4072018-01-02 15:12:34 +00001594 enable_execlists(engine);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001595
Chris Wilson64f09f02017-08-07 13:19:19 +01001596 /* After a GPU reset, we may have requests to replay */
Michał Winiarski9bdc3572017-10-25 18:25:19 +01001597 if (execlists->first)
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05301598 tasklet_schedule(&execlists->tasklet);
Chris Wilson6b764a52017-04-25 11:38:35 +01001599
Chris Wilson821ed7d2016-09-09 14:11:53 +01001600 return 0;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001601}
1602
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001603static int gen8_init_render_ring(struct intel_engine_cs *engine)
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001604{
Chris Wilsonc0336662016-05-06 15:40:21 +01001605 struct drm_i915_private *dev_priv = engine->i915;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001606 int ret;
1607
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001608 ret = gen8_init_common_ring(engine);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001609 if (ret)
1610 return ret;
1611
1612 /* We need to disable the AsyncFlip performance optimisations in order
1613 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1614 * programmed to '1' on all products.
1615 *
1616 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
1617 */
1618 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1619
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001620 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1621
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001622 return init_workarounds_ring(engine);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001623}
1624
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001625static int gen9_init_render_ring(struct intel_engine_cs *engine)
Damien Lespiau82ef8222015-02-09 19:33:08 +00001626{
1627 int ret;
1628
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001629 ret = gen8_init_common_ring(engine);
Damien Lespiau82ef8222015-02-09 19:33:08 +00001630 if (ret)
1631 return ret;
1632
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001633 return init_workarounds_ring(engine);
Damien Lespiau82ef8222015-02-09 19:33:08 +00001634}
1635
Chris Wilson42232212018-01-02 15:12:32 +00001636static void reset_irq(struct intel_engine_cs *engine)
1637{
1638 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson274de872018-02-02 14:54:55 +00001639 int i;
Chris Wilson42232212018-01-02 15:12:32 +00001640
Chris Wilsone8401302018-02-05 15:24:30 +00001641 GEM_BUG_ON(engine->id >= ARRAY_SIZE(gtiir));
1642
Chris Wilson42232212018-01-02 15:12:32 +00001643 /*
1644 * Clear any pending interrupt state.
1645 *
1646 * We do it twice out of paranoia that some of the IIR are double
1647 * buffered, and if we only reset it once there may still be
1648 * an interrupt pending.
1649 */
Chris Wilson274de872018-02-02 14:54:55 +00001650 for (i = 0; i < 2; i++) {
1651 I915_WRITE(GEN8_GT_IIR(gtiir[engine->id]),
1652 GT_CONTEXT_SWITCH_INTERRUPT << engine->irq_shift);
1653 POSTING_READ(GEN8_GT_IIR(gtiir[engine->id]));
1654 }
1655 GEM_BUG_ON(I915_READ(GEN8_GT_IIR(gtiir[engine->id])) &
1656 (GT_CONTEXT_SWITCH_INTERRUPT << engine->irq_shift));
1657
Chris Wilson42232212018-01-02 15:12:32 +00001658 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
1659}
1660
Chris Wilson821ed7d2016-09-09 14:11:53 +01001661static void reset_common_ring(struct intel_engine_cs *engine,
Chris Wilsone61e0f52018-02-21 09:56:36 +00001662 struct i915_request *request)
Chris Wilson821ed7d2016-09-09 14:11:53 +01001663{
Mika Kuoppalab620e872017-09-22 15:43:03 +03001664 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001665 struct intel_context *ce;
Chris Wilson221ab97192017-09-16 21:44:14 +01001666 unsigned long flags;
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001667
Chris Wilson16a87392017-12-20 09:06:26 +00001668 GEM_TRACE("%s seqno=%x\n",
1669 engine->name, request ? request->global_seqno : 0);
Chris Wilson42232212018-01-02 15:12:32 +00001670
Chris Wilsona3e38832018-03-02 14:32:45 +00001671 /* See execlists_cancel_requests() for the irq/spinlock split. */
1672 local_irq_save(flags);
Chris Wilson221ab97192017-09-16 21:44:14 +01001673
Chris Wilsonaebbc2d2018-03-02 13:12:46 +00001674 reset_irq(engine);
1675
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001676 /*
1677 * Catch up with any missed context-switch interrupts.
1678 *
1679 * Ideally we would just read the remaining CSB entries now that we
1680 * know the gpu is idle. However, the CSB registers are sometimes^W
1681 * often trashed across a GPU reset! Instead we have to rely on
1682 * guessing the missed context-switch events by looking at what
1683 * requests were completed.
1684 */
Michał Winiarskia4598d12017-10-25 22:00:18 +02001685 execlists_cancel_port_requests(execlists);
Chris Wilson221ab97192017-09-16 21:44:14 +01001686
1687 /* Push back any incomplete requests for replay after the reset. */
Chris Wilsona3e38832018-03-02 14:32:45 +00001688 spin_lock(&engine->timeline->lock);
Michał Winiarskia4598d12017-10-25 22:00:18 +02001689 __unwind_incomplete_requests(engine);
Chris Wilsona3e38832018-03-02 14:32:45 +00001690 spin_unlock(&engine->timeline->lock);
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001691
Chris Wilsone8401302018-02-05 15:24:30 +00001692 /* Mark all CS interrupts as complete */
1693 execlists->active = 0;
1694
Chris Wilsona3e38832018-03-02 14:32:45 +00001695 local_irq_restore(flags);
Chris Wilsonaebbc2d2018-03-02 13:12:46 +00001696
Chris Wilsona3e38832018-03-02 14:32:45 +00001697 /*
1698 * If the request was innocent, we leave the request in the ELSP
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001699 * and will try to replay it on restarting. The context image may
1700 * have been corrupted by the reset, in which case we may have
1701 * to service a new GPU hang, but more likely we can continue on
1702 * without impact.
1703 *
1704 * If the request was guilty, we presume the context is corrupt
1705 * and have to at least restore the RING register in the context
1706 * image back to the expected values to skip over the guilty request.
1707 */
Chris Wilson221ab97192017-09-16 21:44:14 +01001708 if (!request || request->fence.error != -EIO)
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001709 return;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001710
Chris Wilsona3e38832018-03-02 14:32:45 +00001711 /*
1712 * We want a simple context + ring to execute the breadcrumb update.
Chris Wilsona3aabe82016-10-04 21:11:26 +01001713 * We cannot rely on the context being intact across the GPU hang,
1714 * so clear it and rebuild just what we need for the breadcrumb.
1715 * All pending requests for this context will be zapped, and any
1716 * future request will be after userspace has had the opportunity
1717 * to recreate its own state.
1718 */
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001719 ce = &request->ctx->engine[engine->id];
Chris Wilsona3aabe82016-10-04 21:11:26 +01001720 execlists_init_reg_state(ce->lrc_reg_state,
1721 request->ctx, engine, ce->ring);
1722
Chris Wilson821ed7d2016-09-09 14:11:53 +01001723 /* Move the RING_HEAD onto the breadcrumb, past the hanging batch */
Chris Wilsona3aabe82016-10-04 21:11:26 +01001724 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
1725 i915_ggtt_offset(ce->ring->vma);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001726 ce->lrc_reg_state[CTX_RING_HEAD+1] = request->postfix;
Chris Wilsona3aabe82016-10-04 21:11:26 +01001727
Chris Wilson821ed7d2016-09-09 14:11:53 +01001728 request->ring->head = request->postfix;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001729 intel_ring_update_space(request->ring);
1730
Chris Wilsona3aabe82016-10-04 21:11:26 +01001731 /* Reset WaIdleLiteRestore:bdw,skl as well */
Chris Wilson7e4992a2017-09-28 20:38:59 +01001732 unwind_wa_tail(request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001733}
1734
Chris Wilsone61e0f52018-02-21 09:56:36 +00001735static int intel_logical_ring_emit_pdps(struct i915_request *rq)
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001736{
Chris Wilsone61e0f52018-02-21 09:56:36 +00001737 struct i915_hw_ppgtt *ppgtt = rq->ctx->ppgtt;
1738 struct intel_engine_cs *engine = rq->engine;
Mika Kuoppalae7167762017-02-28 17:28:10 +02001739 const int num_lri_cmds = GEN8_3LVL_PDPES * 2;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001740 u32 *cs;
1741 int i;
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001742
Chris Wilsone61e0f52018-02-21 09:56:36 +00001743 cs = intel_ring_begin(rq, num_lri_cmds * 2 + 2);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001744 if (IS_ERR(cs))
1745 return PTR_ERR(cs);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001746
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001747 *cs++ = MI_LOAD_REGISTER_IMM(num_lri_cmds);
Mika Kuoppalae7167762017-02-28 17:28:10 +02001748 for (i = GEN8_3LVL_PDPES - 1; i >= 0; i--) {
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001749 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
1750
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001751 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, i));
1752 *cs++ = upper_32_bits(pd_daddr);
1753 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, i));
1754 *cs++ = lower_32_bits(pd_daddr);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001755 }
1756
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001757 *cs++ = MI_NOOP;
Chris Wilsone61e0f52018-02-21 09:56:36 +00001758 intel_ring_advance(rq, cs);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001759
1760 return 0;
1761}
1762
Chris Wilsone61e0f52018-02-21 09:56:36 +00001763static int gen8_emit_bb_start(struct i915_request *rq,
Chris Wilson803688b2016-08-02 22:50:27 +01001764 u64 offset, u32 len,
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001765 const unsigned int flags)
Oscar Mateo15648582014-07-24 17:04:32 +01001766{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001767 u32 *cs;
Oscar Mateo15648582014-07-24 17:04:32 +01001768 int ret;
1769
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001770 /* Don't rely in hw updating PDPs, specially in lite-restore.
1771 * Ideally, we should set Force PD Restore in ctx descriptor,
1772 * but we can't. Force Restore would be a second option, but
1773 * it is unsafe in case of lite-restore (because the ctx is
Michel Thierry2dba3232015-07-30 11:06:23 +01001774 * not idle). PML4 is allocated during ppgtt init so this is
1775 * not needed in 48-bit.*/
Chris Wilsone61e0f52018-02-21 09:56:36 +00001776 if (rq->ctx->ppgtt &&
1777 (intel_engine_flag(rq->engine) & rq->ctx->ppgtt->pd_dirty_rings) &&
1778 !i915_vm_is_48bit(&rq->ctx->ppgtt->base) &&
1779 !intel_vgpu_active(rq->i915)) {
1780 ret = intel_logical_ring_emit_pdps(rq);
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001781 if (ret)
1782 return ret;
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001783
Chris Wilsone61e0f52018-02-21 09:56:36 +00001784 rq->ctx->ppgtt->pd_dirty_rings &= ~intel_engine_flag(rq->engine);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001785 }
1786
Chris Wilsone61e0f52018-02-21 09:56:36 +00001787 cs = intel_ring_begin(rq, 4);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001788 if (IS_ERR(cs))
1789 return PTR_ERR(cs);
Oscar Mateo15648582014-07-24 17:04:32 +01001790
Chris Wilson279f5a02017-10-05 20:10:05 +01001791 /*
1792 * WaDisableCtxRestoreArbitration:bdw,chv
1793 *
1794 * We don't need to perform MI_ARB_ENABLE as often as we do (in
1795 * particular all the gen that do not need the w/a at all!), if we
1796 * took care to make sure that on every switch into this context
1797 * (both ordinary and for preemption) that arbitrartion was enabled
1798 * we would be fine. However, there doesn't seem to be a downside to
1799 * being paranoid and making sure it is set before each batch and
1800 * every context-switch.
1801 *
1802 * Note that if we fail to enable arbitration before the request
1803 * is complete, then we do not see the context-switch interrupt and
1804 * the engine hangs (with RING_HEAD == RING_TAIL).
1805 *
1806 * That satisfies both the GPGPU w/a and our heavy-handed paranoia.
1807 */
Chris Wilson3ad7b522017-10-03 21:34:49 +01001808 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1809
Oscar Mateo15648582014-07-24 17:04:32 +01001810 /* FIXME(BDW): Address space and security selectors. */
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001811 *cs++ = MI_BATCH_BUFFER_START_GEN8 |
1812 (flags & I915_DISPATCH_SECURE ? 0 : BIT(8)) |
1813 (flags & I915_DISPATCH_RS ? MI_BATCH_RESOURCE_STREAMER : 0);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001814 *cs++ = lower_32_bits(offset);
1815 *cs++ = upper_32_bits(offset);
Chris Wilsone61e0f52018-02-21 09:56:36 +00001816 intel_ring_advance(rq, cs);
Oscar Mateo15648582014-07-24 17:04:32 +01001817
1818 return 0;
1819}
1820
Chris Wilson31bb59c2016-07-01 17:23:27 +01001821static void gen8_logical_ring_enable_irq(struct intel_engine_cs *engine)
Oscar Mateo73d477f2014-07-24 17:04:31 +01001822{
Chris Wilsonc0336662016-05-06 15:40:21 +01001823 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson31bb59c2016-07-01 17:23:27 +01001824 I915_WRITE_IMR(engine,
1825 ~(engine->irq_enable_mask | engine->irq_keep_mask));
1826 POSTING_READ_FW(RING_IMR(engine->mmio_base));
Oscar Mateo73d477f2014-07-24 17:04:31 +01001827}
1828
Chris Wilson31bb59c2016-07-01 17:23:27 +01001829static void gen8_logical_ring_disable_irq(struct intel_engine_cs *engine)
Oscar Mateo73d477f2014-07-24 17:04:31 +01001830{
Chris Wilsonc0336662016-05-06 15:40:21 +01001831 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson31bb59c2016-07-01 17:23:27 +01001832 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Oscar Mateo73d477f2014-07-24 17:04:31 +01001833}
1834
Chris Wilsone61e0f52018-02-21 09:56:36 +00001835static int gen8_emit_flush(struct i915_request *request, u32 mode)
Oscar Mateo47122742014-07-24 17:04:28 +01001836{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001837 u32 cmd, *cs;
Oscar Mateo47122742014-07-24 17:04:28 +01001838
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001839 cs = intel_ring_begin(request, 4);
1840 if (IS_ERR(cs))
1841 return PTR_ERR(cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001842
1843 cmd = MI_FLUSH_DW + 1;
1844
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001845 /* We always require a command barrier so that subsequent
1846 * commands, such as breadcrumb interrupts, are strictly ordered
1847 * wrt the contents of the write cache being flushed to memory
1848 * (and thus being coherent from the CPU).
1849 */
1850 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1851
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001852 if (mode & EMIT_INVALIDATE) {
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001853 cmd |= MI_INVALIDATE_TLB;
Chris Wilson1dae2df2016-08-02 22:50:19 +01001854 if (request->engine->id == VCS)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001855 cmd |= MI_INVALIDATE_BSD;
Oscar Mateo47122742014-07-24 17:04:28 +01001856 }
1857
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001858 *cs++ = cmd;
1859 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
1860 *cs++ = 0; /* upper addr */
1861 *cs++ = 0; /* value */
1862 intel_ring_advance(request, cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001863
1864 return 0;
1865}
1866
Chris Wilsone61e0f52018-02-21 09:56:36 +00001867static int gen8_emit_flush_render(struct i915_request *request,
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001868 u32 mode)
Oscar Mateo47122742014-07-24 17:04:28 +01001869{
Chris Wilsonb5321f32016-08-02 22:50:18 +01001870 struct intel_engine_cs *engine = request->engine;
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001871 u32 scratch_addr =
1872 i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001873 bool vf_flush_wa = false, dc_flush_wa = false;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001874 u32 *cs, flags = 0;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001875 int len;
Oscar Mateo47122742014-07-24 17:04:28 +01001876
1877 flags |= PIPE_CONTROL_CS_STALL;
1878
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001879 if (mode & EMIT_FLUSH) {
Oscar Mateo47122742014-07-24 17:04:28 +01001880 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
1881 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -08001882 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +01001883 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Oscar Mateo47122742014-07-24 17:04:28 +01001884 }
1885
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001886 if (mode & EMIT_INVALIDATE) {
Oscar Mateo47122742014-07-24 17:04:28 +01001887 flags |= PIPE_CONTROL_TLB_INVALIDATE;
1888 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
1889 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
1890 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
1891 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
1892 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
1893 flags |= PIPE_CONTROL_QW_WRITE;
1894 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Oscar Mateo47122742014-07-24 17:04:28 +01001895
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001896 /*
1897 * On GEN9: before VF_CACHE_INVALIDATE we need to emit a NULL
1898 * pipe control.
1899 */
Chris Wilsonc0336662016-05-06 15:40:21 +01001900 if (IS_GEN9(request->i915))
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001901 vf_flush_wa = true;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001902
1903 /* WaForGAMHang:kbl */
1904 if (IS_KBL_REVID(request->i915, 0, KBL_REVID_B0))
1905 dc_flush_wa = true;
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001906 }
Imre Deak9647ff32015-01-25 13:27:11 -08001907
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001908 len = 6;
1909
1910 if (vf_flush_wa)
1911 len += 6;
1912
1913 if (dc_flush_wa)
1914 len += 12;
1915
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001916 cs = intel_ring_begin(request, len);
1917 if (IS_ERR(cs))
1918 return PTR_ERR(cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001919
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001920 if (vf_flush_wa)
1921 cs = gen8_emit_pipe_control(cs, 0, 0);
Imre Deak9647ff32015-01-25 13:27:11 -08001922
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001923 if (dc_flush_wa)
1924 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_DC_FLUSH_ENABLE,
1925 0);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001926
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001927 cs = gen8_emit_pipe_control(cs, flags, scratch_addr);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001928
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001929 if (dc_flush_wa)
1930 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_CS_STALL, 0);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001931
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001932 intel_ring_advance(request, cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001933
1934 return 0;
1935}
1936
Chris Wilson7c17d372016-01-20 15:43:35 +02001937/*
1938 * Reserve space for 2 NOOPs at the end of each request to be
1939 * used as a workaround for not being allowed to do lite
1940 * restore with HEAD==TAIL (WaIdleLiteRestore).
1941 */
Chris Wilsone61e0f52018-02-21 09:56:36 +00001942static void gen8_emit_wa_tail(struct i915_request *request, u32 *cs)
Oscar Mateo4da46e12014-07-24 17:04:27 +01001943{
Chris Wilsonbeecec92017-10-03 21:34:52 +01001944 /* Ensure there's always at least one preemption point per-request. */
1945 *cs++ = MI_ARB_CHECK;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001946 *cs++ = MI_NOOP;
1947 request->wa_tail = intel_ring_offset(request, cs);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001948}
Oscar Mateo4da46e12014-07-24 17:04:27 +01001949
Chris Wilsone61e0f52018-02-21 09:56:36 +00001950static void gen8_emit_breadcrumb(struct i915_request *request, u32 *cs)
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001951{
Chris Wilson7c17d372016-01-20 15:43:35 +02001952 /* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */
1953 BUILD_BUG_ON(I915_GEM_HWS_INDEX_ADDR & (1 << 5));
Oscar Mateo4da46e12014-07-24 17:04:27 +01001954
Michał Winiarskidf77cd82017-10-25 22:00:15 +02001955 cs = gen8_emit_ggtt_write(cs, request->global_seqno,
1956 intel_hws_seqno_address(request->engine));
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001957 *cs++ = MI_USER_INTERRUPT;
1958 *cs++ = MI_NOOP;
1959 request->tail = intel_ring_offset(request, cs);
Chris Wilsoned1501d2017-03-27 14:14:12 +01001960 assert_ring_tail_valid(request->ring, request->tail);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001961
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001962 gen8_emit_wa_tail(request, cs);
Chris Wilson7c17d372016-01-20 15:43:35 +02001963}
Chris Wilson98f29e82016-10-28 13:58:51 +01001964static const int gen8_emit_breadcrumb_sz = 6 + WA_TAIL_DWORDS;
1965
Chris Wilsone61e0f52018-02-21 09:56:36 +00001966static void gen8_emit_breadcrumb_rcs(struct i915_request *request, u32 *cs)
Chris Wilson7c17d372016-01-20 15:43:35 +02001967{
Michał Winiarskice81a652016-04-12 15:51:55 +02001968 /* We're using qword write, seqno should be aligned to 8 bytes. */
1969 BUILD_BUG_ON(I915_GEM_HWS_INDEX & 1);
1970
Michał Winiarskidf77cd82017-10-25 22:00:15 +02001971 cs = gen8_emit_ggtt_write_rcs(cs, request->global_seqno,
1972 intel_hws_seqno_address(request->engine));
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001973 *cs++ = MI_USER_INTERRUPT;
1974 *cs++ = MI_NOOP;
1975 request->tail = intel_ring_offset(request, cs);
Chris Wilsoned1501d2017-03-27 14:14:12 +01001976 assert_ring_tail_valid(request->ring, request->tail);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001977
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001978 gen8_emit_wa_tail(request, cs);
Oscar Mateo4da46e12014-07-24 17:04:27 +01001979}
Michał Winiarskidf77cd82017-10-25 22:00:15 +02001980static const int gen8_emit_breadcrumb_rcs_sz = 8 + WA_TAIL_DWORDS;
Chris Wilson98f29e82016-10-28 13:58:51 +01001981
Chris Wilsone61e0f52018-02-21 09:56:36 +00001982static int gen8_init_rcs_context(struct i915_request *rq)
Thomas Daniele7778be2014-12-02 12:50:48 +00001983{
1984 int ret;
1985
Chris Wilsone61e0f52018-02-21 09:56:36 +00001986 ret = intel_ring_workarounds_emit(rq);
Thomas Daniele7778be2014-12-02 12:50:48 +00001987 if (ret)
1988 return ret;
1989
Chris Wilsone61e0f52018-02-21 09:56:36 +00001990 ret = intel_rcs_context_init_mocs(rq);
Peter Antoine3bbaba02015-07-10 20:13:11 +03001991 /*
1992 * Failing to program the MOCS is non-fatal.The system will not
1993 * run at peak performance. So generate an error and carry on.
1994 */
1995 if (ret)
1996 DRM_ERROR("MOCS failed to program: expect performance issues.\n");
1997
Chris Wilsone61e0f52018-02-21 09:56:36 +00001998 return i915_gem_render_state_emit(rq);
Thomas Daniele7778be2014-12-02 12:50:48 +00001999}
2000
Oscar Mateo73e4d072014-07-24 17:04:48 +01002001/**
2002 * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01002003 * @engine: Engine Command Streamer.
Oscar Mateo73e4d072014-07-24 17:04:48 +01002004 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002005void intel_logical_ring_cleanup(struct intel_engine_cs *engine)
Oscar Mateo454afeb2014-07-24 17:04:22 +01002006{
John Harrison6402c332014-10-31 12:00:26 +00002007 struct drm_i915_private *dev_priv;
Oscar Mateo9832b9d2014-07-24 17:04:30 +01002008
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01002009 /*
2010 * Tasklet cannot be active at this point due intel_mark_active/idle
2011 * so this is just for documentation.
2012 */
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302013 if (WARN_ON(test_bit(TASKLET_STATE_SCHED,
2014 &engine->execlists.tasklet.state)))
2015 tasklet_kill(&engine->execlists.tasklet);
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01002016
Chris Wilsonc0336662016-05-06 15:40:21 +01002017 dev_priv = engine->i915;
John Harrison6402c332014-10-31 12:00:26 +00002018
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002019 if (engine->buffer) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002020 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
Dave Gordonb0366a52015-12-08 15:02:36 +00002021 }
Oscar Mateo48d82382014-07-24 17:04:23 +01002022
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002023 if (engine->cleanup)
2024 engine->cleanup(engine);
Oscar Mateo48d82382014-07-24 17:04:23 +01002025
Chris Wilsone8a9c582016-12-18 15:37:20 +00002026 intel_engine_cleanup_common(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01002027
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00002028 lrc_destroy_wa_ctx(engine);
Chris Wilsonf3c9d4072018-01-02 15:12:34 +00002029
Chris Wilsonc0336662016-05-06 15:40:21 +01002030 engine->i915 = NULL;
Akash Goel3b3f1652016-10-13 22:44:48 +05302031 dev_priv->engine[engine->id] = NULL;
2032 kfree(engine);
Oscar Mateo454afeb2014-07-24 17:04:22 +01002033}
2034
Chris Wilsonff44ad52017-03-16 17:13:03 +00002035static void execlists_set_default_submission(struct intel_engine_cs *engine)
Chris Wilsonddd66c52016-08-02 22:50:31 +01002036{
Chris Wilsonff44ad52017-03-16 17:13:03 +00002037 engine->submit_request = execlists_submit_request;
Chris Wilson27a5f612017-09-15 18:31:00 +01002038 engine->cancel_requests = execlists_cancel_requests;
Chris Wilsonff44ad52017-03-16 17:13:03 +00002039 engine->schedule = execlists_schedule;
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302040 engine->execlists.tasklet.func = execlists_submission_tasklet;
Chris Wilsonaba5e272017-10-25 15:39:41 +01002041
2042 engine->park = NULL;
2043 engine->unpark = NULL;
Tvrtko Ursulincf669b42017-11-29 10:28:05 +00002044
2045 engine->flags |= I915_ENGINE_SUPPORTS_STATS;
Chris Wilson3fed1802018-02-07 21:05:43 +00002046
2047 engine->i915->caps.scheduler =
2048 I915_SCHEDULER_CAP_ENABLED |
2049 I915_SCHEDULER_CAP_PRIORITY;
Chris Wilsond6376372018-02-07 21:05:44 +00002050 if (engine->i915->preempt_context)
Chris Wilson3fed1802018-02-07 21:05:43 +00002051 engine->i915->caps.scheduler |= I915_SCHEDULER_CAP_PREEMPTION;
Chris Wilsonddd66c52016-08-02 22:50:31 +01002052}
2053
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00002054static void
Chris Wilsone1382ef2016-05-06 15:40:20 +01002055logical_ring_default_vfuncs(struct intel_engine_cs *engine)
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00002056{
2057 /* Default vfuncs which can be overriden by each engine. */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002058 engine->init_hw = gen8_init_common_ring;
Chris Wilson821ed7d2016-09-09 14:11:53 +01002059 engine->reset_hw = reset_common_ring;
Chris Wilsone8a9c582016-12-18 15:37:20 +00002060
2061 engine->context_pin = execlists_context_pin;
2062 engine->context_unpin = execlists_context_unpin;
2063
Chris Wilsonf73e7392016-12-18 15:37:24 +00002064 engine->request_alloc = execlists_request_alloc;
2065
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002066 engine->emit_flush = gen8_emit_flush;
Chris Wilson9b81d552016-10-28 13:58:50 +01002067 engine->emit_breadcrumb = gen8_emit_breadcrumb;
Chris Wilson98f29e82016-10-28 13:58:51 +01002068 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_sz;
Chris Wilsonff44ad52017-03-16 17:13:03 +00002069
2070 engine->set_default_submission = execlists_set_default_submission;
Chris Wilsonddd66c52016-08-02 22:50:31 +01002071
Tvrtko Ursulind4ccceb2018-03-02 18:14:56 +02002072 if (INTEL_GEN(engine->i915) < 11) {
2073 engine->irq_enable = gen8_logical_ring_enable_irq;
2074 engine->irq_disable = gen8_logical_ring_disable_irq;
2075 } else {
2076 /*
2077 * TODO: On Gen11 interrupt masks need to be clear
2078 * to allow C6 entry. Keep interrupts enabled at
2079 * and take the hit of generating extra interrupts
2080 * until a more refined solution exists.
2081 */
2082 }
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002083 engine->emit_bb_start = gen8_emit_bb_start;
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00002084}
2085
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00002086static inline void
Dave Gordonc2c7f242016-07-13 16:03:35 +01002087logical_ring_default_irqs(struct intel_engine_cs *engine)
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00002088{
Dave Gordonc2c7f242016-07-13 16:03:35 +01002089 unsigned shift = engine->irq_shift;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002090 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << shift;
2091 engine->irq_keep_mask = GT_CONTEXT_SWITCH_INTERRUPT << shift;
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00002092}
2093
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002094static void
2095logical_ring_setup(struct intel_engine_cs *engine)
2096{
2097 struct drm_i915_private *dev_priv = engine->i915;
2098 enum forcewake_domains fw_domains;
2099
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01002100 intel_engine_setup_common(engine);
2101
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002102 /* Intentionally left blank. */
2103 engine->buffer = NULL;
2104
2105 fw_domains = intel_uncore_forcewake_for_reg(dev_priv,
2106 RING_ELSP(engine),
2107 FW_REG_WRITE);
2108
2109 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
2110 RING_CONTEXT_STATUS_PTR(engine),
2111 FW_REG_READ | FW_REG_WRITE);
2112
2113 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
2114 RING_CONTEXT_STATUS_BUF_BASE(engine),
2115 FW_REG_READ);
2116
Mika Kuoppalab620e872017-09-22 15:43:03 +03002117 engine->execlists.fw_domains = fw_domains;
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002118
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302119 tasklet_init(&engine->execlists.tasklet,
2120 execlists_submission_tasklet, (unsigned long)engine);
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002121
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002122 logical_ring_default_vfuncs(engine);
2123 logical_ring_default_irqs(engine);
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002124}
2125
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +01002126static int logical_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002127{
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002128 int ret;
2129
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01002130 ret = intel_engine_init_common(engine);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002131 if (ret)
2132 goto error;
2133
Chris Wilson693cfbf2018-01-02 15:12:33 +00002134 engine->execlists.elsp =
2135 engine->i915->regs + i915_mmio_reg_offset(RING_ELSP(engine));
2136
Chris Wilsond6376372018-02-07 21:05:44 +00002137 engine->execlists.preempt_complete_status = ~0u;
2138 if (engine->i915->preempt_context)
2139 engine->execlists.preempt_complete_status =
2140 upper_32_bits(engine->i915->preempt_context->engine[engine->id].lrc_desc);
2141
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002142 return 0;
2143
2144error:
2145 intel_logical_ring_cleanup(engine);
2146 return ret;
2147}
2148
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +01002149int logical_render_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002150{
2151 struct drm_i915_private *dev_priv = engine->i915;
2152 int ret;
2153
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002154 logical_ring_setup(engine);
2155
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002156 if (HAS_L3_DPF(dev_priv))
2157 engine->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
2158
2159 /* Override some for render ring. */
2160 if (INTEL_GEN(dev_priv) >= 9)
2161 engine->init_hw = gen9_init_render_ring;
2162 else
2163 engine->init_hw = gen8_init_render_ring;
2164 engine->init_context = gen8_init_rcs_context;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002165 engine->emit_flush = gen8_emit_flush_render;
Michał Winiarskidf77cd82017-10-25 22:00:15 +02002166 engine->emit_breadcrumb = gen8_emit_breadcrumb_rcs;
2167 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_rcs_sz;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002168
Chris Wilsonf51455d2017-01-10 14:47:34 +00002169 ret = intel_engine_create_scratch(engine, PAGE_SIZE);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002170 if (ret)
2171 return ret;
2172
2173 ret = intel_init_workaround_bb(engine);
2174 if (ret) {
2175 /*
2176 * We continue even if we fail to initialize WA batch
2177 * because we only expect rare glitches but nothing
2178 * critical to prevent us from using GPU
2179 */
2180 DRM_ERROR("WA batch buffer initialization failed: %d\n",
2181 ret);
2182 }
2183
Tvrtko Ursulind038fc72016-12-16 13:18:42 +00002184 return logical_ring_init(engine);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002185}
2186
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +01002187int logical_xcs_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002188{
2189 logical_ring_setup(engine);
2190
2191 return logical_ring_init(engine);
2192}
2193
Jeff McGee0cea6502015-02-13 10:27:56 -06002194static u32
Chris Wilsonc0336662016-05-06 15:40:21 +01002195make_rpcs(struct drm_i915_private *dev_priv)
Jeff McGee0cea6502015-02-13 10:27:56 -06002196{
2197 u32 rpcs = 0;
2198
2199 /*
2200 * No explicit RPCS request is needed to ensure full
2201 * slice/subslice/EU enablement prior to Gen9.
2202 */
Chris Wilsonc0336662016-05-06 15:40:21 +01002203 if (INTEL_GEN(dev_priv) < 9)
Jeff McGee0cea6502015-02-13 10:27:56 -06002204 return 0;
2205
2206 /*
2207 * Starting in Gen9, render power gating can leave
2208 * slice/subslice/EU in a partially enabled state. We
2209 * must make an explicit request through RPCS for full
2210 * enablement.
2211 */
Imre Deak43b67992016-08-31 19:13:02 +03002212 if (INTEL_INFO(dev_priv)->sseu.has_slice_pg) {
Jeff McGee0cea6502015-02-13 10:27:56 -06002213 rpcs |= GEN8_RPCS_S_CNT_ENABLE;
Imre Deakf08a0c92016-08-31 19:13:04 +03002214 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.slice_mask) <<
Jeff McGee0cea6502015-02-13 10:27:56 -06002215 GEN8_RPCS_S_CNT_SHIFT;
2216 rpcs |= GEN8_RPCS_ENABLE;
2217 }
2218
Imre Deak43b67992016-08-31 19:13:02 +03002219 if (INTEL_INFO(dev_priv)->sseu.has_subslice_pg) {
Jeff McGee0cea6502015-02-13 10:27:56 -06002220 rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
Imre Deak57ec1712016-08-31 19:13:05 +03002221 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.subslice_mask) <<
Jeff McGee0cea6502015-02-13 10:27:56 -06002222 GEN8_RPCS_SS_CNT_SHIFT;
2223 rpcs |= GEN8_RPCS_ENABLE;
2224 }
2225
Imre Deak43b67992016-08-31 19:13:02 +03002226 if (INTEL_INFO(dev_priv)->sseu.has_eu_pg) {
2227 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
Jeff McGee0cea6502015-02-13 10:27:56 -06002228 GEN8_RPCS_EU_MIN_SHIFT;
Imre Deak43b67992016-08-31 19:13:02 +03002229 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
Jeff McGee0cea6502015-02-13 10:27:56 -06002230 GEN8_RPCS_EU_MAX_SHIFT;
2231 rpcs |= GEN8_RPCS_ENABLE;
2232 }
2233
2234 return rpcs;
2235}
2236
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002237static u32 intel_lr_indirect_ctx_offset(struct intel_engine_cs *engine)
Michel Thierry71562912016-02-23 10:31:49 +00002238{
2239 u32 indirect_ctx_offset;
2240
Chris Wilsonc0336662016-05-06 15:40:21 +01002241 switch (INTEL_GEN(engine->i915)) {
Michel Thierry71562912016-02-23 10:31:49 +00002242 default:
Chris Wilsonc0336662016-05-06 15:40:21 +01002243 MISSING_CASE(INTEL_GEN(engine->i915));
Michel Thierry71562912016-02-23 10:31:49 +00002244 /* fall through */
Michel Thierry7bd0a2c2017-06-06 13:30:38 -07002245 case 10:
2246 indirect_ctx_offset =
2247 GEN10_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2248 break;
Michel Thierry71562912016-02-23 10:31:49 +00002249 case 9:
2250 indirect_ctx_offset =
2251 GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2252 break;
2253 case 8:
2254 indirect_ctx_offset =
2255 GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2256 break;
2257 }
2258
2259 return indirect_ctx_offset;
2260}
2261
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002262static void execlists_init_reg_state(u32 *regs,
Chris Wilsona3aabe82016-10-04 21:11:26 +01002263 struct i915_gem_context *ctx,
2264 struct intel_engine_cs *engine,
2265 struct intel_ring *ring)
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002266{
Chris Wilsona3aabe82016-10-04 21:11:26 +01002267 struct drm_i915_private *dev_priv = engine->i915;
2268 struct i915_hw_ppgtt *ppgtt = ctx->ppgtt ?: dev_priv->mm.aliasing_ppgtt;
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002269 u32 base = engine->mmio_base;
2270 bool rcs = engine->id == RCS;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002271
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002272 /* A context is actually a big batch buffer with several
2273 * MI_LOAD_REGISTER_IMM commands followed by (reg, value) pairs. The
2274 * values we are setting here are only for the first context restore:
2275 * on a subsequent save, the GPU will recreate this batchbuffer with new
2276 * values (including all the missing MI_LOAD_REGISTER_IMM commands that
2277 * we are not initializing here).
2278 */
2279 regs[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(rcs ? 14 : 11) |
2280 MI_LRI_FORCE_POSTED;
2281
2282 CTX_REG(regs, CTX_CONTEXT_CONTROL, RING_CONTEXT_CONTROL(engine),
Chris Wilson09b1a4e2018-01-25 11:24:42 +00002283 _MASKED_BIT_DISABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
2284 CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT) |
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002285 _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002286 (HAS_RESOURCE_STREAMER(dev_priv) ?
2287 CTX_CTRL_RS_CTX_ENABLE : 0)));
2288 CTX_REG(regs, CTX_RING_HEAD, RING_HEAD(base), 0);
2289 CTX_REG(regs, CTX_RING_TAIL, RING_TAIL(base), 0);
2290 CTX_REG(regs, CTX_RING_BUFFER_START, RING_START(base), 0);
2291 CTX_REG(regs, CTX_RING_BUFFER_CONTROL, RING_CTL(base),
2292 RING_CTL_SIZE(ring->size) | RING_VALID);
2293 CTX_REG(regs, CTX_BB_HEAD_U, RING_BBADDR_UDW(base), 0);
2294 CTX_REG(regs, CTX_BB_HEAD_L, RING_BBADDR(base), 0);
2295 CTX_REG(regs, CTX_BB_STATE, RING_BBSTATE(base), RING_BB_PPGTT);
2296 CTX_REG(regs, CTX_SECOND_BB_HEAD_U, RING_SBBADDR_UDW(base), 0);
2297 CTX_REG(regs, CTX_SECOND_BB_HEAD_L, RING_SBBADDR(base), 0);
2298 CTX_REG(regs, CTX_SECOND_BB_STATE, RING_SBBSTATE(base), 0);
2299 if (rcs) {
Chris Wilson604a8f62017-09-21 14:54:43 +01002300 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
2301
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002302 CTX_REG(regs, CTX_RCS_INDIRECT_CTX, RING_INDIRECT_CTX(base), 0);
2303 CTX_REG(regs, CTX_RCS_INDIRECT_CTX_OFFSET,
2304 RING_INDIRECT_CTX_OFFSET(base), 0);
Chris Wilson604a8f62017-09-21 14:54:43 +01002305 if (wa_ctx->indirect_ctx.size) {
Chris Wilsonbde13eb2016-08-15 10:49:07 +01002306 u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01002307
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002308 regs[CTX_RCS_INDIRECT_CTX + 1] =
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00002309 (ggtt_offset + wa_ctx->indirect_ctx.offset) |
2310 (wa_ctx->indirect_ctx.size / CACHELINE_BYTES);
Arun Siluvery17ee9502015-06-19 19:07:01 +01002311
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002312 regs[CTX_RCS_INDIRECT_CTX_OFFSET + 1] =
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002313 intel_lr_indirect_ctx_offset(engine) << 6;
Chris Wilson604a8f62017-09-21 14:54:43 +01002314 }
2315
2316 CTX_REG(regs, CTX_BB_PER_CTX_PTR, RING_BB_PER_CTX_PTR(base), 0);
2317 if (wa_ctx->per_ctx.size) {
2318 u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01002319
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002320 regs[CTX_BB_PER_CTX_PTR + 1] =
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00002321 (ggtt_offset + wa_ctx->per_ctx.offset) | 0x01;
Arun Siluvery17ee9502015-06-19 19:07:01 +01002322 }
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002323 }
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002324
2325 regs[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9) | MI_LRI_FORCE_POSTED;
2326
2327 CTX_REG(regs, CTX_CTX_TIMESTAMP, RING_CTX_TIMESTAMP(base), 0);
Ville Syrjälä0d925ea2015-11-04 23:20:11 +02002328 /* PDP values well be assigned later if needed */
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002329 CTX_REG(regs, CTX_PDP3_UDW, GEN8_RING_PDP_UDW(engine, 3), 0);
2330 CTX_REG(regs, CTX_PDP3_LDW, GEN8_RING_PDP_LDW(engine, 3), 0);
2331 CTX_REG(regs, CTX_PDP2_UDW, GEN8_RING_PDP_UDW(engine, 2), 0);
2332 CTX_REG(regs, CTX_PDP2_LDW, GEN8_RING_PDP_LDW(engine, 2), 0);
2333 CTX_REG(regs, CTX_PDP1_UDW, GEN8_RING_PDP_UDW(engine, 1), 0);
2334 CTX_REG(regs, CTX_PDP1_LDW, GEN8_RING_PDP_LDW(engine, 1), 0);
2335 CTX_REG(regs, CTX_PDP0_UDW, GEN8_RING_PDP_UDW(engine, 0), 0);
2336 CTX_REG(regs, CTX_PDP0_LDW, GEN8_RING_PDP_LDW(engine, 0), 0);
Michel Thierryd7b26332015-04-08 12:13:34 +01002337
Chris Wilson949e8ab2017-02-09 14:40:36 +00002338 if (ppgtt && i915_vm_is_48bit(&ppgtt->base)) {
Michel Thierry2dba3232015-07-30 11:06:23 +01002339 /* 64b PPGTT (48bit canonical)
2340 * PDP0_DESCRIPTOR contains the base address to PML4 and
2341 * other PDP Descriptors are ignored.
2342 */
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002343 ASSIGN_CTX_PML4(ppgtt, regs);
Michel Thierry2dba3232015-07-30 11:06:23 +01002344 }
2345
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002346 if (rcs) {
2347 regs[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
2348 CTX_REG(regs, CTX_R_PWR_CLK_STATE, GEN8_R_PWR_CLK_STATE,
2349 make_rpcs(dev_priv));
Robert Bragg19f81df2017-06-13 12:23:03 +01002350
2351 i915_oa_init_reg_state(engine, ctx, regs);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002352 }
Chris Wilsona3aabe82016-10-04 21:11:26 +01002353}
2354
2355static int
2356populate_lr_context(struct i915_gem_context *ctx,
2357 struct drm_i915_gem_object *ctx_obj,
2358 struct intel_engine_cs *engine,
2359 struct intel_ring *ring)
2360{
2361 void *vaddr;
Chris Wilsond2b4b972017-11-10 14:26:33 +00002362 u32 *regs;
Chris Wilsona3aabe82016-10-04 21:11:26 +01002363 int ret;
2364
2365 ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
2366 if (ret) {
2367 DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
2368 return ret;
2369 }
2370
2371 vaddr = i915_gem_object_pin_map(ctx_obj, I915_MAP_WB);
2372 if (IS_ERR(vaddr)) {
2373 ret = PTR_ERR(vaddr);
2374 DRM_DEBUG_DRIVER("Could not map object pages! (%d)\n", ret);
2375 return ret;
2376 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002377 ctx_obj->mm.dirty = true;
Chris Wilsona3aabe82016-10-04 21:11:26 +01002378
Chris Wilsond2b4b972017-11-10 14:26:33 +00002379 if (engine->default_state) {
2380 /*
2381 * We only want to copy over the template context state;
2382 * skipping over the headers reserved for GuC communication,
2383 * leaving those as zero.
2384 */
2385 const unsigned long start = LRC_HEADER_PAGES * PAGE_SIZE;
2386 void *defaults;
2387
2388 defaults = i915_gem_object_pin_map(engine->default_state,
2389 I915_MAP_WB);
2390 if (IS_ERR(defaults))
2391 return PTR_ERR(defaults);
2392
2393 memcpy(vaddr + start, defaults + start, engine->context_size);
2394 i915_gem_object_unpin_map(engine->default_state);
2395 }
2396
Chris Wilsona3aabe82016-10-04 21:11:26 +01002397 /* The second page of the context object contains some fields which must
2398 * be set up prior to the first execution. */
Chris Wilsond2b4b972017-11-10 14:26:33 +00002399 regs = vaddr + LRC_STATE_PN * PAGE_SIZE;
2400 execlists_init_reg_state(regs, ctx, engine, ring);
2401 if (!engine->default_state)
2402 regs[CTX_CONTEXT_CONTROL + 1] |=
2403 _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT);
Chris Wilsond6376372018-02-07 21:05:44 +00002404 if (ctx == ctx->i915->preempt_context)
Chris Wilson517aaff2018-01-23 21:04:12 +00002405 regs[CTX_CONTEXT_CONTROL + 1] |=
2406 _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
2407 CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002408
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01002409 i915_gem_object_unpin_map(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002410
2411 return 0;
2412}
2413
Chris Wilsone2efd132016-05-24 14:53:34 +01002414static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
Chris Wilson978f1e02016-04-28 09:56:54 +01002415 struct intel_engine_cs *engine)
Oscar Mateoede7d422014-07-24 17:04:12 +01002416{
Oscar Mateo8c8579172014-07-24 17:04:14 +01002417 struct drm_i915_gem_object *ctx_obj;
Chris Wilson9021ad02016-05-24 14:53:37 +01002418 struct intel_context *ce = &ctx->engine[engine->id];
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002419 struct i915_vma *vma;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002420 uint32_t context_size;
Chris Wilson7e37f882016-08-02 22:50:21 +01002421 struct intel_ring *ring;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002422 int ret;
2423
Chris Wilson1d2a19c2018-01-26 12:18:46 +00002424 if (ce->state)
2425 return 0;
Oscar Mateoede7d422014-07-24 17:04:12 +01002426
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +03002427 context_size = round_up(engine->context_size, I915_GTT_PAGE_SIZE);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002428
Michel Thierry0b29c752017-09-13 09:56:00 +01002429 /*
2430 * Before the actual start of the context image, we insert a few pages
2431 * for our own use and for sharing with the GuC.
2432 */
2433 context_size += LRC_HEADER_PAGES * PAGE_SIZE;
Alex Daid1675192015-08-12 15:43:43 +01002434
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00002435 ctx_obj = i915_gem_object_create(ctx->i915, context_size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01002436 if (IS_ERR(ctx_obj)) {
Dan Carpenter3126a662015-04-30 17:30:50 +03002437 DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
Chris Wilsonfe3db792016-04-25 13:32:13 +01002438 return PTR_ERR(ctx_obj);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002439 }
2440
Chris Wilsona01cb372017-01-16 15:21:30 +00002441 vma = i915_vma_instance(ctx_obj, &ctx->i915->ggtt.base, NULL);
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002442 if (IS_ERR(vma)) {
2443 ret = PTR_ERR(vma);
2444 goto error_deref_obj;
2445 }
2446
Chris Wilson7e37f882016-08-02 22:50:21 +01002447 ring = intel_engine_create_ring(engine, ctx->ring_size);
Chris Wilsondca33ec2016-08-02 22:50:20 +01002448 if (IS_ERR(ring)) {
2449 ret = PTR_ERR(ring);
Nick Hoathe84fe802015-09-11 12:53:46 +01002450 goto error_deref_obj;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002451 }
2452
Chris Wilsondca33ec2016-08-02 22:50:20 +01002453 ret = populate_lr_context(ctx, ctx_obj, engine, ring);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002454 if (ret) {
2455 DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
Chris Wilsondca33ec2016-08-02 22:50:20 +01002456 goto error_ring_free;
Oscar Mateo84c23772014-07-24 17:04:15 +01002457 }
2458
Chris Wilsondca33ec2016-08-02 22:50:20 +01002459 ce->ring = ring;
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002460 ce->state = vma;
Oscar Mateoede7d422014-07-24 17:04:12 +01002461
2462 return 0;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002463
Chris Wilsondca33ec2016-08-02 22:50:20 +01002464error_ring_free:
Chris Wilson7e37f882016-08-02 22:50:21 +01002465 intel_ring_free(ring);
Nick Hoathe84fe802015-09-11 12:53:46 +01002466error_deref_obj:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002467 i915_gem_object_put(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002468 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01002469}
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002470
Chris Wilson821ed7d2016-09-09 14:11:53 +01002471void intel_lr_context_resume(struct drm_i915_private *dev_priv)
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002472{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002473 struct intel_engine_cs *engine;
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002474 struct i915_gem_context *ctx;
Akash Goel3b3f1652016-10-13 22:44:48 +05302475 enum intel_engine_id id;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002476
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002477 /* Because we emit WA_TAIL_DWORDS there may be a disparity
2478 * between our bookkeeping in ce->ring->head and ce->ring->tail and
2479 * that stored in context. As we only write new commands from
2480 * ce->ring->tail onwards, everything before that is junk. If the GPU
2481 * starts reading from its RING_HEAD from the context, it may try to
2482 * execute that junk and die.
2483 *
2484 * So to avoid that we reset the context images upon resume. For
2485 * simplicity, we just zero everything out.
2486 */
Chris Wilson829a0af2017-06-20 12:05:45 +01002487 list_for_each_entry(ctx, &dev_priv->contexts.list, link) {
Akash Goel3b3f1652016-10-13 22:44:48 +05302488 for_each_engine(engine, dev_priv, id) {
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002489 struct intel_context *ce = &ctx->engine[engine->id];
2490 u32 *reg;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002491
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002492 if (!ce->state)
2493 continue;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002494
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002495 reg = i915_gem_object_pin_map(ce->state->obj,
2496 I915_MAP_WB);
2497 if (WARN_ON(IS_ERR(reg)))
2498 continue;
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01002499
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002500 reg += LRC_STATE_PN * PAGE_SIZE / sizeof(*reg);
2501 reg[CTX_RING_HEAD+1] = 0;
2502 reg[CTX_RING_TAIL+1] = 0;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002503
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002504 ce->state->obj->mm.dirty = true;
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002505 i915_gem_object_unpin_map(ce->state->obj);
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002506
Chris Wilsone6ba9992017-04-25 14:00:49 +01002507 intel_ring_reset(ce->ring, 0);
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002508 }
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002509 }
2510}