xref: /dflybsd-src/sys/dev/drm/i915/intel_ringbuffer.h (revision 15ac624938166e19db9a17ff4cf67bfa246ce505)
1e3adcf8fSFrançois Tigeot #ifndef _INTEL_RINGBUFFER_H_
2e3adcf8fSFrançois Tigeot #define _INTEL_RINGBUFFER_H_
3e3adcf8fSFrançois Tigeot 
470f8ca4eSFrançois Tigeot #include <linux/io.h>
570f8ca4eSFrançois Tigeot 
6f4e1c372SFrançois Tigeot /*
7f4e1c372SFrançois Tigeot  * Gen2 BSpec "1. Programming Environment" / 1.4.4.6 "Ring Buffer Use"
8f4e1c372SFrançois Tigeot  * Gen3 BSpec "vol1c Memory Interface Functions" / 2.3.4.5 "Ring Buffer Use"
9f4e1c372SFrançois Tigeot  * Gen4+ BSpec "vol1c Memory Interface and Command Stream" / 5.3.4.5 "Ring Buffer Use"
10f4e1c372SFrançois Tigeot  *
11f4e1c372SFrançois Tigeot  * "If the Ring Buffer Head Pointer and the Tail Pointer are on the same
12f4e1c372SFrançois Tigeot  * cacheline, the Head Pointer must not be greater than the Tail
13f4e1c372SFrançois Tigeot  * Pointer."
14f4e1c372SFrançois Tigeot  */
15f4e1c372SFrançois Tigeot #define I915_RING_FREE_SPACE 64
16f4e1c372SFrançois Tigeot 
17e3adcf8fSFrançois Tigeot struct  intel_hw_status_page {
18f4e1c372SFrançois Tigeot 	u32		*page_addr;
19e3adcf8fSFrançois Tigeot 	unsigned int	gfx_addr;
20e3adcf8fSFrançois Tigeot 	struct		drm_i915_gem_object *obj;
21e3adcf8fSFrançois Tigeot };
22e3adcf8fSFrançois Tigeot 
23e3adcf8fSFrançois Tigeot #define I915_READ_TAIL(ring) I915_READ(RING_TAIL((ring)->mmio_base))
24e3adcf8fSFrançois Tigeot #define I915_WRITE_TAIL(ring, val) I915_WRITE(RING_TAIL((ring)->mmio_base), val)
25e3adcf8fSFrançois Tigeot 
26e3adcf8fSFrançois Tigeot #define I915_READ_START(ring) I915_READ(RING_START((ring)->mmio_base))
27e3adcf8fSFrançois Tigeot #define I915_WRITE_START(ring, val) I915_WRITE(RING_START((ring)->mmio_base), val)
28e3adcf8fSFrançois Tigeot 
29e3adcf8fSFrançois Tigeot #define I915_READ_HEAD(ring)  I915_READ(RING_HEAD((ring)->mmio_base))
30e3adcf8fSFrançois Tigeot #define I915_WRITE_HEAD(ring, val) I915_WRITE(RING_HEAD((ring)->mmio_base), val)
31e3adcf8fSFrançois Tigeot 
32e3adcf8fSFrançois Tigeot #define I915_READ_CTL(ring) I915_READ(RING_CTL((ring)->mmio_base))
33e3adcf8fSFrançois Tigeot #define I915_WRITE_CTL(ring, val) I915_WRITE(RING_CTL((ring)->mmio_base), val)
34e3adcf8fSFrançois Tigeot 
35e3adcf8fSFrançois Tigeot #define I915_READ_IMR(ring) I915_READ(RING_IMR((ring)->mmio_base))
36e3adcf8fSFrançois Tigeot #define I915_WRITE_IMR(ring, val) I915_WRITE(RING_IMR((ring)->mmio_base), val)
37e3adcf8fSFrançois Tigeot 
38e3adcf8fSFrançois Tigeot #define I915_READ_NOPID(ring) I915_READ(RING_NOPID((ring)->mmio_base))
39e3adcf8fSFrançois Tigeot #define I915_READ_SYNC_0(ring) I915_READ(RING_SYNC_0((ring)->mmio_base))
40e3adcf8fSFrançois Tigeot #define I915_READ_SYNC_1(ring) I915_READ(RING_SYNC_1((ring)->mmio_base))
41e3adcf8fSFrançois Tigeot 
42e3adcf8fSFrançois Tigeot struct  intel_ring_buffer {
43e3adcf8fSFrançois Tigeot 	const char	*name;
44e3adcf8fSFrançois Tigeot 	enum intel_ring_id {
45e3adcf8fSFrançois Tigeot 		RCS = 0x0,
46e3adcf8fSFrançois Tigeot 		VCS,
47e3adcf8fSFrançois Tigeot 		BCS,
48e3adcf8fSFrançois Tigeot 	} id;
49e3adcf8fSFrançois Tigeot #define I915_NUM_RINGS 3
50*15ac6249SFrançois Tigeot 	u32		mmio_base;
51*15ac6249SFrançois Tigeot 	void		__iomem *virtual_start;
52e3adcf8fSFrançois Tigeot 	struct		drm_device *dev;
53e3adcf8fSFrançois Tigeot 	struct		drm_i915_gem_object *obj;
54e3adcf8fSFrançois Tigeot 
55*15ac6249SFrançois Tigeot 	u32		head;
56*15ac6249SFrançois Tigeot 	u32		tail;
57e3adcf8fSFrançois Tigeot 	int		space;
58e3adcf8fSFrançois Tigeot 	int		size;
59e3adcf8fSFrançois Tigeot 	int		effective_size;
60e3adcf8fSFrançois Tigeot 	struct intel_hw_status_page status_page;
61e3adcf8fSFrançois Tigeot 
62e3adcf8fSFrançois Tigeot 	/** We track the position of the requests in the ring buffer, and
63e3adcf8fSFrançois Tigeot 	 * when each is retired we increment last_retired_head as the GPU
64e3adcf8fSFrançois Tigeot 	 * must have finished processing the request and so we know we
65e3adcf8fSFrançois Tigeot 	 * can advance the ringbuffer up to that position.
66e3adcf8fSFrançois Tigeot 	 *
67e3adcf8fSFrançois Tigeot 	 * last_retired_head is set to -1 after the value is consumed so
68e3adcf8fSFrançois Tigeot 	 * we can detect new retirements.
69e3adcf8fSFrançois Tigeot 	 */
70e3adcf8fSFrançois Tigeot 	u32		last_retired_head;
71e3adcf8fSFrançois Tigeot 
72e3adcf8fSFrançois Tigeot 	struct lock	irq_lock;
73b030f26bSFrançois Tigeot 	u32		irq_mask;
74*15ac6249SFrançois Tigeot 	u32		irq_refcount;		/* protected by dev_priv->irq_lock */
75*15ac6249SFrançois Tigeot 	u32		irq_enable_mask;	/* bitmask to enable ring interrupt */
76b030f26bSFrançois Tigeot 	u32		trace_irq_seqno;
77b030f26bSFrançois Tigeot 	u32		sync_seqno[I915_NUM_RINGS-1];
78*15ac6249SFrançois Tigeot 	bool __must_check (*irq_get)(struct intel_ring_buffer *ring);
79e3adcf8fSFrançois Tigeot 	void		(*irq_put)(struct intel_ring_buffer *ring);
80e3adcf8fSFrançois Tigeot 
81e3adcf8fSFrançois Tigeot 	int		(*init)(struct intel_ring_buffer *ring);
82e3adcf8fSFrançois Tigeot 
83e3adcf8fSFrançois Tigeot 	void		(*write_tail)(struct intel_ring_buffer *ring,
84*15ac6249SFrançois Tigeot 				      u32 value);
85*15ac6249SFrançois Tigeot 	int __must_check (*flush)(struct intel_ring_buffer *ring,
86*15ac6249SFrançois Tigeot 				  u32	invalidate_domains,
87*15ac6249SFrançois Tigeot 				  u32	flush_domains);
88e3adcf8fSFrançois Tigeot 	int		(*add_request)(struct intel_ring_buffer *ring,
89e3adcf8fSFrançois Tigeot 				       uint32_t *seqno);
90b030f26bSFrançois Tigeot 	/* Some chipsets are not quite as coherent as advertised and need
91b030f26bSFrançois Tigeot 	 * an expensive kick to force a true read of the up-to-date seqno.
92b030f26bSFrançois Tigeot 	 * However, the up-to-date seqno is not always required and the last
93b030f26bSFrançois Tigeot 	 * seen value is good enough. Note that the seqno will always be
94b030f26bSFrançois Tigeot 	 * monotonic, even if not coherent.
95b030f26bSFrançois Tigeot 	 */
96b030f26bSFrançois Tigeot 	u32		(*get_seqno)(struct intel_ring_buffer *ring,
97b030f26bSFrançois Tigeot 				     bool lazy_coherency);
98e3adcf8fSFrançois Tigeot 	int		(*dispatch_execbuffer)(struct intel_ring_buffer *ring,
99e3adcf8fSFrançois Tigeot 					       uint32_t offset, uint32_t length);
100*15ac6249SFrançois Tigeot #define I915_DISPATCH_SECURE 0x1
101*15ac6249SFrançois Tigeot #define I915_DISPATCH_PINNED 0x2
102e3adcf8fSFrançois Tigeot 	void		(*cleanup)(struct intel_ring_buffer *ring);
103e3adcf8fSFrançois Tigeot 	int		(*sync_to)(struct intel_ring_buffer *ring,
104e3adcf8fSFrançois Tigeot 				   struct intel_ring_buffer *to,
105e3adcf8fSFrançois Tigeot 				   u32 seqno);
106e3adcf8fSFrançois Tigeot 
107e3adcf8fSFrançois Tigeot 	u32		semaphore_register[3]; /*our mbox written by others */
108e3adcf8fSFrançois Tigeot 	u32		signal_mbox[2]; /* mboxes this ring signals to */
109e3adcf8fSFrançois Tigeot 	/**
110e3adcf8fSFrançois Tigeot 	 * List of objects currently involved in rendering from the
111e3adcf8fSFrançois Tigeot 	 * ringbuffer.
112e3adcf8fSFrançois Tigeot 	 *
113e3adcf8fSFrançois Tigeot 	 * Includes buffers having the contents of their GPU caches
114e3adcf8fSFrançois Tigeot 	 * flushed, not necessarily primitives.  last_rendering_seqno
115e3adcf8fSFrançois Tigeot 	 * represents when the rendering involved will be completed.
116e3adcf8fSFrançois Tigeot 	 *
117e3adcf8fSFrançois Tigeot 	 * A reference is held on the buffer while on this list.
118e3adcf8fSFrançois Tigeot 	 */
119e3adcf8fSFrançois Tigeot 	struct list_head active_list;
120e3adcf8fSFrançois Tigeot 
121e3adcf8fSFrançois Tigeot 	/**
122e3adcf8fSFrançois Tigeot 	 * List of breadcrumbs associated with GPU requests currently
123e3adcf8fSFrançois Tigeot 	 * outstanding.
124e3adcf8fSFrançois Tigeot 	 */
125e3adcf8fSFrançois Tigeot 	struct list_head request_list;
126e3adcf8fSFrançois Tigeot 
127e3adcf8fSFrançois Tigeot 	struct list_head gpu_write_list;
128e3adcf8fSFrançois Tigeot 
129e3adcf8fSFrançois Tigeot 	/**
130e3adcf8fSFrançois Tigeot 	 * Do we have some not yet emitted requests outstanding?
131e3adcf8fSFrançois Tigeot 	 */
132*15ac6249SFrançois Tigeot 	u32 outstanding_lazy_request;
133b030f26bSFrançois Tigeot 	bool gpu_caches_dirty;
134b030f26bSFrançois Tigeot 
135b030f26bSFrançois Tigeot 	wait_queue_head_t irq_queue;
136e3adcf8fSFrançois Tigeot 
137e3adcf8fSFrançois Tigeot 	drm_local_map_t map;
138e3adcf8fSFrançois Tigeot 
139*15ac6249SFrançois Tigeot 	/**
140*15ac6249SFrançois Tigeot 	 * Do an explicit TLB flush before MI_SET_CONTEXT
141*15ac6249SFrançois Tigeot 	 */
142*15ac6249SFrançois Tigeot 	bool itlb_before_ctx_switch;
143*15ac6249SFrançois Tigeot 	struct i915_hw_context *default_context;
144*15ac6249SFrançois Tigeot 	struct drm_i915_gem_object *last_context_obj;
145*15ac6249SFrançois Tigeot 
146e3adcf8fSFrançois Tigeot 	void *private;
147e3adcf8fSFrançois Tigeot };
148e3adcf8fSFrançois Tigeot 
149f4e1c372SFrançois Tigeot static inline bool
150f4e1c372SFrançois Tigeot intel_ring_initialized(struct intel_ring_buffer *ring)
151f4e1c372SFrançois Tigeot {
152f4e1c372SFrançois Tigeot 	return ring->obj != NULL;
153f4e1c372SFrançois Tigeot }
154f4e1c372SFrançois Tigeot 
155e3adcf8fSFrançois Tigeot static inline unsigned
156e3adcf8fSFrançois Tigeot intel_ring_flag(struct intel_ring_buffer *ring)
157e3adcf8fSFrançois Tigeot {
158e3adcf8fSFrançois Tigeot 	return 1 << ring->id;
159e3adcf8fSFrançois Tigeot }
160e3adcf8fSFrançois Tigeot 
161f4e1c372SFrançois Tigeot static inline u32
162e3adcf8fSFrançois Tigeot intel_ring_sync_index(struct intel_ring_buffer *ring,
163e3adcf8fSFrançois Tigeot 		      struct intel_ring_buffer *other)
164e3adcf8fSFrançois Tigeot {
165e3adcf8fSFrançois Tigeot 	int idx;
166e3adcf8fSFrançois Tigeot 
167e3adcf8fSFrançois Tigeot 	/*
168e3adcf8fSFrançois Tigeot 	 * cs -> 0 = vcs, 1 = bcs
169e3adcf8fSFrançois Tigeot 	 * vcs -> 0 = bcs, 1 = cs,
170e3adcf8fSFrançois Tigeot 	 * bcs -> 0 = cs, 1 = vcs.
171e3adcf8fSFrançois Tigeot 	 */
172e3adcf8fSFrançois Tigeot 
173e3adcf8fSFrançois Tigeot 	idx = (other - ring) - 1;
174e3adcf8fSFrançois Tigeot 	if (idx < 0)
175e3adcf8fSFrançois Tigeot 		idx += I915_NUM_RINGS;
176e3adcf8fSFrançois Tigeot 
177e3adcf8fSFrançois Tigeot 	return idx;
178e3adcf8fSFrançois Tigeot }
179e3adcf8fSFrançois Tigeot 
180f4e1c372SFrançois Tigeot static inline u32
181f4e1c372SFrançois Tigeot intel_read_status_page(struct intel_ring_buffer *ring,
182f4e1c372SFrançois Tigeot 		       int reg)
183e3adcf8fSFrançois Tigeot {
184f4e1c372SFrançois Tigeot 	/* Ensure that the compiler doesn't optimize away the load. */
185f4e1c372SFrançois Tigeot 	cpu_ccfence();
186f4e1c372SFrançois Tigeot 	return ring->status_page.page_addr[reg];
187e3adcf8fSFrançois Tigeot }
188e3adcf8fSFrançois Tigeot 
189f4e1c372SFrançois Tigeot /**
190f4e1c372SFrançois Tigeot  * Reads a dword out of the status page, which is written to from the command
191f4e1c372SFrançois Tigeot  * queue by automatic updates, MI_REPORT_HEAD, MI_STORE_DATA_INDEX, or
192f4e1c372SFrançois Tigeot  * MI_STORE_DATA_IMM.
193f4e1c372SFrançois Tigeot  *
194f4e1c372SFrançois Tigeot  * The following dwords have a reserved meaning:
195f4e1c372SFrançois Tigeot  * 0x00: ISR copy, updated when an ISR bit not set in the HWSTAM changes.
196f4e1c372SFrançois Tigeot  * 0x04: ring 0 head pointer
197f4e1c372SFrançois Tigeot  * 0x05: ring 1 head pointer (915-class)
198f4e1c372SFrançois Tigeot  * 0x06: ring 2 head pointer (915-class)
199f4e1c372SFrançois Tigeot  * 0x10-0x1b: Context status DWords (GM45)
200f4e1c372SFrançois Tigeot  * 0x1f: Last written status offset. (GM45)
201f4e1c372SFrançois Tigeot  *
202f4e1c372SFrançois Tigeot  * The area from dword 0x20 to 0x3ff is available for driver usage.
203f4e1c372SFrançois Tigeot  */
204f4e1c372SFrançois Tigeot #define I915_GEM_HWS_INDEX		0x20
205f4e1c372SFrançois Tigeot #define I915_GEM_HWS_SCRATCH_INDEX	0x30
206f4e1c372SFrançois Tigeot #define I915_GEM_HWS_SCRATCH_ADDR (I915_GEM_HWS_SCRATCH_INDEX << MI_STORE_DWORD_INDEX_SHIFT)
207e3adcf8fSFrançois Tigeot 
208f4e1c372SFrançois Tigeot void intel_cleanup_ring_buffer(struct intel_ring_buffer *ring);
209f4e1c372SFrançois Tigeot 
210f4e1c372SFrançois Tigeot int __must_check intel_ring_begin(struct intel_ring_buffer *ring, int n);
211e3adcf8fSFrançois Tigeot static inline void intel_ring_emit(struct intel_ring_buffer *ring,
212f4e1c372SFrançois Tigeot 				   u32 data)
213e3adcf8fSFrançois Tigeot {
214f4e1c372SFrançois Tigeot 	iowrite32(data, ring->virtual_start + ring->tail);
215e3adcf8fSFrançois Tigeot 	ring->tail += 4;
216e3adcf8fSFrançois Tigeot }
217e3adcf8fSFrançois Tigeot void intel_ring_advance(struct intel_ring_buffer *ring);
218f4e1c372SFrançois Tigeot int __must_check intel_ring_idle(struct intel_ring_buffer *ring);
219e3adcf8fSFrançois Tigeot 
220f4e1c372SFrançois Tigeot int intel_ring_flush_all_caches(struct intel_ring_buffer *ring);
221f4e1c372SFrançois Tigeot int intel_ring_invalidate_all_caches(struct intel_ring_buffer *ring);
222e3adcf8fSFrançois Tigeot 
223e3adcf8fSFrançois Tigeot int intel_init_render_ring_buffer(struct drm_device *dev);
224e3adcf8fSFrançois Tigeot int intel_init_bsd_ring_buffer(struct drm_device *dev);
225e3adcf8fSFrançois Tigeot int intel_init_blt_ring_buffer(struct drm_device *dev);
226e3adcf8fSFrançois Tigeot 
227e3adcf8fSFrançois Tigeot u32 intel_ring_get_active_head(struct intel_ring_buffer *ring);
228e3adcf8fSFrançois Tigeot void intel_ring_setup_status_page(struct intel_ring_buffer *ring);
229e3adcf8fSFrançois Tigeot 
230e3adcf8fSFrançois Tigeot static inline u32 intel_ring_get_tail(struct intel_ring_buffer *ring)
231e3adcf8fSFrançois Tigeot {
232e3adcf8fSFrançois Tigeot 	return ring->tail;
233e3adcf8fSFrançois Tigeot }
234e3adcf8fSFrançois Tigeot 
235f4e1c372SFrançois Tigeot static inline u32 intel_ring_get_seqno(struct intel_ring_buffer *ring)
236f4e1c372SFrançois Tigeot {
237f4e1c372SFrançois Tigeot 	BUG_ON(ring->outstanding_lazy_request == 0);
238f4e1c372SFrançois Tigeot 	return ring->outstanding_lazy_request;
239f4e1c372SFrançois Tigeot }
240f4e1c372SFrançois Tigeot 
241f4e1c372SFrançois Tigeot static inline void i915_trace_irq_get(struct intel_ring_buffer *ring, u32 seqno)
242f4e1c372SFrançois Tigeot {
243f4e1c372SFrançois Tigeot 	if (ring->trace_irq_seqno == 0 && ring->irq_get(ring))
244f4e1c372SFrançois Tigeot 		ring->trace_irq_seqno = seqno;
245f4e1c372SFrançois Tigeot }
246e3adcf8fSFrançois Tigeot 
247e3adcf8fSFrançois Tigeot /* DRI warts */
248f4e1c372SFrançois Tigeot int intel_render_ring_init_dri(struct drm_device *dev, u64 start, u32 size);
249e3adcf8fSFrançois Tigeot 
250e3adcf8fSFrançois Tigeot #endif /* _INTEL_RINGBUFFER_H_ */
251