xref: /dragonfly/sys/dev/drm/i915/intel_lrc.c (revision d2d20701)
11b13d190SFrançois Tigeot /*
21b13d190SFrançois Tigeot  * Copyright © 2014 Intel Corporation
31b13d190SFrançois Tigeot  *
41b13d190SFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
51b13d190SFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
61b13d190SFrançois Tigeot  * to deal in the Software without restriction, including without limitation
71b13d190SFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
81b13d190SFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
91b13d190SFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
101b13d190SFrançois Tigeot  *
111b13d190SFrançois Tigeot  * The above copyright notice and this permission notice (including the next
121b13d190SFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
131b13d190SFrançois Tigeot  * Software.
141b13d190SFrançois Tigeot  *
151b13d190SFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
161b13d190SFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
171b13d190SFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
181b13d190SFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
191b13d190SFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
201b13d190SFrançois Tigeot  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
211b13d190SFrançois Tigeot  * IN THE SOFTWARE.
221b13d190SFrançois Tigeot  *
231b13d190SFrançois Tigeot  * Authors:
241b13d190SFrançois Tigeot  *    Ben Widawsky <ben@bwidawsk.net>
251b13d190SFrançois Tigeot  *    Michel Thierry <michel.thierry@intel.com>
261b13d190SFrançois Tigeot  *    Thomas Daniel <thomas.daniel@intel.com>
271b13d190SFrançois Tigeot  *    Oscar Mateo <oscar.mateo@intel.com>
281b13d190SFrançois Tigeot  *
291b13d190SFrançois Tigeot  */
301b13d190SFrançois Tigeot 
311b13d190SFrançois Tigeot /**
321b13d190SFrançois Tigeot  * DOC: Logical Rings, Logical Ring Contexts and Execlists
331b13d190SFrançois Tigeot  *
341b13d190SFrançois Tigeot  * Motivation:
351b13d190SFrançois Tigeot  * GEN8 brings an expansion of the HW contexts: "Logical Ring Contexts".
361b13d190SFrançois Tigeot  * These expanded contexts enable a number of new abilities, especially
371b13d190SFrançois Tigeot  * "Execlists" (also implemented in this file).
381b13d190SFrançois Tigeot  *
391b13d190SFrançois Tigeot  * One of the main differences with the legacy HW contexts is that logical
401b13d190SFrançois Tigeot  * ring contexts incorporate many more things to the context's state, like
411b13d190SFrançois Tigeot  * PDPs or ringbuffer control registers:
421b13d190SFrançois Tigeot  *
431b13d190SFrançois Tigeot  * The reason why PDPs are included in the context is straightforward: as
441b13d190SFrançois Tigeot  * PPGTTs (per-process GTTs) are actually per-context, having the PDPs
451b13d190SFrançois Tigeot  * contained there mean you don't need to do a ppgtt->switch_mm yourself,
461b13d190SFrançois Tigeot  * instead, the GPU will do it for you on the context switch.
471b13d190SFrançois Tigeot  *
481b13d190SFrançois Tigeot  * But, what about the ringbuffer control registers (head, tail, etc..)?
491b13d190SFrançois Tigeot  * shouldn't we just need a set of those per engine command streamer? This is
501b13d190SFrançois Tigeot  * where the name "Logical Rings" starts to make sense: by virtualizing the
511b13d190SFrançois Tigeot  * rings, the engine cs shifts to a new "ring buffer" with every context
521b13d190SFrançois Tigeot  * switch. When you want to submit a workload to the GPU you: A) choose your
531b13d190SFrançois Tigeot  * context, B) find its appropriate virtualized ring, C) write commands to it
541b13d190SFrançois Tigeot  * and then, finally, D) tell the GPU to switch to that context.
551b13d190SFrançois Tigeot  *
561b13d190SFrançois Tigeot  * Instead of the legacy MI_SET_CONTEXT, the way you tell the GPU to switch
571b13d190SFrançois Tigeot  * to a contexts is via a context execution list, ergo "Execlists".
581b13d190SFrançois Tigeot  *
591b13d190SFrançois Tigeot  * LRC implementation:
601b13d190SFrançois Tigeot  * Regarding the creation of contexts, we have:
611b13d190SFrançois Tigeot  *
621b13d190SFrançois Tigeot  * - One global default context.
631b13d190SFrançois Tigeot  * - One local default context for each opened fd.
641b13d190SFrançois Tigeot  * - One local extra context for each context create ioctl call.
651b13d190SFrançois Tigeot  *
661b13d190SFrançois Tigeot  * Now that ringbuffers belong per-context (and not per-engine, like before)
671b13d190SFrançois Tigeot  * and that contexts are uniquely tied to a given engine (and not reusable,
681b13d190SFrançois Tigeot  * like before) we need:
691b13d190SFrançois Tigeot  *
701b13d190SFrançois Tigeot  * - One ringbuffer per-engine inside each context.
711b13d190SFrançois Tigeot  * - One backing object per-engine inside each context.
721b13d190SFrançois Tigeot  *
731b13d190SFrançois Tigeot  * The global default context starts its life with these new objects fully
741b13d190SFrançois Tigeot  * allocated and populated. The local default context for each opened fd is
751b13d190SFrançois Tigeot  * more complex, because we don't know at creation time which engine is going
761b13d190SFrançois Tigeot  * to use them. To handle this, we have implemented a deferred creation of LR
771b13d190SFrançois Tigeot  * contexts:
781b13d190SFrançois Tigeot  *
791b13d190SFrançois Tigeot  * The local context starts its life as a hollow or blank holder, that only
801b13d190SFrançois Tigeot  * gets populated for a given engine once we receive an execbuffer. If later
811b13d190SFrançois Tigeot  * on we receive another execbuffer ioctl for the same context but a different
821b13d190SFrançois Tigeot  * engine, we allocate/populate a new ringbuffer and context backing object and
831b13d190SFrançois Tigeot  * so on.
841b13d190SFrançois Tigeot  *
851b13d190SFrançois Tigeot  * Finally, regarding local contexts created using the ioctl call: as they are
861b13d190SFrançois Tigeot  * only allowed with the render ring, we can allocate & populate them right
871b13d190SFrançois Tigeot  * away (no need to defer anything, at least for now).
881b13d190SFrançois Tigeot  *
891b13d190SFrançois Tigeot  * Execlists implementation:
901b13d190SFrançois Tigeot  * Execlists are the new method by which, on gen8+ hardware, workloads are
911b13d190SFrançois Tigeot  * submitted for execution (as opposed to the legacy, ringbuffer-based, method).
921b13d190SFrançois Tigeot  * This method works as follows:
931b13d190SFrançois Tigeot  *
941b13d190SFrançois Tigeot  * When a request is committed, its commands (the BB start and any leading or
951b13d190SFrançois Tigeot  * trailing commands, like the seqno breadcrumbs) are placed in the ringbuffer
961b13d190SFrançois Tigeot  * for the appropriate context. The tail pointer in the hardware context is not
971b13d190SFrançois Tigeot  * updated at this time, but instead, kept by the driver in the ringbuffer
981b13d190SFrançois Tigeot  * structure. A structure representing this request is added to a request queue
991b13d190SFrançois Tigeot  * for the appropriate engine: this structure contains a copy of the context's
1001b13d190SFrançois Tigeot  * tail after the request was written to the ring buffer and a pointer to the
1011b13d190SFrançois Tigeot  * context itself.
1021b13d190SFrançois Tigeot  *
1031b13d190SFrançois Tigeot  * If the engine's request queue was empty before the request was added, the
1041b13d190SFrançois Tigeot  * queue is processed immediately. Otherwise the queue will be processed during
1051b13d190SFrançois Tigeot  * a context switch interrupt. In any case, elements on the queue will get sent
1061b13d190SFrançois Tigeot  * (in pairs) to the GPU's ExecLists Submit Port (ELSP, for short) with a
1071b13d190SFrançois Tigeot  * globally unique 20-bits submission ID.
1081b13d190SFrançois Tigeot  *
1091b13d190SFrançois Tigeot  * When execution of a request completes, the GPU updates the context status
1101b13d190SFrançois Tigeot  * buffer with a context complete event and generates a context switch interrupt.
1111b13d190SFrançois Tigeot  * During the interrupt handling, the driver examines the events in the buffer:
1121b13d190SFrançois Tigeot  * for each context complete event, if the announced ID matches that on the head
1131b13d190SFrançois Tigeot  * of the request queue, then that request is retired and removed from the queue.
1141b13d190SFrançois Tigeot  *
1151b13d190SFrançois Tigeot  * After processing, if any requests were retired and the queue is not empty
1161b13d190SFrançois Tigeot  * then a new execution list can be submitted. The two requests at the front of
1171b13d190SFrançois Tigeot  * the queue are next to be submitted but since a context may not occur twice in
1181b13d190SFrançois Tigeot  * an execution list, if subsequent requests have the same ID as the first then
1191b13d190SFrançois Tigeot  * the two requests must be combined. This is done simply by discarding requests
1201b13d190SFrançois Tigeot  * at the head of the queue until either only one requests is left (in which case
1211b13d190SFrançois Tigeot  * we use a NULL second context) or the first two requests have unique IDs.
1221b13d190SFrançois Tigeot  *
1231b13d190SFrançois Tigeot  * By always executing the first two requests in the queue the driver ensures
1241b13d190SFrançois Tigeot  * that the GPU is kept as busy as possible. In the case where a single context
1251b13d190SFrançois Tigeot  * completes but a second context is still executing, the request for this second
1261b13d190SFrançois Tigeot  * context will be at the head of the queue when we remove the first one. This
1271b13d190SFrançois Tigeot  * request will then be resubmitted along with a new request for a different context,
1281b13d190SFrançois Tigeot  * which will cause the hardware to continue executing the second request and queue
1291b13d190SFrançois Tigeot  * the new request (the GPU detects the condition of a context getting preempted
1301b13d190SFrançois Tigeot  * with the same context and optimizes the context switch flow by not doing
1311b13d190SFrançois Tigeot  * preemption, but just sampling the new tail pointer).
1321b13d190SFrançois Tigeot  *
1331b13d190SFrançois Tigeot  */
1348621f407SFrançois Tigeot #include <linux/interrupt.h>
1351b13d190SFrançois Tigeot 
1361b13d190SFrançois Tigeot #include <drm/drmP.h>
1371b13d190SFrançois Tigeot #include <drm/i915_drm.h>
1381b13d190SFrançois Tigeot #include "i915_drv.h"
139a05eeebfSFrançois Tigeot #include "intel_mocs.h"
1401b13d190SFrançois Tigeot 
1411b13d190SFrançois Tigeot #define RING_EXECLIST_QFULL		(1 << 0x2)
1421b13d190SFrançois Tigeot #define RING_EXECLIST1_VALID		(1 << 0x3)
1431b13d190SFrançois Tigeot #define RING_EXECLIST0_VALID		(1 << 0x4)
1441b13d190SFrançois Tigeot #define RING_EXECLIST_ACTIVE_STATUS	(3 << 0xE)
1451b13d190SFrançois Tigeot #define RING_EXECLIST1_ACTIVE		(1 << 0x11)
1461b13d190SFrançois Tigeot #define RING_EXECLIST0_ACTIVE		(1 << 0x12)
1471b13d190SFrançois Tigeot 
1481b13d190SFrançois Tigeot #define GEN8_CTX_STATUS_IDLE_ACTIVE	(1 << 0)
1491b13d190SFrançois Tigeot #define GEN8_CTX_STATUS_PREEMPTED	(1 << 1)
1501b13d190SFrançois Tigeot #define GEN8_CTX_STATUS_ELEMENT_SWITCH	(1 << 2)
1511b13d190SFrançois Tigeot #define GEN8_CTX_STATUS_ACTIVE_IDLE	(1 << 3)
1521b13d190SFrançois Tigeot #define GEN8_CTX_STATUS_COMPLETE	(1 << 4)
1531b13d190SFrançois Tigeot #define GEN8_CTX_STATUS_LITE_RESTORE	(1 << 15)
1541b13d190SFrançois Tigeot 
1551e12ee3bSFrançois Tigeot #define GEN8_CTX_STATUS_COMPLETED_MASK \
1561e12ee3bSFrançois Tigeot 	 (GEN8_CTX_STATUS_ACTIVE_IDLE | \
1571e12ee3bSFrançois Tigeot 	  GEN8_CTX_STATUS_PREEMPTED | \
1581e12ee3bSFrançois Tigeot 	  GEN8_CTX_STATUS_ELEMENT_SWITCH)
1591e12ee3bSFrançois Tigeot 
1601b13d190SFrançois Tigeot #define CTX_LRI_HEADER_0		0x01
1611b13d190SFrançois Tigeot #define CTX_CONTEXT_CONTROL		0x02
1621b13d190SFrançois Tigeot #define CTX_RING_HEAD			0x04
1631b13d190SFrançois Tigeot #define CTX_RING_TAIL			0x06
1641b13d190SFrançois Tigeot #define CTX_RING_BUFFER_START		0x08
1651b13d190SFrançois Tigeot #define CTX_RING_BUFFER_CONTROL		0x0a
1661b13d190SFrançois Tigeot #define CTX_BB_HEAD_U			0x0c
1671b13d190SFrançois Tigeot #define CTX_BB_HEAD_L			0x0e
1681b13d190SFrançois Tigeot #define CTX_BB_STATE			0x10
1691b13d190SFrançois Tigeot #define CTX_SECOND_BB_HEAD_U		0x12
1701b13d190SFrançois Tigeot #define CTX_SECOND_BB_HEAD_L		0x14
1711b13d190SFrançois Tigeot #define CTX_SECOND_BB_STATE		0x16
1721b13d190SFrançois Tigeot #define CTX_BB_PER_CTX_PTR		0x18
1731b13d190SFrançois Tigeot #define CTX_RCS_INDIRECT_CTX		0x1a
1741b13d190SFrançois Tigeot #define CTX_RCS_INDIRECT_CTX_OFFSET	0x1c
1751b13d190SFrançois Tigeot #define CTX_LRI_HEADER_1		0x21
1761b13d190SFrançois Tigeot #define CTX_CTX_TIMESTAMP		0x22
1771b13d190SFrançois Tigeot #define CTX_PDP3_UDW			0x24
1781b13d190SFrançois Tigeot #define CTX_PDP3_LDW			0x26
1791b13d190SFrançois Tigeot #define CTX_PDP2_UDW			0x28
1801b13d190SFrançois Tigeot #define CTX_PDP2_LDW			0x2a
1811b13d190SFrançois Tigeot #define CTX_PDP1_UDW			0x2c
1821b13d190SFrançois Tigeot #define CTX_PDP1_LDW			0x2e
1831b13d190SFrançois Tigeot #define CTX_PDP0_UDW			0x30
1841b13d190SFrançois Tigeot #define CTX_PDP0_LDW			0x32
1851b13d190SFrançois Tigeot #define CTX_LRI_HEADER_2		0x41
1861b13d190SFrançois Tigeot #define CTX_R_PWR_CLK_STATE		0x42
1871b13d190SFrançois Tigeot #define CTX_GPGPU_CSR_BASE_ADDRESS	0x44
1881b13d190SFrançois Tigeot 
189a85cb24fSFrançois Tigeot #define CTX_REG(reg_state, pos, reg, val) do { \
190aee94f86SFrançois Tigeot 	(reg_state)[(pos)+0] = i915_mmio_reg_offset(reg); \
191aee94f86SFrançois Tigeot 	(reg_state)[(pos)+1] = (val); \
192aee94f86SFrançois Tigeot } while (0)
193aee94f86SFrançois Tigeot 
194aee94f86SFrançois Tigeot #define ASSIGN_CTX_PDP(ppgtt, reg_state, n) do {		\
195a05eeebfSFrançois Tigeot 	const u64 _addr = i915_page_dir_dma_addr((ppgtt), (n));	\
19619c468b4SFrançois Tigeot 	reg_state[CTX_PDP ## n ## _UDW+1] = upper_32_bits(_addr); \
19719c468b4SFrançois Tigeot 	reg_state[CTX_PDP ## n ## _LDW+1] = lower_32_bits(_addr); \
198aee94f86SFrançois Tigeot } while (0)
19919c468b4SFrançois Tigeot 
200aee94f86SFrançois Tigeot #define ASSIGN_CTX_PML4(ppgtt, reg_state) do { \
201352ff8bdSFrançois Tigeot 	reg_state[CTX_PDP0_UDW + 1] = upper_32_bits(px_dma(&ppgtt->pml4)); \
202352ff8bdSFrançois Tigeot 	reg_state[CTX_PDP0_LDW + 1] = lower_32_bits(px_dma(&ppgtt->pml4)); \
203aee94f86SFrançois Tigeot } while (0)
204352ff8bdSFrançois Tigeot 
205c0e85e96SFrançois Tigeot #define GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT	0x17
206c0e85e96SFrançois Tigeot #define GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT	0x26
2073f2dd94aSFrançois Tigeot #define GEN10_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT	0x19
2081b13d190SFrançois Tigeot 
2091487f786SFrançois Tigeot /* Typical size of the average request (2 pipecontrols and a MI_BB) */
2101487f786SFrançois Tigeot #define EXECLISTS_REQUEST_SIZE 64 /* bytes */
2111e12ee3bSFrançois Tigeot #define WA_TAIL_DWORDS 2
2123f2dd94aSFrançois Tigeot #define WA_TAIL_BYTES (sizeof(u32) * WA_TAIL_DWORDS)
2133f2dd94aSFrançois Tigeot #define PREEMPT_ID 0x1
2141e12ee3bSFrançois Tigeot 
2151487f786SFrançois Tigeot static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
2161487f786SFrançois Tigeot 					    struct intel_engine_cs *engine);
2171e12ee3bSFrançois Tigeot static void execlists_init_reg_state(u32 *reg_state,
2181e12ee3bSFrançois Tigeot 				     struct i915_gem_context *ctx,
2191e12ee3bSFrançois Tigeot 				     struct intel_engine_cs *engine,
2201e12ee3bSFrançois Tigeot 				     struct intel_ring *ring);
2212c9916cdSFrançois Tigeot 
2221b13d190SFrançois Tigeot /**
2231b13d190SFrançois Tigeot  * intel_sanitize_enable_execlists() - sanitize i915.enable_execlists
2241487f786SFrançois Tigeot  * @dev_priv: i915 device private
2251b13d190SFrançois Tigeot  * @enable_execlists: value of i915.enable_execlists module parameter.
2261b13d190SFrançois Tigeot  *
2271b13d190SFrançois Tigeot  * Only certain platforms support Execlists (the prerequisites being
2282c9916cdSFrançois Tigeot  * support for Logical Ring Contexts and Aliasing PPGTT or better).
2291b13d190SFrançois Tigeot  *
2301b13d190SFrançois Tigeot  * Return: 1 if Execlists is supported and has to be enabled.
2311b13d190SFrançois Tigeot  */
intel_sanitize_enable_execlists(struct drm_i915_private * dev_priv,int enable_execlists)2321487f786SFrançois Tigeot int intel_sanitize_enable_execlists(struct drm_i915_private *dev_priv, int enable_execlists)
2331b13d190SFrançois Tigeot {
234352ff8bdSFrançois Tigeot 	/* On platforms with execlist available, vGPU will only
235352ff8bdSFrançois Tigeot 	 * support execlist mode, no ring buffer mode.
236352ff8bdSFrançois Tigeot 	 */
2371487f786SFrançois Tigeot 	if (HAS_LOGICAL_RING_CONTEXTS(dev_priv) && intel_vgpu_active(dev_priv))
238352ff8bdSFrançois Tigeot 		return 1;
239352ff8bdSFrançois Tigeot 
2401487f786SFrançois Tigeot 	if (INTEL_GEN(dev_priv) >= 9)
2412c9916cdSFrançois Tigeot 		return 1;
2422c9916cdSFrançois Tigeot 
2431b13d190SFrançois Tigeot 	if (enable_execlists == 0)
2441b13d190SFrançois Tigeot 		return 0;
2451b13d190SFrançois Tigeot 
2461487f786SFrançois Tigeot 	if (HAS_LOGICAL_RING_CONTEXTS(dev_priv) &&
2473f2dd94aSFrançois Tigeot 	    USES_PPGTT(dev_priv))
2481b13d190SFrançois Tigeot 		return 1;
2491b13d190SFrançois Tigeot 
2501b13d190SFrançois Tigeot 	return 0;
2511b13d190SFrançois Tigeot }
2521b13d190SFrançois Tigeot 
253c0e85e96SFrançois Tigeot /**
254c0e85e96SFrançois Tigeot  * intel_lr_context_descriptor_update() - calculate & cache the descriptor
255c0e85e96SFrançois Tigeot  * 					  descriptor for a pinned context
256c0e85e96SFrançois Tigeot  * @ctx: Context to work on
2571487f786SFrançois Tigeot  * @engine: Engine the descriptor will be used with
258c0e85e96SFrançois Tigeot  *
259c0e85e96SFrançois Tigeot  * The context descriptor encodes various attributes of a context,
260c0e85e96SFrançois Tigeot  * including its GTT address and some flags. Because it's fairly
261c0e85e96SFrançois Tigeot  * expensive to calculate, we'll just do it once and cache the result,
262c0e85e96SFrançois Tigeot  * which remains valid until the context is unpinned.
263c0e85e96SFrançois Tigeot  *
26487df8fc6SFrançois Tigeot  * This is what a descriptor looks like, from LSB to MSB::
26587df8fc6SFrançois Tigeot  *
266a85cb24fSFrançois Tigeot  *      bits  0-11:    flags, GEN8_CTX_* (cached in ctx->desc_template)
267c0e85e96SFrançois Tigeot  *      bits 12-31:    LRCA, GTT address of (the HWSP of) this context
2681487f786SFrançois Tigeot  *      bits 32-52:    ctx ID, a globally unique tag
2691487f786SFrançois Tigeot  *      bits 53-54:    mbz, reserved for use by hardware
2701487f786SFrançois Tigeot  *      bits 55-63:    group ID, currently unused and set to 0
271c0e85e96SFrançois Tigeot  */
272c0e85e96SFrançois Tigeot static void
intel_lr_context_descriptor_update(struct i915_gem_context * ctx,struct intel_engine_cs * engine)2731487f786SFrançois Tigeot intel_lr_context_descriptor_update(struct i915_gem_context *ctx,
2748621f407SFrançois Tigeot 				   struct intel_engine_cs *engine)
275c0e85e96SFrançois Tigeot {
2761487f786SFrançois Tigeot 	struct intel_context *ce = &ctx->engine[engine->id];
2771487f786SFrançois Tigeot 	u64 desc;
278c0e85e96SFrançois Tigeot 
2791487f786SFrançois Tigeot 	BUILD_BUG_ON(MAX_CONTEXT_HW_ID > (1<<GEN8_CTX_ID_WIDTH));
280c0e85e96SFrançois Tigeot 
281a85cb24fSFrançois Tigeot 	desc = ctx->desc_template;				/* bits  0-11 */
2823f2dd94aSFrançois Tigeot 	desc |= i915_ggtt_offset(ce->state) + LRC_HEADER_PAGES * PAGE_SIZE;
2831487f786SFrançois Tigeot 								/* bits 12-31 */
2841487f786SFrançois Tigeot 	desc |= (u64)ctx->hw_id << GEN8_CTX_ID_SHIFT;		/* bits 32-52 */
285c0e85e96SFrançois Tigeot 
2861487f786SFrançois Tigeot 	ce->lrc_desc = desc;
287c0e85e96SFrançois Tigeot }
288c0e85e96SFrançois Tigeot 
2893f2dd94aSFrançois Tigeot static struct i915_priolist *
lookup_priolist(struct intel_engine_cs * engine,struct i915_priotree * pt,int prio)2903f2dd94aSFrançois Tigeot lookup_priolist(struct intel_engine_cs *engine,
2913f2dd94aSFrançois Tigeot 		struct i915_priotree *pt,
2923f2dd94aSFrançois Tigeot 		int prio)
293c0e85e96SFrançois Tigeot {
2943f2dd94aSFrançois Tigeot 	struct intel_engine_execlists * const execlists = &engine->execlists;
2953f2dd94aSFrançois Tigeot 	struct i915_priolist *p;
2963f2dd94aSFrançois Tigeot 	struct rb_node **parent, *rb;
2973f2dd94aSFrançois Tigeot 	bool first = true;
2983f2dd94aSFrançois Tigeot 
2993f2dd94aSFrançois Tigeot 	if (unlikely(execlists->no_priolist))
3003f2dd94aSFrançois Tigeot 		prio = I915_PRIORITY_NORMAL;
3013f2dd94aSFrançois Tigeot 
3023f2dd94aSFrançois Tigeot find_priolist:
3033f2dd94aSFrançois Tigeot 	/* most positive priority is scheduled first, equal priorities fifo */
3043f2dd94aSFrançois Tigeot 	rb = NULL;
3053f2dd94aSFrançois Tigeot 	parent = &execlists->queue.rb_node;
3063f2dd94aSFrançois Tigeot 	while (*parent) {
3073f2dd94aSFrançois Tigeot 		rb = *parent;
3083f2dd94aSFrançois Tigeot 		p = rb_entry(rb, typeof(*p), node);
3093f2dd94aSFrançois Tigeot 		if (prio > p->priority) {
3103f2dd94aSFrançois Tigeot 			parent = &rb->rb_left;
3113f2dd94aSFrançois Tigeot 		} else if (prio < p->priority) {
3123f2dd94aSFrançois Tigeot 			parent = &rb->rb_right;
3133f2dd94aSFrançois Tigeot 			first = false;
3143f2dd94aSFrançois Tigeot 		} else {
3153f2dd94aSFrançois Tigeot 			return p;
3163f2dd94aSFrançois Tigeot 		}
3173f2dd94aSFrançois Tigeot 	}
3183f2dd94aSFrançois Tigeot 
3193f2dd94aSFrançois Tigeot 	if (prio == I915_PRIORITY_NORMAL) {
3203f2dd94aSFrançois Tigeot 		p = &execlists->default_priolist;
3213f2dd94aSFrançois Tigeot 	} else {
3223f2dd94aSFrançois Tigeot 		p = kmem_cache_alloc(engine->i915->priorities, GFP_ATOMIC);
3233f2dd94aSFrançois Tigeot 		/* Convert an allocation failure to a priority bump */
3243f2dd94aSFrançois Tigeot 		if (unlikely(!p)) {
3253f2dd94aSFrançois Tigeot 			prio = I915_PRIORITY_NORMAL; /* recurses just once */
3263f2dd94aSFrançois Tigeot 
3273f2dd94aSFrançois Tigeot 			/* To maintain ordering with all rendering, after an
3283f2dd94aSFrançois Tigeot 			 * allocation failure we have to disable all scheduling.
3293f2dd94aSFrançois Tigeot 			 * Requests will then be executed in fifo, and schedule
3303f2dd94aSFrançois Tigeot 			 * will ensure that dependencies are emitted in fifo.
3313f2dd94aSFrançois Tigeot 			 * There will be still some reordering with existing
3323f2dd94aSFrançois Tigeot 			 * requests, so if userspace lied about their
3333f2dd94aSFrançois Tigeot 			 * dependencies that reordering may be visible.
3343f2dd94aSFrançois Tigeot 			 */
3353f2dd94aSFrançois Tigeot 			execlists->no_priolist = true;
3363f2dd94aSFrançois Tigeot 			goto find_priolist;
3373f2dd94aSFrançois Tigeot 		}
3383f2dd94aSFrançois Tigeot 	}
3393f2dd94aSFrançois Tigeot 
3403f2dd94aSFrançois Tigeot 	p->priority = prio;
3413f2dd94aSFrançois Tigeot 	INIT_LIST_HEAD(&p->requests);
3423f2dd94aSFrançois Tigeot 	rb_link_node(&p->node, rb, parent);
3433f2dd94aSFrançois Tigeot 	rb_insert_color(&p->node, &execlists->queue);
3443f2dd94aSFrançois Tigeot 
3453f2dd94aSFrançois Tigeot 	if (first)
3463f2dd94aSFrançois Tigeot 		execlists->first = &p->node;
3473f2dd94aSFrançois Tigeot 
3483f2dd94aSFrançois Tigeot 	return ptr_pack_bits(p, first, 1);
3493f2dd94aSFrançois Tigeot }
3503f2dd94aSFrançois Tigeot 
unwind_wa_tail(struct drm_i915_gem_request * rq)3513f2dd94aSFrançois Tigeot static void unwind_wa_tail(struct drm_i915_gem_request *rq)
3523f2dd94aSFrançois Tigeot {
3533f2dd94aSFrançois Tigeot 	rq->tail = intel_ring_wrap(rq->ring, rq->wa_tail - WA_TAIL_BYTES);
3543f2dd94aSFrançois Tigeot 	assert_ring_tail_valid(rq->ring, rq->tail);
3553f2dd94aSFrançois Tigeot }
3563f2dd94aSFrançois Tigeot 
unwind_incomplete_requests(struct intel_engine_cs * engine)3573f2dd94aSFrançois Tigeot static void unwind_incomplete_requests(struct intel_engine_cs *engine)
3583f2dd94aSFrançois Tigeot {
3593f2dd94aSFrançois Tigeot 	struct drm_i915_gem_request *rq, *rn;
3603f2dd94aSFrançois Tigeot 	struct i915_priolist *p = NULL;
3613f2dd94aSFrançois Tigeot 	int last_prio = I915_PRIORITY_INVALID;
3623f2dd94aSFrançois Tigeot 
3633f2dd94aSFrançois Tigeot 	lockdep_assert_held(&engine->timeline->lock);
3643f2dd94aSFrançois Tigeot 
3653f2dd94aSFrançois Tigeot 	list_for_each_entry_safe_reverse(rq, rn,
3663f2dd94aSFrançois Tigeot 					 &engine->timeline->requests,
3673f2dd94aSFrançois Tigeot 					 link) {
3683f2dd94aSFrançois Tigeot 		if (i915_gem_request_completed(rq))
3693f2dd94aSFrançois Tigeot 			return;
3703f2dd94aSFrançois Tigeot 
3713f2dd94aSFrançois Tigeot 		__i915_gem_request_unsubmit(rq);
3723f2dd94aSFrançois Tigeot 		unwind_wa_tail(rq);
3733f2dd94aSFrançois Tigeot 
3743f2dd94aSFrançois Tigeot 		GEM_BUG_ON(rq->priotree.priority == I915_PRIORITY_INVALID);
3753f2dd94aSFrançois Tigeot 		if (rq->priotree.priority != last_prio) {
3763f2dd94aSFrançois Tigeot 			p = lookup_priolist(engine,
3773f2dd94aSFrançois Tigeot 					    &rq->priotree,
3783f2dd94aSFrançois Tigeot 					    rq->priotree.priority);
3793f2dd94aSFrançois Tigeot 			p = ptr_mask_bits(p, 1);
3803f2dd94aSFrançois Tigeot 
3813f2dd94aSFrançois Tigeot 			last_prio = rq->priotree.priority;
3823f2dd94aSFrançois Tigeot 		}
3833f2dd94aSFrançois Tigeot 
3843f2dd94aSFrançois Tigeot 		list_add(&rq->priotree.link, &p->requests);
3853f2dd94aSFrançois Tigeot 	}
386c0e85e96SFrançois Tigeot }
387c0e85e96SFrançois Tigeot 
3881e12ee3bSFrançois Tigeot static inline void
execlists_context_status_change(struct drm_i915_gem_request * rq,unsigned long status)3891e12ee3bSFrançois Tigeot execlists_context_status_change(struct drm_i915_gem_request *rq,
3901487f786SFrançois Tigeot 				unsigned long status)
3911487f786SFrançois Tigeot {
3921487f786SFrançois Tigeot 	/*
3931487f786SFrançois Tigeot 	 * Only used when GVT-g is enabled now. When GVT-g is disabled,
3941487f786SFrançois Tigeot 	 * The compiler should eliminate this function as dead-code.
3951487f786SFrançois Tigeot 	 */
3961487f786SFrançois Tigeot 	if (!IS_ENABLED(CONFIG_DRM_I915_GVT))
3971487f786SFrançois Tigeot 		return;
3981487f786SFrançois Tigeot 
399a85cb24fSFrançois Tigeot 	atomic_notifier_call_chain(&rq->engine->context_status_notifier,
400a85cb24fSFrançois Tigeot 				   status, rq);
4011487f786SFrançois Tigeot }
4021487f786SFrançois Tigeot 
4031e12ee3bSFrançois Tigeot static void
execlists_update_context_pdps(struct i915_hw_ppgtt * ppgtt,u32 * reg_state)4041e12ee3bSFrançois Tigeot execlists_update_context_pdps(struct i915_hw_ppgtt *ppgtt, u32 *reg_state)
4051b13d190SFrançois Tigeot {
4061e12ee3bSFrançois Tigeot 	ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
4071e12ee3bSFrançois Tigeot 	ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
4081e12ee3bSFrançois Tigeot 	ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
4091e12ee3bSFrançois Tigeot 	ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
4101b13d190SFrançois Tigeot }
4111b13d190SFrançois Tigeot 
execlists_update_context(struct drm_i915_gem_request * rq)4121e12ee3bSFrançois Tigeot static u64 execlists_update_context(struct drm_i915_gem_request *rq)
4131b13d190SFrançois Tigeot {
4141e12ee3bSFrançois Tigeot 	struct intel_context *ce = &rq->ctx->engine[rq->engine->id];
4154be47400SFrançois Tigeot 	struct i915_hw_ppgtt *ppgtt =
4164be47400SFrançois Tigeot 		rq->ctx->ppgtt ?: rq->i915->mm.aliasing_ppgtt;
4171e12ee3bSFrançois Tigeot 	u32 *reg_state = ce->lrc_reg_state;
4181b13d190SFrançois Tigeot 
419a85cb24fSFrançois Tigeot 	reg_state[CTX_RING_TAIL+1] = intel_ring_set_tail(rq->ring, rq->tail);
4201b13d190SFrançois Tigeot 
4211e12ee3bSFrançois Tigeot 	/* True 32b PPGTT with dynamic page allocation: update PDP
4221e12ee3bSFrançois Tigeot 	 * registers and point the unallocated PDPs to scratch page.
4231e12ee3bSFrançois Tigeot 	 * PML4 is allocated during ppgtt init, so this is not needed
4241e12ee3bSFrançois Tigeot 	 * in 48-bit mode.
4251e12ee3bSFrançois Tigeot 	 */
426a85cb24fSFrançois Tigeot 	if (ppgtt && !i915_vm_is_48bit(&ppgtt->base))
4271e12ee3bSFrançois Tigeot 		execlists_update_context_pdps(ppgtt, reg_state);
4281b13d190SFrançois Tigeot 
4291e12ee3bSFrançois Tigeot 	return ce->lrc_desc;
4301b13d190SFrançois Tigeot }
4311b13d190SFrançois Tigeot 
elsp_write(u64 desc,u32 __iomem * elsp)4323f2dd94aSFrançois Tigeot static inline void elsp_write(u64 desc, u32 __iomem *elsp)
4333f2dd94aSFrançois Tigeot {
4343f2dd94aSFrançois Tigeot 	writel(upper_32_bits(desc), elsp);
4353f2dd94aSFrançois Tigeot 	writel(lower_32_bits(desc), elsp);
4363f2dd94aSFrançois Tigeot }
4373f2dd94aSFrançois Tigeot 
execlists_submit_ports(struct intel_engine_cs * engine)4381e12ee3bSFrançois Tigeot static void execlists_submit_ports(struct intel_engine_cs *engine)
439c0e85e96SFrançois Tigeot {
4403f2dd94aSFrançois Tigeot 	struct execlist_port *port = engine->execlists.port;
4411e12ee3bSFrançois Tigeot 	u32 __iomem *elsp =
4423f2dd94aSFrançois Tigeot 		engine->i915->regs + i915_mmio_reg_offset(RING_ELSP(engine));
4433f2dd94aSFrançois Tigeot 	unsigned int n;
444c0e85e96SFrançois Tigeot 
4453f2dd94aSFrançois Tigeot 	for (n = execlists_num_ports(&engine->execlists); n--; ) {
4463f2dd94aSFrançois Tigeot 		struct drm_i915_gem_request *rq;
4473f2dd94aSFrançois Tigeot 		unsigned int count;
4483f2dd94aSFrançois Tigeot 		u64 desc;
449c0e85e96SFrançois Tigeot 
4503f2dd94aSFrançois Tigeot 		rq = port_unpack(&port[n], &count);
4513f2dd94aSFrançois Tigeot 		if (rq) {
4523f2dd94aSFrançois Tigeot 			GEM_BUG_ON(count > !n);
4533f2dd94aSFrançois Tigeot 			if (!count++)
4543f2dd94aSFrançois Tigeot 				execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_IN);
4553f2dd94aSFrançois Tigeot 			port_set(&port[n], port_pack(rq, count));
4563f2dd94aSFrançois Tigeot 			desc = execlists_update_context(rq);
4573f2dd94aSFrançois Tigeot 			GEM_DEBUG_EXEC(port[n].context_id = upper_32_bits(desc));
4581e12ee3bSFrançois Tigeot 		} else {
4593f2dd94aSFrançois Tigeot 			GEM_BUG_ON(!n);
4603f2dd94aSFrançois Tigeot 			desc = 0;
4611e12ee3bSFrançois Tigeot 		}
4628621f407SFrançois Tigeot 
4633f2dd94aSFrançois Tigeot 		elsp_write(desc, elsp);
4643f2dd94aSFrançois Tigeot 	}
4651e12ee3bSFrançois Tigeot }
4668621f407SFrançois Tigeot 
ctx_single_port_submission(const struct i915_gem_context * ctx)4671e12ee3bSFrançois Tigeot static bool ctx_single_port_submission(const struct i915_gem_context *ctx)
4681e12ee3bSFrançois Tigeot {
4691e12ee3bSFrançois Tigeot 	return (IS_ENABLED(CONFIG_DRM_I915_GVT) &&
470a85cb24fSFrançois Tigeot 		i915_gem_context_force_single_submission(ctx));
4711e12ee3bSFrançois Tigeot }
4721e12ee3bSFrançois Tigeot 
can_merge_ctx(const struct i915_gem_context * prev,const struct i915_gem_context * next)4731e12ee3bSFrançois Tigeot static bool can_merge_ctx(const struct i915_gem_context *prev,
4741e12ee3bSFrançois Tigeot 			  const struct i915_gem_context *next)
4751e12ee3bSFrançois Tigeot {
4761e12ee3bSFrançois Tigeot 	if (prev != next)
4771e12ee3bSFrançois Tigeot 		return false;
4781e12ee3bSFrançois Tigeot 
4791e12ee3bSFrançois Tigeot 	if (ctx_single_port_submission(prev))
4801e12ee3bSFrançois Tigeot 		return false;
4811e12ee3bSFrançois Tigeot 
4821e12ee3bSFrançois Tigeot 	return true;
4831e12ee3bSFrançois Tigeot }
4841e12ee3bSFrançois Tigeot 
port_assign(struct execlist_port * port,struct drm_i915_gem_request * rq)4853f2dd94aSFrançois Tigeot static void port_assign(struct execlist_port *port,
4863f2dd94aSFrançois Tigeot 			struct drm_i915_gem_request *rq)
4873f2dd94aSFrançois Tigeot {
4883f2dd94aSFrançois Tigeot 	GEM_BUG_ON(rq == port_request(port));
4893f2dd94aSFrançois Tigeot 
4903f2dd94aSFrançois Tigeot 	if (port_isset(port))
4913f2dd94aSFrançois Tigeot 		i915_gem_request_put(port_request(port));
4923f2dd94aSFrançois Tigeot 
4933f2dd94aSFrançois Tigeot 	port_set(port, port_pack(i915_gem_request_get(rq), port_count(port)));
4943f2dd94aSFrançois Tigeot }
4953f2dd94aSFrançois Tigeot 
inject_preempt_context(struct intel_engine_cs * engine)4963f2dd94aSFrançois Tigeot static void inject_preempt_context(struct intel_engine_cs *engine)
4973f2dd94aSFrançois Tigeot {
4983f2dd94aSFrançois Tigeot 	struct intel_context *ce =
4993f2dd94aSFrançois Tigeot 		&engine->i915->preempt_context->engine[engine->id];
5003f2dd94aSFrançois Tigeot 	u32 __iomem *elsp =
5013f2dd94aSFrançois Tigeot 		engine->i915->regs + i915_mmio_reg_offset(RING_ELSP(engine));
5023f2dd94aSFrançois Tigeot 	unsigned int n;
5033f2dd94aSFrançois Tigeot 
5043f2dd94aSFrançois Tigeot 	GEM_BUG_ON(engine->i915->preempt_context->hw_id != PREEMPT_ID);
5053f2dd94aSFrançois Tigeot 	GEM_BUG_ON(!IS_ALIGNED(ce->ring->size, WA_TAIL_BYTES));
5063f2dd94aSFrançois Tigeot 
5073f2dd94aSFrançois Tigeot 	memset(ce->ring->vaddr + ce->ring->tail, 0, WA_TAIL_BYTES);
5083f2dd94aSFrançois Tigeot 	ce->ring->tail += WA_TAIL_BYTES;
5093f2dd94aSFrançois Tigeot 	ce->ring->tail &= (ce->ring->size - 1);
5103f2dd94aSFrançois Tigeot 	ce->lrc_reg_state[CTX_RING_TAIL+1] = ce->ring->tail;
5113f2dd94aSFrançois Tigeot 
5123f2dd94aSFrançois Tigeot 	for (n = execlists_num_ports(&engine->execlists); --n; )
5133f2dd94aSFrançois Tigeot 		elsp_write(0, elsp);
5143f2dd94aSFrançois Tigeot 
5153f2dd94aSFrançois Tigeot 	elsp_write(ce->lrc_desc, elsp);
5163f2dd94aSFrançois Tigeot }
5173f2dd94aSFrançois Tigeot 
can_preempt(struct intel_engine_cs * engine)5183f2dd94aSFrançois Tigeot static bool can_preempt(struct intel_engine_cs *engine)
5193f2dd94aSFrançois Tigeot {
5203f2dd94aSFrançois Tigeot 	return INTEL_INFO(engine->i915)->has_logical_ring_preemption;
5213f2dd94aSFrançois Tigeot }
5223f2dd94aSFrançois Tigeot 
execlists_dequeue(struct intel_engine_cs * engine)5231e12ee3bSFrançois Tigeot static void execlists_dequeue(struct intel_engine_cs *engine)
5241e12ee3bSFrançois Tigeot {
5253f2dd94aSFrançois Tigeot 	struct intel_engine_execlists * const execlists = &engine->execlists;
5263f2dd94aSFrançois Tigeot 	struct execlist_port *port = execlists->port;
5273f2dd94aSFrançois Tigeot 	const struct execlist_port * const last_port =
5283f2dd94aSFrançois Tigeot 		&execlists->port[execlists->port_mask];
5293f2dd94aSFrançois Tigeot 	struct drm_i915_gem_request *last = port_request(port);
5304be47400SFrançois Tigeot 	struct rb_node *rb;
5311e12ee3bSFrançois Tigeot 	bool submit = false;
5321e12ee3bSFrançois Tigeot 
5331e12ee3bSFrançois Tigeot 	/* Hardware submission is through 2 ports. Conceptually each port
5341e12ee3bSFrançois Tigeot 	 * has a (RING_START, RING_HEAD, RING_TAIL) tuple. RING_START is
5351e12ee3bSFrançois Tigeot 	 * static for a context, and unique to each, so we only execute
5361e12ee3bSFrançois Tigeot 	 * requests belonging to a single context from each ring. RING_HEAD
5371e12ee3bSFrançois Tigeot 	 * is maintained by the CS in the context image, it marks the place
5381e12ee3bSFrançois Tigeot 	 * where it got up to last time, and through RING_TAIL we tell the CS
5391e12ee3bSFrançois Tigeot 	 * where we want to execute up to this time.
5401e12ee3bSFrançois Tigeot 	 *
5411e12ee3bSFrançois Tigeot 	 * In this list the requests are in order of execution. Consecutive
5421e12ee3bSFrançois Tigeot 	 * requests from the same context are adjacent in the ringbuffer. We
5431e12ee3bSFrançois Tigeot 	 * can combine these requests into a single RING_TAIL update:
5441e12ee3bSFrançois Tigeot 	 *
5451e12ee3bSFrançois Tigeot 	 *              RING_HEAD...req1...req2
5461e12ee3bSFrançois Tigeot 	 *                                    ^- RING_TAIL
5471e12ee3bSFrançois Tigeot 	 * since to execute req2 the CS must first execute req1.
5481e12ee3bSFrançois Tigeot 	 *
5491e12ee3bSFrançois Tigeot 	 * Our goal then is to point each port to the end of a consecutive
5501e12ee3bSFrançois Tigeot 	 * sequence of requests as being the most optimal (fewest wake ups
5511e12ee3bSFrançois Tigeot 	 * and context switches) submission.
5521e12ee3bSFrançois Tigeot 	 */
5531e12ee3bSFrançois Tigeot 
554a85cb24fSFrançois Tigeot 	spin_lock_irq(&engine->timeline->lock);
5553f2dd94aSFrançois Tigeot 	rb = execlists->first;
5563f2dd94aSFrançois Tigeot 	GEM_BUG_ON(rb_first(&execlists->queue) != rb);
5573f2dd94aSFrançois Tigeot 	if (!rb)
5583f2dd94aSFrançois Tigeot 		goto unlock;
5594be47400SFrançois Tigeot 
5603f2dd94aSFrançois Tigeot 	if (last) {
5613f2dd94aSFrançois Tigeot 		/*
5623f2dd94aSFrançois Tigeot 		 * Don't resubmit or switch until all outstanding
5633f2dd94aSFrançois Tigeot 		 * preemptions (lite-restore) are seen. Then we
5643f2dd94aSFrançois Tigeot 		 * know the next preemption status we see corresponds
5653f2dd94aSFrançois Tigeot 		 * to this ELSP update.
5663f2dd94aSFrançois Tigeot 		 */
5673f2dd94aSFrançois Tigeot 		if (port_count(&port[0]) > 1)
5683f2dd94aSFrançois Tigeot 			goto unlock;
5693f2dd94aSFrançois Tigeot 
5703f2dd94aSFrançois Tigeot 		if (can_preempt(engine) &&
5713f2dd94aSFrançois Tigeot 		    rb_entry(rb, struct i915_priolist, node)->priority >
5723f2dd94aSFrançois Tigeot 		    max(last->priotree.priority, 0)) {
5733f2dd94aSFrançois Tigeot 			/*
5743f2dd94aSFrançois Tigeot 			 * Switch to our empty preempt context so
5753f2dd94aSFrançois Tigeot 			 * the state of the GPU is known (idle).
5763f2dd94aSFrançois Tigeot 			 */
5773f2dd94aSFrançois Tigeot 			inject_preempt_context(engine);
5783f2dd94aSFrançois Tigeot 			execlists_set_active(execlists,
5793f2dd94aSFrançois Tigeot 					     EXECLISTS_ACTIVE_PREEMPT);
5803f2dd94aSFrançois Tigeot 			goto unlock;
5813f2dd94aSFrançois Tigeot 		} else {
5823f2dd94aSFrançois Tigeot 			/*
5833f2dd94aSFrançois Tigeot 			 * In theory, we could coalesce more requests onto
5843f2dd94aSFrançois Tigeot 			 * the second port (the first port is active, with
5853f2dd94aSFrançois Tigeot 			 * no preemptions pending). However, that means we
5863f2dd94aSFrançois Tigeot 			 * then have to deal with the possible lite-restore
5873f2dd94aSFrançois Tigeot 			 * of the second port (as we submit the ELSP, there
5883f2dd94aSFrançois Tigeot 			 * may be a context-switch) but also we may complete
5893f2dd94aSFrançois Tigeot 			 * the resubmission before the context-switch. Ergo,
5903f2dd94aSFrançois Tigeot 			 * coalescing onto the second port will cause a
5913f2dd94aSFrançois Tigeot 			 * preemption event, but we cannot predict whether
5923f2dd94aSFrançois Tigeot 			 * that will affect port[0] or port[1].
5931e12ee3bSFrançois Tigeot 			 *
5943f2dd94aSFrançois Tigeot 			 * If the second port is already active, we can wait
5953f2dd94aSFrançois Tigeot 			 * until the next context-switch before contemplating
5963f2dd94aSFrançois Tigeot 			 * new requests. The GPU will be busy and we should be
5973f2dd94aSFrançois Tigeot 			 * able to resubmit the new ELSP before it idles,
5983f2dd94aSFrançois Tigeot 			 * avoiding pipeline bubbles (momentary pauses where
5993f2dd94aSFrançois Tigeot 			 * the driver is unable to keep up the supply of new
6003f2dd94aSFrançois Tigeot 			 * work).
6011e12ee3bSFrançois Tigeot 			 */
6023f2dd94aSFrançois Tigeot 			if (port_count(&port[1]))
6033f2dd94aSFrançois Tigeot 				goto unlock;
6041e12ee3bSFrançois Tigeot 
6053f2dd94aSFrançois Tigeot 			/* WaIdleLiteRestore:bdw,skl
6063f2dd94aSFrançois Tigeot 			 * Apply the wa NOOPs to prevent
6073f2dd94aSFrançois Tigeot 			 * ring:HEAD == req:TAIL as we resubmit the
6083f2dd94aSFrançois Tigeot 			 * request. See gen8_emit_breadcrumb() for
6093f2dd94aSFrançois Tigeot 			 * where we prepare the padding after the
6103f2dd94aSFrançois Tigeot 			 * end of the request.
6113f2dd94aSFrançois Tigeot 			 */
6123f2dd94aSFrançois Tigeot 			last->tail = last->wa_tail;
6133f2dd94aSFrançois Tigeot 		}
6143f2dd94aSFrançois Tigeot 	}
6153f2dd94aSFrançois Tigeot 
6163f2dd94aSFrançois Tigeot 	do {
6173f2dd94aSFrançois Tigeot 		struct i915_priolist *p = rb_entry(rb, typeof(*p), node);
6183f2dd94aSFrançois Tigeot 		struct drm_i915_gem_request *rq, *rn;
6193f2dd94aSFrançois Tigeot 
6203f2dd94aSFrançois Tigeot 		list_for_each_entry_safe(rq, rn, &p->requests, priotree.link) {
6213f2dd94aSFrançois Tigeot 			/*
6223f2dd94aSFrançois Tigeot 			 * Can we combine this request with the current port?
6233f2dd94aSFrançois Tigeot 			 * It has to be the same context/ringbuffer and not
6243f2dd94aSFrançois Tigeot 			 * have any exceptions (e.g. GVT saying never to
6253f2dd94aSFrançois Tigeot 			 * combine contexts).
6263f2dd94aSFrançois Tigeot 			 *
6273f2dd94aSFrançois Tigeot 			 * If we can combine the requests, we can execute both
6283f2dd94aSFrançois Tigeot 			 * by updating the RING_TAIL to point to the end of the
6293f2dd94aSFrançois Tigeot 			 * second request, and so we never need to tell the
6303f2dd94aSFrançois Tigeot 			 * hardware about the first.
6313f2dd94aSFrançois Tigeot 			 */
6323f2dd94aSFrançois Tigeot 			if (last && !can_merge_ctx(rq->ctx, last->ctx)) {
6333f2dd94aSFrançois Tigeot 				/*
6343f2dd94aSFrançois Tigeot 				 * If we are on the second port and cannot
6353f2dd94aSFrançois Tigeot 				 * combine this request with the last, then we
6363f2dd94aSFrançois Tigeot 				 * are done.
6373f2dd94aSFrançois Tigeot 				 */
6383f2dd94aSFrançois Tigeot 				if (port == last_port) {
6393f2dd94aSFrançois Tigeot 					__list_del_many(&p->requests,
6403f2dd94aSFrançois Tigeot 							&rq->priotree.link);
6413f2dd94aSFrançois Tigeot 					goto done;
6423f2dd94aSFrançois Tigeot 				}
6433f2dd94aSFrançois Tigeot 
6443f2dd94aSFrançois Tigeot 				/*
6453f2dd94aSFrançois Tigeot 				 * If GVT overrides us we only ever submit
6463f2dd94aSFrançois Tigeot 				 * port[0], leaving port[1] empty. Note that we
6473f2dd94aSFrançois Tigeot 				 * also have to be careful that we don't queue
6483f2dd94aSFrançois Tigeot 				 * the same context (even though a different
6493f2dd94aSFrançois Tigeot 				 * request) to the second port.
6501e12ee3bSFrançois Tigeot 				 */
6514be47400SFrançois Tigeot 				if (ctx_single_port_submission(last->ctx) ||
6523f2dd94aSFrançois Tigeot 				    ctx_single_port_submission(rq->ctx)) {
6533f2dd94aSFrançois Tigeot 					__list_del_many(&p->requests,
6543f2dd94aSFrançois Tigeot 							&rq->priotree.link);
6553f2dd94aSFrançois Tigeot 					goto done;
6563f2dd94aSFrançois Tigeot 				}
6571e12ee3bSFrançois Tigeot 
6583f2dd94aSFrançois Tigeot 				GEM_BUG_ON(last->ctx == rq->ctx);
6591e12ee3bSFrançois Tigeot 
6603f2dd94aSFrançois Tigeot 				if (submit)
6613f2dd94aSFrançois Tigeot 					port_assign(port, last);
6621e12ee3bSFrançois Tigeot 				port++;
6633f2dd94aSFrançois Tigeot 
6643f2dd94aSFrançois Tigeot 				GEM_BUG_ON(port_isset(port));
6653f2dd94aSFrançois Tigeot 			}
6663f2dd94aSFrançois Tigeot 
6673f2dd94aSFrançois Tigeot 			INIT_LIST_HEAD(&rq->priotree.link);
6683f2dd94aSFrançois Tigeot 			__i915_gem_request_submit(rq);
6693f2dd94aSFrançois Tigeot 			trace_i915_gem_request_in(rq, port_index(port, execlists));
6703f2dd94aSFrançois Tigeot 			last = rq;
6713f2dd94aSFrançois Tigeot 			submit = true;
6721e12ee3bSFrançois Tigeot 		}
6734be47400SFrançois Tigeot 
6744be47400SFrançois Tigeot 		rb = rb_next(rb);
6753f2dd94aSFrançois Tigeot 		rb_erase(&p->node, &execlists->queue);
6763f2dd94aSFrançois Tigeot 		INIT_LIST_HEAD(&p->requests);
6773f2dd94aSFrançois Tigeot 		if (p->priority != I915_PRIORITY_NORMAL)
6783f2dd94aSFrançois Tigeot 			kmem_cache_free(engine->i915->priorities, p);
6793f2dd94aSFrançois Tigeot 	} while (rb);
6803f2dd94aSFrançois Tigeot done:
6813f2dd94aSFrançois Tigeot 	execlists->first = rb;
6823f2dd94aSFrançois Tigeot 	if (submit)
6833f2dd94aSFrançois Tigeot 		port_assign(port, last);
6843f2dd94aSFrançois Tigeot unlock:
685a85cb24fSFrançois Tigeot 	spin_unlock_irq(&engine->timeline->lock);
6861e12ee3bSFrançois Tigeot 
6873f2dd94aSFrançois Tigeot 	if (submit) {
6883f2dd94aSFrançois Tigeot 		execlists_set_active(execlists, EXECLISTS_ACTIVE_USER);
6891e12ee3bSFrançois Tigeot 		execlists_submit_ports(engine);
6901e12ee3bSFrançois Tigeot 	}
6911e12ee3bSFrançois Tigeot }
6921e12ee3bSFrançois Tigeot 
6933f2dd94aSFrançois Tigeot static void
execlist_cancel_port_requests(struct intel_engine_execlists * execlists)6943f2dd94aSFrançois Tigeot execlist_cancel_port_requests(struct intel_engine_execlists *execlists)
6954be47400SFrançois Tigeot {
6963f2dd94aSFrançois Tigeot 	struct execlist_port *port = execlists->port;
6973f2dd94aSFrançois Tigeot 	unsigned int num_ports = execlists_num_ports(execlists);
6984be47400SFrançois Tigeot 
6993f2dd94aSFrançois Tigeot 	while (num_ports-- && port_isset(port)) {
7003f2dd94aSFrançois Tigeot 		struct drm_i915_gem_request *rq = port_request(port);
7013f2dd94aSFrançois Tigeot 
7023f2dd94aSFrançois Tigeot 		GEM_BUG_ON(!execlists->active);
7033f2dd94aSFrançois Tigeot 		execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_PREEMPTED);
7043f2dd94aSFrançois Tigeot 		i915_gem_request_put(rq);
7053f2dd94aSFrançois Tigeot 
7063f2dd94aSFrançois Tigeot 		memset(port, 0, sizeof(*port));
7073f2dd94aSFrançois Tigeot 		port++;
7083f2dd94aSFrançois Tigeot 	}
7093f2dd94aSFrançois Tigeot }
7103f2dd94aSFrançois Tigeot 
execlists_cancel_requests(struct intel_engine_cs * engine)7113f2dd94aSFrançois Tigeot static void execlists_cancel_requests(struct intel_engine_cs *engine)
7123f2dd94aSFrançois Tigeot {
7133f2dd94aSFrançois Tigeot 	struct intel_engine_execlists * const execlists = &engine->execlists;
7143f2dd94aSFrançois Tigeot 	struct drm_i915_gem_request *rq, *rn;
7153f2dd94aSFrançois Tigeot 	struct rb_node *rb;
7163f2dd94aSFrançois Tigeot 	unsigned long flags;
7173f2dd94aSFrançois Tigeot 
7183f2dd94aSFrançois Tigeot 	spin_lock_irqsave(&engine->timeline->lock, flags);
7193f2dd94aSFrançois Tigeot 
7203f2dd94aSFrançois Tigeot 	/* Cancel the requests on the HW and clear the ELSP tracker. */
7213f2dd94aSFrançois Tigeot 	execlist_cancel_port_requests(execlists);
7223f2dd94aSFrançois Tigeot 
7233f2dd94aSFrançois Tigeot 	/* Mark all executing requests as skipped. */
7243f2dd94aSFrançois Tigeot 	list_for_each_entry(rq, &engine->timeline->requests, link) {
7253f2dd94aSFrançois Tigeot 		GEM_BUG_ON(!rq->global_seqno);
7263f2dd94aSFrançois Tigeot 		if (!i915_gem_request_completed(rq))
7273f2dd94aSFrançois Tigeot 			dma_fence_set_error(&rq->fence, -EIO);
7283f2dd94aSFrançois Tigeot 	}
7293f2dd94aSFrançois Tigeot 
7303f2dd94aSFrançois Tigeot 	/* Flush the queued requests to the timeline list (for retiring). */
7313f2dd94aSFrançois Tigeot 	rb = execlists->first;
7323f2dd94aSFrançois Tigeot 	while (rb) {
7333f2dd94aSFrançois Tigeot 		struct i915_priolist *p = rb_entry(rb, typeof(*p), node);
7343f2dd94aSFrançois Tigeot 
7353f2dd94aSFrançois Tigeot 		list_for_each_entry_safe(rq, rn, &p->requests, priotree.link) {
7363f2dd94aSFrançois Tigeot 			INIT_LIST_HEAD(&rq->priotree.link);
7373f2dd94aSFrançois Tigeot 
7383f2dd94aSFrançois Tigeot 			dma_fence_set_error(&rq->fence, -EIO);
7393f2dd94aSFrançois Tigeot 			__i915_gem_request_submit(rq);
7403f2dd94aSFrançois Tigeot 		}
7413f2dd94aSFrançois Tigeot 
7423f2dd94aSFrançois Tigeot 		rb = rb_next(rb);
7433f2dd94aSFrançois Tigeot 		rb_erase(&p->node, &execlists->queue);
7443f2dd94aSFrançois Tigeot 		INIT_LIST_HEAD(&p->requests);
7453f2dd94aSFrançois Tigeot 		if (p->priority != I915_PRIORITY_NORMAL)
7463f2dd94aSFrançois Tigeot 			kmem_cache_free(engine->i915->priorities, p);
7473f2dd94aSFrançois Tigeot 	}
7483f2dd94aSFrançois Tigeot 
7493f2dd94aSFrançois Tigeot 	/* Remaining _unready_ requests will be nop'ed when submitted */
7503f2dd94aSFrançois Tigeot 
7513f2dd94aSFrançois Tigeot 
7523f2dd94aSFrançois Tigeot 	execlists->queue = LINUX_RB_ROOT;
7533f2dd94aSFrançois Tigeot 	execlists->first = NULL;
7543f2dd94aSFrançois Tigeot 	GEM_BUG_ON(port_isset(execlists->port));
7553f2dd94aSFrançois Tigeot 
7563f2dd94aSFrançois Tigeot 	/*
7573f2dd94aSFrançois Tigeot 	 * The port is checked prior to scheduling a tasklet, but
7583f2dd94aSFrançois Tigeot 	 * just in case we have suspended the tasklet to do the
7593f2dd94aSFrançois Tigeot 	 * wedging make sure that when it wakes, it decides there
7603f2dd94aSFrançois Tigeot 	 * is no work to do by clearing the irq_posted bit.
7613f2dd94aSFrançois Tigeot 	 */
7623f2dd94aSFrançois Tigeot 	clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
7633f2dd94aSFrançois Tigeot 
7643f2dd94aSFrançois Tigeot 	spin_unlock_irqrestore(&engine->timeline->lock, flags);
765c0e85e96SFrançois Tigeot }
766c0e85e96SFrançois Tigeot 
76787df8fc6SFrançois Tigeot /*
7681b13d190SFrançois Tigeot  * Check the unread Context Status Buffers and manage the submission of new
7691b13d190SFrançois Tigeot  * contexts to the ELSP accordingly.
7701b13d190SFrançois Tigeot  */
intel_lrc_irq_handler(unsigned long data)7718621f407SFrançois Tigeot static void intel_lrc_irq_handler(unsigned long data)
7721b13d190SFrançois Tigeot {
7733f2dd94aSFrançois Tigeot 	struct intel_engine_cs * const engine = (struct intel_engine_cs *)data;
7743f2dd94aSFrançois Tigeot 	struct intel_engine_execlists * const execlists = &engine->execlists;
7753f2dd94aSFrançois Tigeot 	struct execlist_port * const port = execlists->port;
7761487f786SFrançois Tigeot 	struct drm_i915_private *dev_priv = engine->i915;
7771b13d190SFrançois Tigeot 
7783f2dd94aSFrançois Tigeot 	/* We can skip acquiring intel_runtime_pm_get() here as it was taken
7793f2dd94aSFrançois Tigeot 	 * on our behalf by the request (see i915_gem_mark_busy()) and it will
7803f2dd94aSFrançois Tigeot 	 * not be relinquished until the device is idle (see
7813f2dd94aSFrançois Tigeot 	 * i915_gem_idle_work_handler()). As a precaution, we make sure
7823f2dd94aSFrançois Tigeot 	 * that all ELSP are drained i.e. we have processed the CSB,
7833f2dd94aSFrançois Tigeot 	 * before allowing ourselves to idle and calling intel_runtime_pm_put().
7843f2dd94aSFrançois Tigeot 	 */
7853f2dd94aSFrançois Tigeot 	GEM_BUG_ON(!dev_priv->gt.awake);
7863f2dd94aSFrançois Tigeot 
7873f2dd94aSFrançois Tigeot 	intel_uncore_forcewake_get(dev_priv, execlists->fw_domains);
7881b13d190SFrançois Tigeot 
789a85cb24fSFrançois Tigeot 	/* Prefer doing test_and_clear_bit() as a two stage operation to avoid
790a85cb24fSFrançois Tigeot 	 * imposing the cost of a locked atomic transaction when submitting a
791a85cb24fSFrançois Tigeot 	 * new request (outside of the context-switch interrupt).
792a85cb24fSFrançois Tigeot 	 */
793a85cb24fSFrançois Tigeot 	while (test_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted)) {
7943f2dd94aSFrançois Tigeot 		/* The HWSP contains a (cacheable) mirror of the CSB */
7953f2dd94aSFrançois Tigeot 		const u32 *buf =
7963f2dd94aSFrançois Tigeot 			&engine->status_page.page_addr[I915_HWS_CSB_BUF0_INDEX];
797a85cb24fSFrançois Tigeot 		unsigned int head, tail;
7988621f407SFrançois Tigeot 
7993f2dd94aSFrançois Tigeot 		if (unlikely(execlists->csb_use_mmio)) {
8003f2dd94aSFrançois Tigeot 			buf = (u32 * __force)
8013f2dd94aSFrançois Tigeot 				(dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_BUF_LO(engine, 0)));
8023f2dd94aSFrançois Tigeot 			execlists->csb_head = -1; /* force mmio read of CSB ptrs */
8033f2dd94aSFrançois Tigeot 		}
8043f2dd94aSFrançois Tigeot 
805a85cb24fSFrançois Tigeot 		/* The write will be ordered by the uncached read (itself
806a85cb24fSFrançois Tigeot 		 * a memory barrier), so we do not need another in the form
807a85cb24fSFrançois Tigeot 		 * of a locked instruction. The race between the interrupt
808a85cb24fSFrançois Tigeot 		 * handler and the split test/clear is harmless as we order
809a85cb24fSFrançois Tigeot 		 * our clear before the CSB read. If the interrupt arrived
810a85cb24fSFrançois Tigeot 		 * first between the test and the clear, we read the updated
811a85cb24fSFrançois Tigeot 		 * CSB and clear the bit. If the interrupt arrives as we read
812a85cb24fSFrançois Tigeot 		 * the CSB or later (i.e. after we had cleared the bit) the bit
813a85cb24fSFrançois Tigeot 		 * is set and we do a new loop.
814a85cb24fSFrançois Tigeot 		 */
815a85cb24fSFrançois Tigeot 		__clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
8163f2dd94aSFrançois Tigeot 		if (unlikely(execlists->csb_head == -1)) { /* following a reset */
8173f2dd94aSFrançois Tigeot 			head = readl(dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)));
818a85cb24fSFrançois Tigeot 			tail = GEN8_CSB_WRITE_PTR(head);
819a85cb24fSFrançois Tigeot 			head = GEN8_CSB_READ_PTR(head);
8203f2dd94aSFrançois Tigeot 			execlists->csb_head = head;
8213f2dd94aSFrançois Tigeot 		} else {
8223f2dd94aSFrançois Tigeot 			const int write_idx =
8233f2dd94aSFrançois Tigeot 				intel_hws_csb_write_index(dev_priv) -
8243f2dd94aSFrançois Tigeot 				I915_HWS_CSB_BUF0_INDEX;
8253f2dd94aSFrançois Tigeot 
8263f2dd94aSFrançois Tigeot 			head = execlists->csb_head;
8273f2dd94aSFrançois Tigeot 			tail = READ_ONCE(buf[write_idx]);
8283f2dd94aSFrançois Tigeot 		}
8293f2dd94aSFrançois Tigeot 
830a85cb24fSFrançois Tigeot 		while (head != tail) {
8313f2dd94aSFrançois Tigeot 			struct drm_i915_gem_request *rq;
832a85cb24fSFrançois Tigeot 			unsigned int status;
8333f2dd94aSFrançois Tigeot 			unsigned int count;
8341b13d190SFrançois Tigeot 
835a85cb24fSFrançois Tigeot 			if (++head == GEN8_CSB_ENTRIES)
836a85cb24fSFrançois Tigeot 				head = 0;
837a85cb24fSFrançois Tigeot 
838a85cb24fSFrançois Tigeot 			/* We are flying near dragons again.
839a85cb24fSFrançois Tigeot 			 *
840a85cb24fSFrançois Tigeot 			 * We hold a reference to the request in execlist_port[]
841a85cb24fSFrançois Tigeot 			 * but no more than that. We are operating in softirq
842a85cb24fSFrançois Tigeot 			 * context and so cannot hold any mutex or sleep. That
843a85cb24fSFrançois Tigeot 			 * prevents us stopping the requests we are processing
844a85cb24fSFrançois Tigeot 			 * in port[] from being retired simultaneously (the
845a85cb24fSFrançois Tigeot 			 * breadcrumb will be complete before we see the
846a85cb24fSFrançois Tigeot 			 * context-switch). As we only hold the reference to the
847a85cb24fSFrançois Tigeot 			 * request, any pointer chasing underneath the request
848a85cb24fSFrançois Tigeot 			 * is subject to a potential use-after-free. Thus we
849a85cb24fSFrançois Tigeot 			 * store all of the bookkeeping within port[] as
850a85cb24fSFrançois Tigeot 			 * required, and avoid using unguarded pointers beneath
851a85cb24fSFrançois Tigeot 			 * request itself. The same applies to the atomic
852a85cb24fSFrançois Tigeot 			 * status notifier.
853a85cb24fSFrançois Tigeot 			 */
854a85cb24fSFrançois Tigeot 
8553f2dd94aSFrançois Tigeot 			status = READ_ONCE(buf[2 * head]); /* maybe mmio! */
8561e12ee3bSFrançois Tigeot 			if (!(status & GEN8_CTX_STATUS_COMPLETED_MASK))
8571e12ee3bSFrançois Tigeot 				continue;
8581e12ee3bSFrançois Tigeot 
8593f2dd94aSFrançois Tigeot 			if (status & GEN8_CTX_STATUS_ACTIVE_IDLE &&
8603f2dd94aSFrançois Tigeot 			    buf[2*head + 1] == PREEMPT_ID) {
8613f2dd94aSFrançois Tigeot 				execlist_cancel_port_requests(execlists);
8623f2dd94aSFrançois Tigeot 
8633f2dd94aSFrançois Tigeot 				spin_lock_irq(&engine->timeline->lock);
8643f2dd94aSFrançois Tigeot 				unwind_incomplete_requests(engine);
8653f2dd94aSFrançois Tigeot 				spin_unlock_irq(&engine->timeline->lock);
8663f2dd94aSFrançois Tigeot 
8673f2dd94aSFrançois Tigeot 				GEM_BUG_ON(!execlists_is_active(execlists,
8683f2dd94aSFrançois Tigeot 								EXECLISTS_ACTIVE_PREEMPT));
8693f2dd94aSFrançois Tigeot 				execlists_clear_active(execlists,
8703f2dd94aSFrançois Tigeot 						       EXECLISTS_ACTIVE_PREEMPT);
8713f2dd94aSFrançois Tigeot 				continue;
8723f2dd94aSFrançois Tigeot 			}
8733f2dd94aSFrançois Tigeot 
8743f2dd94aSFrançois Tigeot 			if (status & GEN8_CTX_STATUS_PREEMPTED &&
8753f2dd94aSFrançois Tigeot 			    execlists_is_active(execlists,
8763f2dd94aSFrançois Tigeot 						EXECLISTS_ACTIVE_PREEMPT))
8773f2dd94aSFrançois Tigeot 				continue;
8783f2dd94aSFrançois Tigeot 
8793f2dd94aSFrançois Tigeot 			GEM_BUG_ON(!execlists_is_active(execlists,
8803f2dd94aSFrançois Tigeot 							EXECLISTS_ACTIVE_USER));
8813f2dd94aSFrançois Tigeot 
882a85cb24fSFrançois Tigeot 			/* Check the context/desc id for this event matches */
8833f2dd94aSFrançois Tigeot 			GEM_DEBUG_BUG_ON(buf[2 * head + 1] != port->context_id);
884a85cb24fSFrançois Tigeot 
8853f2dd94aSFrançois Tigeot 			rq = port_unpack(port, &count);
8863f2dd94aSFrançois Tigeot 			GEM_BUG_ON(count == 0);
8873f2dd94aSFrançois Tigeot 			if (--count == 0) {
8881e12ee3bSFrançois Tigeot 				GEM_BUG_ON(status & GEN8_CTX_STATUS_PREEMPTED);
889*d2d20701SMatthew Dillon 				/*
890*d2d20701SMatthew Dillon 				 * XXX DragonFly XXX
891*d2d20701SMatthew Dillon 				 *
892*d2d20701SMatthew Dillon 				 * This gets hit for me on an i5-6500 on X
893*d2d20701SMatthew Dillon 				 * startup.  Report and ignore for now.  May
894*d2d20701SMatthew Dillon 				 * be related to a ring timeout during early
895*d2d20701SMatthew Dillon 				 * startup.
896*d2d20701SMatthew Dillon 				 */
897*d2d20701SMatthew Dillon 				//GEM_BUG_ON(!i915_gem_request_completed(rq));
898*d2d20701SMatthew Dillon 				if (!i915_gem_request_completed(rq)) {
899*d2d20701SMatthew Dillon 					kprintf("i915: warning, request %p "
900*d2d20701SMatthew Dillon 						"not completed\n", rq);
901*d2d20701SMatthew Dillon 				}
9023f2dd94aSFrançois Tigeot 				execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_OUT);
9031e12ee3bSFrançois Tigeot 
9043f2dd94aSFrançois Tigeot 				trace_i915_gem_request_out(rq);
9053f2dd94aSFrançois Tigeot 				i915_gem_request_put(rq);
9063f2dd94aSFrançois Tigeot 
9073f2dd94aSFrançois Tigeot 				execlists_port_complete(execlists, port);
9083f2dd94aSFrançois Tigeot 			} else {
9093f2dd94aSFrançois Tigeot 				port_set(port, port_pack(rq, count));
9108621f407SFrançois Tigeot 			}
911c0e85e96SFrançois Tigeot 
9123f2dd94aSFrançois Tigeot 			/* After the final element, the hw should be idle */
9133f2dd94aSFrançois Tigeot 			GEM_BUG_ON(port_count(port) == 0 &&
9141e12ee3bSFrançois Tigeot 				   !(status & GEN8_CTX_STATUS_ACTIVE_IDLE));
9153f2dd94aSFrançois Tigeot 			if (port_count(port) == 0)
9163f2dd94aSFrançois Tigeot 				execlists_clear_active(execlists,
9173f2dd94aSFrançois Tigeot 						       EXECLISTS_ACTIVE_USER);
9181e12ee3bSFrançois Tigeot 		}
919a05eeebfSFrançois Tigeot 
9203f2dd94aSFrançois Tigeot 		if (head != execlists->csb_head) {
9213f2dd94aSFrançois Tigeot 			execlists->csb_head = head;
922a85cb24fSFrançois Tigeot 			writel(_MASKED_FIELD(GEN8_CSB_READ_PTR_MASK, head << 8),
9233f2dd94aSFrançois Tigeot 			       dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)));
9243f2dd94aSFrançois Tigeot 		}
9251e12ee3bSFrançois Tigeot 	}
9261e12ee3bSFrançois Tigeot 
9273f2dd94aSFrançois Tigeot 	if (!execlists_is_active(execlists, EXECLISTS_ACTIVE_PREEMPT))
9281e12ee3bSFrançois Tigeot 		execlists_dequeue(engine);
9291b13d190SFrançois Tigeot 
9303f2dd94aSFrançois Tigeot 	intel_uncore_forcewake_put(dev_priv, execlists->fw_domains);
9311b13d190SFrançois Tigeot }
9321b13d190SFrançois Tigeot 
insert_request(struct intel_engine_cs * engine,struct i915_priotree * pt,int prio)9333f2dd94aSFrançois Tigeot static void insert_request(struct intel_engine_cs *engine,
9343f2dd94aSFrançois Tigeot 			   struct i915_priotree *pt,
9353f2dd94aSFrançois Tigeot 			   int prio)
9364be47400SFrançois Tigeot {
9373f2dd94aSFrançois Tigeot 	struct i915_priolist *p = lookup_priolist(engine, pt, prio);
9384be47400SFrançois Tigeot 
9393f2dd94aSFrançois Tigeot 	list_add_tail(&pt->link, &ptr_mask_bits(p, 1)->requests);
9403f2dd94aSFrançois Tigeot 	if (ptr_unmask_bits(p, 1))
9413f2dd94aSFrançois Tigeot 		tasklet_hi_schedule(&engine->execlists.irq_tasklet);
9424be47400SFrançois Tigeot }
9434be47400SFrançois Tigeot 
execlists_submit_request(struct drm_i915_gem_request * request)94471f41f3eSFrançois Tigeot static void execlists_submit_request(struct drm_i915_gem_request *request)
9451b13d190SFrançois Tigeot {
9468621f407SFrançois Tigeot 	struct intel_engine_cs *engine = request->engine;
9471e12ee3bSFrançois Tigeot 	unsigned long flags;
9481b13d190SFrançois Tigeot 
9494be47400SFrançois Tigeot 	/* Will be called from irq-context when using foreign fences. */
9504be47400SFrançois Tigeot 	spin_lock_irqsave(&engine->timeline->lock, flags);
9511b13d190SFrançois Tigeot 
9523f2dd94aSFrançois Tigeot 	insert_request(engine, &request->priotree, request->priotree.priority);
9533f2dd94aSFrançois Tigeot 
9543f2dd94aSFrançois Tigeot 	GEM_BUG_ON(!engine->execlists.first);
9553f2dd94aSFrançois Tigeot 	GEM_BUG_ON(list_empty(&request->priotree.link));
9561b13d190SFrançois Tigeot 
9574be47400SFrançois Tigeot 	spin_unlock_irqrestore(&engine->timeline->lock, flags);
9584be47400SFrançois Tigeot }
9594be47400SFrançois Tigeot 
pt_to_request(struct i915_priotree * pt)9603f2dd94aSFrançois Tigeot static struct drm_i915_gem_request *pt_to_request(struct i915_priotree *pt)
9613f2dd94aSFrançois Tigeot {
9623f2dd94aSFrançois Tigeot 	return container_of(pt, struct drm_i915_gem_request, priotree);
9633f2dd94aSFrançois Tigeot }
9643f2dd94aSFrançois Tigeot 
9654be47400SFrançois Tigeot static struct intel_engine_cs *
pt_lock_engine(struct i915_priotree * pt,struct intel_engine_cs * locked)9664be47400SFrançois Tigeot pt_lock_engine(struct i915_priotree *pt, struct intel_engine_cs *locked)
9674be47400SFrançois Tigeot {
9683f2dd94aSFrançois Tigeot 	struct intel_engine_cs *engine = pt_to_request(pt)->engine;
9694be47400SFrançois Tigeot 
970a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!locked);
971a85cb24fSFrançois Tigeot 
9724be47400SFrançois Tigeot 	if (engine != locked) {
973a85cb24fSFrançois Tigeot 		lockmgr(&locked->timeline->lock, LK_RELEASE);
974a85cb24fSFrançois Tigeot 		lockmgr(&engine->timeline->lock, LK_EXCLUSIVE);
9754be47400SFrançois Tigeot 	}
9764be47400SFrançois Tigeot 
9774be47400SFrançois Tigeot 	return engine;
9784be47400SFrançois Tigeot }
9794be47400SFrançois Tigeot 
execlists_schedule(struct drm_i915_gem_request * request,int prio)9804be47400SFrançois Tigeot static void execlists_schedule(struct drm_i915_gem_request *request, int prio)
9814be47400SFrançois Tigeot {
982a85cb24fSFrançois Tigeot 	struct intel_engine_cs *engine;
9834be47400SFrançois Tigeot 	struct i915_dependency *dep, *p;
9844be47400SFrançois Tigeot 	struct i915_dependency stack;
9854be47400SFrançois Tigeot 	LINUX_LIST_HEAD(dfs);
9864be47400SFrançois Tigeot 
9873f2dd94aSFrançois Tigeot 	GEM_BUG_ON(prio == I915_PRIORITY_INVALID);
9883f2dd94aSFrançois Tigeot 
9893f2dd94aSFrançois Tigeot 	if (i915_gem_request_completed(request))
9903f2dd94aSFrançois Tigeot 		return;
9913f2dd94aSFrançois Tigeot 
9924be47400SFrançois Tigeot 	if (prio <= READ_ONCE(request->priotree.priority))
9934be47400SFrançois Tigeot 		return;
9944be47400SFrançois Tigeot 
995a85cb24fSFrançois Tigeot 	/* Need BKL in order to use the temporary link inside i915_dependency */
996a85cb24fSFrançois Tigeot 	lockdep_assert_held(&request->i915->drm.struct_mutex);
9974be47400SFrançois Tigeot 
9984be47400SFrançois Tigeot 	stack.signaler = &request->priotree;
9994be47400SFrançois Tigeot 	list_add(&stack.dfs_link, &dfs);
10004be47400SFrançois Tigeot 
10014be47400SFrançois Tigeot 	/* Recursively bump all dependent priorities to match the new request.
10024be47400SFrançois Tigeot 	 *
10034be47400SFrançois Tigeot 	 * A naive approach would be to use recursion:
10044be47400SFrançois Tigeot 	 * static void update_priorities(struct i915_priotree *pt, prio) {
10054be47400SFrançois Tigeot 	 *	list_for_each_entry(dep, &pt->signalers_list, signal_link)
10064be47400SFrançois Tigeot 	 *		update_priorities(dep->signal, prio)
10074be47400SFrançois Tigeot 	 *	insert_request(pt);
10084be47400SFrançois Tigeot 	 * }
10094be47400SFrançois Tigeot 	 * but that may have unlimited recursion depth and so runs a very
10104be47400SFrançois Tigeot 	 * real risk of overunning the kernel stack. Instead, we build
10114be47400SFrançois Tigeot 	 * a flat list of all dependencies starting with the current request.
10124be47400SFrançois Tigeot 	 * As we walk the list of dependencies, we add all of its dependencies
10134be47400SFrançois Tigeot 	 * to the end of the list (this may include an already visited
10144be47400SFrançois Tigeot 	 * request) and continue to walk onwards onto the new dependencies. The
10154be47400SFrançois Tigeot 	 * end result is a topological list of requests in reverse order, the
10164be47400SFrançois Tigeot 	 * last element in the list is the request we must execute first.
10174be47400SFrançois Tigeot 	 */
10184be47400SFrançois Tigeot 	list_for_each_entry_safe(dep, p, &dfs, dfs_link) {
10194be47400SFrançois Tigeot 		struct i915_priotree *pt = dep->signaler;
10204be47400SFrançois Tigeot 
1021a85cb24fSFrançois Tigeot 		/* Within an engine, there can be no cycle, but we may
1022a85cb24fSFrançois Tigeot 		 * refer to the same dependency chain multiple times
1023a85cb24fSFrançois Tigeot 		 * (redundant dependencies are not eliminated) and across
1024a85cb24fSFrançois Tigeot 		 * engines.
1025a85cb24fSFrançois Tigeot 		 */
1026a85cb24fSFrançois Tigeot 		list_for_each_entry(p, &pt->signalers_list, signal_link) {
10273f2dd94aSFrançois Tigeot 			if (i915_gem_request_completed(pt_to_request(p->signaler)))
10283f2dd94aSFrançois Tigeot 				continue;
10293f2dd94aSFrançois Tigeot 
1030a85cb24fSFrançois Tigeot 			GEM_BUG_ON(p->signaler->priority < pt->priority);
10314be47400SFrançois Tigeot 			if (prio > READ_ONCE(p->signaler->priority))
10324be47400SFrançois Tigeot 				list_move_tail(&p->dfs_link, &dfs);
10334be47400SFrançois Tigeot 		}
1034a85cb24fSFrançois Tigeot 
1035a85cb24fSFrançois Tigeot 		list_safe_reset_next(dep, p, dfs_link);
10364be47400SFrançois Tigeot 	}
10374be47400SFrançois Tigeot 
10383f2dd94aSFrançois Tigeot 	/* If we didn't need to bump any existing priorities, and we haven't
10393f2dd94aSFrançois Tigeot 	 * yet submitted this request (i.e. there is no potential race with
10403f2dd94aSFrançois Tigeot 	 * execlists_submit_request()), we can set our own priority and skip
10413f2dd94aSFrançois Tigeot 	 * acquiring the engine locks.
10423f2dd94aSFrançois Tigeot 	 */
10433f2dd94aSFrançois Tigeot 	if (request->priotree.priority == I915_PRIORITY_INVALID) {
10443f2dd94aSFrançois Tigeot 		GEM_BUG_ON(!list_empty(&request->priotree.link));
10453f2dd94aSFrançois Tigeot 		request->priotree.priority = prio;
10463f2dd94aSFrançois Tigeot 		if (stack.dfs_link.next == stack.dfs_link.prev)
10473f2dd94aSFrançois Tigeot 			return;
10483f2dd94aSFrançois Tigeot 		__list_del_entry(&stack.dfs_link);
10493f2dd94aSFrançois Tigeot 	}
10503f2dd94aSFrançois Tigeot 
1051a85cb24fSFrançois Tigeot 	engine = request->engine;
1052a85cb24fSFrançois Tigeot 	spin_lock_irq(&engine->timeline->lock);
1053a85cb24fSFrançois Tigeot 
10544be47400SFrançois Tigeot 	/* Fifo and depth-first replacement ensure our deps execute before us */
10554be47400SFrançois Tigeot 	list_for_each_entry_safe_reverse(dep, p, &dfs, dfs_link) {
10564be47400SFrançois Tigeot 		struct i915_priotree *pt = dep->signaler;
10574be47400SFrançois Tigeot 
10584be47400SFrançois Tigeot 		INIT_LIST_HEAD(&dep->dfs_link);
10594be47400SFrançois Tigeot 
10604be47400SFrançois Tigeot 		engine = pt_lock_engine(pt, engine);
10614be47400SFrançois Tigeot 
10624be47400SFrançois Tigeot 		if (prio <= pt->priority)
10634be47400SFrançois Tigeot 			continue;
10644be47400SFrançois Tigeot 
10654be47400SFrançois Tigeot 		pt->priority = prio;
10663f2dd94aSFrançois Tigeot 		if (!list_empty(&pt->link)) {
10673f2dd94aSFrançois Tigeot 			__list_del_entry(&pt->link);
10683f2dd94aSFrançois Tigeot 			insert_request(engine, pt, prio);
10694be47400SFrançois Tigeot 		}
1070a85cb24fSFrançois Tigeot 	}
10714be47400SFrançois Tigeot 
10724be47400SFrançois Tigeot 	spin_unlock_irq(&engine->timeline->lock);
10731b13d190SFrançois Tigeot }
10741b13d190SFrançois Tigeot 
10753f2dd94aSFrançois Tigeot static struct intel_ring *
execlists_context_pin(struct intel_engine_cs * engine,struct i915_gem_context * ctx)10763f2dd94aSFrançois Tigeot execlists_context_pin(struct intel_engine_cs *engine,
1077a85cb24fSFrançois Tigeot 		      struct i915_gem_context *ctx)
1078a85cb24fSFrançois Tigeot {
1079a85cb24fSFrançois Tigeot 	struct intel_context *ce = &ctx->engine[engine->id];
1080a85cb24fSFrançois Tigeot 	unsigned int flags;
1081a85cb24fSFrançois Tigeot 	void *vaddr;
1082a85cb24fSFrançois Tigeot 	int ret;
1083a85cb24fSFrançois Tigeot 
1084a85cb24fSFrançois Tigeot 	lockdep_assert_held(&ctx->i915->drm.struct_mutex);
1085a85cb24fSFrançois Tigeot 
10863f2dd94aSFrançois Tigeot 	if (likely(ce->pin_count++))
10873f2dd94aSFrançois Tigeot 		goto out;
1088a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!ce->pin_count); /* no overflow please! */
1089a85cb24fSFrançois Tigeot 
1090a85cb24fSFrançois Tigeot 	if (!ce->state) {
1091a85cb24fSFrançois Tigeot 		ret = execlists_context_deferred_alloc(ctx, engine);
1092a85cb24fSFrançois Tigeot 		if (ret)
1093a85cb24fSFrançois Tigeot 			goto err;
1094a85cb24fSFrançois Tigeot 	}
1095a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!ce->state);
1096a85cb24fSFrançois Tigeot 
1097a85cb24fSFrançois Tigeot 	flags = PIN_GLOBAL | PIN_HIGH;
1098a85cb24fSFrançois Tigeot 	if (ctx->ggtt_offset_bias)
1099a85cb24fSFrançois Tigeot 		flags |= PIN_OFFSET_BIAS | ctx->ggtt_offset_bias;
1100a85cb24fSFrançois Tigeot 
1101a85cb24fSFrançois Tigeot 	ret = i915_vma_pin(ce->state, 0, GEN8_LR_CONTEXT_ALIGN, flags);
1102a85cb24fSFrançois Tigeot 	if (ret)
1103a85cb24fSFrançois Tigeot 		goto err;
1104a85cb24fSFrançois Tigeot 
1105a85cb24fSFrançois Tigeot 	vaddr = i915_gem_object_pin_map(ce->state->obj, I915_MAP_WB);
1106a85cb24fSFrançois Tigeot 	if (IS_ERR(vaddr)) {
1107a85cb24fSFrançois Tigeot 		ret = PTR_ERR(vaddr);
1108a85cb24fSFrançois Tigeot 		goto unpin_vma;
1109a85cb24fSFrançois Tigeot 	}
1110a85cb24fSFrançois Tigeot 
11113f2dd94aSFrançois Tigeot 	ret = intel_ring_pin(ce->ring, ctx->i915, ctx->ggtt_offset_bias);
1112a85cb24fSFrançois Tigeot 	if (ret)
1113a85cb24fSFrançois Tigeot 		goto unpin_map;
1114a85cb24fSFrançois Tigeot 
1115a85cb24fSFrançois Tigeot 	intel_lr_context_descriptor_update(ctx, engine);
1116a85cb24fSFrançois Tigeot 
1117a85cb24fSFrançois Tigeot 	ce->lrc_reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
1118a85cb24fSFrançois Tigeot 	ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
1119a85cb24fSFrançois Tigeot 		i915_ggtt_offset(ce->ring->vma);
1120a85cb24fSFrançois Tigeot 
1121a85cb24fSFrançois Tigeot 	ce->state->obj->mm.dirty = true;
11223f2dd94aSFrançois Tigeot 	ce->state->obj->pin_global++;
1123a85cb24fSFrançois Tigeot 
1124a85cb24fSFrançois Tigeot 	i915_gem_context_get(ctx);
11253f2dd94aSFrançois Tigeot out:
11263f2dd94aSFrançois Tigeot 	return ce->ring;
1127a85cb24fSFrançois Tigeot 
1128a85cb24fSFrançois Tigeot unpin_map:
1129a85cb24fSFrançois Tigeot 	i915_gem_object_unpin_map(ce->state->obj);
1130a85cb24fSFrançois Tigeot unpin_vma:
1131a85cb24fSFrançois Tigeot 	__i915_vma_unpin(ce->state);
1132a85cb24fSFrançois Tigeot err:
1133a85cb24fSFrançois Tigeot 	ce->pin_count = 0;
11343f2dd94aSFrançois Tigeot 	return ERR_PTR(ret);
1135a85cb24fSFrançois Tigeot }
1136a85cb24fSFrançois Tigeot 
execlists_context_unpin(struct intel_engine_cs * engine,struct i915_gem_context * ctx)1137a85cb24fSFrançois Tigeot static void execlists_context_unpin(struct intel_engine_cs *engine,
1138a85cb24fSFrançois Tigeot 				    struct i915_gem_context *ctx)
1139a85cb24fSFrançois Tigeot {
1140a85cb24fSFrançois Tigeot 	struct intel_context *ce = &ctx->engine[engine->id];
1141a85cb24fSFrançois Tigeot 
1142a85cb24fSFrançois Tigeot 	lockdep_assert_held(&ctx->i915->drm.struct_mutex);
1143a85cb24fSFrançois Tigeot 	GEM_BUG_ON(ce->pin_count == 0);
1144a85cb24fSFrançois Tigeot 
1145a85cb24fSFrançois Tigeot 	if (--ce->pin_count)
1146a85cb24fSFrançois Tigeot 		return;
1147a85cb24fSFrançois Tigeot 
1148a85cb24fSFrançois Tigeot 	intel_ring_unpin(ce->ring);
1149a85cb24fSFrançois Tigeot 
11503f2dd94aSFrançois Tigeot 	ce->state->obj->pin_global--;
1151a85cb24fSFrançois Tigeot 	i915_gem_object_unpin_map(ce->state->obj);
1152a85cb24fSFrançois Tigeot 	i915_vma_unpin(ce->state);
1153a85cb24fSFrançois Tigeot 
1154a85cb24fSFrançois Tigeot 	i915_gem_context_put(ctx);
1155a85cb24fSFrançois Tigeot }
1156a85cb24fSFrançois Tigeot 
execlists_request_alloc(struct drm_i915_gem_request * request)1157a85cb24fSFrançois Tigeot static int execlists_request_alloc(struct drm_i915_gem_request *request)
115819c468b4SFrançois Tigeot {
11591487f786SFrançois Tigeot 	struct intel_engine_cs *engine = request->engine;
11601487f786SFrançois Tigeot 	struct intel_context *ce = &request->ctx->engine[engine->id];
1161a85cb24fSFrançois Tigeot 	u32 *cs;
11621487f786SFrançois Tigeot 	int ret;
116319c468b4SFrançois Tigeot 
1164a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!ce->pin_count);
1165a85cb24fSFrançois Tigeot 
11661487f786SFrançois Tigeot 	/* Flush enough space to reduce the likelihood of waiting after
11671487f786SFrançois Tigeot 	 * we start building the request - in which case we will just
11681487f786SFrançois Tigeot 	 * have to repeat work.
11691487f786SFrançois Tigeot 	 */
11701487f786SFrançois Tigeot 	request->reserved_space += EXECLISTS_REQUEST_SIZE;
11711487f786SFrançois Tigeot 
1172a85cb24fSFrançois Tigeot 	cs = intel_ring_begin(request, 0);
11733f2dd94aSFrançois Tigeot 	if (IS_ERR(cs))
11743f2dd94aSFrançois Tigeot 		return PTR_ERR(cs);
11751487f786SFrançois Tigeot 
11761487f786SFrançois Tigeot 	if (!ce->initialised) {
11771487f786SFrançois Tigeot 		ret = engine->init_context(request);
11781487f786SFrançois Tigeot 		if (ret)
11793f2dd94aSFrançois Tigeot 			return ret;
11801487f786SFrançois Tigeot 
11811487f786SFrançois Tigeot 		ce->initialised = true;
11821487f786SFrançois Tigeot 	}
11831487f786SFrançois Tigeot 
11841487f786SFrançois Tigeot 	/* Note that after this point, we have committed to using
11851487f786SFrançois Tigeot 	 * this request as it is being used to both track the
11861487f786SFrançois Tigeot 	 * state of engine initialisation and liveness of the
11871487f786SFrançois Tigeot 	 * golden renderstate above. Think twice before you try
11881487f786SFrançois Tigeot 	 * to cancel/unwind this request now.
11891487f786SFrançois Tigeot 	 */
11901487f786SFrançois Tigeot 
11911487f786SFrançois Tigeot 	request->reserved_space -= EXECLISTS_REQUEST_SIZE;
11921487f786SFrançois Tigeot 	return 0;
1193352ff8bdSFrançois Tigeot }
1194352ff8bdSFrançois Tigeot 
1195a05eeebfSFrançois Tigeot /*
1196a05eeebfSFrançois Tigeot  * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
1197a05eeebfSFrançois Tigeot  * PIPE_CONTROL instruction. This is required for the flush to happen correctly
1198a05eeebfSFrançois Tigeot  * but there is a slight complication as this is applied in WA batch where the
1199a05eeebfSFrançois Tigeot  * values are only initialized once so we cannot take register value at the
1200a05eeebfSFrançois Tigeot  * beginning and reuse it further; hence we save its value to memory, upload a
1201a05eeebfSFrançois Tigeot  * constant value with bit21 set and then we restore it back with the saved value.
1202a05eeebfSFrançois Tigeot  * To simplify the WA, a constant value is formed by using the default value
1203a05eeebfSFrançois Tigeot  * of this register. This shouldn't be a problem because we are only modifying
1204a05eeebfSFrançois Tigeot  * it for a short period and this batch in non-premptible. We can ofcourse
1205a05eeebfSFrançois Tigeot  * use additional instructions that read the actual value of the register
1206a05eeebfSFrançois Tigeot  * at that time and set our bit of interest but it makes the WA complicated.
1207a05eeebfSFrançois Tigeot  *
1208a05eeebfSFrançois Tigeot  * This WA is also required for Gen9 so extracting as a function avoids
1209a05eeebfSFrançois Tigeot  * code duplication.
1210a05eeebfSFrançois Tigeot  */
1211a85cb24fSFrançois Tigeot static u32 *
gen8_emit_flush_coherentl3_wa(struct intel_engine_cs * engine,u32 * batch)1212a85cb24fSFrançois Tigeot gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine, u32 *batch)
1213a05eeebfSFrançois Tigeot {
1214a85cb24fSFrançois Tigeot 	*batch++ = MI_STORE_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1215a85cb24fSFrançois Tigeot 	*batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1216a85cb24fSFrançois Tigeot 	*batch++ = i915_ggtt_offset(engine->scratch) + 256;
1217a85cb24fSFrançois Tigeot 	*batch++ = 0;
1218a05eeebfSFrançois Tigeot 
1219a85cb24fSFrançois Tigeot 	*batch++ = MI_LOAD_REGISTER_IMM(1);
1220a85cb24fSFrançois Tigeot 	*batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1221a85cb24fSFrançois Tigeot 	*batch++ = 0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES;
1222a05eeebfSFrançois Tigeot 
1223a85cb24fSFrançois Tigeot 	batch = gen8_emit_pipe_control(batch,
1224a85cb24fSFrançois Tigeot 				       PIPE_CONTROL_CS_STALL |
1225a85cb24fSFrançois Tigeot 				       PIPE_CONTROL_DC_FLUSH_ENABLE,
1226a85cb24fSFrançois Tigeot 				       0);
1227a05eeebfSFrançois Tigeot 
1228a85cb24fSFrançois Tigeot 	*batch++ = MI_LOAD_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
1229a85cb24fSFrançois Tigeot 	*batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
1230a85cb24fSFrançois Tigeot 	*batch++ = i915_ggtt_offset(engine->scratch) + 256;
1231a85cb24fSFrançois Tigeot 	*batch++ = 0;
1232a05eeebfSFrançois Tigeot 
1233a85cb24fSFrançois Tigeot 	return batch;
1234a05eeebfSFrançois Tigeot }
1235a05eeebfSFrançois Tigeot 
123687df8fc6SFrançois Tigeot /*
1237a05eeebfSFrançois Tigeot  * Typically we only have one indirect_ctx and per_ctx batch buffer which are
1238a05eeebfSFrançois Tigeot  * initialized at the beginning and shared across all contexts but this field
1239a05eeebfSFrançois Tigeot  * helps us to have multiple batches at different offsets and select them based
1240a05eeebfSFrançois Tigeot  * on a criteria. At the moment this batch always start at the beginning of the page
1241a05eeebfSFrançois Tigeot  * and at this point we don't have multiple wa_ctx batch buffers.
1242a05eeebfSFrançois Tigeot  *
1243a05eeebfSFrançois Tigeot  * The number of WA applied are not known at the beginning; we use this field
1244a05eeebfSFrançois Tigeot  * to return the no of DWORDS written.
1245a05eeebfSFrançois Tigeot  *
1246a05eeebfSFrançois Tigeot  * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
1247a05eeebfSFrançois Tigeot  * so it adds NOOPs as padding to make it cacheline aligned.
1248a05eeebfSFrançois Tigeot  * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
1249a05eeebfSFrançois Tigeot  * makes a complete batch buffer.
1250a05eeebfSFrançois Tigeot  */
gen8_init_indirectctx_bb(struct intel_engine_cs * engine,u32 * batch)1251a85cb24fSFrançois Tigeot static u32 *gen8_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
1252a05eeebfSFrançois Tigeot {
1253a05eeebfSFrançois Tigeot 	/* WaDisableCtxRestoreArbitration:bdw,chv */
1254a85cb24fSFrançois Tigeot 	*batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
1255a05eeebfSFrançois Tigeot 
1256a05eeebfSFrançois Tigeot 	/* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
1257a85cb24fSFrançois Tigeot 	if (IS_BROADWELL(engine->i915))
1258a85cb24fSFrançois Tigeot 		batch = gen8_emit_flush_coherentl3_wa(engine, batch);
1259a05eeebfSFrançois Tigeot 
1260a05eeebfSFrançois Tigeot 	/* WaClearSlmSpaceAtContextSwitch:bdw,chv */
1261a05eeebfSFrançois Tigeot 	/* Actual scratch location is at 128 bytes offset */
1262a85cb24fSFrançois Tigeot 	batch = gen8_emit_pipe_control(batch,
1263a85cb24fSFrançois Tigeot 				       PIPE_CONTROL_FLUSH_L3 |
1264a05eeebfSFrançois Tigeot 				       PIPE_CONTROL_GLOBAL_GTT_IVB |
1265a05eeebfSFrançois Tigeot 				       PIPE_CONTROL_CS_STALL |
1266a85cb24fSFrançois Tigeot 				       PIPE_CONTROL_QW_WRITE,
1267a85cb24fSFrançois Tigeot 				       i915_ggtt_offset(engine->scratch) +
1268a85cb24fSFrançois Tigeot 				       2 * CACHELINE_BYTES);
1269a05eeebfSFrançois Tigeot 
12703f2dd94aSFrançois Tigeot 	*batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
12713f2dd94aSFrançois Tigeot 
1272a05eeebfSFrançois Tigeot 	/* Pad to end of cacheline */
1273a85cb24fSFrançois Tigeot 	while ((unsigned long)batch % CACHELINE_BYTES)
1274a85cb24fSFrançois Tigeot 		*batch++ = MI_NOOP;
1275a05eeebfSFrançois Tigeot 
1276a05eeebfSFrançois Tigeot 	/*
1277a05eeebfSFrançois Tigeot 	 * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
1278a05eeebfSFrançois Tigeot 	 * execution depends on the length specified in terms of cache lines
1279a05eeebfSFrançois Tigeot 	 * in the register CTX_RCS_INDIRECT_CTX
1280a05eeebfSFrançois Tigeot 	 */
1281a05eeebfSFrançois Tigeot 
1282a85cb24fSFrançois Tigeot 	return batch;
1283a05eeebfSFrançois Tigeot }
1284a05eeebfSFrançois Tigeot 
gen9_init_indirectctx_bb(struct intel_engine_cs * engine,u32 * batch)1285a85cb24fSFrançois Tigeot static u32 *gen9_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
1286a05eeebfSFrançois Tigeot {
12873f2dd94aSFrançois Tigeot 	*batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
12883f2dd94aSFrançois Tigeot 
1289a85cb24fSFrançois Tigeot 	/* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt,glk */
1290a85cb24fSFrançois Tigeot 	batch = gen8_emit_flush_coherentl3_wa(engine, batch);
1291a05eeebfSFrançois Tigeot 
1292a85cb24fSFrançois Tigeot 	/* WaDisableGatherAtSetShaderCommonSlice:skl,bxt,kbl,glk */
1293a85cb24fSFrançois Tigeot 	*batch++ = MI_LOAD_REGISTER_IMM(1);
1294a85cb24fSFrançois Tigeot 	*batch++ = i915_mmio_reg_offset(COMMON_SLICE_CHICKEN2);
1295a85cb24fSFrançois Tigeot 	*batch++ = _MASKED_BIT_DISABLE(
1296a85cb24fSFrançois Tigeot 			GEN9_DISABLE_GATHER_AT_SET_SHADER_COMMON_SLICE);
1297a85cb24fSFrançois Tigeot 	*batch++ = MI_NOOP;
129887df8fc6SFrançois Tigeot 
12998621f407SFrançois Tigeot 	/* WaClearSlmSpaceAtContextSwitch:kbl */
13008621f407SFrançois Tigeot 	/* Actual scratch location is at 128 bytes offset */
1301a85cb24fSFrançois Tigeot 	if (IS_KBL_REVID(engine->i915, 0, KBL_REVID_A0)) {
1302a85cb24fSFrançois Tigeot 		batch = gen8_emit_pipe_control(batch,
1303a85cb24fSFrançois Tigeot 					       PIPE_CONTROL_FLUSH_L3 |
13048621f407SFrançois Tigeot 					       PIPE_CONTROL_GLOBAL_GTT_IVB |
13058621f407SFrançois Tigeot 					       PIPE_CONTROL_CS_STALL |
1306a85cb24fSFrançois Tigeot 					       PIPE_CONTROL_QW_WRITE,
1307a85cb24fSFrançois Tigeot 					       i915_ggtt_offset(engine->scratch)
1308a85cb24fSFrançois Tigeot 					       + 2 * CACHELINE_BYTES);
13098621f407SFrançois Tigeot 	}
1310303bf270SFrançois Tigeot 
1311a85cb24fSFrançois Tigeot 	/* WaMediaPoolStateCmdInWABB:bxt,glk */
1312303bf270SFrançois Tigeot 	if (HAS_POOLED_EU(engine->i915)) {
1313303bf270SFrançois Tigeot 		/*
1314303bf270SFrançois Tigeot 		 * EU pool configuration is setup along with golden context
1315303bf270SFrançois Tigeot 		 * during context initialization. This value depends on
1316303bf270SFrançois Tigeot 		 * device type (2x6 or 3x6) and needs to be updated based
1317303bf270SFrançois Tigeot 		 * on which subslice is disabled especially for 2x6
1318303bf270SFrançois Tigeot 		 * devices, however it is safe to load default
1319303bf270SFrançois Tigeot 		 * configuration of 3x6 device instead of masking off
1320303bf270SFrançois Tigeot 		 * corresponding bits because HW ignores bits of a disabled
1321303bf270SFrançois Tigeot 		 * subslice and drops down to appropriate config. Please
1322303bf270SFrançois Tigeot 		 * see render_state_setup() in i915_gem_render_state.c for
1323303bf270SFrançois Tigeot 		 * possible configurations, to avoid duplication they are
1324303bf270SFrançois Tigeot 		 * not shown here again.
1325303bf270SFrançois Tigeot 		 */
1326a85cb24fSFrançois Tigeot 		*batch++ = GEN9_MEDIA_POOL_STATE;
1327a85cb24fSFrançois Tigeot 		*batch++ = GEN9_MEDIA_POOL_ENABLE;
1328a85cb24fSFrançois Tigeot 		*batch++ = 0x00777000;
1329a85cb24fSFrançois Tigeot 		*batch++ = 0;
1330a85cb24fSFrançois Tigeot 		*batch++ = 0;
1331a85cb24fSFrançois Tigeot 		*batch++ = 0;
1332303bf270SFrançois Tigeot 	}
1333303bf270SFrançois Tigeot 
13343f2dd94aSFrançois Tigeot 	*batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
13353f2dd94aSFrançois Tigeot 
1336a05eeebfSFrançois Tigeot 	/* Pad to end of cacheline */
1337a85cb24fSFrançois Tigeot 	while ((unsigned long)batch % CACHELINE_BYTES)
1338a85cb24fSFrançois Tigeot 		*batch++ = MI_NOOP;
1339a05eeebfSFrançois Tigeot 
1340a85cb24fSFrançois Tigeot 	return batch;
1341a05eeebfSFrançois Tigeot }
1342a05eeebfSFrançois Tigeot 
1343a85cb24fSFrançois Tigeot #define CTX_WA_BB_OBJ_SIZE (PAGE_SIZE)
13448621f407SFrançois Tigeot 
lrc_setup_wa_ctx(struct intel_engine_cs * engine)1345a85cb24fSFrançois Tigeot static int lrc_setup_wa_ctx(struct intel_engine_cs *engine)
1346a05eeebfSFrançois Tigeot {
13471e12ee3bSFrançois Tigeot 	struct drm_i915_gem_object *obj;
13481e12ee3bSFrançois Tigeot 	struct i915_vma *vma;
13491e12ee3bSFrançois Tigeot 	int err;
1350a05eeebfSFrançois Tigeot 
1351a85cb24fSFrançois Tigeot 	obj = i915_gem_object_create(engine->i915, CTX_WA_BB_OBJ_SIZE);
13521e12ee3bSFrançois Tigeot 	if (IS_ERR(obj))
13531e12ee3bSFrançois Tigeot 		return PTR_ERR(obj);
13541e12ee3bSFrançois Tigeot 
1355a85cb24fSFrançois Tigeot 	vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
13561e12ee3bSFrançois Tigeot 	if (IS_ERR(vma)) {
13571e12ee3bSFrançois Tigeot 		err = PTR_ERR(vma);
13581e12ee3bSFrançois Tigeot 		goto err;
1359a05eeebfSFrançois Tigeot 	}
1360a05eeebfSFrançois Tigeot 
13611e12ee3bSFrançois Tigeot 	err = i915_vma_pin(vma, 0, PAGE_SIZE, PIN_GLOBAL | PIN_HIGH);
13621e12ee3bSFrançois Tigeot 	if (err)
13631e12ee3bSFrançois Tigeot 		goto err;
1364a05eeebfSFrançois Tigeot 
13651e12ee3bSFrançois Tigeot 	engine->wa_ctx.vma = vma;
1366a05eeebfSFrançois Tigeot 	return 0;
13671e12ee3bSFrançois Tigeot 
13681e12ee3bSFrançois Tigeot err:
13691e12ee3bSFrançois Tigeot 	i915_gem_object_put(obj);
13701e12ee3bSFrançois Tigeot 	return err;
1371a05eeebfSFrançois Tigeot }
1372a05eeebfSFrançois Tigeot 
lrc_destroy_wa_ctx(struct intel_engine_cs * engine)1373a85cb24fSFrançois Tigeot static void lrc_destroy_wa_ctx(struct intel_engine_cs *engine)
1374a05eeebfSFrançois Tigeot {
13751e12ee3bSFrançois Tigeot 	i915_vma_unpin_and_release(&engine->wa_ctx.vma);
1376a05eeebfSFrançois Tigeot }
1377a05eeebfSFrançois Tigeot 
1378a85cb24fSFrançois Tigeot typedef u32 *(*wa_bb_func_t)(struct intel_engine_cs *engine, u32 *batch);
1379a85cb24fSFrançois Tigeot 
intel_init_workaround_bb(struct intel_engine_cs * engine)13808621f407SFrançois Tigeot static int intel_init_workaround_bb(struct intel_engine_cs *engine)
1381a05eeebfSFrançois Tigeot {
13821e12ee3bSFrançois Tigeot 	struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
1383a85cb24fSFrançois Tigeot 	struct i915_wa_ctx_bb *wa_bb[2] = { &wa_ctx->indirect_ctx,
1384a85cb24fSFrançois Tigeot 					    &wa_ctx->per_ctx };
1385a85cb24fSFrançois Tigeot 	wa_bb_func_t wa_bb_fn[2];
1386f0bba3d1SFrançois Tigeot 	struct page *page;
1387a85cb24fSFrançois Tigeot 	void *batch, *batch_ptr;
1388a85cb24fSFrançois Tigeot 	unsigned int i;
13891e12ee3bSFrançois Tigeot 	int ret;
1390a05eeebfSFrançois Tigeot 
1391a85cb24fSFrançois Tigeot 	if (WARN_ON(engine->id != RCS || !engine->scratch))
1392a85cb24fSFrançois Tigeot 		return -EINVAL;
1393a05eeebfSFrançois Tigeot 
1394a85cb24fSFrançois Tigeot 	switch (INTEL_GEN(engine->i915)) {
13953f2dd94aSFrançois Tigeot 	case 10:
13963f2dd94aSFrançois Tigeot 		return 0;
1397a85cb24fSFrançois Tigeot 	case 9:
1398a85cb24fSFrançois Tigeot 		wa_bb_fn[0] = gen9_init_indirectctx_bb;
13993f2dd94aSFrançois Tigeot 		wa_bb_fn[1] = NULL;
1400a85cb24fSFrançois Tigeot 		break;
1401a85cb24fSFrançois Tigeot 	case 8:
1402a85cb24fSFrançois Tigeot 		wa_bb_fn[0] = gen8_init_indirectctx_bb;
14033f2dd94aSFrançois Tigeot 		wa_bb_fn[1] = NULL;
1404a85cb24fSFrançois Tigeot 		break;
1405a85cb24fSFrançois Tigeot 	default:
1406a85cb24fSFrançois Tigeot 		MISSING_CASE(INTEL_GEN(engine->i915));
1407a05eeebfSFrançois Tigeot 		return 0;
1408a05eeebfSFrançois Tigeot 	}
1409a05eeebfSFrançois Tigeot 
1410a85cb24fSFrançois Tigeot 	ret = lrc_setup_wa_ctx(engine);
1411a05eeebfSFrançois Tigeot 	if (ret) {
1412a05eeebfSFrançois Tigeot 		DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
1413a05eeebfSFrançois Tigeot 		return ret;
1414a05eeebfSFrançois Tigeot 	}
1415a05eeebfSFrançois Tigeot 
14161e12ee3bSFrançois Tigeot 	page = i915_gem_object_get_dirty_page(wa_ctx->vma->obj, 0);
1417a85cb24fSFrançois Tigeot 	batch = batch_ptr = kmap_atomic(page);
1418a05eeebfSFrançois Tigeot 
1419a85cb24fSFrançois Tigeot 	/*
1420a85cb24fSFrançois Tigeot 	 * Emit the two workaround batch buffers, recording the offset from the
1421a85cb24fSFrançois Tigeot 	 * start of the workaround batch buffer object for each and their
1422a85cb24fSFrançois Tigeot 	 * respective sizes.
1423a85cb24fSFrançois Tigeot 	 */
1424a85cb24fSFrançois Tigeot 	for (i = 0; i < ARRAY_SIZE(wa_bb_fn); i++) {
1425a85cb24fSFrançois Tigeot 		wa_bb[i]->offset = batch_ptr - batch;
1426a85cb24fSFrançois Tigeot 		if (WARN_ON(!IS_ALIGNED(wa_bb[i]->offset, CACHELINE_BYTES))) {
1427a85cb24fSFrançois Tigeot 			ret = -EINVAL;
1428a85cb24fSFrançois Tigeot 			break;
1429a85cb24fSFrançois Tigeot 		}
14303f2dd94aSFrançois Tigeot 		if (wa_bb_fn[i])
1431a85cb24fSFrançois Tigeot 			batch_ptr = wa_bb_fn[i](engine, batch_ptr);
1432a85cb24fSFrançois Tigeot 		wa_bb[i]->size = batch_ptr - (batch + wa_bb[i]->offset);
1433a05eeebfSFrançois Tigeot 	}
1434a05eeebfSFrançois Tigeot 
1435a85cb24fSFrançois Tigeot 	BUG_ON(batch_ptr - batch > CTX_WA_BB_OBJ_SIZE);
1436a85cb24fSFrançois Tigeot 
1437a05eeebfSFrançois Tigeot 	kunmap_atomic(batch);
1438a05eeebfSFrançois Tigeot 	if (ret)
1439a85cb24fSFrançois Tigeot 		lrc_destroy_wa_ctx(engine);
1440a05eeebfSFrançois Tigeot 
1441a05eeebfSFrançois Tigeot 	return ret;
1442a05eeebfSFrançois Tigeot }
1443a05eeebfSFrançois Tigeot 
14443f2dd94aSFrançois Tigeot static u8 gtiir[] = {
14453f2dd94aSFrançois Tigeot 	[RCS] = 0,
14463f2dd94aSFrançois Tigeot 	[BCS] = 0,
14473f2dd94aSFrançois Tigeot 	[VCS] = 1,
14483f2dd94aSFrançois Tigeot 	[VCS2] = 1,
14493f2dd94aSFrançois Tigeot 	[VECS] = 3,
14503f2dd94aSFrançois Tigeot };
14518621f407SFrançois Tigeot 
gen8_init_common_ring(struct intel_engine_cs * engine)14528621f407SFrançois Tigeot static int gen8_init_common_ring(struct intel_engine_cs *engine)
14538621f407SFrançois Tigeot {
14541487f786SFrançois Tigeot 	struct drm_i915_private *dev_priv = engine->i915;
14553f2dd94aSFrançois Tigeot 	struct intel_engine_execlists * const execlists = &engine->execlists;
14561e12ee3bSFrançois Tigeot 	int ret;
14571e12ee3bSFrançois Tigeot 
14581e12ee3bSFrançois Tigeot 	ret = intel_mocs_init_engine(engine);
14591e12ee3bSFrançois Tigeot 	if (ret)
14601e12ee3bSFrançois Tigeot 		return ret;
14611b13d190SFrançois Tigeot 
14621e12ee3bSFrançois Tigeot 	intel_engine_reset_breadcrumbs(engine);
1463a85cb24fSFrançois Tigeot 	intel_engine_init_hangcheck(engine);
14641e12ee3bSFrançois Tigeot 
14658621f407SFrançois Tigeot 	I915_WRITE(RING_HWSTAM(engine->mmio_base), 0xffffffff);
14668621f407SFrançois Tigeot 	I915_WRITE(RING_MODE_GEN7(engine),
14671b13d190SFrançois Tigeot 		   _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
1468a85cb24fSFrançois Tigeot 	I915_WRITE(RING_HWS_PGA(engine->mmio_base),
1469a85cb24fSFrançois Tigeot 		   engine->status_page.ggtt_offset);
1470a85cb24fSFrançois Tigeot 	POSTING_READ(RING_HWS_PGA(engine->mmio_base));
1471a05eeebfSFrançois Tigeot 
14728621f407SFrançois Tigeot 	DRM_DEBUG_DRIVER("Execlists enabled for %s\n", engine->name);
14731b13d190SFrançois Tigeot 
14743f2dd94aSFrançois Tigeot 	GEM_BUG_ON(engine->id >= ARRAY_SIZE(gtiir));
14753f2dd94aSFrançois Tigeot 
14763f2dd94aSFrançois Tigeot 	/*
14773f2dd94aSFrançois Tigeot 	 * Clear any pending interrupt state.
14783f2dd94aSFrançois Tigeot 	 *
14793f2dd94aSFrançois Tigeot 	 * We do it twice out of paranoia that some of the IIR are double
14803f2dd94aSFrançois Tigeot 	 * buffered, and if we only reset it once there may still be
14813f2dd94aSFrançois Tigeot 	 * an interrupt pending.
14823f2dd94aSFrançois Tigeot 	 */
14833f2dd94aSFrançois Tigeot 	I915_WRITE(GEN8_GT_IIR(gtiir[engine->id]),
14843f2dd94aSFrançois Tigeot 		   GT_CONTEXT_SWITCH_INTERRUPT << engine->irq_shift);
14853f2dd94aSFrançois Tigeot 	I915_WRITE(GEN8_GT_IIR(gtiir[engine->id]),
14863f2dd94aSFrançois Tigeot 		   GT_CONTEXT_SWITCH_INTERRUPT << engine->irq_shift);
1487a85cb24fSFrançois Tigeot 	clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
14883f2dd94aSFrançois Tigeot 	execlists->csb_head = -1;
14893f2dd94aSFrançois Tigeot 	execlists->active = 0;
14903f2dd94aSFrançois Tigeot 
14913f2dd94aSFrançois Tigeot 	/* After a GPU reset, we may have requests to replay */
14923f2dd94aSFrançois Tigeot 	if (!i915_modparams.enable_guc_submission && execlists->first)
14933f2dd94aSFrançois Tigeot 		tasklet_schedule(&execlists->irq_tasklet);
14941e12ee3bSFrançois Tigeot 
14951e12ee3bSFrançois Tigeot 	return 0;
14961b13d190SFrançois Tigeot }
14971b13d190SFrançois Tigeot 
gen8_init_render_ring(struct intel_engine_cs * engine)14988621f407SFrançois Tigeot static int gen8_init_render_ring(struct intel_engine_cs *engine)
14991b13d190SFrançois Tigeot {
15001487f786SFrançois Tigeot 	struct drm_i915_private *dev_priv = engine->i915;
15011b13d190SFrançois Tigeot 	int ret;
15021b13d190SFrançois Tigeot 
15038621f407SFrançois Tigeot 	ret = gen8_init_common_ring(engine);
15041b13d190SFrançois Tigeot 	if (ret)
15051b13d190SFrançois Tigeot 		return ret;
15061b13d190SFrançois Tigeot 
15071b13d190SFrançois Tigeot 	/* We need to disable the AsyncFlip performance optimisations in order
15081b13d190SFrançois Tigeot 	 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
15091b13d190SFrançois Tigeot 	 * programmed to '1' on all products.
15101b13d190SFrançois Tigeot 	 *
15111b13d190SFrançois Tigeot 	 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
15121b13d190SFrançois Tigeot 	 */
15131b13d190SFrançois Tigeot 	I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
15141b13d190SFrançois Tigeot 
15151b13d190SFrançois Tigeot 	I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
15161b13d190SFrançois Tigeot 
15178621f407SFrançois Tigeot 	return init_workarounds_ring(engine);
15181b13d190SFrançois Tigeot }
15191b13d190SFrançois Tigeot 
gen9_init_render_ring(struct intel_engine_cs * engine)15208621f407SFrançois Tigeot static int gen9_init_render_ring(struct intel_engine_cs *engine)
1521477eb7f9SFrançois Tigeot {
1522477eb7f9SFrançois Tigeot 	int ret;
1523477eb7f9SFrançois Tigeot 
15248621f407SFrançois Tigeot 	ret = gen8_init_common_ring(engine);
1525477eb7f9SFrançois Tigeot 	if (ret)
1526477eb7f9SFrançois Tigeot 		return ret;
1527477eb7f9SFrançois Tigeot 
15288621f407SFrançois Tigeot 	return init_workarounds_ring(engine);
1529477eb7f9SFrançois Tigeot }
1530477eb7f9SFrançois Tigeot 
reset_common_ring(struct intel_engine_cs * engine,struct drm_i915_gem_request * request)15311e12ee3bSFrançois Tigeot static void reset_common_ring(struct intel_engine_cs *engine,
15321e12ee3bSFrançois Tigeot 			      struct drm_i915_gem_request *request)
15331e12ee3bSFrançois Tigeot {
15343f2dd94aSFrançois Tigeot 	struct intel_engine_execlists * const execlists = &engine->execlists;
1535a85cb24fSFrançois Tigeot 	struct intel_context *ce;
15363f2dd94aSFrançois Tigeot 	unsigned long flags;
15373f2dd94aSFrançois Tigeot 
15383f2dd94aSFrançois Tigeot 	spin_lock_irqsave(&engine->timeline->lock, flags);
15393f2dd94aSFrançois Tigeot 
15403f2dd94aSFrançois Tigeot 	/*
15413f2dd94aSFrançois Tigeot 	 * Catch up with any missed context-switch interrupts.
15423f2dd94aSFrançois Tigeot 	 *
15433f2dd94aSFrançois Tigeot 	 * Ideally we would just read the remaining CSB entries now that we
15443f2dd94aSFrançois Tigeot 	 * know the gpu is idle. However, the CSB registers are sometimes^W
15453f2dd94aSFrançois Tigeot 	 * often trashed across a GPU reset! Instead we have to rely on
15463f2dd94aSFrançois Tigeot 	 * guessing the missed context-switch events by looking at what
15473f2dd94aSFrançois Tigeot 	 * requests were completed.
15483f2dd94aSFrançois Tigeot 	 */
15493f2dd94aSFrançois Tigeot 	execlist_cancel_port_requests(execlists);
15503f2dd94aSFrançois Tigeot 
15513f2dd94aSFrançois Tigeot 	/* Push back any incomplete requests for replay after the reset. */
15523f2dd94aSFrançois Tigeot 	unwind_incomplete_requests(engine);
15533f2dd94aSFrançois Tigeot 
15543f2dd94aSFrançois Tigeot 	spin_unlock_irqrestore(&engine->timeline->lock, flags);
1555a85cb24fSFrançois Tigeot 
1556a85cb24fSFrançois Tigeot 	/* If the request was innocent, we leave the request in the ELSP
1557a85cb24fSFrançois Tigeot 	 * and will try to replay it on restarting. The context image may
1558a85cb24fSFrançois Tigeot 	 * have been corrupted by the reset, in which case we may have
1559a85cb24fSFrançois Tigeot 	 * to service a new GPU hang, but more likely we can continue on
1560a85cb24fSFrançois Tigeot 	 * without impact.
1561a85cb24fSFrançois Tigeot 	 *
1562a85cb24fSFrançois Tigeot 	 * If the request was guilty, we presume the context is corrupt
1563a85cb24fSFrançois Tigeot 	 * and have to at least restore the RING register in the context
1564a85cb24fSFrançois Tigeot 	 * image back to the expected values to skip over the guilty request.
1565a85cb24fSFrançois Tigeot 	 */
1566a85cb24fSFrançois Tigeot 	if (!request || request->fence.error != -EIO)
1567a85cb24fSFrançois Tigeot 		return;
15681e12ee3bSFrançois Tigeot 
15691e12ee3bSFrançois Tigeot 	/* We want a simple context + ring to execute the breadcrumb update.
15701e12ee3bSFrançois Tigeot 	 * We cannot rely on the context being intact across the GPU hang,
15711e12ee3bSFrançois Tigeot 	 * so clear it and rebuild just what we need for the breadcrumb.
15721e12ee3bSFrançois Tigeot 	 * All pending requests for this context will be zapped, and any
15731e12ee3bSFrançois Tigeot 	 * future request will be after userspace has had the opportunity
15741e12ee3bSFrançois Tigeot 	 * to recreate its own state.
15751e12ee3bSFrançois Tigeot 	 */
1576a85cb24fSFrançois Tigeot 	ce = &request->ctx->engine[engine->id];
15771e12ee3bSFrançois Tigeot 	execlists_init_reg_state(ce->lrc_reg_state,
15781e12ee3bSFrançois Tigeot 				 request->ctx, engine, ce->ring);
15791e12ee3bSFrançois Tigeot 
15801e12ee3bSFrançois Tigeot 	/* Move the RING_HEAD onto the breadcrumb, past the hanging batch */
15811e12ee3bSFrançois Tigeot 	ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
15821e12ee3bSFrançois Tigeot 		i915_ggtt_offset(ce->ring->vma);
15831e12ee3bSFrançois Tigeot 	ce->lrc_reg_state[CTX_RING_HEAD+1] = request->postfix;
15841e12ee3bSFrançois Tigeot 
15851e12ee3bSFrançois Tigeot 	request->ring->head = request->postfix;
15861e12ee3bSFrançois Tigeot 	intel_ring_update_space(request->ring);
15871e12ee3bSFrançois Tigeot 
15881e12ee3bSFrançois Tigeot 	/* Reset WaIdleLiteRestore:bdw,skl as well */
15893f2dd94aSFrançois Tigeot 	unwind_wa_tail(request);
15901e12ee3bSFrançois Tigeot }
15911e12ee3bSFrançois Tigeot 
intel_logical_ring_emit_pdps(struct drm_i915_gem_request * req)1592a05eeebfSFrançois Tigeot static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request *req)
1593a05eeebfSFrançois Tigeot {
1594a05eeebfSFrançois Tigeot 	struct i915_hw_ppgtt *ppgtt = req->ctx->ppgtt;
15958621f407SFrançois Tigeot 	struct intel_engine_cs *engine = req->engine;
1596a85cb24fSFrançois Tigeot 	const int num_lri_cmds = GEN8_3LVL_PDPES * 2;
1597a85cb24fSFrançois Tigeot 	u32 *cs;
1598a85cb24fSFrançois Tigeot 	int i;
1599a05eeebfSFrançois Tigeot 
1600a85cb24fSFrançois Tigeot 	cs = intel_ring_begin(req, num_lri_cmds * 2 + 2);
1601a85cb24fSFrançois Tigeot 	if (IS_ERR(cs))
1602a85cb24fSFrançois Tigeot 		return PTR_ERR(cs);
1603a05eeebfSFrançois Tigeot 
1604a85cb24fSFrançois Tigeot 	*cs++ = MI_LOAD_REGISTER_IMM(num_lri_cmds);
1605a85cb24fSFrançois Tigeot 	for (i = GEN8_3LVL_PDPES - 1; i >= 0; i--) {
1606a05eeebfSFrançois Tigeot 		const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
1607a05eeebfSFrançois Tigeot 
1608a85cb24fSFrançois Tigeot 		*cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, i));
1609a85cb24fSFrançois Tigeot 		*cs++ = upper_32_bits(pd_daddr);
1610a85cb24fSFrançois Tigeot 		*cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, i));
1611a85cb24fSFrançois Tigeot 		*cs++ = lower_32_bits(pd_daddr);
1612a05eeebfSFrançois Tigeot 	}
1613a05eeebfSFrançois Tigeot 
1614a85cb24fSFrançois Tigeot 	*cs++ = MI_NOOP;
1615a85cb24fSFrançois Tigeot 	intel_ring_advance(req, cs);
1616a05eeebfSFrançois Tigeot 
1617a05eeebfSFrançois Tigeot 	return 0;
1618a05eeebfSFrançois Tigeot }
1619a05eeebfSFrançois Tigeot 
gen8_emit_bb_start(struct drm_i915_gem_request * req,u64 offset,u32 len,const unsigned int flags)1620a05eeebfSFrançois Tigeot static int gen8_emit_bb_start(struct drm_i915_gem_request *req,
162171f41f3eSFrançois Tigeot 			      u64 offset, u32 len,
1622a85cb24fSFrançois Tigeot 			      const unsigned int flags)
16231b13d190SFrançois Tigeot {
1624a85cb24fSFrançois Tigeot 	u32 *cs;
16251b13d190SFrançois Tigeot 	int ret;
16261b13d190SFrançois Tigeot 
1627a05eeebfSFrançois Tigeot 	/* Don't rely in hw updating PDPs, specially in lite-restore.
1628a05eeebfSFrançois Tigeot 	 * Ideally, we should set Force PD Restore in ctx descriptor,
1629a05eeebfSFrançois Tigeot 	 * but we can't. Force Restore would be a second option, but
1630a05eeebfSFrançois Tigeot 	 * it is unsafe in case of lite-restore (because the ctx is
1631352ff8bdSFrançois Tigeot 	 * not idle). PML4 is allocated during ppgtt init so this is
1632352ff8bdSFrançois Tigeot 	 * not needed in 48-bit.*/
1633a05eeebfSFrançois Tigeot 	if (req->ctx->ppgtt &&
1634a85cb24fSFrançois Tigeot 	    (intel_engine_flag(req->engine) & req->ctx->ppgtt->pd_dirty_rings) &&
1635a85cb24fSFrançois Tigeot 	    !i915_vm_is_48bit(&req->ctx->ppgtt->base) &&
16361487f786SFrançois Tigeot 	    !intel_vgpu_active(req->i915)) {
1637a05eeebfSFrançois Tigeot 		ret = intel_logical_ring_emit_pdps(req);
1638a05eeebfSFrançois Tigeot 		if (ret)
1639a05eeebfSFrançois Tigeot 			return ret;
1640a05eeebfSFrançois Tigeot 
16418621f407SFrançois Tigeot 		req->ctx->ppgtt->pd_dirty_rings &= ~intel_engine_flag(req->engine);
1642a05eeebfSFrançois Tigeot 	}
1643a05eeebfSFrançois Tigeot 
1644a85cb24fSFrançois Tigeot 	cs = intel_ring_begin(req, 4);
1645a85cb24fSFrançois Tigeot 	if (IS_ERR(cs))
1646a85cb24fSFrançois Tigeot 		return PTR_ERR(cs);
16471b13d190SFrançois Tigeot 
16483f2dd94aSFrançois Tigeot 	/*
16493f2dd94aSFrançois Tigeot 	 * WaDisableCtxRestoreArbitration:bdw,chv
16503f2dd94aSFrançois Tigeot 	 *
16513f2dd94aSFrançois Tigeot 	 * We don't need to perform MI_ARB_ENABLE as often as we do (in
16523f2dd94aSFrançois Tigeot 	 * particular all the gen that do not need the w/a at all!), if we
16533f2dd94aSFrançois Tigeot 	 * took care to make sure that on every switch into this context
16543f2dd94aSFrançois Tigeot 	 * (both ordinary and for preemption) that arbitrartion was enabled
16553f2dd94aSFrançois Tigeot 	 * we would be fine. However, there doesn't seem to be a downside to
16563f2dd94aSFrançois Tigeot 	 * being paranoid and making sure it is set before each batch and
16573f2dd94aSFrançois Tigeot 	 * every context-switch.
16583f2dd94aSFrançois Tigeot 	 *
16593f2dd94aSFrançois Tigeot 	 * Note that if we fail to enable arbitration before the request
16603f2dd94aSFrançois Tigeot 	 * is complete, then we do not see the context-switch interrupt and
16613f2dd94aSFrançois Tigeot 	 * the engine hangs (with RING_HEAD == RING_TAIL).
16623f2dd94aSFrançois Tigeot 	 *
16633f2dd94aSFrançois Tigeot 	 * That satisfies both the GPGPU w/a and our heavy-handed paranoia.
16643f2dd94aSFrançois Tigeot 	 */
16653f2dd94aSFrançois Tigeot 	*cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
16663f2dd94aSFrançois Tigeot 
16671b13d190SFrançois Tigeot 	/* FIXME(BDW): Address space and security selectors. */
1668a85cb24fSFrançois Tigeot 	*cs++ = MI_BATCH_BUFFER_START_GEN8 |
1669a85cb24fSFrançois Tigeot 		(flags & I915_DISPATCH_SECURE ? 0 : BIT(8)) |
1670a85cb24fSFrançois Tigeot 		(flags & I915_DISPATCH_RS ? MI_BATCH_RESOURCE_STREAMER : 0);
1671a85cb24fSFrançois Tigeot 	*cs++ = lower_32_bits(offset);
1672a85cb24fSFrançois Tigeot 	*cs++ = upper_32_bits(offset);
1673a85cb24fSFrançois Tigeot 	intel_ring_advance(req, cs);
16741b13d190SFrançois Tigeot 
16751b13d190SFrançois Tigeot 	return 0;
16761b13d190SFrançois Tigeot }
16771b13d190SFrançois Tigeot 
gen8_logical_ring_enable_irq(struct intel_engine_cs * engine)1678303bf270SFrançois Tigeot static void gen8_logical_ring_enable_irq(struct intel_engine_cs *engine)
16791b13d190SFrançois Tigeot {
16801487f786SFrançois Tigeot 	struct drm_i915_private *dev_priv = engine->i915;
16818621f407SFrançois Tigeot 	I915_WRITE_IMR(engine,
16828621f407SFrançois Tigeot 		       ~(engine->irq_enable_mask | engine->irq_keep_mask));
1683303bf270SFrançois Tigeot 	POSTING_READ_FW(RING_IMR(engine->mmio_base));
16841b13d190SFrançois Tigeot }
16851b13d190SFrançois Tigeot 
gen8_logical_ring_disable_irq(struct intel_engine_cs * engine)1686303bf270SFrançois Tigeot static void gen8_logical_ring_disable_irq(struct intel_engine_cs *engine)
16871b13d190SFrançois Tigeot {
16881487f786SFrançois Tigeot 	struct drm_i915_private *dev_priv = engine->i915;
16898621f407SFrançois Tigeot 	I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
16901b13d190SFrançois Tigeot }
16911b13d190SFrançois Tigeot 
gen8_emit_flush(struct drm_i915_gem_request * request,u32 mode)169271f41f3eSFrançois Tigeot static int gen8_emit_flush(struct drm_i915_gem_request *request, u32 mode)
16931b13d190SFrançois Tigeot {
1694a85cb24fSFrançois Tigeot 	u32 cmd, *cs;
16951b13d190SFrançois Tigeot 
1696a85cb24fSFrançois Tigeot 	cs = intel_ring_begin(request, 4);
1697a85cb24fSFrançois Tigeot 	if (IS_ERR(cs))
1698a85cb24fSFrançois Tigeot 		return PTR_ERR(cs);
16991b13d190SFrançois Tigeot 
17001b13d190SFrançois Tigeot 	cmd = MI_FLUSH_DW + 1;
17011b13d190SFrançois Tigeot 
17022c9916cdSFrançois Tigeot 	/* We always require a command barrier so that subsequent
17032c9916cdSFrançois Tigeot 	 * commands, such as breadcrumb interrupts, are strictly ordered
17042c9916cdSFrançois Tigeot 	 * wrt the contents of the write cache being flushed to memory
17052c9916cdSFrançois Tigeot 	 * (and thus being coherent from the CPU).
17062c9916cdSFrançois Tigeot 	 */
17072c9916cdSFrançois Tigeot 	cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
17082c9916cdSFrançois Tigeot 
170971f41f3eSFrançois Tigeot 	if (mode & EMIT_INVALIDATE) {
17102c9916cdSFrançois Tigeot 		cmd |= MI_INVALIDATE_TLB;
171171f41f3eSFrançois Tigeot 		if (request->engine->id == VCS)
17122c9916cdSFrançois Tigeot 			cmd |= MI_INVALIDATE_BSD;
17131b13d190SFrançois Tigeot 	}
17141b13d190SFrançois Tigeot 
1715a85cb24fSFrançois Tigeot 	*cs++ = cmd;
1716a85cb24fSFrançois Tigeot 	*cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
1717a85cb24fSFrançois Tigeot 	*cs++ = 0; /* upper addr */
1718a85cb24fSFrançois Tigeot 	*cs++ = 0; /* value */
1719a85cb24fSFrançois Tigeot 	intel_ring_advance(request, cs);
17201b13d190SFrançois Tigeot 
17211b13d190SFrançois Tigeot 	return 0;
17221b13d190SFrançois Tigeot }
17231b13d190SFrançois Tigeot 
gen8_emit_flush_render(struct drm_i915_gem_request * request,u32 mode)1724a05eeebfSFrançois Tigeot static int gen8_emit_flush_render(struct drm_i915_gem_request *request,
172571f41f3eSFrançois Tigeot 				  u32 mode)
17261b13d190SFrançois Tigeot {
172771f41f3eSFrançois Tigeot 	struct intel_engine_cs *engine = request->engine;
17281e12ee3bSFrançois Tigeot 	u32 scratch_addr =
17291e12ee3bSFrançois Tigeot 		i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES;
17308621f407SFrançois Tigeot 	bool vf_flush_wa = false, dc_flush_wa = false;
1731a85cb24fSFrançois Tigeot 	u32 *cs, flags = 0;
17328621f407SFrançois Tigeot 	int len;
17331b13d190SFrançois Tigeot 
17341b13d190SFrançois Tigeot 	flags |= PIPE_CONTROL_CS_STALL;
17351b13d190SFrançois Tigeot 
173671f41f3eSFrançois Tigeot 	if (mode & EMIT_FLUSH) {
17371b13d190SFrançois Tigeot 		flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
17381b13d190SFrançois Tigeot 		flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
1739aee94f86SFrançois Tigeot 		flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
1740b49c8cf9SFrançois Tigeot 		flags |= PIPE_CONTROL_FLUSH_ENABLE;
17411b13d190SFrançois Tigeot 	}
17421b13d190SFrançois Tigeot 
174371f41f3eSFrançois Tigeot 	if (mode & EMIT_INVALIDATE) {
17441b13d190SFrançois Tigeot 		flags |= PIPE_CONTROL_TLB_INVALIDATE;
17451b13d190SFrançois Tigeot 		flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
17461b13d190SFrançois Tigeot 		flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
17471b13d190SFrançois Tigeot 		flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
17481b13d190SFrançois Tigeot 		flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
17491b13d190SFrançois Tigeot 		flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
17501b13d190SFrançois Tigeot 		flags |= PIPE_CONTROL_QW_WRITE;
17511b13d190SFrançois Tigeot 		flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
17521b13d190SFrançois Tigeot 
175319c468b4SFrançois Tigeot 		/*
1754c0e85e96SFrançois Tigeot 		 * On GEN9: before VF_CACHE_INVALIDATE we need to emit a NULL
1755c0e85e96SFrançois Tigeot 		 * pipe control.
175619c468b4SFrançois Tigeot 		 */
17571487f786SFrançois Tigeot 		if (IS_GEN9(request->i915))
1758c0e85e96SFrançois Tigeot 			vf_flush_wa = true;
17598621f407SFrançois Tigeot 
17608621f407SFrançois Tigeot 		/* WaForGAMHang:kbl */
17618621f407SFrançois Tigeot 		if (IS_KBL_REVID(request->i915, 0, KBL_REVID_B0))
17628621f407SFrançois Tigeot 			dc_flush_wa = true;
1763c0e85e96SFrançois Tigeot 	}
176419c468b4SFrançois Tigeot 
17658621f407SFrançois Tigeot 	len = 6;
17668621f407SFrançois Tigeot 
17678621f407SFrançois Tigeot 	if (vf_flush_wa)
17688621f407SFrançois Tigeot 		len += 6;
17698621f407SFrançois Tigeot 
17708621f407SFrançois Tigeot 	if (dc_flush_wa)
17718621f407SFrançois Tigeot 		len += 12;
17728621f407SFrançois Tigeot 
1773a85cb24fSFrançois Tigeot 	cs = intel_ring_begin(request, len);
1774a85cb24fSFrançois Tigeot 	if (IS_ERR(cs))
1775a85cb24fSFrançois Tigeot 		return PTR_ERR(cs);
17761b13d190SFrançois Tigeot 
1777a85cb24fSFrançois Tigeot 	if (vf_flush_wa)
1778a85cb24fSFrançois Tigeot 		cs = gen8_emit_pipe_control(cs, 0, 0);
177919c468b4SFrançois Tigeot 
1780a85cb24fSFrançois Tigeot 	if (dc_flush_wa)
1781a85cb24fSFrançois Tigeot 		cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_DC_FLUSH_ENABLE,
1782a85cb24fSFrançois Tigeot 					    0);
17838621f407SFrançois Tigeot 
1784a85cb24fSFrançois Tigeot 	cs = gen8_emit_pipe_control(cs, flags, scratch_addr);
17858621f407SFrançois Tigeot 
1786a85cb24fSFrançois Tigeot 	if (dc_flush_wa)
1787a85cb24fSFrançois Tigeot 		cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_CS_STALL, 0);
17888621f407SFrançois Tigeot 
1789a85cb24fSFrançois Tigeot 	intel_ring_advance(request, cs);
17901b13d190SFrançois Tigeot 
17911b13d190SFrançois Tigeot 	return 0;
17921b13d190SFrançois Tigeot }
17931b13d190SFrançois Tigeot 
1794477eb7f9SFrançois Tigeot /*
1795477eb7f9SFrançois Tigeot  * Reserve space for 2 NOOPs at the end of each request to be
1796477eb7f9SFrançois Tigeot  * used as a workaround for not being allowed to do lite
1797477eb7f9SFrançois Tigeot  * restore with HEAD==TAIL (WaIdleLiteRestore).
1798477eb7f9SFrançois Tigeot  */
gen8_emit_wa_tail(struct drm_i915_gem_request * request,u32 * cs)1799a85cb24fSFrançois Tigeot static void gen8_emit_wa_tail(struct drm_i915_gem_request *request, u32 *cs)
1800c0e85e96SFrançois Tigeot {
18013f2dd94aSFrançois Tigeot 	/* Ensure there's always at least one preemption point per-request. */
18023f2dd94aSFrançois Tigeot 	*cs++ = MI_ARB_CHECK;
1803a85cb24fSFrançois Tigeot 	*cs++ = MI_NOOP;
1804a85cb24fSFrançois Tigeot 	request->wa_tail = intel_ring_offset(request, cs);
18054be47400SFrançois Tigeot }
1806c0e85e96SFrançois Tigeot 
gen8_emit_breadcrumb(struct drm_i915_gem_request * request,u32 * cs)1807a85cb24fSFrançois Tigeot static void gen8_emit_breadcrumb(struct drm_i915_gem_request *request, u32 *cs)
18084be47400SFrançois Tigeot {
1809c0e85e96SFrançois Tigeot 	/* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */
1810c0e85e96SFrançois Tigeot 	BUILD_BUG_ON(I915_GEM_HWS_INDEX_ADDR & (1 << 5));
18111b13d190SFrançois Tigeot 
1812a85cb24fSFrançois Tigeot 	*cs++ = (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW;
1813a85cb24fSFrançois Tigeot 	*cs++ = intel_hws_seqno_address(request->engine) | MI_FLUSH_DW_USE_GTT;
1814a85cb24fSFrançois Tigeot 	*cs++ = 0;
1815a85cb24fSFrançois Tigeot 	*cs++ = request->global_seqno;
1816a85cb24fSFrançois Tigeot 	*cs++ = MI_USER_INTERRUPT;
1817a85cb24fSFrançois Tigeot 	*cs++ = MI_NOOP;
1818a85cb24fSFrançois Tigeot 	request->tail = intel_ring_offset(request, cs);
1819a85cb24fSFrançois Tigeot 	assert_ring_tail_valid(request->ring, request->tail);
18204be47400SFrançois Tigeot 
1821a85cb24fSFrançois Tigeot 	gen8_emit_wa_tail(request, cs);
1822c0e85e96SFrançois Tigeot }
18234be47400SFrançois Tigeot static const int gen8_emit_breadcrumb_sz = 6 + WA_TAIL_DWORDS;
18244be47400SFrançois Tigeot 
gen8_emit_breadcrumb_render(struct drm_i915_gem_request * request,u32 * cs)18254be47400SFrançois Tigeot static void gen8_emit_breadcrumb_render(struct drm_i915_gem_request *request,
1826a85cb24fSFrançois Tigeot 					u32 *cs)
1827c0e85e96SFrançois Tigeot {
1828c0e85e96SFrançois Tigeot 	/* We're using qword write, seqno should be aligned to 8 bytes. */
1829c0e85e96SFrançois Tigeot 	BUILD_BUG_ON(I915_GEM_HWS_INDEX & 1);
1830c0e85e96SFrançois Tigeot 
1831c0e85e96SFrançois Tigeot 	/* w/a for post sync ops following a GPGPU operation we
1832c0e85e96SFrançois Tigeot 	 * need a prior CS_STALL, which is emitted by the flush
1833c0e85e96SFrançois Tigeot 	 * following the batch.
1834477eb7f9SFrançois Tigeot 	 */
1835a85cb24fSFrançois Tigeot 	*cs++ = GFX_OP_PIPE_CONTROL(6);
1836a85cb24fSFrançois Tigeot 	*cs++ = PIPE_CONTROL_GLOBAL_GTT_IVB | PIPE_CONTROL_CS_STALL |
1837a85cb24fSFrançois Tigeot 		PIPE_CONTROL_QW_WRITE;
1838a85cb24fSFrançois Tigeot 	*cs++ = intel_hws_seqno_address(request->engine);
1839a85cb24fSFrançois Tigeot 	*cs++ = 0;
1840a85cb24fSFrançois Tigeot 	*cs++ = request->global_seqno;
1841c0e85e96SFrançois Tigeot 	/* We're thrashing one dword of HWS. */
1842a85cb24fSFrançois Tigeot 	*cs++ = 0;
1843a85cb24fSFrançois Tigeot 	*cs++ = MI_USER_INTERRUPT;
1844a85cb24fSFrançois Tigeot 	*cs++ = MI_NOOP;
1845a85cb24fSFrançois Tigeot 	request->tail = intel_ring_offset(request, cs);
1846a85cb24fSFrançois Tigeot 	assert_ring_tail_valid(request->ring, request->tail);
18474be47400SFrançois Tigeot 
1848a85cb24fSFrançois Tigeot 	gen8_emit_wa_tail(request, cs);
1849477eb7f9SFrançois Tigeot }
18504be47400SFrançois Tigeot static const int gen8_emit_breadcrumb_render_sz = 8 + WA_TAIL_DWORDS;
18514be47400SFrançois Tigeot 
gen8_init_rcs_context(struct drm_i915_gem_request * req)1852a05eeebfSFrançois Tigeot static int gen8_init_rcs_context(struct drm_i915_gem_request *req)
18532c9916cdSFrançois Tigeot {
18542c9916cdSFrançois Tigeot 	int ret;
18552c9916cdSFrançois Tigeot 
1856a85cb24fSFrançois Tigeot 	ret = intel_ring_workarounds_emit(req);
18572c9916cdSFrançois Tigeot 	if (ret)
18582c9916cdSFrançois Tigeot 		return ret;
18592c9916cdSFrançois Tigeot 
1860a05eeebfSFrançois Tigeot 	ret = intel_rcs_context_init_mocs(req);
1861a05eeebfSFrançois Tigeot 	/*
1862a05eeebfSFrançois Tigeot 	 * Failing to program the MOCS is non-fatal.The system will not
1863a05eeebfSFrançois Tigeot 	 * run at peak performance. So generate an error and carry on.
1864a05eeebfSFrançois Tigeot 	 */
1865a05eeebfSFrançois Tigeot 	if (ret)
1866a05eeebfSFrançois Tigeot 		DRM_ERROR("MOCS failed to program: expect performance issues.\n");
1867a05eeebfSFrançois Tigeot 
18684be47400SFrançois Tigeot 	return i915_gem_render_state_emit(req);
18692c9916cdSFrançois Tigeot }
18702c9916cdSFrançois Tigeot 
18711b13d190SFrançois Tigeot /**
18721b13d190SFrançois Tigeot  * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
18731487f786SFrançois Tigeot  * @engine: Engine Command Streamer.
18741b13d190SFrançois Tigeot  */
intel_logical_ring_cleanup(struct intel_engine_cs * engine)18758621f407SFrançois Tigeot void intel_logical_ring_cleanup(struct intel_engine_cs *engine)
18761b13d190SFrançois Tigeot {
18772c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv;
18781b13d190SFrançois Tigeot 
18798621f407SFrançois Tigeot 	/*
18808621f407SFrançois Tigeot 	 * Tasklet cannot be active at this point due intel_mark_active/idle
18818621f407SFrançois Tigeot 	 * so this is just for documentation.
18828621f407SFrançois Tigeot 	 */
18833f2dd94aSFrançois Tigeot 	if (WARN_ON(test_bit(TASKLET_STATE_SCHED, &engine->execlists.irq_tasklet.state)))
18843f2dd94aSFrançois Tigeot 		tasklet_kill(&engine->execlists.irq_tasklet);
18852c9916cdSFrançois Tigeot 
18861487f786SFrançois Tigeot 	dev_priv = engine->i915;
18878621f407SFrançois Tigeot 
18888621f407SFrançois Tigeot 	if (engine->buffer) {
18898621f407SFrançois Tigeot 		WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
1890aee94f86SFrançois Tigeot 	}
18911b13d190SFrançois Tigeot 
18928621f407SFrançois Tigeot 	if (engine->cleanup)
18938621f407SFrançois Tigeot 		engine->cleanup(engine);
18941b13d190SFrançois Tigeot 
1895a85cb24fSFrançois Tigeot 	intel_engine_cleanup_common(engine);
1896a85cb24fSFrançois Tigeot 
1897a85cb24fSFrançois Tigeot 	lrc_destroy_wa_ctx(engine);
18981487f786SFrançois Tigeot 	engine->i915 = NULL;
18991e12ee3bSFrançois Tigeot 	dev_priv->engine[engine->id] = NULL;
19001e12ee3bSFrançois Tigeot 	kfree(engine);
19011b13d190SFrançois Tigeot }
19021b13d190SFrançois Tigeot 
execlists_set_default_submission(struct intel_engine_cs * engine)1903a85cb24fSFrançois Tigeot static void execlists_set_default_submission(struct intel_engine_cs *engine)
190471f41f3eSFrançois Tigeot {
190571f41f3eSFrançois Tigeot 	engine->submit_request = execlists_submit_request;
19063f2dd94aSFrançois Tigeot 	engine->cancel_requests = execlists_cancel_requests;
19074be47400SFrançois Tigeot 	engine->schedule = execlists_schedule;
19083f2dd94aSFrançois Tigeot 	engine->execlists.irq_tasklet.func = intel_lrc_irq_handler;
190971f41f3eSFrançois Tigeot }
191071f41f3eSFrançois Tigeot 
1911c0e85e96SFrançois Tigeot static void
logical_ring_default_vfuncs(struct intel_engine_cs * engine)19121487f786SFrançois Tigeot logical_ring_default_vfuncs(struct intel_engine_cs *engine)
19131b13d190SFrançois Tigeot {
1914c0e85e96SFrançois Tigeot 	/* Default vfuncs which can be overriden by each engine. */
19158621f407SFrançois Tigeot 	engine->init_hw = gen8_init_common_ring;
19161e12ee3bSFrançois Tigeot 	engine->reset_hw = reset_common_ring;
1917a85cb24fSFrançois Tigeot 
1918a85cb24fSFrançois Tigeot 	engine->context_pin = execlists_context_pin;
1919a85cb24fSFrançois Tigeot 	engine->context_unpin = execlists_context_unpin;
1920a85cb24fSFrançois Tigeot 
1921a85cb24fSFrançois Tigeot 	engine->request_alloc = execlists_request_alloc;
1922a85cb24fSFrançois Tigeot 
19238621f407SFrançois Tigeot 	engine->emit_flush = gen8_emit_flush;
19244be47400SFrançois Tigeot 	engine->emit_breadcrumb = gen8_emit_breadcrumb;
19254be47400SFrançois Tigeot 	engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_sz;
1926a85cb24fSFrançois Tigeot 
1927a85cb24fSFrançois Tigeot 	engine->set_default_submission = execlists_set_default_submission;
192871f41f3eSFrançois Tigeot 
1929303bf270SFrançois Tigeot 	engine->irq_enable = gen8_logical_ring_enable_irq;
1930303bf270SFrançois Tigeot 	engine->irq_disable = gen8_logical_ring_disable_irq;
19318621f407SFrançois Tigeot 	engine->emit_bb_start = gen8_emit_bb_start;
1932c0e85e96SFrançois Tigeot }
1933c0e85e96SFrançois Tigeot 
1934c0e85e96SFrançois Tigeot static inline void
logical_ring_default_irqs(struct intel_engine_cs * engine)193587df8fc6SFrançois Tigeot logical_ring_default_irqs(struct intel_engine_cs *engine)
1936c0e85e96SFrançois Tigeot {
193787df8fc6SFrançois Tigeot 	unsigned shift = engine->irq_shift;
19388621f407SFrançois Tigeot 	engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << shift;
19398621f407SFrançois Tigeot 	engine->irq_keep_mask = GT_CONTEXT_SWITCH_INTERRUPT << shift;
1940c0e85e96SFrançois Tigeot }
1941c0e85e96SFrançois Tigeot 
194287df8fc6SFrançois Tigeot static void
logical_ring_setup(struct intel_engine_cs * engine)194387df8fc6SFrançois Tigeot logical_ring_setup(struct intel_engine_cs *engine)
194487df8fc6SFrançois Tigeot {
194587df8fc6SFrançois Tigeot 	struct drm_i915_private *dev_priv = engine->i915;
194687df8fc6SFrançois Tigeot 	enum forcewake_domains fw_domains;
194787df8fc6SFrançois Tigeot 
194887df8fc6SFrançois Tigeot 	intel_engine_setup_common(engine);
194987df8fc6SFrançois Tigeot 
195087df8fc6SFrançois Tigeot 	/* Intentionally left blank. */
195187df8fc6SFrançois Tigeot 	engine->buffer = NULL;
195287df8fc6SFrançois Tigeot 
195387df8fc6SFrançois Tigeot 	fw_domains = intel_uncore_forcewake_for_reg(dev_priv,
195487df8fc6SFrançois Tigeot 						    RING_ELSP(engine),
195587df8fc6SFrançois Tigeot 						    FW_REG_WRITE);
195687df8fc6SFrançois Tigeot 
195787df8fc6SFrançois Tigeot 	fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
195887df8fc6SFrançois Tigeot 						     RING_CONTEXT_STATUS_PTR(engine),
195987df8fc6SFrançois Tigeot 						     FW_REG_READ | FW_REG_WRITE);
196087df8fc6SFrançois Tigeot 
196187df8fc6SFrançois Tigeot 	fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
196287df8fc6SFrançois Tigeot 						     RING_CONTEXT_STATUS_BUF_BASE(engine),
196387df8fc6SFrançois Tigeot 						     FW_REG_READ);
196487df8fc6SFrançois Tigeot 
19653f2dd94aSFrançois Tigeot 	engine->execlists.fw_domains = fw_domains;
196687df8fc6SFrançois Tigeot 
19673f2dd94aSFrançois Tigeot 	tasklet_init(&engine->execlists.irq_tasklet,
196887df8fc6SFrançois Tigeot 		     intel_lrc_irq_handler, (unsigned long)engine);
196987df8fc6SFrançois Tigeot 
197087df8fc6SFrançois Tigeot 	logical_ring_default_vfuncs(engine);
197187df8fc6SFrançois Tigeot 	logical_ring_default_irqs(engine);
197287df8fc6SFrançois Tigeot }
197387df8fc6SFrançois Tigeot 
logical_ring_init(struct intel_engine_cs * engine)19743f2dd94aSFrançois Tigeot static int logical_ring_init(struct intel_engine_cs *engine)
19758621f407SFrançois Tigeot {
19761b13d190SFrançois Tigeot 	int ret;
19771b13d190SFrançois Tigeot 
197887df8fc6SFrançois Tigeot 	ret = intel_engine_init_common(engine);
19791b13d190SFrançois Tigeot 	if (ret)
1980aee94f86SFrançois Tigeot 		goto error;
19811b13d190SFrançois Tigeot 
1982aee94f86SFrançois Tigeot 	return 0;
1983aee94f86SFrançois Tigeot 
1984aee94f86SFrançois Tigeot error:
19858621f407SFrançois Tigeot 	intel_logical_ring_cleanup(engine);
19861b13d190SFrançois Tigeot 	return ret;
19871b13d190SFrançois Tigeot }
19881b13d190SFrançois Tigeot 
logical_render_ring_init(struct intel_engine_cs * engine)198987df8fc6SFrançois Tigeot int logical_render_ring_init(struct intel_engine_cs *engine)
19901b13d190SFrançois Tigeot {
19911487f786SFrançois Tigeot 	struct drm_i915_private *dev_priv = engine->i915;
19922c9916cdSFrançois Tigeot 	int ret;
19931b13d190SFrançois Tigeot 
199487df8fc6SFrançois Tigeot 	logical_ring_setup(engine);
199587df8fc6SFrançois Tigeot 
19961487f786SFrançois Tigeot 	if (HAS_L3_DPF(dev_priv))
19978621f407SFrançois Tigeot 		engine->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
19981b13d190SFrançois Tigeot 
1999c0e85e96SFrançois Tigeot 	/* Override some for render ring. */
20001487f786SFrançois Tigeot 	if (INTEL_GEN(dev_priv) >= 9)
20018621f407SFrançois Tigeot 		engine->init_hw = gen9_init_render_ring;
2002477eb7f9SFrançois Tigeot 	else
20038621f407SFrançois Tigeot 		engine->init_hw = gen8_init_render_ring;
20048621f407SFrançois Tigeot 	engine->init_context = gen8_init_rcs_context;
20058621f407SFrançois Tigeot 	engine->emit_flush = gen8_emit_flush_render;
20064be47400SFrançois Tigeot 	engine->emit_breadcrumb = gen8_emit_breadcrumb_render;
20074be47400SFrançois Tigeot 	engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_render_sz;
20081b13d190SFrançois Tigeot 
2009a85cb24fSFrançois Tigeot 	ret = intel_engine_create_scratch(engine, PAGE_SIZE);
20102c9916cdSFrançois Tigeot 	if (ret)
20112c9916cdSFrançois Tigeot 		return ret;
20122c9916cdSFrançois Tigeot 
20138621f407SFrançois Tigeot 	ret = intel_init_workaround_bb(engine);
2014a05eeebfSFrançois Tigeot 	if (ret) {
2015a05eeebfSFrançois Tigeot 		/*
2016a05eeebfSFrançois Tigeot 		 * We continue even if we fail to initialize WA batch
2017a05eeebfSFrançois Tigeot 		 * because we only expect rare glitches but nothing
2018a05eeebfSFrançois Tigeot 		 * critical to prevent us from using GPU
2019a05eeebfSFrançois Tigeot 		 */
2020a05eeebfSFrançois Tigeot 		DRM_ERROR("WA batch buffer initialization failed: %d\n",
2021a05eeebfSFrançois Tigeot 			  ret);
2022a05eeebfSFrançois Tigeot 	}
2023a05eeebfSFrançois Tigeot 
20244be47400SFrançois Tigeot 	return logical_ring_init(engine);
20251b13d190SFrançois Tigeot }
20261b13d190SFrançois Tigeot 
logical_xcs_ring_init(struct intel_engine_cs * engine)202787df8fc6SFrançois Tigeot int logical_xcs_ring_init(struct intel_engine_cs *engine)
20281b13d190SFrançois Tigeot {
202987df8fc6SFrançois Tigeot 	logical_ring_setup(engine);
20301b13d190SFrançois Tigeot 
203187df8fc6SFrançois Tigeot 	return logical_ring_init(engine);
20321b13d190SFrançois Tigeot }
20331b13d190SFrançois Tigeot 
2034477eb7f9SFrançois Tigeot static u32
make_rpcs(struct drm_i915_private * dev_priv)20351487f786SFrançois Tigeot make_rpcs(struct drm_i915_private *dev_priv)
20361b13d190SFrançois Tigeot {
2037477eb7f9SFrançois Tigeot 	u32 rpcs = 0;
20381b13d190SFrançois Tigeot 
2039477eb7f9SFrançois Tigeot 	/*
2040477eb7f9SFrançois Tigeot 	 * No explicit RPCS request is needed to ensure full
2041477eb7f9SFrançois Tigeot 	 * slice/subslice/EU enablement prior to Gen9.
2042477eb7f9SFrançois Tigeot 	*/
20431487f786SFrançois Tigeot 	if (INTEL_GEN(dev_priv) < 9)
20441b13d190SFrançois Tigeot 		return 0;
20451b13d190SFrançois Tigeot 
2046477eb7f9SFrançois Tigeot 	/*
2047477eb7f9SFrançois Tigeot 	 * Starting in Gen9, render power gating can leave
2048477eb7f9SFrançois Tigeot 	 * slice/subslice/EU in a partially enabled state. We
2049477eb7f9SFrançois Tigeot 	 * must make an explicit request through RPCS for full
2050477eb7f9SFrançois Tigeot 	 * enablement.
2051477eb7f9SFrançois Tigeot 	*/
20521e12ee3bSFrançois Tigeot 	if (INTEL_INFO(dev_priv)->sseu.has_slice_pg) {
2053477eb7f9SFrançois Tigeot 		rpcs |= GEN8_RPCS_S_CNT_ENABLE;
20541e12ee3bSFrançois Tigeot 		rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.slice_mask) <<
2055477eb7f9SFrançois Tigeot 			GEN8_RPCS_S_CNT_SHIFT;
2056477eb7f9SFrançois Tigeot 		rpcs |= GEN8_RPCS_ENABLE;
2057477eb7f9SFrançois Tigeot 	}
20581b13d190SFrançois Tigeot 
20591e12ee3bSFrançois Tigeot 	if (INTEL_INFO(dev_priv)->sseu.has_subslice_pg) {
2060477eb7f9SFrançois Tigeot 		rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
20611e12ee3bSFrançois Tigeot 		rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.subslice_mask) <<
2062477eb7f9SFrançois Tigeot 			GEN8_RPCS_SS_CNT_SHIFT;
2063477eb7f9SFrançois Tigeot 		rpcs |= GEN8_RPCS_ENABLE;
2064477eb7f9SFrançois Tigeot 	}
20651b13d190SFrançois Tigeot 
20661e12ee3bSFrançois Tigeot 	if (INTEL_INFO(dev_priv)->sseu.has_eu_pg) {
20671e12ee3bSFrançois Tigeot 		rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
2068477eb7f9SFrançois Tigeot 			GEN8_RPCS_EU_MIN_SHIFT;
20691e12ee3bSFrançois Tigeot 		rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
2070477eb7f9SFrançois Tigeot 			GEN8_RPCS_EU_MAX_SHIFT;
2071477eb7f9SFrançois Tigeot 		rpcs |= GEN8_RPCS_ENABLE;
2072477eb7f9SFrançois Tigeot 	}
2073477eb7f9SFrançois Tigeot 
2074477eb7f9SFrançois Tigeot 	return rpcs;
20751b13d190SFrançois Tigeot }
20761b13d190SFrançois Tigeot 
intel_lr_indirect_ctx_offset(struct intel_engine_cs * engine)20778621f407SFrançois Tigeot static u32 intel_lr_indirect_ctx_offset(struct intel_engine_cs *engine)
2078c0e85e96SFrançois Tigeot {
2079c0e85e96SFrançois Tigeot 	u32 indirect_ctx_offset;
2080c0e85e96SFrançois Tigeot 
20811487f786SFrançois Tigeot 	switch (INTEL_GEN(engine->i915)) {
2082c0e85e96SFrançois Tigeot 	default:
20831487f786SFrançois Tigeot 		MISSING_CASE(INTEL_GEN(engine->i915));
2084c0e85e96SFrançois Tigeot 		/* fall through */
20853f2dd94aSFrançois Tigeot 	case 10:
20863f2dd94aSFrançois Tigeot 		indirect_ctx_offset =
20873f2dd94aSFrançois Tigeot 			GEN10_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
20883f2dd94aSFrançois Tigeot 		break;
2089c0e85e96SFrançois Tigeot 	case 9:
2090c0e85e96SFrançois Tigeot 		indirect_ctx_offset =
2091c0e85e96SFrançois Tigeot 			GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2092c0e85e96SFrançois Tigeot 		break;
2093c0e85e96SFrançois Tigeot 	case 8:
2094c0e85e96SFrançois Tigeot 		indirect_ctx_offset =
2095c0e85e96SFrançois Tigeot 			GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
2096c0e85e96SFrançois Tigeot 		break;
2097c0e85e96SFrançois Tigeot 	}
2098c0e85e96SFrançois Tigeot 
2099c0e85e96SFrançois Tigeot 	return indirect_ctx_offset;
2100c0e85e96SFrançois Tigeot }
2101c0e85e96SFrançois Tigeot 
execlists_init_reg_state(u32 * regs,struct i915_gem_context * ctx,struct intel_engine_cs * engine,struct intel_ring * ring)2102a85cb24fSFrançois Tigeot static void execlists_init_reg_state(u32 *regs,
21031e12ee3bSFrançois Tigeot 				     struct i915_gem_context *ctx,
21048621f407SFrançois Tigeot 				     struct intel_engine_cs *engine,
210571f41f3eSFrançois Tigeot 				     struct intel_ring *ring)
21061b13d190SFrançois Tigeot {
21071e12ee3bSFrançois Tigeot 	struct drm_i915_private *dev_priv = engine->i915;
21081e12ee3bSFrançois Tigeot 	struct i915_hw_ppgtt *ppgtt = ctx->ppgtt ?: dev_priv->mm.aliasing_ppgtt;
2109a85cb24fSFrançois Tigeot 	u32 base = engine->mmio_base;
2110a85cb24fSFrançois Tigeot 	bool rcs = engine->id == RCS;
21111b13d190SFrançois Tigeot 
2112a85cb24fSFrançois Tigeot 	/* A context is actually a big batch buffer with several
2113a85cb24fSFrançois Tigeot 	 * MI_LOAD_REGISTER_IMM commands followed by (reg, value) pairs. The
2114a85cb24fSFrançois Tigeot 	 * values we are setting here are only for the first context restore:
2115a85cb24fSFrançois Tigeot 	 * on a subsequent save, the GPU will recreate this batchbuffer with new
2116a85cb24fSFrançois Tigeot 	 * values (including all the missing MI_LOAD_REGISTER_IMM commands that
2117a85cb24fSFrançois Tigeot 	 * we are not initializing here).
2118a85cb24fSFrançois Tigeot 	 */
2119a85cb24fSFrançois Tigeot 	regs[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(rcs ? 14 : 11) |
2120a85cb24fSFrançois Tigeot 				 MI_LRI_FORCE_POSTED;
2121a85cb24fSFrançois Tigeot 
2122a85cb24fSFrançois Tigeot 	CTX_REG(regs, CTX_CONTEXT_CONTROL, RING_CONTEXT_CONTROL(engine),
2123477eb7f9SFrançois Tigeot 		_MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
2124a05eeebfSFrançois Tigeot 				   CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
21251487f786SFrançois Tigeot 				   (HAS_RESOURCE_STREAMER(dev_priv) ?
2126c0e85e96SFrançois Tigeot 				   CTX_CTRL_RS_CTX_ENABLE : 0)));
2127a85cb24fSFrançois Tigeot 	CTX_REG(regs, CTX_RING_HEAD, RING_HEAD(base), 0);
2128a85cb24fSFrançois Tigeot 	CTX_REG(regs, CTX_RING_TAIL, RING_TAIL(base), 0);
2129a85cb24fSFrançois Tigeot 	CTX_REG(regs, CTX_RING_BUFFER_START, RING_START(base), 0);
2130a85cb24fSFrançois Tigeot 	CTX_REG(regs, CTX_RING_BUFFER_CONTROL, RING_CTL(base),
21311e12ee3bSFrançois Tigeot 		RING_CTL_SIZE(ring->size) | RING_VALID);
2132a85cb24fSFrançois Tigeot 	CTX_REG(regs, CTX_BB_HEAD_U, RING_BBADDR_UDW(base), 0);
2133a85cb24fSFrançois Tigeot 	CTX_REG(regs, CTX_BB_HEAD_L, RING_BBADDR(base), 0);
2134a85cb24fSFrançois Tigeot 	CTX_REG(regs, CTX_BB_STATE, RING_BBSTATE(base), RING_BB_PPGTT);
2135a85cb24fSFrançois Tigeot 	CTX_REG(regs, CTX_SECOND_BB_HEAD_U, RING_SBBADDR_UDW(base), 0);
2136a85cb24fSFrançois Tigeot 	CTX_REG(regs, CTX_SECOND_BB_HEAD_L, RING_SBBADDR(base), 0);
2137a85cb24fSFrançois Tigeot 	CTX_REG(regs, CTX_SECOND_BB_STATE, RING_SBBSTATE(base), 0);
2138a85cb24fSFrançois Tigeot 	if (rcs) {
21393f2dd94aSFrançois Tigeot 		struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
21403f2dd94aSFrançois Tigeot 
2141a85cb24fSFrançois Tigeot 		CTX_REG(regs, CTX_RCS_INDIRECT_CTX, RING_INDIRECT_CTX(base), 0);
2142a85cb24fSFrançois Tigeot 		CTX_REG(regs, CTX_RCS_INDIRECT_CTX_OFFSET,
2143a85cb24fSFrançois Tigeot 			RING_INDIRECT_CTX_OFFSET(base), 0);
21443f2dd94aSFrançois Tigeot 		if (wa_ctx->indirect_ctx.size) {
21451e12ee3bSFrançois Tigeot 			u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
2146a05eeebfSFrançois Tigeot 
2147a85cb24fSFrançois Tigeot 			regs[CTX_RCS_INDIRECT_CTX + 1] =
2148a85cb24fSFrançois Tigeot 				(ggtt_offset + wa_ctx->indirect_ctx.offset) |
2149a85cb24fSFrançois Tigeot 				(wa_ctx->indirect_ctx.size / CACHELINE_BYTES);
2150a05eeebfSFrançois Tigeot 
2151a85cb24fSFrançois Tigeot 			regs[CTX_RCS_INDIRECT_CTX_OFFSET + 1] =
21528621f407SFrançois Tigeot 				intel_lr_indirect_ctx_offset(engine) << 6;
21533f2dd94aSFrançois Tigeot 		}
21543f2dd94aSFrançois Tigeot 
21553f2dd94aSFrançois Tigeot 		CTX_REG(regs, CTX_BB_PER_CTX_PTR, RING_BB_PER_CTX_PTR(base), 0);
21563f2dd94aSFrançois Tigeot 		if (wa_ctx->per_ctx.size) {
21573f2dd94aSFrançois Tigeot 			u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
2158a05eeebfSFrançois Tigeot 
2159a85cb24fSFrançois Tigeot 			regs[CTX_BB_PER_CTX_PTR + 1] =
2160a85cb24fSFrançois Tigeot 				(ggtt_offset + wa_ctx->per_ctx.offset) | 0x01;
2161a05eeebfSFrançois Tigeot 		}
21621b13d190SFrançois Tigeot 	}
216319c468b4SFrançois Tigeot 
2164a85cb24fSFrançois Tigeot 	regs[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9) | MI_LRI_FORCE_POSTED;
2165a85cb24fSFrançois Tigeot 
2166a85cb24fSFrançois Tigeot 	CTX_REG(regs, CTX_CTX_TIMESTAMP, RING_CTX_TIMESTAMP(base), 0);
2167a85cb24fSFrançois Tigeot 	/* PDP values well be assigned later if needed */
2168a85cb24fSFrançois Tigeot 	CTX_REG(regs, CTX_PDP3_UDW, GEN8_RING_PDP_UDW(engine, 3), 0);
2169a85cb24fSFrançois Tigeot 	CTX_REG(regs, CTX_PDP3_LDW, GEN8_RING_PDP_LDW(engine, 3), 0);
2170a85cb24fSFrançois Tigeot 	CTX_REG(regs, CTX_PDP2_UDW, GEN8_RING_PDP_UDW(engine, 2), 0);
2171a85cb24fSFrançois Tigeot 	CTX_REG(regs, CTX_PDP2_LDW, GEN8_RING_PDP_LDW(engine, 2), 0);
2172a85cb24fSFrançois Tigeot 	CTX_REG(regs, CTX_PDP1_UDW, GEN8_RING_PDP_UDW(engine, 1), 0);
2173a85cb24fSFrançois Tigeot 	CTX_REG(regs, CTX_PDP1_LDW, GEN8_RING_PDP_LDW(engine, 1), 0);
2174a85cb24fSFrançois Tigeot 	CTX_REG(regs, CTX_PDP0_UDW, GEN8_RING_PDP_UDW(engine, 0), 0);
2175a85cb24fSFrançois Tigeot 	CTX_REG(regs, CTX_PDP0_LDW, GEN8_RING_PDP_LDW(engine, 0), 0);
2176a85cb24fSFrançois Tigeot 
2177a85cb24fSFrançois Tigeot 	if (ppgtt && i915_vm_is_48bit(&ppgtt->base)) {
2178352ff8bdSFrançois Tigeot 		/* 64b PPGTT (48bit canonical)
2179352ff8bdSFrançois Tigeot 		 * PDP0_DESCRIPTOR contains the base address to PML4 and
2180352ff8bdSFrançois Tigeot 		 * other PDP Descriptors are ignored.
2181352ff8bdSFrançois Tigeot 		 */
2182a85cb24fSFrançois Tigeot 		ASSIGN_CTX_PML4(ppgtt, regs);
2183352ff8bdSFrançois Tigeot 	}
2184352ff8bdSFrançois Tigeot 
2185a85cb24fSFrançois Tigeot 	if (rcs) {
2186a85cb24fSFrançois Tigeot 		regs[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
2187a85cb24fSFrançois Tigeot 		CTX_REG(regs, CTX_R_PWR_CLK_STATE, GEN8_R_PWR_CLK_STATE,
21881487f786SFrançois Tigeot 			make_rpcs(dev_priv));
21893f2dd94aSFrançois Tigeot 
21903f2dd94aSFrançois Tigeot 		i915_oa_init_reg_state(engine, ctx, regs);
21911b13d190SFrançois Tigeot 	}
21921e12ee3bSFrançois Tigeot }
21931e12ee3bSFrançois Tigeot 
21941e12ee3bSFrançois Tigeot static int
populate_lr_context(struct i915_gem_context * ctx,struct drm_i915_gem_object * ctx_obj,struct intel_engine_cs * engine,struct intel_ring * ring)21951e12ee3bSFrançois Tigeot populate_lr_context(struct i915_gem_context *ctx,
21961e12ee3bSFrançois Tigeot 		    struct drm_i915_gem_object *ctx_obj,
21971e12ee3bSFrançois Tigeot 		    struct intel_engine_cs *engine,
21981e12ee3bSFrançois Tigeot 		    struct intel_ring *ring)
21991e12ee3bSFrançois Tigeot {
22001e12ee3bSFrançois Tigeot 	void *vaddr;
22011e12ee3bSFrançois Tigeot 	int ret;
22021e12ee3bSFrançois Tigeot 
22031e12ee3bSFrançois Tigeot 	ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
22041e12ee3bSFrançois Tigeot 	if (ret) {
22051e12ee3bSFrançois Tigeot 		DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
22061e12ee3bSFrançois Tigeot 		return ret;
22071e12ee3bSFrançois Tigeot 	}
22081e12ee3bSFrançois Tigeot 
22091e12ee3bSFrançois Tigeot 	vaddr = i915_gem_object_pin_map(ctx_obj, I915_MAP_WB);
22101e12ee3bSFrançois Tigeot 	if (IS_ERR(vaddr)) {
22111e12ee3bSFrançois Tigeot 		ret = PTR_ERR(vaddr);
22121e12ee3bSFrançois Tigeot 		DRM_DEBUG_DRIVER("Could not map object pages! (%d)\n", ret);
22131e12ee3bSFrançois Tigeot 		return ret;
22141e12ee3bSFrançois Tigeot 	}
22154be47400SFrançois Tigeot 	ctx_obj->mm.dirty = true;
22161e12ee3bSFrançois Tigeot 
22171e12ee3bSFrançois Tigeot 	/* The second page of the context object contains some fields which must
22181e12ee3bSFrançois Tigeot 	 * be set up prior to the first execution. */
22191e12ee3bSFrançois Tigeot 
22201e12ee3bSFrançois Tigeot 	execlists_init_reg_state(vaddr + LRC_STATE_PN * PAGE_SIZE,
22211e12ee3bSFrançois Tigeot 				 ctx, engine, ring);
22221b13d190SFrançois Tigeot 
22238621f407SFrançois Tigeot 	i915_gem_object_unpin_map(ctx_obj);
22241b13d190SFrançois Tigeot 
22251b13d190SFrançois Tigeot 	return 0;
22261b13d190SFrançois Tigeot }
22271b13d190SFrançois Tigeot 
execlists_context_deferred_alloc(struct i915_gem_context * ctx,struct intel_engine_cs * engine)22281487f786SFrançois Tigeot static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
22298621f407SFrançois Tigeot 					    struct intel_engine_cs *engine)
22301b13d190SFrançois Tigeot {
22311b13d190SFrançois Tigeot 	struct drm_i915_gem_object *ctx_obj;
22321487f786SFrançois Tigeot 	struct intel_context *ce = &ctx->engine[engine->id];
22331e12ee3bSFrançois Tigeot 	struct i915_vma *vma;
22341b13d190SFrançois Tigeot 	uint32_t context_size;
223571f41f3eSFrançois Tigeot 	struct intel_ring *ring;
22361b13d190SFrançois Tigeot 	int ret;
22371b13d190SFrançois Tigeot 
22381487f786SFrançois Tigeot 	WARN_ON(ce->state);
22391b13d190SFrançois Tigeot 
22403f2dd94aSFrançois Tigeot 	context_size = round_up(engine->context_size, I915_GTT_PAGE_SIZE);
22411b13d190SFrançois Tigeot 
22423f2dd94aSFrançois Tigeot 	/*
22433f2dd94aSFrançois Tigeot 	 * Before the actual start of the context image, we insert a few pages
22443f2dd94aSFrançois Tigeot 	 * for our own use and for sharing with the GuC.
22453f2dd94aSFrançois Tigeot 	 */
22463f2dd94aSFrançois Tigeot 	context_size += LRC_HEADER_PAGES * PAGE_SIZE;
2247352ff8bdSFrançois Tigeot 
2248a85cb24fSFrançois Tigeot 	ctx_obj = i915_gem_object_create(ctx->i915, context_size);
22491487f786SFrançois Tigeot 	if (IS_ERR(ctx_obj)) {
225019c468b4SFrançois Tigeot 		DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
22511487f786SFrançois Tigeot 		return PTR_ERR(ctx_obj);
22521b13d190SFrançois Tigeot 	}
22531b13d190SFrançois Tigeot 
2254a85cb24fSFrançois Tigeot 	vma = i915_vma_instance(ctx_obj, &ctx->i915->ggtt.base, NULL);
22551e12ee3bSFrançois Tigeot 	if (IS_ERR(vma)) {
22561e12ee3bSFrançois Tigeot 		ret = PTR_ERR(vma);
22571e12ee3bSFrançois Tigeot 		goto error_deref_obj;
22581e12ee3bSFrançois Tigeot 	}
22591e12ee3bSFrançois Tigeot 
226071f41f3eSFrançois Tigeot 	ring = intel_engine_create_ring(engine, ctx->ring_size);
226171f41f3eSFrançois Tigeot 	if (IS_ERR(ring)) {
226271f41f3eSFrançois Tigeot 		ret = PTR_ERR(ring);
2263352ff8bdSFrançois Tigeot 		goto error_deref_obj;
22641b13d190SFrançois Tigeot 	}
22651b13d190SFrançois Tigeot 
226671f41f3eSFrançois Tigeot 	ret = populate_lr_context(ctx, ctx_obj, engine, ring);
22671b13d190SFrançois Tigeot 	if (ret) {
22681b13d190SFrançois Tigeot 		DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
226971f41f3eSFrançois Tigeot 		goto error_ring_free;
22701b13d190SFrançois Tigeot 	}
22711b13d190SFrançois Tigeot 
227271f41f3eSFrançois Tigeot 	ce->ring = ring;
22731e12ee3bSFrançois Tigeot 	ce->state = vma;
2274a85cb24fSFrançois Tigeot 	ce->initialised |= engine->init_context == NULL;
22751b13d190SFrançois Tigeot 
22761b13d190SFrançois Tigeot 	return 0;
22771b13d190SFrançois Tigeot 
227871f41f3eSFrançois Tigeot error_ring_free:
227971f41f3eSFrançois Tigeot 	intel_ring_free(ring);
2280352ff8bdSFrançois Tigeot error_deref_obj:
228187df8fc6SFrançois Tigeot 	i915_gem_object_put(ctx_obj);
22821b13d190SFrançois Tigeot 	return ret;
22831b13d190SFrançois Tigeot }
2284477eb7f9SFrançois Tigeot 
intel_lr_context_resume(struct drm_i915_private * dev_priv)22851e12ee3bSFrançois Tigeot void intel_lr_context_resume(struct drm_i915_private *dev_priv)
2286477eb7f9SFrançois Tigeot {
22878621f407SFrançois Tigeot 	struct intel_engine_cs *engine;
22881e12ee3bSFrançois Tigeot 	struct i915_gem_context *ctx;
22891e12ee3bSFrançois Tigeot 	enum intel_engine_id id;
2290477eb7f9SFrançois Tigeot 
22911e12ee3bSFrançois Tigeot 	/* Because we emit WA_TAIL_DWORDS there may be a disparity
22921e12ee3bSFrançois Tigeot 	 * between our bookkeeping in ce->ring->head and ce->ring->tail and
22931e12ee3bSFrançois Tigeot 	 * that stored in context. As we only write new commands from
22941e12ee3bSFrançois Tigeot 	 * ce->ring->tail onwards, everything before that is junk. If the GPU
22951e12ee3bSFrançois Tigeot 	 * starts reading from its RING_HEAD from the context, it may try to
22961e12ee3bSFrançois Tigeot 	 * execute that junk and die.
22971e12ee3bSFrançois Tigeot 	 *
22981e12ee3bSFrançois Tigeot 	 * So to avoid that we reset the context images upon resume. For
22991e12ee3bSFrançois Tigeot 	 * simplicity, we just zero everything out.
23001e12ee3bSFrançois Tigeot 	 */
23013f2dd94aSFrançois Tigeot 	list_for_each_entry(ctx, &dev_priv->contexts.list, link) {
23021e12ee3bSFrançois Tigeot 		for_each_engine(engine, dev_priv, id) {
23031487f786SFrançois Tigeot 			struct intel_context *ce = &ctx->engine[engine->id];
23041e12ee3bSFrançois Tigeot 			u32 *reg;
2305477eb7f9SFrançois Tigeot 
23061e12ee3bSFrançois Tigeot 			if (!ce->state)
2307477eb7f9SFrançois Tigeot 				continue;
2308477eb7f9SFrançois Tigeot 
23091e12ee3bSFrançois Tigeot 			reg = i915_gem_object_pin_map(ce->state->obj,
23101e12ee3bSFrançois Tigeot 						      I915_MAP_WB);
23111e12ee3bSFrançois Tigeot 			if (WARN_ON(IS_ERR(reg)))
2312477eb7f9SFrançois Tigeot 				continue;
23138621f407SFrançois Tigeot 
23141e12ee3bSFrançois Tigeot 			reg += LRC_STATE_PN * PAGE_SIZE / sizeof(*reg);
23151e12ee3bSFrançois Tigeot 			reg[CTX_RING_HEAD+1] = 0;
23161e12ee3bSFrançois Tigeot 			reg[CTX_RING_TAIL+1] = 0;
2317477eb7f9SFrançois Tigeot 
23184be47400SFrançois Tigeot 			ce->state->obj->mm.dirty = true;
23191e12ee3bSFrançois Tigeot 			i915_gem_object_unpin_map(ce->state->obj);
2320477eb7f9SFrançois Tigeot 
2321a85cb24fSFrançois Tigeot 			intel_ring_reset(ce->ring, 0);
23221e12ee3bSFrançois Tigeot 		}
2323477eb7f9SFrançois Tigeot 	}
2324477eb7f9SFrançois Tigeot }
2325