blob: 4c47a64a974ff1c37b77d474810ccc024b039b83 [file] [log] [blame]
Oscar Mateob20385f2014-07-24 17:04:10 +01001/*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Ben Widawsky <ben@bwidawsk.net>
25 * Michel Thierry <michel.thierry@intel.com>
26 * Thomas Daniel <thomas.daniel@intel.com>
27 * Oscar Mateo <oscar.mateo@intel.com>
28 *
29 */
30
Oscar Mateo73e4d072014-07-24 17:04:48 +010031/**
32 * DOC: Logical Rings, Logical Ring Contexts and Execlists
33 *
34 * Motivation:
Oscar Mateob20385f2014-07-24 17:04:10 +010035 * GEN8 brings an expansion of the HW contexts: "Logical Ring Contexts".
36 * These expanded contexts enable a number of new abilities, especially
37 * "Execlists" (also implemented in this file).
38 *
Oscar Mateo73e4d072014-07-24 17:04:48 +010039 * One of the main differences with the legacy HW contexts is that logical
40 * ring contexts incorporate many more things to the context's state, like
41 * PDPs or ringbuffer control registers:
42 *
43 * The reason why PDPs are included in the context is straightforward: as
44 * PPGTTs (per-process GTTs) are actually per-context, having the PDPs
45 * contained there mean you don't need to do a ppgtt->switch_mm yourself,
46 * instead, the GPU will do it for you on the context switch.
47 *
48 * But, what about the ringbuffer control registers (head, tail, etc..)?
49 * shouldn't we just need a set of those per engine command streamer? This is
50 * where the name "Logical Rings" starts to make sense: by virtualizing the
51 * rings, the engine cs shifts to a new "ring buffer" with every context
52 * switch. When you want to submit a workload to the GPU you: A) choose your
53 * context, B) find its appropriate virtualized ring, C) write commands to it
54 * and then, finally, D) tell the GPU to switch to that context.
55 *
56 * Instead of the legacy MI_SET_CONTEXT, the way you tell the GPU to switch
57 * to a contexts is via a context execution list, ergo "Execlists".
58 *
59 * LRC implementation:
60 * Regarding the creation of contexts, we have:
61 *
62 * - One global default context.
63 * - One local default context for each opened fd.
64 * - One local extra context for each context create ioctl call.
65 *
66 * Now that ringbuffers belong per-context (and not per-engine, like before)
67 * and that contexts are uniquely tied to a given engine (and not reusable,
68 * like before) we need:
69 *
70 * - One ringbuffer per-engine inside each context.
71 * - One backing object per-engine inside each context.
72 *
73 * The global default context starts its life with these new objects fully
74 * allocated and populated. The local default context for each opened fd is
75 * more complex, because we don't know at creation time which engine is going
76 * to use them. To handle this, we have implemented a deferred creation of LR
77 * contexts:
78 *
79 * The local context starts its life as a hollow or blank holder, that only
80 * gets populated for a given engine once we receive an execbuffer. If later
81 * on we receive another execbuffer ioctl for the same context but a different
82 * engine, we allocate/populate a new ringbuffer and context backing object and
83 * so on.
84 *
85 * Finally, regarding local contexts created using the ioctl call: as they are
86 * only allowed with the render ring, we can allocate & populate them right
87 * away (no need to defer anything, at least for now).
88 *
89 * Execlists implementation:
Oscar Mateob20385f2014-07-24 17:04:10 +010090 * Execlists are the new method by which, on gen8+ hardware, workloads are
91 * submitted for execution (as opposed to the legacy, ringbuffer-based, method).
Oscar Mateo73e4d072014-07-24 17:04:48 +010092 * This method works as follows:
93 *
94 * When a request is committed, its commands (the BB start and any leading or
95 * trailing commands, like the seqno breadcrumbs) are placed in the ringbuffer
96 * for the appropriate context. The tail pointer in the hardware context is not
97 * updated at this time, but instead, kept by the driver in the ringbuffer
98 * structure. A structure representing this request is added to a request queue
99 * for the appropriate engine: this structure contains a copy of the context's
100 * tail after the request was written to the ring buffer and a pointer to the
101 * context itself.
102 *
103 * If the engine's request queue was empty before the request was added, the
104 * queue is processed immediately. Otherwise the queue will be processed during
105 * a context switch interrupt. In any case, elements on the queue will get sent
106 * (in pairs) to the GPU's ExecLists Submit Port (ELSP, for short) with a
107 * globally unique 20-bits submission ID.
108 *
109 * When execution of a request completes, the GPU updates the context status
110 * buffer with a context complete event and generates a context switch interrupt.
111 * During the interrupt handling, the driver examines the events in the buffer:
112 * for each context complete event, if the announced ID matches that on the head
113 * of the request queue, then that request is retired and removed from the queue.
114 *
115 * After processing, if any requests were retired and the queue is not empty
116 * then a new execution list can be submitted. The two requests at the front of
117 * the queue are next to be submitted but since a context may not occur twice in
118 * an execution list, if subsequent requests have the same ID as the first then
119 * the two requests must be combined. This is done simply by discarding requests
120 * at the head of the queue until either only one requests is left (in which case
121 * we use a NULL second context) or the first two requests have unique IDs.
122 *
123 * By always executing the first two requests in the queue the driver ensures
124 * that the GPU is kept as busy as possible. In the case where a single context
125 * completes but a second context is still executing, the request for this second
126 * context will be at the head of the queue when we remove the first one. This
127 * request will then be resubmitted along with a new request for a different context,
128 * which will cause the hardware to continue executing the second request and queue
129 * the new request (the GPU detects the condition of a context getting preempted
130 * with the same context and optimizes the context switch flow by not doing
131 * preemption, but just sampling the new tail pointer).
132 *
Oscar Mateob20385f2014-07-24 17:04:10 +0100133 */
134
135#include <drm/drmP.h>
136#include <drm/i915_drm.h>
137#include "i915_drv.h"
Oscar Mateo127f1002014-07-24 17:04:11 +0100138
Michael H. Nguyen468c6812014-11-13 17:51:49 +0000139#define GEN9_LR_CONTEXT_RENDER_SIZE (22 * PAGE_SIZE)
Oscar Mateo8c8579172014-07-24 17:04:14 +0100140#define GEN8_LR_CONTEXT_RENDER_SIZE (20 * PAGE_SIZE)
141#define GEN8_LR_CONTEXT_OTHER_SIZE (2 * PAGE_SIZE)
142
Thomas Daniele981e7b2014-07-24 17:04:39 +0100143#define RING_EXECLIST_QFULL (1 << 0x2)
144#define RING_EXECLIST1_VALID (1 << 0x3)
145#define RING_EXECLIST0_VALID (1 << 0x4)
146#define RING_EXECLIST_ACTIVE_STATUS (3 << 0xE)
147#define RING_EXECLIST1_ACTIVE (1 << 0x11)
148#define RING_EXECLIST0_ACTIVE (1 << 0x12)
149
150#define GEN8_CTX_STATUS_IDLE_ACTIVE (1 << 0)
151#define GEN8_CTX_STATUS_PREEMPTED (1 << 1)
152#define GEN8_CTX_STATUS_ELEMENT_SWITCH (1 << 2)
153#define GEN8_CTX_STATUS_ACTIVE_IDLE (1 << 3)
154#define GEN8_CTX_STATUS_COMPLETE (1 << 4)
155#define GEN8_CTX_STATUS_LITE_RESTORE (1 << 15)
Oscar Mateo8670d6f2014-07-24 17:04:17 +0100156
157#define CTX_LRI_HEADER_0 0x01
158#define CTX_CONTEXT_CONTROL 0x02
159#define CTX_RING_HEAD 0x04
160#define CTX_RING_TAIL 0x06
161#define CTX_RING_BUFFER_START 0x08
162#define CTX_RING_BUFFER_CONTROL 0x0a
163#define CTX_BB_HEAD_U 0x0c
164#define CTX_BB_HEAD_L 0x0e
165#define CTX_BB_STATE 0x10
166#define CTX_SECOND_BB_HEAD_U 0x12
167#define CTX_SECOND_BB_HEAD_L 0x14
168#define CTX_SECOND_BB_STATE 0x16
169#define CTX_BB_PER_CTX_PTR 0x18
170#define CTX_RCS_INDIRECT_CTX 0x1a
171#define CTX_RCS_INDIRECT_CTX_OFFSET 0x1c
172#define CTX_LRI_HEADER_1 0x21
173#define CTX_CTX_TIMESTAMP 0x22
174#define CTX_PDP3_UDW 0x24
175#define CTX_PDP3_LDW 0x26
176#define CTX_PDP2_UDW 0x28
177#define CTX_PDP2_LDW 0x2a
178#define CTX_PDP1_UDW 0x2c
179#define CTX_PDP1_LDW 0x2e
180#define CTX_PDP0_UDW 0x30
181#define CTX_PDP0_LDW 0x32
182#define CTX_LRI_HEADER_2 0x41
183#define CTX_R_PWR_CLK_STATE 0x42
184#define CTX_GPGPU_CSR_BASE_ADDRESS 0x44
185
Ben Widawsky84b790f2014-07-24 17:04:36 +0100186#define GEN8_CTX_VALID (1<<0)
187#define GEN8_CTX_FORCE_PD_RESTORE (1<<1)
188#define GEN8_CTX_FORCE_RESTORE (1<<2)
189#define GEN8_CTX_L3LLC_COHERENT (1<<5)
190#define GEN8_CTX_PRIVILEGE (1<<8)
Michel Thierrye5815a22015-04-08 12:13:32 +0100191
192#define ASSIGN_CTX_PDP(ppgtt, reg_state, n) { \
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300193 const u64 _addr = i915_page_dir_dma_addr((ppgtt), (n)); \
Michel Thierrye5815a22015-04-08 12:13:32 +0100194 reg_state[CTX_PDP ## n ## _UDW+1] = upper_32_bits(_addr); \
195 reg_state[CTX_PDP ## n ## _LDW+1] = lower_32_bits(_addr); \
196}
197
Ben Widawsky84b790f2014-07-24 17:04:36 +0100198enum {
199 ADVANCED_CONTEXT = 0,
200 LEGACY_CONTEXT,
201 ADVANCED_AD_CONTEXT,
202 LEGACY_64B_CONTEXT
203};
204#define GEN8_CTX_MODE_SHIFT 3
205enum {
206 FAULT_AND_HANG = 0,
207 FAULT_AND_HALT, /* Debug only */
208 FAULT_AND_STREAM,
209 FAULT_AND_CONTINUE /* Unsupported */
210};
211#define GEN8_CTX_ID_SHIFT 32
Arun Siluvery17ee9502015-06-19 19:07:01 +0100212#define CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x17
Ben Widawsky84b790f2014-07-24 17:04:36 +0100213
Mika Kuoppala8ba319d2015-07-03 17:09:35 +0300214static int intel_lr_context_pin(struct drm_i915_gem_request *rq);
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000215
Oscar Mateo73e4d072014-07-24 17:04:48 +0100216/**
217 * intel_sanitize_enable_execlists() - sanitize i915.enable_execlists
218 * @dev: DRM device.
219 * @enable_execlists: value of i915.enable_execlists module parameter.
220 *
221 * Only certain platforms support Execlists (the prerequisites being
Thomas Daniel27401d12014-12-11 12:48:35 +0000222 * support for Logical Ring Contexts and Aliasing PPGTT or better).
Oscar Mateo73e4d072014-07-24 17:04:48 +0100223 *
224 * Return: 1 if Execlists is supported and has to be enabled.
225 */
Oscar Mateo127f1002014-07-24 17:04:11 +0100226int intel_sanitize_enable_execlists(struct drm_device *dev, int enable_execlists)
227{
Daniel Vetterbd84b1e2014-08-11 15:57:57 +0200228 WARN_ON(i915.enable_ppgtt == -1);
229
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000230 if (INTEL_INFO(dev)->gen >= 9)
231 return 1;
232
Oscar Mateo127f1002014-07-24 17:04:11 +0100233 if (enable_execlists == 0)
234 return 0;
235
Oscar Mateo14bf9932014-07-24 17:04:34 +0100236 if (HAS_LOGICAL_RING_CONTEXTS(dev) && USES_PPGTT(dev) &&
237 i915.use_mmio_flip >= 0)
Oscar Mateo127f1002014-07-24 17:04:11 +0100238 return 1;
239
240 return 0;
241}
Oscar Mateoede7d422014-07-24 17:04:12 +0100242
Oscar Mateo73e4d072014-07-24 17:04:48 +0100243/**
244 * intel_execlists_ctx_id() - get the Execlists Context ID
245 * @ctx_obj: Logical Ring Context backing object.
246 *
247 * Do not confuse with ctx->id! Unfortunately we have a name overload
248 * here: the old context ID we pass to userspace as a handler so that
249 * they can refer to a context, and the new context ID we pass to the
250 * ELSP so that the GPU can inform us of the context status via
251 * interrupts.
252 *
253 * Return: 20-bits globally unique context ID.
254 */
Ben Widawsky84b790f2014-07-24 17:04:36 +0100255u32 intel_execlists_ctx_id(struct drm_i915_gem_object *ctx_obj)
256{
257 u32 lrca = i915_gem_obj_ggtt_offset(ctx_obj);
258
259 /* LRCA is required to be 4K aligned so the more significant 20 bits
260 * are globally unique */
261 return lrca >> 12;
262}
263
Mika Kuoppala8ee36152015-07-03 17:09:37 +0300264static uint64_t execlists_ctx_descriptor(struct drm_i915_gem_request *rq)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100265{
Mika Kuoppala8ee36152015-07-03 17:09:37 +0300266 struct intel_engine_cs *ring = rq->ring;
Nick Hoath203a5712015-02-06 11:30:04 +0000267 struct drm_device *dev = ring->dev;
Mika Kuoppala8ee36152015-07-03 17:09:37 +0300268 struct drm_i915_gem_object *ctx_obj = rq->ctx->engine[ring->id].state;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100269 uint64_t desc;
270 uint64_t lrca = i915_gem_obj_ggtt_offset(ctx_obj);
Michel Thierryacdd8842014-07-24 17:04:38 +0100271
272 WARN_ON(lrca & 0xFFFFFFFF00000FFFULL);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100273
274 desc = GEN8_CTX_VALID;
275 desc |= LEGACY_CONTEXT << GEN8_CTX_MODE_SHIFT;
Arun Siluvery51847fb2015-04-07 14:01:33 +0100276 if (IS_GEN8(ctx_obj->base.dev))
277 desc |= GEN8_CTX_L3LLC_COHERENT;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100278 desc |= GEN8_CTX_PRIVILEGE;
279 desc |= lrca;
280 desc |= (u64)intel_execlists_ctx_id(ctx_obj) << GEN8_CTX_ID_SHIFT;
281
282 /* TODO: WaDisableLiteRestore when we start using semaphore
283 * signalling between Command Streamers */
284 /* desc |= GEN8_CTX_FORCE_RESTORE; */
285
Nick Hoath203a5712015-02-06 11:30:04 +0000286 /* WaEnableForceRestoreInCtxtDescForVCS:skl */
287 if (IS_GEN9(dev) &&
288 INTEL_REVID(dev) <= SKL_REVID_B0 &&
289 (ring->id == BCS || ring->id == VCS ||
290 ring->id == VECS || ring->id == VCS2))
291 desc |= GEN8_CTX_FORCE_RESTORE;
292
Ben Widawsky84b790f2014-07-24 17:04:36 +0100293 return desc;
294}
295
Mika Kuoppalacc3c4252015-07-03 17:09:36 +0300296static void execlists_elsp_write(struct drm_i915_gem_request *rq0,
297 struct drm_i915_gem_request *rq1)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100298{
Mika Kuoppalacc3c4252015-07-03 17:09:36 +0300299
300 struct intel_engine_cs *ring = rq0->ring;
Tvrtko Ursulin6e7cc472014-11-13 17:51:51 +0000301 struct drm_device *dev = ring->dev;
302 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppala1cff8cc2015-07-06 11:09:25 +0300303 uint64_t desc[2];
Ben Widawsky84b790f2014-07-24 17:04:36 +0100304
Mika Kuoppala1cff8cc2015-07-06 11:09:25 +0300305 if (rq1) {
306 desc[1] = execlists_ctx_descriptor(rq1);
307 rq1->elsp_submitted++;
308 } else {
309 desc[1] = 0;
310 }
Ben Widawsky84b790f2014-07-24 17:04:36 +0100311
Mika Kuoppala1cff8cc2015-07-06 11:09:25 +0300312 desc[0] = execlists_ctx_descriptor(rq0);
313 rq0->elsp_submitted++;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100314
Mika Kuoppala1cff8cc2015-07-06 11:09:25 +0300315 /* You must always write both descriptors in the order below. */
Chris Wilsona6111f72015-04-07 16:21:02 +0100316 spin_lock(&dev_priv->uncore.lock);
317 intel_uncore_forcewake_get__locked(dev_priv, FORCEWAKE_ALL);
Mika Kuoppala1cff8cc2015-07-06 11:09:25 +0300318 I915_WRITE_FW(RING_ELSP(ring), upper_32_bits(desc[1]));
319 I915_WRITE_FW(RING_ELSP(ring), lower_32_bits(desc[1]));
Chris Wilson6daccb02015-01-16 11:34:35 +0200320
Mika Kuoppala1cff8cc2015-07-06 11:09:25 +0300321 I915_WRITE_FW(RING_ELSP(ring), upper_32_bits(desc[0]));
Ben Widawsky84b790f2014-07-24 17:04:36 +0100322 /* The context is automatically loaded after the following */
Mika Kuoppala1cff8cc2015-07-06 11:09:25 +0300323 I915_WRITE_FW(RING_ELSP(ring), lower_32_bits(desc[0]));
Ben Widawsky84b790f2014-07-24 17:04:36 +0100324
Mika Kuoppala1cff8cc2015-07-06 11:09:25 +0300325 /* ELSP is a wo register, use another nearby reg for posting */
Chris Wilsona6111f72015-04-07 16:21:02 +0100326 POSTING_READ_FW(RING_EXECLIST_STATUS(ring));
327 intel_uncore_forcewake_put__locked(dev_priv, FORCEWAKE_ALL);
328 spin_unlock(&dev_priv->uncore.lock);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100329}
330
Mika Kuoppala05d98242015-07-03 17:09:33 +0300331static int execlists_update_context(struct drm_i915_gem_request *rq)
Oscar Mateoae1250b2014-07-24 17:04:37 +0100332{
Mika Kuoppala05d98242015-07-03 17:09:33 +0300333 struct intel_engine_cs *ring = rq->ring;
334 struct i915_hw_ppgtt *ppgtt = rq->ctx->ppgtt;
335 struct drm_i915_gem_object *ctx_obj = rq->ctx->engine[ring->id].state;
336 struct drm_i915_gem_object *rb_obj = rq->ringbuf->obj;
Oscar Mateoae1250b2014-07-24 17:04:37 +0100337 struct page *page;
338 uint32_t *reg_state;
339
Mika Kuoppala05d98242015-07-03 17:09:33 +0300340 BUG_ON(!ctx_obj);
341 WARN_ON(!i915_gem_obj_is_pinned(ctx_obj));
342 WARN_ON(!i915_gem_obj_is_pinned(rb_obj));
343
Oscar Mateoae1250b2014-07-24 17:04:37 +0100344 page = i915_gem_object_get_page(ctx_obj, 1);
345 reg_state = kmap_atomic(page);
346
Mika Kuoppala05d98242015-07-03 17:09:33 +0300347 reg_state[CTX_RING_TAIL+1] = rq->tail;
348 reg_state[CTX_RING_BUFFER_START+1] = i915_gem_obj_ggtt_offset(rb_obj);
Oscar Mateoae1250b2014-07-24 17:04:37 +0100349
Michel Thierryd7b26332015-04-08 12:13:34 +0100350 /* True PPGTT with dynamic page allocation: update PDP registers and
351 * point the unallocated PDPs to the scratch page
352 */
353 if (ppgtt) {
354 ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
355 ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
356 ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
357 ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
358 }
359
Oscar Mateoae1250b2014-07-24 17:04:37 +0100360 kunmap_atomic(reg_state);
361
362 return 0;
363}
364
Mika Kuoppalad8cb8872015-07-03 17:09:32 +0300365static void execlists_submit_requests(struct drm_i915_gem_request *rq0,
366 struct drm_i915_gem_request *rq1)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100367{
Mika Kuoppala05d98242015-07-03 17:09:33 +0300368 execlists_update_context(rq0);
Oscar Mateoae1250b2014-07-24 17:04:37 +0100369
Mika Kuoppalacc3c4252015-07-03 17:09:36 +0300370 if (rq1)
Mika Kuoppala05d98242015-07-03 17:09:33 +0300371 execlists_update_context(rq1);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100372
Mika Kuoppalacc3c4252015-07-03 17:09:36 +0300373 execlists_elsp_write(rq0, rq1);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100374}
375
Michel Thierryacdd8842014-07-24 17:04:38 +0100376static void execlists_context_unqueue(struct intel_engine_cs *ring)
377{
Nick Hoath6d3d8272015-01-15 13:10:39 +0000378 struct drm_i915_gem_request *req0 = NULL, *req1 = NULL;
379 struct drm_i915_gem_request *cursor = NULL, *tmp = NULL;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100380
381 assert_spin_locked(&ring->execlist_lock);
Michel Thierryacdd8842014-07-24 17:04:38 +0100382
Peter Antoine779949f2015-05-11 16:03:27 +0100383 /*
384 * If irqs are not active generate a warning as batches that finish
385 * without the irqs may get lost and a GPU Hang may occur.
386 */
387 WARN_ON(!intel_irqs_enabled(ring->dev->dev_private));
388
Michel Thierryacdd8842014-07-24 17:04:38 +0100389 if (list_empty(&ring->execlist_queue))
390 return;
391
392 /* Try to read in pairs */
393 list_for_each_entry_safe(cursor, tmp, &ring->execlist_queue,
394 execlist_link) {
395 if (!req0) {
396 req0 = cursor;
Nick Hoath6d3d8272015-01-15 13:10:39 +0000397 } else if (req0->ctx == cursor->ctx) {
Michel Thierryacdd8842014-07-24 17:04:38 +0100398 /* Same ctx: ignore first request, as second request
399 * will update tail past first request's workload */
Oscar Mateoe1fee722014-07-24 17:04:40 +0100400 cursor->elsp_submitted = req0->elsp_submitted;
Michel Thierryacdd8842014-07-24 17:04:38 +0100401 list_del(&req0->execlist_link);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000402 list_add_tail(&req0->execlist_link,
403 &ring->execlist_retired_req_list);
Michel Thierryacdd8842014-07-24 17:04:38 +0100404 req0 = cursor;
405 } else {
406 req1 = cursor;
407 break;
408 }
409 }
410
Michel Thierry53292cd2015-04-15 18:11:33 +0100411 if (IS_GEN8(ring->dev) || IS_GEN9(ring->dev)) {
412 /*
413 * WaIdleLiteRestore: make sure we never cause a lite
414 * restore with HEAD==TAIL
415 */
Michel Thierryd63f8202015-04-27 12:31:44 +0100416 if (req0->elsp_submitted) {
Michel Thierry53292cd2015-04-15 18:11:33 +0100417 /*
418 * Apply the wa NOOPS to prevent ring:HEAD == req:TAIL
419 * as we resubmit the request. See gen8_emit_request()
420 * for where we prepare the padding after the end of the
421 * request.
422 */
423 struct intel_ringbuffer *ringbuf;
424
425 ringbuf = req0->ctx->engine[ring->id].ringbuf;
426 req0->tail += 8;
427 req0->tail &= ringbuf->size - 1;
428 }
429 }
430
Oscar Mateoe1fee722014-07-24 17:04:40 +0100431 WARN_ON(req1 && req1->elsp_submitted);
432
Mika Kuoppalad8cb8872015-07-03 17:09:32 +0300433 execlists_submit_requests(req0, req1);
Michel Thierryacdd8842014-07-24 17:04:38 +0100434}
435
Thomas Daniele981e7b2014-07-24 17:04:39 +0100436static bool execlists_check_remove_request(struct intel_engine_cs *ring,
437 u32 request_id)
438{
Nick Hoath6d3d8272015-01-15 13:10:39 +0000439 struct drm_i915_gem_request *head_req;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100440
441 assert_spin_locked(&ring->execlist_lock);
442
443 head_req = list_first_entry_or_null(&ring->execlist_queue,
Nick Hoath6d3d8272015-01-15 13:10:39 +0000444 struct drm_i915_gem_request,
Thomas Daniele981e7b2014-07-24 17:04:39 +0100445 execlist_link);
446
447 if (head_req != NULL) {
448 struct drm_i915_gem_object *ctx_obj =
Nick Hoath6d3d8272015-01-15 13:10:39 +0000449 head_req->ctx->engine[ring->id].state;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100450 if (intel_execlists_ctx_id(ctx_obj) == request_id) {
Oscar Mateoe1fee722014-07-24 17:04:40 +0100451 WARN(head_req->elsp_submitted == 0,
452 "Never submitted head request\n");
453
454 if (--head_req->elsp_submitted <= 0) {
455 list_del(&head_req->execlist_link);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000456 list_add_tail(&head_req->execlist_link,
457 &ring->execlist_retired_req_list);
Oscar Mateoe1fee722014-07-24 17:04:40 +0100458 return true;
459 }
Thomas Daniele981e7b2014-07-24 17:04:39 +0100460 }
461 }
462
463 return false;
464}
465
Oscar Mateo73e4d072014-07-24 17:04:48 +0100466/**
Daniel Vetter3f7531c2014-12-10 17:41:43 +0100467 * intel_lrc_irq_handler() - handle Context Switch interrupts
Oscar Mateo73e4d072014-07-24 17:04:48 +0100468 * @ring: Engine Command Streamer to handle.
469 *
470 * Check the unread Context Status Buffers and manage the submission of new
471 * contexts to the ELSP accordingly.
472 */
Daniel Vetter3f7531c2014-12-10 17:41:43 +0100473void intel_lrc_irq_handler(struct intel_engine_cs *ring)
Thomas Daniele981e7b2014-07-24 17:04:39 +0100474{
475 struct drm_i915_private *dev_priv = ring->dev->dev_private;
476 u32 status_pointer;
477 u8 read_pointer;
478 u8 write_pointer;
479 u32 status;
480 u32 status_id;
481 u32 submit_contexts = 0;
482
483 status_pointer = I915_READ(RING_CONTEXT_STATUS_PTR(ring));
484
485 read_pointer = ring->next_context_status_buffer;
486 write_pointer = status_pointer & 0x07;
487 if (read_pointer > write_pointer)
488 write_pointer += 6;
489
490 spin_lock(&ring->execlist_lock);
491
492 while (read_pointer < write_pointer) {
493 read_pointer++;
494 status = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
495 (read_pointer % 6) * 8);
496 status_id = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
497 (read_pointer % 6) * 8 + 4);
498
Oscar Mateoe1fee722014-07-24 17:04:40 +0100499 if (status & GEN8_CTX_STATUS_PREEMPTED) {
500 if (status & GEN8_CTX_STATUS_LITE_RESTORE) {
501 if (execlists_check_remove_request(ring, status_id))
502 WARN(1, "Lite Restored request removed from queue\n");
503 } else
504 WARN(1, "Preemption without Lite Restore\n");
505 }
506
507 if ((status & GEN8_CTX_STATUS_ACTIVE_IDLE) ||
508 (status & GEN8_CTX_STATUS_ELEMENT_SWITCH)) {
Thomas Daniele981e7b2014-07-24 17:04:39 +0100509 if (execlists_check_remove_request(ring, status_id))
510 submit_contexts++;
511 }
512 }
513
514 if (submit_contexts != 0)
515 execlists_context_unqueue(ring);
516
517 spin_unlock(&ring->execlist_lock);
518
519 WARN(submit_contexts > 2, "More than two context complete events?\n");
520 ring->next_context_status_buffer = write_pointer % 6;
521
522 I915_WRITE(RING_CONTEXT_STATUS_PTR(ring),
523 ((u32)ring->next_context_status_buffer & 0x07) << 8);
524}
525
John Harrisonae707972015-05-29 17:44:14 +0100526static int execlists_context_queue(struct drm_i915_gem_request *request)
Michel Thierryacdd8842014-07-24 17:04:38 +0100527{
John Harrisonae707972015-05-29 17:44:14 +0100528 struct intel_engine_cs *ring = request->ring;
Nick Hoath6d3d8272015-01-15 13:10:39 +0000529 struct drm_i915_gem_request *cursor;
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100530 int num_elements = 0;
Michel Thierryacdd8842014-07-24 17:04:38 +0100531
John Harrisonae707972015-05-29 17:44:14 +0100532 if (request->ctx != ring->default_context)
Mika Kuoppala8ba319d2015-07-03 17:09:35 +0300533 intel_lr_context_pin(request);
John Harrison9bb1af42015-05-29 17:44:13 +0100534
535 i915_gem_request_reference(request);
536
John Harrisonae707972015-05-29 17:44:14 +0100537 request->tail = request->ringbuf->tail;
Nick Hoath2d129552015-01-15 13:10:36 +0000538
Chris Wilsonb5eba372015-04-07 16:20:48 +0100539 spin_lock_irq(&ring->execlist_lock);
Michel Thierryacdd8842014-07-24 17:04:38 +0100540
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100541 list_for_each_entry(cursor, &ring->execlist_queue, execlist_link)
542 if (++num_elements > 2)
543 break;
544
545 if (num_elements > 2) {
Nick Hoath6d3d8272015-01-15 13:10:39 +0000546 struct drm_i915_gem_request *tail_req;
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100547
548 tail_req = list_last_entry(&ring->execlist_queue,
Nick Hoath6d3d8272015-01-15 13:10:39 +0000549 struct drm_i915_gem_request,
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100550 execlist_link);
551
John Harrisonae707972015-05-29 17:44:14 +0100552 if (request->ctx == tail_req->ctx) {
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100553 WARN(tail_req->elsp_submitted != 0,
Thomas Daniel7ba717c2014-11-13 10:28:56 +0000554 "More than 2 already-submitted reqs queued\n");
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100555 list_del(&tail_req->execlist_link);
Thomas Danielc86ee3a92014-11-13 10:27:05 +0000556 list_add_tail(&tail_req->execlist_link,
557 &ring->execlist_retired_req_list);
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100558 }
559 }
560
Nick Hoath6d3d8272015-01-15 13:10:39 +0000561 list_add_tail(&request->execlist_link, &ring->execlist_queue);
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100562 if (num_elements == 0)
Michel Thierryacdd8842014-07-24 17:04:38 +0100563 execlists_context_unqueue(ring);
564
Chris Wilsonb5eba372015-04-07 16:20:48 +0100565 spin_unlock_irq(&ring->execlist_lock);
Michel Thierryacdd8842014-07-24 17:04:38 +0100566
567 return 0;
568}
569
John Harrison2f200552015-05-29 17:43:53 +0100570static int logical_ring_invalidate_all_caches(struct drm_i915_gem_request *req)
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100571{
John Harrison2f200552015-05-29 17:43:53 +0100572 struct intel_engine_cs *ring = req->ring;
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100573 uint32_t flush_domains;
574 int ret;
575
576 flush_domains = 0;
577 if (ring->gpu_caches_dirty)
578 flush_domains = I915_GEM_GPU_DOMAINS;
579
John Harrison7deb4d32015-05-29 17:43:59 +0100580 ret = ring->emit_flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100581 if (ret)
582 return ret;
583
584 ring->gpu_caches_dirty = false;
585 return 0;
586}
587
John Harrison535fbe82015-05-29 17:43:32 +0100588static int execlists_move_to_gpu(struct drm_i915_gem_request *req,
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100589 struct list_head *vmas)
590{
John Harrison535fbe82015-05-29 17:43:32 +0100591 const unsigned other_rings = ~intel_ring_flag(req->ring);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100592 struct i915_vma *vma;
593 uint32_t flush_domains = 0;
594 bool flush_chipset = false;
595 int ret;
596
597 list_for_each_entry(vma, vmas, exec_list) {
598 struct drm_i915_gem_object *obj = vma->obj;
599
Chris Wilson03ade512015-04-27 13:41:18 +0100600 if (obj->active & other_rings) {
John Harrison91af1272015-06-18 13:14:56 +0100601 ret = i915_gem_object_sync(obj, req->ring, &req);
Chris Wilson03ade512015-04-27 13:41:18 +0100602 if (ret)
603 return ret;
604 }
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100605
606 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
607 flush_chipset |= i915_gem_clflush_object(obj, false);
608
609 flush_domains |= obj->base.write_domain;
610 }
611
612 if (flush_domains & I915_GEM_DOMAIN_GTT)
613 wmb();
614
615 /* Unconditionally invalidate gpu caches and ensure that we do flush
616 * any residual writes from the previous batch.
617 */
John Harrison2f200552015-05-29 17:43:53 +0100618 return logical_ring_invalidate_all_caches(req);
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100619}
620
John Harrison40e895c2015-05-29 17:43:26 +0100621int intel_logical_ring_alloc_request_extras(struct drm_i915_gem_request *request)
John Harrisonbc0dce32015-03-19 12:30:07 +0000622{
John Harrisonbc0dce32015-03-19 12:30:07 +0000623 int ret;
624
Mika Kuoppalaf3cc01f2015-07-06 11:08:30 +0300625 request->ringbuf = request->ctx->engine[request->ring->id].ringbuf;
626
John Harrison40e895c2015-05-29 17:43:26 +0100627 if (request->ctx != request->ring->default_context) {
Mika Kuoppala8ba319d2015-07-03 17:09:35 +0300628 ret = intel_lr_context_pin(request);
John Harrison6689cb22015-03-19 12:30:08 +0000629 if (ret)
John Harrisonbc0dce32015-03-19 12:30:07 +0000630 return ret;
John Harrisonbc0dce32015-03-19 12:30:07 +0000631 }
632
John Harrisonbc0dce32015-03-19 12:30:07 +0000633 return 0;
634}
635
John Harrisonae707972015-05-29 17:44:14 +0100636static int logical_ring_wait_for_space(struct drm_i915_gem_request *req,
Chris Wilson595e1ee2015-04-07 16:20:51 +0100637 int bytes)
John Harrisonbc0dce32015-03-19 12:30:07 +0000638{
John Harrisonae707972015-05-29 17:44:14 +0100639 struct intel_ringbuffer *ringbuf = req->ringbuf;
640 struct intel_engine_cs *ring = req->ring;
641 struct drm_i915_gem_request *target;
Chris Wilsonb4716182015-04-27 13:41:17 +0100642 unsigned space;
643 int ret;
John Harrisonbc0dce32015-03-19 12:30:07 +0000644
645 if (intel_ring_space(ringbuf) >= bytes)
646 return 0;
647
John Harrison79bbcc22015-06-30 12:40:55 +0100648 /* The whole point of reserving space is to not wait! */
649 WARN_ON(ringbuf->reserved_in_use);
650
John Harrisonae707972015-05-29 17:44:14 +0100651 list_for_each_entry(target, &ring->request_list, list) {
John Harrisonbc0dce32015-03-19 12:30:07 +0000652 /*
653 * The request queue is per-engine, so can contain requests
654 * from multiple ringbuffers. Here, we must ignore any that
655 * aren't from the ringbuffer we're considering.
656 */
John Harrisonae707972015-05-29 17:44:14 +0100657 if (target->ringbuf != ringbuf)
John Harrisonbc0dce32015-03-19 12:30:07 +0000658 continue;
659
660 /* Would completion of this request free enough space? */
John Harrisonae707972015-05-29 17:44:14 +0100661 space = __intel_ring_space(target->postfix, ringbuf->tail,
Chris Wilsonb4716182015-04-27 13:41:17 +0100662 ringbuf->size);
663 if (space >= bytes)
John Harrisonbc0dce32015-03-19 12:30:07 +0000664 break;
John Harrisonbc0dce32015-03-19 12:30:07 +0000665 }
666
John Harrisonae707972015-05-29 17:44:14 +0100667 if (WARN_ON(&target->list == &ring->request_list))
John Harrisonbc0dce32015-03-19 12:30:07 +0000668 return -ENOSPC;
669
John Harrisonae707972015-05-29 17:44:14 +0100670 ret = i915_wait_request(target);
John Harrisonbc0dce32015-03-19 12:30:07 +0000671 if (ret)
672 return ret;
673
Chris Wilsonb4716182015-04-27 13:41:17 +0100674 ringbuf->space = space;
675 return 0;
John Harrisonbc0dce32015-03-19 12:30:07 +0000676}
677
678/*
679 * intel_logical_ring_advance_and_submit() - advance the tail and submit the workload
John Harrisonae707972015-05-29 17:44:14 +0100680 * @request: Request to advance the logical ringbuffer of.
John Harrisonbc0dce32015-03-19 12:30:07 +0000681 *
682 * The tail is updated in our logical ringbuffer struct, not in the actual context. What
683 * really happens during submission is that the context and current tail will be placed
684 * on a queue waiting for the ELSP to be ready to accept a new context submission. At that
685 * point, the tail *inside* the context is updated and the ELSP written to.
686 */
687static void
John Harrisonae707972015-05-29 17:44:14 +0100688intel_logical_ring_advance_and_submit(struct drm_i915_gem_request *request)
John Harrisonbc0dce32015-03-19 12:30:07 +0000689{
John Harrisonae707972015-05-29 17:44:14 +0100690 struct intel_engine_cs *ring = request->ring;
John Harrisonbc0dce32015-03-19 12:30:07 +0000691
John Harrisonae707972015-05-29 17:44:14 +0100692 intel_logical_ring_advance(request->ringbuf);
John Harrisonbc0dce32015-03-19 12:30:07 +0000693
694 if (intel_ring_stopped(ring))
695 return;
696
John Harrisonae707972015-05-29 17:44:14 +0100697 execlists_context_queue(request);
John Harrisonbc0dce32015-03-19 12:30:07 +0000698}
699
John Harrison79bbcc22015-06-30 12:40:55 +0100700static void __wrap_ring_buffer(struct intel_ringbuffer *ringbuf)
John Harrisonbc0dce32015-03-19 12:30:07 +0000701{
702 uint32_t __iomem *virt;
703 int rem = ringbuf->size - ringbuf->tail;
704
John Harrisonbc0dce32015-03-19 12:30:07 +0000705 virt = ringbuf->virtual_start + ringbuf->tail;
706 rem /= 4;
707 while (rem--)
708 iowrite32(MI_NOOP, virt++);
709
710 ringbuf->tail = 0;
711 intel_ring_update_space(ringbuf);
John Harrisonbc0dce32015-03-19 12:30:07 +0000712}
713
John Harrisonae707972015-05-29 17:44:14 +0100714static int logical_ring_prepare(struct drm_i915_gem_request *req, int bytes)
John Harrisonbc0dce32015-03-19 12:30:07 +0000715{
John Harrisonae707972015-05-29 17:44:14 +0100716 struct intel_ringbuffer *ringbuf = req->ringbuf;
John Harrison79bbcc22015-06-30 12:40:55 +0100717 int remain_usable = ringbuf->effective_size - ringbuf->tail;
718 int remain_actual = ringbuf->size - ringbuf->tail;
719 int ret, total_bytes, wait_bytes = 0;
720 bool need_wrap = false;
John Harrisonbc0dce32015-03-19 12:30:07 +0000721
John Harrison79bbcc22015-06-30 12:40:55 +0100722 if (ringbuf->reserved_in_use)
723 total_bytes = bytes;
724 else
725 total_bytes = bytes + ringbuf->reserved_size;
John Harrison29b1b412015-06-18 13:10:09 +0100726
John Harrison79bbcc22015-06-30 12:40:55 +0100727 if (unlikely(bytes > remain_usable)) {
728 /*
729 * Not enough space for the basic request. So need to flush
730 * out the remainder and then wait for base + reserved.
731 */
732 wait_bytes = remain_actual + total_bytes;
733 need_wrap = true;
734 } else {
735 if (unlikely(total_bytes > remain_usable)) {
736 /*
737 * The base request will fit but the reserved space
738 * falls off the end. So only need to to wait for the
739 * reserved size after flushing out the remainder.
740 */
741 wait_bytes = remain_actual + ringbuf->reserved_size;
742 need_wrap = true;
743 } else if (total_bytes > ringbuf->space) {
744 /* No wrapping required, just waiting. */
745 wait_bytes = total_bytes;
John Harrison29b1b412015-06-18 13:10:09 +0100746 }
John Harrisonbc0dce32015-03-19 12:30:07 +0000747 }
748
John Harrison79bbcc22015-06-30 12:40:55 +0100749 if (wait_bytes) {
750 ret = logical_ring_wait_for_space(req, wait_bytes);
John Harrisonbc0dce32015-03-19 12:30:07 +0000751 if (unlikely(ret))
752 return ret;
John Harrison79bbcc22015-06-30 12:40:55 +0100753
754 if (need_wrap)
755 __wrap_ring_buffer(ringbuf);
John Harrisonbc0dce32015-03-19 12:30:07 +0000756 }
757
758 return 0;
759}
760
761/**
762 * intel_logical_ring_begin() - prepare the logical ringbuffer to accept some commands
763 *
John Harrison4d616a22015-05-29 17:44:08 +0100764 * @request: The request to start some new work for
Arun Siluvery4d78c8d2015-06-23 15:50:43 +0100765 * @ctx: Logical ring context whose ringbuffer is being prepared.
John Harrisonbc0dce32015-03-19 12:30:07 +0000766 * @num_dwords: number of DWORDs that we plan to write to the ringbuffer.
767 *
768 * The ringbuffer might not be ready to accept the commands right away (maybe it needs to
769 * be wrapped, or wait a bit for the tail to be updated). This function takes care of that
770 * and also preallocates a request (every workload submission is still mediated through
771 * requests, same as it did with legacy ringbuffer submission).
772 *
773 * Return: non-zero if the ringbuffer is not ready to be written to.
774 */
John Harrison4d616a22015-05-29 17:44:08 +0100775static int intel_logical_ring_begin(struct drm_i915_gem_request *req,
776 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 Siluvery17ee9502015-06-19 19:07:01 +01001068#define wa_ctx_emit(batch, cmd) \
1069 do { \
1070 if (WARN_ON(index >= (PAGE_SIZE / sizeof(uint32_t)))) { \
1071 return -ENOSPC; \
1072 } \
1073 batch[index++] = (cmd); \
1074 } while (0)
1075
Arun Siluvery9e000842015-07-03 14:27:31 +01001076
1077/*
1078 * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
1079 * PIPE_CONTROL instruction. This is required for the flush to happen correctly
1080 * but there is a slight complication as this is applied in WA batch where the
1081 * values are only initialized once so we cannot take register value at the
1082 * beginning and reuse it further; hence we save its value to memory, upload a
1083 * constant value with bit21 set and then we restore it back with the saved value.
1084 * To simplify the WA, a constant value is formed by using the default value
1085 * of this register. This shouldn't be a problem because we are only modifying
1086 * it for a short period and this batch in non-premptible. We can ofcourse
1087 * use additional instructions that read the actual value of the register
1088 * at that time and set our bit of interest but it makes the WA complicated.
1089 *
1090 * This WA is also required for Gen9 so extracting as a function avoids
1091 * code duplication.
1092 */
1093static inline int gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *ring,
1094 uint32_t *const batch,
1095 uint32_t index)
1096{
1097 uint32_t l3sqc4_flush = (0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES);
1098
1099 wa_ctx_emit(batch, (MI_STORE_REGISTER_MEM_GEN8(1) |
1100 MI_SRM_LRM_GLOBAL_GTT));
1101 wa_ctx_emit(batch, GEN8_L3SQCREG4);
1102 wa_ctx_emit(batch, ring->scratch.gtt_offset + 256);
1103 wa_ctx_emit(batch, 0);
1104
1105 wa_ctx_emit(batch, MI_LOAD_REGISTER_IMM(1));
1106 wa_ctx_emit(batch, GEN8_L3SQCREG4);
1107 wa_ctx_emit(batch, l3sqc4_flush);
1108
1109 wa_ctx_emit(batch, GFX_OP_PIPE_CONTROL(6));
1110 wa_ctx_emit(batch, (PIPE_CONTROL_CS_STALL |
1111 PIPE_CONTROL_DC_FLUSH_ENABLE));
1112 wa_ctx_emit(batch, 0);
1113 wa_ctx_emit(batch, 0);
1114 wa_ctx_emit(batch, 0);
1115 wa_ctx_emit(batch, 0);
1116
1117 wa_ctx_emit(batch, (MI_LOAD_REGISTER_MEM_GEN8(1) |
1118 MI_SRM_LRM_GLOBAL_GTT));
1119 wa_ctx_emit(batch, GEN8_L3SQCREG4);
1120 wa_ctx_emit(batch, ring->scratch.gtt_offset + 256);
1121 wa_ctx_emit(batch, 0);
1122
1123 return index;
1124}
1125
Arun Siluvery17ee9502015-06-19 19:07:01 +01001126static inline uint32_t wa_ctx_start(struct i915_wa_ctx_bb *wa_ctx,
1127 uint32_t offset,
1128 uint32_t start_alignment)
1129{
1130 return wa_ctx->offset = ALIGN(offset, start_alignment);
1131}
1132
1133static inline int wa_ctx_end(struct i915_wa_ctx_bb *wa_ctx,
1134 uint32_t offset,
1135 uint32_t size_alignment)
1136{
1137 wa_ctx->size = offset - wa_ctx->offset;
1138
1139 WARN(wa_ctx->size % size_alignment,
1140 "wa_ctx_bb failed sanity checks: size %d is not aligned to %d\n",
1141 wa_ctx->size, size_alignment);
1142 return 0;
1143}
1144
1145/**
1146 * gen8_init_indirectctx_bb() - initialize indirect ctx batch with WA
1147 *
1148 * @ring: only applicable for RCS
1149 * @wa_ctx: structure representing wa_ctx
1150 * offset: specifies start of the batch, should be cache-aligned. This is updated
1151 * with the offset value received as input.
1152 * size: size of the batch in DWORDS but HW expects in terms of cachelines
1153 * @batch: page in which WA are loaded
1154 * @offset: This field specifies the start of the batch, it should be
1155 * cache-aligned otherwise it is adjusted accordingly.
1156 * Typically we only have one indirect_ctx and per_ctx batch buffer which are
1157 * initialized at the beginning and shared across all contexts but this field
1158 * helps us to have multiple batches at different offsets and select them based
1159 * on a criteria. At the moment this batch always start at the beginning of the page
1160 * and at this point we don't have multiple wa_ctx batch buffers.
1161 *
1162 * The number of WA applied are not known at the beginning; we use this field
1163 * to return the no of DWORDS written.
Arun Siluvery4d78c8d2015-06-23 15:50:43 +01001164 *
Arun Siluvery17ee9502015-06-19 19:07:01 +01001165 * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
1166 * so it adds NOOPs as padding to make it cacheline aligned.
1167 * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
1168 * makes a complete batch buffer.
1169 *
1170 * Return: non-zero if we exceed the PAGE_SIZE limit.
1171 */
1172
1173static int gen8_init_indirectctx_bb(struct intel_engine_cs *ring,
1174 struct i915_wa_ctx_bb *wa_ctx,
1175 uint32_t *const batch,
1176 uint32_t *offset)
1177{
Arun Siluvery0160f052015-06-23 15:46:57 +01001178 uint32_t scratch_addr;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001179 uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
1180
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001181 /* WaDisableCtxRestoreArbitration:bdw,chv */
1182 wa_ctx_emit(batch, MI_ARB_ON_OFF | MI_ARB_DISABLE);
Arun Siluvery17ee9502015-06-19 19:07:01 +01001183
Arun Siluveryc82435b2015-06-19 18:37:13 +01001184 /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
1185 if (IS_BROADWELL(ring->dev)) {
Arun Siluvery9e000842015-07-03 14:27:31 +01001186 index = gen8_emit_flush_coherentl3_wa(ring, batch, index);
1187 if (index < 0)
1188 return index;
Arun Siluveryc82435b2015-06-19 18:37:13 +01001189 }
1190
Arun Siluvery0160f052015-06-23 15:46:57 +01001191 /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
1192 /* Actual scratch location is at 128 bytes offset */
1193 scratch_addr = ring->scratch.gtt_offset + 2*CACHELINE_BYTES;
1194
1195 wa_ctx_emit(batch, GFX_OP_PIPE_CONTROL(6));
1196 wa_ctx_emit(batch, (PIPE_CONTROL_FLUSH_L3 |
1197 PIPE_CONTROL_GLOBAL_GTT_IVB |
1198 PIPE_CONTROL_CS_STALL |
1199 PIPE_CONTROL_QW_WRITE));
1200 wa_ctx_emit(batch, scratch_addr);
1201 wa_ctx_emit(batch, 0);
1202 wa_ctx_emit(batch, 0);
1203 wa_ctx_emit(batch, 0);
1204
Arun Siluvery17ee9502015-06-19 19:07:01 +01001205 /* Pad to end of cacheline */
1206 while (index % CACHELINE_DWORDS)
1207 wa_ctx_emit(batch, MI_NOOP);
1208
1209 /*
1210 * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
1211 * execution depends on the length specified in terms of cache lines
1212 * in the register CTX_RCS_INDIRECT_CTX
1213 */
1214
1215 return wa_ctx_end(wa_ctx, *offset = index, CACHELINE_DWORDS);
1216}
1217
1218/**
1219 * gen8_init_perctx_bb() - initialize per ctx batch with WA
1220 *
1221 * @ring: only applicable for RCS
1222 * @wa_ctx: structure representing wa_ctx
1223 * offset: specifies start of the batch, should be cache-aligned.
1224 * size: size of the batch in DWORDS but HW expects in terms of cachelines
Arun Siluvery4d78c8d2015-06-23 15:50:43 +01001225 * @batch: page in which WA are loaded
Arun Siluvery17ee9502015-06-19 19:07:01 +01001226 * @offset: This field specifies the start of this batch.
1227 * This batch is started immediately after indirect_ctx batch. Since we ensure
1228 * that indirect_ctx ends on a cacheline this batch is aligned automatically.
1229 *
1230 * The number of DWORDS written are returned using this field.
1231 *
1232 * This batch is terminated with MI_BATCH_BUFFER_END and so we need not add padding
1233 * to align it with cacheline as padding after MI_BATCH_BUFFER_END is redundant.
1234 */
1235static int gen8_init_perctx_bb(struct intel_engine_cs *ring,
1236 struct i915_wa_ctx_bb *wa_ctx,
1237 uint32_t *const batch,
1238 uint32_t *offset)
1239{
1240 uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
1241
Arun Siluvery7ad00d12015-06-19 18:37:12 +01001242 /* WaDisableCtxRestoreArbitration:bdw,chv */
1243 wa_ctx_emit(batch, MI_ARB_ON_OFF | MI_ARB_ENABLE);
1244
Arun Siluvery17ee9502015-06-19 19:07:01 +01001245 wa_ctx_emit(batch, MI_BATCH_BUFFER_END);
1246
1247 return wa_ctx_end(wa_ctx, *offset = index, 1);
1248}
1249
1250static int lrc_setup_wa_ctx_obj(struct intel_engine_cs *ring, u32 size)
1251{
1252 int ret;
1253
1254 ring->wa_ctx.obj = i915_gem_alloc_object(ring->dev, PAGE_ALIGN(size));
1255 if (!ring->wa_ctx.obj) {
1256 DRM_DEBUG_DRIVER("alloc LRC WA ctx backing obj failed.\n");
1257 return -ENOMEM;
1258 }
1259
1260 ret = i915_gem_obj_ggtt_pin(ring->wa_ctx.obj, PAGE_SIZE, 0);
1261 if (ret) {
1262 DRM_DEBUG_DRIVER("pin LRC WA ctx backing obj failed: %d\n",
1263 ret);
1264 drm_gem_object_unreference(&ring->wa_ctx.obj->base);
1265 return ret;
1266 }
1267
1268 return 0;
1269}
1270
1271static void lrc_destroy_wa_ctx_obj(struct intel_engine_cs *ring)
1272{
1273 if (ring->wa_ctx.obj) {
1274 i915_gem_object_ggtt_unpin(ring->wa_ctx.obj);
1275 drm_gem_object_unreference(&ring->wa_ctx.obj->base);
1276 ring->wa_ctx.obj = NULL;
1277 }
1278}
1279
1280static int intel_init_workaround_bb(struct intel_engine_cs *ring)
1281{
1282 int ret;
1283 uint32_t *batch;
1284 uint32_t offset;
1285 struct page *page;
1286 struct i915_ctx_workarounds *wa_ctx = &ring->wa_ctx;
1287
1288 WARN_ON(ring->id != RCS);
1289
Arun Siluvery5e60d792015-06-23 15:50:44 +01001290 /* update this when WA for higher Gen are added */
1291 if (WARN(INTEL_INFO(ring->dev)->gen > 8,
1292 "WA batch buffer is not initialized for Gen%d\n",
1293 INTEL_INFO(ring->dev)->gen))
1294 return 0;
1295
Arun Siluveryc4db7592015-06-19 18:37:11 +01001296 /* some WA perform writes to scratch page, ensure it is valid */
1297 if (ring->scratch.obj == NULL) {
1298 DRM_ERROR("scratch page not allocated for %s\n", ring->name);
1299 return -EINVAL;
1300 }
1301
Arun Siluvery17ee9502015-06-19 19:07:01 +01001302 ret = lrc_setup_wa_ctx_obj(ring, PAGE_SIZE);
1303 if (ret) {
1304 DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
1305 return ret;
1306 }
1307
1308 page = i915_gem_object_get_page(wa_ctx->obj, 0);
1309 batch = kmap_atomic(page);
1310 offset = 0;
1311
1312 if (INTEL_INFO(ring->dev)->gen == 8) {
1313 ret = gen8_init_indirectctx_bb(ring,
1314 &wa_ctx->indirect_ctx,
1315 batch,
1316 &offset);
1317 if (ret)
1318 goto out;
1319
1320 ret = gen8_init_perctx_bb(ring,
1321 &wa_ctx->per_ctx,
1322 batch,
1323 &offset);
1324 if (ret)
1325 goto out;
Arun Siluvery17ee9502015-06-19 19:07:01 +01001326 }
1327
1328out:
1329 kunmap_atomic(batch);
1330 if (ret)
1331 lrc_destroy_wa_ctx_obj(ring);
1332
1333 return ret;
1334}
1335
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001336static int gen8_init_common_ring(struct intel_engine_cs *ring)
1337{
1338 struct drm_device *dev = ring->dev;
1339 struct drm_i915_private *dev_priv = dev->dev_private;
1340
Oscar Mateo73d477f2014-07-24 17:04:31 +01001341 I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
1342 I915_WRITE(RING_HWSTAM(ring->mmio_base), 0xffffffff);
1343
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001344 I915_WRITE(RING_MODE_GEN7(ring),
1345 _MASKED_BIT_DISABLE(GFX_REPLAY_MODE) |
1346 _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
1347 POSTING_READ(RING_MODE_GEN7(ring));
Thomas Danielc0a03a22015-01-09 11:09:37 +00001348 ring->next_context_status_buffer = 0;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001349 DRM_DEBUG_DRIVER("Execlists enabled for %s\n", ring->name);
1350
1351 memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
1352
1353 return 0;
1354}
1355
1356static int gen8_init_render_ring(struct intel_engine_cs *ring)
1357{
1358 struct drm_device *dev = ring->dev;
1359 struct drm_i915_private *dev_priv = dev->dev_private;
1360 int ret;
1361
1362 ret = gen8_init_common_ring(ring);
1363 if (ret)
1364 return ret;
1365
1366 /* We need to disable the AsyncFlip performance optimisations in order
1367 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1368 * programmed to '1' on all products.
1369 *
1370 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
1371 */
1372 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1373
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001374 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1375
Michel Thierry771b9a52014-11-11 16:47:33 +00001376 return init_workarounds_ring(ring);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001377}
1378
Damien Lespiau82ef8222015-02-09 19:33:08 +00001379static int gen9_init_render_ring(struct intel_engine_cs *ring)
1380{
1381 int ret;
1382
1383 ret = gen8_init_common_ring(ring);
1384 if (ret)
1385 return ret;
1386
1387 return init_workarounds_ring(ring);
1388}
1389
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001390static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request *req)
1391{
1392 struct i915_hw_ppgtt *ppgtt = req->ctx->ppgtt;
1393 struct intel_engine_cs *ring = req->ring;
1394 struct intel_ringbuffer *ringbuf = req->ringbuf;
1395 const int num_lri_cmds = GEN8_LEGACY_PDPES * 2;
1396 int i, ret;
1397
1398 ret = intel_logical_ring_begin(req, num_lri_cmds * 2 + 2);
1399 if (ret)
1400 return ret;
1401
1402 intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(num_lri_cmds));
1403 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
1404 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
1405
1406 intel_logical_ring_emit(ringbuf, GEN8_RING_PDP_UDW(ring, i));
1407 intel_logical_ring_emit(ringbuf, upper_32_bits(pd_daddr));
1408 intel_logical_ring_emit(ringbuf, GEN8_RING_PDP_LDW(ring, i));
1409 intel_logical_ring_emit(ringbuf, lower_32_bits(pd_daddr));
1410 }
1411
1412 intel_logical_ring_emit(ringbuf, MI_NOOP);
1413 intel_logical_ring_advance(ringbuf);
1414
1415 return 0;
1416}
1417
John Harrisonbe795fc2015-05-29 17:44:03 +01001418static int gen8_emit_bb_start(struct drm_i915_gem_request *req,
John Harrison8e004ef2015-02-13 11:48:10 +00001419 u64 offset, unsigned dispatch_flags)
Oscar Mateo15648582014-07-24 17:04:32 +01001420{
John Harrisonbe795fc2015-05-29 17:44:03 +01001421 struct intel_ringbuffer *ringbuf = req->ringbuf;
John Harrison8e004ef2015-02-13 11:48:10 +00001422 bool ppgtt = !(dispatch_flags & I915_DISPATCH_SECURE);
Oscar Mateo15648582014-07-24 17:04:32 +01001423 int ret;
1424
Michel Thierry7a01a0a2015-06-26 13:46:14 +01001425 /* Don't rely in hw updating PDPs, specially in lite-restore.
1426 * Ideally, we should set Force PD Restore in ctx descriptor,
1427 * but we can't. Force Restore would be a second option, but
1428 * it is unsafe in case of lite-restore (because the ctx is
1429 * not idle). */
1430 if (req->ctx->ppgtt &&
1431 (intel_ring_flag(req->ring) & req->ctx->ppgtt->pd_dirty_rings)) {
1432 ret = intel_logical_ring_emit_pdps(req);
1433 if (ret)
1434 return ret;
1435
1436 req->ctx->ppgtt->pd_dirty_rings &= ~intel_ring_flag(req->ring);
1437 }
1438
John Harrison4d616a22015-05-29 17:44:08 +01001439 ret = intel_logical_ring_begin(req, 4);
Oscar Mateo15648582014-07-24 17:04:32 +01001440 if (ret)
1441 return ret;
1442
1443 /* FIXME(BDW): Address space and security selectors. */
Abdiel Janulgue69225282015-06-16 13:39:42 +03001444 intel_logical_ring_emit(ringbuf, MI_BATCH_BUFFER_START_GEN8 |
1445 (ppgtt<<8) |
1446 (dispatch_flags & I915_DISPATCH_RS ?
1447 MI_BATCH_RESOURCE_STREAMER : 0));
Oscar Mateo15648582014-07-24 17:04:32 +01001448 intel_logical_ring_emit(ringbuf, lower_32_bits(offset));
1449 intel_logical_ring_emit(ringbuf, upper_32_bits(offset));
1450 intel_logical_ring_emit(ringbuf, MI_NOOP);
1451 intel_logical_ring_advance(ringbuf);
1452
1453 return 0;
1454}
1455
Oscar Mateo73d477f2014-07-24 17:04:31 +01001456static bool gen8_logical_ring_get_irq(struct intel_engine_cs *ring)
1457{
1458 struct drm_device *dev = ring->dev;
1459 struct drm_i915_private *dev_priv = dev->dev_private;
1460 unsigned long flags;
1461
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001462 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Oscar Mateo73d477f2014-07-24 17:04:31 +01001463 return false;
1464
1465 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1466 if (ring->irq_refcount++ == 0) {
1467 I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
1468 POSTING_READ(RING_IMR(ring->mmio_base));
1469 }
1470 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1471
1472 return true;
1473}
1474
1475static void gen8_logical_ring_put_irq(struct intel_engine_cs *ring)
1476{
1477 struct drm_device *dev = ring->dev;
1478 struct drm_i915_private *dev_priv = dev->dev_private;
1479 unsigned long flags;
1480
1481 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1482 if (--ring->irq_refcount == 0) {
1483 I915_WRITE_IMR(ring, ~ring->irq_keep_mask);
1484 POSTING_READ(RING_IMR(ring->mmio_base));
1485 }
1486 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1487}
1488
John Harrison7deb4d32015-05-29 17:43:59 +01001489static int gen8_emit_flush(struct drm_i915_gem_request *request,
Oscar Mateo47122742014-07-24 17:04:28 +01001490 u32 invalidate_domains,
1491 u32 unused)
1492{
John Harrison7deb4d32015-05-29 17:43:59 +01001493 struct intel_ringbuffer *ringbuf = request->ringbuf;
Oscar Mateo47122742014-07-24 17:04:28 +01001494 struct intel_engine_cs *ring = ringbuf->ring;
1495 struct drm_device *dev = ring->dev;
1496 struct drm_i915_private *dev_priv = dev->dev_private;
1497 uint32_t cmd;
1498 int ret;
1499
John Harrison4d616a22015-05-29 17:44:08 +01001500 ret = intel_logical_ring_begin(request, 4);
Oscar Mateo47122742014-07-24 17:04:28 +01001501 if (ret)
1502 return ret;
1503
1504 cmd = MI_FLUSH_DW + 1;
1505
Chris Wilsonf0a1fb12015-01-22 13:42:00 +00001506 /* We always require a command barrier so that subsequent
1507 * commands, such as breadcrumb interrupts, are strictly ordered
1508 * wrt the contents of the write cache being flushed to memory
1509 * (and thus being coherent from the CPU).
1510 */
1511 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1512
1513 if (invalidate_domains & I915_GEM_GPU_DOMAINS) {
1514 cmd |= MI_INVALIDATE_TLB;
1515 if (ring == &dev_priv->ring[VCS])
1516 cmd |= MI_INVALIDATE_BSD;
Oscar Mateo47122742014-07-24 17:04:28 +01001517 }
1518
1519 intel_logical_ring_emit(ringbuf, cmd);
1520 intel_logical_ring_emit(ringbuf,
1521 I915_GEM_HWS_SCRATCH_ADDR |
1522 MI_FLUSH_DW_USE_GTT);
1523 intel_logical_ring_emit(ringbuf, 0); /* upper addr */
1524 intel_logical_ring_emit(ringbuf, 0); /* value */
1525 intel_logical_ring_advance(ringbuf);
1526
1527 return 0;
1528}
1529
John Harrison7deb4d32015-05-29 17:43:59 +01001530static int gen8_emit_flush_render(struct drm_i915_gem_request *request,
Oscar Mateo47122742014-07-24 17:04:28 +01001531 u32 invalidate_domains,
1532 u32 flush_domains)
1533{
John Harrison7deb4d32015-05-29 17:43:59 +01001534 struct intel_ringbuffer *ringbuf = request->ringbuf;
Oscar Mateo47122742014-07-24 17:04:28 +01001535 struct intel_engine_cs *ring = ringbuf->ring;
1536 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
Imre Deak9647ff32015-01-25 13:27:11 -08001537 bool vf_flush_wa;
Oscar Mateo47122742014-07-24 17:04:28 +01001538 u32 flags = 0;
1539 int ret;
1540
1541 flags |= PIPE_CONTROL_CS_STALL;
1542
1543 if (flush_domains) {
1544 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
1545 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
1546 }
1547
1548 if (invalidate_domains) {
1549 flags |= PIPE_CONTROL_TLB_INVALIDATE;
1550 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
1551 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
1552 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
1553 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
1554 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
1555 flags |= PIPE_CONTROL_QW_WRITE;
1556 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
1557 }
1558
Imre Deak9647ff32015-01-25 13:27:11 -08001559 /*
1560 * On GEN9+ Before VF_CACHE_INVALIDATE we need to emit a NULL pipe
1561 * control.
1562 */
1563 vf_flush_wa = INTEL_INFO(ring->dev)->gen >= 9 &&
1564 flags & PIPE_CONTROL_VF_CACHE_INVALIDATE;
1565
John Harrison4d616a22015-05-29 17:44:08 +01001566 ret = intel_logical_ring_begin(request, vf_flush_wa ? 12 : 6);
Oscar Mateo47122742014-07-24 17:04:28 +01001567 if (ret)
1568 return ret;
1569
Imre Deak9647ff32015-01-25 13:27:11 -08001570 if (vf_flush_wa) {
1571 intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
1572 intel_logical_ring_emit(ringbuf, 0);
1573 intel_logical_ring_emit(ringbuf, 0);
1574 intel_logical_ring_emit(ringbuf, 0);
1575 intel_logical_ring_emit(ringbuf, 0);
1576 intel_logical_ring_emit(ringbuf, 0);
1577 }
1578
Oscar Mateo47122742014-07-24 17:04:28 +01001579 intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
1580 intel_logical_ring_emit(ringbuf, flags);
1581 intel_logical_ring_emit(ringbuf, scratch_addr);
1582 intel_logical_ring_emit(ringbuf, 0);
1583 intel_logical_ring_emit(ringbuf, 0);
1584 intel_logical_ring_emit(ringbuf, 0);
1585 intel_logical_ring_advance(ringbuf);
1586
1587 return 0;
1588}
1589
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001590static u32 gen8_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
1591{
1592 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1593}
1594
1595static void gen8_set_seqno(struct intel_engine_cs *ring, u32 seqno)
1596{
1597 intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
1598}
1599
John Harrisonc4e76632015-05-29 17:44:01 +01001600static int gen8_emit_request(struct drm_i915_gem_request *request)
Oscar Mateo4da46e12014-07-24 17:04:27 +01001601{
John Harrisonc4e76632015-05-29 17:44:01 +01001602 struct intel_ringbuffer *ringbuf = request->ringbuf;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001603 struct intel_engine_cs *ring = ringbuf->ring;
1604 u32 cmd;
1605 int ret;
1606
Michel Thierry53292cd2015-04-15 18:11:33 +01001607 /*
1608 * Reserve space for 2 NOOPs at the end of each request to be
1609 * used as a workaround for not being allowed to do lite
1610 * restore with HEAD==TAIL (WaIdleLiteRestore).
1611 */
John Harrison4d616a22015-05-29 17:44:08 +01001612 ret = intel_logical_ring_begin(request, 8);
Oscar Mateo4da46e12014-07-24 17:04:27 +01001613 if (ret)
1614 return ret;
1615
Ville Syrjälä8edfbb82014-11-14 18:16:56 +02001616 cmd = MI_STORE_DWORD_IMM_GEN4;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001617 cmd |= MI_GLOBAL_GTT;
1618
1619 intel_logical_ring_emit(ringbuf, cmd);
1620 intel_logical_ring_emit(ringbuf,
1621 (ring->status_page.gfx_addr +
1622 (I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT)));
1623 intel_logical_ring_emit(ringbuf, 0);
John Harrisonc4e76632015-05-29 17:44:01 +01001624 intel_logical_ring_emit(ringbuf, i915_gem_request_get_seqno(request));
Oscar Mateo4da46e12014-07-24 17:04:27 +01001625 intel_logical_ring_emit(ringbuf, MI_USER_INTERRUPT);
1626 intel_logical_ring_emit(ringbuf, MI_NOOP);
John Harrisonae707972015-05-29 17:44:14 +01001627 intel_logical_ring_advance_and_submit(request);
Oscar Mateo4da46e12014-07-24 17:04:27 +01001628
Michel Thierry53292cd2015-04-15 18:11:33 +01001629 /*
1630 * Here we add two extra NOOPs as padding to avoid
1631 * lite restore of a context with HEAD==TAIL.
1632 */
1633 intel_logical_ring_emit(ringbuf, MI_NOOP);
1634 intel_logical_ring_emit(ringbuf, MI_NOOP);
1635 intel_logical_ring_advance(ringbuf);
1636
Oscar Mateo4da46e12014-07-24 17:04:27 +01001637 return 0;
1638}
1639
John Harrisonbe013632015-05-29 17:43:45 +01001640static int intel_lr_context_render_state_init(struct drm_i915_gem_request *req)
Damien Lespiaucef437a2015-02-10 19:32:19 +00001641{
Damien Lespiaucef437a2015-02-10 19:32:19 +00001642 struct render_state so;
Damien Lespiaucef437a2015-02-10 19:32:19 +00001643 int ret;
1644
John Harrisonbe013632015-05-29 17:43:45 +01001645 ret = i915_gem_render_state_prepare(req->ring, &so);
Damien Lespiaucef437a2015-02-10 19:32:19 +00001646 if (ret)
1647 return ret;
1648
1649 if (so.rodata == NULL)
1650 return 0;
1651
John Harrisonbe795fc2015-05-29 17:44:03 +01001652 ret = req->ring->emit_bb_start(req, so.ggtt_offset,
John Harrisonbe013632015-05-29 17:43:45 +01001653 I915_DISPATCH_SECURE);
Damien Lespiaucef437a2015-02-10 19:32:19 +00001654 if (ret)
1655 goto out;
1656
John Harrisonb2af0372015-05-29 17:43:50 +01001657 i915_vma_move_to_active(i915_gem_obj_to_ggtt(so.obj), req);
Damien Lespiaucef437a2015-02-10 19:32:19 +00001658
Damien Lespiaucef437a2015-02-10 19:32:19 +00001659out:
1660 i915_gem_render_state_fini(&so);
1661 return ret;
1662}
1663
John Harrison87531812015-05-29 17:43:44 +01001664static int gen8_init_rcs_context(struct drm_i915_gem_request *req)
Thomas Daniele7778be2014-12-02 12:50:48 +00001665{
1666 int ret;
1667
John Harrisone2be4fa2015-05-29 17:43:54 +01001668 ret = intel_logical_ring_workarounds_emit(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001669 if (ret)
1670 return ret;
1671
John Harrisonbe013632015-05-29 17:43:45 +01001672 return intel_lr_context_render_state_init(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00001673}
1674
Oscar Mateo73e4d072014-07-24 17:04:48 +01001675/**
1676 * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
1677 *
1678 * @ring: Engine Command Streamer.
1679 *
1680 */
Oscar Mateo454afeb2014-07-24 17:04:22 +01001681void intel_logical_ring_cleanup(struct intel_engine_cs *ring)
1682{
John Harrison6402c332014-10-31 12:00:26 +00001683 struct drm_i915_private *dev_priv;
Oscar Mateo9832b9d2014-07-24 17:04:30 +01001684
Oscar Mateo48d82382014-07-24 17:04:23 +01001685 if (!intel_ring_initialized(ring))
1686 return;
1687
John Harrison6402c332014-10-31 12:00:26 +00001688 dev_priv = ring->dev->dev_private;
1689
Oscar Mateo9832b9d2014-07-24 17:04:30 +01001690 intel_logical_ring_stop(ring);
1691 WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
Oscar Mateo48d82382014-07-24 17:04:23 +01001692
1693 if (ring->cleanup)
1694 ring->cleanup(ring);
1695
1696 i915_cmd_parser_fini_ring(ring);
Chris Wilson06fbca72015-04-07 16:20:36 +01001697 i915_gem_batch_pool_fini(&ring->batch_pool);
Oscar Mateo48d82382014-07-24 17:04:23 +01001698
1699 if (ring->status_page.obj) {
1700 kunmap(sg_page(ring->status_page.obj->pages->sgl));
1701 ring->status_page.obj = NULL;
1702 }
Arun Siluvery17ee9502015-06-19 19:07:01 +01001703
1704 lrc_destroy_wa_ctx_obj(ring);
Oscar Mateo454afeb2014-07-24 17:04:22 +01001705}
1706
1707static int logical_ring_init(struct drm_device *dev, struct intel_engine_cs *ring)
1708{
Oscar Mateo48d82382014-07-24 17:04:23 +01001709 int ret;
Oscar Mateo48d82382014-07-24 17:04:23 +01001710
1711 /* Intentionally left blank. */
1712 ring->buffer = NULL;
1713
1714 ring->dev = dev;
1715 INIT_LIST_HEAD(&ring->active_list);
1716 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson06fbca72015-04-07 16:20:36 +01001717 i915_gem_batch_pool_init(dev, &ring->batch_pool);
Oscar Mateo48d82382014-07-24 17:04:23 +01001718 init_waitqueue_head(&ring->irq_queue);
1719
Michel Thierryacdd8842014-07-24 17:04:38 +01001720 INIT_LIST_HEAD(&ring->execlist_queue);
Thomas Danielc86ee3a92014-11-13 10:27:05 +00001721 INIT_LIST_HEAD(&ring->execlist_retired_req_list);
Michel Thierryacdd8842014-07-24 17:04:38 +01001722 spin_lock_init(&ring->execlist_lock);
1723
Oscar Mateo48d82382014-07-24 17:04:23 +01001724 ret = i915_cmd_parser_init_ring(ring);
1725 if (ret)
1726 return ret;
1727
Oscar Mateo564ddb22014-08-21 11:40:54 +01001728 ret = intel_lr_context_deferred_create(ring->default_context, ring);
1729
1730 return ret;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001731}
1732
1733static int logical_render_ring_init(struct drm_device *dev)
1734{
1735 struct drm_i915_private *dev_priv = dev->dev_private;
1736 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Daniel Vetter99be1df2014-11-20 00:33:06 +01001737 int ret;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001738
1739 ring->name = "render ring";
1740 ring->id = RCS;
1741 ring->mmio_base = RENDER_RING_BASE;
1742 ring->irq_enable_mask =
1743 GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001744 ring->irq_keep_mask =
1745 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
1746 if (HAS_L3_DPF(dev))
1747 ring->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001748
Damien Lespiau82ef8222015-02-09 19:33:08 +00001749 if (INTEL_INFO(dev)->gen >= 9)
1750 ring->init_hw = gen9_init_render_ring;
1751 else
1752 ring->init_hw = gen8_init_render_ring;
Thomas Daniele7778be2014-12-02 12:50:48 +00001753 ring->init_context = gen8_init_rcs_context;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001754 ring->cleanup = intel_fini_pipe_control;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001755 ring->get_seqno = gen8_get_seqno;
1756 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001757 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001758 ring->emit_flush = gen8_emit_flush_render;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001759 ring->irq_get = gen8_logical_ring_get_irq;
1760 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001761 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001762
Daniel Vetter99be1df2014-11-20 00:33:06 +01001763 ring->dev = dev;
Arun Siluveryc4db7592015-06-19 18:37:11 +01001764
1765 ret = intel_init_pipe_control(ring);
Daniel Vetter99be1df2014-11-20 00:33:06 +01001766 if (ret)
1767 return ret;
1768
Arun Siluvery17ee9502015-06-19 19:07:01 +01001769 ret = intel_init_workaround_bb(ring);
1770 if (ret) {
1771 /*
1772 * We continue even if we fail to initialize WA batch
1773 * because we only expect rare glitches but nothing
1774 * critical to prevent us from using GPU
1775 */
1776 DRM_ERROR("WA batch buffer initialization failed: %d\n",
1777 ret);
1778 }
1779
Arun Siluveryc4db7592015-06-19 18:37:11 +01001780 ret = logical_ring_init(dev, ring);
1781 if (ret) {
Arun Siluvery17ee9502015-06-19 19:07:01 +01001782 lrc_destroy_wa_ctx_obj(ring);
Arun Siluveryc4db7592015-06-19 18:37:11 +01001783 }
Arun Siluvery17ee9502015-06-19 19:07:01 +01001784
1785 return ret;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001786}
1787
1788static int logical_bsd_ring_init(struct drm_device *dev)
1789{
1790 struct drm_i915_private *dev_priv = dev->dev_private;
1791 struct intel_engine_cs *ring = &dev_priv->ring[VCS];
1792
1793 ring->name = "bsd ring";
1794 ring->id = VCS;
1795 ring->mmio_base = GEN6_BSD_RING_BASE;
1796 ring->irq_enable_mask =
1797 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001798 ring->irq_keep_mask =
1799 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001800
Daniel Vetterecfe00d2014-11-20 00:33:04 +01001801 ring->init_hw = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001802 ring->get_seqno = gen8_get_seqno;
1803 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001804 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001805 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001806 ring->irq_get = gen8_logical_ring_get_irq;
1807 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001808 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001809
Oscar Mateo454afeb2014-07-24 17:04:22 +01001810 return logical_ring_init(dev, ring);
1811}
1812
1813static int logical_bsd2_ring_init(struct drm_device *dev)
1814{
1815 struct drm_i915_private *dev_priv = dev->dev_private;
1816 struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
1817
1818 ring->name = "bds2 ring";
1819 ring->id = VCS2;
1820 ring->mmio_base = GEN8_BSD2_RING_BASE;
1821 ring->irq_enable_mask =
1822 GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001823 ring->irq_keep_mask =
1824 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001825
Daniel Vetterecfe00d2014-11-20 00:33:04 +01001826 ring->init_hw = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001827 ring->get_seqno = gen8_get_seqno;
1828 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001829 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001830 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001831 ring->irq_get = gen8_logical_ring_get_irq;
1832 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001833 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001834
Oscar Mateo454afeb2014-07-24 17:04:22 +01001835 return logical_ring_init(dev, ring);
1836}
1837
1838static int logical_blt_ring_init(struct drm_device *dev)
1839{
1840 struct drm_i915_private *dev_priv = dev->dev_private;
1841 struct intel_engine_cs *ring = &dev_priv->ring[BCS];
1842
1843 ring->name = "blitter ring";
1844 ring->id = BCS;
1845 ring->mmio_base = BLT_RING_BASE;
1846 ring->irq_enable_mask =
1847 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001848 ring->irq_keep_mask =
1849 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001850
Daniel Vetterecfe00d2014-11-20 00:33:04 +01001851 ring->init_hw = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001852 ring->get_seqno = gen8_get_seqno;
1853 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001854 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001855 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001856 ring->irq_get = gen8_logical_ring_get_irq;
1857 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001858 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001859
Oscar Mateo454afeb2014-07-24 17:04:22 +01001860 return logical_ring_init(dev, ring);
1861}
1862
1863static int logical_vebox_ring_init(struct drm_device *dev)
1864{
1865 struct drm_i915_private *dev_priv = dev->dev_private;
1866 struct intel_engine_cs *ring = &dev_priv->ring[VECS];
1867
1868 ring->name = "video enhancement ring";
1869 ring->id = VECS;
1870 ring->mmio_base = VEBOX_RING_BASE;
1871 ring->irq_enable_mask =
1872 GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001873 ring->irq_keep_mask =
1874 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001875
Daniel Vetterecfe00d2014-11-20 00:33:04 +01001876 ring->init_hw = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001877 ring->get_seqno = gen8_get_seqno;
1878 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001879 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001880 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001881 ring->irq_get = gen8_logical_ring_get_irq;
1882 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001883 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001884
Oscar Mateo454afeb2014-07-24 17:04:22 +01001885 return logical_ring_init(dev, ring);
1886}
1887
Oscar Mateo73e4d072014-07-24 17:04:48 +01001888/**
1889 * intel_logical_rings_init() - allocate, populate and init the Engine Command Streamers
1890 * @dev: DRM device.
1891 *
1892 * This function inits the engines for an Execlists submission style (the equivalent in the
1893 * legacy ringbuffer submission world would be i915_gem_init_rings). It does it only for
1894 * those engines that are present in the hardware.
1895 *
1896 * Return: non-zero if the initialization failed.
1897 */
Oscar Mateo454afeb2014-07-24 17:04:22 +01001898int intel_logical_rings_init(struct drm_device *dev)
1899{
1900 struct drm_i915_private *dev_priv = dev->dev_private;
1901 int ret;
1902
1903 ret = logical_render_ring_init(dev);
1904 if (ret)
1905 return ret;
1906
1907 if (HAS_BSD(dev)) {
1908 ret = logical_bsd_ring_init(dev);
1909 if (ret)
1910 goto cleanup_render_ring;
1911 }
1912
1913 if (HAS_BLT(dev)) {
1914 ret = logical_blt_ring_init(dev);
1915 if (ret)
1916 goto cleanup_bsd_ring;
1917 }
1918
1919 if (HAS_VEBOX(dev)) {
1920 ret = logical_vebox_ring_init(dev);
1921 if (ret)
1922 goto cleanup_blt_ring;
1923 }
1924
1925 if (HAS_BSD2(dev)) {
1926 ret = logical_bsd2_ring_init(dev);
1927 if (ret)
1928 goto cleanup_vebox_ring;
1929 }
1930
1931 ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
1932 if (ret)
1933 goto cleanup_bsd2_ring;
1934
1935 return 0;
1936
1937cleanup_bsd2_ring:
1938 intel_logical_ring_cleanup(&dev_priv->ring[VCS2]);
1939cleanup_vebox_ring:
1940 intel_logical_ring_cleanup(&dev_priv->ring[VECS]);
1941cleanup_blt_ring:
1942 intel_logical_ring_cleanup(&dev_priv->ring[BCS]);
1943cleanup_bsd_ring:
1944 intel_logical_ring_cleanup(&dev_priv->ring[VCS]);
1945cleanup_render_ring:
1946 intel_logical_ring_cleanup(&dev_priv->ring[RCS]);
1947
1948 return ret;
1949}
1950
Jeff McGee0cea6502015-02-13 10:27:56 -06001951static u32
1952make_rpcs(struct drm_device *dev)
1953{
1954 u32 rpcs = 0;
1955
1956 /*
1957 * No explicit RPCS request is needed to ensure full
1958 * slice/subslice/EU enablement prior to Gen9.
1959 */
1960 if (INTEL_INFO(dev)->gen < 9)
1961 return 0;
1962
1963 /*
1964 * Starting in Gen9, render power gating can leave
1965 * slice/subslice/EU in a partially enabled state. We
1966 * must make an explicit request through RPCS for full
1967 * enablement.
1968 */
1969 if (INTEL_INFO(dev)->has_slice_pg) {
1970 rpcs |= GEN8_RPCS_S_CNT_ENABLE;
1971 rpcs |= INTEL_INFO(dev)->slice_total <<
1972 GEN8_RPCS_S_CNT_SHIFT;
1973 rpcs |= GEN8_RPCS_ENABLE;
1974 }
1975
1976 if (INTEL_INFO(dev)->has_subslice_pg) {
1977 rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
1978 rpcs |= INTEL_INFO(dev)->subslice_per_slice <<
1979 GEN8_RPCS_SS_CNT_SHIFT;
1980 rpcs |= GEN8_RPCS_ENABLE;
1981 }
1982
1983 if (INTEL_INFO(dev)->has_eu_pg) {
1984 rpcs |= INTEL_INFO(dev)->eu_per_subslice <<
1985 GEN8_RPCS_EU_MIN_SHIFT;
1986 rpcs |= INTEL_INFO(dev)->eu_per_subslice <<
1987 GEN8_RPCS_EU_MAX_SHIFT;
1988 rpcs |= GEN8_RPCS_ENABLE;
1989 }
1990
1991 return rpcs;
1992}
1993
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001994static int
1995populate_lr_context(struct intel_context *ctx, struct drm_i915_gem_object *ctx_obj,
1996 struct intel_engine_cs *ring, struct intel_ringbuffer *ringbuf)
1997{
Thomas Daniel2d965532014-08-19 10:13:36 +01001998 struct drm_device *dev = ring->dev;
1999 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterae6c4802014-08-06 15:04:53 +02002000 struct i915_hw_ppgtt *ppgtt = ctx->ppgtt;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002001 struct page *page;
2002 uint32_t *reg_state;
2003 int ret;
2004
Thomas Daniel2d965532014-08-19 10:13:36 +01002005 if (!ppgtt)
2006 ppgtt = dev_priv->mm.aliasing_ppgtt;
2007
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002008 ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
2009 if (ret) {
2010 DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
2011 return ret;
2012 }
2013
2014 ret = i915_gem_object_get_pages(ctx_obj);
2015 if (ret) {
2016 DRM_DEBUG_DRIVER("Could not get object pages\n");
2017 return ret;
2018 }
2019
2020 i915_gem_object_pin_pages(ctx_obj);
2021
2022 /* The second page of the context object contains some fields which must
2023 * be set up prior to the first execution. */
2024 page = i915_gem_object_get_page(ctx_obj, 1);
2025 reg_state = kmap_atomic(page);
2026
2027 /* A context is actually a big batch buffer with several MI_LOAD_REGISTER_IMM
2028 * commands followed by (reg, value) pairs. The values we are setting here are
2029 * only for the first context restore: on a subsequent save, the GPU will
2030 * recreate this batchbuffer with new values (including all the missing
2031 * MI_LOAD_REGISTER_IMM commands that we are not initializing here). */
2032 if (ring->id == RCS)
2033 reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(14);
2034 else
2035 reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(11);
2036 reg_state[CTX_LRI_HEADER_0] |= MI_LRI_FORCE_POSTED;
2037 reg_state[CTX_CONTEXT_CONTROL] = RING_CONTEXT_CONTROL(ring);
2038 reg_state[CTX_CONTEXT_CONTROL+1] =
Zhi Wang5baa22c52015-02-10 17:11:36 +08002039 _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
Abdiel Janulgue69225282015-06-16 13:39:42 +03002040 CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
2041 CTX_CTRL_RS_CTX_ENABLE);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002042 reg_state[CTX_RING_HEAD] = RING_HEAD(ring->mmio_base);
2043 reg_state[CTX_RING_HEAD+1] = 0;
2044 reg_state[CTX_RING_TAIL] = RING_TAIL(ring->mmio_base);
2045 reg_state[CTX_RING_TAIL+1] = 0;
2046 reg_state[CTX_RING_BUFFER_START] = RING_START(ring->mmio_base);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002047 /* Ring buffer start address is not known until the buffer is pinned.
2048 * It is written to the context image in execlists_update_context()
2049 */
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002050 reg_state[CTX_RING_BUFFER_CONTROL] = RING_CTL(ring->mmio_base);
2051 reg_state[CTX_RING_BUFFER_CONTROL+1] =
2052 ((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES) | RING_VALID;
2053 reg_state[CTX_BB_HEAD_U] = ring->mmio_base + 0x168;
2054 reg_state[CTX_BB_HEAD_U+1] = 0;
2055 reg_state[CTX_BB_HEAD_L] = ring->mmio_base + 0x140;
2056 reg_state[CTX_BB_HEAD_L+1] = 0;
2057 reg_state[CTX_BB_STATE] = ring->mmio_base + 0x110;
2058 reg_state[CTX_BB_STATE+1] = (1<<5);
2059 reg_state[CTX_SECOND_BB_HEAD_U] = ring->mmio_base + 0x11c;
2060 reg_state[CTX_SECOND_BB_HEAD_U+1] = 0;
2061 reg_state[CTX_SECOND_BB_HEAD_L] = ring->mmio_base + 0x114;
2062 reg_state[CTX_SECOND_BB_HEAD_L+1] = 0;
2063 reg_state[CTX_SECOND_BB_STATE] = ring->mmio_base + 0x118;
2064 reg_state[CTX_SECOND_BB_STATE+1] = 0;
2065 if (ring->id == RCS) {
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002066 reg_state[CTX_BB_PER_CTX_PTR] = ring->mmio_base + 0x1c0;
2067 reg_state[CTX_BB_PER_CTX_PTR+1] = 0;
2068 reg_state[CTX_RCS_INDIRECT_CTX] = ring->mmio_base + 0x1c4;
2069 reg_state[CTX_RCS_INDIRECT_CTX+1] = 0;
2070 reg_state[CTX_RCS_INDIRECT_CTX_OFFSET] = ring->mmio_base + 0x1c8;
2071 reg_state[CTX_RCS_INDIRECT_CTX_OFFSET+1] = 0;
Arun Siluvery17ee9502015-06-19 19:07:01 +01002072 if (ring->wa_ctx.obj) {
2073 struct i915_ctx_workarounds *wa_ctx = &ring->wa_ctx;
2074 uint32_t ggtt_offset = i915_gem_obj_ggtt_offset(wa_ctx->obj);
2075
2076 reg_state[CTX_RCS_INDIRECT_CTX+1] =
2077 (ggtt_offset + wa_ctx->indirect_ctx.offset * sizeof(uint32_t)) |
2078 (wa_ctx->indirect_ctx.size / CACHELINE_DWORDS);
2079
2080 reg_state[CTX_RCS_INDIRECT_CTX_OFFSET+1] =
2081 CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT << 6;
2082
2083 reg_state[CTX_BB_PER_CTX_PTR+1] =
2084 (ggtt_offset + wa_ctx->per_ctx.offset * sizeof(uint32_t)) |
2085 0x01;
2086 }
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002087 }
2088 reg_state[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9);
2089 reg_state[CTX_LRI_HEADER_1] |= MI_LRI_FORCE_POSTED;
2090 reg_state[CTX_CTX_TIMESTAMP] = ring->mmio_base + 0x3a8;
2091 reg_state[CTX_CTX_TIMESTAMP+1] = 0;
2092 reg_state[CTX_PDP3_UDW] = GEN8_RING_PDP_UDW(ring, 3);
2093 reg_state[CTX_PDP3_LDW] = GEN8_RING_PDP_LDW(ring, 3);
2094 reg_state[CTX_PDP2_UDW] = GEN8_RING_PDP_UDW(ring, 2);
2095 reg_state[CTX_PDP2_LDW] = GEN8_RING_PDP_LDW(ring, 2);
2096 reg_state[CTX_PDP1_UDW] = GEN8_RING_PDP_UDW(ring, 1);
2097 reg_state[CTX_PDP1_LDW] = GEN8_RING_PDP_LDW(ring, 1);
2098 reg_state[CTX_PDP0_UDW] = GEN8_RING_PDP_UDW(ring, 0);
2099 reg_state[CTX_PDP0_LDW] = GEN8_RING_PDP_LDW(ring, 0);
Michel Thierryd7b26332015-04-08 12:13:34 +01002100
2101 /* With dynamic page allocation, PDPs may not be allocated at this point,
2102 * Point the unallocated PDPs to the scratch page
Michel Thierrye5815a22015-04-08 12:13:32 +01002103 */
2104 ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
2105 ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
2106 ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
2107 ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002108 if (ring->id == RCS) {
2109 reg_state[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
Jeff McGee0cea6502015-02-13 10:27:56 -06002110 reg_state[CTX_R_PWR_CLK_STATE] = GEN8_R_PWR_CLK_STATE;
2111 reg_state[CTX_R_PWR_CLK_STATE+1] = make_rpcs(dev);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002112 }
2113
2114 kunmap_atomic(reg_state);
2115
2116 ctx_obj->dirty = 1;
2117 set_page_dirty(page);
2118 i915_gem_object_unpin_pages(ctx_obj);
2119
2120 return 0;
2121}
2122
Oscar Mateo73e4d072014-07-24 17:04:48 +01002123/**
2124 * intel_lr_context_free() - free the LRC specific bits of a context
2125 * @ctx: the LR context to free.
2126 *
2127 * The real context freeing is done in i915_gem_context_free: this only
2128 * takes care of the bits that are LRC related: the per-engine backing
2129 * objects and the logical ringbuffer.
2130 */
Oscar Mateoede7d422014-07-24 17:04:12 +01002131void intel_lr_context_free(struct intel_context *ctx)
2132{
Oscar Mateo8c8579172014-07-24 17:04:14 +01002133 int i;
2134
2135 for (i = 0; i < I915_NUM_RINGS; i++) {
2136 struct drm_i915_gem_object *ctx_obj = ctx->engine[i].state;
Oscar Mateo84c23772014-07-24 17:04:15 +01002137
Oscar Mateo8c8579172014-07-24 17:04:14 +01002138 if (ctx_obj) {
Oscar Mateodcb4c122014-11-13 10:28:10 +00002139 struct intel_ringbuffer *ringbuf =
2140 ctx->engine[i].ringbuf;
2141 struct intel_engine_cs *ring = ringbuf->ring;
2142
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002143 if (ctx == ring->default_context) {
2144 intel_unpin_ringbuffer_obj(ringbuf);
2145 i915_gem_object_ggtt_unpin(ctx_obj);
2146 }
Mika Kuoppalaa7cbede2015-01-13 11:32:25 +02002147 WARN_ON(ctx->engine[ring->id].pin_count);
Oscar Mateo84c23772014-07-24 17:04:15 +01002148 intel_destroy_ringbuffer_obj(ringbuf);
2149 kfree(ringbuf);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002150 drm_gem_object_unreference(&ctx_obj->base);
2151 }
2152 }
2153}
2154
2155static uint32_t get_lr_context_size(struct intel_engine_cs *ring)
2156{
2157 int ret = 0;
2158
Michael H. Nguyen468c6812014-11-13 17:51:49 +00002159 WARN_ON(INTEL_INFO(ring->dev)->gen < 8);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002160
2161 switch (ring->id) {
2162 case RCS:
Michael H. Nguyen468c6812014-11-13 17:51:49 +00002163 if (INTEL_INFO(ring->dev)->gen >= 9)
2164 ret = GEN9_LR_CONTEXT_RENDER_SIZE;
2165 else
2166 ret = GEN8_LR_CONTEXT_RENDER_SIZE;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002167 break;
2168 case VCS:
2169 case BCS:
2170 case VECS:
2171 case VCS2:
2172 ret = GEN8_LR_CONTEXT_OTHER_SIZE;
2173 break;
2174 }
2175
2176 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01002177}
2178
Daniel Vetter70b0ea82014-11-18 09:09:32 +01002179static void lrc_setup_hardware_status_page(struct intel_engine_cs *ring,
Thomas Daniel1df06b72014-10-29 09:52:51 +00002180 struct drm_i915_gem_object *default_ctx_obj)
2181{
2182 struct drm_i915_private *dev_priv = ring->dev->dev_private;
2183
2184 /* The status page is offset 0 from the default context object
2185 * in LRC mode. */
2186 ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(default_ctx_obj);
2187 ring->status_page.page_addr =
2188 kmap(sg_page(default_ctx_obj->pages->sgl));
Thomas Daniel1df06b72014-10-29 09:52:51 +00002189 ring->status_page.obj = default_ctx_obj;
2190
2191 I915_WRITE(RING_HWS_PGA(ring->mmio_base),
2192 (u32)ring->status_page.gfx_addr);
2193 POSTING_READ(RING_HWS_PGA(ring->mmio_base));
Thomas Daniel1df06b72014-10-29 09:52:51 +00002194}
2195
Oscar Mateo73e4d072014-07-24 17:04:48 +01002196/**
2197 * intel_lr_context_deferred_create() - create the LRC specific bits of a context
2198 * @ctx: LR context to create.
2199 * @ring: engine to be used with the context.
2200 *
2201 * This function can be called more than once, with different engines, if we plan
2202 * to use the context with them. The context backing objects and the ringbuffers
2203 * (specially the ringbuffer backing objects) suck a lot of memory up, and that's why
2204 * the creation is a deferred call: it's better to make sure first that we need to use
2205 * a given ring with the context.
2206 *
Masanari Iida32197aa2014-10-20 23:53:13 +09002207 * Return: non-zero on error.
Oscar Mateo73e4d072014-07-24 17:04:48 +01002208 */
Oscar Mateoede7d422014-07-24 17:04:12 +01002209int intel_lr_context_deferred_create(struct intel_context *ctx,
2210 struct intel_engine_cs *ring)
2211{
Oscar Mateodcb4c122014-11-13 10:28:10 +00002212 const bool is_global_default_ctx = (ctx == ring->default_context);
Oscar Mateo8c8579172014-07-24 17:04:14 +01002213 struct drm_device *dev = ring->dev;
2214 struct drm_i915_gem_object *ctx_obj;
2215 uint32_t context_size;
Oscar Mateo84c23772014-07-24 17:04:15 +01002216 struct intel_ringbuffer *ringbuf;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002217 int ret;
2218
Oscar Mateoede7d422014-07-24 17:04:12 +01002219 WARN_ON(ctx->legacy_hw_ctx.rcs_state != NULL);
Daniel Vetterbfc882b2014-11-20 00:33:08 +01002220 WARN_ON(ctx->engine[ring->id].state);
Oscar Mateoede7d422014-07-24 17:04:12 +01002221
Oscar Mateo8c8579172014-07-24 17:04:14 +01002222 context_size = round_up(get_lr_context_size(ring), 4096);
2223
Chris Wilson149c86e2015-04-07 16:21:11 +01002224 ctx_obj = i915_gem_alloc_object(dev, context_size);
Dan Carpenter3126a662015-04-30 17:30:50 +03002225 if (!ctx_obj) {
2226 DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
2227 return -ENOMEM;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002228 }
2229
Oscar Mateodcb4c122014-11-13 10:28:10 +00002230 if (is_global_default_ctx) {
2231 ret = i915_gem_obj_ggtt_pin(ctx_obj, GEN8_LR_CONTEXT_ALIGN, 0);
2232 if (ret) {
2233 DRM_DEBUG_DRIVER("Pin LRC backing obj failed: %d\n",
2234 ret);
2235 drm_gem_object_unreference(&ctx_obj->base);
2236 return ret;
2237 }
Oscar Mateo8c8579172014-07-24 17:04:14 +01002238 }
2239
Oscar Mateo84c23772014-07-24 17:04:15 +01002240 ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
2241 if (!ringbuf) {
2242 DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s\n",
2243 ring->name);
Oscar Mateo84c23772014-07-24 17:04:15 +01002244 ret = -ENOMEM;
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002245 goto error_unpin_ctx;
Oscar Mateo84c23772014-07-24 17:04:15 +01002246 }
2247
Daniel Vetter0c7dd532014-08-11 16:17:44 +02002248 ringbuf->ring = ring;
Oscar Mateo582d67f2014-07-24 17:04:16 +01002249
Oscar Mateo84c23772014-07-24 17:04:15 +01002250 ringbuf->size = 32 * PAGE_SIZE;
2251 ringbuf->effective_size = ringbuf->size;
2252 ringbuf->head = 0;
2253 ringbuf->tail = 0;
Oscar Mateo84c23772014-07-24 17:04:15 +01002254 ringbuf->last_retired_head = -1;
Dave Gordonebd0fd42014-11-27 11:22:49 +00002255 intel_ring_update_space(ringbuf);
Oscar Mateo84c23772014-07-24 17:04:15 +01002256
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002257 if (ringbuf->obj == NULL) {
2258 ret = intel_alloc_ringbuffer_obj(dev, ringbuf);
2259 if (ret) {
2260 DRM_DEBUG_DRIVER(
2261 "Failed to allocate ringbuffer obj %s: %d\n",
Oscar Mateo84c23772014-07-24 17:04:15 +01002262 ring->name, ret);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002263 goto error_free_rbuf;
2264 }
2265
2266 if (is_global_default_ctx) {
2267 ret = intel_pin_and_map_ringbuffer_obj(dev, ringbuf);
2268 if (ret) {
2269 DRM_ERROR(
2270 "Failed to pin and map ringbuffer %s: %d\n",
2271 ring->name, ret);
2272 goto error_destroy_rbuf;
2273 }
2274 }
2275
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002276 }
2277
2278 ret = populate_lr_context(ctx, ctx_obj, ring, ringbuf);
2279 if (ret) {
2280 DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002281 goto error;
Oscar Mateo84c23772014-07-24 17:04:15 +01002282 }
2283
2284 ctx->engine[ring->id].ringbuf = ringbuf;
Oscar Mateo8c8579172014-07-24 17:04:14 +01002285 ctx->engine[ring->id].state = ctx_obj;
Oscar Mateoede7d422014-07-24 17:04:12 +01002286
Daniel Vetter70b0ea82014-11-18 09:09:32 +01002287 if (ctx == ring->default_context)
2288 lrc_setup_hardware_status_page(ring, ctx_obj);
Thomas Daniele7778be2014-12-02 12:50:48 +00002289 else if (ring->id == RCS && !ctx->rcs_initialized) {
Michel Thierry771b9a52014-11-11 16:47:33 +00002290 if (ring->init_context) {
John Harrison76c39162015-05-29 17:43:43 +01002291 struct drm_i915_gem_request *req;
2292
2293 ret = i915_gem_request_alloc(ring, ctx, &req);
2294 if (ret)
2295 return ret;
2296
John Harrison87531812015-05-29 17:43:44 +01002297 ret = ring->init_context(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00002298 if (ret) {
Michel Thierry771b9a52014-11-11 16:47:33 +00002299 DRM_ERROR("ring init context: %d\n", ret);
John Harrison76c39162015-05-29 17:43:43 +01002300 i915_gem_request_cancel(req);
Thomas Daniele7778be2014-12-02 12:50:48 +00002301 ctx->engine[ring->id].ringbuf = NULL;
2302 ctx->engine[ring->id].state = NULL;
2303 goto error;
2304 }
John Harrison76c39162015-05-29 17:43:43 +01002305
John Harrison75289872015-05-29 17:43:49 +01002306 i915_add_request_no_flush(req);
Michel Thierry771b9a52014-11-11 16:47:33 +00002307 }
2308
Oscar Mateo564ddb22014-08-21 11:40:54 +01002309 ctx->rcs_initialized = true;
2310 }
2311
Oscar Mateoede7d422014-07-24 17:04:12 +01002312 return 0;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002313
2314error:
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002315 if (is_global_default_ctx)
2316 intel_unpin_ringbuffer_obj(ringbuf);
2317error_destroy_rbuf:
2318 intel_destroy_ringbuffer_obj(ringbuf);
2319error_free_rbuf:
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002320 kfree(ringbuf);
Thomas Daniel7ba717c2014-11-13 10:28:56 +00002321error_unpin_ctx:
Oscar Mateodcb4c122014-11-13 10:28:10 +00002322 if (is_global_default_ctx)
2323 i915_gem_object_ggtt_unpin(ctx_obj);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01002324 drm_gem_object_unreference(&ctx_obj->base);
2325 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01002326}
Thomas Daniel3e5b6f02015-02-16 16:12:53 +00002327
2328void intel_lr_context_reset(struct drm_device *dev,
2329 struct intel_context *ctx)
2330{
2331 struct drm_i915_private *dev_priv = dev->dev_private;
2332 struct intel_engine_cs *ring;
2333 int i;
2334
2335 for_each_ring(ring, dev_priv, i) {
2336 struct drm_i915_gem_object *ctx_obj =
2337 ctx->engine[ring->id].state;
2338 struct intel_ringbuffer *ringbuf =
2339 ctx->engine[ring->id].ringbuf;
2340 uint32_t *reg_state;
2341 struct page *page;
2342
2343 if (!ctx_obj)
2344 continue;
2345
2346 if (i915_gem_object_get_pages(ctx_obj)) {
2347 WARN(1, "Failed get_pages for context obj\n");
2348 continue;
2349 }
2350 page = i915_gem_object_get_page(ctx_obj, 1);
2351 reg_state = kmap_atomic(page);
2352
2353 reg_state[CTX_RING_HEAD+1] = 0;
2354 reg_state[CTX_RING_TAIL+1] = 0;
2355
2356 kunmap_atomic(reg_state);
2357
2358 ringbuf->head = 0;
2359 ringbuf->tail = 0;
2360 }
2361}