blob: 29b14d7d4b07af31c6a7fcdb8ed227a2ebf6dbe7 [file] [log] [blame]
Oscar Mateob20385f2014-07-24 17:04:10 +01001/*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Ben Widawsky <ben@bwidawsk.net>
25 * Michel Thierry <michel.thierry@intel.com>
26 * Thomas Daniel <thomas.daniel@intel.com>
27 * Oscar Mateo <oscar.mateo@intel.com>
28 *
29 */
30
Oscar Mateo73e4d072014-07-24 17:04:48 +010031/**
32 * DOC: Logical Rings, Logical Ring Contexts and Execlists
33 *
34 * Motivation:
Oscar Mateob20385f2014-07-24 17:04:10 +010035 * GEN8 brings an expansion of the HW contexts: "Logical Ring Contexts".
36 * These expanded contexts enable a number of new abilities, especially
37 * "Execlists" (also implemented in this file).
38 *
Oscar Mateo73e4d072014-07-24 17:04:48 +010039 * One of the main differences with the legacy HW contexts is that logical
40 * ring contexts incorporate many more things to the context's state, like
41 * PDPs or ringbuffer control registers:
42 *
43 * The reason why PDPs are included in the context is straightforward: as
44 * PPGTTs (per-process GTTs) are actually per-context, having the PDPs
45 * contained there mean you don't need to do a ppgtt->switch_mm yourself,
46 * instead, the GPU will do it for you on the context switch.
47 *
48 * But, what about the ringbuffer control registers (head, tail, etc..)?
49 * shouldn't we just need a set of those per engine command streamer? This is
50 * where the name "Logical Rings" starts to make sense: by virtualizing the
51 * rings, the engine cs shifts to a new "ring buffer" with every context
52 * switch. When you want to submit a workload to the GPU you: A) choose your
53 * context, B) find its appropriate virtualized ring, C) write commands to it
54 * and then, finally, D) tell the GPU to switch to that context.
55 *
56 * Instead of the legacy MI_SET_CONTEXT, the way you tell the GPU to switch
57 * to a contexts is via a context execution list, ergo "Execlists".
58 *
59 * LRC implementation:
60 * Regarding the creation of contexts, we have:
61 *
62 * - One global default context.
63 * - One local default context for each opened fd.
64 * - One local extra context for each context create ioctl call.
65 *
66 * Now that ringbuffers belong per-context (and not per-engine, like before)
67 * and that contexts are uniquely tied to a given engine (and not reusable,
68 * like before) we need:
69 *
70 * - One ringbuffer per-engine inside each context.
71 * - One backing object per-engine inside each context.
72 *
73 * The global default context starts its life with these new objects fully
74 * allocated and populated. The local default context for each opened fd is
75 * more complex, because we don't know at creation time which engine is going
76 * to use them. To handle this, we have implemented a deferred creation of LR
77 * contexts:
78 *
79 * The local context starts its life as a hollow or blank holder, that only
80 * gets populated for a given engine once we receive an execbuffer. If later
81 * on we receive another execbuffer ioctl for the same context but a different
82 * engine, we allocate/populate a new ringbuffer and context backing object and
83 * so on.
84 *
85 * Finally, regarding local contexts created using the ioctl call: as they are
86 * only allowed with the render ring, we can allocate & populate them right
87 * away (no need to defer anything, at least for now).
88 *
89 * Execlists implementation:
Oscar Mateob20385f2014-07-24 17:04:10 +010090 * Execlists are the new method by which, on gen8+ hardware, workloads are
91 * submitted for execution (as opposed to the legacy, ringbuffer-based, method).
Oscar Mateo73e4d072014-07-24 17:04:48 +010092 * This method works as follows:
93 *
94 * When a request is committed, its commands (the BB start and any leading or
95 * trailing commands, like the seqno breadcrumbs) are placed in the ringbuffer
96 * for the appropriate context. The tail pointer in the hardware context is not
97 * updated at this time, but instead, kept by the driver in the ringbuffer
98 * structure. A structure representing this request is added to a request queue
99 * for the appropriate engine: this structure contains a copy of the context's
100 * tail after the request was written to the ring buffer and a pointer to the
101 * context itself.
102 *
103 * If the engine's request queue was empty before the request was added, the
104 * queue is processed immediately. Otherwise the queue will be processed during
105 * a context switch interrupt. In any case, elements on the queue will get sent
106 * (in pairs) to the GPU's ExecLists Submit Port (ELSP, for short) with a
107 * globally unique 20-bits submission ID.
108 *
109 * When execution of a request completes, the GPU updates the context status
110 * buffer with a context complete event and generates a context switch interrupt.
111 * During the interrupt handling, the driver examines the events in the buffer:
112 * for each context complete event, if the announced ID matches that on the head
113 * of the request queue, then that request is retired and removed from the queue.
114 *
115 * After processing, if any requests were retired and the queue is not empty
116 * then a new execution list can be submitted. The two requests at the front of
117 * the queue are next to be submitted but since a context may not occur twice in
118 * an execution list, if subsequent requests have the same ID as the first then
119 * the two requests must be combined. This is done simply by discarding requests
120 * at the head of the queue until either only one requests is left (in which case
121 * we use a NULL second context) or the first two requests have unique IDs.
122 *
123 * By always executing the first two requests in the queue the driver ensures
124 * that the GPU is kept as busy as possible. In the case where a single context
125 * completes but a second context is still executing, the request for this second
126 * context will be at the head of the queue when we remove the first one. This
127 * request will then be resubmitted along with a new request for a different context,
128 * which will cause the hardware to continue executing the second request and queue
129 * the new request (the GPU detects the condition of a context getting preempted
130 * with the same context and optimizes the context switch flow by not doing
131 * preemption, but just sampling the new tail pointer).
132 *
Oscar Mateob20385f2014-07-24 17:04:10 +0100133 */
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +0100134#include <linux/interrupt.h>
Oscar Mateob20385f2014-07-24 17:04:10 +0100135
136#include <drm/drmP.h>
137#include <drm/i915_drm.h>
138#include "i915_drv.h"
Chris Wilson7c2fa7f2017-11-10 14:26:34 +0000139#include "i915_gem_render_state.h"
Michel Thierry578f1ac2018-01-23 16:43:49 -0800140#include "intel_lrc_reg.h"
Peter Antoine3bbaba02015-07-10 20:13:11 +0300141#include "intel_mocs.h"
Oscar Mateo127f1002014-07-24 17:04:11 +0100142
Thomas Daniele981e7b2014-07-24 17:04:39 +0100143#define RING_EXECLIST_QFULL (1 << 0x2)
144#define RING_EXECLIST1_VALID (1 << 0x3)
145#define RING_EXECLIST0_VALID (1 << 0x4)
146#define RING_EXECLIST_ACTIVE_STATUS (3 << 0xE)
147#define RING_EXECLIST1_ACTIVE (1 << 0x11)
148#define RING_EXECLIST0_ACTIVE (1 << 0x12)
149
150#define GEN8_CTX_STATUS_IDLE_ACTIVE (1 << 0)
151#define GEN8_CTX_STATUS_PREEMPTED (1 << 1)
152#define GEN8_CTX_STATUS_ELEMENT_SWITCH (1 << 2)
153#define GEN8_CTX_STATUS_ACTIVE_IDLE (1 << 3)
154#define GEN8_CTX_STATUS_COMPLETE (1 << 4)
155#define GEN8_CTX_STATUS_LITE_RESTORE (1 << 15)
Oscar Mateo8670d6f2014-07-24 17:04:17 +0100156
Chris Wilson70c2a242016-09-09 14:11:46 +0100157#define GEN8_CTX_STATUS_COMPLETED_MASK \
Chris Wilsond8747af2017-11-20 12:34:56 +0000158 (GEN8_CTX_STATUS_COMPLETE | GEN8_CTX_STATUS_PREEMPTED)
Chris Wilson70c2a242016-09-09 14:11:46 +0100159
Chris Wilson0e93cdd2016-04-29 09:07:06 +0100160/* Typical size of the average request (2 pipecontrols and a MI_BB) */
161#define EXECLISTS_REQUEST_SIZE 64 /* bytes */
Chris Wilsona3aabe82016-10-04 21:11:26 +0100162#define WA_TAIL_DWORDS 2
Chris Wilson7e4992a2017-09-28 20:38:59 +0100163#define WA_TAIL_BYTES (sizeof(u32) * WA_TAIL_DWORDS)
Chris Wilsonbeecec92017-10-03 21:34:52 +0100164#define PREEMPT_ID 0x1
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 Wilson978f1e02016-04-28 09:56:54 +0100167 struct intel_engine_cs *engine);
Chris Wilsona3aabe82016-10-04 21:11:26 +0100168static void execlists_init_reg_state(u32 *reg_state,
169 struct i915_gem_context *ctx,
170 struct intel_engine_cs *engine,
171 struct intel_ring *ring);
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000172
Oscar Mateo73e4d072014-07-24 17:04:48 +0100173/**
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000174 * intel_lr_context_descriptor_update() - calculate & cache the descriptor
175 * descriptor for a pinned context
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000176 * @ctx: Context to work on
Chris Wilson9021ad02016-05-24 14:53:37 +0100177 * @engine: Engine the descriptor will be used with
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000178 *
179 * The context descriptor encodes various attributes of a context,
180 * including its GTT address and some flags. Because it's fairly
181 * expensive to calculate, we'll just do it once and cache the result,
182 * which remains valid until the context is unpinned.
183 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200184 * This is what a descriptor looks like, from LSB to MSB::
185 *
Mika Kuoppala2355cf02017-01-27 15:03:09 +0200186 * bits 0-11: flags, GEN8_CTX_* (cached in ctx->desc_template)
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200187 * bits 12-31: LRCA, GTT address of (the HWSP of) this context
188 * bits 32-52: ctx ID, a globally unique tag
189 * bits 53-54: mbz, reserved for use by hardware
190 * bits 55-63: group ID, currently unused and set to 0
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000191 */
192static void
Chris Wilsone2efd132016-05-24 14:53:34 +0100193intel_lr_context_descriptor_update(struct i915_gem_context *ctx,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000194 struct intel_engine_cs *engine)
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000195{
Chris Wilson9021ad02016-05-24 14:53:37 +0100196 struct intel_context *ce = &ctx->engine[engine->id];
Chris Wilson7069b142016-04-28 09:56:52 +0100197 u64 desc;
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000198
Chris Wilson7069b142016-04-28 09:56:52 +0100199 BUILD_BUG_ON(MAX_CONTEXT_HW_ID > (1<<GEN8_CTX_ID_WIDTH));
200
Mika Kuoppala2355cf02017-01-27 15:03:09 +0200201 desc = ctx->desc_template; /* bits 0-11 */
Michel Thierry0b29c752017-09-13 09:56:00 +0100202 desc |= i915_ggtt_offset(ce->state) + LRC_HEADER_PAGES * PAGE_SIZE;
Chris Wilson9021ad02016-05-24 14:53:37 +0100203 /* bits 12-31 */
Chris Wilson7069b142016-04-28 09:56:52 +0100204 desc |= (u64)ctx->hw_id << GEN8_CTX_ID_SHIFT; /* bits 32-52 */
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000205
Chris Wilson9021ad02016-05-24 14:53:37 +0100206 ce->lrc_desc = desc;
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000207}
208
Chris Wilson27606fd2017-09-16 21:44:13 +0100209static struct i915_priolist *
210lookup_priolist(struct intel_engine_cs *engine,
211 struct i915_priotree *pt,
212 int prio)
Chris Wilson08dd3e12017-09-16 21:44:12 +0100213{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300214 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100215 struct i915_priolist *p;
216 struct rb_node **parent, *rb;
217 bool first = true;
218
Mika Kuoppalab620e872017-09-22 15:43:03 +0300219 if (unlikely(execlists->no_priolist))
Chris Wilson08dd3e12017-09-16 21:44:12 +0100220 prio = I915_PRIORITY_NORMAL;
221
222find_priolist:
223 /* most positive priority is scheduled first, equal priorities fifo */
224 rb = NULL;
Mika Kuoppalab620e872017-09-22 15:43:03 +0300225 parent = &execlists->queue.rb_node;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100226 while (*parent) {
227 rb = *parent;
228 p = rb_entry(rb, typeof(*p), node);
229 if (prio > p->priority) {
230 parent = &rb->rb_left;
231 } else if (prio < p->priority) {
232 parent = &rb->rb_right;
233 first = false;
234 } else {
Chris Wilson27606fd2017-09-16 21:44:13 +0100235 return p;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100236 }
237 }
238
239 if (prio == I915_PRIORITY_NORMAL) {
Mika Kuoppalab620e872017-09-22 15:43:03 +0300240 p = &execlists->default_priolist;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100241 } else {
242 p = kmem_cache_alloc(engine->i915->priorities, GFP_ATOMIC);
243 /* Convert an allocation failure to a priority bump */
244 if (unlikely(!p)) {
245 prio = I915_PRIORITY_NORMAL; /* recurses just once */
246
247 /* To maintain ordering with all rendering, after an
248 * allocation failure we have to disable all scheduling.
249 * Requests will then be executed in fifo, and schedule
250 * will ensure that dependencies are emitted in fifo.
251 * There will be still some reordering with existing
252 * requests, so if userspace lied about their
253 * dependencies that reordering may be visible.
254 */
Mika Kuoppalab620e872017-09-22 15:43:03 +0300255 execlists->no_priolist = true;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100256 goto find_priolist;
257 }
258 }
259
260 p->priority = prio;
Chris Wilson27606fd2017-09-16 21:44:13 +0100261 INIT_LIST_HEAD(&p->requests);
Chris Wilson08dd3e12017-09-16 21:44:12 +0100262 rb_link_node(&p->node, rb, parent);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300263 rb_insert_color(&p->node, &execlists->queue);
Chris Wilson08dd3e12017-09-16 21:44:12 +0100264
Chris Wilson08dd3e12017-09-16 21:44:12 +0100265 if (first)
Mika Kuoppalab620e872017-09-22 15:43:03 +0300266 execlists->first = &p->node;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100267
Chris Wilson27606fd2017-09-16 21:44:13 +0100268 return ptr_pack_bits(p, first, 1);
Chris Wilson08dd3e12017-09-16 21:44:12 +0100269}
270
Chris Wilson7e4992a2017-09-28 20:38:59 +0100271static void unwind_wa_tail(struct drm_i915_gem_request *rq)
272{
273 rq->tail = intel_ring_wrap(rq->ring, rq->wa_tail - WA_TAIL_BYTES);
274 assert_ring_tail_valid(rq->ring, rq->tail);
275}
276
Michał Winiarskia4598d12017-10-25 22:00:18 +0200277static void __unwind_incomplete_requests(struct intel_engine_cs *engine)
Chris Wilson7e4992a2017-09-28 20:38:59 +0100278{
279 struct drm_i915_gem_request *rq, *rn;
Michał Winiarski097a9482017-09-28 20:39:01 +0100280 struct i915_priolist *uninitialized_var(p);
281 int last_prio = I915_PRIORITY_INVALID;
Chris Wilson7e4992a2017-09-28 20:38:59 +0100282
283 lockdep_assert_held(&engine->timeline->lock);
284
285 list_for_each_entry_safe_reverse(rq, rn,
286 &engine->timeline->requests,
287 link) {
Chris Wilson7e4992a2017-09-28 20:38:59 +0100288 if (i915_gem_request_completed(rq))
289 return;
290
291 __i915_gem_request_unsubmit(rq);
292 unwind_wa_tail(rq);
293
Michał Winiarski097a9482017-09-28 20:39:01 +0100294 GEM_BUG_ON(rq->priotree.priority == I915_PRIORITY_INVALID);
295 if (rq->priotree.priority != last_prio) {
296 p = lookup_priolist(engine,
297 &rq->priotree,
298 rq->priotree.priority);
299 p = ptr_mask_bits(p, 1);
300
301 last_prio = rq->priotree.priority;
302 }
303
304 list_add(&rq->priotree.link, &p->requests);
Chris Wilson7e4992a2017-09-28 20:38:59 +0100305 }
306}
307
Michał Winiarskic41937fd2017-10-26 15:35:58 +0200308void
Michał Winiarskia4598d12017-10-25 22:00:18 +0200309execlists_unwind_incomplete_requests(struct intel_engine_execlists *execlists)
310{
311 struct intel_engine_cs *engine =
312 container_of(execlists, typeof(*engine), execlists);
313
314 spin_lock_irq(&engine->timeline->lock);
315 __unwind_incomplete_requests(engine);
316 spin_unlock_irq(&engine->timeline->lock);
317}
318
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100319static inline void
320execlists_context_status_change(struct drm_i915_gem_request *rq,
321 unsigned long status)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100322{
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100323 /*
324 * Only used when GVT-g is enabled now. When GVT-g is disabled,
325 * The compiler should eliminate this function as dead-code.
326 */
327 if (!IS_ENABLED(CONFIG_DRM_I915_GVT))
328 return;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100329
Changbin Du3fc03062017-03-13 10:47:11 +0800330 atomic_notifier_call_chain(&rq->engine->context_status_notifier,
331 status, rq);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100332}
333
Tvrtko Ursulin73fd9d32017-11-21 18:18:47 +0000334static inline void
335execlists_context_schedule_in(struct drm_i915_gem_request *rq)
336{
337 execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_IN);
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +0000338 intel_engine_context_in(rq->engine);
Tvrtko Ursulin73fd9d32017-11-21 18:18:47 +0000339}
340
341static inline void
342execlists_context_schedule_out(struct drm_i915_gem_request *rq)
343{
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +0000344 intel_engine_context_out(rq->engine);
Tvrtko Ursulin73fd9d32017-11-21 18:18:47 +0000345 execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_OUT);
346}
347
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000348static void
349execlists_update_context_pdps(struct i915_hw_ppgtt *ppgtt, u32 *reg_state)
350{
351 ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
352 ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
353 ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
354 ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
355}
356
Chris Wilson70c2a242016-09-09 14:11:46 +0100357static u64 execlists_update_context(struct drm_i915_gem_request *rq)
Oscar Mateoae1250b2014-07-24 17:04:37 +0100358{
Chris Wilson70c2a242016-09-09 14:11:46 +0100359 struct intel_context *ce = &rq->ctx->engine[rq->engine->id];
Zhi Wang04da8112017-02-06 18:37:16 +0800360 struct i915_hw_ppgtt *ppgtt =
361 rq->ctx->ppgtt ?: rq->i915->mm.aliasing_ppgtt;
Chris Wilson70c2a242016-09-09 14:11:46 +0100362 u32 *reg_state = ce->lrc_reg_state;
Oscar Mateoae1250b2014-07-24 17:04:37 +0100363
Chris Wilsone6ba9992017-04-25 14:00:49 +0100364 reg_state[CTX_RING_TAIL+1] = intel_ring_set_tail(rq->ring, rq->tail);
Oscar Mateoae1250b2014-07-24 17:04:37 +0100365
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000366 /* True 32b PPGTT with dynamic page allocation: update PDP
367 * registers and point the unallocated PDPs to scratch page.
368 * PML4 is allocated during ppgtt init, so this is not needed
369 * in 48-bit mode.
370 */
Chris Wilson949e8ab2017-02-09 14:40:36 +0000371 if (ppgtt && !i915_vm_is_48bit(&ppgtt->base))
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000372 execlists_update_context_pdps(ppgtt, reg_state);
Chris Wilson70c2a242016-09-09 14:11:46 +0100373
374 return ce->lrc_desc;
Oscar Mateoae1250b2014-07-24 17:04:37 +0100375}
376
Chris Wilsonbeecec92017-10-03 21:34:52 +0100377static inline void elsp_write(u64 desc, u32 __iomem *elsp)
378{
379 writel(upper_32_bits(desc), elsp);
380 writel(lower_32_bits(desc), elsp);
381}
382
Chris Wilson70c2a242016-09-09 14:11:46 +0100383static void execlists_submit_ports(struct intel_engine_cs *engine)
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100384{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300385 struct execlist_port *port = engine->execlists.port;
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100386 unsigned int n;
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100387
Mika Kuoppala76e70082017-09-22 15:43:07 +0300388 for (n = execlists_num_ports(&engine->execlists); n--; ) {
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100389 struct drm_i915_gem_request *rq;
390 unsigned int count;
391 u64 desc;
Chris Wilson70c2a242016-09-09 14:11:46 +0100392
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100393 rq = port_unpack(&port[n], &count);
394 if (rq) {
395 GEM_BUG_ON(count > !n);
396 if (!count++)
Tvrtko Ursulin73fd9d32017-11-21 18:18:47 +0000397 execlists_context_schedule_in(rq);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100398 port_set(&port[n], port_pack(rq, count));
399 desc = execlists_update_context(rq);
400 GEM_DEBUG_EXEC(port[n].context_id = upper_32_bits(desc));
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000401
402 GEM_TRACE("%s in[%d]: ctx=%d.%d, seqno=%x\n",
403 engine->name, n,
Chris Wilson16c86192017-12-19 22:09:16 +0000404 port[n].context_id, count,
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000405 rq->global_seqno);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100406 } else {
407 GEM_BUG_ON(!n);
408 desc = 0;
409 }
410
Chris Wilson2fc7a062017-12-07 22:24:34 +0000411 elsp_write(desc, engine->execlists.elsp);
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100412 }
Michel Thierryba74cb12017-11-20 12:34:58 +0000413 execlists_clear_active(&engine->execlists, EXECLISTS_ACTIVE_HWACK);
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100414}
415
Chris Wilson70c2a242016-09-09 14:11:46 +0100416static bool ctx_single_port_submission(const struct i915_gem_context *ctx)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100417{
Chris Wilson70c2a242016-09-09 14:11:46 +0100418 return (IS_ENABLED(CONFIG_DRM_I915_GVT) &&
Chris Wilson60958682016-12-31 11:20:11 +0000419 i915_gem_context_force_single_submission(ctx));
Ben Widawsky84b790f2014-07-24 17:04:36 +0100420}
421
Chris Wilson70c2a242016-09-09 14:11:46 +0100422static bool can_merge_ctx(const struct i915_gem_context *prev,
423 const struct i915_gem_context *next)
Michel Thierryacdd8842014-07-24 17:04:38 +0100424{
Chris Wilson70c2a242016-09-09 14:11:46 +0100425 if (prev != next)
426 return false;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100427
Chris Wilson70c2a242016-09-09 14:11:46 +0100428 if (ctx_single_port_submission(prev))
429 return false;
Michel Thierryacdd8842014-07-24 17:04:38 +0100430
Chris Wilson70c2a242016-09-09 14:11:46 +0100431 return true;
432}
Peter Antoine779949f2015-05-11 16:03:27 +0100433
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100434static void port_assign(struct execlist_port *port,
435 struct drm_i915_gem_request *rq)
436{
437 GEM_BUG_ON(rq == port_request(port));
438
439 if (port_isset(port))
440 i915_gem_request_put(port_request(port));
441
442 port_set(port, port_pack(i915_gem_request_get(rq), port_count(port)));
443}
444
Chris Wilsonbeecec92017-10-03 21:34:52 +0100445static void inject_preempt_context(struct intel_engine_cs *engine)
446{
447 struct intel_context *ce =
448 &engine->i915->preempt_context->engine[engine->id];
Chris Wilsonbeecec92017-10-03 21:34:52 +0100449 unsigned int n;
450
451 GEM_BUG_ON(engine->i915->preempt_context->hw_id != PREEMPT_ID);
452 GEM_BUG_ON(!IS_ALIGNED(ce->ring->size, WA_TAIL_BYTES));
453
454 memset(ce->ring->vaddr + ce->ring->tail, 0, WA_TAIL_BYTES);
455 ce->ring->tail += WA_TAIL_BYTES;
456 ce->ring->tail &= (ce->ring->size - 1);
457 ce->lrc_reg_state[CTX_RING_TAIL+1] = ce->ring->tail;
458
Chris Wilson09b1a4e2018-01-25 11:24:42 +0000459 GEM_BUG_ON((ce->lrc_reg_state[CTX_CONTEXT_CONTROL + 1] &
460 _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
461 CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT)) !=
462 _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
463 CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT));
464
Chris Wilson16a87392017-12-20 09:06:26 +0000465 GEM_TRACE("%s\n", engine->name);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100466 for (n = execlists_num_ports(&engine->execlists); --n; )
Chris Wilson2fc7a062017-12-07 22:24:34 +0000467 elsp_write(0, engine->execlists.elsp);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100468
Chris Wilson2fc7a062017-12-07 22:24:34 +0000469 elsp_write(ce->lrc_desc, engine->execlists.elsp);
Michel Thierryba74cb12017-11-20 12:34:58 +0000470 execlists_clear_active(&engine->execlists, EXECLISTS_ACTIVE_HWACK);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100471}
472
Chris Wilson70c2a242016-09-09 14:11:46 +0100473static void execlists_dequeue(struct intel_engine_cs *engine)
474{
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300475 struct intel_engine_execlists * const execlists = &engine->execlists;
476 struct execlist_port *port = execlists->port;
Mika Kuoppala76e70082017-09-22 15:43:07 +0300477 const struct execlist_port * const last_port =
478 &execlists->port[execlists->port_mask];
Chris Wilsonbeecec92017-10-03 21:34:52 +0100479 struct drm_i915_gem_request *last = port_request(port);
Chris Wilson20311bd2016-11-14 20:41:03 +0000480 struct rb_node *rb;
Chris Wilson70c2a242016-09-09 14:11:46 +0100481 bool submit = false;
Michel Thierryacdd8842014-07-24 17:04:38 +0100482
Chris Wilson70c2a242016-09-09 14:11:46 +0100483 /* Hardware submission is through 2 ports. Conceptually each port
484 * has a (RING_START, RING_HEAD, RING_TAIL) tuple. RING_START is
485 * static for a context, and unique to each, so we only execute
486 * requests belonging to a single context from each ring. RING_HEAD
487 * is maintained by the CS in the context image, it marks the place
488 * where it got up to last time, and through RING_TAIL we tell the CS
489 * where we want to execute up to this time.
490 *
491 * In this list the requests are in order of execution. Consecutive
492 * requests from the same context are adjacent in the ringbuffer. We
493 * can combine these requests into a single RING_TAIL update:
494 *
495 * RING_HEAD...req1...req2
496 * ^- RING_TAIL
497 * since to execute req2 the CS must first execute req1.
498 *
499 * Our goal then is to point each port to the end of a consecutive
500 * sequence of requests as being the most optimal (fewest wake ups
501 * and context switches) submission.
502 */
503
Tvrtko Ursulin9f7886d2017-03-21 10:55:11 +0000504 spin_lock_irq(&engine->timeline->lock);
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300505 rb = execlists->first;
506 GEM_BUG_ON(rb_first(&execlists->queue) != rb);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100507 if (!rb)
508 goto unlock;
509
510 if (last) {
511 /*
512 * Don't resubmit or switch until all outstanding
513 * preemptions (lite-restore) are seen. Then we
514 * know the next preemption status we see corresponds
515 * to this ELSP update.
516 */
Michel Thierryba74cb12017-11-20 12:34:58 +0000517 GEM_BUG_ON(!port_count(&port[0]));
Chris Wilsonbeecec92017-10-03 21:34:52 +0100518 if (port_count(&port[0]) > 1)
519 goto unlock;
520
Michel Thierryba74cb12017-11-20 12:34:58 +0000521 /*
522 * If we write to ELSP a second time before the HW has had
523 * a chance to respond to the previous write, we can confuse
524 * the HW and hit "undefined behaviour". After writing to ELSP,
525 * we must then wait until we see a context-switch event from
526 * the HW to indicate that it has had a chance to respond.
527 */
528 if (!execlists_is_active(execlists, EXECLISTS_ACTIVE_HWACK))
529 goto unlock;
530
Michał Winiarskia4598d12017-10-25 22:00:18 +0200531 if (HAS_LOGICAL_RING_PREEMPTION(engine->i915) &&
Chris Wilsonbeecec92017-10-03 21:34:52 +0100532 rb_entry(rb, struct i915_priolist, node)->priority >
533 max(last->priotree.priority, 0)) {
534 /*
535 * Switch to our empty preempt context so
536 * the state of the GPU is known (idle).
537 */
538 inject_preempt_context(engine);
Chris Wilson4a118ec2017-10-23 22:32:36 +0100539 execlists_set_active(execlists,
540 EXECLISTS_ACTIVE_PREEMPT);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100541 goto unlock;
542 } else {
543 /*
544 * In theory, we could coalesce more requests onto
545 * the second port (the first port is active, with
546 * no preemptions pending). However, that means we
547 * then have to deal with the possible lite-restore
548 * of the second port (as we submit the ELSP, there
549 * may be a context-switch) but also we may complete
550 * the resubmission before the context-switch. Ergo,
551 * coalescing onto the second port will cause a
552 * preemption event, but we cannot predict whether
553 * that will affect port[0] or port[1].
554 *
555 * If the second port is already active, we can wait
556 * until the next context-switch before contemplating
557 * new requests. The GPU will be busy and we should be
558 * able to resubmit the new ELSP before it idles,
559 * avoiding pipeline bubbles (momentary pauses where
560 * the driver is unable to keep up the supply of new
561 * work).
562 */
563 if (port_count(&port[1]))
564 goto unlock;
565
566 /* WaIdleLiteRestore:bdw,skl
567 * Apply the wa NOOPs to prevent
568 * ring:HEAD == req:TAIL as we resubmit the
569 * request. See gen8_emit_breadcrumb() for
570 * where we prepare the padding after the
571 * end of the request.
572 */
573 last->tail = last->wa_tail;
574 }
575 }
576
577 do {
Chris Wilson6c067572017-05-17 13:10:03 +0100578 struct i915_priolist *p = rb_entry(rb, typeof(*p), node);
579 struct drm_i915_gem_request *rq, *rn;
Chris Wilson20311bd2016-11-14 20:41:03 +0000580
Chris Wilson6c067572017-05-17 13:10:03 +0100581 list_for_each_entry_safe(rq, rn, &p->requests, priotree.link) {
582 /*
583 * Can we combine this request with the current port?
584 * It has to be the same context/ringbuffer and not
585 * have any exceptions (e.g. GVT saying never to
586 * combine contexts).
587 *
588 * If we can combine the requests, we can execute both
589 * by updating the RING_TAIL to point to the end of the
590 * second request, and so we never need to tell the
591 * hardware about the first.
Chris Wilson70c2a242016-09-09 14:11:46 +0100592 */
Chris Wilson6c067572017-05-17 13:10:03 +0100593 if (last && !can_merge_ctx(rq->ctx, last->ctx)) {
594 /*
595 * If we are on the second port and cannot
596 * combine this request with the last, then we
597 * are done.
598 */
Mika Kuoppala76e70082017-09-22 15:43:07 +0300599 if (port == last_port) {
Chris Wilson6c067572017-05-17 13:10:03 +0100600 __list_del_many(&p->requests,
601 &rq->priotree.link);
602 goto done;
603 }
Chris Wilson70c2a242016-09-09 14:11:46 +0100604
Chris Wilson6c067572017-05-17 13:10:03 +0100605 /*
606 * If GVT overrides us we only ever submit
607 * port[0], leaving port[1] empty. Note that we
608 * also have to be careful that we don't queue
609 * the same context (even though a different
610 * request) to the second port.
611 */
612 if (ctx_single_port_submission(last->ctx) ||
613 ctx_single_port_submission(rq->ctx)) {
614 __list_del_many(&p->requests,
615 &rq->priotree.link);
616 goto done;
617 }
Chris Wilson70c2a242016-09-09 14:11:46 +0100618
Chris Wilson6c067572017-05-17 13:10:03 +0100619 GEM_BUG_ON(last->ctx == rq->ctx);
Chris Wilson70c2a242016-09-09 14:11:46 +0100620
Chris Wilson6c067572017-05-17 13:10:03 +0100621 if (submit)
622 port_assign(port, last);
623 port++;
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300624
625 GEM_BUG_ON(port_isset(port));
Chris Wilson6c067572017-05-17 13:10:03 +0100626 }
627
628 INIT_LIST_HEAD(&rq->priotree.link);
Chris Wilson6c067572017-05-17 13:10:03 +0100629 __i915_gem_request_submit(rq);
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300630 trace_i915_gem_request_in(rq, port_index(port, execlists));
Chris Wilson6c067572017-05-17 13:10:03 +0100631 last = rq;
632 submit = true;
Chris Wilson70c2a242016-09-09 14:11:46 +0100633 }
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000634
Chris Wilson20311bd2016-11-14 20:41:03 +0000635 rb = rb_next(rb);
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300636 rb_erase(&p->node, &execlists->queue);
Chris Wilson6c067572017-05-17 13:10:03 +0100637 INIT_LIST_HEAD(&p->requests);
638 if (p->priority != I915_PRIORITY_NORMAL)
Chris Wilsonc5cf9a92017-05-17 13:10:04 +0100639 kmem_cache_free(engine->i915->priorities, p);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100640 } while (rb);
Chris Wilson6c067572017-05-17 13:10:03 +0100641done:
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300642 execlists->first = rb;
Chris Wilson6c067572017-05-17 13:10:03 +0100643 if (submit)
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100644 port_assign(port, last);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100645unlock:
Tvrtko Ursulin9f7886d2017-03-21 10:55:11 +0000646 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson70c2a242016-09-09 14:11:46 +0100647
Chris Wilson4a118ec2017-10-23 22:32:36 +0100648 if (submit) {
649 execlists_set_active(execlists, EXECLISTS_ACTIVE_USER);
Chris Wilson70c2a242016-09-09 14:11:46 +0100650 execlists_submit_ports(engine);
Chris Wilson4a118ec2017-10-23 22:32:36 +0100651 }
Michel Thierryacdd8842014-07-24 17:04:38 +0100652}
653
Michał Winiarskic41937fd2017-10-26 15:35:58 +0200654void
Michał Winiarskia4598d12017-10-25 22:00:18 +0200655execlists_cancel_port_requests(struct intel_engine_execlists * const execlists)
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300656{
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100657 struct execlist_port *port = execlists->port;
Mika Kuoppaladc2279e2017-10-10 14:48:57 +0300658 unsigned int num_ports = execlists_num_ports(execlists);
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300659
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100660 while (num_ports-- && port_isset(port)) {
Chris Wilson7e44fc22017-09-26 11:17:19 +0100661 struct drm_i915_gem_request *rq = port_request(port);
662
Chris Wilson4a118ec2017-10-23 22:32:36 +0100663 GEM_BUG_ON(!execlists->active);
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +0000664 intel_engine_context_out(rq->engine);
Chris Wilsond6c05112017-10-03 21:34:47 +0100665 execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_PREEMPTED);
Chris Wilson7e44fc22017-09-26 11:17:19 +0100666 i915_gem_request_put(rq);
667
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100668 memset(port, 0, sizeof(*port));
669 port++;
670 }
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300671}
672
Chris Wilson27a5f612017-09-15 18:31:00 +0100673static void execlists_cancel_requests(struct intel_engine_cs *engine)
674{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300675 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilson27a5f612017-09-15 18:31:00 +0100676 struct drm_i915_gem_request *rq, *rn;
677 struct rb_node *rb;
678 unsigned long flags;
Chris Wilson27a5f612017-09-15 18:31:00 +0100679
680 spin_lock_irqsave(&engine->timeline->lock, flags);
681
682 /* Cancel the requests on the HW and clear the ELSP tracker. */
Michał Winiarskia4598d12017-10-25 22:00:18 +0200683 execlists_cancel_port_requests(execlists);
Chris Wilson27a5f612017-09-15 18:31:00 +0100684
685 /* Mark all executing requests as skipped. */
686 list_for_each_entry(rq, &engine->timeline->requests, link) {
687 GEM_BUG_ON(!rq->global_seqno);
688 if (!i915_gem_request_completed(rq))
689 dma_fence_set_error(&rq->fence, -EIO);
690 }
691
692 /* Flush the queued requests to the timeline list (for retiring). */
Mika Kuoppalab620e872017-09-22 15:43:03 +0300693 rb = execlists->first;
Chris Wilson27a5f612017-09-15 18:31:00 +0100694 while (rb) {
695 struct i915_priolist *p = rb_entry(rb, typeof(*p), node);
696
697 list_for_each_entry_safe(rq, rn, &p->requests, priotree.link) {
698 INIT_LIST_HEAD(&rq->priotree.link);
Chris Wilson27a5f612017-09-15 18:31:00 +0100699
700 dma_fence_set_error(&rq->fence, -EIO);
701 __i915_gem_request_submit(rq);
702 }
703
704 rb = rb_next(rb);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300705 rb_erase(&p->node, &execlists->queue);
Chris Wilson27a5f612017-09-15 18:31:00 +0100706 INIT_LIST_HEAD(&p->requests);
707 if (p->priority != I915_PRIORITY_NORMAL)
708 kmem_cache_free(engine->i915->priorities, p);
709 }
710
711 /* Remaining _unready_ requests will be nop'ed when submitted */
712
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300713
Mika Kuoppalab620e872017-09-22 15:43:03 +0300714 execlists->queue = RB_ROOT;
715 execlists->first = NULL;
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100716 GEM_BUG_ON(port_isset(execlists->port));
Chris Wilson27a5f612017-09-15 18:31:00 +0100717
718 /*
719 * The port is checked prior to scheduling a tasklet, but
720 * just in case we have suspended the tasklet to do the
721 * wedging make sure that when it wakes, it decides there
722 * is no work to do by clearing the irq_posted bit.
723 */
724 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
725
726 spin_unlock_irqrestore(&engine->timeline->lock, flags);
727}
728
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200729/*
Oscar Mateo73e4d072014-07-24 17:04:48 +0100730 * Check the unread Context Status Buffers and manage the submission of new
731 * contexts to the ELSP accordingly.
732 */
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +0530733static void execlists_submission_tasklet(unsigned long data)
Thomas Daniele981e7b2014-07-24 17:04:39 +0100734{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300735 struct intel_engine_cs * const engine = (struct intel_engine_cs *)data;
736 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilsonbeecec92017-10-03 21:34:52 +0100737 struct execlist_port * const port = execlists->port;
Chris Wilsonc0336662016-05-06 15:40:21 +0100738 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonbb5db7e2018-01-22 10:07:14 +0000739 bool fw = false;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100740
Chris Wilson48921262017-04-11 18:58:50 +0100741 /* We can skip acquiring intel_runtime_pm_get() here as it was taken
742 * on our behalf by the request (see i915_gem_mark_busy()) and it will
743 * not be relinquished until the device is idle (see
744 * i915_gem_idle_work_handler()). As a precaution, we make sure
745 * that all ELSP are drained i.e. we have processed the CSB,
746 * before allowing ourselves to idle and calling intel_runtime_pm_put().
747 */
748 GEM_BUG_ON(!dev_priv->gt.awake);
749
Chris Wilson899f6202017-03-21 11:33:20 +0000750 /* Prefer doing test_and_clear_bit() as a two stage operation to avoid
751 * imposing the cost of a locked atomic transaction when submitting a
752 * new request (outside of the context-switch interrupt).
753 */
754 while (test_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted)) {
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100755 /* The HWSP contains a (cacheable) mirror of the CSB */
756 const u32 *buf =
757 &engine->status_page.page_addr[I915_HWS_CSB_BUF0_INDEX];
Chris Wilson4af0d722017-03-25 20:10:53 +0000758 unsigned int head, tail;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100759
Mika Kuoppalab620e872017-09-22 15:43:03 +0300760 if (unlikely(execlists->csb_use_mmio)) {
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100761 buf = (u32 * __force)
762 (dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_BUF_LO(engine, 0)));
Mika Kuoppalab620e872017-09-22 15:43:03 +0300763 execlists->csb_head = -1; /* force mmio read of CSB ptrs */
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100764 }
765
Chris Wilson2e70b8c2017-03-23 13:48:03 +0000766 /* The write will be ordered by the uncached read (itself
767 * a memory barrier), so we do not need another in the form
768 * of a locked instruction. The race between the interrupt
769 * handler and the split test/clear is harmless as we order
770 * our clear before the CSB read. If the interrupt arrived
771 * first between the test and the clear, we read the updated
772 * CSB and clear the bit. If the interrupt arrives as we read
773 * the CSB or later (i.e. after we had cleared the bit) the bit
774 * is set and we do a new loop.
775 */
776 __clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300777 if (unlikely(execlists->csb_head == -1)) { /* following a reset */
Chris Wilsonbb5db7e2018-01-22 10:07:14 +0000778 if (!fw) {
779 intel_uncore_forcewake_get(dev_priv,
780 execlists->fw_domains);
781 fw = true;
782 }
783
Chris Wilson767a9832017-09-13 09:56:05 +0100784 head = readl(dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)));
785 tail = GEN8_CSB_WRITE_PTR(head);
786 head = GEN8_CSB_READ_PTR(head);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300787 execlists->csb_head = head;
Chris Wilson767a9832017-09-13 09:56:05 +0100788 } else {
789 const int write_idx =
790 intel_hws_csb_write_index(dev_priv) -
791 I915_HWS_CSB_BUF0_INDEX;
792
Mika Kuoppalab620e872017-09-22 15:43:03 +0300793 head = execlists->csb_head;
Chris Wilson767a9832017-09-13 09:56:05 +0100794 tail = READ_ONCE(buf[write_idx]);
795 }
Chris Wilsonbb5db7e2018-01-22 10:07:14 +0000796 GEM_TRACE("%s cs-irq head=%d [%d%s], tail=%d [%d%s]\n",
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000797 engine->name,
Chris Wilsonbb5db7e2018-01-22 10:07:14 +0000798 head, GEN8_CSB_READ_PTR(readl(dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)))), fw ? "" : "?",
799 tail, GEN8_CSB_WRITE_PTR(readl(dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)))), fw ? "" : "?");
Mika Kuoppalab620e872017-09-22 15:43:03 +0300800
Chris Wilson4af0d722017-03-25 20:10:53 +0000801 while (head != tail) {
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100802 struct drm_i915_gem_request *rq;
Chris Wilson4af0d722017-03-25 20:10:53 +0000803 unsigned int status;
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100804 unsigned int count;
Chris Wilsona37951a2017-01-24 11:00:06 +0000805
Chris Wilson4af0d722017-03-25 20:10:53 +0000806 if (++head == GEN8_CSB_ENTRIES)
807 head = 0;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100808
Chris Wilson2ffe80a2017-02-06 17:05:02 +0000809 /* We are flying near dragons again.
810 *
811 * We hold a reference to the request in execlist_port[]
812 * but no more than that. We are operating in softirq
813 * context and so cannot hold any mutex or sleep. That
814 * prevents us stopping the requests we are processing
815 * in port[] from being retired simultaneously (the
816 * breadcrumb will be complete before we see the
817 * context-switch). As we only hold the reference to the
818 * request, any pointer chasing underneath the request
819 * is subject to a potential use-after-free. Thus we
820 * store all of the bookkeeping within port[] as
821 * required, and avoid using unguarded pointers beneath
822 * request itself. The same applies to the atomic
823 * status notifier.
824 */
825
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100826 status = READ_ONCE(buf[2 * head]); /* maybe mmio! */
Chris Wilson193a98d2017-12-22 13:27:42 +0000827 GEM_TRACE("%s csb[%d]: status=0x%08x:0x%08x, active=0x%x\n",
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000828 engine->name, head,
Chris Wilson193a98d2017-12-22 13:27:42 +0000829 status, buf[2*head + 1],
830 execlists->active);
Michel Thierryba74cb12017-11-20 12:34:58 +0000831
832 if (status & (GEN8_CTX_STATUS_IDLE_ACTIVE |
833 GEN8_CTX_STATUS_PREEMPTED))
834 execlists_set_active(execlists,
835 EXECLISTS_ACTIVE_HWACK);
836 if (status & GEN8_CTX_STATUS_ACTIVE_IDLE)
837 execlists_clear_active(execlists,
838 EXECLISTS_ACTIVE_HWACK);
839
Chris Wilson70c2a242016-09-09 14:11:46 +0100840 if (!(status & GEN8_CTX_STATUS_COMPLETED_MASK))
841 continue;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100842
Chris Wilson1f5f9ed2017-11-20 12:34:57 +0000843 /* We should never get a COMPLETED | IDLE_ACTIVE! */
844 GEM_BUG_ON(status & GEN8_CTX_STATUS_IDLE_ACTIVE);
845
Chris Wilsone40dd222017-11-20 12:34:55 +0000846 if (status & GEN8_CTX_STATUS_COMPLETE &&
Chris Wilsonbeecec92017-10-03 21:34:52 +0100847 buf[2*head + 1] == PREEMPT_ID) {
Chris Wilson193a98d2017-12-22 13:27:42 +0000848 GEM_TRACE("%s preempt-idle\n", engine->name);
849
Michał Winiarskia4598d12017-10-25 22:00:18 +0200850 execlists_cancel_port_requests(execlists);
851 execlists_unwind_incomplete_requests(execlists);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100852
Chris Wilson4a118ec2017-10-23 22:32:36 +0100853 GEM_BUG_ON(!execlists_is_active(execlists,
854 EXECLISTS_ACTIVE_PREEMPT));
855 execlists_clear_active(execlists,
856 EXECLISTS_ACTIVE_PREEMPT);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100857 continue;
858 }
859
860 if (status & GEN8_CTX_STATUS_PREEMPTED &&
Chris Wilson4a118ec2017-10-23 22:32:36 +0100861 execlists_is_active(execlists,
862 EXECLISTS_ACTIVE_PREEMPT))
Chris Wilsonbeecec92017-10-03 21:34:52 +0100863 continue;
864
Chris Wilson4a118ec2017-10-23 22:32:36 +0100865 GEM_BUG_ON(!execlists_is_active(execlists,
866 EXECLISTS_ACTIVE_USER));
867
Chris Wilson86aa7e72017-01-23 11:31:32 +0000868 /* Check the context/desc id for this event matches */
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100869 GEM_DEBUG_BUG_ON(buf[2 * head + 1] != port->context_id);
Chris Wilson86aa7e72017-01-23 11:31:32 +0000870
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100871 rq = port_unpack(port, &count);
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000872 GEM_TRACE("%s out[0]: ctx=%d.%d, seqno=%x\n",
873 engine->name,
Chris Wilson16c86192017-12-19 22:09:16 +0000874 port->context_id, count,
Chris Wilson16a87392017-12-20 09:06:26 +0000875 rq ? rq->global_seqno : 0);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100876 GEM_BUG_ON(count == 0);
877 if (--count == 0) {
Chris Wilson70c2a242016-09-09 14:11:46 +0100878 GEM_BUG_ON(status & GEN8_CTX_STATUS_PREEMPTED);
Chris Wilsond8747af2017-11-20 12:34:56 +0000879 GEM_BUG_ON(port_isset(&port[1]) &&
880 !(status & GEN8_CTX_STATUS_ELEMENT_SWITCH));
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100881 GEM_BUG_ON(!i915_gem_request_completed(rq));
Tvrtko Ursulin73fd9d32017-11-21 18:18:47 +0000882 execlists_context_schedule_out(rq);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100883 trace_i915_gem_request_out(rq);
884 i915_gem_request_put(rq);
885
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300886 execlists_port_complete(execlists, port);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100887 } else {
888 port_set(port, port_pack(rq, count));
Chris Wilson70c2a242016-09-09 14:11:46 +0100889 }
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000890
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100891 /* After the final element, the hw should be idle */
892 GEM_BUG_ON(port_count(port) == 0 &&
Chris Wilson70c2a242016-09-09 14:11:46 +0100893 !(status & GEN8_CTX_STATUS_ACTIVE_IDLE));
Chris Wilson4a118ec2017-10-23 22:32:36 +0100894 if (port_count(port) == 0)
895 execlists_clear_active(execlists,
896 EXECLISTS_ACTIVE_USER);
Chris Wilson4af0d722017-03-25 20:10:53 +0000897 }
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000898
Mika Kuoppalab620e872017-09-22 15:43:03 +0300899 if (head != execlists->csb_head) {
900 execlists->csb_head = head;
Chris Wilson767a9832017-09-13 09:56:05 +0100901 writel(_MASKED_FIELD(GEN8_CSB_READ_PTR_MASK, head << 8),
902 dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)));
903 }
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000904 }
905
Chris Wilson4a118ec2017-10-23 22:32:36 +0100906 if (!execlists_is_active(execlists, EXECLISTS_ACTIVE_PREEMPT))
Chris Wilson70c2a242016-09-09 14:11:46 +0100907 execlists_dequeue(engine);
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000908
Chris Wilsonbb5db7e2018-01-22 10:07:14 +0000909 if (fw)
910 intel_uncore_forcewake_put(dev_priv, execlists->fw_domains);
Thomas Daniele981e7b2014-07-24 17:04:39 +0100911}
912
Chris Wilson27606fd2017-09-16 21:44:13 +0100913static void insert_request(struct intel_engine_cs *engine,
914 struct i915_priotree *pt,
915 int prio)
916{
917 struct i915_priolist *p = lookup_priolist(engine, pt, prio);
918
919 list_add_tail(&pt->link, &ptr_mask_bits(p, 1)->requests);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100920 if (ptr_unmask_bits(p, 1))
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +0530921 tasklet_hi_schedule(&engine->execlists.tasklet);
Chris Wilson27606fd2017-09-16 21:44:13 +0100922}
923
Chris Wilsonf4ea6bd2016-08-02 22:50:32 +0100924static void execlists_submit_request(struct drm_i915_gem_request *request)
Michel Thierryacdd8842014-07-24 17:04:38 +0100925{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000926 struct intel_engine_cs *engine = request->engine;
Chris Wilson5590af32016-09-09 14:11:54 +0100927 unsigned long flags;
Michel Thierryacdd8842014-07-24 17:04:38 +0100928
Chris Wilson663f71e2016-11-14 20:41:00 +0000929 /* Will be called from irq-context when using foreign fences. */
930 spin_lock_irqsave(&engine->timeline->lock, flags);
Michel Thierryacdd8842014-07-24 17:04:38 +0100931
Chris Wilson27606fd2017-09-16 21:44:13 +0100932 insert_request(engine, &request->priotree, request->priotree.priority);
Michel Thierryacdd8842014-07-24 17:04:38 +0100933
Mika Kuoppalab620e872017-09-22 15:43:03 +0300934 GEM_BUG_ON(!engine->execlists.first);
Chris Wilson6c067572017-05-17 13:10:03 +0100935 GEM_BUG_ON(list_empty(&request->priotree.link));
936
Chris Wilson663f71e2016-11-14 20:41:00 +0000937 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Michel Thierryacdd8842014-07-24 17:04:38 +0100938}
939
Chris Wilson1f181222017-10-03 21:34:50 +0100940static struct drm_i915_gem_request *pt_to_request(struct i915_priotree *pt)
941{
942 return container_of(pt, struct drm_i915_gem_request, priotree);
943}
944
Chris Wilson20311bd2016-11-14 20:41:03 +0000945static struct intel_engine_cs *
946pt_lock_engine(struct i915_priotree *pt, struct intel_engine_cs *locked)
947{
Chris Wilson1f181222017-10-03 21:34:50 +0100948 struct intel_engine_cs *engine = pt_to_request(pt)->engine;
Chris Wilson20311bd2016-11-14 20:41:03 +0000949
Chris Wilsona79a5242017-03-27 21:21:43 +0100950 GEM_BUG_ON(!locked);
951
Chris Wilson20311bd2016-11-14 20:41:03 +0000952 if (engine != locked) {
Chris Wilsona79a5242017-03-27 21:21:43 +0100953 spin_unlock(&locked->timeline->lock);
954 spin_lock(&engine->timeline->lock);
Chris Wilson20311bd2016-11-14 20:41:03 +0000955 }
956
957 return engine;
958}
959
960static void execlists_schedule(struct drm_i915_gem_request *request, int prio)
961{
Chris Wilsona79a5242017-03-27 21:21:43 +0100962 struct intel_engine_cs *engine;
Chris Wilson20311bd2016-11-14 20:41:03 +0000963 struct i915_dependency *dep, *p;
964 struct i915_dependency stack;
965 LIST_HEAD(dfs);
966
Chris Wilson7d1ea602017-09-28 20:39:00 +0100967 GEM_BUG_ON(prio == I915_PRIORITY_INVALID);
968
Chris Wilsonc218ee02018-01-06 10:56:18 +0000969 if (i915_gem_request_completed(request))
970 return;
971
Chris Wilson20311bd2016-11-14 20:41:03 +0000972 if (prio <= READ_ONCE(request->priotree.priority))
973 return;
974
Chris Wilson70cd1472016-11-28 14:36:49 +0000975 /* Need BKL in order to use the temporary link inside i915_dependency */
976 lockdep_assert_held(&request->i915->drm.struct_mutex);
Chris Wilson20311bd2016-11-14 20:41:03 +0000977
978 stack.signaler = &request->priotree;
979 list_add(&stack.dfs_link, &dfs);
980
Chris Wilsonce01b172018-01-02 15:12:26 +0000981 /*
982 * Recursively bump all dependent priorities to match the new request.
Chris Wilson20311bd2016-11-14 20:41:03 +0000983 *
984 * A naive approach would be to use recursion:
985 * static void update_priorities(struct i915_priotree *pt, prio) {
986 * list_for_each_entry(dep, &pt->signalers_list, signal_link)
987 * update_priorities(dep->signal, prio)
988 * insert_request(pt);
989 * }
990 * but that may have unlimited recursion depth and so runs a very
991 * real risk of overunning the kernel stack. Instead, we build
992 * a flat list of all dependencies starting with the current request.
993 * As we walk the list of dependencies, we add all of its dependencies
994 * to the end of the list (this may include an already visited
995 * request) and continue to walk onwards onto the new dependencies. The
996 * end result is a topological list of requests in reverse order, the
997 * last element in the list is the request we must execute first.
998 */
Chris Wilson2221c5b2018-01-02 15:12:27 +0000999 list_for_each_entry(dep, &dfs, dfs_link) {
Chris Wilson20311bd2016-11-14 20:41:03 +00001000 struct i915_priotree *pt = dep->signaler;
1001
Chris Wilsonce01b172018-01-02 15:12:26 +00001002 /*
1003 * Within an engine, there can be no cycle, but we may
Chris Wilsona79a5242017-03-27 21:21:43 +01001004 * refer to the same dependency chain multiple times
1005 * (redundant dependencies are not eliminated) and across
1006 * engines.
1007 */
1008 list_for_each_entry(p, &pt->signalers_list, signal_link) {
Chris Wilsonce01b172018-01-02 15:12:26 +00001009 GEM_BUG_ON(p == dep); /* no cycles! */
1010
Chris Wilson83cc84c2018-01-02 15:12:25 +00001011 if (i915_priotree_signaled(p->signaler))
Chris Wilson1f181222017-10-03 21:34:50 +01001012 continue;
1013
Chris Wilsona79a5242017-03-27 21:21:43 +01001014 GEM_BUG_ON(p->signaler->priority < pt->priority);
Chris Wilson20311bd2016-11-14 20:41:03 +00001015 if (prio > READ_ONCE(p->signaler->priority))
1016 list_move_tail(&p->dfs_link, &dfs);
Chris Wilsona79a5242017-03-27 21:21:43 +01001017 }
Chris Wilson20311bd2016-11-14 20:41:03 +00001018 }
1019
Chris Wilsonce01b172018-01-02 15:12:26 +00001020 /*
1021 * If we didn't need to bump any existing priorities, and we haven't
Chris Wilson349bdb62017-05-17 13:10:05 +01001022 * yet submitted this request (i.e. there is no potential race with
1023 * execlists_submit_request()), we can set our own priority and skip
1024 * acquiring the engine locks.
1025 */
Chris Wilson7d1ea602017-09-28 20:39:00 +01001026 if (request->priotree.priority == I915_PRIORITY_INVALID) {
Chris Wilson349bdb62017-05-17 13:10:05 +01001027 GEM_BUG_ON(!list_empty(&request->priotree.link));
1028 request->priotree.priority = prio;
1029 if (stack.dfs_link.next == stack.dfs_link.prev)
1030 return;
1031 __list_del_entry(&stack.dfs_link);
1032 }
1033
Chris Wilsona79a5242017-03-27 21:21:43 +01001034 engine = request->engine;
1035 spin_lock_irq(&engine->timeline->lock);
1036
Chris Wilson20311bd2016-11-14 20:41:03 +00001037 /* Fifo and depth-first replacement ensure our deps execute before us */
1038 list_for_each_entry_safe_reverse(dep, p, &dfs, dfs_link) {
1039 struct i915_priotree *pt = dep->signaler;
1040
1041 INIT_LIST_HEAD(&dep->dfs_link);
1042
1043 engine = pt_lock_engine(pt, engine);
1044
1045 if (prio <= pt->priority)
1046 continue;
1047
Chris Wilson20311bd2016-11-14 20:41:03 +00001048 pt->priority = prio;
Chris Wilson6c067572017-05-17 13:10:03 +01001049 if (!list_empty(&pt->link)) {
1050 __list_del_entry(&pt->link);
1051 insert_request(engine, pt, prio);
Chris Wilsona79a5242017-03-27 21:21:43 +01001052 }
Chris Wilson20311bd2016-11-14 20:41:03 +00001053 }
1054
Chris Wilsona79a5242017-03-27 21:21:43 +01001055 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson20311bd2016-11-14 20:41:03 +00001056}
1057
Chris Wilsonf4e15af2017-11-10 14:26:32 +00001058static int __context_pin(struct i915_gem_context *ctx, struct i915_vma *vma)
1059{
1060 unsigned int flags;
1061 int err;
1062
1063 /*
1064 * Clear this page out of any CPU caches for coherent swap-in/out.
1065 * We only want to do this on the first bind so that we do not stall
1066 * on an active context (which by nature is already on the GPU).
1067 */
1068 if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
1069 err = i915_gem_object_set_to_gtt_domain(vma->obj, true);
1070 if (err)
1071 return err;
1072 }
1073
1074 flags = PIN_GLOBAL | PIN_HIGH;
1075 if (ctx->ggtt_offset_bias)
1076 flags |= PIN_OFFSET_BIAS | ctx->ggtt_offset_bias;
1077
1078 return i915_vma_pin(vma, 0, GEN8_LR_CONTEXT_ALIGN, flags);
1079}
1080
Chris Wilson266a2402017-05-04 10:33:08 +01001081static struct intel_ring *
1082execlists_context_pin(struct intel_engine_cs *engine,
1083 struct i915_gem_context *ctx)
Oscar Mateodcb4c122014-11-13 10:28:10 +00001084{
Chris Wilson9021ad02016-05-24 14:53:37 +01001085 struct intel_context *ce = &ctx->engine[engine->id];
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001086 void *vaddr;
Tvrtko Ursulinca825802016-01-15 15:10:27 +00001087 int ret;
Oscar Mateodcb4c122014-11-13 10:28:10 +00001088
Chris Wilson91c8a322016-07-05 10:40:23 +01001089 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Tvrtko Ursulinca825802016-01-15 15:10:27 +00001090
Chris Wilson266a2402017-05-04 10:33:08 +01001091 if (likely(ce->pin_count++))
1092 goto out;
Chris Wilsona533b4b2017-03-16 17:16:28 +00001093 GEM_BUG_ON(!ce->pin_count); /* no overflow please! */
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001094
Chris Wilsone8a9c582016-12-18 15:37:20 +00001095 if (!ce->state) {
1096 ret = execlists_context_deferred_alloc(ctx, engine);
1097 if (ret)
1098 goto err;
1099 }
Chris Wilson56f6e0a2017-01-05 15:30:20 +00001100 GEM_BUG_ON(!ce->state);
Chris Wilsone8a9c582016-12-18 15:37:20 +00001101
Chris Wilsonf4e15af2017-11-10 14:26:32 +00001102 ret = __context_pin(ctx, ce->state);
Nick Hoathe84fe802015-09-11 12:53:46 +01001103 if (ret)
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001104 goto err;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001105
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001106 vaddr = i915_gem_object_pin_map(ce->state->obj, I915_MAP_WB);
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001107 if (IS_ERR(vaddr)) {
1108 ret = PTR_ERR(vaddr);
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001109 goto unpin_vma;
Tvrtko Ursulin82352e92016-01-15 17:12:45 +00001110 }
1111
Chris Wilsond822bb12017-04-03 12:34:25 +01001112 ret = intel_ring_pin(ce->ring, ctx->i915, ctx->ggtt_offset_bias);
Nick Hoathe84fe802015-09-11 12:53:46 +01001113 if (ret)
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001114 goto unpin_map;
Alex Daid1675192015-08-12 15:43:43 +01001115
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001116 intel_lr_context_descriptor_update(ctx, engine);
Chris Wilson9021ad02016-05-24 14:53:37 +01001117
Chris Wilsona3aabe82016-10-04 21:11:26 +01001118 ce->lrc_reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
1119 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001120 i915_ggtt_offset(ce->ring->vma);
Chris Wilsona3aabe82016-10-04 21:11:26 +01001121
Chris Wilson3d574a62017-10-13 21:26:16 +01001122 ce->state->obj->pin_global++;
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001123 i915_gem_context_get(ctx);
Chris Wilson266a2402017-05-04 10:33:08 +01001124out:
1125 return ce->ring;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001126
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001127unpin_map:
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001128 i915_gem_object_unpin_map(ce->state->obj);
1129unpin_vma:
1130 __i915_vma_unpin(ce->state);
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001131err:
Chris Wilson9021ad02016-05-24 14:53:37 +01001132 ce->pin_count = 0;
Chris Wilson266a2402017-05-04 10:33:08 +01001133 return ERR_PTR(ret);
Oscar Mateodcb4c122014-11-13 10:28:10 +00001134}
1135
Chris Wilsone8a9c582016-12-18 15:37:20 +00001136static void execlists_context_unpin(struct intel_engine_cs *engine,
1137 struct i915_gem_context *ctx)
Oscar Mateodcb4c122014-11-13 10:28:10 +00001138{
Chris Wilson9021ad02016-05-24 14:53:37 +01001139 struct intel_context *ce = &ctx->engine[engine->id];
Daniel Vetteraf3302b2015-12-04 17:27:15 +01001140
Chris Wilson91c8a322016-07-05 10:40:23 +01001141 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilson9021ad02016-05-24 14:53:37 +01001142 GEM_BUG_ON(ce->pin_count == 0);
Tvrtko Ursulin321fe302016-01-28 10:29:55 +00001143
Chris Wilson9021ad02016-05-24 14:53:37 +01001144 if (--ce->pin_count)
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001145 return;
1146
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001147 intel_ring_unpin(ce->ring);
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001148
Chris Wilson3d574a62017-10-13 21:26:16 +01001149 ce->state->obj->pin_global--;
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001150 i915_gem_object_unpin_map(ce->state->obj);
1151 i915_vma_unpin(ce->state);
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001152
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001153 i915_gem_context_put(ctx);
Oscar Mateodcb4c122014-11-13 10:28:10 +00001154}
1155
Chris Wilsonf73e7392016-12-18 15:37:24 +00001156static int execlists_request_alloc(struct drm_i915_gem_request *request)
Chris Wilsonef11c012016-12-18 15:37:19 +00001157{
1158 struct intel_engine_cs *engine = request->engine;
1159 struct intel_context *ce = &request->ctx->engine[engine->id];
Chris Wilsonfd138212017-11-15 15:12:04 +00001160 int ret;
Chris Wilsonef11c012016-12-18 15:37:19 +00001161
Chris Wilsone8a9c582016-12-18 15:37:20 +00001162 GEM_BUG_ON(!ce->pin_count);
1163
Chris Wilsonef11c012016-12-18 15:37:19 +00001164 /* Flush enough space to reduce the likelihood of waiting after
1165 * we start building the request - in which case we will just
1166 * have to repeat work.
1167 */
1168 request->reserved_space += EXECLISTS_REQUEST_SIZE;
1169
Chris Wilsonfd138212017-11-15 15:12:04 +00001170 ret = intel_ring_wait_for_space(request->ring, request->reserved_space);
1171 if (ret)
1172 return ret;
Chris Wilsonef11c012016-12-18 15:37:19 +00001173
Chris Wilsonef11c012016-12-18 15:37:19 +00001174 /* Note that after this point, we have committed to using
1175 * this request as it is being used to both track the
1176 * state of engine initialisation and liveness of the
1177 * golden renderstate above. Think twice before you try
1178 * to cancel/unwind this request now.
1179 */
1180
1181 request->reserved_space -= EXECLISTS_REQUEST_SIZE;
1182 return 0;
Chris Wilsonef11c012016-12-18 15:37:19 +00001183}
1184
Arun Siluvery9e000842015-07-03 14:27:31 +01001185/*
1186 * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
1187 * PIPE_CONTROL instruction. This is required for the flush to happen correctly
1188 * but there is a slight complication as this is applied in WA batch where the
1189 * values are only initialized once so we cannot take register value at the
1190 * beginning and reuse it further; hence we save its value to memory, upload a
1191 * constant value with bit21 set and then we restore it back with the saved value.
1192 * To simplify the WA, a constant value is formed by using the default value
1193 * of this register. This shouldn't be a problem because we are only modifying
1194 * it for a short period and this batch in non-premptible. We can ofcourse
1195 * use additional instructions that read the actual value of the register
1196 * at that time and set our bit of interest but it makes the WA complicated.
1197 *
1198 * This WA is also required for Gen9 so extracting as a function avoids
1199 * code duplication.
1200 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001201static u32 *
1202gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery9e000842015-07-03 14:27:31 +01001203{
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001204 *batch++ = MI_STORE_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1205 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1206 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
1207 *batch++ = 0;
Arun Siluvery9e000842015-07-03 14:27:31 +01001208
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001209 *batch++ = MI_LOAD_REGISTER_IMM(1);
1210 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1211 *batch++ = 0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES;
Arun Siluvery9e000842015-07-03 14:27:31 +01001212
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001213 batch = gen8_emit_pipe_control(batch,
1214 PIPE_CONTROL_CS_STALL |
1215 PIPE_CONTROL_DC_FLUSH_ENABLE,
1216 0);
Arun Siluvery9e000842015-07-03 14:27:31 +01001217
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001218 *batch++ = MI_LOAD_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1219 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1220 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
1221 *batch++ = 0;
Arun Siluvery9e000842015-07-03 14:27:31 +01001222
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001223 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001224}
1225
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001226/*
1227 * Typically we only have one indirect_ctx and per_ctx batch buffer which are
1228 * initialized at the beginning and shared across all contexts but this field
1229 * helps us to have multiple batches at different offsets and select them based
1230 * on a criteria. At the moment this batch always start at the beginning of the page
1231 * and at this point we don't have multiple wa_ctx batch buffers.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001232 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001233 * The number of WA applied are not known at the beginning; we use this field
1234 * to return the no of DWORDS written.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001235 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001236 * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
1237 * so it adds NOOPs as padding to make it cacheline aligned.
1238 * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
1239 * makes a complete batch buffer.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001240 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001241static u32 *gen8_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001242{
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001243 /* WaDisableCtxRestoreArbitration:bdw,chv */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001244 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001245
Arun Siluveryc82435b2015-06-19 18:37:13 +01001246 /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001247 if (IS_BROADWELL(engine->i915))
1248 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
Arun Siluveryc82435b2015-06-19 18:37:13 +01001249
Arun Siluvery0160f052015-06-23 15:46:57 +01001250 /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
1251 /* Actual scratch location is at 128 bytes offset */
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001252 batch = gen8_emit_pipe_control(batch,
1253 PIPE_CONTROL_FLUSH_L3 |
1254 PIPE_CONTROL_GLOBAL_GTT_IVB |
1255 PIPE_CONTROL_CS_STALL |
1256 PIPE_CONTROL_QW_WRITE,
1257 i915_ggtt_offset(engine->scratch) +
1258 2 * CACHELINE_BYTES);
Arun Siluvery0160f052015-06-23 15:46:57 +01001259
Chris Wilsonbeecec92017-10-03 21:34:52 +01001260 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1261
Arun Siluvery17ee9502015-06-19 19:07:01 +01001262 /* Pad to end of cacheline */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001263 while ((unsigned long)batch % CACHELINE_BYTES)
1264 *batch++ = MI_NOOP;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001265
1266 /*
1267 * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
1268 * execution depends on the length specified in terms of cache lines
1269 * in the register CTX_RCS_INDIRECT_CTX
1270 */
1271
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001272 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001273}
1274
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001275static u32 *gen9_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery0504cff2015-07-14 15:01:27 +01001276{
Chris Wilsonbeecec92017-10-03 21:34:52 +01001277 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
1278
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001279 /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt,glk */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001280 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
Arun Siluverya4106a72015-07-14 15:01:29 +01001281
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001282 /* WaDisableGatherAtSetShaderCommonSlice:skl,bxt,kbl,glk */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001283 *batch++ = MI_LOAD_REGISTER_IMM(1);
1284 *batch++ = i915_mmio_reg_offset(COMMON_SLICE_CHICKEN2);
1285 *batch++ = _MASKED_BIT_DISABLE(
1286 GEN9_DISABLE_GATHER_AT_SET_SHADER_COMMON_SLICE);
1287 *batch++ = MI_NOOP;
Mika Kuoppala873e8172016-07-20 14:26:13 +03001288
Mika Kuoppala066d4622016-06-07 17:19:15 +03001289 /* WaClearSlmSpaceAtContextSwitch:kbl */
1290 /* Actual scratch location is at 128 bytes offset */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001291 if (IS_KBL_REVID(engine->i915, 0, KBL_REVID_A0)) {
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001292 batch = gen8_emit_pipe_control(batch,
1293 PIPE_CONTROL_FLUSH_L3 |
1294 PIPE_CONTROL_GLOBAL_GTT_IVB |
1295 PIPE_CONTROL_CS_STALL |
1296 PIPE_CONTROL_QW_WRITE,
1297 i915_ggtt_offset(engine->scratch)
1298 + 2 * CACHELINE_BYTES);
Mika Kuoppala066d4622016-06-07 17:19:15 +03001299 }
Tim Gore3485d992016-07-05 10:01:30 +01001300
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001301 /* WaMediaPoolStateCmdInWABB:bxt,glk */
Tim Gore3485d992016-07-05 10:01:30 +01001302 if (HAS_POOLED_EU(engine->i915)) {
1303 /*
1304 * EU pool configuration is setup along with golden context
1305 * during context initialization. This value depends on
1306 * device type (2x6 or 3x6) and needs to be updated based
1307 * on which subslice is disabled especially for 2x6
1308 * devices, however it is safe to load default
1309 * configuration of 3x6 device instead of masking off
1310 * corresponding bits because HW ignores bits of a disabled
1311 * subslice and drops down to appropriate config. Please
1312 * see render_state_setup() in i915_gem_render_state.c for
1313 * possible configurations, to avoid duplication they are
1314 * not shown here again.
1315 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001316 *batch++ = GEN9_MEDIA_POOL_STATE;
1317 *batch++ = GEN9_MEDIA_POOL_ENABLE;
1318 *batch++ = 0x00777000;
1319 *batch++ = 0;
1320 *batch++ = 0;
1321 *batch++ = 0;
Tim Gore3485d992016-07-05 10:01:30 +01001322 }
1323
Chris Wilsonbeecec92017-10-03 21:34:52 +01001324 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1325
Arun Siluvery0504cff2015-07-14 15:01:27 +01001326 /* Pad to end of cacheline */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001327 while ((unsigned long)batch % CACHELINE_BYTES)
1328 *batch++ = MI_NOOP;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001329
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001330 return batch;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001331}
1332
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001333#define CTX_WA_BB_OBJ_SIZE (PAGE_SIZE)
1334
1335static int lrc_setup_wa_ctx(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001336{
Chris Wilson48bb74e2016-08-15 10:49:04 +01001337 struct drm_i915_gem_object *obj;
1338 struct i915_vma *vma;
1339 int err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001340
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001341 obj = i915_gem_object_create(engine->i915, CTX_WA_BB_OBJ_SIZE);
Chris Wilson48bb74e2016-08-15 10:49:04 +01001342 if (IS_ERR(obj))
1343 return PTR_ERR(obj);
1344
Chris Wilsona01cb37a2017-01-16 15:21:30 +00001345 vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
Chris Wilson48bb74e2016-08-15 10:49:04 +01001346 if (IS_ERR(vma)) {
1347 err = PTR_ERR(vma);
1348 goto err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001349 }
1350
Chris Wilson48bb74e2016-08-15 10:49:04 +01001351 err = i915_vma_pin(vma, 0, PAGE_SIZE, PIN_GLOBAL | PIN_HIGH);
1352 if (err)
1353 goto err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001354
Chris Wilson48bb74e2016-08-15 10:49:04 +01001355 engine->wa_ctx.vma = vma;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001356 return 0;
Chris Wilson48bb74e2016-08-15 10:49:04 +01001357
1358err:
1359 i915_gem_object_put(obj);
1360 return err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001361}
1362
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001363static void lrc_destroy_wa_ctx(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001364{
Chris Wilson19880c42016-08-15 10:49:05 +01001365 i915_vma_unpin_and_release(&engine->wa_ctx.vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001366}
1367
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001368typedef u32 *(*wa_bb_func_t)(struct intel_engine_cs *engine, u32 *batch);
1369
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001370static int intel_init_workaround_bb(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001371{
Chris Wilson48bb74e2016-08-15 10:49:04 +01001372 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001373 struct i915_wa_ctx_bb *wa_bb[2] = { &wa_ctx->indirect_ctx,
1374 &wa_ctx->per_ctx };
1375 wa_bb_func_t wa_bb_fn[2];
Arun Siluvery17ee9502015-06-19 19:07:01 +01001376 struct page *page;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001377 void *batch, *batch_ptr;
1378 unsigned int i;
Chris Wilson48bb74e2016-08-15 10:49:04 +01001379 int ret;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001380
Tvrtko Ursulin10bde232018-01-19 10:00:04 +00001381 if (GEM_WARN_ON(engine->id != RCS))
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001382 return -EINVAL;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001383
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001384 switch (INTEL_GEN(engine->i915)) {
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001385 case 10:
1386 return 0;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001387 case 9:
1388 wa_bb_fn[0] = gen9_init_indirectctx_bb;
Chris Wilsonb8aa2232017-09-21 14:54:44 +01001389 wa_bb_fn[1] = NULL;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001390 break;
1391 case 8:
1392 wa_bb_fn[0] = gen8_init_indirectctx_bb;
Chris Wilson3ad7b522017-10-03 21:34:49 +01001393 wa_bb_fn[1] = NULL;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001394 break;
1395 default:
1396 MISSING_CASE(INTEL_GEN(engine->i915));
Arun Siluvery5e60d792015-06-23 15:50:44 +01001397 return 0;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001398 }
Arun Siluvery5e60d792015-06-23 15:50:44 +01001399
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001400 ret = lrc_setup_wa_ctx(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001401 if (ret) {
1402 DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
1403 return ret;
1404 }
1405
Chris Wilson48bb74e2016-08-15 10:49:04 +01001406 page = i915_gem_object_get_dirty_page(wa_ctx->vma->obj, 0);
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001407 batch = batch_ptr = kmap_atomic(page);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001408
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001409 /*
1410 * Emit the two workaround batch buffers, recording the offset from the
1411 * start of the workaround batch buffer object for each and their
1412 * respective sizes.
1413 */
1414 for (i = 0; i < ARRAY_SIZE(wa_bb_fn); i++) {
1415 wa_bb[i]->offset = batch_ptr - batch;
1416 if (WARN_ON(!IS_ALIGNED(wa_bb[i]->offset, CACHELINE_BYTES))) {
1417 ret = -EINVAL;
1418 break;
1419 }
Chris Wilson604a8f62017-09-21 14:54:43 +01001420 if (wa_bb_fn[i])
1421 batch_ptr = wa_bb_fn[i](engine, batch_ptr);
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001422 wa_bb[i]->size = batch_ptr - (batch + wa_bb[i]->offset);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001423 }
1424
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001425 BUG_ON(batch_ptr - batch > CTX_WA_BB_OBJ_SIZE);
1426
Arun Siluvery17ee9502015-06-19 19:07:01 +01001427 kunmap_atomic(batch);
1428 if (ret)
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001429 lrc_destroy_wa_ctx(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001430
1431 return ret;
1432}
1433
Chris Wilson64f09f02017-08-07 13:19:19 +01001434static u8 gtiir[] = {
1435 [RCS] = 0,
1436 [BCS] = 0,
1437 [VCS] = 1,
1438 [VCS2] = 1,
1439 [VECS] = 3,
1440};
1441
Chris Wilsonf3c9d4072018-01-02 15:12:34 +00001442static void enable_execlists(struct intel_engine_cs *engine)
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001443{
Chris Wilsonc0336662016-05-06 15:40:21 +01001444 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonf3c9d4072018-01-02 15:12:34 +00001445
1446 I915_WRITE(RING_HWSTAM(engine->mmio_base), 0xffffffff);
1447 I915_WRITE(RING_MODE_GEN7(engine),
1448 _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
1449 I915_WRITE(RING_HWS_PGA(engine->mmio_base),
1450 engine->status_page.ggtt_offset);
1451 POSTING_READ(RING_HWS_PGA(engine->mmio_base));
1452}
1453
1454static int gen8_init_common_ring(struct intel_engine_cs *engine)
1455{
Mika Kuoppalab620e872017-09-22 15:43:03 +03001456 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001457 int ret;
1458
1459 ret = intel_mocs_init_engine(engine);
1460 if (ret)
1461 return ret;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001462
Chris Wilsonad07dfc2016-10-07 07:53:26 +01001463 intel_engine_reset_breadcrumbs(engine);
Chris Wilsonf3b8f912017-01-05 15:30:21 +00001464 intel_engine_init_hangcheck(engine);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001465
Chris Wilsonf3c9d4072018-01-02 15:12:34 +00001466 enable_execlists(engine);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001467 DRM_DEBUG_DRIVER("Execlists enabled for %s\n", engine->name);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001468
Chris Wilson64f09f02017-08-07 13:19:19 +01001469 GEM_BUG_ON(engine->id >= ARRAY_SIZE(gtiir));
1470
Mika Kuoppalab620e872017-09-22 15:43:03 +03001471 execlists->csb_head = -1;
Chris Wilson4a118ec2017-10-23 22:32:36 +01001472 execlists->active = 0;
Chris Wilson6b764a52017-04-25 11:38:35 +01001473
Chris Wilson64f09f02017-08-07 13:19:19 +01001474 /* After a GPU reset, we may have requests to replay */
Michał Winiarski9bdc3572017-10-25 18:25:19 +01001475 if (execlists->first)
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05301476 tasklet_schedule(&execlists->tasklet);
Chris Wilson6b764a52017-04-25 11:38:35 +01001477
Chris Wilson821ed7d2016-09-09 14:11:53 +01001478 return 0;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001479}
1480
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001481static int gen8_init_render_ring(struct intel_engine_cs *engine)
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001482{
Chris Wilsonc0336662016-05-06 15:40:21 +01001483 struct drm_i915_private *dev_priv = engine->i915;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001484 int ret;
1485
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001486 ret = gen8_init_common_ring(engine);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001487 if (ret)
1488 return ret;
1489
1490 /* We need to disable the AsyncFlip performance optimisations in order
1491 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1492 * programmed to '1' on all products.
1493 *
1494 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
1495 */
1496 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1497
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001498 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1499
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001500 return init_workarounds_ring(engine);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001501}
1502
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001503static int gen9_init_render_ring(struct intel_engine_cs *engine)
Damien Lespiau82ef8222015-02-09 19:33:08 +00001504{
1505 int ret;
1506
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001507 ret = gen8_init_common_ring(engine);
Damien Lespiau82ef8222015-02-09 19:33:08 +00001508 if (ret)
1509 return ret;
1510
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001511 return init_workarounds_ring(engine);
Damien Lespiau82ef8222015-02-09 19:33:08 +00001512}
1513
Chris Wilson42232212018-01-02 15:12:32 +00001514static void reset_irq(struct intel_engine_cs *engine)
1515{
1516 struct drm_i915_private *dev_priv = engine->i915;
1517
1518 /*
1519 * Clear any pending interrupt state.
1520 *
1521 * We do it twice out of paranoia that some of the IIR are double
1522 * buffered, and if we only reset it once there may still be
1523 * an interrupt pending.
1524 */
1525 I915_WRITE(GEN8_GT_IIR(gtiir[engine->id]),
1526 GT_CONTEXT_SWITCH_INTERRUPT << engine->irq_shift);
1527 I915_WRITE(GEN8_GT_IIR(gtiir[engine->id]),
1528 GT_CONTEXT_SWITCH_INTERRUPT << engine->irq_shift);
1529 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
1530}
1531
Chris Wilson821ed7d2016-09-09 14:11:53 +01001532static void reset_common_ring(struct intel_engine_cs *engine,
1533 struct drm_i915_gem_request *request)
1534{
Mika Kuoppalab620e872017-09-22 15:43:03 +03001535 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001536 struct intel_context *ce;
Chris Wilson221ab97192017-09-16 21:44:14 +01001537 unsigned long flags;
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001538
Chris Wilson16a87392017-12-20 09:06:26 +00001539 GEM_TRACE("%s seqno=%x\n",
1540 engine->name, request ? request->global_seqno : 0);
Chris Wilson42232212018-01-02 15:12:32 +00001541
1542 reset_irq(engine);
1543
Chris Wilson221ab97192017-09-16 21:44:14 +01001544 spin_lock_irqsave(&engine->timeline->lock, flags);
1545
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001546 /*
1547 * Catch up with any missed context-switch interrupts.
1548 *
1549 * Ideally we would just read the remaining CSB entries now that we
1550 * know the gpu is idle. However, the CSB registers are sometimes^W
1551 * often trashed across a GPU reset! Instead we have to rely on
1552 * guessing the missed context-switch events by looking at what
1553 * requests were completed.
1554 */
Michał Winiarskia4598d12017-10-25 22:00:18 +02001555 execlists_cancel_port_requests(execlists);
Chris Wilson221ab97192017-09-16 21:44:14 +01001556
1557 /* Push back any incomplete requests for replay after the reset. */
Michał Winiarskia4598d12017-10-25 22:00:18 +02001558 __unwind_incomplete_requests(engine);
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001559
Chris Wilson221ab97192017-09-16 21:44:14 +01001560 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001561
1562 /* If the request was innocent, we leave the request in the ELSP
1563 * and will try to replay it on restarting. The context image may
1564 * have been corrupted by the reset, in which case we may have
1565 * to service a new GPU hang, but more likely we can continue on
1566 * without impact.
1567 *
1568 * If the request was guilty, we presume the context is corrupt
1569 * and have to at least restore the RING register in the context
1570 * image back to the expected values to skip over the guilty request.
1571 */
Chris Wilson221ab97192017-09-16 21:44:14 +01001572 if (!request || request->fence.error != -EIO)
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001573 return;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001574
Chris Wilsona3aabe82016-10-04 21:11:26 +01001575 /* We want a simple context + ring to execute the breadcrumb update.
1576 * We cannot rely on the context being intact across the GPU hang,
1577 * so clear it and rebuild just what we need for the breadcrumb.
1578 * All pending requests for this context will be zapped, and any
1579 * future request will be after userspace has had the opportunity
1580 * to recreate its own state.
1581 */
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001582 ce = &request->ctx->engine[engine->id];
Chris Wilsona3aabe82016-10-04 21:11:26 +01001583 execlists_init_reg_state(ce->lrc_reg_state,
1584 request->ctx, engine, ce->ring);
1585
Chris Wilson821ed7d2016-09-09 14:11:53 +01001586 /* Move the RING_HEAD onto the breadcrumb, past the hanging batch */
Chris Wilsona3aabe82016-10-04 21:11:26 +01001587 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
1588 i915_ggtt_offset(ce->ring->vma);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001589 ce->lrc_reg_state[CTX_RING_HEAD+1] = request->postfix;
Chris Wilsona3aabe82016-10-04 21:11:26 +01001590
Chris Wilson821ed7d2016-09-09 14:11:53 +01001591 request->ring->head = request->postfix;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001592 intel_ring_update_space(request->ring);
1593
Chris Wilsona3aabe82016-10-04 21:11:26 +01001594 /* Reset WaIdleLiteRestore:bdw,skl as well */
Chris Wilson7e4992a2017-09-28 20:38:59 +01001595 unwind_wa_tail(request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001596}
1597
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001598static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request *req)
1599{
1600 struct i915_hw_ppgtt *ppgtt = req->ctx->ppgtt;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001601 struct intel_engine_cs *engine = req->engine;
Mika Kuoppalae7167762017-02-28 17:28:10 +02001602 const int num_lri_cmds = GEN8_3LVL_PDPES * 2;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001603 u32 *cs;
1604 int i;
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001605
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001606 cs = intel_ring_begin(req, num_lri_cmds * 2 + 2);
1607 if (IS_ERR(cs))
1608 return PTR_ERR(cs);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001609
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001610 *cs++ = MI_LOAD_REGISTER_IMM(num_lri_cmds);
Mika Kuoppalae7167762017-02-28 17:28:10 +02001611 for (i = GEN8_3LVL_PDPES - 1; i >= 0; i--) {
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001612 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
1613
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001614 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, i));
1615 *cs++ = upper_32_bits(pd_daddr);
1616 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, i));
1617 *cs++ = lower_32_bits(pd_daddr);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001618 }
1619
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001620 *cs++ = MI_NOOP;
1621 intel_ring_advance(req, cs);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001622
1623 return 0;
1624}
1625
John Harrisonbe795fc2015-05-29 17:44:03 +01001626static int gen8_emit_bb_start(struct drm_i915_gem_request *req,
Chris Wilson803688b2016-08-02 22:50:27 +01001627 u64 offset, u32 len,
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001628 const unsigned int flags)
Oscar Mateo15648582014-07-24 17:04:32 +01001629{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001630 u32 *cs;
Oscar Mateo15648582014-07-24 17:04:32 +01001631 int ret;
1632
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001633 /* Don't rely in hw updating PDPs, specially in lite-restore.
1634 * Ideally, we should set Force PD Restore in ctx descriptor,
1635 * but we can't. Force Restore would be a second option, but
1636 * it is unsafe in case of lite-restore (because the ctx is
Michel Thierry2dba3232015-07-30 11:06:23 +01001637 * not idle). PML4 is allocated during ppgtt init so this is
1638 * not needed in 48-bit.*/
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001639 if (req->ctx->ppgtt &&
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001640 (intel_engine_flag(req->engine) & req->ctx->ppgtt->pd_dirty_rings) &&
1641 !i915_vm_is_48bit(&req->ctx->ppgtt->base) &&
1642 !intel_vgpu_active(req->i915)) {
1643 ret = intel_logical_ring_emit_pdps(req);
1644 if (ret)
1645 return ret;
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001646
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00001647 req->ctx->ppgtt->pd_dirty_rings &= ~intel_engine_flag(req->engine);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001648 }
1649
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001650 cs = intel_ring_begin(req, 4);
1651 if (IS_ERR(cs))
1652 return PTR_ERR(cs);
Oscar Mateo15648582014-07-24 17:04:32 +01001653
Chris Wilson279f5a02017-10-05 20:10:05 +01001654 /*
1655 * WaDisableCtxRestoreArbitration:bdw,chv
1656 *
1657 * We don't need to perform MI_ARB_ENABLE as often as we do (in
1658 * particular all the gen that do not need the w/a at all!), if we
1659 * took care to make sure that on every switch into this context
1660 * (both ordinary and for preemption) that arbitrartion was enabled
1661 * we would be fine. However, there doesn't seem to be a downside to
1662 * being paranoid and making sure it is set before each batch and
1663 * every context-switch.
1664 *
1665 * Note that if we fail to enable arbitration before the request
1666 * is complete, then we do not see the context-switch interrupt and
1667 * the engine hangs (with RING_HEAD == RING_TAIL).
1668 *
1669 * That satisfies both the GPGPU w/a and our heavy-handed paranoia.
1670 */
Chris Wilson3ad7b522017-10-03 21:34:49 +01001671 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1672
Oscar Mateo15648582014-07-24 17:04:32 +01001673 /* FIXME(BDW): Address space and security selectors. */
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001674 *cs++ = MI_BATCH_BUFFER_START_GEN8 |
1675 (flags & I915_DISPATCH_SECURE ? 0 : BIT(8)) |
1676 (flags & I915_DISPATCH_RS ? MI_BATCH_RESOURCE_STREAMER : 0);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001677 *cs++ = lower_32_bits(offset);
1678 *cs++ = upper_32_bits(offset);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001679 intel_ring_advance(req, cs);
Oscar Mateo15648582014-07-24 17:04:32 +01001680
1681 return 0;
1682}
1683
Chris Wilson31bb59c2016-07-01 17:23:27 +01001684static void gen8_logical_ring_enable_irq(struct intel_engine_cs *engine)
Oscar Mateo73d477f2014-07-24 17:04:31 +01001685{
Chris Wilsonc0336662016-05-06 15:40:21 +01001686 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson31bb59c2016-07-01 17:23:27 +01001687 I915_WRITE_IMR(engine,
1688 ~(engine->irq_enable_mask | engine->irq_keep_mask));
1689 POSTING_READ_FW(RING_IMR(engine->mmio_base));
Oscar Mateo73d477f2014-07-24 17:04:31 +01001690}
1691
Chris Wilson31bb59c2016-07-01 17:23:27 +01001692static void gen8_logical_ring_disable_irq(struct intel_engine_cs *engine)
Oscar Mateo73d477f2014-07-24 17:04:31 +01001693{
Chris Wilsonc0336662016-05-06 15:40:21 +01001694 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson31bb59c2016-07-01 17:23:27 +01001695 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Oscar Mateo73d477f2014-07-24 17:04:31 +01001696}
1697
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001698static int gen8_emit_flush(struct drm_i915_gem_request *request, u32 mode)
Oscar Mateo47122742014-07-24 17:04:28 +01001699{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001700 u32 cmd, *cs;
Oscar Mateo47122742014-07-24 17:04:28 +01001701
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001702 cs = intel_ring_begin(request, 4);
1703 if (IS_ERR(cs))
1704 return PTR_ERR(cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001705
1706 cmd = MI_FLUSH_DW + 1;
1707
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001708 /* We always require a command barrier so that subsequent
1709 * commands, such as breadcrumb interrupts, are strictly ordered
1710 * wrt the contents of the write cache being flushed to memory
1711 * (and thus being coherent from the CPU).
1712 */
1713 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1714
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001715 if (mode & EMIT_INVALIDATE) {
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001716 cmd |= MI_INVALIDATE_TLB;
Chris Wilson1dae2df2016-08-02 22:50:19 +01001717 if (request->engine->id == VCS)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001718 cmd |= MI_INVALIDATE_BSD;
Oscar Mateo47122742014-07-24 17:04:28 +01001719 }
1720
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001721 *cs++ = cmd;
1722 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
1723 *cs++ = 0; /* upper addr */
1724 *cs++ = 0; /* value */
1725 intel_ring_advance(request, cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001726
1727 return 0;
1728}
1729
John Harrison7deb4d32015-05-29 17:43:59 +01001730static int gen8_emit_flush_render(struct drm_i915_gem_request *request,
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001731 u32 mode)
Oscar Mateo47122742014-07-24 17:04:28 +01001732{
Chris Wilsonb5321f32016-08-02 22:50:18 +01001733 struct intel_engine_cs *engine = request->engine;
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001734 u32 scratch_addr =
1735 i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001736 bool vf_flush_wa = false, dc_flush_wa = false;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001737 u32 *cs, flags = 0;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001738 int len;
Oscar Mateo47122742014-07-24 17:04:28 +01001739
1740 flags |= PIPE_CONTROL_CS_STALL;
1741
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001742 if (mode & EMIT_FLUSH) {
Oscar Mateo47122742014-07-24 17:04:28 +01001743 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
1744 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -08001745 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +01001746 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Oscar Mateo47122742014-07-24 17:04:28 +01001747 }
1748
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001749 if (mode & EMIT_INVALIDATE) {
Oscar Mateo47122742014-07-24 17:04:28 +01001750 flags |= PIPE_CONTROL_TLB_INVALIDATE;
1751 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
1752 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
1753 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
1754 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
1755 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
1756 flags |= PIPE_CONTROL_QW_WRITE;
1757 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Oscar Mateo47122742014-07-24 17:04:28 +01001758
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001759 /*
1760 * On GEN9: before VF_CACHE_INVALIDATE we need to emit a NULL
1761 * pipe control.
1762 */
Chris Wilsonc0336662016-05-06 15:40:21 +01001763 if (IS_GEN9(request->i915))
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001764 vf_flush_wa = true;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001765
1766 /* WaForGAMHang:kbl */
1767 if (IS_KBL_REVID(request->i915, 0, KBL_REVID_B0))
1768 dc_flush_wa = true;
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001769 }
Imre Deak9647ff32015-01-25 13:27:11 -08001770
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001771 len = 6;
1772
1773 if (vf_flush_wa)
1774 len += 6;
1775
1776 if (dc_flush_wa)
1777 len += 12;
1778
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001779 cs = intel_ring_begin(request, len);
1780 if (IS_ERR(cs))
1781 return PTR_ERR(cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001782
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001783 if (vf_flush_wa)
1784 cs = gen8_emit_pipe_control(cs, 0, 0);
Imre Deak9647ff32015-01-25 13:27:11 -08001785
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001786 if (dc_flush_wa)
1787 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_DC_FLUSH_ENABLE,
1788 0);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001789
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001790 cs = gen8_emit_pipe_control(cs, flags, scratch_addr);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001791
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001792 if (dc_flush_wa)
1793 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_CS_STALL, 0);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001794
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001795 intel_ring_advance(request, cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001796
1797 return 0;
1798}
1799
Chris Wilson7c17d372016-01-20 15:43:35 +02001800/*
1801 * Reserve space for 2 NOOPs at the end of each request to be
1802 * used as a workaround for not being allowed to do lite
1803 * restore with HEAD==TAIL (WaIdleLiteRestore).
1804 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001805static void gen8_emit_wa_tail(struct drm_i915_gem_request *request, u32 *cs)
Oscar Mateo4da46e12014-07-24 17:04:27 +01001806{
Chris Wilsonbeecec92017-10-03 21:34:52 +01001807 /* Ensure there's always at least one preemption point per-request. */
1808 *cs++ = MI_ARB_CHECK;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001809 *cs++ = MI_NOOP;
1810 request->wa_tail = intel_ring_offset(request, cs);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001811}
Oscar Mateo4da46e12014-07-24 17:04:27 +01001812
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001813static void gen8_emit_breadcrumb(struct drm_i915_gem_request *request, u32 *cs)
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001814{
Chris Wilson7c17d372016-01-20 15:43:35 +02001815 /* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */
1816 BUILD_BUG_ON(I915_GEM_HWS_INDEX_ADDR & (1 << 5));
Oscar Mateo4da46e12014-07-24 17:04:27 +01001817
Michał Winiarskidf77cd82017-10-25 22:00:15 +02001818 cs = gen8_emit_ggtt_write(cs, request->global_seqno,
1819 intel_hws_seqno_address(request->engine));
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001820 *cs++ = MI_USER_INTERRUPT;
1821 *cs++ = MI_NOOP;
1822 request->tail = intel_ring_offset(request, cs);
Chris Wilsoned1501d2017-03-27 14:14:12 +01001823 assert_ring_tail_valid(request->ring, request->tail);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001824
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001825 gen8_emit_wa_tail(request, cs);
Chris Wilson7c17d372016-01-20 15:43:35 +02001826}
Chris Wilson98f29e82016-10-28 13:58:51 +01001827static const int gen8_emit_breadcrumb_sz = 6 + WA_TAIL_DWORDS;
1828
Michał Winiarskidf77cd82017-10-25 22:00:15 +02001829static void gen8_emit_breadcrumb_rcs(struct drm_i915_gem_request *request,
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001830 u32 *cs)
Chris Wilson7c17d372016-01-20 15:43:35 +02001831{
Michał Winiarskice81a652016-04-12 15:51:55 +02001832 /* We're using qword write, seqno should be aligned to 8 bytes. */
1833 BUILD_BUG_ON(I915_GEM_HWS_INDEX & 1);
1834
Michał Winiarskidf77cd82017-10-25 22:00:15 +02001835 cs = gen8_emit_ggtt_write_rcs(cs, request->global_seqno,
1836 intel_hws_seqno_address(request->engine));
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001837 *cs++ = MI_USER_INTERRUPT;
1838 *cs++ = MI_NOOP;
1839 request->tail = intel_ring_offset(request, cs);
Chris Wilsoned1501d2017-03-27 14:14:12 +01001840 assert_ring_tail_valid(request->ring, request->tail);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001841
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001842 gen8_emit_wa_tail(request, cs);
Oscar Mateo4da46e12014-07-24 17:04:27 +01001843}
Michał Winiarskidf77cd82017-10-25 22:00:15 +02001844static const int gen8_emit_breadcrumb_rcs_sz = 8 + WA_TAIL_DWORDS;
Chris Wilson98f29e82016-10-28 13:58:51 +01001845
John Harrison87531812015-05-29 17:43:44 +01001846static int gen8_init_rcs_context(struct drm_i915_gem_request *req)
Thomas Daniele7778be2014-12-02 12:50:48 +00001847{
1848 int ret;
1849
Tvrtko Ursulin4ac96592017-02-14 15:00:17 +00001850 ret = intel_ring_workarounds_emit(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001851 if (ret)
1852 return ret;
1853
Peter Antoine3bbaba02015-07-10 20:13:11 +03001854 ret = intel_rcs_context_init_mocs(req);
1855 /*
1856 * Failing to program the MOCS is non-fatal.The system will not
1857 * run at peak performance. So generate an error and carry on.
1858 */
1859 if (ret)
1860 DRM_ERROR("MOCS failed to program: expect performance issues.\n");
1861
Chris Wilson4e50f082016-10-28 13:58:31 +01001862 return i915_gem_render_state_emit(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001863}
1864
Oscar Mateo73e4d072014-07-24 17:04:48 +01001865/**
1866 * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001867 * @engine: Engine Command Streamer.
Oscar Mateo73e4d072014-07-24 17:04:48 +01001868 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001869void intel_logical_ring_cleanup(struct intel_engine_cs *engine)
Oscar Mateo454afeb2014-07-24 17:04:22 +01001870{
John Harrison6402c332014-10-31 12:00:26 +00001871 struct drm_i915_private *dev_priv;
Oscar Mateo9832b9d2014-07-24 17:04:30 +01001872
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01001873 /*
1874 * Tasklet cannot be active at this point due intel_mark_active/idle
1875 * so this is just for documentation.
1876 */
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05301877 if (WARN_ON(test_bit(TASKLET_STATE_SCHED,
1878 &engine->execlists.tasklet.state)))
1879 tasklet_kill(&engine->execlists.tasklet);
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01001880
Chris Wilsonc0336662016-05-06 15:40:21 +01001881 dev_priv = engine->i915;
John Harrison6402c332014-10-31 12:00:26 +00001882
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001883 if (engine->buffer) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001884 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
Dave Gordonb0366a52015-12-08 15:02:36 +00001885 }
Oscar Mateo48d82382014-07-24 17:04:23 +01001886
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001887 if (engine->cleanup)
1888 engine->cleanup(engine);
Oscar Mateo48d82382014-07-24 17:04:23 +01001889
Chris Wilsone8a9c582016-12-18 15:37:20 +00001890 intel_engine_cleanup_common(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001891
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001892 lrc_destroy_wa_ctx(engine);
Chris Wilsonf3c9d4072018-01-02 15:12:34 +00001893
Chris Wilsonc0336662016-05-06 15:40:21 +01001894 engine->i915 = NULL;
Akash Goel3b3f1652016-10-13 22:44:48 +05301895 dev_priv->engine[engine->id] = NULL;
1896 kfree(engine);
Oscar Mateo454afeb2014-07-24 17:04:22 +01001897}
1898
Chris Wilsonff44ad52017-03-16 17:13:03 +00001899static void execlists_set_default_submission(struct intel_engine_cs *engine)
Chris Wilsonddd66c52016-08-02 22:50:31 +01001900{
Chris Wilsonff44ad52017-03-16 17:13:03 +00001901 engine->submit_request = execlists_submit_request;
Chris Wilson27a5f612017-09-15 18:31:00 +01001902 engine->cancel_requests = execlists_cancel_requests;
Chris Wilsonff44ad52017-03-16 17:13:03 +00001903 engine->schedule = execlists_schedule;
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05301904 engine->execlists.tasklet.func = execlists_submission_tasklet;
Chris Wilsonaba5e272017-10-25 15:39:41 +01001905
1906 engine->park = NULL;
1907 engine->unpark = NULL;
Tvrtko Ursulincf669b42017-11-29 10:28:05 +00001908
1909 engine->flags |= I915_ENGINE_SUPPORTS_STATS;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001910}
1911
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001912static void
Chris Wilsone1382ef2016-05-06 15:40:20 +01001913logical_ring_default_vfuncs(struct intel_engine_cs *engine)
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001914{
1915 /* Default vfuncs which can be overriden by each engine. */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001916 engine->init_hw = gen8_init_common_ring;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001917 engine->reset_hw = reset_common_ring;
Chris Wilsone8a9c582016-12-18 15:37:20 +00001918
1919 engine->context_pin = execlists_context_pin;
1920 engine->context_unpin = execlists_context_unpin;
1921
Chris Wilsonf73e7392016-12-18 15:37:24 +00001922 engine->request_alloc = execlists_request_alloc;
1923
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001924 engine->emit_flush = gen8_emit_flush;
Chris Wilson9b81d552016-10-28 13:58:50 +01001925 engine->emit_breadcrumb = gen8_emit_breadcrumb;
Chris Wilson98f29e82016-10-28 13:58:51 +01001926 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_sz;
Chris Wilsonff44ad52017-03-16 17:13:03 +00001927
1928 engine->set_default_submission = execlists_set_default_submission;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001929
Chris Wilson31bb59c2016-07-01 17:23:27 +01001930 engine->irq_enable = gen8_logical_ring_enable_irq;
1931 engine->irq_disable = gen8_logical_ring_disable_irq;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001932 engine->emit_bb_start = gen8_emit_bb_start;
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001933}
1934
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001935static inline void
Dave Gordonc2c7f242016-07-13 16:03:35 +01001936logical_ring_default_irqs(struct intel_engine_cs *engine)
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001937{
Dave Gordonc2c7f242016-07-13 16:03:35 +01001938 unsigned shift = engine->irq_shift;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001939 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << shift;
1940 engine->irq_keep_mask = GT_CONTEXT_SWITCH_INTERRUPT << shift;
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001941}
1942
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001943static void
1944logical_ring_setup(struct intel_engine_cs *engine)
1945{
1946 struct drm_i915_private *dev_priv = engine->i915;
1947 enum forcewake_domains fw_domains;
1948
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01001949 intel_engine_setup_common(engine);
1950
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001951 /* Intentionally left blank. */
1952 engine->buffer = NULL;
1953
1954 fw_domains = intel_uncore_forcewake_for_reg(dev_priv,
1955 RING_ELSP(engine),
1956 FW_REG_WRITE);
1957
1958 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
1959 RING_CONTEXT_STATUS_PTR(engine),
1960 FW_REG_READ | FW_REG_WRITE);
1961
1962 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
1963 RING_CONTEXT_STATUS_BUF_BASE(engine),
1964 FW_REG_READ);
1965
Mika Kuoppalab620e872017-09-22 15:43:03 +03001966 engine->execlists.fw_domains = fw_domains;
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001967
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05301968 tasklet_init(&engine->execlists.tasklet,
1969 execlists_submission_tasklet, (unsigned long)engine);
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001970
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001971 logical_ring_default_vfuncs(engine);
1972 logical_ring_default_irqs(engine);
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001973}
1974
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +01001975static int logical_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001976{
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001977 int ret;
1978
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01001979 ret = intel_engine_init_common(engine);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001980 if (ret)
1981 goto error;
1982
Chris Wilson693cfbf2018-01-02 15:12:33 +00001983 engine->execlists.elsp =
1984 engine->i915->regs + i915_mmio_reg_offset(RING_ELSP(engine));
1985
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001986 return 0;
1987
1988error:
1989 intel_logical_ring_cleanup(engine);
1990 return ret;
1991}
1992
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +01001993int logical_render_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001994{
1995 struct drm_i915_private *dev_priv = engine->i915;
1996 int ret;
1997
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001998 logical_ring_setup(engine);
1999
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002000 if (HAS_L3_DPF(dev_priv))
2001 engine->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
2002
2003 /* Override some for render ring. */
2004 if (INTEL_GEN(dev_priv) >= 9)
2005 engine->init_hw = gen9_init_render_ring;
2006 else
2007 engine->init_hw = gen8_init_render_ring;
2008 engine->init_context = gen8_init_rcs_context;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002009 engine->emit_flush = gen8_emit_flush_render;
Michał Winiarskidf77cd82017-10-25 22:00:15 +02002010 engine->emit_breadcrumb = gen8_emit_breadcrumb_rcs;
2011 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_rcs_sz;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002012
Chris Wilsonf51455d2017-01-10 14:47:34 +00002013 ret = intel_engine_create_scratch(engine, PAGE_SIZE);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002014 if (ret)
2015 return ret;
2016
2017 ret = intel_init_workaround_bb(engine);
2018 if (ret) {
2019 /*
2020 * We continue even if we fail to initialize WA batch
2021 * because we only expect rare glitches but nothing
2022 * critical to prevent us from using GPU
2023 */
2024 DRM_ERROR("WA batch buffer initialization failed: %d\n",
2025 ret);
2026 }
2027
Tvrtko Ursulind038fc72016-12-16 13:18:42 +00002028 return logical_ring_init(engine);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002029}
2030
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +01002031int logical_xcs_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002032{
2033 logical_ring_setup(engine);
2034
2035 return logical_ring_init(engine);
2036}
2037
Jeff McGee0cea6502015-02-13 10:27:56 -06002038static u32
Chris Wilsonc0336662016-05-06 15:40:21 +01002039make_rpcs(struct drm_i915_private *dev_priv)
Jeff McGee0cea6502015-02-13 10:27:56 -06002040{
2041 u32 rpcs = 0;
2042
2043 /*
2044 * No explicit RPCS request is needed to ensure full
2045 * slice/subslice/EU enablement prior to Gen9.
2046 */
Chris Wilsonc0336662016-05-06 15:40:21 +01002047 if (INTEL_GEN(dev_priv) < 9)
Jeff McGee0cea6502015-02-13 10:27:56 -06002048 return 0;
2049
2050 /*
2051 * Starting in Gen9, render power gating can leave
2052 * slice/subslice/EU in a partially enabled state. We
2053 * must make an explicit request through RPCS for full
2054 * enablement.
2055 */
Imre Deak43b67992016-08-31 19:13:02 +03002056 if (INTEL_INFO(dev_priv)->sseu.has_slice_pg) {
Jeff McGee0cea6502015-02-13 10:27:56 -06002057 rpcs |= GEN8_RPCS_S_CNT_ENABLE;
Imre Deakf08a0c92016-08-31 19:13:04 +03002058 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.slice_mask) <<
Jeff McGee0cea6502015-02-13 10:27:56 -06002059 GEN8_RPCS_S_CNT_SHIFT;
2060 rpcs |= GEN8_RPCS_ENABLE;
2061 }
2062
Imre Deak43b67992016-08-31 19:13:02 +03002063 if (INTEL_INFO(dev_priv)->sseu.has_subslice_pg) {
Jeff McGee0cea6502015-02-13 10:27:56 -06002064 rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
Imre Deak57ec1712016-08-31 19:13:05 +03002065 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.subslice_mask) <<
Jeff McGee0cea6502015-02-13 10:27:56 -06002066 GEN8_RPCS_SS_CNT_SHIFT;
2067 rpcs |= GEN8_RPCS_ENABLE;
2068 }
2069
Imre Deak43b67992016-08-31 19:13:02 +03002070 if (INTEL_INFO(dev_priv)->sseu.has_eu_pg) {
2071 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
Jeff McGee0cea6502015-02-13 10:27:56 -06002072 GEN8_RPCS_EU_MIN_SHIFT;
Imre Deak43b67992016-08-31 19:13:02 +03002073 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
Jeff McGee0cea6502015-02-13 10:27:56 -06002074 GEN8_RPCS_EU_MAX_SHIFT;
2075 rpcs |= GEN8_RPCS_ENABLE;
2076 }
2077
2078 return rpcs;
2079}
2080
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002081static u32 intel_lr_indirect_ctx_offset(struct intel_engine_cs *engine)
Michel Thierry71562912016-02-23 10:31:49 +00002082{
2083 u32 indirect_ctx_offset;
2084
Chris Wilsonc0336662016-05-06 15:40:21 +01002085 switch (INTEL_GEN(engine->i915)) {
Michel Thierry71562912016-02-23 10:31:49 +00002086 default:
Chris Wilsonc0336662016-05-06 15:40:21 +01002087 MISSING_CASE(INTEL_GEN(engine->i915));
Michel Thierry71562912016-02-23 10:31:49 +00002088 /* fall through */
Michel Thierry7bd0a2c2017-06-06 13:30:38 -07002089 case 10:
2090 indirect_ctx_offset =
2091 GEN10_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2092 break;
Michel Thierry71562912016-02-23 10:31:49 +00002093 case 9:
2094 indirect_ctx_offset =
2095 GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2096 break;
2097 case 8:
2098 indirect_ctx_offset =
2099 GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2100 break;
2101 }
2102
2103 return indirect_ctx_offset;
2104}
2105
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002106static void execlists_init_reg_state(u32 *regs,
Chris Wilsona3aabe82016-10-04 21:11:26 +01002107 struct i915_gem_context *ctx,
2108 struct intel_engine_cs *engine,
2109 struct intel_ring *ring)
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002110{
Chris Wilsona3aabe82016-10-04 21:11:26 +01002111 struct drm_i915_private *dev_priv = engine->i915;
2112 struct i915_hw_ppgtt *ppgtt = ctx->ppgtt ?: dev_priv->mm.aliasing_ppgtt;
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002113 u32 base = engine->mmio_base;
2114 bool rcs = engine->id == RCS;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002115
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002116 /* A context is actually a big batch buffer with several
2117 * MI_LOAD_REGISTER_IMM commands followed by (reg, value) pairs. The
2118 * values we are setting here are only for the first context restore:
2119 * on a subsequent save, the GPU will recreate this batchbuffer with new
2120 * values (including all the missing MI_LOAD_REGISTER_IMM commands that
2121 * we are not initializing here).
2122 */
2123 regs[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(rcs ? 14 : 11) |
2124 MI_LRI_FORCE_POSTED;
2125
2126 CTX_REG(regs, CTX_CONTEXT_CONTROL, RING_CONTEXT_CONTROL(engine),
Chris Wilson09b1a4e2018-01-25 11:24:42 +00002127 _MASKED_BIT_DISABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
2128 CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT) |
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002129 _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002130 (HAS_RESOURCE_STREAMER(dev_priv) ?
2131 CTX_CTRL_RS_CTX_ENABLE : 0)));
2132 CTX_REG(regs, CTX_RING_HEAD, RING_HEAD(base), 0);
2133 CTX_REG(regs, CTX_RING_TAIL, RING_TAIL(base), 0);
2134 CTX_REG(regs, CTX_RING_BUFFER_START, RING_START(base), 0);
2135 CTX_REG(regs, CTX_RING_BUFFER_CONTROL, RING_CTL(base),
2136 RING_CTL_SIZE(ring->size) | RING_VALID);
2137 CTX_REG(regs, CTX_BB_HEAD_U, RING_BBADDR_UDW(base), 0);
2138 CTX_REG(regs, CTX_BB_HEAD_L, RING_BBADDR(base), 0);
2139 CTX_REG(regs, CTX_BB_STATE, RING_BBSTATE(base), RING_BB_PPGTT);
2140 CTX_REG(regs, CTX_SECOND_BB_HEAD_U, RING_SBBADDR_UDW(base), 0);
2141 CTX_REG(regs, CTX_SECOND_BB_HEAD_L, RING_SBBADDR(base), 0);
2142 CTX_REG(regs, CTX_SECOND_BB_STATE, RING_SBBSTATE(base), 0);
2143 if (rcs) {
Chris Wilson604a8f62017-09-21 14:54:43 +01002144 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
2145
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002146 CTX_REG(regs, CTX_RCS_INDIRECT_CTX, RING_INDIRECT_CTX(base), 0);
2147 CTX_REG(regs, CTX_RCS_INDIRECT_CTX_OFFSET,
2148 RING_INDIRECT_CTX_OFFSET(base), 0);
Chris Wilson604a8f62017-09-21 14:54:43 +01002149 if (wa_ctx->indirect_ctx.size) {
Chris Wilsonbde13eb2016-08-15 10:49:07 +01002150 u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01002151
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002152 regs[CTX_RCS_INDIRECT_CTX + 1] =
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00002153 (ggtt_offset + wa_ctx->indirect_ctx.offset) |
2154 (wa_ctx->indirect_ctx.size / CACHELINE_BYTES);
Arun Siluvery17ee9502015-06-19 19:07:01 +01002155
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002156 regs[CTX_RCS_INDIRECT_CTX_OFFSET + 1] =
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002157 intel_lr_indirect_ctx_offset(engine) << 6;
Chris Wilson604a8f62017-09-21 14:54:43 +01002158 }
2159
2160 CTX_REG(regs, CTX_BB_PER_CTX_PTR, RING_BB_PER_CTX_PTR(base), 0);
2161 if (wa_ctx->per_ctx.size) {
2162 u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01002163
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002164 regs[CTX_BB_PER_CTX_PTR + 1] =
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00002165 (ggtt_offset + wa_ctx->per_ctx.offset) | 0x01;
Arun Siluvery17ee9502015-06-19 19:07:01 +01002166 }
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002167 }
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002168
2169 regs[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9) | MI_LRI_FORCE_POSTED;
2170
2171 CTX_REG(regs, CTX_CTX_TIMESTAMP, RING_CTX_TIMESTAMP(base), 0);
Ville Syrjälä0d925ea2015-11-04 23:20:11 +02002172 /* PDP values well be assigned later if needed */
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002173 CTX_REG(regs, CTX_PDP3_UDW, GEN8_RING_PDP_UDW(engine, 3), 0);
2174 CTX_REG(regs, CTX_PDP3_LDW, GEN8_RING_PDP_LDW(engine, 3), 0);
2175 CTX_REG(regs, CTX_PDP2_UDW, GEN8_RING_PDP_UDW(engine, 2), 0);
2176 CTX_REG(regs, CTX_PDP2_LDW, GEN8_RING_PDP_LDW(engine, 2), 0);
2177 CTX_REG(regs, CTX_PDP1_UDW, GEN8_RING_PDP_UDW(engine, 1), 0);
2178 CTX_REG(regs, CTX_PDP1_LDW, GEN8_RING_PDP_LDW(engine, 1), 0);
2179 CTX_REG(regs, CTX_PDP0_UDW, GEN8_RING_PDP_UDW(engine, 0), 0);
2180 CTX_REG(regs, CTX_PDP0_LDW, GEN8_RING_PDP_LDW(engine, 0), 0);
Michel Thierryd7b26332015-04-08 12:13:34 +01002181
Chris Wilson949e8ab2017-02-09 14:40:36 +00002182 if (ppgtt && i915_vm_is_48bit(&ppgtt->base)) {
Michel Thierry2dba3232015-07-30 11:06:23 +01002183 /* 64b PPGTT (48bit canonical)
2184 * PDP0_DESCRIPTOR contains the base address to PML4 and
2185 * other PDP Descriptors are ignored.
2186 */
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002187 ASSIGN_CTX_PML4(ppgtt, regs);
Michel Thierry2dba3232015-07-30 11:06:23 +01002188 }
2189
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002190 if (rcs) {
2191 regs[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
2192 CTX_REG(regs, CTX_R_PWR_CLK_STATE, GEN8_R_PWR_CLK_STATE,
2193 make_rpcs(dev_priv));
Robert Bragg19f81df2017-06-13 12:23:03 +01002194
2195 i915_oa_init_reg_state(engine, ctx, regs);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002196 }
Chris Wilsona3aabe82016-10-04 21:11:26 +01002197}
2198
2199static int
2200populate_lr_context(struct i915_gem_context *ctx,
2201 struct drm_i915_gem_object *ctx_obj,
2202 struct intel_engine_cs *engine,
2203 struct intel_ring *ring)
2204{
2205 void *vaddr;
Chris Wilsond2b4b972017-11-10 14:26:33 +00002206 u32 *regs;
Chris Wilsona3aabe82016-10-04 21:11:26 +01002207 int ret;
2208
2209 ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
2210 if (ret) {
2211 DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
2212 return ret;
2213 }
2214
2215 vaddr = i915_gem_object_pin_map(ctx_obj, I915_MAP_WB);
2216 if (IS_ERR(vaddr)) {
2217 ret = PTR_ERR(vaddr);
2218 DRM_DEBUG_DRIVER("Could not map object pages! (%d)\n", ret);
2219 return ret;
2220 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002221 ctx_obj->mm.dirty = true;
Chris Wilsona3aabe82016-10-04 21:11:26 +01002222
Chris Wilsond2b4b972017-11-10 14:26:33 +00002223 if (engine->default_state) {
2224 /*
2225 * We only want to copy over the template context state;
2226 * skipping over the headers reserved for GuC communication,
2227 * leaving those as zero.
2228 */
2229 const unsigned long start = LRC_HEADER_PAGES * PAGE_SIZE;
2230 void *defaults;
2231
2232 defaults = i915_gem_object_pin_map(engine->default_state,
2233 I915_MAP_WB);
2234 if (IS_ERR(defaults))
2235 return PTR_ERR(defaults);
2236
2237 memcpy(vaddr + start, defaults + start, engine->context_size);
2238 i915_gem_object_unpin_map(engine->default_state);
2239 }
2240
Chris Wilsona3aabe82016-10-04 21:11:26 +01002241 /* The second page of the context object contains some fields which must
2242 * be set up prior to the first execution. */
Chris Wilsond2b4b972017-11-10 14:26:33 +00002243 regs = vaddr + LRC_STATE_PN * PAGE_SIZE;
2244 execlists_init_reg_state(regs, ctx, engine, ring);
2245 if (!engine->default_state)
2246 regs[CTX_CONTEXT_CONTROL + 1] |=
2247 _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT);
Chris Wilson517aaff2018-01-23 21:04:12 +00002248 if (ctx->hw_id == PREEMPT_ID)
2249 regs[CTX_CONTEXT_CONTROL + 1] |=
2250 _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
2251 CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002252
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01002253 i915_gem_object_unpin_map(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002254
2255 return 0;
2256}
2257
Chris Wilsone2efd132016-05-24 14:53:34 +01002258static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
Chris Wilson978f1e02016-04-28 09:56:54 +01002259 struct intel_engine_cs *engine)
Oscar Mateoede7d422014-07-24 17:04:12 +01002260{
Oscar Mateo8c8579172014-07-24 17:04:14 +01002261 struct drm_i915_gem_object *ctx_obj;
Chris Wilson9021ad02016-05-24 14:53:37 +01002262 struct intel_context *ce = &ctx->engine[engine->id];
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002263 struct i915_vma *vma;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002264 uint32_t context_size;
Chris Wilson7e37f882016-08-02 22:50:21 +01002265 struct intel_ring *ring;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002266 int ret;
2267
Chris Wilson9021ad02016-05-24 14:53:37 +01002268 WARN_ON(ce->state);
Oscar Mateoede7d422014-07-24 17:04:12 +01002269
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +03002270 context_size = round_up(engine->context_size, I915_GTT_PAGE_SIZE);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002271
Michel Thierry0b29c752017-09-13 09:56:00 +01002272 /*
2273 * Before the actual start of the context image, we insert a few pages
2274 * for our own use and for sharing with the GuC.
2275 */
2276 context_size += LRC_HEADER_PAGES * PAGE_SIZE;
Alex Daid1675192015-08-12 15:43:43 +01002277
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00002278 ctx_obj = i915_gem_object_create(ctx->i915, context_size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01002279 if (IS_ERR(ctx_obj)) {
Dan Carpenter3126a662015-04-30 17:30:50 +03002280 DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
Chris Wilsonfe3db792016-04-25 13:32:13 +01002281 return PTR_ERR(ctx_obj);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002282 }
2283
Chris Wilsona01cb37a2017-01-16 15:21:30 +00002284 vma = i915_vma_instance(ctx_obj, &ctx->i915->ggtt.base, NULL);
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002285 if (IS_ERR(vma)) {
2286 ret = PTR_ERR(vma);
2287 goto error_deref_obj;
2288 }
2289
Chris Wilson7e37f882016-08-02 22:50:21 +01002290 ring = intel_engine_create_ring(engine, ctx->ring_size);
Chris Wilsondca33ec2016-08-02 22:50:20 +01002291 if (IS_ERR(ring)) {
2292 ret = PTR_ERR(ring);
Nick Hoathe84fe802015-09-11 12:53:46 +01002293 goto error_deref_obj;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002294 }
2295
Chris Wilsondca33ec2016-08-02 22:50:20 +01002296 ret = populate_lr_context(ctx, ctx_obj, engine, ring);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002297 if (ret) {
2298 DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
Chris Wilsondca33ec2016-08-02 22:50:20 +01002299 goto error_ring_free;
Oscar Mateo84c23772014-07-24 17:04:15 +01002300 }
2301
Chris Wilsondca33ec2016-08-02 22:50:20 +01002302 ce->ring = ring;
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002303 ce->state = vma;
Oscar Mateoede7d422014-07-24 17:04:12 +01002304
2305 return 0;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002306
Chris Wilsondca33ec2016-08-02 22:50:20 +01002307error_ring_free:
Chris Wilson7e37f882016-08-02 22:50:21 +01002308 intel_ring_free(ring);
Nick Hoathe84fe802015-09-11 12:53:46 +01002309error_deref_obj:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002310 i915_gem_object_put(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002311 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01002312}
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002313
Chris Wilson821ed7d2016-09-09 14:11:53 +01002314void intel_lr_context_resume(struct drm_i915_private *dev_priv)
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002315{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002316 struct intel_engine_cs *engine;
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002317 struct i915_gem_context *ctx;
Akash Goel3b3f1652016-10-13 22:44:48 +05302318 enum intel_engine_id id;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002319
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002320 /* Because we emit WA_TAIL_DWORDS there may be a disparity
2321 * between our bookkeeping in ce->ring->head and ce->ring->tail and
2322 * that stored in context. As we only write new commands from
2323 * ce->ring->tail onwards, everything before that is junk. If the GPU
2324 * starts reading from its RING_HEAD from the context, it may try to
2325 * execute that junk and die.
2326 *
2327 * So to avoid that we reset the context images upon resume. For
2328 * simplicity, we just zero everything out.
2329 */
Chris Wilson829a0af2017-06-20 12:05:45 +01002330 list_for_each_entry(ctx, &dev_priv->contexts.list, link) {
Akash Goel3b3f1652016-10-13 22:44:48 +05302331 for_each_engine(engine, dev_priv, id) {
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002332 struct intel_context *ce = &ctx->engine[engine->id];
2333 u32 *reg;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002334
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002335 if (!ce->state)
2336 continue;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002337
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002338 reg = i915_gem_object_pin_map(ce->state->obj,
2339 I915_MAP_WB);
2340 if (WARN_ON(IS_ERR(reg)))
2341 continue;
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01002342
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002343 reg += LRC_STATE_PN * PAGE_SIZE / sizeof(*reg);
2344 reg[CTX_RING_HEAD+1] = 0;
2345 reg[CTX_RING_TAIL+1] = 0;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002346
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002347 ce->state->obj->mm.dirty = true;
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002348 i915_gem_object_unpin_map(ce->state->obj);
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002349
Chris Wilsone6ba9992017-04-25 14:00:49 +01002350 intel_ring_reset(ce->ring, 0);
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002351 }
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002352 }
2353}