blob: fd285d32f62ae51696942d336e0f1d11cd9c0e9d [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
Mika Kuoppala05d98242015-07-03 17:09:33 +0300332static int execlists_update_context(struct drm_i915_gem_request *rq)
Oscar Mateoae1250b2014-07-24 17:04:37 +0100333{
Mika Kuoppala05d98242015-07-03 17:09:33 +0300334 struct intel_engine_cs *ring = rq->ring;
335 struct i915_hw_ppgtt *ppgtt = rq->ctx->ppgtt;
336 struct drm_i915_gem_object *ctx_obj = rq->ctx->engine[ring->id].state;
337 struct drm_i915_gem_object *rb_obj = rq->ringbuf->obj;
Oscar Mateoae1250b2014-07-24 17:04:37 +0100338 struct page *page;
339 uint32_t *reg_state;
340
Mika Kuoppala05d98242015-07-03 17:09:33 +0300341 BUG_ON(!ctx_obj);
342 WARN_ON(!i915_gem_obj_is_pinned(ctx_obj));
343 WARN_ON(!i915_gem_obj_is_pinned(rb_obj));
344
Oscar Mateoae1250b2014-07-24 17:04:37 +0100345 page = i915_gem_object_get_page(ctx_obj, 1);
346 reg_state = kmap_atomic(page);
347
Mika Kuoppala05d98242015-07-03 17:09:33 +0300348 reg_state[CTX_RING_TAIL+1] = rq->tail;
349 reg_state[CTX_RING_BUFFER_START+1] = i915_gem_obj_ggtt_offset(rb_obj);
Oscar Mateoae1250b2014-07-24 17:04:37 +0100350
Michel Thierryd7b26332015-04-08 12:13:34 +0100351 /* True PPGTT with dynamic page allocation: update PDP registers and
352 * point the unallocated PDPs to the scratch page
353 */
354 if (ppgtt) {
355 ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
356 ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
357 ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
358 ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
359 }
360
Oscar Mateoae1250b2014-07-24 17:04:37 +0100361 kunmap_atomic(reg_state);
362
363 return 0;
364}
365
Mika Kuoppalad8cb8872015-07-03 17:09:32 +0300366static void execlists_submit_requests(struct drm_i915_gem_request *rq0,
367 struct drm_i915_gem_request *rq1)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100368{
Mika Kuoppalad8cb8872015-07-03 17:09:32 +0300369 struct intel_engine_cs *ring = rq0->ring;
370 struct drm_i915_gem_object *ctx_obj0 = rq0->ctx->engine[ring->id].state;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100371 struct drm_i915_gem_object *ctx_obj1 = NULL;
372
Mika Kuoppala05d98242015-07-03 17:09:33 +0300373 execlists_update_context(rq0);
Oscar Mateoae1250b2014-07-24 17:04:37 +0100374
Mika Kuoppalad8cb8872015-07-03 17:09:32 +0300375 if (rq1) {
Mika Kuoppala05d98242015-07-03 17:09:33 +0300376 execlists_update_context(rq1);
Mika Kuoppalad8cb8872015-07-03 17:09:32 +0300377 ctx_obj1 = rq1->ctx->engine[ring->id].state;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100378 }
379
380 execlists_elsp_write(ring, ctx_obj0, ctx_obj1);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100381}
382
Michel Thierryacdd8842014-07-24 17:04:38 +0100383static void execlists_context_unqueue(struct intel_engine_cs *ring)
384{
Nick Hoath6d3d8272015-01-15 13:10:39 +0000385 struct drm_i915_gem_request *req0 = NULL, *req1 = NULL;
386 struct drm_i915_gem_request *cursor = NULL, *tmp = NULL;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100387
388 assert_spin_locked(&ring->execlist_lock);
Michel Thierryacdd8842014-07-24 17:04:38 +0100389
Peter Antoine779949f2015-05-11 16:03:27 +0100390 /*
391 * If irqs are not active generate a warning as batches that finish
392 * without the irqs may get lost and a GPU Hang may occur.
393 */
394 WARN_ON(!intel_irqs_enabled(ring->dev->dev_private));
395
Michel Thierryacdd8842014-07-24 17:04:38 +0100396 if (list_empty(&ring->execlist_queue))
397 return;
398
399 /* Try to read in pairs */
400 list_for_each_entry_safe(cursor, tmp, &ring->execlist_queue,
401 execlist_link) {
402 if (!req0) {
403 req0 = cursor;
Nick Hoath6d3d8272015-01-15 13:10:39 +0000404 } else if (req0->ctx == cursor->ctx) {
Michel Thierryacdd8842014-07-24 17:04:38 +0100405 /* Same ctx: ignore first request, as second request
406 * will update tail past first request's workload */
Oscar Mateoe1fee722014-07-24 17:04:40 +0100407 cursor->elsp_submitted = req0->elsp_submitted;
Michel Thierryacdd8842014-07-24 17:04:38 +0100408 list_del(&req0->execlist_link);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000409 list_add_tail(&req0->execlist_link,
410 &ring->execlist_retired_req_list);
Michel Thierryacdd8842014-07-24 17:04:38 +0100411 req0 = cursor;
412 } else {
413 req1 = cursor;
414 break;
415 }
416 }
417
Michel Thierry53292cd2015-04-15 18:11:33 +0100418 if (IS_GEN8(ring->dev) || IS_GEN9(ring->dev)) {
419 /*
420 * WaIdleLiteRestore: make sure we never cause a lite
421 * restore with HEAD==TAIL
422 */
Michel Thierryd63f8202015-04-27 12:31:44 +0100423 if (req0->elsp_submitted) {
Michel Thierry53292cd2015-04-15 18:11:33 +0100424 /*
425 * Apply the wa NOOPS to prevent ring:HEAD == req:TAIL
426 * as we resubmit the request. See gen8_emit_request()
427 * for where we prepare the padding after the end of the
428 * request.
429 */
430 struct intel_ringbuffer *ringbuf;
431
432 ringbuf = req0->ctx->engine[ring->id].ringbuf;
433 req0->tail += 8;
434 req0->tail &= ringbuf->size - 1;
435 }
436 }
437
Oscar Mateoe1fee722014-07-24 17:04:40 +0100438 WARN_ON(req1 && req1->elsp_submitted);
439
Mika Kuoppalad8cb8872015-07-03 17:09:32 +0300440 execlists_submit_requests(req0, req1);
Oscar Mateoe1fee722014-07-24 17:04:40 +0100441
442 req0->elsp_submitted++;
443 if (req1)
444 req1->elsp_submitted++;
Michel Thierryacdd8842014-07-24 17:04:38 +0100445}
446
Thomas Daniele981e7b2014-07-24 17:04:39 +0100447static bool execlists_check_remove_request(struct intel_engine_cs *ring,
448 u32 request_id)
449{
Nick Hoath6d3d8272015-01-15 13:10:39 +0000450 struct drm_i915_gem_request *head_req;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100451
452 assert_spin_locked(&ring->execlist_lock);
453
454 head_req = list_first_entry_or_null(&ring->execlist_queue,
Nick Hoath6d3d8272015-01-15 13:10:39 +0000455 struct drm_i915_gem_request,
Thomas Daniele981e7b2014-07-24 17:04:39 +0100456 execlist_link);
457
458 if (head_req != NULL) {
459 struct drm_i915_gem_object *ctx_obj =
Nick Hoath6d3d8272015-01-15 13:10:39 +0000460 head_req->ctx->engine[ring->id].state;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100461 if (intel_execlists_ctx_id(ctx_obj) == request_id) {
Oscar Mateoe1fee722014-07-24 17:04:40 +0100462 WARN(head_req->elsp_submitted == 0,
463 "Never submitted head request\n");
464
465 if (--head_req->elsp_submitted <= 0) {
466 list_del(&head_req->execlist_link);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000467 list_add_tail(&head_req->execlist_link,
468 &ring->execlist_retired_req_list);
Oscar Mateoe1fee722014-07-24 17:04:40 +0100469 return true;
470 }
Thomas Daniele981e7b2014-07-24 17:04:39 +0100471 }
472 }
473
474 return false;
475}
476
Oscar Mateo73e4d072014-07-24 17:04:48 +0100477/**
Daniel Vetter3f7531c2014-12-10 17:41:43 +0100478 * intel_lrc_irq_handler() - handle Context Switch interrupts
Oscar Mateo73e4d072014-07-24 17:04:48 +0100479 * @ring: Engine Command Streamer to handle.
480 *
481 * Check the unread Context Status Buffers and manage the submission of new
482 * contexts to the ELSP accordingly.
483 */
Daniel Vetter3f7531c2014-12-10 17:41:43 +0100484void intel_lrc_irq_handler(struct intel_engine_cs *ring)
Thomas Daniele981e7b2014-07-24 17:04:39 +0100485{
486 struct drm_i915_private *dev_priv = ring->dev->dev_private;
487 u32 status_pointer;
488 u8 read_pointer;
489 u8 write_pointer;
490 u32 status;
491 u32 status_id;
492 u32 submit_contexts = 0;
493
494 status_pointer = I915_READ(RING_CONTEXT_STATUS_PTR(ring));
495
496 read_pointer = ring->next_context_status_buffer;
497 write_pointer = status_pointer & 0x07;
498 if (read_pointer > write_pointer)
499 write_pointer += 6;
500
501 spin_lock(&ring->execlist_lock);
502
503 while (read_pointer < write_pointer) {
504 read_pointer++;
505 status = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
506 (read_pointer % 6) * 8);
507 status_id = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
508 (read_pointer % 6) * 8 + 4);
509
Oscar Mateoe1fee722014-07-24 17:04:40 +0100510 if (status & GEN8_CTX_STATUS_PREEMPTED) {
511 if (status & GEN8_CTX_STATUS_LITE_RESTORE) {
512 if (execlists_check_remove_request(ring, status_id))
513 WARN(1, "Lite Restored request removed from queue\n");
514 } else
515 WARN(1, "Preemption without Lite Restore\n");
516 }
517
518 if ((status & GEN8_CTX_STATUS_ACTIVE_IDLE) ||
519 (status & GEN8_CTX_STATUS_ELEMENT_SWITCH)) {
Thomas Daniele981e7b2014-07-24 17:04:39 +0100520 if (execlists_check_remove_request(ring, status_id))
521 submit_contexts++;
522 }
523 }
524
525 if (submit_contexts != 0)
526 execlists_context_unqueue(ring);
527
528 spin_unlock(&ring->execlist_lock);
529
530 WARN(submit_contexts > 2, "More than two context complete events?\n");
531 ring->next_context_status_buffer = write_pointer % 6;
532
533 I915_WRITE(RING_CONTEXT_STATUS_PTR(ring),
534 ((u32)ring->next_context_status_buffer & 0x07) << 8);
535}
536
John Harrisonae707972015-05-29 17:44:14 +0100537static int execlists_context_queue(struct drm_i915_gem_request *request)
Michel Thierryacdd8842014-07-24 17:04:38 +0100538{
John Harrisonae707972015-05-29 17:44:14 +0100539 struct intel_engine_cs *ring = request->ring;
Nick Hoath6d3d8272015-01-15 13:10:39 +0000540 struct drm_i915_gem_request *cursor;
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100541 int num_elements = 0;
Michel Thierryacdd8842014-07-24 17:04:38 +0100542
John Harrisonae707972015-05-29 17:44:14 +0100543 if (request->ctx != ring->default_context)
544 intel_lr_context_pin(ring, request->ctx);
John Harrison9bb1af42015-05-29 17:44:13 +0100545
546 i915_gem_request_reference(request);
547
John Harrisonae707972015-05-29 17:44:14 +0100548 request->tail = request->ringbuf->tail;
Nick Hoath2d129552015-01-15 13:10:36 +0000549
Chris Wilsonb5eba372015-04-07 16:20:48 +0100550 spin_lock_irq(&ring->execlist_lock);
Michel Thierryacdd8842014-07-24 17:04:38 +0100551
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100552 list_for_each_entry(cursor, &ring->execlist_queue, execlist_link)
553 if (++num_elements > 2)
554 break;
555
556 if (num_elements > 2) {
Nick Hoath6d3d8272015-01-15 13:10:39 +0000557 struct drm_i915_gem_request *tail_req;
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100558
559 tail_req = list_last_entry(&ring->execlist_queue,
Nick Hoath6d3d8272015-01-15 13:10:39 +0000560 struct drm_i915_gem_request,
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100561 execlist_link);
562
John Harrisonae707972015-05-29 17:44:14 +0100563 if (request->ctx == tail_req->ctx) {
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100564 WARN(tail_req->elsp_submitted != 0,
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000565 "More than 2 already-submitted reqs queued\n");
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100566 list_del(&tail_req->execlist_link);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000567 list_add_tail(&tail_req->execlist_link,
568 &ring->execlist_retired_req_list);
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100569 }
570 }
571
Nick Hoath6d3d8272015-01-15 13:10:39 +0000572 list_add_tail(&request->execlist_link, &ring->execlist_queue);
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100573 if (num_elements == 0)
Michel Thierryacdd8842014-07-24 17:04:38 +0100574 execlists_context_unqueue(ring);
575
Chris Wilsonb5eba372015-04-07 16:20:48 +0100576 spin_unlock_irq(&ring->execlist_lock);
Michel Thierryacdd8842014-07-24 17:04:38 +0100577
578 return 0;
579}
580
John Harrison2f200552015-05-29 17:43:53 +0100581static int logical_ring_invalidate_all_caches(struct drm_i915_gem_request *req)
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100582{
John Harrison2f200552015-05-29 17:43:53 +0100583 struct intel_engine_cs *ring = req->ring;
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100584 uint32_t flush_domains;
585 int ret;
586
587 flush_domains = 0;
588 if (ring->gpu_caches_dirty)
589 flush_domains = I915_GEM_GPU_DOMAINS;
590
John Harrison7deb4d32015-05-29 17:43:59 +0100591 ret = ring->emit_flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100592 if (ret)
593 return ret;
594
595 ring->gpu_caches_dirty = false;
596 return 0;
597}
598
John Harrison535fbe82015-05-29 17:43:32 +0100599static int execlists_move_to_gpu(struct drm_i915_gem_request *req,
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100600 struct list_head *vmas)
601{
John Harrison535fbe82015-05-29 17:43:32 +0100602 const unsigned other_rings = ~intel_ring_flag(req->ring);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100603 struct i915_vma *vma;
604 uint32_t flush_domains = 0;
605 bool flush_chipset = false;
606 int ret;
607
608 list_for_each_entry(vma, vmas, exec_list) {
609 struct drm_i915_gem_object *obj = vma->obj;
610
Chris Wilson03ade512015-04-27 13:41:18 +0100611 if (obj->active & other_rings) {
John Harrison91af1272015-06-18 13:14:56 +0100612 ret = i915_gem_object_sync(obj, req->ring, &req);
Chris Wilson03ade512015-04-27 13:41:18 +0100613 if (ret)
614 return ret;
615 }
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100616
617 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
618 flush_chipset |= i915_gem_clflush_object(obj, false);
619
620 flush_domains |= obj->base.write_domain;
621 }
622
623 if (flush_domains & I915_GEM_DOMAIN_GTT)
624 wmb();
625
626 /* Unconditionally invalidate gpu caches and ensure that we do flush
627 * any residual writes from the previous batch.
628 */
John Harrison2f200552015-05-29 17:43:53 +0100629 return logical_ring_invalidate_all_caches(req);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100630}
631
John Harrison40e895c2015-05-29 17:43:26 +0100632int intel_logical_ring_alloc_request_extras(struct drm_i915_gem_request *request)
John Harrisonbc0dce32015-03-19 12:30:07 +0000633{
John Harrisonbc0dce32015-03-19 12:30:07 +0000634 int ret;
635
Mika Kuoppalaf3cc01f2015-07-06 11:08:30 +0300636 request->ringbuf = request->ctx->engine[request->ring->id].ringbuf;
637
John Harrison40e895c2015-05-29 17:43:26 +0100638 if (request->ctx != request->ring->default_context) {
639 ret = intel_lr_context_pin(request->ring, request->ctx);
John Harrison6689cb22015-03-19 12:30:08 +0000640 if (ret)
John Harrisonbc0dce32015-03-19 12:30:07 +0000641 return ret;
John Harrisonbc0dce32015-03-19 12:30:07 +0000642 }
643
John Harrisonbc0dce32015-03-19 12:30:07 +0000644 return 0;
645}
646
John Harrisonae707972015-05-29 17:44:14 +0100647static int logical_ring_wait_for_space(struct drm_i915_gem_request *req,
Chris Wilson595e1ee2015-04-07 16:20:51 +0100648 int bytes)
John Harrisonbc0dce32015-03-19 12:30:07 +0000649{
John Harrisonae707972015-05-29 17:44:14 +0100650 struct intel_ringbuffer *ringbuf = req->ringbuf;
651 struct intel_engine_cs *ring = req->ring;
652 struct drm_i915_gem_request *target;
Chris Wilsonb4716182015-04-27 13:41:17 +0100653 unsigned space;
654 int ret;
John Harrisonbc0dce32015-03-19 12:30:07 +0000655
656 if (intel_ring_space(ringbuf) >= bytes)
657 return 0;
658
John Harrison79bbcc22015-06-30 12:40:55 +0100659 /* The whole point of reserving space is to not wait! */
660 WARN_ON(ringbuf->reserved_in_use);
661
John Harrisonae707972015-05-29 17:44:14 +0100662 list_for_each_entry(target, &ring->request_list, list) {
John Harrisonbc0dce32015-03-19 12:30:07 +0000663 /*
664 * The request queue is per-engine, so can contain requests
665 * from multiple ringbuffers. Here, we must ignore any that
666 * aren't from the ringbuffer we're considering.
667 */
John Harrisonae707972015-05-29 17:44:14 +0100668 if (target->ringbuf != ringbuf)
John Harrisonbc0dce32015-03-19 12:30:07 +0000669 continue;
670
671 /* Would completion of this request free enough space? */
John Harrisonae707972015-05-29 17:44:14 +0100672 space = __intel_ring_space(target->postfix, ringbuf->tail,
Chris Wilsonb4716182015-04-27 13:41:17 +0100673 ringbuf->size);
674 if (space >= bytes)
John Harrisonbc0dce32015-03-19 12:30:07 +0000675 break;
John Harrisonbc0dce32015-03-19 12:30:07 +0000676 }
677
John Harrisonae707972015-05-29 17:44:14 +0100678 if (WARN_ON(&target->list == &ring->request_list))
John Harrisonbc0dce32015-03-19 12:30:07 +0000679 return -ENOSPC;
680
John Harrisonae707972015-05-29 17:44:14 +0100681 ret = i915_wait_request(target);
John Harrisonbc0dce32015-03-19 12:30:07 +0000682 if (ret)
683 return ret;
684
Chris Wilsonb4716182015-04-27 13:41:17 +0100685 ringbuf->space = space;
686 return 0;
John Harrisonbc0dce32015-03-19 12:30:07 +0000687}
688
689/*
690 * intel_logical_ring_advance_and_submit() - advance the tail and submit the workload
John Harrisonae707972015-05-29 17:44:14 +0100691 * @request: Request to advance the logical ringbuffer of.
John Harrisonbc0dce32015-03-19 12:30:07 +0000692 *
693 * The tail is updated in our logical ringbuffer struct, not in the actual context. What
694 * really happens during submission is that the context and current tail will be placed
695 * on a queue waiting for the ELSP to be ready to accept a new context submission. At that
696 * point, the tail *inside* the context is updated and the ELSP written to.
697 */
698static void
John Harrisonae707972015-05-29 17:44:14 +0100699intel_logical_ring_advance_and_submit(struct drm_i915_gem_request *request)
John Harrisonbc0dce32015-03-19 12:30:07 +0000700{
John Harrisonae707972015-05-29 17:44:14 +0100701 struct intel_engine_cs *ring = request->ring;
John Harrisonbc0dce32015-03-19 12:30:07 +0000702
John Harrisonae707972015-05-29 17:44:14 +0100703 intel_logical_ring_advance(request->ringbuf);
John Harrisonbc0dce32015-03-19 12:30:07 +0000704
705 if (intel_ring_stopped(ring))
706 return;
707
John Harrisonae707972015-05-29 17:44:14 +0100708 execlists_context_queue(request);
John Harrisonbc0dce32015-03-19 12:30:07 +0000709}
710
John Harrison79bbcc22015-06-30 12:40:55 +0100711static void __wrap_ring_buffer(struct intel_ringbuffer *ringbuf)
John Harrisonbc0dce32015-03-19 12:30:07 +0000712{
713 uint32_t __iomem *virt;
714 int rem = ringbuf->size - ringbuf->tail;
715
John Harrisonbc0dce32015-03-19 12:30:07 +0000716 virt = ringbuf->virtual_start + ringbuf->tail;
717 rem /= 4;
718 while (rem--)
719 iowrite32(MI_NOOP, virt++);
720
721 ringbuf->tail = 0;
722 intel_ring_update_space(ringbuf);
John Harrisonbc0dce32015-03-19 12:30:07 +0000723}
724
John Harrisonae707972015-05-29 17:44:14 +0100725static int logical_ring_prepare(struct drm_i915_gem_request *req, int bytes)
John Harrisonbc0dce32015-03-19 12:30:07 +0000726{
John Harrisonae707972015-05-29 17:44:14 +0100727 struct intel_ringbuffer *ringbuf = req->ringbuf;
John Harrison79bbcc22015-06-30 12:40:55 +0100728 int remain_usable = ringbuf->effective_size - ringbuf->tail;
729 int remain_actual = ringbuf->size - ringbuf->tail;
730 int ret, total_bytes, wait_bytes = 0;
731 bool need_wrap = false;
John Harrisonbc0dce32015-03-19 12:30:07 +0000732
John Harrison79bbcc22015-06-30 12:40:55 +0100733 if (ringbuf->reserved_in_use)
734 total_bytes = bytes;
735 else
736 total_bytes = bytes + ringbuf->reserved_size;
John Harrison29b1b412015-06-18 13:10:09 +0100737
John Harrison79bbcc22015-06-30 12:40:55 +0100738 if (unlikely(bytes > remain_usable)) {
739 /*
740 * Not enough space for the basic request. So need to flush
741 * out the remainder and then wait for base + reserved.
742 */
743 wait_bytes = remain_actual + total_bytes;
744 need_wrap = true;
745 } else {
746 if (unlikely(total_bytes > remain_usable)) {
747 /*
748 * The base request will fit but the reserved space
749 * falls off the end. So only need to to wait for the
750 * reserved size after flushing out the remainder.
751 */
752 wait_bytes = remain_actual + ringbuf->reserved_size;
753 need_wrap = true;
754 } else if (total_bytes > ringbuf->space) {
755 /* No wrapping required, just waiting. */
756 wait_bytes = total_bytes;
John Harrison29b1b412015-06-18 13:10:09 +0100757 }
John Harrisonbc0dce32015-03-19 12:30:07 +0000758 }
759
John Harrison79bbcc22015-06-30 12:40:55 +0100760 if (wait_bytes) {
761 ret = logical_ring_wait_for_space(req, wait_bytes);
John Harrisonbc0dce32015-03-19 12:30:07 +0000762 if (unlikely(ret))
763 return ret;
John Harrison79bbcc22015-06-30 12:40:55 +0100764
765 if (need_wrap)
766 __wrap_ring_buffer(ringbuf);
John Harrisonbc0dce32015-03-19 12:30:07 +0000767 }
768
769 return 0;
770}
771
772/**
773 * intel_logical_ring_begin() - prepare the logical ringbuffer to accept some commands
774 *
John Harrison4d616a22015-05-29 17:44:08 +0100775 * @request: The request to start some new work for
Arun Siluvery4d78c8d2015-06-23 15:50:43 +0100776 * @ctx: Logical ring context whose ringbuffer is being prepared.
John Harrisonbc0dce32015-03-19 12:30:07 +0000777 * @num_dwords: number of DWORDs that we plan to write to the ringbuffer.
778 *
779 * The ringbuffer might not be ready to accept the commands right away (maybe it needs to
780 * be wrapped, or wait a bit for the tail to be updated). This function takes care of that
781 * and also preallocates a request (every workload submission is still mediated through
782 * requests, same as it did with legacy ringbuffer submission).
783 *
784 * Return: non-zero if the ringbuffer is not ready to be written to.
785 */
John Harrison4d616a22015-05-29 17:44:08 +0100786static int intel_logical_ring_begin(struct drm_i915_gem_request *req,
787 int num_dwords)
John Harrisonbc0dce32015-03-19 12:30:07 +0000788{
John Harrison4d616a22015-05-29 17:44:08 +0100789 struct drm_i915_private *dev_priv;
John Harrisonbc0dce32015-03-19 12:30:07 +0000790 int ret;
791
John Harrison4d616a22015-05-29 17:44:08 +0100792 WARN_ON(req == NULL);
793 dev_priv = req->ring->dev->dev_private;
794
John Harrisonbc0dce32015-03-19 12:30:07 +0000795 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
796 dev_priv->mm.interruptible);
797 if (ret)
798 return ret;
799
John Harrisonae707972015-05-29 17:44:14 +0100800 ret = logical_ring_prepare(req, num_dwords * sizeof(uint32_t));
John Harrisonbc0dce32015-03-19 12:30:07 +0000801 if (ret)
802 return ret;
803
John Harrison4d616a22015-05-29 17:44:08 +0100804 req->ringbuf->space -= num_dwords * sizeof(uint32_t);
John Harrisonbc0dce32015-03-19 12:30:07 +0000805 return 0;
806}
807
John Harrisonccd98fe2015-05-29 17:44:09 +0100808int intel_logical_ring_reserve_space(struct drm_i915_gem_request *request)
809{
810 /*
811 * The first call merely notes the reserve request and is common for
812 * all back ends. The subsequent localised _begin() call actually
813 * ensures that the reservation is available. Without the begin, if
814 * the request creator immediately submitted the request without
815 * adding any commands to it then there might not actually be
816 * sufficient room for the submission commands.
817 */
818 intel_ring_reserved_space_reserve(request->ringbuf, MIN_SPACE_FOR_ADD_REQUEST);
819
820 return intel_logical_ring_begin(request, 0);
821}
822
Oscar Mateo73e4d072014-07-24 17:04:48 +0100823/**
824 * execlists_submission() - submit a batchbuffer for execution, Execlists style
825 * @dev: DRM device.
826 * @file: DRM file.
827 * @ring: Engine Command Streamer to submit to.
828 * @ctx: Context to employ for this submission.
829 * @args: execbuffer call arguments.
830 * @vmas: list of vmas.
831 * @batch_obj: the batchbuffer to submit.
832 * @exec_start: batchbuffer start virtual address pointer.
John Harrison8e004ef2015-02-13 11:48:10 +0000833 * @dispatch_flags: translated execbuffer call flags.
Oscar Mateo73e4d072014-07-24 17:04:48 +0100834 *
835 * This is the evil twin version of i915_gem_ringbuffer_submission. It abstracts
836 * away the submission details of the execbuffer ioctl call.
837 *
838 * Return: non-zero if the submission fails.
839 */
John Harrison5f19e2b2015-05-29 17:43:27 +0100840int intel_execlists_submission(struct i915_execbuffer_params *params,
Oscar Mateo454afeb2014-07-24 17:04:22 +0100841 struct drm_i915_gem_execbuffer2 *args,
John Harrison5f19e2b2015-05-29 17:43:27 +0100842 struct list_head *vmas)
Oscar Mateo454afeb2014-07-24 17:04:22 +0100843{
John Harrison5f19e2b2015-05-29 17:43:27 +0100844 struct drm_device *dev = params->dev;
845 struct intel_engine_cs *ring = params->ring;
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100846 struct drm_i915_private *dev_priv = dev->dev_private;
John Harrison5f19e2b2015-05-29 17:43:27 +0100847 struct intel_ringbuffer *ringbuf = params->ctx->engine[ring->id].ringbuf;
848 u64 exec_start;
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100849 int instp_mode;
850 u32 instp_mask;
851 int ret;
852
853 instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
854 instp_mask = I915_EXEC_CONSTANTS_MASK;
855 switch (instp_mode) {
856 case I915_EXEC_CONSTANTS_REL_GENERAL:
857 case I915_EXEC_CONSTANTS_ABSOLUTE:
858 case I915_EXEC_CONSTANTS_REL_SURFACE:
859 if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) {
860 DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
861 return -EINVAL;
862 }
863
864 if (instp_mode != dev_priv->relative_constants_mode) {
865 if (instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
866 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
867 return -EINVAL;
868 }
869
870 /* The HW changed the meaning on this bit on gen6 */
871 instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
872 }
873 break;
874 default:
875 DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
876 return -EINVAL;
877 }
878
879 if (args->num_cliprects != 0) {
880 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
881 return -EINVAL;
882 } else {
883 if (args->DR4 == 0xffffffff) {
884 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
885 args->DR4 = 0;
886 }
887
888 if (args->DR1 || args->DR4 || args->cliprects_ptr) {
889 DRM_DEBUG("0 cliprects but dirt in cliprects fields\n");
890 return -EINVAL;
891 }
892 }
893
894 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
895 DRM_DEBUG("sol reset is gen7 only\n");
896 return -EINVAL;
897 }
898
John Harrison535fbe82015-05-29 17:43:32 +0100899 ret = execlists_move_to_gpu(params->request, vmas);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100900 if (ret)
901 return ret;
902
903 if (ring == &dev_priv->ring[RCS] &&
904 instp_mode != dev_priv->relative_constants_mode) {
John Harrison4d616a22015-05-29 17:44:08 +0100905 ret = intel_logical_ring_begin(params->request, 4);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100906 if (ret)
907 return ret;
908
909 intel_logical_ring_emit(ringbuf, MI_NOOP);
910 intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(1));
911 intel_logical_ring_emit(ringbuf, INSTPM);
912 intel_logical_ring_emit(ringbuf, instp_mask << 16 | instp_mode);
913 intel_logical_ring_advance(ringbuf);
914
915 dev_priv->relative_constants_mode = instp_mode;
916 }
917
John Harrison5f19e2b2015-05-29 17:43:27 +0100918 exec_start = params->batch_obj_vm_offset +
919 args->batch_start_offset;
920
John Harrisonbe795fc2015-05-29 17:44:03 +0100921 ret = ring->emit_bb_start(params->request, exec_start, params->dispatch_flags);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100922 if (ret)
923 return ret;
924
John Harrison95c24162015-05-29 17:43:31 +0100925 trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
John Harrison5e4be7b2015-02-13 11:48:11 +0000926
John Harrison8a8edb52015-05-29 17:43:33 +0100927 i915_gem_execbuffer_move_to_active(vmas, params->request);
John Harrisonadeca762015-05-29 17:43:28 +0100928 i915_gem_execbuffer_retire_commands(params);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100929
Oscar Mateo454afeb2014-07-24 17:04:22 +0100930 return 0;
931}
932
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000933void intel_execlists_retire_requests(struct intel_engine_cs *ring)
934{
Nick Hoath6d3d8272015-01-15 13:10:39 +0000935 struct drm_i915_gem_request *req, *tmp;
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000936 struct list_head retired_list;
937
938 WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
939 if (list_empty(&ring->execlist_retired_req_list))
940 return;
941
942 INIT_LIST_HEAD(&retired_list);
Chris Wilsonb5eba372015-04-07 16:20:48 +0100943 spin_lock_irq(&ring->execlist_lock);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000944 list_replace_init(&ring->execlist_retired_req_list, &retired_list);
Chris Wilsonb5eba372015-04-07 16:20:48 +0100945 spin_unlock_irq(&ring->execlist_lock);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000946
947 list_for_each_entry_safe(req, tmp, &retired_list, execlist_link) {
Nick Hoath6d3d8272015-01-15 13:10:39 +0000948 struct intel_context *ctx = req->ctx;
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000949 struct drm_i915_gem_object *ctx_obj =
950 ctx->engine[ring->id].state;
951
952 if (ctx_obj && (ctx != ring->default_context))
953 intel_lr_context_unpin(ring, ctx);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000954 list_del(&req->execlist_link);
Nick Hoathf8210792015-01-29 16:55:07 +0000955 i915_gem_request_unreference(req);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000956 }
957}
958
Oscar Mateo454afeb2014-07-24 17:04:22 +0100959void intel_logical_ring_stop(struct intel_engine_cs *ring)
960{
Oscar Mateo9832b9d2014-07-24 17:04:30 +0100961 struct drm_i915_private *dev_priv = ring->dev->dev_private;
962 int ret;
963
964 if (!intel_ring_initialized(ring))
965 return;
966
967 ret = intel_ring_idle(ring);
968 if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
969 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
970 ring->name, ret);
971
972 /* TODO: Is this correct with Execlists enabled? */
973 I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
974 if (wait_for_atomic((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
975 DRM_ERROR("%s :timed out trying to stop ring\n", ring->name);
976 return;
977 }
978 I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
Oscar Mateo454afeb2014-07-24 17:04:22 +0100979}
980
John Harrison4866d722015-05-29 17:43:55 +0100981int logical_ring_flush_all_caches(struct drm_i915_gem_request *req)
Oscar Mateo48e29f52014-07-24 17:04:29 +0100982{
John Harrison4866d722015-05-29 17:43:55 +0100983 struct intel_engine_cs *ring = req->ring;
Oscar Mateo48e29f52014-07-24 17:04:29 +0100984 int ret;
985
986 if (!ring->gpu_caches_dirty)
987 return 0;
988
John Harrison7deb4d32015-05-29 17:43:59 +0100989 ret = ring->emit_flush(req, 0, I915_GEM_GPU_DOMAINS);
Oscar Mateo48e29f52014-07-24 17:04:29 +0100990 if (ret)
991 return ret;
992
993 ring->gpu_caches_dirty = false;
994 return 0;
995}
996
Oscar Mateodcb4c122014-11-13 10:28:10 +0000997static int intel_lr_context_pin(struct intel_engine_cs *ring,
998 struct intel_context *ctx)
999{
1000 struct drm_i915_gem_object *ctx_obj = ctx->engine[ring->id].state;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001001 struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
Oscar Mateodcb4c122014-11-13 10:28:10 +00001002 int ret = 0;
1003
1004 WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
Mika Kuoppalaa7cbede2015-01-13 11:32:25 +02001005 if (ctx->engine[ring->id].pin_count++ == 0) {
Oscar Mateodcb4c122014-11-13 10:28:10 +00001006 ret = i915_gem_obj_ggtt_pin(ctx_obj,
1007 GEN8_LR_CONTEXT_ALIGN, 0);
1008 if (ret)
Mika Kuoppalaa7cbede2015-01-13 11:32:25 +02001009 goto reset_pin_count;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001010
1011 ret = intel_pin_and_map_ringbuffer_obj(ring->dev, ringbuf);
1012 if (ret)
1013 goto unpin_ctx_obj;
Oscar Mateodcb4c122014-11-13 10:28:10 +00001014 }
1015
1016 return ret;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001017
1018unpin_ctx_obj:
1019 i915_gem_object_ggtt_unpin(ctx_obj);
Mika Kuoppalaa7cbede2015-01-13 11:32:25 +02001020reset_pin_count:
1021 ctx->engine[ring->id].pin_count = 0;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001022
1023 return ret;
Oscar Mateodcb4c122014-11-13 10:28:10 +00001024}
1025
1026void intel_lr_context_unpin(struct intel_engine_cs *ring,
1027 struct intel_context *ctx)
1028{
1029 struct drm_i915_gem_object *ctx_obj = ctx->engine[ring->id].state;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001030 struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
Oscar Mateodcb4c122014-11-13 10:28:10 +00001031
1032 if (ctx_obj) {
1033 WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
Mika Kuoppalaa7cbede2015-01-13 11:32:25 +02001034 if (--ctx->engine[ring->id].pin_count == 0) {
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001035 intel_unpin_ringbuffer_obj(ringbuf);
Oscar Mateodcb4c122014-11-13 10:28:10 +00001036 i915_gem_object_ggtt_unpin(ctx_obj);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001037 }
Oscar Mateodcb4c122014-11-13 10:28:10 +00001038 }
1039}
1040
John Harrisone2be4fa2015-05-29 17:43:54 +01001041static int intel_logical_ring_workarounds_emit(struct drm_i915_gem_request *req)
Michel Thierry771b9a52014-11-11 16:47:33 +00001042{
1043 int ret, i;
John Harrisone2be4fa2015-05-29 17:43:54 +01001044 struct intel_engine_cs *ring = req->ring;
1045 struct intel_ringbuffer *ringbuf = req->ringbuf;
Michel Thierry771b9a52014-11-11 16:47:33 +00001046 struct drm_device *dev = ring->dev;
1047 struct drm_i915_private *dev_priv = dev->dev_private;
1048 struct i915_workarounds *w = &dev_priv->workarounds;
1049
Michel Thierrye6c1abb2014-11-26 14:21:02 +00001050 if (WARN_ON_ONCE(w->count == 0))
Michel Thierry771b9a52014-11-11 16:47:33 +00001051 return 0;
1052
1053 ring->gpu_caches_dirty = true;
John Harrison4866d722015-05-29 17:43:55 +01001054 ret = logical_ring_flush_all_caches(req);
Michel Thierry771b9a52014-11-11 16:47:33 +00001055 if (ret)
1056 return ret;
1057
John Harrison4d616a22015-05-29 17:44:08 +01001058 ret = intel_logical_ring_begin(req, w->count * 2 + 2);
Michel Thierry771b9a52014-11-11 16:47:33 +00001059 if (ret)
1060 return ret;
1061
1062 intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(w->count));
1063 for (i = 0; i < w->count; i++) {
1064 intel_logical_ring_emit(ringbuf, w->reg[i].addr);
1065 intel_logical_ring_emit(ringbuf, w->reg[i].value);
1066 }
1067 intel_logical_ring_emit(ringbuf, MI_NOOP);
1068
1069 intel_logical_ring_advance(ringbuf);
1070
1071 ring->gpu_caches_dirty = true;
John Harrison4866d722015-05-29 17:43:55 +01001072 ret = logical_ring_flush_all_caches(req);
Michel Thierry771b9a52014-11-11 16:47:33 +00001073 if (ret)
1074 return ret;
1075
1076 return 0;
1077}
1078
Arun Siluvery17ee9502015-06-19 19:07:01 +01001079#define wa_ctx_emit(batch, cmd) \
1080 do { \
1081 if (WARN_ON(index >= (PAGE_SIZE / sizeof(uint32_t)))) { \
1082 return -ENOSPC; \
1083 } \
1084 batch[index++] = (cmd); \
1085 } while (0)
1086
Arun Siluvery9e000842015-07-03 14:27:31 +01001087
1088/*
1089 * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
1090 * PIPE_CONTROL instruction. This is required for the flush to happen correctly
1091 * but there is a slight complication as this is applied in WA batch where the
1092 * values are only initialized once so we cannot take register value at the
1093 * beginning and reuse it further; hence we save its value to memory, upload a
1094 * constant value with bit21 set and then we restore it back with the saved value.
1095 * To simplify the WA, a constant value is formed by using the default value
1096 * of this register. This shouldn't be a problem because we are only modifying
1097 * it for a short period and this batch in non-premptible. We can ofcourse
1098 * use additional instructions that read the actual value of the register
1099 * at that time and set our bit of interest but it makes the WA complicated.
1100 *
1101 * This WA is also required for Gen9 so extracting as a function avoids
1102 * code duplication.
1103 */
1104static inline int gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *ring,
1105 uint32_t *const batch,
1106 uint32_t index)
1107{
1108 uint32_t l3sqc4_flush = (0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES);
1109
1110 wa_ctx_emit(batch, (MI_STORE_REGISTER_MEM_GEN8(1) |
1111 MI_SRM_LRM_GLOBAL_GTT));
1112 wa_ctx_emit(batch, GEN8_L3SQCREG4);
1113 wa_ctx_emit(batch, ring->scratch.gtt_offset + 256);
1114 wa_ctx_emit(batch, 0);
1115
1116 wa_ctx_emit(batch, MI_LOAD_REGISTER_IMM(1));
1117 wa_ctx_emit(batch, GEN8_L3SQCREG4);
1118 wa_ctx_emit(batch, l3sqc4_flush);
1119
1120 wa_ctx_emit(batch, GFX_OP_PIPE_CONTROL(6));
1121 wa_ctx_emit(batch, (PIPE_CONTROL_CS_STALL |
1122 PIPE_CONTROL_DC_FLUSH_ENABLE));
1123 wa_ctx_emit(batch, 0);
1124 wa_ctx_emit(batch, 0);
1125 wa_ctx_emit(batch, 0);
1126 wa_ctx_emit(batch, 0);
1127
1128 wa_ctx_emit(batch, (MI_LOAD_REGISTER_MEM_GEN8(1) |
1129 MI_SRM_LRM_GLOBAL_GTT));
1130 wa_ctx_emit(batch, GEN8_L3SQCREG4);
1131 wa_ctx_emit(batch, ring->scratch.gtt_offset + 256);
1132 wa_ctx_emit(batch, 0);
1133
1134 return index;
1135}
1136
Arun Siluvery17ee9502015-06-19 19:07:01 +01001137static inline uint32_t wa_ctx_start(struct i915_wa_ctx_bb *wa_ctx,
1138 uint32_t offset,
1139 uint32_t start_alignment)
1140{
1141 return wa_ctx->offset = ALIGN(offset, start_alignment);
1142}
1143
1144static inline int wa_ctx_end(struct i915_wa_ctx_bb *wa_ctx,
1145 uint32_t offset,
1146 uint32_t size_alignment)
1147{
1148 wa_ctx->size = offset - wa_ctx->offset;
1149
1150 WARN(wa_ctx->size % size_alignment,
1151 "wa_ctx_bb failed sanity checks: size %d is not aligned to %d\n",
1152 wa_ctx->size, size_alignment);
1153 return 0;
1154}
1155
1156/**
1157 * gen8_init_indirectctx_bb() - initialize indirect ctx batch with WA
1158 *
1159 * @ring: only applicable for RCS
1160 * @wa_ctx: structure representing wa_ctx
1161 * offset: specifies start of the batch, should be cache-aligned. This is updated
1162 * with the offset value received as input.
1163 * size: size of the batch in DWORDS but HW expects in terms of cachelines
1164 * @batch: page in which WA are loaded
1165 * @offset: This field specifies the start of the batch, it should be
1166 * cache-aligned otherwise it is adjusted accordingly.
1167 * Typically we only have one indirect_ctx and per_ctx batch buffer which are
1168 * initialized at the beginning and shared across all contexts but this field
1169 * helps us to have multiple batches at different offsets and select them based
1170 * on a criteria. At the moment this batch always start at the beginning of the page
1171 * and at this point we don't have multiple wa_ctx batch buffers.
1172 *
1173 * The number of WA applied are not known at the beginning; we use this field
1174 * to return the no of DWORDS written.
Arun Siluvery4d78c8d2015-06-23 15:50:43 +01001175 *
Arun Siluvery17ee9502015-06-19 19:07:01 +01001176 * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
1177 * so it adds NOOPs as padding to make it cacheline aligned.
1178 * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
1179 * makes a complete batch buffer.
1180 *
1181 * Return: non-zero if we exceed the PAGE_SIZE limit.
1182 */
1183
1184static int gen8_init_indirectctx_bb(struct intel_engine_cs *ring,
1185 struct i915_wa_ctx_bb *wa_ctx,
1186 uint32_t *const batch,
1187 uint32_t *offset)
1188{
Arun Siluvery0160f052015-06-23 15:46:57 +01001189 uint32_t scratch_addr;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001190 uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
1191
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001192 /* WaDisableCtxRestoreArbitration:bdw,chv */
1193 wa_ctx_emit(batch, MI_ARB_ON_OFF | MI_ARB_DISABLE);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001194
Arun Siluveryc82435b2015-06-19 18:37:13 +01001195 /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
1196 if (IS_BROADWELL(ring->dev)) {
Arun Siluvery9e000842015-07-03 14:27:31 +01001197 index = gen8_emit_flush_coherentl3_wa(ring, batch, index);
1198 if (index < 0)
1199 return index;
Arun Siluveryc82435b2015-06-19 18:37:13 +01001200 }
1201
Arun Siluvery0160f052015-06-23 15:46:57 +01001202 /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
1203 /* Actual scratch location is at 128 bytes offset */
1204 scratch_addr = ring->scratch.gtt_offset + 2*CACHELINE_BYTES;
1205
1206 wa_ctx_emit(batch, GFX_OP_PIPE_CONTROL(6));
1207 wa_ctx_emit(batch, (PIPE_CONTROL_FLUSH_L3 |
1208 PIPE_CONTROL_GLOBAL_GTT_IVB |
1209 PIPE_CONTROL_CS_STALL |
1210 PIPE_CONTROL_QW_WRITE));
1211 wa_ctx_emit(batch, scratch_addr);
1212 wa_ctx_emit(batch, 0);
1213 wa_ctx_emit(batch, 0);
1214 wa_ctx_emit(batch, 0);
1215
Arun Siluvery17ee9502015-06-19 19:07:01 +01001216 /* Pad to end of cacheline */
1217 while (index % CACHELINE_DWORDS)
1218 wa_ctx_emit(batch, MI_NOOP);
1219
1220 /*
1221 * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
1222 * execution depends on the length specified in terms of cache lines
1223 * in the register CTX_RCS_INDIRECT_CTX
1224 */
1225
1226 return wa_ctx_end(wa_ctx, *offset = index, CACHELINE_DWORDS);
1227}
1228
1229/**
1230 * gen8_init_perctx_bb() - initialize per ctx batch with WA
1231 *
1232 * @ring: only applicable for RCS
1233 * @wa_ctx: structure representing wa_ctx
1234 * offset: specifies start of the batch, should be cache-aligned.
1235 * size: size of the batch in DWORDS but HW expects in terms of cachelines
Arun Siluvery4d78c8d2015-06-23 15:50:43 +01001236 * @batch: page in which WA are loaded
Arun Siluvery17ee9502015-06-19 19:07:01 +01001237 * @offset: This field specifies the start of this batch.
1238 * This batch is started immediately after indirect_ctx batch. Since we ensure
1239 * that indirect_ctx ends on a cacheline this batch is aligned automatically.
1240 *
1241 * The number of DWORDS written are returned using this field.
1242 *
1243 * This batch is terminated with MI_BATCH_BUFFER_END and so we need not add padding
1244 * to align it with cacheline as padding after MI_BATCH_BUFFER_END is redundant.
1245 */
1246static int gen8_init_perctx_bb(struct intel_engine_cs *ring,
1247 struct i915_wa_ctx_bb *wa_ctx,
1248 uint32_t *const batch,
1249 uint32_t *offset)
1250{
1251 uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
1252
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001253 /* WaDisableCtxRestoreArbitration:bdw,chv */
1254 wa_ctx_emit(batch, MI_ARB_ON_OFF | MI_ARB_ENABLE);
1255
Arun Siluvery17ee9502015-06-19 19:07:01 +01001256 wa_ctx_emit(batch, MI_BATCH_BUFFER_END);
1257
1258 return wa_ctx_end(wa_ctx, *offset = index, 1);
1259}
1260
1261static int lrc_setup_wa_ctx_obj(struct intel_engine_cs *ring, u32 size)
1262{
1263 int ret;
1264
1265 ring->wa_ctx.obj = i915_gem_alloc_object(ring->dev, PAGE_ALIGN(size));
1266 if (!ring->wa_ctx.obj) {
1267 DRM_DEBUG_DRIVER("alloc LRC WA ctx backing obj failed.\n");
1268 return -ENOMEM;
1269 }
1270
1271 ret = i915_gem_obj_ggtt_pin(ring->wa_ctx.obj, PAGE_SIZE, 0);
1272 if (ret) {
1273 DRM_DEBUG_DRIVER("pin LRC WA ctx backing obj failed: %d\n",
1274 ret);
1275 drm_gem_object_unreference(&ring->wa_ctx.obj->base);
1276 return ret;
1277 }
1278
1279 return 0;
1280}
1281
1282static void lrc_destroy_wa_ctx_obj(struct intel_engine_cs *ring)
1283{
1284 if (ring->wa_ctx.obj) {
1285 i915_gem_object_ggtt_unpin(ring->wa_ctx.obj);
1286 drm_gem_object_unreference(&ring->wa_ctx.obj->base);
1287 ring->wa_ctx.obj = NULL;
1288 }
1289}
1290
1291static int intel_init_workaround_bb(struct intel_engine_cs *ring)
1292{
1293 int ret;
1294 uint32_t *batch;
1295 uint32_t offset;
1296 struct page *page;
1297 struct i915_ctx_workarounds *wa_ctx = &ring->wa_ctx;
1298
1299 WARN_ON(ring->id != RCS);
1300
Arun Siluvery5e60d792015-06-23 15:50:44 +01001301 /* update this when WA for higher Gen are added */
1302 if (WARN(INTEL_INFO(ring->dev)->gen > 8,
1303 "WA batch buffer is not initialized for Gen%d\n",
1304 INTEL_INFO(ring->dev)->gen))
1305 return 0;
1306
Arun Siluveryc4db7592015-06-19 18:37:11 +01001307 /* some WA perform writes to scratch page, ensure it is valid */
1308 if (ring->scratch.obj == NULL) {
1309 DRM_ERROR("scratch page not allocated for %s\n", ring->name);
1310 return -EINVAL;
1311 }
1312
Arun Siluvery17ee9502015-06-19 19:07:01 +01001313 ret = lrc_setup_wa_ctx_obj(ring, PAGE_SIZE);
1314 if (ret) {
1315 DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
1316 return ret;
1317 }
1318
1319 page = i915_gem_object_get_page(wa_ctx->obj, 0);
1320 batch = kmap_atomic(page);
1321 offset = 0;
1322
1323 if (INTEL_INFO(ring->dev)->gen == 8) {
1324 ret = gen8_init_indirectctx_bb(ring,
1325 &wa_ctx->indirect_ctx,
1326 batch,
1327 &offset);
1328 if (ret)
1329 goto out;
1330
1331 ret = gen8_init_perctx_bb(ring,
1332 &wa_ctx->per_ctx,
1333 batch,
1334 &offset);
1335 if (ret)
1336 goto out;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001337 }
1338
1339out:
1340 kunmap_atomic(batch);
1341 if (ret)
1342 lrc_destroy_wa_ctx_obj(ring);
1343
1344 return ret;
1345}
1346
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001347static int gen8_init_common_ring(struct intel_engine_cs *ring)
1348{
1349 struct drm_device *dev = ring->dev;
1350 struct drm_i915_private *dev_priv = dev->dev_private;
1351
Oscar Mateo73d477f2014-07-24 17:04:31 +01001352 I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
1353 I915_WRITE(RING_HWSTAM(ring->mmio_base), 0xffffffff);
1354
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001355 I915_WRITE(RING_MODE_GEN7(ring),
1356 _MASKED_BIT_DISABLE(GFX_REPLAY_MODE) |
1357 _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
1358 POSTING_READ(RING_MODE_GEN7(ring));
Thomas Danielc0a03a22015-01-09 11:09:37 +00001359 ring->next_context_status_buffer = 0;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001360 DRM_DEBUG_DRIVER("Execlists enabled for %s\n", ring->name);
1361
1362 memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
1363
1364 return 0;
1365}
1366
1367static int gen8_init_render_ring(struct intel_engine_cs *ring)
1368{
1369 struct drm_device *dev = ring->dev;
1370 struct drm_i915_private *dev_priv = dev->dev_private;
1371 int ret;
1372
1373 ret = gen8_init_common_ring(ring);
1374 if (ret)
1375 return ret;
1376
1377 /* We need to disable the AsyncFlip performance optimisations in order
1378 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1379 * programmed to '1' on all products.
1380 *
1381 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
1382 */
1383 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1384
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001385 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1386
Michel Thierry771b9a52014-11-11 16:47:33 +00001387 return init_workarounds_ring(ring);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001388}
1389
Damien Lespiau82ef8222015-02-09 19:33:08 +00001390static int gen9_init_render_ring(struct intel_engine_cs *ring)
1391{
1392 int ret;
1393
1394 ret = gen8_init_common_ring(ring);
1395 if (ret)
1396 return ret;
1397
1398 return init_workarounds_ring(ring);
1399}
1400
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001401static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request *req)
1402{
1403 struct i915_hw_ppgtt *ppgtt = req->ctx->ppgtt;
1404 struct intel_engine_cs *ring = req->ring;
1405 struct intel_ringbuffer *ringbuf = req->ringbuf;
1406 const int num_lri_cmds = GEN8_LEGACY_PDPES * 2;
1407 int i, ret;
1408
1409 ret = intel_logical_ring_begin(req, num_lri_cmds * 2 + 2);
1410 if (ret)
1411 return ret;
1412
1413 intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(num_lri_cmds));
1414 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
1415 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
1416
1417 intel_logical_ring_emit(ringbuf, GEN8_RING_PDP_UDW(ring, i));
1418 intel_logical_ring_emit(ringbuf, upper_32_bits(pd_daddr));
1419 intel_logical_ring_emit(ringbuf, GEN8_RING_PDP_LDW(ring, i));
1420 intel_logical_ring_emit(ringbuf, lower_32_bits(pd_daddr));
1421 }
1422
1423 intel_logical_ring_emit(ringbuf, MI_NOOP);
1424 intel_logical_ring_advance(ringbuf);
1425
1426 return 0;
1427}
1428
John Harrisonbe795fc2015-05-29 17:44:03 +01001429static int gen8_emit_bb_start(struct drm_i915_gem_request *req,
John Harrison8e004ef2015-02-13 11:48:10 +00001430 u64 offset, unsigned dispatch_flags)
Oscar Mateo15648582014-07-24 17:04:32 +01001431{
John Harrisonbe795fc2015-05-29 17:44:03 +01001432 struct intel_ringbuffer *ringbuf = req->ringbuf;
John Harrison8e004ef2015-02-13 11:48:10 +00001433 bool ppgtt = !(dispatch_flags & I915_DISPATCH_SECURE);
Oscar Mateo15648582014-07-24 17:04:32 +01001434 int ret;
1435
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001436 /* Don't rely in hw updating PDPs, specially in lite-restore.
1437 * Ideally, we should set Force PD Restore in ctx descriptor,
1438 * but we can't. Force Restore would be a second option, but
1439 * it is unsafe in case of lite-restore (because the ctx is
1440 * not idle). */
1441 if (req->ctx->ppgtt &&
1442 (intel_ring_flag(req->ring) & req->ctx->ppgtt->pd_dirty_rings)) {
1443 ret = intel_logical_ring_emit_pdps(req);
1444 if (ret)
1445 return ret;
1446
1447 req->ctx->ppgtt->pd_dirty_rings &= ~intel_ring_flag(req->ring);
1448 }
1449
John Harrison4d616a22015-05-29 17:44:08 +01001450 ret = intel_logical_ring_begin(req, 4);
Oscar Mateo15648582014-07-24 17:04:32 +01001451 if (ret)
1452 return ret;
1453
1454 /* FIXME(BDW): Address space and security selectors. */
Abdiel Janulgue69225282015-06-16 13:39:42 +03001455 intel_logical_ring_emit(ringbuf, MI_BATCH_BUFFER_START_GEN8 |
1456 (ppgtt<<8) |
1457 (dispatch_flags & I915_DISPATCH_RS ?
1458 MI_BATCH_RESOURCE_STREAMER : 0));
Oscar Mateo15648582014-07-24 17:04:32 +01001459 intel_logical_ring_emit(ringbuf, lower_32_bits(offset));
1460 intel_logical_ring_emit(ringbuf, upper_32_bits(offset));
1461 intel_logical_ring_emit(ringbuf, MI_NOOP);
1462 intel_logical_ring_advance(ringbuf);
1463
1464 return 0;
1465}
1466
Oscar Mateo73d477f2014-07-24 17:04:31 +01001467static bool gen8_logical_ring_get_irq(struct intel_engine_cs *ring)
1468{
1469 struct drm_device *dev = ring->dev;
1470 struct drm_i915_private *dev_priv = dev->dev_private;
1471 unsigned long flags;
1472
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001473 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Oscar Mateo73d477f2014-07-24 17:04:31 +01001474 return false;
1475
1476 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1477 if (ring->irq_refcount++ == 0) {
1478 I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
1479 POSTING_READ(RING_IMR(ring->mmio_base));
1480 }
1481 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1482
1483 return true;
1484}
1485
1486static void gen8_logical_ring_put_irq(struct intel_engine_cs *ring)
1487{
1488 struct drm_device *dev = ring->dev;
1489 struct drm_i915_private *dev_priv = dev->dev_private;
1490 unsigned long flags;
1491
1492 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1493 if (--ring->irq_refcount == 0) {
1494 I915_WRITE_IMR(ring, ~ring->irq_keep_mask);
1495 POSTING_READ(RING_IMR(ring->mmio_base));
1496 }
1497 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1498}
1499
John Harrison7deb4d32015-05-29 17:43:59 +01001500static int gen8_emit_flush(struct drm_i915_gem_request *request,
Oscar Mateo47122742014-07-24 17:04:28 +01001501 u32 invalidate_domains,
1502 u32 unused)
1503{
John Harrison7deb4d32015-05-29 17:43:59 +01001504 struct intel_ringbuffer *ringbuf = request->ringbuf;
Oscar Mateo47122742014-07-24 17:04:28 +01001505 struct intel_engine_cs *ring = ringbuf->ring;
1506 struct drm_device *dev = ring->dev;
1507 struct drm_i915_private *dev_priv = dev->dev_private;
1508 uint32_t cmd;
1509 int ret;
1510
John Harrison4d616a22015-05-29 17:44:08 +01001511 ret = intel_logical_ring_begin(request, 4);
Oscar Mateo47122742014-07-24 17:04:28 +01001512 if (ret)
1513 return ret;
1514
1515 cmd = MI_FLUSH_DW + 1;
1516
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001517 /* We always require a command barrier so that subsequent
1518 * commands, such as breadcrumb interrupts, are strictly ordered
1519 * wrt the contents of the write cache being flushed to memory
1520 * (and thus being coherent from the CPU).
1521 */
1522 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1523
1524 if (invalidate_domains & I915_GEM_GPU_DOMAINS) {
1525 cmd |= MI_INVALIDATE_TLB;
1526 if (ring == &dev_priv->ring[VCS])
1527 cmd |= MI_INVALIDATE_BSD;
Oscar Mateo47122742014-07-24 17:04:28 +01001528 }
1529
1530 intel_logical_ring_emit(ringbuf, cmd);
1531 intel_logical_ring_emit(ringbuf,
1532 I915_GEM_HWS_SCRATCH_ADDR |
1533 MI_FLUSH_DW_USE_GTT);
1534 intel_logical_ring_emit(ringbuf, 0); /* upper addr */
1535 intel_logical_ring_emit(ringbuf, 0); /* value */
1536 intel_logical_ring_advance(ringbuf);
1537
1538 return 0;
1539}
1540
John Harrison7deb4d32015-05-29 17:43:59 +01001541static int gen8_emit_flush_render(struct drm_i915_gem_request *request,
Oscar Mateo47122742014-07-24 17:04:28 +01001542 u32 invalidate_domains,
1543 u32 flush_domains)
1544{
John Harrison7deb4d32015-05-29 17:43:59 +01001545 struct intel_ringbuffer *ringbuf = request->ringbuf;
Oscar Mateo47122742014-07-24 17:04:28 +01001546 struct intel_engine_cs *ring = ringbuf->ring;
1547 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Imre Deak9647ff32015-01-25 13:27:11 -08001548 bool vf_flush_wa;
Oscar Mateo47122742014-07-24 17:04:28 +01001549 u32 flags = 0;
1550 int ret;
1551
1552 flags |= PIPE_CONTROL_CS_STALL;
1553
1554 if (flush_domains) {
1555 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
1556 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
1557 }
1558
1559 if (invalidate_domains) {
1560 flags |= PIPE_CONTROL_TLB_INVALIDATE;
1561 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
1562 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
1563 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
1564 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
1565 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
1566 flags |= PIPE_CONTROL_QW_WRITE;
1567 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
1568 }
1569
Imre Deak9647ff32015-01-25 13:27:11 -08001570 /*
1571 * On GEN9+ Before VF_CACHE_INVALIDATE we need to emit a NULL pipe
1572 * control.
1573 */
1574 vf_flush_wa = INTEL_INFO(ring->dev)->gen >= 9 &&
1575 flags & PIPE_CONTROL_VF_CACHE_INVALIDATE;
1576
John Harrison4d616a22015-05-29 17:44:08 +01001577 ret = intel_logical_ring_begin(request, vf_flush_wa ? 12 : 6);
Oscar Mateo47122742014-07-24 17:04:28 +01001578 if (ret)
1579 return ret;
1580
Imre Deak9647ff32015-01-25 13:27:11 -08001581 if (vf_flush_wa) {
1582 intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
1583 intel_logical_ring_emit(ringbuf, 0);
1584 intel_logical_ring_emit(ringbuf, 0);
1585 intel_logical_ring_emit(ringbuf, 0);
1586 intel_logical_ring_emit(ringbuf, 0);
1587 intel_logical_ring_emit(ringbuf, 0);
1588 }
1589
Oscar Mateo47122742014-07-24 17:04:28 +01001590 intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
1591 intel_logical_ring_emit(ringbuf, flags);
1592 intel_logical_ring_emit(ringbuf, scratch_addr);
1593 intel_logical_ring_emit(ringbuf, 0);
1594 intel_logical_ring_emit(ringbuf, 0);
1595 intel_logical_ring_emit(ringbuf, 0);
1596 intel_logical_ring_advance(ringbuf);
1597
1598 return 0;
1599}
1600
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001601static u32 gen8_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
1602{
1603 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1604}
1605
1606static void gen8_set_seqno(struct intel_engine_cs *ring, u32 seqno)
1607{
1608 intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
1609}
1610
John Harrisonc4e76632015-05-29 17:44:01 +01001611static int gen8_emit_request(struct drm_i915_gem_request *request)
Oscar Mateo4da46e12014-07-24 17:04:27 +01001612{
John Harrisonc4e76632015-05-29 17:44:01 +01001613 struct intel_ringbuffer *ringbuf = request->ringbuf;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001614 struct intel_engine_cs *ring = ringbuf->ring;
1615 u32 cmd;
1616 int ret;
1617
Michel Thierry53292cd2015-04-15 18:11:33 +01001618 /*
1619 * Reserve space for 2 NOOPs at the end of each request to be
1620 * used as a workaround for not being allowed to do lite
1621 * restore with HEAD==TAIL (WaIdleLiteRestore).
1622 */
John Harrison4d616a22015-05-29 17:44:08 +01001623 ret = intel_logical_ring_begin(request, 8);
Oscar Mateo4da46e12014-07-24 17:04:27 +01001624 if (ret)
1625 return ret;
1626
Ville Syrjälä8edfbb82014-11-14 18:16:56 +02001627 cmd = MI_STORE_DWORD_IMM_GEN4;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001628 cmd |= MI_GLOBAL_GTT;
1629
1630 intel_logical_ring_emit(ringbuf, cmd);
1631 intel_logical_ring_emit(ringbuf,
1632 (ring->status_page.gfx_addr +
1633 (I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT)));
1634 intel_logical_ring_emit(ringbuf, 0);
John Harrisonc4e76632015-05-29 17:44:01 +01001635 intel_logical_ring_emit(ringbuf, i915_gem_request_get_seqno(request));
Oscar Mateo4da46e12014-07-24 17:04:27 +01001636 intel_logical_ring_emit(ringbuf, MI_USER_INTERRUPT);
1637 intel_logical_ring_emit(ringbuf, MI_NOOP);
John Harrisonae707972015-05-29 17:44:14 +01001638 intel_logical_ring_advance_and_submit(request);
Oscar Mateo4da46e12014-07-24 17:04:27 +01001639
Michel Thierry53292cd2015-04-15 18:11:33 +01001640 /*
1641 * Here we add two extra NOOPs as padding to avoid
1642 * lite restore of a context with HEAD==TAIL.
1643 */
1644 intel_logical_ring_emit(ringbuf, MI_NOOP);
1645 intel_logical_ring_emit(ringbuf, MI_NOOP);
1646 intel_logical_ring_advance(ringbuf);
1647
Oscar Mateo4da46e12014-07-24 17:04:27 +01001648 return 0;
1649}
1650
John Harrisonbe013632015-05-29 17:43:45 +01001651static int intel_lr_context_render_state_init(struct drm_i915_gem_request *req)
Damien Lespiaucef437a2015-02-10 19:32:19 +00001652{
Damien Lespiaucef437a2015-02-10 19:32:19 +00001653 struct render_state so;
Damien Lespiaucef437a2015-02-10 19:32:19 +00001654 int ret;
1655
John Harrisonbe013632015-05-29 17:43:45 +01001656 ret = i915_gem_render_state_prepare(req->ring, &so);
Damien Lespiaucef437a2015-02-10 19:32:19 +00001657 if (ret)
1658 return ret;
1659
1660 if (so.rodata == NULL)
1661 return 0;
1662
John Harrisonbe795fc2015-05-29 17:44:03 +01001663 ret = req->ring->emit_bb_start(req, so.ggtt_offset,
John Harrisonbe013632015-05-29 17:43:45 +01001664 I915_DISPATCH_SECURE);
Damien Lespiaucef437a2015-02-10 19:32:19 +00001665 if (ret)
1666 goto out;
1667
John Harrisonb2af0372015-05-29 17:43:50 +01001668 i915_vma_move_to_active(i915_gem_obj_to_ggtt(so.obj), req);
Damien Lespiaucef437a2015-02-10 19:32:19 +00001669
Damien Lespiaucef437a2015-02-10 19:32:19 +00001670out:
1671 i915_gem_render_state_fini(&so);
1672 return ret;
1673}
1674
John Harrison87531812015-05-29 17:43:44 +01001675static int gen8_init_rcs_context(struct drm_i915_gem_request *req)
Thomas Daniele7778be2014-12-02 12:50:48 +00001676{
1677 int ret;
1678
John Harrisone2be4fa2015-05-29 17:43:54 +01001679 ret = intel_logical_ring_workarounds_emit(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001680 if (ret)
1681 return ret;
1682
John Harrisonbe013632015-05-29 17:43:45 +01001683 return intel_lr_context_render_state_init(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001684}
1685
Oscar Mateo73e4d072014-07-24 17:04:48 +01001686/**
1687 * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
1688 *
1689 * @ring: Engine Command Streamer.
1690 *
1691 */
Oscar Mateo454afeb2014-07-24 17:04:22 +01001692void intel_logical_ring_cleanup(struct intel_engine_cs *ring)
1693{
John Harrison6402c332014-10-31 12:00:26 +00001694 struct drm_i915_private *dev_priv;
Oscar Mateo9832b9d2014-07-24 17:04:30 +01001695
Oscar Mateo48d82382014-07-24 17:04:23 +01001696 if (!intel_ring_initialized(ring))
1697 return;
1698
John Harrison6402c332014-10-31 12:00:26 +00001699 dev_priv = ring->dev->dev_private;
1700
Oscar Mateo9832b9d2014-07-24 17:04:30 +01001701 intel_logical_ring_stop(ring);
1702 WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
Oscar Mateo48d82382014-07-24 17:04:23 +01001703
1704 if (ring->cleanup)
1705 ring->cleanup(ring);
1706
1707 i915_cmd_parser_fini_ring(ring);
Chris Wilson06fbca72015-04-07 16:20:36 +01001708 i915_gem_batch_pool_fini(&ring->batch_pool);
Oscar Mateo48d82382014-07-24 17:04:23 +01001709
1710 if (ring->status_page.obj) {
1711 kunmap(sg_page(ring->status_page.obj->pages->sgl));
1712 ring->status_page.obj = NULL;
1713 }
Arun Siluvery17ee9502015-06-19 19:07:01 +01001714
1715 lrc_destroy_wa_ctx_obj(ring);
Oscar Mateo454afeb2014-07-24 17:04:22 +01001716}
1717
1718static int logical_ring_init(struct drm_device *dev, struct intel_engine_cs *ring)
1719{
Oscar Mateo48d82382014-07-24 17:04:23 +01001720 int ret;
Oscar Mateo48d82382014-07-24 17:04:23 +01001721
1722 /* Intentionally left blank. */
1723 ring->buffer = NULL;
1724
1725 ring->dev = dev;
1726 INIT_LIST_HEAD(&ring->active_list);
1727 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson06fbca72015-04-07 16:20:36 +01001728 i915_gem_batch_pool_init(dev, &ring->batch_pool);
Oscar Mateo48d82382014-07-24 17:04:23 +01001729 init_waitqueue_head(&ring->irq_queue);
1730
Michel Thierryacdd8842014-07-24 17:04:38 +01001731 INIT_LIST_HEAD(&ring->execlist_queue);
Thomas Danielc86ee3a92014-11-13 10:27:05 +00001732 INIT_LIST_HEAD(&ring->execlist_retired_req_list);
Michel Thierryacdd8842014-07-24 17:04:38 +01001733 spin_lock_init(&ring->execlist_lock);
1734
Oscar Mateo48d82382014-07-24 17:04:23 +01001735 ret = i915_cmd_parser_init_ring(ring);
1736 if (ret)
1737 return ret;
1738
Oscar Mateo564ddb22014-08-21 11:40:54 +01001739 ret = intel_lr_context_deferred_create(ring->default_context, ring);
1740
1741 return ret;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001742}
1743
1744static int logical_render_ring_init(struct drm_device *dev)
1745{
1746 struct drm_i915_private *dev_priv = dev->dev_private;
1747 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Daniel Vetter99be1df2014-11-20 00:33:06 +01001748 int ret;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001749
1750 ring->name = "render ring";
1751 ring->id = RCS;
1752 ring->mmio_base = RENDER_RING_BASE;
1753 ring->irq_enable_mask =
1754 GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001755 ring->irq_keep_mask =
1756 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
1757 if (HAS_L3_DPF(dev))
1758 ring->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001759
Damien Lespiau82ef8222015-02-09 19:33:08 +00001760 if (INTEL_INFO(dev)->gen >= 9)
1761 ring->init_hw = gen9_init_render_ring;
1762 else
1763 ring->init_hw = gen8_init_render_ring;
Thomas Daniele7778be2014-12-02 12:50:48 +00001764 ring->init_context = gen8_init_rcs_context;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001765 ring->cleanup = intel_fini_pipe_control;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001766 ring->get_seqno = gen8_get_seqno;
1767 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001768 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001769 ring->emit_flush = gen8_emit_flush_render;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001770 ring->irq_get = gen8_logical_ring_get_irq;
1771 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001772 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001773
Daniel Vetter99be1df2014-11-20 00:33:06 +01001774 ring->dev = dev;
Arun Siluveryc4db7592015-06-19 18:37:11 +01001775
1776 ret = intel_init_pipe_control(ring);
Daniel Vetter99be1df2014-11-20 00:33:06 +01001777 if (ret)
1778 return ret;
1779
Arun Siluvery17ee9502015-06-19 19:07:01 +01001780 ret = intel_init_workaround_bb(ring);
1781 if (ret) {
1782 /*
1783 * We continue even if we fail to initialize WA batch
1784 * because we only expect rare glitches but nothing
1785 * critical to prevent us from using GPU
1786 */
1787 DRM_ERROR("WA batch buffer initialization failed: %d\n",
1788 ret);
1789 }
1790
Arun Siluveryc4db7592015-06-19 18:37:11 +01001791 ret = logical_ring_init(dev, ring);
1792 if (ret) {
Arun Siluvery17ee9502015-06-19 19:07:01 +01001793 lrc_destroy_wa_ctx_obj(ring);
Arun Siluveryc4db7592015-06-19 18:37:11 +01001794 }
Arun Siluvery17ee9502015-06-19 19:07:01 +01001795
1796 return ret;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001797}
1798
1799static int logical_bsd_ring_init(struct drm_device *dev)
1800{
1801 struct drm_i915_private *dev_priv = dev->dev_private;
1802 struct intel_engine_cs *ring = &dev_priv->ring[VCS];
1803
1804 ring->name = "bsd ring";
1805 ring->id = VCS;
1806 ring->mmio_base = GEN6_BSD_RING_BASE;
1807 ring->irq_enable_mask =
1808 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001809 ring->irq_keep_mask =
1810 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001811
Daniel Vetterecfe00d2014-11-20 00:33:04 +01001812 ring->init_hw = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001813 ring->get_seqno = gen8_get_seqno;
1814 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001815 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001816 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001817 ring->irq_get = gen8_logical_ring_get_irq;
1818 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001819 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001820
Oscar Mateo454afeb2014-07-24 17:04:22 +01001821 return logical_ring_init(dev, ring);
1822}
1823
1824static int logical_bsd2_ring_init(struct drm_device *dev)
1825{
1826 struct drm_i915_private *dev_priv = dev->dev_private;
1827 struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
1828
1829 ring->name = "bds2 ring";
1830 ring->id = VCS2;
1831 ring->mmio_base = GEN8_BSD2_RING_BASE;
1832 ring->irq_enable_mask =
1833 GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001834 ring->irq_keep_mask =
1835 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001836
Daniel Vetterecfe00d2014-11-20 00:33:04 +01001837 ring->init_hw = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001838 ring->get_seqno = gen8_get_seqno;
1839 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001840 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001841 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001842 ring->irq_get = gen8_logical_ring_get_irq;
1843 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001844 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001845
Oscar Mateo454afeb2014-07-24 17:04:22 +01001846 return logical_ring_init(dev, ring);
1847}
1848
1849static int logical_blt_ring_init(struct drm_device *dev)
1850{
1851 struct drm_i915_private *dev_priv = dev->dev_private;
1852 struct intel_engine_cs *ring = &dev_priv->ring[BCS];
1853
1854 ring->name = "blitter ring";
1855 ring->id = BCS;
1856 ring->mmio_base = BLT_RING_BASE;
1857 ring->irq_enable_mask =
1858 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001859 ring->irq_keep_mask =
1860 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001861
Daniel Vetterecfe00d2014-11-20 00:33:04 +01001862 ring->init_hw = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001863 ring->get_seqno = gen8_get_seqno;
1864 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001865 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001866 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001867 ring->irq_get = gen8_logical_ring_get_irq;
1868 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001869 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001870
Oscar Mateo454afeb2014-07-24 17:04:22 +01001871 return logical_ring_init(dev, ring);
1872}
1873
1874static int logical_vebox_ring_init(struct drm_device *dev)
1875{
1876 struct drm_i915_private *dev_priv = dev->dev_private;
1877 struct intel_engine_cs *ring = &dev_priv->ring[VECS];
1878
1879 ring->name = "video enhancement ring";
1880 ring->id = VECS;
1881 ring->mmio_base = VEBOX_RING_BASE;
1882 ring->irq_enable_mask =
1883 GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001884 ring->irq_keep_mask =
1885 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001886
Daniel Vetterecfe00d2014-11-20 00:33:04 +01001887 ring->init_hw = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001888 ring->get_seqno = gen8_get_seqno;
1889 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001890 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001891 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001892 ring->irq_get = gen8_logical_ring_get_irq;
1893 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001894 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001895
Oscar Mateo454afeb2014-07-24 17:04:22 +01001896 return logical_ring_init(dev, ring);
1897}
1898
Oscar Mateo73e4d072014-07-24 17:04:48 +01001899/**
1900 * intel_logical_rings_init() - allocate, populate and init the Engine Command Streamers
1901 * @dev: DRM device.
1902 *
1903 * This function inits the engines for an Execlists submission style (the equivalent in the
1904 * legacy ringbuffer submission world would be i915_gem_init_rings). It does it only for
1905 * those engines that are present in the hardware.
1906 *
1907 * Return: non-zero if the initialization failed.
1908 */
Oscar Mateo454afeb2014-07-24 17:04:22 +01001909int intel_logical_rings_init(struct drm_device *dev)
1910{
1911 struct drm_i915_private *dev_priv = dev->dev_private;
1912 int ret;
1913
1914 ret = logical_render_ring_init(dev);
1915 if (ret)
1916 return ret;
1917
1918 if (HAS_BSD(dev)) {
1919 ret = logical_bsd_ring_init(dev);
1920 if (ret)
1921 goto cleanup_render_ring;
1922 }
1923
1924 if (HAS_BLT(dev)) {
1925 ret = logical_blt_ring_init(dev);
1926 if (ret)
1927 goto cleanup_bsd_ring;
1928 }
1929
1930 if (HAS_VEBOX(dev)) {
1931 ret = logical_vebox_ring_init(dev);
1932 if (ret)
1933 goto cleanup_blt_ring;
1934 }
1935
1936 if (HAS_BSD2(dev)) {
1937 ret = logical_bsd2_ring_init(dev);
1938 if (ret)
1939 goto cleanup_vebox_ring;
1940 }
1941
1942 ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
1943 if (ret)
1944 goto cleanup_bsd2_ring;
1945
1946 return 0;
1947
1948cleanup_bsd2_ring:
1949 intel_logical_ring_cleanup(&dev_priv->ring[VCS2]);
1950cleanup_vebox_ring:
1951 intel_logical_ring_cleanup(&dev_priv->ring[VECS]);
1952cleanup_blt_ring:
1953 intel_logical_ring_cleanup(&dev_priv->ring[BCS]);
1954cleanup_bsd_ring:
1955 intel_logical_ring_cleanup(&dev_priv->ring[VCS]);
1956cleanup_render_ring:
1957 intel_logical_ring_cleanup(&dev_priv->ring[RCS]);
1958
1959 return ret;
1960}
1961
Jeff McGee0cea6502015-02-13 10:27:56 -06001962static u32
1963make_rpcs(struct drm_device *dev)
1964{
1965 u32 rpcs = 0;
1966
1967 /*
1968 * No explicit RPCS request is needed to ensure full
1969 * slice/subslice/EU enablement prior to Gen9.
1970 */
1971 if (INTEL_INFO(dev)->gen < 9)
1972 return 0;
1973
1974 /*
1975 * Starting in Gen9, render power gating can leave
1976 * slice/subslice/EU in a partially enabled state. We
1977 * must make an explicit request through RPCS for full
1978 * enablement.
1979 */
1980 if (INTEL_INFO(dev)->has_slice_pg) {
1981 rpcs |= GEN8_RPCS_S_CNT_ENABLE;
1982 rpcs |= INTEL_INFO(dev)->slice_total <<
1983 GEN8_RPCS_S_CNT_SHIFT;
1984 rpcs |= GEN8_RPCS_ENABLE;
1985 }
1986
1987 if (INTEL_INFO(dev)->has_subslice_pg) {
1988 rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
1989 rpcs |= INTEL_INFO(dev)->subslice_per_slice <<
1990 GEN8_RPCS_SS_CNT_SHIFT;
1991 rpcs |= GEN8_RPCS_ENABLE;
1992 }
1993
1994 if (INTEL_INFO(dev)->has_eu_pg) {
1995 rpcs |= INTEL_INFO(dev)->eu_per_subslice <<
1996 GEN8_RPCS_EU_MIN_SHIFT;
1997 rpcs |= INTEL_INFO(dev)->eu_per_subslice <<
1998 GEN8_RPCS_EU_MAX_SHIFT;
1999 rpcs |= GEN8_RPCS_ENABLE;
2000 }
2001
2002 return rpcs;
2003}
2004
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002005static int
2006populate_lr_context(struct intel_context *ctx, struct drm_i915_gem_object *ctx_obj,
2007 struct intel_engine_cs *ring, struct intel_ringbuffer *ringbuf)
2008{
Thomas Daniel2d965532014-08-19 10:13:36 +01002009 struct drm_device *dev = ring->dev;
2010 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterae6c4802014-08-06 15:04:53 +02002011 struct i915_hw_ppgtt *ppgtt = ctx->ppgtt;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002012 struct page *page;
2013 uint32_t *reg_state;
2014 int ret;
2015
Thomas Daniel2d965532014-08-19 10:13:36 +01002016 if (!ppgtt)
2017 ppgtt = dev_priv->mm.aliasing_ppgtt;
2018
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002019 ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
2020 if (ret) {
2021 DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
2022 return ret;
2023 }
2024
2025 ret = i915_gem_object_get_pages(ctx_obj);
2026 if (ret) {
2027 DRM_DEBUG_DRIVER("Could not get object pages\n");
2028 return ret;
2029 }
2030
2031 i915_gem_object_pin_pages(ctx_obj);
2032
2033 /* The second page of the context object contains some fields which must
2034 * be set up prior to the first execution. */
2035 page = i915_gem_object_get_page(ctx_obj, 1);
2036 reg_state = kmap_atomic(page);
2037
2038 /* A context is actually a big batch buffer with several MI_LOAD_REGISTER_IMM
2039 * commands followed by (reg, value) pairs. The values we are setting here are
2040 * only for the first context restore: on a subsequent save, the GPU will
2041 * recreate this batchbuffer with new values (including all the missing
2042 * MI_LOAD_REGISTER_IMM commands that we are not initializing here). */
2043 if (ring->id == RCS)
2044 reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(14);
2045 else
2046 reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(11);
2047 reg_state[CTX_LRI_HEADER_0] |= MI_LRI_FORCE_POSTED;
2048 reg_state[CTX_CONTEXT_CONTROL] = RING_CONTEXT_CONTROL(ring);
2049 reg_state[CTX_CONTEXT_CONTROL+1] =
Zhi Wang5baa22c52015-02-10 17:11:36 +08002050 _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
Abdiel Janulgue69225282015-06-16 13:39:42 +03002051 CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
2052 CTX_CTRL_RS_CTX_ENABLE);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002053 reg_state[CTX_RING_HEAD] = RING_HEAD(ring->mmio_base);
2054 reg_state[CTX_RING_HEAD+1] = 0;
2055 reg_state[CTX_RING_TAIL] = RING_TAIL(ring->mmio_base);
2056 reg_state[CTX_RING_TAIL+1] = 0;
2057 reg_state[CTX_RING_BUFFER_START] = RING_START(ring->mmio_base);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002058 /* Ring buffer start address is not known until the buffer is pinned.
2059 * It is written to the context image in execlists_update_context()
2060 */
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002061 reg_state[CTX_RING_BUFFER_CONTROL] = RING_CTL(ring->mmio_base);
2062 reg_state[CTX_RING_BUFFER_CONTROL+1] =
2063 ((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES) | RING_VALID;
2064 reg_state[CTX_BB_HEAD_U] = ring->mmio_base + 0x168;
2065 reg_state[CTX_BB_HEAD_U+1] = 0;
2066 reg_state[CTX_BB_HEAD_L] = ring->mmio_base + 0x140;
2067 reg_state[CTX_BB_HEAD_L+1] = 0;
2068 reg_state[CTX_BB_STATE] = ring->mmio_base + 0x110;
2069 reg_state[CTX_BB_STATE+1] = (1<<5);
2070 reg_state[CTX_SECOND_BB_HEAD_U] = ring->mmio_base + 0x11c;
2071 reg_state[CTX_SECOND_BB_HEAD_U+1] = 0;
2072 reg_state[CTX_SECOND_BB_HEAD_L] = ring->mmio_base + 0x114;
2073 reg_state[CTX_SECOND_BB_HEAD_L+1] = 0;
2074 reg_state[CTX_SECOND_BB_STATE] = ring->mmio_base + 0x118;
2075 reg_state[CTX_SECOND_BB_STATE+1] = 0;
2076 if (ring->id == RCS) {
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002077 reg_state[CTX_BB_PER_CTX_PTR] = ring->mmio_base + 0x1c0;
2078 reg_state[CTX_BB_PER_CTX_PTR+1] = 0;
2079 reg_state[CTX_RCS_INDIRECT_CTX] = ring->mmio_base + 0x1c4;
2080 reg_state[CTX_RCS_INDIRECT_CTX+1] = 0;
2081 reg_state[CTX_RCS_INDIRECT_CTX_OFFSET] = ring->mmio_base + 0x1c8;
2082 reg_state[CTX_RCS_INDIRECT_CTX_OFFSET+1] = 0;
Arun Siluvery17ee9502015-06-19 19:07:01 +01002083 if (ring->wa_ctx.obj) {
2084 struct i915_ctx_workarounds *wa_ctx = &ring->wa_ctx;
2085 uint32_t ggtt_offset = i915_gem_obj_ggtt_offset(wa_ctx->obj);
2086
2087 reg_state[CTX_RCS_INDIRECT_CTX+1] =
2088 (ggtt_offset + wa_ctx->indirect_ctx.offset * sizeof(uint32_t)) |
2089 (wa_ctx->indirect_ctx.size / CACHELINE_DWORDS);
2090
2091 reg_state[CTX_RCS_INDIRECT_CTX_OFFSET+1] =
2092 CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT << 6;
2093
2094 reg_state[CTX_BB_PER_CTX_PTR+1] =
2095 (ggtt_offset + wa_ctx->per_ctx.offset * sizeof(uint32_t)) |
2096 0x01;
2097 }
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002098 }
2099 reg_state[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9);
2100 reg_state[CTX_LRI_HEADER_1] |= MI_LRI_FORCE_POSTED;
2101 reg_state[CTX_CTX_TIMESTAMP] = ring->mmio_base + 0x3a8;
2102 reg_state[CTX_CTX_TIMESTAMP+1] = 0;
2103 reg_state[CTX_PDP3_UDW] = GEN8_RING_PDP_UDW(ring, 3);
2104 reg_state[CTX_PDP3_LDW] = GEN8_RING_PDP_LDW(ring, 3);
2105 reg_state[CTX_PDP2_UDW] = GEN8_RING_PDP_UDW(ring, 2);
2106 reg_state[CTX_PDP2_LDW] = GEN8_RING_PDP_LDW(ring, 2);
2107 reg_state[CTX_PDP1_UDW] = GEN8_RING_PDP_UDW(ring, 1);
2108 reg_state[CTX_PDP1_LDW] = GEN8_RING_PDP_LDW(ring, 1);
2109 reg_state[CTX_PDP0_UDW] = GEN8_RING_PDP_UDW(ring, 0);
2110 reg_state[CTX_PDP0_LDW] = GEN8_RING_PDP_LDW(ring, 0);
Michel Thierryd7b26332015-04-08 12:13:34 +01002111
2112 /* With dynamic page allocation, PDPs may not be allocated at this point,
2113 * Point the unallocated PDPs to the scratch page
Michel Thierrye5815a22015-04-08 12:13:32 +01002114 */
2115 ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
2116 ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
2117 ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
2118 ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002119 if (ring->id == RCS) {
2120 reg_state[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
Jeff McGee0cea6502015-02-13 10:27:56 -06002121 reg_state[CTX_R_PWR_CLK_STATE] = GEN8_R_PWR_CLK_STATE;
2122 reg_state[CTX_R_PWR_CLK_STATE+1] = make_rpcs(dev);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002123 }
2124
2125 kunmap_atomic(reg_state);
2126
2127 ctx_obj->dirty = 1;
2128 set_page_dirty(page);
2129 i915_gem_object_unpin_pages(ctx_obj);
2130
2131 return 0;
2132}
2133
Oscar Mateo73e4d072014-07-24 17:04:48 +01002134/**
2135 * intel_lr_context_free() - free the LRC specific bits of a context
2136 * @ctx: the LR context to free.
2137 *
2138 * The real context freeing is done in i915_gem_context_free: this only
2139 * takes care of the bits that are LRC related: the per-engine backing
2140 * objects and the logical ringbuffer.
2141 */
Oscar Mateoede7d422014-07-24 17:04:12 +01002142void intel_lr_context_free(struct intel_context *ctx)
2143{
Oscar Mateo8c8579172014-07-24 17:04:14 +01002144 int i;
2145
2146 for (i = 0; i < I915_NUM_RINGS; i++) {
2147 struct drm_i915_gem_object *ctx_obj = ctx->engine[i].state;
Oscar Mateo84c23772014-07-24 17:04:15 +01002148
Oscar Mateo8c8579172014-07-24 17:04:14 +01002149 if (ctx_obj) {
Oscar Mateodcb4c122014-11-13 10:28:10 +00002150 struct intel_ringbuffer *ringbuf =
2151 ctx->engine[i].ringbuf;
2152 struct intel_engine_cs *ring = ringbuf->ring;
2153
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002154 if (ctx == ring->default_context) {
2155 intel_unpin_ringbuffer_obj(ringbuf);
2156 i915_gem_object_ggtt_unpin(ctx_obj);
2157 }
Mika Kuoppalaa7cbede2015-01-13 11:32:25 +02002158 WARN_ON(ctx->engine[ring->id].pin_count);
Oscar Mateo84c23772014-07-24 17:04:15 +01002159 intel_destroy_ringbuffer_obj(ringbuf);
2160 kfree(ringbuf);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002161 drm_gem_object_unreference(&ctx_obj->base);
2162 }
2163 }
2164}
2165
2166static uint32_t get_lr_context_size(struct intel_engine_cs *ring)
2167{
2168 int ret = 0;
2169
Michael H. Nguyen468c6812014-11-13 17:51:49 +00002170 WARN_ON(INTEL_INFO(ring->dev)->gen < 8);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002171
2172 switch (ring->id) {
2173 case RCS:
Michael H. Nguyen468c6812014-11-13 17:51:49 +00002174 if (INTEL_INFO(ring->dev)->gen >= 9)
2175 ret = GEN9_LR_CONTEXT_RENDER_SIZE;
2176 else
2177 ret = GEN8_LR_CONTEXT_RENDER_SIZE;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002178 break;
2179 case VCS:
2180 case BCS:
2181 case VECS:
2182 case VCS2:
2183 ret = GEN8_LR_CONTEXT_OTHER_SIZE;
2184 break;
2185 }
2186
2187 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01002188}
2189
Daniel Vetter70b0ea82014-11-18 09:09:32 +01002190static void lrc_setup_hardware_status_page(struct intel_engine_cs *ring,
Thomas Daniel1df06b72014-10-29 09:52:51 +00002191 struct drm_i915_gem_object *default_ctx_obj)
2192{
2193 struct drm_i915_private *dev_priv = ring->dev->dev_private;
2194
2195 /* The status page is offset 0 from the default context object
2196 * in LRC mode. */
2197 ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(default_ctx_obj);
2198 ring->status_page.page_addr =
2199 kmap(sg_page(default_ctx_obj->pages->sgl));
Thomas Daniel1df06b72014-10-29 09:52:51 +00002200 ring->status_page.obj = default_ctx_obj;
2201
2202 I915_WRITE(RING_HWS_PGA(ring->mmio_base),
2203 (u32)ring->status_page.gfx_addr);
2204 POSTING_READ(RING_HWS_PGA(ring->mmio_base));
Thomas Daniel1df06b72014-10-29 09:52:51 +00002205}
2206
Oscar Mateo73e4d072014-07-24 17:04:48 +01002207/**
2208 * intel_lr_context_deferred_create() - create the LRC specific bits of a context
2209 * @ctx: LR context to create.
2210 * @ring: engine to be used with the context.
2211 *
2212 * This function can be called more than once, with different engines, if we plan
2213 * to use the context with them. The context backing objects and the ringbuffers
2214 * (specially the ringbuffer backing objects) suck a lot of memory up, and that's why
2215 * the creation is a deferred call: it's better to make sure first that we need to use
2216 * a given ring with the context.
2217 *
Masanari Iida32197aa2014-10-20 23:53:13 +09002218 * Return: non-zero on error.
Oscar Mateo73e4d072014-07-24 17:04:48 +01002219 */
Oscar Mateoede7d422014-07-24 17:04:12 +01002220int intel_lr_context_deferred_create(struct intel_context *ctx,
2221 struct intel_engine_cs *ring)
2222{
Oscar Mateodcb4c122014-11-13 10:28:10 +00002223 const bool is_global_default_ctx = (ctx == ring->default_context);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002224 struct drm_device *dev = ring->dev;
2225 struct drm_i915_gem_object *ctx_obj;
2226 uint32_t context_size;
Oscar Mateo84c23772014-07-24 17:04:15 +01002227 struct intel_ringbuffer *ringbuf;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002228 int ret;
2229
Oscar Mateoede7d422014-07-24 17:04:12 +01002230 WARN_ON(ctx->legacy_hw_ctx.rcs_state != NULL);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002231 WARN_ON(ctx->engine[ring->id].state);
Oscar Mateoede7d422014-07-24 17:04:12 +01002232
Oscar Mateo8c8579172014-07-24 17:04:14 +01002233 context_size = round_up(get_lr_context_size(ring), 4096);
2234
Chris Wilson149c86e2015-04-07 16:21:11 +01002235 ctx_obj = i915_gem_alloc_object(dev, context_size);
Dan Carpenter3126a662015-04-30 17:30:50 +03002236 if (!ctx_obj) {
2237 DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
2238 return -ENOMEM;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002239 }
2240
Oscar Mateodcb4c122014-11-13 10:28:10 +00002241 if (is_global_default_ctx) {
2242 ret = i915_gem_obj_ggtt_pin(ctx_obj, GEN8_LR_CONTEXT_ALIGN, 0);
2243 if (ret) {
2244 DRM_DEBUG_DRIVER("Pin LRC backing obj failed: %d\n",
2245 ret);
2246 drm_gem_object_unreference(&ctx_obj->base);
2247 return ret;
2248 }
Oscar Mateo8c8579172014-07-24 17:04:14 +01002249 }
2250
Oscar Mateo84c23772014-07-24 17:04:15 +01002251 ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
2252 if (!ringbuf) {
2253 DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s\n",
2254 ring->name);
Oscar Mateo84c23772014-07-24 17:04:15 +01002255 ret = -ENOMEM;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002256 goto error_unpin_ctx;
Oscar Mateo84c23772014-07-24 17:04:15 +01002257 }
2258
Daniel Vetter0c7dd532014-08-11 16:17:44 +02002259 ringbuf->ring = ring;
Oscar Mateo582d67f2014-07-24 17:04:16 +01002260
Oscar Mateo84c23772014-07-24 17:04:15 +01002261 ringbuf->size = 32 * PAGE_SIZE;
2262 ringbuf->effective_size = ringbuf->size;
2263 ringbuf->head = 0;
2264 ringbuf->tail = 0;
Oscar Mateo84c23772014-07-24 17:04:15 +01002265 ringbuf->last_retired_head = -1;
Dave Gordonebd0fd42014-11-27 11:22:49 +00002266 intel_ring_update_space(ringbuf);
Oscar Mateo84c23772014-07-24 17:04:15 +01002267
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002268 if (ringbuf->obj == NULL) {
2269 ret = intel_alloc_ringbuffer_obj(dev, ringbuf);
2270 if (ret) {
2271 DRM_DEBUG_DRIVER(
2272 "Failed to allocate ringbuffer obj %s: %d\n",
Oscar Mateo84c23772014-07-24 17:04:15 +01002273 ring->name, ret);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002274 goto error_free_rbuf;
2275 }
2276
2277 if (is_global_default_ctx) {
2278 ret = intel_pin_and_map_ringbuffer_obj(dev, ringbuf);
2279 if (ret) {
2280 DRM_ERROR(
2281 "Failed to pin and map ringbuffer %s: %d\n",
2282 ring->name, ret);
2283 goto error_destroy_rbuf;
2284 }
2285 }
2286
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002287 }
2288
2289 ret = populate_lr_context(ctx, ctx_obj, ring, ringbuf);
2290 if (ret) {
2291 DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002292 goto error;
Oscar Mateo84c23772014-07-24 17:04:15 +01002293 }
2294
2295 ctx->engine[ring->id].ringbuf = ringbuf;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002296 ctx->engine[ring->id].state = ctx_obj;
Oscar Mateoede7d422014-07-24 17:04:12 +01002297
Daniel Vetter70b0ea82014-11-18 09:09:32 +01002298 if (ctx == ring->default_context)
2299 lrc_setup_hardware_status_page(ring, ctx_obj);
Thomas Daniele7778be2014-12-02 12:50:48 +00002300 else if (ring->id == RCS && !ctx->rcs_initialized) {
Michel Thierry771b9a52014-11-11 16:47:33 +00002301 if (ring->init_context) {
John Harrison76c39162015-05-29 17:43:43 +01002302 struct drm_i915_gem_request *req;
2303
2304 ret = i915_gem_request_alloc(ring, ctx, &req);
2305 if (ret)
2306 return ret;
2307
John Harrison87531812015-05-29 17:43:44 +01002308 ret = ring->init_context(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00002309 if (ret) {
Michel Thierry771b9a52014-11-11 16:47:33 +00002310 DRM_ERROR("ring init context: %d\n", ret);
John Harrison76c39162015-05-29 17:43:43 +01002311 i915_gem_request_cancel(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00002312 ctx->engine[ring->id].ringbuf = NULL;
2313 ctx->engine[ring->id].state = NULL;
2314 goto error;
2315 }
John Harrison76c39162015-05-29 17:43:43 +01002316
John Harrison75289872015-05-29 17:43:49 +01002317 i915_add_request_no_flush(req);
Michel Thierry771b9a52014-11-11 16:47:33 +00002318 }
2319
Oscar Mateo564ddb22014-08-21 11:40:54 +01002320 ctx->rcs_initialized = true;
2321 }
2322
Oscar Mateoede7d422014-07-24 17:04:12 +01002323 return 0;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002324
2325error:
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002326 if (is_global_default_ctx)
2327 intel_unpin_ringbuffer_obj(ringbuf);
2328error_destroy_rbuf:
2329 intel_destroy_ringbuffer_obj(ringbuf);
2330error_free_rbuf:
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002331 kfree(ringbuf);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002332error_unpin_ctx:
Oscar Mateodcb4c122014-11-13 10:28:10 +00002333 if (is_global_default_ctx)
2334 i915_gem_object_ggtt_unpin(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002335 drm_gem_object_unreference(&ctx_obj->base);
2336 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01002337}
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002338
2339void intel_lr_context_reset(struct drm_device *dev,
2340 struct intel_context *ctx)
2341{
2342 struct drm_i915_private *dev_priv = dev->dev_private;
2343 struct intel_engine_cs *ring;
2344 int i;
2345
2346 for_each_ring(ring, dev_priv, i) {
2347 struct drm_i915_gem_object *ctx_obj =
2348 ctx->engine[ring->id].state;
2349 struct intel_ringbuffer *ringbuf =
2350 ctx->engine[ring->id].ringbuf;
2351 uint32_t *reg_state;
2352 struct page *page;
2353
2354 if (!ctx_obj)
2355 continue;
2356
2357 if (i915_gem_object_get_pages(ctx_obj)) {
2358 WARN(1, "Failed get_pages for context obj\n");
2359 continue;
2360 }
2361 page = i915_gem_object_get_page(ctx_obj, 1);
2362 reg_state = kmap_atomic(page);
2363
2364 reg_state[CTX_RING_HEAD+1] = 0;
2365 reg_state[CTX_RING_TAIL+1] = 0;
2366
2367 kunmap_atomic(reg_state);
2368
2369 ringbuf->head = 0;
2370 ringbuf->tail = 0;
2371 }
2372}