intel_ringbuffer.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. /* seqno size is actually only a uint32, but since we plan to use MI_FLUSH_DW to
  33. * do the writes, and that must have qw aligned offsets, simply pretend it's 8b.
  34. */
  35. #define i915_semaphore_seqno_size sizeof(uint64_t)
  36. #define GEN8_SIGNAL_OFFSET(__ring, to) \
  37. (i915_gem_obj_ggtt_offset(dev_priv->semaphore_obj) + \
  38. ((__ring)->id * I915_NUM_RINGS * i915_semaphore_seqno_size) + \
  39. (i915_semaphore_seqno_size * (to)))
  40. #define GEN8_WAIT_OFFSET(__ring, from) \
  41. (i915_gem_obj_ggtt_offset(dev_priv->semaphore_obj) + \
  42. ((from) * I915_NUM_RINGS * i915_semaphore_seqno_size) + \
  43. (i915_semaphore_seqno_size * (__ring)->id))
  44. #define GEN8_RING_SEMAPHORE_INIT do { \
  45. if (!dev_priv->semaphore_obj) { \
  46. break; \
  47. } \
  48. ring->semaphore.signal_ggtt[RCS] = GEN8_SIGNAL_OFFSET(ring, RCS); \
  49. ring->semaphore.signal_ggtt[VCS] = GEN8_SIGNAL_OFFSET(ring, VCS); \
  50. ring->semaphore.signal_ggtt[BCS] = GEN8_SIGNAL_OFFSET(ring, BCS); \
  51. ring->semaphore.signal_ggtt[VECS] = GEN8_SIGNAL_OFFSET(ring, VECS); \
  52. ring->semaphore.signal_ggtt[VCS2] = GEN8_SIGNAL_OFFSET(ring, VCS2); \
  53. ring->semaphore.signal_ggtt[ring->id] = MI_SEMAPHORE_SYNC_INVALID; \
  54. } while(0)
  55. enum intel_ring_hangcheck_action {
  56. HANGCHECK_IDLE = 0,
  57. HANGCHECK_WAIT,
  58. HANGCHECK_ACTIVE,
  59. HANGCHECK_ACTIVE_LOOP,
  60. HANGCHECK_KICK,
  61. HANGCHECK_HUNG,
  62. };
  63. #define HANGCHECK_SCORE_RING_HUNG 31
  64. struct intel_ring_hangcheck {
  65. u64 acthd;
  66. u64 max_acthd;
  67. u32 seqno;
  68. int score;
  69. enum intel_ring_hangcheck_action action;
  70. int deadlock;
  71. };
  72. struct intel_ringbuffer {
  73. struct drm_i915_gem_object *obj;
  74. void __iomem *virtual_start;
  75. u32 head;
  76. u32 tail;
  77. int space;
  78. int size;
  79. int effective_size;
  80. /** We track the position of the requests in the ring buffer, and
  81. * when each is retired we increment last_retired_head as the GPU
  82. * must have finished processing the request and so we know we
  83. * can advance the ringbuffer up to that position.
  84. *
  85. * last_retired_head is set to -1 after the value is consumed so
  86. * we can detect new retirements.
  87. */
  88. u32 last_retired_head;
  89. };
  90. struct intel_engine_cs {
  91. const char *name;
  92. enum intel_ring_id {
  93. RCS = 0x0,
  94. VCS,
  95. BCS,
  96. VECS,
  97. VCS2
  98. } id;
  99. #define I915_NUM_RINGS 5
  100. #define LAST_USER_RING (VECS + 1)
  101. u32 mmio_base;
  102. struct drm_device *dev;
  103. struct intel_ringbuffer *buffer;
  104. struct intel_hw_status_page status_page;
  105. unsigned irq_refcount; /* protected by dev_priv->irq_lock */
  106. u32 irq_enable_mask; /* bitmask to enable ring interrupt */
  107. u32 trace_irq_seqno;
  108. bool __must_check (*irq_get)(struct intel_engine_cs *ring);
  109. void (*irq_put)(struct intel_engine_cs *ring);
  110. int (*init)(struct intel_engine_cs *ring);
  111. void (*write_tail)(struct intel_engine_cs *ring,
  112. u32 value);
  113. int __must_check (*flush)(struct intel_engine_cs *ring,
  114. u32 invalidate_domains,
  115. u32 flush_domains);
  116. int (*add_request)(struct intel_engine_cs *ring);
  117. /* Some chipsets are not quite as coherent as advertised and need
  118. * an expensive kick to force a true read of the up-to-date seqno.
  119. * However, the up-to-date seqno is not always required and the last
  120. * seen value is good enough. Note that the seqno will always be
  121. * monotonic, even if not coherent.
  122. */
  123. u32 (*get_seqno)(struct intel_engine_cs *ring,
  124. bool lazy_coherency);
  125. void (*set_seqno)(struct intel_engine_cs *ring,
  126. u32 seqno);
  127. int (*dispatch_execbuffer)(struct intel_engine_cs *ring,
  128. u64 offset, u32 length,
  129. unsigned flags);
  130. #define I915_DISPATCH_SECURE 0x1
  131. #define I915_DISPATCH_PINNED 0x2
  132. void (*cleanup)(struct intel_engine_cs *ring);
  133. /* GEN8 signal/wait table - never trust comments!
  134. * signal to signal to signal to signal to signal to
  135. * RCS VCS BCS VECS VCS2
  136. * --------------------------------------------------------------------
  137. * RCS | NOP (0x00) | VCS (0x08) | BCS (0x10) | VECS (0x18) | VCS2 (0x20) |
  138. * |-------------------------------------------------------------------
  139. * VCS | RCS (0x28) | NOP (0x30) | BCS (0x38) | VECS (0x40) | VCS2 (0x48) |
  140. * |-------------------------------------------------------------------
  141. * BCS | RCS (0x50) | VCS (0x58) | NOP (0x60) | VECS (0x68) | VCS2 (0x70) |
  142. * |-------------------------------------------------------------------
  143. * VECS | RCS (0x78) | VCS (0x80) | BCS (0x88) | NOP (0x90) | VCS2 (0x98) |
  144. * |-------------------------------------------------------------------
  145. * VCS2 | RCS (0xa0) | VCS (0xa8) | BCS (0xb0) | VECS (0xb8) | NOP (0xc0) |
  146. * |-------------------------------------------------------------------
  147. *
  148. * Generalization:
  149. * f(x, y) := (x->id * NUM_RINGS * seqno_size) + (seqno_size * y->id)
  150. * ie. transpose of g(x, y)
  151. *
  152. * sync from sync from sync from sync from sync from
  153. * RCS VCS BCS VECS VCS2
  154. * --------------------------------------------------------------------
  155. * RCS | NOP (0x00) | VCS (0x28) | BCS (0x50) | VECS (0x78) | VCS2 (0xa0) |
  156. * |-------------------------------------------------------------------
  157. * VCS | RCS (0x08) | NOP (0x30) | BCS (0x58) | VECS (0x80) | VCS2 (0xa8) |
  158. * |-------------------------------------------------------------------
  159. * BCS | RCS (0x10) | VCS (0x38) | NOP (0x60) | VECS (0x88) | VCS2 (0xb0) |
  160. * |-------------------------------------------------------------------
  161. * VECS | RCS (0x18) | VCS (0x40) | BCS (0x68) | NOP (0x90) | VCS2 (0xb8) |
  162. * |-------------------------------------------------------------------
  163. * VCS2 | RCS (0x20) | VCS (0x48) | BCS (0x70) | VECS (0x98) | NOP (0xc0) |
  164. * |-------------------------------------------------------------------
  165. *
  166. * Generalization:
  167. * g(x, y) := (y->id * NUM_RINGS * seqno_size) + (seqno_size * x->id)
  168. * ie. transpose of f(x, y)
  169. */
  170. struct {
  171. u32 sync_seqno[I915_NUM_RINGS-1];
  172. union {
  173. struct {
  174. /* our mbox written by others */
  175. u32 wait[I915_NUM_RINGS];
  176. /* mboxes this ring signals to */
  177. u32 signal[I915_NUM_RINGS];
  178. } mbox;
  179. u64 signal_ggtt[I915_NUM_RINGS];
  180. };
  181. /* AKA wait() */
  182. int (*sync_to)(struct intel_engine_cs *ring,
  183. struct intel_engine_cs *to,
  184. u32 seqno);
  185. int (*signal)(struct intel_engine_cs *signaller,
  186. /* num_dwords needed by caller */
  187. unsigned int num_dwords);
  188. } semaphore;
  189. /**
  190. * List of objects currently involved in rendering from the
  191. * ringbuffer.
  192. *
  193. * Includes buffers having the contents of their GPU caches
  194. * flushed, not necessarily primitives. last_rendering_seqno
  195. * represents when the rendering involved will be completed.
  196. *
  197. * A reference is held on the buffer while on this list.
  198. */
  199. struct list_head active_list;
  200. /**
  201. * List of breadcrumbs associated with GPU requests currently
  202. * outstanding.
  203. */
  204. struct list_head request_list;
  205. /**
  206. * Do we have some not yet emitted requests outstanding?
  207. */
  208. struct drm_i915_gem_request *preallocated_lazy_request;
  209. u32 outstanding_lazy_seqno;
  210. bool gpu_caches_dirty;
  211. bool fbc_dirty;
  212. wait_queue_head_t irq_queue;
  213. struct intel_context *default_context;
  214. struct intel_context *last_context;
  215. struct intel_ring_hangcheck hangcheck;
  216. struct {
  217. struct drm_i915_gem_object *obj;
  218. u32 gtt_offset;
  219. volatile u32 *cpu_page;
  220. } scratch;
  221. bool needs_cmd_parser;
  222. /*
  223. * Table of commands the command parser needs to know about
  224. * for this ring.
  225. */
  226. DECLARE_HASHTABLE(cmd_hash, I915_CMD_HASH_ORDER);
  227. /*
  228. * Table of registers allowed in commands that read/write registers.
  229. */
  230. const u32 *reg_table;
  231. int reg_count;
  232. /*
  233. * Table of registers allowed in commands that read/write registers, but
  234. * only from the DRM master.
  235. */
  236. const u32 *master_reg_table;
  237. int master_reg_count;
  238. /*
  239. * Returns the bitmask for the length field of the specified command.
  240. * Return 0 for an unrecognized/invalid command.
  241. *
  242. * If the command parser finds an entry for a command in the ring's
  243. * cmd_tables, it gets the command's length based on the table entry.
  244. * If not, it calls this function to determine the per-ring length field
  245. * encoding for the command (i.e. certain opcode ranges use certain bits
  246. * to encode the command length in the header).
  247. */
  248. u32 (*get_cmd_length_mask)(u32 cmd_header);
  249. };
  250. static inline bool
  251. intel_ring_initialized(struct intel_engine_cs *ring)
  252. {
  253. return ring->buffer && ring->buffer->obj;
  254. }
  255. static inline unsigned
  256. intel_ring_flag(struct intel_engine_cs *ring)
  257. {
  258. return 1 << ring->id;
  259. }
  260. static inline u32
  261. intel_ring_sync_index(struct intel_engine_cs *ring,
  262. struct intel_engine_cs *other)
  263. {
  264. int idx;
  265. /*
  266. * rcs -> 0 = vcs, 1 = bcs, 2 = vecs, 3 = vcs2;
  267. * vcs -> 0 = bcs, 1 = vecs, 2 = vcs2, 3 = rcs;
  268. * bcs -> 0 = vecs, 1 = vcs2. 2 = rcs, 3 = vcs;
  269. * vecs -> 0 = vcs2, 1 = rcs, 2 = vcs, 3 = bcs;
  270. * vcs2 -> 0 = rcs, 1 = vcs, 2 = bcs, 3 = vecs;
  271. */
  272. idx = (other - ring) - 1;
  273. if (idx < 0)
  274. idx += I915_NUM_RINGS;
  275. return idx;
  276. }
  277. static inline u32
  278. intel_read_status_page(struct intel_engine_cs *ring,
  279. int reg)
  280. {
  281. /* Ensure that the compiler doesn't optimize away the load. */
  282. barrier();
  283. return ring->status_page.page_addr[reg];
  284. }
  285. static inline void
  286. intel_write_status_page(struct intel_engine_cs *ring,
  287. int reg, u32 value)
  288. {
  289. ring->status_page.page_addr[reg] = value;
  290. }
  291. /**
  292. * Reads a dword out of the status page, which is written to from the command
  293. * queue by automatic updates, MI_REPORT_HEAD, MI_STORE_DATA_INDEX, or
  294. * MI_STORE_DATA_IMM.
  295. *
  296. * The following dwords have a reserved meaning:
  297. * 0x00: ISR copy, updated when an ISR bit not set in the HWSTAM changes.
  298. * 0x04: ring 0 head pointer
  299. * 0x05: ring 1 head pointer (915-class)
  300. * 0x06: ring 2 head pointer (915-class)
  301. * 0x10-0x1b: Context status DWords (GM45)
  302. * 0x1f: Last written status offset. (GM45)
  303. *
  304. * The area from dword 0x20 to 0x3ff is available for driver usage.
  305. */
  306. #define I915_GEM_HWS_INDEX 0x20
  307. #define I915_GEM_HWS_SCRATCH_INDEX 0x30
  308. #define I915_GEM_HWS_SCRATCH_ADDR (I915_GEM_HWS_SCRATCH_INDEX << MI_STORE_DWORD_INDEX_SHIFT)
  309. void intel_stop_ring_buffer(struct intel_engine_cs *ring);
  310. void intel_cleanup_ring_buffer(struct intel_engine_cs *ring);
  311. int __must_check intel_ring_begin(struct intel_engine_cs *ring, int n);
  312. int __must_check intel_ring_cacheline_align(struct intel_engine_cs *ring);
  313. static inline void intel_ring_emit(struct intel_engine_cs *ring,
  314. u32 data)
  315. {
  316. struct intel_ringbuffer *ringbuf = ring->buffer;
  317. iowrite32(data, ringbuf->virtual_start + ringbuf->tail);
  318. ringbuf->tail += 4;
  319. }
  320. static inline void intel_ring_advance(struct intel_engine_cs *ring)
  321. {
  322. struct intel_ringbuffer *ringbuf = ring->buffer;
  323. ringbuf->tail &= ringbuf->size - 1;
  324. }
  325. void __intel_ring_advance(struct intel_engine_cs *ring);
  326. int __must_check intel_ring_idle(struct intel_engine_cs *ring);
  327. void intel_ring_init_seqno(struct intel_engine_cs *ring, u32 seqno);
  328. int intel_ring_flush_all_caches(struct intel_engine_cs *ring);
  329. int intel_ring_invalidate_all_caches(struct intel_engine_cs *ring);
  330. int intel_init_render_ring_buffer(struct drm_device *dev);
  331. int intel_init_bsd_ring_buffer(struct drm_device *dev);
  332. int intel_init_bsd2_ring_buffer(struct drm_device *dev);
  333. int intel_init_blt_ring_buffer(struct drm_device *dev);
  334. int intel_init_vebox_ring_buffer(struct drm_device *dev);
  335. u64 intel_ring_get_active_head(struct intel_engine_cs *ring);
  336. void intel_ring_setup_status_page(struct intel_engine_cs *ring);
  337. static inline u32 intel_ring_get_tail(struct intel_ringbuffer *ringbuf)
  338. {
  339. return ringbuf->tail;
  340. }
  341. static inline u32 intel_ring_get_seqno(struct intel_engine_cs *ring)
  342. {
  343. BUG_ON(ring->outstanding_lazy_seqno == 0);
  344. return ring->outstanding_lazy_seqno;
  345. }
  346. static inline void i915_trace_irq_get(struct intel_engine_cs *ring, u32 seqno)
  347. {
  348. if (ring->trace_irq_seqno == 0 && ring->irq_get(ring))
  349. ring->trace_irq_seqno = seqno;
  350. }
  351. /* DRI warts */
  352. int intel_render_ring_init_dri(struct drm_device *dev, u64 start, u32 size);
  353. #endif /* _INTEL_RINGBUFFER_H_ */