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