intel_ringbuffer.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. #ifndef _INTEL_RINGBUFFER_H_
  2. #define _INTEL_RINGBUFFER_H_
  3. #include <linux/hashtable.h>
  4. #define I915_CMD_HASH_ORDER 9
  5. /*
  6. * Gen2 BSpec "1. Programming Environment" / 1.4.4.6 "Ring Buffer Use"
  7. * Gen3 BSpec "vol1c Memory Interface Functions" / 2.3.4.5 "Ring Buffer Use"
  8. * Gen4+ BSpec "vol1c Memory Interface and Command Stream" / 5.3.4.5 "Ring Buffer Use"
  9. *
  10. * "If the Ring Buffer Head Pointer and the Tail Pointer are on the same
  11. * cacheline, the Head Pointer must not be greater than the Tail
  12. * Pointer."
  13. */
  14. #define I915_RING_FREE_SPACE 64
  15. struct intel_hw_status_page {
  16. u32 *page_addr;
  17. unsigned int gfx_addr;
  18. struct drm_i915_gem_object *obj;
  19. };
  20. #define I915_READ_TAIL(ring) I915_READ(RING_TAIL((ring)->mmio_base))
  21. #define I915_WRITE_TAIL(ring, val) I915_WRITE(RING_TAIL((ring)->mmio_base), val)
  22. #define I915_READ_START(ring) I915_READ(RING_START((ring)->mmio_base))
  23. #define I915_WRITE_START(ring, val) I915_WRITE(RING_START((ring)->mmio_base), val)
  24. #define I915_READ_HEAD(ring) I915_READ(RING_HEAD((ring)->mmio_base))
  25. #define I915_WRITE_HEAD(ring, val) I915_WRITE(RING_HEAD((ring)->mmio_base), val)
  26. #define I915_READ_CTL(ring) I915_READ(RING_CTL((ring)->mmio_base))
  27. #define I915_WRITE_CTL(ring, val) I915_WRITE(RING_CTL((ring)->mmio_base), val)
  28. #define I915_READ_IMR(ring) I915_READ(RING_IMR((ring)->mmio_base))
  29. #define I915_WRITE_IMR(ring, val) I915_WRITE(RING_IMR((ring)->mmio_base), val)
  30. #define I915_READ_MODE(ring) I915_READ(RING_MI_MODE((ring)->mmio_base))
  31. #define I915_WRITE_MODE(ring, val) I915_WRITE(RING_MI_MODE((ring)->mmio_base), val)
  32. enum intel_ring_hangcheck_action {
  33. HANGCHECK_IDLE = 0,
  34. HANGCHECK_WAIT,
  35. HANGCHECK_ACTIVE,
  36. HANGCHECK_KICK,
  37. HANGCHECK_HUNG,
  38. };
  39. #define HANGCHECK_SCORE_RING_HUNG 31
  40. struct intel_ring_hangcheck {
  41. u64 acthd;
  42. u32 seqno;
  43. int score;
  44. enum intel_ring_hangcheck_action action;
  45. int deadlock;
  46. };
  47. struct intel_ringbuffer {
  48. struct drm_i915_gem_object *obj;
  49. void __iomem *virtual_start;
  50. u32 head;
  51. u32 tail;
  52. int space;
  53. int size;
  54. int effective_size;
  55. /** We track the position of the requests in the ring buffer, and
  56. * when each is retired we increment last_retired_head as the GPU
  57. * must have finished processing the request and so we know we
  58. * can advance the ringbuffer up to that position.
  59. *
  60. * last_retired_head is set to -1 after the value is consumed so
  61. * we can detect new retirements.
  62. */
  63. u32 last_retired_head;
  64. };
  65. struct intel_engine_cs {
  66. const char *name;
  67. enum intel_ring_id {
  68. RCS = 0x0,
  69. VCS,
  70. BCS,
  71. VECS,
  72. VCS2
  73. } id;
  74. #define I915_NUM_RINGS 5
  75. #define LAST_USER_RING (VECS + 1)
  76. u32 mmio_base;
  77. struct drm_device *dev;
  78. struct intel_ringbuffer *buffer;
  79. struct intel_hw_status_page status_page;
  80. unsigned irq_refcount; /* protected by dev_priv->irq_lock */
  81. u32 irq_enable_mask; /* bitmask to enable ring interrupt */
  82. u32 trace_irq_seqno;
  83. bool __must_check (*irq_get)(struct intel_engine_cs *ring);
  84. void (*irq_put)(struct intel_engine_cs *ring);
  85. int (*init)(struct intel_engine_cs *ring);
  86. void (*write_tail)(struct intel_engine_cs *ring,
  87. u32 value);
  88. int __must_check (*flush)(struct intel_engine_cs *ring,
  89. u32 invalidate_domains,
  90. u32 flush_domains);
  91. int (*add_request)(struct intel_engine_cs *ring);
  92. /* Some chipsets are not quite as coherent as advertised and need
  93. * an expensive kick to force a true read of the up-to-date seqno.
  94. * However, the up-to-date seqno is not always required and the last
  95. * seen value is good enough. Note that the seqno will always be
  96. * monotonic, even if not coherent.
  97. */
  98. u32 (*get_seqno)(struct intel_engine_cs *ring,
  99. bool lazy_coherency);
  100. void (*set_seqno)(struct intel_engine_cs *ring,
  101. u32 seqno);
  102. int (*dispatch_execbuffer)(struct intel_engine_cs *ring,
  103. u64 offset, u32 length,
  104. unsigned flags);
  105. #define I915_DISPATCH_SECURE 0x1
  106. #define I915_DISPATCH_PINNED 0x2
  107. void (*cleanup)(struct intel_engine_cs *ring);
  108. struct {
  109. u32 sync_seqno[I915_NUM_RINGS-1];
  110. struct {
  111. /* our mbox written by others */
  112. u32 wait[I915_NUM_RINGS];
  113. /* mboxes this ring signals to */
  114. u32 signal[I915_NUM_RINGS];
  115. } mbox;
  116. /* AKA wait() */
  117. int (*sync_to)(struct intel_engine_cs *ring,
  118. struct intel_engine_cs *to,
  119. u32 seqno);
  120. int (*signal)(struct intel_engine_cs *signaller,
  121. /* num_dwords needed by caller */
  122. unsigned int num_dwords);
  123. } semaphore;
  124. /**
  125. * List of objects currently involved in rendering from the
  126. * ringbuffer.
  127. *
  128. * Includes buffers having the contents of their GPU caches
  129. * flushed, not necessarily primitives. last_rendering_seqno
  130. * represents when the rendering involved will be completed.
  131. *
  132. * A reference is held on the buffer while on this list.
  133. */
  134. struct list_head active_list;
  135. /**
  136. * List of breadcrumbs associated with GPU requests currently
  137. * outstanding.
  138. */
  139. struct list_head request_list;
  140. /**
  141. * Do we have some not yet emitted requests outstanding?
  142. */
  143. struct drm_i915_gem_request *preallocated_lazy_request;
  144. u32 outstanding_lazy_seqno;
  145. bool gpu_caches_dirty;
  146. bool fbc_dirty;
  147. wait_queue_head_t irq_queue;
  148. struct intel_context *default_context;
  149. struct intel_context *last_context;
  150. struct intel_ring_hangcheck hangcheck;
  151. struct {
  152. struct drm_i915_gem_object *obj;
  153. u32 gtt_offset;
  154. volatile u32 *cpu_page;
  155. } scratch;
  156. bool needs_cmd_parser;
  157. /*
  158. * Table of commands the command parser needs to know about
  159. * for this ring.
  160. */
  161. DECLARE_HASHTABLE(cmd_hash, I915_CMD_HASH_ORDER);
  162. /*
  163. * Table of registers allowed in commands that read/write registers.
  164. */
  165. const u32 *reg_table;
  166. int reg_count;
  167. /*
  168. * Table of registers allowed in commands that read/write registers, but
  169. * only from the DRM master.
  170. */
  171. const u32 *master_reg_table;
  172. int master_reg_count;
  173. /*
  174. * Returns the bitmask for the length field of the specified command.
  175. * Return 0 for an unrecognized/invalid command.
  176. *
  177. * If the command parser finds an entry for a command in the ring's
  178. * cmd_tables, it gets the command's length based on the table entry.
  179. * If not, it calls this function to determine the per-ring length field
  180. * encoding for the command (i.e. certain opcode ranges use certain bits
  181. * to encode the command length in the header).
  182. */
  183. u32 (*get_cmd_length_mask)(u32 cmd_header);
  184. };
  185. static inline bool
  186. intel_ring_initialized(struct intel_engine_cs *ring)
  187. {
  188. return ring->buffer && ring->buffer->obj;
  189. }
  190. static inline unsigned
  191. intel_ring_flag(struct intel_engine_cs *ring)
  192. {
  193. return 1 << ring->id;
  194. }
  195. static inline u32
  196. intel_ring_sync_index(struct intel_engine_cs *ring,
  197. struct intel_engine_cs *other)
  198. {
  199. int idx;
  200. /*
  201. * cs -> 0 = vcs, 1 = bcs
  202. * vcs -> 0 = bcs, 1 = cs,
  203. * bcs -> 0 = cs, 1 = vcs.
  204. */
  205. idx = (other - ring) - 1;
  206. if (idx < 0)
  207. idx += I915_NUM_RINGS;
  208. return idx;
  209. }
  210. static inline u32
  211. intel_read_status_page(struct intel_engine_cs *ring,
  212. int reg)
  213. {
  214. /* Ensure that the compiler doesn't optimize away the load. */
  215. barrier();
  216. return ring->status_page.page_addr[reg];
  217. }
  218. static inline void
  219. intel_write_status_page(struct intel_engine_cs *ring,
  220. int reg, u32 value)
  221. {
  222. ring->status_page.page_addr[reg] = value;
  223. }
  224. /**
  225. * Reads a dword out of the status page, which is written to from the command
  226. * queue by automatic updates, MI_REPORT_HEAD, MI_STORE_DATA_INDEX, or
  227. * MI_STORE_DATA_IMM.
  228. *
  229. * The following dwords have a reserved meaning:
  230. * 0x00: ISR copy, updated when an ISR bit not set in the HWSTAM changes.
  231. * 0x04: ring 0 head pointer
  232. * 0x05: ring 1 head pointer (915-class)
  233. * 0x06: ring 2 head pointer (915-class)
  234. * 0x10-0x1b: Context status DWords (GM45)
  235. * 0x1f: Last written status offset. (GM45)
  236. *
  237. * The area from dword 0x20 to 0x3ff is available for driver usage.
  238. */
  239. #define I915_GEM_HWS_INDEX 0x20
  240. #define I915_GEM_HWS_SCRATCH_INDEX 0x30
  241. #define I915_GEM_HWS_SCRATCH_ADDR (I915_GEM_HWS_SCRATCH_INDEX << MI_STORE_DWORD_INDEX_SHIFT)
  242. void intel_stop_ring_buffer(struct intel_engine_cs *ring);
  243. void intel_cleanup_ring_buffer(struct intel_engine_cs *ring);
  244. int __must_check intel_ring_begin(struct intel_engine_cs *ring, int n);
  245. int __must_check intel_ring_cacheline_align(struct intel_engine_cs *ring);
  246. static inline void intel_ring_emit(struct intel_engine_cs *ring,
  247. u32 data)
  248. {
  249. struct intel_ringbuffer *ringbuf = ring->buffer;
  250. iowrite32(data, ringbuf->virtual_start + ringbuf->tail);
  251. ringbuf->tail += 4;
  252. }
  253. static inline void intel_ring_advance(struct intel_engine_cs *ring)
  254. {
  255. struct intel_ringbuffer *ringbuf = ring->buffer;
  256. ringbuf->tail &= ringbuf->size - 1;
  257. }
  258. void __intel_ring_advance(struct intel_engine_cs *ring);
  259. int __must_check intel_ring_idle(struct intel_engine_cs *ring);
  260. void intel_ring_init_seqno(struct intel_engine_cs *ring, u32 seqno);
  261. int intel_ring_flush_all_caches(struct intel_engine_cs *ring);
  262. int intel_ring_invalidate_all_caches(struct intel_engine_cs *ring);
  263. int intel_init_render_ring_buffer(struct drm_device *dev);
  264. int intel_init_bsd_ring_buffer(struct drm_device *dev);
  265. int intel_init_bsd2_ring_buffer(struct drm_device *dev);
  266. int intel_init_blt_ring_buffer(struct drm_device *dev);
  267. int intel_init_vebox_ring_buffer(struct drm_device *dev);
  268. u64 intel_ring_get_active_head(struct intel_engine_cs *ring);
  269. void intel_ring_setup_status_page(struct intel_engine_cs *ring);
  270. static inline u32 intel_ring_get_tail(struct intel_engine_cs *ring)
  271. {
  272. return ring->buffer->tail;
  273. }
  274. static inline u32 intel_ring_get_seqno(struct intel_engine_cs *ring)
  275. {
  276. BUG_ON(ring->outstanding_lazy_seqno == 0);
  277. return ring->outstanding_lazy_seqno;
  278. }
  279. static inline void i915_trace_irq_get(struct intel_engine_cs *ring, u32 seqno)
  280. {
  281. if (ring->trace_irq_seqno == 0 && ring->irq_get(ring))
  282. ring->trace_irq_seqno = seqno;
  283. }
  284. /* DRI warts */
  285. int intel_render_ring_init_dri(struct drm_device *dev, u64 start, u32 size);
  286. #endif /* _INTEL_RINGBUFFER_H_ */