blob: f0fa0f767eb6a85e159601dfcb02cc1cc846f92d [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
Oscar Mateob20385f2014-07-24 17:04:10 +0100136#include <drm/i915_drm.h>
137#include "i915_drv.h"
Chris Wilson7c2fa7f2017-11-10 14:26:34 +0000138#include "i915_gem_render_state.h"
Chris Wilsonbc4237e2018-06-28 21:12:07 +0100139#include "i915_vgpu.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 Mateo7d3c4252018-04-10 09:12:46 -0700142#include "intel_workarounds.h"
Oscar Mateo127f1002014-07-24 17:04:11 +0100143
Thomas Daniele981e7b2014-07-24 17:04:39 +0100144#define RING_EXECLIST_QFULL (1 << 0x2)
145#define RING_EXECLIST1_VALID (1 << 0x3)
146#define RING_EXECLIST0_VALID (1 << 0x4)
147#define RING_EXECLIST_ACTIVE_STATUS (3 << 0xE)
148#define RING_EXECLIST1_ACTIVE (1 << 0x11)
149#define RING_EXECLIST0_ACTIVE (1 << 0x12)
150
151#define GEN8_CTX_STATUS_IDLE_ACTIVE (1 << 0)
152#define GEN8_CTX_STATUS_PREEMPTED (1 << 1)
153#define GEN8_CTX_STATUS_ELEMENT_SWITCH (1 << 2)
154#define GEN8_CTX_STATUS_ACTIVE_IDLE (1 << 3)
155#define GEN8_CTX_STATUS_COMPLETE (1 << 4)
156#define GEN8_CTX_STATUS_LITE_RESTORE (1 << 15)
Oscar Mateo8670d6f2014-07-24 17:04:17 +0100157
Chris Wilson70c2a242016-09-09 14:11:46 +0100158#define GEN8_CTX_STATUS_COMPLETED_MASK \
Chris Wilsond8747af2017-11-20 12:34:56 +0000159 (GEN8_CTX_STATUS_COMPLETE | GEN8_CTX_STATUS_PREEMPTED)
Chris Wilson70c2a242016-09-09 14:11:46 +0100160
Chris Wilson0e93cdd2016-04-29 09:07:06 +0100161/* Typical size of the average request (2 pipecontrols and a MI_BB) */
162#define EXECLISTS_REQUEST_SIZE 64 /* bytes */
Chris Wilsona3aabe82016-10-04 21:11:26 +0100163#define WA_TAIL_DWORDS 2
Chris Wilson7e4992a2017-09-28 20:38:59 +0100164#define WA_TAIL_BYTES (sizeof(u32) * WA_TAIL_DWORDS)
Chris Wilsona3aabe82016-10-04 21:11:26 +0100165
Chris Wilsone2efd132016-05-24 14:53:34 +0100166static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
Chris Wilson1fc44d92018-05-17 22:26:32 +0100167 struct intel_engine_cs *engine,
168 struct intel_context *ce);
Chris Wilsona3aabe82016-10-04 21:11:26 +0100169static void execlists_init_reg_state(u32 *reg_state,
170 struct i915_gem_context *ctx,
171 struct intel_engine_cs *engine,
172 struct intel_ring *ring);
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000173
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000174static inline struct i915_priolist *to_priolist(struct rb_node *rb)
175{
176 return rb_entry(rb, struct i915_priolist, node);
177}
178
179static inline int rq_prio(const struct i915_request *rq)
180{
Chris Wilsonb7268c52018-04-18 19:40:52 +0100181 return rq->sched.attr.priority;
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000182}
183
184static inline bool need_preempt(const struct intel_engine_cs *engine,
185 const struct i915_request *last,
186 int prio)
187{
Chris Wilson2a694fe2018-04-03 19:35:37 +0100188 return (intel_engine_has_preemption(engine) &&
Chris Wilsonc5ce3b82018-05-01 13:21:31 +0100189 __execlists_need_preempt(prio, rq_prio(last)) &&
190 !i915_request_completed(last));
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000191}
192
Chris Wilson1fc44d92018-05-17 22:26:32 +0100193/*
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000194 * The context descriptor encodes various attributes of a context,
195 * including its GTT address and some flags. Because it's fairly
196 * expensive to calculate, we'll just do it once and cache the result,
197 * which remains valid until the context is unpinned.
198 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200199 * This is what a descriptor looks like, from LSB to MSB::
200 *
Mika Kuoppala2355cf02017-01-27 15:03:09 +0200201 * bits 0-11: flags, GEN8_CTX_* (cached in ctx->desc_template)
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200202 * bits 12-31: LRCA, GTT address of (the HWSP of) this context
Lionel Landwerlin218b5002018-06-02 12:29:45 +0100203 * bits 32-52: ctx ID, a globally unique tag (highest bit used by GuC)
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200204 * bits 53-54: mbz, reserved for use by hardware
205 * bits 55-63: group ID, currently unused and set to 0
Daniele Ceraolo Spurioac52da62018-03-02 18:14:58 +0200206 *
207 * Starting from Gen11, the upper dword of the descriptor has a new format:
208 *
209 * bits 32-36: reserved
210 * bits 37-47: SW context ID
211 * bits 48:53: engine instance
212 * bit 54: mbz, reserved for use by hardware
213 * bits 55-60: SW counter
214 * bits 61-63: engine class
215 *
216 * engine info, SW context ID and SW counter need to form a unique number
217 * (Context ID) per lrc.
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000218 */
219static void
Chris Wilsone2efd132016-05-24 14:53:34 +0100220intel_lr_context_descriptor_update(struct i915_gem_context *ctx,
Chris Wilson1fc44d92018-05-17 22:26:32 +0100221 struct intel_engine_cs *engine,
222 struct intel_context *ce)
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000223{
Chris Wilson7069b142016-04-28 09:56:52 +0100224 u64 desc;
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000225
Daniele Ceraolo Spurioac52da62018-03-02 18:14:58 +0200226 BUILD_BUG_ON(MAX_CONTEXT_HW_ID > (BIT(GEN8_CTX_ID_WIDTH)));
227 BUILD_BUG_ON(GEN11_MAX_CONTEXT_HW_ID > (BIT(GEN11_SW_CTX_ID_WIDTH)));
Chris Wilson7069b142016-04-28 09:56:52 +0100228
Mika Kuoppala2355cf02017-01-27 15:03:09 +0200229 desc = ctx->desc_template; /* bits 0-11 */
Daniele Ceraolo Spurioac52da62018-03-02 18:14:58 +0200230 GEM_BUG_ON(desc & GENMASK_ULL(63, 12));
231
Michel Thierry0b29c752017-09-13 09:56:00 +0100232 desc |= i915_ggtt_offset(ce->state) + LRC_HEADER_PAGES * PAGE_SIZE;
Chris Wilson9021ad02016-05-24 14:53:37 +0100233 /* bits 12-31 */
Daniele Ceraolo Spurioac52da62018-03-02 18:14:58 +0200234 GEM_BUG_ON(desc & GENMASK_ULL(63, 32));
235
Lionel Landwerlin61d56762018-06-02 12:29:46 +0100236 /*
237 * The following 32bits are copied into the OA reports (dword 2).
238 * Consider updating oa_get_render_ctx_id in i915_perf.c when changing
239 * anything below.
240 */
Daniele Ceraolo Spurioac52da62018-03-02 18:14:58 +0200241 if (INTEL_GEN(ctx->i915) >= 11) {
242 GEM_BUG_ON(ctx->hw_id >= BIT(GEN11_SW_CTX_ID_WIDTH));
243 desc |= (u64)ctx->hw_id << GEN11_SW_CTX_ID_SHIFT;
244 /* bits 37-47 */
245
246 desc |= (u64)engine->instance << GEN11_ENGINE_INSTANCE_SHIFT;
247 /* bits 48-53 */
248
249 /* TODO: decide what to do with SW counter (bits 55-60) */
250
251 desc |= (u64)engine->class << GEN11_ENGINE_CLASS_SHIFT;
252 /* bits 61-63 */
253 } else {
254 GEM_BUG_ON(ctx->hw_id >= BIT(GEN8_CTX_ID_WIDTH));
255 desc |= (u64)ctx->hw_id << GEN8_CTX_ID_SHIFT; /* bits 32-52 */
256 }
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000257
Chris Wilson9021ad02016-05-24 14:53:37 +0100258 ce->lrc_desc = desc;
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000259}
260
Chris Wilsone61e0f52018-02-21 09:56:36 +0000261static void unwind_wa_tail(struct i915_request *rq)
Chris Wilson7e4992a2017-09-28 20:38:59 +0100262{
263 rq->tail = intel_ring_wrap(rq->ring, rq->wa_tail - WA_TAIL_BYTES);
264 assert_ring_tail_valid(rq->ring, rq->tail);
265}
266
Michał Winiarskia4598d12017-10-25 22:00:18 +0200267static void __unwind_incomplete_requests(struct intel_engine_cs *engine)
Chris Wilson7e4992a2017-09-28 20:38:59 +0100268{
Chris Wilsonb16c7652018-10-01 15:47:53 +0100269 struct i915_request *rq, *rn, *active = NULL;
Chris Wilson85f5e1f2018-10-01 13:32:04 +0100270 struct list_head *uninitialized_var(pl);
Chris Wilsonb16c7652018-10-01 15:47:53 +0100271 int prio = I915_PRIORITY_INVALID | I915_PRIORITY_NEWCLIENT;
Chris Wilson7e4992a2017-09-28 20:38:59 +0100272
Chris Wilsona89d1f92018-05-02 17:38:39 +0100273 lockdep_assert_held(&engine->timeline.lock);
Chris Wilson7e4992a2017-09-28 20:38:59 +0100274
275 list_for_each_entry_safe_reverse(rq, rn,
Chris Wilsona89d1f92018-05-02 17:38:39 +0100276 &engine->timeline.requests,
Chris Wilson7e4992a2017-09-28 20:38:59 +0100277 link) {
Chris Wilsone61e0f52018-02-21 09:56:36 +0000278 if (i915_request_completed(rq))
Chris Wilsonb16c7652018-10-01 15:47:53 +0100279 break;
Chris Wilson7e4992a2017-09-28 20:38:59 +0100280
Chris Wilsone61e0f52018-02-21 09:56:36 +0000281 __i915_request_unsubmit(rq);
Chris Wilson7e4992a2017-09-28 20:38:59 +0100282 unwind_wa_tail(rq);
283
Chris Wilsonbc2477f2018-10-03 12:09:41 +0100284 GEM_BUG_ON(rq->hw_context->active);
285
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000286 GEM_BUG_ON(rq_prio(rq) == I915_PRIORITY_INVALID);
Chris Wilsonb16c7652018-10-01 15:47:53 +0100287 if (rq_prio(rq) != prio) {
288 prio = rq_prio(rq);
Chris Wilsone2f34962018-10-01 15:47:54 +0100289 pl = i915_sched_lookup_priolist(engine, prio);
Michał Winiarski097a9482017-09-28 20:39:01 +0100290 }
Chris Wilson8db05f52018-09-19 20:55:16 +0100291 GEM_BUG_ON(RB_EMPTY_ROOT(&engine->execlists.queue.rb_root));
Michał Winiarski097a9482017-09-28 20:39:01 +0100292
Chris Wilson85f5e1f2018-10-01 13:32:04 +0100293 list_add(&rq->sched.link, pl);
Chris Wilsonb16c7652018-10-01 15:47:53 +0100294
295 active = rq;
296 }
297
298 /*
299 * The active request is now effectively the start of a new client
300 * stream, so give it the equivalent small priority bump to prevent
301 * it being gazumped a second time by another peer.
302 */
303 if (!(prio & I915_PRIORITY_NEWCLIENT)) {
304 prio |= I915_PRIORITY_NEWCLIENT;
305 list_move_tail(&active->sched.link,
Chris Wilsone2f34962018-10-01 15:47:54 +0100306 i915_sched_lookup_priolist(engine, prio));
Chris Wilson7e4992a2017-09-28 20:38:59 +0100307 }
308}
309
Michał Winiarskic41937fd2017-10-26 15:35:58 +0200310void
Michał Winiarskia4598d12017-10-25 22:00:18 +0200311execlists_unwind_incomplete_requests(struct intel_engine_execlists *execlists)
312{
313 struct intel_engine_cs *engine =
314 container_of(execlists, typeof(*engine), execlists);
Chris Wilson4413c472018-05-08 22:03:17 +0100315
Michał Winiarskia4598d12017-10-25 22:00:18 +0200316 __unwind_incomplete_requests(engine);
Michał Winiarskia4598d12017-10-25 22:00:18 +0200317}
318
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100319static inline void
Chris Wilsone61e0f52018-02-21 09:56:36 +0000320execlists_context_status_change(struct i915_request *rq, unsigned long status)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100321{
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100322 /*
323 * Only used when GVT-g is enabled now. When GVT-g is disabled,
324 * The compiler should eliminate this function as dead-code.
325 */
326 if (!IS_ENABLED(CONFIG_DRM_I915_GVT))
327 return;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100328
Changbin Du3fc03062017-03-13 10:47:11 +0800329 atomic_notifier_call_chain(&rq->engine->context_status_notifier,
330 status, rq);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100331}
332
Chris Wilsonf2605202018-03-31 14:06:26 +0100333inline void
334execlists_user_begin(struct intel_engine_execlists *execlists,
335 const struct execlist_port *port)
336{
337 execlists_set_active_once(execlists, EXECLISTS_ACTIVE_USER);
338}
339
340inline void
341execlists_user_end(struct intel_engine_execlists *execlists)
342{
343 execlists_clear_active(execlists, EXECLISTS_ACTIVE_USER);
344}
345
Tvrtko Ursulin73fd9d32017-11-21 18:18:47 +0000346static inline void
Chris Wilsone61e0f52018-02-21 09:56:36 +0000347execlists_context_schedule_in(struct i915_request *rq)
Tvrtko Ursulin73fd9d32017-11-21 18:18:47 +0000348{
Chris Wilsonbc2477f2018-10-03 12:09:41 +0100349 GEM_BUG_ON(rq->hw_context->active);
350
Tvrtko Ursulin73fd9d32017-11-21 18:18:47 +0000351 execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_IN);
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +0000352 intel_engine_context_in(rq->engine);
Chris Wilsonbc2477f2018-10-03 12:09:41 +0100353 rq->hw_context->active = rq->engine;
Tvrtko Ursulin73fd9d32017-11-21 18:18:47 +0000354}
355
356static inline void
Chris Wilsonb9b77422018-05-03 00:02:02 +0100357execlists_context_schedule_out(struct i915_request *rq, unsigned long status)
Tvrtko Ursulin73fd9d32017-11-21 18:18:47 +0000358{
Chris Wilsonbc2477f2018-10-03 12:09:41 +0100359 rq->hw_context->active = NULL;
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +0000360 intel_engine_context_out(rq->engine);
Chris Wilsonb9b77422018-05-03 00:02:02 +0100361 execlists_context_status_change(rq, status);
362 trace_i915_request_out(rq);
Tvrtko Ursulin73fd9d32017-11-21 18:18:47 +0000363}
364
Chris Wilsone61e0f52018-02-21 09:56:36 +0000365static u64 execlists_update_context(struct i915_request *rq)
Oscar Mateoae1250b2014-07-24 17:04:37 +0100366{
Chris Wilson1fc44d92018-05-17 22:26:32 +0100367 struct intel_context *ce = rq->hw_context;
Oscar Mateoae1250b2014-07-24 17:04:37 +0100368
Chris Wilsone8894262018-12-07 09:02:13 +0000369 ce->lrc_reg_state[CTX_RING_TAIL + 1] =
370 intel_ring_set_tail(rq->ring, rq->tail);
Chris Wilson70c2a242016-09-09 14:11:46 +0100371
Chris Wilson987abd52018-11-08 08:17:38 +0000372 /*
373 * Make sure the context image is complete before we submit it to HW.
374 *
375 * Ostensibly, writes (including the WCB) should be flushed prior to
376 * an uncached write such as our mmio register access, the empirical
377 * evidence (esp. on Braswell) suggests that the WC write into memory
378 * may not be visible to the HW prior to the completion of the UC
379 * register write and that we may begin execution from the context
380 * before its image is complete leading to invalid PD chasing.
Chris Wilson490b8c62018-12-06 08:44:31 +0000381 *
382 * Furthermore, Braswell, at least, wants a full mb to be sure that
383 * the writes are coherent in memory (visible to the GPU) prior to
384 * execution, and not just visible to other CPUs (as is the result of
385 * wmb).
Chris Wilson987abd52018-11-08 08:17:38 +0000386 */
Chris Wilson490b8c62018-12-06 08:44:31 +0000387 mb();
Chris Wilson70c2a242016-09-09 14:11:46 +0100388 return ce->lrc_desc;
Oscar Mateoae1250b2014-07-24 17:04:37 +0100389}
390
Thomas Daniel05f0add2018-03-02 18:14:59 +0200391static inline void write_desc(struct intel_engine_execlists *execlists, u64 desc, u32 port)
Chris Wilsonbeecec92017-10-03 21:34:52 +0100392{
Thomas Daniel05f0add2018-03-02 18:14:59 +0200393 if (execlists->ctrl_reg) {
394 writel(lower_32_bits(desc), execlists->submit_reg + port * 2);
395 writel(upper_32_bits(desc), execlists->submit_reg + port * 2 + 1);
396 } else {
397 writel(upper_32_bits(desc), execlists->submit_reg);
398 writel(lower_32_bits(desc), execlists->submit_reg);
399 }
Chris Wilsonbeecec92017-10-03 21:34:52 +0100400}
401
Chris Wilson70c2a242016-09-09 14:11:46 +0100402static void execlists_submit_ports(struct intel_engine_cs *engine)
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100403{
Thomas Daniel05f0add2018-03-02 18:14:59 +0200404 struct intel_engine_execlists *execlists = &engine->execlists;
405 struct execlist_port *port = execlists->port;
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100406 unsigned int n;
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100407
Thomas Daniel05f0add2018-03-02 18:14:59 +0200408 /*
Chris Wilsond78d3342018-07-19 08:50:29 +0100409 * We can skip acquiring intel_runtime_pm_get() here as it was taken
410 * on our behalf by the request (see i915_gem_mark_busy()) and it will
411 * not be relinquished until the device is idle (see
412 * i915_gem_idle_work_handler()). As a precaution, we make sure
413 * that all ELSP are drained i.e. we have processed the CSB,
414 * before allowing ourselves to idle and calling intel_runtime_pm_put().
415 */
416 GEM_BUG_ON(!engine->i915->gt.awake);
417
418 /*
Thomas Daniel05f0add2018-03-02 18:14:59 +0200419 * ELSQ note: the submit queue is not cleared after being submitted
420 * to the HW so we need to make sure we always clean it up. This is
421 * currently ensured by the fact that we always write the same number
422 * of elsq entries, keep this in mind before changing the loop below.
423 */
424 for (n = execlists_num_ports(execlists); n--; ) {
Chris Wilsone61e0f52018-02-21 09:56:36 +0000425 struct i915_request *rq;
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100426 unsigned int count;
427 u64 desc;
Chris Wilson70c2a242016-09-09 14:11:46 +0100428
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100429 rq = port_unpack(&port[n], &count);
430 if (rq) {
431 GEM_BUG_ON(count > !n);
432 if (!count++)
Tvrtko Ursulin73fd9d32017-11-21 18:18:47 +0000433 execlists_context_schedule_in(rq);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100434 port_set(&port[n], port_pack(rq, count));
435 desc = execlists_update_context(rq);
436 GEM_DEBUG_EXEC(port[n].context_id = upper_32_bits(desc));
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000437
Tvrtko Ursulin0c5c7df2018-04-06 13:35:14 +0100438 GEM_TRACE("%s in[%d]: ctx=%d.%d, global=%d (fence %llx:%d) (current %d), prio=%d\n",
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000439 engine->name, n,
Chris Wilson16c86192017-12-19 22:09:16 +0000440 port[n].context_id, count,
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000441 rq->global_seqno,
Tvrtko Ursulin0c5c7df2018-04-06 13:35:14 +0100442 rq->fence.context, rq->fence.seqno,
Chris Wilsone7702762018-03-27 22:01:57 +0100443 intel_engine_get_seqno(engine),
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000444 rq_prio(rq));
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100445 } else {
446 GEM_BUG_ON(!n);
447 desc = 0;
448 }
449
Thomas Daniel05f0add2018-03-02 18:14:59 +0200450 write_desc(execlists, desc, n);
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100451 }
Thomas Daniel05f0add2018-03-02 18:14:59 +0200452
453 /* we need to manually load the submit queue */
454 if (execlists->ctrl_reg)
455 writel(EL_CTRL_LOAD, execlists->ctrl_reg);
456
457 execlists_clear_active(execlists, EXECLISTS_ACTIVE_HWACK);
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100458}
459
Chris Wilson1fc44d92018-05-17 22:26:32 +0100460static bool ctx_single_port_submission(const struct intel_context *ce)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100461{
Chris Wilson70c2a242016-09-09 14:11:46 +0100462 return (IS_ENABLED(CONFIG_DRM_I915_GVT) &&
Chris Wilson1fc44d92018-05-17 22:26:32 +0100463 i915_gem_context_force_single_submission(ce->gem_context));
Ben Widawsky84b790f2014-07-24 17:04:36 +0100464}
465
Chris Wilson1fc44d92018-05-17 22:26:32 +0100466static bool can_merge_ctx(const struct intel_context *prev,
467 const struct intel_context *next)
Michel Thierryacdd8842014-07-24 17:04:38 +0100468{
Chris Wilson70c2a242016-09-09 14:11:46 +0100469 if (prev != next)
470 return false;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100471
Chris Wilson70c2a242016-09-09 14:11:46 +0100472 if (ctx_single_port_submission(prev))
473 return false;
Michel Thierryacdd8842014-07-24 17:04:38 +0100474
Chris Wilson70c2a242016-09-09 14:11:46 +0100475 return true;
476}
Peter Antoine779949f2015-05-11 16:03:27 +0100477
Chris Wilsone61e0f52018-02-21 09:56:36 +0000478static void port_assign(struct execlist_port *port, struct i915_request *rq)
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100479{
480 GEM_BUG_ON(rq == port_request(port));
481
482 if (port_isset(port))
Chris Wilsone61e0f52018-02-21 09:56:36 +0000483 i915_request_put(port_request(port));
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100484
Chris Wilsone61e0f52018-02-21 09:56:36 +0000485 port_set(port, port_pack(i915_request_get(rq), port_count(port)));
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100486}
487
Chris Wilsonbeecec92017-10-03 21:34:52 +0100488static void inject_preempt_context(struct intel_engine_cs *engine)
489{
Thomas Daniel05f0add2018-03-02 18:14:59 +0200490 struct intel_engine_execlists *execlists = &engine->execlists;
Chris Wilsonbeecec92017-10-03 21:34:52 +0100491 struct intel_context *ce =
Chris Wilsonab82a062018-04-30 14:15:01 +0100492 to_intel_context(engine->i915->preempt_context, engine);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100493 unsigned int n;
494
Thomas Daniel05f0add2018-03-02 18:14:59 +0200495 GEM_BUG_ON(execlists->preempt_complete_status !=
Chris Wilsond6376372018-02-07 21:05:44 +0000496 upper_32_bits(ce->lrc_desc));
Chris Wilson09b1a4e2018-01-25 11:24:42 +0000497
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000498 /*
499 * Switch to our empty preempt context so
500 * the state of the GPU is known (idle).
501 */
Chris Wilson16a87392017-12-20 09:06:26 +0000502 GEM_TRACE("%s\n", engine->name);
Thomas Daniel05f0add2018-03-02 18:14:59 +0200503 for (n = execlists_num_ports(execlists); --n; )
504 write_desc(execlists, 0, n);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100505
Thomas Daniel05f0add2018-03-02 18:14:59 +0200506 write_desc(execlists, ce->lrc_desc, n);
507
508 /* we need to manually load the submit queue */
509 if (execlists->ctrl_reg)
510 writel(EL_CTRL_LOAD, execlists->ctrl_reg);
511
Chris Wilsonef2fb722018-05-16 19:33:50 +0100512 execlists_clear_active(execlists, EXECLISTS_ACTIVE_HWACK);
513 execlists_set_active(execlists, EXECLISTS_ACTIVE_PREEMPT);
514}
515
516static void complete_preempt_context(struct intel_engine_execlists *execlists)
517{
518 GEM_BUG_ON(!execlists_is_active(execlists, EXECLISTS_ACTIVE_PREEMPT));
519
Chris Wilson0f6b79f2018-07-16 14:21:54 +0100520 if (inject_preempt_hang(execlists))
521 return;
522
Chris Wilsonef2fb722018-05-16 19:33:50 +0100523 execlists_cancel_port_requests(execlists);
Chris Wilson9512f982018-06-28 21:12:11 +0100524 __unwind_incomplete_requests(container_of(execlists,
525 struct intel_engine_cs,
526 execlists));
Chris Wilsonbeecec92017-10-03 21:34:52 +0100527}
528
Chris Wilson9512f982018-06-28 21:12:11 +0100529static void execlists_dequeue(struct intel_engine_cs *engine)
Chris Wilson70c2a242016-09-09 14:11:46 +0100530{
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300531 struct intel_engine_execlists * const execlists = &engine->execlists;
532 struct execlist_port *port = execlists->port;
Mika Kuoppala76e70082017-09-22 15:43:07 +0300533 const struct execlist_port * const last_port =
534 &execlists->port[execlists->port_mask];
Chris Wilsone61e0f52018-02-21 09:56:36 +0000535 struct i915_request *last = port_request(port);
Chris Wilson20311bd2016-11-14 20:41:03 +0000536 struct rb_node *rb;
Chris Wilson70c2a242016-09-09 14:11:46 +0100537 bool submit = false;
Michel Thierryacdd8842014-07-24 17:04:38 +0100538
Chris Wilson9512f982018-06-28 21:12:11 +0100539 /*
540 * Hardware submission is through 2 ports. Conceptually each port
Chris Wilson70c2a242016-09-09 14:11:46 +0100541 * has a (RING_START, RING_HEAD, RING_TAIL) tuple. RING_START is
542 * static for a context, and unique to each, so we only execute
543 * requests belonging to a single context from each ring. RING_HEAD
544 * is maintained by the CS in the context image, it marks the place
545 * where it got up to last time, and through RING_TAIL we tell the CS
546 * where we want to execute up to this time.
547 *
548 * In this list the requests are in order of execution. Consecutive
549 * requests from the same context are adjacent in the ringbuffer. We
550 * can combine these requests into a single RING_TAIL update:
551 *
552 * RING_HEAD...req1...req2
553 * ^- RING_TAIL
554 * since to execute req2 the CS must first execute req1.
555 *
556 * Our goal then is to point each port to the end of a consecutive
557 * sequence of requests as being the most optimal (fewest wake ups
558 * and context switches) submission.
559 */
560
Chris Wilsonbeecec92017-10-03 21:34:52 +0100561 if (last) {
562 /*
563 * Don't resubmit or switch until all outstanding
564 * preemptions (lite-restore) are seen. Then we
565 * know the next preemption status we see corresponds
566 * to this ELSP update.
567 */
Chris Wilsoneed7ec52018-03-24 12:58:29 +0000568 GEM_BUG_ON(!execlists_is_active(execlists,
569 EXECLISTS_ACTIVE_USER));
Michel Thierryba74cb12017-11-20 12:34:58 +0000570 GEM_BUG_ON(!port_count(&port[0]));
Chris Wilsonbeecec92017-10-03 21:34:52 +0100571
Michel Thierryba74cb12017-11-20 12:34:58 +0000572 /*
573 * If we write to ELSP a second time before the HW has had
574 * a chance to respond to the previous write, we can confuse
575 * the HW and hit "undefined behaviour". After writing to ELSP,
576 * we must then wait until we see a context-switch event from
577 * the HW to indicate that it has had a chance to respond.
578 */
579 if (!execlists_is_active(execlists, EXECLISTS_ACTIVE_HWACK))
Chris Wilson0b02bef2018-06-28 21:12:04 +0100580 return;
Michel Thierryba74cb12017-11-20 12:34:58 +0000581
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000582 if (need_preempt(engine, last, execlists->queue_priority)) {
Chris Wilsonbeecec92017-10-03 21:34:52 +0100583 inject_preempt_context(engine);
Chris Wilson0b02bef2018-06-28 21:12:04 +0100584 return;
Chris Wilsonbeecec92017-10-03 21:34:52 +0100585 }
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000586
587 /*
588 * In theory, we could coalesce more requests onto
589 * the second port (the first port is active, with
590 * no preemptions pending). However, that means we
591 * then have to deal with the possible lite-restore
592 * of the second port (as we submit the ELSP, there
593 * may be a context-switch) but also we may complete
594 * the resubmission before the context-switch. Ergo,
595 * coalescing onto the second port will cause a
596 * preemption event, but we cannot predict whether
597 * that will affect port[0] or port[1].
598 *
599 * If the second port is already active, we can wait
600 * until the next context-switch before contemplating
601 * new requests. The GPU will be busy and we should be
602 * able to resubmit the new ELSP before it idles,
603 * avoiding pipeline bubbles (momentary pauses where
604 * the driver is unable to keep up the supply of new
605 * work). However, we have to double check that the
606 * priorities of the ports haven't been switch.
607 */
608 if (port_count(&port[1]))
Chris Wilson0b02bef2018-06-28 21:12:04 +0100609 return;
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000610
611 /*
612 * WaIdleLiteRestore:bdw,skl
613 * Apply the wa NOOPs to prevent
614 * ring:HEAD == rq:TAIL as we resubmit the
615 * request. See gen8_emit_breadcrumb() for
616 * where we prepare the padding after the
617 * end of the request.
618 */
619 last->tail = last->wa_tail;
Chris Wilsonbeecec92017-10-03 21:34:52 +0100620 }
621
Chris Wilson655250a2018-06-29 08:53:20 +0100622 while ((rb = rb_first_cached(&execlists->queue))) {
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000623 struct i915_priolist *p = to_priolist(rb);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000624 struct i915_request *rq, *rn;
Chris Wilson85f5e1f2018-10-01 13:32:04 +0100625 int i;
Chris Wilson20311bd2016-11-14 20:41:03 +0000626
Chris Wilson85f5e1f2018-10-01 13:32:04 +0100627 priolist_for_each_request_consume(rq, rn, p, i) {
Chris Wilson6c067572017-05-17 13:10:03 +0100628 /*
629 * Can we combine this request with the current port?
630 * It has to be the same context/ringbuffer and not
631 * have any exceptions (e.g. GVT saying never to
632 * combine contexts).
633 *
634 * If we can combine the requests, we can execute both
635 * by updating the RING_TAIL to point to the end of the
636 * second request, and so we never need to tell the
637 * hardware about the first.
Chris Wilson70c2a242016-09-09 14:11:46 +0100638 */
Chris Wilson1fc44d92018-05-17 22:26:32 +0100639 if (last &&
640 !can_merge_ctx(rq->hw_context, last->hw_context)) {
Chris Wilson6c067572017-05-17 13:10:03 +0100641 /*
642 * If we are on the second port and cannot
643 * combine this request with the last, then we
644 * are done.
645 */
Chris Wilson85f5e1f2018-10-01 13:32:04 +0100646 if (port == last_port)
Chris Wilson6c067572017-05-17 13:10:03 +0100647 goto done;
Chris Wilson70c2a242016-09-09 14:11:46 +0100648
Chris Wilson6c067572017-05-17 13:10:03 +0100649 /*
650 * If GVT overrides us we only ever submit
651 * port[0], leaving port[1] empty. Note that we
652 * also have to be careful that we don't queue
653 * the same context (even though a different
654 * request) to the second port.
655 */
Chris Wilson1fc44d92018-05-17 22:26:32 +0100656 if (ctx_single_port_submission(last->hw_context) ||
Chris Wilson85f5e1f2018-10-01 13:32:04 +0100657 ctx_single_port_submission(rq->hw_context))
Chris Wilson6c067572017-05-17 13:10:03 +0100658 goto done;
Chris Wilson70c2a242016-09-09 14:11:46 +0100659
Chris Wilson1fc44d92018-05-17 22:26:32 +0100660 GEM_BUG_ON(last->hw_context == rq->hw_context);
Chris Wilson70c2a242016-09-09 14:11:46 +0100661
Chris Wilson6c067572017-05-17 13:10:03 +0100662 if (submit)
663 port_assign(port, last);
664 port++;
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300665
666 GEM_BUG_ON(port_isset(port));
Chris Wilson6c067572017-05-17 13:10:03 +0100667 }
668
Chris Wilson85f5e1f2018-10-01 13:32:04 +0100669 list_del_init(&rq->sched.link);
670
Chris Wilsone61e0f52018-02-21 09:56:36 +0000671 __i915_request_submit(rq);
672 trace_i915_request_in(rq, port_index(port, execlists));
Chris Wilson85f5e1f2018-10-01 13:32:04 +0100673
Chris Wilson6c067572017-05-17 13:10:03 +0100674 last = rq;
675 submit = true;
Chris Wilson70c2a242016-09-09 14:11:46 +0100676 }
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000677
Chris Wilson655250a2018-06-29 08:53:20 +0100678 rb_erase_cached(&p->node, &execlists->queue);
Chris Wilson6c067572017-05-17 13:10:03 +0100679 if (p->priority != I915_PRIORITY_NORMAL)
Chris Wilsonc5cf9a92017-05-17 13:10:04 +0100680 kmem_cache_free(engine->i915->priorities, p);
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000681 }
Chris Wilson15c83c42018-04-11 11:39:29 +0100682
Chris Wilson6c067572017-05-17 13:10:03 +0100683done:
Chris Wilson15c83c42018-04-11 11:39:29 +0100684 /*
685 * Here be a bit of magic! Or sleight-of-hand, whichever you prefer.
686 *
687 * We choose queue_priority such that if we add a request of greater
688 * priority than this, we kick the submission tasklet to decide on
689 * the right order of submitting the requests to hardware. We must
690 * also be prepared to reorder requests as they are in-flight on the
691 * HW. We derive the queue_priority then as the first "hole" in
692 * the HW submission ports and if there are no available slots,
693 * the priority of the lowest executing request, i.e. last.
694 *
695 * When we do receive a higher priority request ready to run from the
696 * user, see queue_request(), the queue_priority is bumped to that
697 * request triggering preemption on the next dequeue (or subsequent
698 * interrupt for secondary ports).
699 */
700 execlists->queue_priority =
701 port != execlists->port ? rq_prio(last) : INT_MIN;
702
Chris Wilson0b02bef2018-06-28 21:12:04 +0100703 if (submit) {
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100704 port_assign(port, last);
Chris Wilson0b02bef2018-06-28 21:12:04 +0100705 execlists_submit_ports(engine);
706 }
Chris Wilson339ccd32018-02-15 16:25:53 +0000707
708 /* We must always keep the beast fed if we have work piled up */
Chris Wilson655250a2018-06-29 08:53:20 +0100709 GEM_BUG_ON(rb_first_cached(&execlists->queue) &&
710 !port_isset(execlists->port));
Chris Wilson339ccd32018-02-15 16:25:53 +0000711
Chris Wilson4413c472018-05-08 22:03:17 +0100712 /* Re-evaluate the executing context setup after each preemptive kick */
713 if (last)
Chris Wilsonf2605202018-03-31 14:06:26 +0100714 execlists_user_begin(execlists, execlists->port);
Chris Wilson4413c472018-05-08 22:03:17 +0100715
Chris Wilson0b02bef2018-06-28 21:12:04 +0100716 /* If the engine is now idle, so should be the flag; and vice versa. */
717 GEM_BUG_ON(execlists_is_active(&engine->execlists,
718 EXECLISTS_ACTIVE_USER) ==
719 !port_isset(engine->execlists.port));
Chris Wilson4413c472018-05-08 22:03:17 +0100720}
721
Michał Winiarskic41937fd2017-10-26 15:35:58 +0200722void
Michał Winiarskia4598d12017-10-25 22:00:18 +0200723execlists_cancel_port_requests(struct intel_engine_execlists * const execlists)
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300724{
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100725 struct execlist_port *port = execlists->port;
Mika Kuoppaladc2279e2017-10-10 14:48:57 +0300726 unsigned int num_ports = execlists_num_ports(execlists);
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300727
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100728 while (num_ports-- && port_isset(port)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +0000729 struct i915_request *rq = port_request(port);
Chris Wilson7e44fc22017-09-26 11:17:19 +0100730
Tvrtko Ursulin0c5c7df2018-04-06 13:35:14 +0100731 GEM_TRACE("%s:port%u global=%d (fence %llx:%d), (current %d)\n",
732 rq->engine->name,
733 (unsigned int)(port - execlists->port),
734 rq->global_seqno,
735 rq->fence.context, rq->fence.seqno,
736 intel_engine_get_seqno(rq->engine));
737
Chris Wilson4a118ec2017-10-23 22:32:36 +0100738 GEM_BUG_ON(!execlists->active);
Chris Wilsonb9b77422018-05-03 00:02:02 +0100739 execlists_context_schedule_out(rq,
740 i915_request_completed(rq) ?
741 INTEL_CONTEXT_SCHEDULE_OUT :
742 INTEL_CONTEXT_SCHEDULE_PREEMPTED);
Weinan Li702791f2018-03-06 10:15:57 +0800743
Chris Wilsone61e0f52018-02-21 09:56:36 +0000744 i915_request_put(rq);
Chris Wilson7e44fc22017-09-26 11:17:19 +0100745
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100746 memset(port, 0, sizeof(*port));
747 port++;
748 }
Chris Wilsoneed7ec52018-03-24 12:58:29 +0000749
Chris Wilson00511632018-07-16 13:54:24 +0100750 execlists_clear_all_active(execlists);
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300751}
752
Mika Kuoppalad8f505312018-12-05 15:46:12 +0200753static inline void
754invalidate_csb_entries(const u32 *first, const u32 *last)
755{
756 clflush((void *)first);
757 clflush((void *)last);
758}
759
Chris Wilsonf4b58f02018-06-28 21:12:08 +0100760static void reset_csb_pointers(struct intel_engine_execlists *execlists)
761{
Chris Wilson46592892018-11-30 12:59:54 +0000762 const unsigned int reset_value = GEN8_CSB_ENTRIES - 1;
763
Chris Wilsonf4b58f02018-06-28 21:12:08 +0100764 /*
765 * After a reset, the HW starts writing into CSB entry [0]. We
766 * therefore have to set our HEAD pointer back one entry so that
767 * the *first* entry we check is entry 0. To complicate this further,
768 * as we don't wait for the first interrupt after reset, we have to
769 * fake the HW write to point back to the last entry so that our
770 * inline comparison of our cached head position against the last HW
771 * write works even before the first interrupt.
772 */
Chris Wilson46592892018-11-30 12:59:54 +0000773 execlists->csb_head = reset_value;
774 WRITE_ONCE(*execlists->csb_write, reset_value);
Mika Kuoppalad8f505312018-12-05 15:46:12 +0200775
776 invalidate_csb_entries(&execlists->csb_status[0],
777 &execlists->csb_status[GEN8_CSB_ENTRIES - 1]);
Chris Wilsonf4b58f02018-06-28 21:12:08 +0100778}
779
Chris Wilsonf1a498f2018-07-16 09:03:30 +0100780static void nop_submission_tasklet(unsigned long data)
781{
782 /* The driver is wedged; don't process any more events. */
783}
784
Chris Wilson27a5f612017-09-15 18:31:00 +0100785static void execlists_cancel_requests(struct intel_engine_cs *engine)
786{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300787 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilsone61e0f52018-02-21 09:56:36 +0000788 struct i915_request *rq, *rn;
Chris Wilson27a5f612017-09-15 18:31:00 +0100789 struct rb_node *rb;
790 unsigned long flags;
Chris Wilson27a5f612017-09-15 18:31:00 +0100791
Tvrtko Ursulin0c5c7df2018-04-06 13:35:14 +0100792 GEM_TRACE("%s current %d\n",
793 engine->name, intel_engine_get_seqno(engine));
Chris Wilson963ddd62018-03-02 11:33:24 +0000794
Chris Wilsona3e38832018-03-02 14:32:45 +0000795 /*
796 * Before we call engine->cancel_requests(), we should have exclusive
797 * access to the submission state. This is arranged for us by the
798 * caller disabling the interrupt generation, the tasklet and other
799 * threads that may then access the same state, giving us a free hand
800 * to reset state. However, we still need to let lockdep be aware that
801 * we know this state may be accessed in hardirq context, so we
802 * disable the irq around this manipulation and we want to keep
803 * the spinlock focused on its duties and not accidentally conflate
804 * coverage to the submission's irq state. (Similarly, although we
805 * shouldn't need to disable irq around the manipulation of the
806 * submission's irq state, we also wish to remind ourselves that
807 * it is irq state.)
808 */
Chris Wilsond8857d52018-06-28 21:12:05 +0100809 spin_lock_irqsave(&engine->timeline.lock, flags);
Chris Wilson27a5f612017-09-15 18:31:00 +0100810
811 /* Cancel the requests on the HW and clear the ELSP tracker. */
Michał Winiarskia4598d12017-10-25 22:00:18 +0200812 execlists_cancel_port_requests(execlists);
Chris Wilson00511632018-07-16 13:54:24 +0100813 execlists_user_end(execlists);
Chris Wilson27a5f612017-09-15 18:31:00 +0100814
815 /* Mark all executing requests as skipped. */
Chris Wilsona89d1f92018-05-02 17:38:39 +0100816 list_for_each_entry(rq, &engine->timeline.requests, link) {
Chris Wilson27a5f612017-09-15 18:31:00 +0100817 GEM_BUG_ON(!rq->global_seqno);
Chris Wilson38009602018-12-03 11:36:55 +0000818
819 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &rq->fence.flags))
820 continue;
821
822 dma_fence_set_error(&rq->fence, -EIO);
Chris Wilson27a5f612017-09-15 18:31:00 +0100823 }
824
825 /* Flush the queued requests to the timeline list (for retiring). */
Chris Wilson655250a2018-06-29 08:53:20 +0100826 while ((rb = rb_first_cached(&execlists->queue))) {
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000827 struct i915_priolist *p = to_priolist(rb);
Chris Wilson85f5e1f2018-10-01 13:32:04 +0100828 int i;
Chris Wilson27a5f612017-09-15 18:31:00 +0100829
Chris Wilson85f5e1f2018-10-01 13:32:04 +0100830 priolist_for_each_request_consume(rq, rn, p, i) {
831 list_del_init(&rq->sched.link);
Chris Wilson27a5f612017-09-15 18:31:00 +0100832
833 dma_fence_set_error(&rq->fence, -EIO);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000834 __i915_request_submit(rq);
Chris Wilson27a5f612017-09-15 18:31:00 +0100835 }
836
Chris Wilson655250a2018-06-29 08:53:20 +0100837 rb_erase_cached(&p->node, &execlists->queue);
Chris Wilson27a5f612017-09-15 18:31:00 +0100838 if (p->priority != I915_PRIORITY_NORMAL)
839 kmem_cache_free(engine->i915->priorities, p);
840 }
841
Chris Wilson38009602018-12-03 11:36:55 +0000842 intel_write_status_page(engine,
843 I915_GEM_HWS_INDEX,
844 intel_engine_last_submit(engine));
845
Chris Wilson27a5f612017-09-15 18:31:00 +0100846 /* Remaining _unready_ requests will be nop'ed when submitted */
847
Chris Wilsonf6322ed2018-02-22 14:22:29 +0000848 execlists->queue_priority = INT_MIN;
Chris Wilson655250a2018-06-29 08:53:20 +0100849 execlists->queue = RB_ROOT_CACHED;
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100850 GEM_BUG_ON(port_isset(execlists->port));
Chris Wilson27a5f612017-09-15 18:31:00 +0100851
Chris Wilsonf1a498f2018-07-16 09:03:30 +0100852 GEM_BUG_ON(__tasklet_is_enabled(&execlists->tasklet));
853 execlists->tasklet.func = nop_submission_tasklet;
854
Chris Wilsond8857d52018-06-28 21:12:05 +0100855 spin_unlock_irqrestore(&engine->timeline.lock, flags);
Chris Wilson27a5f612017-09-15 18:31:00 +0100856}
857
Chris Wilson9512f982018-06-28 21:12:11 +0100858static inline bool
859reset_in_progress(const struct intel_engine_execlists *execlists)
860{
861 return unlikely(!__tasklet_is_enabled(&execlists->tasklet));
862}
863
Chris Wilson73377db2018-05-16 19:33:53 +0100864static void process_csb(struct intel_engine_cs *engine)
Thomas Daniele981e7b2014-07-24 17:04:39 +0100865{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300866 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilsonf2605202018-03-31 14:06:26 +0100867 struct execlist_port *port = execlists->port;
Chris Wilsonbc4237e2018-06-28 21:12:07 +0100868 const u32 * const buf = execlists->csb_status;
869 u8 head, tail;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100870
Chris Wilsonbc4237e2018-06-28 21:12:07 +0100871 /*
872 * Note that csb_write, csb_status may be either in HWSP or mmio.
873 * When reading from the csb_write mmio register, we have to be
874 * careful to only use the GEN8_CSB_WRITE_PTR portion, which is
875 * the low 4bits. As it happens we know the next 4bits are always
876 * zero and so we can simply masked off the low u8 of the register
877 * and treat it identically to reading from the HWSP (without having
878 * to use explicit shifting and masking, and probably bifurcating
879 * the code to handle the legacy mmio read).
880 */
881 head = execlists->csb_head;
882 tail = READ_ONCE(*execlists->csb_write);
883 GEM_TRACE("%s cs-irq head=%d, tail=%d\n", engine->name, head, tail);
884 if (unlikely(head == tail))
885 return;
Chris Wilson9153e6b2018-03-21 09:10:27 +0000886
Chris Wilsonbc4237e2018-06-28 21:12:07 +0100887 /*
888 * Hopefully paired with a wmb() in HW!
889 *
890 * We must complete the read of the write pointer before any reads
891 * from the CSB, so that we do not see stale values. Without an rmb
892 * (lfence) the HW may speculatively perform the CSB[] reads *before*
893 * we perform the READ_ONCE(*csb_write).
894 */
895 rmb();
Chris Wilsonbb5db7e2018-01-22 10:07:14 +0000896
Chris Wilsonbc4237e2018-06-28 21:12:07 +0100897 do {
Chris Wilson8ea397f2018-06-28 21:12:06 +0100898 struct i915_request *rq;
899 unsigned int status;
900 unsigned int count;
901
902 if (++head == GEN8_CSB_ENTRIES)
903 head = 0;
904
905 /*
906 * We are flying near dragons again.
907 *
908 * We hold a reference to the request in execlist_port[]
909 * but no more than that. We are operating in softirq
910 * context and so cannot hold any mutex or sleep. That
911 * prevents us stopping the requests we are processing
912 * in port[] from being retired simultaneously (the
913 * breadcrumb will be complete before we see the
914 * context-switch). As we only hold the reference to the
915 * request, any pointer chasing underneath the request
916 * is subject to a potential use-after-free. Thus we
917 * store all of the bookkeeping within port[] as
918 * required, and avoid using unguarded pointers beneath
919 * request itself. The same applies to the atomic
920 * status notifier.
921 */
922
Chris Wilson8ea397f2018-06-28 21:12:06 +0100923 GEM_TRACE("%s csb[%d]: status=0x%08x:0x%08x, active=0x%x\n",
924 engine->name, head,
Chris Wilsonbc4237e2018-06-28 21:12:07 +0100925 buf[2 * head + 0], buf[2 * head + 1],
Chris Wilson8ea397f2018-06-28 21:12:06 +0100926 execlists->active);
927
Chris Wilsonbc4237e2018-06-28 21:12:07 +0100928 status = buf[2 * head];
Chris Wilson8ea397f2018-06-28 21:12:06 +0100929 if (status & (GEN8_CTX_STATUS_IDLE_ACTIVE |
930 GEN8_CTX_STATUS_PREEMPTED))
931 execlists_set_active(execlists,
932 EXECLISTS_ACTIVE_HWACK);
933 if (status & GEN8_CTX_STATUS_ACTIVE_IDLE)
934 execlists_clear_active(execlists,
935 EXECLISTS_ACTIVE_HWACK);
936
937 if (!(status & GEN8_CTX_STATUS_COMPLETED_MASK))
938 continue;
939
940 /* We should never get a COMPLETED | IDLE_ACTIVE! */
941 GEM_BUG_ON(status & GEN8_CTX_STATUS_IDLE_ACTIVE);
942
943 if (status & GEN8_CTX_STATUS_COMPLETE &&
944 buf[2*head + 1] == execlists->preempt_complete_status) {
945 GEM_TRACE("%s preempt-idle\n", engine->name);
946 complete_preempt_context(execlists);
947 continue;
Chris Wilson767a9832017-09-13 09:56:05 +0100948 }
Chris Wilson8ea397f2018-06-28 21:12:06 +0100949
950 if (status & GEN8_CTX_STATUS_PREEMPTED &&
951 execlists_is_active(execlists,
952 EXECLISTS_ACTIVE_PREEMPT))
953 continue;
954
955 GEM_BUG_ON(!execlists_is_active(execlists,
956 EXECLISTS_ACTIVE_USER));
957
958 rq = port_unpack(port, &count);
959 GEM_TRACE("%s out[0]: ctx=%d.%d, global=%d (fence %llx:%d) (current %d), prio=%d\n",
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000960 engine->name,
Chris Wilson8ea397f2018-06-28 21:12:06 +0100961 port->context_id, count,
962 rq ? rq->global_seqno : 0,
963 rq ? rq->fence.context : 0,
964 rq ? rq->fence.seqno : 0,
965 intel_engine_get_seqno(engine),
966 rq ? rq_prio(rq) : 0);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300967
Chris Wilson8ea397f2018-06-28 21:12:06 +0100968 /* Check the context/desc id for this event matches */
969 GEM_DEBUG_BUG_ON(buf[2 * head + 1] != port->context_id);
Chris Wilsona37951a2017-01-24 11:00:06 +0000970
Chris Wilson8ea397f2018-06-28 21:12:06 +0100971 GEM_BUG_ON(count == 0);
972 if (--count == 0) {
973 /*
974 * On the final event corresponding to the
975 * submission of this context, we expect either
976 * an element-switch event or a completion
977 * event (and on completion, the active-idle
978 * marker). No more preemptions, lite-restore
979 * or otherwise.
980 */
981 GEM_BUG_ON(status & GEN8_CTX_STATUS_PREEMPTED);
982 GEM_BUG_ON(port_isset(&port[1]) &&
983 !(status & GEN8_CTX_STATUS_ELEMENT_SWITCH));
984 GEM_BUG_ON(!port_isset(&port[1]) &&
985 !(status & GEN8_CTX_STATUS_ACTIVE_IDLE));
Thomas Daniele981e7b2014-07-24 17:04:39 +0100986
Chris Wilson73377db2018-05-16 19:33:53 +0100987 /*
Chris Wilson8ea397f2018-06-28 21:12:06 +0100988 * We rely on the hardware being strongly
989 * ordered, that the breadcrumb write is
990 * coherent (visible from the CPU) before the
991 * user interrupt and CSB is processed.
Chris Wilson2ffe80a2017-02-06 17:05:02 +0000992 */
Chris Wilson8ea397f2018-06-28 21:12:06 +0100993 GEM_BUG_ON(!i915_request_completed(rq));
Chris Wilson2ffe80a2017-02-06 17:05:02 +0000994
Chris Wilson8ea397f2018-06-28 21:12:06 +0100995 execlists_context_schedule_out(rq,
996 INTEL_CONTEXT_SCHEDULE_OUT);
997 i915_request_put(rq);
Michel Thierryba74cb12017-11-20 12:34:58 +0000998
Chris Wilson8ea397f2018-06-28 21:12:06 +0100999 GEM_TRACE("%s completed ctx=%d\n",
1000 engine->name, port->context_id);
Michel Thierryba74cb12017-11-20 12:34:58 +00001001
Chris Wilson8ea397f2018-06-28 21:12:06 +01001002 port = execlists_port_complete(execlists, port);
1003 if (port_isset(port))
1004 execlists_user_begin(execlists, port);
1005 else
1006 execlists_user_end(execlists);
1007 } else {
1008 port_set(port, port_pack(rq, count));
Chris Wilson4af0d722017-03-25 20:10:53 +00001009 }
Chris Wilsonbc4237e2018-06-28 21:12:07 +01001010 } while (head != tail);
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +00001011
Chris Wilsonbc4237e2018-06-28 21:12:07 +01001012 execlists->csb_head = head;
Mika Kuoppalad8f505312018-12-05 15:46:12 +02001013
1014 /*
1015 * Gen11 has proven to fail wrt global observation point between
1016 * entry and tail update, failing on the ordering and thus
1017 * we see an old entry in the context status buffer.
1018 *
1019 * Forcibly evict out entries for the next gpu csb update,
1020 * to increase the odds that we get a fresh entries with non
1021 * working hardware. The cost for doing so comes out mostly with
1022 * the wash as hardware, working or not, will need to do the
1023 * invalidation before.
1024 */
1025 invalidate_csb_entries(&buf[0], &buf[GEN8_CSB_ENTRIES - 1]);
Chris Wilson73377db2018-05-16 19:33:53 +01001026}
1027
Chris Wilson9512f982018-06-28 21:12:11 +01001028static void __execlists_submission_tasklet(struct intel_engine_cs *const engine)
Chris Wilson73377db2018-05-16 19:33:53 +01001029{
Chris Wilson9512f982018-06-28 21:12:11 +01001030 lockdep_assert_held(&engine->timeline.lock);
Chris Wilson73377db2018-05-16 19:33:53 +01001031
Chris Wilsonfd8526e2018-06-28 21:12:10 +01001032 process_csb(engine);
Chris Wilson73377db2018-05-16 19:33:53 +01001033 if (!execlists_is_active(&engine->execlists, EXECLISTS_ACTIVE_PREEMPT))
Chris Wilson70c2a242016-09-09 14:11:46 +01001034 execlists_dequeue(engine);
Thomas Daniele981e7b2014-07-24 17:04:39 +01001035}
1036
Chris Wilson9512f982018-06-28 21:12:11 +01001037/*
1038 * Check the unread Context Status Buffers and manage the submission of new
1039 * contexts to the ELSP accordingly.
1040 */
1041static void execlists_submission_tasklet(unsigned long data)
1042{
1043 struct intel_engine_cs * const engine = (struct intel_engine_cs *)data;
1044 unsigned long flags;
1045
1046 GEM_TRACE("%s awake?=%d, active=%x\n",
1047 engine->name,
Chris Wilson8d761e72019-01-14 14:21:28 +00001048 !!engine->i915->gt.awake,
Chris Wilson9512f982018-06-28 21:12:11 +01001049 engine->execlists.active);
1050
1051 spin_lock_irqsave(&engine->timeline.lock, flags);
Chris Wilsond78d3342018-07-19 08:50:29 +01001052 __execlists_submission_tasklet(engine);
Chris Wilson9512f982018-06-28 21:12:11 +01001053 spin_unlock_irqrestore(&engine->timeline.lock, flags);
1054}
1055
Chris Wilsonf6322ed2018-02-22 14:22:29 +00001056static void queue_request(struct intel_engine_cs *engine,
Chris Wilson0c7112a2018-04-18 19:40:51 +01001057 struct i915_sched_node *node,
Chris Wilsonf6322ed2018-02-22 14:22:29 +00001058 int prio)
Chris Wilson27606fd2017-09-16 21:44:13 +01001059{
Chris Wilsone2f34962018-10-01 15:47:54 +01001060 list_add_tail(&node->link, i915_sched_lookup_priolist(engine, prio));
Chris Wilson9512f982018-06-28 21:12:11 +01001061}
1062
1063static void __submit_queue_imm(struct intel_engine_cs *engine)
1064{
1065 struct intel_engine_execlists * const execlists = &engine->execlists;
1066
1067 if (reset_in_progress(execlists))
1068 return; /* defer until we restart the engine following reset */
1069
1070 if (execlists->tasklet.func == execlists_submission_tasklet)
1071 __execlists_submission_tasklet(engine);
1072 else
1073 tasklet_hi_schedule(&execlists->tasklet);
Chris Wilsonae2f5c02018-03-26 12:50:34 +01001074}
1075
Chris Wilsonf6322ed2018-02-22 14:22:29 +00001076static void submit_queue(struct intel_engine_cs *engine, int prio)
1077{
Chris Wilson9512f982018-06-28 21:12:11 +01001078 if (prio > engine->execlists.queue_priority) {
Chris Wilsone2f34962018-10-01 15:47:54 +01001079 engine->execlists.queue_priority = prio;
Chris Wilson9512f982018-06-28 21:12:11 +01001080 __submit_queue_imm(engine);
1081 }
Chris Wilson27606fd2017-09-16 21:44:13 +01001082}
1083
Chris Wilsone61e0f52018-02-21 09:56:36 +00001084static void execlists_submit_request(struct i915_request *request)
Michel Thierryacdd8842014-07-24 17:04:38 +01001085{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001086 struct intel_engine_cs *engine = request->engine;
Chris Wilson5590af32016-09-09 14:11:54 +01001087 unsigned long flags;
Michel Thierryacdd8842014-07-24 17:04:38 +01001088
Chris Wilson663f71e2016-11-14 20:41:00 +00001089 /* Will be called from irq-context when using foreign fences. */
Chris Wilsona89d1f92018-05-02 17:38:39 +01001090 spin_lock_irqsave(&engine->timeline.lock, flags);
Michel Thierryacdd8842014-07-24 17:04:38 +01001091
Chris Wilson0c7112a2018-04-18 19:40:51 +01001092 queue_request(engine, &request->sched, rq_prio(request));
Michel Thierryacdd8842014-07-24 17:04:38 +01001093
Chris Wilson655250a2018-06-29 08:53:20 +01001094 GEM_BUG_ON(RB_EMPTY_ROOT(&engine->execlists.queue.rb_root));
Chris Wilson0c7112a2018-04-18 19:40:51 +01001095 GEM_BUG_ON(list_empty(&request->sched.link));
Chris Wilson6c067572017-05-17 13:10:03 +01001096
Chris Wilson9512f982018-06-28 21:12:11 +01001097 submit_queue(engine, rq_prio(request));
1098
Chris Wilsona89d1f92018-05-02 17:38:39 +01001099 spin_unlock_irqrestore(&engine->timeline.lock, flags);
Michel Thierryacdd8842014-07-24 17:04:38 +01001100}
1101
Chris Wilson1fc44d92018-05-17 22:26:32 +01001102static void execlists_context_destroy(struct intel_context *ce)
1103{
Chris Wilson1fc44d92018-05-17 22:26:32 +01001104 GEM_BUG_ON(ce->pin_count);
1105
Chris Wilsondd12c6c2018-06-25 11:06:03 +01001106 if (!ce->state)
1107 return;
1108
Chris Wilson1fc44d92018-05-17 22:26:32 +01001109 intel_ring_free(ce->ring);
Chris Wilsonefe79d42018-06-25 11:06:04 +01001110
1111 GEM_BUG_ON(i915_gem_object_is_active(ce->state->obj));
1112 i915_gem_object_put(ce->state->obj);
Chris Wilson1fc44d92018-05-17 22:26:32 +01001113}
1114
Chris Wilson867985d2018-05-17 22:26:33 +01001115static void execlists_context_unpin(struct intel_context *ce)
Chris Wilson1fc44d92018-05-17 22:26:32 +01001116{
Chris Wilsonbc2477f2018-10-03 12:09:41 +01001117 struct intel_engine_cs *engine;
1118
1119 /*
1120 * The tasklet may still be using a pointer to our state, via an
1121 * old request. However, since we know we only unpin the context
1122 * on retirement of the following request, we know that the last
1123 * request referencing us will have had a completion CS interrupt.
1124 * If we see that it is still active, it means that the tasklet hasn't
1125 * had the chance to run yet; let it run before we teardown the
1126 * reference it may use.
1127 */
1128 engine = READ_ONCE(ce->active);
1129 if (unlikely(engine)) {
1130 unsigned long flags;
1131
1132 spin_lock_irqsave(&engine->timeline.lock, flags);
1133 process_csb(engine);
1134 spin_unlock_irqrestore(&engine->timeline.lock, flags);
1135
1136 GEM_BUG_ON(READ_ONCE(ce->active));
1137 }
1138
Chris Wilson288f1ce2018-09-04 16:31:17 +01001139 i915_gem_context_unpin_hw_id(ce->gem_context);
1140
Chris Wilson1fc44d92018-05-17 22:26:32 +01001141 intel_ring_unpin(ce->ring);
1142
1143 ce->state->obj->pin_global--;
1144 i915_gem_object_unpin_map(ce->state->obj);
1145 i915_vma_unpin(ce->state);
1146
1147 i915_gem_context_put(ce->gem_context);
1148}
1149
Chris Wilsonf4e15af2017-11-10 14:26:32 +00001150static int __context_pin(struct i915_gem_context *ctx, struct i915_vma *vma)
1151{
1152 unsigned int flags;
1153 int err;
1154
1155 /*
1156 * Clear this page out of any CPU caches for coherent swap-in/out.
1157 * We only want to do this on the first bind so that we do not stall
1158 * on an active context (which by nature is already on the GPU).
1159 */
1160 if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
Chris Wilson666424a2018-09-14 13:35:04 +01001161 err = i915_gem_object_set_to_wc_domain(vma->obj, true);
Chris Wilsonf4e15af2017-11-10 14:26:32 +00001162 if (err)
1163 return err;
1164 }
1165
1166 flags = PIN_GLOBAL | PIN_HIGH;
Jakub Bartmiński496bcce2018-07-27 16:11:46 +02001167 flags |= PIN_OFFSET_BIAS | i915_ggtt_pin_bias(vma);
Chris Wilsonf4e15af2017-11-10 14:26:32 +00001168
Chris Wilsonc00db492018-07-27 10:29:47 +01001169 return i915_vma_pin(vma, 0, 0, flags);
Chris Wilsonf4e15af2017-11-10 14:26:32 +00001170}
1171
Chris Wilson1fc44d92018-05-17 22:26:32 +01001172static struct intel_context *
1173__execlists_context_pin(struct intel_engine_cs *engine,
1174 struct i915_gem_context *ctx,
1175 struct intel_context *ce)
Oscar Mateodcb4c122014-11-13 10:28:10 +00001176{
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001177 void *vaddr;
Tvrtko Ursulinca825802016-01-15 15:10:27 +00001178 int ret;
Oscar Mateodcb4c122014-11-13 10:28:10 +00001179
Chris Wilson1fc44d92018-05-17 22:26:32 +01001180 ret = execlists_context_deferred_alloc(ctx, engine, ce);
Chris Wilson1d2a19c2018-01-26 12:18:46 +00001181 if (ret)
1182 goto err;
Chris Wilson56f6e0a2017-01-05 15:30:20 +00001183 GEM_BUG_ON(!ce->state);
Chris Wilsone8a9c582016-12-18 15:37:20 +00001184
Chris Wilsonf4e15af2017-11-10 14:26:32 +00001185 ret = __context_pin(ctx, ce->state);
Nick Hoathe84fe802015-09-11 12:53:46 +01001186 if (ret)
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001187 goto err;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001188
Chris Wilson666424a2018-09-14 13:35:04 +01001189 vaddr = i915_gem_object_pin_map(ce->state->obj,
1190 i915_coherent_map_type(ctx->i915) |
1191 I915_MAP_OVERRIDE);
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001192 if (IS_ERR(vaddr)) {
1193 ret = PTR_ERR(vaddr);
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001194 goto unpin_vma;
Tvrtko Ursulin82352e92016-01-15 17:12:45 +00001195 }
1196
Chris Wilson5503cb02018-07-27 16:55:01 +01001197 ret = intel_ring_pin(ce->ring);
Nick Hoathe84fe802015-09-11 12:53:46 +01001198 if (ret)
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001199 goto unpin_map;
Alex Daid1675192015-08-12 15:43:43 +01001200
Chris Wilson288f1ce2018-09-04 16:31:17 +01001201 ret = i915_gem_context_pin_hw_id(ctx);
1202 if (ret)
1203 goto unpin_ring;
1204
Chris Wilson1fc44d92018-05-17 22:26:32 +01001205 intel_lr_context_descriptor_update(ctx, engine, ce);
Chris Wilson9021ad02016-05-24 14:53:37 +01001206
Chris Wilsondee60ca2018-09-14 13:35:02 +01001207 GEM_BUG_ON(!intel_ring_offset_valid(ce->ring, ce->ring->head));
1208
Chris Wilsona3aabe82016-10-04 21:11:26 +01001209 ce->lrc_reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
1210 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001211 i915_ggtt_offset(ce->ring->vma);
Chris Wilsondee60ca2018-09-14 13:35:02 +01001212 ce->lrc_reg_state[CTX_RING_HEAD + 1] = ce->ring->head;
1213 ce->lrc_reg_state[CTX_RING_TAIL + 1] = ce->ring->tail;
Chris Wilsona3aabe82016-10-04 21:11:26 +01001214
Chris Wilson3d574a62017-10-13 21:26:16 +01001215 ce->state->obj->pin_global++;
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001216 i915_gem_context_get(ctx);
Chris Wilson1fc44d92018-05-17 22:26:32 +01001217 return ce;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001218
Chris Wilson288f1ce2018-09-04 16:31:17 +01001219unpin_ring:
1220 intel_ring_unpin(ce->ring);
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001221unpin_map:
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001222 i915_gem_object_unpin_map(ce->state->obj);
1223unpin_vma:
1224 __i915_vma_unpin(ce->state);
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001225err:
Chris Wilson9021ad02016-05-24 14:53:37 +01001226 ce->pin_count = 0;
Chris Wilson266a2402017-05-04 10:33:08 +01001227 return ERR_PTR(ret);
Oscar Mateodcb4c122014-11-13 10:28:10 +00001228}
1229
Chris Wilson1fc44d92018-05-17 22:26:32 +01001230static const struct intel_context_ops execlists_context_ops = {
1231 .unpin = execlists_context_unpin,
1232 .destroy = execlists_context_destroy,
1233};
1234
1235static struct intel_context *
1236execlists_context_pin(struct intel_engine_cs *engine,
1237 struct i915_gem_context *ctx)
Oscar Mateodcb4c122014-11-13 10:28:10 +00001238{
Chris Wilsonab82a062018-04-30 14:15:01 +01001239 struct intel_context *ce = to_intel_context(ctx, engine);
Daniel Vetteraf3302b2015-12-04 17:27:15 +01001240
Chris Wilson91c8a322016-07-05 10:40:23 +01001241 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilson4bdafb92018-09-26 21:12:22 +01001242 GEM_BUG_ON(!ctx->ppgtt);
Tvrtko Ursulin321fe302016-01-28 10:29:55 +00001243
Chris Wilson1fc44d92018-05-17 22:26:32 +01001244 if (likely(ce->pin_count++))
1245 return ce;
1246 GEM_BUG_ON(!ce->pin_count); /* no overflow please! */
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001247
Chris Wilson1fc44d92018-05-17 22:26:32 +01001248 ce->ops = &execlists_context_ops;
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001249
Chris Wilson1fc44d92018-05-17 22:26:32 +01001250 return __execlists_context_pin(engine, ctx, ce);
Oscar Mateodcb4c122014-11-13 10:28:10 +00001251}
1252
Chris Wilsone8894262018-12-07 09:02:13 +00001253static int emit_pdps(struct i915_request *rq)
1254{
1255 const struct intel_engine_cs * const engine = rq->engine;
1256 struct i915_hw_ppgtt * const ppgtt = rq->gem_context->ppgtt;
1257 int err, i;
1258 u32 *cs;
1259
1260 GEM_BUG_ON(intel_vgpu_active(rq->i915));
1261
1262 /*
1263 * Beware ye of the dragons, this sequence is magic!
1264 *
1265 * Small changes to this sequence can cause anything from
1266 * GPU hangs to forcewake errors and machine lockups!
1267 */
1268
1269 /* Flush any residual operations from the context load */
1270 err = engine->emit_flush(rq, EMIT_FLUSH);
1271 if (err)
1272 return err;
1273
1274 /* Magic required to prevent forcewake errors! */
1275 err = engine->emit_flush(rq, EMIT_INVALIDATE);
1276 if (err)
1277 return err;
1278
1279 cs = intel_ring_begin(rq, 4 * GEN8_3LVL_PDPES + 2);
1280 if (IS_ERR(cs))
1281 return PTR_ERR(cs);
1282
1283 /* Ensure the LRI have landed before we invalidate & continue */
1284 *cs++ = MI_LOAD_REGISTER_IMM(2 * GEN8_3LVL_PDPES) | MI_LRI_FORCE_POSTED;
1285 for (i = GEN8_3LVL_PDPES; i--; ) {
1286 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
1287
1288 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, i));
1289 *cs++ = upper_32_bits(pd_daddr);
1290 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, i));
1291 *cs++ = lower_32_bits(pd_daddr);
1292 }
1293 *cs++ = MI_NOOP;
1294
1295 intel_ring_advance(rq, cs);
1296
1297 /* Be doubly sure the LRI have landed before proceeding */
1298 err = engine->emit_flush(rq, EMIT_FLUSH);
1299 if (err)
1300 return err;
1301
1302 /* Re-invalidate the TLB for luck */
1303 return engine->emit_flush(rq, EMIT_INVALIDATE);
1304}
1305
Chris Wilsone61e0f52018-02-21 09:56:36 +00001306static int execlists_request_alloc(struct i915_request *request)
Chris Wilsonef11c012016-12-18 15:37:19 +00001307{
Chris Wilsonfd138212017-11-15 15:12:04 +00001308 int ret;
Chris Wilsonef11c012016-12-18 15:37:19 +00001309
Chris Wilson1fc44d92018-05-17 22:26:32 +01001310 GEM_BUG_ON(!request->hw_context->pin_count);
Chris Wilsone8a9c582016-12-18 15:37:20 +00001311
Chris Wilson5f5800a2018-12-07 09:02:11 +00001312 /*
1313 * Flush enough space to reduce the likelihood of waiting after
Chris Wilsonef11c012016-12-18 15:37:19 +00001314 * we start building the request - in which case we will just
1315 * have to repeat work.
1316 */
1317 request->reserved_space += EXECLISTS_REQUEST_SIZE;
1318
Chris Wilson5f5800a2018-12-07 09:02:11 +00001319 /*
1320 * Note that after this point, we have committed to using
Chris Wilsonef11c012016-12-18 15:37:19 +00001321 * this request as it is being used to both track the
1322 * state of engine initialisation and liveness of the
1323 * golden renderstate above. Think twice before you try
1324 * to cancel/unwind this request now.
1325 */
1326
Chris Wilsone8894262018-12-07 09:02:13 +00001327 /* Unconditionally invalidate GPU caches and TLBs. */
1328 if (i915_vm_is_48bit(&request->gem_context->ppgtt->vm))
1329 ret = request->engine->emit_flush(request, EMIT_INVALIDATE);
1330 else
1331 ret = emit_pdps(request);
1332 if (ret)
1333 return ret;
1334
Chris Wilsonef11c012016-12-18 15:37:19 +00001335 request->reserved_space -= EXECLISTS_REQUEST_SIZE;
1336 return 0;
Chris Wilsonef11c012016-12-18 15:37:19 +00001337}
1338
Arun Siluvery9e000842015-07-03 14:27:31 +01001339/*
1340 * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
1341 * PIPE_CONTROL instruction. This is required for the flush to happen correctly
1342 * but there is a slight complication as this is applied in WA batch where the
1343 * values are only initialized once so we cannot take register value at the
1344 * beginning and reuse it further; hence we save its value to memory, upload a
1345 * constant value with bit21 set and then we restore it back with the saved value.
1346 * To simplify the WA, a constant value is formed by using the default value
1347 * of this register. This shouldn't be a problem because we are only modifying
1348 * it for a short period and this batch in non-premptible. We can ofcourse
1349 * use additional instructions that read the actual value of the register
1350 * at that time and set our bit of interest but it makes the WA complicated.
1351 *
1352 * This WA is also required for Gen9 so extracting as a function avoids
1353 * code duplication.
1354 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001355static u32 *
1356gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery9e000842015-07-03 14:27:31 +01001357{
Chris Wilson51797492018-12-04 14:15:16 +00001358 /* NB no one else is allowed to scribble over scratch + 256! */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001359 *batch++ = MI_STORE_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1360 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
Chris Wilson51797492018-12-04 14:15:16 +00001361 *batch++ = i915_scratch_offset(engine->i915) + 256;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001362 *batch++ = 0;
Arun Siluvery9e000842015-07-03 14:27:31 +01001363
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001364 *batch++ = MI_LOAD_REGISTER_IMM(1);
1365 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1366 *batch++ = 0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES;
Arun Siluvery9e000842015-07-03 14:27:31 +01001367
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001368 batch = gen8_emit_pipe_control(batch,
1369 PIPE_CONTROL_CS_STALL |
1370 PIPE_CONTROL_DC_FLUSH_ENABLE,
1371 0);
Arun Siluvery9e000842015-07-03 14:27:31 +01001372
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001373 *batch++ = MI_LOAD_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1374 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
Chris Wilson51797492018-12-04 14:15:16 +00001375 *batch++ = i915_scratch_offset(engine->i915) + 256;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001376 *batch++ = 0;
Arun Siluvery9e000842015-07-03 14:27:31 +01001377
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001378 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001379}
1380
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001381/*
1382 * Typically we only have one indirect_ctx and per_ctx batch buffer which are
1383 * initialized at the beginning and shared across all contexts but this field
1384 * helps us to have multiple batches at different offsets and select them based
1385 * on a criteria. At the moment this batch always start at the beginning of the page
1386 * and at this point we don't have multiple wa_ctx batch buffers.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001387 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001388 * The number of WA applied are not known at the beginning; we use this field
1389 * to return the no of DWORDS written.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001390 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001391 * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
1392 * so it adds NOOPs as padding to make it cacheline aligned.
1393 * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
1394 * makes a complete batch buffer.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001395 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001396static u32 *gen8_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001397{
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001398 /* WaDisableCtxRestoreArbitration:bdw,chv */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001399 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001400
Arun Siluveryc82435b2015-06-19 18:37:13 +01001401 /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001402 if (IS_BROADWELL(engine->i915))
1403 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
Arun Siluveryc82435b2015-06-19 18:37:13 +01001404
Arun Siluvery0160f052015-06-23 15:46:57 +01001405 /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
1406 /* Actual scratch location is at 128 bytes offset */
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001407 batch = gen8_emit_pipe_control(batch,
1408 PIPE_CONTROL_FLUSH_L3 |
1409 PIPE_CONTROL_GLOBAL_GTT_IVB |
1410 PIPE_CONTROL_CS_STALL |
1411 PIPE_CONTROL_QW_WRITE,
Chris Wilson51797492018-12-04 14:15:16 +00001412 i915_scratch_offset(engine->i915) +
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001413 2 * CACHELINE_BYTES);
Arun Siluvery0160f052015-06-23 15:46:57 +01001414
Chris Wilsonbeecec92017-10-03 21:34:52 +01001415 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1416
Arun Siluvery17ee9502015-06-19 19:07:01 +01001417 /* Pad to end of cacheline */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001418 while ((unsigned long)batch % CACHELINE_BYTES)
1419 *batch++ = MI_NOOP;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001420
1421 /*
1422 * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
1423 * execution depends on the length specified in terms of cache lines
1424 * in the register CTX_RCS_INDIRECT_CTX
1425 */
1426
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001427 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001428}
1429
Chris Wilson5ee4a7a2018-06-18 10:41:50 +01001430struct lri {
1431 i915_reg_t reg;
1432 u32 value;
1433};
1434
1435static u32 *emit_lri(u32 *batch, const struct lri *lri, unsigned int count)
1436{
1437 GEM_BUG_ON(!count || count > 63);
1438
1439 *batch++ = MI_LOAD_REGISTER_IMM(count);
1440 do {
1441 *batch++ = i915_mmio_reg_offset(lri->reg);
1442 *batch++ = lri->value;
1443 } while (lri++, --count);
1444 *batch++ = MI_NOOP;
1445
1446 return batch;
1447}
1448
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001449static u32 *gen9_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery0504cff2015-07-14 15:01:27 +01001450{
Chris Wilson5ee4a7a2018-06-18 10:41:50 +01001451 static const struct lri lri[] = {
1452 /* WaDisableGatherAtSetShaderCommonSlice:skl,bxt,kbl,glk */
1453 {
1454 COMMON_SLICE_CHICKEN2,
1455 __MASKED_FIELD(GEN9_DISABLE_GATHER_AT_SET_SHADER_COMMON_SLICE,
1456 0),
1457 },
1458
1459 /* BSpec: 11391 */
1460 {
1461 FF_SLICE_CHICKEN,
1462 __MASKED_FIELD(FF_SLICE_CHICKEN_CL_PROVOKING_VERTEX_FIX,
1463 FF_SLICE_CHICKEN_CL_PROVOKING_VERTEX_FIX),
1464 },
1465
1466 /* BSpec: 11299 */
1467 {
1468 _3D_CHICKEN3,
1469 __MASKED_FIELD(_3D_CHICKEN_SF_PROVOKING_VERTEX_FIX,
1470 _3D_CHICKEN_SF_PROVOKING_VERTEX_FIX),
1471 }
1472 };
1473
Chris Wilsonbeecec92017-10-03 21:34:52 +01001474 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
1475
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001476 /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt,glk */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001477 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
Arun Siluverya4106a72015-07-14 15:01:29 +01001478
Chris Wilson5ee4a7a2018-06-18 10:41:50 +01001479 batch = emit_lri(batch, lri, ARRAY_SIZE(lri));
Mika Kuoppala873e8172016-07-20 14:26:13 +03001480
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001481 /* WaMediaPoolStateCmdInWABB:bxt,glk */
Tim Gore3485d992016-07-05 10:01:30 +01001482 if (HAS_POOLED_EU(engine->i915)) {
1483 /*
1484 * EU pool configuration is setup along with golden context
1485 * during context initialization. This value depends on
1486 * device type (2x6 or 3x6) and needs to be updated based
1487 * on which subslice is disabled especially for 2x6
1488 * devices, however it is safe to load default
1489 * configuration of 3x6 device instead of masking off
1490 * corresponding bits because HW ignores bits of a disabled
1491 * subslice and drops down to appropriate config. Please
1492 * see render_state_setup() in i915_gem_render_state.c for
1493 * possible configurations, to avoid duplication they are
1494 * not shown here again.
1495 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001496 *batch++ = GEN9_MEDIA_POOL_STATE;
1497 *batch++ = GEN9_MEDIA_POOL_ENABLE;
1498 *batch++ = 0x00777000;
1499 *batch++ = 0;
1500 *batch++ = 0;
1501 *batch++ = 0;
Tim Gore3485d992016-07-05 10:01:30 +01001502 }
1503
Chris Wilsonbeecec92017-10-03 21:34:52 +01001504 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1505
Arun Siluvery0504cff2015-07-14 15:01:27 +01001506 /* Pad to end of cacheline */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001507 while ((unsigned long)batch % CACHELINE_BYTES)
1508 *batch++ = MI_NOOP;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001509
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001510 return batch;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001511}
1512
Rafael Antognolli4b6ce682018-02-05 15:33:30 -08001513static u32 *
1514gen10_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
1515{
1516 int i;
1517
1518 /*
1519 * WaPipeControlBefore3DStateSamplePattern: cnl
1520 *
1521 * Ensure the engine is idle prior to programming a
1522 * 3DSTATE_SAMPLE_PATTERN during a context restore.
1523 */
1524 batch = gen8_emit_pipe_control(batch,
1525 PIPE_CONTROL_CS_STALL,
1526 0);
1527 /*
1528 * WaPipeControlBefore3DStateSamplePattern says we need 4 dwords for
1529 * the PIPE_CONTROL followed by 12 dwords of 0x0, so 16 dwords in
1530 * total. However, a PIPE_CONTROL is 6 dwords long, not 4, which is
1531 * confusing. Since gen8_emit_pipe_control() already advances the
1532 * batch by 6 dwords, we advance the other 10 here, completing a
1533 * cacheline. It's not clear if the workaround requires this padding
1534 * before other commands, or if it's just the regular padding we would
1535 * already have for the workaround bb, so leave it here for now.
1536 */
1537 for (i = 0; i < 10; i++)
1538 *batch++ = MI_NOOP;
1539
1540 /* Pad to end of cacheline */
1541 while ((unsigned long)batch % CACHELINE_BYTES)
1542 *batch++ = MI_NOOP;
1543
1544 return batch;
1545}
1546
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001547#define CTX_WA_BB_OBJ_SIZE (PAGE_SIZE)
1548
1549static int lrc_setup_wa_ctx(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001550{
Chris Wilson48bb74e2016-08-15 10:49:04 +01001551 struct drm_i915_gem_object *obj;
1552 struct i915_vma *vma;
1553 int err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001554
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001555 obj = i915_gem_object_create(engine->i915, CTX_WA_BB_OBJ_SIZE);
Chris Wilson48bb74e2016-08-15 10:49:04 +01001556 if (IS_ERR(obj))
1557 return PTR_ERR(obj);
1558
Chris Wilson82ad6442018-06-05 16:37:58 +01001559 vma = i915_vma_instance(obj, &engine->i915->ggtt.vm, NULL);
Chris Wilson48bb74e2016-08-15 10:49:04 +01001560 if (IS_ERR(vma)) {
1561 err = PTR_ERR(vma);
1562 goto err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001563 }
1564
Chris Wilson7a859c62018-07-27 10:18:55 +01001565 err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
Chris Wilson48bb74e2016-08-15 10:49:04 +01001566 if (err)
1567 goto err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001568
Chris Wilson48bb74e2016-08-15 10:49:04 +01001569 engine->wa_ctx.vma = vma;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001570 return 0;
Chris Wilson48bb74e2016-08-15 10:49:04 +01001571
1572err:
1573 i915_gem_object_put(obj);
1574 return err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001575}
1576
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001577static void lrc_destroy_wa_ctx(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001578{
Chris Wilson6a2f59e2018-07-21 13:50:37 +01001579 i915_vma_unpin_and_release(&engine->wa_ctx.vma, 0);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001580}
1581
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001582typedef u32 *(*wa_bb_func_t)(struct intel_engine_cs *engine, u32 *batch);
1583
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001584static int intel_init_workaround_bb(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001585{
Chris Wilson48bb74e2016-08-15 10:49:04 +01001586 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001587 struct i915_wa_ctx_bb *wa_bb[2] = { &wa_ctx->indirect_ctx,
1588 &wa_ctx->per_ctx };
1589 wa_bb_func_t wa_bb_fn[2];
Arun Siluvery17ee9502015-06-19 19:07:01 +01001590 struct page *page;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001591 void *batch, *batch_ptr;
1592 unsigned int i;
Chris Wilson48bb74e2016-08-15 10:49:04 +01001593 int ret;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001594
Tvrtko Ursulinbbb8a9d2018-10-12 07:31:42 +01001595 if (GEM_DEBUG_WARN_ON(engine->id != RCS))
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001596 return -EINVAL;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001597
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001598 switch (INTEL_GEN(engine->i915)) {
Oscar Mateocc38cae2018-05-08 14:29:23 -07001599 case 11:
1600 return 0;
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001601 case 10:
Rafael Antognolli4b6ce682018-02-05 15:33:30 -08001602 wa_bb_fn[0] = gen10_init_indirectctx_bb;
1603 wa_bb_fn[1] = NULL;
1604 break;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001605 case 9:
1606 wa_bb_fn[0] = gen9_init_indirectctx_bb;
Chris Wilsonb8aa2232017-09-21 14:54:44 +01001607 wa_bb_fn[1] = NULL;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001608 break;
1609 case 8:
1610 wa_bb_fn[0] = gen8_init_indirectctx_bb;
Chris Wilson3ad7b522017-10-03 21:34:49 +01001611 wa_bb_fn[1] = NULL;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001612 break;
1613 default:
1614 MISSING_CASE(INTEL_GEN(engine->i915));
Arun Siluvery5e60d792015-06-23 15:50:44 +01001615 return 0;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001616 }
Arun Siluvery5e60d792015-06-23 15:50:44 +01001617
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001618 ret = lrc_setup_wa_ctx(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001619 if (ret) {
1620 DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
1621 return ret;
1622 }
1623
Chris Wilson48bb74e2016-08-15 10:49:04 +01001624 page = i915_gem_object_get_dirty_page(wa_ctx->vma->obj, 0);
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001625 batch = batch_ptr = kmap_atomic(page);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001626
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001627 /*
1628 * Emit the two workaround batch buffers, recording the offset from the
1629 * start of the workaround batch buffer object for each and their
1630 * respective sizes.
1631 */
1632 for (i = 0; i < ARRAY_SIZE(wa_bb_fn); i++) {
1633 wa_bb[i]->offset = batch_ptr - batch;
Tvrtko Ursulinbbb8a9d2018-10-12 07:31:42 +01001634 if (GEM_DEBUG_WARN_ON(!IS_ALIGNED(wa_bb[i]->offset,
1635 CACHELINE_BYTES))) {
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001636 ret = -EINVAL;
1637 break;
1638 }
Chris Wilson604a8f62017-09-21 14:54:43 +01001639 if (wa_bb_fn[i])
1640 batch_ptr = wa_bb_fn[i](engine, batch_ptr);
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001641 wa_bb[i]->size = batch_ptr - (batch + wa_bb[i]->offset);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001642 }
1643
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001644 BUG_ON(batch_ptr - batch > CTX_WA_BB_OBJ_SIZE);
1645
Arun Siluvery17ee9502015-06-19 19:07:01 +01001646 kunmap_atomic(batch);
1647 if (ret)
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001648 lrc_destroy_wa_ctx(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001649
1650 return ret;
1651}
1652
Chris Wilsonf3c9d4072018-01-02 15:12:34 +00001653static void enable_execlists(struct intel_engine_cs *engine)
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001654{
Chris Wilsonc0336662016-05-06 15:40:21 +01001655 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonf3c9d4072018-01-02 15:12:34 +00001656
Chris Wilson060f2322018-12-18 10:27:12 +00001657 intel_engine_set_hwsp_writemask(engine, ~0u); /* HWSTAM */
Kelvin Gardiner225701f2018-01-30 11:49:17 -02001658
1659 /*
1660 * Make sure we're not enabling the new 12-deep CSB
1661 * FIFO as that requires a slightly updated handling
1662 * in the ctx switch irq. Since we're currently only
1663 * using only 2 elements of the enhanced execlists the
1664 * deeper FIFO it's not needed and it's not worth adding
1665 * more statements to the irq handler to support it.
1666 */
1667 if (INTEL_GEN(dev_priv) >= 11)
1668 I915_WRITE(RING_MODE_GEN7(engine),
1669 _MASKED_BIT_DISABLE(GEN11_GFX_DISABLE_LEGACY_MODE));
1670 else
1671 I915_WRITE(RING_MODE_GEN7(engine),
1672 _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
1673
Chris Wilson9a4dc802018-05-18 11:09:33 +01001674 I915_WRITE(RING_MI_MODE(engine->mmio_base),
1675 _MASKED_BIT_DISABLE(STOP_RING));
1676
Chris Wilsonf3c9d4072018-01-02 15:12:34 +00001677 I915_WRITE(RING_HWS_PGA(engine->mmio_base),
1678 engine->status_page.ggtt_offset);
1679 POSTING_READ(RING_HWS_PGA(engine->mmio_base));
1680}
1681
Chris Wilson9a4dc802018-05-18 11:09:33 +01001682static bool unexpected_starting_state(struct intel_engine_cs *engine)
1683{
1684 struct drm_i915_private *dev_priv = engine->i915;
1685 bool unexpected = false;
1686
1687 if (I915_READ(RING_MI_MODE(engine->mmio_base)) & STOP_RING) {
1688 DRM_DEBUG_DRIVER("STOP_RING still set in RING_MI_MODE\n");
1689 unexpected = true;
1690 }
1691
1692 return unexpected;
1693}
1694
Chris Wilsonf3c9d4072018-01-02 15:12:34 +00001695static int gen8_init_common_ring(struct intel_engine_cs *engine)
1696{
Tvrtko Ursulin4a15c75c2018-12-03 13:33:41 +00001697 intel_engine_apply_workarounds(engine);
Chris Wilson5a688ee2018-12-06 18:07:13 +00001698 intel_engine_apply_whitelist(engine);
Tvrtko Ursulin4a15c75c2018-12-03 13:33:41 +00001699
Chris Wilson805615d2018-08-15 19:42:51 +01001700 intel_mocs_init_engine(engine);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001701
Chris Wilsonad07dfc2016-10-07 07:53:26 +01001702 intel_engine_reset_breadcrumbs(engine);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001703
Chris Wilson9a4dc802018-05-18 11:09:33 +01001704 if (GEM_SHOW_DEBUG() && unexpected_starting_state(engine)) {
1705 struct drm_printer p = drm_debug_printer(__func__);
1706
1707 intel_engine_dump(engine, &p, NULL);
1708 }
1709
Chris Wilsonf3c9d4072018-01-02 15:12:34 +00001710 enable_execlists(engine);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001711
Chris Wilson821ed7d2016-09-09 14:11:53 +01001712 return 0;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001713}
1714
Chris Wilson5adfb772018-05-16 19:33:51 +01001715static struct i915_request *
1716execlists_reset_prepare(struct intel_engine_cs *engine)
1717{
1718 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilson63572932018-05-16 19:33:54 +01001719 struct i915_request *request, *active;
Chris Wilson9512f982018-06-28 21:12:11 +01001720 unsigned long flags;
Chris Wilson5adfb772018-05-16 19:33:51 +01001721
Chris Wilson66fc8292018-08-15 14:58:27 +01001722 GEM_TRACE("%s: depth<-%d\n", engine->name,
1723 atomic_read(&execlists->tasklet.count));
Chris Wilson5adfb772018-05-16 19:33:51 +01001724
1725 /*
1726 * Prevent request submission to the hardware until we have
1727 * completed the reset in i915_gem_reset_finish(). If a request
1728 * is completed by one engine, it may then queue a request
1729 * to a second via its execlists->tasklet *just* as we are
1730 * calling engine->init_hw() and also writing the ELSP.
1731 * Turning off the execlists->tasklet until the reset is over
1732 * prevents the race.
1733 */
1734 __tasklet_disable_sync_once(&execlists->tasklet);
1735
Chris Wilson9512f982018-06-28 21:12:11 +01001736 spin_lock_irqsave(&engine->timeline.lock, flags);
1737
Chris Wilson63572932018-05-16 19:33:54 +01001738 /*
1739 * We want to flush the pending context switches, having disabled
1740 * the tasklet above, we can assume exclusive access to the execlists.
1741 * For this allows us to catch up with an inflight preemption event,
1742 * and avoid blaming an innocent request if the stall was due to the
1743 * preemption itself.
1744 */
Chris Wilsonfd8526e2018-06-28 21:12:10 +01001745 process_csb(engine);
Chris Wilson63572932018-05-16 19:33:54 +01001746
1747 /*
1748 * The last active request can then be no later than the last request
1749 * now in ELSP[0]. So search backwards from there, so that if the GPU
1750 * has advanced beyond the last CSB update, it will be pardoned.
1751 */
1752 active = NULL;
1753 request = port_request(execlists->port);
1754 if (request) {
Chris Wilson3f6e9822018-05-16 19:33:55 +01001755 /*
1756 * Prevent the breadcrumb from advancing before we decide
1757 * which request is currently active.
1758 */
1759 intel_engine_stop_cs(engine);
1760
Chris Wilson63572932018-05-16 19:33:54 +01001761 list_for_each_entry_from_reverse(request,
1762 &engine->timeline.requests,
1763 link) {
1764 if (__i915_request_completed(request,
1765 request->global_seqno))
1766 break;
1767
1768 active = request;
1769 }
Chris Wilson63572932018-05-16 19:33:54 +01001770 }
1771
Chris Wilson9512f982018-06-28 21:12:11 +01001772 spin_unlock_irqrestore(&engine->timeline.lock, flags);
1773
Chris Wilson63572932018-05-16 19:33:54 +01001774 return active;
Chris Wilson5adfb772018-05-16 19:33:51 +01001775}
1776
1777static void execlists_reset(struct intel_engine_cs *engine,
1778 struct i915_request *request)
Chris Wilson821ed7d2016-09-09 14:11:53 +01001779{
Mika Kuoppalab620e872017-09-22 15:43:03 +03001780 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilson221ab97192017-09-16 21:44:14 +01001781 unsigned long flags;
Chris Wilson56922512018-04-28 12:15:32 +01001782 u32 *regs;
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001783
Tvrtko Ursulinc5f6d572018-09-26 15:50:33 +01001784 GEM_TRACE("%s request global=%d, current=%d\n",
Tvrtko Ursulin0c5c7df2018-04-06 13:35:14 +01001785 engine->name, request ? request->global_seqno : 0,
1786 intel_engine_get_seqno(engine));
Chris Wilson42232212018-01-02 15:12:32 +00001787
Chris Wilsond8857d52018-06-28 21:12:05 +01001788 spin_lock_irqsave(&engine->timeline.lock, flags);
Chris Wilson221ab97192017-09-16 21:44:14 +01001789
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001790 /*
1791 * Catch up with any missed context-switch interrupts.
1792 *
1793 * Ideally we would just read the remaining CSB entries now that we
1794 * know the gpu is idle. However, the CSB registers are sometimes^W
1795 * often trashed across a GPU reset! Instead we have to rely on
1796 * guessing the missed context-switch events by looking at what
1797 * requests were completed.
1798 */
Michał Winiarskia4598d12017-10-25 22:00:18 +02001799 execlists_cancel_port_requests(execlists);
Chris Wilson221ab97192017-09-16 21:44:14 +01001800
1801 /* Push back any incomplete requests for replay after the reset. */
Michał Winiarskia4598d12017-10-25 22:00:18 +02001802 __unwind_incomplete_requests(engine);
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001803
Chris Wilsonc3160da2018-05-31 09:22:45 +01001804 /* Following the reset, we need to reload the CSB read/write pointers */
Chris Wilsonf4b58f02018-06-28 21:12:08 +01001805 reset_csb_pointers(&engine->execlists);
Chris Wilsonc3160da2018-05-31 09:22:45 +01001806
Chris Wilsond8857d52018-06-28 21:12:05 +01001807 spin_unlock_irqrestore(&engine->timeline.lock, flags);
Chris Wilsonaebbc2d2018-03-02 13:12:46 +00001808
Chris Wilsona3e38832018-03-02 14:32:45 +00001809 /*
1810 * If the request was innocent, we leave the request in the ELSP
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001811 * and will try to replay it on restarting. The context image may
1812 * have been corrupted by the reset, in which case we may have
1813 * to service a new GPU hang, but more likely we can continue on
1814 * without impact.
1815 *
1816 * If the request was guilty, we presume the context is corrupt
1817 * and have to at least restore the RING register in the context
1818 * image back to the expected values to skip over the guilty request.
1819 */
Chris Wilson221ab97192017-09-16 21:44:14 +01001820 if (!request || request->fence.error != -EIO)
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001821 return;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001822
Chris Wilsona3e38832018-03-02 14:32:45 +00001823 /*
1824 * We want a simple context + ring to execute the breadcrumb update.
Chris Wilsona3aabe82016-10-04 21:11:26 +01001825 * We cannot rely on the context being intact across the GPU hang,
1826 * so clear it and rebuild just what we need for the breadcrumb.
1827 * All pending requests for this context will be zapped, and any
1828 * future request will be after userspace has had the opportunity
1829 * to recreate its own state.
1830 */
Chris Wilson1fc44d92018-05-17 22:26:32 +01001831 regs = request->hw_context->lrc_reg_state;
Chris Wilsonfe0c4932018-05-18 10:02:11 +01001832 if (engine->pinned_default_state) {
1833 memcpy(regs, /* skip restoring the vanilla PPHWSP */
1834 engine->pinned_default_state + LRC_STATE_PN * PAGE_SIZE,
1835 engine->context_size - PAGE_SIZE);
Chris Wilson56922512018-04-28 12:15:32 +01001836 }
Chris Wilson4e0d64d2018-05-17 22:26:30 +01001837 execlists_init_reg_state(regs,
1838 request->gem_context, engine, request->ring);
Chris Wilsona3aabe82016-10-04 21:11:26 +01001839
Chris Wilson821ed7d2016-09-09 14:11:53 +01001840 /* Move the RING_HEAD onto the breadcrumb, past the hanging batch */
Chris Wilson56922512018-04-28 12:15:32 +01001841 regs[CTX_RING_BUFFER_START + 1] = i915_ggtt_offset(request->ring->vma);
Chris Wilsona3aabe82016-10-04 21:11:26 +01001842
Chris Wilson41d37682018-06-11 12:08:45 +01001843 request->ring->head = intel_ring_wrap(request->ring, request->postfix);
1844 regs[CTX_RING_HEAD + 1] = request->ring->head;
1845
Chris Wilson821ed7d2016-09-09 14:11:53 +01001846 intel_ring_update_space(request->ring);
1847
Chris Wilsona3aabe82016-10-04 21:11:26 +01001848 /* Reset WaIdleLiteRestore:bdw,skl as well */
Chris Wilson7e4992a2017-09-28 20:38:59 +01001849 unwind_wa_tail(request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001850}
1851
Chris Wilson5adfb772018-05-16 19:33:51 +01001852static void execlists_reset_finish(struct intel_engine_cs *engine)
1853{
Chris Wilson5db1d4e2018-06-04 08:34:40 +01001854 struct intel_engine_execlists * const execlists = &engine->execlists;
1855
Chris Wilsonfe25f302018-05-22 11:19:37 +01001856 /*
Chris Wilson9e4fa012018-08-28 16:27:02 +01001857 * After a GPU reset, we may have requests to replay. Do so now while
1858 * we still have the forcewake to be sure that the GPU is not allowed
1859 * to sleep before we restart and reload a context.
Chris Wilsonfe25f302018-05-22 11:19:37 +01001860 *
Chris Wilsonfe25f302018-05-22 11:19:37 +01001861 */
Chris Wilson9e4fa012018-08-28 16:27:02 +01001862 if (!RB_EMPTY_ROOT(&execlists->queue.rb_root))
1863 execlists->tasklet.func(execlists->tasklet.data);
Chris Wilson5adfb772018-05-16 19:33:51 +01001864
Chris Wilson9e4fa012018-08-28 16:27:02 +01001865 tasklet_enable(&execlists->tasklet);
Chris Wilson66fc8292018-08-15 14:58:27 +01001866 GEM_TRACE("%s: depth->%d\n", engine->name,
1867 atomic_read(&execlists->tasklet.count));
Chris Wilson5adfb772018-05-16 19:33:51 +01001868}
1869
Chris Wilsone61e0f52018-02-21 09:56:36 +00001870static int gen8_emit_bb_start(struct i915_request *rq,
Chris Wilson803688b2016-08-02 22:50:27 +01001871 u64 offset, u32 len,
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001872 const unsigned int flags)
Oscar Mateo15648582014-07-24 17:04:32 +01001873{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001874 u32 *cs;
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001875
Chris Wilson74f9474122018-05-03 20:54:16 +01001876 cs = intel_ring_begin(rq, 6);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001877 if (IS_ERR(cs))
1878 return PTR_ERR(cs);
Oscar Mateo15648582014-07-24 17:04:32 +01001879
Chris Wilson279f5a02017-10-05 20:10:05 +01001880 /*
1881 * WaDisableCtxRestoreArbitration:bdw,chv
1882 *
1883 * We don't need to perform MI_ARB_ENABLE as often as we do (in
1884 * particular all the gen that do not need the w/a at all!), if we
1885 * took care to make sure that on every switch into this context
1886 * (both ordinary and for preemption) that arbitrartion was enabled
1887 * we would be fine. However, there doesn't seem to be a downside to
1888 * being paranoid and making sure it is set before each batch and
1889 * every context-switch.
1890 *
1891 * Note that if we fail to enable arbitration before the request
1892 * is complete, then we do not see the context-switch interrupt and
1893 * the engine hangs (with RING_HEAD == RING_TAIL).
1894 *
1895 * That satisfies both the GPGPU w/a and our heavy-handed paranoia.
1896 */
Chris Wilson3ad7b522017-10-03 21:34:49 +01001897 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1898
Oscar Mateo15648582014-07-24 17:04:32 +01001899 /* FIXME(BDW): Address space and security selectors. */
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001900 *cs++ = MI_BATCH_BUFFER_START_GEN8 |
Lucas De Marchi08e3e212018-08-03 16:24:43 -07001901 (flags & I915_DISPATCH_SECURE ? 0 : BIT(8));
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001902 *cs++ = lower_32_bits(offset);
1903 *cs++ = upper_32_bits(offset);
Chris Wilson74f9474122018-05-03 20:54:16 +01001904
1905 *cs++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
1906 *cs++ = MI_NOOP;
Chris Wilsone8894262018-12-07 09:02:13 +00001907
Chris Wilsone61e0f52018-02-21 09:56:36 +00001908 intel_ring_advance(rq, cs);
Oscar Mateo15648582014-07-24 17:04:32 +01001909
1910 return 0;
1911}
1912
Chris Wilson31bb59c2016-07-01 17:23:27 +01001913static void gen8_logical_ring_enable_irq(struct intel_engine_cs *engine)
Oscar Mateo73d477f2014-07-24 17:04:31 +01001914{
Chris Wilsonc0336662016-05-06 15:40:21 +01001915 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson31bb59c2016-07-01 17:23:27 +01001916 I915_WRITE_IMR(engine,
1917 ~(engine->irq_enable_mask | engine->irq_keep_mask));
1918 POSTING_READ_FW(RING_IMR(engine->mmio_base));
Oscar Mateo73d477f2014-07-24 17:04:31 +01001919}
1920
Chris Wilson31bb59c2016-07-01 17:23:27 +01001921static void gen8_logical_ring_disable_irq(struct intel_engine_cs *engine)
Oscar Mateo73d477f2014-07-24 17:04:31 +01001922{
Chris Wilsonc0336662016-05-06 15:40:21 +01001923 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson31bb59c2016-07-01 17:23:27 +01001924 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Oscar Mateo73d477f2014-07-24 17:04:31 +01001925}
1926
Chris Wilsone61e0f52018-02-21 09:56:36 +00001927static int gen8_emit_flush(struct i915_request *request, u32 mode)
Oscar Mateo47122742014-07-24 17:04:28 +01001928{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001929 u32 cmd, *cs;
Oscar Mateo47122742014-07-24 17:04:28 +01001930
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001931 cs = intel_ring_begin(request, 4);
1932 if (IS_ERR(cs))
1933 return PTR_ERR(cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001934
1935 cmd = MI_FLUSH_DW + 1;
1936
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001937 /* We always require a command barrier so that subsequent
1938 * commands, such as breadcrumb interrupts, are strictly ordered
1939 * wrt the contents of the write cache being flushed to memory
1940 * (and thus being coherent from the CPU).
1941 */
1942 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1943
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001944 if (mode & EMIT_INVALIDATE) {
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001945 cmd |= MI_INVALIDATE_TLB;
Chris Wilson5fc28052018-11-08 14:00:39 +00001946 if (request->engine->class == VIDEO_DECODE_CLASS)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001947 cmd |= MI_INVALIDATE_BSD;
Oscar Mateo47122742014-07-24 17:04:28 +01001948 }
1949
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001950 *cs++ = cmd;
1951 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
1952 *cs++ = 0; /* upper addr */
1953 *cs++ = 0; /* value */
1954 intel_ring_advance(request, cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001955
1956 return 0;
1957}
1958
Chris Wilsone61e0f52018-02-21 09:56:36 +00001959static int gen8_emit_flush_render(struct i915_request *request,
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001960 u32 mode)
Oscar Mateo47122742014-07-24 17:04:28 +01001961{
Chris Wilsonb5321f32016-08-02 22:50:18 +01001962 struct intel_engine_cs *engine = request->engine;
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001963 u32 scratch_addr =
Chris Wilson51797492018-12-04 14:15:16 +00001964 i915_scratch_offset(engine->i915) + 2 * CACHELINE_BYTES;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001965 bool vf_flush_wa = false, dc_flush_wa = false;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001966 u32 *cs, flags = 0;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001967 int len;
Oscar Mateo47122742014-07-24 17:04:28 +01001968
1969 flags |= PIPE_CONTROL_CS_STALL;
1970
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001971 if (mode & EMIT_FLUSH) {
Oscar Mateo47122742014-07-24 17:04:28 +01001972 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
1973 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -08001974 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +01001975 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Oscar Mateo47122742014-07-24 17:04:28 +01001976 }
1977
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001978 if (mode & EMIT_INVALIDATE) {
Oscar Mateo47122742014-07-24 17:04:28 +01001979 flags |= PIPE_CONTROL_TLB_INVALIDATE;
1980 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
1981 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
1982 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
1983 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
1984 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
1985 flags |= PIPE_CONTROL_QW_WRITE;
1986 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Oscar Mateo47122742014-07-24 17:04:28 +01001987
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001988 /*
1989 * On GEN9: before VF_CACHE_INVALIDATE we need to emit a NULL
1990 * pipe control.
1991 */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08001992 if (IS_GEN(request->i915, 9))
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001993 vf_flush_wa = true;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001994
1995 /* WaForGAMHang:kbl */
1996 if (IS_KBL_REVID(request->i915, 0, KBL_REVID_B0))
1997 dc_flush_wa = true;
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001998 }
Imre Deak9647ff32015-01-25 13:27:11 -08001999
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03002000 len = 6;
2001
2002 if (vf_flush_wa)
2003 len += 6;
2004
2005 if (dc_flush_wa)
2006 len += 12;
2007
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00002008 cs = intel_ring_begin(request, len);
2009 if (IS_ERR(cs))
2010 return PTR_ERR(cs);
Oscar Mateo47122742014-07-24 17:04:28 +01002011
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00002012 if (vf_flush_wa)
2013 cs = gen8_emit_pipe_control(cs, 0, 0);
Imre Deak9647ff32015-01-25 13:27:11 -08002014
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00002015 if (dc_flush_wa)
2016 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_DC_FLUSH_ENABLE,
2017 0);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03002018
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00002019 cs = gen8_emit_pipe_control(cs, flags, scratch_addr);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03002020
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00002021 if (dc_flush_wa)
2022 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_CS_STALL, 0);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03002023
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00002024 intel_ring_advance(request, cs);
Oscar Mateo47122742014-07-24 17:04:28 +01002025
2026 return 0;
2027}
2028
Chris Wilson7c17d372016-01-20 15:43:35 +02002029/*
2030 * Reserve space for 2 NOOPs at the end of each request to be
2031 * used as a workaround for not being allowed to do lite
2032 * restore with HEAD==TAIL (WaIdleLiteRestore).
2033 */
Chris Wilsone61e0f52018-02-21 09:56:36 +00002034static void gen8_emit_wa_tail(struct i915_request *request, u32 *cs)
Oscar Mateo4da46e12014-07-24 17:04:27 +01002035{
Chris Wilsonbeecec92017-10-03 21:34:52 +01002036 /* Ensure there's always at least one preemption point per-request. */
2037 *cs++ = MI_ARB_CHECK;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00002038 *cs++ = MI_NOOP;
2039 request->wa_tail = intel_ring_offset(request, cs);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01002040}
Oscar Mateo4da46e12014-07-24 17:04:27 +01002041
Chris Wilsone61e0f52018-02-21 09:56:36 +00002042static void gen8_emit_breadcrumb(struct i915_request *request, u32 *cs)
Chris Wilsoncaddfe72016-10-28 13:58:52 +01002043{
Chris Wilson7c17d372016-01-20 15:43:35 +02002044 /* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */
2045 BUILD_BUG_ON(I915_GEM_HWS_INDEX_ADDR & (1 << 5));
Oscar Mateo4da46e12014-07-24 17:04:27 +01002046
Michał Winiarskidf77cd82017-10-25 22:00:15 +02002047 cs = gen8_emit_ggtt_write(cs, request->global_seqno,
2048 intel_hws_seqno_address(request->engine));
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00002049 *cs++ = MI_USER_INTERRUPT;
Chris Wilson74f9474122018-05-03 20:54:16 +01002050 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00002051 request->tail = intel_ring_offset(request, cs);
Chris Wilsoned1501d2017-03-27 14:14:12 +01002052 assert_ring_tail_valid(request->ring, request->tail);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01002053
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00002054 gen8_emit_wa_tail(request, cs);
Chris Wilson7c17d372016-01-20 15:43:35 +02002055}
Chris Wilson98f29e82016-10-28 13:58:51 +01002056static const int gen8_emit_breadcrumb_sz = 6 + WA_TAIL_DWORDS;
2057
Chris Wilsone61e0f52018-02-21 09:56:36 +00002058static void gen8_emit_breadcrumb_rcs(struct i915_request *request, u32 *cs)
Chris Wilson7c17d372016-01-20 15:43:35 +02002059{
Michał Winiarskice81a652016-04-12 15:51:55 +02002060 /* We're using qword write, seqno should be aligned to 8 bytes. */
2061 BUILD_BUG_ON(I915_GEM_HWS_INDEX & 1);
2062
Chris Wilson6a623722018-12-28 15:31:13 +00002063 cs = gen8_emit_ggtt_write_rcs(cs,
2064 request->global_seqno,
2065 intel_hws_seqno_address(request->engine),
2066 PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH |
2067 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
2068 PIPE_CONTROL_DC_FLUSH_ENABLE |
2069 PIPE_CONTROL_FLUSH_ENABLE |
2070 PIPE_CONTROL_CS_STALL);
2071
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00002072 *cs++ = MI_USER_INTERRUPT;
Chris Wilson74f9474122018-05-03 20:54:16 +01002073 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
Chris Wilson6a623722018-12-28 15:31:13 +00002074
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00002075 request->tail = intel_ring_offset(request, cs);
Chris Wilsoned1501d2017-03-27 14:14:12 +01002076 assert_ring_tail_valid(request->ring, request->tail);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01002077
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00002078 gen8_emit_wa_tail(request, cs);
Oscar Mateo4da46e12014-07-24 17:04:27 +01002079}
Michał Winiarskidf77cd82017-10-25 22:00:15 +02002080static const int gen8_emit_breadcrumb_rcs_sz = 8 + WA_TAIL_DWORDS;
Chris Wilson98f29e82016-10-28 13:58:51 +01002081
Chris Wilsone61e0f52018-02-21 09:56:36 +00002082static int gen8_init_rcs_context(struct i915_request *rq)
Thomas Daniele7778be2014-12-02 12:50:48 +00002083{
2084 int ret;
2085
Tvrtko Ursulin452420d2018-12-03 13:33:57 +00002086 ret = intel_engine_emit_ctx_wa(rq);
Thomas Daniele7778be2014-12-02 12:50:48 +00002087 if (ret)
2088 return ret;
2089
Chris Wilsone61e0f52018-02-21 09:56:36 +00002090 ret = intel_rcs_context_init_mocs(rq);
Peter Antoine3bbaba02015-07-10 20:13:11 +03002091 /*
2092 * Failing to program the MOCS is non-fatal.The system will not
2093 * run at peak performance. So generate an error and carry on.
2094 */
2095 if (ret)
2096 DRM_ERROR("MOCS failed to program: expect performance issues.\n");
2097
Chris Wilsone61e0f52018-02-21 09:56:36 +00002098 return i915_gem_render_state_emit(rq);
Thomas Daniele7778be2014-12-02 12:50:48 +00002099}
2100
Oscar Mateo73e4d072014-07-24 17:04:48 +01002101/**
2102 * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01002103 * @engine: Engine Command Streamer.
Oscar Mateo73e4d072014-07-24 17:04:48 +01002104 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002105void intel_logical_ring_cleanup(struct intel_engine_cs *engine)
Oscar Mateo454afeb2014-07-24 17:04:22 +01002106{
John Harrison6402c332014-10-31 12:00:26 +00002107 struct drm_i915_private *dev_priv;
Oscar Mateo9832b9d2014-07-24 17:04:30 +01002108
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01002109 /*
2110 * Tasklet cannot be active at this point due intel_mark_active/idle
2111 * so this is just for documentation.
2112 */
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302113 if (WARN_ON(test_bit(TASKLET_STATE_SCHED,
2114 &engine->execlists.tasklet.state)))
2115 tasklet_kill(&engine->execlists.tasklet);
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01002116
Chris Wilsonc0336662016-05-06 15:40:21 +01002117 dev_priv = engine->i915;
John Harrison6402c332014-10-31 12:00:26 +00002118
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002119 if (engine->buffer) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002120 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
Dave Gordonb0366a52015-12-08 15:02:36 +00002121 }
Oscar Mateo48d82382014-07-24 17:04:23 +01002122
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002123 if (engine->cleanup)
2124 engine->cleanup(engine);
Oscar Mateo48d82382014-07-24 17:04:23 +01002125
Chris Wilsone8a9c582016-12-18 15:37:20 +00002126 intel_engine_cleanup_common(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01002127
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00002128 lrc_destroy_wa_ctx(engine);
Chris Wilsonf3c9d4072018-01-02 15:12:34 +00002129
Chris Wilsonc0336662016-05-06 15:40:21 +01002130 engine->i915 = NULL;
Akash Goel3b3f1652016-10-13 22:44:48 +05302131 dev_priv->engine[engine->id] = NULL;
2132 kfree(engine);
Oscar Mateo454afeb2014-07-24 17:04:22 +01002133}
2134
Chris Wilson209b7952018-07-17 21:29:32 +01002135void intel_execlists_set_default_submission(struct intel_engine_cs *engine)
Chris Wilsonddd66c52016-08-02 22:50:31 +01002136{
Chris Wilsonff44ad52017-03-16 17:13:03 +00002137 engine->submit_request = execlists_submit_request;
Chris Wilson27a5f612017-09-15 18:31:00 +01002138 engine->cancel_requests = execlists_cancel_requests;
Chris Wilsone2f34962018-10-01 15:47:54 +01002139 engine->schedule = i915_schedule;
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302140 engine->execlists.tasklet.func = execlists_submission_tasklet;
Chris Wilsonaba5e272017-10-25 15:39:41 +01002141
Chris Wilson13291152018-05-16 19:33:52 +01002142 engine->reset.prepare = execlists_reset_prepare;
2143
Chris Wilsonaba5e272017-10-25 15:39:41 +01002144 engine->park = NULL;
2145 engine->unpark = NULL;
Tvrtko Ursulincf669b42017-11-29 10:28:05 +00002146
2147 engine->flags |= I915_ENGINE_SUPPORTS_STATS;
Chris Wilson2a694fe2018-04-03 19:35:37 +01002148 if (engine->i915->preempt_context)
2149 engine->flags |= I915_ENGINE_HAS_PREEMPTION;
Chris Wilson3fed1802018-02-07 21:05:43 +00002150
2151 engine->i915->caps.scheduler =
2152 I915_SCHEDULER_CAP_ENABLED |
2153 I915_SCHEDULER_CAP_PRIORITY;
Chris Wilson2a694fe2018-04-03 19:35:37 +01002154 if (intel_engine_has_preemption(engine))
Chris Wilson3fed1802018-02-07 21:05:43 +00002155 engine->i915->caps.scheduler |= I915_SCHEDULER_CAP_PREEMPTION;
Chris Wilsonddd66c52016-08-02 22:50:31 +01002156}
2157
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00002158static void
Chris Wilsone1382ef2016-05-06 15:40:20 +01002159logical_ring_default_vfuncs(struct intel_engine_cs *engine)
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00002160{
2161 /* Default vfuncs which can be overriden by each engine. */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002162 engine->init_hw = gen8_init_common_ring;
Chris Wilson5adfb772018-05-16 19:33:51 +01002163
2164 engine->reset.prepare = execlists_reset_prepare;
2165 engine->reset.reset = execlists_reset;
2166 engine->reset.finish = execlists_reset_finish;
Chris Wilsone8a9c582016-12-18 15:37:20 +00002167
2168 engine->context_pin = execlists_context_pin;
Chris Wilsonf73e7392016-12-18 15:37:24 +00002169 engine->request_alloc = execlists_request_alloc;
2170
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002171 engine->emit_flush = gen8_emit_flush;
Chris Wilson9b81d552016-10-28 13:58:50 +01002172 engine->emit_breadcrumb = gen8_emit_breadcrumb;
Chris Wilson98f29e82016-10-28 13:58:51 +01002173 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_sz;
Chris Wilsonff44ad52017-03-16 17:13:03 +00002174
Chris Wilson209b7952018-07-17 21:29:32 +01002175 engine->set_default_submission = intel_execlists_set_default_submission;
Chris Wilsonddd66c52016-08-02 22:50:31 +01002176
Tvrtko Ursulind4ccceb2018-03-02 18:14:56 +02002177 if (INTEL_GEN(engine->i915) < 11) {
2178 engine->irq_enable = gen8_logical_ring_enable_irq;
2179 engine->irq_disable = gen8_logical_ring_disable_irq;
2180 } else {
2181 /*
2182 * TODO: On Gen11 interrupt masks need to be clear
2183 * to allow C6 entry. Keep interrupts enabled at
2184 * and take the hit of generating extra interrupts
2185 * until a more refined solution exists.
2186 */
2187 }
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002188 engine->emit_bb_start = gen8_emit_bb_start;
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00002189}
2190
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00002191static inline void
Dave Gordonc2c7f242016-07-13 16:03:35 +01002192logical_ring_default_irqs(struct intel_engine_cs *engine)
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00002193{
Daniele Ceraolo Spuriofa6f0712018-03-14 11:26:53 -07002194 unsigned int shift = 0;
2195
2196 if (INTEL_GEN(engine->i915) < 11) {
2197 const u8 irq_shifts[] = {
2198 [RCS] = GEN8_RCS_IRQ_SHIFT,
2199 [BCS] = GEN8_BCS_IRQ_SHIFT,
2200 [VCS] = GEN8_VCS1_IRQ_SHIFT,
2201 [VCS2] = GEN8_VCS2_IRQ_SHIFT,
2202 [VECS] = GEN8_VECS_IRQ_SHIFT,
2203 };
2204
2205 shift = irq_shifts[engine->id];
2206 }
2207
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002208 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << shift;
2209 engine->irq_keep_mask = GT_CONTEXT_SWITCH_INTERRUPT << shift;
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00002210}
2211
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002212static void
2213logical_ring_setup(struct intel_engine_cs *engine)
2214{
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01002215 intel_engine_setup_common(engine);
2216
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002217 /* Intentionally left blank. */
2218 engine->buffer = NULL;
2219
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302220 tasklet_init(&engine->execlists.tasklet,
2221 execlists_submission_tasklet, (unsigned long)engine);
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002222
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002223 logical_ring_default_vfuncs(engine);
2224 logical_ring_default_irqs(engine);
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002225}
2226
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +01002227static int logical_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002228{
Chris Wilsonbc4237e2018-06-28 21:12:07 +01002229 struct drm_i915_private *i915 = engine->i915;
2230 struct intel_engine_execlists * const execlists = &engine->execlists;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002231 int ret;
2232
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01002233 ret = intel_engine_init_common(engine);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002234 if (ret)
Chris Wilsonb2164e42018-09-20 20:59:48 +01002235 return ret;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002236
Daniele Ceraolo Spurioa60acb22019-01-09 17:32:32 -08002237 intel_engine_init_workarounds(engine);
2238
Chris Wilsonbc4237e2018-06-28 21:12:07 +01002239 if (HAS_LOGICAL_RING_ELSQ(i915)) {
2240 execlists->submit_reg = i915->regs +
Thomas Daniel05f0add2018-03-02 18:14:59 +02002241 i915_mmio_reg_offset(RING_EXECLIST_SQ_CONTENTS(engine));
Chris Wilsonbc4237e2018-06-28 21:12:07 +01002242 execlists->ctrl_reg = i915->regs +
Thomas Daniel05f0add2018-03-02 18:14:59 +02002243 i915_mmio_reg_offset(RING_EXECLIST_CONTROL(engine));
2244 } else {
Chris Wilsonbc4237e2018-06-28 21:12:07 +01002245 execlists->submit_reg = i915->regs +
Thomas Daniel05f0add2018-03-02 18:14:59 +02002246 i915_mmio_reg_offset(RING_ELSP(engine));
2247 }
Chris Wilson693cfbf2018-01-02 15:12:33 +00002248
Chris Wilsonbc4237e2018-06-28 21:12:07 +01002249 execlists->preempt_complete_status = ~0u;
2250 if (i915->preempt_context) {
Chris Wilsonab82a062018-04-30 14:15:01 +01002251 struct intel_context *ce =
Chris Wilsonbc4237e2018-06-28 21:12:07 +01002252 to_intel_context(i915->preempt_context, engine);
Chris Wilsonab82a062018-04-30 14:15:01 +01002253
Chris Wilsonbc4237e2018-06-28 21:12:07 +01002254 execlists->preempt_complete_status =
Chris Wilsonab82a062018-04-30 14:15:01 +01002255 upper_32_bits(ce->lrc_desc);
2256 }
Chris Wilsond6376372018-02-07 21:05:44 +00002257
Chris Wilson46592892018-11-30 12:59:54 +00002258 execlists->csb_status =
2259 &engine->status_page.page_addr[I915_HWS_CSB_BUF0_INDEX];
Chris Wilsonbc4237e2018-06-28 21:12:07 +01002260
Chris Wilson46592892018-11-30 12:59:54 +00002261 execlists->csb_write =
2262 &engine->status_page.page_addr[intel_hws_csb_write_index(i915)];
Chris Wilsonbc4237e2018-06-28 21:12:07 +01002263
Chris Wilsonf4b58f02018-06-28 21:12:08 +01002264 reset_csb_pointers(execlists);
Chris Wilsonc3160da2018-05-31 09:22:45 +01002265
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002266 return 0;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002267}
2268
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +01002269int logical_render_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002270{
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002271 int ret;
2272
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002273 logical_ring_setup(engine);
2274
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002275 /* Override some for render ring. */
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002276 engine->init_context = gen8_init_rcs_context;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002277 engine->emit_flush = gen8_emit_flush_render;
Michał Winiarskidf77cd82017-10-25 22:00:15 +02002278 engine->emit_breadcrumb = gen8_emit_breadcrumb_rcs;
2279 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_rcs_sz;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002280
Chris Wilsonb2164e42018-09-20 20:59:48 +01002281 ret = logical_ring_init(engine);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002282 if (ret)
2283 return ret;
2284
2285 ret = intel_init_workaround_bb(engine);
2286 if (ret) {
2287 /*
2288 * We continue even if we fail to initialize WA batch
2289 * because we only expect rare glitches but nothing
2290 * critical to prevent us from using GPU
2291 */
2292 DRM_ERROR("WA batch buffer initialization failed: %d\n",
2293 ret);
2294 }
2295
Tvrtko Ursulin69bcdec2018-12-03 12:50:12 +00002296 intel_engine_init_whitelist(engine);
Tvrtko Ursulin4a15c75c2018-12-03 13:33:41 +00002297
Chris Wilsonb2164e42018-09-20 20:59:48 +01002298 return 0;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002299}
2300
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +01002301int logical_xcs_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002302{
2303 logical_ring_setup(engine);
2304
2305 return logical_ring_init(engine);
2306}
2307
Jeff McGee0cea6502015-02-13 10:27:56 -06002308static u32
Chris Wilsonc0336662016-05-06 15:40:21 +01002309make_rpcs(struct drm_i915_private *dev_priv)
Jeff McGee0cea6502015-02-13 10:27:56 -06002310{
Jani Nikula02584042018-12-31 16:56:41 +02002311 bool subslice_pg = RUNTIME_INFO(dev_priv)->sseu.has_subslice_pg;
2312 u8 slices = hweight8(RUNTIME_INFO(dev_priv)->sseu.slice_mask);
2313 u8 subslices = hweight8(RUNTIME_INFO(dev_priv)->sseu.subslice_mask[0]);
Jeff McGee0cea6502015-02-13 10:27:56 -06002314 u32 rpcs = 0;
2315
2316 /*
2317 * No explicit RPCS request is needed to ensure full
2318 * slice/subslice/EU enablement prior to Gen9.
2319 */
Chris Wilsonc0336662016-05-06 15:40:21 +01002320 if (INTEL_GEN(dev_priv) < 9)
Jeff McGee0cea6502015-02-13 10:27:56 -06002321 return 0;
2322
2323 /*
Tvrtko Ursulinb212f0a2018-09-03 12:30:07 +01002324 * Since the SScount bitfield in GEN8_R_PWR_CLK_STATE is only three bits
2325 * wide and Icelake has up to eight subslices, specfial programming is
2326 * needed in order to correctly enable all subslices.
2327 *
2328 * According to documentation software must consider the configuration
2329 * as 2x4x8 and hardware will translate this to 1x8x8.
2330 *
2331 * Furthemore, even though SScount is three bits, maximum documented
2332 * value for it is four. From this some rules/restrictions follow:
2333 *
2334 * 1.
2335 * If enabled subslice count is greater than four, two whole slices must
2336 * be enabled instead.
2337 *
2338 * 2.
2339 * When more than one slice is enabled, hardware ignores the subslice
2340 * count altogether.
2341 *
2342 * From these restrictions it follows that it is not possible to enable
2343 * a count of subslices between the SScount maximum of four restriction,
2344 * and the maximum available number on a particular SKU. Either all
2345 * subslices are enabled, or a count between one and four on the first
2346 * slice.
2347 */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08002348 if (IS_GEN(dev_priv, 11) && slices == 1 && subslices >= 4) {
Tvrtko Ursulinb212f0a2018-09-03 12:30:07 +01002349 GEM_BUG_ON(subslices & 1);
2350
2351 subslice_pg = false;
2352 slices *= 2;
2353 }
2354
2355 /*
Jeff McGee0cea6502015-02-13 10:27:56 -06002356 * Starting in Gen9, render power gating can leave
2357 * slice/subslice/EU in a partially enabled state. We
2358 * must make an explicit request through RPCS for full
2359 * enablement.
2360 */
Jani Nikula02584042018-12-31 16:56:41 +02002361 if (RUNTIME_INFO(dev_priv)->sseu.has_slice_pg) {
Tvrtko Ursulinb212f0a2018-09-03 12:30:07 +01002362 u32 mask, val = slices;
2363
2364 if (INTEL_GEN(dev_priv) >= 11) {
2365 mask = GEN11_RPCS_S_CNT_MASK;
2366 val <<= GEN11_RPCS_S_CNT_SHIFT;
2367 } else {
2368 mask = GEN8_RPCS_S_CNT_MASK;
2369 val <<= GEN8_RPCS_S_CNT_SHIFT;
2370 }
2371
2372 GEM_BUG_ON(val & ~mask);
2373 val &= mask;
2374
2375 rpcs |= GEN8_RPCS_ENABLE | GEN8_RPCS_S_CNT_ENABLE | val;
Jeff McGee0cea6502015-02-13 10:27:56 -06002376 }
2377
Tvrtko Ursulinb212f0a2018-09-03 12:30:07 +01002378 if (subslice_pg) {
2379 u32 val = subslices;
2380
2381 val <<= GEN8_RPCS_SS_CNT_SHIFT;
2382
2383 GEM_BUG_ON(val & ~GEN8_RPCS_SS_CNT_MASK);
2384 val &= GEN8_RPCS_SS_CNT_MASK;
2385
2386 rpcs |= GEN8_RPCS_ENABLE | GEN8_RPCS_SS_CNT_ENABLE | val;
Jeff McGee0cea6502015-02-13 10:27:56 -06002387 }
2388
Jani Nikula02584042018-12-31 16:56:41 +02002389 if (RUNTIME_INFO(dev_priv)->sseu.has_eu_pg) {
Tvrtko Ursulinb212f0a2018-09-03 12:30:07 +01002390 u32 val;
2391
Jani Nikula02584042018-12-31 16:56:41 +02002392 val = RUNTIME_INFO(dev_priv)->sseu.eu_per_subslice <<
Tvrtko Ursulinb212f0a2018-09-03 12:30:07 +01002393 GEN8_RPCS_EU_MIN_SHIFT;
2394 GEM_BUG_ON(val & ~GEN8_RPCS_EU_MIN_MASK);
2395 val &= GEN8_RPCS_EU_MIN_MASK;
2396
2397 rpcs |= val;
2398
Jani Nikula02584042018-12-31 16:56:41 +02002399 val = RUNTIME_INFO(dev_priv)->sseu.eu_per_subslice <<
Tvrtko Ursulinb212f0a2018-09-03 12:30:07 +01002400 GEN8_RPCS_EU_MAX_SHIFT;
2401 GEM_BUG_ON(val & ~GEN8_RPCS_EU_MAX_MASK);
2402 val &= GEN8_RPCS_EU_MAX_MASK;
2403
2404 rpcs |= val;
2405
Jeff McGee0cea6502015-02-13 10:27:56 -06002406 rpcs |= GEN8_RPCS_ENABLE;
2407 }
2408
2409 return rpcs;
2410}
2411
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002412static u32 intel_lr_indirect_ctx_offset(struct intel_engine_cs *engine)
Michel Thierry71562912016-02-23 10:31:49 +00002413{
2414 u32 indirect_ctx_offset;
2415
Chris Wilsonc0336662016-05-06 15:40:21 +01002416 switch (INTEL_GEN(engine->i915)) {
Michel Thierry71562912016-02-23 10:31:49 +00002417 default:
Chris Wilsonc0336662016-05-06 15:40:21 +01002418 MISSING_CASE(INTEL_GEN(engine->i915));
Michel Thierry71562912016-02-23 10:31:49 +00002419 /* fall through */
Michel Thierryfd034c72018-03-02 18:15:00 +02002420 case 11:
2421 indirect_ctx_offset =
2422 GEN11_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2423 break;
Michel Thierry7bd0a2c2017-06-06 13:30:38 -07002424 case 10:
2425 indirect_ctx_offset =
2426 GEN10_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2427 break;
Michel Thierry71562912016-02-23 10:31:49 +00002428 case 9:
2429 indirect_ctx_offset =
2430 GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2431 break;
2432 case 8:
2433 indirect_ctx_offset =
2434 GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2435 break;
2436 }
2437
2438 return indirect_ctx_offset;
2439}
2440
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002441static void execlists_init_reg_state(u32 *regs,
Chris Wilsona3aabe82016-10-04 21:11:26 +01002442 struct i915_gem_context *ctx,
2443 struct intel_engine_cs *engine,
2444 struct intel_ring *ring)
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002445{
Chris Wilsona3aabe82016-10-04 21:11:26 +01002446 struct drm_i915_private *dev_priv = engine->i915;
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002447 u32 base = engine->mmio_base;
Chris Wilson1fc44d92018-05-17 22:26:32 +01002448 bool rcs = engine->class == RENDER_CLASS;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002449
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002450 /* A context is actually a big batch buffer with several
2451 * MI_LOAD_REGISTER_IMM commands followed by (reg, value) pairs. The
2452 * values we are setting here are only for the first context restore:
2453 * on a subsequent save, the GPU will recreate this batchbuffer with new
2454 * values (including all the missing MI_LOAD_REGISTER_IMM commands that
2455 * we are not initializing here).
2456 */
2457 regs[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(rcs ? 14 : 11) |
2458 MI_LRI_FORCE_POSTED;
2459
2460 CTX_REG(regs, CTX_CONTEXT_CONTROL, RING_CONTEXT_CONTROL(engine),
Paulo Zanoniee435832018-08-09 16:58:52 -07002461 _MASKED_BIT_DISABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT) |
Lucas De Marchi08e3e212018-08-03 16:24:43 -07002462 _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH));
Paulo Zanoniee435832018-08-09 16:58:52 -07002463 if (INTEL_GEN(dev_priv) < 11) {
2464 regs[CTX_CONTEXT_CONTROL + 1] |=
2465 _MASKED_BIT_DISABLE(CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT |
2466 CTX_CTRL_RS_CTX_ENABLE);
2467 }
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002468 CTX_REG(regs, CTX_RING_HEAD, RING_HEAD(base), 0);
2469 CTX_REG(regs, CTX_RING_TAIL, RING_TAIL(base), 0);
2470 CTX_REG(regs, CTX_RING_BUFFER_START, RING_START(base), 0);
2471 CTX_REG(regs, CTX_RING_BUFFER_CONTROL, RING_CTL(base),
2472 RING_CTL_SIZE(ring->size) | RING_VALID);
2473 CTX_REG(regs, CTX_BB_HEAD_U, RING_BBADDR_UDW(base), 0);
2474 CTX_REG(regs, CTX_BB_HEAD_L, RING_BBADDR(base), 0);
2475 CTX_REG(regs, CTX_BB_STATE, RING_BBSTATE(base), RING_BB_PPGTT);
2476 CTX_REG(regs, CTX_SECOND_BB_HEAD_U, RING_SBBADDR_UDW(base), 0);
2477 CTX_REG(regs, CTX_SECOND_BB_HEAD_L, RING_SBBADDR(base), 0);
2478 CTX_REG(regs, CTX_SECOND_BB_STATE, RING_SBBSTATE(base), 0);
2479 if (rcs) {
Chris Wilson604a8f62017-09-21 14:54:43 +01002480 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
2481
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002482 CTX_REG(regs, CTX_RCS_INDIRECT_CTX, RING_INDIRECT_CTX(base), 0);
2483 CTX_REG(regs, CTX_RCS_INDIRECT_CTX_OFFSET,
2484 RING_INDIRECT_CTX_OFFSET(base), 0);
Chris Wilson604a8f62017-09-21 14:54:43 +01002485 if (wa_ctx->indirect_ctx.size) {
Chris Wilsonbde13eb2016-08-15 10:49:07 +01002486 u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01002487
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002488 regs[CTX_RCS_INDIRECT_CTX + 1] =
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00002489 (ggtt_offset + wa_ctx->indirect_ctx.offset) |
2490 (wa_ctx->indirect_ctx.size / CACHELINE_BYTES);
Arun Siluvery17ee9502015-06-19 19:07:01 +01002491
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002492 regs[CTX_RCS_INDIRECT_CTX_OFFSET + 1] =
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002493 intel_lr_indirect_ctx_offset(engine) << 6;
Chris Wilson604a8f62017-09-21 14:54:43 +01002494 }
2495
2496 CTX_REG(regs, CTX_BB_PER_CTX_PTR, RING_BB_PER_CTX_PTR(base), 0);
2497 if (wa_ctx->per_ctx.size) {
2498 u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01002499
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002500 regs[CTX_BB_PER_CTX_PTR + 1] =
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00002501 (ggtt_offset + wa_ctx->per_ctx.offset) | 0x01;
Arun Siluvery17ee9502015-06-19 19:07:01 +01002502 }
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002503 }
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002504
2505 regs[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9) | MI_LRI_FORCE_POSTED;
2506
2507 CTX_REG(regs, CTX_CTX_TIMESTAMP, RING_CTX_TIMESTAMP(base), 0);
Ville Syrjälä0d925ea2015-11-04 23:20:11 +02002508 /* PDP values well be assigned later if needed */
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002509 CTX_REG(regs, CTX_PDP3_UDW, GEN8_RING_PDP_UDW(engine, 3), 0);
2510 CTX_REG(regs, CTX_PDP3_LDW, GEN8_RING_PDP_LDW(engine, 3), 0);
2511 CTX_REG(regs, CTX_PDP2_UDW, GEN8_RING_PDP_UDW(engine, 2), 0);
2512 CTX_REG(regs, CTX_PDP2_LDW, GEN8_RING_PDP_LDW(engine, 2), 0);
2513 CTX_REG(regs, CTX_PDP1_UDW, GEN8_RING_PDP_UDW(engine, 1), 0);
2514 CTX_REG(regs, CTX_PDP1_LDW, GEN8_RING_PDP_LDW(engine, 1), 0);
2515 CTX_REG(regs, CTX_PDP0_UDW, GEN8_RING_PDP_UDW(engine, 0), 0);
2516 CTX_REG(regs, CTX_PDP0_LDW, GEN8_RING_PDP_LDW(engine, 0), 0);
Michel Thierryd7b26332015-04-08 12:13:34 +01002517
Chris Wilson4bdafb92018-09-26 21:12:22 +01002518 if (i915_vm_is_48bit(&ctx->ppgtt->vm)) {
Michel Thierry2dba3232015-07-30 11:06:23 +01002519 /* 64b PPGTT (48bit canonical)
2520 * PDP0_DESCRIPTOR contains the base address to PML4 and
2521 * other PDP Descriptors are ignored.
2522 */
Chris Wilson4bdafb92018-09-26 21:12:22 +01002523 ASSIGN_CTX_PML4(ctx->ppgtt, regs);
Chris Wilsone8894262018-12-07 09:02:13 +00002524 } else {
2525 ASSIGN_CTX_PDP(ctx->ppgtt, regs, 3);
2526 ASSIGN_CTX_PDP(ctx->ppgtt, regs, 2);
2527 ASSIGN_CTX_PDP(ctx->ppgtt, regs, 1);
2528 ASSIGN_CTX_PDP(ctx->ppgtt, regs, 0);
Michel Thierry2dba3232015-07-30 11:06:23 +01002529 }
2530
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002531 if (rcs) {
2532 regs[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
2533 CTX_REG(regs, CTX_R_PWR_CLK_STATE, GEN8_R_PWR_CLK_STATE,
2534 make_rpcs(dev_priv));
Robert Bragg19f81df2017-06-13 12:23:03 +01002535
2536 i915_oa_init_reg_state(engine, ctx, regs);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002537 }
Chris Wilsond0f5cc52018-07-30 17:43:25 +01002538
2539 regs[CTX_END] = MI_BATCH_BUFFER_END;
2540 if (INTEL_GEN(dev_priv) >= 10)
2541 regs[CTX_END] |= BIT(0);
Chris Wilsona3aabe82016-10-04 21:11:26 +01002542}
2543
2544static int
2545populate_lr_context(struct i915_gem_context *ctx,
2546 struct drm_i915_gem_object *ctx_obj,
2547 struct intel_engine_cs *engine,
2548 struct intel_ring *ring)
2549{
2550 void *vaddr;
Chris Wilsond2b4b972017-11-10 14:26:33 +00002551 u32 *regs;
Chris Wilsona3aabe82016-10-04 21:11:26 +01002552 int ret;
2553
2554 ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
2555 if (ret) {
2556 DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
2557 return ret;
2558 }
2559
2560 vaddr = i915_gem_object_pin_map(ctx_obj, I915_MAP_WB);
2561 if (IS_ERR(vaddr)) {
2562 ret = PTR_ERR(vaddr);
2563 DRM_DEBUG_DRIVER("Could not map object pages! (%d)\n", ret);
2564 return ret;
2565 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002566 ctx_obj->mm.dirty = true;
Chris Wilsona3aabe82016-10-04 21:11:26 +01002567
Chris Wilsond2b4b972017-11-10 14:26:33 +00002568 if (engine->default_state) {
2569 /*
2570 * We only want to copy over the template context state;
2571 * skipping over the headers reserved for GuC communication,
2572 * leaving those as zero.
2573 */
2574 const unsigned long start = LRC_HEADER_PAGES * PAGE_SIZE;
2575 void *defaults;
2576
2577 defaults = i915_gem_object_pin_map(engine->default_state,
2578 I915_MAP_WB);
Matthew Auldaaefa062018-03-01 11:46:39 +00002579 if (IS_ERR(defaults)) {
2580 ret = PTR_ERR(defaults);
2581 goto err_unpin_ctx;
2582 }
Chris Wilsond2b4b972017-11-10 14:26:33 +00002583
2584 memcpy(vaddr + start, defaults + start, engine->context_size);
2585 i915_gem_object_unpin_map(engine->default_state);
2586 }
2587
Chris Wilsona3aabe82016-10-04 21:11:26 +01002588 /* The second page of the context object contains some fields which must
2589 * be set up prior to the first execution. */
Chris Wilsond2b4b972017-11-10 14:26:33 +00002590 regs = vaddr + LRC_STATE_PN * PAGE_SIZE;
2591 execlists_init_reg_state(regs, ctx, engine, ring);
2592 if (!engine->default_state)
2593 regs[CTX_CONTEXT_CONTROL + 1] |=
2594 _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT);
Thomas Daniel05f0add2018-03-02 18:14:59 +02002595 if (ctx == ctx->i915->preempt_context && INTEL_GEN(engine->i915) < 11)
Chris Wilson517aaff2018-01-23 21:04:12 +00002596 regs[CTX_CONTEXT_CONTROL + 1] |=
2597 _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
2598 CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002599
Matthew Auldaaefa062018-03-01 11:46:39 +00002600err_unpin_ctx:
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01002601 i915_gem_object_unpin_map(ctx_obj);
Matthew Auldaaefa062018-03-01 11:46:39 +00002602 return ret;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002603}
2604
Chris Wilsone2efd132016-05-24 14:53:34 +01002605static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
Chris Wilson1fc44d92018-05-17 22:26:32 +01002606 struct intel_engine_cs *engine,
2607 struct intel_context *ce)
Oscar Mateoede7d422014-07-24 17:04:12 +01002608{
Oscar Mateo8c8579172014-07-24 17:04:14 +01002609 struct drm_i915_gem_object *ctx_obj;
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002610 struct i915_vma *vma;
Jani Nikula739f3ab2019-01-16 11:15:19 +02002611 u32 context_size;
Chris Wilson7e37f882016-08-02 22:50:21 +01002612 struct intel_ring *ring;
Chris Wilsona89d1f92018-05-02 17:38:39 +01002613 struct i915_timeline *timeline;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002614 int ret;
2615
Chris Wilson1d2a19c2018-01-26 12:18:46 +00002616 if (ce->state)
2617 return 0;
Oscar Mateoede7d422014-07-24 17:04:12 +01002618
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +03002619 context_size = round_up(engine->context_size, I915_GTT_PAGE_SIZE);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002620
Michel Thierry0b29c752017-09-13 09:56:00 +01002621 /*
2622 * Before the actual start of the context image, we insert a few pages
2623 * for our own use and for sharing with the GuC.
2624 */
2625 context_size += LRC_HEADER_PAGES * PAGE_SIZE;
Alex Daid1675192015-08-12 15:43:43 +01002626
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00002627 ctx_obj = i915_gem_object_create(ctx->i915, context_size);
Chris Wilson467d3572018-06-11 16:33:32 +01002628 if (IS_ERR(ctx_obj))
2629 return PTR_ERR(ctx_obj);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002630
Chris Wilson82ad6442018-06-05 16:37:58 +01002631 vma = i915_vma_instance(ctx_obj, &ctx->i915->ggtt.vm, NULL);
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002632 if (IS_ERR(vma)) {
2633 ret = PTR_ERR(vma);
2634 goto error_deref_obj;
2635 }
2636
Chris Wilsona89d1f92018-05-02 17:38:39 +01002637 timeline = i915_timeline_create(ctx->i915, ctx->name);
2638 if (IS_ERR(timeline)) {
2639 ret = PTR_ERR(timeline);
2640 goto error_deref_obj;
2641 }
2642
2643 ring = intel_engine_create_ring(engine, timeline, ctx->ring_size);
2644 i915_timeline_put(timeline);
Chris Wilsondca33ec2016-08-02 22:50:20 +01002645 if (IS_ERR(ring)) {
2646 ret = PTR_ERR(ring);
Nick Hoathe84fe802015-09-11 12:53:46 +01002647 goto error_deref_obj;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002648 }
2649
Chris Wilsondca33ec2016-08-02 22:50:20 +01002650 ret = populate_lr_context(ctx, ctx_obj, engine, ring);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002651 if (ret) {
2652 DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
Chris Wilsondca33ec2016-08-02 22:50:20 +01002653 goto error_ring_free;
Oscar Mateo84c23772014-07-24 17:04:15 +01002654 }
2655
Chris Wilsondca33ec2016-08-02 22:50:20 +01002656 ce->ring = ring;
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002657 ce->state = vma;
Oscar Mateoede7d422014-07-24 17:04:12 +01002658
2659 return 0;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002660
Chris Wilsondca33ec2016-08-02 22:50:20 +01002661error_ring_free:
Chris Wilson7e37f882016-08-02 22:50:21 +01002662 intel_ring_free(ring);
Nick Hoathe84fe802015-09-11 12:53:46 +01002663error_deref_obj:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002664 i915_gem_object_put(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002665 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01002666}
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002667
Chris Wilsondee60ca2018-09-14 13:35:02 +01002668void intel_lr_context_resume(struct drm_i915_private *i915)
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002669{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002670 struct intel_engine_cs *engine;
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002671 struct i915_gem_context *ctx;
Akash Goel3b3f1652016-10-13 22:44:48 +05302672 enum intel_engine_id id;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002673
Chris Wilsondee60ca2018-09-14 13:35:02 +01002674 /*
2675 * Because we emit WA_TAIL_DWORDS there may be a disparity
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002676 * between our bookkeeping in ce->ring->head and ce->ring->tail and
2677 * that stored in context. As we only write new commands from
2678 * ce->ring->tail onwards, everything before that is junk. If the GPU
2679 * starts reading from its RING_HEAD from the context, it may try to
2680 * execute that junk and die.
2681 *
2682 * So to avoid that we reset the context images upon resume. For
2683 * simplicity, we just zero everything out.
2684 */
Chris Wilsondee60ca2018-09-14 13:35:02 +01002685 list_for_each_entry(ctx, &i915->contexts.list, link) {
2686 for_each_engine(engine, i915, id) {
Chris Wilsonab82a062018-04-30 14:15:01 +01002687 struct intel_context *ce =
2688 to_intel_context(ctx, engine);
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002689
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002690 if (!ce->state)
2691 continue;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002692
Chris Wilsone6ba9992017-04-25 14:00:49 +01002693 intel_ring_reset(ce->ring, 0);
Chris Wilsondee60ca2018-09-14 13:35:02 +01002694
2695 if (ce->pin_count) { /* otherwise done in context_pin */
2696 u32 *regs = ce->lrc_reg_state;
2697
2698 regs[CTX_RING_HEAD + 1] = ce->ring->head;
2699 regs[CTX_RING_TAIL + 1] = ce->ring->tail;
2700 }
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002701 }
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002702 }
2703}
Chris Wilson2c665552018-04-04 10:33:29 +01002704
Chris Wilson0212bde2019-01-15 21:29:48 +00002705void intel_execlists_show_requests(struct intel_engine_cs *engine,
2706 struct drm_printer *m,
2707 void (*show_request)(struct drm_printer *m,
2708 struct i915_request *rq,
2709 const char *prefix),
2710 unsigned int max)
2711{
2712 const struct intel_engine_execlists *execlists = &engine->execlists;
2713 struct i915_request *rq, *last;
2714 unsigned long flags;
2715 unsigned int count;
2716 struct rb_node *rb;
2717
2718 spin_lock_irqsave(&engine->timeline.lock, flags);
2719
2720 last = NULL;
2721 count = 0;
2722 list_for_each_entry(rq, &engine->timeline.requests, link) {
2723 if (count++ < max - 1)
2724 show_request(m, rq, "\t\tE ");
2725 else
2726 last = rq;
2727 }
2728 if (last) {
2729 if (count > max) {
2730 drm_printf(m,
2731 "\t\t...skipping %d executing requests...\n",
2732 count - max);
2733 }
2734 show_request(m, last, "\t\tE ");
2735 }
2736
2737 last = NULL;
2738 count = 0;
2739 drm_printf(m, "\t\tQueue priority: %d\n", execlists->queue_priority);
2740 for (rb = rb_first_cached(&execlists->queue); rb; rb = rb_next(rb)) {
2741 struct i915_priolist *p = rb_entry(rb, typeof(*p), node);
2742 int i;
2743
2744 priolist_for_each_request(rq, p, i) {
2745 if (count++ < max - 1)
2746 show_request(m, rq, "\t\tQ ");
2747 else
2748 last = rq;
2749 }
2750 }
2751 if (last) {
2752 if (count > max) {
2753 drm_printf(m,
2754 "\t\t...skipping %d queued requests...\n",
2755 count - max);
2756 }
2757 show_request(m, last, "\t\tQ ");
2758 }
2759
2760 spin_unlock_irqrestore(&engine->timeline.lock, flags);
2761}
2762
Chris Wilson2c665552018-04-04 10:33:29 +01002763#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
2764#include "selftests/intel_lrc.c"
2765#endif