xref: /dflybsd-src/sys/dev/drm/i915/intel_ringbuffer.h (revision aee94f86171368465eaa15d649743f13cea3363a)
1e3adcf8fSFrançois Tigeot #ifndef _INTEL_RINGBUFFER_H_
2e3adcf8fSFrançois Tigeot #define _INTEL_RINGBUFFER_H_
3e3adcf8fSFrançois Tigeot 
4ba55f2f5SFrançois Tigeot #include <linux/hashtable.h>
519c468b4SFrançois Tigeot #include "i915_gem_batch_pool.h"
6ba55f2f5SFrançois Tigeot 
7ba55f2f5SFrançois Tigeot #define I915_CMD_HASH_ORDER 9
8ba55f2f5SFrançois Tigeot 
91b13d190SFrançois Tigeot /* Early gen2 devices have a cacheline of just 32 bytes, using 64 is overkill,
101b13d190SFrançois Tigeot  * but keeps the logic simple. Indeed, the whole purpose of this macro is just
111b13d190SFrançois Tigeot  * to give some inclination as to some of the magic values used in the various
121b13d190SFrançois Tigeot  * workarounds!
131b13d190SFrançois Tigeot  */
141b13d190SFrançois Tigeot #define CACHELINE_BYTES 64
15a05eeebfSFrançois Tigeot #define CACHELINE_DWORDS (CACHELINE_BYTES / sizeof(uint32_t))
161b13d190SFrançois Tigeot 
17f4e1c372SFrançois Tigeot /*
18f4e1c372SFrançois Tigeot  * Gen2 BSpec "1. Programming Environment" / 1.4.4.6 "Ring Buffer Use"
19f4e1c372SFrançois Tigeot  * Gen3 BSpec "vol1c Memory Interface Functions" / 2.3.4.5 "Ring Buffer Use"
20f4e1c372SFrançois Tigeot  * Gen4+ BSpec "vol1c Memory Interface and Command Stream" / 5.3.4.5 "Ring Buffer Use"
21f4e1c372SFrançois Tigeot  *
22f4e1c372SFrançois Tigeot  * "If the Ring Buffer Head Pointer and the Tail Pointer are on the same
23f4e1c372SFrançois Tigeot  * cacheline, the Head Pointer must not be greater than the Tail
24f4e1c372SFrançois Tigeot  * Pointer."
25f4e1c372SFrançois Tigeot  */
26f4e1c372SFrançois Tigeot #define I915_RING_FREE_SPACE 64
27f4e1c372SFrançois Tigeot 
28e3adcf8fSFrançois Tigeot struct  intel_hw_status_page {
29f4e1c372SFrançois Tigeot 	u32		*page_addr;
30e3adcf8fSFrançois Tigeot 	unsigned int	gfx_addr;
31e3adcf8fSFrançois Tigeot 	struct		drm_i915_gem_object *obj;
32e3adcf8fSFrançois Tigeot };
33e3adcf8fSFrançois Tigeot 
34e3adcf8fSFrançois Tigeot #define I915_READ_TAIL(ring) I915_READ(RING_TAIL((ring)->mmio_base))
35e3adcf8fSFrançois Tigeot #define I915_WRITE_TAIL(ring, val) I915_WRITE(RING_TAIL((ring)->mmio_base), val)
36e3adcf8fSFrançois Tigeot 
37e3adcf8fSFrançois Tigeot #define I915_READ_START(ring) I915_READ(RING_START((ring)->mmio_base))
38e3adcf8fSFrançois Tigeot #define I915_WRITE_START(ring, val) I915_WRITE(RING_START((ring)->mmio_base), val)
39e3adcf8fSFrançois Tigeot 
40e3adcf8fSFrançois Tigeot #define I915_READ_HEAD(ring)  I915_READ(RING_HEAD((ring)->mmio_base))
41e3adcf8fSFrançois Tigeot #define I915_WRITE_HEAD(ring, val) I915_WRITE(RING_HEAD((ring)->mmio_base), val)
42e3adcf8fSFrançois Tigeot 
43e3adcf8fSFrançois Tigeot #define I915_READ_CTL(ring) I915_READ(RING_CTL((ring)->mmio_base))
44e3adcf8fSFrançois Tigeot #define I915_WRITE_CTL(ring, val) I915_WRITE(RING_CTL((ring)->mmio_base), val)
45e3adcf8fSFrançois Tigeot 
46e3adcf8fSFrançois Tigeot #define I915_READ_IMR(ring) I915_READ(RING_IMR((ring)->mmio_base))
47e3adcf8fSFrançois Tigeot #define I915_WRITE_IMR(ring, val) I915_WRITE(RING_IMR((ring)->mmio_base), val)
48e3adcf8fSFrançois Tigeot 
49ba55f2f5SFrançois Tigeot #define I915_READ_MODE(ring) I915_READ(RING_MI_MODE((ring)->mmio_base))
50ba55f2f5SFrançois Tigeot #define I915_WRITE_MODE(ring, val) I915_WRITE(RING_MI_MODE((ring)->mmio_base), val)
51ba55f2f5SFrançois Tigeot 
5224edb884SFrançois Tigeot /* seqno size is actually only a uint32, but since we plan to use MI_FLUSH_DW to
5324edb884SFrançois Tigeot  * do the writes, and that must have qw aligned offsets, simply pretend it's 8b.
5424edb884SFrançois Tigeot  */
5524edb884SFrançois Tigeot #define i915_semaphore_seqno_size sizeof(uint64_t)
5624edb884SFrançois Tigeot #define GEN8_SIGNAL_OFFSET(__ring, to)			     \
5724edb884SFrançois Tigeot 	(i915_gem_obj_ggtt_offset(dev_priv->semaphore_obj) + \
5824edb884SFrançois Tigeot 	((__ring)->id * I915_NUM_RINGS * i915_semaphore_seqno_size) +	\
5924edb884SFrançois Tigeot 	(i915_semaphore_seqno_size * (to)))
6024edb884SFrançois Tigeot 
6124edb884SFrançois Tigeot #define GEN8_WAIT_OFFSET(__ring, from)			     \
6224edb884SFrançois Tigeot 	(i915_gem_obj_ggtt_offset(dev_priv->semaphore_obj) + \
6324edb884SFrançois Tigeot 	((from) * I915_NUM_RINGS * i915_semaphore_seqno_size) + \
6424edb884SFrançois Tigeot 	(i915_semaphore_seqno_size * (__ring)->id))
6524edb884SFrançois Tigeot 
6624edb884SFrançois Tigeot #define GEN8_RING_SEMAPHORE_INIT do { \
6724edb884SFrançois Tigeot 	if (!dev_priv->semaphore_obj) { \
6824edb884SFrançois Tigeot 		break; \
6924edb884SFrançois Tigeot 	} \
7024edb884SFrançois Tigeot 	ring->semaphore.signal_ggtt[RCS] = GEN8_SIGNAL_OFFSET(ring, RCS); \
7124edb884SFrançois Tigeot 	ring->semaphore.signal_ggtt[VCS] = GEN8_SIGNAL_OFFSET(ring, VCS); \
7224edb884SFrançois Tigeot 	ring->semaphore.signal_ggtt[BCS] = GEN8_SIGNAL_OFFSET(ring, BCS); \
7324edb884SFrançois Tigeot 	ring->semaphore.signal_ggtt[VECS] = GEN8_SIGNAL_OFFSET(ring, VECS); \
7424edb884SFrançois Tigeot 	ring->semaphore.signal_ggtt[VCS2] = GEN8_SIGNAL_OFFSET(ring, VCS2); \
7524edb884SFrançois Tigeot 	ring->semaphore.signal_ggtt[ring->id] = MI_SEMAPHORE_SYNC_INVALID; \
7624edb884SFrançois Tigeot 	} while(0)
7724edb884SFrançois Tigeot 
789edbd4a0SFrançois Tigeot enum intel_ring_hangcheck_action {
799edbd4a0SFrançois Tigeot 	HANGCHECK_IDLE = 0,
809edbd4a0SFrançois Tigeot 	HANGCHECK_WAIT,
819edbd4a0SFrançois Tigeot 	HANGCHECK_ACTIVE,
8224edb884SFrançois Tigeot 	HANGCHECK_ACTIVE_LOOP,
839edbd4a0SFrançois Tigeot 	HANGCHECK_KICK,
849edbd4a0SFrançois Tigeot 	HANGCHECK_HUNG,
859edbd4a0SFrançois Tigeot };
865d0b1887SFrançois Tigeot 
87ba55f2f5SFrançois Tigeot #define HANGCHECK_SCORE_RING_HUNG 31
88ba55f2f5SFrançois Tigeot 
895d0b1887SFrançois Tigeot struct intel_ring_hangcheck {
90ba55f2f5SFrançois Tigeot 	u64 acthd;
9124edb884SFrançois Tigeot 	u64 max_acthd;
925d0b1887SFrançois Tigeot 	u32 seqno;
935d0b1887SFrançois Tigeot 	int score;
945d0b1887SFrançois Tigeot 	enum intel_ring_hangcheck_action action;
95ba55f2f5SFrançois Tigeot 	int deadlock;
965d0b1887SFrançois Tigeot };
975d0b1887SFrançois Tigeot 
98ba55f2f5SFrançois Tigeot struct intel_ringbuffer {
99e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
10019c468b4SFrançois Tigeot 	char __iomem *virtual_start;
101e3adcf8fSFrançois Tigeot 
1021b13d190SFrançois Tigeot 	struct intel_engine_cs *ring;
103*aee94f86SFrançois Tigeot 	struct list_head link;
1041b13d190SFrançois Tigeot 
10515ac6249SFrançois Tigeot 	u32 head;
10615ac6249SFrançois Tigeot 	u32 tail;
107e3adcf8fSFrançois Tigeot 	int space;
108e3adcf8fSFrançois Tigeot 	int size;
109e3adcf8fSFrançois Tigeot 	int effective_size;
110a05eeebfSFrançois Tigeot 	int reserved_size;
111a05eeebfSFrançois Tigeot 	int reserved_tail;
112a05eeebfSFrançois Tigeot 	bool reserved_in_use;
113e3adcf8fSFrançois Tigeot 
114e3adcf8fSFrançois Tigeot 	/** We track the position of the requests in the ring buffer, and
115e3adcf8fSFrançois Tigeot 	 * when each is retired we increment last_retired_head as the GPU
116e3adcf8fSFrançois Tigeot 	 * must have finished processing the request and so we know we
117e3adcf8fSFrançois Tigeot 	 * can advance the ringbuffer up to that position.
118e3adcf8fSFrançois Tigeot 	 *
119e3adcf8fSFrançois Tigeot 	 * last_retired_head is set to -1 after the value is consumed so
120e3adcf8fSFrançois Tigeot 	 * we can detect new retirements.
121e3adcf8fSFrançois Tigeot 	 */
122e3adcf8fSFrançois Tigeot 	u32 last_retired_head;
123ba55f2f5SFrançois Tigeot };
124ba55f2f5SFrançois Tigeot 
1252c9916cdSFrançois Tigeot struct	intel_context;
12619c468b4SFrançois Tigeot struct drm_i915_reg_descriptor;
1272c9916cdSFrançois Tigeot 
128a05eeebfSFrançois Tigeot /*
129a05eeebfSFrançois Tigeot  * we use a single page to load ctx workarounds so all of these
130a05eeebfSFrançois Tigeot  * values are referred in terms of dwords
131a05eeebfSFrançois Tigeot  *
132a05eeebfSFrançois Tigeot  * struct i915_wa_ctx_bb:
133a05eeebfSFrançois Tigeot  *  offset: specifies batch starting position, also helpful in case
134a05eeebfSFrançois Tigeot  *    if we want to have multiple batches at different offsets based on
135a05eeebfSFrançois Tigeot  *    some criteria. It is not a requirement at the moment but provides
136a05eeebfSFrançois Tigeot  *    an option for future use.
137a05eeebfSFrançois Tigeot  *  size: size of the batch in DWORDS
138a05eeebfSFrançois Tigeot  */
139a05eeebfSFrançois Tigeot struct  i915_ctx_workarounds {
140a05eeebfSFrançois Tigeot 	struct i915_wa_ctx_bb {
141a05eeebfSFrançois Tigeot 		u32 offset;
142a05eeebfSFrançois Tigeot 		u32 size;
143a05eeebfSFrançois Tigeot 	} indirect_ctx, per_ctx;
144a05eeebfSFrançois Tigeot 	struct drm_i915_gem_object *obj;
145a05eeebfSFrançois Tigeot };
146a05eeebfSFrançois Tigeot 
147ba55f2f5SFrançois Tigeot struct  intel_engine_cs {
148ba55f2f5SFrançois Tigeot 	const char	*name;
149ba55f2f5SFrançois Tigeot 	enum intel_ring_id {
150ba55f2f5SFrançois Tigeot 		RCS = 0x0,
151ba55f2f5SFrançois Tigeot 		VCS,
152ba55f2f5SFrançois Tigeot 		BCS,
153ba55f2f5SFrançois Tigeot 		VECS,
154ba55f2f5SFrançois Tigeot 		VCS2
155ba55f2f5SFrançois Tigeot 	} id;
156ba55f2f5SFrançois Tigeot #define I915_NUM_RINGS 5
157ba55f2f5SFrançois Tigeot #define LAST_USER_RING (VECS + 1)
158ba55f2f5SFrançois Tigeot 	u32		mmio_base;
159ba55f2f5SFrançois Tigeot 	struct		drm_device *dev;
160ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *buffer;
161*aee94f86SFrançois Tigeot 	struct list_head buffers;
162ba55f2f5SFrançois Tigeot 
16319c468b4SFrançois Tigeot 	/*
16419c468b4SFrançois Tigeot 	 * A pool of objects to use as shadow copies of client batch buffers
16519c468b4SFrançois Tigeot 	 * when the command parser is enabled. Prevents the client from
16619c468b4SFrançois Tigeot 	 * modifying the batch contents after software parsing.
16719c468b4SFrançois Tigeot 	 */
16819c468b4SFrançois Tigeot 	struct i915_gem_batch_pool batch_pool;
16919c468b4SFrançois Tigeot 
170ba55f2f5SFrançois Tigeot 	struct intel_hw_status_page status_page;
171a05eeebfSFrançois Tigeot 	struct i915_ctx_workarounds wa_ctx;
172e3adcf8fSFrançois Tigeot 
1739edbd4a0SFrançois Tigeot 	unsigned irq_refcount; /* protected by dev_priv->irq_lock */
17415ac6249SFrançois Tigeot 	u32		irq_enable_mask;	/* bitmask to enable ring interrupt */
1752c9916cdSFrançois Tigeot 	struct drm_i915_gem_request *trace_irq_req;
176ba55f2f5SFrançois Tigeot 	bool __must_check (*irq_get)(struct intel_engine_cs *ring);
177ba55f2f5SFrançois Tigeot 	void		(*irq_put)(struct intel_engine_cs *ring);
178e3adcf8fSFrançois Tigeot 
1792c9916cdSFrançois Tigeot 	int		(*init_hw)(struct intel_engine_cs *ring);
180e3adcf8fSFrançois Tigeot 
181a05eeebfSFrançois Tigeot 	int		(*init_context)(struct drm_i915_gem_request *req);
1821b13d190SFrançois Tigeot 
183ba55f2f5SFrançois Tigeot 	void		(*write_tail)(struct intel_engine_cs *ring,
18415ac6249SFrançois Tigeot 				      u32 value);
185a05eeebfSFrançois Tigeot 	int __must_check (*flush)(struct drm_i915_gem_request *req,
18615ac6249SFrançois Tigeot 				  u32	invalidate_domains,
18715ac6249SFrançois Tigeot 				  u32	flush_domains);
188a05eeebfSFrançois Tigeot 	int		(*add_request)(struct drm_i915_gem_request *req);
189b030f26bSFrançois Tigeot 	/* Some chipsets are not quite as coherent as advertised and need
190b030f26bSFrançois Tigeot 	 * an expensive kick to force a true read of the up-to-date seqno.
191b030f26bSFrançois Tigeot 	 * However, the up-to-date seqno is not always required and the last
192b030f26bSFrançois Tigeot 	 * seen value is good enough. Note that the seqno will always be
193b030f26bSFrançois Tigeot 	 * monotonic, even if not coherent.
194b030f26bSFrançois Tigeot 	 */
195ba55f2f5SFrançois Tigeot 	u32		(*get_seqno)(struct intel_engine_cs *ring,
196b030f26bSFrançois Tigeot 				     bool lazy_coherency);
197ba55f2f5SFrançois Tigeot 	void		(*set_seqno)(struct intel_engine_cs *ring,
198a2fdbec6SFrançois Tigeot 				     u32 seqno);
199a05eeebfSFrançois Tigeot 	int		(*dispatch_execbuffer)(struct drm_i915_gem_request *req,
200ba55f2f5SFrançois Tigeot 					       u64 offset, u32 length,
201477eb7f9SFrançois Tigeot 					       unsigned dispatch_flags);
20215ac6249SFrançois Tigeot #define I915_DISPATCH_SECURE 0x1
20315ac6249SFrançois Tigeot #define I915_DISPATCH_PINNED 0x2
204a05eeebfSFrançois Tigeot #define I915_DISPATCH_RS     0x4
205ba55f2f5SFrançois Tigeot 	void		(*cleanup)(struct intel_engine_cs *ring);
206e3adcf8fSFrançois Tigeot 
20724edb884SFrançois Tigeot 	/* GEN8 signal/wait table - never trust comments!
20824edb884SFrançois Tigeot 	 *	  signal to	signal to    signal to   signal to      signal to
20924edb884SFrançois Tigeot 	 *	    RCS		   VCS          BCS        VECS		 VCS2
21024edb884SFrançois Tigeot 	 *      --------------------------------------------------------------------
21124edb884SFrançois Tigeot 	 *  RCS | NOP (0x00) | VCS (0x08) | BCS (0x10) | VECS (0x18) | VCS2 (0x20) |
21224edb884SFrançois Tigeot 	 *	|-------------------------------------------------------------------
21324edb884SFrançois Tigeot 	 *  VCS | RCS (0x28) | NOP (0x30) | BCS (0x38) | VECS (0x40) | VCS2 (0x48) |
21424edb884SFrançois Tigeot 	 *	|-------------------------------------------------------------------
21524edb884SFrançois Tigeot 	 *  BCS | RCS (0x50) | VCS (0x58) | NOP (0x60) | VECS (0x68) | VCS2 (0x70) |
21624edb884SFrançois Tigeot 	 *	|-------------------------------------------------------------------
21724edb884SFrançois Tigeot 	 * VECS | RCS (0x78) | VCS (0x80) | BCS (0x88) |  NOP (0x90) | VCS2 (0x98) |
21824edb884SFrançois Tigeot 	 *	|-------------------------------------------------------------------
21924edb884SFrançois Tigeot 	 * VCS2 | RCS (0xa0) | VCS (0xa8) | BCS (0xb0) | VECS (0xb8) | NOP  (0xc0) |
22024edb884SFrançois Tigeot 	 *	|-------------------------------------------------------------------
22124edb884SFrançois Tigeot 	 *
22224edb884SFrançois Tigeot 	 * Generalization:
22324edb884SFrançois Tigeot 	 *  f(x, y) := (x->id * NUM_RINGS * seqno_size) + (seqno_size * y->id)
22424edb884SFrançois Tigeot 	 *  ie. transpose of g(x, y)
22524edb884SFrançois Tigeot 	 *
22624edb884SFrançois Tigeot 	 *	 sync from	sync from    sync from    sync from	sync from
22724edb884SFrançois Tigeot 	 *	    RCS		   VCS          BCS        VECS		 VCS2
22824edb884SFrançois Tigeot 	 *      --------------------------------------------------------------------
22924edb884SFrançois Tigeot 	 *  RCS | NOP (0x00) | VCS (0x28) | BCS (0x50) | VECS (0x78) | VCS2 (0xa0) |
23024edb884SFrançois Tigeot 	 *	|-------------------------------------------------------------------
23124edb884SFrançois Tigeot 	 *  VCS | RCS (0x08) | NOP (0x30) | BCS (0x58) | VECS (0x80) | VCS2 (0xa8) |
23224edb884SFrançois Tigeot 	 *	|-------------------------------------------------------------------
23324edb884SFrançois Tigeot 	 *  BCS | RCS (0x10) | VCS (0x38) | NOP (0x60) | VECS (0x88) | VCS2 (0xb0) |
23424edb884SFrançois Tigeot 	 *	|-------------------------------------------------------------------
23524edb884SFrançois Tigeot 	 * VECS | RCS (0x18) | VCS (0x40) | BCS (0x68) |  NOP (0x90) | VCS2 (0xb8) |
23624edb884SFrançois Tigeot 	 *	|-------------------------------------------------------------------
23724edb884SFrançois Tigeot 	 * VCS2 | RCS (0x20) | VCS (0x48) | BCS (0x70) | VECS (0x98) |  NOP (0xc0) |
23824edb884SFrançois Tigeot 	 *	|-------------------------------------------------------------------
23924edb884SFrançois Tigeot 	 *
24024edb884SFrançois Tigeot 	 * Generalization:
24124edb884SFrançois Tigeot 	 *  g(x, y) := (y->id * NUM_RINGS * seqno_size) + (seqno_size * x->id)
24224edb884SFrançois Tigeot 	 *  ie. transpose of f(x, y)
24324edb884SFrançois Tigeot 	 */
244ba55f2f5SFrançois Tigeot 	struct {
245ba55f2f5SFrançois Tigeot 		u32	sync_seqno[I915_NUM_RINGS-1];
246ba55f2f5SFrançois Tigeot 
24724edb884SFrançois Tigeot 		union {
248ba55f2f5SFrançois Tigeot 			struct {
2495d0b1887SFrançois Tigeot 				/* our mbox written by others */
250ba55f2f5SFrançois Tigeot 				u32		wait[I915_NUM_RINGS];
2515d0b1887SFrançois Tigeot 				/* mboxes this ring signals to */
252*aee94f86SFrançois Tigeot 				i915_reg_t	signal[I915_NUM_RINGS];
253ba55f2f5SFrançois Tigeot 			} mbox;
25424edb884SFrançois Tigeot 			u64		signal_ggtt[I915_NUM_RINGS];
25524edb884SFrançois Tigeot 		};
256ba55f2f5SFrançois Tigeot 
257ba55f2f5SFrançois Tigeot 		/* AKA wait() */
258a05eeebfSFrançois Tigeot 		int	(*sync_to)(struct drm_i915_gem_request *to_req,
259a05eeebfSFrançois Tigeot 				   struct intel_engine_cs *from,
260ba55f2f5SFrançois Tigeot 				   u32 seqno);
261a05eeebfSFrançois Tigeot 		int	(*signal)(struct drm_i915_gem_request *signaller_req,
262ba55f2f5SFrançois Tigeot 				  /* num_dwords needed by caller */
263ba55f2f5SFrançois Tigeot 				  unsigned int num_dwords);
264ba55f2f5SFrançois Tigeot 	} semaphore;
2655d0b1887SFrançois Tigeot 
2661b13d190SFrançois Tigeot 	/* Execlists */
2671b13d190SFrançois Tigeot 	struct lock execlist_lock;
2681b13d190SFrançois Tigeot 	struct list_head execlist_queue;
2692c9916cdSFrançois Tigeot 	struct list_head execlist_retired_req_list;
2701b13d190SFrançois Tigeot 	u8 next_context_status_buffer;
2711b13d190SFrançois Tigeot 	u32             irq_keep_mask; /* bitmask for interrupts that should not be masked */
272a05eeebfSFrançois Tigeot 	int		(*emit_request)(struct drm_i915_gem_request *request);
273a05eeebfSFrançois Tigeot 	int		(*emit_flush)(struct drm_i915_gem_request *request,
2741b13d190SFrançois Tigeot 				      u32 invalidate_domains,
2751b13d190SFrançois Tigeot 				      u32 flush_domains);
276a05eeebfSFrançois Tigeot 	int		(*emit_bb_start)(struct drm_i915_gem_request *req,
277477eb7f9SFrançois Tigeot 					 u64 offset, unsigned dispatch_flags);
2781b13d190SFrançois Tigeot 
279e3adcf8fSFrançois Tigeot 	/**
280e3adcf8fSFrançois Tigeot 	 * List of objects currently involved in rendering from the
281e3adcf8fSFrançois Tigeot 	 * ringbuffer.
282e3adcf8fSFrançois Tigeot 	 *
283e3adcf8fSFrançois Tigeot 	 * Includes buffers having the contents of their GPU caches
2842c9916cdSFrançois Tigeot 	 * flushed, not necessarily primitives.  last_read_req
285e3adcf8fSFrançois Tigeot 	 * represents when the rendering involved will be completed.
286e3adcf8fSFrançois Tigeot 	 *
287e3adcf8fSFrançois Tigeot 	 * A reference is held on the buffer while on this list.
288e3adcf8fSFrançois Tigeot 	 */
289e3adcf8fSFrançois Tigeot 	struct list_head active_list;
290e3adcf8fSFrançois Tigeot 
291e3adcf8fSFrançois Tigeot 	/**
292e3adcf8fSFrançois Tigeot 	 * List of breadcrumbs associated with GPU requests currently
293e3adcf8fSFrançois Tigeot 	 * outstanding.
294e3adcf8fSFrançois Tigeot 	 */
295e3adcf8fSFrançois Tigeot 	struct list_head request_list;
296e3adcf8fSFrançois Tigeot 
297e3adcf8fSFrançois Tigeot 	/**
29819c468b4SFrançois Tigeot 	 * Seqno of request most recently submitted to request_list.
29919c468b4SFrançois Tigeot 	 * Used exclusively by hang checker to avoid grabbing lock while
30019c468b4SFrançois Tigeot 	 * inspecting request list.
30119c468b4SFrançois Tigeot 	 */
30219c468b4SFrançois Tigeot 	u32 last_submitted_seqno;
30319c468b4SFrançois Tigeot 
304b030f26bSFrançois Tigeot 	bool gpu_caches_dirty;
305b030f26bSFrançois Tigeot 
306b030f26bSFrançois Tigeot 	wait_queue_head_t irq_queue;
307e3adcf8fSFrançois Tigeot 
308ba55f2f5SFrançois Tigeot 	struct intel_context *default_context;
309ba55f2f5SFrançois Tigeot 	struct intel_context *last_context;
3105d0b1887SFrançois Tigeot 
3115d0b1887SFrançois Tigeot 	struct intel_ring_hangcheck hangcheck;
31215ac6249SFrançois Tigeot 
3139edbd4a0SFrançois Tigeot 	struct {
3149edbd4a0SFrançois Tigeot 		struct drm_i915_gem_object *obj;
3159edbd4a0SFrançois Tigeot 		u32 gtt_offset;
3169edbd4a0SFrançois Tigeot 		volatile u32 *cpu_page;
3179edbd4a0SFrançois Tigeot 	} scratch;
318ba55f2f5SFrançois Tigeot 
319ba55f2f5SFrançois Tigeot 	bool needs_cmd_parser;
320ba55f2f5SFrançois Tigeot 
321ba55f2f5SFrançois Tigeot 	/*
322ba55f2f5SFrançois Tigeot 	 * Table of commands the command parser needs to know about
323ba55f2f5SFrançois Tigeot 	 * for this ring.
324ba55f2f5SFrançois Tigeot 	 */
325ba55f2f5SFrançois Tigeot 	DECLARE_HASHTABLE(cmd_hash, I915_CMD_HASH_ORDER);
326ba55f2f5SFrançois Tigeot 
327ba55f2f5SFrançois Tigeot 	/*
328ba55f2f5SFrançois Tigeot 	 * Table of registers allowed in commands that read/write registers.
329ba55f2f5SFrançois Tigeot 	 */
33019c468b4SFrançois Tigeot 	const struct drm_i915_reg_descriptor *reg_table;
331ba55f2f5SFrançois Tigeot 	int reg_count;
332ba55f2f5SFrançois Tigeot 
333ba55f2f5SFrançois Tigeot 	/*
334ba55f2f5SFrançois Tigeot 	 * Table of registers allowed in commands that read/write registers, but
335ba55f2f5SFrançois Tigeot 	 * only from the DRM master.
336ba55f2f5SFrançois Tigeot 	 */
33719c468b4SFrançois Tigeot 	const struct drm_i915_reg_descriptor *master_reg_table;
338ba55f2f5SFrançois Tigeot 	int master_reg_count;
339ba55f2f5SFrançois Tigeot 
340ba55f2f5SFrançois Tigeot 	/*
341ba55f2f5SFrançois Tigeot 	 * Returns the bitmask for the length field of the specified command.
342ba55f2f5SFrançois Tigeot 	 * Return 0 for an unrecognized/invalid command.
343ba55f2f5SFrançois Tigeot 	 *
344ba55f2f5SFrançois Tigeot 	 * If the command parser finds an entry for a command in the ring's
345ba55f2f5SFrançois Tigeot 	 * cmd_tables, it gets the command's length based on the table entry.
346ba55f2f5SFrançois Tigeot 	 * If not, it calls this function to determine the per-ring length field
347ba55f2f5SFrançois Tigeot 	 * encoding for the command (i.e. certain opcode ranges use certain bits
348ba55f2f5SFrançois Tigeot 	 * to encode the command length in the header).
349ba55f2f5SFrançois Tigeot 	 */
350ba55f2f5SFrançois Tigeot 	u32 (*get_cmd_length_mask)(u32 cmd_header);
351e3adcf8fSFrançois Tigeot };
352e3adcf8fSFrançois Tigeot 
353*aee94f86SFrançois Tigeot static inline bool
354*aee94f86SFrançois Tigeot intel_ring_initialized(struct intel_engine_cs *ring)
355*aee94f86SFrançois Tigeot {
356*aee94f86SFrançois Tigeot 	return ring->dev != NULL;
357*aee94f86SFrançois Tigeot }
358f4e1c372SFrançois Tigeot 
359e3adcf8fSFrançois Tigeot static inline unsigned
360ba55f2f5SFrançois Tigeot intel_ring_flag(struct intel_engine_cs *ring)
361e3adcf8fSFrançois Tigeot {
362e3adcf8fSFrançois Tigeot 	return 1 << ring->id;
363e3adcf8fSFrançois Tigeot }
364e3adcf8fSFrançois Tigeot 
365f4e1c372SFrançois Tigeot static inline u32
366ba55f2f5SFrançois Tigeot intel_ring_sync_index(struct intel_engine_cs *ring,
367ba55f2f5SFrançois Tigeot 		      struct intel_engine_cs *other)
368e3adcf8fSFrançois Tigeot {
369e3adcf8fSFrançois Tigeot 	int idx;
370e3adcf8fSFrançois Tigeot 
371e3adcf8fSFrançois Tigeot 	/*
37224edb884SFrançois Tigeot 	 * rcs -> 0 = vcs, 1 = bcs, 2 = vecs, 3 = vcs2;
37324edb884SFrançois Tigeot 	 * vcs -> 0 = bcs, 1 = vecs, 2 = vcs2, 3 = rcs;
37424edb884SFrançois Tigeot 	 * bcs -> 0 = vecs, 1 = vcs2. 2 = rcs, 3 = vcs;
37524edb884SFrançois Tigeot 	 * vecs -> 0 = vcs2, 1 = rcs, 2 = vcs, 3 = bcs;
37624edb884SFrançois Tigeot 	 * vcs2 -> 0 = rcs, 1 = vcs, 2 = bcs, 3 = vecs;
377e3adcf8fSFrançois Tigeot 	 */
378e3adcf8fSFrançois Tigeot 
379e3adcf8fSFrançois Tigeot 	idx = (other - ring) - 1;
380e3adcf8fSFrançois Tigeot 	if (idx < 0)
381e3adcf8fSFrançois Tigeot 		idx += I915_NUM_RINGS;
382e3adcf8fSFrançois Tigeot 
383e3adcf8fSFrançois Tigeot 	return idx;
384e3adcf8fSFrançois Tigeot }
385e3adcf8fSFrançois Tigeot 
386352ff8bdSFrançois Tigeot static inline void
387352ff8bdSFrançois Tigeot intel_flush_status_page(struct intel_engine_cs *ring, int reg)
388352ff8bdSFrançois Tigeot {
389352ff8bdSFrançois Tigeot 	drm_clflush_virt_range(&ring->status_page.page_addr[reg],
390352ff8bdSFrançois Tigeot 			       sizeof(uint32_t));
391352ff8bdSFrançois Tigeot }
392352ff8bdSFrançois Tigeot 
393f4e1c372SFrançois Tigeot static inline u32
394ba55f2f5SFrançois Tigeot intel_read_status_page(struct intel_engine_cs *ring,
395f4e1c372SFrançois Tigeot 		       int reg)
396e3adcf8fSFrançois Tigeot {
397f4e1c372SFrançois Tigeot 	/* Ensure that the compiler doesn't optimize away the load. */
3989edbd4a0SFrançois Tigeot 	barrier();
399f4e1c372SFrançois Tigeot 	return ring->status_page.page_addr[reg];
400e3adcf8fSFrançois Tigeot }
401e3adcf8fSFrançois Tigeot 
402a2fdbec6SFrançois Tigeot static inline void
403ba55f2f5SFrançois Tigeot intel_write_status_page(struct intel_engine_cs *ring,
404a2fdbec6SFrançois Tigeot 			int reg, u32 value)
405a2fdbec6SFrançois Tigeot {
406a2fdbec6SFrançois Tigeot 	ring->status_page.page_addr[reg] = value;
407a2fdbec6SFrançois Tigeot }
408a2fdbec6SFrançois Tigeot 
409f4e1c372SFrançois Tigeot /**
410f4e1c372SFrançois Tigeot  * Reads a dword out of the status page, which is written to from the command
411f4e1c372SFrançois Tigeot  * queue by automatic updates, MI_REPORT_HEAD, MI_STORE_DATA_INDEX, or
412f4e1c372SFrançois Tigeot  * MI_STORE_DATA_IMM.
413f4e1c372SFrançois Tigeot  *
414f4e1c372SFrançois Tigeot  * The following dwords have a reserved meaning:
415f4e1c372SFrançois Tigeot  * 0x00: ISR copy, updated when an ISR bit not set in the HWSTAM changes.
416f4e1c372SFrançois Tigeot  * 0x04: ring 0 head pointer
417f4e1c372SFrançois Tigeot  * 0x05: ring 1 head pointer (915-class)
418f4e1c372SFrançois Tigeot  * 0x06: ring 2 head pointer (915-class)
419f4e1c372SFrançois Tigeot  * 0x10-0x1b: Context status DWords (GM45)
420f4e1c372SFrançois Tigeot  * 0x1f: Last written status offset. (GM45)
421477eb7f9SFrançois Tigeot  * 0x20-0x2f: Reserved (Gen6+)
422f4e1c372SFrançois Tigeot  *
423477eb7f9SFrançois Tigeot  * The area from dword 0x30 to 0x3ff is available for driver usage.
424f4e1c372SFrançois Tigeot  */
425477eb7f9SFrançois Tigeot #define I915_GEM_HWS_INDEX		0x30
426477eb7f9SFrançois Tigeot #define I915_GEM_HWS_SCRATCH_INDEX	0x40
427f4e1c372SFrançois Tigeot #define I915_GEM_HWS_SCRATCH_ADDR (I915_GEM_HWS_SCRATCH_INDEX << MI_STORE_DWORD_INDEX_SHIFT)
428e3adcf8fSFrançois Tigeot 
429352ff8bdSFrançois Tigeot struct intel_ringbuffer *
430352ff8bdSFrançois Tigeot intel_engine_create_ringbuffer(struct intel_engine_cs *engine, int size);
4312c9916cdSFrançois Tigeot int intel_pin_and_map_ringbuffer_obj(struct drm_device *dev,
4322c9916cdSFrançois Tigeot 				     struct intel_ringbuffer *ringbuf);
433352ff8bdSFrançois Tigeot void intel_unpin_ringbuffer_obj(struct intel_ringbuffer *ringbuf);
434352ff8bdSFrançois Tigeot void intel_ringbuffer_free(struct intel_ringbuffer *ring);
4351b13d190SFrançois Tigeot 
436ba55f2f5SFrançois Tigeot void intel_stop_ring_buffer(struct intel_engine_cs *ring);
437ba55f2f5SFrançois Tigeot void intel_cleanup_ring_buffer(struct intel_engine_cs *ring);
438f4e1c372SFrançois Tigeot 
43919c468b4SFrançois Tigeot int intel_ring_alloc_request_extras(struct drm_i915_gem_request *request);
44019c468b4SFrançois Tigeot 
441a05eeebfSFrançois Tigeot int __must_check intel_ring_begin(struct drm_i915_gem_request *req, int n);
442a05eeebfSFrançois Tigeot int __must_check intel_ring_cacheline_align(struct drm_i915_gem_request *req);
443ba55f2f5SFrançois Tigeot static inline void intel_ring_emit(struct intel_engine_cs *ring,
444f4e1c372SFrançois Tigeot 				   u32 data)
445e3adcf8fSFrançois Tigeot {
446ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
447ba55f2f5SFrançois Tigeot 	iowrite32(data, ringbuf->virtual_start + ringbuf->tail);
448ba55f2f5SFrançois Tigeot 	ringbuf->tail += 4;
449e3adcf8fSFrançois Tigeot }
450*aee94f86SFrançois Tigeot static inline void intel_ring_emit_reg(struct intel_engine_cs *ring,
451*aee94f86SFrançois Tigeot 				       i915_reg_t reg)
452*aee94f86SFrançois Tigeot {
453*aee94f86SFrançois Tigeot 	intel_ring_emit(ring, i915_mmio_reg_offset(reg));
454*aee94f86SFrançois Tigeot }
455ba55f2f5SFrançois Tigeot static inline void intel_ring_advance(struct intel_engine_cs *ring)
4569edbd4a0SFrançois Tigeot {
457ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = ring->buffer;
458ba55f2f5SFrançois Tigeot 	ringbuf->tail &= ringbuf->size - 1;
4599edbd4a0SFrançois Tigeot }
4601b13d190SFrançois Tigeot int __intel_ring_space(int head, int tail, int size);
4612c9916cdSFrançois Tigeot void intel_ring_update_space(struct intel_ringbuffer *ringbuf);
4621b13d190SFrançois Tigeot int intel_ring_space(struct intel_ringbuffer *ringbuf);
4631b13d190SFrançois Tigeot bool intel_ring_stopped(struct intel_engine_cs *ring);
4649edbd4a0SFrançois Tigeot 
465ba55f2f5SFrançois Tigeot int __must_check intel_ring_idle(struct intel_engine_cs *ring);
466ba55f2f5SFrançois Tigeot void intel_ring_init_seqno(struct intel_engine_cs *ring, u32 seqno);
467a05eeebfSFrançois Tigeot int intel_ring_flush_all_caches(struct drm_i915_gem_request *req);
468a05eeebfSFrançois Tigeot int intel_ring_invalidate_all_caches(struct drm_i915_gem_request *req);
469e3adcf8fSFrançois Tigeot 
4701b13d190SFrançois Tigeot void intel_fini_pipe_control(struct intel_engine_cs *ring);
4711b13d190SFrançois Tigeot int intel_init_pipe_control(struct intel_engine_cs *ring);
4721b13d190SFrançois Tigeot 
473e3adcf8fSFrançois Tigeot int intel_init_render_ring_buffer(struct drm_device *dev);
474e3adcf8fSFrançois Tigeot int intel_init_bsd_ring_buffer(struct drm_device *dev);
475ba55f2f5SFrançois Tigeot int intel_init_bsd2_ring_buffer(struct drm_device *dev);
476e3adcf8fSFrançois Tigeot int intel_init_blt_ring_buffer(struct drm_device *dev);
4775d0b1887SFrançois Tigeot int intel_init_vebox_ring_buffer(struct drm_device *dev);
478e3adcf8fSFrançois Tigeot 
479ba55f2f5SFrançois Tigeot u64 intel_ring_get_active_head(struct intel_engine_cs *ring);
480e3adcf8fSFrançois Tigeot 
4812c9916cdSFrançois Tigeot int init_workarounds_ring(struct intel_engine_cs *ring);
4822c9916cdSFrançois Tigeot 
48324edb884SFrançois Tigeot static inline u32 intel_ring_get_tail(struct intel_ringbuffer *ringbuf)
484e3adcf8fSFrançois Tigeot {
48524edb884SFrançois Tigeot 	return ringbuf->tail;
486e3adcf8fSFrançois Tigeot }
487e3adcf8fSFrançois Tigeot 
488a05eeebfSFrançois Tigeot /*
489a05eeebfSFrançois Tigeot  * Arbitrary size for largest possible 'add request' sequence. The code paths
490a05eeebfSFrançois Tigeot  * are complex and variable. Empirical measurement shows that the worst case
491a05eeebfSFrançois Tigeot  * is ILK at 136 words. Reserving too much is better than reserving too little
492a05eeebfSFrançois Tigeot  * as that allows for corner cases that might have been missed. So the figure
493a05eeebfSFrançois Tigeot  * has been rounded up to 160 words.
494a05eeebfSFrançois Tigeot  */
495a05eeebfSFrançois Tigeot #define MIN_SPACE_FOR_ADD_REQUEST	160
496a05eeebfSFrançois Tigeot 
497a05eeebfSFrançois Tigeot /*
498a05eeebfSFrançois Tigeot  * Reserve space in the ring to guarantee that the i915_add_request() call
499a05eeebfSFrançois Tigeot  * will always have sufficient room to do its stuff. The request creation
500a05eeebfSFrançois Tigeot  * code calls this automatically.
501a05eeebfSFrançois Tigeot  */
502a05eeebfSFrançois Tigeot void intel_ring_reserved_space_reserve(struct intel_ringbuffer *ringbuf, int size);
503a05eeebfSFrançois Tigeot /* Cancel the reservation, e.g. because the request is being discarded. */
504a05eeebfSFrançois Tigeot void intel_ring_reserved_space_cancel(struct intel_ringbuffer *ringbuf);
505a05eeebfSFrançois Tigeot /* Use the reserved space - for use by i915_add_request() only. */
506a05eeebfSFrançois Tigeot void intel_ring_reserved_space_use(struct intel_ringbuffer *ringbuf);
507a05eeebfSFrançois Tigeot /* Finish with the reserved space - for use by i915_add_request() only. */
508a05eeebfSFrançois Tigeot void intel_ring_reserved_space_end(struct intel_ringbuffer *ringbuf);
509a05eeebfSFrançois Tigeot 
510a05eeebfSFrançois Tigeot /* Legacy ringbuffer specific portion of reservation code: */
511a05eeebfSFrançois Tigeot int intel_ring_reserve_space(struct drm_i915_gem_request *request);
512f4e1c372SFrançois Tigeot 
513e3adcf8fSFrançois Tigeot #endif /* _INTEL_RINGBUFFER_H_ */
514