blob: 964885b5d7cb009c60e4c31e255554e58a6813af [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
Oscar Mateo73e4d072014-07-24 17:04:48 +0100172/**
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000173 * intel_lr_context_descriptor_update() - calculate & cache the descriptor
174 * descriptor for a pinned context
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000175 * @ctx: Context to work on
Chris Wilson9021ad02016-05-24 14:53:37 +0100176 * @engine: Engine the descriptor will be used with
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000177 *
178 * The context descriptor encodes various attributes of a context,
179 * including its GTT address and some flags. Because it's fairly
180 * expensive to calculate, we'll just do it once and cache the result,
181 * which remains valid until the context is unpinned.
182 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200183 * This is what a descriptor looks like, from LSB to MSB::
184 *
Mika Kuoppala2355cf02017-01-27 15:03:09 +0200185 * bits 0-11: flags, GEN8_CTX_* (cached in ctx->desc_template)
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200186 * bits 12-31: LRCA, GTT address of (the HWSP of) this context
187 * bits 32-52: ctx ID, a globally unique tag
188 * bits 53-54: mbz, reserved for use by hardware
189 * bits 55-63: group ID, currently unused and set to 0
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000190 */
191static void
Chris Wilsone2efd132016-05-24 14:53:34 +0100192intel_lr_context_descriptor_update(struct i915_gem_context *ctx,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000193 struct intel_engine_cs *engine)
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000194{
Chris Wilson9021ad02016-05-24 14:53:37 +0100195 struct intel_context *ce = &ctx->engine[engine->id];
Chris Wilson7069b142016-04-28 09:56:52 +0100196 u64 desc;
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000197
Chris Wilson7069b142016-04-28 09:56:52 +0100198 BUILD_BUG_ON(MAX_CONTEXT_HW_ID > (1<<GEN8_CTX_ID_WIDTH));
199
Mika Kuoppala2355cf02017-01-27 15:03:09 +0200200 desc = ctx->desc_template; /* bits 0-11 */
Michel Thierry0b29c752017-09-13 09:56:00 +0100201 desc |= i915_ggtt_offset(ce->state) + LRC_HEADER_PAGES * PAGE_SIZE;
Chris Wilson9021ad02016-05-24 14:53:37 +0100202 /* bits 12-31 */
Chris Wilson7069b142016-04-28 09:56:52 +0100203 desc |= (u64)ctx->hw_id << GEN8_CTX_ID_SHIFT; /* bits 32-52 */
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000204
Chris Wilson9021ad02016-05-24 14:53:37 +0100205 ce->lrc_desc = desc;
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000206}
207
Chris Wilson27606fd2017-09-16 21:44:13 +0100208static struct i915_priolist *
209lookup_priolist(struct intel_engine_cs *engine,
210 struct i915_priotree *pt,
211 int prio)
Chris Wilson08dd3e12017-09-16 21:44:12 +0100212{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300213 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100214 struct i915_priolist *p;
215 struct rb_node **parent, *rb;
216 bool first = true;
217
Mika Kuoppalab620e872017-09-22 15:43:03 +0300218 if (unlikely(execlists->no_priolist))
Chris Wilson08dd3e12017-09-16 21:44:12 +0100219 prio = I915_PRIORITY_NORMAL;
220
221find_priolist:
222 /* most positive priority is scheduled first, equal priorities fifo */
223 rb = NULL;
Mika Kuoppalab620e872017-09-22 15:43:03 +0300224 parent = &execlists->queue.rb_node;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100225 while (*parent) {
226 rb = *parent;
227 p = rb_entry(rb, typeof(*p), node);
228 if (prio > p->priority) {
229 parent = &rb->rb_left;
230 } else if (prio < p->priority) {
231 parent = &rb->rb_right;
232 first = false;
233 } else {
Chris Wilson27606fd2017-09-16 21:44:13 +0100234 return p;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100235 }
236 }
237
238 if (prio == I915_PRIORITY_NORMAL) {
Mika Kuoppalab620e872017-09-22 15:43:03 +0300239 p = &execlists->default_priolist;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100240 } else {
241 p = kmem_cache_alloc(engine->i915->priorities, GFP_ATOMIC);
242 /* Convert an allocation failure to a priority bump */
243 if (unlikely(!p)) {
244 prio = I915_PRIORITY_NORMAL; /* recurses just once */
245
246 /* To maintain ordering with all rendering, after an
247 * allocation failure we have to disable all scheduling.
248 * Requests will then be executed in fifo, and schedule
249 * will ensure that dependencies are emitted in fifo.
250 * There will be still some reordering with existing
251 * requests, so if userspace lied about their
252 * dependencies that reordering may be visible.
253 */
Mika Kuoppalab620e872017-09-22 15:43:03 +0300254 execlists->no_priolist = true;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100255 goto find_priolist;
256 }
257 }
258
259 p->priority = prio;
Chris Wilson27606fd2017-09-16 21:44:13 +0100260 INIT_LIST_HEAD(&p->requests);
Chris Wilson08dd3e12017-09-16 21:44:12 +0100261 rb_link_node(&p->node, rb, parent);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300262 rb_insert_color(&p->node, &execlists->queue);
Chris Wilson08dd3e12017-09-16 21:44:12 +0100263
Chris Wilson08dd3e12017-09-16 21:44:12 +0100264 if (first)
Mika Kuoppalab620e872017-09-22 15:43:03 +0300265 execlists->first = &p->node;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100266
Chris Wilson27606fd2017-09-16 21:44:13 +0100267 return ptr_pack_bits(p, first, 1);
Chris Wilson08dd3e12017-09-16 21:44:12 +0100268}
269
Chris Wilsone61e0f52018-02-21 09:56:36 +0000270static void unwind_wa_tail(struct i915_request *rq)
Chris Wilson7e4992a2017-09-28 20:38:59 +0100271{
272 rq->tail = intel_ring_wrap(rq->ring, rq->wa_tail - WA_TAIL_BYTES);
273 assert_ring_tail_valid(rq->ring, rq->tail);
274}
275
Michał Winiarskia4598d12017-10-25 22:00:18 +0200276static void __unwind_incomplete_requests(struct intel_engine_cs *engine)
Chris Wilson7e4992a2017-09-28 20:38:59 +0100277{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000278 struct i915_request *rq, *rn;
Michał Winiarski097a9482017-09-28 20:39:01 +0100279 struct i915_priolist *uninitialized_var(p);
280 int last_prio = I915_PRIORITY_INVALID;
Chris Wilson7e4992a2017-09-28 20:38:59 +0100281
282 lockdep_assert_held(&engine->timeline->lock);
283
284 list_for_each_entry_safe_reverse(rq, rn,
285 &engine->timeline->requests,
286 link) {
Chris Wilsone61e0f52018-02-21 09:56:36 +0000287 if (i915_request_completed(rq))
Chris Wilson7e4992a2017-09-28 20:38:59 +0100288 return;
289
Chris Wilsone61e0f52018-02-21 09:56:36 +0000290 __i915_request_unsubmit(rq);
Chris Wilson7e4992a2017-09-28 20:38:59 +0100291 unwind_wa_tail(rq);
292
Michał Winiarski097a9482017-09-28 20:39:01 +0100293 GEM_BUG_ON(rq->priotree.priority == I915_PRIORITY_INVALID);
294 if (rq->priotree.priority != last_prio) {
295 p = lookup_priolist(engine,
296 &rq->priotree,
297 rq->priotree.priority);
298 p = ptr_mask_bits(p, 1);
299
300 last_prio = rq->priotree.priority;
301 }
302
303 list_add(&rq->priotree.link, &p->requests);
Chris Wilson7e4992a2017-09-28 20:38:59 +0100304 }
305}
306
Michał Winiarskic41937f2017-10-26 15:35:58 +0200307void
Michał Winiarskia4598d12017-10-25 22:00:18 +0200308execlists_unwind_incomplete_requests(struct intel_engine_execlists *execlists)
309{
310 struct intel_engine_cs *engine =
311 container_of(execlists, typeof(*engine), execlists);
312
313 spin_lock_irq(&engine->timeline->lock);
314 __unwind_incomplete_requests(engine);
315 spin_unlock_irq(&engine->timeline->lock);
316}
317
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100318static inline void
Chris Wilsone61e0f52018-02-21 09:56:36 +0000319execlists_context_status_change(struct i915_request *rq, unsigned long status)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100320{
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100321 /*
322 * Only used when GVT-g is enabled now. When GVT-g is disabled,
323 * The compiler should eliminate this function as dead-code.
324 */
325 if (!IS_ENABLED(CONFIG_DRM_I915_GVT))
326 return;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100327
Changbin Du3fc03062017-03-13 10:47:11 +0800328 atomic_notifier_call_chain(&rq->engine->context_status_notifier,
329 status, rq);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100330}
331
Tvrtko Ursulin73fd9d32017-11-21 18:18:47 +0000332static inline void
Chris Wilsone61e0f52018-02-21 09:56:36 +0000333execlists_context_schedule_in(struct i915_request *rq)
Tvrtko Ursulin73fd9d32017-11-21 18:18:47 +0000334{
335 execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_IN);
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +0000336 intel_engine_context_in(rq->engine);
Tvrtko Ursulin73fd9d32017-11-21 18:18:47 +0000337}
338
339static inline void
Chris Wilsone61e0f52018-02-21 09:56:36 +0000340execlists_context_schedule_out(struct i915_request *rq)
Tvrtko Ursulin73fd9d32017-11-21 18:18:47 +0000341{
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +0000342 intel_engine_context_out(rq->engine);
Tvrtko Ursulin73fd9d32017-11-21 18:18:47 +0000343 execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_OUT);
344}
345
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000346static void
347execlists_update_context_pdps(struct i915_hw_ppgtt *ppgtt, u32 *reg_state)
348{
349 ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
350 ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
351 ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
352 ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
353}
354
Chris Wilsone61e0f52018-02-21 09:56:36 +0000355static u64 execlists_update_context(struct i915_request *rq)
Oscar Mateoae1250b2014-07-24 17:04:37 +0100356{
Chris Wilson70c2a242016-09-09 14:11:46 +0100357 struct intel_context *ce = &rq->ctx->engine[rq->engine->id];
Zhi Wang04da8112017-02-06 18:37:16 +0800358 struct i915_hw_ppgtt *ppgtt =
359 rq->ctx->ppgtt ?: rq->i915->mm.aliasing_ppgtt;
Chris Wilson70c2a242016-09-09 14:11:46 +0100360 u32 *reg_state = ce->lrc_reg_state;
Oscar Mateoae1250b2014-07-24 17:04:37 +0100361
Chris Wilsone6ba9992017-04-25 14:00:49 +0100362 reg_state[CTX_RING_TAIL+1] = intel_ring_set_tail(rq->ring, rq->tail);
Oscar Mateoae1250b2014-07-24 17:04:37 +0100363
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000364 /* True 32b PPGTT with dynamic page allocation: update PDP
365 * registers and point the unallocated PDPs to scratch page.
366 * PML4 is allocated during ppgtt init, so this is not needed
367 * in 48-bit mode.
368 */
Chris Wilson949e8ab2017-02-09 14:40:36 +0000369 if (ppgtt && !i915_vm_is_48bit(&ppgtt->base))
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000370 execlists_update_context_pdps(ppgtt, reg_state);
Chris Wilson70c2a242016-09-09 14:11:46 +0100371
372 return ce->lrc_desc;
Oscar Mateoae1250b2014-07-24 17:04:37 +0100373}
374
Chris Wilsonbeecec92017-10-03 21:34:52 +0100375static inline void elsp_write(u64 desc, u32 __iomem *elsp)
376{
377 writel(upper_32_bits(desc), elsp);
378 writel(lower_32_bits(desc), elsp);
379}
380
Chris Wilson70c2a242016-09-09 14:11:46 +0100381static void execlists_submit_ports(struct intel_engine_cs *engine)
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100382{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300383 struct execlist_port *port = engine->execlists.port;
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100384 unsigned int n;
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100385
Mika Kuoppala76e70082017-09-22 15:43:07 +0300386 for (n = execlists_num_ports(&engine->execlists); n--; ) {
Chris Wilsone61e0f52018-02-21 09:56:36 +0000387 struct i915_request *rq;
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100388 unsigned int count;
389 u64 desc;
Chris Wilson70c2a242016-09-09 14:11:46 +0100390
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100391 rq = port_unpack(&port[n], &count);
392 if (rq) {
393 GEM_BUG_ON(count > !n);
394 if (!count++)
Tvrtko Ursulin73fd9d32017-11-21 18:18:47 +0000395 execlists_context_schedule_in(rq);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100396 port_set(&port[n], port_pack(rq, count));
397 desc = execlists_update_context(rq);
398 GEM_DEBUG_EXEC(port[n].context_id = upper_32_bits(desc));
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000399
400 GEM_TRACE("%s in[%d]: ctx=%d.%d, seqno=%x\n",
401 engine->name, n,
Chris Wilson16c86192017-12-19 22:09:16 +0000402 port[n].context_id, count,
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000403 rq->global_seqno);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100404 } else {
405 GEM_BUG_ON(!n);
406 desc = 0;
407 }
408
Chris Wilson2fc7a062017-12-07 22:24:34 +0000409 elsp_write(desc, engine->execlists.elsp);
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100410 }
Michel Thierryba74cb12017-11-20 12:34:58 +0000411 execlists_clear_active(&engine->execlists, EXECLISTS_ACTIVE_HWACK);
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100412}
413
Chris Wilson70c2a242016-09-09 14:11:46 +0100414static bool ctx_single_port_submission(const struct i915_gem_context *ctx)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100415{
Chris Wilson70c2a242016-09-09 14:11:46 +0100416 return (IS_ENABLED(CONFIG_DRM_I915_GVT) &&
Chris Wilson60958682016-12-31 11:20:11 +0000417 i915_gem_context_force_single_submission(ctx));
Ben Widawsky84b790f2014-07-24 17:04:36 +0100418}
419
Chris Wilson70c2a242016-09-09 14:11:46 +0100420static bool can_merge_ctx(const struct i915_gem_context *prev,
421 const struct i915_gem_context *next)
Michel Thierryacdd8842014-07-24 17:04:38 +0100422{
Chris Wilson70c2a242016-09-09 14:11:46 +0100423 if (prev != next)
424 return false;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100425
Chris Wilson70c2a242016-09-09 14:11:46 +0100426 if (ctx_single_port_submission(prev))
427 return false;
Michel Thierryacdd8842014-07-24 17:04:38 +0100428
Chris Wilson70c2a242016-09-09 14:11:46 +0100429 return true;
430}
Peter Antoine779949f2015-05-11 16:03:27 +0100431
Chris Wilsone61e0f52018-02-21 09:56:36 +0000432static void port_assign(struct execlist_port *port, struct i915_request *rq)
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100433{
434 GEM_BUG_ON(rq == port_request(port));
435
436 if (port_isset(port))
Chris Wilsone61e0f52018-02-21 09:56:36 +0000437 i915_request_put(port_request(port));
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100438
Chris Wilsone61e0f52018-02-21 09:56:36 +0000439 port_set(port, port_pack(i915_request_get(rq), port_count(port)));
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100440}
441
Chris Wilsonbeecec92017-10-03 21:34:52 +0100442static void inject_preempt_context(struct intel_engine_cs *engine)
443{
444 struct intel_context *ce =
445 &engine->i915->preempt_context->engine[engine->id];
Chris Wilsonbeecec92017-10-03 21:34:52 +0100446 unsigned int n;
447
Chris Wilsond6376372018-02-07 21:05:44 +0000448 GEM_BUG_ON(engine->execlists.preempt_complete_status !=
449 upper_32_bits(ce->lrc_desc));
Chris Wilson09b1a4e2018-01-25 11:24:42 +0000450 GEM_BUG_ON((ce->lrc_reg_state[CTX_CONTEXT_CONTROL + 1] &
451 _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
452 CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT)) !=
453 _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
454 CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT));
455
Chris Wilson16a87392017-12-20 09:06:26 +0000456 GEM_TRACE("%s\n", engine->name);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100457 for (n = execlists_num_ports(&engine->execlists); --n; )
Chris Wilson2fc7a062017-12-07 22:24:34 +0000458 elsp_write(0, engine->execlists.elsp);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100459
Chris Wilson2fc7a062017-12-07 22:24:34 +0000460 elsp_write(ce->lrc_desc, engine->execlists.elsp);
Michel Thierryba74cb12017-11-20 12:34:58 +0000461 execlists_clear_active(&engine->execlists, EXECLISTS_ACTIVE_HWACK);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100462}
463
Chris Wilson70c2a242016-09-09 14:11:46 +0100464static void execlists_dequeue(struct intel_engine_cs *engine)
465{
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300466 struct intel_engine_execlists * const execlists = &engine->execlists;
467 struct execlist_port *port = execlists->port;
Mika Kuoppala76e70082017-09-22 15:43:07 +0300468 const struct execlist_port * const last_port =
469 &execlists->port[execlists->port_mask];
Chris Wilsone61e0f52018-02-21 09:56:36 +0000470 struct i915_request *last = port_request(port);
Chris Wilson20311bd2016-11-14 20:41:03 +0000471 struct rb_node *rb;
Chris Wilson70c2a242016-09-09 14:11:46 +0100472 bool submit = false;
Michel Thierryacdd8842014-07-24 17:04:38 +0100473
Chris Wilson70c2a242016-09-09 14:11:46 +0100474 /* Hardware submission is through 2 ports. Conceptually each port
475 * has a (RING_START, RING_HEAD, RING_TAIL) tuple. RING_START is
476 * static for a context, and unique to each, so we only execute
477 * requests belonging to a single context from each ring. RING_HEAD
478 * is maintained by the CS in the context image, it marks the place
479 * where it got up to last time, and through RING_TAIL we tell the CS
480 * where we want to execute up to this time.
481 *
482 * In this list the requests are in order of execution. Consecutive
483 * requests from the same context are adjacent in the ringbuffer. We
484 * can combine these requests into a single RING_TAIL update:
485 *
486 * RING_HEAD...req1...req2
487 * ^- RING_TAIL
488 * since to execute req2 the CS must first execute req1.
489 *
490 * Our goal then is to point each port to the end of a consecutive
491 * sequence of requests as being the most optimal (fewest wake ups
492 * and context switches) submission.
493 */
494
Tvrtko Ursulin9f7886d2017-03-21 10:55:11 +0000495 spin_lock_irq(&engine->timeline->lock);
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300496 rb = execlists->first;
497 GEM_BUG_ON(rb_first(&execlists->queue) != rb);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100498 if (!rb)
499 goto unlock;
500
501 if (last) {
502 /*
503 * Don't resubmit or switch until all outstanding
504 * preemptions (lite-restore) are seen. Then we
505 * know the next preemption status we see corresponds
506 * to this ELSP update.
507 */
Michel Thierryba74cb12017-11-20 12:34:58 +0000508 GEM_BUG_ON(!port_count(&port[0]));
Chris Wilsonbeecec92017-10-03 21:34:52 +0100509 if (port_count(&port[0]) > 1)
510 goto unlock;
511
Michel Thierryba74cb12017-11-20 12:34:58 +0000512 /*
513 * If we write to ELSP a second time before the HW has had
514 * a chance to respond to the previous write, we can confuse
515 * the HW and hit "undefined behaviour". After writing to ELSP,
516 * we must then wait until we see a context-switch event from
517 * the HW to indicate that it has had a chance to respond.
518 */
519 if (!execlists_is_active(execlists, EXECLISTS_ACTIVE_HWACK))
520 goto unlock;
521
Chris Wilsond6376372018-02-07 21:05:44 +0000522 if (engine->i915->preempt_context &&
Chris Wilsonbeecec92017-10-03 21:34:52 +0100523 rb_entry(rb, struct i915_priolist, node)->priority >
524 max(last->priotree.priority, 0)) {
525 /*
526 * Switch to our empty preempt context so
527 * the state of the GPU is known (idle).
528 */
529 inject_preempt_context(engine);
Chris Wilson4a118ec2017-10-23 22:32:36 +0100530 execlists_set_active(execlists,
531 EXECLISTS_ACTIVE_PREEMPT);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100532 goto unlock;
533 } else {
534 /*
535 * In theory, we could coalesce more requests onto
536 * the second port (the first port is active, with
537 * no preemptions pending). However, that means we
538 * then have to deal with the possible lite-restore
539 * of the second port (as we submit the ELSP, there
540 * may be a context-switch) but also we may complete
541 * the resubmission before the context-switch. Ergo,
542 * coalescing onto the second port will cause a
543 * preemption event, but we cannot predict whether
544 * that will affect port[0] or port[1].
545 *
546 * If the second port is already active, we can wait
547 * until the next context-switch before contemplating
548 * new requests. The GPU will be busy and we should be
549 * able to resubmit the new ELSP before it idles,
550 * avoiding pipeline bubbles (momentary pauses where
551 * the driver is unable to keep up the supply of new
552 * work).
553 */
554 if (port_count(&port[1]))
555 goto unlock;
556
557 /* WaIdleLiteRestore:bdw,skl
558 * Apply the wa NOOPs to prevent
Chris Wilsone61e0f52018-02-21 09:56:36 +0000559 * ring:HEAD == rq:TAIL as we resubmit the
Chris Wilsonbeecec92017-10-03 21:34:52 +0100560 * request. See gen8_emit_breadcrumb() for
561 * where we prepare the padding after the
562 * end of the request.
563 */
564 last->tail = last->wa_tail;
565 }
566 }
567
568 do {
Chris Wilson6c067572017-05-17 13:10:03 +0100569 struct i915_priolist *p = rb_entry(rb, typeof(*p), node);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000570 struct i915_request *rq, *rn;
Chris Wilson20311bd2016-11-14 20:41:03 +0000571
Chris Wilson6c067572017-05-17 13:10:03 +0100572 list_for_each_entry_safe(rq, rn, &p->requests, priotree.link) {
573 /*
574 * Can we combine this request with the current port?
575 * It has to be the same context/ringbuffer and not
576 * have any exceptions (e.g. GVT saying never to
577 * combine contexts).
578 *
579 * If we can combine the requests, we can execute both
580 * by updating the RING_TAIL to point to the end of the
581 * second request, and so we never need to tell the
582 * hardware about the first.
Chris Wilson70c2a242016-09-09 14:11:46 +0100583 */
Chris Wilson6c067572017-05-17 13:10:03 +0100584 if (last && !can_merge_ctx(rq->ctx, last->ctx)) {
585 /*
586 * If we are on the second port and cannot
587 * combine this request with the last, then we
588 * are done.
589 */
Mika Kuoppala76e70082017-09-22 15:43:07 +0300590 if (port == last_port) {
Chris Wilson6c067572017-05-17 13:10:03 +0100591 __list_del_many(&p->requests,
592 &rq->priotree.link);
593 goto done;
594 }
Chris Wilson70c2a242016-09-09 14:11:46 +0100595
Chris Wilson6c067572017-05-17 13:10:03 +0100596 /*
597 * If GVT overrides us we only ever submit
598 * port[0], leaving port[1] empty. Note that we
599 * also have to be careful that we don't queue
600 * the same context (even though a different
601 * request) to the second port.
602 */
603 if (ctx_single_port_submission(last->ctx) ||
604 ctx_single_port_submission(rq->ctx)) {
605 __list_del_many(&p->requests,
606 &rq->priotree.link);
607 goto done;
608 }
Chris Wilson70c2a242016-09-09 14:11:46 +0100609
Chris Wilson6c067572017-05-17 13:10:03 +0100610 GEM_BUG_ON(last->ctx == rq->ctx);
Chris Wilson70c2a242016-09-09 14:11:46 +0100611
Chris Wilson6c067572017-05-17 13:10:03 +0100612 if (submit)
613 port_assign(port, last);
614 port++;
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300615
616 GEM_BUG_ON(port_isset(port));
Chris Wilson6c067572017-05-17 13:10:03 +0100617 }
618
619 INIT_LIST_HEAD(&rq->priotree.link);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000620 __i915_request_submit(rq);
621 trace_i915_request_in(rq, port_index(port, execlists));
Chris Wilson6c067572017-05-17 13:10:03 +0100622 last = rq;
623 submit = true;
Chris Wilson70c2a242016-09-09 14:11:46 +0100624 }
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000625
Chris Wilson20311bd2016-11-14 20:41:03 +0000626 rb = rb_next(rb);
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300627 rb_erase(&p->node, &execlists->queue);
Chris Wilson6c067572017-05-17 13:10:03 +0100628 INIT_LIST_HEAD(&p->requests);
629 if (p->priority != I915_PRIORITY_NORMAL)
Chris Wilsonc5cf9a92017-05-17 13:10:04 +0100630 kmem_cache_free(engine->i915->priorities, p);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100631 } while (rb);
Chris Wilson6c067572017-05-17 13:10:03 +0100632done:
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300633 execlists->first = rb;
Chris Wilson6c067572017-05-17 13:10:03 +0100634 if (submit)
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100635 port_assign(port, last);
Chris Wilson339ccd32018-02-15 16:25:53 +0000636
637 /* We must always keep the beast fed if we have work piled up */
Chris Wilson339ccd32018-02-15 16:25:53 +0000638 GEM_BUG_ON(execlists->first && !port_isset(execlists->port));
639
Chris Wilsonbeecec92017-10-03 21:34:52 +0100640unlock:
Tvrtko Ursulin9f7886d2017-03-21 10:55:11 +0000641 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson70c2a242016-09-09 14:11:46 +0100642
Chris Wilson4a118ec2017-10-23 22:32:36 +0100643 if (submit) {
644 execlists_set_active(execlists, EXECLISTS_ACTIVE_USER);
Chris Wilson70c2a242016-09-09 14:11:46 +0100645 execlists_submit_ports(engine);
Chris Wilson4a118ec2017-10-23 22:32:36 +0100646 }
Chris Wilsond081e022018-02-16 15:32:10 +0000647
648 GEM_BUG_ON(port_isset(execlists->port) &&
649 !execlists_is_active(execlists, EXECLISTS_ACTIVE_USER));
Michel Thierryacdd8842014-07-24 17:04:38 +0100650}
651
Michał Winiarskic41937f2017-10-26 15:35:58 +0200652void
Michał Winiarskia4598d12017-10-25 22:00:18 +0200653execlists_cancel_port_requests(struct intel_engine_execlists * const execlists)
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300654{
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100655 struct execlist_port *port = execlists->port;
Mika Kuoppaladc2279e2017-10-10 14:48:57 +0300656 unsigned int num_ports = execlists_num_ports(execlists);
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300657
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100658 while (num_ports-- && port_isset(port)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +0000659 struct i915_request *rq = port_request(port);
Chris Wilson7e44fc22017-09-26 11:17:19 +0100660
Chris Wilson4a118ec2017-10-23 22:32:36 +0100661 GEM_BUG_ON(!execlists->active);
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +0000662 intel_engine_context_out(rq->engine);
Chris Wilsond6c05112017-10-03 21:34:47 +0100663 execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_PREEMPTED);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000664 i915_request_put(rq);
Chris Wilson7e44fc22017-09-26 11:17:19 +0100665
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100666 memset(port, 0, sizeof(*port));
667 port++;
668 }
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300669}
670
Chris Wilson27a5f612017-09-15 18:31:00 +0100671static void execlists_cancel_requests(struct intel_engine_cs *engine)
672{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300673 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilsone61e0f52018-02-21 09:56:36 +0000674 struct i915_request *rq, *rn;
Chris Wilson27a5f612017-09-15 18:31:00 +0100675 struct rb_node *rb;
676 unsigned long flags;
Chris Wilson27a5f612017-09-15 18:31:00 +0100677
678 spin_lock_irqsave(&engine->timeline->lock, flags);
679
680 /* Cancel the requests on the HW and clear the ELSP tracker. */
Michał Winiarskia4598d12017-10-25 22:00:18 +0200681 execlists_cancel_port_requests(execlists);
Chris Wilson27a5f612017-09-15 18:31:00 +0100682
683 /* Mark all executing requests as skipped. */
684 list_for_each_entry(rq, &engine->timeline->requests, link) {
685 GEM_BUG_ON(!rq->global_seqno);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000686 if (!i915_request_completed(rq))
Chris Wilson27a5f612017-09-15 18:31:00 +0100687 dma_fence_set_error(&rq->fence, -EIO);
688 }
689
690 /* Flush the queued requests to the timeline list (for retiring). */
Mika Kuoppalab620e872017-09-22 15:43:03 +0300691 rb = execlists->first;
Chris Wilson27a5f612017-09-15 18:31:00 +0100692 while (rb) {
693 struct i915_priolist *p = rb_entry(rb, typeof(*p), node);
694
695 list_for_each_entry_safe(rq, rn, &p->requests, priotree.link) {
696 INIT_LIST_HEAD(&rq->priotree.link);
Chris Wilson27a5f612017-09-15 18:31:00 +0100697
698 dma_fence_set_error(&rq->fence, -EIO);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000699 __i915_request_submit(rq);
Chris Wilson27a5f612017-09-15 18:31:00 +0100700 }
701
702 rb = rb_next(rb);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300703 rb_erase(&p->node, &execlists->queue);
Chris Wilson27a5f612017-09-15 18:31:00 +0100704 INIT_LIST_HEAD(&p->requests);
705 if (p->priority != I915_PRIORITY_NORMAL)
706 kmem_cache_free(engine->i915->priorities, p);
707 }
708
709 /* Remaining _unready_ requests will be nop'ed when submitted */
710
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300711
Mika Kuoppalab620e872017-09-22 15:43:03 +0300712 execlists->queue = RB_ROOT;
713 execlists->first = NULL;
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100714 GEM_BUG_ON(port_isset(execlists->port));
Chris Wilson27a5f612017-09-15 18:31:00 +0100715
716 /*
717 * The port is checked prior to scheduling a tasklet, but
718 * just in case we have suspended the tasklet to do the
719 * wedging make sure that when it wakes, it decides there
720 * is no work to do by clearing the irq_posted bit.
721 */
722 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
723
724 spin_unlock_irqrestore(&engine->timeline->lock, flags);
725}
726
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200727/*
Oscar Mateo73e4d072014-07-24 17:04:48 +0100728 * Check the unread Context Status Buffers and manage the submission of new
729 * contexts to the ELSP accordingly.
730 */
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +0530731static void execlists_submission_tasklet(unsigned long data)
Thomas Daniele981e7b2014-07-24 17:04:39 +0100732{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300733 struct intel_engine_cs * const engine = (struct intel_engine_cs *)data;
734 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilsonbeecec92017-10-03 21:34:52 +0100735 struct execlist_port * const port = execlists->port;
Chris Wilsonc0336662016-05-06 15:40:21 +0100736 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonbb5db7e2018-01-22 10:07:14 +0000737 bool fw = false;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100738
Chris Wilson48921262017-04-11 18:58:50 +0100739 /* We can skip acquiring intel_runtime_pm_get() here as it was taken
740 * on our behalf by the request (see i915_gem_mark_busy()) and it will
741 * not be relinquished until the device is idle (see
742 * i915_gem_idle_work_handler()). As a precaution, we make sure
743 * that all ELSP are drained i.e. we have processed the CSB,
744 * before allowing ourselves to idle and calling intel_runtime_pm_put().
745 */
746 GEM_BUG_ON(!dev_priv->gt.awake);
747
Chris Wilson899f6202017-03-21 11:33:20 +0000748 /* Prefer doing test_and_clear_bit() as a two stage operation to avoid
749 * imposing the cost of a locked atomic transaction when submitting a
750 * new request (outside of the context-switch interrupt).
751 */
752 while (test_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted)) {
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100753 /* The HWSP contains a (cacheable) mirror of the CSB */
754 const u32 *buf =
755 &engine->status_page.page_addr[I915_HWS_CSB_BUF0_INDEX];
Chris Wilson4af0d722017-03-25 20:10:53 +0000756 unsigned int head, tail;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100757
Mika Kuoppalab620e872017-09-22 15:43:03 +0300758 if (unlikely(execlists->csb_use_mmio)) {
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100759 buf = (u32 * __force)
760 (dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_BUF_LO(engine, 0)));
Mika Kuoppalab620e872017-09-22 15:43:03 +0300761 execlists->csb_head = -1; /* force mmio read of CSB ptrs */
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100762 }
763
Chris Wilson2e70b8c2017-03-23 13:48:03 +0000764 /* The write will be ordered by the uncached read (itself
765 * a memory barrier), so we do not need another in the form
766 * of a locked instruction. The race between the interrupt
767 * handler and the split test/clear is harmless as we order
768 * our clear before the CSB read. If the interrupt arrived
769 * first between the test and the clear, we read the updated
770 * CSB and clear the bit. If the interrupt arrives as we read
771 * the CSB or later (i.e. after we had cleared the bit) the bit
772 * is set and we do a new loop.
773 */
774 __clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300775 if (unlikely(execlists->csb_head == -1)) { /* following a reset */
Chris Wilsonbb5db7e2018-01-22 10:07:14 +0000776 if (!fw) {
777 intel_uncore_forcewake_get(dev_priv,
778 execlists->fw_domains);
779 fw = true;
780 }
781
Chris Wilson767a9832017-09-13 09:56:05 +0100782 head = readl(dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)));
783 tail = GEN8_CSB_WRITE_PTR(head);
784 head = GEN8_CSB_READ_PTR(head);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300785 execlists->csb_head = head;
Chris Wilson767a9832017-09-13 09:56:05 +0100786 } else {
787 const int write_idx =
788 intel_hws_csb_write_index(dev_priv) -
789 I915_HWS_CSB_BUF0_INDEX;
790
Mika Kuoppalab620e872017-09-22 15:43:03 +0300791 head = execlists->csb_head;
Chris Wilson767a9832017-09-13 09:56:05 +0100792 tail = READ_ONCE(buf[write_idx]);
793 }
Chris Wilsonbb5db7e2018-01-22 10:07:14 +0000794 GEM_TRACE("%s cs-irq head=%d [%d%s], tail=%d [%d%s]\n",
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000795 engine->name,
Chris Wilsonbb5db7e2018-01-22 10:07:14 +0000796 head, GEN8_CSB_READ_PTR(readl(dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)))), fw ? "" : "?",
797 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 +0300798
Chris Wilson4af0d722017-03-25 20:10:53 +0000799 while (head != tail) {
Chris Wilsone61e0f52018-02-21 09:56:36 +0000800 struct i915_request *rq;
Chris Wilson4af0d722017-03-25 20:10:53 +0000801 unsigned int status;
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100802 unsigned int count;
Chris Wilsona37951a2017-01-24 11:00:06 +0000803
Chris Wilson4af0d722017-03-25 20:10:53 +0000804 if (++head == GEN8_CSB_ENTRIES)
805 head = 0;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100806
Chris Wilson2ffe80a2017-02-06 17:05:02 +0000807 /* We are flying near dragons again.
808 *
809 * We hold a reference to the request in execlist_port[]
810 * but no more than that. We are operating in softirq
811 * context and so cannot hold any mutex or sleep. That
812 * prevents us stopping the requests we are processing
813 * in port[] from being retired simultaneously (the
814 * breadcrumb will be complete before we see the
815 * context-switch). As we only hold the reference to the
816 * request, any pointer chasing underneath the request
817 * is subject to a potential use-after-free. Thus we
818 * store all of the bookkeeping within port[] as
819 * required, and avoid using unguarded pointers beneath
820 * request itself. The same applies to the atomic
821 * status notifier.
822 */
823
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100824 status = READ_ONCE(buf[2 * head]); /* maybe mmio! */
Chris Wilson193a98d2017-12-22 13:27:42 +0000825 GEM_TRACE("%s csb[%d]: status=0x%08x:0x%08x, active=0x%x\n",
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000826 engine->name, head,
Chris Wilson193a98d2017-12-22 13:27:42 +0000827 status, buf[2*head + 1],
828 execlists->active);
Michel Thierryba74cb12017-11-20 12:34:58 +0000829
830 if (status & (GEN8_CTX_STATUS_IDLE_ACTIVE |
831 GEN8_CTX_STATUS_PREEMPTED))
832 execlists_set_active(execlists,
833 EXECLISTS_ACTIVE_HWACK);
834 if (status & GEN8_CTX_STATUS_ACTIVE_IDLE)
835 execlists_clear_active(execlists,
836 EXECLISTS_ACTIVE_HWACK);
837
Chris Wilson70c2a242016-09-09 14:11:46 +0100838 if (!(status & GEN8_CTX_STATUS_COMPLETED_MASK))
839 continue;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100840
Chris Wilson1f5f9ed2017-11-20 12:34:57 +0000841 /* We should never get a COMPLETED | IDLE_ACTIVE! */
842 GEM_BUG_ON(status & GEN8_CTX_STATUS_IDLE_ACTIVE);
843
Chris Wilsone40dd222017-11-20 12:34:55 +0000844 if (status & GEN8_CTX_STATUS_COMPLETE &&
Chris Wilsond6376372018-02-07 21:05:44 +0000845 buf[2*head + 1] == execlists->preempt_complete_status) {
Chris Wilson193a98d2017-12-22 13:27:42 +0000846 GEM_TRACE("%s preempt-idle\n", engine->name);
847
Michał Winiarskia4598d12017-10-25 22:00:18 +0200848 execlists_cancel_port_requests(execlists);
849 execlists_unwind_incomplete_requests(execlists);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100850
Chris Wilson4a118ec2017-10-23 22:32:36 +0100851 GEM_BUG_ON(!execlists_is_active(execlists,
852 EXECLISTS_ACTIVE_PREEMPT));
853 execlists_clear_active(execlists,
854 EXECLISTS_ACTIVE_PREEMPT);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100855 continue;
856 }
857
858 if (status & GEN8_CTX_STATUS_PREEMPTED &&
Chris Wilson4a118ec2017-10-23 22:32:36 +0100859 execlists_is_active(execlists,
860 EXECLISTS_ACTIVE_PREEMPT))
Chris Wilsonbeecec92017-10-03 21:34:52 +0100861 continue;
862
Chris Wilson4a118ec2017-10-23 22:32:36 +0100863 GEM_BUG_ON(!execlists_is_active(execlists,
864 EXECLISTS_ACTIVE_USER));
865
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100866 rq = port_unpack(port, &count);
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000867 GEM_TRACE("%s out[0]: ctx=%d.%d, seqno=%x\n",
868 engine->name,
Chris Wilson16c86192017-12-19 22:09:16 +0000869 port->context_id, count,
Chris Wilson16a87392017-12-20 09:06:26 +0000870 rq ? rq->global_seqno : 0);
Chris Wilsone0840392018-02-21 15:23:01 +0000871
872 /* Check the context/desc id for this event matches */
873 GEM_DEBUG_BUG_ON(buf[2 * head + 1] != port->context_id);
874
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100875 GEM_BUG_ON(count == 0);
876 if (--count == 0) {
Chris Wilson70c2a242016-09-09 14:11:46 +0100877 GEM_BUG_ON(status & GEN8_CTX_STATUS_PREEMPTED);
Chris Wilsond8747af2017-11-20 12:34:56 +0000878 GEM_BUG_ON(port_isset(&port[1]) &&
879 !(status & GEN8_CTX_STATUS_ELEMENT_SWITCH));
Chris Wilsone61e0f52018-02-21 09:56:36 +0000880 GEM_BUG_ON(!i915_request_completed(rq));
Tvrtko Ursulin73fd9d32017-11-21 18:18:47 +0000881 execlists_context_schedule_out(rq);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000882 trace_i915_request_out(rq);
883 i915_request_put(rq);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100884
Chris Wilson65cb8c02018-02-21 15:15:53 +0000885 GEM_TRACE("%s completed ctx=%d\n",
886 engine->name, port->context_id);
887
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300888 execlists_port_complete(execlists, port);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100889 } else {
890 port_set(port, port_pack(rq, count));
Chris Wilson70c2a242016-09-09 14:11:46 +0100891 }
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000892
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100893 /* After the final element, the hw should be idle */
894 GEM_BUG_ON(port_count(port) == 0 &&
Chris Wilson70c2a242016-09-09 14:11:46 +0100895 !(status & GEN8_CTX_STATUS_ACTIVE_IDLE));
Chris Wilson4a118ec2017-10-23 22:32:36 +0100896 if (port_count(port) == 0)
897 execlists_clear_active(execlists,
898 EXECLISTS_ACTIVE_USER);
Chris Wilson4af0d722017-03-25 20:10:53 +0000899 }
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000900
Mika Kuoppalab620e872017-09-22 15:43:03 +0300901 if (head != execlists->csb_head) {
902 execlists->csb_head = head;
Chris Wilson767a9832017-09-13 09:56:05 +0100903 writel(_MASKED_FIELD(GEN8_CSB_READ_PTR_MASK, head << 8),
904 dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)));
905 }
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000906 }
907
Chris Wilson4a118ec2017-10-23 22:32:36 +0100908 if (!execlists_is_active(execlists, EXECLISTS_ACTIVE_PREEMPT))
Chris Wilson70c2a242016-09-09 14:11:46 +0100909 execlists_dequeue(engine);
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000910
Chris Wilsonbb5db7e2018-01-22 10:07:14 +0000911 if (fw)
912 intel_uncore_forcewake_put(dev_priv, execlists->fw_domains);
Thomas Daniele981e7b2014-07-24 17:04:39 +0100913}
914
Chris Wilson27606fd2017-09-16 21:44:13 +0100915static void insert_request(struct intel_engine_cs *engine,
916 struct i915_priotree *pt,
917 int prio)
918{
919 struct i915_priolist *p = lookup_priolist(engine, pt, prio);
920
921 list_add_tail(&pt->link, &ptr_mask_bits(p, 1)->requests);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100922 if (ptr_unmask_bits(p, 1))
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +0530923 tasklet_hi_schedule(&engine->execlists.tasklet);
Chris Wilson27606fd2017-09-16 21:44:13 +0100924}
925
Chris Wilsone61e0f52018-02-21 09:56:36 +0000926static void execlists_submit_request(struct i915_request *request)
Michel Thierryacdd8842014-07-24 17:04:38 +0100927{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000928 struct intel_engine_cs *engine = request->engine;
Chris Wilson5590af32016-09-09 14:11:54 +0100929 unsigned long flags;
Michel Thierryacdd8842014-07-24 17:04:38 +0100930
Chris Wilson663f71e2016-11-14 20:41:00 +0000931 /* Will be called from irq-context when using foreign fences. */
932 spin_lock_irqsave(&engine->timeline->lock, flags);
Michel Thierryacdd8842014-07-24 17:04:38 +0100933
Chris Wilson27606fd2017-09-16 21:44:13 +0100934 insert_request(engine, &request->priotree, request->priotree.priority);
Michel Thierryacdd8842014-07-24 17:04:38 +0100935
Mika Kuoppalab620e872017-09-22 15:43:03 +0300936 GEM_BUG_ON(!engine->execlists.first);
Chris Wilson6c067572017-05-17 13:10:03 +0100937 GEM_BUG_ON(list_empty(&request->priotree.link));
938
Chris Wilson663f71e2016-11-14 20:41:00 +0000939 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Michel Thierryacdd8842014-07-24 17:04:38 +0100940}
941
Chris Wilsone61e0f52018-02-21 09:56:36 +0000942static struct i915_request *pt_to_request(struct i915_priotree *pt)
Chris Wilson1f181222017-10-03 21:34:50 +0100943{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000944 return container_of(pt, struct i915_request, priotree);
Chris Wilson1f181222017-10-03 21:34:50 +0100945}
946
Chris Wilson20311bd2016-11-14 20:41:03 +0000947static struct intel_engine_cs *
948pt_lock_engine(struct i915_priotree *pt, struct intel_engine_cs *locked)
949{
Chris Wilson1f181222017-10-03 21:34:50 +0100950 struct intel_engine_cs *engine = pt_to_request(pt)->engine;
Chris Wilson20311bd2016-11-14 20:41:03 +0000951
Chris Wilsona79a5242017-03-27 21:21:43 +0100952 GEM_BUG_ON(!locked);
953
Chris Wilson20311bd2016-11-14 20:41:03 +0000954 if (engine != locked) {
Chris Wilsona79a5242017-03-27 21:21:43 +0100955 spin_unlock(&locked->timeline->lock);
956 spin_lock(&engine->timeline->lock);
Chris Wilson20311bd2016-11-14 20:41:03 +0000957 }
958
959 return engine;
960}
961
Chris Wilsone61e0f52018-02-21 09:56:36 +0000962static void execlists_schedule(struct i915_request *request, int prio)
Chris Wilson20311bd2016-11-14 20:41:03 +0000963{
Chris Wilsona79a5242017-03-27 21:21:43 +0100964 struct intel_engine_cs *engine;
Chris Wilson20311bd2016-11-14 20:41:03 +0000965 struct i915_dependency *dep, *p;
966 struct i915_dependency stack;
967 LIST_HEAD(dfs);
968
Chris Wilson7d1ea602017-09-28 20:39:00 +0100969 GEM_BUG_ON(prio == I915_PRIORITY_INVALID);
970
Chris Wilsone61e0f52018-02-21 09:56:36 +0000971 if (i915_request_completed(request))
Chris Wilsonc218ee02018-01-06 10:56:18 +0000972 return;
973
Chris Wilson20311bd2016-11-14 20:41:03 +0000974 if (prio <= READ_ONCE(request->priotree.priority))
975 return;
976
Chris Wilson70cd1472016-11-28 14:36:49 +0000977 /* Need BKL in order to use the temporary link inside i915_dependency */
978 lockdep_assert_held(&request->i915->drm.struct_mutex);
Chris Wilson20311bd2016-11-14 20:41:03 +0000979
980 stack.signaler = &request->priotree;
981 list_add(&stack.dfs_link, &dfs);
982
Chris Wilsonce01b172018-01-02 15:12:26 +0000983 /*
984 * Recursively bump all dependent priorities to match the new request.
Chris Wilson20311bd2016-11-14 20:41:03 +0000985 *
986 * A naive approach would be to use recursion:
987 * static void update_priorities(struct i915_priotree *pt, prio) {
988 * list_for_each_entry(dep, &pt->signalers_list, signal_link)
989 * update_priorities(dep->signal, prio)
990 * insert_request(pt);
991 * }
992 * but that may have unlimited recursion depth and so runs a very
993 * real risk of overunning the kernel stack. Instead, we build
994 * a flat list of all dependencies starting with the current request.
995 * As we walk the list of dependencies, we add all of its dependencies
996 * to the end of the list (this may include an already visited
997 * request) and continue to walk onwards onto the new dependencies. The
998 * end result is a topological list of requests in reverse order, the
999 * last element in the list is the request we must execute first.
1000 */
Chris Wilson2221c5b2018-01-02 15:12:27 +00001001 list_for_each_entry(dep, &dfs, dfs_link) {
Chris Wilson20311bd2016-11-14 20:41:03 +00001002 struct i915_priotree *pt = dep->signaler;
1003
Chris Wilsonce01b172018-01-02 15:12:26 +00001004 /*
1005 * Within an engine, there can be no cycle, but we may
Chris Wilsona79a5242017-03-27 21:21:43 +01001006 * refer to the same dependency chain multiple times
1007 * (redundant dependencies are not eliminated) and across
1008 * engines.
1009 */
1010 list_for_each_entry(p, &pt->signalers_list, signal_link) {
Chris Wilsonce01b172018-01-02 15:12:26 +00001011 GEM_BUG_ON(p == dep); /* no cycles! */
1012
Chris Wilson83cc84c2018-01-02 15:12:25 +00001013 if (i915_priotree_signaled(p->signaler))
Chris Wilson1f181222017-10-03 21:34:50 +01001014 continue;
1015
Chris Wilsona79a5242017-03-27 21:21:43 +01001016 GEM_BUG_ON(p->signaler->priority < pt->priority);
Chris Wilson20311bd2016-11-14 20:41:03 +00001017 if (prio > READ_ONCE(p->signaler->priority))
1018 list_move_tail(&p->dfs_link, &dfs);
Chris Wilsona79a5242017-03-27 21:21:43 +01001019 }
Chris Wilson20311bd2016-11-14 20:41:03 +00001020 }
1021
Chris Wilsonce01b172018-01-02 15:12:26 +00001022 /*
1023 * If we didn't need to bump any existing priorities, and we haven't
Chris Wilson349bdb62017-05-17 13:10:05 +01001024 * yet submitted this request (i.e. there is no potential race with
1025 * execlists_submit_request()), we can set our own priority and skip
1026 * acquiring the engine locks.
1027 */
Chris Wilson7d1ea602017-09-28 20:39:00 +01001028 if (request->priotree.priority == I915_PRIORITY_INVALID) {
Chris Wilson349bdb62017-05-17 13:10:05 +01001029 GEM_BUG_ON(!list_empty(&request->priotree.link));
1030 request->priotree.priority = prio;
1031 if (stack.dfs_link.next == stack.dfs_link.prev)
1032 return;
1033 __list_del_entry(&stack.dfs_link);
1034 }
1035
Chris Wilsona79a5242017-03-27 21:21:43 +01001036 engine = request->engine;
1037 spin_lock_irq(&engine->timeline->lock);
1038
Chris Wilson20311bd2016-11-14 20:41:03 +00001039 /* Fifo and depth-first replacement ensure our deps execute before us */
1040 list_for_each_entry_safe_reverse(dep, p, &dfs, dfs_link) {
1041 struct i915_priotree *pt = dep->signaler;
1042
1043 INIT_LIST_HEAD(&dep->dfs_link);
1044
1045 engine = pt_lock_engine(pt, engine);
1046
1047 if (prio <= pt->priority)
1048 continue;
1049
Chris Wilson20311bd2016-11-14 20:41:03 +00001050 pt->priority = prio;
Chris Wilson6c067572017-05-17 13:10:03 +01001051 if (!list_empty(&pt->link)) {
1052 __list_del_entry(&pt->link);
1053 insert_request(engine, pt, prio);
Chris Wilsona79a5242017-03-27 21:21:43 +01001054 }
Chris Wilson20311bd2016-11-14 20:41:03 +00001055 }
1056
Chris Wilsona79a5242017-03-27 21:21:43 +01001057 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson20311bd2016-11-14 20:41:03 +00001058}
1059
Chris Wilsonf4e15af2017-11-10 14:26:32 +00001060static int __context_pin(struct i915_gem_context *ctx, struct i915_vma *vma)
1061{
1062 unsigned int flags;
1063 int err;
1064
1065 /*
1066 * Clear this page out of any CPU caches for coherent swap-in/out.
1067 * We only want to do this on the first bind so that we do not stall
1068 * on an active context (which by nature is already on the GPU).
1069 */
1070 if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
1071 err = i915_gem_object_set_to_gtt_domain(vma->obj, true);
1072 if (err)
1073 return err;
1074 }
1075
1076 flags = PIN_GLOBAL | PIN_HIGH;
1077 if (ctx->ggtt_offset_bias)
1078 flags |= PIN_OFFSET_BIAS | ctx->ggtt_offset_bias;
1079
1080 return i915_vma_pin(vma, 0, GEN8_LR_CONTEXT_ALIGN, flags);
1081}
1082
Chris Wilson266a2402017-05-04 10:33:08 +01001083static struct intel_ring *
1084execlists_context_pin(struct intel_engine_cs *engine,
1085 struct i915_gem_context *ctx)
Oscar Mateodcb4c122014-11-13 10:28:10 +00001086{
Chris Wilson9021ad02016-05-24 14:53:37 +01001087 struct intel_context *ce = &ctx->engine[engine->id];
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001088 void *vaddr;
Tvrtko Ursulinca825802016-01-15 15:10:27 +00001089 int ret;
Oscar Mateodcb4c122014-11-13 10:28:10 +00001090
Chris Wilson91c8a322016-07-05 10:40:23 +01001091 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Tvrtko Ursulinca825802016-01-15 15:10:27 +00001092
Chris Wilson266a2402017-05-04 10:33:08 +01001093 if (likely(ce->pin_count++))
1094 goto out;
Chris Wilsona533b4b2017-03-16 17:16:28 +00001095 GEM_BUG_ON(!ce->pin_count); /* no overflow please! */
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001096
Chris Wilson1d2a19c2018-01-26 12:18:46 +00001097 ret = execlists_context_deferred_alloc(ctx, engine);
1098 if (ret)
1099 goto err;
Chris Wilson56f6e0a2017-01-05 15:30:20 +00001100 GEM_BUG_ON(!ce->state);
Chris Wilsone8a9c582016-12-18 15:37:20 +00001101
Chris Wilsonf4e15af2017-11-10 14:26:32 +00001102 ret = __context_pin(ctx, ce->state);
Nick Hoathe84fe802015-09-11 12:53:46 +01001103 if (ret)
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001104 goto err;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001105
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001106 vaddr = i915_gem_object_pin_map(ce->state->obj, I915_MAP_WB);
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001107 if (IS_ERR(vaddr)) {
1108 ret = PTR_ERR(vaddr);
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001109 goto unpin_vma;
Tvrtko Ursulin82352e92016-01-15 17:12:45 +00001110 }
1111
Chris Wilsond822bb12017-04-03 12:34:25 +01001112 ret = intel_ring_pin(ce->ring, ctx->i915, ctx->ggtt_offset_bias);
Nick Hoathe84fe802015-09-11 12:53:46 +01001113 if (ret)
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001114 goto unpin_map;
Alex Daid1675192015-08-12 15:43:43 +01001115
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001116 intel_lr_context_descriptor_update(ctx, engine);
Chris Wilson9021ad02016-05-24 14:53:37 +01001117
Chris Wilsona3aabe82016-10-04 21:11:26 +01001118 ce->lrc_reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
1119 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001120 i915_ggtt_offset(ce->ring->vma);
Chris Wilsona3aabe82016-10-04 21:11:26 +01001121
Chris Wilson3d574a62017-10-13 21:26:16 +01001122 ce->state->obj->pin_global++;
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001123 i915_gem_context_get(ctx);
Chris Wilson266a2402017-05-04 10:33:08 +01001124out:
1125 return ce->ring;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001126
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001127unpin_map:
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001128 i915_gem_object_unpin_map(ce->state->obj);
1129unpin_vma:
1130 __i915_vma_unpin(ce->state);
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001131err:
Chris Wilson9021ad02016-05-24 14:53:37 +01001132 ce->pin_count = 0;
Chris Wilson266a2402017-05-04 10:33:08 +01001133 return ERR_PTR(ret);
Oscar Mateodcb4c122014-11-13 10:28:10 +00001134}
1135
Chris Wilsone8a9c582016-12-18 15:37:20 +00001136static void execlists_context_unpin(struct intel_engine_cs *engine,
1137 struct i915_gem_context *ctx)
Oscar Mateodcb4c122014-11-13 10:28:10 +00001138{
Chris Wilson9021ad02016-05-24 14:53:37 +01001139 struct intel_context *ce = &ctx->engine[engine->id];
Daniel Vetteraf3302b2015-12-04 17:27:15 +01001140
Chris Wilson91c8a322016-07-05 10:40:23 +01001141 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilson9021ad02016-05-24 14:53:37 +01001142 GEM_BUG_ON(ce->pin_count == 0);
Tvrtko Ursulin321fe302016-01-28 10:29:55 +00001143
Chris Wilson9021ad02016-05-24 14:53:37 +01001144 if (--ce->pin_count)
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001145 return;
1146
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001147 intel_ring_unpin(ce->ring);
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001148
Chris Wilson3d574a62017-10-13 21:26:16 +01001149 ce->state->obj->pin_global--;
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001150 i915_gem_object_unpin_map(ce->state->obj);
1151 i915_vma_unpin(ce->state);
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001152
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001153 i915_gem_context_put(ctx);
Oscar Mateodcb4c122014-11-13 10:28:10 +00001154}
1155
Chris Wilsone61e0f52018-02-21 09:56:36 +00001156static int execlists_request_alloc(struct i915_request *request)
Chris Wilsonef11c012016-12-18 15:37:19 +00001157{
1158 struct intel_engine_cs *engine = request->engine;
1159 struct intel_context *ce = &request->ctx->engine[engine->id];
Chris Wilsonfd138212017-11-15 15:12:04 +00001160 int ret;
Chris Wilsonef11c012016-12-18 15:37:19 +00001161
Chris Wilsone8a9c582016-12-18 15:37:20 +00001162 GEM_BUG_ON(!ce->pin_count);
1163
Chris Wilsonef11c012016-12-18 15:37:19 +00001164 /* Flush enough space to reduce the likelihood of waiting after
1165 * we start building the request - in which case we will just
1166 * have to repeat work.
1167 */
1168 request->reserved_space += EXECLISTS_REQUEST_SIZE;
1169
Chris Wilsonfd138212017-11-15 15:12:04 +00001170 ret = intel_ring_wait_for_space(request->ring, request->reserved_space);
1171 if (ret)
1172 return ret;
Chris Wilsonef11c012016-12-18 15:37:19 +00001173
Chris Wilsonef11c012016-12-18 15:37:19 +00001174 /* Note that after this point, we have committed to using
1175 * this request as it is being used to both track the
1176 * state of engine initialisation and liveness of the
1177 * golden renderstate above. Think twice before you try
1178 * to cancel/unwind this request now.
1179 */
1180
1181 request->reserved_space -= EXECLISTS_REQUEST_SIZE;
1182 return 0;
Chris Wilsonef11c012016-12-18 15:37:19 +00001183}
1184
Arun Siluvery9e000842015-07-03 14:27:31 +01001185/*
1186 * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
1187 * PIPE_CONTROL instruction. This is required for the flush to happen correctly
1188 * but there is a slight complication as this is applied in WA batch where the
1189 * values are only initialized once so we cannot take register value at the
1190 * beginning and reuse it further; hence we save its value to memory, upload a
1191 * constant value with bit21 set and then we restore it back with the saved value.
1192 * To simplify the WA, a constant value is formed by using the default value
1193 * of this register. This shouldn't be a problem because we are only modifying
1194 * it for a short period and this batch in non-premptible. We can ofcourse
1195 * use additional instructions that read the actual value of the register
1196 * at that time and set our bit of interest but it makes the WA complicated.
1197 *
1198 * This WA is also required for Gen9 so extracting as a function avoids
1199 * code duplication.
1200 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001201static u32 *
1202gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery9e000842015-07-03 14:27:31 +01001203{
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001204 *batch++ = MI_STORE_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1205 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1206 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
1207 *batch++ = 0;
Arun Siluvery9e000842015-07-03 14:27:31 +01001208
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001209 *batch++ = MI_LOAD_REGISTER_IMM(1);
1210 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1211 *batch++ = 0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES;
Arun Siluvery9e000842015-07-03 14:27:31 +01001212
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001213 batch = gen8_emit_pipe_control(batch,
1214 PIPE_CONTROL_CS_STALL |
1215 PIPE_CONTROL_DC_FLUSH_ENABLE,
1216 0);
Arun Siluvery9e000842015-07-03 14:27:31 +01001217
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001218 *batch++ = MI_LOAD_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1219 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1220 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
1221 *batch++ = 0;
Arun Siluvery9e000842015-07-03 14:27:31 +01001222
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001223 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001224}
1225
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001226/*
1227 * Typically we only have one indirect_ctx and per_ctx batch buffer which are
1228 * initialized at the beginning and shared across all contexts but this field
1229 * helps us to have multiple batches at different offsets and select them based
1230 * on a criteria. At the moment this batch always start at the beginning of the page
1231 * and at this point we don't have multiple wa_ctx batch buffers.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001232 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001233 * The number of WA applied are not known at the beginning; we use this field
1234 * to return the no of DWORDS written.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001235 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001236 * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
1237 * so it adds NOOPs as padding to make it cacheline aligned.
1238 * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
1239 * makes a complete batch buffer.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001240 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001241static u32 *gen8_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001242{
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001243 /* WaDisableCtxRestoreArbitration:bdw,chv */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001244 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001245
Arun Siluveryc82435b2015-06-19 18:37:13 +01001246 /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001247 if (IS_BROADWELL(engine->i915))
1248 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
Arun Siluveryc82435b2015-06-19 18:37:13 +01001249
Arun Siluvery0160f052015-06-23 15:46:57 +01001250 /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
1251 /* Actual scratch location is at 128 bytes offset */
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001252 batch = gen8_emit_pipe_control(batch,
1253 PIPE_CONTROL_FLUSH_L3 |
1254 PIPE_CONTROL_GLOBAL_GTT_IVB |
1255 PIPE_CONTROL_CS_STALL |
1256 PIPE_CONTROL_QW_WRITE,
1257 i915_ggtt_offset(engine->scratch) +
1258 2 * CACHELINE_BYTES);
Arun Siluvery0160f052015-06-23 15:46:57 +01001259
Chris Wilsonbeecec92017-10-03 21:34:52 +01001260 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1261
Arun Siluvery17ee9502015-06-19 19:07:01 +01001262 /* Pad to end of cacheline */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001263 while ((unsigned long)batch % CACHELINE_BYTES)
1264 *batch++ = MI_NOOP;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001265
1266 /*
1267 * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
1268 * execution depends on the length specified in terms of cache lines
1269 * in the register CTX_RCS_INDIRECT_CTX
1270 */
1271
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001272 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001273}
1274
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001275static u32 *gen9_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery0504cff2015-07-14 15:01:27 +01001276{
Chris Wilsonbeecec92017-10-03 21:34:52 +01001277 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
1278
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001279 /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt,glk */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001280 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
Arun Siluverya4106a72015-07-14 15:01:29 +01001281
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001282 /* WaDisableGatherAtSetShaderCommonSlice:skl,bxt,kbl,glk */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001283 *batch++ = MI_LOAD_REGISTER_IMM(1);
1284 *batch++ = i915_mmio_reg_offset(COMMON_SLICE_CHICKEN2);
1285 *batch++ = _MASKED_BIT_DISABLE(
1286 GEN9_DISABLE_GATHER_AT_SET_SHADER_COMMON_SLICE);
1287 *batch++ = MI_NOOP;
Mika Kuoppala873e8172016-07-20 14:26:13 +03001288
Mika Kuoppala066d4622016-06-07 17:19:15 +03001289 /* WaClearSlmSpaceAtContextSwitch:kbl */
1290 /* Actual scratch location is at 128 bytes offset */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001291 if (IS_KBL_REVID(engine->i915, 0, KBL_REVID_A0)) {
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001292 batch = gen8_emit_pipe_control(batch,
1293 PIPE_CONTROL_FLUSH_L3 |
1294 PIPE_CONTROL_GLOBAL_GTT_IVB |
1295 PIPE_CONTROL_CS_STALL |
1296 PIPE_CONTROL_QW_WRITE,
1297 i915_ggtt_offset(engine->scratch)
1298 + 2 * CACHELINE_BYTES);
Mika Kuoppala066d4622016-06-07 17:19:15 +03001299 }
Tim Gore3485d992016-07-05 10:01:30 +01001300
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001301 /* WaMediaPoolStateCmdInWABB:bxt,glk */
Tim Gore3485d992016-07-05 10:01:30 +01001302 if (HAS_POOLED_EU(engine->i915)) {
1303 /*
1304 * EU pool configuration is setup along with golden context
1305 * during context initialization. This value depends on
1306 * device type (2x6 or 3x6) and needs to be updated based
1307 * on which subslice is disabled especially for 2x6
1308 * devices, however it is safe to load default
1309 * configuration of 3x6 device instead of masking off
1310 * corresponding bits because HW ignores bits of a disabled
1311 * subslice and drops down to appropriate config. Please
1312 * see render_state_setup() in i915_gem_render_state.c for
1313 * possible configurations, to avoid duplication they are
1314 * not shown here again.
1315 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001316 *batch++ = GEN9_MEDIA_POOL_STATE;
1317 *batch++ = GEN9_MEDIA_POOL_ENABLE;
1318 *batch++ = 0x00777000;
1319 *batch++ = 0;
1320 *batch++ = 0;
1321 *batch++ = 0;
Tim Gore3485d992016-07-05 10:01:30 +01001322 }
1323
Chris Wilsonbeecec92017-10-03 21:34:52 +01001324 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1325
Arun Siluvery0504cff2015-07-14 15:01:27 +01001326 /* Pad to end of cacheline */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001327 while ((unsigned long)batch % CACHELINE_BYTES)
1328 *batch++ = MI_NOOP;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001329
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001330 return batch;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001331}
1332
Rafael Antognolli4b6ce682018-02-05 15:33:30 -08001333static u32 *
1334gen10_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
1335{
1336 int i;
1337
1338 /*
1339 * WaPipeControlBefore3DStateSamplePattern: cnl
1340 *
1341 * Ensure the engine is idle prior to programming a
1342 * 3DSTATE_SAMPLE_PATTERN during a context restore.
1343 */
1344 batch = gen8_emit_pipe_control(batch,
1345 PIPE_CONTROL_CS_STALL,
1346 0);
1347 /*
1348 * WaPipeControlBefore3DStateSamplePattern says we need 4 dwords for
1349 * the PIPE_CONTROL followed by 12 dwords of 0x0, so 16 dwords in
1350 * total. However, a PIPE_CONTROL is 6 dwords long, not 4, which is
1351 * confusing. Since gen8_emit_pipe_control() already advances the
1352 * batch by 6 dwords, we advance the other 10 here, completing a
1353 * cacheline. It's not clear if the workaround requires this padding
1354 * before other commands, or if it's just the regular padding we would
1355 * already have for the workaround bb, so leave it here for now.
1356 */
1357 for (i = 0; i < 10; i++)
1358 *batch++ = MI_NOOP;
1359
1360 /* Pad to end of cacheline */
1361 while ((unsigned long)batch % CACHELINE_BYTES)
1362 *batch++ = MI_NOOP;
1363
1364 return batch;
1365}
1366
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001367#define CTX_WA_BB_OBJ_SIZE (PAGE_SIZE)
1368
1369static int lrc_setup_wa_ctx(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001370{
Chris Wilson48bb74e2016-08-15 10:49:04 +01001371 struct drm_i915_gem_object *obj;
1372 struct i915_vma *vma;
1373 int err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001374
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001375 obj = i915_gem_object_create(engine->i915, CTX_WA_BB_OBJ_SIZE);
Chris Wilson48bb74e2016-08-15 10:49:04 +01001376 if (IS_ERR(obj))
1377 return PTR_ERR(obj);
1378
Chris Wilsona01cb372017-01-16 15:21:30 +00001379 vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
Chris Wilson48bb74e2016-08-15 10:49:04 +01001380 if (IS_ERR(vma)) {
1381 err = PTR_ERR(vma);
1382 goto err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001383 }
1384
Chris Wilson48bb74e2016-08-15 10:49:04 +01001385 err = i915_vma_pin(vma, 0, PAGE_SIZE, PIN_GLOBAL | PIN_HIGH);
1386 if (err)
1387 goto err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001388
Chris Wilson48bb74e2016-08-15 10:49:04 +01001389 engine->wa_ctx.vma = vma;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001390 return 0;
Chris Wilson48bb74e2016-08-15 10:49:04 +01001391
1392err:
1393 i915_gem_object_put(obj);
1394 return err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001395}
1396
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001397static void lrc_destroy_wa_ctx(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001398{
Chris Wilson19880c42016-08-15 10:49:05 +01001399 i915_vma_unpin_and_release(&engine->wa_ctx.vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001400}
1401
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001402typedef u32 *(*wa_bb_func_t)(struct intel_engine_cs *engine, u32 *batch);
1403
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001404static int intel_init_workaround_bb(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001405{
Chris Wilson48bb74e2016-08-15 10:49:04 +01001406 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001407 struct i915_wa_ctx_bb *wa_bb[2] = { &wa_ctx->indirect_ctx,
1408 &wa_ctx->per_ctx };
1409 wa_bb_func_t wa_bb_fn[2];
Arun Siluvery17ee9502015-06-19 19:07:01 +01001410 struct page *page;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001411 void *batch, *batch_ptr;
1412 unsigned int i;
Chris Wilson48bb74e2016-08-15 10:49:04 +01001413 int ret;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001414
Tvrtko Ursulin10bde232018-01-19 10:00:04 +00001415 if (GEM_WARN_ON(engine->id != RCS))
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001416 return -EINVAL;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001417
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001418 switch (INTEL_GEN(engine->i915)) {
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001419 case 10:
Rafael Antognolli4b6ce682018-02-05 15:33:30 -08001420 wa_bb_fn[0] = gen10_init_indirectctx_bb;
1421 wa_bb_fn[1] = NULL;
1422 break;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001423 case 9:
1424 wa_bb_fn[0] = gen9_init_indirectctx_bb;
Chris Wilsonb8aa2232017-09-21 14:54:44 +01001425 wa_bb_fn[1] = NULL;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001426 break;
1427 case 8:
1428 wa_bb_fn[0] = gen8_init_indirectctx_bb;
Chris Wilson3ad7b522017-10-03 21:34:49 +01001429 wa_bb_fn[1] = NULL;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001430 break;
1431 default:
1432 MISSING_CASE(INTEL_GEN(engine->i915));
Arun Siluvery5e60d792015-06-23 15:50:44 +01001433 return 0;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001434 }
Arun Siluvery5e60d792015-06-23 15:50:44 +01001435
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001436 ret = lrc_setup_wa_ctx(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001437 if (ret) {
1438 DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
1439 return ret;
1440 }
1441
Chris Wilson48bb74e2016-08-15 10:49:04 +01001442 page = i915_gem_object_get_dirty_page(wa_ctx->vma->obj, 0);
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001443 batch = batch_ptr = kmap_atomic(page);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001444
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001445 /*
1446 * Emit the two workaround batch buffers, recording the offset from the
1447 * start of the workaround batch buffer object for each and their
1448 * respective sizes.
1449 */
1450 for (i = 0; i < ARRAY_SIZE(wa_bb_fn); i++) {
1451 wa_bb[i]->offset = batch_ptr - batch;
Chris Wilson1d2a19c2018-01-26 12:18:46 +00001452 if (GEM_WARN_ON(!IS_ALIGNED(wa_bb[i]->offset,
1453 CACHELINE_BYTES))) {
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001454 ret = -EINVAL;
1455 break;
1456 }
Chris Wilson604a8f62017-09-21 14:54:43 +01001457 if (wa_bb_fn[i])
1458 batch_ptr = wa_bb_fn[i](engine, batch_ptr);
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001459 wa_bb[i]->size = batch_ptr - (batch + wa_bb[i]->offset);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001460 }
1461
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001462 BUG_ON(batch_ptr - batch > CTX_WA_BB_OBJ_SIZE);
1463
Arun Siluvery17ee9502015-06-19 19:07:01 +01001464 kunmap_atomic(batch);
1465 if (ret)
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001466 lrc_destroy_wa_ctx(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001467
1468 return ret;
1469}
1470
Chris Wilson64f09f02017-08-07 13:19:19 +01001471static u8 gtiir[] = {
1472 [RCS] = 0,
1473 [BCS] = 0,
1474 [VCS] = 1,
1475 [VCS2] = 1,
1476 [VECS] = 3,
1477};
1478
Chris Wilsonf3c9d4072018-01-02 15:12:34 +00001479static void enable_execlists(struct intel_engine_cs *engine)
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001480{
Chris Wilsonc0336662016-05-06 15:40:21 +01001481 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonf3c9d4072018-01-02 15:12:34 +00001482
1483 I915_WRITE(RING_HWSTAM(engine->mmio_base), 0xffffffff);
Kelvin Gardiner225701f2018-01-30 11:49:17 -02001484
1485 /*
1486 * Make sure we're not enabling the new 12-deep CSB
1487 * FIFO as that requires a slightly updated handling
1488 * in the ctx switch irq. Since we're currently only
1489 * using only 2 elements of the enhanced execlists the
1490 * deeper FIFO it's not needed and it's not worth adding
1491 * more statements to the irq handler to support it.
1492 */
1493 if (INTEL_GEN(dev_priv) >= 11)
1494 I915_WRITE(RING_MODE_GEN7(engine),
1495 _MASKED_BIT_DISABLE(GEN11_GFX_DISABLE_LEGACY_MODE));
1496 else
1497 I915_WRITE(RING_MODE_GEN7(engine),
1498 _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
1499
Chris Wilsonf3c9d4072018-01-02 15:12:34 +00001500 I915_WRITE(RING_HWS_PGA(engine->mmio_base),
1501 engine->status_page.ggtt_offset);
1502 POSTING_READ(RING_HWS_PGA(engine->mmio_base));
Chris Wilsone8401302018-02-05 15:24:30 +00001503
1504 /* Following the reset, we need to reload the CSB read/write pointers */
1505 engine->execlists.csb_head = -1;
Chris Wilsonf3c9d4072018-01-02 15:12:34 +00001506}
1507
1508static int gen8_init_common_ring(struct intel_engine_cs *engine)
1509{
Mika Kuoppalab620e872017-09-22 15:43:03 +03001510 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001511 int ret;
1512
1513 ret = intel_mocs_init_engine(engine);
1514 if (ret)
1515 return ret;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001516
Chris Wilsonad07dfc2016-10-07 07:53:26 +01001517 intel_engine_reset_breadcrumbs(engine);
Chris Wilsonf3b8f912017-01-05 15:30:21 +00001518 intel_engine_init_hangcheck(engine);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001519
Chris Wilsonf3c9d4072018-01-02 15:12:34 +00001520 enable_execlists(engine);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001521
Chris Wilson64f09f02017-08-07 13:19:19 +01001522 /* After a GPU reset, we may have requests to replay */
Michał Winiarski9bdc3572017-10-25 18:25:19 +01001523 if (execlists->first)
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05301524 tasklet_schedule(&execlists->tasklet);
Chris Wilson6b764a52017-04-25 11:38:35 +01001525
Chris Wilson821ed7d2016-09-09 14:11:53 +01001526 return 0;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001527}
1528
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001529static int gen8_init_render_ring(struct intel_engine_cs *engine)
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001530{
Chris Wilsonc0336662016-05-06 15:40:21 +01001531 struct drm_i915_private *dev_priv = engine->i915;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001532 int ret;
1533
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001534 ret = gen8_init_common_ring(engine);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001535 if (ret)
1536 return ret;
1537
1538 /* We need to disable the AsyncFlip performance optimisations in order
1539 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1540 * programmed to '1' on all products.
1541 *
1542 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
1543 */
1544 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1545
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001546 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1547
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001548 return init_workarounds_ring(engine);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001549}
1550
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001551static int gen9_init_render_ring(struct intel_engine_cs *engine)
Damien Lespiau82ef8222015-02-09 19:33:08 +00001552{
1553 int ret;
1554
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001555 ret = gen8_init_common_ring(engine);
Damien Lespiau82ef8222015-02-09 19:33:08 +00001556 if (ret)
1557 return ret;
1558
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001559 return init_workarounds_ring(engine);
Damien Lespiau82ef8222015-02-09 19:33:08 +00001560}
1561
Chris Wilson42232212018-01-02 15:12:32 +00001562static void reset_irq(struct intel_engine_cs *engine)
1563{
1564 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson274de872018-02-02 14:54:55 +00001565 int i;
Chris Wilson42232212018-01-02 15:12:32 +00001566
Chris Wilsone8401302018-02-05 15:24:30 +00001567 GEM_BUG_ON(engine->id >= ARRAY_SIZE(gtiir));
1568
Chris Wilson42232212018-01-02 15:12:32 +00001569 /*
1570 * Clear any pending interrupt state.
1571 *
1572 * We do it twice out of paranoia that some of the IIR are double
1573 * buffered, and if we only reset it once there may still be
1574 * an interrupt pending.
1575 */
Chris Wilson274de872018-02-02 14:54:55 +00001576 for (i = 0; i < 2; i++) {
1577 I915_WRITE(GEN8_GT_IIR(gtiir[engine->id]),
1578 GT_CONTEXT_SWITCH_INTERRUPT << engine->irq_shift);
1579 POSTING_READ(GEN8_GT_IIR(gtiir[engine->id]));
1580 }
1581 GEM_BUG_ON(I915_READ(GEN8_GT_IIR(gtiir[engine->id])) &
1582 (GT_CONTEXT_SWITCH_INTERRUPT << engine->irq_shift));
1583
Chris Wilson42232212018-01-02 15:12:32 +00001584 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
1585}
1586
Chris Wilson821ed7d2016-09-09 14:11:53 +01001587static void reset_common_ring(struct intel_engine_cs *engine,
Chris Wilsone61e0f52018-02-21 09:56:36 +00001588 struct i915_request *request)
Chris Wilson821ed7d2016-09-09 14:11:53 +01001589{
Mika Kuoppalab620e872017-09-22 15:43:03 +03001590 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001591 struct intel_context *ce;
Chris Wilson221ab97192017-09-16 21:44:14 +01001592 unsigned long flags;
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001593
Chris Wilson16a87392017-12-20 09:06:26 +00001594 GEM_TRACE("%s seqno=%x\n",
1595 engine->name, request ? request->global_seqno : 0);
Chris Wilson42232212018-01-02 15:12:32 +00001596
1597 reset_irq(engine);
1598
Chris Wilson221ab97192017-09-16 21:44:14 +01001599 spin_lock_irqsave(&engine->timeline->lock, flags);
1600
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001601 /*
1602 * Catch up with any missed context-switch interrupts.
1603 *
1604 * Ideally we would just read the remaining CSB entries now that we
1605 * know the gpu is idle. However, the CSB registers are sometimes^W
1606 * often trashed across a GPU reset! Instead we have to rely on
1607 * guessing the missed context-switch events by looking at what
1608 * requests were completed.
1609 */
Michał Winiarskia4598d12017-10-25 22:00:18 +02001610 execlists_cancel_port_requests(execlists);
Chris Wilson221ab97192017-09-16 21:44:14 +01001611
1612 /* Push back any incomplete requests for replay after the reset. */
Michał Winiarskia4598d12017-10-25 22:00:18 +02001613 __unwind_incomplete_requests(engine);
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001614
Chris Wilson221ab97192017-09-16 21:44:14 +01001615 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001616
Chris Wilsone8401302018-02-05 15:24:30 +00001617 /* Mark all CS interrupts as complete */
1618 execlists->active = 0;
1619
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001620 /* If the request was innocent, we leave the request in the ELSP
1621 * and will try to replay it on restarting. The context image may
1622 * have been corrupted by the reset, in which case we may have
1623 * to service a new GPU hang, but more likely we can continue on
1624 * without impact.
1625 *
1626 * If the request was guilty, we presume the context is corrupt
1627 * and have to at least restore the RING register in the context
1628 * image back to the expected values to skip over the guilty request.
1629 */
Chris Wilson221ab97192017-09-16 21:44:14 +01001630 if (!request || request->fence.error != -EIO)
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001631 return;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001632
Chris Wilsona3aabe82016-10-04 21:11:26 +01001633 /* We want a simple context + ring to execute the breadcrumb update.
1634 * We cannot rely on the context being intact across the GPU hang,
1635 * so clear it and rebuild just what we need for the breadcrumb.
1636 * All pending requests for this context will be zapped, and any
1637 * future request will be after userspace has had the opportunity
1638 * to recreate its own state.
1639 */
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001640 ce = &request->ctx->engine[engine->id];
Chris Wilsona3aabe82016-10-04 21:11:26 +01001641 execlists_init_reg_state(ce->lrc_reg_state,
1642 request->ctx, engine, ce->ring);
1643
Chris Wilson821ed7d2016-09-09 14:11:53 +01001644 /* Move the RING_HEAD onto the breadcrumb, past the hanging batch */
Chris Wilsona3aabe82016-10-04 21:11:26 +01001645 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
1646 i915_ggtt_offset(ce->ring->vma);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001647 ce->lrc_reg_state[CTX_RING_HEAD+1] = request->postfix;
Chris Wilsona3aabe82016-10-04 21:11:26 +01001648
Chris Wilson821ed7d2016-09-09 14:11:53 +01001649 request->ring->head = request->postfix;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001650 intel_ring_update_space(request->ring);
1651
Chris Wilsona3aabe82016-10-04 21:11:26 +01001652 /* Reset WaIdleLiteRestore:bdw,skl as well */
Chris Wilson7e4992a2017-09-28 20:38:59 +01001653 unwind_wa_tail(request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001654}
1655
Chris Wilsone61e0f52018-02-21 09:56:36 +00001656static int intel_logical_ring_emit_pdps(struct i915_request *rq)
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001657{
Chris Wilsone61e0f52018-02-21 09:56:36 +00001658 struct i915_hw_ppgtt *ppgtt = rq->ctx->ppgtt;
1659 struct intel_engine_cs *engine = rq->engine;
Mika Kuoppalae7167762017-02-28 17:28:10 +02001660 const int num_lri_cmds = GEN8_3LVL_PDPES * 2;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001661 u32 *cs;
1662 int i;
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001663
Chris Wilsone61e0f52018-02-21 09:56:36 +00001664 cs = intel_ring_begin(rq, num_lri_cmds * 2 + 2);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001665 if (IS_ERR(cs))
1666 return PTR_ERR(cs);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001667
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001668 *cs++ = MI_LOAD_REGISTER_IMM(num_lri_cmds);
Mika Kuoppalae7167762017-02-28 17:28:10 +02001669 for (i = GEN8_3LVL_PDPES - 1; i >= 0; i--) {
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001670 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
1671
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001672 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, i));
1673 *cs++ = upper_32_bits(pd_daddr);
1674 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, i));
1675 *cs++ = lower_32_bits(pd_daddr);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001676 }
1677
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001678 *cs++ = MI_NOOP;
Chris Wilsone61e0f52018-02-21 09:56:36 +00001679 intel_ring_advance(rq, cs);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001680
1681 return 0;
1682}
1683
Chris Wilsone61e0f52018-02-21 09:56:36 +00001684static int gen8_emit_bb_start(struct i915_request *rq,
Chris Wilson803688b2016-08-02 22:50:27 +01001685 u64 offset, u32 len,
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001686 const unsigned int flags)
Oscar Mateo15648582014-07-24 17:04:32 +01001687{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001688 u32 *cs;
Oscar Mateo15648582014-07-24 17:04:32 +01001689 int ret;
1690
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001691 /* Don't rely in hw updating PDPs, specially in lite-restore.
1692 * Ideally, we should set Force PD Restore in ctx descriptor,
1693 * but we can't. Force Restore would be a second option, but
1694 * it is unsafe in case of lite-restore (because the ctx is
Michel Thierry2dba3232015-07-30 11:06:23 +01001695 * not idle). PML4 is allocated during ppgtt init so this is
1696 * not needed in 48-bit.*/
Chris Wilsone61e0f52018-02-21 09:56:36 +00001697 if (rq->ctx->ppgtt &&
1698 (intel_engine_flag(rq->engine) & rq->ctx->ppgtt->pd_dirty_rings) &&
1699 !i915_vm_is_48bit(&rq->ctx->ppgtt->base) &&
1700 !intel_vgpu_active(rq->i915)) {
1701 ret = intel_logical_ring_emit_pdps(rq);
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001702 if (ret)
1703 return ret;
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001704
Chris Wilsone61e0f52018-02-21 09:56:36 +00001705 rq->ctx->ppgtt->pd_dirty_rings &= ~intel_engine_flag(rq->engine);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001706 }
1707
Chris Wilsone61e0f52018-02-21 09:56:36 +00001708 cs = intel_ring_begin(rq, 4);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001709 if (IS_ERR(cs))
1710 return PTR_ERR(cs);
Oscar Mateo15648582014-07-24 17:04:32 +01001711
Chris Wilson279f5a02017-10-05 20:10:05 +01001712 /*
1713 * WaDisableCtxRestoreArbitration:bdw,chv
1714 *
1715 * We don't need to perform MI_ARB_ENABLE as often as we do (in
1716 * particular all the gen that do not need the w/a at all!), if we
1717 * took care to make sure that on every switch into this context
1718 * (both ordinary and for preemption) that arbitrartion was enabled
1719 * we would be fine. However, there doesn't seem to be a downside to
1720 * being paranoid and making sure it is set before each batch and
1721 * every context-switch.
1722 *
1723 * Note that if we fail to enable arbitration before the request
1724 * is complete, then we do not see the context-switch interrupt and
1725 * the engine hangs (with RING_HEAD == RING_TAIL).
1726 *
1727 * That satisfies both the GPGPU w/a and our heavy-handed paranoia.
1728 */
Chris Wilson3ad7b522017-10-03 21:34:49 +01001729 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1730
Oscar Mateo15648582014-07-24 17:04:32 +01001731 /* FIXME(BDW): Address space and security selectors. */
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001732 *cs++ = MI_BATCH_BUFFER_START_GEN8 |
1733 (flags & I915_DISPATCH_SECURE ? 0 : BIT(8)) |
1734 (flags & I915_DISPATCH_RS ? MI_BATCH_RESOURCE_STREAMER : 0);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001735 *cs++ = lower_32_bits(offset);
1736 *cs++ = upper_32_bits(offset);
Chris Wilsone61e0f52018-02-21 09:56:36 +00001737 intel_ring_advance(rq, cs);
Oscar Mateo15648582014-07-24 17:04:32 +01001738
1739 return 0;
1740}
1741
Chris Wilson31bb59c2016-07-01 17:23:27 +01001742static void gen8_logical_ring_enable_irq(struct intel_engine_cs *engine)
Oscar Mateo73d477f2014-07-24 17:04:31 +01001743{
Chris Wilsonc0336662016-05-06 15:40:21 +01001744 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson31bb59c2016-07-01 17:23:27 +01001745 I915_WRITE_IMR(engine,
1746 ~(engine->irq_enable_mask | engine->irq_keep_mask));
1747 POSTING_READ_FW(RING_IMR(engine->mmio_base));
Oscar Mateo73d477f2014-07-24 17:04:31 +01001748}
1749
Chris Wilson31bb59c2016-07-01 17:23:27 +01001750static void gen8_logical_ring_disable_irq(struct intel_engine_cs *engine)
Oscar Mateo73d477f2014-07-24 17:04:31 +01001751{
Chris Wilsonc0336662016-05-06 15:40:21 +01001752 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson31bb59c2016-07-01 17:23:27 +01001753 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Oscar Mateo73d477f2014-07-24 17:04:31 +01001754}
1755
Chris Wilsone61e0f52018-02-21 09:56:36 +00001756static int gen8_emit_flush(struct i915_request *request, u32 mode)
Oscar Mateo47122742014-07-24 17:04:28 +01001757{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001758 u32 cmd, *cs;
Oscar Mateo47122742014-07-24 17:04:28 +01001759
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001760 cs = intel_ring_begin(request, 4);
1761 if (IS_ERR(cs))
1762 return PTR_ERR(cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001763
1764 cmd = MI_FLUSH_DW + 1;
1765
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001766 /* We always require a command barrier so that subsequent
1767 * commands, such as breadcrumb interrupts, are strictly ordered
1768 * wrt the contents of the write cache being flushed to memory
1769 * (and thus being coherent from the CPU).
1770 */
1771 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1772
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001773 if (mode & EMIT_INVALIDATE) {
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001774 cmd |= MI_INVALIDATE_TLB;
Chris Wilson1dae2df2016-08-02 22:50:19 +01001775 if (request->engine->id == VCS)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001776 cmd |= MI_INVALIDATE_BSD;
Oscar Mateo47122742014-07-24 17:04:28 +01001777 }
1778
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001779 *cs++ = cmd;
1780 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
1781 *cs++ = 0; /* upper addr */
1782 *cs++ = 0; /* value */
1783 intel_ring_advance(request, cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001784
1785 return 0;
1786}
1787
Chris Wilsone61e0f52018-02-21 09:56:36 +00001788static int gen8_emit_flush_render(struct i915_request *request,
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001789 u32 mode)
Oscar Mateo47122742014-07-24 17:04:28 +01001790{
Chris Wilsonb5321f32016-08-02 22:50:18 +01001791 struct intel_engine_cs *engine = request->engine;
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001792 u32 scratch_addr =
1793 i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001794 bool vf_flush_wa = false, dc_flush_wa = false;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001795 u32 *cs, flags = 0;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001796 int len;
Oscar Mateo47122742014-07-24 17:04:28 +01001797
1798 flags |= PIPE_CONTROL_CS_STALL;
1799
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001800 if (mode & EMIT_FLUSH) {
Oscar Mateo47122742014-07-24 17:04:28 +01001801 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
1802 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -08001803 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +01001804 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Oscar Mateo47122742014-07-24 17:04:28 +01001805 }
1806
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001807 if (mode & EMIT_INVALIDATE) {
Oscar Mateo47122742014-07-24 17:04:28 +01001808 flags |= PIPE_CONTROL_TLB_INVALIDATE;
1809 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
1810 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
1811 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
1812 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
1813 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
1814 flags |= PIPE_CONTROL_QW_WRITE;
1815 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Oscar Mateo47122742014-07-24 17:04:28 +01001816
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001817 /*
1818 * On GEN9: before VF_CACHE_INVALIDATE we need to emit a NULL
1819 * pipe control.
1820 */
Chris Wilsonc0336662016-05-06 15:40:21 +01001821 if (IS_GEN9(request->i915))
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001822 vf_flush_wa = true;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001823
1824 /* WaForGAMHang:kbl */
1825 if (IS_KBL_REVID(request->i915, 0, KBL_REVID_B0))
1826 dc_flush_wa = true;
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001827 }
Imre Deak9647ff32015-01-25 13:27:11 -08001828
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001829 len = 6;
1830
1831 if (vf_flush_wa)
1832 len += 6;
1833
1834 if (dc_flush_wa)
1835 len += 12;
1836
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001837 cs = intel_ring_begin(request, len);
1838 if (IS_ERR(cs))
1839 return PTR_ERR(cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001840
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001841 if (vf_flush_wa)
1842 cs = gen8_emit_pipe_control(cs, 0, 0);
Imre Deak9647ff32015-01-25 13:27:11 -08001843
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001844 if (dc_flush_wa)
1845 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_DC_FLUSH_ENABLE,
1846 0);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001847
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001848 cs = gen8_emit_pipe_control(cs, flags, scratch_addr);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001849
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001850 if (dc_flush_wa)
1851 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_CS_STALL, 0);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001852
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001853 intel_ring_advance(request, cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001854
1855 return 0;
1856}
1857
Chris Wilson7c17d372016-01-20 15:43:35 +02001858/*
1859 * Reserve space for 2 NOOPs at the end of each request to be
1860 * used as a workaround for not being allowed to do lite
1861 * restore with HEAD==TAIL (WaIdleLiteRestore).
1862 */
Chris Wilsone61e0f52018-02-21 09:56:36 +00001863static void gen8_emit_wa_tail(struct i915_request *request, u32 *cs)
Oscar Mateo4da46e12014-07-24 17:04:27 +01001864{
Chris Wilsonbeecec92017-10-03 21:34:52 +01001865 /* Ensure there's always at least one preemption point per-request. */
1866 *cs++ = MI_ARB_CHECK;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001867 *cs++ = MI_NOOP;
1868 request->wa_tail = intel_ring_offset(request, cs);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001869}
Oscar Mateo4da46e12014-07-24 17:04:27 +01001870
Chris Wilsone61e0f52018-02-21 09:56:36 +00001871static void gen8_emit_breadcrumb(struct i915_request *request, u32 *cs)
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001872{
Chris Wilson7c17d372016-01-20 15:43:35 +02001873 /* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */
1874 BUILD_BUG_ON(I915_GEM_HWS_INDEX_ADDR & (1 << 5));
Oscar Mateo4da46e12014-07-24 17:04:27 +01001875
Michał Winiarskidf77cd82017-10-25 22:00:15 +02001876 cs = gen8_emit_ggtt_write(cs, request->global_seqno,
1877 intel_hws_seqno_address(request->engine));
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001878 *cs++ = MI_USER_INTERRUPT;
1879 *cs++ = MI_NOOP;
1880 request->tail = intel_ring_offset(request, cs);
Chris Wilsoned1501d2017-03-27 14:14:12 +01001881 assert_ring_tail_valid(request->ring, request->tail);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001882
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001883 gen8_emit_wa_tail(request, cs);
Chris Wilson7c17d372016-01-20 15:43:35 +02001884}
Chris Wilson98f29e82016-10-28 13:58:51 +01001885static const int gen8_emit_breadcrumb_sz = 6 + WA_TAIL_DWORDS;
1886
Chris Wilsone61e0f52018-02-21 09:56:36 +00001887static void gen8_emit_breadcrumb_rcs(struct i915_request *request, u32 *cs)
Chris Wilson7c17d372016-01-20 15:43:35 +02001888{
Michał Winiarskice81a652016-04-12 15:51:55 +02001889 /* We're using qword write, seqno should be aligned to 8 bytes. */
1890 BUILD_BUG_ON(I915_GEM_HWS_INDEX & 1);
1891
Michał Winiarskidf77cd82017-10-25 22:00:15 +02001892 cs = gen8_emit_ggtt_write_rcs(cs, request->global_seqno,
1893 intel_hws_seqno_address(request->engine));
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001894 *cs++ = MI_USER_INTERRUPT;
1895 *cs++ = MI_NOOP;
1896 request->tail = intel_ring_offset(request, cs);
Chris Wilsoned1501d2017-03-27 14:14:12 +01001897 assert_ring_tail_valid(request->ring, request->tail);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001898
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001899 gen8_emit_wa_tail(request, cs);
Oscar Mateo4da46e12014-07-24 17:04:27 +01001900}
Michał Winiarskidf77cd82017-10-25 22:00:15 +02001901static const int gen8_emit_breadcrumb_rcs_sz = 8 + WA_TAIL_DWORDS;
Chris Wilson98f29e82016-10-28 13:58:51 +01001902
Chris Wilsone61e0f52018-02-21 09:56:36 +00001903static int gen8_init_rcs_context(struct i915_request *rq)
Thomas Daniele7778be2014-12-02 12:50:48 +00001904{
1905 int ret;
1906
Chris Wilsone61e0f52018-02-21 09:56:36 +00001907 ret = intel_ring_workarounds_emit(rq);
Thomas Daniele7778be2014-12-02 12:50:48 +00001908 if (ret)
1909 return ret;
1910
Chris Wilsone61e0f52018-02-21 09:56:36 +00001911 ret = intel_rcs_context_init_mocs(rq);
Peter Antoine3bbaba02015-07-10 20:13:11 +03001912 /*
1913 * Failing to program the MOCS is non-fatal.The system will not
1914 * run at peak performance. So generate an error and carry on.
1915 */
1916 if (ret)
1917 DRM_ERROR("MOCS failed to program: expect performance issues.\n");
1918
Chris Wilsone61e0f52018-02-21 09:56:36 +00001919 return i915_gem_render_state_emit(rq);
Thomas Daniele7778be2014-12-02 12:50:48 +00001920}
1921
Oscar Mateo73e4d072014-07-24 17:04:48 +01001922/**
1923 * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001924 * @engine: Engine Command Streamer.
Oscar Mateo73e4d072014-07-24 17:04:48 +01001925 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001926void intel_logical_ring_cleanup(struct intel_engine_cs *engine)
Oscar Mateo454afeb2014-07-24 17:04:22 +01001927{
John Harrison6402c332014-10-31 12:00:26 +00001928 struct drm_i915_private *dev_priv;
Oscar Mateo9832b9d2014-07-24 17:04:30 +01001929
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01001930 /*
1931 * Tasklet cannot be active at this point due intel_mark_active/idle
1932 * so this is just for documentation.
1933 */
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05301934 if (WARN_ON(test_bit(TASKLET_STATE_SCHED,
1935 &engine->execlists.tasklet.state)))
1936 tasklet_kill(&engine->execlists.tasklet);
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01001937
Chris Wilsonc0336662016-05-06 15:40:21 +01001938 dev_priv = engine->i915;
John Harrison6402c332014-10-31 12:00:26 +00001939
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001940 if (engine->buffer) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001941 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
Dave Gordonb0366a52015-12-08 15:02:36 +00001942 }
Oscar Mateo48d82382014-07-24 17:04:23 +01001943
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001944 if (engine->cleanup)
1945 engine->cleanup(engine);
Oscar Mateo48d82382014-07-24 17:04:23 +01001946
Chris Wilsone8a9c582016-12-18 15:37:20 +00001947 intel_engine_cleanup_common(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001948
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001949 lrc_destroy_wa_ctx(engine);
Chris Wilsonf3c9d4072018-01-02 15:12:34 +00001950
Chris Wilsonc0336662016-05-06 15:40:21 +01001951 engine->i915 = NULL;
Akash Goel3b3f1652016-10-13 22:44:48 +05301952 dev_priv->engine[engine->id] = NULL;
1953 kfree(engine);
Oscar Mateo454afeb2014-07-24 17:04:22 +01001954}
1955
Chris Wilsonff44ad52017-03-16 17:13:03 +00001956static void execlists_set_default_submission(struct intel_engine_cs *engine)
Chris Wilsonddd66c52016-08-02 22:50:31 +01001957{
Chris Wilsonff44ad52017-03-16 17:13:03 +00001958 engine->submit_request = execlists_submit_request;
Chris Wilson27a5f612017-09-15 18:31:00 +01001959 engine->cancel_requests = execlists_cancel_requests;
Chris Wilsonff44ad52017-03-16 17:13:03 +00001960 engine->schedule = execlists_schedule;
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05301961 engine->execlists.tasklet.func = execlists_submission_tasklet;
Chris Wilsonaba5e272017-10-25 15:39:41 +01001962
1963 engine->park = NULL;
1964 engine->unpark = NULL;
Tvrtko Ursulincf669b42017-11-29 10:28:05 +00001965
1966 engine->flags |= I915_ENGINE_SUPPORTS_STATS;
Chris Wilson3fed1802018-02-07 21:05:43 +00001967
1968 engine->i915->caps.scheduler =
1969 I915_SCHEDULER_CAP_ENABLED |
1970 I915_SCHEDULER_CAP_PRIORITY;
Chris Wilsond6376372018-02-07 21:05:44 +00001971 if (engine->i915->preempt_context)
Chris Wilson3fed1802018-02-07 21:05:43 +00001972 engine->i915->caps.scheduler |= I915_SCHEDULER_CAP_PREEMPTION;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001973}
1974
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001975static void
Chris Wilsone1382ef2016-05-06 15:40:20 +01001976logical_ring_default_vfuncs(struct intel_engine_cs *engine)
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001977{
1978 /* Default vfuncs which can be overriden by each engine. */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001979 engine->init_hw = gen8_init_common_ring;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001980 engine->reset_hw = reset_common_ring;
Chris Wilsone8a9c582016-12-18 15:37:20 +00001981
1982 engine->context_pin = execlists_context_pin;
1983 engine->context_unpin = execlists_context_unpin;
1984
Chris Wilsonf73e7392016-12-18 15:37:24 +00001985 engine->request_alloc = execlists_request_alloc;
1986
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001987 engine->emit_flush = gen8_emit_flush;
Chris Wilson9b81d552016-10-28 13:58:50 +01001988 engine->emit_breadcrumb = gen8_emit_breadcrumb;
Chris Wilson98f29e82016-10-28 13:58:51 +01001989 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_sz;
Chris Wilsonff44ad52017-03-16 17:13:03 +00001990
1991 engine->set_default_submission = execlists_set_default_submission;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001992
Chris Wilson31bb59c2016-07-01 17:23:27 +01001993 engine->irq_enable = gen8_logical_ring_enable_irq;
1994 engine->irq_disable = gen8_logical_ring_disable_irq;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001995 engine->emit_bb_start = gen8_emit_bb_start;
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001996}
1997
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001998static inline void
Dave Gordonc2c7f242016-07-13 16:03:35 +01001999logical_ring_default_irqs(struct intel_engine_cs *engine)
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00002000{
Dave Gordonc2c7f242016-07-13 16:03:35 +01002001 unsigned shift = engine->irq_shift;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002002 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << shift;
2003 engine->irq_keep_mask = GT_CONTEXT_SWITCH_INTERRUPT << shift;
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00002004}
2005
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002006static void
2007logical_ring_setup(struct intel_engine_cs *engine)
2008{
2009 struct drm_i915_private *dev_priv = engine->i915;
2010 enum forcewake_domains fw_domains;
2011
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01002012 intel_engine_setup_common(engine);
2013
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002014 /* Intentionally left blank. */
2015 engine->buffer = NULL;
2016
2017 fw_domains = intel_uncore_forcewake_for_reg(dev_priv,
2018 RING_ELSP(engine),
2019 FW_REG_WRITE);
2020
2021 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
2022 RING_CONTEXT_STATUS_PTR(engine),
2023 FW_REG_READ | FW_REG_WRITE);
2024
2025 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
2026 RING_CONTEXT_STATUS_BUF_BASE(engine),
2027 FW_REG_READ);
2028
Mika Kuoppalab620e872017-09-22 15:43:03 +03002029 engine->execlists.fw_domains = fw_domains;
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002030
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302031 tasklet_init(&engine->execlists.tasklet,
2032 execlists_submission_tasklet, (unsigned long)engine);
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002033
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002034 logical_ring_default_vfuncs(engine);
2035 logical_ring_default_irqs(engine);
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002036}
2037
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +01002038static int logical_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002039{
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002040 int ret;
2041
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01002042 ret = intel_engine_init_common(engine);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002043 if (ret)
2044 goto error;
2045
Chris Wilson693cfbf2018-01-02 15:12:33 +00002046 engine->execlists.elsp =
2047 engine->i915->regs + i915_mmio_reg_offset(RING_ELSP(engine));
2048
Chris Wilsond6376372018-02-07 21:05:44 +00002049 engine->execlists.preempt_complete_status = ~0u;
2050 if (engine->i915->preempt_context)
2051 engine->execlists.preempt_complete_status =
2052 upper_32_bits(engine->i915->preempt_context->engine[engine->id].lrc_desc);
2053
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002054 return 0;
2055
2056error:
2057 intel_logical_ring_cleanup(engine);
2058 return ret;
2059}
2060
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +01002061int logical_render_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002062{
2063 struct drm_i915_private *dev_priv = engine->i915;
2064 int ret;
2065
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002066 logical_ring_setup(engine);
2067
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002068 if (HAS_L3_DPF(dev_priv))
2069 engine->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
2070
2071 /* Override some for render ring. */
2072 if (INTEL_GEN(dev_priv) >= 9)
2073 engine->init_hw = gen9_init_render_ring;
2074 else
2075 engine->init_hw = gen8_init_render_ring;
2076 engine->init_context = gen8_init_rcs_context;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002077 engine->emit_flush = gen8_emit_flush_render;
Michał Winiarskidf77cd82017-10-25 22:00:15 +02002078 engine->emit_breadcrumb = gen8_emit_breadcrumb_rcs;
2079 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_rcs_sz;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002080
Chris Wilsonf51455d2017-01-10 14:47:34 +00002081 ret = intel_engine_create_scratch(engine, PAGE_SIZE);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002082 if (ret)
2083 return ret;
2084
2085 ret = intel_init_workaround_bb(engine);
2086 if (ret) {
2087 /*
2088 * We continue even if we fail to initialize WA batch
2089 * because we only expect rare glitches but nothing
2090 * critical to prevent us from using GPU
2091 */
2092 DRM_ERROR("WA batch buffer initialization failed: %d\n",
2093 ret);
2094 }
2095
Tvrtko Ursulind038fc72016-12-16 13:18:42 +00002096 return logical_ring_init(engine);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002097}
2098
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +01002099int logical_xcs_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002100{
2101 logical_ring_setup(engine);
2102
2103 return logical_ring_init(engine);
2104}
2105
Jeff McGee0cea6502015-02-13 10:27:56 -06002106static u32
Chris Wilsonc0336662016-05-06 15:40:21 +01002107make_rpcs(struct drm_i915_private *dev_priv)
Jeff McGee0cea6502015-02-13 10:27:56 -06002108{
2109 u32 rpcs = 0;
2110
2111 /*
2112 * No explicit RPCS request is needed to ensure full
2113 * slice/subslice/EU enablement prior to Gen9.
2114 */
Chris Wilsonc0336662016-05-06 15:40:21 +01002115 if (INTEL_GEN(dev_priv) < 9)
Jeff McGee0cea6502015-02-13 10:27:56 -06002116 return 0;
2117
2118 /*
2119 * Starting in Gen9, render power gating can leave
2120 * slice/subslice/EU in a partially enabled state. We
2121 * must make an explicit request through RPCS for full
2122 * enablement.
2123 */
Imre Deak43b67992016-08-31 19:13:02 +03002124 if (INTEL_INFO(dev_priv)->sseu.has_slice_pg) {
Jeff McGee0cea6502015-02-13 10:27:56 -06002125 rpcs |= GEN8_RPCS_S_CNT_ENABLE;
Imre Deakf08a0c92016-08-31 19:13:04 +03002126 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.slice_mask) <<
Jeff McGee0cea6502015-02-13 10:27:56 -06002127 GEN8_RPCS_S_CNT_SHIFT;
2128 rpcs |= GEN8_RPCS_ENABLE;
2129 }
2130
Imre Deak43b67992016-08-31 19:13:02 +03002131 if (INTEL_INFO(dev_priv)->sseu.has_subslice_pg) {
Jeff McGee0cea6502015-02-13 10:27:56 -06002132 rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
Imre Deak57ec1712016-08-31 19:13:05 +03002133 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.subslice_mask) <<
Jeff McGee0cea6502015-02-13 10:27:56 -06002134 GEN8_RPCS_SS_CNT_SHIFT;
2135 rpcs |= GEN8_RPCS_ENABLE;
2136 }
2137
Imre Deak43b67992016-08-31 19:13:02 +03002138 if (INTEL_INFO(dev_priv)->sseu.has_eu_pg) {
2139 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
Jeff McGee0cea6502015-02-13 10:27:56 -06002140 GEN8_RPCS_EU_MIN_SHIFT;
Imre Deak43b67992016-08-31 19:13:02 +03002141 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
Jeff McGee0cea6502015-02-13 10:27:56 -06002142 GEN8_RPCS_EU_MAX_SHIFT;
2143 rpcs |= GEN8_RPCS_ENABLE;
2144 }
2145
2146 return rpcs;
2147}
2148
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002149static u32 intel_lr_indirect_ctx_offset(struct intel_engine_cs *engine)
Michel Thierry71562912016-02-23 10:31:49 +00002150{
2151 u32 indirect_ctx_offset;
2152
Chris Wilsonc0336662016-05-06 15:40:21 +01002153 switch (INTEL_GEN(engine->i915)) {
Michel Thierry71562912016-02-23 10:31:49 +00002154 default:
Chris Wilsonc0336662016-05-06 15:40:21 +01002155 MISSING_CASE(INTEL_GEN(engine->i915));
Michel Thierry71562912016-02-23 10:31:49 +00002156 /* fall through */
Michel Thierry7bd0a2c2017-06-06 13:30:38 -07002157 case 10:
2158 indirect_ctx_offset =
2159 GEN10_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2160 break;
Michel Thierry71562912016-02-23 10:31:49 +00002161 case 9:
2162 indirect_ctx_offset =
2163 GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2164 break;
2165 case 8:
2166 indirect_ctx_offset =
2167 GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2168 break;
2169 }
2170
2171 return indirect_ctx_offset;
2172}
2173
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002174static void execlists_init_reg_state(u32 *regs,
Chris Wilsona3aabe82016-10-04 21:11:26 +01002175 struct i915_gem_context *ctx,
2176 struct intel_engine_cs *engine,
2177 struct intel_ring *ring)
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002178{
Chris Wilsona3aabe82016-10-04 21:11:26 +01002179 struct drm_i915_private *dev_priv = engine->i915;
2180 struct i915_hw_ppgtt *ppgtt = ctx->ppgtt ?: dev_priv->mm.aliasing_ppgtt;
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002181 u32 base = engine->mmio_base;
2182 bool rcs = engine->id == RCS;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002183
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002184 /* A context is actually a big batch buffer with several
2185 * MI_LOAD_REGISTER_IMM commands followed by (reg, value) pairs. The
2186 * values we are setting here are only for the first context restore:
2187 * on a subsequent save, the GPU will recreate this batchbuffer with new
2188 * values (including all the missing MI_LOAD_REGISTER_IMM commands that
2189 * we are not initializing here).
2190 */
2191 regs[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(rcs ? 14 : 11) |
2192 MI_LRI_FORCE_POSTED;
2193
2194 CTX_REG(regs, CTX_CONTEXT_CONTROL, RING_CONTEXT_CONTROL(engine),
Chris Wilson09b1a4e2018-01-25 11:24:42 +00002195 _MASKED_BIT_DISABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
2196 CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT) |
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002197 _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002198 (HAS_RESOURCE_STREAMER(dev_priv) ?
2199 CTX_CTRL_RS_CTX_ENABLE : 0)));
2200 CTX_REG(regs, CTX_RING_HEAD, RING_HEAD(base), 0);
2201 CTX_REG(regs, CTX_RING_TAIL, RING_TAIL(base), 0);
2202 CTX_REG(regs, CTX_RING_BUFFER_START, RING_START(base), 0);
2203 CTX_REG(regs, CTX_RING_BUFFER_CONTROL, RING_CTL(base),
2204 RING_CTL_SIZE(ring->size) | RING_VALID);
2205 CTX_REG(regs, CTX_BB_HEAD_U, RING_BBADDR_UDW(base), 0);
2206 CTX_REG(regs, CTX_BB_HEAD_L, RING_BBADDR(base), 0);
2207 CTX_REG(regs, CTX_BB_STATE, RING_BBSTATE(base), RING_BB_PPGTT);
2208 CTX_REG(regs, CTX_SECOND_BB_HEAD_U, RING_SBBADDR_UDW(base), 0);
2209 CTX_REG(regs, CTX_SECOND_BB_HEAD_L, RING_SBBADDR(base), 0);
2210 CTX_REG(regs, CTX_SECOND_BB_STATE, RING_SBBSTATE(base), 0);
2211 if (rcs) {
Chris Wilson604a8f62017-09-21 14:54:43 +01002212 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
2213
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002214 CTX_REG(regs, CTX_RCS_INDIRECT_CTX, RING_INDIRECT_CTX(base), 0);
2215 CTX_REG(regs, CTX_RCS_INDIRECT_CTX_OFFSET,
2216 RING_INDIRECT_CTX_OFFSET(base), 0);
Chris Wilson604a8f62017-09-21 14:54:43 +01002217 if (wa_ctx->indirect_ctx.size) {
Chris Wilsonbde13eb2016-08-15 10:49:07 +01002218 u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01002219
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002220 regs[CTX_RCS_INDIRECT_CTX + 1] =
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00002221 (ggtt_offset + wa_ctx->indirect_ctx.offset) |
2222 (wa_ctx->indirect_ctx.size / CACHELINE_BYTES);
Arun Siluvery17ee9502015-06-19 19:07:01 +01002223
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002224 regs[CTX_RCS_INDIRECT_CTX_OFFSET + 1] =
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002225 intel_lr_indirect_ctx_offset(engine) << 6;
Chris Wilson604a8f62017-09-21 14:54:43 +01002226 }
2227
2228 CTX_REG(regs, CTX_BB_PER_CTX_PTR, RING_BB_PER_CTX_PTR(base), 0);
2229 if (wa_ctx->per_ctx.size) {
2230 u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01002231
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002232 regs[CTX_BB_PER_CTX_PTR + 1] =
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00002233 (ggtt_offset + wa_ctx->per_ctx.offset) | 0x01;
Arun Siluvery17ee9502015-06-19 19:07:01 +01002234 }
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002235 }
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002236
2237 regs[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9) | MI_LRI_FORCE_POSTED;
2238
2239 CTX_REG(regs, CTX_CTX_TIMESTAMP, RING_CTX_TIMESTAMP(base), 0);
Ville Syrjälä0d925ea2015-11-04 23:20:11 +02002240 /* PDP values well be assigned later if needed */
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002241 CTX_REG(regs, CTX_PDP3_UDW, GEN8_RING_PDP_UDW(engine, 3), 0);
2242 CTX_REG(regs, CTX_PDP3_LDW, GEN8_RING_PDP_LDW(engine, 3), 0);
2243 CTX_REG(regs, CTX_PDP2_UDW, GEN8_RING_PDP_UDW(engine, 2), 0);
2244 CTX_REG(regs, CTX_PDP2_LDW, GEN8_RING_PDP_LDW(engine, 2), 0);
2245 CTX_REG(regs, CTX_PDP1_UDW, GEN8_RING_PDP_UDW(engine, 1), 0);
2246 CTX_REG(regs, CTX_PDP1_LDW, GEN8_RING_PDP_LDW(engine, 1), 0);
2247 CTX_REG(regs, CTX_PDP0_UDW, GEN8_RING_PDP_UDW(engine, 0), 0);
2248 CTX_REG(regs, CTX_PDP0_LDW, GEN8_RING_PDP_LDW(engine, 0), 0);
Michel Thierryd7b26332015-04-08 12:13:34 +01002249
Chris Wilson949e8ab2017-02-09 14:40:36 +00002250 if (ppgtt && i915_vm_is_48bit(&ppgtt->base)) {
Michel Thierry2dba3232015-07-30 11:06:23 +01002251 /* 64b PPGTT (48bit canonical)
2252 * PDP0_DESCRIPTOR contains the base address to PML4 and
2253 * other PDP Descriptors are ignored.
2254 */
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002255 ASSIGN_CTX_PML4(ppgtt, regs);
Michel Thierry2dba3232015-07-30 11:06:23 +01002256 }
2257
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002258 if (rcs) {
2259 regs[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
2260 CTX_REG(regs, CTX_R_PWR_CLK_STATE, GEN8_R_PWR_CLK_STATE,
2261 make_rpcs(dev_priv));
Robert Bragg19f81df2017-06-13 12:23:03 +01002262
2263 i915_oa_init_reg_state(engine, ctx, regs);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002264 }
Chris Wilsona3aabe82016-10-04 21:11:26 +01002265}
2266
2267static int
2268populate_lr_context(struct i915_gem_context *ctx,
2269 struct drm_i915_gem_object *ctx_obj,
2270 struct intel_engine_cs *engine,
2271 struct intel_ring *ring)
2272{
2273 void *vaddr;
Chris Wilsond2b4b972017-11-10 14:26:33 +00002274 u32 *regs;
Chris Wilsona3aabe82016-10-04 21:11:26 +01002275 int ret;
2276
2277 ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
2278 if (ret) {
2279 DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
2280 return ret;
2281 }
2282
2283 vaddr = i915_gem_object_pin_map(ctx_obj, I915_MAP_WB);
2284 if (IS_ERR(vaddr)) {
2285 ret = PTR_ERR(vaddr);
2286 DRM_DEBUG_DRIVER("Could not map object pages! (%d)\n", ret);
2287 return ret;
2288 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002289 ctx_obj->mm.dirty = true;
Chris Wilsona3aabe82016-10-04 21:11:26 +01002290
Chris Wilsond2b4b972017-11-10 14:26:33 +00002291 if (engine->default_state) {
2292 /*
2293 * We only want to copy over the template context state;
2294 * skipping over the headers reserved for GuC communication,
2295 * leaving those as zero.
2296 */
2297 const unsigned long start = LRC_HEADER_PAGES * PAGE_SIZE;
2298 void *defaults;
2299
2300 defaults = i915_gem_object_pin_map(engine->default_state,
2301 I915_MAP_WB);
2302 if (IS_ERR(defaults))
2303 return PTR_ERR(defaults);
2304
2305 memcpy(vaddr + start, defaults + start, engine->context_size);
2306 i915_gem_object_unpin_map(engine->default_state);
2307 }
2308
Chris Wilsona3aabe82016-10-04 21:11:26 +01002309 /* The second page of the context object contains some fields which must
2310 * be set up prior to the first execution. */
Chris Wilsond2b4b972017-11-10 14:26:33 +00002311 regs = vaddr + LRC_STATE_PN * PAGE_SIZE;
2312 execlists_init_reg_state(regs, ctx, engine, ring);
2313 if (!engine->default_state)
2314 regs[CTX_CONTEXT_CONTROL + 1] |=
2315 _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT);
Chris Wilsond6376372018-02-07 21:05:44 +00002316 if (ctx == ctx->i915->preempt_context)
Chris Wilson517aaff2018-01-23 21:04:12 +00002317 regs[CTX_CONTEXT_CONTROL + 1] |=
2318 _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
2319 CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002320
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01002321 i915_gem_object_unpin_map(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002322
2323 return 0;
2324}
2325
Chris Wilsone2efd132016-05-24 14:53:34 +01002326static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
Chris Wilson978f1e02016-04-28 09:56:54 +01002327 struct intel_engine_cs *engine)
Oscar Mateoede7d422014-07-24 17:04:12 +01002328{
Oscar Mateo8c8579172014-07-24 17:04:14 +01002329 struct drm_i915_gem_object *ctx_obj;
Chris Wilson9021ad02016-05-24 14:53:37 +01002330 struct intel_context *ce = &ctx->engine[engine->id];
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002331 struct i915_vma *vma;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002332 uint32_t context_size;
Chris Wilson7e37f882016-08-02 22:50:21 +01002333 struct intel_ring *ring;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002334 int ret;
2335
Chris Wilson1d2a19c2018-01-26 12:18:46 +00002336 if (ce->state)
2337 return 0;
Oscar Mateoede7d422014-07-24 17:04:12 +01002338
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +03002339 context_size = round_up(engine->context_size, I915_GTT_PAGE_SIZE);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002340
Michel Thierry0b29c752017-09-13 09:56:00 +01002341 /*
2342 * Before the actual start of the context image, we insert a few pages
2343 * for our own use and for sharing with the GuC.
2344 */
2345 context_size += LRC_HEADER_PAGES * PAGE_SIZE;
Alex Daid1675192015-08-12 15:43:43 +01002346
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00002347 ctx_obj = i915_gem_object_create(ctx->i915, context_size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01002348 if (IS_ERR(ctx_obj)) {
Dan Carpenter3126a662015-04-30 17:30:50 +03002349 DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
Chris Wilsonfe3db792016-04-25 13:32:13 +01002350 return PTR_ERR(ctx_obj);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002351 }
2352
Chris Wilsona01cb372017-01-16 15:21:30 +00002353 vma = i915_vma_instance(ctx_obj, &ctx->i915->ggtt.base, NULL);
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002354 if (IS_ERR(vma)) {
2355 ret = PTR_ERR(vma);
2356 goto error_deref_obj;
2357 }
2358
Chris Wilson7e37f882016-08-02 22:50:21 +01002359 ring = intel_engine_create_ring(engine, ctx->ring_size);
Chris Wilsondca33ec2016-08-02 22:50:20 +01002360 if (IS_ERR(ring)) {
2361 ret = PTR_ERR(ring);
Nick Hoathe84fe802015-09-11 12:53:46 +01002362 goto error_deref_obj;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002363 }
2364
Chris Wilsondca33ec2016-08-02 22:50:20 +01002365 ret = populate_lr_context(ctx, ctx_obj, engine, ring);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002366 if (ret) {
2367 DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
Chris Wilsondca33ec2016-08-02 22:50:20 +01002368 goto error_ring_free;
Oscar Mateo84c23772014-07-24 17:04:15 +01002369 }
2370
Chris Wilsondca33ec2016-08-02 22:50:20 +01002371 ce->ring = ring;
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002372 ce->state = vma;
Oscar Mateoede7d422014-07-24 17:04:12 +01002373
2374 return 0;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002375
Chris Wilsondca33ec2016-08-02 22:50:20 +01002376error_ring_free:
Chris Wilson7e37f882016-08-02 22:50:21 +01002377 intel_ring_free(ring);
Nick Hoathe84fe802015-09-11 12:53:46 +01002378error_deref_obj:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002379 i915_gem_object_put(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002380 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01002381}
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002382
Chris Wilson821ed7d2016-09-09 14:11:53 +01002383void intel_lr_context_resume(struct drm_i915_private *dev_priv)
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002384{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002385 struct intel_engine_cs *engine;
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002386 struct i915_gem_context *ctx;
Akash Goel3b3f1652016-10-13 22:44:48 +05302387 enum intel_engine_id id;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002388
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002389 /* Because we emit WA_TAIL_DWORDS there may be a disparity
2390 * between our bookkeeping in ce->ring->head and ce->ring->tail and
2391 * that stored in context. As we only write new commands from
2392 * ce->ring->tail onwards, everything before that is junk. If the GPU
2393 * starts reading from its RING_HEAD from the context, it may try to
2394 * execute that junk and die.
2395 *
2396 * So to avoid that we reset the context images upon resume. For
2397 * simplicity, we just zero everything out.
2398 */
Chris Wilson829a0af2017-06-20 12:05:45 +01002399 list_for_each_entry(ctx, &dev_priv->contexts.list, link) {
Akash Goel3b3f1652016-10-13 22:44:48 +05302400 for_each_engine(engine, dev_priv, id) {
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002401 struct intel_context *ce = &ctx->engine[engine->id];
2402 u32 *reg;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002403
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002404 if (!ce->state)
2405 continue;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002406
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002407 reg = i915_gem_object_pin_map(ce->state->obj,
2408 I915_MAP_WB);
2409 if (WARN_ON(IS_ERR(reg)))
2410 continue;
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01002411
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002412 reg += LRC_STATE_PN * PAGE_SIZE / sizeof(*reg);
2413 reg[CTX_RING_HEAD+1] = 0;
2414 reg[CTX_RING_TAIL+1] = 0;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002415
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002416 ce->state->obj->mm.dirty = true;
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002417 i915_gem_object_unpin_map(ce->state->obj);
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002418
Chris Wilsone6ba9992017-04-25 14:00:49 +01002419 intel_ring_reset(ce->ring, 0);
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002420 }
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002421 }
2422}