blob: 58ac414cf603071e42dfdd5d856e5c6313eab0b2 [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) { \
Michel Thierryd7b26332015-04-08 12:13:34 +0100193 const u64 _addr = test_bit(n, ppgtt->pdp.used_pdpes) ? \
Michel Thierrye5815a22015-04-08 12:13:32 +0100194 ppgtt->pdp.page_directory[n]->daddr : \
195 ppgtt->scratch_pd->daddr; \
196 reg_state[CTX_PDP ## n ## _UDW+1] = upper_32_bits(_addr); \
197 reg_state[CTX_PDP ## n ## _LDW+1] = lower_32_bits(_addr); \
198}
199
Ben Widawsky84b790f2014-07-24 17:04:36 +0100200enum {
201 ADVANCED_CONTEXT = 0,
202 LEGACY_CONTEXT,
203 ADVANCED_AD_CONTEXT,
204 LEGACY_64B_CONTEXT
205};
206#define GEN8_CTX_MODE_SHIFT 3
207enum {
208 FAULT_AND_HANG = 0,
209 FAULT_AND_HALT, /* Debug only */
210 FAULT_AND_STREAM,
211 FAULT_AND_CONTINUE /* Unsupported */
212};
213#define GEN8_CTX_ID_SHIFT 32
214
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000215static int intel_lr_context_pin(struct intel_engine_cs *ring,
216 struct intel_context *ctx);
217
Oscar Mateo73e4d072014-07-24 17:04:48 +0100218/**
219 * intel_sanitize_enable_execlists() - sanitize i915.enable_execlists
220 * @dev: DRM device.
221 * @enable_execlists: value of i915.enable_execlists module parameter.
222 *
223 * Only certain platforms support Execlists (the prerequisites being
Thomas Daniel27401d12014-12-11 12:48:35 +0000224 * support for Logical Ring Contexts and Aliasing PPGTT or better).
Oscar Mateo73e4d072014-07-24 17:04:48 +0100225 *
226 * Return: 1 if Execlists is supported and has to be enabled.
227 */
Oscar Mateo127f1002014-07-24 17:04:11 +0100228int intel_sanitize_enable_execlists(struct drm_device *dev, int enable_execlists)
229{
Daniel Vetterbd84b1e2014-08-11 15:57:57 +0200230 WARN_ON(i915.enable_ppgtt == -1);
231
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000232 if (INTEL_INFO(dev)->gen >= 9)
233 return 1;
234
Oscar Mateo127f1002014-07-24 17:04:11 +0100235 if (enable_execlists == 0)
236 return 0;
237
Oscar Mateo14bf9932014-07-24 17:04:34 +0100238 if (HAS_LOGICAL_RING_CONTEXTS(dev) && USES_PPGTT(dev) &&
239 i915.use_mmio_flip >= 0)
Oscar Mateo127f1002014-07-24 17:04:11 +0100240 return 1;
241
242 return 0;
243}
Oscar Mateoede7d422014-07-24 17:04:12 +0100244
Oscar Mateo73e4d072014-07-24 17:04:48 +0100245/**
246 * intel_execlists_ctx_id() - get the Execlists Context ID
247 * @ctx_obj: Logical Ring Context backing object.
248 *
249 * Do not confuse with ctx->id! Unfortunately we have a name overload
250 * here: the old context ID we pass to userspace as a handler so that
251 * they can refer to a context, and the new context ID we pass to the
252 * ELSP so that the GPU can inform us of the context status via
253 * interrupts.
254 *
255 * Return: 20-bits globally unique context ID.
256 */
Ben Widawsky84b790f2014-07-24 17:04:36 +0100257u32 intel_execlists_ctx_id(struct drm_i915_gem_object *ctx_obj)
258{
259 u32 lrca = i915_gem_obj_ggtt_offset(ctx_obj);
260
261 /* LRCA is required to be 4K aligned so the more significant 20 bits
262 * are globally unique */
263 return lrca >> 12;
264}
265
Nick Hoath203a5712015-02-06 11:30:04 +0000266static uint64_t execlists_ctx_descriptor(struct intel_engine_cs *ring,
267 struct drm_i915_gem_object *ctx_obj)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100268{
Nick Hoath203a5712015-02-06 11:30:04 +0000269 struct drm_device *dev = ring->dev;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100270 uint64_t desc;
271 uint64_t lrca = i915_gem_obj_ggtt_offset(ctx_obj);
Michel Thierryacdd8842014-07-24 17:04:38 +0100272
273 WARN_ON(lrca & 0xFFFFFFFF00000FFFULL);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100274
275 desc = GEN8_CTX_VALID;
276 desc |= LEGACY_CONTEXT << GEN8_CTX_MODE_SHIFT;
Arun Siluvery51847fb2015-04-07 14:01:33 +0100277 if (IS_GEN8(ctx_obj->base.dev))
278 desc |= GEN8_CTX_L3LLC_COHERENT;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100279 desc |= GEN8_CTX_PRIVILEGE;
280 desc |= lrca;
281 desc |= (u64)intel_execlists_ctx_id(ctx_obj) << GEN8_CTX_ID_SHIFT;
282
283 /* TODO: WaDisableLiteRestore when we start using semaphore
284 * signalling between Command Streamers */
285 /* desc |= GEN8_CTX_FORCE_RESTORE; */
286
Nick Hoath203a5712015-02-06 11:30:04 +0000287 /* WaEnableForceRestoreInCtxtDescForVCS:skl */
288 if (IS_GEN9(dev) &&
289 INTEL_REVID(dev) <= SKL_REVID_B0 &&
290 (ring->id == BCS || ring->id == VCS ||
291 ring->id == VECS || ring->id == VCS2))
292 desc |= GEN8_CTX_FORCE_RESTORE;
293
Ben Widawsky84b790f2014-07-24 17:04:36 +0100294 return desc;
295}
296
297static void execlists_elsp_write(struct intel_engine_cs *ring,
298 struct drm_i915_gem_object *ctx_obj0,
299 struct drm_i915_gem_object *ctx_obj1)
300{
Tvrtko Ursulin6e7cc472014-11-13 17:51:51 +0000301 struct drm_device *dev = ring->dev;
302 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100303 uint64_t temp = 0;
304 uint32_t desc[4];
305
306 /* XXX: You must always write both descriptors in the order below. */
307 if (ctx_obj1)
Nick Hoath203a5712015-02-06 11:30:04 +0000308 temp = execlists_ctx_descriptor(ring, ctx_obj1);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100309 else
310 temp = 0;
311 desc[1] = (u32)(temp >> 32);
312 desc[0] = (u32)temp;
313
Nick Hoath203a5712015-02-06 11:30:04 +0000314 temp = execlists_ctx_descriptor(ring, ctx_obj0);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100315 desc[3] = (u32)(temp >> 32);
316 desc[2] = (u32)temp;
317
Mika Kuoppala59bad942015-01-16 11:34:40 +0200318 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100319 I915_WRITE(RING_ELSP(ring), desc[1]);
320 I915_WRITE(RING_ELSP(ring), desc[0]);
321 I915_WRITE(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 */
324 I915_WRITE(RING_ELSP(ring), desc[2]);
325
326 /* ELSP is a wo register, so use another nearby reg for posting instead */
327 POSTING_READ(RING_EXECLIST_STATUS(ring));
Mika Kuoppala59bad942015-01-16 11:34:40 +0200328 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100329}
330
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000331static int execlists_update_context(struct drm_i915_gem_object *ctx_obj,
332 struct drm_i915_gem_object *ring_obj,
Michel Thierryd7b26332015-04-08 12:13:34 +0100333 struct i915_hw_ppgtt *ppgtt,
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000334 u32 tail)
Oscar Mateoae1250b2014-07-24 17:04:37 +0100335{
336 struct page *page;
337 uint32_t *reg_state;
338
339 page = i915_gem_object_get_page(ctx_obj, 1);
340 reg_state = kmap_atomic(page);
341
342 reg_state[CTX_RING_TAIL+1] = tail;
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000343 reg_state[CTX_RING_BUFFER_START+1] = i915_gem_obj_ggtt_offset(ring_obj);
Oscar Mateoae1250b2014-07-24 17:04:37 +0100344
Michel Thierryd7b26332015-04-08 12:13:34 +0100345 /* True PPGTT with dynamic page allocation: update PDP registers and
346 * point the unallocated PDPs to the scratch page
347 */
348 if (ppgtt) {
349 ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
350 ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
351 ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
352 ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
353 }
354
Oscar Mateoae1250b2014-07-24 17:04:37 +0100355 kunmap_atomic(reg_state);
356
357 return 0;
358}
359
Dave Gordoncd0707c2014-10-30 15:41:56 +0000360static void execlists_submit_contexts(struct intel_engine_cs *ring,
361 struct intel_context *to0, u32 tail0,
362 struct intel_context *to1, u32 tail1)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100363{
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000364 struct drm_i915_gem_object *ctx_obj0 = to0->engine[ring->id].state;
365 struct intel_ringbuffer *ringbuf0 = to0->engine[ring->id].ringbuf;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100366 struct drm_i915_gem_object *ctx_obj1 = NULL;
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000367 struct intel_ringbuffer *ringbuf1 = NULL;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100368
Ben Widawsky84b790f2014-07-24 17:04:36 +0100369 BUG_ON(!ctx_obj0);
Michel Thierryacdd8842014-07-24 17:04:38 +0100370 WARN_ON(!i915_gem_obj_is_pinned(ctx_obj0));
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000371 WARN_ON(!i915_gem_obj_is_pinned(ringbuf0->obj));
Ben Widawsky84b790f2014-07-24 17:04:36 +0100372
Michel Thierryd7b26332015-04-08 12:13:34 +0100373 execlists_update_context(ctx_obj0, ringbuf0->obj, to0->ppgtt, tail0);
Oscar Mateoae1250b2014-07-24 17:04:37 +0100374
Ben Widawsky84b790f2014-07-24 17:04:36 +0100375 if (to1) {
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000376 ringbuf1 = to1->engine[ring->id].ringbuf;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100377 ctx_obj1 = to1->engine[ring->id].state;
378 BUG_ON(!ctx_obj1);
Michel Thierryacdd8842014-07-24 17:04:38 +0100379 WARN_ON(!i915_gem_obj_is_pinned(ctx_obj1));
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000380 WARN_ON(!i915_gem_obj_is_pinned(ringbuf1->obj));
Oscar Mateoae1250b2014-07-24 17:04:37 +0100381
Michel Thierryd7b26332015-04-08 12:13:34 +0100382 execlists_update_context(ctx_obj1, ringbuf1->obj, to1->ppgtt, tail1);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100383 }
384
385 execlists_elsp_write(ring, ctx_obj0, ctx_obj1);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100386}
387
Michel Thierryacdd8842014-07-24 17:04:38 +0100388static void execlists_context_unqueue(struct intel_engine_cs *ring)
389{
Nick Hoath6d3d8272015-01-15 13:10:39 +0000390 struct drm_i915_gem_request *req0 = NULL, *req1 = NULL;
391 struct drm_i915_gem_request *cursor = NULL, *tmp = NULL;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100392
393 assert_spin_locked(&ring->execlist_lock);
Michel Thierryacdd8842014-07-24 17:04:38 +0100394
395 if (list_empty(&ring->execlist_queue))
396 return;
397
398 /* Try to read in pairs */
399 list_for_each_entry_safe(cursor, tmp, &ring->execlist_queue,
400 execlist_link) {
401 if (!req0) {
402 req0 = cursor;
Nick Hoath6d3d8272015-01-15 13:10:39 +0000403 } else if (req0->ctx == cursor->ctx) {
Michel Thierryacdd8842014-07-24 17:04:38 +0100404 /* Same ctx: ignore first request, as second request
405 * will update tail past first request's workload */
Oscar Mateoe1fee722014-07-24 17:04:40 +0100406 cursor->elsp_submitted = req0->elsp_submitted;
Michel Thierryacdd8842014-07-24 17:04:38 +0100407 list_del(&req0->execlist_link);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000408 list_add_tail(&req0->execlist_link,
409 &ring->execlist_retired_req_list);
Michel Thierryacdd8842014-07-24 17:04:38 +0100410 req0 = cursor;
411 } else {
412 req1 = cursor;
413 break;
414 }
415 }
416
Oscar Mateoe1fee722014-07-24 17:04:40 +0100417 WARN_ON(req1 && req1->elsp_submitted);
418
Nick Hoath6d3d8272015-01-15 13:10:39 +0000419 execlists_submit_contexts(ring, req0->ctx, req0->tail,
420 req1 ? req1->ctx : NULL,
421 req1 ? req1->tail : 0);
Oscar Mateoe1fee722014-07-24 17:04:40 +0100422
423 req0->elsp_submitted++;
424 if (req1)
425 req1->elsp_submitted++;
Michel Thierryacdd8842014-07-24 17:04:38 +0100426}
427
Thomas Daniele981e7b2014-07-24 17:04:39 +0100428static bool execlists_check_remove_request(struct intel_engine_cs *ring,
429 u32 request_id)
430{
Nick Hoath6d3d8272015-01-15 13:10:39 +0000431 struct drm_i915_gem_request *head_req;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100432
433 assert_spin_locked(&ring->execlist_lock);
434
435 head_req = list_first_entry_or_null(&ring->execlist_queue,
Nick Hoath6d3d8272015-01-15 13:10:39 +0000436 struct drm_i915_gem_request,
Thomas Daniele981e7b2014-07-24 17:04:39 +0100437 execlist_link);
438
439 if (head_req != NULL) {
440 struct drm_i915_gem_object *ctx_obj =
Nick Hoath6d3d8272015-01-15 13:10:39 +0000441 head_req->ctx->engine[ring->id].state;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100442 if (intel_execlists_ctx_id(ctx_obj) == request_id) {
Oscar Mateoe1fee722014-07-24 17:04:40 +0100443 WARN(head_req->elsp_submitted == 0,
444 "Never submitted head request\n");
445
446 if (--head_req->elsp_submitted <= 0) {
447 list_del(&head_req->execlist_link);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000448 list_add_tail(&head_req->execlist_link,
449 &ring->execlist_retired_req_list);
Oscar Mateoe1fee722014-07-24 17:04:40 +0100450 return true;
451 }
Thomas Daniele981e7b2014-07-24 17:04:39 +0100452 }
453 }
454
455 return false;
456}
457
Oscar Mateo73e4d072014-07-24 17:04:48 +0100458/**
Daniel Vetter3f7531c2014-12-10 17:41:43 +0100459 * intel_lrc_irq_handler() - handle Context Switch interrupts
Oscar Mateo73e4d072014-07-24 17:04:48 +0100460 * @ring: Engine Command Streamer to handle.
461 *
462 * Check the unread Context Status Buffers and manage the submission of new
463 * contexts to the ELSP accordingly.
464 */
Daniel Vetter3f7531c2014-12-10 17:41:43 +0100465void intel_lrc_irq_handler(struct intel_engine_cs *ring)
Thomas Daniele981e7b2014-07-24 17:04:39 +0100466{
467 struct drm_i915_private *dev_priv = ring->dev->dev_private;
468 u32 status_pointer;
469 u8 read_pointer;
470 u8 write_pointer;
471 u32 status;
472 u32 status_id;
473 u32 submit_contexts = 0;
474
475 status_pointer = I915_READ(RING_CONTEXT_STATUS_PTR(ring));
476
477 read_pointer = ring->next_context_status_buffer;
478 write_pointer = status_pointer & 0x07;
479 if (read_pointer > write_pointer)
480 write_pointer += 6;
481
482 spin_lock(&ring->execlist_lock);
483
484 while (read_pointer < write_pointer) {
485 read_pointer++;
486 status = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
487 (read_pointer % 6) * 8);
488 status_id = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
489 (read_pointer % 6) * 8 + 4);
490
Oscar Mateoe1fee722014-07-24 17:04:40 +0100491 if (status & GEN8_CTX_STATUS_PREEMPTED) {
492 if (status & GEN8_CTX_STATUS_LITE_RESTORE) {
493 if (execlists_check_remove_request(ring, status_id))
494 WARN(1, "Lite Restored request removed from queue\n");
495 } else
496 WARN(1, "Preemption without Lite Restore\n");
497 }
498
499 if ((status & GEN8_CTX_STATUS_ACTIVE_IDLE) ||
500 (status & GEN8_CTX_STATUS_ELEMENT_SWITCH)) {
Thomas Daniele981e7b2014-07-24 17:04:39 +0100501 if (execlists_check_remove_request(ring, status_id))
502 submit_contexts++;
503 }
504 }
505
506 if (submit_contexts != 0)
507 execlists_context_unqueue(ring);
508
509 spin_unlock(&ring->execlist_lock);
510
511 WARN(submit_contexts > 2, "More than two context complete events?\n");
512 ring->next_context_status_buffer = write_pointer % 6;
513
514 I915_WRITE(RING_CONTEXT_STATUS_PTR(ring),
515 ((u32)ring->next_context_status_buffer & 0x07) << 8);
516}
517
Michel Thierryacdd8842014-07-24 17:04:38 +0100518static int execlists_context_queue(struct intel_engine_cs *ring,
519 struct intel_context *to,
Nick Hoath2d129552015-01-15 13:10:36 +0000520 u32 tail,
521 struct drm_i915_gem_request *request)
Michel Thierryacdd8842014-07-24 17:04:38 +0100522{
Nick Hoath6d3d8272015-01-15 13:10:39 +0000523 struct drm_i915_gem_request *cursor;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100524 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Michel Thierryacdd8842014-07-24 17:04:38 +0100525 unsigned long flags;
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100526 int num_elements = 0;
Michel Thierryacdd8842014-07-24 17:04:38 +0100527
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000528 if (to != ring->default_context)
529 intel_lr_context_pin(ring, to);
530
Nick Hoath2d129552015-01-15 13:10:36 +0000531 if (!request) {
532 /*
533 * If there isn't a request associated with this submission,
534 * create one as a temporary holder.
535 */
Nick Hoath2d129552015-01-15 13:10:36 +0000536 request = kzalloc(sizeof(*request), GFP_KERNEL);
537 if (request == NULL)
538 return -ENOMEM;
Nick Hoath2d129552015-01-15 13:10:36 +0000539 request->ring = ring;
Nick Hoath6d3d8272015-01-15 13:10:39 +0000540 request->ctx = to;
Nick Hoathb3a38992015-02-19 16:30:47 +0000541 kref_init(&request->ref);
542 request->uniq = dev_priv->request_uniq++;
543 i915_gem_context_reference(request->ctx);
Nick Hoath21076372015-01-15 13:10:38 +0000544 } else {
Nick Hoathb3a38992015-02-19 16:30:47 +0000545 i915_gem_request_reference(request);
Nick Hoath21076372015-01-15 13:10:38 +0000546 WARN_ON(to != request->ctx);
Nick Hoath2d129552015-01-15 13:10:36 +0000547 }
Nick Hoath72f95af2015-01-15 13:10:37 +0000548 request->tail = tail;
Nick Hoath2d129552015-01-15 13:10:36 +0000549
Thomas Daniele981e7b2014-07-24 17:04:39 +0100550 intel_runtime_pm_get(dev_priv);
Michel Thierryacdd8842014-07-24 17:04:38 +0100551
552 spin_lock_irqsave(&ring->execlist_lock, flags);
553
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100554 list_for_each_entry(cursor, &ring->execlist_queue, execlist_link)
555 if (++num_elements > 2)
556 break;
557
558 if (num_elements > 2) {
Nick Hoath6d3d8272015-01-15 13:10:39 +0000559 struct drm_i915_gem_request *tail_req;
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100560
561 tail_req = list_last_entry(&ring->execlist_queue,
Nick Hoath6d3d8272015-01-15 13:10:39 +0000562 struct drm_i915_gem_request,
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100563 execlist_link);
564
Nick Hoath6d3d8272015-01-15 13:10:39 +0000565 if (to == tail_req->ctx) {
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100566 WARN(tail_req->elsp_submitted != 0,
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000567 "More than 2 already-submitted reqs queued\n");
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100568 list_del(&tail_req->execlist_link);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000569 list_add_tail(&tail_req->execlist_link,
570 &ring->execlist_retired_req_list);
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100571 }
572 }
573
Nick Hoath6d3d8272015-01-15 13:10:39 +0000574 list_add_tail(&request->execlist_link, &ring->execlist_queue);
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100575 if (num_elements == 0)
Michel Thierryacdd8842014-07-24 17:04:38 +0100576 execlists_context_unqueue(ring);
577
578 spin_unlock_irqrestore(&ring->execlist_lock, flags);
579
580 return 0;
581}
582
Nick Hoath21076372015-01-15 13:10:38 +0000583static int logical_ring_invalidate_all_caches(struct intel_ringbuffer *ringbuf,
584 struct intel_context *ctx)
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100585{
586 struct intel_engine_cs *ring = ringbuf->ring;
587 uint32_t flush_domains;
588 int ret;
589
590 flush_domains = 0;
591 if (ring->gpu_caches_dirty)
592 flush_domains = I915_GEM_GPU_DOMAINS;
593
Nick Hoath21076372015-01-15 13:10:38 +0000594 ret = ring->emit_flush(ringbuf, ctx,
595 I915_GEM_GPU_DOMAINS, flush_domains);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100596 if (ret)
597 return ret;
598
599 ring->gpu_caches_dirty = false;
600 return 0;
601}
602
603static int execlists_move_to_gpu(struct intel_ringbuffer *ringbuf,
Nick Hoath21076372015-01-15 13:10:38 +0000604 struct intel_context *ctx,
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100605 struct list_head *vmas)
606{
607 struct intel_engine_cs *ring = ringbuf->ring;
608 struct i915_vma *vma;
609 uint32_t flush_domains = 0;
610 bool flush_chipset = false;
611 int ret;
612
613 list_for_each_entry(vma, vmas, exec_list) {
614 struct drm_i915_gem_object *obj = vma->obj;
615
616 ret = i915_gem_object_sync(obj, ring);
617 if (ret)
618 return ret;
619
620 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
621 flush_chipset |= i915_gem_clflush_object(obj, false);
622
623 flush_domains |= obj->base.write_domain;
624 }
625
626 if (flush_domains & I915_GEM_DOMAIN_GTT)
627 wmb();
628
629 /* Unconditionally invalidate gpu caches and ensure that we do flush
630 * any residual writes from the previous batch.
631 */
Nick Hoath21076372015-01-15 13:10:38 +0000632 return logical_ring_invalidate_all_caches(ringbuf, ctx);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100633}
634
John Harrison6689cb22015-03-19 12:30:08 +0000635int intel_logical_ring_alloc_request_extras(struct drm_i915_gem_request *request,
636 struct intel_context *ctx)
John Harrisonbc0dce32015-03-19 12:30:07 +0000637{
John Harrisonbc0dce32015-03-19 12:30:07 +0000638 int ret;
639
John Harrison6689cb22015-03-19 12:30:08 +0000640 if (ctx != request->ring->default_context) {
641 ret = intel_lr_context_pin(request->ring, ctx);
642 if (ret)
John Harrisonbc0dce32015-03-19 12:30:07 +0000643 return ret;
John Harrisonbc0dce32015-03-19 12:30:07 +0000644 }
645
John Harrison6689cb22015-03-19 12:30:08 +0000646 request->ringbuf = ctx->engine[request->ring->id].ringbuf;
647 request->ctx = ctx;
John Harrisonbc0dce32015-03-19 12:30:07 +0000648 i915_gem_context_reference(request->ctx);
John Harrisonbc0dce32015-03-19 12:30:07 +0000649
John Harrisonbc0dce32015-03-19 12:30:07 +0000650 return 0;
651}
652
653static int logical_ring_wait_request(struct intel_ringbuffer *ringbuf,
654 int bytes)
655{
656 struct intel_engine_cs *ring = ringbuf->ring;
657 struct drm_i915_gem_request *request;
John Harrisondbe46462015-03-19 12:30:09 +0000658 int ret, new_space;
John Harrisonbc0dce32015-03-19 12:30:07 +0000659
660 if (intel_ring_space(ringbuf) >= bytes)
661 return 0;
662
663 list_for_each_entry(request, &ring->request_list, list) {
664 /*
665 * The request queue is per-engine, so can contain requests
666 * from multiple ringbuffers. Here, we must ignore any that
667 * aren't from the ringbuffer we're considering.
668 */
669 struct intel_context *ctx = request->ctx;
670 if (ctx->engine[ring->id].ringbuf != ringbuf)
671 continue;
672
673 /* Would completion of this request free enough space? */
John Harrisondbe46462015-03-19 12:30:09 +0000674 new_space = __intel_ring_space(request->postfix, ringbuf->tail,
675 ringbuf->size);
676 if (new_space >= bytes)
John Harrisonbc0dce32015-03-19 12:30:07 +0000677 break;
John Harrisonbc0dce32015-03-19 12:30:07 +0000678 }
679
680 if (&request->list == &ring->request_list)
681 return -ENOSPC;
682
683 ret = i915_wait_request(request);
684 if (ret)
685 return ret;
686
687 i915_gem_retire_requests_ring(ring);
688
John Harrisondbe46462015-03-19 12:30:09 +0000689 WARN_ON(intel_ring_space(ringbuf) < new_space);
690
John Harrisonbc0dce32015-03-19 12:30:07 +0000691 return intel_ring_space(ringbuf) >= bytes ? 0 : -ENOSPC;
692}
693
694/*
695 * intel_logical_ring_advance_and_submit() - advance the tail and submit the workload
696 * @ringbuf: Logical Ringbuffer to advance.
697 *
698 * The tail is updated in our logical ringbuffer struct, not in the actual context. What
699 * really happens during submission is that the context and current tail will be placed
700 * on a queue waiting for the ELSP to be ready to accept a new context submission. At that
701 * point, the tail *inside* the context is updated and the ELSP written to.
702 */
703static void
704intel_logical_ring_advance_and_submit(struct intel_ringbuffer *ringbuf,
705 struct intel_context *ctx,
706 struct drm_i915_gem_request *request)
707{
708 struct intel_engine_cs *ring = ringbuf->ring;
709
710 intel_logical_ring_advance(ringbuf);
711
712 if (intel_ring_stopped(ring))
713 return;
714
715 execlists_context_queue(ring, ctx, ringbuf->tail, request);
716}
717
718static int logical_ring_wait_for_space(struct intel_ringbuffer *ringbuf,
719 struct intel_context *ctx,
720 int bytes)
721{
722 struct intel_engine_cs *ring = ringbuf->ring;
723 struct drm_device *dev = ring->dev;
724 struct drm_i915_private *dev_priv = dev->dev_private;
725 unsigned long end;
726 int ret;
727
728 ret = logical_ring_wait_request(ringbuf, bytes);
729 if (ret != -ENOSPC)
730 return ret;
731
732 /* Force the context submission in case we have been skipping it */
733 intel_logical_ring_advance_and_submit(ringbuf, ctx, NULL);
734
735 /* With GEM the hangcheck timer should kick us out of the loop,
736 * leaving it early runs the risk of corrupting GEM state (due
737 * to running on almost untested codepaths). But on resume
738 * timers don't work yet, so prevent a complete hang in that
739 * case by choosing an insanely large timeout. */
740 end = jiffies + 60 * HZ;
741
742 ret = 0;
743 do {
744 if (intel_ring_space(ringbuf) >= bytes)
745 break;
746
747 msleep(1);
748
749 if (dev_priv->mm.interruptible && signal_pending(current)) {
750 ret = -ERESTARTSYS;
751 break;
752 }
753
754 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
755 dev_priv->mm.interruptible);
756 if (ret)
757 break;
758
759 if (time_after(jiffies, end)) {
760 ret = -EBUSY;
761 break;
762 }
763 } while (1);
764
765 return ret;
766}
767
768static int logical_ring_wrap_buffer(struct intel_ringbuffer *ringbuf,
769 struct intel_context *ctx)
770{
771 uint32_t __iomem *virt;
772 int rem = ringbuf->size - ringbuf->tail;
773
774 if (ringbuf->space < rem) {
775 int ret = logical_ring_wait_for_space(ringbuf, ctx, rem);
776
777 if (ret)
778 return ret;
779 }
780
781 virt = ringbuf->virtual_start + ringbuf->tail;
782 rem /= 4;
783 while (rem--)
784 iowrite32(MI_NOOP, virt++);
785
786 ringbuf->tail = 0;
787 intel_ring_update_space(ringbuf);
788
789 return 0;
790}
791
792static int logical_ring_prepare(struct intel_ringbuffer *ringbuf,
793 struct intel_context *ctx, int bytes)
794{
795 int ret;
796
797 if (unlikely(ringbuf->tail + bytes > ringbuf->effective_size)) {
798 ret = logical_ring_wrap_buffer(ringbuf, ctx);
799 if (unlikely(ret))
800 return ret;
801 }
802
803 if (unlikely(ringbuf->space < bytes)) {
804 ret = logical_ring_wait_for_space(ringbuf, ctx, bytes);
805 if (unlikely(ret))
806 return ret;
807 }
808
809 return 0;
810}
811
812/**
813 * intel_logical_ring_begin() - prepare the logical ringbuffer to accept some commands
814 *
815 * @ringbuf: Logical ringbuffer.
816 * @num_dwords: number of DWORDs that we plan to write to the ringbuffer.
817 *
818 * The ringbuffer might not be ready to accept the commands right away (maybe it needs to
819 * be wrapped, or wait a bit for the tail to be updated). This function takes care of that
820 * and also preallocates a request (every workload submission is still mediated through
821 * requests, same as it did with legacy ringbuffer submission).
822 *
823 * Return: non-zero if the ringbuffer is not ready to be written to.
824 */
825static int intel_logical_ring_begin(struct intel_ringbuffer *ringbuf,
826 struct intel_context *ctx, int num_dwords)
827{
828 struct intel_engine_cs *ring = ringbuf->ring;
829 struct drm_device *dev = ring->dev;
830 struct drm_i915_private *dev_priv = dev->dev_private;
831 int ret;
832
833 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
834 dev_priv->mm.interruptible);
835 if (ret)
836 return ret;
837
838 ret = logical_ring_prepare(ringbuf, ctx, num_dwords * sizeof(uint32_t));
839 if (ret)
840 return ret;
841
842 /* Preallocate the olr before touching the ring */
John Harrison6689cb22015-03-19 12:30:08 +0000843 ret = i915_gem_request_alloc(ring, ctx);
John Harrisonbc0dce32015-03-19 12:30:07 +0000844 if (ret)
845 return ret;
846
847 ringbuf->space -= num_dwords * sizeof(uint32_t);
848 return 0;
849}
850
Oscar Mateo73e4d072014-07-24 17:04:48 +0100851/**
852 * execlists_submission() - submit a batchbuffer for execution, Execlists style
853 * @dev: DRM device.
854 * @file: DRM file.
855 * @ring: Engine Command Streamer to submit to.
856 * @ctx: Context to employ for this submission.
857 * @args: execbuffer call arguments.
858 * @vmas: list of vmas.
859 * @batch_obj: the batchbuffer to submit.
860 * @exec_start: batchbuffer start virtual address pointer.
John Harrison8e004ef2015-02-13 11:48:10 +0000861 * @dispatch_flags: translated execbuffer call flags.
Oscar Mateo73e4d072014-07-24 17:04:48 +0100862 *
863 * This is the evil twin version of i915_gem_ringbuffer_submission. It abstracts
864 * away the submission details of the execbuffer ioctl call.
865 *
866 * Return: non-zero if the submission fails.
867 */
Oscar Mateo454afeb2014-07-24 17:04:22 +0100868int intel_execlists_submission(struct drm_device *dev, struct drm_file *file,
869 struct intel_engine_cs *ring,
870 struct intel_context *ctx,
871 struct drm_i915_gem_execbuffer2 *args,
872 struct list_head *vmas,
873 struct drm_i915_gem_object *batch_obj,
John Harrison8e004ef2015-02-13 11:48:10 +0000874 u64 exec_start, u32 dispatch_flags)
Oscar Mateo454afeb2014-07-24 17:04:22 +0100875{
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100876 struct drm_i915_private *dev_priv = dev->dev_private;
877 struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
878 int instp_mode;
879 u32 instp_mask;
880 int ret;
881
882 instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
883 instp_mask = I915_EXEC_CONSTANTS_MASK;
884 switch (instp_mode) {
885 case I915_EXEC_CONSTANTS_REL_GENERAL:
886 case I915_EXEC_CONSTANTS_ABSOLUTE:
887 case I915_EXEC_CONSTANTS_REL_SURFACE:
888 if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) {
889 DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
890 return -EINVAL;
891 }
892
893 if (instp_mode != dev_priv->relative_constants_mode) {
894 if (instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
895 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
896 return -EINVAL;
897 }
898
899 /* The HW changed the meaning on this bit on gen6 */
900 instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
901 }
902 break;
903 default:
904 DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
905 return -EINVAL;
906 }
907
908 if (args->num_cliprects != 0) {
909 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
910 return -EINVAL;
911 } else {
912 if (args->DR4 == 0xffffffff) {
913 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
914 args->DR4 = 0;
915 }
916
917 if (args->DR1 || args->DR4 || args->cliprects_ptr) {
918 DRM_DEBUG("0 cliprects but dirt in cliprects fields\n");
919 return -EINVAL;
920 }
921 }
922
923 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
924 DRM_DEBUG("sol reset is gen7 only\n");
925 return -EINVAL;
926 }
927
Nick Hoath21076372015-01-15 13:10:38 +0000928 ret = execlists_move_to_gpu(ringbuf, ctx, vmas);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100929 if (ret)
930 return ret;
931
932 if (ring == &dev_priv->ring[RCS] &&
933 instp_mode != dev_priv->relative_constants_mode) {
Nick Hoath21076372015-01-15 13:10:38 +0000934 ret = intel_logical_ring_begin(ringbuf, ctx, 4);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100935 if (ret)
936 return ret;
937
938 intel_logical_ring_emit(ringbuf, MI_NOOP);
939 intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(1));
940 intel_logical_ring_emit(ringbuf, INSTPM);
941 intel_logical_ring_emit(ringbuf, instp_mask << 16 | instp_mode);
942 intel_logical_ring_advance(ringbuf);
943
944 dev_priv->relative_constants_mode = instp_mode;
945 }
946
John Harrison8e004ef2015-02-13 11:48:10 +0000947 ret = ring->emit_bb_start(ringbuf, ctx, exec_start, dispatch_flags);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100948 if (ret)
949 return ret;
950
John Harrison5e4be7b2015-02-13 11:48:11 +0000951 trace_i915_gem_ring_dispatch(intel_ring_get_request(ring), dispatch_flags);
952
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100953 i915_gem_execbuffer_move_to_active(vmas, ring);
954 i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
955
Oscar Mateo454afeb2014-07-24 17:04:22 +0100956 return 0;
957}
958
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000959void intel_execlists_retire_requests(struct intel_engine_cs *ring)
960{
Nick Hoath6d3d8272015-01-15 13:10:39 +0000961 struct drm_i915_gem_request *req, *tmp;
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000962 struct drm_i915_private *dev_priv = ring->dev->dev_private;
963 unsigned long flags;
964 struct list_head retired_list;
965
966 WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
967 if (list_empty(&ring->execlist_retired_req_list))
968 return;
969
970 INIT_LIST_HEAD(&retired_list);
971 spin_lock_irqsave(&ring->execlist_lock, flags);
972 list_replace_init(&ring->execlist_retired_req_list, &retired_list);
973 spin_unlock_irqrestore(&ring->execlist_lock, flags);
974
975 list_for_each_entry_safe(req, tmp, &retired_list, execlist_link) {
Nick Hoath6d3d8272015-01-15 13:10:39 +0000976 struct intel_context *ctx = req->ctx;
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000977 struct drm_i915_gem_object *ctx_obj =
978 ctx->engine[ring->id].state;
979
980 if (ctx_obj && (ctx != ring->default_context))
981 intel_lr_context_unpin(ring, ctx);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000982 intel_runtime_pm_put(dev_priv);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000983 list_del(&req->execlist_link);
Nick Hoathf8210792015-01-29 16:55:07 +0000984 i915_gem_request_unreference(req);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000985 }
986}
987
Oscar Mateo454afeb2014-07-24 17:04:22 +0100988void intel_logical_ring_stop(struct intel_engine_cs *ring)
989{
Oscar Mateo9832b9d2014-07-24 17:04:30 +0100990 struct drm_i915_private *dev_priv = ring->dev->dev_private;
991 int ret;
992
993 if (!intel_ring_initialized(ring))
994 return;
995
996 ret = intel_ring_idle(ring);
997 if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
998 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
999 ring->name, ret);
1000
1001 /* TODO: Is this correct with Execlists enabled? */
1002 I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
1003 if (wait_for_atomic((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
1004 DRM_ERROR("%s :timed out trying to stop ring\n", ring->name);
1005 return;
1006 }
1007 I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
Oscar Mateo454afeb2014-07-24 17:04:22 +01001008}
1009
Nick Hoath21076372015-01-15 13:10:38 +00001010int logical_ring_flush_all_caches(struct intel_ringbuffer *ringbuf,
1011 struct intel_context *ctx)
Oscar Mateo48e29f52014-07-24 17:04:29 +01001012{
1013 struct intel_engine_cs *ring = ringbuf->ring;
1014 int ret;
1015
1016 if (!ring->gpu_caches_dirty)
1017 return 0;
1018
Nick Hoath21076372015-01-15 13:10:38 +00001019 ret = ring->emit_flush(ringbuf, ctx, 0, I915_GEM_GPU_DOMAINS);
Oscar Mateo48e29f52014-07-24 17:04:29 +01001020 if (ret)
1021 return ret;
1022
1023 ring->gpu_caches_dirty = false;
1024 return 0;
1025}
1026
Oscar Mateodcb4c122014-11-13 10:28:10 +00001027static int intel_lr_context_pin(struct intel_engine_cs *ring,
1028 struct intel_context *ctx)
1029{
1030 struct drm_i915_gem_object *ctx_obj = ctx->engine[ring->id].state;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001031 struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
Oscar Mateodcb4c122014-11-13 10:28:10 +00001032 int ret = 0;
1033
1034 WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
Mika Kuoppalaa7cbede2015-01-13 11:32:25 +02001035 if (ctx->engine[ring->id].pin_count++ == 0) {
Oscar Mateodcb4c122014-11-13 10:28:10 +00001036 ret = i915_gem_obj_ggtt_pin(ctx_obj,
1037 GEN8_LR_CONTEXT_ALIGN, 0);
1038 if (ret)
Mika Kuoppalaa7cbede2015-01-13 11:32:25 +02001039 goto reset_pin_count;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001040
1041 ret = intel_pin_and_map_ringbuffer_obj(ring->dev, ringbuf);
1042 if (ret)
1043 goto unpin_ctx_obj;
Oscar Mateodcb4c122014-11-13 10:28:10 +00001044 }
1045
1046 return ret;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001047
1048unpin_ctx_obj:
1049 i915_gem_object_ggtt_unpin(ctx_obj);
Mika Kuoppalaa7cbede2015-01-13 11:32:25 +02001050reset_pin_count:
1051 ctx->engine[ring->id].pin_count = 0;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001052
1053 return ret;
Oscar Mateodcb4c122014-11-13 10:28:10 +00001054}
1055
1056void intel_lr_context_unpin(struct intel_engine_cs *ring,
1057 struct intel_context *ctx)
1058{
1059 struct drm_i915_gem_object *ctx_obj = ctx->engine[ring->id].state;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001060 struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
Oscar Mateodcb4c122014-11-13 10:28:10 +00001061
1062 if (ctx_obj) {
1063 WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
Mika Kuoppalaa7cbede2015-01-13 11:32:25 +02001064 if (--ctx->engine[ring->id].pin_count == 0) {
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001065 intel_unpin_ringbuffer_obj(ringbuf);
Oscar Mateodcb4c122014-11-13 10:28:10 +00001066 i915_gem_object_ggtt_unpin(ctx_obj);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001067 }
Oscar Mateodcb4c122014-11-13 10:28:10 +00001068 }
1069}
1070
Michel Thierry771b9a52014-11-11 16:47:33 +00001071static int intel_logical_ring_workarounds_emit(struct intel_engine_cs *ring,
1072 struct intel_context *ctx)
1073{
1074 int ret, i;
1075 struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
1076 struct drm_device *dev = ring->dev;
1077 struct drm_i915_private *dev_priv = dev->dev_private;
1078 struct i915_workarounds *w = &dev_priv->workarounds;
1079
Michel Thierrye6c1abb2014-11-26 14:21:02 +00001080 if (WARN_ON_ONCE(w->count == 0))
Michel Thierry771b9a52014-11-11 16:47:33 +00001081 return 0;
1082
1083 ring->gpu_caches_dirty = true;
Nick Hoath21076372015-01-15 13:10:38 +00001084 ret = logical_ring_flush_all_caches(ringbuf, ctx);
Michel Thierry771b9a52014-11-11 16:47:33 +00001085 if (ret)
1086 return ret;
1087
Nick Hoath21076372015-01-15 13:10:38 +00001088 ret = intel_logical_ring_begin(ringbuf, ctx, w->count * 2 + 2);
Michel Thierry771b9a52014-11-11 16:47:33 +00001089 if (ret)
1090 return ret;
1091
1092 intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(w->count));
1093 for (i = 0; i < w->count; i++) {
1094 intel_logical_ring_emit(ringbuf, w->reg[i].addr);
1095 intel_logical_ring_emit(ringbuf, w->reg[i].value);
1096 }
1097 intel_logical_ring_emit(ringbuf, MI_NOOP);
1098
1099 intel_logical_ring_advance(ringbuf);
1100
1101 ring->gpu_caches_dirty = true;
Nick Hoath21076372015-01-15 13:10:38 +00001102 ret = logical_ring_flush_all_caches(ringbuf, ctx);
Michel Thierry771b9a52014-11-11 16:47:33 +00001103 if (ret)
1104 return ret;
1105
1106 return 0;
1107}
1108
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001109static int gen8_init_common_ring(struct intel_engine_cs *ring)
1110{
1111 struct drm_device *dev = ring->dev;
1112 struct drm_i915_private *dev_priv = dev->dev_private;
1113
Oscar Mateo73d477f2014-07-24 17:04:31 +01001114 I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
1115 I915_WRITE(RING_HWSTAM(ring->mmio_base), 0xffffffff);
1116
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001117 I915_WRITE(RING_MODE_GEN7(ring),
1118 _MASKED_BIT_DISABLE(GFX_REPLAY_MODE) |
1119 _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
1120 POSTING_READ(RING_MODE_GEN7(ring));
Thomas Danielc0a03a22015-01-09 11:09:37 +00001121 ring->next_context_status_buffer = 0;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001122 DRM_DEBUG_DRIVER("Execlists enabled for %s\n", ring->name);
1123
1124 memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
1125
1126 return 0;
1127}
1128
1129static int gen8_init_render_ring(struct intel_engine_cs *ring)
1130{
1131 struct drm_device *dev = ring->dev;
1132 struct drm_i915_private *dev_priv = dev->dev_private;
1133 int ret;
1134
1135 ret = gen8_init_common_ring(ring);
1136 if (ret)
1137 return ret;
1138
1139 /* We need to disable the AsyncFlip performance optimisations in order
1140 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1141 * programmed to '1' on all products.
1142 *
1143 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
1144 */
1145 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1146
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001147 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1148
Michel Thierry771b9a52014-11-11 16:47:33 +00001149 return init_workarounds_ring(ring);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001150}
1151
Damien Lespiau82ef8222015-02-09 19:33:08 +00001152static int gen9_init_render_ring(struct intel_engine_cs *ring)
1153{
1154 int ret;
1155
1156 ret = gen8_init_common_ring(ring);
1157 if (ret)
1158 return ret;
1159
1160 return init_workarounds_ring(ring);
1161}
1162
Oscar Mateo15648582014-07-24 17:04:32 +01001163static int gen8_emit_bb_start(struct intel_ringbuffer *ringbuf,
Nick Hoath21076372015-01-15 13:10:38 +00001164 struct intel_context *ctx,
John Harrison8e004ef2015-02-13 11:48:10 +00001165 u64 offset, unsigned dispatch_flags)
Oscar Mateo15648582014-07-24 17:04:32 +01001166{
John Harrison8e004ef2015-02-13 11:48:10 +00001167 bool ppgtt = !(dispatch_flags & I915_DISPATCH_SECURE);
Oscar Mateo15648582014-07-24 17:04:32 +01001168 int ret;
1169
Nick Hoath21076372015-01-15 13:10:38 +00001170 ret = intel_logical_ring_begin(ringbuf, ctx, 4);
Oscar Mateo15648582014-07-24 17:04:32 +01001171 if (ret)
1172 return ret;
1173
1174 /* FIXME(BDW): Address space and security selectors. */
1175 intel_logical_ring_emit(ringbuf, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8));
1176 intel_logical_ring_emit(ringbuf, lower_32_bits(offset));
1177 intel_logical_ring_emit(ringbuf, upper_32_bits(offset));
1178 intel_logical_ring_emit(ringbuf, MI_NOOP);
1179 intel_logical_ring_advance(ringbuf);
1180
1181 return 0;
1182}
1183
Oscar Mateo73d477f2014-07-24 17:04:31 +01001184static bool gen8_logical_ring_get_irq(struct intel_engine_cs *ring)
1185{
1186 struct drm_device *dev = ring->dev;
1187 struct drm_i915_private *dev_priv = dev->dev_private;
1188 unsigned long flags;
1189
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001190 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Oscar Mateo73d477f2014-07-24 17:04:31 +01001191 return false;
1192
1193 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1194 if (ring->irq_refcount++ == 0) {
1195 I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
1196 POSTING_READ(RING_IMR(ring->mmio_base));
1197 }
1198 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1199
1200 return true;
1201}
1202
1203static void gen8_logical_ring_put_irq(struct intel_engine_cs *ring)
1204{
1205 struct drm_device *dev = ring->dev;
1206 struct drm_i915_private *dev_priv = dev->dev_private;
1207 unsigned long flags;
1208
1209 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1210 if (--ring->irq_refcount == 0) {
1211 I915_WRITE_IMR(ring, ~ring->irq_keep_mask);
1212 POSTING_READ(RING_IMR(ring->mmio_base));
1213 }
1214 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1215}
1216
Oscar Mateo47122742014-07-24 17:04:28 +01001217static int gen8_emit_flush(struct intel_ringbuffer *ringbuf,
Nick Hoath21076372015-01-15 13:10:38 +00001218 struct intel_context *ctx,
Oscar Mateo47122742014-07-24 17:04:28 +01001219 u32 invalidate_domains,
1220 u32 unused)
1221{
1222 struct intel_engine_cs *ring = ringbuf->ring;
1223 struct drm_device *dev = ring->dev;
1224 struct drm_i915_private *dev_priv = dev->dev_private;
1225 uint32_t cmd;
1226 int ret;
1227
Nick Hoath21076372015-01-15 13:10:38 +00001228 ret = intel_logical_ring_begin(ringbuf, ctx, 4);
Oscar Mateo47122742014-07-24 17:04:28 +01001229 if (ret)
1230 return ret;
1231
1232 cmd = MI_FLUSH_DW + 1;
1233
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001234 /* We always require a command barrier so that subsequent
1235 * commands, such as breadcrumb interrupts, are strictly ordered
1236 * wrt the contents of the write cache being flushed to memory
1237 * (and thus being coherent from the CPU).
1238 */
1239 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1240
1241 if (invalidate_domains & I915_GEM_GPU_DOMAINS) {
1242 cmd |= MI_INVALIDATE_TLB;
1243 if (ring == &dev_priv->ring[VCS])
1244 cmd |= MI_INVALIDATE_BSD;
Oscar Mateo47122742014-07-24 17:04:28 +01001245 }
1246
1247 intel_logical_ring_emit(ringbuf, cmd);
1248 intel_logical_ring_emit(ringbuf,
1249 I915_GEM_HWS_SCRATCH_ADDR |
1250 MI_FLUSH_DW_USE_GTT);
1251 intel_logical_ring_emit(ringbuf, 0); /* upper addr */
1252 intel_logical_ring_emit(ringbuf, 0); /* value */
1253 intel_logical_ring_advance(ringbuf);
1254
1255 return 0;
1256}
1257
1258static int gen8_emit_flush_render(struct intel_ringbuffer *ringbuf,
Nick Hoath21076372015-01-15 13:10:38 +00001259 struct intel_context *ctx,
Oscar Mateo47122742014-07-24 17:04:28 +01001260 u32 invalidate_domains,
1261 u32 flush_domains)
1262{
1263 struct intel_engine_cs *ring = ringbuf->ring;
1264 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
1265 u32 flags = 0;
1266 int ret;
1267
1268 flags |= PIPE_CONTROL_CS_STALL;
1269
1270 if (flush_domains) {
1271 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
1272 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
1273 }
1274
1275 if (invalidate_domains) {
1276 flags |= PIPE_CONTROL_TLB_INVALIDATE;
1277 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
1278 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
1279 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
1280 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
1281 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
1282 flags |= PIPE_CONTROL_QW_WRITE;
1283 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
1284 }
1285
Nick Hoath21076372015-01-15 13:10:38 +00001286 ret = intel_logical_ring_begin(ringbuf, ctx, 6);
Oscar Mateo47122742014-07-24 17:04:28 +01001287 if (ret)
1288 return ret;
1289
1290 intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
1291 intel_logical_ring_emit(ringbuf, flags);
1292 intel_logical_ring_emit(ringbuf, scratch_addr);
1293 intel_logical_ring_emit(ringbuf, 0);
1294 intel_logical_ring_emit(ringbuf, 0);
1295 intel_logical_ring_emit(ringbuf, 0);
1296 intel_logical_ring_advance(ringbuf);
1297
1298 return 0;
1299}
1300
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001301static u32 gen8_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
1302{
1303 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1304}
1305
1306static void gen8_set_seqno(struct intel_engine_cs *ring, u32 seqno)
1307{
1308 intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
1309}
1310
Nick Hoath2d129552015-01-15 13:10:36 +00001311static int gen8_emit_request(struct intel_ringbuffer *ringbuf,
1312 struct drm_i915_gem_request *request)
Oscar Mateo4da46e12014-07-24 17:04:27 +01001313{
1314 struct intel_engine_cs *ring = ringbuf->ring;
1315 u32 cmd;
1316 int ret;
1317
Nick Hoath21076372015-01-15 13:10:38 +00001318 ret = intel_logical_ring_begin(ringbuf, request->ctx, 6);
Oscar Mateo4da46e12014-07-24 17:04:27 +01001319 if (ret)
1320 return ret;
1321
Ville Syrjälä8edfbb82014-11-14 18:16:56 +02001322 cmd = MI_STORE_DWORD_IMM_GEN4;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001323 cmd |= MI_GLOBAL_GTT;
1324
1325 intel_logical_ring_emit(ringbuf, cmd);
1326 intel_logical_ring_emit(ringbuf,
1327 (ring->status_page.gfx_addr +
1328 (I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT)));
1329 intel_logical_ring_emit(ringbuf, 0);
John Harrison6259cea2014-11-24 18:49:29 +00001330 intel_logical_ring_emit(ringbuf,
1331 i915_gem_request_get_seqno(ring->outstanding_lazy_request));
Oscar Mateo4da46e12014-07-24 17:04:27 +01001332 intel_logical_ring_emit(ringbuf, MI_USER_INTERRUPT);
1333 intel_logical_ring_emit(ringbuf, MI_NOOP);
Nick Hoath21076372015-01-15 13:10:38 +00001334 intel_logical_ring_advance_and_submit(ringbuf, request->ctx, request);
Oscar Mateo4da46e12014-07-24 17:04:27 +01001335
1336 return 0;
1337}
1338
Damien Lespiaucef437a2015-02-10 19:32:19 +00001339static int intel_lr_context_render_state_init(struct intel_engine_cs *ring,
1340 struct intel_context *ctx)
1341{
1342 struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
1343 struct render_state so;
1344 struct drm_i915_file_private *file_priv = ctx->file_priv;
1345 struct drm_file *file = file_priv ? file_priv->file : NULL;
1346 int ret;
1347
1348 ret = i915_gem_render_state_prepare(ring, &so);
1349 if (ret)
1350 return ret;
1351
1352 if (so.rodata == NULL)
1353 return 0;
1354
1355 ret = ring->emit_bb_start(ringbuf,
1356 ctx,
1357 so.ggtt_offset,
1358 I915_DISPATCH_SECURE);
1359 if (ret)
1360 goto out;
1361
1362 i915_vma_move_to_active(i915_gem_obj_to_ggtt(so.obj), ring);
1363
1364 ret = __i915_add_request(ring, file, so.obj);
1365 /* intel_logical_ring_add_request moves object to inactive if it
1366 * fails */
1367out:
1368 i915_gem_render_state_fini(&so);
1369 return ret;
1370}
1371
Thomas Daniele7778be2014-12-02 12:50:48 +00001372static int gen8_init_rcs_context(struct intel_engine_cs *ring,
1373 struct intel_context *ctx)
1374{
1375 int ret;
1376
1377 ret = intel_logical_ring_workarounds_emit(ring, ctx);
1378 if (ret)
1379 return ret;
1380
1381 return intel_lr_context_render_state_init(ring, ctx);
1382}
1383
Oscar Mateo73e4d072014-07-24 17:04:48 +01001384/**
1385 * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
1386 *
1387 * @ring: Engine Command Streamer.
1388 *
1389 */
Oscar Mateo454afeb2014-07-24 17:04:22 +01001390void intel_logical_ring_cleanup(struct intel_engine_cs *ring)
1391{
John Harrison6402c332014-10-31 12:00:26 +00001392 struct drm_i915_private *dev_priv;
Oscar Mateo9832b9d2014-07-24 17:04:30 +01001393
Oscar Mateo48d82382014-07-24 17:04:23 +01001394 if (!intel_ring_initialized(ring))
1395 return;
1396
John Harrison6402c332014-10-31 12:00:26 +00001397 dev_priv = ring->dev->dev_private;
1398
Oscar Mateo9832b9d2014-07-24 17:04:30 +01001399 intel_logical_ring_stop(ring);
1400 WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
John Harrison6259cea2014-11-24 18:49:29 +00001401 i915_gem_request_assign(&ring->outstanding_lazy_request, NULL);
Oscar Mateo48d82382014-07-24 17:04:23 +01001402
1403 if (ring->cleanup)
1404 ring->cleanup(ring);
1405
1406 i915_cmd_parser_fini_ring(ring);
Chris Wilson06fbca72015-04-07 16:20:36 +01001407 i915_gem_batch_pool_fini(&ring->batch_pool);
Oscar Mateo48d82382014-07-24 17:04:23 +01001408
1409 if (ring->status_page.obj) {
1410 kunmap(sg_page(ring->status_page.obj->pages->sgl));
1411 ring->status_page.obj = NULL;
1412 }
Oscar Mateo454afeb2014-07-24 17:04:22 +01001413}
1414
1415static int logical_ring_init(struct drm_device *dev, struct intel_engine_cs *ring)
1416{
Oscar Mateo48d82382014-07-24 17:04:23 +01001417 int ret;
Oscar Mateo48d82382014-07-24 17:04:23 +01001418
1419 /* Intentionally left blank. */
1420 ring->buffer = NULL;
1421
1422 ring->dev = dev;
1423 INIT_LIST_HEAD(&ring->active_list);
1424 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson06fbca72015-04-07 16:20:36 +01001425 i915_gem_batch_pool_init(dev, &ring->batch_pool);
Oscar Mateo48d82382014-07-24 17:04:23 +01001426 init_waitqueue_head(&ring->irq_queue);
1427
Michel Thierryacdd8842014-07-24 17:04:38 +01001428 INIT_LIST_HEAD(&ring->execlist_queue);
Thomas Danielc86ee3a92014-11-13 10:27:05 +00001429 INIT_LIST_HEAD(&ring->execlist_retired_req_list);
Michel Thierryacdd8842014-07-24 17:04:38 +01001430 spin_lock_init(&ring->execlist_lock);
1431
Oscar Mateo48d82382014-07-24 17:04:23 +01001432 ret = i915_cmd_parser_init_ring(ring);
1433 if (ret)
1434 return ret;
1435
Oscar Mateo564ddb22014-08-21 11:40:54 +01001436 ret = intel_lr_context_deferred_create(ring->default_context, ring);
1437
1438 return ret;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001439}
1440
1441static int logical_render_ring_init(struct drm_device *dev)
1442{
1443 struct drm_i915_private *dev_priv = dev->dev_private;
1444 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Daniel Vetter99be1df2014-11-20 00:33:06 +01001445 int ret;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001446
1447 ring->name = "render ring";
1448 ring->id = RCS;
1449 ring->mmio_base = RENDER_RING_BASE;
1450 ring->irq_enable_mask =
1451 GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001452 ring->irq_keep_mask =
1453 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
1454 if (HAS_L3_DPF(dev))
1455 ring->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001456
Damien Lespiau82ef8222015-02-09 19:33:08 +00001457 if (INTEL_INFO(dev)->gen >= 9)
1458 ring->init_hw = gen9_init_render_ring;
1459 else
1460 ring->init_hw = gen8_init_render_ring;
Thomas Daniele7778be2014-12-02 12:50:48 +00001461 ring->init_context = gen8_init_rcs_context;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001462 ring->cleanup = intel_fini_pipe_control;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001463 ring->get_seqno = gen8_get_seqno;
1464 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001465 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001466 ring->emit_flush = gen8_emit_flush_render;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001467 ring->irq_get = gen8_logical_ring_get_irq;
1468 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001469 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001470
Daniel Vetter99be1df2014-11-20 00:33:06 +01001471 ring->dev = dev;
1472 ret = logical_ring_init(dev, ring);
1473 if (ret)
1474 return ret;
1475
1476 return intel_init_pipe_control(ring);
Oscar Mateo454afeb2014-07-24 17:04:22 +01001477}
1478
1479static int logical_bsd_ring_init(struct drm_device *dev)
1480{
1481 struct drm_i915_private *dev_priv = dev->dev_private;
1482 struct intel_engine_cs *ring = &dev_priv->ring[VCS];
1483
1484 ring->name = "bsd ring";
1485 ring->id = VCS;
1486 ring->mmio_base = GEN6_BSD_RING_BASE;
1487 ring->irq_enable_mask =
1488 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001489 ring->irq_keep_mask =
1490 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001491
Daniel Vetterecfe00d2014-11-20 00:33:04 +01001492 ring->init_hw = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001493 ring->get_seqno = gen8_get_seqno;
1494 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001495 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001496 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001497 ring->irq_get = gen8_logical_ring_get_irq;
1498 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001499 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001500
Oscar Mateo454afeb2014-07-24 17:04:22 +01001501 return logical_ring_init(dev, ring);
1502}
1503
1504static int logical_bsd2_ring_init(struct drm_device *dev)
1505{
1506 struct drm_i915_private *dev_priv = dev->dev_private;
1507 struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
1508
1509 ring->name = "bds2 ring";
1510 ring->id = VCS2;
1511 ring->mmio_base = GEN8_BSD2_RING_BASE;
1512 ring->irq_enable_mask =
1513 GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001514 ring->irq_keep_mask =
1515 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001516
Daniel Vetterecfe00d2014-11-20 00:33:04 +01001517 ring->init_hw = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001518 ring->get_seqno = gen8_get_seqno;
1519 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001520 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001521 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001522 ring->irq_get = gen8_logical_ring_get_irq;
1523 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001524 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001525
Oscar Mateo454afeb2014-07-24 17:04:22 +01001526 return logical_ring_init(dev, ring);
1527}
1528
1529static int logical_blt_ring_init(struct drm_device *dev)
1530{
1531 struct drm_i915_private *dev_priv = dev->dev_private;
1532 struct intel_engine_cs *ring = &dev_priv->ring[BCS];
1533
1534 ring->name = "blitter ring";
1535 ring->id = BCS;
1536 ring->mmio_base = BLT_RING_BASE;
1537 ring->irq_enable_mask =
1538 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001539 ring->irq_keep_mask =
1540 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001541
Daniel Vetterecfe00d2014-11-20 00:33:04 +01001542 ring->init_hw = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001543 ring->get_seqno = gen8_get_seqno;
1544 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001545 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001546 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001547 ring->irq_get = gen8_logical_ring_get_irq;
1548 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001549 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001550
Oscar Mateo454afeb2014-07-24 17:04:22 +01001551 return logical_ring_init(dev, ring);
1552}
1553
1554static int logical_vebox_ring_init(struct drm_device *dev)
1555{
1556 struct drm_i915_private *dev_priv = dev->dev_private;
1557 struct intel_engine_cs *ring = &dev_priv->ring[VECS];
1558
1559 ring->name = "video enhancement ring";
1560 ring->id = VECS;
1561 ring->mmio_base = VEBOX_RING_BASE;
1562 ring->irq_enable_mask =
1563 GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001564 ring->irq_keep_mask =
1565 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001566
Daniel Vetterecfe00d2014-11-20 00:33:04 +01001567 ring->init_hw = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001568 ring->get_seqno = gen8_get_seqno;
1569 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001570 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001571 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001572 ring->irq_get = gen8_logical_ring_get_irq;
1573 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001574 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001575
Oscar Mateo454afeb2014-07-24 17:04:22 +01001576 return logical_ring_init(dev, ring);
1577}
1578
Oscar Mateo73e4d072014-07-24 17:04:48 +01001579/**
1580 * intel_logical_rings_init() - allocate, populate and init the Engine Command Streamers
1581 * @dev: DRM device.
1582 *
1583 * This function inits the engines for an Execlists submission style (the equivalent in the
1584 * legacy ringbuffer submission world would be i915_gem_init_rings). It does it only for
1585 * those engines that are present in the hardware.
1586 *
1587 * Return: non-zero if the initialization failed.
1588 */
Oscar Mateo454afeb2014-07-24 17:04:22 +01001589int intel_logical_rings_init(struct drm_device *dev)
1590{
1591 struct drm_i915_private *dev_priv = dev->dev_private;
1592 int ret;
1593
1594 ret = logical_render_ring_init(dev);
1595 if (ret)
1596 return ret;
1597
1598 if (HAS_BSD(dev)) {
1599 ret = logical_bsd_ring_init(dev);
1600 if (ret)
1601 goto cleanup_render_ring;
1602 }
1603
1604 if (HAS_BLT(dev)) {
1605 ret = logical_blt_ring_init(dev);
1606 if (ret)
1607 goto cleanup_bsd_ring;
1608 }
1609
1610 if (HAS_VEBOX(dev)) {
1611 ret = logical_vebox_ring_init(dev);
1612 if (ret)
1613 goto cleanup_blt_ring;
1614 }
1615
1616 if (HAS_BSD2(dev)) {
1617 ret = logical_bsd2_ring_init(dev);
1618 if (ret)
1619 goto cleanup_vebox_ring;
1620 }
1621
1622 ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
1623 if (ret)
1624 goto cleanup_bsd2_ring;
1625
1626 return 0;
1627
1628cleanup_bsd2_ring:
1629 intel_logical_ring_cleanup(&dev_priv->ring[VCS2]);
1630cleanup_vebox_ring:
1631 intel_logical_ring_cleanup(&dev_priv->ring[VECS]);
1632cleanup_blt_ring:
1633 intel_logical_ring_cleanup(&dev_priv->ring[BCS]);
1634cleanup_bsd_ring:
1635 intel_logical_ring_cleanup(&dev_priv->ring[VCS]);
1636cleanup_render_ring:
1637 intel_logical_ring_cleanup(&dev_priv->ring[RCS]);
1638
1639 return ret;
1640}
1641
Jeff McGee0cea6502015-02-13 10:27:56 -06001642static u32
1643make_rpcs(struct drm_device *dev)
1644{
1645 u32 rpcs = 0;
1646
1647 /*
1648 * No explicit RPCS request is needed to ensure full
1649 * slice/subslice/EU enablement prior to Gen9.
1650 */
1651 if (INTEL_INFO(dev)->gen < 9)
1652 return 0;
1653
1654 /*
1655 * Starting in Gen9, render power gating can leave
1656 * slice/subslice/EU in a partially enabled state. We
1657 * must make an explicit request through RPCS for full
1658 * enablement.
1659 */
1660 if (INTEL_INFO(dev)->has_slice_pg) {
1661 rpcs |= GEN8_RPCS_S_CNT_ENABLE;
1662 rpcs |= INTEL_INFO(dev)->slice_total <<
1663 GEN8_RPCS_S_CNT_SHIFT;
1664 rpcs |= GEN8_RPCS_ENABLE;
1665 }
1666
1667 if (INTEL_INFO(dev)->has_subslice_pg) {
1668 rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
1669 rpcs |= INTEL_INFO(dev)->subslice_per_slice <<
1670 GEN8_RPCS_SS_CNT_SHIFT;
1671 rpcs |= GEN8_RPCS_ENABLE;
1672 }
1673
1674 if (INTEL_INFO(dev)->has_eu_pg) {
1675 rpcs |= INTEL_INFO(dev)->eu_per_subslice <<
1676 GEN8_RPCS_EU_MIN_SHIFT;
1677 rpcs |= INTEL_INFO(dev)->eu_per_subslice <<
1678 GEN8_RPCS_EU_MAX_SHIFT;
1679 rpcs |= GEN8_RPCS_ENABLE;
1680 }
1681
1682 return rpcs;
1683}
1684
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001685static int
1686populate_lr_context(struct intel_context *ctx, struct drm_i915_gem_object *ctx_obj,
1687 struct intel_engine_cs *ring, struct intel_ringbuffer *ringbuf)
1688{
Thomas Daniel2d965532014-08-19 10:13:36 +01001689 struct drm_device *dev = ring->dev;
1690 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterae6c4802014-08-06 15:04:53 +02001691 struct i915_hw_ppgtt *ppgtt = ctx->ppgtt;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001692 struct page *page;
1693 uint32_t *reg_state;
1694 int ret;
1695
Thomas Daniel2d965532014-08-19 10:13:36 +01001696 if (!ppgtt)
1697 ppgtt = dev_priv->mm.aliasing_ppgtt;
1698
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001699 ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
1700 if (ret) {
1701 DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
1702 return ret;
1703 }
1704
1705 ret = i915_gem_object_get_pages(ctx_obj);
1706 if (ret) {
1707 DRM_DEBUG_DRIVER("Could not get object pages\n");
1708 return ret;
1709 }
1710
1711 i915_gem_object_pin_pages(ctx_obj);
1712
1713 /* The second page of the context object contains some fields which must
1714 * be set up prior to the first execution. */
1715 page = i915_gem_object_get_page(ctx_obj, 1);
1716 reg_state = kmap_atomic(page);
1717
1718 /* A context is actually a big batch buffer with several MI_LOAD_REGISTER_IMM
1719 * commands followed by (reg, value) pairs. The values we are setting here are
1720 * only for the first context restore: on a subsequent save, the GPU will
1721 * recreate this batchbuffer with new values (including all the missing
1722 * MI_LOAD_REGISTER_IMM commands that we are not initializing here). */
1723 if (ring->id == RCS)
1724 reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(14);
1725 else
1726 reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(11);
1727 reg_state[CTX_LRI_HEADER_0] |= MI_LRI_FORCE_POSTED;
1728 reg_state[CTX_CONTEXT_CONTROL] = RING_CONTEXT_CONTROL(ring);
1729 reg_state[CTX_CONTEXT_CONTROL+1] =
Zhi Wang5baa22c52015-02-10 17:11:36 +08001730 _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
1731 CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001732 reg_state[CTX_RING_HEAD] = RING_HEAD(ring->mmio_base);
1733 reg_state[CTX_RING_HEAD+1] = 0;
1734 reg_state[CTX_RING_TAIL] = RING_TAIL(ring->mmio_base);
1735 reg_state[CTX_RING_TAIL+1] = 0;
1736 reg_state[CTX_RING_BUFFER_START] = RING_START(ring->mmio_base);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001737 /* Ring buffer start address is not known until the buffer is pinned.
1738 * It is written to the context image in execlists_update_context()
1739 */
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001740 reg_state[CTX_RING_BUFFER_CONTROL] = RING_CTL(ring->mmio_base);
1741 reg_state[CTX_RING_BUFFER_CONTROL+1] =
1742 ((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES) | RING_VALID;
1743 reg_state[CTX_BB_HEAD_U] = ring->mmio_base + 0x168;
1744 reg_state[CTX_BB_HEAD_U+1] = 0;
1745 reg_state[CTX_BB_HEAD_L] = ring->mmio_base + 0x140;
1746 reg_state[CTX_BB_HEAD_L+1] = 0;
1747 reg_state[CTX_BB_STATE] = ring->mmio_base + 0x110;
1748 reg_state[CTX_BB_STATE+1] = (1<<5);
1749 reg_state[CTX_SECOND_BB_HEAD_U] = ring->mmio_base + 0x11c;
1750 reg_state[CTX_SECOND_BB_HEAD_U+1] = 0;
1751 reg_state[CTX_SECOND_BB_HEAD_L] = ring->mmio_base + 0x114;
1752 reg_state[CTX_SECOND_BB_HEAD_L+1] = 0;
1753 reg_state[CTX_SECOND_BB_STATE] = ring->mmio_base + 0x118;
1754 reg_state[CTX_SECOND_BB_STATE+1] = 0;
1755 if (ring->id == RCS) {
1756 /* TODO: according to BSpec, the register state context
1757 * for CHV does not have these. OTOH, these registers do
1758 * exist in CHV. I'm waiting for a clarification */
1759 reg_state[CTX_BB_PER_CTX_PTR] = ring->mmio_base + 0x1c0;
1760 reg_state[CTX_BB_PER_CTX_PTR+1] = 0;
1761 reg_state[CTX_RCS_INDIRECT_CTX] = ring->mmio_base + 0x1c4;
1762 reg_state[CTX_RCS_INDIRECT_CTX+1] = 0;
1763 reg_state[CTX_RCS_INDIRECT_CTX_OFFSET] = ring->mmio_base + 0x1c8;
1764 reg_state[CTX_RCS_INDIRECT_CTX_OFFSET+1] = 0;
1765 }
1766 reg_state[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9);
1767 reg_state[CTX_LRI_HEADER_1] |= MI_LRI_FORCE_POSTED;
1768 reg_state[CTX_CTX_TIMESTAMP] = ring->mmio_base + 0x3a8;
1769 reg_state[CTX_CTX_TIMESTAMP+1] = 0;
1770 reg_state[CTX_PDP3_UDW] = GEN8_RING_PDP_UDW(ring, 3);
1771 reg_state[CTX_PDP3_LDW] = GEN8_RING_PDP_LDW(ring, 3);
1772 reg_state[CTX_PDP2_UDW] = GEN8_RING_PDP_UDW(ring, 2);
1773 reg_state[CTX_PDP2_LDW] = GEN8_RING_PDP_LDW(ring, 2);
1774 reg_state[CTX_PDP1_UDW] = GEN8_RING_PDP_UDW(ring, 1);
1775 reg_state[CTX_PDP1_LDW] = GEN8_RING_PDP_LDW(ring, 1);
1776 reg_state[CTX_PDP0_UDW] = GEN8_RING_PDP_UDW(ring, 0);
1777 reg_state[CTX_PDP0_LDW] = GEN8_RING_PDP_LDW(ring, 0);
Michel Thierryd7b26332015-04-08 12:13:34 +01001778
1779 /* With dynamic page allocation, PDPs may not be allocated at this point,
1780 * Point the unallocated PDPs to the scratch page
Michel Thierrye5815a22015-04-08 12:13:32 +01001781 */
1782 ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
1783 ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
1784 ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
1785 ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001786 if (ring->id == RCS) {
1787 reg_state[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
Jeff McGee0cea6502015-02-13 10:27:56 -06001788 reg_state[CTX_R_PWR_CLK_STATE] = GEN8_R_PWR_CLK_STATE;
1789 reg_state[CTX_R_PWR_CLK_STATE+1] = make_rpcs(dev);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001790 }
1791
1792 kunmap_atomic(reg_state);
1793
1794 ctx_obj->dirty = 1;
1795 set_page_dirty(page);
1796 i915_gem_object_unpin_pages(ctx_obj);
1797
1798 return 0;
1799}
1800
Oscar Mateo73e4d072014-07-24 17:04:48 +01001801/**
1802 * intel_lr_context_free() - free the LRC specific bits of a context
1803 * @ctx: the LR context to free.
1804 *
1805 * The real context freeing is done in i915_gem_context_free: this only
1806 * takes care of the bits that are LRC related: the per-engine backing
1807 * objects and the logical ringbuffer.
1808 */
Oscar Mateoede7d422014-07-24 17:04:12 +01001809void intel_lr_context_free(struct intel_context *ctx)
1810{
Oscar Mateo8c8579172014-07-24 17:04:14 +01001811 int i;
1812
1813 for (i = 0; i < I915_NUM_RINGS; i++) {
1814 struct drm_i915_gem_object *ctx_obj = ctx->engine[i].state;
Oscar Mateo84c23772014-07-24 17:04:15 +01001815
Oscar Mateo8c8579172014-07-24 17:04:14 +01001816 if (ctx_obj) {
Oscar Mateodcb4c122014-11-13 10:28:10 +00001817 struct intel_ringbuffer *ringbuf =
1818 ctx->engine[i].ringbuf;
1819 struct intel_engine_cs *ring = ringbuf->ring;
1820
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001821 if (ctx == ring->default_context) {
1822 intel_unpin_ringbuffer_obj(ringbuf);
1823 i915_gem_object_ggtt_unpin(ctx_obj);
1824 }
Mika Kuoppalaa7cbede2015-01-13 11:32:25 +02001825 WARN_ON(ctx->engine[ring->id].pin_count);
Oscar Mateo84c23772014-07-24 17:04:15 +01001826 intel_destroy_ringbuffer_obj(ringbuf);
1827 kfree(ringbuf);
Oscar Mateo8c8579172014-07-24 17:04:14 +01001828 drm_gem_object_unreference(&ctx_obj->base);
1829 }
1830 }
1831}
1832
1833static uint32_t get_lr_context_size(struct intel_engine_cs *ring)
1834{
1835 int ret = 0;
1836
Michael H. Nguyen468c6812014-11-13 17:51:49 +00001837 WARN_ON(INTEL_INFO(ring->dev)->gen < 8);
Oscar Mateo8c8579172014-07-24 17:04:14 +01001838
1839 switch (ring->id) {
1840 case RCS:
Michael H. Nguyen468c6812014-11-13 17:51:49 +00001841 if (INTEL_INFO(ring->dev)->gen >= 9)
1842 ret = GEN9_LR_CONTEXT_RENDER_SIZE;
1843 else
1844 ret = GEN8_LR_CONTEXT_RENDER_SIZE;
Oscar Mateo8c8579172014-07-24 17:04:14 +01001845 break;
1846 case VCS:
1847 case BCS:
1848 case VECS:
1849 case VCS2:
1850 ret = GEN8_LR_CONTEXT_OTHER_SIZE;
1851 break;
1852 }
1853
1854 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01001855}
1856
Daniel Vetter70b0ea82014-11-18 09:09:32 +01001857static void lrc_setup_hardware_status_page(struct intel_engine_cs *ring,
Thomas Daniel1df06b72014-10-29 09:52:51 +00001858 struct drm_i915_gem_object *default_ctx_obj)
1859{
1860 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1861
1862 /* The status page is offset 0 from the default context object
1863 * in LRC mode. */
1864 ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(default_ctx_obj);
1865 ring->status_page.page_addr =
1866 kmap(sg_page(default_ctx_obj->pages->sgl));
Thomas Daniel1df06b72014-10-29 09:52:51 +00001867 ring->status_page.obj = default_ctx_obj;
1868
1869 I915_WRITE(RING_HWS_PGA(ring->mmio_base),
1870 (u32)ring->status_page.gfx_addr);
1871 POSTING_READ(RING_HWS_PGA(ring->mmio_base));
Thomas Daniel1df06b72014-10-29 09:52:51 +00001872}
1873
Oscar Mateo73e4d072014-07-24 17:04:48 +01001874/**
1875 * intel_lr_context_deferred_create() - create the LRC specific bits of a context
1876 * @ctx: LR context to create.
1877 * @ring: engine to be used with the context.
1878 *
1879 * This function can be called more than once, with different engines, if we plan
1880 * to use the context with them. The context backing objects and the ringbuffers
1881 * (specially the ringbuffer backing objects) suck a lot of memory up, and that's why
1882 * the creation is a deferred call: it's better to make sure first that we need to use
1883 * a given ring with the context.
1884 *
Masanari Iida32197aa2014-10-20 23:53:13 +09001885 * Return: non-zero on error.
Oscar Mateo73e4d072014-07-24 17:04:48 +01001886 */
Oscar Mateoede7d422014-07-24 17:04:12 +01001887int intel_lr_context_deferred_create(struct intel_context *ctx,
1888 struct intel_engine_cs *ring)
1889{
Oscar Mateodcb4c122014-11-13 10:28:10 +00001890 const bool is_global_default_ctx = (ctx == ring->default_context);
Oscar Mateo8c8579172014-07-24 17:04:14 +01001891 struct drm_device *dev = ring->dev;
1892 struct drm_i915_gem_object *ctx_obj;
1893 uint32_t context_size;
Oscar Mateo84c23772014-07-24 17:04:15 +01001894 struct intel_ringbuffer *ringbuf;
Oscar Mateo8c8579172014-07-24 17:04:14 +01001895 int ret;
1896
Oscar Mateoede7d422014-07-24 17:04:12 +01001897 WARN_ON(ctx->legacy_hw_ctx.rcs_state != NULL);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01001898 WARN_ON(ctx->engine[ring->id].state);
Oscar Mateoede7d422014-07-24 17:04:12 +01001899
Oscar Mateo8c8579172014-07-24 17:04:14 +01001900 context_size = round_up(get_lr_context_size(ring), 4096);
1901
1902 ctx_obj = i915_gem_alloc_context_obj(dev, context_size);
1903 if (IS_ERR(ctx_obj)) {
1904 ret = PTR_ERR(ctx_obj);
1905 DRM_DEBUG_DRIVER("Alloc LRC backing obj failed: %d\n", ret);
1906 return ret;
1907 }
1908
Oscar Mateodcb4c122014-11-13 10:28:10 +00001909 if (is_global_default_ctx) {
1910 ret = i915_gem_obj_ggtt_pin(ctx_obj, GEN8_LR_CONTEXT_ALIGN, 0);
1911 if (ret) {
1912 DRM_DEBUG_DRIVER("Pin LRC backing obj failed: %d\n",
1913 ret);
1914 drm_gem_object_unreference(&ctx_obj->base);
1915 return ret;
1916 }
Oscar Mateo8c8579172014-07-24 17:04:14 +01001917 }
1918
Oscar Mateo84c23772014-07-24 17:04:15 +01001919 ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
1920 if (!ringbuf) {
1921 DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s\n",
1922 ring->name);
Oscar Mateo84c23772014-07-24 17:04:15 +01001923 ret = -ENOMEM;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001924 goto error_unpin_ctx;
Oscar Mateo84c23772014-07-24 17:04:15 +01001925 }
1926
Daniel Vetter0c7dd532014-08-11 16:17:44 +02001927 ringbuf->ring = ring;
Oscar Mateo582d67f2014-07-24 17:04:16 +01001928
Oscar Mateo84c23772014-07-24 17:04:15 +01001929 ringbuf->size = 32 * PAGE_SIZE;
1930 ringbuf->effective_size = ringbuf->size;
1931 ringbuf->head = 0;
1932 ringbuf->tail = 0;
Oscar Mateo84c23772014-07-24 17:04:15 +01001933 ringbuf->last_retired_head = -1;
Dave Gordonebd0fd42014-11-27 11:22:49 +00001934 intel_ring_update_space(ringbuf);
Oscar Mateo84c23772014-07-24 17:04:15 +01001935
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001936 if (ringbuf->obj == NULL) {
1937 ret = intel_alloc_ringbuffer_obj(dev, ringbuf);
1938 if (ret) {
1939 DRM_DEBUG_DRIVER(
1940 "Failed to allocate ringbuffer obj %s: %d\n",
Oscar Mateo84c23772014-07-24 17:04:15 +01001941 ring->name, ret);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001942 goto error_free_rbuf;
1943 }
1944
1945 if (is_global_default_ctx) {
1946 ret = intel_pin_and_map_ringbuffer_obj(dev, ringbuf);
1947 if (ret) {
1948 DRM_ERROR(
1949 "Failed to pin and map ringbuffer %s: %d\n",
1950 ring->name, ret);
1951 goto error_destroy_rbuf;
1952 }
1953 }
1954
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001955 }
1956
1957 ret = populate_lr_context(ctx, ctx_obj, ring, ringbuf);
1958 if (ret) {
1959 DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001960 goto error;
Oscar Mateo84c23772014-07-24 17:04:15 +01001961 }
1962
1963 ctx->engine[ring->id].ringbuf = ringbuf;
Oscar Mateo8c8579172014-07-24 17:04:14 +01001964 ctx->engine[ring->id].state = ctx_obj;
Oscar Mateoede7d422014-07-24 17:04:12 +01001965
Daniel Vetter70b0ea82014-11-18 09:09:32 +01001966 if (ctx == ring->default_context)
1967 lrc_setup_hardware_status_page(ring, ctx_obj);
Thomas Daniele7778be2014-12-02 12:50:48 +00001968 else if (ring->id == RCS && !ctx->rcs_initialized) {
Michel Thierry771b9a52014-11-11 16:47:33 +00001969 if (ring->init_context) {
1970 ret = ring->init_context(ring, ctx);
Thomas Daniele7778be2014-12-02 12:50:48 +00001971 if (ret) {
Michel Thierry771b9a52014-11-11 16:47:33 +00001972 DRM_ERROR("ring init context: %d\n", ret);
Thomas Daniele7778be2014-12-02 12:50:48 +00001973 ctx->engine[ring->id].ringbuf = NULL;
1974 ctx->engine[ring->id].state = NULL;
1975 goto error;
1976 }
Michel Thierry771b9a52014-11-11 16:47:33 +00001977 }
1978
Oscar Mateo564ddb22014-08-21 11:40:54 +01001979 ctx->rcs_initialized = true;
1980 }
1981
Oscar Mateoede7d422014-07-24 17:04:12 +01001982 return 0;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001983
1984error:
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001985 if (is_global_default_ctx)
1986 intel_unpin_ringbuffer_obj(ringbuf);
1987error_destroy_rbuf:
1988 intel_destroy_ringbuffer_obj(ringbuf);
1989error_free_rbuf:
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001990 kfree(ringbuf);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001991error_unpin_ctx:
Oscar Mateodcb4c122014-11-13 10:28:10 +00001992 if (is_global_default_ctx)
1993 i915_gem_object_ggtt_unpin(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001994 drm_gem_object_unreference(&ctx_obj->base);
1995 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01001996}
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00001997
1998void intel_lr_context_reset(struct drm_device *dev,
1999 struct intel_context *ctx)
2000{
2001 struct drm_i915_private *dev_priv = dev->dev_private;
2002 struct intel_engine_cs *ring;
2003 int i;
2004
2005 for_each_ring(ring, dev_priv, i) {
2006 struct drm_i915_gem_object *ctx_obj =
2007 ctx->engine[ring->id].state;
2008 struct intel_ringbuffer *ringbuf =
2009 ctx->engine[ring->id].ringbuf;
2010 uint32_t *reg_state;
2011 struct page *page;
2012
2013 if (!ctx_obj)
2014 continue;
2015
2016 if (i915_gem_object_get_pages(ctx_obj)) {
2017 WARN(1, "Failed get_pages for context obj\n");
2018 continue;
2019 }
2020 page = i915_gem_object_get_page(ctx_obj, 1);
2021 reg_state = kmap_atomic(page);
2022
2023 reg_state[CTX_RING_HEAD+1] = 0;
2024 reg_state[CTX_RING_TAIL+1] = 0;
2025
2026 kunmap_atomic(reg_state);
2027
2028 ringbuf->head = 0;
2029 ringbuf->tail = 0;
2030 }
2031}