intel_ringbuffer.h 34 KB

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