blob: 8cac4cab1666fa6a6d8dbd8081a25750beefaa7e [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"
Oscar Mateo127f1002014-07-24 17:04:11 +0100138
Michael H. Nguyen468c6812014-11-13 17:51:49 +0000139#define GEN9_LR_CONTEXT_RENDER_SIZE (22 * PAGE_SIZE)
Oscar Mateo8c8579172014-07-24 17:04:14 +0100140#define GEN8_LR_CONTEXT_RENDER_SIZE (20 * PAGE_SIZE)
141#define GEN8_LR_CONTEXT_OTHER_SIZE (2 * PAGE_SIZE)
142
Thomas Daniele981e7b2014-07-24 17:04:39 +0100143#define RING_EXECLIST_QFULL (1 << 0x2)
144#define RING_EXECLIST1_VALID (1 << 0x3)
145#define RING_EXECLIST0_VALID (1 << 0x4)
146#define RING_EXECLIST_ACTIVE_STATUS (3 << 0xE)
147#define RING_EXECLIST1_ACTIVE (1 << 0x11)
148#define RING_EXECLIST0_ACTIVE (1 << 0x12)
149
150#define GEN8_CTX_STATUS_IDLE_ACTIVE (1 << 0)
151#define GEN8_CTX_STATUS_PREEMPTED (1 << 1)
152#define GEN8_CTX_STATUS_ELEMENT_SWITCH (1 << 2)
153#define GEN8_CTX_STATUS_ACTIVE_IDLE (1 << 3)
154#define GEN8_CTX_STATUS_COMPLETE (1 << 4)
155#define GEN8_CTX_STATUS_LITE_RESTORE (1 << 15)
Oscar Mateo8670d6f2014-07-24 17:04:17 +0100156
157#define CTX_LRI_HEADER_0 0x01
158#define CTX_CONTEXT_CONTROL 0x02
159#define CTX_RING_HEAD 0x04
160#define CTX_RING_TAIL 0x06
161#define CTX_RING_BUFFER_START 0x08
162#define CTX_RING_BUFFER_CONTROL 0x0a
163#define CTX_BB_HEAD_U 0x0c
164#define CTX_BB_HEAD_L 0x0e
165#define CTX_BB_STATE 0x10
166#define CTX_SECOND_BB_HEAD_U 0x12
167#define CTX_SECOND_BB_HEAD_L 0x14
168#define CTX_SECOND_BB_STATE 0x16
169#define CTX_BB_PER_CTX_PTR 0x18
170#define CTX_RCS_INDIRECT_CTX 0x1a
171#define CTX_RCS_INDIRECT_CTX_OFFSET 0x1c
172#define CTX_LRI_HEADER_1 0x21
173#define CTX_CTX_TIMESTAMP 0x22
174#define CTX_PDP3_UDW 0x24
175#define CTX_PDP3_LDW 0x26
176#define CTX_PDP2_UDW 0x28
177#define CTX_PDP2_LDW 0x2a
178#define CTX_PDP1_UDW 0x2c
179#define CTX_PDP1_LDW 0x2e
180#define CTX_PDP0_UDW 0x30
181#define CTX_PDP0_LDW 0x32
182#define CTX_LRI_HEADER_2 0x41
183#define CTX_R_PWR_CLK_STATE 0x42
184#define CTX_GPGPU_CSR_BASE_ADDRESS 0x44
185
Ben Widawsky84b790f2014-07-24 17:04:36 +0100186#define GEN8_CTX_VALID (1<<0)
187#define GEN8_CTX_FORCE_PD_RESTORE (1<<1)
188#define GEN8_CTX_FORCE_RESTORE (1<<2)
189#define GEN8_CTX_L3LLC_COHERENT (1<<5)
190#define GEN8_CTX_PRIVILEGE (1<<8)
Michel Thierrye5815a22015-04-08 12:13:32 +0100191
192#define ASSIGN_CTX_PDP(ppgtt, reg_state, n) { \
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300193 const u64 _addr = i915_page_dir_dma_addr((ppgtt), (n)); \
Michel Thierrye5815a22015-04-08 12:13:32 +0100194 reg_state[CTX_PDP ## n ## _UDW+1] = upper_32_bits(_addr); \
195 reg_state[CTX_PDP ## n ## _LDW+1] = lower_32_bits(_addr); \
196}
197
Ben Widawsky84b790f2014-07-24 17:04:36 +0100198enum {
199 ADVANCED_CONTEXT = 0,
200 LEGACY_CONTEXT,
201 ADVANCED_AD_CONTEXT,
202 LEGACY_64B_CONTEXT
203};
204#define GEN8_CTX_MODE_SHIFT 3
205enum {
206 FAULT_AND_HANG = 0,
207 FAULT_AND_HALT, /* Debug only */
208 FAULT_AND_STREAM,
209 FAULT_AND_CONTINUE /* Unsupported */
210};
211#define GEN8_CTX_ID_SHIFT 32
Arun Siluvery17ee9502015-06-19 19:07:01 +0100212#define CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x17
Ben Widawsky84b790f2014-07-24 17:04:36 +0100213
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000214static int intel_lr_context_pin(struct intel_engine_cs *ring,
215 struct intel_context *ctx);
216
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
Nick Hoath203a5712015-02-06 11:30:04 +0000265static uint64_t execlists_ctx_descriptor(struct intel_engine_cs *ring,
266 struct drm_i915_gem_object *ctx_obj)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100267{
Nick Hoath203a5712015-02-06 11:30:04 +0000268 struct drm_device *dev = ring->dev;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100269 uint64_t desc;
270 uint64_t lrca = i915_gem_obj_ggtt_offset(ctx_obj);
Michel Thierryacdd8842014-07-24 17:04:38 +0100271
272 WARN_ON(lrca & 0xFFFFFFFF00000FFFULL);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100273
274 desc = GEN8_CTX_VALID;
275 desc |= LEGACY_CONTEXT << GEN8_CTX_MODE_SHIFT;
Arun Siluvery51847fb2015-04-07 14:01:33 +0100276 if (IS_GEN8(ctx_obj->base.dev))
277 desc |= GEN8_CTX_L3LLC_COHERENT;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100278 desc |= GEN8_CTX_PRIVILEGE;
279 desc |= lrca;
280 desc |= (u64)intel_execlists_ctx_id(ctx_obj) << GEN8_CTX_ID_SHIFT;
281
282 /* TODO: WaDisableLiteRestore when we start using semaphore
283 * signalling between Command Streamers */
284 /* desc |= GEN8_CTX_FORCE_RESTORE; */
285
Nick Hoath203a5712015-02-06 11:30:04 +0000286 /* WaEnableForceRestoreInCtxtDescForVCS:skl */
287 if (IS_GEN9(dev) &&
288 INTEL_REVID(dev) <= SKL_REVID_B0 &&
289 (ring->id == BCS || ring->id == VCS ||
290 ring->id == VECS || ring->id == VCS2))
291 desc |= GEN8_CTX_FORCE_RESTORE;
292
Ben Widawsky84b790f2014-07-24 17:04:36 +0100293 return desc;
294}
295
296static void execlists_elsp_write(struct intel_engine_cs *ring,
297 struct drm_i915_gem_object *ctx_obj0,
298 struct drm_i915_gem_object *ctx_obj1)
299{
Tvrtko Ursulin6e7cc472014-11-13 17:51:51 +0000300 struct drm_device *dev = ring->dev;
301 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100302 uint64_t temp = 0;
303 uint32_t desc[4];
304
305 /* XXX: You must always write both descriptors in the order below. */
306 if (ctx_obj1)
Nick Hoath203a5712015-02-06 11:30:04 +0000307 temp = execlists_ctx_descriptor(ring, ctx_obj1);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100308 else
309 temp = 0;
310 desc[1] = (u32)(temp >> 32);
311 desc[0] = (u32)temp;
312
Nick Hoath203a5712015-02-06 11:30:04 +0000313 temp = execlists_ctx_descriptor(ring, ctx_obj0);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100314 desc[3] = (u32)(temp >> 32);
315 desc[2] = (u32)temp;
316
Chris Wilsona6111f72015-04-07 16:21:02 +0100317 spin_lock(&dev_priv->uncore.lock);
318 intel_uncore_forcewake_get__locked(dev_priv, FORCEWAKE_ALL);
319 I915_WRITE_FW(RING_ELSP(ring), desc[1]);
320 I915_WRITE_FW(RING_ELSP(ring), desc[0]);
321 I915_WRITE_FW(RING_ELSP(ring), desc[3]);
Chris Wilson6daccb02015-01-16 11:34:35 +0200322
Ben Widawsky84b790f2014-07-24 17:04:36 +0100323 /* The context is automatically loaded after the following */
Chris Wilsona6111f72015-04-07 16:21:02 +0100324 I915_WRITE_FW(RING_ELSP(ring), desc[2]);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100325
326 /* ELSP is a wo register, so use another nearby reg for posting instead */
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
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000332static int execlists_update_context(struct drm_i915_gem_object *ctx_obj,
333 struct drm_i915_gem_object *ring_obj,
Michel Thierryd7b26332015-04-08 12:13:34 +0100334 struct i915_hw_ppgtt *ppgtt,
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000335 u32 tail)
Oscar Mateoae1250b2014-07-24 17:04:37 +0100336{
337 struct page *page;
338 uint32_t *reg_state;
339
340 page = i915_gem_object_get_page(ctx_obj, 1);
341 reg_state = kmap_atomic(page);
342
343 reg_state[CTX_RING_TAIL+1] = tail;
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000344 reg_state[CTX_RING_BUFFER_START+1] = i915_gem_obj_ggtt_offset(ring_obj);
Oscar Mateoae1250b2014-07-24 17:04:37 +0100345
Michel Thierryd7b26332015-04-08 12:13:34 +0100346 /* True PPGTT with dynamic page allocation: update PDP registers and
347 * point the unallocated PDPs to the scratch page
348 */
349 if (ppgtt) {
350 ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
351 ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
352 ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
353 ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
354 }
355
Oscar Mateoae1250b2014-07-24 17:04:37 +0100356 kunmap_atomic(reg_state);
357
358 return 0;
359}
360
Dave Gordoncd0707c2014-10-30 15:41:56 +0000361static void execlists_submit_contexts(struct intel_engine_cs *ring,
362 struct intel_context *to0, u32 tail0,
363 struct intel_context *to1, u32 tail1)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100364{
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000365 struct drm_i915_gem_object *ctx_obj0 = to0->engine[ring->id].state;
366 struct intel_ringbuffer *ringbuf0 = to0->engine[ring->id].ringbuf;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100367 struct drm_i915_gem_object *ctx_obj1 = NULL;
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000368 struct intel_ringbuffer *ringbuf1 = NULL;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100369
Ben Widawsky84b790f2014-07-24 17:04:36 +0100370 BUG_ON(!ctx_obj0);
Michel Thierryacdd8842014-07-24 17:04:38 +0100371 WARN_ON(!i915_gem_obj_is_pinned(ctx_obj0));
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000372 WARN_ON(!i915_gem_obj_is_pinned(ringbuf0->obj));
Ben Widawsky84b790f2014-07-24 17:04:36 +0100373
Michel Thierryd7b26332015-04-08 12:13:34 +0100374 execlists_update_context(ctx_obj0, ringbuf0->obj, to0->ppgtt, tail0);
Oscar Mateoae1250b2014-07-24 17:04:37 +0100375
Ben Widawsky84b790f2014-07-24 17:04:36 +0100376 if (to1) {
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000377 ringbuf1 = to1->engine[ring->id].ringbuf;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100378 ctx_obj1 = to1->engine[ring->id].state;
379 BUG_ON(!ctx_obj1);
Michel Thierryacdd8842014-07-24 17:04:38 +0100380 WARN_ON(!i915_gem_obj_is_pinned(ctx_obj1));
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000381 WARN_ON(!i915_gem_obj_is_pinned(ringbuf1->obj));
Oscar Mateoae1250b2014-07-24 17:04:37 +0100382
Michel Thierryd7b26332015-04-08 12:13:34 +0100383 execlists_update_context(ctx_obj1, ringbuf1->obj, to1->ppgtt, tail1);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100384 }
385
386 execlists_elsp_write(ring, ctx_obj0, ctx_obj1);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100387}
388
Michel Thierryacdd8842014-07-24 17:04:38 +0100389static void execlists_context_unqueue(struct intel_engine_cs *ring)
390{
Nick Hoath6d3d8272015-01-15 13:10:39 +0000391 struct drm_i915_gem_request *req0 = NULL, *req1 = NULL;
392 struct drm_i915_gem_request *cursor = NULL, *tmp = NULL;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100393
394 assert_spin_locked(&ring->execlist_lock);
Michel Thierryacdd8842014-07-24 17:04:38 +0100395
Peter Antoine779949f2015-05-11 16:03:27 +0100396 /*
397 * If irqs are not active generate a warning as batches that finish
398 * without the irqs may get lost and a GPU Hang may occur.
399 */
400 WARN_ON(!intel_irqs_enabled(ring->dev->dev_private));
401
Michel Thierryacdd8842014-07-24 17:04:38 +0100402 if (list_empty(&ring->execlist_queue))
403 return;
404
405 /* Try to read in pairs */
406 list_for_each_entry_safe(cursor, tmp, &ring->execlist_queue,
407 execlist_link) {
408 if (!req0) {
409 req0 = cursor;
Nick Hoath6d3d8272015-01-15 13:10:39 +0000410 } else if (req0->ctx == cursor->ctx) {
Michel Thierryacdd8842014-07-24 17:04:38 +0100411 /* Same ctx: ignore first request, as second request
412 * will update tail past first request's workload */
Oscar Mateoe1fee722014-07-24 17:04:40 +0100413 cursor->elsp_submitted = req0->elsp_submitted;
Michel Thierryacdd8842014-07-24 17:04:38 +0100414 list_del(&req0->execlist_link);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000415 list_add_tail(&req0->execlist_link,
416 &ring->execlist_retired_req_list);
Michel Thierryacdd8842014-07-24 17:04:38 +0100417 req0 = cursor;
418 } else {
419 req1 = cursor;
420 break;
421 }
422 }
423
Michel Thierry53292cd2015-04-15 18:11:33 +0100424 if (IS_GEN8(ring->dev) || IS_GEN9(ring->dev)) {
425 /*
426 * WaIdleLiteRestore: make sure we never cause a lite
427 * restore with HEAD==TAIL
428 */
Michel Thierryd63f8202015-04-27 12:31:44 +0100429 if (req0->elsp_submitted) {
Michel Thierry53292cd2015-04-15 18:11:33 +0100430 /*
431 * Apply the wa NOOPS to prevent ring:HEAD == req:TAIL
432 * as we resubmit the request. See gen8_emit_request()
433 * for where we prepare the padding after the end of the
434 * request.
435 */
436 struct intel_ringbuffer *ringbuf;
437
438 ringbuf = req0->ctx->engine[ring->id].ringbuf;
439 req0->tail += 8;
440 req0->tail &= ringbuf->size - 1;
441 }
442 }
443
Oscar Mateoe1fee722014-07-24 17:04:40 +0100444 WARN_ON(req1 && req1->elsp_submitted);
445
Nick Hoath6d3d8272015-01-15 13:10:39 +0000446 execlists_submit_contexts(ring, req0->ctx, req0->tail,
447 req1 ? req1->ctx : NULL,
448 req1 ? req1->tail : 0);
Oscar Mateoe1fee722014-07-24 17:04:40 +0100449
450 req0->elsp_submitted++;
451 if (req1)
452 req1->elsp_submitted++;
Michel Thierryacdd8842014-07-24 17:04:38 +0100453}
454
Thomas Daniele981e7b2014-07-24 17:04:39 +0100455static bool execlists_check_remove_request(struct intel_engine_cs *ring,
456 u32 request_id)
457{
Nick Hoath6d3d8272015-01-15 13:10:39 +0000458 struct drm_i915_gem_request *head_req;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100459
460 assert_spin_locked(&ring->execlist_lock);
461
462 head_req = list_first_entry_or_null(&ring->execlist_queue,
Nick Hoath6d3d8272015-01-15 13:10:39 +0000463 struct drm_i915_gem_request,
Thomas Daniele981e7b2014-07-24 17:04:39 +0100464 execlist_link);
465
466 if (head_req != NULL) {
467 struct drm_i915_gem_object *ctx_obj =
Nick Hoath6d3d8272015-01-15 13:10:39 +0000468 head_req->ctx->engine[ring->id].state;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100469 if (intel_execlists_ctx_id(ctx_obj) == request_id) {
Oscar Mateoe1fee722014-07-24 17:04:40 +0100470 WARN(head_req->elsp_submitted == 0,
471 "Never submitted head request\n");
472
473 if (--head_req->elsp_submitted <= 0) {
474 list_del(&head_req->execlist_link);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000475 list_add_tail(&head_req->execlist_link,
476 &ring->execlist_retired_req_list);
Oscar Mateoe1fee722014-07-24 17:04:40 +0100477 return true;
478 }
Thomas Daniele981e7b2014-07-24 17:04:39 +0100479 }
480 }
481
482 return false;
483}
484
Oscar Mateo73e4d072014-07-24 17:04:48 +0100485/**
Daniel Vetter3f7531c2014-12-10 17:41:43 +0100486 * intel_lrc_irq_handler() - handle Context Switch interrupts
Oscar Mateo73e4d072014-07-24 17:04:48 +0100487 * @ring: Engine Command Streamer to handle.
488 *
489 * Check the unread Context Status Buffers and manage the submission of new
490 * contexts to the ELSP accordingly.
491 */
Daniel Vetter3f7531c2014-12-10 17:41:43 +0100492void intel_lrc_irq_handler(struct intel_engine_cs *ring)
Thomas Daniele981e7b2014-07-24 17:04:39 +0100493{
494 struct drm_i915_private *dev_priv = ring->dev->dev_private;
495 u32 status_pointer;
496 u8 read_pointer;
497 u8 write_pointer;
498 u32 status;
499 u32 status_id;
500 u32 submit_contexts = 0;
501
502 status_pointer = I915_READ(RING_CONTEXT_STATUS_PTR(ring));
503
504 read_pointer = ring->next_context_status_buffer;
505 write_pointer = status_pointer & 0x07;
506 if (read_pointer > write_pointer)
507 write_pointer += 6;
508
509 spin_lock(&ring->execlist_lock);
510
511 while (read_pointer < write_pointer) {
512 read_pointer++;
513 status = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
514 (read_pointer % 6) * 8);
515 status_id = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
516 (read_pointer % 6) * 8 + 4);
517
Oscar Mateoe1fee722014-07-24 17:04:40 +0100518 if (status & GEN8_CTX_STATUS_PREEMPTED) {
519 if (status & GEN8_CTX_STATUS_LITE_RESTORE) {
520 if (execlists_check_remove_request(ring, status_id))
521 WARN(1, "Lite Restored request removed from queue\n");
522 } else
523 WARN(1, "Preemption without Lite Restore\n");
524 }
525
526 if ((status & GEN8_CTX_STATUS_ACTIVE_IDLE) ||
527 (status & GEN8_CTX_STATUS_ELEMENT_SWITCH)) {
Thomas Daniele981e7b2014-07-24 17:04:39 +0100528 if (execlists_check_remove_request(ring, status_id))
529 submit_contexts++;
530 }
531 }
532
533 if (submit_contexts != 0)
534 execlists_context_unqueue(ring);
535
536 spin_unlock(&ring->execlist_lock);
537
538 WARN(submit_contexts > 2, "More than two context complete events?\n");
539 ring->next_context_status_buffer = write_pointer % 6;
540
541 I915_WRITE(RING_CONTEXT_STATUS_PTR(ring),
542 ((u32)ring->next_context_status_buffer & 0x07) << 8);
543}
544
John Harrisonae707972015-05-29 17:44:14 +0100545static int execlists_context_queue(struct drm_i915_gem_request *request)
Michel Thierryacdd8842014-07-24 17:04:38 +0100546{
John Harrisonae707972015-05-29 17:44:14 +0100547 struct intel_engine_cs *ring = request->ring;
Nick Hoath6d3d8272015-01-15 13:10:39 +0000548 struct drm_i915_gem_request *cursor;
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100549 int num_elements = 0;
Michel Thierryacdd8842014-07-24 17:04:38 +0100550
John Harrisonae707972015-05-29 17:44:14 +0100551 if (request->ctx != ring->default_context)
552 intel_lr_context_pin(ring, request->ctx);
John Harrison9bb1af42015-05-29 17:44:13 +0100553
554 i915_gem_request_reference(request);
555
John Harrisonae707972015-05-29 17:44:14 +0100556 request->tail = request->ringbuf->tail;
Nick Hoath2d129552015-01-15 13:10:36 +0000557
Chris Wilsonb5eba372015-04-07 16:20:48 +0100558 spin_lock_irq(&ring->execlist_lock);
Michel Thierryacdd8842014-07-24 17:04:38 +0100559
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100560 list_for_each_entry(cursor, &ring->execlist_queue, execlist_link)
561 if (++num_elements > 2)
562 break;
563
564 if (num_elements > 2) {
Nick Hoath6d3d8272015-01-15 13:10:39 +0000565 struct drm_i915_gem_request *tail_req;
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100566
567 tail_req = list_last_entry(&ring->execlist_queue,
Nick Hoath6d3d8272015-01-15 13:10:39 +0000568 struct drm_i915_gem_request,
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100569 execlist_link);
570
John Harrisonae707972015-05-29 17:44:14 +0100571 if (request->ctx == tail_req->ctx) {
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100572 WARN(tail_req->elsp_submitted != 0,
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000573 "More than 2 already-submitted reqs queued\n");
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100574 list_del(&tail_req->execlist_link);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000575 list_add_tail(&tail_req->execlist_link,
576 &ring->execlist_retired_req_list);
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100577 }
578 }
579
Nick Hoath6d3d8272015-01-15 13:10:39 +0000580 list_add_tail(&request->execlist_link, &ring->execlist_queue);
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100581 if (num_elements == 0)
Michel Thierryacdd8842014-07-24 17:04:38 +0100582 execlists_context_unqueue(ring);
583
Chris Wilsonb5eba372015-04-07 16:20:48 +0100584 spin_unlock_irq(&ring->execlist_lock);
Michel Thierryacdd8842014-07-24 17:04:38 +0100585
586 return 0;
587}
588
John Harrison2f200552015-05-29 17:43:53 +0100589static int logical_ring_invalidate_all_caches(struct drm_i915_gem_request *req)
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100590{
John Harrison2f200552015-05-29 17:43:53 +0100591 struct intel_engine_cs *ring = req->ring;
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100592 uint32_t flush_domains;
593 int ret;
594
595 flush_domains = 0;
596 if (ring->gpu_caches_dirty)
597 flush_domains = I915_GEM_GPU_DOMAINS;
598
John Harrison7deb4d32015-05-29 17:43:59 +0100599 ret = ring->emit_flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100600 if (ret)
601 return ret;
602
603 ring->gpu_caches_dirty = false;
604 return 0;
605}
606
John Harrison535fbe82015-05-29 17:43:32 +0100607static int execlists_move_to_gpu(struct drm_i915_gem_request *req,
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100608 struct list_head *vmas)
609{
John Harrison535fbe82015-05-29 17:43:32 +0100610 const unsigned other_rings = ~intel_ring_flag(req->ring);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100611 struct i915_vma *vma;
612 uint32_t flush_domains = 0;
613 bool flush_chipset = false;
614 int ret;
615
616 list_for_each_entry(vma, vmas, exec_list) {
617 struct drm_i915_gem_object *obj = vma->obj;
618
Chris Wilson03ade512015-04-27 13:41:18 +0100619 if (obj->active & other_rings) {
John Harrison91af1272015-06-18 13:14:56 +0100620 ret = i915_gem_object_sync(obj, req->ring, &req);
Chris Wilson03ade512015-04-27 13:41:18 +0100621 if (ret)
622 return ret;
623 }
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100624
625 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
626 flush_chipset |= i915_gem_clflush_object(obj, false);
627
628 flush_domains |= obj->base.write_domain;
629 }
630
631 if (flush_domains & I915_GEM_DOMAIN_GTT)
632 wmb();
633
634 /* Unconditionally invalidate gpu caches and ensure that we do flush
635 * any residual writes from the previous batch.
636 */
John Harrison2f200552015-05-29 17:43:53 +0100637 return logical_ring_invalidate_all_caches(req);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100638}
639
John Harrison40e895c2015-05-29 17:43:26 +0100640int intel_logical_ring_alloc_request_extras(struct drm_i915_gem_request *request)
John Harrisonbc0dce32015-03-19 12:30:07 +0000641{
John Harrisonbc0dce32015-03-19 12:30:07 +0000642 int ret;
643
John Harrison40e895c2015-05-29 17:43:26 +0100644 if (request->ctx != request->ring->default_context) {
645 ret = intel_lr_context_pin(request->ring, request->ctx);
John Harrison6689cb22015-03-19 12:30:08 +0000646 if (ret)
John Harrisonbc0dce32015-03-19 12:30:07 +0000647 return ret;
John Harrisonbc0dce32015-03-19 12:30:07 +0000648 }
649
John Harrison40e895c2015-05-29 17:43:26 +0100650 request->ringbuf = request->ctx->engine[request->ring->id].ringbuf;
John Harrisonbc0dce32015-03-19 12:30:07 +0000651
John Harrisonbc0dce32015-03-19 12:30:07 +0000652 return 0;
653}
654
John Harrisonae707972015-05-29 17:44:14 +0100655static int logical_ring_wait_for_space(struct drm_i915_gem_request *req,
Chris Wilson595e1ee2015-04-07 16:20:51 +0100656 int bytes)
John Harrisonbc0dce32015-03-19 12:30:07 +0000657{
John Harrisonae707972015-05-29 17:44:14 +0100658 struct intel_ringbuffer *ringbuf = req->ringbuf;
659 struct intel_engine_cs *ring = req->ring;
660 struct drm_i915_gem_request *target;
Chris Wilsonb4716182015-04-27 13:41:17 +0100661 unsigned space;
662 int ret;
John Harrisonbc0dce32015-03-19 12:30:07 +0000663
John Harrison29b1b412015-06-18 13:10:09 +0100664 /* The whole point of reserving space is to not wait! */
665 WARN_ON(ringbuf->reserved_in_use);
666
John Harrisonbc0dce32015-03-19 12:30:07 +0000667 if (intel_ring_space(ringbuf) >= bytes)
668 return 0;
669
John Harrisonae707972015-05-29 17:44:14 +0100670 list_for_each_entry(target, &ring->request_list, list) {
John Harrisonbc0dce32015-03-19 12:30:07 +0000671 /*
672 * The request queue is per-engine, so can contain requests
673 * from multiple ringbuffers. Here, we must ignore any that
674 * aren't from the ringbuffer we're considering.
675 */
John Harrisonae707972015-05-29 17:44:14 +0100676 if (target->ringbuf != ringbuf)
John Harrisonbc0dce32015-03-19 12:30:07 +0000677 continue;
678
679 /* Would completion of this request free enough space? */
John Harrisonae707972015-05-29 17:44:14 +0100680 space = __intel_ring_space(target->postfix, ringbuf->tail,
Chris Wilsonb4716182015-04-27 13:41:17 +0100681 ringbuf->size);
682 if (space >= bytes)
John Harrisonbc0dce32015-03-19 12:30:07 +0000683 break;
John Harrisonbc0dce32015-03-19 12:30:07 +0000684 }
685
John Harrisonae707972015-05-29 17:44:14 +0100686 if (WARN_ON(&target->list == &ring->request_list))
John Harrisonbc0dce32015-03-19 12:30:07 +0000687 return -ENOSPC;
688
John Harrisonae707972015-05-29 17:44:14 +0100689 ret = i915_wait_request(target);
John Harrisonbc0dce32015-03-19 12:30:07 +0000690 if (ret)
691 return ret;
692
Chris Wilsonb4716182015-04-27 13:41:17 +0100693 ringbuf->space = space;
694 return 0;
John Harrisonbc0dce32015-03-19 12:30:07 +0000695}
696
697/*
698 * intel_logical_ring_advance_and_submit() - advance the tail and submit the workload
John Harrisonae707972015-05-29 17:44:14 +0100699 * @request: Request to advance the logical ringbuffer of.
John Harrisonbc0dce32015-03-19 12:30:07 +0000700 *
701 * The tail is updated in our logical ringbuffer struct, not in the actual context. What
702 * really happens during submission is that the context and current tail will be placed
703 * on a queue waiting for the ELSP to be ready to accept a new context submission. At that
704 * point, the tail *inside* the context is updated and the ELSP written to.
705 */
706static void
John Harrisonae707972015-05-29 17:44:14 +0100707intel_logical_ring_advance_and_submit(struct drm_i915_gem_request *request)
John Harrisonbc0dce32015-03-19 12:30:07 +0000708{
John Harrisonae707972015-05-29 17:44:14 +0100709 struct intel_engine_cs *ring = request->ring;
John Harrisonbc0dce32015-03-19 12:30:07 +0000710
John Harrisonae707972015-05-29 17:44:14 +0100711 intel_logical_ring_advance(request->ringbuf);
John Harrisonbc0dce32015-03-19 12:30:07 +0000712
713 if (intel_ring_stopped(ring))
714 return;
715
John Harrisonae707972015-05-29 17:44:14 +0100716 execlists_context_queue(request);
John Harrisonbc0dce32015-03-19 12:30:07 +0000717}
718
John Harrisonae707972015-05-29 17:44:14 +0100719static int logical_ring_wrap_buffer(struct drm_i915_gem_request *req)
John Harrisonbc0dce32015-03-19 12:30:07 +0000720{
John Harrisonae707972015-05-29 17:44:14 +0100721 struct intel_ringbuffer *ringbuf = req->ringbuf;
John Harrisonbc0dce32015-03-19 12:30:07 +0000722 uint32_t __iomem *virt;
723 int rem = ringbuf->size - ringbuf->tail;
724
John Harrison29b1b412015-06-18 13:10:09 +0100725 /* Can't wrap if space has already been reserved! */
726 WARN_ON(ringbuf->reserved_in_use);
727
John Harrisonbc0dce32015-03-19 12:30:07 +0000728 if (ringbuf->space < rem) {
John Harrisonae707972015-05-29 17:44:14 +0100729 int ret = logical_ring_wait_for_space(req, rem);
John Harrisonbc0dce32015-03-19 12:30:07 +0000730
731 if (ret)
732 return ret;
733 }
734
735 virt = ringbuf->virtual_start + ringbuf->tail;
736 rem /= 4;
737 while (rem--)
738 iowrite32(MI_NOOP, virt++);
739
740 ringbuf->tail = 0;
741 intel_ring_update_space(ringbuf);
742
743 return 0;
744}
745
John Harrisonae707972015-05-29 17:44:14 +0100746static int logical_ring_prepare(struct drm_i915_gem_request *req, int bytes)
John Harrisonbc0dce32015-03-19 12:30:07 +0000747{
John Harrisonae707972015-05-29 17:44:14 +0100748 struct intel_ringbuffer *ringbuf = req->ringbuf;
John Harrisonbc0dce32015-03-19 12:30:07 +0000749 int ret;
750
John Harrison29b1b412015-06-18 13:10:09 +0100751 /*
752 * Add on the reserved size to the request to make sure that after
753 * the intended commands have been emitted, there is guaranteed to
754 * still be enough free space to send them to the hardware.
755 */
756 if (!ringbuf->reserved_in_use)
757 bytes += ringbuf->reserved_size;
758
John Harrisonbc0dce32015-03-19 12:30:07 +0000759 if (unlikely(ringbuf->tail + bytes > ringbuf->effective_size)) {
John Harrisonae707972015-05-29 17:44:14 +0100760 ret = logical_ring_wrap_buffer(req);
John Harrisonbc0dce32015-03-19 12:30:07 +0000761 if (unlikely(ret))
762 return ret;
John Harrison29b1b412015-06-18 13:10:09 +0100763
764 if(ringbuf->reserved_size) {
765 uint32_t size = ringbuf->reserved_size;
766
767 intel_ring_reserved_space_cancel(ringbuf);
768 intel_ring_reserved_space_reserve(ringbuf, size);
769 }
John Harrisonbc0dce32015-03-19 12:30:07 +0000770 }
771
772 if (unlikely(ringbuf->space < bytes)) {
John Harrisonae707972015-05-29 17:44:14 +0100773 ret = logical_ring_wait_for_space(req, bytes);
John Harrisonbc0dce32015-03-19 12:30:07 +0000774 if (unlikely(ret))
775 return ret;
776 }
777
778 return 0;
779}
780
781/**
782 * intel_logical_ring_begin() - prepare the logical ringbuffer to accept some commands
783 *
John Harrison4d616a22015-05-29 17:44:08 +0100784 * @request: The request to start some new work for
Arun Siluvery4d78c8d2015-06-23 15:50:43 +0100785 * @ctx: Logical ring context whose ringbuffer is being prepared.
John Harrisonbc0dce32015-03-19 12:30:07 +0000786 * @num_dwords: number of DWORDs that we plan to write to the ringbuffer.
787 *
788 * The ringbuffer might not be ready to accept the commands right away (maybe it needs to
789 * be wrapped, or wait a bit for the tail to be updated). This function takes care of that
790 * and also preallocates a request (every workload submission is still mediated through
791 * requests, same as it did with legacy ringbuffer submission).
792 *
793 * Return: non-zero if the ringbuffer is not ready to be written to.
794 */
John Harrison4d616a22015-05-29 17:44:08 +0100795static int intel_logical_ring_begin(struct drm_i915_gem_request *req,
796 int num_dwords)
John Harrisonbc0dce32015-03-19 12:30:07 +0000797{
John Harrison4d616a22015-05-29 17:44:08 +0100798 struct drm_i915_private *dev_priv;
John Harrisonbc0dce32015-03-19 12:30:07 +0000799 int ret;
800
John Harrison4d616a22015-05-29 17:44:08 +0100801 WARN_ON(req == NULL);
802 dev_priv = req->ring->dev->dev_private;
803
John Harrisonbc0dce32015-03-19 12:30:07 +0000804 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
805 dev_priv->mm.interruptible);
806 if (ret)
807 return ret;
808
John Harrisonae707972015-05-29 17:44:14 +0100809 ret = logical_ring_prepare(req, num_dwords * sizeof(uint32_t));
John Harrisonbc0dce32015-03-19 12:30:07 +0000810 if (ret)
811 return ret;
812
John Harrison4d616a22015-05-29 17:44:08 +0100813 req->ringbuf->space -= num_dwords * sizeof(uint32_t);
John Harrisonbc0dce32015-03-19 12:30:07 +0000814 return 0;
815}
816
John Harrisonccd98fe2015-05-29 17:44:09 +0100817int intel_logical_ring_reserve_space(struct drm_i915_gem_request *request)
818{
819 /*
820 * The first call merely notes the reserve request and is common for
821 * all back ends. The subsequent localised _begin() call actually
822 * ensures that the reservation is available. Without the begin, if
823 * the request creator immediately submitted the request without
824 * adding any commands to it then there might not actually be
825 * sufficient room for the submission commands.
826 */
827 intel_ring_reserved_space_reserve(request->ringbuf, MIN_SPACE_FOR_ADD_REQUEST);
828
829 return intel_logical_ring_begin(request, 0);
830}
831
Oscar Mateo73e4d072014-07-24 17:04:48 +0100832/**
833 * execlists_submission() - submit a batchbuffer for execution, Execlists style
834 * @dev: DRM device.
835 * @file: DRM file.
836 * @ring: Engine Command Streamer to submit to.
837 * @ctx: Context to employ for this submission.
838 * @args: execbuffer call arguments.
839 * @vmas: list of vmas.
840 * @batch_obj: the batchbuffer to submit.
841 * @exec_start: batchbuffer start virtual address pointer.
John Harrison8e004ef2015-02-13 11:48:10 +0000842 * @dispatch_flags: translated execbuffer call flags.
Oscar Mateo73e4d072014-07-24 17:04:48 +0100843 *
844 * This is the evil twin version of i915_gem_ringbuffer_submission. It abstracts
845 * away the submission details of the execbuffer ioctl call.
846 *
847 * Return: non-zero if the submission fails.
848 */
John Harrison5f19e2b2015-05-29 17:43:27 +0100849int intel_execlists_submission(struct i915_execbuffer_params *params,
Oscar Mateo454afeb2014-07-24 17:04:22 +0100850 struct drm_i915_gem_execbuffer2 *args,
John Harrison5f19e2b2015-05-29 17:43:27 +0100851 struct list_head *vmas)
Oscar Mateo454afeb2014-07-24 17:04:22 +0100852{
John Harrison5f19e2b2015-05-29 17:43:27 +0100853 struct drm_device *dev = params->dev;
854 struct intel_engine_cs *ring = params->ring;
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100855 struct drm_i915_private *dev_priv = dev->dev_private;
John Harrison5f19e2b2015-05-29 17:43:27 +0100856 struct intel_ringbuffer *ringbuf = params->ctx->engine[ring->id].ringbuf;
857 u64 exec_start;
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100858 int instp_mode;
859 u32 instp_mask;
860 int ret;
861
862 instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
863 instp_mask = I915_EXEC_CONSTANTS_MASK;
864 switch (instp_mode) {
865 case I915_EXEC_CONSTANTS_REL_GENERAL:
866 case I915_EXEC_CONSTANTS_ABSOLUTE:
867 case I915_EXEC_CONSTANTS_REL_SURFACE:
868 if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) {
869 DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
870 return -EINVAL;
871 }
872
873 if (instp_mode != dev_priv->relative_constants_mode) {
874 if (instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
875 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
876 return -EINVAL;
877 }
878
879 /* The HW changed the meaning on this bit on gen6 */
880 instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
881 }
882 break;
883 default:
884 DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
885 return -EINVAL;
886 }
887
888 if (args->num_cliprects != 0) {
889 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
890 return -EINVAL;
891 } else {
892 if (args->DR4 == 0xffffffff) {
893 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
894 args->DR4 = 0;
895 }
896
897 if (args->DR1 || args->DR4 || args->cliprects_ptr) {
898 DRM_DEBUG("0 cliprects but dirt in cliprects fields\n");
899 return -EINVAL;
900 }
901 }
902
903 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
904 DRM_DEBUG("sol reset is gen7 only\n");
905 return -EINVAL;
906 }
907
John Harrison535fbe82015-05-29 17:43:32 +0100908 ret = execlists_move_to_gpu(params->request, vmas);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100909 if (ret)
910 return ret;
911
912 if (ring == &dev_priv->ring[RCS] &&
913 instp_mode != dev_priv->relative_constants_mode) {
John Harrison4d616a22015-05-29 17:44:08 +0100914 ret = intel_logical_ring_begin(params->request, 4);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100915 if (ret)
916 return ret;
917
918 intel_logical_ring_emit(ringbuf, MI_NOOP);
919 intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(1));
920 intel_logical_ring_emit(ringbuf, INSTPM);
921 intel_logical_ring_emit(ringbuf, instp_mask << 16 | instp_mode);
922 intel_logical_ring_advance(ringbuf);
923
924 dev_priv->relative_constants_mode = instp_mode;
925 }
926
John Harrison5f19e2b2015-05-29 17:43:27 +0100927 exec_start = params->batch_obj_vm_offset +
928 args->batch_start_offset;
929
John Harrisonbe795fc2015-05-29 17:44:03 +0100930 ret = ring->emit_bb_start(params->request, exec_start, params->dispatch_flags);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100931 if (ret)
932 return ret;
933
John Harrison95c24162015-05-29 17:43:31 +0100934 trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
John Harrison5e4be7b2015-02-13 11:48:11 +0000935
John Harrison8a8edb52015-05-29 17:43:33 +0100936 i915_gem_execbuffer_move_to_active(vmas, params->request);
John Harrisonadeca762015-05-29 17:43:28 +0100937 i915_gem_execbuffer_retire_commands(params);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100938
Oscar Mateo454afeb2014-07-24 17:04:22 +0100939 return 0;
940}
941
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000942void intel_execlists_retire_requests(struct intel_engine_cs *ring)
943{
Nick Hoath6d3d8272015-01-15 13:10:39 +0000944 struct drm_i915_gem_request *req, *tmp;
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000945 struct list_head retired_list;
946
947 WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
948 if (list_empty(&ring->execlist_retired_req_list))
949 return;
950
951 INIT_LIST_HEAD(&retired_list);
Chris Wilsonb5eba372015-04-07 16:20:48 +0100952 spin_lock_irq(&ring->execlist_lock);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000953 list_replace_init(&ring->execlist_retired_req_list, &retired_list);
Chris Wilsonb5eba372015-04-07 16:20:48 +0100954 spin_unlock_irq(&ring->execlist_lock);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000955
956 list_for_each_entry_safe(req, tmp, &retired_list, execlist_link) {
Nick Hoath6d3d8272015-01-15 13:10:39 +0000957 struct intel_context *ctx = req->ctx;
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000958 struct drm_i915_gem_object *ctx_obj =
959 ctx->engine[ring->id].state;
960
961 if (ctx_obj && (ctx != ring->default_context))
962 intel_lr_context_unpin(ring, ctx);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000963 list_del(&req->execlist_link);
Nick Hoathf8210792015-01-29 16:55:07 +0000964 i915_gem_request_unreference(req);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000965 }
966}
967
Oscar Mateo454afeb2014-07-24 17:04:22 +0100968void intel_logical_ring_stop(struct intel_engine_cs *ring)
969{
Oscar Mateo9832b9d2014-07-24 17:04:30 +0100970 struct drm_i915_private *dev_priv = ring->dev->dev_private;
971 int ret;
972
973 if (!intel_ring_initialized(ring))
974 return;
975
976 ret = intel_ring_idle(ring);
977 if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
978 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
979 ring->name, ret);
980
981 /* TODO: Is this correct with Execlists enabled? */
982 I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
983 if (wait_for_atomic((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
984 DRM_ERROR("%s :timed out trying to stop ring\n", ring->name);
985 return;
986 }
987 I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
Oscar Mateo454afeb2014-07-24 17:04:22 +0100988}
989
John Harrison4866d722015-05-29 17:43:55 +0100990int logical_ring_flush_all_caches(struct drm_i915_gem_request *req)
Oscar Mateo48e29f52014-07-24 17:04:29 +0100991{
John Harrison4866d722015-05-29 17:43:55 +0100992 struct intel_engine_cs *ring = req->ring;
Oscar Mateo48e29f52014-07-24 17:04:29 +0100993 int ret;
994
995 if (!ring->gpu_caches_dirty)
996 return 0;
997
John Harrison7deb4d32015-05-29 17:43:59 +0100998 ret = ring->emit_flush(req, 0, I915_GEM_GPU_DOMAINS);
Oscar Mateo48e29f52014-07-24 17:04:29 +0100999 if (ret)
1000 return ret;
1001
1002 ring->gpu_caches_dirty = false;
1003 return 0;
1004}
1005
Oscar Mateodcb4c122014-11-13 10:28:10 +00001006static int intel_lr_context_pin(struct intel_engine_cs *ring,
1007 struct intel_context *ctx)
1008{
1009 struct drm_i915_gem_object *ctx_obj = ctx->engine[ring->id].state;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001010 struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
Oscar Mateodcb4c122014-11-13 10:28:10 +00001011 int ret = 0;
1012
1013 WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
Mika Kuoppalaa7cbede2015-01-13 11:32:25 +02001014 if (ctx->engine[ring->id].pin_count++ == 0) {
Oscar Mateodcb4c122014-11-13 10:28:10 +00001015 ret = i915_gem_obj_ggtt_pin(ctx_obj,
1016 GEN8_LR_CONTEXT_ALIGN, 0);
1017 if (ret)
Mika Kuoppalaa7cbede2015-01-13 11:32:25 +02001018 goto reset_pin_count;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001019
1020 ret = intel_pin_and_map_ringbuffer_obj(ring->dev, ringbuf);
1021 if (ret)
1022 goto unpin_ctx_obj;
Oscar Mateodcb4c122014-11-13 10:28:10 +00001023 }
1024
1025 return ret;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001026
1027unpin_ctx_obj:
1028 i915_gem_object_ggtt_unpin(ctx_obj);
Mika Kuoppalaa7cbede2015-01-13 11:32:25 +02001029reset_pin_count:
1030 ctx->engine[ring->id].pin_count = 0;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001031
1032 return ret;
Oscar Mateodcb4c122014-11-13 10:28:10 +00001033}
1034
1035void intel_lr_context_unpin(struct intel_engine_cs *ring,
1036 struct intel_context *ctx)
1037{
1038 struct drm_i915_gem_object *ctx_obj = ctx->engine[ring->id].state;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001039 struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
Oscar Mateodcb4c122014-11-13 10:28:10 +00001040
1041 if (ctx_obj) {
1042 WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
Mika Kuoppalaa7cbede2015-01-13 11:32:25 +02001043 if (--ctx->engine[ring->id].pin_count == 0) {
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001044 intel_unpin_ringbuffer_obj(ringbuf);
Oscar Mateodcb4c122014-11-13 10:28:10 +00001045 i915_gem_object_ggtt_unpin(ctx_obj);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001046 }
Oscar Mateodcb4c122014-11-13 10:28:10 +00001047 }
1048}
1049
John Harrisone2be4fa2015-05-29 17:43:54 +01001050static int intel_logical_ring_workarounds_emit(struct drm_i915_gem_request *req)
Michel Thierry771b9a52014-11-11 16:47:33 +00001051{
1052 int ret, i;
John Harrisone2be4fa2015-05-29 17:43:54 +01001053 struct intel_engine_cs *ring = req->ring;
1054 struct intel_ringbuffer *ringbuf = req->ringbuf;
Michel Thierry771b9a52014-11-11 16:47:33 +00001055 struct drm_device *dev = ring->dev;
1056 struct drm_i915_private *dev_priv = dev->dev_private;
1057 struct i915_workarounds *w = &dev_priv->workarounds;
1058
Michel Thierrye6c1abb2014-11-26 14:21:02 +00001059 if (WARN_ON_ONCE(w->count == 0))
Michel Thierry771b9a52014-11-11 16:47:33 +00001060 return 0;
1061
1062 ring->gpu_caches_dirty = true;
John Harrison4866d722015-05-29 17:43:55 +01001063 ret = logical_ring_flush_all_caches(req);
Michel Thierry771b9a52014-11-11 16:47:33 +00001064 if (ret)
1065 return ret;
1066
John Harrison4d616a22015-05-29 17:44:08 +01001067 ret = intel_logical_ring_begin(req, w->count * 2 + 2);
Michel Thierry771b9a52014-11-11 16:47:33 +00001068 if (ret)
1069 return ret;
1070
1071 intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(w->count));
1072 for (i = 0; i < w->count; i++) {
1073 intel_logical_ring_emit(ringbuf, w->reg[i].addr);
1074 intel_logical_ring_emit(ringbuf, w->reg[i].value);
1075 }
1076 intel_logical_ring_emit(ringbuf, MI_NOOP);
1077
1078 intel_logical_ring_advance(ringbuf);
1079
1080 ring->gpu_caches_dirty = true;
John Harrison4866d722015-05-29 17:43:55 +01001081 ret = logical_ring_flush_all_caches(req);
Michel Thierry771b9a52014-11-11 16:47:33 +00001082 if (ret)
1083 return ret;
1084
1085 return 0;
1086}
1087
Arun Siluvery17ee9502015-06-19 19:07:01 +01001088#define wa_ctx_emit(batch, cmd) \
1089 do { \
1090 if (WARN_ON(index >= (PAGE_SIZE / sizeof(uint32_t)))) { \
1091 return -ENOSPC; \
1092 } \
1093 batch[index++] = (cmd); \
1094 } while (0)
1095
1096static inline uint32_t wa_ctx_start(struct i915_wa_ctx_bb *wa_ctx,
1097 uint32_t offset,
1098 uint32_t start_alignment)
1099{
1100 return wa_ctx->offset = ALIGN(offset, start_alignment);
1101}
1102
1103static inline int wa_ctx_end(struct i915_wa_ctx_bb *wa_ctx,
1104 uint32_t offset,
1105 uint32_t size_alignment)
1106{
1107 wa_ctx->size = offset - wa_ctx->offset;
1108
1109 WARN(wa_ctx->size % size_alignment,
1110 "wa_ctx_bb failed sanity checks: size %d is not aligned to %d\n",
1111 wa_ctx->size, size_alignment);
1112 return 0;
1113}
1114
1115/**
1116 * gen8_init_indirectctx_bb() - initialize indirect ctx batch with WA
1117 *
1118 * @ring: only applicable for RCS
1119 * @wa_ctx: structure representing wa_ctx
1120 * offset: specifies start of the batch, should be cache-aligned. This is updated
1121 * with the offset value received as input.
1122 * size: size of the batch in DWORDS but HW expects in terms of cachelines
1123 * @batch: page in which WA are loaded
1124 * @offset: This field specifies the start of the batch, it should be
1125 * cache-aligned otherwise it is adjusted accordingly.
1126 * Typically we only have one indirect_ctx and per_ctx batch buffer which are
1127 * initialized at the beginning and shared across all contexts but this field
1128 * helps us to have multiple batches at different offsets and select them based
1129 * on a criteria. At the moment this batch always start at the beginning of the page
1130 * and at this point we don't have multiple wa_ctx batch buffers.
1131 *
1132 * The number of WA applied are not known at the beginning; we use this field
1133 * to return the no of DWORDS written.
Arun Siluvery4d78c8d2015-06-23 15:50:43 +01001134 *
Arun Siluvery17ee9502015-06-19 19:07:01 +01001135 * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
1136 * so it adds NOOPs as padding to make it cacheline aligned.
1137 * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
1138 * makes a complete batch buffer.
1139 *
1140 * Return: non-zero if we exceed the PAGE_SIZE limit.
1141 */
1142
1143static int gen8_init_indirectctx_bb(struct intel_engine_cs *ring,
1144 struct i915_wa_ctx_bb *wa_ctx,
1145 uint32_t *const batch,
1146 uint32_t *offset)
1147{
Arun Siluvery0160f052015-06-23 15:46:57 +01001148 uint32_t scratch_addr;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001149 uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
1150
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001151 /* WaDisableCtxRestoreArbitration:bdw,chv */
1152 wa_ctx_emit(batch, MI_ARB_ON_OFF | MI_ARB_DISABLE);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001153
Arun Siluveryc82435b2015-06-19 18:37:13 +01001154 /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
1155 if (IS_BROADWELL(ring->dev)) {
1156 struct drm_i915_private *dev_priv = to_i915(ring->dev);
1157 uint32_t l3sqc4_flush = (I915_READ(GEN8_L3SQCREG4) |
1158 GEN8_LQSC_FLUSH_COHERENT_LINES);
1159
1160 wa_ctx_emit(batch, MI_LOAD_REGISTER_IMM(1));
1161 wa_ctx_emit(batch, GEN8_L3SQCREG4);
1162 wa_ctx_emit(batch, l3sqc4_flush);
1163
1164 wa_ctx_emit(batch, GFX_OP_PIPE_CONTROL(6));
1165 wa_ctx_emit(batch, (PIPE_CONTROL_CS_STALL |
1166 PIPE_CONTROL_DC_FLUSH_ENABLE));
1167 wa_ctx_emit(batch, 0);
1168 wa_ctx_emit(batch, 0);
1169 wa_ctx_emit(batch, 0);
1170 wa_ctx_emit(batch, 0);
1171
1172 wa_ctx_emit(batch, MI_LOAD_REGISTER_IMM(1));
1173 wa_ctx_emit(batch, GEN8_L3SQCREG4);
1174 wa_ctx_emit(batch, l3sqc4_flush & ~GEN8_LQSC_FLUSH_COHERENT_LINES);
1175 }
1176
Arun Siluvery0160f052015-06-23 15:46:57 +01001177 /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
1178 /* Actual scratch location is at 128 bytes offset */
1179 scratch_addr = ring->scratch.gtt_offset + 2*CACHELINE_BYTES;
1180
1181 wa_ctx_emit(batch, GFX_OP_PIPE_CONTROL(6));
1182 wa_ctx_emit(batch, (PIPE_CONTROL_FLUSH_L3 |
1183 PIPE_CONTROL_GLOBAL_GTT_IVB |
1184 PIPE_CONTROL_CS_STALL |
1185 PIPE_CONTROL_QW_WRITE));
1186 wa_ctx_emit(batch, scratch_addr);
1187 wa_ctx_emit(batch, 0);
1188 wa_ctx_emit(batch, 0);
1189 wa_ctx_emit(batch, 0);
1190
Arun Siluvery17ee9502015-06-19 19:07:01 +01001191 /* Pad to end of cacheline */
1192 while (index % CACHELINE_DWORDS)
1193 wa_ctx_emit(batch, MI_NOOP);
1194
1195 /*
1196 * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
1197 * execution depends on the length specified in terms of cache lines
1198 * in the register CTX_RCS_INDIRECT_CTX
1199 */
1200
1201 return wa_ctx_end(wa_ctx, *offset = index, CACHELINE_DWORDS);
1202}
1203
1204/**
1205 * gen8_init_perctx_bb() - initialize per ctx batch with WA
1206 *
1207 * @ring: only applicable for RCS
1208 * @wa_ctx: structure representing wa_ctx
1209 * offset: specifies start of the batch, should be cache-aligned.
1210 * size: size of the batch in DWORDS but HW expects in terms of cachelines
Arun Siluvery4d78c8d2015-06-23 15:50:43 +01001211 * @batch: page in which WA are loaded
Arun Siluvery17ee9502015-06-19 19:07:01 +01001212 * @offset: This field specifies the start of this batch.
1213 * This batch is started immediately after indirect_ctx batch. Since we ensure
1214 * that indirect_ctx ends on a cacheline this batch is aligned automatically.
1215 *
1216 * The number of DWORDS written are returned using this field.
1217 *
1218 * This batch is terminated with MI_BATCH_BUFFER_END and so we need not add padding
1219 * to align it with cacheline as padding after MI_BATCH_BUFFER_END is redundant.
1220 */
1221static int gen8_init_perctx_bb(struct intel_engine_cs *ring,
1222 struct i915_wa_ctx_bb *wa_ctx,
1223 uint32_t *const batch,
1224 uint32_t *offset)
1225{
1226 uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
1227
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001228 /* WaDisableCtxRestoreArbitration:bdw,chv */
1229 wa_ctx_emit(batch, MI_ARB_ON_OFF | MI_ARB_ENABLE);
1230
Arun Siluvery17ee9502015-06-19 19:07:01 +01001231 wa_ctx_emit(batch, MI_BATCH_BUFFER_END);
1232
1233 return wa_ctx_end(wa_ctx, *offset = index, 1);
1234}
1235
1236static int lrc_setup_wa_ctx_obj(struct intel_engine_cs *ring, u32 size)
1237{
1238 int ret;
1239
1240 ring->wa_ctx.obj = i915_gem_alloc_object(ring->dev, PAGE_ALIGN(size));
1241 if (!ring->wa_ctx.obj) {
1242 DRM_DEBUG_DRIVER("alloc LRC WA ctx backing obj failed.\n");
1243 return -ENOMEM;
1244 }
1245
1246 ret = i915_gem_obj_ggtt_pin(ring->wa_ctx.obj, PAGE_SIZE, 0);
1247 if (ret) {
1248 DRM_DEBUG_DRIVER("pin LRC WA ctx backing obj failed: %d\n",
1249 ret);
1250 drm_gem_object_unreference(&ring->wa_ctx.obj->base);
1251 return ret;
1252 }
1253
1254 return 0;
1255}
1256
1257static void lrc_destroy_wa_ctx_obj(struct intel_engine_cs *ring)
1258{
1259 if (ring->wa_ctx.obj) {
1260 i915_gem_object_ggtt_unpin(ring->wa_ctx.obj);
1261 drm_gem_object_unreference(&ring->wa_ctx.obj->base);
1262 ring->wa_ctx.obj = NULL;
1263 }
1264}
1265
1266static int intel_init_workaround_bb(struct intel_engine_cs *ring)
1267{
1268 int ret;
1269 uint32_t *batch;
1270 uint32_t offset;
1271 struct page *page;
1272 struct i915_ctx_workarounds *wa_ctx = &ring->wa_ctx;
1273
1274 WARN_ON(ring->id != RCS);
1275
Arun Siluvery5e60d792015-06-23 15:50:44 +01001276 /* update this when WA for higher Gen are added */
1277 if (WARN(INTEL_INFO(ring->dev)->gen > 8,
1278 "WA batch buffer is not initialized for Gen%d\n",
1279 INTEL_INFO(ring->dev)->gen))
1280 return 0;
1281
Arun Siluveryc4db7592015-06-19 18:37:11 +01001282 /* some WA perform writes to scratch page, ensure it is valid */
1283 if (ring->scratch.obj == NULL) {
1284 DRM_ERROR("scratch page not allocated for %s\n", ring->name);
1285 return -EINVAL;
1286 }
1287
Arun Siluvery17ee9502015-06-19 19:07:01 +01001288 ret = lrc_setup_wa_ctx_obj(ring, PAGE_SIZE);
1289 if (ret) {
1290 DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
1291 return ret;
1292 }
1293
1294 page = i915_gem_object_get_page(wa_ctx->obj, 0);
1295 batch = kmap_atomic(page);
1296 offset = 0;
1297
1298 if (INTEL_INFO(ring->dev)->gen == 8) {
1299 ret = gen8_init_indirectctx_bb(ring,
1300 &wa_ctx->indirect_ctx,
1301 batch,
1302 &offset);
1303 if (ret)
1304 goto out;
1305
1306 ret = gen8_init_perctx_bb(ring,
1307 &wa_ctx->per_ctx,
1308 batch,
1309 &offset);
1310 if (ret)
1311 goto out;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001312 }
1313
1314out:
1315 kunmap_atomic(batch);
1316 if (ret)
1317 lrc_destroy_wa_ctx_obj(ring);
1318
1319 return ret;
1320}
1321
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001322static int gen8_init_common_ring(struct intel_engine_cs *ring)
1323{
1324 struct drm_device *dev = ring->dev;
1325 struct drm_i915_private *dev_priv = dev->dev_private;
1326
Oscar Mateo73d477f2014-07-24 17:04:31 +01001327 I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
1328 I915_WRITE(RING_HWSTAM(ring->mmio_base), 0xffffffff);
1329
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001330 I915_WRITE(RING_MODE_GEN7(ring),
1331 _MASKED_BIT_DISABLE(GFX_REPLAY_MODE) |
1332 _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
1333 POSTING_READ(RING_MODE_GEN7(ring));
Thomas Danielc0a03a22015-01-09 11:09:37 +00001334 ring->next_context_status_buffer = 0;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001335 DRM_DEBUG_DRIVER("Execlists enabled for %s\n", ring->name);
1336
1337 memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
1338
1339 return 0;
1340}
1341
1342static int gen8_init_render_ring(struct intel_engine_cs *ring)
1343{
1344 struct drm_device *dev = ring->dev;
1345 struct drm_i915_private *dev_priv = dev->dev_private;
1346 int ret;
1347
1348 ret = gen8_init_common_ring(ring);
1349 if (ret)
1350 return ret;
1351
1352 /* We need to disable the AsyncFlip performance optimisations in order
1353 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1354 * programmed to '1' on all products.
1355 *
1356 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
1357 */
1358 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1359
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001360 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1361
Michel Thierry771b9a52014-11-11 16:47:33 +00001362 return init_workarounds_ring(ring);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001363}
1364
Damien Lespiau82ef8222015-02-09 19:33:08 +00001365static int gen9_init_render_ring(struct intel_engine_cs *ring)
1366{
1367 int ret;
1368
1369 ret = gen8_init_common_ring(ring);
1370 if (ret)
1371 return ret;
1372
1373 return init_workarounds_ring(ring);
1374}
1375
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001376static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request *req)
1377{
1378 struct i915_hw_ppgtt *ppgtt = req->ctx->ppgtt;
1379 struct intel_engine_cs *ring = req->ring;
1380 struct intel_ringbuffer *ringbuf = req->ringbuf;
1381 const int num_lri_cmds = GEN8_LEGACY_PDPES * 2;
1382 int i, ret;
1383
1384 ret = intel_logical_ring_begin(req, num_lri_cmds * 2 + 2);
1385 if (ret)
1386 return ret;
1387
1388 intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(num_lri_cmds));
1389 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
1390 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
1391
1392 intel_logical_ring_emit(ringbuf, GEN8_RING_PDP_UDW(ring, i));
1393 intel_logical_ring_emit(ringbuf, upper_32_bits(pd_daddr));
1394 intel_logical_ring_emit(ringbuf, GEN8_RING_PDP_LDW(ring, i));
1395 intel_logical_ring_emit(ringbuf, lower_32_bits(pd_daddr));
1396 }
1397
1398 intel_logical_ring_emit(ringbuf, MI_NOOP);
1399 intel_logical_ring_advance(ringbuf);
1400
1401 return 0;
1402}
1403
John Harrisonbe795fc2015-05-29 17:44:03 +01001404static int gen8_emit_bb_start(struct drm_i915_gem_request *req,
John Harrison8e004ef2015-02-13 11:48:10 +00001405 u64 offset, unsigned dispatch_flags)
Oscar Mateo15648582014-07-24 17:04:32 +01001406{
John Harrisonbe795fc2015-05-29 17:44:03 +01001407 struct intel_ringbuffer *ringbuf = req->ringbuf;
John Harrison8e004ef2015-02-13 11:48:10 +00001408 bool ppgtt = !(dispatch_flags & I915_DISPATCH_SECURE);
Oscar Mateo15648582014-07-24 17:04:32 +01001409 int ret;
1410
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001411 /* Don't rely in hw updating PDPs, specially in lite-restore.
1412 * Ideally, we should set Force PD Restore in ctx descriptor,
1413 * but we can't. Force Restore would be a second option, but
1414 * it is unsafe in case of lite-restore (because the ctx is
1415 * not idle). */
1416 if (req->ctx->ppgtt &&
1417 (intel_ring_flag(req->ring) & req->ctx->ppgtt->pd_dirty_rings)) {
1418 ret = intel_logical_ring_emit_pdps(req);
1419 if (ret)
1420 return ret;
1421
1422 req->ctx->ppgtt->pd_dirty_rings &= ~intel_ring_flag(req->ring);
1423 }
1424
John Harrison4d616a22015-05-29 17:44:08 +01001425 ret = intel_logical_ring_begin(req, 4);
Oscar Mateo15648582014-07-24 17:04:32 +01001426 if (ret)
1427 return ret;
1428
1429 /* FIXME(BDW): Address space and security selectors. */
1430 intel_logical_ring_emit(ringbuf, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8));
1431 intel_logical_ring_emit(ringbuf, lower_32_bits(offset));
1432 intel_logical_ring_emit(ringbuf, upper_32_bits(offset));
1433 intel_logical_ring_emit(ringbuf, MI_NOOP);
1434 intel_logical_ring_advance(ringbuf);
1435
1436 return 0;
1437}
1438
Oscar Mateo73d477f2014-07-24 17:04:31 +01001439static bool gen8_logical_ring_get_irq(struct intel_engine_cs *ring)
1440{
1441 struct drm_device *dev = ring->dev;
1442 struct drm_i915_private *dev_priv = dev->dev_private;
1443 unsigned long flags;
1444
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001445 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Oscar Mateo73d477f2014-07-24 17:04:31 +01001446 return false;
1447
1448 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1449 if (ring->irq_refcount++ == 0) {
1450 I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
1451 POSTING_READ(RING_IMR(ring->mmio_base));
1452 }
1453 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1454
1455 return true;
1456}
1457
1458static void gen8_logical_ring_put_irq(struct intel_engine_cs *ring)
1459{
1460 struct drm_device *dev = ring->dev;
1461 struct drm_i915_private *dev_priv = dev->dev_private;
1462 unsigned long flags;
1463
1464 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1465 if (--ring->irq_refcount == 0) {
1466 I915_WRITE_IMR(ring, ~ring->irq_keep_mask);
1467 POSTING_READ(RING_IMR(ring->mmio_base));
1468 }
1469 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1470}
1471
John Harrison7deb4d32015-05-29 17:43:59 +01001472static int gen8_emit_flush(struct drm_i915_gem_request *request,
Oscar Mateo47122742014-07-24 17:04:28 +01001473 u32 invalidate_domains,
1474 u32 unused)
1475{
John Harrison7deb4d32015-05-29 17:43:59 +01001476 struct intel_ringbuffer *ringbuf = request->ringbuf;
Oscar Mateo47122742014-07-24 17:04:28 +01001477 struct intel_engine_cs *ring = ringbuf->ring;
1478 struct drm_device *dev = ring->dev;
1479 struct drm_i915_private *dev_priv = dev->dev_private;
1480 uint32_t cmd;
1481 int ret;
1482
John Harrison4d616a22015-05-29 17:44:08 +01001483 ret = intel_logical_ring_begin(request, 4);
Oscar Mateo47122742014-07-24 17:04:28 +01001484 if (ret)
1485 return ret;
1486
1487 cmd = MI_FLUSH_DW + 1;
1488
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001489 /* We always require a command barrier so that subsequent
1490 * commands, such as breadcrumb interrupts, are strictly ordered
1491 * wrt the contents of the write cache being flushed to memory
1492 * (and thus being coherent from the CPU).
1493 */
1494 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1495
1496 if (invalidate_domains & I915_GEM_GPU_DOMAINS) {
1497 cmd |= MI_INVALIDATE_TLB;
1498 if (ring == &dev_priv->ring[VCS])
1499 cmd |= MI_INVALIDATE_BSD;
Oscar Mateo47122742014-07-24 17:04:28 +01001500 }
1501
1502 intel_logical_ring_emit(ringbuf, cmd);
1503 intel_logical_ring_emit(ringbuf,
1504 I915_GEM_HWS_SCRATCH_ADDR |
1505 MI_FLUSH_DW_USE_GTT);
1506 intel_logical_ring_emit(ringbuf, 0); /* upper addr */
1507 intel_logical_ring_emit(ringbuf, 0); /* value */
1508 intel_logical_ring_advance(ringbuf);
1509
1510 return 0;
1511}
1512
John Harrison7deb4d32015-05-29 17:43:59 +01001513static int gen8_emit_flush_render(struct drm_i915_gem_request *request,
Oscar Mateo47122742014-07-24 17:04:28 +01001514 u32 invalidate_domains,
1515 u32 flush_domains)
1516{
John Harrison7deb4d32015-05-29 17:43:59 +01001517 struct intel_ringbuffer *ringbuf = request->ringbuf;
Oscar Mateo47122742014-07-24 17:04:28 +01001518 struct intel_engine_cs *ring = ringbuf->ring;
1519 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Imre Deak9647ff32015-01-25 13:27:11 -08001520 bool vf_flush_wa;
Oscar Mateo47122742014-07-24 17:04:28 +01001521 u32 flags = 0;
1522 int ret;
1523
1524 flags |= PIPE_CONTROL_CS_STALL;
1525
1526 if (flush_domains) {
1527 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
1528 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
1529 }
1530
1531 if (invalidate_domains) {
1532 flags |= PIPE_CONTROL_TLB_INVALIDATE;
1533 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
1534 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
1535 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
1536 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
1537 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
1538 flags |= PIPE_CONTROL_QW_WRITE;
1539 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
1540 }
1541
Imre Deak9647ff32015-01-25 13:27:11 -08001542 /*
1543 * On GEN9+ Before VF_CACHE_INVALIDATE we need to emit a NULL pipe
1544 * control.
1545 */
1546 vf_flush_wa = INTEL_INFO(ring->dev)->gen >= 9 &&
1547 flags & PIPE_CONTROL_VF_CACHE_INVALIDATE;
1548
John Harrison4d616a22015-05-29 17:44:08 +01001549 ret = intel_logical_ring_begin(request, vf_flush_wa ? 12 : 6);
Oscar Mateo47122742014-07-24 17:04:28 +01001550 if (ret)
1551 return ret;
1552
Imre Deak9647ff32015-01-25 13:27:11 -08001553 if (vf_flush_wa) {
1554 intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
1555 intel_logical_ring_emit(ringbuf, 0);
1556 intel_logical_ring_emit(ringbuf, 0);
1557 intel_logical_ring_emit(ringbuf, 0);
1558 intel_logical_ring_emit(ringbuf, 0);
1559 intel_logical_ring_emit(ringbuf, 0);
1560 }
1561
Oscar Mateo47122742014-07-24 17:04:28 +01001562 intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
1563 intel_logical_ring_emit(ringbuf, flags);
1564 intel_logical_ring_emit(ringbuf, scratch_addr);
1565 intel_logical_ring_emit(ringbuf, 0);
1566 intel_logical_ring_emit(ringbuf, 0);
1567 intel_logical_ring_emit(ringbuf, 0);
1568 intel_logical_ring_advance(ringbuf);
1569
1570 return 0;
1571}
1572
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001573static u32 gen8_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
1574{
1575 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1576}
1577
1578static void gen8_set_seqno(struct intel_engine_cs *ring, u32 seqno)
1579{
1580 intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
1581}
1582
John Harrisonc4e76632015-05-29 17:44:01 +01001583static int gen8_emit_request(struct drm_i915_gem_request *request)
Oscar Mateo4da46e12014-07-24 17:04:27 +01001584{
John Harrisonc4e76632015-05-29 17:44:01 +01001585 struct intel_ringbuffer *ringbuf = request->ringbuf;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001586 struct intel_engine_cs *ring = ringbuf->ring;
1587 u32 cmd;
1588 int ret;
1589
Michel Thierry53292cd2015-04-15 18:11:33 +01001590 /*
1591 * Reserve space for 2 NOOPs at the end of each request to be
1592 * used as a workaround for not being allowed to do lite
1593 * restore with HEAD==TAIL (WaIdleLiteRestore).
1594 */
John Harrison4d616a22015-05-29 17:44:08 +01001595 ret = intel_logical_ring_begin(request, 8);
Oscar Mateo4da46e12014-07-24 17:04:27 +01001596 if (ret)
1597 return ret;
1598
Ville Syrjälä8edfbb82014-11-14 18:16:56 +02001599 cmd = MI_STORE_DWORD_IMM_GEN4;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001600 cmd |= MI_GLOBAL_GTT;
1601
1602 intel_logical_ring_emit(ringbuf, cmd);
1603 intel_logical_ring_emit(ringbuf,
1604 (ring->status_page.gfx_addr +
1605 (I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT)));
1606 intel_logical_ring_emit(ringbuf, 0);
John Harrisonc4e76632015-05-29 17:44:01 +01001607 intel_logical_ring_emit(ringbuf, i915_gem_request_get_seqno(request));
Oscar Mateo4da46e12014-07-24 17:04:27 +01001608 intel_logical_ring_emit(ringbuf, MI_USER_INTERRUPT);
1609 intel_logical_ring_emit(ringbuf, MI_NOOP);
John Harrisonae707972015-05-29 17:44:14 +01001610 intel_logical_ring_advance_and_submit(request);
Oscar Mateo4da46e12014-07-24 17:04:27 +01001611
Michel Thierry53292cd2015-04-15 18:11:33 +01001612 /*
1613 * Here we add two extra NOOPs as padding to avoid
1614 * lite restore of a context with HEAD==TAIL.
1615 */
1616 intel_logical_ring_emit(ringbuf, MI_NOOP);
1617 intel_logical_ring_emit(ringbuf, MI_NOOP);
1618 intel_logical_ring_advance(ringbuf);
1619
Oscar Mateo4da46e12014-07-24 17:04:27 +01001620 return 0;
1621}
1622
John Harrisonbe013632015-05-29 17:43:45 +01001623static int intel_lr_context_render_state_init(struct drm_i915_gem_request *req)
Damien Lespiaucef437a2015-02-10 19:32:19 +00001624{
Damien Lespiaucef437a2015-02-10 19:32:19 +00001625 struct render_state so;
Damien Lespiaucef437a2015-02-10 19:32:19 +00001626 int ret;
1627
John Harrisonbe013632015-05-29 17:43:45 +01001628 ret = i915_gem_render_state_prepare(req->ring, &so);
Damien Lespiaucef437a2015-02-10 19:32:19 +00001629 if (ret)
1630 return ret;
1631
1632 if (so.rodata == NULL)
1633 return 0;
1634
John Harrisonbe795fc2015-05-29 17:44:03 +01001635 ret = req->ring->emit_bb_start(req, so.ggtt_offset,
John Harrisonbe013632015-05-29 17:43:45 +01001636 I915_DISPATCH_SECURE);
Damien Lespiaucef437a2015-02-10 19:32:19 +00001637 if (ret)
1638 goto out;
1639
John Harrisonb2af0372015-05-29 17:43:50 +01001640 i915_vma_move_to_active(i915_gem_obj_to_ggtt(so.obj), req);
Damien Lespiaucef437a2015-02-10 19:32:19 +00001641
Damien Lespiaucef437a2015-02-10 19:32:19 +00001642out:
1643 i915_gem_render_state_fini(&so);
1644 return ret;
1645}
1646
John Harrison87531812015-05-29 17:43:44 +01001647static int gen8_init_rcs_context(struct drm_i915_gem_request *req)
Thomas Daniele7778be2014-12-02 12:50:48 +00001648{
1649 int ret;
1650
John Harrisone2be4fa2015-05-29 17:43:54 +01001651 ret = intel_logical_ring_workarounds_emit(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001652 if (ret)
1653 return ret;
1654
John Harrisonbe013632015-05-29 17:43:45 +01001655 return intel_lr_context_render_state_init(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001656}
1657
Oscar Mateo73e4d072014-07-24 17:04:48 +01001658/**
1659 * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
1660 *
1661 * @ring: Engine Command Streamer.
1662 *
1663 */
Oscar Mateo454afeb2014-07-24 17:04:22 +01001664void intel_logical_ring_cleanup(struct intel_engine_cs *ring)
1665{
John Harrison6402c332014-10-31 12:00:26 +00001666 struct drm_i915_private *dev_priv;
Oscar Mateo9832b9d2014-07-24 17:04:30 +01001667
Oscar Mateo48d82382014-07-24 17:04:23 +01001668 if (!intel_ring_initialized(ring))
1669 return;
1670
John Harrison6402c332014-10-31 12:00:26 +00001671 dev_priv = ring->dev->dev_private;
1672
Oscar Mateo9832b9d2014-07-24 17:04:30 +01001673 intel_logical_ring_stop(ring);
1674 WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
Oscar Mateo48d82382014-07-24 17:04:23 +01001675
1676 if (ring->cleanup)
1677 ring->cleanup(ring);
1678
1679 i915_cmd_parser_fini_ring(ring);
Chris Wilson06fbca72015-04-07 16:20:36 +01001680 i915_gem_batch_pool_fini(&ring->batch_pool);
Oscar Mateo48d82382014-07-24 17:04:23 +01001681
1682 if (ring->status_page.obj) {
1683 kunmap(sg_page(ring->status_page.obj->pages->sgl));
1684 ring->status_page.obj = NULL;
1685 }
Arun Siluvery17ee9502015-06-19 19:07:01 +01001686
1687 lrc_destroy_wa_ctx_obj(ring);
Oscar Mateo454afeb2014-07-24 17:04:22 +01001688}
1689
1690static int logical_ring_init(struct drm_device *dev, struct intel_engine_cs *ring)
1691{
Oscar Mateo48d82382014-07-24 17:04:23 +01001692 int ret;
Oscar Mateo48d82382014-07-24 17:04:23 +01001693
1694 /* Intentionally left blank. */
1695 ring->buffer = NULL;
1696
1697 ring->dev = dev;
1698 INIT_LIST_HEAD(&ring->active_list);
1699 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson06fbca72015-04-07 16:20:36 +01001700 i915_gem_batch_pool_init(dev, &ring->batch_pool);
Oscar Mateo48d82382014-07-24 17:04:23 +01001701 init_waitqueue_head(&ring->irq_queue);
1702
Michel Thierryacdd8842014-07-24 17:04:38 +01001703 INIT_LIST_HEAD(&ring->execlist_queue);
Thomas Danielc86ee3a92014-11-13 10:27:05 +00001704 INIT_LIST_HEAD(&ring->execlist_retired_req_list);
Michel Thierryacdd8842014-07-24 17:04:38 +01001705 spin_lock_init(&ring->execlist_lock);
1706
Oscar Mateo48d82382014-07-24 17:04:23 +01001707 ret = i915_cmd_parser_init_ring(ring);
1708 if (ret)
1709 return ret;
1710
Oscar Mateo564ddb22014-08-21 11:40:54 +01001711 ret = intel_lr_context_deferred_create(ring->default_context, ring);
1712
1713 return ret;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001714}
1715
1716static int logical_render_ring_init(struct drm_device *dev)
1717{
1718 struct drm_i915_private *dev_priv = dev->dev_private;
1719 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Daniel Vetter99be1df2014-11-20 00:33:06 +01001720 int ret;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001721
1722 ring->name = "render ring";
1723 ring->id = RCS;
1724 ring->mmio_base = RENDER_RING_BASE;
1725 ring->irq_enable_mask =
1726 GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001727 ring->irq_keep_mask =
1728 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
1729 if (HAS_L3_DPF(dev))
1730 ring->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001731
Damien Lespiau82ef8222015-02-09 19:33:08 +00001732 if (INTEL_INFO(dev)->gen >= 9)
1733 ring->init_hw = gen9_init_render_ring;
1734 else
1735 ring->init_hw = gen8_init_render_ring;
Thomas Daniele7778be2014-12-02 12:50:48 +00001736 ring->init_context = gen8_init_rcs_context;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001737 ring->cleanup = intel_fini_pipe_control;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001738 ring->get_seqno = gen8_get_seqno;
1739 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001740 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001741 ring->emit_flush = gen8_emit_flush_render;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001742 ring->irq_get = gen8_logical_ring_get_irq;
1743 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001744 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001745
Daniel Vetter99be1df2014-11-20 00:33:06 +01001746 ring->dev = dev;
Arun Siluveryc4db7592015-06-19 18:37:11 +01001747
1748 ret = intel_init_pipe_control(ring);
Daniel Vetter99be1df2014-11-20 00:33:06 +01001749 if (ret)
1750 return ret;
1751
Arun Siluvery17ee9502015-06-19 19:07:01 +01001752 ret = intel_init_workaround_bb(ring);
1753 if (ret) {
1754 /*
1755 * We continue even if we fail to initialize WA batch
1756 * because we only expect rare glitches but nothing
1757 * critical to prevent us from using GPU
1758 */
1759 DRM_ERROR("WA batch buffer initialization failed: %d\n",
1760 ret);
1761 }
1762
Arun Siluveryc4db7592015-06-19 18:37:11 +01001763 ret = logical_ring_init(dev, ring);
1764 if (ret) {
Arun Siluvery17ee9502015-06-19 19:07:01 +01001765 lrc_destroy_wa_ctx_obj(ring);
Arun Siluveryc4db7592015-06-19 18:37:11 +01001766 }
Arun Siluvery17ee9502015-06-19 19:07:01 +01001767
1768 return ret;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001769}
1770
1771static int logical_bsd_ring_init(struct drm_device *dev)
1772{
1773 struct drm_i915_private *dev_priv = dev->dev_private;
1774 struct intel_engine_cs *ring = &dev_priv->ring[VCS];
1775
1776 ring->name = "bsd ring";
1777 ring->id = VCS;
1778 ring->mmio_base = GEN6_BSD_RING_BASE;
1779 ring->irq_enable_mask =
1780 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001781 ring->irq_keep_mask =
1782 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001783
Daniel Vetterecfe00d2014-11-20 00:33:04 +01001784 ring->init_hw = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001785 ring->get_seqno = gen8_get_seqno;
1786 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001787 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001788 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001789 ring->irq_get = gen8_logical_ring_get_irq;
1790 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001791 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001792
Oscar Mateo454afeb2014-07-24 17:04:22 +01001793 return logical_ring_init(dev, ring);
1794}
1795
1796static int logical_bsd2_ring_init(struct drm_device *dev)
1797{
1798 struct drm_i915_private *dev_priv = dev->dev_private;
1799 struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
1800
1801 ring->name = "bds2 ring";
1802 ring->id = VCS2;
1803 ring->mmio_base = GEN8_BSD2_RING_BASE;
1804 ring->irq_enable_mask =
1805 GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001806 ring->irq_keep_mask =
1807 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001808
Daniel Vetterecfe00d2014-11-20 00:33:04 +01001809 ring->init_hw = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001810 ring->get_seqno = gen8_get_seqno;
1811 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001812 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001813 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001814 ring->irq_get = gen8_logical_ring_get_irq;
1815 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001816 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001817
Oscar Mateo454afeb2014-07-24 17:04:22 +01001818 return logical_ring_init(dev, ring);
1819}
1820
1821static int logical_blt_ring_init(struct drm_device *dev)
1822{
1823 struct drm_i915_private *dev_priv = dev->dev_private;
1824 struct intel_engine_cs *ring = &dev_priv->ring[BCS];
1825
1826 ring->name = "blitter ring";
1827 ring->id = BCS;
1828 ring->mmio_base = BLT_RING_BASE;
1829 ring->irq_enable_mask =
1830 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001831 ring->irq_keep_mask =
1832 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001833
Daniel Vetterecfe00d2014-11-20 00:33:04 +01001834 ring->init_hw = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001835 ring->get_seqno = gen8_get_seqno;
1836 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001837 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001838 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001839 ring->irq_get = gen8_logical_ring_get_irq;
1840 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001841 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001842
Oscar Mateo454afeb2014-07-24 17:04:22 +01001843 return logical_ring_init(dev, ring);
1844}
1845
1846static int logical_vebox_ring_init(struct drm_device *dev)
1847{
1848 struct drm_i915_private *dev_priv = dev->dev_private;
1849 struct intel_engine_cs *ring = &dev_priv->ring[VECS];
1850
1851 ring->name = "video enhancement ring";
1852 ring->id = VECS;
1853 ring->mmio_base = VEBOX_RING_BASE;
1854 ring->irq_enable_mask =
1855 GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001856 ring->irq_keep_mask =
1857 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001858
Daniel Vetterecfe00d2014-11-20 00:33:04 +01001859 ring->init_hw = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001860 ring->get_seqno = gen8_get_seqno;
1861 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001862 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001863 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001864 ring->irq_get = gen8_logical_ring_get_irq;
1865 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001866 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001867
Oscar Mateo454afeb2014-07-24 17:04:22 +01001868 return logical_ring_init(dev, ring);
1869}
1870
Oscar Mateo73e4d072014-07-24 17:04:48 +01001871/**
1872 * intel_logical_rings_init() - allocate, populate and init the Engine Command Streamers
1873 * @dev: DRM device.
1874 *
1875 * This function inits the engines for an Execlists submission style (the equivalent in the
1876 * legacy ringbuffer submission world would be i915_gem_init_rings). It does it only for
1877 * those engines that are present in the hardware.
1878 *
1879 * Return: non-zero if the initialization failed.
1880 */
Oscar Mateo454afeb2014-07-24 17:04:22 +01001881int intel_logical_rings_init(struct drm_device *dev)
1882{
1883 struct drm_i915_private *dev_priv = dev->dev_private;
1884 int ret;
1885
1886 ret = logical_render_ring_init(dev);
1887 if (ret)
1888 return ret;
1889
1890 if (HAS_BSD(dev)) {
1891 ret = logical_bsd_ring_init(dev);
1892 if (ret)
1893 goto cleanup_render_ring;
1894 }
1895
1896 if (HAS_BLT(dev)) {
1897 ret = logical_blt_ring_init(dev);
1898 if (ret)
1899 goto cleanup_bsd_ring;
1900 }
1901
1902 if (HAS_VEBOX(dev)) {
1903 ret = logical_vebox_ring_init(dev);
1904 if (ret)
1905 goto cleanup_blt_ring;
1906 }
1907
1908 if (HAS_BSD2(dev)) {
1909 ret = logical_bsd2_ring_init(dev);
1910 if (ret)
1911 goto cleanup_vebox_ring;
1912 }
1913
1914 ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
1915 if (ret)
1916 goto cleanup_bsd2_ring;
1917
1918 return 0;
1919
1920cleanup_bsd2_ring:
1921 intel_logical_ring_cleanup(&dev_priv->ring[VCS2]);
1922cleanup_vebox_ring:
1923 intel_logical_ring_cleanup(&dev_priv->ring[VECS]);
1924cleanup_blt_ring:
1925 intel_logical_ring_cleanup(&dev_priv->ring[BCS]);
1926cleanup_bsd_ring:
1927 intel_logical_ring_cleanup(&dev_priv->ring[VCS]);
1928cleanup_render_ring:
1929 intel_logical_ring_cleanup(&dev_priv->ring[RCS]);
1930
1931 return ret;
1932}
1933
Jeff McGee0cea6502015-02-13 10:27:56 -06001934static u32
1935make_rpcs(struct drm_device *dev)
1936{
1937 u32 rpcs = 0;
1938
1939 /*
1940 * No explicit RPCS request is needed to ensure full
1941 * slice/subslice/EU enablement prior to Gen9.
1942 */
1943 if (INTEL_INFO(dev)->gen < 9)
1944 return 0;
1945
1946 /*
1947 * Starting in Gen9, render power gating can leave
1948 * slice/subslice/EU in a partially enabled state. We
1949 * must make an explicit request through RPCS for full
1950 * enablement.
1951 */
1952 if (INTEL_INFO(dev)->has_slice_pg) {
1953 rpcs |= GEN8_RPCS_S_CNT_ENABLE;
1954 rpcs |= INTEL_INFO(dev)->slice_total <<
1955 GEN8_RPCS_S_CNT_SHIFT;
1956 rpcs |= GEN8_RPCS_ENABLE;
1957 }
1958
1959 if (INTEL_INFO(dev)->has_subslice_pg) {
1960 rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
1961 rpcs |= INTEL_INFO(dev)->subslice_per_slice <<
1962 GEN8_RPCS_SS_CNT_SHIFT;
1963 rpcs |= GEN8_RPCS_ENABLE;
1964 }
1965
1966 if (INTEL_INFO(dev)->has_eu_pg) {
1967 rpcs |= INTEL_INFO(dev)->eu_per_subslice <<
1968 GEN8_RPCS_EU_MIN_SHIFT;
1969 rpcs |= INTEL_INFO(dev)->eu_per_subslice <<
1970 GEN8_RPCS_EU_MAX_SHIFT;
1971 rpcs |= GEN8_RPCS_ENABLE;
1972 }
1973
1974 return rpcs;
1975}
1976
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001977static int
1978populate_lr_context(struct intel_context *ctx, struct drm_i915_gem_object *ctx_obj,
1979 struct intel_engine_cs *ring, struct intel_ringbuffer *ringbuf)
1980{
Thomas Daniel2d965532014-08-19 10:13:36 +01001981 struct drm_device *dev = ring->dev;
1982 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterae6c4802014-08-06 15:04:53 +02001983 struct i915_hw_ppgtt *ppgtt = ctx->ppgtt;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001984 struct page *page;
1985 uint32_t *reg_state;
1986 int ret;
1987
Thomas Daniel2d965532014-08-19 10:13:36 +01001988 if (!ppgtt)
1989 ppgtt = dev_priv->mm.aliasing_ppgtt;
1990
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001991 ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
1992 if (ret) {
1993 DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
1994 return ret;
1995 }
1996
1997 ret = i915_gem_object_get_pages(ctx_obj);
1998 if (ret) {
1999 DRM_DEBUG_DRIVER("Could not get object pages\n");
2000 return ret;
2001 }
2002
2003 i915_gem_object_pin_pages(ctx_obj);
2004
2005 /* The second page of the context object contains some fields which must
2006 * be set up prior to the first execution. */
2007 page = i915_gem_object_get_page(ctx_obj, 1);
2008 reg_state = kmap_atomic(page);
2009
2010 /* A context is actually a big batch buffer with several MI_LOAD_REGISTER_IMM
2011 * commands followed by (reg, value) pairs. The values we are setting here are
2012 * only for the first context restore: on a subsequent save, the GPU will
2013 * recreate this batchbuffer with new values (including all the missing
2014 * MI_LOAD_REGISTER_IMM commands that we are not initializing here). */
2015 if (ring->id == RCS)
2016 reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(14);
2017 else
2018 reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(11);
2019 reg_state[CTX_LRI_HEADER_0] |= MI_LRI_FORCE_POSTED;
2020 reg_state[CTX_CONTEXT_CONTROL] = RING_CONTEXT_CONTROL(ring);
2021 reg_state[CTX_CONTEXT_CONTROL+1] =
Zhi Wang5baa22c52015-02-10 17:11:36 +08002022 _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
2023 CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002024 reg_state[CTX_RING_HEAD] = RING_HEAD(ring->mmio_base);
2025 reg_state[CTX_RING_HEAD+1] = 0;
2026 reg_state[CTX_RING_TAIL] = RING_TAIL(ring->mmio_base);
2027 reg_state[CTX_RING_TAIL+1] = 0;
2028 reg_state[CTX_RING_BUFFER_START] = RING_START(ring->mmio_base);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002029 /* Ring buffer start address is not known until the buffer is pinned.
2030 * It is written to the context image in execlists_update_context()
2031 */
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002032 reg_state[CTX_RING_BUFFER_CONTROL] = RING_CTL(ring->mmio_base);
2033 reg_state[CTX_RING_BUFFER_CONTROL+1] =
2034 ((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES) | RING_VALID;
2035 reg_state[CTX_BB_HEAD_U] = ring->mmio_base + 0x168;
2036 reg_state[CTX_BB_HEAD_U+1] = 0;
2037 reg_state[CTX_BB_HEAD_L] = ring->mmio_base + 0x140;
2038 reg_state[CTX_BB_HEAD_L+1] = 0;
2039 reg_state[CTX_BB_STATE] = ring->mmio_base + 0x110;
2040 reg_state[CTX_BB_STATE+1] = (1<<5);
2041 reg_state[CTX_SECOND_BB_HEAD_U] = ring->mmio_base + 0x11c;
2042 reg_state[CTX_SECOND_BB_HEAD_U+1] = 0;
2043 reg_state[CTX_SECOND_BB_HEAD_L] = ring->mmio_base + 0x114;
2044 reg_state[CTX_SECOND_BB_HEAD_L+1] = 0;
2045 reg_state[CTX_SECOND_BB_STATE] = ring->mmio_base + 0x118;
2046 reg_state[CTX_SECOND_BB_STATE+1] = 0;
2047 if (ring->id == RCS) {
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002048 reg_state[CTX_BB_PER_CTX_PTR] = ring->mmio_base + 0x1c0;
2049 reg_state[CTX_BB_PER_CTX_PTR+1] = 0;
2050 reg_state[CTX_RCS_INDIRECT_CTX] = ring->mmio_base + 0x1c4;
2051 reg_state[CTX_RCS_INDIRECT_CTX+1] = 0;
2052 reg_state[CTX_RCS_INDIRECT_CTX_OFFSET] = ring->mmio_base + 0x1c8;
2053 reg_state[CTX_RCS_INDIRECT_CTX_OFFSET+1] = 0;
Arun Siluvery17ee9502015-06-19 19:07:01 +01002054 if (ring->wa_ctx.obj) {
2055 struct i915_ctx_workarounds *wa_ctx = &ring->wa_ctx;
2056 uint32_t ggtt_offset = i915_gem_obj_ggtt_offset(wa_ctx->obj);
2057
2058 reg_state[CTX_RCS_INDIRECT_CTX+1] =
2059 (ggtt_offset + wa_ctx->indirect_ctx.offset * sizeof(uint32_t)) |
2060 (wa_ctx->indirect_ctx.size / CACHELINE_DWORDS);
2061
2062 reg_state[CTX_RCS_INDIRECT_CTX_OFFSET+1] =
2063 CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT << 6;
2064
2065 reg_state[CTX_BB_PER_CTX_PTR+1] =
2066 (ggtt_offset + wa_ctx->per_ctx.offset * sizeof(uint32_t)) |
2067 0x01;
2068 }
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002069 }
2070 reg_state[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9);
2071 reg_state[CTX_LRI_HEADER_1] |= MI_LRI_FORCE_POSTED;
2072 reg_state[CTX_CTX_TIMESTAMP] = ring->mmio_base + 0x3a8;
2073 reg_state[CTX_CTX_TIMESTAMP+1] = 0;
2074 reg_state[CTX_PDP3_UDW] = GEN8_RING_PDP_UDW(ring, 3);
2075 reg_state[CTX_PDP3_LDW] = GEN8_RING_PDP_LDW(ring, 3);
2076 reg_state[CTX_PDP2_UDW] = GEN8_RING_PDP_UDW(ring, 2);
2077 reg_state[CTX_PDP2_LDW] = GEN8_RING_PDP_LDW(ring, 2);
2078 reg_state[CTX_PDP1_UDW] = GEN8_RING_PDP_UDW(ring, 1);
2079 reg_state[CTX_PDP1_LDW] = GEN8_RING_PDP_LDW(ring, 1);
2080 reg_state[CTX_PDP0_UDW] = GEN8_RING_PDP_UDW(ring, 0);
2081 reg_state[CTX_PDP0_LDW] = GEN8_RING_PDP_LDW(ring, 0);
Michel Thierryd7b26332015-04-08 12:13:34 +01002082
2083 /* With dynamic page allocation, PDPs may not be allocated at this point,
2084 * Point the unallocated PDPs to the scratch page
Michel Thierrye5815a22015-04-08 12:13:32 +01002085 */
2086 ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
2087 ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
2088 ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
2089 ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002090 if (ring->id == RCS) {
2091 reg_state[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
Jeff McGee0cea6502015-02-13 10:27:56 -06002092 reg_state[CTX_R_PWR_CLK_STATE] = GEN8_R_PWR_CLK_STATE;
2093 reg_state[CTX_R_PWR_CLK_STATE+1] = make_rpcs(dev);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002094 }
2095
2096 kunmap_atomic(reg_state);
2097
2098 ctx_obj->dirty = 1;
2099 set_page_dirty(page);
2100 i915_gem_object_unpin_pages(ctx_obj);
2101
2102 return 0;
2103}
2104
Oscar Mateo73e4d072014-07-24 17:04:48 +01002105/**
2106 * intel_lr_context_free() - free the LRC specific bits of a context
2107 * @ctx: the LR context to free.
2108 *
2109 * The real context freeing is done in i915_gem_context_free: this only
2110 * takes care of the bits that are LRC related: the per-engine backing
2111 * objects and the logical ringbuffer.
2112 */
Oscar Mateoede7d422014-07-24 17:04:12 +01002113void intel_lr_context_free(struct intel_context *ctx)
2114{
Oscar Mateo8c8579172014-07-24 17:04:14 +01002115 int i;
2116
2117 for (i = 0; i < I915_NUM_RINGS; i++) {
2118 struct drm_i915_gem_object *ctx_obj = ctx->engine[i].state;
Oscar Mateo84c23772014-07-24 17:04:15 +01002119
Oscar Mateo8c8579172014-07-24 17:04:14 +01002120 if (ctx_obj) {
Oscar Mateodcb4c122014-11-13 10:28:10 +00002121 struct intel_ringbuffer *ringbuf =
2122 ctx->engine[i].ringbuf;
2123 struct intel_engine_cs *ring = ringbuf->ring;
2124
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002125 if (ctx == ring->default_context) {
2126 intel_unpin_ringbuffer_obj(ringbuf);
2127 i915_gem_object_ggtt_unpin(ctx_obj);
2128 }
Mika Kuoppalaa7cbede2015-01-13 11:32:25 +02002129 WARN_ON(ctx->engine[ring->id].pin_count);
Oscar Mateo84c23772014-07-24 17:04:15 +01002130 intel_destroy_ringbuffer_obj(ringbuf);
2131 kfree(ringbuf);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002132 drm_gem_object_unreference(&ctx_obj->base);
2133 }
2134 }
2135}
2136
2137static uint32_t get_lr_context_size(struct intel_engine_cs *ring)
2138{
2139 int ret = 0;
2140
Michael H. Nguyen468c6812014-11-13 17:51:49 +00002141 WARN_ON(INTEL_INFO(ring->dev)->gen < 8);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002142
2143 switch (ring->id) {
2144 case RCS:
Michael H. Nguyen468c6812014-11-13 17:51:49 +00002145 if (INTEL_INFO(ring->dev)->gen >= 9)
2146 ret = GEN9_LR_CONTEXT_RENDER_SIZE;
2147 else
2148 ret = GEN8_LR_CONTEXT_RENDER_SIZE;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002149 break;
2150 case VCS:
2151 case BCS:
2152 case VECS:
2153 case VCS2:
2154 ret = GEN8_LR_CONTEXT_OTHER_SIZE;
2155 break;
2156 }
2157
2158 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01002159}
2160
Daniel Vetter70b0ea82014-11-18 09:09:32 +01002161static void lrc_setup_hardware_status_page(struct intel_engine_cs *ring,
Thomas Daniel1df06b72014-10-29 09:52:51 +00002162 struct drm_i915_gem_object *default_ctx_obj)
2163{
2164 struct drm_i915_private *dev_priv = ring->dev->dev_private;
2165
2166 /* The status page is offset 0 from the default context object
2167 * in LRC mode. */
2168 ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(default_ctx_obj);
2169 ring->status_page.page_addr =
2170 kmap(sg_page(default_ctx_obj->pages->sgl));
Thomas Daniel1df06b72014-10-29 09:52:51 +00002171 ring->status_page.obj = default_ctx_obj;
2172
2173 I915_WRITE(RING_HWS_PGA(ring->mmio_base),
2174 (u32)ring->status_page.gfx_addr);
2175 POSTING_READ(RING_HWS_PGA(ring->mmio_base));
Thomas Daniel1df06b72014-10-29 09:52:51 +00002176}
2177
Oscar Mateo73e4d072014-07-24 17:04:48 +01002178/**
2179 * intel_lr_context_deferred_create() - create the LRC specific bits of a context
2180 * @ctx: LR context to create.
2181 * @ring: engine to be used with the context.
2182 *
2183 * This function can be called more than once, with different engines, if we plan
2184 * to use the context with them. The context backing objects and the ringbuffers
2185 * (specially the ringbuffer backing objects) suck a lot of memory up, and that's why
2186 * the creation is a deferred call: it's better to make sure first that we need to use
2187 * a given ring with the context.
2188 *
Masanari Iida32197aa2014-10-20 23:53:13 +09002189 * Return: non-zero on error.
Oscar Mateo73e4d072014-07-24 17:04:48 +01002190 */
Oscar Mateoede7d422014-07-24 17:04:12 +01002191int intel_lr_context_deferred_create(struct intel_context *ctx,
2192 struct intel_engine_cs *ring)
2193{
Oscar Mateodcb4c122014-11-13 10:28:10 +00002194 const bool is_global_default_ctx = (ctx == ring->default_context);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002195 struct drm_device *dev = ring->dev;
2196 struct drm_i915_gem_object *ctx_obj;
2197 uint32_t context_size;
Oscar Mateo84c23772014-07-24 17:04:15 +01002198 struct intel_ringbuffer *ringbuf;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002199 int ret;
2200
Oscar Mateoede7d422014-07-24 17:04:12 +01002201 WARN_ON(ctx->legacy_hw_ctx.rcs_state != NULL);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002202 WARN_ON(ctx->engine[ring->id].state);
Oscar Mateoede7d422014-07-24 17:04:12 +01002203
Oscar Mateo8c8579172014-07-24 17:04:14 +01002204 context_size = round_up(get_lr_context_size(ring), 4096);
2205
Chris Wilson149c86e2015-04-07 16:21:11 +01002206 ctx_obj = i915_gem_alloc_object(dev, context_size);
Dan Carpenter3126a662015-04-30 17:30:50 +03002207 if (!ctx_obj) {
2208 DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
2209 return -ENOMEM;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002210 }
2211
Oscar Mateodcb4c122014-11-13 10:28:10 +00002212 if (is_global_default_ctx) {
2213 ret = i915_gem_obj_ggtt_pin(ctx_obj, GEN8_LR_CONTEXT_ALIGN, 0);
2214 if (ret) {
2215 DRM_DEBUG_DRIVER("Pin LRC backing obj failed: %d\n",
2216 ret);
2217 drm_gem_object_unreference(&ctx_obj->base);
2218 return ret;
2219 }
Oscar Mateo8c8579172014-07-24 17:04:14 +01002220 }
2221
Oscar Mateo84c23772014-07-24 17:04:15 +01002222 ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
2223 if (!ringbuf) {
2224 DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s\n",
2225 ring->name);
Oscar Mateo84c23772014-07-24 17:04:15 +01002226 ret = -ENOMEM;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002227 goto error_unpin_ctx;
Oscar Mateo84c23772014-07-24 17:04:15 +01002228 }
2229
Daniel Vetter0c7dd532014-08-11 16:17:44 +02002230 ringbuf->ring = ring;
Oscar Mateo582d67f2014-07-24 17:04:16 +01002231
Oscar Mateo84c23772014-07-24 17:04:15 +01002232 ringbuf->size = 32 * PAGE_SIZE;
2233 ringbuf->effective_size = ringbuf->size;
2234 ringbuf->head = 0;
2235 ringbuf->tail = 0;
Oscar Mateo84c23772014-07-24 17:04:15 +01002236 ringbuf->last_retired_head = -1;
Dave Gordonebd0fd42014-11-27 11:22:49 +00002237 intel_ring_update_space(ringbuf);
Oscar Mateo84c23772014-07-24 17:04:15 +01002238
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002239 if (ringbuf->obj == NULL) {
2240 ret = intel_alloc_ringbuffer_obj(dev, ringbuf);
2241 if (ret) {
2242 DRM_DEBUG_DRIVER(
2243 "Failed to allocate ringbuffer obj %s: %d\n",
Oscar Mateo84c23772014-07-24 17:04:15 +01002244 ring->name, ret);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002245 goto error_free_rbuf;
2246 }
2247
2248 if (is_global_default_ctx) {
2249 ret = intel_pin_and_map_ringbuffer_obj(dev, ringbuf);
2250 if (ret) {
2251 DRM_ERROR(
2252 "Failed to pin and map ringbuffer %s: %d\n",
2253 ring->name, ret);
2254 goto error_destroy_rbuf;
2255 }
2256 }
2257
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002258 }
2259
2260 ret = populate_lr_context(ctx, ctx_obj, ring, ringbuf);
2261 if (ret) {
2262 DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002263 goto error;
Oscar Mateo84c23772014-07-24 17:04:15 +01002264 }
2265
2266 ctx->engine[ring->id].ringbuf = ringbuf;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002267 ctx->engine[ring->id].state = ctx_obj;
Oscar Mateoede7d422014-07-24 17:04:12 +01002268
Daniel Vetter70b0ea82014-11-18 09:09:32 +01002269 if (ctx == ring->default_context)
2270 lrc_setup_hardware_status_page(ring, ctx_obj);
Thomas Daniele7778be2014-12-02 12:50:48 +00002271 else if (ring->id == RCS && !ctx->rcs_initialized) {
Michel Thierry771b9a52014-11-11 16:47:33 +00002272 if (ring->init_context) {
John Harrison76c39162015-05-29 17:43:43 +01002273 struct drm_i915_gem_request *req;
2274
2275 ret = i915_gem_request_alloc(ring, ctx, &req);
2276 if (ret)
2277 return ret;
2278
John Harrison87531812015-05-29 17:43:44 +01002279 ret = ring->init_context(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00002280 if (ret) {
Michel Thierry771b9a52014-11-11 16:47:33 +00002281 DRM_ERROR("ring init context: %d\n", ret);
John Harrison76c39162015-05-29 17:43:43 +01002282 i915_gem_request_cancel(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00002283 ctx->engine[ring->id].ringbuf = NULL;
2284 ctx->engine[ring->id].state = NULL;
2285 goto error;
2286 }
John Harrison76c39162015-05-29 17:43:43 +01002287
John Harrison75289872015-05-29 17:43:49 +01002288 i915_add_request_no_flush(req);
Michel Thierry771b9a52014-11-11 16:47:33 +00002289 }
2290
Oscar Mateo564ddb22014-08-21 11:40:54 +01002291 ctx->rcs_initialized = true;
2292 }
2293
Oscar Mateoede7d422014-07-24 17:04:12 +01002294 return 0;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002295
2296error:
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002297 if (is_global_default_ctx)
2298 intel_unpin_ringbuffer_obj(ringbuf);
2299error_destroy_rbuf:
2300 intel_destroy_ringbuffer_obj(ringbuf);
2301error_free_rbuf:
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002302 kfree(ringbuf);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002303error_unpin_ctx:
Oscar Mateodcb4c122014-11-13 10:28:10 +00002304 if (is_global_default_ctx)
2305 i915_gem_object_ggtt_unpin(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002306 drm_gem_object_unreference(&ctx_obj->base);
2307 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01002308}
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002309
2310void intel_lr_context_reset(struct drm_device *dev,
2311 struct intel_context *ctx)
2312{
2313 struct drm_i915_private *dev_priv = dev->dev_private;
2314 struct intel_engine_cs *ring;
2315 int i;
2316
2317 for_each_ring(ring, dev_priv, i) {
2318 struct drm_i915_gem_object *ctx_obj =
2319 ctx->engine[ring->id].state;
2320 struct intel_ringbuffer *ringbuf =
2321 ctx->engine[ring->id].ringbuf;
2322 uint32_t *reg_state;
2323 struct page *page;
2324
2325 if (!ctx_obj)
2326 continue;
2327
2328 if (i915_gem_object_get_pages(ctx_obj)) {
2329 WARN(1, "Failed get_pages for context obj\n");
2330 continue;
2331 }
2332 page = i915_gem_object_get_page(ctx_obj, 1);
2333 reg_state = kmap_atomic(page);
2334
2335 reg_state[CTX_RING_HEAD+1] = 0;
2336 reg_state[CTX_RING_TAIL+1] = 0;
2337
2338 kunmap_atomic(reg_state);
2339
2340 ringbuf->head = 0;
2341 ringbuf->tail = 0;
2342 }
2343}