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