xref: /linux/drivers/gpu/drm/i915/display/intel_dsb.c (revision a4871e6201c46c8e1d04308265b4b4c5753c8209)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2019 Intel Corporation
4  *
5  */
6 
7 #include <drm/drm_vblank.h>
8 
9 #include "i915_drv.h"
10 #include "i915_irq.h"
11 #include "i915_reg.h"
12 #include "intel_crtc.h"
13 #include "intel_de.h"
14 #include "intel_display_rpm.h"
15 #include "intel_display_types.h"
16 #include "intel_dsb.h"
17 #include "intel_dsb_buffer.h"
18 #include "intel_dsb_regs.h"
19 #include "intel_vblank.h"
20 #include "intel_vrr.h"
21 #include "skl_watermark.h"
22 
23 #define CACHELINE_BYTES 64
24 
25 struct intel_dsb {
26 	enum intel_dsb_id id;
27 
28 	struct intel_dsb_buffer dsb_buf;
29 	struct intel_crtc *crtc;
30 
31 	/*
32 	 * maximum number of dwords the buffer will hold.
33 	 */
34 	unsigned int size;
35 
36 	/*
37 	 * free_pos will point the first free dword and
38 	 * help in calculating tail of command buffer.
39 	 */
40 	unsigned int free_pos;
41 
42 	/*
43 	 * Previously emitted DSB instruction. Used to
44 	 * identify/adjust the instruction for indexed
45 	 * register writes.
46 	 */
47 	u32 ins[2];
48 
49 	/*
50 	 * Start of the previously emitted DSB instruction.
51 	 * Used to adjust the instruction for indexed
52 	 * register writes.
53 	 */
54 	unsigned int ins_start_offset;
55 
56 	u32 chicken;
57 	int hw_dewake_scanline;
58 };
59 
60 /**
61  * DOC: DSB
62  *
63  * A DSB (Display State Buffer) is a queue of MMIO instructions in the memory
64  * which can be offloaded to DSB HW in Display Controller. DSB HW is a DMA
65  * engine that can be programmed to download the DSB from memory.
66  * It allows driver to batch submit display HW programming. This helps to
67  * reduce loading time and CPU activity, thereby making the context switch
68  * faster. DSB Support added from Gen12 Intel graphics based platform.
69  *
70  * DSB's can access only the pipe, plane, and transcoder Data Island Packet
71  * registers.
72  *
73  * DSB HW can support only register writes (both indexed and direct MMIO
74  * writes). There are no registers reads possible with DSB HW engine.
75  */
76 
77 /* DSB opcodes. */
78 #define DSB_OPCODE_SHIFT		24
79 #define DSB_OPCODE_NOOP			0x0
80 #define DSB_OPCODE_MMIO_WRITE		0x1
81 #define   DSB_BYTE_EN			0xf
82 #define   DSB_BYTE_EN_SHIFT		20
83 #define   DSB_REG_VALUE_MASK		0xfffff
84 #define DSB_OPCODE_WAIT_USEC		0x2
85 #define DSB_OPCODE_WAIT_SCANLINE	0x3
86 #define DSB_OPCODE_WAIT_VBLANKS		0x4
87 #define DSB_OPCODE_WAIT_DSL_IN		0x5
88 #define DSB_OPCODE_WAIT_DSL_OUT		0x6
89 #define   DSB_SCANLINE_UPPER_SHIFT	20
90 #define   DSB_SCANLINE_LOWER_SHIFT	0
91 #define DSB_OPCODE_INTERRUPT		0x7
92 #define DSB_OPCODE_INDEXED_WRITE	0x9
93 /* see DSB_REG_VALUE_MASK */
94 #define DSB_OPCODE_POLL			0xA
95 /* see DSB_REG_VALUE_MASK */
96 
97 static bool pre_commit_is_vrr_active(struct intel_atomic_state *state,
98 				     struct intel_crtc *crtc)
99 {
100 	const struct intel_crtc_state *old_crtc_state =
101 		intel_atomic_get_old_crtc_state(state, crtc);
102 	const struct intel_crtc_state *new_crtc_state =
103 		intel_atomic_get_new_crtc_state(state, crtc);
104 
105 	/* VRR will be enabled afterwards, if necessary */
106 	if (intel_crtc_needs_modeset(new_crtc_state))
107 		return false;
108 
109 	/* VRR will have been disabled during intel_pre_plane_update() */
110 	return old_crtc_state->vrr.enable && !intel_crtc_vrr_disabling(state, crtc);
111 }
112 
113 static int dsb_vblank_delay(struct intel_atomic_state *state,
114 			    struct intel_crtc *crtc)
115 {
116 	const struct intel_crtc_state *crtc_state =
117 		intel_pre_commit_crtc_state(state, crtc);
118 
119 	if (pre_commit_is_vrr_active(state, crtc))
120 		/*
121 		 * When the push is sent during vblank it will trigger
122 		 * on the next scanline, hence we have up to one extra
123 		 * scanline until the delayed vblank occurs after
124 		 * TRANS_PUSH has been written.
125 		 */
126 		return intel_vrr_vblank_delay(crtc_state) + 1;
127 	else
128 		return intel_mode_vblank_delay(&crtc_state->hw.adjusted_mode);
129 }
130 
131 static int dsb_vtotal(struct intel_atomic_state *state,
132 		      struct intel_crtc *crtc)
133 {
134 	const struct intel_crtc_state *crtc_state =
135 		intel_pre_commit_crtc_state(state, crtc);
136 
137 	if (pre_commit_is_vrr_active(state, crtc))
138 		return intel_vrr_vmax_vtotal(crtc_state);
139 	else
140 		return intel_mode_vtotal(&crtc_state->hw.adjusted_mode);
141 }
142 
143 static int dsb_dewake_scanline_start(struct intel_atomic_state *state,
144 				     struct intel_crtc *crtc)
145 {
146 	struct intel_display *display = to_intel_display(state);
147 	const struct intel_crtc_state *crtc_state =
148 		intel_pre_commit_crtc_state(state, crtc);
149 	unsigned int latency = skl_watermark_max_latency(display, 0);
150 
151 	return intel_mode_vdisplay(&crtc_state->hw.adjusted_mode) -
152 		intel_usecs_to_scanlines(&crtc_state->hw.adjusted_mode, latency);
153 }
154 
155 static int dsb_dewake_scanline_end(struct intel_atomic_state *state,
156 				   struct intel_crtc *crtc)
157 {
158 	const struct intel_crtc_state *crtc_state =
159 		intel_pre_commit_crtc_state(state, crtc);
160 
161 	return intel_mode_vdisplay(&crtc_state->hw.adjusted_mode);
162 }
163 
164 static int dsb_scanline_to_hw(struct intel_atomic_state *state,
165 			      struct intel_crtc *crtc, int scanline)
166 {
167 	const struct intel_crtc_state *crtc_state =
168 		intel_pre_commit_crtc_state(state, crtc);
169 	int vtotal = dsb_vtotal(state, crtc);
170 
171 	return (scanline + vtotal - intel_crtc_scanline_offset(crtc_state)) % vtotal;
172 }
173 
174 /*
175  * Bspec suggests that we should always set DSB_SKIP_WAITS_EN. We have approach
176  * different from what is explained in Bspec on how flip is considered being
177  * complete. We are waiting for vblank in DSB and generate interrupt when it
178  * happens and this interrupt is considered as indication of completion -> we
179  * definitely do not want to skip vblank wait. We also have concern what comes
180  * to skipping vblank evasion. I.e. arming registers are latched before we have
181  * managed writing them. Due to these reasons we are not setting
182  * DSB_SKIP_WAITS_EN.
183  */
184 static u32 dsb_chicken(struct intel_atomic_state *state,
185 		       struct intel_crtc *crtc)
186 {
187 	if (pre_commit_is_vrr_active(state, crtc))
188 		return DSB_CTRL_WAIT_SAFE_WINDOW |
189 			DSB_CTRL_NO_WAIT_VBLANK |
190 			DSB_INST_WAIT_SAFE_WINDOW |
191 			DSB_INST_NO_WAIT_VBLANK;
192 	else
193 		return 0;
194 }
195 
196 static bool assert_dsb_has_room(struct intel_dsb *dsb)
197 {
198 	struct intel_crtc *crtc = dsb->crtc;
199 	struct intel_display *display = to_intel_display(crtc->base.dev);
200 
201 	/* each instruction is 2 dwords */
202 	return !drm_WARN(display->drm, dsb->free_pos > dsb->size - 2,
203 			 "[CRTC:%d:%s] DSB %d buffer overflow\n",
204 			 crtc->base.base.id, crtc->base.name, dsb->id);
205 }
206 
207 static void intel_dsb_dump(struct intel_dsb *dsb)
208 {
209 	struct intel_crtc *crtc = dsb->crtc;
210 	struct intel_display *display = to_intel_display(crtc->base.dev);
211 	int i;
212 
213 	drm_dbg_kms(display->drm, "[CRTC:%d:%s] DSB %d commands {\n",
214 		    crtc->base.base.id, crtc->base.name, dsb->id);
215 	for (i = 0; i < ALIGN(dsb->free_pos, 64 / 4); i += 4)
216 		drm_dbg_kms(display->drm,
217 			    " 0x%08x: 0x%08x 0x%08x 0x%08x 0x%08x\n", i * 4,
218 			    intel_dsb_buffer_read(&dsb->dsb_buf, i),
219 			    intel_dsb_buffer_read(&dsb->dsb_buf, i + 1),
220 			    intel_dsb_buffer_read(&dsb->dsb_buf, i + 2),
221 			    intel_dsb_buffer_read(&dsb->dsb_buf, i + 3));
222 	drm_dbg_kms(display->drm, "}\n");
223 }
224 
225 static bool is_dsb_busy(struct intel_display *display, enum pipe pipe,
226 			enum intel_dsb_id dsb_id)
227 {
228 	return intel_de_read_fw(display, DSB_CTRL(pipe, dsb_id)) & DSB_STATUS_BUSY;
229 }
230 
231 static void intel_dsb_emit(struct intel_dsb *dsb, u32 ldw, u32 udw)
232 {
233 	if (!assert_dsb_has_room(dsb))
234 		return;
235 
236 	/* Every instruction should be 8 byte aligned. */
237 	dsb->free_pos = ALIGN(dsb->free_pos, 2);
238 
239 	dsb->ins_start_offset = dsb->free_pos;
240 	dsb->ins[0] = ldw;
241 	dsb->ins[1] = udw;
242 
243 	intel_dsb_buffer_write(&dsb->dsb_buf, dsb->free_pos++, dsb->ins[0]);
244 	intel_dsb_buffer_write(&dsb->dsb_buf, dsb->free_pos++, dsb->ins[1]);
245 }
246 
247 static bool intel_dsb_prev_ins_is_write(struct intel_dsb *dsb,
248 					u32 opcode, i915_reg_t reg)
249 {
250 	u32 prev_opcode, prev_reg;
251 
252 	/*
253 	 * Nothing emitted yet? Must check before looking
254 	 * at the actual data since i915_gem_object_create_internal()
255 	 * does *not* give you zeroed memory!
256 	 */
257 	if (dsb->free_pos == 0)
258 		return false;
259 
260 	prev_opcode = dsb->ins[1] & ~DSB_REG_VALUE_MASK;
261 	prev_reg =  dsb->ins[1] & DSB_REG_VALUE_MASK;
262 
263 	return prev_opcode == opcode && prev_reg == i915_mmio_reg_offset(reg);
264 }
265 
266 static bool intel_dsb_prev_ins_is_indexed_write(struct intel_dsb *dsb, i915_reg_t reg)
267 {
268 	return intel_dsb_prev_ins_is_write(dsb,
269 					   DSB_OPCODE_INDEXED_WRITE << DSB_OPCODE_SHIFT,
270 					   reg);
271 }
272 
273 /**
274  * intel_dsb_reg_write_indexed() - Emit indexed register write to the DSB context
275  * @dsb: DSB context
276  * @reg: register address.
277  * @val: value.
278  *
279  * This function is used for writing register-value pair in command
280  * buffer of DSB.
281  *
282  * Note that indexed writes are slower than normal MMIO writes
283  * for a small number (less than 5 or so) of writes to the same
284  * register.
285  */
286 void intel_dsb_reg_write_indexed(struct intel_dsb *dsb,
287 				 i915_reg_t reg, u32 val)
288 {
289 	/*
290 	 * For example the buffer will look like below for 3 dwords for auto
291 	 * increment register:
292 	 * +--------------------------------------------------------+
293 	 * | size = 3 | offset &| value1 | value2 | value3 | zero   |
294 	 * |          | opcode  |        |        |        |        |
295 	 * +--------------------------------------------------------+
296 	 * +          +         +        +        +        +        +
297 	 * 0          4         8        12       16       20       24
298 	 * Byte
299 	 *
300 	 * As every instruction is 8 byte aligned the index of dsb instruction
301 	 * will start always from even number while dealing with u32 array. If
302 	 * we are writing odd no of dwords, Zeros will be added in the end for
303 	 * padding.
304 	 */
305 	if (!intel_dsb_prev_ins_is_indexed_write(dsb, reg))
306 		intel_dsb_emit(dsb, 0, /* count */
307 			       (DSB_OPCODE_INDEXED_WRITE << DSB_OPCODE_SHIFT) |
308 			       i915_mmio_reg_offset(reg));
309 
310 	if (!assert_dsb_has_room(dsb))
311 		return;
312 
313 	/* Update the count */
314 	dsb->ins[0]++;
315 	intel_dsb_buffer_write(&dsb->dsb_buf, dsb->ins_start_offset + 0,
316 			       dsb->ins[0]);
317 
318 	intel_dsb_buffer_write(&dsb->dsb_buf, dsb->free_pos++, val);
319 	/* if number of data words is odd, then the last dword should be 0.*/
320 	if (dsb->free_pos & 0x1)
321 		intel_dsb_buffer_write(&dsb->dsb_buf, dsb->free_pos, 0);
322 }
323 
324 void intel_dsb_reg_write(struct intel_dsb *dsb,
325 			 i915_reg_t reg, u32 val)
326 {
327 	intel_dsb_emit(dsb, val,
328 		       (DSB_OPCODE_MMIO_WRITE << DSB_OPCODE_SHIFT) |
329 		       (DSB_BYTE_EN << DSB_BYTE_EN_SHIFT) |
330 		       i915_mmio_reg_offset(reg));
331 }
332 
333 static u32 intel_dsb_mask_to_byte_en(u32 mask)
334 {
335 	return (!!(mask & 0xff000000) << 3 |
336 		!!(mask & 0x00ff0000) << 2 |
337 		!!(mask & 0x0000ff00) << 1 |
338 		!!(mask & 0x000000ff) << 0);
339 }
340 
341 /* Note: mask implemented via byte enables! */
342 void intel_dsb_reg_write_masked(struct intel_dsb *dsb,
343 				i915_reg_t reg, u32 mask, u32 val)
344 {
345 	intel_dsb_emit(dsb, val,
346 		       (DSB_OPCODE_MMIO_WRITE << DSB_OPCODE_SHIFT) |
347 		       (intel_dsb_mask_to_byte_en(mask) << DSB_BYTE_EN_SHIFT) |
348 		       i915_mmio_reg_offset(reg));
349 }
350 
351 void intel_dsb_noop(struct intel_dsb *dsb, int count)
352 {
353 	int i;
354 
355 	for (i = 0; i < count; i++)
356 		intel_dsb_emit(dsb, 0,
357 			       DSB_OPCODE_NOOP << DSB_OPCODE_SHIFT);
358 }
359 
360 void intel_dsb_nonpost_start(struct intel_dsb *dsb)
361 {
362 	struct intel_crtc *crtc = dsb->crtc;
363 	enum pipe pipe = crtc->pipe;
364 
365 	intel_dsb_reg_write_masked(dsb, DSB_CTRL(pipe, dsb->id),
366 				   DSB_NON_POSTED, DSB_NON_POSTED);
367 	intel_dsb_noop(dsb, 4);
368 }
369 
370 void intel_dsb_nonpost_end(struct intel_dsb *dsb)
371 {
372 	struct intel_crtc *crtc = dsb->crtc;
373 	enum pipe pipe = crtc->pipe;
374 
375 	intel_dsb_reg_write_masked(dsb, DSB_CTRL(pipe, dsb->id),
376 				   DSB_NON_POSTED, 0);
377 	intel_dsb_noop(dsb, 4);
378 }
379 
380 void intel_dsb_interrupt(struct intel_dsb *dsb)
381 {
382 	intel_dsb_emit(dsb, 0,
383 		       DSB_OPCODE_INTERRUPT << DSB_OPCODE_SHIFT);
384 }
385 
386 void intel_dsb_wait_usec(struct intel_dsb *dsb, int count)
387 {
388 	/* +1 to make sure we never wait less time than asked for */
389 	intel_dsb_emit(dsb, count + 1,
390 		       DSB_OPCODE_WAIT_USEC << DSB_OPCODE_SHIFT);
391 }
392 
393 void intel_dsb_wait_vblanks(struct intel_dsb *dsb, int count)
394 {
395 	intel_dsb_emit(dsb, count,
396 		       DSB_OPCODE_WAIT_VBLANKS << DSB_OPCODE_SHIFT);
397 }
398 
399 static void intel_dsb_emit_wait_dsl(struct intel_dsb *dsb,
400 				    u32 opcode, int lower, int upper)
401 {
402 	u64 window = ((u64)upper << DSB_SCANLINE_UPPER_SHIFT) |
403 		((u64)lower << DSB_SCANLINE_LOWER_SHIFT);
404 
405 	intel_dsb_emit(dsb, lower_32_bits(window),
406 		       (opcode << DSB_OPCODE_SHIFT) |
407 		       upper_32_bits(window));
408 }
409 
410 static void intel_dsb_wait_dsl(struct intel_atomic_state *state,
411 			       struct intel_dsb *dsb,
412 			       int lower_in, int upper_in,
413 			       int lower_out, int upper_out)
414 {
415 	struct intel_crtc *crtc = dsb->crtc;
416 
417 	lower_in = dsb_scanline_to_hw(state, crtc, lower_in);
418 	upper_in = dsb_scanline_to_hw(state, crtc, upper_in);
419 
420 	lower_out = dsb_scanline_to_hw(state, crtc, lower_out);
421 	upper_out = dsb_scanline_to_hw(state, crtc, upper_out);
422 
423 	if (upper_in >= lower_in)
424 		intel_dsb_emit_wait_dsl(dsb, DSB_OPCODE_WAIT_DSL_IN,
425 					lower_in, upper_in);
426 	else if (upper_out >= lower_out)
427 		intel_dsb_emit_wait_dsl(dsb, DSB_OPCODE_WAIT_DSL_OUT,
428 					lower_out, upper_out);
429 	else
430 		drm_WARN_ON(crtc->base.dev, 1); /* assert_dsl_ok() should have caught it already */
431 }
432 
433 static void assert_dsl_ok(struct intel_atomic_state *state,
434 			  struct intel_dsb *dsb,
435 			  int start, int end)
436 {
437 	struct intel_crtc *crtc = dsb->crtc;
438 	int vtotal = dsb_vtotal(state, crtc);
439 
440 	/*
441 	 * Waiting for the entire frame doesn't make sense,
442 	 * (IN==don't wait, OUT=wait forever).
443 	 */
444 	drm_WARN(crtc->base.dev, (end - start + vtotal) % vtotal == vtotal - 1,
445 		 "[CRTC:%d:%s] DSB %d bad scanline window wait: %d-%d (vt=%d)\n",
446 		 crtc->base.base.id, crtc->base.name, dsb->id,
447 		 start, end, vtotal);
448 }
449 
450 void intel_dsb_wait_scanline_in(struct intel_atomic_state *state,
451 				struct intel_dsb *dsb,
452 				int start, int end)
453 {
454 	assert_dsl_ok(state, dsb, start, end);
455 
456 	intel_dsb_wait_dsl(state, dsb,
457 			   start, end,
458 			   end + 1, start - 1);
459 }
460 
461 void intel_dsb_wait_scanline_out(struct intel_atomic_state *state,
462 				 struct intel_dsb *dsb,
463 				 int start, int end)
464 {
465 	assert_dsl_ok(state, dsb, start, end);
466 
467 	intel_dsb_wait_dsl(state, dsb,
468 			   end + 1, start - 1,
469 			   start, end);
470 }
471 
472 void intel_dsb_poll(struct intel_dsb *dsb,
473 		    i915_reg_t reg, u32 mask, u32 val,
474 		    int wait_us, int count)
475 {
476 	struct intel_crtc *crtc = dsb->crtc;
477 	enum pipe pipe = crtc->pipe;
478 
479 	intel_dsb_reg_write(dsb, DSB_POLLMASK(pipe, dsb->id), mask);
480 	intel_dsb_reg_write(dsb, DSB_POLLFUNC(pipe, dsb->id),
481 			    DSB_POLL_ENABLE |
482 			    DSB_POLL_WAIT(wait_us) | DSB_POLL_COUNT(count));
483 
484 	intel_dsb_noop(dsb, 5);
485 
486 	intel_dsb_emit(dsb, val,
487 		       (DSB_OPCODE_POLL << DSB_OPCODE_SHIFT) |
488 		       i915_mmio_reg_offset(reg));
489 }
490 
491 static void intel_dsb_align_tail(struct intel_dsb *dsb)
492 {
493 	u32 aligned_tail, tail;
494 
495 	tail = dsb->free_pos * 4;
496 	aligned_tail = ALIGN(tail, CACHELINE_BYTES);
497 
498 	if (aligned_tail > tail)
499 		intel_dsb_buffer_memset(&dsb->dsb_buf, dsb->free_pos, 0,
500 					aligned_tail - tail);
501 
502 	dsb->free_pos = aligned_tail / 4;
503 }
504 
505 void intel_dsb_finish(struct intel_dsb *dsb)
506 {
507 	struct intel_crtc *crtc = dsb->crtc;
508 
509 	/*
510 	 * DSB_FORCE_DEWAKE remains active even after DSB is
511 	 * disabled, so make sure to clear it (if set during
512 	 * intel_dsb_commit()). And clear DSB_ENABLE_DEWAKE as
513 	 * well for good measure.
514 	 */
515 	intel_dsb_reg_write(dsb, DSB_PMCTRL(crtc->pipe, dsb->id), 0);
516 	intel_dsb_reg_write_masked(dsb, DSB_PMCTRL_2(crtc->pipe, dsb->id),
517 				   DSB_FORCE_DEWAKE, 0);
518 
519 	intel_dsb_align_tail(dsb);
520 
521 	intel_dsb_buffer_flush_map(&dsb->dsb_buf);
522 }
523 
524 static u32 dsb_error_int_status(struct intel_display *display)
525 {
526 	u32 errors;
527 
528 	errors = DSB_GTT_FAULT_INT_STATUS |
529 		DSB_RSPTIMEOUT_INT_STATUS |
530 		DSB_POLL_ERR_INT_STATUS;
531 
532 	/*
533 	 * All the non-existing status bits operate as
534 	 * normal r/w bits, so any attempt to clear them
535 	 * will just end up setting them. Never do that so
536 	 * we won't mistake them for actual error interrupts.
537 	 */
538 	if (DISPLAY_VER(display) >= 14)
539 		errors |= DSB_ATS_FAULT_INT_STATUS;
540 
541 	return errors;
542 }
543 
544 static u32 dsb_error_int_en(struct intel_display *display)
545 {
546 	u32 errors;
547 
548 	errors = DSB_GTT_FAULT_INT_EN |
549 		DSB_RSPTIMEOUT_INT_EN |
550 		DSB_POLL_ERR_INT_EN;
551 
552 	if (DISPLAY_VER(display) >= 14)
553 		errors |= DSB_ATS_FAULT_INT_EN;
554 
555 	return errors;
556 }
557 
558 void intel_dsb_vblank_evade(struct intel_atomic_state *state,
559 			    struct intel_dsb *dsb)
560 {
561 	struct intel_crtc *crtc = dsb->crtc;
562 	const struct intel_crtc_state *crtc_state =
563 		intel_pre_commit_crtc_state(state, crtc);
564 	/* FIXME calibrate sensibly */
565 	int latency = intel_usecs_to_scanlines(&crtc_state->hw.adjusted_mode, 20);
566 	int start, end;
567 
568 	/*
569 	 * PIPEDSL is reading as 0 when in SRDENT(PSR1) or DEEP_SLEEP(PSR2). On
570 	 * wake-up scanline counting starts from vblank_start - 1. We don't know
571 	 * if wake-up is already ongoing when evasion starts. In worst case
572 	 * PIPEDSL could start reading valid value right after checking the
573 	 * scanline. In this scenario we wouldn't have enough time to write all
574 	 * registers. To tackle this evade scanline 0 as well. As a drawback we
575 	 * have 1 frame delay in flip when waking up.
576 	 */
577 	if (crtc_state->has_psr)
578 		intel_dsb_emit_wait_dsl(dsb, DSB_OPCODE_WAIT_DSL_OUT, 0, 0);
579 
580 	if (pre_commit_is_vrr_active(state, crtc)) {
581 		int vblank_delay = intel_vrr_vblank_delay(crtc_state);
582 
583 		end = intel_vrr_vmin_vblank_start(crtc_state);
584 		start = end - vblank_delay - latency;
585 		intel_dsb_wait_scanline_out(state, dsb, start, end);
586 
587 		end = intel_vrr_vmax_vblank_start(crtc_state);
588 		start = end - vblank_delay - latency;
589 		intel_dsb_wait_scanline_out(state, dsb, start, end);
590 	} else {
591 		int vblank_delay = intel_mode_vblank_delay(&crtc_state->hw.adjusted_mode);
592 
593 		end = intel_mode_vblank_start(&crtc_state->hw.adjusted_mode);
594 		start = end - vblank_delay - latency;
595 		intel_dsb_wait_scanline_out(state, dsb, start, end);
596 	}
597 }
598 
599 static void _intel_dsb_chain(struct intel_atomic_state *state,
600 			     struct intel_dsb *dsb,
601 			     struct intel_dsb *chained_dsb,
602 			     u32 ctrl)
603 {
604 	struct intel_display *display = to_intel_display(state->base.dev);
605 	struct intel_crtc *crtc = dsb->crtc;
606 	enum pipe pipe = crtc->pipe;
607 	u32 tail;
608 
609 	if (drm_WARN_ON(display->drm, dsb->id == chained_dsb->id))
610 		return;
611 
612 	tail = chained_dsb->free_pos * 4;
613 	if (drm_WARN_ON(display->drm, !IS_ALIGNED(tail, CACHELINE_BYTES)))
614 		return;
615 
616 	intel_dsb_reg_write(dsb, DSB_CTRL(pipe, chained_dsb->id),
617 			    ctrl | DSB_ENABLE);
618 
619 	intel_dsb_reg_write(dsb, DSB_CHICKEN(pipe, chained_dsb->id),
620 			    dsb_chicken(state, crtc));
621 
622 	intel_dsb_reg_write(dsb, DSB_INTERRUPT(pipe, chained_dsb->id),
623 			    dsb_error_int_status(display) | DSB_PROG_INT_STATUS |
624 			    dsb_error_int_en(display) | DSB_PROG_INT_EN);
625 
626 	if (ctrl & DSB_WAIT_FOR_VBLANK) {
627 		int dewake_scanline = dsb_dewake_scanline_start(state, crtc);
628 		int hw_dewake_scanline = dsb_scanline_to_hw(state, crtc, dewake_scanline);
629 
630 		intel_dsb_reg_write(dsb, DSB_PMCTRL(pipe, chained_dsb->id),
631 				    DSB_ENABLE_DEWAKE |
632 				    DSB_SCANLINE_FOR_DEWAKE(hw_dewake_scanline));
633 	}
634 
635 	intel_dsb_reg_write(dsb, DSB_HEAD(pipe, chained_dsb->id),
636 			    intel_dsb_buffer_ggtt_offset(&chained_dsb->dsb_buf));
637 
638 	intel_dsb_reg_write(dsb, DSB_TAIL(pipe, chained_dsb->id),
639 			    intel_dsb_buffer_ggtt_offset(&chained_dsb->dsb_buf) + tail);
640 
641 	if (ctrl & DSB_WAIT_FOR_VBLANK) {
642 		/*
643 		 * Keep DEwake alive via the first DSB, in
644 		 * case we're already past dewake_scanline,
645 		 * and thus DSB_ENABLE_DEWAKE on the second
646 		 * DSB won't do its job.
647 		 */
648 		intel_dsb_reg_write_masked(dsb, DSB_PMCTRL_2(pipe, dsb->id),
649 					   DSB_FORCE_DEWAKE, DSB_FORCE_DEWAKE);
650 
651 		intel_dsb_wait_scanline_out(state, dsb,
652 					    dsb_dewake_scanline_start(state, crtc),
653 					    dsb_dewake_scanline_end(state, crtc));
654 	}
655 }
656 
657 void intel_dsb_chain(struct intel_atomic_state *state,
658 		     struct intel_dsb *dsb,
659 		     struct intel_dsb *chained_dsb,
660 		     bool wait_for_vblank)
661 {
662 	_intel_dsb_chain(state, dsb, chained_dsb,
663 			 wait_for_vblank ? DSB_WAIT_FOR_VBLANK : 0);
664 }
665 
666 void intel_dsb_wait_vblank_delay(struct intel_atomic_state *state,
667 				 struct intel_dsb *dsb)
668 {
669 	struct intel_crtc *crtc = dsb->crtc;
670 	const struct intel_crtc_state *crtc_state =
671 		intel_pre_commit_crtc_state(state, crtc);
672 	int usecs = intel_scanlines_to_usecs(&crtc_state->hw.adjusted_mode,
673 					     dsb_vblank_delay(state, crtc));
674 
675 	intel_dsb_wait_usec(dsb, usecs);
676 }
677 
678 static void _intel_dsb_commit(struct intel_dsb *dsb, u32 ctrl,
679 			      int hw_dewake_scanline)
680 {
681 	struct intel_crtc *crtc = dsb->crtc;
682 	struct intel_display *display = to_intel_display(crtc->base.dev);
683 	enum pipe pipe = crtc->pipe;
684 	u32 tail;
685 
686 	tail = dsb->free_pos * 4;
687 	if (drm_WARN_ON(display->drm, !IS_ALIGNED(tail, CACHELINE_BYTES)))
688 		return;
689 
690 	if (is_dsb_busy(display, pipe, dsb->id)) {
691 		drm_err(display->drm, "[CRTC:%d:%s] DSB %d is busy\n",
692 			crtc->base.base.id, crtc->base.name, dsb->id);
693 		return;
694 	}
695 
696 	intel_de_write_fw(display, DSB_CTRL(pipe, dsb->id),
697 			  ctrl | DSB_ENABLE);
698 
699 	intel_de_write_fw(display, DSB_CHICKEN(pipe, dsb->id),
700 			  dsb->chicken);
701 
702 	intel_de_write_fw(display, DSB_INTERRUPT(pipe, dsb->id),
703 			  dsb_error_int_status(display) | DSB_PROG_INT_STATUS |
704 			  dsb_error_int_en(display) | DSB_PROG_INT_EN);
705 
706 	intel_de_write_fw(display, DSB_HEAD(pipe, dsb->id),
707 			  intel_dsb_buffer_ggtt_offset(&dsb->dsb_buf));
708 
709 	if (hw_dewake_scanline >= 0) {
710 		int diff, position;
711 
712 		intel_de_write_fw(display, DSB_PMCTRL(pipe, dsb->id),
713 				  DSB_ENABLE_DEWAKE |
714 				  DSB_SCANLINE_FOR_DEWAKE(hw_dewake_scanline));
715 
716 		/*
717 		 * Force DEwake immediately if we're already past
718 		 * or close to racing past the target scanline.
719 		 */
720 		position = intel_de_read_fw(display, PIPEDSL(display, pipe)) & PIPEDSL_LINE_MASK;
721 
722 		diff = hw_dewake_scanline - position;
723 		intel_de_write_fw(display, DSB_PMCTRL_2(pipe, dsb->id),
724 				  (diff >= 0 && diff < 5 ? DSB_FORCE_DEWAKE : 0) |
725 				  DSB_BLOCK_DEWAKE_EXTENSION);
726 	}
727 
728 	intel_de_write_fw(display, DSB_TAIL(pipe, dsb->id),
729 			  intel_dsb_buffer_ggtt_offset(&dsb->dsb_buf) + tail);
730 }
731 
732 /**
733  * intel_dsb_commit() - Trigger workload execution of DSB.
734  * @dsb: DSB context
735  * @wait_for_vblank: wait for vblank before executing
736  *
737  * This function is used to do actual write to hardware using DSB.
738  */
739 void intel_dsb_commit(struct intel_dsb *dsb,
740 		      bool wait_for_vblank)
741 {
742 	_intel_dsb_commit(dsb,
743 			  wait_for_vblank ? DSB_WAIT_FOR_VBLANK : 0,
744 			  wait_for_vblank ? dsb->hw_dewake_scanline : -1);
745 }
746 
747 void intel_dsb_wait(struct intel_dsb *dsb)
748 {
749 	struct intel_crtc *crtc = dsb->crtc;
750 	struct intel_display *display = to_intel_display(crtc->base.dev);
751 	enum pipe pipe = crtc->pipe;
752 
753 	if (wait_for(!is_dsb_busy(display, pipe, dsb->id), 1)) {
754 		u32 offset = intel_dsb_buffer_ggtt_offset(&dsb->dsb_buf);
755 
756 		intel_de_write_fw(display, DSB_CTRL(pipe, dsb->id),
757 				  DSB_ENABLE | DSB_HALT);
758 
759 		drm_err(display->drm,
760 			"[CRTC:%d:%s] DSB %d timed out waiting for idle (current head=0x%x, head=0x%x, tail=0x%x)\n",
761 			crtc->base.base.id, crtc->base.name, dsb->id,
762 			intel_de_read_fw(display, DSB_CURRENT_HEAD(pipe, dsb->id)) - offset,
763 			intel_de_read_fw(display, DSB_HEAD(pipe, dsb->id)) - offset,
764 			intel_de_read_fw(display, DSB_TAIL(pipe, dsb->id)) - offset);
765 
766 		intel_dsb_dump(dsb);
767 	}
768 
769 	/* Attempt to reset it */
770 	dsb->free_pos = 0;
771 	dsb->ins_start_offset = 0;
772 	dsb->ins[0] = 0;
773 	dsb->ins[1] = 0;
774 
775 	intel_de_write_fw(display, DSB_CTRL(pipe, dsb->id), 0);
776 
777 	intel_de_write_fw(display, DSB_INTERRUPT(pipe, dsb->id),
778 			  dsb_error_int_status(display) | DSB_PROG_INT_STATUS);
779 }
780 
781 /**
782  * intel_dsb_prepare() - Allocate, pin and map the DSB command buffer.
783  * @state: the atomic state
784  * @crtc: the CRTC
785  * @dsb_id: the DSB engine to use
786  * @max_cmds: number of commands we need to fit into command buffer
787  *
788  * This function prepare the command buffer which is used to store dsb
789  * instructions with data.
790  *
791  * Returns:
792  * DSB context, NULL on failure
793  */
794 struct intel_dsb *intel_dsb_prepare(struct intel_atomic_state *state,
795 				    struct intel_crtc *crtc,
796 				    enum intel_dsb_id dsb_id,
797 				    unsigned int max_cmds)
798 {
799 	struct intel_display *display = to_intel_display(state);
800 	struct ref_tracker *wakeref;
801 	struct intel_dsb *dsb;
802 	unsigned int size;
803 
804 	if (!HAS_DSB(display))
805 		return NULL;
806 
807 	if (!display->params.enable_dsb)
808 		return NULL;
809 
810 	dsb = kzalloc(sizeof(*dsb), GFP_KERNEL);
811 	if (!dsb)
812 		goto out;
813 
814 	wakeref = intel_display_rpm_get(display);
815 
816 	/* ~1 qword per instruction, full cachelines */
817 	size = ALIGN(max_cmds * 8, CACHELINE_BYTES);
818 
819 	if (!intel_dsb_buffer_create(crtc, &dsb->dsb_buf, size))
820 		goto out_put_rpm;
821 
822 	intel_display_rpm_put(display, wakeref);
823 
824 	dsb->id = dsb_id;
825 	dsb->crtc = crtc;
826 	dsb->size = size / 4; /* in dwords */
827 
828 	dsb->chicken = dsb_chicken(state, crtc);
829 	dsb->hw_dewake_scanline =
830 		dsb_scanline_to_hw(state, crtc, dsb_dewake_scanline_start(state, crtc));
831 
832 	return dsb;
833 
834 out_put_rpm:
835 	intel_display_rpm_put(display, wakeref);
836 	kfree(dsb);
837 out:
838 	drm_info_once(display->drm,
839 		      "[CRTC:%d:%s] DSB %d queue setup failed, will fallback to MMIO for display HW programming\n",
840 		      crtc->base.base.id, crtc->base.name, dsb_id);
841 
842 	return NULL;
843 }
844 
845 /**
846  * intel_dsb_cleanup() - To cleanup DSB context.
847  * @dsb: DSB context
848  *
849  * This function cleanup the DSB context by unpinning and releasing
850  * the VMA object associated with it.
851  */
852 void intel_dsb_cleanup(struct intel_dsb *dsb)
853 {
854 	intel_dsb_buffer_cleanup(&dsb->dsb_buf);
855 	kfree(dsb);
856 }
857 
858 void intel_dsb_irq_handler(struct intel_display *display,
859 			   enum pipe pipe, enum intel_dsb_id dsb_id)
860 {
861 	struct intel_crtc *crtc = intel_crtc_for_pipe(display, pipe);
862 	u32 tmp, errors;
863 
864 	tmp = intel_de_read_fw(display, DSB_INTERRUPT(pipe, dsb_id));
865 	intel_de_write_fw(display, DSB_INTERRUPT(pipe, dsb_id), tmp);
866 
867 	if (tmp & DSB_PROG_INT_STATUS) {
868 		spin_lock(&display->drm->event_lock);
869 
870 		if (crtc->dsb_event) {
871 			/*
872 			 * Update vblank counter/timestamp in case it
873 			 * hasn't been done yet for this frame.
874 			 */
875 			drm_crtc_accurate_vblank_count(&crtc->base);
876 
877 			drm_crtc_send_vblank_event(&crtc->base, crtc->dsb_event);
878 			crtc->dsb_event = NULL;
879 		}
880 
881 		spin_unlock(&display->drm->event_lock);
882 	}
883 
884 	errors = tmp & dsb_error_int_status(display);
885 	if (errors & DSB_ATS_FAULT_INT_STATUS)
886 		drm_err(display->drm, "[CRTC:%d:%s] DSB %d ATS fault\n",
887 			crtc->base.base.id, crtc->base.name, dsb_id);
888 	if (errors & DSB_GTT_FAULT_INT_STATUS)
889 		drm_err(display->drm, "[CRTC:%d:%s] DSB %d GTT fault\n",
890 			crtc->base.base.id, crtc->base.name, dsb_id);
891 	if (errors & DSB_RSPTIMEOUT_INT_STATUS)
892 		drm_err(display->drm, "[CRTC:%d:%s] DSB %d response timeout\n",
893 			crtc->base.base.id, crtc->base.name, dsb_id);
894 	if (errors & DSB_POLL_ERR_INT_STATUS)
895 		drm_err(display->drm, "[CRTC:%d:%s] DSB %d poll error\n",
896 			crtc->base.base.id, crtc->base.name, dsb_id);
897 }
898