xref: /dflybsd-src/sys/dev/drm/i915/intel_ringbuffer.h (revision 303bf2704d314de5be18baeedb8cdc82d3f944de)
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  */
558621f407SFrançois Tigeot #define gen8_semaphore_seqno_size sizeof(uint64_t)
568621f407SFrançois Tigeot #define GEN8_SEMAPHORE_OFFSET(__from, __to)			     \
578621f407SFrançois Tigeot 	(((__from) * I915_NUM_ENGINES  + (__to)) * gen8_semaphore_seqno_size)
5824edb884SFrançois Tigeot #define GEN8_SIGNAL_OFFSET(__ring, to)			     \
5924edb884SFrançois Tigeot 	(i915_gem_obj_ggtt_offset(dev_priv->semaphore_obj) + \
608621f407SFrançois Tigeot 	 GEN8_SEMAPHORE_OFFSET((__ring)->id, (to)))
6124edb884SFrançois Tigeot #define GEN8_WAIT_OFFSET(__ring, from)			     \
6224edb884SFrançois Tigeot 	(i915_gem_obj_ggtt_offset(dev_priv->semaphore_obj) + \
638621f407SFrançois Tigeot 	 GEN8_SEMAPHORE_OFFSET(from, (__ring)->id))
6424edb884SFrançois Tigeot 
659edbd4a0SFrançois Tigeot enum intel_ring_hangcheck_action {
669edbd4a0SFrançois Tigeot 	HANGCHECK_IDLE = 0,
679edbd4a0SFrançois Tigeot 	HANGCHECK_WAIT,
689edbd4a0SFrançois Tigeot 	HANGCHECK_ACTIVE,
699edbd4a0SFrançois Tigeot 	HANGCHECK_KICK,
709edbd4a0SFrançois Tigeot 	HANGCHECK_HUNG,
719edbd4a0SFrançois Tigeot };
725d0b1887SFrançois Tigeot 
73ba55f2f5SFrançois Tigeot #define HANGCHECK_SCORE_RING_HUNG 31
74ba55f2f5SFrançois Tigeot 
755d0b1887SFrançois Tigeot struct intel_ring_hangcheck {
76ba55f2f5SFrançois Tigeot 	u64 acthd;
77*303bf270SFrançois Tigeot 	unsigned long user_interrupts;
785d0b1887SFrançois Tigeot 	u32 seqno;
795d0b1887SFrançois Tigeot 	int score;
805d0b1887SFrançois Tigeot 	enum intel_ring_hangcheck_action action;
81ba55f2f5SFrançois Tigeot 	int deadlock;
82c0e85e96SFrançois Tigeot 	u32 instdone[I915_NUM_INSTDONE_REG];
835d0b1887SFrançois Tigeot };
845d0b1887SFrançois Tigeot 
85ba55f2f5SFrançois Tigeot struct intel_ringbuffer {
86e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
87*303bf270SFrançois Tigeot 	void __iomem *virtual_start;
88c0e85e96SFrançois Tigeot 	struct i915_vma *vma;
89e3adcf8fSFrançois Tigeot 
908621f407SFrançois Tigeot 	struct intel_engine_cs *engine;
91aee94f86SFrançois Tigeot 	struct list_head link;
921b13d190SFrançois Tigeot 
9315ac6249SFrançois Tigeot 	u32 head;
9415ac6249SFrançois Tigeot 	u32 tail;
95e3adcf8fSFrançois Tigeot 	int space;
96e3adcf8fSFrançois Tigeot 	int size;
97e3adcf8fSFrançois Tigeot 	int effective_size;
98e3adcf8fSFrançois Tigeot 
99e3adcf8fSFrançois Tigeot 	/** We track the position of the requests in the ring buffer, and
100e3adcf8fSFrançois Tigeot 	 * when each is retired we increment last_retired_head as the GPU
101e3adcf8fSFrançois Tigeot 	 * must have finished processing the request and so we know we
102e3adcf8fSFrançois Tigeot 	 * can advance the ringbuffer up to that position.
103e3adcf8fSFrançois Tigeot 	 *
104e3adcf8fSFrançois Tigeot 	 * last_retired_head is set to -1 after the value is consumed so
105e3adcf8fSFrançois Tigeot 	 * we can detect new retirements.
106e3adcf8fSFrançois Tigeot 	 */
107e3adcf8fSFrançois Tigeot 	u32 last_retired_head;
108ba55f2f5SFrançois Tigeot };
109ba55f2f5SFrançois Tigeot 
1101487f786SFrançois Tigeot struct i915_gem_context;
1118621f407SFrançois Tigeot struct drm_i915_reg_table;
1122c9916cdSFrançois Tigeot 
113a05eeebfSFrançois Tigeot /*
114a05eeebfSFrançois Tigeot  * we use a single page to load ctx workarounds so all of these
115a05eeebfSFrançois Tigeot  * values are referred in terms of dwords
116a05eeebfSFrançois Tigeot  *
117a05eeebfSFrançois Tigeot  * struct i915_wa_ctx_bb:
118a05eeebfSFrançois Tigeot  *  offset: specifies batch starting position, also helpful in case
119a05eeebfSFrançois Tigeot  *    if we want to have multiple batches at different offsets based on
120a05eeebfSFrançois Tigeot  *    some criteria. It is not a requirement at the moment but provides
121a05eeebfSFrançois Tigeot  *    an option for future use.
122a05eeebfSFrançois Tigeot  *  size: size of the batch in DWORDS
123a05eeebfSFrançois Tigeot  */
124a05eeebfSFrançois Tigeot struct  i915_ctx_workarounds {
125a05eeebfSFrançois Tigeot 	struct i915_wa_ctx_bb {
126a05eeebfSFrançois Tigeot 		u32 offset;
127a05eeebfSFrançois Tigeot 		u32 size;
128a05eeebfSFrançois Tigeot 	} indirect_ctx, per_ctx;
129a05eeebfSFrançois Tigeot 	struct drm_i915_gem_object *obj;
130a05eeebfSFrançois Tigeot };
131a05eeebfSFrançois Tigeot 
132*303bf270SFrançois Tigeot struct drm_i915_gem_request;
133*303bf270SFrançois Tigeot 
134ba55f2f5SFrançois Tigeot struct intel_engine_cs {
1351487f786SFrançois Tigeot 	struct drm_i915_private *i915;
136ba55f2f5SFrançois Tigeot 	const char	*name;
1378621f407SFrançois Tigeot 	enum intel_engine_id {
138c0e85e96SFrançois Tigeot 		RCS = 0,
139ba55f2f5SFrançois Tigeot 		BCS,
140c0e85e96SFrançois Tigeot 		VCS,
141c0e85e96SFrançois Tigeot 		VCS2,	/* Keep instances of the same type engine together. */
142c0e85e96SFrançois Tigeot 		VECS
143ba55f2f5SFrançois Tigeot 	} id;
1448621f407SFrançois Tigeot #define I915_NUM_ENGINES 5
145c0e85e96SFrançois Tigeot #define _VCS(n) (VCS + (n))
146c0e85e96SFrançois Tigeot 	unsigned int exec_id;
1478621f407SFrançois Tigeot 	unsigned int hw_id;
1488621f407SFrançois Tigeot 	unsigned int guc_id; /* XXX same as hw_id? */
149ba55f2f5SFrançois Tigeot 	u32		mmio_base;
150ba55f2f5SFrançois Tigeot 	struct intel_ringbuffer *buffer;
151aee94f86SFrançois Tigeot 	struct list_head buffers;
152ba55f2f5SFrançois Tigeot 
153*303bf270SFrançois Tigeot 	/* Rather than have every client wait upon all user interrupts,
154*303bf270SFrançois Tigeot 	 * with the herd waking after every interrupt and each doing the
155*303bf270SFrançois Tigeot 	 * heavyweight seqno dance, we delegate the task (of being the
156*303bf270SFrançois Tigeot 	 * bottom-half of the user interrupt) to the first client. After
157*303bf270SFrançois Tigeot 	 * every interrupt, we wake up one client, who does the heavyweight
158*303bf270SFrançois Tigeot 	 * coherent seqno read and either goes back to sleep (if incomplete),
159*303bf270SFrançois Tigeot 	 * or wakes up all the completed clients in parallel, before then
160*303bf270SFrançois Tigeot 	 * transferring the bottom-half status to the next client in the queue.
161*303bf270SFrançois Tigeot 	 *
162*303bf270SFrançois Tigeot 	 * Compared to walking the entire list of waiters in a single dedicated
163*303bf270SFrançois Tigeot 	 * bottom-half, we reduce the latency of the first waiter by avoiding
164*303bf270SFrançois Tigeot 	 * a context switch, but incur additional coherent seqno reads when
165*303bf270SFrançois Tigeot 	 * following the chain of request breadcrumbs. Since it is most likely
166*303bf270SFrançois Tigeot 	 * that we have a single client waiting on each seqno, then reducing
167*303bf270SFrançois Tigeot 	 * the overhead of waking that client is much preferred.
168*303bf270SFrançois Tigeot 	 */
169*303bf270SFrançois Tigeot 	struct intel_breadcrumbs {
170*303bf270SFrançois Tigeot 		struct task_struct *irq_seqno_bh; /* bh for user interrupts */
171*303bf270SFrançois Tigeot 		unsigned long irq_wakeups;
172*303bf270SFrançois Tigeot 		bool irq_posted;
173*303bf270SFrançois Tigeot 
174*303bf270SFrançois Tigeot 		struct lock lock; /* protects the lists of requests */
175*303bf270SFrançois Tigeot 		struct rb_root waiters; /* sorted by retirement, priority */
176*303bf270SFrançois Tigeot 		struct rb_root signals; /* sorted by retirement */
177*303bf270SFrançois Tigeot 		struct intel_wait *first_wait; /* oldest waiter by retirement */
178*303bf270SFrançois Tigeot 		struct task_struct *signaler; /* used for fence signalling */
179*303bf270SFrançois Tigeot 		struct drm_i915_gem_request *first_signal;
180*303bf270SFrançois Tigeot 		struct timer_list fake_irq; /* used after a missed interrupt */
181*303bf270SFrançois Tigeot 
182*303bf270SFrançois Tigeot 		bool irq_enabled : 1;
183*303bf270SFrançois Tigeot 		bool rpm_wakelock : 1;
184*303bf270SFrançois Tigeot 	} breadcrumbs;
185*303bf270SFrançois Tigeot 
18619c468b4SFrançois Tigeot 	/*
18719c468b4SFrançois Tigeot 	 * A pool of objects to use as shadow copies of client batch buffers
18819c468b4SFrançois Tigeot 	 * when the command parser is enabled. Prevents the client from
18919c468b4SFrançois Tigeot 	 * modifying the batch contents after software parsing.
19019c468b4SFrançois Tigeot 	 */
19119c468b4SFrançois Tigeot 	struct i915_gem_batch_pool batch_pool;
19219c468b4SFrançois Tigeot 
193ba55f2f5SFrançois Tigeot 	struct intel_hw_status_page status_page;
194a05eeebfSFrançois Tigeot 	struct i915_ctx_workarounds wa_ctx;
195e3adcf8fSFrançois Tigeot 
196*303bf270SFrançois Tigeot 	u32             irq_keep_mask; /* always keep these interrupts */
19715ac6249SFrançois Tigeot 	u32		irq_enable_mask; /* bitmask to enable ring interrupt */
198*303bf270SFrançois Tigeot 	void		(*irq_enable)(struct intel_engine_cs *ring);
199*303bf270SFrançois Tigeot 	void		(*irq_disable)(struct intel_engine_cs *ring);
200e3adcf8fSFrançois Tigeot 
2012c9916cdSFrançois Tigeot 	int		(*init_hw)(struct intel_engine_cs *ring);
202e3adcf8fSFrançois Tigeot 
203a05eeebfSFrançois Tigeot 	int		(*init_context)(struct drm_i915_gem_request *req);
2041b13d190SFrançois Tigeot 
205ba55f2f5SFrançois Tigeot 	void		(*write_tail)(struct intel_engine_cs *ring,
20615ac6249SFrançois Tigeot 				      u32 value);
207a05eeebfSFrançois Tigeot 	int __must_check (*flush)(struct drm_i915_gem_request *req,
20815ac6249SFrançois Tigeot 				  u32	invalidate_domains,
20915ac6249SFrançois Tigeot 				  u32	flush_domains);
210a05eeebfSFrançois Tigeot 	int		(*add_request)(struct drm_i915_gem_request *req);
211b030f26bSFrançois Tigeot 	/* Some chipsets are not quite as coherent as advertised and need
212b030f26bSFrançois Tigeot 	 * an expensive kick to force a true read of the up-to-date seqno.
213b030f26bSFrançois Tigeot 	 * However, the up-to-date seqno is not always required and the last
214b030f26bSFrançois Tigeot 	 * seen value is good enough. Note that the seqno will always be
215b030f26bSFrançois Tigeot 	 * monotonic, even if not coherent.
216b030f26bSFrançois Tigeot 	 */
2178621f407SFrançois Tigeot 	void		(*irq_seqno_barrier)(struct intel_engine_cs *ring);
218a05eeebfSFrançois Tigeot 	int		(*dispatch_execbuffer)(struct drm_i915_gem_request *req,
219ba55f2f5SFrançois Tigeot 					       u64 offset, u32 length,
220477eb7f9SFrançois Tigeot 					       unsigned dispatch_flags);
22115ac6249SFrançois Tigeot #define I915_DISPATCH_SECURE 0x1
22215ac6249SFrançois Tigeot #define I915_DISPATCH_PINNED 0x2
223a05eeebfSFrançois Tigeot #define I915_DISPATCH_RS     0x4
224ba55f2f5SFrançois Tigeot 	void		(*cleanup)(struct intel_engine_cs *ring);
225e3adcf8fSFrançois Tigeot 
22624edb884SFrançois Tigeot 	/* GEN8 signal/wait table - never trust comments!
22724edb884SFrançois Tigeot 	 *	  signal to	signal to    signal to   signal to      signal to
22824edb884SFrançois Tigeot 	 *	    RCS		   VCS          BCS        VECS		 VCS2
22924edb884SFrançois Tigeot 	 *      --------------------------------------------------------------------
23024edb884SFrançois Tigeot 	 *  RCS | NOP (0x00) | VCS (0x08) | BCS (0x10) | VECS (0x18) | VCS2 (0x20) |
23124edb884SFrançois Tigeot 	 *	|-------------------------------------------------------------------
23224edb884SFrançois Tigeot 	 *  VCS | RCS (0x28) | NOP (0x30) | BCS (0x38) | VECS (0x40) | VCS2 (0x48) |
23324edb884SFrançois Tigeot 	 *	|-------------------------------------------------------------------
23424edb884SFrançois Tigeot 	 *  BCS | RCS (0x50) | VCS (0x58) | NOP (0x60) | VECS (0x68) | VCS2 (0x70) |
23524edb884SFrançois Tigeot 	 *	|-------------------------------------------------------------------
23624edb884SFrançois Tigeot 	 * VECS | RCS (0x78) | VCS (0x80) | BCS (0x88) |  NOP (0x90) | VCS2 (0x98) |
23724edb884SFrançois Tigeot 	 *	|-------------------------------------------------------------------
23824edb884SFrançois Tigeot 	 * VCS2 | RCS (0xa0) | VCS (0xa8) | BCS (0xb0) | VECS (0xb8) | NOP  (0xc0) |
23924edb884SFrançois Tigeot 	 *	|-------------------------------------------------------------------
24024edb884SFrançois Tigeot 	 *
24124edb884SFrançois Tigeot 	 * Generalization:
24224edb884SFrançois Tigeot 	 *  f(x, y) := (x->id * NUM_RINGS * seqno_size) + (seqno_size * y->id)
24324edb884SFrançois Tigeot 	 *  ie. transpose of g(x, y)
24424edb884SFrançois Tigeot 	 *
24524edb884SFrançois Tigeot 	 *	 sync from	sync from    sync from    sync from	sync from
24624edb884SFrançois Tigeot 	 *	    RCS		   VCS          BCS        VECS		 VCS2
24724edb884SFrançois Tigeot 	 *      --------------------------------------------------------------------
24824edb884SFrançois Tigeot 	 *  RCS | NOP (0x00) | VCS (0x28) | BCS (0x50) | VECS (0x78) | VCS2 (0xa0) |
24924edb884SFrançois Tigeot 	 *	|-------------------------------------------------------------------
25024edb884SFrançois Tigeot 	 *  VCS | RCS (0x08) | NOP (0x30) | BCS (0x58) | VECS (0x80) | VCS2 (0xa8) |
25124edb884SFrançois Tigeot 	 *	|-------------------------------------------------------------------
25224edb884SFrançois Tigeot 	 *  BCS | RCS (0x10) | VCS (0x38) | NOP (0x60) | VECS (0x88) | VCS2 (0xb0) |
25324edb884SFrançois Tigeot 	 *	|-------------------------------------------------------------------
25424edb884SFrançois Tigeot 	 * VECS | RCS (0x18) | VCS (0x40) | BCS (0x68) |  NOP (0x90) | VCS2 (0xb8) |
25524edb884SFrançois Tigeot 	 *	|-------------------------------------------------------------------
25624edb884SFrançois Tigeot 	 * VCS2 | RCS (0x20) | VCS (0x48) | BCS (0x70) | VECS (0x98) |  NOP (0xc0) |
25724edb884SFrançois Tigeot 	 *	|-------------------------------------------------------------------
25824edb884SFrançois Tigeot 	 *
25924edb884SFrançois Tigeot 	 * Generalization:
26024edb884SFrançois Tigeot 	 *  g(x, y) := (y->id * NUM_RINGS * seqno_size) + (seqno_size * x->id)
26124edb884SFrançois Tigeot 	 *  ie. transpose of f(x, y)
26224edb884SFrançois Tigeot 	 */
263ba55f2f5SFrançois Tigeot 	struct {
2648621f407SFrançois Tigeot 		u32	sync_seqno[I915_NUM_ENGINES-1];
265ba55f2f5SFrançois Tigeot 
26624edb884SFrançois Tigeot 		union {
267ba55f2f5SFrançois Tigeot 			struct {
2685d0b1887SFrançois Tigeot 				/* our mbox written by others */
2698621f407SFrançois Tigeot 				u32		wait[I915_NUM_ENGINES];
2705d0b1887SFrançois Tigeot 				/* mboxes this ring signals to */
2718621f407SFrançois Tigeot 				i915_reg_t	signal[I915_NUM_ENGINES];
272ba55f2f5SFrançois Tigeot 			} mbox;
2738621f407SFrançois Tigeot 			u64		signal_ggtt[I915_NUM_ENGINES];
27424edb884SFrançois Tigeot 		};
275ba55f2f5SFrançois Tigeot 
276ba55f2f5SFrançois Tigeot 		/* AKA wait() */
277a05eeebfSFrançois Tigeot 		int	(*sync_to)(struct drm_i915_gem_request *to_req,
278a05eeebfSFrançois Tigeot 				   struct intel_engine_cs *from,
279ba55f2f5SFrançois Tigeot 				   u32 seqno);
280a05eeebfSFrançois Tigeot 		int	(*signal)(struct drm_i915_gem_request *signaller_req,
281ba55f2f5SFrançois Tigeot 				  /* num_dwords needed by caller */
282ba55f2f5SFrançois Tigeot 				  unsigned int num_dwords);
283ba55f2f5SFrançois Tigeot 	} semaphore;
2845d0b1887SFrançois Tigeot 
2851b13d190SFrançois Tigeot 	/* Execlists */
2868621f407SFrançois Tigeot 	struct tasklet_struct irq_tasklet;
2878621f407SFrançois Tigeot 	struct lock execlist_lock;	/* used inside tasklet, use spin_lock_bh */
2881b13d190SFrançois Tigeot 	struct list_head execlist_queue;
2898621f407SFrançois Tigeot 	unsigned int fw_domains;
2908621f407SFrançois Tigeot 	unsigned int next_context_status_buffer;
2918621f407SFrançois Tigeot 	unsigned int idle_lite_restore_wa;
292c0e85e96SFrançois Tigeot 	bool disable_lite_restore_wa;
293c0e85e96SFrançois Tigeot 	u32 ctx_desc_template;
294a05eeebfSFrançois Tigeot 	int		(*emit_request)(struct drm_i915_gem_request *request);
295a05eeebfSFrançois Tigeot 	int		(*emit_flush)(struct drm_i915_gem_request *request,
2961b13d190SFrançois Tigeot 				      u32 invalidate_domains,
2971b13d190SFrançois Tigeot 				      u32 flush_domains);
298a05eeebfSFrançois Tigeot 	int		(*emit_bb_start)(struct drm_i915_gem_request *req,
299477eb7f9SFrançois Tigeot 					 u64 offset, unsigned dispatch_flags);
3001b13d190SFrançois Tigeot 
301e3adcf8fSFrançois Tigeot 	/**
302e3adcf8fSFrançois Tigeot 	 * List of objects currently involved in rendering from the
303e3adcf8fSFrançois Tigeot 	 * ringbuffer.
304e3adcf8fSFrançois Tigeot 	 *
305e3adcf8fSFrançois Tigeot 	 * Includes buffers having the contents of their GPU caches
3062c9916cdSFrançois Tigeot 	 * flushed, not necessarily primitives.  last_read_req
307e3adcf8fSFrançois Tigeot 	 * represents when the rendering involved will be completed.
308e3adcf8fSFrançois Tigeot 	 *
309e3adcf8fSFrançois Tigeot 	 * A reference is held on the buffer while on this list.
310e3adcf8fSFrançois Tigeot 	 */
311e3adcf8fSFrançois Tigeot 	struct list_head active_list;
312e3adcf8fSFrançois Tigeot 
313e3adcf8fSFrançois Tigeot 	/**
314e3adcf8fSFrançois Tigeot 	 * List of breadcrumbs associated with GPU requests currently
315e3adcf8fSFrançois Tigeot 	 * outstanding.
316e3adcf8fSFrançois Tigeot 	 */
317e3adcf8fSFrançois Tigeot 	struct list_head request_list;
318e3adcf8fSFrançois Tigeot 
319e3adcf8fSFrançois Tigeot 	/**
32019c468b4SFrançois Tigeot 	 * Seqno of request most recently submitted to request_list.
32119c468b4SFrançois Tigeot 	 * Used exclusively by hang checker to avoid grabbing lock while
32219c468b4SFrançois Tigeot 	 * inspecting request list.
32319c468b4SFrançois Tigeot 	 */
32419c468b4SFrançois Tigeot 	u32 last_submitted_seqno;
32519c468b4SFrançois Tigeot 
326b030f26bSFrançois Tigeot 	bool gpu_caches_dirty;
327b030f26bSFrançois Tigeot 
3281487f786SFrançois Tigeot 	struct i915_gem_context *last_context;
3295d0b1887SFrançois Tigeot 
3305d0b1887SFrançois Tigeot 	struct intel_ring_hangcheck hangcheck;
33115ac6249SFrançois Tigeot 
3329edbd4a0SFrançois Tigeot 	struct {
3339edbd4a0SFrançois Tigeot 		struct drm_i915_gem_object *obj;
3349edbd4a0SFrançois Tigeot 		u32 gtt_offset;
3359edbd4a0SFrançois Tigeot 	} scratch;
336ba55f2f5SFrançois Tigeot 
337ba55f2f5SFrançois Tigeot 	bool needs_cmd_parser;
338ba55f2f5SFrançois Tigeot 
339ba55f2f5SFrançois Tigeot 	/*
340ba55f2f5SFrançois Tigeot 	 * Table of commands the command parser needs to know about
341ba55f2f5SFrançois Tigeot 	 * for this ring.
342ba55f2f5SFrançois Tigeot 	 */
343ba55f2f5SFrançois Tigeot 	DECLARE_HASHTABLE(cmd_hash, I915_CMD_HASH_ORDER);
344ba55f2f5SFrançois Tigeot 
345ba55f2f5SFrançois Tigeot 	/*
346ba55f2f5SFrançois Tigeot 	 * Table of registers allowed in commands that read/write registers.
347ba55f2f5SFrançois Tigeot 	 */
3488621f407SFrançois Tigeot 	const struct drm_i915_reg_table *reg_tables;
3498621f407SFrançois Tigeot 	int reg_table_count;
350ba55f2f5SFrançois Tigeot 
351ba55f2f5SFrançois Tigeot 	/*
352ba55f2f5SFrançois Tigeot 	 * Returns the bitmask for the length field of the specified command.
353ba55f2f5SFrançois Tigeot 	 * Return 0 for an unrecognized/invalid command.
354ba55f2f5SFrançois Tigeot 	 *
355ba55f2f5SFrançois Tigeot 	 * If the command parser finds an entry for a command in the ring's
356ba55f2f5SFrançois Tigeot 	 * cmd_tables, it gets the command's length based on the table entry.
357ba55f2f5SFrançois Tigeot 	 * If not, it calls this function to determine the per-ring length field
358ba55f2f5SFrançois Tigeot 	 * encoding for the command (i.e. certain opcode ranges use certain bits
359ba55f2f5SFrançois Tigeot 	 * to encode the command length in the header).
360ba55f2f5SFrançois Tigeot 	 */
361ba55f2f5SFrançois Tigeot 	u32 (*get_cmd_length_mask)(u32 cmd_header);
362e3adcf8fSFrançois Tigeot };
363e3adcf8fSFrançois Tigeot 
364aee94f86SFrançois Tigeot static inline bool
365*303bf270SFrançois Tigeot intel_engine_initialized(const struct intel_engine_cs *engine)
366aee94f86SFrançois Tigeot {
3671487f786SFrançois Tigeot 	return engine->i915 != NULL;
368aee94f86SFrançois Tigeot }
369f4e1c372SFrançois Tigeot 
370e3adcf8fSFrançois Tigeot static inline unsigned
371*303bf270SFrançois Tigeot intel_engine_flag(const struct intel_engine_cs *engine)
372e3adcf8fSFrançois Tigeot {
3738621f407SFrançois Tigeot 	return 1 << engine->id;
374e3adcf8fSFrançois Tigeot }
375e3adcf8fSFrançois Tigeot 
376f4e1c372SFrançois Tigeot static inline u32
3778621f407SFrançois Tigeot intel_ring_sync_index(struct intel_engine_cs *engine,
378ba55f2f5SFrançois Tigeot 		      struct intel_engine_cs *other)
379e3adcf8fSFrançois Tigeot {
380e3adcf8fSFrançois Tigeot 	int idx;
381e3adcf8fSFrançois Tigeot 
382e3adcf8fSFrançois Tigeot 	/*
38324edb884SFrançois Tigeot 	 * rcs -> 0 = vcs, 1 = bcs, 2 = vecs, 3 = vcs2;
38424edb884SFrançois Tigeot 	 * vcs -> 0 = bcs, 1 = vecs, 2 = vcs2, 3 = rcs;
38524edb884SFrançois Tigeot 	 * bcs -> 0 = vecs, 1 = vcs2. 2 = rcs, 3 = vcs;
38624edb884SFrançois Tigeot 	 * vecs -> 0 = vcs2, 1 = rcs, 2 = vcs, 3 = bcs;
38724edb884SFrançois Tigeot 	 * vcs2 -> 0 = rcs, 1 = vcs, 2 = bcs, 3 = vecs;
388e3adcf8fSFrançois Tigeot 	 */
389e3adcf8fSFrançois Tigeot 
3908621f407SFrançois Tigeot 	idx = (other - engine) - 1;
391e3adcf8fSFrançois Tigeot 	if (idx < 0)
3928621f407SFrançois Tigeot 		idx += I915_NUM_ENGINES;
393e3adcf8fSFrançois Tigeot 
394e3adcf8fSFrançois Tigeot 	return idx;
395e3adcf8fSFrançois Tigeot }
396e3adcf8fSFrançois Tigeot 
397352ff8bdSFrançois Tigeot static inline void
3988621f407SFrançois Tigeot intel_flush_status_page(struct intel_engine_cs *engine, int reg)
399352ff8bdSFrançois Tigeot {
4008621f407SFrançois Tigeot 	mb();
4018621f407SFrançois Tigeot 	linux_clflush(&engine->status_page.page_addr[reg]);
4028621f407SFrançois Tigeot 	mb();
403352ff8bdSFrançois Tigeot }
404352ff8bdSFrançois Tigeot 
405f4e1c372SFrançois Tigeot static inline u32
4068621f407SFrançois Tigeot intel_read_status_page(struct intel_engine_cs *engine, int reg)
407e3adcf8fSFrançois Tigeot {
408f4e1c372SFrançois Tigeot 	/* Ensure that the compiler doesn't optimize away the load. */
4098621f407SFrançois Tigeot 	return READ_ONCE(engine->status_page.page_addr[reg]);
410e3adcf8fSFrançois Tigeot }
411e3adcf8fSFrançois Tigeot 
412a2fdbec6SFrançois Tigeot static inline void
4138621f407SFrançois Tigeot intel_write_status_page(struct intel_engine_cs *engine,
414a2fdbec6SFrançois Tigeot 			int reg, u32 value)
415a2fdbec6SFrançois Tigeot {
4168621f407SFrançois Tigeot 	engine->status_page.page_addr[reg] = value;
417a2fdbec6SFrançois Tigeot }
418a2fdbec6SFrançois Tigeot 
419c0e85e96SFrançois Tigeot /*
420f4e1c372SFrançois Tigeot  * Reads a dword out of the status page, which is written to from the command
421f4e1c372SFrançois Tigeot  * queue by automatic updates, MI_REPORT_HEAD, MI_STORE_DATA_INDEX, or
422f4e1c372SFrançois Tigeot  * MI_STORE_DATA_IMM.
423f4e1c372SFrançois Tigeot  *
424f4e1c372SFrançois Tigeot  * The following dwords have a reserved meaning:
425f4e1c372SFrançois Tigeot  * 0x00: ISR copy, updated when an ISR bit not set in the HWSTAM changes.
426f4e1c372SFrançois Tigeot  * 0x04: ring 0 head pointer
427f4e1c372SFrançois Tigeot  * 0x05: ring 1 head pointer (915-class)
428f4e1c372SFrançois Tigeot  * 0x06: ring 2 head pointer (915-class)
429f4e1c372SFrançois Tigeot  * 0x10-0x1b: Context status DWords (GM45)
430f4e1c372SFrançois Tigeot  * 0x1f: Last written status offset. (GM45)
431477eb7f9SFrançois Tigeot  * 0x20-0x2f: Reserved (Gen6+)
432f4e1c372SFrançois Tigeot  *
433477eb7f9SFrançois Tigeot  * The area from dword 0x30 to 0x3ff is available for driver usage.
434f4e1c372SFrançois Tigeot  */
435477eb7f9SFrançois Tigeot #define I915_GEM_HWS_INDEX		0x30
436c0e85e96SFrançois Tigeot #define I915_GEM_HWS_INDEX_ADDR (I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT)
437477eb7f9SFrançois Tigeot #define I915_GEM_HWS_SCRATCH_INDEX	0x40
438f4e1c372SFrançois Tigeot #define I915_GEM_HWS_SCRATCH_ADDR (I915_GEM_HWS_SCRATCH_INDEX << MI_STORE_DWORD_INDEX_SHIFT)
439e3adcf8fSFrançois Tigeot 
440352ff8bdSFrançois Tigeot struct intel_ringbuffer *
441352ff8bdSFrançois Tigeot intel_engine_create_ringbuffer(struct intel_engine_cs *engine, int size);
4421487f786SFrançois Tigeot int intel_pin_and_map_ringbuffer_obj(struct drm_i915_private *dev_priv,
4432c9916cdSFrançois Tigeot 				     struct intel_ringbuffer *ringbuf);
444352ff8bdSFrançois Tigeot void intel_unpin_ringbuffer_obj(struct intel_ringbuffer *ringbuf);
445352ff8bdSFrançois Tigeot void intel_ringbuffer_free(struct intel_ringbuffer *ring);
4461b13d190SFrançois Tigeot 
4478621f407SFrançois Tigeot void intel_stop_engine(struct intel_engine_cs *engine);
4488621f407SFrançois Tigeot void intel_cleanup_engine(struct intel_engine_cs *engine);
449f4e1c372SFrançois Tigeot 
45019c468b4SFrançois Tigeot int intel_ring_alloc_request_extras(struct drm_i915_gem_request *request);
45119c468b4SFrançois Tigeot 
452a05eeebfSFrançois Tigeot int __must_check intel_ring_begin(struct drm_i915_gem_request *req, int n);
453a05eeebfSFrançois Tigeot int __must_check intel_ring_cacheline_align(struct drm_i915_gem_request *req);
4548621f407SFrançois Tigeot static inline void intel_ring_emit(struct intel_engine_cs *engine,
455f4e1c372SFrançois Tigeot 				   u32 data)
456e3adcf8fSFrançois Tigeot {
4578621f407SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = engine->buffer;
458ba55f2f5SFrançois Tigeot 	iowrite32(data, ringbuf->virtual_start + ringbuf->tail);
459ba55f2f5SFrançois Tigeot 	ringbuf->tail += 4;
460e3adcf8fSFrançois Tigeot }
4618621f407SFrançois Tigeot static inline void intel_ring_emit_reg(struct intel_engine_cs *engine,
462aee94f86SFrançois Tigeot 				       i915_reg_t reg)
463aee94f86SFrançois Tigeot {
4648621f407SFrançois Tigeot 	intel_ring_emit(engine, i915_mmio_reg_offset(reg));
465aee94f86SFrançois Tigeot }
4668621f407SFrançois Tigeot static inline void intel_ring_advance(struct intel_engine_cs *engine)
4679edbd4a0SFrançois Tigeot {
4688621f407SFrançois Tigeot 	struct intel_ringbuffer *ringbuf = engine->buffer;
469ba55f2f5SFrançois Tigeot 	ringbuf->tail &= ringbuf->size - 1;
4709edbd4a0SFrançois Tigeot }
4711b13d190SFrançois Tigeot int __intel_ring_space(int head, int tail, int size);
4722c9916cdSFrançois Tigeot void intel_ring_update_space(struct intel_ringbuffer *ringbuf);
4739edbd4a0SFrançois Tigeot 
4748621f407SFrançois Tigeot int __must_check intel_engine_idle(struct intel_engine_cs *engine);
4758621f407SFrançois Tigeot void intel_ring_init_seqno(struct intel_engine_cs *engine, u32 seqno);
476a05eeebfSFrançois Tigeot int intel_ring_flush_all_caches(struct drm_i915_gem_request *req);
477a05eeebfSFrançois Tigeot int intel_ring_invalidate_all_caches(struct drm_i915_gem_request *req);
478e3adcf8fSFrançois Tigeot 
479*303bf270SFrançois Tigeot int intel_init_pipe_control(struct intel_engine_cs *engine, int size);
4808621f407SFrançois Tigeot void intel_fini_pipe_control(struct intel_engine_cs *engine);
4811b13d190SFrançois Tigeot 
482e3adcf8fSFrançois Tigeot int intel_init_render_ring_buffer(struct drm_device *dev);
483e3adcf8fSFrançois Tigeot int intel_init_bsd_ring_buffer(struct drm_device *dev);
484ba55f2f5SFrançois Tigeot int intel_init_bsd2_ring_buffer(struct drm_device *dev);
485e3adcf8fSFrançois Tigeot int intel_init_blt_ring_buffer(struct drm_device *dev);
4865d0b1887SFrançois Tigeot int intel_init_vebox_ring_buffer(struct drm_device *dev);
487e3adcf8fSFrançois Tigeot 
4888621f407SFrançois Tigeot u64 intel_ring_get_active_head(struct intel_engine_cs *engine);
489*303bf270SFrançois Tigeot static inline u32 intel_engine_get_seqno(struct intel_engine_cs *engine)
490*303bf270SFrançois Tigeot {
491*303bf270SFrançois Tigeot 	return intel_read_status_page(engine, I915_GEM_HWS_INDEX);
492*303bf270SFrançois Tigeot }
493e3adcf8fSFrançois Tigeot 
4948621f407SFrançois Tigeot int init_workarounds_ring(struct intel_engine_cs *engine);
4952c9916cdSFrançois Tigeot 
49624edb884SFrançois Tigeot static inline u32 intel_ring_get_tail(struct intel_ringbuffer *ringbuf)
497e3adcf8fSFrançois Tigeot {
49824edb884SFrançois Tigeot 	return ringbuf->tail;
499e3adcf8fSFrançois Tigeot }
500e3adcf8fSFrançois Tigeot 
501a05eeebfSFrançois Tigeot /*
502a05eeebfSFrançois Tigeot  * Arbitrary size for largest possible 'add request' sequence. The code paths
503a05eeebfSFrançois Tigeot  * are complex and variable. Empirical measurement shows that the worst case
5041487f786SFrançois Tigeot  * is BDW at 192 bytes (6 + 6 + 36 dwords), then ILK at 136 bytes. However,
5051487f786SFrançois Tigeot  * we need to allocate double the largest single packet within that emission
5061487f786SFrançois Tigeot  * to account for tail wraparound (so 6 + 6 + 72 dwords for BDW).
507a05eeebfSFrançois Tigeot  */
5081487f786SFrançois Tigeot #define MIN_SPACE_FOR_ADD_REQUEST 336
509a05eeebfSFrançois Tigeot 
5101487f786SFrançois Tigeot static inline u32 intel_hws_seqno_address(struct intel_engine_cs *engine)
5111487f786SFrançois Tigeot {
5121487f786SFrançois Tigeot 	return engine->status_page.gfx_addr + I915_GEM_HWS_INDEX_ADDR;
5131487f786SFrançois Tigeot }
514f4e1c372SFrançois Tigeot 
515*303bf270SFrançois Tigeot /* intel_breadcrumbs.c -- user interrupt bottom-half for waiters */
516*303bf270SFrançois Tigeot struct intel_wait {
517*303bf270SFrançois Tigeot 	struct rb_node node;
518*303bf270SFrançois Tigeot 	struct task_struct *tsk;
519*303bf270SFrançois Tigeot 	u32 seqno;
520*303bf270SFrançois Tigeot };
521*303bf270SFrançois Tigeot 
522*303bf270SFrançois Tigeot struct intel_signal_node {
523*303bf270SFrançois Tigeot 	struct rb_node node;
524*303bf270SFrançois Tigeot 	struct intel_wait wait;
525*303bf270SFrançois Tigeot };
526*303bf270SFrançois Tigeot 
527*303bf270SFrançois Tigeot int intel_engine_init_breadcrumbs(struct intel_engine_cs *engine);
528*303bf270SFrançois Tigeot 
529*303bf270SFrançois Tigeot static inline void intel_wait_init(struct intel_wait *wait, u32 seqno)
530*303bf270SFrançois Tigeot {
531*303bf270SFrançois Tigeot 	wait->tsk = current;
532*303bf270SFrançois Tigeot 	wait->seqno = seqno;
533*303bf270SFrançois Tigeot }
534*303bf270SFrançois Tigeot 
535*303bf270SFrançois Tigeot static inline bool intel_wait_complete(const struct intel_wait *wait)
536*303bf270SFrançois Tigeot {
537*303bf270SFrançois Tigeot 	return RB_EMPTY_NODE(&wait->node);
538*303bf270SFrançois Tigeot }
539*303bf270SFrançois Tigeot 
540*303bf270SFrançois Tigeot bool intel_engine_add_wait(struct intel_engine_cs *engine,
541*303bf270SFrançois Tigeot 			   struct intel_wait *wait);
542*303bf270SFrançois Tigeot void intel_engine_remove_wait(struct intel_engine_cs *engine,
543*303bf270SFrançois Tigeot 			      struct intel_wait *wait);
544*303bf270SFrançois Tigeot void intel_engine_enable_signaling(struct drm_i915_gem_request *request);
545*303bf270SFrançois Tigeot 
546*303bf270SFrançois Tigeot static inline bool intel_engine_has_waiter(struct intel_engine_cs *engine)
547*303bf270SFrançois Tigeot {
548*303bf270SFrançois Tigeot 	return READ_ONCE(engine->breadcrumbs.irq_seqno_bh);
549*303bf270SFrançois Tigeot }
550*303bf270SFrançois Tigeot 
551*303bf270SFrançois Tigeot static inline bool intel_engine_wakeup(struct intel_engine_cs *engine)
552*303bf270SFrançois Tigeot {
553*303bf270SFrançois Tigeot 	bool wakeup = false;
554*303bf270SFrançois Tigeot 	struct task_struct *tsk = READ_ONCE(engine->breadcrumbs.irq_seqno_bh);
555*303bf270SFrançois Tigeot 	/* Note that for this not to dangerously chase a dangling pointer,
556*303bf270SFrançois Tigeot 	 * the caller is responsible for ensure that the task remain valid for
557*303bf270SFrançois Tigeot 	 * wake_up_process() i.e. that the RCU grace period cannot expire.
558*303bf270SFrançois Tigeot 	 *
559*303bf270SFrançois Tigeot 	 * Also note that tsk is likely to be in !TASK_RUNNING state so an
560*303bf270SFrançois Tigeot 	 * early test for tsk->state != TASK_RUNNING before wake_up_process()
561*303bf270SFrançois Tigeot 	 * is unlikely to be beneficial.
562*303bf270SFrançois Tigeot 	 */
563*303bf270SFrançois Tigeot 	if (tsk)
564*303bf270SFrançois Tigeot 		wakeup = wake_up_process(tsk);
565*303bf270SFrançois Tigeot 	return wakeup;
566*303bf270SFrançois Tigeot }
567*303bf270SFrançois Tigeot 
568*303bf270SFrançois Tigeot void intel_engine_enable_fake_irq(struct intel_engine_cs *engine);
569*303bf270SFrançois Tigeot void intel_engine_fini_breadcrumbs(struct intel_engine_cs *engine);
570*303bf270SFrançois Tigeot unsigned int intel_kick_waiters(struct drm_i915_private *i915);
571*303bf270SFrançois Tigeot unsigned int intel_kick_signalers(struct drm_i915_private *i915);
572*303bf270SFrançois Tigeot 
573e3adcf8fSFrançois Tigeot #endif /* _INTEL_RINGBUFFER_H_ */
574