drm/i915: Do kunmap if renderstate parsing fails
[deliverable/linux.git] / drivers / gpu / drm / i915 / intel_lrc.c
CommitLineData
b20385f1
OM
1/*
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
73e4d07f
OM
31/**
32 * DOC: Logical Rings, Logical Ring Contexts and Execlists
33 *
34 * Motivation:
b20385f1
OM
35 * 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 *
73e4d07f
OM
39 * 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:
b20385f1
OM
90 * Execlists are the new method by which, on gen8+ hardware, workloads are
91 * submitted for execution (as opposed to the legacy, ringbuffer-based, method).
73e4d07f
OM
92 * 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 *
b20385f1
OM
133 */
134
135#include <drm/drmP.h>
136#include <drm/i915_drm.h>
137#include "i915_drv.h"
3bbaba0c 138#include "intel_mocs.h"
127f1003 139
468c6816 140#define GEN9_LR_CONTEXT_RENDER_SIZE (22 * PAGE_SIZE)
8c857917
OM
141#define GEN8_LR_CONTEXT_RENDER_SIZE (20 * PAGE_SIZE)
142#define GEN8_LR_CONTEXT_OTHER_SIZE (2 * PAGE_SIZE)
143
e981e7b1
TD
144#define RING_EXECLIST_QFULL (1 << 0x2)
145#define RING_EXECLIST1_VALID (1 << 0x3)
146#define RING_EXECLIST0_VALID (1 << 0x4)
147#define RING_EXECLIST_ACTIVE_STATUS (3 << 0xE)
148#define RING_EXECLIST1_ACTIVE (1 << 0x11)
149#define RING_EXECLIST0_ACTIVE (1 << 0x12)
150
151#define GEN8_CTX_STATUS_IDLE_ACTIVE (1 << 0)
152#define GEN8_CTX_STATUS_PREEMPTED (1 << 1)
153#define GEN8_CTX_STATUS_ELEMENT_SWITCH (1 << 2)
154#define GEN8_CTX_STATUS_ACTIVE_IDLE (1 << 3)
155#define GEN8_CTX_STATUS_COMPLETE (1 << 4)
156#define GEN8_CTX_STATUS_LITE_RESTORE (1 << 15)
8670d6f9
OM
157
158#define CTX_LRI_HEADER_0 0x01
159#define CTX_CONTEXT_CONTROL 0x02
160#define CTX_RING_HEAD 0x04
161#define CTX_RING_TAIL 0x06
162#define CTX_RING_BUFFER_START 0x08
163#define CTX_RING_BUFFER_CONTROL 0x0a
164#define CTX_BB_HEAD_U 0x0c
165#define CTX_BB_HEAD_L 0x0e
166#define CTX_BB_STATE 0x10
167#define CTX_SECOND_BB_HEAD_U 0x12
168#define CTX_SECOND_BB_HEAD_L 0x14
169#define CTX_SECOND_BB_STATE 0x16
170#define CTX_BB_PER_CTX_PTR 0x18
171#define CTX_RCS_INDIRECT_CTX 0x1a
172#define CTX_RCS_INDIRECT_CTX_OFFSET 0x1c
173#define CTX_LRI_HEADER_1 0x21
174#define CTX_CTX_TIMESTAMP 0x22
175#define CTX_PDP3_UDW 0x24
176#define CTX_PDP3_LDW 0x26
177#define CTX_PDP2_UDW 0x28
178#define CTX_PDP2_LDW 0x2a
179#define CTX_PDP1_UDW 0x2c
180#define CTX_PDP1_LDW 0x2e
181#define CTX_PDP0_UDW 0x30
182#define CTX_PDP0_LDW 0x32
183#define CTX_LRI_HEADER_2 0x41
184#define CTX_R_PWR_CLK_STATE 0x42
185#define CTX_GPGPU_CSR_BASE_ADDRESS 0x44
186
84b790f8
BW
187#define GEN8_CTX_VALID (1<<0)
188#define GEN8_CTX_FORCE_PD_RESTORE (1<<1)
189#define GEN8_CTX_FORCE_RESTORE (1<<2)
190#define GEN8_CTX_L3LLC_COHERENT (1<<5)
191#define GEN8_CTX_PRIVILEGE (1<<8)
e5815a2e
MT
192
193#define ASSIGN_CTX_PDP(ppgtt, reg_state, n) { \
d852c7bf 194 const u64 _addr = i915_page_dir_dma_addr((ppgtt), (n)); \
e5815a2e
MT
195 reg_state[CTX_PDP ## n ## _UDW+1] = upper_32_bits(_addr); \
196 reg_state[CTX_PDP ## n ## _LDW+1] = lower_32_bits(_addr); \
197}
198
84b790f8
BW
199enum {
200 ADVANCED_CONTEXT = 0,
201 LEGACY_CONTEXT,
202 ADVANCED_AD_CONTEXT,
203 LEGACY_64B_CONTEXT
204};
205#define GEN8_CTX_MODE_SHIFT 3
206enum {
207 FAULT_AND_HANG = 0,
208 FAULT_AND_HALT, /* Debug only */
209 FAULT_AND_STREAM,
210 FAULT_AND_CONTINUE /* Unsupported */
211};
212#define GEN8_CTX_ID_SHIFT 32
17ee950d 213#define CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x17
84b790f8 214
8ba319da 215static int intel_lr_context_pin(struct drm_i915_gem_request *rq);
7ba717cf 216
73e4d07f
OM
217/**
218 * intel_sanitize_enable_execlists() - sanitize i915.enable_execlists
219 * @dev: DRM device.
220 * @enable_execlists: value of i915.enable_execlists module parameter.
221 *
222 * Only certain platforms support Execlists (the prerequisites being
27401d12 223 * support for Logical Ring Contexts and Aliasing PPGTT or better).
73e4d07f
OM
224 *
225 * Return: 1 if Execlists is supported and has to be enabled.
226 */
127f1003
OM
227int intel_sanitize_enable_execlists(struct drm_device *dev, int enable_execlists)
228{
bd84b1e9
DV
229 WARN_ON(i915.enable_ppgtt == -1);
230
70ee45e1
DL
231 if (INTEL_INFO(dev)->gen >= 9)
232 return 1;
233
127f1003
OM
234 if (enable_execlists == 0)
235 return 0;
236
14bf993e
OM
237 if (HAS_LOGICAL_RING_CONTEXTS(dev) && USES_PPGTT(dev) &&
238 i915.use_mmio_flip >= 0)
127f1003
OM
239 return 1;
240
241 return 0;
242}
ede7d42b 243
73e4d07f
OM
244/**
245 * intel_execlists_ctx_id() - get the Execlists Context ID
246 * @ctx_obj: Logical Ring Context backing object.
247 *
248 * Do not confuse with ctx->id! Unfortunately we have a name overload
249 * here: the old context ID we pass to userspace as a handler so that
250 * they can refer to a context, and the new context ID we pass to the
251 * ELSP so that the GPU can inform us of the context status via
252 * interrupts.
253 *
254 * Return: 20-bits globally unique context ID.
255 */
84b790f8
BW
256u32 intel_execlists_ctx_id(struct drm_i915_gem_object *ctx_obj)
257{
258 u32 lrca = i915_gem_obj_ggtt_offset(ctx_obj);
259
260 /* LRCA is required to be 4K aligned so the more significant 20 bits
261 * are globally unique */
262 return lrca >> 12;
263}
264
8ee36152 265static uint64_t execlists_ctx_descriptor(struct drm_i915_gem_request *rq)
84b790f8 266{
8ee36152 267 struct intel_engine_cs *ring = rq->ring;
203a571b 268 struct drm_device *dev = ring->dev;
8ee36152 269 struct drm_i915_gem_object *ctx_obj = rq->ctx->engine[ring->id].state;
84b790f8
BW
270 uint64_t desc;
271 uint64_t lrca = i915_gem_obj_ggtt_offset(ctx_obj);
acdd884a
MT
272
273 WARN_ON(lrca & 0xFFFFFFFF00000FFFULL);
84b790f8
BW
274
275 desc = GEN8_CTX_VALID;
276 desc |= LEGACY_CONTEXT << GEN8_CTX_MODE_SHIFT;
51847fb9
AS
277 if (IS_GEN8(ctx_obj->base.dev))
278 desc |= GEN8_CTX_L3LLC_COHERENT;
84b790f8
BW
279 desc |= GEN8_CTX_PRIVILEGE;
280 desc |= lrca;
281 desc |= (u64)intel_execlists_ctx_id(ctx_obj) << GEN8_CTX_ID_SHIFT;
282
283 /* TODO: WaDisableLiteRestore when we start using semaphore
284 * signalling between Command Streamers */
285 /* desc |= GEN8_CTX_FORCE_RESTORE; */
286
203a571b
NH
287 /* WaEnableForceRestoreInCtxtDescForVCS:skl */
288 if (IS_GEN9(dev) &&
289 INTEL_REVID(dev) <= SKL_REVID_B0 &&
290 (ring->id == BCS || ring->id == VCS ||
291 ring->id == VECS || ring->id == VCS2))
292 desc |= GEN8_CTX_FORCE_RESTORE;
293
84b790f8
BW
294 return desc;
295}
296
cc3c4253
MK
297static void execlists_elsp_write(struct drm_i915_gem_request *rq0,
298 struct drm_i915_gem_request *rq1)
84b790f8 299{
cc3c4253
MK
300
301 struct intel_engine_cs *ring = rq0->ring;
6e7cc470
TU
302 struct drm_device *dev = ring->dev;
303 struct drm_i915_private *dev_priv = dev->dev_private;
1cff8cc3 304 uint64_t desc[2];
84b790f8 305
1cff8cc3
MK
306 if (rq1) {
307 desc[1] = execlists_ctx_descriptor(rq1);
308 rq1->elsp_submitted++;
309 } else {
310 desc[1] = 0;
311 }
84b790f8 312
1cff8cc3
MK
313 desc[0] = execlists_ctx_descriptor(rq0);
314 rq0->elsp_submitted++;
84b790f8 315
1cff8cc3 316 /* You must always write both descriptors in the order below. */
a6111f7b
CW
317 spin_lock(&dev_priv->uncore.lock);
318 intel_uncore_forcewake_get__locked(dev_priv, FORCEWAKE_ALL);
1cff8cc3
MK
319 I915_WRITE_FW(RING_ELSP(ring), upper_32_bits(desc[1]));
320 I915_WRITE_FW(RING_ELSP(ring), lower_32_bits(desc[1]));
6daccb0b 321
1cff8cc3 322 I915_WRITE_FW(RING_ELSP(ring), upper_32_bits(desc[0]));
84b790f8 323 /* The context is automatically loaded after the following */
1cff8cc3 324 I915_WRITE_FW(RING_ELSP(ring), lower_32_bits(desc[0]));
84b790f8 325
1cff8cc3 326 /* ELSP is a wo register, use another nearby reg for posting */
a6111f7b
CW
327 POSTING_READ_FW(RING_EXECLIST_STATUS(ring));
328 intel_uncore_forcewake_put__locked(dev_priv, FORCEWAKE_ALL);
329 spin_unlock(&dev_priv->uncore.lock);
84b790f8
BW
330}
331
05d9824b 332static int execlists_update_context(struct drm_i915_gem_request *rq)
ae1250b9 333{
05d9824b
MK
334 struct intel_engine_cs *ring = rq->ring;
335 struct i915_hw_ppgtt *ppgtt = rq->ctx->ppgtt;
336 struct drm_i915_gem_object *ctx_obj = rq->ctx->engine[ring->id].state;
337 struct drm_i915_gem_object *rb_obj = rq->ringbuf->obj;
ae1250b9
OM
338 struct page *page;
339 uint32_t *reg_state;
340
05d9824b
MK
341 BUG_ON(!ctx_obj);
342 WARN_ON(!i915_gem_obj_is_pinned(ctx_obj));
343 WARN_ON(!i915_gem_obj_is_pinned(rb_obj));
344
ae1250b9
OM
345 page = i915_gem_object_get_page(ctx_obj, 1);
346 reg_state = kmap_atomic(page);
347
05d9824b
MK
348 reg_state[CTX_RING_TAIL+1] = rq->tail;
349 reg_state[CTX_RING_BUFFER_START+1] = i915_gem_obj_ggtt_offset(rb_obj);
ae1250b9 350
d7b2633d
MT
351 /* True PPGTT with dynamic page allocation: update PDP registers and
352 * point the unallocated PDPs to the scratch page
353 */
354 if (ppgtt) {
355 ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
356 ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
357 ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
358 ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
359 }
360
ae1250b9
OM
361 kunmap_atomic(reg_state);
362
363 return 0;
364}
365
d8cb8875
MK
366static void execlists_submit_requests(struct drm_i915_gem_request *rq0,
367 struct drm_i915_gem_request *rq1)
84b790f8 368{
05d9824b 369 execlists_update_context(rq0);
d8cb8875 370
cc3c4253 371 if (rq1)
05d9824b 372 execlists_update_context(rq1);
84b790f8 373
cc3c4253 374 execlists_elsp_write(rq0, rq1);
84b790f8
BW
375}
376
acdd884a
MT
377static void execlists_context_unqueue(struct intel_engine_cs *ring)
378{
6d3d8274
NH
379 struct drm_i915_gem_request *req0 = NULL, *req1 = NULL;
380 struct drm_i915_gem_request *cursor = NULL, *tmp = NULL;
e981e7b1
TD
381
382 assert_spin_locked(&ring->execlist_lock);
acdd884a 383
779949f4
PA
384 /*
385 * If irqs are not active generate a warning as batches that finish
386 * without the irqs may get lost and a GPU Hang may occur.
387 */
388 WARN_ON(!intel_irqs_enabled(ring->dev->dev_private));
389
acdd884a
MT
390 if (list_empty(&ring->execlist_queue))
391 return;
392
393 /* Try to read in pairs */
394 list_for_each_entry_safe(cursor, tmp, &ring->execlist_queue,
395 execlist_link) {
396 if (!req0) {
397 req0 = cursor;
6d3d8274 398 } else if (req0->ctx == cursor->ctx) {
acdd884a
MT
399 /* Same ctx: ignore first request, as second request
400 * will update tail past first request's workload */
e1fee72c 401 cursor->elsp_submitted = req0->elsp_submitted;
acdd884a 402 list_del(&req0->execlist_link);
c86ee3a9
TD
403 list_add_tail(&req0->execlist_link,
404 &ring->execlist_retired_req_list);
acdd884a
MT
405 req0 = cursor;
406 } else {
407 req1 = cursor;
408 break;
409 }
410 }
411
53292cdb
MT
412 if (IS_GEN8(ring->dev) || IS_GEN9(ring->dev)) {
413 /*
414 * WaIdleLiteRestore: make sure we never cause a lite
415 * restore with HEAD==TAIL
416 */
d63f820f 417 if (req0->elsp_submitted) {
53292cdb
MT
418 /*
419 * Apply the wa NOOPS to prevent ring:HEAD == req:TAIL
420 * as we resubmit the request. See gen8_emit_request()
421 * for where we prepare the padding after the end of the
422 * request.
423 */
424 struct intel_ringbuffer *ringbuf;
425
426 ringbuf = req0->ctx->engine[ring->id].ringbuf;
427 req0->tail += 8;
428 req0->tail &= ringbuf->size - 1;
429 }
430 }
431
e1fee72c
OM
432 WARN_ON(req1 && req1->elsp_submitted);
433
d8cb8875 434 execlists_submit_requests(req0, req1);
acdd884a
MT
435}
436
e981e7b1
TD
437static bool execlists_check_remove_request(struct intel_engine_cs *ring,
438 u32 request_id)
439{
6d3d8274 440 struct drm_i915_gem_request *head_req;
e981e7b1
TD
441
442 assert_spin_locked(&ring->execlist_lock);
443
444 head_req = list_first_entry_or_null(&ring->execlist_queue,
6d3d8274 445 struct drm_i915_gem_request,
e981e7b1
TD
446 execlist_link);
447
448 if (head_req != NULL) {
449 struct drm_i915_gem_object *ctx_obj =
6d3d8274 450 head_req->ctx->engine[ring->id].state;
e981e7b1 451 if (intel_execlists_ctx_id(ctx_obj) == request_id) {
e1fee72c
OM
452 WARN(head_req->elsp_submitted == 0,
453 "Never submitted head request\n");
454
455 if (--head_req->elsp_submitted <= 0) {
456 list_del(&head_req->execlist_link);
c86ee3a9
TD
457 list_add_tail(&head_req->execlist_link,
458 &ring->execlist_retired_req_list);
e1fee72c
OM
459 return true;
460 }
e981e7b1
TD
461 }
462 }
463
464 return false;
465}
466
73e4d07f 467/**
3f7531c3 468 * intel_lrc_irq_handler() - handle Context Switch interrupts
73e4d07f
OM
469 * @ring: Engine Command Streamer to handle.
470 *
471 * Check the unread Context Status Buffers and manage the submission of new
472 * contexts to the ELSP accordingly.
473 */
3f7531c3 474void intel_lrc_irq_handler(struct intel_engine_cs *ring)
e981e7b1
TD
475{
476 struct drm_i915_private *dev_priv = ring->dev->dev_private;
477 u32 status_pointer;
478 u8 read_pointer;
479 u8 write_pointer;
480 u32 status;
481 u32 status_id;
482 u32 submit_contexts = 0;
483
484 status_pointer = I915_READ(RING_CONTEXT_STATUS_PTR(ring));
485
486 read_pointer = ring->next_context_status_buffer;
487 write_pointer = status_pointer & 0x07;
488 if (read_pointer > write_pointer)
489 write_pointer += 6;
490
491 spin_lock(&ring->execlist_lock);
492
493 while (read_pointer < write_pointer) {
494 read_pointer++;
495 status = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
496 (read_pointer % 6) * 8);
497 status_id = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
498 (read_pointer % 6) * 8 + 4);
499
e1fee72c
OM
500 if (status & GEN8_CTX_STATUS_PREEMPTED) {
501 if (status & GEN8_CTX_STATUS_LITE_RESTORE) {
502 if (execlists_check_remove_request(ring, status_id))
503 WARN(1, "Lite Restored request removed from queue\n");
504 } else
505 WARN(1, "Preemption without Lite Restore\n");
506 }
507
508 if ((status & GEN8_CTX_STATUS_ACTIVE_IDLE) ||
509 (status & GEN8_CTX_STATUS_ELEMENT_SWITCH)) {
e981e7b1
TD
510 if (execlists_check_remove_request(ring, status_id))
511 submit_contexts++;
512 }
513 }
514
515 if (submit_contexts != 0)
516 execlists_context_unqueue(ring);
517
518 spin_unlock(&ring->execlist_lock);
519
520 WARN(submit_contexts > 2, "More than two context complete events?\n");
521 ring->next_context_status_buffer = write_pointer % 6;
522
523 I915_WRITE(RING_CONTEXT_STATUS_PTR(ring),
524 ((u32)ring->next_context_status_buffer & 0x07) << 8);
525}
526
ae70797d 527static int execlists_context_queue(struct drm_i915_gem_request *request)
acdd884a 528{
ae70797d 529 struct intel_engine_cs *ring = request->ring;
6d3d8274 530 struct drm_i915_gem_request *cursor;
f1ad5a1f 531 int num_elements = 0;
acdd884a 532
ae70797d 533 if (request->ctx != ring->default_context)
8ba319da 534 intel_lr_context_pin(request);
9bb1af44
JH
535
536 i915_gem_request_reference(request);
537
ae70797d 538 request->tail = request->ringbuf->tail;
2d12955a 539
b5eba372 540 spin_lock_irq(&ring->execlist_lock);
acdd884a 541
f1ad5a1f
OM
542 list_for_each_entry(cursor, &ring->execlist_queue, execlist_link)
543 if (++num_elements > 2)
544 break;
545
546 if (num_elements > 2) {
6d3d8274 547 struct drm_i915_gem_request *tail_req;
f1ad5a1f
OM
548
549 tail_req = list_last_entry(&ring->execlist_queue,
6d3d8274 550 struct drm_i915_gem_request,
f1ad5a1f
OM
551 execlist_link);
552
ae70797d 553 if (request->ctx == tail_req->ctx) {
f1ad5a1f 554 WARN(tail_req->elsp_submitted != 0,
7ba717cf 555 "More than 2 already-submitted reqs queued\n");
f1ad5a1f 556 list_del(&tail_req->execlist_link);
c86ee3a9
TD
557 list_add_tail(&tail_req->execlist_link,
558 &ring->execlist_retired_req_list);
f1ad5a1f
OM
559 }
560 }
561
6d3d8274 562 list_add_tail(&request->execlist_link, &ring->execlist_queue);
f1ad5a1f 563 if (num_elements == 0)
acdd884a
MT
564 execlists_context_unqueue(ring);
565
b5eba372 566 spin_unlock_irq(&ring->execlist_lock);
acdd884a
MT
567
568 return 0;
569}
570
2f20055d 571static int logical_ring_invalidate_all_caches(struct drm_i915_gem_request *req)
ba8b7ccb 572{
2f20055d 573 struct intel_engine_cs *ring = req->ring;
ba8b7ccb
OM
574 uint32_t flush_domains;
575 int ret;
576
577 flush_domains = 0;
578 if (ring->gpu_caches_dirty)
579 flush_domains = I915_GEM_GPU_DOMAINS;
580
7deb4d39 581 ret = ring->emit_flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
ba8b7ccb
OM
582 if (ret)
583 return ret;
584
585 ring->gpu_caches_dirty = false;
586 return 0;
587}
588
535fbe82 589static int execlists_move_to_gpu(struct drm_i915_gem_request *req,
ba8b7ccb
OM
590 struct list_head *vmas)
591{
535fbe82 592 const unsigned other_rings = ~intel_ring_flag(req->ring);
ba8b7ccb
OM
593 struct i915_vma *vma;
594 uint32_t flush_domains = 0;
595 bool flush_chipset = false;
596 int ret;
597
598 list_for_each_entry(vma, vmas, exec_list) {
599 struct drm_i915_gem_object *obj = vma->obj;
600
03ade511 601 if (obj->active & other_rings) {
91af127f 602 ret = i915_gem_object_sync(obj, req->ring, &req);
03ade511
CW
603 if (ret)
604 return ret;
605 }
ba8b7ccb
OM
606
607 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
608 flush_chipset |= i915_gem_clflush_object(obj, false);
609
610 flush_domains |= obj->base.write_domain;
611 }
612
613 if (flush_domains & I915_GEM_DOMAIN_GTT)
614 wmb();
615
616 /* Unconditionally invalidate gpu caches and ensure that we do flush
617 * any residual writes from the previous batch.
618 */
2f20055d 619 return logical_ring_invalidate_all_caches(req);
ba8b7ccb
OM
620}
621
40e895ce 622int intel_logical_ring_alloc_request_extras(struct drm_i915_gem_request *request)
bc0dce3f 623{
bc0dce3f
JH
624 int ret;
625
f3cc01f0
MK
626 request->ringbuf = request->ctx->engine[request->ring->id].ringbuf;
627
40e895ce 628 if (request->ctx != request->ring->default_context) {
8ba319da 629 ret = intel_lr_context_pin(request);
6689cb2b 630 if (ret)
bc0dce3f 631 return ret;
bc0dce3f
JH
632 }
633
bc0dce3f
JH
634 return 0;
635}
636
ae70797d 637static int logical_ring_wait_for_space(struct drm_i915_gem_request *req,
595e1eeb 638 int bytes)
bc0dce3f 639{
ae70797d
JH
640 struct intel_ringbuffer *ringbuf = req->ringbuf;
641 struct intel_engine_cs *ring = req->ring;
642 struct drm_i915_gem_request *target;
b4716185
CW
643 unsigned space;
644 int ret;
bc0dce3f
JH
645
646 if (intel_ring_space(ringbuf) >= bytes)
647 return 0;
648
79bbcc29
JH
649 /* The whole point of reserving space is to not wait! */
650 WARN_ON(ringbuf->reserved_in_use);
651
ae70797d 652 list_for_each_entry(target, &ring->request_list, list) {
bc0dce3f
JH
653 /*
654 * The request queue is per-engine, so can contain requests
655 * from multiple ringbuffers. Here, we must ignore any that
656 * aren't from the ringbuffer we're considering.
657 */
ae70797d 658 if (target->ringbuf != ringbuf)
bc0dce3f
JH
659 continue;
660
661 /* Would completion of this request free enough space? */
ae70797d 662 space = __intel_ring_space(target->postfix, ringbuf->tail,
b4716185
CW
663 ringbuf->size);
664 if (space >= bytes)
bc0dce3f 665 break;
bc0dce3f
JH
666 }
667
ae70797d 668 if (WARN_ON(&target->list == &ring->request_list))
bc0dce3f
JH
669 return -ENOSPC;
670
ae70797d 671 ret = i915_wait_request(target);
bc0dce3f
JH
672 if (ret)
673 return ret;
674
b4716185
CW
675 ringbuf->space = space;
676 return 0;
bc0dce3f
JH
677}
678
679/*
680 * intel_logical_ring_advance_and_submit() - advance the tail and submit the workload
ae70797d 681 * @request: Request to advance the logical ringbuffer of.
bc0dce3f
JH
682 *
683 * The tail is updated in our logical ringbuffer struct, not in the actual context. What
684 * really happens during submission is that the context and current tail will be placed
685 * on a queue waiting for the ELSP to be ready to accept a new context submission. At that
686 * point, the tail *inside* the context is updated and the ELSP written to.
687 */
688static void
ae70797d 689intel_logical_ring_advance_and_submit(struct drm_i915_gem_request *request)
bc0dce3f 690{
ae70797d 691 struct intel_engine_cs *ring = request->ring;
bc0dce3f 692
ae70797d 693 intel_logical_ring_advance(request->ringbuf);
bc0dce3f
JH
694
695 if (intel_ring_stopped(ring))
696 return;
697
ae70797d 698 execlists_context_queue(request);
bc0dce3f
JH
699}
700
79bbcc29 701static void __wrap_ring_buffer(struct intel_ringbuffer *ringbuf)
bc0dce3f
JH
702{
703 uint32_t __iomem *virt;
704 int rem = ringbuf->size - ringbuf->tail;
705
bc0dce3f
JH
706 virt = ringbuf->virtual_start + ringbuf->tail;
707 rem /= 4;
708 while (rem--)
709 iowrite32(MI_NOOP, virt++);
710
711 ringbuf->tail = 0;
712 intel_ring_update_space(ringbuf);
bc0dce3f
JH
713}
714
ae70797d 715static int logical_ring_prepare(struct drm_i915_gem_request *req, int bytes)
bc0dce3f 716{
ae70797d 717 struct intel_ringbuffer *ringbuf = req->ringbuf;
79bbcc29
JH
718 int remain_usable = ringbuf->effective_size - ringbuf->tail;
719 int remain_actual = ringbuf->size - ringbuf->tail;
720 int ret, total_bytes, wait_bytes = 0;
721 bool need_wrap = false;
29b1b415 722
79bbcc29
JH
723 if (ringbuf->reserved_in_use)
724 total_bytes = bytes;
725 else
726 total_bytes = bytes + ringbuf->reserved_size;
29b1b415 727
79bbcc29
JH
728 if (unlikely(bytes > remain_usable)) {
729 /*
730 * Not enough space for the basic request. So need to flush
731 * out the remainder and then wait for base + reserved.
732 */
733 wait_bytes = remain_actual + total_bytes;
734 need_wrap = true;
735 } else {
736 if (unlikely(total_bytes > remain_usable)) {
737 /*
738 * The base request will fit but the reserved space
739 * falls off the end. So only need to to wait for the
740 * reserved size after flushing out the remainder.
741 */
742 wait_bytes = remain_actual + ringbuf->reserved_size;
743 need_wrap = true;
744 } else if (total_bytes > ringbuf->space) {
745 /* No wrapping required, just waiting. */
746 wait_bytes = total_bytes;
29b1b415 747 }
bc0dce3f
JH
748 }
749
79bbcc29
JH
750 if (wait_bytes) {
751 ret = logical_ring_wait_for_space(req, wait_bytes);
bc0dce3f
JH
752 if (unlikely(ret))
753 return ret;
79bbcc29
JH
754
755 if (need_wrap)
756 __wrap_ring_buffer(ringbuf);
bc0dce3f
JH
757 }
758
759 return 0;
760}
761
762/**
763 * intel_logical_ring_begin() - prepare the logical ringbuffer to accept some commands
764 *
4d616a29 765 * @request: The request to start some new work for
4d78c8dc 766 * @ctx: Logical ring context whose ringbuffer is being prepared.
bc0dce3f
JH
767 * @num_dwords: number of DWORDs that we plan to write to the ringbuffer.
768 *
769 * The ringbuffer might not be ready to accept the commands right away (maybe it needs to
770 * be wrapped, or wait a bit for the tail to be updated). This function takes care of that
771 * and also preallocates a request (every workload submission is still mediated through
772 * requests, same as it did with legacy ringbuffer submission).
773 *
774 * Return: non-zero if the ringbuffer is not ready to be written to.
775 */
3bbaba0c 776int intel_logical_ring_begin(struct drm_i915_gem_request *req, int num_dwords)
bc0dce3f 777{
4d616a29 778 struct drm_i915_private *dev_priv;
bc0dce3f
JH
779 int ret;
780
4d616a29
JH
781 WARN_ON(req == NULL);
782 dev_priv = req->ring->dev->dev_private;
783
bc0dce3f
JH
784 ret = i915_gem_check_wedge(&dev_priv->gpu_error,
785 dev_priv->mm.interruptible);
786 if (ret)
787 return ret;
788
ae70797d 789 ret = logical_ring_prepare(req, num_dwords * sizeof(uint32_t));
bc0dce3f
JH
790 if (ret)
791 return ret;
792
4d616a29 793 req->ringbuf->space -= num_dwords * sizeof(uint32_t);
bc0dce3f
JH
794 return 0;
795}
796
ccd98fe4
JH
797int intel_logical_ring_reserve_space(struct drm_i915_gem_request *request)
798{
799 /*
800 * The first call merely notes the reserve request and is common for
801 * all back ends. The subsequent localised _begin() call actually
802 * ensures that the reservation is available. Without the begin, if
803 * the request creator immediately submitted the request without
804 * adding any commands to it then there might not actually be
805 * sufficient room for the submission commands.
806 */
807 intel_ring_reserved_space_reserve(request->ringbuf, MIN_SPACE_FOR_ADD_REQUEST);
808
809 return intel_logical_ring_begin(request, 0);
810}
811
73e4d07f
OM
812/**
813 * execlists_submission() - submit a batchbuffer for execution, Execlists style
814 * @dev: DRM device.
815 * @file: DRM file.
816 * @ring: Engine Command Streamer to submit to.
817 * @ctx: Context to employ for this submission.
818 * @args: execbuffer call arguments.
819 * @vmas: list of vmas.
820 * @batch_obj: the batchbuffer to submit.
821 * @exec_start: batchbuffer start virtual address pointer.
8e004efc 822 * @dispatch_flags: translated execbuffer call flags.
73e4d07f
OM
823 *
824 * This is the evil twin version of i915_gem_ringbuffer_submission. It abstracts
825 * away the submission details of the execbuffer ioctl call.
826 *
827 * Return: non-zero if the submission fails.
828 */
5f19e2bf 829int intel_execlists_submission(struct i915_execbuffer_params *params,
454afebd 830 struct drm_i915_gem_execbuffer2 *args,
5f19e2bf 831 struct list_head *vmas)
454afebd 832{
5f19e2bf
JH
833 struct drm_device *dev = params->dev;
834 struct intel_engine_cs *ring = params->ring;
ba8b7ccb 835 struct drm_i915_private *dev_priv = dev->dev_private;
5f19e2bf
JH
836 struct intel_ringbuffer *ringbuf = params->ctx->engine[ring->id].ringbuf;
837 u64 exec_start;
ba8b7ccb
OM
838 int instp_mode;
839 u32 instp_mask;
840 int ret;
841
842 instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
843 instp_mask = I915_EXEC_CONSTANTS_MASK;
844 switch (instp_mode) {
845 case I915_EXEC_CONSTANTS_REL_GENERAL:
846 case I915_EXEC_CONSTANTS_ABSOLUTE:
847 case I915_EXEC_CONSTANTS_REL_SURFACE:
848 if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) {
849 DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
850 return -EINVAL;
851 }
852
853 if (instp_mode != dev_priv->relative_constants_mode) {
854 if (instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
855 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
856 return -EINVAL;
857 }
858
859 /* The HW changed the meaning on this bit on gen6 */
860 instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
861 }
862 break;
863 default:
864 DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
865 return -EINVAL;
866 }
867
868 if (args->num_cliprects != 0) {
869 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
870 return -EINVAL;
871 } else {
872 if (args->DR4 == 0xffffffff) {
873 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
874 args->DR4 = 0;
875 }
876
877 if (args->DR1 || args->DR4 || args->cliprects_ptr) {
878 DRM_DEBUG("0 cliprects but dirt in cliprects fields\n");
879 return -EINVAL;
880 }
881 }
882
883 if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
884 DRM_DEBUG("sol reset is gen7 only\n");
885 return -EINVAL;
886 }
887
535fbe82 888 ret = execlists_move_to_gpu(params->request, vmas);
ba8b7ccb
OM
889 if (ret)
890 return ret;
891
892 if (ring == &dev_priv->ring[RCS] &&
893 instp_mode != dev_priv->relative_constants_mode) {
4d616a29 894 ret = intel_logical_ring_begin(params->request, 4);
ba8b7ccb
OM
895 if (ret)
896 return ret;
897
898 intel_logical_ring_emit(ringbuf, MI_NOOP);
899 intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(1));
900 intel_logical_ring_emit(ringbuf, INSTPM);
901 intel_logical_ring_emit(ringbuf, instp_mask << 16 | instp_mode);
902 intel_logical_ring_advance(ringbuf);
903
904 dev_priv->relative_constants_mode = instp_mode;
905 }
906
5f19e2bf
JH
907 exec_start = params->batch_obj_vm_offset +
908 args->batch_start_offset;
909
be795fc1 910 ret = ring->emit_bb_start(params->request, exec_start, params->dispatch_flags);
ba8b7ccb
OM
911 if (ret)
912 return ret;
913
95c24161 914 trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
5e4be7bd 915
8a8edb59 916 i915_gem_execbuffer_move_to_active(vmas, params->request);
adeca76d 917 i915_gem_execbuffer_retire_commands(params);
ba8b7ccb 918
454afebd
OM
919 return 0;
920}
921
c86ee3a9
TD
922void intel_execlists_retire_requests(struct intel_engine_cs *ring)
923{
6d3d8274 924 struct drm_i915_gem_request *req, *tmp;
c86ee3a9
TD
925 struct list_head retired_list;
926
927 WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
928 if (list_empty(&ring->execlist_retired_req_list))
929 return;
930
931 INIT_LIST_HEAD(&retired_list);
b5eba372 932 spin_lock_irq(&ring->execlist_lock);
c86ee3a9 933 list_replace_init(&ring->execlist_retired_req_list, &retired_list);
b5eba372 934 spin_unlock_irq(&ring->execlist_lock);
c86ee3a9
TD
935
936 list_for_each_entry_safe(req, tmp, &retired_list, execlist_link) {
6d3d8274 937 struct intel_context *ctx = req->ctx;
7ba717cf
TD
938 struct drm_i915_gem_object *ctx_obj =
939 ctx->engine[ring->id].state;
940
941 if (ctx_obj && (ctx != ring->default_context))
8ba319da 942 intel_lr_context_unpin(req);
c86ee3a9 943 list_del(&req->execlist_link);
f8210795 944 i915_gem_request_unreference(req);
c86ee3a9
TD
945 }
946}
947
454afebd
OM
948void intel_logical_ring_stop(struct intel_engine_cs *ring)
949{
9832b9da
OM
950 struct drm_i915_private *dev_priv = ring->dev->dev_private;
951 int ret;
952
953 if (!intel_ring_initialized(ring))
954 return;
955
956 ret = intel_ring_idle(ring);
957 if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
958 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
959 ring->name, ret);
960
961 /* TODO: Is this correct with Execlists enabled? */
962 I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
963 if (wait_for_atomic((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
964 DRM_ERROR("%s :timed out trying to stop ring\n", ring->name);
965 return;
966 }
967 I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
454afebd
OM
968}
969
4866d729 970int logical_ring_flush_all_caches(struct drm_i915_gem_request *req)
48e29f55 971{
4866d729 972 struct intel_engine_cs *ring = req->ring;
48e29f55
OM
973 int ret;
974
975 if (!ring->gpu_caches_dirty)
976 return 0;
977
7deb4d39 978 ret = ring->emit_flush(req, 0, I915_GEM_GPU_DOMAINS);
48e29f55
OM
979 if (ret)
980 return ret;
981
982 ring->gpu_caches_dirty = false;
983 return 0;
984}
985
8ba319da 986static int intel_lr_context_pin(struct drm_i915_gem_request *rq)
dcb4c12a 987{
8ba319da
MK
988 struct intel_engine_cs *ring = rq->ring;
989 struct drm_i915_gem_object *ctx_obj = rq->ctx->engine[ring->id].state;
990 struct intel_ringbuffer *ringbuf = rq->ringbuf;
dcb4c12a
OM
991 int ret = 0;
992
993 WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
8ba319da 994 if (rq->ctx->engine[ring->id].pin_count++ == 0) {
dcb4c12a
OM
995 ret = i915_gem_obj_ggtt_pin(ctx_obj,
996 GEN8_LR_CONTEXT_ALIGN, 0);
997 if (ret)
a7cbedec 998 goto reset_pin_count;
7ba717cf
TD
999
1000 ret = intel_pin_and_map_ringbuffer_obj(ring->dev, ringbuf);
1001 if (ret)
1002 goto unpin_ctx_obj;
dcb4c12a
OM
1003 }
1004
7ba717cf
TD
1005 return ret;
1006
1007unpin_ctx_obj:
1008 i915_gem_object_ggtt_unpin(ctx_obj);
a7cbedec 1009reset_pin_count:
8ba319da 1010 rq->ctx->engine[ring->id].pin_count = 0;
7ba717cf 1011
dcb4c12a
OM
1012 return ret;
1013}
1014
8ba319da 1015void intel_lr_context_unpin(struct drm_i915_gem_request *rq)
dcb4c12a 1016{
8ba319da
MK
1017 struct intel_engine_cs *ring = rq->ring;
1018 struct drm_i915_gem_object *ctx_obj = rq->ctx->engine[ring->id].state;
1019 struct intel_ringbuffer *ringbuf = rq->ringbuf;
dcb4c12a
OM
1020
1021 if (ctx_obj) {
1022 WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
8ba319da 1023 if (--rq->ctx->engine[ring->id].pin_count == 0) {
7ba717cf 1024 intel_unpin_ringbuffer_obj(ringbuf);
dcb4c12a 1025 i915_gem_object_ggtt_unpin(ctx_obj);
7ba717cf 1026 }
dcb4c12a
OM
1027 }
1028}
1029
e2be4faf 1030static int intel_logical_ring_workarounds_emit(struct drm_i915_gem_request *req)
771b9a53
MT
1031{
1032 int ret, i;
e2be4faf
JH
1033 struct intel_engine_cs *ring = req->ring;
1034 struct intel_ringbuffer *ringbuf = req->ringbuf;
771b9a53
MT
1035 struct drm_device *dev = ring->dev;
1036 struct drm_i915_private *dev_priv = dev->dev_private;
1037 struct i915_workarounds *w = &dev_priv->workarounds;
1038
e6c1abb7 1039 if (WARN_ON_ONCE(w->count == 0))
771b9a53
MT
1040 return 0;
1041
1042 ring->gpu_caches_dirty = true;
4866d729 1043 ret = logical_ring_flush_all_caches(req);
771b9a53
MT
1044 if (ret)
1045 return ret;
1046
4d616a29 1047 ret = intel_logical_ring_begin(req, w->count * 2 + 2);
771b9a53
MT
1048 if (ret)
1049 return ret;
1050
1051 intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(w->count));
1052 for (i = 0; i < w->count; i++) {
1053 intel_logical_ring_emit(ringbuf, w->reg[i].addr);
1054 intel_logical_ring_emit(ringbuf, w->reg[i].value);
1055 }
1056 intel_logical_ring_emit(ringbuf, MI_NOOP);
1057
1058 intel_logical_ring_advance(ringbuf);
1059
1060 ring->gpu_caches_dirty = true;
4866d729 1061 ret = logical_ring_flush_all_caches(req);
771b9a53
MT
1062 if (ret)
1063 return ret;
1064
1065 return 0;
1066}
1067
83b8a982 1068#define wa_ctx_emit(batch, index, cmd) \
17ee950d 1069 do { \
83b8a982
AS
1070 int __index = (index)++; \
1071 if (WARN_ON(__index >= (PAGE_SIZE / sizeof(uint32_t)))) { \
17ee950d
AS
1072 return -ENOSPC; \
1073 } \
83b8a982 1074 batch[__index] = (cmd); \
17ee950d
AS
1075 } while (0)
1076
9e000847
AS
1077
1078/*
1079 * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
1080 * PIPE_CONTROL instruction. This is required for the flush to happen correctly
1081 * but there is a slight complication as this is applied in WA batch where the
1082 * values are only initialized once so we cannot take register value at the
1083 * beginning and reuse it further; hence we save its value to memory, upload a
1084 * constant value with bit21 set and then we restore it back with the saved value.
1085 * To simplify the WA, a constant value is formed by using the default value
1086 * of this register. This shouldn't be a problem because we are only modifying
1087 * it for a short period and this batch in non-premptible. We can ofcourse
1088 * use additional instructions that read the actual value of the register
1089 * at that time and set our bit of interest but it makes the WA complicated.
1090 *
1091 * This WA is also required for Gen9 so extracting as a function avoids
1092 * code duplication.
1093 */
1094static inline int gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *ring,
1095 uint32_t *const batch,
1096 uint32_t index)
1097{
1098 uint32_t l3sqc4_flush = (0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES);
1099
a4106a78
AS
1100 /*
1101 * WaDisableLSQCROPERFforOCL:skl
1102 * This WA is implemented in skl_init_clock_gating() but since
1103 * this batch updates GEN8_L3SQCREG4 with default value we need to
1104 * set this bit here to retain the WA during flush.
1105 */
1106 if (IS_SKYLAKE(ring->dev) && INTEL_REVID(ring->dev) <= SKL_REVID_E0)
1107 l3sqc4_flush |= GEN8_LQSC_RO_PERF_DIS;
1108
83b8a982
AS
1109 wa_ctx_emit(batch, index, (MI_STORE_REGISTER_MEM_GEN8(1) |
1110 MI_SRM_LRM_GLOBAL_GTT));
1111 wa_ctx_emit(batch, index, GEN8_L3SQCREG4);
1112 wa_ctx_emit(batch, index, ring->scratch.gtt_offset + 256);
1113 wa_ctx_emit(batch, index, 0);
1114
1115 wa_ctx_emit(batch, index, MI_LOAD_REGISTER_IMM(1));
1116 wa_ctx_emit(batch, index, GEN8_L3SQCREG4);
1117 wa_ctx_emit(batch, index, l3sqc4_flush);
1118
1119 wa_ctx_emit(batch, index, GFX_OP_PIPE_CONTROL(6));
1120 wa_ctx_emit(batch, index, (PIPE_CONTROL_CS_STALL |
1121 PIPE_CONTROL_DC_FLUSH_ENABLE));
1122 wa_ctx_emit(batch, index, 0);
1123 wa_ctx_emit(batch, index, 0);
1124 wa_ctx_emit(batch, index, 0);
1125 wa_ctx_emit(batch, index, 0);
1126
1127 wa_ctx_emit(batch, index, (MI_LOAD_REGISTER_MEM_GEN8(1) |
1128 MI_SRM_LRM_GLOBAL_GTT));
1129 wa_ctx_emit(batch, index, GEN8_L3SQCREG4);
1130 wa_ctx_emit(batch, index, ring->scratch.gtt_offset + 256);
1131 wa_ctx_emit(batch, index, 0);
9e000847
AS
1132
1133 return index;
1134}
1135
17ee950d
AS
1136static inline uint32_t wa_ctx_start(struct i915_wa_ctx_bb *wa_ctx,
1137 uint32_t offset,
1138 uint32_t start_alignment)
1139{
1140 return wa_ctx->offset = ALIGN(offset, start_alignment);
1141}
1142
1143static inline int wa_ctx_end(struct i915_wa_ctx_bb *wa_ctx,
1144 uint32_t offset,
1145 uint32_t size_alignment)
1146{
1147 wa_ctx->size = offset - wa_ctx->offset;
1148
1149 WARN(wa_ctx->size % size_alignment,
1150 "wa_ctx_bb failed sanity checks: size %d is not aligned to %d\n",
1151 wa_ctx->size, size_alignment);
1152 return 0;
1153}
1154
1155/**
1156 * gen8_init_indirectctx_bb() - initialize indirect ctx batch with WA
1157 *
1158 * @ring: only applicable for RCS
1159 * @wa_ctx: structure representing wa_ctx
1160 * offset: specifies start of the batch, should be cache-aligned. This is updated
1161 * with the offset value received as input.
1162 * size: size of the batch in DWORDS but HW expects in terms of cachelines
1163 * @batch: page in which WA are loaded
1164 * @offset: This field specifies the start of the batch, it should be
1165 * cache-aligned otherwise it is adjusted accordingly.
1166 * Typically we only have one indirect_ctx and per_ctx batch buffer which are
1167 * initialized at the beginning and shared across all contexts but this field
1168 * helps us to have multiple batches at different offsets and select them based
1169 * on a criteria. At the moment this batch always start at the beginning of the page
1170 * and at this point we don't have multiple wa_ctx batch buffers.
1171 *
1172 * The number of WA applied are not known at the beginning; we use this field
1173 * to return the no of DWORDS written.
4d78c8dc 1174 *
17ee950d
AS
1175 * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
1176 * so it adds NOOPs as padding to make it cacheline aligned.
1177 * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
1178 * makes a complete batch buffer.
1179 *
1180 * Return: non-zero if we exceed the PAGE_SIZE limit.
1181 */
1182
1183static int gen8_init_indirectctx_bb(struct intel_engine_cs *ring,
1184 struct i915_wa_ctx_bb *wa_ctx,
1185 uint32_t *const batch,
1186 uint32_t *offset)
1187{
0160f055 1188 uint32_t scratch_addr;
17ee950d
AS
1189 uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
1190
7ad00d1a 1191 /* WaDisableCtxRestoreArbitration:bdw,chv */
83b8a982 1192 wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_DISABLE);
17ee950d 1193
c82435bb
AS
1194 /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
1195 if (IS_BROADWELL(ring->dev)) {
9e000847
AS
1196 index = gen8_emit_flush_coherentl3_wa(ring, batch, index);
1197 if (index < 0)
1198 return index;
c82435bb
AS
1199 }
1200
0160f055
AS
1201 /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
1202 /* Actual scratch location is at 128 bytes offset */
1203 scratch_addr = ring->scratch.gtt_offset + 2*CACHELINE_BYTES;
1204
83b8a982
AS
1205 wa_ctx_emit(batch, index, GFX_OP_PIPE_CONTROL(6));
1206 wa_ctx_emit(batch, index, (PIPE_CONTROL_FLUSH_L3 |
1207 PIPE_CONTROL_GLOBAL_GTT_IVB |
1208 PIPE_CONTROL_CS_STALL |
1209 PIPE_CONTROL_QW_WRITE));
1210 wa_ctx_emit(batch, index, scratch_addr);
1211 wa_ctx_emit(batch, index, 0);
1212 wa_ctx_emit(batch, index, 0);
1213 wa_ctx_emit(batch, index, 0);
0160f055 1214
17ee950d
AS
1215 /* Pad to end of cacheline */
1216 while (index % CACHELINE_DWORDS)
83b8a982 1217 wa_ctx_emit(batch, index, MI_NOOP);
17ee950d
AS
1218
1219 /*
1220 * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
1221 * execution depends on the length specified in terms of cache lines
1222 * in the register CTX_RCS_INDIRECT_CTX
1223 */
1224
1225 return wa_ctx_end(wa_ctx, *offset = index, CACHELINE_DWORDS);
1226}
1227
1228/**
1229 * gen8_init_perctx_bb() - initialize per ctx batch with WA
1230 *
1231 * @ring: only applicable for RCS
1232 * @wa_ctx: structure representing wa_ctx
1233 * offset: specifies start of the batch, should be cache-aligned.
1234 * size: size of the batch in DWORDS but HW expects in terms of cachelines
4d78c8dc 1235 * @batch: page in which WA are loaded
17ee950d
AS
1236 * @offset: This field specifies the start of this batch.
1237 * This batch is started immediately after indirect_ctx batch. Since we ensure
1238 * that indirect_ctx ends on a cacheline this batch is aligned automatically.
1239 *
1240 * The number of DWORDS written are returned using this field.
1241 *
1242 * This batch is terminated with MI_BATCH_BUFFER_END and so we need not add padding
1243 * to align it with cacheline as padding after MI_BATCH_BUFFER_END is redundant.
1244 */
1245static int gen8_init_perctx_bb(struct intel_engine_cs *ring,
1246 struct i915_wa_ctx_bb *wa_ctx,
1247 uint32_t *const batch,
1248 uint32_t *offset)
1249{
1250 uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
1251
7ad00d1a 1252 /* WaDisableCtxRestoreArbitration:bdw,chv */
83b8a982 1253 wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_ENABLE);
7ad00d1a 1254
83b8a982 1255 wa_ctx_emit(batch, index, MI_BATCH_BUFFER_END);
17ee950d
AS
1256
1257 return wa_ctx_end(wa_ctx, *offset = index, 1);
1258}
1259
0504cffc
AS
1260static int gen9_init_indirectctx_bb(struct intel_engine_cs *ring,
1261 struct i915_wa_ctx_bb *wa_ctx,
1262 uint32_t *const batch,
1263 uint32_t *offset)
1264{
a4106a78 1265 int ret;
0907c8f7 1266 struct drm_device *dev = ring->dev;
0504cffc
AS
1267 uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
1268
0907c8f7
AS
1269 /* WaDisableCtxRestoreArbitration:skl,bxt */
1270 if ((IS_SKYLAKE(dev) && (INTEL_REVID(dev) <= SKL_REVID_D0)) ||
1271 (IS_BROXTON(dev) && (INTEL_REVID(dev) == BXT_REVID_A0)))
1272 wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_DISABLE);
0504cffc 1273
a4106a78
AS
1274 /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt */
1275 ret = gen8_emit_flush_coherentl3_wa(ring, batch, index);
1276 if (ret < 0)
1277 return ret;
1278 index = ret;
1279
0504cffc
AS
1280 /* Pad to end of cacheline */
1281 while (index % CACHELINE_DWORDS)
1282 wa_ctx_emit(batch, index, MI_NOOP);
1283
1284 return wa_ctx_end(wa_ctx, *offset = index, CACHELINE_DWORDS);
1285}
1286
1287static int gen9_init_perctx_bb(struct intel_engine_cs *ring,
1288 struct i915_wa_ctx_bb *wa_ctx,
1289 uint32_t *const batch,
1290 uint32_t *offset)
1291{
0907c8f7 1292 struct drm_device *dev = ring->dev;
0504cffc
AS
1293 uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
1294
9b01435d
AS
1295 /* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:skl,bxt */
1296 if ((IS_SKYLAKE(dev) && (INTEL_REVID(dev) <= SKL_REVID_B0)) ||
1297 (IS_BROXTON(dev) && (INTEL_REVID(dev) == BXT_REVID_A0))) {
1298 wa_ctx_emit(batch, index, MI_LOAD_REGISTER_IMM(1));
1299 wa_ctx_emit(batch, index, GEN9_SLICE_COMMON_ECO_CHICKEN0);
1300 wa_ctx_emit(batch, index,
1301 _MASKED_BIT_ENABLE(DISABLE_PIXEL_MASK_CAMMING));
1302 wa_ctx_emit(batch, index, MI_NOOP);
1303 }
1304
0907c8f7
AS
1305 /* WaDisableCtxRestoreArbitration:skl,bxt */
1306 if ((IS_SKYLAKE(dev) && (INTEL_REVID(dev) <= SKL_REVID_D0)) ||
1307 (IS_BROXTON(dev) && (INTEL_REVID(dev) == BXT_REVID_A0)))
1308 wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_ENABLE);
1309
0504cffc
AS
1310 wa_ctx_emit(batch, index, MI_BATCH_BUFFER_END);
1311
1312 return wa_ctx_end(wa_ctx, *offset = index, 1);
1313}
1314
17ee950d
AS
1315static int lrc_setup_wa_ctx_obj(struct intel_engine_cs *ring, u32 size)
1316{
1317 int ret;
1318
1319 ring->wa_ctx.obj = i915_gem_alloc_object(ring->dev, PAGE_ALIGN(size));
1320 if (!ring->wa_ctx.obj) {
1321 DRM_DEBUG_DRIVER("alloc LRC WA ctx backing obj failed.\n");
1322 return -ENOMEM;
1323 }
1324
1325 ret = i915_gem_obj_ggtt_pin(ring->wa_ctx.obj, PAGE_SIZE, 0);
1326 if (ret) {
1327 DRM_DEBUG_DRIVER("pin LRC WA ctx backing obj failed: %d\n",
1328 ret);
1329 drm_gem_object_unreference(&ring->wa_ctx.obj->base);
1330 return ret;
1331 }
1332
1333 return 0;
1334}
1335
1336static void lrc_destroy_wa_ctx_obj(struct intel_engine_cs *ring)
1337{
1338 if (ring->wa_ctx.obj) {
1339 i915_gem_object_ggtt_unpin(ring->wa_ctx.obj);
1340 drm_gem_object_unreference(&ring->wa_ctx.obj->base);
1341 ring->wa_ctx.obj = NULL;
1342 }
1343}
1344
1345static int intel_init_workaround_bb(struct intel_engine_cs *ring)
1346{
1347 int ret;
1348 uint32_t *batch;
1349 uint32_t offset;
1350 struct page *page;
1351 struct i915_ctx_workarounds *wa_ctx = &ring->wa_ctx;
1352
1353 WARN_ON(ring->id != RCS);
1354
5e60d790 1355 /* update this when WA for higher Gen are added */
0504cffc
AS
1356 if (INTEL_INFO(ring->dev)->gen > 9) {
1357 DRM_ERROR("WA batch buffer is not initialized for Gen%d\n",
1358 INTEL_INFO(ring->dev)->gen);
5e60d790 1359 return 0;
0504cffc 1360 }
5e60d790 1361
c4db7599
AS
1362 /* some WA perform writes to scratch page, ensure it is valid */
1363 if (ring->scratch.obj == NULL) {
1364 DRM_ERROR("scratch page not allocated for %s\n", ring->name);
1365 return -EINVAL;
1366 }
1367
17ee950d
AS
1368 ret = lrc_setup_wa_ctx_obj(ring, PAGE_SIZE);
1369 if (ret) {
1370 DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
1371 return ret;
1372 }
1373
1374 page = i915_gem_object_get_page(wa_ctx->obj, 0);
1375 batch = kmap_atomic(page);
1376 offset = 0;
1377
1378 if (INTEL_INFO(ring->dev)->gen == 8) {
1379 ret = gen8_init_indirectctx_bb(ring,
1380 &wa_ctx->indirect_ctx,
1381 batch,
1382 &offset);
1383 if (ret)
1384 goto out;
1385
1386 ret = gen8_init_perctx_bb(ring,
1387 &wa_ctx->per_ctx,
1388 batch,
1389 &offset);
1390 if (ret)
1391 goto out;
0504cffc
AS
1392 } else if (INTEL_INFO(ring->dev)->gen == 9) {
1393 ret = gen9_init_indirectctx_bb(ring,
1394 &wa_ctx->indirect_ctx,
1395 batch,
1396 &offset);
1397 if (ret)
1398 goto out;
1399
1400 ret = gen9_init_perctx_bb(ring,
1401 &wa_ctx->per_ctx,
1402 batch,
1403 &offset);
1404 if (ret)
1405 goto out;
17ee950d
AS
1406 }
1407
1408out:
1409 kunmap_atomic(batch);
1410 if (ret)
1411 lrc_destroy_wa_ctx_obj(ring);
1412
1413 return ret;
1414}
1415
9b1136d5
OM
1416static int gen8_init_common_ring(struct intel_engine_cs *ring)
1417{
1418 struct drm_device *dev = ring->dev;
1419 struct drm_i915_private *dev_priv = dev->dev_private;
1420
73d477f6
OM
1421 I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
1422 I915_WRITE(RING_HWSTAM(ring->mmio_base), 0xffffffff);
1423
2e5356da
AS
1424 if (ring->status_page.obj) {
1425 I915_WRITE(RING_HWS_PGA(ring->mmio_base),
1426 (u32)ring->status_page.gfx_addr);
1427 POSTING_READ(RING_HWS_PGA(ring->mmio_base));
1428 }
1429
9b1136d5
OM
1430 I915_WRITE(RING_MODE_GEN7(ring),
1431 _MASKED_BIT_DISABLE(GFX_REPLAY_MODE) |
1432 _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
1433 POSTING_READ(RING_MODE_GEN7(ring));
c0a03a2e 1434 ring->next_context_status_buffer = 0;
9b1136d5
OM
1435 DRM_DEBUG_DRIVER("Execlists enabled for %s\n", ring->name);
1436
1437 memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
1438
1439 return 0;
1440}
1441
1442static int gen8_init_render_ring(struct intel_engine_cs *ring)
1443{
1444 struct drm_device *dev = ring->dev;
1445 struct drm_i915_private *dev_priv = dev->dev_private;
1446 int ret;
1447
1448 ret = gen8_init_common_ring(ring);
1449 if (ret)
1450 return ret;
1451
1452 /* We need to disable the AsyncFlip performance optimisations in order
1453 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1454 * programmed to '1' on all products.
1455 *
1456 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
1457 */
1458 I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
1459
9b1136d5
OM
1460 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
1461
771b9a53 1462 return init_workarounds_ring(ring);
9b1136d5
OM
1463}
1464
82ef822e
DL
1465static int gen9_init_render_ring(struct intel_engine_cs *ring)
1466{
1467 int ret;
1468
1469 ret = gen8_init_common_ring(ring);
1470 if (ret)
1471 return ret;
1472
1473 return init_workarounds_ring(ring);
1474}
1475
7a01a0a2
MT
1476static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request *req)
1477{
1478 struct i915_hw_ppgtt *ppgtt = req->ctx->ppgtt;
1479 struct intel_engine_cs *ring = req->ring;
1480 struct intel_ringbuffer *ringbuf = req->ringbuf;
1481 const int num_lri_cmds = GEN8_LEGACY_PDPES * 2;
1482 int i, ret;
1483
1484 ret = intel_logical_ring_begin(req, num_lri_cmds * 2 + 2);
1485 if (ret)
1486 return ret;
1487
1488 intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(num_lri_cmds));
1489 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
1490 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
1491
1492 intel_logical_ring_emit(ringbuf, GEN8_RING_PDP_UDW(ring, i));
1493 intel_logical_ring_emit(ringbuf, upper_32_bits(pd_daddr));
1494 intel_logical_ring_emit(ringbuf, GEN8_RING_PDP_LDW(ring, i));
1495 intel_logical_ring_emit(ringbuf, lower_32_bits(pd_daddr));
1496 }
1497
1498 intel_logical_ring_emit(ringbuf, MI_NOOP);
1499 intel_logical_ring_advance(ringbuf);
1500
1501 return 0;
1502}
1503
be795fc1 1504static int gen8_emit_bb_start(struct drm_i915_gem_request *req,
8e004efc 1505 u64 offset, unsigned dispatch_flags)
15648585 1506{
be795fc1 1507 struct intel_ringbuffer *ringbuf = req->ringbuf;
8e004efc 1508 bool ppgtt = !(dispatch_flags & I915_DISPATCH_SECURE);
15648585
OM
1509 int ret;
1510
7a01a0a2
MT
1511 /* Don't rely in hw updating PDPs, specially in lite-restore.
1512 * Ideally, we should set Force PD Restore in ctx descriptor,
1513 * but we can't. Force Restore would be a second option, but
1514 * it is unsafe in case of lite-restore (because the ctx is
1515 * not idle). */
1516 if (req->ctx->ppgtt &&
1517 (intel_ring_flag(req->ring) & req->ctx->ppgtt->pd_dirty_rings)) {
1518 ret = intel_logical_ring_emit_pdps(req);
1519 if (ret)
1520 return ret;
1521
1522 req->ctx->ppgtt->pd_dirty_rings &= ~intel_ring_flag(req->ring);
1523 }
1524
4d616a29 1525 ret = intel_logical_ring_begin(req, 4);
15648585
OM
1526 if (ret)
1527 return ret;
1528
1529 /* FIXME(BDW): Address space and security selectors. */
6922528a
AJ
1530 intel_logical_ring_emit(ringbuf, MI_BATCH_BUFFER_START_GEN8 |
1531 (ppgtt<<8) |
1532 (dispatch_flags & I915_DISPATCH_RS ?
1533 MI_BATCH_RESOURCE_STREAMER : 0));
15648585
OM
1534 intel_logical_ring_emit(ringbuf, lower_32_bits(offset));
1535 intel_logical_ring_emit(ringbuf, upper_32_bits(offset));
1536 intel_logical_ring_emit(ringbuf, MI_NOOP);
1537 intel_logical_ring_advance(ringbuf);
1538
1539 return 0;
1540}
1541
73d477f6
OM
1542static bool gen8_logical_ring_get_irq(struct intel_engine_cs *ring)
1543{
1544 struct drm_device *dev = ring->dev;
1545 struct drm_i915_private *dev_priv = dev->dev_private;
1546 unsigned long flags;
1547
7cd512f1 1548 if (WARN_ON(!intel_irqs_enabled(dev_priv)))
73d477f6
OM
1549 return false;
1550
1551 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1552 if (ring->irq_refcount++ == 0) {
1553 I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
1554 POSTING_READ(RING_IMR(ring->mmio_base));
1555 }
1556 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1557
1558 return true;
1559}
1560
1561static void gen8_logical_ring_put_irq(struct intel_engine_cs *ring)
1562{
1563 struct drm_device *dev = ring->dev;
1564 struct drm_i915_private *dev_priv = dev->dev_private;
1565 unsigned long flags;
1566
1567 spin_lock_irqsave(&dev_priv->irq_lock, flags);
1568 if (--ring->irq_refcount == 0) {
1569 I915_WRITE_IMR(ring, ~ring->irq_keep_mask);
1570 POSTING_READ(RING_IMR(ring->mmio_base));
1571 }
1572 spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
1573}
1574
7deb4d39 1575static int gen8_emit_flush(struct drm_i915_gem_request *request,
4712274c
OM
1576 u32 invalidate_domains,
1577 u32 unused)
1578{
7deb4d39 1579 struct intel_ringbuffer *ringbuf = request->ringbuf;
4712274c
OM
1580 struct intel_engine_cs *ring = ringbuf->ring;
1581 struct drm_device *dev = ring->dev;
1582 struct drm_i915_private *dev_priv = dev->dev_private;
1583 uint32_t cmd;
1584 int ret;
1585
4d616a29 1586 ret = intel_logical_ring_begin(request, 4);
4712274c
OM
1587 if (ret)
1588 return ret;
1589
1590 cmd = MI_FLUSH_DW + 1;
1591
f0a1fb10
CW
1592 /* We always require a command barrier so that subsequent
1593 * commands, such as breadcrumb interrupts, are strictly ordered
1594 * wrt the contents of the write cache being flushed to memory
1595 * (and thus being coherent from the CPU).
1596 */
1597 cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1598
1599 if (invalidate_domains & I915_GEM_GPU_DOMAINS) {
1600 cmd |= MI_INVALIDATE_TLB;
1601 if (ring == &dev_priv->ring[VCS])
1602 cmd |= MI_INVALIDATE_BSD;
4712274c
OM
1603 }
1604
1605 intel_logical_ring_emit(ringbuf, cmd);
1606 intel_logical_ring_emit(ringbuf,
1607 I915_GEM_HWS_SCRATCH_ADDR |
1608 MI_FLUSH_DW_USE_GTT);
1609 intel_logical_ring_emit(ringbuf, 0); /* upper addr */
1610 intel_logical_ring_emit(ringbuf, 0); /* value */
1611 intel_logical_ring_advance(ringbuf);
1612
1613 return 0;
1614}
1615
7deb4d39 1616static int gen8_emit_flush_render(struct drm_i915_gem_request *request,
4712274c
OM
1617 u32 invalidate_domains,
1618 u32 flush_domains)
1619{
7deb4d39 1620 struct intel_ringbuffer *ringbuf = request->ringbuf;
4712274c
OM
1621 struct intel_engine_cs *ring = ringbuf->ring;
1622 u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
9647ff36 1623 bool vf_flush_wa;
4712274c
OM
1624 u32 flags = 0;
1625 int ret;
1626
1627 flags |= PIPE_CONTROL_CS_STALL;
1628
1629 if (flush_domains) {
1630 flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
1631 flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
1632 }
1633
1634 if (invalidate_domains) {
1635 flags |= PIPE_CONTROL_TLB_INVALIDATE;
1636 flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
1637 flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
1638 flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
1639 flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
1640 flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
1641 flags |= PIPE_CONTROL_QW_WRITE;
1642 flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
1643 }
1644
9647ff36
ID
1645 /*
1646 * On GEN9+ Before VF_CACHE_INVALIDATE we need to emit a NULL pipe
1647 * control.
1648 */
1649 vf_flush_wa = INTEL_INFO(ring->dev)->gen >= 9 &&
1650 flags & PIPE_CONTROL_VF_CACHE_INVALIDATE;
1651
4d616a29 1652 ret = intel_logical_ring_begin(request, vf_flush_wa ? 12 : 6);
4712274c
OM
1653 if (ret)
1654 return ret;
1655
9647ff36
ID
1656 if (vf_flush_wa) {
1657 intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
1658 intel_logical_ring_emit(ringbuf, 0);
1659 intel_logical_ring_emit(ringbuf, 0);
1660 intel_logical_ring_emit(ringbuf, 0);
1661 intel_logical_ring_emit(ringbuf, 0);
1662 intel_logical_ring_emit(ringbuf, 0);
1663 }
1664
4712274c
OM
1665 intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
1666 intel_logical_ring_emit(ringbuf, flags);
1667 intel_logical_ring_emit(ringbuf, scratch_addr);
1668 intel_logical_ring_emit(ringbuf, 0);
1669 intel_logical_ring_emit(ringbuf, 0);
1670 intel_logical_ring_emit(ringbuf, 0);
1671 intel_logical_ring_advance(ringbuf);
1672
1673 return 0;
1674}
1675
e94e37ad
OM
1676static u32 gen8_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
1677{
1678 return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
1679}
1680
1681static void gen8_set_seqno(struct intel_engine_cs *ring, u32 seqno)
1682{
1683 intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
1684}
1685
c4e76638 1686static int gen8_emit_request(struct drm_i915_gem_request *request)
4da46e1e 1687{
c4e76638 1688 struct intel_ringbuffer *ringbuf = request->ringbuf;
4da46e1e
OM
1689 struct intel_engine_cs *ring = ringbuf->ring;
1690 u32 cmd;
1691 int ret;
1692
53292cdb
MT
1693 /*
1694 * Reserve space for 2 NOOPs at the end of each request to be
1695 * used as a workaround for not being allowed to do lite
1696 * restore with HEAD==TAIL (WaIdleLiteRestore).
1697 */
4d616a29 1698 ret = intel_logical_ring_begin(request, 8);
4da46e1e
OM
1699 if (ret)
1700 return ret;
1701
8edfbb8b 1702 cmd = MI_STORE_DWORD_IMM_GEN4;
4da46e1e
OM
1703 cmd |= MI_GLOBAL_GTT;
1704
1705 intel_logical_ring_emit(ringbuf, cmd);
1706 intel_logical_ring_emit(ringbuf,
1707 (ring->status_page.gfx_addr +
1708 (I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT)));
1709 intel_logical_ring_emit(ringbuf, 0);
c4e76638 1710 intel_logical_ring_emit(ringbuf, i915_gem_request_get_seqno(request));
4da46e1e
OM
1711 intel_logical_ring_emit(ringbuf, MI_USER_INTERRUPT);
1712 intel_logical_ring_emit(ringbuf, MI_NOOP);
ae70797d 1713 intel_logical_ring_advance_and_submit(request);
4da46e1e 1714
53292cdb
MT
1715 /*
1716 * Here we add two extra NOOPs as padding to avoid
1717 * lite restore of a context with HEAD==TAIL.
1718 */
1719 intel_logical_ring_emit(ringbuf, MI_NOOP);
1720 intel_logical_ring_emit(ringbuf, MI_NOOP);
1721 intel_logical_ring_advance(ringbuf);
1722
4da46e1e
OM
1723 return 0;
1724}
1725
be01363f 1726static int intel_lr_context_render_state_init(struct drm_i915_gem_request *req)
cef437ad 1727{
cef437ad 1728 struct render_state so;
cef437ad
DL
1729 int ret;
1730
be01363f 1731 ret = i915_gem_render_state_prepare(req->ring, &so);
cef437ad
DL
1732 if (ret)
1733 return ret;
1734
1735 if (so.rodata == NULL)
1736 return 0;
1737
be795fc1 1738 ret = req->ring->emit_bb_start(req, so.ggtt_offset,
be01363f 1739 I915_DISPATCH_SECURE);
cef437ad
DL
1740 if (ret)
1741 goto out;
1742
b2af0376 1743 i915_vma_move_to_active(i915_gem_obj_to_ggtt(so.obj), req);
cef437ad 1744
cef437ad
DL
1745out:
1746 i915_gem_render_state_fini(&so);
1747 return ret;
1748}
1749
8753181e 1750static int gen8_init_rcs_context(struct drm_i915_gem_request *req)
e7778be1
TD
1751{
1752 int ret;
1753
e2be4faf 1754 ret = intel_logical_ring_workarounds_emit(req);
e7778be1
TD
1755 if (ret)
1756 return ret;
1757
3bbaba0c
PA
1758 ret = intel_rcs_context_init_mocs(req);
1759 /*
1760 * Failing to program the MOCS is non-fatal.The system will not
1761 * run at peak performance. So generate an error and carry on.
1762 */
1763 if (ret)
1764 DRM_ERROR("MOCS failed to program: expect performance issues.\n");
1765
be01363f 1766 return intel_lr_context_render_state_init(req);
e7778be1
TD
1767}
1768
73e4d07f
OM
1769/**
1770 * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
1771 *
1772 * @ring: Engine Command Streamer.
1773 *
1774 */
454afebd
OM
1775void intel_logical_ring_cleanup(struct intel_engine_cs *ring)
1776{
6402c330 1777 struct drm_i915_private *dev_priv;
9832b9da 1778
48d82387
OM
1779 if (!intel_ring_initialized(ring))
1780 return;
1781
6402c330
JH
1782 dev_priv = ring->dev->dev_private;
1783
9832b9da
OM
1784 intel_logical_ring_stop(ring);
1785 WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
48d82387
OM
1786
1787 if (ring->cleanup)
1788 ring->cleanup(ring);
1789
1790 i915_cmd_parser_fini_ring(ring);
06fbca71 1791 i915_gem_batch_pool_fini(&ring->batch_pool);
48d82387
OM
1792
1793 if (ring->status_page.obj) {
1794 kunmap(sg_page(ring->status_page.obj->pages->sgl));
1795 ring->status_page.obj = NULL;
1796 }
17ee950d
AS
1797
1798 lrc_destroy_wa_ctx_obj(ring);
454afebd
OM
1799}
1800
1801static int logical_ring_init(struct drm_device *dev, struct intel_engine_cs *ring)
1802{
48d82387 1803 int ret;
48d82387
OM
1804
1805 /* Intentionally left blank. */
1806 ring->buffer = NULL;
1807
1808 ring->dev = dev;
1809 INIT_LIST_HEAD(&ring->active_list);
1810 INIT_LIST_HEAD(&ring->request_list);
06fbca71 1811 i915_gem_batch_pool_init(dev, &ring->batch_pool);
48d82387
OM
1812 init_waitqueue_head(&ring->irq_queue);
1813
acdd884a 1814 INIT_LIST_HEAD(&ring->execlist_queue);
c86ee3a9 1815 INIT_LIST_HEAD(&ring->execlist_retired_req_list);
acdd884a
MT
1816 spin_lock_init(&ring->execlist_lock);
1817
48d82387
OM
1818 ret = i915_cmd_parser_init_ring(ring);
1819 if (ret)
1820 return ret;
1821
564ddb2f
OM
1822 ret = intel_lr_context_deferred_create(ring->default_context, ring);
1823
1824 return ret;
454afebd
OM
1825}
1826
1827static int logical_render_ring_init(struct drm_device *dev)
1828{
1829 struct drm_i915_private *dev_priv = dev->dev_private;
1830 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
99be1dfe 1831 int ret;
454afebd
OM
1832
1833 ring->name = "render ring";
1834 ring->id = RCS;
1835 ring->mmio_base = RENDER_RING_BASE;
1836 ring->irq_enable_mask =
1837 GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
73d477f6
OM
1838 ring->irq_keep_mask =
1839 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
1840 if (HAS_L3_DPF(dev))
1841 ring->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
454afebd 1842
82ef822e
DL
1843 if (INTEL_INFO(dev)->gen >= 9)
1844 ring->init_hw = gen9_init_render_ring;
1845 else
1846 ring->init_hw = gen8_init_render_ring;
e7778be1 1847 ring->init_context = gen8_init_rcs_context;
9b1136d5 1848 ring->cleanup = intel_fini_pipe_control;
e94e37ad
OM
1849 ring->get_seqno = gen8_get_seqno;
1850 ring->set_seqno = gen8_set_seqno;
4da46e1e 1851 ring->emit_request = gen8_emit_request;
4712274c 1852 ring->emit_flush = gen8_emit_flush_render;
73d477f6
OM
1853 ring->irq_get = gen8_logical_ring_get_irq;
1854 ring->irq_put = gen8_logical_ring_put_irq;
15648585 1855 ring->emit_bb_start = gen8_emit_bb_start;
9b1136d5 1856
99be1dfe 1857 ring->dev = dev;
c4db7599
AS
1858
1859 ret = intel_init_pipe_control(ring);
99be1dfe
DV
1860 if (ret)
1861 return ret;
1862
17ee950d
AS
1863 ret = intel_init_workaround_bb(ring);
1864 if (ret) {
1865 /*
1866 * We continue even if we fail to initialize WA batch
1867 * because we only expect rare glitches but nothing
1868 * critical to prevent us from using GPU
1869 */
1870 DRM_ERROR("WA batch buffer initialization failed: %d\n",
1871 ret);
1872 }
1873
c4db7599
AS
1874 ret = logical_ring_init(dev, ring);
1875 if (ret) {
17ee950d 1876 lrc_destroy_wa_ctx_obj(ring);
c4db7599 1877 }
17ee950d
AS
1878
1879 return ret;
454afebd
OM
1880}
1881
1882static int logical_bsd_ring_init(struct drm_device *dev)
1883{
1884 struct drm_i915_private *dev_priv = dev->dev_private;
1885 struct intel_engine_cs *ring = &dev_priv->ring[VCS];
1886
1887 ring->name = "bsd ring";
1888 ring->id = VCS;
1889 ring->mmio_base = GEN6_BSD_RING_BASE;
1890 ring->irq_enable_mask =
1891 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
73d477f6
OM
1892 ring->irq_keep_mask =
1893 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
454afebd 1894
ecfe00d8 1895 ring->init_hw = gen8_init_common_ring;
e94e37ad
OM
1896 ring->get_seqno = gen8_get_seqno;
1897 ring->set_seqno = gen8_set_seqno;
4da46e1e 1898 ring->emit_request = gen8_emit_request;
4712274c 1899 ring->emit_flush = gen8_emit_flush;
73d477f6
OM
1900 ring->irq_get = gen8_logical_ring_get_irq;
1901 ring->irq_put = gen8_logical_ring_put_irq;
15648585 1902 ring->emit_bb_start = gen8_emit_bb_start;
9b1136d5 1903
454afebd
OM
1904 return logical_ring_init(dev, ring);
1905}
1906
1907static int logical_bsd2_ring_init(struct drm_device *dev)
1908{
1909 struct drm_i915_private *dev_priv = dev->dev_private;
1910 struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
1911
1912 ring->name = "bds2 ring";
1913 ring->id = VCS2;
1914 ring->mmio_base = GEN8_BSD2_RING_BASE;
1915 ring->irq_enable_mask =
1916 GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
73d477f6
OM
1917 ring->irq_keep_mask =
1918 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
454afebd 1919
ecfe00d8 1920 ring->init_hw = gen8_init_common_ring;
e94e37ad
OM
1921 ring->get_seqno = gen8_get_seqno;
1922 ring->set_seqno = gen8_set_seqno;
4da46e1e 1923 ring->emit_request = gen8_emit_request;
4712274c 1924 ring->emit_flush = gen8_emit_flush;
73d477f6
OM
1925 ring->irq_get = gen8_logical_ring_get_irq;
1926 ring->irq_put = gen8_logical_ring_put_irq;
15648585 1927 ring->emit_bb_start = gen8_emit_bb_start;
9b1136d5 1928
454afebd
OM
1929 return logical_ring_init(dev, ring);
1930}
1931
1932static int logical_blt_ring_init(struct drm_device *dev)
1933{
1934 struct drm_i915_private *dev_priv = dev->dev_private;
1935 struct intel_engine_cs *ring = &dev_priv->ring[BCS];
1936
1937 ring->name = "blitter ring";
1938 ring->id = BCS;
1939 ring->mmio_base = BLT_RING_BASE;
1940 ring->irq_enable_mask =
1941 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
73d477f6
OM
1942 ring->irq_keep_mask =
1943 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
454afebd 1944
ecfe00d8 1945 ring->init_hw = gen8_init_common_ring;
e94e37ad
OM
1946 ring->get_seqno = gen8_get_seqno;
1947 ring->set_seqno = gen8_set_seqno;
4da46e1e 1948 ring->emit_request = gen8_emit_request;
4712274c 1949 ring->emit_flush = gen8_emit_flush;
73d477f6
OM
1950 ring->irq_get = gen8_logical_ring_get_irq;
1951 ring->irq_put = gen8_logical_ring_put_irq;
15648585 1952 ring->emit_bb_start = gen8_emit_bb_start;
9b1136d5 1953
454afebd
OM
1954 return logical_ring_init(dev, ring);
1955}
1956
1957static int logical_vebox_ring_init(struct drm_device *dev)
1958{
1959 struct drm_i915_private *dev_priv = dev->dev_private;
1960 struct intel_engine_cs *ring = &dev_priv->ring[VECS];
1961
1962 ring->name = "video enhancement ring";
1963 ring->id = VECS;
1964 ring->mmio_base = VEBOX_RING_BASE;
1965 ring->irq_enable_mask =
1966 GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
73d477f6
OM
1967 ring->irq_keep_mask =
1968 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
454afebd 1969
ecfe00d8 1970 ring->init_hw = gen8_init_common_ring;
e94e37ad
OM
1971 ring->get_seqno = gen8_get_seqno;
1972 ring->set_seqno = gen8_set_seqno;
4da46e1e 1973 ring->emit_request = gen8_emit_request;
4712274c 1974 ring->emit_flush = gen8_emit_flush;
73d477f6
OM
1975 ring->irq_get = gen8_logical_ring_get_irq;
1976 ring->irq_put = gen8_logical_ring_put_irq;
15648585 1977 ring->emit_bb_start = gen8_emit_bb_start;
9b1136d5 1978
454afebd
OM
1979 return logical_ring_init(dev, ring);
1980}
1981
73e4d07f
OM
1982/**
1983 * intel_logical_rings_init() - allocate, populate and init the Engine Command Streamers
1984 * @dev: DRM device.
1985 *
1986 * This function inits the engines for an Execlists submission style (the equivalent in the
1987 * legacy ringbuffer submission world would be i915_gem_init_rings). It does it only for
1988 * those engines that are present in the hardware.
1989 *
1990 * Return: non-zero if the initialization failed.
1991 */
454afebd
OM
1992int intel_logical_rings_init(struct drm_device *dev)
1993{
1994 struct drm_i915_private *dev_priv = dev->dev_private;
1995 int ret;
1996
1997 ret = logical_render_ring_init(dev);
1998 if (ret)
1999 return ret;
2000
2001 if (HAS_BSD(dev)) {
2002 ret = logical_bsd_ring_init(dev);
2003 if (ret)
2004 goto cleanup_render_ring;
2005 }
2006
2007 if (HAS_BLT(dev)) {
2008 ret = logical_blt_ring_init(dev);
2009 if (ret)
2010 goto cleanup_bsd_ring;
2011 }
2012
2013 if (HAS_VEBOX(dev)) {
2014 ret = logical_vebox_ring_init(dev);
2015 if (ret)
2016 goto cleanup_blt_ring;
2017 }
2018
2019 if (HAS_BSD2(dev)) {
2020 ret = logical_bsd2_ring_init(dev);
2021 if (ret)
2022 goto cleanup_vebox_ring;
2023 }
2024
2025 ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
2026 if (ret)
2027 goto cleanup_bsd2_ring;
2028
2029 return 0;
2030
2031cleanup_bsd2_ring:
2032 intel_logical_ring_cleanup(&dev_priv->ring[VCS2]);
2033cleanup_vebox_ring:
2034 intel_logical_ring_cleanup(&dev_priv->ring[VECS]);
2035cleanup_blt_ring:
2036 intel_logical_ring_cleanup(&dev_priv->ring[BCS]);
2037cleanup_bsd_ring:
2038 intel_logical_ring_cleanup(&dev_priv->ring[VCS]);
2039cleanup_render_ring:
2040 intel_logical_ring_cleanup(&dev_priv->ring[RCS]);
2041
2042 return ret;
2043}
2044
0cea6502
JM
2045static u32
2046make_rpcs(struct drm_device *dev)
2047{
2048 u32 rpcs = 0;
2049
2050 /*
2051 * No explicit RPCS request is needed to ensure full
2052 * slice/subslice/EU enablement prior to Gen9.
2053 */
2054 if (INTEL_INFO(dev)->gen < 9)
2055 return 0;
2056
2057 /*
2058 * Starting in Gen9, render power gating can leave
2059 * slice/subslice/EU in a partially enabled state. We
2060 * must make an explicit request through RPCS for full
2061 * enablement.
2062 */
2063 if (INTEL_INFO(dev)->has_slice_pg) {
2064 rpcs |= GEN8_RPCS_S_CNT_ENABLE;
2065 rpcs |= INTEL_INFO(dev)->slice_total <<
2066 GEN8_RPCS_S_CNT_SHIFT;
2067 rpcs |= GEN8_RPCS_ENABLE;
2068 }
2069
2070 if (INTEL_INFO(dev)->has_subslice_pg) {
2071 rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
2072 rpcs |= INTEL_INFO(dev)->subslice_per_slice <<
2073 GEN8_RPCS_SS_CNT_SHIFT;
2074 rpcs |= GEN8_RPCS_ENABLE;
2075 }
2076
2077 if (INTEL_INFO(dev)->has_eu_pg) {
2078 rpcs |= INTEL_INFO(dev)->eu_per_subslice <<
2079 GEN8_RPCS_EU_MIN_SHIFT;
2080 rpcs |= INTEL_INFO(dev)->eu_per_subslice <<
2081 GEN8_RPCS_EU_MAX_SHIFT;
2082 rpcs |= GEN8_RPCS_ENABLE;
2083 }
2084
2085 return rpcs;
2086}
2087
8670d6f9
OM
2088static int
2089populate_lr_context(struct intel_context *ctx, struct drm_i915_gem_object *ctx_obj,
2090 struct intel_engine_cs *ring, struct intel_ringbuffer *ringbuf)
2091{
2d965536
TD
2092 struct drm_device *dev = ring->dev;
2093 struct drm_i915_private *dev_priv = dev->dev_private;
ae6c4806 2094 struct i915_hw_ppgtt *ppgtt = ctx->ppgtt;
8670d6f9
OM
2095 struct page *page;
2096 uint32_t *reg_state;
2097 int ret;
2098
2d965536
TD
2099 if (!ppgtt)
2100 ppgtt = dev_priv->mm.aliasing_ppgtt;
2101
8670d6f9
OM
2102 ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
2103 if (ret) {
2104 DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
2105 return ret;
2106 }
2107
2108 ret = i915_gem_object_get_pages(ctx_obj);
2109 if (ret) {
2110 DRM_DEBUG_DRIVER("Could not get object pages\n");
2111 return ret;
2112 }
2113
2114 i915_gem_object_pin_pages(ctx_obj);
2115
2116 /* The second page of the context object contains some fields which must
2117 * be set up prior to the first execution. */
2118 page = i915_gem_object_get_page(ctx_obj, 1);
2119 reg_state = kmap_atomic(page);
2120
2121 /* A context is actually a big batch buffer with several MI_LOAD_REGISTER_IMM
2122 * commands followed by (reg, value) pairs. The values we are setting here are
2123 * only for the first context restore: on a subsequent save, the GPU will
2124 * recreate this batchbuffer with new values (including all the missing
2125 * MI_LOAD_REGISTER_IMM commands that we are not initializing here). */
2126 if (ring->id == RCS)
2127 reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(14);
2128 else
2129 reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(11);
2130 reg_state[CTX_LRI_HEADER_0] |= MI_LRI_FORCE_POSTED;
2131 reg_state[CTX_CONTEXT_CONTROL] = RING_CONTEXT_CONTROL(ring);
2132 reg_state[CTX_CONTEXT_CONTROL+1] =
5baa22c5 2133 _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
6922528a
AJ
2134 CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
2135 CTX_CTRL_RS_CTX_ENABLE);
8670d6f9
OM
2136 reg_state[CTX_RING_HEAD] = RING_HEAD(ring->mmio_base);
2137 reg_state[CTX_RING_HEAD+1] = 0;
2138 reg_state[CTX_RING_TAIL] = RING_TAIL(ring->mmio_base);
2139 reg_state[CTX_RING_TAIL+1] = 0;
2140 reg_state[CTX_RING_BUFFER_START] = RING_START(ring->mmio_base);
7ba717cf
TD
2141 /* Ring buffer start address is not known until the buffer is pinned.
2142 * It is written to the context image in execlists_update_context()
2143 */
8670d6f9
OM
2144 reg_state[CTX_RING_BUFFER_CONTROL] = RING_CTL(ring->mmio_base);
2145 reg_state[CTX_RING_BUFFER_CONTROL+1] =
2146 ((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES) | RING_VALID;
2147 reg_state[CTX_BB_HEAD_U] = ring->mmio_base + 0x168;
2148 reg_state[CTX_BB_HEAD_U+1] = 0;
2149 reg_state[CTX_BB_HEAD_L] = ring->mmio_base + 0x140;
2150 reg_state[CTX_BB_HEAD_L+1] = 0;
2151 reg_state[CTX_BB_STATE] = ring->mmio_base + 0x110;
2152 reg_state[CTX_BB_STATE+1] = (1<<5);
2153 reg_state[CTX_SECOND_BB_HEAD_U] = ring->mmio_base + 0x11c;
2154 reg_state[CTX_SECOND_BB_HEAD_U+1] = 0;
2155 reg_state[CTX_SECOND_BB_HEAD_L] = ring->mmio_base + 0x114;
2156 reg_state[CTX_SECOND_BB_HEAD_L+1] = 0;
2157 reg_state[CTX_SECOND_BB_STATE] = ring->mmio_base + 0x118;
2158 reg_state[CTX_SECOND_BB_STATE+1] = 0;
2159 if (ring->id == RCS) {
8670d6f9
OM
2160 reg_state[CTX_BB_PER_CTX_PTR] = ring->mmio_base + 0x1c0;
2161 reg_state[CTX_BB_PER_CTX_PTR+1] = 0;
2162 reg_state[CTX_RCS_INDIRECT_CTX] = ring->mmio_base + 0x1c4;
2163 reg_state[CTX_RCS_INDIRECT_CTX+1] = 0;
2164 reg_state[CTX_RCS_INDIRECT_CTX_OFFSET] = ring->mmio_base + 0x1c8;
2165 reg_state[CTX_RCS_INDIRECT_CTX_OFFSET+1] = 0;
17ee950d
AS
2166 if (ring->wa_ctx.obj) {
2167 struct i915_ctx_workarounds *wa_ctx = &ring->wa_ctx;
2168 uint32_t ggtt_offset = i915_gem_obj_ggtt_offset(wa_ctx->obj);
2169
2170 reg_state[CTX_RCS_INDIRECT_CTX+1] =
2171 (ggtt_offset + wa_ctx->indirect_ctx.offset * sizeof(uint32_t)) |
2172 (wa_ctx->indirect_ctx.size / CACHELINE_DWORDS);
2173
2174 reg_state[CTX_RCS_INDIRECT_CTX_OFFSET+1] =
2175 CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT << 6;
2176
2177 reg_state[CTX_BB_PER_CTX_PTR+1] =
2178 (ggtt_offset + wa_ctx->per_ctx.offset * sizeof(uint32_t)) |
2179 0x01;
2180 }
8670d6f9
OM
2181 }
2182 reg_state[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9);
2183 reg_state[CTX_LRI_HEADER_1] |= MI_LRI_FORCE_POSTED;
2184 reg_state[CTX_CTX_TIMESTAMP] = ring->mmio_base + 0x3a8;
2185 reg_state[CTX_CTX_TIMESTAMP+1] = 0;
2186 reg_state[CTX_PDP3_UDW] = GEN8_RING_PDP_UDW(ring, 3);
2187 reg_state[CTX_PDP3_LDW] = GEN8_RING_PDP_LDW(ring, 3);
2188 reg_state[CTX_PDP2_UDW] = GEN8_RING_PDP_UDW(ring, 2);
2189 reg_state[CTX_PDP2_LDW] = GEN8_RING_PDP_LDW(ring, 2);
2190 reg_state[CTX_PDP1_UDW] = GEN8_RING_PDP_UDW(ring, 1);
2191 reg_state[CTX_PDP1_LDW] = GEN8_RING_PDP_LDW(ring, 1);
2192 reg_state[CTX_PDP0_UDW] = GEN8_RING_PDP_UDW(ring, 0);
2193 reg_state[CTX_PDP0_LDW] = GEN8_RING_PDP_LDW(ring, 0);
d7b2633d
MT
2194
2195 /* With dynamic page allocation, PDPs may not be allocated at this point,
2196 * Point the unallocated PDPs to the scratch page
e5815a2e
MT
2197 */
2198 ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
2199 ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
2200 ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
2201 ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
8670d6f9
OM
2202 if (ring->id == RCS) {
2203 reg_state[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
0cea6502
JM
2204 reg_state[CTX_R_PWR_CLK_STATE] = GEN8_R_PWR_CLK_STATE;
2205 reg_state[CTX_R_PWR_CLK_STATE+1] = make_rpcs(dev);
8670d6f9
OM
2206 }
2207
2208 kunmap_atomic(reg_state);
2209
2210 ctx_obj->dirty = 1;
2211 set_page_dirty(page);
2212 i915_gem_object_unpin_pages(ctx_obj);
2213
2214 return 0;
2215}
2216
73e4d07f
OM
2217/**
2218 * intel_lr_context_free() - free the LRC specific bits of a context
2219 * @ctx: the LR context to free.
2220 *
2221 * The real context freeing is done in i915_gem_context_free: this only
2222 * takes care of the bits that are LRC related: the per-engine backing
2223 * objects and the logical ringbuffer.
2224 */
ede7d42b
OM
2225void intel_lr_context_free(struct intel_context *ctx)
2226{
8c857917
OM
2227 int i;
2228
2229 for (i = 0; i < I915_NUM_RINGS; i++) {
2230 struct drm_i915_gem_object *ctx_obj = ctx->engine[i].state;
84c2377f 2231
8c857917 2232 if (ctx_obj) {
dcb4c12a
OM
2233 struct intel_ringbuffer *ringbuf =
2234 ctx->engine[i].ringbuf;
2235 struct intel_engine_cs *ring = ringbuf->ring;
2236
7ba717cf
TD
2237 if (ctx == ring->default_context) {
2238 intel_unpin_ringbuffer_obj(ringbuf);
2239 i915_gem_object_ggtt_unpin(ctx_obj);
2240 }
a7cbedec 2241 WARN_ON(ctx->engine[ring->id].pin_count);
84c2377f
OM
2242 intel_destroy_ringbuffer_obj(ringbuf);
2243 kfree(ringbuf);
8c857917
OM
2244 drm_gem_object_unreference(&ctx_obj->base);
2245 }
2246 }
2247}
2248
2249static uint32_t get_lr_context_size(struct intel_engine_cs *ring)
2250{
2251 int ret = 0;
2252
468c6816 2253 WARN_ON(INTEL_INFO(ring->dev)->gen < 8);
8c857917
OM
2254
2255 switch (ring->id) {
2256 case RCS:
468c6816
MN
2257 if (INTEL_INFO(ring->dev)->gen >= 9)
2258 ret = GEN9_LR_CONTEXT_RENDER_SIZE;
2259 else
2260 ret = GEN8_LR_CONTEXT_RENDER_SIZE;
8c857917
OM
2261 break;
2262 case VCS:
2263 case BCS:
2264 case VECS:
2265 case VCS2:
2266 ret = GEN8_LR_CONTEXT_OTHER_SIZE;
2267 break;
2268 }
2269
2270 return ret;
ede7d42b
OM
2271}
2272
70b0ea86 2273static void lrc_setup_hardware_status_page(struct intel_engine_cs *ring,
1df06b75
TD
2274 struct drm_i915_gem_object *default_ctx_obj)
2275{
2276 struct drm_i915_private *dev_priv = ring->dev->dev_private;
2277
2278 /* The status page is offset 0 from the default context object
2279 * in LRC mode. */
2280 ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(default_ctx_obj);
2281 ring->status_page.page_addr =
2282 kmap(sg_page(default_ctx_obj->pages->sgl));
1df06b75
TD
2283 ring->status_page.obj = default_ctx_obj;
2284
2285 I915_WRITE(RING_HWS_PGA(ring->mmio_base),
2286 (u32)ring->status_page.gfx_addr);
2287 POSTING_READ(RING_HWS_PGA(ring->mmio_base));
1df06b75
TD
2288}
2289
73e4d07f
OM
2290/**
2291 * intel_lr_context_deferred_create() - create the LRC specific bits of a context
2292 * @ctx: LR context to create.
2293 * @ring: engine to be used with the context.
2294 *
2295 * This function can be called more than once, with different engines, if we plan
2296 * to use the context with them. The context backing objects and the ringbuffers
2297 * (specially the ringbuffer backing objects) suck a lot of memory up, and that's why
2298 * the creation is a deferred call: it's better to make sure first that we need to use
2299 * a given ring with the context.
2300 *
32197aab 2301 * Return: non-zero on error.
73e4d07f 2302 */
ede7d42b
OM
2303int intel_lr_context_deferred_create(struct intel_context *ctx,
2304 struct intel_engine_cs *ring)
2305{
dcb4c12a 2306 const bool is_global_default_ctx = (ctx == ring->default_context);
8c857917
OM
2307 struct drm_device *dev = ring->dev;
2308 struct drm_i915_gem_object *ctx_obj;
2309 uint32_t context_size;
84c2377f 2310 struct intel_ringbuffer *ringbuf;
8c857917
OM
2311 int ret;
2312
ede7d42b 2313 WARN_ON(ctx->legacy_hw_ctx.rcs_state != NULL);
bfc882b4 2314 WARN_ON(ctx->engine[ring->id].state);
ede7d42b 2315
8c857917
OM
2316 context_size = round_up(get_lr_context_size(ring), 4096);
2317
149c86e7 2318 ctx_obj = i915_gem_alloc_object(dev, context_size);
3126a660
DC
2319 if (!ctx_obj) {
2320 DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
2321 return -ENOMEM;
8c857917
OM
2322 }
2323
dcb4c12a
OM
2324 if (is_global_default_ctx) {
2325 ret = i915_gem_obj_ggtt_pin(ctx_obj, GEN8_LR_CONTEXT_ALIGN, 0);
2326 if (ret) {
2327 DRM_DEBUG_DRIVER("Pin LRC backing obj failed: %d\n",
2328 ret);
2329 drm_gem_object_unreference(&ctx_obj->base);
2330 return ret;
2331 }
8c857917
OM
2332 }
2333
84c2377f
OM
2334 ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
2335 if (!ringbuf) {
2336 DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s\n",
2337 ring->name);
84c2377f 2338 ret = -ENOMEM;
7ba717cf 2339 goto error_unpin_ctx;
84c2377f
OM
2340 }
2341
0c7dd53b 2342 ringbuf->ring = ring;
582d67f0 2343
84c2377f
OM
2344 ringbuf->size = 32 * PAGE_SIZE;
2345 ringbuf->effective_size = ringbuf->size;
2346 ringbuf->head = 0;
2347 ringbuf->tail = 0;
84c2377f 2348 ringbuf->last_retired_head = -1;
ebd0fd4b 2349 intel_ring_update_space(ringbuf);
84c2377f 2350
7ba717cf
TD
2351 if (ringbuf->obj == NULL) {
2352 ret = intel_alloc_ringbuffer_obj(dev, ringbuf);
2353 if (ret) {
2354 DRM_DEBUG_DRIVER(
2355 "Failed to allocate ringbuffer obj %s: %d\n",
84c2377f 2356 ring->name, ret);
7ba717cf
TD
2357 goto error_free_rbuf;
2358 }
2359
2360 if (is_global_default_ctx) {
2361 ret = intel_pin_and_map_ringbuffer_obj(dev, ringbuf);
2362 if (ret) {
2363 DRM_ERROR(
2364 "Failed to pin and map ringbuffer %s: %d\n",
2365 ring->name, ret);
2366 goto error_destroy_rbuf;
2367 }
2368 }
2369
8670d6f9
OM
2370 }
2371
2372 ret = populate_lr_context(ctx, ctx_obj, ring, ringbuf);
2373 if (ret) {
2374 DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
8670d6f9 2375 goto error;
84c2377f
OM
2376 }
2377
2378 ctx->engine[ring->id].ringbuf = ringbuf;
8c857917 2379 ctx->engine[ring->id].state = ctx_obj;
ede7d42b 2380
70b0ea86
DV
2381 if (ctx == ring->default_context)
2382 lrc_setup_hardware_status_page(ring, ctx_obj);
e7778be1 2383 else if (ring->id == RCS && !ctx->rcs_initialized) {
771b9a53 2384 if (ring->init_context) {
76c39168
JH
2385 struct drm_i915_gem_request *req;
2386
2387 ret = i915_gem_request_alloc(ring, ctx, &req);
2388 if (ret)
2389 return ret;
2390
8753181e 2391 ret = ring->init_context(req);
e7778be1 2392 if (ret) {
771b9a53 2393 DRM_ERROR("ring init context: %d\n", ret);
76c39168 2394 i915_gem_request_cancel(req);
e7778be1
TD
2395 ctx->engine[ring->id].ringbuf = NULL;
2396 ctx->engine[ring->id].state = NULL;
2397 goto error;
2398 }
76c39168 2399
75289874 2400 i915_add_request_no_flush(req);
771b9a53
MT
2401 }
2402
564ddb2f
OM
2403 ctx->rcs_initialized = true;
2404 }
2405
ede7d42b 2406 return 0;
8670d6f9
OM
2407
2408error:
7ba717cf
TD
2409 if (is_global_default_ctx)
2410 intel_unpin_ringbuffer_obj(ringbuf);
2411error_destroy_rbuf:
2412 intel_destroy_ringbuffer_obj(ringbuf);
2413error_free_rbuf:
8670d6f9 2414 kfree(ringbuf);
7ba717cf 2415error_unpin_ctx:
dcb4c12a
OM
2416 if (is_global_default_ctx)
2417 i915_gem_object_ggtt_unpin(ctx_obj);
8670d6f9
OM
2418 drm_gem_object_unreference(&ctx_obj->base);
2419 return ret;
ede7d42b 2420}
3e5b6f05
TD
2421
2422void intel_lr_context_reset(struct drm_device *dev,
2423 struct intel_context *ctx)
2424{
2425 struct drm_i915_private *dev_priv = dev->dev_private;
2426 struct intel_engine_cs *ring;
2427 int i;
2428
2429 for_each_ring(ring, dev_priv, i) {
2430 struct drm_i915_gem_object *ctx_obj =
2431 ctx->engine[ring->id].state;
2432 struct intel_ringbuffer *ringbuf =
2433 ctx->engine[ring->id].ringbuf;
2434 uint32_t *reg_state;
2435 struct page *page;
2436
2437 if (!ctx_obj)
2438 continue;
2439
2440 if (i915_gem_object_get_pages(ctx_obj)) {
2441 WARN(1, "Failed get_pages for context obj\n");
2442 continue;
2443 }
2444 page = i915_gem_object_get_page(ctx_obj, 1);
2445 reg_state = kmap_atomic(page);
2446
2447 reg_state[CTX_RING_HEAD+1] = 0;
2448 reg_state[CTX_RING_TAIL+1] = 0;
2449
2450 kunmap_atomic(reg_state);
2451
2452 ringbuf->head = 0;
2453 ringbuf->tail = 0;
2454 }
2455}
This page took 0.393893 seconds and 5 git commands to generate.