2 * Copyright © 2014 Intel Corporation
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:
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
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
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>
32 * DOC: Logical Rings, Logical Ring Contexts and Execlists
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).
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:
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.
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.
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".
60 * Regarding the creation of contexts, we have:
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.
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:
70 * - One ringbuffer per-engine inside each context.
71 * - One backing object per-engine inside each context.
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
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
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).
89 * Execlists implementation:
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).
92 * This method works as follows:
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
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.
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.
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.
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).
135 #include <drm/drmP.h>
136 #include <drm/i915_drm.h>
137 #include "i915_drv.h"
139 #define GEN9_LR_CONTEXT_RENDER_SIZE (22 * PAGE_SIZE)
140 #define GEN8_LR_CONTEXT_RENDER_SIZE (20 * PAGE_SIZE)
141 #define GEN8_LR_CONTEXT_OTHER_SIZE (2 * PAGE_SIZE)
143 #define RING_EXECLIST_QFULL (1 << 0x2)
144 #define RING_EXECLIST1_VALID (1 << 0x3)
145 #define RING_EXECLIST0_VALID (1 << 0x4)
146 #define RING_EXECLIST_ACTIVE_STATUS (3 << 0xE)
147 #define RING_EXECLIST1_ACTIVE (1 << 0x11)
148 #define RING_EXECLIST0_ACTIVE (1 << 0x12)
150 #define GEN8_CTX_STATUS_IDLE_ACTIVE (1 << 0)
151 #define GEN8_CTX_STATUS_PREEMPTED (1 << 1)
152 #define GEN8_CTX_STATUS_ELEMENT_SWITCH (1 << 2)
153 #define GEN8_CTX_STATUS_ACTIVE_IDLE (1 << 3)
154 #define GEN8_CTX_STATUS_COMPLETE (1 << 4)
155 #define GEN8_CTX_STATUS_LITE_RESTORE (1 << 15)
157 #define CTX_LRI_HEADER_0 0x01
158 #define CTX_CONTEXT_CONTROL 0x02
159 #define CTX_RING_HEAD 0x04
160 #define CTX_RING_TAIL 0x06
161 #define CTX_RING_BUFFER_START 0x08
162 #define CTX_RING_BUFFER_CONTROL 0x0a
163 #define CTX_BB_HEAD_U 0x0c
164 #define CTX_BB_HEAD_L 0x0e
165 #define CTX_BB_STATE 0x10
166 #define CTX_SECOND_BB_HEAD_U 0x12
167 #define CTX_SECOND_BB_HEAD_L 0x14
168 #define CTX_SECOND_BB_STATE 0x16
169 #define CTX_BB_PER_CTX_PTR 0x18
170 #define CTX_RCS_INDIRECT_CTX 0x1a
171 #define CTX_RCS_INDIRECT_CTX_OFFSET 0x1c
172 #define CTX_LRI_HEADER_1 0x21
173 #define CTX_CTX_TIMESTAMP 0x22
174 #define CTX_PDP3_UDW 0x24
175 #define CTX_PDP3_LDW 0x26
176 #define CTX_PDP2_UDW 0x28
177 #define CTX_PDP2_LDW 0x2a
178 #define CTX_PDP1_UDW 0x2c
179 #define CTX_PDP1_LDW 0x2e
180 #define CTX_PDP0_UDW 0x30
181 #define CTX_PDP0_LDW 0x32
182 #define CTX_LRI_HEADER_2 0x41
183 #define CTX_R_PWR_CLK_STATE 0x42
184 #define CTX_GPGPU_CSR_BASE_ADDRESS 0x44
186 #define GEN8_CTX_VALID (1<<0)
187 #define GEN8_CTX_FORCE_PD_RESTORE (1<<1)
188 #define GEN8_CTX_FORCE_RESTORE (1<<2)
189 #define GEN8_CTX_L3LLC_COHERENT (1<<5)
190 #define GEN8_CTX_PRIVILEGE (1<<8)
192 #define ASSIGN_CTX_PDP(ppgtt, reg_state, n) { \
193 const u64 _addr = i915_page_dir_dma_addr((ppgtt), (n)); \
194 reg_state[CTX_PDP ## n ## _UDW+1] = upper_32_bits(_addr); \
195 reg_state[CTX_PDP ## n ## _LDW+1] = lower_32_bits(_addr); \
199 ADVANCED_CONTEXT
= 0,
204 #define GEN8_CTX_MODE_SHIFT 3
207 FAULT_AND_HALT
, /* Debug only */
209 FAULT_AND_CONTINUE
/* Unsupported */
211 #define GEN8_CTX_ID_SHIFT 32
212 #define CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x17
214 static int intel_lr_context_pin(struct intel_engine_cs
*ring
,
215 struct intel_context
*ctx
);
218 * intel_sanitize_enable_execlists() - sanitize i915.enable_execlists
220 * @enable_execlists: value of i915.enable_execlists module parameter.
222 * Only certain platforms support Execlists (the prerequisites being
223 * support for Logical Ring Contexts and Aliasing PPGTT or better).
225 * Return: 1 if Execlists is supported and has to be enabled.
227 int intel_sanitize_enable_execlists(struct drm_device
*dev
, int enable_execlists
)
229 WARN_ON(i915
.enable_ppgtt
== -1);
231 if (INTEL_INFO(dev
)->gen
>= 9)
234 if (enable_execlists
== 0)
237 if (HAS_LOGICAL_RING_CONTEXTS(dev
) && USES_PPGTT(dev
) &&
238 i915
.use_mmio_flip
>= 0)
245 * intel_execlists_ctx_id() - get the Execlists Context ID
246 * @ctx_obj: Logical Ring Context backing object.
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
254 * Return: 20-bits globally unique context ID.
256 u32
intel_execlists_ctx_id(struct drm_i915_gem_object
*ctx_obj
)
258 u32 lrca
= i915_gem_obj_ggtt_offset(ctx_obj
);
260 /* LRCA is required to be 4K aligned so the more significant 20 bits
261 * are globally unique */
265 static uint64_t execlists_ctx_descriptor(struct intel_engine_cs
*ring
,
266 struct drm_i915_gem_object
*ctx_obj
)
268 struct drm_device
*dev
= ring
->dev
;
270 uint64_t lrca
= i915_gem_obj_ggtt_offset(ctx_obj
);
272 WARN_ON(lrca
& 0xFFFFFFFF00000FFFULL
);
274 desc
= GEN8_CTX_VALID
;
275 desc
|= LEGACY_CONTEXT
<< GEN8_CTX_MODE_SHIFT
;
276 if (IS_GEN8(ctx_obj
->base
.dev
))
277 desc
|= GEN8_CTX_L3LLC_COHERENT
;
278 desc
|= GEN8_CTX_PRIVILEGE
;
280 desc
|= (u64
)intel_execlists_ctx_id(ctx_obj
) << GEN8_CTX_ID_SHIFT
;
282 /* TODO: WaDisableLiteRestore when we start using semaphore
283 * signalling between Command Streamers */
284 /* desc |= GEN8_CTX_FORCE_RESTORE; */
286 /* WaEnableForceRestoreInCtxtDescForVCS:skl */
288 INTEL_REVID(dev
) <= SKL_REVID_B0
&&
289 (ring
->id
== BCS
|| ring
->id
== VCS
||
290 ring
->id
== VECS
|| ring
->id
== VCS2
))
291 desc
|= GEN8_CTX_FORCE_RESTORE
;
296 static void execlists_elsp_write(struct intel_engine_cs
*ring
,
297 struct drm_i915_gem_object
*ctx_obj0
,
298 struct drm_i915_gem_object
*ctx_obj1
)
300 struct drm_device
*dev
= ring
->dev
;
301 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
305 /* XXX: You must always write both descriptors in the order below. */
307 temp
= execlists_ctx_descriptor(ring
, ctx_obj1
);
310 desc
[1] = (u32
)(temp
>> 32);
313 temp
= execlists_ctx_descriptor(ring
, ctx_obj0
);
314 desc
[3] = (u32
)(temp
>> 32);
317 spin_lock(&dev_priv
->uncore
.lock
);
318 intel_uncore_forcewake_get__locked(dev_priv
, FORCEWAKE_ALL
);
319 I915_WRITE_FW(RING_ELSP(ring
), desc
[1]);
320 I915_WRITE_FW(RING_ELSP(ring
), desc
[0]);
321 I915_WRITE_FW(RING_ELSP(ring
), desc
[3]);
323 /* The context is automatically loaded after the following */
324 I915_WRITE_FW(RING_ELSP(ring
), desc
[2]);
326 /* ELSP is a wo register, so use another nearby reg for posting instead */
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
);
332 static int execlists_update_context(struct drm_i915_gem_object
*ctx_obj
,
333 struct drm_i915_gem_object
*ring_obj
,
334 struct i915_hw_ppgtt
*ppgtt
,
340 page
= i915_gem_object_get_page(ctx_obj
, 1);
341 reg_state
= kmap_atomic(page
);
343 reg_state
[CTX_RING_TAIL
+1] = tail
;
344 reg_state
[CTX_RING_BUFFER_START
+1] = i915_gem_obj_ggtt_offset(ring_obj
);
346 /* True PPGTT with dynamic page allocation: update PDP registers and
347 * point the unallocated PDPs to the scratch page
350 ASSIGN_CTX_PDP(ppgtt
, reg_state
, 3);
351 ASSIGN_CTX_PDP(ppgtt
, reg_state
, 2);
352 ASSIGN_CTX_PDP(ppgtt
, reg_state
, 1);
353 ASSIGN_CTX_PDP(ppgtt
, reg_state
, 0);
356 kunmap_atomic(reg_state
);
361 static void execlists_submit_contexts(struct intel_engine_cs
*ring
,
362 struct intel_context
*to0
, u32 tail0
,
363 struct intel_context
*to1
, u32 tail1
)
365 struct drm_i915_gem_object
*ctx_obj0
= to0
->engine
[ring
->id
].state
;
366 struct intel_ringbuffer
*ringbuf0
= to0
->engine
[ring
->id
].ringbuf
;
367 struct drm_i915_gem_object
*ctx_obj1
= NULL
;
368 struct intel_ringbuffer
*ringbuf1
= NULL
;
371 WARN_ON(!i915_gem_obj_is_pinned(ctx_obj0
));
372 WARN_ON(!i915_gem_obj_is_pinned(ringbuf0
->obj
));
374 execlists_update_context(ctx_obj0
, ringbuf0
->obj
, to0
->ppgtt
, tail0
);
377 ringbuf1
= to1
->engine
[ring
->id
].ringbuf
;
378 ctx_obj1
= to1
->engine
[ring
->id
].state
;
380 WARN_ON(!i915_gem_obj_is_pinned(ctx_obj1
));
381 WARN_ON(!i915_gem_obj_is_pinned(ringbuf1
->obj
));
383 execlists_update_context(ctx_obj1
, ringbuf1
->obj
, to1
->ppgtt
, tail1
);
386 execlists_elsp_write(ring
, ctx_obj0
, ctx_obj1
);
389 static void execlists_context_unqueue(struct intel_engine_cs
*ring
)
391 struct drm_i915_gem_request
*req0
= NULL
, *req1
= NULL
;
392 struct drm_i915_gem_request
*cursor
= NULL
, *tmp
= NULL
;
394 assert_spin_locked(&ring
->execlist_lock
);
397 * If irqs are not active generate a warning as batches that finish
398 * without the irqs may get lost and a GPU Hang may occur.
400 WARN_ON(!intel_irqs_enabled(ring
->dev
->dev_private
));
402 if (list_empty(&ring
->execlist_queue
))
405 /* Try to read in pairs */
406 list_for_each_entry_safe(cursor
, tmp
, &ring
->execlist_queue
,
410 } else if (req0
->ctx
== cursor
->ctx
) {
411 /* Same ctx: ignore first request, as second request
412 * will update tail past first request's workload */
413 cursor
->elsp_submitted
= req0
->elsp_submitted
;
414 list_del(&req0
->execlist_link
);
415 list_add_tail(&req0
->execlist_link
,
416 &ring
->execlist_retired_req_list
);
424 if (IS_GEN8(ring
->dev
) || IS_GEN9(ring
->dev
)) {
426 * WaIdleLiteRestore: make sure we never cause a lite
427 * restore with HEAD==TAIL
429 if (req0
->elsp_submitted
) {
431 * Apply the wa NOOPS to prevent ring:HEAD == req:TAIL
432 * as we resubmit the request. See gen8_emit_request()
433 * for where we prepare the padding after the end of the
436 struct intel_ringbuffer
*ringbuf
;
438 ringbuf
= req0
->ctx
->engine
[ring
->id
].ringbuf
;
440 req0
->tail
&= ringbuf
->size
- 1;
444 WARN_ON(req1
&& req1
->elsp_submitted
);
446 execlists_submit_contexts(ring
, req0
->ctx
, req0
->tail
,
447 req1
? req1
->ctx
: NULL
,
448 req1
? req1
->tail
: 0);
450 req0
->elsp_submitted
++;
452 req1
->elsp_submitted
++;
455 static bool execlists_check_remove_request(struct intel_engine_cs
*ring
,
458 struct drm_i915_gem_request
*head_req
;
460 assert_spin_locked(&ring
->execlist_lock
);
462 head_req
= list_first_entry_or_null(&ring
->execlist_queue
,
463 struct drm_i915_gem_request
,
466 if (head_req
!= NULL
) {
467 struct drm_i915_gem_object
*ctx_obj
=
468 head_req
->ctx
->engine
[ring
->id
].state
;
469 if (intel_execlists_ctx_id(ctx_obj
) == request_id
) {
470 WARN(head_req
->elsp_submitted
== 0,
471 "Never submitted head request\n");
473 if (--head_req
->elsp_submitted
<= 0) {
474 list_del(&head_req
->execlist_link
);
475 list_add_tail(&head_req
->execlist_link
,
476 &ring
->execlist_retired_req_list
);
486 * intel_lrc_irq_handler() - handle Context Switch interrupts
487 * @ring: Engine Command Streamer to handle.
489 * Check the unread Context Status Buffers and manage the submission of new
490 * contexts to the ELSP accordingly.
492 void intel_lrc_irq_handler(struct intel_engine_cs
*ring
)
494 struct drm_i915_private
*dev_priv
= ring
->dev
->dev_private
;
500 u32 submit_contexts
= 0;
502 status_pointer
= I915_READ(RING_CONTEXT_STATUS_PTR(ring
));
504 read_pointer
= ring
->next_context_status_buffer
;
505 write_pointer
= status_pointer
& 0x07;
506 if (read_pointer
> write_pointer
)
509 spin_lock(&ring
->execlist_lock
);
511 while (read_pointer
< write_pointer
) {
513 status
= I915_READ(RING_CONTEXT_STATUS_BUF(ring
) +
514 (read_pointer
% 6) * 8);
515 status_id
= I915_READ(RING_CONTEXT_STATUS_BUF(ring
) +
516 (read_pointer
% 6) * 8 + 4);
518 if (status
& GEN8_CTX_STATUS_PREEMPTED
) {
519 if (status
& GEN8_CTX_STATUS_LITE_RESTORE
) {
520 if (execlists_check_remove_request(ring
, status_id
))
521 WARN(1, "Lite Restored request removed from queue\n");
523 WARN(1, "Preemption without Lite Restore\n");
526 if ((status
& GEN8_CTX_STATUS_ACTIVE_IDLE
) ||
527 (status
& GEN8_CTX_STATUS_ELEMENT_SWITCH
)) {
528 if (execlists_check_remove_request(ring
, status_id
))
533 if (submit_contexts
!= 0)
534 execlists_context_unqueue(ring
);
536 spin_unlock(&ring
->execlist_lock
);
538 WARN(submit_contexts
> 2, "More than two context complete events?\n");
539 ring
->next_context_status_buffer
= write_pointer
% 6;
541 I915_WRITE(RING_CONTEXT_STATUS_PTR(ring
),
542 ((u32
)ring
->next_context_status_buffer
& 0x07) << 8);
545 static int execlists_context_queue(struct drm_i915_gem_request
*request
)
547 struct intel_engine_cs
*ring
= request
->ring
;
548 struct drm_i915_gem_request
*cursor
;
549 int num_elements
= 0;
551 if (request
->ctx
!= ring
->default_context
)
552 intel_lr_context_pin(ring
, request
->ctx
);
554 i915_gem_request_reference(request
);
556 request
->tail
= request
->ringbuf
->tail
;
558 spin_lock_irq(&ring
->execlist_lock
);
560 list_for_each_entry(cursor
, &ring
->execlist_queue
, execlist_link
)
561 if (++num_elements
> 2)
564 if (num_elements
> 2) {
565 struct drm_i915_gem_request
*tail_req
;
567 tail_req
= list_last_entry(&ring
->execlist_queue
,
568 struct drm_i915_gem_request
,
571 if (request
->ctx
== tail_req
->ctx
) {
572 WARN(tail_req
->elsp_submitted
!= 0,
573 "More than 2 already-submitted reqs queued\n");
574 list_del(&tail_req
->execlist_link
);
575 list_add_tail(&tail_req
->execlist_link
,
576 &ring
->execlist_retired_req_list
);
580 list_add_tail(&request
->execlist_link
, &ring
->execlist_queue
);
581 if (num_elements
== 0)
582 execlists_context_unqueue(ring
);
584 spin_unlock_irq(&ring
->execlist_lock
);
589 static int logical_ring_invalidate_all_caches(struct drm_i915_gem_request
*req
)
591 struct intel_engine_cs
*ring
= req
->ring
;
592 uint32_t flush_domains
;
596 if (ring
->gpu_caches_dirty
)
597 flush_domains
= I915_GEM_GPU_DOMAINS
;
599 ret
= ring
->emit_flush(req
, I915_GEM_GPU_DOMAINS
, flush_domains
);
603 ring
->gpu_caches_dirty
= false;
607 static int execlists_move_to_gpu(struct drm_i915_gem_request
*req
,
608 struct list_head
*vmas
)
610 const unsigned other_rings
= ~intel_ring_flag(req
->ring
);
611 struct i915_vma
*vma
;
612 uint32_t flush_domains
= 0;
613 bool flush_chipset
= false;
616 list_for_each_entry(vma
, vmas
, exec_list
) {
617 struct drm_i915_gem_object
*obj
= vma
->obj
;
619 if (obj
->active
& other_rings
) {
620 ret
= i915_gem_object_sync(obj
, req
->ring
, &req
);
625 if (obj
->base
.write_domain
& I915_GEM_DOMAIN_CPU
)
626 flush_chipset
|= i915_gem_clflush_object(obj
, false);
628 flush_domains
|= obj
->base
.write_domain
;
631 if (flush_domains
& I915_GEM_DOMAIN_GTT
)
634 /* Unconditionally invalidate gpu caches and ensure that we do flush
635 * any residual writes from the previous batch.
637 return logical_ring_invalidate_all_caches(req
);
640 int intel_logical_ring_alloc_request_extras(struct drm_i915_gem_request
*request
)
644 if (request
->ctx
!= request
->ring
->default_context
) {
645 ret
= intel_lr_context_pin(request
->ring
, request
->ctx
);
650 request
->ringbuf
= request
->ctx
->engine
[request
->ring
->id
].ringbuf
;
655 static int logical_ring_wait_for_space(struct drm_i915_gem_request
*req
,
658 struct intel_ringbuffer
*ringbuf
= req
->ringbuf
;
659 struct intel_engine_cs
*ring
= req
->ring
;
660 struct drm_i915_gem_request
*target
;
664 if (intel_ring_space(ringbuf
) >= bytes
)
667 /* The whole point of reserving space is to not wait! */
668 WARN_ON(ringbuf
->reserved_in_use
);
670 list_for_each_entry(target
, &ring
->request_list
, list
) {
672 * The request queue is per-engine, so can contain requests
673 * from multiple ringbuffers. Here, we must ignore any that
674 * aren't from the ringbuffer we're considering.
676 if (target
->ringbuf
!= ringbuf
)
679 /* Would completion of this request free enough space? */
680 space
= __intel_ring_space(target
->postfix
, ringbuf
->tail
,
686 if (WARN_ON(&target
->list
== &ring
->request_list
))
689 ret
= i915_wait_request(target
);
693 ringbuf
->space
= space
;
698 * intel_logical_ring_advance_and_submit() - advance the tail and submit the workload
699 * @request: Request to advance the logical ringbuffer of.
701 * The tail is updated in our logical ringbuffer struct, not in the actual context. What
702 * really happens during submission is that the context and current tail will be placed
703 * on a queue waiting for the ELSP to be ready to accept a new context submission. At that
704 * point, the tail *inside* the context is updated and the ELSP written to.
707 intel_logical_ring_advance_and_submit(struct drm_i915_gem_request
*request
)
709 struct intel_engine_cs
*ring
= request
->ring
;
711 intel_logical_ring_advance(request
->ringbuf
);
713 if (intel_ring_stopped(ring
))
716 execlists_context_queue(request
);
719 static void __wrap_ring_buffer(struct intel_ringbuffer
*ringbuf
)
721 uint32_t __iomem
*virt
;
722 int rem
= ringbuf
->size
- ringbuf
->tail
;
724 virt
= ringbuf
->virtual_start
+ ringbuf
->tail
;
727 iowrite32(MI_NOOP
, virt
++);
730 intel_ring_update_space(ringbuf
);
733 static int logical_ring_prepare(struct drm_i915_gem_request
*req
, int bytes
)
735 struct intel_ringbuffer
*ringbuf
= req
->ringbuf
;
736 int remain_usable
= ringbuf
->effective_size
- ringbuf
->tail
;
737 int remain_actual
= ringbuf
->size
- ringbuf
->tail
;
738 int ret
, total_bytes
, wait_bytes
= 0;
739 bool need_wrap
= false;
741 if (ringbuf
->reserved_in_use
)
744 total_bytes
= bytes
+ ringbuf
->reserved_size
;
746 if (unlikely(bytes
> remain_usable
)) {
748 * Not enough space for the basic request. So need to flush
749 * out the remainder and then wait for base + reserved.
751 wait_bytes
= remain_actual
+ total_bytes
;
754 if (unlikely(total_bytes
> remain_usable
)) {
756 * The base request will fit but the reserved space
757 * falls off the end. So only need to to wait for the
758 * reserved size after flushing out the remainder.
760 wait_bytes
= remain_actual
+ ringbuf
->reserved_size
;
762 } else if (total_bytes
> ringbuf
->space
) {
763 /* No wrapping required, just waiting. */
764 wait_bytes
= total_bytes
;
769 ret
= logical_ring_wait_for_space(req
, wait_bytes
);
774 __wrap_ring_buffer(ringbuf
);
781 * intel_logical_ring_begin() - prepare the logical ringbuffer to accept some commands
783 * @request: The request to start some new work for
784 * @ctx: Logical ring context whose ringbuffer is being prepared.
785 * @num_dwords: number of DWORDs that we plan to write to the ringbuffer.
787 * The ringbuffer might not be ready to accept the commands right away (maybe it needs to
788 * be wrapped, or wait a bit for the tail to be updated). This function takes care of that
789 * and also preallocates a request (every workload submission is still mediated through
790 * requests, same as it did with legacy ringbuffer submission).
792 * Return: non-zero if the ringbuffer is not ready to be written to.
794 static int intel_logical_ring_begin(struct drm_i915_gem_request
*req
,
797 struct drm_i915_private
*dev_priv
;
800 WARN_ON(req
== NULL
);
801 dev_priv
= req
->ring
->dev
->dev_private
;
803 ret
= i915_gem_check_wedge(&dev_priv
->gpu_error
,
804 dev_priv
->mm
.interruptible
);
808 ret
= logical_ring_prepare(req
, num_dwords
* sizeof(uint32_t));
812 req
->ringbuf
->space
-= num_dwords
* sizeof(uint32_t);
816 int intel_logical_ring_reserve_space(struct drm_i915_gem_request
*request
)
819 * The first call merely notes the reserve request and is common for
820 * all back ends. The subsequent localised _begin() call actually
821 * ensures that the reservation is available. Without the begin, if
822 * the request creator immediately submitted the request without
823 * adding any commands to it then there might not actually be
824 * sufficient room for the submission commands.
826 intel_ring_reserved_space_reserve(request
->ringbuf
, MIN_SPACE_FOR_ADD_REQUEST
);
828 return intel_logical_ring_begin(request
, 0);
832 * execlists_submission() - submit a batchbuffer for execution, Execlists style
835 * @ring: Engine Command Streamer to submit to.
836 * @ctx: Context to employ for this submission.
837 * @args: execbuffer call arguments.
838 * @vmas: list of vmas.
839 * @batch_obj: the batchbuffer to submit.
840 * @exec_start: batchbuffer start virtual address pointer.
841 * @dispatch_flags: translated execbuffer call flags.
843 * This is the evil twin version of i915_gem_ringbuffer_submission. It abstracts
844 * away the submission details of the execbuffer ioctl call.
846 * Return: non-zero if the submission fails.
848 int intel_execlists_submission(struct i915_execbuffer_params
*params
,
849 struct drm_i915_gem_execbuffer2
*args
,
850 struct list_head
*vmas
)
852 struct drm_device
*dev
= params
->dev
;
853 struct intel_engine_cs
*ring
= params
->ring
;
854 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
855 struct intel_ringbuffer
*ringbuf
= params
->ctx
->engine
[ring
->id
].ringbuf
;
861 instp_mode
= args
->flags
& I915_EXEC_CONSTANTS_MASK
;
862 instp_mask
= I915_EXEC_CONSTANTS_MASK
;
863 switch (instp_mode
) {
864 case I915_EXEC_CONSTANTS_REL_GENERAL
:
865 case I915_EXEC_CONSTANTS_ABSOLUTE
:
866 case I915_EXEC_CONSTANTS_REL_SURFACE
:
867 if (instp_mode
!= 0 && ring
!= &dev_priv
->ring
[RCS
]) {
868 DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
872 if (instp_mode
!= dev_priv
->relative_constants_mode
) {
873 if (instp_mode
== I915_EXEC_CONSTANTS_REL_SURFACE
) {
874 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
878 /* The HW changed the meaning on this bit on gen6 */
879 instp_mask
&= ~I915_EXEC_CONSTANTS_REL_SURFACE
;
883 DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode
);
887 if (args
->num_cliprects
!= 0) {
888 DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
891 if (args
->DR4
== 0xffffffff) {
892 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
896 if (args
->DR1
|| args
->DR4
|| args
->cliprects_ptr
) {
897 DRM_DEBUG("0 cliprects but dirt in cliprects fields\n");
902 if (args
->flags
& I915_EXEC_GEN7_SOL_RESET
) {
903 DRM_DEBUG("sol reset is gen7 only\n");
907 ret
= execlists_move_to_gpu(params
->request
, vmas
);
911 if (ring
== &dev_priv
->ring
[RCS
] &&
912 instp_mode
!= dev_priv
->relative_constants_mode
) {
913 ret
= intel_logical_ring_begin(params
->request
, 4);
917 intel_logical_ring_emit(ringbuf
, MI_NOOP
);
918 intel_logical_ring_emit(ringbuf
, MI_LOAD_REGISTER_IMM(1));
919 intel_logical_ring_emit(ringbuf
, INSTPM
);
920 intel_logical_ring_emit(ringbuf
, instp_mask
<< 16 | instp_mode
);
921 intel_logical_ring_advance(ringbuf
);
923 dev_priv
->relative_constants_mode
= instp_mode
;
926 exec_start
= params
->batch_obj_vm_offset
+
927 args
->batch_start_offset
;
929 ret
= ring
->emit_bb_start(params
->request
, exec_start
, params
->dispatch_flags
);
933 trace_i915_gem_ring_dispatch(params
->request
, params
->dispatch_flags
);
935 i915_gem_execbuffer_move_to_active(vmas
, params
->request
);
936 i915_gem_execbuffer_retire_commands(params
);
941 void intel_execlists_retire_requests(struct intel_engine_cs
*ring
)
943 struct drm_i915_gem_request
*req
, *tmp
;
944 struct list_head retired_list
;
946 WARN_ON(!mutex_is_locked(&ring
->dev
->struct_mutex
));
947 if (list_empty(&ring
->execlist_retired_req_list
))
950 INIT_LIST_HEAD(&retired_list
);
951 spin_lock_irq(&ring
->execlist_lock
);
952 list_replace_init(&ring
->execlist_retired_req_list
, &retired_list
);
953 spin_unlock_irq(&ring
->execlist_lock
);
955 list_for_each_entry_safe(req
, tmp
, &retired_list
, execlist_link
) {
956 struct intel_context
*ctx
= req
->ctx
;
957 struct drm_i915_gem_object
*ctx_obj
=
958 ctx
->engine
[ring
->id
].state
;
960 if (ctx_obj
&& (ctx
!= ring
->default_context
))
961 intel_lr_context_unpin(ring
, ctx
);
962 list_del(&req
->execlist_link
);
963 i915_gem_request_unreference(req
);
967 void intel_logical_ring_stop(struct intel_engine_cs
*ring
)
969 struct drm_i915_private
*dev_priv
= ring
->dev
->dev_private
;
972 if (!intel_ring_initialized(ring
))
975 ret
= intel_ring_idle(ring
);
976 if (ret
&& !i915_reset_in_progress(&to_i915(ring
->dev
)->gpu_error
))
977 DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
980 /* TODO: Is this correct with Execlists enabled? */
981 I915_WRITE_MODE(ring
, _MASKED_BIT_ENABLE(STOP_RING
));
982 if (wait_for_atomic((I915_READ_MODE(ring
) & MODE_IDLE
) != 0, 1000)) {
983 DRM_ERROR("%s :timed out trying to stop ring\n", ring
->name
);
986 I915_WRITE_MODE(ring
, _MASKED_BIT_DISABLE(STOP_RING
));
989 int logical_ring_flush_all_caches(struct drm_i915_gem_request
*req
)
991 struct intel_engine_cs
*ring
= req
->ring
;
994 if (!ring
->gpu_caches_dirty
)
997 ret
= ring
->emit_flush(req
, 0, I915_GEM_GPU_DOMAINS
);
1001 ring
->gpu_caches_dirty
= false;
1005 static int intel_lr_context_pin(struct intel_engine_cs
*ring
,
1006 struct intel_context
*ctx
)
1008 struct drm_i915_gem_object
*ctx_obj
= ctx
->engine
[ring
->id
].state
;
1009 struct intel_ringbuffer
*ringbuf
= ctx
->engine
[ring
->id
].ringbuf
;
1012 WARN_ON(!mutex_is_locked(&ring
->dev
->struct_mutex
));
1013 if (ctx
->engine
[ring
->id
].pin_count
++ == 0) {
1014 ret
= i915_gem_obj_ggtt_pin(ctx_obj
,
1015 GEN8_LR_CONTEXT_ALIGN
, 0);
1017 goto reset_pin_count
;
1019 ret
= intel_pin_and_map_ringbuffer_obj(ring
->dev
, ringbuf
);
1027 i915_gem_object_ggtt_unpin(ctx_obj
);
1029 ctx
->engine
[ring
->id
].pin_count
= 0;
1034 void intel_lr_context_unpin(struct intel_engine_cs
*ring
,
1035 struct intel_context
*ctx
)
1037 struct drm_i915_gem_object
*ctx_obj
= ctx
->engine
[ring
->id
].state
;
1038 struct intel_ringbuffer
*ringbuf
= ctx
->engine
[ring
->id
].ringbuf
;
1041 WARN_ON(!mutex_is_locked(&ring
->dev
->struct_mutex
));
1042 if (--ctx
->engine
[ring
->id
].pin_count
== 0) {
1043 intel_unpin_ringbuffer_obj(ringbuf
);
1044 i915_gem_object_ggtt_unpin(ctx_obj
);
1049 static int intel_logical_ring_workarounds_emit(struct drm_i915_gem_request
*req
)
1052 struct intel_engine_cs
*ring
= req
->ring
;
1053 struct intel_ringbuffer
*ringbuf
= req
->ringbuf
;
1054 struct drm_device
*dev
= ring
->dev
;
1055 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1056 struct i915_workarounds
*w
= &dev_priv
->workarounds
;
1058 if (WARN_ON_ONCE(w
->count
== 0))
1061 ring
->gpu_caches_dirty
= true;
1062 ret
= logical_ring_flush_all_caches(req
);
1066 ret
= intel_logical_ring_begin(req
, w
->count
* 2 + 2);
1070 intel_logical_ring_emit(ringbuf
, MI_LOAD_REGISTER_IMM(w
->count
));
1071 for (i
= 0; i
< w
->count
; i
++) {
1072 intel_logical_ring_emit(ringbuf
, w
->reg
[i
].addr
);
1073 intel_logical_ring_emit(ringbuf
, w
->reg
[i
].value
);
1075 intel_logical_ring_emit(ringbuf
, MI_NOOP
);
1077 intel_logical_ring_advance(ringbuf
);
1079 ring
->gpu_caches_dirty
= true;
1080 ret
= logical_ring_flush_all_caches(req
);
1087 #define wa_ctx_emit(batch, cmd) \
1089 if (WARN_ON(index >= (PAGE_SIZE / sizeof(uint32_t)))) { \
1092 batch[index++] = (cmd); \
1095 static inline uint32_t wa_ctx_start(struct i915_wa_ctx_bb
*wa_ctx
,
1097 uint32_t start_alignment
)
1099 return wa_ctx
->offset
= ALIGN(offset
, start_alignment
);
1102 static inline int wa_ctx_end(struct i915_wa_ctx_bb
*wa_ctx
,
1104 uint32_t size_alignment
)
1106 wa_ctx
->size
= offset
- wa_ctx
->offset
;
1108 WARN(wa_ctx
->size
% size_alignment
,
1109 "wa_ctx_bb failed sanity checks: size %d is not aligned to %d\n",
1110 wa_ctx
->size
, size_alignment
);
1115 * gen8_init_indirectctx_bb() - initialize indirect ctx batch with WA
1117 * @ring: only applicable for RCS
1118 * @wa_ctx: structure representing wa_ctx
1119 * offset: specifies start of the batch, should be cache-aligned. This is updated
1120 * with the offset value received as input.
1121 * size: size of the batch in DWORDS but HW expects in terms of cachelines
1122 * @batch: page in which WA are loaded
1123 * @offset: This field specifies the start of the batch, it should be
1124 * cache-aligned otherwise it is adjusted accordingly.
1125 * Typically we only have one indirect_ctx and per_ctx batch buffer which are
1126 * initialized at the beginning and shared across all contexts but this field
1127 * helps us to have multiple batches at different offsets and select them based
1128 * on a criteria. At the moment this batch always start at the beginning of the page
1129 * and at this point we don't have multiple wa_ctx batch buffers.
1131 * The number of WA applied are not known at the beginning; we use this field
1132 * to return the no of DWORDS written.
1134 * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
1135 * so it adds NOOPs as padding to make it cacheline aligned.
1136 * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
1137 * makes a complete batch buffer.
1139 * Return: non-zero if we exceed the PAGE_SIZE limit.
1142 static int gen8_init_indirectctx_bb(struct intel_engine_cs
*ring
,
1143 struct i915_wa_ctx_bb
*wa_ctx
,
1144 uint32_t *const batch
,
1147 uint32_t scratch_addr
;
1148 uint32_t index
= wa_ctx_start(wa_ctx
, *offset
, CACHELINE_DWORDS
);
1150 /* WaDisableCtxRestoreArbitration:bdw,chv */
1151 wa_ctx_emit(batch
, MI_ARB_ON_OFF
| MI_ARB_DISABLE
);
1153 /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
1154 if (IS_BROADWELL(ring
->dev
)) {
1155 struct drm_i915_private
*dev_priv
= to_i915(ring
->dev
);
1156 uint32_t l3sqc4_flush
= (I915_READ(GEN8_L3SQCREG4
) |
1157 GEN8_LQSC_FLUSH_COHERENT_LINES
);
1159 wa_ctx_emit(batch
, MI_LOAD_REGISTER_IMM(1));
1160 wa_ctx_emit(batch
, GEN8_L3SQCREG4
);
1161 wa_ctx_emit(batch
, l3sqc4_flush
);
1163 wa_ctx_emit(batch
, GFX_OP_PIPE_CONTROL(6));
1164 wa_ctx_emit(batch
, (PIPE_CONTROL_CS_STALL
|
1165 PIPE_CONTROL_DC_FLUSH_ENABLE
));
1166 wa_ctx_emit(batch
, 0);
1167 wa_ctx_emit(batch
, 0);
1168 wa_ctx_emit(batch
, 0);
1169 wa_ctx_emit(batch
, 0);
1171 wa_ctx_emit(batch
, MI_LOAD_REGISTER_IMM(1));
1172 wa_ctx_emit(batch
, GEN8_L3SQCREG4
);
1173 wa_ctx_emit(batch
, l3sqc4_flush
& ~GEN8_LQSC_FLUSH_COHERENT_LINES
);
1176 /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
1177 /* Actual scratch location is at 128 bytes offset */
1178 scratch_addr
= ring
->scratch
.gtt_offset
+ 2*CACHELINE_BYTES
;
1180 wa_ctx_emit(batch
, GFX_OP_PIPE_CONTROL(6));
1181 wa_ctx_emit(batch
, (PIPE_CONTROL_FLUSH_L3
|
1182 PIPE_CONTROL_GLOBAL_GTT_IVB
|
1183 PIPE_CONTROL_CS_STALL
|
1184 PIPE_CONTROL_QW_WRITE
));
1185 wa_ctx_emit(batch
, scratch_addr
);
1186 wa_ctx_emit(batch
, 0);
1187 wa_ctx_emit(batch
, 0);
1188 wa_ctx_emit(batch
, 0);
1190 /* Pad to end of cacheline */
1191 while (index
% CACHELINE_DWORDS
)
1192 wa_ctx_emit(batch
, MI_NOOP
);
1195 * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
1196 * execution depends on the length specified in terms of cache lines
1197 * in the register CTX_RCS_INDIRECT_CTX
1200 return wa_ctx_end(wa_ctx
, *offset
= index
, CACHELINE_DWORDS
);
1204 * gen8_init_perctx_bb() - initialize per ctx batch with WA
1206 * @ring: only applicable for RCS
1207 * @wa_ctx: structure representing wa_ctx
1208 * offset: specifies start of the batch, should be cache-aligned.
1209 * size: size of the batch in DWORDS but HW expects in terms of cachelines
1210 * @batch: page in which WA are loaded
1211 * @offset: This field specifies the start of this batch.
1212 * This batch is started immediately after indirect_ctx batch. Since we ensure
1213 * that indirect_ctx ends on a cacheline this batch is aligned automatically.
1215 * The number of DWORDS written are returned using this field.
1217 * This batch is terminated with MI_BATCH_BUFFER_END and so we need not add padding
1218 * to align it with cacheline as padding after MI_BATCH_BUFFER_END is redundant.
1220 static int gen8_init_perctx_bb(struct intel_engine_cs
*ring
,
1221 struct i915_wa_ctx_bb
*wa_ctx
,
1222 uint32_t *const batch
,
1225 uint32_t index
= wa_ctx_start(wa_ctx
, *offset
, CACHELINE_DWORDS
);
1227 /* WaDisableCtxRestoreArbitration:bdw,chv */
1228 wa_ctx_emit(batch
, MI_ARB_ON_OFF
| MI_ARB_ENABLE
);
1230 wa_ctx_emit(batch
, MI_BATCH_BUFFER_END
);
1232 return wa_ctx_end(wa_ctx
, *offset
= index
, 1);
1235 static int lrc_setup_wa_ctx_obj(struct intel_engine_cs
*ring
, u32 size
)
1239 ring
->wa_ctx
.obj
= i915_gem_alloc_object(ring
->dev
, PAGE_ALIGN(size
));
1240 if (!ring
->wa_ctx
.obj
) {
1241 DRM_DEBUG_DRIVER("alloc LRC WA ctx backing obj failed.\n");
1245 ret
= i915_gem_obj_ggtt_pin(ring
->wa_ctx
.obj
, PAGE_SIZE
, 0);
1247 DRM_DEBUG_DRIVER("pin LRC WA ctx backing obj failed: %d\n",
1249 drm_gem_object_unreference(&ring
->wa_ctx
.obj
->base
);
1256 static void lrc_destroy_wa_ctx_obj(struct intel_engine_cs
*ring
)
1258 if (ring
->wa_ctx
.obj
) {
1259 i915_gem_object_ggtt_unpin(ring
->wa_ctx
.obj
);
1260 drm_gem_object_unreference(&ring
->wa_ctx
.obj
->base
);
1261 ring
->wa_ctx
.obj
= NULL
;
1265 static int intel_init_workaround_bb(struct intel_engine_cs
*ring
)
1271 struct i915_ctx_workarounds
*wa_ctx
= &ring
->wa_ctx
;
1273 WARN_ON(ring
->id
!= RCS
);
1275 /* update this when WA for higher Gen are added */
1276 if (WARN(INTEL_INFO(ring
->dev
)->gen
> 8,
1277 "WA batch buffer is not initialized for Gen%d\n",
1278 INTEL_INFO(ring
->dev
)->gen
))
1281 /* some WA perform writes to scratch page, ensure it is valid */
1282 if (ring
->scratch
.obj
== NULL
) {
1283 DRM_ERROR("scratch page not allocated for %s\n", ring
->name
);
1287 ret
= lrc_setup_wa_ctx_obj(ring
, PAGE_SIZE
);
1289 DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret
);
1293 page
= i915_gem_object_get_page(wa_ctx
->obj
, 0);
1294 batch
= kmap_atomic(page
);
1297 if (INTEL_INFO(ring
->dev
)->gen
== 8) {
1298 ret
= gen8_init_indirectctx_bb(ring
,
1299 &wa_ctx
->indirect_ctx
,
1305 ret
= gen8_init_perctx_bb(ring
,
1314 kunmap_atomic(batch
);
1316 lrc_destroy_wa_ctx_obj(ring
);
1321 static int gen8_init_common_ring(struct intel_engine_cs
*ring
)
1323 struct drm_device
*dev
= ring
->dev
;
1324 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1326 I915_WRITE_IMR(ring
, ~(ring
->irq_enable_mask
| ring
->irq_keep_mask
));
1327 I915_WRITE(RING_HWSTAM(ring
->mmio_base
), 0xffffffff);
1329 I915_WRITE(RING_MODE_GEN7(ring
),
1330 _MASKED_BIT_DISABLE(GFX_REPLAY_MODE
) |
1331 _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE
));
1332 POSTING_READ(RING_MODE_GEN7(ring
));
1333 ring
->next_context_status_buffer
= 0;
1334 DRM_DEBUG_DRIVER("Execlists enabled for %s\n", ring
->name
);
1336 memset(&ring
->hangcheck
, 0, sizeof(ring
->hangcheck
));
1341 static int gen8_init_render_ring(struct intel_engine_cs
*ring
)
1343 struct drm_device
*dev
= ring
->dev
;
1344 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1347 ret
= gen8_init_common_ring(ring
);
1351 /* We need to disable the AsyncFlip performance optimisations in order
1352 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
1353 * programmed to '1' on all products.
1355 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
1357 I915_WRITE(MI_MODE
, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE
));
1359 I915_WRITE(INSTPM
, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING
));
1361 return init_workarounds_ring(ring
);
1364 static int gen9_init_render_ring(struct intel_engine_cs
*ring
)
1368 ret
= gen8_init_common_ring(ring
);
1372 return init_workarounds_ring(ring
);
1375 static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request
*req
)
1377 struct i915_hw_ppgtt
*ppgtt
= req
->ctx
->ppgtt
;
1378 struct intel_engine_cs
*ring
= req
->ring
;
1379 struct intel_ringbuffer
*ringbuf
= req
->ringbuf
;
1380 const int num_lri_cmds
= GEN8_LEGACY_PDPES
* 2;
1383 ret
= intel_logical_ring_begin(req
, num_lri_cmds
* 2 + 2);
1387 intel_logical_ring_emit(ringbuf
, MI_LOAD_REGISTER_IMM(num_lri_cmds
));
1388 for (i
= GEN8_LEGACY_PDPES
- 1; i
>= 0; i
--) {
1389 const dma_addr_t pd_daddr
= i915_page_dir_dma_addr(ppgtt
, i
);
1391 intel_logical_ring_emit(ringbuf
, GEN8_RING_PDP_UDW(ring
, i
));
1392 intel_logical_ring_emit(ringbuf
, upper_32_bits(pd_daddr
));
1393 intel_logical_ring_emit(ringbuf
, GEN8_RING_PDP_LDW(ring
, i
));
1394 intel_logical_ring_emit(ringbuf
, lower_32_bits(pd_daddr
));
1397 intel_logical_ring_emit(ringbuf
, MI_NOOP
);
1398 intel_logical_ring_advance(ringbuf
);
1403 static int gen8_emit_bb_start(struct drm_i915_gem_request
*req
,
1404 u64 offset
, unsigned dispatch_flags
)
1406 struct intel_ringbuffer
*ringbuf
= req
->ringbuf
;
1407 bool ppgtt
= !(dispatch_flags
& I915_DISPATCH_SECURE
);
1410 /* Don't rely in hw updating PDPs, specially in lite-restore.
1411 * Ideally, we should set Force PD Restore in ctx descriptor,
1412 * but we can't. Force Restore would be a second option, but
1413 * it is unsafe in case of lite-restore (because the ctx is
1415 if (req
->ctx
->ppgtt
&&
1416 (intel_ring_flag(req
->ring
) & req
->ctx
->ppgtt
->pd_dirty_rings
)) {
1417 ret
= intel_logical_ring_emit_pdps(req
);
1421 req
->ctx
->ppgtt
->pd_dirty_rings
&= ~intel_ring_flag(req
->ring
);
1424 ret
= intel_logical_ring_begin(req
, 4);
1428 /* FIXME(BDW): Address space and security selectors. */
1429 intel_logical_ring_emit(ringbuf
, MI_BATCH_BUFFER_START_GEN8
|
1431 (dispatch_flags
& I915_DISPATCH_RS
?
1432 MI_BATCH_RESOURCE_STREAMER
: 0));
1433 intel_logical_ring_emit(ringbuf
, lower_32_bits(offset
));
1434 intel_logical_ring_emit(ringbuf
, upper_32_bits(offset
));
1435 intel_logical_ring_emit(ringbuf
, MI_NOOP
);
1436 intel_logical_ring_advance(ringbuf
);
1441 static bool gen8_logical_ring_get_irq(struct intel_engine_cs
*ring
)
1443 struct drm_device
*dev
= ring
->dev
;
1444 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1445 unsigned long flags
;
1447 if (WARN_ON(!intel_irqs_enabled(dev_priv
)))
1450 spin_lock_irqsave(&dev_priv
->irq_lock
, flags
);
1451 if (ring
->irq_refcount
++ == 0) {
1452 I915_WRITE_IMR(ring
, ~(ring
->irq_enable_mask
| ring
->irq_keep_mask
));
1453 POSTING_READ(RING_IMR(ring
->mmio_base
));
1455 spin_unlock_irqrestore(&dev_priv
->irq_lock
, flags
);
1460 static void gen8_logical_ring_put_irq(struct intel_engine_cs
*ring
)
1462 struct drm_device
*dev
= ring
->dev
;
1463 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1464 unsigned long flags
;
1466 spin_lock_irqsave(&dev_priv
->irq_lock
, flags
);
1467 if (--ring
->irq_refcount
== 0) {
1468 I915_WRITE_IMR(ring
, ~ring
->irq_keep_mask
);
1469 POSTING_READ(RING_IMR(ring
->mmio_base
));
1471 spin_unlock_irqrestore(&dev_priv
->irq_lock
, flags
);
1474 static int gen8_emit_flush(struct drm_i915_gem_request
*request
,
1475 u32 invalidate_domains
,
1478 struct intel_ringbuffer
*ringbuf
= request
->ringbuf
;
1479 struct intel_engine_cs
*ring
= ringbuf
->ring
;
1480 struct drm_device
*dev
= ring
->dev
;
1481 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1485 ret
= intel_logical_ring_begin(request
, 4);
1489 cmd
= MI_FLUSH_DW
+ 1;
1491 /* We always require a command barrier so that subsequent
1492 * commands, such as breadcrumb interrupts, are strictly ordered
1493 * wrt the contents of the write cache being flushed to memory
1494 * (and thus being coherent from the CPU).
1496 cmd
|= MI_FLUSH_DW_STORE_INDEX
| MI_FLUSH_DW_OP_STOREDW
;
1498 if (invalidate_domains
& I915_GEM_GPU_DOMAINS
) {
1499 cmd
|= MI_INVALIDATE_TLB
;
1500 if (ring
== &dev_priv
->ring
[VCS
])
1501 cmd
|= MI_INVALIDATE_BSD
;
1504 intel_logical_ring_emit(ringbuf
, cmd
);
1505 intel_logical_ring_emit(ringbuf
,
1506 I915_GEM_HWS_SCRATCH_ADDR
|
1507 MI_FLUSH_DW_USE_GTT
);
1508 intel_logical_ring_emit(ringbuf
, 0); /* upper addr */
1509 intel_logical_ring_emit(ringbuf
, 0); /* value */
1510 intel_logical_ring_advance(ringbuf
);
1515 static int gen8_emit_flush_render(struct drm_i915_gem_request
*request
,
1516 u32 invalidate_domains
,
1519 struct intel_ringbuffer
*ringbuf
= request
->ringbuf
;
1520 struct intel_engine_cs
*ring
= ringbuf
->ring
;
1521 u32 scratch_addr
= ring
->scratch
.gtt_offset
+ 2 * CACHELINE_BYTES
;
1526 flags
|= PIPE_CONTROL_CS_STALL
;
1528 if (flush_domains
) {
1529 flags
|= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH
;
1530 flags
|= PIPE_CONTROL_DEPTH_CACHE_FLUSH
;
1533 if (invalidate_domains
) {
1534 flags
|= PIPE_CONTROL_TLB_INVALIDATE
;
1535 flags
|= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE
;
1536 flags
|= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE
;
1537 flags
|= PIPE_CONTROL_VF_CACHE_INVALIDATE
;
1538 flags
|= PIPE_CONTROL_CONST_CACHE_INVALIDATE
;
1539 flags
|= PIPE_CONTROL_STATE_CACHE_INVALIDATE
;
1540 flags
|= PIPE_CONTROL_QW_WRITE
;
1541 flags
|= PIPE_CONTROL_GLOBAL_GTT_IVB
;
1545 * On GEN9+ Before VF_CACHE_INVALIDATE we need to emit a NULL pipe
1548 vf_flush_wa
= INTEL_INFO(ring
->dev
)->gen
>= 9 &&
1549 flags
& PIPE_CONTROL_VF_CACHE_INVALIDATE
;
1551 ret
= intel_logical_ring_begin(request
, vf_flush_wa
? 12 : 6);
1556 intel_logical_ring_emit(ringbuf
, GFX_OP_PIPE_CONTROL(6));
1557 intel_logical_ring_emit(ringbuf
, 0);
1558 intel_logical_ring_emit(ringbuf
, 0);
1559 intel_logical_ring_emit(ringbuf
, 0);
1560 intel_logical_ring_emit(ringbuf
, 0);
1561 intel_logical_ring_emit(ringbuf
, 0);
1564 intel_logical_ring_emit(ringbuf
, GFX_OP_PIPE_CONTROL(6));
1565 intel_logical_ring_emit(ringbuf
, flags
);
1566 intel_logical_ring_emit(ringbuf
, scratch_addr
);
1567 intel_logical_ring_emit(ringbuf
, 0);
1568 intel_logical_ring_emit(ringbuf
, 0);
1569 intel_logical_ring_emit(ringbuf
, 0);
1570 intel_logical_ring_advance(ringbuf
);
1575 static u32
gen8_get_seqno(struct intel_engine_cs
*ring
, bool lazy_coherency
)
1577 return intel_read_status_page(ring
, I915_GEM_HWS_INDEX
);
1580 static void gen8_set_seqno(struct intel_engine_cs
*ring
, u32 seqno
)
1582 intel_write_status_page(ring
, I915_GEM_HWS_INDEX
, seqno
);
1585 static int gen8_emit_request(struct drm_i915_gem_request
*request
)
1587 struct intel_ringbuffer
*ringbuf
= request
->ringbuf
;
1588 struct intel_engine_cs
*ring
= ringbuf
->ring
;
1593 * Reserve space for 2 NOOPs at the end of each request to be
1594 * used as a workaround for not being allowed to do lite
1595 * restore with HEAD==TAIL (WaIdleLiteRestore).
1597 ret
= intel_logical_ring_begin(request
, 8);
1601 cmd
= MI_STORE_DWORD_IMM_GEN4
;
1602 cmd
|= MI_GLOBAL_GTT
;
1604 intel_logical_ring_emit(ringbuf
, cmd
);
1605 intel_logical_ring_emit(ringbuf
,
1606 (ring
->status_page
.gfx_addr
+
1607 (I915_GEM_HWS_INDEX
<< MI_STORE_DWORD_INDEX_SHIFT
)));
1608 intel_logical_ring_emit(ringbuf
, 0);
1609 intel_logical_ring_emit(ringbuf
, i915_gem_request_get_seqno(request
));
1610 intel_logical_ring_emit(ringbuf
, MI_USER_INTERRUPT
);
1611 intel_logical_ring_emit(ringbuf
, MI_NOOP
);
1612 intel_logical_ring_advance_and_submit(request
);
1615 * Here we add two extra NOOPs as padding to avoid
1616 * lite restore of a context with HEAD==TAIL.
1618 intel_logical_ring_emit(ringbuf
, MI_NOOP
);
1619 intel_logical_ring_emit(ringbuf
, MI_NOOP
);
1620 intel_logical_ring_advance(ringbuf
);
1625 static int intel_lr_context_render_state_init(struct drm_i915_gem_request
*req
)
1627 struct render_state so
;
1630 ret
= i915_gem_render_state_prepare(req
->ring
, &so
);
1634 if (so
.rodata
== NULL
)
1637 ret
= req
->ring
->emit_bb_start(req
, so
.ggtt_offset
,
1638 I915_DISPATCH_SECURE
);
1642 i915_vma_move_to_active(i915_gem_obj_to_ggtt(so
.obj
), req
);
1645 i915_gem_render_state_fini(&so
);
1649 static int gen8_init_rcs_context(struct drm_i915_gem_request
*req
)
1653 ret
= intel_logical_ring_workarounds_emit(req
);
1657 return intel_lr_context_render_state_init(req
);
1661 * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
1663 * @ring: Engine Command Streamer.
1666 void intel_logical_ring_cleanup(struct intel_engine_cs
*ring
)
1668 struct drm_i915_private
*dev_priv
;
1670 if (!intel_ring_initialized(ring
))
1673 dev_priv
= ring
->dev
->dev_private
;
1675 intel_logical_ring_stop(ring
);
1676 WARN_ON((I915_READ_MODE(ring
) & MODE_IDLE
) == 0);
1679 ring
->cleanup(ring
);
1681 i915_cmd_parser_fini_ring(ring
);
1682 i915_gem_batch_pool_fini(&ring
->batch_pool
);
1684 if (ring
->status_page
.obj
) {
1685 kunmap(sg_page(ring
->status_page
.obj
->pages
->sgl
));
1686 ring
->status_page
.obj
= NULL
;
1689 lrc_destroy_wa_ctx_obj(ring
);
1692 static int logical_ring_init(struct drm_device
*dev
, struct intel_engine_cs
*ring
)
1696 /* Intentionally left blank. */
1697 ring
->buffer
= NULL
;
1700 INIT_LIST_HEAD(&ring
->active_list
);
1701 INIT_LIST_HEAD(&ring
->request_list
);
1702 i915_gem_batch_pool_init(dev
, &ring
->batch_pool
);
1703 init_waitqueue_head(&ring
->irq_queue
);
1705 INIT_LIST_HEAD(&ring
->execlist_queue
);
1706 INIT_LIST_HEAD(&ring
->execlist_retired_req_list
);
1707 spin_lock_init(&ring
->execlist_lock
);
1709 ret
= i915_cmd_parser_init_ring(ring
);
1713 ret
= intel_lr_context_deferred_create(ring
->default_context
, ring
);
1718 static int logical_render_ring_init(struct drm_device
*dev
)
1720 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1721 struct intel_engine_cs
*ring
= &dev_priv
->ring
[RCS
];
1724 ring
->name
= "render ring";
1726 ring
->mmio_base
= RENDER_RING_BASE
;
1727 ring
->irq_enable_mask
=
1728 GT_RENDER_USER_INTERRUPT
<< GEN8_RCS_IRQ_SHIFT
;
1729 ring
->irq_keep_mask
=
1730 GT_CONTEXT_SWITCH_INTERRUPT
<< GEN8_RCS_IRQ_SHIFT
;
1731 if (HAS_L3_DPF(dev
))
1732 ring
->irq_keep_mask
|= GT_RENDER_L3_PARITY_ERROR_INTERRUPT
;
1734 if (INTEL_INFO(dev
)->gen
>= 9)
1735 ring
->init_hw
= gen9_init_render_ring
;
1737 ring
->init_hw
= gen8_init_render_ring
;
1738 ring
->init_context
= gen8_init_rcs_context
;
1739 ring
->cleanup
= intel_fini_pipe_control
;
1740 ring
->get_seqno
= gen8_get_seqno
;
1741 ring
->set_seqno
= gen8_set_seqno
;
1742 ring
->emit_request
= gen8_emit_request
;
1743 ring
->emit_flush
= gen8_emit_flush_render
;
1744 ring
->irq_get
= gen8_logical_ring_get_irq
;
1745 ring
->irq_put
= gen8_logical_ring_put_irq
;
1746 ring
->emit_bb_start
= gen8_emit_bb_start
;
1750 ret
= intel_init_pipe_control(ring
);
1754 ret
= intel_init_workaround_bb(ring
);
1757 * We continue even if we fail to initialize WA batch
1758 * because we only expect rare glitches but nothing
1759 * critical to prevent us from using GPU
1761 DRM_ERROR("WA batch buffer initialization failed: %d\n",
1765 ret
= logical_ring_init(dev
, ring
);
1767 lrc_destroy_wa_ctx_obj(ring
);
1773 static int logical_bsd_ring_init(struct drm_device
*dev
)
1775 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1776 struct intel_engine_cs
*ring
= &dev_priv
->ring
[VCS
];
1778 ring
->name
= "bsd ring";
1780 ring
->mmio_base
= GEN6_BSD_RING_BASE
;
1781 ring
->irq_enable_mask
=
1782 GT_RENDER_USER_INTERRUPT
<< GEN8_VCS1_IRQ_SHIFT
;
1783 ring
->irq_keep_mask
=
1784 GT_CONTEXT_SWITCH_INTERRUPT
<< GEN8_VCS1_IRQ_SHIFT
;
1786 ring
->init_hw
= gen8_init_common_ring
;
1787 ring
->get_seqno
= gen8_get_seqno
;
1788 ring
->set_seqno
= gen8_set_seqno
;
1789 ring
->emit_request
= gen8_emit_request
;
1790 ring
->emit_flush
= gen8_emit_flush
;
1791 ring
->irq_get
= gen8_logical_ring_get_irq
;
1792 ring
->irq_put
= gen8_logical_ring_put_irq
;
1793 ring
->emit_bb_start
= gen8_emit_bb_start
;
1795 return logical_ring_init(dev
, ring
);
1798 static int logical_bsd2_ring_init(struct drm_device
*dev
)
1800 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1801 struct intel_engine_cs
*ring
= &dev_priv
->ring
[VCS2
];
1803 ring
->name
= "bds2 ring";
1805 ring
->mmio_base
= GEN8_BSD2_RING_BASE
;
1806 ring
->irq_enable_mask
=
1807 GT_RENDER_USER_INTERRUPT
<< GEN8_VCS2_IRQ_SHIFT
;
1808 ring
->irq_keep_mask
=
1809 GT_CONTEXT_SWITCH_INTERRUPT
<< GEN8_VCS2_IRQ_SHIFT
;
1811 ring
->init_hw
= gen8_init_common_ring
;
1812 ring
->get_seqno
= gen8_get_seqno
;
1813 ring
->set_seqno
= gen8_set_seqno
;
1814 ring
->emit_request
= gen8_emit_request
;
1815 ring
->emit_flush
= gen8_emit_flush
;
1816 ring
->irq_get
= gen8_logical_ring_get_irq
;
1817 ring
->irq_put
= gen8_logical_ring_put_irq
;
1818 ring
->emit_bb_start
= gen8_emit_bb_start
;
1820 return logical_ring_init(dev
, ring
);
1823 static int logical_blt_ring_init(struct drm_device
*dev
)
1825 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1826 struct intel_engine_cs
*ring
= &dev_priv
->ring
[BCS
];
1828 ring
->name
= "blitter ring";
1830 ring
->mmio_base
= BLT_RING_BASE
;
1831 ring
->irq_enable_mask
=
1832 GT_RENDER_USER_INTERRUPT
<< GEN8_BCS_IRQ_SHIFT
;
1833 ring
->irq_keep_mask
=
1834 GT_CONTEXT_SWITCH_INTERRUPT
<< GEN8_BCS_IRQ_SHIFT
;
1836 ring
->init_hw
= gen8_init_common_ring
;
1837 ring
->get_seqno
= gen8_get_seqno
;
1838 ring
->set_seqno
= gen8_set_seqno
;
1839 ring
->emit_request
= gen8_emit_request
;
1840 ring
->emit_flush
= gen8_emit_flush
;
1841 ring
->irq_get
= gen8_logical_ring_get_irq
;
1842 ring
->irq_put
= gen8_logical_ring_put_irq
;
1843 ring
->emit_bb_start
= gen8_emit_bb_start
;
1845 return logical_ring_init(dev
, ring
);
1848 static int logical_vebox_ring_init(struct drm_device
*dev
)
1850 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1851 struct intel_engine_cs
*ring
= &dev_priv
->ring
[VECS
];
1853 ring
->name
= "video enhancement ring";
1855 ring
->mmio_base
= VEBOX_RING_BASE
;
1856 ring
->irq_enable_mask
=
1857 GT_RENDER_USER_INTERRUPT
<< GEN8_VECS_IRQ_SHIFT
;
1858 ring
->irq_keep_mask
=
1859 GT_CONTEXT_SWITCH_INTERRUPT
<< GEN8_VECS_IRQ_SHIFT
;
1861 ring
->init_hw
= gen8_init_common_ring
;
1862 ring
->get_seqno
= gen8_get_seqno
;
1863 ring
->set_seqno
= gen8_set_seqno
;
1864 ring
->emit_request
= gen8_emit_request
;
1865 ring
->emit_flush
= gen8_emit_flush
;
1866 ring
->irq_get
= gen8_logical_ring_get_irq
;
1867 ring
->irq_put
= gen8_logical_ring_put_irq
;
1868 ring
->emit_bb_start
= gen8_emit_bb_start
;
1870 return logical_ring_init(dev
, ring
);
1874 * intel_logical_rings_init() - allocate, populate and init the Engine Command Streamers
1877 * This function inits the engines for an Execlists submission style (the equivalent in the
1878 * legacy ringbuffer submission world would be i915_gem_init_rings). It does it only for
1879 * those engines that are present in the hardware.
1881 * Return: non-zero if the initialization failed.
1883 int intel_logical_rings_init(struct drm_device
*dev
)
1885 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1888 ret
= logical_render_ring_init(dev
);
1893 ret
= logical_bsd_ring_init(dev
);
1895 goto cleanup_render_ring
;
1899 ret
= logical_blt_ring_init(dev
);
1901 goto cleanup_bsd_ring
;
1904 if (HAS_VEBOX(dev
)) {
1905 ret
= logical_vebox_ring_init(dev
);
1907 goto cleanup_blt_ring
;
1910 if (HAS_BSD2(dev
)) {
1911 ret
= logical_bsd2_ring_init(dev
);
1913 goto cleanup_vebox_ring
;
1916 ret
= i915_gem_set_seqno(dev
, ((u32
)~0 - 0x1000));
1918 goto cleanup_bsd2_ring
;
1923 intel_logical_ring_cleanup(&dev_priv
->ring
[VCS2
]);
1925 intel_logical_ring_cleanup(&dev_priv
->ring
[VECS
]);
1927 intel_logical_ring_cleanup(&dev_priv
->ring
[BCS
]);
1929 intel_logical_ring_cleanup(&dev_priv
->ring
[VCS
]);
1930 cleanup_render_ring
:
1931 intel_logical_ring_cleanup(&dev_priv
->ring
[RCS
]);
1937 make_rpcs(struct drm_device
*dev
)
1942 * No explicit RPCS request is needed to ensure full
1943 * slice/subslice/EU enablement prior to Gen9.
1945 if (INTEL_INFO(dev
)->gen
< 9)
1949 * Starting in Gen9, render power gating can leave
1950 * slice/subslice/EU in a partially enabled state. We
1951 * must make an explicit request through RPCS for full
1954 if (INTEL_INFO(dev
)->has_slice_pg
) {
1955 rpcs
|= GEN8_RPCS_S_CNT_ENABLE
;
1956 rpcs
|= INTEL_INFO(dev
)->slice_total
<<
1957 GEN8_RPCS_S_CNT_SHIFT
;
1958 rpcs
|= GEN8_RPCS_ENABLE
;
1961 if (INTEL_INFO(dev
)->has_subslice_pg
) {
1962 rpcs
|= GEN8_RPCS_SS_CNT_ENABLE
;
1963 rpcs
|= INTEL_INFO(dev
)->subslice_per_slice
<<
1964 GEN8_RPCS_SS_CNT_SHIFT
;
1965 rpcs
|= GEN8_RPCS_ENABLE
;
1968 if (INTEL_INFO(dev
)->has_eu_pg
) {
1969 rpcs
|= INTEL_INFO(dev
)->eu_per_subslice
<<
1970 GEN8_RPCS_EU_MIN_SHIFT
;
1971 rpcs
|= INTEL_INFO(dev
)->eu_per_subslice
<<
1972 GEN8_RPCS_EU_MAX_SHIFT
;
1973 rpcs
|= GEN8_RPCS_ENABLE
;
1980 populate_lr_context(struct intel_context
*ctx
, struct drm_i915_gem_object
*ctx_obj
,
1981 struct intel_engine_cs
*ring
, struct intel_ringbuffer
*ringbuf
)
1983 struct drm_device
*dev
= ring
->dev
;
1984 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
1985 struct i915_hw_ppgtt
*ppgtt
= ctx
->ppgtt
;
1987 uint32_t *reg_state
;
1991 ppgtt
= dev_priv
->mm
.aliasing_ppgtt
;
1993 ret
= i915_gem_object_set_to_cpu_domain(ctx_obj
, true);
1995 DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
1999 ret
= i915_gem_object_get_pages(ctx_obj
);
2001 DRM_DEBUG_DRIVER("Could not get object pages\n");
2005 i915_gem_object_pin_pages(ctx_obj
);
2007 /* The second page of the context object contains some fields which must
2008 * be set up prior to the first execution. */
2009 page
= i915_gem_object_get_page(ctx_obj
, 1);
2010 reg_state
= kmap_atomic(page
);
2012 /* A context is actually a big batch buffer with several MI_LOAD_REGISTER_IMM
2013 * commands followed by (reg, value) pairs. The values we are setting here are
2014 * only for the first context restore: on a subsequent save, the GPU will
2015 * recreate this batchbuffer with new values (including all the missing
2016 * MI_LOAD_REGISTER_IMM commands that we are not initializing here). */
2017 if (ring
->id
== RCS
)
2018 reg_state
[CTX_LRI_HEADER_0
] = MI_LOAD_REGISTER_IMM(14);
2020 reg_state
[CTX_LRI_HEADER_0
] = MI_LOAD_REGISTER_IMM(11);
2021 reg_state
[CTX_LRI_HEADER_0
] |= MI_LRI_FORCE_POSTED
;
2022 reg_state
[CTX_CONTEXT_CONTROL
] = RING_CONTEXT_CONTROL(ring
);
2023 reg_state
[CTX_CONTEXT_CONTROL
+1] =
2024 _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH
|
2025 CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT
|
2026 CTX_CTRL_RS_CTX_ENABLE
);
2027 reg_state
[CTX_RING_HEAD
] = RING_HEAD(ring
->mmio_base
);
2028 reg_state
[CTX_RING_HEAD
+1] = 0;
2029 reg_state
[CTX_RING_TAIL
] = RING_TAIL(ring
->mmio_base
);
2030 reg_state
[CTX_RING_TAIL
+1] = 0;
2031 reg_state
[CTX_RING_BUFFER_START
] = RING_START(ring
->mmio_base
);
2032 /* Ring buffer start address is not known until the buffer is pinned.
2033 * It is written to the context image in execlists_update_context()
2035 reg_state
[CTX_RING_BUFFER_CONTROL
] = RING_CTL(ring
->mmio_base
);
2036 reg_state
[CTX_RING_BUFFER_CONTROL
+1] =
2037 ((ringbuf
->size
- PAGE_SIZE
) & RING_NR_PAGES
) | RING_VALID
;
2038 reg_state
[CTX_BB_HEAD_U
] = ring
->mmio_base
+ 0x168;
2039 reg_state
[CTX_BB_HEAD_U
+1] = 0;
2040 reg_state
[CTX_BB_HEAD_L
] = ring
->mmio_base
+ 0x140;
2041 reg_state
[CTX_BB_HEAD_L
+1] = 0;
2042 reg_state
[CTX_BB_STATE
] = ring
->mmio_base
+ 0x110;
2043 reg_state
[CTX_BB_STATE
+1] = (1<<5);
2044 reg_state
[CTX_SECOND_BB_HEAD_U
] = ring
->mmio_base
+ 0x11c;
2045 reg_state
[CTX_SECOND_BB_HEAD_U
+1] = 0;
2046 reg_state
[CTX_SECOND_BB_HEAD_L
] = ring
->mmio_base
+ 0x114;
2047 reg_state
[CTX_SECOND_BB_HEAD_L
+1] = 0;
2048 reg_state
[CTX_SECOND_BB_STATE
] = ring
->mmio_base
+ 0x118;
2049 reg_state
[CTX_SECOND_BB_STATE
+1] = 0;
2050 if (ring
->id
== RCS
) {
2051 reg_state
[CTX_BB_PER_CTX_PTR
] = ring
->mmio_base
+ 0x1c0;
2052 reg_state
[CTX_BB_PER_CTX_PTR
+1] = 0;
2053 reg_state
[CTX_RCS_INDIRECT_CTX
] = ring
->mmio_base
+ 0x1c4;
2054 reg_state
[CTX_RCS_INDIRECT_CTX
+1] = 0;
2055 reg_state
[CTX_RCS_INDIRECT_CTX_OFFSET
] = ring
->mmio_base
+ 0x1c8;
2056 reg_state
[CTX_RCS_INDIRECT_CTX_OFFSET
+1] = 0;
2057 if (ring
->wa_ctx
.obj
) {
2058 struct i915_ctx_workarounds
*wa_ctx
= &ring
->wa_ctx
;
2059 uint32_t ggtt_offset
= i915_gem_obj_ggtt_offset(wa_ctx
->obj
);
2061 reg_state
[CTX_RCS_INDIRECT_CTX
+1] =
2062 (ggtt_offset
+ wa_ctx
->indirect_ctx
.offset
* sizeof(uint32_t)) |
2063 (wa_ctx
->indirect_ctx
.size
/ CACHELINE_DWORDS
);
2065 reg_state
[CTX_RCS_INDIRECT_CTX_OFFSET
+1] =
2066 CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT
<< 6;
2068 reg_state
[CTX_BB_PER_CTX_PTR
+1] =
2069 (ggtt_offset
+ wa_ctx
->per_ctx
.offset
* sizeof(uint32_t)) |
2073 reg_state
[CTX_LRI_HEADER_1
] = MI_LOAD_REGISTER_IMM(9);
2074 reg_state
[CTX_LRI_HEADER_1
] |= MI_LRI_FORCE_POSTED
;
2075 reg_state
[CTX_CTX_TIMESTAMP
] = ring
->mmio_base
+ 0x3a8;
2076 reg_state
[CTX_CTX_TIMESTAMP
+1] = 0;
2077 reg_state
[CTX_PDP3_UDW
] = GEN8_RING_PDP_UDW(ring
, 3);
2078 reg_state
[CTX_PDP3_LDW
] = GEN8_RING_PDP_LDW(ring
, 3);
2079 reg_state
[CTX_PDP2_UDW
] = GEN8_RING_PDP_UDW(ring
, 2);
2080 reg_state
[CTX_PDP2_LDW
] = GEN8_RING_PDP_LDW(ring
, 2);
2081 reg_state
[CTX_PDP1_UDW
] = GEN8_RING_PDP_UDW(ring
, 1);
2082 reg_state
[CTX_PDP1_LDW
] = GEN8_RING_PDP_LDW(ring
, 1);
2083 reg_state
[CTX_PDP0_UDW
] = GEN8_RING_PDP_UDW(ring
, 0);
2084 reg_state
[CTX_PDP0_LDW
] = GEN8_RING_PDP_LDW(ring
, 0);
2086 /* With dynamic page allocation, PDPs may not be allocated at this point,
2087 * Point the unallocated PDPs to the scratch page
2089 ASSIGN_CTX_PDP(ppgtt
, reg_state
, 3);
2090 ASSIGN_CTX_PDP(ppgtt
, reg_state
, 2);
2091 ASSIGN_CTX_PDP(ppgtt
, reg_state
, 1);
2092 ASSIGN_CTX_PDP(ppgtt
, reg_state
, 0);
2093 if (ring
->id
== RCS
) {
2094 reg_state
[CTX_LRI_HEADER_2
] = MI_LOAD_REGISTER_IMM(1);
2095 reg_state
[CTX_R_PWR_CLK_STATE
] = GEN8_R_PWR_CLK_STATE
;
2096 reg_state
[CTX_R_PWR_CLK_STATE
+1] = make_rpcs(dev
);
2099 kunmap_atomic(reg_state
);
2102 set_page_dirty(page
);
2103 i915_gem_object_unpin_pages(ctx_obj
);
2109 * intel_lr_context_free() - free the LRC specific bits of a context
2110 * @ctx: the LR context to free.
2112 * The real context freeing is done in i915_gem_context_free: this only
2113 * takes care of the bits that are LRC related: the per-engine backing
2114 * objects and the logical ringbuffer.
2116 void intel_lr_context_free(struct intel_context
*ctx
)
2120 for (i
= 0; i
< I915_NUM_RINGS
; i
++) {
2121 struct drm_i915_gem_object
*ctx_obj
= ctx
->engine
[i
].state
;
2124 struct intel_ringbuffer
*ringbuf
=
2125 ctx
->engine
[i
].ringbuf
;
2126 struct intel_engine_cs
*ring
= ringbuf
->ring
;
2128 if (ctx
== ring
->default_context
) {
2129 intel_unpin_ringbuffer_obj(ringbuf
);
2130 i915_gem_object_ggtt_unpin(ctx_obj
);
2132 WARN_ON(ctx
->engine
[ring
->id
].pin_count
);
2133 intel_destroy_ringbuffer_obj(ringbuf
);
2135 drm_gem_object_unreference(&ctx_obj
->base
);
2140 static uint32_t get_lr_context_size(struct intel_engine_cs
*ring
)
2144 WARN_ON(INTEL_INFO(ring
->dev
)->gen
< 8);
2148 if (INTEL_INFO(ring
->dev
)->gen
>= 9)
2149 ret
= GEN9_LR_CONTEXT_RENDER_SIZE
;
2151 ret
= GEN8_LR_CONTEXT_RENDER_SIZE
;
2157 ret
= GEN8_LR_CONTEXT_OTHER_SIZE
;
2164 static void lrc_setup_hardware_status_page(struct intel_engine_cs
*ring
,
2165 struct drm_i915_gem_object
*default_ctx_obj
)
2167 struct drm_i915_private
*dev_priv
= ring
->dev
->dev_private
;
2169 /* The status page is offset 0 from the default context object
2171 ring
->status_page
.gfx_addr
= i915_gem_obj_ggtt_offset(default_ctx_obj
);
2172 ring
->status_page
.page_addr
=
2173 kmap(sg_page(default_ctx_obj
->pages
->sgl
));
2174 ring
->status_page
.obj
= default_ctx_obj
;
2176 I915_WRITE(RING_HWS_PGA(ring
->mmio_base
),
2177 (u32
)ring
->status_page
.gfx_addr
);
2178 POSTING_READ(RING_HWS_PGA(ring
->mmio_base
));
2182 * intel_lr_context_deferred_create() - create the LRC specific bits of a context
2183 * @ctx: LR context to create.
2184 * @ring: engine to be used with the context.
2186 * This function can be called more than once, with different engines, if we plan
2187 * to use the context with them. The context backing objects and the ringbuffers
2188 * (specially the ringbuffer backing objects) suck a lot of memory up, and that's why
2189 * the creation is a deferred call: it's better to make sure first that we need to use
2190 * a given ring with the context.
2192 * Return: non-zero on error.
2194 int intel_lr_context_deferred_create(struct intel_context
*ctx
,
2195 struct intel_engine_cs
*ring
)
2197 const bool is_global_default_ctx
= (ctx
== ring
->default_context
);
2198 struct drm_device
*dev
= ring
->dev
;
2199 struct drm_i915_gem_object
*ctx_obj
;
2200 uint32_t context_size
;
2201 struct intel_ringbuffer
*ringbuf
;
2204 WARN_ON(ctx
->legacy_hw_ctx
.rcs_state
!= NULL
);
2205 WARN_ON(ctx
->engine
[ring
->id
].state
);
2207 context_size
= round_up(get_lr_context_size(ring
), 4096);
2209 ctx_obj
= i915_gem_alloc_object(dev
, context_size
);
2211 DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
2215 if (is_global_default_ctx
) {
2216 ret
= i915_gem_obj_ggtt_pin(ctx_obj
, GEN8_LR_CONTEXT_ALIGN
, 0);
2218 DRM_DEBUG_DRIVER("Pin LRC backing obj failed: %d\n",
2220 drm_gem_object_unreference(&ctx_obj
->base
);
2225 ringbuf
= kzalloc(sizeof(*ringbuf
), GFP_KERNEL
);
2227 DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s\n",
2230 goto error_unpin_ctx
;
2233 ringbuf
->ring
= ring
;
2235 ringbuf
->size
= 32 * PAGE_SIZE
;
2236 ringbuf
->effective_size
= ringbuf
->size
;
2239 ringbuf
->last_retired_head
= -1;
2240 intel_ring_update_space(ringbuf
);
2242 if (ringbuf
->obj
== NULL
) {
2243 ret
= intel_alloc_ringbuffer_obj(dev
, ringbuf
);
2246 "Failed to allocate ringbuffer obj %s: %d\n",
2248 goto error_free_rbuf
;
2251 if (is_global_default_ctx
) {
2252 ret
= intel_pin_and_map_ringbuffer_obj(dev
, ringbuf
);
2255 "Failed to pin and map ringbuffer %s: %d\n",
2257 goto error_destroy_rbuf
;
2263 ret
= populate_lr_context(ctx
, ctx_obj
, ring
, ringbuf
);
2265 DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret
);
2269 ctx
->engine
[ring
->id
].ringbuf
= ringbuf
;
2270 ctx
->engine
[ring
->id
].state
= ctx_obj
;
2272 if (ctx
== ring
->default_context
)
2273 lrc_setup_hardware_status_page(ring
, ctx_obj
);
2274 else if (ring
->id
== RCS
&& !ctx
->rcs_initialized
) {
2275 if (ring
->init_context
) {
2276 struct drm_i915_gem_request
*req
;
2278 ret
= i915_gem_request_alloc(ring
, ctx
, &req
);
2282 ret
= ring
->init_context(req
);
2284 DRM_ERROR("ring init context: %d\n", ret
);
2285 i915_gem_request_cancel(req
);
2286 ctx
->engine
[ring
->id
].ringbuf
= NULL
;
2287 ctx
->engine
[ring
->id
].state
= NULL
;
2291 i915_add_request_no_flush(req
);
2294 ctx
->rcs_initialized
= true;
2300 if (is_global_default_ctx
)
2301 intel_unpin_ringbuffer_obj(ringbuf
);
2303 intel_destroy_ringbuffer_obj(ringbuf
);
2307 if (is_global_default_ctx
)
2308 i915_gem_object_ggtt_unpin(ctx_obj
);
2309 drm_gem_object_unreference(&ctx_obj
->base
);
2313 void intel_lr_context_reset(struct drm_device
*dev
,
2314 struct intel_context
*ctx
)
2316 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
2317 struct intel_engine_cs
*ring
;
2320 for_each_ring(ring
, dev_priv
, i
) {
2321 struct drm_i915_gem_object
*ctx_obj
=
2322 ctx
->engine
[ring
->id
].state
;
2323 struct intel_ringbuffer
*ringbuf
=
2324 ctx
->engine
[ring
->id
].ringbuf
;
2325 uint32_t *reg_state
;
2331 if (i915_gem_object_get_pages(ctx_obj
)) {
2332 WARN(1, "Failed get_pages for context obj\n");
2335 page
= i915_gem_object_get_page(ctx_obj
, 1);
2336 reg_state
= kmap_atomic(page
);
2338 reg_state
[CTX_RING_HEAD
+1] = 0;
2339 reg_state
[CTX_RING_TAIL
+1] = 0;
2341 kunmap_atomic(reg_state
);