blob: 1baaab35905c8d818ba0c041a861187ebfa056f2 [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"
Peter Antoine3bbaba02015-07-10 20:13:11 +0300139#include "intel_mocs.h"
Oscar Mateo127f1002014-07-24 17:04:11 +0100140
Thomas Daniele981e7b2014-07-24 17:04:39 +0100141#define RING_EXECLIST_QFULL (1 << 0x2)
142#define RING_EXECLIST1_VALID (1 << 0x3)
143#define RING_EXECLIST0_VALID (1 << 0x4)
144#define RING_EXECLIST_ACTIVE_STATUS (3 << 0xE)
145#define RING_EXECLIST1_ACTIVE (1 << 0x11)
146#define RING_EXECLIST0_ACTIVE (1 << 0x12)
147
148#define GEN8_CTX_STATUS_IDLE_ACTIVE (1 << 0)
149#define GEN8_CTX_STATUS_PREEMPTED (1 << 1)
150#define GEN8_CTX_STATUS_ELEMENT_SWITCH (1 << 2)
151#define GEN8_CTX_STATUS_ACTIVE_IDLE (1 << 3)
152#define GEN8_CTX_STATUS_COMPLETE (1 << 4)
153#define GEN8_CTX_STATUS_LITE_RESTORE (1 << 15)
Oscar Mateo8670d6f2014-07-24 17:04:17 +0100154
Chris Wilson70c2a242016-09-09 14:11:46 +0100155#define GEN8_CTX_STATUS_COMPLETED_MASK \
156 (GEN8_CTX_STATUS_ACTIVE_IDLE | \
157 GEN8_CTX_STATUS_PREEMPTED | \
158 GEN8_CTX_STATUS_ELEMENT_SWITCH)
159
Oscar Mateo8670d6f2014-07-24 17:04:17 +0100160#define CTX_LRI_HEADER_0 0x01
161#define CTX_CONTEXT_CONTROL 0x02
162#define CTX_RING_HEAD 0x04
163#define CTX_RING_TAIL 0x06
164#define CTX_RING_BUFFER_START 0x08
165#define CTX_RING_BUFFER_CONTROL 0x0a
166#define CTX_BB_HEAD_U 0x0c
167#define CTX_BB_HEAD_L 0x0e
168#define CTX_BB_STATE 0x10
169#define CTX_SECOND_BB_HEAD_U 0x12
170#define CTX_SECOND_BB_HEAD_L 0x14
171#define CTX_SECOND_BB_STATE 0x16
172#define CTX_BB_PER_CTX_PTR 0x18
173#define CTX_RCS_INDIRECT_CTX 0x1a
174#define CTX_RCS_INDIRECT_CTX_OFFSET 0x1c
175#define CTX_LRI_HEADER_1 0x21
176#define CTX_CTX_TIMESTAMP 0x22
177#define CTX_PDP3_UDW 0x24
178#define CTX_PDP3_LDW 0x26
179#define CTX_PDP2_UDW 0x28
180#define CTX_PDP2_LDW 0x2a
181#define CTX_PDP1_UDW 0x2c
182#define CTX_PDP1_LDW 0x2e
183#define CTX_PDP0_UDW 0x30
184#define CTX_PDP0_LDW 0x32
185#define CTX_LRI_HEADER_2 0x41
186#define CTX_R_PWR_CLK_STATE 0x42
187#define CTX_GPGPU_CSR_BASE_ADDRESS 0x44
188
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +0000189#define CTX_REG(reg_state, pos, reg, val) do { \
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200190 (reg_state)[(pos)+0] = i915_mmio_reg_offset(reg); \
Ville Syrjälä0d925ea2015-11-04 23:20:11 +0200191 (reg_state)[(pos)+1] = (val); \
192} while (0)
193
194#define ASSIGN_CTX_PDP(ppgtt, reg_state, n) do { \
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300195 const u64 _addr = i915_page_dir_dma_addr((ppgtt), (n)); \
Michel Thierrye5815a22015-04-08 12:13:32 +0100196 reg_state[CTX_PDP ## n ## _UDW+1] = upper_32_bits(_addr); \
197 reg_state[CTX_PDP ## n ## _LDW+1] = lower_32_bits(_addr); \
Ville Syrjälä9244a812015-11-04 23:20:09 +0200198} while (0)
Michel Thierrye5815a22015-04-08 12:13:32 +0100199
Ville Syrjälä9244a812015-11-04 23:20:09 +0200200#define ASSIGN_CTX_PML4(ppgtt, reg_state) do { \
Michel Thierry2dba3232015-07-30 11:06:23 +0100201 reg_state[CTX_PDP0_UDW + 1] = upper_32_bits(px_dma(&ppgtt->pml4)); \
202 reg_state[CTX_PDP0_LDW + 1] = lower_32_bits(px_dma(&ppgtt->pml4)); \
Ville Syrjälä9244a812015-11-04 23:20:09 +0200203} while (0)
Michel Thierry2dba3232015-07-30 11:06:23 +0100204
Michel Thierry71562912016-02-23 10:31:49 +0000205#define GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x17
206#define GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x26
Michel Thierry7bd0a2c2017-06-06 13:30:38 -0700207#define GEN10_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x19
Ben Widawsky84b790f2014-07-24 17:04:36 +0100208
Chris Wilson0e93cdd2016-04-29 09:07:06 +0100209/* Typical size of the average request (2 pipecontrols and a MI_BB) */
210#define EXECLISTS_REQUEST_SIZE 64 /* bytes */
Chris Wilsona3aabe82016-10-04 21:11:26 +0100211#define WA_TAIL_DWORDS 2
Chris Wilson7e4992a2017-09-28 20:38:59 +0100212#define WA_TAIL_BYTES (sizeof(u32) * WA_TAIL_DWORDS)
Chris Wilsonbeecec92017-10-03 21:34:52 +0100213#define PREEMPT_ID 0x1
Chris Wilsona3aabe82016-10-04 21:11:26 +0100214
Chris Wilsone2efd132016-05-24 14:53:34 +0100215static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
Chris Wilson978f1e02016-04-28 09:56:54 +0100216 struct intel_engine_cs *engine);
Chris Wilsona3aabe82016-10-04 21:11:26 +0100217static void execlists_init_reg_state(u32 *reg_state,
218 struct i915_gem_context *ctx,
219 struct intel_engine_cs *engine,
220 struct intel_ring *ring);
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000221
Oscar Mateo73e4d072014-07-24 17:04:48 +0100222/**
223 * intel_sanitize_enable_execlists() - sanitize i915.enable_execlists
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100224 * @dev_priv: i915 device private
Oscar Mateo73e4d072014-07-24 17:04:48 +0100225 * @enable_execlists: value of i915.enable_execlists module parameter.
226 *
227 * Only certain platforms support Execlists (the prerequisites being
Thomas Daniel27401d12014-12-11 12:48:35 +0000228 * support for Logical Ring Contexts and Aliasing PPGTT or better).
Oscar Mateo73e4d072014-07-24 17:04:48 +0100229 *
230 * Return: 1 if Execlists is supported and has to be enabled.
231 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100232int intel_sanitize_enable_execlists(struct drm_i915_private *dev_priv, int enable_execlists)
Oscar Mateo127f1002014-07-24 17:04:11 +0100233{
Zhiyuan Lva0bd6c32015-08-28 15:41:16 +0800234 /* On platforms with execlist available, vGPU will only
235 * support execlist mode, no ring buffer mode.
236 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100237 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv) && intel_vgpu_active(dev_priv))
Zhiyuan Lva0bd6c32015-08-28 15:41:16 +0800238 return 1;
239
Chris Wilsonc0336662016-05-06 15:40:21 +0100240 if (INTEL_GEN(dev_priv) >= 9)
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000241 return 1;
242
Oscar Mateo127f1002014-07-24 17:04:11 +0100243 if (enable_execlists == 0)
244 return 0;
245
Daniel Vetter5a21b662016-05-24 17:13:53 +0200246 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv) &&
Maarten Lankhorst8279aaf2017-10-04 11:44:16 +0200247 USES_PPGTT(dev_priv))
Oscar Mateo127f1002014-07-24 17:04:11 +0100248 return 1;
249
250 return 0;
251}
Oscar Mateoede7d422014-07-24 17:04:12 +0100252
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000253/**
254 * intel_lr_context_descriptor_update() - calculate & cache the descriptor
255 * descriptor for a pinned context
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000256 * @ctx: Context to work on
Chris Wilson9021ad02016-05-24 14:53:37 +0100257 * @engine: Engine the descriptor will be used with
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000258 *
259 * The context descriptor encodes various attributes of a context,
260 * including its GTT address and some flags. Because it's fairly
261 * expensive to calculate, we'll just do it once and cache the result,
262 * which remains valid until the context is unpinned.
263 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200264 * This is what a descriptor looks like, from LSB to MSB::
265 *
Mika Kuoppala2355cf02017-01-27 15:03:09 +0200266 * bits 0-11: flags, GEN8_CTX_* (cached in ctx->desc_template)
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200267 * bits 12-31: LRCA, GTT address of (the HWSP of) this context
268 * bits 32-52: ctx ID, a globally unique tag
269 * bits 53-54: mbz, reserved for use by hardware
270 * bits 55-63: group ID, currently unused and set to 0
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000271 */
272static void
Chris Wilsone2efd132016-05-24 14:53:34 +0100273intel_lr_context_descriptor_update(struct i915_gem_context *ctx,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000274 struct intel_engine_cs *engine)
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000275{
Chris Wilson9021ad02016-05-24 14:53:37 +0100276 struct intel_context *ce = &ctx->engine[engine->id];
Chris Wilson7069b142016-04-28 09:56:52 +0100277 u64 desc;
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000278
Chris Wilson7069b142016-04-28 09:56:52 +0100279 BUILD_BUG_ON(MAX_CONTEXT_HW_ID > (1<<GEN8_CTX_ID_WIDTH));
280
Mika Kuoppala2355cf02017-01-27 15:03:09 +0200281 desc = ctx->desc_template; /* bits 0-11 */
Michel Thierry0b29c752017-09-13 09:56:00 +0100282 desc |= i915_ggtt_offset(ce->state) + LRC_HEADER_PAGES * PAGE_SIZE;
Chris Wilson9021ad02016-05-24 14:53:37 +0100283 /* bits 12-31 */
Chris Wilson7069b142016-04-28 09:56:52 +0100284 desc |= (u64)ctx->hw_id << GEN8_CTX_ID_SHIFT; /* bits 32-52 */
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000285
Chris Wilson9021ad02016-05-24 14:53:37 +0100286 ce->lrc_desc = desc;
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000287}
288
Chris Wilson27606fd2017-09-16 21:44:13 +0100289static struct i915_priolist *
290lookup_priolist(struct intel_engine_cs *engine,
291 struct i915_priotree *pt,
292 int prio)
Chris Wilson08dd3e12017-09-16 21:44:12 +0100293{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300294 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100295 struct i915_priolist *p;
296 struct rb_node **parent, *rb;
297 bool first = true;
298
Mika Kuoppalab620e872017-09-22 15:43:03 +0300299 if (unlikely(execlists->no_priolist))
Chris Wilson08dd3e12017-09-16 21:44:12 +0100300 prio = I915_PRIORITY_NORMAL;
301
302find_priolist:
303 /* most positive priority is scheduled first, equal priorities fifo */
304 rb = NULL;
Mika Kuoppalab620e872017-09-22 15:43:03 +0300305 parent = &execlists->queue.rb_node;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100306 while (*parent) {
307 rb = *parent;
308 p = rb_entry(rb, typeof(*p), node);
309 if (prio > p->priority) {
310 parent = &rb->rb_left;
311 } else if (prio < p->priority) {
312 parent = &rb->rb_right;
313 first = false;
314 } else {
Chris Wilson27606fd2017-09-16 21:44:13 +0100315 return p;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100316 }
317 }
318
319 if (prio == I915_PRIORITY_NORMAL) {
Mika Kuoppalab620e872017-09-22 15:43:03 +0300320 p = &execlists->default_priolist;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100321 } else {
322 p = kmem_cache_alloc(engine->i915->priorities, GFP_ATOMIC);
323 /* Convert an allocation failure to a priority bump */
324 if (unlikely(!p)) {
325 prio = I915_PRIORITY_NORMAL; /* recurses just once */
326
327 /* To maintain ordering with all rendering, after an
328 * allocation failure we have to disable all scheduling.
329 * Requests will then be executed in fifo, and schedule
330 * will ensure that dependencies are emitted in fifo.
331 * There will be still some reordering with existing
332 * requests, so if userspace lied about their
333 * dependencies that reordering may be visible.
334 */
Mika Kuoppalab620e872017-09-22 15:43:03 +0300335 execlists->no_priolist = true;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100336 goto find_priolist;
337 }
338 }
339
340 p->priority = prio;
Chris Wilson27606fd2017-09-16 21:44:13 +0100341 INIT_LIST_HEAD(&p->requests);
Chris Wilson08dd3e12017-09-16 21:44:12 +0100342 rb_link_node(&p->node, rb, parent);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300343 rb_insert_color(&p->node, &execlists->queue);
Chris Wilson08dd3e12017-09-16 21:44:12 +0100344
Chris Wilson08dd3e12017-09-16 21:44:12 +0100345 if (first)
Mika Kuoppalab620e872017-09-22 15:43:03 +0300346 execlists->first = &p->node;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100347
Chris Wilson27606fd2017-09-16 21:44:13 +0100348 return ptr_pack_bits(p, first, 1);
Chris Wilson08dd3e12017-09-16 21:44:12 +0100349}
350
Chris Wilson7e4992a2017-09-28 20:38:59 +0100351static void unwind_wa_tail(struct drm_i915_gem_request *rq)
352{
353 rq->tail = intel_ring_wrap(rq->ring, rq->wa_tail - WA_TAIL_BYTES);
354 assert_ring_tail_valid(rq->ring, rq->tail);
355}
356
Michał Winiarskia4598d12017-10-25 22:00:18 +0200357static void __unwind_incomplete_requests(struct intel_engine_cs *engine)
Chris Wilson7e4992a2017-09-28 20:38:59 +0100358{
359 struct drm_i915_gem_request *rq, *rn;
Michał Winiarski097a9482017-09-28 20:39:01 +0100360 struct i915_priolist *uninitialized_var(p);
361 int last_prio = I915_PRIORITY_INVALID;
Chris Wilson7e4992a2017-09-28 20:38:59 +0100362
363 lockdep_assert_held(&engine->timeline->lock);
364
365 list_for_each_entry_safe_reverse(rq, rn,
366 &engine->timeline->requests,
367 link) {
Chris Wilson7e4992a2017-09-28 20:38:59 +0100368 if (i915_gem_request_completed(rq))
369 return;
370
371 __i915_gem_request_unsubmit(rq);
372 unwind_wa_tail(rq);
373
Michał Winiarski097a9482017-09-28 20:39:01 +0100374 GEM_BUG_ON(rq->priotree.priority == I915_PRIORITY_INVALID);
375 if (rq->priotree.priority != last_prio) {
376 p = lookup_priolist(engine,
377 &rq->priotree,
378 rq->priotree.priority);
379 p = ptr_mask_bits(p, 1);
380
381 last_prio = rq->priotree.priority;
382 }
383
384 list_add(&rq->priotree.link, &p->requests);
Chris Wilson7e4992a2017-09-28 20:38:59 +0100385 }
386}
387
Michał Winiarskic41937f2017-10-26 15:35:58 +0200388void
Michał Winiarskia4598d12017-10-25 22:00:18 +0200389execlists_unwind_incomplete_requests(struct intel_engine_execlists *execlists)
390{
391 struct intel_engine_cs *engine =
392 container_of(execlists, typeof(*engine), execlists);
393
394 spin_lock_irq(&engine->timeline->lock);
395 __unwind_incomplete_requests(engine);
396 spin_unlock_irq(&engine->timeline->lock);
397}
398
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100399static inline void
400execlists_context_status_change(struct drm_i915_gem_request *rq,
401 unsigned long status)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100402{
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100403 /*
404 * Only used when GVT-g is enabled now. When GVT-g is disabled,
405 * The compiler should eliminate this function as dead-code.
406 */
407 if (!IS_ENABLED(CONFIG_DRM_I915_GVT))
408 return;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100409
Changbin Du3fc03062017-03-13 10:47:11 +0800410 atomic_notifier_call_chain(&rq->engine->context_status_notifier,
411 status, rq);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100412}
413
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000414static void
415execlists_update_context_pdps(struct i915_hw_ppgtt *ppgtt, u32 *reg_state)
416{
417 ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
418 ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
419 ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
420 ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
421}
422
Chris Wilson70c2a242016-09-09 14:11:46 +0100423static u64 execlists_update_context(struct drm_i915_gem_request *rq)
Oscar Mateoae1250b2014-07-24 17:04:37 +0100424{
Chris Wilson70c2a242016-09-09 14:11:46 +0100425 struct intel_context *ce = &rq->ctx->engine[rq->engine->id];
Zhi Wang04da8112017-02-06 18:37:16 +0800426 struct i915_hw_ppgtt *ppgtt =
427 rq->ctx->ppgtt ?: rq->i915->mm.aliasing_ppgtt;
Chris Wilson70c2a242016-09-09 14:11:46 +0100428 u32 *reg_state = ce->lrc_reg_state;
Oscar Mateoae1250b2014-07-24 17:04:37 +0100429
Chris Wilsone6ba9992017-04-25 14:00:49 +0100430 reg_state[CTX_RING_TAIL+1] = intel_ring_set_tail(rq->ring, rq->tail);
Oscar Mateoae1250b2014-07-24 17:04:37 +0100431
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000432 /* True 32b PPGTT with dynamic page allocation: update PDP
433 * registers and point the unallocated PDPs to scratch page.
434 * PML4 is allocated during ppgtt init, so this is not needed
435 * in 48-bit mode.
436 */
Chris Wilson949e8ab2017-02-09 14:40:36 +0000437 if (ppgtt && !i915_vm_is_48bit(&ppgtt->base))
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000438 execlists_update_context_pdps(ppgtt, reg_state);
Chris Wilson70c2a242016-09-09 14:11:46 +0100439
440 return ce->lrc_desc;
Oscar Mateoae1250b2014-07-24 17:04:37 +0100441}
442
Chris Wilsonbeecec92017-10-03 21:34:52 +0100443static inline void elsp_write(u64 desc, u32 __iomem *elsp)
444{
445 writel(upper_32_bits(desc), elsp);
446 writel(lower_32_bits(desc), elsp);
447}
448
Chris Wilson70c2a242016-09-09 14:11:46 +0100449static void execlists_submit_ports(struct intel_engine_cs *engine)
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100450{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300451 struct execlist_port *port = engine->execlists.port;
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100452 u32 __iomem *elsp =
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100453 engine->i915->regs + i915_mmio_reg_offset(RING_ELSP(engine));
454 unsigned int n;
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100455
Mika Kuoppala76e70082017-09-22 15:43:07 +0300456 for (n = execlists_num_ports(&engine->execlists); n--; ) {
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100457 struct drm_i915_gem_request *rq;
458 unsigned int count;
459 u64 desc;
Chris Wilson70c2a242016-09-09 14:11:46 +0100460
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100461 rq = port_unpack(&port[n], &count);
462 if (rq) {
463 GEM_BUG_ON(count > !n);
464 if (!count++)
465 execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_IN);
466 port_set(&port[n], port_pack(rq, count));
467 desc = execlists_update_context(rq);
468 GEM_DEBUG_EXEC(port[n].context_id = upper_32_bits(desc));
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000469
470 GEM_TRACE("%s in[%d]: ctx=%d.%d, seqno=%x\n",
471 engine->name, n,
472 rq->ctx->hw_id, count,
473 rq->global_seqno);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100474 } else {
475 GEM_BUG_ON(!n);
476 desc = 0;
477 }
478
Chris Wilsonbeecec92017-10-03 21:34:52 +0100479 elsp_write(desc, elsp);
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100480 }
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100481}
482
Chris Wilson70c2a242016-09-09 14:11:46 +0100483static bool ctx_single_port_submission(const struct i915_gem_context *ctx)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100484{
Chris Wilson70c2a242016-09-09 14:11:46 +0100485 return (IS_ENABLED(CONFIG_DRM_I915_GVT) &&
Chris Wilson60958682016-12-31 11:20:11 +0000486 i915_gem_context_force_single_submission(ctx));
Ben Widawsky84b790f2014-07-24 17:04:36 +0100487}
488
Chris Wilson70c2a242016-09-09 14:11:46 +0100489static bool can_merge_ctx(const struct i915_gem_context *prev,
490 const struct i915_gem_context *next)
Michel Thierryacdd8842014-07-24 17:04:38 +0100491{
Chris Wilson70c2a242016-09-09 14:11:46 +0100492 if (prev != next)
493 return false;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100494
Chris Wilson70c2a242016-09-09 14:11:46 +0100495 if (ctx_single_port_submission(prev))
496 return false;
Michel Thierryacdd8842014-07-24 17:04:38 +0100497
Chris Wilson70c2a242016-09-09 14:11:46 +0100498 return true;
499}
Peter Antoine779949f2015-05-11 16:03:27 +0100500
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100501static void port_assign(struct execlist_port *port,
502 struct drm_i915_gem_request *rq)
503{
504 GEM_BUG_ON(rq == port_request(port));
505
506 if (port_isset(port))
507 i915_gem_request_put(port_request(port));
508
509 port_set(port, port_pack(i915_gem_request_get(rq), port_count(port)));
510}
511
Chris Wilsonbeecec92017-10-03 21:34:52 +0100512static void inject_preempt_context(struct intel_engine_cs *engine)
513{
514 struct intel_context *ce =
515 &engine->i915->preempt_context->engine[engine->id];
516 u32 __iomem *elsp =
517 engine->i915->regs + i915_mmio_reg_offset(RING_ELSP(engine));
518 unsigned int n;
519
520 GEM_BUG_ON(engine->i915->preempt_context->hw_id != PREEMPT_ID);
521 GEM_BUG_ON(!IS_ALIGNED(ce->ring->size, WA_TAIL_BYTES));
522
523 memset(ce->ring->vaddr + ce->ring->tail, 0, WA_TAIL_BYTES);
524 ce->ring->tail += WA_TAIL_BYTES;
525 ce->ring->tail &= (ce->ring->size - 1);
526 ce->lrc_reg_state[CTX_RING_TAIL+1] = ce->ring->tail;
527
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000528 GEM_TRACE("\n");
Chris Wilsonbeecec92017-10-03 21:34:52 +0100529 for (n = execlists_num_ports(&engine->execlists); --n; )
530 elsp_write(0, elsp);
531
532 elsp_write(ce->lrc_desc, elsp);
533}
534
Chris Wilson70c2a242016-09-09 14:11:46 +0100535static void execlists_dequeue(struct intel_engine_cs *engine)
536{
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300537 struct intel_engine_execlists * const execlists = &engine->execlists;
538 struct execlist_port *port = execlists->port;
Mika Kuoppala76e70082017-09-22 15:43:07 +0300539 const struct execlist_port * const last_port =
540 &execlists->port[execlists->port_mask];
Chris Wilsonbeecec92017-10-03 21:34:52 +0100541 struct drm_i915_gem_request *last = port_request(port);
Chris Wilson20311bd2016-11-14 20:41:03 +0000542 struct rb_node *rb;
Chris Wilson70c2a242016-09-09 14:11:46 +0100543 bool submit = false;
Michel Thierryacdd8842014-07-24 17:04:38 +0100544
Chris Wilson70c2a242016-09-09 14:11:46 +0100545 /* Hardware submission is through 2 ports. Conceptually each port
546 * has a (RING_START, RING_HEAD, RING_TAIL) tuple. RING_START is
547 * static for a context, and unique to each, so we only execute
548 * requests belonging to a single context from each ring. RING_HEAD
549 * is maintained by the CS in the context image, it marks the place
550 * where it got up to last time, and through RING_TAIL we tell the CS
551 * where we want to execute up to this time.
552 *
553 * In this list the requests are in order of execution. Consecutive
554 * requests from the same context are adjacent in the ringbuffer. We
555 * can combine these requests into a single RING_TAIL update:
556 *
557 * RING_HEAD...req1...req2
558 * ^- RING_TAIL
559 * since to execute req2 the CS must first execute req1.
560 *
561 * Our goal then is to point each port to the end of a consecutive
562 * sequence of requests as being the most optimal (fewest wake ups
563 * and context switches) submission.
564 */
565
Tvrtko Ursulin9f7886d2017-03-21 10:55:11 +0000566 spin_lock_irq(&engine->timeline->lock);
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300567 rb = execlists->first;
568 GEM_BUG_ON(rb_first(&execlists->queue) != rb);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100569 if (!rb)
570 goto unlock;
571
572 if (last) {
573 /*
574 * Don't resubmit or switch until all outstanding
575 * preemptions (lite-restore) are seen. Then we
576 * know the next preemption status we see corresponds
577 * to this ELSP update.
578 */
579 if (port_count(&port[0]) > 1)
580 goto unlock;
581
Michał Winiarskia4598d12017-10-25 22:00:18 +0200582 if (HAS_LOGICAL_RING_PREEMPTION(engine->i915) &&
Chris Wilsonbeecec92017-10-03 21:34:52 +0100583 rb_entry(rb, struct i915_priolist, node)->priority >
584 max(last->priotree.priority, 0)) {
585 /*
586 * Switch to our empty preempt context so
587 * the state of the GPU is known (idle).
588 */
589 inject_preempt_context(engine);
Chris Wilson4a118ec2017-10-23 22:32:36 +0100590 execlists_set_active(execlists,
591 EXECLISTS_ACTIVE_PREEMPT);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100592 goto unlock;
593 } else {
594 /*
595 * In theory, we could coalesce more requests onto
596 * the second port (the first port is active, with
597 * no preemptions pending). However, that means we
598 * then have to deal with the possible lite-restore
599 * of the second port (as we submit the ELSP, there
600 * may be a context-switch) but also we may complete
601 * the resubmission before the context-switch. Ergo,
602 * coalescing onto the second port will cause a
603 * preemption event, but we cannot predict whether
604 * that will affect port[0] or port[1].
605 *
606 * If the second port is already active, we can wait
607 * until the next context-switch before contemplating
608 * new requests. The GPU will be busy and we should be
609 * able to resubmit the new ELSP before it idles,
610 * avoiding pipeline bubbles (momentary pauses where
611 * the driver is unable to keep up the supply of new
612 * work).
613 */
614 if (port_count(&port[1]))
615 goto unlock;
616
617 /* WaIdleLiteRestore:bdw,skl
618 * Apply the wa NOOPs to prevent
619 * ring:HEAD == req:TAIL as we resubmit the
620 * request. See gen8_emit_breadcrumb() for
621 * where we prepare the padding after the
622 * end of the request.
623 */
624 last->tail = last->wa_tail;
625 }
626 }
627
628 do {
Chris Wilson6c067572017-05-17 13:10:03 +0100629 struct i915_priolist *p = rb_entry(rb, typeof(*p), node);
630 struct drm_i915_gem_request *rq, *rn;
Chris Wilson20311bd2016-11-14 20:41:03 +0000631
Chris Wilson6c067572017-05-17 13:10:03 +0100632 list_for_each_entry_safe(rq, rn, &p->requests, priotree.link) {
633 /*
634 * Can we combine this request with the current port?
635 * It has to be the same context/ringbuffer and not
636 * have any exceptions (e.g. GVT saying never to
637 * combine contexts).
638 *
639 * If we can combine the requests, we can execute both
640 * by updating the RING_TAIL to point to the end of the
641 * second request, and so we never need to tell the
642 * hardware about the first.
Chris Wilson70c2a242016-09-09 14:11:46 +0100643 */
Chris Wilson6c067572017-05-17 13:10:03 +0100644 if (last && !can_merge_ctx(rq->ctx, last->ctx)) {
645 /*
646 * If we are on the second port and cannot
647 * combine this request with the last, then we
648 * are done.
649 */
Mika Kuoppala76e70082017-09-22 15:43:07 +0300650 if (port == last_port) {
Chris Wilson6c067572017-05-17 13:10:03 +0100651 __list_del_many(&p->requests,
652 &rq->priotree.link);
653 goto done;
654 }
Chris Wilson70c2a242016-09-09 14:11:46 +0100655
Chris Wilson6c067572017-05-17 13:10:03 +0100656 /*
657 * If GVT overrides us we only ever submit
658 * port[0], leaving port[1] empty. Note that we
659 * also have to be careful that we don't queue
660 * the same context (even though a different
661 * request) to the second port.
662 */
663 if (ctx_single_port_submission(last->ctx) ||
664 ctx_single_port_submission(rq->ctx)) {
665 __list_del_many(&p->requests,
666 &rq->priotree.link);
667 goto done;
668 }
Chris Wilson70c2a242016-09-09 14:11:46 +0100669
Chris Wilson6c067572017-05-17 13:10:03 +0100670 GEM_BUG_ON(last->ctx == rq->ctx);
Chris Wilson70c2a242016-09-09 14:11:46 +0100671
Chris Wilson6c067572017-05-17 13:10:03 +0100672 if (submit)
673 port_assign(port, last);
674 port++;
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300675
676 GEM_BUG_ON(port_isset(port));
Chris Wilson6c067572017-05-17 13:10:03 +0100677 }
678
679 INIT_LIST_HEAD(&rq->priotree.link);
Chris Wilson6c067572017-05-17 13:10:03 +0100680 __i915_gem_request_submit(rq);
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300681 trace_i915_gem_request_in(rq, port_index(port, execlists));
Chris Wilson6c067572017-05-17 13:10:03 +0100682 last = rq;
683 submit = true;
Chris Wilson70c2a242016-09-09 14:11:46 +0100684 }
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000685
Chris Wilson20311bd2016-11-14 20:41:03 +0000686 rb = rb_next(rb);
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300687 rb_erase(&p->node, &execlists->queue);
Chris Wilson6c067572017-05-17 13:10:03 +0100688 INIT_LIST_HEAD(&p->requests);
689 if (p->priority != I915_PRIORITY_NORMAL)
Chris Wilsonc5cf9a92017-05-17 13:10:04 +0100690 kmem_cache_free(engine->i915->priorities, p);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100691 } while (rb);
Chris Wilson6c067572017-05-17 13:10:03 +0100692done:
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300693 execlists->first = rb;
Chris Wilson6c067572017-05-17 13:10:03 +0100694 if (submit)
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100695 port_assign(port, last);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100696unlock:
Tvrtko Ursulin9f7886d2017-03-21 10:55:11 +0000697 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson70c2a242016-09-09 14:11:46 +0100698
Chris Wilson4a118ec2017-10-23 22:32:36 +0100699 if (submit) {
700 execlists_set_active(execlists, EXECLISTS_ACTIVE_USER);
Chris Wilson70c2a242016-09-09 14:11:46 +0100701 execlists_submit_ports(engine);
Chris Wilson4a118ec2017-10-23 22:32:36 +0100702 }
Michel Thierryacdd8842014-07-24 17:04:38 +0100703}
704
Michał Winiarskic41937f2017-10-26 15:35:58 +0200705void
Michał Winiarskia4598d12017-10-25 22:00:18 +0200706execlists_cancel_port_requests(struct intel_engine_execlists * const execlists)
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300707{
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100708 struct execlist_port *port = execlists->port;
Mika Kuoppaladc2279e2017-10-10 14:48:57 +0300709 unsigned int num_ports = execlists_num_ports(execlists);
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300710
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100711 while (num_ports-- && port_isset(port)) {
Chris Wilson7e44fc22017-09-26 11:17:19 +0100712 struct drm_i915_gem_request *rq = port_request(port);
713
Chris Wilson4a118ec2017-10-23 22:32:36 +0100714 GEM_BUG_ON(!execlists->active);
Chris Wilsond6c05112017-10-03 21:34:47 +0100715 execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_PREEMPTED);
Chris Wilson7e44fc22017-09-26 11:17:19 +0100716 i915_gem_request_put(rq);
717
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100718 memset(port, 0, sizeof(*port));
719 port++;
720 }
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300721}
722
Chris Wilson27a5f612017-09-15 18:31:00 +0100723static void execlists_cancel_requests(struct intel_engine_cs *engine)
724{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300725 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilson27a5f612017-09-15 18:31:00 +0100726 struct drm_i915_gem_request *rq, *rn;
727 struct rb_node *rb;
728 unsigned long flags;
Chris Wilson27a5f612017-09-15 18:31:00 +0100729
730 spin_lock_irqsave(&engine->timeline->lock, flags);
731
732 /* Cancel the requests on the HW and clear the ELSP tracker. */
Michał Winiarskia4598d12017-10-25 22:00:18 +0200733 execlists_cancel_port_requests(execlists);
Chris Wilson27a5f612017-09-15 18:31:00 +0100734
735 /* Mark all executing requests as skipped. */
736 list_for_each_entry(rq, &engine->timeline->requests, link) {
737 GEM_BUG_ON(!rq->global_seqno);
738 if (!i915_gem_request_completed(rq))
739 dma_fence_set_error(&rq->fence, -EIO);
740 }
741
742 /* Flush the queued requests to the timeline list (for retiring). */
Mika Kuoppalab620e872017-09-22 15:43:03 +0300743 rb = execlists->first;
Chris Wilson27a5f612017-09-15 18:31:00 +0100744 while (rb) {
745 struct i915_priolist *p = rb_entry(rb, typeof(*p), node);
746
747 list_for_each_entry_safe(rq, rn, &p->requests, priotree.link) {
748 INIT_LIST_HEAD(&rq->priotree.link);
Chris Wilson27a5f612017-09-15 18:31:00 +0100749
750 dma_fence_set_error(&rq->fence, -EIO);
751 __i915_gem_request_submit(rq);
752 }
753
754 rb = rb_next(rb);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300755 rb_erase(&p->node, &execlists->queue);
Chris Wilson27a5f612017-09-15 18:31:00 +0100756 INIT_LIST_HEAD(&p->requests);
757 if (p->priority != I915_PRIORITY_NORMAL)
758 kmem_cache_free(engine->i915->priorities, p);
759 }
760
761 /* Remaining _unready_ requests will be nop'ed when submitted */
762
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300763
Mika Kuoppalab620e872017-09-22 15:43:03 +0300764 execlists->queue = RB_ROOT;
765 execlists->first = NULL;
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100766 GEM_BUG_ON(port_isset(execlists->port));
Chris Wilson27a5f612017-09-15 18:31:00 +0100767
768 /*
769 * The port is checked prior to scheduling a tasklet, but
770 * just in case we have suspended the tasklet to do the
771 * wedging make sure that when it wakes, it decides there
772 * is no work to do by clearing the irq_posted bit.
773 */
774 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
775
776 spin_unlock_irqrestore(&engine->timeline->lock, flags);
777}
778
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200779/*
Oscar Mateo73e4d072014-07-24 17:04:48 +0100780 * Check the unread Context Status Buffers and manage the submission of new
781 * contexts to the ELSP accordingly.
782 */
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +0100783static void intel_lrc_irq_handler(unsigned long data)
Thomas Daniele981e7b2014-07-24 17:04:39 +0100784{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300785 struct intel_engine_cs * const engine = (struct intel_engine_cs *)data;
786 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilsonbeecec92017-10-03 21:34:52 +0100787 struct execlist_port * const port = execlists->port;
Chris Wilsonc0336662016-05-06 15:40:21 +0100788 struct drm_i915_private *dev_priv = engine->i915;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100789
Chris Wilson48921262017-04-11 18:58:50 +0100790 /* We can skip acquiring intel_runtime_pm_get() here as it was taken
791 * on our behalf by the request (see i915_gem_mark_busy()) and it will
792 * not be relinquished until the device is idle (see
793 * i915_gem_idle_work_handler()). As a precaution, we make sure
794 * that all ELSP are drained i.e. we have processed the CSB,
795 * before allowing ourselves to idle and calling intel_runtime_pm_put().
796 */
797 GEM_BUG_ON(!dev_priv->gt.awake);
798
Mika Kuoppalab620e872017-09-22 15:43:03 +0300799 intel_uncore_forcewake_get(dev_priv, execlists->fw_domains);
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000800
Chris Wilson899f6202017-03-21 11:33:20 +0000801 /* Prefer doing test_and_clear_bit() as a two stage operation to avoid
802 * imposing the cost of a locked atomic transaction when submitting a
803 * new request (outside of the context-switch interrupt).
804 */
805 while (test_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted)) {
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100806 /* The HWSP contains a (cacheable) mirror of the CSB */
807 const u32 *buf =
808 &engine->status_page.page_addr[I915_HWS_CSB_BUF0_INDEX];
Chris Wilson4af0d722017-03-25 20:10:53 +0000809 unsigned int head, tail;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100810
Mika Kuoppalab620e872017-09-22 15:43:03 +0300811 if (unlikely(execlists->csb_use_mmio)) {
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100812 buf = (u32 * __force)
813 (dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_BUF_LO(engine, 0)));
Mika Kuoppalab620e872017-09-22 15:43:03 +0300814 execlists->csb_head = -1; /* force mmio read of CSB ptrs */
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100815 }
816
Chris Wilson2e70b8c2017-03-23 13:48:03 +0000817 /* The write will be ordered by the uncached read (itself
818 * a memory barrier), so we do not need another in the form
819 * of a locked instruction. The race between the interrupt
820 * handler and the split test/clear is harmless as we order
821 * our clear before the CSB read. If the interrupt arrived
822 * first between the test and the clear, we read the updated
823 * CSB and clear the bit. If the interrupt arrives as we read
824 * the CSB or later (i.e. after we had cleared the bit) the bit
825 * is set and we do a new loop.
826 */
827 __clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300828 if (unlikely(execlists->csb_head == -1)) { /* following a reset */
Chris Wilson767a9832017-09-13 09:56:05 +0100829 head = readl(dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)));
830 tail = GEN8_CSB_WRITE_PTR(head);
831 head = GEN8_CSB_READ_PTR(head);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300832 execlists->csb_head = head;
Chris Wilson767a9832017-09-13 09:56:05 +0100833 } else {
834 const int write_idx =
835 intel_hws_csb_write_index(dev_priv) -
836 I915_HWS_CSB_BUF0_INDEX;
837
Mika Kuoppalab620e872017-09-22 15:43:03 +0300838 head = execlists->csb_head;
Chris Wilson767a9832017-09-13 09:56:05 +0100839 tail = READ_ONCE(buf[write_idx]);
840 }
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000841 GEM_TRACE("%s cs-irq head=%d [%d], tail=%d [%d]\n",
842 engine->name,
843 head, GEN8_CSB_READ_PTR(readl(dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)))),
844 tail, GEN8_CSB_WRITE_PTR(readl(dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)))));
Mika Kuoppalab620e872017-09-22 15:43:03 +0300845
Chris Wilson4af0d722017-03-25 20:10:53 +0000846 while (head != tail) {
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100847 struct drm_i915_gem_request *rq;
Chris Wilson4af0d722017-03-25 20:10:53 +0000848 unsigned int status;
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100849 unsigned int count;
Chris Wilsona37951a2017-01-24 11:00:06 +0000850
Chris Wilson4af0d722017-03-25 20:10:53 +0000851 if (++head == GEN8_CSB_ENTRIES)
852 head = 0;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100853
Chris Wilson2ffe80a2017-02-06 17:05:02 +0000854 /* We are flying near dragons again.
855 *
856 * We hold a reference to the request in execlist_port[]
857 * but no more than that. We are operating in softirq
858 * context and so cannot hold any mutex or sleep. That
859 * prevents us stopping the requests we are processing
860 * in port[] from being retired simultaneously (the
861 * breadcrumb will be complete before we see the
862 * context-switch). As we only hold the reference to the
863 * request, any pointer chasing underneath the request
864 * is subject to a potential use-after-free. Thus we
865 * store all of the bookkeeping within port[] as
866 * required, and avoid using unguarded pointers beneath
867 * request itself. The same applies to the atomic
868 * status notifier.
869 */
870
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100871 status = READ_ONCE(buf[2 * head]); /* maybe mmio! */
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000872 GEM_TRACE("%s csb[%dd]: status=0x%08x:0x%08x\n",
873 engine->name, head,
874 status, buf[2*head + 1]);
Chris Wilson70c2a242016-09-09 14:11:46 +0100875 if (!(status & GEN8_CTX_STATUS_COMPLETED_MASK))
876 continue;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100877
Chris Wilsonbeecec92017-10-03 21:34:52 +0100878 if (status & GEN8_CTX_STATUS_ACTIVE_IDLE &&
879 buf[2*head + 1] == PREEMPT_ID) {
Michał Winiarskia4598d12017-10-25 22:00:18 +0200880 execlists_cancel_port_requests(execlists);
881 execlists_unwind_incomplete_requests(execlists);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100882
Chris Wilson4a118ec2017-10-23 22:32:36 +0100883 GEM_BUG_ON(!execlists_is_active(execlists,
884 EXECLISTS_ACTIVE_PREEMPT));
885 execlists_clear_active(execlists,
886 EXECLISTS_ACTIVE_PREEMPT);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100887 continue;
888 }
889
890 if (status & GEN8_CTX_STATUS_PREEMPTED &&
Chris Wilson4a118ec2017-10-23 22:32:36 +0100891 execlists_is_active(execlists,
892 EXECLISTS_ACTIVE_PREEMPT))
Chris Wilsonbeecec92017-10-03 21:34:52 +0100893 continue;
894
Chris Wilson4a118ec2017-10-23 22:32:36 +0100895 GEM_BUG_ON(!execlists_is_active(execlists,
896 EXECLISTS_ACTIVE_USER));
897
Chris Wilson86aa7e72017-01-23 11:31:32 +0000898 /* Check the context/desc id for this event matches */
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100899 GEM_DEBUG_BUG_ON(buf[2 * head + 1] != port->context_id);
Chris Wilson86aa7e72017-01-23 11:31:32 +0000900
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100901 rq = port_unpack(port, &count);
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000902 GEM_TRACE("%s out[0]: ctx=%d.%d, seqno=%x\n",
903 engine->name,
904 rq->ctx->hw_id, count,
905 rq->global_seqno);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100906 GEM_BUG_ON(count == 0);
907 if (--count == 0) {
Chris Wilson70c2a242016-09-09 14:11:46 +0100908 GEM_BUG_ON(status & GEN8_CTX_STATUS_PREEMPTED);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100909 GEM_BUG_ON(!i915_gem_request_completed(rq));
910 execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_OUT);
Thomas Daniele981e7b2014-07-24 17:04:39 +0100911
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100912 trace_i915_gem_request_out(rq);
913 i915_gem_request_put(rq);
914
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300915 execlists_port_complete(execlists, port);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100916 } else {
917 port_set(port, port_pack(rq, count));
Chris Wilson70c2a242016-09-09 14:11:46 +0100918 }
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000919
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100920 /* After the final element, the hw should be idle */
921 GEM_BUG_ON(port_count(port) == 0 &&
Chris Wilson70c2a242016-09-09 14:11:46 +0100922 !(status & GEN8_CTX_STATUS_ACTIVE_IDLE));
Chris Wilson4a118ec2017-10-23 22:32:36 +0100923 if (port_count(port) == 0)
924 execlists_clear_active(execlists,
925 EXECLISTS_ACTIVE_USER);
Chris Wilson4af0d722017-03-25 20:10:53 +0000926 }
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000927
Mika Kuoppalab620e872017-09-22 15:43:03 +0300928 if (head != execlists->csb_head) {
929 execlists->csb_head = head;
Chris Wilson767a9832017-09-13 09:56:05 +0100930 writel(_MASKED_FIELD(GEN8_CSB_READ_PTR_MASK, head << 8),
931 dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)));
932 }
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000933 }
934
Chris Wilson4a118ec2017-10-23 22:32:36 +0100935 if (!execlists_is_active(execlists, EXECLISTS_ACTIVE_PREEMPT))
Chris Wilson70c2a242016-09-09 14:11:46 +0100936 execlists_dequeue(engine);
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000937
Mika Kuoppalab620e872017-09-22 15:43:03 +0300938 intel_uncore_forcewake_put(dev_priv, execlists->fw_domains);
Thomas Daniele981e7b2014-07-24 17:04:39 +0100939}
940
Chris Wilson27606fd2017-09-16 21:44:13 +0100941static void insert_request(struct intel_engine_cs *engine,
942 struct i915_priotree *pt,
943 int prio)
944{
945 struct i915_priolist *p = lookup_priolist(engine, pt, prio);
946
947 list_add_tail(&pt->link, &ptr_mask_bits(p, 1)->requests);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100948 if (ptr_unmask_bits(p, 1))
Mika Kuoppalab620e872017-09-22 15:43:03 +0300949 tasklet_hi_schedule(&engine->execlists.irq_tasklet);
Chris Wilson27606fd2017-09-16 21:44:13 +0100950}
951
Chris Wilsonf4ea6bd2016-08-02 22:50:32 +0100952static void execlists_submit_request(struct drm_i915_gem_request *request)
Michel Thierryacdd8842014-07-24 17:04:38 +0100953{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000954 struct intel_engine_cs *engine = request->engine;
Chris Wilson5590af32016-09-09 14:11:54 +0100955 unsigned long flags;
Michel Thierryacdd8842014-07-24 17:04:38 +0100956
Chris Wilson663f71e2016-11-14 20:41:00 +0000957 /* Will be called from irq-context when using foreign fences. */
958 spin_lock_irqsave(&engine->timeline->lock, flags);
Michel Thierryacdd8842014-07-24 17:04:38 +0100959
Chris Wilson27606fd2017-09-16 21:44:13 +0100960 insert_request(engine, &request->priotree, request->priotree.priority);
Michel Thierryacdd8842014-07-24 17:04:38 +0100961
Mika Kuoppalab620e872017-09-22 15:43:03 +0300962 GEM_BUG_ON(!engine->execlists.first);
Chris Wilson6c067572017-05-17 13:10:03 +0100963 GEM_BUG_ON(list_empty(&request->priotree.link));
964
Chris Wilson663f71e2016-11-14 20:41:00 +0000965 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Michel Thierryacdd8842014-07-24 17:04:38 +0100966}
967
Chris Wilson1f181222017-10-03 21:34:50 +0100968static struct drm_i915_gem_request *pt_to_request(struct i915_priotree *pt)
969{
970 return container_of(pt, struct drm_i915_gem_request, priotree);
971}
972
Chris Wilson20311bd2016-11-14 20:41:03 +0000973static struct intel_engine_cs *
974pt_lock_engine(struct i915_priotree *pt, struct intel_engine_cs *locked)
975{
Chris Wilson1f181222017-10-03 21:34:50 +0100976 struct intel_engine_cs *engine = pt_to_request(pt)->engine;
Chris Wilson20311bd2016-11-14 20:41:03 +0000977
Chris Wilsona79a5242017-03-27 21:21:43 +0100978 GEM_BUG_ON(!locked);
979
Chris Wilson20311bd2016-11-14 20:41:03 +0000980 if (engine != locked) {
Chris Wilsona79a5242017-03-27 21:21:43 +0100981 spin_unlock(&locked->timeline->lock);
982 spin_lock(&engine->timeline->lock);
Chris Wilson20311bd2016-11-14 20:41:03 +0000983 }
984
985 return engine;
986}
987
988static void execlists_schedule(struct drm_i915_gem_request *request, int prio)
989{
Chris Wilsona79a5242017-03-27 21:21:43 +0100990 struct intel_engine_cs *engine;
Chris Wilson20311bd2016-11-14 20:41:03 +0000991 struct i915_dependency *dep, *p;
992 struct i915_dependency stack;
993 LIST_HEAD(dfs);
994
Chris Wilson7d1ea602017-09-28 20:39:00 +0100995 GEM_BUG_ON(prio == I915_PRIORITY_INVALID);
996
Chris Wilson20311bd2016-11-14 20:41:03 +0000997 if (prio <= READ_ONCE(request->priotree.priority))
998 return;
999
Chris Wilson70cd1472016-11-28 14:36:49 +00001000 /* Need BKL in order to use the temporary link inside i915_dependency */
1001 lockdep_assert_held(&request->i915->drm.struct_mutex);
Chris Wilson20311bd2016-11-14 20:41:03 +00001002
1003 stack.signaler = &request->priotree;
1004 list_add(&stack.dfs_link, &dfs);
1005
1006 /* Recursively bump all dependent priorities to match the new request.
1007 *
1008 * A naive approach would be to use recursion:
1009 * static void update_priorities(struct i915_priotree *pt, prio) {
1010 * list_for_each_entry(dep, &pt->signalers_list, signal_link)
1011 * update_priorities(dep->signal, prio)
1012 * insert_request(pt);
1013 * }
1014 * but that may have unlimited recursion depth and so runs a very
1015 * real risk of overunning the kernel stack. Instead, we build
1016 * a flat list of all dependencies starting with the current request.
1017 * As we walk the list of dependencies, we add all of its dependencies
1018 * to the end of the list (this may include an already visited
1019 * request) and continue to walk onwards onto the new dependencies. The
1020 * end result is a topological list of requests in reverse order, the
1021 * last element in the list is the request we must execute first.
1022 */
1023 list_for_each_entry_safe(dep, p, &dfs, dfs_link) {
1024 struct i915_priotree *pt = dep->signaler;
1025
Chris Wilsona79a5242017-03-27 21:21:43 +01001026 /* Within an engine, there can be no cycle, but we may
1027 * refer to the same dependency chain multiple times
1028 * (redundant dependencies are not eliminated) and across
1029 * engines.
1030 */
1031 list_for_each_entry(p, &pt->signalers_list, signal_link) {
Chris Wilson1f181222017-10-03 21:34:50 +01001032 if (i915_gem_request_completed(pt_to_request(p->signaler)))
1033 continue;
1034
Chris Wilsona79a5242017-03-27 21:21:43 +01001035 GEM_BUG_ON(p->signaler->priority < pt->priority);
Chris Wilson20311bd2016-11-14 20:41:03 +00001036 if (prio > READ_ONCE(p->signaler->priority))
1037 list_move_tail(&p->dfs_link, &dfs);
Chris Wilsona79a5242017-03-27 21:21:43 +01001038 }
Chris Wilson20311bd2016-11-14 20:41:03 +00001039
Chris Wilson0798cff2016-12-05 14:29:41 +00001040 list_safe_reset_next(dep, p, dfs_link);
Chris Wilson20311bd2016-11-14 20:41:03 +00001041 }
1042
Chris Wilson349bdb62017-05-17 13:10:05 +01001043 /* If we didn't need to bump any existing priorities, and we haven't
1044 * yet submitted this request (i.e. there is no potential race with
1045 * execlists_submit_request()), we can set our own priority and skip
1046 * acquiring the engine locks.
1047 */
Chris Wilson7d1ea602017-09-28 20:39:00 +01001048 if (request->priotree.priority == I915_PRIORITY_INVALID) {
Chris Wilson349bdb62017-05-17 13:10:05 +01001049 GEM_BUG_ON(!list_empty(&request->priotree.link));
1050 request->priotree.priority = prio;
1051 if (stack.dfs_link.next == stack.dfs_link.prev)
1052 return;
1053 __list_del_entry(&stack.dfs_link);
1054 }
1055
Chris Wilsona79a5242017-03-27 21:21:43 +01001056 engine = request->engine;
1057 spin_lock_irq(&engine->timeline->lock);
1058
Chris Wilson20311bd2016-11-14 20:41:03 +00001059 /* Fifo and depth-first replacement ensure our deps execute before us */
1060 list_for_each_entry_safe_reverse(dep, p, &dfs, dfs_link) {
1061 struct i915_priotree *pt = dep->signaler;
1062
1063 INIT_LIST_HEAD(&dep->dfs_link);
1064
1065 engine = pt_lock_engine(pt, engine);
1066
1067 if (prio <= pt->priority)
1068 continue;
1069
Chris Wilson20311bd2016-11-14 20:41:03 +00001070 pt->priority = prio;
Chris Wilson6c067572017-05-17 13:10:03 +01001071 if (!list_empty(&pt->link)) {
1072 __list_del_entry(&pt->link);
1073 insert_request(engine, pt, prio);
Chris Wilsona79a5242017-03-27 21:21:43 +01001074 }
Chris Wilson20311bd2016-11-14 20:41:03 +00001075 }
1076
Chris Wilsona79a5242017-03-27 21:21:43 +01001077 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson20311bd2016-11-14 20:41:03 +00001078}
1079
Chris Wilsonf4e15af2017-11-10 14:26:32 +00001080static int __context_pin(struct i915_gem_context *ctx, struct i915_vma *vma)
1081{
1082 unsigned int flags;
1083 int err;
1084
1085 /*
1086 * Clear this page out of any CPU caches for coherent swap-in/out.
1087 * We only want to do this on the first bind so that we do not stall
1088 * on an active context (which by nature is already on the GPU).
1089 */
1090 if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
1091 err = i915_gem_object_set_to_gtt_domain(vma->obj, true);
1092 if (err)
1093 return err;
1094 }
1095
1096 flags = PIN_GLOBAL | PIN_HIGH;
1097 if (ctx->ggtt_offset_bias)
1098 flags |= PIN_OFFSET_BIAS | ctx->ggtt_offset_bias;
1099
1100 return i915_vma_pin(vma, 0, GEN8_LR_CONTEXT_ALIGN, flags);
1101}
1102
Chris Wilson266a2402017-05-04 10:33:08 +01001103static struct intel_ring *
1104execlists_context_pin(struct intel_engine_cs *engine,
1105 struct i915_gem_context *ctx)
Oscar Mateodcb4c122014-11-13 10:28:10 +00001106{
Chris Wilson9021ad02016-05-24 14:53:37 +01001107 struct intel_context *ce = &ctx->engine[engine->id];
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001108 void *vaddr;
Tvrtko Ursulinca825802016-01-15 15:10:27 +00001109 int ret;
Oscar Mateodcb4c122014-11-13 10:28:10 +00001110
Chris Wilson91c8a322016-07-05 10:40:23 +01001111 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Tvrtko Ursulinca825802016-01-15 15:10:27 +00001112
Chris Wilson266a2402017-05-04 10:33:08 +01001113 if (likely(ce->pin_count++))
1114 goto out;
Chris Wilsona533b4b2017-03-16 17:16:28 +00001115 GEM_BUG_ON(!ce->pin_count); /* no overflow please! */
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001116
Chris Wilsone8a9c582016-12-18 15:37:20 +00001117 if (!ce->state) {
1118 ret = execlists_context_deferred_alloc(ctx, engine);
1119 if (ret)
1120 goto err;
1121 }
Chris Wilson56f6e0a2017-01-05 15:30:20 +00001122 GEM_BUG_ON(!ce->state);
Chris Wilsone8a9c582016-12-18 15:37:20 +00001123
Chris Wilsonf4e15af2017-11-10 14:26:32 +00001124 ret = __context_pin(ctx, ce->state);
Nick Hoathe84fe802015-09-11 12:53:46 +01001125 if (ret)
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001126 goto err;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001127
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001128 vaddr = i915_gem_object_pin_map(ce->state->obj, I915_MAP_WB);
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001129 if (IS_ERR(vaddr)) {
1130 ret = PTR_ERR(vaddr);
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001131 goto unpin_vma;
Tvrtko Ursulin82352e92016-01-15 17:12:45 +00001132 }
1133
Chris Wilsond822bb12017-04-03 12:34:25 +01001134 ret = intel_ring_pin(ce->ring, ctx->i915, ctx->ggtt_offset_bias);
Nick Hoathe84fe802015-09-11 12:53:46 +01001135 if (ret)
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001136 goto unpin_map;
Alex Daid1675192015-08-12 15:43:43 +01001137
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001138 intel_lr_context_descriptor_update(ctx, engine);
Chris Wilson9021ad02016-05-24 14:53:37 +01001139
Chris Wilsona3aabe82016-10-04 21:11:26 +01001140 ce->lrc_reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
1141 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001142 i915_ggtt_offset(ce->ring->vma);
Chris Wilsona3aabe82016-10-04 21:11:26 +01001143
Chris Wilson3d574a62017-10-13 21:26:16 +01001144 ce->state->obj->pin_global++;
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001145 i915_gem_context_get(ctx);
Chris Wilson266a2402017-05-04 10:33:08 +01001146out:
1147 return ce->ring;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001148
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001149unpin_map:
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001150 i915_gem_object_unpin_map(ce->state->obj);
1151unpin_vma:
1152 __i915_vma_unpin(ce->state);
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001153err:
Chris Wilson9021ad02016-05-24 14:53:37 +01001154 ce->pin_count = 0;
Chris Wilson266a2402017-05-04 10:33:08 +01001155 return ERR_PTR(ret);
Oscar Mateodcb4c122014-11-13 10:28:10 +00001156}
1157
Chris Wilsone8a9c582016-12-18 15:37:20 +00001158static void execlists_context_unpin(struct intel_engine_cs *engine,
1159 struct i915_gem_context *ctx)
Oscar Mateodcb4c122014-11-13 10:28:10 +00001160{
Chris Wilson9021ad02016-05-24 14:53:37 +01001161 struct intel_context *ce = &ctx->engine[engine->id];
Daniel Vetteraf3302b2015-12-04 17:27:15 +01001162
Chris Wilson91c8a322016-07-05 10:40:23 +01001163 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilson9021ad02016-05-24 14:53:37 +01001164 GEM_BUG_ON(ce->pin_count == 0);
Tvrtko Ursulin321fe302016-01-28 10:29:55 +00001165
Chris Wilson9021ad02016-05-24 14:53:37 +01001166 if (--ce->pin_count)
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001167 return;
1168
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001169 intel_ring_unpin(ce->ring);
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001170
Chris Wilson3d574a62017-10-13 21:26:16 +01001171 ce->state->obj->pin_global--;
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001172 i915_gem_object_unpin_map(ce->state->obj);
1173 i915_vma_unpin(ce->state);
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001174
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001175 i915_gem_context_put(ctx);
Oscar Mateodcb4c122014-11-13 10:28:10 +00001176}
1177
Chris Wilsonf73e7392016-12-18 15:37:24 +00001178static int execlists_request_alloc(struct drm_i915_gem_request *request)
Chris Wilsonef11c012016-12-18 15:37:19 +00001179{
1180 struct intel_engine_cs *engine = request->engine;
1181 struct intel_context *ce = &request->ctx->engine[engine->id];
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001182 u32 *cs;
Chris Wilsonef11c012016-12-18 15:37:19 +00001183 int ret;
1184
Chris Wilsone8a9c582016-12-18 15:37:20 +00001185 GEM_BUG_ON(!ce->pin_count);
1186
Chris Wilsonef11c012016-12-18 15:37:19 +00001187 /* Flush enough space to reduce the likelihood of waiting after
1188 * we start building the request - in which case we will just
1189 * have to repeat work.
1190 */
1191 request->reserved_space += EXECLISTS_REQUEST_SIZE;
1192
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001193 cs = intel_ring_begin(request, 0);
Michał Winiarski85e2fe62017-09-14 10:32:13 +02001194 if (IS_ERR(cs))
1195 return PTR_ERR(cs);
Chris Wilsonef11c012016-12-18 15:37:19 +00001196
1197 if (!ce->initialised) {
1198 ret = engine->init_context(request);
1199 if (ret)
Michał Winiarski85e2fe62017-09-14 10:32:13 +02001200 return ret;
Chris Wilsonef11c012016-12-18 15:37:19 +00001201
1202 ce->initialised = true;
1203 }
1204
1205 /* Note that after this point, we have committed to using
1206 * this request as it is being used to both track the
1207 * state of engine initialisation and liveness of the
1208 * golden renderstate above. Think twice before you try
1209 * to cancel/unwind this request now.
1210 */
1211
1212 request->reserved_space -= EXECLISTS_REQUEST_SIZE;
1213 return 0;
Chris Wilsonef11c012016-12-18 15:37:19 +00001214}
1215
Arun Siluvery9e000842015-07-03 14:27:31 +01001216/*
1217 * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
1218 * PIPE_CONTROL instruction. This is required for the flush to happen correctly
1219 * but there is a slight complication as this is applied in WA batch where the
1220 * values are only initialized once so we cannot take register value at the
1221 * beginning and reuse it further; hence we save its value to memory, upload a
1222 * constant value with bit21 set and then we restore it back with the saved value.
1223 * To simplify the WA, a constant value is formed by using the default value
1224 * of this register. This shouldn't be a problem because we are only modifying
1225 * it for a short period and this batch in non-premptible. We can ofcourse
1226 * use additional instructions that read the actual value of the register
1227 * at that time and set our bit of interest but it makes the WA complicated.
1228 *
1229 * This WA is also required for Gen9 so extracting as a function avoids
1230 * code duplication.
1231 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001232static u32 *
1233gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery9e000842015-07-03 14:27:31 +01001234{
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001235 *batch++ = MI_STORE_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1236 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1237 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
1238 *batch++ = 0;
Arun Siluvery9e000842015-07-03 14:27:31 +01001239
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001240 *batch++ = MI_LOAD_REGISTER_IMM(1);
1241 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1242 *batch++ = 0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES;
Arun Siluvery9e000842015-07-03 14:27:31 +01001243
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001244 batch = gen8_emit_pipe_control(batch,
1245 PIPE_CONTROL_CS_STALL |
1246 PIPE_CONTROL_DC_FLUSH_ENABLE,
1247 0);
Arun Siluvery9e000842015-07-03 14:27:31 +01001248
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001249 *batch++ = MI_LOAD_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1250 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1251 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
1252 *batch++ = 0;
Arun Siluvery9e000842015-07-03 14:27:31 +01001253
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001254 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001255}
1256
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001257/*
1258 * Typically we only have one indirect_ctx and per_ctx batch buffer which are
1259 * initialized at the beginning and shared across all contexts but this field
1260 * helps us to have multiple batches at different offsets and select them based
1261 * on a criteria. At the moment this batch always start at the beginning of the page
1262 * and at this point we don't have multiple wa_ctx batch buffers.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001263 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001264 * The number of WA applied are not known at the beginning; we use this field
1265 * to return the no of DWORDS written.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001266 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001267 * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
1268 * so it adds NOOPs as padding to make it cacheline aligned.
1269 * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
1270 * makes a complete batch buffer.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001271 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001272static u32 *gen8_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001273{
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001274 /* WaDisableCtxRestoreArbitration:bdw,chv */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001275 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001276
Arun Siluveryc82435b2015-06-19 18:37:13 +01001277 /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001278 if (IS_BROADWELL(engine->i915))
1279 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
Arun Siluveryc82435b2015-06-19 18:37:13 +01001280
Arun Siluvery0160f052015-06-23 15:46:57 +01001281 /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
1282 /* Actual scratch location is at 128 bytes offset */
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001283 batch = gen8_emit_pipe_control(batch,
1284 PIPE_CONTROL_FLUSH_L3 |
1285 PIPE_CONTROL_GLOBAL_GTT_IVB |
1286 PIPE_CONTROL_CS_STALL |
1287 PIPE_CONTROL_QW_WRITE,
1288 i915_ggtt_offset(engine->scratch) +
1289 2 * CACHELINE_BYTES);
Arun Siluvery0160f052015-06-23 15:46:57 +01001290
Chris Wilsonbeecec92017-10-03 21:34:52 +01001291 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1292
Arun Siluvery17ee9502015-06-19 19:07:01 +01001293 /* Pad to end of cacheline */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001294 while ((unsigned long)batch % CACHELINE_BYTES)
1295 *batch++ = MI_NOOP;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001296
1297 /*
1298 * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
1299 * execution depends on the length specified in terms of cache lines
1300 * in the register CTX_RCS_INDIRECT_CTX
1301 */
1302
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001303 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001304}
1305
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001306static u32 *gen9_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery0504cff2015-07-14 15:01:27 +01001307{
Chris Wilsonbeecec92017-10-03 21:34:52 +01001308 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
1309
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001310 /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt,glk */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001311 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
Arun Siluverya4106a72015-07-14 15:01:29 +01001312
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001313 /* WaDisableGatherAtSetShaderCommonSlice:skl,bxt,kbl,glk */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001314 *batch++ = MI_LOAD_REGISTER_IMM(1);
1315 *batch++ = i915_mmio_reg_offset(COMMON_SLICE_CHICKEN2);
1316 *batch++ = _MASKED_BIT_DISABLE(
1317 GEN9_DISABLE_GATHER_AT_SET_SHADER_COMMON_SLICE);
1318 *batch++ = MI_NOOP;
Mika Kuoppala873e8172016-07-20 14:26:13 +03001319
Mika Kuoppala066d4622016-06-07 17:19:15 +03001320 /* WaClearSlmSpaceAtContextSwitch:kbl */
1321 /* Actual scratch location is at 128 bytes offset */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001322 if (IS_KBL_REVID(engine->i915, 0, KBL_REVID_A0)) {
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001323 batch = gen8_emit_pipe_control(batch,
1324 PIPE_CONTROL_FLUSH_L3 |
1325 PIPE_CONTROL_GLOBAL_GTT_IVB |
1326 PIPE_CONTROL_CS_STALL |
1327 PIPE_CONTROL_QW_WRITE,
1328 i915_ggtt_offset(engine->scratch)
1329 + 2 * CACHELINE_BYTES);
Mika Kuoppala066d4622016-06-07 17:19:15 +03001330 }
Tim Gore3485d992016-07-05 10:01:30 +01001331
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001332 /* WaMediaPoolStateCmdInWABB:bxt,glk */
Tim Gore3485d992016-07-05 10:01:30 +01001333 if (HAS_POOLED_EU(engine->i915)) {
1334 /*
1335 * EU pool configuration is setup along with golden context
1336 * during context initialization. This value depends on
1337 * device type (2x6 or 3x6) and needs to be updated based
1338 * on which subslice is disabled especially for 2x6
1339 * devices, however it is safe to load default
1340 * configuration of 3x6 device instead of masking off
1341 * corresponding bits because HW ignores bits of a disabled
1342 * subslice and drops down to appropriate config. Please
1343 * see render_state_setup() in i915_gem_render_state.c for
1344 * possible configurations, to avoid duplication they are
1345 * not shown here again.
1346 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001347 *batch++ = GEN9_MEDIA_POOL_STATE;
1348 *batch++ = GEN9_MEDIA_POOL_ENABLE;
1349 *batch++ = 0x00777000;
1350 *batch++ = 0;
1351 *batch++ = 0;
1352 *batch++ = 0;
Tim Gore3485d992016-07-05 10:01:30 +01001353 }
1354
Chris Wilsonbeecec92017-10-03 21:34:52 +01001355 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1356
Arun Siluvery0504cff2015-07-14 15:01:27 +01001357 /* Pad to end of cacheline */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001358 while ((unsigned long)batch % CACHELINE_BYTES)
1359 *batch++ = MI_NOOP;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001360
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001361 return batch;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001362}
1363
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001364#define CTX_WA_BB_OBJ_SIZE (PAGE_SIZE)
1365
1366static int lrc_setup_wa_ctx(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001367{
Chris Wilson48bb74e2016-08-15 10:49:04 +01001368 struct drm_i915_gem_object *obj;
1369 struct i915_vma *vma;
1370 int err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001371
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001372 obj = i915_gem_object_create(engine->i915, CTX_WA_BB_OBJ_SIZE);
Chris Wilson48bb74e2016-08-15 10:49:04 +01001373 if (IS_ERR(obj))
1374 return PTR_ERR(obj);
1375
Chris Wilsona01cb372017-01-16 15:21:30 +00001376 vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
Chris Wilson48bb74e2016-08-15 10:49:04 +01001377 if (IS_ERR(vma)) {
1378 err = PTR_ERR(vma);
1379 goto err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001380 }
1381
Chris Wilson48bb74e2016-08-15 10:49:04 +01001382 err = i915_vma_pin(vma, 0, PAGE_SIZE, PIN_GLOBAL | PIN_HIGH);
1383 if (err)
1384 goto err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001385
Chris Wilson48bb74e2016-08-15 10:49:04 +01001386 engine->wa_ctx.vma = vma;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001387 return 0;
Chris Wilson48bb74e2016-08-15 10:49:04 +01001388
1389err:
1390 i915_gem_object_put(obj);
1391 return err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001392}
1393
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001394static void lrc_destroy_wa_ctx(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001395{
Chris Wilson19880c42016-08-15 10:49:05 +01001396 i915_vma_unpin_and_release(&engine->wa_ctx.vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001397}
1398
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001399typedef u32 *(*wa_bb_func_t)(struct intel_engine_cs *engine, u32 *batch);
1400
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001401static int intel_init_workaround_bb(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001402{
Chris Wilson48bb74e2016-08-15 10:49:04 +01001403 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001404 struct i915_wa_ctx_bb *wa_bb[2] = { &wa_ctx->indirect_ctx,
1405 &wa_ctx->per_ctx };
1406 wa_bb_func_t wa_bb_fn[2];
Arun Siluvery17ee9502015-06-19 19:07:01 +01001407 struct page *page;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001408 void *batch, *batch_ptr;
1409 unsigned int i;
Chris Wilson48bb74e2016-08-15 10:49:04 +01001410 int ret;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001411
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001412 if (WARN_ON(engine->id != RCS || !engine->scratch))
1413 return -EINVAL;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001414
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001415 switch (INTEL_GEN(engine->i915)) {
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001416 case 10:
1417 return 0;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001418 case 9:
1419 wa_bb_fn[0] = gen9_init_indirectctx_bb;
Chris Wilsonb8aa2232017-09-21 14:54:44 +01001420 wa_bb_fn[1] = NULL;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001421 break;
1422 case 8:
1423 wa_bb_fn[0] = gen8_init_indirectctx_bb;
Chris Wilson3ad7b522017-10-03 21:34:49 +01001424 wa_bb_fn[1] = NULL;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001425 break;
1426 default:
1427 MISSING_CASE(INTEL_GEN(engine->i915));
Arun Siluvery5e60d792015-06-23 15:50:44 +01001428 return 0;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001429 }
Arun Siluvery5e60d792015-06-23 15:50:44 +01001430
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001431 ret = lrc_setup_wa_ctx(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001432 if (ret) {
1433 DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
1434 return ret;
1435 }
1436
Chris Wilson48bb74e2016-08-15 10:49:04 +01001437 page = i915_gem_object_get_dirty_page(wa_ctx->vma->obj, 0);
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001438 batch = batch_ptr = kmap_atomic(page);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001439
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001440 /*
1441 * Emit the two workaround batch buffers, recording the offset from the
1442 * start of the workaround batch buffer object for each and their
1443 * respective sizes.
1444 */
1445 for (i = 0; i < ARRAY_SIZE(wa_bb_fn); i++) {
1446 wa_bb[i]->offset = batch_ptr - batch;
1447 if (WARN_ON(!IS_ALIGNED(wa_bb[i]->offset, CACHELINE_BYTES))) {
1448 ret = -EINVAL;
1449 break;
1450 }
Chris Wilson604a8f62017-09-21 14:54:43 +01001451 if (wa_bb_fn[i])
1452 batch_ptr = wa_bb_fn[i](engine, batch_ptr);
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001453 wa_bb[i]->size = batch_ptr - (batch + wa_bb[i]->offset);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001454 }
1455
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001456 BUG_ON(batch_ptr - batch > CTX_WA_BB_OBJ_SIZE);
1457
Arun Siluvery17ee9502015-06-19 19:07:01 +01001458 kunmap_atomic(batch);
1459 if (ret)
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001460 lrc_destroy_wa_ctx(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001461
1462 return ret;
1463}
1464
Chris Wilson64f09f02017-08-07 13:19:19 +01001465static u8 gtiir[] = {
1466 [RCS] = 0,
1467 [BCS] = 0,
1468 [VCS] = 1,
1469 [VCS2] = 1,
1470 [VECS] = 3,
1471};
1472
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001473static int gen8_init_common_ring(struct intel_engine_cs *engine)
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001474{
Chris Wilsonc0336662016-05-06 15:40:21 +01001475 struct drm_i915_private *dev_priv = engine->i915;
Mika Kuoppalab620e872017-09-22 15:43:03 +03001476 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001477 int ret;
1478
1479 ret = intel_mocs_init_engine(engine);
1480 if (ret)
1481 return ret;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001482
Chris Wilsonad07dfc2016-10-07 07:53:26 +01001483 intel_engine_reset_breadcrumbs(engine);
Chris Wilsonf3b8f912017-01-05 15:30:21 +00001484 intel_engine_init_hangcheck(engine);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001485
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001486 I915_WRITE(RING_HWSTAM(engine->mmio_base), 0xffffffff);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001487 I915_WRITE(RING_MODE_GEN7(engine),
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001488 _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
Chris Wilsonf3b8f912017-01-05 15:30:21 +00001489 I915_WRITE(RING_HWS_PGA(engine->mmio_base),
1490 engine->status_page.ggtt_offset);
1491 POSTING_READ(RING_HWS_PGA(engine->mmio_base));
Michel Thierrydfc53c52015-09-28 13:25:12 +01001492
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001493 DRM_DEBUG_DRIVER("Execlists enabled for %s\n", engine->name);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001494
Chris Wilson64f09f02017-08-07 13:19:19 +01001495 GEM_BUG_ON(engine->id >= ARRAY_SIZE(gtiir));
1496
1497 /*
1498 * Clear any pending interrupt state.
1499 *
1500 * We do it twice out of paranoia that some of the IIR are double
1501 * buffered, and if we only reset it once there may still be
1502 * an interrupt pending.
1503 */
1504 I915_WRITE(GEN8_GT_IIR(gtiir[engine->id]),
1505 GT_CONTEXT_SWITCH_INTERRUPT << engine->irq_shift);
1506 I915_WRITE(GEN8_GT_IIR(gtiir[engine->id]),
1507 GT_CONTEXT_SWITCH_INTERRUPT << engine->irq_shift);
Chris Wilsonf7470262017-01-24 15:20:21 +00001508 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
Mika Kuoppalab620e872017-09-22 15:43:03 +03001509 execlists->csb_head = -1;
Chris Wilson4a118ec2017-10-23 22:32:36 +01001510 execlists->active = 0;
Chris Wilson6b764a52017-04-25 11:38:35 +01001511
Chris Wilson64f09f02017-08-07 13:19:19 +01001512 /* After a GPU reset, we may have requests to replay */
Michał Winiarski9bdc3572017-10-25 18:25:19 +01001513 if (execlists->first)
Mika Kuoppalab620e872017-09-22 15:43:03 +03001514 tasklet_schedule(&execlists->irq_tasklet);
Chris Wilson6b764a52017-04-25 11:38:35 +01001515
Chris Wilson821ed7d2016-09-09 14:11:53 +01001516 return 0;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001517}
1518
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001519static int gen8_init_render_ring(struct intel_engine_cs *engine)
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001520{
Chris Wilsonc0336662016-05-06 15:40:21 +01001521 struct drm_i915_private *dev_priv = engine->i915;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001522 int ret;
1523
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001524 ret = gen8_init_common_ring(engine);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001525 if (ret)
1526 return ret;
1527
1528 /* We need to disable the AsyncFlip performance optimisations in order
1529 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1530 * programmed to '1' on all products.
1531 *
1532 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
1533 */
1534 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1535
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001536 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1537
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001538 return init_workarounds_ring(engine);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001539}
1540
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001541static int gen9_init_render_ring(struct intel_engine_cs *engine)
Damien Lespiau82ef8222015-02-09 19:33:08 +00001542{
1543 int ret;
1544
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001545 ret = gen8_init_common_ring(engine);
Damien Lespiau82ef8222015-02-09 19:33:08 +00001546 if (ret)
1547 return ret;
1548
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001549 return init_workarounds_ring(engine);
Damien Lespiau82ef8222015-02-09 19:33:08 +00001550}
1551
Chris Wilson821ed7d2016-09-09 14:11:53 +01001552static void reset_common_ring(struct intel_engine_cs *engine,
1553 struct drm_i915_gem_request *request)
1554{
Mika Kuoppalab620e872017-09-22 15:43:03 +03001555 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001556 struct intel_context *ce;
Chris Wilson221ab97192017-09-16 21:44:14 +01001557 unsigned long flags;
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001558
Chris Wilson221ab97192017-09-16 21:44:14 +01001559 spin_lock_irqsave(&engine->timeline->lock, flags);
1560
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001561 /*
1562 * Catch up with any missed context-switch interrupts.
1563 *
1564 * Ideally we would just read the remaining CSB entries now that we
1565 * know the gpu is idle. However, the CSB registers are sometimes^W
1566 * often trashed across a GPU reset! Instead we have to rely on
1567 * guessing the missed context-switch events by looking at what
1568 * requests were completed.
1569 */
Michał Winiarskia4598d12017-10-25 22:00:18 +02001570 execlists_cancel_port_requests(execlists);
Chris Wilson221ab97192017-09-16 21:44:14 +01001571
1572 /* Push back any incomplete requests for replay after the reset. */
Michał Winiarskia4598d12017-10-25 22:00:18 +02001573 __unwind_incomplete_requests(engine);
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001574
Chris Wilson221ab97192017-09-16 21:44:14 +01001575 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001576
1577 /* If the request was innocent, we leave the request in the ELSP
1578 * and will try to replay it on restarting. The context image may
1579 * have been corrupted by the reset, in which case we may have
1580 * to service a new GPU hang, but more likely we can continue on
1581 * without impact.
1582 *
1583 * If the request was guilty, we presume the context is corrupt
1584 * and have to at least restore the RING register in the context
1585 * image back to the expected values to skip over the guilty request.
1586 */
Chris Wilson221ab97192017-09-16 21:44:14 +01001587 if (!request || request->fence.error != -EIO)
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001588 return;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001589
Chris Wilsona3aabe82016-10-04 21:11:26 +01001590 /* We want a simple context + ring to execute the breadcrumb update.
1591 * We cannot rely on the context being intact across the GPU hang,
1592 * so clear it and rebuild just what we need for the breadcrumb.
1593 * All pending requests for this context will be zapped, and any
1594 * future request will be after userspace has had the opportunity
1595 * to recreate its own state.
1596 */
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001597 ce = &request->ctx->engine[engine->id];
Chris Wilsona3aabe82016-10-04 21:11:26 +01001598 execlists_init_reg_state(ce->lrc_reg_state,
1599 request->ctx, engine, ce->ring);
1600
Chris Wilson821ed7d2016-09-09 14:11:53 +01001601 /* Move the RING_HEAD onto the breadcrumb, past the hanging batch */
Chris Wilsona3aabe82016-10-04 21:11:26 +01001602 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
1603 i915_ggtt_offset(ce->ring->vma);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001604 ce->lrc_reg_state[CTX_RING_HEAD+1] = request->postfix;
Chris Wilsona3aabe82016-10-04 21:11:26 +01001605
Chris Wilson821ed7d2016-09-09 14:11:53 +01001606 request->ring->head = request->postfix;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001607 intel_ring_update_space(request->ring);
1608
Chris Wilsona3aabe82016-10-04 21:11:26 +01001609 /* Reset WaIdleLiteRestore:bdw,skl as well */
Chris Wilson7e4992a2017-09-28 20:38:59 +01001610 unwind_wa_tail(request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001611}
1612
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001613static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request *req)
1614{
1615 struct i915_hw_ppgtt *ppgtt = req->ctx->ppgtt;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001616 struct intel_engine_cs *engine = req->engine;
Mika Kuoppalae7167762017-02-28 17:28:10 +02001617 const int num_lri_cmds = GEN8_3LVL_PDPES * 2;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001618 u32 *cs;
1619 int i;
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001620
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001621 cs = intel_ring_begin(req, num_lri_cmds * 2 + 2);
1622 if (IS_ERR(cs))
1623 return PTR_ERR(cs);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001624
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001625 *cs++ = MI_LOAD_REGISTER_IMM(num_lri_cmds);
Mika Kuoppalae7167762017-02-28 17:28:10 +02001626 for (i = GEN8_3LVL_PDPES - 1; i >= 0; i--) {
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001627 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
1628
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001629 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, i));
1630 *cs++ = upper_32_bits(pd_daddr);
1631 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, i));
1632 *cs++ = lower_32_bits(pd_daddr);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001633 }
1634
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001635 *cs++ = MI_NOOP;
1636 intel_ring_advance(req, cs);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001637
1638 return 0;
1639}
1640
John Harrisonbe795fc2015-05-29 17:44:03 +01001641static int gen8_emit_bb_start(struct drm_i915_gem_request *req,
Chris Wilson803688b2016-08-02 22:50:27 +01001642 u64 offset, u32 len,
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001643 const unsigned int flags)
Oscar Mateo15648582014-07-24 17:04:32 +01001644{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001645 u32 *cs;
Oscar Mateo15648582014-07-24 17:04:32 +01001646 int ret;
1647
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001648 /* Don't rely in hw updating PDPs, specially in lite-restore.
1649 * Ideally, we should set Force PD Restore in ctx descriptor,
1650 * but we can't. Force Restore would be a second option, but
1651 * it is unsafe in case of lite-restore (because the ctx is
Michel Thierry2dba3232015-07-30 11:06:23 +01001652 * not idle). PML4 is allocated during ppgtt init so this is
1653 * not needed in 48-bit.*/
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001654 if (req->ctx->ppgtt &&
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001655 (intel_engine_flag(req->engine) & req->ctx->ppgtt->pd_dirty_rings) &&
1656 !i915_vm_is_48bit(&req->ctx->ppgtt->base) &&
1657 !intel_vgpu_active(req->i915)) {
1658 ret = intel_logical_ring_emit_pdps(req);
1659 if (ret)
1660 return ret;
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001661
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00001662 req->ctx->ppgtt->pd_dirty_rings &= ~intel_engine_flag(req->engine);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001663 }
1664
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001665 cs = intel_ring_begin(req, 4);
1666 if (IS_ERR(cs))
1667 return PTR_ERR(cs);
Oscar Mateo15648582014-07-24 17:04:32 +01001668
Chris Wilson279f5a02017-10-05 20:10:05 +01001669 /*
1670 * WaDisableCtxRestoreArbitration:bdw,chv
1671 *
1672 * We don't need to perform MI_ARB_ENABLE as often as we do (in
1673 * particular all the gen that do not need the w/a at all!), if we
1674 * took care to make sure that on every switch into this context
1675 * (both ordinary and for preemption) that arbitrartion was enabled
1676 * we would be fine. However, there doesn't seem to be a downside to
1677 * being paranoid and making sure it is set before each batch and
1678 * every context-switch.
1679 *
1680 * Note that if we fail to enable arbitration before the request
1681 * is complete, then we do not see the context-switch interrupt and
1682 * the engine hangs (with RING_HEAD == RING_TAIL).
1683 *
1684 * That satisfies both the GPGPU w/a and our heavy-handed paranoia.
1685 */
Chris Wilson3ad7b522017-10-03 21:34:49 +01001686 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1687
Oscar Mateo15648582014-07-24 17:04:32 +01001688 /* FIXME(BDW): Address space and security selectors. */
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001689 *cs++ = MI_BATCH_BUFFER_START_GEN8 |
1690 (flags & I915_DISPATCH_SECURE ? 0 : BIT(8)) |
1691 (flags & I915_DISPATCH_RS ? MI_BATCH_RESOURCE_STREAMER : 0);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001692 *cs++ = lower_32_bits(offset);
1693 *cs++ = upper_32_bits(offset);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001694 intel_ring_advance(req, cs);
Oscar Mateo15648582014-07-24 17:04:32 +01001695
1696 return 0;
1697}
1698
Chris Wilson31bb59c2016-07-01 17:23:27 +01001699static void gen8_logical_ring_enable_irq(struct intel_engine_cs *engine)
Oscar Mateo73d477f2014-07-24 17:04:31 +01001700{
Chris Wilsonc0336662016-05-06 15:40:21 +01001701 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson31bb59c2016-07-01 17:23:27 +01001702 I915_WRITE_IMR(engine,
1703 ~(engine->irq_enable_mask | engine->irq_keep_mask));
1704 POSTING_READ_FW(RING_IMR(engine->mmio_base));
Oscar Mateo73d477f2014-07-24 17:04:31 +01001705}
1706
Chris Wilson31bb59c2016-07-01 17:23:27 +01001707static void gen8_logical_ring_disable_irq(struct intel_engine_cs *engine)
Oscar Mateo73d477f2014-07-24 17:04:31 +01001708{
Chris Wilsonc0336662016-05-06 15:40:21 +01001709 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson31bb59c2016-07-01 17:23:27 +01001710 I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
Oscar Mateo73d477f2014-07-24 17:04:31 +01001711}
1712
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001713static int gen8_emit_flush(struct drm_i915_gem_request *request, u32 mode)
Oscar Mateo47122742014-07-24 17:04:28 +01001714{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001715 u32 cmd, *cs;
Oscar Mateo47122742014-07-24 17:04:28 +01001716
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001717 cs = intel_ring_begin(request, 4);
1718 if (IS_ERR(cs))
1719 return PTR_ERR(cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001720
1721 cmd = MI_FLUSH_DW + 1;
1722
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001723 /* We always require a command barrier so that subsequent
1724 * commands, such as breadcrumb interrupts, are strictly ordered
1725 * wrt the contents of the write cache being flushed to memory
1726 * (and thus being coherent from the CPU).
1727 */
1728 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1729
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001730 if (mode & EMIT_INVALIDATE) {
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001731 cmd |= MI_INVALIDATE_TLB;
Chris Wilson1dae2df2016-08-02 22:50:19 +01001732 if (request->engine->id == VCS)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001733 cmd |= MI_INVALIDATE_BSD;
Oscar Mateo47122742014-07-24 17:04:28 +01001734 }
1735
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001736 *cs++ = cmd;
1737 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
1738 *cs++ = 0; /* upper addr */
1739 *cs++ = 0; /* value */
1740 intel_ring_advance(request, cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001741
1742 return 0;
1743}
1744
John Harrison7deb4d32015-05-29 17:43:59 +01001745static int gen8_emit_flush_render(struct drm_i915_gem_request *request,
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001746 u32 mode)
Oscar Mateo47122742014-07-24 17:04:28 +01001747{
Chris Wilsonb5321f32016-08-02 22:50:18 +01001748 struct intel_engine_cs *engine = request->engine;
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001749 u32 scratch_addr =
1750 i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001751 bool vf_flush_wa = false, dc_flush_wa = false;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001752 u32 *cs, flags = 0;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001753 int len;
Oscar Mateo47122742014-07-24 17:04:28 +01001754
1755 flags |= PIPE_CONTROL_CS_STALL;
1756
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001757 if (mode & EMIT_FLUSH) {
Oscar Mateo47122742014-07-24 17:04:28 +01001758 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
1759 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -08001760 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +01001761 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Oscar Mateo47122742014-07-24 17:04:28 +01001762 }
1763
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001764 if (mode & EMIT_INVALIDATE) {
Oscar Mateo47122742014-07-24 17:04:28 +01001765 flags |= PIPE_CONTROL_TLB_INVALIDATE;
1766 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
1767 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
1768 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
1769 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
1770 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
1771 flags |= PIPE_CONTROL_QW_WRITE;
1772 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Oscar Mateo47122742014-07-24 17:04:28 +01001773
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001774 /*
1775 * On GEN9: before VF_CACHE_INVALIDATE we need to emit a NULL
1776 * pipe control.
1777 */
Chris Wilsonc0336662016-05-06 15:40:21 +01001778 if (IS_GEN9(request->i915))
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001779 vf_flush_wa = true;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001780
1781 /* WaForGAMHang:kbl */
1782 if (IS_KBL_REVID(request->i915, 0, KBL_REVID_B0))
1783 dc_flush_wa = true;
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001784 }
Imre Deak9647ff32015-01-25 13:27:11 -08001785
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001786 len = 6;
1787
1788 if (vf_flush_wa)
1789 len += 6;
1790
1791 if (dc_flush_wa)
1792 len += 12;
1793
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001794 cs = intel_ring_begin(request, len);
1795 if (IS_ERR(cs))
1796 return PTR_ERR(cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001797
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001798 if (vf_flush_wa)
1799 cs = gen8_emit_pipe_control(cs, 0, 0);
Imre Deak9647ff32015-01-25 13:27:11 -08001800
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001801 if (dc_flush_wa)
1802 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_DC_FLUSH_ENABLE,
1803 0);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001804
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001805 cs = gen8_emit_pipe_control(cs, flags, scratch_addr);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001806
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001807 if (dc_flush_wa)
1808 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_CS_STALL, 0);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001809
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001810 intel_ring_advance(request, cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001811
1812 return 0;
1813}
1814
Chris Wilson7c17d372016-01-20 15:43:35 +02001815/*
1816 * Reserve space for 2 NOOPs at the end of each request to be
1817 * used as a workaround for not being allowed to do lite
1818 * restore with HEAD==TAIL (WaIdleLiteRestore).
1819 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001820static void gen8_emit_wa_tail(struct drm_i915_gem_request *request, u32 *cs)
Oscar Mateo4da46e12014-07-24 17:04:27 +01001821{
Chris Wilsonbeecec92017-10-03 21:34:52 +01001822 /* Ensure there's always at least one preemption point per-request. */
1823 *cs++ = MI_ARB_CHECK;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001824 *cs++ = MI_NOOP;
1825 request->wa_tail = intel_ring_offset(request, cs);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001826}
Oscar Mateo4da46e12014-07-24 17:04:27 +01001827
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001828static void gen8_emit_breadcrumb(struct drm_i915_gem_request *request, u32 *cs)
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001829{
Chris Wilson7c17d372016-01-20 15:43:35 +02001830 /* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */
1831 BUILD_BUG_ON(I915_GEM_HWS_INDEX_ADDR & (1 << 5));
Oscar Mateo4da46e12014-07-24 17:04:27 +01001832
Michał Winiarskidf77cd82017-10-25 22:00:15 +02001833 cs = gen8_emit_ggtt_write(cs, request->global_seqno,
1834 intel_hws_seqno_address(request->engine));
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001835 *cs++ = MI_USER_INTERRUPT;
1836 *cs++ = MI_NOOP;
1837 request->tail = intel_ring_offset(request, cs);
Chris Wilsoned1501d2017-03-27 14:14:12 +01001838 assert_ring_tail_valid(request->ring, request->tail);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001839
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001840 gen8_emit_wa_tail(request, cs);
Chris Wilson7c17d372016-01-20 15:43:35 +02001841}
Chris Wilson98f29e82016-10-28 13:58:51 +01001842static const int gen8_emit_breadcrumb_sz = 6 + WA_TAIL_DWORDS;
1843
Michał Winiarskidf77cd82017-10-25 22:00:15 +02001844static void gen8_emit_breadcrumb_rcs(struct drm_i915_gem_request *request,
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001845 u32 *cs)
Chris Wilson7c17d372016-01-20 15:43:35 +02001846{
Michał Winiarskice81a652016-04-12 15:51:55 +02001847 /* We're using qword write, seqno should be aligned to 8 bytes. */
1848 BUILD_BUG_ON(I915_GEM_HWS_INDEX & 1);
1849
Michał Winiarskidf77cd82017-10-25 22:00:15 +02001850 cs = gen8_emit_ggtt_write_rcs(cs, request->global_seqno,
1851 intel_hws_seqno_address(request->engine));
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001852 *cs++ = MI_USER_INTERRUPT;
1853 *cs++ = MI_NOOP;
1854 request->tail = intel_ring_offset(request, cs);
Chris Wilsoned1501d2017-03-27 14:14:12 +01001855 assert_ring_tail_valid(request->ring, request->tail);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001856
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001857 gen8_emit_wa_tail(request, cs);
Oscar Mateo4da46e12014-07-24 17:04:27 +01001858}
Michał Winiarskidf77cd82017-10-25 22:00:15 +02001859static const int gen8_emit_breadcrumb_rcs_sz = 8 + WA_TAIL_DWORDS;
Chris Wilson98f29e82016-10-28 13:58:51 +01001860
John Harrison87531812015-05-29 17:43:44 +01001861static int gen8_init_rcs_context(struct drm_i915_gem_request *req)
Thomas Daniele7778be2014-12-02 12:50:48 +00001862{
1863 int ret;
1864
Tvrtko Ursulin4ac96592017-02-14 15:00:17 +00001865 ret = intel_ring_workarounds_emit(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001866 if (ret)
1867 return ret;
1868
Peter Antoine3bbaba02015-07-10 20:13:11 +03001869 ret = intel_rcs_context_init_mocs(req);
1870 /*
1871 * Failing to program the MOCS is non-fatal.The system will not
1872 * run at peak performance. So generate an error and carry on.
1873 */
1874 if (ret)
1875 DRM_ERROR("MOCS failed to program: expect performance issues.\n");
1876
Chris Wilson4e50f082016-10-28 13:58:31 +01001877 return i915_gem_render_state_emit(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001878}
1879
Oscar Mateo73e4d072014-07-24 17:04:48 +01001880/**
1881 * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001882 * @engine: Engine Command Streamer.
Oscar Mateo73e4d072014-07-24 17:04:48 +01001883 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001884void intel_logical_ring_cleanup(struct intel_engine_cs *engine)
Oscar Mateo454afeb2014-07-24 17:04:22 +01001885{
John Harrison6402c332014-10-31 12:00:26 +00001886 struct drm_i915_private *dev_priv;
Oscar Mateo9832b9d2014-07-24 17:04:30 +01001887
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01001888 /*
1889 * Tasklet cannot be active at this point due intel_mark_active/idle
1890 * so this is just for documentation.
1891 */
Mika Kuoppalab620e872017-09-22 15:43:03 +03001892 if (WARN_ON(test_bit(TASKLET_STATE_SCHED, &engine->execlists.irq_tasklet.state)))
1893 tasklet_kill(&engine->execlists.irq_tasklet);
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01001894
Chris Wilsonc0336662016-05-06 15:40:21 +01001895 dev_priv = engine->i915;
John Harrison6402c332014-10-31 12:00:26 +00001896
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001897 if (engine->buffer) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001898 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
Dave Gordonb0366a52015-12-08 15:02:36 +00001899 }
Oscar Mateo48d82382014-07-24 17:04:23 +01001900
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001901 if (engine->cleanup)
1902 engine->cleanup(engine);
Oscar Mateo48d82382014-07-24 17:04:23 +01001903
Chris Wilsone8a9c582016-12-18 15:37:20 +00001904 intel_engine_cleanup_common(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001905
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001906 lrc_destroy_wa_ctx(engine);
Chris Wilsonc0336662016-05-06 15:40:21 +01001907 engine->i915 = NULL;
Akash Goel3b3f1652016-10-13 22:44:48 +05301908 dev_priv->engine[engine->id] = NULL;
1909 kfree(engine);
Oscar Mateo454afeb2014-07-24 17:04:22 +01001910}
1911
Chris Wilsonff44ad52017-03-16 17:13:03 +00001912static void execlists_set_default_submission(struct intel_engine_cs *engine)
Chris Wilsonddd66c52016-08-02 22:50:31 +01001913{
Chris Wilsonff44ad52017-03-16 17:13:03 +00001914 engine->submit_request = execlists_submit_request;
Chris Wilson27a5f612017-09-15 18:31:00 +01001915 engine->cancel_requests = execlists_cancel_requests;
Chris Wilsonff44ad52017-03-16 17:13:03 +00001916 engine->schedule = execlists_schedule;
Mika Kuoppalab620e872017-09-22 15:43:03 +03001917 engine->execlists.irq_tasklet.func = intel_lrc_irq_handler;
Chris Wilsonaba5e272017-10-25 15:39:41 +01001918
1919 engine->park = NULL;
1920 engine->unpark = NULL;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001921}
1922
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001923static void
Chris Wilsone1382ef2016-05-06 15:40:20 +01001924logical_ring_default_vfuncs(struct intel_engine_cs *engine)
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001925{
1926 /* Default vfuncs which can be overriden by each engine. */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001927 engine->init_hw = gen8_init_common_ring;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001928 engine->reset_hw = reset_common_ring;
Chris Wilsone8a9c582016-12-18 15:37:20 +00001929
1930 engine->context_pin = execlists_context_pin;
1931 engine->context_unpin = execlists_context_unpin;
1932
Chris Wilsonf73e7392016-12-18 15:37:24 +00001933 engine->request_alloc = execlists_request_alloc;
1934
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001935 engine->emit_flush = gen8_emit_flush;
Chris Wilson9b81d552016-10-28 13:58:50 +01001936 engine->emit_breadcrumb = gen8_emit_breadcrumb;
Chris Wilson98f29e82016-10-28 13:58:51 +01001937 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_sz;
Chris Wilsonff44ad52017-03-16 17:13:03 +00001938
1939 engine->set_default_submission = execlists_set_default_submission;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001940
Chris Wilson31bb59c2016-07-01 17:23:27 +01001941 engine->irq_enable = gen8_logical_ring_enable_irq;
1942 engine->irq_disable = gen8_logical_ring_disable_irq;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001943 engine->emit_bb_start = gen8_emit_bb_start;
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001944}
1945
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001946static inline void
Dave Gordonc2c7f242016-07-13 16:03:35 +01001947logical_ring_default_irqs(struct intel_engine_cs *engine)
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001948{
Dave Gordonc2c7f242016-07-13 16:03:35 +01001949 unsigned shift = engine->irq_shift;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001950 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << shift;
1951 engine->irq_keep_mask = GT_CONTEXT_SWITCH_INTERRUPT << shift;
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001952}
1953
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001954static void
1955logical_ring_setup(struct intel_engine_cs *engine)
1956{
1957 struct drm_i915_private *dev_priv = engine->i915;
1958 enum forcewake_domains fw_domains;
1959
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01001960 intel_engine_setup_common(engine);
1961
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001962 /* Intentionally left blank. */
1963 engine->buffer = NULL;
1964
1965 fw_domains = intel_uncore_forcewake_for_reg(dev_priv,
1966 RING_ELSP(engine),
1967 FW_REG_WRITE);
1968
1969 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
1970 RING_CONTEXT_STATUS_PTR(engine),
1971 FW_REG_READ | FW_REG_WRITE);
1972
1973 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
1974 RING_CONTEXT_STATUS_BUF_BASE(engine),
1975 FW_REG_READ);
1976
Mika Kuoppalab620e872017-09-22 15:43:03 +03001977 engine->execlists.fw_domains = fw_domains;
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001978
Mika Kuoppalab620e872017-09-22 15:43:03 +03001979 tasklet_init(&engine->execlists.irq_tasklet,
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001980 intel_lrc_irq_handler, (unsigned long)engine);
1981
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001982 logical_ring_default_vfuncs(engine);
1983 logical_ring_default_irqs(engine);
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001984}
1985
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +01001986static int logical_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001987{
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001988 int ret;
1989
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01001990 ret = intel_engine_init_common(engine);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001991 if (ret)
1992 goto error;
1993
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001994 return 0;
1995
1996error:
1997 intel_logical_ring_cleanup(engine);
1998 return ret;
1999}
2000
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +01002001int logical_render_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002002{
2003 struct drm_i915_private *dev_priv = engine->i915;
2004 int ret;
2005
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002006 logical_ring_setup(engine);
2007
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002008 if (HAS_L3_DPF(dev_priv))
2009 engine->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
2010
2011 /* Override some for render ring. */
2012 if (INTEL_GEN(dev_priv) >= 9)
2013 engine->init_hw = gen9_init_render_ring;
2014 else
2015 engine->init_hw = gen8_init_render_ring;
2016 engine->init_context = gen8_init_rcs_context;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002017 engine->emit_flush = gen8_emit_flush_render;
Michał Winiarskidf77cd82017-10-25 22:00:15 +02002018 engine->emit_breadcrumb = gen8_emit_breadcrumb_rcs;
2019 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_rcs_sz;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002020
Chris Wilsonf51455d2017-01-10 14:47:34 +00002021 ret = intel_engine_create_scratch(engine, PAGE_SIZE);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002022 if (ret)
2023 return ret;
2024
2025 ret = intel_init_workaround_bb(engine);
2026 if (ret) {
2027 /*
2028 * We continue even if we fail to initialize WA batch
2029 * because we only expect rare glitches but nothing
2030 * critical to prevent us from using GPU
2031 */
2032 DRM_ERROR("WA batch buffer initialization failed: %d\n",
2033 ret);
2034 }
2035
Tvrtko Ursulind038fc72016-12-16 13:18:42 +00002036 return logical_ring_init(engine);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002037}
2038
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +01002039int logical_xcs_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002040{
2041 logical_ring_setup(engine);
2042
2043 return logical_ring_init(engine);
2044}
2045
Jeff McGee0cea6502015-02-13 10:27:56 -06002046static u32
Chris Wilsonc0336662016-05-06 15:40:21 +01002047make_rpcs(struct drm_i915_private *dev_priv)
Jeff McGee0cea6502015-02-13 10:27:56 -06002048{
2049 u32 rpcs = 0;
2050
2051 /*
2052 * No explicit RPCS request is needed to ensure full
2053 * slice/subslice/EU enablement prior to Gen9.
2054 */
Chris Wilsonc0336662016-05-06 15:40:21 +01002055 if (INTEL_GEN(dev_priv) < 9)
Jeff McGee0cea6502015-02-13 10:27:56 -06002056 return 0;
2057
2058 /*
2059 * Starting in Gen9, render power gating can leave
2060 * slice/subslice/EU in a partially enabled state. We
2061 * must make an explicit request through RPCS for full
2062 * enablement.
2063 */
Imre Deak43b67992016-08-31 19:13:02 +03002064 if (INTEL_INFO(dev_priv)->sseu.has_slice_pg) {
Jeff McGee0cea6502015-02-13 10:27:56 -06002065 rpcs |= GEN8_RPCS_S_CNT_ENABLE;
Imre Deakf08a0c92016-08-31 19:13:04 +03002066 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.slice_mask) <<
Jeff McGee0cea6502015-02-13 10:27:56 -06002067 GEN8_RPCS_S_CNT_SHIFT;
2068 rpcs |= GEN8_RPCS_ENABLE;
2069 }
2070
Imre Deak43b67992016-08-31 19:13:02 +03002071 if (INTEL_INFO(dev_priv)->sseu.has_subslice_pg) {
Jeff McGee0cea6502015-02-13 10:27:56 -06002072 rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
Imre Deak57ec1712016-08-31 19:13:05 +03002073 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.subslice_mask) <<
Jeff McGee0cea6502015-02-13 10:27:56 -06002074 GEN8_RPCS_SS_CNT_SHIFT;
2075 rpcs |= GEN8_RPCS_ENABLE;
2076 }
2077
Imre Deak43b67992016-08-31 19:13:02 +03002078 if (INTEL_INFO(dev_priv)->sseu.has_eu_pg) {
2079 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
Jeff McGee0cea6502015-02-13 10:27:56 -06002080 GEN8_RPCS_EU_MIN_SHIFT;
Imre Deak43b67992016-08-31 19:13:02 +03002081 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
Jeff McGee0cea6502015-02-13 10:27:56 -06002082 GEN8_RPCS_EU_MAX_SHIFT;
2083 rpcs |= GEN8_RPCS_ENABLE;
2084 }
2085
2086 return rpcs;
2087}
2088
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002089static u32 intel_lr_indirect_ctx_offset(struct intel_engine_cs *engine)
Michel Thierry71562912016-02-23 10:31:49 +00002090{
2091 u32 indirect_ctx_offset;
2092
Chris Wilsonc0336662016-05-06 15:40:21 +01002093 switch (INTEL_GEN(engine->i915)) {
Michel Thierry71562912016-02-23 10:31:49 +00002094 default:
Chris Wilsonc0336662016-05-06 15:40:21 +01002095 MISSING_CASE(INTEL_GEN(engine->i915));
Michel Thierry71562912016-02-23 10:31:49 +00002096 /* fall through */
Michel Thierry7bd0a2c2017-06-06 13:30:38 -07002097 case 10:
2098 indirect_ctx_offset =
2099 GEN10_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2100 break;
Michel Thierry71562912016-02-23 10:31:49 +00002101 case 9:
2102 indirect_ctx_offset =
2103 GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2104 break;
2105 case 8:
2106 indirect_ctx_offset =
2107 GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2108 break;
2109 }
2110
2111 return indirect_ctx_offset;
2112}
2113
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002114static void execlists_init_reg_state(u32 *regs,
Chris Wilsona3aabe82016-10-04 21:11:26 +01002115 struct i915_gem_context *ctx,
2116 struct intel_engine_cs *engine,
2117 struct intel_ring *ring)
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002118{
Chris Wilsona3aabe82016-10-04 21:11:26 +01002119 struct drm_i915_private *dev_priv = engine->i915;
2120 struct i915_hw_ppgtt *ppgtt = ctx->ppgtt ?: dev_priv->mm.aliasing_ppgtt;
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002121 u32 base = engine->mmio_base;
2122 bool rcs = engine->id == RCS;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002123
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002124 /* A context is actually a big batch buffer with several
2125 * MI_LOAD_REGISTER_IMM commands followed by (reg, value) pairs. The
2126 * values we are setting here are only for the first context restore:
2127 * on a subsequent save, the GPU will recreate this batchbuffer with new
2128 * values (including all the missing MI_LOAD_REGISTER_IMM commands that
2129 * we are not initializing here).
2130 */
2131 regs[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(rcs ? 14 : 11) |
2132 MI_LRI_FORCE_POSTED;
2133
2134 CTX_REG(regs, CTX_CONTEXT_CONTROL, RING_CONTEXT_CONTROL(engine),
2135 _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
2136 CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
2137 (HAS_RESOURCE_STREAMER(dev_priv) ?
2138 CTX_CTRL_RS_CTX_ENABLE : 0)));
2139 CTX_REG(regs, CTX_RING_HEAD, RING_HEAD(base), 0);
2140 CTX_REG(regs, CTX_RING_TAIL, RING_TAIL(base), 0);
2141 CTX_REG(regs, CTX_RING_BUFFER_START, RING_START(base), 0);
2142 CTX_REG(regs, CTX_RING_BUFFER_CONTROL, RING_CTL(base),
2143 RING_CTL_SIZE(ring->size) | RING_VALID);
2144 CTX_REG(regs, CTX_BB_HEAD_U, RING_BBADDR_UDW(base), 0);
2145 CTX_REG(regs, CTX_BB_HEAD_L, RING_BBADDR(base), 0);
2146 CTX_REG(regs, CTX_BB_STATE, RING_BBSTATE(base), RING_BB_PPGTT);
2147 CTX_REG(regs, CTX_SECOND_BB_HEAD_U, RING_SBBADDR_UDW(base), 0);
2148 CTX_REG(regs, CTX_SECOND_BB_HEAD_L, RING_SBBADDR(base), 0);
2149 CTX_REG(regs, CTX_SECOND_BB_STATE, RING_SBBSTATE(base), 0);
2150 if (rcs) {
Chris Wilson604a8f62017-09-21 14:54:43 +01002151 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
2152
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002153 CTX_REG(regs, CTX_RCS_INDIRECT_CTX, RING_INDIRECT_CTX(base), 0);
2154 CTX_REG(regs, CTX_RCS_INDIRECT_CTX_OFFSET,
2155 RING_INDIRECT_CTX_OFFSET(base), 0);
Chris Wilson604a8f62017-09-21 14:54:43 +01002156 if (wa_ctx->indirect_ctx.size) {
Chris Wilsonbde13eb2016-08-15 10:49:07 +01002157 u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01002158
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002159 regs[CTX_RCS_INDIRECT_CTX + 1] =
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00002160 (ggtt_offset + wa_ctx->indirect_ctx.offset) |
2161 (wa_ctx->indirect_ctx.size / CACHELINE_BYTES);
Arun Siluvery17ee9502015-06-19 19:07:01 +01002162
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002163 regs[CTX_RCS_INDIRECT_CTX_OFFSET + 1] =
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002164 intel_lr_indirect_ctx_offset(engine) << 6;
Chris Wilson604a8f62017-09-21 14:54:43 +01002165 }
2166
2167 CTX_REG(regs, CTX_BB_PER_CTX_PTR, RING_BB_PER_CTX_PTR(base), 0);
2168 if (wa_ctx->per_ctx.size) {
2169 u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01002170
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002171 regs[CTX_BB_PER_CTX_PTR + 1] =
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00002172 (ggtt_offset + wa_ctx->per_ctx.offset) | 0x01;
Arun Siluvery17ee9502015-06-19 19:07:01 +01002173 }
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002174 }
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002175
2176 regs[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9) | MI_LRI_FORCE_POSTED;
2177
2178 CTX_REG(regs, CTX_CTX_TIMESTAMP, RING_CTX_TIMESTAMP(base), 0);
Ville Syrjälä0d925ea2015-11-04 23:20:11 +02002179 /* PDP values well be assigned later if needed */
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002180 CTX_REG(regs, CTX_PDP3_UDW, GEN8_RING_PDP_UDW(engine, 3), 0);
2181 CTX_REG(regs, CTX_PDP3_LDW, GEN8_RING_PDP_LDW(engine, 3), 0);
2182 CTX_REG(regs, CTX_PDP2_UDW, GEN8_RING_PDP_UDW(engine, 2), 0);
2183 CTX_REG(regs, CTX_PDP2_LDW, GEN8_RING_PDP_LDW(engine, 2), 0);
2184 CTX_REG(regs, CTX_PDP1_UDW, GEN8_RING_PDP_UDW(engine, 1), 0);
2185 CTX_REG(regs, CTX_PDP1_LDW, GEN8_RING_PDP_LDW(engine, 1), 0);
2186 CTX_REG(regs, CTX_PDP0_UDW, GEN8_RING_PDP_UDW(engine, 0), 0);
2187 CTX_REG(regs, CTX_PDP0_LDW, GEN8_RING_PDP_LDW(engine, 0), 0);
Michel Thierryd7b26332015-04-08 12:13:34 +01002188
Chris Wilson949e8ab2017-02-09 14:40:36 +00002189 if (ppgtt && i915_vm_is_48bit(&ppgtt->base)) {
Michel Thierry2dba3232015-07-30 11:06:23 +01002190 /* 64b PPGTT (48bit canonical)
2191 * PDP0_DESCRIPTOR contains the base address to PML4 and
2192 * other PDP Descriptors are ignored.
2193 */
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002194 ASSIGN_CTX_PML4(ppgtt, regs);
Michel Thierry2dba3232015-07-30 11:06:23 +01002195 }
2196
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002197 if (rcs) {
2198 regs[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
2199 CTX_REG(regs, CTX_R_PWR_CLK_STATE, GEN8_R_PWR_CLK_STATE,
2200 make_rpcs(dev_priv));
Robert Bragg19f81df2017-06-13 12:23:03 +01002201
2202 i915_oa_init_reg_state(engine, ctx, regs);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002203 }
Chris Wilsona3aabe82016-10-04 21:11:26 +01002204}
2205
2206static int
2207populate_lr_context(struct i915_gem_context *ctx,
2208 struct drm_i915_gem_object *ctx_obj,
2209 struct intel_engine_cs *engine,
2210 struct intel_ring *ring)
2211{
2212 void *vaddr;
2213 int ret;
2214
2215 ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
2216 if (ret) {
2217 DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
2218 return ret;
2219 }
2220
2221 vaddr = i915_gem_object_pin_map(ctx_obj, I915_MAP_WB);
2222 if (IS_ERR(vaddr)) {
2223 ret = PTR_ERR(vaddr);
2224 DRM_DEBUG_DRIVER("Could not map object pages! (%d)\n", ret);
2225 return ret;
2226 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002227 ctx_obj->mm.dirty = true;
Chris Wilsona3aabe82016-10-04 21:11:26 +01002228
2229 /* The second page of the context object contains some fields which must
2230 * be set up prior to the first execution. */
2231
2232 execlists_init_reg_state(vaddr + LRC_STATE_PN * PAGE_SIZE,
2233 ctx, engine, ring);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002234
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01002235 i915_gem_object_unpin_map(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002236
2237 return 0;
2238}
2239
Chris Wilsone2efd132016-05-24 14:53:34 +01002240static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
Chris Wilson978f1e02016-04-28 09:56:54 +01002241 struct intel_engine_cs *engine)
Oscar Mateoede7d422014-07-24 17:04:12 +01002242{
Oscar Mateo8c8579172014-07-24 17:04:14 +01002243 struct drm_i915_gem_object *ctx_obj;
Chris Wilson9021ad02016-05-24 14:53:37 +01002244 struct intel_context *ce = &ctx->engine[engine->id];
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002245 struct i915_vma *vma;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002246 uint32_t context_size;
Chris Wilson7e37f882016-08-02 22:50:21 +01002247 struct intel_ring *ring;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002248 int ret;
2249
Chris Wilson9021ad02016-05-24 14:53:37 +01002250 WARN_ON(ce->state);
Oscar Mateoede7d422014-07-24 17:04:12 +01002251
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +03002252 context_size = round_up(engine->context_size, I915_GTT_PAGE_SIZE);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002253
Michel Thierry0b29c752017-09-13 09:56:00 +01002254 /*
2255 * Before the actual start of the context image, we insert a few pages
2256 * for our own use and for sharing with the GuC.
2257 */
2258 context_size += LRC_HEADER_PAGES * PAGE_SIZE;
Alex Daid1675192015-08-12 15:43:43 +01002259
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00002260 ctx_obj = i915_gem_object_create(ctx->i915, context_size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01002261 if (IS_ERR(ctx_obj)) {
Dan Carpenter3126a662015-04-30 17:30:50 +03002262 DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
Chris Wilsonfe3db792016-04-25 13:32:13 +01002263 return PTR_ERR(ctx_obj);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002264 }
2265
Chris Wilsona01cb372017-01-16 15:21:30 +00002266 vma = i915_vma_instance(ctx_obj, &ctx->i915->ggtt.base, NULL);
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002267 if (IS_ERR(vma)) {
2268 ret = PTR_ERR(vma);
2269 goto error_deref_obj;
2270 }
2271
Chris Wilson7e37f882016-08-02 22:50:21 +01002272 ring = intel_engine_create_ring(engine, ctx->ring_size);
Chris Wilsondca33ec2016-08-02 22:50:20 +01002273 if (IS_ERR(ring)) {
2274 ret = PTR_ERR(ring);
Nick Hoathe84fe802015-09-11 12:53:46 +01002275 goto error_deref_obj;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002276 }
2277
Chris Wilsondca33ec2016-08-02 22:50:20 +01002278 ret = populate_lr_context(ctx, ctx_obj, engine, ring);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002279 if (ret) {
2280 DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
Chris Wilsondca33ec2016-08-02 22:50:20 +01002281 goto error_ring_free;
Oscar Mateo84c23772014-07-24 17:04:15 +01002282 }
2283
Chris Wilsondca33ec2016-08-02 22:50:20 +01002284 ce->ring = ring;
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002285 ce->state = vma;
Chuanxiao Dong0d402a22017-05-11 18:07:42 +08002286 ce->initialised |= engine->init_context == NULL;
Oscar Mateoede7d422014-07-24 17:04:12 +01002287
2288 return 0;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002289
Chris Wilsondca33ec2016-08-02 22:50:20 +01002290error_ring_free:
Chris Wilson7e37f882016-08-02 22:50:21 +01002291 intel_ring_free(ring);
Nick Hoathe84fe802015-09-11 12:53:46 +01002292error_deref_obj:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002293 i915_gem_object_put(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002294 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01002295}
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002296
Chris Wilson821ed7d2016-09-09 14:11:53 +01002297void intel_lr_context_resume(struct drm_i915_private *dev_priv)
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002298{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002299 struct intel_engine_cs *engine;
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002300 struct i915_gem_context *ctx;
Akash Goel3b3f1652016-10-13 22:44:48 +05302301 enum intel_engine_id id;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002302
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002303 /* Because we emit WA_TAIL_DWORDS there may be a disparity
2304 * between our bookkeeping in ce->ring->head and ce->ring->tail and
2305 * that stored in context. As we only write new commands from
2306 * ce->ring->tail onwards, everything before that is junk. If the GPU
2307 * starts reading from its RING_HEAD from the context, it may try to
2308 * execute that junk and die.
2309 *
2310 * So to avoid that we reset the context images upon resume. For
2311 * simplicity, we just zero everything out.
2312 */
Chris Wilson829a0af2017-06-20 12:05:45 +01002313 list_for_each_entry(ctx, &dev_priv->contexts.list, link) {
Akash Goel3b3f1652016-10-13 22:44:48 +05302314 for_each_engine(engine, dev_priv, id) {
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002315 struct intel_context *ce = &ctx->engine[engine->id];
2316 u32 *reg;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002317
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002318 if (!ce->state)
2319 continue;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002320
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002321 reg = i915_gem_object_pin_map(ce->state->obj,
2322 I915_MAP_WB);
2323 if (WARN_ON(IS_ERR(reg)))
2324 continue;
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01002325
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002326 reg += LRC_STATE_PN * PAGE_SIZE / sizeof(*reg);
2327 reg[CTX_RING_HEAD+1] = 0;
2328 reg[CTX_RING_TAIL+1] = 0;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002329
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002330 ce->state->obj->mm.dirty = true;
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002331 i915_gem_object_unpin_map(ce->state->obj);
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002332
Chris Wilsone6ba9992017-04-25 14:00:49 +01002333 intel_ring_reset(ce->ring, 0);
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002334 }
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002335 }
2336}