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