intel_ringbuffer.h 35 KB

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