intel_ringbuffer.h 35 KB

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