intel_ringbuffer.h 35 KB

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