1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright 2015-2026 Advanced Micro Devices, Inc. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be included in 13 * all copies or substantial portions of the 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 COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 21 * OTHER DEALINGS IN THE SOFTWARE. 22 * 23 * Authors: AMD 24 * 25 */ 26 27 /* The caprices of the preprocessor require that this be declared right here */ 28 #define CREATE_TRACE_POINTS 29 30 #include "dm_services_types.h" 31 #include "dc.h" 32 #include "link_enc_cfg.h" 33 #include "dc/inc/core_types.h" 34 #include "dal_asic_id.h" 35 #include "dmub/dmub_srv.h" 36 #include "dc/inc/hw/dmcu.h" 37 #include "dc/inc/hw/abm.h" 38 #include "dc/dc_dmub_srv.h" 39 #include "dc/dc_stat.h" 40 #include "dc/dc_state.h" 41 #include "amdgpu_dm_trace.h" 42 #include "link/protocols/link_dpcd.h" 43 #include "link_service_types.h" 44 #include "link/protocols/link_dp_capability.h" 45 #include "link/protocols/link_ddc.h" 46 47 #include "amdgpu.h" 48 #include "amdgpu_display.h" 49 #include "amdgpu_ucode.h" 50 #include "atom.h" 51 #include "amdgpu_dm.h" 52 #include "amdgpu_dm_plane.h" 53 #include "amdgpu_dm_crtc.h" 54 #include "amdgpu_dm_hdcp.h" 55 #include <drm/display/drm_hdcp_helper.h> 56 #include "amdgpu_dm_wb.h" 57 #include "amdgpu_atombios.h" 58 59 #include "amd_shared.h" 60 #include "amdgpu_dm_irq.h" 61 #include "dm_helpers.h" 62 #include "amdgpu_dm_mst_types.h" 63 #if defined(CONFIG_DEBUG_FS) 64 #include "amdgpu_dm_debugfs.h" 65 #endif 66 #include "amdgpu_dm_psr.h" 67 #include "amdgpu_dm_replay.h" 68 #include "amdgpu_dm_backlight.h" 69 #include "amdgpu_dm_audio.h" 70 #include "amdgpu_dm_dmub.h" 71 #include "amdgpu_dm_connector.h" 72 #include "amdgpu_dm_pp_smu.h" 73 #include "amdgpu_dm_freesync.h" 74 #include "amdgpu_dm_cursor.h" 75 76 #include "ivsrcid/ivsrcid_vislands30.h" 77 78 #include <linux/backlight.h> 79 #include <linux/module.h> 80 #include <linux/moduleparam.h> 81 #include <linux/types.h> 82 #include <linux/pm_runtime.h> 83 #include <linux/pci.h> 84 #include <linux/power_supply.h> 85 #include <linux/firmware.h> 86 #include <linux/component.h> 87 #include <linux/sort.h> 88 89 #include <drm/drm_privacy_screen_consumer.h> 90 #include <drm/display/drm_dp_mst_helper.h> 91 #include <drm/display/drm_hdmi_helper.h> 92 #include <drm/drm_atomic.h> 93 #include <drm/drm_atomic_uapi.h> 94 #include <drm/drm_atomic_helper.h> 95 #include <drm/drm_blend.h> 96 #include <drm/drm_fixed.h> 97 #include <drm/drm_fourcc.h> 98 #include <drm/drm_edid.h> 99 #include <drm/drm_eld.h> 100 #include <drm/drm_mode.h> 101 #include <drm/drm_vblank.h> 102 #include <drm/drm_colorop.h> 103 #include <drm/drm_gem_atomic_helper.h> 104 105 #include <media/cec-notifier.h> 106 #include <acpi/video.h> 107 108 #include "ivsrcid/dcn/irqsrcs_dcn_1_0.h" 109 110 #include "modules/inc/mod_freesync.h" 111 #include "modules/inc/mod_power.h" 112 #include "modules/power/power_helpers.h" 113 114 MODULE_FIRMWARE(FIRMWARE_RAVEN_DMCU); 115 MODULE_FIRMWARE(FIRMWARE_NAVI12_DMCU); 116 117 /** 118 * DOC: overview 119 * 120 * The AMDgpu display manager, **amdgpu_dm** (or even simpler, 121 * **dm**) sits between DRM and DC. It acts as a liaison, converting DRM 122 * requests into DC requests, and DC responses into DRM responses. 123 * 124 * The root control structure is &struct amdgpu_display_manager. 125 */ 126 127 /* basic init/fini API */ 128 static int amdgpu_dm_init(struct amdgpu_device *adev); 129 static void amdgpu_dm_fini(struct amdgpu_device *adev); 130 131 /* 132 * initializes drm_device display related structures, based on the information 133 * provided by DAL. The drm strcutures are: drm_crtc, drm_connector, 134 * drm_encoder, drm_mode_config 135 * 136 * Returns 0 on success 137 */ 138 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev); 139 /* removes and deallocates the drm structures, created by the above function */ 140 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm); 141 142 STATIC_IFN_KUNIT int amdgpu_dm_atomic_setup_commit(struct drm_atomic_commit *state); 143 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_commit *state); 144 STATIC_IFN_KUNIT void dm_enable_per_frame_crtc_master_sync(struct dc_state *context); 145 146 #if !IS_ENABLED(CONFIG_DRM_AMD_DC_KUNIT_TEST) 147 static int amdgpu_dm_atomic_check(struct drm_device *dev, 148 struct drm_atomic_commit *state); 149 #endif 150 151 static inline void amdgpu_dm_exit_ips_for_hw_access(struct dc *dc) 152 { 153 if (dc->ctx->dmub_srv && !dc->ctx->dmub_srv->idle_exit_counter) 154 dc_exit_ips_for_hw_access(dc); 155 } 156 157 /* 158 * dm_vblank_get_counter 159 * 160 * @brief 161 * Get counter for number of vertical blanks 162 * 163 * @param 164 * struct amdgpu_device *adev - [in] desired amdgpu device 165 * int disp_idx - [in] which CRTC to get the counter from 166 * 167 * @return 168 * Counter for vertical blanks 169 */ 170 STATIC_IFN_KUNIT u32 dm_vblank_get_counter(struct amdgpu_device *adev, int crtc) 171 { 172 struct amdgpu_crtc *acrtc = NULL; 173 174 if (crtc >= adev->mode_info.num_crtc) 175 return 0; 176 177 acrtc = adev->mode_info.crtcs[crtc]; 178 179 if (!acrtc->dm_irq_params.stream) { 180 drm_err(adev_to_drm(adev), "dc_stream_state is NULL for crtc '%d'!\n", 181 crtc); 182 return 0; 183 } 184 185 return dc_stream_get_vblank_counter(acrtc->dm_irq_params.stream); 186 } 187 EXPORT_IF_KUNIT(dm_vblank_get_counter); 188 189 STATIC_IFN_KUNIT int dm_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc, 190 u32 *vbl, u32 *position) 191 { 192 u32 v_blank_start = 0, v_blank_end = 0, h_position = 0, v_position = 0; 193 struct amdgpu_crtc *acrtc = NULL; 194 struct dc *dc = adev->dm.dc; 195 196 if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc)) 197 return -EINVAL; 198 199 acrtc = adev->mode_info.crtcs[crtc]; 200 201 if (!acrtc->dm_irq_params.stream) { 202 drm_err(adev_to_drm(adev), "dc_stream_state is NULL for crtc '%d'!\n", 203 crtc); 204 return 0; 205 } 206 207 if (dc && dc->caps.ips_support && dc->idle_optimizations_allowed) 208 dc_allow_idle_optimizations(dc, false); 209 210 /* 211 * TODO rework base driver to use values directly. 212 * for now parse it back into reg-format 213 */ 214 dc_stream_get_scanoutpos(acrtc->dm_irq_params.stream, 215 &v_blank_start, 216 &v_blank_end, 217 &h_position, 218 &v_position); 219 220 *position = v_position | (h_position << 16); 221 *vbl = v_blank_start | (v_blank_end << 16); 222 223 return 0; 224 } 225 EXPORT_IF_KUNIT(dm_crtc_get_scanoutpos); 226 227 STATIC_IFN_KUNIT int dm_wait_for_idle(struct amdgpu_ip_block *ip_block) 228 { 229 /* XXX todo */ 230 return 0; 231 } 232 EXPORT_IF_KUNIT(dm_wait_for_idle); 233 234 /* 235 * DC will program planes with their z-order determined by their ordering 236 * in the dc_surface_updates array. This comparator is used to sort them 237 * by descending zpos. 238 */ 239 STATIC_IFN_KUNIT int dm_plane_layer_index_cmp(const void *a, const void *b) 240 { 241 const struct dc_surface_update *sa = (struct dc_surface_update *)a; 242 const struct dc_surface_update *sb = (struct dc_surface_update *)b; 243 244 /* Sort by descending dc_plane layer_index (i.e. normalized_zpos) */ 245 return sb->surface->layer_index - sa->surface->layer_index; 246 } 247 EXPORT_IF_KUNIT(dm_plane_layer_index_cmp); 248 249 #if IS_ENABLED(CONFIG_DRM_AMD_DC_KUNIT_TEST) 250 static const struct amdgpu_dm_kunit_ops *amdgpu_dm_ops; 251 252 #define dm_post_update_surfaces_to_stream amdgpu_dm_ops->post_update_surfaces_to_stream 253 #define dm_update_planes_and_stream amdgpu_dm_ops->update_planes_and_stream 254 #else 255 #define dm_post_update_surfaces_to_stream dc_post_update_surfaces_to_stream 256 #define dm_update_planes_and_stream dc_update_planes_and_stream 257 #endif 258 259 /** 260 * update_planes_and_stream_adapter() - Send planes to be updated in DC 261 * 262 * DC has a generic way to update planes and stream via 263 * dc_update_planes_and_stream function; however, DM might need some 264 * adjustments and preparation before calling it. This function is a wrapper 265 * for the dc_update_planes_and_stream that does any required configuration 266 * before passing control to DC. 267 * 268 * @dc: Display Core control structure 269 * @planes_count: planes count to update 270 * @stream: stream state 271 * @stream_update: stream update 272 * @array_of_surface_update: dc surface update pointer 273 * 274 */ 275 STATIC_IFN_KUNIT INLINE_IFN_KUNIT 276 bool update_planes_and_stream_adapter(struct dc *dc, 277 int planes_count, 278 struct dc_stream_state *stream, 279 struct dc_stream_update *stream_update, 280 struct dc_surface_update *array_of_surface_update) 281 { 282 sort(array_of_surface_update, planes_count, 283 sizeof(*array_of_surface_update), dm_plane_layer_index_cmp, NULL); 284 285 /* 286 * Previous frame finished and HW is ready for optimization. 287 */ 288 dm_post_update_surfaces_to_stream(dc); 289 290 return dm_update_planes_and_stream(dc, 291 array_of_surface_update, 292 planes_count, 293 stream, 294 stream_update); 295 } 296 EXPORT_IF_KUNIT(update_planes_and_stream_adapter); 297 298 STATIC_IFN_KUNIT int dm_set_clockgating_state(struct amdgpu_ip_block *ip_block, 299 enum amd_clockgating_state state) 300 { 301 return 0; 302 } 303 EXPORT_IF_KUNIT(dm_set_clockgating_state); 304 305 STATIC_IFN_KUNIT int dm_set_powergating_state(struct amdgpu_ip_block *ip_block, 306 enum amd_powergating_state state) 307 { 308 return 0; 309 } 310 EXPORT_IF_KUNIT(dm_set_powergating_state); 311 312 /* Prototypes of private functions */ 313 STATIC_IFN_KUNIT int dm_early_init(struct amdgpu_ip_block *ip_block); 314 315 #if IS_ENABLED(CONFIG_DRM_AMD_DC_KUNIT_TEST) 316 static const struct amdgpu_dm_kunit_ops amdgpu_dm_default_ops = { 317 .gmc_pd_addr = amdgpu_gmc_pd_addr, 318 .post_update_surfaces_to_stream = dc_post_update_surfaces_to_stream, 319 .update_planes_and_stream = dc_update_planes_and_stream, 320 .atomic_helper_suspend = drm_atomic_helper_suspend, 321 }; 322 323 static const struct amdgpu_dm_kunit_ops *amdgpu_dm_ops = &amdgpu_dm_default_ops; 324 325 void amdgpu_dm_kunit_set_ops(const struct amdgpu_dm_kunit_ops *ops) 326 { 327 amdgpu_dm_ops = ops ? ops : &amdgpu_dm_default_ops; 328 } 329 EXPORT_IF_KUNIT(amdgpu_dm_kunit_set_ops); 330 331 #define dm_gmc_pd_addr amdgpu_dm_ops->gmc_pd_addr 332 #define dm_atomic_helper_suspend amdgpu_dm_ops->atomic_helper_suspend 333 #else 334 #define dm_gmc_pd_addr amdgpu_gmc_pd_addr 335 #define dm_atomic_helper_suspend drm_atomic_helper_suspend 336 #endif 337 338 /* Allocate memory for FBC compressed data */ 339 STATIC_IFN_KUNIT void mmhub_read_system_context(struct amdgpu_device *adev, 340 struct dc_phy_addr_space_config *pa_config) 341 { 342 u64 pt_base; 343 u32 logical_addr_low; 344 u32 logical_addr_high; 345 u32 agp_base, agp_bot, agp_top; 346 PHYSICAL_ADDRESS_LOC page_table_start, page_table_end, page_table_base; 347 348 memset(pa_config, 0, sizeof(*pa_config)); 349 350 agp_base = 0; 351 agp_bot = adev->gmc.agp_start >> 24; 352 agp_top = adev->gmc.agp_end >> 24; 353 354 /* AGP aperture is disabled */ 355 if (agp_bot > agp_top) { 356 logical_addr_low = adev->gmc.fb_start >> 18; 357 if (adev->apu_flags & (AMD_APU_IS_RAVEN2 | 358 AMD_APU_IS_RENOIR | 359 AMD_APU_IS_GREEN_SARDINE)) 360 /* 361 * Raven2 has a HW issue that it is unable to use the vram which 362 * is out of MC_VM_SYSTEM_APERTURE_HIGH_ADDR. So here is the 363 * workaround that increase system aperture high address (add 1) 364 * to get rid of the VM fault and hardware hang. 365 */ 366 logical_addr_high = (adev->gmc.fb_end >> 18) + 0x1; 367 else 368 logical_addr_high = adev->gmc.fb_end >> 18; 369 } else { 370 logical_addr_low = min(adev->gmc.fb_start, adev->gmc.agp_start) >> 18; 371 if (adev->apu_flags & (AMD_APU_IS_RAVEN2 | 372 AMD_APU_IS_RENOIR | 373 AMD_APU_IS_GREEN_SARDINE)) 374 /* 375 * Raven2 has a HW issue that it is unable to use the vram which 376 * is out of MC_VM_SYSTEM_APERTURE_HIGH_ADDR. So here is the 377 * workaround that increase system aperture high address (add 1) 378 * to get rid of the VM fault and hardware hang. 379 */ 380 logical_addr_high = max((adev->gmc.fb_end >> 18) + 0x1, adev->gmc.agp_end >> 18); 381 else 382 logical_addr_high = max(adev->gmc.fb_end, adev->gmc.agp_end) >> 18; 383 } 384 385 pt_base = dm_gmc_pd_addr(adev->gart.bo); 386 387 page_table_start.high_part = upper_32_bits(adev->gmc.gart_start >> 388 AMDGPU_GPU_PAGE_SHIFT); 389 page_table_start.low_part = lower_32_bits(adev->gmc.gart_start >> 390 AMDGPU_GPU_PAGE_SHIFT); 391 page_table_end.high_part = upper_32_bits(adev->gmc.gart_end >> 392 AMDGPU_GPU_PAGE_SHIFT); 393 page_table_end.low_part = lower_32_bits(adev->gmc.gart_end >> 394 AMDGPU_GPU_PAGE_SHIFT); 395 page_table_base.high_part = upper_32_bits(pt_base); 396 page_table_base.low_part = lower_32_bits(pt_base); 397 398 pa_config->system_aperture.start_addr = (uint64_t)logical_addr_low << 18; 399 pa_config->system_aperture.end_addr = (uint64_t)logical_addr_high << 18; 400 401 pa_config->system_aperture.agp_base = (uint64_t)agp_base << 24; 402 pa_config->system_aperture.agp_bot = (uint64_t)agp_bot << 24; 403 pa_config->system_aperture.agp_top = (uint64_t)agp_top << 24; 404 405 pa_config->system_aperture.fb_base = adev->gmc.fb_start; 406 pa_config->system_aperture.fb_offset = adev->vm_manager.vram_base_offset; 407 pa_config->system_aperture.fb_top = adev->gmc.fb_end; 408 409 pa_config->gart_config.page_table_start_addr = page_table_start.quad_part << 12; 410 pa_config->gart_config.page_table_end_addr = page_table_end.quad_part << 12; 411 pa_config->gart_config.page_table_base_addr = page_table_base.quad_part; 412 413 pa_config->is_hvm_enabled = adev->mode_info.gpu_vm_support; 414 415 } 416 EXPORT_IF_KUNIT(mmhub_read_system_context); 417 418 STATIC_IFN_KUNIT int amdgpu_dm_init_power_module(struct amdgpu_display_manager *dm) 419 { 420 struct mod_power_init_params init_data[MAX_NUM_EDP]; 421 422 if (dm->num_of_edps == 0) { 423 drm_dbg_driver( 424 dm->ddev, 425 "amdgpu: No eDP detected, skip initializing power module\n"); 426 return 0; 427 } 428 429 /* Initialize all the power module parameters */ 430 for (int i = 0; i < dm->num_of_edps; i++) { 431 init_data[i].allow_psr_smu_optimizations = 432 !!(amdgpu_dc_feature_mask & DC_PSR_ALLOW_SMU_OPT); 433 init_data[i].allow_psr_multi_disp_optimizations = 434 !!(amdgpu_dc_feature_mask & DC_PSR_ALLOW_MULTI_DISP_OPT); 435 /* See dm_late_init */ 436 init_data[i].backlight_ramping_override = false; 437 init_data[i].backlight_ramping_start = 0xCCCC; 438 init_data[i].backlight_ramping_reduction = 0xCCCCCCCC; 439 init_data[i].def_varibright_level = 0; 440 init_data[i].abm_config_setting = 0; 441 init_data[i].num_backlight_levels = 101; 442 init_data[i].use_nits_based_brightness = false; 443 init_data[i].panel_max_millinits = 0; 444 init_data[i].panel_min_millinits = 0; 445 init_data[i].disable_fractional_pwm = 446 !(amdgpu_dc_feature_mask & DC_DISABLE_FRACTIONAL_PWM_MASK); 447 init_data[i].use_custom_backlight_caps = false; 448 init_data[i].custom_backlight_caps_config_no = 0; 449 init_data[i].use_linear_backlight_curve = false; 450 if (amdgpu_dc_debug_mask & DC_DISABLE_CUSTOM_BRIGHTNESS_CURVE) 451 init_data[i].use_linear_backlight_curve = true; 452 init_data[i].def_varibright_enable = 0; 453 init_data[i].varibright_level = 0; 454 /* 455 * Power module uses 16-bit backlight levels (0xFFFF max) rather 456 * than 8-bit(0XFF max) 457 */ 458 init_data[i].min_backlight_pwm = 459 dm->backlight_caps[i].min_input_signal * 0x101; 460 init_data[i].max_backlight_pwm = 461 dm->backlight_caps[i].max_input_signal * 0x101; 462 init_data[i].min_abm_backlight = 463 dm->backlight_caps[i].min_input_signal * 0x101; 464 465 /* Min backlight level after ABM reduction, Don't allow below 1% 466 * 0xFFFF x 0.01 = 0x28F 467 */ 468 init_data[i].min_abm_backlight = (init_data[i].min_abm_backlight < 0x28F) ? 469 0x28F : init_data[i].min_abm_backlight; 470 } 471 472 dm->power_module = mod_power_create(dm->dc, init_data, dm->num_of_edps); 473 if (!dm->power_module) { 474 drm_err(dm->ddev, "amdgpu: Error allocating memory for power module\n"); 475 return -ENOMEM; 476 } 477 478 mod_power_hw_init(dm->power_module); 479 drm_dbg_driver(dm->ddev, "amdgpu: Power module init done\n"); 480 481 return 0; 482 } 483 EXPORT_IF_KUNIT(amdgpu_dm_init_power_module); 484 485 static int amdgpu_dm_init(struct amdgpu_device *adev) 486 { 487 struct dc_init_data init_data; 488 struct dc_callback_init init_params; 489 int r; 490 491 adev->dm.ddev = adev_to_drm(adev); 492 adev->dm.adev = adev; 493 494 /* Zero all the fields */ 495 memset(&init_data, 0, sizeof(init_data)); 496 memset(&init_params, 0, sizeof(init_params)); 497 498 mutex_init(&adev->dm.dpia_aux_lock); 499 mutex_init(&adev->dm.dc_lock); 500 mutex_init(&adev->dm.audio_lock); 501 502 spin_lock_init(&adev->dm.dmub_lock); 503 504 if (amdgpu_dm_irq_init(adev)) { 505 drm_err(adev_to_drm(adev), "failed to initialize DM IRQ support.\n"); 506 goto error; 507 } 508 509 /* special handling for early revisions of GC 11.5.4 */ 510 if (amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(11, 5, 4)) 511 init_data.asic_id.chip_family = AMDGPU_FAMILY_GC_11_5_4; 512 else 513 init_data.asic_id.chip_family = adev->family; 514 515 init_data.asic_id.pci_revision_id = adev->pdev->revision; 516 init_data.asic_id.hw_internal_rev = adev->external_rev_id; 517 init_data.asic_id.chip_id = adev->pdev->device; 518 519 init_data.asic_id.vram_width = adev->gmc.vram_width; 520 /* TODO: initialize init_data.asic_id.vram_type here!!!! */ 521 init_data.asic_id.atombios_base_address = 522 adev->mode_info.atom_context->bios; 523 524 init_data.driver = adev; 525 526 /* cgs_device was created in dm_sw_init() */ 527 init_data.cgs_device = adev->dm.cgs_device; 528 529 init_data.dce_environment = DCE_ENV_PRODUCTION_DRV; 530 531 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) { 532 case IP_VERSION(2, 1, 0): 533 switch (adev->dm.dmcub_fw_version) { 534 case 0: /* development */ 535 case 0x1: /* linux-firmware.git hash 6d9f399 */ 536 case 0x01000000: /* linux-firmware.git hash 9a0b0f4 */ 537 init_data.flags.disable_dmcu = false; 538 break; 539 default: 540 init_data.flags.disable_dmcu = true; 541 } 542 break; 543 case IP_VERSION(2, 0, 3): 544 init_data.flags.disable_dmcu = true; 545 break; 546 default: 547 break; 548 } 549 550 /* APU support S/G display by default except: 551 * ASICs before Carrizo, 552 * RAVEN1 (Users reported stability issue) 553 */ 554 555 if (adev->asic_type < CHIP_CARRIZO) { 556 init_data.flags.gpu_vm_support = false; 557 } else if (adev->asic_type == CHIP_RAVEN) { 558 if (adev->apu_flags & AMD_APU_IS_RAVEN) 559 init_data.flags.gpu_vm_support = false; 560 else 561 init_data.flags.gpu_vm_support = (amdgpu_sg_display != 0); 562 } else { 563 if (amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(2, 0, 3)) 564 init_data.flags.gpu_vm_support = (amdgpu_sg_display == 1); 565 else 566 init_data.flags.gpu_vm_support = 567 (amdgpu_sg_display != 0) && (adev->flags & AMD_IS_APU); 568 } 569 570 adev->mode_info.gpu_vm_support = init_data.flags.gpu_vm_support; 571 572 if (amdgpu_dc_feature_mask & DC_FBC_MASK) 573 init_data.flags.fbc_support = true; 574 575 if (amdgpu_dc_feature_mask & DC_MULTI_MON_PP_MCLK_SWITCH_MASK) 576 init_data.flags.multi_mon_pp_mclk_switch = true; 577 578 if (amdgpu_dc_feature_mask & DC_DISABLE_FRACTIONAL_PWM_MASK) 579 init_data.flags.disable_fractional_pwm = true; 580 581 if (amdgpu_dc_feature_mask & DC_EDP_NO_POWER_SEQUENCING) 582 init_data.flags.edp_no_power_sequencing = true; 583 584 if (amdgpu_dc_feature_mask & DC_DISABLE_LTTPR_DP1_4A) 585 init_data.flags.allow_lttpr_non_transparent_mode.bits.DP1_4A = true; 586 if (amdgpu_dc_feature_mask & DC_DISABLE_LTTPR_DP2_0) 587 init_data.flags.allow_lttpr_non_transparent_mode.bits.DP2_0 = true; 588 589 if (amdgpu_dc_feature_mask & DC_FRL_MASK) 590 init_data.flags.enable_frl = true; 591 592 init_data.flags.seamless_boot_edp_requested = false; 593 594 if (amdgpu_device_seamless_boot_supported(adev)) { 595 init_data.flags.seamless_boot_edp_requested = true; 596 init_data.flags.allow_seamless_boot_optimization = true; 597 drm_dbg(adev->dm.ddev, "Seamless boot requested\n"); 598 } 599 600 init_data.flags.enable_mipi_converter_optimization = true; 601 602 init_data.dcn_reg_offsets = adev->reg_offset[DCE_HWIP][0]; 603 init_data.nbio_reg_offsets = adev->reg_offset[NBIO_HWIP][0]; 604 init_data.clk_reg_offsets = adev->reg_offset[CLK_HWIP][0]; 605 606 if (amdgpu_dc_debug_mask & DC_DISABLE_IPS) 607 init_data.flags.disable_ips = DMUB_IPS_DISABLE_ALL; 608 else if (amdgpu_dc_debug_mask & DC_DISABLE_IPS_DYNAMIC) 609 init_data.flags.disable_ips = DMUB_IPS_DISABLE_DYNAMIC; 610 else if (amdgpu_dc_debug_mask & DC_DISABLE_IPS2_DYNAMIC) 611 init_data.flags.disable_ips = DMUB_IPS_RCG_IN_ACTIVE_IPS2_IN_OFF; 612 else if (amdgpu_dc_debug_mask & DC_FORCE_IPS_ENABLE) 613 init_data.flags.disable_ips = DMUB_IPS_ENABLE; 614 else 615 init_data.flags.disable_ips = dm_get_default_ips_mode(adev); 616 617 init_data.flags.disable_ips_in_vpb = 0; 618 619 /* DCN35 and above supports dynamic DTBCLK switch */ 620 if (amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(3, 5, 0)) 621 init_data.flags.allow_0_dtb_clk = true; 622 623 /* Enable DWB for tested platforms only */ 624 if (amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(3, 0, 0)) 625 init_data.num_virtual_links = 1; 626 627 /* DCN42 and above dpia switch to unified link training path */ 628 if (amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(4, 2, 0)) { 629 init_data.flags.consolidated_dpia_dp_lt = true; 630 init_data.flags.enable_dpia_pre_training = true; 631 init_data.flags.unify_link_enc_assignment = true; 632 init_data.flags.usb4_bw_alloc_support = true; 633 } 634 635 /* DCN201 audio desyncs using DP SS */ 636 if (adev->apu_flags & AMD_APU_IS_CYAN_SKILLFISH2) 637 init_data.flags.ignore_dpref_ss = true; 638 639 retrieve_dmi_info(&adev->dm); 640 if (adev->dm.edp0_on_dp1_quirk) 641 init_data.flags.support_edp0_on_dp1 = true; 642 643 if (adev->dm.bb_from_dmub) 644 init_data.bb_from_dmub = adev->dm.bb_from_dmub; 645 else 646 init_data.bb_from_dmub = NULL; 647 648 /* Display Core create. */ 649 adev->dm.dc = dc_create(&init_data); 650 651 if (adev->dm.dc) { 652 drm_info(adev_to_drm(adev), "Display Core v%s initialized on %s\n", DC_VER, 653 dce_version_to_string(adev->dm.dc->ctx->dce_version)); 654 } else { 655 drm_info(adev_to_drm(adev), "Display Core failed to initialize with v%s!\n", DC_VER); 656 goto error; 657 } 658 659 if (amdgpu_dc_debug_mask & DC_DISABLE_PIPE_SPLIT) { 660 adev->dm.dc->debug.force_single_disp_pipe_split = false; 661 adev->dm.dc->debug.pipe_split_policy = MPC_SPLIT_AVOID; 662 } 663 664 if (adev->asic_type != CHIP_CARRIZO && adev->asic_type != CHIP_STONEY) 665 adev->dm.dc->debug.disable_stutter = amdgpu_pp_feature_mask & PP_STUTTER_MODE ? false : true; 666 if (dm_should_disable_stutter(adev->pdev)) 667 adev->dm.dc->debug.disable_stutter = true; 668 669 if (amdgpu_dc_debug_mask & DC_DISABLE_STUTTER) 670 adev->dm.dc->debug.disable_stutter = true; 671 672 if (amdgpu_dc_debug_mask & DC_DISABLE_DSC) 673 adev->dm.dc->debug.disable_dsc = true; 674 675 if (amdgpu_dc_debug_mask & DC_DISABLE_CLOCK_GATING) 676 adev->dm.dc->debug.disable_clock_gate = true; 677 678 if (amdgpu_dc_debug_mask & DC_FORCE_SUBVP_MCLK_SWITCH) 679 adev->dm.dc->debug.force_subvp_mclk_switch = true; 680 681 if (amdgpu_dc_debug_mask & DC_DISABLE_SUBVP_FAMS) { 682 adev->dm.dc->debug.force_disable_subvp = true; 683 adev->dm.dc->debug.fams2_config.bits.enable = false; 684 } 685 686 if (amdgpu_dc_debug_mask & DC_ENABLE_DML2) { 687 adev->dm.dc->debug.using_dml2 = true; 688 adev->dm.dc->debug.using_dml21 = true; 689 } 690 691 if (amdgpu_dc_debug_mask & DC_HDCP_LC_FORCE_FW_ENABLE) 692 adev->dm.dc->debug.hdcp_lc_force_fw_enable = true; 693 694 if (amdgpu_dc_debug_mask & DC_HDCP_LC_ENABLE_SW_FALLBACK) 695 adev->dm.dc->debug.hdcp_lc_enable_sw_fallback = true; 696 697 if (amdgpu_dc_debug_mask & DC_SKIP_DETECTION_LT) 698 adev->dm.dc->debug.skip_detection_link_training = true; 699 700 adev->dm.dc->debug.visual_confirm = amdgpu_dc_visual_confirm; 701 702 /* TODO: Remove after DP2 receiver gets proper support of Cable ID feature */ 703 adev->dm.dc->debug.ignore_cable_id = true; 704 705 /* temporary enable immediate restore for FAMS2 */ 706 adev->dm.dc->debug.fams2_imm_restore_drr = true; 707 708 if (adev->dm.dc->caps.dp_hdmi21_pcon_support) 709 drm_info(adev_to_drm(adev), "DP-HDMI FRL PCON supported\n"); 710 711 r = dm_dmub_hw_init(adev); 712 if (r) { 713 drm_err(adev_to_drm(adev), "DMUB interface failed to initialize: status=%d\n", r); 714 goto error; 715 } 716 717 dc_hardware_init(adev->dm.dc); 718 719 /* Enable cursor offload if the ASIC supports it. */ 720 dc_dmub_srv_cursor_offload_init(adev->dm.dc); 721 722 /* GOP/vBIOS may leave an OPTC enabled for a display present at power-on 723 * but no longer driven (e.g. an external DP unplugged at boot). Such a 724 * dangling pipe keeps DCN out of idle and blocks s0i3. Power it down 725 * here when nothing needs to be preserved. 726 */ 727 if (adev->flags & AMD_IS_APU) 728 dc_disable_dangling_timing_generators(adev->dm.dc); 729 730 adev->dm.hpd_rx_offload_wq = amdgpu_dm_hpd_rx_irq_create_workqueue(adev); 731 if (!adev->dm.hpd_rx_offload_wq) { 732 drm_err(adev_to_drm(adev), "failed to create hpd rx offload workqueue.\n"); 733 goto error; 734 } 735 736 if ((adev->flags & AMD_IS_APU) && (adev->asic_type >= CHIP_CARRIZO)) { 737 struct dc_phy_addr_space_config pa_config; 738 739 mmhub_read_system_context(adev, &pa_config); 740 741 // Call the DC init_memory func 742 dc_setup_system_context(adev->dm.dc, &pa_config); 743 } 744 745 adev->dm.freesync_module = mod_freesync_create(adev->dm.dc); 746 if (!adev->dm.freesync_module) { 747 drm_err(adev_to_drm(adev), 748 "failed to initialize freesync_module.\n"); 749 } else 750 drm_dbg_driver(adev_to_drm(adev), "freesync_module init done %p.\n", 751 adev->dm.freesync_module); 752 753 amdgpu_dm_init_color_mod(); 754 755 if (adev->dm.dc->caps.max_links > 0) { 756 adev->dm.vblank_control_workqueue = 757 create_singlethread_workqueue("dm_vblank_control_workqueue"); 758 if (!adev->dm.vblank_control_workqueue) 759 drm_err(adev_to_drm(adev), "failed to initialize vblank_workqueue.\n"); 760 } 761 762 if (adev->dm.dc->caps.ips_support && 763 adev->dm.dc->config.disable_ips != DMUB_IPS_DISABLE_ALL) 764 adev->dm.idle_workqueue = idle_create_workqueue(adev); 765 766 if (adev->dm.dc->caps.max_links > 0 && adev->family >= AMDGPU_FAMILY_RV) { 767 adev->dm.hdcp_workqueue = hdcp_create_workqueue(adev, &init_params.cp_psp, adev->dm.dc); 768 769 if (!adev->dm.hdcp_workqueue) 770 drm_err(adev_to_drm(adev), "failed to initialize hdcp_workqueue.\n"); 771 else 772 drm_dbg_driver(adev_to_drm(adev), 773 "hdcp_workqueue init done %p.\n", 774 adev->dm.hdcp_workqueue); 775 776 dc_init_callbacks(adev->dm.dc, &init_params); 777 } 778 if (adev->dm.dc->caps.max_links > 0) { 779 adev->dm.hdmi_frl_status_polling_wq = 780 create_singlethread_workqueue("hdmi_frl_status_polling_wq"); 781 if (!adev->dm.hdmi_frl_status_polling_wq) 782 drm_err(adev_to_drm(adev), "failed to initialize hdmi_frl_status_polling_wq\n"); 783 } 784 if (dc_is_dmub_outbox_supported(adev->dm.dc)) { 785 init_completion(&adev->dm.dmub_aux_transfer_done); 786 adev->dm.dmub_notify = kzalloc_obj(struct dmub_notification); 787 if (!adev->dm.dmub_notify) { 788 drm_info(adev_to_drm(adev), "fail to allocate adev->dm.dmub_notify"); 789 goto error; 790 } 791 792 adev->dm.delayed_hpd_wq = create_singlethread_workqueue("amdgpu_dm_hpd_wq"); 793 if (!adev->dm.delayed_hpd_wq) { 794 drm_err(adev_to_drm(adev), "failed to create hpd offload workqueue.\n"); 795 goto error; 796 } 797 798 amdgpu_dm_outbox_init(adev); 799 if (!dm_register_dmub_notify_callback(adev, DMUB_NOTIFICATION_AUX_REPLY, 800 dm_dmub_aux_setconfig_callback, false)) { 801 drm_err(adev_to_drm(adev), "fail to register dmub aux callback"); 802 goto error; 803 } 804 805 for (size_t i = 0; i < ARRAY_SIZE(adev->dm.fused_io); i++) 806 init_completion(&adev->dm.fused_io[i].replied); 807 808 if (!dm_register_dmub_notify_callback(adev, DMUB_NOTIFICATION_FUSED_IO, 809 dm_dmub_aux_fused_io_callback, false)) { 810 drm_err(adev_to_drm(adev), "fail to register dmub fused io callback"); 811 goto error; 812 } 813 /* Enable outbox notification only after IRQ handlers are registered and DMUB is alive. 814 * It is expected that DMUB will resend any pending notifications at this point. Note 815 * that hpd and hpd_irq handler registration are deferred to 816 * amdgpu_dm_register_hpd_handlers() to align legacy interface initialization 817 * sequence. Connection status will be proactivly detected once in the 818 * amdgpu_dm_initialize_drm_device. 819 */ 820 dc_enable_dmub_outbox(adev->dm.dc); 821 822 /* DPIA trace goes to dmesg logs only if outbox is enabled */ 823 if (amdgpu_dc_debug_mask & DC_ENABLE_DPIA_TRACE) 824 dc_dmub_srv_enable_dpia_trace(adev->dm.dc); 825 } 826 827 if (amdgpu_dm_initialize_drm_device(adev)) { 828 drm_err(adev_to_drm(adev), 829 "failed to initialize sw for display support.\n"); 830 goto error; 831 } 832 833 if (amdgpu_dm_init_power_module(&adev->dm)) 834 goto error; 835 836 /* create fake encoders for MST */ 837 dm_dp_create_fake_mst_encoders(adev); 838 839 /* TODO: Add_display_info? */ 840 841 /* TODO use dynamic cursor width */ 842 adev_to_drm(adev)->mode_config.cursor_width = adev->dm.dc->caps.max_cursor_size; 843 adev_to_drm(adev)->mode_config.cursor_height = adev->dm.dc->caps.max_cursor_size; 844 845 if (drm_vblank_init(adev_to_drm(adev), adev->dm.display_indexes_num)) { 846 drm_err(adev_to_drm(adev), 847 "failed to initialize vblank for display support.\n"); 848 goto error; 849 } 850 851 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY) 852 amdgpu_dm_crtc_secure_display_create_contexts(adev); 853 if (!adev->dm.secure_display_ctx.crtc_ctx) 854 drm_err(adev_to_drm(adev), "failed to initialize secure display contexts.\n"); 855 856 if (amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(4, 0, 1)) 857 adev->dm.secure_display_ctx.support_mul_roi = true; 858 859 #endif 860 861 drm_dbg_driver(adev_to_drm(adev), "KMS initialized.\n"); 862 863 return 0; 864 error: 865 amdgpu_dm_fini(adev); 866 867 return -EINVAL; 868 } 869 870 STATIC_IFN_KUNIT int amdgpu_dm_early_fini(struct amdgpu_ip_block *ip_block) 871 { 872 struct amdgpu_device *adev = ip_block->adev; 873 874 amdgpu_dm_audio_fini(adev); 875 876 return 0; 877 } 878 EXPORT_IF_KUNIT(amdgpu_dm_early_fini); 879 880 static void amdgpu_dm_fini(struct amdgpu_device *adev) 881 { 882 int i; 883 884 if (adev->dm.vblank_control_workqueue) { 885 destroy_workqueue(adev->dm.vblank_control_workqueue); 886 adev->dm.vblank_control_workqueue = NULL; 887 } 888 889 /* The poll re-queues itself, so stop it before the wq and dc go away. */ 890 if (adev->dm.hdmi_frl_status_polling_wq) { 891 cancel_delayed_work_sync(&adev->dm.hdmi_frl_status_polling_work); 892 destroy_workqueue(adev->dm.hdmi_frl_status_polling_wq); 893 adev->dm.hdmi_frl_status_polling_wq = NULL; 894 } 895 896 if (adev->dm.idle_workqueue) { 897 if (adev->dm.idle_workqueue->running) { 898 adev->dm.idle_workqueue->enable = false; 899 flush_work(&adev->dm.idle_workqueue->work); 900 } 901 902 kfree(adev->dm.idle_workqueue); 903 adev->dm.idle_workqueue = NULL; 904 } 905 906 /* 907 * Disable ISM before dc_destroy() invalidates dm->dc. 908 * 909 * Quiesce workers first without dc_lock (they take dc_lock 910 * themselves, so syncing under it would deadlock), then drive the 911 * FSM back to FULL_POWER_RUNNING under dc_lock. 912 */ 913 amdgpu_dm_ism_disable(&adev->dm); 914 scoped_guard(mutex, &adev->dm.dc_lock) 915 amdgpu_dm_ism_force_full_power(&adev->dm); 916 917 amdgpu_dm_destroy_drm_device(&adev->dm); 918 919 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY) 920 if (adev->dm.secure_display_ctx.crtc_ctx) { 921 for (i = 0; i < adev->mode_info.num_crtc; i++) { 922 if (adev->dm.secure_display_ctx.crtc_ctx[i].crtc) { 923 flush_work(&adev->dm.secure_display_ctx.crtc_ctx[i].notify_ta_work); 924 flush_work(&adev->dm.secure_display_ctx.crtc_ctx[i].forward_roi_work); 925 } 926 } 927 kfree(adev->dm.secure_display_ctx.crtc_ctx); 928 adev->dm.secure_display_ctx.crtc_ctx = NULL; 929 } 930 #endif 931 if (adev->dm.hdcp_workqueue) { 932 hdcp_destroy(&adev->dev->kobj, adev->dm.hdcp_workqueue); 933 adev->dm.hdcp_workqueue = NULL; 934 } 935 936 if (adev->dm.dc) { 937 dc_deinit_callbacks(adev->dm.dc); 938 dc_dmub_srv_destroy(&adev->dm.dc->ctx->dmub_srv); 939 if (dc_enable_dmub_notifications(adev->dm.dc)) { 940 kfree(adev->dm.dmub_notify); 941 adev->dm.dmub_notify = NULL; 942 destroy_workqueue(adev->dm.delayed_hpd_wq); 943 adev->dm.delayed_hpd_wq = NULL; 944 } 945 } 946 947 if (adev->dm.dmub_bo) 948 amdgpu_bo_free_kernel(&adev->dm.dmub_bo, 949 &adev->dm.dmub_bo_gpu_addr, 950 &adev->dm.dmub_bo_cpu_addr); 951 952 if (adev->dm.boot_time_crc_info.bo_ptr) 953 amdgpu_bo_free_kernel(&adev->dm.boot_time_crc_info.bo_ptr, 954 &adev->dm.boot_time_crc_info.gpu_addr, 955 &adev->dm.boot_time_crc_info.cpu_addr); 956 957 if (adev->dm.hpd_rx_offload_wq && adev->dm.dc) { 958 for (i = 0; i < adev->dm.dc->caps.max_links; i++) { 959 if (adev->dm.hpd_rx_offload_wq[i].wq) { 960 destroy_workqueue(adev->dm.hpd_rx_offload_wq[i].wq); 961 adev->dm.hpd_rx_offload_wq[i].wq = NULL; 962 } 963 } 964 965 kfree(adev->dm.hpd_rx_offload_wq); 966 adev->dm.hpd_rx_offload_wq = NULL; 967 } 968 969 amdgpu_dm_irq_fini(adev); 970 971 /* DC Destroy TODO: Replace destroy DAL */ 972 if (adev->dm.dc) 973 dc_destroy(&adev->dm.dc); 974 975 if (adev->dm.cgs_device) { 976 amdgpu_cgs_destroy_device(adev->dm.cgs_device); 977 adev->dm.cgs_device = NULL; 978 } 979 if (adev->dm.freesync_module) { 980 mod_freesync_destroy(adev->dm.freesync_module); 981 adev->dm.freesync_module = NULL; 982 } 983 984 if (adev->dm.power_module) { 985 mod_power_destroy(adev->dm.power_module); 986 adev->dm.power_module = NULL; 987 } 988 mutex_destroy(&adev->dm.audio_lock); 989 mutex_destroy(&adev->dm.dc_lock); 990 mutex_destroy(&adev->dm.dpia_aux_lock); 991 } 992 993 STATIC_IFN_KUNIT int load_dmcu_fw(struct amdgpu_device *adev) 994 { 995 const char *fw_name_dmcu = NULL; 996 int r; 997 const struct dmcu_firmware_header_v1_0 *hdr; 998 999 switch (adev->asic_type) { 1000 #if defined(CONFIG_DRM_AMD_DC_SI) 1001 case CHIP_TAHITI: 1002 case CHIP_PITCAIRN: 1003 case CHIP_VERDE: 1004 case CHIP_OLAND: 1005 #endif 1006 case CHIP_BONAIRE: 1007 case CHIP_HAWAII: 1008 case CHIP_KAVERI: 1009 case CHIP_KABINI: 1010 case CHIP_MULLINS: 1011 case CHIP_TONGA: 1012 case CHIP_FIJI: 1013 case CHIP_CARRIZO: 1014 case CHIP_STONEY: 1015 case CHIP_POLARIS11: 1016 case CHIP_POLARIS10: 1017 case CHIP_POLARIS12: 1018 case CHIP_VEGAM: 1019 case CHIP_VEGA10: 1020 case CHIP_VEGA12: 1021 case CHIP_VEGA20: 1022 return 0; 1023 case CHIP_NAVI12: 1024 fw_name_dmcu = FIRMWARE_NAVI12_DMCU; 1025 break; 1026 case CHIP_RAVEN: 1027 if (ASICREV_IS_PICASSO(adev->external_rev_id)) 1028 fw_name_dmcu = FIRMWARE_RAVEN_DMCU; 1029 else if (ASICREV_IS_RAVEN2(adev->external_rev_id)) 1030 fw_name_dmcu = FIRMWARE_RAVEN_DMCU; 1031 else 1032 return 0; 1033 break; 1034 default: 1035 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) { 1036 case IP_VERSION(2, 0, 2): 1037 case IP_VERSION(2, 0, 3): 1038 case IP_VERSION(2, 0, 0): 1039 case IP_VERSION(2, 1, 0): 1040 case IP_VERSION(3, 0, 0): 1041 case IP_VERSION(3, 0, 2): 1042 case IP_VERSION(3, 0, 3): 1043 case IP_VERSION(3, 0, 1): 1044 case IP_VERSION(3, 1, 2): 1045 case IP_VERSION(3, 1, 3): 1046 case IP_VERSION(3, 1, 4): 1047 case IP_VERSION(3, 1, 5): 1048 case IP_VERSION(3, 1, 6): 1049 case IP_VERSION(3, 2, 0): 1050 case IP_VERSION(3, 2, 1): 1051 case IP_VERSION(3, 5, 0): 1052 case IP_VERSION(3, 5, 1): 1053 case IP_VERSION(3, 6, 0): 1054 case IP_VERSION(4, 0, 1): 1055 case IP_VERSION(4, 2, 0): 1056 case IP_VERSION(4, 2, 1): 1057 case IP_VERSION(6, 0, 0): 1058 return 0; 1059 default: 1060 break; 1061 } 1062 drm_err(adev_to_drm(adev), "Unsupported ASIC type: 0x%X\n", adev->asic_type); 1063 return -EINVAL; 1064 } 1065 1066 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) { 1067 drm_dbg_kms(adev_to_drm(adev), "dm: DMCU firmware not supported on direct or SMU loading\n"); 1068 return 0; 1069 } 1070 1071 r = amdgpu_ucode_request(adev, &adev->dm.fw_dmcu, AMDGPU_UCODE_REQUIRED, 1072 "%s", fw_name_dmcu); 1073 if (r == -ENODEV) { 1074 /* DMCU firmware is not necessary, so don't raise a fuss if it's missing */ 1075 drm_dbg_kms(adev_to_drm(adev), "dm: DMCU firmware not found\n"); 1076 adev->dm.fw_dmcu = NULL; 1077 return 0; 1078 } 1079 if (r) { 1080 drm_err(adev_to_drm(adev), "amdgpu_dm: Can't validate firmware \"%s\"\n", 1081 fw_name_dmcu); 1082 amdgpu_ucode_release(&adev->dm.fw_dmcu); 1083 return r; 1084 } 1085 1086 hdr = (const struct dmcu_firmware_header_v1_0 *)adev->dm.fw_dmcu->data; 1087 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].ucode_id = AMDGPU_UCODE_ID_DMCU_ERAM; 1088 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].fw = adev->dm.fw_dmcu; 1089 adev->firmware.fw_size += 1090 ALIGN(le32_to_cpu(hdr->header.ucode_size_bytes) - le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE); 1091 1092 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].ucode_id = AMDGPU_UCODE_ID_DMCU_INTV; 1093 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].fw = adev->dm.fw_dmcu; 1094 adev->firmware.fw_size += 1095 ALIGN(le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE); 1096 1097 adev->dm.dmcu_fw_version = le32_to_cpu(hdr->header.ucode_version); 1098 1099 drm_dbg_kms(adev_to_drm(adev), "PSP loading DMCU firmware\n"); 1100 1101 return 0; 1102 } 1103 EXPORT_IF_KUNIT(load_dmcu_fw); 1104 1105 STATIC_IFN_KUNIT int dm_sw_init(struct amdgpu_ip_block *ip_block) 1106 { 1107 struct amdgpu_device *adev = ip_block->adev; 1108 int r; 1109 1110 adev->dm.cgs_device = amdgpu_cgs_create_device(adev); 1111 1112 if (!adev->dm.cgs_device) { 1113 drm_err(adev_to_drm(adev), "failed to create cgs device.\n"); 1114 return -EINVAL; 1115 } 1116 1117 /* Moved from dm init since we need to use allocations for storing bounding box data */ 1118 INIT_LIST_HEAD(&adev->dm.da_list); 1119 1120 r = dm_dmub_sw_init(adev); 1121 if (r) 1122 return r; 1123 1124 return load_dmcu_fw(adev); 1125 } 1126 EXPORT_IF_KUNIT(dm_sw_init); 1127 1128 STATIC_IFN_KUNIT int dm_sw_fini(struct amdgpu_ip_block *ip_block) 1129 { 1130 struct amdgpu_device *adev = ip_block->adev; 1131 struct dal_allocation *da; 1132 1133 list_for_each_entry(da, &adev->dm.da_list, list) { 1134 if (adev->dm.bb_from_dmub == (void *) da->cpu_ptr) { 1135 amdgpu_bo_free_kernel(&da->bo, &da->gpu_addr, &da->cpu_ptr); 1136 list_del(&da->list); 1137 kfree(da); 1138 adev->dm.bb_from_dmub = NULL; 1139 break; 1140 } 1141 } 1142 1143 1144 kfree(adev->dm.dmub_fb_info); 1145 adev->dm.dmub_fb_info = NULL; 1146 1147 if (adev->dm.dmub_srv) { 1148 dmub_srv_destroy(adev->dm.dmub_srv); 1149 kfree(adev->dm.dmub_srv); 1150 adev->dm.dmub_srv = NULL; 1151 } 1152 1153 amdgpu_ucode_release(&adev->dm.dmub_fw); 1154 amdgpu_ucode_release(&adev->dm.fw_dmcu); 1155 1156 return 0; 1157 } 1158 EXPORT_IF_KUNIT(dm_sw_fini); 1159 1160 1161 static void amdgpu_dm_boot_time_crc_init(struct amdgpu_device *adev) 1162 { 1163 struct dm_boot_time_crc_info *bootcrc_info = NULL; 1164 struct dmub_srv *dmub = NULL; 1165 union dmub_fw_boot_options option = {0}; 1166 int ret = 0; 1167 const uint32_t fb_size = 3 * 1024 * 1024; /* 3MB for DCC pattern */ 1168 1169 if (!adev || !adev->dm.dc || !adev->dm.dc->ctx || 1170 !adev->dm.dc->ctx->dmub_srv) { 1171 return; 1172 } 1173 1174 dmub = adev->dm.dc->ctx->dmub_srv->dmub; 1175 bootcrc_info = &adev->dm.boot_time_crc_info; 1176 1177 if (!dmub || !dmub->hw_funcs.get_fw_boot_option) { 1178 drm_dbg(adev_to_drm(adev), "failed to init boot time crc buffer\n"); 1179 return; 1180 } 1181 1182 option = dmub->hw_funcs.get_fw_boot_option(dmub); 1183 1184 /* Return if boot time CRC is not enabled */ 1185 if (option.bits.bootcrc_en_at_S0i3 == 0) 1186 return; 1187 1188 /* Create a buffer for boot time CRC */ 1189 ret = amdgpu_bo_create_kernel(adev, fb_size, PAGE_SIZE, 1190 AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT, 1191 &bootcrc_info->bo_ptr, 1192 &bootcrc_info->gpu_addr, 1193 &bootcrc_info->cpu_addr); 1194 1195 if (ret) { 1196 drm_dbg(adev_to_drm(adev), "failed to create boot time crc buffer\n"); 1197 } else { 1198 bootcrc_info->size = fb_size; 1199 1200 drm_dbg(adev_to_drm(adev), "boot time crc buffer created addr 0x%llx, size %u\n", 1201 bootcrc_info->gpu_addr, bootcrc_info->size); 1202 1203 /* Send the buffer info to DMUB */ 1204 dc_dmub_srv_boot_time_crc_init(adev->dm.dc, 1205 bootcrc_info->gpu_addr, bootcrc_info->size); 1206 } 1207 } 1208 1209 STATIC_IFN_KUNIT int dm_late_init(struct amdgpu_ip_block *ip_block) 1210 { 1211 struct amdgpu_device *adev = ip_block->adev; 1212 1213 struct dmcu_iram_parameters params; 1214 unsigned int linear_lut[16]; 1215 int i; 1216 struct dmcu *dmcu = NULL; 1217 1218 dmcu = adev->dm.dc->res_pool->dmcu; 1219 1220 /* Init the boot time CRC (skip in resume) */ 1221 if ((adev->in_suspend == 0) && 1222 (amdgpu_ip_version(adev, DCE_HWIP, 0) == IP_VERSION(3, 6, 0))) 1223 amdgpu_dm_boot_time_crc_init(adev); 1224 1225 for (i = 0; i < 16; i++) 1226 linear_lut[i] = 0xFFFF * i / 15; 1227 1228 params.set = 0; 1229 params.backlight_ramping_override = false; 1230 params.backlight_ramping_start = 0xCCCC; 1231 params.backlight_ramping_reduction = 0xCCCCCCCC; 1232 params.backlight_lut_array_size = 16; 1233 params.backlight_lut_array = linear_lut; 1234 1235 /* Min backlight level after ABM reduction, Don't allow below 1% 1236 * 0xFFFF x 0.01 = 0x28F 1237 */ 1238 params.min_abm_backlight = 0x28F; 1239 /* In the case where abm is implemented on dmcub, 1240 * dmcu object will be null. 1241 * ABM 2.4 and up are implemented on dmcub. 1242 */ 1243 if (dmcu) { 1244 if (!dmcu_load_iram(dmcu, params)) 1245 return -EINVAL; 1246 } else if (adev->dm.dc->ctx->dmub_srv) { 1247 struct dc_link *edp_links[MAX_NUM_EDP]; 1248 int edp_num; 1249 1250 dc_get_edp_links(adev->dm.dc, edp_links, &edp_num); 1251 for (i = 0; i < edp_num; i++) { 1252 if (!dmub_init_abm_config(adev->dm.dc->res_pool, params, i)) 1253 return -EINVAL; 1254 } 1255 } 1256 1257 return amdgpu_dm_detect_mst_link_for_all_connectors(adev_to_drm(adev)); 1258 } 1259 EXPORT_IF_KUNIT(dm_late_init); 1260 1261 STATIC_IFN_KUNIT void resume_mst_branch_status(struct drm_dp_mst_topology_mgr *mgr) 1262 { 1263 u8 buf[UUID_SIZE]; 1264 guid_t guid; 1265 int ret; 1266 1267 mutex_lock(&mgr->lock); 1268 if (!mgr->mst_primary) 1269 goto out_fail; 1270 1271 if (drm_dp_read_dpcd_caps(mgr->aux, mgr->dpcd) < 0) { 1272 drm_dbg_kms(mgr->dev, "dpcd read failed - undocked during suspend?\n"); 1273 goto out_fail; 1274 } 1275 1276 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 1277 DP_MST_EN | 1278 DP_UP_REQ_EN | 1279 DP_UPSTREAM_IS_SRC); 1280 if (ret < 0) { 1281 drm_dbg_kms(mgr->dev, "mst write failed - undocked during suspend?\n"); 1282 goto out_fail; 1283 } 1284 1285 /* Some hubs forget their guids after they resume */ 1286 ret = drm_dp_dpcd_read(mgr->aux, DP_GUID, buf, sizeof(buf)); 1287 if (ret != sizeof(buf)) { 1288 drm_dbg_kms(mgr->dev, "dpcd read failed - undocked during suspend?\n"); 1289 goto out_fail; 1290 } 1291 1292 import_guid(&guid, buf); 1293 1294 if (guid_is_null(&guid)) { 1295 guid_gen(&guid); 1296 export_guid(buf, &guid); 1297 1298 ret = drm_dp_dpcd_write(mgr->aux, DP_GUID, buf, sizeof(buf)); 1299 1300 if (ret != sizeof(buf)) { 1301 drm_dbg_kms(mgr->dev, "check mstb guid failed - undocked during suspend?\n"); 1302 goto out_fail; 1303 } 1304 } 1305 1306 guid_copy(&mgr->mst_primary->guid, &guid); 1307 1308 out_fail: 1309 mutex_unlock(&mgr->lock); 1310 } 1311 EXPORT_IF_KUNIT(resume_mst_branch_status); 1312 1313 STATIC_IFN_KUNIT void s3_handle_mst(struct drm_device *dev, bool suspend) 1314 { 1315 struct amdgpu_dm_connector *aconnector; 1316 struct drm_connector *connector; 1317 struct drm_connector_list_iter iter; 1318 struct drm_dp_mst_topology_mgr *mgr; 1319 1320 drm_connector_list_iter_begin(dev, &iter); 1321 drm_for_each_connector_iter(connector, &iter) { 1322 1323 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK) 1324 continue; 1325 1326 aconnector = to_amdgpu_dm_connector(connector); 1327 if (aconnector->dc_link->type != dc_connection_mst_branch || 1328 aconnector->mst_root) 1329 continue; 1330 1331 mgr = &aconnector->mst_mgr; 1332 1333 if (suspend) { 1334 drm_dp_mst_topology_mgr_suspend(mgr); 1335 } else { 1336 /* if extended timeout is supported in hardware, 1337 * default to LTTPR timeout (3.2ms) first as a W/A for DP link layer 1338 * CTS 4.2.1.1 regression introduced by CTS specs requirement update. 1339 */ 1340 try_to_configure_aux_timeout(aconnector->dc_link->ddc, LINK_AUX_DEFAULT_LTTPR_TIMEOUT_PERIOD); 1341 if (!dp_is_lttpr_present(aconnector->dc_link)) 1342 try_to_configure_aux_timeout(aconnector->dc_link->ddc, LINK_AUX_DEFAULT_TIMEOUT_PERIOD); 1343 1344 /* TODO: move resume_mst_branch_status() into drm mst resume again 1345 * once topology probing work is pulled out from mst resume into mst 1346 * resume 2nd step. mst resume 2nd step should be called after old 1347 * state getting restored (i.e. drm_atomic_helper_resume()). 1348 */ 1349 resume_mst_branch_status(mgr); 1350 } 1351 } 1352 drm_connector_list_iter_end(&iter); 1353 } 1354 EXPORT_IF_KUNIT(s3_handle_mst); 1355 1356 STATIC_IFN_KUNIT int dm_oem_i2c_hw_init(struct amdgpu_device *adev) 1357 { 1358 struct amdgpu_display_manager *dm = &adev->dm; 1359 struct amdgpu_i2c_adapter *oem_i2c; 1360 struct ddc_service *oem_ddc_service; 1361 int r; 1362 1363 oem_ddc_service = dc_get_oem_i2c_device(adev->dm.dc); 1364 if (oem_ddc_service) { 1365 oem_i2c = amdgpu_dm_create_i2c(oem_ddc_service, true); 1366 if (!oem_i2c) { 1367 drm_info(adev_to_drm(adev), "Failed to create oem i2c adapter data\n"); 1368 return -ENOMEM; 1369 } 1370 1371 r = devm_i2c_add_adapter(adev->dev, &oem_i2c->base); 1372 if (r) { 1373 drm_info(adev_to_drm(adev), "Failed to register oem i2c\n"); 1374 kfree(oem_i2c); 1375 return r; 1376 } 1377 dm->oem_i2c = oem_i2c; 1378 } 1379 1380 return 0; 1381 } 1382 EXPORT_IF_KUNIT(dm_oem_i2c_hw_init); 1383 1384 /** 1385 * dm_hw_init() - Initialize DC device 1386 * @ip_block: Pointer to the amdgpu_ip_block for this hw instance. 1387 * 1388 * Initialize the &struct amdgpu_display_manager device. This involves calling 1389 * the initializers of each DM component, then populating the struct with them. 1390 * 1391 * Although the function implies hardware initialization, both hardware and 1392 * software are initialized here. Splitting them out to their relevant init 1393 * hooks is a future TODO item. 1394 * 1395 * Some notable things that are initialized here: 1396 * 1397 * - Display Core, both software and hardware 1398 * - DC modules that we need (freesync and color management) 1399 * - DRM software states 1400 * - Interrupt sources and handlers 1401 * - Vblank support 1402 * - Debug FS entries, if enabled 1403 */ 1404 static int dm_hw_init(struct amdgpu_ip_block *ip_block) 1405 { 1406 struct amdgpu_device *adev = ip_block->adev; 1407 int r; 1408 1409 adev->dm.i2c_devres_group = devres_open_group(adev->dev, NULL, GFP_KERNEL); 1410 if (!adev->dm.i2c_devres_group) 1411 return -ENOMEM; 1412 1413 /* Create DAL display manager */ 1414 r = amdgpu_dm_init(adev); 1415 if (r) 1416 goto err_release_i2c; 1417 amdgpu_dm_hpd_init(adev); 1418 1419 r = dm_oem_i2c_hw_init(adev); 1420 if (r) 1421 drm_info(adev_to_drm(adev), "Failed to add OEM i2c bus\n"); 1422 1423 devres_close_group(adev->dev, adev->dm.i2c_devres_group); 1424 return 0; 1425 1426 err_release_i2c: 1427 devres_release_group(adev->dev, adev->dm.i2c_devres_group); 1428 return r; 1429 } 1430 1431 /** 1432 * dm_hw_fini() - Teardown DC device 1433 * @ip_block: Pointer to the amdgpu_ip_block for this hw instance. 1434 * 1435 * Teardown components within &struct amdgpu_display_manager that require 1436 * cleanup. This involves cleaning up the DRM device, DC, and any modules that 1437 * were loaded. Also flush IRQ workqueues and disable them. 1438 */ 1439 static int dm_hw_fini(struct amdgpu_ip_block *ip_block) 1440 { 1441 struct amdgpu_device *adev = ip_block->adev; 1442 1443 if (adev->dm.i2c_devres_group) 1444 devres_release_group(adev->dev, adev->dm.i2c_devres_group); 1445 1446 amdgpu_dm_hpd_fini(adev); 1447 1448 amdgpu_dm_fini(adev); 1449 return 0; 1450 } 1451 1452 1453 STATIC_IFN_KUNIT void dm_gpureset_toggle_interrupts(struct amdgpu_device *adev, 1454 struct dc_state *state, bool enable) 1455 { 1456 enum dc_irq_source irq_source; 1457 struct amdgpu_crtc *acrtc; 1458 int rc = -EBUSY; 1459 int i = 0; 1460 1461 for (i = 0; i < state->stream_count; i++) { 1462 acrtc = amdgpu_dm_get_crtc_by_otg_inst( 1463 adev, state->stream_status[i].primary_otg_inst); 1464 1465 if (acrtc && state->stream_status[i].plane_count != 0 && 1466 amdgpu_ip_version(adev, DCE_HWIP, 0) == 0) { 1467 irq_source = IRQ_TYPE_PFLIP + acrtc->otg_inst; 1468 rc = amdgpu_dm_irq_set(adev, irq_source, enable) ? 0 : -EBUSY; 1469 if (rc) 1470 drm_warn(adev_to_drm(adev), "Failed to %s pflip interrupts\n", 1471 enable ? "enable" : "disable"); 1472 1473 if (dc_supports_vrr(adev->dm.dc->ctx->dce_version)) { 1474 if (enable) { 1475 if (amdgpu_dm_crtc_vrr_active( 1476 to_dm_crtc_state(acrtc->base.state))) 1477 rc = amdgpu_dm_crtc_set_vupdate_irq( 1478 &acrtc->base, true); 1479 } else 1480 rc = amdgpu_dm_crtc_set_vupdate_irq( 1481 &acrtc->base, false); 1482 1483 if (rc) 1484 drm_warn(adev_to_drm(adev), "Failed to %sable vupdate interrupt\n", 1485 enable ? "en" : "dis"); 1486 } 1487 1488 irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst; 1489 /* During gpu-reset we disable and then enable vblank irq, so 1490 * don't use amdgpu_irq_get/put() to avoid refcount change. 1491 */ 1492 if (!amdgpu_dm_irq_set(adev, irq_source, enable)) 1493 drm_warn(adev_to_drm(adev), "Failed to %sable vblank interrupt\n", enable ? "en" : "dis"); 1494 1495 } else if (acrtc && state->stream_status[i].plane_count != 0) { 1496 /* DCN only needs to toggle VUPDATE_NO_LOCK */ 1497 rc = amdgpu_dm_crtc_set_vupdate_irq(&acrtc->base, enable); 1498 if (rc) 1499 drm_warn(adev_to_drm(adev), "Failed to %sable vupdate interrupt\n", 1500 enable ? "en" : "dis"); 1501 } 1502 } 1503 1504 } 1505 EXPORT_IF_KUNIT(dm_gpureset_toggle_interrupts); 1506 1507 DEFINE_FREE(state_release, struct dc_state *, dc_state_release(_T)) 1508 1509 STATIC_IFN_KUNIT enum dc_status amdgpu_dm_commit_zero_streams(struct dc *dc) 1510 { 1511 struct dc_state *context __free(state_release) = NULL; 1512 int i; 1513 struct dc_stream_state *del_streams[MAX_PIPES]; 1514 int del_streams_count = 0; 1515 struct dc_commit_streams_params params = {}; 1516 1517 memset(del_streams, 0, sizeof(del_streams)); 1518 1519 context = dc_state_create_current_copy(dc); 1520 if (context == NULL) 1521 return DC_ERROR_UNEXPECTED; 1522 1523 /* First remove from context all streams */ 1524 for (i = 0; i < context->stream_count; i++) { 1525 struct dc_stream_state *stream = context->streams[i]; 1526 1527 del_streams[del_streams_count++] = stream; 1528 } 1529 1530 /* Remove all planes for removed streams and then remove the streams */ 1531 for (i = 0; i < del_streams_count; i++) { 1532 enum dc_status res; 1533 1534 if (!dc_state_rem_all_planes_for_stream(dc, del_streams[i], context)) 1535 return DC_FAIL_DETACH_SURFACES; 1536 1537 res = dc_state_remove_stream(dc, context, del_streams[i]); 1538 if (res != DC_OK) 1539 return res; 1540 } 1541 1542 params.streams = context->streams; 1543 params.stream_count = context->stream_count; 1544 1545 return dc_commit_streams(dc, ¶ms); 1546 } 1547 EXPORT_IF_KUNIT(amdgpu_dm_commit_zero_streams); 1548 1549 STATIC_IFN_KUNIT int dm_cache_state(struct amdgpu_device *adev) 1550 { 1551 int r; 1552 1553 adev->dm.cached_state = dm_atomic_helper_suspend(adev_to_drm(adev)); 1554 if (IS_ERR(adev->dm.cached_state)) { 1555 r = PTR_ERR(adev->dm.cached_state); 1556 adev->dm.cached_state = NULL; 1557 } 1558 1559 return adev->dm.cached_state ? 0 : r; 1560 } 1561 EXPORT_IF_KUNIT(dm_cache_state); 1562 1563 STATIC_IFN_KUNIT void dm_destroy_cached_state(struct amdgpu_device *adev) 1564 { 1565 struct amdgpu_display_manager *dm = &adev->dm; 1566 struct drm_device *ddev = adev_to_drm(adev); 1567 struct dm_plane_state *dm_new_plane_state; 1568 struct drm_plane_state *new_plane_state; 1569 struct dm_crtc_state *dm_new_crtc_state; 1570 struct drm_crtc_state *new_crtc_state; 1571 struct drm_plane *plane; 1572 struct drm_crtc *crtc; 1573 int i; 1574 1575 if (!dm->cached_state) 1576 return; 1577 1578 /* Force mode set in atomic commit */ 1579 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) { 1580 new_crtc_state->active_changed = true; 1581 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 1582 amdgpu_dm_reset_freesync_config_for_crtc(dm_new_crtc_state); 1583 } 1584 1585 /* 1586 * atomic_check is expected to create the dc states. We need to release 1587 * them here, since they were duplicated as part of the suspend 1588 * procedure. 1589 */ 1590 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) { 1591 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 1592 if (dm_new_crtc_state->stream) { 1593 WARN_ON(kref_read(&dm_new_crtc_state->stream->refcount) > 1); 1594 dc_stream_release(dm_new_crtc_state->stream); 1595 dm_new_crtc_state->stream = NULL; 1596 } 1597 dm_new_crtc_state->base.color_mgmt_changed = true; 1598 } 1599 1600 for_each_new_plane_in_state(dm->cached_state, plane, new_plane_state, i) { 1601 dm_new_plane_state = to_dm_plane_state(new_plane_state); 1602 if (dm_new_plane_state->dc_state) { 1603 WARN_ON(kref_read(&dm_new_plane_state->dc_state->refcount) > 1); 1604 dc_plane_state_release(dm_new_plane_state->dc_state); 1605 dm_new_plane_state->dc_state = NULL; 1606 } 1607 } 1608 1609 drm_atomic_helper_resume(ddev, dm->cached_state); 1610 1611 dm->cached_state = NULL; 1612 } 1613 EXPORT_IF_KUNIT(dm_destroy_cached_state); 1614 1615 static int dm_suspend(struct amdgpu_ip_block *ip_block) 1616 { 1617 struct amdgpu_device *adev = ip_block->adev; 1618 struct amdgpu_display_manager *dm = &adev->dm; 1619 1620 if (amdgpu_in_reset(adev)) { 1621 enum dc_status res; 1622 1623 /* Quiesce ISM workers before taking dc_lock (workers take 1624 * dc_lock themselves; syncing under it would deadlock). 1625 */ 1626 amdgpu_dm_ism_disable(dm); 1627 1628 mutex_lock(&dm->dc_lock); 1629 1630 amdgpu_dm_ism_force_full_power(dm); 1631 dc_allow_idle_optimizations(adev->dm.dc, false); 1632 1633 dm->cached_dc_state = dc_state_create_copy(dm->dc->current_state); 1634 if (!dm->cached_dc_state) { 1635 drm_err(adev_to_drm(adev), 1636 "Failed to allocate cached DC state during suspend\n"); 1637 mutex_unlock(&dm->dc_lock); 1638 return -ENOMEM; 1639 } 1640 1641 dm_gpureset_toggle_interrupts(adev, dm->cached_dc_state, false); 1642 1643 res = amdgpu_dm_commit_zero_streams(dm->dc); 1644 if (res != DC_OK) { 1645 drm_err(adev_to_drm(adev), "Failed to commit zero streams: %d\n", res); 1646 dc_state_release(dm->cached_dc_state); 1647 dm->cached_dc_state = NULL; 1648 mutex_unlock(&dm->dc_lock); 1649 return -EINVAL; 1650 } 1651 1652 amdgpu_dm_irq_suspend(adev); 1653 1654 amdgpu_dm_hpd_rx_irq_work_suspend(dm); 1655 1656 return 0; 1657 } 1658 1659 if (!adev->dm.cached_state) { 1660 int r = dm_cache_state(adev); 1661 1662 if (r) 1663 return r; 1664 } 1665 1666 amdgpu_dm_s3_handle_hdmi_cec(adev_to_drm(adev), true); 1667 1668 s3_handle_mst(adev_to_drm(adev), true); 1669 1670 amdgpu_dm_irq_suspend(adev); 1671 1672 /* 1673 * Quiesce ISM workers before taking dc_lock (workers take dc_lock 1674 * themselves; syncing under it would deadlock). 1675 */ 1676 amdgpu_dm_ism_disable(dm); 1677 scoped_guard(mutex, &dm->dc_lock) 1678 amdgpu_dm_ism_force_full_power(dm); 1679 1680 amdgpu_dm_hpd_rx_irq_work_suspend(dm); 1681 1682 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D3); 1683 1684 if (dm->dc->caps.ips_support && adev->in_s0ix) 1685 dc_allow_idle_optimizations(dm->dc, true); 1686 1687 dc_dmub_srv_set_power_state(dm->dc->ctx->dmub_srv, DC_ACPI_CM_POWER_STATE_D3); 1688 1689 return 0; 1690 } 1691 1692 void amdgpu_dm_emulated_link_detect(struct dc_link *link) 1693 { 1694 struct dc_sink_init_data sink_init_data = { 0 }; 1695 struct display_sink_capability sink_caps = { 0 }; 1696 enum dc_edid_status edid_status; 1697 struct dc_context *dc_ctx = link->ctx; 1698 struct drm_device *dev = adev_to_drm(dc_ctx->driver_context); 1699 struct dc_sink *sink = NULL; 1700 struct dc_sink *prev_sink = NULL; 1701 1702 link->type = dc_connection_none; 1703 prev_sink = link->local_sink; 1704 1705 if (prev_sink) 1706 dc_sink_release(prev_sink); 1707 1708 switch (link->connector_signal) { 1709 case SIGNAL_TYPE_HDMI_TYPE_A: { 1710 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C; 1711 sink_caps.signal = SIGNAL_TYPE_HDMI_TYPE_A; 1712 break; 1713 } 1714 1715 case SIGNAL_TYPE_DVI_SINGLE_LINK: { 1716 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C; 1717 sink_caps.signal = SIGNAL_TYPE_DVI_SINGLE_LINK; 1718 break; 1719 } 1720 1721 case SIGNAL_TYPE_DVI_DUAL_LINK: { 1722 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C; 1723 sink_caps.signal = SIGNAL_TYPE_DVI_DUAL_LINK; 1724 break; 1725 } 1726 1727 case SIGNAL_TYPE_LVDS: { 1728 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C; 1729 sink_caps.signal = SIGNAL_TYPE_LVDS; 1730 break; 1731 } 1732 1733 case SIGNAL_TYPE_EDP: { 1734 sink_caps.transaction_type = 1735 DDC_TRANSACTION_TYPE_I2C_OVER_AUX; 1736 sink_caps.signal = SIGNAL_TYPE_EDP; 1737 break; 1738 } 1739 1740 case SIGNAL_TYPE_DISPLAY_PORT: { 1741 sink_caps.transaction_type = 1742 DDC_TRANSACTION_TYPE_I2C_OVER_AUX; 1743 sink_caps.signal = SIGNAL_TYPE_VIRTUAL; 1744 break; 1745 } 1746 1747 default: 1748 drm_err(dev, "Invalid connector type! signal:%d\n", 1749 link->connector_signal); 1750 return; 1751 } 1752 1753 sink_init_data.link = link; 1754 sink_init_data.sink_signal = sink_caps.signal; 1755 1756 sink = dc_sink_create(&sink_init_data); 1757 if (!sink) { 1758 drm_err(dev, "Failed to create sink!\n"); 1759 return; 1760 } 1761 1762 /* dc_sink_create returns a new reference */ 1763 link->local_sink = sink; 1764 1765 edid_status = dm_helpers_read_local_edid( 1766 link->ctx, 1767 link, 1768 sink); 1769 1770 if (edid_status != EDID_OK) 1771 drm_err(dev, "Failed to read EDID\n"); 1772 1773 } 1774 EXPORT_IF_KUNIT(amdgpu_dm_emulated_link_detect); 1775 1776 STATIC_IFN_KUNIT void dm_gpureset_commit_state(struct dc_state *dc_state, 1777 struct amdgpu_display_manager *dm) 1778 { 1779 struct { 1780 struct dc_surface_update surface_updates[MAX_SURFACES]; 1781 struct dc_plane_info plane_infos[MAX_SURFACES]; 1782 struct dc_scaling_info scaling_infos[MAX_SURFACES]; 1783 struct dc_flip_addrs flip_addrs[MAX_SURFACES]; 1784 struct dc_stream_update stream_update; 1785 } *bundle __free(kfree); 1786 int k, m; 1787 1788 bundle = kzalloc_obj(*bundle); 1789 1790 if (!bundle) { 1791 drm_err(dm->ddev, "Failed to allocate update bundle\n"); 1792 return; 1793 } 1794 1795 for (k = 0; k < dc_state->stream_count; k++) { 1796 bundle->stream_update.stream = dc_state->streams[k]; 1797 1798 for (m = 0; m < dc_state->stream_status[k].plane_count; m++) { 1799 bundle->surface_updates[m].surface = 1800 dc_state->stream_status[k].plane_states[m]; 1801 bundle->surface_updates[m].surface->force_full_update = 1802 true; 1803 } 1804 1805 update_planes_and_stream_adapter(dm->dc, 1806 dc_state->stream_status[k].plane_count, 1807 dc_state->streams[k], 1808 &bundle->stream_update, 1809 bundle->surface_updates); 1810 } 1811 } 1812 EXPORT_IF_KUNIT(dm_gpureset_commit_state); 1813 1814 void amdgpu_dm_apply_delay_after_dpcd_poweroff(struct amdgpu_device *adev, 1815 struct dc_sink *sink) 1816 { 1817 struct dc_panel_patch *ppatch = NULL; 1818 1819 if (!sink) 1820 return; 1821 1822 ppatch = &sink->edid_caps.panel_patch; 1823 if (ppatch->wait_after_dpcd_poweroff_ms) { 1824 msleep(ppatch->wait_after_dpcd_poweroff_ms); 1825 drm_dbg_driver(adev_to_drm(adev), 1826 "%s: adding a %ds delay as w/a for panel\n", 1827 __func__, 1828 ppatch->wait_after_dpcd_poweroff_ms / 1000); 1829 } 1830 } 1831 EXPORT_IF_KUNIT(amdgpu_dm_apply_delay_after_dpcd_poweroff); 1832 1833 /** 1834 * amdgpu_dm_dump_links_and_sinks - Debug dump of all DC links and their sinks 1835 * @adev: amdgpu device pointer 1836 * 1837 * Iterates through all DC links and dumps information about local and remote 1838 * (MST) sinks. Should be called after connector detection is complete to see 1839 * the final state of all links. 1840 */ 1841 STATIC_IFN_KUNIT void amdgpu_dm_dump_links_and_sinks(struct amdgpu_device *adev) 1842 { 1843 struct dc *dc = adev->dm.dc; 1844 struct drm_device *dev = adev_to_drm(adev); 1845 int li; 1846 1847 if (!dc) 1848 return; 1849 1850 for (li = 0; li < dc->link_count; li++) { 1851 struct dc_link *l = dc->links[li]; 1852 const char *name = NULL; 1853 int rs; 1854 1855 if (!l) 1856 continue; 1857 if (l->local_sink && l->local_sink->edid_caps.display_name[0]) 1858 name = l->local_sink->edid_caps.display_name; 1859 else 1860 name = "n/a"; 1861 1862 drm_dbg_kms(dev, 1863 "LINK_DUMP[%d]: local_sink=%p type=%d sink_signal=%d sink_count=%u edid_name=%s mst_capable=%d mst_alloc_streams=%d\n", 1864 li, 1865 l->local_sink, 1866 l->type, 1867 l->local_sink ? l->local_sink->sink_signal : SIGNAL_TYPE_NONE, 1868 l->sink_count, 1869 name, 1870 l->dpcd_caps.is_mst_capable, 1871 l->mst_stream_alloc_table.stream_count); 1872 1873 /* Dump remote (MST) sinks if any */ 1874 for (rs = 0; rs < l->sink_count; rs++) { 1875 struct dc_sink *rsink = l->remote_sinks[rs]; 1876 const char *rname = NULL; 1877 1878 if (!rsink) 1879 continue; 1880 if (rsink->edid_caps.display_name[0]) 1881 rname = rsink->edid_caps.display_name; 1882 else 1883 rname = "n/a"; 1884 drm_dbg_kms(dev, 1885 " REMOTE_SINK[%d:%d]: sink=%p signal=%d edid_name=%s\n", 1886 li, rs, 1887 rsink, 1888 rsink->sink_signal, 1889 rname); 1890 } 1891 } 1892 } 1893 EXPORT_IF_KUNIT(amdgpu_dm_dump_links_and_sinks); 1894 1895 static int dm_resume(struct amdgpu_ip_block *ip_block) 1896 { 1897 struct amdgpu_device *adev = ip_block->adev; 1898 struct drm_device *ddev = adev_to_drm(adev); 1899 struct amdgpu_display_manager *dm = &adev->dm; 1900 struct amdgpu_dm_connector *aconnector; 1901 struct drm_connector *connector; 1902 struct drm_connector_list_iter iter; 1903 struct dm_atomic_state *dm_state = to_dm_atomic_state(dm->atomic_obj.state); 1904 enum dc_connection_type new_connection_type = dc_connection_none; 1905 struct dc_state *dc_state; 1906 int i, r, j; 1907 struct dc_commit_streams_params commit_params = {}; 1908 1909 if (dm->dc->caps.ips_support) { 1910 if (!amdgpu_in_reset(adev)) 1911 mutex_lock(&dm->dc_lock); 1912 1913 /* Need to set POWER_STATE_D0 first or it will not execute 1914 * idle_power_optimizations command to DMUB. 1915 */ 1916 dc_dmub_srv_set_power_state(dm->dc->ctx->dmub_srv, DC_ACPI_CM_POWER_STATE_D0); 1917 dc_dmub_srv_apply_idle_power_optimizations(dm->dc, false); 1918 1919 if (!amdgpu_in_reset(adev)) 1920 mutex_unlock(&dm->dc_lock); 1921 } 1922 1923 if (amdgpu_in_reset(adev)) { 1924 dc_state = dm->cached_dc_state; 1925 1926 /* 1927 * The dc->current_state is backed up into dm->cached_dc_state 1928 * before we commit 0 streams. 1929 * 1930 * DC will clear link encoder assignments on the real state 1931 * but the changes won't propagate over to the copy we made 1932 * before the 0 streams commit. 1933 * 1934 * DC expects that link encoder assignments are *not* valid 1935 * when committing a state, so as a workaround we can copy 1936 * off of the current state. 1937 * 1938 * We lose the previous assignments, but we had already 1939 * commit 0 streams anyway. 1940 */ 1941 link_enc_cfg_copy(adev->dm.dc->current_state, dc_state); 1942 1943 r = dm_dmub_hw_init(adev); 1944 if (r) { 1945 drm_err(adev_to_drm(adev), "DMUB interface failed to initialize: status=%d\n", r); 1946 dc_state_release(dm->cached_dc_state); 1947 dm->cached_dc_state = NULL; 1948 mutex_unlock(&dm->dc_lock); 1949 return r; 1950 } 1951 1952 dc_dmub_srv_set_power_state(dm->dc->ctx->dmub_srv, DC_ACPI_CM_POWER_STATE_D0); 1953 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0); 1954 1955 dc_resume(dm->dc); 1956 1957 amdgpu_dm_ism_enable(dm); 1958 amdgpu_dm_irq_resume_early(adev); 1959 1960 for (i = 0; i < dc_state->stream_count; i++) { 1961 dc_state->streams[i]->mode_changed = true; 1962 for (j = 0; j < dc_state->stream_status[i].plane_count; j++) { 1963 dc_pipe_update_bits_set_full(&dc_state->stream_status[i].plane_states[j]->update_bits); 1964 } 1965 } 1966 1967 if (dc_is_dmub_outbox_supported(adev->dm.dc)) { 1968 amdgpu_dm_outbox_init(adev); 1969 dc_enable_dmub_outbox(adev->dm.dc); 1970 } 1971 1972 commit_params.streams = dc_state->streams; 1973 commit_params.stream_count = dc_state->stream_count; 1974 dc_exit_ips_for_hw_access(dm->dc); 1975 WARN_ON(!dc_commit_streams(dm->dc, &commit_params)); 1976 1977 dm_gpureset_commit_state(dm->cached_dc_state, dm); 1978 1979 dm_gpureset_toggle_interrupts(adev, dm->cached_dc_state, true); 1980 1981 dc_state_release(dm->cached_dc_state); 1982 dm->cached_dc_state = NULL; 1983 1984 amdgpu_dm_irq_resume_late(adev); 1985 1986 mutex_unlock(&dm->dc_lock); 1987 1988 /* set the backlight after a reset */ 1989 for (i = 0; i < dm->num_of_edps; i++) { 1990 if (dm->backlight_dev[i]) 1991 amdgpu_dm_backlight_set_level(dm, i, dm->brightness[i]); 1992 } 1993 1994 return 0; 1995 } 1996 /* Recreate dc_state - DC invalidates it when setting power state to S3. */ 1997 dc_state_release(dm_state->context); 1998 dm_state->context = dc_state_create(dm->dc, NULL); 1999 /* TODO: Remove dc_state->dccg, use dc->dccg directly. */ 2000 2001 /* Before powering on DC we need to re-initialize DMUB. */ 2002 dm_dmub_hw_resume(adev); 2003 2004 /* Re-enable outbox interrupts for DPIA. */ 2005 if (dc_is_dmub_outbox_supported(adev->dm.dc)) { 2006 amdgpu_dm_outbox_init(adev); 2007 dc_enable_dmub_outbox(adev->dm.dc); 2008 } 2009 2010 /* power on hardware */ 2011 dc_dmub_srv_set_power_state(dm->dc->ctx->dmub_srv, DC_ACPI_CM_POWER_STATE_D0); 2012 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0); 2013 2014 /* program HPD filter */ 2015 dc_resume(dm->dc); 2016 2017 scoped_guard(mutex, &dm->dc_lock) 2018 amdgpu_dm_ism_enable(dm); 2019 2020 /* 2021 * early enable HPD Rx IRQ, should be done before set mode as short 2022 * pulse interrupts are used for MST 2023 */ 2024 amdgpu_dm_irq_resume_early(adev); 2025 2026 amdgpu_dm_s3_handle_hdmi_cec(ddev, false); 2027 2028 /* On resume we need to rewrite the MSTM control bits to enable MST*/ 2029 s3_handle_mst(ddev, false); 2030 2031 /* Do detection*/ 2032 drm_connector_list_iter_begin(ddev, &iter); 2033 drm_for_each_connector_iter(connector, &iter) { 2034 bool ret; 2035 2036 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK) 2037 continue; 2038 2039 aconnector = to_amdgpu_dm_connector(connector); 2040 2041 if (!aconnector->dc_link) 2042 continue; 2043 2044 /* 2045 * this is the case when traversing through already created end sink 2046 * MST connectors, should be skipped 2047 */ 2048 if (aconnector->mst_root) 2049 continue; 2050 2051 /* Skip eDP detection, when there is no sink present */ 2052 if (aconnector->dc_link->connector_signal == SIGNAL_TYPE_EDP && 2053 !aconnector->dc_link->edp_sink_present) 2054 continue; 2055 2056 guard(mutex)(&aconnector->hpd_lock); 2057 if (!dc_link_detect_connection_type(aconnector->dc_link, &new_connection_type)) 2058 drm_err(adev_to_drm(adev), "KMS: Failed to detect connector\n"); 2059 2060 if (aconnector->base.force && new_connection_type == dc_connection_none) { 2061 amdgpu_dm_emulated_link_detect(aconnector->dc_link); 2062 } else { 2063 guard(mutex)(&dm->dc_lock); 2064 dc_exit_ips_for_hw_access(dm->dc); 2065 ret = dc_link_detect(aconnector->dc_link, DETECT_REASON_RESUMEFROMS3S4); 2066 if (ret) { 2067 /* w/a delay for certain panels */ 2068 amdgpu_dm_apply_delay_after_dpcd_poweroff(adev, aconnector->dc_sink); 2069 } 2070 } 2071 2072 if (aconnector->fake_enable && aconnector->dc_link->local_sink) 2073 aconnector->fake_enable = false; 2074 2075 if (aconnector->dc_sink) 2076 dc_sink_release(aconnector->dc_sink); 2077 aconnector->dc_sink = NULL; 2078 amdgpu_dm_update_connector_after_detect(aconnector); 2079 } 2080 drm_connector_list_iter_end(&iter); 2081 2082 dm_destroy_cached_state(adev); 2083 2084 /* Do mst topology probing after resuming cached state*/ 2085 drm_connector_list_iter_begin(ddev, &iter); 2086 drm_for_each_connector_iter(connector, &iter) { 2087 bool init = false; 2088 2089 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK) 2090 continue; 2091 2092 aconnector = to_amdgpu_dm_connector(connector); 2093 if (aconnector->dc_link->type != dc_connection_mst_branch || 2094 aconnector->mst_root) 2095 continue; 2096 2097 scoped_guard(mutex, &aconnector->mst_mgr.lock) { 2098 init = !aconnector->mst_mgr.mst_primary; 2099 } 2100 if (init) 2101 dm_helpers_dp_mst_start_top_mgr(aconnector->dc_link->ctx, 2102 aconnector->dc_link, false); 2103 else 2104 drm_dp_mst_topology_queue_probe(&aconnector->mst_mgr); 2105 } 2106 drm_connector_list_iter_end(&iter); 2107 2108 /* Debug dump: list all DC links and their associated sinks after detection 2109 * is complete for all connectors. This provides a comprehensive view of the 2110 * final state without repeating the dump for each connector. 2111 */ 2112 amdgpu_dm_dump_links_and_sinks(adev); 2113 2114 amdgpu_dm_irq_resume_late(adev); 2115 2116 amdgpu_dm_smu_write_watermarks_table(adev); 2117 2118 drm_kms_helper_hotplug_event(ddev); 2119 2120 return 0; 2121 } 2122 2123 /** 2124 * DOC: DM Lifecycle 2125 * 2126 * DM (and consequently DC) is registered in the amdgpu base driver as a IP 2127 * block. When CONFIG_DRM_AMD_DC is enabled, the DM device IP block is added to 2128 * the base driver's device list to be initialized and torn down accordingly. 2129 * 2130 * The functions to do so are provided as hooks in &struct amd_ip_funcs. 2131 */ 2132 2133 static const struct amd_ip_funcs amdgpu_dm_funcs = { 2134 .name = "dm", 2135 .early_init = dm_early_init, 2136 .late_init = dm_late_init, 2137 .sw_init = dm_sw_init, 2138 .sw_fini = dm_sw_fini, 2139 .early_fini = amdgpu_dm_early_fini, 2140 .hw_init = dm_hw_init, 2141 .hw_fini = dm_hw_fini, 2142 .suspend = dm_suspend, 2143 .resume = dm_resume, 2144 .wait_for_idle = dm_wait_for_idle, 2145 .set_clockgating_state = dm_set_clockgating_state, 2146 .set_powergating_state = dm_set_powergating_state, 2147 }; 2148 2149 const struct amdgpu_ip_block_version dm_ip_block = { 2150 .type = AMD_IP_BLOCK_TYPE_DCE, 2151 .major = 1, 2152 .minor = 0, 2153 .rev = 0, 2154 .funcs = &amdgpu_dm_funcs, 2155 }; 2156 2157 2158 /** 2159 * DOC: atomic 2160 * 2161 * *WIP* 2162 */ 2163 2164 static const struct drm_mode_config_funcs amdgpu_dm_mode_funcs = { 2165 .fb_create = amdgpu_display_user_framebuffer_create, 2166 .get_format_info = amdgpu_dm_plane_get_format_info, 2167 .atomic_check = amdgpu_dm_atomic_check, 2168 .atomic_commit = drm_atomic_helper_commit, 2169 }; 2170 2171 static struct drm_mode_config_helper_funcs amdgpu_dm_mode_config_helperfuncs = { 2172 .atomic_commit_tail = amdgpu_dm_atomic_commit_tail, 2173 .atomic_commit_setup = amdgpu_dm_atomic_setup_commit, 2174 }; 2175 2176 /* 2177 * Acquires the lock for the atomic state object and returns 2178 * the new atomic state. 2179 * 2180 * This should only be called during atomic check. 2181 */ 2182 int dm_atomic_get_state(struct drm_atomic_commit *state, 2183 struct dm_atomic_state **dm_state) 2184 { 2185 struct drm_device *dev = state->dev; 2186 struct amdgpu_device *adev = drm_to_adev(dev); 2187 struct amdgpu_display_manager *dm = &adev->dm; 2188 struct drm_private_state *priv_state; 2189 2190 if (*dm_state) 2191 return 0; 2192 2193 priv_state = drm_atomic_get_private_obj_state(state, &dm->atomic_obj); 2194 if (IS_ERR(priv_state)) 2195 return PTR_ERR(priv_state); 2196 2197 *dm_state = to_dm_atomic_state(priv_state); 2198 2199 return 0; 2200 } 2201 EXPORT_IF_KUNIT(dm_atomic_get_state); 2202 2203 STATIC_IFN_KUNIT struct dm_atomic_state * 2204 dm_atomic_get_new_state(struct drm_atomic_commit *state) 2205 { 2206 struct drm_device *dev = state->dev; 2207 struct amdgpu_device *adev = drm_to_adev(dev); 2208 struct amdgpu_display_manager *dm = &adev->dm; 2209 struct drm_private_obj *obj; 2210 struct drm_private_state *new_obj_state; 2211 int i; 2212 2213 for_each_new_private_obj_in_state(state, obj, new_obj_state, i) { 2214 if (obj->funcs == dm->atomic_obj.funcs) 2215 return to_dm_atomic_state(new_obj_state); 2216 } 2217 2218 return NULL; 2219 } 2220 EXPORT_IF_KUNIT(dm_atomic_get_new_state); 2221 2222 STATIC_IFN_KUNIT struct drm_private_state * 2223 dm_atomic_duplicate_state(struct drm_private_obj *obj) 2224 { 2225 struct dm_atomic_state *old_state, *new_state; 2226 2227 new_state = kzalloc_obj(*new_state); 2228 if (!new_state) 2229 return NULL; 2230 2231 __drm_atomic_helper_private_obj_duplicate_state(obj, &new_state->base); 2232 2233 old_state = to_dm_atomic_state(obj->state); 2234 2235 if (old_state && old_state->context) 2236 new_state->context = dc_state_create_copy(old_state->context); 2237 2238 if (!new_state->context) { 2239 kfree(new_state); 2240 return NULL; 2241 } 2242 2243 return &new_state->base; 2244 } 2245 EXPORT_IF_KUNIT(dm_atomic_duplicate_state); 2246 2247 STATIC_IFN_KUNIT void dm_atomic_destroy_state(struct drm_private_obj *obj, 2248 struct drm_private_state *state) 2249 { 2250 struct dm_atomic_state *dm_state = to_dm_atomic_state(state); 2251 2252 if (dm_state && dm_state->context) 2253 dc_state_release(dm_state->context); 2254 2255 kfree(dm_state); 2256 } 2257 EXPORT_IF_KUNIT(dm_atomic_destroy_state); 2258 2259 static struct drm_private_state * 2260 dm_atomic_create_state(struct drm_private_obj *obj) 2261 { 2262 struct amdgpu_device *adev = drm_to_adev(obj->dev); 2263 struct dm_atomic_state *dm_state; 2264 struct dc_state *context; 2265 2266 dm_state = kzalloc_obj(*dm_state); 2267 if (!dm_state) 2268 return ERR_PTR(-ENOMEM); 2269 2270 context = dc_state_create_current_copy(adev->dm.dc); 2271 if (!context) { 2272 kfree(dm_state); 2273 return ERR_PTR(-ENOMEM); 2274 } 2275 2276 __drm_atomic_helper_private_obj_create_state(obj, &dm_state->base); 2277 dm_state->context = context; 2278 2279 return &dm_state->base; 2280 } 2281 2282 static struct drm_private_state_funcs dm_atomic_state_funcs = { 2283 .atomic_create_state = dm_atomic_create_state, 2284 .atomic_duplicate_state = dm_atomic_duplicate_state, 2285 .atomic_destroy_state = dm_atomic_destroy_state, 2286 }; 2287 2288 STATIC_IFN_KUNIT int amdgpu_dm_mode_config_init(struct amdgpu_device *adev) 2289 { 2290 int r; 2291 2292 adev->mode_info.mode_config_initialized = true; 2293 2294 adev_to_drm(adev)->mode_config.funcs = (void *)&amdgpu_dm_mode_funcs; 2295 adev_to_drm(adev)->mode_config.helper_private = &amdgpu_dm_mode_config_helperfuncs; 2296 2297 adev_to_drm(adev)->mode_config.max_width = 16384; 2298 adev_to_drm(adev)->mode_config.max_height = 16384; 2299 2300 adev_to_drm(adev)->mode_config.preferred_depth = 24; 2301 if (adev->asic_type == CHIP_HAWAII) 2302 /* disable prefer shadow for now due to hibernation issues */ 2303 adev_to_drm(adev)->mode_config.prefer_shadow = 0; 2304 else 2305 adev_to_drm(adev)->mode_config.prefer_shadow = 1; 2306 /* indicates support for immediate flip */ 2307 adev_to_drm(adev)->mode_config.async_page_flip = true; 2308 2309 drm_atomic_private_obj_init(adev_to_drm(adev), 2310 &adev->dm.atomic_obj, 2311 &dm_atomic_state_funcs); 2312 2313 r = amdgpu_display_modeset_create_props(adev); 2314 if (r) 2315 return r; 2316 2317 #ifdef AMD_PRIVATE_COLOR 2318 if (amdgpu_dm_create_color_properties(adev)) 2319 return -ENOMEM; 2320 #endif 2321 2322 r = amdgpu_dm_audio_init(adev); 2323 if (r) 2324 return r; 2325 2326 return 0; 2327 } 2328 EXPORT_IF_KUNIT(amdgpu_dm_mode_config_init); 2329 2330 STATIC_IFN_KUNIT int initialize_plane(struct amdgpu_display_manager *dm, 2331 struct amdgpu_mode_info *mode_info, int plane_id, 2332 enum drm_plane_type plane_type, 2333 const struct dc_plane_cap *plane_cap) 2334 { 2335 struct drm_plane *plane; 2336 unsigned long possible_crtcs; 2337 int ret = 0; 2338 2339 plane = kzalloc_obj(struct drm_plane); 2340 if (!plane) { 2341 drm_err(adev_to_drm(dm->adev), "KMS: Failed to allocate plane\n"); 2342 return -ENOMEM; 2343 } 2344 plane->type = plane_type; 2345 2346 /* 2347 * HACK: IGT tests expect that the primary plane for a CRTC 2348 * can only have one possible CRTC. Only expose support for 2349 * any CRTC if they're not going to be used as a primary plane 2350 * for a CRTC - like overlay or underlay planes. 2351 */ 2352 possible_crtcs = 1 << plane_id; 2353 if (plane_id >= dm->dc->caps.max_streams) 2354 possible_crtcs = 0xff; 2355 2356 ret = amdgpu_dm_plane_init(dm, plane, possible_crtcs, plane_cap); 2357 2358 if (ret) { 2359 drm_err(adev_to_drm(dm->adev), "KMS: Failed to initialize plane\n"); 2360 kfree(plane); 2361 return ret; 2362 } 2363 2364 if (mode_info) 2365 mode_info->planes[plane_id] = plane; 2366 2367 return ret; 2368 } 2369 EXPORT_IF_KUNIT(initialize_plane); 2370 2371 2372 /* 2373 * In this architecture, the association 2374 * connector -> encoder -> crtc 2375 * id not really requried. The crtc and connector will hold the 2376 * display_index as an abstraction to use with DAL component 2377 * 2378 * Returns 0 on success 2379 */ 2380 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev) 2381 { 2382 struct amdgpu_display_manager *dm = &adev->dm; 2383 s32 i; 2384 struct amdgpu_dm_connector *aconnector = NULL; 2385 struct amdgpu_encoder *aencoder = NULL; 2386 struct amdgpu_mode_info *mode_info = &adev->mode_info; 2387 u32 link_cnt; 2388 s32 primary_planes; 2389 enum dc_connection_type new_connection_type = dc_connection_none; 2390 const struct dc_plane_cap *plane; 2391 bool psr_feature_enabled = false; 2392 bool replay_feature_enabled = false; 2393 int max_overlay = dm->dc->caps.max_slave_planes; 2394 2395 dm->display_indexes_num = dm->dc->caps.max_streams; 2396 /* Update the actual used number of crtc */ 2397 adev->mode_info.num_crtc = adev->dm.display_indexes_num; 2398 2399 amdgpu_dm_set_irq_funcs(adev); 2400 2401 link_cnt = dm->dc->caps.max_links; 2402 if (amdgpu_dm_mode_config_init(dm->adev)) { 2403 drm_err(adev_to_drm(adev), "DM: Failed to initialize mode config\n"); 2404 return -EINVAL; 2405 } 2406 2407 /* There is one primary plane per CRTC */ 2408 primary_planes = dm->dc->caps.max_streams; 2409 if (primary_planes > AMDGPU_MAX_PLANES) { 2410 drm_err(adev_to_drm(adev), "DM: Plane nums out of 6 planes\n"); 2411 return -EINVAL; 2412 } 2413 2414 /* 2415 * Initialize primary planes, implicit planes for legacy IOCTLS. 2416 * Order is reversed to match iteration order in atomic check. 2417 */ 2418 for (i = (primary_planes - 1); i >= 0; i--) { 2419 plane = &dm->dc->caps.planes[i]; 2420 2421 if (initialize_plane(dm, mode_info, i, 2422 DRM_PLANE_TYPE_PRIMARY, plane)) { 2423 drm_err(adev_to_drm(adev), "KMS: Failed to initialize primary plane\n"); 2424 goto fail; 2425 } 2426 } 2427 2428 /* 2429 * Initialize overlay planes, index starting after primary planes. 2430 * These planes have a higher DRM index than the primary planes since 2431 * they should be considered as having a higher z-order. 2432 * Order is reversed to match iteration order in atomic check. 2433 * 2434 * Only support DCN for now, and only expose one so we don't encourage 2435 * userspace to use up all the pipes. 2436 */ 2437 for (i = 0; i < dm->dc->caps.max_planes; ++i) { 2438 struct dc_plane_cap *plane = &dm->dc->caps.planes[i]; 2439 2440 /* Do not create overlay if MPO disabled */ 2441 if (amdgpu_dc_debug_mask & DC_DISABLE_MPO) 2442 break; 2443 2444 if (plane->type != DC_PLANE_TYPE_DCN_UNIVERSAL) 2445 continue; 2446 2447 if (!plane->pixel_format_support.argb8888) 2448 continue; 2449 2450 if (max_overlay-- == 0) 2451 break; 2452 2453 if (initialize_plane(dm, NULL, primary_planes + i, 2454 DRM_PLANE_TYPE_OVERLAY, plane)) { 2455 drm_err(adev_to_drm(adev), "KMS: Failed to initialize overlay plane\n"); 2456 goto fail; 2457 } 2458 } 2459 2460 for (i = 0; i < dm->dc->caps.max_streams; i++) 2461 if (amdgpu_dm_crtc_init(dm, mode_info->planes[i], i)) { 2462 drm_err(adev_to_drm(adev), "KMS: Failed to initialize crtc\n"); 2463 goto fail; 2464 } 2465 2466 /* Use Outbox interrupt */ 2467 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) { 2468 case IP_VERSION(3, 0, 0): 2469 case IP_VERSION(3, 1, 2): 2470 case IP_VERSION(3, 1, 3): 2471 case IP_VERSION(3, 1, 4): 2472 case IP_VERSION(3, 1, 5): 2473 case IP_VERSION(3, 1, 6): 2474 case IP_VERSION(3, 2, 0): 2475 case IP_VERSION(3, 2, 1): 2476 case IP_VERSION(2, 1, 0): 2477 case IP_VERSION(3, 5, 0): 2478 case IP_VERSION(3, 5, 1): 2479 case IP_VERSION(3, 6, 0): 2480 case IP_VERSION(4, 0, 1): 2481 case IP_VERSION(4, 2, 0): 2482 case IP_VERSION(4, 2, 1): 2483 case IP_VERSION(6, 0, 0): 2484 if (amdgpu_dm_register_outbox_irq_handlers(dm->adev)) { 2485 drm_err(adev_to_drm(adev), "DM: Failed to initialize IRQ\n"); 2486 goto fail; 2487 } 2488 break; 2489 default: 2490 drm_dbg_kms(adev_to_drm(adev), "Unsupported DCN IP version for outbox: 0x%X\n", 2491 amdgpu_ip_version(adev, DCE_HWIP, 0)); 2492 } 2493 2494 /* Determine whether to enable PSR support by default. */ 2495 if (!(amdgpu_dc_debug_mask & DC_DISABLE_PSR)) { 2496 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) { 2497 case IP_VERSION(3, 1, 2): 2498 case IP_VERSION(3, 1, 3): 2499 case IP_VERSION(3, 1, 4): 2500 case IP_VERSION(3, 1, 5): 2501 case IP_VERSION(3, 1, 6): 2502 case IP_VERSION(3, 2, 0): 2503 case IP_VERSION(3, 2, 1): 2504 case IP_VERSION(3, 5, 0): 2505 case IP_VERSION(3, 5, 1): 2506 case IP_VERSION(3, 6, 0): 2507 case IP_VERSION(4, 0, 1): 2508 case IP_VERSION(4, 2, 0): 2509 case IP_VERSION(4, 2, 1): 2510 case IP_VERSION(6, 0, 0): 2511 psr_feature_enabled = true; 2512 break; 2513 default: 2514 psr_feature_enabled = amdgpu_dc_feature_mask & DC_PSR_MASK; 2515 break; 2516 } 2517 } 2518 2519 /* Determine whether to enable Replay support by default. */ 2520 if (!(amdgpu_dc_debug_mask & DC_DISABLE_REPLAY)) { 2521 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) { 2522 case IP_VERSION(3, 1, 4): 2523 case IP_VERSION(3, 2, 0): 2524 case IP_VERSION(3, 2, 1): 2525 case IP_VERSION(3, 5, 0): 2526 case IP_VERSION(3, 5, 1): 2527 case IP_VERSION(3, 6, 0): 2528 case IP_VERSION(4, 2, 0): 2529 case IP_VERSION(4, 2, 1): 2530 case IP_VERSION(6, 0, 0): 2531 replay_feature_enabled = true; 2532 break; 2533 2534 default: 2535 replay_feature_enabled = amdgpu_dc_feature_mask & DC_REPLAY_MASK; 2536 break; 2537 } 2538 } 2539 2540 if (link_cnt > MAX_LINKS) { 2541 drm_err(adev_to_drm(adev), 2542 "KMS: Cannot support more than %d display indexes\n", 2543 MAX_LINKS); 2544 goto fail; 2545 } 2546 2547 /* loops over all connectors on the board */ 2548 for (i = 0; i < link_cnt; i++) { 2549 struct dc_link *link = NULL; 2550 2551 link = dc_get_link_at_index(dm->dc, i); 2552 2553 if (link->connector_signal == SIGNAL_TYPE_VIRTUAL) { 2554 struct amdgpu_dm_wb_connector *wbcon = kzalloc_obj(*wbcon); 2555 2556 if (!wbcon) { 2557 drm_err(adev_to_drm(adev), "KMS: Failed to allocate writeback connector\n"); 2558 continue; 2559 } 2560 2561 if (amdgpu_dm_wb_connector_init(dm, wbcon, i)) { 2562 drm_err(adev_to_drm(adev), "KMS: Failed to initialize writeback connector\n"); 2563 kfree(wbcon); 2564 continue; 2565 } 2566 2567 link->psr_settings.psr_feature_enabled = false; 2568 link->psr_settings.psr_version = DC_PSR_VERSION_UNSUPPORTED; 2569 2570 continue; 2571 } 2572 2573 aconnector = kzalloc_obj(*aconnector); 2574 if (!aconnector) 2575 goto fail; 2576 2577 aencoder = kzalloc_obj(*aencoder); 2578 if (!aencoder) 2579 goto fail; 2580 2581 if (amdgpu_dm_encoder_init(dm->ddev, aencoder, i)) { 2582 drm_err(adev_to_drm(adev), "KMS: Failed to initialize encoder\n"); 2583 goto fail; 2584 } 2585 2586 if (amdgpu_dm_connector_init(dm, aconnector, i, aencoder)) { 2587 drm_err(adev_to_drm(adev), "KMS: Failed to initialize connector\n"); 2588 goto fail; 2589 } 2590 2591 if (dm->hpd_rx_offload_wq) 2592 dm->hpd_rx_offload_wq[aconnector->base.index].aconnector = 2593 aconnector; 2594 2595 if (!dc_link_detect_connection_type(link, &new_connection_type)) 2596 drm_err(adev_to_drm(adev), "KMS: Failed to detect connector\n"); 2597 2598 if (aconnector->base.force && new_connection_type == dc_connection_none) { 2599 amdgpu_dm_emulated_link_detect(link); 2600 amdgpu_dm_update_connector_after_detect(aconnector); 2601 } else { 2602 bool ret = false; 2603 2604 mutex_lock(&dm->dc_lock); 2605 dc_exit_ips_for_hw_access(dm->dc); 2606 ret = dc_link_detect(link, DETECT_REASON_BOOT); 2607 mutex_unlock(&dm->dc_lock); 2608 2609 if (ret) { 2610 amdgpu_dm_update_connector_after_detect(aconnector); 2611 amdgpu_dm_setup_backlight_device(dm, aconnector); 2612 2613 /* Disable PSR if Replay can be enabled */ 2614 if (replay_feature_enabled) 2615 if (amdgpu_dm_set_replay_caps(link, aconnector)) 2616 psr_feature_enabled = false; 2617 2618 if (psr_feature_enabled) { 2619 amdgpu_dm_set_psr_caps(link, aconnector); 2620 drm_info(adev_to_drm(adev), "%s: PSR support %d, DC PSR ver %d, sink PSR ver %d DPCD caps 0x%x su_y_granularity %d\n", 2621 aconnector->base.name, 2622 link->psr_settings.psr_feature_enabled, 2623 link->psr_settings.psr_version, 2624 link->dpcd_caps.psr_info.psr_version, 2625 link->dpcd_caps.psr_info.psr_dpcd_caps.raw, 2626 link->dpcd_caps.psr_info.psr2_su_y_granularity_cap); 2627 } 2628 } 2629 } 2630 amdgpu_set_panel_orientation(&aconnector->base); 2631 } 2632 2633 /* Debug dump: list all DC links and their associated sinks after detection 2634 * is complete for all connectors. This provides a comprehensive view of the 2635 * final state without repeating the dump for each connector. 2636 */ 2637 amdgpu_dm_dump_links_and_sinks(adev); 2638 2639 /* Software is initialized. Now we can register interrupt handlers. */ 2640 switch (adev->asic_type) { 2641 #if defined(CONFIG_DRM_AMD_DC_SI) 2642 case CHIP_TAHITI: 2643 case CHIP_PITCAIRN: 2644 case CHIP_VERDE: 2645 case CHIP_OLAND: 2646 #endif 2647 case CHIP_BONAIRE: 2648 case CHIP_HAWAII: 2649 case CHIP_KAVERI: 2650 case CHIP_KABINI: 2651 case CHIP_MULLINS: 2652 case CHIP_TONGA: 2653 case CHIP_FIJI: 2654 case CHIP_CARRIZO: 2655 case CHIP_STONEY: 2656 case CHIP_POLARIS11: 2657 case CHIP_POLARIS10: 2658 case CHIP_POLARIS12: 2659 case CHIP_VEGAM: 2660 case CHIP_VEGA10: 2661 case CHIP_VEGA12: 2662 case CHIP_VEGA20: 2663 if (amdgpu_dm_dce110_register_irq_handlers(dm->adev)) { 2664 drm_err(adev_to_drm(adev), "DM: Failed to initialize IRQ\n"); 2665 goto fail; 2666 } 2667 break; 2668 default: 2669 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) { 2670 case IP_VERSION(1, 0, 0): 2671 case IP_VERSION(1, 0, 1): 2672 case IP_VERSION(2, 0, 2): 2673 case IP_VERSION(2, 0, 3): 2674 case IP_VERSION(2, 0, 0): 2675 case IP_VERSION(2, 1, 0): 2676 case IP_VERSION(3, 0, 0): 2677 case IP_VERSION(3, 0, 2): 2678 case IP_VERSION(3, 0, 3): 2679 case IP_VERSION(3, 0, 1): 2680 case IP_VERSION(3, 1, 2): 2681 case IP_VERSION(3, 1, 3): 2682 case IP_VERSION(3, 1, 4): 2683 case IP_VERSION(3, 1, 5): 2684 case IP_VERSION(3, 1, 6): 2685 case IP_VERSION(3, 2, 0): 2686 case IP_VERSION(3, 2, 1): 2687 case IP_VERSION(3, 5, 0): 2688 case IP_VERSION(3, 5, 1): 2689 case IP_VERSION(3, 6, 0): 2690 case IP_VERSION(4, 0, 1): 2691 case IP_VERSION(4, 2, 0): 2692 case IP_VERSION(4, 2, 1): 2693 case IP_VERSION(6, 0, 0): 2694 if (amdgpu_dm_dcn10_register_irq_handlers(dm->adev)) { 2695 drm_err(adev_to_drm(adev), "DM: Failed to initialize IRQ\n"); 2696 goto fail; 2697 } 2698 break; 2699 default: 2700 drm_err(adev_to_drm(adev), "Unsupported DCE IP versions: 0x%X\n", 2701 amdgpu_ip_version(adev, DCE_HWIP, 0)); 2702 goto fail; 2703 } 2704 break; 2705 } 2706 2707 return 0; 2708 fail: 2709 kfree(aencoder); 2710 kfree(aconnector); 2711 2712 return -EINVAL; 2713 } 2714 2715 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm) 2716 { 2717 if (dm->atomic_obj.state) 2718 drm_atomic_private_obj_fini(&dm->atomic_obj); 2719 } 2720 2721 /****************************************************************************** 2722 * amdgpu_display_funcs functions 2723 *****************************************************************************/ 2724 2725 /* 2726 * dm_bandwidth_update - program display watermarks 2727 * 2728 * @adev: amdgpu_device pointer 2729 * 2730 * Calculate and program the display watermarks and line buffer allocation. 2731 */ 2732 STATIC_IFN_KUNIT void dm_bandwidth_update(struct amdgpu_device *adev) 2733 { 2734 /* TODO: implement later */ 2735 } 2736 EXPORT_IF_KUNIT(dm_bandwidth_update); 2737 2738 static const struct amdgpu_display_funcs dm_display_funcs = { 2739 .bandwidth_update = dm_bandwidth_update, /* called unconditionally */ 2740 .vblank_get_counter = dm_vblank_get_counter,/* called unconditionally */ 2741 .backlight_set_level = NULL, /* never called for DC */ 2742 .backlight_get_level = NULL, /* never called for DC */ 2743 .hpd_sense = NULL,/* called unconditionally */ 2744 .hpd_set_polarity = NULL, /* called unconditionally */ 2745 .hpd_get_gpio_reg = NULL, /* VBIOS parsing. DAL does it. */ 2746 .page_flip_get_scanoutpos = 2747 dm_crtc_get_scanoutpos,/* called unconditionally */ 2748 .add_encoder = NULL, /* VBIOS parsing. DAL does it. */ 2749 .add_connector = NULL, /* VBIOS parsing. DAL does it. */ 2750 }; 2751 2752 #if defined(CONFIG_DEBUG_KERNEL_DC) 2753 2754 static ssize_t s3_debug_store(struct device *device, 2755 struct device_attribute *attr, 2756 const char *buf, 2757 size_t count) 2758 { 2759 int ret; 2760 int s3_state; 2761 struct drm_device *drm_dev = dev_get_drvdata(device); 2762 struct amdgpu_device *adev = drm_to_adev(drm_dev); 2763 struct amdgpu_ip_block *ip_block; 2764 2765 ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_DCE); 2766 if (!ip_block) 2767 return -EINVAL; 2768 2769 ret = kstrtoint(buf, 0, &s3_state); 2770 2771 if (ret == 0) { 2772 if (s3_state) { 2773 dm_resume(ip_block); 2774 drm_kms_helper_hotplug_event(adev_to_drm(adev)); 2775 } else 2776 dm_suspend(ip_block); 2777 } 2778 2779 return ret == 0 ? count : 0; 2780 } 2781 2782 DEVICE_ATTR_WO(s3_debug); 2783 2784 #endif 2785 2786 STATIC_IFN_KUNIT int dm_early_init(struct amdgpu_ip_block *ip_block) 2787 { 2788 struct amdgpu_device *adev = ip_block->adev; 2789 struct amdgpu_mode_info *mode_info = &adev->mode_info; 2790 struct atom_context *ctx = mode_info->atom_context; 2791 int index = GetIndexIntoMasterTable(DATA, Object_Header); 2792 u16 data_offset; 2793 2794 /* if there is no object header, skip DM */ 2795 if (!amdgpu_atom_parse_data_header(ctx, index, NULL, NULL, NULL, &data_offset)) { 2796 adev->harvest_ip_mask |= AMD_HARVEST_IP_DMU_MASK; 2797 drm_info(adev_to_drm(adev), "No object header, skipping DM\n"); 2798 return -ENOENT; 2799 } 2800 2801 switch (adev->asic_type) { 2802 #if defined(CONFIG_DRM_AMD_DC_SI) 2803 case CHIP_TAHITI: 2804 case CHIP_PITCAIRN: 2805 case CHIP_VERDE: 2806 adev->mode_info.num_crtc = 6; 2807 adev->mode_info.num_hpd = 6; 2808 adev->mode_info.num_dig = 6; 2809 break; 2810 case CHIP_OLAND: 2811 adev->mode_info.num_crtc = 2; 2812 adev->mode_info.num_hpd = 2; 2813 adev->mode_info.num_dig = 2; 2814 break; 2815 #endif 2816 case CHIP_BONAIRE: 2817 case CHIP_HAWAII: 2818 adev->mode_info.num_crtc = 6; 2819 adev->mode_info.num_hpd = 6; 2820 adev->mode_info.num_dig = 6; 2821 break; 2822 case CHIP_KAVERI: 2823 adev->mode_info.num_crtc = 4; 2824 adev->mode_info.num_hpd = 6; 2825 adev->mode_info.num_dig = 7; 2826 break; 2827 case CHIP_KABINI: 2828 case CHIP_MULLINS: 2829 adev->mode_info.num_crtc = 2; 2830 adev->mode_info.num_hpd = 6; 2831 adev->mode_info.num_dig = 6; 2832 break; 2833 case CHIP_FIJI: 2834 case CHIP_TONGA: 2835 adev->mode_info.num_crtc = 6; 2836 adev->mode_info.num_hpd = 6; 2837 adev->mode_info.num_dig = 7; 2838 break; 2839 case CHIP_CARRIZO: 2840 adev->mode_info.num_crtc = 3; 2841 adev->mode_info.num_hpd = 6; 2842 adev->mode_info.num_dig = 9; 2843 break; 2844 case CHIP_STONEY: 2845 adev->mode_info.num_crtc = 2; 2846 adev->mode_info.num_hpd = 6; 2847 adev->mode_info.num_dig = 9; 2848 break; 2849 case CHIP_POLARIS11: 2850 case CHIP_POLARIS12: 2851 adev->mode_info.num_crtc = 5; 2852 adev->mode_info.num_hpd = 5; 2853 adev->mode_info.num_dig = 5; 2854 break; 2855 case CHIP_POLARIS10: 2856 case CHIP_VEGAM: 2857 adev->mode_info.num_crtc = 6; 2858 adev->mode_info.num_hpd = 6; 2859 adev->mode_info.num_dig = 6; 2860 break; 2861 case CHIP_VEGA10: 2862 case CHIP_VEGA12: 2863 case CHIP_VEGA20: 2864 adev->mode_info.num_crtc = 6; 2865 adev->mode_info.num_hpd = 6; 2866 adev->mode_info.num_dig = 6; 2867 break; 2868 default: 2869 2870 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) { 2871 case IP_VERSION(2, 0, 2): 2872 case IP_VERSION(3, 0, 0): 2873 adev->mode_info.num_crtc = 6; 2874 adev->mode_info.num_hpd = 6; 2875 adev->mode_info.num_dig = 6; 2876 break; 2877 case IP_VERSION(2, 0, 0): 2878 case IP_VERSION(3, 0, 2): 2879 adev->mode_info.num_crtc = 5; 2880 adev->mode_info.num_hpd = 5; 2881 adev->mode_info.num_dig = 5; 2882 break; 2883 case IP_VERSION(2, 0, 3): 2884 case IP_VERSION(3, 0, 3): 2885 adev->mode_info.num_crtc = 2; 2886 adev->mode_info.num_hpd = 2; 2887 adev->mode_info.num_dig = 2; 2888 break; 2889 case IP_VERSION(1, 0, 0): 2890 case IP_VERSION(1, 0, 1): 2891 case IP_VERSION(3, 0, 1): 2892 case IP_VERSION(2, 1, 0): 2893 case IP_VERSION(3, 1, 2): 2894 case IP_VERSION(3, 1, 3): 2895 case IP_VERSION(3, 1, 4): 2896 case IP_VERSION(3, 1, 5): 2897 case IP_VERSION(3, 1, 6): 2898 case IP_VERSION(3, 2, 0): 2899 case IP_VERSION(3, 2, 1): 2900 case IP_VERSION(3, 5, 0): 2901 case IP_VERSION(3, 5, 1): 2902 case IP_VERSION(3, 6, 0): 2903 case IP_VERSION(4, 0, 1): 2904 case IP_VERSION(4, 2, 0): 2905 case IP_VERSION(4, 2, 1): 2906 case IP_VERSION(6, 0, 0): 2907 adev->mode_info.num_crtc = 4; 2908 adev->mode_info.num_hpd = 4; 2909 adev->mode_info.num_dig = 4; 2910 break; 2911 default: 2912 drm_err(adev_to_drm(adev), "Unsupported DCE IP versions: 0x%x\n", 2913 amdgpu_ip_version(adev, DCE_HWIP, 0)); 2914 return -EINVAL; 2915 } 2916 break; 2917 } 2918 2919 if (adev->mode_info.funcs == NULL) 2920 adev->mode_info.funcs = &dm_display_funcs; 2921 2922 /* 2923 * Note: Do NOT change adev->reg.audio_endpt.rreg and 2924 * adev->reg.audio_endpt.wreg because they are initialised in 2925 * amdgpu_device_init() 2926 */ 2927 #if defined(CONFIG_DEBUG_KERNEL_DC) 2928 device_create_file( 2929 adev_to_drm(adev)->dev, 2930 &dev_attr_s3_debug); 2931 #endif 2932 adev->dc_enabled = true; 2933 2934 return dm_init_microcode(adev); 2935 } 2936 EXPORT_IF_KUNIT(dm_early_init); 2937 2938 STATIC_IFN_KUNIT bool modereset_required(struct drm_crtc_state *crtc_state) 2939 { 2940 return !crtc_state->active && drm_atomic_crtc_needs_modeset(crtc_state); 2941 } 2942 EXPORT_IF_KUNIT(modereset_required); 2943 2944 STATIC_IFN_KUNIT int 2945 fill_plane_color_attributes(struct drm_atomic_commit *state, 2946 const struct drm_plane_state *plane_state, 2947 const enum surface_pixel_format format, 2948 enum dc_color_space *color_space) 2949 { 2950 bool full_range; 2951 2952 *color_space = COLOR_SPACE_SRGB; 2953 2954 /* Ignore properties when DRM_CLIENT_CAP_PLANE_COLOR_PIPELINE is set */ 2955 if (state && state->plane_color_pipeline) 2956 return 0; 2957 2958 /* DRM color properties only affect non-RGB formats. */ 2959 if (format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) 2960 return 0; 2961 2962 full_range = (plane_state->color_range == DRM_COLOR_YCBCR_FULL_RANGE); 2963 2964 switch (plane_state->color_encoding) { 2965 case DRM_COLOR_YCBCR_BT601: 2966 if (full_range) 2967 *color_space = COLOR_SPACE_YCBCR601; 2968 else 2969 *color_space = COLOR_SPACE_YCBCR601_LIMITED; 2970 break; 2971 2972 case DRM_COLOR_YCBCR_BT709: 2973 if (full_range) 2974 *color_space = COLOR_SPACE_YCBCR709; 2975 else 2976 *color_space = COLOR_SPACE_YCBCR709_LIMITED; 2977 break; 2978 2979 case DRM_COLOR_YCBCR_BT2020: 2980 if (full_range) 2981 *color_space = COLOR_SPACE_2020_YCBCR_FULL; 2982 else 2983 *color_space = COLOR_SPACE_2020_YCBCR_LIMITED; 2984 break; 2985 2986 default: 2987 return -EINVAL; 2988 } 2989 2990 return 0; 2991 } 2992 EXPORT_IF_KUNIT(fill_plane_color_attributes); 2993 2994 STATIC_IFN_KUNIT int 2995 fill_dc_plane_info_and_addr(struct amdgpu_device *adev, 2996 struct drm_atomic_commit *state, 2997 const struct drm_plane_state *plane_state, 2998 struct dc_plane_info *plane_info, 2999 struct dc_plane_address *address, 3000 bool tmz_surface) 3001 { 3002 const struct drm_framebuffer *fb = plane_state->fb; 3003 const struct amdgpu_framebuffer *afb = 3004 to_amdgpu_framebuffer(plane_state->fb); 3005 int ret; 3006 3007 memset(plane_info, 0, sizeof(*plane_info)); 3008 3009 switch (fb->format->format) { 3010 case DRM_FORMAT_C8: 3011 plane_info->format = 3012 SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS; 3013 break; 3014 case DRM_FORMAT_RGB565: 3015 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_RGB565; 3016 break; 3017 case DRM_FORMAT_XRGB8888: 3018 case DRM_FORMAT_ARGB8888: 3019 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888; 3020 break; 3021 case DRM_FORMAT_RGBA8888: 3022 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_RGBA8888; 3023 break; 3024 case DRM_FORMAT_XRGB2101010: 3025 case DRM_FORMAT_ARGB2101010: 3026 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010; 3027 break; 3028 case DRM_FORMAT_XBGR2101010: 3029 case DRM_FORMAT_ABGR2101010: 3030 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010; 3031 break; 3032 case DRM_FORMAT_XBGR8888: 3033 case DRM_FORMAT_ABGR8888: 3034 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR8888; 3035 break; 3036 case DRM_FORMAT_NV21: 3037 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr; 3038 break; 3039 case DRM_FORMAT_NV12: 3040 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb; 3041 break; 3042 case DRM_FORMAT_P010: 3043 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb; 3044 break; 3045 case DRM_FORMAT_XRGB16161616F: 3046 case DRM_FORMAT_ARGB16161616F: 3047 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F; 3048 break; 3049 case DRM_FORMAT_XBGR16161616F: 3050 case DRM_FORMAT_ABGR16161616F: 3051 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F; 3052 break; 3053 case DRM_FORMAT_XRGB16161616: 3054 case DRM_FORMAT_ARGB16161616: 3055 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616; 3056 break; 3057 case DRM_FORMAT_XBGR16161616: 3058 case DRM_FORMAT_ABGR16161616: 3059 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616; 3060 break; 3061 default: 3062 drm_err(adev_to_drm(adev), 3063 "Unsupported screen format %p4cc\n", 3064 &fb->format->format); 3065 return -EINVAL; 3066 } 3067 3068 switch (plane_state->rotation & DRM_MODE_ROTATE_MASK) { 3069 case DRM_MODE_ROTATE_0: 3070 plane_info->rotation = ROTATION_ANGLE_0; 3071 break; 3072 case DRM_MODE_ROTATE_90: 3073 plane_info->rotation = ROTATION_ANGLE_90; 3074 break; 3075 case DRM_MODE_ROTATE_180: 3076 plane_info->rotation = ROTATION_ANGLE_180; 3077 break; 3078 case DRM_MODE_ROTATE_270: 3079 plane_info->rotation = ROTATION_ANGLE_270; 3080 break; 3081 default: 3082 plane_info->rotation = ROTATION_ANGLE_0; 3083 break; 3084 } 3085 3086 3087 plane_info->visible = true; 3088 plane_info->stereo_format = PLANE_STEREO_FORMAT_NONE; 3089 3090 plane_info->layer_index = plane_state->normalized_zpos; 3091 3092 ret = fill_plane_color_attributes(state, plane_state, plane_info->format, 3093 &plane_info->color_space); 3094 if (ret) 3095 return ret; 3096 3097 ret = amdgpu_dm_plane_fill_plane_buffer_attributes(adev, afb, plane_info->format, 3098 plane_info->rotation, 3099 &plane_info->tiling_info, 3100 &plane_info->plane_size, 3101 &plane_info->dcc, address, 3102 tmz_surface); 3103 if (ret) 3104 return ret; 3105 3106 amdgpu_dm_plane_fill_blending_from_plane_state( 3107 plane_state, &plane_info->per_pixel_alpha, &plane_info->pre_multiplied_alpha, 3108 &plane_info->global_alpha, &plane_info->global_alpha_value); 3109 3110 return 0; 3111 } 3112 EXPORT_IF_KUNIT(fill_dc_plane_info_and_addr); 3113 3114 STATIC_IFN_KUNIT int fill_dc_plane_attributes(struct amdgpu_device *adev, 3115 struct dc_plane_state *dc_plane_state, 3116 struct drm_plane_state *plane_state, 3117 struct drm_crtc_state *crtc_state) 3118 { 3119 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state); 3120 struct amdgpu_framebuffer *afb = (struct amdgpu_framebuffer *)plane_state->fb; 3121 struct dc_scaling_info scaling_info; 3122 struct dc_plane_info plane_info; 3123 int ret; 3124 3125 ret = amdgpu_dm_plane_fill_dc_scaling_info(adev, plane_state, &scaling_info); 3126 if (ret) 3127 return ret; 3128 3129 dc_plane_state->src_rect = scaling_info.src_rect; 3130 dc_plane_state->dst_rect = scaling_info.dst_rect; 3131 dc_plane_state->clip_rect = scaling_info.clip_rect; 3132 dc_plane_state->scaling_quality = scaling_info.scaling_quality; 3133 3134 ret = fill_dc_plane_info_and_addr(adev, plane_state->state, plane_state, 3135 &plane_info, 3136 &dc_plane_state->address, 3137 afb->tmz_surface); 3138 if (ret) 3139 return ret; 3140 3141 dc_plane_state->format = plane_info.format; 3142 dc_plane_state->color_space = plane_info.color_space; 3143 dc_plane_state->format = plane_info.format; 3144 dc_plane_state->plane_size = plane_info.plane_size; 3145 dc_plane_state->rotation = plane_info.rotation; 3146 dc_plane_state->horizontal_mirror = plane_info.horizontal_mirror; 3147 dc_plane_state->stereo_format = plane_info.stereo_format; 3148 dc_plane_state->tiling_info = plane_info.tiling_info; 3149 dc_plane_state->visible = plane_info.visible; 3150 dc_plane_state->per_pixel_alpha = plane_info.per_pixel_alpha; 3151 dc_plane_state->pre_multiplied_alpha = plane_info.pre_multiplied_alpha; 3152 dc_plane_state->global_alpha = plane_info.global_alpha; 3153 dc_plane_state->global_alpha_value = plane_info.global_alpha_value; 3154 dc_plane_state->dcc = plane_info.dcc; 3155 dc_plane_state->layer_index = plane_info.layer_index; 3156 dc_plane_state->flip_int_enabled = true; 3157 3158 /* 3159 * Always set input transfer function, since plane state is refreshed 3160 * every time. 3161 */ 3162 ret = amdgpu_dm_update_plane_color_mgmt(dm_crtc_state, 3163 plane_state, 3164 dc_plane_state); 3165 if (ret) 3166 return ret; 3167 3168 return 0; 3169 } 3170 EXPORT_IF_KUNIT(fill_dc_plane_attributes); 3171 3172 static inline void fill_dc_dirty_rect(struct drm_plane *plane, 3173 struct rect *dirty_rect, int32_t x, 3174 s32 y, s32 width, s32 height, 3175 int *i, bool ffu) 3176 { 3177 WARN_ON(*i >= DC_MAX_DIRTY_RECTS); 3178 3179 dirty_rect->x = x; 3180 dirty_rect->y = y; 3181 dirty_rect->width = width; 3182 dirty_rect->height = height; 3183 3184 if (ffu) 3185 drm_dbg(plane->dev, 3186 "[PLANE:%d] PSR FFU dirty rect size (%d, %d)\n", 3187 plane->base.id, width, height); 3188 else 3189 drm_dbg(plane->dev, 3190 "[PLANE:%d] PSR SU dirty rect at (%d, %d) size (%d, %d)", 3191 plane->base.id, x, y, width, height); 3192 3193 (*i)++; 3194 } 3195 3196 /** 3197 * fill_dc_dirty_rects() - Fill DC dirty regions for PSR selective updates 3198 * 3199 * @plane: DRM plane containing dirty regions that need to be flushed to the eDP 3200 * remote fb 3201 * @old_plane_state: Old state of @plane 3202 * @new_plane_state: New state of @plane 3203 * @crtc_state: New state of CRTC connected to the @plane 3204 * @flip_addrs: DC flip tracking struct, which also tracts dirty rects 3205 * @is_psr_su: Flag indicating whether Panel Self Refresh Selective Update (PSR SU) is enabled. 3206 * If PSR SU is enabled and damage clips are available, only the regions of the screen 3207 * that have changed will be updated. If PSR SU is not enabled, 3208 * or if damage clips are not available, the entire screen will be updated. 3209 * @dirty_regions_changed: dirty regions changed 3210 * 3211 * For PSR SU, DC informs the DMUB uController of dirty rectangle regions 3212 * (referred to as "damage clips" in DRM nomenclature) that require updating on 3213 * the eDP remote buffer. The responsibility of specifying the dirty regions is 3214 * amdgpu_dm's. 3215 * 3216 * A damage-aware DRM client should fill the FB_DAMAGE_CLIPS property on the 3217 * plane with regions that require flushing to the eDP remote buffer. In 3218 * addition, certain use cases - such as cursor and multi-plane overlay (MPO) - 3219 * implicitly provide damage clips without any client support via the plane 3220 * bounds. 3221 */ 3222 STATIC_IFN_KUNIT void fill_dc_dirty_rects(struct drm_plane *plane, 3223 struct drm_plane_state *old_plane_state, 3224 struct drm_plane_state *new_plane_state, 3225 struct drm_crtc_state *crtc_state, 3226 struct dc_flip_addrs *flip_addrs, 3227 bool is_psr_su, 3228 bool *dirty_regions_changed) 3229 { 3230 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state); 3231 struct rect *dirty_rects = flip_addrs->dirty_rects; 3232 u32 num_clips = 0; 3233 struct drm_mode_rect *clips = NULL; 3234 bool bb_changed; 3235 bool fb_changed; 3236 u32 i = 0; 3237 *dirty_regions_changed = false; 3238 3239 /* 3240 * Cursor plane has it's own dirty rect update interface. See 3241 * dcn10_dmub_update_cursor_data and dmub_cmd_update_cursor_info_data 3242 */ 3243 if (plane->type == DRM_PLANE_TYPE_CURSOR) 3244 return; 3245 3246 if (new_plane_state->rotation != DRM_MODE_ROTATE_0) 3247 goto ffu; 3248 3249 if (!new_plane_state->ignore_damage_clips) { 3250 num_clips = drm_plane_get_damage_clips_count(new_plane_state); 3251 clips = drm_plane_get_damage_clips(new_plane_state); 3252 } 3253 3254 if (num_clips && (!amdgpu_damage_clips || (amdgpu_damage_clips < 0 && 3255 is_psr_su))) 3256 goto ffu; 3257 3258 if (!dm_crtc_state->mpo_requested) { 3259 if (!num_clips || num_clips > DC_MAX_DIRTY_RECTS) 3260 goto ffu; 3261 3262 for (; flip_addrs->dirty_rect_count < num_clips; clips++) 3263 fill_dc_dirty_rect(new_plane_state->plane, 3264 &dirty_rects[flip_addrs->dirty_rect_count], 3265 clips->x1, clips->y1, 3266 clips->x2 - clips->x1, clips->y2 - clips->y1, 3267 &flip_addrs->dirty_rect_count, 3268 false); 3269 return; 3270 } 3271 3272 /* 3273 * MPO is requested. Add entire plane bounding box to dirty rects if 3274 * flipped to or damaged. 3275 * 3276 * If plane is moved or resized, also add old bounding box to dirty 3277 * rects. 3278 */ 3279 fb_changed = old_plane_state->fb->base.id != 3280 new_plane_state->fb->base.id; 3281 bb_changed = (old_plane_state->crtc_x != new_plane_state->crtc_x || 3282 old_plane_state->crtc_y != new_plane_state->crtc_y || 3283 old_plane_state->crtc_w != new_plane_state->crtc_w || 3284 old_plane_state->crtc_h != new_plane_state->crtc_h); 3285 3286 drm_dbg(plane->dev, 3287 "[PLANE:%d] PSR bb_changed:%d fb_changed:%d num_clips:%d\n", 3288 new_plane_state->plane->base.id, 3289 bb_changed, fb_changed, num_clips); 3290 3291 *dirty_regions_changed = bb_changed; 3292 3293 if ((num_clips + (bb_changed ? 2 : 0)) > DC_MAX_DIRTY_RECTS) 3294 goto ffu; 3295 3296 if (bb_changed) { 3297 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[i], 3298 new_plane_state->crtc_x, 3299 new_plane_state->crtc_y, 3300 new_plane_state->crtc_w, 3301 new_plane_state->crtc_h, &i, false); 3302 3303 /* Add old plane bounding-box if plane is moved or resized */ 3304 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[i], 3305 old_plane_state->crtc_x, 3306 old_plane_state->crtc_y, 3307 old_plane_state->crtc_w, 3308 old_plane_state->crtc_h, &i, false); 3309 } 3310 3311 if (num_clips) { 3312 for (; i < num_clips; clips++) 3313 fill_dc_dirty_rect(new_plane_state->plane, 3314 &dirty_rects[i], clips->x1, 3315 clips->y1, clips->x2 - clips->x1, 3316 clips->y2 - clips->y1, &i, false); 3317 } else if (fb_changed && !bb_changed) { 3318 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[i], 3319 new_plane_state->crtc_x, 3320 new_plane_state->crtc_y, 3321 new_plane_state->crtc_w, 3322 new_plane_state->crtc_h, &i, false); 3323 } 3324 3325 flip_addrs->dirty_rect_count = i; 3326 return; 3327 3328 ffu: 3329 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[0], 0, 0, 3330 dm_crtc_state->base.mode.crtc_hdisplay, 3331 dm_crtc_state->base.mode.crtc_vdisplay, 3332 &flip_addrs->dirty_rect_count, true); 3333 } 3334 EXPORT_IF_KUNIT(fill_dc_dirty_rects); 3335 3336 STATIC_IFN_KUNIT int dm_update_mst_vcpi_slots_for_dsc(struct drm_atomic_commit *state, 3337 struct dc_state *dc_state, 3338 struct dsc_mst_fairness_vars *vars) 3339 { 3340 struct dc_stream_state *stream = NULL; 3341 struct drm_connector *connector; 3342 struct drm_connector_state *new_con_state; 3343 struct amdgpu_dm_connector *aconnector; 3344 struct dm_connector_state *dm_conn_state; 3345 int i, j, ret; 3346 int vcpi, pbn_div, pbn = 0, slot_num = 0; 3347 3348 for_each_new_connector_in_state(state, connector, new_con_state, i) { 3349 3350 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK) 3351 continue; 3352 3353 aconnector = to_amdgpu_dm_connector(connector); 3354 3355 if (!aconnector->mst_output_port) 3356 continue; 3357 3358 if (!new_con_state || !new_con_state->crtc) 3359 continue; 3360 3361 dm_conn_state = to_dm_connector_state(new_con_state); 3362 3363 for (j = 0; j < dc_state->stream_count; j++) { 3364 stream = dc_state->streams[j]; 3365 if (!stream) 3366 continue; 3367 3368 if ((struct amdgpu_dm_connector *)stream->dm_stream_context == aconnector) 3369 break; 3370 3371 stream = NULL; 3372 } 3373 3374 if (!stream) 3375 continue; 3376 3377 pbn_div = dm_mst_get_pbn_divider(stream->link); 3378 /* pbn is calculated by compute_mst_dsc_configs_for_state*/ 3379 for (j = 0; j < dc_state->stream_count; j++) { 3380 if (vars[j].aconnector == aconnector) { 3381 pbn = vars[j].pbn; 3382 break; 3383 } 3384 } 3385 3386 if (j == dc_state->stream_count || pbn_div == 0) 3387 continue; 3388 3389 slot_num = DIV_ROUND_UP(pbn, pbn_div); 3390 3391 if (stream->timing.flags.DSC != 1) { 3392 dm_conn_state->pbn = pbn; 3393 dm_conn_state->vcpi_slots = slot_num; 3394 3395 ret = drm_dp_mst_atomic_enable_dsc(state, aconnector->mst_output_port, 3396 dm_conn_state->pbn, false); 3397 if (ret < 0) 3398 return ret; 3399 3400 continue; 3401 } 3402 3403 vcpi = drm_dp_mst_atomic_enable_dsc(state, aconnector->mst_output_port, pbn, true); 3404 if (vcpi < 0) 3405 return vcpi; 3406 3407 dm_conn_state->pbn = pbn; 3408 dm_conn_state->vcpi_slots = vcpi; 3409 } 3410 return 0; 3411 } 3412 EXPORT_IF_KUNIT(dm_update_mst_vcpi_slots_for_dsc); 3413 3414 STATIC_IFN_KUNIT void manage_dm_interrupts(struct amdgpu_device *adev, 3415 struct amdgpu_crtc *acrtc, 3416 struct dm_crtc_state *acrtc_state) 3417 { /* 3418 * We cannot be sure that the frontend index maps to the same 3419 * backend index - some even map to more than one. 3420 * So we have to go through the CRTC to find the right IRQ. 3421 */ 3422 int irq_type = amdgpu_display_crtc_idx_to_irq_type( 3423 adev, 3424 acrtc->crtc_id); 3425 struct drm_device *dev = adev_to_drm(adev); 3426 3427 struct drm_vblank_crtc_config config = {0}; 3428 struct dc_crtc_timing *timing; 3429 int offdelay; 3430 3431 if (acrtc_state) { 3432 timing = &acrtc_state->stream->timing; 3433 3434 if (amdgpu_ip_version(adev, DCE_HWIP, 0) < 3435 IP_VERSION(3, 5, 0) || 3436 !(adev->flags & AMD_IS_APU)) { 3437 /* 3438 * Older HW and DGPU have issues with instant off; 3439 * use a 2 frame offdelay. 3440 */ 3441 offdelay = DIV64_U64_ROUND_UP((u64)20 * 3442 timing->v_total * 3443 timing->h_total, 3444 timing->pix_clk_100hz); 3445 3446 config.offdelay_ms = offdelay ?: 30; 3447 } else { 3448 /* offdelay_ms = 0 will never disable vblank */ 3449 config.offdelay_ms = 1; 3450 config.disable_immediate = true; 3451 } 3452 3453 drm_crtc_vblank_on_config(&acrtc->base, 3454 &config); 3455 /* 3456 * Since pflip_high_irq is no longer registered for DCN, grab an 3457 * extra reference to vupdate irq instead to workaround this 3458 * issue: 3459 * https://gitlab.freedesktop.org/drm/amd/-/work_items/3936 3460 * 3461 * The callbacks to drm_vblank_on/off should really take care of 3462 * this though. 3463 */ 3464 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) { 3465 case IP_VERSION(3, 0, 0): 3466 case IP_VERSION(3, 0, 2): 3467 case IP_VERSION(3, 0, 3): 3468 case IP_VERSION(3, 2, 0): 3469 if (amdgpu_irq_get(adev, &adev->vupdate_irq, irq_type)) 3470 drm_err(dev, "DM_IRQ: Cannot get vupdate irq!\n"); 3471 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY) 3472 if (amdgpu_irq_get(adev, &adev->vline0_irq, irq_type)) 3473 drm_err(dev, "DM_IRQ: Cannot get vline0 irq!\n"); 3474 #endif 3475 } 3476 3477 } else { 3478 /* Allow RX6xxx, RX7700, RX7800 GPUs to call amdgpu_irq_put.*/ 3479 switch (amdgpu_ip_version(adev, DCE_HWIP, 0)) { 3480 case IP_VERSION(3, 0, 0): 3481 case IP_VERSION(3, 0, 2): 3482 case IP_VERSION(3, 0, 3): 3483 case IP_VERSION(3, 2, 0): 3484 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY) 3485 if (amdgpu_irq_put(adev, &adev->vline0_irq, irq_type)) 3486 drm_err(dev, "DM_IRQ: Cannot put vline0 irq!\n"); 3487 #endif 3488 if (amdgpu_irq_put(adev, &adev->vupdate_irq, irq_type)) 3489 drm_err(dev, "DM_IRQ: Cannot put vupdate irq!\n"); 3490 } 3491 3492 drm_crtc_vblank_off(&acrtc->base); 3493 } 3494 } 3495 EXPORT_IF_KUNIT(manage_dm_interrupts); 3496 3497 STATIC_IFN_KUNIT void dm_update_pflip_irq_state(struct amdgpu_device *adev, 3498 struct amdgpu_crtc *acrtc) 3499 { 3500 int irq_type = 3501 amdgpu_display_crtc_idx_to_irq_type(adev, acrtc->crtc_id); 3502 3503 /* GRPH_PFLIP is not used on DCN; nothing to reapply. */ 3504 if (amdgpu_ip_version(adev, DCE_HWIP, 0) != 0) 3505 return; 3506 3507 /** 3508 * This reads the current state for the IRQ and force reapplies 3509 * the setting to hardware. 3510 */ 3511 amdgpu_irq_update(adev, &adev->pageflip_irq, irq_type); 3512 } 3513 EXPORT_IF_KUNIT(dm_update_pflip_irq_state); 3514 3515 STATIC_IFN_KUNIT bool 3516 is_scaling_state_different(const struct dm_connector_state *dm_state, 3517 const struct dm_connector_state *old_dm_state) 3518 { 3519 if (dm_state->scaling != old_dm_state->scaling) 3520 return true; 3521 if (!dm_state->underscan_enable && old_dm_state->underscan_enable) { 3522 if (old_dm_state->underscan_hborder != 0 && old_dm_state->underscan_vborder != 0) 3523 return true; 3524 } else if (dm_state->underscan_enable && !old_dm_state->underscan_enable) { 3525 if (dm_state->underscan_hborder != 0 && dm_state->underscan_vborder != 0) 3526 return true; 3527 } else if (dm_state->underscan_hborder != old_dm_state->underscan_hborder || 3528 dm_state->underscan_vborder != old_dm_state->underscan_vborder) 3529 return true; 3530 return false; 3531 } 3532 EXPORT_IF_KUNIT(is_scaling_state_different); 3533 3534 STATIC_IFN_KUNIT bool 3535 is_content_protection_different(struct drm_crtc_state *new_crtc_state, 3536 struct drm_crtc_state *old_crtc_state, 3537 struct drm_connector_state *new_conn_state, 3538 struct drm_connector_state *old_conn_state, 3539 const struct drm_connector *connector, 3540 struct hdcp_workqueue *hdcp_w) 3541 { 3542 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector); 3543 struct dm_connector_state *dm_con_state = to_dm_connector_state(connector->state); 3544 3545 pr_debug("[HDCP_DM] connector->index: %x connect_status: %x dpms: %x\n", 3546 connector->index, connector->status, connector->dpms); 3547 pr_debug("[HDCP_DM] state protection old: %x new: %x\n", 3548 old_conn_state->content_protection, new_conn_state->content_protection); 3549 3550 if (old_crtc_state) 3551 pr_debug("[HDCP_DM] old crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n", 3552 old_crtc_state->enable, 3553 old_crtc_state->active, 3554 old_crtc_state->mode_changed, 3555 old_crtc_state->active_changed, 3556 old_crtc_state->connectors_changed); 3557 3558 if (new_crtc_state) 3559 pr_debug("[HDCP_DM] NEW crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n", 3560 new_crtc_state->enable, 3561 new_crtc_state->active, 3562 new_crtc_state->mode_changed, 3563 new_crtc_state->active_changed, 3564 new_crtc_state->connectors_changed); 3565 3566 /* hdcp content type change */ 3567 if (old_conn_state->hdcp_content_type != new_conn_state->hdcp_content_type && 3568 new_conn_state->content_protection != DRM_MODE_CONTENT_PROTECTION_UNDESIRED) { 3569 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED; 3570 pr_debug("[HDCP_DM] Type0/1 change %s :true\n", __func__); 3571 return true; 3572 } 3573 3574 /* CP is being re enabled, ignore this */ 3575 if (old_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED && 3576 new_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED) { 3577 if (new_crtc_state && new_crtc_state->mode_changed) { 3578 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED; 3579 pr_debug("[HDCP_DM] ENABLED->DESIRED & mode_changed %s :true\n", __func__); 3580 return true; 3581 } 3582 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_ENABLED; 3583 pr_debug("[HDCP_DM] ENABLED -> DESIRED %s :false\n", __func__); 3584 return false; 3585 } 3586 3587 /* S3 resume case, since old state will always be 0 (UNDESIRED) and the restored state will be ENABLED 3588 * 3589 * Handles: UNDESIRED -> ENABLED 3590 */ 3591 if (old_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_UNDESIRED && 3592 new_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED) 3593 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED; 3594 3595 /* Stream removed and re-enabled 3596 * 3597 * Can sometimes overlap with the HPD case, 3598 * thus set update_hdcp to false to avoid 3599 * setting HDCP multiple times. 3600 * 3601 * Handles: DESIRED -> DESIRED (Special case) 3602 */ 3603 if (!(old_conn_state->crtc && old_conn_state->crtc->enabled) && 3604 new_conn_state->crtc && new_conn_state->crtc->enabled && 3605 connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED) { 3606 dm_con_state->update_hdcp = false; 3607 pr_debug("[HDCP_DM] DESIRED->DESIRED (Stream removed and re-enabled) %s :true\n", 3608 __func__); 3609 return true; 3610 } 3611 3612 /* Hot-plug, headless s3, dpms 3613 * 3614 * Only start HDCP if the display is connected/enabled. 3615 * update_hdcp flag will be set to false until the next 3616 * HPD comes in. 3617 * 3618 * Handles: DESIRED -> DESIRED (Special case) 3619 */ 3620 if (dm_con_state->update_hdcp && 3621 new_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED && 3622 connector->dpms == DRM_MODE_DPMS_ON && aconnector->dc_sink != NULL) { 3623 dm_con_state->update_hdcp = false; 3624 pr_debug("[HDCP_DM] DESIRED->DESIRED (Hot-plug, headless s3, dpms) %s :true\n", 3625 __func__); 3626 return true; 3627 } 3628 3629 if (old_conn_state->content_protection == new_conn_state->content_protection) { 3630 if (new_conn_state->content_protection >= DRM_MODE_CONTENT_PROTECTION_DESIRED) { 3631 if (new_crtc_state && new_crtc_state->mode_changed) { 3632 pr_debug("[HDCP_DM] DESIRED->DESIRED or ENABLE->ENABLE mode_change %s :true\n", 3633 __func__); 3634 return true; 3635 } 3636 pr_debug("[HDCP_DM] DESIRED->DESIRED & ENABLE->ENABLE %s :false\n", 3637 __func__); 3638 return false; 3639 } 3640 3641 pr_debug("[HDCP_DM] UNDESIRED->UNDESIRED %s :false\n", __func__); 3642 return false; 3643 } 3644 3645 if (new_conn_state->content_protection != DRM_MODE_CONTENT_PROTECTION_ENABLED) { 3646 pr_debug("[HDCP_DM] UNDESIRED->DESIRED or DESIRED->UNDESIRED or ENABLED->UNDESIRED %s :true\n", 3647 __func__); 3648 return true; 3649 } 3650 3651 pr_debug("[HDCP_DM] DESIRED->ENABLED %s :false\n", __func__); 3652 return false; 3653 } 3654 EXPORT_IF_KUNIT(is_content_protection_different); 3655 3656 static void remove_stream(struct amdgpu_device *adev, 3657 struct amdgpu_crtc *acrtc, 3658 struct dc_stream_state *stream) 3659 { 3660 /* this is the update mode case */ 3661 3662 acrtc->otg_inst = -1; 3663 acrtc->enabled = false; 3664 } 3665 3666 STATIC_IFN_KUNIT void amdgpu_dm_commit_cursors(struct drm_atomic_commit *state) 3667 { 3668 struct drm_plane *plane; 3669 struct drm_plane_state *old_plane_state; 3670 int i; 3671 3672 /* 3673 * TODO: Make this per-stream so we don't issue redundant updates for 3674 * commits with multiple streams. 3675 */ 3676 for_each_old_plane_in_state(state, plane, old_plane_state, i) 3677 if (plane->type == DRM_PLANE_TYPE_CURSOR) 3678 amdgpu_dm_plane_handle_cursor_update(plane, old_plane_state); 3679 } 3680 EXPORT_IF_KUNIT(amdgpu_dm_commit_cursors); 3681 3682 static inline uint32_t get_mem_type(struct drm_framebuffer *fb) 3683 { 3684 struct amdgpu_bo *abo = gem_to_amdgpu_bo(fb->obj[0]); 3685 3686 return abo->tbo.resource ? abo->tbo.resource->mem_type : 0; 3687 } 3688 3689 STATIC_IFN_KUNIT void amdgpu_dm_update_cursor(struct drm_plane *plane, 3690 struct drm_plane_state *old_plane_state, 3691 struct dc_stream_update *update) 3692 { 3693 struct amdgpu_device *adev = drm_to_adev(plane->dev); 3694 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(plane->state->fb); 3695 struct drm_crtc *crtc = afb ? plane->state->crtc : old_plane_state->crtc; 3696 struct dm_crtc_state *crtc_state = crtc ? to_dm_crtc_state(crtc->state) : NULL; 3697 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc); 3698 uint64_t address = afb ? afb->address : 0; 3699 struct dc_cursor_position position = {0}; 3700 struct dc_cursor_attributes attributes; 3701 int ret; 3702 3703 if (!plane->state->fb && !old_plane_state->fb) 3704 return; 3705 3706 drm_dbg_atomic(plane->dev, "crtc_id=%d with size %d to %d\n", 3707 amdgpu_crtc->crtc_id, plane->state->crtc_w, 3708 plane->state->crtc_h); 3709 3710 ret = amdgpu_dm_plane_get_cursor_position(plane, crtc, &position); 3711 if (ret) 3712 return; 3713 3714 if (!position.enable) { 3715 /* turn off cursor */ 3716 if (crtc_state && crtc_state->stream) { 3717 dc_stream_set_cursor_position(crtc_state->stream, 3718 &position); 3719 update->cursor_position = &crtc_state->stream->cursor_position; 3720 } 3721 return; 3722 } 3723 3724 amdgpu_crtc->cursor_width = plane->state->crtc_w; 3725 amdgpu_crtc->cursor_height = plane->state->crtc_h; 3726 3727 memset(&attributes, 0, sizeof(attributes)); 3728 attributes.address.high_part = upper_32_bits(address); 3729 attributes.address.low_part = lower_32_bits(address); 3730 attributes.width = plane->state->crtc_w; 3731 attributes.height = plane->state->crtc_h; 3732 attributes.color_format = CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA; 3733 attributes.rotation_angle = 0; 3734 attributes.attribute_flags.value = 0; 3735 3736 /* Enable cursor degamma ROM on DCN3+ for implicit sRGB degamma in DRM 3737 * legacy gamma setup. 3738 */ 3739 if (crtc_state->cm_is_degamma_srgb && 3740 adev->dm.dc->caps.color.dpp.gamma_corr) 3741 attributes.attribute_flags.bits.ENABLE_CURSOR_DEGAMMA = 1; 3742 3743 if (afb) 3744 attributes.pitch = afb->base.pitches[0] / afb->base.format->cpp[0]; 3745 3746 if (crtc_state->stream) { 3747 if (!dc_stream_set_cursor_attributes(crtc_state->stream, 3748 &attributes)) 3749 drm_err(adev_to_drm(adev), "DC failed to set cursor attributes\n"); 3750 3751 update->cursor_attributes = &crtc_state->stream->cursor_attributes; 3752 3753 if (!dc_stream_set_cursor_position(crtc_state->stream, 3754 &position)) 3755 drm_err(adev_to_drm(adev), "DC failed to set cursor position\n"); 3756 3757 update->cursor_position = &crtc_state->stream->cursor_position; 3758 } 3759 } 3760 EXPORT_IF_KUNIT(amdgpu_dm_update_cursor); 3761 3762 STATIC_IFN_KUNIT void amdgpu_dm_enable_self_refresh(struct amdgpu_display_manager *dm, 3763 struct amdgpu_crtc *acrtc_attach, 3764 const struct dm_crtc_state *acrtc_state, 3765 const u64 current_ts) 3766 { 3767 struct psr_settings *psr = &acrtc_state->stream->link->psr_settings; 3768 struct replay_settings *pr = &acrtc_state->stream->link->replay_settings; 3769 struct amdgpu_dm_connector *aconn = 3770 (struct amdgpu_dm_connector *)acrtc_state->stream->dm_stream_context; 3771 3772 if (!acrtc_state->base.async_flip) { 3773 amdgpu_dm_psr_set_event(dm, acrtc_state->stream, false, 3774 psr_event_immediate_flip, false); 3775 amdgpu_dm_replay_set_event(dm, acrtc_state->stream, false, 3776 replay_event_immediate_flip, false); 3777 } 3778 3779 /* Decrement skip count when SR is enabled and we're doing fast updates. */ 3780 if (acrtc_state->update_type == UPDATE_TYPE_FAST && 3781 (psr->psr_feature_enabled || pr->replay_feature_enabled)) { 3782 if (aconn->sr_skip_count > 0) 3783 aconn->sr_skip_count--; 3784 3785 /* Allow SR when skip count is 0. */ 3786 acrtc_attach->dm_irq_params.allow_sr_entry = !aconn->sr_skip_count; 3787 3788 /* 3789 * If sink supports PSR SU/Panel Replay, there is no need to rely on 3790 * a vblank event disable request to enable PSR/RP. PSR SU/RP 3791 * can be enabled immediately once OS demonstrates an 3792 * adequate number of fast atomic commits to notify KMD 3793 * of update events. 3794 * See `amdgpu_dm_crtc_vblank_control_worker()`. 3795 */ 3796 if (acrtc_attach->dm_irq_params.allow_sr_entry && 3797 (current_ts - psr->psr_dirty_rects_change_timestamp_ns) > 500000000) { 3798 amdgpu_dm_psr_set_event(dm, acrtc_state->stream, false, 3799 psr_event_hw_programming, false); 3800 3801 amdgpu_dm_replay_set_event(dm, acrtc_state->stream, false, 3802 replay_event_hw_programming, false); 3803 } 3804 } else { 3805 acrtc_attach->dm_irq_params.allow_sr_entry = false; 3806 } 3807 } 3808 EXPORT_IF_KUNIT(amdgpu_dm_enable_self_refresh); 3809 3810 STATIC_IFN_KUNIT void dm_arm_vblank_event(struct amdgpu_crtc *acrtc, 3811 struct dm_crtc_state *acrtc_state, 3812 bool pflip_update, 3813 bool cursor_update) 3814 { 3815 assert_spin_locked(&acrtc->base.dev->event_lock); 3816 3817 if (!acrtc->base.state->event || acrtc_state->active_planes == 0) 3818 return; 3819 3820 if (pflip_update) { 3821 WARN_ON(acrtc->pflip_status != AMDGPU_FLIP_NONE); 3822 WARN_ON(acrtc->event); 3823 3824 acrtc->pflip_status = AMDGPU_FLIP_SUBMITTED; 3825 acrtc->event = acrtc->base.state->event; 3826 acrtc->base.state->event = NULL; 3827 3828 drm_dbg_state(acrtc->base.dev, 3829 "crtc:%d, pflip_stat:AMDGPU_FLIP_SUBMITTED\n", 3830 acrtc->crtc_id); 3831 } else if (cursor_update) { 3832 acrtc->event = acrtc->base.state->event; 3833 acrtc->base.state->event = NULL; 3834 } 3835 } 3836 EXPORT_IF_KUNIT(dm_arm_vblank_event); 3837 3838 /** 3839 * dm_arm_vblank_event_pre_programming - Prepare for programming 3840 * @acrtc: The amdgpu CRTC to prepare 3841 * @acrtc_state: The new CRTC state 3842 * @pflip_update: Whether a page flip is being programmed 3843 * @cursor_update: Whether a cursor update is being programmed 3844 * 3845 * Grab a reference on the vblank counter if a page flip or cursor update is to 3846 * be programmed. Do this before programming so the HW is not in any 3847 * idle-optimized state (such as PSR). 3848 */ 3849 STATIC_IFN_KUNIT void dm_arm_vblank_event_pre_programming(struct amdgpu_crtc *acrtc, 3850 struct dm_crtc_state *acrtc_state, 3851 bool pflip_update, 3852 bool cursor_update) 3853 { 3854 assert_spin_locked(&acrtc->base.dev->event_lock); 3855 3856 if (!acrtc->base.state->event || acrtc_state->active_planes == 0) 3857 return; 3858 3859 if (pflip_update || cursor_update) 3860 drm_crtc_vblank_get(&acrtc->base); 3861 } 3862 EXPORT_IF_KUNIT(dm_arm_vblank_event_pre_programming); 3863 3864 /* 3865 * FreeSync times each frame from the plane's previous flip, and a plane 3866 * reset gives the new DC plane zeroed flip timing, so copy it over. Do 3867 * this at commit time, not in atomic check: by then the previous commit 3868 * has finished writing it. 3869 */ 3870 static void 3871 dm_plane_inherit_flip_time(struct dc_plane_state *dc_plane_state, 3872 struct drm_plane_state *old_plane_state, 3873 struct drm_plane_state *new_plane_state) 3874 { 3875 struct dc_plane_state *old_dc_plane_state = 3876 to_dm_plane_state(old_plane_state)->dc_state; 3877 3878 if (old_dc_plane_state && old_dc_plane_state != dc_plane_state && 3879 old_plane_state->crtc == new_plane_state->crtc) 3880 dc_plane_state->time = old_dc_plane_state->time; 3881 } 3882 3883 static void amdgpu_dm_commit_planes(struct drm_atomic_commit *state, 3884 struct drm_device *dev, 3885 struct amdgpu_display_manager *dm, 3886 struct drm_crtc *pcrtc, 3887 bool wait_for_vblank) 3888 { 3889 u32 i; 3890 u64 timestamp_ns = ktime_get_ns(); 3891 struct drm_plane *plane; 3892 struct drm_plane_state *old_plane_state, *new_plane_state; 3893 struct amdgpu_crtc *acrtc_attach = to_amdgpu_crtc(pcrtc); 3894 struct drm_crtc_state *new_pcrtc_state = 3895 drm_atomic_get_new_crtc_state(state, pcrtc); 3896 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(new_pcrtc_state); 3897 struct dm_crtc_state *dm_old_crtc_state = 3898 to_dm_crtc_state(drm_atomic_get_old_crtc_state(state, pcrtc)); 3899 int planes_count = 0, vpos, hpos; 3900 unsigned long flags; 3901 u32 target_vblank, last_flip_vblank; 3902 bool vrr_active = amdgpu_dm_crtc_vrr_active(acrtc_state); 3903 bool cursor_update = false; 3904 bool pflip_present = false; 3905 bool immediate_flip = false; 3906 bool flip_latched_during_prog = false; 3907 bool dirty_rects_changed = false; 3908 bool updated_planes_and_streams = false; 3909 bool stream_update_needed = false; 3910 struct { 3911 struct dc_surface_update surface_updates[MAX_SURFACES]; 3912 struct dc_plane_info plane_infos[MAX_SURFACES]; 3913 struct dc_scaling_info scaling_infos[MAX_SURFACES]; 3914 struct dc_flip_addrs flip_addrs[MAX_SURFACES]; 3915 struct dc_stream_update stream_update; 3916 } *bundle; 3917 3918 bundle = kzalloc_obj(*bundle); 3919 3920 if (!bundle) { 3921 drm_err(dev, "Failed to allocate update bundle\n"); 3922 goto cleanup; 3923 } 3924 3925 /* 3926 * Disable the cursor first if we're disabling all the planes. 3927 * It'll remain on the screen after the planes are re-enabled 3928 * if we don't. 3929 * 3930 * If the cursor is transitioning from native to overlay mode, the 3931 * native cursor needs to be disabled first. 3932 */ 3933 if (acrtc_state->cursor_mode == DM_CURSOR_OVERLAY_MODE && 3934 dm_old_crtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE) { 3935 struct dc_cursor_position cursor_position = {0}; 3936 3937 if (!dc_stream_set_cursor_position(acrtc_state->stream, 3938 &cursor_position)) 3939 drm_err(dev, "DC failed to disable native cursor\n"); 3940 3941 bundle->stream_update.cursor_position = 3942 &acrtc_state->stream->cursor_position; 3943 stream_update_needed = true; 3944 } 3945 3946 if (acrtc_state->active_planes == 0 && 3947 dm_old_crtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE) 3948 amdgpu_dm_commit_cursors(state); 3949 3950 /* update planes when needed */ 3951 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) { 3952 struct drm_crtc *crtc = new_plane_state->crtc; 3953 struct drm_crtc_state *new_crtc_state; 3954 struct drm_framebuffer *fb = new_plane_state->fb; 3955 struct amdgpu_framebuffer *afb = (struct amdgpu_framebuffer *)fb; 3956 bool plane_needs_flip; 3957 struct dc_plane_state *dc_plane; 3958 struct dm_plane_state *dm_new_plane_state = to_dm_plane_state(new_plane_state); 3959 struct dm_plane_state *dm_old_plane_state = to_dm_plane_state(old_plane_state); 3960 3961 /* Cursor plane is handled after stream updates */ 3962 if (plane->type == DRM_PLANE_TYPE_CURSOR && 3963 acrtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE) { 3964 if ((fb && crtc == pcrtc) || 3965 (old_plane_state->fb && old_plane_state->crtc == pcrtc)) { 3966 cursor_update = true; 3967 if (amdgpu_ip_version(dm->adev, DCE_HWIP, 0) != 0) { 3968 amdgpu_dm_update_cursor(plane, old_plane_state, &bundle->stream_update); 3969 stream_update_needed = true; 3970 } 3971 } 3972 3973 continue; 3974 } 3975 3976 if (!fb || !crtc || pcrtc != crtc) 3977 continue; 3978 3979 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc); 3980 if (!new_crtc_state->active) 3981 continue; 3982 3983 dc_plane = dm_new_plane_state->dc_state; 3984 if (!dc_plane) 3985 continue; 3986 3987 dm_plane_inherit_flip_time(dc_plane, old_plane_state, 3988 new_plane_state); 3989 3990 bundle->surface_updates[planes_count].surface = dc_plane; 3991 if (new_pcrtc_state->color_mgmt_changed || new_plane_state->color_mgmt_changed) { 3992 bundle->surface_updates[planes_count].gamma = &dc_plane->gamma_correction; 3993 bundle->surface_updates[planes_count].in_transfer_func = &dc_plane->in_transfer_func; 3994 bundle->surface_updates[planes_count].gamut_remap_matrix = &dc_plane->gamut_remap_matrix; 3995 bundle->surface_updates[planes_count].hdr_mult = dc_plane->hdr_mult; 3996 bundle->surface_updates[planes_count].cm = &dc_plane->cm; 3997 } 3998 3999 if (amdgpu_dm_plane_fill_dc_scaling_info(dm->adev, new_plane_state, 4000 &bundle->scaling_infos[planes_count])) { 4001 planes_count += 1; 4002 continue; 4003 } 4004 4005 /* Cache the newly computed scaling_info in the plane state */ 4006 *dm_new_plane_state->scaling_info = 4007 bundle->scaling_infos[planes_count]; 4008 4009 /* Only send a scaling_info update if it changed vs the old state */ 4010 if (memcmp(dm_old_plane_state->scaling_info, 4011 dm_new_plane_state->scaling_info, 4012 sizeof(struct dc_scaling_info))) 4013 bundle->surface_updates[planes_count].scaling_info = 4014 &bundle->scaling_infos[planes_count]; 4015 4016 plane_needs_flip = old_plane_state->fb && new_plane_state->fb; 4017 4018 pflip_present = pflip_present || plane_needs_flip; 4019 4020 if (!plane_needs_flip) { 4021 planes_count += 1; 4022 continue; 4023 } 4024 4025 fill_dc_plane_info_and_addr( 4026 dm->adev, state, new_plane_state, 4027 &bundle->plane_infos[planes_count], 4028 &bundle->flip_addrs[planes_count].address, 4029 afb->tmz_surface); 4030 4031 bundle->plane_infos[planes_count].color_space = dc_plane->color_space; 4032 4033 drm_dbg_state(state->dev, "plane: id=%d dcc_en=%d\n", 4034 new_plane_state->plane->index, 4035 bundle->plane_infos[planes_count].dcc.enable); 4036 4037 /* Cache the newly computed plane_info in the plane state */ 4038 *dm_new_plane_state->plane_info = 4039 bundle->plane_infos[planes_count]; 4040 4041 /* Only send a plane_info update if it changed vs the old state */ 4042 if (memcmp(dm_old_plane_state->plane_info, 4043 dm_new_plane_state->plane_info, 4044 sizeof(struct dc_plane_info))) 4045 bundle->surface_updates[planes_count].plane_info = 4046 &bundle->plane_infos[planes_count]; 4047 4048 if (acrtc_state->stream->link->psr_settings.psr_feature_enabled || 4049 acrtc_state->stream->link->replay_settings.replay_feature_enabled) { 4050 fill_dc_dirty_rects(plane, old_plane_state, 4051 new_plane_state, new_crtc_state, 4052 &bundle->flip_addrs[planes_count], 4053 acrtc_state->stream->link->psr_settings.psr_version == 4054 DC_PSR_VERSION_SU_1, 4055 &dirty_rects_changed); 4056 4057 /* 4058 * If the dirty regions changed, PSR-SU need to be disabled temporarily 4059 * and enabled it again after dirty regions are stable to avoid video glitch. 4060 * PSR-SU will be enabled in 4061 * amdgpu_dm_crtc_vblank_control_worker() if user 4062 * pause the video during the PSR-SU was disabled. 4063 */ 4064 if (acrtc_state->stream->link->psr_settings.psr_version >= DC_PSR_VERSION_SU_1 && 4065 acrtc_attach->dm_irq_params.allow_sr_entry && 4066 dirty_rects_changed) { 4067 mutex_lock(&dm->dc_lock); 4068 acrtc_state->stream->link->psr_settings.psr_dirty_rects_change_timestamp_ns = 4069 timestamp_ns; 4070 dc_exit_ips_for_hw_access(dm->dc); 4071 amdgpu_dm_psr_set_event(dm, acrtc_state->stream, true, 4072 psr_event_hw_programming, true); 4073 mutex_unlock(&dm->dc_lock); 4074 } 4075 } 4076 4077 /* 4078 * Only allow immediate flips for fast updates that don't 4079 * change memory domain, FB pitch, DCC state, rotation or 4080 * mirroring. 4081 * 4082 * dm_crtc_helper_atomic_check() only accepts async flips with 4083 * fast updates. 4084 */ 4085 if (crtc->state->async_flip && 4086 (acrtc_state->update_type != UPDATE_TYPE_FAST || 4087 get_mem_type(old_plane_state->fb) != get_mem_type(fb))) 4088 drm_warn_once(state->dev, 4089 "[PLANE:%d:%s] async flip with non-fast update\n", 4090 plane->base.id, plane->name); 4091 4092 bundle->flip_addrs[planes_count].flip_immediate = 4093 crtc->state->async_flip && 4094 acrtc_state->update_type == UPDATE_TYPE_FAST && 4095 get_mem_type(old_plane_state->fb) == get_mem_type(fb); 4096 4097 immediate_flip |= bundle->flip_addrs[planes_count].flip_immediate; 4098 4099 timestamp_ns = ktime_get_ns(); 4100 bundle->flip_addrs[planes_count].flip_timestamp_in_us = div_u64(timestamp_ns, 1000); 4101 /* Cache the newly computed flip_addr in the plane state */ 4102 *dm_new_plane_state->flip_addr = 4103 bundle->flip_addrs[planes_count]; 4104 4105 /* Only send a flip_addr update if it changed vs the old state */ 4106 if (memcmp(dm_old_plane_state->flip_addr, 4107 dm_new_plane_state->flip_addr, 4108 sizeof(struct dc_flip_addrs))) 4109 bundle->surface_updates[planes_count].flip_addr = 4110 &bundle->flip_addrs[planes_count]; 4111 bundle->surface_updates[planes_count].surface = dc_plane; 4112 4113 if (!bundle->surface_updates[planes_count].surface) { 4114 drm_err(dev, "No surface for CRTC: id=%d\n", 4115 acrtc_attach->crtc_id); 4116 continue; 4117 } 4118 4119 if (plane == pcrtc->primary) 4120 amdgpu_dm_update_freesync_state_on_stream( 4121 dm, 4122 acrtc_state, 4123 acrtc_state->stream, 4124 dc_plane, 4125 bundle->flip_addrs[planes_count].flip_timestamp_in_us); 4126 4127 drm_dbg_state(state->dev, "%s Flipping to hi: 0x%x, low: 0x%x\n", 4128 __func__, 4129 bundle->flip_addrs[planes_count].address.grph.addr.high_part, 4130 bundle->flip_addrs[planes_count].address.grph.addr.low_part); 4131 4132 planes_count += 1; 4133 4134 } 4135 4136 if (pflip_present) { 4137 if (!vrr_active) { 4138 /* Use old throttling in non-vrr fixed refresh rate mode 4139 * to keep flip scheduling based on target vblank counts 4140 * working in a backwards compatible way, e.g., for 4141 * clients using the GLX_OML_sync_control extension or 4142 * DRI3/Present extension with defined target_msc. 4143 */ 4144 last_flip_vblank = amdgpu_get_vblank_counter_kms(pcrtc); 4145 } else { 4146 /* For variable refresh rate mode only: 4147 * Get vblank of last completed flip to avoid > 1 vrr 4148 * flips per video frame by use of throttling, but allow 4149 * flip programming anywhere in the possibly large 4150 * variable vrr vblank interval for fine-grained flip 4151 * timing control and more opportunity to avoid stutter 4152 * on late submission of flips. 4153 */ 4154 spin_lock_irqsave(&pcrtc->dev->event_lock, flags); 4155 last_flip_vblank = acrtc_attach->dm_irq_params.last_flip_vblank; 4156 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags); 4157 } 4158 4159 target_vblank = last_flip_vblank + wait_for_vblank; 4160 4161 /* 4162 * Wait until we're out of the vertical blank period before the one 4163 * targeted by the flip 4164 */ 4165 while ((acrtc_attach->enabled && 4166 (amdgpu_display_get_crtc_scanoutpos(dm->ddev, acrtc_attach->crtc_id, 4167 0, &vpos, &hpos, NULL, 4168 NULL, &pcrtc->hwmode) 4169 & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) == 4170 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) && 4171 (int)(target_vblank - 4172 amdgpu_get_vblank_counter_kms(pcrtc)) > 0)) { 4173 usleep_range(1000, 1100); 4174 } 4175 4176 if (acrtc_state->stream) { 4177 if (acrtc_state->freesync_vrr_info_changed) { 4178 bundle->stream_update.vrr_infopacket = 4179 &acrtc_state->stream->vrr_infopacket; 4180 bundle->stream_update.vtem_infopacket = 4181 &acrtc_state->stream->vtem_infopacket; 4182 bundle->stream_update.vsp_infopacket = 4183 &acrtc_state->stream->vsp_infopacket; 4184 stream_update_needed = true; 4185 } 4186 } 4187 } 4188 4189 scoped_guard(spinlock_irqsave, &pcrtc->dev->event_lock) { 4190 dm_arm_vblank_event_pre_programming(acrtc_attach, acrtc_state, 4191 pflip_present, 4192 cursor_update); 4193 /* 4194 * DCE depends on a combination of GRPH_FLIP, VLINE0, and 4195 * VUPDATE for event delivery. Only GRPH_FLIP handler can send 4196 * pflip events, and it only fires if HW latched to the flip. 4197 * Maintain legacy behavior by arming event before programming. 4198 */ 4199 if (amdgpu_ip_version(dm->adev, DCE_HWIP, 0) == 0) { 4200 dm_arm_vblank_event(acrtc_attach, acrtc_state, 4201 pflip_present, cursor_update); 4202 } 4203 } 4204 4205 /* Update the planes if changed or disable if we don't have any. */ 4206 if ((planes_count || acrtc_state->active_planes == 0) && 4207 acrtc_state->stream) { 4208 /* 4209 * If PSR or idle optimizations are enabled then flush out 4210 * any pending work before hardware programming. 4211 */ 4212 if (dm->vblank_control_workqueue) 4213 flush_workqueue(dm->vblank_control_workqueue); 4214 4215 bundle->stream_update.stream = acrtc_state->stream; 4216 if (new_pcrtc_state->mode_changed) { 4217 bundle->stream_update.src = acrtc_state->stream->src; 4218 bundle->stream_update.dst = acrtc_state->stream->dst; 4219 stream_update_needed = true; 4220 } 4221 4222 if (new_pcrtc_state->color_mgmt_changed) { 4223 /* 4224 * TODO: This isn't fully correct since we've actually 4225 * already modified the stream in place. 4226 */ 4227 bundle->stream_update.gamut_remap = 4228 &acrtc_state->stream->gamut_remap_matrix; 4229 bundle->stream_update.output_csc_transform = 4230 &acrtc_state->stream->csc_color_matrix; 4231 bundle->stream_update.out_transfer_func = 4232 &acrtc_state->stream->out_transfer_func; 4233 bundle->stream_update.lut3d_func = 4234 (struct dc_3dlut *) acrtc_state->stream->lut3d_func; 4235 bundle->stream_update.func_shaper = 4236 (struct dc_transfer_func *) acrtc_state->stream->func_shaper; 4237 stream_update_needed = true; 4238 } 4239 4240 acrtc_state->stream->abm_level = acrtc_state->abm_level; 4241 if (acrtc_state->abm_level != dm_old_crtc_state->abm_level) { 4242 bundle->stream_update.abm_level = &acrtc_state->abm_level; 4243 stream_update_needed = true; 4244 } 4245 4246 /* 4247 * If FreeSync state on the stream has changed then we need to 4248 * re-adjust the min/max bounds now that DC doesn't handle this 4249 * as part of commit. 4250 */ 4251 if (amdgpu_dm_is_dc_timing_adjust_needed(dm_old_crtc_state, acrtc_state)) { 4252 spin_lock_irqsave(&pcrtc->dev->event_lock, flags); 4253 dc_stream_adjust_vmin_vmax( 4254 dm->dc, acrtc_state->stream, 4255 &acrtc_attach->dm_irq_params.vrr_params.adjust); 4256 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags); 4257 } 4258 mutex_lock(&dm->dc_lock); 4259 /* 4260 * Tearing (immediate) flips cannot work with panel self-refresh 4261 * features. The event is cleared by 4262 * amdgpu_dm_enable_self_refresh() once async flips stop. 4263 */ 4264 if (immediate_flip) { 4265 dc_exit_ips_for_hw_access(dm->dc); 4266 amdgpu_dm_psr_set_event(dm, acrtc_state->stream, true, 4267 psr_event_immediate_flip, true); 4268 amdgpu_dm_replay_set_event(dm, acrtc_state->stream, true, 4269 replay_event_immediate_flip, true); 4270 } 4271 update_planes_and_stream_adapter(dm->dc, 4272 planes_count, 4273 acrtc_state->stream, 4274 stream_update_needed ? &bundle->stream_update : NULL, 4275 bundle->surface_updates); 4276 updated_planes_and_streams = true; 4277 4278 /** 4279 * Enable or disable the interrupts on the backend. 4280 * 4281 * Most pipes are put into power gating when unused. 4282 * 4283 * When power gating is enabled on a pipe we lose the 4284 * interrupt enablement state when power gating is disabled. 4285 * 4286 * So we need to update the IRQ control state in hardware 4287 * whenever the pipe turns on (since it could be previously 4288 * power gated) or off (since some pipes can't be power gated 4289 * on some ASICs). 4290 */ 4291 if (dm_old_crtc_state->active_planes != acrtc_state->active_planes) 4292 dm_update_pflip_irq_state(drm_to_adev(dev), 4293 acrtc_attach); 4294 amdgpu_dm_enable_self_refresh(dm, acrtc_attach, acrtc_state, 4295 timestamp_ns); 4296 mutex_unlock(&dm->dc_lock); 4297 } 4298 4299 /* 4300 * Update cursor state *after* programming all the planes. 4301 * This avoids redundant programming in the case where we're going 4302 * to be disabling a single plane - those pipes are being disabled. 4303 */ 4304 if (acrtc_state->active_planes && 4305 (!updated_planes_and_streams || amdgpu_ip_version(dm->adev, DCE_HWIP, 0) == 0) && 4306 acrtc_state->cursor_mode == DM_CURSOR_NATIVE_MODE) 4307 amdgpu_dm_commit_cursors(state); 4308 4309 /* 4310 * DCN specific vblank handling 4311 * ============================ 4312 * 4313 * With the event_lock held, arm the vblank event, and determine whether 4314 * deliver it immediately, or in VUPDATE_NO_LOCK IRQ (i.e. HW latch 4315 * point) handler. Do this *after* programming so that the IRQ handler 4316 * will not deliver the event before HW laches onto the programmed 4317 * values: 4318 * 4319 * Commit thread IRQ handler HW 4320 * ----------------------------------------------------------------- 4321 * arm_vblank_event() 4322 * vupdate() 4323 * vupdate_handler() 4324 * cook_timestamp() 4325 * # prev flip already latched, 4326 * # so flip_latched == true. 4327 * if event_armed && flip_latched: 4328 * send_vblank_event() 4329 * # sent before latch, **BAD!** 4330 * hw_program() 4331 * vupdate() 4332 * **latch** 4333 * 4334 * There's a consequence of arming after: it's possible for HW to latch 4335 * between start of HW programming and acrtc->event/pflip_status arming. 4336 * When this happens, the IRQ handler will send the event on the next 4337 * immediate latch point, even though HW has already latched. This is 4338 * handled by optimistically checking for HW latch after programming, 4339 * and if latched, send the event immediately: 4340 * 4341 * Commit thread IRQ handler HW 4342 * ----------------------------------------------------------------- 4343 * hw_program() 4344 * vupdate() 4345 * **latch** 4346 * vupdate_handler() 4347 * cook_timestamp() 4348 * # event_armed == false 4349 * # **no event sent!** 4350 * arm_vblank_event() 4351 * if flip_latched: 4352 * **send_vblank_event()** 4353 * disarm_vblank_event() 4354 * 4355 * The IRQ handler is expected to cook the timestamp, but we need to 4356 * cook the timestamp before optimistic sending as well. That's because 4357 * the following sequence is possible: 4358 * 4359 * Commit thread IRQ handler HW 4360 * ----------------------------------------------------------------- 4361 * hw_program() 4362 * arm_vblank_event() 4363 * vupdate() 4364 * **latch** 4365 * if flip_latched: 4366 * # Need cook before send! 4367 * **cook_timestamp()** 4368 * send_vblank_event() 4369 * disarm_vblank_event() 4370 * vupdate_handler() 4371 * cook_timestamp() 4372 * # event_armed == false 4373 * # no event sent! 4374 * 4375 * Cooking twice is OK, since DRM scanout accurate timestamps report A) 4376 * the previous vactive start if currently in vactive, or B) the next 4377 * vactive start if currently in vblank (see &get_vblank_counter). 'A)' 4378 * is what we want for the optimistic send, and for 'B)', we'll cook a 4379 * timestamp no later than the next IRQ handler run. 4380 * 4381 * The more correct fix is to wrap programming and arming with the 4382 * event_lock and thus serializing it with the IRQ handler. However, 4383 * there are various sleep-waits within 4384 * update_planes_and_stream_adapter() that makes spin locking illegal. 4385 * And on full updates, it can take 1-2 frame-times to return (see 4386 * commit_planes_for_stream). 4387 * 4388 * On DCE, GRPH_PFLIP IRQ is used and takes care of this. 4389 */ 4390 if (amdgpu_ip_version(dm->adev, DCE_HWIP, 0) != 0) { 4391 spin_lock_irqsave(&pcrtc->dev->event_lock, flags); 4392 4393 if (updated_planes_and_streams) { 4394 flip_latched_during_prog = 4395 !dc_get_flip_pending_on_otg(dm->dc, acrtc_attach->otg_inst); 4396 } 4397 4398 dm_arm_vblank_event(acrtc_attach, acrtc_state, 4399 pflip_present, cursor_update); 4400 4401 /* 4402 * Deliver the event immediately on immediate flip, or on a 4403 * update that has already latched. 4404 */ 4405 if ((immediate_flip || flip_latched_during_prog) && 4406 acrtc_attach->pflip_status == AMDGPU_FLIP_SUBMITTED && 4407 acrtc_attach->event) { 4408 drm_crtc_accurate_vblank_count(&acrtc_attach->base); 4409 drm_crtc_send_vblank_event(&acrtc_attach->base, 4410 acrtc_attach->event); 4411 acrtc_attach->event = NULL; 4412 drm_crtc_vblank_put(&acrtc_attach->base); 4413 acrtc_attach->pflip_status = AMDGPU_FLIP_NONE; 4414 } 4415 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags); 4416 } 4417 4418 cleanup: 4419 kfree(bundle); 4420 } 4421 4422 /* 4423 * amdgpu_dm_crtc_copy_transient_flags - copy mirrored flags from DRM to DC 4424 * @crtc_state: the DRM CRTC state 4425 * @stream_state: the DC stream state. 4426 * 4427 * Copy the mirrored transient state flags from DRM, to DC. It is used to bring 4428 * a dc_stream_state's flags in sync with a drm_crtc_state's flags. 4429 */ 4430 static void amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state *crtc_state, 4431 struct dc_stream_state *stream_state) 4432 { 4433 stream_state->mode_changed = drm_atomic_crtc_needs_modeset(crtc_state); 4434 } 4435 4436 /** 4437 * amdgpu_dm_crtc_complete_writeback - finish a pending writeback job 4438 * @acrtc: the CRTC whose pending writeback should be completed 4439 * 4440 * Clears the pending state, signals the writeback out fence and releases the 4441 * vblank reference taken in dm_set_writeback() while the writeback was armed. 4442 * The pending flag is tested and cleared under the writeback job lock, so this 4443 * is safe to call concurrently from the completion vblank IRQ 4444 * (dm_crtc_high_irq()) and from the writeback teardown path 4445 * (dm_clear_writeback()); only the caller that observes the pending job 4446 * performs the completion. 4447 * 4448 * Return: true if a pending writeback job was completed by this call. 4449 */ 4450 bool amdgpu_dm_crtc_complete_writeback(struct amdgpu_crtc *acrtc) 4451 { 4452 unsigned long flags; 4453 bool pending; 4454 4455 if (!acrtc->wb_conn) 4456 return false; 4457 4458 spin_lock_irqsave(&acrtc->wb_conn->job_lock, flags); 4459 pending = acrtc->wb_pending; 4460 acrtc->wb_pending = false; 4461 acrtc->wb_frame_done = false; 4462 spin_unlock_irqrestore(&acrtc->wb_conn->job_lock, flags); 4463 4464 if (!pending) 4465 return false; 4466 4467 drm_writeback_signal_completion(acrtc->wb_conn, 0); 4468 drm_crtc_vblank_put(&acrtc->base); 4469 4470 return true; 4471 } 4472 EXPORT_IF_KUNIT(amdgpu_dm_crtc_complete_writeback); 4473 4474 STATIC_IFN_KUNIT void dm_clear_writeback(struct amdgpu_display_manager *dm, 4475 struct amdgpu_crtc *acrtc, 4476 struct dm_crtc_state *crtc_state) 4477 { 4478 dc_stream_remove_writeback(dm->dc, crtc_state->stream, 0); 4479 4480 /* 4481 * If the writeback is still pending when it is torn down (its 4482 * completion vblank IRQ never fired), signal the out fence so a 4483 * waiting client does not stall and release the vblank reference 4484 * taken in dm_set_writeback(). 4485 */ 4486 amdgpu_dm_crtc_complete_writeback(acrtc); 4487 } 4488 EXPORT_IF_KUNIT(dm_clear_writeback); 4489 4490 static bool amdgpu_dm_self_refresh_pause_needed(struct drm_atomic_commit *state) 4491 { 4492 struct drm_crtc_state *old_crtc_state, *new_crtc_state; 4493 struct drm_crtc *crtc; 4494 int i; 4495 4496 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) 4497 if (drm_atomic_crtc_needs_modeset(new_crtc_state) && 4498 (old_crtc_state->active || new_crtc_state->active)) 4499 return true; 4500 4501 return false; 4502 } 4503 4504 static bool amdgpu_dm_is_new_modeset_stream(struct drm_atomic_commit *state, 4505 const struct dc_stream_state *stream) 4506 { 4507 struct drm_crtc_state *new_crtc_state; 4508 struct drm_crtc *crtc; 4509 int i; 4510 4511 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) 4512 if (drm_atomic_crtc_needs_modeset(new_crtc_state) && 4513 to_dm_crtc_state(new_crtc_state)->stream == stream) 4514 return true; 4515 4516 return false; 4517 } 4518 4519 /* 4520 * A modeset CRTC may have no PSR or Replay, such as an HDMI output being 4521 * turned off, while an eDP on another CRTC is still in self-refresh. Pause 4522 * self-refresh on every stream mod_power tracks before 4523 * amdgpu_dm_mod_power_update_streams() changes the set: the old streams of 4524 * the CRTCs in the commit and the unchanged streams of the new state. 4525 * Unlike hw_programming, which a fast update on another CRTC clears, the 4526 * pause holds until amdgpu_dm_resume_self_refresh() 4527 */ 4528 static void amdgpu_dm_pause_self_refresh(struct drm_atomic_commit *state, 4529 struct amdgpu_display_manager *dm, 4530 struct dc_state *dc_state) 4531 { 4532 struct drm_crtc_state *old_crtc_state; 4533 struct dc_stream_state *stream; 4534 struct drm_crtc *crtc; 4535 int i; 4536 4537 if (!amdgpu_dm_self_refresh_pause_needed(state)) 4538 return; 4539 4540 guard(mutex)(&dm->dc_lock); 4541 dc_exit_ips_for_hw_access(dm->dc); 4542 4543 for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) { 4544 stream = to_dm_crtc_state(old_crtc_state)->stream; 4545 if (!old_crtc_state->active || !stream) 4546 continue; 4547 4548 amdgpu_dm_psr_set_event(dm, stream, true, psr_event_pause, true); 4549 amdgpu_dm_replay_set_event(dm, stream, true, replay_event_pause, true); 4550 } 4551 4552 for (i = 0; i < dc_state->stream_count; i++) { 4553 stream = dc_state->streams[i]; 4554 if (amdgpu_dm_is_new_modeset_stream(state, stream)) 4555 continue; 4556 4557 amdgpu_dm_psr_set_event(dm, stream, true, psr_event_pause, true); 4558 amdgpu_dm_replay_set_event(dm, stream, true, replay_event_pause, true); 4559 } 4560 } 4561 4562 /* 4563 * Ends amdgpu_dm_pause_self_refresh() on the streams of the commit, which 4564 * mod_power tracks from amdgpu_dm_mod_power_update_streams() on, even if 4565 * dc_commit_streams() failed. mod_power keeps events across 4566 * mod_power_replace_stream(), so the replaced streams carry the pause too. 4567 * Runs after amdgpu_dm_mod_power_setup_streams(), since enabling a stream 4568 * clears psr_feature_enabled and replay_feature_enabled on its link until 4569 * then, and the set_event helpers skip a link without them 4570 */ 4571 static void amdgpu_dm_resume_self_refresh(struct drm_atomic_commit *state, 4572 struct amdgpu_display_manager *dm, 4573 struct dc_state *dc_state) 4574 { 4575 int i; 4576 4577 if (!dc_state || !amdgpu_dm_self_refresh_pause_needed(state)) 4578 return; 4579 4580 guard(mutex)(&dm->dc_lock); 4581 for (i = 0; i < dc_state->stream_count; i++) { 4582 amdgpu_dm_psr_set_event(dm, dc_state->streams[i], false, 4583 psr_event_pause, false); 4584 amdgpu_dm_replay_set_event(dm, dc_state->streams[i], false, 4585 replay_event_pause, false); 4586 } 4587 } 4588 4589 /** 4590 * amdgpu_dm_mod_power_update_streams - update mod_power stream state on modeset 4591 * @state: the drm atomic state 4592 * @dm: the display manager to update mod_power on 4593 * 4594 * Notify mod_power of stream changes on modeset events, and disable PSR/Replay 4595 * in preparation for hardware programming. See also 4596 * amdgpu_dm_mod_power_setup_streams() for post-modeset mod_power setup. 4597 */ 4598 STATIC_IFN_KUNIT void amdgpu_dm_mod_power_update_streams(struct drm_atomic_commit *state, 4599 struct amdgpu_display_manager *dm) 4600 { 4601 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state; 4602 struct drm_crtc_state *old_crtc_state, *new_crtc_state; 4603 struct amdgpu_dm_connector *aconnector; 4604 struct drm_crtc *crtc; 4605 int i = 0; 4606 4607 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 4608 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 4609 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 4610 4611 if (!drm_atomic_crtc_needs_modeset(new_crtc_state)) 4612 continue; 4613 4614 /* 4615 * Update mod_power on modeset event in preparation for hw 4616 * programming. Always use the old stream, since it would have 4617 * been previously added to mod_power. If old stream is null (on 4618 * crtc enable, for example), mod_power will no-op, which is the 4619 * desried behavior. 4620 */ 4621 if (old_crtc_state->active) { 4622 scoped_guard(mutex, &dm->dc_lock) { 4623 dc_exit_ips_for_hw_access(dm->dc); 4624 amdgpu_dm_psr_set_event(dm, dm_old_crtc_state->stream, true, 4625 psr_event_hw_programming, true); 4626 amdgpu_dm_replay_set_event(dm, dm_old_crtc_state->stream, true, 4627 replay_event_hw_programming, true); 4628 amdgpu_dm_replay_set_event(dm, dm_old_crtc_state->stream, false, 4629 replay_event_general_ui, false); 4630 } 4631 } 4632 4633 if (new_crtc_state->active) { 4634 aconnector = (struct amdgpu_dm_connector *) 4635 dm_new_crtc_state->stream->dm_stream_context; 4636 if (old_crtc_state->active) { 4637 mod_power_replace_stream(dm->power_module, 4638 dm_old_crtc_state->stream, 4639 dm_new_crtc_state->stream, 4640 &aconnector->psr_caps); 4641 } else { 4642 mod_power_add_stream(dm->power_module, 4643 dm_new_crtc_state->stream, 4644 &aconnector->psr_caps); 4645 } 4646 } else if (old_crtc_state->active) { 4647 mod_power_remove_stream(dm->power_module, 4648 dm_old_crtc_state->stream); 4649 } 4650 } 4651 } 4652 EXPORT_IF_KUNIT(amdgpu_dm_mod_power_update_streams); 4653 4654 /** 4655 * amdgpu_dm_mod_power_setup_streams - setup mod_power stream state post modeset 4656 * @state: the drm atomic state 4657 * @dm: the display manager to update mod_power on 4658 * 4659 * Notify mod_power of mode_change. This needs to be done after dc_stream 4660 * updates have been committed, and VRR parameters have been updated. 4661 */ 4662 STATIC_IFN_KUNIT void amdgpu_dm_mod_power_setup_streams(struct drm_atomic_commit *state, 4663 struct amdgpu_display_manager *dm) 4664 { 4665 struct dm_crtc_state *dm_new_crtc_state; 4666 struct drm_crtc_state *new_crtc_state; 4667 struct amdgpu_crtc *acrtc; 4668 struct drm_crtc *crtc; 4669 int i = 0; 4670 4671 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { 4672 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 4673 acrtc = to_amdgpu_crtc(crtc); 4674 4675 if (!drm_atomic_crtc_needs_modeset(new_crtc_state)) 4676 continue; 4677 4678 if (new_crtc_state->active) { 4679 amdgpu_dm_link_setup_replay(dm_new_crtc_state->stream, 4680 &acrtc->dm_irq_params.vrr_params); 4681 mod_power_notify_mode_change(dm->power_module, 4682 dm_new_crtc_state->stream, 4683 false); 4684 4685 /* 4686 * Block PSR / Replay on the new stream until display settles post-modeset. 4687 * These events will be cleared by amdgpu_dm_enable_self_refresh() once 4688 * allow_sr_entry becomes true. 4689 */ 4690 amdgpu_dm_psr_set_event(dm, dm_new_crtc_state->stream, true, 4691 psr_event_hw_programming, true); 4692 4693 amdgpu_dm_replay_set_event(dm, dm_new_crtc_state->stream, true, 4694 replay_event_hw_programming | replay_event_general_ui, 4695 true); 4696 } 4697 } 4698 4699 } 4700 EXPORT_IF_KUNIT(amdgpu_dm_mod_power_setup_streams); 4701 4702 static void amdgpu_dm_commit_streams(struct drm_atomic_commit *state, 4703 struct dc_state *dc_state) 4704 { 4705 struct drm_device *dev = state->dev; 4706 struct amdgpu_device *adev = drm_to_adev(dev); 4707 struct amdgpu_display_manager *dm = &adev->dm; 4708 struct drm_crtc *crtc; 4709 struct drm_crtc_state *old_crtc_state, *new_crtc_state; 4710 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state; 4711 struct drm_connector_state *old_con_state; 4712 struct drm_connector *connector; 4713 bool mode_set_reset_required = false; 4714 u32 i; 4715 struct dc_commit_streams_params params = {dc_state->streams, dc_state->stream_count}; 4716 bool set_backlight_level = false; 4717 4718 /* Disable writeback */ 4719 for_each_old_connector_in_state(state, connector, old_con_state, i) { 4720 struct dm_connector_state *dm_old_con_state; 4721 struct amdgpu_crtc *acrtc; 4722 4723 if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK) 4724 continue; 4725 4726 old_crtc_state = NULL; 4727 4728 dm_old_con_state = to_dm_connector_state(old_con_state); 4729 if (!dm_old_con_state->base.crtc) 4730 continue; 4731 4732 acrtc = to_amdgpu_crtc(dm_old_con_state->base.crtc); 4733 if (acrtc) 4734 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base); 4735 4736 if (!acrtc || !acrtc->wb_enabled) 4737 continue; 4738 4739 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 4740 4741 dm_clear_writeback(dm, acrtc, dm_old_crtc_state); 4742 acrtc->wb_enabled = false; 4743 } 4744 4745 amdgpu_dm_pause_self_refresh(state, dm, dc_state); 4746 amdgpu_dm_mod_power_update_streams(state, dm); 4747 4748 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, 4749 new_crtc_state, i) { 4750 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc); 4751 4752 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 4753 4754 if (old_crtc_state->active && 4755 (!new_crtc_state->active || 4756 drm_atomic_crtc_needs_modeset(new_crtc_state))) { 4757 manage_dm_interrupts(adev, acrtc, NULL); 4758 /* 4759 * ISM hysteresis lives on system_dfl_wq, not the 4760 * vblank workqueue. Wait it out so a timer armed while 4761 * the stream existed cannot allow idle after the 4762 * stream is released. 4763 */ 4764 amdgpu_dm_ism_flush(&acrtc->ism); 4765 dc_stream_release(dm_old_crtc_state->stream); 4766 } 4767 } 4768 4769 drm_atomic_helper_calc_timestamping_constants(state); 4770 4771 /* update changed items */ 4772 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 4773 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc); 4774 4775 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 4776 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 4777 4778 drm_dbg_state(state->dev, 4779 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, planes_changed:%d, mode_changed:%d,active_changed:%d,connectors_changed:%d\n", 4780 acrtc->crtc_id, 4781 new_crtc_state->enable, 4782 new_crtc_state->active, 4783 new_crtc_state->planes_changed, 4784 new_crtc_state->mode_changed, 4785 new_crtc_state->active_changed, 4786 new_crtc_state->connectors_changed); 4787 4788 /* Disable cursor if disabling crtc */ 4789 if (old_crtc_state->active && !new_crtc_state->active) { 4790 struct dc_cursor_position position; 4791 4792 memset(&position, 0, sizeof(position)); 4793 mutex_lock(&dm->dc_lock); 4794 dc_exit_ips_for_hw_access(dm->dc); 4795 dc_stream_program_cursor_position(dm_old_crtc_state->stream, &position); 4796 mutex_unlock(&dm->dc_lock); 4797 } 4798 4799 /* Copy all transient state flags into dc state */ 4800 if (dm_new_crtc_state->stream) { 4801 amdgpu_dm_crtc_copy_transient_flags(&dm_new_crtc_state->base, 4802 dm_new_crtc_state->stream); 4803 } 4804 4805 /* handles headless hotplug case, updating new_state and 4806 * aconnector as needed 4807 */ 4808 4809 if (amdgpu_dm_crtc_modeset_required(new_crtc_state, dm_new_crtc_state->stream, dm_old_crtc_state->stream)) { 4810 4811 drm_dbg_atomic(dev, 4812 "Atomic commit: SET crtc id %d: [%p]\n", 4813 acrtc->crtc_id, acrtc); 4814 4815 if (!dm_new_crtc_state->stream) { 4816 /* 4817 * this could happen because of issues with 4818 * userspace notifications delivery. 4819 * In this case userspace tries to set mode on 4820 * display which is disconnected in fact. 4821 * dc_sink is NULL in this case on aconnector. 4822 * We expect reset mode will come soon. 4823 * 4824 * This can also happen when unplug is done 4825 * during resume sequence ended 4826 * 4827 * In this case, we want to pretend we still 4828 * have a sink to keep the pipe running so that 4829 * hw state is consistent with the sw state 4830 */ 4831 drm_dbg_atomic(dev, 4832 "Failed to create new stream for crtc %d\n", 4833 acrtc->base.base.id); 4834 continue; 4835 } 4836 4837 if (dm_old_crtc_state->stream) 4838 remove_stream(adev, acrtc, dm_old_crtc_state->stream); 4839 4840 acrtc->enabled = true; 4841 acrtc->hw_mode = new_crtc_state->mode; 4842 crtc->hwmode = new_crtc_state->mode; 4843 mode_set_reset_required = true; 4844 set_backlight_level = true; 4845 } else if (modereset_required(new_crtc_state)) { 4846 drm_dbg_atomic(dev, 4847 "Atomic commit: RESET. crtc id %d:[%p]\n", 4848 acrtc->crtc_id, acrtc); 4849 /* i.e. reset mode */ 4850 if (dm_old_crtc_state->stream) 4851 remove_stream(adev, acrtc, dm_old_crtc_state->stream); 4852 4853 mode_set_reset_required = true; 4854 } 4855 } /* for_each_crtc_in_state() */ 4856 4857 /* if there mode set or reset, flush vblank work queue */ 4858 if (mode_set_reset_required) { 4859 if (dm->vblank_control_workqueue) 4860 flush_workqueue(dm->vblank_control_workqueue); 4861 } 4862 4863 dm_enable_per_frame_crtc_master_sync(dc_state); 4864 mutex_lock(&dm->dc_lock); 4865 dc_exit_ips_for_hw_access(dm->dc); 4866 WARN_ON(!dc_commit_streams(dm->dc, ¶ms)); 4867 4868 /* Allow idle optimization when vblank count is 0 for display off */ 4869 if (dm->active_vblank_irq_count == 0 && amdgpu_dm_is_headless(dm->adev)) 4870 dc_allow_idle_optimizations(dm->dc, true); 4871 mutex_unlock(&dm->dc_lock); 4872 4873 bool frl_stream_found = false; 4874 4875 for (i = 0; i < params.stream_count; i++) { 4876 struct dc_stream_state *stream = params.streams[i]; 4877 4878 if (stream->signal == SIGNAL_TYPE_HDMI_FRL) { 4879 frl_stream_found = true; 4880 break; 4881 } 4882 } 4883 4884 if (frl_stream_found) { 4885 if (queue_delayed_work(dm->hdmi_frl_status_polling_wq, 4886 &dm->hdmi_frl_status_polling_work, 4887 msecs_to_jiffies(dm->hdmi_frl_status_polling_delay_ms))) 4888 drm_dbg_kms(dev, "200ms frl status polling starts ...\n"); 4889 } else { 4890 if (cancel_delayed_work_sync(&dm->hdmi_frl_status_polling_work)) 4891 drm_dbg_kms(dev, "200ms frl status polling stops ...\n"); 4892 } 4893 4894 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { 4895 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc); 4896 4897 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 4898 4899 if (dm_new_crtc_state->stream != NULL) { 4900 const struct dc_stream_status *status = 4901 dc_stream_get_status(dm_new_crtc_state->stream); 4902 4903 if (!status) 4904 status = dc_state_get_stream_status(dc_state, 4905 dm_new_crtc_state->stream); 4906 if (!status) 4907 drm_err(dev, 4908 "got no status for stream %p on acrtc%p\n", 4909 dm_new_crtc_state->stream, acrtc); 4910 else 4911 acrtc->otg_inst = status->primary_otg_inst; 4912 } 4913 } 4914 4915 /* During boot up and resume the DC layer will reset the panel brightness 4916 * to fix a flicker issue. 4917 * It will cause the dm->actual_brightness is not the current panel brightness 4918 * level. (the dm->brightness is the correct panel level) 4919 * So we set the backlight level with dm->brightness value after set mode 4920 */ 4921 if (set_backlight_level) { 4922 for (i = 0; i < dm->num_of_edps; i++) { 4923 if (dm->backlight_dev[i]) 4924 amdgpu_dm_backlight_set_level(dm, i, dm->brightness[i]); 4925 } 4926 } 4927 } 4928 4929 STATIC_IFN_KUNIT void 4930 dm_set_writeback(struct amdgpu_display_manager *dm, 4931 struct dm_crtc_state *crtc_state, 4932 struct drm_connector *connector, 4933 struct drm_connector_state *new_con_state) 4934 { 4935 struct drm_writeback_connector *wb_conn = drm_connector_to_writeback(connector); 4936 struct amdgpu_device *adev = dm->adev; 4937 struct amdgpu_crtc *acrtc; 4938 struct dc_writeback_info *wb_info; 4939 struct pipe_ctx *pipe = NULL; 4940 struct amdgpu_framebuffer *afb; 4941 int i = 0; 4942 4943 wb_info = kzalloc_obj(*wb_info); 4944 if (!wb_info) { 4945 drm_err(adev_to_drm(adev), "Failed to allocate wb_info\n"); 4946 goto cleanup; 4947 } 4948 4949 acrtc = to_amdgpu_crtc(wb_conn->encoder.crtc); 4950 if (!acrtc) { 4951 drm_err(adev_to_drm(adev), "no amdgpu_crtc found\n"); 4952 goto cleanup; 4953 } 4954 4955 afb = to_amdgpu_framebuffer(new_con_state->writeback_job->fb); 4956 if (!afb) { 4957 drm_err(adev_to_drm(adev), "No amdgpu_framebuffer found\n"); 4958 goto cleanup; 4959 } 4960 4961 for (i = 0; i < MAX_PIPES; i++) { 4962 if (dm->dc->current_state->res_ctx.pipe_ctx[i].stream == crtc_state->stream) { 4963 pipe = &dm->dc->current_state->res_ctx.pipe_ctx[i]; 4964 break; 4965 } 4966 } 4967 4968 if (!pipe) { 4969 drm_err(adev_to_drm(adev), "No pipe found for stream\n"); 4970 goto cleanup; 4971 } 4972 4973 /* fill in wb_info */ 4974 wb_info->wb_enabled = true; 4975 4976 wb_info->dwb_pipe_inst = 0; 4977 wb_info->dwb_params.dwbscl_black_color = 0; 4978 wb_info->dwb_params.hdr_mult = 0x1F000; 4979 wb_info->dwb_params.csc_params.gamut_adjust_type = CM_GAMUT_ADJUST_TYPE_BYPASS; 4980 wb_info->dwb_params.csc_params.gamut_coef_format = CM_GAMUT_REMAP_COEF_FORMAT_S2_13; 4981 wb_info->dwb_params.output_depth = DWB_OUTPUT_PIXEL_DEPTH_10BPC; 4982 wb_info->dwb_params.cnv_params.cnv_out_bpc = DWB_CNV_OUT_BPC_10BPC; 4983 4984 /* width & height from crtc */ 4985 wb_info->dwb_params.cnv_params.src_width = acrtc->base.mode.crtc_hdisplay; 4986 wb_info->dwb_params.cnv_params.src_height = acrtc->base.mode.crtc_vdisplay; 4987 wb_info->dwb_params.dest_width = acrtc->base.mode.crtc_hdisplay; 4988 wb_info->dwb_params.dest_height = acrtc->base.mode.crtc_vdisplay; 4989 4990 wb_info->dwb_params.cnv_params.crop_en = false; 4991 wb_info->dwb_params.stereo_params.stereo_enabled = false; 4992 4993 wb_info->dwb_params.cnv_params.out_max_pix_val = 0x3ff; // 10 bits 4994 wb_info->dwb_params.cnv_params.out_min_pix_val = 0; 4995 wb_info->dwb_params.cnv_params.fc_out_format = DWB_OUT_FORMAT_32BPP_ARGB; 4996 wb_info->dwb_params.cnv_params.out_denorm_mode = DWB_OUT_DENORM_BYPASS; 4997 4998 wb_info->dwb_params.out_format = dwb_scaler_mode_bypass444; 4999 5000 wb_info->dwb_params.capture_rate = dwb_capture_rate_0; 5001 5002 wb_info->dwb_params.scaler_taps.h_taps = 1; 5003 wb_info->dwb_params.scaler_taps.v_taps = 1; 5004 wb_info->dwb_params.scaler_taps.h_taps_c = 1; 5005 wb_info->dwb_params.scaler_taps.v_taps_c = 1; 5006 wb_info->dwb_params.subsample_position = DWB_INTERSTITIAL_SUBSAMPLING; 5007 5008 wb_info->mcif_buf_params.luma_pitch = afb->base.pitches[0]; 5009 wb_info->mcif_buf_params.chroma_pitch = afb->base.pitches[1]; 5010 5011 for (i = 0; i < DWB_MCIF_BUF_COUNT; i++) { 5012 wb_info->mcif_buf_params.luma_address[i] = afb->address; 5013 wb_info->mcif_buf_params.chroma_address[i] = 0; 5014 } 5015 5016 wb_info->mcif_buf_params.p_vmid = 1; 5017 if (amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(3, 0, 0)) { 5018 wb_info->mcif_warmup_params.start_address.quad_part = afb->address; 5019 wb_info->mcif_warmup_params.region_size = 5020 wb_info->mcif_buf_params.luma_pitch * wb_info->dwb_params.dest_height; 5021 } 5022 wb_info->mcif_warmup_params.p_vmid = 1; 5023 wb_info->writeback_source_plane = pipe->plane_state; 5024 5025 dc_stream_add_writeback(dm->dc, crtc_state->stream, wb_info); 5026 5027 acrtc->wb_conn = wb_conn; 5028 drm_writeback_queue_job(wb_conn, new_con_state); 5029 5030 /* 5031 * Writeback completion is detected in the CRTC vblank IRQ 5032 * (dm_crtc_high_irq()). Take a vblank reference so the vblank interrupt 5033 * stays enabled while the writeback is pending; otherwise a 5034 * writeback-only commit right after drm_crtc_vblank_on() (e.g. 5035 * re-enabling a CRTC that was disabled) has no other vblank reference, 5036 * the IRQ never fires and the out fence times out. The matching put 5037 * happens once completion is signalled in dm_crtc_high_irq(), or when 5038 * the writeback is torn down in dm_clear_writeback(). 5039 * 5040 * Arm wb_pending only after the reference is held so the completion IRQ 5041 * cannot run its matching vblank_put before this get. 5042 */ 5043 WARN_ON(drm_crtc_vblank_get(&acrtc->base)); 5044 acrtc->wb_frame_done = false; 5045 acrtc->wb_pending = true; 5046 5047 cleanup: 5048 kfree(wb_info); 5049 } 5050 EXPORT_IF_KUNIT(dm_set_writeback); 5051 5052 STATIC_IFN_KUNIT void amdgpu_dm_update_hdcp(struct drm_atomic_commit *state) 5053 { 5054 struct drm_connector_state *old_con_state, *new_con_state; 5055 struct drm_device *dev = state->dev; 5056 struct drm_connector *connector; 5057 struct amdgpu_device *adev = drm_to_adev(dev); 5058 int i; 5059 5060 if (!adev->dm.hdcp_workqueue) 5061 return; 5062 5063 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) { 5064 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state); 5065 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc); 5066 struct drm_crtc_state *old_crtc_state, *new_crtc_state; 5067 struct dm_crtc_state *dm_new_crtc_state; 5068 struct amdgpu_dm_connector *aconnector; 5069 5070 if (!connector || connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK) 5071 continue; 5072 5073 aconnector = to_amdgpu_dm_connector(connector); 5074 5075 drm_dbg(dev, "[HDCP_DM] -------------- i : %x ----------\n", i); 5076 5077 drm_dbg(dev, "[HDCP_DM] connector->index: %x connect_status: %x dpms: %x\n", 5078 connector->index, connector->status, connector->dpms); 5079 drm_dbg(dev, "[HDCP_DM] state protection old: %x new: %x\n", 5080 old_con_state->content_protection, new_con_state->content_protection); 5081 5082 if (aconnector->dc_sink) { 5083 if (aconnector->dc_sink->sink_signal != SIGNAL_TYPE_VIRTUAL && 5084 aconnector->dc_sink->sink_signal != SIGNAL_TYPE_NONE) { 5085 drm_dbg(dev, "[HDCP_DM] pipe_ctx dispname=%s\n", 5086 aconnector->dc_sink->edid_caps.display_name); 5087 } 5088 } 5089 5090 new_crtc_state = NULL; 5091 old_crtc_state = NULL; 5092 5093 if (acrtc) { 5094 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base); 5095 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base); 5096 } 5097 5098 if (old_crtc_state) 5099 drm_dbg(dev, "old crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n", 5100 old_crtc_state->enable, 5101 old_crtc_state->active, 5102 old_crtc_state->mode_changed, 5103 old_crtc_state->active_changed, 5104 old_crtc_state->connectors_changed); 5105 5106 if (new_crtc_state) 5107 drm_dbg(dev, "NEW crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n", 5108 new_crtc_state->enable, 5109 new_crtc_state->active, 5110 new_crtc_state->mode_changed, 5111 new_crtc_state->active_changed, 5112 new_crtc_state->connectors_changed); 5113 5114 5115 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 5116 5117 if (dm_new_crtc_state && dm_new_crtc_state->stream == NULL && 5118 connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED) { 5119 hdcp_reset_display(adev->dm.hdcp_workqueue, aconnector->dc_link->link_index); 5120 new_con_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED; 5121 dm_new_con_state->update_hdcp = true; 5122 continue; 5123 } 5124 5125 if (is_content_protection_different(new_crtc_state, old_crtc_state, new_con_state, 5126 old_con_state, connector, adev->dm.hdcp_workqueue)) { 5127 /* when display is unplugged from mst hub, connctor will 5128 * be destroyed within dm_dp_mst_connector_destroy. connector 5129 * hdcp perperties, like type, undesired, desired, enabled, 5130 * will be lost. So, save hdcp properties into hdcp_work within 5131 * amdgpu_dm_atomic_commit_tail. if the same display is 5132 * plugged back with same display index, its hdcp properties 5133 * will be retrieved from hdcp_work within dm_dp_mst_get_modes 5134 */ 5135 5136 bool enable_encryption = false; 5137 5138 if (new_con_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED) 5139 enable_encryption = true; 5140 5141 if (aconnector->dc_link && aconnector->dc_sink && 5142 aconnector->dc_link->type == dc_connection_mst_branch) { 5143 struct hdcp_workqueue *hdcp_work = adev->dm.hdcp_workqueue; 5144 struct hdcp_workqueue *hdcp_w = 5145 &hdcp_work[aconnector->dc_link->link_index]; 5146 5147 hdcp_w->hdcp_content_type[connector->index] = 5148 new_con_state->hdcp_content_type; 5149 hdcp_w->content_protection[connector->index] = 5150 new_con_state->content_protection; 5151 } 5152 5153 if (new_crtc_state && new_crtc_state->mode_changed && 5154 new_con_state->content_protection >= DRM_MODE_CONTENT_PROTECTION_DESIRED) 5155 enable_encryption = true; 5156 5157 drm_info(dev, "[HDCP_DM] hdcp_update_display enable_encryption = %x\n", enable_encryption); 5158 5159 if (aconnector->dc_link) 5160 hdcp_update_display( 5161 adev->dm.hdcp_workqueue, aconnector->dc_link->link_index, aconnector, 5162 new_con_state->hdcp_content_type, enable_encryption); 5163 } 5164 } 5165 } 5166 EXPORT_IF_KUNIT(amdgpu_dm_update_hdcp); 5167 5168 STATIC_IFN_KUNIT int amdgpu_dm_atomic_setup_commit(struct drm_atomic_commit *state) 5169 { 5170 struct drm_crtc *crtc; 5171 struct drm_crtc_state *old_crtc_state, *new_crtc_state; 5172 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state; 5173 int i, ret; 5174 5175 ret = drm_dp_mst_atomic_setup_commit(state); 5176 if (ret) 5177 return ret; 5178 5179 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 5180 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 5181 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 5182 /* 5183 * Color management settings. We also update color properties 5184 * when a modeset is needed, to ensure it gets reprogrammed. 5185 */ 5186 if (dm_new_crtc_state->base.active && dm_new_crtc_state->stream && 5187 (dm_new_crtc_state->base.color_mgmt_changed || 5188 dm_old_crtc_state->regamma_tf != dm_new_crtc_state->regamma_tf || 5189 drm_atomic_crtc_needs_modeset(new_crtc_state))) { 5190 ret = amdgpu_dm_update_crtc_color_mgmt(dm_new_crtc_state); 5191 if (ret) { 5192 drm_dbg_atomic(state->dev, "Failed to update color state\n"); 5193 return ret; 5194 } 5195 } 5196 } 5197 5198 return 0; 5199 } 5200 EXPORT_IF_KUNIT(amdgpu_dm_atomic_setup_commit); 5201 5202 STATIC_IFN_KUNIT void set_multisync_trigger_params( 5203 struct dc_stream_state *stream) 5204 { 5205 struct dc_stream_state *master = NULL; 5206 5207 if (stream->triggered_crtc_reset.enabled) { 5208 master = stream->triggered_crtc_reset.event_source; 5209 stream->triggered_crtc_reset.event = 5210 master->timing.flags.VSYNC_POSITIVE_POLARITY ? 5211 CRTC_EVENT_VSYNC_RISING : CRTC_EVENT_VSYNC_FALLING; 5212 stream->triggered_crtc_reset.delay = TRIGGER_DELAY_NEXT_PIXEL; 5213 } 5214 } 5215 EXPORT_IF_KUNIT(set_multisync_trigger_params); 5216 5217 STATIC_IFN_KUNIT void set_master_stream(struct dc_stream_state *stream_set[], 5218 int stream_count) 5219 { 5220 int j, highest_rfr = 0, master_stream = 0; 5221 5222 for (j = 0; j < stream_count; j++) { 5223 if (stream_set[j] && stream_set[j]->triggered_crtc_reset.enabled) { 5224 int refresh_rate = 0; 5225 5226 refresh_rate = (stream_set[j]->timing.pix_clk_100hz*100)/ 5227 (stream_set[j]->timing.h_total*stream_set[j]->timing.v_total); 5228 if (refresh_rate > highest_rfr) { 5229 highest_rfr = refresh_rate; 5230 master_stream = j; 5231 } 5232 } 5233 } 5234 for (j = 0; j < stream_count; j++) { 5235 if (stream_set[j]) 5236 stream_set[j]->triggered_crtc_reset.event_source = stream_set[master_stream]; 5237 } 5238 } 5239 EXPORT_IF_KUNIT(set_master_stream); 5240 5241 STATIC_IFN_KUNIT void dm_enable_per_frame_crtc_master_sync(struct dc_state *context) 5242 { 5243 int i = 0; 5244 struct dc_stream_state *stream; 5245 5246 if (context->stream_count < 2) 5247 return; 5248 for (i = 0; i < context->stream_count ; i++) { 5249 if (!context->streams[i]) 5250 continue; 5251 /* 5252 * TODO: add a function to read AMD VSDB bits and set 5253 * crtc_sync_master.multi_sync_enabled flag 5254 * For now it's set to false 5255 */ 5256 } 5257 5258 set_master_stream(context->streams, context->stream_count); 5259 5260 for (i = 0; i < context->stream_count ; i++) { 5261 stream = context->streams[i]; 5262 5263 if (!stream) 5264 continue; 5265 5266 set_multisync_trigger_params(stream); 5267 } 5268 } 5269 EXPORT_IF_KUNIT(dm_enable_per_frame_crtc_master_sync); 5270 5271 /** 5272 * amdgpu_dm_atomic_commit_tail() - AMDgpu DM's commit tail implementation. 5273 * @state: The atomic state to commit 5274 * 5275 * This will tell DC to commit the constructed DC state from atomic_check, 5276 * programming the hardware. Any failures here implies a hardware failure, since 5277 * atomic check should have filtered anything non-kosher. 5278 */ 5279 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_commit *state) 5280 { 5281 struct drm_device *dev = state->dev; 5282 struct amdgpu_device *adev = drm_to_adev(dev); 5283 struct amdgpu_display_manager *dm = &adev->dm; 5284 struct dm_atomic_state *dm_state; 5285 struct dc_state *dc_state = NULL; 5286 u32 i, j; 5287 struct drm_crtc *crtc; 5288 struct drm_crtc_state *old_crtc_state, *new_crtc_state; 5289 unsigned long flags; 5290 bool wait_for_vblank = true; 5291 struct drm_connector *connector; 5292 struct drm_connector_state *old_con_state = NULL, *new_con_state = NULL; 5293 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state; 5294 int crtc_disable_count = 0; 5295 5296 trace_amdgpu_dm_atomic_commit_tail_begin(state); 5297 5298 drm_atomic_helper_update_legacy_modeset_state(dev, state); 5299 drm_dp_mst_atomic_wait_for_dependencies(state); 5300 5301 dm_state = dm_atomic_get_new_state(state); 5302 if (dm_state && dm_state->context) { 5303 dc_state = dm_state->context; 5304 amdgpu_dm_commit_streams(state, dc_state); 5305 } 5306 5307 amdgpu_dm_update_hdcp(state); 5308 5309 /* Handle connector state changes */ 5310 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) { 5311 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state); 5312 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state); 5313 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc); 5314 struct dc_surface_update *dummy_updates; 5315 struct dc_stream_update stream_update; 5316 struct dc_info_packet hdr_packet; 5317 struct dc_stream_status *status = NULL; 5318 bool abm_changed, hdr_changed, scaling_changed, output_color_space_changed = false; 5319 5320 memset(&stream_update, 0, sizeof(stream_update)); 5321 5322 if (acrtc) { 5323 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base); 5324 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base); 5325 } 5326 5327 /* Skip any modesets/resets */ 5328 if (!acrtc || drm_atomic_crtc_needs_modeset(new_crtc_state)) 5329 continue; 5330 5331 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 5332 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 5333 5334 scaling_changed = is_scaling_state_different(dm_new_con_state, 5335 dm_old_con_state); 5336 5337 if (new_con_state->hdmi.broadcast_rgb != old_con_state->hdmi.broadcast_rgb && 5338 dm_old_crtc_state->stream->output_color_space != 5339 amdgpu_dm_get_output_color_space(&dm_new_crtc_state->stream->timing, 5340 new_con_state, 5341 dm_new_crtc_state->stream->signal)) 5342 output_color_space_changed = true; 5343 5344 abm_changed = dm_new_crtc_state->abm_level != 5345 dm_old_crtc_state->abm_level; 5346 5347 hdr_changed = 5348 !drm_connector_atomic_hdr_metadata_equal(old_con_state, new_con_state); 5349 5350 if (!scaling_changed && !abm_changed && !hdr_changed && !output_color_space_changed) 5351 continue; 5352 5353 stream_update.stream = dm_new_crtc_state->stream; 5354 if (scaling_changed) { 5355 amdgpu_dm_update_stream_scaling_settings(dev, &dm_new_con_state->base.crtc->mode, 5356 dm_new_con_state, dm_new_crtc_state->stream); 5357 5358 stream_update.src = dm_new_crtc_state->stream->src; 5359 stream_update.dst = dm_new_crtc_state->stream->dst; 5360 } 5361 5362 if (output_color_space_changed) { 5363 dm_new_crtc_state->stream->output_color_space = 5364 amdgpu_dm_get_output_color_space(&dm_new_crtc_state->stream->timing, 5365 new_con_state, 5366 dm_new_crtc_state->stream->signal); 5367 5368 stream_update.output_color_space = &dm_new_crtc_state->stream->output_color_space; 5369 } 5370 5371 if (abm_changed) { 5372 dm_new_crtc_state->stream->abm_level = dm_new_crtc_state->abm_level; 5373 5374 stream_update.abm_level = &dm_new_crtc_state->abm_level; 5375 } 5376 5377 if (hdr_changed) { 5378 amdgpu_dm_fill_hdr_info_packet(new_con_state, &hdr_packet); 5379 stream_update.hdr_static_metadata = &hdr_packet; 5380 } 5381 5382 status = dc_stream_get_status(dm_new_crtc_state->stream); 5383 5384 if (WARN_ON(!status)) 5385 continue; 5386 5387 WARN_ON(!status->plane_count); 5388 5389 /* 5390 * TODO: DC refuses to perform stream updates without a dc_surface_update. 5391 * Here we create an empty update on each plane. 5392 * To fix this, DC should permit updating only stream properties. 5393 */ 5394 dummy_updates = kzalloc(sizeof(struct dc_surface_update) * MAX_SURFACES, GFP_KERNEL); 5395 if (!dummy_updates) { 5396 drm_err(adev_to_drm(adev), "Failed to allocate memory for dummy_updates.\n"); 5397 continue; 5398 } 5399 for (j = 0; j < status->plane_count; j++) 5400 dummy_updates[j].surface = status->plane_states[j]; 5401 5402 sort(dummy_updates, status->plane_count, 5403 sizeof(*dummy_updates), dm_plane_layer_index_cmp, NULL); 5404 5405 mutex_lock(&dm->dc_lock); 5406 dc_exit_ips_for_hw_access(dm->dc); 5407 dc_update_planes_and_stream(dm->dc, 5408 dummy_updates, 5409 status->plane_count, 5410 dm_new_crtc_state->stream, 5411 &stream_update); 5412 mutex_unlock(&dm->dc_lock); 5413 kfree(dummy_updates); 5414 5415 drm_connector_update_privacy_screen(new_con_state); 5416 } 5417 5418 /** 5419 * Enable interrupts for CRTCs that are newly enabled or went through 5420 * a modeset. It was intentionally deferred until after the front end 5421 * state was modified to wait until the OTG was on and so the IRQ 5422 * handlers didn't access stale or invalid state. 5423 */ 5424 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 5425 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc); 5426 #ifdef CONFIG_DEBUG_FS 5427 enum amdgpu_dm_pipe_crc_source cur_crc_src; 5428 #endif 5429 /* 5430 * One runtime PM reference per active CRTC: take it when the 5431 * CRTC turns on, count it when it turns off for dropping later. 5432 */ 5433 if (!old_crtc_state->active && new_crtc_state->active) 5434 pm_runtime_get_noresume(dev->dev); 5435 else if (old_crtc_state->active && !new_crtc_state->active) 5436 crtc_disable_count++; 5437 5438 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 5439 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 5440 5441 /* For freesync config update on crtc state and params for irq */ 5442 amdgpu_dm_update_stream_irq_parameters(dm, dm_new_crtc_state); 5443 5444 #ifdef CONFIG_DEBUG_FS 5445 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags); 5446 cur_crc_src = acrtc->dm_irq_params.crc_src; 5447 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags); 5448 #endif 5449 5450 if (new_crtc_state->active && 5451 (!old_crtc_state->active || 5452 drm_atomic_crtc_needs_modeset(new_crtc_state))) { 5453 dc_stream_retain(dm_new_crtc_state->stream); 5454 acrtc->dm_irq_params.stream = dm_new_crtc_state->stream; 5455 manage_dm_interrupts(adev, acrtc, dm_new_crtc_state); 5456 } 5457 /* Handle vrr on->off / off->on transitions */ 5458 amdgpu_dm_handle_vrr_transition(dm, dm_old_crtc_state, dm_new_crtc_state); 5459 5460 #ifdef CONFIG_DEBUG_FS 5461 if (new_crtc_state->active && 5462 (!old_crtc_state->active || 5463 drm_atomic_crtc_needs_modeset(new_crtc_state))) { 5464 /** 5465 * Frontend may have changed so reapply the CRC capture 5466 * settings for the stream. 5467 */ 5468 if (amdgpu_dm_is_valid_crc_source(cur_crc_src)) { 5469 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY) 5470 if (amdgpu_dm_crc_window_is_activated(crtc)) { 5471 uint8_t cnt; 5472 5473 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags); 5474 for (cnt = 0; cnt < MAX_CRC_WINDOW_NUM; cnt++) { 5475 if (acrtc->dm_irq_params.window_param[cnt].enable) { 5476 acrtc->dm_irq_params.window_param[cnt].update_win = true; 5477 5478 /** 5479 * It takes 2 frames for HW to stably generate CRC when 5480 * resuming from suspend, so we set skip_frame_cnt 2. 5481 */ 5482 acrtc->dm_irq_params.window_param[cnt].skip_frame_cnt = 2; 5483 } 5484 } 5485 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags); 5486 } 5487 #endif 5488 if (amdgpu_dm_crtc_configure_crc_source( 5489 crtc, dm_new_crtc_state, cur_crc_src)) 5490 drm_dbg_atomic(dev, "Failed to configure crc source"); 5491 } 5492 } 5493 #endif 5494 } 5495 5496 amdgpu_dm_mod_power_setup_streams(state, dm); 5497 amdgpu_dm_resume_self_refresh(state, dm, dc_state); 5498 5499 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j) 5500 if (new_crtc_state->async_flip) 5501 wait_for_vblank = false; 5502 5503 /* update planes when needed per crtc*/ 5504 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j) { 5505 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 5506 5507 if (dm_new_crtc_state->stream) 5508 amdgpu_dm_commit_planes(state, dev, dm, crtc, wait_for_vblank); 5509 } 5510 5511 /* Enable writeback */ 5512 for_each_new_connector_in_state(state, connector, new_con_state, i) { 5513 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state); 5514 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc); 5515 5516 if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK) 5517 continue; 5518 5519 if (!new_con_state->writeback_job) 5520 continue; 5521 5522 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base); 5523 5524 if (!new_crtc_state) 5525 continue; 5526 5527 if (acrtc->wb_enabled) 5528 continue; 5529 5530 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 5531 5532 dm_set_writeback(dm, dm_new_crtc_state, connector, new_con_state); 5533 acrtc->wb_enabled = true; 5534 } 5535 5536 /* Update audio instances for each connector. */ 5537 amdgpu_dm_commit_audio(dev, state); 5538 5539 /* restore the backlight level */ 5540 for (i = 0; i < dm->num_of_edps; i++) { 5541 if (dm->backlight_dev[i] && 5542 (dm->actual_brightness[i] != dm->brightness[i])) 5543 amdgpu_dm_backlight_set_level(dm, i, dm->brightness[i]); 5544 } 5545 5546 /* 5547 * send vblank event on all events not handled in flip and 5548 * mark consumed event for drm_atomic_helper_commit_hw_done 5549 */ 5550 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags); 5551 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { 5552 5553 if (new_crtc_state->event) 5554 drm_send_event_locked(dev, &new_crtc_state->event->base); 5555 5556 new_crtc_state->event = NULL; 5557 } 5558 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags); 5559 5560 /* Signal HW programming completion */ 5561 drm_atomic_helper_commit_hw_done(state); 5562 5563 if (wait_for_vblank) 5564 drm_atomic_helper_wait_for_flip_done(dev, state); 5565 5566 drm_atomic_helper_cleanup_planes(dev, state); 5567 5568 /* Don't free the memory if we are hitting this as part of suspend. 5569 * This way we don't free any memory during suspend; see 5570 * amdgpu_bo_free_kernel(). The memory will be freed in the first 5571 * non-suspend modeset or when the driver is torn down. 5572 */ 5573 if (!adev->in_suspend) { 5574 /* return the stolen vga memory back to VRAM */ 5575 if (!adev->mman.keep_stolen_vga_memory) 5576 amdgpu_ttm_unmark_vram_reserved(adev, AMDGPU_RESV_STOLEN_VGA); 5577 amdgpu_ttm_unmark_vram_reserved(adev, AMDGPU_RESV_STOLEN_EXTENDED); 5578 } 5579 5580 /* 5581 * Finally, drop a runtime PM reference for each newly disabled CRTC, 5582 * so we can put the GPU into runtime suspend if we're not driving any 5583 * displays anymore 5584 */ 5585 for (i = 0; i < crtc_disable_count; i++) 5586 pm_runtime_put_autosuspend(dev->dev); 5587 pm_runtime_mark_last_busy(dev->dev); 5588 5589 trace_amdgpu_dm_atomic_commit_tail_finish(state); 5590 } 5591 5592 /* 5593 * Grabs all modesetting locks to serialize against any blocking commits, 5594 * Waits for completion of all non blocking commits. 5595 */ 5596 STATIC_IFN_KUNIT int do_aquire_global_lock(struct drm_device *dev, 5597 struct drm_atomic_commit *state) 5598 { 5599 struct drm_crtc *crtc; 5600 struct drm_crtc_commit *commit; 5601 long ret; 5602 5603 /* 5604 * Adding all modeset locks to aquire_ctx will 5605 * ensure that when the framework release it the 5606 * extra locks we are locking here will get released to 5607 */ 5608 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx); 5609 if (ret) 5610 return ret; 5611 5612 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 5613 spin_lock(&crtc->commit_lock); 5614 commit = list_first_entry_or_null(&crtc->commit_list, 5615 struct drm_crtc_commit, commit_entry); 5616 if (commit) 5617 drm_crtc_commit_get(commit); 5618 spin_unlock(&crtc->commit_lock); 5619 5620 if (!commit) 5621 continue; 5622 5623 /* 5624 * Make sure all pending HW programming completed and 5625 * page flips done 5626 */ 5627 ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ); 5628 5629 if (ret > 0) 5630 ret = wait_for_completion_interruptible_timeout( 5631 &commit->flip_done, 10*HZ); 5632 5633 if (ret == 0) 5634 drm_err(dev, "[CRTC:%d:%s] hw_done or flip_done timed out\n", 5635 crtc->base.id, crtc->name); 5636 5637 drm_crtc_commit_put(commit); 5638 } 5639 5640 return ret < 0 ? ret : 0; 5641 } 5642 EXPORT_IF_KUNIT(do_aquire_global_lock); 5643 5644 STATIC_IFN_KUNIT int dm_update_crtc_state(struct amdgpu_display_manager *dm, 5645 struct drm_atomic_commit *state, 5646 struct drm_crtc *crtc, 5647 struct drm_crtc_state *old_crtc_state, 5648 struct drm_crtc_state *new_crtc_state, 5649 bool enable, 5650 bool *needs_dc_state_realloc) 5651 { 5652 struct dm_atomic_state *dm_state = NULL; 5653 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state; 5654 struct dc_stream_state *new_stream; 5655 struct amdgpu_device *adev = dm->adev; 5656 int ret = 0; 5657 5658 /* 5659 * TODO Move this code into dm_crtc_atomic_check once we get rid of dc_validation_set 5660 * update changed items 5661 */ 5662 struct amdgpu_crtc *acrtc = NULL; 5663 struct drm_connector *connector = NULL; 5664 struct amdgpu_dm_connector *aconnector = NULL; 5665 struct drm_connector_state *drm_new_conn_state = NULL, *drm_old_conn_state = NULL; 5666 struct dm_connector_state *dm_new_conn_state = NULL, *dm_old_conn_state = NULL; 5667 5668 new_stream = NULL; 5669 5670 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 5671 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 5672 acrtc = to_amdgpu_crtc(crtc); 5673 connector = amdgpu_dm_find_first_crtc_matching_connector(state, crtc); 5674 if (connector) 5675 aconnector = to_amdgpu_dm_connector(connector); 5676 5677 /* TODO This hack should go away */ 5678 if (connector && enable) { 5679 /* Make sure fake sink is created in plug-in scenario */ 5680 drm_new_conn_state = drm_atomic_get_new_connector_state(state, 5681 connector); 5682 drm_old_conn_state = drm_atomic_get_old_connector_state(state, 5683 connector); 5684 5685 if (WARN_ON(!drm_new_conn_state)) { 5686 ret = -EINVAL; 5687 goto fail; 5688 } 5689 5690 dm_new_conn_state = to_dm_connector_state(drm_new_conn_state); 5691 dm_old_conn_state = to_dm_connector_state(drm_old_conn_state); 5692 5693 if (!drm_atomic_crtc_needs_modeset(new_crtc_state)) 5694 goto skip_modeset; 5695 5696 new_stream = amdgpu_dm_create_validate_stream_for_sink(connector, 5697 &new_crtc_state->mode, 5698 dm_new_conn_state, 5699 dm_old_crtc_state->stream); 5700 5701 /* 5702 * we can have no stream on ACTION_SET if a display 5703 * was disconnected during S3, in this case it is not an 5704 * error, the OS will be updated after detection, and 5705 * will do the right thing on next atomic commit 5706 */ 5707 5708 if (!new_stream) { 5709 drm_dbg_driver(adev_to_drm(adev), "%s: Failed to create new stream for crtc %d\n", 5710 __func__, acrtc->base.base.id); 5711 ret = -ENOMEM; 5712 goto fail; 5713 } 5714 5715 /* 5716 * TODO: Check VSDB bits to decide whether this should 5717 * be enabled or not. 5718 */ 5719 new_stream->triggered_crtc_reset.enabled = 5720 dm->force_timing_sync; 5721 5722 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level; 5723 5724 ret = amdgpu_dm_fill_hdr_info_packet(drm_new_conn_state, 5725 &new_stream->hdr_static_metadata); 5726 if (ret) 5727 goto fail; 5728 5729 /* 5730 * If we already removed the old stream from the context 5731 * (and set the new stream to NULL) then we can't reuse 5732 * the old stream even if the stream and scaling are unchanged. 5733 * We'll hit the BUG_ON and black screen. 5734 * 5735 * TODO: Refactor this function to allow this check to work 5736 * in all conditions. 5737 */ 5738 if (amdgpu_freesync_vid_mode && 5739 dm_new_crtc_state->stream && 5740 amdgpu_dm_is_timing_unchanged_for_freesync(new_crtc_state, old_crtc_state)) 5741 goto skip_modeset; 5742 5743 if (dm_new_crtc_state->stream && 5744 dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) && 5745 dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream)) { 5746 new_crtc_state->mode_changed = false; 5747 drm_dbg_driver(adev_to_drm(adev), "Mode change not required, setting mode_changed to %d", 5748 new_crtc_state->mode_changed); 5749 } 5750 } 5751 5752 /* mode_changed flag may get updated above, need to check again */ 5753 if (!drm_atomic_crtc_needs_modeset(new_crtc_state)) 5754 goto skip_modeset; 5755 5756 drm_dbg_state(state->dev, 5757 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, planes_changed:%d, mode_changed:%d,active_changed:%d,connectors_changed:%d\n", 5758 acrtc->crtc_id, 5759 new_crtc_state->enable, 5760 new_crtc_state->active, 5761 new_crtc_state->planes_changed, 5762 new_crtc_state->mode_changed, 5763 new_crtc_state->active_changed, 5764 new_crtc_state->connectors_changed); 5765 5766 /* Remove stream for any changed/disabled CRTC */ 5767 if (!enable) { 5768 5769 if (!dm_old_crtc_state->stream) 5770 goto skip_modeset; 5771 5772 /* Unset freesync video if it was active before */ 5773 if (dm_old_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED) { 5774 dm_new_crtc_state->freesync_config.state = VRR_STATE_INACTIVE; 5775 dm_new_crtc_state->freesync_config.fixed_refresh_in_uhz = 0; 5776 } 5777 5778 /* Now check if we should set freesync video mode */ 5779 if (amdgpu_freesync_vid_mode && dm_new_crtc_state->stream && 5780 dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) && 5781 dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream) && 5782 amdgpu_dm_is_timing_unchanged_for_freesync(new_crtc_state, 5783 old_crtc_state)) { 5784 new_crtc_state->mode_changed = false; 5785 drm_dbg_driver(adev_to_drm(adev), 5786 "Mode change not required for front porch change, setting mode_changed to %d", 5787 new_crtc_state->mode_changed); 5788 5789 amdgpu_dm_set_freesync_fixed_config(dm_new_crtc_state); 5790 5791 goto skip_modeset; 5792 } else if (amdgpu_freesync_vid_mode && aconnector && 5793 amdgpu_dm_is_freesync_video_mode(&new_crtc_state->mode, 5794 aconnector)) { 5795 struct drm_display_mode *high_mode; 5796 5797 high_mode = amdgpu_dm_get_highest_refresh_rate_mode(aconnector, false); 5798 if (!drm_mode_equal(&new_crtc_state->mode, high_mode)) 5799 amdgpu_dm_set_freesync_fixed_config(dm_new_crtc_state); 5800 } 5801 5802 ret = dm_atomic_get_state(state, &dm_state); 5803 if (ret) 5804 goto fail; 5805 5806 drm_dbg_driver(adev_to_drm(adev), "Disabling DRM crtc: %d\n", 5807 crtc->base.id); 5808 5809 /* i.e. reset mode */ 5810 if (dc_state_remove_stream( 5811 dm->dc, 5812 dm_state->context, 5813 dm_old_crtc_state->stream) != DC_OK) { 5814 ret = -EINVAL; 5815 goto fail; 5816 } 5817 5818 dc_stream_release(dm_old_crtc_state->stream); 5819 dm_new_crtc_state->stream = NULL; 5820 5821 amdgpu_dm_reset_freesync_config_for_crtc(dm_new_crtc_state); 5822 5823 *needs_dc_state_realloc = true; 5824 5825 } else {/* Add stream for any updated/enabled CRTC */ 5826 /* 5827 * Quick fix to prevent NULL pointer on new_stream when 5828 * added MST connectors not found in existing crtc_state in the chained mode 5829 * TODO: need to dig out the root cause of that 5830 */ 5831 if (!connector) 5832 goto skip_modeset; 5833 5834 if (modereset_required(new_crtc_state)) 5835 goto skip_modeset; 5836 5837 if (amdgpu_dm_crtc_modeset_required(new_crtc_state, new_stream, 5838 dm_old_crtc_state->stream)) { 5839 5840 WARN_ON(dm_new_crtc_state->stream); 5841 5842 ret = dm_atomic_get_state(state, &dm_state); 5843 if (ret) 5844 goto fail; 5845 5846 dm_new_crtc_state->stream = new_stream; 5847 5848 dc_stream_retain(new_stream); 5849 5850 drm_dbg_atomic(adev_to_drm(adev), "Enabling DRM crtc: %d\n", 5851 crtc->base.id); 5852 5853 if (dc_state_add_stream( 5854 dm->dc, 5855 dm_state->context, 5856 dm_new_crtc_state->stream) != DC_OK) { 5857 ret = -EINVAL; 5858 goto fail; 5859 } 5860 5861 *needs_dc_state_realloc = true; 5862 } 5863 } 5864 5865 skip_modeset: 5866 /* Release extra reference */ 5867 if (new_stream) { 5868 dc_stream_release(new_stream); 5869 new_stream = NULL; 5870 } 5871 new_stream = NULL; 5872 5873 /* 5874 * We want to do dc stream updates that do not require a 5875 * full modeset below. 5876 */ 5877 if (!(enable && connector && new_crtc_state->active)) 5878 return 0; 5879 /* 5880 * Given above conditions, the dc state cannot be NULL because: 5881 * 1. We're in the process of enabling CRTCs (just been added 5882 * to the dc context, or already is on the context) 5883 * 2. Has a valid connector attached, and 5884 * 3. Is currently active and enabled. 5885 * => The dc stream state currently exists. 5886 */ 5887 BUG_ON(dm_new_crtc_state->stream == NULL); 5888 5889 /* Scaling or underscan settings */ 5890 if (is_scaling_state_different(dm_old_conn_state, dm_new_conn_state) || 5891 drm_atomic_crtc_needs_modeset(new_crtc_state)) 5892 amdgpu_dm_update_stream_scaling_settings(adev_to_drm(adev), 5893 &new_crtc_state->mode, dm_new_conn_state, dm_new_crtc_state->stream); 5894 5895 /* ABM settings */ 5896 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level; 5897 5898 /* 5899 * Color management settings. We also update color properties 5900 * when a modeset is needed, to ensure it gets reprogrammed. 5901 */ 5902 if (dm_new_crtc_state->base.color_mgmt_changed || 5903 dm_old_crtc_state->regamma_tf != dm_new_crtc_state->regamma_tf || 5904 drm_atomic_crtc_needs_modeset(new_crtc_state)) { 5905 ret = amdgpu_dm_check_crtc_color_mgmt(dm_new_crtc_state, true); 5906 if (ret) 5907 goto fail; 5908 } 5909 5910 /* Update Freesync settings. */ 5911 amdgpu_dm_get_freesync_config_for_crtc(dm_new_crtc_state, 5912 dm_new_conn_state); 5913 5914 return ret; 5915 5916 fail: 5917 if (new_stream) 5918 dc_stream_release(new_stream); 5919 return ret; 5920 } 5921 EXPORT_IF_KUNIT(dm_update_crtc_state); 5922 5923 STATIC_IFN_KUNIT bool should_reset_plane(struct drm_atomic_commit *state, 5924 struct drm_plane *plane, 5925 struct drm_plane_state *old_plane_state, 5926 struct drm_plane_state *new_plane_state) 5927 { 5928 struct drm_plane *other; 5929 struct drm_plane_state *old_other_state, *new_other_state; 5930 struct drm_crtc_state *old_crtc_state, *new_crtc_state; 5931 struct dm_crtc_state *old_dm_crtc_state, *new_dm_crtc_state; 5932 struct amdgpu_device *adev = drm_to_adev(plane->dev); 5933 struct drm_connector_state *new_con_state; 5934 struct drm_connector *connector; 5935 int i; 5936 5937 /* 5938 * TODO: Remove this hack for all asics once it proves that the 5939 * fast updates works fine on DCN3.2+. 5940 */ 5941 if (amdgpu_ip_version(adev, DCE_HWIP, 0) < IP_VERSION(3, 2, 0) && 5942 state->allow_modeset) 5943 return true; 5944 5945 /* Check for writeback commit */ 5946 for_each_new_connector_in_state(state, connector, new_con_state, i) { 5947 if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK) 5948 continue; 5949 5950 if (new_con_state->writeback_job) 5951 return true; 5952 } 5953 5954 if (amdgpu_in_reset(adev) && state->allow_modeset) 5955 return true; 5956 5957 /* Exit early if we know that we're adding or removing the plane. */ 5958 if (old_plane_state->crtc != new_plane_state->crtc) 5959 return true; 5960 5961 /* old crtc == new_crtc == NULL, plane not in context. */ 5962 if (!new_plane_state->crtc) 5963 return false; 5964 5965 new_crtc_state = 5966 drm_atomic_get_new_crtc_state(state, new_plane_state->crtc); 5967 old_crtc_state = 5968 drm_atomic_get_old_crtc_state(state, old_plane_state->crtc); 5969 5970 if (!new_crtc_state) 5971 return true; 5972 5973 /* 5974 * A change in cursor mode means a new dc pipe needs to be acquired or 5975 * released from the state 5976 */ 5977 old_dm_crtc_state = to_dm_crtc_state(old_crtc_state); 5978 new_dm_crtc_state = to_dm_crtc_state(new_crtc_state); 5979 if (plane->type == DRM_PLANE_TYPE_CURSOR && 5980 old_dm_crtc_state != NULL && 5981 old_dm_crtc_state->cursor_mode != new_dm_crtc_state->cursor_mode) { 5982 return true; 5983 } 5984 5985 /* CRTC Degamma changes currently require us to recreate planes. */ 5986 if (new_crtc_state->color_mgmt_changed) 5987 return true; 5988 5989 /* Plane color pipeline or its colorop changes. */ 5990 if (new_plane_state->color_mgmt_changed) 5991 return true; 5992 5993 /* 5994 * On zpos change, planes need to be reordered by removing and re-adding 5995 * them one by one to the dc state, in order of descending zpos. 5996 * 5997 * TODO: We can likely skip bandwidth validation if the only thing that 5998 * changed about the plane was it'z z-ordering. 5999 */ 6000 if (old_plane_state->normalized_zpos != new_plane_state->normalized_zpos) 6001 return true; 6002 6003 if (drm_atomic_crtc_needs_modeset(new_crtc_state)) 6004 return true; 6005 6006 /* 6007 * If there are any new primary or overlay planes being added or 6008 * removed then the z-order can potentially change. To ensure 6009 * correct z-order and pipe acquisition the current DC architecture 6010 * requires us to remove and recreate all existing planes. 6011 * 6012 * TODO: Come up with a more elegant solution for this. 6013 */ 6014 for_each_oldnew_plane_in_state(state, other, old_other_state, new_other_state, i) { 6015 struct amdgpu_framebuffer *old_afb, *new_afb; 6016 struct dm_plane_state *dm_new_other_state, *dm_old_other_state; 6017 6018 dm_new_other_state = to_dm_plane_state(new_other_state); 6019 dm_old_other_state = to_dm_plane_state(old_other_state); 6020 6021 if (other->type == DRM_PLANE_TYPE_CURSOR) 6022 continue; 6023 6024 if (old_other_state->crtc != new_plane_state->crtc && 6025 new_other_state->crtc != new_plane_state->crtc) 6026 continue; 6027 6028 if (old_other_state->crtc != new_other_state->crtc) 6029 return true; 6030 6031 /* Src/dst size and scaling updates. */ 6032 if (old_other_state->src_w != new_other_state->src_w || 6033 old_other_state->src_h != new_other_state->src_h || 6034 old_other_state->crtc_w != new_other_state->crtc_w || 6035 old_other_state->crtc_h != new_other_state->crtc_h) 6036 return true; 6037 6038 /* Rotation / mirroring updates. */ 6039 if (old_other_state->rotation != new_other_state->rotation) 6040 return true; 6041 6042 /* Blending updates. */ 6043 if (old_other_state->pixel_blend_mode != 6044 new_other_state->pixel_blend_mode) 6045 return true; 6046 6047 /* Alpha updates. */ 6048 if (old_other_state->alpha != new_other_state->alpha) 6049 return true; 6050 6051 /* Colorspace changes. */ 6052 if (old_other_state->color_range != new_other_state->color_range || 6053 old_other_state->color_encoding != new_other_state->color_encoding) 6054 return true; 6055 6056 /* HDR/Transfer Function changes. */ 6057 if (dm_old_other_state->degamma_tf != dm_new_other_state->degamma_tf || 6058 dm_old_other_state->degamma_lut != dm_new_other_state->degamma_lut || 6059 dm_old_other_state->hdr_mult != dm_new_other_state->hdr_mult || 6060 dm_old_other_state->ctm != dm_new_other_state->ctm || 6061 dm_old_other_state->shaper_lut != dm_new_other_state->shaper_lut || 6062 dm_old_other_state->shaper_tf != dm_new_other_state->shaper_tf || 6063 dm_old_other_state->lut3d != dm_new_other_state->lut3d || 6064 dm_old_other_state->blend_lut != dm_new_other_state->blend_lut || 6065 dm_old_other_state->blend_tf != dm_new_other_state->blend_tf) 6066 return true; 6067 6068 /* Framebuffer checks fall at the end. */ 6069 if (!old_other_state->fb || !new_other_state->fb) 6070 continue; 6071 6072 /* Pixel format changes can require bandwidth updates. */ 6073 if (old_other_state->fb->format != new_other_state->fb->format) 6074 return true; 6075 6076 old_afb = (struct amdgpu_framebuffer *)old_other_state->fb; 6077 new_afb = (struct amdgpu_framebuffer *)new_other_state->fb; 6078 6079 /* Tiling and DCC changes also require bandwidth updates. */ 6080 if (old_afb->base.modifier != new_afb->base.modifier) 6081 return true; 6082 } 6083 6084 return false; 6085 } 6086 EXPORT_IF_KUNIT(should_reset_plane); 6087 6088 STATIC_IFN_KUNIT int 6089 dm_update_plane_state(struct dc *dc, 6090 struct drm_atomic_commit *state, 6091 struct drm_plane *plane, 6092 struct drm_plane_state *old_plane_state, 6093 struct drm_plane_state *new_plane_state, 6094 bool enable, 6095 bool *needs_dc_state_realloc, 6096 bool *is_top_most_overlay) 6097 { 6098 6099 struct dm_atomic_state *dm_state = NULL; 6100 struct drm_crtc *new_plane_crtc, *old_plane_crtc; 6101 struct drm_crtc_state *old_crtc_state, *new_crtc_state; 6102 struct dm_crtc_state *dm_new_crtc_state, *dm_old_crtc_state; 6103 struct dm_plane_state *dm_new_plane_state, *dm_old_plane_state; 6104 bool needs_reset, update_native_cursor; 6105 int ret = 0; 6106 6107 6108 new_plane_crtc = new_plane_state->crtc; 6109 old_plane_crtc = old_plane_state->crtc; 6110 dm_new_plane_state = to_dm_plane_state(new_plane_state); 6111 dm_old_plane_state = to_dm_plane_state(old_plane_state); 6112 6113 update_native_cursor = amdgpu_dm_should_update_native_cursor(state, 6114 old_plane_crtc, 6115 new_plane_crtc, 6116 enable); 6117 6118 if (plane->type == DRM_PLANE_TYPE_CURSOR && update_native_cursor) { 6119 ret = amdgpu_dm_check_native_cursor_state(new_plane_crtc, plane, 6120 new_plane_state, enable); 6121 if (ret) 6122 return ret; 6123 6124 return 0; 6125 } 6126 6127 needs_reset = should_reset_plane(state, plane, old_plane_state, 6128 new_plane_state); 6129 6130 /* Remove any changed/removed planes */ 6131 if (!enable) { 6132 if (!needs_reset) 6133 return 0; 6134 6135 if (!old_plane_crtc) 6136 return 0; 6137 6138 old_crtc_state = drm_atomic_get_old_crtc_state( 6139 state, old_plane_crtc); 6140 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 6141 6142 if (!dm_old_crtc_state->stream) 6143 return 0; 6144 6145 drm_dbg_atomic(old_plane_crtc->dev, "Disabling DRM plane: %d on DRM crtc %d\n", 6146 plane->base.id, old_plane_crtc->base.id); 6147 6148 ret = dm_atomic_get_state(state, &dm_state); 6149 if (ret) 6150 return ret; 6151 6152 if (!dc_state_remove_plane( 6153 dc, 6154 dm_old_crtc_state->stream, 6155 dm_old_plane_state->dc_state, 6156 dm_state->context)) { 6157 6158 return -EINVAL; 6159 } 6160 6161 if (dm_old_plane_state->dc_state) 6162 dc_plane_state_release(dm_old_plane_state->dc_state); 6163 6164 dm_new_plane_state->dc_state = NULL; 6165 6166 *needs_dc_state_realloc = true; 6167 6168 } else { /* Add new planes */ 6169 struct dc_plane_state *dc_new_plane_state; 6170 6171 if (drm_atomic_plane_disabling(plane->state, new_plane_state)) 6172 return 0; 6173 6174 if (!new_plane_crtc) 6175 return 0; 6176 6177 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_crtc); 6178 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 6179 6180 if (!dm_new_crtc_state->stream) 6181 return 0; 6182 6183 if (!needs_reset) 6184 return 0; 6185 6186 ret = amdgpu_dm_plane_helper_check_state(new_plane_state, new_crtc_state); 6187 if (ret) 6188 goto out; 6189 6190 /* 6191 * Recreating the plane re-derives its DC color pipeline from the 6192 * colorop states in this commit. Pull the plane's colorops in so 6193 * the derivation sees the current pipeline; otherwise every stage 6194 * silently defaults to bypass (dropping YUV->RGB, shaper, 3D LUT 6195 * and regamma) when userspace didn't touch color in this commit. 6196 */ 6197 if (new_plane_state->color_pipeline) { 6198 ret = drm_atomic_add_affected_colorops(state, plane); 6199 if (ret) 6200 goto out; 6201 } 6202 6203 WARN_ON(dm_new_plane_state->dc_state); 6204 6205 dc_new_plane_state = dc_create_plane_state(dc); 6206 if (!dc_new_plane_state) { 6207 ret = -ENOMEM; 6208 goto out; 6209 } 6210 6211 drm_dbg_atomic(new_plane_crtc->dev, "Enabling DRM plane: %d on DRM crtc %d\n", 6212 plane->base.id, new_plane_crtc->base.id); 6213 6214 ret = fill_dc_plane_attributes( 6215 drm_to_adev(new_plane_crtc->dev), 6216 dc_new_plane_state, 6217 new_plane_state, 6218 new_crtc_state); 6219 if (ret) { 6220 dc_plane_state_release(dc_new_plane_state); 6221 goto out; 6222 } 6223 6224 ret = dm_atomic_get_state(state, &dm_state); 6225 if (ret) { 6226 dc_plane_state_release(dc_new_plane_state); 6227 goto out; 6228 } 6229 6230 /* 6231 * Any atomic check errors that occur after this will 6232 * not need a release. The plane state will be attached 6233 * to the stream, and therefore part of the atomic 6234 * state. It'll be released when the atomic state is 6235 * cleaned. 6236 */ 6237 if (!dc_state_add_plane( 6238 dc, 6239 dm_new_crtc_state->stream, 6240 dc_new_plane_state, 6241 dm_state->context)) { 6242 6243 dc_plane_state_release(dc_new_plane_state); 6244 ret = -EINVAL; 6245 goto out; 6246 } 6247 6248 dm_new_plane_state->dc_state = dc_new_plane_state; 6249 6250 dm_new_crtc_state->mpo_requested |= (plane->type == DRM_PLANE_TYPE_OVERLAY); 6251 6252 /* Tell DC to do a full surface update every time there 6253 * is a plane change. Inefficient, but works for now. 6254 */ 6255 dm_new_plane_state->dc_state->update_bits.full_update = 1; 6256 6257 *needs_dc_state_realloc = true; 6258 } 6259 6260 out: 6261 /* If enabling cursor overlay failed, attempt fallback to native mode */ 6262 if (enable && ret == -EINVAL && plane->type == DRM_PLANE_TYPE_CURSOR) { 6263 ret = amdgpu_dm_check_native_cursor_state(new_plane_crtc, plane, 6264 new_plane_state, enable); 6265 if (ret) 6266 return ret; 6267 6268 dm_new_crtc_state->cursor_mode = DM_CURSOR_NATIVE_MODE; 6269 } 6270 6271 return ret; 6272 } 6273 EXPORT_IF_KUNIT(dm_update_plane_state); 6274 6275 /* 6276 * The normalized_zpos value cannot be used by this iterator directly. It's only 6277 * calculated for enabled planes, potentially causing normalized_zpos collisions 6278 * between enabled/disabled planes in the atomic state. We need a unique value 6279 * so that the iterator will not generate the same object twice, or loop 6280 * indefinitely. 6281 */ 6282 struct __drm_planes_state *amdgpu_dm_get_next_zpos( 6283 struct drm_atomic_commit *state, 6284 struct __drm_planes_state *prev) 6285 { 6286 unsigned int highest_zpos = 0, prev_zpos = 256; 6287 uint32_t highest_id = 0, prev_id = UINT_MAX; 6288 struct drm_plane_state *new_plane_state; 6289 struct drm_plane *plane; 6290 int i, highest_i = -1; 6291 6292 if (prev != NULL) { 6293 prev_zpos = prev->new_state->zpos; 6294 prev_id = prev->ptr->base.id; 6295 } 6296 6297 for_each_new_plane_in_state(state, plane, new_plane_state, i) { 6298 /* Skip planes with higher zpos than the previously returned */ 6299 if (new_plane_state->zpos > prev_zpos || 6300 (new_plane_state->zpos == prev_zpos && 6301 plane->base.id >= prev_id)) 6302 continue; 6303 6304 /* Save the index of the plane with highest zpos */ 6305 if (new_plane_state->zpos > highest_zpos || 6306 (new_plane_state->zpos == highest_zpos && 6307 plane->base.id > highest_id)) { 6308 highest_zpos = new_plane_state->zpos; 6309 highest_id = plane->base.id; 6310 highest_i = i; 6311 } 6312 } 6313 6314 if (highest_i < 0) 6315 return NULL; 6316 6317 return &state->planes[highest_i]; 6318 } 6319 6320 STATIC_IFN_KUNIT int add_affected_mst_dsc_crtcs(struct drm_atomic_commit *state, 6321 struct drm_crtc *crtc) 6322 { 6323 struct drm_connector *connector; 6324 struct drm_connector_state *conn_state, *old_conn_state; 6325 struct amdgpu_dm_connector *aconnector = NULL; 6326 int i; 6327 6328 for_each_oldnew_connector_in_state(state, connector, old_conn_state, conn_state, i) { 6329 if (!conn_state->crtc) 6330 conn_state = old_conn_state; 6331 6332 if (conn_state->crtc != crtc) 6333 continue; 6334 6335 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK) 6336 continue; 6337 6338 aconnector = to_amdgpu_dm_connector(connector); 6339 if (!aconnector->mst_output_port || !aconnector->mst_root) 6340 aconnector = NULL; 6341 else 6342 break; 6343 } 6344 6345 if (!aconnector) 6346 return 0; 6347 6348 return drm_dp_mst_add_affected_dsc_crtcs(state, &aconnector->mst_root->mst_mgr); 6349 } 6350 EXPORT_IF_KUNIT(add_affected_mst_dsc_crtcs); 6351 6352 STATIC_IFN_KUNIT bool amdgpu_dm_crtc_mem_type_changed(struct drm_device *dev, 6353 struct drm_atomic_commit *state, 6354 struct drm_crtc_state *crtc_state) 6355 { 6356 struct drm_plane *plane; 6357 struct drm_plane_state *new_plane_state, *old_plane_state; 6358 6359 drm_for_each_plane_mask(plane, dev, crtc_state->plane_mask) { 6360 new_plane_state = drm_atomic_get_new_plane_state(state, plane); 6361 old_plane_state = drm_atomic_get_old_plane_state(state, plane); 6362 6363 if (!old_plane_state || !new_plane_state) 6364 continue; 6365 6366 if (old_plane_state->fb && new_plane_state->fb && 6367 get_mem_type(old_plane_state->fb) != get_mem_type(new_plane_state->fb)) 6368 return true; 6369 } 6370 6371 return false; 6372 } 6373 EXPORT_IF_KUNIT(amdgpu_dm_crtc_mem_type_changed); 6374 6375 /** 6376 * amdgpu_dm_atomic_check() - Atomic check implementation for AMDgpu DM. 6377 * 6378 * @dev: The DRM device 6379 * @state: The atomic state to commit 6380 * 6381 * Validate that the given atomic state is programmable by DC into hardware. 6382 * This involves constructing a &struct dc_state reflecting the new hardware 6383 * state we wish to commit, then querying DC to see if it is programmable. It's 6384 * important not to modify the existing DC state. Otherwise, atomic_check 6385 * may unexpectedly commit hardware changes. 6386 * 6387 * When validating the DC state, it's important that the right locks are 6388 * acquired. For full updates case which removes/adds/updates streams on one 6389 * CRTC while flipping on another CRTC, acquiring global lock will guarantee 6390 * that any such full update commit will wait for completion of any outstanding 6391 * flip using DRMs synchronization events. 6392 * 6393 * Note that DM adds the affected connectors for all CRTCs in state, when that 6394 * might not seem necessary. This is because DC stream creation requires the 6395 * DC sink, which is tied to the DRM connector state. Cleaning this up should 6396 * be possible but non-trivial - a possible TODO item. 6397 * 6398 * Return: -Error code if validation failed. 6399 */ 6400 STATIC_IFN_KUNIT int amdgpu_dm_atomic_check(struct drm_device *dev, 6401 struct drm_atomic_commit *state) 6402 { 6403 struct amdgpu_device *adev = drm_to_adev(dev); 6404 struct dm_atomic_state *dm_state = NULL; 6405 struct dc *dc = adev->dm.dc; 6406 struct drm_connector *connector; 6407 struct drm_connector_state *old_con_state, *new_con_state; 6408 struct drm_crtc *crtc; 6409 struct drm_crtc_state *old_crtc_state, *new_crtc_state; 6410 struct drm_plane *plane; 6411 struct drm_plane_state *old_plane_state, *new_plane_state, *new_cursor_state; 6412 enum dc_status status; 6413 int ret, i; 6414 bool needs_dc_state_realloc = false; 6415 bool is_top_most_overlay = true; 6416 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state; 6417 struct drm_dp_mst_topology_mgr *mgr; 6418 struct drm_dp_mst_topology_state *mst_state; 6419 struct dsc_mst_fairness_vars vars[MAX_PIPES] = {0}; 6420 6421 trace_amdgpu_dm_atomic_check_begin(state); 6422 6423 ret = drm_atomic_helper_check_modeset(dev, state); 6424 if (ret) { 6425 drm_dbg_atomic(dev, "drm_atomic_helper_check_modeset() failed: %pe\n", ERR_PTR(ret)); 6426 goto fail; 6427 } 6428 6429 /* Check connector changes */ 6430 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) { 6431 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state); 6432 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state); 6433 6434 /* Skip connectors that are disabled or part of modeset already. */ 6435 if (!new_con_state->crtc) 6436 continue; 6437 6438 new_crtc_state = drm_atomic_get_crtc_state(state, new_con_state->crtc); 6439 if (IS_ERR(new_crtc_state)) { 6440 drm_dbg_atomic(dev, "drm_atomic_get_crtc_state() failed: %pe\n", new_crtc_state); 6441 ret = PTR_ERR(new_crtc_state); 6442 goto fail; 6443 } 6444 6445 if (dm_old_con_state->abm_level != dm_new_con_state->abm_level || 6446 dm_old_con_state->scaling != dm_new_con_state->scaling) 6447 new_crtc_state->connectors_changed = true; 6448 } 6449 6450 if (dc_resource_is_dsc_encoding_supported(dc)) { 6451 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 6452 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 6453 dm_new_crtc_state->mode_changed_independent_from_dsc = new_crtc_state->mode_changed; 6454 } 6455 6456 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 6457 if (drm_atomic_crtc_needs_modeset(new_crtc_state)) { 6458 ret = add_affected_mst_dsc_crtcs(state, crtc); 6459 if (ret) { 6460 drm_dbg_atomic(dev, "add_affected_mst_dsc_crtcs() failed: %pe\n", ERR_PTR(ret)); 6461 goto fail; 6462 } 6463 } 6464 } 6465 } 6466 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 6467 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 6468 6469 if (!drm_atomic_crtc_needs_modeset(new_crtc_state) && 6470 !new_crtc_state->color_mgmt_changed && 6471 old_crtc_state->vrr_enabled == new_crtc_state->vrr_enabled && 6472 dm_old_crtc_state->dsc_force_changed == false) 6473 continue; 6474 6475 ret = amdgpu_dm_verify_lut_sizes(new_crtc_state); 6476 if (ret) { 6477 drm_dbg_atomic(dev, "amdgpu_dm_verify_lut_sizes() failed: %pe\n", ERR_PTR(ret)); 6478 goto fail; 6479 } 6480 6481 if (!new_crtc_state->enable) 6482 continue; 6483 6484 ret = drm_atomic_add_affected_connectors(state, crtc); 6485 if (ret) { 6486 drm_dbg_atomic(dev, "drm_atomic_add_affected_connectors() failed: %pe\n", ERR_PTR(ret)); 6487 goto fail; 6488 } 6489 6490 ret = drm_atomic_add_affected_planes(state, crtc); 6491 if (ret) { 6492 drm_dbg_atomic(dev, "drm_atomic_add_affected_planes() failed: %pe\n", ERR_PTR(ret)); 6493 goto fail; 6494 } 6495 6496 if (dm_old_crtc_state->dsc_force_changed) 6497 new_crtc_state->mode_changed = true; 6498 } 6499 6500 /* 6501 * Add all primary and overlay planes on the CRTC to the state 6502 * whenever a plane is enabled to maintain correct z-ordering 6503 * and to enable fast surface updates. 6504 */ 6505 drm_for_each_crtc(crtc, dev) { 6506 bool modified = false; 6507 6508 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) { 6509 if (plane->type == DRM_PLANE_TYPE_CURSOR) 6510 continue; 6511 6512 if (new_plane_state->crtc == crtc || 6513 old_plane_state->crtc == crtc) { 6514 modified = true; 6515 break; 6516 } 6517 } 6518 6519 if (!modified) 6520 continue; 6521 6522 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) { 6523 if (plane->type == DRM_PLANE_TYPE_CURSOR) 6524 continue; 6525 6526 new_plane_state = 6527 drm_atomic_get_plane_state(state, plane); 6528 6529 if (IS_ERR(new_plane_state)) { 6530 ret = PTR_ERR(new_plane_state); 6531 drm_dbg_atomic(dev, "new_plane_state is BAD: %pe\n", new_plane_state); 6532 goto fail; 6533 } 6534 } 6535 } 6536 6537 /* 6538 * DC consults the zpos (layer_index in DC terminology) to determine the 6539 * hw plane on which to enable the hw cursor (see 6540 * `dcn10_can_pipe_disable_cursor`). By now, all modified planes are in 6541 * atomic state, so call drm helper to normalize zpos. 6542 */ 6543 ret = drm_atomic_normalize_zpos(dev, state); 6544 if (ret) { 6545 drm_dbg(dev, "drm_atomic_normalize_zpos() failed: %pe\n", ERR_PTR(ret)); 6546 goto fail; 6547 } 6548 6549 /* 6550 * Determine whether cursors on each CRTC should be enabled in native or 6551 * overlay mode. 6552 */ 6553 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { 6554 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 6555 6556 ret = amdgpu_dm_crtc_get_cursor_mode(adev, state, dm_new_crtc_state, 6557 &dm_new_crtc_state->cursor_mode); 6558 if (ret) { 6559 drm_dbg(dev, "Failed to determine cursor mode: %pe\n", ERR_PTR(ret)); 6560 goto fail; 6561 } 6562 6563 /* 6564 * If overlay cursor is needed, DC cannot go through the 6565 * native cursor update path. All enabled planes on the CRTC 6566 * need to be added for DC to not disable a plane by mistake 6567 */ 6568 if (dm_new_crtc_state->cursor_mode == DM_CURSOR_OVERLAY_MODE) { 6569 if (amdgpu_ip_version(adev, DCE_HWIP, 0) == 0) { 6570 drm_dbg(dev, "Overlay cursor not supported on DCE\n"); 6571 ret = -EINVAL; 6572 goto fail; 6573 } 6574 6575 ret = drm_atomic_add_affected_planes(state, crtc); 6576 if (ret) 6577 goto fail; 6578 } 6579 } 6580 6581 /* Remove exiting planes if they are modified */ 6582 for_each_oldnew_plane_in_descending_zpos(state, plane, old_plane_state, new_plane_state) { 6583 6584 ret = dm_update_plane_state(dc, state, plane, 6585 old_plane_state, 6586 new_plane_state, 6587 false, 6588 &needs_dc_state_realloc, 6589 &is_top_most_overlay); 6590 if (ret) { 6591 drm_dbg_atomic(dev, "dm_update_plane_state() failed: %pe\n", ERR_PTR(ret)); 6592 goto fail; 6593 } 6594 } 6595 6596 /* Disable all crtcs which require disable */ 6597 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 6598 ret = dm_update_crtc_state(&adev->dm, state, crtc, 6599 old_crtc_state, 6600 new_crtc_state, 6601 false, 6602 &needs_dc_state_realloc); 6603 if (ret) { 6604 drm_dbg_atomic(dev, "DISABLE: dm_update_crtc_state() failed: %pe\n", ERR_PTR(ret)); 6605 goto fail; 6606 } 6607 } 6608 6609 /* Enable all crtcs which require enable */ 6610 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 6611 ret = dm_update_crtc_state(&adev->dm, state, crtc, 6612 old_crtc_state, 6613 new_crtc_state, 6614 true, 6615 &needs_dc_state_realloc); 6616 if (ret) { 6617 drm_dbg_atomic(dev, "ENABLE: dm_update_crtc_state() failed: %pe\n", ERR_PTR(ret)); 6618 goto fail; 6619 } 6620 } 6621 6622 /* Add new/modified planes */ 6623 for_each_oldnew_plane_in_descending_zpos(state, plane, old_plane_state, new_plane_state) { 6624 ret = dm_update_plane_state(dc, state, plane, 6625 old_plane_state, 6626 new_plane_state, 6627 true, 6628 &needs_dc_state_realloc, 6629 &is_top_most_overlay); 6630 if (ret) { 6631 drm_dbg_atomic(dev, "dm_update_plane_state() failed: %pe\n", ERR_PTR(ret)); 6632 goto fail; 6633 } 6634 } 6635 6636 #if defined(CONFIG_DRM_AMD_DC_FP) 6637 if (dc_resource_is_dsc_encoding_supported(dc)) { 6638 ret = pre_validate_dsc(state, &dm_state, vars); 6639 if (ret != 0) 6640 goto fail; 6641 } 6642 #endif 6643 6644 /* Run this here since we want to validate the streams we created */ 6645 ret = drm_atomic_helper_check_planes(dev, state); 6646 if (ret) { 6647 drm_dbg_atomic(dev, "drm_atomic_helper_check_planes() failed: %pe\n", ERR_PTR(ret)); 6648 goto fail; 6649 } 6650 6651 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { 6652 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 6653 if (dm_new_crtc_state->mpo_requested) 6654 drm_dbg_atomic(dev, "MPO enablement requested on crtc:[%p]\n", crtc); 6655 } 6656 6657 /* Check cursor restrictions */ 6658 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { 6659 enum amdgpu_dm_cursor_mode required_cursor_mode; 6660 int is_rotated, is_scaled; 6661 6662 /* Overlay cusor not subject to native cursor restrictions */ 6663 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 6664 if (dm_new_crtc_state->cursor_mode == DM_CURSOR_OVERLAY_MODE) 6665 continue; 6666 6667 /* Check if rotation or scaling is enabled on DCN 4x and above */ 6668 if ((drm_plane_mask(crtc->cursor) & new_crtc_state->plane_mask) && 6669 (amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(4, 0, 1))) { 6670 new_cursor_state = drm_atomic_get_new_plane_state(state, crtc->cursor); 6671 6672 is_rotated = new_cursor_state && 6673 ((new_cursor_state->rotation & DRM_MODE_ROTATE_MASK) != DRM_MODE_ROTATE_0); 6674 is_scaled = new_cursor_state && ((new_cursor_state->src_w >> 16 != new_cursor_state->crtc_w) || 6675 (new_cursor_state->src_h >> 16 != new_cursor_state->crtc_h)); 6676 6677 if (is_rotated || is_scaled) { 6678 drm_dbg_driver( 6679 crtc->dev, 6680 "[CRTC:%d:%s] cannot enable hardware cursor due to rotation/scaling\n", 6681 crtc->base.id, crtc->name); 6682 ret = -EINVAL; 6683 goto fail; 6684 } 6685 } 6686 6687 /* If HW can only do native cursor, check restrictions again */ 6688 ret = amdgpu_dm_crtc_get_cursor_mode(adev, state, dm_new_crtc_state, 6689 &required_cursor_mode); 6690 if (ret) { 6691 drm_dbg_driver(crtc->dev, 6692 "[CRTC:%d:%s] Checking cursor mode failed\n", 6693 crtc->base.id, crtc->name); 6694 goto fail; 6695 } else if (required_cursor_mode == DM_CURSOR_OVERLAY_MODE) { 6696 drm_dbg_driver(crtc->dev, 6697 "[CRTC:%d:%s] Cannot enable native cursor due to scaling, YUV, or color pipeline restrictions\n", 6698 crtc->base.id, crtc->name); 6699 ret = -EINVAL; 6700 goto fail; 6701 } 6702 } 6703 6704 if (state->legacy_cursor_update) { 6705 /* 6706 * This is a fast cursor update coming from the plane update 6707 * helper, check if it can be done asynchronously for better 6708 * performance. 6709 */ 6710 state->async_update = 6711 !drm_atomic_helper_async_check(dev, state); 6712 6713 /* 6714 * Skip the remaining global validation if this is an async 6715 * update. Cursor updates can be done without affecting 6716 * state or bandwidth calcs and this avoids the performance 6717 * penalty of locking the private state object and 6718 * allocating a new dc_state. 6719 */ 6720 if (state->async_update) 6721 return 0; 6722 } 6723 6724 /* Check scaling and underscan changes*/ 6725 /* TODO Removed scaling changes validation due to inability to commit 6726 * new stream into context w\o causing full reset. Need to 6727 * decide how to handle. 6728 */ 6729 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) { 6730 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state); 6731 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state); 6732 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc); 6733 6734 /* Skip any modesets/resets */ 6735 if (!acrtc || drm_atomic_crtc_needs_modeset( 6736 drm_atomic_get_new_crtc_state(state, &acrtc->base))) 6737 continue; 6738 6739 /* Skip any thing not scale or underscan changes */ 6740 if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state)) 6741 continue; 6742 6743 needs_dc_state_realloc = true; 6744 } 6745 6746 /* set the slot info for each mst_state based on the link encoding format */ 6747 for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) { 6748 struct amdgpu_dm_connector *aconnector; 6749 struct drm_connector *connector; 6750 struct drm_connector_list_iter iter; 6751 u8 link_coding_cap; 6752 6753 drm_connector_list_iter_begin(dev, &iter); 6754 drm_for_each_connector_iter(connector, &iter) { 6755 if (connector->index == mst_state->mgr->conn_base_id) { 6756 aconnector = to_amdgpu_dm_connector(connector); 6757 link_coding_cap = dc_link_dp_mst_decide_link_encoding_format(aconnector->dc_link); 6758 drm_dp_mst_update_slots(mst_state, link_coding_cap); 6759 6760 break; 6761 } 6762 } 6763 drm_connector_list_iter_end(&iter); 6764 } 6765 6766 /** 6767 * Streams and planes are reset when there are changes that affect 6768 * bandwidth. Anything that affects bandwidth needs to go through 6769 * DC global validation to ensure that the configuration can be applied 6770 * to hardware. 6771 * 6772 * We have to currently stall out here in atomic_check for outstanding 6773 * commits to finish in this case because our IRQ handlers reference 6774 * DRM state directly - we can end up disabling interrupts too early 6775 * if we don't. 6776 * 6777 * TODO: Remove this stall and drop DM state private objects. 6778 */ 6779 if (needs_dc_state_realloc) { 6780 ret = dm_atomic_get_state(state, &dm_state); 6781 if (ret) { 6782 drm_dbg_atomic(dev, "dm_atomic_get_state() failed: %pe\n", ERR_PTR(ret)); 6783 goto fail; 6784 } 6785 6786 ret = do_aquire_global_lock(dev, state); 6787 if (ret) { 6788 drm_dbg_atomic(dev, "do_aquire_global_lock() failed: %pe\n", ERR_PTR(ret)); 6789 goto fail; 6790 } 6791 6792 #if defined(CONFIG_DRM_AMD_DC_FP) 6793 if (dc_resource_is_dsc_encoding_supported(dc)) { 6794 ret = compute_mst_dsc_configs_for_state(state, dm_state->context, vars); 6795 if (ret) { 6796 drm_dbg_atomic(dev, "MST_DSC compute_mst_dsc_configs_for_state() failed: %pe\n", ERR_PTR(ret)); 6797 ret = -EINVAL; 6798 goto fail; 6799 } 6800 } 6801 #endif 6802 6803 ret = dm_update_mst_vcpi_slots_for_dsc(state, dm_state->context, vars); 6804 if (ret) { 6805 drm_dbg_atomic(dev, "dm_update_mst_vcpi_slots_for_dsc() failed: %pe\n", ERR_PTR(ret)); 6806 goto fail; 6807 } 6808 6809 /* 6810 * Perform validation of MST topology in the state: 6811 * We need to perform MST atomic check before calling 6812 * dc_validate_global_state(), or there is a chance 6813 * to get stuck in an infinite loop and hang eventually. 6814 */ 6815 ret = drm_dp_mst_atomic_check(state); 6816 if (ret) { 6817 drm_dbg_atomic(dev, "MST drm_dp_mst_atomic_check() failed: %pe\n", ERR_PTR(ret)); 6818 goto fail; 6819 } 6820 status = dc_validate_global_state(dc, dm_state->context, DC_VALIDATE_MODE_ONLY); 6821 if (status != DC_OK) { 6822 drm_dbg_atomic(dev, "DC global validation failure: %s (%d)", 6823 dc_status_to_str(status), status); 6824 ret = -EINVAL; 6825 goto fail; 6826 } 6827 } else { 6828 /* 6829 * The commit is a fast update. Fast updates shouldn't change 6830 * the DC context, affect global validation, and can have their 6831 * commit work done in parallel with other commits not touching 6832 * the same resource. If we have a new DC context as part of 6833 * the DM atomic state from validation we need to free it and 6834 * retain the existing one instead. 6835 * 6836 * Furthermore, since the DM atomic state only contains the DC 6837 * context and can safely be annulled, we can free the state 6838 * and clear the associated private object now to free 6839 * some memory and avoid a possible use-after-free later. 6840 */ 6841 6842 for (i = 0; i < state->num_private_objs; i++) { 6843 struct drm_private_obj *obj = state->private_objs[i].ptr; 6844 6845 if (obj->funcs == adev->dm.atomic_obj.funcs) { 6846 int j = state->num_private_objs-1; 6847 6848 dm_atomic_destroy_state(obj, 6849 state->private_objs[i].state_to_destroy); 6850 6851 /* If i is not at the end of the array then the 6852 * last element needs to be moved to where i was 6853 * before the array can safely be truncated. 6854 */ 6855 if (i != j) 6856 state->private_objs[i] = 6857 state->private_objs[j]; 6858 6859 state->private_objs[j].ptr = NULL; 6860 state->private_objs[j].state_to_destroy = NULL; 6861 state->private_objs[j].old_state = NULL; 6862 state->private_objs[j].new_state = NULL; 6863 6864 state->num_private_objs = j; 6865 break; 6866 } 6867 } 6868 } 6869 6870 /* Store the overall update type for use later in atomic check. */ 6871 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { 6872 struct dm_crtc_state *dm_new_crtc_state = 6873 to_dm_crtc_state(new_crtc_state); 6874 6875 /* 6876 * Only allow async flips for fast updates that don't change 6877 * the FB pitch, the DCC state, rotation, mem_type, etc. 6878 */ 6879 if (new_crtc_state->async_flip && 6880 (needs_dc_state_realloc || 6881 amdgpu_dm_crtc_mem_type_changed(dev, state, new_crtc_state))) { 6882 drm_dbg_atomic(crtc->dev, 6883 "[CRTC:%d:%s] async flips are only supported for fast updates\n", 6884 crtc->base.id, crtc->name); 6885 ret = -EINVAL; 6886 goto fail; 6887 } 6888 6889 dm_new_crtc_state->update_type = needs_dc_state_realloc ? 6890 UPDATE_TYPE_FULL : UPDATE_TYPE_FAST; 6891 } 6892 6893 /* Must be success */ 6894 WARN_ON(ret); 6895 6896 trace_amdgpu_dm_atomic_check_finish(state, ret); 6897 6898 return ret; 6899 6900 fail: 6901 if (ret == -EDEADLK) 6902 drm_dbg_atomic(dev, "Atomic check stopped to avoid deadlock.\n"); 6903 else if (ret == -EINTR || ret == -EAGAIN || ret == -ERESTARTSYS) 6904 drm_dbg_atomic(dev, "Atomic check stopped due to signal.\n"); 6905 else 6906 drm_dbg_atomic(dev, "Atomic check failed: %pe\n", ERR_PTR(ret)); 6907 6908 trace_amdgpu_dm_atomic_check_finish(state, ret); 6909 6910 return ret; 6911 } 6912 EXPORT_IF_KUNIT(amdgpu_dm_atomic_check); 6913 6914 void amdgpu_dm_trigger_timing_sync(struct drm_device *dev) 6915 { 6916 struct amdgpu_device *adev = drm_to_adev(dev); 6917 struct dc *dc = adev->dm.dc; 6918 int i; 6919 6920 mutex_lock(&adev->dm.dc_lock); 6921 if (dc->current_state) { 6922 for (i = 0; i < dc->current_state->stream_count; ++i) 6923 dc->current_state->streams[i] 6924 ->triggered_crtc_reset.enabled = 6925 adev->dm.force_timing_sync; 6926 6927 dm_enable_per_frame_crtc_master_sync(dc->current_state); 6928 dc_trigger_sync(dc, dc->current_state); 6929 } 6930 mutex_unlock(&adev->dm.dc_lock); 6931 } 6932 EXPORT_IF_KUNIT(amdgpu_dm_trigger_timing_sync); 6933 6934 void dm_write_reg_func(const struct dc_context *ctx, uint32_t address, 6935 u32 value, const char *func_name) 6936 { 6937 #ifdef DM_CHECK_ADDR_0 6938 if (address == 0) { 6939 drm_err(adev_to_drm(ctx->driver_context), 6940 "invalid register write. address = 0"); 6941 return; 6942 } 6943 #endif 6944 6945 amdgpu_dm_exit_ips_for_hw_access(ctx->dc); 6946 cgs_write_register(ctx->cgs_device, address, value); 6947 trace_amdgpu_dc_wreg(&ctx->perf_trace->write_count, address, value); 6948 } 6949 6950 uint32_t dm_read_reg_func(const struct dc_context *ctx, uint32_t address, 6951 const char *func_name) 6952 { 6953 u32 value; 6954 #ifdef DM_CHECK_ADDR_0 6955 if (address == 0) { 6956 drm_err(adev_to_drm(ctx->driver_context), 6957 "invalid register read; address = 0\n"); 6958 return 0; 6959 } 6960 #endif 6961 6962 amdgpu_dm_exit_ips_for_hw_access(ctx->dc); 6963 6964 value = cgs_read_register(ctx->cgs_device, address); 6965 6966 trace_amdgpu_dc_rreg(&ctx->perf_trace->read_count, address, value); 6967 6968 return value; 6969 } 6970 6971 void dm_acpi_process_phy_transition_interlock( 6972 const struct dc_context *ctx, 6973 struct dm_process_phy_transition_init_params process_phy_transition_init_params) 6974 { 6975 // Not yet implemented 6976 } 6977 EXPORT_IF_KUNIT(dm_acpi_process_phy_transition_interlock); 6978