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