blob: b7e16293b9a9d3c5e90c119c4bcc4fb786ee6163 [file] [log] [blame]
Oscar Mateob20385f2014-07-24 17:04:10 +01001/*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Ben Widawsky <ben@bwidawsk.net>
25 * Michel Thierry <michel.thierry@intel.com>
26 * Thomas Daniel <thomas.daniel@intel.com>
27 * Oscar Mateo <oscar.mateo@intel.com>
28 *
29 */
30
Oscar Mateo73e4d072014-07-24 17:04:48 +010031/**
32 * DOC: Logical Rings, Logical Ring Contexts and Execlists
33 *
34 * Motivation:
Oscar Mateob20385f2014-07-24 17:04:10 +010035 * GEN8 brings an expansion of the HW contexts: "Logical Ring Contexts".
36 * These expanded contexts enable a number of new abilities, especially
37 * "Execlists" (also implemented in this file).
38 *
Oscar Mateo73e4d072014-07-24 17:04:48 +010039 * One of the main differences with the legacy HW contexts is that logical
40 * ring contexts incorporate many more things to the context's state, like
41 * PDPs or ringbuffer control registers:
42 *
43 * The reason why PDPs are included in the context is straightforward: as
44 * PPGTTs (per-process GTTs) are actually per-context, having the PDPs
45 * contained there mean you don't need to do a ppgtt->switch_mm yourself,
46 * instead, the GPU will do it for you on the context switch.
47 *
48 * But, what about the ringbuffer control registers (head, tail, etc..)?
49 * shouldn't we just need a set of those per engine command streamer? This is
50 * where the name "Logical Rings" starts to make sense: by virtualizing the
51 * rings, the engine cs shifts to a new "ring buffer" with every context
52 * switch. When you want to submit a workload to the GPU you: A) choose your
53 * context, B) find its appropriate virtualized ring, C) write commands to it
54 * and then, finally, D) tell the GPU to switch to that context.
55 *
56 * Instead of the legacy MI_SET_CONTEXT, the way you tell the GPU to switch
57 * to a contexts is via a context execution list, ergo "Execlists".
58 *
59 * LRC implementation:
60 * Regarding the creation of contexts, we have:
61 *
62 * - One global default context.
63 * - One local default context for each opened fd.
64 * - One local extra context for each context create ioctl call.
65 *
66 * Now that ringbuffers belong per-context (and not per-engine, like before)
67 * and that contexts are uniquely tied to a given engine (and not reusable,
68 * like before) we need:
69 *
70 * - One ringbuffer per-engine inside each context.
71 * - One backing object per-engine inside each context.
72 *
73 * The global default context starts its life with these new objects fully
74 * allocated and populated. The local default context for each opened fd is
75 * more complex, because we don't know at creation time which engine is going
76 * to use them. To handle this, we have implemented a deferred creation of LR
77 * contexts:
78 *
79 * The local context starts its life as a hollow or blank holder, that only
80 * gets populated for a given engine once we receive an execbuffer. If later
81 * on we receive another execbuffer ioctl for the same context but a different
82 * engine, we allocate/populate a new ringbuffer and context backing object and
83 * so on.
84 *
85 * Finally, regarding local contexts created using the ioctl call: as they are
86 * only allowed with the render ring, we can allocate & populate them right
87 * away (no need to defer anything, at least for now).
88 *
89 * Execlists implementation:
Oscar Mateob20385f2014-07-24 17:04:10 +010090 * Execlists are the new method by which, on gen8+ hardware, workloads are
91 * submitted for execution (as opposed to the legacy, ringbuffer-based, method).
Oscar Mateo73e4d072014-07-24 17:04:48 +010092 * This method works as follows:
93 *
94 * When a request is committed, its commands (the BB start and any leading or
95 * trailing commands, like the seqno breadcrumbs) are placed in the ringbuffer
96 * for the appropriate context. The tail pointer in the hardware context is not
97 * updated at this time, but instead, kept by the driver in the ringbuffer
98 * structure. A structure representing this request is added to a request queue
99 * for the appropriate engine: this structure contains a copy of the context's
100 * tail after the request was written to the ring buffer and a pointer to the
101 * context itself.
102 *
103 * If the engine's request queue was empty before the request was added, the
104 * queue is processed immediately. Otherwise the queue will be processed during
105 * a context switch interrupt. In any case, elements on the queue will get sent
106 * (in pairs) to the GPU's ExecLists Submit Port (ELSP, for short) with a
107 * globally unique 20-bits submission ID.
108 *
109 * When execution of a request completes, the GPU updates the context status
110 * buffer with a context complete event and generates a context switch interrupt.
111 * During the interrupt handling, the driver examines the events in the buffer:
112 * for each context complete event, if the announced ID matches that on the head
113 * of the request queue, then that request is retired and removed from the queue.
114 *
115 * After processing, if any requests were retired and the queue is not empty
116 * then a new execution list can be submitted. The two requests at the front of
117 * the queue are next to be submitted but since a context may not occur twice in
118 * an execution list, if subsequent requests have the same ID as the first then
119 * the two requests must be combined. This is done simply by discarding requests
120 * at the head of the queue until either only one requests is left (in which case
121 * we use a NULL second context) or the first two requests have unique IDs.
122 *
123 * By always executing the first two requests in the queue the driver ensures
124 * that the GPU is kept as busy as possible. In the case where a single context
125 * completes but a second context is still executing, the request for this second
126 * context will be at the head of the queue when we remove the first one. This
127 * request will then be resubmitted along with a new request for a different context,
128 * which will cause the hardware to continue executing the second request and queue
129 * the new request (the GPU detects the condition of a context getting preempted
130 * with the same context and optimizes the context switch flow by not doing
131 * preemption, but just sampling the new tail pointer).
132 *
Oscar Mateob20385f2014-07-24 17:04:10 +0100133 */
134
135#include <drm/drmP.h>
136#include <drm/i915_drm.h>
137#include "i915_drv.h"
Peter Antoine3bbaba02015-07-10 20:13:11 +0300138#include "intel_mocs.h"
Oscar Mateo127f1002014-07-24 17:04:11 +0100139
Michael H. Nguyen468c6812014-11-13 17:51:49 +0000140#define GEN9_LR_CONTEXT_RENDER_SIZE (22 * PAGE_SIZE)
Oscar Mateo8c8579172014-07-24 17:04:14 +0100141#define GEN8_LR_CONTEXT_RENDER_SIZE (20 * PAGE_SIZE)
142#define GEN8_LR_CONTEXT_OTHER_SIZE (2 * PAGE_SIZE)
143
Thomas Daniele981e7b2014-07-24 17:04:39 +0100144#define RING_EXECLIST_QFULL (1 << 0x2)
145#define RING_EXECLIST1_VALID (1 << 0x3)
146#define RING_EXECLIST0_VALID (1 << 0x4)
147#define RING_EXECLIST_ACTIVE_STATUS (3 << 0xE)
148#define RING_EXECLIST1_ACTIVE (1 << 0x11)
149#define RING_EXECLIST0_ACTIVE (1 << 0x12)
150
151#define GEN8_CTX_STATUS_IDLE_ACTIVE (1 << 0)
152#define GEN8_CTX_STATUS_PREEMPTED (1 << 1)
153#define GEN8_CTX_STATUS_ELEMENT_SWITCH (1 << 2)
154#define GEN8_CTX_STATUS_ACTIVE_IDLE (1 << 3)
155#define GEN8_CTX_STATUS_COMPLETE (1 << 4)
156#define GEN8_CTX_STATUS_LITE_RESTORE (1 << 15)
Oscar Mateo8670d6f2014-07-24 17:04:17 +0100157
158#define CTX_LRI_HEADER_0 0x01
159#define CTX_CONTEXT_CONTROL 0x02
160#define CTX_RING_HEAD 0x04
161#define CTX_RING_TAIL 0x06
162#define CTX_RING_BUFFER_START 0x08
163#define CTX_RING_BUFFER_CONTROL 0x0a
164#define CTX_BB_HEAD_U 0x0c
165#define CTX_BB_HEAD_L 0x0e
166#define CTX_BB_STATE 0x10
167#define CTX_SECOND_BB_HEAD_U 0x12
168#define CTX_SECOND_BB_HEAD_L 0x14
169#define CTX_SECOND_BB_STATE 0x16
170#define CTX_BB_PER_CTX_PTR 0x18
171#define CTX_RCS_INDIRECT_CTX 0x1a
172#define CTX_RCS_INDIRECT_CTX_OFFSET 0x1c
173#define CTX_LRI_HEADER_1 0x21
174#define CTX_CTX_TIMESTAMP 0x22
175#define CTX_PDP3_UDW 0x24
176#define CTX_PDP3_LDW 0x26
177#define CTX_PDP2_UDW 0x28
178#define CTX_PDP2_LDW 0x2a
179#define CTX_PDP1_UDW 0x2c
180#define CTX_PDP1_LDW 0x2e
181#define CTX_PDP0_UDW 0x30
182#define CTX_PDP0_LDW 0x32
183#define CTX_LRI_HEADER_2 0x41
184#define CTX_R_PWR_CLK_STATE 0x42
185#define CTX_GPGPU_CSR_BASE_ADDRESS 0x44
186
Ben Widawsky84b790f2014-07-24 17:04:36 +0100187#define GEN8_CTX_VALID (1<<0)
188#define GEN8_CTX_FORCE_PD_RESTORE (1<<1)
189#define GEN8_CTX_FORCE_RESTORE (1<<2)
190#define GEN8_CTX_L3LLC_COHERENT (1<<5)
191#define GEN8_CTX_PRIVILEGE (1<<8)
Michel Thierrye5815a22015-04-08 12:13:32 +0100192
193#define ASSIGN_CTX_PDP(ppgtt, reg_state, n) { \
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300194 const u64 _addr = i915_page_dir_dma_addr((ppgtt), (n)); \
Michel Thierrye5815a22015-04-08 12:13:32 +0100195 reg_state[CTX_PDP ## n ## _UDW+1] = upper_32_bits(_addr); \
196 reg_state[CTX_PDP ## n ## _LDW+1] = lower_32_bits(_addr); \
197}
198
Ben Widawsky84b790f2014-07-24 17:04:36 +0100199enum {
200 ADVANCED_CONTEXT = 0,
201 LEGACY_CONTEXT,
202 ADVANCED_AD_CONTEXT,
203 LEGACY_64B_CONTEXT
204};
205#define GEN8_CTX_MODE_SHIFT 3
206enum {
207 FAULT_AND_HANG = 0,
208 FAULT_AND_HALT, /* Debug only */
209 FAULT_AND_STREAM,
210 FAULT_AND_CONTINUE /* Unsupported */
211};
212#define GEN8_CTX_ID_SHIFT 32
Arun Siluvery17ee9502015-06-19 19:07:01 +0100213#define CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x17
Ben Widawsky84b790f2014-07-24 17:04:36 +0100214
Mika Kuoppala8ba319d2015-07-03 17:09:35 +0300215static int intel_lr_context_pin(struct drm_i915_gem_request *rq);
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000216
Oscar Mateo73e4d072014-07-24 17:04:48 +0100217/**
218 * intel_sanitize_enable_execlists() - sanitize i915.enable_execlists
219 * @dev: DRM device.
220 * @enable_execlists: value of i915.enable_execlists module parameter.
221 *
222 * Only certain platforms support Execlists (the prerequisites being
Thomas Daniel27401d12014-12-11 12:48:35 +0000223 * support for Logical Ring Contexts and Aliasing PPGTT or better).
Oscar Mateo73e4d072014-07-24 17:04:48 +0100224 *
225 * Return: 1 if Execlists is supported and has to be enabled.
226 */
Oscar Mateo127f1002014-07-24 17:04:11 +0100227int intel_sanitize_enable_execlists(struct drm_device *dev, int enable_execlists)
228{
Daniel Vetterbd84b1e2014-08-11 15:57:57 +0200229 WARN_ON(i915.enable_ppgtt == -1);
230
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000231 if (INTEL_INFO(dev)->gen >= 9)
232 return 1;
233
Oscar Mateo127f1002014-07-24 17:04:11 +0100234 if (enable_execlists == 0)
235 return 0;
236
Oscar Mateo14bf9932014-07-24 17:04:34 +0100237 if (HAS_LOGICAL_RING_CONTEXTS(dev) && USES_PPGTT(dev) &&
238 i915.use_mmio_flip >= 0)
Oscar Mateo127f1002014-07-24 17:04:11 +0100239 return 1;
240
241 return 0;
242}
Oscar Mateoede7d422014-07-24 17:04:12 +0100243
Oscar Mateo73e4d072014-07-24 17:04:48 +0100244/**
245 * intel_execlists_ctx_id() - get the Execlists Context ID
246 * @ctx_obj: Logical Ring Context backing object.
247 *
248 * Do not confuse with ctx->id! Unfortunately we have a name overload
249 * here: the old context ID we pass to userspace as a handler so that
250 * they can refer to a context, and the new context ID we pass to the
251 * ELSP so that the GPU can inform us of the context status via
252 * interrupts.
253 *
254 * Return: 20-bits globally unique context ID.
255 */
Ben Widawsky84b790f2014-07-24 17:04:36 +0100256u32 intel_execlists_ctx_id(struct drm_i915_gem_object *ctx_obj)
257{
258 u32 lrca = i915_gem_obj_ggtt_offset(ctx_obj);
259
260 /* LRCA is required to be 4K aligned so the more significant 20 bits
261 * are globally unique */
262 return lrca >> 12;
263}
264
Mika Kuoppala8ee36152015-07-03 17:09:37 +0300265static uint64_t execlists_ctx_descriptor(struct drm_i915_gem_request *rq)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100266{
Mika Kuoppala8ee36152015-07-03 17:09:37 +0300267 struct intel_engine_cs *ring = rq->ring;
Nick Hoath203a5712015-02-06 11:30:04 +0000268 struct drm_device *dev = ring->dev;
Mika Kuoppala8ee36152015-07-03 17:09:37 +0300269 struct drm_i915_gem_object *ctx_obj = rq->ctx->engine[ring->id].state;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100270 uint64_t desc;
271 uint64_t lrca = i915_gem_obj_ggtt_offset(ctx_obj);
Michel Thierryacdd8842014-07-24 17:04:38 +0100272
273 WARN_ON(lrca & 0xFFFFFFFF00000FFFULL);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100274
275 desc = GEN8_CTX_VALID;
276 desc |= LEGACY_CONTEXT << GEN8_CTX_MODE_SHIFT;
Arun Siluvery51847fb2015-04-07 14:01:33 +0100277 if (IS_GEN8(ctx_obj->base.dev))
278 desc |= GEN8_CTX_L3LLC_COHERENT;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100279 desc |= GEN8_CTX_PRIVILEGE;
280 desc |= lrca;
281 desc |= (u64)intel_execlists_ctx_id(ctx_obj) << GEN8_CTX_ID_SHIFT;
282
283 /* TODO: WaDisableLiteRestore when we start using semaphore
284 * signalling between Command Streamers */
285 /* desc |= GEN8_CTX_FORCE_RESTORE; */
286
Nick Hoath203a5712015-02-06 11:30:04 +0000287 /* WaEnableForceRestoreInCtxtDescForVCS:skl */
288 if (IS_GEN9(dev) &&
289 INTEL_REVID(dev) <= SKL_REVID_B0 &&
290 (ring->id == BCS || ring->id == VCS ||
291 ring->id == VECS || ring->id == VCS2))
292 desc |= GEN8_CTX_FORCE_RESTORE;
293
Ben Widawsky84b790f2014-07-24 17:04:36 +0100294 return desc;
295}
296
Mika Kuoppalacc3c4252015-07-03 17:09:36 +0300297static void execlists_elsp_write(struct drm_i915_gem_request *rq0,
298 struct drm_i915_gem_request *rq1)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100299{
Mika Kuoppalacc3c4252015-07-03 17:09:36 +0300300
301 struct intel_engine_cs *ring = rq0->ring;
Tvrtko Ursulin6e7cc472014-11-13 17:51:51 +0000302 struct drm_device *dev = ring->dev;
303 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppala1cff8cc2015-07-06 11:09:25 +0300304 uint64_t desc[2];
Ben Widawsky84b790f2014-07-24 17:04:36 +0100305
Mika Kuoppala1cff8cc2015-07-06 11:09:25 +0300306 if (rq1) {
307 desc[1] = execlists_ctx_descriptor(rq1);
308 rq1->elsp_submitted++;
309 } else {
310 desc[1] = 0;
311 }
Ben Widawsky84b790f2014-07-24 17:04:36 +0100312
Mika Kuoppala1cff8cc2015-07-06 11:09:25 +0300313 desc[0] = execlists_ctx_descriptor(rq0);
314 rq0->elsp_submitted++;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100315
Mika Kuoppala1cff8cc2015-07-06 11:09:25 +0300316 /* You must always write both descriptors in the order below. */
Chris Wilsona6111f72015-04-07 16:21:02 +0100317 spin_lock(&dev_priv->uncore.lock);
318 intel_uncore_forcewake_get__locked(dev_priv, FORCEWAKE_ALL);
Mika Kuoppala1cff8cc2015-07-06 11:09:25 +0300319 I915_WRITE_FW(RING_ELSP(ring), upper_32_bits(desc[1]));
320 I915_WRITE_FW(RING_ELSP(ring), lower_32_bits(desc[1]));
Chris Wilson6daccb02015-01-16 11:34:35 +0200321
Mika Kuoppala1cff8cc2015-07-06 11:09:25 +0300322 I915_WRITE_FW(RING_ELSP(ring), upper_32_bits(desc[0]));
Ben Widawsky84b790f2014-07-24 17:04:36 +0100323 /* The context is automatically loaded after the following */
Mika Kuoppala1cff8cc2015-07-06 11:09:25 +0300324 I915_WRITE_FW(RING_ELSP(ring), lower_32_bits(desc[0]));
Ben Widawsky84b790f2014-07-24 17:04:36 +0100325
Mika Kuoppala1cff8cc2015-07-06 11:09:25 +0300326 /* ELSP is a wo register, use another nearby reg for posting */
Chris Wilsona6111f72015-04-07 16:21:02 +0100327 POSTING_READ_FW(RING_EXECLIST_STATUS(ring));
328 intel_uncore_forcewake_put__locked(dev_priv, FORCEWAKE_ALL);
329 spin_unlock(&dev_priv->uncore.lock);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100330}
331
Mika Kuoppala05d98242015-07-03 17:09:33 +0300332static int execlists_update_context(struct drm_i915_gem_request *rq)
Oscar Mateoae1250b2014-07-24 17:04:37 +0100333{
Mika Kuoppala05d98242015-07-03 17:09:33 +0300334 struct intel_engine_cs *ring = rq->ring;
335 struct i915_hw_ppgtt *ppgtt = rq->ctx->ppgtt;
336 struct drm_i915_gem_object *ctx_obj = rq->ctx->engine[ring->id].state;
337 struct drm_i915_gem_object *rb_obj = rq->ringbuf->obj;
Oscar Mateoae1250b2014-07-24 17:04:37 +0100338 struct page *page;
339 uint32_t *reg_state;
340
Mika Kuoppala05d98242015-07-03 17:09:33 +0300341 BUG_ON(!ctx_obj);
342 WARN_ON(!i915_gem_obj_is_pinned(ctx_obj));
343 WARN_ON(!i915_gem_obj_is_pinned(rb_obj));
344
Oscar Mateoae1250b2014-07-24 17:04:37 +0100345 page = i915_gem_object_get_page(ctx_obj, 1);
346 reg_state = kmap_atomic(page);
347
Mika Kuoppala05d98242015-07-03 17:09:33 +0300348 reg_state[CTX_RING_TAIL+1] = rq->tail;
349 reg_state[CTX_RING_BUFFER_START+1] = i915_gem_obj_ggtt_offset(rb_obj);
Oscar Mateoae1250b2014-07-24 17:04:37 +0100350
Michel Thierryd7b26332015-04-08 12:13:34 +0100351 /* True PPGTT with dynamic page allocation: update PDP registers and
352 * point the unallocated PDPs to the scratch page
353 */
354 if (ppgtt) {
355 ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
356 ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
357 ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
358 ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
359 }
360
Oscar Mateoae1250b2014-07-24 17:04:37 +0100361 kunmap_atomic(reg_state);
362
363 return 0;
364}
365
Mika Kuoppalad8cb8872015-07-03 17:09:32 +0300366static void execlists_submit_requests(struct drm_i915_gem_request *rq0,
367 struct drm_i915_gem_request *rq1)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100368{
Mika Kuoppala05d98242015-07-03 17:09:33 +0300369 execlists_update_context(rq0);
Oscar Mateoae1250b2014-07-24 17:04:37 +0100370
Mika Kuoppalacc3c4252015-07-03 17:09:36 +0300371 if (rq1)
Mika Kuoppala05d98242015-07-03 17:09:33 +0300372 execlists_update_context(rq1);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100373
Mika Kuoppalacc3c4252015-07-03 17:09:36 +0300374 execlists_elsp_write(rq0, rq1);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100375}
376
Michel Thierryacdd8842014-07-24 17:04:38 +0100377static void execlists_context_unqueue(struct intel_engine_cs *ring)
378{
Nick Hoath6d3d8272015-01-15 13:10:39 +0000379 struct drm_i915_gem_request *req0 = NULL, *req1 = NULL;
380 struct drm_i915_gem_request *cursor = NULL, *tmp = NULL;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100381
382 assert_spin_locked(&ring->execlist_lock);
Michel Thierryacdd8842014-07-24 17:04:38 +0100383
Peter Antoine779949f2015-05-11 16:03:27 +0100384 /*
385 * If irqs are not active generate a warning as batches that finish
386 * without the irqs may get lost and a GPU Hang may occur.
387 */
388 WARN_ON(!intel_irqs_enabled(ring->dev->dev_private));
389
Michel Thierryacdd8842014-07-24 17:04:38 +0100390 if (list_empty(&ring->execlist_queue))
391 return;
392
393 /* Try to read in pairs */
394 list_for_each_entry_safe(cursor, tmp, &ring->execlist_queue,
395 execlist_link) {
396 if (!req0) {
397 req0 = cursor;
Nick Hoath6d3d8272015-01-15 13:10:39 +0000398 } else if (req0->ctx == cursor->ctx) {
Michel Thierryacdd8842014-07-24 17:04:38 +0100399 /* Same ctx: ignore first request, as second request
400 * will update tail past first request's workload */
Oscar Mateoe1fee722014-07-24 17:04:40 +0100401 cursor->elsp_submitted = req0->elsp_submitted;
Michel Thierryacdd8842014-07-24 17:04:38 +0100402 list_del(&req0->execlist_link);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000403 list_add_tail(&req0->execlist_link,
404 &ring->execlist_retired_req_list);
Michel Thierryacdd8842014-07-24 17:04:38 +0100405 req0 = cursor;
406 } else {
407 req1 = cursor;
408 break;
409 }
410 }
411
Michel Thierry53292cd2015-04-15 18:11:33 +0100412 if (IS_GEN8(ring->dev) || IS_GEN9(ring->dev)) {
413 /*
414 * WaIdleLiteRestore: make sure we never cause a lite
415 * restore with HEAD==TAIL
416 */
Michel Thierryd63f8202015-04-27 12:31:44 +0100417 if (req0->elsp_submitted) {
Michel Thierry53292cd2015-04-15 18:11:33 +0100418 /*
419 * Apply the wa NOOPS to prevent ring:HEAD == req:TAIL
420 * as we resubmit the request. See gen8_emit_request()
421 * for where we prepare the padding after the end of the
422 * request.
423 */
424 struct intel_ringbuffer *ringbuf;
425
426 ringbuf = req0->ctx->engine[ring->id].ringbuf;
427 req0->tail += 8;
428 req0->tail &= ringbuf->size - 1;
429 }
430 }
431
Oscar Mateoe1fee722014-07-24 17:04:40 +0100432 WARN_ON(req1 && req1->elsp_submitted);
433
Mika Kuoppalad8cb8872015-07-03 17:09:32 +0300434 execlists_submit_requests(req0, req1);
Michel Thierryacdd8842014-07-24 17:04:38 +0100435}
436
Thomas Daniele981e7b2014-07-24 17:04:39 +0100437static bool execlists_check_remove_request(struct intel_engine_cs *ring,
438 u32 request_id)
439{
Nick Hoath6d3d8272015-01-15 13:10:39 +0000440 struct drm_i915_gem_request *head_req;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100441
442 assert_spin_locked(&ring->execlist_lock);
443
444 head_req = list_first_entry_or_null(&ring->execlist_queue,
Nick Hoath6d3d8272015-01-15 13:10:39 +0000445 struct drm_i915_gem_request,
Thomas Daniele981e7b2014-07-24 17:04:39 +0100446 execlist_link);
447
448 if (head_req != NULL) {
449 struct drm_i915_gem_object *ctx_obj =
Nick Hoath6d3d8272015-01-15 13:10:39 +0000450 head_req->ctx->engine[ring->id].state;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100451 if (intel_execlists_ctx_id(ctx_obj) == request_id) {
Oscar Mateoe1fee722014-07-24 17:04:40 +0100452 WARN(head_req->elsp_submitted == 0,
453 "Never submitted head request\n");
454
455 if (--head_req->elsp_submitted <= 0) {
456 list_del(&head_req->execlist_link);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000457 list_add_tail(&head_req->execlist_link,
458 &ring->execlist_retired_req_list);
Oscar Mateoe1fee722014-07-24 17:04:40 +0100459 return true;
460 }
Thomas Daniele981e7b2014-07-24 17:04:39 +0100461 }
462 }
463
464 return false;
465}
466
Oscar Mateo73e4d072014-07-24 17:04:48 +0100467/**
Daniel Vetter3f7531c2014-12-10 17:41:43 +0100468 * intel_lrc_irq_handler() - handle Context Switch interrupts
Oscar Mateo73e4d072014-07-24 17:04:48 +0100469 * @ring: Engine Command Streamer to handle.
470 *
471 * Check the unread Context Status Buffers and manage the submission of new
472 * contexts to the ELSP accordingly.
473 */
Daniel Vetter3f7531c2014-12-10 17:41:43 +0100474void intel_lrc_irq_handler(struct intel_engine_cs *ring)
Thomas Daniele981e7b2014-07-24 17:04:39 +0100475{
476 struct drm_i915_private *dev_priv = ring->dev->dev_private;
477 u32 status_pointer;
478 u8 read_pointer;
479 u8 write_pointer;
480 u32 status;
481 u32 status_id;
482 u32 submit_contexts = 0;
483
484 status_pointer = I915_READ(RING_CONTEXT_STATUS_PTR(ring));
485
486 read_pointer = ring->next_context_status_buffer;
487 write_pointer = status_pointer & 0x07;
488 if (read_pointer > write_pointer)
489 write_pointer += 6;
490
491 spin_lock(&ring->execlist_lock);
492
493 while (read_pointer < write_pointer) {
494 read_pointer++;
495 status = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
496 (read_pointer % 6) * 8);
497 status_id = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
498 (read_pointer % 6) * 8 + 4);
499
Oscar Mateoe1fee722014-07-24 17:04:40 +0100500 if (status & GEN8_CTX_STATUS_PREEMPTED) {
501 if (status & GEN8_CTX_STATUS_LITE_RESTORE) {
502 if (execlists_check_remove_request(ring, status_id))
503 WARN(1, "Lite Restored request removed from queue\n");
504 } else
505 WARN(1, "Preemption without Lite Restore\n");
506 }
507
508 if ((status & GEN8_CTX_STATUS_ACTIVE_IDLE) ||
509 (status & GEN8_CTX_STATUS_ELEMENT_SWITCH)) {
Thomas Daniele981e7b2014-07-24 17:04:39 +0100510 if (execlists_check_remove_request(ring, status_id))
511 submit_contexts++;
512 }
513 }
514
515 if (submit_contexts != 0)
516 execlists_context_unqueue(ring);
517
518 spin_unlock(&ring->execlist_lock);
519
520 WARN(submit_contexts > 2, "More than two context complete events?\n");
521 ring->next_context_status_buffer = write_pointer % 6;
522
523 I915_WRITE(RING_CONTEXT_STATUS_PTR(ring),
524 ((u32)ring->next_context_status_buffer & 0x07) << 8);
525}
526
John Harrisonae707972015-05-29 17:44:14 +0100527static int execlists_context_queue(struct drm_i915_gem_request *request)
Michel Thierryacdd8842014-07-24 17:04:38 +0100528{
John Harrisonae707972015-05-29 17:44:14 +0100529 struct intel_engine_cs *ring = request->ring;
Nick Hoath6d3d8272015-01-15 13:10:39 +0000530 struct drm_i915_gem_request *cursor;
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100531 int num_elements = 0;
Michel Thierryacdd8842014-07-24 17:04:38 +0100532
John Harrisonae707972015-05-29 17:44:14 +0100533 if (request->ctx != ring->default_context)
Mika Kuoppala8ba319d2015-07-03 17:09:35 +0300534 intel_lr_context_pin(request);
John Harrison9bb1af42015-05-29 17:44:13 +0100535
536 i915_gem_request_reference(request);
537
John Harrisonae707972015-05-29 17:44:14 +0100538 request->tail = request->ringbuf->tail;
Nick Hoath2d129552015-01-15 13:10:36 +0000539
Chris Wilsonb5eba372015-04-07 16:20:48 +0100540 spin_lock_irq(&ring->execlist_lock);
Michel Thierryacdd8842014-07-24 17:04:38 +0100541
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100542 list_for_each_entry(cursor, &ring->execlist_queue, execlist_link)
543 if (++num_elements > 2)
544 break;
545
546 if (num_elements > 2) {
Nick Hoath6d3d8272015-01-15 13:10:39 +0000547 struct drm_i915_gem_request *tail_req;
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100548
549 tail_req = list_last_entry(&ring->execlist_queue,
Nick Hoath6d3d8272015-01-15 13:10:39 +0000550 struct drm_i915_gem_request,
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100551 execlist_link);
552
John Harrisonae707972015-05-29 17:44:14 +0100553 if (request->ctx == tail_req->ctx) {
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100554 WARN(tail_req->elsp_submitted != 0,
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000555 "More than 2 already-submitted reqs queued\n");
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100556 list_del(&tail_req->execlist_link);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000557 list_add_tail(&tail_req->execlist_link,
558 &ring->execlist_retired_req_list);
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100559 }
560 }
561
Nick Hoath6d3d8272015-01-15 13:10:39 +0000562 list_add_tail(&request->execlist_link, &ring->execlist_queue);
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100563 if (num_elements == 0)
Michel Thierryacdd8842014-07-24 17:04:38 +0100564 execlists_context_unqueue(ring);
565
Chris Wilsonb5eba372015-04-07 16:20:48 +0100566 spin_unlock_irq(&ring->execlist_lock);
Michel Thierryacdd8842014-07-24 17:04:38 +0100567
568 return 0;
569}
570
John Harrison2f200552015-05-29 17:43:53 +0100571static int logical_ring_invalidate_all_caches(struct drm_i915_gem_request *req)
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100572{
John Harrison2f200552015-05-29 17:43:53 +0100573 struct intel_engine_cs *ring = req->ring;
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100574 uint32_t flush_domains;
575 int ret;
576
577 flush_domains = 0;
578 if (ring->gpu_caches_dirty)
579 flush_domains = I915_GEM_GPU_DOMAINS;
580
John Harrison7deb4d32015-05-29 17:43:59 +0100581 ret = ring->emit_flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100582 if (ret)
583 return ret;
584
585 ring->gpu_caches_dirty = false;
586 return 0;
587}
588
John Harrison535fbe82015-05-29 17:43:32 +0100589static int execlists_move_to_gpu(struct drm_i915_gem_request *req,
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100590 struct list_head *vmas)
591{
John Harrison535fbe82015-05-29 17:43:32 +0100592 const unsigned other_rings = ~intel_ring_flag(req->ring);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100593 struct i915_vma *vma;
594 uint32_t flush_domains = 0;
595 bool flush_chipset = false;
596 int ret;
597
598 list_for_each_entry(vma, vmas, exec_list) {
599 struct drm_i915_gem_object *obj = vma->obj;
600
Chris Wilson03ade512015-04-27 13:41:18 +0100601 if (obj->active & other_rings) {
John Harrison91af1272015-06-18 13:14:56 +0100602 ret = i915_gem_object_sync(obj, req->ring, &req);
Chris Wilson03ade512015-04-27 13:41:18 +0100603 if (ret)
604 return ret;
605 }
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100606
607 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
608 flush_chipset |= i915_gem_clflush_object(obj, false);
609
610 flush_domains |= obj->base.write_domain;
611 }
612
613 if (flush_domains & I915_GEM_DOMAIN_GTT)
614 wmb();
615
616 /* Unconditionally invalidate gpu caches and ensure that we do flush
617 * any residual writes from the previous batch.
618 */
John Harrison2f200552015-05-29 17:43:53 +0100619 return logical_ring_invalidate_all_caches(req);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100620}
621
John Harrison40e895c2015-05-29 17:43:26 +0100622int intel_logical_ring_alloc_request_extras(struct drm_i915_gem_request *request)
John Harrisonbc0dce32015-03-19 12:30:07 +0000623{
John Harrisonbc0dce32015-03-19 12:30:07 +0000624 int ret;
625
Mika Kuoppalaf3cc01f2015-07-06 11:08:30 +0300626 request->ringbuf = request->ctx->engine[request->ring->id].ringbuf;
627
John Harrison40e895c2015-05-29 17:43:26 +0100628 if (request->ctx != request->ring->default_context) {
Mika Kuoppala8ba319d2015-07-03 17:09:35 +0300629 ret = intel_lr_context_pin(request);
John Harrison6689cb22015-03-19 12:30:08 +0000630 if (ret)
John Harrisonbc0dce32015-03-19 12:30:07 +0000631 return ret;
John Harrisonbc0dce32015-03-19 12:30:07 +0000632 }
633
John Harrisonbc0dce32015-03-19 12:30:07 +0000634 return 0;
635}
636
John Harrisonae707972015-05-29 17:44:14 +0100637static int logical_ring_wait_for_space(struct drm_i915_gem_request *req,
Chris Wilson595e1ee2015-04-07 16:20:51 +0100638 int bytes)
John Harrisonbc0dce32015-03-19 12:30:07 +0000639{
John Harrisonae707972015-05-29 17:44:14 +0100640 struct intel_ringbuffer *ringbuf = req->ringbuf;
641 struct intel_engine_cs *ring = req->ring;
642 struct drm_i915_gem_request *target;
Chris Wilsonb4716182015-04-27 13:41:17 +0100643 unsigned space;
644 int ret;
John Harrisonbc0dce32015-03-19 12:30:07 +0000645
646 if (intel_ring_space(ringbuf) >= bytes)
647 return 0;
648
John Harrison79bbcc22015-06-30 12:40:55 +0100649 /* The whole point of reserving space is to not wait! */
650 WARN_ON(ringbuf->reserved_in_use);
651
John Harrisonae707972015-05-29 17:44:14 +0100652 list_for_each_entry(target, &ring->request_list, list) {
John Harrisonbc0dce32015-03-19 12:30:07 +0000653 /*
654 * The request queue is per-engine, so can contain requests
655 * from multiple ringbuffers. Here, we must ignore any that
656 * aren't from the ringbuffer we're considering.
657 */
John Harrisonae707972015-05-29 17:44:14 +0100658 if (target->ringbuf != ringbuf)
John Harrisonbc0dce32015-03-19 12:30:07 +0000659 continue;
660
661 /* Would completion of this request free enough space? */
John Harrisonae707972015-05-29 17:44:14 +0100662 space = __intel_ring_space(target->postfix, ringbuf->tail,
Chris Wilsonb4716182015-04-27 13:41:17 +0100663 ringbuf->size);
664 if (space >= bytes)
John Harrisonbc0dce32015-03-19 12:30:07 +0000665 break;
John Harrisonbc0dce32015-03-19 12:30:07 +0000666 }
667
John Harrisonae707972015-05-29 17:44:14 +0100668 if (WARN_ON(&target->list == &ring->request_list))
John Harrisonbc0dce32015-03-19 12:30:07 +0000669 return -ENOSPC;
670
John Harrisonae707972015-05-29 17:44:14 +0100671 ret = i915_wait_request(target);
John Harrisonbc0dce32015-03-19 12:30:07 +0000672 if (ret)
673 return ret;
674
Chris Wilsonb4716182015-04-27 13:41:17 +0100675 ringbuf->space = space;
676 return 0;
John Harrisonbc0dce32015-03-19 12:30:07 +0000677}
678
679/*
680 * intel_logical_ring_advance_and_submit() - advance the tail and submit the workload
John Harrisonae707972015-05-29 17:44:14 +0100681 * @request: Request to advance the logical ringbuffer of.
John Harrisonbc0dce32015-03-19 12:30:07 +0000682 *
683 * The tail is updated in our logical ringbuffer struct, not in the actual context. What
684 * really happens during submission is that the context and current tail will be placed
685 * on a queue waiting for the ELSP to be ready to accept a new context submission. At that
686 * point, the tail *inside* the context is updated and the ELSP written to.
687 */
688static void
John Harrisonae707972015-05-29 17:44:14 +0100689intel_logical_ring_advance_and_submit(struct drm_i915_gem_request *request)
John Harrisonbc0dce32015-03-19 12:30:07 +0000690{
John Harrisonae707972015-05-29 17:44:14 +0100691 struct intel_engine_cs *ring = request->ring;
John Harrisonbc0dce32015-03-19 12:30:07 +0000692
John Harrisonae707972015-05-29 17:44:14 +0100693 intel_logical_ring_advance(request->ringbuf);
John Harrisonbc0dce32015-03-19 12:30:07 +0000694
695 if (intel_ring_stopped(ring))
696 return;
697
John Harrisonae707972015-05-29 17:44:14 +0100698 execlists_context_queue(request);
John Harrisonbc0dce32015-03-19 12:30:07 +0000699}
700
John Harrison79bbcc22015-06-30 12:40:55 +0100701static void __wrap_ring_buffer(struct intel_ringbuffer *ringbuf)
John Harrisonbc0dce32015-03-19 12:30:07 +0000702{
703 uint32_t __iomem *virt;
704 int rem = ringbuf->size - ringbuf->tail;
705
John Harrisonbc0dce32015-03-19 12:30:07 +0000706 virt = ringbuf->virtual_start + ringbuf->tail;
707 rem /= 4;
708 while (rem--)
709 iowrite32(MI_NOOP, virt++);
710
711 ringbuf->tail = 0;
712 intel_ring_update_space(ringbuf);
John Harrisonbc0dce32015-03-19 12:30:07 +0000713}
714
John Harrisonae707972015-05-29 17:44:14 +0100715static int logical_ring_prepare(struct drm_i915_gem_request *req, int bytes)
John Harrisonbc0dce32015-03-19 12:30:07 +0000716{
John Harrisonae707972015-05-29 17:44:14 +0100717 struct intel_ringbuffer *ringbuf = req->ringbuf;
John Harrison79bbcc22015-06-30 12:40:55 +0100718 int remain_usable = ringbuf->effective_size - ringbuf->tail;
719 int remain_actual = ringbuf->size - ringbuf->tail;
720 int ret, total_bytes, wait_bytes = 0;
721 bool need_wrap = false;
John Harrisonbc0dce32015-03-19 12:30:07 +0000722
John Harrison79bbcc22015-06-30 12:40:55 +0100723 if (ringbuf->reserved_in_use)
724 total_bytes = bytes;
725 else
726 total_bytes = bytes + ringbuf->reserved_size;
John Harrison29b1b412015-06-18 13:10:09 +0100727
John Harrison79bbcc22015-06-30 12:40:55 +0100728 if (unlikely(bytes > remain_usable)) {
729 /*
730 * Not enough space for the basic request. So need to flush
731 * out the remainder and then wait for base + reserved.
732 */
733 wait_bytes = remain_actual + total_bytes;
734 need_wrap = true;
735 } else {
736 if (unlikely(total_bytes > remain_usable)) {
737 /*
738 * The base request will fit but the reserved space
739 * falls off the end. So only need to to wait for the
740 * reserved size after flushing out the remainder.
741 */
742 wait_bytes = remain_actual + ringbuf->reserved_size;
743 need_wrap = true;
744 } else if (total_bytes > ringbuf->space) {
745 /* No wrapping required, just waiting. */
746 wait_bytes = total_bytes;
John Harrison29b1b412015-06-18 13:10:09 +0100747 }
John Harrisonbc0dce32015-03-19 12:30:07 +0000748 }
749
John Harrison79bbcc22015-06-30 12:40:55 +0100750 if (wait_bytes) {
751 ret = logical_ring_wait_for_space(req, wait_bytes);
John Harrisonbc0dce32015-03-19 12:30:07 +0000752 if (unlikely(ret))
753 return ret;
John Harrison79bbcc22015-06-30 12:40:55 +0100754
755 if (need_wrap)
756 __wrap_ring_buffer(ringbuf);
John Harrisonbc0dce32015-03-19 12:30:07 +0000757 }
758
759 return 0;
760}
761
762/**
763 * intel_logical_ring_begin() - prepare the logical ringbuffer to accept some commands
764 *
John Harrison4d616a22015-05-29 17:44:08 +0100765 * @request: The request to start some new work for
Arun Siluvery4d78c8d2015-06-23 15:50:43 +0100766 * @ctx: Logical ring context whose ringbuffer is being prepared.
John Harrisonbc0dce32015-03-19 12:30:07 +0000767 * @num_dwords: number of DWORDs that we plan to write to the ringbuffer.
768 *
769 * The ringbuffer might not be ready to accept the commands right away (maybe it needs to
770 * be wrapped, or wait a bit for the tail to be updated). This function takes care of that
771 * and also preallocates a request (every workload submission is still mediated through
772 * requests, same as it did with legacy ringbuffer submission).
773 *
774 * Return: non-zero if the ringbuffer is not ready to be written to.
775 */
Peter Antoine3bbaba02015-07-10 20:13:11 +0300776int intel_logical_ring_begin(struct drm_i915_gem_request *req, int num_dwords)
John Harrisonbc0dce32015-03-19 12:30:07 +0000777{
John Harrison4d616a22015-05-29 17:44:08 +0100778 struct drm_i915_private *dev_priv;
John Harrisonbc0dce32015-03-19 12:30:07 +0000779 int ret;
780
John Harrison4d616a22015-05-29 17:44:08 +0100781 WARN_ON(req == NULL);
782 dev_priv = req->ring->dev->dev_private;
783
John Harrisonbc0dce32015-03-19 12:30:07 +0000784 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
785 dev_priv->mm.interruptible);
786 if (ret)
787 return ret;
788
John Harrisonae707972015-05-29 17:44:14 +0100789 ret = logical_ring_prepare(req, num_dwords * sizeof(uint32_t));
John Harrisonbc0dce32015-03-19 12:30:07 +0000790 if (ret)
791 return ret;
792
John Harrison4d616a22015-05-29 17:44:08 +0100793 req->ringbuf->space -= num_dwords * sizeof(uint32_t);
John Harrisonbc0dce32015-03-19 12:30:07 +0000794 return 0;
795}
796
John Harrisonccd98fe2015-05-29 17:44:09 +0100797int intel_logical_ring_reserve_space(struct drm_i915_gem_request *request)
798{
799 /*
800 * The first call merely notes the reserve request and is common for
801 * all back ends. The subsequent localised _begin() call actually
802 * ensures that the reservation is available. Without the begin, if
803 * the request creator immediately submitted the request without
804 * adding any commands to it then there might not actually be
805 * sufficient room for the submission commands.
806 */
807 intel_ring_reserved_space_reserve(request->ringbuf, MIN_SPACE_FOR_ADD_REQUEST);
808
809 return intel_logical_ring_begin(request, 0);
810}
811
Oscar Mateo73e4d072014-07-24 17:04:48 +0100812/**
813 * execlists_submission() - submit a batchbuffer for execution, Execlists style
814 * @dev: DRM device.
815 * @file: DRM file.
816 * @ring: Engine Command Streamer to submit to.
817 * @ctx: Context to employ for this submission.
818 * @args: execbuffer call arguments.
819 * @vmas: list of vmas.
820 * @batch_obj: the batchbuffer to submit.
821 * @exec_start: batchbuffer start virtual address pointer.
John Harrison8e004ef2015-02-13 11:48:10 +0000822 * @dispatch_flags: translated execbuffer call flags.
Oscar Mateo73e4d072014-07-24 17:04:48 +0100823 *
824 * This is the evil twin version of i915_gem_ringbuffer_submission. It abstracts
825 * away the submission details of the execbuffer ioctl call.
826 *
827 * Return: non-zero if the submission fails.
828 */
John Harrison5f19e2b2015-05-29 17:43:27 +0100829int intel_execlists_submission(struct i915_execbuffer_params *params,
Oscar Mateo454afeb2014-07-24 17:04:22 +0100830 struct drm_i915_gem_execbuffer2 *args,
John Harrison5f19e2b2015-05-29 17:43:27 +0100831 struct list_head *vmas)
Oscar Mateo454afeb2014-07-24 17:04:22 +0100832{
John Harrison5f19e2b2015-05-29 17:43:27 +0100833 struct drm_device *dev = params->dev;
834 struct intel_engine_cs *ring = params->ring;
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100835 struct drm_i915_private *dev_priv = dev->dev_private;
John Harrison5f19e2b2015-05-29 17:43:27 +0100836 struct intel_ringbuffer *ringbuf = params->ctx->engine[ring->id].ringbuf;
837 u64 exec_start;
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100838 int instp_mode;
839 u32 instp_mask;
840 int ret;
841
842 instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
843 instp_mask = I915_EXEC_CONSTANTS_MASK;
844 switch (instp_mode) {
845 case I915_EXEC_CONSTANTS_REL_GENERAL:
846 case I915_EXEC_CONSTANTS_ABSOLUTE:
847 case I915_EXEC_CONSTANTS_REL_SURFACE:
848 if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) {
849 DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
850 return -EINVAL;
851 }
852
853 if (instp_mode != dev_priv->relative_constants_mode) {
854 if (instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
855 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
856 return -EINVAL;
857 }
858
859 /* The HW changed the meaning on this bit on gen6 */
860 instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
861 }
862 break;
863 default:
864 DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
865 return -EINVAL;
866 }
867
868 if (args->num_cliprects != 0) {
869 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
870 return -EINVAL;
871 } else {
872 if (args->DR4 == 0xffffffff) {
873 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
874 args->DR4 = 0;
875 }
876
877 if (args->DR1 || args->DR4 || args->cliprects_ptr) {
878 DRM_DEBUG("0 cliprects but dirt in cliprects fields\n");
879 return -EINVAL;
880 }
881 }
882
883 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
884 DRM_DEBUG("sol reset is gen7 only\n");
885 return -EINVAL;
886 }
887
John Harrison535fbe82015-05-29 17:43:32 +0100888 ret = execlists_move_to_gpu(params->request, vmas);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100889 if (ret)
890 return ret;
891
892 if (ring == &dev_priv->ring[RCS] &&
893 instp_mode != dev_priv->relative_constants_mode) {
John Harrison4d616a22015-05-29 17:44:08 +0100894 ret = intel_logical_ring_begin(params->request, 4);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100895 if (ret)
896 return ret;
897
898 intel_logical_ring_emit(ringbuf, MI_NOOP);
899 intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(1));
900 intel_logical_ring_emit(ringbuf, INSTPM);
901 intel_logical_ring_emit(ringbuf, instp_mask << 16 | instp_mode);
902 intel_logical_ring_advance(ringbuf);
903
904 dev_priv->relative_constants_mode = instp_mode;
905 }
906
John Harrison5f19e2b2015-05-29 17:43:27 +0100907 exec_start = params->batch_obj_vm_offset +
908 args->batch_start_offset;
909
John Harrisonbe795fc2015-05-29 17:44:03 +0100910 ret = ring->emit_bb_start(params->request, exec_start, params->dispatch_flags);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100911 if (ret)
912 return ret;
913
John Harrison95c24162015-05-29 17:43:31 +0100914 trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
John Harrison5e4be7b2015-02-13 11:48:11 +0000915
John Harrison8a8edb52015-05-29 17:43:33 +0100916 i915_gem_execbuffer_move_to_active(vmas, params->request);
John Harrisonadeca762015-05-29 17:43:28 +0100917 i915_gem_execbuffer_retire_commands(params);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100918
Oscar Mateo454afeb2014-07-24 17:04:22 +0100919 return 0;
920}
921
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000922void intel_execlists_retire_requests(struct intel_engine_cs *ring)
923{
Nick Hoath6d3d8272015-01-15 13:10:39 +0000924 struct drm_i915_gem_request *req, *tmp;
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000925 struct list_head retired_list;
926
927 WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
928 if (list_empty(&ring->execlist_retired_req_list))
929 return;
930
931 INIT_LIST_HEAD(&retired_list);
Chris Wilsonb5eba372015-04-07 16:20:48 +0100932 spin_lock_irq(&ring->execlist_lock);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000933 list_replace_init(&ring->execlist_retired_req_list, &retired_list);
Chris Wilsonb5eba372015-04-07 16:20:48 +0100934 spin_unlock_irq(&ring->execlist_lock);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000935
936 list_for_each_entry_safe(req, tmp, &retired_list, execlist_link) {
Nick Hoath6d3d8272015-01-15 13:10:39 +0000937 struct intel_context *ctx = req->ctx;
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000938 struct drm_i915_gem_object *ctx_obj =
939 ctx->engine[ring->id].state;
940
941 if (ctx_obj && (ctx != ring->default_context))
Mika Kuoppala8ba319d2015-07-03 17:09:35 +0300942 intel_lr_context_unpin(req);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000943 list_del(&req->execlist_link);
Nick Hoathf8210792015-01-29 16:55:07 +0000944 i915_gem_request_unreference(req);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000945 }
946}
947
Oscar Mateo454afeb2014-07-24 17:04:22 +0100948void intel_logical_ring_stop(struct intel_engine_cs *ring)
949{
Oscar Mateo9832b9d2014-07-24 17:04:30 +0100950 struct drm_i915_private *dev_priv = ring->dev->dev_private;
951 int ret;
952
953 if (!intel_ring_initialized(ring))
954 return;
955
956 ret = intel_ring_idle(ring);
957 if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
958 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
959 ring->name, ret);
960
961 /* TODO: Is this correct with Execlists enabled? */
962 I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
963 if (wait_for_atomic((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
964 DRM_ERROR("%s :timed out trying to stop ring\n", ring->name);
965 return;
966 }
967 I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
Oscar Mateo454afeb2014-07-24 17:04:22 +0100968}
969
John Harrison4866d722015-05-29 17:43:55 +0100970int logical_ring_flush_all_caches(struct drm_i915_gem_request *req)
Oscar Mateo48e29f52014-07-24 17:04:29 +0100971{
John Harrison4866d722015-05-29 17:43:55 +0100972 struct intel_engine_cs *ring = req->ring;
Oscar Mateo48e29f52014-07-24 17:04:29 +0100973 int ret;
974
975 if (!ring->gpu_caches_dirty)
976 return 0;
977
John Harrison7deb4d32015-05-29 17:43:59 +0100978 ret = ring->emit_flush(req, 0, I915_GEM_GPU_DOMAINS);
Oscar Mateo48e29f52014-07-24 17:04:29 +0100979 if (ret)
980 return ret;
981
982 ring->gpu_caches_dirty = false;
983 return 0;
984}
985
Mika Kuoppala8ba319d2015-07-03 17:09:35 +0300986static int intel_lr_context_pin(struct drm_i915_gem_request *rq)
Oscar Mateodcb4c122014-11-13 10:28:10 +0000987{
Mika Kuoppala8ba319d2015-07-03 17:09:35 +0300988 struct intel_engine_cs *ring = rq->ring;
989 struct drm_i915_gem_object *ctx_obj = rq->ctx->engine[ring->id].state;
990 struct intel_ringbuffer *ringbuf = rq->ringbuf;
Oscar Mateodcb4c122014-11-13 10:28:10 +0000991 int ret = 0;
992
993 WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
Mika Kuoppala8ba319d2015-07-03 17:09:35 +0300994 if (rq->ctx->engine[ring->id].pin_count++ == 0) {
Oscar Mateodcb4c122014-11-13 10:28:10 +0000995 ret = i915_gem_obj_ggtt_pin(ctx_obj,
996 GEN8_LR_CONTEXT_ALIGN, 0);
997 if (ret)
Mika Kuoppalaa7cbede2015-01-13 11:32:25 +0200998 goto reset_pin_count;
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000999
1000 ret = intel_pin_and_map_ringbuffer_obj(ring->dev, ringbuf);
1001 if (ret)
1002 goto unpin_ctx_obj;
Oscar Mateodcb4c122014-11-13 10:28:10 +00001003 }
1004
1005 return ret;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001006
1007unpin_ctx_obj:
1008 i915_gem_object_ggtt_unpin(ctx_obj);
Mika Kuoppalaa7cbede2015-01-13 11:32:25 +02001009reset_pin_count:
Mika Kuoppala8ba319d2015-07-03 17:09:35 +03001010 rq->ctx->engine[ring->id].pin_count = 0;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001011
1012 return ret;
Oscar Mateodcb4c122014-11-13 10:28:10 +00001013}
1014
Mika Kuoppala8ba319d2015-07-03 17:09:35 +03001015void intel_lr_context_unpin(struct drm_i915_gem_request *rq)
Oscar Mateodcb4c122014-11-13 10:28:10 +00001016{
Mika Kuoppala8ba319d2015-07-03 17:09:35 +03001017 struct intel_engine_cs *ring = rq->ring;
1018 struct drm_i915_gem_object *ctx_obj = rq->ctx->engine[ring->id].state;
1019 struct intel_ringbuffer *ringbuf = rq->ringbuf;
Oscar Mateodcb4c122014-11-13 10:28:10 +00001020
1021 if (ctx_obj) {
1022 WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
Mika Kuoppala8ba319d2015-07-03 17:09:35 +03001023 if (--rq->ctx->engine[ring->id].pin_count == 0) {
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001024 intel_unpin_ringbuffer_obj(ringbuf);
Oscar Mateodcb4c122014-11-13 10:28:10 +00001025 i915_gem_object_ggtt_unpin(ctx_obj);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00001026 }
Oscar Mateodcb4c122014-11-13 10:28:10 +00001027 }
1028}
1029
John Harrisone2be4fa2015-05-29 17:43:54 +01001030static int intel_logical_ring_workarounds_emit(struct drm_i915_gem_request *req)
Michel Thierry771b9a52014-11-11 16:47:33 +00001031{
1032 int ret, i;
John Harrisone2be4fa2015-05-29 17:43:54 +01001033 struct intel_engine_cs *ring = req->ring;
1034 struct intel_ringbuffer *ringbuf = req->ringbuf;
Michel Thierry771b9a52014-11-11 16:47:33 +00001035 struct drm_device *dev = ring->dev;
1036 struct drm_i915_private *dev_priv = dev->dev_private;
1037 struct i915_workarounds *w = &dev_priv->workarounds;
1038
Michel Thierrye6c1abb2014-11-26 14:21:02 +00001039 if (WARN_ON_ONCE(w->count == 0))
Michel Thierry771b9a52014-11-11 16:47:33 +00001040 return 0;
1041
1042 ring->gpu_caches_dirty = true;
John Harrison4866d722015-05-29 17:43:55 +01001043 ret = logical_ring_flush_all_caches(req);
Michel Thierry771b9a52014-11-11 16:47:33 +00001044 if (ret)
1045 return ret;
1046
John Harrison4d616a22015-05-29 17:44:08 +01001047 ret = intel_logical_ring_begin(req, w->count * 2 + 2);
Michel Thierry771b9a52014-11-11 16:47:33 +00001048 if (ret)
1049 return ret;
1050
1051 intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(w->count));
1052 for (i = 0; i < w->count; i++) {
1053 intel_logical_ring_emit(ringbuf, w->reg[i].addr);
1054 intel_logical_ring_emit(ringbuf, w->reg[i].value);
1055 }
1056 intel_logical_ring_emit(ringbuf, MI_NOOP);
1057
1058 intel_logical_ring_advance(ringbuf);
1059
1060 ring->gpu_caches_dirty = true;
John Harrison4866d722015-05-29 17:43:55 +01001061 ret = logical_ring_flush_all_caches(req);
Michel Thierry771b9a52014-11-11 16:47:33 +00001062 if (ret)
1063 return ret;
1064
1065 return 0;
1066}
1067
Arun Siluvery83b8a982015-07-08 10:27:05 +01001068#define wa_ctx_emit(batch, index, cmd) \
Arun Siluvery17ee9502015-06-19 19:07:01 +01001069 do { \
Arun Siluvery83b8a982015-07-08 10:27:05 +01001070 int __index = (index)++; \
1071 if (WARN_ON(__index >= (PAGE_SIZE / sizeof(uint32_t)))) { \
Arun Siluvery17ee9502015-06-19 19:07:01 +01001072 return -ENOSPC; \
1073 } \
Arun Siluvery83b8a982015-07-08 10:27:05 +01001074 batch[__index] = (cmd); \
Arun Siluvery17ee9502015-06-19 19:07:01 +01001075 } while (0)
1076
Arun Siluvery9e000842015-07-03 14:27:31 +01001077
1078/*
1079 * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
1080 * PIPE_CONTROL instruction. This is required for the flush to happen correctly
1081 * but there is a slight complication as this is applied in WA batch where the
1082 * values are only initialized once so we cannot take register value at the
1083 * beginning and reuse it further; hence we save its value to memory, upload a
1084 * constant value with bit21 set and then we restore it back with the saved value.
1085 * To simplify the WA, a constant value is formed by using the default value
1086 * of this register. This shouldn't be a problem because we are only modifying
1087 * it for a short period and this batch in non-premptible. We can ofcourse
1088 * use additional instructions that read the actual value of the register
1089 * at that time and set our bit of interest but it makes the WA complicated.
1090 *
1091 * This WA is also required for Gen9 so extracting as a function avoids
1092 * code duplication.
1093 */
1094static inline int gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *ring,
1095 uint32_t *const batch,
1096 uint32_t index)
1097{
1098 uint32_t l3sqc4_flush = (0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES);
1099
Arun Siluverya4106a72015-07-14 15:01:29 +01001100 /*
1101 * WaDisableLSQCROPERFforOCL:skl
1102 * This WA is implemented in skl_init_clock_gating() but since
1103 * this batch updates GEN8_L3SQCREG4 with default value we need to
1104 * set this bit here to retain the WA during flush.
1105 */
1106 if (IS_SKYLAKE(ring->dev) && INTEL_REVID(ring->dev) <= SKL_REVID_E0)
1107 l3sqc4_flush |= GEN8_LQSC_RO_PERF_DIS;
1108
Arun Siluvery83b8a982015-07-08 10:27:05 +01001109 wa_ctx_emit(batch, index, (MI_STORE_REGISTER_MEM_GEN8(1) |
1110 MI_SRM_LRM_GLOBAL_GTT));
1111 wa_ctx_emit(batch, index, GEN8_L3SQCREG4);
1112 wa_ctx_emit(batch, index, ring->scratch.gtt_offset + 256);
1113 wa_ctx_emit(batch, index, 0);
Arun Siluvery9e000842015-07-03 14:27:31 +01001114
Arun Siluvery83b8a982015-07-08 10:27:05 +01001115 wa_ctx_emit(batch, index, MI_LOAD_REGISTER_IMM(1));
1116 wa_ctx_emit(batch, index, GEN8_L3SQCREG4);
1117 wa_ctx_emit(batch, index, l3sqc4_flush);
Arun Siluvery9e000842015-07-03 14:27:31 +01001118
Arun Siluvery83b8a982015-07-08 10:27:05 +01001119 wa_ctx_emit(batch, index, GFX_OP_PIPE_CONTROL(6));
1120 wa_ctx_emit(batch, index, (PIPE_CONTROL_CS_STALL |
1121 PIPE_CONTROL_DC_FLUSH_ENABLE));
1122 wa_ctx_emit(batch, index, 0);
1123 wa_ctx_emit(batch, index, 0);
1124 wa_ctx_emit(batch, index, 0);
1125 wa_ctx_emit(batch, index, 0);
Arun Siluvery9e000842015-07-03 14:27:31 +01001126
Arun Siluvery83b8a982015-07-08 10:27:05 +01001127 wa_ctx_emit(batch, index, (MI_LOAD_REGISTER_MEM_GEN8(1) |
1128 MI_SRM_LRM_GLOBAL_GTT));
1129 wa_ctx_emit(batch, index, GEN8_L3SQCREG4);
1130 wa_ctx_emit(batch, index, ring->scratch.gtt_offset + 256);
1131 wa_ctx_emit(batch, index, 0);
Arun Siluvery9e000842015-07-03 14:27:31 +01001132
1133 return index;
1134}
1135
Arun Siluvery17ee9502015-06-19 19:07:01 +01001136static inline uint32_t wa_ctx_start(struct i915_wa_ctx_bb *wa_ctx,
1137 uint32_t offset,
1138 uint32_t start_alignment)
1139{
1140 return wa_ctx->offset = ALIGN(offset, start_alignment);
1141}
1142
1143static inline int wa_ctx_end(struct i915_wa_ctx_bb *wa_ctx,
1144 uint32_t offset,
1145 uint32_t size_alignment)
1146{
1147 wa_ctx->size = offset - wa_ctx->offset;
1148
1149 WARN(wa_ctx->size % size_alignment,
1150 "wa_ctx_bb failed sanity checks: size %d is not aligned to %d\n",
1151 wa_ctx->size, size_alignment);
1152 return 0;
1153}
1154
1155/**
1156 * gen8_init_indirectctx_bb() - initialize indirect ctx batch with WA
1157 *
1158 * @ring: only applicable for RCS
1159 * @wa_ctx: structure representing wa_ctx
1160 * offset: specifies start of the batch, should be cache-aligned. This is updated
1161 * with the offset value received as input.
1162 * size: size of the batch in DWORDS but HW expects in terms of cachelines
1163 * @batch: page in which WA are loaded
1164 * @offset: This field specifies the start of the batch, it should be
1165 * cache-aligned otherwise it is adjusted accordingly.
1166 * Typically we only have one indirect_ctx and per_ctx batch buffer which are
1167 * initialized at the beginning and shared across all contexts but this field
1168 * helps us to have multiple batches at different offsets and select them based
1169 * on a criteria. At the moment this batch always start at the beginning of the page
1170 * and at this point we don't have multiple wa_ctx batch buffers.
1171 *
1172 * The number of WA applied are not known at the beginning; we use this field
1173 * to return the no of DWORDS written.
Arun Siluvery4d78c8d2015-06-23 15:50:43 +01001174 *
Arun Siluvery17ee9502015-06-19 19:07:01 +01001175 * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
1176 * so it adds NOOPs as padding to make it cacheline aligned.
1177 * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
1178 * makes a complete batch buffer.
1179 *
1180 * Return: non-zero if we exceed the PAGE_SIZE limit.
1181 */
1182
1183static int gen8_init_indirectctx_bb(struct intel_engine_cs *ring,
1184 struct i915_wa_ctx_bb *wa_ctx,
1185 uint32_t *const batch,
1186 uint32_t *offset)
1187{
Arun Siluvery0160f052015-06-23 15:46:57 +01001188 uint32_t scratch_addr;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001189 uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
1190
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001191 /* WaDisableCtxRestoreArbitration:bdw,chv */
Arun Siluvery83b8a982015-07-08 10:27:05 +01001192 wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_DISABLE);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001193
Arun Siluveryc82435b2015-06-19 18:37:13 +01001194 /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
1195 if (IS_BROADWELL(ring->dev)) {
Arun Siluvery9e000842015-07-03 14:27:31 +01001196 index = gen8_emit_flush_coherentl3_wa(ring, batch, index);
1197 if (index < 0)
1198 return index;
Arun Siluveryc82435b2015-06-19 18:37:13 +01001199 }
1200
Arun Siluvery0160f052015-06-23 15:46:57 +01001201 /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
1202 /* Actual scratch location is at 128 bytes offset */
1203 scratch_addr = ring->scratch.gtt_offset + 2*CACHELINE_BYTES;
1204
Arun Siluvery83b8a982015-07-08 10:27:05 +01001205 wa_ctx_emit(batch, index, GFX_OP_PIPE_CONTROL(6));
1206 wa_ctx_emit(batch, index, (PIPE_CONTROL_FLUSH_L3 |
1207 PIPE_CONTROL_GLOBAL_GTT_IVB |
1208 PIPE_CONTROL_CS_STALL |
1209 PIPE_CONTROL_QW_WRITE));
1210 wa_ctx_emit(batch, index, scratch_addr);
1211 wa_ctx_emit(batch, index, 0);
1212 wa_ctx_emit(batch, index, 0);
1213 wa_ctx_emit(batch, index, 0);
Arun Siluvery0160f052015-06-23 15:46:57 +01001214
Arun Siluvery17ee9502015-06-19 19:07:01 +01001215 /* Pad to end of cacheline */
1216 while (index % CACHELINE_DWORDS)
Arun Siluvery83b8a982015-07-08 10:27:05 +01001217 wa_ctx_emit(batch, index, MI_NOOP);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001218
1219 /*
1220 * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
1221 * execution depends on the length specified in terms of cache lines
1222 * in the register CTX_RCS_INDIRECT_CTX
1223 */
1224
1225 return wa_ctx_end(wa_ctx, *offset = index, CACHELINE_DWORDS);
1226}
1227
1228/**
1229 * gen8_init_perctx_bb() - initialize per ctx batch with WA
1230 *
1231 * @ring: only applicable for RCS
1232 * @wa_ctx: structure representing wa_ctx
1233 * offset: specifies start of the batch, should be cache-aligned.
1234 * size: size of the batch in DWORDS but HW expects in terms of cachelines
Arun Siluvery4d78c8d2015-06-23 15:50:43 +01001235 * @batch: page in which WA are loaded
Arun Siluvery17ee9502015-06-19 19:07:01 +01001236 * @offset: This field specifies the start of this batch.
1237 * This batch is started immediately after indirect_ctx batch. Since we ensure
1238 * that indirect_ctx ends on a cacheline this batch is aligned automatically.
1239 *
1240 * The number of DWORDS written are returned using this field.
1241 *
1242 * This batch is terminated with MI_BATCH_BUFFER_END and so we need not add padding
1243 * to align it with cacheline as padding after MI_BATCH_BUFFER_END is redundant.
1244 */
1245static int gen8_init_perctx_bb(struct intel_engine_cs *ring,
1246 struct i915_wa_ctx_bb *wa_ctx,
1247 uint32_t *const batch,
1248 uint32_t *offset)
1249{
1250 uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
1251
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001252 /* WaDisableCtxRestoreArbitration:bdw,chv */
Arun Siluvery83b8a982015-07-08 10:27:05 +01001253 wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_ENABLE);
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001254
Arun Siluvery83b8a982015-07-08 10:27:05 +01001255 wa_ctx_emit(batch, index, MI_BATCH_BUFFER_END);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001256
1257 return wa_ctx_end(wa_ctx, *offset = index, 1);
1258}
1259
Arun Siluvery0504cff2015-07-14 15:01:27 +01001260static int gen9_init_indirectctx_bb(struct intel_engine_cs *ring,
1261 struct i915_wa_ctx_bb *wa_ctx,
1262 uint32_t *const batch,
1263 uint32_t *offset)
1264{
Arun Siluverya4106a72015-07-14 15:01:29 +01001265 int ret;
Arun Siluvery0907c8f2015-07-14 15:01:28 +01001266 struct drm_device *dev = ring->dev;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001267 uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
1268
Arun Siluvery0907c8f2015-07-14 15:01:28 +01001269 /* WaDisableCtxRestoreArbitration:skl,bxt */
1270 if ((IS_SKYLAKE(dev) && (INTEL_REVID(dev) <= SKL_REVID_D0)) ||
1271 (IS_BROXTON(dev) && (INTEL_REVID(dev) == BXT_REVID_A0)))
1272 wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_DISABLE);
Arun Siluvery0504cff2015-07-14 15:01:27 +01001273
Arun Siluverya4106a72015-07-14 15:01:29 +01001274 /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt */
1275 ret = gen8_emit_flush_coherentl3_wa(ring, batch, index);
1276 if (ret < 0)
1277 return ret;
1278 index = ret;
1279
Arun Siluvery0504cff2015-07-14 15:01:27 +01001280 /* Pad to end of cacheline */
1281 while (index % CACHELINE_DWORDS)
1282 wa_ctx_emit(batch, index, MI_NOOP);
1283
1284 return wa_ctx_end(wa_ctx, *offset = index, CACHELINE_DWORDS);
1285}
1286
1287static int gen9_init_perctx_bb(struct intel_engine_cs *ring,
1288 struct i915_wa_ctx_bb *wa_ctx,
1289 uint32_t *const batch,
1290 uint32_t *offset)
1291{
Arun Siluvery0907c8f2015-07-14 15:01:28 +01001292 struct drm_device *dev = ring->dev;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001293 uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
1294
Arun Siluvery0907c8f2015-07-14 15:01:28 +01001295 /* WaDisableCtxRestoreArbitration:skl,bxt */
1296 if ((IS_SKYLAKE(dev) && (INTEL_REVID(dev) <= SKL_REVID_D0)) ||
1297 (IS_BROXTON(dev) && (INTEL_REVID(dev) == BXT_REVID_A0)))
1298 wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_ENABLE);
1299
Arun Siluvery0504cff2015-07-14 15:01:27 +01001300 wa_ctx_emit(batch, index, MI_BATCH_BUFFER_END);
1301
1302 return wa_ctx_end(wa_ctx, *offset = index, 1);
1303}
1304
Arun Siluvery17ee9502015-06-19 19:07:01 +01001305static int lrc_setup_wa_ctx_obj(struct intel_engine_cs *ring, u32 size)
1306{
1307 int ret;
1308
1309 ring->wa_ctx.obj = i915_gem_alloc_object(ring->dev, PAGE_ALIGN(size));
1310 if (!ring->wa_ctx.obj) {
1311 DRM_DEBUG_DRIVER("alloc LRC WA ctx backing obj failed.\n");
1312 return -ENOMEM;
1313 }
1314
1315 ret = i915_gem_obj_ggtt_pin(ring->wa_ctx.obj, PAGE_SIZE, 0);
1316 if (ret) {
1317 DRM_DEBUG_DRIVER("pin LRC WA ctx backing obj failed: %d\n",
1318 ret);
1319 drm_gem_object_unreference(&ring->wa_ctx.obj->base);
1320 return ret;
1321 }
1322
1323 return 0;
1324}
1325
1326static void lrc_destroy_wa_ctx_obj(struct intel_engine_cs *ring)
1327{
1328 if (ring->wa_ctx.obj) {
1329 i915_gem_object_ggtt_unpin(ring->wa_ctx.obj);
1330 drm_gem_object_unreference(&ring->wa_ctx.obj->base);
1331 ring->wa_ctx.obj = NULL;
1332 }
1333}
1334
1335static int intel_init_workaround_bb(struct intel_engine_cs *ring)
1336{
1337 int ret;
1338 uint32_t *batch;
1339 uint32_t offset;
1340 struct page *page;
1341 struct i915_ctx_workarounds *wa_ctx = &ring->wa_ctx;
1342
1343 WARN_ON(ring->id != RCS);
1344
Arun Siluvery5e60d792015-06-23 15:50:44 +01001345 /* update this when WA for higher Gen are added */
Arun Siluvery0504cff2015-07-14 15:01:27 +01001346 if (INTEL_INFO(ring->dev)->gen > 9) {
1347 DRM_ERROR("WA batch buffer is not initialized for Gen%d\n",
1348 INTEL_INFO(ring->dev)->gen);
Arun Siluvery5e60d792015-06-23 15:50:44 +01001349 return 0;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001350 }
Arun Siluvery5e60d792015-06-23 15:50:44 +01001351
Arun Siluveryc4db7592015-06-19 18:37:11 +01001352 /* some WA perform writes to scratch page, ensure it is valid */
1353 if (ring->scratch.obj == NULL) {
1354 DRM_ERROR("scratch page not allocated for %s\n", ring->name);
1355 return -EINVAL;
1356 }
1357
Arun Siluvery17ee9502015-06-19 19:07:01 +01001358 ret = lrc_setup_wa_ctx_obj(ring, PAGE_SIZE);
1359 if (ret) {
1360 DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
1361 return ret;
1362 }
1363
1364 page = i915_gem_object_get_page(wa_ctx->obj, 0);
1365 batch = kmap_atomic(page);
1366 offset = 0;
1367
1368 if (INTEL_INFO(ring->dev)->gen == 8) {
1369 ret = gen8_init_indirectctx_bb(ring,
1370 &wa_ctx->indirect_ctx,
1371 batch,
1372 &offset);
1373 if (ret)
1374 goto out;
1375
1376 ret = gen8_init_perctx_bb(ring,
1377 &wa_ctx->per_ctx,
1378 batch,
1379 &offset);
1380 if (ret)
1381 goto out;
Arun Siluvery0504cff2015-07-14 15:01:27 +01001382 } else if (INTEL_INFO(ring->dev)->gen == 9) {
1383 ret = gen9_init_indirectctx_bb(ring,
1384 &wa_ctx->indirect_ctx,
1385 batch,
1386 &offset);
1387 if (ret)
1388 goto out;
1389
1390 ret = gen9_init_perctx_bb(ring,
1391 &wa_ctx->per_ctx,
1392 batch,
1393 &offset);
1394 if (ret)
1395 goto out;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001396 }
1397
1398out:
1399 kunmap_atomic(batch);
1400 if (ret)
1401 lrc_destroy_wa_ctx_obj(ring);
1402
1403 return ret;
1404}
1405
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001406static int gen8_init_common_ring(struct intel_engine_cs *ring)
1407{
1408 struct drm_device *dev = ring->dev;
1409 struct drm_i915_private *dev_priv = dev->dev_private;
1410
Oscar Mateo73d477f2014-07-24 17:04:31 +01001411 I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
1412 I915_WRITE(RING_HWSTAM(ring->mmio_base), 0xffffffff);
1413
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001414 I915_WRITE(RING_MODE_GEN7(ring),
1415 _MASKED_BIT_DISABLE(GFX_REPLAY_MODE) |
1416 _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
1417 POSTING_READ(RING_MODE_GEN7(ring));
Thomas Danielc0a03a22015-01-09 11:09:37 +00001418 ring->next_context_status_buffer = 0;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001419 DRM_DEBUG_DRIVER("Execlists enabled for %s\n", ring->name);
1420
1421 memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
1422
1423 return 0;
1424}
1425
1426static int gen8_init_render_ring(struct intel_engine_cs *ring)
1427{
1428 struct drm_device *dev = ring->dev;
1429 struct drm_i915_private *dev_priv = dev->dev_private;
1430 int ret;
1431
1432 ret = gen8_init_common_ring(ring);
1433 if (ret)
1434 return ret;
1435
1436 /* We need to disable the AsyncFlip performance optimisations in order
1437 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1438 * programmed to '1' on all products.
1439 *
1440 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
1441 */
1442 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1443
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001444 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1445
Michel Thierry771b9a52014-11-11 16:47:33 +00001446 return init_workarounds_ring(ring);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001447}
1448
Damien Lespiau82ef8222015-02-09 19:33:08 +00001449static int gen9_init_render_ring(struct intel_engine_cs *ring)
1450{
1451 int ret;
1452
1453 ret = gen8_init_common_ring(ring);
1454 if (ret)
1455 return ret;
1456
1457 return init_workarounds_ring(ring);
1458}
1459
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001460static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request *req)
1461{
1462 struct i915_hw_ppgtt *ppgtt = req->ctx->ppgtt;
1463 struct intel_engine_cs *ring = req->ring;
1464 struct intel_ringbuffer *ringbuf = req->ringbuf;
1465 const int num_lri_cmds = GEN8_LEGACY_PDPES * 2;
1466 int i, ret;
1467
1468 ret = intel_logical_ring_begin(req, num_lri_cmds * 2 + 2);
1469 if (ret)
1470 return ret;
1471
1472 intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(num_lri_cmds));
1473 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
1474 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
1475
1476 intel_logical_ring_emit(ringbuf, GEN8_RING_PDP_UDW(ring, i));
1477 intel_logical_ring_emit(ringbuf, upper_32_bits(pd_daddr));
1478 intel_logical_ring_emit(ringbuf, GEN8_RING_PDP_LDW(ring, i));
1479 intel_logical_ring_emit(ringbuf, lower_32_bits(pd_daddr));
1480 }
1481
1482 intel_logical_ring_emit(ringbuf, MI_NOOP);
1483 intel_logical_ring_advance(ringbuf);
1484
1485 return 0;
1486}
1487
John Harrisonbe795fc2015-05-29 17:44:03 +01001488static int gen8_emit_bb_start(struct drm_i915_gem_request *req,
John Harrison8e004ef2015-02-13 11:48:10 +00001489 u64 offset, unsigned dispatch_flags)
Oscar Mateo15648582014-07-24 17:04:32 +01001490{
John Harrisonbe795fc2015-05-29 17:44:03 +01001491 struct intel_ringbuffer *ringbuf = req->ringbuf;
John Harrison8e004ef2015-02-13 11:48:10 +00001492 bool ppgtt = !(dispatch_flags & I915_DISPATCH_SECURE);
Oscar Mateo15648582014-07-24 17:04:32 +01001493 int ret;
1494
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001495 /* Don't rely in hw updating PDPs, specially in lite-restore.
1496 * Ideally, we should set Force PD Restore in ctx descriptor,
1497 * but we can't. Force Restore would be a second option, but
1498 * it is unsafe in case of lite-restore (because the ctx is
1499 * not idle). */
1500 if (req->ctx->ppgtt &&
1501 (intel_ring_flag(req->ring) & req->ctx->ppgtt->pd_dirty_rings)) {
1502 ret = intel_logical_ring_emit_pdps(req);
1503 if (ret)
1504 return ret;
1505
1506 req->ctx->ppgtt->pd_dirty_rings &= ~intel_ring_flag(req->ring);
1507 }
1508
John Harrison4d616a22015-05-29 17:44:08 +01001509 ret = intel_logical_ring_begin(req, 4);
Oscar Mateo15648582014-07-24 17:04:32 +01001510 if (ret)
1511 return ret;
1512
1513 /* FIXME(BDW): Address space and security selectors. */
Abdiel Janulgue69225282015-06-16 13:39:42 +03001514 intel_logical_ring_emit(ringbuf, MI_BATCH_BUFFER_START_GEN8 |
1515 (ppgtt<<8) |
1516 (dispatch_flags & I915_DISPATCH_RS ?
1517 MI_BATCH_RESOURCE_STREAMER : 0));
Oscar Mateo15648582014-07-24 17:04:32 +01001518 intel_logical_ring_emit(ringbuf, lower_32_bits(offset));
1519 intel_logical_ring_emit(ringbuf, upper_32_bits(offset));
1520 intel_logical_ring_emit(ringbuf, MI_NOOP);
1521 intel_logical_ring_advance(ringbuf);
1522
1523 return 0;
1524}
1525
Oscar Mateo73d477f2014-07-24 17:04:31 +01001526static bool gen8_logical_ring_get_irq(struct intel_engine_cs *ring)
1527{
1528 struct drm_device *dev = ring->dev;
1529 struct drm_i915_private *dev_priv = dev->dev_private;
1530 unsigned long flags;
1531
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001532 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Oscar Mateo73d477f2014-07-24 17:04:31 +01001533 return false;
1534
1535 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1536 if (ring->irq_refcount++ == 0) {
1537 I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
1538 POSTING_READ(RING_IMR(ring->mmio_base));
1539 }
1540 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1541
1542 return true;
1543}
1544
1545static void gen8_logical_ring_put_irq(struct intel_engine_cs *ring)
1546{
1547 struct drm_device *dev = ring->dev;
1548 struct drm_i915_private *dev_priv = dev->dev_private;
1549 unsigned long flags;
1550
1551 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1552 if (--ring->irq_refcount == 0) {
1553 I915_WRITE_IMR(ring, ~ring->irq_keep_mask);
1554 POSTING_READ(RING_IMR(ring->mmio_base));
1555 }
1556 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1557}
1558
John Harrison7deb4d32015-05-29 17:43:59 +01001559static int gen8_emit_flush(struct drm_i915_gem_request *request,
Oscar Mateo47122742014-07-24 17:04:28 +01001560 u32 invalidate_domains,
1561 u32 unused)
1562{
John Harrison7deb4d32015-05-29 17:43:59 +01001563 struct intel_ringbuffer *ringbuf = request->ringbuf;
Oscar Mateo47122742014-07-24 17:04:28 +01001564 struct intel_engine_cs *ring = ringbuf->ring;
1565 struct drm_device *dev = ring->dev;
1566 struct drm_i915_private *dev_priv = dev->dev_private;
1567 uint32_t cmd;
1568 int ret;
1569
John Harrison4d616a22015-05-29 17:44:08 +01001570 ret = intel_logical_ring_begin(request, 4);
Oscar Mateo47122742014-07-24 17:04:28 +01001571 if (ret)
1572 return ret;
1573
1574 cmd = MI_FLUSH_DW + 1;
1575
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001576 /* We always require a command barrier so that subsequent
1577 * commands, such as breadcrumb interrupts, are strictly ordered
1578 * wrt the contents of the write cache being flushed to memory
1579 * (and thus being coherent from the CPU).
1580 */
1581 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1582
1583 if (invalidate_domains & I915_GEM_GPU_DOMAINS) {
1584 cmd |= MI_INVALIDATE_TLB;
1585 if (ring == &dev_priv->ring[VCS])
1586 cmd |= MI_INVALIDATE_BSD;
Oscar Mateo47122742014-07-24 17:04:28 +01001587 }
1588
1589 intel_logical_ring_emit(ringbuf, cmd);
1590 intel_logical_ring_emit(ringbuf,
1591 I915_GEM_HWS_SCRATCH_ADDR |
1592 MI_FLUSH_DW_USE_GTT);
1593 intel_logical_ring_emit(ringbuf, 0); /* upper addr */
1594 intel_logical_ring_emit(ringbuf, 0); /* value */
1595 intel_logical_ring_advance(ringbuf);
1596
1597 return 0;
1598}
1599
John Harrison7deb4d32015-05-29 17:43:59 +01001600static int gen8_emit_flush_render(struct drm_i915_gem_request *request,
Oscar Mateo47122742014-07-24 17:04:28 +01001601 u32 invalidate_domains,
1602 u32 flush_domains)
1603{
John Harrison7deb4d32015-05-29 17:43:59 +01001604 struct intel_ringbuffer *ringbuf = request->ringbuf;
Oscar Mateo47122742014-07-24 17:04:28 +01001605 struct intel_engine_cs *ring = ringbuf->ring;
1606 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Imre Deak9647ff32015-01-25 13:27:11 -08001607 bool vf_flush_wa;
Oscar Mateo47122742014-07-24 17:04:28 +01001608 u32 flags = 0;
1609 int ret;
1610
1611 flags |= PIPE_CONTROL_CS_STALL;
1612
1613 if (flush_domains) {
1614 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
1615 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
1616 }
1617
1618 if (invalidate_domains) {
1619 flags |= PIPE_CONTROL_TLB_INVALIDATE;
1620 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
1621 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
1622 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
1623 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
1624 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
1625 flags |= PIPE_CONTROL_QW_WRITE;
1626 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
1627 }
1628
Imre Deak9647ff32015-01-25 13:27:11 -08001629 /*
1630 * On GEN9+ Before VF_CACHE_INVALIDATE we need to emit a NULL pipe
1631 * control.
1632 */
1633 vf_flush_wa = INTEL_INFO(ring->dev)->gen >= 9 &&
1634 flags & PIPE_CONTROL_VF_CACHE_INVALIDATE;
1635
John Harrison4d616a22015-05-29 17:44:08 +01001636 ret = intel_logical_ring_begin(request, vf_flush_wa ? 12 : 6);
Oscar Mateo47122742014-07-24 17:04:28 +01001637 if (ret)
1638 return ret;
1639
Imre Deak9647ff32015-01-25 13:27:11 -08001640 if (vf_flush_wa) {
1641 intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
1642 intel_logical_ring_emit(ringbuf, 0);
1643 intel_logical_ring_emit(ringbuf, 0);
1644 intel_logical_ring_emit(ringbuf, 0);
1645 intel_logical_ring_emit(ringbuf, 0);
1646 intel_logical_ring_emit(ringbuf, 0);
1647 }
1648
Oscar Mateo47122742014-07-24 17:04:28 +01001649 intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
1650 intel_logical_ring_emit(ringbuf, flags);
1651 intel_logical_ring_emit(ringbuf, scratch_addr);
1652 intel_logical_ring_emit(ringbuf, 0);
1653 intel_logical_ring_emit(ringbuf, 0);
1654 intel_logical_ring_emit(ringbuf, 0);
1655 intel_logical_ring_advance(ringbuf);
1656
1657 return 0;
1658}
1659
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001660static u32 gen8_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
1661{
1662 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1663}
1664
1665static void gen8_set_seqno(struct intel_engine_cs *ring, u32 seqno)
1666{
1667 intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
1668}
1669
John Harrisonc4e76632015-05-29 17:44:01 +01001670static int gen8_emit_request(struct drm_i915_gem_request *request)
Oscar Mateo4da46e12014-07-24 17:04:27 +01001671{
John Harrisonc4e76632015-05-29 17:44:01 +01001672 struct intel_ringbuffer *ringbuf = request->ringbuf;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001673 struct intel_engine_cs *ring = ringbuf->ring;
1674 u32 cmd;
1675 int ret;
1676
Michel Thierry53292cd2015-04-15 18:11:33 +01001677 /*
1678 * Reserve space for 2 NOOPs at the end of each request to be
1679 * used as a workaround for not being allowed to do lite
1680 * restore with HEAD==TAIL (WaIdleLiteRestore).
1681 */
John Harrison4d616a22015-05-29 17:44:08 +01001682 ret = intel_logical_ring_begin(request, 8);
Oscar Mateo4da46e12014-07-24 17:04:27 +01001683 if (ret)
1684 return ret;
1685
Ville Syrjälä8edfbb82014-11-14 18:16:56 +02001686 cmd = MI_STORE_DWORD_IMM_GEN4;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001687 cmd |= MI_GLOBAL_GTT;
1688
1689 intel_logical_ring_emit(ringbuf, cmd);
1690 intel_logical_ring_emit(ringbuf,
1691 (ring->status_page.gfx_addr +
1692 (I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT)));
1693 intel_logical_ring_emit(ringbuf, 0);
John Harrisonc4e76632015-05-29 17:44:01 +01001694 intel_logical_ring_emit(ringbuf, i915_gem_request_get_seqno(request));
Oscar Mateo4da46e12014-07-24 17:04:27 +01001695 intel_logical_ring_emit(ringbuf, MI_USER_INTERRUPT);
1696 intel_logical_ring_emit(ringbuf, MI_NOOP);
John Harrisonae707972015-05-29 17:44:14 +01001697 intel_logical_ring_advance_and_submit(request);
Oscar Mateo4da46e12014-07-24 17:04:27 +01001698
Michel Thierry53292cd2015-04-15 18:11:33 +01001699 /*
1700 * Here we add two extra NOOPs as padding to avoid
1701 * lite restore of a context with HEAD==TAIL.
1702 */
1703 intel_logical_ring_emit(ringbuf, MI_NOOP);
1704 intel_logical_ring_emit(ringbuf, MI_NOOP);
1705 intel_logical_ring_advance(ringbuf);
1706
Oscar Mateo4da46e12014-07-24 17:04:27 +01001707 return 0;
1708}
1709
John Harrisonbe013632015-05-29 17:43:45 +01001710static int intel_lr_context_render_state_init(struct drm_i915_gem_request *req)
Damien Lespiaucef437a2015-02-10 19:32:19 +00001711{
Damien Lespiaucef437a2015-02-10 19:32:19 +00001712 struct render_state so;
Damien Lespiaucef437a2015-02-10 19:32:19 +00001713 int ret;
1714
John Harrisonbe013632015-05-29 17:43:45 +01001715 ret = i915_gem_render_state_prepare(req->ring, &so);
Damien Lespiaucef437a2015-02-10 19:32:19 +00001716 if (ret)
1717 return ret;
1718
1719 if (so.rodata == NULL)
1720 return 0;
1721
John Harrisonbe795fc2015-05-29 17:44:03 +01001722 ret = req->ring->emit_bb_start(req, so.ggtt_offset,
John Harrisonbe013632015-05-29 17:43:45 +01001723 I915_DISPATCH_SECURE);
Damien Lespiaucef437a2015-02-10 19:32:19 +00001724 if (ret)
1725 goto out;
1726
John Harrisonb2af0372015-05-29 17:43:50 +01001727 i915_vma_move_to_active(i915_gem_obj_to_ggtt(so.obj), req);
Damien Lespiaucef437a2015-02-10 19:32:19 +00001728
Damien Lespiaucef437a2015-02-10 19:32:19 +00001729out:
1730 i915_gem_render_state_fini(&so);
1731 return ret;
1732}
1733
John Harrison87531812015-05-29 17:43:44 +01001734static int gen8_init_rcs_context(struct drm_i915_gem_request *req)
Thomas Daniele7778be2014-12-02 12:50:48 +00001735{
1736 int ret;
1737
John Harrisone2be4fa2015-05-29 17:43:54 +01001738 ret = intel_logical_ring_workarounds_emit(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001739 if (ret)
1740 return ret;
1741
Peter Antoine3bbaba02015-07-10 20:13:11 +03001742 ret = intel_rcs_context_init_mocs(req);
1743 /*
1744 * Failing to program the MOCS is non-fatal.The system will not
1745 * run at peak performance. So generate an error and carry on.
1746 */
1747 if (ret)
1748 DRM_ERROR("MOCS failed to program: expect performance issues.\n");
1749
John Harrisonbe013632015-05-29 17:43:45 +01001750 return intel_lr_context_render_state_init(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001751}
1752
Oscar Mateo73e4d072014-07-24 17:04:48 +01001753/**
1754 * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
1755 *
1756 * @ring: Engine Command Streamer.
1757 *
1758 */
Oscar Mateo454afeb2014-07-24 17:04:22 +01001759void intel_logical_ring_cleanup(struct intel_engine_cs *ring)
1760{
John Harrison6402c332014-10-31 12:00:26 +00001761 struct drm_i915_private *dev_priv;
Oscar Mateo9832b9d2014-07-24 17:04:30 +01001762
Oscar Mateo48d82382014-07-24 17:04:23 +01001763 if (!intel_ring_initialized(ring))
1764 return;
1765
John Harrison6402c332014-10-31 12:00:26 +00001766 dev_priv = ring->dev->dev_private;
1767
Oscar Mateo9832b9d2014-07-24 17:04:30 +01001768 intel_logical_ring_stop(ring);
1769 WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
Oscar Mateo48d82382014-07-24 17:04:23 +01001770
1771 if (ring->cleanup)
1772 ring->cleanup(ring);
1773
1774 i915_cmd_parser_fini_ring(ring);
Chris Wilson06fbca72015-04-07 16:20:36 +01001775 i915_gem_batch_pool_fini(&ring->batch_pool);
Oscar Mateo48d82382014-07-24 17:04:23 +01001776
1777 if (ring->status_page.obj) {
1778 kunmap(sg_page(ring->status_page.obj->pages->sgl));
1779 ring->status_page.obj = NULL;
1780 }
Arun Siluvery17ee9502015-06-19 19:07:01 +01001781
1782 lrc_destroy_wa_ctx_obj(ring);
Oscar Mateo454afeb2014-07-24 17:04:22 +01001783}
1784
1785static int logical_ring_init(struct drm_device *dev, struct intel_engine_cs *ring)
1786{
Oscar Mateo48d82382014-07-24 17:04:23 +01001787 int ret;
Oscar Mateo48d82382014-07-24 17:04:23 +01001788
1789 /* Intentionally left blank. */
1790 ring->buffer = NULL;
1791
1792 ring->dev = dev;
1793 INIT_LIST_HEAD(&ring->active_list);
1794 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson06fbca72015-04-07 16:20:36 +01001795 i915_gem_batch_pool_init(dev, &ring->batch_pool);
Oscar Mateo48d82382014-07-24 17:04:23 +01001796 init_waitqueue_head(&ring->irq_queue);
1797
Michel Thierryacdd8842014-07-24 17:04:38 +01001798 INIT_LIST_HEAD(&ring->execlist_queue);
Thomas Danielc86ee3a92014-11-13 10:27:05 +00001799 INIT_LIST_HEAD(&ring->execlist_retired_req_list);
Michel Thierryacdd8842014-07-24 17:04:38 +01001800 spin_lock_init(&ring->execlist_lock);
1801
Oscar Mateo48d82382014-07-24 17:04:23 +01001802 ret = i915_cmd_parser_init_ring(ring);
1803 if (ret)
1804 return ret;
1805
Oscar Mateo564ddb22014-08-21 11:40:54 +01001806 ret = intel_lr_context_deferred_create(ring->default_context, ring);
1807
1808 return ret;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001809}
1810
1811static int logical_render_ring_init(struct drm_device *dev)
1812{
1813 struct drm_i915_private *dev_priv = dev->dev_private;
1814 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Daniel Vetter99be1df2014-11-20 00:33:06 +01001815 int ret;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001816
1817 ring->name = "render ring";
1818 ring->id = RCS;
1819 ring->mmio_base = RENDER_RING_BASE;
1820 ring->irq_enable_mask =
1821 GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001822 ring->irq_keep_mask =
1823 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
1824 if (HAS_L3_DPF(dev))
1825 ring->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001826
Damien Lespiau82ef8222015-02-09 19:33:08 +00001827 if (INTEL_INFO(dev)->gen >= 9)
1828 ring->init_hw = gen9_init_render_ring;
1829 else
1830 ring->init_hw = gen8_init_render_ring;
Thomas Daniele7778be2014-12-02 12:50:48 +00001831 ring->init_context = gen8_init_rcs_context;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001832 ring->cleanup = intel_fini_pipe_control;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001833 ring->get_seqno = gen8_get_seqno;
1834 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001835 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001836 ring->emit_flush = gen8_emit_flush_render;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001837 ring->irq_get = gen8_logical_ring_get_irq;
1838 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001839 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001840
Daniel Vetter99be1df2014-11-20 00:33:06 +01001841 ring->dev = dev;
Arun Siluveryc4db7592015-06-19 18:37:11 +01001842
1843 ret = intel_init_pipe_control(ring);
Daniel Vetter99be1df2014-11-20 00:33:06 +01001844 if (ret)
1845 return ret;
1846
Arun Siluvery17ee9502015-06-19 19:07:01 +01001847 ret = intel_init_workaround_bb(ring);
1848 if (ret) {
1849 /*
1850 * We continue even if we fail to initialize WA batch
1851 * because we only expect rare glitches but nothing
1852 * critical to prevent us from using GPU
1853 */
1854 DRM_ERROR("WA batch buffer initialization failed: %d\n",
1855 ret);
1856 }
1857
Arun Siluveryc4db7592015-06-19 18:37:11 +01001858 ret = logical_ring_init(dev, ring);
1859 if (ret) {
Arun Siluvery17ee9502015-06-19 19:07:01 +01001860 lrc_destroy_wa_ctx_obj(ring);
Arun Siluveryc4db7592015-06-19 18:37:11 +01001861 }
Arun Siluvery17ee9502015-06-19 19:07:01 +01001862
1863 return ret;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001864}
1865
1866static int logical_bsd_ring_init(struct drm_device *dev)
1867{
1868 struct drm_i915_private *dev_priv = dev->dev_private;
1869 struct intel_engine_cs *ring = &dev_priv->ring[VCS];
1870
1871 ring->name = "bsd ring";
1872 ring->id = VCS;
1873 ring->mmio_base = GEN6_BSD_RING_BASE;
1874 ring->irq_enable_mask =
1875 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001876 ring->irq_keep_mask =
1877 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001878
Daniel Vetterecfe00d2014-11-20 00:33:04 +01001879 ring->init_hw = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001880 ring->get_seqno = gen8_get_seqno;
1881 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001882 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001883 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001884 ring->irq_get = gen8_logical_ring_get_irq;
1885 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001886 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001887
Oscar Mateo454afeb2014-07-24 17:04:22 +01001888 return logical_ring_init(dev, ring);
1889}
1890
1891static int logical_bsd2_ring_init(struct drm_device *dev)
1892{
1893 struct drm_i915_private *dev_priv = dev->dev_private;
1894 struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
1895
1896 ring->name = "bds2 ring";
1897 ring->id = VCS2;
1898 ring->mmio_base = GEN8_BSD2_RING_BASE;
1899 ring->irq_enable_mask =
1900 GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001901 ring->irq_keep_mask =
1902 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001903
Daniel Vetterecfe00d2014-11-20 00:33:04 +01001904 ring->init_hw = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001905 ring->get_seqno = gen8_get_seqno;
1906 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001907 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001908 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001909 ring->irq_get = gen8_logical_ring_get_irq;
1910 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001911 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001912
Oscar Mateo454afeb2014-07-24 17:04:22 +01001913 return logical_ring_init(dev, ring);
1914}
1915
1916static int logical_blt_ring_init(struct drm_device *dev)
1917{
1918 struct drm_i915_private *dev_priv = dev->dev_private;
1919 struct intel_engine_cs *ring = &dev_priv->ring[BCS];
1920
1921 ring->name = "blitter ring";
1922 ring->id = BCS;
1923 ring->mmio_base = BLT_RING_BASE;
1924 ring->irq_enable_mask =
1925 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001926 ring->irq_keep_mask =
1927 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001928
Daniel Vetterecfe00d2014-11-20 00:33:04 +01001929 ring->init_hw = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001930 ring->get_seqno = gen8_get_seqno;
1931 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001932 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001933 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001934 ring->irq_get = gen8_logical_ring_get_irq;
1935 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001936 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001937
Oscar Mateo454afeb2014-07-24 17:04:22 +01001938 return logical_ring_init(dev, ring);
1939}
1940
1941static int logical_vebox_ring_init(struct drm_device *dev)
1942{
1943 struct drm_i915_private *dev_priv = dev->dev_private;
1944 struct intel_engine_cs *ring = &dev_priv->ring[VECS];
1945
1946 ring->name = "video enhancement ring";
1947 ring->id = VECS;
1948 ring->mmio_base = VEBOX_RING_BASE;
1949 ring->irq_enable_mask =
1950 GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001951 ring->irq_keep_mask =
1952 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001953
Daniel Vetterecfe00d2014-11-20 00:33:04 +01001954 ring->init_hw = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001955 ring->get_seqno = gen8_get_seqno;
1956 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001957 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001958 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001959 ring->irq_get = gen8_logical_ring_get_irq;
1960 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001961 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001962
Oscar Mateo454afeb2014-07-24 17:04:22 +01001963 return logical_ring_init(dev, ring);
1964}
1965
Oscar Mateo73e4d072014-07-24 17:04:48 +01001966/**
1967 * intel_logical_rings_init() - allocate, populate and init the Engine Command Streamers
1968 * @dev: DRM device.
1969 *
1970 * This function inits the engines for an Execlists submission style (the equivalent in the
1971 * legacy ringbuffer submission world would be i915_gem_init_rings). It does it only for
1972 * those engines that are present in the hardware.
1973 *
1974 * Return: non-zero if the initialization failed.
1975 */
Oscar Mateo454afeb2014-07-24 17:04:22 +01001976int intel_logical_rings_init(struct drm_device *dev)
1977{
1978 struct drm_i915_private *dev_priv = dev->dev_private;
1979 int ret;
1980
1981 ret = logical_render_ring_init(dev);
1982 if (ret)
1983 return ret;
1984
1985 if (HAS_BSD(dev)) {
1986 ret = logical_bsd_ring_init(dev);
1987 if (ret)
1988 goto cleanup_render_ring;
1989 }
1990
1991 if (HAS_BLT(dev)) {
1992 ret = logical_blt_ring_init(dev);
1993 if (ret)
1994 goto cleanup_bsd_ring;
1995 }
1996
1997 if (HAS_VEBOX(dev)) {
1998 ret = logical_vebox_ring_init(dev);
1999 if (ret)
2000 goto cleanup_blt_ring;
2001 }
2002
2003 if (HAS_BSD2(dev)) {
2004 ret = logical_bsd2_ring_init(dev);
2005 if (ret)
2006 goto cleanup_vebox_ring;
2007 }
2008
2009 ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
2010 if (ret)
2011 goto cleanup_bsd2_ring;
2012
2013 return 0;
2014
2015cleanup_bsd2_ring:
2016 intel_logical_ring_cleanup(&dev_priv->ring[VCS2]);
2017cleanup_vebox_ring:
2018 intel_logical_ring_cleanup(&dev_priv->ring[VECS]);
2019cleanup_blt_ring:
2020 intel_logical_ring_cleanup(&dev_priv->ring[BCS]);
2021cleanup_bsd_ring:
2022 intel_logical_ring_cleanup(&dev_priv->ring[VCS]);
2023cleanup_render_ring:
2024 intel_logical_ring_cleanup(&dev_priv->ring[RCS]);
2025
2026 return ret;
2027}
2028
Jeff McGee0cea6502015-02-13 10:27:56 -06002029static u32
2030make_rpcs(struct drm_device *dev)
2031{
2032 u32 rpcs = 0;
2033
2034 /*
2035 * No explicit RPCS request is needed to ensure full
2036 * slice/subslice/EU enablement prior to Gen9.
2037 */
2038 if (INTEL_INFO(dev)->gen < 9)
2039 return 0;
2040
2041 /*
2042 * Starting in Gen9, render power gating can leave
2043 * slice/subslice/EU in a partially enabled state. We
2044 * must make an explicit request through RPCS for full
2045 * enablement.
2046 */
2047 if (INTEL_INFO(dev)->has_slice_pg) {
2048 rpcs |= GEN8_RPCS_S_CNT_ENABLE;
2049 rpcs |= INTEL_INFO(dev)->slice_total <<
2050 GEN8_RPCS_S_CNT_SHIFT;
2051 rpcs |= GEN8_RPCS_ENABLE;
2052 }
2053
2054 if (INTEL_INFO(dev)->has_subslice_pg) {
2055 rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
2056 rpcs |= INTEL_INFO(dev)->subslice_per_slice <<
2057 GEN8_RPCS_SS_CNT_SHIFT;
2058 rpcs |= GEN8_RPCS_ENABLE;
2059 }
2060
2061 if (INTEL_INFO(dev)->has_eu_pg) {
2062 rpcs |= INTEL_INFO(dev)->eu_per_subslice <<
2063 GEN8_RPCS_EU_MIN_SHIFT;
2064 rpcs |= INTEL_INFO(dev)->eu_per_subslice <<
2065 GEN8_RPCS_EU_MAX_SHIFT;
2066 rpcs |= GEN8_RPCS_ENABLE;
2067 }
2068
2069 return rpcs;
2070}
2071
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002072static int
2073populate_lr_context(struct intel_context *ctx, struct drm_i915_gem_object *ctx_obj,
2074 struct intel_engine_cs *ring, struct intel_ringbuffer *ringbuf)
2075{
Thomas Daniel2d965532014-08-19 10:13:36 +01002076 struct drm_device *dev = ring->dev;
2077 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterae6c4802014-08-06 15:04:53 +02002078 struct i915_hw_ppgtt *ppgtt = ctx->ppgtt;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002079 struct page *page;
2080 uint32_t *reg_state;
2081 int ret;
2082
Thomas Daniel2d965532014-08-19 10:13:36 +01002083 if (!ppgtt)
2084 ppgtt = dev_priv->mm.aliasing_ppgtt;
2085
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002086 ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
2087 if (ret) {
2088 DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
2089 return ret;
2090 }
2091
2092 ret = i915_gem_object_get_pages(ctx_obj);
2093 if (ret) {
2094 DRM_DEBUG_DRIVER("Could not get object pages\n");
2095 return ret;
2096 }
2097
2098 i915_gem_object_pin_pages(ctx_obj);
2099
2100 /* The second page of the context object contains some fields which must
2101 * be set up prior to the first execution. */
2102 page = i915_gem_object_get_page(ctx_obj, 1);
2103 reg_state = kmap_atomic(page);
2104
2105 /* A context is actually a big batch buffer with several MI_LOAD_REGISTER_IMM
2106 * commands followed by (reg, value) pairs. The values we are setting here are
2107 * only for the first context restore: on a subsequent save, the GPU will
2108 * recreate this batchbuffer with new values (including all the missing
2109 * MI_LOAD_REGISTER_IMM commands that we are not initializing here). */
2110 if (ring->id == RCS)
2111 reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(14);
2112 else
2113 reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(11);
2114 reg_state[CTX_LRI_HEADER_0] |= MI_LRI_FORCE_POSTED;
2115 reg_state[CTX_CONTEXT_CONTROL] = RING_CONTEXT_CONTROL(ring);
2116 reg_state[CTX_CONTEXT_CONTROL+1] =
Zhi Wang5baa22c52015-02-10 17:11:36 +08002117 _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
Abdiel Janulgue69225282015-06-16 13:39:42 +03002118 CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
2119 CTX_CTRL_RS_CTX_ENABLE);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002120 reg_state[CTX_RING_HEAD] = RING_HEAD(ring->mmio_base);
2121 reg_state[CTX_RING_HEAD+1] = 0;
2122 reg_state[CTX_RING_TAIL] = RING_TAIL(ring->mmio_base);
2123 reg_state[CTX_RING_TAIL+1] = 0;
2124 reg_state[CTX_RING_BUFFER_START] = RING_START(ring->mmio_base);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002125 /* Ring buffer start address is not known until the buffer is pinned.
2126 * It is written to the context image in execlists_update_context()
2127 */
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002128 reg_state[CTX_RING_BUFFER_CONTROL] = RING_CTL(ring->mmio_base);
2129 reg_state[CTX_RING_BUFFER_CONTROL+1] =
2130 ((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES) | RING_VALID;
2131 reg_state[CTX_BB_HEAD_U] = ring->mmio_base + 0x168;
2132 reg_state[CTX_BB_HEAD_U+1] = 0;
2133 reg_state[CTX_BB_HEAD_L] = ring->mmio_base + 0x140;
2134 reg_state[CTX_BB_HEAD_L+1] = 0;
2135 reg_state[CTX_BB_STATE] = ring->mmio_base + 0x110;
2136 reg_state[CTX_BB_STATE+1] = (1<<5);
2137 reg_state[CTX_SECOND_BB_HEAD_U] = ring->mmio_base + 0x11c;
2138 reg_state[CTX_SECOND_BB_HEAD_U+1] = 0;
2139 reg_state[CTX_SECOND_BB_HEAD_L] = ring->mmio_base + 0x114;
2140 reg_state[CTX_SECOND_BB_HEAD_L+1] = 0;
2141 reg_state[CTX_SECOND_BB_STATE] = ring->mmio_base + 0x118;
2142 reg_state[CTX_SECOND_BB_STATE+1] = 0;
2143 if (ring->id == RCS) {
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002144 reg_state[CTX_BB_PER_CTX_PTR] = ring->mmio_base + 0x1c0;
2145 reg_state[CTX_BB_PER_CTX_PTR+1] = 0;
2146 reg_state[CTX_RCS_INDIRECT_CTX] = ring->mmio_base + 0x1c4;
2147 reg_state[CTX_RCS_INDIRECT_CTX+1] = 0;
2148 reg_state[CTX_RCS_INDIRECT_CTX_OFFSET] = ring->mmio_base + 0x1c8;
2149 reg_state[CTX_RCS_INDIRECT_CTX_OFFSET+1] = 0;
Arun Siluvery17ee9502015-06-19 19:07:01 +01002150 if (ring->wa_ctx.obj) {
2151 struct i915_ctx_workarounds *wa_ctx = &ring->wa_ctx;
2152 uint32_t ggtt_offset = i915_gem_obj_ggtt_offset(wa_ctx->obj);
2153
2154 reg_state[CTX_RCS_INDIRECT_CTX+1] =
2155 (ggtt_offset + wa_ctx->indirect_ctx.offset * sizeof(uint32_t)) |
2156 (wa_ctx->indirect_ctx.size / CACHELINE_DWORDS);
2157
2158 reg_state[CTX_RCS_INDIRECT_CTX_OFFSET+1] =
2159 CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT << 6;
2160
2161 reg_state[CTX_BB_PER_CTX_PTR+1] =
2162 (ggtt_offset + wa_ctx->per_ctx.offset * sizeof(uint32_t)) |
2163 0x01;
2164 }
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002165 }
2166 reg_state[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9);
2167 reg_state[CTX_LRI_HEADER_1] |= MI_LRI_FORCE_POSTED;
2168 reg_state[CTX_CTX_TIMESTAMP] = ring->mmio_base + 0x3a8;
2169 reg_state[CTX_CTX_TIMESTAMP+1] = 0;
2170 reg_state[CTX_PDP3_UDW] = GEN8_RING_PDP_UDW(ring, 3);
2171 reg_state[CTX_PDP3_LDW] = GEN8_RING_PDP_LDW(ring, 3);
2172 reg_state[CTX_PDP2_UDW] = GEN8_RING_PDP_UDW(ring, 2);
2173 reg_state[CTX_PDP2_LDW] = GEN8_RING_PDP_LDW(ring, 2);
2174 reg_state[CTX_PDP1_UDW] = GEN8_RING_PDP_UDW(ring, 1);
2175 reg_state[CTX_PDP1_LDW] = GEN8_RING_PDP_LDW(ring, 1);
2176 reg_state[CTX_PDP0_UDW] = GEN8_RING_PDP_UDW(ring, 0);
2177 reg_state[CTX_PDP0_LDW] = GEN8_RING_PDP_LDW(ring, 0);
Michel Thierryd7b26332015-04-08 12:13:34 +01002178
2179 /* With dynamic page allocation, PDPs may not be allocated at this point,
2180 * Point the unallocated PDPs to the scratch page
Michel Thierrye5815a22015-04-08 12:13:32 +01002181 */
2182 ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
2183 ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
2184 ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
2185 ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002186 if (ring->id == RCS) {
2187 reg_state[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
Jeff McGee0cea6502015-02-13 10:27:56 -06002188 reg_state[CTX_R_PWR_CLK_STATE] = GEN8_R_PWR_CLK_STATE;
2189 reg_state[CTX_R_PWR_CLK_STATE+1] = make_rpcs(dev);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002190 }
2191
2192 kunmap_atomic(reg_state);
2193
2194 ctx_obj->dirty = 1;
2195 set_page_dirty(page);
2196 i915_gem_object_unpin_pages(ctx_obj);
2197
2198 return 0;
2199}
2200
Oscar Mateo73e4d072014-07-24 17:04:48 +01002201/**
2202 * intel_lr_context_free() - free the LRC specific bits of a context
2203 * @ctx: the LR context to free.
2204 *
2205 * The real context freeing is done in i915_gem_context_free: this only
2206 * takes care of the bits that are LRC related: the per-engine backing
2207 * objects and the logical ringbuffer.
2208 */
Oscar Mateoede7d422014-07-24 17:04:12 +01002209void intel_lr_context_free(struct intel_context *ctx)
2210{
Oscar Mateo8c8579172014-07-24 17:04:14 +01002211 int i;
2212
2213 for (i = 0; i < I915_NUM_RINGS; i++) {
2214 struct drm_i915_gem_object *ctx_obj = ctx->engine[i].state;
Oscar Mateo84c23772014-07-24 17:04:15 +01002215
Oscar Mateo8c8579172014-07-24 17:04:14 +01002216 if (ctx_obj) {
Oscar Mateodcb4c122014-11-13 10:28:10 +00002217 struct intel_ringbuffer *ringbuf =
2218 ctx->engine[i].ringbuf;
2219 struct intel_engine_cs *ring = ringbuf->ring;
2220
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002221 if (ctx == ring->default_context) {
2222 intel_unpin_ringbuffer_obj(ringbuf);
2223 i915_gem_object_ggtt_unpin(ctx_obj);
2224 }
Mika Kuoppalaa7cbede2015-01-13 11:32:25 +02002225 WARN_ON(ctx->engine[ring->id].pin_count);
Oscar Mateo84c23772014-07-24 17:04:15 +01002226 intel_destroy_ringbuffer_obj(ringbuf);
2227 kfree(ringbuf);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002228 drm_gem_object_unreference(&ctx_obj->base);
2229 }
2230 }
2231}
2232
2233static uint32_t get_lr_context_size(struct intel_engine_cs *ring)
2234{
2235 int ret = 0;
2236
Michael H. Nguyen468c6812014-11-13 17:51:49 +00002237 WARN_ON(INTEL_INFO(ring->dev)->gen < 8);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002238
2239 switch (ring->id) {
2240 case RCS:
Michael H. Nguyen468c6812014-11-13 17:51:49 +00002241 if (INTEL_INFO(ring->dev)->gen >= 9)
2242 ret = GEN9_LR_CONTEXT_RENDER_SIZE;
2243 else
2244 ret = GEN8_LR_CONTEXT_RENDER_SIZE;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002245 break;
2246 case VCS:
2247 case BCS:
2248 case VECS:
2249 case VCS2:
2250 ret = GEN8_LR_CONTEXT_OTHER_SIZE;
2251 break;
2252 }
2253
2254 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01002255}
2256
Daniel Vetter70b0ea82014-11-18 09:09:32 +01002257static void lrc_setup_hardware_status_page(struct intel_engine_cs *ring,
Thomas Daniel1df06b72014-10-29 09:52:51 +00002258 struct drm_i915_gem_object *default_ctx_obj)
2259{
2260 struct drm_i915_private *dev_priv = ring->dev->dev_private;
2261
2262 /* The status page is offset 0 from the default context object
2263 * in LRC mode. */
2264 ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(default_ctx_obj);
2265 ring->status_page.page_addr =
2266 kmap(sg_page(default_ctx_obj->pages->sgl));
Thomas Daniel1df06b72014-10-29 09:52:51 +00002267 ring->status_page.obj = default_ctx_obj;
2268
2269 I915_WRITE(RING_HWS_PGA(ring->mmio_base),
2270 (u32)ring->status_page.gfx_addr);
2271 POSTING_READ(RING_HWS_PGA(ring->mmio_base));
Thomas Daniel1df06b72014-10-29 09:52:51 +00002272}
2273
Oscar Mateo73e4d072014-07-24 17:04:48 +01002274/**
2275 * intel_lr_context_deferred_create() - create the LRC specific bits of a context
2276 * @ctx: LR context to create.
2277 * @ring: engine to be used with the context.
2278 *
2279 * This function can be called more than once, with different engines, if we plan
2280 * to use the context with them. The context backing objects and the ringbuffers
2281 * (specially the ringbuffer backing objects) suck a lot of memory up, and that's why
2282 * the creation is a deferred call: it's better to make sure first that we need to use
2283 * a given ring with the context.
2284 *
Masanari Iida32197aa2014-10-20 23:53:13 +09002285 * Return: non-zero on error.
Oscar Mateo73e4d072014-07-24 17:04:48 +01002286 */
Oscar Mateoede7d422014-07-24 17:04:12 +01002287int intel_lr_context_deferred_create(struct intel_context *ctx,
2288 struct intel_engine_cs *ring)
2289{
Oscar Mateodcb4c122014-11-13 10:28:10 +00002290 const bool is_global_default_ctx = (ctx == ring->default_context);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002291 struct drm_device *dev = ring->dev;
2292 struct drm_i915_gem_object *ctx_obj;
2293 uint32_t context_size;
Oscar Mateo84c23772014-07-24 17:04:15 +01002294 struct intel_ringbuffer *ringbuf;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002295 int ret;
2296
Oscar Mateoede7d422014-07-24 17:04:12 +01002297 WARN_ON(ctx->legacy_hw_ctx.rcs_state != NULL);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002298 WARN_ON(ctx->engine[ring->id].state);
Oscar Mateoede7d422014-07-24 17:04:12 +01002299
Oscar Mateo8c8579172014-07-24 17:04:14 +01002300 context_size = round_up(get_lr_context_size(ring), 4096);
2301
Chris Wilson149c86e2015-04-07 16:21:11 +01002302 ctx_obj = i915_gem_alloc_object(dev, context_size);
Dan Carpenter3126a662015-04-30 17:30:50 +03002303 if (!ctx_obj) {
2304 DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
2305 return -ENOMEM;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002306 }
2307
Oscar Mateodcb4c122014-11-13 10:28:10 +00002308 if (is_global_default_ctx) {
2309 ret = i915_gem_obj_ggtt_pin(ctx_obj, GEN8_LR_CONTEXT_ALIGN, 0);
2310 if (ret) {
2311 DRM_DEBUG_DRIVER("Pin LRC backing obj failed: %d\n",
2312 ret);
2313 drm_gem_object_unreference(&ctx_obj->base);
2314 return ret;
2315 }
Oscar Mateo8c8579172014-07-24 17:04:14 +01002316 }
2317
Oscar Mateo84c23772014-07-24 17:04:15 +01002318 ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
2319 if (!ringbuf) {
2320 DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s\n",
2321 ring->name);
Oscar Mateo84c23772014-07-24 17:04:15 +01002322 ret = -ENOMEM;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002323 goto error_unpin_ctx;
Oscar Mateo84c23772014-07-24 17:04:15 +01002324 }
2325
Daniel Vetter0c7dd532014-08-11 16:17:44 +02002326 ringbuf->ring = ring;
Oscar Mateo582d67f2014-07-24 17:04:16 +01002327
Oscar Mateo84c23772014-07-24 17:04:15 +01002328 ringbuf->size = 32 * PAGE_SIZE;
2329 ringbuf->effective_size = ringbuf->size;
2330 ringbuf->head = 0;
2331 ringbuf->tail = 0;
Oscar Mateo84c23772014-07-24 17:04:15 +01002332 ringbuf->last_retired_head = -1;
Dave Gordonebd0fd42014-11-27 11:22:49 +00002333 intel_ring_update_space(ringbuf);
Oscar Mateo84c23772014-07-24 17:04:15 +01002334
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002335 if (ringbuf->obj == NULL) {
2336 ret = intel_alloc_ringbuffer_obj(dev, ringbuf);
2337 if (ret) {
2338 DRM_DEBUG_DRIVER(
2339 "Failed to allocate ringbuffer obj %s: %d\n",
Oscar Mateo84c23772014-07-24 17:04:15 +01002340 ring->name, ret);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002341 goto error_free_rbuf;
2342 }
2343
2344 if (is_global_default_ctx) {
2345 ret = intel_pin_and_map_ringbuffer_obj(dev, ringbuf);
2346 if (ret) {
2347 DRM_ERROR(
2348 "Failed to pin and map ringbuffer %s: %d\n",
2349 ring->name, ret);
2350 goto error_destroy_rbuf;
2351 }
2352 }
2353
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002354 }
2355
2356 ret = populate_lr_context(ctx, ctx_obj, ring, ringbuf);
2357 if (ret) {
2358 DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002359 goto error;
Oscar Mateo84c23772014-07-24 17:04:15 +01002360 }
2361
2362 ctx->engine[ring->id].ringbuf = ringbuf;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002363 ctx->engine[ring->id].state = ctx_obj;
Oscar Mateoede7d422014-07-24 17:04:12 +01002364
Daniel Vetter70b0ea82014-11-18 09:09:32 +01002365 if (ctx == ring->default_context)
2366 lrc_setup_hardware_status_page(ring, ctx_obj);
Thomas Daniele7778be2014-12-02 12:50:48 +00002367 else if (ring->id == RCS && !ctx->rcs_initialized) {
Michel Thierry771b9a52014-11-11 16:47:33 +00002368 if (ring->init_context) {
John Harrison76c39162015-05-29 17:43:43 +01002369 struct drm_i915_gem_request *req;
2370
2371 ret = i915_gem_request_alloc(ring, ctx, &req);
2372 if (ret)
2373 return ret;
2374
John Harrison87531812015-05-29 17:43:44 +01002375 ret = ring->init_context(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00002376 if (ret) {
Michel Thierry771b9a52014-11-11 16:47:33 +00002377 DRM_ERROR("ring init context: %d\n", ret);
John Harrison76c39162015-05-29 17:43:43 +01002378 i915_gem_request_cancel(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00002379 ctx->engine[ring->id].ringbuf = NULL;
2380 ctx->engine[ring->id].state = NULL;
2381 goto error;
2382 }
John Harrison76c39162015-05-29 17:43:43 +01002383
John Harrison75289872015-05-29 17:43:49 +01002384 i915_add_request_no_flush(req);
Michel Thierry771b9a52014-11-11 16:47:33 +00002385 }
2386
Oscar Mateo564ddb22014-08-21 11:40:54 +01002387 ctx->rcs_initialized = true;
2388 }
2389
Oscar Mateoede7d422014-07-24 17:04:12 +01002390 return 0;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002391
2392error:
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002393 if (is_global_default_ctx)
2394 intel_unpin_ringbuffer_obj(ringbuf);
2395error_destroy_rbuf:
2396 intel_destroy_ringbuffer_obj(ringbuf);
2397error_free_rbuf:
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002398 kfree(ringbuf);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002399error_unpin_ctx:
Oscar Mateodcb4c122014-11-13 10:28:10 +00002400 if (is_global_default_ctx)
2401 i915_gem_object_ggtt_unpin(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002402 drm_gem_object_unreference(&ctx_obj->base);
2403 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01002404}
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002405
2406void intel_lr_context_reset(struct drm_device *dev,
2407 struct intel_context *ctx)
2408{
2409 struct drm_i915_private *dev_priv = dev->dev_private;
2410 struct intel_engine_cs *ring;
2411 int i;
2412
2413 for_each_ring(ring, dev_priv, i) {
2414 struct drm_i915_gem_object *ctx_obj =
2415 ctx->engine[ring->id].state;
2416 struct intel_ringbuffer *ringbuf =
2417 ctx->engine[ring->id].ringbuf;
2418 uint32_t *reg_state;
2419 struct page *page;
2420
2421 if (!ctx_obj)
2422 continue;
2423
2424 if (i915_gem_object_get_pages(ctx_obj)) {
2425 WARN(1, "Failed get_pages for context obj\n");
2426 continue;
2427 }
2428 page = i915_gem_object_get_page(ctx_obj, 1);
2429 reg_state = kmap_atomic(page);
2430
2431 reg_state[CTX_RING_HEAD+1] = 0;
2432 reg_state[CTX_RING_TAIL+1] = 0;
2433
2434 kunmap_atomic(reg_state);
2435
2436 ringbuf->head = 0;
2437 ringbuf->tail = 0;
2438 }
2439}