intel_ringbuffer.h 36 KB

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