1 /* 2 * Copyright © 2014 Intel Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 * 23 */ 24 25 #include <linux/debugfs.h> 26 #include <linux/firmware.h> 27 #include <drm/drm_vblank.h> 28 29 #include <drm/drm_file.h> 30 #include <drm/drm_print.h> 31 32 #include "i915_reg.h" 33 #include "i915_utils.h" 34 #include "intel_crtc.h" 35 #include "intel_de.h" 36 #include "intel_display_power_well.h" 37 #include "intel_display_regs.h" 38 #include "intel_display_rpm.h" 39 #include "intel_display_types.h" 40 #include "intel_dmc.h" 41 #include "intel_dmc_regs.h" 42 #include "intel_flipq.h" 43 #include "intel_step.h" 44 45 /** 46 * DOC: DMC Firmware Support 47 * 48 * From gen9 onwards we have newly added DMC (Display microcontroller) in display 49 * engine to save and restore the state of display engine when it enter into 50 * low-power state and comes back to normal. 51 */ 52 53 #define INTEL_DMC_FIRMWARE_URL "https://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git" 54 55 enum intel_dmc_id { 56 DMC_FW_MAIN = 0, 57 DMC_FW_PIPEA, 58 DMC_FW_PIPEB, 59 DMC_FW_PIPEC, 60 DMC_FW_PIPED, 61 DMC_FW_MAX 62 }; 63 64 struct intel_dmc { 65 struct intel_display *display; 66 struct work_struct work; 67 const char *fw_path; 68 u32 max_fw_size; /* bytes */ 69 u32 version; 70 struct { 71 u32 dc5_start; 72 u32 count; 73 } dc6_allowed; 74 struct dmc_fw_info { 75 u32 mmio_count; 76 i915_reg_t mmioaddr[20]; 77 u32 mmiodata[20]; 78 u32 dmc_offset; 79 u32 start_mmioaddr; 80 u32 dmc_fw_size; /*dwords */ 81 u32 *payload; 82 bool present; 83 } dmc_info[DMC_FW_MAX]; 84 }; 85 86 /* Note: This may be NULL. */ 87 static struct intel_dmc *display_to_dmc(struct intel_display *display) 88 { 89 return display->dmc.dmc; 90 } 91 92 static const char *dmc_firmware_param(struct intel_display *display) 93 { 94 const char *p = display->params.dmc_firmware_path; 95 96 return p && *p ? p : NULL; 97 } 98 99 static bool dmc_firmware_param_disabled(struct intel_display *display) 100 { 101 const char *p = dmc_firmware_param(display); 102 103 /* Magic path to indicate disabled */ 104 return p && !strcmp(p, "/dev/null"); 105 } 106 107 #define DMC_VERSION(major, minor) ((major) << 16 | (minor)) 108 #define DMC_VERSION_MAJOR(version) ((version) >> 16) 109 #define DMC_VERSION_MINOR(version) ((version) & 0xffff) 110 111 #define DMC_PATH(platform) \ 112 "i915/" __stringify(platform) "_dmc.bin" 113 114 /* 115 * New DMC additions should not use this. This is used solely to remain 116 * compatible with systems that have not yet updated DMC blobs to use 117 * unversioned file names. 118 */ 119 #define DMC_LEGACY_PATH(platform, major, minor) \ 120 "i915/" \ 121 __stringify(platform) "_dmc_ver" \ 122 __stringify(major) "_" \ 123 __stringify(minor) ".bin" 124 125 #define XE2LPD_DMC_MAX_FW_SIZE 0x8000 126 #define XELPDP_DMC_MAX_FW_SIZE 0x7000 127 #define DISPLAY_VER13_DMC_MAX_FW_SIZE 0x20000 128 #define DISPLAY_VER12_DMC_MAX_FW_SIZE ICL_DMC_MAX_FW_SIZE 129 130 #define XE3LPD_3002_DMC_PATH DMC_PATH(xe3lpd_3002) 131 MODULE_FIRMWARE(XE3LPD_3002_DMC_PATH); 132 133 #define XE3LPD_DMC_PATH DMC_PATH(xe3lpd) 134 MODULE_FIRMWARE(XE3LPD_DMC_PATH); 135 136 #define XE2LPD_DMC_PATH DMC_PATH(xe2lpd) 137 MODULE_FIRMWARE(XE2LPD_DMC_PATH); 138 139 #define BMG_DMC_PATH DMC_PATH(bmg) 140 MODULE_FIRMWARE(BMG_DMC_PATH); 141 142 #define MTL_DMC_PATH DMC_PATH(mtl) 143 MODULE_FIRMWARE(MTL_DMC_PATH); 144 145 #define DG2_DMC_PATH DMC_LEGACY_PATH(dg2, 2, 08) 146 MODULE_FIRMWARE(DG2_DMC_PATH); 147 148 #define ADLP_DMC_PATH DMC_PATH(adlp) 149 #define ADLP_DMC_FALLBACK_PATH DMC_LEGACY_PATH(adlp, 2, 16) 150 MODULE_FIRMWARE(ADLP_DMC_PATH); 151 MODULE_FIRMWARE(ADLP_DMC_FALLBACK_PATH); 152 153 #define ADLS_DMC_PATH DMC_LEGACY_PATH(adls, 2, 01) 154 MODULE_FIRMWARE(ADLS_DMC_PATH); 155 156 #define DG1_DMC_PATH DMC_LEGACY_PATH(dg1, 2, 02) 157 MODULE_FIRMWARE(DG1_DMC_PATH); 158 159 #define RKL_DMC_PATH DMC_LEGACY_PATH(rkl, 2, 03) 160 MODULE_FIRMWARE(RKL_DMC_PATH); 161 162 #define TGL_DMC_PATH DMC_LEGACY_PATH(tgl, 2, 12) 163 MODULE_FIRMWARE(TGL_DMC_PATH); 164 165 #define ICL_DMC_PATH DMC_LEGACY_PATH(icl, 1, 09) 166 #define ICL_DMC_MAX_FW_SIZE 0x6000 167 MODULE_FIRMWARE(ICL_DMC_PATH); 168 169 #define GLK_DMC_PATH DMC_LEGACY_PATH(glk, 1, 04) 170 #define GLK_DMC_MAX_FW_SIZE 0x4000 171 MODULE_FIRMWARE(GLK_DMC_PATH); 172 173 #define KBL_DMC_PATH DMC_LEGACY_PATH(kbl, 1, 04) 174 #define KBL_DMC_MAX_FW_SIZE BXT_DMC_MAX_FW_SIZE 175 MODULE_FIRMWARE(KBL_DMC_PATH); 176 177 #define SKL_DMC_PATH DMC_LEGACY_PATH(skl, 1, 27) 178 #define SKL_DMC_MAX_FW_SIZE BXT_DMC_MAX_FW_SIZE 179 MODULE_FIRMWARE(SKL_DMC_PATH); 180 181 #define BXT_DMC_PATH DMC_LEGACY_PATH(bxt, 1, 07) 182 #define BXT_DMC_MAX_FW_SIZE 0x3000 183 MODULE_FIRMWARE(BXT_DMC_PATH); 184 185 static const char *dmc_firmware_default(struct intel_display *display, u32 *size) 186 { 187 const char *fw_path = NULL; 188 u32 max_fw_size = 0; 189 if (DISPLAY_VERx100(display) == 3002) { 190 fw_path = XE3LPD_3002_DMC_PATH; 191 max_fw_size = XE2LPD_DMC_MAX_FW_SIZE; 192 } else if (DISPLAY_VERx100(display) == 3000) { 193 fw_path = XE3LPD_DMC_PATH; 194 max_fw_size = XE2LPD_DMC_MAX_FW_SIZE; 195 } else if (DISPLAY_VERx100(display) == 2000) { 196 fw_path = XE2LPD_DMC_PATH; 197 max_fw_size = XE2LPD_DMC_MAX_FW_SIZE; 198 } else if (DISPLAY_VERx100(display) == 1401) { 199 fw_path = BMG_DMC_PATH; 200 max_fw_size = XELPDP_DMC_MAX_FW_SIZE; 201 } else if (DISPLAY_VERx100(display) == 1400) { 202 fw_path = MTL_DMC_PATH; 203 max_fw_size = XELPDP_DMC_MAX_FW_SIZE; 204 } else if (display->platform.dg2) { 205 fw_path = DG2_DMC_PATH; 206 max_fw_size = DISPLAY_VER13_DMC_MAX_FW_SIZE; 207 } else if (display->platform.alderlake_p) { 208 fw_path = ADLP_DMC_PATH; 209 max_fw_size = DISPLAY_VER13_DMC_MAX_FW_SIZE; 210 } else if (display->platform.alderlake_s) { 211 fw_path = ADLS_DMC_PATH; 212 max_fw_size = DISPLAY_VER12_DMC_MAX_FW_SIZE; 213 } else if (display->platform.dg1) { 214 fw_path = DG1_DMC_PATH; 215 max_fw_size = DISPLAY_VER12_DMC_MAX_FW_SIZE; 216 } else if (display->platform.rocketlake) { 217 fw_path = RKL_DMC_PATH; 218 max_fw_size = DISPLAY_VER12_DMC_MAX_FW_SIZE; 219 } else if (display->platform.tigerlake) { 220 fw_path = TGL_DMC_PATH; 221 max_fw_size = DISPLAY_VER12_DMC_MAX_FW_SIZE; 222 } else if (DISPLAY_VER(display) == 11) { 223 fw_path = ICL_DMC_PATH; 224 max_fw_size = ICL_DMC_MAX_FW_SIZE; 225 } else if (display->platform.geminilake) { 226 fw_path = GLK_DMC_PATH; 227 max_fw_size = GLK_DMC_MAX_FW_SIZE; 228 } else if (display->platform.kabylake || 229 display->platform.coffeelake || 230 display->platform.cometlake) { 231 fw_path = KBL_DMC_PATH; 232 max_fw_size = KBL_DMC_MAX_FW_SIZE; 233 } else if (display->platform.skylake) { 234 fw_path = SKL_DMC_PATH; 235 max_fw_size = SKL_DMC_MAX_FW_SIZE; 236 } else if (display->platform.broxton) { 237 fw_path = BXT_DMC_PATH; 238 max_fw_size = BXT_DMC_MAX_FW_SIZE; 239 } 240 241 *size = max_fw_size; 242 243 return fw_path; 244 } 245 246 #define DMC_DEFAULT_FW_OFFSET 0xFFFFFFFF 247 #define PACKAGE_MAX_FW_INFO_ENTRIES 20 248 #define PACKAGE_V2_MAX_FW_INFO_ENTRIES 32 249 #define DMC_V1_MAX_MMIO_COUNT 8 250 #define DMC_V3_MAX_MMIO_COUNT 20 251 #define DMC_V1_MMIO_START_RANGE 0x80000 252 253 #define PIPE_TO_DMC_ID(pipe) (DMC_FW_PIPEA + ((pipe) - PIPE_A)) 254 255 struct intel_css_header { 256 /* 0x09 for DMC */ 257 u32 module_type; 258 259 /* Includes the DMC specific header in dwords */ 260 u32 header_len; 261 262 /* always value would be 0x10000 */ 263 u32 header_ver; 264 265 /* Not used */ 266 u32 module_id; 267 268 /* Not used */ 269 u32 module_vendor; 270 271 /* in YYYYMMDD format */ 272 u32 date; 273 274 /* Size in dwords (CSS_Headerlen + PackageHeaderLen + dmc FWsLen)/4 */ 275 u32 size; 276 277 /* Not used */ 278 u32 key_size; 279 280 /* Not used */ 281 u32 modulus_size; 282 283 /* Not used */ 284 u32 exponent_size; 285 286 /* Not used */ 287 u32 reserved1[12]; 288 289 /* Major Minor */ 290 u32 version; 291 292 /* Not used */ 293 u32 reserved2[8]; 294 295 /* Not used */ 296 u32 kernel_header_info; 297 } __packed; 298 299 struct intel_fw_info { 300 u8 reserved1; 301 302 /* reserved on package_header version 1, must be 0 on version 2 */ 303 u8 dmc_id; 304 305 /* Stepping (A, B, C, ..., *). * is a wildcard */ 306 char stepping; 307 308 /* Sub-stepping (0, 1, ..., *). * is a wildcard */ 309 char substepping; 310 311 u32 offset; 312 u32 reserved2; 313 } __packed; 314 315 struct intel_package_header { 316 /* DMC container header length in dwords */ 317 u8 header_len; 318 319 /* 0x01, 0x02 */ 320 u8 header_ver; 321 322 u8 reserved[10]; 323 324 /* Number of valid entries in the FWInfo array below */ 325 u32 num_entries; 326 } __packed; 327 328 struct intel_dmc_header_base { 329 /* always value would be 0x40403E3E */ 330 u32 signature; 331 332 /* DMC binary header length */ 333 u8 header_len; 334 335 /* 0x01 */ 336 u8 header_ver; 337 338 /* Reserved */ 339 u16 dmcc_ver; 340 341 /* Major, Minor */ 342 u32 project; 343 344 /* Firmware program size (excluding header) in dwords */ 345 u32 fw_size; 346 347 /* Major Minor version */ 348 u32 fw_version; 349 } __packed; 350 351 struct intel_dmc_header_v1 { 352 struct intel_dmc_header_base base; 353 354 /* Number of valid MMIO cycles present. */ 355 u32 mmio_count; 356 357 /* MMIO address */ 358 u32 mmioaddr[DMC_V1_MAX_MMIO_COUNT]; 359 360 /* MMIO data */ 361 u32 mmiodata[DMC_V1_MAX_MMIO_COUNT]; 362 363 /* FW filename */ 364 char dfile[32]; 365 366 u32 reserved1[2]; 367 } __packed; 368 369 struct intel_dmc_header_v3 { 370 struct intel_dmc_header_base base; 371 372 /* DMC RAM start MMIO address */ 373 u32 start_mmioaddr; 374 375 u32 reserved[9]; 376 377 /* FW filename */ 378 char dfile[32]; 379 380 /* Number of valid MMIO cycles present. */ 381 u32 mmio_count; 382 383 /* MMIO address */ 384 u32 mmioaddr[DMC_V3_MAX_MMIO_COUNT]; 385 386 /* MMIO data */ 387 u32 mmiodata[DMC_V3_MAX_MMIO_COUNT]; 388 } __packed; 389 390 struct stepping_info { 391 char stepping; 392 char substepping; 393 }; 394 395 #define for_each_dmc_id(__dmc_id) \ 396 for ((__dmc_id) = DMC_FW_MAIN; (__dmc_id) < DMC_FW_MAX; (__dmc_id)++) 397 398 static bool is_valid_dmc_id(enum intel_dmc_id dmc_id) 399 { 400 return dmc_id >= DMC_FW_MAIN && dmc_id < DMC_FW_MAX; 401 } 402 403 static bool has_dmc_id_fw(struct intel_display *display, enum intel_dmc_id dmc_id) 404 { 405 struct intel_dmc *dmc = display_to_dmc(display); 406 407 return dmc && dmc->dmc_info[dmc_id].payload; 408 } 409 410 bool intel_dmc_has_payload(struct intel_display *display) 411 { 412 return has_dmc_id_fw(display, DMC_FW_MAIN); 413 } 414 415 static const struct stepping_info * 416 intel_get_stepping_info(struct intel_display *display, 417 struct stepping_info *si) 418 { 419 const char *step_name = intel_step_name(INTEL_DISPLAY_STEP(display)); 420 421 si->stepping = step_name[0]; 422 si->substepping = step_name[1]; 423 return si; 424 } 425 426 static void gen9_set_dc_state_debugmask(struct intel_display *display) 427 { 428 /* The below bit doesn't need to be cleared ever afterwards */ 429 intel_de_rmw(display, DC_STATE_DEBUG, 0, 430 DC_STATE_DEBUG_MASK_CORES | DC_STATE_DEBUG_MASK_MEMORY_UP); 431 intel_de_posting_read(display, DC_STATE_DEBUG); 432 } 433 434 static void disable_event_handler(struct intel_display *display, 435 i915_reg_t ctl_reg, i915_reg_t htp_reg) 436 { 437 intel_de_write(display, ctl_reg, 438 REG_FIELD_PREP(DMC_EVT_CTL_TYPE_MASK, 439 DMC_EVT_CTL_TYPE_EDGE_0_1) | 440 REG_FIELD_PREP(DMC_EVT_CTL_EVENT_ID_MASK, 441 DMC_EVENT_FALSE)); 442 intel_de_write(display, htp_reg, 0); 443 } 444 445 static void disable_all_event_handlers(struct intel_display *display, 446 enum intel_dmc_id dmc_id) 447 { 448 int handler; 449 450 /* TODO: disable the event handlers on pre-GEN12 platforms as well */ 451 if (DISPLAY_VER(display) < 12) 452 return; 453 454 if (!has_dmc_id_fw(display, dmc_id)) 455 return; 456 457 for (handler = 0; handler < DMC_EVENT_HANDLER_COUNT_GEN12; handler++) 458 disable_event_handler(display, 459 DMC_EVT_CTL(display, dmc_id, handler), 460 DMC_EVT_HTP(display, dmc_id, handler)); 461 } 462 463 static void adlp_pipedmc_clock_gating_wa(struct intel_display *display, bool enable) 464 { 465 enum pipe pipe; 466 467 /* 468 * Wa_16015201720:adl-p,dg2 469 * The WA requires clock gating to be disabled all the time 470 * for pipe A and B. 471 * For pipe C and D clock gating needs to be disabled only 472 * during initializing the firmware. 473 */ 474 if (enable) 475 for (pipe = PIPE_A; pipe <= PIPE_D; pipe++) 476 intel_de_rmw(display, CLKGATE_DIS_PSL_EXT(pipe), 477 0, PIPEDMC_GATING_DIS); 478 else 479 for (pipe = PIPE_C; pipe <= PIPE_D; pipe++) 480 intel_de_rmw(display, CLKGATE_DIS_PSL_EXT(pipe), 481 PIPEDMC_GATING_DIS, 0); 482 } 483 484 static void mtl_pipedmc_clock_gating_wa(struct intel_display *display) 485 { 486 /* 487 * Wa_16015201720 488 * The WA requires clock gating to be disabled all the time 489 * for pipe A and B. 490 */ 491 intel_de_rmw(display, GEN9_CLKGATE_DIS_0, 0, 492 MTL_PIPEDMC_GATING_DIS(PIPE_A) | 493 MTL_PIPEDMC_GATING_DIS(PIPE_B)); 494 } 495 496 static void pipedmc_clock_gating_wa(struct intel_display *display, bool enable) 497 { 498 if (display->platform.meteorlake && enable) 499 mtl_pipedmc_clock_gating_wa(display); 500 else if (DISPLAY_VER(display) == 13) 501 adlp_pipedmc_clock_gating_wa(display, enable); 502 } 503 504 static u32 pipedmc_interrupt_mask(struct intel_display *display) 505 { 506 /* 507 * FIXME PIPEDMC_ERROR not enabled for now due to LNL pipe B 508 * triggering it during the first DC state transition. Figure 509 * out what is going on... 510 */ 511 return PIPEDMC_FLIPQ_PROG_DONE | 512 PIPEDMC_GTT_FAULT | 513 PIPEDMC_ATS_FAULT; 514 } 515 516 static u32 dmc_evt_ctl_disable(void) 517 { 518 return REG_FIELD_PREP(DMC_EVT_CTL_TYPE_MASK, 519 DMC_EVT_CTL_TYPE_EDGE_0_1) | 520 REG_FIELD_PREP(DMC_EVT_CTL_EVENT_ID_MASK, 521 DMC_EVENT_FALSE); 522 } 523 524 static bool is_dmc_evt_ctl_reg(struct intel_display *display, 525 enum intel_dmc_id dmc_id, i915_reg_t reg) 526 { 527 u32 offset = i915_mmio_reg_offset(reg); 528 u32 start = i915_mmio_reg_offset(DMC_EVT_CTL(display, dmc_id, 0)); 529 u32 end = i915_mmio_reg_offset(DMC_EVT_CTL(display, dmc_id, DMC_EVENT_HANDLER_COUNT_GEN12)); 530 531 return offset >= start && offset < end; 532 } 533 534 static bool is_dmc_evt_htp_reg(struct intel_display *display, 535 enum intel_dmc_id dmc_id, i915_reg_t reg) 536 { 537 u32 offset = i915_mmio_reg_offset(reg); 538 u32 start = i915_mmio_reg_offset(DMC_EVT_HTP(display, dmc_id, 0)); 539 u32 end = i915_mmio_reg_offset(DMC_EVT_HTP(display, dmc_id, DMC_EVENT_HANDLER_COUNT_GEN12)); 540 541 return offset >= start && offset < end; 542 } 543 544 static bool is_event_handler(struct intel_display *display, 545 enum intel_dmc_id dmc_id, 546 unsigned int event_id, 547 i915_reg_t reg, u32 data) 548 { 549 return is_dmc_evt_ctl_reg(display, dmc_id, reg) && 550 REG_FIELD_GET(DMC_EVT_CTL_EVENT_ID_MASK, data) == event_id; 551 } 552 553 static bool fixup_dmc_evt(struct intel_display *display, 554 enum intel_dmc_id dmc_id, 555 i915_reg_t reg_ctl, u32 *data_ctl, 556 i915_reg_t reg_htp, u32 *data_htp) 557 { 558 if (!is_dmc_evt_ctl_reg(display, dmc_id, reg_ctl)) 559 return false; 560 561 if (!is_dmc_evt_htp_reg(display, dmc_id, reg_htp)) 562 return false; 563 564 /* make sure reg_ctl and reg_htp are for the same event */ 565 if (i915_mmio_reg_offset(reg_ctl) - i915_mmio_reg_offset(DMC_EVT_CTL(display, dmc_id, 0)) != 566 i915_mmio_reg_offset(reg_htp) - i915_mmio_reg_offset(DMC_EVT_HTP(display, dmc_id, 0))) 567 return false; 568 569 /* 570 * On ADL-S the HRR event handler is not restored after DC6. 571 * Clear it to zero from the beginning to avoid mismatches later. 572 */ 573 if (display->platform.alderlake_s && dmc_id == DMC_FW_MAIN && 574 is_event_handler(display, dmc_id, MAINDMC_EVENT_VBLANK_A, reg_ctl, *data_ctl)) { 575 *data_ctl = 0; 576 *data_htp = 0; 577 return true; 578 } 579 580 return false; 581 } 582 583 static bool disable_dmc_evt(struct intel_display *display, 584 enum intel_dmc_id dmc_id, 585 i915_reg_t reg, u32 data) 586 { 587 if (!is_dmc_evt_ctl_reg(display, dmc_id, reg)) 588 return false; 589 590 /* keep all pipe DMC events disabled by default */ 591 if (dmc_id != DMC_FW_MAIN) 592 return true; 593 594 /* also disable the flip queue event on the main DMC on TGL */ 595 if (display->platform.tigerlake && 596 is_event_handler(display, dmc_id, MAINDMC_EVENT_CLK_MSEC, reg, data)) 597 return true; 598 599 /* also disable the HRR event on the main DMC on TGL/ADLS */ 600 if ((display->platform.tigerlake || display->platform.alderlake_s) && 601 is_event_handler(display, dmc_id, MAINDMC_EVENT_VBLANK_A, reg, data)) 602 return true; 603 604 return false; 605 } 606 607 static u32 dmc_mmiodata(struct intel_display *display, 608 struct intel_dmc *dmc, 609 enum intel_dmc_id dmc_id, int i) 610 { 611 if (disable_dmc_evt(display, dmc_id, 612 dmc->dmc_info[dmc_id].mmioaddr[i], 613 dmc->dmc_info[dmc_id].mmiodata[i])) 614 return dmc_evt_ctl_disable(); 615 else 616 return dmc->dmc_info[dmc_id].mmiodata[i]; 617 } 618 619 static void dmc_load_mmio(struct intel_display *display, enum intel_dmc_id dmc_id) 620 { 621 struct intel_dmc *dmc = display_to_dmc(display); 622 int i; 623 624 for (i = 0; i < dmc->dmc_info[dmc_id].mmio_count; i++) { 625 intel_de_write(display, dmc->dmc_info[dmc_id].mmioaddr[i], 626 dmc_mmiodata(display, dmc, dmc_id, i)); 627 } 628 } 629 630 static void dmc_load_program(struct intel_display *display, enum intel_dmc_id dmc_id) 631 { 632 struct intel_dmc *dmc = display_to_dmc(display); 633 int i; 634 635 disable_all_event_handlers(display, dmc_id); 636 637 preempt_disable(); 638 639 for (i = 0; i < dmc->dmc_info[dmc_id].dmc_fw_size; i++) { 640 intel_de_write_fw(display, 641 DMC_PROGRAM(dmc->dmc_info[dmc_id].start_mmioaddr, i), 642 dmc->dmc_info[dmc_id].payload[i]); 643 } 644 645 preempt_enable(); 646 647 dmc_load_mmio(display, dmc_id); 648 } 649 650 static void assert_dmc_loaded(struct intel_display *display, 651 enum intel_dmc_id dmc_id) 652 { 653 struct intel_dmc *dmc = display_to_dmc(display); 654 u32 expected, found; 655 int i; 656 657 if (!is_valid_dmc_id(dmc_id) || !has_dmc_id_fw(display, dmc_id)) 658 return; 659 660 found = intel_de_read(display, DMC_PROGRAM(dmc->dmc_info[dmc_id].start_mmioaddr, 0)); 661 expected = dmc->dmc_info[dmc_id].payload[0]; 662 663 drm_WARN(display->drm, found != expected, 664 "DMC %d program storage start incorrect (expected 0x%x, current 0x%x)\n", 665 dmc_id, expected, found); 666 667 for (i = 0; i < dmc->dmc_info[dmc_id].mmio_count; i++) { 668 i915_reg_t reg = dmc->dmc_info[dmc_id].mmioaddr[i]; 669 670 found = intel_de_read(display, reg); 671 expected = dmc_mmiodata(display, dmc, dmc_id, i); 672 673 /* once set DMC_EVT_CTL_ENABLE can't be cleared :/ */ 674 if (is_dmc_evt_ctl_reg(display, dmc_id, reg)) { 675 found &= ~DMC_EVT_CTL_ENABLE; 676 expected &= ~DMC_EVT_CTL_ENABLE; 677 } 678 679 drm_WARN(display->drm, found != expected, 680 "DMC %d mmio[%d]/0x%x incorrect (expected 0x%x, current 0x%x)\n", 681 dmc_id, i, i915_mmio_reg_offset(reg), expected, found); 682 } 683 } 684 685 void assert_main_dmc_loaded(struct intel_display *display) 686 { 687 assert_dmc_loaded(display, DMC_FW_MAIN); 688 } 689 690 static bool need_pipedmc_load_program(struct intel_display *display) 691 { 692 /* On TGL/derivatives pipe DMC state is lost when PG1 is disabled */ 693 return DISPLAY_VER(display) == 12; 694 } 695 696 static bool need_pipedmc_load_mmio(struct intel_display *display, enum pipe pipe) 697 { 698 /* 699 * PTL: 700 * - pipe A/B DMC doesn't need save/restore 701 * - pipe C/D DMC is in PG0, needs manual save/restore 702 */ 703 if (DISPLAY_VER(display) == 30) 704 return pipe >= PIPE_C; 705 706 /* 707 * FIXME LNL unclear, main DMC firmware has the pipe DMC A/B PG0 708 * save/restore, but so far unable to see the loss of pipe DMC state 709 * in action. Are we just failing to turn off PG0 due to some other 710 * SoC level stuff? 711 */ 712 if (DISPLAY_VER(display) == 20) 713 return false; 714 715 /* 716 * FIXME BMG untested, main DMC firmware has the 717 * pipe DMC A/B PG0 save/restore... 718 */ 719 if (display->platform.battlemage) 720 return false; 721 722 /* 723 * DG2: 724 * - Pipe DMCs presumably in PG0? 725 * - No DC6, and even DC9 doesn't seem to result 726 * in loss of DMC state for whatever reason 727 */ 728 if (display->platform.dg2) 729 return false; 730 731 /* 732 * ADL/MTL: 733 * - pipe A/B DMC is in PG0, saved/restored by the main DMC 734 * - pipe C/D DMC is in PG0, needs manual save/restore 735 */ 736 if (IS_DISPLAY_VER(display, 13, 14)) 737 return pipe >= PIPE_C; 738 739 return false; 740 } 741 742 static bool can_enable_pipedmc(const struct intel_crtc_state *crtc_state) 743 { 744 struct intel_display *display = to_intel_display(crtc_state); 745 746 /* 747 * On TGL/derivatives pipe DMC state is lost when PG1 is disabled. 748 * Do not even enable the pipe DMC when that can happen outside 749 * of driver control (PSR+DC5/6). 750 */ 751 if (DISPLAY_VER(display) == 12 && crtc_state->has_psr) 752 return false; 753 754 return true; 755 } 756 757 void intel_dmc_enable_pipe(const struct intel_crtc_state *crtc_state) 758 { 759 struct intel_display *display = to_intel_display(crtc_state); 760 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc); 761 enum pipe pipe = crtc->pipe; 762 enum intel_dmc_id dmc_id = PIPE_TO_DMC_ID(pipe); 763 764 if (!is_valid_dmc_id(dmc_id) || !has_dmc_id_fw(display, dmc_id)) 765 return; 766 767 if (!can_enable_pipedmc(crtc_state)) { 768 intel_dmc_disable_pipe(crtc_state); 769 return; 770 } 771 772 if (need_pipedmc_load_program(display)) 773 dmc_load_program(display, dmc_id); 774 else if (need_pipedmc_load_mmio(display, pipe)) 775 dmc_load_mmio(display, dmc_id); 776 777 assert_dmc_loaded(display, dmc_id); 778 779 if (DISPLAY_VER(display) >= 20) { 780 intel_flipq_reset(display, pipe); 781 782 intel_de_write(display, PIPEDMC_INTERRUPT(pipe), pipedmc_interrupt_mask(display)); 783 intel_de_write(display, PIPEDMC_INTERRUPT_MASK(pipe), ~pipedmc_interrupt_mask(display)); 784 } 785 786 if (DISPLAY_VER(display) >= 14) 787 intel_de_rmw(display, MTL_PIPEDMC_CONTROL, 0, PIPEDMC_ENABLE_MTL(pipe)); 788 else 789 intel_de_rmw(display, PIPEDMC_CONTROL(pipe), 0, PIPEDMC_ENABLE); 790 } 791 792 void intel_dmc_disable_pipe(const struct intel_crtc_state *crtc_state) 793 { 794 struct intel_display *display = to_intel_display(crtc_state); 795 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc); 796 enum pipe pipe = crtc->pipe; 797 enum intel_dmc_id dmc_id = PIPE_TO_DMC_ID(pipe); 798 799 if (!is_valid_dmc_id(dmc_id) || !has_dmc_id_fw(display, dmc_id)) 800 return; 801 802 if (DISPLAY_VER(display) >= 14) 803 intel_de_rmw(display, MTL_PIPEDMC_CONTROL, PIPEDMC_ENABLE_MTL(pipe), 0); 804 else 805 intel_de_rmw(display, PIPEDMC_CONTROL(pipe), PIPEDMC_ENABLE, 0); 806 807 if (DISPLAY_VER(display) >= 20) { 808 intel_de_write(display, PIPEDMC_INTERRUPT_MASK(pipe), ~0); 809 intel_de_write(display, PIPEDMC_INTERRUPT(pipe), pipedmc_interrupt_mask(display)); 810 811 intel_flipq_reset(display, pipe); 812 } 813 } 814 815 static void dmc_configure_event(struct intel_display *display, 816 enum intel_dmc_id dmc_id, 817 unsigned int event_id, 818 bool enable) 819 { 820 struct intel_dmc *dmc = display_to_dmc(display); 821 int num_handlers = 0; 822 int i; 823 824 for (i = 0; i < dmc->dmc_info[dmc_id].mmio_count; i++) { 825 i915_reg_t reg = dmc->dmc_info[dmc_id].mmioaddr[i]; 826 u32 data = dmc->dmc_info[dmc_id].mmiodata[i]; 827 828 if (!is_event_handler(display, dmc_id, event_id, reg, data)) 829 continue; 830 831 intel_de_write(display, reg, enable ? data : dmc_evt_ctl_disable()); 832 num_handlers++; 833 } 834 835 drm_WARN_ONCE(display->drm, num_handlers != 1, 836 "DMC %d has %d handlers for event 0x%x\n", 837 dmc_id, num_handlers, event_id); 838 } 839 840 /** 841 * intel_dmc_block_pkgc() - block PKG C-state 842 * @display: display instance 843 * @pipe: pipe which register use to block 844 * @block: block/unblock 845 * 846 * This interface is target for Wa_16025596647 usage. I.e. to set/clear 847 * PIPEDMC_BLOCK_PKGC_SW_BLOCK_PKGC_ALWAYS bit in PIPEDMC_BLOCK_PKGC_SW register. 848 */ 849 void intel_dmc_block_pkgc(struct intel_display *display, enum pipe pipe, 850 bool block) 851 { 852 intel_de_rmw(display, PIPEDMC_BLOCK_PKGC_SW(pipe), 853 PIPEDMC_BLOCK_PKGC_SW_BLOCK_PKGC_ALWAYS, block ? 854 PIPEDMC_BLOCK_PKGC_SW_BLOCK_PKGC_ALWAYS : 0); 855 } 856 857 /** 858 * intel_dmc_start_pkgc_exit_at_start_of_undelayed_vblank() - start of PKG 859 * C-state exit 860 * @display: display instance 861 * @pipe: pipe which register use to block 862 * @enable: enable/disable 863 * 864 * This interface is target for Wa_16025596647 usage. I.e. start the package C 865 * exit at the start of the undelayed vblank 866 */ 867 void intel_dmc_start_pkgc_exit_at_start_of_undelayed_vblank(struct intel_display *display, 868 enum pipe pipe, bool enable) 869 { 870 enum intel_dmc_id dmc_id = PIPE_TO_DMC_ID(pipe); 871 872 dmc_configure_event(display, dmc_id, PIPEDMC_EVENT_VBLANK, enable); 873 } 874 875 /** 876 * intel_dmc_load_program() - write the firmware from memory to register. 877 * @display: display instance 878 * 879 * DMC firmware is read from a .bin file and kept in internal memory one time. 880 * Everytime display comes back from low power state this function is called to 881 * copy the firmware from internal memory to registers. 882 */ 883 void intel_dmc_load_program(struct intel_display *display) 884 { 885 struct i915_power_domains *power_domains = &display->power.domains; 886 enum intel_dmc_id dmc_id; 887 888 if (!intel_dmc_has_payload(display)) 889 return; 890 891 assert_display_rpm_held(display); 892 893 pipedmc_clock_gating_wa(display, true); 894 895 for_each_dmc_id(dmc_id) { 896 dmc_load_program(display, dmc_id); 897 assert_dmc_loaded(display, dmc_id); 898 } 899 900 if (DISPLAY_VER(display) >= 20) 901 intel_de_write(display, DMC_FQ_W2_PTS_CFG_SEL, 902 PIPE_D_DMC_W2_PTS_CONFIG_SELECT(PIPE_D) | 903 PIPE_C_DMC_W2_PTS_CONFIG_SELECT(PIPE_C) | 904 PIPE_B_DMC_W2_PTS_CONFIG_SELECT(PIPE_B) | 905 PIPE_A_DMC_W2_PTS_CONFIG_SELECT(PIPE_A)); 906 907 power_domains->dc_state = 0; 908 909 gen9_set_dc_state_debugmask(display); 910 911 pipedmc_clock_gating_wa(display, false); 912 } 913 914 /** 915 * intel_dmc_disable_program() - disable the firmware 916 * @display: display instance 917 * 918 * Disable all event handlers in the firmware, making sure the firmware is 919 * inactive after the display is uninitialized. 920 */ 921 void intel_dmc_disable_program(struct intel_display *display) 922 { 923 enum intel_dmc_id dmc_id; 924 925 if (!intel_dmc_has_payload(display)) 926 return; 927 928 pipedmc_clock_gating_wa(display, true); 929 930 for_each_dmc_id(dmc_id) 931 disable_all_event_handlers(display, dmc_id); 932 933 pipedmc_clock_gating_wa(display, false); 934 } 935 936 static bool fw_info_matches_stepping(const struct intel_fw_info *fw_info, 937 const struct stepping_info *si) 938 { 939 if ((fw_info->substepping == '*' && si->stepping == fw_info->stepping) || 940 (si->stepping == fw_info->stepping && si->substepping == fw_info->substepping) || 941 /* 942 * If we don't find a more specific one from above two checks, we 943 * then check for the generic one to be sure to work even with 944 * "broken firmware" 945 */ 946 (si->stepping == '*' && si->substepping == fw_info->substepping) || 947 (fw_info->stepping == '*' && fw_info->substepping == '*')) 948 return true; 949 950 return false; 951 } 952 953 /* 954 * Search fw_info table for dmc_offset to find firmware binary: num_entries is 955 * already sanitized. 956 */ 957 static void dmc_set_fw_offset(struct intel_dmc *dmc, 958 const struct intel_fw_info *fw_info, 959 unsigned int num_entries, 960 const struct stepping_info *si, 961 u8 package_ver) 962 { 963 struct intel_display *display = dmc->display; 964 enum intel_dmc_id dmc_id; 965 unsigned int i; 966 967 for (i = 0; i < num_entries; i++) { 968 dmc_id = package_ver <= 1 ? DMC_FW_MAIN : fw_info[i].dmc_id; 969 970 if (!is_valid_dmc_id(dmc_id)) { 971 drm_dbg(display->drm, "Unsupported firmware id: %u\n", dmc_id); 972 continue; 973 } 974 975 /* More specific versions come first, so we don't even have to 976 * check for the stepping since we already found a previous FW 977 * for this id. 978 */ 979 if (dmc->dmc_info[dmc_id].present) 980 continue; 981 982 if (fw_info_matches_stepping(&fw_info[i], si)) { 983 dmc->dmc_info[dmc_id].present = true; 984 dmc->dmc_info[dmc_id].dmc_offset = fw_info[i].offset; 985 } 986 } 987 } 988 989 static bool dmc_mmio_addr_sanity_check(struct intel_dmc *dmc, 990 const u32 *mmioaddr, u32 mmio_count, 991 int header_ver, enum intel_dmc_id dmc_id) 992 { 993 struct intel_display *display = dmc->display; 994 u32 start_range, end_range; 995 int i; 996 997 if (header_ver == 1) { 998 start_range = DMC_MMIO_START_RANGE; 999 end_range = DMC_MMIO_END_RANGE; 1000 } else if (dmc_id == DMC_FW_MAIN) { 1001 start_range = TGL_MAIN_MMIO_START; 1002 end_range = TGL_MAIN_MMIO_END; 1003 } else if (DISPLAY_VER(display) >= 13) { 1004 start_range = ADLP_PIPE_MMIO_START; 1005 end_range = ADLP_PIPE_MMIO_END; 1006 } else if (DISPLAY_VER(display) >= 12) { 1007 start_range = TGL_PIPE_MMIO_START(dmc_id); 1008 end_range = TGL_PIPE_MMIO_END(dmc_id); 1009 } else { 1010 drm_warn(display->drm, "Unknown mmio range for sanity check"); 1011 return false; 1012 } 1013 1014 for (i = 0; i < mmio_count; i++) { 1015 if (mmioaddr[i] < start_range || mmioaddr[i] > end_range) 1016 return false; 1017 } 1018 1019 return true; 1020 } 1021 1022 static u32 parse_dmc_fw_header(struct intel_dmc *dmc, 1023 const struct intel_dmc_header_base *dmc_header, 1024 size_t rem_size, enum intel_dmc_id dmc_id) 1025 { 1026 struct intel_display *display = dmc->display; 1027 struct dmc_fw_info *dmc_info = &dmc->dmc_info[dmc_id]; 1028 unsigned int header_len_bytes, dmc_header_size, payload_size, i; 1029 const u32 *mmioaddr, *mmiodata; 1030 u32 mmio_count, mmio_count_max, start_mmioaddr; 1031 u8 *payload; 1032 1033 BUILD_BUG_ON(ARRAY_SIZE(dmc_info->mmioaddr) < DMC_V3_MAX_MMIO_COUNT || 1034 ARRAY_SIZE(dmc_info->mmioaddr) < DMC_V1_MAX_MMIO_COUNT); 1035 1036 /* 1037 * Check if we can access common fields, we will checkc again below 1038 * after we have read the version 1039 */ 1040 if (rem_size < sizeof(struct intel_dmc_header_base)) 1041 goto error_truncated; 1042 1043 /* Cope with small differences between v1 and v3 */ 1044 if (dmc_header->header_ver == 3) { 1045 const struct intel_dmc_header_v3 *v3 = 1046 (const struct intel_dmc_header_v3 *)dmc_header; 1047 1048 if (rem_size < sizeof(struct intel_dmc_header_v3)) 1049 goto error_truncated; 1050 1051 mmioaddr = v3->mmioaddr; 1052 mmiodata = v3->mmiodata; 1053 mmio_count = v3->mmio_count; 1054 mmio_count_max = DMC_V3_MAX_MMIO_COUNT; 1055 /* header_len is in dwords */ 1056 header_len_bytes = dmc_header->header_len * 4; 1057 start_mmioaddr = v3->start_mmioaddr; 1058 dmc_header_size = sizeof(*v3); 1059 } else if (dmc_header->header_ver == 1) { 1060 const struct intel_dmc_header_v1 *v1 = 1061 (const struct intel_dmc_header_v1 *)dmc_header; 1062 1063 if (rem_size < sizeof(struct intel_dmc_header_v1)) 1064 goto error_truncated; 1065 1066 mmioaddr = v1->mmioaddr; 1067 mmiodata = v1->mmiodata; 1068 mmio_count = v1->mmio_count; 1069 mmio_count_max = DMC_V1_MAX_MMIO_COUNT; 1070 header_len_bytes = dmc_header->header_len; 1071 start_mmioaddr = DMC_V1_MMIO_START_RANGE; 1072 dmc_header_size = sizeof(*v1); 1073 } else { 1074 drm_err(display->drm, "Unknown DMC fw header version: %u\n", 1075 dmc_header->header_ver); 1076 return 0; 1077 } 1078 1079 if (header_len_bytes != dmc_header_size) { 1080 drm_err(display->drm, "DMC firmware has wrong dmc header length " 1081 "(%u bytes)\n", header_len_bytes); 1082 return 0; 1083 } 1084 1085 /* Cache the dmc header info. */ 1086 if (mmio_count > mmio_count_max) { 1087 drm_err(display->drm, "DMC firmware has wrong mmio count %u\n", mmio_count); 1088 return 0; 1089 } 1090 1091 if (!dmc_mmio_addr_sanity_check(dmc, mmioaddr, mmio_count, 1092 dmc_header->header_ver, dmc_id)) { 1093 drm_err(display->drm, "DMC firmware has Wrong MMIO Addresses\n"); 1094 return 0; 1095 } 1096 1097 drm_dbg_kms(display->drm, "DMC %d:\n", dmc_id); 1098 for (i = 0; i < mmio_count; i++) { 1099 dmc_info->mmioaddr[i] = _MMIO(mmioaddr[i]); 1100 dmc_info->mmiodata[i] = mmiodata[i]; 1101 } 1102 1103 for (i = 0; i < mmio_count - 1; i++) { 1104 u32 orig_mmiodata[2] = { 1105 dmc_info->mmiodata[i], 1106 dmc_info->mmiodata[i+1], 1107 }; 1108 1109 if (!fixup_dmc_evt(display, dmc_id, 1110 dmc_info->mmioaddr[i], &dmc_info->mmiodata[i], 1111 dmc_info->mmioaddr[i+1], &dmc_info->mmiodata[i+1])) 1112 continue; 1113 1114 drm_dbg_kms(display->drm, 1115 " mmio[%d]: 0x%x = 0x%x->0x%x (EVT_CTL)\n", 1116 i, i915_mmio_reg_offset(dmc_info->mmioaddr[i]), 1117 orig_mmiodata[0], dmc_info->mmiodata[i]); 1118 drm_dbg_kms(display->drm, 1119 " mmio[%d]: 0x%x = 0x%x->0x%x (EVT_HTP)\n", 1120 i+1, i915_mmio_reg_offset(dmc_info->mmioaddr[i+1]), 1121 orig_mmiodata[1], dmc_info->mmiodata[i+1]); 1122 } 1123 1124 for (i = 0; i < mmio_count; i++) { 1125 drm_dbg_kms(display->drm, " mmio[%d]: 0x%x = 0x%x%s%s\n", 1126 i, i915_mmio_reg_offset(dmc_info->mmioaddr[i]), dmc_info->mmiodata[i], 1127 is_dmc_evt_ctl_reg(display, dmc_id, dmc_info->mmioaddr[i]) ? " (EVT_CTL)" : 1128 is_dmc_evt_htp_reg(display, dmc_id, dmc_info->mmioaddr[i]) ? " (EVT_HTP)" : "", 1129 disable_dmc_evt(display, dmc_id, dmc_info->mmioaddr[i], 1130 dmc_info->mmiodata[i]) ? " (disabling)" : ""); 1131 } 1132 dmc_info->mmio_count = mmio_count; 1133 dmc_info->start_mmioaddr = start_mmioaddr; 1134 1135 rem_size -= header_len_bytes; 1136 1137 /* fw_size is in dwords, so multiplied by 4 to convert into bytes. */ 1138 payload_size = dmc_header->fw_size * 4; 1139 if (rem_size < payload_size) 1140 goto error_truncated; 1141 1142 if (payload_size > dmc->max_fw_size) { 1143 drm_err(display->drm, "DMC FW too big (%u bytes)\n", payload_size); 1144 return 0; 1145 } 1146 dmc_info->dmc_fw_size = dmc_header->fw_size; 1147 1148 dmc_info->payload = kmalloc(payload_size, GFP_KERNEL); 1149 if (!dmc_info->payload) 1150 return 0; 1151 1152 payload = (u8 *)(dmc_header) + header_len_bytes; 1153 memcpy(dmc_info->payload, payload, payload_size); 1154 1155 return header_len_bytes + payload_size; 1156 1157 error_truncated: 1158 drm_err(display->drm, "Truncated DMC firmware, refusing.\n"); 1159 return 0; 1160 } 1161 1162 static u32 1163 parse_dmc_fw_package(struct intel_dmc *dmc, 1164 const struct intel_package_header *package_header, 1165 const struct stepping_info *si, 1166 size_t rem_size) 1167 { 1168 struct intel_display *display = dmc->display; 1169 u32 package_size = sizeof(struct intel_package_header); 1170 u32 num_entries, max_entries; 1171 const struct intel_fw_info *fw_info; 1172 1173 if (rem_size < package_size) 1174 goto error_truncated; 1175 1176 if (package_header->header_ver == 1) { 1177 max_entries = PACKAGE_MAX_FW_INFO_ENTRIES; 1178 } else if (package_header->header_ver == 2) { 1179 max_entries = PACKAGE_V2_MAX_FW_INFO_ENTRIES; 1180 } else { 1181 drm_err(display->drm, "DMC firmware has unknown header version %u\n", 1182 package_header->header_ver); 1183 return 0; 1184 } 1185 1186 /* 1187 * We should always have space for max_entries, 1188 * even if not all are used 1189 */ 1190 package_size += max_entries * sizeof(struct intel_fw_info); 1191 if (rem_size < package_size) 1192 goto error_truncated; 1193 1194 if (package_header->header_len * 4 != package_size) { 1195 drm_err(display->drm, "DMC firmware has wrong package header length " 1196 "(%u bytes)\n", package_size); 1197 return 0; 1198 } 1199 1200 num_entries = package_header->num_entries; 1201 if (WARN_ON(package_header->num_entries > max_entries)) 1202 num_entries = max_entries; 1203 1204 fw_info = (const struct intel_fw_info *) 1205 ((u8 *)package_header + sizeof(*package_header)); 1206 dmc_set_fw_offset(dmc, fw_info, num_entries, si, 1207 package_header->header_ver); 1208 1209 /* dmc_offset is in dwords */ 1210 return package_size; 1211 1212 error_truncated: 1213 drm_err(display->drm, "Truncated DMC firmware, refusing.\n"); 1214 return 0; 1215 } 1216 1217 /* Return number of bytes parsed or 0 on error */ 1218 static u32 parse_dmc_fw_css(struct intel_dmc *dmc, 1219 struct intel_css_header *css_header, 1220 size_t rem_size) 1221 { 1222 struct intel_display *display = dmc->display; 1223 1224 if (rem_size < sizeof(struct intel_css_header)) { 1225 drm_err(display->drm, "Truncated DMC firmware, refusing.\n"); 1226 return 0; 1227 } 1228 1229 if (sizeof(struct intel_css_header) != 1230 (css_header->header_len * 4)) { 1231 drm_err(display->drm, "DMC firmware has wrong CSS header length " 1232 "(%u bytes)\n", 1233 (css_header->header_len * 4)); 1234 return 0; 1235 } 1236 1237 dmc->version = css_header->version; 1238 1239 return sizeof(struct intel_css_header); 1240 } 1241 1242 static int parse_dmc_fw(struct intel_dmc *dmc, const struct firmware *fw) 1243 { 1244 struct intel_display *display = dmc->display; 1245 struct intel_css_header *css_header; 1246 struct intel_package_header *package_header; 1247 struct intel_dmc_header_base *dmc_header; 1248 struct stepping_info display_info = { '*', '*'}; 1249 const struct stepping_info *si = intel_get_stepping_info(display, &display_info); 1250 enum intel_dmc_id dmc_id; 1251 u32 readcount = 0; 1252 u32 r, offset; 1253 1254 if (!fw) 1255 return -EINVAL; 1256 1257 /* Extract CSS Header information */ 1258 css_header = (struct intel_css_header *)fw->data; 1259 r = parse_dmc_fw_css(dmc, css_header, fw->size); 1260 if (!r) 1261 return -EINVAL; 1262 1263 readcount += r; 1264 1265 /* Extract Package Header information */ 1266 package_header = (struct intel_package_header *)&fw->data[readcount]; 1267 r = parse_dmc_fw_package(dmc, package_header, si, fw->size - readcount); 1268 if (!r) 1269 return -EINVAL; 1270 1271 readcount += r; 1272 1273 for_each_dmc_id(dmc_id) { 1274 if (!dmc->dmc_info[dmc_id].present) 1275 continue; 1276 1277 offset = readcount + dmc->dmc_info[dmc_id].dmc_offset * 4; 1278 if (offset > fw->size) { 1279 drm_err(display->drm, "Reading beyond the fw_size\n"); 1280 continue; 1281 } 1282 1283 dmc_header = (struct intel_dmc_header_base *)&fw->data[offset]; 1284 parse_dmc_fw_header(dmc, dmc_header, fw->size - offset, dmc_id); 1285 } 1286 1287 if (!intel_dmc_has_payload(display)) { 1288 drm_err(display->drm, "DMC firmware main program not found\n"); 1289 return -ENOENT; 1290 } 1291 1292 return 0; 1293 } 1294 1295 static void intel_dmc_runtime_pm_get(struct intel_display *display) 1296 { 1297 drm_WARN_ON(display->drm, display->dmc.wakeref); 1298 display->dmc.wakeref = intel_display_power_get(display, POWER_DOMAIN_INIT); 1299 } 1300 1301 static void intel_dmc_runtime_pm_put(struct intel_display *display) 1302 { 1303 intel_wakeref_t wakeref __maybe_unused = 1304 fetch_and_zero(&display->dmc.wakeref); 1305 1306 intel_display_power_put(display, POWER_DOMAIN_INIT, wakeref); 1307 } 1308 1309 static const char *dmc_fallback_path(struct intel_display *display) 1310 { 1311 if (display->platform.alderlake_p) 1312 return ADLP_DMC_FALLBACK_PATH; 1313 1314 return NULL; 1315 } 1316 1317 static void dmc_load_work_fn(struct work_struct *work) 1318 { 1319 struct intel_dmc *dmc = container_of(work, typeof(*dmc), work); 1320 struct intel_display *display = dmc->display; 1321 const struct firmware *fw = NULL; 1322 const char *fallback_path; 1323 int err; 1324 1325 err = request_firmware(&fw, dmc->fw_path, display->drm->dev); 1326 1327 if (err == -ENOENT && !dmc_firmware_param(display)) { 1328 fallback_path = dmc_fallback_path(display); 1329 if (fallback_path) { 1330 drm_dbg_kms(display->drm, "%s not found, falling back to %s\n", 1331 dmc->fw_path, fallback_path); 1332 err = request_firmware(&fw, fallback_path, display->drm->dev); 1333 if (err == 0) 1334 dmc->fw_path = fallback_path; 1335 } 1336 } 1337 1338 if (err) { 1339 drm_notice(display->drm, 1340 "Failed to load DMC firmware %s (%pe). Disabling runtime power management.\n", 1341 dmc->fw_path, ERR_PTR(err)); 1342 drm_notice(display->drm, "DMC firmware homepage: %s", 1343 INTEL_DMC_FIRMWARE_URL); 1344 return; 1345 } 1346 1347 err = parse_dmc_fw(dmc, fw); 1348 if (err) { 1349 drm_notice(display->drm, 1350 "Failed to parse DMC firmware %s (%pe). Disabling runtime power management.\n", 1351 dmc->fw_path, ERR_PTR(err)); 1352 goto out; 1353 } 1354 1355 intel_dmc_load_program(display); 1356 intel_dmc_runtime_pm_put(display); 1357 1358 drm_info(display->drm, "Finished loading DMC firmware %s (v%u.%u)\n", 1359 dmc->fw_path, DMC_VERSION_MAJOR(dmc->version), 1360 DMC_VERSION_MINOR(dmc->version)); 1361 1362 out: 1363 release_firmware(fw); 1364 } 1365 1366 /** 1367 * intel_dmc_init() - initialize the firmware loading. 1368 * @display: display instance 1369 * 1370 * This function is called at the time of loading the display driver to read 1371 * firmware from a .bin file and copied into a internal memory. 1372 */ 1373 void intel_dmc_init(struct intel_display *display) 1374 { 1375 struct intel_dmc *dmc; 1376 1377 if (!HAS_DMC(display)) 1378 return; 1379 1380 /* 1381 * Obtain a runtime pm reference, until DMC is loaded, to avoid entering 1382 * runtime-suspend. 1383 * 1384 * On error, we return with the rpm wakeref held to prevent runtime 1385 * suspend as runtime suspend *requires* a working DMC for whatever 1386 * reason. 1387 */ 1388 intel_dmc_runtime_pm_get(display); 1389 1390 dmc = kzalloc(sizeof(*dmc), GFP_KERNEL); 1391 if (!dmc) 1392 return; 1393 1394 dmc->display = display; 1395 1396 INIT_WORK(&dmc->work, dmc_load_work_fn); 1397 1398 dmc->fw_path = dmc_firmware_default(display, &dmc->max_fw_size); 1399 1400 if (dmc_firmware_param_disabled(display)) { 1401 drm_info(display->drm, "Disabling DMC firmware and runtime PM\n"); 1402 goto out; 1403 } 1404 1405 if (dmc_firmware_param(display)) 1406 dmc->fw_path = dmc_firmware_param(display); 1407 1408 if (!dmc->fw_path) { 1409 drm_dbg_kms(display->drm, 1410 "No known DMC firmware for platform, disabling runtime PM\n"); 1411 goto out; 1412 } 1413 1414 display->dmc.dmc = dmc; 1415 1416 drm_dbg_kms(display->drm, "Loading %s\n", dmc->fw_path); 1417 queue_work(display->wq.unordered, &dmc->work); 1418 1419 return; 1420 1421 out: 1422 kfree(dmc); 1423 } 1424 1425 /** 1426 * intel_dmc_suspend() - prepare DMC firmware before system suspend 1427 * @display: display instance 1428 * 1429 * Prepare the DMC firmware before entering system suspend. This includes 1430 * flushing pending work items and releasing any resources acquired during 1431 * init. 1432 */ 1433 void intel_dmc_suspend(struct intel_display *display) 1434 { 1435 struct intel_dmc *dmc = display_to_dmc(display); 1436 1437 if (!HAS_DMC(display)) 1438 return; 1439 1440 if (dmc) 1441 flush_work(&dmc->work); 1442 1443 /* Drop the reference held in case DMC isn't loaded. */ 1444 if (!intel_dmc_has_payload(display)) 1445 intel_dmc_runtime_pm_put(display); 1446 } 1447 1448 void intel_dmc_wait_fw_load(struct intel_display *display) 1449 { 1450 struct intel_dmc *dmc = display_to_dmc(display); 1451 1452 if (!HAS_DMC(display)) 1453 return; 1454 1455 if (dmc) 1456 flush_work(&dmc->work); 1457 } 1458 1459 /** 1460 * intel_dmc_resume() - init DMC firmware during system resume 1461 * @display: display instance 1462 * 1463 * Reinitialize the DMC firmware during system resume, reacquiring any 1464 * resources released in intel_dmc_suspend(). 1465 */ 1466 void intel_dmc_resume(struct intel_display *display) 1467 { 1468 if (!HAS_DMC(display)) 1469 return; 1470 1471 /* 1472 * Reacquire the reference to keep RPM disabled in case DMC isn't 1473 * loaded. 1474 */ 1475 if (!intel_dmc_has_payload(display)) 1476 intel_dmc_runtime_pm_get(display); 1477 } 1478 1479 /** 1480 * intel_dmc_fini() - unload the DMC firmware. 1481 * @display: display instance 1482 * 1483 * Firmmware unloading includes freeing the internal memory and reset the 1484 * firmware loading status. 1485 */ 1486 void intel_dmc_fini(struct intel_display *display) 1487 { 1488 struct intel_dmc *dmc = display_to_dmc(display); 1489 enum intel_dmc_id dmc_id; 1490 1491 if (!HAS_DMC(display)) 1492 return; 1493 1494 intel_dmc_suspend(display); 1495 drm_WARN_ON(display->drm, display->dmc.wakeref); 1496 1497 if (dmc) { 1498 for_each_dmc_id(dmc_id) 1499 kfree(dmc->dmc_info[dmc_id].payload); 1500 1501 kfree(dmc); 1502 display->dmc.dmc = NULL; 1503 } 1504 } 1505 1506 struct intel_dmc_snapshot { 1507 bool initialized; 1508 bool loaded; 1509 u32 version; 1510 }; 1511 1512 struct intel_dmc_snapshot *intel_dmc_snapshot_capture(struct intel_display *display) 1513 { 1514 struct intel_dmc *dmc = display_to_dmc(display); 1515 struct intel_dmc_snapshot *snapshot; 1516 1517 if (!HAS_DMC(display)) 1518 return NULL; 1519 1520 snapshot = kzalloc(sizeof(*snapshot), GFP_ATOMIC); 1521 if (!snapshot) 1522 return NULL; 1523 1524 snapshot->initialized = dmc; 1525 snapshot->loaded = intel_dmc_has_payload(display); 1526 if (dmc) 1527 snapshot->version = dmc->version; 1528 1529 return snapshot; 1530 } 1531 1532 void intel_dmc_snapshot_print(const struct intel_dmc_snapshot *snapshot, struct drm_printer *p) 1533 { 1534 if (!snapshot) 1535 return; 1536 1537 drm_printf(p, "DMC initialized: %s\n", str_yes_no(snapshot->initialized)); 1538 drm_printf(p, "DMC loaded: %s\n", str_yes_no(snapshot->loaded)); 1539 if (snapshot->initialized) 1540 drm_printf(p, "DMC fw version: %d.%d\n", 1541 DMC_VERSION_MAJOR(snapshot->version), 1542 DMC_VERSION_MINOR(snapshot->version)); 1543 } 1544 1545 void intel_dmc_update_dc6_allowed_count(struct intel_display *display, 1546 bool start_tracking) 1547 { 1548 struct intel_dmc *dmc = display_to_dmc(display); 1549 u32 dc5_cur_count; 1550 1551 if (DISPLAY_VER(dmc->display) < 14) 1552 return; 1553 1554 dc5_cur_count = intel_de_read(dmc->display, DG1_DMC_DEBUG_DC5_COUNT); 1555 1556 if (!start_tracking) 1557 dmc->dc6_allowed.count += dc5_cur_count - dmc->dc6_allowed.dc5_start; 1558 1559 dmc->dc6_allowed.dc5_start = dc5_cur_count; 1560 } 1561 1562 static bool intel_dmc_get_dc6_allowed_count(struct intel_display *display, u32 *count) 1563 { 1564 struct i915_power_domains *power_domains = &display->power.domains; 1565 struct intel_dmc *dmc = display_to_dmc(display); 1566 bool dc6_enabled; 1567 1568 if (DISPLAY_VER(display) < 14) 1569 return false; 1570 1571 mutex_lock(&power_domains->lock); 1572 dc6_enabled = intel_de_read(display, DC_STATE_EN) & 1573 DC_STATE_EN_UPTO_DC6; 1574 if (dc6_enabled) 1575 intel_dmc_update_dc6_allowed_count(display, false); 1576 1577 *count = dmc->dc6_allowed.count; 1578 mutex_unlock(&power_domains->lock); 1579 1580 return true; 1581 } 1582 1583 static int intel_dmc_debugfs_status_show(struct seq_file *m, void *unused) 1584 { 1585 struct intel_display *display = m->private; 1586 struct intel_dmc *dmc = display_to_dmc(display); 1587 struct ref_tracker *wakeref; 1588 i915_reg_t dc5_reg, dc6_reg = INVALID_MMIO_REG; 1589 u32 dc6_allowed_count; 1590 1591 if (!HAS_DMC(display)) 1592 return -ENODEV; 1593 1594 wakeref = intel_display_rpm_get(display); 1595 1596 seq_printf(m, "DMC initialized: %s\n", str_yes_no(dmc)); 1597 seq_printf(m, "fw loaded: %s\n", 1598 str_yes_no(intel_dmc_has_payload(display))); 1599 seq_printf(m, "path: %s\n", dmc ? dmc->fw_path : "N/A"); 1600 seq_printf(m, "Pipe A fw needed: %s\n", 1601 str_yes_no(DISPLAY_VER(display) >= 12)); 1602 seq_printf(m, "Pipe A fw loaded: %s\n", 1603 str_yes_no(has_dmc_id_fw(display, DMC_FW_PIPEA))); 1604 seq_printf(m, "Pipe B fw needed: %s\n", 1605 str_yes_no(display->platform.alderlake_p || 1606 DISPLAY_VER(display) >= 14)); 1607 seq_printf(m, "Pipe B fw loaded: %s\n", 1608 str_yes_no(has_dmc_id_fw(display, DMC_FW_PIPEB))); 1609 1610 if (!intel_dmc_has_payload(display)) 1611 goto out; 1612 1613 seq_printf(m, "version: %d.%d\n", DMC_VERSION_MAJOR(dmc->version), 1614 DMC_VERSION_MINOR(dmc->version)); 1615 1616 if (DISPLAY_VER(display) >= 12) { 1617 i915_reg_t dc3co_reg; 1618 1619 if (display->platform.dgfx || DISPLAY_VER(display) >= 14) { 1620 dc3co_reg = DG1_DMC_DEBUG3; 1621 dc5_reg = DG1_DMC_DEBUG_DC5_COUNT; 1622 } else { 1623 dc3co_reg = TGL_DMC_DEBUG3; 1624 dc5_reg = TGL_DMC_DEBUG_DC5_COUNT; 1625 dc6_reg = TGL_DMC_DEBUG_DC6_COUNT; 1626 } 1627 1628 seq_printf(m, "DC3CO count: %d\n", 1629 intel_de_read(display, dc3co_reg)); 1630 } else { 1631 dc5_reg = display->platform.broxton ? BXT_DMC_DC3_DC5_COUNT : 1632 SKL_DMC_DC3_DC5_COUNT; 1633 if (!display->platform.geminilake && !display->platform.broxton) 1634 dc6_reg = SKL_DMC_DC5_DC6_COUNT; 1635 } 1636 1637 seq_printf(m, "DC3 -> DC5 count: %d\n", intel_de_read(display, dc5_reg)); 1638 1639 if (intel_dmc_get_dc6_allowed_count(display, &dc6_allowed_count)) 1640 seq_printf(m, "DC5 -> DC6 allowed count: %d\n", 1641 dc6_allowed_count); 1642 else if (i915_mmio_reg_valid(dc6_reg)) 1643 seq_printf(m, "DC5 -> DC6 count: %d\n", 1644 intel_de_read(display, dc6_reg)); 1645 1646 seq_printf(m, "program base: 0x%08x\n", 1647 intel_de_read(display, DMC_PROGRAM(dmc->dmc_info[DMC_FW_MAIN].start_mmioaddr, 0))); 1648 1649 out: 1650 seq_printf(m, "ssp base: 0x%08x\n", 1651 intel_de_read(display, DMC_SSP_BASE)); 1652 seq_printf(m, "htp: 0x%08x\n", intel_de_read(display, DMC_HTP_SKL)); 1653 1654 intel_display_rpm_put(display, wakeref); 1655 1656 return 0; 1657 } 1658 1659 DEFINE_SHOW_ATTRIBUTE(intel_dmc_debugfs_status); 1660 1661 void intel_dmc_debugfs_register(struct intel_display *display) 1662 { 1663 debugfs_create_file("i915_dmc_info", 0444, display->drm->debugfs_root, 1664 display, &intel_dmc_debugfs_status_fops); 1665 } 1666 1667 void intel_pipedmc_irq_handler(struct intel_display *display, enum pipe pipe) 1668 { 1669 struct intel_crtc *crtc = intel_crtc_for_pipe(display, pipe); 1670 u32 tmp = 0, int_vector; 1671 1672 if (DISPLAY_VER(display) >= 20) { 1673 tmp = intel_de_read(display, PIPEDMC_INTERRUPT(pipe)); 1674 intel_de_write(display, PIPEDMC_INTERRUPT(pipe), tmp); 1675 1676 if (tmp & PIPEDMC_FLIPQ_PROG_DONE) { 1677 spin_lock(&display->drm->event_lock); 1678 1679 if (crtc->flipq_event) { 1680 /* 1681 * Update vblank counter/timestamp in case it 1682 * hasn't been done yet for this frame. 1683 */ 1684 drm_crtc_accurate_vblank_count(&crtc->base); 1685 1686 drm_crtc_send_vblank_event(&crtc->base, crtc->flipq_event); 1687 crtc->flipq_event = NULL; 1688 } 1689 1690 spin_unlock(&display->drm->event_lock); 1691 } 1692 1693 if (tmp & PIPEDMC_ATS_FAULT) 1694 drm_err_ratelimited(display->drm, "[CRTC:%d:%s] PIPEDMC ATS fault\n", 1695 crtc->base.base.id, crtc->base.name); 1696 if (tmp & PIPEDMC_GTT_FAULT) 1697 drm_err_ratelimited(display->drm, "[CRTC:%d:%s] PIPEDMC GTT fault\n", 1698 crtc->base.base.id, crtc->base.name); 1699 if (tmp & PIPEDMC_ERROR) 1700 drm_err(display->drm, "[CRTC:%d:%s]] PIPEDMC error\n", 1701 crtc->base.base.id, crtc->base.name); 1702 } 1703 1704 int_vector = intel_de_read(display, PIPEDMC_STATUS(pipe)) & PIPEDMC_INT_VECTOR_MASK; 1705 if (tmp == 0 && int_vector != 0) 1706 drm_err(display->drm, "[CRTC:%d:%s]] PIPEDMC interrupt vector 0x%x\n", 1707 crtc->base.base.id, crtc->base.name, tmp); 1708 } 1709 1710 void intel_pipedmc_enable_event(struct intel_crtc *crtc, 1711 enum pipedmc_event_id event) 1712 { 1713 struct intel_display *display = to_intel_display(crtc); 1714 enum intel_dmc_id dmc_id = PIPE_TO_DMC_ID(crtc->pipe); 1715 1716 dmc_configure_event(display, dmc_id, event, true); 1717 } 1718 1719 void intel_pipedmc_disable_event(struct intel_crtc *crtc, 1720 enum pipedmc_event_id event) 1721 { 1722 struct intel_display *display = to_intel_display(crtc); 1723 enum intel_dmc_id dmc_id = PIPE_TO_DMC_ID(crtc->pipe); 1724 1725 dmc_configure_event(display, dmc_id, event, false); 1726 } 1727 1728 u32 intel_pipedmc_start_mmioaddr(struct intel_crtc *crtc) 1729 { 1730 struct intel_display *display = to_intel_display(crtc); 1731 struct intel_dmc *dmc = display_to_dmc(display); 1732 enum intel_dmc_id dmc_id = PIPE_TO_DMC_ID(crtc->pipe); 1733 1734 return dmc ? dmc->dmc_info[dmc_id].start_mmioaddr : 0; 1735 } 1736