1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright © 2019 Intel Corporation
4 *
5 */
6
7 #include <drm/drm_print.h>
8 #include <drm/drm_vblank.h>
9
10 #include "i915_utils.h"
11 #include "intel_crtc.h"
12 #include "intel_de.h"
13 #include "intel_display_regs.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 #define DSB_OPCODE_GOSUB 0xC /* ptl+ */
97 #define DSB_GOSUB_HEAD_SHIFT 26
98 #define DSB_GOSUB_TAIL_SHIFT 0
99 #define DSB_GOSUB_CONVERT_ADDR(x) ((x) >> 6)
100
pre_commit_is_vrr_active(struct intel_atomic_state * state,struct intel_crtc * crtc)101 static bool pre_commit_is_vrr_active(struct intel_atomic_state *state,
102 struct intel_crtc *crtc)
103 {
104 const struct intel_crtc_state *old_crtc_state =
105 intel_atomic_get_old_crtc_state(state, crtc);
106 const struct intel_crtc_state *new_crtc_state =
107 intel_atomic_get_new_crtc_state(state, crtc);
108
109 /* VRR will be enabled afterwards, if necessary */
110 if (intel_crtc_needs_modeset(new_crtc_state))
111 return false;
112
113 /* VRR will have been disabled during intel_pre_plane_update() */
114 return old_crtc_state->vrr.enable && !intel_crtc_vrr_disabling(state, crtc);
115 }
116
dsb_vblank_delay(struct intel_atomic_state * state,struct intel_crtc * crtc)117 static int dsb_vblank_delay(struct intel_atomic_state *state,
118 struct intel_crtc *crtc)
119 {
120 const struct intel_crtc_state *crtc_state =
121 intel_pre_commit_crtc_state(state, crtc);
122
123 if (pre_commit_is_vrr_active(state, crtc))
124 /*
125 * When the push is sent during vblank it will trigger
126 * on the next scanline, hence we have up to one extra
127 * scanline until the delayed vblank occurs after
128 * TRANS_PUSH has been written.
129 */
130 return intel_vrr_vblank_delay(crtc_state) + 1;
131 else
132 return intel_mode_vblank_delay(&crtc_state->hw.adjusted_mode);
133 }
134
dsb_vtotal(struct intel_atomic_state * state,struct intel_crtc * crtc)135 static int dsb_vtotal(struct intel_atomic_state *state,
136 struct intel_crtc *crtc)
137 {
138 const struct intel_crtc_state *crtc_state =
139 intel_pre_commit_crtc_state(state, crtc);
140
141 if (pre_commit_is_vrr_active(state, crtc))
142 return intel_vrr_vmax_vtotal(crtc_state);
143 else
144 return intel_mode_vtotal(&crtc_state->hw.adjusted_mode);
145 }
146
dsb_dewake_scanline_start(struct intel_atomic_state * state,struct intel_crtc * crtc)147 static int dsb_dewake_scanline_start(struct intel_atomic_state *state,
148 struct intel_crtc *crtc)
149 {
150 struct intel_display *display = to_intel_display(state);
151 const struct intel_crtc_state *crtc_state =
152 intel_pre_commit_crtc_state(state, crtc);
153 unsigned int latency = skl_watermark_max_latency(display, 0);
154
155 return intel_mode_vdisplay(&crtc_state->hw.adjusted_mode) -
156 intel_usecs_to_scanlines(&crtc_state->hw.adjusted_mode, latency);
157 }
158
dsb_dewake_scanline_end(struct intel_atomic_state * state,struct intel_crtc * crtc)159 static int dsb_dewake_scanline_end(struct intel_atomic_state *state,
160 struct intel_crtc *crtc)
161 {
162 const struct intel_crtc_state *crtc_state =
163 intel_pre_commit_crtc_state(state, crtc);
164
165 return intel_mode_vdisplay(&crtc_state->hw.adjusted_mode);
166 }
167
dsb_scanline_to_hw(struct intel_atomic_state * state,struct intel_crtc * crtc,int scanline)168 static int dsb_scanline_to_hw(struct intel_atomic_state *state,
169 struct intel_crtc *crtc, int scanline)
170 {
171 const struct intel_crtc_state *crtc_state =
172 intel_pre_commit_crtc_state(state, crtc);
173 int vtotal = dsb_vtotal(state, crtc);
174
175 return (scanline + vtotal - intel_crtc_scanline_offset(crtc_state)) % vtotal;
176 }
177
178 /*
179 * Bspec suggests that we should always set DSB_SKIP_WAITS_EN. We have approach
180 * different from what is explained in Bspec on how flip is considered being
181 * complete. We are waiting for vblank in DSB and generate interrupt when it
182 * happens and this interrupt is considered as indication of completion -> we
183 * definitely do not want to skip vblank wait. We also have concern what comes
184 * to skipping vblank evasion. I.e. arming registers are latched before we have
185 * managed writing them. Due to these reasons we are not setting
186 * DSB_SKIP_WAITS_EN.
187 */
dsb_chicken(struct intel_atomic_state * state,struct intel_crtc * crtc)188 static u32 dsb_chicken(struct intel_atomic_state *state,
189 struct intel_crtc *crtc)
190 {
191 if (pre_commit_is_vrr_active(state, crtc))
192 return DSB_CTRL_WAIT_SAFE_WINDOW |
193 DSB_CTRL_NO_WAIT_VBLANK |
194 DSB_INST_WAIT_SAFE_WINDOW |
195 DSB_INST_NO_WAIT_VBLANK;
196 else
197 return 0;
198 }
199
assert_dsb_has_room(struct intel_dsb * dsb)200 static bool assert_dsb_has_room(struct intel_dsb *dsb)
201 {
202 struct intel_crtc *crtc = dsb->crtc;
203 struct intel_display *display = to_intel_display(crtc->base.dev);
204
205 /* each instruction is 2 dwords */
206 return !drm_WARN(display->drm, dsb->free_pos > dsb->size - 2,
207 "[CRTC:%d:%s] DSB %d buffer overflow\n",
208 crtc->base.base.id, crtc->base.name, dsb->id);
209 }
210
assert_dsb_tail_is_aligned(struct intel_dsb * dsb)211 static bool assert_dsb_tail_is_aligned(struct intel_dsb *dsb)
212 {
213 struct intel_crtc *crtc = dsb->crtc;
214 struct intel_display *display = to_intel_display(crtc->base.dev);
215
216 return !drm_WARN_ON(display->drm,
217 !IS_ALIGNED(dsb->free_pos * 4, CACHELINE_BYTES));
218 }
219
intel_dsb_dump(struct intel_dsb * dsb)220 static void intel_dsb_dump(struct intel_dsb *dsb)
221 {
222 struct intel_crtc *crtc = dsb->crtc;
223 struct intel_display *display = to_intel_display(crtc->base.dev);
224 int i;
225
226 drm_dbg_kms(display->drm, "[CRTC:%d:%s] DSB %d commands {\n",
227 crtc->base.base.id, crtc->base.name, dsb->id);
228 for (i = 0; i < ALIGN(dsb->free_pos, 64 / 4); i += 4)
229 drm_dbg_kms(display->drm,
230 " 0x%08x: 0x%08x 0x%08x 0x%08x 0x%08x\n", i * 4,
231 intel_dsb_buffer_read(&dsb->dsb_buf, i),
232 intel_dsb_buffer_read(&dsb->dsb_buf, i + 1),
233 intel_dsb_buffer_read(&dsb->dsb_buf, i + 2),
234 intel_dsb_buffer_read(&dsb->dsb_buf, i + 3));
235 drm_dbg_kms(display->drm, "}\n");
236 }
237
is_dsb_busy(struct intel_display * display,enum pipe pipe,enum intel_dsb_id dsb_id)238 static bool is_dsb_busy(struct intel_display *display, enum pipe pipe,
239 enum intel_dsb_id dsb_id)
240 {
241 return intel_de_read_fw(display, DSB_CTRL(pipe, dsb_id)) & DSB_STATUS_BUSY;
242 }
243
intel_dsb_size(struct intel_dsb * dsb)244 unsigned int intel_dsb_size(struct intel_dsb *dsb)
245 {
246 return dsb->free_pos * 4;
247 }
248
intel_dsb_head(struct intel_dsb * dsb)249 unsigned int intel_dsb_head(struct intel_dsb *dsb)
250 {
251 return intel_dsb_buffer_ggtt_offset(&dsb->dsb_buf);
252 }
253
intel_dsb_tail(struct intel_dsb * dsb)254 static unsigned int intel_dsb_tail(struct intel_dsb *dsb)
255 {
256 return intel_dsb_buffer_ggtt_offset(&dsb->dsb_buf) + intel_dsb_size(dsb);
257 }
258
intel_dsb_ins_align(struct intel_dsb * dsb)259 static void intel_dsb_ins_align(struct intel_dsb *dsb)
260 {
261 /*
262 * Every instruction should be 8 byte aligned.
263 *
264 * The only way to get unaligned free_pos is via
265 * intel_dsb_reg_write_indexed() which already
266 * makes sure the next dword is zeroed, so no need
267 * to clear it here.
268 */
269 dsb->free_pos = ALIGN(dsb->free_pos, 2);
270 }
271
intel_dsb_emit(struct intel_dsb * dsb,u32 ldw,u32 udw)272 static void intel_dsb_emit(struct intel_dsb *dsb, u32 ldw, u32 udw)
273 {
274 if (!assert_dsb_has_room(dsb))
275 return;
276
277 intel_dsb_ins_align(dsb);
278
279 dsb->ins_start_offset = dsb->free_pos;
280 dsb->ins[0] = ldw;
281 dsb->ins[1] = udw;
282
283 intel_dsb_buffer_write(&dsb->dsb_buf, dsb->free_pos++, dsb->ins[0]);
284 intel_dsb_buffer_write(&dsb->dsb_buf, dsb->free_pos++, dsb->ins[1]);
285 }
286
intel_dsb_prev_ins_is_write(struct intel_dsb * dsb,u32 opcode,i915_reg_t reg)287 static bool intel_dsb_prev_ins_is_write(struct intel_dsb *dsb,
288 u32 opcode, i915_reg_t reg)
289 {
290 u32 prev_opcode, prev_reg;
291
292 /*
293 * Nothing emitted yet? Must check before looking
294 * at the actual data since i915_gem_object_create_internal()
295 * does *not* give you zeroed memory!
296 */
297 if (dsb->free_pos == 0)
298 return false;
299
300 prev_opcode = dsb->ins[1] & ~DSB_REG_VALUE_MASK;
301 prev_reg = dsb->ins[1] & DSB_REG_VALUE_MASK;
302
303 return prev_opcode == opcode && prev_reg == i915_mmio_reg_offset(reg);
304 }
305
intel_dsb_prev_ins_is_indexed_write(struct intel_dsb * dsb,i915_reg_t reg)306 static bool intel_dsb_prev_ins_is_indexed_write(struct intel_dsb *dsb, i915_reg_t reg)
307 {
308 return intel_dsb_prev_ins_is_write(dsb,
309 DSB_OPCODE_INDEXED_WRITE << DSB_OPCODE_SHIFT,
310 reg);
311 }
312
313 /**
314 * intel_dsb_reg_write_indexed() - Emit indexed register write to the DSB context
315 * @dsb: DSB context
316 * @reg: register address.
317 * @val: value.
318 *
319 * This function is used for writing register-value pair in command
320 * buffer of DSB.
321 *
322 * Note that indexed writes are slower than normal MMIO writes
323 * for a small number (less than 5 or so) of writes to the same
324 * register.
325 */
intel_dsb_reg_write_indexed(struct intel_dsb * dsb,i915_reg_t reg,u32 val)326 void intel_dsb_reg_write_indexed(struct intel_dsb *dsb,
327 i915_reg_t reg, u32 val)
328 {
329 /*
330 * For example the buffer will look like below for 3 dwords for auto
331 * increment register:
332 * +--------------------------------------------------------+
333 * | size = 3 | offset &| value1 | value2 | value3 | zero |
334 * | | opcode | | | | |
335 * +--------------------------------------------------------+
336 * + + + + + + +
337 * 0 4 8 12 16 20 24
338 * Byte
339 *
340 * As every instruction is 8 byte aligned the index of dsb instruction
341 * will start always from even number while dealing with u32 array. If
342 * we are writing odd no of dwords, Zeros will be added in the end for
343 * padding.
344 */
345 if (!intel_dsb_prev_ins_is_indexed_write(dsb, reg))
346 intel_dsb_emit(dsb, 0, /* count */
347 (DSB_OPCODE_INDEXED_WRITE << DSB_OPCODE_SHIFT) |
348 i915_mmio_reg_offset(reg));
349
350 if (!assert_dsb_has_room(dsb))
351 return;
352
353 /* Update the count */
354 dsb->ins[0]++;
355 intel_dsb_buffer_write(&dsb->dsb_buf, dsb->ins_start_offset + 0,
356 dsb->ins[0]);
357
358 intel_dsb_buffer_write(&dsb->dsb_buf, dsb->free_pos++, val);
359 /* if number of data words is odd, then the last dword should be 0.*/
360 if (dsb->free_pos & 0x1)
361 intel_dsb_buffer_write(&dsb->dsb_buf, dsb->free_pos, 0);
362 }
363
intel_dsb_reg_write(struct intel_dsb * dsb,i915_reg_t reg,u32 val)364 void intel_dsb_reg_write(struct intel_dsb *dsb,
365 i915_reg_t reg, u32 val)
366 {
367 intel_dsb_emit(dsb, val,
368 (DSB_OPCODE_MMIO_WRITE << DSB_OPCODE_SHIFT) |
369 (DSB_BYTE_EN << DSB_BYTE_EN_SHIFT) |
370 i915_mmio_reg_offset(reg));
371 }
372
intel_dsb_mask_to_byte_en(u32 mask)373 static u32 intel_dsb_mask_to_byte_en(u32 mask)
374 {
375 return (!!(mask & 0xff000000) << 3 |
376 !!(mask & 0x00ff0000) << 2 |
377 !!(mask & 0x0000ff00) << 1 |
378 !!(mask & 0x000000ff) << 0);
379 }
380
381 /* Note: mask implemented via byte enables! */
intel_dsb_reg_write_masked(struct intel_dsb * dsb,i915_reg_t reg,u32 mask,u32 val)382 void intel_dsb_reg_write_masked(struct intel_dsb *dsb,
383 i915_reg_t reg, u32 mask, u32 val)
384 {
385 intel_dsb_emit(dsb, val,
386 (DSB_OPCODE_MMIO_WRITE << DSB_OPCODE_SHIFT) |
387 (intel_dsb_mask_to_byte_en(mask) << DSB_BYTE_EN_SHIFT) |
388 i915_mmio_reg_offset(reg));
389 }
390
intel_dsb_noop(struct intel_dsb * dsb,int count)391 void intel_dsb_noop(struct intel_dsb *dsb, int count)
392 {
393 int i;
394
395 for (i = 0; i < count; i++)
396 intel_dsb_emit(dsb, 0,
397 DSB_OPCODE_NOOP << DSB_OPCODE_SHIFT);
398 }
399
intel_dsb_nonpost_start(struct intel_dsb * dsb)400 void intel_dsb_nonpost_start(struct intel_dsb *dsb)
401 {
402 struct intel_crtc *crtc = dsb->crtc;
403 enum pipe pipe = crtc->pipe;
404
405 intel_dsb_reg_write_masked(dsb, DSB_CTRL(pipe, dsb->id),
406 DSB_NON_POSTED, DSB_NON_POSTED);
407 intel_dsb_noop(dsb, 4);
408 }
409
intel_dsb_nonpost_end(struct intel_dsb * dsb)410 void intel_dsb_nonpost_end(struct intel_dsb *dsb)
411 {
412 struct intel_crtc *crtc = dsb->crtc;
413 enum pipe pipe = crtc->pipe;
414
415 intel_dsb_reg_write_masked(dsb, DSB_CTRL(pipe, dsb->id),
416 DSB_NON_POSTED, 0);
417 intel_dsb_noop(dsb, 4);
418 }
419
intel_dsb_interrupt(struct intel_dsb * dsb)420 void intel_dsb_interrupt(struct intel_dsb *dsb)
421 {
422 intel_dsb_emit(dsb, 0,
423 DSB_OPCODE_INTERRUPT << DSB_OPCODE_SHIFT);
424 }
425
intel_dsb_wait_usec(struct intel_dsb * dsb,int count)426 void intel_dsb_wait_usec(struct intel_dsb *dsb, int count)
427 {
428 /* +1 to make sure we never wait less time than asked for */
429 intel_dsb_emit(dsb, count + 1,
430 DSB_OPCODE_WAIT_USEC << DSB_OPCODE_SHIFT);
431 }
432
intel_dsb_wait_vblanks(struct intel_dsb * dsb,int count)433 void intel_dsb_wait_vblanks(struct intel_dsb *dsb, int count)
434 {
435 intel_dsb_emit(dsb, count,
436 DSB_OPCODE_WAIT_VBLANKS << DSB_OPCODE_SHIFT);
437 }
438
intel_dsb_emit_wait_dsl(struct intel_dsb * dsb,u32 opcode,int lower,int upper)439 static void intel_dsb_emit_wait_dsl(struct intel_dsb *dsb,
440 u32 opcode, int lower, int upper)
441 {
442 u64 window = ((u64)upper << DSB_SCANLINE_UPPER_SHIFT) |
443 ((u64)lower << DSB_SCANLINE_LOWER_SHIFT);
444
445 intel_dsb_emit(dsb, lower_32_bits(window),
446 (opcode << DSB_OPCODE_SHIFT) |
447 upper_32_bits(window));
448 }
449
intel_dsb_wait_dsl(struct intel_atomic_state * state,struct intel_dsb * dsb,int lower_in,int upper_in,int lower_out,int upper_out)450 static void intel_dsb_wait_dsl(struct intel_atomic_state *state,
451 struct intel_dsb *dsb,
452 int lower_in, int upper_in,
453 int lower_out, int upper_out)
454 {
455 struct intel_crtc *crtc = dsb->crtc;
456
457 lower_in = dsb_scanline_to_hw(state, crtc, lower_in);
458 upper_in = dsb_scanline_to_hw(state, crtc, upper_in);
459
460 lower_out = dsb_scanline_to_hw(state, crtc, lower_out);
461 upper_out = dsb_scanline_to_hw(state, crtc, upper_out);
462
463 if (upper_in >= lower_in)
464 intel_dsb_emit_wait_dsl(dsb, DSB_OPCODE_WAIT_DSL_IN,
465 lower_in, upper_in);
466 else if (upper_out >= lower_out)
467 intel_dsb_emit_wait_dsl(dsb, DSB_OPCODE_WAIT_DSL_OUT,
468 lower_out, upper_out);
469 else
470 drm_WARN_ON(crtc->base.dev, 1); /* assert_dsl_ok() should have caught it already */
471 }
472
assert_dsl_ok(struct intel_atomic_state * state,struct intel_dsb * dsb,int start,int end)473 static void assert_dsl_ok(struct intel_atomic_state *state,
474 struct intel_dsb *dsb,
475 int start, int end)
476 {
477 struct intel_crtc *crtc = dsb->crtc;
478 int vtotal = dsb_vtotal(state, crtc);
479
480 /*
481 * Waiting for the entire frame doesn't make sense,
482 * (IN==don't wait, OUT=wait forever).
483 */
484 drm_WARN(crtc->base.dev, (end - start + vtotal) % vtotal == vtotal - 1,
485 "[CRTC:%d:%s] DSB %d bad scanline window wait: %d-%d (vt=%d)\n",
486 crtc->base.base.id, crtc->base.name, dsb->id,
487 start, end, vtotal);
488 }
489
intel_dsb_wait_scanline_in(struct intel_atomic_state * state,struct intel_dsb * dsb,int start,int end)490 void intel_dsb_wait_scanline_in(struct intel_atomic_state *state,
491 struct intel_dsb *dsb,
492 int start, int end)
493 {
494 assert_dsl_ok(state, dsb, start, end);
495
496 intel_dsb_wait_dsl(state, dsb,
497 start, end,
498 end + 1, start - 1);
499 }
500
intel_dsb_wait_scanline_out(struct intel_atomic_state * state,struct intel_dsb * dsb,int start,int end)501 void intel_dsb_wait_scanline_out(struct intel_atomic_state *state,
502 struct intel_dsb *dsb,
503 int start, int end)
504 {
505 assert_dsl_ok(state, dsb, start, end);
506
507 intel_dsb_wait_dsl(state, dsb,
508 end + 1, start - 1,
509 start, end);
510 }
511
intel_dsb_poll(struct intel_dsb * dsb,i915_reg_t reg,u32 mask,u32 val,int wait_us,int count)512 void intel_dsb_poll(struct intel_dsb *dsb,
513 i915_reg_t reg, u32 mask, u32 val,
514 int wait_us, int count)
515 {
516 struct intel_crtc *crtc = dsb->crtc;
517 enum pipe pipe = crtc->pipe;
518
519 intel_dsb_reg_write(dsb, DSB_POLLMASK(pipe, dsb->id), mask);
520 intel_dsb_reg_write(dsb, DSB_POLLFUNC(pipe, dsb->id),
521 DSB_POLL_ENABLE |
522 DSB_POLL_WAIT(wait_us) | DSB_POLL_COUNT(count));
523
524 intel_dsb_noop(dsb, 5);
525
526 intel_dsb_emit(dsb, val,
527 (DSB_OPCODE_POLL << DSB_OPCODE_SHIFT) |
528 i915_mmio_reg_offset(reg));
529 }
530
intel_dsb_align_tail(struct intel_dsb * dsb)531 static void intel_dsb_align_tail(struct intel_dsb *dsb)
532 {
533 u32 aligned_tail, tail;
534
535 intel_dsb_ins_align(dsb);
536
537 tail = dsb->free_pos * 4;
538 aligned_tail = ALIGN(tail, CACHELINE_BYTES);
539
540 if (aligned_tail > tail)
541 intel_dsb_buffer_memset(&dsb->dsb_buf, dsb->free_pos, 0,
542 aligned_tail - tail);
543
544 dsb->free_pos = aligned_tail / 4;
545 }
546
intel_dsb_gosub_align(struct intel_dsb * dsb)547 static void intel_dsb_gosub_align(struct intel_dsb *dsb)
548 {
549 u32 aligned_tail, tail;
550
551 intel_dsb_ins_align(dsb);
552
553 tail = dsb->free_pos * 4;
554 aligned_tail = ALIGN(tail, CACHELINE_BYTES);
555
556 /*
557 * Wa_16024917128
558 * "Ensure GOSUB is not placed in cacheline QW slot 6 or 7 (numbered 0-7)"
559 */
560 if (aligned_tail - tail <= 2 * 8)
561 intel_dsb_buffer_memset(&dsb->dsb_buf, dsb->free_pos, 0,
562 aligned_tail - tail);
563
564 dsb->free_pos = aligned_tail / 4;
565 }
566
intel_dsb_gosub(struct intel_dsb * dsb,struct intel_dsb * sub_dsb)567 void intel_dsb_gosub(struct intel_dsb *dsb,
568 struct intel_dsb *sub_dsb)
569 {
570 struct intel_crtc *crtc = dsb->crtc;
571 struct intel_display *display = to_intel_display(crtc->base.dev);
572 unsigned int head, tail;
573 u64 head_tail;
574
575 if (drm_WARN_ON(display->drm, dsb->id != sub_dsb->id))
576 return;
577
578 if (!assert_dsb_tail_is_aligned(sub_dsb))
579 return;
580
581 intel_dsb_gosub_align(dsb);
582
583 head = intel_dsb_head(sub_dsb);
584 tail = intel_dsb_tail(sub_dsb);
585
586 /*
587 * The GOSUB instruction has the following memory layout.
588 *
589 * +------------------------------------------------------------+
590 * | Opcode | Rsvd | Head Ptr | Tail Ptr |
591 * | 0x0c | | | |
592 * +------------------------------------------------------------+
593 * |<- 8bits->|<- 4bits ->|<-- 26bits -->|<-- 26bits -->|
594 *
595 * We have only 26 bits each to represent the head and tail
596 * pointers even though the addresses itself are of 32 bit. However, this
597 * is not a problem because the addresses are 64 bit aligned and therefore
598 * the last 6 bits are always Zero's. Therefore, we right shift the address
599 * by 6 before embedding it into the GOSUB instruction.
600 */
601
602 head_tail = ((u64)(DSB_GOSUB_CONVERT_ADDR(head)) << DSB_GOSUB_HEAD_SHIFT) |
603 ((u64)(DSB_GOSUB_CONVERT_ADDR(tail)) << DSB_GOSUB_TAIL_SHIFT);
604
605 intel_dsb_emit(dsb, lower_32_bits(head_tail),
606 (DSB_OPCODE_GOSUB << DSB_OPCODE_SHIFT) |
607 upper_32_bits(head_tail));
608
609 /*
610 * "NOTE: the instructions within the cacheline
611 * FOLLOWING the GOSUB instruction must be NOPs."
612 */
613 intel_dsb_align_tail(dsb);
614 }
615
intel_dsb_gosub_finish(struct intel_dsb * dsb)616 void intel_dsb_gosub_finish(struct intel_dsb *dsb)
617 {
618 intel_dsb_align_tail(dsb);
619
620 /*
621 * Wa_16024917128
622 * "Ensure that all subroutines called by GOSUB end with a cacheline of NOPs"
623 */
624 intel_dsb_noop(dsb, 8);
625
626 intel_dsb_buffer_flush_map(&dsb->dsb_buf);
627 }
628
intel_dsb_finish(struct intel_dsb * dsb)629 void intel_dsb_finish(struct intel_dsb *dsb)
630 {
631 intel_dsb_align_tail(dsb);
632
633 intel_dsb_buffer_flush_map(&dsb->dsb_buf);
634 }
635
dsb_error_int_status(struct intel_display * display)636 static u32 dsb_error_int_status(struct intel_display *display)
637 {
638 u32 errors;
639
640 errors = DSB_GTT_FAULT_INT_STATUS |
641 DSB_RSPTIMEOUT_INT_STATUS |
642 DSB_POLL_ERR_INT_STATUS;
643
644 /*
645 * All the non-existing status bits operate as
646 * normal r/w bits, so any attempt to clear them
647 * will just end up setting them. Never do that so
648 * we won't mistake them for actual error interrupts.
649 */
650 if (DISPLAY_VER(display) >= 14)
651 errors |= DSB_ATS_FAULT_INT_STATUS;
652
653 if (DISPLAY_VER(display) >= 30)
654 errors |= DSB_GOSUB_INT_STATUS;
655
656 return errors;
657 }
658
dsb_error_int_en(struct intel_display * display)659 static u32 dsb_error_int_en(struct intel_display *display)
660 {
661 u32 errors;
662
663 errors = DSB_GTT_FAULT_INT_EN |
664 DSB_RSPTIMEOUT_INT_EN |
665 DSB_POLL_ERR_INT_EN;
666
667 if (DISPLAY_VER(display) >= 14)
668 errors |= DSB_ATS_FAULT_INT_EN;
669
670 /*
671 * Wa_16024917128
672 * "Disable nested GOSUB interrupt (DSB_INTERRUPT bit 21)"
673 */
674 if (0 && DISPLAY_VER(display) >= 30)
675 errors |= DSB_GOSUB_INT_EN;
676
677 return errors;
678 }
679
680 /*
681 * FIXME calibrate these sensibly, ideally compute based on
682 * the number of regisetrs to be written. But that requires
683 * measuring the actual DSB execution speed on each platform
684 * (and the speed also depends on CDCLK and memory clock)...
685 */
intel_dsb_noarm_exec_time_us(void)686 static int intel_dsb_noarm_exec_time_us(void)
687 {
688 return 80;
689 }
690
intel_dsb_arm_exec_time_us(void)691 static int intel_dsb_arm_exec_time_us(void)
692 {
693 return 20;
694 }
695
intel_dsb_exec_time_us(void)696 int intel_dsb_exec_time_us(void)
697 {
698 return intel_dsb_noarm_exec_time_us() +
699 intel_dsb_arm_exec_time_us();
700 }
701
intel_dsb_vblank_evade(struct intel_atomic_state * state,struct intel_dsb * dsb)702 void intel_dsb_vblank_evade(struct intel_atomic_state *state,
703 struct intel_dsb *dsb)
704 {
705 struct intel_crtc *crtc = dsb->crtc;
706 const struct intel_crtc_state *crtc_state =
707 intel_pre_commit_crtc_state(state, crtc);
708 int latency = intel_usecs_to_scanlines(&crtc_state->hw.adjusted_mode,
709 intel_dsb_arm_exec_time_us());
710 int start, end;
711
712 /*
713 * PIPEDSL is reading as 0 when in SRDENT(PSR1) or DEEP_SLEEP(PSR2). On
714 * wake-up scanline counting starts from vblank_start - 1. We don't know
715 * if wake-up is already ongoing when evasion starts. In worst case
716 * PIPEDSL could start reading valid value right after checking the
717 * scanline. In this scenario we wouldn't have enough time to write all
718 * registers. To tackle this evade scanline 0 as well. As a drawback we
719 * have 1 frame delay in flip when waking up.
720 */
721 if (crtc_state->has_psr)
722 intel_dsb_emit_wait_dsl(dsb, DSB_OPCODE_WAIT_DSL_OUT, 0, 0);
723
724 if (pre_commit_is_vrr_active(state, crtc)) {
725 int vblank_delay = intel_vrr_vblank_delay(crtc_state);
726
727 end = intel_vrr_vmin_vblank_start(crtc_state);
728 start = end - vblank_delay - latency;
729 intel_dsb_wait_scanline_out(state, dsb, start, end);
730
731 end = intel_vrr_vmax_vblank_start(crtc_state);
732 start = end - vblank_delay - latency;
733 intel_dsb_wait_scanline_out(state, dsb, start, end);
734 } else {
735 int vblank_delay = intel_mode_vblank_delay(&crtc_state->hw.adjusted_mode);
736
737 end = intel_mode_vblank_start(&crtc_state->hw.adjusted_mode);
738 start = end - vblank_delay - latency;
739 intel_dsb_wait_scanline_out(state, dsb, start, end);
740 }
741 }
742
_intel_dsb_chain(struct intel_atomic_state * state,struct intel_dsb * dsb,struct intel_dsb * chained_dsb,u32 ctrl)743 static void _intel_dsb_chain(struct intel_atomic_state *state,
744 struct intel_dsb *dsb,
745 struct intel_dsb *chained_dsb,
746 u32 ctrl)
747 {
748 struct intel_display *display = to_intel_display(state->base.dev);
749 struct intel_crtc *crtc = dsb->crtc;
750 enum pipe pipe = crtc->pipe;
751
752 if (drm_WARN_ON(display->drm, dsb->id == chained_dsb->id))
753 return;
754
755 if (!assert_dsb_tail_is_aligned(chained_dsb))
756 return;
757
758 intel_dsb_reg_write(dsb, DSB_CTRL(pipe, chained_dsb->id),
759 ctrl | DSB_ENABLE);
760
761 intel_dsb_reg_write(dsb, DSB_CHICKEN(pipe, chained_dsb->id),
762 dsb_chicken(state, crtc));
763
764 intel_dsb_reg_write(dsb, DSB_INTERRUPT(pipe, chained_dsb->id),
765 dsb_error_int_status(display) | DSB_PROG_INT_STATUS |
766 dsb_error_int_en(display) | DSB_PROG_INT_EN);
767
768 if (ctrl & DSB_WAIT_FOR_VBLANK) {
769 int dewake_scanline = dsb_dewake_scanline_start(state, crtc);
770 int hw_dewake_scanline = dsb_scanline_to_hw(state, crtc, dewake_scanline);
771
772 intel_dsb_reg_write(dsb, DSB_PMCTRL(pipe, chained_dsb->id),
773 DSB_ENABLE_DEWAKE |
774 DSB_SCANLINE_FOR_DEWAKE(hw_dewake_scanline));
775 } else {
776 intel_dsb_reg_write(dsb, DSB_PMCTRL(pipe, chained_dsb->id), 0);
777 }
778
779 intel_dsb_reg_write(dsb, DSB_HEAD(pipe, chained_dsb->id),
780 intel_dsb_head(chained_dsb));
781
782 intel_dsb_reg_write(dsb, DSB_TAIL(pipe, chained_dsb->id),
783 intel_dsb_tail(chained_dsb));
784
785 if (ctrl & DSB_WAIT_FOR_VBLANK) {
786 /*
787 * Keep DEwake alive via the first DSB, in
788 * case we're already past dewake_scanline,
789 * and thus DSB_ENABLE_DEWAKE on the second
790 * DSB won't do its job.
791 */
792 intel_dsb_reg_write_masked(dsb, DSB_PMCTRL_2(pipe, dsb->id),
793 DSB_FORCE_DEWAKE, DSB_FORCE_DEWAKE);
794
795 intel_dsb_wait_scanline_out(state, dsb,
796 dsb_dewake_scanline_start(state, crtc),
797 dsb_dewake_scanline_end(state, crtc));
798
799 /*
800 * DSB_FORCE_DEWAKE remains active even after DSB is
801 * disabled, so make sure to clear it.
802 */
803 intel_dsb_reg_write_masked(dsb, DSB_PMCTRL_2(crtc->pipe, dsb->id),
804 DSB_FORCE_DEWAKE, 0);
805 }
806 }
807
intel_dsb_chain(struct intel_atomic_state * state,struct intel_dsb * dsb,struct intel_dsb * chained_dsb,bool wait_for_vblank)808 void intel_dsb_chain(struct intel_atomic_state *state,
809 struct intel_dsb *dsb,
810 struct intel_dsb *chained_dsb,
811 bool wait_for_vblank)
812 {
813 _intel_dsb_chain(state, dsb, chained_dsb,
814 wait_for_vblank ? DSB_WAIT_FOR_VBLANK : 0);
815 }
816
intel_dsb_wait_vblank_delay(struct intel_atomic_state * state,struct intel_dsb * dsb)817 void intel_dsb_wait_vblank_delay(struct intel_atomic_state *state,
818 struct intel_dsb *dsb)
819 {
820 struct intel_crtc *crtc = dsb->crtc;
821 const struct intel_crtc_state *crtc_state =
822 intel_pre_commit_crtc_state(state, crtc);
823 int usecs = intel_scanlines_to_usecs(&crtc_state->hw.adjusted_mode,
824 dsb_vblank_delay(state, crtc));
825
826 intel_dsb_wait_usec(dsb, usecs);
827 }
828
829 /**
830 * intel_dsb_commit() - Trigger workload execution of DSB.
831 * @dsb: DSB context
832 *
833 * This function is used to do actual write to hardware using DSB.
834 */
intel_dsb_commit(struct intel_dsb * dsb)835 void intel_dsb_commit(struct intel_dsb *dsb)
836 {
837 struct intel_crtc *crtc = dsb->crtc;
838 struct intel_display *display = to_intel_display(crtc->base.dev);
839 enum pipe pipe = crtc->pipe;
840
841 if (!assert_dsb_tail_is_aligned(dsb))
842 return;
843
844 if (is_dsb_busy(display, pipe, dsb->id)) {
845 drm_err(display->drm, "[CRTC:%d:%s] DSB %d is busy\n",
846 crtc->base.base.id, crtc->base.name, dsb->id);
847 return;
848 }
849
850 intel_de_write_fw(display, DSB_CTRL(pipe, dsb->id),
851 DSB_ENABLE);
852
853 intel_de_write_fw(display, DSB_CHICKEN(pipe, dsb->id),
854 dsb->chicken);
855
856 intel_de_write_fw(display, DSB_INTERRUPT(pipe, dsb->id),
857 dsb_error_int_status(display) | DSB_PROG_INT_STATUS |
858 dsb_error_int_en(display) | DSB_PROG_INT_EN);
859
860 intel_de_write_fw(display, DSB_PMCTRL(pipe, dsb->id), 0);
861
862 intel_de_write_fw(display, DSB_HEAD(pipe, dsb->id),
863 intel_dsb_head(dsb));
864
865 intel_de_write_fw(display, DSB_TAIL(pipe, dsb->id),
866 intel_dsb_tail(dsb));
867 }
868
intel_dsb_wait(struct intel_dsb * dsb)869 void intel_dsb_wait(struct intel_dsb *dsb)
870 {
871 struct intel_crtc *crtc = dsb->crtc;
872 struct intel_display *display = to_intel_display(crtc->base.dev);
873 enum pipe pipe = crtc->pipe;
874
875 if (wait_for(!is_dsb_busy(display, pipe, dsb->id), 1)) {
876 u32 offset = intel_dsb_buffer_ggtt_offset(&dsb->dsb_buf);
877
878 intel_de_write_fw(display, DSB_CTRL(pipe, dsb->id),
879 DSB_ENABLE | DSB_HALT);
880
881 drm_err(display->drm,
882 "[CRTC:%d:%s] DSB %d timed out waiting for idle (current head=0x%x, head=0x%x, tail=0x%x)\n",
883 crtc->base.base.id, crtc->base.name, dsb->id,
884 intel_de_read_fw(display, DSB_CURRENT_HEAD(pipe, dsb->id)) - offset,
885 intel_de_read_fw(display, DSB_HEAD(pipe, dsb->id)) - offset,
886 intel_de_read_fw(display, DSB_TAIL(pipe, dsb->id)) - offset);
887
888 intel_dsb_dump(dsb);
889 }
890
891 /* Attempt to reset it */
892 dsb->free_pos = 0;
893 dsb->ins_start_offset = 0;
894 dsb->ins[0] = 0;
895 dsb->ins[1] = 0;
896
897 intel_de_write_fw(display, DSB_CTRL(pipe, dsb->id), 0);
898
899 intel_de_write_fw(display, DSB_INTERRUPT(pipe, dsb->id),
900 dsb_error_int_status(display) | DSB_PROG_INT_STATUS);
901 }
902
903 /**
904 * intel_dsb_prepare() - Allocate, pin and map the DSB command buffer.
905 * @state: the atomic state
906 * @crtc: the CRTC
907 * @dsb_id: the DSB engine to use
908 * @max_cmds: number of commands we need to fit into command buffer
909 *
910 * This function prepare the command buffer which is used to store dsb
911 * instructions with data.
912 *
913 * Returns:
914 * DSB context, NULL on failure
915 */
intel_dsb_prepare(struct intel_atomic_state * state,struct intel_crtc * crtc,enum intel_dsb_id dsb_id,unsigned int max_cmds)916 struct intel_dsb *intel_dsb_prepare(struct intel_atomic_state *state,
917 struct intel_crtc *crtc,
918 enum intel_dsb_id dsb_id,
919 unsigned int max_cmds)
920 {
921 struct intel_display *display = to_intel_display(state);
922 struct ref_tracker *wakeref;
923 struct intel_dsb *dsb;
924 unsigned int size;
925
926 if (!HAS_DSB(display))
927 return NULL;
928
929 if (!display->params.enable_dsb)
930 return NULL;
931
932 dsb = kzalloc(sizeof(*dsb), GFP_KERNEL);
933 if (!dsb)
934 goto out;
935
936 wakeref = intel_display_rpm_get(display);
937
938 /* ~1 qword per instruction, full cachelines */
939 size = ALIGN(max_cmds * 8, CACHELINE_BYTES);
940
941 if (!intel_dsb_buffer_create(crtc, &dsb->dsb_buf, size))
942 goto out_put_rpm;
943
944 intel_display_rpm_put(display, wakeref);
945
946 dsb->id = dsb_id;
947 dsb->crtc = crtc;
948 dsb->size = size / 4; /* in dwords */
949
950 dsb->chicken = dsb_chicken(state, crtc);
951 dsb->hw_dewake_scanline =
952 dsb_scanline_to_hw(state, crtc, dsb_dewake_scanline_start(state, crtc));
953
954 return dsb;
955
956 out_put_rpm:
957 intel_display_rpm_put(display, wakeref);
958 kfree(dsb);
959 out:
960 drm_info_once(display->drm,
961 "[CRTC:%d:%s] DSB %d queue setup failed, will fallback to MMIO for display HW programming\n",
962 crtc->base.base.id, crtc->base.name, dsb_id);
963
964 return NULL;
965 }
966
967 /**
968 * intel_dsb_cleanup() - To cleanup DSB context.
969 * @dsb: DSB context
970 *
971 * This function cleanup the DSB context by unpinning and releasing
972 * the VMA object associated with it.
973 */
intel_dsb_cleanup(struct intel_dsb * dsb)974 void intel_dsb_cleanup(struct intel_dsb *dsb)
975 {
976 intel_dsb_buffer_cleanup(&dsb->dsb_buf);
977 kfree(dsb);
978 }
979
intel_dsb_irq_handler(struct intel_display * display,enum pipe pipe,enum intel_dsb_id dsb_id)980 void intel_dsb_irq_handler(struct intel_display *display,
981 enum pipe pipe, enum intel_dsb_id dsb_id)
982 {
983 struct intel_crtc *crtc = intel_crtc_for_pipe(display, pipe);
984 u32 tmp, errors;
985
986 tmp = intel_de_read_fw(display, DSB_INTERRUPT(pipe, dsb_id));
987 intel_de_write_fw(display, DSB_INTERRUPT(pipe, dsb_id), tmp);
988
989 if (tmp & DSB_PROG_INT_STATUS) {
990 spin_lock(&display->drm->event_lock);
991
992 if (crtc->dsb_event) {
993 /*
994 * Update vblank counter/timestamp in case it
995 * hasn't been done yet for this frame.
996 */
997 drm_crtc_accurate_vblank_count(&crtc->base);
998
999 drm_crtc_send_vblank_event(&crtc->base, crtc->dsb_event);
1000 crtc->dsb_event = NULL;
1001 }
1002
1003 spin_unlock(&display->drm->event_lock);
1004 }
1005
1006 errors = tmp & dsb_error_int_status(display);
1007 if (errors & DSB_ATS_FAULT_INT_STATUS)
1008 drm_err(display->drm, "[CRTC:%d:%s] DSB %d ATS fault\n",
1009 crtc->base.base.id, crtc->base.name, dsb_id);
1010 if (errors & DSB_GTT_FAULT_INT_STATUS)
1011 drm_err(display->drm, "[CRTC:%d:%s] DSB %d GTT fault\n",
1012 crtc->base.base.id, crtc->base.name, dsb_id);
1013 if (errors & DSB_RSPTIMEOUT_INT_STATUS)
1014 drm_err(display->drm, "[CRTC:%d:%s] DSB %d response timeout\n",
1015 crtc->base.base.id, crtc->base.name, dsb_id);
1016 if (errors & DSB_POLL_ERR_INT_STATUS)
1017 drm_err(display->drm, "[CRTC:%d:%s] DSB %d poll error\n",
1018 crtc->base.base.id, crtc->base.name, dsb_id);
1019 if (errors & DSB_GOSUB_INT_STATUS)
1020 drm_err(display->drm, "[CRTC:%d:%s] DSB %d GOSUB programming error\n",
1021 crtc->base.base.id, crtc->base.name, dsb_id);
1022 }
1023