blob: ec3012623697df2b78b67ff289fd9753387a60e7 [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ł Winiarskic41937f2017-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 Wilson16a87392017-12-20 09:06:26 +0000459 GEM_TRACE("%s\n", engine->name);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100460 for (n = execlists_num_ports(&engine->execlists); --n; )
Chris Wilson2fc7a062017-12-07 22:24:34 +0000461 elsp_write(0, engine->execlists.elsp);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100462
Chris Wilson2fc7a062017-12-07 22:24:34 +0000463 elsp_write(ce->lrc_desc, engine->execlists.elsp);
Michel Thierryba74cb12017-11-20 12:34:58 +0000464 execlists_clear_active(&engine->execlists, EXECLISTS_ACTIVE_HWACK);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100465}
466
Chris Wilson70c2a242016-09-09 14:11:46 +0100467static void execlists_dequeue(struct intel_engine_cs *engine)
468{
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300469 struct intel_engine_execlists * const execlists = &engine->execlists;
470 struct execlist_port *port = execlists->port;
Mika Kuoppala76e70082017-09-22 15:43:07 +0300471 const struct execlist_port * const last_port =
472 &execlists->port[execlists->port_mask];
Chris Wilsonbeecec92017-10-03 21:34:52 +0100473 struct drm_i915_gem_request *last = port_request(port);
Chris Wilson20311bd2016-11-14 20:41:03 +0000474 struct rb_node *rb;
Chris Wilson70c2a242016-09-09 14:11:46 +0100475 bool submit = false;
Michel Thierryacdd8842014-07-24 17:04:38 +0100476
Chris Wilson70c2a242016-09-09 14:11:46 +0100477 /* Hardware submission is through 2 ports. Conceptually each port
478 * has a (RING_START, RING_HEAD, RING_TAIL) tuple. RING_START is
479 * static for a context, and unique to each, so we only execute
480 * requests belonging to a single context from each ring. RING_HEAD
481 * is maintained by the CS in the context image, it marks the place
482 * where it got up to last time, and through RING_TAIL we tell the CS
483 * where we want to execute up to this time.
484 *
485 * In this list the requests are in order of execution. Consecutive
486 * requests from the same context are adjacent in the ringbuffer. We
487 * can combine these requests into a single RING_TAIL update:
488 *
489 * RING_HEAD...req1...req2
490 * ^- RING_TAIL
491 * since to execute req2 the CS must first execute req1.
492 *
493 * Our goal then is to point each port to the end of a consecutive
494 * sequence of requests as being the most optimal (fewest wake ups
495 * and context switches) submission.
496 */
497
Tvrtko Ursulin9f7886d2017-03-21 10:55:11 +0000498 spin_lock_irq(&engine->timeline->lock);
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300499 rb = execlists->first;
500 GEM_BUG_ON(rb_first(&execlists->queue) != rb);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100501 if (!rb)
502 goto unlock;
503
504 if (last) {
505 /*
506 * Don't resubmit or switch until all outstanding
507 * preemptions (lite-restore) are seen. Then we
508 * know the next preemption status we see corresponds
509 * to this ELSP update.
510 */
Michel Thierryba74cb12017-11-20 12:34:58 +0000511 GEM_BUG_ON(!port_count(&port[0]));
Chris Wilsonbeecec92017-10-03 21:34:52 +0100512 if (port_count(&port[0]) > 1)
513 goto unlock;
514
Michel Thierryba74cb12017-11-20 12:34:58 +0000515 /*
516 * If we write to ELSP a second time before the HW has had
517 * a chance to respond to the previous write, we can confuse
518 * the HW and hit "undefined behaviour". After writing to ELSP,
519 * we must then wait until we see a context-switch event from
520 * the HW to indicate that it has had a chance to respond.
521 */
522 if (!execlists_is_active(execlists, EXECLISTS_ACTIVE_HWACK))
523 goto unlock;
524
Michał Winiarskia4598d12017-10-25 22:00:18 +0200525 if (HAS_LOGICAL_RING_PREEMPTION(engine->i915) &&
Chris Wilsonbeecec92017-10-03 21:34:52 +0100526 rb_entry(rb, struct i915_priolist, node)->priority >
527 max(last->priotree.priority, 0)) {
528 /*
529 * Switch to our empty preempt context so
530 * the state of the GPU is known (idle).
531 */
532 inject_preempt_context(engine);
Chris Wilson4a118ec2017-10-23 22:32:36 +0100533 execlists_set_active(execlists,
534 EXECLISTS_ACTIVE_PREEMPT);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100535 goto unlock;
536 } else {
537 /*
538 * In theory, we could coalesce more requests onto
539 * the second port (the first port is active, with
540 * no preemptions pending). However, that means we
541 * then have to deal with the possible lite-restore
542 * of the second port (as we submit the ELSP, there
543 * may be a context-switch) but also we may complete
544 * the resubmission before the context-switch. Ergo,
545 * coalescing onto the second port will cause a
546 * preemption event, but we cannot predict whether
547 * that will affect port[0] or port[1].
548 *
549 * If the second port is already active, we can wait
550 * until the next context-switch before contemplating
551 * new requests. The GPU will be busy and we should be
552 * able to resubmit the new ELSP before it idles,
553 * avoiding pipeline bubbles (momentary pauses where
554 * the driver is unable to keep up the supply of new
555 * work).
556 */
557 if (port_count(&port[1]))
558 goto unlock;
559
560 /* WaIdleLiteRestore:bdw,skl
561 * Apply the wa NOOPs to prevent
562 * ring:HEAD == req:TAIL as we resubmit the
563 * request. See gen8_emit_breadcrumb() for
564 * where we prepare the padding after the
565 * end of the request.
566 */
567 last->tail = last->wa_tail;
568 }
569 }
570
571 do {
Chris Wilson6c067572017-05-17 13:10:03 +0100572 struct i915_priolist *p = rb_entry(rb, typeof(*p), node);
573 struct drm_i915_gem_request *rq, *rn;
Chris Wilson20311bd2016-11-14 20:41:03 +0000574
Chris Wilson6c067572017-05-17 13:10:03 +0100575 list_for_each_entry_safe(rq, rn, &p->requests, priotree.link) {
576 /*
577 * Can we combine this request with the current port?
578 * It has to be the same context/ringbuffer and not
579 * have any exceptions (e.g. GVT saying never to
580 * combine contexts).
581 *
582 * If we can combine the requests, we can execute both
583 * by updating the RING_TAIL to point to the end of the
584 * second request, and so we never need to tell the
585 * hardware about the first.
Chris Wilson70c2a242016-09-09 14:11:46 +0100586 */
Chris Wilson6c067572017-05-17 13:10:03 +0100587 if (last && !can_merge_ctx(rq->ctx, last->ctx)) {
588 /*
589 * If we are on the second port and cannot
590 * combine this request with the last, then we
591 * are done.
592 */
Mika Kuoppala76e70082017-09-22 15:43:07 +0300593 if (port == last_port) {
Chris Wilson6c067572017-05-17 13:10:03 +0100594 __list_del_many(&p->requests,
595 &rq->priotree.link);
596 goto done;
597 }
Chris Wilson70c2a242016-09-09 14:11:46 +0100598
Chris Wilson6c067572017-05-17 13:10:03 +0100599 /*
600 * If GVT overrides us we only ever submit
601 * port[0], leaving port[1] empty. Note that we
602 * also have to be careful that we don't queue
603 * the same context (even though a different
604 * request) to the second port.
605 */
606 if (ctx_single_port_submission(last->ctx) ||
607 ctx_single_port_submission(rq->ctx)) {
608 __list_del_many(&p->requests,
609 &rq->priotree.link);
610 goto done;
611 }
Chris Wilson70c2a242016-09-09 14:11:46 +0100612
Chris Wilson6c067572017-05-17 13:10:03 +0100613 GEM_BUG_ON(last->ctx == rq->ctx);
Chris Wilson70c2a242016-09-09 14:11:46 +0100614
Chris Wilson6c067572017-05-17 13:10:03 +0100615 if (submit)
616 port_assign(port, last);
617 port++;
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300618
619 GEM_BUG_ON(port_isset(port));
Chris Wilson6c067572017-05-17 13:10:03 +0100620 }
621
622 INIT_LIST_HEAD(&rq->priotree.link);
Chris Wilson6c067572017-05-17 13:10:03 +0100623 __i915_gem_request_submit(rq);
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300624 trace_i915_gem_request_in(rq, port_index(port, execlists));
Chris Wilson6c067572017-05-17 13:10:03 +0100625 last = rq;
626 submit = true;
Chris Wilson70c2a242016-09-09 14:11:46 +0100627 }
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000628
Chris Wilson20311bd2016-11-14 20:41:03 +0000629 rb = rb_next(rb);
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300630 rb_erase(&p->node, &execlists->queue);
Chris Wilson6c067572017-05-17 13:10:03 +0100631 INIT_LIST_HEAD(&p->requests);
632 if (p->priority != I915_PRIORITY_NORMAL)
Chris Wilsonc5cf9a92017-05-17 13:10:04 +0100633 kmem_cache_free(engine->i915->priorities, p);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100634 } while (rb);
Chris Wilson6c067572017-05-17 13:10:03 +0100635done:
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300636 execlists->first = rb;
Chris Wilson6c067572017-05-17 13:10:03 +0100637 if (submit)
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100638 port_assign(port, last);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100639unlock:
Tvrtko Ursulin9f7886d2017-03-21 10:55:11 +0000640 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson70c2a242016-09-09 14:11:46 +0100641
Chris Wilson4a118ec2017-10-23 22:32:36 +0100642 if (submit) {
643 execlists_set_active(execlists, EXECLISTS_ACTIVE_USER);
Chris Wilson70c2a242016-09-09 14:11:46 +0100644 execlists_submit_ports(engine);
Chris Wilson4a118ec2017-10-23 22:32:36 +0100645 }
Michel Thierryacdd8842014-07-24 17:04:38 +0100646}
647
Michał Winiarskic41937f2017-10-26 15:35:58 +0200648void
Michał Winiarskia4598d12017-10-25 22:00:18 +0200649execlists_cancel_port_requests(struct intel_engine_execlists * const execlists)
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300650{
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100651 struct execlist_port *port = execlists->port;
Mika Kuoppaladc2279e2017-10-10 14:48:57 +0300652 unsigned int num_ports = execlists_num_ports(execlists);
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300653
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100654 while (num_ports-- && port_isset(port)) {
Chris Wilson7e44fc22017-09-26 11:17:19 +0100655 struct drm_i915_gem_request *rq = port_request(port);
656
Chris Wilson4a118ec2017-10-23 22:32:36 +0100657 GEM_BUG_ON(!execlists->active);
Tvrtko Ursulin30e17b72017-11-21 18:18:48 +0000658 intel_engine_context_out(rq->engine);
Chris Wilsond6c05112017-10-03 21:34:47 +0100659 execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_PREEMPTED);
Chris Wilson7e44fc22017-09-26 11:17:19 +0100660 i915_gem_request_put(rq);
661
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100662 memset(port, 0, sizeof(*port));
663 port++;
664 }
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300665}
666
Chris Wilson27a5f612017-09-15 18:31:00 +0100667static void execlists_cancel_requests(struct intel_engine_cs *engine)
668{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300669 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilson27a5f612017-09-15 18:31:00 +0100670 struct drm_i915_gem_request *rq, *rn;
671 struct rb_node *rb;
672 unsigned long flags;
Chris Wilson27a5f612017-09-15 18:31:00 +0100673
674 spin_lock_irqsave(&engine->timeline->lock, flags);
675
676 /* Cancel the requests on the HW and clear the ELSP tracker. */
Michał Winiarskia4598d12017-10-25 22:00:18 +0200677 execlists_cancel_port_requests(execlists);
Chris Wilson27a5f612017-09-15 18:31:00 +0100678
679 /* Mark all executing requests as skipped. */
680 list_for_each_entry(rq, &engine->timeline->requests, link) {
681 GEM_BUG_ON(!rq->global_seqno);
682 if (!i915_gem_request_completed(rq))
683 dma_fence_set_error(&rq->fence, -EIO);
684 }
685
686 /* Flush the queued requests to the timeline list (for retiring). */
Mika Kuoppalab620e872017-09-22 15:43:03 +0300687 rb = execlists->first;
Chris Wilson27a5f612017-09-15 18:31:00 +0100688 while (rb) {
689 struct i915_priolist *p = rb_entry(rb, typeof(*p), node);
690
691 list_for_each_entry_safe(rq, rn, &p->requests, priotree.link) {
692 INIT_LIST_HEAD(&rq->priotree.link);
Chris Wilson27a5f612017-09-15 18:31:00 +0100693
694 dma_fence_set_error(&rq->fence, -EIO);
695 __i915_gem_request_submit(rq);
696 }
697
698 rb = rb_next(rb);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300699 rb_erase(&p->node, &execlists->queue);
Chris Wilson27a5f612017-09-15 18:31:00 +0100700 INIT_LIST_HEAD(&p->requests);
701 if (p->priority != I915_PRIORITY_NORMAL)
702 kmem_cache_free(engine->i915->priorities, p);
703 }
704
705 /* Remaining _unready_ requests will be nop'ed when submitted */
706
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300707
Mika Kuoppalab620e872017-09-22 15:43:03 +0300708 execlists->queue = RB_ROOT;
709 execlists->first = NULL;
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100710 GEM_BUG_ON(port_isset(execlists->port));
Chris Wilson27a5f612017-09-15 18:31:00 +0100711
712 /*
713 * The port is checked prior to scheduling a tasklet, but
714 * just in case we have suspended the tasklet to do the
715 * wedging make sure that when it wakes, it decides there
716 * is no work to do by clearing the irq_posted bit.
717 */
718 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
719
720 spin_unlock_irqrestore(&engine->timeline->lock, flags);
721}
722
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200723/*
Oscar Mateo73e4d072014-07-24 17:04:48 +0100724 * Check the unread Context Status Buffers and manage the submission of new
725 * contexts to the ELSP accordingly.
726 */
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +0530727static void execlists_submission_tasklet(unsigned long data)
Thomas Daniele981e7b2014-07-24 17:04:39 +0100728{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300729 struct intel_engine_cs * const engine = (struct intel_engine_cs *)data;
730 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilsonbeecec92017-10-03 21:34:52 +0100731 struct execlist_port * const port = execlists->port;
Chris Wilsonc0336662016-05-06 15:40:21 +0100732 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonbb5db7e2018-01-22 10:07:14 +0000733 bool fw = false;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100734
Chris Wilson48921262017-04-11 18:58:50 +0100735 /* We can skip acquiring intel_runtime_pm_get() here as it was taken
736 * on our behalf by the request (see i915_gem_mark_busy()) and it will
737 * not be relinquished until the device is idle (see
738 * i915_gem_idle_work_handler()). As a precaution, we make sure
739 * that all ELSP are drained i.e. we have processed the CSB,
740 * before allowing ourselves to idle and calling intel_runtime_pm_put().
741 */
742 GEM_BUG_ON(!dev_priv->gt.awake);
743
Chris Wilson899f6202017-03-21 11:33:20 +0000744 /* Prefer doing test_and_clear_bit() as a two stage operation to avoid
745 * imposing the cost of a locked atomic transaction when submitting a
746 * new request (outside of the context-switch interrupt).
747 */
748 while (test_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted)) {
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100749 /* The HWSP contains a (cacheable) mirror of the CSB */
750 const u32 *buf =
751 &engine->status_page.page_addr[I915_HWS_CSB_BUF0_INDEX];
Chris Wilson4af0d722017-03-25 20:10:53 +0000752 unsigned int head, tail;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100753
Mika Kuoppalab620e872017-09-22 15:43:03 +0300754 if (unlikely(execlists->csb_use_mmio)) {
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100755 buf = (u32 * __force)
756 (dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_BUF_LO(engine, 0)));
Mika Kuoppalab620e872017-09-22 15:43:03 +0300757 execlists->csb_head = -1; /* force mmio read of CSB ptrs */
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100758 }
759
Chris Wilson2e70b8c2017-03-23 13:48:03 +0000760 /* The write will be ordered by the uncached read (itself
761 * a memory barrier), so we do not need another in the form
762 * of a locked instruction. The race between the interrupt
763 * handler and the split test/clear is harmless as we order
764 * our clear before the CSB read. If the interrupt arrived
765 * first between the test and the clear, we read the updated
766 * CSB and clear the bit. If the interrupt arrives as we read
767 * the CSB or later (i.e. after we had cleared the bit) the bit
768 * is set and we do a new loop.
769 */
770 __clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300771 if (unlikely(execlists->csb_head == -1)) { /* following a reset */
Chris Wilsonbb5db7e2018-01-22 10:07:14 +0000772 if (!fw) {
773 intel_uncore_forcewake_get(dev_priv,
774 execlists->fw_domains);
775 fw = true;
776 }
777
Chris Wilson767a9832017-09-13 09:56:05 +0100778 head = readl(dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)));
779 tail = GEN8_CSB_WRITE_PTR(head);
780 head = GEN8_CSB_READ_PTR(head);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300781 execlists->csb_head = head;
Chris Wilson767a9832017-09-13 09:56:05 +0100782 } else {
783 const int write_idx =
784 intel_hws_csb_write_index(dev_priv) -
785 I915_HWS_CSB_BUF0_INDEX;
786
Mika Kuoppalab620e872017-09-22 15:43:03 +0300787 head = execlists->csb_head;
Chris Wilson767a9832017-09-13 09:56:05 +0100788 tail = READ_ONCE(buf[write_idx]);
789 }
Chris Wilsonbb5db7e2018-01-22 10:07:14 +0000790 GEM_TRACE("%s cs-irq head=%d [%d%s], tail=%d [%d%s]\n",
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000791 engine->name,
Chris Wilsonbb5db7e2018-01-22 10:07:14 +0000792 head, GEN8_CSB_READ_PTR(readl(dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)))), fw ? "" : "?",
793 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 +0300794
Chris Wilson4af0d722017-03-25 20:10:53 +0000795 while (head != tail) {
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100796 struct drm_i915_gem_request *rq;
Chris Wilson4af0d722017-03-25 20:10:53 +0000797 unsigned int status;
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100798 unsigned int count;
Chris Wilsona37951a2017-01-24 11:00:06 +0000799
Chris Wilson4af0d722017-03-25 20:10:53 +0000800 if (++head == GEN8_CSB_ENTRIES)
801 head = 0;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100802
Chris Wilson2ffe80a2017-02-06 17:05:02 +0000803 /* We are flying near dragons again.
804 *
805 * We hold a reference to the request in execlist_port[]
806 * but no more than that. We are operating in softirq
807 * context and so cannot hold any mutex or sleep. That
808 * prevents us stopping the requests we are processing
809 * in port[] from being retired simultaneously (the
810 * breadcrumb will be complete before we see the
811 * context-switch). As we only hold the reference to the
812 * request, any pointer chasing underneath the request
813 * is subject to a potential use-after-free. Thus we
814 * store all of the bookkeeping within port[] as
815 * required, and avoid using unguarded pointers beneath
816 * request itself. The same applies to the atomic
817 * status notifier.
818 */
819
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100820 status = READ_ONCE(buf[2 * head]); /* maybe mmio! */
Chris Wilson193a98d2017-12-22 13:27:42 +0000821 GEM_TRACE("%s csb[%d]: status=0x%08x:0x%08x, active=0x%x\n",
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000822 engine->name, head,
Chris Wilson193a98d2017-12-22 13:27:42 +0000823 status, buf[2*head + 1],
824 execlists->active);
Michel Thierryba74cb12017-11-20 12:34:58 +0000825
826 if (status & (GEN8_CTX_STATUS_IDLE_ACTIVE |
827 GEN8_CTX_STATUS_PREEMPTED))
828 execlists_set_active(execlists,
829 EXECLISTS_ACTIVE_HWACK);
830 if (status & GEN8_CTX_STATUS_ACTIVE_IDLE)
831 execlists_clear_active(execlists,
832 EXECLISTS_ACTIVE_HWACK);
833
Chris Wilson70c2a242016-09-09 14:11:46 +0100834 if (!(status & GEN8_CTX_STATUS_COMPLETED_MASK))
835 continue;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100836
Chris Wilson1f5f9ed2017-11-20 12:34:57 +0000837 /* We should never get a COMPLETED | IDLE_ACTIVE! */
838 GEM_BUG_ON(status & GEN8_CTX_STATUS_IDLE_ACTIVE);
839
Chris Wilsone40dd222017-11-20 12:34:55 +0000840 if (status & GEN8_CTX_STATUS_COMPLETE &&
Chris Wilsonbeecec92017-10-03 21:34:52 +0100841 buf[2*head + 1] == PREEMPT_ID) {
Chris Wilson193a98d2017-12-22 13:27:42 +0000842 GEM_TRACE("%s preempt-idle\n", engine->name);
843
Michał Winiarskia4598d12017-10-25 22:00:18 +0200844 execlists_cancel_port_requests(execlists);
845 execlists_unwind_incomplete_requests(execlists);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100846
Chris Wilson4a118ec2017-10-23 22:32:36 +0100847 GEM_BUG_ON(!execlists_is_active(execlists,
848 EXECLISTS_ACTIVE_PREEMPT));
849 execlists_clear_active(execlists,
850 EXECLISTS_ACTIVE_PREEMPT);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100851 continue;
852 }
853
854 if (status & GEN8_CTX_STATUS_PREEMPTED &&
Chris Wilson4a118ec2017-10-23 22:32:36 +0100855 execlists_is_active(execlists,
856 EXECLISTS_ACTIVE_PREEMPT))
Chris Wilsonbeecec92017-10-03 21:34:52 +0100857 continue;
858
Chris Wilson4a118ec2017-10-23 22:32:36 +0100859 GEM_BUG_ON(!execlists_is_active(execlists,
860 EXECLISTS_ACTIVE_USER));
861
Chris Wilson86aa7e72017-01-23 11:31:32 +0000862 /* Check the context/desc id for this event matches */
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100863 GEM_DEBUG_BUG_ON(buf[2 * head + 1] != port->context_id);
Chris Wilson86aa7e72017-01-23 11:31:32 +0000864
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100865 rq = port_unpack(port, &count);
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000866 GEM_TRACE("%s out[0]: ctx=%d.%d, seqno=%x\n",
867 engine->name,
Chris Wilson16c86192017-12-19 22:09:16 +0000868 port->context_id, count,
Chris Wilson16a87392017-12-20 09:06:26 +0000869 rq ? rq->global_seqno : 0);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100870 GEM_BUG_ON(count == 0);
871 if (--count == 0) {
Chris Wilson70c2a242016-09-09 14:11:46 +0100872 GEM_BUG_ON(status & GEN8_CTX_STATUS_PREEMPTED);
Chris Wilsond8747af2017-11-20 12:34:56 +0000873 GEM_BUG_ON(port_isset(&port[1]) &&
874 !(status & GEN8_CTX_STATUS_ELEMENT_SWITCH));
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100875 GEM_BUG_ON(!i915_gem_request_completed(rq));
Tvrtko Ursulin73fd9d32017-11-21 18:18:47 +0000876 execlists_context_schedule_out(rq);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100877 trace_i915_gem_request_out(rq);
878 i915_gem_request_put(rq);
879
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300880 execlists_port_complete(execlists, port);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100881 } else {
882 port_set(port, port_pack(rq, count));
Chris Wilson70c2a242016-09-09 14:11:46 +0100883 }
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000884
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100885 /* After the final element, the hw should be idle */
886 GEM_BUG_ON(port_count(port) == 0 &&
Chris Wilson70c2a242016-09-09 14:11:46 +0100887 !(status & GEN8_CTX_STATUS_ACTIVE_IDLE));
Chris Wilson4a118ec2017-10-23 22:32:36 +0100888 if (port_count(port) == 0)
889 execlists_clear_active(execlists,
890 EXECLISTS_ACTIVE_USER);
Chris Wilson4af0d722017-03-25 20:10:53 +0000891 }
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000892
Mika Kuoppalab620e872017-09-22 15:43:03 +0300893 if (head != execlists->csb_head) {
894 execlists->csb_head = head;
Chris Wilson767a9832017-09-13 09:56:05 +0100895 writel(_MASKED_FIELD(GEN8_CSB_READ_PTR_MASK, head << 8),
896 dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)));
897 }
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000898 }
899
Chris Wilson4a118ec2017-10-23 22:32:36 +0100900 if (!execlists_is_active(execlists, EXECLISTS_ACTIVE_PREEMPT))
Chris Wilson70c2a242016-09-09 14:11:46 +0100901 execlists_dequeue(engine);
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000902
Chris Wilsonbb5db7e2018-01-22 10:07:14 +0000903 if (fw)
904 intel_uncore_forcewake_put(dev_priv, execlists->fw_domains);
Thomas Daniele981e7b2014-07-24 17:04:39 +0100905}
906
Chris Wilson27606fd2017-09-16 21:44:13 +0100907static void insert_request(struct intel_engine_cs *engine,
908 struct i915_priotree *pt,
909 int prio)
910{
911 struct i915_priolist *p = lookup_priolist(engine, pt, prio);
912
913 list_add_tail(&pt->link, &ptr_mask_bits(p, 1)->requests);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100914 if (ptr_unmask_bits(p, 1))
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +0530915 tasklet_hi_schedule(&engine->execlists.tasklet);
Chris Wilson27606fd2017-09-16 21:44:13 +0100916}
917
Chris Wilsonf4ea6bd2016-08-02 22:50:32 +0100918static void execlists_submit_request(struct drm_i915_gem_request *request)
Michel Thierryacdd8842014-07-24 17:04:38 +0100919{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000920 struct intel_engine_cs *engine = request->engine;
Chris Wilson5590af32016-09-09 14:11:54 +0100921 unsigned long flags;
Michel Thierryacdd8842014-07-24 17:04:38 +0100922
Chris Wilson663f71e2016-11-14 20:41:00 +0000923 /* Will be called from irq-context when using foreign fences. */
924 spin_lock_irqsave(&engine->timeline->lock, flags);
Michel Thierryacdd8842014-07-24 17:04:38 +0100925
Chris Wilson27606fd2017-09-16 21:44:13 +0100926 insert_request(engine, &request->priotree, request->priotree.priority);
Michel Thierryacdd8842014-07-24 17:04:38 +0100927
Mika Kuoppalab620e872017-09-22 15:43:03 +0300928 GEM_BUG_ON(!engine->execlists.first);
Chris Wilson6c067572017-05-17 13:10:03 +0100929 GEM_BUG_ON(list_empty(&request->priotree.link));
930
Chris Wilson663f71e2016-11-14 20:41:00 +0000931 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Michel Thierryacdd8842014-07-24 17:04:38 +0100932}
933
Chris Wilson1f181222017-10-03 21:34:50 +0100934static struct drm_i915_gem_request *pt_to_request(struct i915_priotree *pt)
935{
936 return container_of(pt, struct drm_i915_gem_request, priotree);
937}
938
Chris Wilson20311bd2016-11-14 20:41:03 +0000939static struct intel_engine_cs *
940pt_lock_engine(struct i915_priotree *pt, struct intel_engine_cs *locked)
941{
Chris Wilson1f181222017-10-03 21:34:50 +0100942 struct intel_engine_cs *engine = pt_to_request(pt)->engine;
Chris Wilson20311bd2016-11-14 20:41:03 +0000943
Chris Wilsona79a5242017-03-27 21:21:43 +0100944 GEM_BUG_ON(!locked);
945
Chris Wilson20311bd2016-11-14 20:41:03 +0000946 if (engine != locked) {
Chris Wilsona79a5242017-03-27 21:21:43 +0100947 spin_unlock(&locked->timeline->lock);
948 spin_lock(&engine->timeline->lock);
Chris Wilson20311bd2016-11-14 20:41:03 +0000949 }
950
951 return engine;
952}
953
954static void execlists_schedule(struct drm_i915_gem_request *request, int prio)
955{
Chris Wilsona79a5242017-03-27 21:21:43 +0100956 struct intel_engine_cs *engine;
Chris Wilson20311bd2016-11-14 20:41:03 +0000957 struct i915_dependency *dep, *p;
958 struct i915_dependency stack;
959 LIST_HEAD(dfs);
960
Chris Wilson7d1ea602017-09-28 20:39:00 +0100961 GEM_BUG_ON(prio == I915_PRIORITY_INVALID);
962
Chris Wilsonc218ee02018-01-06 10:56:18 +0000963 if (i915_gem_request_completed(request))
964 return;
965
Chris Wilson20311bd2016-11-14 20:41:03 +0000966 if (prio <= READ_ONCE(request->priotree.priority))
967 return;
968
Chris Wilson70cd1472016-11-28 14:36:49 +0000969 /* Need BKL in order to use the temporary link inside i915_dependency */
970 lockdep_assert_held(&request->i915->drm.struct_mutex);
Chris Wilson20311bd2016-11-14 20:41:03 +0000971
972 stack.signaler = &request->priotree;
973 list_add(&stack.dfs_link, &dfs);
974
Chris Wilsonce01b172018-01-02 15:12:26 +0000975 /*
976 * Recursively bump all dependent priorities to match the new request.
Chris Wilson20311bd2016-11-14 20:41:03 +0000977 *
978 * A naive approach would be to use recursion:
979 * static void update_priorities(struct i915_priotree *pt, prio) {
980 * list_for_each_entry(dep, &pt->signalers_list, signal_link)
981 * update_priorities(dep->signal, prio)
982 * insert_request(pt);
983 * }
984 * but that may have unlimited recursion depth and so runs a very
985 * real risk of overunning the kernel stack. Instead, we build
986 * a flat list of all dependencies starting with the current request.
987 * As we walk the list of dependencies, we add all of its dependencies
988 * to the end of the list (this may include an already visited
989 * request) and continue to walk onwards onto the new dependencies. The
990 * end result is a topological list of requests in reverse order, the
991 * last element in the list is the request we must execute first.
992 */
Chris Wilson2221c5b2018-01-02 15:12:27 +0000993 list_for_each_entry(dep, &dfs, dfs_link) {
Chris Wilson20311bd2016-11-14 20:41:03 +0000994 struct i915_priotree *pt = dep->signaler;
995
Chris Wilsonce01b172018-01-02 15:12:26 +0000996 /*
997 * Within an engine, there can be no cycle, but we may
Chris Wilsona79a5242017-03-27 21:21:43 +0100998 * refer to the same dependency chain multiple times
999 * (redundant dependencies are not eliminated) and across
1000 * engines.
1001 */
1002 list_for_each_entry(p, &pt->signalers_list, signal_link) {
Chris Wilsonce01b172018-01-02 15:12:26 +00001003 GEM_BUG_ON(p == dep); /* no cycles! */
1004
Chris Wilson83cc84c2018-01-02 15:12:25 +00001005 if (i915_priotree_signaled(p->signaler))
Chris Wilson1f181222017-10-03 21:34:50 +01001006 continue;
1007
Chris Wilsona79a5242017-03-27 21:21:43 +01001008 GEM_BUG_ON(p->signaler->priority < pt->priority);
Chris Wilson20311bd2016-11-14 20:41:03 +00001009 if (prio > READ_ONCE(p->signaler->priority))
1010 list_move_tail(&p->dfs_link, &dfs);
Chris Wilsona79a5242017-03-27 21:21:43 +01001011 }
Chris Wilson20311bd2016-11-14 20:41:03 +00001012 }
1013
Chris Wilsonce01b172018-01-02 15:12:26 +00001014 /*
1015 * If we didn't need to bump any existing priorities, and we haven't
Chris Wilson349bdb62017-05-17 13:10:05 +01001016 * yet submitted this request (i.e. there is no potential race with
1017 * execlists_submit_request()), we can set our own priority and skip
1018 * acquiring the engine locks.
1019 */
Chris Wilson7d1ea602017-09-28 20:39:00 +01001020 if (request->priotree.priority == I915_PRIORITY_INVALID) {
Chris Wilson349bdb62017-05-17 13:10:05 +01001021 GEM_BUG_ON(!list_empty(&request->priotree.link));
1022 request->priotree.priority = prio;
1023 if (stack.dfs_link.next == stack.dfs_link.prev)
1024 return;
1025 __list_del_entry(&stack.dfs_link);
1026 }
1027
Chris Wilsona79a5242017-03-27 21:21:43 +01001028 engine = request->engine;
1029 spin_lock_irq(&engine->timeline->lock);
1030
Chris Wilson20311bd2016-11-14 20:41:03 +00001031 /* Fifo and depth-first replacement ensure our deps execute before us */
1032 list_for_each_entry_safe_reverse(dep, p, &dfs, dfs_link) {
1033 struct i915_priotree *pt = dep->signaler;
1034
1035 INIT_LIST_HEAD(&dep->dfs_link);
1036
1037 engine = pt_lock_engine(pt, engine);
1038
1039 if (prio <= pt->priority)
1040 continue;
1041
Chris Wilson20311bd2016-11-14 20:41:03 +00001042 pt->priority = prio;
Chris Wilson6c067572017-05-17 13:10:03 +01001043 if (!list_empty(&pt->link)) {
1044 __list_del_entry(&pt->link);
1045 insert_request(engine, pt, prio);
Chris Wilsona79a5242017-03-27 21:21:43 +01001046 }
Chris Wilson20311bd2016-11-14 20:41:03 +00001047 }
1048
Chris Wilsona79a5242017-03-27 21:21:43 +01001049 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson20311bd2016-11-14 20:41:03 +00001050}
1051
Chris Wilsonf4e15af2017-11-10 14:26:32 +00001052static int __context_pin(struct i915_gem_context *ctx, struct i915_vma *vma)
1053{
1054 unsigned int flags;
1055 int err;
1056
1057 /*
1058 * Clear this page out of any CPU caches for coherent swap-in/out.
1059 * We only want to do this on the first bind so that we do not stall
1060 * on an active context (which by nature is already on the GPU).
1061 */
1062 if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
1063 err = i915_gem_object_set_to_gtt_domain(vma->obj, true);
1064 if (err)
1065 return err;
1066 }
1067
1068 flags = PIN_GLOBAL | PIN_HIGH;
1069 if (ctx->ggtt_offset_bias)
1070 flags |= PIN_OFFSET_BIAS | ctx->ggtt_offset_bias;
1071
1072 return i915_vma_pin(vma, 0, GEN8_LR_CONTEXT_ALIGN, flags);
1073}
1074
Chris Wilson266a2402017-05-04 10:33:08 +01001075static struct intel_ring *
1076execlists_context_pin(struct intel_engine_cs *engine,
1077 struct i915_gem_context *ctx)
Oscar Mateodcb4c122014-11-13 10:28:10 +00001078{
Chris Wilson9021ad02016-05-24 14:53:37 +01001079 struct intel_context *ce = &ctx->engine[engine->id];
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001080 void *vaddr;
Tvrtko Ursulinca825802016-01-15 15:10:27 +00001081 int ret;
Oscar Mateodcb4c122014-11-13 10:28:10 +00001082
Chris Wilson91c8a322016-07-05 10:40:23 +01001083 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Tvrtko Ursulinca825802016-01-15 15:10:27 +00001084
Chris Wilson266a2402017-05-04 10:33:08 +01001085 if (likely(ce->pin_count++))
1086 goto out;
Chris Wilsona533b4b2017-03-16 17:16:28 +00001087 GEM_BUG_ON(!ce->pin_count); /* no overflow please! */
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001088
Chris Wilsone8a9c582016-12-18 15:37:20 +00001089 if (!ce->state) {
1090 ret = execlists_context_deferred_alloc(ctx, engine);
1091 if (ret)
1092 goto err;
1093 }
Chris Wilson56f6e0a2017-01-05 15:30:20 +00001094 GEM_BUG_ON(!ce->state);
Chris Wilsone8a9c582016-12-18 15:37:20 +00001095
Chris Wilsonf4e15af2017-11-10 14:26:32 +00001096 ret = __context_pin(ctx, ce->state);
Nick Hoathe84fe802015-09-11 12:53:46 +01001097 if (ret)
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001098 goto err;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001099
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001100 vaddr = i915_gem_object_pin_map(ce->state->obj, I915_MAP_WB);
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001101 if (IS_ERR(vaddr)) {
1102 ret = PTR_ERR(vaddr);
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001103 goto unpin_vma;
Tvrtko Ursulin82352e92016-01-15 17:12:45 +00001104 }
1105
Chris Wilsond822bb12017-04-03 12:34:25 +01001106 ret = intel_ring_pin(ce->ring, ctx->i915, ctx->ggtt_offset_bias);
Nick Hoathe84fe802015-09-11 12:53:46 +01001107 if (ret)
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001108 goto unpin_map;
Alex Daid1675192015-08-12 15:43:43 +01001109
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001110 intel_lr_context_descriptor_update(ctx, engine);
Chris Wilson9021ad02016-05-24 14:53:37 +01001111
Chris Wilsona3aabe82016-10-04 21:11:26 +01001112 ce->lrc_reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
1113 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001114 i915_ggtt_offset(ce->ring->vma);
Chris Wilsona3aabe82016-10-04 21:11:26 +01001115
Chris Wilson3d574a62017-10-13 21:26:16 +01001116 ce->state->obj->pin_global++;
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001117 i915_gem_context_get(ctx);
Chris Wilson266a2402017-05-04 10:33:08 +01001118out:
1119 return ce->ring;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001120
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001121unpin_map:
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001122 i915_gem_object_unpin_map(ce->state->obj);
1123unpin_vma:
1124 __i915_vma_unpin(ce->state);
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001125err:
Chris Wilson9021ad02016-05-24 14:53:37 +01001126 ce->pin_count = 0;
Chris Wilson266a2402017-05-04 10:33:08 +01001127 return ERR_PTR(ret);
Oscar Mateodcb4c122014-11-13 10:28:10 +00001128}
1129
Chris Wilsone8a9c582016-12-18 15:37:20 +00001130static void execlists_context_unpin(struct intel_engine_cs *engine,
1131 struct i915_gem_context *ctx)
Oscar Mateodcb4c122014-11-13 10:28:10 +00001132{
Chris Wilson9021ad02016-05-24 14:53:37 +01001133 struct intel_context *ce = &ctx->engine[engine->id];
Daniel Vetteraf3302b2015-12-04 17:27:15 +01001134
Chris Wilson91c8a322016-07-05 10:40:23 +01001135 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilson9021ad02016-05-24 14:53:37 +01001136 GEM_BUG_ON(ce->pin_count == 0);
Tvrtko Ursulin321fe302016-01-28 10:29:55 +00001137
Chris Wilson9021ad02016-05-24 14:53:37 +01001138 if (--ce->pin_count)
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001139 return;
1140
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001141 intel_ring_unpin(ce->ring);
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001142
Chris Wilson3d574a62017-10-13 21:26:16 +01001143 ce->state->obj->pin_global--;
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001144 i915_gem_object_unpin_map(ce->state->obj);
1145 i915_vma_unpin(ce->state);
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001146
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001147 i915_gem_context_put(ctx);
Oscar Mateodcb4c122014-11-13 10:28:10 +00001148}
1149
Chris Wilsonf73e7392016-12-18 15:37:24 +00001150static int execlists_request_alloc(struct drm_i915_gem_request *request)
Chris Wilsonef11c012016-12-18 15:37:19 +00001151{
1152 struct intel_engine_cs *engine = request->engine;
1153 struct intel_context *ce = &request->ctx->engine[engine->id];
Chris Wilsonfd138212017-11-15 15:12:04 +00001154 int ret;
Chris Wilsonef11c012016-12-18 15:37:19 +00001155
Chris Wilsone8a9c582016-12-18 15:37:20 +00001156 GEM_BUG_ON(!ce->pin_count);
1157
Chris Wilsonef11c012016-12-18 15:37:19 +00001158 /* Flush enough space to reduce the likelihood of waiting after
1159 * we start building the request - in which case we will just
1160 * have to repeat work.
1161 */
1162 request->reserved_space += EXECLISTS_REQUEST_SIZE;
1163
Chris Wilsonfd138212017-11-15 15:12:04 +00001164 ret = intel_ring_wait_for_space(request->ring, request->reserved_space);
1165 if (ret)
1166 return ret;
Chris Wilsonef11c012016-12-18 15:37:19 +00001167
Chris Wilsonef11c012016-12-18 15:37:19 +00001168 /* Note that after this point, we have committed to using
1169 * this request as it is being used to both track the
1170 * state of engine initialisation and liveness of the
1171 * golden renderstate above. Think twice before you try
1172 * to cancel/unwind this request now.
1173 */
1174
1175 request->reserved_space -= EXECLISTS_REQUEST_SIZE;
1176 return 0;
Chris Wilsonef11c012016-12-18 15:37:19 +00001177}
1178
Arun Siluvery9e000842015-07-03 14:27:31 +01001179/*
1180 * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
1181 * PIPE_CONTROL instruction. This is required for the flush to happen correctly
1182 * but there is a slight complication as this is applied in WA batch where the
1183 * values are only initialized once so we cannot take register value at the
1184 * beginning and reuse it further; hence we save its value to memory, upload a
1185 * constant value with bit21 set and then we restore it back with the saved value.
1186 * To simplify the WA, a constant value is formed by using the default value
1187 * of this register. This shouldn't be a problem because we are only modifying
1188 * it for a short period and this batch in non-premptible. We can ofcourse
1189 * use additional instructions that read the actual value of the register
1190 * at that time and set our bit of interest but it makes the WA complicated.
1191 *
1192 * This WA is also required for Gen9 so extracting as a function avoids
1193 * code duplication.
1194 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001195static u32 *
1196gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery9e000842015-07-03 14:27:31 +01001197{
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001198 *batch++ = MI_STORE_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1199 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1200 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
1201 *batch++ = 0;
Arun Siluvery9e000842015-07-03 14:27:31 +01001202
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001203 *batch++ = MI_LOAD_REGISTER_IMM(1);
1204 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1205 *batch++ = 0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES;
Arun Siluvery9e000842015-07-03 14:27:31 +01001206
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001207 batch = gen8_emit_pipe_control(batch,
1208 PIPE_CONTROL_CS_STALL |
1209 PIPE_CONTROL_DC_FLUSH_ENABLE,
1210 0);
Arun Siluvery9e000842015-07-03 14:27:31 +01001211
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001212 *batch++ = MI_LOAD_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1213 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1214 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
1215 *batch++ = 0;
Arun Siluvery9e000842015-07-03 14:27:31 +01001216
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001217 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001218}
1219
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001220/*
1221 * Typically we only have one indirect_ctx and per_ctx batch buffer which are
1222 * initialized at the beginning and shared across all contexts but this field
1223 * helps us to have multiple batches at different offsets and select them based
1224 * on a criteria. At the moment this batch always start at the beginning of the page
1225 * and at this point we don't have multiple wa_ctx batch buffers.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001226 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001227 * The number of WA applied are not known at the beginning; we use this field
1228 * to return the no of DWORDS written.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001229 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001230 * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
1231 * so it adds NOOPs as padding to make it cacheline aligned.
1232 * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
1233 * makes a complete batch buffer.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001234 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001235static u32 *gen8_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001236{
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001237 /* WaDisableCtxRestoreArbitration:bdw,chv */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001238 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001239
Arun Siluveryc82435b2015-06-19 18:37:13 +01001240 /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001241 if (IS_BROADWELL(engine->i915))
1242 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
Arun Siluveryc82435b2015-06-19 18:37:13 +01001243
Arun Siluvery0160f052015-06-23 15:46:57 +01001244 /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
1245 /* Actual scratch location is at 128 bytes offset */
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001246 batch = gen8_emit_pipe_control(batch,
1247 PIPE_CONTROL_FLUSH_L3 |
1248 PIPE_CONTROL_GLOBAL_GTT_IVB |
1249 PIPE_CONTROL_CS_STALL |
1250 PIPE_CONTROL_QW_WRITE,
1251 i915_ggtt_offset(engine->scratch) +
1252 2 * CACHELINE_BYTES);
Arun Siluvery0160f052015-06-23 15:46:57 +01001253
Chris Wilsonbeecec92017-10-03 21:34:52 +01001254 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1255
Arun Siluvery17ee9502015-06-19 19:07:01 +01001256 /* Pad to end of cacheline */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001257 while ((unsigned long)batch % CACHELINE_BYTES)
1258 *batch++ = MI_NOOP;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001259
1260 /*
1261 * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
1262 * execution depends on the length specified in terms of cache lines
1263 * in the register CTX_RCS_INDIRECT_CTX
1264 */
1265
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001266 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001267}
1268
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001269static u32 *gen9_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery0504cff2015-07-14 15:01:27 +01001270{
Chris Wilsonbeecec92017-10-03 21:34:52 +01001271 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
1272
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001273 /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt,glk */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001274 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
Arun Siluverya4106a72015-07-14 15:01:29 +01001275
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001276 /* WaDisableGatherAtSetShaderCommonSlice:skl,bxt,kbl,glk */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001277 *batch++ = MI_LOAD_REGISTER_IMM(1);
1278 *batch++ = i915_mmio_reg_offset(COMMON_SLICE_CHICKEN2);
1279 *batch++ = _MASKED_BIT_DISABLE(
1280 GEN9_DISABLE_GATHER_AT_SET_SHADER_COMMON_SLICE);
1281 *batch++ = MI_NOOP;
Mika Kuoppala873e8172016-07-20 14:26:13 +03001282
Mika Kuoppala066d4622016-06-07 17:19:15 +03001283 /* WaClearSlmSpaceAtContextSwitch:kbl */
1284 /* Actual scratch location is at 128 bytes offset */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001285 if (IS_KBL_REVID(engine->i915, 0, KBL_REVID_A0)) {
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001286 batch = gen8_emit_pipe_control(batch,
1287 PIPE_CONTROL_FLUSH_L3 |
1288 PIPE_CONTROL_GLOBAL_GTT_IVB |
1289 PIPE_CONTROL_CS_STALL |
1290 PIPE_CONTROL_QW_WRITE,
1291 i915_ggtt_offset(engine->scratch)
1292 + 2 * CACHELINE_BYTES);
Mika Kuoppala066d4622016-06-07 17:19:15 +03001293 }
Tim Gore3485d992016-07-05 10:01:30 +01001294
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001295 /* WaMediaPoolStateCmdInWABB:bxt,glk */
Tim Gore3485d992016-07-05 10:01:30 +01001296 if (HAS_POOLED_EU(engine->i915)) {
1297 /*
1298 * EU pool configuration is setup along with golden context
1299 * during context initialization. This value depends on
1300 * device type (2x6 or 3x6) and needs to be updated based
1301 * on which subslice is disabled especially for 2x6
1302 * devices, however it is safe to load default
1303 * configuration of 3x6 device instead of masking off
1304 * corresponding bits because HW ignores bits of a disabled
1305 * subslice and drops down to appropriate config. Please
1306 * see render_state_setup() in i915_gem_render_state.c for
1307 * possible configurations, to avoid duplication they are
1308 * not shown here again.
1309 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001310 *batch++ = GEN9_MEDIA_POOL_STATE;
1311 *batch++ = GEN9_MEDIA_POOL_ENABLE;
1312 *batch++ = 0x00777000;
1313 *batch++ = 0;
1314 *batch++ = 0;
1315 *batch++ = 0;
Tim Gore3485d992016-07-05 10:01:30 +01001316 }
1317
Chris Wilsonbeecec92017-10-03 21:34:52 +01001318 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1319
Arun Siluvery0504cff2015-07-14 15:01:27 +01001320 /* Pad to end of cacheline */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001321 while ((unsigned long)batch % CACHELINE_BYTES)
1322 *batch++ = MI_NOOP;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001323
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001324 return batch;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001325}
1326
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001327#define CTX_WA_BB_OBJ_SIZE (PAGE_SIZE)
1328
1329static int lrc_setup_wa_ctx(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001330{
Chris Wilson48bb74e2016-08-15 10:49:04 +01001331 struct drm_i915_gem_object *obj;
1332 struct i915_vma *vma;
1333 int err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001334
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001335 obj = i915_gem_object_create(engine->i915, CTX_WA_BB_OBJ_SIZE);
Chris Wilson48bb74e2016-08-15 10:49:04 +01001336 if (IS_ERR(obj))
1337 return PTR_ERR(obj);
1338
Chris Wilsona01cb372017-01-16 15:21:30 +00001339 vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
Chris Wilson48bb74e2016-08-15 10:49:04 +01001340 if (IS_ERR(vma)) {
1341 err = PTR_ERR(vma);
1342 goto err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001343 }
1344
Chris Wilson48bb74e2016-08-15 10:49:04 +01001345 err = i915_vma_pin(vma, 0, PAGE_SIZE, PIN_GLOBAL | PIN_HIGH);
1346 if (err)
1347 goto err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001348
Chris Wilson48bb74e2016-08-15 10:49:04 +01001349 engine->wa_ctx.vma = vma;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001350 return 0;
Chris Wilson48bb74e2016-08-15 10:49:04 +01001351
1352err:
1353 i915_gem_object_put(obj);
1354 return err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001355}
1356
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001357static void lrc_destroy_wa_ctx(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001358{
Chris Wilson19880c42016-08-15 10:49:05 +01001359 i915_vma_unpin_and_release(&engine->wa_ctx.vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001360}
1361
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001362typedef u32 *(*wa_bb_func_t)(struct intel_engine_cs *engine, u32 *batch);
1363
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001364static int intel_init_workaround_bb(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001365{
Chris Wilson48bb74e2016-08-15 10:49:04 +01001366 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001367 struct i915_wa_ctx_bb *wa_bb[2] = { &wa_ctx->indirect_ctx,
1368 &wa_ctx->per_ctx };
1369 wa_bb_func_t wa_bb_fn[2];
Arun Siluvery17ee9502015-06-19 19:07:01 +01001370 struct page *page;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001371 void *batch, *batch_ptr;
1372 unsigned int i;
Chris Wilson48bb74e2016-08-15 10:49:04 +01001373 int ret;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001374
Tvrtko Ursulin10bde232018-01-19 10:00:04 +00001375 if (GEM_WARN_ON(engine->id != RCS))
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001376 return -EINVAL;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001377
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001378 switch (INTEL_GEN(engine->i915)) {
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001379 case 10:
1380 return 0;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001381 case 9:
1382 wa_bb_fn[0] = gen9_init_indirectctx_bb;
Chris Wilsonb8aa2232017-09-21 14:54:44 +01001383 wa_bb_fn[1] = NULL;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001384 break;
1385 case 8:
1386 wa_bb_fn[0] = gen8_init_indirectctx_bb;
Chris Wilson3ad7b522017-10-03 21:34:49 +01001387 wa_bb_fn[1] = NULL;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001388 break;
1389 default:
1390 MISSING_CASE(INTEL_GEN(engine->i915));
Arun Siluvery5e60d792015-06-23 15:50:44 +01001391 return 0;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001392 }
Arun Siluvery5e60d792015-06-23 15:50:44 +01001393
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001394 ret = lrc_setup_wa_ctx(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001395 if (ret) {
1396 DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
1397 return ret;
1398 }
1399
Chris Wilson48bb74e2016-08-15 10:49:04 +01001400 page = i915_gem_object_get_dirty_page(wa_ctx->vma->obj, 0);
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001401 batch = batch_ptr = kmap_atomic(page);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001402
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001403 /*
1404 * Emit the two workaround batch buffers, recording the offset from the
1405 * start of the workaround batch buffer object for each and their
1406 * respective sizes.
1407 */
1408 for (i = 0; i < ARRAY_SIZE(wa_bb_fn); i++) {
1409 wa_bb[i]->offset = batch_ptr - batch;
1410 if (WARN_ON(!IS_ALIGNED(wa_bb[i]->offset, CACHELINE_BYTES))) {
1411 ret = -EINVAL;
1412 break;
1413 }
Chris Wilson604a8f62017-09-21 14:54:43 +01001414 if (wa_bb_fn[i])
1415 batch_ptr = wa_bb_fn[i](engine, batch_ptr);
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001416 wa_bb[i]->size = batch_ptr - (batch + wa_bb[i]->offset);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001417 }
1418
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001419 BUG_ON(batch_ptr - batch > CTX_WA_BB_OBJ_SIZE);
1420
Arun Siluvery17ee9502015-06-19 19:07:01 +01001421 kunmap_atomic(batch);
1422 if (ret)
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001423 lrc_destroy_wa_ctx(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001424
1425 return ret;
1426}
1427
Chris Wilson64f09f02017-08-07 13:19:19 +01001428static u8 gtiir[] = {
1429 [RCS] = 0,
1430 [BCS] = 0,
1431 [VCS] = 1,
1432 [VCS2] = 1,
1433 [VECS] = 3,
1434};
1435
Chris Wilsonf3c9d4072018-01-02 15:12:34 +00001436static void enable_execlists(struct intel_engine_cs *engine)
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001437{
Chris Wilsonc0336662016-05-06 15:40:21 +01001438 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilsonf3c9d4072018-01-02 15:12:34 +00001439
1440 I915_WRITE(RING_HWSTAM(engine->mmio_base), 0xffffffff);
1441 I915_WRITE(RING_MODE_GEN7(engine),
1442 _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
1443 I915_WRITE(RING_HWS_PGA(engine->mmio_base),
1444 engine->status_page.ggtt_offset);
1445 POSTING_READ(RING_HWS_PGA(engine->mmio_base));
1446}
1447
1448static int gen8_init_common_ring(struct intel_engine_cs *engine)
1449{
Mika Kuoppalab620e872017-09-22 15:43:03 +03001450 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001451 int ret;
1452
1453 ret = intel_mocs_init_engine(engine);
1454 if (ret)
1455 return ret;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001456
Chris Wilsonad07dfc2016-10-07 07:53:26 +01001457 intel_engine_reset_breadcrumbs(engine);
Chris Wilsonf3b8f912017-01-05 15:30:21 +00001458 intel_engine_init_hangcheck(engine);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001459
Chris Wilsonf3c9d4072018-01-02 15:12:34 +00001460 enable_execlists(engine);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001461 DRM_DEBUG_DRIVER("Execlists enabled for %s\n", engine->name);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001462
Chris Wilson64f09f02017-08-07 13:19:19 +01001463 GEM_BUG_ON(engine->id >= ARRAY_SIZE(gtiir));
1464
Mika Kuoppalab620e872017-09-22 15:43:03 +03001465 execlists->csb_head = -1;
Chris Wilson4a118ec2017-10-23 22:32:36 +01001466 execlists->active = 0;
Chris Wilson6b764a52017-04-25 11:38:35 +01001467
Chris Wilson64f09f02017-08-07 13:19:19 +01001468 /* After a GPU reset, we may have requests to replay */
Michał Winiarski9bdc3572017-10-25 18:25:19 +01001469 if (execlists->first)
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05301470 tasklet_schedule(&execlists->tasklet);
Chris Wilson6b764a52017-04-25 11:38:35 +01001471
Chris Wilson821ed7d2016-09-09 14:11:53 +01001472 return 0;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001473}
1474
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001475static int gen8_init_render_ring(struct intel_engine_cs *engine)
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001476{
Chris Wilsonc0336662016-05-06 15:40:21 +01001477 struct drm_i915_private *dev_priv = engine->i915;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001478 int ret;
1479
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001480 ret = gen8_init_common_ring(engine);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001481 if (ret)
1482 return ret;
1483
1484 /* We need to disable the AsyncFlip performance optimisations in order
1485 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1486 * programmed to '1' on all products.
1487 *
1488 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
1489 */
1490 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1491
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001492 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1493
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001494 return init_workarounds_ring(engine);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001495}
1496
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001497static int gen9_init_render_ring(struct intel_engine_cs *engine)
Damien Lespiau82ef8222015-02-09 19:33:08 +00001498{
1499 int ret;
1500
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001501 ret = gen8_init_common_ring(engine);
Damien Lespiau82ef8222015-02-09 19:33:08 +00001502 if (ret)
1503 return ret;
1504
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001505 return init_workarounds_ring(engine);
Damien Lespiau82ef8222015-02-09 19:33:08 +00001506}
1507
Chris Wilson42232212018-01-02 15:12:32 +00001508static void reset_irq(struct intel_engine_cs *engine)
1509{
1510 struct drm_i915_private *dev_priv = engine->i915;
1511
1512 /*
1513 * Clear any pending interrupt state.
1514 *
1515 * We do it twice out of paranoia that some of the IIR are double
1516 * buffered, and if we only reset it once there may still be
1517 * an interrupt pending.
1518 */
1519 I915_WRITE(GEN8_GT_IIR(gtiir[engine->id]),
1520 GT_CONTEXT_SWITCH_INTERRUPT << engine->irq_shift);
1521 I915_WRITE(GEN8_GT_IIR(gtiir[engine->id]),
1522 GT_CONTEXT_SWITCH_INTERRUPT << engine->irq_shift);
1523 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
1524}
1525
Chris Wilson821ed7d2016-09-09 14:11:53 +01001526static void reset_common_ring(struct intel_engine_cs *engine,
1527 struct drm_i915_gem_request *request)
1528{
Mika Kuoppalab620e872017-09-22 15:43:03 +03001529 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001530 struct intel_context *ce;
Chris Wilson221ab97192017-09-16 21:44:14 +01001531 unsigned long flags;
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001532
Chris Wilson16a87392017-12-20 09:06:26 +00001533 GEM_TRACE("%s seqno=%x\n",
1534 engine->name, request ? request->global_seqno : 0);
Chris Wilson42232212018-01-02 15:12:32 +00001535
1536 reset_irq(engine);
1537
Chris Wilson221ab97192017-09-16 21:44:14 +01001538 spin_lock_irqsave(&engine->timeline->lock, flags);
1539
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001540 /*
1541 * Catch up with any missed context-switch interrupts.
1542 *
1543 * Ideally we would just read the remaining CSB entries now that we
1544 * know the gpu is idle. However, the CSB registers are sometimes^W
1545 * often trashed across a GPU reset! Instead we have to rely on
1546 * guessing the missed context-switch events by looking at what
1547 * requests were completed.
1548 */
Michał Winiarskia4598d12017-10-25 22:00:18 +02001549 execlists_cancel_port_requests(execlists);
Chris Wilson221ab97192017-09-16 21:44:14 +01001550
1551 /* Push back any incomplete requests for replay after the reset. */
Michał Winiarskia4598d12017-10-25 22:00:18 +02001552 __unwind_incomplete_requests(engine);
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001553
Chris Wilson221ab97192017-09-16 21:44:14 +01001554 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001555
1556 /* If the request was innocent, we leave the request in the ELSP
1557 * and will try to replay it on restarting. The context image may
1558 * have been corrupted by the reset, in which case we may have
1559 * to service a new GPU hang, but more likely we can continue on
1560 * without impact.
1561 *
1562 * If the request was guilty, we presume the context is corrupt
1563 * and have to at least restore the RING register in the context
1564 * image back to the expected values to skip over the guilty request.
1565 */
Chris Wilson221ab97192017-09-16 21:44:14 +01001566 if (!request || request->fence.error != -EIO)
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001567 return;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001568
Chris Wilsona3aabe82016-10-04 21:11:26 +01001569 /* We want a simple context + ring to execute the breadcrumb update.
1570 * We cannot rely on the context being intact across the GPU hang,
1571 * so clear it and rebuild just what we need for the breadcrumb.
1572 * All pending requests for this context will be zapped, and any
1573 * future request will be after userspace has had the opportunity
1574 * to recreate its own state.
1575 */
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001576 ce = &request->ctx->engine[engine->id];
Chris Wilsona3aabe82016-10-04 21:11:26 +01001577 execlists_init_reg_state(ce->lrc_reg_state,
1578 request->ctx, engine, ce->ring);
1579
Chris Wilson821ed7d2016-09-09 14:11:53 +01001580 /* Move the RING_HEAD onto the breadcrumb, past the hanging batch */
Chris Wilsona3aabe82016-10-04 21:11:26 +01001581 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
1582 i915_ggtt_offset(ce->ring->vma);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001583 ce->lrc_reg_state[CTX_RING_HEAD+1] = request->postfix;
Chris Wilsona3aabe82016-10-04 21:11:26 +01001584
Chris Wilson821ed7d2016-09-09 14:11:53 +01001585 request->ring->head = request->postfix;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001586 intel_ring_update_space(request->ring);
1587
Chris Wilsona3aabe82016-10-04 21:11:26 +01001588 /* Reset WaIdleLiteRestore:bdw,skl as well */
Chris Wilson7e4992a2017-09-28 20:38:59 +01001589 unwind_wa_tail(request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001590}
1591
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001592static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request *req)
1593{
1594 struct i915_hw_ppgtt *ppgtt = req->ctx->ppgtt;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001595 struct intel_engine_cs *engine = req->engine;
Mika Kuoppalae7167762017-02-28 17:28:10 +02001596 const int num_lri_cmds = GEN8_3LVL_PDPES * 2;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001597 u32 *cs;
1598 int i;
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001599
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001600 cs = intel_ring_begin(req, num_lri_cmds * 2 + 2);
1601 if (IS_ERR(cs))
1602 return PTR_ERR(cs);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001603
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001604 *cs++ = MI_LOAD_REGISTER_IMM(num_lri_cmds);
Mika Kuoppalae7167762017-02-28 17:28:10 +02001605 for (i = GEN8_3LVL_PDPES - 1; i >= 0; i--) {
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001606 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
1607
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001608 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, i));
1609 *cs++ = upper_32_bits(pd_daddr);
1610 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, i));
1611 *cs++ = lower_32_bits(pd_daddr);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001612 }
1613
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001614 *cs++ = MI_NOOP;
1615 intel_ring_advance(req, cs);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001616
1617 return 0;
1618}
1619
John Harrisonbe795fc2015-05-29 17:44:03 +01001620static int gen8_emit_bb_start(struct drm_i915_gem_request *req,
Chris Wilson803688b2016-08-02 22:50:27 +01001621 u64 offset, u32 len,
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001622 const unsigned int flags)
Oscar Mateo15648582014-07-24 17:04:32 +01001623{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001624 u32 *cs;
Oscar Mateo15648582014-07-24 17:04:32 +01001625 int ret;
1626
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001627 /* Don't rely in hw updating PDPs, specially in lite-restore.
1628 * Ideally, we should set Force PD Restore in ctx descriptor,
1629 * but we can't. Force Restore would be a second option, but
1630 * it is unsafe in case of lite-restore (because the ctx is
Michel Thierry2dba3232015-07-30 11:06:23 +01001631 * not idle). PML4 is allocated during ppgtt init so this is
1632 * not needed in 48-bit.*/
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001633 if (req->ctx->ppgtt &&
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001634 (intel_engine_flag(req->engine) & req->ctx->ppgtt->pd_dirty_rings) &&
1635 !i915_vm_is_48bit(&req->ctx->ppgtt->base) &&
1636 !intel_vgpu_active(req->i915)) {
1637 ret = intel_logical_ring_emit_pdps(req);
1638 if (ret)
1639 return ret;
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001640
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00001641 req->ctx->ppgtt->pd_dirty_rings &= ~intel_engine_flag(req->engine);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001642 }
1643
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001644 cs = intel_ring_begin(req, 4);
1645 if (IS_ERR(cs))
1646 return PTR_ERR(cs);
Oscar Mateo15648582014-07-24 17:04:32 +01001647
Chris Wilson279f5a02017-10-05 20:10:05 +01001648 /*
1649 * WaDisableCtxRestoreArbitration:bdw,chv
1650 *
1651 * We don't need to perform MI_ARB_ENABLE as often as we do (in
1652 * particular all the gen that do not need the w/a at all!), if we
1653 * took care to make sure that on every switch into this context
1654 * (both ordinary and for preemption) that arbitrartion was enabled
1655 * we would be fine. However, there doesn't seem to be a downside to
1656 * being paranoid and making sure it is set before each batch and
1657 * every context-switch.
1658 *
1659 * Note that if we fail to enable arbitration before the request
1660 * is complete, then we do not see the context-switch interrupt and
1661 * the engine hangs (with RING_HEAD == RING_TAIL).
1662 *
1663 * That satisfies both the GPGPU w/a and our heavy-handed paranoia.
1664 */
Chris Wilson3ad7b522017-10-03 21:34:49 +01001665 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1666
Oscar Mateo15648582014-07-24 17:04:32 +01001667 /* FIXME(BDW): Address space and security selectors. */
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001668 *cs++ = MI_BATCH_BUFFER_START_GEN8 |
1669 (flags & I915_DISPATCH_SECURE ? 0 : BIT(8)) |
1670 (flags & I915_DISPATCH_RS ? MI_BATCH_RESOURCE_STREAMER : 0);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001671 *cs++ = lower_32_bits(offset);
1672 *cs++ = upper_32_bits(offset);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001673 intel_ring_advance(req, cs);
Oscar Mateo15648582014-07-24 17:04:32 +01001674
1675 return 0;
1676}
1677
Chris Wilson31bb59c2016-07-01 17:23:27 +01001678static void gen8_logical_ring_enable_irq(struct intel_engine_cs *engine)
Oscar Mateo73d477f2014-07-24 17:04:31 +01001679{
Chris Wilsonc0336662016-05-06 15:40:21 +01001680 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson31bb59c2016-07-01 17:23:27 +01001681 I915_WRITE_IMR(engine,
1682 ~(engine->irq_enable_mask | engine->irq_keep_mask));
1683 POSTING_READ_FW(RING_IMR(engine->mmio_base));
Oscar Mateo73d477f2014-07-24 17:04:31 +01001684}
1685
Chris Wilson31bb59c2016-07-01 17:23:27 +01001686static void gen8_logical_ring_disable_irq(struct intel_engine_cs *engine)
Oscar Mateo73d477f2014-07-24 17:04:31 +01001687{
Chris Wilsonc0336662016-05-06 15:40:21 +01001688 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson31bb59c2016-07-01 17:23:27 +01001689 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Oscar Mateo73d477f2014-07-24 17:04:31 +01001690}
1691
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001692static int gen8_emit_flush(struct drm_i915_gem_request *request, u32 mode)
Oscar Mateo47122742014-07-24 17:04:28 +01001693{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001694 u32 cmd, *cs;
Oscar Mateo47122742014-07-24 17:04:28 +01001695
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001696 cs = intel_ring_begin(request, 4);
1697 if (IS_ERR(cs))
1698 return PTR_ERR(cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001699
1700 cmd = MI_FLUSH_DW + 1;
1701
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001702 /* We always require a command barrier so that subsequent
1703 * commands, such as breadcrumb interrupts, are strictly ordered
1704 * wrt the contents of the write cache being flushed to memory
1705 * (and thus being coherent from the CPU).
1706 */
1707 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1708
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001709 if (mode & EMIT_INVALIDATE) {
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001710 cmd |= MI_INVALIDATE_TLB;
Chris Wilson1dae2df2016-08-02 22:50:19 +01001711 if (request->engine->id == VCS)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001712 cmd |= MI_INVALIDATE_BSD;
Oscar Mateo47122742014-07-24 17:04:28 +01001713 }
1714
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001715 *cs++ = cmd;
1716 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
1717 *cs++ = 0; /* upper addr */
1718 *cs++ = 0; /* value */
1719 intel_ring_advance(request, cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001720
1721 return 0;
1722}
1723
John Harrison7deb4d32015-05-29 17:43:59 +01001724static int gen8_emit_flush_render(struct drm_i915_gem_request *request,
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001725 u32 mode)
Oscar Mateo47122742014-07-24 17:04:28 +01001726{
Chris Wilsonb5321f32016-08-02 22:50:18 +01001727 struct intel_engine_cs *engine = request->engine;
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001728 u32 scratch_addr =
1729 i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001730 bool vf_flush_wa = false, dc_flush_wa = false;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001731 u32 *cs, flags = 0;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001732 int len;
Oscar Mateo47122742014-07-24 17:04:28 +01001733
1734 flags |= PIPE_CONTROL_CS_STALL;
1735
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001736 if (mode & EMIT_FLUSH) {
Oscar Mateo47122742014-07-24 17:04:28 +01001737 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
1738 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -08001739 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +01001740 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Oscar Mateo47122742014-07-24 17:04:28 +01001741 }
1742
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001743 if (mode & EMIT_INVALIDATE) {
Oscar Mateo47122742014-07-24 17:04:28 +01001744 flags |= PIPE_CONTROL_TLB_INVALIDATE;
1745 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
1746 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
1747 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
1748 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
1749 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
1750 flags |= PIPE_CONTROL_QW_WRITE;
1751 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Oscar Mateo47122742014-07-24 17:04:28 +01001752
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001753 /*
1754 * On GEN9: before VF_CACHE_INVALIDATE we need to emit a NULL
1755 * pipe control.
1756 */
Chris Wilsonc0336662016-05-06 15:40:21 +01001757 if (IS_GEN9(request->i915))
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001758 vf_flush_wa = true;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001759
1760 /* WaForGAMHang:kbl */
1761 if (IS_KBL_REVID(request->i915, 0, KBL_REVID_B0))
1762 dc_flush_wa = true;
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001763 }
Imre Deak9647ff32015-01-25 13:27:11 -08001764
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001765 len = 6;
1766
1767 if (vf_flush_wa)
1768 len += 6;
1769
1770 if (dc_flush_wa)
1771 len += 12;
1772
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001773 cs = intel_ring_begin(request, len);
1774 if (IS_ERR(cs))
1775 return PTR_ERR(cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001776
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001777 if (vf_flush_wa)
1778 cs = gen8_emit_pipe_control(cs, 0, 0);
Imre Deak9647ff32015-01-25 13:27:11 -08001779
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001780 if (dc_flush_wa)
1781 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_DC_FLUSH_ENABLE,
1782 0);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001783
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001784 cs = gen8_emit_pipe_control(cs, flags, scratch_addr);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001785
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001786 if (dc_flush_wa)
1787 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_CS_STALL, 0);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001788
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001789 intel_ring_advance(request, cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001790
1791 return 0;
1792}
1793
Chris Wilson7c17d372016-01-20 15:43:35 +02001794/*
1795 * Reserve space for 2 NOOPs at the end of each request to be
1796 * used as a workaround for not being allowed to do lite
1797 * restore with HEAD==TAIL (WaIdleLiteRestore).
1798 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001799static void gen8_emit_wa_tail(struct drm_i915_gem_request *request, u32 *cs)
Oscar Mateo4da46e12014-07-24 17:04:27 +01001800{
Chris Wilsonbeecec92017-10-03 21:34:52 +01001801 /* Ensure there's always at least one preemption point per-request. */
1802 *cs++ = MI_ARB_CHECK;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001803 *cs++ = MI_NOOP;
1804 request->wa_tail = intel_ring_offset(request, cs);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001805}
Oscar Mateo4da46e12014-07-24 17:04:27 +01001806
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001807static void gen8_emit_breadcrumb(struct drm_i915_gem_request *request, u32 *cs)
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001808{
Chris Wilson7c17d372016-01-20 15:43:35 +02001809 /* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */
1810 BUILD_BUG_ON(I915_GEM_HWS_INDEX_ADDR & (1 << 5));
Oscar Mateo4da46e12014-07-24 17:04:27 +01001811
Michał Winiarskidf77cd82017-10-25 22:00:15 +02001812 cs = gen8_emit_ggtt_write(cs, request->global_seqno,
1813 intel_hws_seqno_address(request->engine));
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001814 *cs++ = MI_USER_INTERRUPT;
1815 *cs++ = MI_NOOP;
1816 request->tail = intel_ring_offset(request, cs);
Chris Wilsoned1501d2017-03-27 14:14:12 +01001817 assert_ring_tail_valid(request->ring, request->tail);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001818
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001819 gen8_emit_wa_tail(request, cs);
Chris Wilson7c17d372016-01-20 15:43:35 +02001820}
Chris Wilson98f29e82016-10-28 13:58:51 +01001821static const int gen8_emit_breadcrumb_sz = 6 + WA_TAIL_DWORDS;
1822
Michał Winiarskidf77cd82017-10-25 22:00:15 +02001823static void gen8_emit_breadcrumb_rcs(struct drm_i915_gem_request *request,
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001824 u32 *cs)
Chris Wilson7c17d372016-01-20 15:43:35 +02001825{
Michał Winiarskice81a652016-04-12 15:51:55 +02001826 /* We're using qword write, seqno should be aligned to 8 bytes. */
1827 BUILD_BUG_ON(I915_GEM_HWS_INDEX & 1);
1828
Michał Winiarskidf77cd82017-10-25 22:00:15 +02001829 cs = gen8_emit_ggtt_write_rcs(cs, request->global_seqno,
1830 intel_hws_seqno_address(request->engine));
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001831 *cs++ = MI_USER_INTERRUPT;
1832 *cs++ = MI_NOOP;
1833 request->tail = intel_ring_offset(request, cs);
Chris Wilsoned1501d2017-03-27 14:14:12 +01001834 assert_ring_tail_valid(request->ring, request->tail);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001835
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001836 gen8_emit_wa_tail(request, cs);
Oscar Mateo4da46e12014-07-24 17:04:27 +01001837}
Michał Winiarskidf77cd82017-10-25 22:00:15 +02001838static const int gen8_emit_breadcrumb_rcs_sz = 8 + WA_TAIL_DWORDS;
Chris Wilson98f29e82016-10-28 13:58:51 +01001839
John Harrison87531812015-05-29 17:43:44 +01001840static int gen8_init_rcs_context(struct drm_i915_gem_request *req)
Thomas Daniele7778be2014-12-02 12:50:48 +00001841{
1842 int ret;
1843
Tvrtko Ursulin4ac96592017-02-14 15:00:17 +00001844 ret = intel_ring_workarounds_emit(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001845 if (ret)
1846 return ret;
1847
Peter Antoine3bbaba02015-07-10 20:13:11 +03001848 ret = intel_rcs_context_init_mocs(req);
1849 /*
1850 * Failing to program the MOCS is non-fatal.The system will not
1851 * run at peak performance. So generate an error and carry on.
1852 */
1853 if (ret)
1854 DRM_ERROR("MOCS failed to program: expect performance issues.\n");
1855
Chris Wilson4e50f082016-10-28 13:58:31 +01001856 return i915_gem_render_state_emit(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001857}
1858
Oscar Mateo73e4d072014-07-24 17:04:48 +01001859/**
1860 * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001861 * @engine: Engine Command Streamer.
Oscar Mateo73e4d072014-07-24 17:04:48 +01001862 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001863void intel_logical_ring_cleanup(struct intel_engine_cs *engine)
Oscar Mateo454afeb2014-07-24 17:04:22 +01001864{
John Harrison6402c332014-10-31 12:00:26 +00001865 struct drm_i915_private *dev_priv;
Oscar Mateo9832b9d2014-07-24 17:04:30 +01001866
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01001867 /*
1868 * Tasklet cannot be active at this point due intel_mark_active/idle
1869 * so this is just for documentation.
1870 */
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05301871 if (WARN_ON(test_bit(TASKLET_STATE_SCHED,
1872 &engine->execlists.tasklet.state)))
1873 tasklet_kill(&engine->execlists.tasklet);
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01001874
Chris Wilsonc0336662016-05-06 15:40:21 +01001875 dev_priv = engine->i915;
John Harrison6402c332014-10-31 12:00:26 +00001876
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001877 if (engine->buffer) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001878 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
Dave Gordonb0366a52015-12-08 15:02:36 +00001879 }
Oscar Mateo48d82382014-07-24 17:04:23 +01001880
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001881 if (engine->cleanup)
1882 engine->cleanup(engine);
Oscar Mateo48d82382014-07-24 17:04:23 +01001883
Chris Wilsone8a9c582016-12-18 15:37:20 +00001884 intel_engine_cleanup_common(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001885
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001886 lrc_destroy_wa_ctx(engine);
Chris Wilsonf3c9d4072018-01-02 15:12:34 +00001887
Chris Wilsonc0336662016-05-06 15:40:21 +01001888 engine->i915 = NULL;
Akash Goel3b3f1652016-10-13 22:44:48 +05301889 dev_priv->engine[engine->id] = NULL;
1890 kfree(engine);
Oscar Mateo454afeb2014-07-24 17:04:22 +01001891}
1892
Chris Wilsonff44ad52017-03-16 17:13:03 +00001893static void execlists_set_default_submission(struct intel_engine_cs *engine)
Chris Wilsonddd66c52016-08-02 22:50:31 +01001894{
Chris Wilsonff44ad52017-03-16 17:13:03 +00001895 engine->submit_request = execlists_submit_request;
Chris Wilson27a5f612017-09-15 18:31:00 +01001896 engine->cancel_requests = execlists_cancel_requests;
Chris Wilsonff44ad52017-03-16 17:13:03 +00001897 engine->schedule = execlists_schedule;
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05301898 engine->execlists.tasklet.func = execlists_submission_tasklet;
Chris Wilsonaba5e272017-10-25 15:39:41 +01001899
1900 engine->park = NULL;
1901 engine->unpark = NULL;
Tvrtko Ursulincf669b42017-11-29 10:28:05 +00001902
1903 engine->flags |= I915_ENGINE_SUPPORTS_STATS;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001904}
1905
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001906static void
Chris Wilsone1382ef2016-05-06 15:40:20 +01001907logical_ring_default_vfuncs(struct intel_engine_cs *engine)
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001908{
1909 /* Default vfuncs which can be overriden by each engine. */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001910 engine->init_hw = gen8_init_common_ring;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001911 engine->reset_hw = reset_common_ring;
Chris Wilsone8a9c582016-12-18 15:37:20 +00001912
1913 engine->context_pin = execlists_context_pin;
1914 engine->context_unpin = execlists_context_unpin;
1915
Chris Wilsonf73e7392016-12-18 15:37:24 +00001916 engine->request_alloc = execlists_request_alloc;
1917
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001918 engine->emit_flush = gen8_emit_flush;
Chris Wilson9b81d552016-10-28 13:58:50 +01001919 engine->emit_breadcrumb = gen8_emit_breadcrumb;
Chris Wilson98f29e82016-10-28 13:58:51 +01001920 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_sz;
Chris Wilsonff44ad52017-03-16 17:13:03 +00001921
1922 engine->set_default_submission = execlists_set_default_submission;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001923
Chris Wilson31bb59c2016-07-01 17:23:27 +01001924 engine->irq_enable = gen8_logical_ring_enable_irq;
1925 engine->irq_disable = gen8_logical_ring_disable_irq;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001926 engine->emit_bb_start = gen8_emit_bb_start;
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001927}
1928
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001929static inline void
Dave Gordonc2c7f242016-07-13 16:03:35 +01001930logical_ring_default_irqs(struct intel_engine_cs *engine)
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001931{
Dave Gordonc2c7f242016-07-13 16:03:35 +01001932 unsigned shift = engine->irq_shift;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001933 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << shift;
1934 engine->irq_keep_mask = GT_CONTEXT_SWITCH_INTERRUPT << shift;
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001935}
1936
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001937static void
1938logical_ring_setup(struct intel_engine_cs *engine)
1939{
1940 struct drm_i915_private *dev_priv = engine->i915;
1941 enum forcewake_domains fw_domains;
1942
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01001943 intel_engine_setup_common(engine);
1944
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001945 /* Intentionally left blank. */
1946 engine->buffer = NULL;
1947
1948 fw_domains = intel_uncore_forcewake_for_reg(dev_priv,
1949 RING_ELSP(engine),
1950 FW_REG_WRITE);
1951
1952 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
1953 RING_CONTEXT_STATUS_PTR(engine),
1954 FW_REG_READ | FW_REG_WRITE);
1955
1956 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
1957 RING_CONTEXT_STATUS_BUF_BASE(engine),
1958 FW_REG_READ);
1959
Mika Kuoppalab620e872017-09-22 15:43:03 +03001960 engine->execlists.fw_domains = fw_domains;
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001961
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05301962 tasklet_init(&engine->execlists.tasklet,
1963 execlists_submission_tasklet, (unsigned long)engine);
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001964
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001965 logical_ring_default_vfuncs(engine);
1966 logical_ring_default_irqs(engine);
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001967}
1968
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +01001969static int logical_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001970{
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001971 int ret;
1972
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01001973 ret = intel_engine_init_common(engine);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001974 if (ret)
1975 goto error;
1976
Chris Wilson693cfbf2018-01-02 15:12:33 +00001977 engine->execlists.elsp =
1978 engine->i915->regs + i915_mmio_reg_offset(RING_ELSP(engine));
1979
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001980 return 0;
1981
1982error:
1983 intel_logical_ring_cleanup(engine);
1984 return ret;
1985}
1986
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +01001987int logical_render_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001988{
1989 struct drm_i915_private *dev_priv = engine->i915;
1990 int ret;
1991
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001992 logical_ring_setup(engine);
1993
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001994 if (HAS_L3_DPF(dev_priv))
1995 engine->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
1996
1997 /* Override some for render ring. */
1998 if (INTEL_GEN(dev_priv) >= 9)
1999 engine->init_hw = gen9_init_render_ring;
2000 else
2001 engine->init_hw = gen8_init_render_ring;
2002 engine->init_context = gen8_init_rcs_context;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002003 engine->emit_flush = gen8_emit_flush_render;
Michał Winiarskidf77cd82017-10-25 22:00:15 +02002004 engine->emit_breadcrumb = gen8_emit_breadcrumb_rcs;
2005 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_rcs_sz;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002006
Chris Wilsonf51455d2017-01-10 14:47:34 +00002007 ret = intel_engine_create_scratch(engine, PAGE_SIZE);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002008 if (ret)
2009 return ret;
2010
2011 ret = intel_init_workaround_bb(engine);
2012 if (ret) {
2013 /*
2014 * We continue even if we fail to initialize WA batch
2015 * because we only expect rare glitches but nothing
2016 * critical to prevent us from using GPU
2017 */
2018 DRM_ERROR("WA batch buffer initialization failed: %d\n",
2019 ret);
2020 }
2021
Tvrtko Ursulind038fc72016-12-16 13:18:42 +00002022 return logical_ring_init(engine);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002023}
2024
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +01002025int logical_xcs_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002026{
2027 logical_ring_setup(engine);
2028
2029 return logical_ring_init(engine);
2030}
2031
Jeff McGee0cea6502015-02-13 10:27:56 -06002032static u32
Chris Wilsonc0336662016-05-06 15:40:21 +01002033make_rpcs(struct drm_i915_private *dev_priv)
Jeff McGee0cea6502015-02-13 10:27:56 -06002034{
2035 u32 rpcs = 0;
2036
2037 /*
2038 * No explicit RPCS request is needed to ensure full
2039 * slice/subslice/EU enablement prior to Gen9.
2040 */
Chris Wilsonc0336662016-05-06 15:40:21 +01002041 if (INTEL_GEN(dev_priv) < 9)
Jeff McGee0cea6502015-02-13 10:27:56 -06002042 return 0;
2043
2044 /*
2045 * Starting in Gen9, render power gating can leave
2046 * slice/subslice/EU in a partially enabled state. We
2047 * must make an explicit request through RPCS for full
2048 * enablement.
2049 */
Imre Deak43b67992016-08-31 19:13:02 +03002050 if (INTEL_INFO(dev_priv)->sseu.has_slice_pg) {
Jeff McGee0cea6502015-02-13 10:27:56 -06002051 rpcs |= GEN8_RPCS_S_CNT_ENABLE;
Imre Deakf08a0c92016-08-31 19:13:04 +03002052 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.slice_mask) <<
Jeff McGee0cea6502015-02-13 10:27:56 -06002053 GEN8_RPCS_S_CNT_SHIFT;
2054 rpcs |= GEN8_RPCS_ENABLE;
2055 }
2056
Imre Deak43b67992016-08-31 19:13:02 +03002057 if (INTEL_INFO(dev_priv)->sseu.has_subslice_pg) {
Jeff McGee0cea6502015-02-13 10:27:56 -06002058 rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
Imre Deak57ec1712016-08-31 19:13:05 +03002059 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.subslice_mask) <<
Jeff McGee0cea6502015-02-13 10:27:56 -06002060 GEN8_RPCS_SS_CNT_SHIFT;
2061 rpcs |= GEN8_RPCS_ENABLE;
2062 }
2063
Imre Deak43b67992016-08-31 19:13:02 +03002064 if (INTEL_INFO(dev_priv)->sseu.has_eu_pg) {
2065 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
Jeff McGee0cea6502015-02-13 10:27:56 -06002066 GEN8_RPCS_EU_MIN_SHIFT;
Imre Deak43b67992016-08-31 19:13:02 +03002067 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
Jeff McGee0cea6502015-02-13 10:27:56 -06002068 GEN8_RPCS_EU_MAX_SHIFT;
2069 rpcs |= GEN8_RPCS_ENABLE;
2070 }
2071
2072 return rpcs;
2073}
2074
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002075static u32 intel_lr_indirect_ctx_offset(struct intel_engine_cs *engine)
Michel Thierry71562912016-02-23 10:31:49 +00002076{
2077 u32 indirect_ctx_offset;
2078
Chris Wilsonc0336662016-05-06 15:40:21 +01002079 switch (INTEL_GEN(engine->i915)) {
Michel Thierry71562912016-02-23 10:31:49 +00002080 default:
Chris Wilsonc0336662016-05-06 15:40:21 +01002081 MISSING_CASE(INTEL_GEN(engine->i915));
Michel Thierry71562912016-02-23 10:31:49 +00002082 /* fall through */
Michel Thierry7bd0a2c2017-06-06 13:30:38 -07002083 case 10:
2084 indirect_ctx_offset =
2085 GEN10_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2086 break;
Michel Thierry71562912016-02-23 10:31:49 +00002087 case 9:
2088 indirect_ctx_offset =
2089 GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2090 break;
2091 case 8:
2092 indirect_ctx_offset =
2093 GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2094 break;
2095 }
2096
2097 return indirect_ctx_offset;
2098}
2099
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002100static void execlists_init_reg_state(u32 *regs,
Chris Wilsona3aabe82016-10-04 21:11:26 +01002101 struct i915_gem_context *ctx,
2102 struct intel_engine_cs *engine,
2103 struct intel_ring *ring)
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002104{
Chris Wilsona3aabe82016-10-04 21:11:26 +01002105 struct drm_i915_private *dev_priv = engine->i915;
2106 struct i915_hw_ppgtt *ppgtt = ctx->ppgtt ?: dev_priv->mm.aliasing_ppgtt;
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002107 u32 base = engine->mmio_base;
2108 bool rcs = engine->id == RCS;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002109
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002110 /* A context is actually a big batch buffer with several
2111 * MI_LOAD_REGISTER_IMM commands followed by (reg, value) pairs. The
2112 * values we are setting here are only for the first context restore:
2113 * on a subsequent save, the GPU will recreate this batchbuffer with new
2114 * values (including all the missing MI_LOAD_REGISTER_IMM commands that
2115 * we are not initializing here).
2116 */
2117 regs[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(rcs ? 14 : 11) |
2118 MI_LRI_FORCE_POSTED;
2119
2120 CTX_REG(regs, CTX_CONTEXT_CONTROL, RING_CONTEXT_CONTROL(engine),
2121 _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002122 (HAS_RESOURCE_STREAMER(dev_priv) ?
2123 CTX_CTRL_RS_CTX_ENABLE : 0)));
2124 CTX_REG(regs, CTX_RING_HEAD, RING_HEAD(base), 0);
2125 CTX_REG(regs, CTX_RING_TAIL, RING_TAIL(base), 0);
2126 CTX_REG(regs, CTX_RING_BUFFER_START, RING_START(base), 0);
2127 CTX_REG(regs, CTX_RING_BUFFER_CONTROL, RING_CTL(base),
2128 RING_CTL_SIZE(ring->size) | RING_VALID);
2129 CTX_REG(regs, CTX_BB_HEAD_U, RING_BBADDR_UDW(base), 0);
2130 CTX_REG(regs, CTX_BB_HEAD_L, RING_BBADDR(base), 0);
2131 CTX_REG(regs, CTX_BB_STATE, RING_BBSTATE(base), RING_BB_PPGTT);
2132 CTX_REG(regs, CTX_SECOND_BB_HEAD_U, RING_SBBADDR_UDW(base), 0);
2133 CTX_REG(regs, CTX_SECOND_BB_HEAD_L, RING_SBBADDR(base), 0);
2134 CTX_REG(regs, CTX_SECOND_BB_STATE, RING_SBBSTATE(base), 0);
2135 if (rcs) {
Chris Wilson604a8f62017-09-21 14:54:43 +01002136 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
2137
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002138 CTX_REG(regs, CTX_RCS_INDIRECT_CTX, RING_INDIRECT_CTX(base), 0);
2139 CTX_REG(regs, CTX_RCS_INDIRECT_CTX_OFFSET,
2140 RING_INDIRECT_CTX_OFFSET(base), 0);
Chris Wilson604a8f62017-09-21 14:54:43 +01002141 if (wa_ctx->indirect_ctx.size) {
Chris Wilsonbde13eb2016-08-15 10:49:07 +01002142 u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01002143
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002144 regs[CTX_RCS_INDIRECT_CTX + 1] =
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00002145 (ggtt_offset + wa_ctx->indirect_ctx.offset) |
2146 (wa_ctx->indirect_ctx.size / CACHELINE_BYTES);
Arun Siluvery17ee9502015-06-19 19:07:01 +01002147
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002148 regs[CTX_RCS_INDIRECT_CTX_OFFSET + 1] =
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002149 intel_lr_indirect_ctx_offset(engine) << 6;
Chris Wilson604a8f62017-09-21 14:54:43 +01002150 }
2151
2152 CTX_REG(regs, CTX_BB_PER_CTX_PTR, RING_BB_PER_CTX_PTR(base), 0);
2153 if (wa_ctx->per_ctx.size) {
2154 u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01002155
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002156 regs[CTX_BB_PER_CTX_PTR + 1] =
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00002157 (ggtt_offset + wa_ctx->per_ctx.offset) | 0x01;
Arun Siluvery17ee9502015-06-19 19:07:01 +01002158 }
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002159 }
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002160
2161 regs[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9) | MI_LRI_FORCE_POSTED;
2162
2163 CTX_REG(regs, CTX_CTX_TIMESTAMP, RING_CTX_TIMESTAMP(base), 0);
Ville Syrjälä0d925ea2015-11-04 23:20:11 +02002164 /* PDP values well be assigned later if needed */
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002165 CTX_REG(regs, CTX_PDP3_UDW, GEN8_RING_PDP_UDW(engine, 3), 0);
2166 CTX_REG(regs, CTX_PDP3_LDW, GEN8_RING_PDP_LDW(engine, 3), 0);
2167 CTX_REG(regs, CTX_PDP2_UDW, GEN8_RING_PDP_UDW(engine, 2), 0);
2168 CTX_REG(regs, CTX_PDP2_LDW, GEN8_RING_PDP_LDW(engine, 2), 0);
2169 CTX_REG(regs, CTX_PDP1_UDW, GEN8_RING_PDP_UDW(engine, 1), 0);
2170 CTX_REG(regs, CTX_PDP1_LDW, GEN8_RING_PDP_LDW(engine, 1), 0);
2171 CTX_REG(regs, CTX_PDP0_UDW, GEN8_RING_PDP_UDW(engine, 0), 0);
2172 CTX_REG(regs, CTX_PDP0_LDW, GEN8_RING_PDP_LDW(engine, 0), 0);
Michel Thierryd7b26332015-04-08 12:13:34 +01002173
Chris Wilson949e8ab2017-02-09 14:40:36 +00002174 if (ppgtt && i915_vm_is_48bit(&ppgtt->base)) {
Michel Thierry2dba3232015-07-30 11:06:23 +01002175 /* 64b PPGTT (48bit canonical)
2176 * PDP0_DESCRIPTOR contains the base address to PML4 and
2177 * other PDP Descriptors are ignored.
2178 */
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002179 ASSIGN_CTX_PML4(ppgtt, regs);
Michel Thierry2dba3232015-07-30 11:06:23 +01002180 }
2181
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002182 if (rcs) {
2183 regs[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
2184 CTX_REG(regs, CTX_R_PWR_CLK_STATE, GEN8_R_PWR_CLK_STATE,
2185 make_rpcs(dev_priv));
Robert Bragg19f81df2017-06-13 12:23:03 +01002186
2187 i915_oa_init_reg_state(engine, ctx, regs);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002188 }
Chris Wilsona3aabe82016-10-04 21:11:26 +01002189}
2190
2191static int
2192populate_lr_context(struct i915_gem_context *ctx,
2193 struct drm_i915_gem_object *ctx_obj,
2194 struct intel_engine_cs *engine,
2195 struct intel_ring *ring)
2196{
2197 void *vaddr;
Chris Wilsond2b4b972017-11-10 14:26:33 +00002198 u32 *regs;
Chris Wilsona3aabe82016-10-04 21:11:26 +01002199 int ret;
2200
2201 ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
2202 if (ret) {
2203 DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
2204 return ret;
2205 }
2206
2207 vaddr = i915_gem_object_pin_map(ctx_obj, I915_MAP_WB);
2208 if (IS_ERR(vaddr)) {
2209 ret = PTR_ERR(vaddr);
2210 DRM_DEBUG_DRIVER("Could not map object pages! (%d)\n", ret);
2211 return ret;
2212 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002213 ctx_obj->mm.dirty = true;
Chris Wilsona3aabe82016-10-04 21:11:26 +01002214
Chris Wilsond2b4b972017-11-10 14:26:33 +00002215 if (engine->default_state) {
2216 /*
2217 * We only want to copy over the template context state;
2218 * skipping over the headers reserved for GuC communication,
2219 * leaving those as zero.
2220 */
2221 const unsigned long start = LRC_HEADER_PAGES * PAGE_SIZE;
2222 void *defaults;
2223
2224 defaults = i915_gem_object_pin_map(engine->default_state,
2225 I915_MAP_WB);
2226 if (IS_ERR(defaults))
2227 return PTR_ERR(defaults);
2228
2229 memcpy(vaddr + start, defaults + start, engine->context_size);
2230 i915_gem_object_unpin_map(engine->default_state);
2231 }
2232
Chris Wilsona3aabe82016-10-04 21:11:26 +01002233 /* The second page of the context object contains some fields which must
2234 * be set up prior to the first execution. */
Chris Wilsond2b4b972017-11-10 14:26:33 +00002235 regs = vaddr + LRC_STATE_PN * PAGE_SIZE;
2236 execlists_init_reg_state(regs, ctx, engine, ring);
2237 if (!engine->default_state)
2238 regs[CTX_CONTEXT_CONTROL + 1] |=
2239 _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002240
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01002241 i915_gem_object_unpin_map(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002242
2243 return 0;
2244}
2245
Chris Wilsone2efd132016-05-24 14:53:34 +01002246static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
Chris Wilson978f1e02016-04-28 09:56:54 +01002247 struct intel_engine_cs *engine)
Oscar Mateoede7d422014-07-24 17:04:12 +01002248{
Oscar Mateo8c8579172014-07-24 17:04:14 +01002249 struct drm_i915_gem_object *ctx_obj;
Chris Wilson9021ad02016-05-24 14:53:37 +01002250 struct intel_context *ce = &ctx->engine[engine->id];
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002251 struct i915_vma *vma;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002252 uint32_t context_size;
Chris Wilson7e37f882016-08-02 22:50:21 +01002253 struct intel_ring *ring;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002254 int ret;
2255
Chris Wilson9021ad02016-05-24 14:53:37 +01002256 WARN_ON(ce->state);
Oscar Mateoede7d422014-07-24 17:04:12 +01002257
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +03002258 context_size = round_up(engine->context_size, I915_GTT_PAGE_SIZE);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002259
Michel Thierry0b29c752017-09-13 09:56:00 +01002260 /*
2261 * Before the actual start of the context image, we insert a few pages
2262 * for our own use and for sharing with the GuC.
2263 */
2264 context_size += LRC_HEADER_PAGES * PAGE_SIZE;
Alex Daid1675192015-08-12 15:43:43 +01002265
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00002266 ctx_obj = i915_gem_object_create(ctx->i915, context_size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01002267 if (IS_ERR(ctx_obj)) {
Dan Carpenter3126a662015-04-30 17:30:50 +03002268 DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
Chris Wilsonfe3db792016-04-25 13:32:13 +01002269 return PTR_ERR(ctx_obj);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002270 }
2271
Chris Wilsona01cb372017-01-16 15:21:30 +00002272 vma = i915_vma_instance(ctx_obj, &ctx->i915->ggtt.base, NULL);
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002273 if (IS_ERR(vma)) {
2274 ret = PTR_ERR(vma);
2275 goto error_deref_obj;
2276 }
2277
Chris Wilson7e37f882016-08-02 22:50:21 +01002278 ring = intel_engine_create_ring(engine, ctx->ring_size);
Chris Wilsondca33ec2016-08-02 22:50:20 +01002279 if (IS_ERR(ring)) {
2280 ret = PTR_ERR(ring);
Nick Hoathe84fe802015-09-11 12:53:46 +01002281 goto error_deref_obj;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002282 }
2283
Chris Wilsondca33ec2016-08-02 22:50:20 +01002284 ret = populate_lr_context(ctx, ctx_obj, engine, ring);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002285 if (ret) {
2286 DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
Chris Wilsondca33ec2016-08-02 22:50:20 +01002287 goto error_ring_free;
Oscar Mateo84c23772014-07-24 17:04:15 +01002288 }
2289
Chris Wilsondca33ec2016-08-02 22:50:20 +01002290 ce->ring = ring;
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002291 ce->state = vma;
Oscar Mateoede7d422014-07-24 17:04:12 +01002292
2293 return 0;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002294
Chris Wilsondca33ec2016-08-02 22:50:20 +01002295error_ring_free:
Chris Wilson7e37f882016-08-02 22:50:21 +01002296 intel_ring_free(ring);
Nick Hoathe84fe802015-09-11 12:53:46 +01002297error_deref_obj:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002298 i915_gem_object_put(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002299 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01002300}
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002301
Chris Wilson821ed7d2016-09-09 14:11:53 +01002302void intel_lr_context_resume(struct drm_i915_private *dev_priv)
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002303{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002304 struct intel_engine_cs *engine;
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002305 struct i915_gem_context *ctx;
Akash Goel3b3f1652016-10-13 22:44:48 +05302306 enum intel_engine_id id;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002307
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002308 /* Because we emit WA_TAIL_DWORDS there may be a disparity
2309 * between our bookkeeping in ce->ring->head and ce->ring->tail and
2310 * that stored in context. As we only write new commands from
2311 * ce->ring->tail onwards, everything before that is junk. If the GPU
2312 * starts reading from its RING_HEAD from the context, it may try to
2313 * execute that junk and die.
2314 *
2315 * So to avoid that we reset the context images upon resume. For
2316 * simplicity, we just zero everything out.
2317 */
Chris Wilson829a0af2017-06-20 12:05:45 +01002318 list_for_each_entry(ctx, &dev_priv->contexts.list, link) {
Akash Goel3b3f1652016-10-13 22:44:48 +05302319 for_each_engine(engine, dev_priv, id) {
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002320 struct intel_context *ce = &ctx->engine[engine->id];
2321 u32 *reg;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002322
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002323 if (!ce->state)
2324 continue;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002325
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002326 reg = i915_gem_object_pin_map(ce->state->obj,
2327 I915_MAP_WB);
2328 if (WARN_ON(IS_ERR(reg)))
2329 continue;
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01002330
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002331 reg += LRC_STATE_PN * PAGE_SIZE / sizeof(*reg);
2332 reg[CTX_RING_HEAD+1] = 0;
2333 reg[CTX_RING_TAIL+1] = 0;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002334
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002335 ce->state->obj->mm.dirty = true;
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002336 i915_gem_object_unpin_map(ce->state->obj);
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002337
Chris Wilsone6ba9992017-04-25 14:00:49 +01002338 intel_ring_reset(ce->ring, 0);
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002339 }
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002340 }
2341}