intel_ringbuffer.h 33 KB

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