intel_ringbuffer.h 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _INTEL_RINGBUFFER_H_
  3. #define _INTEL_RINGBUFFER_H_
  4. #include <linux/hashtable.h>
  5. #include "i915_gem_batch_pool.h"
  6. #include "i915_gem_timeline.h"
  7. #include "i915_pmu.h"
  8. #include "i915_request.h"
  9. #include "i915_selftest.h"
  10. struct drm_printer;
  11. #define I915_CMD_HASH_ORDER 9
  12. /* Early gen2 devices have a cacheline of just 32 bytes, using 64 is overkill,
  13. * but keeps the logic simple. Indeed, the whole purpose of this macro is just
  14. * to give some inclination as to some of the magic values used in the various
  15. * workarounds!
  16. */
  17. #define CACHELINE_BYTES 64
  18. #define CACHELINE_DWORDS (CACHELINE_BYTES / sizeof(uint32_t))
  19. struct intel_hw_status_page {
  20. struct i915_vma *vma;
  21. u32 *page_addr;
  22. u32 ggtt_offset;
  23. };
  24. #define I915_READ_TAIL(engine) I915_READ(RING_TAIL((engine)->mmio_base))
  25. #define I915_WRITE_TAIL(engine, val) I915_WRITE(RING_TAIL((engine)->mmio_base), val)
  26. #define I915_READ_START(engine) I915_READ(RING_START((engine)->mmio_base))
  27. #define I915_WRITE_START(engine, val) I915_WRITE(RING_START((engine)->mmio_base), val)
  28. #define I915_READ_HEAD(engine) I915_READ(RING_HEAD((engine)->mmio_base))
  29. #define I915_WRITE_HEAD(engine, val) I915_WRITE(RING_HEAD((engine)->mmio_base), val)
  30. #define I915_READ_CTL(engine) I915_READ(RING_CTL((engine)->mmio_base))
  31. #define I915_WRITE_CTL(engine, val) I915_WRITE(RING_CTL((engine)->mmio_base), val)
  32. #define I915_READ_IMR(engine) I915_READ(RING_IMR((engine)->mmio_base))
  33. #define I915_WRITE_IMR(engine, val) I915_WRITE(RING_IMR((engine)->mmio_base), val)
  34. #define I915_READ_MODE(engine) I915_READ(RING_MI_MODE((engine)->mmio_base))
  35. #define I915_WRITE_MODE(engine, val) I915_WRITE(RING_MI_MODE((engine)->mmio_base), val)
  36. /* seqno size is actually only a uint32, but since we plan to use MI_FLUSH_DW to
  37. * do the writes, and that must have qw aligned offsets, simply pretend it's 8b.
  38. */
  39. enum intel_engine_hangcheck_action {
  40. ENGINE_IDLE = 0,
  41. ENGINE_WAIT,
  42. ENGINE_ACTIVE_SEQNO,
  43. ENGINE_ACTIVE_HEAD,
  44. ENGINE_ACTIVE_SUBUNITS,
  45. ENGINE_WAIT_KICK,
  46. ENGINE_DEAD,
  47. };
  48. static inline const char *
  49. hangcheck_action_to_str(const enum intel_engine_hangcheck_action a)
  50. {
  51. switch (a) {
  52. case ENGINE_IDLE:
  53. return "idle";
  54. case ENGINE_WAIT:
  55. return "wait";
  56. case ENGINE_ACTIVE_SEQNO:
  57. return "active seqno";
  58. case ENGINE_ACTIVE_HEAD:
  59. return "active head";
  60. case ENGINE_ACTIVE_SUBUNITS:
  61. return "active subunits";
  62. case ENGINE_WAIT_KICK:
  63. return "wait kick";
  64. case ENGINE_DEAD:
  65. return "dead";
  66. }
  67. return "unknown";
  68. }
  69. #define I915_MAX_SLICES 3
  70. #define I915_MAX_SUBSLICES 3
  71. #define instdone_slice_mask(dev_priv__) \
  72. (INTEL_GEN(dev_priv__) == 7 ? \
  73. 1 : INTEL_INFO(dev_priv__)->sseu.slice_mask)
  74. #define instdone_subslice_mask(dev_priv__) \
  75. (INTEL_GEN(dev_priv__) == 7 ? \
  76. 1 : INTEL_INFO(dev_priv__)->sseu.subslice_mask[0])
  77. #define for_each_instdone_slice_subslice(dev_priv__, slice__, subslice__) \
  78. for ((slice__) = 0, (subslice__) = 0; \
  79. (slice__) < I915_MAX_SLICES; \
  80. (subslice__) = ((subslice__) + 1) < I915_MAX_SUBSLICES ? (subslice__) + 1 : 0, \
  81. (slice__) += ((subslice__) == 0)) \
  82. for_each_if((BIT(slice__) & instdone_slice_mask(dev_priv__)) && \
  83. (BIT(subslice__) & instdone_subslice_mask(dev_priv__)))
  84. struct intel_instdone {
  85. u32 instdone;
  86. /* The following exist only in the RCS engine */
  87. u32 slice_common;
  88. u32 sampler[I915_MAX_SLICES][I915_MAX_SUBSLICES];
  89. u32 row[I915_MAX_SLICES][I915_MAX_SUBSLICES];
  90. };
  91. struct intel_engine_hangcheck {
  92. u64 acthd;
  93. u32 seqno;
  94. enum intel_engine_hangcheck_action action;
  95. unsigned long action_timestamp;
  96. int deadlock;
  97. struct intel_instdone instdone;
  98. struct i915_request *active_request;
  99. bool stalled;
  100. };
  101. struct intel_ring {
  102. struct i915_vma *vma;
  103. void *vaddr;
  104. struct list_head request_list;
  105. u32 head;
  106. u32 tail;
  107. u32 emit;
  108. u32 space;
  109. u32 size;
  110. u32 effective_size;
  111. };
  112. struct i915_gem_context;
  113. struct drm_i915_reg_table;
  114. /*
  115. * we use a single page to load ctx workarounds so all of these
  116. * values are referred in terms of dwords
  117. *
  118. * struct i915_wa_ctx_bb:
  119. * offset: specifies batch starting position, also helpful in case
  120. * if we want to have multiple batches at different offsets based on
  121. * some criteria. It is not a requirement at the moment but provides
  122. * an option for future use.
  123. * size: size of the batch in DWORDS
  124. */
  125. struct i915_ctx_workarounds {
  126. struct i915_wa_ctx_bb {
  127. u32 offset;
  128. u32 size;
  129. } indirect_ctx, per_ctx;
  130. struct i915_vma *vma;
  131. };
  132. struct i915_request;
  133. #define I915_MAX_VCS 4
  134. #define I915_MAX_VECS 2
  135. /*
  136. * Engine IDs definitions.
  137. * Keep instances of the same type engine together.
  138. */
  139. enum intel_engine_id {
  140. RCS = 0,
  141. BCS,
  142. VCS,
  143. VCS2,
  144. VCS3,
  145. VCS4,
  146. #define _VCS(n) (VCS + (n))
  147. VECS,
  148. VECS2
  149. #define _VECS(n) (VECS + (n))
  150. };
  151. struct i915_priolist {
  152. struct rb_node node;
  153. struct list_head requests;
  154. int priority;
  155. };
  156. /**
  157. * struct intel_engine_execlists - execlist submission queue and port state
  158. *
  159. * The struct intel_engine_execlists represents the combined logical state of
  160. * driver and the hardware state for execlist mode of submission.
  161. */
  162. struct intel_engine_execlists {
  163. /**
  164. * @tasklet: softirq tasklet for bottom handler
  165. */
  166. struct tasklet_struct tasklet;
  167. /**
  168. * @default_priolist: priority list for I915_PRIORITY_NORMAL
  169. */
  170. struct i915_priolist default_priolist;
  171. /**
  172. * @no_priolist: priority lists disabled
  173. */
  174. bool no_priolist;
  175. /**
  176. * @submit_reg: gen-specific execlist submission register
  177. * set to the ExecList Submission Port (elsp) register pre-Gen11 and to
  178. * the ExecList Submission Queue Contents register array for Gen11+
  179. */
  180. u32 __iomem *submit_reg;
  181. /**
  182. * @ctrl_reg: the enhanced execlists control register, used to load the
  183. * submit queue on the HW and to request preemptions to idle
  184. */
  185. u32 __iomem *ctrl_reg;
  186. /**
  187. * @port: execlist port states
  188. *
  189. * For each hardware ELSP (ExecList Submission Port) we keep
  190. * track of the last request and the number of times we submitted
  191. * that port to hw. We then count the number of times the hw reports
  192. * a context completion or preemption. As only one context can
  193. * be active on hw, we limit resubmission of context to port[0]. This
  194. * is called Lite Restore, of the context.
  195. */
  196. struct execlist_port {
  197. /**
  198. * @request_count: combined request and submission count
  199. */
  200. struct i915_request *request_count;
  201. #define EXECLIST_COUNT_BITS 2
  202. #define port_request(p) ptr_mask_bits((p)->request_count, EXECLIST_COUNT_BITS)
  203. #define port_count(p) ptr_unmask_bits((p)->request_count, EXECLIST_COUNT_BITS)
  204. #define port_pack(rq, count) ptr_pack_bits(rq, count, EXECLIST_COUNT_BITS)
  205. #define port_unpack(p, count) ptr_unpack_bits((p)->request_count, count, EXECLIST_COUNT_BITS)
  206. #define port_set(p, packed) ((p)->request_count = (packed))
  207. #define port_isset(p) ((p)->request_count)
  208. #define port_index(p, execlists) ((p) - (execlists)->port)
  209. /**
  210. * @context_id: context ID for port
  211. */
  212. GEM_DEBUG_DECL(u32 context_id);
  213. #define EXECLIST_MAX_PORTS 2
  214. } port[EXECLIST_MAX_PORTS];
  215. /**
  216. * @active: is the HW active? We consider the HW as active after
  217. * submitting any context for execution and until we have seen the
  218. * last context completion event. After that, we do not expect any
  219. * more events until we submit, and so can park the HW.
  220. *
  221. * As we have a small number of different sources from which we feed
  222. * the HW, we track the state of each inside a single bitfield.
  223. */
  224. unsigned int active;
  225. #define EXECLISTS_ACTIVE_USER 0
  226. #define EXECLISTS_ACTIVE_PREEMPT 1
  227. #define EXECLISTS_ACTIVE_HWACK 2
  228. /**
  229. * @port_mask: number of execlist ports - 1
  230. */
  231. unsigned int port_mask;
  232. /**
  233. * @queue_priority: Highest pending priority.
  234. *
  235. * When we add requests into the queue, or adjust the priority of
  236. * executing requests, we compute the maximum priority of those
  237. * pending requests. We can then use this value to determine if
  238. * we need to preempt the executing requests to service the queue.
  239. */
  240. int queue_priority;
  241. /**
  242. * @queue: queue of requests, in priority lists
  243. */
  244. struct rb_root queue;
  245. /**
  246. * @first: leftmost level in priority @queue
  247. */
  248. struct rb_node *first;
  249. /**
  250. * @fw_domains: forcewake domains for irq tasklet
  251. */
  252. unsigned int fw_domains;
  253. /**
  254. * @csb_head: context status buffer head
  255. */
  256. unsigned int csb_head;
  257. /**
  258. * @csb_use_mmio: access csb through mmio, instead of hwsp
  259. */
  260. bool csb_use_mmio;
  261. /**
  262. * @preempt_complete_status: expected CSB upon completing preemption
  263. */
  264. u32 preempt_complete_status;
  265. };
  266. #define INTEL_ENGINE_CS_MAX_NAME 8
  267. struct intel_engine_cs {
  268. struct drm_i915_private *i915;
  269. char name[INTEL_ENGINE_CS_MAX_NAME];
  270. enum intel_engine_id id;
  271. unsigned int hw_id;
  272. unsigned int guc_id;
  273. u8 uabi_id;
  274. u8 uabi_class;
  275. u8 class;
  276. u8 instance;
  277. u32 context_size;
  278. u32 mmio_base;
  279. unsigned int irq_shift;
  280. struct intel_ring *buffer;
  281. struct intel_timeline *timeline;
  282. struct drm_i915_gem_object *default_state;
  283. atomic_t irq_count;
  284. unsigned long irq_posted;
  285. #define ENGINE_IRQ_BREADCRUMB 0
  286. #define ENGINE_IRQ_EXECLIST 1
  287. /* Rather than have every client wait upon all user interrupts,
  288. * with the herd waking after every interrupt and each doing the
  289. * heavyweight seqno dance, we delegate the task (of being the
  290. * bottom-half of the user interrupt) to the first client. After
  291. * every interrupt, we wake up one client, who does the heavyweight
  292. * coherent seqno read and either goes back to sleep (if incomplete),
  293. * or wakes up all the completed clients in parallel, before then
  294. * transferring the bottom-half status to the next client in the queue.
  295. *
  296. * Compared to walking the entire list of waiters in a single dedicated
  297. * bottom-half, we reduce the latency of the first waiter by avoiding
  298. * a context switch, but incur additional coherent seqno reads when
  299. * following the chain of request breadcrumbs. Since it is most likely
  300. * that we have a single client waiting on each seqno, then reducing
  301. * the overhead of waking that client is much preferred.
  302. */
  303. struct intel_breadcrumbs {
  304. spinlock_t irq_lock; /* protects irq_*; irqsafe */
  305. struct intel_wait *irq_wait; /* oldest waiter by retirement */
  306. spinlock_t rb_lock; /* protects the rb and wraps irq_lock */
  307. struct rb_root waiters; /* sorted by retirement, priority */
  308. struct list_head signals; /* sorted by retirement */
  309. struct task_struct *signaler; /* used for fence signalling */
  310. struct timer_list fake_irq; /* used after a missed interrupt */
  311. struct timer_list hangcheck; /* detect missed interrupts */
  312. unsigned int hangcheck_interrupts;
  313. unsigned int irq_enabled;
  314. bool irq_armed : 1;
  315. I915_SELFTEST_DECLARE(bool mock : 1);
  316. } breadcrumbs;
  317. struct {
  318. /**
  319. * @enable: Bitmask of enable sample events on this engine.
  320. *
  321. * Bits correspond to sample event types, for instance
  322. * I915_SAMPLE_QUEUED is bit 0 etc.
  323. */
  324. u32 enable;
  325. /**
  326. * @enable_count: Reference count for the enabled samplers.
  327. *
  328. * Index number corresponds to the bit number from @enable.
  329. */
  330. unsigned int enable_count[I915_PMU_SAMPLE_BITS];
  331. /**
  332. * @sample: Counter values for sampling events.
  333. *
  334. * Our internal timer stores the current counters in this field.
  335. */
  336. #define I915_ENGINE_SAMPLE_MAX (I915_SAMPLE_SEMA + 1)
  337. struct i915_pmu_sample sample[I915_ENGINE_SAMPLE_MAX];
  338. } pmu;
  339. /*
  340. * A pool of objects to use as shadow copies of client batch buffers
  341. * when the command parser is enabled. Prevents the client from
  342. * modifying the batch contents after software parsing.
  343. */
  344. struct i915_gem_batch_pool batch_pool;
  345. struct intel_hw_status_page status_page;
  346. struct i915_ctx_workarounds wa_ctx;
  347. struct i915_vma *scratch;
  348. u32 irq_keep_mask; /* always keep these interrupts */
  349. u32 irq_enable_mask; /* bitmask to enable ring interrupt */
  350. void (*irq_enable)(struct intel_engine_cs *engine);
  351. void (*irq_disable)(struct intel_engine_cs *engine);
  352. int (*init_hw)(struct intel_engine_cs *engine);
  353. void (*reset_hw)(struct intel_engine_cs *engine,
  354. struct i915_request *rq);
  355. void (*park)(struct intel_engine_cs *engine);
  356. void (*unpark)(struct intel_engine_cs *engine);
  357. void (*set_default_submission)(struct intel_engine_cs *engine);
  358. struct intel_ring *(*context_pin)(struct intel_engine_cs *engine,
  359. struct i915_gem_context *ctx);
  360. void (*context_unpin)(struct intel_engine_cs *engine,
  361. struct i915_gem_context *ctx);
  362. int (*request_alloc)(struct i915_request *rq);
  363. int (*init_context)(struct i915_request *rq);
  364. int (*emit_flush)(struct i915_request *request, u32 mode);
  365. #define EMIT_INVALIDATE BIT(0)
  366. #define EMIT_FLUSH BIT(1)
  367. #define EMIT_BARRIER (EMIT_INVALIDATE | EMIT_FLUSH)
  368. int (*emit_bb_start)(struct i915_request *rq,
  369. u64 offset, u32 length,
  370. unsigned int dispatch_flags);
  371. #define I915_DISPATCH_SECURE BIT(0)
  372. #define I915_DISPATCH_PINNED BIT(1)
  373. #define I915_DISPATCH_RS BIT(2)
  374. void (*emit_breadcrumb)(struct i915_request *rq, u32 *cs);
  375. int emit_breadcrumb_sz;
  376. /* Pass the request to the hardware queue (e.g. directly into
  377. * the legacy ringbuffer or to the end of an execlist).
  378. *
  379. * This is called from an atomic context with irqs disabled; must
  380. * be irq safe.
  381. */
  382. void (*submit_request)(struct i915_request *rq);
  383. /* Call when the priority on a request has changed and it and its
  384. * dependencies may need rescheduling. Note the request itself may
  385. * not be ready to run!
  386. *
  387. * Called under the struct_mutex.
  388. */
  389. void (*schedule)(struct i915_request *request, int priority);
  390. /*
  391. * Cancel all requests on the hardware, or queued for execution.
  392. * This should only cancel the ready requests that have been
  393. * submitted to the engine (via the engine->submit_request callback).
  394. * This is called when marking the device as wedged.
  395. */
  396. void (*cancel_requests)(struct intel_engine_cs *engine);
  397. /* Some chipsets are not quite as coherent as advertised and need
  398. * an expensive kick to force a true read of the up-to-date seqno.
  399. * However, the up-to-date seqno is not always required and the last
  400. * seen value is good enough. Note that the seqno will always be
  401. * monotonic, even if not coherent.
  402. */
  403. void (*irq_seqno_barrier)(struct intel_engine_cs *engine);
  404. void (*cleanup)(struct intel_engine_cs *engine);
  405. /* GEN8 signal/wait table - never trust comments!
  406. * signal to signal to signal to signal to signal to
  407. * RCS VCS BCS VECS VCS2
  408. * --------------------------------------------------------------------
  409. * RCS | NOP (0x00) | VCS (0x08) | BCS (0x10) | VECS (0x18) | VCS2 (0x20) |
  410. * |-------------------------------------------------------------------
  411. * VCS | RCS (0x28) | NOP (0x30) | BCS (0x38) | VECS (0x40) | VCS2 (0x48) |
  412. * |-------------------------------------------------------------------
  413. * BCS | RCS (0x50) | VCS (0x58) | NOP (0x60) | VECS (0x68) | VCS2 (0x70) |
  414. * |-------------------------------------------------------------------
  415. * VECS | RCS (0x78) | VCS (0x80) | BCS (0x88) | NOP (0x90) | VCS2 (0x98) |
  416. * |-------------------------------------------------------------------
  417. * VCS2 | RCS (0xa0) | VCS (0xa8) | BCS (0xb0) | VECS (0xb8) | NOP (0xc0) |
  418. * |-------------------------------------------------------------------
  419. *
  420. * Generalization:
  421. * f(x, y) := (x->id * NUM_RINGS * seqno_size) + (seqno_size * y->id)
  422. * ie. transpose of g(x, y)
  423. *
  424. * sync from sync from sync from sync from sync from
  425. * RCS VCS BCS VECS VCS2
  426. * --------------------------------------------------------------------
  427. * RCS | NOP (0x00) | VCS (0x28) | BCS (0x50) | VECS (0x78) | VCS2 (0xa0) |
  428. * |-------------------------------------------------------------------
  429. * VCS | RCS (0x08) | NOP (0x30) | BCS (0x58) | VECS (0x80) | VCS2 (0xa8) |
  430. * |-------------------------------------------------------------------
  431. * BCS | RCS (0x10) | VCS (0x38) | NOP (0x60) | VECS (0x88) | VCS2 (0xb0) |
  432. * |-------------------------------------------------------------------
  433. * VECS | RCS (0x18) | VCS (0x40) | BCS (0x68) | NOP (0x90) | VCS2 (0xb8) |
  434. * |-------------------------------------------------------------------
  435. * VCS2 | RCS (0x20) | VCS (0x48) | BCS (0x70) | VECS (0x98) | NOP (0xc0) |
  436. * |-------------------------------------------------------------------
  437. *
  438. * Generalization:
  439. * g(x, y) := (y->id * NUM_RINGS * seqno_size) + (seqno_size * x->id)
  440. * ie. transpose of f(x, y)
  441. */
  442. struct {
  443. #define GEN6_SEMAPHORE_LAST VECS_HW
  444. #define GEN6_NUM_SEMAPHORES (GEN6_SEMAPHORE_LAST + 1)
  445. #define GEN6_SEMAPHORES_MASK GENMASK(GEN6_SEMAPHORE_LAST, 0)
  446. struct {
  447. /* our mbox written by others */
  448. u32 wait[GEN6_NUM_SEMAPHORES];
  449. /* mboxes this ring signals to */
  450. i915_reg_t signal[GEN6_NUM_SEMAPHORES];
  451. } mbox;
  452. /* AKA wait() */
  453. int (*sync_to)(struct i915_request *rq,
  454. struct i915_request *signal);
  455. u32 *(*signal)(struct i915_request *rq, u32 *cs);
  456. } semaphore;
  457. struct intel_engine_execlists execlists;
  458. /* Contexts are pinned whilst they are active on the GPU. The last
  459. * context executed remains active whilst the GPU is idle - the
  460. * switch away and write to the context object only occurs on the
  461. * next execution. Contexts are only unpinned on retirement of the
  462. * following request ensuring that we can always write to the object
  463. * on the context switch even after idling. Across suspend, we switch
  464. * to the kernel context and trash it as the save may not happen
  465. * before the hardware is powered down.
  466. */
  467. struct i915_gem_context *last_retired_context;
  468. /* We track the current MI_SET_CONTEXT in order to eliminate
  469. * redudant context switches. This presumes that requests are not
  470. * reordered! Or when they are the tracking is updated along with
  471. * the emission of individual requests into the legacy command
  472. * stream (ring).
  473. */
  474. struct i915_gem_context *legacy_active_context;
  475. struct i915_hw_ppgtt *legacy_active_ppgtt;
  476. /* status_notifier: list of callbacks for context-switch changes */
  477. struct atomic_notifier_head context_status_notifier;
  478. struct intel_engine_hangcheck hangcheck;
  479. #define I915_ENGINE_NEEDS_CMD_PARSER BIT(0)
  480. #define I915_ENGINE_SUPPORTS_STATS BIT(1)
  481. unsigned int flags;
  482. /*
  483. * Table of commands the command parser needs to know about
  484. * for this engine.
  485. */
  486. DECLARE_HASHTABLE(cmd_hash, I915_CMD_HASH_ORDER);
  487. /*
  488. * Table of registers allowed in commands that read/write registers.
  489. */
  490. const struct drm_i915_reg_table *reg_tables;
  491. int reg_table_count;
  492. /*
  493. * Returns the bitmask for the length field of the specified command.
  494. * Return 0 for an unrecognized/invalid command.
  495. *
  496. * If the command parser finds an entry for a command in the engine's
  497. * cmd_tables, it gets the command's length based on the table entry.
  498. * If not, it calls this function to determine the per-engine length
  499. * field encoding for the command (i.e. different opcode ranges use
  500. * certain bits to encode the command length in the header).
  501. */
  502. u32 (*get_cmd_length_mask)(u32 cmd_header);
  503. struct {
  504. /**
  505. * @lock: Lock protecting the below fields.
  506. */
  507. spinlock_t lock;
  508. /**
  509. * @enabled: Reference count indicating number of listeners.
  510. */
  511. unsigned int enabled;
  512. /**
  513. * @active: Number of contexts currently scheduled in.
  514. */
  515. unsigned int active;
  516. /**
  517. * @enabled_at: Timestamp when busy stats were enabled.
  518. */
  519. ktime_t enabled_at;
  520. /**
  521. * @start: Timestamp of the last idle to active transition.
  522. *
  523. * Idle is defined as active == 0, active is active > 0.
  524. */
  525. ktime_t start;
  526. /**
  527. * @total: Total time this engine was busy.
  528. *
  529. * Accumulated time not counting the most recent block in cases
  530. * where engine is currently busy (active > 0).
  531. */
  532. ktime_t total;
  533. } stats;
  534. };
  535. static inline bool intel_engine_needs_cmd_parser(struct intel_engine_cs *engine)
  536. {
  537. return engine->flags & I915_ENGINE_NEEDS_CMD_PARSER;
  538. }
  539. static inline bool intel_engine_supports_stats(struct intel_engine_cs *engine)
  540. {
  541. return engine->flags & I915_ENGINE_SUPPORTS_STATS;
  542. }
  543. static inline void
  544. execlists_set_active(struct intel_engine_execlists *execlists,
  545. unsigned int bit)
  546. {
  547. __set_bit(bit, (unsigned long *)&execlists->active);
  548. }
  549. static inline void
  550. execlists_clear_active(struct intel_engine_execlists *execlists,
  551. unsigned int bit)
  552. {
  553. __clear_bit(bit, (unsigned long *)&execlists->active);
  554. }
  555. static inline bool
  556. execlists_is_active(const struct intel_engine_execlists *execlists,
  557. unsigned int bit)
  558. {
  559. return test_bit(bit, (unsigned long *)&execlists->active);
  560. }
  561. void
  562. execlists_cancel_port_requests(struct intel_engine_execlists * const execlists);
  563. void
  564. execlists_unwind_incomplete_requests(struct intel_engine_execlists *execlists);
  565. static inline unsigned int
  566. execlists_num_ports(const struct intel_engine_execlists * const execlists)
  567. {
  568. return execlists->port_mask + 1;
  569. }
  570. static inline void
  571. execlists_port_complete(struct intel_engine_execlists * const execlists,
  572. struct execlist_port * const port)
  573. {
  574. const unsigned int m = execlists->port_mask;
  575. GEM_BUG_ON(port_index(port, execlists) != 0);
  576. GEM_BUG_ON(!execlists_is_active(execlists, EXECLISTS_ACTIVE_USER));
  577. memmove(port, port + 1, m * sizeof(struct execlist_port));
  578. memset(port + m, 0, sizeof(struct execlist_port));
  579. }
  580. static inline unsigned int
  581. intel_engine_flag(const struct intel_engine_cs *engine)
  582. {
  583. return BIT(engine->id);
  584. }
  585. static inline u32
  586. intel_read_status_page(const struct intel_engine_cs *engine, int reg)
  587. {
  588. /* Ensure that the compiler doesn't optimize away the load. */
  589. return READ_ONCE(engine->status_page.page_addr[reg]);
  590. }
  591. static inline void
  592. intel_write_status_page(struct intel_engine_cs *engine, int reg, u32 value)
  593. {
  594. /* Writing into the status page should be done sparingly. Since
  595. * we do when we are uncertain of the device state, we take a bit
  596. * of extra paranoia to try and ensure that the HWS takes the value
  597. * we give and that it doesn't end up trapped inside the CPU!
  598. */
  599. if (static_cpu_has(X86_FEATURE_CLFLUSH)) {
  600. mb();
  601. clflush(&engine->status_page.page_addr[reg]);
  602. engine->status_page.page_addr[reg] = value;
  603. clflush(&engine->status_page.page_addr[reg]);
  604. mb();
  605. } else {
  606. WRITE_ONCE(engine->status_page.page_addr[reg], value);
  607. }
  608. }
  609. /*
  610. * Reads a dword out of the status page, which is written to from the command
  611. * queue by automatic updates, MI_REPORT_HEAD, MI_STORE_DATA_INDEX, or
  612. * MI_STORE_DATA_IMM.
  613. *
  614. * The following dwords have a reserved meaning:
  615. * 0x00: ISR copy, updated when an ISR bit not set in the HWSTAM changes.
  616. * 0x04: ring 0 head pointer
  617. * 0x05: ring 1 head pointer (915-class)
  618. * 0x06: ring 2 head pointer (915-class)
  619. * 0x10-0x1b: Context status DWords (GM45)
  620. * 0x1f: Last written status offset. (GM45)
  621. * 0x20-0x2f: Reserved (Gen6+)
  622. *
  623. * The area from dword 0x30 to 0x3ff is available for driver usage.
  624. */
  625. #define I915_GEM_HWS_INDEX 0x30
  626. #define I915_GEM_HWS_INDEX_ADDR (I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT)
  627. #define I915_GEM_HWS_PREEMPT_INDEX 0x32
  628. #define I915_GEM_HWS_PREEMPT_ADDR (I915_GEM_HWS_PREEMPT_INDEX << MI_STORE_DWORD_INDEX_SHIFT)
  629. #define I915_GEM_HWS_SCRATCH_INDEX 0x40
  630. #define I915_GEM_HWS_SCRATCH_ADDR (I915_GEM_HWS_SCRATCH_INDEX << MI_STORE_DWORD_INDEX_SHIFT)
  631. #define I915_HWS_CSB_BUF0_INDEX 0x10
  632. #define I915_HWS_CSB_WRITE_INDEX 0x1f
  633. #define CNL_HWS_CSB_WRITE_INDEX 0x2f
  634. struct intel_ring *
  635. intel_engine_create_ring(struct intel_engine_cs *engine, int size);
  636. int intel_ring_pin(struct intel_ring *ring,
  637. struct drm_i915_private *i915,
  638. unsigned int offset_bias);
  639. void intel_ring_reset(struct intel_ring *ring, u32 tail);
  640. unsigned int intel_ring_update_space(struct intel_ring *ring);
  641. void intel_ring_unpin(struct intel_ring *ring);
  642. void intel_ring_free(struct intel_ring *ring);
  643. void intel_engine_stop(struct intel_engine_cs *engine);
  644. void intel_engine_cleanup(struct intel_engine_cs *engine);
  645. void intel_legacy_submission_resume(struct drm_i915_private *dev_priv);
  646. int __must_check intel_ring_cacheline_align(struct i915_request *rq);
  647. int intel_ring_wait_for_space(struct intel_ring *ring, unsigned int bytes);
  648. u32 __must_check *intel_ring_begin(struct i915_request *rq, unsigned int n);
  649. static inline void intel_ring_advance(struct i915_request *rq, u32 *cs)
  650. {
  651. /* Dummy function.
  652. *
  653. * This serves as a placeholder in the code so that the reader
  654. * can compare against the preceding intel_ring_begin() and
  655. * check that the number of dwords emitted matches the space
  656. * reserved for the command packet (i.e. the value passed to
  657. * intel_ring_begin()).
  658. */
  659. GEM_BUG_ON((rq->ring->vaddr + rq->ring->emit) != cs);
  660. }
  661. static inline u32 intel_ring_wrap(const struct intel_ring *ring, u32 pos)
  662. {
  663. return pos & (ring->size - 1);
  664. }
  665. static inline u32 intel_ring_offset(const struct i915_request *rq, void *addr)
  666. {
  667. /* Don't write ring->size (equivalent to 0) as that hangs some GPUs. */
  668. u32 offset = addr - rq->ring->vaddr;
  669. GEM_BUG_ON(offset > rq->ring->size);
  670. return intel_ring_wrap(rq->ring, offset);
  671. }
  672. static inline void
  673. assert_ring_tail_valid(const struct intel_ring *ring, unsigned int tail)
  674. {
  675. /* We could combine these into a single tail operation, but keeping
  676. * them as seperate tests will help identify the cause should one
  677. * ever fire.
  678. */
  679. GEM_BUG_ON(!IS_ALIGNED(tail, 8));
  680. GEM_BUG_ON(tail >= ring->size);
  681. /*
  682. * "Ring Buffer Use"
  683. * Gen2 BSpec "1. Programming Environment" / 1.4.4.6
  684. * Gen3 BSpec "1c Memory Interface Functions" / 2.3.4.5
  685. * Gen4+ BSpec "1c Memory Interface and Command Stream" / 5.3.4.5
  686. * "If the Ring Buffer Head Pointer and the Tail Pointer are on the
  687. * same cacheline, the Head Pointer must not be greater than the Tail
  688. * Pointer."
  689. *
  690. * We use ring->head as the last known location of the actual RING_HEAD,
  691. * it may have advanced but in the worst case it is equally the same
  692. * as ring->head and so we should never program RING_TAIL to advance
  693. * into the same cacheline as ring->head.
  694. */
  695. #define cacheline(a) round_down(a, CACHELINE_BYTES)
  696. GEM_BUG_ON(cacheline(tail) == cacheline(ring->head) &&
  697. tail < ring->head);
  698. #undef cacheline
  699. }
  700. static inline unsigned int
  701. intel_ring_set_tail(struct intel_ring *ring, unsigned int tail)
  702. {
  703. /* Whilst writes to the tail are strictly order, there is no
  704. * serialisation between readers and the writers. The tail may be
  705. * read by i915_request_retire() just as it is being updated
  706. * by execlists, as although the breadcrumb is complete, the context
  707. * switch hasn't been seen.
  708. */
  709. assert_ring_tail_valid(ring, tail);
  710. ring->tail = tail;
  711. return tail;
  712. }
  713. void intel_engine_init_global_seqno(struct intel_engine_cs *engine, u32 seqno);
  714. void intel_engine_setup_common(struct intel_engine_cs *engine);
  715. int intel_engine_init_common(struct intel_engine_cs *engine);
  716. int intel_engine_create_scratch(struct intel_engine_cs *engine, int size);
  717. void intel_engine_cleanup_common(struct intel_engine_cs *engine);
  718. int intel_init_render_ring_buffer(struct intel_engine_cs *engine);
  719. int intel_init_bsd_ring_buffer(struct intel_engine_cs *engine);
  720. int intel_init_blt_ring_buffer(struct intel_engine_cs *engine);
  721. int intel_init_vebox_ring_buffer(struct intel_engine_cs *engine);
  722. u64 intel_engine_get_active_head(const struct intel_engine_cs *engine);
  723. u64 intel_engine_get_last_batch_head(const struct intel_engine_cs *engine);
  724. static inline u32 intel_engine_get_seqno(struct intel_engine_cs *engine)
  725. {
  726. return intel_read_status_page(engine, I915_GEM_HWS_INDEX);
  727. }
  728. static inline u32 intel_engine_last_submit(struct intel_engine_cs *engine)
  729. {
  730. /* We are only peeking at the tail of the submit queue (and not the
  731. * queue itself) in order to gain a hint as to the current active
  732. * state of the engine. Callers are not expected to be taking
  733. * engine->timeline->lock, nor are they expected to be concerned
  734. * wtih serialising this hint with anything, so document it as
  735. * a hint and nothing more.
  736. */
  737. return READ_ONCE(engine->timeline->seqno);
  738. }
  739. int init_workarounds_ring(struct intel_engine_cs *engine);
  740. int intel_ring_workarounds_emit(struct i915_request *rq);
  741. void intel_engine_get_instdone(struct intel_engine_cs *engine,
  742. struct intel_instdone *instdone);
  743. /*
  744. * Arbitrary size for largest possible 'add request' sequence. The code paths
  745. * are complex and variable. Empirical measurement shows that the worst case
  746. * is BDW at 192 bytes (6 + 6 + 36 dwords), then ILK at 136 bytes. However,
  747. * we need to allocate double the largest single packet within that emission
  748. * to account for tail wraparound (so 6 + 6 + 72 dwords for BDW).
  749. */
  750. #define MIN_SPACE_FOR_ADD_REQUEST 336
  751. static inline u32 intel_hws_seqno_address(struct intel_engine_cs *engine)
  752. {
  753. return engine->status_page.ggtt_offset + I915_GEM_HWS_INDEX_ADDR;
  754. }
  755. static inline u32 intel_hws_preempt_done_address(struct intel_engine_cs *engine)
  756. {
  757. return engine->status_page.ggtt_offset + I915_GEM_HWS_PREEMPT_ADDR;
  758. }
  759. /* intel_breadcrumbs.c -- user interrupt bottom-half for waiters */
  760. int intel_engine_init_breadcrumbs(struct intel_engine_cs *engine);
  761. static inline void intel_wait_init(struct intel_wait *wait,
  762. struct i915_request *rq)
  763. {
  764. wait->tsk = current;
  765. wait->request = rq;
  766. }
  767. static inline void intel_wait_init_for_seqno(struct intel_wait *wait, u32 seqno)
  768. {
  769. wait->tsk = current;
  770. wait->seqno = seqno;
  771. }
  772. static inline bool intel_wait_has_seqno(const struct intel_wait *wait)
  773. {
  774. return wait->seqno;
  775. }
  776. static inline bool
  777. intel_wait_update_seqno(struct intel_wait *wait, u32 seqno)
  778. {
  779. wait->seqno = seqno;
  780. return intel_wait_has_seqno(wait);
  781. }
  782. static inline bool
  783. intel_wait_update_request(struct intel_wait *wait,
  784. const struct i915_request *rq)
  785. {
  786. return intel_wait_update_seqno(wait, i915_request_global_seqno(rq));
  787. }
  788. static inline bool
  789. intel_wait_check_seqno(const struct intel_wait *wait, u32 seqno)
  790. {
  791. return wait->seqno == seqno;
  792. }
  793. static inline bool
  794. intel_wait_check_request(const struct intel_wait *wait,
  795. const struct i915_request *rq)
  796. {
  797. return intel_wait_check_seqno(wait, i915_request_global_seqno(rq));
  798. }
  799. static inline bool intel_wait_complete(const struct intel_wait *wait)
  800. {
  801. return RB_EMPTY_NODE(&wait->node);
  802. }
  803. bool intel_engine_add_wait(struct intel_engine_cs *engine,
  804. struct intel_wait *wait);
  805. void intel_engine_remove_wait(struct intel_engine_cs *engine,
  806. struct intel_wait *wait);
  807. void intel_engine_enable_signaling(struct i915_request *request, bool wakeup);
  808. void intel_engine_cancel_signaling(struct i915_request *request);
  809. static inline bool intel_engine_has_waiter(const struct intel_engine_cs *engine)
  810. {
  811. return READ_ONCE(engine->breadcrumbs.irq_wait);
  812. }
  813. unsigned int intel_engine_wakeup(struct intel_engine_cs *engine);
  814. #define ENGINE_WAKEUP_WAITER BIT(0)
  815. #define ENGINE_WAKEUP_ASLEEP BIT(1)
  816. void intel_engine_pin_breadcrumbs_irq(struct intel_engine_cs *engine);
  817. void intel_engine_unpin_breadcrumbs_irq(struct intel_engine_cs *engine);
  818. void __intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine);
  819. void intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine);
  820. void intel_engine_reset_breadcrumbs(struct intel_engine_cs *engine);
  821. void intel_engine_fini_breadcrumbs(struct intel_engine_cs *engine);
  822. static inline u32 *gen8_emit_pipe_control(u32 *batch, u32 flags, u32 offset)
  823. {
  824. memset(batch, 0, 6 * sizeof(u32));
  825. batch[0] = GFX_OP_PIPE_CONTROL(6);
  826. batch[1] = flags;
  827. batch[2] = offset;
  828. return batch + 6;
  829. }
  830. static inline u32 *
  831. gen8_emit_ggtt_write_rcs(u32 *cs, u32 value, u32 gtt_offset)
  832. {
  833. /* We're using qword write, offset should be aligned to 8 bytes. */
  834. GEM_BUG_ON(!IS_ALIGNED(gtt_offset, 8));
  835. /* w/a for post sync ops following a GPGPU operation we
  836. * need a prior CS_STALL, which is emitted by the flush
  837. * following the batch.
  838. */
  839. *cs++ = GFX_OP_PIPE_CONTROL(6);
  840. *cs++ = PIPE_CONTROL_GLOBAL_GTT_IVB | PIPE_CONTROL_CS_STALL |
  841. PIPE_CONTROL_QW_WRITE;
  842. *cs++ = gtt_offset;
  843. *cs++ = 0;
  844. *cs++ = value;
  845. /* We're thrashing one dword of HWS. */
  846. *cs++ = 0;
  847. return cs;
  848. }
  849. static inline u32 *
  850. gen8_emit_ggtt_write(u32 *cs, u32 value, u32 gtt_offset)
  851. {
  852. /* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */
  853. GEM_BUG_ON(gtt_offset & (1 << 5));
  854. /* Offset should be aligned to 8 bytes for both (QW/DW) write types */
  855. GEM_BUG_ON(!IS_ALIGNED(gtt_offset, 8));
  856. *cs++ = (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW;
  857. *cs++ = gtt_offset | MI_FLUSH_DW_USE_GTT;
  858. *cs++ = 0;
  859. *cs++ = value;
  860. return cs;
  861. }
  862. bool intel_engine_is_idle(struct intel_engine_cs *engine);
  863. bool intel_engines_are_idle(struct drm_i915_private *dev_priv);
  864. bool intel_engine_has_kernel_context(const struct intel_engine_cs *engine);
  865. void intel_engines_park(struct drm_i915_private *i915);
  866. void intel_engines_unpark(struct drm_i915_private *i915);
  867. void intel_engines_reset_default_submission(struct drm_i915_private *i915);
  868. unsigned int intel_engines_has_context_isolation(struct drm_i915_private *i915);
  869. bool intel_engine_can_store_dword(struct intel_engine_cs *engine);
  870. __printf(3, 4)
  871. void intel_engine_dump(struct intel_engine_cs *engine,
  872. struct drm_printer *m,
  873. const char *header, ...);
  874. struct intel_engine_cs *
  875. intel_engine_lookup_user(struct drm_i915_private *i915, u8 class, u8 instance);
  876. static inline void intel_engine_context_in(struct intel_engine_cs *engine)
  877. {
  878. unsigned long flags;
  879. if (READ_ONCE(engine->stats.enabled) == 0)
  880. return;
  881. spin_lock_irqsave(&engine->stats.lock, flags);
  882. if (engine->stats.enabled > 0) {
  883. if (engine->stats.active++ == 0)
  884. engine->stats.start = ktime_get();
  885. GEM_BUG_ON(engine->stats.active == 0);
  886. }
  887. spin_unlock_irqrestore(&engine->stats.lock, flags);
  888. }
  889. static inline void intel_engine_context_out(struct intel_engine_cs *engine)
  890. {
  891. unsigned long flags;
  892. if (READ_ONCE(engine->stats.enabled) == 0)
  893. return;
  894. spin_lock_irqsave(&engine->stats.lock, flags);
  895. if (engine->stats.enabled > 0) {
  896. ktime_t last;
  897. if (engine->stats.active && --engine->stats.active == 0) {
  898. /*
  899. * Decrement the active context count and in case GPU
  900. * is now idle add up to the running total.
  901. */
  902. last = ktime_sub(ktime_get(), engine->stats.start);
  903. engine->stats.total = ktime_add(engine->stats.total,
  904. last);
  905. } else if (engine->stats.active == 0) {
  906. /*
  907. * After turning on engine stats, context out might be
  908. * the first event in which case we account from the
  909. * time stats gathering was turned on.
  910. */
  911. last = ktime_sub(ktime_get(), engine->stats.enabled_at);
  912. engine->stats.total = ktime_add(engine->stats.total,
  913. last);
  914. }
  915. }
  916. spin_unlock_irqrestore(&engine->stats.lock, flags);
  917. }
  918. int intel_enable_engine_stats(struct intel_engine_cs *engine);
  919. void intel_disable_engine_stats(struct intel_engine_cs *engine);
  920. ktime_t intel_engine_get_busy_time(struct intel_engine_cs *engine);
  921. #endif /* _INTEL_RINGBUFFER_H_ */