blob: 58d050a9a8665cc06d803f413e2794d63b09d731 [file] [log] [blame]
Oscar Mateob20385f2014-07-24 17:04:10 +01001/*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Ben Widawsky <ben@bwidawsk.net>
25 * Michel Thierry <michel.thierry@intel.com>
26 * Thomas Daniel <thomas.daniel@intel.com>
27 * Oscar Mateo <oscar.mateo@intel.com>
28 *
29 */
30
Oscar Mateo73e4d072014-07-24 17:04:48 +010031/**
32 * DOC: Logical Rings, Logical Ring Contexts and Execlists
33 *
34 * Motivation:
Oscar Mateob20385f2014-07-24 17:04:10 +010035 * GEN8 brings an expansion of the HW contexts: "Logical Ring Contexts".
36 * These expanded contexts enable a number of new abilities, especially
37 * "Execlists" (also implemented in this file).
38 *
Oscar Mateo73e4d072014-07-24 17:04:48 +010039 * One of the main differences with the legacy HW contexts is that logical
40 * ring contexts incorporate many more things to the context's state, like
41 * PDPs or ringbuffer control registers:
42 *
43 * The reason why PDPs are included in the context is straightforward: as
44 * PPGTTs (per-process GTTs) are actually per-context, having the PDPs
45 * contained there mean you don't need to do a ppgtt->switch_mm yourself,
46 * instead, the GPU will do it for you on the context switch.
47 *
48 * But, what about the ringbuffer control registers (head, tail, etc..)?
49 * shouldn't we just need a set of those per engine command streamer? This is
50 * where the name "Logical Rings" starts to make sense: by virtualizing the
51 * rings, the engine cs shifts to a new "ring buffer" with every context
52 * switch. When you want to submit a workload to the GPU you: A) choose your
53 * context, B) find its appropriate virtualized ring, C) write commands to it
54 * and then, finally, D) tell the GPU to switch to that context.
55 *
56 * Instead of the legacy MI_SET_CONTEXT, the way you tell the GPU to switch
57 * to a contexts is via a context execution list, ergo "Execlists".
58 *
59 * LRC implementation:
60 * Regarding the creation of contexts, we have:
61 *
62 * - One global default context.
63 * - One local default context for each opened fd.
64 * - One local extra context for each context create ioctl call.
65 *
66 * Now that ringbuffers belong per-context (and not per-engine, like before)
67 * and that contexts are uniquely tied to a given engine (and not reusable,
68 * like before) we need:
69 *
70 * - One ringbuffer per-engine inside each context.
71 * - One backing object per-engine inside each context.
72 *
73 * The global default context starts its life with these new objects fully
74 * allocated and populated. The local default context for each opened fd is
75 * more complex, because we don't know at creation time which engine is going
76 * to use them. To handle this, we have implemented a deferred creation of LR
77 * contexts:
78 *
79 * The local context starts its life as a hollow or blank holder, that only
80 * gets populated for a given engine once we receive an execbuffer. If later
81 * on we receive another execbuffer ioctl for the same context but a different
82 * engine, we allocate/populate a new ringbuffer and context backing object and
83 * so on.
84 *
85 * Finally, regarding local contexts created using the ioctl call: as they are
86 * only allowed with the render ring, we can allocate & populate them right
87 * away (no need to defer anything, at least for now).
88 *
89 * Execlists implementation:
Oscar Mateob20385f2014-07-24 17:04:10 +010090 * Execlists are the new method by which, on gen8+ hardware, workloads are
91 * submitted for execution (as opposed to the legacy, ringbuffer-based, method).
Oscar Mateo73e4d072014-07-24 17:04:48 +010092 * This method works as follows:
93 *
94 * When a request is committed, its commands (the BB start and any leading or
95 * trailing commands, like the seqno breadcrumbs) are placed in the ringbuffer
96 * for the appropriate context. The tail pointer in the hardware context is not
97 * updated at this time, but instead, kept by the driver in the ringbuffer
98 * structure. A structure representing this request is added to a request queue
99 * for the appropriate engine: this structure contains a copy of the context's
100 * tail after the request was written to the ring buffer and a pointer to the
101 * context itself.
102 *
103 * If the engine's request queue was empty before the request was added, the
104 * queue is processed immediately. Otherwise the queue will be processed during
105 * a context switch interrupt. In any case, elements on the queue will get sent
106 * (in pairs) to the GPU's ExecLists Submit Port (ELSP, for short) with a
107 * globally unique 20-bits submission ID.
108 *
109 * When execution of a request completes, the GPU updates the context status
110 * buffer with a context complete event and generates a context switch interrupt.
111 * During the interrupt handling, the driver examines the events in the buffer:
112 * for each context complete event, if the announced ID matches that on the head
113 * of the request queue, then that request is retired and removed from the queue.
114 *
115 * After processing, if any requests were retired and the queue is not empty
116 * then a new execution list can be submitted. The two requests at the front of
117 * the queue are next to be submitted but since a context may not occur twice in
118 * an execution list, if subsequent requests have the same ID as the first then
119 * the two requests must be combined. This is done simply by discarding requests
120 * at the head of the queue until either only one requests is left (in which case
121 * we use a NULL second context) or the first two requests have unique IDs.
122 *
123 * By always executing the first two requests in the queue the driver ensures
124 * that the GPU is kept as busy as possible. In the case where a single context
125 * completes but a second context is still executing, the request for this second
126 * context will be at the head of the queue when we remove the first one. This
127 * request will then be resubmitted along with a new request for a different context,
128 * which will cause the hardware to continue executing the second request and queue
129 * the new request (the GPU detects the condition of a context getting preempted
130 * with the same context and optimizes the context switch flow by not doing
131 * preemption, but just sampling the new tail pointer).
132 *
Oscar Mateob20385f2014-07-24 17:04:10 +0100133 */
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +0100134#include <linux/interrupt.h>
Oscar Mateob20385f2014-07-24 17:04:10 +0100135
136#include <drm/drmP.h>
137#include <drm/i915_drm.h>
138#include "i915_drv.h"
Chris Wilson7c2fa7f2017-11-10 14:26:34 +0000139#include "i915_gem_render_state.h"
Peter Antoine3bbaba02015-07-10 20:13:11 +0300140#include "intel_mocs.h"
Oscar Mateo127f1002014-07-24 17:04:11 +0100141
Thomas Daniele981e7b2014-07-24 17:04:39 +0100142#define RING_EXECLIST_QFULL (1 << 0x2)
143#define RING_EXECLIST1_VALID (1 << 0x3)
144#define RING_EXECLIST0_VALID (1 << 0x4)
145#define RING_EXECLIST_ACTIVE_STATUS (3 << 0xE)
146#define RING_EXECLIST1_ACTIVE (1 << 0x11)
147#define RING_EXECLIST0_ACTIVE (1 << 0x12)
148
149#define GEN8_CTX_STATUS_IDLE_ACTIVE (1 << 0)
150#define GEN8_CTX_STATUS_PREEMPTED (1 << 1)
151#define GEN8_CTX_STATUS_ELEMENT_SWITCH (1 << 2)
152#define GEN8_CTX_STATUS_ACTIVE_IDLE (1 << 3)
153#define GEN8_CTX_STATUS_COMPLETE (1 << 4)
154#define GEN8_CTX_STATUS_LITE_RESTORE (1 << 15)
Oscar Mateo8670d6f2014-07-24 17:04:17 +0100155
Chris Wilson70c2a242016-09-09 14:11:46 +0100156#define GEN8_CTX_STATUS_COMPLETED_MASK \
157 (GEN8_CTX_STATUS_ACTIVE_IDLE | \
158 GEN8_CTX_STATUS_PREEMPTED | \
159 GEN8_CTX_STATUS_ELEMENT_SWITCH)
160
Oscar Mateo8670d6f2014-07-24 17:04:17 +0100161#define CTX_LRI_HEADER_0 0x01
162#define CTX_CONTEXT_CONTROL 0x02
163#define CTX_RING_HEAD 0x04
164#define CTX_RING_TAIL 0x06
165#define CTX_RING_BUFFER_START 0x08
166#define CTX_RING_BUFFER_CONTROL 0x0a
167#define CTX_BB_HEAD_U 0x0c
168#define CTX_BB_HEAD_L 0x0e
169#define CTX_BB_STATE 0x10
170#define CTX_SECOND_BB_HEAD_U 0x12
171#define CTX_SECOND_BB_HEAD_L 0x14
172#define CTX_SECOND_BB_STATE 0x16
173#define CTX_BB_PER_CTX_PTR 0x18
174#define CTX_RCS_INDIRECT_CTX 0x1a
175#define CTX_RCS_INDIRECT_CTX_OFFSET 0x1c
176#define CTX_LRI_HEADER_1 0x21
177#define CTX_CTX_TIMESTAMP 0x22
178#define CTX_PDP3_UDW 0x24
179#define CTX_PDP3_LDW 0x26
180#define CTX_PDP2_UDW 0x28
181#define CTX_PDP2_LDW 0x2a
182#define CTX_PDP1_UDW 0x2c
183#define CTX_PDP1_LDW 0x2e
184#define CTX_PDP0_UDW 0x30
185#define CTX_PDP0_LDW 0x32
186#define CTX_LRI_HEADER_2 0x41
187#define CTX_R_PWR_CLK_STATE 0x42
188#define CTX_GPGPU_CSR_BASE_ADDRESS 0x44
189
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +0000190#define CTX_REG(reg_state, pos, reg, val) do { \
Ville Syrjäläf0f59a02015-11-18 15:33:26 +0200191 (reg_state)[(pos)+0] = i915_mmio_reg_offset(reg); \
Ville Syrjälä0d925ea2015-11-04 23:20:11 +0200192 (reg_state)[(pos)+1] = (val); \
193} while (0)
194
195#define ASSIGN_CTX_PDP(ppgtt, reg_state, n) do { \
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300196 const u64 _addr = i915_page_dir_dma_addr((ppgtt), (n)); \
Michel Thierrye5815a22015-04-08 12:13:32 +0100197 reg_state[CTX_PDP ## n ## _UDW+1] = upper_32_bits(_addr); \
198 reg_state[CTX_PDP ## n ## _LDW+1] = lower_32_bits(_addr); \
Ville Syrjälä9244a812015-11-04 23:20:09 +0200199} while (0)
Michel Thierrye5815a22015-04-08 12:13:32 +0100200
Ville Syrjälä9244a812015-11-04 23:20:09 +0200201#define ASSIGN_CTX_PML4(ppgtt, reg_state) do { \
Michel Thierry2dba3232015-07-30 11:06:23 +0100202 reg_state[CTX_PDP0_UDW + 1] = upper_32_bits(px_dma(&ppgtt->pml4)); \
203 reg_state[CTX_PDP0_LDW + 1] = lower_32_bits(px_dma(&ppgtt->pml4)); \
Ville Syrjälä9244a812015-11-04 23:20:09 +0200204} while (0)
Michel Thierry2dba3232015-07-30 11:06:23 +0100205
Michel Thierry71562912016-02-23 10:31:49 +0000206#define GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x17
207#define GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x26
Michel Thierry7bd0a2c2017-06-06 13:30:38 -0700208#define GEN10_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x19
Ben Widawsky84b790f2014-07-24 17:04:36 +0100209
Chris Wilson0e93cdd2016-04-29 09:07:06 +0100210/* Typical size of the average request (2 pipecontrols and a MI_BB) */
211#define EXECLISTS_REQUEST_SIZE 64 /* bytes */
Chris Wilsona3aabe82016-10-04 21:11:26 +0100212#define WA_TAIL_DWORDS 2
Chris Wilson7e4992a2017-09-28 20:38:59 +0100213#define WA_TAIL_BYTES (sizeof(u32) * WA_TAIL_DWORDS)
Chris Wilsonbeecec92017-10-03 21:34:52 +0100214#define PREEMPT_ID 0x1
Chris Wilsona3aabe82016-10-04 21:11:26 +0100215
Chris Wilsone2efd132016-05-24 14:53:34 +0100216static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
Chris Wilson978f1e02016-04-28 09:56:54 +0100217 struct intel_engine_cs *engine);
Chris Wilsona3aabe82016-10-04 21:11:26 +0100218static void execlists_init_reg_state(u32 *reg_state,
219 struct i915_gem_context *ctx,
220 struct intel_engine_cs *engine,
221 struct intel_ring *ring);
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000222
Oscar Mateo73e4d072014-07-24 17:04:48 +0100223/**
224 * intel_sanitize_enable_execlists() - sanitize i915.enable_execlists
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100225 * @dev_priv: i915 device private
Oscar Mateo73e4d072014-07-24 17:04:48 +0100226 * @enable_execlists: value of i915.enable_execlists module parameter.
227 *
228 * Only certain platforms support Execlists (the prerequisites being
Thomas Daniel27401d12014-12-11 12:48:35 +0000229 * support for Logical Ring Contexts and Aliasing PPGTT or better).
Oscar Mateo73e4d072014-07-24 17:04:48 +0100230 *
231 * Return: 1 if Execlists is supported and has to be enabled.
232 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100233int intel_sanitize_enable_execlists(struct drm_i915_private *dev_priv, int enable_execlists)
Oscar Mateo127f1002014-07-24 17:04:11 +0100234{
Zhiyuan Lva0bd6c32015-08-28 15:41:16 +0800235 /* On platforms with execlist available, vGPU will only
236 * support execlist mode, no ring buffer mode.
237 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100238 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv) && intel_vgpu_active(dev_priv))
Zhiyuan Lva0bd6c32015-08-28 15:41:16 +0800239 return 1;
240
Chris Wilsonc0336662016-05-06 15:40:21 +0100241 if (INTEL_GEN(dev_priv) >= 9)
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000242 return 1;
243
Oscar Mateo127f1002014-07-24 17:04:11 +0100244 if (enable_execlists == 0)
245 return 0;
246
Daniel Vetter5a21b662016-05-24 17:13:53 +0200247 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv) &&
Maarten Lankhorst8279aaf2017-10-04 11:44:16 +0200248 USES_PPGTT(dev_priv))
Oscar Mateo127f1002014-07-24 17:04:11 +0100249 return 1;
250
251 return 0;
252}
Oscar Mateoede7d422014-07-24 17:04:12 +0100253
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000254/**
255 * intel_lr_context_descriptor_update() - calculate & cache the descriptor
256 * descriptor for a pinned context
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000257 * @ctx: Context to work on
Chris Wilson9021ad02016-05-24 14:53:37 +0100258 * @engine: Engine the descriptor will be used with
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000259 *
260 * The context descriptor encodes various attributes of a context,
261 * including its GTT address and some flags. Because it's fairly
262 * expensive to calculate, we'll just do it once and cache the result,
263 * which remains valid until the context is unpinned.
264 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200265 * This is what a descriptor looks like, from LSB to MSB::
266 *
Mika Kuoppala2355cf02017-01-27 15:03:09 +0200267 * bits 0-11: flags, GEN8_CTX_* (cached in ctx->desc_template)
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200268 * bits 12-31: LRCA, GTT address of (the HWSP of) this context
269 * bits 32-52: ctx ID, a globally unique tag
270 * bits 53-54: mbz, reserved for use by hardware
271 * bits 55-63: group ID, currently unused and set to 0
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000272 */
273static void
Chris Wilsone2efd132016-05-24 14:53:34 +0100274intel_lr_context_descriptor_update(struct i915_gem_context *ctx,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +0000275 struct intel_engine_cs *engine)
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000276{
Chris Wilson9021ad02016-05-24 14:53:37 +0100277 struct intel_context *ce = &ctx->engine[engine->id];
Chris Wilson7069b142016-04-28 09:56:52 +0100278 u64 desc;
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000279
Chris Wilson7069b142016-04-28 09:56:52 +0100280 BUILD_BUG_ON(MAX_CONTEXT_HW_ID > (1<<GEN8_CTX_ID_WIDTH));
281
Mika Kuoppala2355cf02017-01-27 15:03:09 +0200282 desc = ctx->desc_template; /* bits 0-11 */
Michel Thierry0b29c752017-09-13 09:56:00 +0100283 desc |= i915_ggtt_offset(ce->state) + LRC_HEADER_PAGES * PAGE_SIZE;
Chris Wilson9021ad02016-05-24 14:53:37 +0100284 /* bits 12-31 */
Chris Wilson7069b142016-04-28 09:56:52 +0100285 desc |= (u64)ctx->hw_id << GEN8_CTX_ID_SHIFT; /* bits 32-52 */
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000286
Chris Wilson9021ad02016-05-24 14:53:37 +0100287 ce->lrc_desc = desc;
Tvrtko Ursulinca825802016-01-15 15:10:27 +0000288}
289
Chris Wilson27606fd2017-09-16 21:44:13 +0100290static struct i915_priolist *
291lookup_priolist(struct intel_engine_cs *engine,
292 struct i915_priotree *pt,
293 int prio)
Chris Wilson08dd3e12017-09-16 21:44:12 +0100294{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300295 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100296 struct i915_priolist *p;
297 struct rb_node **parent, *rb;
298 bool first = true;
299
Mika Kuoppalab620e872017-09-22 15:43:03 +0300300 if (unlikely(execlists->no_priolist))
Chris Wilson08dd3e12017-09-16 21:44:12 +0100301 prio = I915_PRIORITY_NORMAL;
302
303find_priolist:
304 /* most positive priority is scheduled first, equal priorities fifo */
305 rb = NULL;
Mika Kuoppalab620e872017-09-22 15:43:03 +0300306 parent = &execlists->queue.rb_node;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100307 while (*parent) {
308 rb = *parent;
309 p = rb_entry(rb, typeof(*p), node);
310 if (prio > p->priority) {
311 parent = &rb->rb_left;
312 } else if (prio < p->priority) {
313 parent = &rb->rb_right;
314 first = false;
315 } else {
Chris Wilson27606fd2017-09-16 21:44:13 +0100316 return p;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100317 }
318 }
319
320 if (prio == I915_PRIORITY_NORMAL) {
Mika Kuoppalab620e872017-09-22 15:43:03 +0300321 p = &execlists->default_priolist;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100322 } else {
323 p = kmem_cache_alloc(engine->i915->priorities, GFP_ATOMIC);
324 /* Convert an allocation failure to a priority bump */
325 if (unlikely(!p)) {
326 prio = I915_PRIORITY_NORMAL; /* recurses just once */
327
328 /* To maintain ordering with all rendering, after an
329 * allocation failure we have to disable all scheduling.
330 * Requests will then be executed in fifo, and schedule
331 * will ensure that dependencies are emitted in fifo.
332 * There will be still some reordering with existing
333 * requests, so if userspace lied about their
334 * dependencies that reordering may be visible.
335 */
Mika Kuoppalab620e872017-09-22 15:43:03 +0300336 execlists->no_priolist = true;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100337 goto find_priolist;
338 }
339 }
340
341 p->priority = prio;
Chris Wilson27606fd2017-09-16 21:44:13 +0100342 INIT_LIST_HEAD(&p->requests);
Chris Wilson08dd3e12017-09-16 21:44:12 +0100343 rb_link_node(&p->node, rb, parent);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300344 rb_insert_color(&p->node, &execlists->queue);
Chris Wilson08dd3e12017-09-16 21:44:12 +0100345
Chris Wilson08dd3e12017-09-16 21:44:12 +0100346 if (first)
Mika Kuoppalab620e872017-09-22 15:43:03 +0300347 execlists->first = &p->node;
Chris Wilson08dd3e12017-09-16 21:44:12 +0100348
Chris Wilson27606fd2017-09-16 21:44:13 +0100349 return ptr_pack_bits(p, first, 1);
Chris Wilson08dd3e12017-09-16 21:44:12 +0100350}
351
Chris Wilson7e4992a2017-09-28 20:38:59 +0100352static void unwind_wa_tail(struct drm_i915_gem_request *rq)
353{
354 rq->tail = intel_ring_wrap(rq->ring, rq->wa_tail - WA_TAIL_BYTES);
355 assert_ring_tail_valid(rq->ring, rq->tail);
356}
357
Michał Winiarskia4598d12017-10-25 22:00:18 +0200358static void __unwind_incomplete_requests(struct intel_engine_cs *engine)
Chris Wilson7e4992a2017-09-28 20:38:59 +0100359{
360 struct drm_i915_gem_request *rq, *rn;
Michał Winiarski097a9482017-09-28 20:39:01 +0100361 struct i915_priolist *uninitialized_var(p);
362 int last_prio = I915_PRIORITY_INVALID;
Chris Wilson7e4992a2017-09-28 20:38:59 +0100363
364 lockdep_assert_held(&engine->timeline->lock);
365
366 list_for_each_entry_safe_reverse(rq, rn,
367 &engine->timeline->requests,
368 link) {
Chris Wilson7e4992a2017-09-28 20:38:59 +0100369 if (i915_gem_request_completed(rq))
370 return;
371
372 __i915_gem_request_unsubmit(rq);
373 unwind_wa_tail(rq);
374
Michał Winiarski097a9482017-09-28 20:39:01 +0100375 GEM_BUG_ON(rq->priotree.priority == I915_PRIORITY_INVALID);
376 if (rq->priotree.priority != last_prio) {
377 p = lookup_priolist(engine,
378 &rq->priotree,
379 rq->priotree.priority);
380 p = ptr_mask_bits(p, 1);
381
382 last_prio = rq->priotree.priority;
383 }
384
385 list_add(&rq->priotree.link, &p->requests);
Chris Wilson7e4992a2017-09-28 20:38:59 +0100386 }
387}
388
Michał Winiarskic41937f2017-10-26 15:35:58 +0200389void
Michał Winiarskia4598d12017-10-25 22:00:18 +0200390execlists_unwind_incomplete_requests(struct intel_engine_execlists *execlists)
391{
392 struct intel_engine_cs *engine =
393 container_of(execlists, typeof(*engine), execlists);
394
395 spin_lock_irq(&engine->timeline->lock);
396 __unwind_incomplete_requests(engine);
397 spin_unlock_irq(&engine->timeline->lock);
398}
399
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100400static inline void
401execlists_context_status_change(struct drm_i915_gem_request *rq,
402 unsigned long status)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100403{
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100404 /*
405 * Only used when GVT-g is enabled now. When GVT-g is disabled,
406 * The compiler should eliminate this function as dead-code.
407 */
408 if (!IS_ENABLED(CONFIG_DRM_I915_GVT))
409 return;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100410
Changbin Du3fc03062017-03-13 10:47:11 +0800411 atomic_notifier_call_chain(&rq->engine->context_status_notifier,
412 status, rq);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100413}
414
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000415static void
416execlists_update_context_pdps(struct i915_hw_ppgtt *ppgtt, u32 *reg_state)
417{
418 ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
419 ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
420 ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
421 ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
422}
423
Chris Wilson70c2a242016-09-09 14:11:46 +0100424static u64 execlists_update_context(struct drm_i915_gem_request *rq)
Oscar Mateoae1250b2014-07-24 17:04:37 +0100425{
Chris Wilson70c2a242016-09-09 14:11:46 +0100426 struct intel_context *ce = &rq->ctx->engine[rq->engine->id];
Zhi Wang04da8112017-02-06 18:37:16 +0800427 struct i915_hw_ppgtt *ppgtt =
428 rq->ctx->ppgtt ?: rq->i915->mm.aliasing_ppgtt;
Chris Wilson70c2a242016-09-09 14:11:46 +0100429 u32 *reg_state = ce->lrc_reg_state;
Oscar Mateoae1250b2014-07-24 17:04:37 +0100430
Chris Wilsone6ba9992017-04-25 14:00:49 +0100431 reg_state[CTX_RING_TAIL+1] = intel_ring_set_tail(rq->ring, rq->tail);
Oscar Mateoae1250b2014-07-24 17:04:37 +0100432
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000433 /* True 32b PPGTT with dynamic page allocation: update PDP
434 * registers and point the unallocated PDPs to scratch page.
435 * PML4 is allocated during ppgtt init, so this is not needed
436 * in 48-bit mode.
437 */
Chris Wilson949e8ab2017-02-09 14:40:36 +0000438 if (ppgtt && !i915_vm_is_48bit(&ppgtt->base))
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000439 execlists_update_context_pdps(ppgtt, reg_state);
Chris Wilson70c2a242016-09-09 14:11:46 +0100440
441 return ce->lrc_desc;
Oscar Mateoae1250b2014-07-24 17:04:37 +0100442}
443
Chris Wilsonbeecec92017-10-03 21:34:52 +0100444static inline void elsp_write(u64 desc, u32 __iomem *elsp)
445{
446 writel(upper_32_bits(desc), elsp);
447 writel(lower_32_bits(desc), elsp);
448}
449
Chris Wilson70c2a242016-09-09 14:11:46 +0100450static void execlists_submit_ports(struct intel_engine_cs *engine)
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100451{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300452 struct execlist_port *port = engine->execlists.port;
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100453 u32 __iomem *elsp =
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100454 engine->i915->regs + i915_mmio_reg_offset(RING_ELSP(engine));
455 unsigned int n;
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100456
Mika Kuoppala76e70082017-09-22 15:43:07 +0300457 for (n = execlists_num_ports(&engine->execlists); n--; ) {
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100458 struct drm_i915_gem_request *rq;
459 unsigned int count;
460 u64 desc;
Chris Wilson70c2a242016-09-09 14:11:46 +0100461
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100462 rq = port_unpack(&port[n], &count);
463 if (rq) {
464 GEM_BUG_ON(count > !n);
465 if (!count++)
466 execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_IN);
467 port_set(&port[n], port_pack(rq, count));
468 desc = execlists_update_context(rq);
469 GEM_DEBUG_EXEC(port[n].context_id = upper_32_bits(desc));
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000470
471 GEM_TRACE("%s in[%d]: ctx=%d.%d, seqno=%x\n",
472 engine->name, n,
473 rq->ctx->hw_id, count,
474 rq->global_seqno);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100475 } else {
476 GEM_BUG_ON(!n);
477 desc = 0;
478 }
479
Chris Wilsonbeecec92017-10-03 21:34:52 +0100480 elsp_write(desc, elsp);
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100481 }
Chris Wilsonbbd6c472016-09-09 14:11:45 +0100482}
483
Chris Wilson70c2a242016-09-09 14:11:46 +0100484static bool ctx_single_port_submission(const struct i915_gem_context *ctx)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100485{
Chris Wilson70c2a242016-09-09 14:11:46 +0100486 return (IS_ENABLED(CONFIG_DRM_I915_GVT) &&
Chris Wilson60958682016-12-31 11:20:11 +0000487 i915_gem_context_force_single_submission(ctx));
Ben Widawsky84b790f2014-07-24 17:04:36 +0100488}
489
Chris Wilson70c2a242016-09-09 14:11:46 +0100490static bool can_merge_ctx(const struct i915_gem_context *prev,
491 const struct i915_gem_context *next)
Michel Thierryacdd8842014-07-24 17:04:38 +0100492{
Chris Wilson70c2a242016-09-09 14:11:46 +0100493 if (prev != next)
494 return false;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100495
Chris Wilson70c2a242016-09-09 14:11:46 +0100496 if (ctx_single_port_submission(prev))
497 return false;
Michel Thierryacdd8842014-07-24 17:04:38 +0100498
Chris Wilson70c2a242016-09-09 14:11:46 +0100499 return true;
500}
Peter Antoine779949f2015-05-11 16:03:27 +0100501
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100502static void port_assign(struct execlist_port *port,
503 struct drm_i915_gem_request *rq)
504{
505 GEM_BUG_ON(rq == port_request(port));
506
507 if (port_isset(port))
508 i915_gem_request_put(port_request(port));
509
510 port_set(port, port_pack(i915_gem_request_get(rq), port_count(port)));
511}
512
Chris Wilsonbeecec92017-10-03 21:34:52 +0100513static void inject_preempt_context(struct intel_engine_cs *engine)
514{
515 struct intel_context *ce =
516 &engine->i915->preempt_context->engine[engine->id];
517 u32 __iomem *elsp =
518 engine->i915->regs + i915_mmio_reg_offset(RING_ELSP(engine));
519 unsigned int n;
520
521 GEM_BUG_ON(engine->i915->preempt_context->hw_id != PREEMPT_ID);
522 GEM_BUG_ON(!IS_ALIGNED(ce->ring->size, WA_TAIL_BYTES));
523
524 memset(ce->ring->vaddr + ce->ring->tail, 0, WA_TAIL_BYTES);
525 ce->ring->tail += WA_TAIL_BYTES;
526 ce->ring->tail &= (ce->ring->size - 1);
527 ce->lrc_reg_state[CTX_RING_TAIL+1] = ce->ring->tail;
528
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000529 GEM_TRACE("\n");
Chris Wilsonbeecec92017-10-03 21:34:52 +0100530 for (n = execlists_num_ports(&engine->execlists); --n; )
531 elsp_write(0, elsp);
532
533 elsp_write(ce->lrc_desc, elsp);
534}
535
Chris Wilson70c2a242016-09-09 14:11:46 +0100536static void execlists_dequeue(struct intel_engine_cs *engine)
537{
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300538 struct intel_engine_execlists * const execlists = &engine->execlists;
539 struct execlist_port *port = execlists->port;
Mika Kuoppala76e70082017-09-22 15:43:07 +0300540 const struct execlist_port * const last_port =
541 &execlists->port[execlists->port_mask];
Chris Wilsonbeecec92017-10-03 21:34:52 +0100542 struct drm_i915_gem_request *last = port_request(port);
Chris Wilson20311bd2016-11-14 20:41:03 +0000543 struct rb_node *rb;
Chris Wilson70c2a242016-09-09 14:11:46 +0100544 bool submit = false;
Michel Thierryacdd8842014-07-24 17:04:38 +0100545
Chris Wilson70c2a242016-09-09 14:11:46 +0100546 /* Hardware submission is through 2 ports. Conceptually each port
547 * has a (RING_START, RING_HEAD, RING_TAIL) tuple. RING_START is
548 * static for a context, and unique to each, so we only execute
549 * requests belonging to a single context from each ring. RING_HEAD
550 * is maintained by the CS in the context image, it marks the place
551 * where it got up to last time, and through RING_TAIL we tell the CS
552 * where we want to execute up to this time.
553 *
554 * In this list the requests are in order of execution. Consecutive
555 * requests from the same context are adjacent in the ringbuffer. We
556 * can combine these requests into a single RING_TAIL update:
557 *
558 * RING_HEAD...req1...req2
559 * ^- RING_TAIL
560 * since to execute req2 the CS must first execute req1.
561 *
562 * Our goal then is to point each port to the end of a consecutive
563 * sequence of requests as being the most optimal (fewest wake ups
564 * and context switches) submission.
565 */
566
Tvrtko Ursulin9f7886d2017-03-21 10:55:11 +0000567 spin_lock_irq(&engine->timeline->lock);
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300568 rb = execlists->first;
569 GEM_BUG_ON(rb_first(&execlists->queue) != rb);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100570 if (!rb)
571 goto unlock;
572
573 if (last) {
574 /*
575 * Don't resubmit or switch until all outstanding
576 * preemptions (lite-restore) are seen. Then we
577 * know the next preemption status we see corresponds
578 * to this ELSP update.
579 */
580 if (port_count(&port[0]) > 1)
581 goto unlock;
582
Michał Winiarskia4598d12017-10-25 22:00:18 +0200583 if (HAS_LOGICAL_RING_PREEMPTION(engine->i915) &&
Chris Wilsonbeecec92017-10-03 21:34:52 +0100584 rb_entry(rb, struct i915_priolist, node)->priority >
585 max(last->priotree.priority, 0)) {
586 /*
587 * Switch to our empty preempt context so
588 * the state of the GPU is known (idle).
589 */
590 inject_preempt_context(engine);
Chris Wilson4a118ec2017-10-23 22:32:36 +0100591 execlists_set_active(execlists,
592 EXECLISTS_ACTIVE_PREEMPT);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100593 goto unlock;
594 } else {
595 /*
596 * In theory, we could coalesce more requests onto
597 * the second port (the first port is active, with
598 * no preemptions pending). However, that means we
599 * then have to deal with the possible lite-restore
600 * of the second port (as we submit the ELSP, there
601 * may be a context-switch) but also we may complete
602 * the resubmission before the context-switch. Ergo,
603 * coalescing onto the second port will cause a
604 * preemption event, but we cannot predict whether
605 * that will affect port[0] or port[1].
606 *
607 * If the second port is already active, we can wait
608 * until the next context-switch before contemplating
609 * new requests. The GPU will be busy and we should be
610 * able to resubmit the new ELSP before it idles,
611 * avoiding pipeline bubbles (momentary pauses where
612 * the driver is unable to keep up the supply of new
613 * work).
614 */
615 if (port_count(&port[1]))
616 goto unlock;
617
618 /* WaIdleLiteRestore:bdw,skl
619 * Apply the wa NOOPs to prevent
620 * ring:HEAD == req:TAIL as we resubmit the
621 * request. See gen8_emit_breadcrumb() for
622 * where we prepare the padding after the
623 * end of the request.
624 */
625 last->tail = last->wa_tail;
626 }
627 }
628
629 do {
Chris Wilson6c067572017-05-17 13:10:03 +0100630 struct i915_priolist *p = rb_entry(rb, typeof(*p), node);
631 struct drm_i915_gem_request *rq, *rn;
Chris Wilson20311bd2016-11-14 20:41:03 +0000632
Chris Wilson6c067572017-05-17 13:10:03 +0100633 list_for_each_entry_safe(rq, rn, &p->requests, priotree.link) {
634 /*
635 * Can we combine this request with the current port?
636 * It has to be the same context/ringbuffer and not
637 * have any exceptions (e.g. GVT saying never to
638 * combine contexts).
639 *
640 * If we can combine the requests, we can execute both
641 * by updating the RING_TAIL to point to the end of the
642 * second request, and so we never need to tell the
643 * hardware about the first.
Chris Wilson70c2a242016-09-09 14:11:46 +0100644 */
Chris Wilson6c067572017-05-17 13:10:03 +0100645 if (last && !can_merge_ctx(rq->ctx, last->ctx)) {
646 /*
647 * If we are on the second port and cannot
648 * combine this request with the last, then we
649 * are done.
650 */
Mika Kuoppala76e70082017-09-22 15:43:07 +0300651 if (port == last_port) {
Chris Wilson6c067572017-05-17 13:10:03 +0100652 __list_del_many(&p->requests,
653 &rq->priotree.link);
654 goto done;
655 }
Chris Wilson70c2a242016-09-09 14:11:46 +0100656
Chris Wilson6c067572017-05-17 13:10:03 +0100657 /*
658 * If GVT overrides us we only ever submit
659 * port[0], leaving port[1] empty. Note that we
660 * also have to be careful that we don't queue
661 * the same context (even though a different
662 * request) to the second port.
663 */
664 if (ctx_single_port_submission(last->ctx) ||
665 ctx_single_port_submission(rq->ctx)) {
666 __list_del_many(&p->requests,
667 &rq->priotree.link);
668 goto done;
669 }
Chris Wilson70c2a242016-09-09 14:11:46 +0100670
Chris Wilson6c067572017-05-17 13:10:03 +0100671 GEM_BUG_ON(last->ctx == rq->ctx);
Chris Wilson70c2a242016-09-09 14:11:46 +0100672
Chris Wilson6c067572017-05-17 13:10:03 +0100673 if (submit)
674 port_assign(port, last);
675 port++;
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300676
677 GEM_BUG_ON(port_isset(port));
Chris Wilson6c067572017-05-17 13:10:03 +0100678 }
679
680 INIT_LIST_HEAD(&rq->priotree.link);
Chris Wilson6c067572017-05-17 13:10:03 +0100681 __i915_gem_request_submit(rq);
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300682 trace_i915_gem_request_in(rq, port_index(port, execlists));
Chris Wilson6c067572017-05-17 13:10:03 +0100683 last = rq;
684 submit = true;
Chris Wilson70c2a242016-09-09 14:11:46 +0100685 }
Chris Wilsond55ac5b2016-11-14 20:40:59 +0000686
Chris Wilson20311bd2016-11-14 20:41:03 +0000687 rb = rb_next(rb);
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300688 rb_erase(&p->node, &execlists->queue);
Chris Wilson6c067572017-05-17 13:10:03 +0100689 INIT_LIST_HEAD(&p->requests);
690 if (p->priority != I915_PRIORITY_NORMAL)
Chris Wilsonc5cf9a92017-05-17 13:10:04 +0100691 kmem_cache_free(engine->i915->priorities, p);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100692 } while (rb);
Chris Wilson6c067572017-05-17 13:10:03 +0100693done:
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300694 execlists->first = rb;
Chris Wilson6c067572017-05-17 13:10:03 +0100695 if (submit)
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100696 port_assign(port, last);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100697unlock:
Tvrtko Ursulin9f7886d2017-03-21 10:55:11 +0000698 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson70c2a242016-09-09 14:11:46 +0100699
Chris Wilson4a118ec2017-10-23 22:32:36 +0100700 if (submit) {
701 execlists_set_active(execlists, EXECLISTS_ACTIVE_USER);
Chris Wilson70c2a242016-09-09 14:11:46 +0100702 execlists_submit_ports(engine);
Chris Wilson4a118ec2017-10-23 22:32:36 +0100703 }
Michel Thierryacdd8842014-07-24 17:04:38 +0100704}
705
Michał Winiarskic41937f2017-10-26 15:35:58 +0200706void
Michał Winiarskia4598d12017-10-25 22:00:18 +0200707execlists_cancel_port_requests(struct intel_engine_execlists * const execlists)
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300708{
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100709 struct execlist_port *port = execlists->port;
Mika Kuoppaladc2279e2017-10-10 14:48:57 +0300710 unsigned int num_ports = execlists_num_ports(execlists);
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300711
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100712 while (num_ports-- && port_isset(port)) {
Chris Wilson7e44fc22017-09-26 11:17:19 +0100713 struct drm_i915_gem_request *rq = port_request(port);
714
Chris Wilson4a118ec2017-10-23 22:32:36 +0100715 GEM_BUG_ON(!execlists->active);
Chris Wilsond6c05112017-10-03 21:34:47 +0100716 execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_PREEMPTED);
Chris Wilson7e44fc22017-09-26 11:17:19 +0100717 i915_gem_request_put(rq);
718
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100719 memset(port, 0, sizeof(*port));
720 port++;
721 }
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300722}
723
Chris Wilson27a5f612017-09-15 18:31:00 +0100724static void execlists_cancel_requests(struct intel_engine_cs *engine)
725{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300726 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilson27a5f612017-09-15 18:31:00 +0100727 struct drm_i915_gem_request *rq, *rn;
728 struct rb_node *rb;
729 unsigned long flags;
Chris Wilson27a5f612017-09-15 18:31:00 +0100730
731 spin_lock_irqsave(&engine->timeline->lock, flags);
732
733 /* Cancel the requests on the HW and clear the ELSP tracker. */
Michał Winiarskia4598d12017-10-25 22:00:18 +0200734 execlists_cancel_port_requests(execlists);
Chris Wilson27a5f612017-09-15 18:31:00 +0100735
736 /* Mark all executing requests as skipped. */
737 list_for_each_entry(rq, &engine->timeline->requests, link) {
738 GEM_BUG_ON(!rq->global_seqno);
739 if (!i915_gem_request_completed(rq))
740 dma_fence_set_error(&rq->fence, -EIO);
741 }
742
743 /* Flush the queued requests to the timeline list (for retiring). */
Mika Kuoppalab620e872017-09-22 15:43:03 +0300744 rb = execlists->first;
Chris Wilson27a5f612017-09-15 18:31:00 +0100745 while (rb) {
746 struct i915_priolist *p = rb_entry(rb, typeof(*p), node);
747
748 list_for_each_entry_safe(rq, rn, &p->requests, priotree.link) {
749 INIT_LIST_HEAD(&rq->priotree.link);
Chris Wilson27a5f612017-09-15 18:31:00 +0100750
751 dma_fence_set_error(&rq->fence, -EIO);
752 __i915_gem_request_submit(rq);
753 }
754
755 rb = rb_next(rb);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300756 rb_erase(&p->node, &execlists->queue);
Chris Wilson27a5f612017-09-15 18:31:00 +0100757 INIT_LIST_HEAD(&p->requests);
758 if (p->priority != I915_PRIORITY_NORMAL)
759 kmem_cache_free(engine->i915->priorities, p);
760 }
761
762 /* Remaining _unready_ requests will be nop'ed when submitted */
763
Mika Kuoppalacf4591d2017-09-22 15:43:05 +0300764
Mika Kuoppalab620e872017-09-22 15:43:03 +0300765 execlists->queue = RB_ROOT;
766 execlists->first = NULL;
Chris Wilson3f9e6cd2017-09-25 13:49:27 +0100767 GEM_BUG_ON(port_isset(execlists->port));
Chris Wilson27a5f612017-09-15 18:31:00 +0100768
769 /*
770 * The port is checked prior to scheduling a tasklet, but
771 * just in case we have suspended the tasklet to do the
772 * wedging make sure that when it wakes, it decides there
773 * is no work to do by clearing the irq_posted bit.
774 */
775 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
776
777 spin_unlock_irqrestore(&engine->timeline->lock, flags);
778}
779
Daniel Vetter6e5248b2016-07-15 21:48:06 +0200780/*
Oscar Mateo73e4d072014-07-24 17:04:48 +0100781 * Check the unread Context Status Buffers and manage the submission of new
782 * contexts to the ELSP accordingly.
783 */
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +0100784static void intel_lrc_irq_handler(unsigned long data)
Thomas Daniele981e7b2014-07-24 17:04:39 +0100785{
Mika Kuoppalab620e872017-09-22 15:43:03 +0300786 struct intel_engine_cs * const engine = (struct intel_engine_cs *)data;
787 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilsonbeecec92017-10-03 21:34:52 +0100788 struct execlist_port * const port = execlists->port;
Chris Wilsonc0336662016-05-06 15:40:21 +0100789 struct drm_i915_private *dev_priv = engine->i915;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100790
Chris Wilson48921262017-04-11 18:58:50 +0100791 /* We can skip acquiring intel_runtime_pm_get() here as it was taken
792 * on our behalf by the request (see i915_gem_mark_busy()) and it will
793 * not be relinquished until the device is idle (see
794 * i915_gem_idle_work_handler()). As a precaution, we make sure
795 * that all ELSP are drained i.e. we have processed the CSB,
796 * before allowing ourselves to idle and calling intel_runtime_pm_put().
797 */
798 GEM_BUG_ON(!dev_priv->gt.awake);
799
Mika Kuoppalab620e872017-09-22 15:43:03 +0300800 intel_uncore_forcewake_get(dev_priv, execlists->fw_domains);
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000801
Chris Wilson899f6202017-03-21 11:33:20 +0000802 /* Prefer doing test_and_clear_bit() as a two stage operation to avoid
803 * imposing the cost of a locked atomic transaction when submitting a
804 * new request (outside of the context-switch interrupt).
805 */
806 while (test_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted)) {
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100807 /* The HWSP contains a (cacheable) mirror of the CSB */
808 const u32 *buf =
809 &engine->status_page.page_addr[I915_HWS_CSB_BUF0_INDEX];
Chris Wilson4af0d722017-03-25 20:10:53 +0000810 unsigned int head, tail;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100811
Mika Kuoppalab620e872017-09-22 15:43:03 +0300812 if (unlikely(execlists->csb_use_mmio)) {
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100813 buf = (u32 * __force)
814 (dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_BUF_LO(engine, 0)));
Mika Kuoppalab620e872017-09-22 15:43:03 +0300815 execlists->csb_head = -1; /* force mmio read of CSB ptrs */
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100816 }
817
Chris Wilson2e70b8c2017-03-23 13:48:03 +0000818 /* The write will be ordered by the uncached read (itself
819 * a memory barrier), so we do not need another in the form
820 * of a locked instruction. The race between the interrupt
821 * handler and the split test/clear is harmless as we order
822 * our clear before the CSB read. If the interrupt arrived
823 * first between the test and the clear, we read the updated
824 * CSB and clear the bit. If the interrupt arrives as we read
825 * the CSB or later (i.e. after we had cleared the bit) the bit
826 * is set and we do a new loop.
827 */
828 __clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300829 if (unlikely(execlists->csb_head == -1)) { /* following a reset */
Chris Wilson767a9832017-09-13 09:56:05 +0100830 head = readl(dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)));
831 tail = GEN8_CSB_WRITE_PTR(head);
832 head = GEN8_CSB_READ_PTR(head);
Mika Kuoppalab620e872017-09-22 15:43:03 +0300833 execlists->csb_head = head;
Chris Wilson767a9832017-09-13 09:56:05 +0100834 } else {
835 const int write_idx =
836 intel_hws_csb_write_index(dev_priv) -
837 I915_HWS_CSB_BUF0_INDEX;
838
Mika Kuoppalab620e872017-09-22 15:43:03 +0300839 head = execlists->csb_head;
Chris Wilson767a9832017-09-13 09:56:05 +0100840 tail = READ_ONCE(buf[write_idx]);
841 }
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000842 GEM_TRACE("%s cs-irq head=%d [%d], tail=%d [%d]\n",
843 engine->name,
844 head, GEN8_CSB_READ_PTR(readl(dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)))),
845 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 +0300846
Chris Wilson4af0d722017-03-25 20:10:53 +0000847 while (head != tail) {
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100848 struct drm_i915_gem_request *rq;
Chris Wilson4af0d722017-03-25 20:10:53 +0000849 unsigned int status;
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100850 unsigned int count;
Chris Wilsona37951a2017-01-24 11:00:06 +0000851
Chris Wilson4af0d722017-03-25 20:10:53 +0000852 if (++head == GEN8_CSB_ENTRIES)
853 head = 0;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100854
Chris Wilson2ffe80a2017-02-06 17:05:02 +0000855 /* We are flying near dragons again.
856 *
857 * We hold a reference to the request in execlist_port[]
858 * but no more than that. We are operating in softirq
859 * context and so cannot hold any mutex or sleep. That
860 * prevents us stopping the requests we are processing
861 * in port[] from being retired simultaneously (the
862 * breadcrumb will be complete before we see the
863 * context-switch). As we only hold the reference to the
864 * request, any pointer chasing underneath the request
865 * is subject to a potential use-after-free. Thus we
866 * store all of the bookkeeping within port[] as
867 * required, and avoid using unguarded pointers beneath
868 * request itself. The same applies to the atomic
869 * status notifier.
870 */
871
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100872 status = READ_ONCE(buf[2 * head]); /* maybe mmio! */
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000873 GEM_TRACE("%s csb[%dd]: status=0x%08x:0x%08x\n",
874 engine->name, head,
875 status, buf[2*head + 1]);
Chris Wilson70c2a242016-09-09 14:11:46 +0100876 if (!(status & GEN8_CTX_STATUS_COMPLETED_MASK))
877 continue;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100878
Chris Wilsonbeecec92017-10-03 21:34:52 +0100879 if (status & GEN8_CTX_STATUS_ACTIVE_IDLE &&
880 buf[2*head + 1] == PREEMPT_ID) {
Michał Winiarskia4598d12017-10-25 22:00:18 +0200881 execlists_cancel_port_requests(execlists);
882 execlists_unwind_incomplete_requests(execlists);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100883
Chris Wilson4a118ec2017-10-23 22:32:36 +0100884 GEM_BUG_ON(!execlists_is_active(execlists,
885 EXECLISTS_ACTIVE_PREEMPT));
886 execlists_clear_active(execlists,
887 EXECLISTS_ACTIVE_PREEMPT);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100888 continue;
889 }
890
891 if (status & GEN8_CTX_STATUS_PREEMPTED &&
Chris Wilson4a118ec2017-10-23 22:32:36 +0100892 execlists_is_active(execlists,
893 EXECLISTS_ACTIVE_PREEMPT))
Chris Wilsonbeecec92017-10-03 21:34:52 +0100894 continue;
895
Chris Wilson4a118ec2017-10-23 22:32:36 +0100896 GEM_BUG_ON(!execlists_is_active(execlists,
897 EXECLISTS_ACTIVE_USER));
898
Chris Wilson86aa7e72017-01-23 11:31:32 +0000899 /* Check the context/desc id for this event matches */
Chris Wilson6d2cb5a2017-09-13 14:35:34 +0100900 GEM_DEBUG_BUG_ON(buf[2 * head + 1] != port->context_id);
Chris Wilson86aa7e72017-01-23 11:31:32 +0000901
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100902 rq = port_unpack(port, &count);
Chris Wilsonbccd3b82017-11-09 14:30:19 +0000903 GEM_TRACE("%s out[0]: ctx=%d.%d, seqno=%x\n",
904 engine->name,
905 rq->ctx->hw_id, count,
906 rq->global_seqno);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100907 GEM_BUG_ON(count == 0);
908 if (--count == 0) {
Chris Wilson70c2a242016-09-09 14:11:46 +0100909 GEM_BUG_ON(status & GEN8_CTX_STATUS_PREEMPTED);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100910 GEM_BUG_ON(!i915_gem_request_completed(rq));
911 execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_OUT);
Thomas Daniele981e7b2014-07-24 17:04:39 +0100912
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100913 trace_i915_gem_request_out(rq);
914 i915_gem_request_put(rq);
915
Mika Kuoppala7a62cc62017-09-22 15:43:06 +0300916 execlists_port_complete(execlists, port);
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100917 } else {
918 port_set(port, port_pack(rq, count));
Chris Wilson70c2a242016-09-09 14:11:46 +0100919 }
Tvrtko Ursulinc6a2ac72016-02-26 16:58:32 +0000920
Chris Wilson77f0d0e2017-05-17 13:10:00 +0100921 /* After the final element, the hw should be idle */
922 GEM_BUG_ON(port_count(port) == 0 &&
Chris Wilson70c2a242016-09-09 14:11:46 +0100923 !(status & GEN8_CTX_STATUS_ACTIVE_IDLE));
Chris Wilson4a118ec2017-10-23 22:32:36 +0100924 if (port_count(port) == 0)
925 execlists_clear_active(execlists,
926 EXECLISTS_ACTIVE_USER);
Chris Wilson4af0d722017-03-25 20:10:53 +0000927 }
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000928
Mika Kuoppalab620e872017-09-22 15:43:03 +0300929 if (head != execlists->csb_head) {
930 execlists->csb_head = head;
Chris Wilson767a9832017-09-13 09:56:05 +0100931 writel(_MASKED_FIELD(GEN8_CSB_READ_PTR_MASK, head << 8),
932 dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)));
933 }
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000934 }
935
Chris Wilson4a118ec2017-10-23 22:32:36 +0100936 if (!execlists_is_active(execlists, EXECLISTS_ACTIVE_PREEMPT))
Chris Wilson70c2a242016-09-09 14:11:46 +0100937 execlists_dequeue(engine);
Tvrtko Ursulin26720ab2016-03-17 12:59:46 +0000938
Mika Kuoppalab620e872017-09-22 15:43:03 +0300939 intel_uncore_forcewake_put(dev_priv, execlists->fw_domains);
Thomas Daniele981e7b2014-07-24 17:04:39 +0100940}
941
Chris Wilson27606fd2017-09-16 21:44:13 +0100942static void insert_request(struct intel_engine_cs *engine,
943 struct i915_priotree *pt,
944 int prio)
945{
946 struct i915_priolist *p = lookup_priolist(engine, pt, prio);
947
948 list_add_tail(&pt->link, &ptr_mask_bits(p, 1)->requests);
Chris Wilsonbeecec92017-10-03 21:34:52 +0100949 if (ptr_unmask_bits(p, 1))
Mika Kuoppalab620e872017-09-22 15:43:03 +0300950 tasklet_hi_schedule(&engine->execlists.irq_tasklet);
Chris Wilson27606fd2017-09-16 21:44:13 +0100951}
952
Chris Wilsonf4ea6bd2016-08-02 22:50:32 +0100953static void execlists_submit_request(struct drm_i915_gem_request *request)
Michel Thierryacdd8842014-07-24 17:04:38 +0100954{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000955 struct intel_engine_cs *engine = request->engine;
Chris Wilson5590af32016-09-09 14:11:54 +0100956 unsigned long flags;
Michel Thierryacdd8842014-07-24 17:04:38 +0100957
Chris Wilson663f71e2016-11-14 20:41:00 +0000958 /* Will be called from irq-context when using foreign fences. */
959 spin_lock_irqsave(&engine->timeline->lock, flags);
Michel Thierryacdd8842014-07-24 17:04:38 +0100960
Chris Wilson27606fd2017-09-16 21:44:13 +0100961 insert_request(engine, &request->priotree, request->priotree.priority);
Michel Thierryacdd8842014-07-24 17:04:38 +0100962
Mika Kuoppalab620e872017-09-22 15:43:03 +0300963 GEM_BUG_ON(!engine->execlists.first);
Chris Wilson6c067572017-05-17 13:10:03 +0100964 GEM_BUG_ON(list_empty(&request->priotree.link));
965
Chris Wilson663f71e2016-11-14 20:41:00 +0000966 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Michel Thierryacdd8842014-07-24 17:04:38 +0100967}
968
Chris Wilson1f181222017-10-03 21:34:50 +0100969static struct drm_i915_gem_request *pt_to_request(struct i915_priotree *pt)
970{
971 return container_of(pt, struct drm_i915_gem_request, priotree);
972}
973
Chris Wilson20311bd2016-11-14 20:41:03 +0000974static struct intel_engine_cs *
975pt_lock_engine(struct i915_priotree *pt, struct intel_engine_cs *locked)
976{
Chris Wilson1f181222017-10-03 21:34:50 +0100977 struct intel_engine_cs *engine = pt_to_request(pt)->engine;
Chris Wilson20311bd2016-11-14 20:41:03 +0000978
Chris Wilsona79a5242017-03-27 21:21:43 +0100979 GEM_BUG_ON(!locked);
980
Chris Wilson20311bd2016-11-14 20:41:03 +0000981 if (engine != locked) {
Chris Wilsona79a5242017-03-27 21:21:43 +0100982 spin_unlock(&locked->timeline->lock);
983 spin_lock(&engine->timeline->lock);
Chris Wilson20311bd2016-11-14 20:41:03 +0000984 }
985
986 return engine;
987}
988
989static void execlists_schedule(struct drm_i915_gem_request *request, int prio)
990{
Chris Wilsona79a5242017-03-27 21:21:43 +0100991 struct intel_engine_cs *engine;
Chris Wilson20311bd2016-11-14 20:41:03 +0000992 struct i915_dependency *dep, *p;
993 struct i915_dependency stack;
994 LIST_HEAD(dfs);
995
Chris Wilson7d1ea602017-09-28 20:39:00 +0100996 GEM_BUG_ON(prio == I915_PRIORITY_INVALID);
997
Chris Wilson20311bd2016-11-14 20:41:03 +0000998 if (prio <= READ_ONCE(request->priotree.priority))
999 return;
1000
Chris Wilson70cd1472016-11-28 14:36:49 +00001001 /* Need BKL in order to use the temporary link inside i915_dependency */
1002 lockdep_assert_held(&request->i915->drm.struct_mutex);
Chris Wilson20311bd2016-11-14 20:41:03 +00001003
1004 stack.signaler = &request->priotree;
1005 list_add(&stack.dfs_link, &dfs);
1006
1007 /* Recursively bump all dependent priorities to match the new request.
1008 *
1009 * A naive approach would be to use recursion:
1010 * static void update_priorities(struct i915_priotree *pt, prio) {
1011 * list_for_each_entry(dep, &pt->signalers_list, signal_link)
1012 * update_priorities(dep->signal, prio)
1013 * insert_request(pt);
1014 * }
1015 * but that may have unlimited recursion depth and so runs a very
1016 * real risk of overunning the kernel stack. Instead, we build
1017 * a flat list of all dependencies starting with the current request.
1018 * As we walk the list of dependencies, we add all of its dependencies
1019 * to the end of the list (this may include an already visited
1020 * request) and continue to walk onwards onto the new dependencies. The
1021 * end result is a topological list of requests in reverse order, the
1022 * last element in the list is the request we must execute first.
1023 */
1024 list_for_each_entry_safe(dep, p, &dfs, dfs_link) {
1025 struct i915_priotree *pt = dep->signaler;
1026
Chris Wilsona79a5242017-03-27 21:21:43 +01001027 /* Within an engine, there can be no cycle, but we may
1028 * refer to the same dependency chain multiple times
1029 * (redundant dependencies are not eliminated) and across
1030 * engines.
1031 */
1032 list_for_each_entry(p, &pt->signalers_list, signal_link) {
Chris Wilson1f181222017-10-03 21:34:50 +01001033 if (i915_gem_request_completed(pt_to_request(p->signaler)))
1034 continue;
1035
Chris Wilsona79a5242017-03-27 21:21:43 +01001036 GEM_BUG_ON(p->signaler->priority < pt->priority);
Chris Wilson20311bd2016-11-14 20:41:03 +00001037 if (prio > READ_ONCE(p->signaler->priority))
1038 list_move_tail(&p->dfs_link, &dfs);
Chris Wilsona79a5242017-03-27 21:21:43 +01001039 }
Chris Wilson20311bd2016-11-14 20:41:03 +00001040
Chris Wilson0798cff2016-12-05 14:29:41 +00001041 list_safe_reset_next(dep, p, dfs_link);
Chris Wilson20311bd2016-11-14 20:41:03 +00001042 }
1043
Chris Wilson349bdb62017-05-17 13:10:05 +01001044 /* If we didn't need to bump any existing priorities, and we haven't
1045 * yet submitted this request (i.e. there is no potential race with
1046 * execlists_submit_request()), we can set our own priority and skip
1047 * acquiring the engine locks.
1048 */
Chris Wilson7d1ea602017-09-28 20:39:00 +01001049 if (request->priotree.priority == I915_PRIORITY_INVALID) {
Chris Wilson349bdb62017-05-17 13:10:05 +01001050 GEM_BUG_ON(!list_empty(&request->priotree.link));
1051 request->priotree.priority = prio;
1052 if (stack.dfs_link.next == stack.dfs_link.prev)
1053 return;
1054 __list_del_entry(&stack.dfs_link);
1055 }
1056
Chris Wilsona79a5242017-03-27 21:21:43 +01001057 engine = request->engine;
1058 spin_lock_irq(&engine->timeline->lock);
1059
Chris Wilson20311bd2016-11-14 20:41:03 +00001060 /* Fifo and depth-first replacement ensure our deps execute before us */
1061 list_for_each_entry_safe_reverse(dep, p, &dfs, dfs_link) {
1062 struct i915_priotree *pt = dep->signaler;
1063
1064 INIT_LIST_HEAD(&dep->dfs_link);
1065
1066 engine = pt_lock_engine(pt, engine);
1067
1068 if (prio <= pt->priority)
1069 continue;
1070
Chris Wilson20311bd2016-11-14 20:41:03 +00001071 pt->priority = prio;
Chris Wilson6c067572017-05-17 13:10:03 +01001072 if (!list_empty(&pt->link)) {
1073 __list_del_entry(&pt->link);
1074 insert_request(engine, pt, prio);
Chris Wilsona79a5242017-03-27 21:21:43 +01001075 }
Chris Wilson20311bd2016-11-14 20:41:03 +00001076 }
1077
Chris Wilsona79a5242017-03-27 21:21:43 +01001078 spin_unlock_irq(&engine->timeline->lock);
Chris Wilson20311bd2016-11-14 20:41:03 +00001079}
1080
Chris Wilsonf4e15af2017-11-10 14:26:32 +00001081static int __context_pin(struct i915_gem_context *ctx, struct i915_vma *vma)
1082{
1083 unsigned int flags;
1084 int err;
1085
1086 /*
1087 * Clear this page out of any CPU caches for coherent swap-in/out.
1088 * We only want to do this on the first bind so that we do not stall
1089 * on an active context (which by nature is already on the GPU).
1090 */
1091 if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
1092 err = i915_gem_object_set_to_gtt_domain(vma->obj, true);
1093 if (err)
1094 return err;
1095 }
1096
1097 flags = PIN_GLOBAL | PIN_HIGH;
1098 if (ctx->ggtt_offset_bias)
1099 flags |= PIN_OFFSET_BIAS | ctx->ggtt_offset_bias;
1100
1101 return i915_vma_pin(vma, 0, GEN8_LR_CONTEXT_ALIGN, flags);
1102}
1103
Chris Wilson266a2402017-05-04 10:33:08 +01001104static struct intel_ring *
1105execlists_context_pin(struct intel_engine_cs *engine,
1106 struct i915_gem_context *ctx)
Oscar Mateodcb4c122014-11-13 10:28:10 +00001107{
Chris Wilson9021ad02016-05-24 14:53:37 +01001108 struct intel_context *ce = &ctx->engine[engine->id];
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001109 void *vaddr;
Tvrtko Ursulinca825802016-01-15 15:10:27 +00001110 int ret;
Oscar Mateodcb4c122014-11-13 10:28:10 +00001111
Chris Wilson91c8a322016-07-05 10:40:23 +01001112 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Tvrtko Ursulinca825802016-01-15 15:10:27 +00001113
Chris Wilson266a2402017-05-04 10:33:08 +01001114 if (likely(ce->pin_count++))
1115 goto out;
Chris Wilsona533b4b2017-03-16 17:16:28 +00001116 GEM_BUG_ON(!ce->pin_count); /* no overflow please! */
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001117
Chris Wilsone8a9c582016-12-18 15:37:20 +00001118 if (!ce->state) {
1119 ret = execlists_context_deferred_alloc(ctx, engine);
1120 if (ret)
1121 goto err;
1122 }
Chris Wilson56f6e0a2017-01-05 15:30:20 +00001123 GEM_BUG_ON(!ce->state);
Chris Wilsone8a9c582016-12-18 15:37:20 +00001124
Chris Wilsonf4e15af2017-11-10 14:26:32 +00001125 ret = __context_pin(ctx, ce->state);
Nick Hoathe84fe802015-09-11 12:53:46 +01001126 if (ret)
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001127 goto err;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001128
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001129 vaddr = i915_gem_object_pin_map(ce->state->obj, I915_MAP_WB);
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001130 if (IS_ERR(vaddr)) {
1131 ret = PTR_ERR(vaddr);
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001132 goto unpin_vma;
Tvrtko Ursulin82352e92016-01-15 17:12:45 +00001133 }
1134
Chris Wilsond822bb12017-04-03 12:34:25 +01001135 ret = intel_ring_pin(ce->ring, ctx->i915, ctx->ggtt_offset_bias);
Nick Hoathe84fe802015-09-11 12:53:46 +01001136 if (ret)
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001137 goto unpin_map;
Alex Daid1675192015-08-12 15:43:43 +01001138
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001139 intel_lr_context_descriptor_update(ctx, engine);
Chris Wilson9021ad02016-05-24 14:53:37 +01001140
Chris Wilsona3aabe82016-10-04 21:11:26 +01001141 ce->lrc_reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
1142 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001143 i915_ggtt_offset(ce->ring->vma);
Chris Wilsona3aabe82016-10-04 21:11:26 +01001144
Chris Wilson3d574a62017-10-13 21:26:16 +01001145 ce->state->obj->pin_global++;
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001146 i915_gem_context_get(ctx);
Chris Wilson266a2402017-05-04 10:33:08 +01001147out:
1148 return ce->ring;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001149
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01001150unpin_map:
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001151 i915_gem_object_unpin_map(ce->state->obj);
1152unpin_vma:
1153 __i915_vma_unpin(ce->state);
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001154err:
Chris Wilson9021ad02016-05-24 14:53:37 +01001155 ce->pin_count = 0;
Chris Wilson266a2402017-05-04 10:33:08 +01001156 return ERR_PTR(ret);
Oscar Mateodcb4c122014-11-13 10:28:10 +00001157}
1158
Chris Wilsone8a9c582016-12-18 15:37:20 +00001159static void execlists_context_unpin(struct intel_engine_cs *engine,
1160 struct i915_gem_context *ctx)
Oscar Mateodcb4c122014-11-13 10:28:10 +00001161{
Chris Wilson9021ad02016-05-24 14:53:37 +01001162 struct intel_context *ce = &ctx->engine[engine->id];
Daniel Vetteraf3302b2015-12-04 17:27:15 +01001163
Chris Wilson91c8a322016-07-05 10:40:23 +01001164 lockdep_assert_held(&ctx->i915->drm.struct_mutex);
Chris Wilson9021ad02016-05-24 14:53:37 +01001165 GEM_BUG_ON(ce->pin_count == 0);
Tvrtko Ursulin321fe302016-01-28 10:29:55 +00001166
Chris Wilson9021ad02016-05-24 14:53:37 +01001167 if (--ce->pin_count)
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001168 return;
1169
Chris Wilsonaad29fb2016-08-02 22:50:23 +01001170 intel_ring_unpin(ce->ring);
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001171
Chris Wilson3d574a62017-10-13 21:26:16 +01001172 ce->state->obj->pin_global--;
Chris Wilsonbf3783e2016-08-15 10:48:54 +01001173 i915_gem_object_unpin_map(ce->state->obj);
1174 i915_vma_unpin(ce->state);
Chris Wilson24f1d3c2016-04-28 09:56:53 +01001175
Chris Wilson9a6feaf2016-07-20 13:31:50 +01001176 i915_gem_context_put(ctx);
Oscar Mateodcb4c122014-11-13 10:28:10 +00001177}
1178
Chris Wilsonf73e7392016-12-18 15:37:24 +00001179static int execlists_request_alloc(struct drm_i915_gem_request *request)
Chris Wilsonef11c012016-12-18 15:37:19 +00001180{
1181 struct intel_engine_cs *engine = request->engine;
1182 struct intel_context *ce = &request->ctx->engine[engine->id];
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001183 u32 *cs;
Chris Wilsonef11c012016-12-18 15:37:19 +00001184
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
Chris Wilsonef11c012016-12-18 15:37:19 +00001197 /* Note that after this point, we have committed to using
1198 * this request as it is being used to both track the
1199 * state of engine initialisation and liveness of the
1200 * golden renderstate above. Think twice before you try
1201 * to cancel/unwind this request now.
1202 */
1203
1204 request->reserved_space -= EXECLISTS_REQUEST_SIZE;
1205 return 0;
Chris Wilsonef11c012016-12-18 15:37:19 +00001206}
1207
Arun Siluvery9e000842015-07-03 14:27:31 +01001208/*
1209 * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
1210 * PIPE_CONTROL instruction. This is required for the flush to happen correctly
1211 * but there is a slight complication as this is applied in WA batch where the
1212 * values are only initialized once so we cannot take register value at the
1213 * beginning and reuse it further; hence we save its value to memory, upload a
1214 * constant value with bit21 set and then we restore it back with the saved value.
1215 * To simplify the WA, a constant value is formed by using the default value
1216 * of this register. This shouldn't be a problem because we are only modifying
1217 * it for a short period and this batch in non-premptible. We can ofcourse
1218 * use additional instructions that read the actual value of the register
1219 * at that time and set our bit of interest but it makes the WA complicated.
1220 *
1221 * This WA is also required for Gen9 so extracting as a function avoids
1222 * code duplication.
1223 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001224static u32 *
1225gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery9e000842015-07-03 14:27:31 +01001226{
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001227 *batch++ = MI_STORE_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1228 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1229 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
1230 *batch++ = 0;
Arun Siluvery9e000842015-07-03 14:27:31 +01001231
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001232 *batch++ = MI_LOAD_REGISTER_IMM(1);
1233 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1234 *batch++ = 0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES;
Arun Siluvery9e000842015-07-03 14:27:31 +01001235
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001236 batch = gen8_emit_pipe_control(batch,
1237 PIPE_CONTROL_CS_STALL |
1238 PIPE_CONTROL_DC_FLUSH_ENABLE,
1239 0);
Arun Siluvery9e000842015-07-03 14:27:31 +01001240
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001241 *batch++ = MI_LOAD_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1242 *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1243 *batch++ = i915_ggtt_offset(engine->scratch) + 256;
1244 *batch++ = 0;
Arun Siluvery9e000842015-07-03 14:27:31 +01001245
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001246 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001247}
1248
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001249/*
1250 * Typically we only have one indirect_ctx and per_ctx batch buffer which are
1251 * initialized at the beginning and shared across all contexts but this field
1252 * helps us to have multiple batches at different offsets and select them based
1253 * on a criteria. At the moment this batch always start at the beginning of the page
1254 * and at this point we don't have multiple wa_ctx batch buffers.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001255 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001256 * The number of WA applied are not known at the beginning; we use this field
1257 * to return the no of DWORDS written.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001258 *
Daniel Vetter6e5248b2016-07-15 21:48:06 +02001259 * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
1260 * so it adds NOOPs as padding to make it cacheline aligned.
1261 * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
1262 * makes a complete batch buffer.
Arun Siluvery17ee9502015-06-19 19:07:01 +01001263 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001264static u32 *gen8_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001265{
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001266 /* WaDisableCtxRestoreArbitration:bdw,chv */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001267 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001268
Arun Siluveryc82435b2015-06-19 18:37:13 +01001269 /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001270 if (IS_BROADWELL(engine->i915))
1271 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
Arun Siluveryc82435b2015-06-19 18:37:13 +01001272
Arun Siluvery0160f052015-06-23 15:46:57 +01001273 /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
1274 /* Actual scratch location is at 128 bytes offset */
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001275 batch = gen8_emit_pipe_control(batch,
1276 PIPE_CONTROL_FLUSH_L3 |
1277 PIPE_CONTROL_GLOBAL_GTT_IVB |
1278 PIPE_CONTROL_CS_STALL |
1279 PIPE_CONTROL_QW_WRITE,
1280 i915_ggtt_offset(engine->scratch) +
1281 2 * CACHELINE_BYTES);
Arun Siluvery0160f052015-06-23 15:46:57 +01001282
Chris Wilsonbeecec92017-10-03 21:34:52 +01001283 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1284
Arun Siluvery17ee9502015-06-19 19:07:01 +01001285 /* Pad to end of cacheline */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001286 while ((unsigned long)batch % CACHELINE_BYTES)
1287 *batch++ = MI_NOOP;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001288
1289 /*
1290 * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
1291 * execution depends on the length specified in terms of cache lines
1292 * in the register CTX_RCS_INDIRECT_CTX
1293 */
1294
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001295 return batch;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001296}
1297
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001298static u32 *gen9_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
Arun Siluvery0504cff2015-07-14 15:01:27 +01001299{
Chris Wilsonbeecec92017-10-03 21:34:52 +01001300 *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
1301
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001302 /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt,glk */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001303 batch = gen8_emit_flush_coherentl3_wa(engine, batch);
Arun Siluverya4106a72015-07-14 15:01:29 +01001304
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001305 /* WaDisableGatherAtSetShaderCommonSlice:skl,bxt,kbl,glk */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001306 *batch++ = MI_LOAD_REGISTER_IMM(1);
1307 *batch++ = i915_mmio_reg_offset(COMMON_SLICE_CHICKEN2);
1308 *batch++ = _MASKED_BIT_DISABLE(
1309 GEN9_DISABLE_GATHER_AT_SET_SHADER_COMMON_SLICE);
1310 *batch++ = MI_NOOP;
Mika Kuoppala873e8172016-07-20 14:26:13 +03001311
Mika Kuoppala066d4622016-06-07 17:19:15 +03001312 /* WaClearSlmSpaceAtContextSwitch:kbl */
1313 /* Actual scratch location is at 128 bytes offset */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001314 if (IS_KBL_REVID(engine->i915, 0, KBL_REVID_A0)) {
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001315 batch = gen8_emit_pipe_control(batch,
1316 PIPE_CONTROL_FLUSH_L3 |
1317 PIPE_CONTROL_GLOBAL_GTT_IVB |
1318 PIPE_CONTROL_CS_STALL |
1319 PIPE_CONTROL_QW_WRITE,
1320 i915_ggtt_offset(engine->scratch)
1321 + 2 * CACHELINE_BYTES);
Mika Kuoppala066d4622016-06-07 17:19:15 +03001322 }
Tim Gore3485d992016-07-05 10:01:30 +01001323
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001324 /* WaMediaPoolStateCmdInWABB:bxt,glk */
Tim Gore3485d992016-07-05 10:01:30 +01001325 if (HAS_POOLED_EU(engine->i915)) {
1326 /*
1327 * EU pool configuration is setup along with golden context
1328 * during context initialization. This value depends on
1329 * device type (2x6 or 3x6) and needs to be updated based
1330 * on which subslice is disabled especially for 2x6
1331 * devices, however it is safe to load default
1332 * configuration of 3x6 device instead of masking off
1333 * corresponding bits because HW ignores bits of a disabled
1334 * subslice and drops down to appropriate config. Please
1335 * see render_state_setup() in i915_gem_render_state.c for
1336 * possible configurations, to avoid duplication they are
1337 * not shown here again.
1338 */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001339 *batch++ = GEN9_MEDIA_POOL_STATE;
1340 *batch++ = GEN9_MEDIA_POOL_ENABLE;
1341 *batch++ = 0x00777000;
1342 *batch++ = 0;
1343 *batch++ = 0;
1344 *batch++ = 0;
Tim Gore3485d992016-07-05 10:01:30 +01001345 }
1346
Chris Wilsonbeecec92017-10-03 21:34:52 +01001347 *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1348
Arun Siluvery0504cff2015-07-14 15:01:27 +01001349 /* Pad to end of cacheline */
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001350 while ((unsigned long)batch % CACHELINE_BYTES)
1351 *batch++ = MI_NOOP;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001352
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001353 return batch;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001354}
1355
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001356#define CTX_WA_BB_OBJ_SIZE (PAGE_SIZE)
1357
1358static int lrc_setup_wa_ctx(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001359{
Chris Wilson48bb74e2016-08-15 10:49:04 +01001360 struct drm_i915_gem_object *obj;
1361 struct i915_vma *vma;
1362 int err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001363
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001364 obj = i915_gem_object_create(engine->i915, CTX_WA_BB_OBJ_SIZE);
Chris Wilson48bb74e2016-08-15 10:49:04 +01001365 if (IS_ERR(obj))
1366 return PTR_ERR(obj);
1367
Chris Wilsona01cb372017-01-16 15:21:30 +00001368 vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
Chris Wilson48bb74e2016-08-15 10:49:04 +01001369 if (IS_ERR(vma)) {
1370 err = PTR_ERR(vma);
1371 goto err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001372 }
1373
Chris Wilson48bb74e2016-08-15 10:49:04 +01001374 err = i915_vma_pin(vma, 0, PAGE_SIZE, PIN_GLOBAL | PIN_HIGH);
1375 if (err)
1376 goto err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001377
Chris Wilson48bb74e2016-08-15 10:49:04 +01001378 engine->wa_ctx.vma = vma;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001379 return 0;
Chris Wilson48bb74e2016-08-15 10:49:04 +01001380
1381err:
1382 i915_gem_object_put(obj);
1383 return err;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001384}
1385
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001386static void lrc_destroy_wa_ctx(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001387{
Chris Wilson19880c42016-08-15 10:49:05 +01001388 i915_vma_unpin_and_release(&engine->wa_ctx.vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001389}
1390
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001391typedef u32 *(*wa_bb_func_t)(struct intel_engine_cs *engine, u32 *batch);
1392
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001393static int intel_init_workaround_bb(struct intel_engine_cs *engine)
Arun Siluvery17ee9502015-06-19 19:07:01 +01001394{
Chris Wilson48bb74e2016-08-15 10:49:04 +01001395 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001396 struct i915_wa_ctx_bb *wa_bb[2] = { &wa_ctx->indirect_ctx,
1397 &wa_ctx->per_ctx };
1398 wa_bb_func_t wa_bb_fn[2];
Arun Siluvery17ee9502015-06-19 19:07:01 +01001399 struct page *page;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001400 void *batch, *batch_ptr;
1401 unsigned int i;
Chris Wilson48bb74e2016-08-15 10:49:04 +01001402 int ret;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001403
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001404 if (WARN_ON(engine->id != RCS || !engine->scratch))
1405 return -EINVAL;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001406
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001407 switch (INTEL_GEN(engine->i915)) {
Rodrigo Vivi90007bc2017-08-15 16:16:48 -07001408 case 10:
1409 return 0;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001410 case 9:
1411 wa_bb_fn[0] = gen9_init_indirectctx_bb;
Chris Wilsonb8aa2232017-09-21 14:54:44 +01001412 wa_bb_fn[1] = NULL;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001413 break;
1414 case 8:
1415 wa_bb_fn[0] = gen8_init_indirectctx_bb;
Chris Wilson3ad7b522017-10-03 21:34:49 +01001416 wa_bb_fn[1] = NULL;
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001417 break;
1418 default:
1419 MISSING_CASE(INTEL_GEN(engine->i915));
Arun Siluvery5e60d792015-06-23 15:50:44 +01001420 return 0;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001421 }
Arun Siluvery5e60d792015-06-23 15:50:44 +01001422
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001423 ret = lrc_setup_wa_ctx(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001424 if (ret) {
1425 DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
1426 return ret;
1427 }
1428
Chris Wilson48bb74e2016-08-15 10:49:04 +01001429 page = i915_gem_object_get_dirty_page(wa_ctx->vma->obj, 0);
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001430 batch = batch_ptr = kmap_atomic(page);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001431
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001432 /*
1433 * Emit the two workaround batch buffers, recording the offset from the
1434 * start of the workaround batch buffer object for each and their
1435 * respective sizes.
1436 */
1437 for (i = 0; i < ARRAY_SIZE(wa_bb_fn); i++) {
1438 wa_bb[i]->offset = batch_ptr - batch;
1439 if (WARN_ON(!IS_ALIGNED(wa_bb[i]->offset, CACHELINE_BYTES))) {
1440 ret = -EINVAL;
1441 break;
1442 }
Chris Wilson604a8f62017-09-21 14:54:43 +01001443 if (wa_bb_fn[i])
1444 batch_ptr = wa_bb_fn[i](engine, batch_ptr);
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001445 wa_bb[i]->size = batch_ptr - (batch + wa_bb[i]->offset);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001446 }
1447
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001448 BUG_ON(batch_ptr - batch > CTX_WA_BB_OBJ_SIZE);
1449
Arun Siluvery17ee9502015-06-19 19:07:01 +01001450 kunmap_atomic(batch);
1451 if (ret)
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001452 lrc_destroy_wa_ctx(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001453
1454 return ret;
1455}
1456
Chris Wilson64f09f02017-08-07 13:19:19 +01001457static u8 gtiir[] = {
1458 [RCS] = 0,
1459 [BCS] = 0,
1460 [VCS] = 1,
1461 [VCS2] = 1,
1462 [VECS] = 3,
1463};
1464
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001465static int gen8_init_common_ring(struct intel_engine_cs *engine)
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001466{
Chris Wilsonc0336662016-05-06 15:40:21 +01001467 struct drm_i915_private *dev_priv = engine->i915;
Mika Kuoppalab620e872017-09-22 15:43:03 +03001468 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001469 int ret;
1470
1471 ret = intel_mocs_init_engine(engine);
1472 if (ret)
1473 return ret;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001474
Chris Wilsonad07dfc2016-10-07 07:53:26 +01001475 intel_engine_reset_breadcrumbs(engine);
Chris Wilsonf3b8f912017-01-05 15:30:21 +00001476 intel_engine_init_hangcheck(engine);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001477
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001478 I915_WRITE(RING_HWSTAM(engine->mmio_base), 0xffffffff);
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001479 I915_WRITE(RING_MODE_GEN7(engine),
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001480 _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
Chris Wilsonf3b8f912017-01-05 15:30:21 +00001481 I915_WRITE(RING_HWS_PGA(engine->mmio_base),
1482 engine->status_page.ggtt_offset);
1483 POSTING_READ(RING_HWS_PGA(engine->mmio_base));
Michel Thierrydfc53c52015-09-28 13:25:12 +01001484
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001485 DRM_DEBUG_DRIVER("Execlists enabled for %s\n", engine->name);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001486
Chris Wilson64f09f02017-08-07 13:19:19 +01001487 GEM_BUG_ON(engine->id >= ARRAY_SIZE(gtiir));
1488
1489 /*
1490 * Clear any pending interrupt state.
1491 *
1492 * We do it twice out of paranoia that some of the IIR are double
1493 * buffered, and if we only reset it once there may still be
1494 * an interrupt pending.
1495 */
1496 I915_WRITE(GEN8_GT_IIR(gtiir[engine->id]),
1497 GT_CONTEXT_SWITCH_INTERRUPT << engine->irq_shift);
1498 I915_WRITE(GEN8_GT_IIR(gtiir[engine->id]),
1499 GT_CONTEXT_SWITCH_INTERRUPT << engine->irq_shift);
Chris Wilsonf7470262017-01-24 15:20:21 +00001500 clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
Mika Kuoppalab620e872017-09-22 15:43:03 +03001501 execlists->csb_head = -1;
Chris Wilson4a118ec2017-10-23 22:32:36 +01001502 execlists->active = 0;
Chris Wilson6b764a52017-04-25 11:38:35 +01001503
Chris Wilson64f09f02017-08-07 13:19:19 +01001504 /* After a GPU reset, we may have requests to replay */
Michał Winiarski9bdc3572017-10-25 18:25:19 +01001505 if (execlists->first)
Mika Kuoppalab620e872017-09-22 15:43:03 +03001506 tasklet_schedule(&execlists->irq_tasklet);
Chris Wilson6b764a52017-04-25 11:38:35 +01001507
Chris Wilson821ed7d2016-09-09 14:11:53 +01001508 return 0;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001509}
1510
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001511static int gen8_init_render_ring(struct intel_engine_cs *engine)
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001512{
Chris Wilsonc0336662016-05-06 15:40:21 +01001513 struct drm_i915_private *dev_priv = engine->i915;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001514 int ret;
1515
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001516 ret = gen8_init_common_ring(engine);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001517 if (ret)
1518 return ret;
1519
1520 /* We need to disable the AsyncFlip performance optimisations in order
1521 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1522 * programmed to '1' on all products.
1523 *
1524 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
1525 */
1526 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1527
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001528 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1529
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001530 return init_workarounds_ring(engine);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001531}
1532
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001533static int gen9_init_render_ring(struct intel_engine_cs *engine)
Damien Lespiau82ef8222015-02-09 19:33:08 +00001534{
1535 int ret;
1536
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001537 ret = gen8_init_common_ring(engine);
Damien Lespiau82ef8222015-02-09 19:33:08 +00001538 if (ret)
1539 return ret;
1540
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001541 return init_workarounds_ring(engine);
Damien Lespiau82ef8222015-02-09 19:33:08 +00001542}
1543
Chris Wilson821ed7d2016-09-09 14:11:53 +01001544static void reset_common_ring(struct intel_engine_cs *engine,
1545 struct drm_i915_gem_request *request)
1546{
Mika Kuoppalab620e872017-09-22 15:43:03 +03001547 struct intel_engine_execlists * const execlists = &engine->execlists;
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001548 struct intel_context *ce;
Chris Wilson221ab97192017-09-16 21:44:14 +01001549 unsigned long flags;
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001550
Chris Wilson221ab97192017-09-16 21:44:14 +01001551 spin_lock_irqsave(&engine->timeline->lock, flags);
1552
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001553 /*
1554 * Catch up with any missed context-switch interrupts.
1555 *
1556 * Ideally we would just read the remaining CSB entries now that we
1557 * know the gpu is idle. However, the CSB registers are sometimes^W
1558 * often trashed across a GPU reset! Instead we have to rely on
1559 * guessing the missed context-switch events by looking at what
1560 * requests were completed.
1561 */
Michał Winiarskia4598d12017-10-25 22:00:18 +02001562 execlists_cancel_port_requests(execlists);
Chris Wilson221ab97192017-09-16 21:44:14 +01001563
1564 /* Push back any incomplete requests for replay after the reset. */
Michał Winiarskia4598d12017-10-25 22:00:18 +02001565 __unwind_incomplete_requests(engine);
Chris Wilsoncdb6ded2017-07-21 13:32:22 +01001566
Chris Wilson221ab97192017-09-16 21:44:14 +01001567 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001568
1569 /* If the request was innocent, we leave the request in the ELSP
1570 * and will try to replay it on restarting. The context image may
1571 * have been corrupted by the reset, in which case we may have
1572 * to service a new GPU hang, but more likely we can continue on
1573 * without impact.
1574 *
1575 * If the request was guilty, we presume the context is corrupt
1576 * and have to at least restore the RING register in the context
1577 * image back to the expected values to skip over the guilty request.
1578 */
Chris Wilson221ab97192017-09-16 21:44:14 +01001579 if (!request || request->fence.error != -EIO)
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001580 return;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001581
Chris Wilsona3aabe82016-10-04 21:11:26 +01001582 /* We want a simple context + ring to execute the breadcrumb update.
1583 * We cannot rely on the context being intact across the GPU hang,
1584 * so clear it and rebuild just what we need for the breadcrumb.
1585 * All pending requests for this context will be zapped, and any
1586 * future request will be after userspace has had the opportunity
1587 * to recreate its own state.
1588 */
Chris Wilsonc0dcb202017-02-07 15:24:37 +00001589 ce = &request->ctx->engine[engine->id];
Chris Wilsona3aabe82016-10-04 21:11:26 +01001590 execlists_init_reg_state(ce->lrc_reg_state,
1591 request->ctx, engine, ce->ring);
1592
Chris Wilson821ed7d2016-09-09 14:11:53 +01001593 /* Move the RING_HEAD onto the breadcrumb, past the hanging batch */
Chris Wilsona3aabe82016-10-04 21:11:26 +01001594 ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
1595 i915_ggtt_offset(ce->ring->vma);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001596 ce->lrc_reg_state[CTX_RING_HEAD+1] = request->postfix;
Chris Wilsona3aabe82016-10-04 21:11:26 +01001597
Chris Wilson821ed7d2016-09-09 14:11:53 +01001598 request->ring->head = request->postfix;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001599 intel_ring_update_space(request->ring);
1600
Chris Wilsona3aabe82016-10-04 21:11:26 +01001601 /* Reset WaIdleLiteRestore:bdw,skl as well */
Chris Wilson7e4992a2017-09-28 20:38:59 +01001602 unwind_wa_tail(request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01001603}
1604
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001605static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request *req)
1606{
1607 struct i915_hw_ppgtt *ppgtt = req->ctx->ppgtt;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001608 struct intel_engine_cs *engine = req->engine;
Mika Kuoppalae7167762017-02-28 17:28:10 +02001609 const int num_lri_cmds = GEN8_3LVL_PDPES * 2;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001610 u32 *cs;
1611 int i;
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001612
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001613 cs = intel_ring_begin(req, num_lri_cmds * 2 + 2);
1614 if (IS_ERR(cs))
1615 return PTR_ERR(cs);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001616
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001617 *cs++ = MI_LOAD_REGISTER_IMM(num_lri_cmds);
Mika Kuoppalae7167762017-02-28 17:28:10 +02001618 for (i = GEN8_3LVL_PDPES - 1; i >= 0; i--) {
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001619 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
1620
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001621 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, i));
1622 *cs++ = upper_32_bits(pd_daddr);
1623 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, i));
1624 *cs++ = lower_32_bits(pd_daddr);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001625 }
1626
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001627 *cs++ = MI_NOOP;
1628 intel_ring_advance(req, cs);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001629
1630 return 0;
1631}
1632
John Harrisonbe795fc2015-05-29 17:44:03 +01001633static int gen8_emit_bb_start(struct drm_i915_gem_request *req,
Chris Wilson803688b2016-08-02 22:50:27 +01001634 u64 offset, u32 len,
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001635 const unsigned int flags)
Oscar Mateo15648582014-07-24 17:04:32 +01001636{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001637 u32 *cs;
Oscar Mateo15648582014-07-24 17:04:32 +01001638 int ret;
1639
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001640 /* Don't rely in hw updating PDPs, specially in lite-restore.
1641 * Ideally, we should set Force PD Restore in ctx descriptor,
1642 * but we can't. Force Restore would be a second option, but
1643 * it is unsafe in case of lite-restore (because the ctx is
Michel Thierry2dba3232015-07-30 11:06:23 +01001644 * not idle). PML4 is allocated during ppgtt init so this is
1645 * not needed in 48-bit.*/
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001646 if (req->ctx->ppgtt &&
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001647 (intel_engine_flag(req->engine) & req->ctx->ppgtt->pd_dirty_rings) &&
1648 !i915_vm_is_48bit(&req->ctx->ppgtt->base) &&
1649 !intel_vgpu_active(req->i915)) {
1650 ret = intel_logical_ring_emit_pdps(req);
1651 if (ret)
1652 return ret;
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001653
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00001654 req->ctx->ppgtt->pd_dirty_rings &= ~intel_engine_flag(req->engine);
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001655 }
1656
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001657 cs = intel_ring_begin(req, 4);
1658 if (IS_ERR(cs))
1659 return PTR_ERR(cs);
Oscar Mateo15648582014-07-24 17:04:32 +01001660
Chris Wilson279f5a02017-10-05 20:10:05 +01001661 /*
1662 * WaDisableCtxRestoreArbitration:bdw,chv
1663 *
1664 * We don't need to perform MI_ARB_ENABLE as often as we do (in
1665 * particular all the gen that do not need the w/a at all!), if we
1666 * took care to make sure that on every switch into this context
1667 * (both ordinary and for preemption) that arbitrartion was enabled
1668 * we would be fine. However, there doesn't seem to be a downside to
1669 * being paranoid and making sure it is set before each batch and
1670 * every context-switch.
1671 *
1672 * Note that if we fail to enable arbitration before the request
1673 * is complete, then we do not see the context-switch interrupt and
1674 * the engine hangs (with RING_HEAD == RING_TAIL).
1675 *
1676 * That satisfies both the GPGPU w/a and our heavy-handed paranoia.
1677 */
Chris Wilson3ad7b522017-10-03 21:34:49 +01001678 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1679
Oscar Mateo15648582014-07-24 17:04:32 +01001680 /* FIXME(BDW): Address space and security selectors. */
Mika Kuoppala54af56d2017-02-28 17:28:08 +02001681 *cs++ = MI_BATCH_BUFFER_START_GEN8 |
1682 (flags & I915_DISPATCH_SECURE ? 0 : BIT(8)) |
1683 (flags & I915_DISPATCH_RS ? MI_BATCH_RESOURCE_STREAMER : 0);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001684 *cs++ = lower_32_bits(offset);
1685 *cs++ = upper_32_bits(offset);
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001686 intel_ring_advance(req, cs);
Oscar Mateo15648582014-07-24 17:04:32 +01001687
1688 return 0;
1689}
1690
Chris Wilson31bb59c2016-07-01 17:23:27 +01001691static void gen8_logical_ring_enable_irq(struct intel_engine_cs *engine)
Oscar Mateo73d477f2014-07-24 17:04:31 +01001692{
Chris Wilsonc0336662016-05-06 15:40:21 +01001693 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson31bb59c2016-07-01 17:23:27 +01001694 I915_WRITE_IMR(engine,
1695 ~(engine->irq_enable_mask | engine->irq_keep_mask));
1696 POSTING_READ_FW(RING_IMR(engine->mmio_base));
Oscar Mateo73d477f2014-07-24 17:04:31 +01001697}
1698
Chris Wilson31bb59c2016-07-01 17:23:27 +01001699static void gen8_logical_ring_disable_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, ~engine->irq_keep_mask);
Oscar Mateo73d477f2014-07-24 17:04:31 +01001703}
1704
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001705static int gen8_emit_flush(struct drm_i915_gem_request *request, u32 mode)
Oscar Mateo47122742014-07-24 17:04:28 +01001706{
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001707 u32 cmd, *cs;
Oscar Mateo47122742014-07-24 17:04:28 +01001708
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001709 cs = intel_ring_begin(request, 4);
1710 if (IS_ERR(cs))
1711 return PTR_ERR(cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001712
1713 cmd = MI_FLUSH_DW + 1;
1714
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001715 /* We always require a command barrier so that subsequent
1716 * commands, such as breadcrumb interrupts, are strictly ordered
1717 * wrt the contents of the write cache being flushed to memory
1718 * (and thus being coherent from the CPU).
1719 */
1720 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1721
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001722 if (mode & EMIT_INVALIDATE) {
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001723 cmd |= MI_INVALIDATE_TLB;
Chris Wilson1dae2df2016-08-02 22:50:19 +01001724 if (request->engine->id == VCS)
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001725 cmd |= MI_INVALIDATE_BSD;
Oscar Mateo47122742014-07-24 17:04:28 +01001726 }
1727
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001728 *cs++ = cmd;
1729 *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
1730 *cs++ = 0; /* upper addr */
1731 *cs++ = 0; /* value */
1732 intel_ring_advance(request, cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001733
1734 return 0;
1735}
1736
John Harrison7deb4d32015-05-29 17:43:59 +01001737static int gen8_emit_flush_render(struct drm_i915_gem_request *request,
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001738 u32 mode)
Oscar Mateo47122742014-07-24 17:04:28 +01001739{
Chris Wilsonb5321f32016-08-02 22:50:18 +01001740 struct intel_engine_cs *engine = request->engine;
Chris Wilsonbde13eb2016-08-15 10:49:07 +01001741 u32 scratch_addr =
1742 i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001743 bool vf_flush_wa = false, dc_flush_wa = false;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001744 u32 *cs, flags = 0;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001745 int len;
Oscar Mateo47122742014-07-24 17:04:28 +01001746
1747 flags |= PIPE_CONTROL_CS_STALL;
1748
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001749 if (mode & EMIT_FLUSH) {
Oscar Mateo47122742014-07-24 17:04:28 +01001750 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
1751 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
Francisco Jerez965fd602016-01-13 18:59:39 -08001752 flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
Chris Wilson40a24482015-08-21 16:08:41 +01001753 flags |= PIPE_CONTROL_FLUSH_ENABLE;
Oscar Mateo47122742014-07-24 17:04:28 +01001754 }
1755
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001756 if (mode & EMIT_INVALIDATE) {
Oscar Mateo47122742014-07-24 17:04:28 +01001757 flags |= PIPE_CONTROL_TLB_INVALIDATE;
1758 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
1759 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
1760 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
1761 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
1762 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
1763 flags |= PIPE_CONTROL_QW_WRITE;
1764 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
Oscar Mateo47122742014-07-24 17:04:28 +01001765
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001766 /*
1767 * On GEN9: before VF_CACHE_INVALIDATE we need to emit a NULL
1768 * pipe control.
1769 */
Chris Wilsonc0336662016-05-06 15:40:21 +01001770 if (IS_GEN9(request->i915))
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001771 vf_flush_wa = true;
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001772
1773 /* WaForGAMHang:kbl */
1774 if (IS_KBL_REVID(request->i915, 0, KBL_REVID_B0))
1775 dc_flush_wa = true;
Ben Widawsky1a5a9ce2015-12-17 09:49:57 -08001776 }
Imre Deak9647ff32015-01-25 13:27:11 -08001777
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001778 len = 6;
1779
1780 if (vf_flush_wa)
1781 len += 6;
1782
1783 if (dc_flush_wa)
1784 len += 12;
1785
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001786 cs = intel_ring_begin(request, len);
1787 if (IS_ERR(cs))
1788 return PTR_ERR(cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001789
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001790 if (vf_flush_wa)
1791 cs = gen8_emit_pipe_control(cs, 0, 0);
Imre Deak9647ff32015-01-25 13:27:11 -08001792
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001793 if (dc_flush_wa)
1794 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_DC_FLUSH_ENABLE,
1795 0);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001796
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001797 cs = gen8_emit_pipe_control(cs, flags, scratch_addr);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001798
Tvrtko Ursulin9f235df2017-02-16 12:23:25 +00001799 if (dc_flush_wa)
1800 cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_CS_STALL, 0);
Mika Kuoppala0b2d0932016-06-07 17:19:10 +03001801
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001802 intel_ring_advance(request, cs);
Oscar Mateo47122742014-07-24 17:04:28 +01001803
1804 return 0;
1805}
1806
Chris Wilson7c17d372016-01-20 15:43:35 +02001807/*
1808 * Reserve space for 2 NOOPs at the end of each request to be
1809 * used as a workaround for not being allowed to do lite
1810 * restore with HEAD==TAIL (WaIdleLiteRestore).
1811 */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001812static void gen8_emit_wa_tail(struct drm_i915_gem_request *request, u32 *cs)
Oscar Mateo4da46e12014-07-24 17:04:27 +01001813{
Chris Wilsonbeecec92017-10-03 21:34:52 +01001814 /* Ensure there's always at least one preemption point per-request. */
1815 *cs++ = MI_ARB_CHECK;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001816 *cs++ = MI_NOOP;
1817 request->wa_tail = intel_ring_offset(request, cs);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001818}
Oscar Mateo4da46e12014-07-24 17:04:27 +01001819
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001820static void gen8_emit_breadcrumb(struct drm_i915_gem_request *request, u32 *cs)
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001821{
Chris Wilson7c17d372016-01-20 15:43:35 +02001822 /* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */
1823 BUILD_BUG_ON(I915_GEM_HWS_INDEX_ADDR & (1 << 5));
Oscar Mateo4da46e12014-07-24 17:04:27 +01001824
Michał Winiarskidf77cd82017-10-25 22:00:15 +02001825 cs = gen8_emit_ggtt_write(cs, request->global_seqno,
1826 intel_hws_seqno_address(request->engine));
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001827 *cs++ = MI_USER_INTERRUPT;
1828 *cs++ = MI_NOOP;
1829 request->tail = intel_ring_offset(request, cs);
Chris Wilsoned1501d2017-03-27 14:14:12 +01001830 assert_ring_tail_valid(request->ring, request->tail);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001831
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001832 gen8_emit_wa_tail(request, cs);
Chris Wilson7c17d372016-01-20 15:43:35 +02001833}
Chris Wilson98f29e82016-10-28 13:58:51 +01001834static const int gen8_emit_breadcrumb_sz = 6 + WA_TAIL_DWORDS;
1835
Michał Winiarskidf77cd82017-10-25 22:00:15 +02001836static void gen8_emit_breadcrumb_rcs(struct drm_i915_gem_request *request,
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001837 u32 *cs)
Chris Wilson7c17d372016-01-20 15:43:35 +02001838{
Michał Winiarskice81a652016-04-12 15:51:55 +02001839 /* We're using qword write, seqno should be aligned to 8 bytes. */
1840 BUILD_BUG_ON(I915_GEM_HWS_INDEX & 1);
1841
Michał Winiarskidf77cd82017-10-25 22:00:15 +02001842 cs = gen8_emit_ggtt_write_rcs(cs, request->global_seqno,
1843 intel_hws_seqno_address(request->engine));
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001844 *cs++ = MI_USER_INTERRUPT;
1845 *cs++ = MI_NOOP;
1846 request->tail = intel_ring_offset(request, cs);
Chris Wilsoned1501d2017-03-27 14:14:12 +01001847 assert_ring_tail_valid(request->ring, request->tail);
Chris Wilsoncaddfe72016-10-28 13:58:52 +01001848
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001849 gen8_emit_wa_tail(request, cs);
Oscar Mateo4da46e12014-07-24 17:04:27 +01001850}
Michał Winiarskidf77cd82017-10-25 22:00:15 +02001851static const int gen8_emit_breadcrumb_rcs_sz = 8 + WA_TAIL_DWORDS;
Chris Wilson98f29e82016-10-28 13:58:51 +01001852
John Harrison87531812015-05-29 17:43:44 +01001853static int gen8_init_rcs_context(struct drm_i915_gem_request *req)
Thomas Daniele7778be2014-12-02 12:50:48 +00001854{
1855 int ret;
1856
Tvrtko Ursulin4ac96592017-02-14 15:00:17 +00001857 ret = intel_ring_workarounds_emit(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001858 if (ret)
1859 return ret;
1860
Peter Antoine3bbaba02015-07-10 20:13:11 +03001861 ret = intel_rcs_context_init_mocs(req);
1862 /*
1863 * Failing to program the MOCS is non-fatal.The system will not
1864 * run at peak performance. So generate an error and carry on.
1865 */
1866 if (ret)
1867 DRM_ERROR("MOCS failed to program: expect performance issues.\n");
1868
Chris Wilson4e50f082016-10-28 13:58:31 +01001869 return i915_gem_render_state_emit(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001870}
1871
Oscar Mateo73e4d072014-07-24 17:04:48 +01001872/**
1873 * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001874 * @engine: Engine Command Streamer.
Oscar Mateo73e4d072014-07-24 17:04:48 +01001875 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001876void intel_logical_ring_cleanup(struct intel_engine_cs *engine)
Oscar Mateo454afeb2014-07-24 17:04:22 +01001877{
John Harrison6402c332014-10-31 12:00:26 +00001878 struct drm_i915_private *dev_priv;
Oscar Mateo9832b9d2014-07-24 17:04:30 +01001879
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01001880 /*
1881 * Tasklet cannot be active at this point due intel_mark_active/idle
1882 * so this is just for documentation.
1883 */
Mika Kuoppalab620e872017-09-22 15:43:03 +03001884 if (WARN_ON(test_bit(TASKLET_STATE_SCHED, &engine->execlists.irq_tasklet.state)))
1885 tasklet_kill(&engine->execlists.irq_tasklet);
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01001886
Chris Wilsonc0336662016-05-06 15:40:21 +01001887 dev_priv = engine->i915;
John Harrison6402c332014-10-31 12:00:26 +00001888
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001889 if (engine->buffer) {
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001890 WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
Dave Gordonb0366a52015-12-08 15:02:36 +00001891 }
Oscar Mateo48d82382014-07-24 17:04:23 +01001892
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001893 if (engine->cleanup)
1894 engine->cleanup(engine);
Oscar Mateo48d82382014-07-24 17:04:23 +01001895
Chris Wilsone8a9c582016-12-18 15:37:20 +00001896 intel_engine_cleanup_common(engine);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001897
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00001898 lrc_destroy_wa_ctx(engine);
Chris Wilsonc0336662016-05-06 15:40:21 +01001899 engine->i915 = NULL;
Akash Goel3b3f1652016-10-13 22:44:48 +05301900 dev_priv->engine[engine->id] = NULL;
1901 kfree(engine);
Oscar Mateo454afeb2014-07-24 17:04:22 +01001902}
1903
Chris Wilsonff44ad52017-03-16 17:13:03 +00001904static void execlists_set_default_submission(struct intel_engine_cs *engine)
Chris Wilsonddd66c52016-08-02 22:50:31 +01001905{
Chris Wilsonff44ad52017-03-16 17:13:03 +00001906 engine->submit_request = execlists_submit_request;
Chris Wilson27a5f612017-09-15 18:31:00 +01001907 engine->cancel_requests = execlists_cancel_requests;
Chris Wilsonff44ad52017-03-16 17:13:03 +00001908 engine->schedule = execlists_schedule;
Mika Kuoppalab620e872017-09-22 15:43:03 +03001909 engine->execlists.irq_tasklet.func = intel_lrc_irq_handler;
Chris Wilsonaba5e272017-10-25 15:39:41 +01001910
1911 engine->park = NULL;
1912 engine->unpark = NULL;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001913}
1914
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001915static void
Chris Wilsone1382ef2016-05-06 15:40:20 +01001916logical_ring_default_vfuncs(struct intel_engine_cs *engine)
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001917{
1918 /* Default vfuncs which can be overriden by each engine. */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001919 engine->init_hw = gen8_init_common_ring;
Chris Wilson821ed7d2016-09-09 14:11:53 +01001920 engine->reset_hw = reset_common_ring;
Chris Wilsone8a9c582016-12-18 15:37:20 +00001921
1922 engine->context_pin = execlists_context_pin;
1923 engine->context_unpin = execlists_context_unpin;
1924
Chris Wilsonf73e7392016-12-18 15:37:24 +00001925 engine->request_alloc = execlists_request_alloc;
1926
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001927 engine->emit_flush = gen8_emit_flush;
Chris Wilson9b81d552016-10-28 13:58:50 +01001928 engine->emit_breadcrumb = gen8_emit_breadcrumb;
Chris Wilson98f29e82016-10-28 13:58:51 +01001929 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_sz;
Chris Wilsonff44ad52017-03-16 17:13:03 +00001930
1931 engine->set_default_submission = execlists_set_default_submission;
Chris Wilsonddd66c52016-08-02 22:50:31 +01001932
Chris Wilson31bb59c2016-07-01 17:23:27 +01001933 engine->irq_enable = gen8_logical_ring_enable_irq;
1934 engine->irq_disable = gen8_logical_ring_disable_irq;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001935 engine->emit_bb_start = gen8_emit_bb_start;
Tvrtko Ursulinc9cacf92016-01-12 17:32:34 +00001936}
1937
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001938static inline void
Dave Gordonc2c7f242016-07-13 16:03:35 +01001939logical_ring_default_irqs(struct intel_engine_cs *engine)
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001940{
Dave Gordonc2c7f242016-07-13 16:03:35 +01001941 unsigned shift = engine->irq_shift;
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001942 engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << shift;
1943 engine->irq_keep_mask = GT_CONTEXT_SWITCH_INTERRUPT << shift;
Tvrtko Ursulind9f3af92016-01-12 17:32:35 +00001944}
1945
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001946static void
1947logical_ring_setup(struct intel_engine_cs *engine)
1948{
1949 struct drm_i915_private *dev_priv = engine->i915;
1950 enum forcewake_domains fw_domains;
1951
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01001952 intel_engine_setup_common(engine);
1953
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001954 /* Intentionally left blank. */
1955 engine->buffer = NULL;
1956
1957 fw_domains = intel_uncore_forcewake_for_reg(dev_priv,
1958 RING_ELSP(engine),
1959 FW_REG_WRITE);
1960
1961 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
1962 RING_CONTEXT_STATUS_PTR(engine),
1963 FW_REG_READ | FW_REG_WRITE);
1964
1965 fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
1966 RING_CONTEXT_STATUS_BUF_BASE(engine),
1967 FW_REG_READ);
1968
Mika Kuoppalab620e872017-09-22 15:43:03 +03001969 engine->execlists.fw_domains = fw_domains;
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001970
Mika Kuoppalab620e872017-09-22 15:43:03 +03001971 tasklet_init(&engine->execlists.irq_tasklet,
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001972 intel_lrc_irq_handler, (unsigned long)engine);
1973
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001974 logical_ring_default_vfuncs(engine);
1975 logical_ring_default_irqs(engine);
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001976}
1977
Daniele Ceraolo Spurio486e93f2017-09-13 09:56:02 +01001978static int logical_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001979{
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001980 int ret;
1981
Tvrtko Ursulin019bf272016-07-13 16:03:41 +01001982 ret = intel_engine_init_common(engine);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001983 if (ret)
1984 goto error;
1985
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001986 return 0;
1987
1988error:
1989 intel_logical_ring_cleanup(engine);
1990 return ret;
1991}
1992
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +01001993int logical_render_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01001994{
1995 struct drm_i915_private *dev_priv = engine->i915;
1996 int ret;
1997
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01001998 logical_ring_setup(engine);
1999
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002000 if (HAS_L3_DPF(dev_priv))
2001 engine->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
2002
2003 /* Override some for render ring. */
2004 if (INTEL_GEN(dev_priv) >= 9)
2005 engine->init_hw = gen9_init_render_ring;
2006 else
2007 engine->init_hw = gen8_init_render_ring;
2008 engine->init_context = gen8_init_rcs_context;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002009 engine->emit_flush = gen8_emit_flush_render;
Michał Winiarskidf77cd82017-10-25 22:00:15 +02002010 engine->emit_breadcrumb = gen8_emit_breadcrumb_rcs;
2011 engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_rcs_sz;
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002012
Chris Wilsonf51455d2017-01-10 14:47:34 +00002013 ret = intel_engine_create_scratch(engine, PAGE_SIZE);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002014 if (ret)
2015 return ret;
2016
2017 ret = intel_init_workaround_bb(engine);
2018 if (ret) {
2019 /*
2020 * We continue even if we fail to initialize WA batch
2021 * because we only expect rare glitches but nothing
2022 * critical to prevent us from using GPU
2023 */
2024 DRM_ERROR("WA batch buffer initialization failed: %d\n",
2025 ret);
2026 }
2027
Tvrtko Ursulind038fc72016-12-16 13:18:42 +00002028 return logical_ring_init(engine);
Tvrtko Ursulina19d6ff2016-06-23 14:52:41 +01002029}
2030
Tvrtko Ursulin88d2ba22016-07-13 16:03:40 +01002031int logical_xcs_ring_init(struct intel_engine_cs *engine)
Tvrtko Ursulinbb454382016-07-13 16:03:36 +01002032{
2033 logical_ring_setup(engine);
2034
2035 return logical_ring_init(engine);
2036}
2037
Jeff McGee0cea6502015-02-13 10:27:56 -06002038static u32
Chris Wilsonc0336662016-05-06 15:40:21 +01002039make_rpcs(struct drm_i915_private *dev_priv)
Jeff McGee0cea6502015-02-13 10:27:56 -06002040{
2041 u32 rpcs = 0;
2042
2043 /*
2044 * No explicit RPCS request is needed to ensure full
2045 * slice/subslice/EU enablement prior to Gen9.
2046 */
Chris Wilsonc0336662016-05-06 15:40:21 +01002047 if (INTEL_GEN(dev_priv) < 9)
Jeff McGee0cea6502015-02-13 10:27:56 -06002048 return 0;
2049
2050 /*
2051 * Starting in Gen9, render power gating can leave
2052 * slice/subslice/EU in a partially enabled state. We
2053 * must make an explicit request through RPCS for full
2054 * enablement.
2055 */
Imre Deak43b67992016-08-31 19:13:02 +03002056 if (INTEL_INFO(dev_priv)->sseu.has_slice_pg) {
Jeff McGee0cea6502015-02-13 10:27:56 -06002057 rpcs |= GEN8_RPCS_S_CNT_ENABLE;
Imre Deakf08a0c92016-08-31 19:13:04 +03002058 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.slice_mask) <<
Jeff McGee0cea6502015-02-13 10:27:56 -06002059 GEN8_RPCS_S_CNT_SHIFT;
2060 rpcs |= GEN8_RPCS_ENABLE;
2061 }
2062
Imre Deak43b67992016-08-31 19:13:02 +03002063 if (INTEL_INFO(dev_priv)->sseu.has_subslice_pg) {
Jeff McGee0cea6502015-02-13 10:27:56 -06002064 rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
Imre Deak57ec1712016-08-31 19:13:05 +03002065 rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.subslice_mask) <<
Jeff McGee0cea6502015-02-13 10:27:56 -06002066 GEN8_RPCS_SS_CNT_SHIFT;
2067 rpcs |= GEN8_RPCS_ENABLE;
2068 }
2069
Imre Deak43b67992016-08-31 19:13:02 +03002070 if (INTEL_INFO(dev_priv)->sseu.has_eu_pg) {
2071 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
Jeff McGee0cea6502015-02-13 10:27:56 -06002072 GEN8_RPCS_EU_MIN_SHIFT;
Imre Deak43b67992016-08-31 19:13:02 +03002073 rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
Jeff McGee0cea6502015-02-13 10:27:56 -06002074 GEN8_RPCS_EU_MAX_SHIFT;
2075 rpcs |= GEN8_RPCS_ENABLE;
2076 }
2077
2078 return rpcs;
2079}
2080
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002081static u32 intel_lr_indirect_ctx_offset(struct intel_engine_cs *engine)
Michel Thierry71562912016-02-23 10:31:49 +00002082{
2083 u32 indirect_ctx_offset;
2084
Chris Wilsonc0336662016-05-06 15:40:21 +01002085 switch (INTEL_GEN(engine->i915)) {
Michel Thierry71562912016-02-23 10:31:49 +00002086 default:
Chris Wilsonc0336662016-05-06 15:40:21 +01002087 MISSING_CASE(INTEL_GEN(engine->i915));
Michel Thierry71562912016-02-23 10:31:49 +00002088 /* fall through */
Michel Thierry7bd0a2c2017-06-06 13:30:38 -07002089 case 10:
2090 indirect_ctx_offset =
2091 GEN10_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2092 break;
Michel Thierry71562912016-02-23 10:31:49 +00002093 case 9:
2094 indirect_ctx_offset =
2095 GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2096 break;
2097 case 8:
2098 indirect_ctx_offset =
2099 GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2100 break;
2101 }
2102
2103 return indirect_ctx_offset;
2104}
2105
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002106static void execlists_init_reg_state(u32 *regs,
Chris Wilsona3aabe82016-10-04 21:11:26 +01002107 struct i915_gem_context *ctx,
2108 struct intel_engine_cs *engine,
2109 struct intel_ring *ring)
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002110{
Chris Wilsona3aabe82016-10-04 21:11:26 +01002111 struct drm_i915_private *dev_priv = engine->i915;
2112 struct i915_hw_ppgtt *ppgtt = ctx->ppgtt ?: dev_priv->mm.aliasing_ppgtt;
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002113 u32 base = engine->mmio_base;
2114 bool rcs = engine->id == RCS;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002115
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002116 /* A context is actually a big batch buffer with several
2117 * MI_LOAD_REGISTER_IMM commands followed by (reg, value) pairs. The
2118 * values we are setting here are only for the first context restore:
2119 * on a subsequent save, the GPU will recreate this batchbuffer with new
2120 * values (including all the missing MI_LOAD_REGISTER_IMM commands that
2121 * we are not initializing here).
2122 */
2123 regs[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(rcs ? 14 : 11) |
2124 MI_LRI_FORCE_POSTED;
2125
2126 CTX_REG(regs, CTX_CONTEXT_CONTROL, RING_CONTEXT_CONTROL(engine),
2127 _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002128 (HAS_RESOURCE_STREAMER(dev_priv) ?
2129 CTX_CTRL_RS_CTX_ENABLE : 0)));
2130 CTX_REG(regs, CTX_RING_HEAD, RING_HEAD(base), 0);
2131 CTX_REG(regs, CTX_RING_TAIL, RING_TAIL(base), 0);
2132 CTX_REG(regs, CTX_RING_BUFFER_START, RING_START(base), 0);
2133 CTX_REG(regs, CTX_RING_BUFFER_CONTROL, RING_CTL(base),
2134 RING_CTL_SIZE(ring->size) | RING_VALID);
2135 CTX_REG(regs, CTX_BB_HEAD_U, RING_BBADDR_UDW(base), 0);
2136 CTX_REG(regs, CTX_BB_HEAD_L, RING_BBADDR(base), 0);
2137 CTX_REG(regs, CTX_BB_STATE, RING_BBSTATE(base), RING_BB_PPGTT);
2138 CTX_REG(regs, CTX_SECOND_BB_HEAD_U, RING_SBBADDR_UDW(base), 0);
2139 CTX_REG(regs, CTX_SECOND_BB_HEAD_L, RING_SBBADDR(base), 0);
2140 CTX_REG(regs, CTX_SECOND_BB_STATE, RING_SBBSTATE(base), 0);
2141 if (rcs) {
Chris Wilson604a8f62017-09-21 14:54:43 +01002142 struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
2143
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002144 CTX_REG(regs, CTX_RCS_INDIRECT_CTX, RING_INDIRECT_CTX(base), 0);
2145 CTX_REG(regs, CTX_RCS_INDIRECT_CTX_OFFSET,
2146 RING_INDIRECT_CTX_OFFSET(base), 0);
Chris Wilson604a8f62017-09-21 14:54:43 +01002147 if (wa_ctx->indirect_ctx.size) {
Chris Wilsonbde13eb2016-08-15 10:49:07 +01002148 u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01002149
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002150 regs[CTX_RCS_INDIRECT_CTX + 1] =
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00002151 (ggtt_offset + wa_ctx->indirect_ctx.offset) |
2152 (wa_ctx->indirect_ctx.size / CACHELINE_BYTES);
Arun Siluvery17ee9502015-06-19 19:07:01 +01002153
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002154 regs[CTX_RCS_INDIRECT_CTX_OFFSET + 1] =
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002155 intel_lr_indirect_ctx_offset(engine) << 6;
Chris Wilson604a8f62017-09-21 14:54:43 +01002156 }
2157
2158 CTX_REG(regs, CTX_BB_PER_CTX_PTR, RING_BB_PER_CTX_PTR(base), 0);
2159 if (wa_ctx->per_ctx.size) {
2160 u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
Arun Siluvery17ee9502015-06-19 19:07:01 +01002161
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002162 regs[CTX_BB_PER_CTX_PTR + 1] =
Tvrtko Ursulin097d4f12017-02-17 07:58:59 +00002163 (ggtt_offset + wa_ctx->per_ctx.offset) | 0x01;
Arun Siluvery17ee9502015-06-19 19:07:01 +01002164 }
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002165 }
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002166
2167 regs[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9) | MI_LRI_FORCE_POSTED;
2168
2169 CTX_REG(regs, CTX_CTX_TIMESTAMP, RING_CTX_TIMESTAMP(base), 0);
Ville Syrjälä0d925ea2015-11-04 23:20:11 +02002170 /* PDP values well be assigned later if needed */
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002171 CTX_REG(regs, CTX_PDP3_UDW, GEN8_RING_PDP_UDW(engine, 3), 0);
2172 CTX_REG(regs, CTX_PDP3_LDW, GEN8_RING_PDP_LDW(engine, 3), 0);
2173 CTX_REG(regs, CTX_PDP2_UDW, GEN8_RING_PDP_UDW(engine, 2), 0);
2174 CTX_REG(regs, CTX_PDP2_LDW, GEN8_RING_PDP_LDW(engine, 2), 0);
2175 CTX_REG(regs, CTX_PDP1_UDW, GEN8_RING_PDP_UDW(engine, 1), 0);
2176 CTX_REG(regs, CTX_PDP1_LDW, GEN8_RING_PDP_LDW(engine, 1), 0);
2177 CTX_REG(regs, CTX_PDP0_UDW, GEN8_RING_PDP_UDW(engine, 0), 0);
2178 CTX_REG(regs, CTX_PDP0_LDW, GEN8_RING_PDP_LDW(engine, 0), 0);
Michel Thierryd7b26332015-04-08 12:13:34 +01002179
Chris Wilson949e8ab2017-02-09 14:40:36 +00002180 if (ppgtt && i915_vm_is_48bit(&ppgtt->base)) {
Michel Thierry2dba3232015-07-30 11:06:23 +01002181 /* 64b PPGTT (48bit canonical)
2182 * PDP0_DESCRIPTOR contains the base address to PML4 and
2183 * other PDP Descriptors are ignored.
2184 */
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002185 ASSIGN_CTX_PML4(ppgtt, regs);
Michel Thierry2dba3232015-07-30 11:06:23 +01002186 }
2187
Tvrtko Ursulin56e51bf2017-02-21 09:58:39 +00002188 if (rcs) {
2189 regs[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
2190 CTX_REG(regs, CTX_R_PWR_CLK_STATE, GEN8_R_PWR_CLK_STATE,
2191 make_rpcs(dev_priv));
Robert Bragg19f81df2017-06-13 12:23:03 +01002192
2193 i915_oa_init_reg_state(engine, ctx, regs);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002194 }
Chris Wilsona3aabe82016-10-04 21:11:26 +01002195}
2196
2197static int
2198populate_lr_context(struct i915_gem_context *ctx,
2199 struct drm_i915_gem_object *ctx_obj,
2200 struct intel_engine_cs *engine,
2201 struct intel_ring *ring)
2202{
2203 void *vaddr;
Chris Wilsond2b4b972017-11-10 14:26:33 +00002204 u32 *regs;
Chris Wilsona3aabe82016-10-04 21:11:26 +01002205 int ret;
2206
2207 ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
2208 if (ret) {
2209 DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
2210 return ret;
2211 }
2212
2213 vaddr = i915_gem_object_pin_map(ctx_obj, I915_MAP_WB);
2214 if (IS_ERR(vaddr)) {
2215 ret = PTR_ERR(vaddr);
2216 DRM_DEBUG_DRIVER("Could not map object pages! (%d)\n", ret);
2217 return ret;
2218 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002219 ctx_obj->mm.dirty = true;
Chris Wilsona3aabe82016-10-04 21:11:26 +01002220
Chris Wilsond2b4b972017-11-10 14:26:33 +00002221 if (engine->default_state) {
2222 /*
2223 * We only want to copy over the template context state;
2224 * skipping over the headers reserved for GuC communication,
2225 * leaving those as zero.
2226 */
2227 const unsigned long start = LRC_HEADER_PAGES * PAGE_SIZE;
2228 void *defaults;
2229
2230 defaults = i915_gem_object_pin_map(engine->default_state,
2231 I915_MAP_WB);
2232 if (IS_ERR(defaults))
2233 return PTR_ERR(defaults);
2234
2235 memcpy(vaddr + start, defaults + start, engine->context_size);
2236 i915_gem_object_unpin_map(engine->default_state);
2237 }
2238
Chris Wilsona3aabe82016-10-04 21:11:26 +01002239 /* The second page of the context object contains some fields which must
2240 * be set up prior to the first execution. */
Chris Wilsond2b4b972017-11-10 14:26:33 +00002241 regs = vaddr + LRC_STATE_PN * PAGE_SIZE;
2242 execlists_init_reg_state(regs, ctx, engine, ring);
2243 if (!engine->default_state)
2244 regs[CTX_CONTEXT_CONTROL + 1] |=
2245 _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002246
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01002247 i915_gem_object_unpin_map(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002248
2249 return 0;
2250}
2251
Chris Wilsone2efd132016-05-24 14:53:34 +01002252static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
Chris Wilson978f1e02016-04-28 09:56:54 +01002253 struct intel_engine_cs *engine)
Oscar Mateoede7d422014-07-24 17:04:12 +01002254{
Oscar Mateo8c8579172014-07-24 17:04:14 +01002255 struct drm_i915_gem_object *ctx_obj;
Chris Wilson9021ad02016-05-24 14:53:37 +01002256 struct intel_context *ce = &ctx->engine[engine->id];
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002257 struct i915_vma *vma;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002258 uint32_t context_size;
Chris Wilson7e37f882016-08-02 22:50:21 +01002259 struct intel_ring *ring;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002260 int ret;
2261
Chris Wilson9021ad02016-05-24 14:53:37 +01002262 WARN_ON(ce->state);
Oscar Mateoede7d422014-07-24 17:04:12 +01002263
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +03002264 context_size = round_up(engine->context_size, I915_GTT_PAGE_SIZE);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002265
Michel Thierry0b29c752017-09-13 09:56:00 +01002266 /*
2267 * Before the actual start of the context image, we insert a few pages
2268 * for our own use and for sharing with the GuC.
2269 */
2270 context_size += LRC_HEADER_PAGES * PAGE_SIZE;
Alex Daid1675192015-08-12 15:43:43 +01002271
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00002272 ctx_obj = i915_gem_object_create(ctx->i915, context_size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01002273 if (IS_ERR(ctx_obj)) {
Dan Carpenter3126a662015-04-30 17:30:50 +03002274 DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
Chris Wilsonfe3db792016-04-25 13:32:13 +01002275 return PTR_ERR(ctx_obj);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002276 }
2277
Chris Wilsona01cb372017-01-16 15:21:30 +00002278 vma = i915_vma_instance(ctx_obj, &ctx->i915->ggtt.base, NULL);
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002279 if (IS_ERR(vma)) {
2280 ret = PTR_ERR(vma);
2281 goto error_deref_obj;
2282 }
2283
Chris Wilson7e37f882016-08-02 22:50:21 +01002284 ring = intel_engine_create_ring(engine, ctx->ring_size);
Chris Wilsondca33ec2016-08-02 22:50:20 +01002285 if (IS_ERR(ring)) {
2286 ret = PTR_ERR(ring);
Nick Hoathe84fe802015-09-11 12:53:46 +01002287 goto error_deref_obj;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002288 }
2289
Chris Wilsondca33ec2016-08-02 22:50:20 +01002290 ret = populate_lr_context(ctx, ctx_obj, engine, ring);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002291 if (ret) {
2292 DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
Chris Wilsondca33ec2016-08-02 22:50:20 +01002293 goto error_ring_free;
Oscar Mateo84c23772014-07-24 17:04:15 +01002294 }
2295
Chris Wilsondca33ec2016-08-02 22:50:20 +01002296 ce->ring = ring;
Chris Wilsonbf3783e2016-08-15 10:48:54 +01002297 ce->state = vma;
Oscar Mateoede7d422014-07-24 17:04:12 +01002298
2299 return 0;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002300
Chris Wilsondca33ec2016-08-02 22:50:20 +01002301error_ring_free:
Chris Wilson7e37f882016-08-02 22:50:21 +01002302 intel_ring_free(ring);
Nick Hoathe84fe802015-09-11 12:53:46 +01002303error_deref_obj:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01002304 i915_gem_object_put(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002305 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01002306}
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002307
Chris Wilson821ed7d2016-09-09 14:11:53 +01002308void intel_lr_context_resume(struct drm_i915_private *dev_priv)
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002309{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002310 struct intel_engine_cs *engine;
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002311 struct i915_gem_context *ctx;
Akash Goel3b3f1652016-10-13 22:44:48 +05302312 enum intel_engine_id id;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002313
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002314 /* Because we emit WA_TAIL_DWORDS there may be a disparity
2315 * between our bookkeeping in ce->ring->head and ce->ring->tail and
2316 * that stored in context. As we only write new commands from
2317 * ce->ring->tail onwards, everything before that is junk. If the GPU
2318 * starts reading from its RING_HEAD from the context, it may try to
2319 * execute that junk and die.
2320 *
2321 * So to avoid that we reset the context images upon resume. For
2322 * simplicity, we just zero everything out.
2323 */
Chris Wilson829a0af2017-06-20 12:05:45 +01002324 list_for_each_entry(ctx, &dev_priv->contexts.list, link) {
Akash Goel3b3f1652016-10-13 22:44:48 +05302325 for_each_engine(engine, dev_priv, id) {
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002326 struct intel_context *ce = &ctx->engine[engine->id];
2327 u32 *reg;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002328
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002329 if (!ce->state)
2330 continue;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002331
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002332 reg = i915_gem_object_pin_map(ce->state->obj,
2333 I915_MAP_WB);
2334 if (WARN_ON(IS_ERR(reg)))
2335 continue;
Tvrtko Ursulin7d774ca2016-04-12 15:40:42 +01002336
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002337 reg += LRC_STATE_PN * PAGE_SIZE / sizeof(*reg);
2338 reg[CTX_RING_HEAD+1] = 0;
2339 reg[CTX_RING_TAIL+1] = 0;
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002340
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002341 ce->state->obj->mm.dirty = true;
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002342 i915_gem_object_unpin_map(ce->state->obj);
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002343
Chris Wilsone6ba9992017-04-25 14:00:49 +01002344 intel_ring_reset(ce->ring, 0);
Chris Wilsonbafb2f72016-09-21 14:51:08 +01002345 }
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002346 }
2347}