blob: 3cf15c4da0e871960975f76bd078f4143d27d2b9 [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
143#define GEN8_LR_CONTEXT_ALIGN 4096
144
Thomas Daniele981e7b2014-07-24 17:04:39 +0100145#define RING_EXECLIST_QFULL (1 << 0x2)
146#define RING_EXECLIST1_VALID (1 << 0x3)
147#define RING_EXECLIST0_VALID (1 << 0x4)
148#define RING_EXECLIST_ACTIVE_STATUS (3 << 0xE)
149#define RING_EXECLIST1_ACTIVE (1 << 0x11)
150#define RING_EXECLIST0_ACTIVE (1 << 0x12)
151
152#define GEN8_CTX_STATUS_IDLE_ACTIVE (1 << 0)
153#define GEN8_CTX_STATUS_PREEMPTED (1 << 1)
154#define GEN8_CTX_STATUS_ELEMENT_SWITCH (1 << 2)
155#define GEN8_CTX_STATUS_ACTIVE_IDLE (1 << 3)
156#define GEN8_CTX_STATUS_COMPLETE (1 << 4)
157#define GEN8_CTX_STATUS_LITE_RESTORE (1 << 15)
Oscar Mateo8670d6f2014-07-24 17:04:17 +0100158
159#define CTX_LRI_HEADER_0 0x01
160#define CTX_CONTEXT_CONTROL 0x02
161#define CTX_RING_HEAD 0x04
162#define CTX_RING_TAIL 0x06
163#define CTX_RING_BUFFER_START 0x08
164#define CTX_RING_BUFFER_CONTROL 0x0a
165#define CTX_BB_HEAD_U 0x0c
166#define CTX_BB_HEAD_L 0x0e
167#define CTX_BB_STATE 0x10
168#define CTX_SECOND_BB_HEAD_U 0x12
169#define CTX_SECOND_BB_HEAD_L 0x14
170#define CTX_SECOND_BB_STATE 0x16
171#define CTX_BB_PER_CTX_PTR 0x18
172#define CTX_RCS_INDIRECT_CTX 0x1a
173#define CTX_RCS_INDIRECT_CTX_OFFSET 0x1c
174#define CTX_LRI_HEADER_1 0x21
175#define CTX_CTX_TIMESTAMP 0x22
176#define CTX_PDP3_UDW 0x24
177#define CTX_PDP3_LDW 0x26
178#define CTX_PDP2_UDW 0x28
179#define CTX_PDP2_LDW 0x2a
180#define CTX_PDP1_UDW 0x2c
181#define CTX_PDP1_LDW 0x2e
182#define CTX_PDP0_UDW 0x30
183#define CTX_PDP0_LDW 0x32
184#define CTX_LRI_HEADER_2 0x41
185#define CTX_R_PWR_CLK_STATE 0x42
186#define CTX_GPGPU_CSR_BASE_ADDRESS 0x44
187
Ben Widawsky84b790f2014-07-24 17:04:36 +0100188#define GEN8_CTX_VALID (1<<0)
189#define GEN8_CTX_FORCE_PD_RESTORE (1<<1)
190#define GEN8_CTX_FORCE_RESTORE (1<<2)
191#define GEN8_CTX_L3LLC_COHERENT (1<<5)
192#define GEN8_CTX_PRIVILEGE (1<<8)
193enum {
194 ADVANCED_CONTEXT = 0,
195 LEGACY_CONTEXT,
196 ADVANCED_AD_CONTEXT,
197 LEGACY_64B_CONTEXT
198};
199#define GEN8_CTX_MODE_SHIFT 3
200enum {
201 FAULT_AND_HANG = 0,
202 FAULT_AND_HALT, /* Debug only */
203 FAULT_AND_STREAM,
204 FAULT_AND_CONTINUE /* Unsupported */
205};
206#define GEN8_CTX_ID_SHIFT 32
207
Oscar Mateo73e4d072014-07-24 17:04:48 +0100208/**
209 * intel_sanitize_enable_execlists() - sanitize i915.enable_execlists
210 * @dev: DRM device.
211 * @enable_execlists: value of i915.enable_execlists module parameter.
212 *
213 * Only certain platforms support Execlists (the prerequisites being
214 * support for Logical Ring Contexts and Aliasing PPGTT or better),
215 * and only when enabled via module parameter.
216 *
217 * Return: 1 if Execlists is supported and has to be enabled.
218 */
Oscar Mateo127f1002014-07-24 17:04:11 +0100219int intel_sanitize_enable_execlists(struct drm_device *dev, int enable_execlists)
220{
Daniel Vetterbd84b1e2014-08-11 15:57:57 +0200221 WARN_ON(i915.enable_ppgtt == -1);
222
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000223 if (INTEL_INFO(dev)->gen >= 9)
224 return 1;
225
Oscar Mateo127f1002014-07-24 17:04:11 +0100226 if (enable_execlists == 0)
227 return 0;
228
Oscar Mateo14bf9932014-07-24 17:04:34 +0100229 if (HAS_LOGICAL_RING_CONTEXTS(dev) && USES_PPGTT(dev) &&
230 i915.use_mmio_flip >= 0)
Oscar Mateo127f1002014-07-24 17:04:11 +0100231 return 1;
232
233 return 0;
234}
Oscar Mateoede7d422014-07-24 17:04:12 +0100235
Oscar Mateo73e4d072014-07-24 17:04:48 +0100236/**
237 * intel_execlists_ctx_id() - get the Execlists Context ID
238 * @ctx_obj: Logical Ring Context backing object.
239 *
240 * Do not confuse with ctx->id! Unfortunately we have a name overload
241 * here: the old context ID we pass to userspace as a handler so that
242 * they can refer to a context, and the new context ID we pass to the
243 * ELSP so that the GPU can inform us of the context status via
244 * interrupts.
245 *
246 * Return: 20-bits globally unique context ID.
247 */
Ben Widawsky84b790f2014-07-24 17:04:36 +0100248u32 intel_execlists_ctx_id(struct drm_i915_gem_object *ctx_obj)
249{
250 u32 lrca = i915_gem_obj_ggtt_offset(ctx_obj);
251
252 /* LRCA is required to be 4K aligned so the more significant 20 bits
253 * are globally unique */
254 return lrca >> 12;
255}
256
257static uint64_t execlists_ctx_descriptor(struct drm_i915_gem_object *ctx_obj)
258{
259 uint64_t desc;
260 uint64_t lrca = i915_gem_obj_ggtt_offset(ctx_obj);
Michel Thierryacdd8842014-07-24 17:04:38 +0100261
262 WARN_ON(lrca & 0xFFFFFFFF00000FFFULL);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100263
264 desc = GEN8_CTX_VALID;
265 desc |= LEGACY_CONTEXT << GEN8_CTX_MODE_SHIFT;
266 desc |= GEN8_CTX_L3LLC_COHERENT;
267 desc |= GEN8_CTX_PRIVILEGE;
268 desc |= lrca;
269 desc |= (u64)intel_execlists_ctx_id(ctx_obj) << GEN8_CTX_ID_SHIFT;
270
271 /* TODO: WaDisableLiteRestore when we start using semaphore
272 * signalling between Command Streamers */
273 /* desc |= GEN8_CTX_FORCE_RESTORE; */
274
275 return desc;
276}
277
278static void execlists_elsp_write(struct intel_engine_cs *ring,
279 struct drm_i915_gem_object *ctx_obj0,
280 struct drm_i915_gem_object *ctx_obj1)
281{
Tvrtko Ursulin6e7cc472014-11-13 17:51:51 +0000282 struct drm_device *dev = ring->dev;
283 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100284 uint64_t temp = 0;
285 uint32_t desc[4];
Thomas Daniele981e7b2014-07-24 17:04:39 +0100286 unsigned long flags;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100287
288 /* XXX: You must always write both descriptors in the order below. */
289 if (ctx_obj1)
290 temp = execlists_ctx_descriptor(ctx_obj1);
291 else
292 temp = 0;
293 desc[1] = (u32)(temp >> 32);
294 desc[0] = (u32)temp;
295
296 temp = execlists_ctx_descriptor(ctx_obj0);
297 desc[3] = (u32)(temp >> 32);
298 desc[2] = (u32)temp;
299
Thomas Daniele981e7b2014-07-24 17:04:39 +0100300 /* Set Force Wakeup bit to prevent GT from entering C6 while ELSP writes
301 * are in progress.
302 *
303 * The other problem is that we can't just call gen6_gt_force_wake_get()
304 * because that function calls intel_runtime_pm_get(), which might sleep.
305 * Instead, we do the runtime_pm_get/put when creating/destroying requests.
306 */
307 spin_lock_irqsave(&dev_priv->uncore.lock, flags);
Tvrtko Ursulin6e7cc472014-11-13 17:51:51 +0000308 if (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen >= 9) {
Deepak Sa01b0e92014-09-09 19:14:16 +0530309 if (dev_priv->uncore.fw_rendercount++ == 0)
310 dev_priv->uncore.funcs.force_wake_get(dev_priv,
311 FORCEWAKE_RENDER);
312 if (dev_priv->uncore.fw_mediacount++ == 0)
313 dev_priv->uncore.funcs.force_wake_get(dev_priv,
314 FORCEWAKE_MEDIA);
Tvrtko Ursulin6e7cc472014-11-13 17:51:51 +0000315 if (INTEL_INFO(dev)->gen >= 9) {
316 if (dev_priv->uncore.fw_blittercount++ == 0)
317 dev_priv->uncore.funcs.force_wake_get(dev_priv,
318 FORCEWAKE_BLITTER);
319 }
Deepak Sa01b0e92014-09-09 19:14:16 +0530320 } else {
321 if (dev_priv->uncore.forcewake_count++ == 0)
322 dev_priv->uncore.funcs.force_wake_get(dev_priv,
323 FORCEWAKE_ALL);
324 }
Thomas Daniele981e7b2014-07-24 17:04:39 +0100325 spin_unlock_irqrestore(&dev_priv->uncore.lock, flags);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100326
327 I915_WRITE(RING_ELSP(ring), desc[1]);
328 I915_WRITE(RING_ELSP(ring), desc[0]);
329 I915_WRITE(RING_ELSP(ring), desc[3]);
330 /* The context is automatically loaded after the following */
331 I915_WRITE(RING_ELSP(ring), desc[2]);
332
333 /* ELSP is a wo register, so use another nearby reg for posting instead */
334 POSTING_READ(RING_EXECLIST_STATUS(ring));
335
Thomas Daniele981e7b2014-07-24 17:04:39 +0100336 /* Release Force Wakeup (see the big comment above). */
337 spin_lock_irqsave(&dev_priv->uncore.lock, flags);
Tvrtko Ursulin6e7cc472014-11-13 17:51:51 +0000338 if (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen >= 9) {
Deepak Sa01b0e92014-09-09 19:14:16 +0530339 if (--dev_priv->uncore.fw_rendercount == 0)
340 dev_priv->uncore.funcs.force_wake_put(dev_priv,
341 FORCEWAKE_RENDER);
342 if (--dev_priv->uncore.fw_mediacount == 0)
343 dev_priv->uncore.funcs.force_wake_put(dev_priv,
344 FORCEWAKE_MEDIA);
Tvrtko Ursulin6e7cc472014-11-13 17:51:51 +0000345 if (INTEL_INFO(dev)->gen >= 9) {
346 if (--dev_priv->uncore.fw_blittercount == 0)
347 dev_priv->uncore.funcs.force_wake_put(dev_priv,
348 FORCEWAKE_BLITTER);
349 }
Deepak Sa01b0e92014-09-09 19:14:16 +0530350 } else {
351 if (--dev_priv->uncore.forcewake_count == 0)
352 dev_priv->uncore.funcs.force_wake_put(dev_priv,
353 FORCEWAKE_ALL);
354 }
355
Thomas Daniele981e7b2014-07-24 17:04:39 +0100356 spin_unlock_irqrestore(&dev_priv->uncore.lock, flags);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100357}
358
Oscar Mateoae1250b2014-07-24 17:04:37 +0100359static int execlists_ctx_write_tail(struct drm_i915_gem_object *ctx_obj, u32 tail)
360{
361 struct page *page;
362 uint32_t *reg_state;
363
364 page = i915_gem_object_get_page(ctx_obj, 1);
365 reg_state = kmap_atomic(page);
366
367 reg_state[CTX_RING_TAIL+1] = tail;
368
369 kunmap_atomic(reg_state);
370
371 return 0;
372}
373
Dave Gordoncd0707c2014-10-30 15:41:56 +0000374static void execlists_submit_contexts(struct intel_engine_cs *ring,
375 struct intel_context *to0, u32 tail0,
376 struct intel_context *to1, u32 tail1)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100377{
378 struct drm_i915_gem_object *ctx_obj0;
379 struct drm_i915_gem_object *ctx_obj1 = NULL;
380
381 ctx_obj0 = to0->engine[ring->id].state;
382 BUG_ON(!ctx_obj0);
Michel Thierryacdd8842014-07-24 17:04:38 +0100383 WARN_ON(!i915_gem_obj_is_pinned(ctx_obj0));
Ben Widawsky84b790f2014-07-24 17:04:36 +0100384
Oscar Mateoae1250b2014-07-24 17:04:37 +0100385 execlists_ctx_write_tail(ctx_obj0, tail0);
386
Ben Widawsky84b790f2014-07-24 17:04:36 +0100387 if (to1) {
388 ctx_obj1 = to1->engine[ring->id].state;
389 BUG_ON(!ctx_obj1);
Michel Thierryacdd8842014-07-24 17:04:38 +0100390 WARN_ON(!i915_gem_obj_is_pinned(ctx_obj1));
Oscar Mateoae1250b2014-07-24 17:04:37 +0100391
392 execlists_ctx_write_tail(ctx_obj1, tail1);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100393 }
394
395 execlists_elsp_write(ring, ctx_obj0, ctx_obj1);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100396}
397
Michel Thierryacdd8842014-07-24 17:04:38 +0100398static void execlists_context_unqueue(struct intel_engine_cs *ring)
399{
400 struct intel_ctx_submit_request *req0 = NULL, *req1 = NULL;
401 struct intel_ctx_submit_request *cursor = NULL, *tmp = NULL;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100402 struct drm_i915_private *dev_priv = ring->dev->dev_private;
403
404 assert_spin_locked(&ring->execlist_lock);
Michel Thierryacdd8842014-07-24 17:04:38 +0100405
406 if (list_empty(&ring->execlist_queue))
407 return;
408
409 /* Try to read in pairs */
410 list_for_each_entry_safe(cursor, tmp, &ring->execlist_queue,
411 execlist_link) {
412 if (!req0) {
413 req0 = cursor;
414 } else if (req0->ctx == cursor->ctx) {
415 /* Same ctx: ignore first request, as second request
416 * will update tail past first request's workload */
Oscar Mateoe1fee722014-07-24 17:04:40 +0100417 cursor->elsp_submitted = req0->elsp_submitted;
Michel Thierryacdd8842014-07-24 17:04:38 +0100418 list_del(&req0->execlist_link);
Thomas Daniele981e7b2014-07-24 17:04:39 +0100419 queue_work(dev_priv->wq, &req0->work);
Michel Thierryacdd8842014-07-24 17:04:38 +0100420 req0 = cursor;
421 } else {
422 req1 = cursor;
423 break;
424 }
425 }
426
Oscar Mateoe1fee722014-07-24 17:04:40 +0100427 WARN_ON(req1 && req1->elsp_submitted);
428
Dave Gordoncd0707c2014-10-30 15:41:56 +0000429 execlists_submit_contexts(ring, req0->ctx, req0->tail,
430 req1 ? req1->ctx : NULL,
431 req1 ? req1->tail : 0);
Oscar Mateoe1fee722014-07-24 17:04:40 +0100432
433 req0->elsp_submitted++;
434 if (req1)
435 req1->elsp_submitted++;
Michel Thierryacdd8842014-07-24 17:04:38 +0100436}
437
Thomas Daniele981e7b2014-07-24 17:04:39 +0100438static bool execlists_check_remove_request(struct intel_engine_cs *ring,
439 u32 request_id)
440{
441 struct drm_i915_private *dev_priv = ring->dev->dev_private;
442 struct intel_ctx_submit_request *head_req;
443
444 assert_spin_locked(&ring->execlist_lock);
445
446 head_req = list_first_entry_or_null(&ring->execlist_queue,
447 struct intel_ctx_submit_request,
448 execlist_link);
449
450 if (head_req != NULL) {
451 struct drm_i915_gem_object *ctx_obj =
452 head_req->ctx->engine[ring->id].state;
453 if (intel_execlists_ctx_id(ctx_obj) == request_id) {
Oscar Mateoe1fee722014-07-24 17:04:40 +0100454 WARN(head_req->elsp_submitted == 0,
455 "Never submitted head request\n");
456
457 if (--head_req->elsp_submitted <= 0) {
458 list_del(&head_req->execlist_link);
459 queue_work(dev_priv->wq, &head_req->work);
460 return true;
461 }
Thomas Daniele981e7b2014-07-24 17:04:39 +0100462 }
463 }
464
465 return false;
466}
467
Oscar Mateo73e4d072014-07-24 17:04:48 +0100468/**
469 * intel_execlists_handle_ctx_events() - handle Context Switch interrupts
470 * @ring: Engine Command Streamer to handle.
471 *
472 * Check the unread Context Status Buffers and manage the submission of new
473 * contexts to the ELSP accordingly.
474 */
Thomas Daniele981e7b2014-07-24 17:04:39 +0100475void intel_execlists_handle_ctx_events(struct intel_engine_cs *ring)
476{
477 struct drm_i915_private *dev_priv = ring->dev->dev_private;
478 u32 status_pointer;
479 u8 read_pointer;
480 u8 write_pointer;
481 u32 status;
482 u32 status_id;
483 u32 submit_contexts = 0;
484
485 status_pointer = I915_READ(RING_CONTEXT_STATUS_PTR(ring));
486
487 read_pointer = ring->next_context_status_buffer;
488 write_pointer = status_pointer & 0x07;
489 if (read_pointer > write_pointer)
490 write_pointer += 6;
491
492 spin_lock(&ring->execlist_lock);
493
494 while (read_pointer < write_pointer) {
495 read_pointer++;
496 status = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
497 (read_pointer % 6) * 8);
498 status_id = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
499 (read_pointer % 6) * 8 + 4);
500
Oscar Mateoe1fee722014-07-24 17:04:40 +0100501 if (status & GEN8_CTX_STATUS_PREEMPTED) {
502 if (status & GEN8_CTX_STATUS_LITE_RESTORE) {
503 if (execlists_check_remove_request(ring, status_id))
504 WARN(1, "Lite Restored request removed from queue\n");
505 } else
506 WARN(1, "Preemption without Lite Restore\n");
507 }
508
509 if ((status & GEN8_CTX_STATUS_ACTIVE_IDLE) ||
510 (status & GEN8_CTX_STATUS_ELEMENT_SWITCH)) {
Thomas Daniele981e7b2014-07-24 17:04:39 +0100511 if (execlists_check_remove_request(ring, status_id))
512 submit_contexts++;
513 }
514 }
515
516 if (submit_contexts != 0)
517 execlists_context_unqueue(ring);
518
519 spin_unlock(&ring->execlist_lock);
520
521 WARN(submit_contexts > 2, "More than two context complete events?\n");
522 ring->next_context_status_buffer = write_pointer % 6;
523
524 I915_WRITE(RING_CONTEXT_STATUS_PTR(ring),
525 ((u32)ring->next_context_status_buffer & 0x07) << 8);
526}
527
528static void execlists_free_request_task(struct work_struct *work)
529{
530 struct intel_ctx_submit_request *req =
531 container_of(work, struct intel_ctx_submit_request, work);
532 struct drm_device *dev = req->ring->dev;
533 struct drm_i915_private *dev_priv = dev->dev_private;
534
535 intel_runtime_pm_put(dev_priv);
536
537 mutex_lock(&dev->struct_mutex);
538 i915_gem_context_unreference(req->ctx);
539 mutex_unlock(&dev->struct_mutex);
540
541 kfree(req);
542}
543
Michel Thierryacdd8842014-07-24 17:04:38 +0100544static int execlists_context_queue(struct intel_engine_cs *ring,
545 struct intel_context *to,
546 u32 tail)
547{
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100548 struct intel_ctx_submit_request *req = NULL, *cursor;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100549 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Michel Thierryacdd8842014-07-24 17:04:38 +0100550 unsigned long flags;
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100551 int num_elements = 0;
Michel Thierryacdd8842014-07-24 17:04:38 +0100552
553 req = kzalloc(sizeof(*req), GFP_KERNEL);
554 if (req == NULL)
555 return -ENOMEM;
556 req->ctx = to;
557 i915_gem_context_reference(req->ctx);
558 req->ring = ring;
559 req->tail = tail;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100560 INIT_WORK(&req->work, execlists_free_request_task);
561
562 intel_runtime_pm_get(dev_priv);
Michel Thierryacdd8842014-07-24 17:04:38 +0100563
564 spin_lock_irqsave(&ring->execlist_lock, flags);
565
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100566 list_for_each_entry(cursor, &ring->execlist_queue, execlist_link)
567 if (++num_elements > 2)
568 break;
569
570 if (num_elements > 2) {
571 struct intel_ctx_submit_request *tail_req;
572
573 tail_req = list_last_entry(&ring->execlist_queue,
574 struct intel_ctx_submit_request,
575 execlist_link);
576
577 if (to == tail_req->ctx) {
578 WARN(tail_req->elsp_submitted != 0,
579 "More than 2 already-submitted reqs queued\n");
580 list_del(&tail_req->execlist_link);
581 queue_work(dev_priv->wq, &tail_req->work);
582 }
583 }
584
Michel Thierryacdd8842014-07-24 17:04:38 +0100585 list_add_tail(&req->execlist_link, &ring->execlist_queue);
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100586 if (num_elements == 0)
Michel Thierryacdd8842014-07-24 17:04:38 +0100587 execlists_context_unqueue(ring);
588
589 spin_unlock_irqrestore(&ring->execlist_lock, flags);
590
591 return 0;
592}
593
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100594static int logical_ring_invalidate_all_caches(struct intel_ringbuffer *ringbuf)
595{
596 struct intel_engine_cs *ring = ringbuf->ring;
597 uint32_t flush_domains;
598 int ret;
599
600 flush_domains = 0;
601 if (ring->gpu_caches_dirty)
602 flush_domains = I915_GEM_GPU_DOMAINS;
603
604 ret = ring->emit_flush(ringbuf, I915_GEM_GPU_DOMAINS, flush_domains);
605 if (ret)
606 return ret;
607
608 ring->gpu_caches_dirty = false;
609 return 0;
610}
611
612static int execlists_move_to_gpu(struct intel_ringbuffer *ringbuf,
613 struct list_head *vmas)
614{
615 struct intel_engine_cs *ring = ringbuf->ring;
616 struct i915_vma *vma;
617 uint32_t flush_domains = 0;
618 bool flush_chipset = false;
619 int ret;
620
621 list_for_each_entry(vma, vmas, exec_list) {
622 struct drm_i915_gem_object *obj = vma->obj;
623
624 ret = i915_gem_object_sync(obj, ring);
625 if (ret)
626 return ret;
627
628 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
629 flush_chipset |= i915_gem_clflush_object(obj, false);
630
631 flush_domains |= obj->base.write_domain;
632 }
633
634 if (flush_domains & I915_GEM_DOMAIN_GTT)
635 wmb();
636
637 /* Unconditionally invalidate gpu caches and ensure that we do flush
638 * any residual writes from the previous batch.
639 */
640 return logical_ring_invalidate_all_caches(ringbuf);
641}
642
Oscar Mateo73e4d072014-07-24 17:04:48 +0100643/**
644 * execlists_submission() - submit a batchbuffer for execution, Execlists style
645 * @dev: DRM device.
646 * @file: DRM file.
647 * @ring: Engine Command Streamer to submit to.
648 * @ctx: Context to employ for this submission.
649 * @args: execbuffer call arguments.
650 * @vmas: list of vmas.
651 * @batch_obj: the batchbuffer to submit.
652 * @exec_start: batchbuffer start virtual address pointer.
653 * @flags: translated execbuffer call flags.
654 *
655 * This is the evil twin version of i915_gem_ringbuffer_submission. It abstracts
656 * away the submission details of the execbuffer ioctl call.
657 *
658 * Return: non-zero if the submission fails.
659 */
Oscar Mateo454afeb2014-07-24 17:04:22 +0100660int intel_execlists_submission(struct drm_device *dev, struct drm_file *file,
661 struct intel_engine_cs *ring,
662 struct intel_context *ctx,
663 struct drm_i915_gem_execbuffer2 *args,
664 struct list_head *vmas,
665 struct drm_i915_gem_object *batch_obj,
666 u64 exec_start, u32 flags)
667{
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100668 struct drm_i915_private *dev_priv = dev->dev_private;
669 struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
670 int instp_mode;
671 u32 instp_mask;
672 int ret;
673
674 instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
675 instp_mask = I915_EXEC_CONSTANTS_MASK;
676 switch (instp_mode) {
677 case I915_EXEC_CONSTANTS_REL_GENERAL:
678 case I915_EXEC_CONSTANTS_ABSOLUTE:
679 case I915_EXEC_CONSTANTS_REL_SURFACE:
680 if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) {
681 DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
682 return -EINVAL;
683 }
684
685 if (instp_mode != dev_priv->relative_constants_mode) {
686 if (instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
687 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
688 return -EINVAL;
689 }
690
691 /* The HW changed the meaning on this bit on gen6 */
692 instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
693 }
694 break;
695 default:
696 DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
697 return -EINVAL;
698 }
699
700 if (args->num_cliprects != 0) {
701 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
702 return -EINVAL;
703 } else {
704 if (args->DR4 == 0xffffffff) {
705 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
706 args->DR4 = 0;
707 }
708
709 if (args->DR1 || args->DR4 || args->cliprects_ptr) {
710 DRM_DEBUG("0 cliprects but dirt in cliprects fields\n");
711 return -EINVAL;
712 }
713 }
714
715 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
716 DRM_DEBUG("sol reset is gen7 only\n");
717 return -EINVAL;
718 }
719
720 ret = execlists_move_to_gpu(ringbuf, vmas);
721 if (ret)
722 return ret;
723
724 if (ring == &dev_priv->ring[RCS] &&
725 instp_mode != dev_priv->relative_constants_mode) {
726 ret = intel_logical_ring_begin(ringbuf, 4);
727 if (ret)
728 return ret;
729
730 intel_logical_ring_emit(ringbuf, MI_NOOP);
731 intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(1));
732 intel_logical_ring_emit(ringbuf, INSTPM);
733 intel_logical_ring_emit(ringbuf, instp_mask << 16 | instp_mode);
734 intel_logical_ring_advance(ringbuf);
735
736 dev_priv->relative_constants_mode = instp_mode;
737 }
738
739 ret = ring->emit_bb_start(ringbuf, exec_start, flags);
740 if (ret)
741 return ret;
742
743 i915_gem_execbuffer_move_to_active(vmas, ring);
744 i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
745
Oscar Mateo454afeb2014-07-24 17:04:22 +0100746 return 0;
747}
748
749void intel_logical_ring_stop(struct intel_engine_cs *ring)
750{
Oscar Mateo9832b9d2014-07-24 17:04:30 +0100751 struct drm_i915_private *dev_priv = ring->dev->dev_private;
752 int ret;
753
754 if (!intel_ring_initialized(ring))
755 return;
756
757 ret = intel_ring_idle(ring);
758 if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
759 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
760 ring->name, ret);
761
762 /* TODO: Is this correct with Execlists enabled? */
763 I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
764 if (wait_for_atomic((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
765 DRM_ERROR("%s :timed out trying to stop ring\n", ring->name);
766 return;
767 }
768 I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
Oscar Mateo454afeb2014-07-24 17:04:22 +0100769}
770
Oscar Mateo48e29f52014-07-24 17:04:29 +0100771int logical_ring_flush_all_caches(struct intel_ringbuffer *ringbuf)
772{
773 struct intel_engine_cs *ring = ringbuf->ring;
774 int ret;
775
776 if (!ring->gpu_caches_dirty)
777 return 0;
778
779 ret = ring->emit_flush(ringbuf, 0, I915_GEM_GPU_DOMAINS);
780 if (ret)
781 return ret;
782
783 ring->gpu_caches_dirty = false;
784 return 0;
785}
786
Oscar Mateo73e4d072014-07-24 17:04:48 +0100787/**
788 * intel_logical_ring_advance_and_submit() - advance the tail and submit the workload
789 * @ringbuf: Logical Ringbuffer to advance.
790 *
791 * The tail is updated in our logical ringbuffer struct, not in the actual context. What
792 * really happens during submission is that the context and current tail will be placed
793 * on a queue waiting for the ELSP to be ready to accept a new context submission. At that
794 * point, the tail *inside* the context is updated and the ELSP written to.
795 */
Oscar Mateo82e104c2014-07-24 17:04:26 +0100796void intel_logical_ring_advance_and_submit(struct intel_ringbuffer *ringbuf)
797{
Ben Widawsky84b790f2014-07-24 17:04:36 +0100798 struct intel_engine_cs *ring = ringbuf->ring;
799 struct intel_context *ctx = ringbuf->FIXME_lrc_ctx;
800
Oscar Mateo82e104c2014-07-24 17:04:26 +0100801 intel_logical_ring_advance(ringbuf);
802
Ben Widawsky84b790f2014-07-24 17:04:36 +0100803 if (intel_ring_stopped(ring))
Oscar Mateo82e104c2014-07-24 17:04:26 +0100804 return;
805
Michel Thierryacdd8842014-07-24 17:04:38 +0100806 execlists_context_queue(ring, ctx, ringbuf->tail);
Oscar Mateo82e104c2014-07-24 17:04:26 +0100807}
808
Oscar Mateo48e29f52014-07-24 17:04:29 +0100809static int logical_ring_alloc_seqno(struct intel_engine_cs *ring,
810 struct intel_context *ctx)
Oscar Mateo82e104c2014-07-24 17:04:26 +0100811{
812 if (ring->outstanding_lazy_seqno)
813 return 0;
814
815 if (ring->preallocated_lazy_request == NULL) {
816 struct drm_i915_gem_request *request;
817
818 request = kmalloc(sizeof(*request), GFP_KERNEL);
819 if (request == NULL)
820 return -ENOMEM;
821
Oscar Mateo48e29f52014-07-24 17:04:29 +0100822 /* Hold a reference to the context this request belongs to
823 * (we will need it when the time comes to emit/retire the
824 * request).
825 */
826 request->ctx = ctx;
827 i915_gem_context_reference(request->ctx);
828
Oscar Mateo82e104c2014-07-24 17:04:26 +0100829 ring->preallocated_lazy_request = request;
830 }
831
832 return i915_gem_get_seqno(ring->dev, &ring->outstanding_lazy_seqno);
833}
834
835static int logical_ring_wait_request(struct intel_ringbuffer *ringbuf,
836 int bytes)
837{
838 struct intel_engine_cs *ring = ringbuf->ring;
839 struct drm_i915_gem_request *request;
840 u32 seqno = 0;
841 int ret;
842
843 if (ringbuf->last_retired_head != -1) {
844 ringbuf->head = ringbuf->last_retired_head;
845 ringbuf->last_retired_head = -1;
846
847 ringbuf->space = intel_ring_space(ringbuf);
848 if (ringbuf->space >= bytes)
849 return 0;
850 }
851
852 list_for_each_entry(request, &ring->request_list, list) {
853 if (__intel_ring_space(request->tail, ringbuf->tail,
854 ringbuf->size) >= bytes) {
855 seqno = request->seqno;
856 break;
857 }
858 }
859
860 if (seqno == 0)
861 return -ENOSPC;
862
863 ret = i915_wait_seqno(ring, seqno);
864 if (ret)
865 return ret;
866
Oscar Mateo82e104c2014-07-24 17:04:26 +0100867 i915_gem_retire_requests_ring(ring);
868 ringbuf->head = ringbuf->last_retired_head;
869 ringbuf->last_retired_head = -1;
870
871 ringbuf->space = intel_ring_space(ringbuf);
872 return 0;
873}
874
875static int logical_ring_wait_for_space(struct intel_ringbuffer *ringbuf,
876 int bytes)
877{
878 struct intel_engine_cs *ring = ringbuf->ring;
879 struct drm_device *dev = ring->dev;
880 struct drm_i915_private *dev_priv = dev->dev_private;
881 unsigned long end;
882 int ret;
883
884 ret = logical_ring_wait_request(ringbuf, bytes);
885 if (ret != -ENOSPC)
886 return ret;
887
888 /* Force the context submission in case we have been skipping it */
889 intel_logical_ring_advance_and_submit(ringbuf);
890
891 /* With GEM the hangcheck timer should kick us out of the loop,
892 * leaving it early runs the risk of corrupting GEM state (due
893 * to running on almost untested codepaths). But on resume
894 * timers don't work yet, so prevent a complete hang in that
895 * case by choosing an insanely large timeout. */
896 end = jiffies + 60 * HZ;
897
898 do {
899 ringbuf->head = I915_READ_HEAD(ring);
900 ringbuf->space = intel_ring_space(ringbuf);
901 if (ringbuf->space >= bytes) {
902 ret = 0;
903 break;
904 }
905
906 msleep(1);
907
908 if (dev_priv->mm.interruptible && signal_pending(current)) {
909 ret = -ERESTARTSYS;
910 break;
911 }
912
913 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
914 dev_priv->mm.interruptible);
915 if (ret)
916 break;
917
918 if (time_after(jiffies, end)) {
919 ret = -EBUSY;
920 break;
921 }
922 } while (1);
923
924 return ret;
925}
926
927static int logical_ring_wrap_buffer(struct intel_ringbuffer *ringbuf)
928{
929 uint32_t __iomem *virt;
930 int rem = ringbuf->size - ringbuf->tail;
931
932 if (ringbuf->space < rem) {
933 int ret = logical_ring_wait_for_space(ringbuf, rem);
934
935 if (ret)
936 return ret;
937 }
938
939 virt = ringbuf->virtual_start + ringbuf->tail;
940 rem /= 4;
941 while (rem--)
942 iowrite32(MI_NOOP, virt++);
943
944 ringbuf->tail = 0;
945 ringbuf->space = intel_ring_space(ringbuf);
946
947 return 0;
948}
949
950static int logical_ring_prepare(struct intel_ringbuffer *ringbuf, int bytes)
951{
952 int ret;
953
954 if (unlikely(ringbuf->tail + bytes > ringbuf->effective_size)) {
955 ret = logical_ring_wrap_buffer(ringbuf);
956 if (unlikely(ret))
957 return ret;
958 }
959
960 if (unlikely(ringbuf->space < bytes)) {
961 ret = logical_ring_wait_for_space(ringbuf, bytes);
962 if (unlikely(ret))
963 return ret;
964 }
965
966 return 0;
967}
968
Oscar Mateo73e4d072014-07-24 17:04:48 +0100969/**
970 * intel_logical_ring_begin() - prepare the logical ringbuffer to accept some commands
971 *
972 * @ringbuf: Logical ringbuffer.
973 * @num_dwords: number of DWORDs that we plan to write to the ringbuffer.
974 *
975 * The ringbuffer might not be ready to accept the commands right away (maybe it needs to
976 * be wrapped, or wait a bit for the tail to be updated). This function takes care of that
977 * and also preallocates a request (every workload submission is still mediated through
978 * requests, same as it did with legacy ringbuffer submission).
979 *
980 * Return: non-zero if the ringbuffer is not ready to be written to.
981 */
Oscar Mateo82e104c2014-07-24 17:04:26 +0100982int intel_logical_ring_begin(struct intel_ringbuffer *ringbuf, int num_dwords)
983{
984 struct intel_engine_cs *ring = ringbuf->ring;
985 struct drm_device *dev = ring->dev;
986 struct drm_i915_private *dev_priv = dev->dev_private;
987 int ret;
988
989 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
990 dev_priv->mm.interruptible);
991 if (ret)
992 return ret;
993
994 ret = logical_ring_prepare(ringbuf, num_dwords * sizeof(uint32_t));
995 if (ret)
996 return ret;
997
998 /* Preallocate the olr before touching the ring */
Oscar Mateo48e29f52014-07-24 17:04:29 +0100999 ret = logical_ring_alloc_seqno(ring, ringbuf->FIXME_lrc_ctx);
Oscar Mateo82e104c2014-07-24 17:04:26 +01001000 if (ret)
1001 return ret;
1002
1003 ringbuf->space -= num_dwords * sizeof(uint32_t);
1004 return 0;
1005}
1006
Michel Thierry771b9a52014-11-11 16:47:33 +00001007static int intel_logical_ring_workarounds_emit(struct intel_engine_cs *ring,
1008 struct intel_context *ctx)
1009{
1010 int ret, i;
1011 struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
1012 struct drm_device *dev = ring->dev;
1013 struct drm_i915_private *dev_priv = dev->dev_private;
1014 struct i915_workarounds *w = &dev_priv->workarounds;
1015
1016 if (WARN_ON(w->count == 0))
1017 return 0;
1018
1019 ring->gpu_caches_dirty = true;
1020 ret = logical_ring_flush_all_caches(ringbuf);
1021 if (ret)
1022 return ret;
1023
1024 ret = intel_logical_ring_begin(ringbuf, w->count * 2 + 2);
1025 if (ret)
1026 return ret;
1027
1028 intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(w->count));
1029 for (i = 0; i < w->count; i++) {
1030 intel_logical_ring_emit(ringbuf, w->reg[i].addr);
1031 intel_logical_ring_emit(ringbuf, w->reg[i].value);
1032 }
1033 intel_logical_ring_emit(ringbuf, MI_NOOP);
1034
1035 intel_logical_ring_advance(ringbuf);
1036
1037 ring->gpu_caches_dirty = true;
1038 ret = logical_ring_flush_all_caches(ringbuf);
1039 if (ret)
1040 return ret;
1041
1042 return 0;
1043}
1044
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001045static int gen8_init_common_ring(struct intel_engine_cs *ring)
1046{
1047 struct drm_device *dev = ring->dev;
1048 struct drm_i915_private *dev_priv = dev->dev_private;
1049
Oscar Mateo73d477f2014-07-24 17:04:31 +01001050 I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
1051 I915_WRITE(RING_HWSTAM(ring->mmio_base), 0xffffffff);
1052
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001053 I915_WRITE(RING_MODE_GEN7(ring),
1054 _MASKED_BIT_DISABLE(GFX_REPLAY_MODE) |
1055 _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
1056 POSTING_READ(RING_MODE_GEN7(ring));
1057 DRM_DEBUG_DRIVER("Execlists enabled for %s\n", ring->name);
1058
1059 memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
1060
1061 return 0;
1062}
1063
1064static int gen8_init_render_ring(struct intel_engine_cs *ring)
1065{
1066 struct drm_device *dev = ring->dev;
1067 struct drm_i915_private *dev_priv = dev->dev_private;
1068 int ret;
1069
1070 ret = gen8_init_common_ring(ring);
1071 if (ret)
1072 return ret;
1073
1074 /* We need to disable the AsyncFlip performance optimisations in order
1075 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1076 * programmed to '1' on all products.
1077 *
1078 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
1079 */
1080 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1081
1082 ret = intel_init_pipe_control(ring);
1083 if (ret)
1084 return ret;
1085
1086 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1087
Michel Thierry771b9a52014-11-11 16:47:33 +00001088 return init_workarounds_ring(ring);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001089}
1090
Oscar Mateo15648582014-07-24 17:04:32 +01001091static int gen8_emit_bb_start(struct intel_ringbuffer *ringbuf,
1092 u64 offset, unsigned flags)
1093{
Oscar Mateo15648582014-07-24 17:04:32 +01001094 bool ppgtt = !(flags & I915_DISPATCH_SECURE);
1095 int ret;
1096
1097 ret = intel_logical_ring_begin(ringbuf, 4);
1098 if (ret)
1099 return ret;
1100
1101 /* FIXME(BDW): Address space and security selectors. */
1102 intel_logical_ring_emit(ringbuf, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8));
1103 intel_logical_ring_emit(ringbuf, lower_32_bits(offset));
1104 intel_logical_ring_emit(ringbuf, upper_32_bits(offset));
1105 intel_logical_ring_emit(ringbuf, MI_NOOP);
1106 intel_logical_ring_advance(ringbuf);
1107
1108 return 0;
1109}
1110
Oscar Mateo73d477f2014-07-24 17:04:31 +01001111static bool gen8_logical_ring_get_irq(struct intel_engine_cs *ring)
1112{
1113 struct drm_device *dev = ring->dev;
1114 struct drm_i915_private *dev_priv = dev->dev_private;
1115 unsigned long flags;
1116
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001117 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Oscar Mateo73d477f2014-07-24 17:04:31 +01001118 return false;
1119
1120 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1121 if (ring->irq_refcount++ == 0) {
1122 I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
1123 POSTING_READ(RING_IMR(ring->mmio_base));
1124 }
1125 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1126
1127 return true;
1128}
1129
1130static void gen8_logical_ring_put_irq(struct intel_engine_cs *ring)
1131{
1132 struct drm_device *dev = ring->dev;
1133 struct drm_i915_private *dev_priv = dev->dev_private;
1134 unsigned long flags;
1135
1136 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1137 if (--ring->irq_refcount == 0) {
1138 I915_WRITE_IMR(ring, ~ring->irq_keep_mask);
1139 POSTING_READ(RING_IMR(ring->mmio_base));
1140 }
1141 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1142}
1143
Oscar Mateo47122742014-07-24 17:04:28 +01001144static int gen8_emit_flush(struct intel_ringbuffer *ringbuf,
1145 u32 invalidate_domains,
1146 u32 unused)
1147{
1148 struct intel_engine_cs *ring = ringbuf->ring;
1149 struct drm_device *dev = ring->dev;
1150 struct drm_i915_private *dev_priv = dev->dev_private;
1151 uint32_t cmd;
1152 int ret;
1153
1154 ret = intel_logical_ring_begin(ringbuf, 4);
1155 if (ret)
1156 return ret;
1157
1158 cmd = MI_FLUSH_DW + 1;
1159
1160 if (ring == &dev_priv->ring[VCS]) {
1161 if (invalidate_domains & I915_GEM_GPU_DOMAINS)
1162 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD |
1163 MI_FLUSH_DW_STORE_INDEX |
1164 MI_FLUSH_DW_OP_STOREDW;
1165 } else {
1166 if (invalidate_domains & I915_GEM_DOMAIN_RENDER)
1167 cmd |= MI_INVALIDATE_TLB | MI_FLUSH_DW_STORE_INDEX |
1168 MI_FLUSH_DW_OP_STOREDW;
1169 }
1170
1171 intel_logical_ring_emit(ringbuf, cmd);
1172 intel_logical_ring_emit(ringbuf,
1173 I915_GEM_HWS_SCRATCH_ADDR |
1174 MI_FLUSH_DW_USE_GTT);
1175 intel_logical_ring_emit(ringbuf, 0); /* upper addr */
1176 intel_logical_ring_emit(ringbuf, 0); /* value */
1177 intel_logical_ring_advance(ringbuf);
1178
1179 return 0;
1180}
1181
1182static int gen8_emit_flush_render(struct intel_ringbuffer *ringbuf,
1183 u32 invalidate_domains,
1184 u32 flush_domains)
1185{
1186 struct intel_engine_cs *ring = ringbuf->ring;
1187 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
1188 u32 flags = 0;
1189 int ret;
1190
1191 flags |= PIPE_CONTROL_CS_STALL;
1192
1193 if (flush_domains) {
1194 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
1195 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
1196 }
1197
1198 if (invalidate_domains) {
1199 flags |= PIPE_CONTROL_TLB_INVALIDATE;
1200 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
1201 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
1202 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
1203 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
1204 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
1205 flags |= PIPE_CONTROL_QW_WRITE;
1206 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
1207 }
1208
1209 ret = intel_logical_ring_begin(ringbuf, 6);
1210 if (ret)
1211 return ret;
1212
1213 intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
1214 intel_logical_ring_emit(ringbuf, flags);
1215 intel_logical_ring_emit(ringbuf, scratch_addr);
1216 intel_logical_ring_emit(ringbuf, 0);
1217 intel_logical_ring_emit(ringbuf, 0);
1218 intel_logical_ring_emit(ringbuf, 0);
1219 intel_logical_ring_advance(ringbuf);
1220
1221 return 0;
1222}
1223
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001224static u32 gen8_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
1225{
1226 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1227}
1228
1229static void gen8_set_seqno(struct intel_engine_cs *ring, u32 seqno)
1230{
1231 intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
1232}
1233
Oscar Mateo4da46e12014-07-24 17:04:27 +01001234static int gen8_emit_request(struct intel_ringbuffer *ringbuf)
1235{
1236 struct intel_engine_cs *ring = ringbuf->ring;
1237 u32 cmd;
1238 int ret;
1239
1240 ret = intel_logical_ring_begin(ringbuf, 6);
1241 if (ret)
1242 return ret;
1243
1244 cmd = MI_STORE_DWORD_IMM_GEN8;
1245 cmd |= MI_GLOBAL_GTT;
1246
1247 intel_logical_ring_emit(ringbuf, cmd);
1248 intel_logical_ring_emit(ringbuf,
1249 (ring->status_page.gfx_addr +
1250 (I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT)));
1251 intel_logical_ring_emit(ringbuf, 0);
1252 intel_logical_ring_emit(ringbuf, ring->outstanding_lazy_seqno);
1253 intel_logical_ring_emit(ringbuf, MI_USER_INTERRUPT);
1254 intel_logical_ring_emit(ringbuf, MI_NOOP);
1255 intel_logical_ring_advance_and_submit(ringbuf);
1256
1257 return 0;
1258}
1259
Oscar Mateo73e4d072014-07-24 17:04:48 +01001260/**
1261 * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
1262 *
1263 * @ring: Engine Command Streamer.
1264 *
1265 */
Oscar Mateo454afeb2014-07-24 17:04:22 +01001266void intel_logical_ring_cleanup(struct intel_engine_cs *ring)
1267{
John Harrison6402c332014-10-31 12:00:26 +00001268 struct drm_i915_private *dev_priv;
Oscar Mateo9832b9d2014-07-24 17:04:30 +01001269
Oscar Mateo48d82382014-07-24 17:04:23 +01001270 if (!intel_ring_initialized(ring))
1271 return;
1272
John Harrison6402c332014-10-31 12:00:26 +00001273 dev_priv = ring->dev->dev_private;
1274
Oscar Mateo9832b9d2014-07-24 17:04:30 +01001275 intel_logical_ring_stop(ring);
1276 WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
Oscar Mateo48d82382014-07-24 17:04:23 +01001277 ring->preallocated_lazy_request = NULL;
1278 ring->outstanding_lazy_seqno = 0;
1279
1280 if (ring->cleanup)
1281 ring->cleanup(ring);
1282
1283 i915_cmd_parser_fini_ring(ring);
1284
1285 if (ring->status_page.obj) {
1286 kunmap(sg_page(ring->status_page.obj->pages->sgl));
1287 ring->status_page.obj = NULL;
1288 }
Oscar Mateo454afeb2014-07-24 17:04:22 +01001289}
1290
1291static int logical_ring_init(struct drm_device *dev, struct intel_engine_cs *ring)
1292{
Oscar Mateo48d82382014-07-24 17:04:23 +01001293 int ret;
Oscar Mateo48d82382014-07-24 17:04:23 +01001294
1295 /* Intentionally left blank. */
1296 ring->buffer = NULL;
1297
1298 ring->dev = dev;
1299 INIT_LIST_HEAD(&ring->active_list);
1300 INIT_LIST_HEAD(&ring->request_list);
1301 init_waitqueue_head(&ring->irq_queue);
1302
Michel Thierryacdd8842014-07-24 17:04:38 +01001303 INIT_LIST_HEAD(&ring->execlist_queue);
1304 spin_lock_init(&ring->execlist_lock);
Thomas Daniele981e7b2014-07-24 17:04:39 +01001305 ring->next_context_status_buffer = 0;
Michel Thierryacdd8842014-07-24 17:04:38 +01001306
Oscar Mateo48d82382014-07-24 17:04:23 +01001307 ret = i915_cmd_parser_init_ring(ring);
1308 if (ret)
1309 return ret;
1310
1311 if (ring->init) {
1312 ret = ring->init(ring);
1313 if (ret)
1314 return ret;
1315 }
1316
Oscar Mateo564ddb22014-08-21 11:40:54 +01001317 ret = intel_lr_context_deferred_create(ring->default_context, ring);
1318
1319 return ret;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001320}
1321
1322static int logical_render_ring_init(struct drm_device *dev)
1323{
1324 struct drm_i915_private *dev_priv = dev->dev_private;
1325 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
1326
1327 ring->name = "render ring";
1328 ring->id = RCS;
1329 ring->mmio_base = RENDER_RING_BASE;
1330 ring->irq_enable_mask =
1331 GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001332 ring->irq_keep_mask =
1333 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
1334 if (HAS_L3_DPF(dev))
1335 ring->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001336
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001337 ring->init = gen8_init_render_ring;
Michel Thierry771b9a52014-11-11 16:47:33 +00001338 ring->init_context = intel_logical_ring_workarounds_emit;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001339 ring->cleanup = intel_fini_pipe_control;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001340 ring->get_seqno = gen8_get_seqno;
1341 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001342 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001343 ring->emit_flush = gen8_emit_flush_render;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001344 ring->irq_get = gen8_logical_ring_get_irq;
1345 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001346 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001347
Oscar Mateo454afeb2014-07-24 17:04:22 +01001348 return logical_ring_init(dev, ring);
1349}
1350
1351static int logical_bsd_ring_init(struct drm_device *dev)
1352{
1353 struct drm_i915_private *dev_priv = dev->dev_private;
1354 struct intel_engine_cs *ring = &dev_priv->ring[VCS];
1355
1356 ring->name = "bsd ring";
1357 ring->id = VCS;
1358 ring->mmio_base = GEN6_BSD_RING_BASE;
1359 ring->irq_enable_mask =
1360 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001361 ring->irq_keep_mask =
1362 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001363
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001364 ring->init = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001365 ring->get_seqno = gen8_get_seqno;
1366 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001367 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001368 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001369 ring->irq_get = gen8_logical_ring_get_irq;
1370 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001371 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001372
Oscar Mateo454afeb2014-07-24 17:04:22 +01001373 return logical_ring_init(dev, ring);
1374}
1375
1376static int logical_bsd2_ring_init(struct drm_device *dev)
1377{
1378 struct drm_i915_private *dev_priv = dev->dev_private;
1379 struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
1380
1381 ring->name = "bds2 ring";
1382 ring->id = VCS2;
1383 ring->mmio_base = GEN8_BSD2_RING_BASE;
1384 ring->irq_enable_mask =
1385 GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001386 ring->irq_keep_mask =
1387 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001388
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001389 ring->init = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001390 ring->get_seqno = gen8_get_seqno;
1391 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001392 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001393 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001394 ring->irq_get = gen8_logical_ring_get_irq;
1395 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001396 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001397
Oscar Mateo454afeb2014-07-24 17:04:22 +01001398 return logical_ring_init(dev, ring);
1399}
1400
1401static int logical_blt_ring_init(struct drm_device *dev)
1402{
1403 struct drm_i915_private *dev_priv = dev->dev_private;
1404 struct intel_engine_cs *ring = &dev_priv->ring[BCS];
1405
1406 ring->name = "blitter ring";
1407 ring->id = BCS;
1408 ring->mmio_base = BLT_RING_BASE;
1409 ring->irq_enable_mask =
1410 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001411 ring->irq_keep_mask =
1412 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001413
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001414 ring->init = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001415 ring->get_seqno = gen8_get_seqno;
1416 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001417 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001418 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001419 ring->irq_get = gen8_logical_ring_get_irq;
1420 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001421 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001422
Oscar Mateo454afeb2014-07-24 17:04:22 +01001423 return logical_ring_init(dev, ring);
1424}
1425
1426static int logical_vebox_ring_init(struct drm_device *dev)
1427{
1428 struct drm_i915_private *dev_priv = dev->dev_private;
1429 struct intel_engine_cs *ring = &dev_priv->ring[VECS];
1430
1431 ring->name = "video enhancement ring";
1432 ring->id = VECS;
1433 ring->mmio_base = VEBOX_RING_BASE;
1434 ring->irq_enable_mask =
1435 GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001436 ring->irq_keep_mask =
1437 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001438
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001439 ring->init = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001440 ring->get_seqno = gen8_get_seqno;
1441 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001442 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001443 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001444 ring->irq_get = gen8_logical_ring_get_irq;
1445 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001446 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001447
Oscar Mateo454afeb2014-07-24 17:04:22 +01001448 return logical_ring_init(dev, ring);
1449}
1450
Oscar Mateo73e4d072014-07-24 17:04:48 +01001451/**
1452 * intel_logical_rings_init() - allocate, populate and init the Engine Command Streamers
1453 * @dev: DRM device.
1454 *
1455 * This function inits the engines for an Execlists submission style (the equivalent in the
1456 * legacy ringbuffer submission world would be i915_gem_init_rings). It does it only for
1457 * those engines that are present in the hardware.
1458 *
1459 * Return: non-zero if the initialization failed.
1460 */
Oscar Mateo454afeb2014-07-24 17:04:22 +01001461int intel_logical_rings_init(struct drm_device *dev)
1462{
1463 struct drm_i915_private *dev_priv = dev->dev_private;
1464 int ret;
1465
1466 ret = logical_render_ring_init(dev);
1467 if (ret)
1468 return ret;
1469
1470 if (HAS_BSD(dev)) {
1471 ret = logical_bsd_ring_init(dev);
1472 if (ret)
1473 goto cleanup_render_ring;
1474 }
1475
1476 if (HAS_BLT(dev)) {
1477 ret = logical_blt_ring_init(dev);
1478 if (ret)
1479 goto cleanup_bsd_ring;
1480 }
1481
1482 if (HAS_VEBOX(dev)) {
1483 ret = logical_vebox_ring_init(dev);
1484 if (ret)
1485 goto cleanup_blt_ring;
1486 }
1487
1488 if (HAS_BSD2(dev)) {
1489 ret = logical_bsd2_ring_init(dev);
1490 if (ret)
1491 goto cleanup_vebox_ring;
1492 }
1493
1494 ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
1495 if (ret)
1496 goto cleanup_bsd2_ring;
1497
1498 return 0;
1499
1500cleanup_bsd2_ring:
1501 intel_logical_ring_cleanup(&dev_priv->ring[VCS2]);
1502cleanup_vebox_ring:
1503 intel_logical_ring_cleanup(&dev_priv->ring[VECS]);
1504cleanup_blt_ring:
1505 intel_logical_ring_cleanup(&dev_priv->ring[BCS]);
1506cleanup_bsd_ring:
1507 intel_logical_ring_cleanup(&dev_priv->ring[VCS]);
1508cleanup_render_ring:
1509 intel_logical_ring_cleanup(&dev_priv->ring[RCS]);
1510
1511 return ret;
1512}
1513
Oscar Mateo564ddb22014-08-21 11:40:54 +01001514int intel_lr_context_render_state_init(struct intel_engine_cs *ring,
1515 struct intel_context *ctx)
1516{
1517 struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
1518 struct render_state so;
1519 struct drm_i915_file_private *file_priv = ctx->file_priv;
1520 struct drm_file *file = file_priv ? file_priv->file : NULL;
1521 int ret;
1522
1523 ret = i915_gem_render_state_prepare(ring, &so);
1524 if (ret)
1525 return ret;
1526
1527 if (so.rodata == NULL)
1528 return 0;
1529
1530 ret = ring->emit_bb_start(ringbuf,
1531 so.ggtt_offset,
1532 I915_DISPATCH_SECURE);
1533 if (ret)
1534 goto out;
1535
1536 i915_vma_move_to_active(i915_gem_obj_to_ggtt(so.obj), ring);
1537
1538 ret = __i915_add_request(ring, file, so.obj, NULL);
1539 /* intel_logical_ring_add_request moves object to inactive if it
1540 * fails */
1541out:
1542 i915_gem_render_state_fini(&so);
1543 return ret;
1544}
1545
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001546static int
1547populate_lr_context(struct intel_context *ctx, struct drm_i915_gem_object *ctx_obj,
1548 struct intel_engine_cs *ring, struct intel_ringbuffer *ringbuf)
1549{
Thomas Daniel2d965532014-08-19 10:13:36 +01001550 struct drm_device *dev = ring->dev;
1551 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001552 struct drm_i915_gem_object *ring_obj = ringbuf->obj;
Daniel Vetterae6c4802014-08-06 15:04:53 +02001553 struct i915_hw_ppgtt *ppgtt = ctx->ppgtt;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001554 struct page *page;
1555 uint32_t *reg_state;
1556 int ret;
1557
Thomas Daniel2d965532014-08-19 10:13:36 +01001558 if (!ppgtt)
1559 ppgtt = dev_priv->mm.aliasing_ppgtt;
1560
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001561 ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
1562 if (ret) {
1563 DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
1564 return ret;
1565 }
1566
1567 ret = i915_gem_object_get_pages(ctx_obj);
1568 if (ret) {
1569 DRM_DEBUG_DRIVER("Could not get object pages\n");
1570 return ret;
1571 }
1572
1573 i915_gem_object_pin_pages(ctx_obj);
1574
1575 /* The second page of the context object contains some fields which must
1576 * be set up prior to the first execution. */
1577 page = i915_gem_object_get_page(ctx_obj, 1);
1578 reg_state = kmap_atomic(page);
1579
1580 /* A context is actually a big batch buffer with several MI_LOAD_REGISTER_IMM
1581 * commands followed by (reg, value) pairs. The values we are setting here are
1582 * only for the first context restore: on a subsequent save, the GPU will
1583 * recreate this batchbuffer with new values (including all the missing
1584 * MI_LOAD_REGISTER_IMM commands that we are not initializing here). */
1585 if (ring->id == RCS)
1586 reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(14);
1587 else
1588 reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(11);
1589 reg_state[CTX_LRI_HEADER_0] |= MI_LRI_FORCE_POSTED;
1590 reg_state[CTX_CONTEXT_CONTROL] = RING_CONTEXT_CONTROL(ring);
1591 reg_state[CTX_CONTEXT_CONTROL+1] =
1592 _MASKED_BIT_ENABLE((1<<3) | MI_RESTORE_INHIBIT);
1593 reg_state[CTX_RING_HEAD] = RING_HEAD(ring->mmio_base);
1594 reg_state[CTX_RING_HEAD+1] = 0;
1595 reg_state[CTX_RING_TAIL] = RING_TAIL(ring->mmio_base);
1596 reg_state[CTX_RING_TAIL+1] = 0;
1597 reg_state[CTX_RING_BUFFER_START] = RING_START(ring->mmio_base);
1598 reg_state[CTX_RING_BUFFER_START+1] = i915_gem_obj_ggtt_offset(ring_obj);
1599 reg_state[CTX_RING_BUFFER_CONTROL] = RING_CTL(ring->mmio_base);
1600 reg_state[CTX_RING_BUFFER_CONTROL+1] =
1601 ((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES) | RING_VALID;
1602 reg_state[CTX_BB_HEAD_U] = ring->mmio_base + 0x168;
1603 reg_state[CTX_BB_HEAD_U+1] = 0;
1604 reg_state[CTX_BB_HEAD_L] = ring->mmio_base + 0x140;
1605 reg_state[CTX_BB_HEAD_L+1] = 0;
1606 reg_state[CTX_BB_STATE] = ring->mmio_base + 0x110;
1607 reg_state[CTX_BB_STATE+1] = (1<<5);
1608 reg_state[CTX_SECOND_BB_HEAD_U] = ring->mmio_base + 0x11c;
1609 reg_state[CTX_SECOND_BB_HEAD_U+1] = 0;
1610 reg_state[CTX_SECOND_BB_HEAD_L] = ring->mmio_base + 0x114;
1611 reg_state[CTX_SECOND_BB_HEAD_L+1] = 0;
1612 reg_state[CTX_SECOND_BB_STATE] = ring->mmio_base + 0x118;
1613 reg_state[CTX_SECOND_BB_STATE+1] = 0;
1614 if (ring->id == RCS) {
1615 /* TODO: according to BSpec, the register state context
1616 * for CHV does not have these. OTOH, these registers do
1617 * exist in CHV. I'm waiting for a clarification */
1618 reg_state[CTX_BB_PER_CTX_PTR] = ring->mmio_base + 0x1c0;
1619 reg_state[CTX_BB_PER_CTX_PTR+1] = 0;
1620 reg_state[CTX_RCS_INDIRECT_CTX] = ring->mmio_base + 0x1c4;
1621 reg_state[CTX_RCS_INDIRECT_CTX+1] = 0;
1622 reg_state[CTX_RCS_INDIRECT_CTX_OFFSET] = ring->mmio_base + 0x1c8;
1623 reg_state[CTX_RCS_INDIRECT_CTX_OFFSET+1] = 0;
1624 }
1625 reg_state[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9);
1626 reg_state[CTX_LRI_HEADER_1] |= MI_LRI_FORCE_POSTED;
1627 reg_state[CTX_CTX_TIMESTAMP] = ring->mmio_base + 0x3a8;
1628 reg_state[CTX_CTX_TIMESTAMP+1] = 0;
1629 reg_state[CTX_PDP3_UDW] = GEN8_RING_PDP_UDW(ring, 3);
1630 reg_state[CTX_PDP3_LDW] = GEN8_RING_PDP_LDW(ring, 3);
1631 reg_state[CTX_PDP2_UDW] = GEN8_RING_PDP_UDW(ring, 2);
1632 reg_state[CTX_PDP2_LDW] = GEN8_RING_PDP_LDW(ring, 2);
1633 reg_state[CTX_PDP1_UDW] = GEN8_RING_PDP_UDW(ring, 1);
1634 reg_state[CTX_PDP1_LDW] = GEN8_RING_PDP_LDW(ring, 1);
1635 reg_state[CTX_PDP0_UDW] = GEN8_RING_PDP_UDW(ring, 0);
1636 reg_state[CTX_PDP0_LDW] = GEN8_RING_PDP_LDW(ring, 0);
1637 reg_state[CTX_PDP3_UDW+1] = upper_32_bits(ppgtt->pd_dma_addr[3]);
1638 reg_state[CTX_PDP3_LDW+1] = lower_32_bits(ppgtt->pd_dma_addr[3]);
1639 reg_state[CTX_PDP2_UDW+1] = upper_32_bits(ppgtt->pd_dma_addr[2]);
1640 reg_state[CTX_PDP2_LDW+1] = lower_32_bits(ppgtt->pd_dma_addr[2]);
1641 reg_state[CTX_PDP1_UDW+1] = upper_32_bits(ppgtt->pd_dma_addr[1]);
1642 reg_state[CTX_PDP1_LDW+1] = lower_32_bits(ppgtt->pd_dma_addr[1]);
1643 reg_state[CTX_PDP0_UDW+1] = upper_32_bits(ppgtt->pd_dma_addr[0]);
1644 reg_state[CTX_PDP0_LDW+1] = lower_32_bits(ppgtt->pd_dma_addr[0]);
1645 if (ring->id == RCS) {
1646 reg_state[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
1647 reg_state[CTX_R_PWR_CLK_STATE] = 0x20c8;
1648 reg_state[CTX_R_PWR_CLK_STATE+1] = 0;
1649 }
1650
1651 kunmap_atomic(reg_state);
1652
1653 ctx_obj->dirty = 1;
1654 set_page_dirty(page);
1655 i915_gem_object_unpin_pages(ctx_obj);
1656
1657 return 0;
1658}
1659
Oscar Mateo73e4d072014-07-24 17:04:48 +01001660/**
1661 * intel_lr_context_free() - free the LRC specific bits of a context
1662 * @ctx: the LR context to free.
1663 *
1664 * The real context freeing is done in i915_gem_context_free: this only
1665 * takes care of the bits that are LRC related: the per-engine backing
1666 * objects and the logical ringbuffer.
1667 */
Oscar Mateoede7d422014-07-24 17:04:12 +01001668void intel_lr_context_free(struct intel_context *ctx)
1669{
Oscar Mateo8c8579172014-07-24 17:04:14 +01001670 int i;
1671
1672 for (i = 0; i < I915_NUM_RINGS; i++) {
1673 struct drm_i915_gem_object *ctx_obj = ctx->engine[i].state;
Oscar Mateo84c23772014-07-24 17:04:15 +01001674 struct intel_ringbuffer *ringbuf = ctx->engine[i].ringbuf;
1675
Oscar Mateo8c8579172014-07-24 17:04:14 +01001676 if (ctx_obj) {
Oscar Mateo84c23772014-07-24 17:04:15 +01001677 intel_destroy_ringbuffer_obj(ringbuf);
1678 kfree(ringbuf);
Oscar Mateo8c8579172014-07-24 17:04:14 +01001679 i915_gem_object_ggtt_unpin(ctx_obj);
1680 drm_gem_object_unreference(&ctx_obj->base);
1681 }
1682 }
1683}
1684
1685static uint32_t get_lr_context_size(struct intel_engine_cs *ring)
1686{
1687 int ret = 0;
1688
Michael H. Nguyen468c6812014-11-13 17:51:49 +00001689 WARN_ON(INTEL_INFO(ring->dev)->gen < 8);
Oscar Mateo8c8579172014-07-24 17:04:14 +01001690
1691 switch (ring->id) {
1692 case RCS:
Michael H. Nguyen468c6812014-11-13 17:51:49 +00001693 if (INTEL_INFO(ring->dev)->gen >= 9)
1694 ret = GEN9_LR_CONTEXT_RENDER_SIZE;
1695 else
1696 ret = GEN8_LR_CONTEXT_RENDER_SIZE;
Oscar Mateo8c8579172014-07-24 17:04:14 +01001697 break;
1698 case VCS:
1699 case BCS:
1700 case VECS:
1701 case VCS2:
1702 ret = GEN8_LR_CONTEXT_OTHER_SIZE;
1703 break;
1704 }
1705
1706 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01001707}
1708
Daniel Vetter70b0ea82014-11-18 09:09:32 +01001709static void lrc_setup_hardware_status_page(struct intel_engine_cs *ring,
Thomas Daniel1df06b72014-10-29 09:52:51 +00001710 struct drm_i915_gem_object *default_ctx_obj)
1711{
1712 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1713
1714 /* The status page is offset 0 from the default context object
1715 * in LRC mode. */
1716 ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(default_ctx_obj);
1717 ring->status_page.page_addr =
1718 kmap(sg_page(default_ctx_obj->pages->sgl));
Thomas Daniel1df06b72014-10-29 09:52:51 +00001719 ring->status_page.obj = default_ctx_obj;
1720
1721 I915_WRITE(RING_HWS_PGA(ring->mmio_base),
1722 (u32)ring->status_page.gfx_addr);
1723 POSTING_READ(RING_HWS_PGA(ring->mmio_base));
Thomas Daniel1df06b72014-10-29 09:52:51 +00001724}
1725
Oscar Mateo73e4d072014-07-24 17:04:48 +01001726/**
1727 * intel_lr_context_deferred_create() - create the LRC specific bits of a context
1728 * @ctx: LR context to create.
1729 * @ring: engine to be used with the context.
1730 *
1731 * This function can be called more than once, with different engines, if we plan
1732 * to use the context with them. The context backing objects and the ringbuffers
1733 * (specially the ringbuffer backing objects) suck a lot of memory up, and that's why
1734 * the creation is a deferred call: it's better to make sure first that we need to use
1735 * a given ring with the context.
1736 *
Masanari Iida32197aa2014-10-20 23:53:13 +09001737 * Return: non-zero on error.
Oscar Mateo73e4d072014-07-24 17:04:48 +01001738 */
Oscar Mateoede7d422014-07-24 17:04:12 +01001739int intel_lr_context_deferred_create(struct intel_context *ctx,
1740 struct intel_engine_cs *ring)
1741{
Oscar Mateo8c8579172014-07-24 17:04:14 +01001742 struct drm_device *dev = ring->dev;
1743 struct drm_i915_gem_object *ctx_obj;
1744 uint32_t context_size;
Oscar Mateo84c23772014-07-24 17:04:15 +01001745 struct intel_ringbuffer *ringbuf;
Oscar Mateo8c8579172014-07-24 17:04:14 +01001746 int ret;
1747
Oscar Mateoede7d422014-07-24 17:04:12 +01001748 WARN_ON(ctx->legacy_hw_ctx.rcs_state != NULL);
Oscar Mateo48d82382014-07-24 17:04:23 +01001749 if (ctx->engine[ring->id].state)
1750 return 0;
Oscar Mateoede7d422014-07-24 17:04:12 +01001751
Oscar Mateo8c8579172014-07-24 17:04:14 +01001752 context_size = round_up(get_lr_context_size(ring), 4096);
1753
1754 ctx_obj = i915_gem_alloc_context_obj(dev, context_size);
1755 if (IS_ERR(ctx_obj)) {
1756 ret = PTR_ERR(ctx_obj);
1757 DRM_DEBUG_DRIVER("Alloc LRC backing obj failed: %d\n", ret);
1758 return ret;
1759 }
1760
1761 ret = i915_gem_obj_ggtt_pin(ctx_obj, GEN8_LR_CONTEXT_ALIGN, 0);
1762 if (ret) {
1763 DRM_DEBUG_DRIVER("Pin LRC backing obj failed: %d\n", ret);
1764 drm_gem_object_unreference(&ctx_obj->base);
1765 return ret;
1766 }
1767
Oscar Mateo84c23772014-07-24 17:04:15 +01001768 ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
1769 if (!ringbuf) {
1770 DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s\n",
1771 ring->name);
1772 i915_gem_object_ggtt_unpin(ctx_obj);
1773 drm_gem_object_unreference(&ctx_obj->base);
1774 ret = -ENOMEM;
1775 return ret;
1776 }
1777
Daniel Vetter0c7dd532014-08-11 16:17:44 +02001778 ringbuf->ring = ring;
Oscar Mateo582d67f2014-07-24 17:04:16 +01001779 ringbuf->FIXME_lrc_ctx = ctx;
1780
Oscar Mateo84c23772014-07-24 17:04:15 +01001781 ringbuf->size = 32 * PAGE_SIZE;
1782 ringbuf->effective_size = ringbuf->size;
1783 ringbuf->head = 0;
1784 ringbuf->tail = 0;
1785 ringbuf->space = ringbuf->size;
1786 ringbuf->last_retired_head = -1;
1787
1788 /* TODO: For now we put this in the mappable region so that we can reuse
1789 * the existing ringbuffer code which ioremaps it. When we start
1790 * creating many contexts, this will no longer work and we must switch
1791 * to a kmapish interface.
1792 */
1793 ret = intel_alloc_ringbuffer_obj(dev, ringbuf);
1794 if (ret) {
1795 DRM_DEBUG_DRIVER("Failed to allocate ringbuffer obj %s: %d\n",
1796 ring->name, ret);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001797 goto error;
1798 }
1799
1800 ret = populate_lr_context(ctx, ctx_obj, ring, ringbuf);
1801 if (ret) {
1802 DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
1803 intel_destroy_ringbuffer_obj(ringbuf);
1804 goto error;
Oscar Mateo84c23772014-07-24 17:04:15 +01001805 }
1806
1807 ctx->engine[ring->id].ringbuf = ringbuf;
Oscar Mateo8c8579172014-07-24 17:04:14 +01001808 ctx->engine[ring->id].state = ctx_obj;
Oscar Mateoede7d422014-07-24 17:04:12 +01001809
Daniel Vetter70b0ea82014-11-18 09:09:32 +01001810 if (ctx == ring->default_context)
1811 lrc_setup_hardware_status_page(ring, ctx_obj);
Oscar Mateo564ddb22014-08-21 11:40:54 +01001812
1813 if (ring->id == RCS && !ctx->rcs_initialized) {
Michel Thierry771b9a52014-11-11 16:47:33 +00001814 if (ring->init_context) {
1815 ret = ring->init_context(ring, ctx);
1816 if (ret)
1817 DRM_ERROR("ring init context: %d\n", ret);
1818 }
1819
Oscar Mateo564ddb22014-08-21 11:40:54 +01001820 ret = intel_lr_context_render_state_init(ring, ctx);
1821 if (ret) {
1822 DRM_ERROR("Init render state failed: %d\n", ret);
1823 ctx->engine[ring->id].ringbuf = NULL;
1824 ctx->engine[ring->id].state = NULL;
1825 intel_destroy_ringbuffer_obj(ringbuf);
1826 goto error;
1827 }
1828 ctx->rcs_initialized = true;
1829 }
1830
Oscar Mateoede7d422014-07-24 17:04:12 +01001831 return 0;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001832
1833error:
1834 kfree(ringbuf);
1835 i915_gem_object_ggtt_unpin(ctx_obj);
1836 drm_gem_object_unreference(&ctx_obj->base);
1837 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01001838}