blob: 5bc0ce1347cef5d7dd0e1c0397f2eefb4ba4e151 [file] [log] [blame]
Oscar Mateob20385f2014-07-24 17:04:10 +01001/*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Ben Widawsky <ben@bwidawsk.net>
25 * Michel Thierry <michel.thierry@intel.com>
26 * Thomas Daniel <thomas.daniel@intel.com>
27 * Oscar Mateo <oscar.mateo@intel.com>
28 *
29 */
30
Oscar Mateo73e4d072014-07-24 17:04:48 +010031/**
32 * DOC: Logical Rings, Logical Ring Contexts and Execlists
33 *
34 * Motivation:
Oscar Mateob20385f2014-07-24 17:04:10 +010035 * GEN8 brings an expansion of the HW contexts: "Logical Ring Contexts".
36 * These expanded contexts enable a number of new abilities, especially
37 * "Execlists" (also implemented in this file).
38 *
Oscar Mateo73e4d072014-07-24 17:04:48 +010039 * One of the main differences with the legacy HW contexts is that logical
40 * ring contexts incorporate many more things to the context's state, like
41 * PDPs or ringbuffer control registers:
42 *
43 * The reason why PDPs are included in the context is straightforward: as
44 * PPGTTs (per-process GTTs) are actually per-context, having the PDPs
45 * contained there mean you don't need to do a ppgtt->switch_mm yourself,
46 * instead, the GPU will do it for you on the context switch.
47 *
48 * But, what about the ringbuffer control registers (head, tail, etc..)?
49 * shouldn't we just need a set of those per engine command streamer? This is
50 * where the name "Logical Rings" starts to make sense: by virtualizing the
51 * rings, the engine cs shifts to a new "ring buffer" with every context
52 * switch. When you want to submit a workload to the GPU you: A) choose your
53 * context, B) find its appropriate virtualized ring, C) write commands to it
54 * and then, finally, D) tell the GPU to switch to that context.
55 *
56 * Instead of the legacy MI_SET_CONTEXT, the way you tell the GPU to switch
57 * to a contexts is via a context execution list, ergo "Execlists".
58 *
59 * LRC implementation:
60 * Regarding the creation of contexts, we have:
61 *
62 * - One global default context.
63 * - One local default context for each opened fd.
64 * - One local extra context for each context create ioctl call.
65 *
66 * Now that ringbuffers belong per-context (and not per-engine, like before)
67 * and that contexts are uniquely tied to a given engine (and not reusable,
68 * like before) we need:
69 *
70 * - One ringbuffer per-engine inside each context.
71 * - One backing object per-engine inside each context.
72 *
73 * The global default context starts its life with these new objects fully
74 * allocated and populated. The local default context for each opened fd is
75 * more complex, because we don't know at creation time which engine is going
76 * to use them. To handle this, we have implemented a deferred creation of LR
77 * contexts:
78 *
79 * The local context starts its life as a hollow or blank holder, that only
80 * gets populated for a given engine once we receive an execbuffer. If later
81 * on we receive another execbuffer ioctl for the same context but a different
82 * engine, we allocate/populate a new ringbuffer and context backing object and
83 * so on.
84 *
85 * Finally, regarding local contexts created using the ioctl call: as they are
86 * only allowed with the render ring, we can allocate & populate them right
87 * away (no need to defer anything, at least for now).
88 *
89 * Execlists implementation:
Oscar Mateob20385f2014-07-24 17:04:10 +010090 * Execlists are the new method by which, on gen8+ hardware, workloads are
91 * submitted for execution (as opposed to the legacy, ringbuffer-based, method).
Oscar Mateo73e4d072014-07-24 17:04:48 +010092 * This method works as follows:
93 *
94 * When a request is committed, its commands (the BB start and any leading or
95 * trailing commands, like the seqno breadcrumbs) are placed in the ringbuffer
96 * for the appropriate context. The tail pointer in the hardware context is not
97 * updated at this time, but instead, kept by the driver in the ringbuffer
98 * structure. A structure representing this request is added to a request queue
99 * for the appropriate engine: this structure contains a copy of the context's
100 * tail after the request was written to the ring buffer and a pointer to the
101 * context itself.
102 *
103 * If the engine's request queue was empty before the request was added, the
104 * queue is processed immediately. Otherwise the queue will be processed during
105 * a context switch interrupt. In any case, elements on the queue will get sent
106 * (in pairs) to the GPU's ExecLists Submit Port (ELSP, for short) with a
107 * globally unique 20-bits submission ID.
108 *
109 * When execution of a request completes, the GPU updates the context status
110 * buffer with a context complete event and generates a context switch interrupt.
111 * During the interrupt handling, the driver examines the events in the buffer:
112 * for each context complete event, if the announced ID matches that on the head
113 * of the request queue, then that request is retired and removed from the queue.
114 *
115 * After processing, if any requests were retired and the queue is not empty
116 * then a new execution list can be submitted. The two requests at the front of
117 * the queue are next to be submitted but since a context may not occur twice in
118 * an execution list, if subsequent requests have the same ID as the first then
119 * the two requests must be combined. This is done simply by discarding requests
120 * at the head of the queue until either only one requests is left (in which case
121 * we use a NULL second context) or the first two requests have unique IDs.
122 *
123 * By always executing the first two requests in the queue the driver ensures
124 * that the GPU is kept as busy as possible. In the case where a single context
125 * completes but a second context is still executing, the request for this second
126 * context will be at the head of the queue when we remove the first one. This
127 * request will then be resubmitted along with a new request for a different context,
128 * which will cause the hardware to continue executing the second request and queue
129 * the new request (the GPU detects the condition of a context getting preempted
130 * with the same context and optimizes the context switch flow by not doing
131 * preemption, but just sampling the new tail pointer).
132 *
Oscar Mateob20385f2014-07-24 17:04:10 +0100133 */
134
135#include <drm/drmP.h>
136#include <drm/i915_drm.h>
137#include "i915_drv.h"
Peter Antoine3bbaba02015-07-10 20:13:11 +0300138#include "intel_mocs.h"
Oscar Mateo127f1002014-07-24 17:04:11 +0100139
Michael H. Nguyen468c6812014-11-13 17:51:49 +0000140#define GEN9_LR_CONTEXT_RENDER_SIZE (22 * PAGE_SIZE)
Oscar Mateo8c8579172014-07-24 17:04:14 +0100141#define GEN8_LR_CONTEXT_RENDER_SIZE (20 * PAGE_SIZE)
142#define GEN8_LR_CONTEXT_OTHER_SIZE (2 * PAGE_SIZE)
143
Thomas Daniele981e7b2014-07-24 17:04:39 +0100144#define RING_EXECLIST_QFULL (1 << 0x2)
145#define RING_EXECLIST1_VALID (1 << 0x3)
146#define RING_EXECLIST0_VALID (1 << 0x4)
147#define RING_EXECLIST_ACTIVE_STATUS (3 << 0xE)
148#define RING_EXECLIST1_ACTIVE (1 << 0x11)
149#define RING_EXECLIST0_ACTIVE (1 << 0x12)
150
151#define GEN8_CTX_STATUS_IDLE_ACTIVE (1 << 0)
152#define GEN8_CTX_STATUS_PREEMPTED (1 << 1)
153#define GEN8_CTX_STATUS_ELEMENT_SWITCH (1 << 2)
154#define GEN8_CTX_STATUS_ACTIVE_IDLE (1 << 3)
155#define GEN8_CTX_STATUS_COMPLETE (1 << 4)
156#define GEN8_CTX_STATUS_LITE_RESTORE (1 << 15)
Oscar Mateo8670d6f2014-07-24 17:04:17 +0100157
158#define CTX_LRI_HEADER_0 0x01
159#define CTX_CONTEXT_CONTROL 0x02
160#define CTX_RING_HEAD 0x04
161#define CTX_RING_TAIL 0x06
162#define CTX_RING_BUFFER_START 0x08
163#define CTX_RING_BUFFER_CONTROL 0x0a
164#define CTX_BB_HEAD_U 0x0c
165#define CTX_BB_HEAD_L 0x0e
166#define CTX_BB_STATE 0x10
167#define CTX_SECOND_BB_HEAD_U 0x12
168#define CTX_SECOND_BB_HEAD_L 0x14
169#define CTX_SECOND_BB_STATE 0x16
170#define CTX_BB_PER_CTX_PTR 0x18
171#define CTX_RCS_INDIRECT_CTX 0x1a
172#define CTX_RCS_INDIRECT_CTX_OFFSET 0x1c
173#define CTX_LRI_HEADER_1 0x21
174#define CTX_CTX_TIMESTAMP 0x22
175#define CTX_PDP3_UDW 0x24
176#define CTX_PDP3_LDW 0x26
177#define CTX_PDP2_UDW 0x28
178#define CTX_PDP2_LDW 0x2a
179#define CTX_PDP1_UDW 0x2c
180#define CTX_PDP1_LDW 0x2e
181#define CTX_PDP0_UDW 0x30
182#define CTX_PDP0_LDW 0x32
183#define CTX_LRI_HEADER_2 0x41
184#define CTX_R_PWR_CLK_STATE 0x42
185#define CTX_GPGPU_CSR_BASE_ADDRESS 0x44
186
Ben Widawsky84b790f2014-07-24 17:04:36 +0100187#define GEN8_CTX_VALID (1<<0)
188#define GEN8_CTX_FORCE_PD_RESTORE (1<<1)
189#define GEN8_CTX_FORCE_RESTORE (1<<2)
190#define GEN8_CTX_L3LLC_COHERENT (1<<5)
191#define GEN8_CTX_PRIVILEGE (1<<8)
Michel Thierrye5815a22015-04-08 12:13:32 +0100192
193#define ASSIGN_CTX_PDP(ppgtt, reg_state, n) { \
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300194 const u64 _addr = i915_page_dir_dma_addr((ppgtt), (n)); \
Michel Thierrye5815a22015-04-08 12:13:32 +0100195 reg_state[CTX_PDP ## n ## _UDW+1] = upper_32_bits(_addr); \
196 reg_state[CTX_PDP ## n ## _LDW+1] = lower_32_bits(_addr); \
197}
198
Ben Widawsky84b790f2014-07-24 17:04:36 +0100199enum {
200 ADVANCED_CONTEXT = 0,
201 LEGACY_CONTEXT,
202 ADVANCED_AD_CONTEXT,
203 LEGACY_64B_CONTEXT
204};
205#define GEN8_CTX_MODE_SHIFT 3
206enum {
207 FAULT_AND_HANG = 0,
208 FAULT_AND_HALT, /* Debug only */
209 FAULT_AND_STREAM,
210 FAULT_AND_CONTINUE /* Unsupported */
211};
212#define GEN8_CTX_ID_SHIFT 32
Arun Siluvery17ee9502015-06-19 19:07:01 +0100213#define CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x17
Ben Widawsky84b790f2014-07-24 17:04:36 +0100214
Mika Kuoppala8ba319d2015-07-03 17:09:35 +0300215static int intel_lr_context_pin(struct drm_i915_gem_request *rq);
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000216
Oscar Mateo73e4d072014-07-24 17:04:48 +0100217/**
218 * intel_sanitize_enable_execlists() - sanitize i915.enable_execlists
219 * @dev: DRM device.
220 * @enable_execlists: value of i915.enable_execlists module parameter.
221 *
222 * Only certain platforms support Execlists (the prerequisites being
Thomas Daniel27401d12014-12-11 12:48:35 +0000223 * support for Logical Ring Contexts and Aliasing PPGTT or better).
Oscar Mateo73e4d072014-07-24 17:04:48 +0100224 *
225 * Return: 1 if Execlists is supported and has to be enabled.
226 */
Oscar Mateo127f1002014-07-24 17:04:11 +0100227int intel_sanitize_enable_execlists(struct drm_device *dev, int enable_execlists)
228{
Daniel Vetterbd84b1e2014-08-11 15:57:57 +0200229 WARN_ON(i915.enable_ppgtt == -1);
230
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000231 if (INTEL_INFO(dev)->gen >= 9)
232 return 1;
233
Oscar Mateo127f1002014-07-24 17:04:11 +0100234 if (enable_execlists == 0)
235 return 0;
236
Oscar Mateo14bf9932014-07-24 17:04:34 +0100237 if (HAS_LOGICAL_RING_CONTEXTS(dev) && USES_PPGTT(dev) &&
238 i915.use_mmio_flip >= 0)
Oscar Mateo127f1002014-07-24 17:04:11 +0100239 return 1;
240
241 return 0;
242}
Oscar Mateoede7d422014-07-24 17:04:12 +0100243
Oscar Mateo73e4d072014-07-24 17:04:48 +0100244/**
245 * intel_execlists_ctx_id() - get the Execlists Context ID
246 * @ctx_obj: Logical Ring Context backing object.
247 *
248 * Do not confuse with ctx->id! Unfortunately we have a name overload
249 * here: the old context ID we pass to userspace as a handler so that
250 * they can refer to a context, and the new context ID we pass to the
251 * ELSP so that the GPU can inform us of the context status via
252 * interrupts.
253 *
254 * Return: 20-bits globally unique context ID.
255 */
Ben Widawsky84b790f2014-07-24 17:04:36 +0100256u32 intel_execlists_ctx_id(struct drm_i915_gem_object *ctx_obj)
257{
258 u32 lrca = i915_gem_obj_ggtt_offset(ctx_obj);
259
260 /* LRCA is required to be 4K aligned so the more significant 20 bits
261 * are globally unique */
262 return lrca >> 12;
263}
264
Mika Kuoppala8ee36152015-07-03 17:09:37 +0300265static uint64_t execlists_ctx_descriptor(struct drm_i915_gem_request *rq)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100266{
Mika Kuoppala8ee36152015-07-03 17:09:37 +0300267 struct intel_engine_cs *ring = rq->ring;
Nick Hoath203a5712015-02-06 11:30:04 +0000268 struct drm_device *dev = ring->dev;
Mika Kuoppala8ee36152015-07-03 17:09:37 +0300269 struct drm_i915_gem_object *ctx_obj = rq->ctx->engine[ring->id].state;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100270 uint64_t desc;
271 uint64_t lrca = i915_gem_obj_ggtt_offset(ctx_obj);
Michel Thierryacdd8842014-07-24 17:04:38 +0100272
273 WARN_ON(lrca & 0xFFFFFFFF00000FFFULL);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100274
275 desc = GEN8_CTX_VALID;
276 desc |= LEGACY_CONTEXT << GEN8_CTX_MODE_SHIFT;
Arun Siluvery51847fb2015-04-07 14:01:33 +0100277 if (IS_GEN8(ctx_obj->base.dev))
278 desc |= GEN8_CTX_L3LLC_COHERENT;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100279 desc |= GEN8_CTX_PRIVILEGE;
280 desc |= lrca;
281 desc |= (u64)intel_execlists_ctx_id(ctx_obj) << GEN8_CTX_ID_SHIFT;
282
283 /* TODO: WaDisableLiteRestore when we start using semaphore
284 * signalling between Command Streamers */
285 /* desc |= GEN8_CTX_FORCE_RESTORE; */
286
Nick Hoath203a5712015-02-06 11:30:04 +0000287 /* WaEnableForceRestoreInCtxtDescForVCS:skl */
288 if (IS_GEN9(dev) &&
289 INTEL_REVID(dev) <= SKL_REVID_B0 &&
290 (ring->id == BCS || ring->id == VCS ||
291 ring->id == VECS || ring->id == VCS2))
292 desc |= GEN8_CTX_FORCE_RESTORE;
293
Ben Widawsky84b790f2014-07-24 17:04:36 +0100294 return desc;
295}
296
Mika Kuoppalacc3c4252015-07-03 17:09:36 +0300297static void execlists_elsp_write(struct drm_i915_gem_request *rq0,
298 struct drm_i915_gem_request *rq1)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100299{
Mika Kuoppalacc3c4252015-07-03 17:09:36 +0300300
301 struct intel_engine_cs *ring = rq0->ring;
Tvrtko Ursulin6e7cc472014-11-13 17:51:51 +0000302 struct drm_device *dev = ring->dev;
303 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppala1cff8cc2015-07-06 11:09:25 +0300304 uint64_t desc[2];
Ben Widawsky84b790f2014-07-24 17:04:36 +0100305
Mika Kuoppala1cff8cc2015-07-06 11:09:25 +0300306 if (rq1) {
307 desc[1] = execlists_ctx_descriptor(rq1);
308 rq1->elsp_submitted++;
309 } else {
310 desc[1] = 0;
311 }
Ben Widawsky84b790f2014-07-24 17:04:36 +0100312
Mika Kuoppala1cff8cc2015-07-06 11:09:25 +0300313 desc[0] = execlists_ctx_descriptor(rq0);
314 rq0->elsp_submitted++;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100315
Mika Kuoppala1cff8cc2015-07-06 11:09:25 +0300316 /* You must always write both descriptors in the order below. */
Chris Wilsona6111f72015-04-07 16:21:02 +0100317 spin_lock(&dev_priv->uncore.lock);
318 intel_uncore_forcewake_get__locked(dev_priv, FORCEWAKE_ALL);
Mika Kuoppala1cff8cc2015-07-06 11:09:25 +0300319 I915_WRITE_FW(RING_ELSP(ring), upper_32_bits(desc[1]));
320 I915_WRITE_FW(RING_ELSP(ring), lower_32_bits(desc[1]));
Chris Wilson6daccb02015-01-16 11:34:35 +0200321
Mika Kuoppala1cff8cc2015-07-06 11:09:25 +0300322 I915_WRITE_FW(RING_ELSP(ring), upper_32_bits(desc[0]));
Ben Widawsky84b790f2014-07-24 17:04:36 +0100323 /* The context is automatically loaded after the following */
Mika Kuoppala1cff8cc2015-07-06 11:09:25 +0300324 I915_WRITE_FW(RING_ELSP(ring), lower_32_bits(desc[0]));
Ben Widawsky84b790f2014-07-24 17:04:36 +0100325
Mika Kuoppala1cff8cc2015-07-06 11:09:25 +0300326 /* ELSP is a wo register, use another nearby reg for posting */
Chris Wilsona6111f72015-04-07 16:21:02 +0100327 POSTING_READ_FW(RING_EXECLIST_STATUS(ring));
328 intel_uncore_forcewake_put__locked(dev_priv, FORCEWAKE_ALL);
329 spin_unlock(&dev_priv->uncore.lock);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100330}
331
Mika Kuoppala05d98242015-07-03 17:09:33 +0300332static int execlists_update_context(struct drm_i915_gem_request *rq)
Oscar Mateoae1250b2014-07-24 17:04:37 +0100333{
Mika Kuoppala05d98242015-07-03 17:09:33 +0300334 struct intel_engine_cs *ring = rq->ring;
335 struct i915_hw_ppgtt *ppgtt = rq->ctx->ppgtt;
336 struct drm_i915_gem_object *ctx_obj = rq->ctx->engine[ring->id].state;
337 struct drm_i915_gem_object *rb_obj = rq->ringbuf->obj;
Oscar Mateoae1250b2014-07-24 17:04:37 +0100338 struct page *page;
339 uint32_t *reg_state;
340
Mika Kuoppala05d98242015-07-03 17:09:33 +0300341 BUG_ON(!ctx_obj);
342 WARN_ON(!i915_gem_obj_is_pinned(ctx_obj));
343 WARN_ON(!i915_gem_obj_is_pinned(rb_obj));
344
Oscar Mateoae1250b2014-07-24 17:04:37 +0100345 page = i915_gem_object_get_page(ctx_obj, 1);
346 reg_state = kmap_atomic(page);
347
Mika Kuoppala05d98242015-07-03 17:09:33 +0300348 reg_state[CTX_RING_TAIL+1] = rq->tail;
349 reg_state[CTX_RING_BUFFER_START+1] = i915_gem_obj_ggtt_offset(rb_obj);
Oscar Mateoae1250b2014-07-24 17:04:37 +0100350
Michel Thierryd7b26332015-04-08 12:13:34 +0100351 /* True PPGTT with dynamic page allocation: update PDP registers and
352 * point the unallocated PDPs to the scratch page
353 */
354 if (ppgtt) {
355 ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
356 ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
357 ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
358 ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
359 }
360
Oscar Mateoae1250b2014-07-24 17:04:37 +0100361 kunmap_atomic(reg_state);
362
363 return 0;
364}
365
Mika Kuoppalad8cb8872015-07-03 17:09:32 +0300366static void execlists_submit_requests(struct drm_i915_gem_request *rq0,
367 struct drm_i915_gem_request *rq1)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100368{
Mika Kuoppala05d98242015-07-03 17:09:33 +0300369 execlists_update_context(rq0);
Oscar Mateoae1250b2014-07-24 17:04:37 +0100370
Mika Kuoppalacc3c4252015-07-03 17:09:36 +0300371 if (rq1)
Mika Kuoppala05d98242015-07-03 17:09:33 +0300372 execlists_update_context(rq1);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100373
Mika Kuoppalacc3c4252015-07-03 17:09:36 +0300374 execlists_elsp_write(rq0, rq1);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100375}
376
Michel Thierryacdd8842014-07-24 17:04:38 +0100377static void execlists_context_unqueue(struct intel_engine_cs *ring)
378{
Nick Hoath6d3d8272015-01-15 13:10:39 +0000379 struct drm_i915_gem_request *req0 = NULL, *req1 = NULL;
380 struct drm_i915_gem_request *cursor = NULL, *tmp = NULL;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100381
382 assert_spin_locked(&ring->execlist_lock);
Michel Thierryacdd8842014-07-24 17:04:38 +0100383
Peter Antoine779949f2015-05-11 16:03:27 +0100384 /*
385 * If irqs are not active generate a warning as batches that finish
386 * without the irqs may get lost and a GPU Hang may occur.
387 */
388 WARN_ON(!intel_irqs_enabled(ring->dev->dev_private));
389
Michel Thierryacdd8842014-07-24 17:04:38 +0100390 if (list_empty(&ring->execlist_queue))
391 return;
392
393 /* Try to read in pairs */
394 list_for_each_entry_safe(cursor, tmp, &ring->execlist_queue,
395 execlist_link) {
396 if (!req0) {
397 req0 = cursor;
Nick Hoath6d3d8272015-01-15 13:10:39 +0000398 } else if (req0->ctx == cursor->ctx) {
Michel Thierryacdd8842014-07-24 17:04:38 +0100399 /* Same ctx: ignore first request, as second request
400 * will update tail past first request's workload */
Oscar Mateoe1fee722014-07-24 17:04:40 +0100401 cursor->elsp_submitted = req0->elsp_submitted;
Michel Thierryacdd8842014-07-24 17:04:38 +0100402 list_del(&req0->execlist_link);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000403 list_add_tail(&req0->execlist_link,
404 &ring->execlist_retired_req_list);
Michel Thierryacdd8842014-07-24 17:04:38 +0100405 req0 = cursor;
406 } else {
407 req1 = cursor;
408 break;
409 }
410 }
411
Michel Thierry53292cd2015-04-15 18:11:33 +0100412 if (IS_GEN8(ring->dev) || IS_GEN9(ring->dev)) {
413 /*
414 * WaIdleLiteRestore: make sure we never cause a lite
415 * restore with HEAD==TAIL
416 */
Michel Thierryd63f8202015-04-27 12:31:44 +0100417 if (req0->elsp_submitted) {
Michel Thierry53292cd2015-04-15 18:11:33 +0100418 /*
419 * Apply the wa NOOPS to prevent ring:HEAD == req:TAIL
420 * as we resubmit the request. See gen8_emit_request()
421 * for where we prepare the padding after the end of the
422 * request.
423 */
424 struct intel_ringbuffer *ringbuf;
425
426 ringbuf = req0->ctx->engine[ring->id].ringbuf;
427 req0->tail += 8;
428 req0->tail &= ringbuf->size - 1;
429 }
430 }
431
Oscar Mateoe1fee722014-07-24 17:04:40 +0100432 WARN_ON(req1 && req1->elsp_submitted);
433
Mika Kuoppalad8cb8872015-07-03 17:09:32 +0300434 execlists_submit_requests(req0, req1);
Michel Thierryacdd8842014-07-24 17:04:38 +0100435}
436
Thomas Daniele981e7b2014-07-24 17:04:39 +0100437static bool execlists_check_remove_request(struct intel_engine_cs *ring,
438 u32 request_id)
439{
Nick Hoath6d3d8272015-01-15 13:10:39 +0000440 struct drm_i915_gem_request *head_req;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100441
442 assert_spin_locked(&ring->execlist_lock);
443
444 head_req = list_first_entry_or_null(&ring->execlist_queue,
Nick Hoath6d3d8272015-01-15 13:10:39 +0000445 struct drm_i915_gem_request,
Thomas Daniele981e7b2014-07-24 17:04:39 +0100446 execlist_link);
447
448 if (head_req != NULL) {
449 struct drm_i915_gem_object *ctx_obj =
Nick Hoath6d3d8272015-01-15 13:10:39 +0000450 head_req->ctx->engine[ring->id].state;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100451 if (intel_execlists_ctx_id(ctx_obj) == request_id) {
Oscar Mateoe1fee722014-07-24 17:04:40 +0100452 WARN(head_req->elsp_submitted == 0,
453 "Never submitted head request\n");
454
455 if (--head_req->elsp_submitted <= 0) {
456 list_del(&head_req->execlist_link);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000457 list_add_tail(&head_req->execlist_link,
458 &ring->execlist_retired_req_list);
Oscar Mateoe1fee722014-07-24 17:04:40 +0100459 return true;
460 }
Thomas Daniele981e7b2014-07-24 17:04:39 +0100461 }
462 }
463
464 return false;
465}
466
Oscar Mateo73e4d072014-07-24 17:04:48 +0100467/**
Daniel Vetter3f7531c2014-12-10 17:41:43 +0100468 * intel_lrc_irq_handler() - handle Context Switch interrupts
Oscar Mateo73e4d072014-07-24 17:04:48 +0100469 * @ring: Engine Command Streamer to handle.
470 *
471 * Check the unread Context Status Buffers and manage the submission of new
472 * contexts to the ELSP accordingly.
473 */
Daniel Vetter3f7531c2014-12-10 17:41:43 +0100474void intel_lrc_irq_handler(struct intel_engine_cs *ring)
Thomas Daniele981e7b2014-07-24 17:04:39 +0100475{
476 struct drm_i915_private *dev_priv = ring->dev->dev_private;
477 u32 status_pointer;
478 u8 read_pointer;
479 u8 write_pointer;
480 u32 status;
481 u32 status_id;
482 u32 submit_contexts = 0;
483
484 status_pointer = I915_READ(RING_CONTEXT_STATUS_PTR(ring));
485
486 read_pointer = ring->next_context_status_buffer;
487 write_pointer = status_pointer & 0x07;
488 if (read_pointer > write_pointer)
489 write_pointer += 6;
490
491 spin_lock(&ring->execlist_lock);
492
493 while (read_pointer < write_pointer) {
494 read_pointer++;
495 status = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
496 (read_pointer % 6) * 8);
497 status_id = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
498 (read_pointer % 6) * 8 + 4);
499
Mika Kuoppala031a8932015-08-06 17:09:17 +0300500 if (status & GEN8_CTX_STATUS_IDLE_ACTIVE)
501 continue;
502
Oscar Mateoe1fee722014-07-24 17:04:40 +0100503 if (status & GEN8_CTX_STATUS_PREEMPTED) {
504 if (status & GEN8_CTX_STATUS_LITE_RESTORE) {
505 if (execlists_check_remove_request(ring, status_id))
506 WARN(1, "Lite Restored request removed from queue\n");
507 } else
508 WARN(1, "Preemption without Lite Restore\n");
509 }
510
511 if ((status & GEN8_CTX_STATUS_ACTIVE_IDLE) ||
512 (status & GEN8_CTX_STATUS_ELEMENT_SWITCH)) {
Thomas Daniele981e7b2014-07-24 17:04:39 +0100513 if (execlists_check_remove_request(ring, status_id))
514 submit_contexts++;
515 }
516 }
517
518 if (submit_contexts != 0)
519 execlists_context_unqueue(ring);
520
521 spin_unlock(&ring->execlist_lock);
522
523 WARN(submit_contexts > 2, "More than two context complete events?\n");
524 ring->next_context_status_buffer = write_pointer % 6;
525
526 I915_WRITE(RING_CONTEXT_STATUS_PTR(ring),
Mika Kuoppalacc536992015-08-06 17:00:59 +0300527 _MASKED_FIELD(0x07 << 8, ((u32)ring->next_context_status_buffer & 0x07) << 8));
Thomas Daniele981e7b2014-07-24 17:04:39 +0100528}
529
John Harrisonae707972015-05-29 17:44:14 +0100530static int execlists_context_queue(struct drm_i915_gem_request *request)
Michel Thierryacdd8842014-07-24 17:04:38 +0100531{
John Harrisonae707972015-05-29 17:44:14 +0100532 struct intel_engine_cs *ring = request->ring;
Nick Hoath6d3d8272015-01-15 13:10:39 +0000533 struct drm_i915_gem_request *cursor;
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100534 int num_elements = 0;
Michel Thierryacdd8842014-07-24 17:04:38 +0100535
John Harrisonae707972015-05-29 17:44:14 +0100536 if (request->ctx != ring->default_context)
Mika Kuoppala8ba319d2015-07-03 17:09:35 +0300537 intel_lr_context_pin(request);
John Harrison9bb1af42015-05-29 17:44:13 +0100538
539 i915_gem_request_reference(request);
540
John Harrisonae707972015-05-29 17:44:14 +0100541 request->tail = request->ringbuf->tail;
Nick Hoath2d129552015-01-15 13:10:36 +0000542
Chris Wilsonb5eba372015-04-07 16:20:48 +0100543 spin_lock_irq(&ring->execlist_lock);
Michel Thierryacdd8842014-07-24 17:04:38 +0100544
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100545 list_for_each_entry(cursor, &ring->execlist_queue, execlist_link)
546 if (++num_elements > 2)
547 break;
548
549 if (num_elements > 2) {
Nick Hoath6d3d8272015-01-15 13:10:39 +0000550 struct drm_i915_gem_request *tail_req;
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100551
552 tail_req = list_last_entry(&ring->execlist_queue,
Nick Hoath6d3d8272015-01-15 13:10:39 +0000553 struct drm_i915_gem_request,
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100554 execlist_link);
555
John Harrisonae707972015-05-29 17:44:14 +0100556 if (request->ctx == tail_req->ctx) {
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100557 WARN(tail_req->elsp_submitted != 0,
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000558 "More than 2 already-submitted reqs queued\n");
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100559 list_del(&tail_req->execlist_link);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000560 list_add_tail(&tail_req->execlist_link,
561 &ring->execlist_retired_req_list);
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100562 }
563 }
564
Nick Hoath6d3d8272015-01-15 13:10:39 +0000565 list_add_tail(&request->execlist_link, &ring->execlist_queue);
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100566 if (num_elements == 0)
Michel Thierryacdd8842014-07-24 17:04:38 +0100567 execlists_context_unqueue(ring);
568
Chris Wilsonb5eba372015-04-07 16:20:48 +0100569 spin_unlock_irq(&ring->execlist_lock);
Michel Thierryacdd8842014-07-24 17:04:38 +0100570
571 return 0;
572}
573
John Harrison2f200552015-05-29 17:43:53 +0100574static int logical_ring_invalidate_all_caches(struct drm_i915_gem_request *req)
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100575{
John Harrison2f200552015-05-29 17:43:53 +0100576 struct intel_engine_cs *ring = req->ring;
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100577 uint32_t flush_domains;
578 int ret;
579
580 flush_domains = 0;
581 if (ring->gpu_caches_dirty)
582 flush_domains = I915_GEM_GPU_DOMAINS;
583
John Harrison7deb4d32015-05-29 17:43:59 +0100584 ret = ring->emit_flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100585 if (ret)
586 return ret;
587
588 ring->gpu_caches_dirty = false;
589 return 0;
590}
591
John Harrison535fbe82015-05-29 17:43:32 +0100592static int execlists_move_to_gpu(struct drm_i915_gem_request *req,
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100593 struct list_head *vmas)
594{
John Harrison535fbe82015-05-29 17:43:32 +0100595 const unsigned other_rings = ~intel_ring_flag(req->ring);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100596 struct i915_vma *vma;
597 uint32_t flush_domains = 0;
598 bool flush_chipset = false;
599 int ret;
600
601 list_for_each_entry(vma, vmas, exec_list) {
602 struct drm_i915_gem_object *obj = vma->obj;
603
Chris Wilson03ade512015-04-27 13:41:18 +0100604 if (obj->active & other_rings) {
John Harrison91af1272015-06-18 13:14:56 +0100605 ret = i915_gem_object_sync(obj, req->ring, &req);
Chris Wilson03ade512015-04-27 13:41:18 +0100606 if (ret)
607 return ret;
608 }
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100609
610 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
611 flush_chipset |= i915_gem_clflush_object(obj, false);
612
613 flush_domains |= obj->base.write_domain;
614 }
615
616 if (flush_domains & I915_GEM_DOMAIN_GTT)
617 wmb();
618
619 /* Unconditionally invalidate gpu caches and ensure that we do flush
620 * any residual writes from the previous batch.
621 */
John Harrison2f200552015-05-29 17:43:53 +0100622 return logical_ring_invalidate_all_caches(req);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100623}
624
John Harrison40e895c2015-05-29 17:43:26 +0100625int intel_logical_ring_alloc_request_extras(struct drm_i915_gem_request *request)
John Harrisonbc0dce32015-03-19 12:30:07 +0000626{
John Harrisonbc0dce32015-03-19 12:30:07 +0000627 int ret;
628
Mika Kuoppalaf3cc01f2015-07-06 11:08:30 +0300629 request->ringbuf = request->ctx->engine[request->ring->id].ringbuf;
630
John Harrison40e895c2015-05-29 17:43:26 +0100631 if (request->ctx != request->ring->default_context) {
Mika Kuoppala8ba319d2015-07-03 17:09:35 +0300632 ret = intel_lr_context_pin(request);
John Harrison6689cb22015-03-19 12:30:08 +0000633 if (ret)
John Harrisonbc0dce32015-03-19 12:30:07 +0000634 return ret;
John Harrisonbc0dce32015-03-19 12:30:07 +0000635 }
636
John Harrisonbc0dce32015-03-19 12:30:07 +0000637 return 0;
638}
639
John Harrisonae707972015-05-29 17:44:14 +0100640static int logical_ring_wait_for_space(struct drm_i915_gem_request *req,
Chris Wilson595e1ee2015-04-07 16:20:51 +0100641 int bytes)
John Harrisonbc0dce32015-03-19 12:30:07 +0000642{
John Harrisonae707972015-05-29 17:44:14 +0100643 struct intel_ringbuffer *ringbuf = req->ringbuf;
644 struct intel_engine_cs *ring = req->ring;
645 struct drm_i915_gem_request *target;
Chris Wilsonb4716182015-04-27 13:41:17 +0100646 unsigned space;
647 int ret;
John Harrisonbc0dce32015-03-19 12:30:07 +0000648
649 if (intel_ring_space(ringbuf) >= bytes)
650 return 0;
651
John Harrison79bbcc22015-06-30 12:40:55 +0100652 /* The whole point of reserving space is to not wait! */
653 WARN_ON(ringbuf->reserved_in_use);
654
John Harrisonae707972015-05-29 17:44:14 +0100655 list_for_each_entry(target, &ring->request_list, list) {
John Harrisonbc0dce32015-03-19 12:30:07 +0000656 /*
657 * The request queue is per-engine, so can contain requests
658 * from multiple ringbuffers. Here, we must ignore any that
659 * aren't from the ringbuffer we're considering.
660 */
John Harrisonae707972015-05-29 17:44:14 +0100661 if (target->ringbuf != ringbuf)
John Harrisonbc0dce32015-03-19 12:30:07 +0000662 continue;
663
664 /* Would completion of this request free enough space? */
John Harrisonae707972015-05-29 17:44:14 +0100665 space = __intel_ring_space(target->postfix, ringbuf->tail,
Chris Wilsonb4716182015-04-27 13:41:17 +0100666 ringbuf->size);
667 if (space >= bytes)
John Harrisonbc0dce32015-03-19 12:30:07 +0000668 break;
John Harrisonbc0dce32015-03-19 12:30:07 +0000669 }
670
John Harrisonae707972015-05-29 17:44:14 +0100671 if (WARN_ON(&target->list == &ring->request_list))
John Harrisonbc0dce32015-03-19 12:30:07 +0000672 return -ENOSPC;
673
John Harrisonae707972015-05-29 17:44:14 +0100674 ret = i915_wait_request(target);
John Harrisonbc0dce32015-03-19 12:30:07 +0000675 if (ret)
676 return ret;
677
Chris Wilsonb4716182015-04-27 13:41:17 +0100678 ringbuf->space = space;
679 return 0;
John Harrisonbc0dce32015-03-19 12:30:07 +0000680}
681
682/*
683 * intel_logical_ring_advance_and_submit() - advance the tail and submit the workload
John Harrisonae707972015-05-29 17:44:14 +0100684 * @request: Request to advance the logical ringbuffer of.
John Harrisonbc0dce32015-03-19 12:30:07 +0000685 *
686 * The tail is updated in our logical ringbuffer struct, not in the actual context. What
687 * really happens during submission is that the context and current tail will be placed
688 * on a queue waiting for the ELSP to be ready to accept a new context submission. At that
689 * point, the tail *inside* the context is updated and the ELSP written to.
690 */
691static void
John Harrisonae707972015-05-29 17:44:14 +0100692intel_logical_ring_advance_and_submit(struct drm_i915_gem_request *request)
John Harrisonbc0dce32015-03-19 12:30:07 +0000693{
John Harrisonae707972015-05-29 17:44:14 +0100694 struct intel_engine_cs *ring = request->ring;
John Harrisonbc0dce32015-03-19 12:30:07 +0000695
John Harrisonae707972015-05-29 17:44:14 +0100696 intel_logical_ring_advance(request->ringbuf);
John Harrisonbc0dce32015-03-19 12:30:07 +0000697
698 if (intel_ring_stopped(ring))
699 return;
700
John Harrisonae707972015-05-29 17:44:14 +0100701 execlists_context_queue(request);
John Harrisonbc0dce32015-03-19 12:30:07 +0000702}
703
John Harrison79bbcc22015-06-30 12:40:55 +0100704static void __wrap_ring_buffer(struct intel_ringbuffer *ringbuf)
John Harrisonbc0dce32015-03-19 12:30:07 +0000705{
706 uint32_t __iomem *virt;
707 int rem = ringbuf->size - ringbuf->tail;
708
John Harrisonbc0dce32015-03-19 12:30:07 +0000709 virt = ringbuf->virtual_start + ringbuf->tail;
710 rem /= 4;
711 while (rem--)
712 iowrite32(MI_NOOP, virt++);
713
714 ringbuf->tail = 0;
715 intel_ring_update_space(ringbuf);
John Harrisonbc0dce32015-03-19 12:30:07 +0000716}
717
John Harrisonae707972015-05-29 17:44:14 +0100718static int logical_ring_prepare(struct drm_i915_gem_request *req, int bytes)
John Harrisonbc0dce32015-03-19 12:30:07 +0000719{
John Harrisonae707972015-05-29 17:44:14 +0100720 struct intel_ringbuffer *ringbuf = req->ringbuf;
John Harrison79bbcc22015-06-30 12:40:55 +0100721 int remain_usable = ringbuf->effective_size - ringbuf->tail;
722 int remain_actual = ringbuf->size - ringbuf->tail;
723 int ret, total_bytes, wait_bytes = 0;
724 bool need_wrap = false;
John Harrisonbc0dce32015-03-19 12:30:07 +0000725
John Harrison79bbcc22015-06-30 12:40:55 +0100726 if (ringbuf->reserved_in_use)
727 total_bytes = bytes;
728 else
729 total_bytes = bytes + ringbuf->reserved_size;
John Harrison29b1b412015-06-18 13:10:09 +0100730
John Harrison79bbcc22015-06-30 12:40:55 +0100731 if (unlikely(bytes > remain_usable)) {
732 /*
733 * Not enough space for the basic request. So need to flush
734 * out the remainder and then wait for base + reserved.
735 */
736 wait_bytes = remain_actual + total_bytes;
737 need_wrap = true;
738 } else {
739 if (unlikely(total_bytes > remain_usable)) {
740 /*
741 * The base request will fit but the reserved space
742 * falls off the end. So only need to to wait for the
743 * reserved size after flushing out the remainder.
744 */
745 wait_bytes = remain_actual + ringbuf->reserved_size;
746 need_wrap = true;
747 } else if (total_bytes > ringbuf->space) {
748 /* No wrapping required, just waiting. */
749 wait_bytes = total_bytes;
John Harrison29b1b412015-06-18 13:10:09 +0100750 }
John Harrisonbc0dce32015-03-19 12:30:07 +0000751 }
752
John Harrison79bbcc22015-06-30 12:40:55 +0100753 if (wait_bytes) {
754 ret = logical_ring_wait_for_space(req, wait_bytes);
John Harrisonbc0dce32015-03-19 12:30:07 +0000755 if (unlikely(ret))
756 return ret;
John Harrison79bbcc22015-06-30 12:40:55 +0100757
758 if (need_wrap)
759 __wrap_ring_buffer(ringbuf);
John Harrisonbc0dce32015-03-19 12:30:07 +0000760 }
761
762 return 0;
763}
764
765/**
766 * intel_logical_ring_begin() - prepare the logical ringbuffer to accept some commands
767 *
John Harrison4d616a22015-05-29 17:44:08 +0100768 * @request: The request to start some new work for
Arun Siluvery4d78c8d2015-06-23 15:50:43 +0100769 * @ctx: Logical ring context whose ringbuffer is being prepared.
John Harrisonbc0dce32015-03-19 12:30:07 +0000770 * @num_dwords: number of DWORDs that we plan to write to the ringbuffer.
771 *
772 * The ringbuffer might not be ready to accept the commands right away (maybe it needs to
773 * be wrapped, or wait a bit for the tail to be updated). This function takes care of that
774 * and also preallocates a request (every workload submission is still mediated through
775 * requests, same as it did with legacy ringbuffer submission).
776 *
777 * Return: non-zero if the ringbuffer is not ready to be written to.
778 */
Peter Antoine3bbaba02015-07-10 20:13:11 +0300779int intel_logical_ring_begin(struct drm_i915_gem_request *req, int num_dwords)
John Harrisonbc0dce32015-03-19 12:30:07 +0000780{
John Harrison4d616a22015-05-29 17:44:08 +0100781 struct drm_i915_private *dev_priv;
John Harrisonbc0dce32015-03-19 12:30:07 +0000782 int ret;
783
John Harrison4d616a22015-05-29 17:44:08 +0100784 WARN_ON(req == NULL);
785 dev_priv = req->ring->dev->dev_private;
786
John Harrisonbc0dce32015-03-19 12:30:07 +0000787 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
788 dev_priv->mm.interruptible);
789 if (ret)
790 return ret;
791
John Harrisonae707972015-05-29 17:44:14 +0100792 ret = logical_ring_prepare(req, num_dwords * sizeof(uint32_t));
John Harrisonbc0dce32015-03-19 12:30:07 +0000793 if (ret)
794 return ret;
795
John Harrison4d616a22015-05-29 17:44:08 +0100796 req->ringbuf->space -= num_dwords * sizeof(uint32_t);
John Harrisonbc0dce32015-03-19 12:30:07 +0000797 return 0;
798}
799
John Harrisonccd98fe2015-05-29 17:44:09 +0100800int intel_logical_ring_reserve_space(struct drm_i915_gem_request *request)
801{
802 /*
803 * The first call merely notes the reserve request and is common for
804 * all back ends. The subsequent localised _begin() call actually
805 * ensures that the reservation is available. Without the begin, if
806 * the request creator immediately submitted the request without
807 * adding any commands to it then there might not actually be
808 * sufficient room for the submission commands.
809 */
810 intel_ring_reserved_space_reserve(request->ringbuf, MIN_SPACE_FOR_ADD_REQUEST);
811
812 return intel_logical_ring_begin(request, 0);
813}
814
Oscar Mateo73e4d072014-07-24 17:04:48 +0100815/**
816 * execlists_submission() - submit a batchbuffer for execution, Execlists style
817 * @dev: DRM device.
818 * @file: DRM file.
819 * @ring: Engine Command Streamer to submit to.
820 * @ctx: Context to employ for this submission.
821 * @args: execbuffer call arguments.
822 * @vmas: list of vmas.
823 * @batch_obj: the batchbuffer to submit.
824 * @exec_start: batchbuffer start virtual address pointer.
John Harrison8e004ef2015-02-13 11:48:10 +0000825 * @dispatch_flags: translated execbuffer call flags.
Oscar Mateo73e4d072014-07-24 17:04:48 +0100826 *
827 * This is the evil twin version of i915_gem_ringbuffer_submission. It abstracts
828 * away the submission details of the execbuffer ioctl call.
829 *
830 * Return: non-zero if the submission fails.
831 */
John Harrison5f19e2b2015-05-29 17:43:27 +0100832int intel_execlists_submission(struct i915_execbuffer_params *params,
Oscar Mateo454afeb2014-07-24 17:04:22 +0100833 struct drm_i915_gem_execbuffer2 *args,
John Harrison5f19e2b2015-05-29 17:43:27 +0100834 struct list_head *vmas)
Oscar Mateo454afeb2014-07-24 17:04:22 +0100835{
John Harrison5f19e2b2015-05-29 17:43:27 +0100836 struct drm_device *dev = params->dev;
837 struct intel_engine_cs *ring = params->ring;
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100838 struct drm_i915_private *dev_priv = dev->dev_private;
John Harrison5f19e2b2015-05-29 17:43:27 +0100839 struct intel_ringbuffer *ringbuf = params->ctx->engine[ring->id].ringbuf;
840 u64 exec_start;
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100841 int instp_mode;
842 u32 instp_mask;
843 int ret;
844
845 instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
846 instp_mask = I915_EXEC_CONSTANTS_MASK;
847 switch (instp_mode) {
848 case I915_EXEC_CONSTANTS_REL_GENERAL:
849 case I915_EXEC_CONSTANTS_ABSOLUTE:
850 case I915_EXEC_CONSTANTS_REL_SURFACE:
851 if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) {
852 DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
853 return -EINVAL;
854 }
855
856 if (instp_mode != dev_priv->relative_constants_mode) {
857 if (instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
858 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
859 return -EINVAL;
860 }
861
862 /* The HW changed the meaning on this bit on gen6 */
863 instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
864 }
865 break;
866 default:
867 DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
868 return -EINVAL;
869 }
870
871 if (args->num_cliprects != 0) {
872 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
873 return -EINVAL;
874 } else {
875 if (args->DR4 == 0xffffffff) {
876 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
877 args->DR4 = 0;
878 }
879
880 if (args->DR1 || args->DR4 || args->cliprects_ptr) {
881 DRM_DEBUG("0 cliprects but dirt in cliprects fields\n");
882 return -EINVAL;
883 }
884 }
885
886 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
887 DRM_DEBUG("sol reset is gen7 only\n");
888 return -EINVAL;
889 }
890
John Harrison535fbe82015-05-29 17:43:32 +0100891 ret = execlists_move_to_gpu(params->request, vmas);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100892 if (ret)
893 return ret;
894
895 if (ring == &dev_priv->ring[RCS] &&
896 instp_mode != dev_priv->relative_constants_mode) {
John Harrison4d616a22015-05-29 17:44:08 +0100897 ret = intel_logical_ring_begin(params->request, 4);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100898 if (ret)
899 return ret;
900
901 intel_logical_ring_emit(ringbuf, MI_NOOP);
902 intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(1));
903 intel_logical_ring_emit(ringbuf, INSTPM);
904 intel_logical_ring_emit(ringbuf, instp_mask << 16 | instp_mode);
905 intel_logical_ring_advance(ringbuf);
906
907 dev_priv->relative_constants_mode = instp_mode;
908 }
909
John Harrison5f19e2b2015-05-29 17:43:27 +0100910 exec_start = params->batch_obj_vm_offset +
911 args->batch_start_offset;
912
John Harrisonbe795fc2015-05-29 17:44:03 +0100913 ret = ring->emit_bb_start(params->request, exec_start, params->dispatch_flags);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100914 if (ret)
915 return ret;
916
John Harrison95c24162015-05-29 17:43:31 +0100917 trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
John Harrison5e4be7b2015-02-13 11:48:11 +0000918
John Harrison8a8edb52015-05-29 17:43:33 +0100919 i915_gem_execbuffer_move_to_active(vmas, params->request);
John Harrisonadeca762015-05-29 17:43:28 +0100920 i915_gem_execbuffer_retire_commands(params);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100921
Oscar Mateo454afeb2014-07-24 17:04:22 +0100922 return 0;
923}
924
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000925void intel_execlists_retire_requests(struct intel_engine_cs *ring)
926{
Nick Hoath6d3d8272015-01-15 13:10:39 +0000927 struct drm_i915_gem_request *req, *tmp;
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000928 struct list_head retired_list;
929
930 WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
931 if (list_empty(&ring->execlist_retired_req_list))
932 return;
933
934 INIT_LIST_HEAD(&retired_list);
Chris Wilsonb5eba372015-04-07 16:20:48 +0100935 spin_lock_irq(&ring->execlist_lock);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000936 list_replace_init(&ring->execlist_retired_req_list, &retired_list);
Chris Wilsonb5eba372015-04-07 16:20:48 +0100937 spin_unlock_irq(&ring->execlist_lock);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000938
939 list_for_each_entry_safe(req, tmp, &retired_list, execlist_link) {
Nick Hoath6d3d8272015-01-15 13:10:39 +0000940 struct intel_context *ctx = req->ctx;
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000941 struct drm_i915_gem_object *ctx_obj =
942 ctx->engine[ring->id].state;
943
944 if (ctx_obj && (ctx != ring->default_context))
Mika Kuoppala8ba319d2015-07-03 17:09:35 +0300945 intel_lr_context_unpin(req);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000946 list_del(&req->execlist_link);
Nick Hoathf8210792015-01-29 16:55:07 +0000947 i915_gem_request_unreference(req);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000948 }
949}
950
Oscar Mateo454afeb2014-07-24 17:04:22 +0100951void intel_logical_ring_stop(struct intel_engine_cs *ring)
952{
Oscar Mateo9832b9d2014-07-24 17:04:30 +0100953 struct drm_i915_private *dev_priv = ring->dev->dev_private;
954 int ret;
955
956 if (!intel_ring_initialized(ring))
957 return;
958
959 ret = intel_ring_idle(ring);
960 if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
961 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
962 ring->name, ret);
963
964 /* TODO: Is this correct with Execlists enabled? */
965 I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
966 if (wait_for_atomic((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
967 DRM_ERROR("%s :timed out trying to stop ring\n", ring->name);
968 return;
969 }
970 I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
Oscar Mateo454afeb2014-07-24 17:04:22 +0100971}
972
John Harrison4866d722015-05-29 17:43:55 +0100973int logical_ring_flush_all_caches(struct drm_i915_gem_request *req)
Oscar Mateo48e29f52014-07-24 17:04:29 +0100974{
John Harrison4866d722015-05-29 17:43:55 +0100975 struct intel_engine_cs *ring = req->ring;
Oscar Mateo48e29f52014-07-24 17:04:29 +0100976 int ret;
977
978 if (!ring->gpu_caches_dirty)
979 return 0;
980
John Harrison7deb4d32015-05-29 17:43:59 +0100981 ret = ring->emit_flush(req, 0, I915_GEM_GPU_DOMAINS);
Oscar Mateo48e29f52014-07-24 17:04:29 +0100982 if (ret)
983 return ret;
984
985 ring->gpu_caches_dirty = false;
986 return 0;
987}
988
Mika Kuoppala8ba319d2015-07-03 17:09:35 +0300989static int intel_lr_context_pin(struct drm_i915_gem_request *rq)
Oscar Mateodcb4c122014-11-13 10:28:10 +0000990{
Mika Kuoppala8ba319d2015-07-03 17:09:35 +0300991 struct intel_engine_cs *ring = rq->ring;
992 struct drm_i915_gem_object *ctx_obj = rq->ctx->engine[ring->id].state;
993 struct intel_ringbuffer *ringbuf = rq->ringbuf;
Oscar Mateodcb4c122014-11-13 10:28:10 +0000994 int ret = 0;
995
996 WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
Mika Kuoppala8ba319d2015-07-03 17:09:35 +0300997 if (rq->ctx->engine[ring->id].pin_count++ == 0) {
Oscar Mateodcb4c122014-11-13 10:28:10 +0000998 ret = i915_gem_obj_ggtt_pin(ctx_obj,
999 GEN8_LR_CONTEXT_ALIGN, 0);
1000 if (ret)
Mika Kuoppalaa7cbede2015-01-13 11:32:25 +02001001 goto reset_pin_count;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001002
1003 ret = intel_pin_and_map_ringbuffer_obj(ring->dev, ringbuf);
1004 if (ret)
1005 goto unpin_ctx_obj;
Oscar Mateodcb4c122014-11-13 10:28:10 +00001006 }
1007
1008 return ret;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001009
1010unpin_ctx_obj:
1011 i915_gem_object_ggtt_unpin(ctx_obj);
Mika Kuoppalaa7cbede2015-01-13 11:32:25 +02001012reset_pin_count:
Mika Kuoppala8ba319d2015-07-03 17:09:35 +03001013 rq->ctx->engine[ring->id].pin_count = 0;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001014
1015 return ret;
Oscar Mateodcb4c122014-11-13 10:28:10 +00001016}
1017
Mika Kuoppala8ba319d2015-07-03 17:09:35 +03001018void intel_lr_context_unpin(struct drm_i915_gem_request *rq)
Oscar Mateodcb4c122014-11-13 10:28:10 +00001019{
Mika Kuoppala8ba319d2015-07-03 17:09:35 +03001020 struct intel_engine_cs *ring = rq->ring;
1021 struct drm_i915_gem_object *ctx_obj = rq->ctx->engine[ring->id].state;
1022 struct intel_ringbuffer *ringbuf = rq->ringbuf;
Oscar Mateodcb4c122014-11-13 10:28:10 +00001023
1024 if (ctx_obj) {
1025 WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
Mika Kuoppala8ba319d2015-07-03 17:09:35 +03001026 if (--rq->ctx->engine[ring->id].pin_count == 0) {
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001027 intel_unpin_ringbuffer_obj(ringbuf);
Oscar Mateodcb4c122014-11-13 10:28:10 +00001028 i915_gem_object_ggtt_unpin(ctx_obj);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001029 }
Oscar Mateodcb4c122014-11-13 10:28:10 +00001030 }
1031}
1032
John Harrisone2be4fa2015-05-29 17:43:54 +01001033static int intel_logical_ring_workarounds_emit(struct drm_i915_gem_request *req)
Michel Thierry771b9a52014-11-11 16:47:33 +00001034{
1035 int ret, i;
John Harrisone2be4fa2015-05-29 17:43:54 +01001036 struct intel_engine_cs *ring = req->ring;
1037 struct intel_ringbuffer *ringbuf = req->ringbuf;
Michel Thierry771b9a52014-11-11 16:47:33 +00001038 struct drm_device *dev = ring->dev;
1039 struct drm_i915_private *dev_priv = dev->dev_private;
1040 struct i915_workarounds *w = &dev_priv->workarounds;
1041
Michel Thierrye6c1abb2014-11-26 14:21:02 +00001042 if (WARN_ON_ONCE(w->count == 0))
Michel Thierry771b9a52014-11-11 16:47:33 +00001043 return 0;
1044
1045 ring->gpu_caches_dirty = true;
John Harrison4866d722015-05-29 17:43:55 +01001046 ret = logical_ring_flush_all_caches(req);
Michel Thierry771b9a52014-11-11 16:47:33 +00001047 if (ret)
1048 return ret;
1049
John Harrison4d616a22015-05-29 17:44:08 +01001050 ret = intel_logical_ring_begin(req, w->count * 2 + 2);
Michel Thierry771b9a52014-11-11 16:47:33 +00001051 if (ret)
1052 return ret;
1053
1054 intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(w->count));
1055 for (i = 0; i < w->count; i++) {
1056 intel_logical_ring_emit(ringbuf, w->reg[i].addr);
1057 intel_logical_ring_emit(ringbuf, w->reg[i].value);
1058 }
1059 intel_logical_ring_emit(ringbuf, MI_NOOP);
1060
1061 intel_logical_ring_advance(ringbuf);
1062
1063 ring->gpu_caches_dirty = true;
John Harrison4866d722015-05-29 17:43:55 +01001064 ret = logical_ring_flush_all_caches(req);
Michel Thierry771b9a52014-11-11 16:47:33 +00001065 if (ret)
1066 return ret;
1067
1068 return 0;
1069}
1070
Arun Siluvery83b8a982015-07-08 10:27:05 +01001071#define wa_ctx_emit(batch, index, cmd) \
Arun Siluvery17ee9502015-06-19 19:07:01 +01001072 do { \
Arun Siluvery83b8a982015-07-08 10:27:05 +01001073 int __index = (index)++; \
1074 if (WARN_ON(__index >= (PAGE_SIZE / sizeof(uint32_t)))) { \
Arun Siluvery17ee9502015-06-19 19:07:01 +01001075 return -ENOSPC; \
1076 } \
Arun Siluvery83b8a982015-07-08 10:27:05 +01001077 batch[__index] = (cmd); \
Arun Siluvery17ee9502015-06-19 19:07:01 +01001078 } while (0)
1079
Arun Siluvery9e000842015-07-03 14:27:31 +01001080
1081/*
1082 * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
1083 * PIPE_CONTROL instruction. This is required for the flush to happen correctly
1084 * but there is a slight complication as this is applied in WA batch where the
1085 * values are only initialized once so we cannot take register value at the
1086 * beginning and reuse it further; hence we save its value to memory, upload a
1087 * constant value with bit21 set and then we restore it back with the saved value.
1088 * To simplify the WA, a constant value is formed by using the default value
1089 * of this register. This shouldn't be a problem because we are only modifying
1090 * it for a short period and this batch in non-premptible. We can ofcourse
1091 * use additional instructions that read the actual value of the register
1092 * at that time and set our bit of interest but it makes the WA complicated.
1093 *
1094 * This WA is also required for Gen9 so extracting as a function avoids
1095 * code duplication.
1096 */
1097static inline int gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *ring,
1098 uint32_t *const batch,
1099 uint32_t index)
1100{
1101 uint32_t l3sqc4_flush = (0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES);
1102
Arun Siluverya4106a72015-07-14 15:01:29 +01001103 /*
1104 * WaDisableLSQCROPERFforOCL:skl
1105 * This WA is implemented in skl_init_clock_gating() but since
1106 * this batch updates GEN8_L3SQCREG4 with default value we need to
1107 * set this bit here to retain the WA during flush.
1108 */
1109 if (IS_SKYLAKE(ring->dev) && INTEL_REVID(ring->dev) <= SKL_REVID_E0)
1110 l3sqc4_flush |= GEN8_LQSC_RO_PERF_DIS;
1111
Arun Siluvery83b8a982015-07-08 10:27:05 +01001112 wa_ctx_emit(batch, index, (MI_STORE_REGISTER_MEM_GEN8(1) |
1113 MI_SRM_LRM_GLOBAL_GTT));
1114 wa_ctx_emit(batch, index, GEN8_L3SQCREG4);
1115 wa_ctx_emit(batch, index, ring->scratch.gtt_offset + 256);
1116 wa_ctx_emit(batch, index, 0);
Arun Siluvery9e000842015-07-03 14:27:31 +01001117
Arun Siluvery83b8a982015-07-08 10:27:05 +01001118 wa_ctx_emit(batch, index, MI_LOAD_REGISTER_IMM(1));
1119 wa_ctx_emit(batch, index, GEN8_L3SQCREG4);
1120 wa_ctx_emit(batch, index, l3sqc4_flush);
Arun Siluvery9e000842015-07-03 14:27:31 +01001121
Arun Siluvery83b8a982015-07-08 10:27:05 +01001122 wa_ctx_emit(batch, index, GFX_OP_PIPE_CONTROL(6));
1123 wa_ctx_emit(batch, index, (PIPE_CONTROL_CS_STALL |
1124 PIPE_CONTROL_DC_FLUSH_ENABLE));
1125 wa_ctx_emit(batch, index, 0);
1126 wa_ctx_emit(batch, index, 0);
1127 wa_ctx_emit(batch, index, 0);
1128 wa_ctx_emit(batch, index, 0);
Arun Siluvery9e000842015-07-03 14:27:31 +01001129
Arun Siluvery83b8a982015-07-08 10:27:05 +01001130 wa_ctx_emit(batch, index, (MI_LOAD_REGISTER_MEM_GEN8(1) |
1131 MI_SRM_LRM_GLOBAL_GTT));
1132 wa_ctx_emit(batch, index, GEN8_L3SQCREG4);
1133 wa_ctx_emit(batch, index, ring->scratch.gtt_offset + 256);
1134 wa_ctx_emit(batch, index, 0);
Arun Siluvery9e000842015-07-03 14:27:31 +01001135
1136 return index;
1137}
1138
Arun Siluvery17ee9502015-06-19 19:07:01 +01001139static inline uint32_t wa_ctx_start(struct i915_wa_ctx_bb *wa_ctx,
1140 uint32_t offset,
1141 uint32_t start_alignment)
1142{
1143 return wa_ctx->offset = ALIGN(offset, start_alignment);
1144}
1145
1146static inline int wa_ctx_end(struct i915_wa_ctx_bb *wa_ctx,
1147 uint32_t offset,
1148 uint32_t size_alignment)
1149{
1150 wa_ctx->size = offset - wa_ctx->offset;
1151
1152 WARN(wa_ctx->size % size_alignment,
1153 "wa_ctx_bb failed sanity checks: size %d is not aligned to %d\n",
1154 wa_ctx->size, size_alignment);
1155 return 0;
1156}
1157
1158/**
1159 * gen8_init_indirectctx_bb() - initialize indirect ctx batch with WA
1160 *
1161 * @ring: only applicable for RCS
1162 * @wa_ctx: structure representing wa_ctx
1163 * offset: specifies start of the batch, should be cache-aligned. This is updated
1164 * with the offset value received as input.
1165 * size: size of the batch in DWORDS but HW expects in terms of cachelines
1166 * @batch: page in which WA are loaded
1167 * @offset: This field specifies the start of the batch, it should be
1168 * cache-aligned otherwise it is adjusted accordingly.
1169 * Typically we only have one indirect_ctx and per_ctx batch buffer which are
1170 * initialized at the beginning and shared across all contexts but this field
1171 * helps us to have multiple batches at different offsets and select them based
1172 * on a criteria. At the moment this batch always start at the beginning of the page
1173 * and at this point we don't have multiple wa_ctx batch buffers.
1174 *
1175 * The number of WA applied are not known at the beginning; we use this field
1176 * to return the no of DWORDS written.
Arun Siluvery4d78c8d2015-06-23 15:50:43 +01001177 *
Arun Siluvery17ee9502015-06-19 19:07:01 +01001178 * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
1179 * so it adds NOOPs as padding to make it cacheline aligned.
1180 * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
1181 * makes a complete batch buffer.
1182 *
1183 * Return: non-zero if we exceed the PAGE_SIZE limit.
1184 */
1185
1186static int gen8_init_indirectctx_bb(struct intel_engine_cs *ring,
1187 struct i915_wa_ctx_bb *wa_ctx,
1188 uint32_t *const batch,
1189 uint32_t *offset)
1190{
Arun Siluvery0160f052015-06-23 15:46:57 +01001191 uint32_t scratch_addr;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001192 uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
1193
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001194 /* WaDisableCtxRestoreArbitration:bdw,chv */
Arun Siluvery83b8a982015-07-08 10:27:05 +01001195 wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_DISABLE);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001196
Arun Siluveryc82435b2015-06-19 18:37:13 +01001197 /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
1198 if (IS_BROADWELL(ring->dev)) {
Arun Siluvery9e000842015-07-03 14:27:31 +01001199 index = gen8_emit_flush_coherentl3_wa(ring, batch, index);
1200 if (index < 0)
1201 return index;
Arun Siluveryc82435b2015-06-19 18:37:13 +01001202 }
1203
Arun Siluvery0160f052015-06-23 15:46:57 +01001204 /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
1205 /* Actual scratch location is at 128 bytes offset */
1206 scratch_addr = ring->scratch.gtt_offset + 2*CACHELINE_BYTES;
1207
Arun Siluvery83b8a982015-07-08 10:27:05 +01001208 wa_ctx_emit(batch, index, GFX_OP_PIPE_CONTROL(6));
1209 wa_ctx_emit(batch, index, (PIPE_CONTROL_FLUSH_L3 |
1210 PIPE_CONTROL_GLOBAL_GTT_IVB |
1211 PIPE_CONTROL_CS_STALL |
1212 PIPE_CONTROL_QW_WRITE));
1213 wa_ctx_emit(batch, index, scratch_addr);
1214 wa_ctx_emit(batch, index, 0);
1215 wa_ctx_emit(batch, index, 0);
1216 wa_ctx_emit(batch, index, 0);
Arun Siluvery0160f052015-06-23 15:46:57 +01001217
Arun Siluvery17ee9502015-06-19 19:07:01 +01001218 /* Pad to end of cacheline */
1219 while (index % CACHELINE_DWORDS)
Arun Siluvery83b8a982015-07-08 10:27:05 +01001220 wa_ctx_emit(batch, index, MI_NOOP);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001221
1222 /*
1223 * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
1224 * execution depends on the length specified in terms of cache lines
1225 * in the register CTX_RCS_INDIRECT_CTX
1226 */
1227
1228 return wa_ctx_end(wa_ctx, *offset = index, CACHELINE_DWORDS);
1229}
1230
1231/**
1232 * gen8_init_perctx_bb() - initialize per ctx batch with WA
1233 *
1234 * @ring: only applicable for RCS
1235 * @wa_ctx: structure representing wa_ctx
1236 * offset: specifies start of the batch, should be cache-aligned.
1237 * size: size of the batch in DWORDS but HW expects in terms of cachelines
Arun Siluvery4d78c8d2015-06-23 15:50:43 +01001238 * @batch: page in which WA are loaded
Arun Siluvery17ee9502015-06-19 19:07:01 +01001239 * @offset: This field specifies the start of this batch.
1240 * This batch is started immediately after indirect_ctx batch. Since we ensure
1241 * that indirect_ctx ends on a cacheline this batch is aligned automatically.
1242 *
1243 * The number of DWORDS written are returned using this field.
1244 *
1245 * This batch is terminated with MI_BATCH_BUFFER_END and so we need not add padding
1246 * to align it with cacheline as padding after MI_BATCH_BUFFER_END is redundant.
1247 */
1248static int gen8_init_perctx_bb(struct intel_engine_cs *ring,
1249 struct i915_wa_ctx_bb *wa_ctx,
1250 uint32_t *const batch,
1251 uint32_t *offset)
1252{
1253 uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
1254
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001255 /* WaDisableCtxRestoreArbitration:bdw,chv */
Arun Siluvery83b8a982015-07-08 10:27:05 +01001256 wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_ENABLE);
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001257
Arun Siluvery83b8a982015-07-08 10:27:05 +01001258 wa_ctx_emit(batch, index, MI_BATCH_BUFFER_END);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001259
1260 return wa_ctx_end(wa_ctx, *offset = index, 1);
1261}
1262
Arun Siluvery0504cff2015-07-14 15:01:27 +01001263static int gen9_init_indirectctx_bb(struct intel_engine_cs *ring,
1264 struct i915_wa_ctx_bb *wa_ctx,
1265 uint32_t *const batch,
1266 uint32_t *offset)
1267{
Arun Siluverya4106a72015-07-14 15:01:29 +01001268 int ret;
Arun Siluvery0907c8f2015-07-14 15:01:28 +01001269 struct drm_device *dev = ring->dev;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001270 uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
1271
Arun Siluvery0907c8f2015-07-14 15:01:28 +01001272 /* WaDisableCtxRestoreArbitration:skl,bxt */
1273 if ((IS_SKYLAKE(dev) && (INTEL_REVID(dev) <= SKL_REVID_D0)) ||
1274 (IS_BROXTON(dev) && (INTEL_REVID(dev) == BXT_REVID_A0)))
1275 wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_DISABLE);
Arun Siluvery0504cff2015-07-14 15:01:27 +01001276
Arun Siluverya4106a72015-07-14 15:01:29 +01001277 /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt */
1278 ret = gen8_emit_flush_coherentl3_wa(ring, batch, index);
1279 if (ret < 0)
1280 return ret;
1281 index = ret;
1282
Arun Siluvery0504cff2015-07-14 15:01:27 +01001283 /* Pad to end of cacheline */
1284 while (index % CACHELINE_DWORDS)
1285 wa_ctx_emit(batch, index, MI_NOOP);
1286
1287 return wa_ctx_end(wa_ctx, *offset = index, CACHELINE_DWORDS);
1288}
1289
1290static int gen9_init_perctx_bb(struct intel_engine_cs *ring,
1291 struct i915_wa_ctx_bb *wa_ctx,
1292 uint32_t *const batch,
1293 uint32_t *offset)
1294{
Arun Siluvery0907c8f2015-07-14 15:01:28 +01001295 struct drm_device *dev = ring->dev;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001296 uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
1297
Arun Siluvery9b014352015-07-14 15:01:30 +01001298 /* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:skl,bxt */
1299 if ((IS_SKYLAKE(dev) && (INTEL_REVID(dev) <= SKL_REVID_B0)) ||
1300 (IS_BROXTON(dev) && (INTEL_REVID(dev) == BXT_REVID_A0))) {
1301 wa_ctx_emit(batch, index, MI_LOAD_REGISTER_IMM(1));
1302 wa_ctx_emit(batch, index, GEN9_SLICE_COMMON_ECO_CHICKEN0);
1303 wa_ctx_emit(batch, index,
1304 _MASKED_BIT_ENABLE(DISABLE_PIXEL_MASK_CAMMING));
1305 wa_ctx_emit(batch, index, MI_NOOP);
1306 }
1307
Arun Siluvery0907c8f2015-07-14 15:01:28 +01001308 /* WaDisableCtxRestoreArbitration:skl,bxt */
1309 if ((IS_SKYLAKE(dev) && (INTEL_REVID(dev) <= SKL_REVID_D0)) ||
1310 (IS_BROXTON(dev) && (INTEL_REVID(dev) == BXT_REVID_A0)))
1311 wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_ENABLE);
1312
Arun Siluvery0504cff2015-07-14 15:01:27 +01001313 wa_ctx_emit(batch, index, MI_BATCH_BUFFER_END);
1314
1315 return wa_ctx_end(wa_ctx, *offset = index, 1);
1316}
1317
Arun Siluvery17ee9502015-06-19 19:07:01 +01001318static int lrc_setup_wa_ctx_obj(struct intel_engine_cs *ring, u32 size)
1319{
1320 int ret;
1321
1322 ring->wa_ctx.obj = i915_gem_alloc_object(ring->dev, PAGE_ALIGN(size));
1323 if (!ring->wa_ctx.obj) {
1324 DRM_DEBUG_DRIVER("alloc LRC WA ctx backing obj failed.\n");
1325 return -ENOMEM;
1326 }
1327
1328 ret = i915_gem_obj_ggtt_pin(ring->wa_ctx.obj, PAGE_SIZE, 0);
1329 if (ret) {
1330 DRM_DEBUG_DRIVER("pin LRC WA ctx backing obj failed: %d\n",
1331 ret);
1332 drm_gem_object_unreference(&ring->wa_ctx.obj->base);
1333 return ret;
1334 }
1335
1336 return 0;
1337}
1338
1339static void lrc_destroy_wa_ctx_obj(struct intel_engine_cs *ring)
1340{
1341 if (ring->wa_ctx.obj) {
1342 i915_gem_object_ggtt_unpin(ring->wa_ctx.obj);
1343 drm_gem_object_unreference(&ring->wa_ctx.obj->base);
1344 ring->wa_ctx.obj = NULL;
1345 }
1346}
1347
1348static int intel_init_workaround_bb(struct intel_engine_cs *ring)
1349{
1350 int ret;
1351 uint32_t *batch;
1352 uint32_t offset;
1353 struct page *page;
1354 struct i915_ctx_workarounds *wa_ctx = &ring->wa_ctx;
1355
1356 WARN_ON(ring->id != RCS);
1357
Arun Siluvery5e60d792015-06-23 15:50:44 +01001358 /* update this when WA for higher Gen are added */
Arun Siluvery0504cff2015-07-14 15:01:27 +01001359 if (INTEL_INFO(ring->dev)->gen > 9) {
1360 DRM_ERROR("WA batch buffer is not initialized for Gen%d\n",
1361 INTEL_INFO(ring->dev)->gen);
Arun Siluvery5e60d792015-06-23 15:50:44 +01001362 return 0;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001363 }
Arun Siluvery5e60d792015-06-23 15:50:44 +01001364
Arun Siluveryc4db7592015-06-19 18:37:11 +01001365 /* some WA perform writes to scratch page, ensure it is valid */
1366 if (ring->scratch.obj == NULL) {
1367 DRM_ERROR("scratch page not allocated for %s\n", ring->name);
1368 return -EINVAL;
1369 }
1370
Arun Siluvery17ee9502015-06-19 19:07:01 +01001371 ret = lrc_setup_wa_ctx_obj(ring, PAGE_SIZE);
1372 if (ret) {
1373 DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
1374 return ret;
1375 }
1376
1377 page = i915_gem_object_get_page(wa_ctx->obj, 0);
1378 batch = kmap_atomic(page);
1379 offset = 0;
1380
1381 if (INTEL_INFO(ring->dev)->gen == 8) {
1382 ret = gen8_init_indirectctx_bb(ring,
1383 &wa_ctx->indirect_ctx,
1384 batch,
1385 &offset);
1386 if (ret)
1387 goto out;
1388
1389 ret = gen8_init_perctx_bb(ring,
1390 &wa_ctx->per_ctx,
1391 batch,
1392 &offset);
1393 if (ret)
1394 goto out;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001395 } else if (INTEL_INFO(ring->dev)->gen == 9) {
1396 ret = gen9_init_indirectctx_bb(ring,
1397 &wa_ctx->indirect_ctx,
1398 batch,
1399 &offset);
1400 if (ret)
1401 goto out;
1402
1403 ret = gen9_init_perctx_bb(ring,
1404 &wa_ctx->per_ctx,
1405 batch,
1406 &offset);
1407 if (ret)
1408 goto out;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001409 }
1410
1411out:
1412 kunmap_atomic(batch);
1413 if (ret)
1414 lrc_destroy_wa_ctx_obj(ring);
1415
1416 return ret;
1417}
1418
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001419static int gen8_init_common_ring(struct intel_engine_cs *ring)
1420{
1421 struct drm_device *dev = ring->dev;
1422 struct drm_i915_private *dev_priv = dev->dev_private;
1423
Oscar Mateo73d477f2014-07-24 17:04:31 +01001424 I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
1425 I915_WRITE(RING_HWSTAM(ring->mmio_base), 0xffffffff);
1426
Arun Siluvery2e5356d2015-06-02 20:06:59 +01001427 if (ring->status_page.obj) {
1428 I915_WRITE(RING_HWS_PGA(ring->mmio_base),
1429 (u32)ring->status_page.gfx_addr);
1430 POSTING_READ(RING_HWS_PGA(ring->mmio_base));
1431 }
1432
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001433 I915_WRITE(RING_MODE_GEN7(ring),
1434 _MASKED_BIT_DISABLE(GFX_REPLAY_MODE) |
1435 _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
1436 POSTING_READ(RING_MODE_GEN7(ring));
Thomas Danielc0a03a22015-01-09 11:09:37 +00001437 ring->next_context_status_buffer = 0;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001438 DRM_DEBUG_DRIVER("Execlists enabled for %s\n", ring->name);
1439
1440 memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
1441
1442 return 0;
1443}
1444
1445static int gen8_init_render_ring(struct intel_engine_cs *ring)
1446{
1447 struct drm_device *dev = ring->dev;
1448 struct drm_i915_private *dev_priv = dev->dev_private;
1449 int ret;
1450
1451 ret = gen8_init_common_ring(ring);
1452 if (ret)
1453 return ret;
1454
1455 /* We need to disable the AsyncFlip performance optimisations in order
1456 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1457 * programmed to '1' on all products.
1458 *
1459 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
1460 */
1461 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1462
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001463 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1464
Michel Thierry771b9a52014-11-11 16:47:33 +00001465 return init_workarounds_ring(ring);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001466}
1467
Damien Lespiau82ef8222015-02-09 19:33:08 +00001468static int gen9_init_render_ring(struct intel_engine_cs *ring)
1469{
1470 int ret;
1471
1472 ret = gen8_init_common_ring(ring);
1473 if (ret)
1474 return ret;
1475
1476 return init_workarounds_ring(ring);
1477}
1478
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001479static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request *req)
1480{
1481 struct i915_hw_ppgtt *ppgtt = req->ctx->ppgtt;
1482 struct intel_engine_cs *ring = req->ring;
1483 struct intel_ringbuffer *ringbuf = req->ringbuf;
1484 const int num_lri_cmds = GEN8_LEGACY_PDPES * 2;
1485 int i, ret;
1486
1487 ret = intel_logical_ring_begin(req, num_lri_cmds * 2 + 2);
1488 if (ret)
1489 return ret;
1490
1491 intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(num_lri_cmds));
1492 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
1493 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
1494
1495 intel_logical_ring_emit(ringbuf, GEN8_RING_PDP_UDW(ring, i));
1496 intel_logical_ring_emit(ringbuf, upper_32_bits(pd_daddr));
1497 intel_logical_ring_emit(ringbuf, GEN8_RING_PDP_LDW(ring, i));
1498 intel_logical_ring_emit(ringbuf, lower_32_bits(pd_daddr));
1499 }
1500
1501 intel_logical_ring_emit(ringbuf, MI_NOOP);
1502 intel_logical_ring_advance(ringbuf);
1503
1504 return 0;
1505}
1506
John Harrisonbe795fc2015-05-29 17:44:03 +01001507static int gen8_emit_bb_start(struct drm_i915_gem_request *req,
John Harrison8e004ef2015-02-13 11:48:10 +00001508 u64 offset, unsigned dispatch_flags)
Oscar Mateo15648582014-07-24 17:04:32 +01001509{
John Harrisonbe795fc2015-05-29 17:44:03 +01001510 struct intel_ringbuffer *ringbuf = req->ringbuf;
John Harrison8e004ef2015-02-13 11:48:10 +00001511 bool ppgtt = !(dispatch_flags & I915_DISPATCH_SECURE);
Oscar Mateo15648582014-07-24 17:04:32 +01001512 int ret;
1513
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001514 /* Don't rely in hw updating PDPs, specially in lite-restore.
1515 * Ideally, we should set Force PD Restore in ctx descriptor,
1516 * but we can't. Force Restore would be a second option, but
1517 * it is unsafe in case of lite-restore (because the ctx is
1518 * not idle). */
1519 if (req->ctx->ppgtt &&
1520 (intel_ring_flag(req->ring) & req->ctx->ppgtt->pd_dirty_rings)) {
1521 ret = intel_logical_ring_emit_pdps(req);
1522 if (ret)
1523 return ret;
1524
1525 req->ctx->ppgtt->pd_dirty_rings &= ~intel_ring_flag(req->ring);
1526 }
1527
John Harrison4d616a22015-05-29 17:44:08 +01001528 ret = intel_logical_ring_begin(req, 4);
Oscar Mateo15648582014-07-24 17:04:32 +01001529 if (ret)
1530 return ret;
1531
1532 /* FIXME(BDW): Address space and security selectors. */
Abdiel Janulgue69225282015-06-16 13:39:42 +03001533 intel_logical_ring_emit(ringbuf, MI_BATCH_BUFFER_START_GEN8 |
1534 (ppgtt<<8) |
1535 (dispatch_flags & I915_DISPATCH_RS ?
1536 MI_BATCH_RESOURCE_STREAMER : 0));
Oscar Mateo15648582014-07-24 17:04:32 +01001537 intel_logical_ring_emit(ringbuf, lower_32_bits(offset));
1538 intel_logical_ring_emit(ringbuf, upper_32_bits(offset));
1539 intel_logical_ring_emit(ringbuf, MI_NOOP);
1540 intel_logical_ring_advance(ringbuf);
1541
1542 return 0;
1543}
1544
Oscar Mateo73d477f2014-07-24 17:04:31 +01001545static bool gen8_logical_ring_get_irq(struct intel_engine_cs *ring)
1546{
1547 struct drm_device *dev = ring->dev;
1548 struct drm_i915_private *dev_priv = dev->dev_private;
1549 unsigned long flags;
1550
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001551 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Oscar Mateo73d477f2014-07-24 17:04:31 +01001552 return false;
1553
1554 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1555 if (ring->irq_refcount++ == 0) {
1556 I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
1557 POSTING_READ(RING_IMR(ring->mmio_base));
1558 }
1559 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1560
1561 return true;
1562}
1563
1564static void gen8_logical_ring_put_irq(struct intel_engine_cs *ring)
1565{
1566 struct drm_device *dev = ring->dev;
1567 struct drm_i915_private *dev_priv = dev->dev_private;
1568 unsigned long flags;
1569
1570 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1571 if (--ring->irq_refcount == 0) {
1572 I915_WRITE_IMR(ring, ~ring->irq_keep_mask);
1573 POSTING_READ(RING_IMR(ring->mmio_base));
1574 }
1575 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1576}
1577
John Harrison7deb4d32015-05-29 17:43:59 +01001578static int gen8_emit_flush(struct drm_i915_gem_request *request,
Oscar Mateo47122742014-07-24 17:04:28 +01001579 u32 invalidate_domains,
1580 u32 unused)
1581{
John Harrison7deb4d32015-05-29 17:43:59 +01001582 struct intel_ringbuffer *ringbuf = request->ringbuf;
Oscar Mateo47122742014-07-24 17:04:28 +01001583 struct intel_engine_cs *ring = ringbuf->ring;
1584 struct drm_device *dev = ring->dev;
1585 struct drm_i915_private *dev_priv = dev->dev_private;
1586 uint32_t cmd;
1587 int ret;
1588
John Harrison4d616a22015-05-29 17:44:08 +01001589 ret = intel_logical_ring_begin(request, 4);
Oscar Mateo47122742014-07-24 17:04:28 +01001590 if (ret)
1591 return ret;
1592
1593 cmd = MI_FLUSH_DW + 1;
1594
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001595 /* We always require a command barrier so that subsequent
1596 * commands, such as breadcrumb interrupts, are strictly ordered
1597 * wrt the contents of the write cache being flushed to memory
1598 * (and thus being coherent from the CPU).
1599 */
1600 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1601
1602 if (invalidate_domains & I915_GEM_GPU_DOMAINS) {
1603 cmd |= MI_INVALIDATE_TLB;
1604 if (ring == &dev_priv->ring[VCS])
1605 cmd |= MI_INVALIDATE_BSD;
Oscar Mateo47122742014-07-24 17:04:28 +01001606 }
1607
1608 intel_logical_ring_emit(ringbuf, cmd);
1609 intel_logical_ring_emit(ringbuf,
1610 I915_GEM_HWS_SCRATCH_ADDR |
1611 MI_FLUSH_DW_USE_GTT);
1612 intel_logical_ring_emit(ringbuf, 0); /* upper addr */
1613 intel_logical_ring_emit(ringbuf, 0); /* value */
1614 intel_logical_ring_advance(ringbuf);
1615
1616 return 0;
1617}
1618
John Harrison7deb4d32015-05-29 17:43:59 +01001619static int gen8_emit_flush_render(struct drm_i915_gem_request *request,
Oscar Mateo47122742014-07-24 17:04:28 +01001620 u32 invalidate_domains,
1621 u32 flush_domains)
1622{
John Harrison7deb4d32015-05-29 17:43:59 +01001623 struct intel_ringbuffer *ringbuf = request->ringbuf;
Oscar Mateo47122742014-07-24 17:04:28 +01001624 struct intel_engine_cs *ring = ringbuf->ring;
1625 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Imre Deak9647ff32015-01-25 13:27:11 -08001626 bool vf_flush_wa;
Oscar Mateo47122742014-07-24 17:04:28 +01001627 u32 flags = 0;
1628 int ret;
1629
1630 flags |= PIPE_CONTROL_CS_STALL;
1631
1632 if (flush_domains) {
1633 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
1634 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
1635 }
1636
1637 if (invalidate_domains) {
1638 flags |= PIPE_CONTROL_TLB_INVALIDATE;
1639 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
1640 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
1641 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
1642 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
1643 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
1644 flags |= PIPE_CONTROL_QW_WRITE;
1645 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
1646 }
1647
Imre Deak9647ff32015-01-25 13:27:11 -08001648 /*
1649 * On GEN9+ Before VF_CACHE_INVALIDATE we need to emit a NULL pipe
1650 * control.
1651 */
1652 vf_flush_wa = INTEL_INFO(ring->dev)->gen >= 9 &&
1653 flags & PIPE_CONTROL_VF_CACHE_INVALIDATE;
1654
John Harrison4d616a22015-05-29 17:44:08 +01001655 ret = intel_logical_ring_begin(request, vf_flush_wa ? 12 : 6);
Oscar Mateo47122742014-07-24 17:04:28 +01001656 if (ret)
1657 return ret;
1658
Imre Deak9647ff32015-01-25 13:27:11 -08001659 if (vf_flush_wa) {
1660 intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
1661 intel_logical_ring_emit(ringbuf, 0);
1662 intel_logical_ring_emit(ringbuf, 0);
1663 intel_logical_ring_emit(ringbuf, 0);
1664 intel_logical_ring_emit(ringbuf, 0);
1665 intel_logical_ring_emit(ringbuf, 0);
1666 }
1667
Oscar Mateo47122742014-07-24 17:04:28 +01001668 intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
1669 intel_logical_ring_emit(ringbuf, flags);
1670 intel_logical_ring_emit(ringbuf, scratch_addr);
1671 intel_logical_ring_emit(ringbuf, 0);
1672 intel_logical_ring_emit(ringbuf, 0);
1673 intel_logical_ring_emit(ringbuf, 0);
1674 intel_logical_ring_advance(ringbuf);
1675
1676 return 0;
1677}
1678
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001679static u32 gen8_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
1680{
1681 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1682}
1683
1684static void gen8_set_seqno(struct intel_engine_cs *ring, u32 seqno)
1685{
1686 intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
1687}
1688
John Harrisonc4e76632015-05-29 17:44:01 +01001689static int gen8_emit_request(struct drm_i915_gem_request *request)
Oscar Mateo4da46e12014-07-24 17:04:27 +01001690{
John Harrisonc4e76632015-05-29 17:44:01 +01001691 struct intel_ringbuffer *ringbuf = request->ringbuf;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001692 struct intel_engine_cs *ring = ringbuf->ring;
1693 u32 cmd;
1694 int ret;
1695
Michel Thierry53292cd2015-04-15 18:11:33 +01001696 /*
1697 * Reserve space for 2 NOOPs at the end of each request to be
1698 * used as a workaround for not being allowed to do lite
1699 * restore with HEAD==TAIL (WaIdleLiteRestore).
1700 */
John Harrison4d616a22015-05-29 17:44:08 +01001701 ret = intel_logical_ring_begin(request, 8);
Oscar Mateo4da46e12014-07-24 17:04:27 +01001702 if (ret)
1703 return ret;
1704
Ville Syrjälä8edfbb82014-11-14 18:16:56 +02001705 cmd = MI_STORE_DWORD_IMM_GEN4;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001706 cmd |= MI_GLOBAL_GTT;
1707
1708 intel_logical_ring_emit(ringbuf, cmd);
1709 intel_logical_ring_emit(ringbuf,
1710 (ring->status_page.gfx_addr +
1711 (I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT)));
1712 intel_logical_ring_emit(ringbuf, 0);
John Harrisonc4e76632015-05-29 17:44:01 +01001713 intel_logical_ring_emit(ringbuf, i915_gem_request_get_seqno(request));
Oscar Mateo4da46e12014-07-24 17:04:27 +01001714 intel_logical_ring_emit(ringbuf, MI_USER_INTERRUPT);
1715 intel_logical_ring_emit(ringbuf, MI_NOOP);
John Harrisonae707972015-05-29 17:44:14 +01001716 intel_logical_ring_advance_and_submit(request);
Oscar Mateo4da46e12014-07-24 17:04:27 +01001717
Michel Thierry53292cd2015-04-15 18:11:33 +01001718 /*
1719 * Here we add two extra NOOPs as padding to avoid
1720 * lite restore of a context with HEAD==TAIL.
1721 */
1722 intel_logical_ring_emit(ringbuf, MI_NOOP);
1723 intel_logical_ring_emit(ringbuf, MI_NOOP);
1724 intel_logical_ring_advance(ringbuf);
1725
Oscar Mateo4da46e12014-07-24 17:04:27 +01001726 return 0;
1727}
1728
John Harrisonbe013632015-05-29 17:43:45 +01001729static int intel_lr_context_render_state_init(struct drm_i915_gem_request *req)
Damien Lespiaucef437a2015-02-10 19:32:19 +00001730{
Damien Lespiaucef437a2015-02-10 19:32:19 +00001731 struct render_state so;
Damien Lespiaucef437a2015-02-10 19:32:19 +00001732 int ret;
1733
John Harrisonbe013632015-05-29 17:43:45 +01001734 ret = i915_gem_render_state_prepare(req->ring, &so);
Damien Lespiaucef437a2015-02-10 19:32:19 +00001735 if (ret)
1736 return ret;
1737
1738 if (so.rodata == NULL)
1739 return 0;
1740
John Harrisonbe795fc2015-05-29 17:44:03 +01001741 ret = req->ring->emit_bb_start(req, so.ggtt_offset,
John Harrisonbe013632015-05-29 17:43:45 +01001742 I915_DISPATCH_SECURE);
Damien Lespiaucef437a2015-02-10 19:32:19 +00001743 if (ret)
1744 goto out;
1745
Arun Siluvery84e81022015-07-20 10:46:10 +01001746 ret = req->ring->emit_bb_start(req,
1747 (so.ggtt_offset + so.aux_batch_offset),
1748 I915_DISPATCH_SECURE);
1749 if (ret)
1750 goto out;
1751
John Harrisonb2af0372015-05-29 17:43:50 +01001752 i915_vma_move_to_active(i915_gem_obj_to_ggtt(so.obj), req);
Damien Lespiaucef437a2015-02-10 19:32:19 +00001753
Damien Lespiaucef437a2015-02-10 19:32:19 +00001754out:
1755 i915_gem_render_state_fini(&so);
1756 return ret;
1757}
1758
John Harrison87531812015-05-29 17:43:44 +01001759static int gen8_init_rcs_context(struct drm_i915_gem_request *req)
Thomas Daniele7778be2014-12-02 12:50:48 +00001760{
1761 int ret;
1762
John Harrisone2be4fa2015-05-29 17:43:54 +01001763 ret = intel_logical_ring_workarounds_emit(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001764 if (ret)
1765 return ret;
1766
Peter Antoine3bbaba02015-07-10 20:13:11 +03001767 ret = intel_rcs_context_init_mocs(req);
1768 /*
1769 * Failing to program the MOCS is non-fatal.The system will not
1770 * run at peak performance. So generate an error and carry on.
1771 */
1772 if (ret)
1773 DRM_ERROR("MOCS failed to program: expect performance issues.\n");
1774
John Harrisonbe013632015-05-29 17:43:45 +01001775 return intel_lr_context_render_state_init(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001776}
1777
Oscar Mateo73e4d072014-07-24 17:04:48 +01001778/**
1779 * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
1780 *
1781 * @ring: Engine Command Streamer.
1782 *
1783 */
Oscar Mateo454afeb2014-07-24 17:04:22 +01001784void intel_logical_ring_cleanup(struct intel_engine_cs *ring)
1785{
John Harrison6402c332014-10-31 12:00:26 +00001786 struct drm_i915_private *dev_priv;
Oscar Mateo9832b9d2014-07-24 17:04:30 +01001787
Oscar Mateo48d82382014-07-24 17:04:23 +01001788 if (!intel_ring_initialized(ring))
1789 return;
1790
John Harrison6402c332014-10-31 12:00:26 +00001791 dev_priv = ring->dev->dev_private;
1792
Oscar Mateo9832b9d2014-07-24 17:04:30 +01001793 intel_logical_ring_stop(ring);
1794 WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
Oscar Mateo48d82382014-07-24 17:04:23 +01001795
1796 if (ring->cleanup)
1797 ring->cleanup(ring);
1798
1799 i915_cmd_parser_fini_ring(ring);
Chris Wilson06fbca72015-04-07 16:20:36 +01001800 i915_gem_batch_pool_fini(&ring->batch_pool);
Oscar Mateo48d82382014-07-24 17:04:23 +01001801
1802 if (ring->status_page.obj) {
1803 kunmap(sg_page(ring->status_page.obj->pages->sgl));
1804 ring->status_page.obj = NULL;
1805 }
Arun Siluvery17ee9502015-06-19 19:07:01 +01001806
1807 lrc_destroy_wa_ctx_obj(ring);
Oscar Mateo454afeb2014-07-24 17:04:22 +01001808}
1809
1810static int logical_ring_init(struct drm_device *dev, struct intel_engine_cs *ring)
1811{
Oscar Mateo48d82382014-07-24 17:04:23 +01001812 int ret;
Oscar Mateo48d82382014-07-24 17:04:23 +01001813
1814 /* Intentionally left blank. */
1815 ring->buffer = NULL;
1816
1817 ring->dev = dev;
1818 INIT_LIST_HEAD(&ring->active_list);
1819 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson06fbca72015-04-07 16:20:36 +01001820 i915_gem_batch_pool_init(dev, &ring->batch_pool);
Oscar Mateo48d82382014-07-24 17:04:23 +01001821 init_waitqueue_head(&ring->irq_queue);
1822
Michel Thierryacdd8842014-07-24 17:04:38 +01001823 INIT_LIST_HEAD(&ring->execlist_queue);
Thomas Danielc86ee3a92014-11-13 10:27:05 +00001824 INIT_LIST_HEAD(&ring->execlist_retired_req_list);
Michel Thierryacdd8842014-07-24 17:04:38 +01001825 spin_lock_init(&ring->execlist_lock);
1826
Oscar Mateo48d82382014-07-24 17:04:23 +01001827 ret = i915_cmd_parser_init_ring(ring);
1828 if (ret)
1829 return ret;
1830
Oscar Mateo564ddb22014-08-21 11:40:54 +01001831 ret = intel_lr_context_deferred_create(ring->default_context, ring);
1832
1833 return ret;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001834}
1835
1836static int logical_render_ring_init(struct drm_device *dev)
1837{
1838 struct drm_i915_private *dev_priv = dev->dev_private;
1839 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Daniel Vetter99be1df2014-11-20 00:33:06 +01001840 int ret;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001841
1842 ring->name = "render ring";
1843 ring->id = RCS;
1844 ring->mmio_base = RENDER_RING_BASE;
1845 ring->irq_enable_mask =
1846 GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001847 ring->irq_keep_mask =
1848 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
1849 if (HAS_L3_DPF(dev))
1850 ring->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001851
Damien Lespiau82ef8222015-02-09 19:33:08 +00001852 if (INTEL_INFO(dev)->gen >= 9)
1853 ring->init_hw = gen9_init_render_ring;
1854 else
1855 ring->init_hw = gen8_init_render_ring;
Thomas Daniele7778be2014-12-02 12:50:48 +00001856 ring->init_context = gen8_init_rcs_context;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001857 ring->cleanup = intel_fini_pipe_control;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001858 ring->get_seqno = gen8_get_seqno;
1859 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001860 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001861 ring->emit_flush = gen8_emit_flush_render;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001862 ring->irq_get = gen8_logical_ring_get_irq;
1863 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001864 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001865
Daniel Vetter99be1df2014-11-20 00:33:06 +01001866 ring->dev = dev;
Arun Siluveryc4db7592015-06-19 18:37:11 +01001867
1868 ret = intel_init_pipe_control(ring);
Daniel Vetter99be1df2014-11-20 00:33:06 +01001869 if (ret)
1870 return ret;
1871
Arun Siluvery17ee9502015-06-19 19:07:01 +01001872 ret = intel_init_workaround_bb(ring);
1873 if (ret) {
1874 /*
1875 * We continue even if we fail to initialize WA batch
1876 * because we only expect rare glitches but nothing
1877 * critical to prevent us from using GPU
1878 */
1879 DRM_ERROR("WA batch buffer initialization failed: %d\n",
1880 ret);
1881 }
1882
Arun Siluveryc4db7592015-06-19 18:37:11 +01001883 ret = logical_ring_init(dev, ring);
1884 if (ret) {
Arun Siluvery17ee9502015-06-19 19:07:01 +01001885 lrc_destroy_wa_ctx_obj(ring);
Arun Siluveryc4db7592015-06-19 18:37:11 +01001886 }
Arun Siluvery17ee9502015-06-19 19:07:01 +01001887
1888 return ret;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001889}
1890
1891static int logical_bsd_ring_init(struct drm_device *dev)
1892{
1893 struct drm_i915_private *dev_priv = dev->dev_private;
1894 struct intel_engine_cs *ring = &dev_priv->ring[VCS];
1895
1896 ring->name = "bsd ring";
1897 ring->id = VCS;
1898 ring->mmio_base = GEN6_BSD_RING_BASE;
1899 ring->irq_enable_mask =
1900 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001901 ring->irq_keep_mask =
1902 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001903
Daniel Vetterecfe00d2014-11-20 00:33:04 +01001904 ring->init_hw = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001905 ring->get_seqno = gen8_get_seqno;
1906 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001907 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001908 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001909 ring->irq_get = gen8_logical_ring_get_irq;
1910 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001911 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001912
Oscar Mateo454afeb2014-07-24 17:04:22 +01001913 return logical_ring_init(dev, ring);
1914}
1915
1916static int logical_bsd2_ring_init(struct drm_device *dev)
1917{
1918 struct drm_i915_private *dev_priv = dev->dev_private;
1919 struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
1920
1921 ring->name = "bds2 ring";
1922 ring->id = VCS2;
1923 ring->mmio_base = GEN8_BSD2_RING_BASE;
1924 ring->irq_enable_mask =
1925 GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001926 ring->irq_keep_mask =
1927 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001928
Daniel Vetterecfe00d2014-11-20 00:33:04 +01001929 ring->init_hw = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001930 ring->get_seqno = gen8_get_seqno;
1931 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001932 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001933 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001934 ring->irq_get = gen8_logical_ring_get_irq;
1935 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001936 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001937
Oscar Mateo454afeb2014-07-24 17:04:22 +01001938 return logical_ring_init(dev, ring);
1939}
1940
1941static int logical_blt_ring_init(struct drm_device *dev)
1942{
1943 struct drm_i915_private *dev_priv = dev->dev_private;
1944 struct intel_engine_cs *ring = &dev_priv->ring[BCS];
1945
1946 ring->name = "blitter ring";
1947 ring->id = BCS;
1948 ring->mmio_base = BLT_RING_BASE;
1949 ring->irq_enable_mask =
1950 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001951 ring->irq_keep_mask =
1952 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001953
Daniel Vetterecfe00d2014-11-20 00:33:04 +01001954 ring->init_hw = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001955 ring->get_seqno = gen8_get_seqno;
1956 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001957 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001958 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001959 ring->irq_get = gen8_logical_ring_get_irq;
1960 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001961 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001962
Oscar Mateo454afeb2014-07-24 17:04:22 +01001963 return logical_ring_init(dev, ring);
1964}
1965
1966static int logical_vebox_ring_init(struct drm_device *dev)
1967{
1968 struct drm_i915_private *dev_priv = dev->dev_private;
1969 struct intel_engine_cs *ring = &dev_priv->ring[VECS];
1970
1971 ring->name = "video enhancement ring";
1972 ring->id = VECS;
1973 ring->mmio_base = VEBOX_RING_BASE;
1974 ring->irq_enable_mask =
1975 GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001976 ring->irq_keep_mask =
1977 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001978
Daniel Vetterecfe00d2014-11-20 00:33:04 +01001979 ring->init_hw = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001980 ring->get_seqno = gen8_get_seqno;
1981 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001982 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001983 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001984 ring->irq_get = gen8_logical_ring_get_irq;
1985 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001986 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001987
Oscar Mateo454afeb2014-07-24 17:04:22 +01001988 return logical_ring_init(dev, ring);
1989}
1990
Oscar Mateo73e4d072014-07-24 17:04:48 +01001991/**
1992 * intel_logical_rings_init() - allocate, populate and init the Engine Command Streamers
1993 * @dev: DRM device.
1994 *
1995 * This function inits the engines for an Execlists submission style (the equivalent in the
1996 * legacy ringbuffer submission world would be i915_gem_init_rings). It does it only for
1997 * those engines that are present in the hardware.
1998 *
1999 * Return: non-zero if the initialization failed.
2000 */
Oscar Mateo454afeb2014-07-24 17:04:22 +01002001int intel_logical_rings_init(struct drm_device *dev)
2002{
2003 struct drm_i915_private *dev_priv = dev->dev_private;
2004 int ret;
2005
2006 ret = logical_render_ring_init(dev);
2007 if (ret)
2008 return ret;
2009
2010 if (HAS_BSD(dev)) {
2011 ret = logical_bsd_ring_init(dev);
2012 if (ret)
2013 goto cleanup_render_ring;
2014 }
2015
2016 if (HAS_BLT(dev)) {
2017 ret = logical_blt_ring_init(dev);
2018 if (ret)
2019 goto cleanup_bsd_ring;
2020 }
2021
2022 if (HAS_VEBOX(dev)) {
2023 ret = logical_vebox_ring_init(dev);
2024 if (ret)
2025 goto cleanup_blt_ring;
2026 }
2027
2028 if (HAS_BSD2(dev)) {
2029 ret = logical_bsd2_ring_init(dev);
2030 if (ret)
2031 goto cleanup_vebox_ring;
2032 }
2033
2034 ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
2035 if (ret)
2036 goto cleanup_bsd2_ring;
2037
2038 return 0;
2039
2040cleanup_bsd2_ring:
2041 intel_logical_ring_cleanup(&dev_priv->ring[VCS2]);
2042cleanup_vebox_ring:
2043 intel_logical_ring_cleanup(&dev_priv->ring[VECS]);
2044cleanup_blt_ring:
2045 intel_logical_ring_cleanup(&dev_priv->ring[BCS]);
2046cleanup_bsd_ring:
2047 intel_logical_ring_cleanup(&dev_priv->ring[VCS]);
2048cleanup_render_ring:
2049 intel_logical_ring_cleanup(&dev_priv->ring[RCS]);
2050
2051 return ret;
2052}
2053
Jeff McGee0cea6502015-02-13 10:27:56 -06002054static u32
2055make_rpcs(struct drm_device *dev)
2056{
2057 u32 rpcs = 0;
2058
2059 /*
2060 * No explicit RPCS request is needed to ensure full
2061 * slice/subslice/EU enablement prior to Gen9.
2062 */
2063 if (INTEL_INFO(dev)->gen < 9)
2064 return 0;
2065
2066 /*
2067 * Starting in Gen9, render power gating can leave
2068 * slice/subslice/EU in a partially enabled state. We
2069 * must make an explicit request through RPCS for full
2070 * enablement.
2071 */
2072 if (INTEL_INFO(dev)->has_slice_pg) {
2073 rpcs |= GEN8_RPCS_S_CNT_ENABLE;
2074 rpcs |= INTEL_INFO(dev)->slice_total <<
2075 GEN8_RPCS_S_CNT_SHIFT;
2076 rpcs |= GEN8_RPCS_ENABLE;
2077 }
2078
2079 if (INTEL_INFO(dev)->has_subslice_pg) {
2080 rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
2081 rpcs |= INTEL_INFO(dev)->subslice_per_slice <<
2082 GEN8_RPCS_SS_CNT_SHIFT;
2083 rpcs |= GEN8_RPCS_ENABLE;
2084 }
2085
2086 if (INTEL_INFO(dev)->has_eu_pg) {
2087 rpcs |= INTEL_INFO(dev)->eu_per_subslice <<
2088 GEN8_RPCS_EU_MIN_SHIFT;
2089 rpcs |= INTEL_INFO(dev)->eu_per_subslice <<
2090 GEN8_RPCS_EU_MAX_SHIFT;
2091 rpcs |= GEN8_RPCS_ENABLE;
2092 }
2093
2094 return rpcs;
2095}
2096
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002097static int
2098populate_lr_context(struct intel_context *ctx, struct drm_i915_gem_object *ctx_obj,
2099 struct intel_engine_cs *ring, struct intel_ringbuffer *ringbuf)
2100{
Thomas Daniel2d965532014-08-19 10:13:36 +01002101 struct drm_device *dev = ring->dev;
2102 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterae6c4802014-08-06 15:04:53 +02002103 struct i915_hw_ppgtt *ppgtt = ctx->ppgtt;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002104 struct page *page;
2105 uint32_t *reg_state;
2106 int ret;
2107
Thomas Daniel2d965532014-08-19 10:13:36 +01002108 if (!ppgtt)
2109 ppgtt = dev_priv->mm.aliasing_ppgtt;
2110
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002111 ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
2112 if (ret) {
2113 DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
2114 return ret;
2115 }
2116
2117 ret = i915_gem_object_get_pages(ctx_obj);
2118 if (ret) {
2119 DRM_DEBUG_DRIVER("Could not get object pages\n");
2120 return ret;
2121 }
2122
2123 i915_gem_object_pin_pages(ctx_obj);
2124
2125 /* The second page of the context object contains some fields which must
2126 * be set up prior to the first execution. */
2127 page = i915_gem_object_get_page(ctx_obj, 1);
2128 reg_state = kmap_atomic(page);
2129
2130 /* A context is actually a big batch buffer with several MI_LOAD_REGISTER_IMM
2131 * commands followed by (reg, value) pairs. The values we are setting here are
2132 * only for the first context restore: on a subsequent save, the GPU will
2133 * recreate this batchbuffer with new values (including all the missing
2134 * MI_LOAD_REGISTER_IMM commands that we are not initializing here). */
2135 if (ring->id == RCS)
2136 reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(14);
2137 else
2138 reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(11);
2139 reg_state[CTX_LRI_HEADER_0] |= MI_LRI_FORCE_POSTED;
2140 reg_state[CTX_CONTEXT_CONTROL] = RING_CONTEXT_CONTROL(ring);
2141 reg_state[CTX_CONTEXT_CONTROL+1] =
Zhi Wang5baa22c52015-02-10 17:11:36 +08002142 _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
Abdiel Janulgue69225282015-06-16 13:39:42 +03002143 CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
2144 CTX_CTRL_RS_CTX_ENABLE);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002145 reg_state[CTX_RING_HEAD] = RING_HEAD(ring->mmio_base);
2146 reg_state[CTX_RING_HEAD+1] = 0;
2147 reg_state[CTX_RING_TAIL] = RING_TAIL(ring->mmio_base);
2148 reg_state[CTX_RING_TAIL+1] = 0;
2149 reg_state[CTX_RING_BUFFER_START] = RING_START(ring->mmio_base);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002150 /* Ring buffer start address is not known until the buffer is pinned.
2151 * It is written to the context image in execlists_update_context()
2152 */
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002153 reg_state[CTX_RING_BUFFER_CONTROL] = RING_CTL(ring->mmio_base);
2154 reg_state[CTX_RING_BUFFER_CONTROL+1] =
2155 ((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES) | RING_VALID;
2156 reg_state[CTX_BB_HEAD_U] = ring->mmio_base + 0x168;
2157 reg_state[CTX_BB_HEAD_U+1] = 0;
2158 reg_state[CTX_BB_HEAD_L] = ring->mmio_base + 0x140;
2159 reg_state[CTX_BB_HEAD_L+1] = 0;
2160 reg_state[CTX_BB_STATE] = ring->mmio_base + 0x110;
2161 reg_state[CTX_BB_STATE+1] = (1<<5);
2162 reg_state[CTX_SECOND_BB_HEAD_U] = ring->mmio_base + 0x11c;
2163 reg_state[CTX_SECOND_BB_HEAD_U+1] = 0;
2164 reg_state[CTX_SECOND_BB_HEAD_L] = ring->mmio_base + 0x114;
2165 reg_state[CTX_SECOND_BB_HEAD_L+1] = 0;
2166 reg_state[CTX_SECOND_BB_STATE] = ring->mmio_base + 0x118;
2167 reg_state[CTX_SECOND_BB_STATE+1] = 0;
2168 if (ring->id == RCS) {
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002169 reg_state[CTX_BB_PER_CTX_PTR] = ring->mmio_base + 0x1c0;
2170 reg_state[CTX_BB_PER_CTX_PTR+1] = 0;
2171 reg_state[CTX_RCS_INDIRECT_CTX] = ring->mmio_base + 0x1c4;
2172 reg_state[CTX_RCS_INDIRECT_CTX+1] = 0;
2173 reg_state[CTX_RCS_INDIRECT_CTX_OFFSET] = ring->mmio_base + 0x1c8;
2174 reg_state[CTX_RCS_INDIRECT_CTX_OFFSET+1] = 0;
Arun Siluvery17ee9502015-06-19 19:07:01 +01002175 if (ring->wa_ctx.obj) {
2176 struct i915_ctx_workarounds *wa_ctx = &ring->wa_ctx;
2177 uint32_t ggtt_offset = i915_gem_obj_ggtt_offset(wa_ctx->obj);
2178
2179 reg_state[CTX_RCS_INDIRECT_CTX+1] =
2180 (ggtt_offset + wa_ctx->indirect_ctx.offset * sizeof(uint32_t)) |
2181 (wa_ctx->indirect_ctx.size / CACHELINE_DWORDS);
2182
2183 reg_state[CTX_RCS_INDIRECT_CTX_OFFSET+1] =
2184 CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT << 6;
2185
2186 reg_state[CTX_BB_PER_CTX_PTR+1] =
2187 (ggtt_offset + wa_ctx->per_ctx.offset * sizeof(uint32_t)) |
2188 0x01;
2189 }
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002190 }
2191 reg_state[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9);
2192 reg_state[CTX_LRI_HEADER_1] |= MI_LRI_FORCE_POSTED;
2193 reg_state[CTX_CTX_TIMESTAMP] = ring->mmio_base + 0x3a8;
2194 reg_state[CTX_CTX_TIMESTAMP+1] = 0;
2195 reg_state[CTX_PDP3_UDW] = GEN8_RING_PDP_UDW(ring, 3);
2196 reg_state[CTX_PDP3_LDW] = GEN8_RING_PDP_LDW(ring, 3);
2197 reg_state[CTX_PDP2_UDW] = GEN8_RING_PDP_UDW(ring, 2);
2198 reg_state[CTX_PDP2_LDW] = GEN8_RING_PDP_LDW(ring, 2);
2199 reg_state[CTX_PDP1_UDW] = GEN8_RING_PDP_UDW(ring, 1);
2200 reg_state[CTX_PDP1_LDW] = GEN8_RING_PDP_LDW(ring, 1);
2201 reg_state[CTX_PDP0_UDW] = GEN8_RING_PDP_UDW(ring, 0);
2202 reg_state[CTX_PDP0_LDW] = GEN8_RING_PDP_LDW(ring, 0);
Michel Thierryd7b26332015-04-08 12:13:34 +01002203
2204 /* With dynamic page allocation, PDPs may not be allocated at this point,
2205 * Point the unallocated PDPs to the scratch page
Michel Thierrye5815a22015-04-08 12:13:32 +01002206 */
2207 ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
2208 ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
2209 ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
2210 ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002211 if (ring->id == RCS) {
2212 reg_state[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
Jeff McGee0cea6502015-02-13 10:27:56 -06002213 reg_state[CTX_R_PWR_CLK_STATE] = GEN8_R_PWR_CLK_STATE;
2214 reg_state[CTX_R_PWR_CLK_STATE+1] = make_rpcs(dev);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002215 }
2216
2217 kunmap_atomic(reg_state);
2218
2219 ctx_obj->dirty = 1;
2220 set_page_dirty(page);
2221 i915_gem_object_unpin_pages(ctx_obj);
2222
2223 return 0;
2224}
2225
Oscar Mateo73e4d072014-07-24 17:04:48 +01002226/**
2227 * intel_lr_context_free() - free the LRC specific bits of a context
2228 * @ctx: the LR context to free.
2229 *
2230 * The real context freeing is done in i915_gem_context_free: this only
2231 * takes care of the bits that are LRC related: the per-engine backing
2232 * objects and the logical ringbuffer.
2233 */
Oscar Mateoede7d422014-07-24 17:04:12 +01002234void intel_lr_context_free(struct intel_context *ctx)
2235{
Oscar Mateo8c8579172014-07-24 17:04:14 +01002236 int i;
2237
2238 for (i = 0; i < I915_NUM_RINGS; i++) {
2239 struct drm_i915_gem_object *ctx_obj = ctx->engine[i].state;
Oscar Mateo84c23772014-07-24 17:04:15 +01002240
Oscar Mateo8c8579172014-07-24 17:04:14 +01002241 if (ctx_obj) {
Oscar Mateodcb4c122014-11-13 10:28:10 +00002242 struct intel_ringbuffer *ringbuf =
2243 ctx->engine[i].ringbuf;
2244 struct intel_engine_cs *ring = ringbuf->ring;
2245
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002246 if (ctx == ring->default_context) {
2247 intel_unpin_ringbuffer_obj(ringbuf);
2248 i915_gem_object_ggtt_unpin(ctx_obj);
2249 }
Mika Kuoppalaa7cbede2015-01-13 11:32:25 +02002250 WARN_ON(ctx->engine[ring->id].pin_count);
Oscar Mateo84c23772014-07-24 17:04:15 +01002251 intel_destroy_ringbuffer_obj(ringbuf);
2252 kfree(ringbuf);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002253 drm_gem_object_unreference(&ctx_obj->base);
2254 }
2255 }
2256}
2257
2258static uint32_t get_lr_context_size(struct intel_engine_cs *ring)
2259{
2260 int ret = 0;
2261
Michael H. Nguyen468c6812014-11-13 17:51:49 +00002262 WARN_ON(INTEL_INFO(ring->dev)->gen < 8);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002263
2264 switch (ring->id) {
2265 case RCS:
Michael H. Nguyen468c6812014-11-13 17:51:49 +00002266 if (INTEL_INFO(ring->dev)->gen >= 9)
2267 ret = GEN9_LR_CONTEXT_RENDER_SIZE;
2268 else
2269 ret = GEN8_LR_CONTEXT_RENDER_SIZE;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002270 break;
2271 case VCS:
2272 case BCS:
2273 case VECS:
2274 case VCS2:
2275 ret = GEN8_LR_CONTEXT_OTHER_SIZE;
2276 break;
2277 }
2278
2279 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01002280}
2281
Daniel Vetter70b0ea82014-11-18 09:09:32 +01002282static void lrc_setup_hardware_status_page(struct intel_engine_cs *ring,
Thomas Daniel1df06b72014-10-29 09:52:51 +00002283 struct drm_i915_gem_object *default_ctx_obj)
2284{
2285 struct drm_i915_private *dev_priv = ring->dev->dev_private;
2286
2287 /* The status page is offset 0 from the default context object
2288 * in LRC mode. */
2289 ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(default_ctx_obj);
2290 ring->status_page.page_addr =
2291 kmap(sg_page(default_ctx_obj->pages->sgl));
Thomas Daniel1df06b72014-10-29 09:52:51 +00002292 ring->status_page.obj = default_ctx_obj;
2293
2294 I915_WRITE(RING_HWS_PGA(ring->mmio_base),
2295 (u32)ring->status_page.gfx_addr);
2296 POSTING_READ(RING_HWS_PGA(ring->mmio_base));
Thomas Daniel1df06b72014-10-29 09:52:51 +00002297}
2298
Oscar Mateo73e4d072014-07-24 17:04:48 +01002299/**
2300 * intel_lr_context_deferred_create() - create the LRC specific bits of a context
2301 * @ctx: LR context to create.
2302 * @ring: engine to be used with the context.
2303 *
2304 * This function can be called more than once, with different engines, if we plan
2305 * to use the context with them. The context backing objects and the ringbuffers
2306 * (specially the ringbuffer backing objects) suck a lot of memory up, and that's why
2307 * the creation is a deferred call: it's better to make sure first that we need to use
2308 * a given ring with the context.
2309 *
Masanari Iida32197aa2014-10-20 23:53:13 +09002310 * Return: non-zero on error.
Oscar Mateo73e4d072014-07-24 17:04:48 +01002311 */
Oscar Mateoede7d422014-07-24 17:04:12 +01002312int intel_lr_context_deferred_create(struct intel_context *ctx,
2313 struct intel_engine_cs *ring)
2314{
Oscar Mateodcb4c122014-11-13 10:28:10 +00002315 const bool is_global_default_ctx = (ctx == ring->default_context);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002316 struct drm_device *dev = ring->dev;
2317 struct drm_i915_gem_object *ctx_obj;
2318 uint32_t context_size;
Oscar Mateo84c23772014-07-24 17:04:15 +01002319 struct intel_ringbuffer *ringbuf;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002320 int ret;
2321
Oscar Mateoede7d422014-07-24 17:04:12 +01002322 WARN_ON(ctx->legacy_hw_ctx.rcs_state != NULL);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002323 WARN_ON(ctx->engine[ring->id].state);
Oscar Mateoede7d422014-07-24 17:04:12 +01002324
Oscar Mateo8c8579172014-07-24 17:04:14 +01002325 context_size = round_up(get_lr_context_size(ring), 4096);
2326
Chris Wilson149c86e2015-04-07 16:21:11 +01002327 ctx_obj = i915_gem_alloc_object(dev, context_size);
Dan Carpenter3126a662015-04-30 17:30:50 +03002328 if (!ctx_obj) {
2329 DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
2330 return -ENOMEM;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002331 }
2332
Oscar Mateodcb4c122014-11-13 10:28:10 +00002333 if (is_global_default_ctx) {
2334 ret = i915_gem_obj_ggtt_pin(ctx_obj, GEN8_LR_CONTEXT_ALIGN, 0);
2335 if (ret) {
2336 DRM_DEBUG_DRIVER("Pin LRC backing obj failed: %d\n",
2337 ret);
2338 drm_gem_object_unreference(&ctx_obj->base);
2339 return ret;
2340 }
Oscar Mateo8c8579172014-07-24 17:04:14 +01002341 }
2342
Oscar Mateo84c23772014-07-24 17:04:15 +01002343 ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
2344 if (!ringbuf) {
2345 DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s\n",
2346 ring->name);
Oscar Mateo84c23772014-07-24 17:04:15 +01002347 ret = -ENOMEM;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002348 goto error_unpin_ctx;
Oscar Mateo84c23772014-07-24 17:04:15 +01002349 }
2350
Daniel Vetter0c7dd532014-08-11 16:17:44 +02002351 ringbuf->ring = ring;
Oscar Mateo582d67f2014-07-24 17:04:16 +01002352
Oscar Mateo84c23772014-07-24 17:04:15 +01002353 ringbuf->size = 32 * PAGE_SIZE;
2354 ringbuf->effective_size = ringbuf->size;
2355 ringbuf->head = 0;
2356 ringbuf->tail = 0;
Oscar Mateo84c23772014-07-24 17:04:15 +01002357 ringbuf->last_retired_head = -1;
Dave Gordonebd0fd42014-11-27 11:22:49 +00002358 intel_ring_update_space(ringbuf);
Oscar Mateo84c23772014-07-24 17:04:15 +01002359
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002360 if (ringbuf->obj == NULL) {
2361 ret = intel_alloc_ringbuffer_obj(dev, ringbuf);
2362 if (ret) {
2363 DRM_DEBUG_DRIVER(
2364 "Failed to allocate ringbuffer obj %s: %d\n",
Oscar Mateo84c23772014-07-24 17:04:15 +01002365 ring->name, ret);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002366 goto error_free_rbuf;
2367 }
2368
2369 if (is_global_default_ctx) {
2370 ret = intel_pin_and_map_ringbuffer_obj(dev, ringbuf);
2371 if (ret) {
2372 DRM_ERROR(
2373 "Failed to pin and map ringbuffer %s: %d\n",
2374 ring->name, ret);
2375 goto error_destroy_rbuf;
2376 }
2377 }
2378
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002379 }
2380
2381 ret = populate_lr_context(ctx, ctx_obj, ring, ringbuf);
2382 if (ret) {
2383 DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002384 goto error;
Oscar Mateo84c23772014-07-24 17:04:15 +01002385 }
2386
2387 ctx->engine[ring->id].ringbuf = ringbuf;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002388 ctx->engine[ring->id].state = ctx_obj;
Oscar Mateoede7d422014-07-24 17:04:12 +01002389
Daniel Vetter70b0ea82014-11-18 09:09:32 +01002390 if (ctx == ring->default_context)
2391 lrc_setup_hardware_status_page(ring, ctx_obj);
Thomas Daniele7778be2014-12-02 12:50:48 +00002392 else if (ring->id == RCS && !ctx->rcs_initialized) {
Michel Thierry771b9a52014-11-11 16:47:33 +00002393 if (ring->init_context) {
John Harrison76c39162015-05-29 17:43:43 +01002394 struct drm_i915_gem_request *req;
2395
2396 ret = i915_gem_request_alloc(ring, ctx, &req);
2397 if (ret)
2398 return ret;
2399
John Harrison87531812015-05-29 17:43:44 +01002400 ret = ring->init_context(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00002401 if (ret) {
Michel Thierry771b9a52014-11-11 16:47:33 +00002402 DRM_ERROR("ring init context: %d\n", ret);
John Harrison76c39162015-05-29 17:43:43 +01002403 i915_gem_request_cancel(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00002404 ctx->engine[ring->id].ringbuf = NULL;
2405 ctx->engine[ring->id].state = NULL;
2406 goto error;
2407 }
John Harrison76c39162015-05-29 17:43:43 +01002408
John Harrison75289872015-05-29 17:43:49 +01002409 i915_add_request_no_flush(req);
Michel Thierry771b9a52014-11-11 16:47:33 +00002410 }
2411
Oscar Mateo564ddb22014-08-21 11:40:54 +01002412 ctx->rcs_initialized = true;
2413 }
2414
Oscar Mateoede7d422014-07-24 17:04:12 +01002415 return 0;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002416
2417error:
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002418 if (is_global_default_ctx)
2419 intel_unpin_ringbuffer_obj(ringbuf);
2420error_destroy_rbuf:
2421 intel_destroy_ringbuffer_obj(ringbuf);
2422error_free_rbuf:
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002423 kfree(ringbuf);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002424error_unpin_ctx:
Oscar Mateodcb4c122014-11-13 10:28:10 +00002425 if (is_global_default_ctx)
2426 i915_gem_object_ggtt_unpin(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002427 drm_gem_object_unreference(&ctx_obj->base);
2428 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01002429}
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002430
2431void intel_lr_context_reset(struct drm_device *dev,
2432 struct intel_context *ctx)
2433{
2434 struct drm_i915_private *dev_priv = dev->dev_private;
2435 struct intel_engine_cs *ring;
2436 int i;
2437
2438 for_each_ring(ring, dev_priv, i) {
2439 struct drm_i915_gem_object *ctx_obj =
2440 ctx->engine[ring->id].state;
2441 struct intel_ringbuffer *ringbuf =
2442 ctx->engine[ring->id].ringbuf;
2443 uint32_t *reg_state;
2444 struct page *page;
2445
2446 if (!ctx_obj)
2447 continue;
2448
2449 if (i915_gem_object_get_pages(ctx_obj)) {
2450 WARN(1, "Failed get_pages for context obj\n");
2451 continue;
2452 }
2453 page = i915_gem_object_get_page(ctx_obj, 1);
2454 reg_state = kmap_atomic(page);
2455
2456 reg_state[CTX_RING_HEAD+1] = 0;
2457 reg_state[CTX_RING_TAIL+1] = 0;
2458
2459 kunmap_atomic(reg_state);
2460
2461 ringbuf->head = 0;
2462 ringbuf->tail = 0;
2463 }
2464}