xref: /dflybsd-src/sys/dev/drm/i915/intel_lrc.c (revision 2c9916cd50d5c4c4defa089bebed8c8865efa896)
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  */
1341b13d190SFrançois Tigeot 
1351b13d190SFrançois Tigeot #include <drm/drmP.h>
1361b13d190SFrançois Tigeot #include <drm/i915_drm.h>
1371b13d190SFrançois Tigeot #include "i915_drv.h"
1381b13d190SFrançois Tigeot #include "intel_drv.h"
1391b13d190SFrançois Tigeot 
140*2c9916cdSFrançois Tigeot #define GEN9_LR_CONTEXT_RENDER_SIZE (22 * PAGE_SIZE)
1411b13d190SFrançois Tigeot #define GEN8_LR_CONTEXT_RENDER_SIZE (20 * PAGE_SIZE)
1421b13d190SFrançois Tigeot #define GEN8_LR_CONTEXT_OTHER_SIZE (2 * PAGE_SIZE)
1431b13d190SFrançois Tigeot 
1441b13d190SFrançois Tigeot #define RING_EXECLIST_QFULL		(1 << 0x2)
1451b13d190SFrançois Tigeot #define RING_EXECLIST1_VALID		(1 << 0x3)
1461b13d190SFrançois Tigeot #define RING_EXECLIST0_VALID		(1 << 0x4)
1471b13d190SFrançois Tigeot #define RING_EXECLIST_ACTIVE_STATUS	(3 << 0xE)
1481b13d190SFrançois Tigeot #define RING_EXECLIST1_ACTIVE		(1 << 0x11)
1491b13d190SFrançois Tigeot #define RING_EXECLIST0_ACTIVE		(1 << 0x12)
1501b13d190SFrançois Tigeot 
1511b13d190SFrançois Tigeot #define GEN8_CTX_STATUS_IDLE_ACTIVE	(1 << 0)
1521b13d190SFrançois Tigeot #define GEN8_CTX_STATUS_PREEMPTED	(1 << 1)
1531b13d190SFrançois Tigeot #define GEN8_CTX_STATUS_ELEMENT_SWITCH	(1 << 2)
1541b13d190SFrançois Tigeot #define GEN8_CTX_STATUS_ACTIVE_IDLE	(1 << 3)
1551b13d190SFrançois Tigeot #define GEN8_CTX_STATUS_COMPLETE	(1 << 4)
1561b13d190SFrançois Tigeot #define GEN8_CTX_STATUS_LITE_RESTORE	(1 << 15)
1571b13d190SFrançois Tigeot 
1581b13d190SFrançois Tigeot #define CTX_LRI_HEADER_0		0x01
1591b13d190SFrançois Tigeot #define CTX_CONTEXT_CONTROL		0x02
1601b13d190SFrançois Tigeot #define CTX_RING_HEAD			0x04
1611b13d190SFrançois Tigeot #define CTX_RING_TAIL			0x06
1621b13d190SFrançois Tigeot #define CTX_RING_BUFFER_START		0x08
1631b13d190SFrançois Tigeot #define CTX_RING_BUFFER_CONTROL		0x0a
1641b13d190SFrançois Tigeot #define CTX_BB_HEAD_U			0x0c
1651b13d190SFrançois Tigeot #define CTX_BB_HEAD_L			0x0e
1661b13d190SFrançois Tigeot #define CTX_BB_STATE			0x10
1671b13d190SFrançois Tigeot #define CTX_SECOND_BB_HEAD_U		0x12
1681b13d190SFrançois Tigeot #define CTX_SECOND_BB_HEAD_L		0x14
1691b13d190SFrançois Tigeot #define CTX_SECOND_BB_STATE		0x16
1701b13d190SFrançois Tigeot #define CTX_BB_PER_CTX_PTR		0x18
1711b13d190SFrançois Tigeot #define CTX_RCS_INDIRECT_CTX		0x1a
1721b13d190SFrançois Tigeot #define CTX_RCS_INDIRECT_CTX_OFFSET	0x1c
1731b13d190SFrançois Tigeot #define CTX_LRI_HEADER_1		0x21
1741b13d190SFrançois Tigeot #define CTX_CTX_TIMESTAMP		0x22
1751b13d190SFrançois Tigeot #define CTX_PDP3_UDW			0x24
1761b13d190SFrançois Tigeot #define CTX_PDP3_LDW			0x26
1771b13d190SFrançois Tigeot #define CTX_PDP2_UDW			0x28
1781b13d190SFrançois Tigeot #define CTX_PDP2_LDW			0x2a
1791b13d190SFrançois Tigeot #define CTX_PDP1_UDW			0x2c
1801b13d190SFrançois Tigeot #define CTX_PDP1_LDW			0x2e
1811b13d190SFrançois Tigeot #define CTX_PDP0_UDW			0x30
1821b13d190SFrançois Tigeot #define CTX_PDP0_LDW			0x32
1831b13d190SFrançois Tigeot #define CTX_LRI_HEADER_2		0x41
1841b13d190SFrançois Tigeot #define CTX_R_PWR_CLK_STATE		0x42
1851b13d190SFrançois Tigeot #define CTX_GPGPU_CSR_BASE_ADDRESS	0x44
1861b13d190SFrançois Tigeot 
1871b13d190SFrançois Tigeot #define GEN8_CTX_VALID (1<<0)
1881b13d190SFrançois Tigeot #define GEN8_CTX_FORCE_PD_RESTORE (1<<1)
1891b13d190SFrançois Tigeot #define GEN8_CTX_FORCE_RESTORE (1<<2)
1901b13d190SFrançois Tigeot #define GEN8_CTX_L3LLC_COHERENT (1<<5)
1911b13d190SFrançois Tigeot #define GEN8_CTX_PRIVILEGE (1<<8)
1921b13d190SFrançois Tigeot enum {
1931b13d190SFrançois Tigeot 	ADVANCED_CONTEXT = 0,
1941b13d190SFrançois Tigeot 	LEGACY_CONTEXT,
1951b13d190SFrançois Tigeot 	ADVANCED_AD_CONTEXT,
1961b13d190SFrançois Tigeot 	LEGACY_64B_CONTEXT
1971b13d190SFrançois Tigeot };
1981b13d190SFrançois Tigeot #define GEN8_CTX_MODE_SHIFT 3
1991b13d190SFrançois Tigeot enum {
2001b13d190SFrançois Tigeot 	FAULT_AND_HANG = 0,
2011b13d190SFrançois Tigeot 	FAULT_AND_HALT, /* Debug only */
2021b13d190SFrançois Tigeot 	FAULT_AND_STREAM,
2031b13d190SFrançois Tigeot 	FAULT_AND_CONTINUE /* Unsupported */
2041b13d190SFrançois Tigeot };
2051b13d190SFrançois Tigeot #define GEN8_CTX_ID_SHIFT 32
2061b13d190SFrançois Tigeot 
207*2c9916cdSFrançois Tigeot static int intel_lr_context_pin(struct intel_engine_cs *ring,
208*2c9916cdSFrançois Tigeot 		struct intel_context *ctx);
209*2c9916cdSFrançois Tigeot 
2101b13d190SFrançois Tigeot /**
2111b13d190SFrançois Tigeot  * intel_sanitize_enable_execlists() - sanitize i915.enable_execlists
2121b13d190SFrançois Tigeot  * @dev: DRM device.
2131b13d190SFrançois Tigeot  * @enable_execlists: value of i915.enable_execlists module parameter.
2141b13d190SFrançois Tigeot  *
2151b13d190SFrançois Tigeot  * Only certain platforms support Execlists (the prerequisites being
216*2c9916cdSFrançois Tigeot  * support for Logical Ring Contexts and Aliasing PPGTT or better).
2171b13d190SFrançois Tigeot  *
2181b13d190SFrançois Tigeot  * Return: 1 if Execlists is supported and has to be enabled.
2191b13d190SFrançois Tigeot  */
2201b13d190SFrançois Tigeot int intel_sanitize_enable_execlists(struct drm_device *dev, int enable_execlists)
2211b13d190SFrançois Tigeot {
2221b13d190SFrançois Tigeot 	WARN_ON(i915.enable_ppgtt == -1);
2231b13d190SFrançois Tigeot 
224*2c9916cdSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 9)
225*2c9916cdSFrançois Tigeot 		return 1;
226*2c9916cdSFrançois Tigeot 
2271b13d190SFrançois Tigeot 	if (enable_execlists == 0)
2281b13d190SFrançois Tigeot 		return 0;
2291b13d190SFrançois Tigeot 
2301b13d190SFrançois Tigeot 	if (HAS_LOGICAL_RING_CONTEXTS(dev) && USES_PPGTT(dev) &&
2311b13d190SFrançois Tigeot 	    i915.use_mmio_flip >= 0)
2321b13d190SFrançois Tigeot 		return 1;
2331b13d190SFrançois Tigeot 
2341b13d190SFrançois Tigeot 	return 0;
2351b13d190SFrançois Tigeot }
2361b13d190SFrançois Tigeot 
2371b13d190SFrançois Tigeot /**
2381b13d190SFrançois Tigeot  * intel_execlists_ctx_id() - get the Execlists Context ID
2391b13d190SFrançois Tigeot  * @ctx_obj: Logical Ring Context backing object.
2401b13d190SFrançois Tigeot  *
2411b13d190SFrançois Tigeot  * Do not confuse with ctx->id! Unfortunately we have a name overload
2421b13d190SFrançois Tigeot  * here: the old context ID we pass to userspace as a handler so that
2431b13d190SFrançois Tigeot  * they can refer to a context, and the new context ID we pass to the
2441b13d190SFrançois Tigeot  * ELSP so that the GPU can inform us of the context status via
2451b13d190SFrançois Tigeot  * interrupts.
2461b13d190SFrançois Tigeot  *
2471b13d190SFrançois Tigeot  * Return: 20-bits globally unique context ID.
2481b13d190SFrançois Tigeot  */
2491b13d190SFrançois Tigeot u32 intel_execlists_ctx_id(struct drm_i915_gem_object *ctx_obj)
2501b13d190SFrançois Tigeot {
2511b13d190SFrançois Tigeot 	u32 lrca = i915_gem_obj_ggtt_offset(ctx_obj);
2521b13d190SFrançois Tigeot 
2531b13d190SFrançois Tigeot 	/* LRCA is required to be 4K aligned so the more significant 20 bits
2541b13d190SFrançois Tigeot 	 * are globally unique */
2551b13d190SFrançois Tigeot 	return lrca >> 12;
2561b13d190SFrançois Tigeot }
2571b13d190SFrançois Tigeot 
2581b13d190SFrançois Tigeot static uint64_t execlists_ctx_descriptor(struct drm_i915_gem_object *ctx_obj)
2591b13d190SFrançois Tigeot {
2601b13d190SFrançois Tigeot 	uint64_t desc;
2611b13d190SFrançois Tigeot 	uint64_t lrca = i915_gem_obj_ggtt_offset(ctx_obj);
2621b13d190SFrançois Tigeot 
2631b13d190SFrançois Tigeot 	WARN_ON(lrca & 0xFFFFFFFF00000FFFULL);
2641b13d190SFrançois Tigeot 
2651b13d190SFrançois Tigeot 	desc = GEN8_CTX_VALID;
2661b13d190SFrançois Tigeot 	desc |= LEGACY_CONTEXT << GEN8_CTX_MODE_SHIFT;
2671b13d190SFrançois Tigeot 	desc |= GEN8_CTX_L3LLC_COHERENT;
2681b13d190SFrançois Tigeot 	desc |= GEN8_CTX_PRIVILEGE;
2691b13d190SFrançois Tigeot 	desc |= lrca;
2701b13d190SFrançois Tigeot 	desc |= (u64)intel_execlists_ctx_id(ctx_obj) << GEN8_CTX_ID_SHIFT;
2711b13d190SFrançois Tigeot 
2721b13d190SFrançois Tigeot 	/* TODO: WaDisableLiteRestore when we start using semaphore
2731b13d190SFrançois Tigeot 	 * signalling between Command Streamers */
2741b13d190SFrançois Tigeot 	/* desc |= GEN8_CTX_FORCE_RESTORE; */
2751b13d190SFrançois Tigeot 
2761b13d190SFrançois Tigeot 	return desc;
2771b13d190SFrançois Tigeot }
2781b13d190SFrançois Tigeot 
2791b13d190SFrançois Tigeot static void execlists_elsp_write(struct intel_engine_cs *ring,
2801b13d190SFrançois Tigeot 				 struct drm_i915_gem_object *ctx_obj0,
2811b13d190SFrançois Tigeot 				 struct drm_i915_gem_object *ctx_obj1)
2821b13d190SFrançois Tigeot {
283*2c9916cdSFrançois Tigeot 	struct drm_device *dev = ring->dev;
284*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2851b13d190SFrançois Tigeot 	uint64_t temp = 0;
2861b13d190SFrançois Tigeot 	uint32_t desc[4];
2871b13d190SFrançois Tigeot 
2881b13d190SFrançois Tigeot 	/* XXX: You must always write both descriptors in the order below. */
2891b13d190SFrançois Tigeot 	if (ctx_obj1)
2901b13d190SFrançois Tigeot 		temp = execlists_ctx_descriptor(ctx_obj1);
2911b13d190SFrançois Tigeot 	else
2921b13d190SFrançois Tigeot 		temp = 0;
2931b13d190SFrançois Tigeot 	desc[1] = (u32)(temp >> 32);
2941b13d190SFrançois Tigeot 	desc[0] = (u32)temp;
2951b13d190SFrançois Tigeot 
2961b13d190SFrançois Tigeot 	temp = execlists_ctx_descriptor(ctx_obj0);
2971b13d190SFrançois Tigeot 	desc[3] = (u32)(temp >> 32);
2981b13d190SFrançois Tigeot 	desc[2] = (u32)temp;
2991b13d190SFrançois Tigeot 
300*2c9916cdSFrançois Tigeot 	intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
3011b13d190SFrançois Tigeot 	I915_WRITE(RING_ELSP(ring), desc[1]);
3021b13d190SFrançois Tigeot 	I915_WRITE(RING_ELSP(ring), desc[0]);
3031b13d190SFrançois Tigeot 	I915_WRITE(RING_ELSP(ring), desc[3]);
304*2c9916cdSFrançois Tigeot 
3051b13d190SFrançois Tigeot 	/* The context is automatically loaded after the following */
3061b13d190SFrançois Tigeot 	I915_WRITE(RING_ELSP(ring), desc[2]);
3071b13d190SFrançois Tigeot 
3081b13d190SFrançois Tigeot 	/* ELSP is a wo register, so use another nearby reg for posting instead */
3091b13d190SFrançois Tigeot 	POSTING_READ(RING_EXECLIST_STATUS(ring));
310*2c9916cdSFrançois Tigeot 	intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
3111b13d190SFrançois Tigeot }
3121b13d190SFrançois Tigeot 
313*2c9916cdSFrançois Tigeot static int execlists_update_context(struct drm_i915_gem_object *ctx_obj,
314*2c9916cdSFrançois Tigeot 				    struct drm_i915_gem_object *ring_obj,
315*2c9916cdSFrançois Tigeot 				    u32 tail)
3161b13d190SFrançois Tigeot {
3171b13d190SFrançois Tigeot 	struct vm_page *page;
3181b13d190SFrançois Tigeot 	uint32_t *reg_state;
3191b13d190SFrançois Tigeot 
3201b13d190SFrançois Tigeot 	page = i915_gem_object_get_page(ctx_obj, 1);
3211b13d190SFrançois Tigeot 	reg_state = kmap_atomic(page);
3221b13d190SFrançois Tigeot 
3231b13d190SFrançois Tigeot 	reg_state[CTX_RING_TAIL+1] = tail;
324*2c9916cdSFrançois Tigeot 	reg_state[CTX_RING_BUFFER_START+1] = i915_gem_obj_ggtt_offset(ring_obj);
3251b13d190SFrançois Tigeot 
3261b13d190SFrançois Tigeot 	kunmap_atomic(reg_state);
3271b13d190SFrançois Tigeot 
3281b13d190SFrançois Tigeot 	return 0;
3291b13d190SFrançois Tigeot }
3301b13d190SFrançois Tigeot 
331*2c9916cdSFrançois Tigeot static void execlists_submit_contexts(struct intel_engine_cs *ring,
3321b13d190SFrançois Tigeot 				      struct intel_context *to0, u32 tail0,
3331b13d190SFrançois Tigeot 				      struct intel_context *to1, u32 tail1)
3341b13d190SFrançois Tigeot {
335*2c9916cdSFrançois Tigeot 	struct drm_i915_gem_object *ctx_obj0 = to0->engine[ring->id].state;
336*2c9916cdSFrançois Tigeot 	struct intel_ringbuffer *ringbuf0 = to0->engine[ring->id].ringbuf;
3371b13d190SFrançois Tigeot 	struct drm_i915_gem_object *ctx_obj1 = NULL;
338*2c9916cdSFrançois Tigeot 	struct intel_ringbuffer *ringbuf1 = NULL;
3391b13d190SFrançois Tigeot 
3401b13d190SFrançois Tigeot 	BUG_ON(!ctx_obj0);
3411b13d190SFrançois Tigeot 	WARN_ON(!i915_gem_obj_is_pinned(ctx_obj0));
342*2c9916cdSFrançois Tigeot 	WARN_ON(!i915_gem_obj_is_pinned(ringbuf0->obj));
3431b13d190SFrançois Tigeot 
344*2c9916cdSFrançois Tigeot 	execlists_update_context(ctx_obj0, ringbuf0->obj, tail0);
3451b13d190SFrançois Tigeot 
3461b13d190SFrançois Tigeot 	if (to1) {
347*2c9916cdSFrançois Tigeot 		ringbuf1 = to1->engine[ring->id].ringbuf;
3481b13d190SFrançois Tigeot 		ctx_obj1 = to1->engine[ring->id].state;
3491b13d190SFrançois Tigeot 		BUG_ON(!ctx_obj1);
3501b13d190SFrançois Tigeot 		WARN_ON(!i915_gem_obj_is_pinned(ctx_obj1));
351*2c9916cdSFrançois Tigeot 		WARN_ON(!i915_gem_obj_is_pinned(ringbuf1->obj));
3521b13d190SFrançois Tigeot 
353*2c9916cdSFrançois Tigeot 		execlists_update_context(ctx_obj1, ringbuf1->obj, tail1);
3541b13d190SFrançois Tigeot 	}
3551b13d190SFrançois Tigeot 
3561b13d190SFrançois Tigeot 	execlists_elsp_write(ring, ctx_obj0, ctx_obj1);
3571b13d190SFrançois Tigeot }
3581b13d190SFrançois Tigeot 
3591b13d190SFrançois Tigeot static void execlists_context_unqueue(struct intel_engine_cs *ring)
3601b13d190SFrançois Tigeot {
361*2c9916cdSFrançois Tigeot 	struct drm_i915_gem_request *req0 = NULL, *req1 = NULL;
362*2c9916cdSFrançois Tigeot 	struct drm_i915_gem_request *cursor = NULL, *tmp = NULL;
3631b13d190SFrançois Tigeot 
3641b13d190SFrançois Tigeot 	assert_spin_locked(&ring->execlist_lock);
3651b13d190SFrançois Tigeot 
3661b13d190SFrançois Tigeot 	if (list_empty(&ring->execlist_queue))
3671b13d190SFrançois Tigeot 		return;
3681b13d190SFrançois Tigeot 
3691b13d190SFrançois Tigeot 	/* Try to read in pairs */
3701b13d190SFrançois Tigeot 	list_for_each_entry_safe(cursor, tmp, &ring->execlist_queue,
3711b13d190SFrançois Tigeot 				 execlist_link) {
3721b13d190SFrançois Tigeot 		if (!req0) {
3731b13d190SFrançois Tigeot 			req0 = cursor;
3741b13d190SFrançois Tigeot 		} else if (req0->ctx == cursor->ctx) {
3751b13d190SFrançois Tigeot 			/* Same ctx: ignore first request, as second request
3761b13d190SFrançois Tigeot 			 * will update tail past first request's workload */
3771b13d190SFrançois Tigeot 			cursor->elsp_submitted = req0->elsp_submitted;
3781b13d190SFrançois Tigeot 			list_del(&req0->execlist_link);
379*2c9916cdSFrançois Tigeot 			list_add_tail(&req0->execlist_link,
380*2c9916cdSFrançois Tigeot 				&ring->execlist_retired_req_list);
3811b13d190SFrançois Tigeot 			req0 = cursor;
3821b13d190SFrançois Tigeot 		} else {
3831b13d190SFrançois Tigeot 			req1 = cursor;
3841b13d190SFrançois Tigeot 			break;
3851b13d190SFrançois Tigeot 		}
3861b13d190SFrançois Tigeot 	}
3871b13d190SFrançois Tigeot 
3881b13d190SFrançois Tigeot 	WARN_ON(req1 && req1->elsp_submitted);
3891b13d190SFrançois Tigeot 
390*2c9916cdSFrançois Tigeot 	execlists_submit_contexts(ring, req0->ctx, req0->tail,
3911b13d190SFrançois Tigeot 				  req1 ? req1->ctx : NULL,
392*2c9916cdSFrançois Tigeot 				  req1 ? req1->tail : 0);
3931b13d190SFrançois Tigeot 
3941b13d190SFrançois Tigeot 	req0->elsp_submitted++;
3951b13d190SFrançois Tigeot 	if (req1)
3961b13d190SFrançois Tigeot 		req1->elsp_submitted++;
3971b13d190SFrançois Tigeot }
3981b13d190SFrançois Tigeot 
3991b13d190SFrançois Tigeot static bool execlists_check_remove_request(struct intel_engine_cs *ring,
4001b13d190SFrançois Tigeot 					   u32 request_id)
4011b13d190SFrançois Tigeot {
402*2c9916cdSFrançois Tigeot 	struct drm_i915_gem_request *head_req;
4031b13d190SFrançois Tigeot 
4041b13d190SFrançois Tigeot 	assert_spin_locked(&ring->execlist_lock);
4051b13d190SFrançois Tigeot 
4061b13d190SFrançois Tigeot 	head_req = list_first_entry_or_null(&ring->execlist_queue,
407*2c9916cdSFrançois Tigeot 					    struct drm_i915_gem_request,
4081b13d190SFrançois Tigeot 					    execlist_link);
4091b13d190SFrançois Tigeot 
4101b13d190SFrançois Tigeot 	if (head_req != NULL) {
4111b13d190SFrançois Tigeot 		struct drm_i915_gem_object *ctx_obj =
4121b13d190SFrançois Tigeot 				head_req->ctx->engine[ring->id].state;
4131b13d190SFrançois Tigeot 		if (intel_execlists_ctx_id(ctx_obj) == request_id) {
4141b13d190SFrançois Tigeot 			WARN(head_req->elsp_submitted == 0,
4151b13d190SFrançois Tigeot 			     "Never submitted head request\n");
4161b13d190SFrançois Tigeot 
4171b13d190SFrançois Tigeot 			if (--head_req->elsp_submitted <= 0) {
4181b13d190SFrançois Tigeot 				list_del(&head_req->execlist_link);
419*2c9916cdSFrançois Tigeot 				list_add_tail(&head_req->execlist_link,
420*2c9916cdSFrançois Tigeot 					&ring->execlist_retired_req_list);
4211b13d190SFrançois Tigeot 				return true;
4221b13d190SFrançois Tigeot 			}
4231b13d190SFrançois Tigeot 		}
4241b13d190SFrançois Tigeot 	}
4251b13d190SFrançois Tigeot 
4261b13d190SFrançois Tigeot 	return false;
4271b13d190SFrançois Tigeot }
4281b13d190SFrançois Tigeot 
4291b13d190SFrançois Tigeot /**
430*2c9916cdSFrançois Tigeot  * intel_lrc_irq_handler() - handle Context Switch interrupts
4311b13d190SFrançois Tigeot  * @ring: Engine Command Streamer to handle.
4321b13d190SFrançois Tigeot  *
4331b13d190SFrançois Tigeot  * Check the unread Context Status Buffers and manage the submission of new
4341b13d190SFrançois Tigeot  * contexts to the ELSP accordingly.
4351b13d190SFrançois Tigeot  */
436*2c9916cdSFrançois Tigeot void intel_lrc_irq_handler(struct intel_engine_cs *ring)
4371b13d190SFrançois Tigeot {
4381b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
4391b13d190SFrançois Tigeot 	u32 status_pointer;
4401b13d190SFrançois Tigeot 	u8 read_pointer;
4411b13d190SFrançois Tigeot 	u8 write_pointer;
4421b13d190SFrançois Tigeot 	u32 status;
4431b13d190SFrançois Tigeot 	u32 status_id;
4441b13d190SFrançois Tigeot 	u32 submit_contexts = 0;
4451b13d190SFrançois Tigeot 
4461b13d190SFrançois Tigeot 	status_pointer = I915_READ(RING_CONTEXT_STATUS_PTR(ring));
4471b13d190SFrançois Tigeot 
4481b13d190SFrançois Tigeot 	read_pointer = ring->next_context_status_buffer;
4491b13d190SFrançois Tigeot 	write_pointer = status_pointer & 0x07;
4501b13d190SFrançois Tigeot 	if (read_pointer > write_pointer)
4511b13d190SFrançois Tigeot 		write_pointer += 6;
4521b13d190SFrançois Tigeot 
4531b13d190SFrançois Tigeot 	lockmgr(&ring->execlist_lock, LK_EXCLUSIVE);
4541b13d190SFrançois Tigeot 
4551b13d190SFrançois Tigeot 	while (read_pointer < write_pointer) {
4561b13d190SFrançois Tigeot 		read_pointer++;
4571b13d190SFrançois Tigeot 		status = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
4581b13d190SFrançois Tigeot 				(read_pointer % 6) * 8);
4591b13d190SFrançois Tigeot 		status_id = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
4601b13d190SFrançois Tigeot 				(read_pointer % 6) * 8 + 4);
4611b13d190SFrançois Tigeot 
4621b13d190SFrançois Tigeot 		if (status & GEN8_CTX_STATUS_PREEMPTED) {
4631b13d190SFrançois Tigeot 			if (status & GEN8_CTX_STATUS_LITE_RESTORE) {
4641b13d190SFrançois Tigeot 				if (execlists_check_remove_request(ring, status_id))
4651b13d190SFrançois Tigeot 					WARN(1, "Lite Restored request removed from queue\n");
4661b13d190SFrançois Tigeot 			} else
4671b13d190SFrançois Tigeot 				WARN(1, "Preemption without Lite Restore\n");
4681b13d190SFrançois Tigeot 		}
4691b13d190SFrançois Tigeot 
4701b13d190SFrançois Tigeot 		 if ((status & GEN8_CTX_STATUS_ACTIVE_IDLE) ||
4711b13d190SFrançois Tigeot 		     (status & GEN8_CTX_STATUS_ELEMENT_SWITCH)) {
4721b13d190SFrançois Tigeot 			if (execlists_check_remove_request(ring, status_id))
4731b13d190SFrançois Tigeot 				submit_contexts++;
4741b13d190SFrançois Tigeot 		}
4751b13d190SFrançois Tigeot 	}
4761b13d190SFrançois Tigeot 
4771b13d190SFrançois Tigeot 	if (submit_contexts != 0)
4781b13d190SFrançois Tigeot 		execlists_context_unqueue(ring);
4791b13d190SFrançois Tigeot 
4801b13d190SFrançois Tigeot 	lockmgr(&ring->execlist_lock, LK_RELEASE);
4811b13d190SFrançois Tigeot 
4821b13d190SFrançois Tigeot 	WARN(submit_contexts > 2, "More than two context complete events?\n");
4831b13d190SFrançois Tigeot 	ring->next_context_status_buffer = write_pointer % 6;
4841b13d190SFrançois Tigeot 
4851b13d190SFrançois Tigeot 	I915_WRITE(RING_CONTEXT_STATUS_PTR(ring),
4861b13d190SFrançois Tigeot 		   ((u32)ring->next_context_status_buffer & 0x07) << 8);
4871b13d190SFrançois Tigeot }
4881b13d190SFrançois Tigeot 
4891b13d190SFrançois Tigeot static int execlists_context_queue(struct intel_engine_cs *ring,
4901b13d190SFrançois Tigeot 				   struct intel_context *to,
491*2c9916cdSFrançois Tigeot 				   u32 tail,
492*2c9916cdSFrançois Tigeot 				   struct drm_i915_gem_request *request)
4931b13d190SFrançois Tigeot {
494*2c9916cdSFrançois Tigeot 	struct drm_i915_gem_request *cursor;
4951b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
4961b13d190SFrançois Tigeot 	int num_elements = 0;
4971b13d190SFrançois Tigeot 
498*2c9916cdSFrançois Tigeot 	if (to != ring->default_context)
499*2c9916cdSFrançois Tigeot 		intel_lr_context_pin(ring, to);
500*2c9916cdSFrançois Tigeot 
501*2c9916cdSFrançois Tigeot 	if (!request) {
502*2c9916cdSFrançois Tigeot 		/*
503*2c9916cdSFrançois Tigeot 		 * If there isn't a request associated with this submission,
504*2c9916cdSFrançois Tigeot 		 * create one as a temporary holder.
505*2c9916cdSFrançois Tigeot 		 */
506*2c9916cdSFrançois Tigeot 		request = kzalloc(sizeof(*request), GFP_KERNEL);
507*2c9916cdSFrançois Tigeot 		if (request == NULL)
5081b13d190SFrançois Tigeot 			return -ENOMEM;
509*2c9916cdSFrançois Tigeot 		request->ring = ring;
510*2c9916cdSFrançois Tigeot 		request->ctx = to;
511*2c9916cdSFrançois Tigeot 		kref_init(&request->ref);
512*2c9916cdSFrançois Tigeot 		request->uniq = dev_priv->request_uniq++;
513*2c9916cdSFrançois Tigeot 		i915_gem_context_reference(request->ctx);
514*2c9916cdSFrançois Tigeot 	} else {
515*2c9916cdSFrançois Tigeot 		i915_gem_request_reference(request);
516*2c9916cdSFrançois Tigeot 		WARN_ON(to != request->ctx);
517*2c9916cdSFrançois Tigeot 	}
518*2c9916cdSFrançois Tigeot 	request->tail = tail;
5191b13d190SFrançois Tigeot 
5201b13d190SFrançois Tigeot 	intel_runtime_pm_get(dev_priv);
5211b13d190SFrançois Tigeot 
5221b13d190SFrançois Tigeot 	lockmgr(&ring->execlist_lock, LK_EXCLUSIVE);
5231b13d190SFrançois Tigeot 
5241b13d190SFrançois Tigeot 	list_for_each_entry(cursor, &ring->execlist_queue, execlist_link)
5251b13d190SFrançois Tigeot 		if (++num_elements > 2)
5261b13d190SFrançois Tigeot 			break;
5271b13d190SFrançois Tigeot 
5281b13d190SFrançois Tigeot 	if (num_elements > 2) {
529*2c9916cdSFrançois Tigeot 		struct drm_i915_gem_request *tail_req;
5301b13d190SFrançois Tigeot 
5311b13d190SFrançois Tigeot 		tail_req = list_last_entry(&ring->execlist_queue,
532*2c9916cdSFrançois Tigeot 					   struct drm_i915_gem_request,
5331b13d190SFrançois Tigeot 					   execlist_link);
5341b13d190SFrançois Tigeot 
5351b13d190SFrançois Tigeot 		if (to == tail_req->ctx) {
5361b13d190SFrançois Tigeot 			WARN(tail_req->elsp_submitted != 0,
5371b13d190SFrançois Tigeot 				"More than 2 already-submitted reqs queued\n");
5381b13d190SFrançois Tigeot 			list_del(&tail_req->execlist_link);
539*2c9916cdSFrançois Tigeot 			list_add_tail(&tail_req->execlist_link,
540*2c9916cdSFrançois Tigeot 				&ring->execlist_retired_req_list);
5411b13d190SFrançois Tigeot 		}
5421b13d190SFrançois Tigeot 	}
5431b13d190SFrançois Tigeot 
544*2c9916cdSFrançois Tigeot 	list_add_tail(&request->execlist_link, &ring->execlist_queue);
5451b13d190SFrançois Tigeot 	if (num_elements == 0)
5461b13d190SFrançois Tigeot 		execlists_context_unqueue(ring);
5471b13d190SFrançois Tigeot 
5481b13d190SFrançois Tigeot 	lockmgr(&ring->execlist_lock, LK_RELEASE);
5491b13d190SFrançois Tigeot 
5501b13d190SFrançois Tigeot 	return 0;
5511b13d190SFrançois Tigeot }
5521b13d190SFrançois Tigeot 
553*2c9916cdSFrançois Tigeot static int logical_ring_invalidate_all_caches(struct intel_ringbuffer *ringbuf,
554*2c9916cdSFrançois Tigeot 					      struct intel_context *ctx)
5551b13d190SFrançois Tigeot {
5561b13d190SFrançois Tigeot 	struct intel_engine_cs *ring = ringbuf->ring;
5571b13d190SFrançois Tigeot 	uint32_t flush_domains;
5581b13d190SFrançois Tigeot 	int ret;
5591b13d190SFrançois Tigeot 
5601b13d190SFrançois Tigeot 	flush_domains = 0;
5611b13d190SFrançois Tigeot 	if (ring->gpu_caches_dirty)
5621b13d190SFrançois Tigeot 		flush_domains = I915_GEM_GPU_DOMAINS;
5631b13d190SFrançois Tigeot 
564*2c9916cdSFrançois Tigeot 	ret = ring->emit_flush(ringbuf, ctx,
565*2c9916cdSFrançois Tigeot 			       I915_GEM_GPU_DOMAINS, flush_domains);
5661b13d190SFrançois Tigeot 	if (ret)
5671b13d190SFrançois Tigeot 		return ret;
5681b13d190SFrançois Tigeot 
5691b13d190SFrançois Tigeot 	ring->gpu_caches_dirty = false;
5701b13d190SFrançois Tigeot 	return 0;
5711b13d190SFrançois Tigeot }
5721b13d190SFrançois Tigeot 
5731b13d190SFrançois Tigeot static int execlists_move_to_gpu(struct intel_ringbuffer *ringbuf,
574*2c9916cdSFrançois Tigeot 				 struct intel_context *ctx,
5751b13d190SFrançois Tigeot 				 struct list_head *vmas)
5761b13d190SFrançois Tigeot {
5771b13d190SFrançois Tigeot 	struct intel_engine_cs *ring = ringbuf->ring;
5781b13d190SFrançois Tigeot 	struct i915_vma *vma;
5791b13d190SFrançois Tigeot 	uint32_t flush_domains = 0;
5801b13d190SFrançois Tigeot 	bool flush_chipset = false;
5811b13d190SFrançois Tigeot 	int ret;
5821b13d190SFrançois Tigeot 
5831b13d190SFrançois Tigeot 	list_for_each_entry(vma, vmas, exec_list) {
5841b13d190SFrançois Tigeot 		struct drm_i915_gem_object *obj = vma->obj;
5851b13d190SFrançois Tigeot 
5861b13d190SFrançois Tigeot 		ret = i915_gem_object_sync(obj, ring);
5871b13d190SFrançois Tigeot 		if (ret)
5881b13d190SFrançois Tigeot 			return ret;
5891b13d190SFrançois Tigeot 
5901b13d190SFrançois Tigeot 		if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
5911b13d190SFrançois Tigeot 			flush_chipset |= i915_gem_clflush_object(obj, false);
5921b13d190SFrançois Tigeot 
5931b13d190SFrançois Tigeot 		flush_domains |= obj->base.write_domain;
5941b13d190SFrançois Tigeot 	}
5951b13d190SFrançois Tigeot 
5961b13d190SFrançois Tigeot 	if (flush_domains & I915_GEM_DOMAIN_GTT)
5971b13d190SFrançois Tigeot 		wmb();
5981b13d190SFrançois Tigeot 
5991b13d190SFrançois Tigeot 	/* Unconditionally invalidate gpu caches and ensure that we do flush
6001b13d190SFrançois Tigeot 	 * any residual writes from the previous batch.
6011b13d190SFrançois Tigeot 	 */
602*2c9916cdSFrançois Tigeot 	return logical_ring_invalidate_all_caches(ringbuf, ctx);
6031b13d190SFrançois Tigeot }
6041b13d190SFrançois Tigeot 
6051b13d190SFrançois Tigeot /**
6061b13d190SFrançois Tigeot  * execlists_submission() - submit a batchbuffer for execution, Execlists style
6071b13d190SFrançois Tigeot  * @dev: DRM device.
6081b13d190SFrançois Tigeot  * @file: DRM file.
6091b13d190SFrançois Tigeot  * @ring: Engine Command Streamer to submit to.
6101b13d190SFrançois Tigeot  * @ctx: Context to employ for this submission.
6111b13d190SFrançois Tigeot  * @args: execbuffer call arguments.
6121b13d190SFrançois Tigeot  * @vmas: list of vmas.
6131b13d190SFrançois Tigeot  * @batch_obj: the batchbuffer to submit.
6141b13d190SFrançois Tigeot  * @exec_start: batchbuffer start virtual address pointer.
6151b13d190SFrançois Tigeot  * @flags: translated execbuffer call flags.
6161b13d190SFrançois Tigeot  *
6171b13d190SFrançois Tigeot  * This is the evil twin version of i915_gem_ringbuffer_submission. It abstracts
6181b13d190SFrançois Tigeot  * away the submission details of the execbuffer ioctl call.
6191b13d190SFrançois Tigeot  *
6201b13d190SFrançois Tigeot  * Return: non-zero if the submission fails.
6211b13d190SFrançois Tigeot  */
6221b13d190SFrançois Tigeot int intel_execlists_submission(struct drm_device *dev, struct drm_file *file,
6231b13d190SFrançois Tigeot 			       struct intel_engine_cs *ring,
6241b13d190SFrançois Tigeot 			       struct intel_context *ctx,
6251b13d190SFrançois Tigeot 			       struct drm_i915_gem_execbuffer2 *args,
6261b13d190SFrançois Tigeot 			       struct list_head *vmas,
6271b13d190SFrançois Tigeot 			       struct drm_i915_gem_object *batch_obj,
6281b13d190SFrançois Tigeot 			       u64 exec_start, u32 flags)
6291b13d190SFrançois Tigeot {
6301b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
6311b13d190SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
6321b13d190SFrançois Tigeot 	int instp_mode;
6331b13d190SFrançois Tigeot 	u32 instp_mask;
6341b13d190SFrançois Tigeot 	int ret;
6351b13d190SFrançois Tigeot 
6361b13d190SFrançois Tigeot 	instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
6371b13d190SFrançois Tigeot 	instp_mask = I915_EXEC_CONSTANTS_MASK;
6381b13d190SFrançois Tigeot 	switch (instp_mode) {
6391b13d190SFrançois Tigeot 	case I915_EXEC_CONSTANTS_REL_GENERAL:
6401b13d190SFrançois Tigeot 	case I915_EXEC_CONSTANTS_ABSOLUTE:
6411b13d190SFrançois Tigeot 	case I915_EXEC_CONSTANTS_REL_SURFACE:
6421b13d190SFrançois Tigeot 		if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) {
6431b13d190SFrançois Tigeot 			DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
6441b13d190SFrançois Tigeot 			return -EINVAL;
6451b13d190SFrançois Tigeot 		}
6461b13d190SFrançois Tigeot 
6471b13d190SFrançois Tigeot 		if (instp_mode != dev_priv->relative_constants_mode) {
6481b13d190SFrançois Tigeot 			if (instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
6491b13d190SFrançois Tigeot 				DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
6501b13d190SFrançois Tigeot 				return -EINVAL;
6511b13d190SFrançois Tigeot 			}
6521b13d190SFrançois Tigeot 
6531b13d190SFrançois Tigeot 			/* The HW changed the meaning on this bit on gen6 */
6541b13d190SFrançois Tigeot 			instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
6551b13d190SFrançois Tigeot 		}
6561b13d190SFrançois Tigeot 		break;
6571b13d190SFrançois Tigeot 	default:
6581b13d190SFrançois Tigeot 		DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
6591b13d190SFrançois Tigeot 		return -EINVAL;
6601b13d190SFrançois Tigeot 	}
6611b13d190SFrançois Tigeot 
6621b13d190SFrançois Tigeot 	if (args->num_cliprects != 0) {
6631b13d190SFrançois Tigeot 		DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
6641b13d190SFrançois Tigeot 		return -EINVAL;
6651b13d190SFrançois Tigeot 	} else {
6661b13d190SFrançois Tigeot 		if (args->DR4 == 0xffffffff) {
6671b13d190SFrançois Tigeot 			DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
6681b13d190SFrançois Tigeot 			args->DR4 = 0;
6691b13d190SFrançois Tigeot 		}
6701b13d190SFrançois Tigeot 
6711b13d190SFrançois Tigeot 		if (args->DR1 || args->DR4 || args->cliprects_ptr) {
6721b13d190SFrançois Tigeot 			DRM_DEBUG("0 cliprects but dirt in cliprects fields\n");
6731b13d190SFrançois Tigeot 			return -EINVAL;
6741b13d190SFrançois Tigeot 		}
6751b13d190SFrançois Tigeot 	}
6761b13d190SFrançois Tigeot 
6771b13d190SFrançois Tigeot 	if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
6781b13d190SFrançois Tigeot 		DRM_DEBUG("sol reset is gen7 only\n");
6791b13d190SFrançois Tigeot 		return -EINVAL;
6801b13d190SFrançois Tigeot 	}
6811b13d190SFrançois Tigeot 
682*2c9916cdSFrançois Tigeot 	ret = execlists_move_to_gpu(ringbuf, ctx, vmas);
6831b13d190SFrançois Tigeot 	if (ret)
6841b13d190SFrançois Tigeot 		return ret;
6851b13d190SFrançois Tigeot 
6861b13d190SFrançois Tigeot 	if (ring == &dev_priv->ring[RCS] &&
6871b13d190SFrançois Tigeot 	    instp_mode != dev_priv->relative_constants_mode) {
688*2c9916cdSFrançois Tigeot 		ret = intel_logical_ring_begin(ringbuf, ctx, 4);
6891b13d190SFrançois Tigeot 		if (ret)
6901b13d190SFrançois Tigeot 			return ret;
6911b13d190SFrançois Tigeot 
6921b13d190SFrançois Tigeot 		intel_logical_ring_emit(ringbuf, MI_NOOP);
6931b13d190SFrançois Tigeot 		intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(1));
6941b13d190SFrançois Tigeot 		intel_logical_ring_emit(ringbuf, INSTPM);
6951b13d190SFrançois Tigeot 		intel_logical_ring_emit(ringbuf, instp_mask << 16 | instp_mode);
6961b13d190SFrançois Tigeot 		intel_logical_ring_advance(ringbuf);
6971b13d190SFrançois Tigeot 
6981b13d190SFrançois Tigeot 		dev_priv->relative_constants_mode = instp_mode;
6991b13d190SFrançois Tigeot 	}
7001b13d190SFrançois Tigeot 
701*2c9916cdSFrançois Tigeot 	ret = ring->emit_bb_start(ringbuf, ctx, exec_start, flags);
7021b13d190SFrançois Tigeot 	if (ret)
7031b13d190SFrançois Tigeot 		return ret;
7041b13d190SFrançois Tigeot 
7051b13d190SFrançois Tigeot 	i915_gem_execbuffer_move_to_active(vmas, ring);
7061b13d190SFrançois Tigeot 	i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
7071b13d190SFrançois Tigeot 
7081b13d190SFrançois Tigeot 	return 0;
7091b13d190SFrançois Tigeot }
7101b13d190SFrançois Tigeot 
711*2c9916cdSFrançois Tigeot void intel_execlists_retire_requests(struct intel_engine_cs *ring)
712*2c9916cdSFrançois Tigeot {
713*2c9916cdSFrançois Tigeot 	struct drm_i915_gem_request *req, *tmp;
714*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
715*2c9916cdSFrançois Tigeot 	struct list_head retired_list;
716*2c9916cdSFrançois Tigeot 
717*2c9916cdSFrançois Tigeot 	WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
718*2c9916cdSFrançois Tigeot 	if (list_empty(&ring->execlist_retired_req_list))
719*2c9916cdSFrançois Tigeot 		return;
720*2c9916cdSFrançois Tigeot 
721*2c9916cdSFrançois Tigeot 	INIT_LIST_HEAD(&retired_list);
722*2c9916cdSFrançois Tigeot 	lockmgr(&ring->execlist_lock, LK_EXCLUSIVE);
723*2c9916cdSFrançois Tigeot 	list_replace_init(&ring->execlist_retired_req_list, &retired_list);
724*2c9916cdSFrançois Tigeot 	lockmgr(&ring->execlist_lock, LK_RELEASE);
725*2c9916cdSFrançois Tigeot 
726*2c9916cdSFrançois Tigeot 	list_for_each_entry_safe(req, tmp, &retired_list, execlist_link) {
727*2c9916cdSFrançois Tigeot 		struct intel_context *ctx = req->ctx;
728*2c9916cdSFrançois Tigeot 		struct drm_i915_gem_object *ctx_obj =
729*2c9916cdSFrançois Tigeot 				ctx->engine[ring->id].state;
730*2c9916cdSFrançois Tigeot 
731*2c9916cdSFrançois Tigeot 		if (ctx_obj && (ctx != ring->default_context))
732*2c9916cdSFrançois Tigeot 			intel_lr_context_unpin(ring, ctx);
733*2c9916cdSFrançois Tigeot 		intel_runtime_pm_put(dev_priv);
734*2c9916cdSFrançois Tigeot 		list_del(&req->execlist_link);
735*2c9916cdSFrançois Tigeot 		i915_gem_request_unreference(req);
736*2c9916cdSFrançois Tigeot 	}
737*2c9916cdSFrançois Tigeot }
738*2c9916cdSFrançois Tigeot 
7391b13d190SFrançois Tigeot void intel_logical_ring_stop(struct intel_engine_cs *ring)
7401b13d190SFrançois Tigeot {
7411b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
7421b13d190SFrançois Tigeot 	int ret;
7431b13d190SFrançois Tigeot 
7441b13d190SFrançois Tigeot 	if (!intel_ring_initialized(ring))
7451b13d190SFrançois Tigeot 		return;
7461b13d190SFrançois Tigeot 
7471b13d190SFrançois Tigeot 	ret = intel_ring_idle(ring);
7481b13d190SFrançois Tigeot 	if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
7491b13d190SFrançois Tigeot 		DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
7501b13d190SFrançois Tigeot 			  ring->name, ret);
7511b13d190SFrançois Tigeot 
7521b13d190SFrançois Tigeot 	/* TODO: Is this correct with Execlists enabled? */
7531b13d190SFrançois Tigeot 	I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
7541b13d190SFrançois Tigeot 	if (wait_for_atomic((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
7551b13d190SFrançois Tigeot 		DRM_ERROR("%s :timed out trying to stop ring\n", ring->name);
7561b13d190SFrançois Tigeot 		return;
7571b13d190SFrançois Tigeot 	}
7581b13d190SFrançois Tigeot 	I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
7591b13d190SFrançois Tigeot }
7601b13d190SFrançois Tigeot 
761*2c9916cdSFrançois Tigeot int logical_ring_flush_all_caches(struct intel_ringbuffer *ringbuf,
762*2c9916cdSFrançois Tigeot 				  struct intel_context *ctx)
7631b13d190SFrançois Tigeot {
7641b13d190SFrançois Tigeot 	struct intel_engine_cs *ring = ringbuf->ring;
7651b13d190SFrançois Tigeot 	int ret;
7661b13d190SFrançois Tigeot 
7671b13d190SFrançois Tigeot 	if (!ring->gpu_caches_dirty)
7681b13d190SFrançois Tigeot 		return 0;
7691b13d190SFrançois Tigeot 
770*2c9916cdSFrançois Tigeot 	ret = ring->emit_flush(ringbuf, ctx, 0, I915_GEM_GPU_DOMAINS);
7711b13d190SFrançois Tigeot 	if (ret)
7721b13d190SFrançois Tigeot 		return ret;
7731b13d190SFrançois Tigeot 
7741b13d190SFrançois Tigeot 	ring->gpu_caches_dirty = false;
7751b13d190SFrançois Tigeot 	return 0;
7761b13d190SFrançois Tigeot }
7771b13d190SFrançois Tigeot 
7781b13d190SFrançois Tigeot /**
7791b13d190SFrançois Tigeot  * intel_logical_ring_advance_and_submit() - advance the tail and submit the workload
7801b13d190SFrançois Tigeot  * @ringbuf: Logical Ringbuffer to advance.
7811b13d190SFrançois Tigeot  *
7821b13d190SFrançois Tigeot  * The tail is updated in our logical ringbuffer struct, not in the actual context. What
7831b13d190SFrançois Tigeot  * really happens during submission is that the context and current tail will be placed
7841b13d190SFrançois Tigeot  * on a queue waiting for the ELSP to be ready to accept a new context submission. At that
7851b13d190SFrançois Tigeot  * point, the tail *inside* the context is updated and the ELSP written to.
7861b13d190SFrançois Tigeot  */
787*2c9916cdSFrançois Tigeot void intel_logical_ring_advance_and_submit(struct intel_ringbuffer *ringbuf,
788*2c9916cdSFrançois Tigeot 					   struct intel_context *ctx,
789*2c9916cdSFrançois Tigeot 					   struct drm_i915_gem_request *request)
7901b13d190SFrançois Tigeot {
7911b13d190SFrançois Tigeot 	struct intel_engine_cs *ring = ringbuf->ring;
7921b13d190SFrançois Tigeot 
7931b13d190SFrançois Tigeot 	intel_logical_ring_advance(ringbuf);
7941b13d190SFrançois Tigeot 
7951b13d190SFrançois Tigeot 	if (intel_ring_stopped(ring))
7961b13d190SFrançois Tigeot 		return;
7971b13d190SFrançois Tigeot 
798*2c9916cdSFrançois Tigeot 	execlists_context_queue(ring, ctx, ringbuf->tail, request);
7991b13d190SFrançois Tigeot }
8001b13d190SFrançois Tigeot 
801*2c9916cdSFrançois Tigeot static int intel_lr_context_pin(struct intel_engine_cs *ring,
8021b13d190SFrançois Tigeot 		struct intel_context *ctx)
8031b13d190SFrançois Tigeot {
804*2c9916cdSFrançois Tigeot 	struct drm_i915_gem_object *ctx_obj = ctx->engine[ring->id].state;
805*2c9916cdSFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
806*2c9916cdSFrançois Tigeot 	int ret = 0;
807*2c9916cdSFrançois Tigeot 
808*2c9916cdSFrançois Tigeot 	WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
809*2c9916cdSFrançois Tigeot 	if (ctx->engine[ring->id].pin_count++ == 0) {
810*2c9916cdSFrançois Tigeot 		ret = i915_gem_obj_ggtt_pin(ctx_obj,
811*2c9916cdSFrançois Tigeot 				GEN8_LR_CONTEXT_ALIGN, 0);
812*2c9916cdSFrançois Tigeot 		if (ret)
813*2c9916cdSFrançois Tigeot 			goto reset_pin_count;
814*2c9916cdSFrançois Tigeot 
815*2c9916cdSFrançois Tigeot 		ret = intel_pin_and_map_ringbuffer_obj(ring->dev, ringbuf);
816*2c9916cdSFrançois Tigeot 		if (ret)
817*2c9916cdSFrançois Tigeot 			goto unpin_ctx_obj;
818*2c9916cdSFrançois Tigeot 	}
819*2c9916cdSFrançois Tigeot 
820*2c9916cdSFrançois Tigeot 	return ret;
821*2c9916cdSFrançois Tigeot 
822*2c9916cdSFrançois Tigeot unpin_ctx_obj:
823*2c9916cdSFrançois Tigeot 	i915_gem_object_ggtt_unpin(ctx_obj);
824*2c9916cdSFrançois Tigeot reset_pin_count:
825*2c9916cdSFrançois Tigeot 	ctx->engine[ring->id].pin_count = 0;
826*2c9916cdSFrançois Tigeot 
827*2c9916cdSFrançois Tigeot 	return ret;
828*2c9916cdSFrançois Tigeot }
829*2c9916cdSFrançois Tigeot 
830*2c9916cdSFrançois Tigeot void intel_lr_context_unpin(struct intel_engine_cs *ring,
831*2c9916cdSFrançois Tigeot 		struct intel_context *ctx)
832*2c9916cdSFrançois Tigeot {
833*2c9916cdSFrançois Tigeot 	struct drm_i915_gem_object *ctx_obj = ctx->engine[ring->id].state;
834*2c9916cdSFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
835*2c9916cdSFrançois Tigeot 
836*2c9916cdSFrançois Tigeot 	if (ctx_obj) {
837*2c9916cdSFrançois Tigeot 		WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
838*2c9916cdSFrançois Tigeot 		if (--ctx->engine[ring->id].pin_count == 0) {
839*2c9916cdSFrançois Tigeot 			intel_unpin_ringbuffer_obj(ringbuf);
840*2c9916cdSFrançois Tigeot 			i915_gem_object_ggtt_unpin(ctx_obj);
841*2c9916cdSFrançois Tigeot 		}
842*2c9916cdSFrançois Tigeot 	}
843*2c9916cdSFrançois Tigeot }
844*2c9916cdSFrançois Tigeot 
845*2c9916cdSFrançois Tigeot static int logical_ring_alloc_request(struct intel_engine_cs *ring,
846*2c9916cdSFrançois Tigeot 				      struct intel_context *ctx)
847*2c9916cdSFrançois Tigeot {
848*2c9916cdSFrançois Tigeot 	struct drm_i915_gem_request *request;
849*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_private = ring->dev->dev_private;
850*2c9916cdSFrançois Tigeot 	int ret;
851*2c9916cdSFrançois Tigeot 
852*2c9916cdSFrançois Tigeot 	if (ring->outstanding_lazy_request)
8531b13d190SFrançois Tigeot 		return 0;
8541b13d190SFrançois Tigeot 
855*2c9916cdSFrançois Tigeot 	request = kzalloc(sizeof(*request), GFP_KERNEL);
8561b13d190SFrançois Tigeot 	if (request == NULL)
8571b13d190SFrançois Tigeot 		return -ENOMEM;
8581b13d190SFrançois Tigeot 
859*2c9916cdSFrançois Tigeot 	if (ctx != ring->default_context) {
860*2c9916cdSFrançois Tigeot 		ret = intel_lr_context_pin(ring, ctx);
861*2c9916cdSFrançois Tigeot 		if (ret) {
862*2c9916cdSFrançois Tigeot 			kfree(request);
863*2c9916cdSFrançois Tigeot 			return ret;
864*2c9916cdSFrançois Tigeot 		}
865*2c9916cdSFrançois Tigeot 	}
866*2c9916cdSFrançois Tigeot 
867*2c9916cdSFrançois Tigeot 	kref_init(&request->ref);
868*2c9916cdSFrançois Tigeot 	request->ring = ring;
869*2c9916cdSFrançois Tigeot 	request->uniq = dev_private->request_uniq++;
870*2c9916cdSFrançois Tigeot 
871*2c9916cdSFrançois Tigeot 	ret = i915_gem_get_seqno(ring->dev, &request->seqno);
872*2c9916cdSFrançois Tigeot 	if (ret) {
873*2c9916cdSFrançois Tigeot 		intel_lr_context_unpin(ring, ctx);
874*2c9916cdSFrançois Tigeot 		kfree(request);
875*2c9916cdSFrançois Tigeot 		return ret;
876*2c9916cdSFrançois Tigeot 	}
877*2c9916cdSFrançois Tigeot 
8781b13d190SFrançois Tigeot 	/* Hold a reference to the context this request belongs to
8791b13d190SFrançois Tigeot 	 * (we will need it when the time comes to emit/retire the
8801b13d190SFrançois Tigeot 	 * request).
8811b13d190SFrançois Tigeot 	 */
8821b13d190SFrançois Tigeot 	request->ctx = ctx;
8831b13d190SFrançois Tigeot 	i915_gem_context_reference(request->ctx);
8841b13d190SFrançois Tigeot 
885*2c9916cdSFrançois Tigeot 	ring->outstanding_lazy_request = request;
886*2c9916cdSFrançois Tigeot 	return 0;
8871b13d190SFrançois Tigeot }
8881b13d190SFrançois Tigeot 
8891b13d190SFrançois Tigeot static int logical_ring_wait_request(struct intel_ringbuffer *ringbuf,
8901b13d190SFrançois Tigeot 				     int bytes)
8911b13d190SFrançois Tigeot {
8921b13d190SFrançois Tigeot 	struct intel_engine_cs *ring = ringbuf->ring;
8931b13d190SFrançois Tigeot 	struct drm_i915_gem_request *request;
8941b13d190SFrançois Tigeot 	int ret;
8951b13d190SFrançois Tigeot 
896*2c9916cdSFrançois Tigeot 	if (intel_ring_space(ringbuf) >= bytes)
8971b13d190SFrançois Tigeot 		return 0;
8981b13d190SFrançois Tigeot 
8991b13d190SFrançois Tigeot 	list_for_each_entry(request, &ring->request_list, list) {
900*2c9916cdSFrançois Tigeot 		/*
901*2c9916cdSFrançois Tigeot 		 * The request queue is per-engine, so can contain requests
902*2c9916cdSFrançois Tigeot 		 * from multiple ringbuffers. Here, we must ignore any that
903*2c9916cdSFrançois Tigeot 		 * aren't from the ringbuffer we're considering.
904*2c9916cdSFrançois Tigeot 		 */
905*2c9916cdSFrançois Tigeot 		struct intel_context *ctx = request->ctx;
906*2c9916cdSFrançois Tigeot 		if (ctx->engine[ring->id].ringbuf != ringbuf)
907*2c9916cdSFrançois Tigeot 			continue;
908*2c9916cdSFrançois Tigeot 
909*2c9916cdSFrançois Tigeot 		/* Would completion of this request free enough space? */
9101b13d190SFrançois Tigeot 		if (__intel_ring_space(request->tail, ringbuf->tail,
9111b13d190SFrançois Tigeot 				       ringbuf->size) >= bytes) {
9121b13d190SFrançois Tigeot 			break;
9131b13d190SFrançois Tigeot 		}
9141b13d190SFrançois Tigeot 	}
9151b13d190SFrançois Tigeot 
916*2c9916cdSFrançois Tigeot 	if (&request->list == &ring->request_list)
9171b13d190SFrançois Tigeot 		return -ENOSPC;
9181b13d190SFrançois Tigeot 
919*2c9916cdSFrançois Tigeot 	ret = i915_wait_request(request);
9201b13d190SFrançois Tigeot 	if (ret)
9211b13d190SFrançois Tigeot 		return ret;
9221b13d190SFrançois Tigeot 
9231b13d190SFrançois Tigeot 	i915_gem_retire_requests_ring(ring);
9241b13d190SFrançois Tigeot 
925*2c9916cdSFrançois Tigeot 	return intel_ring_space(ringbuf) >= bytes ? 0 : -ENOSPC;
9261b13d190SFrançois Tigeot }
9271b13d190SFrançois Tigeot 
9281b13d190SFrançois Tigeot static int logical_ring_wait_for_space(struct intel_ringbuffer *ringbuf,
929*2c9916cdSFrançois Tigeot 				       struct intel_context *ctx,
9301b13d190SFrançois Tigeot 				       int bytes)
9311b13d190SFrançois Tigeot {
9321b13d190SFrançois Tigeot 	struct intel_engine_cs *ring = ringbuf->ring;
9331b13d190SFrançois Tigeot 	struct drm_device *dev = ring->dev;
9341b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
9351b13d190SFrançois Tigeot 	unsigned long end;
9361b13d190SFrançois Tigeot 	int ret;
9371b13d190SFrançois Tigeot 
9381b13d190SFrançois Tigeot 	ret = logical_ring_wait_request(ringbuf, bytes);
9391b13d190SFrançois Tigeot 	if (ret != -ENOSPC)
9401b13d190SFrançois Tigeot 		return ret;
9411b13d190SFrançois Tigeot 
9421b13d190SFrançois Tigeot 	/* Force the context submission in case we have been skipping it */
943*2c9916cdSFrançois Tigeot 	intel_logical_ring_advance_and_submit(ringbuf, ctx, NULL);
9441b13d190SFrançois Tigeot 
9451b13d190SFrançois Tigeot 	/* With GEM the hangcheck timer should kick us out of the loop,
9461b13d190SFrançois Tigeot 	 * leaving it early runs the risk of corrupting GEM state (due
9471b13d190SFrançois Tigeot 	 * to running on almost untested codepaths). But on resume
9481b13d190SFrançois Tigeot 	 * timers don't work yet, so prevent a complete hang in that
9491b13d190SFrançois Tigeot 	 * case by choosing an insanely large timeout. */
9501b13d190SFrançois Tigeot 	end = jiffies + 60 * HZ;
9511b13d190SFrançois Tigeot 
9521b13d190SFrançois Tigeot 	ret = 0;
953*2c9916cdSFrançois Tigeot 	do {
954*2c9916cdSFrançois Tigeot 		if (intel_ring_space(ringbuf) >= bytes)
9551b13d190SFrançois Tigeot 			break;
9561b13d190SFrançois Tigeot 
9571b13d190SFrançois Tigeot 		msleep(1);
9581b13d190SFrançois Tigeot 
959b42320c2SFrançois Tigeot 		if (dev_priv->mm.interruptible && signal_pending(curthread->td_lwp)) {
9601b13d190SFrançois Tigeot 			ret = -ERESTARTSYS;
9611b13d190SFrançois Tigeot 			break;
9621b13d190SFrançois Tigeot 		}
9631b13d190SFrançois Tigeot 
9641b13d190SFrançois Tigeot 		ret = i915_gem_check_wedge(&dev_priv->gpu_error,
9651b13d190SFrançois Tigeot 					   dev_priv->mm.interruptible);
9661b13d190SFrançois Tigeot 		if (ret)
9671b13d190SFrançois Tigeot 			break;
9681b13d190SFrançois Tigeot 
9691b13d190SFrançois Tigeot 		if (time_after(jiffies, end)) {
9701b13d190SFrançois Tigeot 			ret = -EBUSY;
9711b13d190SFrançois Tigeot 			break;
9721b13d190SFrançois Tigeot 		}
9731b13d190SFrançois Tigeot 	} while (1);
9741b13d190SFrançois Tigeot 
9751b13d190SFrançois Tigeot 	return ret;
9761b13d190SFrançois Tigeot }
9771b13d190SFrançois Tigeot 
978*2c9916cdSFrançois Tigeot static int logical_ring_wrap_buffer(struct intel_ringbuffer *ringbuf,
979*2c9916cdSFrançois Tigeot 				    struct intel_context *ctx)
9801b13d190SFrançois Tigeot {
9811b13d190SFrançois Tigeot 	uint32_t __iomem *virt;
9821b13d190SFrançois Tigeot 	int rem = ringbuf->size - ringbuf->tail;
9831b13d190SFrançois Tigeot 
9841b13d190SFrançois Tigeot 	if (ringbuf->space < rem) {
985*2c9916cdSFrançois Tigeot 		int ret = logical_ring_wait_for_space(ringbuf, ctx, rem);
9861b13d190SFrançois Tigeot 
9871b13d190SFrançois Tigeot 		if (ret)
9881b13d190SFrançois Tigeot 			return ret;
9891b13d190SFrançois Tigeot 	}
9901b13d190SFrançois Tigeot 
9911b13d190SFrançois Tigeot 	virt = (unsigned int *)((char *)ringbuf->virtual_start + ringbuf->tail);
9921b13d190SFrançois Tigeot 	rem /= 4;
9931b13d190SFrançois Tigeot 	while (rem--)
9941b13d190SFrançois Tigeot 		iowrite32(MI_NOOP, virt++);
9951b13d190SFrançois Tigeot 
9961b13d190SFrançois Tigeot 	ringbuf->tail = 0;
997*2c9916cdSFrançois Tigeot 	intel_ring_update_space(ringbuf);
9981b13d190SFrançois Tigeot 
9991b13d190SFrançois Tigeot 	return 0;
10001b13d190SFrançois Tigeot }
10011b13d190SFrançois Tigeot 
1002*2c9916cdSFrançois Tigeot static int logical_ring_prepare(struct intel_ringbuffer *ringbuf,
1003*2c9916cdSFrançois Tigeot 				struct intel_context *ctx, int bytes)
10041b13d190SFrançois Tigeot {
10051b13d190SFrançois Tigeot 	int ret;
10061b13d190SFrançois Tigeot 
10071b13d190SFrançois Tigeot 	if (unlikely(ringbuf->tail + bytes > ringbuf->effective_size)) {
1008*2c9916cdSFrançois Tigeot 		ret = logical_ring_wrap_buffer(ringbuf, ctx);
10091b13d190SFrançois Tigeot 		if (unlikely(ret))
10101b13d190SFrançois Tigeot 			return ret;
10111b13d190SFrançois Tigeot 	}
10121b13d190SFrançois Tigeot 
10131b13d190SFrançois Tigeot 	if (unlikely(ringbuf->space < bytes)) {
1014*2c9916cdSFrançois Tigeot 		ret = logical_ring_wait_for_space(ringbuf, ctx, bytes);
10151b13d190SFrançois Tigeot 		if (unlikely(ret))
10161b13d190SFrançois Tigeot 			return ret;
10171b13d190SFrançois Tigeot 	}
10181b13d190SFrançois Tigeot 
10191b13d190SFrançois Tigeot 	return 0;
10201b13d190SFrançois Tigeot }
10211b13d190SFrançois Tigeot 
10221b13d190SFrançois Tigeot /**
10231b13d190SFrançois Tigeot  * intel_logical_ring_begin() - prepare the logical ringbuffer to accept some commands
10241b13d190SFrançois Tigeot  *
10251b13d190SFrançois Tigeot  * @ringbuf: Logical ringbuffer.
10261b13d190SFrançois Tigeot  * @num_dwords: number of DWORDs that we plan to write to the ringbuffer.
10271b13d190SFrançois Tigeot  *
10281b13d190SFrançois Tigeot  * The ringbuffer might not be ready to accept the commands right away (maybe it needs to
10291b13d190SFrançois Tigeot  * be wrapped, or wait a bit for the tail to be updated). This function takes care of that
10301b13d190SFrançois Tigeot  * and also preallocates a request (every workload submission is still mediated through
10311b13d190SFrançois Tigeot  * requests, same as it did with legacy ringbuffer submission).
10321b13d190SFrançois Tigeot  *
10331b13d190SFrançois Tigeot  * Return: non-zero if the ringbuffer is not ready to be written to.
10341b13d190SFrançois Tigeot  */
1035*2c9916cdSFrançois Tigeot int intel_logical_ring_begin(struct intel_ringbuffer *ringbuf,
1036*2c9916cdSFrançois Tigeot 			     struct intel_context *ctx, int num_dwords)
10371b13d190SFrançois Tigeot {
10381b13d190SFrançois Tigeot 	struct intel_engine_cs *ring = ringbuf->ring;
10391b13d190SFrançois Tigeot 	struct drm_device *dev = ring->dev;
10401b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
10411b13d190SFrançois Tigeot 	int ret;
10421b13d190SFrançois Tigeot 
10431b13d190SFrançois Tigeot 	ret = i915_gem_check_wedge(&dev_priv->gpu_error,
10441b13d190SFrançois Tigeot 				   dev_priv->mm.interruptible);
10451b13d190SFrançois Tigeot 	if (ret)
10461b13d190SFrançois Tigeot 		return ret;
10471b13d190SFrançois Tigeot 
1048*2c9916cdSFrançois Tigeot 	ret = logical_ring_prepare(ringbuf, ctx, num_dwords * sizeof(uint32_t));
10491b13d190SFrançois Tigeot 	if (ret)
10501b13d190SFrançois Tigeot 		return ret;
10511b13d190SFrançois Tigeot 
10521b13d190SFrançois Tigeot 	/* Preallocate the olr before touching the ring */
1053*2c9916cdSFrançois Tigeot 	ret = logical_ring_alloc_request(ring, ctx);
10541b13d190SFrançois Tigeot 	if (ret)
10551b13d190SFrançois Tigeot 		return ret;
10561b13d190SFrançois Tigeot 
10571b13d190SFrançois Tigeot 	ringbuf->space -= num_dwords * sizeof(uint32_t);
10581b13d190SFrançois Tigeot 	return 0;
10591b13d190SFrançois Tigeot }
10601b13d190SFrançois Tigeot 
1061*2c9916cdSFrançois Tigeot static int intel_logical_ring_workarounds_emit(struct intel_engine_cs *ring,
1062*2c9916cdSFrançois Tigeot 					       struct intel_context *ctx)
1063*2c9916cdSFrançois Tigeot {
1064*2c9916cdSFrançois Tigeot 	int ret, i;
1065*2c9916cdSFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
1066*2c9916cdSFrançois Tigeot 	struct drm_device *dev = ring->dev;
1067*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1068*2c9916cdSFrançois Tigeot 	struct i915_workarounds *w = &dev_priv->workarounds;
1069*2c9916cdSFrançois Tigeot 
1070*2c9916cdSFrançois Tigeot 	if (WARN_ON_ONCE(w->count == 0))
1071*2c9916cdSFrançois Tigeot 		return 0;
1072*2c9916cdSFrançois Tigeot 
1073*2c9916cdSFrançois Tigeot 	ring->gpu_caches_dirty = true;
1074*2c9916cdSFrançois Tigeot 	ret = logical_ring_flush_all_caches(ringbuf, ctx);
1075*2c9916cdSFrançois Tigeot 	if (ret)
1076*2c9916cdSFrançois Tigeot 		return ret;
1077*2c9916cdSFrançois Tigeot 
1078*2c9916cdSFrançois Tigeot 	ret = intel_logical_ring_begin(ringbuf, ctx, w->count * 2 + 2);
1079*2c9916cdSFrançois Tigeot 	if (ret)
1080*2c9916cdSFrançois Tigeot 		return ret;
1081*2c9916cdSFrançois Tigeot 
1082*2c9916cdSFrançois Tigeot 	intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(w->count));
1083*2c9916cdSFrançois Tigeot 	for (i = 0; i < w->count; i++) {
1084*2c9916cdSFrançois Tigeot 		intel_logical_ring_emit(ringbuf, w->reg[i].addr);
1085*2c9916cdSFrançois Tigeot 		intel_logical_ring_emit(ringbuf, w->reg[i].value);
1086*2c9916cdSFrançois Tigeot 	}
1087*2c9916cdSFrançois Tigeot 	intel_logical_ring_emit(ringbuf, MI_NOOP);
1088*2c9916cdSFrançois Tigeot 
1089*2c9916cdSFrançois Tigeot 	intel_logical_ring_advance(ringbuf);
1090*2c9916cdSFrançois Tigeot 
1091*2c9916cdSFrançois Tigeot 	ring->gpu_caches_dirty = true;
1092*2c9916cdSFrançois Tigeot 	ret = logical_ring_flush_all_caches(ringbuf, ctx);
1093*2c9916cdSFrançois Tigeot 	if (ret)
1094*2c9916cdSFrançois Tigeot 		return ret;
1095*2c9916cdSFrançois Tigeot 
1096*2c9916cdSFrançois Tigeot 	return 0;
1097*2c9916cdSFrançois Tigeot }
1098*2c9916cdSFrançois Tigeot 
10991b13d190SFrançois Tigeot static int gen8_init_common_ring(struct intel_engine_cs *ring)
11001b13d190SFrançois Tigeot {
11011b13d190SFrançois Tigeot 	struct drm_device *dev = ring->dev;
11021b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
11031b13d190SFrançois Tigeot 
11041b13d190SFrançois Tigeot 	I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
11051b13d190SFrançois Tigeot 	I915_WRITE(RING_HWSTAM(ring->mmio_base), 0xffffffff);
11061b13d190SFrançois Tigeot 
11071b13d190SFrançois Tigeot 	I915_WRITE(RING_MODE_GEN7(ring),
11081b13d190SFrançois Tigeot 		   _MASKED_BIT_DISABLE(GFX_REPLAY_MODE) |
11091b13d190SFrançois Tigeot 		   _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
11101b13d190SFrançois Tigeot 	POSTING_READ(RING_MODE_GEN7(ring));
1111*2c9916cdSFrançois Tigeot 	ring->next_context_status_buffer = 0;
11121b13d190SFrançois Tigeot 	DRM_DEBUG_DRIVER("Execlists enabled for %s\n", ring->name);
11131b13d190SFrançois Tigeot 
11141b13d190SFrançois Tigeot 	memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
11151b13d190SFrançois Tigeot 
11161b13d190SFrançois Tigeot 	return 0;
11171b13d190SFrançois Tigeot }
11181b13d190SFrançois Tigeot 
11191b13d190SFrançois Tigeot static int gen8_init_render_ring(struct intel_engine_cs *ring)
11201b13d190SFrançois Tigeot {
11211b13d190SFrançois Tigeot 	struct drm_device *dev = ring->dev;
11221b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
11231b13d190SFrançois Tigeot 	int ret;
11241b13d190SFrançois Tigeot 
11251b13d190SFrançois Tigeot 	ret = gen8_init_common_ring(ring);
11261b13d190SFrançois Tigeot 	if (ret)
11271b13d190SFrançois Tigeot 		return ret;
11281b13d190SFrançois Tigeot 
11291b13d190SFrançois Tigeot 	/* We need to disable the AsyncFlip performance optimisations in order
11301b13d190SFrançois Tigeot 	 * to use MI_WAIT_FOR_EVENT within the CS. It should already be
11311b13d190SFrançois Tigeot 	 * programmed to '1' on all products.
11321b13d190SFrançois Tigeot 	 *
11331b13d190SFrançois Tigeot 	 * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
11341b13d190SFrançois Tigeot 	 */
11351b13d190SFrançois Tigeot 	I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
11361b13d190SFrançois Tigeot 
11371b13d190SFrançois Tigeot 	I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
11381b13d190SFrançois Tigeot 
1139*2c9916cdSFrançois Tigeot 	return init_workarounds_ring(ring);
11401b13d190SFrançois Tigeot }
11411b13d190SFrançois Tigeot 
11421b13d190SFrançois Tigeot static int gen8_emit_bb_start(struct intel_ringbuffer *ringbuf,
1143*2c9916cdSFrançois Tigeot 			      struct intel_context *ctx,
11441b13d190SFrançois Tigeot 			      u64 offset, unsigned flags)
11451b13d190SFrançois Tigeot {
11461b13d190SFrançois Tigeot 	bool ppgtt = !(flags & I915_DISPATCH_SECURE);
11471b13d190SFrançois Tigeot 	int ret;
11481b13d190SFrançois Tigeot 
1149*2c9916cdSFrançois Tigeot 	ret = intel_logical_ring_begin(ringbuf, ctx, 4);
11501b13d190SFrançois Tigeot 	if (ret)
11511b13d190SFrançois Tigeot 		return ret;
11521b13d190SFrançois Tigeot 
11531b13d190SFrançois Tigeot 	/* FIXME(BDW): Address space and security selectors. */
11541b13d190SFrançois Tigeot 	intel_logical_ring_emit(ringbuf, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8));
11551b13d190SFrançois Tigeot 	intel_logical_ring_emit(ringbuf, lower_32_bits(offset));
11561b13d190SFrançois Tigeot 	intel_logical_ring_emit(ringbuf, upper_32_bits(offset));
11571b13d190SFrançois Tigeot 	intel_logical_ring_emit(ringbuf, MI_NOOP);
11581b13d190SFrançois Tigeot 	intel_logical_ring_advance(ringbuf);
11591b13d190SFrançois Tigeot 
11601b13d190SFrançois Tigeot 	return 0;
11611b13d190SFrançois Tigeot }
11621b13d190SFrançois Tigeot 
11631b13d190SFrançois Tigeot static bool gen8_logical_ring_get_irq(struct intel_engine_cs *ring)
11641b13d190SFrançois Tigeot {
11651b13d190SFrançois Tigeot 	struct drm_device *dev = ring->dev;
11661b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
11671b13d190SFrançois Tigeot 
1168*2c9916cdSFrançois Tigeot 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
11691b13d190SFrançois Tigeot 		return false;
11701b13d190SFrançois Tigeot 
11711b13d190SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
11721b13d190SFrançois Tigeot 	if (ring->irq_refcount++ == 0) {
11731b13d190SFrançois Tigeot 		I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
11741b13d190SFrançois Tigeot 		POSTING_READ(RING_IMR(ring->mmio_base));
11751b13d190SFrançois Tigeot 	}
11761b13d190SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
11771b13d190SFrançois Tigeot 
11781b13d190SFrançois Tigeot 	return true;
11791b13d190SFrançois Tigeot }
11801b13d190SFrançois Tigeot 
11811b13d190SFrançois Tigeot static void gen8_logical_ring_put_irq(struct intel_engine_cs *ring)
11821b13d190SFrançois Tigeot {
11831b13d190SFrançois Tigeot 	struct drm_device *dev = ring->dev;
11841b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
11851b13d190SFrançois Tigeot 
11861b13d190SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_EXCLUSIVE);
11871b13d190SFrançois Tigeot 	if (--ring->irq_refcount == 0) {
11881b13d190SFrançois Tigeot 		I915_WRITE_IMR(ring, ~ring->irq_keep_mask);
11891b13d190SFrançois Tigeot 		POSTING_READ(RING_IMR(ring->mmio_base));
11901b13d190SFrançois Tigeot 	}
11911b13d190SFrançois Tigeot 	lockmgr(&dev_priv->irq_lock, LK_RELEASE);
11921b13d190SFrançois Tigeot }
11931b13d190SFrançois Tigeot 
11941b13d190SFrançois Tigeot static int gen8_emit_flush(struct intel_ringbuffer *ringbuf,
1195*2c9916cdSFrançois Tigeot 			   struct intel_context *ctx,
11961b13d190SFrançois Tigeot 			   u32 invalidate_domains,
11971b13d190SFrançois Tigeot 			   u32 unused)
11981b13d190SFrançois Tigeot {
11991b13d190SFrançois Tigeot 	struct intel_engine_cs *ring = ringbuf->ring;
12001b13d190SFrançois Tigeot 	struct drm_device *dev = ring->dev;
12011b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
12021b13d190SFrançois Tigeot 	uint32_t cmd;
12031b13d190SFrançois Tigeot 	int ret;
12041b13d190SFrançois Tigeot 
1205*2c9916cdSFrançois Tigeot 	ret = intel_logical_ring_begin(ringbuf, ctx, 4);
12061b13d190SFrançois Tigeot 	if (ret)
12071b13d190SFrançois Tigeot 		return ret;
12081b13d190SFrançois Tigeot 
12091b13d190SFrançois Tigeot 	cmd = MI_FLUSH_DW + 1;
12101b13d190SFrançois Tigeot 
1211*2c9916cdSFrançois Tigeot 	/* We always require a command barrier so that subsequent
1212*2c9916cdSFrançois Tigeot 	 * commands, such as breadcrumb interrupts, are strictly ordered
1213*2c9916cdSFrançois Tigeot 	 * wrt the contents of the write cache being flushed to memory
1214*2c9916cdSFrançois Tigeot 	 * (and thus being coherent from the CPU).
1215*2c9916cdSFrançois Tigeot 	 */
1216*2c9916cdSFrançois Tigeot 	cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
1217*2c9916cdSFrançois Tigeot 
1218*2c9916cdSFrançois Tigeot 	if (invalidate_domains & I915_GEM_GPU_DOMAINS) {
1219*2c9916cdSFrançois Tigeot 		cmd |= MI_INVALIDATE_TLB;
1220*2c9916cdSFrançois Tigeot 		if (ring == &dev_priv->ring[VCS])
1221*2c9916cdSFrançois Tigeot 			cmd |= MI_INVALIDATE_BSD;
12221b13d190SFrançois Tigeot 	}
12231b13d190SFrançois Tigeot 
12241b13d190SFrançois Tigeot 	intel_logical_ring_emit(ringbuf, cmd);
12251b13d190SFrançois Tigeot 	intel_logical_ring_emit(ringbuf,
12261b13d190SFrançois Tigeot 				I915_GEM_HWS_SCRATCH_ADDR |
12271b13d190SFrançois Tigeot 				MI_FLUSH_DW_USE_GTT);
12281b13d190SFrançois Tigeot 	intel_logical_ring_emit(ringbuf, 0); /* upper addr */
12291b13d190SFrançois Tigeot 	intel_logical_ring_emit(ringbuf, 0); /* value */
12301b13d190SFrançois Tigeot 	intel_logical_ring_advance(ringbuf);
12311b13d190SFrançois Tigeot 
12321b13d190SFrançois Tigeot 	return 0;
12331b13d190SFrançois Tigeot }
12341b13d190SFrançois Tigeot 
12351b13d190SFrançois Tigeot static int gen8_emit_flush_render(struct intel_ringbuffer *ringbuf,
1236*2c9916cdSFrançois Tigeot 				  struct intel_context *ctx,
12371b13d190SFrançois Tigeot 				  u32 invalidate_domains,
12381b13d190SFrançois Tigeot 				  u32 flush_domains)
12391b13d190SFrançois Tigeot {
12401b13d190SFrançois Tigeot 	struct intel_engine_cs *ring = ringbuf->ring;
12411b13d190SFrançois Tigeot 	u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
12421b13d190SFrançois Tigeot 	u32 flags = 0;
12431b13d190SFrançois Tigeot 	int ret;
12441b13d190SFrançois Tigeot 
12451b13d190SFrançois Tigeot 	flags |= PIPE_CONTROL_CS_STALL;
12461b13d190SFrançois Tigeot 
12471b13d190SFrançois Tigeot 	if (flush_domains) {
12481b13d190SFrançois Tigeot 		flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
12491b13d190SFrançois Tigeot 		flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
12501b13d190SFrançois Tigeot 	}
12511b13d190SFrançois Tigeot 
12521b13d190SFrançois Tigeot 	if (invalidate_domains) {
12531b13d190SFrançois Tigeot 		flags |= PIPE_CONTROL_TLB_INVALIDATE;
12541b13d190SFrançois Tigeot 		flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
12551b13d190SFrançois Tigeot 		flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
12561b13d190SFrançois Tigeot 		flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
12571b13d190SFrançois Tigeot 		flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
12581b13d190SFrançois Tigeot 		flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
12591b13d190SFrançois Tigeot 		flags |= PIPE_CONTROL_QW_WRITE;
12601b13d190SFrançois Tigeot 		flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
12611b13d190SFrançois Tigeot 	}
12621b13d190SFrançois Tigeot 
1263*2c9916cdSFrançois Tigeot 	ret = intel_logical_ring_begin(ringbuf, ctx, 6);
12641b13d190SFrançois Tigeot 	if (ret)
12651b13d190SFrançois Tigeot 		return ret;
12661b13d190SFrançois Tigeot 
12671b13d190SFrançois Tigeot 	intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
12681b13d190SFrançois Tigeot 	intel_logical_ring_emit(ringbuf, flags);
12691b13d190SFrançois Tigeot 	intel_logical_ring_emit(ringbuf, scratch_addr);
12701b13d190SFrançois Tigeot 	intel_logical_ring_emit(ringbuf, 0);
12711b13d190SFrançois Tigeot 	intel_logical_ring_emit(ringbuf, 0);
12721b13d190SFrançois Tigeot 	intel_logical_ring_emit(ringbuf, 0);
12731b13d190SFrançois Tigeot 	intel_logical_ring_advance(ringbuf);
12741b13d190SFrançois Tigeot 
12751b13d190SFrançois Tigeot 	return 0;
12761b13d190SFrançois Tigeot }
12771b13d190SFrançois Tigeot 
12781b13d190SFrançois Tigeot static u32 gen8_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
12791b13d190SFrançois Tigeot {
12801b13d190SFrançois Tigeot 	return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
12811b13d190SFrançois Tigeot }
12821b13d190SFrançois Tigeot 
12831b13d190SFrançois Tigeot static void gen8_set_seqno(struct intel_engine_cs *ring, u32 seqno)
12841b13d190SFrançois Tigeot {
12851b13d190SFrançois Tigeot 	intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
12861b13d190SFrançois Tigeot }
12871b13d190SFrançois Tigeot 
1288*2c9916cdSFrançois Tigeot static int gen8_emit_request(struct intel_ringbuffer *ringbuf,
1289*2c9916cdSFrançois Tigeot 			     struct drm_i915_gem_request *request)
12901b13d190SFrançois Tigeot {
12911b13d190SFrançois Tigeot 	struct intel_engine_cs *ring = ringbuf->ring;
12921b13d190SFrançois Tigeot 	u32 cmd;
12931b13d190SFrançois Tigeot 	int ret;
12941b13d190SFrançois Tigeot 
1295*2c9916cdSFrançois Tigeot 	ret = intel_logical_ring_begin(ringbuf, request->ctx, 6);
12961b13d190SFrançois Tigeot 	if (ret)
12971b13d190SFrançois Tigeot 		return ret;
12981b13d190SFrançois Tigeot 
1299*2c9916cdSFrançois Tigeot 	cmd = MI_STORE_DWORD_IMM_GEN4;
13001b13d190SFrançois Tigeot 	cmd |= MI_GLOBAL_GTT;
13011b13d190SFrançois Tigeot 
13021b13d190SFrançois Tigeot 	intel_logical_ring_emit(ringbuf, cmd);
13031b13d190SFrançois Tigeot 	intel_logical_ring_emit(ringbuf,
13041b13d190SFrançois Tigeot 				(ring->status_page.gfx_addr +
13051b13d190SFrançois Tigeot 				(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT)));
13061b13d190SFrançois Tigeot 	intel_logical_ring_emit(ringbuf, 0);
1307*2c9916cdSFrançois Tigeot 	intel_logical_ring_emit(ringbuf,
1308*2c9916cdSFrançois Tigeot 		i915_gem_request_get_seqno(ring->outstanding_lazy_request));
13091b13d190SFrançois Tigeot 	intel_logical_ring_emit(ringbuf, MI_USER_INTERRUPT);
13101b13d190SFrançois Tigeot 	intel_logical_ring_emit(ringbuf, MI_NOOP);
1311*2c9916cdSFrançois Tigeot 	intel_logical_ring_advance_and_submit(ringbuf, request->ctx, request);
13121b13d190SFrançois Tigeot 
13131b13d190SFrançois Tigeot 	return 0;
13141b13d190SFrançois Tigeot }
13151b13d190SFrançois Tigeot 
1316*2c9916cdSFrançois Tigeot static int gen8_init_rcs_context(struct intel_engine_cs *ring,
1317*2c9916cdSFrançois Tigeot 		       struct intel_context *ctx)
1318*2c9916cdSFrançois Tigeot {
1319*2c9916cdSFrançois Tigeot 	int ret;
1320*2c9916cdSFrançois Tigeot 
1321*2c9916cdSFrançois Tigeot 	ret = intel_logical_ring_workarounds_emit(ring, ctx);
1322*2c9916cdSFrançois Tigeot 	if (ret)
1323*2c9916cdSFrançois Tigeot 		return ret;
1324*2c9916cdSFrançois Tigeot 
1325*2c9916cdSFrançois Tigeot 	return intel_lr_context_render_state_init(ring, ctx);
1326*2c9916cdSFrançois Tigeot }
1327*2c9916cdSFrançois Tigeot 
13281b13d190SFrançois Tigeot /**
13291b13d190SFrançois Tigeot  * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
13301b13d190SFrançois Tigeot  *
13311b13d190SFrançois Tigeot  * @ring: Engine Command Streamer.
13321b13d190SFrançois Tigeot  *
13331b13d190SFrançois Tigeot  */
13341b13d190SFrançois Tigeot void intel_logical_ring_cleanup(struct intel_engine_cs *ring)
13351b13d190SFrançois Tigeot {
1336*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv;
13371b13d190SFrançois Tigeot 
13381b13d190SFrançois Tigeot 	if (!intel_ring_initialized(ring))
13391b13d190SFrançois Tigeot 		return;
13401b13d190SFrançois Tigeot 
1341*2c9916cdSFrançois Tigeot 	dev_priv = ring->dev->dev_private;
1342*2c9916cdSFrançois Tigeot 
13431b13d190SFrançois Tigeot 	intel_logical_ring_stop(ring);
13441b13d190SFrançois Tigeot 	WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
1345*2c9916cdSFrançois Tigeot 	i915_gem_request_assign(&ring->outstanding_lazy_request, NULL);
13461b13d190SFrançois Tigeot 
13471b13d190SFrançois Tigeot 	if (ring->cleanup)
13481b13d190SFrançois Tigeot 		ring->cleanup(ring);
13491b13d190SFrançois Tigeot 
13501b13d190SFrançois Tigeot 	i915_cmd_parser_fini_ring(ring);
13511b13d190SFrançois Tigeot 
13521b13d190SFrançois Tigeot 	if (ring->status_page.obj) {
13531b13d190SFrançois Tigeot 		kunmap(ring->status_page.obj->pages[0]);
13541b13d190SFrançois Tigeot 		ring->status_page.obj = NULL;
13551b13d190SFrançois Tigeot 	}
13561b13d190SFrançois Tigeot }
13571b13d190SFrançois Tigeot 
13581b13d190SFrançois Tigeot static int logical_ring_init(struct drm_device *dev, struct intel_engine_cs *ring)
13591b13d190SFrançois Tigeot {
13601b13d190SFrançois Tigeot 	int ret;
13611b13d190SFrançois Tigeot 
13621b13d190SFrançois Tigeot 	/* Intentionally left blank. */
13631b13d190SFrançois Tigeot 	ring->buffer = NULL;
13641b13d190SFrançois Tigeot 
13651b13d190SFrançois Tigeot 	ring->dev = dev;
13661b13d190SFrançois Tigeot 	INIT_LIST_HEAD(&ring->active_list);
13671b13d190SFrançois Tigeot 	INIT_LIST_HEAD(&ring->request_list);
13681b13d190SFrançois Tigeot 	init_waitqueue_head(&ring->irq_queue);
13691b13d190SFrançois Tigeot 
13701b13d190SFrançois Tigeot 	INIT_LIST_HEAD(&ring->execlist_queue);
1371*2c9916cdSFrançois Tigeot 	INIT_LIST_HEAD(&ring->execlist_retired_req_list);
13721b13d190SFrançois Tigeot 	lockinit(&ring->execlist_lock, "i915el", 0, LK_CANRECURSE);
13731b13d190SFrançois Tigeot 
13741b13d190SFrançois Tigeot 	ret = i915_cmd_parser_init_ring(ring);
13751b13d190SFrançois Tigeot 	if (ret)
13761b13d190SFrançois Tigeot 		return ret;
13771b13d190SFrançois Tigeot 
13781b13d190SFrançois Tigeot 	ret = intel_lr_context_deferred_create(ring->default_context, ring);
13791b13d190SFrançois Tigeot 
13801b13d190SFrançois Tigeot 	return ret;
13811b13d190SFrançois Tigeot }
13821b13d190SFrançois Tigeot 
13831b13d190SFrançois Tigeot static int logical_render_ring_init(struct drm_device *dev)
13841b13d190SFrançois Tigeot {
13851b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
13861b13d190SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[RCS];
1387*2c9916cdSFrançois Tigeot 	int ret;
13881b13d190SFrançois Tigeot 
13891b13d190SFrançois Tigeot 	ring->name = "render ring";
13901b13d190SFrançois Tigeot 	ring->id = RCS;
13911b13d190SFrançois Tigeot 	ring->mmio_base = RENDER_RING_BASE;
13921b13d190SFrançois Tigeot 	ring->irq_enable_mask =
13931b13d190SFrançois Tigeot 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
13941b13d190SFrançois Tigeot 	ring->irq_keep_mask =
13951b13d190SFrançois Tigeot 		GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
13961b13d190SFrançois Tigeot 	if (HAS_L3_DPF(dev))
13971b13d190SFrançois Tigeot 		ring->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
13981b13d190SFrançois Tigeot 
1399*2c9916cdSFrançois Tigeot 	ring->init_hw = gen8_init_render_ring;
1400*2c9916cdSFrançois Tigeot 	ring->init_context = gen8_init_rcs_context;
14011b13d190SFrançois Tigeot 	ring->cleanup = intel_fini_pipe_control;
14021b13d190SFrançois Tigeot 	ring->get_seqno = gen8_get_seqno;
14031b13d190SFrançois Tigeot 	ring->set_seqno = gen8_set_seqno;
14041b13d190SFrançois Tigeot 	ring->emit_request = gen8_emit_request;
14051b13d190SFrançois Tigeot 	ring->emit_flush = gen8_emit_flush_render;
14061b13d190SFrançois Tigeot 	ring->irq_get = gen8_logical_ring_get_irq;
14071b13d190SFrançois Tigeot 	ring->irq_put = gen8_logical_ring_put_irq;
14081b13d190SFrançois Tigeot 	ring->emit_bb_start = gen8_emit_bb_start;
14091b13d190SFrançois Tigeot 
1410*2c9916cdSFrançois Tigeot 	ring->dev = dev;
1411*2c9916cdSFrançois Tigeot 	ret = logical_ring_init(dev, ring);
1412*2c9916cdSFrançois Tigeot 	if (ret)
1413*2c9916cdSFrançois Tigeot 		return ret;
1414*2c9916cdSFrançois Tigeot 
1415*2c9916cdSFrançois Tigeot 	return intel_init_pipe_control(ring);
14161b13d190SFrançois Tigeot }
14171b13d190SFrançois Tigeot 
14181b13d190SFrançois Tigeot static int logical_bsd_ring_init(struct drm_device *dev)
14191b13d190SFrançois Tigeot {
14201b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
14211b13d190SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[VCS];
14221b13d190SFrançois Tigeot 
14231b13d190SFrançois Tigeot 	ring->name = "bsd ring";
14241b13d190SFrançois Tigeot 	ring->id = VCS;
14251b13d190SFrançois Tigeot 	ring->mmio_base = GEN6_BSD_RING_BASE;
14261b13d190SFrançois Tigeot 	ring->irq_enable_mask =
14271b13d190SFrançois Tigeot 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
14281b13d190SFrançois Tigeot 	ring->irq_keep_mask =
14291b13d190SFrançois Tigeot 		GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
14301b13d190SFrançois Tigeot 
1431*2c9916cdSFrançois Tigeot 	ring->init_hw = gen8_init_common_ring;
14321b13d190SFrançois Tigeot 	ring->get_seqno = gen8_get_seqno;
14331b13d190SFrançois Tigeot 	ring->set_seqno = gen8_set_seqno;
14341b13d190SFrançois Tigeot 	ring->emit_request = gen8_emit_request;
14351b13d190SFrançois Tigeot 	ring->emit_flush = gen8_emit_flush;
14361b13d190SFrançois Tigeot 	ring->irq_get = gen8_logical_ring_get_irq;
14371b13d190SFrançois Tigeot 	ring->irq_put = gen8_logical_ring_put_irq;
14381b13d190SFrançois Tigeot 	ring->emit_bb_start = gen8_emit_bb_start;
14391b13d190SFrançois Tigeot 
14401b13d190SFrançois Tigeot 	return logical_ring_init(dev, ring);
14411b13d190SFrançois Tigeot }
14421b13d190SFrançois Tigeot 
14431b13d190SFrançois Tigeot static int logical_bsd2_ring_init(struct drm_device *dev)
14441b13d190SFrançois Tigeot {
14451b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
14461b13d190SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
14471b13d190SFrançois Tigeot 
14481b13d190SFrançois Tigeot 	ring->name = "bds2 ring";
14491b13d190SFrançois Tigeot 	ring->id = VCS2;
14501b13d190SFrançois Tigeot 	ring->mmio_base = GEN8_BSD2_RING_BASE;
14511b13d190SFrançois Tigeot 	ring->irq_enable_mask =
14521b13d190SFrançois Tigeot 		GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
14531b13d190SFrançois Tigeot 	ring->irq_keep_mask =
14541b13d190SFrançois Tigeot 		GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
14551b13d190SFrançois Tigeot 
1456*2c9916cdSFrançois Tigeot 	ring->init_hw = gen8_init_common_ring;
14571b13d190SFrançois Tigeot 	ring->get_seqno = gen8_get_seqno;
14581b13d190SFrançois Tigeot 	ring->set_seqno = gen8_set_seqno;
14591b13d190SFrançois Tigeot 	ring->emit_request = gen8_emit_request;
14601b13d190SFrançois Tigeot 	ring->emit_flush = gen8_emit_flush;
14611b13d190SFrançois Tigeot 	ring->irq_get = gen8_logical_ring_get_irq;
14621b13d190SFrançois Tigeot 	ring->irq_put = gen8_logical_ring_put_irq;
14631b13d190SFrançois Tigeot 	ring->emit_bb_start = gen8_emit_bb_start;
14641b13d190SFrançois Tigeot 
14651b13d190SFrançois Tigeot 	return logical_ring_init(dev, ring);
14661b13d190SFrançois Tigeot }
14671b13d190SFrançois Tigeot 
14681b13d190SFrançois Tigeot static int logical_blt_ring_init(struct drm_device *dev)
14691b13d190SFrançois Tigeot {
14701b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
14711b13d190SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[BCS];
14721b13d190SFrançois Tigeot 
14731b13d190SFrançois Tigeot 	ring->name = "blitter ring";
14741b13d190SFrançois Tigeot 	ring->id = BCS;
14751b13d190SFrançois Tigeot 	ring->mmio_base = BLT_RING_BASE;
14761b13d190SFrançois Tigeot 	ring->irq_enable_mask =
14771b13d190SFrançois Tigeot 		GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
14781b13d190SFrançois Tigeot 	ring->irq_keep_mask =
14791b13d190SFrançois Tigeot 		GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
14801b13d190SFrançois Tigeot 
1481*2c9916cdSFrançois Tigeot 	ring->init_hw = gen8_init_common_ring;
14821b13d190SFrançois Tigeot 	ring->get_seqno = gen8_get_seqno;
14831b13d190SFrançois Tigeot 	ring->set_seqno = gen8_set_seqno;
14841b13d190SFrançois Tigeot 	ring->emit_request = gen8_emit_request;
14851b13d190SFrançois Tigeot 	ring->emit_flush = gen8_emit_flush;
14861b13d190SFrançois Tigeot 	ring->irq_get = gen8_logical_ring_get_irq;
14871b13d190SFrançois Tigeot 	ring->irq_put = gen8_logical_ring_put_irq;
14881b13d190SFrançois Tigeot 	ring->emit_bb_start = gen8_emit_bb_start;
14891b13d190SFrançois Tigeot 
14901b13d190SFrançois Tigeot 	return logical_ring_init(dev, ring);
14911b13d190SFrançois Tigeot }
14921b13d190SFrançois Tigeot 
14931b13d190SFrançois Tigeot static int logical_vebox_ring_init(struct drm_device *dev)
14941b13d190SFrançois Tigeot {
14951b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
14961b13d190SFrançois Tigeot 	struct intel_engine_cs *ring = &dev_priv->ring[VECS];
14971b13d190SFrançois Tigeot 
14981b13d190SFrançois Tigeot 	ring->name = "video enhancement ring";
14991b13d190SFrançois Tigeot 	ring->id = VECS;
15001b13d190SFrançois Tigeot 	ring->mmio_base = VEBOX_RING_BASE;
15011b13d190SFrançois Tigeot 	ring->irq_enable_mask =
15021b13d190SFrançois Tigeot 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
15031b13d190SFrançois Tigeot 	ring->irq_keep_mask =
15041b13d190SFrançois Tigeot 		GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
15051b13d190SFrançois Tigeot 
1506*2c9916cdSFrançois Tigeot 	ring->init_hw = gen8_init_common_ring;
15071b13d190SFrançois Tigeot 	ring->get_seqno = gen8_get_seqno;
15081b13d190SFrançois Tigeot 	ring->set_seqno = gen8_set_seqno;
15091b13d190SFrançois Tigeot 	ring->emit_request = gen8_emit_request;
15101b13d190SFrançois Tigeot 	ring->emit_flush = gen8_emit_flush;
15111b13d190SFrançois Tigeot 	ring->irq_get = gen8_logical_ring_get_irq;
15121b13d190SFrançois Tigeot 	ring->irq_put = gen8_logical_ring_put_irq;
15131b13d190SFrançois Tigeot 	ring->emit_bb_start = gen8_emit_bb_start;
15141b13d190SFrançois Tigeot 
15151b13d190SFrançois Tigeot 	return logical_ring_init(dev, ring);
15161b13d190SFrançois Tigeot }
15171b13d190SFrançois Tigeot 
15181b13d190SFrançois Tigeot /**
15191b13d190SFrançois Tigeot  * intel_logical_rings_init() - allocate, populate and init the Engine Command Streamers
15201b13d190SFrançois Tigeot  * @dev: DRM device.
15211b13d190SFrançois Tigeot  *
15221b13d190SFrançois Tigeot  * This function inits the engines for an Execlists submission style (the equivalent in the
15231b13d190SFrançois Tigeot  * legacy ringbuffer submission world would be i915_gem_init_rings). It does it only for
15241b13d190SFrançois Tigeot  * those engines that are present in the hardware.
15251b13d190SFrançois Tigeot  *
15261b13d190SFrançois Tigeot  * Return: non-zero if the initialization failed.
15271b13d190SFrançois Tigeot  */
15281b13d190SFrançois Tigeot int intel_logical_rings_init(struct drm_device *dev)
15291b13d190SFrançois Tigeot {
15301b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
15311b13d190SFrançois Tigeot 	int ret;
15321b13d190SFrançois Tigeot 
15331b13d190SFrançois Tigeot 	ret = logical_render_ring_init(dev);
15341b13d190SFrançois Tigeot 	if (ret)
15351b13d190SFrançois Tigeot 		return ret;
15361b13d190SFrançois Tigeot 
15371b13d190SFrançois Tigeot 	if (HAS_BSD(dev)) {
15381b13d190SFrançois Tigeot 		ret = logical_bsd_ring_init(dev);
15391b13d190SFrançois Tigeot 		if (ret)
15401b13d190SFrançois Tigeot 			goto cleanup_render_ring;
15411b13d190SFrançois Tigeot 	}
15421b13d190SFrançois Tigeot 
15431b13d190SFrançois Tigeot 	if (HAS_BLT(dev)) {
15441b13d190SFrançois Tigeot 		ret = logical_blt_ring_init(dev);
15451b13d190SFrançois Tigeot 		if (ret)
15461b13d190SFrançois Tigeot 			goto cleanup_bsd_ring;
15471b13d190SFrançois Tigeot 	}
15481b13d190SFrançois Tigeot 
15491b13d190SFrançois Tigeot 	if (HAS_VEBOX(dev)) {
15501b13d190SFrançois Tigeot 		ret = logical_vebox_ring_init(dev);
15511b13d190SFrançois Tigeot 		if (ret)
15521b13d190SFrançois Tigeot 			goto cleanup_blt_ring;
15531b13d190SFrançois Tigeot 	}
15541b13d190SFrançois Tigeot 
15551b13d190SFrançois Tigeot 	if (HAS_BSD2(dev)) {
15561b13d190SFrançois Tigeot 		ret = logical_bsd2_ring_init(dev);
15571b13d190SFrançois Tigeot 		if (ret)
15581b13d190SFrançois Tigeot 			goto cleanup_vebox_ring;
15591b13d190SFrançois Tigeot 	}
15601b13d190SFrançois Tigeot 
15611b13d190SFrançois Tigeot 	ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
15621b13d190SFrançois Tigeot 	if (ret)
15631b13d190SFrançois Tigeot 		goto cleanup_bsd2_ring;
15641b13d190SFrançois Tigeot 
15651b13d190SFrançois Tigeot 	return 0;
15661b13d190SFrançois Tigeot 
15671b13d190SFrançois Tigeot cleanup_bsd2_ring:
15681b13d190SFrançois Tigeot 	intel_logical_ring_cleanup(&dev_priv->ring[VCS2]);
15691b13d190SFrançois Tigeot cleanup_vebox_ring:
15701b13d190SFrançois Tigeot 	intel_logical_ring_cleanup(&dev_priv->ring[VECS]);
15711b13d190SFrançois Tigeot cleanup_blt_ring:
15721b13d190SFrançois Tigeot 	intel_logical_ring_cleanup(&dev_priv->ring[BCS]);
15731b13d190SFrançois Tigeot cleanup_bsd_ring:
15741b13d190SFrançois Tigeot 	intel_logical_ring_cleanup(&dev_priv->ring[VCS]);
15751b13d190SFrançois Tigeot cleanup_render_ring:
15761b13d190SFrançois Tigeot 	intel_logical_ring_cleanup(&dev_priv->ring[RCS]);
15771b13d190SFrançois Tigeot 
15781b13d190SFrançois Tigeot 	return ret;
15791b13d190SFrançois Tigeot }
15801b13d190SFrançois Tigeot 
15811b13d190SFrançois Tigeot int intel_lr_context_render_state_init(struct intel_engine_cs *ring,
15821b13d190SFrançois Tigeot 				       struct intel_context *ctx)
15831b13d190SFrançois Tigeot {
15841b13d190SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
15851b13d190SFrançois Tigeot 	struct render_state so;
15861b13d190SFrançois Tigeot 	struct drm_i915_file_private *file_priv = ctx->file_priv;
15871b13d190SFrançois Tigeot 	struct drm_file *file = file_priv ? file_priv->file : NULL;
15881b13d190SFrançois Tigeot 	int ret;
15891b13d190SFrançois Tigeot 
15901b13d190SFrançois Tigeot 	ret = i915_gem_render_state_prepare(ring, &so);
15911b13d190SFrançois Tigeot 	if (ret)
15921b13d190SFrançois Tigeot 		return ret;
15931b13d190SFrançois Tigeot 
15941b13d190SFrançois Tigeot 	if (so.rodata == NULL)
15951b13d190SFrançois Tigeot 		return 0;
15961b13d190SFrançois Tigeot 
15971b13d190SFrançois Tigeot 	ret = ring->emit_bb_start(ringbuf,
1598*2c9916cdSFrançois Tigeot 			ctx,
15991b13d190SFrançois Tigeot 			so.ggtt_offset,
16001b13d190SFrançois Tigeot 			I915_DISPATCH_SECURE);
16011b13d190SFrançois Tigeot 	if (ret)
16021b13d190SFrançois Tigeot 		goto out;
16031b13d190SFrançois Tigeot 
16041b13d190SFrançois Tigeot 	i915_vma_move_to_active(i915_gem_obj_to_ggtt(so.obj), ring);
16051b13d190SFrançois Tigeot 
1606*2c9916cdSFrançois Tigeot 	ret = __i915_add_request(ring, file, so.obj);
16071b13d190SFrançois Tigeot 	/* intel_logical_ring_add_request moves object to inactive if it
16081b13d190SFrançois Tigeot 	 * fails */
16091b13d190SFrançois Tigeot out:
16101b13d190SFrançois Tigeot 	i915_gem_render_state_fini(&so);
16111b13d190SFrançois Tigeot 	return ret;
16121b13d190SFrançois Tigeot }
16131b13d190SFrançois Tigeot 
16141b13d190SFrançois Tigeot static int
16151b13d190SFrançois Tigeot populate_lr_context(struct intel_context *ctx, struct drm_i915_gem_object *ctx_obj,
16161b13d190SFrançois Tigeot 		    struct intel_engine_cs *ring, struct intel_ringbuffer *ringbuf)
16171b13d190SFrançois Tigeot {
16181b13d190SFrançois Tigeot 	struct drm_device *dev = ring->dev;
16191b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
16201b13d190SFrançois Tigeot 	struct i915_hw_ppgtt *ppgtt = ctx->ppgtt;
16211b13d190SFrançois Tigeot 	struct vm_page *page;
16221b13d190SFrançois Tigeot 	uint32_t *reg_state;
16231b13d190SFrançois Tigeot 	int ret;
16241b13d190SFrançois Tigeot 
16251b13d190SFrançois Tigeot 	if (!ppgtt)
16261b13d190SFrançois Tigeot 		ppgtt = dev_priv->mm.aliasing_ppgtt;
16271b13d190SFrançois Tigeot 
16281b13d190SFrançois Tigeot 	ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
16291b13d190SFrançois Tigeot 	if (ret) {
16301b13d190SFrançois Tigeot 		DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
16311b13d190SFrançois Tigeot 		return ret;
16321b13d190SFrançois Tigeot 	}
16331b13d190SFrançois Tigeot 
16341b13d190SFrançois Tigeot 	ret = i915_gem_object_get_pages(ctx_obj);
16351b13d190SFrançois Tigeot 	if (ret) {
16361b13d190SFrançois Tigeot 		DRM_DEBUG_DRIVER("Could not get object pages\n");
16371b13d190SFrançois Tigeot 		return ret;
16381b13d190SFrançois Tigeot 	}
16391b13d190SFrançois Tigeot 
16401b13d190SFrançois Tigeot 	i915_gem_object_pin_pages(ctx_obj);
16411b13d190SFrançois Tigeot 
16421b13d190SFrançois Tigeot 	/* The second page of the context object contains some fields which must
16431b13d190SFrançois Tigeot 	 * be set up prior to the first execution. */
16441b13d190SFrançois Tigeot 	page = i915_gem_object_get_page(ctx_obj, 1);
16451b13d190SFrançois Tigeot 	reg_state = kmap_atomic(page);
16461b13d190SFrançois Tigeot 
16471b13d190SFrançois Tigeot 	/* A context is actually a big batch buffer with several MI_LOAD_REGISTER_IMM
16481b13d190SFrançois Tigeot 	 * commands followed by (reg, value) pairs. The values we are setting here are
16491b13d190SFrançois Tigeot 	 * only for the first context restore: on a subsequent save, the GPU will
16501b13d190SFrançois Tigeot 	 * recreate this batchbuffer with new values (including all the missing
16511b13d190SFrançois Tigeot 	 * MI_LOAD_REGISTER_IMM commands that we are not initializing here). */
16521b13d190SFrançois Tigeot 	if (ring->id == RCS)
16531b13d190SFrançois Tigeot 		reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(14);
16541b13d190SFrançois Tigeot 	else
16551b13d190SFrançois Tigeot 		reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(11);
16561b13d190SFrançois Tigeot 	reg_state[CTX_LRI_HEADER_0] |= MI_LRI_FORCE_POSTED;
16571b13d190SFrançois Tigeot 	reg_state[CTX_CONTEXT_CONTROL] = RING_CONTEXT_CONTROL(ring);
16581b13d190SFrançois Tigeot 	reg_state[CTX_CONTEXT_CONTROL+1] =
16591b13d190SFrançois Tigeot 			_MASKED_BIT_ENABLE((1<<3) | MI_RESTORE_INHIBIT);
16601b13d190SFrançois Tigeot 	reg_state[CTX_RING_HEAD] = RING_HEAD(ring->mmio_base);
16611b13d190SFrançois Tigeot 	reg_state[CTX_RING_HEAD+1] = 0;
16621b13d190SFrançois Tigeot 	reg_state[CTX_RING_TAIL] = RING_TAIL(ring->mmio_base);
16631b13d190SFrançois Tigeot 	reg_state[CTX_RING_TAIL+1] = 0;
16641b13d190SFrançois Tigeot 	reg_state[CTX_RING_BUFFER_START] = RING_START(ring->mmio_base);
1665*2c9916cdSFrançois Tigeot 	/* Ring buffer start address is not known until the buffer is pinned.
1666*2c9916cdSFrançois Tigeot 	 * It is written to the context image in execlists_update_context()
1667*2c9916cdSFrançois Tigeot 	 */
16681b13d190SFrançois Tigeot 	reg_state[CTX_RING_BUFFER_CONTROL] = RING_CTL(ring->mmio_base);
16691b13d190SFrançois Tigeot 	reg_state[CTX_RING_BUFFER_CONTROL+1] =
16701b13d190SFrançois Tigeot 			((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES) | RING_VALID;
16711b13d190SFrançois Tigeot 	reg_state[CTX_BB_HEAD_U] = ring->mmio_base + 0x168;
16721b13d190SFrançois Tigeot 	reg_state[CTX_BB_HEAD_U+1] = 0;
16731b13d190SFrançois Tigeot 	reg_state[CTX_BB_HEAD_L] = ring->mmio_base + 0x140;
16741b13d190SFrançois Tigeot 	reg_state[CTX_BB_HEAD_L+1] = 0;
16751b13d190SFrançois Tigeot 	reg_state[CTX_BB_STATE] = ring->mmio_base + 0x110;
16761b13d190SFrançois Tigeot 	reg_state[CTX_BB_STATE+1] = (1<<5);
16771b13d190SFrançois Tigeot 	reg_state[CTX_SECOND_BB_HEAD_U] = ring->mmio_base + 0x11c;
16781b13d190SFrançois Tigeot 	reg_state[CTX_SECOND_BB_HEAD_U+1] = 0;
16791b13d190SFrançois Tigeot 	reg_state[CTX_SECOND_BB_HEAD_L] = ring->mmio_base + 0x114;
16801b13d190SFrançois Tigeot 	reg_state[CTX_SECOND_BB_HEAD_L+1] = 0;
16811b13d190SFrançois Tigeot 	reg_state[CTX_SECOND_BB_STATE] = ring->mmio_base + 0x118;
16821b13d190SFrançois Tigeot 	reg_state[CTX_SECOND_BB_STATE+1] = 0;
16831b13d190SFrançois Tigeot 	if (ring->id == RCS) {
16841b13d190SFrançois Tigeot 		/* TODO: according to BSpec, the register state context
16851b13d190SFrançois Tigeot 		 * for CHV does not have these. OTOH, these registers do
16861b13d190SFrançois Tigeot 		 * exist in CHV. I'm waiting for a clarification */
16871b13d190SFrançois Tigeot 		reg_state[CTX_BB_PER_CTX_PTR] = ring->mmio_base + 0x1c0;
16881b13d190SFrançois Tigeot 		reg_state[CTX_BB_PER_CTX_PTR+1] = 0;
16891b13d190SFrançois Tigeot 		reg_state[CTX_RCS_INDIRECT_CTX] = ring->mmio_base + 0x1c4;
16901b13d190SFrançois Tigeot 		reg_state[CTX_RCS_INDIRECT_CTX+1] = 0;
16911b13d190SFrançois Tigeot 		reg_state[CTX_RCS_INDIRECT_CTX_OFFSET] = ring->mmio_base + 0x1c8;
16921b13d190SFrançois Tigeot 		reg_state[CTX_RCS_INDIRECT_CTX_OFFSET+1] = 0;
16931b13d190SFrançois Tigeot 	}
16941b13d190SFrançois Tigeot 	reg_state[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9);
16951b13d190SFrançois Tigeot 	reg_state[CTX_LRI_HEADER_1] |= MI_LRI_FORCE_POSTED;
16961b13d190SFrançois Tigeot 	reg_state[CTX_CTX_TIMESTAMP] = ring->mmio_base + 0x3a8;
16971b13d190SFrançois Tigeot 	reg_state[CTX_CTX_TIMESTAMP+1] = 0;
16981b13d190SFrançois Tigeot 	reg_state[CTX_PDP3_UDW] = GEN8_RING_PDP_UDW(ring, 3);
16991b13d190SFrançois Tigeot 	reg_state[CTX_PDP3_LDW] = GEN8_RING_PDP_LDW(ring, 3);
17001b13d190SFrançois Tigeot 	reg_state[CTX_PDP2_UDW] = GEN8_RING_PDP_UDW(ring, 2);
17011b13d190SFrançois Tigeot 	reg_state[CTX_PDP2_LDW] = GEN8_RING_PDP_LDW(ring, 2);
17021b13d190SFrançois Tigeot 	reg_state[CTX_PDP1_UDW] = GEN8_RING_PDP_UDW(ring, 1);
17031b13d190SFrançois Tigeot 	reg_state[CTX_PDP1_LDW] = GEN8_RING_PDP_LDW(ring, 1);
17041b13d190SFrançois Tigeot 	reg_state[CTX_PDP0_UDW] = GEN8_RING_PDP_UDW(ring, 0);
17051b13d190SFrançois Tigeot 	reg_state[CTX_PDP0_LDW] = GEN8_RING_PDP_LDW(ring, 0);
17061b13d190SFrançois Tigeot 	reg_state[CTX_PDP3_UDW+1] = upper_32_bits(ppgtt->pd_dma_addr[3]);
17071b13d190SFrançois Tigeot 	reg_state[CTX_PDP3_LDW+1] = lower_32_bits(ppgtt->pd_dma_addr[3]);
17081b13d190SFrançois Tigeot 	reg_state[CTX_PDP2_UDW+1] = upper_32_bits(ppgtt->pd_dma_addr[2]);
17091b13d190SFrançois Tigeot 	reg_state[CTX_PDP2_LDW+1] = lower_32_bits(ppgtt->pd_dma_addr[2]);
17101b13d190SFrançois Tigeot 	reg_state[CTX_PDP1_UDW+1] = upper_32_bits(ppgtt->pd_dma_addr[1]);
17111b13d190SFrançois Tigeot 	reg_state[CTX_PDP1_LDW+1] = lower_32_bits(ppgtt->pd_dma_addr[1]);
17121b13d190SFrançois Tigeot 	reg_state[CTX_PDP0_UDW+1] = upper_32_bits(ppgtt->pd_dma_addr[0]);
17131b13d190SFrançois Tigeot 	reg_state[CTX_PDP0_LDW+1] = lower_32_bits(ppgtt->pd_dma_addr[0]);
17141b13d190SFrançois Tigeot 	if (ring->id == RCS) {
17151b13d190SFrançois Tigeot 		reg_state[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
17161b13d190SFrançois Tigeot 		reg_state[CTX_R_PWR_CLK_STATE] = 0x20c8;
17171b13d190SFrançois Tigeot 		reg_state[CTX_R_PWR_CLK_STATE+1] = 0;
17181b13d190SFrançois Tigeot 	}
17191b13d190SFrançois Tigeot 
17201b13d190SFrançois Tigeot 	kunmap_atomic(reg_state);
17211b13d190SFrançois Tigeot 
17221b13d190SFrançois Tigeot 	ctx_obj->dirty = 1;
17231b13d190SFrançois Tigeot 	set_page_dirty(page);
17241b13d190SFrançois Tigeot 	i915_gem_object_unpin_pages(ctx_obj);
17251b13d190SFrançois Tigeot 
17261b13d190SFrançois Tigeot 	return 0;
17271b13d190SFrançois Tigeot }
17281b13d190SFrançois Tigeot 
17291b13d190SFrançois Tigeot /**
17301b13d190SFrançois Tigeot  * intel_lr_context_free() - free the LRC specific bits of a context
17311b13d190SFrançois Tigeot  * @ctx: the LR context to free.
17321b13d190SFrançois Tigeot  *
17331b13d190SFrançois Tigeot  * The real context freeing is done in i915_gem_context_free: this only
17341b13d190SFrançois Tigeot  * takes care of the bits that are LRC related: the per-engine backing
17351b13d190SFrançois Tigeot  * objects and the logical ringbuffer.
17361b13d190SFrançois Tigeot  */
17371b13d190SFrançois Tigeot void intel_lr_context_free(struct intel_context *ctx)
17381b13d190SFrançois Tigeot {
17391b13d190SFrançois Tigeot 	int i;
17401b13d190SFrançois Tigeot 
17411b13d190SFrançois Tigeot 	for (i = 0; i < I915_NUM_RINGS; i++) {
17421b13d190SFrançois Tigeot 		struct drm_i915_gem_object *ctx_obj = ctx->engine[i].state;
17431b13d190SFrançois Tigeot 
17441b13d190SFrançois Tigeot 		if (ctx_obj) {
1745*2c9916cdSFrançois Tigeot 			struct intel_ringbuffer *ringbuf =
1746*2c9916cdSFrançois Tigeot 					ctx->engine[i].ringbuf;
1747*2c9916cdSFrançois Tigeot 			struct intel_engine_cs *ring = ringbuf->ring;
1748*2c9916cdSFrançois Tigeot 
1749*2c9916cdSFrançois Tigeot 			if (ctx == ring->default_context) {
1750*2c9916cdSFrançois Tigeot 				intel_unpin_ringbuffer_obj(ringbuf);
1751*2c9916cdSFrançois Tigeot 				i915_gem_object_ggtt_unpin(ctx_obj);
1752*2c9916cdSFrançois Tigeot 			}
1753*2c9916cdSFrançois Tigeot 			WARN_ON(ctx->engine[ring->id].pin_count);
17541b13d190SFrançois Tigeot 			intel_destroy_ringbuffer_obj(ringbuf);
17551b13d190SFrançois Tigeot 			kfree(ringbuf);
17561b13d190SFrançois Tigeot 			drm_gem_object_unreference(&ctx_obj->base);
17571b13d190SFrançois Tigeot 		}
17581b13d190SFrançois Tigeot 	}
17591b13d190SFrançois Tigeot }
17601b13d190SFrançois Tigeot 
17611b13d190SFrançois Tigeot static uint32_t get_lr_context_size(struct intel_engine_cs *ring)
17621b13d190SFrançois Tigeot {
17631b13d190SFrançois Tigeot 	int ret = 0;
17641b13d190SFrançois Tigeot 
1765*2c9916cdSFrançois Tigeot 	WARN_ON(INTEL_INFO(ring->dev)->gen < 8);
17661b13d190SFrançois Tigeot 
17671b13d190SFrançois Tigeot 	switch (ring->id) {
17681b13d190SFrançois Tigeot 	case RCS:
1769*2c9916cdSFrançois Tigeot 		if (INTEL_INFO(ring->dev)->gen >= 9)
1770*2c9916cdSFrançois Tigeot 			ret = GEN9_LR_CONTEXT_RENDER_SIZE;
1771*2c9916cdSFrançois Tigeot 		else
17721b13d190SFrançois Tigeot 			ret = GEN8_LR_CONTEXT_RENDER_SIZE;
17731b13d190SFrançois Tigeot 		break;
17741b13d190SFrançois Tigeot 	case VCS:
17751b13d190SFrançois Tigeot 	case BCS:
17761b13d190SFrançois Tigeot 	case VECS:
17771b13d190SFrançois Tigeot 	case VCS2:
17781b13d190SFrançois Tigeot 		ret = GEN8_LR_CONTEXT_OTHER_SIZE;
17791b13d190SFrançois Tigeot 		break;
17801b13d190SFrançois Tigeot 	}
17811b13d190SFrançois Tigeot 
17821b13d190SFrançois Tigeot 	return ret;
17831b13d190SFrançois Tigeot }
17841b13d190SFrançois Tigeot 
1785*2c9916cdSFrançois Tigeot static void lrc_setup_hardware_status_page(struct intel_engine_cs *ring,
1786*2c9916cdSFrançois Tigeot 		struct drm_i915_gem_object *default_ctx_obj)
1787*2c9916cdSFrançois Tigeot {
1788*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
1789*2c9916cdSFrançois Tigeot 
1790*2c9916cdSFrançois Tigeot 	/* The status page is offset 0 from the default context object
1791*2c9916cdSFrançois Tigeot 	 * in LRC mode. */
1792*2c9916cdSFrançois Tigeot 	ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(default_ctx_obj);
1793*2c9916cdSFrançois Tigeot 	ring->status_page.page_addr =
1794*2c9916cdSFrançois Tigeot 			kmap(default_ctx_obj->pages[0]);
1795*2c9916cdSFrançois Tigeot 	ring->status_page.obj = default_ctx_obj;
1796*2c9916cdSFrançois Tigeot 
1797*2c9916cdSFrançois Tigeot 	I915_WRITE(RING_HWS_PGA(ring->mmio_base),
1798*2c9916cdSFrançois Tigeot 			(u32)ring->status_page.gfx_addr);
1799*2c9916cdSFrançois Tigeot 	POSTING_READ(RING_HWS_PGA(ring->mmio_base));
1800*2c9916cdSFrançois Tigeot }
1801*2c9916cdSFrançois Tigeot 
18021b13d190SFrançois Tigeot /**
18031b13d190SFrançois Tigeot  * intel_lr_context_deferred_create() - create the LRC specific bits of a context
18041b13d190SFrançois Tigeot  * @ctx: LR context to create.
18051b13d190SFrançois Tigeot  * @ring: engine to be used with the context.
18061b13d190SFrançois Tigeot  *
18071b13d190SFrançois Tigeot  * This function can be called more than once, with different engines, if we plan
18081b13d190SFrançois Tigeot  * to use the context with them. The context backing objects and the ringbuffers
18091b13d190SFrançois Tigeot  * (specially the ringbuffer backing objects) suck a lot of memory up, and that's why
18101b13d190SFrançois Tigeot  * the creation is a deferred call: it's better to make sure first that we need to use
18111b13d190SFrançois Tigeot  * a given ring with the context.
18121b13d190SFrançois Tigeot  *
1813*2c9916cdSFrançois Tigeot  * Return: non-zero on error.
18141b13d190SFrançois Tigeot  */
18151b13d190SFrançois Tigeot int intel_lr_context_deferred_create(struct intel_context *ctx,
18161b13d190SFrançois Tigeot 				     struct intel_engine_cs *ring)
18171b13d190SFrançois Tigeot {
1818*2c9916cdSFrançois Tigeot 	const bool is_global_default_ctx = (ctx == ring->default_context);
18191b13d190SFrançois Tigeot 	struct drm_device *dev = ring->dev;
18201b13d190SFrançois Tigeot 	struct drm_i915_gem_object *ctx_obj;
18211b13d190SFrançois Tigeot 	uint32_t context_size;
18221b13d190SFrançois Tigeot 	struct intel_ringbuffer *ringbuf;
18231b13d190SFrançois Tigeot 	int ret;
18241b13d190SFrançois Tigeot 
18251b13d190SFrançois Tigeot 	WARN_ON(ctx->legacy_hw_ctx.rcs_state != NULL);
1826*2c9916cdSFrançois Tigeot 	WARN_ON(ctx->engine[ring->id].state);
18271b13d190SFrançois Tigeot 
18281b13d190SFrançois Tigeot 	context_size = round_up(get_lr_context_size(ring), 4096);
18291b13d190SFrançois Tigeot 
18301b13d190SFrançois Tigeot 	ctx_obj = i915_gem_alloc_context_obj(dev, context_size);
18311b13d190SFrançois Tigeot 	if (IS_ERR(ctx_obj)) {
18321b13d190SFrançois Tigeot 		ret = PTR_ERR(ctx_obj);
18331b13d190SFrançois Tigeot 		DRM_DEBUG_DRIVER("Alloc LRC backing obj failed: %d\n", ret);
18341b13d190SFrançois Tigeot 		return ret;
18351b13d190SFrançois Tigeot 	}
18361b13d190SFrançois Tigeot 
1837*2c9916cdSFrançois Tigeot 	if (is_global_default_ctx) {
18381b13d190SFrançois Tigeot 		ret = i915_gem_obj_ggtt_pin(ctx_obj, GEN8_LR_CONTEXT_ALIGN, 0);
18391b13d190SFrançois Tigeot 		if (ret) {
1840*2c9916cdSFrançois Tigeot 			DRM_DEBUG_DRIVER("Pin LRC backing obj failed: %d\n",
1841*2c9916cdSFrançois Tigeot 					ret);
18421b13d190SFrançois Tigeot 			drm_gem_object_unreference(&ctx_obj->base);
18431b13d190SFrançois Tigeot 			return ret;
18441b13d190SFrançois Tigeot 		}
1845*2c9916cdSFrançois Tigeot 	}
18461b13d190SFrançois Tigeot 
18471b13d190SFrançois Tigeot 	ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
18481b13d190SFrançois Tigeot 	if (!ringbuf) {
18491b13d190SFrançois Tigeot 		DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s\n",
18501b13d190SFrançois Tigeot 				ring->name);
18511b13d190SFrançois Tigeot 		ret = -ENOMEM;
1852*2c9916cdSFrançois Tigeot 		goto error_unpin_ctx;
18531b13d190SFrançois Tigeot 	}
18541b13d190SFrançois Tigeot 
18551b13d190SFrançois Tigeot 	ringbuf->ring = ring;
18561b13d190SFrançois Tigeot 
18571b13d190SFrançois Tigeot 	ringbuf->size = 32 * PAGE_SIZE;
18581b13d190SFrançois Tigeot 	ringbuf->effective_size = ringbuf->size;
18591b13d190SFrançois Tigeot 	ringbuf->head = 0;
18601b13d190SFrançois Tigeot 	ringbuf->tail = 0;
18611b13d190SFrançois Tigeot 	ringbuf->last_retired_head = -1;
1862*2c9916cdSFrançois Tigeot 	intel_ring_update_space(ringbuf);
18631b13d190SFrançois Tigeot 
1864*2c9916cdSFrançois Tigeot 	if (ringbuf->obj == NULL) {
18651b13d190SFrançois Tigeot 		ret = intel_alloc_ringbuffer_obj(dev, ringbuf);
18661b13d190SFrançois Tigeot 		if (ret) {
1867*2c9916cdSFrançois Tigeot 			DRM_DEBUG_DRIVER(
1868*2c9916cdSFrançois Tigeot 				"Failed to allocate ringbuffer obj %s: %d\n",
18691b13d190SFrançois Tigeot 				ring->name, ret);
1870*2c9916cdSFrançois Tigeot 			goto error_free_rbuf;
1871*2c9916cdSFrançois Tigeot 		}
1872*2c9916cdSFrançois Tigeot 
1873*2c9916cdSFrançois Tigeot 		if (is_global_default_ctx) {
1874*2c9916cdSFrançois Tigeot 			ret = intel_pin_and_map_ringbuffer_obj(dev, ringbuf);
1875*2c9916cdSFrançois Tigeot 			if (ret) {
1876*2c9916cdSFrançois Tigeot 				DRM_ERROR(
1877*2c9916cdSFrançois Tigeot 					"Failed to pin and map ringbuffer %s: %d\n",
1878*2c9916cdSFrançois Tigeot 					ring->name, ret);
1879*2c9916cdSFrançois Tigeot 				goto error_destroy_rbuf;
1880*2c9916cdSFrançois Tigeot 			}
1881*2c9916cdSFrançois Tigeot 		}
1882*2c9916cdSFrançois Tigeot 
18831b13d190SFrançois Tigeot 	}
18841b13d190SFrançois Tigeot 
18851b13d190SFrançois Tigeot 	ret = populate_lr_context(ctx, ctx_obj, ring, ringbuf);
18861b13d190SFrançois Tigeot 	if (ret) {
18871b13d190SFrançois Tigeot 		DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
18881b13d190SFrançois Tigeot 		goto error;
18891b13d190SFrançois Tigeot 	}
18901b13d190SFrançois Tigeot 
18911b13d190SFrançois Tigeot 	ctx->engine[ring->id].ringbuf = ringbuf;
18921b13d190SFrançois Tigeot 	ctx->engine[ring->id].state = ctx_obj;
18931b13d190SFrançois Tigeot 
1894*2c9916cdSFrançois Tigeot 	if (ctx == ring->default_context)
1895*2c9916cdSFrançois Tigeot 		lrc_setup_hardware_status_page(ring, ctx_obj);
1896*2c9916cdSFrançois Tigeot 	else if (ring->id == RCS && !ctx->rcs_initialized) {
1897*2c9916cdSFrançois Tigeot 		if (ring->init_context) {
1898*2c9916cdSFrançois Tigeot 			ret = ring->init_context(ring, ctx);
18991b13d190SFrançois Tigeot 			if (ret) {
1900*2c9916cdSFrançois Tigeot 				DRM_ERROR("ring init context: %d\n", ret);
19011b13d190SFrançois Tigeot 				ctx->engine[ring->id].ringbuf = NULL;
19021b13d190SFrançois Tigeot 				ctx->engine[ring->id].state = NULL;
19031b13d190SFrançois Tigeot 				goto error;
19041b13d190SFrançois Tigeot 			}
1905*2c9916cdSFrançois Tigeot 		}
1906*2c9916cdSFrançois Tigeot 
19071b13d190SFrançois Tigeot 		ctx->rcs_initialized = true;
19081b13d190SFrançois Tigeot 	}
19091b13d190SFrançois Tigeot 
19101b13d190SFrançois Tigeot 	return 0;
19111b13d190SFrançois Tigeot 
19121b13d190SFrançois Tigeot error:
1913*2c9916cdSFrançois Tigeot 	if (is_global_default_ctx)
1914*2c9916cdSFrançois Tigeot 		intel_unpin_ringbuffer_obj(ringbuf);
1915*2c9916cdSFrançois Tigeot error_destroy_rbuf:
1916*2c9916cdSFrançois Tigeot 	intel_destroy_ringbuffer_obj(ringbuf);
1917*2c9916cdSFrançois Tigeot error_free_rbuf:
19181b13d190SFrançois Tigeot 	kfree(ringbuf);
1919*2c9916cdSFrançois Tigeot error_unpin_ctx:
1920*2c9916cdSFrançois Tigeot 	if (is_global_default_ctx)
19211b13d190SFrançois Tigeot 		i915_gem_object_ggtt_unpin(ctx_obj);
19221b13d190SFrançois Tigeot 	drm_gem_object_unreference(&ctx_obj->base);
19231b13d190SFrançois Tigeot 	return ret;
19241b13d190SFrançois Tigeot }
1925