xref: /linux/drivers/gpu/drm/xe/xe_ring_ops.c (revision 1c9b88eb8e10ffdb4e6107aaaa3f2581dd1ed3e4)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2022 Intel Corporation
4  */
5 
6 #include "xe_ring_ops.h"
7 
8 #include <generated/xe_wa_oob.h>
9 
10 #include "instructions/xe_gpu_commands.h"
11 #include "instructions/xe_mi_commands.h"
12 #include "regs/xe_engine_regs.h"
13 #include "regs/xe_gt_regs.h"
14 #include "xe_exec_queue.h"
15 #include "xe_gt_types.h"
16 #include "xe_lrc.h"
17 #include "xe_sched_job.h"
18 #include "xe_sriov.h"
19 #include "xe_vm_types.h"
20 #include "xe_vm.h"
21 #include "xe_wa.h"
22 
23 /*
24  * 3D-related flags that can't be set on _engines_ that lack access to the 3D
25  * pipeline (i.e., CCS engines).
26  */
27 #define PIPE_CONTROL_3D_ENGINE_FLAGS (\
28 		PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH | \
29 		PIPE_CONTROL_DEPTH_CACHE_FLUSH | \
30 		PIPE_CONTROL_TILE_CACHE_FLUSH | \
31 		PIPE_CONTROL_DEPTH_STALL | \
32 		PIPE_CONTROL_STALL_AT_SCOREBOARD | \
33 		PIPE_CONTROL_PSD_SYNC | \
34 		PIPE_CONTROL_AMFS_FLUSH | \
35 		PIPE_CONTROL_VF_CACHE_INVALIDATE | \
36 		PIPE_CONTROL_GLOBAL_SNAPSHOT_RESET)
37 
38 /* 3D-related flags that can't be set on _platforms_ that lack a 3D pipeline */
39 #define PIPE_CONTROL_3D_ARCH_FLAGS ( \
40 		PIPE_CONTROL_3D_ENGINE_FLAGS | \
41 		PIPE_CONTROL_INDIRECT_STATE_DISABLE | \
42 		PIPE_CONTROL_FLUSH_ENABLE | \
43 		PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE | \
44 		PIPE_CONTROL_DC_FLUSH_ENABLE)
45 
46 static u32 preparser_disable(bool state)
47 {
48 	return MI_ARB_CHECK | BIT(8) | state;
49 }
50 
51 static u32 *
52 __emit_aux_table_inv(u32 *cmd, const struct xe_reg reg, u32 adj_offset)
53 {
54 	*cmd++ = MI_LOAD_REGISTER_IMM | MI_LRI_NUM_REGS(1) |
55 		 MI_LRI_MMIO_REMAP_EN;
56 	*cmd++ = reg.addr + adj_offset;
57 	*cmd++ = AUX_INV;
58 	*cmd++ = MI_SEMAPHORE_WAIT | MI_SEMW_REGISTER_POLL |
59 		 MI_SEMW_POLL | MI_SEMW_COMPARE(SAD_EQ_SDD);
60 	*cmd++ = 0;
61 	*cmd++ = reg.addr + adj_offset;
62 	*cmd++ = 0;
63 	*cmd++ = 0;
64 
65 	return cmd;
66 }
67 
68 static u32 *emit_aux_table_inv_render_compute(struct xe_gt *gt, u32 *cmd)
69 {
70 	return __emit_aux_table_inv(cmd, CCS_AUX_INV, gt->mmio.adj_offset);
71 }
72 
73 static u32 *emit_aux_table_inv_video_decode(struct xe_gt *gt, u32 *cmd)
74 {
75 	return __emit_aux_table_inv(cmd, VD0_AUX_INV, gt->mmio.adj_offset);
76 }
77 
78 static u32 *emit_aux_table_inv_video_enhance(struct xe_gt *gt, u32 *cmd)
79 {
80 	return __emit_aux_table_inv(cmd, VE0_AUX_INV, gt->mmio.adj_offset);
81 }
82 
83 static int emit_aux_table_inv(struct xe_hw_engine *hwe, u32 *dw, int i)
84 {
85 	struct xe_gt *gt = hwe->gt;
86 	u32 *(*emit)(struct xe_gt *gt, u32 *cmd) =
87 		gt->ring_ops[hwe->class]->emit_aux_table_inv;
88 
89 	if (emit)
90 		return emit(gt, dw + i) - dw;
91 	else
92 		return i;
93 }
94 
95 static int emit_user_interrupt(u32 *dw, int i)
96 {
97 	dw[i++] = MI_USER_INTERRUPT;
98 	dw[i++] = MI_ARB_ON_OFF | MI_ARB_ENABLE;
99 	dw[i++] = MI_ARB_CHECK;
100 
101 	return i;
102 }
103 
104 static int emit_store_imm_ggtt(u32 addr, u32 value, u32 *dw, int i)
105 {
106 	dw[i++] = MI_STORE_DATA_IMM | MI_SDI_GGTT | MI_SDI_NUM_DW(1);
107 	dw[i++] = addr;
108 	dw[i++] = 0;
109 	dw[i++] = value;
110 
111 	return i;
112 }
113 
114 static int emit_flush_dw(u32 *dw, int i)
115 {
116 	dw[i++] = MI_FLUSH_DW | MI_FLUSH_IMM_DW;
117 	dw[i++] = 0;
118 	dw[i++] = 0;
119 	dw[i++] = 0;
120 
121 	return i;
122 }
123 
124 static int emit_flush_imm_ggtt(u32 addr, u32 value, u32 flags, u32 *dw, int i)
125 {
126 	dw[i++] = MI_FLUSH_DW | MI_FLUSH_DW_OP_STOREDW | MI_FLUSH_IMM_DW |
127 		  flags;
128 	dw[i++] = addr | MI_FLUSH_DW_USE_GTT;
129 	dw[i++] = 0;
130 	dw[i++] = value;
131 
132 	return i;
133 }
134 
135 static int emit_bb_start(u64 batch_addr, u32 ppgtt_flag, u32 *dw, int i)
136 {
137 	dw[i++] = MI_BATCH_BUFFER_START | ppgtt_flag | XE_INSTR_NUM_DW(3);
138 	dw[i++] = lower_32_bits(batch_addr);
139 	dw[i++] = upper_32_bits(batch_addr);
140 
141 	return i;
142 }
143 
144 static int emit_flush_invalidate(u32 addr, u32 val, u32 flush_flags, u32 *dw, int i)
145 {
146 	dw[i++] = MI_FLUSH_DW | MI_FLUSH_DW_OP_STOREDW | MI_FLUSH_IMM_DW |
147 		  (flush_flags & MI_INVALIDATE_TLB);
148 	dw[i++] = addr | MI_FLUSH_DW_USE_GTT;
149 	dw[i++] = 0;
150 	dw[i++] = val;
151 
152 	return i;
153 }
154 
155 static int
156 emit_pipe_control(u32 *dw, int i, u32 bit_group_0, u32 bit_group_1, u32 offset, u32 value)
157 {
158 	dw[i++] = GFX_OP_PIPE_CONTROL(6) | bit_group_0;
159 	dw[i++] = bit_group_1;
160 	dw[i++] = offset;
161 	dw[i++] = 0;
162 	dw[i++] = value;
163 	dw[i++] = 0;
164 
165 	return i;
166 }
167 
168 static int emit_pipe_invalidate(struct xe_exec_queue *q, u32 mask_flags,
169 				bool invalidate_tlb, u32 *dw, int i)
170 {
171 	u32 flags0 = 0;
172 	u32 flags1 = PIPE_CONTROL_COMMAND_CACHE_INVALIDATE |
173 		PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE |
174 		PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
175 		PIPE_CONTROL_VF_CACHE_INVALIDATE |
176 		PIPE_CONTROL_CONST_CACHE_INVALIDATE |
177 		PIPE_CONTROL_STATE_CACHE_INVALIDATE |
178 		PIPE_CONTROL_QW_WRITE |
179 		PIPE_CONTROL_STORE_DATA_INDEX;
180 
181 	if (invalidate_tlb)
182 		flags1 |= PIPE_CONTROL_TLB_INVALIDATE;
183 
184 	if (xe_exec_queue_is_multi_queue(q))
185 		flags0 |= PIPE_CONTROL0_QUEUE_DRAIN_MODE;
186 	else
187 		flags1 |= PIPE_CONTROL_CS_STALL;
188 
189 	flags1 &= ~mask_flags;
190 
191 	if (flags1 & PIPE_CONTROL_VF_CACHE_INVALIDATE)
192 		flags0 |= PIPE_CONTROL0_L3_READ_ONLY_CACHE_INVALIDATE;
193 
194 	return emit_pipe_control(dw, i, flags0, flags1,
195 				 LRC_PPHWSP_FLUSH_INVAL_SCRATCH_ADDR, 0);
196 }
197 
198 static int emit_store_imm_ppgtt_posted(u64 addr, u64 value,
199 				       u32 *dw, int i)
200 {
201 	dw[i++] = MI_STORE_DATA_IMM | MI_SDI_NUM_QW(1);
202 	dw[i++] = lower_32_bits(addr);
203 	dw[i++] = upper_32_bits(addr);
204 	dw[i++] = lower_32_bits(value);
205 	dw[i++] = upper_32_bits(value);
206 
207 	return i;
208 }
209 
210 static int emit_render_cache_flush(struct xe_sched_job *job, u32 *dw, int i)
211 {
212 	struct xe_exec_queue *q = job->q;
213 	struct xe_gt *gt = q->gt;
214 	struct xe_device *xe = gt_to_xe(gt);
215 	bool lacks_render = !(gt->info.engine_mask & XE_HW_ENGINE_RCS_MASK);
216 	u32 flags0, flags1;
217 
218 	if (XE_GT_WA(gt, 14016712196))
219 		i = emit_pipe_control(dw, i, 0, PIPE_CONTROL_DEPTH_CACHE_FLUSH,
220 				      LRC_PPHWSP_FLUSH_INVAL_SCRATCH_ADDR, 0);
221 
222 	flags0 = PIPE_CONTROL0_HDC_PIPELINE_FLUSH;
223 	/*
224 	 * Prior to MTL, HDC Pipeline Flush reliably also flushes the LSC
225 	 * untyped L1 dataport cache, provided HDC_CHICKEN0 is programmed
226 	 * correctly. Starting with MTL that coupling no longer holds
227 	 * regardless of how HDC_CHICKEN0 is programmed, but explicitly
228 	 * requesting the flush via PIPE_CONTROL is itself only reliable
229 	 * from Xe2 onward, so only gate it in on Xe2+.
230 	 */
231 	if (GRAPHICS_VERx100(xe) >= 2000)
232 		flags0 |= PIPE_CONTROL0_UNTYPED_DATAPORT_CACHE_FLUSH;
233 	flags1 = (PIPE_CONTROL_TILE_CACHE_FLUSH |
234 		 PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH |
235 		 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
236 		 PIPE_CONTROL_DC_FLUSH_ENABLE |
237 		 PIPE_CONTROL_FLUSH_ENABLE);
238 
239 	if (XE_GT_WA(gt, 1409600907))
240 		flags1 |= PIPE_CONTROL_DEPTH_STALL;
241 
242 	if (lacks_render)
243 		flags1 &= ~PIPE_CONTROL_3D_ARCH_FLAGS;
244 	else if (job->q->class == XE_ENGINE_CLASS_COMPUTE)
245 		flags1 &= ~PIPE_CONTROL_3D_ENGINE_FLAGS;
246 
247 	if (xe_exec_queue_is_multi_queue(q))
248 		flags0 |= PIPE_CONTROL0_QUEUE_DRAIN_MODE;
249 	else
250 		flags1 |= PIPE_CONTROL_CS_STALL;
251 
252 	return emit_pipe_control(dw, i, flags0, flags1, 0, 0);
253 }
254 
255 static int emit_pipe_imm_ggtt(struct xe_exec_queue *q, u32 addr, u32 value,
256 			      bool stall_only, u32 *dw, int i)
257 {
258 	u32 flags0 = 0, flags1 = PIPE_CONTROL_GLOBAL_GTT_IVB | PIPE_CONTROL_QW_WRITE;
259 
260 	if (!stall_only)
261 		flags1 |= PIPE_CONTROL_FLUSH_ENABLE;
262 
263 	if (xe_exec_queue_is_multi_queue(q))
264 		flags0 |= PIPE_CONTROL0_QUEUE_DRAIN_MODE;
265 	else
266 		flags1 |= PIPE_CONTROL_CS_STALL;
267 
268 	return emit_pipe_control(dw, i, flags0, flags1, addr, value);
269 }
270 
271 static u32 get_ppgtt_flag(struct xe_sched_job *job)
272 {
273 	if (job->q->vm && !job->ggtt)
274 		return BIT(8);
275 
276 	return 0;
277 }
278 
279 static int emit_copy_timestamp(struct xe_device *xe, struct xe_lrc *lrc,
280 			       u32 *dw, int i)
281 {
282 	const struct xe_reg reg = xe_lrc_is_multi_queue(lrc) ?
283 				   RING_QUEUE_TIMESTAMP(0) :
284 				   RING_CTX_TIMESTAMP(0);
285 
286 	dw[i++] = MI_STORE_REGISTER_MEM | MI_SRM_USE_GGTT | MI_SRM_ADD_CS_OFFSET;
287 	dw[i++] = reg.addr;
288 	dw[i++] = xe_lrc_ctx_job_timestamp_ggtt_addr(lrc);
289 	dw[i++] = 0;
290 
291 	/*
292 	 * Ensure CTX timestamp >= Job timestamp during VF sampling to avoid
293 	 * arithmetic wraparound in TDR.
294 	 */
295 	if (IS_SRIOV_VF(xe)) {
296 		dw[i++] = MI_STORE_REGISTER_MEM | MI_SRM_USE_GGTT |
297 			MI_SRM_ADD_CS_OFFSET;
298 		dw[i++] = reg.addr;
299 		dw[i++] = xe_lrc_ctx_timestamp_ggtt_addr(lrc);
300 		dw[i++] = 0;
301 	}
302 
303 	return i;
304 }
305 
306 static int emit_fake_watchdog(struct xe_lrc *lrc, u32 *dw, int i)
307 {
308 	/*
309 	 * Setup a watchdog with impossible condition to always trigger an
310 	 * hardware interrupt that would force the GuC to reset the engine.
311 	 */
312 
313 	dw[i++] = MI_LOAD_REGISTER_IMM | MI_LRI_NUM_REGS(2) | MI_LRI_LRM_CS_MMIO;
314 	dw[i++] = PR_CTR_THRSH(0).addr;
315 	dw[i++] = 2; /* small threshold */
316 	dw[i++] = PR_CTR_CTRL(0).addr;
317 	dw[i++] = CTR_LOGIC_OP(START);
318 
319 	dw[i++] = MI_SEMAPHORE_WAIT | MI_SEMW_GGTT | MI_SEMW_POLL | MI_SEMW_COMPARE(SAD_EQ_SDD);
320 	dw[i++] = 0xdead; /* this should never be seen */
321 	dw[i++] = lower_32_bits(xe_lrc_ggtt_addr(lrc));
322 	dw[i++] = upper_32_bits(xe_lrc_ggtt_addr(lrc));
323 	dw[i++] = 0; /* unused token */
324 
325 	dw[i++] = MI_LOAD_REGISTER_IMM | MI_LRI_NUM_REGS(1) | MI_LRI_LRM_CS_MMIO;
326 	dw[i++] = PR_CTR_CTRL(0).addr;
327 	dw[i++] = CTR_LOGIC_OP(STOP);
328 
329 	return i;
330 }
331 
332 /* for engines that don't require any special HW handling (no EUs, no aux inval, etc) */
333 static void __emit_job_gen12_simple(struct xe_sched_job *job, struct xe_lrc *lrc,
334 				    u64 batch_addr, u32 *head, u32 seqno)
335 {
336 	u32 dw[MAX_JOB_SIZE_DW], i = 0;
337 	u32 ppgtt_flag = get_ppgtt_flag(job);
338 	struct xe_gt *gt = job->q->gt;
339 
340 	*head = lrc->ring.tail;
341 
342 	if (job->ring_ops_force_reset)
343 		i = emit_fake_watchdog(lrc, dw, i);
344 
345 	i = emit_copy_timestamp(gt_to_xe(gt), lrc, dw, i);
346 
347 	if (job->ring_ops_flush_tlb) {
348 		dw[i++] = preparser_disable(true);
349 		i = emit_flush_imm_ggtt(xe_lrc_start_seqno_ggtt_addr(lrc),
350 					seqno, MI_INVALIDATE_TLB, dw, i);
351 		dw[i++] = preparser_disable(false);
352 	} else {
353 		i = emit_store_imm_ggtt(xe_lrc_start_seqno_ggtt_addr(lrc),
354 					seqno, dw, i);
355 	}
356 
357 	i = emit_bb_start(batch_addr, ppgtt_flag, dw, i);
358 
359 	/* Don't preempt fence signaling */
360 	dw[i++] = MI_ARB_ON_OFF | MI_ARB_DISABLE;
361 
362 	if (job->user_fence.used) {
363 		i = emit_flush_dw(dw, i);
364 		i = emit_store_imm_ppgtt_posted(job->user_fence.addr,
365 						job->user_fence.value,
366 						dw, i);
367 	}
368 
369 	i = emit_flush_imm_ggtt(xe_lrc_seqno_ggtt_addr(lrc), seqno, 0, dw, i);
370 
371 	i = emit_user_interrupt(dw, i);
372 
373 	xe_gt_assert(gt, i <= MAX_JOB_SIZE_DW);
374 
375 	xe_lrc_write_ring(lrc, dw, i * sizeof(*dw));
376 }
377 
378 static bool has_aux_ccs(struct xe_device *xe)
379 {
380 	/*
381 	 * PVC is a special case that has no compression of either type
382 	 * (FlatCCS or AuxCCS).  Also, AuxCCS is no longer used from Xe2
383 	 * onward, so any future platforms with no FlatCCS will not have
384 	 * AuxCCS, and we explicitly do not want to support it on MTL.
385 	 */
386 	if (GRAPHICS_VERx100(xe) >= 1270 || xe->info.platform == XE_PVC)
387 		return false;
388 
389 	return !xe->info.has_flat_ccs;
390 }
391 
392 static void __emit_job_gen12_video(struct xe_sched_job *job, struct xe_lrc *lrc,
393 				   u64 batch_addr, u32 *head, u32 seqno)
394 {
395 	u32 dw[MAX_JOB_SIZE_DW], i = 0;
396 	u32 ppgtt_flag = get_ppgtt_flag(job);
397 	struct xe_gt *gt = job->q->gt;
398 	struct xe_device *xe = gt_to_xe(gt);
399 
400 	*head = lrc->ring.tail;
401 
402 	if (job->ring_ops_force_reset)
403 		i = emit_fake_watchdog(lrc, dw, i);
404 
405 	i = emit_copy_timestamp(xe, lrc, dw, i);
406 
407 	dw[i++] = preparser_disable(true);
408 
409 	/* hsdes: 1809175790 */
410 	i = emit_aux_table_inv(job->q->hwe, dw, i);
411 
412 	if (job->ring_ops_flush_tlb)
413 		i = emit_flush_imm_ggtt(xe_lrc_start_seqno_ggtt_addr(lrc),
414 					seqno, MI_INVALIDATE_TLB, dw, i);
415 
416 	dw[i++] = preparser_disable(false);
417 
418 	if (!job->ring_ops_flush_tlb)
419 		i = emit_store_imm_ggtt(xe_lrc_start_seqno_ggtt_addr(lrc),
420 					seqno, dw, i);
421 
422 	i = emit_bb_start(batch_addr, ppgtt_flag, dw, i);
423 
424 	/* Don't preempt fence signaling */
425 	dw[i++] = MI_ARB_ON_OFF | MI_ARB_DISABLE;
426 
427 	if (job->user_fence.used) {
428 		i = emit_flush_dw(dw, i);
429 		i = emit_store_imm_ppgtt_posted(job->user_fence.addr,
430 						job->user_fence.value,
431 						dw, i);
432 	}
433 
434 	i = emit_flush_imm_ggtt(xe_lrc_seqno_ggtt_addr(lrc), seqno, 0, dw, i);
435 
436 	i = emit_user_interrupt(dw, i);
437 
438 	xe_gt_assert(gt, i <= MAX_JOB_SIZE_DW);
439 
440 	xe_lrc_write_ring(lrc, dw, i * sizeof(*dw));
441 }
442 
443 static void __emit_job_gen12_render_compute(struct xe_sched_job *job,
444 					    struct xe_lrc *lrc,
445 					    u64 batch_addr, u32 *head,
446 					    u32 seqno)
447 {
448 	u32 dw[MAX_JOB_SIZE_DW], i = 0;
449 	u32 ppgtt_flag = get_ppgtt_flag(job);
450 	struct xe_gt *gt = job->q->gt;
451 	struct xe_device *xe = gt_to_xe(gt);
452 	bool lacks_render = !(gt->info.engine_mask & XE_HW_ENGINE_RCS_MASK);
453 	u32 mask_flags = 0;
454 
455 	*head = lrc->ring.tail;
456 
457 	if (job->ring_ops_force_reset)
458 		i = emit_fake_watchdog(lrc, dw, i);
459 
460 	i = emit_copy_timestamp(xe, lrc, dw, i);
461 
462 	/*
463 	 * On AuxCCS platforms the invalidation of the Aux table requires
464 	 * quiescing the memory traffic beforehand.
465 	 */
466 	if (has_aux_ccs(xe))
467 		i = emit_render_cache_flush(job, dw, i);
468 
469 	dw[i++] = preparser_disable(true);
470 	if (lacks_render)
471 		mask_flags = PIPE_CONTROL_3D_ARCH_FLAGS;
472 	else if (job->q->class == XE_ENGINE_CLASS_COMPUTE)
473 		mask_flags = PIPE_CONTROL_3D_ENGINE_FLAGS;
474 
475 	/* See __xe_pt_bind_vma() for a discussion on TLB invalidations. */
476 	i = emit_pipe_invalidate(job->q, mask_flags, job->ring_ops_flush_tlb, dw, i);
477 
478 	/* hsdes: 1809175790 */
479 	i = emit_aux_table_inv(job->q->hwe, dw, i);
480 
481 	dw[i++] = preparser_disable(false);
482 
483 	i = emit_store_imm_ggtt(xe_lrc_start_seqno_ggtt_addr(lrc),
484 				seqno, dw, i);
485 
486 	i = emit_bb_start(batch_addr, ppgtt_flag, dw, i);
487 
488 	/* Don't preempt fence signaling */
489 	dw[i++] = MI_ARB_ON_OFF | MI_ARB_DISABLE;
490 
491 	i = emit_render_cache_flush(job, dw, i);
492 
493 	if (job->user_fence.used)
494 		i = emit_store_imm_ppgtt_posted(job->user_fence.addr,
495 						job->user_fence.value,
496 						dw, i);
497 
498 	i = emit_pipe_imm_ggtt(job->q, xe_lrc_seqno_ggtt_addr(lrc), seqno, lacks_render, dw, i);
499 
500 	i = emit_user_interrupt(dw, i);
501 
502 	xe_gt_assert(gt, i <= MAX_JOB_SIZE_DW);
503 
504 	xe_lrc_write_ring(lrc, dw, i * sizeof(*dw));
505 }
506 
507 static int emit_ulls_preamble(struct xe_lrc *lrc, u32 *dw, int i, u32 seqno)
508 {
509 	u32 addr = xe_lrc_ulls_semaphore_ggtt_addr(lrc, seqno);
510 
511 	return emit_store_imm_ggtt(addr, LRC_MIGRATION_ULLS_SEMAPHORE_CLEAR,
512 				   dw, i);
513 }
514 
515 /*
516  * Advance the ring tail from within the ring, so submitting the next ULLS job
517  * needs nothing from the CPU beyond signalling the semaphore. All ULLS jobs
518  * occupy exactly ULLS_JOB_SIZE_BYTES, so the tail the next job ends at is two
519  * job slots on from where this job started, even though that job has not been
520  * emitted yet. Both LRC and MMIO ring tail advanced in step.
521  */
522 static int emit_ulls_ring_tail(struct xe_gt *gt, struct xe_lrc *lrc, u32 *dw,
523 			       int i, u32 head)
524 {
525 	u32 next_tail = (head + 2 * ULLS_JOB_SIZE_BYTES) & (lrc->ring.size - 1);
526 
527 	xe_gt_assert(gt, IS_ALIGNED(next_tail, 8));
528 
529 	i = emit_store_imm_ggtt(xe_lrc_ring_tail_ggtt_addr(lrc), next_tail,
530 				dw, i);
531 
532 	dw[i++] = MI_LOAD_REGISTER_IMM | MI_LRI_NUM_REGS(1) |
533 		MI_LRI_LRM_CS_MMIO;
534 	dw[i++] = RING_TAIL(0).addr;
535 	dw[i++] = next_tail;
536 
537 	return i;
538 }
539 
540 /*
541  * Park the engine on the next job's semaphore, then publish its tail.
542  *
543  * The tail must not be published before the wait. The command streamer
544  * fetches ring contents up to the tail while parked, so it would fetch the
545  * next job's slot while it still holds MI_NOOPs and execute those once
546  * released, rather than the job the CPU writes there later. Publishing after
547  * the wait keeps the slot beyond the tail until the job is in place.
548  */
549 static int emit_ulls_postamble(struct xe_gt *gt, struct xe_lrc *lrc, u32 *dw,
550 			       int i, u32 seqno, u32 head)
551 {
552 	dw[i++] = MI_SEMAPHORE_WAIT |
553 		MI_SEMW_GGTT |
554 		MI_SEMW_POLL |
555 		MI_SEMW_COMPARE(SAD_EQ_SDD);
556 	dw[i++] = LRC_MIGRATION_ULLS_SEMAPHORE_SIGNAL;
557 	dw[i++] = xe_lrc_ulls_semaphore_ggtt_addr(lrc, seqno + 1);
558 	dw[i++] = 0;
559 	dw[i++] = 0;
560 
561 	return emit_ulls_ring_tail(gt, lrc, dw, i, head);
562 }
563 
564 /* Pad out to the fixed ULLS job size */
565 static int emit_ulls_pad(struct xe_gt *gt, u32 *dw, int i)
566 {
567 	xe_gt_assert(gt, i <= ULLS_JOB_SIZE_DW);
568 
569 	while (i < ULLS_JOB_SIZE_DW)
570 		dw[i++] = MI_NOOP;
571 
572 	return i;
573 }
574 
575 static void emit_migration_job_gen12(struct xe_sched_job *job,
576 				     struct xe_lrc *lrc, u32 *head,
577 				     u32 seqno)
578 {
579 	struct xe_gt *gt = job->q->gt;
580 	struct xe_device *xe = gt_to_xe(gt);
581 	u32 saddr = xe_lrc_start_seqno_ggtt_addr(lrc);
582 	u32 dw[MAX_JOB_SIZE_DW], i = 0;
583 
584 	*head = lrc->ring.tail;
585 
586 	xe_gt_assert(gt, !job->ring_ops_force_reset);
587 
588 	if (xe_sched_job_is_ulls(job))
589 		i = emit_ulls_preamble(lrc, dw, i, seqno);
590 
591 	i = emit_copy_timestamp(xe, lrc, dw, i);
592 
593 	i = emit_store_imm_ggtt(saddr, seqno, dw, i);
594 
595 	if (!xe_sched_job_ulls_has_batch(job))
596 		goto seqno_write;
597 
598 	dw[i++] = MI_ARB_ON_OFF | MI_ARB_DISABLE; /* Enabled again below */
599 
600 	i = emit_bb_start(job->ptrs[0].batch_addr, BIT(8), dw, i);
601 
602 	dw[i++] = preparser_disable(true);
603 	i = emit_flush_invalidate(saddr, seqno, job->migrate_flush_flags, dw, i);
604 	dw[i++] = preparser_disable(false);
605 
606 	i = emit_bb_start(job->ptrs[1].batch_addr, BIT(8), dw, i);
607 
608 seqno_write:
609 	i = emit_flush_imm_ggtt(xe_lrc_seqno_ggtt_addr(lrc), seqno,
610 				job->migrate_flush_flags,
611 				dw, i);
612 
613 	i = emit_user_interrupt(dw, i);
614 
615 	if (xe_sched_job_ulls_parks(job))
616 		i = emit_ulls_postamble(gt, lrc, dw, i, seqno, *head);
617 
618 	if (xe_sched_job_is_ulls(job))
619 		i = emit_ulls_pad(gt, dw, i);
620 
621 	xe_gt_assert(job->q->gt, i <= MAX_JOB_SIZE_DW);
622 
623 	xe_lrc_write_ring(lrc, dw, i * sizeof(*dw));
624 }
625 
626 static void emit_job_gen12_gsc(struct xe_sched_job *job)
627 {
628 	struct xe_gt *gt = job->q->gt;
629 
630 	xe_gt_assert(gt, job->q->width <= 1); /* no parallel submission for GSCCS */
631 
632 	__emit_job_gen12_simple(job, job->q->lrc[0],
633 				job->ptrs[0].batch_addr,
634 				&job->ptrs[0].head,
635 				xe_sched_job_lrc_seqno(job));
636 }
637 
638 static void emit_job_gen12_copy(struct xe_sched_job *job)
639 {
640 	int i;
641 
642 	if (xe_sched_job_is_migration(job->q)) {
643 		emit_migration_job_gen12(job, job->q->lrc[0],
644 					 &job->ptrs[0].head,
645 					 xe_sched_job_lrc_seqno(job));
646 		return;
647 	}
648 
649 	for (i = 0; i < job->q->width; ++i)
650 		__emit_job_gen12_simple(job, job->q->lrc[i],
651 					job->ptrs[i].batch_addr,
652 					&job->ptrs[i].head,
653 					xe_sched_job_lrc_seqno(job));
654 }
655 
656 static void emit_job_gen12_video(struct xe_sched_job *job)
657 {
658 	int i;
659 
660 	/* FIXME: Not doing parallel handshake for now */
661 	for (i = 0; i < job->q->width; ++i)
662 		__emit_job_gen12_video(job, job->q->lrc[i],
663 				       job->ptrs[i].batch_addr,
664 				       &job->ptrs[i].head,
665 				       xe_sched_job_lrc_seqno(job));
666 }
667 
668 static void emit_job_gen12_render_compute(struct xe_sched_job *job)
669 {
670 	int i;
671 
672 	for (i = 0; i < job->q->width; ++i)
673 		__emit_job_gen12_render_compute(job, job->q->lrc[i],
674 						job->ptrs[i].batch_addr,
675 						&job->ptrs[i].head,
676 						xe_sched_job_lrc_seqno(job));
677 }
678 
679 static const struct xe_ring_ops ring_ops_gen12_gsc = {
680 	.emit_job = emit_job_gen12_gsc,
681 };
682 
683 static const struct xe_ring_ops ring_ops_gen12_copy = {
684 	.emit_job = emit_job_gen12_copy,
685 };
686 
687 static const struct xe_ring_ops ring_ops_gen12_video_decode = {
688 	.emit_job = emit_job_gen12_video,
689 };
690 
691 static const struct xe_ring_ops ring_ops_gen12_video_enhance = {
692 	.emit_job = emit_job_gen12_video,
693 };
694 
695 static const struct xe_ring_ops ring_ops_gen12_render_compute = {
696 	.emit_job = emit_job_gen12_render_compute,
697 };
698 
699 static const struct xe_ring_ops auxccs_ring_ops_gen12_video_decode = {
700 	.emit_job = emit_job_gen12_video,
701 	.emit_aux_table_inv = emit_aux_table_inv_video_decode,
702 };
703 
704 static const struct xe_ring_ops auxccs_ring_ops_gen12_video_enhance = {
705 	.emit_job = emit_job_gen12_video,
706 	.emit_aux_table_inv = emit_aux_table_inv_video_enhance,
707 };
708 
709 static const struct xe_ring_ops auxccs_ring_ops_gen12_render_compute = {
710 	.emit_job = emit_job_gen12_render_compute,
711 	.emit_aux_table_inv = emit_aux_table_inv_render_compute,
712 };
713 
714 const struct xe_ring_ops *
715 xe_ring_ops_get(struct xe_gt *gt, enum xe_engine_class class)
716 {
717 	struct xe_device *xe = gt_to_xe(gt);
718 
719 	switch (class) {
720 	case XE_ENGINE_CLASS_OTHER:
721 		return &ring_ops_gen12_gsc;
722 	case XE_ENGINE_CLASS_COPY:
723 		return &ring_ops_gen12_copy;
724 	case XE_ENGINE_CLASS_VIDEO_DECODE:
725 		if (has_aux_ccs(xe))
726 			return &auxccs_ring_ops_gen12_video_decode;
727 		else
728 			return &ring_ops_gen12_video_decode;
729 	case XE_ENGINE_CLASS_VIDEO_ENHANCE:
730 		if (has_aux_ccs(xe))
731 			return &auxccs_ring_ops_gen12_video_enhance;
732 		else
733 			return &ring_ops_gen12_video_enhance;
734 	case XE_ENGINE_CLASS_RENDER:
735 	case XE_ENGINE_CLASS_COMPUTE:
736 		if (has_aux_ccs(xe))
737 			return &auxccs_ring_ops_gen12_render_compute;
738 		else
739 			return &ring_ops_gen12_render_compute;
740 	default:
741 		return NULL;
742 	}
743 }
744