blob: 0eee4288519bf47780ca32c45cb9599d7c8e1e05 [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
Oscar Mateo127f1002014-07-24 17:04:11 +0100223 if (enable_execlists == 0)
224 return 0;
225
Oscar Mateo14bf9932014-07-24 17:04:34 +0100226 if (HAS_LOGICAL_RING_CONTEXTS(dev) && USES_PPGTT(dev) &&
227 i915.use_mmio_flip >= 0)
Oscar Mateo127f1002014-07-24 17:04:11 +0100228 return 1;
229
230 return 0;
231}
Oscar Mateoede7d422014-07-24 17:04:12 +0100232
Oscar Mateo73e4d072014-07-24 17:04:48 +0100233/**
234 * intel_execlists_ctx_id() - get the Execlists Context ID
235 * @ctx_obj: Logical Ring Context backing object.
236 *
237 * Do not confuse with ctx->id! Unfortunately we have a name overload
238 * here: the old context ID we pass to userspace as a handler so that
239 * they can refer to a context, and the new context ID we pass to the
240 * ELSP so that the GPU can inform us of the context status via
241 * interrupts.
242 *
243 * Return: 20-bits globally unique context ID.
244 */
Ben Widawsky84b790f2014-07-24 17:04:36 +0100245u32 intel_execlists_ctx_id(struct drm_i915_gem_object *ctx_obj)
246{
247 u32 lrca = i915_gem_obj_ggtt_offset(ctx_obj);
248
249 /* LRCA is required to be 4K aligned so the more significant 20 bits
250 * are globally unique */
251 return lrca >> 12;
252}
253
254static uint64_t execlists_ctx_descriptor(struct drm_i915_gem_object *ctx_obj)
255{
256 uint64_t desc;
257 uint64_t lrca = i915_gem_obj_ggtt_offset(ctx_obj);
Michel Thierryacdd8842014-07-24 17:04:38 +0100258
259 WARN_ON(lrca & 0xFFFFFFFF00000FFFULL);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100260
261 desc = GEN8_CTX_VALID;
262 desc |= LEGACY_CONTEXT << GEN8_CTX_MODE_SHIFT;
263 desc |= GEN8_CTX_L3LLC_COHERENT;
264 desc |= GEN8_CTX_PRIVILEGE;
265 desc |= lrca;
266 desc |= (u64)intel_execlists_ctx_id(ctx_obj) << GEN8_CTX_ID_SHIFT;
267
268 /* TODO: WaDisableLiteRestore when we start using semaphore
269 * signalling between Command Streamers */
270 /* desc |= GEN8_CTX_FORCE_RESTORE; */
271
272 return desc;
273}
274
275static void execlists_elsp_write(struct intel_engine_cs *ring,
276 struct drm_i915_gem_object *ctx_obj0,
277 struct drm_i915_gem_object *ctx_obj1)
278{
Tvrtko Ursulin6e7cc472014-11-13 17:51:51 +0000279 struct drm_device *dev = ring->dev;
280 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100281 uint64_t temp = 0;
282 uint32_t desc[4];
Thomas Daniele981e7b2014-07-24 17:04:39 +0100283 unsigned long flags;
Ben Widawsky84b790f2014-07-24 17:04:36 +0100284
285 /* XXX: You must always write both descriptors in the order below. */
286 if (ctx_obj1)
287 temp = execlists_ctx_descriptor(ctx_obj1);
288 else
289 temp = 0;
290 desc[1] = (u32)(temp >> 32);
291 desc[0] = (u32)temp;
292
293 temp = execlists_ctx_descriptor(ctx_obj0);
294 desc[3] = (u32)(temp >> 32);
295 desc[2] = (u32)temp;
296
Thomas Daniele981e7b2014-07-24 17:04:39 +0100297 /* Set Force Wakeup bit to prevent GT from entering C6 while ELSP writes
298 * are in progress.
299 *
300 * The other problem is that we can't just call gen6_gt_force_wake_get()
301 * because that function calls intel_runtime_pm_get(), which might sleep.
302 * Instead, we do the runtime_pm_get/put when creating/destroying requests.
303 */
304 spin_lock_irqsave(&dev_priv->uncore.lock, flags);
Tvrtko Ursulin6e7cc472014-11-13 17:51:51 +0000305 if (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen >= 9) {
Deepak Sa01b0e92014-09-09 19:14:16 +0530306 if (dev_priv->uncore.fw_rendercount++ == 0)
307 dev_priv->uncore.funcs.force_wake_get(dev_priv,
308 FORCEWAKE_RENDER);
309 if (dev_priv->uncore.fw_mediacount++ == 0)
310 dev_priv->uncore.funcs.force_wake_get(dev_priv,
311 FORCEWAKE_MEDIA);
Tvrtko Ursulin6e7cc472014-11-13 17:51:51 +0000312 if (INTEL_INFO(dev)->gen >= 9) {
313 if (dev_priv->uncore.fw_blittercount++ == 0)
314 dev_priv->uncore.funcs.force_wake_get(dev_priv,
315 FORCEWAKE_BLITTER);
316 }
Deepak Sa01b0e92014-09-09 19:14:16 +0530317 } else {
318 if (dev_priv->uncore.forcewake_count++ == 0)
319 dev_priv->uncore.funcs.force_wake_get(dev_priv,
320 FORCEWAKE_ALL);
321 }
Thomas Daniele981e7b2014-07-24 17:04:39 +0100322 spin_unlock_irqrestore(&dev_priv->uncore.lock, flags);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100323
324 I915_WRITE(RING_ELSP(ring), desc[1]);
325 I915_WRITE(RING_ELSP(ring), desc[0]);
326 I915_WRITE(RING_ELSP(ring), desc[3]);
327 /* The context is automatically loaded after the following */
328 I915_WRITE(RING_ELSP(ring), desc[2]);
329
330 /* ELSP is a wo register, so use another nearby reg for posting instead */
331 POSTING_READ(RING_EXECLIST_STATUS(ring));
332
Thomas Daniele981e7b2014-07-24 17:04:39 +0100333 /* Release Force Wakeup (see the big comment above). */
334 spin_lock_irqsave(&dev_priv->uncore.lock, flags);
Tvrtko Ursulin6e7cc472014-11-13 17:51:51 +0000335 if (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen >= 9) {
Deepak Sa01b0e92014-09-09 19:14:16 +0530336 if (--dev_priv->uncore.fw_rendercount == 0)
337 dev_priv->uncore.funcs.force_wake_put(dev_priv,
338 FORCEWAKE_RENDER);
339 if (--dev_priv->uncore.fw_mediacount == 0)
340 dev_priv->uncore.funcs.force_wake_put(dev_priv,
341 FORCEWAKE_MEDIA);
Tvrtko Ursulin6e7cc472014-11-13 17:51:51 +0000342 if (INTEL_INFO(dev)->gen >= 9) {
343 if (--dev_priv->uncore.fw_blittercount == 0)
344 dev_priv->uncore.funcs.force_wake_put(dev_priv,
345 FORCEWAKE_BLITTER);
346 }
Deepak Sa01b0e92014-09-09 19:14:16 +0530347 } else {
348 if (--dev_priv->uncore.forcewake_count == 0)
349 dev_priv->uncore.funcs.force_wake_put(dev_priv,
350 FORCEWAKE_ALL);
351 }
352
Thomas Daniele981e7b2014-07-24 17:04:39 +0100353 spin_unlock_irqrestore(&dev_priv->uncore.lock, flags);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100354}
355
Oscar Mateoae1250b2014-07-24 17:04:37 +0100356static int execlists_ctx_write_tail(struct drm_i915_gem_object *ctx_obj, u32 tail)
357{
358 struct page *page;
359 uint32_t *reg_state;
360
361 page = i915_gem_object_get_page(ctx_obj, 1);
362 reg_state = kmap_atomic(page);
363
364 reg_state[CTX_RING_TAIL+1] = tail;
365
366 kunmap_atomic(reg_state);
367
368 return 0;
369}
370
Dave Gordoncd0707c2014-10-30 15:41:56 +0000371static void execlists_submit_contexts(struct intel_engine_cs *ring,
372 struct intel_context *to0, u32 tail0,
373 struct intel_context *to1, u32 tail1)
Ben Widawsky84b790f2014-07-24 17:04:36 +0100374{
375 struct drm_i915_gem_object *ctx_obj0;
376 struct drm_i915_gem_object *ctx_obj1 = NULL;
377
378 ctx_obj0 = to0->engine[ring->id].state;
379 BUG_ON(!ctx_obj0);
Michel Thierryacdd8842014-07-24 17:04:38 +0100380 WARN_ON(!i915_gem_obj_is_pinned(ctx_obj0));
Ben Widawsky84b790f2014-07-24 17:04:36 +0100381
Oscar Mateoae1250b2014-07-24 17:04:37 +0100382 execlists_ctx_write_tail(ctx_obj0, tail0);
383
Ben Widawsky84b790f2014-07-24 17:04:36 +0100384 if (to1) {
385 ctx_obj1 = to1->engine[ring->id].state;
386 BUG_ON(!ctx_obj1);
Michel Thierryacdd8842014-07-24 17:04:38 +0100387 WARN_ON(!i915_gem_obj_is_pinned(ctx_obj1));
Oscar Mateoae1250b2014-07-24 17:04:37 +0100388
389 execlists_ctx_write_tail(ctx_obj1, tail1);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100390 }
391
392 execlists_elsp_write(ring, ctx_obj0, ctx_obj1);
Ben Widawsky84b790f2014-07-24 17:04:36 +0100393}
394
Michel Thierryacdd8842014-07-24 17:04:38 +0100395static void execlists_context_unqueue(struct intel_engine_cs *ring)
396{
397 struct intel_ctx_submit_request *req0 = NULL, *req1 = NULL;
398 struct intel_ctx_submit_request *cursor = NULL, *tmp = NULL;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100399 struct drm_i915_private *dev_priv = ring->dev->dev_private;
400
401 assert_spin_locked(&ring->execlist_lock);
Michel Thierryacdd8842014-07-24 17:04:38 +0100402
403 if (list_empty(&ring->execlist_queue))
404 return;
405
406 /* Try to read in pairs */
407 list_for_each_entry_safe(cursor, tmp, &ring->execlist_queue,
408 execlist_link) {
409 if (!req0) {
410 req0 = cursor;
411 } else if (req0->ctx == cursor->ctx) {
412 /* Same ctx: ignore first request, as second request
413 * will update tail past first request's workload */
Oscar Mateoe1fee722014-07-24 17:04:40 +0100414 cursor->elsp_submitted = req0->elsp_submitted;
Michel Thierryacdd8842014-07-24 17:04:38 +0100415 list_del(&req0->execlist_link);
Thomas Daniele981e7b2014-07-24 17:04:39 +0100416 queue_work(dev_priv->wq, &req0->work);
Michel Thierryacdd8842014-07-24 17:04:38 +0100417 req0 = cursor;
418 } else {
419 req1 = cursor;
420 break;
421 }
422 }
423
Oscar Mateoe1fee722014-07-24 17:04:40 +0100424 WARN_ON(req1 && req1->elsp_submitted);
425
Dave Gordoncd0707c2014-10-30 15:41:56 +0000426 execlists_submit_contexts(ring, req0->ctx, req0->tail,
427 req1 ? req1->ctx : NULL,
428 req1 ? req1->tail : 0);
Oscar Mateoe1fee722014-07-24 17:04:40 +0100429
430 req0->elsp_submitted++;
431 if (req1)
432 req1->elsp_submitted++;
Michel Thierryacdd8842014-07-24 17:04:38 +0100433}
434
Thomas Daniele981e7b2014-07-24 17:04:39 +0100435static bool execlists_check_remove_request(struct intel_engine_cs *ring,
436 u32 request_id)
437{
438 struct drm_i915_private *dev_priv = ring->dev->dev_private;
439 struct intel_ctx_submit_request *head_req;
440
441 assert_spin_locked(&ring->execlist_lock);
442
443 head_req = list_first_entry_or_null(&ring->execlist_queue,
444 struct intel_ctx_submit_request,
445 execlist_link);
446
447 if (head_req != NULL) {
448 struct drm_i915_gem_object *ctx_obj =
449 head_req->ctx->engine[ring->id].state;
450 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);
456 queue_work(dev_priv->wq, &head_req->work);
457 return true;
458 }
Thomas Daniele981e7b2014-07-24 17:04:39 +0100459 }
460 }
461
462 return false;
463}
464
Oscar Mateo73e4d072014-07-24 17:04:48 +0100465/**
466 * intel_execlists_handle_ctx_events() - handle Context Switch interrupts
467 * @ring: Engine Command Streamer to handle.
468 *
469 * Check the unread Context Status Buffers and manage the submission of new
470 * contexts to the ELSP accordingly.
471 */
Thomas Daniele981e7b2014-07-24 17:04:39 +0100472void intel_execlists_handle_ctx_events(struct intel_engine_cs *ring)
473{
474 struct drm_i915_private *dev_priv = ring->dev->dev_private;
475 u32 status_pointer;
476 u8 read_pointer;
477 u8 write_pointer;
478 u32 status;
479 u32 status_id;
480 u32 submit_contexts = 0;
481
482 status_pointer = I915_READ(RING_CONTEXT_STATUS_PTR(ring));
483
484 read_pointer = ring->next_context_status_buffer;
485 write_pointer = status_pointer & 0x07;
486 if (read_pointer > write_pointer)
487 write_pointer += 6;
488
489 spin_lock(&ring->execlist_lock);
490
491 while (read_pointer < write_pointer) {
492 read_pointer++;
493 status = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
494 (read_pointer % 6) * 8);
495 status_id = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
496 (read_pointer % 6) * 8 + 4);
497
Oscar Mateoe1fee722014-07-24 17:04:40 +0100498 if (status & GEN8_CTX_STATUS_PREEMPTED) {
499 if (status & GEN8_CTX_STATUS_LITE_RESTORE) {
500 if (execlists_check_remove_request(ring, status_id))
501 WARN(1, "Lite Restored request removed from queue\n");
502 } else
503 WARN(1, "Preemption without Lite Restore\n");
504 }
505
506 if ((status & GEN8_CTX_STATUS_ACTIVE_IDLE) ||
507 (status & GEN8_CTX_STATUS_ELEMENT_SWITCH)) {
Thomas Daniele981e7b2014-07-24 17:04:39 +0100508 if (execlists_check_remove_request(ring, status_id))
509 submit_contexts++;
510 }
511 }
512
513 if (submit_contexts != 0)
514 execlists_context_unqueue(ring);
515
516 spin_unlock(&ring->execlist_lock);
517
518 WARN(submit_contexts > 2, "More than two context complete events?\n");
519 ring->next_context_status_buffer = write_pointer % 6;
520
521 I915_WRITE(RING_CONTEXT_STATUS_PTR(ring),
522 ((u32)ring->next_context_status_buffer & 0x07) << 8);
523}
524
525static void execlists_free_request_task(struct work_struct *work)
526{
527 struct intel_ctx_submit_request *req =
528 container_of(work, struct intel_ctx_submit_request, work);
529 struct drm_device *dev = req->ring->dev;
530 struct drm_i915_private *dev_priv = dev->dev_private;
531
532 intel_runtime_pm_put(dev_priv);
533
534 mutex_lock(&dev->struct_mutex);
535 i915_gem_context_unreference(req->ctx);
536 mutex_unlock(&dev->struct_mutex);
537
538 kfree(req);
539}
540
Michel Thierryacdd8842014-07-24 17:04:38 +0100541static int execlists_context_queue(struct intel_engine_cs *ring,
542 struct intel_context *to,
543 u32 tail)
544{
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100545 struct intel_ctx_submit_request *req = NULL, *cursor;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100546 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Michel Thierryacdd8842014-07-24 17:04:38 +0100547 unsigned long flags;
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100548 int num_elements = 0;
Michel Thierryacdd8842014-07-24 17:04:38 +0100549
550 req = kzalloc(sizeof(*req), GFP_KERNEL);
551 if (req == NULL)
552 return -ENOMEM;
553 req->ctx = to;
554 i915_gem_context_reference(req->ctx);
555 req->ring = ring;
556 req->tail = tail;
Thomas Daniele981e7b2014-07-24 17:04:39 +0100557 INIT_WORK(&req->work, execlists_free_request_task);
558
559 intel_runtime_pm_get(dev_priv);
Michel Thierryacdd8842014-07-24 17:04:38 +0100560
561 spin_lock_irqsave(&ring->execlist_lock, flags);
562
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100563 list_for_each_entry(cursor, &ring->execlist_queue, execlist_link)
564 if (++num_elements > 2)
565 break;
566
567 if (num_elements > 2) {
568 struct intel_ctx_submit_request *tail_req;
569
570 tail_req = list_last_entry(&ring->execlist_queue,
571 struct intel_ctx_submit_request,
572 execlist_link);
573
574 if (to == tail_req->ctx) {
575 WARN(tail_req->elsp_submitted != 0,
576 "More than 2 already-submitted reqs queued\n");
577 list_del(&tail_req->execlist_link);
578 queue_work(dev_priv->wq, &tail_req->work);
579 }
580 }
581
Michel Thierryacdd8842014-07-24 17:04:38 +0100582 list_add_tail(&req->execlist_link, &ring->execlist_queue);
Oscar Mateof1ad5a12014-07-24 17:04:41 +0100583 if (num_elements == 0)
Michel Thierryacdd8842014-07-24 17:04:38 +0100584 execlists_context_unqueue(ring);
585
586 spin_unlock_irqrestore(&ring->execlist_lock, flags);
587
588 return 0;
589}
590
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100591static int logical_ring_invalidate_all_caches(struct intel_ringbuffer *ringbuf)
592{
593 struct intel_engine_cs *ring = ringbuf->ring;
594 uint32_t flush_domains;
595 int ret;
596
597 flush_domains = 0;
598 if (ring->gpu_caches_dirty)
599 flush_domains = I915_GEM_GPU_DOMAINS;
600
601 ret = ring->emit_flush(ringbuf, I915_GEM_GPU_DOMAINS, flush_domains);
602 if (ret)
603 return ret;
604
605 ring->gpu_caches_dirty = false;
606 return 0;
607}
608
609static int execlists_move_to_gpu(struct intel_ringbuffer *ringbuf,
610 struct list_head *vmas)
611{
612 struct intel_engine_cs *ring = ringbuf->ring;
613 struct i915_vma *vma;
614 uint32_t flush_domains = 0;
615 bool flush_chipset = false;
616 int ret;
617
618 list_for_each_entry(vma, vmas, exec_list) {
619 struct drm_i915_gem_object *obj = vma->obj;
620
621 ret = i915_gem_object_sync(obj, ring);
622 if (ret)
623 return ret;
624
625 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
626 flush_chipset |= i915_gem_clflush_object(obj, false);
627
628 flush_domains |= obj->base.write_domain;
629 }
630
631 if (flush_domains & I915_GEM_DOMAIN_GTT)
632 wmb();
633
634 /* Unconditionally invalidate gpu caches and ensure that we do flush
635 * any residual writes from the previous batch.
636 */
637 return logical_ring_invalidate_all_caches(ringbuf);
638}
639
Oscar Mateo73e4d072014-07-24 17:04:48 +0100640/**
641 * execlists_submission() - submit a batchbuffer for execution, Execlists style
642 * @dev: DRM device.
643 * @file: DRM file.
644 * @ring: Engine Command Streamer to submit to.
645 * @ctx: Context to employ for this submission.
646 * @args: execbuffer call arguments.
647 * @vmas: list of vmas.
648 * @batch_obj: the batchbuffer to submit.
649 * @exec_start: batchbuffer start virtual address pointer.
650 * @flags: translated execbuffer call flags.
651 *
652 * This is the evil twin version of i915_gem_ringbuffer_submission. It abstracts
653 * away the submission details of the execbuffer ioctl call.
654 *
655 * Return: non-zero if the submission fails.
656 */
Oscar Mateo454afeb2014-07-24 17:04:22 +0100657int intel_execlists_submission(struct drm_device *dev, struct drm_file *file,
658 struct intel_engine_cs *ring,
659 struct intel_context *ctx,
660 struct drm_i915_gem_execbuffer2 *args,
661 struct list_head *vmas,
662 struct drm_i915_gem_object *batch_obj,
663 u64 exec_start, u32 flags)
664{
Oscar Mateoba8b7cc2014-07-24 17:04:33 +0100665 struct drm_i915_private *dev_priv = dev->dev_private;
666 struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
667 int instp_mode;
668 u32 instp_mask;
669 int ret;
670
671 instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
672 instp_mask = I915_EXEC_CONSTANTS_MASK;
673 switch (instp_mode) {
674 case I915_EXEC_CONSTANTS_REL_GENERAL:
675 case I915_EXEC_CONSTANTS_ABSOLUTE:
676 case I915_EXEC_CONSTANTS_REL_SURFACE:
677 if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) {
678 DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
679 return -EINVAL;
680 }
681
682 if (instp_mode != dev_priv->relative_constants_mode) {
683 if (instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
684 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
685 return -EINVAL;
686 }
687
688 /* The HW changed the meaning on this bit on gen6 */
689 instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
690 }
691 break;
692 default:
693 DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
694 return -EINVAL;
695 }
696
697 if (args->num_cliprects != 0) {
698 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
699 return -EINVAL;
700 } else {
701 if (args->DR4 == 0xffffffff) {
702 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
703 args->DR4 = 0;
704 }
705
706 if (args->DR1 || args->DR4 || args->cliprects_ptr) {
707 DRM_DEBUG("0 cliprects but dirt in cliprects fields\n");
708 return -EINVAL;
709 }
710 }
711
712 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
713 DRM_DEBUG("sol reset is gen7 only\n");
714 return -EINVAL;
715 }
716
717 ret = execlists_move_to_gpu(ringbuf, vmas);
718 if (ret)
719 return ret;
720
721 if (ring == &dev_priv->ring[RCS] &&
722 instp_mode != dev_priv->relative_constants_mode) {
723 ret = intel_logical_ring_begin(ringbuf, 4);
724 if (ret)
725 return ret;
726
727 intel_logical_ring_emit(ringbuf, MI_NOOP);
728 intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(1));
729 intel_logical_ring_emit(ringbuf, INSTPM);
730 intel_logical_ring_emit(ringbuf, instp_mask << 16 | instp_mode);
731 intel_logical_ring_advance(ringbuf);
732
733 dev_priv->relative_constants_mode = instp_mode;
734 }
735
736 ret = ring->emit_bb_start(ringbuf, exec_start, flags);
737 if (ret)
738 return ret;
739
740 i915_gem_execbuffer_move_to_active(vmas, ring);
741 i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
742
Oscar Mateo454afeb2014-07-24 17:04:22 +0100743 return 0;
744}
745
746void intel_logical_ring_stop(struct intel_engine_cs *ring)
747{
Oscar Mateo9832b9d2014-07-24 17:04:30 +0100748 struct drm_i915_private *dev_priv = ring->dev->dev_private;
749 int ret;
750
751 if (!intel_ring_initialized(ring))
752 return;
753
754 ret = intel_ring_idle(ring);
755 if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
756 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
757 ring->name, ret);
758
759 /* TODO: Is this correct with Execlists enabled? */
760 I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
761 if (wait_for_atomic((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
762 DRM_ERROR("%s :timed out trying to stop ring\n", ring->name);
763 return;
764 }
765 I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
Oscar Mateo454afeb2014-07-24 17:04:22 +0100766}
767
Oscar Mateo48e29f52014-07-24 17:04:29 +0100768int logical_ring_flush_all_caches(struct intel_ringbuffer *ringbuf)
769{
770 struct intel_engine_cs *ring = ringbuf->ring;
771 int ret;
772
773 if (!ring->gpu_caches_dirty)
774 return 0;
775
776 ret = ring->emit_flush(ringbuf, 0, I915_GEM_GPU_DOMAINS);
777 if (ret)
778 return ret;
779
780 ring->gpu_caches_dirty = false;
781 return 0;
782}
783
Oscar Mateo73e4d072014-07-24 17:04:48 +0100784/**
785 * intel_logical_ring_advance_and_submit() - advance the tail and submit the workload
786 * @ringbuf: Logical Ringbuffer to advance.
787 *
788 * The tail is updated in our logical ringbuffer struct, not in the actual context. What
789 * really happens during submission is that the context and current tail will be placed
790 * on a queue waiting for the ELSP to be ready to accept a new context submission. At that
791 * point, the tail *inside* the context is updated and the ELSP written to.
792 */
Oscar Mateo82e104c2014-07-24 17:04:26 +0100793void intel_logical_ring_advance_and_submit(struct intel_ringbuffer *ringbuf)
794{
Ben Widawsky84b790f2014-07-24 17:04:36 +0100795 struct intel_engine_cs *ring = ringbuf->ring;
796 struct intel_context *ctx = ringbuf->FIXME_lrc_ctx;
797
Oscar Mateo82e104c2014-07-24 17:04:26 +0100798 intel_logical_ring_advance(ringbuf);
799
Ben Widawsky84b790f2014-07-24 17:04:36 +0100800 if (intel_ring_stopped(ring))
Oscar Mateo82e104c2014-07-24 17:04:26 +0100801 return;
802
Michel Thierryacdd8842014-07-24 17:04:38 +0100803 execlists_context_queue(ring, ctx, ringbuf->tail);
Oscar Mateo82e104c2014-07-24 17:04:26 +0100804}
805
Oscar Mateo48e29f52014-07-24 17:04:29 +0100806static int logical_ring_alloc_seqno(struct intel_engine_cs *ring,
807 struct intel_context *ctx)
Oscar Mateo82e104c2014-07-24 17:04:26 +0100808{
809 if (ring->outstanding_lazy_seqno)
810 return 0;
811
812 if (ring->preallocated_lazy_request == NULL) {
813 struct drm_i915_gem_request *request;
814
815 request = kmalloc(sizeof(*request), GFP_KERNEL);
816 if (request == NULL)
817 return -ENOMEM;
818
Oscar Mateo48e29f52014-07-24 17:04:29 +0100819 /* Hold a reference to the context this request belongs to
820 * (we will need it when the time comes to emit/retire the
821 * request).
822 */
823 request->ctx = ctx;
824 i915_gem_context_reference(request->ctx);
825
Oscar Mateo82e104c2014-07-24 17:04:26 +0100826 ring->preallocated_lazy_request = request;
827 }
828
829 return i915_gem_get_seqno(ring->dev, &ring->outstanding_lazy_seqno);
830}
831
832static int logical_ring_wait_request(struct intel_ringbuffer *ringbuf,
833 int bytes)
834{
835 struct intel_engine_cs *ring = ringbuf->ring;
836 struct drm_i915_gem_request *request;
837 u32 seqno = 0;
838 int ret;
839
840 if (ringbuf->last_retired_head != -1) {
841 ringbuf->head = ringbuf->last_retired_head;
842 ringbuf->last_retired_head = -1;
843
844 ringbuf->space = intel_ring_space(ringbuf);
845 if (ringbuf->space >= bytes)
846 return 0;
847 }
848
849 list_for_each_entry(request, &ring->request_list, list) {
850 if (__intel_ring_space(request->tail, ringbuf->tail,
851 ringbuf->size) >= bytes) {
852 seqno = request->seqno;
853 break;
854 }
855 }
856
857 if (seqno == 0)
858 return -ENOSPC;
859
860 ret = i915_wait_seqno(ring, seqno);
861 if (ret)
862 return ret;
863
Oscar Mateo82e104c2014-07-24 17:04:26 +0100864 i915_gem_retire_requests_ring(ring);
865 ringbuf->head = ringbuf->last_retired_head;
866 ringbuf->last_retired_head = -1;
867
868 ringbuf->space = intel_ring_space(ringbuf);
869 return 0;
870}
871
872static int logical_ring_wait_for_space(struct intel_ringbuffer *ringbuf,
873 int bytes)
874{
875 struct intel_engine_cs *ring = ringbuf->ring;
876 struct drm_device *dev = ring->dev;
877 struct drm_i915_private *dev_priv = dev->dev_private;
878 unsigned long end;
879 int ret;
880
881 ret = logical_ring_wait_request(ringbuf, bytes);
882 if (ret != -ENOSPC)
883 return ret;
884
885 /* Force the context submission in case we have been skipping it */
886 intel_logical_ring_advance_and_submit(ringbuf);
887
888 /* With GEM the hangcheck timer should kick us out of the loop,
889 * leaving it early runs the risk of corrupting GEM state (due
890 * to running on almost untested codepaths). But on resume
891 * timers don't work yet, so prevent a complete hang in that
892 * case by choosing an insanely large timeout. */
893 end = jiffies + 60 * HZ;
894
895 do {
896 ringbuf->head = I915_READ_HEAD(ring);
897 ringbuf->space = intel_ring_space(ringbuf);
898 if (ringbuf->space >= bytes) {
899 ret = 0;
900 break;
901 }
902
903 msleep(1);
904
905 if (dev_priv->mm.interruptible && signal_pending(current)) {
906 ret = -ERESTARTSYS;
907 break;
908 }
909
910 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
911 dev_priv->mm.interruptible);
912 if (ret)
913 break;
914
915 if (time_after(jiffies, end)) {
916 ret = -EBUSY;
917 break;
918 }
919 } while (1);
920
921 return ret;
922}
923
924static int logical_ring_wrap_buffer(struct intel_ringbuffer *ringbuf)
925{
926 uint32_t __iomem *virt;
927 int rem = ringbuf->size - ringbuf->tail;
928
929 if (ringbuf->space < rem) {
930 int ret = logical_ring_wait_for_space(ringbuf, rem);
931
932 if (ret)
933 return ret;
934 }
935
936 virt = ringbuf->virtual_start + ringbuf->tail;
937 rem /= 4;
938 while (rem--)
939 iowrite32(MI_NOOP, virt++);
940
941 ringbuf->tail = 0;
942 ringbuf->space = intel_ring_space(ringbuf);
943
944 return 0;
945}
946
947static int logical_ring_prepare(struct intel_ringbuffer *ringbuf, int bytes)
948{
949 int ret;
950
951 if (unlikely(ringbuf->tail + bytes > ringbuf->effective_size)) {
952 ret = logical_ring_wrap_buffer(ringbuf);
953 if (unlikely(ret))
954 return ret;
955 }
956
957 if (unlikely(ringbuf->space < bytes)) {
958 ret = logical_ring_wait_for_space(ringbuf, bytes);
959 if (unlikely(ret))
960 return ret;
961 }
962
963 return 0;
964}
965
Oscar Mateo73e4d072014-07-24 17:04:48 +0100966/**
967 * intel_logical_ring_begin() - prepare the logical ringbuffer to accept some commands
968 *
969 * @ringbuf: Logical ringbuffer.
970 * @num_dwords: number of DWORDs that we plan to write to the ringbuffer.
971 *
972 * The ringbuffer might not be ready to accept the commands right away (maybe it needs to
973 * be wrapped, or wait a bit for the tail to be updated). This function takes care of that
974 * and also preallocates a request (every workload submission is still mediated through
975 * requests, same as it did with legacy ringbuffer submission).
976 *
977 * Return: non-zero if the ringbuffer is not ready to be written to.
978 */
Oscar Mateo82e104c2014-07-24 17:04:26 +0100979int intel_logical_ring_begin(struct intel_ringbuffer *ringbuf, int num_dwords)
980{
981 struct intel_engine_cs *ring = ringbuf->ring;
982 struct drm_device *dev = ring->dev;
983 struct drm_i915_private *dev_priv = dev->dev_private;
984 int ret;
985
986 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
987 dev_priv->mm.interruptible);
988 if (ret)
989 return ret;
990
991 ret = logical_ring_prepare(ringbuf, num_dwords * sizeof(uint32_t));
992 if (ret)
993 return ret;
994
995 /* Preallocate the olr before touching the ring */
Oscar Mateo48e29f52014-07-24 17:04:29 +0100996 ret = logical_ring_alloc_seqno(ring, ringbuf->FIXME_lrc_ctx);
Oscar Mateo82e104c2014-07-24 17:04:26 +0100997 if (ret)
998 return ret;
999
1000 ringbuf->space -= num_dwords * sizeof(uint32_t);
1001 return 0;
1002}
1003
Michel Thierry771b9a52014-11-11 16:47:33 +00001004static int intel_logical_ring_workarounds_emit(struct intel_engine_cs *ring,
1005 struct intel_context *ctx)
1006{
1007 int ret, i;
1008 struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
1009 struct drm_device *dev = ring->dev;
1010 struct drm_i915_private *dev_priv = dev->dev_private;
1011 struct i915_workarounds *w = &dev_priv->workarounds;
1012
1013 if (WARN_ON(w->count == 0))
1014 return 0;
1015
1016 ring->gpu_caches_dirty = true;
1017 ret = logical_ring_flush_all_caches(ringbuf);
1018 if (ret)
1019 return ret;
1020
1021 ret = intel_logical_ring_begin(ringbuf, w->count * 2 + 2);
1022 if (ret)
1023 return ret;
1024
1025 intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(w->count));
1026 for (i = 0; i < w->count; i++) {
1027 intel_logical_ring_emit(ringbuf, w->reg[i].addr);
1028 intel_logical_ring_emit(ringbuf, w->reg[i].value);
1029 }
1030 intel_logical_ring_emit(ringbuf, MI_NOOP);
1031
1032 intel_logical_ring_advance(ringbuf);
1033
1034 ring->gpu_caches_dirty = true;
1035 ret = logical_ring_flush_all_caches(ringbuf);
1036 if (ret)
1037 return ret;
1038
1039 return 0;
1040}
1041
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001042static int gen8_init_common_ring(struct intel_engine_cs *ring)
1043{
1044 struct drm_device *dev = ring->dev;
1045 struct drm_i915_private *dev_priv = dev->dev_private;
1046
Oscar Mateo73d477f2014-07-24 17:04:31 +01001047 I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
1048 I915_WRITE(RING_HWSTAM(ring->mmio_base), 0xffffffff);
1049
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001050 I915_WRITE(RING_MODE_GEN7(ring),
1051 _MASKED_BIT_DISABLE(GFX_REPLAY_MODE) |
1052 _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
1053 POSTING_READ(RING_MODE_GEN7(ring));
1054 DRM_DEBUG_DRIVER("Execlists enabled for %s\n", ring->name);
1055
1056 memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
1057
1058 return 0;
1059}
1060
1061static int gen8_init_render_ring(struct intel_engine_cs *ring)
1062{
1063 struct drm_device *dev = ring->dev;
1064 struct drm_i915_private *dev_priv = dev->dev_private;
1065 int ret;
1066
1067 ret = gen8_init_common_ring(ring);
1068 if (ret)
1069 return ret;
1070
1071 /* We need to disable the AsyncFlip performance optimisations in order
1072 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1073 * programmed to '1' on all products.
1074 *
1075 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
1076 */
1077 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1078
1079 ret = intel_init_pipe_control(ring);
1080 if (ret)
1081 return ret;
1082
1083 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1084
Michel Thierry771b9a52014-11-11 16:47:33 +00001085 return init_workarounds_ring(ring);
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001086}
1087
Oscar Mateo15648582014-07-24 17:04:32 +01001088static int gen8_emit_bb_start(struct intel_ringbuffer *ringbuf,
1089 u64 offset, unsigned flags)
1090{
Oscar Mateo15648582014-07-24 17:04:32 +01001091 bool ppgtt = !(flags & I915_DISPATCH_SECURE);
1092 int ret;
1093
1094 ret = intel_logical_ring_begin(ringbuf, 4);
1095 if (ret)
1096 return ret;
1097
1098 /* FIXME(BDW): Address space and security selectors. */
1099 intel_logical_ring_emit(ringbuf, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8));
1100 intel_logical_ring_emit(ringbuf, lower_32_bits(offset));
1101 intel_logical_ring_emit(ringbuf, upper_32_bits(offset));
1102 intel_logical_ring_emit(ringbuf, MI_NOOP);
1103 intel_logical_ring_advance(ringbuf);
1104
1105 return 0;
1106}
1107
Oscar Mateo73d477f2014-07-24 17:04:31 +01001108static bool gen8_logical_ring_get_irq(struct intel_engine_cs *ring)
1109{
1110 struct drm_device *dev = ring->dev;
1111 struct drm_i915_private *dev_priv = dev->dev_private;
1112 unsigned long flags;
1113
Daniel Vetter7cd512f2014-09-15 11:38:57 +02001114 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
Oscar Mateo73d477f2014-07-24 17:04:31 +01001115 return false;
1116
1117 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1118 if (ring->irq_refcount++ == 0) {
1119 I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
1120 POSTING_READ(RING_IMR(ring->mmio_base));
1121 }
1122 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1123
1124 return true;
1125}
1126
1127static void gen8_logical_ring_put_irq(struct intel_engine_cs *ring)
1128{
1129 struct drm_device *dev = ring->dev;
1130 struct drm_i915_private *dev_priv = dev->dev_private;
1131 unsigned long flags;
1132
1133 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1134 if (--ring->irq_refcount == 0) {
1135 I915_WRITE_IMR(ring, ~ring->irq_keep_mask);
1136 POSTING_READ(RING_IMR(ring->mmio_base));
1137 }
1138 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1139}
1140
Oscar Mateo47122742014-07-24 17:04:28 +01001141static int gen8_emit_flush(struct intel_ringbuffer *ringbuf,
1142 u32 invalidate_domains,
1143 u32 unused)
1144{
1145 struct intel_engine_cs *ring = ringbuf->ring;
1146 struct drm_device *dev = ring->dev;
1147 struct drm_i915_private *dev_priv = dev->dev_private;
1148 uint32_t cmd;
1149 int ret;
1150
1151 ret = intel_logical_ring_begin(ringbuf, 4);
1152 if (ret)
1153 return ret;
1154
1155 cmd = MI_FLUSH_DW + 1;
1156
1157 if (ring == &dev_priv->ring[VCS]) {
1158 if (invalidate_domains & I915_GEM_GPU_DOMAINS)
1159 cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD |
1160 MI_FLUSH_DW_STORE_INDEX |
1161 MI_FLUSH_DW_OP_STOREDW;
1162 } else {
1163 if (invalidate_domains & I915_GEM_DOMAIN_RENDER)
1164 cmd |= MI_INVALIDATE_TLB | MI_FLUSH_DW_STORE_INDEX |
1165 MI_FLUSH_DW_OP_STOREDW;
1166 }
1167
1168 intel_logical_ring_emit(ringbuf, cmd);
1169 intel_logical_ring_emit(ringbuf,
1170 I915_GEM_HWS_SCRATCH_ADDR |
1171 MI_FLUSH_DW_USE_GTT);
1172 intel_logical_ring_emit(ringbuf, 0); /* upper addr */
1173 intel_logical_ring_emit(ringbuf, 0); /* value */
1174 intel_logical_ring_advance(ringbuf);
1175
1176 return 0;
1177}
1178
1179static int gen8_emit_flush_render(struct intel_ringbuffer *ringbuf,
1180 u32 invalidate_domains,
1181 u32 flush_domains)
1182{
1183 struct intel_engine_cs *ring = ringbuf->ring;
1184 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
1185 u32 flags = 0;
1186 int ret;
1187
1188 flags |= PIPE_CONTROL_CS_STALL;
1189
1190 if (flush_domains) {
1191 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
1192 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
1193 }
1194
1195 if (invalidate_domains) {
1196 flags |= PIPE_CONTROL_TLB_INVALIDATE;
1197 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
1198 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
1199 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
1200 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
1201 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
1202 flags |= PIPE_CONTROL_QW_WRITE;
1203 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
1204 }
1205
1206 ret = intel_logical_ring_begin(ringbuf, 6);
1207 if (ret)
1208 return ret;
1209
1210 intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
1211 intel_logical_ring_emit(ringbuf, flags);
1212 intel_logical_ring_emit(ringbuf, scratch_addr);
1213 intel_logical_ring_emit(ringbuf, 0);
1214 intel_logical_ring_emit(ringbuf, 0);
1215 intel_logical_ring_emit(ringbuf, 0);
1216 intel_logical_ring_advance(ringbuf);
1217
1218 return 0;
1219}
1220
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001221static u32 gen8_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
1222{
1223 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1224}
1225
1226static void gen8_set_seqno(struct intel_engine_cs *ring, u32 seqno)
1227{
1228 intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
1229}
1230
Oscar Mateo4da46e12014-07-24 17:04:27 +01001231static int gen8_emit_request(struct intel_ringbuffer *ringbuf)
1232{
1233 struct intel_engine_cs *ring = ringbuf->ring;
1234 u32 cmd;
1235 int ret;
1236
1237 ret = intel_logical_ring_begin(ringbuf, 6);
1238 if (ret)
1239 return ret;
1240
1241 cmd = MI_STORE_DWORD_IMM_GEN8;
1242 cmd |= MI_GLOBAL_GTT;
1243
1244 intel_logical_ring_emit(ringbuf, cmd);
1245 intel_logical_ring_emit(ringbuf,
1246 (ring->status_page.gfx_addr +
1247 (I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT)));
1248 intel_logical_ring_emit(ringbuf, 0);
1249 intel_logical_ring_emit(ringbuf, ring->outstanding_lazy_seqno);
1250 intel_logical_ring_emit(ringbuf, MI_USER_INTERRUPT);
1251 intel_logical_ring_emit(ringbuf, MI_NOOP);
1252 intel_logical_ring_advance_and_submit(ringbuf);
1253
1254 return 0;
1255}
1256
Oscar Mateo73e4d072014-07-24 17:04:48 +01001257/**
1258 * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
1259 *
1260 * @ring: Engine Command Streamer.
1261 *
1262 */
Oscar Mateo454afeb2014-07-24 17:04:22 +01001263void intel_logical_ring_cleanup(struct intel_engine_cs *ring)
1264{
John Harrison6402c332014-10-31 12:00:26 +00001265 struct drm_i915_private *dev_priv;
Oscar Mateo9832b9d2014-07-24 17:04:30 +01001266
Oscar Mateo48d82382014-07-24 17:04:23 +01001267 if (!intel_ring_initialized(ring))
1268 return;
1269
John Harrison6402c332014-10-31 12:00:26 +00001270 dev_priv = ring->dev->dev_private;
1271
Oscar Mateo9832b9d2014-07-24 17:04:30 +01001272 intel_logical_ring_stop(ring);
1273 WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
Oscar Mateo48d82382014-07-24 17:04:23 +01001274 ring->preallocated_lazy_request = NULL;
1275 ring->outstanding_lazy_seqno = 0;
1276
1277 if (ring->cleanup)
1278 ring->cleanup(ring);
1279
1280 i915_cmd_parser_fini_ring(ring);
1281
1282 if (ring->status_page.obj) {
1283 kunmap(sg_page(ring->status_page.obj->pages->sgl));
1284 ring->status_page.obj = NULL;
1285 }
Oscar Mateo454afeb2014-07-24 17:04:22 +01001286}
1287
1288static int logical_ring_init(struct drm_device *dev, struct intel_engine_cs *ring)
1289{
Oscar Mateo48d82382014-07-24 17:04:23 +01001290 int ret;
Oscar Mateo48d82382014-07-24 17:04:23 +01001291
1292 /* Intentionally left blank. */
1293 ring->buffer = NULL;
1294
1295 ring->dev = dev;
1296 INIT_LIST_HEAD(&ring->active_list);
1297 INIT_LIST_HEAD(&ring->request_list);
1298 init_waitqueue_head(&ring->irq_queue);
1299
Michel Thierryacdd8842014-07-24 17:04:38 +01001300 INIT_LIST_HEAD(&ring->execlist_queue);
1301 spin_lock_init(&ring->execlist_lock);
Thomas Daniele981e7b2014-07-24 17:04:39 +01001302 ring->next_context_status_buffer = 0;
Michel Thierryacdd8842014-07-24 17:04:38 +01001303
Oscar Mateo48d82382014-07-24 17:04:23 +01001304 ret = i915_cmd_parser_init_ring(ring);
1305 if (ret)
1306 return ret;
1307
1308 if (ring->init) {
1309 ret = ring->init(ring);
1310 if (ret)
1311 return ret;
1312 }
1313
Oscar Mateo564ddb22014-08-21 11:40:54 +01001314 ret = intel_lr_context_deferred_create(ring->default_context, ring);
1315
1316 return ret;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001317}
1318
1319static int logical_render_ring_init(struct drm_device *dev)
1320{
1321 struct drm_i915_private *dev_priv = dev->dev_private;
1322 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
1323
1324 ring->name = "render ring";
1325 ring->id = RCS;
1326 ring->mmio_base = RENDER_RING_BASE;
1327 ring->irq_enable_mask =
1328 GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001329 ring->irq_keep_mask =
1330 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
1331 if (HAS_L3_DPF(dev))
1332 ring->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001333
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001334 ring->init = gen8_init_render_ring;
Michel Thierry771b9a52014-11-11 16:47:33 +00001335 ring->init_context = intel_logical_ring_workarounds_emit;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001336 ring->cleanup = intel_fini_pipe_control;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001337 ring->get_seqno = gen8_get_seqno;
1338 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001339 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001340 ring->emit_flush = gen8_emit_flush_render;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001341 ring->irq_get = gen8_logical_ring_get_irq;
1342 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001343 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001344
Oscar Mateo454afeb2014-07-24 17:04:22 +01001345 return logical_ring_init(dev, ring);
1346}
1347
1348static int logical_bsd_ring_init(struct drm_device *dev)
1349{
1350 struct drm_i915_private *dev_priv = dev->dev_private;
1351 struct intel_engine_cs *ring = &dev_priv->ring[VCS];
1352
1353 ring->name = "bsd ring";
1354 ring->id = VCS;
1355 ring->mmio_base = GEN6_BSD_RING_BASE;
1356 ring->irq_enable_mask =
1357 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001358 ring->irq_keep_mask =
1359 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001360
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001361 ring->init = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001362 ring->get_seqno = gen8_get_seqno;
1363 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001364 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001365 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001366 ring->irq_get = gen8_logical_ring_get_irq;
1367 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001368 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001369
Oscar Mateo454afeb2014-07-24 17:04:22 +01001370 return logical_ring_init(dev, ring);
1371}
1372
1373static int logical_bsd2_ring_init(struct drm_device *dev)
1374{
1375 struct drm_i915_private *dev_priv = dev->dev_private;
1376 struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
1377
1378 ring->name = "bds2 ring";
1379 ring->id = VCS2;
1380 ring->mmio_base = GEN8_BSD2_RING_BASE;
1381 ring->irq_enable_mask =
1382 GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001383 ring->irq_keep_mask =
1384 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001385
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001386 ring->init = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001387 ring->get_seqno = gen8_get_seqno;
1388 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001389 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001390 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001391 ring->irq_get = gen8_logical_ring_get_irq;
1392 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001393 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001394
Oscar Mateo454afeb2014-07-24 17:04:22 +01001395 return logical_ring_init(dev, ring);
1396}
1397
1398static int logical_blt_ring_init(struct drm_device *dev)
1399{
1400 struct drm_i915_private *dev_priv = dev->dev_private;
1401 struct intel_engine_cs *ring = &dev_priv->ring[BCS];
1402
1403 ring->name = "blitter ring";
1404 ring->id = BCS;
1405 ring->mmio_base = BLT_RING_BASE;
1406 ring->irq_enable_mask =
1407 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001408 ring->irq_keep_mask =
1409 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001410
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001411 ring->init = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001412 ring->get_seqno = gen8_get_seqno;
1413 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001414 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001415 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001416 ring->irq_get = gen8_logical_ring_get_irq;
1417 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001418 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001419
Oscar Mateo454afeb2014-07-24 17:04:22 +01001420 return logical_ring_init(dev, ring);
1421}
1422
1423static int logical_vebox_ring_init(struct drm_device *dev)
1424{
1425 struct drm_i915_private *dev_priv = dev->dev_private;
1426 struct intel_engine_cs *ring = &dev_priv->ring[VECS];
1427
1428 ring->name = "video enhancement ring";
1429 ring->id = VECS;
1430 ring->mmio_base = VEBOX_RING_BASE;
1431 ring->irq_enable_mask =
1432 GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001433 ring->irq_keep_mask =
1434 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
Oscar Mateo454afeb2014-07-24 17:04:22 +01001435
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001436 ring->init = gen8_init_common_ring;
Oscar Mateoe94e37a2014-07-24 17:04:25 +01001437 ring->get_seqno = gen8_get_seqno;
1438 ring->set_seqno = gen8_set_seqno;
Oscar Mateo4da46e12014-07-24 17:04:27 +01001439 ring->emit_request = gen8_emit_request;
Oscar Mateo47122742014-07-24 17:04:28 +01001440 ring->emit_flush = gen8_emit_flush;
Oscar Mateo73d477f2014-07-24 17:04:31 +01001441 ring->irq_get = gen8_logical_ring_get_irq;
1442 ring->irq_put = gen8_logical_ring_put_irq;
Oscar Mateo15648582014-07-24 17:04:32 +01001443 ring->emit_bb_start = gen8_emit_bb_start;
Oscar Mateo9b1136d2014-07-24 17:04:24 +01001444
Oscar Mateo454afeb2014-07-24 17:04:22 +01001445 return logical_ring_init(dev, ring);
1446}
1447
Oscar Mateo73e4d072014-07-24 17:04:48 +01001448/**
1449 * intel_logical_rings_init() - allocate, populate and init the Engine Command Streamers
1450 * @dev: DRM device.
1451 *
1452 * This function inits the engines for an Execlists submission style (the equivalent in the
1453 * legacy ringbuffer submission world would be i915_gem_init_rings). It does it only for
1454 * those engines that are present in the hardware.
1455 *
1456 * Return: non-zero if the initialization failed.
1457 */
Oscar Mateo454afeb2014-07-24 17:04:22 +01001458int intel_logical_rings_init(struct drm_device *dev)
1459{
1460 struct drm_i915_private *dev_priv = dev->dev_private;
1461 int ret;
1462
1463 ret = logical_render_ring_init(dev);
1464 if (ret)
1465 return ret;
1466
1467 if (HAS_BSD(dev)) {
1468 ret = logical_bsd_ring_init(dev);
1469 if (ret)
1470 goto cleanup_render_ring;
1471 }
1472
1473 if (HAS_BLT(dev)) {
1474 ret = logical_blt_ring_init(dev);
1475 if (ret)
1476 goto cleanup_bsd_ring;
1477 }
1478
1479 if (HAS_VEBOX(dev)) {
1480 ret = logical_vebox_ring_init(dev);
1481 if (ret)
1482 goto cleanup_blt_ring;
1483 }
1484
1485 if (HAS_BSD2(dev)) {
1486 ret = logical_bsd2_ring_init(dev);
1487 if (ret)
1488 goto cleanup_vebox_ring;
1489 }
1490
1491 ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
1492 if (ret)
1493 goto cleanup_bsd2_ring;
1494
1495 return 0;
1496
1497cleanup_bsd2_ring:
1498 intel_logical_ring_cleanup(&dev_priv->ring[VCS2]);
1499cleanup_vebox_ring:
1500 intel_logical_ring_cleanup(&dev_priv->ring[VECS]);
1501cleanup_blt_ring:
1502 intel_logical_ring_cleanup(&dev_priv->ring[BCS]);
1503cleanup_bsd_ring:
1504 intel_logical_ring_cleanup(&dev_priv->ring[VCS]);
1505cleanup_render_ring:
1506 intel_logical_ring_cleanup(&dev_priv->ring[RCS]);
1507
1508 return ret;
1509}
1510
Oscar Mateo564ddb22014-08-21 11:40:54 +01001511int intel_lr_context_render_state_init(struct intel_engine_cs *ring,
1512 struct intel_context *ctx)
1513{
1514 struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
1515 struct render_state so;
1516 struct drm_i915_file_private *file_priv = ctx->file_priv;
1517 struct drm_file *file = file_priv ? file_priv->file : NULL;
1518 int ret;
1519
1520 ret = i915_gem_render_state_prepare(ring, &so);
1521 if (ret)
1522 return ret;
1523
1524 if (so.rodata == NULL)
1525 return 0;
1526
1527 ret = ring->emit_bb_start(ringbuf,
1528 so.ggtt_offset,
1529 I915_DISPATCH_SECURE);
1530 if (ret)
1531 goto out;
1532
1533 i915_vma_move_to_active(i915_gem_obj_to_ggtt(so.obj), ring);
1534
1535 ret = __i915_add_request(ring, file, so.obj, NULL);
1536 /* intel_logical_ring_add_request moves object to inactive if it
1537 * fails */
1538out:
1539 i915_gem_render_state_fini(&so);
1540 return ret;
1541}
1542
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001543static int
1544populate_lr_context(struct intel_context *ctx, struct drm_i915_gem_object *ctx_obj,
1545 struct intel_engine_cs *ring, struct intel_ringbuffer *ringbuf)
1546{
Thomas Daniel2d965532014-08-19 10:13:36 +01001547 struct drm_device *dev = ring->dev;
1548 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001549 struct drm_i915_gem_object *ring_obj = ringbuf->obj;
Daniel Vetterae6c4802014-08-06 15:04:53 +02001550 struct i915_hw_ppgtt *ppgtt = ctx->ppgtt;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001551 struct page *page;
1552 uint32_t *reg_state;
1553 int ret;
1554
Thomas Daniel2d965532014-08-19 10:13:36 +01001555 if (!ppgtt)
1556 ppgtt = dev_priv->mm.aliasing_ppgtt;
1557
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001558 ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
1559 if (ret) {
1560 DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
1561 return ret;
1562 }
1563
1564 ret = i915_gem_object_get_pages(ctx_obj);
1565 if (ret) {
1566 DRM_DEBUG_DRIVER("Could not get object pages\n");
1567 return ret;
1568 }
1569
1570 i915_gem_object_pin_pages(ctx_obj);
1571
1572 /* The second page of the context object contains some fields which must
1573 * be set up prior to the first execution. */
1574 page = i915_gem_object_get_page(ctx_obj, 1);
1575 reg_state = kmap_atomic(page);
1576
1577 /* A context is actually a big batch buffer with several MI_LOAD_REGISTER_IMM
1578 * commands followed by (reg, value) pairs. The values we are setting here are
1579 * only for the first context restore: on a subsequent save, the GPU will
1580 * recreate this batchbuffer with new values (including all the missing
1581 * MI_LOAD_REGISTER_IMM commands that we are not initializing here). */
1582 if (ring->id == RCS)
1583 reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(14);
1584 else
1585 reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(11);
1586 reg_state[CTX_LRI_HEADER_0] |= MI_LRI_FORCE_POSTED;
1587 reg_state[CTX_CONTEXT_CONTROL] = RING_CONTEXT_CONTROL(ring);
1588 reg_state[CTX_CONTEXT_CONTROL+1] =
1589 _MASKED_BIT_ENABLE((1<<3) | MI_RESTORE_INHIBIT);
1590 reg_state[CTX_RING_HEAD] = RING_HEAD(ring->mmio_base);
1591 reg_state[CTX_RING_HEAD+1] = 0;
1592 reg_state[CTX_RING_TAIL] = RING_TAIL(ring->mmio_base);
1593 reg_state[CTX_RING_TAIL+1] = 0;
1594 reg_state[CTX_RING_BUFFER_START] = RING_START(ring->mmio_base);
1595 reg_state[CTX_RING_BUFFER_START+1] = i915_gem_obj_ggtt_offset(ring_obj);
1596 reg_state[CTX_RING_BUFFER_CONTROL] = RING_CTL(ring->mmio_base);
1597 reg_state[CTX_RING_BUFFER_CONTROL+1] =
1598 ((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES) | RING_VALID;
1599 reg_state[CTX_BB_HEAD_U] = ring->mmio_base + 0x168;
1600 reg_state[CTX_BB_HEAD_U+1] = 0;
1601 reg_state[CTX_BB_HEAD_L] = ring->mmio_base + 0x140;
1602 reg_state[CTX_BB_HEAD_L+1] = 0;
1603 reg_state[CTX_BB_STATE] = ring->mmio_base + 0x110;
1604 reg_state[CTX_BB_STATE+1] = (1<<5);
1605 reg_state[CTX_SECOND_BB_HEAD_U] = ring->mmio_base + 0x11c;
1606 reg_state[CTX_SECOND_BB_HEAD_U+1] = 0;
1607 reg_state[CTX_SECOND_BB_HEAD_L] = ring->mmio_base + 0x114;
1608 reg_state[CTX_SECOND_BB_HEAD_L+1] = 0;
1609 reg_state[CTX_SECOND_BB_STATE] = ring->mmio_base + 0x118;
1610 reg_state[CTX_SECOND_BB_STATE+1] = 0;
1611 if (ring->id == RCS) {
1612 /* TODO: according to BSpec, the register state context
1613 * for CHV does not have these. OTOH, these registers do
1614 * exist in CHV. I'm waiting for a clarification */
1615 reg_state[CTX_BB_PER_CTX_PTR] = ring->mmio_base + 0x1c0;
1616 reg_state[CTX_BB_PER_CTX_PTR+1] = 0;
1617 reg_state[CTX_RCS_INDIRECT_CTX] = ring->mmio_base + 0x1c4;
1618 reg_state[CTX_RCS_INDIRECT_CTX+1] = 0;
1619 reg_state[CTX_RCS_INDIRECT_CTX_OFFSET] = ring->mmio_base + 0x1c8;
1620 reg_state[CTX_RCS_INDIRECT_CTX_OFFSET+1] = 0;
1621 }
1622 reg_state[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9);
1623 reg_state[CTX_LRI_HEADER_1] |= MI_LRI_FORCE_POSTED;
1624 reg_state[CTX_CTX_TIMESTAMP] = ring->mmio_base + 0x3a8;
1625 reg_state[CTX_CTX_TIMESTAMP+1] = 0;
1626 reg_state[CTX_PDP3_UDW] = GEN8_RING_PDP_UDW(ring, 3);
1627 reg_state[CTX_PDP3_LDW] = GEN8_RING_PDP_LDW(ring, 3);
1628 reg_state[CTX_PDP2_UDW] = GEN8_RING_PDP_UDW(ring, 2);
1629 reg_state[CTX_PDP2_LDW] = GEN8_RING_PDP_LDW(ring, 2);
1630 reg_state[CTX_PDP1_UDW] = GEN8_RING_PDP_UDW(ring, 1);
1631 reg_state[CTX_PDP1_LDW] = GEN8_RING_PDP_LDW(ring, 1);
1632 reg_state[CTX_PDP0_UDW] = GEN8_RING_PDP_UDW(ring, 0);
1633 reg_state[CTX_PDP0_LDW] = GEN8_RING_PDP_LDW(ring, 0);
1634 reg_state[CTX_PDP3_UDW+1] = upper_32_bits(ppgtt->pd_dma_addr[3]);
1635 reg_state[CTX_PDP3_LDW+1] = lower_32_bits(ppgtt->pd_dma_addr[3]);
1636 reg_state[CTX_PDP2_UDW+1] = upper_32_bits(ppgtt->pd_dma_addr[2]);
1637 reg_state[CTX_PDP2_LDW+1] = lower_32_bits(ppgtt->pd_dma_addr[2]);
1638 reg_state[CTX_PDP1_UDW+1] = upper_32_bits(ppgtt->pd_dma_addr[1]);
1639 reg_state[CTX_PDP1_LDW+1] = lower_32_bits(ppgtt->pd_dma_addr[1]);
1640 reg_state[CTX_PDP0_UDW+1] = upper_32_bits(ppgtt->pd_dma_addr[0]);
1641 reg_state[CTX_PDP0_LDW+1] = lower_32_bits(ppgtt->pd_dma_addr[0]);
1642 if (ring->id == RCS) {
1643 reg_state[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
1644 reg_state[CTX_R_PWR_CLK_STATE] = 0x20c8;
1645 reg_state[CTX_R_PWR_CLK_STATE+1] = 0;
1646 }
1647
1648 kunmap_atomic(reg_state);
1649
1650 ctx_obj->dirty = 1;
1651 set_page_dirty(page);
1652 i915_gem_object_unpin_pages(ctx_obj);
1653
1654 return 0;
1655}
1656
Oscar Mateo73e4d072014-07-24 17:04:48 +01001657/**
1658 * intel_lr_context_free() - free the LRC specific bits of a context
1659 * @ctx: the LR context to free.
1660 *
1661 * The real context freeing is done in i915_gem_context_free: this only
1662 * takes care of the bits that are LRC related: the per-engine backing
1663 * objects and the logical ringbuffer.
1664 */
Oscar Mateoede7d422014-07-24 17:04:12 +01001665void intel_lr_context_free(struct intel_context *ctx)
1666{
Oscar Mateo8c8579172014-07-24 17:04:14 +01001667 int i;
1668
1669 for (i = 0; i < I915_NUM_RINGS; i++) {
1670 struct drm_i915_gem_object *ctx_obj = ctx->engine[i].state;
Oscar Mateo84c23772014-07-24 17:04:15 +01001671 struct intel_ringbuffer *ringbuf = ctx->engine[i].ringbuf;
1672
Oscar Mateo8c8579172014-07-24 17:04:14 +01001673 if (ctx_obj) {
Oscar Mateo84c23772014-07-24 17:04:15 +01001674 intel_destroy_ringbuffer_obj(ringbuf);
1675 kfree(ringbuf);
Oscar Mateo8c8579172014-07-24 17:04:14 +01001676 i915_gem_object_ggtt_unpin(ctx_obj);
1677 drm_gem_object_unreference(&ctx_obj->base);
1678 }
1679 }
1680}
1681
1682static uint32_t get_lr_context_size(struct intel_engine_cs *ring)
1683{
1684 int ret = 0;
1685
Michael H. Nguyen468c6812014-11-13 17:51:49 +00001686 WARN_ON(INTEL_INFO(ring->dev)->gen < 8);
Oscar Mateo8c8579172014-07-24 17:04:14 +01001687
1688 switch (ring->id) {
1689 case RCS:
Michael H. Nguyen468c6812014-11-13 17:51:49 +00001690 if (INTEL_INFO(ring->dev)->gen >= 9)
1691 ret = GEN9_LR_CONTEXT_RENDER_SIZE;
1692 else
1693 ret = GEN8_LR_CONTEXT_RENDER_SIZE;
Oscar Mateo8c8579172014-07-24 17:04:14 +01001694 break;
1695 case VCS:
1696 case BCS:
1697 case VECS:
1698 case VCS2:
1699 ret = GEN8_LR_CONTEXT_OTHER_SIZE;
1700 break;
1701 }
1702
1703 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01001704}
1705
Thomas Daniel1df06b72014-10-29 09:52:51 +00001706static int lrc_setup_hardware_status_page(struct intel_engine_cs *ring,
1707 struct drm_i915_gem_object *default_ctx_obj)
1708{
1709 struct drm_i915_private *dev_priv = ring->dev->dev_private;
1710
1711 /* The status page is offset 0 from the default context object
1712 * in LRC mode. */
1713 ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(default_ctx_obj);
1714 ring->status_page.page_addr =
1715 kmap(sg_page(default_ctx_obj->pages->sgl));
1716 if (ring->status_page.page_addr == NULL)
1717 return -ENOMEM;
1718 ring->status_page.obj = default_ctx_obj;
1719
1720 I915_WRITE(RING_HWS_PGA(ring->mmio_base),
1721 (u32)ring->status_page.gfx_addr);
1722 POSTING_READ(RING_HWS_PGA(ring->mmio_base));
1723
1724 return 0;
1725}
1726
Oscar Mateo73e4d072014-07-24 17:04:48 +01001727/**
1728 * intel_lr_context_deferred_create() - create the LRC specific bits of a context
1729 * @ctx: LR context to create.
1730 * @ring: engine to be used with the context.
1731 *
1732 * This function can be called more than once, with different engines, if we plan
1733 * to use the context with them. The context backing objects and the ringbuffers
1734 * (specially the ringbuffer backing objects) suck a lot of memory up, and that's why
1735 * the creation is a deferred call: it's better to make sure first that we need to use
1736 * a given ring with the context.
1737 *
Masanari Iida32197aa2014-10-20 23:53:13 +09001738 * Return: non-zero on error.
Oscar Mateo73e4d072014-07-24 17:04:48 +01001739 */
Oscar Mateoede7d422014-07-24 17:04:12 +01001740int intel_lr_context_deferred_create(struct intel_context *ctx,
1741 struct intel_engine_cs *ring)
1742{
Oscar Mateo8c8579172014-07-24 17:04:14 +01001743 struct drm_device *dev = ring->dev;
1744 struct drm_i915_gem_object *ctx_obj;
1745 uint32_t context_size;
Oscar Mateo84c23772014-07-24 17:04:15 +01001746 struct intel_ringbuffer *ringbuf;
Oscar Mateo8c8579172014-07-24 17:04:14 +01001747 int ret;
1748
Oscar Mateoede7d422014-07-24 17:04:12 +01001749 WARN_ON(ctx->legacy_hw_ctx.rcs_state != NULL);
Oscar Mateo48d82382014-07-24 17:04:23 +01001750 if (ctx->engine[ring->id].state)
1751 return 0;
Oscar Mateoede7d422014-07-24 17:04:12 +01001752
Oscar Mateo8c8579172014-07-24 17:04:14 +01001753 context_size = round_up(get_lr_context_size(ring), 4096);
1754
1755 ctx_obj = i915_gem_alloc_context_obj(dev, context_size);
1756 if (IS_ERR(ctx_obj)) {
1757 ret = PTR_ERR(ctx_obj);
1758 DRM_DEBUG_DRIVER("Alloc LRC backing obj failed: %d\n", ret);
1759 return ret;
1760 }
1761
1762 ret = i915_gem_obj_ggtt_pin(ctx_obj, GEN8_LR_CONTEXT_ALIGN, 0);
1763 if (ret) {
1764 DRM_DEBUG_DRIVER("Pin LRC backing obj failed: %d\n", ret);
1765 drm_gem_object_unreference(&ctx_obj->base);
1766 return ret;
1767 }
1768
Oscar Mateo84c23772014-07-24 17:04:15 +01001769 ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
1770 if (!ringbuf) {
1771 DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s\n",
1772 ring->name);
1773 i915_gem_object_ggtt_unpin(ctx_obj);
1774 drm_gem_object_unreference(&ctx_obj->base);
1775 ret = -ENOMEM;
1776 return ret;
1777 }
1778
Daniel Vetter0c7dd532014-08-11 16:17:44 +02001779 ringbuf->ring = ring;
Oscar Mateo582d67f2014-07-24 17:04:16 +01001780 ringbuf->FIXME_lrc_ctx = ctx;
1781
Oscar Mateo84c23772014-07-24 17:04:15 +01001782 ringbuf->size = 32 * PAGE_SIZE;
1783 ringbuf->effective_size = ringbuf->size;
1784 ringbuf->head = 0;
1785 ringbuf->tail = 0;
1786 ringbuf->space = ringbuf->size;
1787 ringbuf->last_retired_head = -1;
1788
1789 /* TODO: For now we put this in the mappable region so that we can reuse
1790 * the existing ringbuffer code which ioremaps it. When we start
1791 * creating many contexts, this will no longer work and we must switch
1792 * to a kmapish interface.
1793 */
1794 ret = intel_alloc_ringbuffer_obj(dev, ringbuf);
1795 if (ret) {
1796 DRM_DEBUG_DRIVER("Failed to allocate ringbuffer obj %s: %d\n",
1797 ring->name, ret);
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001798 goto error;
1799 }
1800
1801 ret = populate_lr_context(ctx, ctx_obj, ring, ringbuf);
1802 if (ret) {
1803 DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
1804 intel_destroy_ringbuffer_obj(ringbuf);
1805 goto error;
Oscar Mateo84c23772014-07-24 17:04:15 +01001806 }
1807
1808 ctx->engine[ring->id].ringbuf = ringbuf;
Oscar Mateo8c8579172014-07-24 17:04:14 +01001809 ctx->engine[ring->id].state = ctx_obj;
Oscar Mateoede7d422014-07-24 17:04:12 +01001810
Oscar Mateo564ddb22014-08-21 11:40:54 +01001811 if (ctx == ring->default_context) {
Thomas Daniel1df06b72014-10-29 09:52:51 +00001812 ret = lrc_setup_hardware_status_page(ring, ctx_obj);
1813 if (ret) {
1814 DRM_ERROR("Failed to setup hardware status page\n");
1815 goto error;
1816 }
Oscar Mateo564ddb22014-08-21 11:40:54 +01001817 }
1818
1819 if (ring->id == RCS && !ctx->rcs_initialized) {
Michel Thierry771b9a52014-11-11 16:47:33 +00001820 if (ring->init_context) {
1821 ret = ring->init_context(ring, ctx);
1822 if (ret)
1823 DRM_ERROR("ring init context: %d\n", ret);
1824 }
1825
Oscar Mateo564ddb22014-08-21 11:40:54 +01001826 ret = intel_lr_context_render_state_init(ring, ctx);
1827 if (ret) {
1828 DRM_ERROR("Init render state failed: %d\n", ret);
1829 ctx->engine[ring->id].ringbuf = NULL;
1830 ctx->engine[ring->id].state = NULL;
1831 intel_destroy_ringbuffer_obj(ringbuf);
1832 goto error;
1833 }
1834 ctx->rcs_initialized = true;
1835 }
1836
Oscar Mateoede7d422014-07-24 17:04:12 +01001837 return 0;
Oscar Mateo8670d6f2014-07-24 17:04:17 +01001838
1839error:
1840 kfree(ringbuf);
1841 i915_gem_object_ggtt_unpin(ctx_obj);
1842 drm_gem_object_unreference(&ctx_obj->base);
1843 return ret;
Oscar Mateoede7d422014-07-24 17:04:12 +01001844}