intel_engine.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /* SPDX-License-Identifier: MIT */
  2. #ifndef _INTEL_RINGBUFFER_H_
  3. #define _INTEL_RINGBUFFER_H_
  4. #include <asm/cacheflush.h>
  5. #include <drm/drm_util.h>
  6. #include <drm/drm_cache.h>
  7. #include <linux/hashtable.h>
  8. #include <linux/irq_work.h>
  9. #include <linux/random.h>
  10. #include <linux/seqlock.h>
  11. #include "i915_pmu.h"
  12. #include "i915_request.h"
  13. #include "i915_selftest.h"
  14. #include "intel_engine_types.h"
  15. #include "intel_gt_types.h"
  16. #include "intel_timeline.h"
  17. #include "intel_workarounds.h"
  18. struct drm_printer;
  19. struct intel_context;
  20. struct intel_gt;
  21. struct lock_class_key;
  22. /* Early gen2 devices have a cacheline of just 32 bytes, using 64 is overkill,
  23. * but keeps the logic simple. Indeed, the whole purpose of this macro is just
  24. * to give some inclination as to some of the magic values used in the various
  25. * workarounds!
  26. */
  27. #define CACHELINE_BYTES 64
  28. #define CACHELINE_DWORDS (CACHELINE_BYTES / sizeof(u32))
  29. #define ENGINE_TRACE(e, fmt, ...) do { \
  30. const struct intel_engine_cs *e__ __maybe_unused = (e); \
  31. GEM_TRACE("%s %s: " fmt, \
  32. dev_name(e__->i915->drm.dev), e__->name, \
  33. ##__VA_ARGS__); \
  34. } while (0)
  35. /*
  36. * The register defines to be used with the following macros need to accept a
  37. * base param, e.g:
  38. *
  39. * REG_FOO(base) _MMIO((base) + <relative offset>)
  40. * ENGINE_READ(engine, REG_FOO);
  41. *
  42. * register arrays are to be defined and accessed as follows:
  43. *
  44. * REG_BAR(base, i) _MMIO((base) + <relative offset> + (i) * <shift>)
  45. * ENGINE_READ_IDX(engine, REG_BAR, i)
  46. */
  47. #define __ENGINE_REG_OP(op__, engine__, ...) \
  48. intel_uncore_##op__((engine__)->uncore, __VA_ARGS__)
  49. #define __ENGINE_READ_OP(op__, engine__, reg__) \
  50. __ENGINE_REG_OP(op__, (engine__), reg__((engine__)->mmio_base))
  51. #define ENGINE_READ16(...) __ENGINE_READ_OP(read16, __VA_ARGS__)
  52. #define ENGINE_READ(...) __ENGINE_READ_OP(read, __VA_ARGS__)
  53. #define ENGINE_READ_FW(...) __ENGINE_READ_OP(read_fw, __VA_ARGS__)
  54. #define ENGINE_POSTING_READ(...) __ENGINE_READ_OP(posting_read_fw, __VA_ARGS__)
  55. #define ENGINE_POSTING_READ16(...) __ENGINE_READ_OP(posting_read16, __VA_ARGS__)
  56. #define ENGINE_READ64(engine__, lower_reg__, upper_reg__) \
  57. __ENGINE_REG_OP(read64_2x32, (engine__), \
  58. lower_reg__((engine__)->mmio_base), \
  59. upper_reg__((engine__)->mmio_base))
  60. #define ENGINE_READ_IDX(engine__, reg__, idx__) \
  61. __ENGINE_REG_OP(read, (engine__), reg__((engine__)->mmio_base, (idx__)))
  62. #define __ENGINE_WRITE_OP(op__, engine__, reg__, val__) \
  63. __ENGINE_REG_OP(op__, (engine__), reg__((engine__)->mmio_base), (val__))
  64. #define ENGINE_WRITE16(...) __ENGINE_WRITE_OP(write16, __VA_ARGS__)
  65. #define ENGINE_WRITE(...) __ENGINE_WRITE_OP(write, __VA_ARGS__)
  66. #define ENGINE_WRITE_FW(...) __ENGINE_WRITE_OP(write_fw, __VA_ARGS__)
  67. #define __HAS_ENGINE(engine_mask, id) ((engine_mask) & BIT(id))
  68. #define HAS_ENGINE(gt, id) __HAS_ENGINE((gt)->info.engine_mask, id)
  69. #define __ENGINE_INSTANCES_MASK(mask, first, count) ({ \
  70. unsigned int first__ = (first); \
  71. unsigned int count__ = (count); \
  72. ((mask) & GENMASK(first__ + count__ - 1, first__)) >> first__; \
  73. })
  74. #define ENGINE_INSTANCES_MASK(gt, first, count) \
  75. __ENGINE_INSTANCES_MASK((gt)->info.engine_mask, first, count)
  76. #define RCS_MASK(gt) \
  77. ENGINE_INSTANCES_MASK(gt, RCS0, I915_MAX_RCS)
  78. #define BCS_MASK(gt) \
  79. ENGINE_INSTANCES_MASK(gt, BCS0, I915_MAX_BCS)
  80. #define VDBOX_MASK(gt) \
  81. ENGINE_INSTANCES_MASK(gt, VCS0, I915_MAX_VCS)
  82. #define VEBOX_MASK(gt) \
  83. ENGINE_INSTANCES_MASK(gt, VECS0, I915_MAX_VECS)
  84. #define CCS_MASK(gt) \
  85. ENGINE_INSTANCES_MASK(gt, CCS0, I915_MAX_CCS)
  86. #define GEN6_RING_FAULT_REG_READ(engine__) \
  87. intel_uncore_read((engine__)->uncore, RING_FAULT_REG(engine__))
  88. #define GEN6_RING_FAULT_REG_POSTING_READ(engine__) \
  89. intel_uncore_posting_read((engine__)->uncore, RING_FAULT_REG(engine__))
  90. #define GEN6_RING_FAULT_REG_RMW(engine__, clear__, set__) \
  91. ({ \
  92. u32 __val; \
  93. \
  94. __val = intel_uncore_read((engine__)->uncore, \
  95. RING_FAULT_REG(engine__)); \
  96. __val &= ~(clear__); \
  97. __val |= (set__); \
  98. intel_uncore_write((engine__)->uncore, RING_FAULT_REG(engine__), \
  99. __val); \
  100. })
  101. /* seqno size is actually only a uint32, but since we plan to use MI_FLUSH_DW to
  102. * do the writes, and that must have qw aligned offsets, simply pretend it's 8b.
  103. */
  104. static inline unsigned int
  105. execlists_num_ports(const struct intel_engine_execlists * const execlists)
  106. {
  107. return execlists->port_mask + 1;
  108. }
  109. static inline struct i915_request *
  110. execlists_active(const struct intel_engine_execlists *execlists)
  111. {
  112. struct i915_request * const *cur, * const *old, *active;
  113. cur = READ_ONCE(execlists->active);
  114. smp_rmb(); /* pairs with overwrite protection in process_csb() */
  115. do {
  116. old = cur;
  117. active = READ_ONCE(*cur);
  118. cur = READ_ONCE(execlists->active);
  119. smp_rmb(); /* and complete the seqlock retry */
  120. } while (unlikely(cur != old));
  121. return active;
  122. }
  123. static inline u32
  124. intel_read_status_page(const struct intel_engine_cs *engine, int reg)
  125. {
  126. /* Ensure that the compiler doesn't optimize away the load. */
  127. return READ_ONCE(engine->status_page.addr[reg]);
  128. }
  129. static inline void
  130. intel_write_status_page(struct intel_engine_cs *engine, int reg, u32 value)
  131. {
  132. /* Writing into the status page should be done sparingly. Since
  133. * we do when we are uncertain of the device state, we take a bit
  134. * of extra paranoia to try and ensure that the HWS takes the value
  135. * we give and that it doesn't end up trapped inside the CPU!
  136. */
  137. drm_clflush_virt_range(&engine->status_page.addr[reg], sizeof(value));
  138. WRITE_ONCE(engine->status_page.addr[reg], value);
  139. drm_clflush_virt_range(&engine->status_page.addr[reg], sizeof(value));
  140. }
  141. /*
  142. * Reads a dword out of the status page, which is written to from the command
  143. * queue by automatic updates, MI_REPORT_HEAD, MI_STORE_DATA_INDEX, or
  144. * MI_STORE_DATA_IMM.
  145. *
  146. * The following dwords have a reserved meaning:
  147. * 0x00: ISR copy, updated when an ISR bit not set in the HWSTAM changes.
  148. * 0x04: ring 0 head pointer
  149. * 0x05: ring 1 head pointer (915-class)
  150. * 0x06: ring 2 head pointer (915-class)
  151. * 0x10-0x1b: Context status DWords (GM45)
  152. * 0x1f: Last written status offset. (GM45)
  153. * 0x20-0x2f: Reserved (Gen6+)
  154. *
  155. * The area from dword 0x30 to 0x3ff is available for driver usage.
  156. */
  157. #define I915_GEM_HWS_PREEMPT 0x32
  158. #define I915_GEM_HWS_PREEMPT_ADDR (I915_GEM_HWS_PREEMPT * sizeof(u32))
  159. #define I915_GEM_HWS_SEQNO 0x40
  160. #define I915_GEM_HWS_SEQNO_ADDR (I915_GEM_HWS_SEQNO * sizeof(u32))
  161. #define I915_GEM_HWS_MIGRATE (0x42 * sizeof(u32))
  162. #define I915_GEM_HWS_GGTT_BIND 0x46
  163. #define I915_GEM_HWS_GGTT_BIND_ADDR (I915_GEM_HWS_GGTT_BIND * sizeof(u32))
  164. #define I915_GEM_HWS_PXP 0x60
  165. #define I915_GEM_HWS_PXP_ADDR (I915_GEM_HWS_PXP * sizeof(u32))
  166. #define I915_GEM_HWS_GSC 0x62
  167. #define I915_GEM_HWS_GSC_ADDR (I915_GEM_HWS_GSC * sizeof(u32))
  168. #define I915_GEM_HWS_SCRATCH 0x80
  169. #define I915_HWS_CSB_BUF0_INDEX 0x10
  170. #define I915_HWS_CSB_WRITE_INDEX 0x1f
  171. #define ICL_HWS_CSB_WRITE_INDEX 0x2f
  172. #define INTEL_HWS_CSB_WRITE_INDEX(__i915) \
  173. (GRAPHICS_VER(__i915) >= 11 ? ICL_HWS_CSB_WRITE_INDEX : I915_HWS_CSB_WRITE_INDEX)
  174. void intel_engine_stop(struct intel_engine_cs *engine);
  175. void intel_engine_cleanup(struct intel_engine_cs *engine);
  176. int intel_engines_init_mmio(struct intel_gt *gt);
  177. int intel_engines_init(struct intel_gt *gt);
  178. void intel_engine_free_request_pool(struct intel_engine_cs *engine);
  179. void intel_engines_release(struct intel_gt *gt);
  180. void intel_engines_free(struct intel_gt *gt);
  181. int intel_engine_init_common(struct intel_engine_cs *engine);
  182. void intel_engine_cleanup_common(struct intel_engine_cs *engine);
  183. int intel_engine_resume(struct intel_engine_cs *engine);
  184. int intel_ring_submission_setup(struct intel_engine_cs *engine);
  185. int intel_engine_stop_cs(struct intel_engine_cs *engine);
  186. void intel_engine_cancel_stop_cs(struct intel_engine_cs *engine);
  187. void intel_engine_wait_for_pending_mi_fw(struct intel_engine_cs *engine);
  188. void intel_engine_set_hwsp_writemask(struct intel_engine_cs *engine, u32 mask);
  189. u64 intel_engine_get_active_head(const struct intel_engine_cs *engine);
  190. u64 intel_engine_get_last_batch_head(const struct intel_engine_cs *engine);
  191. void intel_engine_get_instdone(const struct intel_engine_cs *engine,
  192. struct intel_instdone *instdone);
  193. void intel_engine_init_execlists(struct intel_engine_cs *engine);
  194. bool intel_engine_irq_enable(struct intel_engine_cs *engine);
  195. void intel_engine_irq_disable(struct intel_engine_cs *engine);
  196. static inline void __intel_engine_reset(struct intel_engine_cs *engine,
  197. bool stalled)
  198. {
  199. if (engine->reset.rewind)
  200. engine->reset.rewind(engine, stalled);
  201. engine->serial++; /* contexts lost */
  202. }
  203. bool intel_engines_are_idle(struct intel_gt *gt);
  204. bool intel_engine_is_idle(struct intel_engine_cs *engine);
  205. void __intel_engine_flush_submission(struct intel_engine_cs *engine, bool sync);
  206. static inline void intel_engine_flush_submission(struct intel_engine_cs *engine)
  207. {
  208. __intel_engine_flush_submission(engine, true);
  209. }
  210. void intel_engines_reset_default_submission(struct intel_gt *gt);
  211. bool intel_engine_can_store_dword(struct intel_engine_cs *engine);
  212. __printf(3, 4)
  213. void intel_engine_dump(struct intel_engine_cs *engine,
  214. struct drm_printer *m,
  215. const char *header, ...);
  216. void intel_engine_dump_active_requests(struct list_head *requests,
  217. struct i915_request *hung_rq,
  218. struct drm_printer *m);
  219. ktime_t intel_engine_get_busy_time(struct intel_engine_cs *engine,
  220. ktime_t *now);
  221. void intel_engine_get_hung_entity(struct intel_engine_cs *engine,
  222. struct intel_context **ce, struct i915_request **rq);
  223. u32 intel_engine_context_size(struct intel_gt *gt, u8 class);
  224. struct intel_context *
  225. intel_engine_create_pinned_context(struct intel_engine_cs *engine,
  226. struct i915_address_space *vm,
  227. unsigned int ring_size,
  228. unsigned int hwsp,
  229. struct lock_class_key *key,
  230. const char *name);
  231. void intel_engine_destroy_pinned_context(struct intel_context *ce);
  232. void xehp_enable_ccs_engines(struct intel_engine_cs *engine);
  233. #define ENGINE_PHYSICAL 0
  234. #define ENGINE_MOCK 1
  235. #define ENGINE_VIRTUAL 2
  236. static inline bool intel_engine_uses_guc(const struct intel_engine_cs *engine)
  237. {
  238. return engine->gt->submission_method >= INTEL_SUBMISSION_GUC;
  239. }
  240. static inline bool
  241. intel_engine_has_preempt_reset(const struct intel_engine_cs *engine)
  242. {
  243. if (!CONFIG_DRM_I915_PREEMPT_TIMEOUT)
  244. return false;
  245. return intel_engine_has_preemption(engine);
  246. }
  247. #define FORCE_VIRTUAL BIT(0)
  248. struct intel_context *
  249. intel_engine_create_virtual(struct intel_engine_cs **siblings,
  250. unsigned int count, unsigned long flags);
  251. static inline struct intel_context *
  252. intel_engine_create_parallel(struct intel_engine_cs **engines,
  253. unsigned int num_engines,
  254. unsigned int width)
  255. {
  256. GEM_BUG_ON(!engines[0]->cops->create_parallel);
  257. return engines[0]->cops->create_parallel(engines, num_engines, width);
  258. }
  259. static inline bool
  260. intel_virtual_engine_has_heartbeat(const struct intel_engine_cs *engine)
  261. {
  262. /*
  263. * For non-GuC submission we expect the back-end to look at the
  264. * heartbeat status of the actual physical engine that the work
  265. * has been (or is being) scheduled on, so we should only reach
  266. * here with GuC submission enabled.
  267. */
  268. GEM_BUG_ON(!intel_engine_uses_guc(engine));
  269. return intel_guc_virtual_engine_has_heartbeat(engine);
  270. }
  271. static inline bool
  272. intel_engine_has_heartbeat(const struct intel_engine_cs *engine)
  273. {
  274. if (!CONFIG_DRM_I915_HEARTBEAT_INTERVAL)
  275. return false;
  276. if (intel_engine_is_virtual(engine))
  277. return intel_virtual_engine_has_heartbeat(engine);
  278. else
  279. return READ_ONCE(engine->props.heartbeat_interval_ms);
  280. }
  281. static inline struct intel_engine_cs *
  282. intel_engine_get_sibling(struct intel_engine_cs *engine, unsigned int sibling)
  283. {
  284. GEM_BUG_ON(!intel_engine_is_virtual(engine));
  285. return engine->cops->get_sibling(engine, sibling);
  286. }
  287. static inline void
  288. intel_engine_set_hung_context(struct intel_engine_cs *engine,
  289. struct intel_context *ce)
  290. {
  291. engine->hung_ce = ce;
  292. }
  293. static inline void
  294. intel_engine_clear_hung_context(struct intel_engine_cs *engine)
  295. {
  296. intel_engine_set_hung_context(engine, NULL);
  297. }
  298. static inline struct intel_context *
  299. intel_engine_get_hung_context(struct intel_engine_cs *engine)
  300. {
  301. return engine->hung_ce;
  302. }
  303. u64 intel_clamp_heartbeat_interval_ms(struct intel_engine_cs *engine, u64 value);
  304. u64 intel_clamp_max_busywait_duration_ns(struct intel_engine_cs *engine, u64 value);
  305. u64 intel_clamp_preempt_timeout_ms(struct intel_engine_cs *engine, u64 value);
  306. u64 intel_clamp_stop_timeout_ms(struct intel_engine_cs *engine, u64 value);
  307. u64 intel_clamp_timeslice_duration_ms(struct intel_engine_cs *engine, u64 value);
  308. #define rb_to_uabi_engine(rb) \
  309. rb_entry_safe(rb, struct intel_engine_cs, uabi_node)
  310. #define for_each_uabi_engine(engine__, i915__) \
  311. for ((engine__) = rb_to_uabi_engine(rb_first(&(i915__)->uabi_engines));\
  312. (engine__); \
  313. (engine__) = rb_to_uabi_engine(rb_next(&(engine__)->uabi_node)))
  314. #endif /* _INTEL_RINGBUFFER_H_ */