Commit | Line | Data |
---|---|---|
b20385f1 OM |
1 | /* |
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 | ||
73e4d07f OM |
31 | /** |
32 | * DOC: Logical Rings, Logical Ring Contexts and Execlists | |
33 | * | |
34 | * Motivation: | |
b20385f1 OM |
35 | * 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 | * | |
73e4d07f OM |
39 | * 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: | |
b20385f1 OM |
90 | * Execlists are the new method by which, on gen8+ hardware, workloads are |
91 | * submitted for execution (as opposed to the legacy, ringbuffer-based, method). | |
73e4d07f OM |
92 | * 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 | * | |
b20385f1 | 133 | */ |
27af5eea | 134 | #include <linux/interrupt.h> |
b20385f1 OM |
135 | |
136 | #include <drm/drmP.h> | |
137 | #include <drm/i915_drm.h> | |
138 | #include "i915_drv.h" | |
7c2fa7fa | 139 | #include "i915_gem_render_state.h" |
3bbaba0c | 140 | #include "intel_mocs.h" |
127f1003 | 141 | |
e981e7b1 TD |
142 | #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) | |
8670d6f9 | 155 | |
70c2a24d | 156 | #define GEN8_CTX_STATUS_COMPLETED_MASK \ |
d8747afb | 157 | (GEN8_CTX_STATUS_COMPLETE | GEN8_CTX_STATUS_PREEMPTED) |
70c2a24d | 158 | |
8670d6f9 OM |
159 | #define CTX_LRI_HEADER_0 0x01 |
160 | #define CTX_CONTEXT_CONTROL 0x02 | |
161 | #define CTX_RING_HEAD 0x04 | |
162 | #define CTX_RING_TAIL 0x06 | |
163 | #define CTX_RING_BUFFER_START 0x08 | |
164 | #define CTX_RING_BUFFER_CONTROL 0x0a | |
165 | #define CTX_BB_HEAD_U 0x0c | |
166 | #define CTX_BB_HEAD_L 0x0e | |
167 | #define CTX_BB_STATE 0x10 | |
168 | #define CTX_SECOND_BB_HEAD_U 0x12 | |
169 | #define CTX_SECOND_BB_HEAD_L 0x14 | |
170 | #define CTX_SECOND_BB_STATE 0x16 | |
171 | #define CTX_BB_PER_CTX_PTR 0x18 | |
172 | #define CTX_RCS_INDIRECT_CTX 0x1a | |
173 | #define CTX_RCS_INDIRECT_CTX_OFFSET 0x1c | |
174 | #define CTX_LRI_HEADER_1 0x21 | |
175 | #define CTX_CTX_TIMESTAMP 0x22 | |
176 | #define CTX_PDP3_UDW 0x24 | |
177 | #define CTX_PDP3_LDW 0x26 | |
178 | #define CTX_PDP2_UDW 0x28 | |
179 | #define CTX_PDP2_LDW 0x2a | |
180 | #define CTX_PDP1_UDW 0x2c | |
181 | #define CTX_PDP1_LDW 0x2e | |
182 | #define CTX_PDP0_UDW 0x30 | |
183 | #define CTX_PDP0_LDW 0x32 | |
184 | #define CTX_LRI_HEADER_2 0x41 | |
185 | #define CTX_R_PWR_CLK_STATE 0x42 | |
186 | #define CTX_GPGPU_CSR_BASE_ADDRESS 0x44 | |
187 | ||
56e51bf0 | 188 | #define CTX_REG(reg_state, pos, reg, val) do { \ |
f0f59a00 | 189 | (reg_state)[(pos)+0] = i915_mmio_reg_offset(reg); \ |
0d925ea0 VS |
190 | (reg_state)[(pos)+1] = (val); \ |
191 | } while (0) | |
192 | ||
193 | #define ASSIGN_CTX_PDP(ppgtt, reg_state, n) do { \ | |
d852c7bf | 194 | const u64 _addr = i915_page_dir_dma_addr((ppgtt), (n)); \ |
e5815a2e MT |
195 | reg_state[CTX_PDP ## n ## _UDW+1] = upper_32_bits(_addr); \ |
196 | reg_state[CTX_PDP ## n ## _LDW+1] = lower_32_bits(_addr); \ | |
9244a817 | 197 | } while (0) |
e5815a2e | 198 | |
9244a817 | 199 | #define ASSIGN_CTX_PML4(ppgtt, reg_state) do { \ |
2dba3239 MT |
200 | reg_state[CTX_PDP0_UDW + 1] = upper_32_bits(px_dma(&ppgtt->pml4)); \ |
201 | reg_state[CTX_PDP0_LDW + 1] = lower_32_bits(px_dma(&ppgtt->pml4)); \ | |
9244a817 | 202 | } while (0) |
2dba3239 | 203 | |
71562919 MT |
204 | #define GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x17 |
205 | #define GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x26 | |
7bd0a2c6 | 206 | #define GEN10_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x19 |
84b790f8 | 207 | |
0e93cdd4 CW |
208 | /* Typical size of the average request (2 pipecontrols and a MI_BB) */ |
209 | #define EXECLISTS_REQUEST_SIZE 64 /* bytes */ | |
a3aabe86 | 210 | #define WA_TAIL_DWORDS 2 |
7e4992ac | 211 | #define WA_TAIL_BYTES (sizeof(u32) * WA_TAIL_DWORDS) |
beecec90 | 212 | #define PREEMPT_ID 0x1 |
a3aabe86 | 213 | |
e2efd130 | 214 | static int execlists_context_deferred_alloc(struct i915_gem_context *ctx, |
978f1e09 | 215 | struct intel_engine_cs *engine); |
a3aabe86 CW |
216 | static void execlists_init_reg_state(u32 *reg_state, |
217 | struct i915_gem_context *ctx, | |
218 | struct intel_engine_cs *engine, | |
219 | struct intel_ring *ring); | |
7ba717cf | 220 | |
73e4d07f OM |
221 | /** |
222 | * intel_sanitize_enable_execlists() - sanitize i915.enable_execlists | |
14bb2c11 | 223 | * @dev_priv: i915 device private |
73e4d07f OM |
224 | * @enable_execlists: value of i915.enable_execlists module parameter. |
225 | * | |
226 | * Only certain platforms support Execlists (the prerequisites being | |
27401d12 | 227 | * support for Logical Ring Contexts and Aliasing PPGTT or better). |
73e4d07f OM |
228 | * |
229 | * Return: 1 if Execlists is supported and has to be enabled. | |
230 | */ | |
c033666a | 231 | int intel_sanitize_enable_execlists(struct drm_i915_private *dev_priv, int enable_execlists) |
127f1003 | 232 | { |
a0bd6c31 ZL |
233 | /* On platforms with execlist available, vGPU will only |
234 | * support execlist mode, no ring buffer mode. | |
235 | */ | |
c033666a | 236 | if (HAS_LOGICAL_RING_CONTEXTS(dev_priv) && intel_vgpu_active(dev_priv)) |
a0bd6c31 ZL |
237 | return 1; |
238 | ||
c033666a | 239 | if (INTEL_GEN(dev_priv) >= 9) |
70ee45e1 DL |
240 | return 1; |
241 | ||
127f1003 OM |
242 | if (enable_execlists == 0) |
243 | return 0; | |
244 | ||
5a21b665 | 245 | if (HAS_LOGICAL_RING_CONTEXTS(dev_priv) && |
8279aaf5 | 246 | USES_PPGTT(dev_priv)) |
127f1003 OM |
247 | return 1; |
248 | ||
249 | return 0; | |
250 | } | |
ede7d42b | 251 | |
73e4d07f | 252 | /** |
ca82580c TU |
253 | * intel_lr_context_descriptor_update() - calculate & cache the descriptor |
254 | * descriptor for a pinned context | |
ca82580c | 255 | * @ctx: Context to work on |
9021ad03 | 256 | * @engine: Engine the descriptor will be used with |
73e4d07f | 257 | * |
ca82580c TU |
258 | * The context descriptor encodes various attributes of a context, |
259 | * including its GTT address and some flags. Because it's fairly | |
260 | * expensive to calculate, we'll just do it once and cache the result, | |
261 | * which remains valid until the context is unpinned. | |
262 | * | |
6e5248b5 DV |
263 | * This is what a descriptor looks like, from LSB to MSB:: |
264 | * | |
2355cf08 | 265 | * bits 0-11: flags, GEN8_CTX_* (cached in ctx->desc_template) |
6e5248b5 DV |
266 | * bits 12-31: LRCA, GTT address of (the HWSP of) this context |
267 | * bits 32-52: ctx ID, a globally unique tag | |
268 | * bits 53-54: mbz, reserved for use by hardware | |
269 | * bits 55-63: group ID, currently unused and set to 0 | |
73e4d07f | 270 | */ |
ca82580c | 271 | static void |
e2efd130 | 272 | intel_lr_context_descriptor_update(struct i915_gem_context *ctx, |
0bc40be8 | 273 | struct intel_engine_cs *engine) |
84b790f8 | 274 | { |
9021ad03 | 275 | struct intel_context *ce = &ctx->engine[engine->id]; |
7069b144 | 276 | u64 desc; |
84b790f8 | 277 | |
7069b144 | 278 | BUILD_BUG_ON(MAX_CONTEXT_HW_ID > (1<<GEN8_CTX_ID_WIDTH)); |
84b790f8 | 279 | |
2355cf08 | 280 | desc = ctx->desc_template; /* bits 0-11 */ |
0b29c75a | 281 | desc |= i915_ggtt_offset(ce->state) + LRC_HEADER_PAGES * PAGE_SIZE; |
9021ad03 | 282 | /* bits 12-31 */ |
7069b144 | 283 | desc |= (u64)ctx->hw_id << GEN8_CTX_ID_SHIFT; /* bits 32-52 */ |
5af05fef | 284 | |
9021ad03 | 285 | ce->lrc_desc = desc; |
5af05fef MT |
286 | } |
287 | ||
27606fd8 CW |
288 | static struct i915_priolist * |
289 | lookup_priolist(struct intel_engine_cs *engine, | |
290 | struct i915_priotree *pt, | |
291 | int prio) | |
08dd3e1a | 292 | { |
b620e870 | 293 | struct intel_engine_execlists * const execlists = &engine->execlists; |
08dd3e1a CW |
294 | struct i915_priolist *p; |
295 | struct rb_node **parent, *rb; | |
296 | bool first = true; | |
297 | ||
b620e870 | 298 | if (unlikely(execlists->no_priolist)) |
08dd3e1a CW |
299 | prio = I915_PRIORITY_NORMAL; |
300 | ||
301 | find_priolist: | |
302 | /* most positive priority is scheduled first, equal priorities fifo */ | |
303 | rb = NULL; | |
b620e870 | 304 | parent = &execlists->queue.rb_node; |
08dd3e1a CW |
305 | while (*parent) { |
306 | rb = *parent; | |
307 | p = rb_entry(rb, typeof(*p), node); | |
308 | if (prio > p->priority) { | |
309 | parent = &rb->rb_left; | |
310 | } else if (prio < p->priority) { | |
311 | parent = &rb->rb_right; | |
312 | first = false; | |
313 | } else { | |
27606fd8 | 314 | return p; |
08dd3e1a CW |
315 | } |
316 | } | |
317 | ||
318 | if (prio == I915_PRIORITY_NORMAL) { | |
b620e870 | 319 | p = &execlists->default_priolist; |
08dd3e1a CW |
320 | } else { |
321 | p = kmem_cache_alloc(engine->i915->priorities, GFP_ATOMIC); | |
322 | /* Convert an allocation failure to a priority bump */ | |
323 | if (unlikely(!p)) { | |
324 | prio = I915_PRIORITY_NORMAL; /* recurses just once */ | |
325 | ||
326 | /* To maintain ordering with all rendering, after an | |
327 | * allocation failure we have to disable all scheduling. | |
328 | * Requests will then be executed in fifo, and schedule | |
329 | * will ensure that dependencies are emitted in fifo. | |
330 | * There will be still some reordering with existing | |
331 | * requests, so if userspace lied about their | |
332 | * dependencies that reordering may be visible. | |
333 | */ | |
b620e870 | 334 | execlists->no_priolist = true; |
08dd3e1a CW |
335 | goto find_priolist; |
336 | } | |
337 | } | |
338 | ||
339 | p->priority = prio; | |
27606fd8 | 340 | INIT_LIST_HEAD(&p->requests); |
08dd3e1a | 341 | rb_link_node(&p->node, rb, parent); |
b620e870 | 342 | rb_insert_color(&p->node, &execlists->queue); |
08dd3e1a | 343 | |
08dd3e1a | 344 | if (first) |
b620e870 | 345 | execlists->first = &p->node; |
08dd3e1a | 346 | |
27606fd8 | 347 | return ptr_pack_bits(p, first, 1); |
08dd3e1a CW |
348 | } |
349 | ||
7e4992ac CW |
350 | static void unwind_wa_tail(struct drm_i915_gem_request *rq) |
351 | { | |
352 | rq->tail = intel_ring_wrap(rq->ring, rq->wa_tail - WA_TAIL_BYTES); | |
353 | assert_ring_tail_valid(rq->ring, rq->tail); | |
354 | } | |
355 | ||
a4598d17 | 356 | static void __unwind_incomplete_requests(struct intel_engine_cs *engine) |
7e4992ac CW |
357 | { |
358 | struct drm_i915_gem_request *rq, *rn; | |
097a9481 MW |
359 | struct i915_priolist *uninitialized_var(p); |
360 | int last_prio = I915_PRIORITY_INVALID; | |
7e4992ac CW |
361 | |
362 | lockdep_assert_held(&engine->timeline->lock); | |
363 | ||
364 | list_for_each_entry_safe_reverse(rq, rn, | |
365 | &engine->timeline->requests, | |
366 | link) { | |
7e4992ac CW |
367 | if (i915_gem_request_completed(rq)) |
368 | return; | |
369 | ||
370 | __i915_gem_request_unsubmit(rq); | |
371 | unwind_wa_tail(rq); | |
372 | ||
097a9481 MW |
373 | GEM_BUG_ON(rq->priotree.priority == I915_PRIORITY_INVALID); |
374 | if (rq->priotree.priority != last_prio) { | |
375 | p = lookup_priolist(engine, | |
376 | &rq->priotree, | |
377 | rq->priotree.priority); | |
378 | p = ptr_mask_bits(p, 1); | |
379 | ||
380 | last_prio = rq->priotree.priority; | |
381 | } | |
382 | ||
383 | list_add(&rq->priotree.link, &p->requests); | |
7e4992ac CW |
384 | } |
385 | } | |
386 | ||
c41937fd | 387 | void |
a4598d17 MW |
388 | execlists_unwind_incomplete_requests(struct intel_engine_execlists *execlists) |
389 | { | |
390 | struct intel_engine_cs *engine = | |
391 | container_of(execlists, typeof(*engine), execlists); | |
392 | ||
393 | spin_lock_irq(&engine->timeline->lock); | |
394 | __unwind_incomplete_requests(engine); | |
395 | spin_unlock_irq(&engine->timeline->lock); | |
396 | } | |
397 | ||
bbd6c47e CW |
398 | static inline void |
399 | execlists_context_status_change(struct drm_i915_gem_request *rq, | |
400 | unsigned long status) | |
84b790f8 | 401 | { |
bbd6c47e CW |
402 | /* |
403 | * Only used when GVT-g is enabled now. When GVT-g is disabled, | |
404 | * The compiler should eliminate this function as dead-code. | |
405 | */ | |
406 | if (!IS_ENABLED(CONFIG_DRM_I915_GVT)) | |
407 | return; | |
6daccb0b | 408 | |
3fc03069 CD |
409 | atomic_notifier_call_chain(&rq->engine->context_status_notifier, |
410 | status, rq); | |
84b790f8 BW |
411 | } |
412 | ||
c6a2ac71 TU |
413 | static void |
414 | execlists_update_context_pdps(struct i915_hw_ppgtt *ppgtt, u32 *reg_state) | |
415 | { | |
416 | ASSIGN_CTX_PDP(ppgtt, reg_state, 3); | |
417 | ASSIGN_CTX_PDP(ppgtt, reg_state, 2); | |
418 | ASSIGN_CTX_PDP(ppgtt, reg_state, 1); | |
419 | ASSIGN_CTX_PDP(ppgtt, reg_state, 0); | |
420 | } | |
421 | ||
70c2a24d | 422 | static u64 execlists_update_context(struct drm_i915_gem_request *rq) |
ae1250b9 | 423 | { |
70c2a24d | 424 | struct intel_context *ce = &rq->ctx->engine[rq->engine->id]; |
04da811b ZW |
425 | struct i915_hw_ppgtt *ppgtt = |
426 | rq->ctx->ppgtt ?: rq->i915->mm.aliasing_ppgtt; | |
70c2a24d | 427 | u32 *reg_state = ce->lrc_reg_state; |
ae1250b9 | 428 | |
e6ba9992 | 429 | reg_state[CTX_RING_TAIL+1] = intel_ring_set_tail(rq->ring, rq->tail); |
ae1250b9 | 430 | |
c6a2ac71 TU |
431 | /* True 32b PPGTT with dynamic page allocation: update PDP |
432 | * registers and point the unallocated PDPs to scratch page. | |
433 | * PML4 is allocated during ppgtt init, so this is not needed | |
434 | * in 48-bit mode. | |
435 | */ | |
949e8ab3 | 436 | if (ppgtt && !i915_vm_is_48bit(&ppgtt->base)) |
c6a2ac71 | 437 | execlists_update_context_pdps(ppgtt, reg_state); |
70c2a24d CW |
438 | |
439 | return ce->lrc_desc; | |
ae1250b9 OM |
440 | } |
441 | ||
beecec90 CW |
442 | static inline void elsp_write(u64 desc, u32 __iomem *elsp) |
443 | { | |
444 | writel(upper_32_bits(desc), elsp); | |
445 | writel(lower_32_bits(desc), elsp); | |
446 | } | |
447 | ||
70c2a24d | 448 | static void execlists_submit_ports(struct intel_engine_cs *engine) |
bbd6c47e | 449 | { |
b620e870 | 450 | struct execlist_port *port = engine->execlists.port; |
bbd6c47e | 451 | u32 __iomem *elsp = |
77f0d0e9 CW |
452 | engine->i915->regs + i915_mmio_reg_offset(RING_ELSP(engine)); |
453 | unsigned int n; | |
bbd6c47e | 454 | |
76e70087 | 455 | for (n = execlists_num_ports(&engine->execlists); n--; ) { |
77f0d0e9 CW |
456 | struct drm_i915_gem_request *rq; |
457 | unsigned int count; | |
458 | u64 desc; | |
459 | ||
460 | rq = port_unpack(&port[n], &count); | |
461 | if (rq) { | |
462 | GEM_BUG_ON(count > !n); | |
463 | if (!count++) | |
464 | execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_IN); | |
465 | port_set(&port[n], port_pack(rq, count)); | |
466 | desc = execlists_update_context(rq); | |
467 | GEM_DEBUG_EXEC(port[n].context_id = upper_32_bits(desc)); | |
bccd3b83 CW |
468 | |
469 | GEM_TRACE("%s in[%d]: ctx=%d.%d, seqno=%x\n", | |
470 | engine->name, n, | |
471 | rq->ctx->hw_id, count, | |
472 | rq->global_seqno); | |
77f0d0e9 CW |
473 | } else { |
474 | GEM_BUG_ON(!n); | |
475 | desc = 0; | |
476 | } | |
bbd6c47e | 477 | |
beecec90 | 478 | elsp_write(desc, elsp); |
77f0d0e9 | 479 | } |
bbd6c47e CW |
480 | } |
481 | ||
70c2a24d | 482 | static bool ctx_single_port_submission(const struct i915_gem_context *ctx) |
84b790f8 | 483 | { |
70c2a24d | 484 | return (IS_ENABLED(CONFIG_DRM_I915_GVT) && |
6095868a | 485 | i915_gem_context_force_single_submission(ctx)); |
70c2a24d | 486 | } |
84b790f8 | 487 | |
70c2a24d CW |
488 | static bool can_merge_ctx(const struct i915_gem_context *prev, |
489 | const struct i915_gem_context *next) | |
490 | { | |
491 | if (prev != next) | |
492 | return false; | |
26720ab9 | 493 | |
70c2a24d CW |
494 | if (ctx_single_port_submission(prev)) |
495 | return false; | |
26720ab9 | 496 | |
70c2a24d | 497 | return true; |
84b790f8 BW |
498 | } |
499 | ||
77f0d0e9 CW |
500 | static void port_assign(struct execlist_port *port, |
501 | struct drm_i915_gem_request *rq) | |
502 | { | |
503 | GEM_BUG_ON(rq == port_request(port)); | |
504 | ||
505 | if (port_isset(port)) | |
506 | i915_gem_request_put(port_request(port)); | |
507 | ||
508 | port_set(port, port_pack(i915_gem_request_get(rq), port_count(port))); | |
509 | } | |
510 | ||
beecec90 CW |
511 | static void inject_preempt_context(struct intel_engine_cs *engine) |
512 | { | |
513 | struct intel_context *ce = | |
514 | &engine->i915->preempt_context->engine[engine->id]; | |
515 | u32 __iomem *elsp = | |
516 | engine->i915->regs + i915_mmio_reg_offset(RING_ELSP(engine)); | |
517 | unsigned int n; | |
518 | ||
519 | GEM_BUG_ON(engine->i915->preempt_context->hw_id != PREEMPT_ID); | |
520 | GEM_BUG_ON(!IS_ALIGNED(ce->ring->size, WA_TAIL_BYTES)); | |
521 | ||
522 | memset(ce->ring->vaddr + ce->ring->tail, 0, WA_TAIL_BYTES); | |
523 | ce->ring->tail += WA_TAIL_BYTES; | |
524 | ce->ring->tail &= (ce->ring->size - 1); | |
525 | ce->lrc_reg_state[CTX_RING_TAIL+1] = ce->ring->tail; | |
526 | ||
bccd3b83 | 527 | GEM_TRACE("\n"); |
beecec90 CW |
528 | for (n = execlists_num_ports(&engine->execlists); --n; ) |
529 | elsp_write(0, elsp); | |
530 | ||
531 | elsp_write(ce->lrc_desc, elsp); | |
532 | } | |
533 | ||
70c2a24d | 534 | static void execlists_dequeue(struct intel_engine_cs *engine) |
acdd884a | 535 | { |
7a62cc61 MK |
536 | struct intel_engine_execlists * const execlists = &engine->execlists; |
537 | struct execlist_port *port = execlists->port; | |
76e70087 MK |
538 | const struct execlist_port * const last_port = |
539 | &execlists->port[execlists->port_mask]; | |
beecec90 | 540 | struct drm_i915_gem_request *last = port_request(port); |
20311bd3 | 541 | struct rb_node *rb; |
70c2a24d CW |
542 | bool submit = false; |
543 | ||
70c2a24d CW |
544 | /* Hardware submission is through 2 ports. Conceptually each port |
545 | * has a (RING_START, RING_HEAD, RING_TAIL) tuple. RING_START is | |
546 | * static for a context, and unique to each, so we only execute | |
547 | * requests belonging to a single context from each ring. RING_HEAD | |
548 | * is maintained by the CS in the context image, it marks the place | |
549 | * where it got up to last time, and through RING_TAIL we tell the CS | |
550 | * where we want to execute up to this time. | |
551 | * | |
552 | * In this list the requests are in order of execution. Consecutive | |
553 | * requests from the same context are adjacent in the ringbuffer. We | |
554 | * can combine these requests into a single RING_TAIL update: | |
555 | * | |
556 | * RING_HEAD...req1...req2 | |
557 | * ^- RING_TAIL | |
558 | * since to execute req2 the CS must first execute req1. | |
559 | * | |
560 | * Our goal then is to point each port to the end of a consecutive | |
561 | * sequence of requests as being the most optimal (fewest wake ups | |
562 | * and context switches) submission. | |
779949f4 | 563 | */ |
acdd884a | 564 | |
9f7886d0 | 565 | spin_lock_irq(&engine->timeline->lock); |
7a62cc61 MK |
566 | rb = execlists->first; |
567 | GEM_BUG_ON(rb_first(&execlists->queue) != rb); | |
beecec90 CW |
568 | if (!rb) |
569 | goto unlock; | |
570 | ||
571 | if (last) { | |
572 | /* | |
573 | * Don't resubmit or switch until all outstanding | |
574 | * preemptions (lite-restore) are seen. Then we | |
575 | * know the next preemption status we see corresponds | |
576 | * to this ELSP update. | |
577 | */ | |
578 | if (port_count(&port[0]) > 1) | |
579 | goto unlock; | |
580 | ||
a4598d17 | 581 | if (HAS_LOGICAL_RING_PREEMPTION(engine->i915) && |
beecec90 CW |
582 | rb_entry(rb, struct i915_priolist, node)->priority > |
583 | max(last->priotree.priority, 0)) { | |
584 | /* | |
585 | * Switch to our empty preempt context so | |
586 | * the state of the GPU is known (idle). | |
587 | */ | |
588 | inject_preempt_context(engine); | |
4a118ecb CW |
589 | execlists_set_active(execlists, |
590 | EXECLISTS_ACTIVE_PREEMPT); | |
beecec90 CW |
591 | goto unlock; |
592 | } else { | |
593 | /* | |
594 | * In theory, we could coalesce more requests onto | |
595 | * the second port (the first port is active, with | |
596 | * no preemptions pending). However, that means we | |
597 | * then have to deal with the possible lite-restore | |
598 | * of the second port (as we submit the ELSP, there | |
599 | * may be a context-switch) but also we may complete | |
600 | * the resubmission before the context-switch. Ergo, | |
601 | * coalescing onto the second port will cause a | |
602 | * preemption event, but we cannot predict whether | |
603 | * that will affect port[0] or port[1]. | |
604 | * | |
605 | * If the second port is already active, we can wait | |
606 | * until the next context-switch before contemplating | |
607 | * new requests. The GPU will be busy and we should be | |
608 | * able to resubmit the new ELSP before it idles, | |
609 | * avoiding pipeline bubbles (momentary pauses where | |
610 | * the driver is unable to keep up the supply of new | |
611 | * work). | |
612 | */ | |
613 | if (port_count(&port[1])) | |
614 | goto unlock; | |
615 | ||
616 | /* WaIdleLiteRestore:bdw,skl | |
617 | * Apply the wa NOOPs to prevent | |
618 | * ring:HEAD == req:TAIL as we resubmit the | |
619 | * request. See gen8_emit_breadcrumb() for | |
620 | * where we prepare the padding after the | |
621 | * end of the request. | |
622 | */ | |
623 | last->tail = last->wa_tail; | |
624 | } | |
625 | } | |
626 | ||
627 | do { | |
6c067579 CW |
628 | struct i915_priolist *p = rb_entry(rb, typeof(*p), node); |
629 | struct drm_i915_gem_request *rq, *rn; | |
630 | ||
631 | list_for_each_entry_safe(rq, rn, &p->requests, priotree.link) { | |
632 | /* | |
633 | * Can we combine this request with the current port? | |
634 | * It has to be the same context/ringbuffer and not | |
635 | * have any exceptions (e.g. GVT saying never to | |
636 | * combine contexts). | |
637 | * | |
638 | * If we can combine the requests, we can execute both | |
639 | * by updating the RING_TAIL to point to the end of the | |
640 | * second request, and so we never need to tell the | |
641 | * hardware about the first. | |
70c2a24d | 642 | */ |
6c067579 CW |
643 | if (last && !can_merge_ctx(rq->ctx, last->ctx)) { |
644 | /* | |
645 | * If we are on the second port and cannot | |
646 | * combine this request with the last, then we | |
647 | * are done. | |
648 | */ | |
76e70087 | 649 | if (port == last_port) { |
6c067579 CW |
650 | __list_del_many(&p->requests, |
651 | &rq->priotree.link); | |
652 | goto done; | |
653 | } | |
654 | ||
655 | /* | |
656 | * If GVT overrides us we only ever submit | |
657 | * port[0], leaving port[1] empty. Note that we | |
658 | * also have to be careful that we don't queue | |
659 | * the same context (even though a different | |
660 | * request) to the second port. | |
661 | */ | |
662 | if (ctx_single_port_submission(last->ctx) || | |
663 | ctx_single_port_submission(rq->ctx)) { | |
664 | __list_del_many(&p->requests, | |
665 | &rq->priotree.link); | |
666 | goto done; | |
667 | } | |
668 | ||
669 | GEM_BUG_ON(last->ctx == rq->ctx); | |
670 | ||
671 | if (submit) | |
672 | port_assign(port, last); | |
673 | port++; | |
7a62cc61 MK |
674 | |
675 | GEM_BUG_ON(port_isset(port)); | |
6c067579 | 676 | } |
70c2a24d | 677 | |
6c067579 | 678 | INIT_LIST_HEAD(&rq->priotree.link); |
6c067579 | 679 | __i915_gem_request_submit(rq); |
7a62cc61 | 680 | trace_i915_gem_request_in(rq, port_index(port, execlists)); |
6c067579 CW |
681 | last = rq; |
682 | submit = true; | |
70c2a24d | 683 | } |
d55ac5bf | 684 | |
20311bd3 | 685 | rb = rb_next(rb); |
7a62cc61 | 686 | rb_erase(&p->node, &execlists->queue); |
6c067579 CW |
687 | INIT_LIST_HEAD(&p->requests); |
688 | if (p->priority != I915_PRIORITY_NORMAL) | |
c5cf9a91 | 689 | kmem_cache_free(engine->i915->priorities, p); |
beecec90 | 690 | } while (rb); |
6c067579 | 691 | done: |
7a62cc61 | 692 | execlists->first = rb; |
6c067579 | 693 | if (submit) |
77f0d0e9 | 694 | port_assign(port, last); |
beecec90 | 695 | unlock: |
9f7886d0 | 696 | spin_unlock_irq(&engine->timeline->lock); |
53292cdb | 697 | |
4a118ecb CW |
698 | if (submit) { |
699 | execlists_set_active(execlists, EXECLISTS_ACTIVE_USER); | |
70c2a24d | 700 | execlists_submit_ports(engine); |
4a118ecb | 701 | } |
acdd884a MT |
702 | } |
703 | ||
c41937fd | 704 | void |
a4598d17 | 705 | execlists_cancel_port_requests(struct intel_engine_execlists * const execlists) |
cf4591d1 | 706 | { |
3f9e6cd8 | 707 | struct execlist_port *port = execlists->port; |
dc2279e1 | 708 | unsigned int num_ports = execlists_num_ports(execlists); |
cf4591d1 | 709 | |
3f9e6cd8 | 710 | while (num_ports-- && port_isset(port)) { |
7e44fc28 CW |
711 | struct drm_i915_gem_request *rq = port_request(port); |
712 | ||
4a118ecb | 713 | GEM_BUG_ON(!execlists->active); |
d6c05113 | 714 | execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_PREEMPTED); |
7e44fc28 CW |
715 | i915_gem_request_put(rq); |
716 | ||
3f9e6cd8 CW |
717 | memset(port, 0, sizeof(*port)); |
718 | port++; | |
719 | } | |
cf4591d1 MK |
720 | } |
721 | ||
27a5f61b CW |
722 | static void execlists_cancel_requests(struct intel_engine_cs *engine) |
723 | { | |
b620e870 | 724 | struct intel_engine_execlists * const execlists = &engine->execlists; |
27a5f61b CW |
725 | struct drm_i915_gem_request *rq, *rn; |
726 | struct rb_node *rb; | |
727 | unsigned long flags; | |
27a5f61b CW |
728 | |
729 | spin_lock_irqsave(&engine->timeline->lock, flags); | |
730 | ||
731 | /* Cancel the requests on the HW and clear the ELSP tracker. */ | |
a4598d17 | 732 | execlists_cancel_port_requests(execlists); |
27a5f61b CW |
733 | |
734 | /* Mark all executing requests as skipped. */ | |
735 | list_for_each_entry(rq, &engine->timeline->requests, link) { | |
736 | GEM_BUG_ON(!rq->global_seqno); | |
737 | if (!i915_gem_request_completed(rq)) | |
738 | dma_fence_set_error(&rq->fence, -EIO); | |
739 | } | |
740 | ||
741 | /* Flush the queued requests to the timeline list (for retiring). */ | |
b620e870 | 742 | rb = execlists->first; |
27a5f61b CW |
743 | while (rb) { |
744 | struct i915_priolist *p = rb_entry(rb, typeof(*p), node); | |
745 | ||
746 | list_for_each_entry_safe(rq, rn, &p->requests, priotree.link) { | |
747 | INIT_LIST_HEAD(&rq->priotree.link); | |
27a5f61b CW |
748 | |
749 | dma_fence_set_error(&rq->fence, -EIO); | |
750 | __i915_gem_request_submit(rq); | |
751 | } | |
752 | ||
753 | rb = rb_next(rb); | |
b620e870 | 754 | rb_erase(&p->node, &execlists->queue); |
27a5f61b CW |
755 | INIT_LIST_HEAD(&p->requests); |
756 | if (p->priority != I915_PRIORITY_NORMAL) | |
757 | kmem_cache_free(engine->i915->priorities, p); | |
758 | } | |
759 | ||
760 | /* Remaining _unready_ requests will be nop'ed when submitted */ | |
761 | ||
cf4591d1 | 762 | |
b620e870 MK |
763 | execlists->queue = RB_ROOT; |
764 | execlists->first = NULL; | |
3f9e6cd8 | 765 | GEM_BUG_ON(port_isset(execlists->port)); |
27a5f61b CW |
766 | |
767 | /* | |
768 | * The port is checked prior to scheduling a tasklet, but | |
769 | * just in case we have suspended the tasklet to do the | |
770 | * wedging make sure that when it wakes, it decides there | |
771 | * is no work to do by clearing the irq_posted bit. | |
772 | */ | |
773 | clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted); | |
774 | ||
775 | spin_unlock_irqrestore(&engine->timeline->lock, flags); | |
776 | } | |
777 | ||
6e5248b5 | 778 | /* |
73e4d07f OM |
779 | * Check the unread Context Status Buffers and manage the submission of new |
780 | * contexts to the ELSP accordingly. | |
781 | */ | |
c6dce8f1 | 782 | static void execlists_submission_tasklet(unsigned long data) |
e981e7b1 | 783 | { |
b620e870 MK |
784 | struct intel_engine_cs * const engine = (struct intel_engine_cs *)data; |
785 | struct intel_engine_execlists * const execlists = &engine->execlists; | |
beecec90 | 786 | struct execlist_port * const port = execlists->port; |
c033666a | 787 | struct drm_i915_private *dev_priv = engine->i915; |
c6a2ac71 | 788 | |
48921260 CW |
789 | /* We can skip acquiring intel_runtime_pm_get() here as it was taken |
790 | * on our behalf by the request (see i915_gem_mark_busy()) and it will | |
791 | * not be relinquished until the device is idle (see | |
792 | * i915_gem_idle_work_handler()). As a precaution, we make sure | |
793 | * that all ELSP are drained i.e. we have processed the CSB, | |
794 | * before allowing ourselves to idle and calling intel_runtime_pm_put(). | |
795 | */ | |
796 | GEM_BUG_ON(!dev_priv->gt.awake); | |
797 | ||
b620e870 | 798 | intel_uncore_forcewake_get(dev_priv, execlists->fw_domains); |
c6a2ac71 | 799 | |
899f6204 CW |
800 | /* Prefer doing test_and_clear_bit() as a two stage operation to avoid |
801 | * imposing the cost of a locked atomic transaction when submitting a | |
802 | * new request (outside of the context-switch interrupt). | |
803 | */ | |
804 | while (test_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted)) { | |
6d2cb5aa CW |
805 | /* The HWSP contains a (cacheable) mirror of the CSB */ |
806 | const u32 *buf = | |
807 | &engine->status_page.page_addr[I915_HWS_CSB_BUF0_INDEX]; | |
4af0d727 | 808 | unsigned int head, tail; |
70c2a24d | 809 | |
b620e870 | 810 | if (unlikely(execlists->csb_use_mmio)) { |
6d2cb5aa CW |
811 | buf = (u32 * __force) |
812 | (dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_BUF_LO(engine, 0))); | |
b620e870 | 813 | execlists->csb_head = -1; /* force mmio read of CSB ptrs */ |
6d2cb5aa CW |
814 | } |
815 | ||
2e70b8c6 CW |
816 | /* The write will be ordered by the uncached read (itself |
817 | * a memory barrier), so we do not need another in the form | |
818 | * of a locked instruction. The race between the interrupt | |
819 | * handler and the split test/clear is harmless as we order | |
820 | * our clear before the CSB read. If the interrupt arrived | |
821 | * first between the test and the clear, we read the updated | |
822 | * CSB and clear the bit. If the interrupt arrives as we read | |
823 | * the CSB or later (i.e. after we had cleared the bit) the bit | |
824 | * is set and we do a new loop. | |
825 | */ | |
826 | __clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted); | |
b620e870 | 827 | if (unlikely(execlists->csb_head == -1)) { /* following a reset */ |
767a983a CW |
828 | head = readl(dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine))); |
829 | tail = GEN8_CSB_WRITE_PTR(head); | |
830 | head = GEN8_CSB_READ_PTR(head); | |
b620e870 | 831 | execlists->csb_head = head; |
767a983a CW |
832 | } else { |
833 | const int write_idx = | |
834 | intel_hws_csb_write_index(dev_priv) - | |
835 | I915_HWS_CSB_BUF0_INDEX; | |
836 | ||
b620e870 | 837 | head = execlists->csb_head; |
767a983a CW |
838 | tail = READ_ONCE(buf[write_idx]); |
839 | } | |
bccd3b83 CW |
840 | GEM_TRACE("%s cs-irq head=%d [%d], tail=%d [%d]\n", |
841 | engine->name, | |
842 | head, GEN8_CSB_READ_PTR(readl(dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)))), | |
843 | tail, GEN8_CSB_WRITE_PTR(readl(dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine))))); | |
b620e870 | 844 | |
4af0d727 | 845 | while (head != tail) { |
77f0d0e9 | 846 | struct drm_i915_gem_request *rq; |
4af0d727 | 847 | unsigned int status; |
77f0d0e9 | 848 | unsigned int count; |
4af0d727 CW |
849 | |
850 | if (++head == GEN8_CSB_ENTRIES) | |
851 | head = 0; | |
70c2a24d | 852 | |
2ffe80aa CW |
853 | /* We are flying near dragons again. |
854 | * | |
855 | * We hold a reference to the request in execlist_port[] | |
856 | * but no more than that. We are operating in softirq | |
857 | * context and so cannot hold any mutex or sleep. That | |
858 | * prevents us stopping the requests we are processing | |
859 | * in port[] from being retired simultaneously (the | |
860 | * breadcrumb will be complete before we see the | |
861 | * context-switch). As we only hold the reference to the | |
862 | * request, any pointer chasing underneath the request | |
863 | * is subject to a potential use-after-free. Thus we | |
864 | * store all of the bookkeeping within port[] as | |
865 | * required, and avoid using unguarded pointers beneath | |
866 | * request itself. The same applies to the atomic | |
867 | * status notifier. | |
868 | */ | |
869 | ||
6d2cb5aa | 870 | status = READ_ONCE(buf[2 * head]); /* maybe mmio! */ |
bccd3b83 CW |
871 | GEM_TRACE("%s csb[%dd]: status=0x%08x:0x%08x\n", |
872 | engine->name, head, | |
873 | status, buf[2*head + 1]); | |
70c2a24d CW |
874 | if (!(status & GEN8_CTX_STATUS_COMPLETED_MASK)) |
875 | continue; | |
876 | ||
e40dd226 | 877 | if (status & GEN8_CTX_STATUS_COMPLETE && |
beecec90 | 878 | buf[2*head + 1] == PREEMPT_ID) { |
a4598d17 MW |
879 | execlists_cancel_port_requests(execlists); |
880 | execlists_unwind_incomplete_requests(execlists); | |
beecec90 | 881 | |
4a118ecb CW |
882 | GEM_BUG_ON(!execlists_is_active(execlists, |
883 | EXECLISTS_ACTIVE_PREEMPT)); | |
884 | execlists_clear_active(execlists, | |
885 | EXECLISTS_ACTIVE_PREEMPT); | |
beecec90 CW |
886 | continue; |
887 | } | |
888 | ||
889 | if (status & GEN8_CTX_STATUS_PREEMPTED && | |
4a118ecb CW |
890 | execlists_is_active(execlists, |
891 | EXECLISTS_ACTIVE_PREEMPT)) | |
beecec90 CW |
892 | continue; |
893 | ||
4a118ecb CW |
894 | GEM_BUG_ON(!execlists_is_active(execlists, |
895 | EXECLISTS_ACTIVE_USER)); | |
896 | ||
86aa7e76 | 897 | /* Check the context/desc id for this event matches */ |
6d2cb5aa | 898 | GEM_DEBUG_BUG_ON(buf[2 * head + 1] != port->context_id); |
86aa7e76 | 899 | |
77f0d0e9 | 900 | rq = port_unpack(port, &count); |
bccd3b83 CW |
901 | GEM_TRACE("%s out[0]: ctx=%d.%d, seqno=%x\n", |
902 | engine->name, | |
903 | rq->ctx->hw_id, count, | |
904 | rq->global_seqno); | |
77f0d0e9 CW |
905 | GEM_BUG_ON(count == 0); |
906 | if (--count == 0) { | |
70c2a24d | 907 | GEM_BUG_ON(status & GEN8_CTX_STATUS_PREEMPTED); |
d8747afb CW |
908 | GEM_BUG_ON(port_isset(&port[1]) && |
909 | !(status & GEN8_CTX_STATUS_ELEMENT_SWITCH)); | |
77f0d0e9 CW |
910 | GEM_BUG_ON(!i915_gem_request_completed(rq)); |
911 | execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_OUT); | |
912 | ||
913 | trace_i915_gem_request_out(rq); | |
914 | i915_gem_request_put(rq); | |
70c2a24d | 915 | |
7a62cc61 | 916 | execlists_port_complete(execlists, port); |
77f0d0e9 CW |
917 | } else { |
918 | port_set(port, port_pack(rq, count)); | |
70c2a24d | 919 | } |
26720ab9 | 920 | |
77f0d0e9 CW |
921 | /* After the final element, the hw should be idle */ |
922 | GEM_BUG_ON(port_count(port) == 0 && | |
70c2a24d | 923 | !(status & GEN8_CTX_STATUS_ACTIVE_IDLE)); |
4a118ecb CW |
924 | if (port_count(port) == 0) |
925 | execlists_clear_active(execlists, | |
926 | EXECLISTS_ACTIVE_USER); | |
4af0d727 | 927 | } |
e1fee72c | 928 | |
b620e870 MK |
929 | if (head != execlists->csb_head) { |
930 | execlists->csb_head = head; | |
767a983a CW |
931 | writel(_MASKED_FIELD(GEN8_CSB_READ_PTR_MASK, head << 8), |
932 | dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine))); | |
933 | } | |
e981e7b1 TD |
934 | } |
935 | ||
4a118ecb | 936 | if (!execlists_is_active(execlists, EXECLISTS_ACTIVE_PREEMPT)) |
70c2a24d | 937 | execlists_dequeue(engine); |
c6a2ac71 | 938 | |
b620e870 | 939 | intel_uncore_forcewake_put(dev_priv, execlists->fw_domains); |
e981e7b1 TD |
940 | } |
941 | ||
27606fd8 CW |
942 | static 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); | |
beecec90 | 949 | if (ptr_unmask_bits(p, 1)) |
c6dce8f1 | 950 | tasklet_hi_schedule(&engine->execlists.tasklet); |
27606fd8 CW |
951 | } |
952 | ||
f4ea6bdd | 953 | static void execlists_submit_request(struct drm_i915_gem_request *request) |
acdd884a | 954 | { |
4a570db5 | 955 | struct intel_engine_cs *engine = request->engine; |
5590af3e | 956 | unsigned long flags; |
acdd884a | 957 | |
663f71e7 CW |
958 | /* Will be called from irq-context when using foreign fences. */ |
959 | spin_lock_irqsave(&engine->timeline->lock, flags); | |
acdd884a | 960 | |
27606fd8 | 961 | insert_request(engine, &request->priotree, request->priotree.priority); |
acdd884a | 962 | |
b620e870 | 963 | GEM_BUG_ON(!engine->execlists.first); |
6c067579 CW |
964 | GEM_BUG_ON(list_empty(&request->priotree.link)); |
965 | ||
663f71e7 | 966 | spin_unlock_irqrestore(&engine->timeline->lock, flags); |
acdd884a MT |
967 | } |
968 | ||
1f181225 CW |
969 | static 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 | ||
20311bd3 CW |
974 | static struct intel_engine_cs * |
975 | pt_lock_engine(struct i915_priotree *pt, struct intel_engine_cs *locked) | |
976 | { | |
1f181225 | 977 | struct intel_engine_cs *engine = pt_to_request(pt)->engine; |
a79a524e CW |
978 | |
979 | GEM_BUG_ON(!locked); | |
20311bd3 | 980 | |
20311bd3 | 981 | if (engine != locked) { |
a79a524e CW |
982 | spin_unlock(&locked->timeline->lock); |
983 | spin_lock(&engine->timeline->lock); | |
20311bd3 CW |
984 | } |
985 | ||
986 | return engine; | |
987 | } | |
988 | ||
989 | static void execlists_schedule(struct drm_i915_gem_request *request, int prio) | |
990 | { | |
a79a524e | 991 | struct intel_engine_cs *engine; |
20311bd3 CW |
992 | struct i915_dependency *dep, *p; |
993 | struct i915_dependency stack; | |
994 | LIST_HEAD(dfs); | |
995 | ||
7d1ea609 CW |
996 | GEM_BUG_ON(prio == I915_PRIORITY_INVALID); |
997 | ||
20311bd3 CW |
998 | if (prio <= READ_ONCE(request->priotree.priority)) |
999 | return; | |
1000 | ||
70cd1476 CW |
1001 | /* Need BKL in order to use the temporary link inside i915_dependency */ |
1002 | lockdep_assert_held(&request->i915->drm.struct_mutex); | |
20311bd3 CW |
1003 | |
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 | ||
a79a524e CW |
1027 | /* 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) { | |
1f181225 CW |
1033 | if (i915_gem_request_completed(pt_to_request(p->signaler))) |
1034 | continue; | |
1035 | ||
a79a524e | 1036 | GEM_BUG_ON(p->signaler->priority < pt->priority); |
20311bd3 CW |
1037 | if (prio > READ_ONCE(p->signaler->priority)) |
1038 | list_move_tail(&p->dfs_link, &dfs); | |
a79a524e | 1039 | } |
20311bd3 | 1040 | |
0798cff4 | 1041 | list_safe_reset_next(dep, p, dfs_link); |
20311bd3 CW |
1042 | } |
1043 | ||
349bdb68 CW |
1044 | /* 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 | */ | |
7d1ea609 | 1049 | if (request->priotree.priority == I915_PRIORITY_INVALID) { |
349bdb68 CW |
1050 | 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 | ||
a79a524e CW |
1057 | engine = request->engine; |
1058 | spin_lock_irq(&engine->timeline->lock); | |
1059 | ||
20311bd3 CW |
1060 | /* 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 | ||
20311bd3 | 1071 | pt->priority = prio; |
6c067579 CW |
1072 | if (!list_empty(&pt->link)) { |
1073 | __list_del_entry(&pt->link); | |
1074 | insert_request(engine, pt, prio); | |
a79a524e | 1075 | } |
20311bd3 CW |
1076 | } |
1077 | ||
a79a524e | 1078 | spin_unlock_irq(&engine->timeline->lock); |
20311bd3 CW |
1079 | } |
1080 | ||
f4e15af7 CW |
1081 | static 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 | ||
266a240b CW |
1104 | static struct intel_ring * |
1105 | execlists_context_pin(struct intel_engine_cs *engine, | |
1106 | struct i915_gem_context *ctx) | |
dcb4c12a | 1107 | { |
9021ad03 | 1108 | struct intel_context *ce = &ctx->engine[engine->id]; |
7d774cac | 1109 | void *vaddr; |
ca82580c | 1110 | int ret; |
dcb4c12a | 1111 | |
91c8a326 | 1112 | lockdep_assert_held(&ctx->i915->drm.struct_mutex); |
ca82580c | 1113 | |
266a240b CW |
1114 | if (likely(ce->pin_count++)) |
1115 | goto out; | |
a533b4ba | 1116 | GEM_BUG_ON(!ce->pin_count); /* no overflow please! */ |
24f1d3cc | 1117 | |
e8a9c58f CW |
1118 | if (!ce->state) { |
1119 | ret = execlists_context_deferred_alloc(ctx, engine); | |
1120 | if (ret) | |
1121 | goto err; | |
1122 | } | |
56f6e0a7 | 1123 | GEM_BUG_ON(!ce->state); |
e8a9c58f | 1124 | |
f4e15af7 | 1125 | ret = __context_pin(ctx, ce->state); |
e84fe803 | 1126 | if (ret) |
24f1d3cc | 1127 | goto err; |
7ba717cf | 1128 | |
bf3783e5 | 1129 | vaddr = i915_gem_object_pin_map(ce->state->obj, I915_MAP_WB); |
7d774cac TU |
1130 | if (IS_ERR(vaddr)) { |
1131 | ret = PTR_ERR(vaddr); | |
bf3783e5 | 1132 | goto unpin_vma; |
82352e90 TU |
1133 | } |
1134 | ||
d822bb18 | 1135 | ret = intel_ring_pin(ce->ring, ctx->i915, ctx->ggtt_offset_bias); |
e84fe803 | 1136 | if (ret) |
7d774cac | 1137 | goto unpin_map; |
d1675198 | 1138 | |
0bc40be8 | 1139 | intel_lr_context_descriptor_update(ctx, engine); |
9021ad03 | 1140 | |
a3aabe86 CW |
1141 | ce->lrc_reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE; |
1142 | ce->lrc_reg_state[CTX_RING_BUFFER_START+1] = | |
bde13ebd | 1143 | i915_ggtt_offset(ce->ring->vma); |
a3aabe86 | 1144 | |
3d574a6b | 1145 | ce->state->obj->pin_global++; |
9a6feaf0 | 1146 | i915_gem_context_get(ctx); |
266a240b CW |
1147 | out: |
1148 | return ce->ring; | |
7ba717cf | 1149 | |
7d774cac | 1150 | unpin_map: |
bf3783e5 CW |
1151 | i915_gem_object_unpin_map(ce->state->obj); |
1152 | unpin_vma: | |
1153 | __i915_vma_unpin(ce->state); | |
24f1d3cc | 1154 | err: |
9021ad03 | 1155 | ce->pin_count = 0; |
266a240b | 1156 | return ERR_PTR(ret); |
e84fe803 NH |
1157 | } |
1158 | ||
e8a9c58f CW |
1159 | static void execlists_context_unpin(struct intel_engine_cs *engine, |
1160 | struct i915_gem_context *ctx) | |
e84fe803 | 1161 | { |
9021ad03 | 1162 | struct intel_context *ce = &ctx->engine[engine->id]; |
e84fe803 | 1163 | |
91c8a326 | 1164 | lockdep_assert_held(&ctx->i915->drm.struct_mutex); |
9021ad03 | 1165 | GEM_BUG_ON(ce->pin_count == 0); |
321fe304 | 1166 | |
9021ad03 | 1167 | if (--ce->pin_count) |
24f1d3cc | 1168 | return; |
e84fe803 | 1169 | |
aad29fbb | 1170 | intel_ring_unpin(ce->ring); |
dcb4c12a | 1171 | |
3d574a6b | 1172 | ce->state->obj->pin_global--; |
bf3783e5 CW |
1173 | i915_gem_object_unpin_map(ce->state->obj); |
1174 | i915_vma_unpin(ce->state); | |
321fe304 | 1175 | |
9a6feaf0 | 1176 | i915_gem_context_put(ctx); |
dcb4c12a OM |
1177 | } |
1178 | ||
f73e7399 | 1179 | static int execlists_request_alloc(struct drm_i915_gem_request *request) |
ef11c01d CW |
1180 | { |
1181 | struct intel_engine_cs *engine = request->engine; | |
1182 | struct intel_context *ce = &request->ctx->engine[engine->id]; | |
fd138212 | 1183 | int ret; |
ef11c01d | 1184 | |
e8a9c58f CW |
1185 | GEM_BUG_ON(!ce->pin_count); |
1186 | ||
ef11c01d CW |
1187 | /* 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 | ||
fd138212 CW |
1193 | ret = intel_ring_wait_for_space(request->ring, request->reserved_space); |
1194 | if (ret) | |
1195 | return ret; | |
ef11c01d | 1196 | |
ef11c01d CW |
1197 | /* 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; | |
ef11c01d CW |
1206 | } |
1207 | ||
9e000847 AS |
1208 | /* |
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 | */ | |
097d4f1c TU |
1224 | static u32 * |
1225 | gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine, u32 *batch) | |
17ee950d | 1226 | { |
097d4f1c TU |
1227 | *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; | |
1231 | ||
1232 | *batch++ = MI_LOAD_REGISTER_IMM(1); | |
1233 | *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4); | |
1234 | *batch++ = 0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES; | |
1235 | ||
9f235dfa TU |
1236 | batch = gen8_emit_pipe_control(batch, |
1237 | PIPE_CONTROL_CS_STALL | | |
1238 | PIPE_CONTROL_DC_FLUSH_ENABLE, | |
1239 | 0); | |
097d4f1c TU |
1240 | |
1241 | *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; | |
1245 | ||
1246 | return batch; | |
17ee950d AS |
1247 | } |
1248 | ||
6e5248b5 DV |
1249 | /* |
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. | |
4d78c8dc | 1255 | * |
6e5248b5 DV |
1256 | * The number of WA applied are not known at the beginning; we use this field |
1257 | * to return the no of DWORDS written. | |
17ee950d | 1258 | * |
6e5248b5 DV |
1259 | * 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. | |
17ee950d | 1263 | */ |
097d4f1c | 1264 | static u32 *gen8_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch) |
17ee950d | 1265 | { |
7ad00d1a | 1266 | /* WaDisableCtxRestoreArbitration:bdw,chv */ |
097d4f1c | 1267 | *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE; |
17ee950d | 1268 | |
c82435bb | 1269 | /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */ |
097d4f1c TU |
1270 | if (IS_BROADWELL(engine->i915)) |
1271 | batch = gen8_emit_flush_coherentl3_wa(engine, batch); | |
c82435bb | 1272 | |
0160f055 AS |
1273 | /* WaClearSlmSpaceAtContextSwitch:bdw,chv */ |
1274 | /* Actual scratch location is at 128 bytes offset */ | |
9f235dfa TU |
1275 | 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); | |
0160f055 | 1282 | |
beecec90 CW |
1283 | *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE; |
1284 | ||
17ee950d | 1285 | /* Pad to end of cacheline */ |
097d4f1c TU |
1286 | while ((unsigned long)batch % CACHELINE_BYTES) |
1287 | *batch++ = MI_NOOP; | |
17ee950d AS |
1288 | |
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 | ||
097d4f1c | 1295 | return batch; |
17ee950d AS |
1296 | } |
1297 | ||
097d4f1c | 1298 | static u32 *gen9_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch) |
0504cffc | 1299 | { |
beecec90 CW |
1300 | *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE; |
1301 | ||
9fb5026f | 1302 | /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt,glk */ |
097d4f1c | 1303 | batch = gen8_emit_flush_coherentl3_wa(engine, batch); |
a4106a78 | 1304 | |
9fb5026f | 1305 | /* WaDisableGatherAtSetShaderCommonSlice:skl,bxt,kbl,glk */ |
097d4f1c TU |
1306 | *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; | |
873e8171 | 1311 | |
066d4628 MK |
1312 | /* WaClearSlmSpaceAtContextSwitch:kbl */ |
1313 | /* Actual scratch location is at 128 bytes offset */ | |
097d4f1c | 1314 | if (IS_KBL_REVID(engine->i915, 0, KBL_REVID_A0)) { |
9f235dfa TU |
1315 | 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); | |
066d4628 | 1322 | } |
3485d99e | 1323 | |
9fb5026f | 1324 | /* WaMediaPoolStateCmdInWABB:bxt,glk */ |
3485d99e TG |
1325 | 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 | */ | |
097d4f1c TU |
1339 | *batch++ = GEN9_MEDIA_POOL_STATE; |
1340 | *batch++ = GEN9_MEDIA_POOL_ENABLE; | |
1341 | *batch++ = 0x00777000; | |
1342 | *batch++ = 0; | |
1343 | *batch++ = 0; | |
1344 | *batch++ = 0; | |
3485d99e TG |
1345 | } |
1346 | ||
beecec90 CW |
1347 | *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE; |
1348 | ||
0504cffc | 1349 | /* Pad to end of cacheline */ |
097d4f1c TU |
1350 | while ((unsigned long)batch % CACHELINE_BYTES) |
1351 | *batch++ = MI_NOOP; | |
0504cffc | 1352 | |
097d4f1c | 1353 | return batch; |
0504cffc AS |
1354 | } |
1355 | ||
097d4f1c TU |
1356 | #define CTX_WA_BB_OBJ_SIZE (PAGE_SIZE) |
1357 | ||
1358 | static int lrc_setup_wa_ctx(struct intel_engine_cs *engine) | |
17ee950d | 1359 | { |
48bb74e4 CW |
1360 | struct drm_i915_gem_object *obj; |
1361 | struct i915_vma *vma; | |
1362 | int err; | |
17ee950d | 1363 | |
097d4f1c | 1364 | obj = i915_gem_object_create(engine->i915, CTX_WA_BB_OBJ_SIZE); |
48bb74e4 CW |
1365 | if (IS_ERR(obj)) |
1366 | return PTR_ERR(obj); | |
17ee950d | 1367 | |
a01cb37a | 1368 | vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL); |
48bb74e4 CW |
1369 | if (IS_ERR(vma)) { |
1370 | err = PTR_ERR(vma); | |
1371 | goto err; | |
17ee950d AS |
1372 | } |
1373 | ||
48bb74e4 CW |
1374 | err = i915_vma_pin(vma, 0, PAGE_SIZE, PIN_GLOBAL | PIN_HIGH); |
1375 | if (err) | |
1376 | goto err; | |
1377 | ||
1378 | engine->wa_ctx.vma = vma; | |
17ee950d | 1379 | return 0; |
48bb74e4 CW |
1380 | |
1381 | err: | |
1382 | i915_gem_object_put(obj); | |
1383 | return err; | |
17ee950d AS |
1384 | } |
1385 | ||
097d4f1c | 1386 | static void lrc_destroy_wa_ctx(struct intel_engine_cs *engine) |
17ee950d | 1387 | { |
19880c4a | 1388 | i915_vma_unpin_and_release(&engine->wa_ctx.vma); |
17ee950d AS |
1389 | } |
1390 | ||
097d4f1c TU |
1391 | typedef u32 *(*wa_bb_func_t)(struct intel_engine_cs *engine, u32 *batch); |
1392 | ||
0bc40be8 | 1393 | static int intel_init_workaround_bb(struct intel_engine_cs *engine) |
17ee950d | 1394 | { |
48bb74e4 | 1395 | struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx; |
097d4f1c TU |
1396 | 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]; | |
17ee950d | 1399 | struct page *page; |
097d4f1c TU |
1400 | void *batch, *batch_ptr; |
1401 | unsigned int i; | |
48bb74e4 | 1402 | int ret; |
17ee950d | 1403 | |
097d4f1c TU |
1404 | if (WARN_ON(engine->id != RCS || !engine->scratch)) |
1405 | return -EINVAL; | |
17ee950d | 1406 | |
097d4f1c | 1407 | switch (INTEL_GEN(engine->i915)) { |
90007bca RV |
1408 | case 10: |
1409 | return 0; | |
097d4f1c TU |
1410 | case 9: |
1411 | wa_bb_fn[0] = gen9_init_indirectctx_bb; | |
b8aa2233 | 1412 | wa_bb_fn[1] = NULL; |
097d4f1c TU |
1413 | break; |
1414 | case 8: | |
1415 | wa_bb_fn[0] = gen8_init_indirectctx_bb; | |
3ad7b52d | 1416 | wa_bb_fn[1] = NULL; |
097d4f1c TU |
1417 | break; |
1418 | default: | |
1419 | MISSING_CASE(INTEL_GEN(engine->i915)); | |
5e60d790 | 1420 | return 0; |
0504cffc | 1421 | } |
5e60d790 | 1422 | |
097d4f1c | 1423 | ret = lrc_setup_wa_ctx(engine); |
17ee950d AS |
1424 | if (ret) { |
1425 | DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret); | |
1426 | return ret; | |
1427 | } | |
1428 | ||
48bb74e4 | 1429 | page = i915_gem_object_get_dirty_page(wa_ctx->vma->obj, 0); |
097d4f1c | 1430 | batch = batch_ptr = kmap_atomic(page); |
17ee950d | 1431 | |
097d4f1c TU |
1432 | /* |
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 | } | |
604a8f6f CW |
1443 | if (wa_bb_fn[i]) |
1444 | batch_ptr = wa_bb_fn[i](engine, batch_ptr); | |
097d4f1c | 1445 | wa_bb[i]->size = batch_ptr - (batch + wa_bb[i]->offset); |
17ee950d AS |
1446 | } |
1447 | ||
097d4f1c TU |
1448 | BUG_ON(batch_ptr - batch > CTX_WA_BB_OBJ_SIZE); |
1449 | ||
17ee950d AS |
1450 | kunmap_atomic(batch); |
1451 | if (ret) | |
097d4f1c | 1452 | lrc_destroy_wa_ctx(engine); |
17ee950d AS |
1453 | |
1454 | return ret; | |
1455 | } | |
1456 | ||
64f09f00 CW |
1457 | static u8 gtiir[] = { |
1458 | [RCS] = 0, | |
1459 | [BCS] = 0, | |
1460 | [VCS] = 1, | |
1461 | [VCS2] = 1, | |
1462 | [VECS] = 3, | |
1463 | }; | |
1464 | ||
0bc40be8 | 1465 | static int gen8_init_common_ring(struct intel_engine_cs *engine) |
9b1136d5 | 1466 | { |
c033666a | 1467 | struct drm_i915_private *dev_priv = engine->i915; |
b620e870 | 1468 | struct intel_engine_execlists * const execlists = &engine->execlists; |
821ed7df CW |
1469 | int ret; |
1470 | ||
1471 | ret = intel_mocs_init_engine(engine); | |
1472 | if (ret) | |
1473 | return ret; | |
9b1136d5 | 1474 | |
ad07dfcd | 1475 | intel_engine_reset_breadcrumbs(engine); |
f3b8f912 | 1476 | intel_engine_init_hangcheck(engine); |
821ed7df | 1477 | |
0bc40be8 | 1478 | I915_WRITE(RING_HWSTAM(engine->mmio_base), 0xffffffff); |
0bc40be8 | 1479 | I915_WRITE(RING_MODE_GEN7(engine), |
9b1136d5 | 1480 | _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE)); |
f3b8f912 CW |
1481 | I915_WRITE(RING_HWS_PGA(engine->mmio_base), |
1482 | engine->status_page.ggtt_offset); | |
1483 | POSTING_READ(RING_HWS_PGA(engine->mmio_base)); | |
dfc53c5e | 1484 | |
0bc40be8 | 1485 | DRM_DEBUG_DRIVER("Execlists enabled for %s\n", engine->name); |
9b1136d5 | 1486 | |
64f09f00 CW |
1487 | 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); | |
f747026c | 1500 | clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted); |
b620e870 | 1501 | execlists->csb_head = -1; |
4a118ecb | 1502 | execlists->active = 0; |
6b764a59 | 1503 | |
64f09f00 | 1504 | /* After a GPU reset, we may have requests to replay */ |
9bdc3573 | 1505 | if (execlists->first) |
c6dce8f1 | 1506 | tasklet_schedule(&execlists->tasklet); |
6b764a59 | 1507 | |
821ed7df | 1508 | return 0; |
9b1136d5 OM |
1509 | } |
1510 | ||
0bc40be8 | 1511 | static int gen8_init_render_ring(struct intel_engine_cs *engine) |
9b1136d5 | 1512 | { |
c033666a | 1513 | struct drm_i915_private *dev_priv = engine->i915; |
9b1136d5 OM |
1514 | int ret; |
1515 | ||
0bc40be8 | 1516 | ret = gen8_init_common_ring(engine); |
9b1136d5 OM |
1517 | 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 | ||
9b1136d5 OM |
1528 | I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING)); |
1529 | ||
0bc40be8 | 1530 | return init_workarounds_ring(engine); |
9b1136d5 OM |
1531 | } |
1532 | ||
0bc40be8 | 1533 | static int gen9_init_render_ring(struct intel_engine_cs *engine) |
82ef822e DL |
1534 | { |
1535 | int ret; | |
1536 | ||
0bc40be8 | 1537 | ret = gen8_init_common_ring(engine); |
82ef822e DL |
1538 | if (ret) |
1539 | return ret; | |
1540 | ||
0bc40be8 | 1541 | return init_workarounds_ring(engine); |
82ef822e DL |
1542 | } |
1543 | ||
821ed7df CW |
1544 | static void reset_common_ring(struct intel_engine_cs *engine, |
1545 | struct drm_i915_gem_request *request) | |
1546 | { | |
b620e870 | 1547 | struct intel_engine_execlists * const execlists = &engine->execlists; |
c0dcb203 | 1548 | struct intel_context *ce; |
221ab971 | 1549 | unsigned long flags; |
cdb6ded4 | 1550 | |
221ab971 CW |
1551 | spin_lock_irqsave(&engine->timeline->lock, flags); |
1552 | ||
cdb6ded4 CW |
1553 | /* |
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 | */ | |
a4598d17 | 1562 | execlists_cancel_port_requests(execlists); |
cdb6ded4 | 1563 | |
221ab971 | 1564 | /* Push back any incomplete requests for replay after the reset. */ |
a4598d17 | 1565 | __unwind_incomplete_requests(engine); |
cdb6ded4 | 1566 | |
221ab971 | 1567 | spin_unlock_irqrestore(&engine->timeline->lock, flags); |
c0dcb203 CW |
1568 | |
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 | */ | |
221ab971 | 1579 | if (!request || request->fence.error != -EIO) |
c0dcb203 | 1580 | return; |
821ed7df | 1581 | |
a3aabe86 CW |
1582 | /* 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 | */ | |
c0dcb203 | 1589 | ce = &request->ctx->engine[engine->id]; |
a3aabe86 CW |
1590 | execlists_init_reg_state(ce->lrc_reg_state, |
1591 | request->ctx, engine, ce->ring); | |
1592 | ||
821ed7df | 1593 | /* Move the RING_HEAD onto the breadcrumb, past the hanging batch */ |
a3aabe86 CW |
1594 | ce->lrc_reg_state[CTX_RING_BUFFER_START+1] = |
1595 | i915_ggtt_offset(ce->ring->vma); | |
821ed7df | 1596 | ce->lrc_reg_state[CTX_RING_HEAD+1] = request->postfix; |
a3aabe86 | 1597 | |
821ed7df | 1598 | request->ring->head = request->postfix; |
821ed7df CW |
1599 | intel_ring_update_space(request->ring); |
1600 | ||
a3aabe86 | 1601 | /* Reset WaIdleLiteRestore:bdw,skl as well */ |
7e4992ac | 1602 | unwind_wa_tail(request); |
821ed7df CW |
1603 | } |
1604 | ||
7a01a0a2 MT |
1605 | static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request *req) |
1606 | { | |
1607 | struct i915_hw_ppgtt *ppgtt = req->ctx->ppgtt; | |
4a570db5 | 1608 | struct intel_engine_cs *engine = req->engine; |
e7167769 | 1609 | const int num_lri_cmds = GEN8_3LVL_PDPES * 2; |
73dec95e TU |
1610 | u32 *cs; |
1611 | int i; | |
7a01a0a2 | 1612 | |
73dec95e TU |
1613 | cs = intel_ring_begin(req, num_lri_cmds * 2 + 2); |
1614 | if (IS_ERR(cs)) | |
1615 | return PTR_ERR(cs); | |
7a01a0a2 | 1616 | |
73dec95e | 1617 | *cs++ = MI_LOAD_REGISTER_IMM(num_lri_cmds); |
e7167769 | 1618 | for (i = GEN8_3LVL_PDPES - 1; i >= 0; i--) { |
7a01a0a2 MT |
1619 | const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i); |
1620 | ||
73dec95e TU |
1621 | *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); | |
7a01a0a2 MT |
1625 | } |
1626 | ||
73dec95e TU |
1627 | *cs++ = MI_NOOP; |
1628 | intel_ring_advance(req, cs); | |
7a01a0a2 MT |
1629 | |
1630 | return 0; | |
1631 | } | |
1632 | ||
be795fc1 | 1633 | static int gen8_emit_bb_start(struct drm_i915_gem_request *req, |
803688ba | 1634 | u64 offset, u32 len, |
54af56db | 1635 | const unsigned int flags) |
15648585 | 1636 | { |
73dec95e | 1637 | u32 *cs; |
15648585 OM |
1638 | int ret; |
1639 | ||
7a01a0a2 MT |
1640 | /* 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 | |
2dba3239 MT |
1644 | * not idle). PML4 is allocated during ppgtt init so this is |
1645 | * not needed in 48-bit.*/ | |
7a01a0a2 | 1646 | if (req->ctx->ppgtt && |
54af56db MK |
1647 | (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; | |
7a01a0a2 | 1653 | |
666796da | 1654 | req->ctx->ppgtt->pd_dirty_rings &= ~intel_engine_flag(req->engine); |
7a01a0a2 MT |
1655 | } |
1656 | ||
73dec95e TU |
1657 | cs = intel_ring_begin(req, 4); |
1658 | if (IS_ERR(cs)) | |
1659 | return PTR_ERR(cs); | |
15648585 | 1660 | |
279f5a00 CW |
1661 | /* |
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 | */ | |
3ad7b52d CW |
1678 | *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE; |
1679 | ||
15648585 | 1680 | /* FIXME(BDW): Address space and security selectors. */ |
54af56db MK |
1681 | *cs++ = MI_BATCH_BUFFER_START_GEN8 | |
1682 | (flags & I915_DISPATCH_SECURE ? 0 : BIT(8)) | | |
1683 | (flags & I915_DISPATCH_RS ? MI_BATCH_RESOURCE_STREAMER : 0); | |
73dec95e TU |
1684 | *cs++ = lower_32_bits(offset); |
1685 | *cs++ = upper_32_bits(offset); | |
73dec95e | 1686 | intel_ring_advance(req, cs); |
15648585 OM |
1687 | |
1688 | return 0; | |
1689 | } | |
1690 | ||
31bb59cc | 1691 | static void gen8_logical_ring_enable_irq(struct intel_engine_cs *engine) |
73d477f6 | 1692 | { |
c033666a | 1693 | struct drm_i915_private *dev_priv = engine->i915; |
31bb59cc CW |
1694 | I915_WRITE_IMR(engine, |
1695 | ~(engine->irq_enable_mask | engine->irq_keep_mask)); | |
1696 | POSTING_READ_FW(RING_IMR(engine->mmio_base)); | |
73d477f6 OM |
1697 | } |
1698 | ||
31bb59cc | 1699 | static void gen8_logical_ring_disable_irq(struct intel_engine_cs *engine) |
73d477f6 | 1700 | { |
c033666a | 1701 | struct drm_i915_private *dev_priv = engine->i915; |
31bb59cc | 1702 | I915_WRITE_IMR(engine, ~engine->irq_keep_mask); |
73d477f6 OM |
1703 | } |
1704 | ||
7c9cf4e3 | 1705 | static int gen8_emit_flush(struct drm_i915_gem_request *request, u32 mode) |
4712274c | 1706 | { |
73dec95e | 1707 | u32 cmd, *cs; |
4712274c | 1708 | |
73dec95e TU |
1709 | cs = intel_ring_begin(request, 4); |
1710 | if (IS_ERR(cs)) | |
1711 | return PTR_ERR(cs); | |
4712274c OM |
1712 | |
1713 | cmd = MI_FLUSH_DW + 1; | |
1714 | ||
f0a1fb10 CW |
1715 | /* 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 | ||
7c9cf4e3 | 1722 | if (mode & EMIT_INVALIDATE) { |
f0a1fb10 | 1723 | cmd |= MI_INVALIDATE_TLB; |
1dae2dfb | 1724 | if (request->engine->id == VCS) |
f0a1fb10 | 1725 | cmd |= MI_INVALIDATE_BSD; |
4712274c OM |
1726 | } |
1727 | ||
73dec95e TU |
1728 | *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); | |
4712274c OM |
1733 | |
1734 | return 0; | |
1735 | } | |
1736 | ||
7deb4d39 | 1737 | static int gen8_emit_flush_render(struct drm_i915_gem_request *request, |
7c9cf4e3 | 1738 | u32 mode) |
4712274c | 1739 | { |
b5321f30 | 1740 | struct intel_engine_cs *engine = request->engine; |
bde13ebd CW |
1741 | u32 scratch_addr = |
1742 | i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES; | |
0b2d0934 | 1743 | bool vf_flush_wa = false, dc_flush_wa = false; |
73dec95e | 1744 | u32 *cs, flags = 0; |
0b2d0934 | 1745 | int len; |
4712274c OM |
1746 | |
1747 | flags |= PIPE_CONTROL_CS_STALL; | |
1748 | ||
7c9cf4e3 | 1749 | if (mode & EMIT_FLUSH) { |
4712274c OM |
1750 | flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH; |
1751 | flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH; | |
965fd602 | 1752 | flags |= PIPE_CONTROL_DC_FLUSH_ENABLE; |
40a24488 | 1753 | flags |= PIPE_CONTROL_FLUSH_ENABLE; |
4712274c OM |
1754 | } |
1755 | ||
7c9cf4e3 | 1756 | if (mode & EMIT_INVALIDATE) { |
4712274c OM |
1757 | 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; | |
4712274c | 1765 | |
1a5a9ce7 BW |
1766 | /* |
1767 | * On GEN9: before VF_CACHE_INVALIDATE we need to emit a NULL | |
1768 | * pipe control. | |
1769 | */ | |
c033666a | 1770 | if (IS_GEN9(request->i915)) |
1a5a9ce7 | 1771 | vf_flush_wa = true; |
0b2d0934 MK |
1772 | |
1773 | /* WaForGAMHang:kbl */ | |
1774 | if (IS_KBL_REVID(request->i915, 0, KBL_REVID_B0)) | |
1775 | dc_flush_wa = true; | |
1a5a9ce7 | 1776 | } |
9647ff36 | 1777 | |
0b2d0934 MK |
1778 | len = 6; |
1779 | ||
1780 | if (vf_flush_wa) | |
1781 | len += 6; | |
1782 | ||
1783 | if (dc_flush_wa) | |
1784 | len += 12; | |
1785 | ||
73dec95e TU |
1786 | cs = intel_ring_begin(request, len); |
1787 | if (IS_ERR(cs)) | |
1788 | return PTR_ERR(cs); | |
4712274c | 1789 | |
9f235dfa TU |
1790 | if (vf_flush_wa) |
1791 | cs = gen8_emit_pipe_control(cs, 0, 0); | |
9647ff36 | 1792 | |
9f235dfa TU |
1793 | if (dc_flush_wa) |
1794 | cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_DC_FLUSH_ENABLE, | |
1795 | 0); | |
0b2d0934 | 1796 | |
9f235dfa | 1797 | cs = gen8_emit_pipe_control(cs, flags, scratch_addr); |
0b2d0934 | 1798 | |
9f235dfa TU |
1799 | if (dc_flush_wa) |
1800 | cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_CS_STALL, 0); | |
0b2d0934 | 1801 | |
73dec95e | 1802 | intel_ring_advance(request, cs); |
4712274c OM |
1803 | |
1804 | return 0; | |
1805 | } | |
1806 | ||
7c17d377 CW |
1807 | /* |
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 | */ | |
73dec95e | 1812 | static void gen8_emit_wa_tail(struct drm_i915_gem_request *request, u32 *cs) |
4da46e1e | 1813 | { |
beecec90 CW |
1814 | /* Ensure there's always at least one preemption point per-request. */ |
1815 | *cs++ = MI_ARB_CHECK; | |
73dec95e TU |
1816 | *cs++ = MI_NOOP; |
1817 | request->wa_tail = intel_ring_offset(request, cs); | |
caddfe71 | 1818 | } |
4da46e1e | 1819 | |
73dec95e | 1820 | static void gen8_emit_breadcrumb(struct drm_i915_gem_request *request, u32 *cs) |
caddfe71 | 1821 | { |
7c17d377 CW |
1822 | /* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */ |
1823 | BUILD_BUG_ON(I915_GEM_HWS_INDEX_ADDR & (1 << 5)); | |
4da46e1e | 1824 | |
df77cd83 MW |
1825 | cs = gen8_emit_ggtt_write(cs, request->global_seqno, |
1826 | intel_hws_seqno_address(request->engine)); | |
73dec95e TU |
1827 | *cs++ = MI_USER_INTERRUPT; |
1828 | *cs++ = MI_NOOP; | |
1829 | request->tail = intel_ring_offset(request, cs); | |
ed1501d4 | 1830 | assert_ring_tail_valid(request->ring, request->tail); |
caddfe71 | 1831 | |
73dec95e | 1832 | gen8_emit_wa_tail(request, cs); |
7c17d377 | 1833 | } |
98f29e8d CW |
1834 | static const int gen8_emit_breadcrumb_sz = 6 + WA_TAIL_DWORDS; |
1835 | ||
df77cd83 | 1836 | static void gen8_emit_breadcrumb_rcs(struct drm_i915_gem_request *request, |
73dec95e | 1837 | u32 *cs) |
7c17d377 | 1838 | { |
ce81a65c MW |
1839 | /* We're using qword write, seqno should be aligned to 8 bytes. */ |
1840 | BUILD_BUG_ON(I915_GEM_HWS_INDEX & 1); | |
1841 | ||
df77cd83 MW |
1842 | cs = gen8_emit_ggtt_write_rcs(cs, request->global_seqno, |
1843 | intel_hws_seqno_address(request->engine)); | |
73dec95e TU |
1844 | *cs++ = MI_USER_INTERRUPT; |
1845 | *cs++ = MI_NOOP; | |
1846 | request->tail = intel_ring_offset(request, cs); | |
ed1501d4 | 1847 | assert_ring_tail_valid(request->ring, request->tail); |
caddfe71 | 1848 | |
73dec95e | 1849 | gen8_emit_wa_tail(request, cs); |
4da46e1e | 1850 | } |
df77cd83 | 1851 | static const int gen8_emit_breadcrumb_rcs_sz = 8 + WA_TAIL_DWORDS; |
98f29e8d | 1852 | |
8753181e | 1853 | static int gen8_init_rcs_context(struct drm_i915_gem_request *req) |
e7778be1 TD |
1854 | { |
1855 | int ret; | |
1856 | ||
4ac9659e | 1857 | ret = intel_ring_workarounds_emit(req); |
e7778be1 TD |
1858 | if (ret) |
1859 | return ret; | |
1860 | ||
3bbaba0c PA |
1861 | 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 | ||
4e50f082 | 1869 | return i915_gem_render_state_emit(req); |
e7778be1 TD |
1870 | } |
1871 | ||
73e4d07f OM |
1872 | /** |
1873 | * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer | |
14bb2c11 | 1874 | * @engine: Engine Command Streamer. |
73e4d07f | 1875 | */ |
0bc40be8 | 1876 | void intel_logical_ring_cleanup(struct intel_engine_cs *engine) |
454afebd | 1877 | { |
6402c330 | 1878 | struct drm_i915_private *dev_priv; |
9832b9da | 1879 | |
27af5eea TU |
1880 | /* |
1881 | * Tasklet cannot be active at this point due intel_mark_active/idle | |
1882 | * so this is just for documentation. | |
1883 | */ | |
c6dce8f1 SAK |
1884 | if (WARN_ON(test_bit(TASKLET_STATE_SCHED, |
1885 | &engine->execlists.tasklet.state))) | |
1886 | tasklet_kill(&engine->execlists.tasklet); | |
27af5eea | 1887 | |
c033666a | 1888 | dev_priv = engine->i915; |
6402c330 | 1889 | |
0bc40be8 | 1890 | if (engine->buffer) { |
0bc40be8 | 1891 | WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0); |
b0366a54 | 1892 | } |
48d82387 | 1893 | |
0bc40be8 TU |
1894 | if (engine->cleanup) |
1895 | engine->cleanup(engine); | |
48d82387 | 1896 | |
e8a9c58f | 1897 | intel_engine_cleanup_common(engine); |
17ee950d | 1898 | |
097d4f1c | 1899 | lrc_destroy_wa_ctx(engine); |
c033666a | 1900 | engine->i915 = NULL; |
3b3f1650 AG |
1901 | dev_priv->engine[engine->id] = NULL; |
1902 | kfree(engine); | |
454afebd OM |
1903 | } |
1904 | ||
ff44ad51 | 1905 | static void execlists_set_default_submission(struct intel_engine_cs *engine) |
ddd66c51 | 1906 | { |
ff44ad51 | 1907 | engine->submit_request = execlists_submit_request; |
27a5f61b | 1908 | engine->cancel_requests = execlists_cancel_requests; |
ff44ad51 | 1909 | engine->schedule = execlists_schedule; |
c6dce8f1 | 1910 | engine->execlists.tasklet.func = execlists_submission_tasklet; |
aba5e278 CW |
1911 | |
1912 | engine->park = NULL; | |
1913 | engine->unpark = NULL; | |
ddd66c51 CW |
1914 | } |
1915 | ||
c9cacf93 | 1916 | static void |
e1382efb | 1917 | logical_ring_default_vfuncs(struct intel_engine_cs *engine) |
c9cacf93 TU |
1918 | { |
1919 | /* Default vfuncs which can be overriden by each engine. */ | |
0bc40be8 | 1920 | engine->init_hw = gen8_init_common_ring; |
821ed7df | 1921 | engine->reset_hw = reset_common_ring; |
e8a9c58f CW |
1922 | |
1923 | engine->context_pin = execlists_context_pin; | |
1924 | engine->context_unpin = execlists_context_unpin; | |
1925 | ||
f73e7399 CW |
1926 | engine->request_alloc = execlists_request_alloc; |
1927 | ||
0bc40be8 | 1928 | engine->emit_flush = gen8_emit_flush; |
9b81d556 | 1929 | engine->emit_breadcrumb = gen8_emit_breadcrumb; |
98f29e8d | 1930 | engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_sz; |
ff44ad51 CW |
1931 | |
1932 | engine->set_default_submission = execlists_set_default_submission; | |
ddd66c51 | 1933 | |
31bb59cc CW |
1934 | engine->irq_enable = gen8_logical_ring_enable_irq; |
1935 | engine->irq_disable = gen8_logical_ring_disable_irq; | |
0bc40be8 | 1936 | engine->emit_bb_start = gen8_emit_bb_start; |
c9cacf93 TU |
1937 | } |
1938 | ||
d9f3af96 | 1939 | static inline void |
c2c7f240 | 1940 | logical_ring_default_irqs(struct intel_engine_cs *engine) |
d9f3af96 | 1941 | { |
c2c7f240 | 1942 | unsigned shift = engine->irq_shift; |
0bc40be8 TU |
1943 | engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << shift; |
1944 | engine->irq_keep_mask = GT_CONTEXT_SWITCH_INTERRUPT << shift; | |
d9f3af96 TU |
1945 | } |
1946 | ||
bb45438f TU |
1947 | static void |
1948 | logical_ring_setup(struct intel_engine_cs *engine) | |
1949 | { | |
1950 | struct drm_i915_private *dev_priv = engine->i915; | |
1951 | enum forcewake_domains fw_domains; | |
1952 | ||
019bf277 TU |
1953 | intel_engine_setup_common(engine); |
1954 | ||
bb45438f TU |
1955 | /* Intentionally left blank. */ |
1956 | engine->buffer = NULL; | |
1957 | ||
1958 | fw_domains = intel_uncore_forcewake_for_reg(dev_priv, | |
1959 | RING_ELSP(engine), | |
1960 | FW_REG_WRITE); | |
1961 | ||
1962 | fw_domains |= intel_uncore_forcewake_for_reg(dev_priv, | |
1963 | RING_CONTEXT_STATUS_PTR(engine), | |
1964 | FW_REG_READ | FW_REG_WRITE); | |
1965 | ||
1966 | fw_domains |= intel_uncore_forcewake_for_reg(dev_priv, | |
1967 | RING_CONTEXT_STATUS_BUF_BASE(engine), | |
1968 | FW_REG_READ); | |
1969 | ||
b620e870 | 1970 | engine->execlists.fw_domains = fw_domains; |
bb45438f | 1971 | |
c6dce8f1 SAK |
1972 | tasklet_init(&engine->execlists.tasklet, |
1973 | execlists_submission_tasklet, (unsigned long)engine); | |
bb45438f | 1974 | |
bb45438f TU |
1975 | logical_ring_default_vfuncs(engine); |
1976 | logical_ring_default_irqs(engine); | |
bb45438f TU |
1977 | } |
1978 | ||
486e93f7 | 1979 | static int logical_ring_init(struct intel_engine_cs *engine) |
a19d6ff2 | 1980 | { |
a19d6ff2 TU |
1981 | int ret; |
1982 | ||
019bf277 | 1983 | ret = intel_engine_init_common(engine); |
a19d6ff2 TU |
1984 | if (ret) |
1985 | goto error; | |
1986 | ||
a19d6ff2 TU |
1987 | return 0; |
1988 | ||
1989 | error: | |
1990 | intel_logical_ring_cleanup(engine); | |
1991 | return ret; | |
1992 | } | |
1993 | ||
88d2ba2e | 1994 | int logical_render_ring_init(struct intel_engine_cs *engine) |
a19d6ff2 TU |
1995 | { |
1996 | struct drm_i915_private *dev_priv = engine->i915; | |
1997 | int ret; | |
1998 | ||
bb45438f TU |
1999 | logical_ring_setup(engine); |
2000 | ||
a19d6ff2 TU |
2001 | if (HAS_L3_DPF(dev_priv)) |
2002 | engine->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT; | |
2003 | ||
2004 | /* Override some for render ring. */ | |
2005 | if (INTEL_GEN(dev_priv) >= 9) | |
2006 | engine->init_hw = gen9_init_render_ring; | |
2007 | else | |
2008 | engine->init_hw = gen8_init_render_ring; | |
2009 | engine->init_context = gen8_init_rcs_context; | |
a19d6ff2 | 2010 | engine->emit_flush = gen8_emit_flush_render; |
df77cd83 MW |
2011 | engine->emit_breadcrumb = gen8_emit_breadcrumb_rcs; |
2012 | engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_rcs_sz; | |
a19d6ff2 | 2013 | |
f51455d4 | 2014 | ret = intel_engine_create_scratch(engine, PAGE_SIZE); |
a19d6ff2 TU |
2015 | if (ret) |
2016 | return ret; | |
2017 | ||
2018 | ret = intel_init_workaround_bb(engine); | |
2019 | if (ret) { | |
2020 | /* | |
2021 | * We continue even if we fail to initialize WA batch | |
2022 | * because we only expect rare glitches but nothing | |
2023 | * critical to prevent us from using GPU | |
2024 | */ | |
2025 | DRM_ERROR("WA batch buffer initialization failed: %d\n", | |
2026 | ret); | |
2027 | } | |
2028 | ||
d038fc7e | 2029 | return logical_ring_init(engine); |
a19d6ff2 TU |
2030 | } |
2031 | ||
88d2ba2e | 2032 | int logical_xcs_ring_init(struct intel_engine_cs *engine) |
bb45438f TU |
2033 | { |
2034 | logical_ring_setup(engine); | |
2035 | ||
2036 | return logical_ring_init(engine); | |
454afebd OM |
2037 | } |
2038 | ||
0cea6502 | 2039 | static u32 |
c033666a | 2040 | make_rpcs(struct drm_i915_private *dev_priv) |
0cea6502 JM |
2041 | { |
2042 | u32 rpcs = 0; | |
2043 | ||
2044 | /* | |
2045 | * No explicit RPCS request is needed to ensure full | |
2046 | * slice/subslice/EU enablement prior to Gen9. | |
2047 | */ | |
c033666a | 2048 | if (INTEL_GEN(dev_priv) < 9) |
0cea6502 JM |
2049 | return 0; |
2050 | ||
2051 | /* | |
2052 | * Starting in Gen9, render power gating can leave | |
2053 | * slice/subslice/EU in a partially enabled state. We | |
2054 | * must make an explicit request through RPCS for full | |
2055 | * enablement. | |
2056 | */ | |
43b67998 | 2057 | if (INTEL_INFO(dev_priv)->sseu.has_slice_pg) { |
0cea6502 | 2058 | rpcs |= GEN8_RPCS_S_CNT_ENABLE; |
f08a0c92 | 2059 | rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.slice_mask) << |
0cea6502 JM |
2060 | GEN8_RPCS_S_CNT_SHIFT; |
2061 | rpcs |= GEN8_RPCS_ENABLE; | |
2062 | } | |
2063 | ||
43b67998 | 2064 | if (INTEL_INFO(dev_priv)->sseu.has_subslice_pg) { |
0cea6502 | 2065 | rpcs |= GEN8_RPCS_SS_CNT_ENABLE; |
57ec171e | 2066 | rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.subslice_mask) << |
0cea6502 JM |
2067 | GEN8_RPCS_SS_CNT_SHIFT; |
2068 | rpcs |= GEN8_RPCS_ENABLE; | |
2069 | } | |
2070 | ||
43b67998 ID |
2071 | if (INTEL_INFO(dev_priv)->sseu.has_eu_pg) { |
2072 | rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice << | |
0cea6502 | 2073 | GEN8_RPCS_EU_MIN_SHIFT; |
43b67998 | 2074 | rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice << |
0cea6502 JM |
2075 | GEN8_RPCS_EU_MAX_SHIFT; |
2076 | rpcs |= GEN8_RPCS_ENABLE; | |
2077 | } | |
2078 | ||
2079 | return rpcs; | |
2080 | } | |
2081 | ||
0bc40be8 | 2082 | static u32 intel_lr_indirect_ctx_offset(struct intel_engine_cs *engine) |
71562919 MT |
2083 | { |
2084 | u32 indirect_ctx_offset; | |
2085 | ||
c033666a | 2086 | switch (INTEL_GEN(engine->i915)) { |
71562919 | 2087 | default: |
c033666a | 2088 | MISSING_CASE(INTEL_GEN(engine->i915)); |
71562919 | 2089 | /* fall through */ |
7bd0a2c6 MT |
2090 | case 10: |
2091 | indirect_ctx_offset = | |
2092 | GEN10_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT; | |
2093 | break; | |
71562919 MT |
2094 | case 9: |
2095 | indirect_ctx_offset = | |
2096 | GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT; | |
2097 | break; | |
2098 | case 8: | |
2099 | indirect_ctx_offset = | |
2100 | GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT; | |
2101 | break; | |
2102 | } | |
2103 | ||
2104 | return indirect_ctx_offset; | |
2105 | } | |
2106 | ||
56e51bf0 | 2107 | static void execlists_init_reg_state(u32 *regs, |
a3aabe86 CW |
2108 | struct i915_gem_context *ctx, |
2109 | struct intel_engine_cs *engine, | |
2110 | struct intel_ring *ring) | |
8670d6f9 | 2111 | { |
a3aabe86 CW |
2112 | struct drm_i915_private *dev_priv = engine->i915; |
2113 | struct i915_hw_ppgtt *ppgtt = ctx->ppgtt ?: dev_priv->mm.aliasing_ppgtt; | |
56e51bf0 TU |
2114 | u32 base = engine->mmio_base; |
2115 | bool rcs = engine->id == RCS; | |
2116 | ||
2117 | /* A context is actually a big batch buffer with several | |
2118 | * MI_LOAD_REGISTER_IMM commands followed by (reg, value) pairs. The | |
2119 | * values we are setting here are only for the first context restore: | |
2120 | * on a subsequent save, the GPU will recreate this batchbuffer with new | |
2121 | * values (including all the missing MI_LOAD_REGISTER_IMM commands that | |
2122 | * we are not initializing here). | |
2123 | */ | |
2124 | regs[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(rcs ? 14 : 11) | | |
2125 | MI_LRI_FORCE_POSTED; | |
2126 | ||
2127 | CTX_REG(regs, CTX_CONTEXT_CONTROL, RING_CONTEXT_CONTROL(engine), | |
2128 | _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH | | |
56e51bf0 TU |
2129 | (HAS_RESOURCE_STREAMER(dev_priv) ? |
2130 | CTX_CTRL_RS_CTX_ENABLE : 0))); | |
2131 | CTX_REG(regs, CTX_RING_HEAD, RING_HEAD(base), 0); | |
2132 | CTX_REG(regs, CTX_RING_TAIL, RING_TAIL(base), 0); | |
2133 | CTX_REG(regs, CTX_RING_BUFFER_START, RING_START(base), 0); | |
2134 | CTX_REG(regs, CTX_RING_BUFFER_CONTROL, RING_CTL(base), | |
2135 | RING_CTL_SIZE(ring->size) | RING_VALID); | |
2136 | CTX_REG(regs, CTX_BB_HEAD_U, RING_BBADDR_UDW(base), 0); | |
2137 | CTX_REG(regs, CTX_BB_HEAD_L, RING_BBADDR(base), 0); | |
2138 | CTX_REG(regs, CTX_BB_STATE, RING_BBSTATE(base), RING_BB_PPGTT); | |
2139 | CTX_REG(regs, CTX_SECOND_BB_HEAD_U, RING_SBBADDR_UDW(base), 0); | |
2140 | CTX_REG(regs, CTX_SECOND_BB_HEAD_L, RING_SBBADDR(base), 0); | |
2141 | CTX_REG(regs, CTX_SECOND_BB_STATE, RING_SBBSTATE(base), 0); | |
2142 | if (rcs) { | |
604a8f6f CW |
2143 | struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx; |
2144 | ||
56e51bf0 TU |
2145 | CTX_REG(regs, CTX_RCS_INDIRECT_CTX, RING_INDIRECT_CTX(base), 0); |
2146 | CTX_REG(regs, CTX_RCS_INDIRECT_CTX_OFFSET, | |
2147 | RING_INDIRECT_CTX_OFFSET(base), 0); | |
604a8f6f | 2148 | if (wa_ctx->indirect_ctx.size) { |
bde13ebd | 2149 | u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma); |
17ee950d | 2150 | |
56e51bf0 | 2151 | regs[CTX_RCS_INDIRECT_CTX + 1] = |
097d4f1c TU |
2152 | (ggtt_offset + wa_ctx->indirect_ctx.offset) | |
2153 | (wa_ctx->indirect_ctx.size / CACHELINE_BYTES); | |
17ee950d | 2154 | |
56e51bf0 | 2155 | regs[CTX_RCS_INDIRECT_CTX_OFFSET + 1] = |
0bc40be8 | 2156 | intel_lr_indirect_ctx_offset(engine) << 6; |
604a8f6f CW |
2157 | } |
2158 | ||
2159 | CTX_REG(regs, CTX_BB_PER_CTX_PTR, RING_BB_PER_CTX_PTR(base), 0); | |
2160 | if (wa_ctx->per_ctx.size) { | |
2161 | u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma); | |
17ee950d | 2162 | |
56e51bf0 | 2163 | regs[CTX_BB_PER_CTX_PTR + 1] = |
097d4f1c | 2164 | (ggtt_offset + wa_ctx->per_ctx.offset) | 0x01; |
17ee950d | 2165 | } |
8670d6f9 | 2166 | } |
56e51bf0 TU |
2167 | |
2168 | regs[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9) | MI_LRI_FORCE_POSTED; | |
2169 | ||
2170 | CTX_REG(regs, CTX_CTX_TIMESTAMP, RING_CTX_TIMESTAMP(base), 0); | |
0d925ea0 | 2171 | /* PDP values well be assigned later if needed */ |
56e51bf0 TU |
2172 | CTX_REG(regs, CTX_PDP3_UDW, GEN8_RING_PDP_UDW(engine, 3), 0); |
2173 | CTX_REG(regs, CTX_PDP3_LDW, GEN8_RING_PDP_LDW(engine, 3), 0); | |
2174 | CTX_REG(regs, CTX_PDP2_UDW, GEN8_RING_PDP_UDW(engine, 2), 0); | |
2175 | CTX_REG(regs, CTX_PDP2_LDW, GEN8_RING_PDP_LDW(engine, 2), 0); | |
2176 | CTX_REG(regs, CTX_PDP1_UDW, GEN8_RING_PDP_UDW(engine, 1), 0); | |
2177 | CTX_REG(regs, CTX_PDP1_LDW, GEN8_RING_PDP_LDW(engine, 1), 0); | |
2178 | CTX_REG(regs, CTX_PDP0_UDW, GEN8_RING_PDP_UDW(engine, 0), 0); | |
2179 | CTX_REG(regs, CTX_PDP0_LDW, GEN8_RING_PDP_LDW(engine, 0), 0); | |
d7b2633d | 2180 | |
949e8ab3 | 2181 | if (ppgtt && i915_vm_is_48bit(&ppgtt->base)) { |
2dba3239 MT |
2182 | /* 64b PPGTT (48bit canonical) |
2183 | * PDP0_DESCRIPTOR contains the base address to PML4 and | |
2184 | * other PDP Descriptors are ignored. | |
2185 | */ | |
56e51bf0 | 2186 | ASSIGN_CTX_PML4(ppgtt, regs); |
2dba3239 MT |
2187 | } |
2188 | ||
56e51bf0 TU |
2189 | if (rcs) { |
2190 | regs[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1); | |
2191 | CTX_REG(regs, CTX_R_PWR_CLK_STATE, GEN8_R_PWR_CLK_STATE, | |
2192 | make_rpcs(dev_priv)); | |
19f81df2 RB |
2193 | |
2194 | i915_oa_init_reg_state(engine, ctx, regs); | |
8670d6f9 | 2195 | } |
a3aabe86 CW |
2196 | } |
2197 | ||
2198 | static int | |
2199 | populate_lr_context(struct i915_gem_context *ctx, | |
2200 | struct drm_i915_gem_object *ctx_obj, | |
2201 | struct intel_engine_cs *engine, | |
2202 | struct intel_ring *ring) | |
2203 | { | |
2204 | void *vaddr; | |
d2b4b979 | 2205 | u32 *regs; |
a3aabe86 CW |
2206 | int ret; |
2207 | ||
2208 | ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true); | |
2209 | if (ret) { | |
2210 | DRM_DEBUG_DRIVER("Could not set to CPU domain\n"); | |
2211 | return ret; | |
2212 | } | |
2213 | ||
2214 | vaddr = i915_gem_object_pin_map(ctx_obj, I915_MAP_WB); | |
2215 | if (IS_ERR(vaddr)) { | |
2216 | ret = PTR_ERR(vaddr); | |
2217 | DRM_DEBUG_DRIVER("Could not map object pages! (%d)\n", ret); | |
2218 | return ret; | |
2219 | } | |
a4f5ea64 | 2220 | ctx_obj->mm.dirty = true; |
a3aabe86 | 2221 | |
d2b4b979 CW |
2222 | if (engine->default_state) { |
2223 | /* | |
2224 | * We only want to copy over the template context state; | |
2225 | * skipping over the headers reserved for GuC communication, | |
2226 | * leaving those as zero. | |
2227 | */ | |
2228 | const unsigned long start = LRC_HEADER_PAGES * PAGE_SIZE; | |
2229 | void *defaults; | |
2230 | ||
2231 | defaults = i915_gem_object_pin_map(engine->default_state, | |
2232 | I915_MAP_WB); | |
2233 | if (IS_ERR(defaults)) | |
2234 | return PTR_ERR(defaults); | |
2235 | ||
2236 | memcpy(vaddr + start, defaults + start, engine->context_size); | |
2237 | i915_gem_object_unpin_map(engine->default_state); | |
2238 | } | |
2239 | ||
a3aabe86 CW |
2240 | /* The second page of the context object contains some fields which must |
2241 | * be set up prior to the first execution. */ | |
d2b4b979 CW |
2242 | regs = vaddr + LRC_STATE_PN * PAGE_SIZE; |
2243 | execlists_init_reg_state(regs, ctx, engine, ring); | |
2244 | if (!engine->default_state) | |
2245 | regs[CTX_CONTEXT_CONTROL + 1] |= | |
2246 | _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT); | |
8670d6f9 | 2247 | |
7d774cac | 2248 | i915_gem_object_unpin_map(ctx_obj); |
8670d6f9 OM |
2249 | |
2250 | return 0; | |
2251 | } | |
2252 | ||
e2efd130 | 2253 | static int execlists_context_deferred_alloc(struct i915_gem_context *ctx, |
978f1e09 | 2254 | struct intel_engine_cs *engine) |
ede7d42b | 2255 | { |
8c857917 | 2256 | struct drm_i915_gem_object *ctx_obj; |
9021ad03 | 2257 | struct intel_context *ce = &ctx->engine[engine->id]; |
bf3783e5 | 2258 | struct i915_vma *vma; |
8c857917 | 2259 | uint32_t context_size; |
7e37f889 | 2260 | struct intel_ring *ring; |
8c857917 OM |
2261 | int ret; |
2262 | ||
9021ad03 | 2263 | WARN_ON(ce->state); |
ede7d42b | 2264 | |
63ffbcda | 2265 | context_size = round_up(engine->context_size, I915_GTT_PAGE_SIZE); |
8c857917 | 2266 | |
0b29c75a MT |
2267 | /* |
2268 | * Before the actual start of the context image, we insert a few pages | |
2269 | * for our own use and for sharing with the GuC. | |
2270 | */ | |
2271 | context_size += LRC_HEADER_PAGES * PAGE_SIZE; | |
d1675198 | 2272 | |
12d79d78 | 2273 | ctx_obj = i915_gem_object_create(ctx->i915, context_size); |
fe3db79b | 2274 | if (IS_ERR(ctx_obj)) { |
3126a660 | 2275 | DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n"); |
fe3db79b | 2276 | return PTR_ERR(ctx_obj); |
8c857917 OM |
2277 | } |
2278 | ||
a01cb37a | 2279 | vma = i915_vma_instance(ctx_obj, &ctx->i915->ggtt.base, NULL); |
bf3783e5 CW |
2280 | if (IS_ERR(vma)) { |
2281 | ret = PTR_ERR(vma); | |
2282 | goto error_deref_obj; | |
2283 | } | |
2284 | ||
7e37f889 | 2285 | ring = intel_engine_create_ring(engine, ctx->ring_size); |
dca33ecc CW |
2286 | if (IS_ERR(ring)) { |
2287 | ret = PTR_ERR(ring); | |
e84fe803 | 2288 | goto error_deref_obj; |
8670d6f9 OM |
2289 | } |
2290 | ||
dca33ecc | 2291 | ret = populate_lr_context(ctx, ctx_obj, engine, ring); |
8670d6f9 OM |
2292 | if (ret) { |
2293 | DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret); | |
dca33ecc | 2294 | goto error_ring_free; |
84c2377f OM |
2295 | } |
2296 | ||
dca33ecc | 2297 | ce->ring = ring; |
bf3783e5 | 2298 | ce->state = vma; |
ede7d42b OM |
2299 | |
2300 | return 0; | |
8670d6f9 | 2301 | |
dca33ecc | 2302 | error_ring_free: |
7e37f889 | 2303 | intel_ring_free(ring); |
e84fe803 | 2304 | error_deref_obj: |
f8c417cd | 2305 | i915_gem_object_put(ctx_obj); |
8670d6f9 | 2306 | return ret; |
ede7d42b | 2307 | } |
3e5b6f05 | 2308 | |
821ed7df | 2309 | void intel_lr_context_resume(struct drm_i915_private *dev_priv) |
3e5b6f05 | 2310 | { |
e2f80391 | 2311 | struct intel_engine_cs *engine; |
bafb2f7d | 2312 | struct i915_gem_context *ctx; |
3b3f1650 | 2313 | enum intel_engine_id id; |
bafb2f7d CW |
2314 | |
2315 | /* Because we emit WA_TAIL_DWORDS there may be a disparity | |
2316 | * between our bookkeeping in ce->ring->head and ce->ring->tail and | |
2317 | * that stored in context. As we only write new commands from | |
2318 | * ce->ring->tail onwards, everything before that is junk. If the GPU | |
2319 | * starts reading from its RING_HEAD from the context, it may try to | |
2320 | * execute that junk and die. | |
2321 | * | |
2322 | * So to avoid that we reset the context images upon resume. For | |
2323 | * simplicity, we just zero everything out. | |
2324 | */ | |
829a0af2 | 2325 | list_for_each_entry(ctx, &dev_priv->contexts.list, link) { |
3b3f1650 | 2326 | for_each_engine(engine, dev_priv, id) { |
bafb2f7d CW |
2327 | struct intel_context *ce = &ctx->engine[engine->id]; |
2328 | u32 *reg; | |
3e5b6f05 | 2329 | |
bafb2f7d CW |
2330 | if (!ce->state) |
2331 | continue; | |
7d774cac | 2332 | |
bafb2f7d CW |
2333 | reg = i915_gem_object_pin_map(ce->state->obj, |
2334 | I915_MAP_WB); | |
2335 | if (WARN_ON(IS_ERR(reg))) | |
2336 | continue; | |
3e5b6f05 | 2337 | |
bafb2f7d CW |
2338 | reg += LRC_STATE_PN * PAGE_SIZE / sizeof(*reg); |
2339 | reg[CTX_RING_HEAD+1] = 0; | |
2340 | reg[CTX_RING_TAIL+1] = 0; | |
3e5b6f05 | 2341 | |
a4f5ea64 | 2342 | ce->state->obj->mm.dirty = true; |
bafb2f7d | 2343 | i915_gem_object_unpin_map(ce->state->obj); |
3e5b6f05 | 2344 | |
e6ba9992 | 2345 | intel_ring_reset(ce->ring, 0); |
bafb2f7d | 2346 | } |
3e5b6f05 TD |
2347 | } |
2348 | } |