1 /* 2 * Copyright 2015 Advanced Micro Devices, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 * 22 * Authors: AMD 23 * 24 */ 25 26 /* The caprices of the preprocessor require that this be declared right here */ 27 #define CREATE_TRACE_POINTS 28 29 #include "dm_services_types.h" 30 #include "dc.h" 31 #include "dc_link_dp.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_edid_parser.h" 40 #include "dc/dc_stat.h" 41 #include "amdgpu_dm_trace.h" 42 #include "dc/inc/dc_link_ddc.h" 43 #include "dpcd_defs.h" 44 #include "dc/inc/link_dpcd.h" 45 #include "link_service_types.h" 46 47 #include "vid.h" 48 #include "amdgpu.h" 49 #include "amdgpu_display.h" 50 #include "amdgpu_ucode.h" 51 #include "atom.h" 52 #include "amdgpu_dm.h" 53 #include "amdgpu_dm_plane.h" 54 #include "amdgpu_dm_crtc.h" 55 #ifdef CONFIG_DRM_AMD_DC_HDCP 56 #include "amdgpu_dm_hdcp.h" 57 #include <drm/display/drm_hdcp_helper.h> 58 #endif 59 #include "amdgpu_pm.h" 60 #include "amdgpu_atombios.h" 61 62 #include "amd_shared.h" 63 #include "amdgpu_dm_irq.h" 64 #include "dm_helpers.h" 65 #include "amdgpu_dm_mst_types.h" 66 #if defined(CONFIG_DEBUG_FS) 67 #include "amdgpu_dm_debugfs.h" 68 #endif 69 #include "amdgpu_dm_psr.h" 70 71 #include "ivsrcid/ivsrcid_vislands30.h" 72 73 #include "i2caux_interface.h" 74 #include <linux/module.h> 75 #include <linux/moduleparam.h> 76 #include <linux/types.h> 77 #include <linux/pm_runtime.h> 78 #include <linux/pci.h> 79 #include <linux/firmware.h> 80 #include <linux/component.h> 81 #include <linux/dmi.h> 82 83 #include <drm/display/drm_dp_mst_helper.h> 84 #include <drm/display/drm_hdmi_helper.h> 85 #include <drm/drm_atomic.h> 86 #include <drm/drm_atomic_uapi.h> 87 #include <drm/drm_atomic_helper.h> 88 #include <drm/drm_blend.h> 89 #include <drm/drm_fourcc.h> 90 #include <drm/drm_edid.h> 91 #include <drm/drm_vblank.h> 92 #include <drm/drm_audio_component.h> 93 #include <drm/drm_gem_atomic_helper.h> 94 #include <drm/drm_plane_helper.h> 95 96 #include <acpi/video.h> 97 98 #include "ivsrcid/dcn/irqsrcs_dcn_1_0.h" 99 100 #include "dcn/dcn_1_0_offset.h" 101 #include "dcn/dcn_1_0_sh_mask.h" 102 #include "soc15_hw_ip.h" 103 #include "soc15_common.h" 104 #include "vega10_ip_offset.h" 105 106 #include "gc/gc_11_0_0_offset.h" 107 #include "gc/gc_11_0_0_sh_mask.h" 108 109 #include "modules/inc/mod_freesync.h" 110 #include "modules/power/power_helpers.h" 111 #include "modules/inc/mod_info_packet.h" 112 113 #define FIRMWARE_RENOIR_DMUB "amdgpu/renoir_dmcub.bin" 114 MODULE_FIRMWARE(FIRMWARE_RENOIR_DMUB); 115 #define FIRMWARE_SIENNA_CICHLID_DMUB "amdgpu/sienna_cichlid_dmcub.bin" 116 MODULE_FIRMWARE(FIRMWARE_SIENNA_CICHLID_DMUB); 117 #define FIRMWARE_NAVY_FLOUNDER_DMUB "amdgpu/navy_flounder_dmcub.bin" 118 MODULE_FIRMWARE(FIRMWARE_NAVY_FLOUNDER_DMUB); 119 #define FIRMWARE_GREEN_SARDINE_DMUB "amdgpu/green_sardine_dmcub.bin" 120 MODULE_FIRMWARE(FIRMWARE_GREEN_SARDINE_DMUB); 121 #define FIRMWARE_VANGOGH_DMUB "amdgpu/vangogh_dmcub.bin" 122 MODULE_FIRMWARE(FIRMWARE_VANGOGH_DMUB); 123 #define FIRMWARE_DIMGREY_CAVEFISH_DMUB "amdgpu/dimgrey_cavefish_dmcub.bin" 124 MODULE_FIRMWARE(FIRMWARE_DIMGREY_CAVEFISH_DMUB); 125 #define FIRMWARE_BEIGE_GOBY_DMUB "amdgpu/beige_goby_dmcub.bin" 126 MODULE_FIRMWARE(FIRMWARE_BEIGE_GOBY_DMUB); 127 #define FIRMWARE_YELLOW_CARP_DMUB "amdgpu/yellow_carp_dmcub.bin" 128 MODULE_FIRMWARE(FIRMWARE_YELLOW_CARP_DMUB); 129 #define FIRMWARE_DCN_314_DMUB "amdgpu/dcn_3_1_4_dmcub.bin" 130 MODULE_FIRMWARE(FIRMWARE_DCN_314_DMUB); 131 #define FIRMWARE_DCN_315_DMUB "amdgpu/dcn_3_1_5_dmcub.bin" 132 MODULE_FIRMWARE(FIRMWARE_DCN_315_DMUB); 133 #define FIRMWARE_DCN316_DMUB "amdgpu/dcn_3_1_6_dmcub.bin" 134 MODULE_FIRMWARE(FIRMWARE_DCN316_DMUB); 135 136 #define FIRMWARE_DCN_V3_2_0_DMCUB "amdgpu/dcn_3_2_0_dmcub.bin" 137 MODULE_FIRMWARE(FIRMWARE_DCN_V3_2_0_DMCUB); 138 #define FIRMWARE_DCN_V3_2_1_DMCUB "amdgpu/dcn_3_2_1_dmcub.bin" 139 MODULE_FIRMWARE(FIRMWARE_DCN_V3_2_1_DMCUB); 140 141 #define FIRMWARE_RAVEN_DMCU "amdgpu/raven_dmcu.bin" 142 MODULE_FIRMWARE(FIRMWARE_RAVEN_DMCU); 143 144 #define FIRMWARE_NAVI12_DMCU "amdgpu/navi12_dmcu.bin" 145 MODULE_FIRMWARE(FIRMWARE_NAVI12_DMCU); 146 147 /* Number of bytes in PSP header for firmware. */ 148 #define PSP_HEADER_BYTES 0x100 149 150 /* Number of bytes in PSP footer for firmware. */ 151 #define PSP_FOOTER_BYTES 0x100 152 153 /** 154 * DOC: overview 155 * 156 * The AMDgpu display manager, **amdgpu_dm** (or even simpler, 157 * **dm**) sits between DRM and DC. It acts as a liaison, converting DRM 158 * requests into DC requests, and DC responses into DRM responses. 159 * 160 * The root control structure is &struct amdgpu_display_manager. 161 */ 162 163 /* basic init/fini API */ 164 static int amdgpu_dm_init(struct amdgpu_device *adev); 165 static void amdgpu_dm_fini(struct amdgpu_device *adev); 166 static bool is_freesync_video_mode(const struct drm_display_mode *mode, struct amdgpu_dm_connector *aconnector); 167 168 static enum drm_mode_subconnector get_subconnector_type(struct dc_link *link) 169 { 170 switch (link->dpcd_caps.dongle_type) { 171 case DISPLAY_DONGLE_NONE: 172 return DRM_MODE_SUBCONNECTOR_Native; 173 case DISPLAY_DONGLE_DP_VGA_CONVERTER: 174 return DRM_MODE_SUBCONNECTOR_VGA; 175 case DISPLAY_DONGLE_DP_DVI_CONVERTER: 176 case DISPLAY_DONGLE_DP_DVI_DONGLE: 177 return DRM_MODE_SUBCONNECTOR_DVID; 178 case DISPLAY_DONGLE_DP_HDMI_CONVERTER: 179 case DISPLAY_DONGLE_DP_HDMI_DONGLE: 180 return DRM_MODE_SUBCONNECTOR_HDMIA; 181 case DISPLAY_DONGLE_DP_HDMI_MISMATCHED_DONGLE: 182 default: 183 return DRM_MODE_SUBCONNECTOR_Unknown; 184 } 185 } 186 187 static void update_subconnector_property(struct amdgpu_dm_connector *aconnector) 188 { 189 struct dc_link *link = aconnector->dc_link; 190 struct drm_connector *connector = &aconnector->base; 191 enum drm_mode_subconnector subconnector = DRM_MODE_SUBCONNECTOR_Unknown; 192 193 if (connector->connector_type != DRM_MODE_CONNECTOR_DisplayPort) 194 return; 195 196 if (aconnector->dc_sink) 197 subconnector = get_subconnector_type(link); 198 199 drm_object_property_set_value(&connector->base, 200 connector->dev->mode_config.dp_subconnector_property, 201 subconnector); 202 } 203 204 /* 205 * initializes drm_device display related structures, based on the information 206 * provided by DAL. The drm strcutures are: drm_crtc, drm_connector, 207 * drm_encoder, drm_mode_config 208 * 209 * Returns 0 on success 210 */ 211 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev); 212 /* removes and deallocates the drm structures, created by the above function */ 213 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm); 214 215 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm, 216 struct amdgpu_dm_connector *amdgpu_dm_connector, 217 u32 link_index, 218 struct amdgpu_encoder *amdgpu_encoder); 219 static int amdgpu_dm_encoder_init(struct drm_device *dev, 220 struct amdgpu_encoder *aencoder, 221 uint32_t link_index); 222 223 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector); 224 225 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state); 226 227 static int amdgpu_dm_atomic_check(struct drm_device *dev, 228 struct drm_atomic_state *state); 229 230 static void handle_hpd_irq_helper(struct amdgpu_dm_connector *aconnector); 231 static void handle_hpd_rx_irq(void *param); 232 233 static bool 234 is_timing_unchanged_for_freesync(struct drm_crtc_state *old_crtc_state, 235 struct drm_crtc_state *new_crtc_state); 236 /* 237 * dm_vblank_get_counter 238 * 239 * @brief 240 * Get counter for number of vertical blanks 241 * 242 * @param 243 * struct amdgpu_device *adev - [in] desired amdgpu device 244 * int disp_idx - [in] which CRTC to get the counter from 245 * 246 * @return 247 * Counter for vertical blanks 248 */ 249 static u32 dm_vblank_get_counter(struct amdgpu_device *adev, int crtc) 250 { 251 if (crtc >= adev->mode_info.num_crtc) 252 return 0; 253 else { 254 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc]; 255 256 if (acrtc->dm_irq_params.stream == NULL) { 257 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n", 258 crtc); 259 return 0; 260 } 261 262 return dc_stream_get_vblank_counter(acrtc->dm_irq_params.stream); 263 } 264 } 265 266 static int dm_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc, 267 u32 *vbl, u32 *position) 268 { 269 u32 v_blank_start, v_blank_end, h_position, v_position; 270 271 if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc)) 272 return -EINVAL; 273 else { 274 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc]; 275 276 if (acrtc->dm_irq_params.stream == NULL) { 277 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n", 278 crtc); 279 return 0; 280 } 281 282 /* 283 * TODO rework base driver to use values directly. 284 * for now parse it back into reg-format 285 */ 286 dc_stream_get_scanoutpos(acrtc->dm_irq_params.stream, 287 &v_blank_start, 288 &v_blank_end, 289 &h_position, 290 &v_position); 291 292 *position = v_position | (h_position << 16); 293 *vbl = v_blank_start | (v_blank_end << 16); 294 } 295 296 return 0; 297 } 298 299 static bool dm_is_idle(void *handle) 300 { 301 /* XXX todo */ 302 return true; 303 } 304 305 static int dm_wait_for_idle(void *handle) 306 { 307 /* XXX todo */ 308 return 0; 309 } 310 311 static bool dm_check_soft_reset(void *handle) 312 { 313 return false; 314 } 315 316 static int dm_soft_reset(void *handle) 317 { 318 /* XXX todo */ 319 return 0; 320 } 321 322 static struct amdgpu_crtc * 323 get_crtc_by_otg_inst(struct amdgpu_device *adev, 324 int otg_inst) 325 { 326 struct drm_device *dev = adev_to_drm(adev); 327 struct drm_crtc *crtc; 328 struct amdgpu_crtc *amdgpu_crtc; 329 330 if (WARN_ON(otg_inst == -1)) 331 return adev->mode_info.crtcs[0]; 332 333 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 334 amdgpu_crtc = to_amdgpu_crtc(crtc); 335 336 if (amdgpu_crtc->otg_inst == otg_inst) 337 return amdgpu_crtc; 338 } 339 340 return NULL; 341 } 342 343 static inline bool is_dc_timing_adjust_needed(struct dm_crtc_state *old_state, 344 struct dm_crtc_state *new_state) 345 { 346 if (new_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED) 347 return true; 348 else if (amdgpu_dm_vrr_active(old_state) != amdgpu_dm_vrr_active(new_state)) 349 return true; 350 else 351 return false; 352 } 353 354 static inline void reverse_planes_order(struct dc_surface_update *array_of_surface_update, 355 int planes_count) 356 { 357 int i, j; 358 struct dc_surface_update surface_updates_temp; 359 360 for (i = 0, j = planes_count - 1; i < j; i++, j--) { 361 surface_updates_temp = array_of_surface_update[i]; 362 array_of_surface_update[i] = array_of_surface_update[j]; 363 array_of_surface_update[j] = surface_updates_temp; 364 } 365 } 366 367 /** 368 * update_planes_and_stream_adapter() - Send planes to be updated in DC 369 * 370 * DC has a generic way to update planes and stream via 371 * dc_update_planes_and_stream function; however, DM might need some 372 * adjustments and preparation before calling it. This function is a wrapper 373 * for the dc_update_planes_and_stream that does any required configuration 374 * before passing control to DC. 375 */ 376 static inline bool update_planes_and_stream_adapter(struct dc *dc, 377 int update_type, 378 int planes_count, 379 struct dc_stream_state *stream, 380 struct dc_stream_update *stream_update, 381 struct dc_surface_update *array_of_surface_update) 382 { 383 reverse_planes_order(array_of_surface_update, planes_count); 384 385 /* 386 * Previous frame finished and HW is ready for optimization. 387 */ 388 if (update_type == UPDATE_TYPE_FAST) 389 dc_post_update_surfaces_to_stream(dc); 390 391 return dc_update_planes_and_stream(dc, 392 array_of_surface_update, 393 planes_count, 394 stream, 395 stream_update); 396 } 397 398 /** 399 * dm_pflip_high_irq() - Handle pageflip interrupt 400 * @interrupt_params: ignored 401 * 402 * Handles the pageflip interrupt by notifying all interested parties 403 * that the pageflip has been completed. 404 */ 405 static void dm_pflip_high_irq(void *interrupt_params) 406 { 407 struct amdgpu_crtc *amdgpu_crtc; 408 struct common_irq_params *irq_params = interrupt_params; 409 struct amdgpu_device *adev = irq_params->adev; 410 unsigned long flags; 411 struct drm_pending_vblank_event *e; 412 u32 vpos, hpos, v_blank_start, v_blank_end; 413 bool vrr_active; 414 415 amdgpu_crtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_PFLIP); 416 417 /* IRQ could occur when in initial stage */ 418 /* TODO work and BO cleanup */ 419 if (amdgpu_crtc == NULL) { 420 DC_LOG_PFLIP("CRTC is null, returning.\n"); 421 return; 422 } 423 424 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags); 425 426 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED) { 427 DC_LOG_PFLIP("amdgpu_crtc->pflip_status = %d !=AMDGPU_FLIP_SUBMITTED(%d) on crtc:%d[%p]\n", 428 amdgpu_crtc->pflip_status, 429 AMDGPU_FLIP_SUBMITTED, 430 amdgpu_crtc->crtc_id, 431 amdgpu_crtc); 432 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags); 433 return; 434 } 435 436 /* page flip completed. */ 437 e = amdgpu_crtc->event; 438 amdgpu_crtc->event = NULL; 439 440 WARN_ON(!e); 441 442 vrr_active = amdgpu_dm_vrr_active_irq(amdgpu_crtc); 443 444 /* Fixed refresh rate, or VRR scanout position outside front-porch? */ 445 if (!vrr_active || 446 !dc_stream_get_scanoutpos(amdgpu_crtc->dm_irq_params.stream, &v_blank_start, 447 &v_blank_end, &hpos, &vpos) || 448 (vpos < v_blank_start)) { 449 /* Update to correct count and vblank timestamp if racing with 450 * vblank irq. This also updates to the correct vblank timestamp 451 * even in VRR mode, as scanout is past the front-porch atm. 452 */ 453 drm_crtc_accurate_vblank_count(&amdgpu_crtc->base); 454 455 /* Wake up userspace by sending the pageflip event with proper 456 * count and timestamp of vblank of flip completion. 457 */ 458 if (e) { 459 drm_crtc_send_vblank_event(&amdgpu_crtc->base, e); 460 461 /* Event sent, so done with vblank for this flip */ 462 drm_crtc_vblank_put(&amdgpu_crtc->base); 463 } 464 } else if (e) { 465 /* VRR active and inside front-porch: vblank count and 466 * timestamp for pageflip event will only be up to date after 467 * drm_crtc_handle_vblank() has been executed from late vblank 468 * irq handler after start of back-porch (vline 0). We queue the 469 * pageflip event for send-out by drm_crtc_handle_vblank() with 470 * updated timestamp and count, once it runs after us. 471 * 472 * We need to open-code this instead of using the helper 473 * drm_crtc_arm_vblank_event(), as that helper would 474 * call drm_crtc_accurate_vblank_count(), which we must 475 * not call in VRR mode while we are in front-porch! 476 */ 477 478 /* sequence will be replaced by real count during send-out. */ 479 e->sequence = drm_crtc_vblank_count(&amdgpu_crtc->base); 480 e->pipe = amdgpu_crtc->crtc_id; 481 482 list_add_tail(&e->base.link, &adev_to_drm(adev)->vblank_event_list); 483 e = NULL; 484 } 485 486 /* Keep track of vblank of this flip for flip throttling. We use the 487 * cooked hw counter, as that one incremented at start of this vblank 488 * of pageflip completion, so last_flip_vblank is the forbidden count 489 * for queueing new pageflips if vsync + VRR is enabled. 490 */ 491 amdgpu_crtc->dm_irq_params.last_flip_vblank = 492 amdgpu_get_vblank_counter_kms(&amdgpu_crtc->base); 493 494 amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE; 495 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags); 496 497 DC_LOG_PFLIP("crtc:%d[%p], pflip_stat:AMDGPU_FLIP_NONE, vrr[%d]-fp %d\n", 498 amdgpu_crtc->crtc_id, amdgpu_crtc, 499 vrr_active, (int) !e); 500 } 501 502 static void dm_vupdate_high_irq(void *interrupt_params) 503 { 504 struct common_irq_params *irq_params = interrupt_params; 505 struct amdgpu_device *adev = irq_params->adev; 506 struct amdgpu_crtc *acrtc; 507 struct drm_device *drm_dev; 508 struct drm_vblank_crtc *vblank; 509 ktime_t frame_duration_ns, previous_timestamp; 510 unsigned long flags; 511 int vrr_active; 512 513 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VUPDATE); 514 515 if (acrtc) { 516 vrr_active = amdgpu_dm_vrr_active_irq(acrtc); 517 drm_dev = acrtc->base.dev; 518 vblank = &drm_dev->vblank[acrtc->base.index]; 519 previous_timestamp = atomic64_read(&irq_params->previous_timestamp); 520 frame_duration_ns = vblank->time - previous_timestamp; 521 522 if (frame_duration_ns > 0) { 523 trace_amdgpu_refresh_rate_track(acrtc->base.index, 524 frame_duration_ns, 525 ktime_divns(NSEC_PER_SEC, frame_duration_ns)); 526 atomic64_set(&irq_params->previous_timestamp, vblank->time); 527 } 528 529 DC_LOG_VBLANK("crtc:%d, vupdate-vrr:%d\n", 530 acrtc->crtc_id, 531 vrr_active); 532 533 /* Core vblank handling is done here after end of front-porch in 534 * vrr mode, as vblank timestamping will give valid results 535 * while now done after front-porch. This will also deliver 536 * page-flip completion events that have been queued to us 537 * if a pageflip happened inside front-porch. 538 */ 539 if (vrr_active) { 540 dm_crtc_handle_vblank(acrtc); 541 542 /* BTR processing for pre-DCE12 ASICs */ 543 if (acrtc->dm_irq_params.stream && 544 adev->family < AMDGPU_FAMILY_AI) { 545 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags); 546 mod_freesync_handle_v_update( 547 adev->dm.freesync_module, 548 acrtc->dm_irq_params.stream, 549 &acrtc->dm_irq_params.vrr_params); 550 551 dc_stream_adjust_vmin_vmax( 552 adev->dm.dc, 553 acrtc->dm_irq_params.stream, 554 &acrtc->dm_irq_params.vrr_params.adjust); 555 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags); 556 } 557 } 558 } 559 } 560 561 /** 562 * dm_crtc_high_irq() - Handles CRTC interrupt 563 * @interrupt_params: used for determining the CRTC instance 564 * 565 * Handles the CRTC/VSYNC interrupt by notfying DRM's VBLANK 566 * event handler. 567 */ 568 static void dm_crtc_high_irq(void *interrupt_params) 569 { 570 struct common_irq_params *irq_params = interrupt_params; 571 struct amdgpu_device *adev = irq_params->adev; 572 struct amdgpu_crtc *acrtc; 573 unsigned long flags; 574 int vrr_active; 575 576 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VBLANK); 577 if (!acrtc) 578 return; 579 580 vrr_active = amdgpu_dm_vrr_active_irq(acrtc); 581 582 DC_LOG_VBLANK("crtc:%d, vupdate-vrr:%d, planes:%d\n", acrtc->crtc_id, 583 vrr_active, acrtc->dm_irq_params.active_planes); 584 585 /** 586 * Core vblank handling at start of front-porch is only possible 587 * in non-vrr mode, as only there vblank timestamping will give 588 * valid results while done in front-porch. Otherwise defer it 589 * to dm_vupdate_high_irq after end of front-porch. 590 */ 591 if (!vrr_active) 592 dm_crtc_handle_vblank(acrtc); 593 594 /** 595 * Following stuff must happen at start of vblank, for crc 596 * computation and below-the-range btr support in vrr mode. 597 */ 598 amdgpu_dm_crtc_handle_crc_irq(&acrtc->base); 599 600 /* BTR updates need to happen before VUPDATE on Vega and above. */ 601 if (adev->family < AMDGPU_FAMILY_AI) 602 return; 603 604 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags); 605 606 if (acrtc->dm_irq_params.stream && 607 acrtc->dm_irq_params.vrr_params.supported && 608 acrtc->dm_irq_params.freesync_config.state == 609 VRR_STATE_ACTIVE_VARIABLE) { 610 mod_freesync_handle_v_update(adev->dm.freesync_module, 611 acrtc->dm_irq_params.stream, 612 &acrtc->dm_irq_params.vrr_params); 613 614 dc_stream_adjust_vmin_vmax(adev->dm.dc, acrtc->dm_irq_params.stream, 615 &acrtc->dm_irq_params.vrr_params.adjust); 616 } 617 618 /* 619 * If there aren't any active_planes then DCH HUBP may be clock-gated. 620 * In that case, pageflip completion interrupts won't fire and pageflip 621 * completion events won't get delivered. Prevent this by sending 622 * pending pageflip events from here if a flip is still pending. 623 * 624 * If any planes are enabled, use dm_pflip_high_irq() instead, to 625 * avoid race conditions between flip programming and completion, 626 * which could cause too early flip completion events. 627 */ 628 if (adev->family >= AMDGPU_FAMILY_RV && 629 acrtc->pflip_status == AMDGPU_FLIP_SUBMITTED && 630 acrtc->dm_irq_params.active_planes == 0) { 631 if (acrtc->event) { 632 drm_crtc_send_vblank_event(&acrtc->base, acrtc->event); 633 acrtc->event = NULL; 634 drm_crtc_vblank_put(&acrtc->base); 635 } 636 acrtc->pflip_status = AMDGPU_FLIP_NONE; 637 } 638 639 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags); 640 } 641 642 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY) 643 /** 644 * dm_dcn_vertical_interrupt0_high_irq() - Handles OTG Vertical interrupt0 for 645 * DCN generation ASICs 646 * @interrupt_params: interrupt parameters 647 * 648 * Used to set crc window/read out crc value at vertical line 0 position 649 */ 650 static void dm_dcn_vertical_interrupt0_high_irq(void *interrupt_params) 651 { 652 struct common_irq_params *irq_params = interrupt_params; 653 struct amdgpu_device *adev = irq_params->adev; 654 struct amdgpu_crtc *acrtc; 655 656 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VLINE0); 657 658 if (!acrtc) 659 return; 660 661 amdgpu_dm_crtc_handle_crc_window_irq(&acrtc->base); 662 } 663 #endif /* CONFIG_DRM_AMD_SECURE_DISPLAY */ 664 665 /** 666 * dmub_aux_setconfig_callback - Callback for AUX or SET_CONFIG command. 667 * @adev: amdgpu_device pointer 668 * @notify: dmub notification structure 669 * 670 * Dmub AUX or SET_CONFIG command completion processing callback 671 * Copies dmub notification to DM which is to be read by AUX command. 672 * issuing thread and also signals the event to wake up the thread. 673 */ 674 static void dmub_aux_setconfig_callback(struct amdgpu_device *adev, 675 struct dmub_notification *notify) 676 { 677 if (adev->dm.dmub_notify) 678 memcpy(adev->dm.dmub_notify, notify, sizeof(struct dmub_notification)); 679 if (notify->type == DMUB_NOTIFICATION_AUX_REPLY) 680 complete(&adev->dm.dmub_aux_transfer_done); 681 } 682 683 /** 684 * dmub_hpd_callback - DMUB HPD interrupt processing callback. 685 * @adev: amdgpu_device pointer 686 * @notify: dmub notification structure 687 * 688 * Dmub Hpd interrupt processing callback. Gets displayindex through the 689 * ink index and calls helper to do the processing. 690 */ 691 static void dmub_hpd_callback(struct amdgpu_device *adev, 692 struct dmub_notification *notify) 693 { 694 struct amdgpu_dm_connector *aconnector; 695 struct amdgpu_dm_connector *hpd_aconnector = NULL; 696 struct drm_connector *connector; 697 struct drm_connector_list_iter iter; 698 struct dc_link *link; 699 u8 link_index = 0; 700 struct drm_device *dev; 701 702 if (adev == NULL) 703 return; 704 705 if (notify == NULL) { 706 DRM_ERROR("DMUB HPD callback notification was NULL"); 707 return; 708 } 709 710 if (notify->link_index > adev->dm.dc->link_count) { 711 DRM_ERROR("DMUB HPD index (%u)is abnormal", notify->link_index); 712 return; 713 } 714 715 link_index = notify->link_index; 716 link = adev->dm.dc->links[link_index]; 717 dev = adev->dm.ddev; 718 719 drm_connector_list_iter_begin(dev, &iter); 720 drm_for_each_connector_iter(connector, &iter) { 721 aconnector = to_amdgpu_dm_connector(connector); 722 if (link && aconnector->dc_link == link) { 723 DRM_INFO("DMUB HPD callback: link_index=%u\n", link_index); 724 hpd_aconnector = aconnector; 725 break; 726 } 727 } 728 drm_connector_list_iter_end(&iter); 729 730 if (hpd_aconnector) { 731 if (notify->type == DMUB_NOTIFICATION_HPD) 732 handle_hpd_irq_helper(hpd_aconnector); 733 else if (notify->type == DMUB_NOTIFICATION_HPD_IRQ) 734 handle_hpd_rx_irq(hpd_aconnector); 735 } 736 } 737 738 /** 739 * register_dmub_notify_callback - Sets callback for DMUB notify 740 * @adev: amdgpu_device pointer 741 * @type: Type of dmub notification 742 * @callback: Dmub interrupt callback function 743 * @dmub_int_thread_offload: offload indicator 744 * 745 * API to register a dmub callback handler for a dmub notification 746 * Also sets indicator whether callback processing to be offloaded. 747 * to dmub interrupt handling thread 748 * Return: true if successfully registered, false if there is existing registration 749 */ 750 static bool register_dmub_notify_callback(struct amdgpu_device *adev, 751 enum dmub_notification_type type, 752 dmub_notify_interrupt_callback_t callback, 753 bool dmub_int_thread_offload) 754 { 755 if (callback != NULL && type < ARRAY_SIZE(adev->dm.dmub_thread_offload)) { 756 adev->dm.dmub_callback[type] = callback; 757 adev->dm.dmub_thread_offload[type] = dmub_int_thread_offload; 758 } else 759 return false; 760 761 return true; 762 } 763 764 static void dm_handle_hpd_work(struct work_struct *work) 765 { 766 struct dmub_hpd_work *dmub_hpd_wrk; 767 768 dmub_hpd_wrk = container_of(work, struct dmub_hpd_work, handle_hpd_work); 769 770 if (!dmub_hpd_wrk->dmub_notify) { 771 DRM_ERROR("dmub_hpd_wrk dmub_notify is NULL"); 772 return; 773 } 774 775 if (dmub_hpd_wrk->dmub_notify->type < ARRAY_SIZE(dmub_hpd_wrk->adev->dm.dmub_callback)) { 776 dmub_hpd_wrk->adev->dm.dmub_callback[dmub_hpd_wrk->dmub_notify->type](dmub_hpd_wrk->adev, 777 dmub_hpd_wrk->dmub_notify); 778 } 779 780 kfree(dmub_hpd_wrk->dmub_notify); 781 kfree(dmub_hpd_wrk); 782 783 } 784 785 #define DMUB_TRACE_MAX_READ 64 786 /** 787 * dm_dmub_outbox1_low_irq() - Handles Outbox interrupt 788 * @interrupt_params: used for determining the Outbox instance 789 * 790 * Handles the Outbox Interrupt 791 * event handler. 792 */ 793 static void dm_dmub_outbox1_low_irq(void *interrupt_params) 794 { 795 struct dmub_notification notify; 796 struct common_irq_params *irq_params = interrupt_params; 797 struct amdgpu_device *adev = irq_params->adev; 798 struct amdgpu_display_manager *dm = &adev->dm; 799 struct dmcub_trace_buf_entry entry = { 0 }; 800 u32 count = 0; 801 struct dmub_hpd_work *dmub_hpd_wrk; 802 struct dc_link *plink = NULL; 803 804 if (dc_enable_dmub_notifications(adev->dm.dc) && 805 irq_params->irq_src == DC_IRQ_SOURCE_DMCUB_OUTBOX) { 806 807 do { 808 dc_stat_get_dmub_notification(adev->dm.dc, ¬ify); 809 if (notify.type >= ARRAY_SIZE(dm->dmub_thread_offload)) { 810 DRM_ERROR("DM: notify type %d invalid!", notify.type); 811 continue; 812 } 813 if (!dm->dmub_callback[notify.type]) { 814 DRM_DEBUG_DRIVER("DMUB notification skipped, no handler: type=%d\n", notify.type); 815 continue; 816 } 817 if (dm->dmub_thread_offload[notify.type] == true) { 818 dmub_hpd_wrk = kzalloc(sizeof(*dmub_hpd_wrk), GFP_ATOMIC); 819 if (!dmub_hpd_wrk) { 820 DRM_ERROR("Failed to allocate dmub_hpd_wrk"); 821 return; 822 } 823 dmub_hpd_wrk->dmub_notify = kzalloc(sizeof(struct dmub_notification), GFP_ATOMIC); 824 if (!dmub_hpd_wrk->dmub_notify) { 825 kfree(dmub_hpd_wrk); 826 DRM_ERROR("Failed to allocate dmub_hpd_wrk->dmub_notify"); 827 return; 828 } 829 INIT_WORK(&dmub_hpd_wrk->handle_hpd_work, dm_handle_hpd_work); 830 if (dmub_hpd_wrk->dmub_notify) 831 memcpy(dmub_hpd_wrk->dmub_notify, ¬ify, sizeof(struct dmub_notification)); 832 dmub_hpd_wrk->adev = adev; 833 if (notify.type == DMUB_NOTIFICATION_HPD) { 834 plink = adev->dm.dc->links[notify.link_index]; 835 if (plink) { 836 plink->hpd_status = 837 notify.hpd_status == DP_HPD_PLUG; 838 } 839 } 840 queue_work(adev->dm.delayed_hpd_wq, &dmub_hpd_wrk->handle_hpd_work); 841 } else { 842 dm->dmub_callback[notify.type](adev, ¬ify); 843 } 844 } while (notify.pending_notification); 845 } 846 847 848 do { 849 if (dc_dmub_srv_get_dmub_outbox0_msg(dm->dc, &entry)) { 850 trace_amdgpu_dmub_trace_high_irq(entry.trace_code, entry.tick_count, 851 entry.param0, entry.param1); 852 853 DRM_DEBUG_DRIVER("trace_code:%u, tick_count:%u, param0:%u, param1:%u\n", 854 entry.trace_code, entry.tick_count, entry.param0, entry.param1); 855 } else 856 break; 857 858 count++; 859 860 } while (count <= DMUB_TRACE_MAX_READ); 861 862 if (count > DMUB_TRACE_MAX_READ) 863 DRM_DEBUG_DRIVER("Warning : count > DMUB_TRACE_MAX_READ"); 864 } 865 866 static int dm_set_clockgating_state(void *handle, 867 enum amd_clockgating_state state) 868 { 869 return 0; 870 } 871 872 static int dm_set_powergating_state(void *handle, 873 enum amd_powergating_state state) 874 { 875 return 0; 876 } 877 878 /* Prototypes of private functions */ 879 static int dm_early_init(void *handle); 880 881 /* Allocate memory for FBC compressed data */ 882 static void amdgpu_dm_fbc_init(struct drm_connector *connector) 883 { 884 struct drm_device *dev = connector->dev; 885 struct amdgpu_device *adev = drm_to_adev(dev); 886 struct dm_compressor_info *compressor = &adev->dm.compressor; 887 struct amdgpu_dm_connector *aconn = to_amdgpu_dm_connector(connector); 888 struct drm_display_mode *mode; 889 unsigned long max_size = 0; 890 891 if (adev->dm.dc->fbc_compressor == NULL) 892 return; 893 894 if (aconn->dc_link->connector_signal != SIGNAL_TYPE_EDP) 895 return; 896 897 if (compressor->bo_ptr) 898 return; 899 900 901 list_for_each_entry(mode, &connector->modes, head) { 902 if (max_size < mode->htotal * mode->vtotal) 903 max_size = mode->htotal * mode->vtotal; 904 } 905 906 if (max_size) { 907 int r = amdgpu_bo_create_kernel(adev, max_size * 4, PAGE_SIZE, 908 AMDGPU_GEM_DOMAIN_GTT, &compressor->bo_ptr, 909 &compressor->gpu_addr, &compressor->cpu_addr); 910 911 if (r) 912 DRM_ERROR("DM: Failed to initialize FBC\n"); 913 else { 914 adev->dm.dc->ctx->fbc_gpu_addr = compressor->gpu_addr; 915 DRM_INFO("DM: FBC alloc %lu\n", max_size*4); 916 } 917 918 } 919 920 } 921 922 static int amdgpu_dm_audio_component_get_eld(struct device *kdev, int port, 923 int pipe, bool *enabled, 924 unsigned char *buf, int max_bytes) 925 { 926 struct drm_device *dev = dev_get_drvdata(kdev); 927 struct amdgpu_device *adev = drm_to_adev(dev); 928 struct drm_connector *connector; 929 struct drm_connector_list_iter conn_iter; 930 struct amdgpu_dm_connector *aconnector; 931 int ret = 0; 932 933 *enabled = false; 934 935 mutex_lock(&adev->dm.audio_lock); 936 937 drm_connector_list_iter_begin(dev, &conn_iter); 938 drm_for_each_connector_iter(connector, &conn_iter) { 939 aconnector = to_amdgpu_dm_connector(connector); 940 if (aconnector->audio_inst != port) 941 continue; 942 943 *enabled = true; 944 ret = drm_eld_size(connector->eld); 945 memcpy(buf, connector->eld, min(max_bytes, ret)); 946 947 break; 948 } 949 drm_connector_list_iter_end(&conn_iter); 950 951 mutex_unlock(&adev->dm.audio_lock); 952 953 DRM_DEBUG_KMS("Get ELD : idx=%d ret=%d en=%d\n", port, ret, *enabled); 954 955 return ret; 956 } 957 958 static const struct drm_audio_component_ops amdgpu_dm_audio_component_ops = { 959 .get_eld = amdgpu_dm_audio_component_get_eld, 960 }; 961 962 static int amdgpu_dm_audio_component_bind(struct device *kdev, 963 struct device *hda_kdev, void *data) 964 { 965 struct drm_device *dev = dev_get_drvdata(kdev); 966 struct amdgpu_device *adev = drm_to_adev(dev); 967 struct drm_audio_component *acomp = data; 968 969 acomp->ops = &amdgpu_dm_audio_component_ops; 970 acomp->dev = kdev; 971 adev->dm.audio_component = acomp; 972 973 return 0; 974 } 975 976 static void amdgpu_dm_audio_component_unbind(struct device *kdev, 977 struct device *hda_kdev, void *data) 978 { 979 struct drm_device *dev = dev_get_drvdata(kdev); 980 struct amdgpu_device *adev = drm_to_adev(dev); 981 struct drm_audio_component *acomp = data; 982 983 acomp->ops = NULL; 984 acomp->dev = NULL; 985 adev->dm.audio_component = NULL; 986 } 987 988 #ifdef notyet 989 static const struct component_ops amdgpu_dm_audio_component_bind_ops = { 990 .bind = amdgpu_dm_audio_component_bind, 991 .unbind = amdgpu_dm_audio_component_unbind, 992 }; 993 #endif 994 995 static int amdgpu_dm_audio_init(struct amdgpu_device *adev) 996 { 997 int i, ret; 998 999 if (!amdgpu_audio) 1000 return 0; 1001 1002 adev->mode_info.audio.enabled = true; 1003 1004 adev->mode_info.audio.num_pins = adev->dm.dc->res_pool->audio_count; 1005 1006 for (i = 0; i < adev->mode_info.audio.num_pins; i++) { 1007 adev->mode_info.audio.pin[i].channels = -1; 1008 adev->mode_info.audio.pin[i].rate = -1; 1009 adev->mode_info.audio.pin[i].bits_per_sample = -1; 1010 adev->mode_info.audio.pin[i].status_bits = 0; 1011 adev->mode_info.audio.pin[i].category_code = 0; 1012 adev->mode_info.audio.pin[i].connected = false; 1013 adev->mode_info.audio.pin[i].id = 1014 adev->dm.dc->res_pool->audios[i]->inst; 1015 adev->mode_info.audio.pin[i].offset = 0; 1016 } 1017 1018 ret = component_add(adev->dev, &amdgpu_dm_audio_component_bind_ops); 1019 if (ret < 0) 1020 return ret; 1021 1022 adev->dm.audio_registered = true; 1023 1024 return 0; 1025 } 1026 1027 static void amdgpu_dm_audio_fini(struct amdgpu_device *adev) 1028 { 1029 if (!amdgpu_audio) 1030 return; 1031 1032 if (!adev->mode_info.audio.enabled) 1033 return; 1034 1035 if (adev->dm.audio_registered) { 1036 component_del(adev->dev, &amdgpu_dm_audio_component_bind_ops); 1037 adev->dm.audio_registered = false; 1038 } 1039 1040 /* TODO: Disable audio? */ 1041 1042 adev->mode_info.audio.enabled = false; 1043 } 1044 1045 static void amdgpu_dm_audio_eld_notify(struct amdgpu_device *adev, int pin) 1046 { 1047 struct drm_audio_component *acomp = adev->dm.audio_component; 1048 1049 if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify) { 1050 DRM_DEBUG_KMS("Notify ELD: %d\n", pin); 1051 1052 acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr, 1053 pin, -1); 1054 } 1055 } 1056 1057 static int dm_dmub_hw_init(struct amdgpu_device *adev) 1058 { 1059 const struct dmcub_firmware_header_v1_0 *hdr; 1060 struct dmub_srv *dmub_srv = adev->dm.dmub_srv; 1061 struct dmub_srv_fb_info *fb_info = adev->dm.dmub_fb_info; 1062 const struct firmware *dmub_fw = adev->dm.dmub_fw; 1063 struct dmcu *dmcu = adev->dm.dc->res_pool->dmcu; 1064 struct abm *abm = adev->dm.dc->res_pool->abm; 1065 struct dmub_srv_hw_params hw_params; 1066 enum dmub_status status; 1067 const unsigned char *fw_inst_const, *fw_bss_data; 1068 u32 i, fw_inst_const_size, fw_bss_data_size; 1069 bool has_hw_support; 1070 1071 if (!dmub_srv) 1072 /* DMUB isn't supported on the ASIC. */ 1073 return 0; 1074 1075 if (!fb_info) { 1076 DRM_ERROR("No framebuffer info for DMUB service.\n"); 1077 return -EINVAL; 1078 } 1079 1080 if (!dmub_fw) { 1081 /* Firmware required for DMUB support. */ 1082 DRM_ERROR("No firmware provided for DMUB.\n"); 1083 return -EINVAL; 1084 } 1085 1086 status = dmub_srv_has_hw_support(dmub_srv, &has_hw_support); 1087 if (status != DMUB_STATUS_OK) { 1088 DRM_ERROR("Error checking HW support for DMUB: %d\n", status); 1089 return -EINVAL; 1090 } 1091 1092 if (!has_hw_support) { 1093 DRM_INFO("DMUB unsupported on ASIC\n"); 1094 return 0; 1095 } 1096 1097 /* Reset DMCUB if it was previously running - before we overwrite its memory. */ 1098 status = dmub_srv_hw_reset(dmub_srv); 1099 if (status != DMUB_STATUS_OK) 1100 DRM_WARN("Error resetting DMUB HW: %d\n", status); 1101 1102 hdr = (const struct dmcub_firmware_header_v1_0 *)dmub_fw->data; 1103 1104 fw_inst_const = dmub_fw->data + 1105 le32_to_cpu(hdr->header.ucode_array_offset_bytes) + 1106 PSP_HEADER_BYTES; 1107 1108 fw_bss_data = dmub_fw->data + 1109 le32_to_cpu(hdr->header.ucode_array_offset_bytes) + 1110 le32_to_cpu(hdr->inst_const_bytes); 1111 1112 /* Copy firmware and bios info into FB memory. */ 1113 fw_inst_const_size = le32_to_cpu(hdr->inst_const_bytes) - 1114 PSP_HEADER_BYTES - PSP_FOOTER_BYTES; 1115 1116 fw_bss_data_size = le32_to_cpu(hdr->bss_data_bytes); 1117 1118 /* if adev->firmware.load_type == AMDGPU_FW_LOAD_PSP, 1119 * amdgpu_ucode_init_single_fw will load dmub firmware 1120 * fw_inst_const part to cw0; otherwise, the firmware back door load 1121 * will be done by dm_dmub_hw_init 1122 */ 1123 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) { 1124 memcpy(fb_info->fb[DMUB_WINDOW_0_INST_CONST].cpu_addr, fw_inst_const, 1125 fw_inst_const_size); 1126 } 1127 1128 if (fw_bss_data_size) 1129 memcpy(fb_info->fb[DMUB_WINDOW_2_BSS_DATA].cpu_addr, 1130 fw_bss_data, fw_bss_data_size); 1131 1132 /* Copy firmware bios info into FB memory. */ 1133 memcpy(fb_info->fb[DMUB_WINDOW_3_VBIOS].cpu_addr, adev->bios, 1134 adev->bios_size); 1135 1136 /* Reset regions that need to be reset. */ 1137 memset(fb_info->fb[DMUB_WINDOW_4_MAILBOX].cpu_addr, 0, 1138 fb_info->fb[DMUB_WINDOW_4_MAILBOX].size); 1139 1140 memset(fb_info->fb[DMUB_WINDOW_5_TRACEBUFF].cpu_addr, 0, 1141 fb_info->fb[DMUB_WINDOW_5_TRACEBUFF].size); 1142 1143 memset(fb_info->fb[DMUB_WINDOW_6_FW_STATE].cpu_addr, 0, 1144 fb_info->fb[DMUB_WINDOW_6_FW_STATE].size); 1145 1146 /* Initialize hardware. */ 1147 memset(&hw_params, 0, sizeof(hw_params)); 1148 hw_params.fb_base = adev->gmc.fb_start; 1149 hw_params.fb_offset = adev->gmc.aper_base; 1150 1151 /* backdoor load firmware and trigger dmub running */ 1152 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) 1153 hw_params.load_inst_const = true; 1154 1155 if (dmcu) 1156 hw_params.psp_version = dmcu->psp_version; 1157 1158 for (i = 0; i < fb_info->num_fb; ++i) 1159 hw_params.fb[i] = &fb_info->fb[i]; 1160 1161 switch (adev->ip_versions[DCE_HWIP][0]) { 1162 case IP_VERSION(3, 1, 3): 1163 case IP_VERSION(3, 1, 4): 1164 hw_params.dpia_supported = true; 1165 hw_params.disable_dpia = adev->dm.dc->debug.dpia_debug.bits.disable_dpia; 1166 break; 1167 default: 1168 break; 1169 } 1170 1171 status = dmub_srv_hw_init(dmub_srv, &hw_params); 1172 if (status != DMUB_STATUS_OK) { 1173 DRM_ERROR("Error initializing DMUB HW: %d\n", status); 1174 return -EINVAL; 1175 } 1176 1177 /* Wait for firmware load to finish. */ 1178 status = dmub_srv_wait_for_auto_load(dmub_srv, 100000); 1179 if (status != DMUB_STATUS_OK) 1180 DRM_WARN("Wait for DMUB auto-load failed: %d\n", status); 1181 1182 /* Init DMCU and ABM if available. */ 1183 if (dmcu && abm) { 1184 dmcu->funcs->dmcu_init(dmcu); 1185 abm->dmcu_is_running = dmcu->funcs->is_dmcu_initialized(dmcu); 1186 } 1187 1188 if (!adev->dm.dc->ctx->dmub_srv) 1189 adev->dm.dc->ctx->dmub_srv = dc_dmub_srv_create(adev->dm.dc, dmub_srv); 1190 if (!adev->dm.dc->ctx->dmub_srv) { 1191 DRM_ERROR("Couldn't allocate DC DMUB server!\n"); 1192 return -ENOMEM; 1193 } 1194 1195 DRM_INFO("DMUB hardware initialized: version=0x%08X\n", 1196 adev->dm.dmcub_fw_version); 1197 1198 return 0; 1199 } 1200 1201 static void dm_dmub_hw_resume(struct amdgpu_device *adev) 1202 { 1203 struct dmub_srv *dmub_srv = adev->dm.dmub_srv; 1204 enum dmub_status status; 1205 bool init; 1206 1207 if (!dmub_srv) { 1208 /* DMUB isn't supported on the ASIC. */ 1209 return; 1210 } 1211 1212 status = dmub_srv_is_hw_init(dmub_srv, &init); 1213 if (status != DMUB_STATUS_OK) 1214 DRM_WARN("DMUB hardware init check failed: %d\n", status); 1215 1216 if (status == DMUB_STATUS_OK && init) { 1217 /* Wait for firmware load to finish. */ 1218 status = dmub_srv_wait_for_auto_load(dmub_srv, 100000); 1219 if (status != DMUB_STATUS_OK) 1220 DRM_WARN("Wait for DMUB auto-load failed: %d\n", status); 1221 } else { 1222 /* Perform the full hardware initialization. */ 1223 dm_dmub_hw_init(adev); 1224 } 1225 } 1226 1227 static void mmhub_read_system_context(struct amdgpu_device *adev, struct dc_phy_addr_space_config *pa_config) 1228 { 1229 u64 pt_base; 1230 u32 logical_addr_low; 1231 u32 logical_addr_high; 1232 u32 agp_base, agp_bot, agp_top; 1233 PHYSICAL_ADDRESS_LOC page_table_start, page_table_end, page_table_base; 1234 1235 memset(pa_config, 0, sizeof(*pa_config)); 1236 1237 agp_base = 0; 1238 agp_bot = adev->gmc.agp_start >> 24; 1239 agp_top = adev->gmc.agp_end >> 24; 1240 1241 /* AGP aperture is disabled */ 1242 if (agp_bot == agp_top) { 1243 logical_addr_low = adev->gmc.vram_start >> 18; 1244 if (adev->apu_flags & AMD_APU_IS_RAVEN2) 1245 /* 1246 * Raven2 has a HW issue that it is unable to use the vram which 1247 * is out of MC_VM_SYSTEM_APERTURE_HIGH_ADDR. So here is the 1248 * workaround that increase system aperture high address (add 1) 1249 * to get rid of the VM fault and hardware hang. 1250 */ 1251 logical_addr_high = (adev->gmc.fb_end >> 18) + 0x1; 1252 else 1253 logical_addr_high = adev->gmc.vram_end >> 18; 1254 } else { 1255 logical_addr_low = min(adev->gmc.fb_start, adev->gmc.agp_start) >> 18; 1256 if (adev->apu_flags & AMD_APU_IS_RAVEN2) 1257 /* 1258 * Raven2 has a HW issue that it is unable to use the vram which 1259 * is out of MC_VM_SYSTEM_APERTURE_HIGH_ADDR. So here is the 1260 * workaround that increase system aperture high address (add 1) 1261 * to get rid of the VM fault and hardware hang. 1262 */ 1263 logical_addr_high = max((adev->gmc.fb_end >> 18) + 0x1, adev->gmc.agp_end >> 18); 1264 else 1265 logical_addr_high = max(adev->gmc.fb_end, adev->gmc.agp_end) >> 18; 1266 } 1267 1268 pt_base = amdgpu_gmc_pd_addr(adev->gart.bo); 1269 1270 page_table_start.high_part = upper_32_bits(adev->gmc.gart_start >> 1271 AMDGPU_GPU_PAGE_SHIFT); 1272 page_table_start.low_part = lower_32_bits(adev->gmc.gart_start >> 1273 AMDGPU_GPU_PAGE_SHIFT); 1274 page_table_end.high_part = upper_32_bits(adev->gmc.gart_end >> 1275 AMDGPU_GPU_PAGE_SHIFT); 1276 page_table_end.low_part = lower_32_bits(adev->gmc.gart_end >> 1277 AMDGPU_GPU_PAGE_SHIFT); 1278 page_table_base.high_part = upper_32_bits(pt_base); 1279 page_table_base.low_part = lower_32_bits(pt_base); 1280 1281 pa_config->system_aperture.start_addr = (uint64_t)logical_addr_low << 18; 1282 pa_config->system_aperture.end_addr = (uint64_t)logical_addr_high << 18; 1283 1284 pa_config->system_aperture.agp_base = (uint64_t)agp_base << 24; 1285 pa_config->system_aperture.agp_bot = (uint64_t)agp_bot << 24; 1286 pa_config->system_aperture.agp_top = (uint64_t)agp_top << 24; 1287 1288 pa_config->system_aperture.fb_base = adev->gmc.fb_start; 1289 pa_config->system_aperture.fb_offset = adev->gmc.aper_base; 1290 pa_config->system_aperture.fb_top = adev->gmc.fb_end; 1291 1292 pa_config->gart_config.page_table_start_addr = page_table_start.quad_part << 12; 1293 pa_config->gart_config.page_table_end_addr = page_table_end.quad_part << 12; 1294 pa_config->gart_config.page_table_base_addr = page_table_base.quad_part; 1295 1296 pa_config->is_hvm_enabled = adev->mode_info.gpu_vm_support; 1297 1298 } 1299 1300 static void force_connector_state( 1301 struct amdgpu_dm_connector *aconnector, 1302 enum drm_connector_force force_state) 1303 { 1304 struct drm_connector *connector = &aconnector->base; 1305 1306 mutex_lock(&connector->dev->mode_config.mutex); 1307 aconnector->base.force = force_state; 1308 mutex_unlock(&connector->dev->mode_config.mutex); 1309 1310 mutex_lock(&aconnector->hpd_lock); 1311 drm_kms_helper_connector_hotplug_event(connector); 1312 mutex_unlock(&aconnector->hpd_lock); 1313 } 1314 1315 static void dm_handle_hpd_rx_offload_work(struct work_struct *work) 1316 { 1317 struct hpd_rx_irq_offload_work *offload_work; 1318 struct amdgpu_dm_connector *aconnector; 1319 struct dc_link *dc_link; 1320 struct amdgpu_device *adev; 1321 enum dc_connection_type new_connection_type = dc_connection_none; 1322 unsigned long flags; 1323 union test_response test_response; 1324 1325 memset(&test_response, 0, sizeof(test_response)); 1326 1327 offload_work = container_of(work, struct hpd_rx_irq_offload_work, work); 1328 aconnector = offload_work->offload_wq->aconnector; 1329 1330 if (!aconnector) { 1331 DRM_ERROR("Can't retrieve aconnector in hpd_rx_irq_offload_work"); 1332 goto skip; 1333 } 1334 1335 adev = drm_to_adev(aconnector->base.dev); 1336 dc_link = aconnector->dc_link; 1337 1338 mutex_lock(&aconnector->hpd_lock); 1339 if (!dc_link_detect_sink(dc_link, &new_connection_type)) 1340 DRM_ERROR("KMS: Failed to detect connector\n"); 1341 mutex_unlock(&aconnector->hpd_lock); 1342 1343 if (new_connection_type == dc_connection_none) 1344 goto skip; 1345 1346 if (amdgpu_in_reset(adev)) 1347 goto skip; 1348 1349 if (offload_work->data.bytes.device_service_irq.bits.UP_REQ_MSG_RDY || 1350 offload_work->data.bytes.device_service_irq.bits.DOWN_REP_MSG_RDY) { 1351 dm_handle_mst_sideband_msg_ready_event(&aconnector->mst_mgr, DOWN_OR_UP_MSG_RDY_EVENT); 1352 spin_lock_irqsave(&offload_work->offload_wq->offload_lock, flags); 1353 offload_work->offload_wq->is_handling_mst_msg_rdy_event = false; 1354 spin_unlock_irqrestore(&offload_work->offload_wq->offload_lock, flags); 1355 goto skip; 1356 } 1357 1358 mutex_lock(&adev->dm.dc_lock); 1359 if (offload_work->data.bytes.device_service_irq.bits.AUTOMATED_TEST) { 1360 dc_link_dp_handle_automated_test(dc_link); 1361 1362 if (aconnector->timing_changed) { 1363 /* force connector disconnect and reconnect */ 1364 force_connector_state(aconnector, DRM_FORCE_OFF); 1365 drm_msleep(100); 1366 force_connector_state(aconnector, DRM_FORCE_UNSPECIFIED); 1367 } 1368 1369 test_response.bits.ACK = 1; 1370 1371 core_link_write_dpcd( 1372 dc_link, 1373 DP_TEST_RESPONSE, 1374 &test_response.raw, 1375 sizeof(test_response)); 1376 } else if ((dc_link->connector_signal != SIGNAL_TYPE_EDP) && 1377 hpd_rx_irq_check_link_loss_status(dc_link, &offload_work->data) && 1378 dc_link_dp_allow_hpd_rx_irq(dc_link)) { 1379 /* offload_work->data is from handle_hpd_rx_irq-> 1380 * schedule_hpd_rx_offload_work.this is defer handle 1381 * for hpd short pulse. upon here, link status may be 1382 * changed, need get latest link status from dpcd 1383 * registers. if link status is good, skip run link 1384 * training again. 1385 */ 1386 union hpd_irq_data irq_data; 1387 1388 memset(&irq_data, 0, sizeof(irq_data)); 1389 1390 /* before dc_link_dp_handle_link_loss, allow new link lost handle 1391 * request be added to work queue if link lost at end of dc_link_ 1392 * dp_handle_link_loss 1393 */ 1394 spin_lock_irqsave(&offload_work->offload_wq->offload_lock, flags); 1395 offload_work->offload_wq->is_handling_link_loss = false; 1396 spin_unlock_irqrestore(&offload_work->offload_wq->offload_lock, flags); 1397 1398 if ((read_hpd_rx_irq_data(dc_link, &irq_data) == DC_OK) && 1399 hpd_rx_irq_check_link_loss_status(dc_link, &irq_data)) 1400 dc_link_dp_handle_link_loss(dc_link); 1401 } 1402 mutex_unlock(&adev->dm.dc_lock); 1403 1404 skip: 1405 kfree(offload_work); 1406 1407 } 1408 1409 static struct hpd_rx_irq_offload_work_queue *hpd_rx_irq_create_workqueue(struct dc *dc) 1410 { 1411 int max_caps = dc->caps.max_links; 1412 int i = 0; 1413 struct hpd_rx_irq_offload_work_queue *hpd_rx_offload_wq = NULL; 1414 1415 hpd_rx_offload_wq = kcalloc(max_caps, sizeof(*hpd_rx_offload_wq), GFP_KERNEL); 1416 1417 if (!hpd_rx_offload_wq) 1418 return NULL; 1419 1420 1421 for (i = 0; i < max_caps; i++) { 1422 hpd_rx_offload_wq[i].wq = 1423 create_singlethread_workqueue("amdgpu_dm_hpd_rx_offload_wq"); 1424 1425 if (hpd_rx_offload_wq[i].wq == NULL) { 1426 DRM_ERROR("create amdgpu_dm_hpd_rx_offload_wq fail!"); 1427 goto out_err; 1428 } 1429 1430 mtx_init(&hpd_rx_offload_wq[i].offload_lock, IPL_TTY); 1431 } 1432 1433 return hpd_rx_offload_wq; 1434 1435 out_err: 1436 for (i = 0; i < max_caps; i++) { 1437 if (hpd_rx_offload_wq[i].wq) 1438 destroy_workqueue(hpd_rx_offload_wq[i].wq); 1439 } 1440 kfree(hpd_rx_offload_wq); 1441 return NULL; 1442 } 1443 1444 struct amdgpu_stutter_quirk { 1445 u16 chip_vendor; 1446 u16 chip_device; 1447 u16 subsys_vendor; 1448 u16 subsys_device; 1449 u8 revision; 1450 }; 1451 1452 static const struct amdgpu_stutter_quirk amdgpu_stutter_quirk_list[] = { 1453 /* https://bugzilla.kernel.org/show_bug.cgi?id=214417 */ 1454 { 0x1002, 0x15dd, 0x1002, 0x15dd, 0xc8 }, 1455 { 0, 0, 0, 0, 0 }, 1456 }; 1457 1458 static bool dm_should_disable_stutter(struct pci_dev *pdev) 1459 { 1460 const struct amdgpu_stutter_quirk *p = amdgpu_stutter_quirk_list; 1461 1462 while (p && p->chip_device != 0) { 1463 if (pdev->vendor == p->chip_vendor && 1464 pdev->device == p->chip_device && 1465 pdev->subsystem_vendor == p->subsys_vendor && 1466 pdev->subsystem_device == p->subsys_device && 1467 pdev->revision == p->revision) { 1468 return true; 1469 } 1470 ++p; 1471 } 1472 return false; 1473 } 1474 1475 static const struct dmi_system_id hpd_disconnect_quirk_table[] = { 1476 { 1477 .matches = { 1478 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1479 DMI_MATCH(DMI_PRODUCT_NAME, "Precision 3660"), 1480 }, 1481 }, 1482 { 1483 .matches = { 1484 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1485 DMI_MATCH(DMI_PRODUCT_NAME, "Precision 3260"), 1486 }, 1487 }, 1488 { 1489 .matches = { 1490 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1491 DMI_MATCH(DMI_PRODUCT_NAME, "Precision 3460"), 1492 }, 1493 }, 1494 { 1495 .matches = { 1496 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1497 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex Tower Plus 7010"), 1498 }, 1499 }, 1500 { 1501 .matches = { 1502 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1503 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex Tower 7010"), 1504 }, 1505 }, 1506 { 1507 .matches = { 1508 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1509 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex SFF Plus 7010"), 1510 }, 1511 }, 1512 { 1513 .matches = { 1514 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1515 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex SFF 7010"), 1516 }, 1517 }, 1518 { 1519 .matches = { 1520 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1521 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex Micro Plus 7010"), 1522 }, 1523 }, 1524 { 1525 .matches = { 1526 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 1527 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex Micro 7010"), 1528 }, 1529 }, 1530 {} 1531 /* TODO: refactor this from a fixed table to a dynamic option */ 1532 }; 1533 1534 static void retrieve_dmi_info(struct amdgpu_display_manager *dm) 1535 { 1536 const struct dmi_system_id *dmi_id; 1537 1538 dm->aux_hpd_discon_quirk = false; 1539 1540 dmi_id = dmi_first_match(hpd_disconnect_quirk_table); 1541 if (dmi_id) { 1542 dm->aux_hpd_discon_quirk = true; 1543 DRM_INFO("aux_hpd_discon_quirk attached\n"); 1544 } 1545 } 1546 1547 static int amdgpu_dm_init(struct amdgpu_device *adev) 1548 { 1549 struct dc_init_data init_data; 1550 #ifdef CONFIG_DRM_AMD_DC_HDCP 1551 struct dc_callback_init init_params; 1552 #endif 1553 int r; 1554 1555 adev->dm.ddev = adev_to_drm(adev); 1556 adev->dm.adev = adev; 1557 1558 /* Zero all the fields */ 1559 memset(&init_data, 0, sizeof(init_data)); 1560 #ifdef CONFIG_DRM_AMD_DC_HDCP 1561 memset(&init_params, 0, sizeof(init_params)); 1562 #endif 1563 1564 rw_init(&adev->dm.dpia_aux_lock, "dmdpia"); 1565 rw_init(&adev->dm.dc_lock, "dmdc"); 1566 rw_init(&adev->dm.audio_lock, "dmaud"); 1567 mtx_init(&adev->dm.vblank_lock, IPL_TTY); 1568 1569 if (amdgpu_dm_irq_init(adev)) { 1570 DRM_ERROR("amdgpu: failed to initialize DM IRQ support.\n"); 1571 goto error; 1572 } 1573 1574 init_data.asic_id.chip_family = adev->family; 1575 1576 init_data.asic_id.pci_revision_id = adev->pdev->revision; 1577 init_data.asic_id.hw_internal_rev = adev->external_rev_id; 1578 init_data.asic_id.chip_id = adev->pdev->device; 1579 1580 init_data.asic_id.vram_width = adev->gmc.vram_width; 1581 /* TODO: initialize init_data.asic_id.vram_type here!!!! */ 1582 init_data.asic_id.atombios_base_address = 1583 adev->mode_info.atom_context->bios; 1584 1585 init_data.driver = adev; 1586 1587 adev->dm.cgs_device = amdgpu_cgs_create_device(adev); 1588 1589 if (!adev->dm.cgs_device) { 1590 DRM_ERROR("amdgpu: failed to create cgs device.\n"); 1591 goto error; 1592 } 1593 1594 init_data.cgs_device = adev->dm.cgs_device; 1595 1596 init_data.dce_environment = DCE_ENV_PRODUCTION_DRV; 1597 1598 switch (adev->ip_versions[DCE_HWIP][0]) { 1599 case IP_VERSION(2, 1, 0): 1600 switch (adev->dm.dmcub_fw_version) { 1601 case 0: /* development */ 1602 case 0x1: /* linux-firmware.git hash 6d9f399 */ 1603 case 0x01000000: /* linux-firmware.git hash 9a0b0f4 */ 1604 init_data.flags.disable_dmcu = false; 1605 break; 1606 default: 1607 init_data.flags.disable_dmcu = true; 1608 } 1609 break; 1610 case IP_VERSION(2, 0, 3): 1611 init_data.flags.disable_dmcu = true; 1612 break; 1613 default: 1614 break; 1615 } 1616 1617 switch (adev->asic_type) { 1618 case CHIP_CARRIZO: 1619 case CHIP_STONEY: 1620 init_data.flags.gpu_vm_support = true; 1621 break; 1622 default: 1623 switch (adev->ip_versions[DCE_HWIP][0]) { 1624 case IP_VERSION(1, 0, 0): 1625 case IP_VERSION(1, 0, 1): 1626 /* enable S/G on PCO and RV2 */ 1627 if ((adev->apu_flags & AMD_APU_IS_RAVEN2) || 1628 (adev->apu_flags & AMD_APU_IS_PICASSO)) 1629 init_data.flags.gpu_vm_support = true; 1630 break; 1631 case IP_VERSION(2, 1, 0): 1632 case IP_VERSION(3, 0, 1): 1633 case IP_VERSION(3, 1, 2): 1634 case IP_VERSION(3, 1, 3): 1635 case IP_VERSION(3, 1, 6): 1636 init_data.flags.gpu_vm_support = true; 1637 break; 1638 default: 1639 break; 1640 } 1641 break; 1642 } 1643 if (init_data.flags.gpu_vm_support && 1644 (amdgpu_sg_display == 0)) 1645 init_data.flags.gpu_vm_support = false; 1646 1647 if (init_data.flags.gpu_vm_support) 1648 adev->mode_info.gpu_vm_support = true; 1649 1650 if (amdgpu_dc_feature_mask & DC_FBC_MASK) 1651 init_data.flags.fbc_support = true; 1652 1653 if (amdgpu_dc_feature_mask & DC_MULTI_MON_PP_MCLK_SWITCH_MASK) 1654 init_data.flags.multi_mon_pp_mclk_switch = true; 1655 1656 if (amdgpu_dc_feature_mask & DC_DISABLE_FRACTIONAL_PWM_MASK) 1657 init_data.flags.disable_fractional_pwm = true; 1658 1659 if (amdgpu_dc_feature_mask & DC_EDP_NO_POWER_SEQUENCING) 1660 init_data.flags.edp_no_power_sequencing = true; 1661 1662 if (amdgpu_dc_feature_mask & DC_DISABLE_LTTPR_DP1_4A) 1663 init_data.flags.allow_lttpr_non_transparent_mode.bits.DP1_4A = true; 1664 if (amdgpu_dc_feature_mask & DC_DISABLE_LTTPR_DP2_0) 1665 init_data.flags.allow_lttpr_non_transparent_mode.bits.DP2_0 = true; 1666 1667 init_data.flags.seamless_boot_edp_requested = false; 1668 1669 if (check_seamless_boot_capability(adev)) { 1670 init_data.flags.seamless_boot_edp_requested = true; 1671 init_data.flags.allow_seamless_boot_optimization = true; 1672 DRM_INFO("Seamless boot condition check passed\n"); 1673 } 1674 1675 init_data.flags.enable_mipi_converter_optimization = true; 1676 1677 init_data.dcn_reg_offsets = adev->reg_offset[DCE_HWIP][0]; 1678 init_data.nbio_reg_offsets = adev->reg_offset[NBIO_HWIP][0]; 1679 1680 INIT_LIST_HEAD(&adev->dm.da_list); 1681 1682 retrieve_dmi_info(&adev->dm); 1683 1684 /* Display Core create. */ 1685 adev->dm.dc = dc_create(&init_data); 1686 1687 if (adev->dm.dc) { 1688 DRM_INFO("Display Core initialized with v%s!\n", DC_VER); 1689 } else { 1690 DRM_INFO("Display Core failed to initialize with v%s!\n", DC_VER); 1691 goto error; 1692 } 1693 1694 if (amdgpu_dc_debug_mask & DC_DISABLE_PIPE_SPLIT) { 1695 adev->dm.dc->debug.force_single_disp_pipe_split = false; 1696 adev->dm.dc->debug.pipe_split_policy = MPC_SPLIT_AVOID; 1697 } 1698 1699 if (adev->asic_type != CHIP_CARRIZO && adev->asic_type != CHIP_STONEY) 1700 adev->dm.dc->debug.disable_stutter = amdgpu_pp_feature_mask & PP_STUTTER_MODE ? false : true; 1701 if (dm_should_disable_stutter(adev->pdev)) 1702 adev->dm.dc->debug.disable_stutter = true; 1703 1704 if (amdgpu_dc_debug_mask & DC_DISABLE_STUTTER) 1705 adev->dm.dc->debug.disable_stutter = true; 1706 1707 if (amdgpu_dc_debug_mask & DC_DISABLE_DSC) 1708 adev->dm.dc->debug.disable_dsc = true; 1709 1710 if (amdgpu_dc_debug_mask & DC_DISABLE_CLOCK_GATING) 1711 adev->dm.dc->debug.disable_clock_gate = true; 1712 1713 if (amdgpu_dc_debug_mask & DC_FORCE_SUBVP_MCLK_SWITCH) 1714 adev->dm.dc->debug.force_subvp_mclk_switch = true; 1715 1716 adev->dm.dc->debug.visual_confirm = amdgpu_dc_visual_confirm; 1717 1718 /* TODO: Remove after DP2 receiver gets proper support of Cable ID feature */ 1719 adev->dm.dc->debug.ignore_cable_id = true; 1720 1721 r = dm_dmub_hw_init(adev); 1722 if (r) { 1723 DRM_ERROR("DMUB interface failed to initialize: status=%d\n", r); 1724 goto error; 1725 } 1726 1727 dc_hardware_init(adev->dm.dc); 1728 1729 adev->dm.hpd_rx_offload_wq = hpd_rx_irq_create_workqueue(adev->dm.dc); 1730 if (!adev->dm.hpd_rx_offload_wq) { 1731 DRM_ERROR("amdgpu: failed to create hpd rx offload workqueue.\n"); 1732 goto error; 1733 } 1734 1735 if ((adev->flags & AMD_IS_APU) && (adev->asic_type >= CHIP_CARRIZO)) { 1736 struct dc_phy_addr_space_config pa_config; 1737 1738 mmhub_read_system_context(adev, &pa_config); 1739 1740 // Call the DC init_memory func 1741 dc_setup_system_context(adev->dm.dc, &pa_config); 1742 } 1743 1744 adev->dm.freesync_module = mod_freesync_create(adev->dm.dc); 1745 if (!adev->dm.freesync_module) { 1746 DRM_ERROR( 1747 "amdgpu: failed to initialize freesync_module.\n"); 1748 } else 1749 DRM_DEBUG_DRIVER("amdgpu: freesync_module init done %p.\n", 1750 adev->dm.freesync_module); 1751 1752 amdgpu_dm_init_color_mod(); 1753 1754 if (adev->dm.dc->caps.max_links > 0) { 1755 adev->dm.vblank_control_workqueue = 1756 create_singlethread_workqueue("dm_vblank_control_workqueue"); 1757 if (!adev->dm.vblank_control_workqueue) 1758 DRM_ERROR("amdgpu: failed to initialize vblank_workqueue.\n"); 1759 } 1760 1761 #ifdef CONFIG_DRM_AMD_DC_HDCP 1762 if (adev->dm.dc->caps.max_links > 0 && adev->family >= AMDGPU_FAMILY_RV) { 1763 adev->dm.hdcp_workqueue = hdcp_create_workqueue(adev, &init_params.cp_psp, adev->dm.dc); 1764 1765 if (!adev->dm.hdcp_workqueue) 1766 DRM_ERROR("amdgpu: failed to initialize hdcp_workqueue.\n"); 1767 else 1768 DRM_DEBUG_DRIVER("amdgpu: hdcp_workqueue init done %p.\n", adev->dm.hdcp_workqueue); 1769 1770 dc_init_callbacks(adev->dm.dc, &init_params); 1771 } 1772 #endif 1773 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY) 1774 adev->dm.crc_rd_wrk = amdgpu_dm_crtc_secure_display_create_work(); 1775 #endif 1776 if (dc_is_dmub_outbox_supported(adev->dm.dc)) { 1777 init_completion(&adev->dm.dmub_aux_transfer_done); 1778 adev->dm.dmub_notify = kzalloc(sizeof(struct dmub_notification), GFP_KERNEL); 1779 if (!adev->dm.dmub_notify) { 1780 DRM_INFO("amdgpu: fail to allocate adev->dm.dmub_notify"); 1781 goto error; 1782 } 1783 1784 adev->dm.delayed_hpd_wq = create_singlethread_workqueue("amdgpu_dm_hpd_wq"); 1785 if (!adev->dm.delayed_hpd_wq) { 1786 DRM_ERROR("amdgpu: failed to create hpd offload workqueue.\n"); 1787 goto error; 1788 } 1789 1790 amdgpu_dm_outbox_init(adev); 1791 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_AUX_REPLY, 1792 dmub_aux_setconfig_callback, false)) { 1793 DRM_ERROR("amdgpu: fail to register dmub aux callback"); 1794 goto error; 1795 } 1796 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_HPD, dmub_hpd_callback, true)) { 1797 DRM_ERROR("amdgpu: fail to register dmub hpd callback"); 1798 goto error; 1799 } 1800 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_HPD_IRQ, dmub_hpd_callback, true)) { 1801 DRM_ERROR("amdgpu: fail to register dmub hpd callback"); 1802 goto error; 1803 } 1804 } 1805 1806 /* Enable outbox notification only after IRQ handlers are registered and DMUB is alive. 1807 * It is expected that DMUB will resend any pending notifications at this point, for 1808 * example HPD from DPIA. 1809 */ 1810 if (dc_is_dmub_outbox_supported(adev->dm.dc)) 1811 dc_enable_dmub_outbox(adev->dm.dc); 1812 1813 if (amdgpu_dm_initialize_drm_device(adev)) { 1814 DRM_ERROR( 1815 "amdgpu: failed to initialize sw for display support.\n"); 1816 goto error; 1817 } 1818 1819 /* create fake encoders for MST */ 1820 dm_dp_create_fake_mst_encoders(adev); 1821 1822 /* TODO: Add_display_info? */ 1823 1824 /* TODO use dynamic cursor width */ 1825 adev_to_drm(adev)->mode_config.cursor_width = adev->dm.dc->caps.max_cursor_size; 1826 adev_to_drm(adev)->mode_config.cursor_height = adev->dm.dc->caps.max_cursor_size; 1827 1828 if (drm_vblank_init(adev_to_drm(adev), adev->dm.display_indexes_num)) { 1829 DRM_ERROR( 1830 "amdgpu: failed to initialize sw for display support.\n"); 1831 goto error; 1832 } 1833 1834 1835 DRM_DEBUG_DRIVER("KMS initialized.\n"); 1836 1837 return 0; 1838 error: 1839 amdgpu_dm_fini(adev); 1840 1841 return -EINVAL; 1842 } 1843 1844 static int amdgpu_dm_early_fini(void *handle) 1845 { 1846 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 1847 1848 amdgpu_dm_audio_fini(adev); 1849 1850 return 0; 1851 } 1852 1853 static void amdgpu_dm_fini(struct amdgpu_device *adev) 1854 { 1855 int i; 1856 1857 if (adev->dm.vblank_control_workqueue) { 1858 destroy_workqueue(adev->dm.vblank_control_workqueue); 1859 adev->dm.vblank_control_workqueue = NULL; 1860 } 1861 1862 amdgpu_dm_destroy_drm_device(&adev->dm); 1863 1864 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY) 1865 if (adev->dm.crc_rd_wrk) { 1866 flush_work(&adev->dm.crc_rd_wrk->notify_ta_work); 1867 kfree(adev->dm.crc_rd_wrk); 1868 adev->dm.crc_rd_wrk = NULL; 1869 } 1870 #endif 1871 #ifdef CONFIG_DRM_AMD_DC_HDCP 1872 if (adev->dm.hdcp_workqueue) { 1873 hdcp_destroy(&adev->dev->kobj, adev->dm.hdcp_workqueue); 1874 adev->dm.hdcp_workqueue = NULL; 1875 } 1876 1877 if (adev->dm.dc) 1878 dc_deinit_callbacks(adev->dm.dc); 1879 #endif 1880 1881 if (adev->dm.dc) 1882 dc_dmub_srv_destroy(&adev->dm.dc->ctx->dmub_srv); 1883 1884 if (dc_enable_dmub_notifications(adev->dm.dc)) { 1885 kfree(adev->dm.dmub_notify); 1886 adev->dm.dmub_notify = NULL; 1887 destroy_workqueue(adev->dm.delayed_hpd_wq); 1888 adev->dm.delayed_hpd_wq = NULL; 1889 } 1890 1891 if (adev->dm.dmub_bo) 1892 amdgpu_bo_free_kernel(&adev->dm.dmub_bo, 1893 &adev->dm.dmub_bo_gpu_addr, 1894 &adev->dm.dmub_bo_cpu_addr); 1895 1896 if (adev->dm.hpd_rx_offload_wq) { 1897 for (i = 0; i < adev->dm.dc->caps.max_links; i++) { 1898 if (adev->dm.hpd_rx_offload_wq[i].wq) { 1899 destroy_workqueue(adev->dm.hpd_rx_offload_wq[i].wq); 1900 adev->dm.hpd_rx_offload_wq[i].wq = NULL; 1901 } 1902 } 1903 1904 kfree(adev->dm.hpd_rx_offload_wq); 1905 adev->dm.hpd_rx_offload_wq = NULL; 1906 } 1907 1908 /* DC Destroy TODO: Replace destroy DAL */ 1909 if (adev->dm.dc) 1910 dc_destroy(&adev->dm.dc); 1911 /* 1912 * TODO: pageflip, vlank interrupt 1913 * 1914 * amdgpu_dm_irq_fini(adev); 1915 */ 1916 1917 if (adev->dm.cgs_device) { 1918 amdgpu_cgs_destroy_device(adev->dm.cgs_device); 1919 adev->dm.cgs_device = NULL; 1920 } 1921 if (adev->dm.freesync_module) { 1922 mod_freesync_destroy(adev->dm.freesync_module); 1923 adev->dm.freesync_module = NULL; 1924 } 1925 1926 mutex_destroy(&adev->dm.audio_lock); 1927 mutex_destroy(&adev->dm.dc_lock); 1928 mutex_destroy(&adev->dm.dpia_aux_lock); 1929 } 1930 1931 static int load_dmcu_fw(struct amdgpu_device *adev) 1932 { 1933 const char *fw_name_dmcu = NULL; 1934 int r; 1935 const struct dmcu_firmware_header_v1_0 *hdr; 1936 1937 switch (adev->asic_type) { 1938 #if defined(CONFIG_DRM_AMD_DC_SI) 1939 case CHIP_TAHITI: 1940 case CHIP_PITCAIRN: 1941 case CHIP_VERDE: 1942 case CHIP_OLAND: 1943 #endif 1944 case CHIP_BONAIRE: 1945 case CHIP_HAWAII: 1946 case CHIP_KAVERI: 1947 case CHIP_KABINI: 1948 case CHIP_MULLINS: 1949 case CHIP_TONGA: 1950 case CHIP_FIJI: 1951 case CHIP_CARRIZO: 1952 case CHIP_STONEY: 1953 case CHIP_POLARIS11: 1954 case CHIP_POLARIS10: 1955 case CHIP_POLARIS12: 1956 case CHIP_VEGAM: 1957 case CHIP_VEGA10: 1958 case CHIP_VEGA12: 1959 case CHIP_VEGA20: 1960 return 0; 1961 case CHIP_NAVI12: 1962 fw_name_dmcu = FIRMWARE_NAVI12_DMCU; 1963 break; 1964 case CHIP_RAVEN: 1965 if (ASICREV_IS_PICASSO(adev->external_rev_id)) 1966 fw_name_dmcu = FIRMWARE_RAVEN_DMCU; 1967 else if (ASICREV_IS_RAVEN2(adev->external_rev_id)) 1968 fw_name_dmcu = FIRMWARE_RAVEN_DMCU; 1969 else 1970 return 0; 1971 break; 1972 default: 1973 switch (adev->ip_versions[DCE_HWIP][0]) { 1974 case IP_VERSION(2, 0, 2): 1975 case IP_VERSION(2, 0, 3): 1976 case IP_VERSION(2, 0, 0): 1977 case IP_VERSION(2, 1, 0): 1978 case IP_VERSION(3, 0, 0): 1979 case IP_VERSION(3, 0, 2): 1980 case IP_VERSION(3, 0, 3): 1981 case IP_VERSION(3, 0, 1): 1982 case IP_VERSION(3, 1, 2): 1983 case IP_VERSION(3, 1, 3): 1984 case IP_VERSION(3, 1, 4): 1985 case IP_VERSION(3, 1, 5): 1986 case IP_VERSION(3, 1, 6): 1987 case IP_VERSION(3, 2, 0): 1988 case IP_VERSION(3, 2, 1): 1989 return 0; 1990 default: 1991 break; 1992 } 1993 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type); 1994 return -EINVAL; 1995 } 1996 1997 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) { 1998 DRM_DEBUG_KMS("dm: DMCU firmware not supported on direct or SMU loading\n"); 1999 return 0; 2000 } 2001 2002 r = request_firmware_direct(&adev->dm.fw_dmcu, fw_name_dmcu, adev->dev); 2003 if (r == -ENOENT) { 2004 /* DMCU firmware is not necessary, so don't raise a fuss if it's missing */ 2005 DRM_DEBUG_KMS("dm: DMCU firmware not found\n"); 2006 adev->dm.fw_dmcu = NULL; 2007 return 0; 2008 } 2009 if (r) { 2010 dev_err(adev->dev, "amdgpu_dm: Can't load firmware \"%s\"\n", 2011 fw_name_dmcu); 2012 return r; 2013 } 2014 2015 r = amdgpu_ucode_validate(adev->dm.fw_dmcu); 2016 if (r) { 2017 dev_err(adev->dev, "amdgpu_dm: Can't validate firmware \"%s\"\n", 2018 fw_name_dmcu); 2019 release_firmware(adev->dm.fw_dmcu); 2020 adev->dm.fw_dmcu = NULL; 2021 return r; 2022 } 2023 2024 hdr = (const struct dmcu_firmware_header_v1_0 *)adev->dm.fw_dmcu->data; 2025 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].ucode_id = AMDGPU_UCODE_ID_DMCU_ERAM; 2026 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].fw = adev->dm.fw_dmcu; 2027 adev->firmware.fw_size += 2028 roundup2(le32_to_cpu(hdr->header.ucode_size_bytes) - le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE); 2029 2030 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].ucode_id = AMDGPU_UCODE_ID_DMCU_INTV; 2031 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].fw = adev->dm.fw_dmcu; 2032 adev->firmware.fw_size += 2033 roundup2(le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE); 2034 2035 adev->dm.dmcu_fw_version = le32_to_cpu(hdr->header.ucode_version); 2036 2037 DRM_DEBUG_KMS("PSP loading DMCU firmware\n"); 2038 2039 return 0; 2040 } 2041 2042 static uint32_t amdgpu_dm_dmub_reg_read(void *ctx, uint32_t address) 2043 { 2044 struct amdgpu_device *adev = ctx; 2045 2046 return dm_read_reg(adev->dm.dc->ctx, address); 2047 } 2048 2049 static void amdgpu_dm_dmub_reg_write(void *ctx, uint32_t address, 2050 uint32_t value) 2051 { 2052 struct amdgpu_device *adev = ctx; 2053 2054 return dm_write_reg(adev->dm.dc->ctx, address, value); 2055 } 2056 2057 static int dm_dmub_sw_init(struct amdgpu_device *adev) 2058 { 2059 struct dmub_srv_create_params create_params; 2060 struct dmub_srv_region_params region_params; 2061 struct dmub_srv_region_info region_info; 2062 struct dmub_srv_fb_params fb_params; 2063 struct dmub_srv_fb_info *fb_info; 2064 struct dmub_srv *dmub_srv; 2065 const struct dmcub_firmware_header_v1_0 *hdr; 2066 const char *fw_name_dmub; 2067 enum dmub_asic dmub_asic; 2068 enum dmub_status status; 2069 int r; 2070 2071 switch (adev->ip_versions[DCE_HWIP][0]) { 2072 case IP_VERSION(2, 1, 0): 2073 dmub_asic = DMUB_ASIC_DCN21; 2074 fw_name_dmub = FIRMWARE_RENOIR_DMUB; 2075 if (ASICREV_IS_GREEN_SARDINE(adev->external_rev_id)) 2076 fw_name_dmub = FIRMWARE_GREEN_SARDINE_DMUB; 2077 break; 2078 case IP_VERSION(3, 0, 0): 2079 if (adev->ip_versions[GC_HWIP][0] == IP_VERSION(10, 3, 0)) { 2080 dmub_asic = DMUB_ASIC_DCN30; 2081 fw_name_dmub = FIRMWARE_SIENNA_CICHLID_DMUB; 2082 } else { 2083 dmub_asic = DMUB_ASIC_DCN30; 2084 fw_name_dmub = FIRMWARE_NAVY_FLOUNDER_DMUB; 2085 } 2086 break; 2087 case IP_VERSION(3, 0, 1): 2088 dmub_asic = DMUB_ASIC_DCN301; 2089 fw_name_dmub = FIRMWARE_VANGOGH_DMUB; 2090 break; 2091 case IP_VERSION(3, 0, 2): 2092 dmub_asic = DMUB_ASIC_DCN302; 2093 fw_name_dmub = FIRMWARE_DIMGREY_CAVEFISH_DMUB; 2094 break; 2095 case IP_VERSION(3, 0, 3): 2096 dmub_asic = DMUB_ASIC_DCN303; 2097 fw_name_dmub = FIRMWARE_BEIGE_GOBY_DMUB; 2098 break; 2099 case IP_VERSION(3, 1, 2): 2100 case IP_VERSION(3, 1, 3): 2101 dmub_asic = (adev->external_rev_id == YELLOW_CARP_B0) ? DMUB_ASIC_DCN31B : DMUB_ASIC_DCN31; 2102 fw_name_dmub = FIRMWARE_YELLOW_CARP_DMUB; 2103 break; 2104 case IP_VERSION(3, 1, 4): 2105 dmub_asic = DMUB_ASIC_DCN314; 2106 fw_name_dmub = FIRMWARE_DCN_314_DMUB; 2107 break; 2108 case IP_VERSION(3, 1, 5): 2109 dmub_asic = DMUB_ASIC_DCN315; 2110 fw_name_dmub = FIRMWARE_DCN_315_DMUB; 2111 break; 2112 case IP_VERSION(3, 1, 6): 2113 dmub_asic = DMUB_ASIC_DCN316; 2114 fw_name_dmub = FIRMWARE_DCN316_DMUB; 2115 break; 2116 case IP_VERSION(3, 2, 0): 2117 dmub_asic = DMUB_ASIC_DCN32; 2118 fw_name_dmub = FIRMWARE_DCN_V3_2_0_DMCUB; 2119 break; 2120 case IP_VERSION(3, 2, 1): 2121 dmub_asic = DMUB_ASIC_DCN321; 2122 fw_name_dmub = FIRMWARE_DCN_V3_2_1_DMCUB; 2123 break; 2124 default: 2125 /* ASIC doesn't support DMUB. */ 2126 return 0; 2127 } 2128 2129 r = request_firmware_direct(&adev->dm.dmub_fw, fw_name_dmub, adev->dev); 2130 if (r) { 2131 DRM_ERROR("DMUB firmware loading failed: %d\n", r); 2132 return 0; 2133 } 2134 2135 r = amdgpu_ucode_validate(adev->dm.dmub_fw); 2136 if (r) { 2137 DRM_ERROR("Couldn't validate DMUB firmware: %d\n", r); 2138 return 0; 2139 } 2140 2141 hdr = (const struct dmcub_firmware_header_v1_0 *)adev->dm.dmub_fw->data; 2142 adev->dm.dmcub_fw_version = le32_to_cpu(hdr->header.ucode_version); 2143 2144 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) { 2145 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCUB].ucode_id = 2146 AMDGPU_UCODE_ID_DMCUB; 2147 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCUB].fw = 2148 adev->dm.dmub_fw; 2149 adev->firmware.fw_size += 2150 roundup2(le32_to_cpu(hdr->inst_const_bytes), PAGE_SIZE); 2151 2152 DRM_INFO("Loading DMUB firmware via PSP: version=0x%08X\n", 2153 adev->dm.dmcub_fw_version); 2154 } 2155 2156 2157 adev->dm.dmub_srv = kzalloc(sizeof(*adev->dm.dmub_srv), GFP_KERNEL); 2158 dmub_srv = adev->dm.dmub_srv; 2159 2160 if (!dmub_srv) { 2161 DRM_ERROR("Failed to allocate DMUB service!\n"); 2162 return -ENOMEM; 2163 } 2164 2165 memset(&create_params, 0, sizeof(create_params)); 2166 create_params.user_ctx = adev; 2167 create_params.funcs.reg_read = amdgpu_dm_dmub_reg_read; 2168 create_params.funcs.reg_write = amdgpu_dm_dmub_reg_write; 2169 create_params.asic = dmub_asic; 2170 2171 /* Create the DMUB service. */ 2172 status = dmub_srv_create(dmub_srv, &create_params); 2173 if (status != DMUB_STATUS_OK) { 2174 DRM_ERROR("Error creating DMUB service: %d\n", status); 2175 return -EINVAL; 2176 } 2177 2178 /* Calculate the size of all the regions for the DMUB service. */ 2179 memset(®ion_params, 0, sizeof(region_params)); 2180 2181 region_params.inst_const_size = le32_to_cpu(hdr->inst_const_bytes) - 2182 PSP_HEADER_BYTES - PSP_FOOTER_BYTES; 2183 region_params.bss_data_size = le32_to_cpu(hdr->bss_data_bytes); 2184 region_params.vbios_size = adev->bios_size; 2185 region_params.fw_bss_data = region_params.bss_data_size ? 2186 adev->dm.dmub_fw->data + 2187 le32_to_cpu(hdr->header.ucode_array_offset_bytes) + 2188 le32_to_cpu(hdr->inst_const_bytes) : NULL; 2189 region_params.fw_inst_const = 2190 adev->dm.dmub_fw->data + 2191 le32_to_cpu(hdr->header.ucode_array_offset_bytes) + 2192 PSP_HEADER_BYTES; 2193 2194 status = dmub_srv_calc_region_info(dmub_srv, ®ion_params, 2195 ®ion_info); 2196 2197 if (status != DMUB_STATUS_OK) { 2198 DRM_ERROR("Error calculating DMUB region info: %d\n", status); 2199 return -EINVAL; 2200 } 2201 2202 /* 2203 * Allocate a framebuffer based on the total size of all the regions. 2204 * TODO: Move this into GART. 2205 */ 2206 r = amdgpu_bo_create_kernel(adev, region_info.fb_size, PAGE_SIZE, 2207 AMDGPU_GEM_DOMAIN_VRAM, &adev->dm.dmub_bo, 2208 &adev->dm.dmub_bo_gpu_addr, 2209 &adev->dm.dmub_bo_cpu_addr); 2210 if (r) 2211 return r; 2212 2213 /* Rebase the regions on the framebuffer address. */ 2214 memset(&fb_params, 0, sizeof(fb_params)); 2215 fb_params.cpu_addr = adev->dm.dmub_bo_cpu_addr; 2216 fb_params.gpu_addr = adev->dm.dmub_bo_gpu_addr; 2217 fb_params.region_info = ®ion_info; 2218 2219 adev->dm.dmub_fb_info = 2220 kzalloc(sizeof(*adev->dm.dmub_fb_info), GFP_KERNEL); 2221 fb_info = adev->dm.dmub_fb_info; 2222 2223 if (!fb_info) { 2224 DRM_ERROR( 2225 "Failed to allocate framebuffer info for DMUB service!\n"); 2226 return -ENOMEM; 2227 } 2228 2229 status = dmub_srv_calc_fb_info(dmub_srv, &fb_params, fb_info); 2230 if (status != DMUB_STATUS_OK) { 2231 DRM_ERROR("Error calculating DMUB FB info: %d\n", status); 2232 return -EINVAL; 2233 } 2234 2235 return 0; 2236 } 2237 2238 static int dm_sw_init(void *handle) 2239 { 2240 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 2241 int r; 2242 2243 r = dm_dmub_sw_init(adev); 2244 if (r) 2245 return r; 2246 2247 return load_dmcu_fw(adev); 2248 } 2249 2250 static int dm_sw_fini(void *handle) 2251 { 2252 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 2253 2254 kfree(adev->dm.dmub_fb_info); 2255 adev->dm.dmub_fb_info = NULL; 2256 2257 if (adev->dm.dmub_srv) { 2258 dmub_srv_destroy(adev->dm.dmub_srv); 2259 adev->dm.dmub_srv = NULL; 2260 } 2261 2262 release_firmware(adev->dm.dmub_fw); 2263 adev->dm.dmub_fw = NULL; 2264 2265 release_firmware(adev->dm.fw_dmcu); 2266 adev->dm.fw_dmcu = NULL; 2267 2268 return 0; 2269 } 2270 2271 static int detect_mst_link_for_all_connectors(struct drm_device *dev) 2272 { 2273 struct amdgpu_dm_connector *aconnector; 2274 struct drm_connector *connector; 2275 struct drm_connector_list_iter iter; 2276 int ret = 0; 2277 2278 drm_connector_list_iter_begin(dev, &iter); 2279 drm_for_each_connector_iter(connector, &iter) { 2280 aconnector = to_amdgpu_dm_connector(connector); 2281 if (aconnector->dc_link->type == dc_connection_mst_branch && 2282 aconnector->mst_mgr.aux) { 2283 DRM_DEBUG_DRIVER("DM_MST: starting TM on aconnector: %p [id: %d]\n", 2284 aconnector, 2285 aconnector->base.base.id); 2286 2287 ret = drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true); 2288 if (ret < 0) { 2289 DRM_ERROR("DM_MST: Failed to start MST\n"); 2290 aconnector->dc_link->type = 2291 dc_connection_single; 2292 ret = dm_helpers_dp_mst_stop_top_mgr(aconnector->dc_link->ctx, 2293 aconnector->dc_link); 2294 break; 2295 } 2296 } 2297 } 2298 drm_connector_list_iter_end(&iter); 2299 2300 return ret; 2301 } 2302 2303 static int dm_late_init(void *handle) 2304 { 2305 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 2306 2307 struct dmcu_iram_parameters params; 2308 unsigned int linear_lut[16]; 2309 int i; 2310 struct dmcu *dmcu = NULL; 2311 2312 dmcu = adev->dm.dc->res_pool->dmcu; 2313 2314 for (i = 0; i < 16; i++) 2315 linear_lut[i] = 0xFFFF * i / 15; 2316 2317 params.set = 0; 2318 params.backlight_ramping_override = false; 2319 params.backlight_ramping_start = 0xCCCC; 2320 params.backlight_ramping_reduction = 0xCCCCCCCC; 2321 params.backlight_lut_array_size = 16; 2322 params.backlight_lut_array = linear_lut; 2323 2324 /* Min backlight level after ABM reduction, Don't allow below 1% 2325 * 0xFFFF x 0.01 = 0x28F 2326 */ 2327 params.min_abm_backlight = 0x28F; 2328 /* In the case where abm is implemented on dmcub, 2329 * dmcu object will be null. 2330 * ABM 2.4 and up are implemented on dmcub. 2331 */ 2332 if (dmcu) { 2333 if (!dmcu_load_iram(dmcu, params)) 2334 return -EINVAL; 2335 } else if (adev->dm.dc->ctx->dmub_srv) { 2336 struct dc_link *edp_links[MAX_NUM_EDP]; 2337 int edp_num; 2338 2339 get_edp_links(adev->dm.dc, edp_links, &edp_num); 2340 for (i = 0; i < edp_num; i++) { 2341 if (!dmub_init_abm_config(adev->dm.dc->res_pool, params, i)) 2342 return -EINVAL; 2343 } 2344 } 2345 2346 return detect_mst_link_for_all_connectors(adev_to_drm(adev)); 2347 } 2348 2349 static void s3_handle_mst(struct drm_device *dev, bool suspend) 2350 { 2351 struct amdgpu_dm_connector *aconnector; 2352 struct drm_connector *connector; 2353 struct drm_connector_list_iter iter; 2354 struct drm_dp_mst_topology_mgr *mgr; 2355 int ret; 2356 bool need_hotplug = false; 2357 2358 drm_connector_list_iter_begin(dev, &iter); 2359 drm_for_each_connector_iter(connector, &iter) { 2360 aconnector = to_amdgpu_dm_connector(connector); 2361 if (aconnector->dc_link->type != dc_connection_mst_branch || 2362 aconnector->mst_port) 2363 continue; 2364 2365 mgr = &aconnector->mst_mgr; 2366 2367 if (suspend) { 2368 drm_dp_mst_topology_mgr_suspend(mgr); 2369 } else { 2370 /* if extended timeout is supported in hardware, 2371 * default to LTTPR timeout (3.2ms) first as a W/A for DP link layer 2372 * CTS 4.2.1.1 regression introduced by CTS specs requirement update. 2373 */ 2374 dc_link_aux_try_to_configure_timeout(aconnector->dc_link->ddc, LINK_AUX_DEFAULT_LTTPR_TIMEOUT_PERIOD); 2375 if (!dp_is_lttpr_present(aconnector->dc_link)) 2376 dc_link_aux_try_to_configure_timeout(aconnector->dc_link->ddc, LINK_AUX_DEFAULT_TIMEOUT_PERIOD); 2377 2378 ret = drm_dp_mst_topology_mgr_resume(mgr, true); 2379 if (ret < 0) { 2380 dm_helpers_dp_mst_stop_top_mgr(aconnector->dc_link->ctx, 2381 aconnector->dc_link); 2382 need_hotplug = true; 2383 } 2384 } 2385 } 2386 drm_connector_list_iter_end(&iter); 2387 2388 if (need_hotplug) 2389 drm_kms_helper_hotplug_event(dev); 2390 } 2391 2392 static int amdgpu_dm_smu_write_watermarks_table(struct amdgpu_device *adev) 2393 { 2394 int ret = 0; 2395 2396 /* This interface is for dGPU Navi1x.Linux dc-pplib interface depends 2397 * on window driver dc implementation. 2398 * For Navi1x, clock settings of dcn watermarks are fixed. the settings 2399 * should be passed to smu during boot up and resume from s3. 2400 * boot up: dc calculate dcn watermark clock settings within dc_create, 2401 * dcn20_resource_construct 2402 * then call pplib functions below to pass the settings to smu: 2403 * smu_set_watermarks_for_clock_ranges 2404 * smu_set_watermarks_table 2405 * navi10_set_watermarks_table 2406 * smu_write_watermarks_table 2407 * 2408 * For Renoir, clock settings of dcn watermark are also fixed values. 2409 * dc has implemented different flow for window driver: 2410 * dc_hardware_init / dc_set_power_state 2411 * dcn10_init_hw 2412 * notify_wm_ranges 2413 * set_wm_ranges 2414 * -- Linux 2415 * smu_set_watermarks_for_clock_ranges 2416 * renoir_set_watermarks_table 2417 * smu_write_watermarks_table 2418 * 2419 * For Linux, 2420 * dc_hardware_init -> amdgpu_dm_init 2421 * dc_set_power_state --> dm_resume 2422 * 2423 * therefore, this function apply to navi10/12/14 but not Renoir 2424 * * 2425 */ 2426 switch (adev->ip_versions[DCE_HWIP][0]) { 2427 case IP_VERSION(2, 0, 2): 2428 case IP_VERSION(2, 0, 0): 2429 break; 2430 default: 2431 return 0; 2432 } 2433 2434 ret = amdgpu_dpm_write_watermarks_table(adev); 2435 if (ret) { 2436 DRM_ERROR("Failed to update WMTABLE!\n"); 2437 return ret; 2438 } 2439 2440 return 0; 2441 } 2442 2443 /** 2444 * dm_hw_init() - Initialize DC device 2445 * @handle: The base driver device containing the amdgpu_dm device. 2446 * 2447 * Initialize the &struct amdgpu_display_manager device. This involves calling 2448 * the initializers of each DM component, then populating the struct with them. 2449 * 2450 * Although the function implies hardware initialization, both hardware and 2451 * software are initialized here. Splitting them out to their relevant init 2452 * hooks is a future TODO item. 2453 * 2454 * Some notable things that are initialized here: 2455 * 2456 * - Display Core, both software and hardware 2457 * - DC modules that we need (freesync and color management) 2458 * - DRM software states 2459 * - Interrupt sources and handlers 2460 * - Vblank support 2461 * - Debug FS entries, if enabled 2462 */ 2463 static int dm_hw_init(void *handle) 2464 { 2465 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 2466 /* Create DAL display manager */ 2467 amdgpu_dm_init(adev); 2468 amdgpu_dm_hpd_init(adev); 2469 2470 return 0; 2471 } 2472 2473 /** 2474 * dm_hw_fini() - Teardown DC device 2475 * @handle: The base driver device containing the amdgpu_dm device. 2476 * 2477 * Teardown components within &struct amdgpu_display_manager that require 2478 * cleanup. This involves cleaning up the DRM device, DC, and any modules that 2479 * were loaded. Also flush IRQ workqueues and disable them. 2480 */ 2481 static int dm_hw_fini(void *handle) 2482 { 2483 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 2484 2485 amdgpu_dm_hpd_fini(adev); 2486 2487 amdgpu_dm_irq_fini(adev); 2488 amdgpu_dm_fini(adev); 2489 return 0; 2490 } 2491 2492 2493 static void dm_gpureset_toggle_interrupts(struct amdgpu_device *adev, 2494 struct dc_state *state, bool enable) 2495 { 2496 enum dc_irq_source irq_source; 2497 struct amdgpu_crtc *acrtc; 2498 int rc = -EBUSY; 2499 int i = 0; 2500 2501 for (i = 0; i < state->stream_count; i++) { 2502 acrtc = get_crtc_by_otg_inst( 2503 adev, state->stream_status[i].primary_otg_inst); 2504 2505 if (acrtc && state->stream_status[i].plane_count != 0) { 2506 irq_source = IRQ_TYPE_PFLIP + acrtc->otg_inst; 2507 rc = dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY; 2508 DRM_DEBUG_VBL("crtc %d - vupdate irq %sabling: r=%d\n", 2509 acrtc->crtc_id, enable ? "en" : "dis", rc); 2510 if (rc) 2511 DRM_WARN("Failed to %s pflip interrupts\n", 2512 enable ? "enable" : "disable"); 2513 2514 if (enable) { 2515 rc = dm_enable_vblank(&acrtc->base); 2516 if (rc) 2517 DRM_WARN("Failed to enable vblank interrupts\n"); 2518 } else { 2519 dm_disable_vblank(&acrtc->base); 2520 } 2521 2522 } 2523 } 2524 2525 } 2526 2527 static enum dc_status amdgpu_dm_commit_zero_streams(struct dc *dc) 2528 { 2529 struct dc_state *context = NULL; 2530 enum dc_status res = DC_ERROR_UNEXPECTED; 2531 int i; 2532 struct dc_stream_state *del_streams[MAX_PIPES]; 2533 int del_streams_count = 0; 2534 2535 memset(del_streams, 0, sizeof(del_streams)); 2536 2537 context = dc_create_state(dc); 2538 if (context == NULL) 2539 goto context_alloc_fail; 2540 2541 dc_resource_state_copy_construct_current(dc, context); 2542 2543 /* First remove from context all streams */ 2544 for (i = 0; i < context->stream_count; i++) { 2545 struct dc_stream_state *stream = context->streams[i]; 2546 2547 del_streams[del_streams_count++] = stream; 2548 } 2549 2550 /* Remove all planes for removed streams and then remove the streams */ 2551 for (i = 0; i < del_streams_count; i++) { 2552 if (!dc_rem_all_planes_for_stream(dc, del_streams[i], context)) { 2553 res = DC_FAIL_DETACH_SURFACES; 2554 goto fail; 2555 } 2556 2557 res = dc_remove_stream_from_ctx(dc, context, del_streams[i]); 2558 if (res != DC_OK) 2559 goto fail; 2560 } 2561 2562 res = dc_commit_state(dc, context); 2563 2564 fail: 2565 dc_release_state(context); 2566 2567 context_alloc_fail: 2568 return res; 2569 } 2570 2571 static void hpd_rx_irq_work_suspend(struct amdgpu_display_manager *dm) 2572 { 2573 int i; 2574 2575 if (dm->hpd_rx_offload_wq) { 2576 for (i = 0; i < dm->dc->caps.max_links; i++) 2577 flush_workqueue(dm->hpd_rx_offload_wq[i].wq); 2578 } 2579 } 2580 2581 static int dm_suspend(void *handle) 2582 { 2583 struct amdgpu_device *adev = handle; 2584 struct amdgpu_display_manager *dm = &adev->dm; 2585 int ret = 0; 2586 2587 if (amdgpu_in_reset(adev)) { 2588 mutex_lock(&dm->dc_lock); 2589 2590 dc_allow_idle_optimizations(adev->dm.dc, false); 2591 2592 dm->cached_dc_state = dc_copy_state(dm->dc->current_state); 2593 2594 dm_gpureset_toggle_interrupts(adev, dm->cached_dc_state, false); 2595 2596 amdgpu_dm_commit_zero_streams(dm->dc); 2597 2598 amdgpu_dm_irq_suspend(adev); 2599 2600 hpd_rx_irq_work_suspend(dm); 2601 2602 return ret; 2603 } 2604 2605 WARN_ON(adev->dm.cached_state); 2606 adev->dm.cached_state = drm_atomic_helper_suspend(adev_to_drm(adev)); 2607 2608 s3_handle_mst(adev_to_drm(adev), true); 2609 2610 amdgpu_dm_irq_suspend(adev); 2611 2612 hpd_rx_irq_work_suspend(dm); 2613 2614 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D3); 2615 2616 return 0; 2617 } 2618 2619 struct amdgpu_dm_connector * 2620 amdgpu_dm_find_first_crtc_matching_connector(struct drm_atomic_state *state, 2621 struct drm_crtc *crtc) 2622 { 2623 u32 i; 2624 struct drm_connector_state *new_con_state; 2625 struct drm_connector *connector; 2626 struct drm_crtc *crtc_from_state; 2627 2628 for_each_new_connector_in_state(state, connector, new_con_state, i) { 2629 crtc_from_state = new_con_state->crtc; 2630 2631 if (crtc_from_state == crtc) 2632 return to_amdgpu_dm_connector(connector); 2633 } 2634 2635 return NULL; 2636 } 2637 2638 static void emulated_link_detect(struct dc_link *link) 2639 { 2640 struct dc_sink_init_data sink_init_data = { 0 }; 2641 struct display_sink_capability sink_caps = { 0 }; 2642 enum dc_edid_status edid_status; 2643 struct dc_context *dc_ctx = link->ctx; 2644 struct dc_sink *sink = NULL; 2645 struct dc_sink *prev_sink = NULL; 2646 2647 link->type = dc_connection_none; 2648 prev_sink = link->local_sink; 2649 2650 if (prev_sink) 2651 dc_sink_release(prev_sink); 2652 2653 switch (link->connector_signal) { 2654 case SIGNAL_TYPE_HDMI_TYPE_A: { 2655 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C; 2656 sink_caps.signal = SIGNAL_TYPE_HDMI_TYPE_A; 2657 break; 2658 } 2659 2660 case SIGNAL_TYPE_DVI_SINGLE_LINK: { 2661 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C; 2662 sink_caps.signal = SIGNAL_TYPE_DVI_SINGLE_LINK; 2663 break; 2664 } 2665 2666 case SIGNAL_TYPE_DVI_DUAL_LINK: { 2667 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C; 2668 sink_caps.signal = SIGNAL_TYPE_DVI_DUAL_LINK; 2669 break; 2670 } 2671 2672 case SIGNAL_TYPE_LVDS: { 2673 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C; 2674 sink_caps.signal = SIGNAL_TYPE_LVDS; 2675 break; 2676 } 2677 2678 case SIGNAL_TYPE_EDP: { 2679 sink_caps.transaction_type = 2680 DDC_TRANSACTION_TYPE_I2C_OVER_AUX; 2681 sink_caps.signal = SIGNAL_TYPE_EDP; 2682 break; 2683 } 2684 2685 case SIGNAL_TYPE_DISPLAY_PORT: { 2686 sink_caps.transaction_type = 2687 DDC_TRANSACTION_TYPE_I2C_OVER_AUX; 2688 sink_caps.signal = SIGNAL_TYPE_VIRTUAL; 2689 break; 2690 } 2691 2692 default: 2693 DC_ERROR("Invalid connector type! signal:%d\n", 2694 link->connector_signal); 2695 return; 2696 } 2697 2698 sink_init_data.link = link; 2699 sink_init_data.sink_signal = sink_caps.signal; 2700 2701 sink = dc_sink_create(&sink_init_data); 2702 if (!sink) { 2703 DC_ERROR("Failed to create sink!\n"); 2704 return; 2705 } 2706 2707 /* dc_sink_create returns a new reference */ 2708 link->local_sink = sink; 2709 2710 edid_status = dm_helpers_read_local_edid( 2711 link->ctx, 2712 link, 2713 sink); 2714 2715 if (edid_status != EDID_OK) 2716 DC_ERROR("Failed to read EDID"); 2717 2718 } 2719 2720 static void dm_gpureset_commit_state(struct dc_state *dc_state, 2721 struct amdgpu_display_manager *dm) 2722 { 2723 struct { 2724 struct dc_surface_update surface_updates[MAX_SURFACES]; 2725 struct dc_plane_info plane_infos[MAX_SURFACES]; 2726 struct dc_scaling_info scaling_infos[MAX_SURFACES]; 2727 struct dc_flip_addrs flip_addrs[MAX_SURFACES]; 2728 struct dc_stream_update stream_update; 2729 } *bundle; 2730 int k, m; 2731 2732 bundle = kzalloc(sizeof(*bundle), GFP_KERNEL); 2733 2734 if (!bundle) { 2735 dm_error("Failed to allocate update bundle\n"); 2736 goto cleanup; 2737 } 2738 2739 for (k = 0; k < dc_state->stream_count; k++) { 2740 bundle->stream_update.stream = dc_state->streams[k]; 2741 2742 for (m = 0; m < dc_state->stream_status->plane_count; m++) { 2743 bundle->surface_updates[m].surface = 2744 dc_state->stream_status->plane_states[m]; 2745 bundle->surface_updates[m].surface->force_full_update = 2746 true; 2747 } 2748 2749 update_planes_and_stream_adapter(dm->dc, 2750 UPDATE_TYPE_FULL, 2751 dc_state->stream_status->plane_count, 2752 dc_state->streams[k], 2753 &bundle->stream_update, 2754 bundle->surface_updates); 2755 } 2756 2757 cleanup: 2758 kfree(bundle); 2759 } 2760 2761 static int dm_resume(void *handle) 2762 { 2763 struct amdgpu_device *adev = handle; 2764 struct drm_device *ddev = adev_to_drm(adev); 2765 struct amdgpu_display_manager *dm = &adev->dm; 2766 struct amdgpu_dm_connector *aconnector; 2767 struct drm_connector *connector; 2768 struct drm_connector_list_iter iter; 2769 struct drm_crtc *crtc; 2770 struct drm_crtc_state *new_crtc_state; 2771 struct dm_crtc_state *dm_new_crtc_state; 2772 struct drm_plane *plane; 2773 struct drm_plane_state *new_plane_state; 2774 struct dm_plane_state *dm_new_plane_state; 2775 struct dm_atomic_state *dm_state = to_dm_atomic_state(dm->atomic_obj.state); 2776 enum dc_connection_type new_connection_type = dc_connection_none; 2777 struct dc_state *dc_state; 2778 int i, r, j; 2779 2780 if (amdgpu_in_reset(adev)) { 2781 dc_state = dm->cached_dc_state; 2782 2783 /* 2784 * The dc->current_state is backed up into dm->cached_dc_state 2785 * before we commit 0 streams. 2786 * 2787 * DC will clear link encoder assignments on the real state 2788 * but the changes won't propagate over to the copy we made 2789 * before the 0 streams commit. 2790 * 2791 * DC expects that link encoder assignments are *not* valid 2792 * when committing a state, so as a workaround we can copy 2793 * off of the current state. 2794 * 2795 * We lose the previous assignments, but we had already 2796 * commit 0 streams anyway. 2797 */ 2798 link_enc_cfg_copy(adev->dm.dc->current_state, dc_state); 2799 2800 r = dm_dmub_hw_init(adev); 2801 if (r) 2802 DRM_ERROR("DMUB interface failed to initialize: status=%d\n", r); 2803 2804 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0); 2805 dc_resume(dm->dc); 2806 2807 amdgpu_dm_irq_resume_early(adev); 2808 2809 for (i = 0; i < dc_state->stream_count; i++) { 2810 dc_state->streams[i]->mode_changed = true; 2811 for (j = 0; j < dc_state->stream_status[i].plane_count; j++) { 2812 dc_state->stream_status[i].plane_states[j]->update_flags.raw 2813 = 0xffffffff; 2814 } 2815 } 2816 2817 if (dc_is_dmub_outbox_supported(adev->dm.dc)) { 2818 amdgpu_dm_outbox_init(adev); 2819 dc_enable_dmub_outbox(adev->dm.dc); 2820 } 2821 2822 WARN_ON(!dc_commit_state(dm->dc, dc_state)); 2823 2824 dm_gpureset_commit_state(dm->cached_dc_state, dm); 2825 2826 dm_gpureset_toggle_interrupts(adev, dm->cached_dc_state, true); 2827 2828 dc_release_state(dm->cached_dc_state); 2829 dm->cached_dc_state = NULL; 2830 2831 amdgpu_dm_irq_resume_late(adev); 2832 2833 mutex_unlock(&dm->dc_lock); 2834 2835 return 0; 2836 } 2837 /* Recreate dc_state - DC invalidates it when setting power state to S3. */ 2838 dc_release_state(dm_state->context); 2839 dm_state->context = dc_create_state(dm->dc); 2840 /* TODO: Remove dc_state->dccg, use dc->dccg directly. */ 2841 dc_resource_state_construct(dm->dc, dm_state->context); 2842 2843 /* Before powering on DC we need to re-initialize DMUB. */ 2844 dm_dmub_hw_resume(adev); 2845 2846 /* Re-enable outbox interrupts for DPIA. */ 2847 if (dc_is_dmub_outbox_supported(adev->dm.dc)) { 2848 amdgpu_dm_outbox_init(adev); 2849 dc_enable_dmub_outbox(adev->dm.dc); 2850 } 2851 2852 /* power on hardware */ 2853 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0); 2854 2855 /* program HPD filter */ 2856 dc_resume(dm->dc); 2857 2858 /* 2859 * early enable HPD Rx IRQ, should be done before set mode as short 2860 * pulse interrupts are used for MST 2861 */ 2862 amdgpu_dm_irq_resume_early(adev); 2863 2864 /* On resume we need to rewrite the MSTM control bits to enable MST*/ 2865 s3_handle_mst(ddev, false); 2866 2867 /* Do detection*/ 2868 drm_connector_list_iter_begin(ddev, &iter); 2869 drm_for_each_connector_iter(connector, &iter) { 2870 aconnector = to_amdgpu_dm_connector(connector); 2871 2872 if (!aconnector->dc_link) 2873 continue; 2874 2875 /* 2876 * this is the case when traversing through already created 2877 * MST connectors, should be skipped 2878 */ 2879 if (aconnector && aconnector->mst_port) 2880 continue; 2881 2882 mutex_lock(&aconnector->hpd_lock); 2883 if (!dc_link_detect_sink(aconnector->dc_link, &new_connection_type)) 2884 DRM_ERROR("KMS: Failed to detect connector\n"); 2885 2886 if (aconnector->base.force && new_connection_type == dc_connection_none) { 2887 emulated_link_detect(aconnector->dc_link); 2888 } else { 2889 mutex_lock(&dm->dc_lock); 2890 dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD); 2891 mutex_unlock(&dm->dc_lock); 2892 } 2893 2894 if (aconnector->fake_enable && aconnector->dc_link->local_sink) 2895 aconnector->fake_enable = false; 2896 2897 if (aconnector->dc_sink) 2898 dc_sink_release(aconnector->dc_sink); 2899 aconnector->dc_sink = NULL; 2900 amdgpu_dm_update_connector_after_detect(aconnector); 2901 mutex_unlock(&aconnector->hpd_lock); 2902 } 2903 drm_connector_list_iter_end(&iter); 2904 2905 /* Force mode set in atomic commit */ 2906 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) 2907 new_crtc_state->active_changed = true; 2908 2909 /* 2910 * atomic_check is expected to create the dc states. We need to release 2911 * them here, since they were duplicated as part of the suspend 2912 * procedure. 2913 */ 2914 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) { 2915 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 2916 if (dm_new_crtc_state->stream) { 2917 WARN_ON(kref_read(&dm_new_crtc_state->stream->refcount) > 1); 2918 dc_stream_release(dm_new_crtc_state->stream); 2919 dm_new_crtc_state->stream = NULL; 2920 } 2921 } 2922 2923 for_each_new_plane_in_state(dm->cached_state, plane, new_plane_state, i) { 2924 dm_new_plane_state = to_dm_plane_state(new_plane_state); 2925 if (dm_new_plane_state->dc_state) { 2926 WARN_ON(kref_read(&dm_new_plane_state->dc_state->refcount) > 1); 2927 dc_plane_state_release(dm_new_plane_state->dc_state); 2928 dm_new_plane_state->dc_state = NULL; 2929 } 2930 } 2931 2932 drm_atomic_helper_resume(ddev, dm->cached_state); 2933 2934 dm->cached_state = NULL; 2935 2936 amdgpu_dm_irq_resume_late(adev); 2937 2938 amdgpu_dm_smu_write_watermarks_table(adev); 2939 2940 return 0; 2941 } 2942 2943 /** 2944 * DOC: DM Lifecycle 2945 * 2946 * DM (and consequently DC) is registered in the amdgpu base driver as a IP 2947 * block. When CONFIG_DRM_AMD_DC is enabled, the DM device IP block is added to 2948 * the base driver's device list to be initialized and torn down accordingly. 2949 * 2950 * The functions to do so are provided as hooks in &struct amd_ip_funcs. 2951 */ 2952 2953 static const struct amd_ip_funcs amdgpu_dm_funcs = { 2954 .name = "dm", 2955 .early_init = dm_early_init, 2956 .late_init = dm_late_init, 2957 .sw_init = dm_sw_init, 2958 .sw_fini = dm_sw_fini, 2959 .early_fini = amdgpu_dm_early_fini, 2960 .hw_init = dm_hw_init, 2961 .hw_fini = dm_hw_fini, 2962 .suspend = dm_suspend, 2963 .resume = dm_resume, 2964 .is_idle = dm_is_idle, 2965 .wait_for_idle = dm_wait_for_idle, 2966 .check_soft_reset = dm_check_soft_reset, 2967 .soft_reset = dm_soft_reset, 2968 .set_clockgating_state = dm_set_clockgating_state, 2969 .set_powergating_state = dm_set_powergating_state, 2970 }; 2971 2972 const struct amdgpu_ip_block_version dm_ip_block = { 2973 .type = AMD_IP_BLOCK_TYPE_DCE, 2974 .major = 1, 2975 .minor = 0, 2976 .rev = 0, 2977 .funcs = &amdgpu_dm_funcs, 2978 }; 2979 2980 2981 /** 2982 * DOC: atomic 2983 * 2984 * *WIP* 2985 */ 2986 2987 static const struct drm_mode_config_funcs amdgpu_dm_mode_funcs = { 2988 .fb_create = amdgpu_display_user_framebuffer_create, 2989 .get_format_info = amd_get_format_info, 2990 .atomic_check = amdgpu_dm_atomic_check, 2991 .atomic_commit = drm_atomic_helper_commit, 2992 }; 2993 2994 static struct drm_mode_config_helper_funcs amdgpu_dm_mode_config_helperfuncs = { 2995 .atomic_commit_tail = amdgpu_dm_atomic_commit_tail, 2996 .atomic_commit_setup = drm_dp_mst_atomic_setup_commit, 2997 }; 2998 2999 static void update_connector_ext_caps(struct amdgpu_dm_connector *aconnector) 3000 { 3001 struct amdgpu_dm_backlight_caps *caps; 3002 struct amdgpu_display_manager *dm; 3003 struct drm_connector *conn_base; 3004 struct amdgpu_device *adev; 3005 struct dc_link *link = NULL; 3006 struct drm_luminance_range_info *luminance_range; 3007 int i; 3008 3009 if (!aconnector || !aconnector->dc_link) 3010 return; 3011 3012 link = aconnector->dc_link; 3013 if (link->connector_signal != SIGNAL_TYPE_EDP) 3014 return; 3015 3016 conn_base = &aconnector->base; 3017 adev = drm_to_adev(conn_base->dev); 3018 dm = &adev->dm; 3019 for (i = 0; i < dm->num_of_edps; i++) { 3020 if (link == dm->backlight_link[i]) 3021 break; 3022 } 3023 if (i >= dm->num_of_edps) 3024 return; 3025 caps = &dm->backlight_caps[i]; 3026 caps->ext_caps = &aconnector->dc_link->dpcd_sink_ext_caps; 3027 caps->aux_support = false; 3028 3029 if (caps->ext_caps->bits.oled == 1 3030 /* 3031 * || 3032 * caps->ext_caps->bits.sdr_aux_backlight_control == 1 || 3033 * caps->ext_caps->bits.hdr_aux_backlight_control == 1 3034 */) 3035 caps->aux_support = true; 3036 3037 if (amdgpu_backlight == 0) 3038 caps->aux_support = false; 3039 else if (amdgpu_backlight == 1) 3040 caps->aux_support = true; 3041 3042 luminance_range = &conn_base->display_info.luminance_range; 3043 caps->aux_min_input_signal = luminance_range->min_luminance; 3044 caps->aux_max_input_signal = luminance_range->max_luminance; 3045 } 3046 3047 void amdgpu_dm_update_connector_after_detect( 3048 struct amdgpu_dm_connector *aconnector) 3049 { 3050 struct drm_connector *connector = &aconnector->base; 3051 struct drm_device *dev = connector->dev; 3052 struct dc_sink *sink; 3053 3054 /* MST handled by drm_mst framework */ 3055 if (aconnector->mst_mgr.mst_state == true) 3056 return; 3057 3058 sink = aconnector->dc_link->local_sink; 3059 if (sink) 3060 dc_sink_retain(sink); 3061 3062 /* 3063 * Edid mgmt connector gets first update only in mode_valid hook and then 3064 * the connector sink is set to either fake or physical sink depends on link status. 3065 * Skip if already done during boot. 3066 */ 3067 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED 3068 && aconnector->dc_em_sink) { 3069 3070 /* 3071 * For S3 resume with headless use eml_sink to fake stream 3072 * because on resume connector->sink is set to NULL 3073 */ 3074 mutex_lock(&dev->mode_config.mutex); 3075 3076 if (sink) { 3077 if (aconnector->dc_sink) { 3078 amdgpu_dm_update_freesync_caps(connector, NULL); 3079 /* 3080 * retain and release below are used to 3081 * bump up refcount for sink because the link doesn't point 3082 * to it anymore after disconnect, so on next crtc to connector 3083 * reshuffle by UMD we will get into unwanted dc_sink release 3084 */ 3085 dc_sink_release(aconnector->dc_sink); 3086 } 3087 aconnector->dc_sink = sink; 3088 dc_sink_retain(aconnector->dc_sink); 3089 amdgpu_dm_update_freesync_caps(connector, 3090 aconnector->edid); 3091 } else { 3092 amdgpu_dm_update_freesync_caps(connector, NULL); 3093 if (!aconnector->dc_sink) { 3094 aconnector->dc_sink = aconnector->dc_em_sink; 3095 dc_sink_retain(aconnector->dc_sink); 3096 } 3097 } 3098 3099 mutex_unlock(&dev->mode_config.mutex); 3100 3101 if (sink) 3102 dc_sink_release(sink); 3103 return; 3104 } 3105 3106 /* 3107 * TODO: temporary guard to look for proper fix 3108 * if this sink is MST sink, we should not do anything 3109 */ 3110 if (sink && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST) { 3111 dc_sink_release(sink); 3112 return; 3113 } 3114 3115 if (aconnector->dc_sink == sink) { 3116 /* 3117 * We got a DP short pulse (Link Loss, DP CTS, etc...). 3118 * Do nothing!! 3119 */ 3120 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: dc_sink didn't change.\n", 3121 aconnector->connector_id); 3122 if (sink) 3123 dc_sink_release(sink); 3124 return; 3125 } 3126 3127 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: Old sink=%p New sink=%p\n", 3128 aconnector->connector_id, aconnector->dc_sink, sink); 3129 3130 mutex_lock(&dev->mode_config.mutex); 3131 3132 /* 3133 * 1. Update status of the drm connector 3134 * 2. Send an event and let userspace tell us what to do 3135 */ 3136 if (sink) { 3137 /* 3138 * TODO: check if we still need the S3 mode update workaround. 3139 * If yes, put it here. 3140 */ 3141 if (aconnector->dc_sink) { 3142 amdgpu_dm_update_freesync_caps(connector, NULL); 3143 dc_sink_release(aconnector->dc_sink); 3144 } 3145 3146 aconnector->dc_sink = sink; 3147 dc_sink_retain(aconnector->dc_sink); 3148 if (sink->dc_edid.length == 0) { 3149 aconnector->edid = NULL; 3150 if (aconnector->dc_link->aux_mode) { 3151 drm_dp_cec_unset_edid( 3152 &aconnector->dm_dp_aux.aux); 3153 } 3154 } else { 3155 aconnector->edid = 3156 (struct edid *)sink->dc_edid.raw_edid; 3157 3158 if (aconnector->dc_link->aux_mode) 3159 drm_dp_cec_set_edid(&aconnector->dm_dp_aux.aux, 3160 aconnector->edid); 3161 } 3162 3163 aconnector->timing_requested = kzalloc(sizeof(struct dc_crtc_timing), GFP_KERNEL); 3164 if (!aconnector->timing_requested) 3165 dm_error("%s: failed to create aconnector->requested_timing\n", __func__); 3166 3167 drm_connector_update_edid_property(connector, aconnector->edid); 3168 amdgpu_dm_update_freesync_caps(connector, aconnector->edid); 3169 update_connector_ext_caps(aconnector); 3170 } else { 3171 drm_dp_cec_unset_edid(&aconnector->dm_dp_aux.aux); 3172 amdgpu_dm_update_freesync_caps(connector, NULL); 3173 drm_connector_update_edid_property(connector, NULL); 3174 aconnector->num_modes = 0; 3175 dc_sink_release(aconnector->dc_sink); 3176 aconnector->dc_sink = NULL; 3177 aconnector->edid = NULL; 3178 kfree(aconnector->timing_requested); 3179 aconnector->timing_requested = NULL; 3180 #ifdef CONFIG_DRM_AMD_DC_HDCP 3181 /* Set CP to DESIRED if it was ENABLED, so we can re-enable it again on hotplug */ 3182 if (connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED) 3183 connector->state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED; 3184 #endif 3185 } 3186 3187 mutex_unlock(&dev->mode_config.mutex); 3188 3189 update_subconnector_property(aconnector); 3190 3191 if (sink) 3192 dc_sink_release(sink); 3193 } 3194 3195 static void handle_hpd_irq_helper(struct amdgpu_dm_connector *aconnector) 3196 { 3197 struct drm_connector *connector = &aconnector->base; 3198 struct drm_device *dev = connector->dev; 3199 enum dc_connection_type new_connection_type = dc_connection_none; 3200 struct amdgpu_device *adev = drm_to_adev(dev); 3201 #ifdef CONFIG_DRM_AMD_DC_HDCP 3202 struct dm_connector_state *dm_con_state = to_dm_connector_state(connector->state); 3203 #endif 3204 bool ret = false; 3205 3206 if (adev->dm.disable_hpd_irq) 3207 return; 3208 3209 /* 3210 * In case of failure or MST no need to update connector status or notify the OS 3211 * since (for MST case) MST does this in its own context. 3212 */ 3213 mutex_lock(&aconnector->hpd_lock); 3214 3215 #ifdef CONFIG_DRM_AMD_DC_HDCP 3216 if (adev->dm.hdcp_workqueue) { 3217 hdcp_reset_display(adev->dm.hdcp_workqueue, aconnector->dc_link->link_index); 3218 dm_con_state->update_hdcp = true; 3219 } 3220 #endif 3221 if (aconnector->fake_enable) 3222 aconnector->fake_enable = false; 3223 3224 aconnector->timing_changed = false; 3225 3226 if (!dc_link_detect_sink(aconnector->dc_link, &new_connection_type)) 3227 DRM_ERROR("KMS: Failed to detect connector\n"); 3228 3229 if (aconnector->base.force && new_connection_type == dc_connection_none) { 3230 emulated_link_detect(aconnector->dc_link); 3231 3232 drm_modeset_lock_all(dev); 3233 dm_restore_drm_connector_state(dev, connector); 3234 drm_modeset_unlock_all(dev); 3235 3236 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED) 3237 drm_kms_helper_connector_hotplug_event(connector); 3238 } else { 3239 mutex_lock(&adev->dm.dc_lock); 3240 ret = dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD); 3241 mutex_unlock(&adev->dm.dc_lock); 3242 if (ret) { 3243 amdgpu_dm_update_connector_after_detect(aconnector); 3244 3245 drm_modeset_lock_all(dev); 3246 dm_restore_drm_connector_state(dev, connector); 3247 drm_modeset_unlock_all(dev); 3248 3249 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED) 3250 drm_kms_helper_connector_hotplug_event(connector); 3251 } 3252 } 3253 mutex_unlock(&aconnector->hpd_lock); 3254 3255 } 3256 3257 static void handle_hpd_irq(void *param) 3258 { 3259 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param; 3260 3261 handle_hpd_irq_helper(aconnector); 3262 3263 } 3264 3265 static void schedule_hpd_rx_offload_work(struct hpd_rx_irq_offload_work_queue *offload_wq, 3266 union hpd_irq_data hpd_irq_data) 3267 { 3268 struct hpd_rx_irq_offload_work *offload_work = 3269 kzalloc(sizeof(*offload_work), GFP_KERNEL); 3270 3271 if (!offload_work) { 3272 DRM_ERROR("Failed to allocate hpd_rx_irq_offload_work.\n"); 3273 return; 3274 } 3275 3276 INIT_WORK(&offload_work->work, dm_handle_hpd_rx_offload_work); 3277 offload_work->data = hpd_irq_data; 3278 offload_work->offload_wq = offload_wq; 3279 3280 queue_work(offload_wq->wq, &offload_work->work); 3281 DRM_DEBUG_KMS("queue work to handle hpd_rx offload work"); 3282 } 3283 3284 static void handle_hpd_rx_irq(void *param) 3285 { 3286 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param; 3287 struct drm_connector *connector = &aconnector->base; 3288 struct drm_device *dev = connector->dev; 3289 struct dc_link *dc_link = aconnector->dc_link; 3290 bool is_mst_root_connector = aconnector->mst_mgr.mst_state; 3291 bool result = false; 3292 enum dc_connection_type new_connection_type = dc_connection_none; 3293 struct amdgpu_device *adev = drm_to_adev(dev); 3294 union hpd_irq_data hpd_irq_data; 3295 bool link_loss = false; 3296 bool has_left_work = false; 3297 int idx = dc_link->link_index; 3298 struct hpd_rx_irq_offload_work_queue *offload_wq = &adev->dm.hpd_rx_offload_wq[idx]; 3299 3300 memset(&hpd_irq_data, 0, sizeof(hpd_irq_data)); 3301 3302 if (adev->dm.disable_hpd_irq) 3303 return; 3304 3305 /* 3306 * TODO:Temporary add mutex to protect hpd interrupt not have a gpio 3307 * conflict, after implement i2c helper, this mutex should be 3308 * retired. 3309 */ 3310 mutex_lock(&aconnector->hpd_lock); 3311 3312 result = dc_link_handle_hpd_rx_irq(dc_link, &hpd_irq_data, 3313 &link_loss, true, &has_left_work); 3314 3315 if (!has_left_work) 3316 goto out; 3317 3318 if (hpd_irq_data.bytes.device_service_irq.bits.AUTOMATED_TEST) { 3319 schedule_hpd_rx_offload_work(offload_wq, hpd_irq_data); 3320 goto out; 3321 } 3322 3323 if (dc_link_dp_allow_hpd_rx_irq(dc_link)) { 3324 if (hpd_irq_data.bytes.device_service_irq.bits.UP_REQ_MSG_RDY || 3325 hpd_irq_data.bytes.device_service_irq.bits.DOWN_REP_MSG_RDY) { 3326 bool skip = false; 3327 3328 /* 3329 * DOWN_REP_MSG_RDY is also handled by polling method 3330 * mgr->cbs->poll_hpd_irq() 3331 */ 3332 spin_lock(&offload_wq->offload_lock); 3333 skip = offload_wq->is_handling_mst_msg_rdy_event; 3334 3335 if (!skip) 3336 offload_wq->is_handling_mst_msg_rdy_event = true; 3337 3338 spin_unlock(&offload_wq->offload_lock); 3339 3340 if (!skip) 3341 schedule_hpd_rx_offload_work(offload_wq, hpd_irq_data); 3342 3343 goto out; 3344 } 3345 3346 if (link_loss) { 3347 bool skip = false; 3348 3349 spin_lock(&offload_wq->offload_lock); 3350 skip = offload_wq->is_handling_link_loss; 3351 3352 if (!skip) 3353 offload_wq->is_handling_link_loss = true; 3354 3355 spin_unlock(&offload_wq->offload_lock); 3356 3357 if (!skip) 3358 schedule_hpd_rx_offload_work(offload_wq, hpd_irq_data); 3359 3360 goto out; 3361 } 3362 } 3363 3364 out: 3365 if (result && !is_mst_root_connector) { 3366 /* Downstream Port status changed. */ 3367 if (!dc_link_detect_sink(dc_link, &new_connection_type)) 3368 DRM_ERROR("KMS: Failed to detect connector\n"); 3369 3370 if (aconnector->base.force && new_connection_type == dc_connection_none) { 3371 emulated_link_detect(dc_link); 3372 3373 if (aconnector->fake_enable) 3374 aconnector->fake_enable = false; 3375 3376 amdgpu_dm_update_connector_after_detect(aconnector); 3377 3378 3379 drm_modeset_lock_all(dev); 3380 dm_restore_drm_connector_state(dev, connector); 3381 drm_modeset_unlock_all(dev); 3382 3383 drm_kms_helper_connector_hotplug_event(connector); 3384 } else { 3385 bool ret = false; 3386 3387 mutex_lock(&adev->dm.dc_lock); 3388 ret = dc_link_detect(dc_link, DETECT_REASON_HPDRX); 3389 mutex_unlock(&adev->dm.dc_lock); 3390 3391 if (ret) { 3392 if (aconnector->fake_enable) 3393 aconnector->fake_enable = false; 3394 3395 amdgpu_dm_update_connector_after_detect(aconnector); 3396 3397 drm_modeset_lock_all(dev); 3398 dm_restore_drm_connector_state(dev, connector); 3399 drm_modeset_unlock_all(dev); 3400 3401 drm_kms_helper_connector_hotplug_event(connector); 3402 } 3403 } 3404 } 3405 #ifdef CONFIG_DRM_AMD_DC_HDCP 3406 if (hpd_irq_data.bytes.device_service_irq.bits.CP_IRQ) { 3407 if (adev->dm.hdcp_workqueue) 3408 hdcp_handle_cpirq(adev->dm.hdcp_workqueue, aconnector->base.index); 3409 } 3410 #endif 3411 3412 if (dc_link->type != dc_connection_mst_branch) 3413 drm_dp_cec_irq(&aconnector->dm_dp_aux.aux); 3414 3415 mutex_unlock(&aconnector->hpd_lock); 3416 } 3417 3418 static void register_hpd_handlers(struct amdgpu_device *adev) 3419 { 3420 struct drm_device *dev = adev_to_drm(adev); 3421 struct drm_connector *connector; 3422 struct amdgpu_dm_connector *aconnector; 3423 const struct dc_link *dc_link; 3424 struct dc_interrupt_params int_params = {0}; 3425 3426 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT; 3427 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT; 3428 3429 list_for_each_entry(connector, 3430 &dev->mode_config.connector_list, head) { 3431 3432 aconnector = to_amdgpu_dm_connector(connector); 3433 dc_link = aconnector->dc_link; 3434 3435 if (dc_link->irq_source_hpd != DC_IRQ_SOURCE_INVALID) { 3436 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT; 3437 int_params.irq_source = dc_link->irq_source_hpd; 3438 3439 amdgpu_dm_irq_register_interrupt(adev, &int_params, 3440 handle_hpd_irq, 3441 (void *) aconnector); 3442 } 3443 3444 if (dc_link->irq_source_hpd_rx != DC_IRQ_SOURCE_INVALID) { 3445 3446 /* Also register for DP short pulse (hpd_rx). */ 3447 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT; 3448 int_params.irq_source = dc_link->irq_source_hpd_rx; 3449 3450 amdgpu_dm_irq_register_interrupt(adev, &int_params, 3451 handle_hpd_rx_irq, 3452 (void *) aconnector); 3453 } 3454 3455 if (adev->dm.hpd_rx_offload_wq) 3456 adev->dm.hpd_rx_offload_wq[connector->index].aconnector = 3457 aconnector; 3458 } 3459 } 3460 3461 #if defined(CONFIG_DRM_AMD_DC_SI) 3462 /* Register IRQ sources and initialize IRQ callbacks */ 3463 static int dce60_register_irq_handlers(struct amdgpu_device *adev) 3464 { 3465 struct dc *dc = adev->dm.dc; 3466 struct common_irq_params *c_irq_params; 3467 struct dc_interrupt_params int_params = {0}; 3468 int r; 3469 int i; 3470 unsigned int client_id = AMDGPU_IRQ_CLIENTID_LEGACY; 3471 3472 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT; 3473 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT; 3474 3475 /* 3476 * Actions of amdgpu_irq_add_id(): 3477 * 1. Register a set() function with base driver. 3478 * Base driver will call set() function to enable/disable an 3479 * interrupt in DC hardware. 3480 * 2. Register amdgpu_dm_irq_handler(). 3481 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts 3482 * coming from DC hardware. 3483 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC 3484 * for acknowledging and handling. 3485 */ 3486 3487 /* Use VBLANK interrupt */ 3488 for (i = 0; i < adev->mode_info.num_crtc; i++) { 3489 r = amdgpu_irq_add_id(adev, client_id, i + 1, &adev->crtc_irq); 3490 if (r) { 3491 DRM_ERROR("Failed to add crtc irq id!\n"); 3492 return r; 3493 } 3494 3495 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT; 3496 int_params.irq_source = 3497 dc_interrupt_to_irq_source(dc, i + 1, 0); 3498 3499 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1]; 3500 3501 c_irq_params->adev = adev; 3502 c_irq_params->irq_src = int_params.irq_source; 3503 3504 amdgpu_dm_irq_register_interrupt(adev, &int_params, 3505 dm_crtc_high_irq, c_irq_params); 3506 } 3507 3508 /* Use GRPH_PFLIP interrupt */ 3509 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP; 3510 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) { 3511 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq); 3512 if (r) { 3513 DRM_ERROR("Failed to add page flip irq id!\n"); 3514 return r; 3515 } 3516 3517 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT; 3518 int_params.irq_source = 3519 dc_interrupt_to_irq_source(dc, i, 0); 3520 3521 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST]; 3522 3523 c_irq_params->adev = adev; 3524 c_irq_params->irq_src = int_params.irq_source; 3525 3526 amdgpu_dm_irq_register_interrupt(adev, &int_params, 3527 dm_pflip_high_irq, c_irq_params); 3528 3529 } 3530 3531 /* HPD */ 3532 r = amdgpu_irq_add_id(adev, client_id, 3533 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq); 3534 if (r) { 3535 DRM_ERROR("Failed to add hpd irq id!\n"); 3536 return r; 3537 } 3538 3539 register_hpd_handlers(adev); 3540 3541 return 0; 3542 } 3543 #endif 3544 3545 /* Register IRQ sources and initialize IRQ callbacks */ 3546 static int dce110_register_irq_handlers(struct amdgpu_device *adev) 3547 { 3548 struct dc *dc = adev->dm.dc; 3549 struct common_irq_params *c_irq_params; 3550 struct dc_interrupt_params int_params = {0}; 3551 int r; 3552 int i; 3553 unsigned int client_id = AMDGPU_IRQ_CLIENTID_LEGACY; 3554 3555 if (adev->family >= AMDGPU_FAMILY_AI) 3556 client_id = SOC15_IH_CLIENTID_DCE; 3557 3558 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT; 3559 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT; 3560 3561 /* 3562 * Actions of amdgpu_irq_add_id(): 3563 * 1. Register a set() function with base driver. 3564 * Base driver will call set() function to enable/disable an 3565 * interrupt in DC hardware. 3566 * 2. Register amdgpu_dm_irq_handler(). 3567 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts 3568 * coming from DC hardware. 3569 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC 3570 * for acknowledging and handling. 3571 */ 3572 3573 /* Use VBLANK interrupt */ 3574 for (i = VISLANDS30_IV_SRCID_D1_VERTICAL_INTERRUPT0; i <= VISLANDS30_IV_SRCID_D6_VERTICAL_INTERRUPT0; i++) { 3575 r = amdgpu_irq_add_id(adev, client_id, i, &adev->crtc_irq); 3576 if (r) { 3577 DRM_ERROR("Failed to add crtc irq id!\n"); 3578 return r; 3579 } 3580 3581 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT; 3582 int_params.irq_source = 3583 dc_interrupt_to_irq_source(dc, i, 0); 3584 3585 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1]; 3586 3587 c_irq_params->adev = adev; 3588 c_irq_params->irq_src = int_params.irq_source; 3589 3590 amdgpu_dm_irq_register_interrupt(adev, &int_params, 3591 dm_crtc_high_irq, c_irq_params); 3592 } 3593 3594 /* Use VUPDATE interrupt */ 3595 for (i = VISLANDS30_IV_SRCID_D1_V_UPDATE_INT; i <= VISLANDS30_IV_SRCID_D6_V_UPDATE_INT; i += 2) { 3596 r = amdgpu_irq_add_id(adev, client_id, i, &adev->vupdate_irq); 3597 if (r) { 3598 DRM_ERROR("Failed to add vupdate irq id!\n"); 3599 return r; 3600 } 3601 3602 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT; 3603 int_params.irq_source = 3604 dc_interrupt_to_irq_source(dc, i, 0); 3605 3606 c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1]; 3607 3608 c_irq_params->adev = adev; 3609 c_irq_params->irq_src = int_params.irq_source; 3610 3611 amdgpu_dm_irq_register_interrupt(adev, &int_params, 3612 dm_vupdate_high_irq, c_irq_params); 3613 } 3614 3615 /* Use GRPH_PFLIP interrupt */ 3616 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP; 3617 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) { 3618 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq); 3619 if (r) { 3620 DRM_ERROR("Failed to add page flip irq id!\n"); 3621 return r; 3622 } 3623 3624 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT; 3625 int_params.irq_source = 3626 dc_interrupt_to_irq_source(dc, i, 0); 3627 3628 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST]; 3629 3630 c_irq_params->adev = adev; 3631 c_irq_params->irq_src = int_params.irq_source; 3632 3633 amdgpu_dm_irq_register_interrupt(adev, &int_params, 3634 dm_pflip_high_irq, c_irq_params); 3635 3636 } 3637 3638 /* HPD */ 3639 r = amdgpu_irq_add_id(adev, client_id, 3640 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq); 3641 if (r) { 3642 DRM_ERROR("Failed to add hpd irq id!\n"); 3643 return r; 3644 } 3645 3646 register_hpd_handlers(adev); 3647 3648 return 0; 3649 } 3650 3651 /* Register IRQ sources and initialize IRQ callbacks */ 3652 static int dcn10_register_irq_handlers(struct amdgpu_device *adev) 3653 { 3654 struct dc *dc = adev->dm.dc; 3655 struct common_irq_params *c_irq_params; 3656 struct dc_interrupt_params int_params = {0}; 3657 int r; 3658 int i; 3659 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY) 3660 static const unsigned int vrtl_int_srcid[] = { 3661 DCN_1_0__SRCID__OTG1_VERTICAL_INTERRUPT0_CONTROL, 3662 DCN_1_0__SRCID__OTG2_VERTICAL_INTERRUPT0_CONTROL, 3663 DCN_1_0__SRCID__OTG3_VERTICAL_INTERRUPT0_CONTROL, 3664 DCN_1_0__SRCID__OTG4_VERTICAL_INTERRUPT0_CONTROL, 3665 DCN_1_0__SRCID__OTG5_VERTICAL_INTERRUPT0_CONTROL, 3666 DCN_1_0__SRCID__OTG6_VERTICAL_INTERRUPT0_CONTROL 3667 }; 3668 #endif 3669 3670 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT; 3671 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT; 3672 3673 /* 3674 * Actions of amdgpu_irq_add_id(): 3675 * 1. Register a set() function with base driver. 3676 * Base driver will call set() function to enable/disable an 3677 * interrupt in DC hardware. 3678 * 2. Register amdgpu_dm_irq_handler(). 3679 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts 3680 * coming from DC hardware. 3681 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC 3682 * for acknowledging and handling. 3683 */ 3684 3685 /* Use VSTARTUP interrupt */ 3686 for (i = DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP; 3687 i <= DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP + adev->mode_info.num_crtc - 1; 3688 i++) { 3689 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->crtc_irq); 3690 3691 if (r) { 3692 DRM_ERROR("Failed to add crtc irq id!\n"); 3693 return r; 3694 } 3695 3696 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT; 3697 int_params.irq_source = 3698 dc_interrupt_to_irq_source(dc, i, 0); 3699 3700 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1]; 3701 3702 c_irq_params->adev = adev; 3703 c_irq_params->irq_src = int_params.irq_source; 3704 3705 amdgpu_dm_irq_register_interrupt( 3706 adev, &int_params, dm_crtc_high_irq, c_irq_params); 3707 } 3708 3709 /* Use otg vertical line interrupt */ 3710 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY) 3711 for (i = 0; i <= adev->mode_info.num_crtc - 1; i++) { 3712 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, 3713 vrtl_int_srcid[i], &adev->vline0_irq); 3714 3715 if (r) { 3716 DRM_ERROR("Failed to add vline0 irq id!\n"); 3717 return r; 3718 } 3719 3720 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT; 3721 int_params.irq_source = 3722 dc_interrupt_to_irq_source(dc, vrtl_int_srcid[i], 0); 3723 3724 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID) { 3725 DRM_ERROR("Failed to register vline0 irq %d!\n", vrtl_int_srcid[i]); 3726 break; 3727 } 3728 3729 c_irq_params = &adev->dm.vline0_params[int_params.irq_source 3730 - DC_IRQ_SOURCE_DC1_VLINE0]; 3731 3732 c_irq_params->adev = adev; 3733 c_irq_params->irq_src = int_params.irq_source; 3734 3735 amdgpu_dm_irq_register_interrupt(adev, &int_params, 3736 dm_dcn_vertical_interrupt0_high_irq, c_irq_params); 3737 } 3738 #endif 3739 3740 /* Use VUPDATE_NO_LOCK interrupt on DCN, which seems to correspond to 3741 * the regular VUPDATE interrupt on DCE. We want DC_IRQ_SOURCE_VUPDATEx 3742 * to trigger at end of each vblank, regardless of state of the lock, 3743 * matching DCE behaviour. 3744 */ 3745 for (i = DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT; 3746 i <= DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT + adev->mode_info.num_crtc - 1; 3747 i++) { 3748 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->vupdate_irq); 3749 3750 if (r) { 3751 DRM_ERROR("Failed to add vupdate irq id!\n"); 3752 return r; 3753 } 3754 3755 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT; 3756 int_params.irq_source = 3757 dc_interrupt_to_irq_source(dc, i, 0); 3758 3759 c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1]; 3760 3761 c_irq_params->adev = adev; 3762 c_irq_params->irq_src = int_params.irq_source; 3763 3764 amdgpu_dm_irq_register_interrupt(adev, &int_params, 3765 dm_vupdate_high_irq, c_irq_params); 3766 } 3767 3768 /* Use GRPH_PFLIP interrupt */ 3769 for (i = DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT; 3770 i <= DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT + dc->caps.max_otg_num - 1; 3771 i++) { 3772 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->pageflip_irq); 3773 if (r) { 3774 DRM_ERROR("Failed to add page flip irq id!\n"); 3775 return r; 3776 } 3777 3778 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT; 3779 int_params.irq_source = 3780 dc_interrupt_to_irq_source(dc, i, 0); 3781 3782 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST]; 3783 3784 c_irq_params->adev = adev; 3785 c_irq_params->irq_src = int_params.irq_source; 3786 3787 amdgpu_dm_irq_register_interrupt(adev, &int_params, 3788 dm_pflip_high_irq, c_irq_params); 3789 3790 } 3791 3792 /* HPD */ 3793 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DC_HPD1_INT, 3794 &adev->hpd_irq); 3795 if (r) { 3796 DRM_ERROR("Failed to add hpd irq id!\n"); 3797 return r; 3798 } 3799 3800 register_hpd_handlers(adev); 3801 3802 return 0; 3803 } 3804 /* Register Outbox IRQ sources and initialize IRQ callbacks */ 3805 static int register_outbox_irq_handlers(struct amdgpu_device *adev) 3806 { 3807 struct dc *dc = adev->dm.dc; 3808 struct common_irq_params *c_irq_params; 3809 struct dc_interrupt_params int_params = {0}; 3810 int r, i; 3811 3812 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT; 3813 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT; 3814 3815 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DMCUB_OUTBOX_LOW_PRIORITY_READY_INT, 3816 &adev->dmub_outbox_irq); 3817 if (r) { 3818 DRM_ERROR("Failed to add outbox irq id!\n"); 3819 return r; 3820 } 3821 3822 if (dc->ctx->dmub_srv) { 3823 i = DCN_1_0__SRCID__DMCUB_OUTBOX_LOW_PRIORITY_READY_INT; 3824 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT; 3825 int_params.irq_source = 3826 dc_interrupt_to_irq_source(dc, i, 0); 3827 3828 c_irq_params = &adev->dm.dmub_outbox_params[0]; 3829 3830 c_irq_params->adev = adev; 3831 c_irq_params->irq_src = int_params.irq_source; 3832 3833 amdgpu_dm_irq_register_interrupt(adev, &int_params, 3834 dm_dmub_outbox1_low_irq, c_irq_params); 3835 } 3836 3837 return 0; 3838 } 3839 3840 /* 3841 * Acquires the lock for the atomic state object and returns 3842 * the new atomic state. 3843 * 3844 * This should only be called during atomic check. 3845 */ 3846 int dm_atomic_get_state(struct drm_atomic_state *state, 3847 struct dm_atomic_state **dm_state) 3848 { 3849 struct drm_device *dev = state->dev; 3850 struct amdgpu_device *adev = drm_to_adev(dev); 3851 struct amdgpu_display_manager *dm = &adev->dm; 3852 struct drm_private_state *priv_state; 3853 3854 if (*dm_state) 3855 return 0; 3856 3857 priv_state = drm_atomic_get_private_obj_state(state, &dm->atomic_obj); 3858 if (IS_ERR(priv_state)) 3859 return PTR_ERR(priv_state); 3860 3861 *dm_state = to_dm_atomic_state(priv_state); 3862 3863 return 0; 3864 } 3865 3866 static struct dm_atomic_state * 3867 dm_atomic_get_new_state(struct drm_atomic_state *state) 3868 { 3869 struct drm_device *dev = state->dev; 3870 struct amdgpu_device *adev = drm_to_adev(dev); 3871 struct amdgpu_display_manager *dm = &adev->dm; 3872 struct drm_private_obj *obj; 3873 struct drm_private_state *new_obj_state; 3874 int i; 3875 3876 for_each_new_private_obj_in_state(state, obj, new_obj_state, i) { 3877 if (obj->funcs == dm->atomic_obj.funcs) 3878 return to_dm_atomic_state(new_obj_state); 3879 } 3880 3881 return NULL; 3882 } 3883 3884 static struct drm_private_state * 3885 dm_atomic_duplicate_state(struct drm_private_obj *obj) 3886 { 3887 struct dm_atomic_state *old_state, *new_state; 3888 3889 new_state = kzalloc(sizeof(*new_state), GFP_KERNEL); 3890 if (!new_state) 3891 return NULL; 3892 3893 __drm_atomic_helper_private_obj_duplicate_state(obj, &new_state->base); 3894 3895 old_state = to_dm_atomic_state(obj->state); 3896 3897 if (old_state && old_state->context) 3898 new_state->context = dc_copy_state(old_state->context); 3899 3900 if (!new_state->context) { 3901 kfree(new_state); 3902 return NULL; 3903 } 3904 3905 return &new_state->base; 3906 } 3907 3908 static void dm_atomic_destroy_state(struct drm_private_obj *obj, 3909 struct drm_private_state *state) 3910 { 3911 struct dm_atomic_state *dm_state = to_dm_atomic_state(state); 3912 3913 if (dm_state && dm_state->context) 3914 dc_release_state(dm_state->context); 3915 3916 kfree(dm_state); 3917 } 3918 3919 static struct drm_private_state_funcs dm_atomic_state_funcs = { 3920 .atomic_duplicate_state = dm_atomic_duplicate_state, 3921 .atomic_destroy_state = dm_atomic_destroy_state, 3922 }; 3923 3924 static int amdgpu_dm_mode_config_init(struct amdgpu_device *adev) 3925 { 3926 struct dm_atomic_state *state; 3927 int r; 3928 3929 adev->mode_info.mode_config_initialized = true; 3930 3931 adev_to_drm(adev)->mode_config.funcs = (void *)&amdgpu_dm_mode_funcs; 3932 adev_to_drm(adev)->mode_config.helper_private = &amdgpu_dm_mode_config_helperfuncs; 3933 3934 adev_to_drm(adev)->mode_config.max_width = 16384; 3935 adev_to_drm(adev)->mode_config.max_height = 16384; 3936 3937 adev_to_drm(adev)->mode_config.preferred_depth = 24; 3938 if (adev->asic_type == CHIP_HAWAII) 3939 /* disable prefer shadow for now due to hibernation issues */ 3940 adev_to_drm(adev)->mode_config.prefer_shadow = 0; 3941 else 3942 adev_to_drm(adev)->mode_config.prefer_shadow = 1; 3943 /* indicates support for immediate flip */ 3944 adev_to_drm(adev)->mode_config.async_page_flip = true; 3945 3946 adev_to_drm(adev)->mode_config.fb_base = adev->gmc.aper_base; 3947 3948 state = kzalloc(sizeof(*state), GFP_KERNEL); 3949 if (!state) 3950 return -ENOMEM; 3951 3952 state->context = dc_create_state(adev->dm.dc); 3953 if (!state->context) { 3954 kfree(state); 3955 return -ENOMEM; 3956 } 3957 3958 dc_resource_state_copy_construct_current(adev->dm.dc, state->context); 3959 3960 drm_atomic_private_obj_init(adev_to_drm(adev), 3961 &adev->dm.atomic_obj, 3962 &state->base, 3963 &dm_atomic_state_funcs); 3964 3965 r = amdgpu_display_modeset_create_props(adev); 3966 if (r) { 3967 dc_release_state(state->context); 3968 kfree(state); 3969 return r; 3970 } 3971 3972 r = amdgpu_dm_audio_init(adev); 3973 if (r) { 3974 dc_release_state(state->context); 3975 kfree(state); 3976 return r; 3977 } 3978 3979 return 0; 3980 } 3981 3982 #define AMDGPU_DM_DEFAULT_MIN_BACKLIGHT 12 3983 #define AMDGPU_DM_DEFAULT_MAX_BACKLIGHT 255 3984 #define AUX_BL_DEFAULT_TRANSITION_TIME_MS 50 3985 3986 static void amdgpu_dm_update_backlight_caps(struct amdgpu_display_manager *dm, 3987 int bl_idx) 3988 { 3989 #if defined(CONFIG_ACPI) 3990 struct amdgpu_dm_backlight_caps caps; 3991 3992 memset(&caps, 0, sizeof(caps)); 3993 3994 if (dm->backlight_caps[bl_idx].caps_valid) 3995 return; 3996 3997 amdgpu_acpi_get_backlight_caps(&caps); 3998 if (caps.caps_valid) { 3999 dm->backlight_caps[bl_idx].caps_valid = true; 4000 if (caps.aux_support) 4001 return; 4002 dm->backlight_caps[bl_idx].min_input_signal = caps.min_input_signal; 4003 dm->backlight_caps[bl_idx].max_input_signal = caps.max_input_signal; 4004 } else { 4005 dm->backlight_caps[bl_idx].min_input_signal = 4006 AMDGPU_DM_DEFAULT_MIN_BACKLIGHT; 4007 dm->backlight_caps[bl_idx].max_input_signal = 4008 AMDGPU_DM_DEFAULT_MAX_BACKLIGHT; 4009 } 4010 #else 4011 if (dm->backlight_caps[bl_idx].aux_support) 4012 return; 4013 4014 dm->backlight_caps[bl_idx].min_input_signal = AMDGPU_DM_DEFAULT_MIN_BACKLIGHT; 4015 dm->backlight_caps[bl_idx].max_input_signal = AMDGPU_DM_DEFAULT_MAX_BACKLIGHT; 4016 #endif 4017 } 4018 4019 static int get_brightness_range(const struct amdgpu_dm_backlight_caps *caps, 4020 unsigned int *min, unsigned int *max) 4021 { 4022 if (!caps) 4023 return 0; 4024 4025 if (caps->aux_support) { 4026 // Firmware limits are in nits, DC API wants millinits. 4027 *max = 1000 * caps->aux_max_input_signal; 4028 *min = 1000 * caps->aux_min_input_signal; 4029 } else { 4030 // Firmware limits are 8-bit, PWM control is 16-bit. 4031 *max = 0x101 * caps->max_input_signal; 4032 *min = 0x101 * caps->min_input_signal; 4033 } 4034 return 1; 4035 } 4036 4037 static u32 convert_brightness_from_user(const struct amdgpu_dm_backlight_caps *caps, 4038 uint32_t brightness) 4039 { 4040 unsigned int min, max; 4041 4042 if (!get_brightness_range(caps, &min, &max)) 4043 return brightness; 4044 4045 // Rescale 0..255 to min..max 4046 return min + DIV_ROUND_CLOSEST((max - min) * brightness, 4047 AMDGPU_MAX_BL_LEVEL); 4048 } 4049 4050 static u32 convert_brightness_to_user(const struct amdgpu_dm_backlight_caps *caps, 4051 uint32_t brightness) 4052 { 4053 unsigned int min, max; 4054 4055 if (!get_brightness_range(caps, &min, &max)) 4056 return brightness; 4057 4058 if (brightness < min) 4059 return 0; 4060 // Rescale min..max to 0..255 4061 return DIV_ROUND_CLOSEST(AMDGPU_MAX_BL_LEVEL * (brightness - min), 4062 max - min); 4063 } 4064 4065 static void amdgpu_dm_backlight_set_level(struct amdgpu_display_manager *dm, 4066 int bl_idx, 4067 u32 user_brightness) 4068 { 4069 struct amdgpu_dm_backlight_caps caps; 4070 struct dc_link *link; 4071 u32 brightness; 4072 bool rc; 4073 4074 amdgpu_dm_update_backlight_caps(dm, bl_idx); 4075 caps = dm->backlight_caps[bl_idx]; 4076 4077 dm->brightness[bl_idx] = user_brightness; 4078 /* update scratch register */ 4079 if (bl_idx == 0) 4080 amdgpu_atombios_scratch_regs_set_backlight_level(dm->adev, dm->brightness[bl_idx]); 4081 brightness = convert_brightness_from_user(&caps, dm->brightness[bl_idx]); 4082 link = (struct dc_link *)dm->backlight_link[bl_idx]; 4083 4084 /* Change brightness based on AUX property */ 4085 if (caps.aux_support) { 4086 rc = dc_link_set_backlight_level_nits(link, true, brightness, 4087 AUX_BL_DEFAULT_TRANSITION_TIME_MS); 4088 if (!rc) 4089 DRM_DEBUG("DM: Failed to update backlight via AUX on eDP[%d]\n", bl_idx); 4090 } else { 4091 rc = dc_link_set_backlight_level(link, brightness, 0); 4092 if (!rc) 4093 DRM_DEBUG("DM: Failed to update backlight on eDP[%d]\n", bl_idx); 4094 } 4095 4096 if (rc) 4097 dm->actual_brightness[bl_idx] = user_brightness; 4098 } 4099 4100 static int amdgpu_dm_backlight_update_status(struct backlight_device *bd) 4101 { 4102 struct amdgpu_display_manager *dm = bl_get_data(bd); 4103 int i; 4104 4105 for (i = 0; i < dm->num_of_edps; i++) { 4106 if (bd == dm->backlight_dev[i]) 4107 break; 4108 } 4109 if (i >= AMDGPU_DM_MAX_NUM_EDP) 4110 i = 0; 4111 amdgpu_dm_backlight_set_level(dm, i, bd->props.brightness); 4112 4113 return 0; 4114 } 4115 4116 static u32 amdgpu_dm_backlight_get_level(struct amdgpu_display_manager *dm, 4117 int bl_idx) 4118 { 4119 struct amdgpu_dm_backlight_caps caps; 4120 struct dc_link *link = (struct dc_link *)dm->backlight_link[bl_idx]; 4121 4122 amdgpu_dm_update_backlight_caps(dm, bl_idx); 4123 caps = dm->backlight_caps[bl_idx]; 4124 4125 if (caps.aux_support) { 4126 u32 avg, peak; 4127 bool rc; 4128 4129 rc = dc_link_get_backlight_level_nits(link, &avg, &peak); 4130 if (!rc) 4131 return dm->brightness[bl_idx]; 4132 return convert_brightness_to_user(&caps, avg); 4133 } else { 4134 int ret = dc_link_get_backlight_level(link); 4135 4136 if (ret == DC_ERROR_UNEXPECTED) 4137 return dm->brightness[bl_idx]; 4138 return convert_brightness_to_user(&caps, ret); 4139 } 4140 } 4141 4142 static int amdgpu_dm_backlight_get_brightness(struct backlight_device *bd) 4143 { 4144 struct amdgpu_display_manager *dm = bl_get_data(bd); 4145 int i; 4146 4147 for (i = 0; i < dm->num_of_edps; i++) { 4148 if (bd == dm->backlight_dev[i]) 4149 break; 4150 } 4151 if (i >= AMDGPU_DM_MAX_NUM_EDP) 4152 i = 0; 4153 return amdgpu_dm_backlight_get_level(dm, i); 4154 } 4155 4156 static const struct backlight_ops amdgpu_dm_backlight_ops = { 4157 .options = BL_CORE_SUSPENDRESUME, 4158 .get_brightness = amdgpu_dm_backlight_get_brightness, 4159 .update_status = amdgpu_dm_backlight_update_status, 4160 }; 4161 4162 static void 4163 amdgpu_dm_register_backlight_device(struct amdgpu_display_manager *dm) 4164 { 4165 char bl_name[16]; 4166 struct backlight_properties props = { 0 }; 4167 4168 amdgpu_dm_update_backlight_caps(dm, dm->num_of_edps); 4169 dm->brightness[dm->num_of_edps] = AMDGPU_MAX_BL_LEVEL; 4170 4171 if (!acpi_video_backlight_use_native()) { 4172 drm_info(adev_to_drm(dm->adev), "Skipping amdgpu DM backlight registration\n"); 4173 /* Try registering an ACPI video backlight device instead. */ 4174 acpi_video_register_backlight(); 4175 return; 4176 } 4177 4178 props.max_brightness = AMDGPU_MAX_BL_LEVEL; 4179 props.brightness = AMDGPU_MAX_BL_LEVEL; 4180 props.type = BACKLIGHT_RAW; 4181 4182 snprintf(bl_name, sizeof(bl_name), "amdgpu_bl%d", 4183 adev_to_drm(dm->adev)->primary->index + dm->num_of_edps); 4184 4185 dm->backlight_dev[dm->num_of_edps] = backlight_device_register(bl_name, 4186 adev_to_drm(dm->adev)->dev, 4187 dm, 4188 &amdgpu_dm_backlight_ops, 4189 &props); 4190 4191 if (IS_ERR(dm->backlight_dev[dm->num_of_edps])) 4192 DRM_ERROR("DM: Backlight registration failed!\n"); 4193 else 4194 DRM_DEBUG_DRIVER("DM: Registered Backlight device: %s\n", bl_name); 4195 } 4196 4197 static int initialize_plane(struct amdgpu_display_manager *dm, 4198 struct amdgpu_mode_info *mode_info, int plane_id, 4199 enum drm_plane_type plane_type, 4200 const struct dc_plane_cap *plane_cap) 4201 { 4202 struct drm_plane *plane; 4203 unsigned long possible_crtcs; 4204 int ret = 0; 4205 4206 plane = kzalloc(sizeof(struct drm_plane), GFP_KERNEL); 4207 if (!plane) { 4208 DRM_ERROR("KMS: Failed to allocate plane\n"); 4209 return -ENOMEM; 4210 } 4211 plane->type = plane_type; 4212 4213 /* 4214 * HACK: IGT tests expect that the primary plane for a CRTC 4215 * can only have one possible CRTC. Only expose support for 4216 * any CRTC if they're not going to be used as a primary plane 4217 * for a CRTC - like overlay or underlay planes. 4218 */ 4219 possible_crtcs = 1 << plane_id; 4220 if (plane_id >= dm->dc->caps.max_streams) 4221 possible_crtcs = 0xff; 4222 4223 ret = amdgpu_dm_plane_init(dm, plane, possible_crtcs, plane_cap); 4224 4225 if (ret) { 4226 DRM_ERROR("KMS: Failed to initialize plane\n"); 4227 kfree(plane); 4228 return ret; 4229 } 4230 4231 if (mode_info) 4232 mode_info->planes[plane_id] = plane; 4233 4234 return ret; 4235 } 4236 4237 4238 static void register_backlight_device(struct amdgpu_display_manager *dm, 4239 struct dc_link *link) 4240 { 4241 if ((link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) && 4242 link->type != dc_connection_none) { 4243 /* 4244 * Event if registration failed, we should continue with 4245 * DM initialization because not having a backlight control 4246 * is better then a black screen. 4247 */ 4248 if (!dm->backlight_dev[dm->num_of_edps]) 4249 amdgpu_dm_register_backlight_device(dm); 4250 4251 if (dm->backlight_dev[dm->num_of_edps]) { 4252 dm->backlight_link[dm->num_of_edps] = link; 4253 dm->num_of_edps++; 4254 } 4255 } 4256 } 4257 4258 static void amdgpu_set_panel_orientation(struct drm_connector *connector); 4259 4260 /* 4261 * In this architecture, the association 4262 * connector -> encoder -> crtc 4263 * id not really requried. The crtc and connector will hold the 4264 * display_index as an abstraction to use with DAL component 4265 * 4266 * Returns 0 on success 4267 */ 4268 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev) 4269 { 4270 struct amdgpu_display_manager *dm = &adev->dm; 4271 s32 i; 4272 struct amdgpu_dm_connector *aconnector = NULL; 4273 struct amdgpu_encoder *aencoder = NULL; 4274 struct amdgpu_mode_info *mode_info = &adev->mode_info; 4275 u32 link_cnt; 4276 s32 primary_planes; 4277 enum dc_connection_type new_connection_type = dc_connection_none; 4278 const struct dc_plane_cap *plane; 4279 bool psr_feature_enabled = false; 4280 4281 dm->display_indexes_num = dm->dc->caps.max_streams; 4282 /* Update the actual used number of crtc */ 4283 adev->mode_info.num_crtc = adev->dm.display_indexes_num; 4284 4285 link_cnt = dm->dc->caps.max_links; 4286 if (amdgpu_dm_mode_config_init(dm->adev)) { 4287 DRM_ERROR("DM: Failed to initialize mode config\n"); 4288 return -EINVAL; 4289 } 4290 4291 /* There is one primary plane per CRTC */ 4292 primary_planes = dm->dc->caps.max_streams; 4293 ASSERT(primary_planes <= AMDGPU_MAX_PLANES); 4294 4295 /* 4296 * Initialize primary planes, implicit planes for legacy IOCTLS. 4297 * Order is reversed to match iteration order in atomic check. 4298 */ 4299 for (i = (primary_planes - 1); i >= 0; i--) { 4300 plane = &dm->dc->caps.planes[i]; 4301 4302 if (initialize_plane(dm, mode_info, i, 4303 DRM_PLANE_TYPE_PRIMARY, plane)) { 4304 DRM_ERROR("KMS: Failed to initialize primary plane\n"); 4305 goto fail; 4306 } 4307 } 4308 4309 /* 4310 * Initialize overlay planes, index starting after primary planes. 4311 * These planes have a higher DRM index than the primary planes since 4312 * they should be considered as having a higher z-order. 4313 * Order is reversed to match iteration order in atomic check. 4314 * 4315 * Only support DCN for now, and only expose one so we don't encourage 4316 * userspace to use up all the pipes. 4317 */ 4318 for (i = 0; i < dm->dc->caps.max_planes; ++i) { 4319 struct dc_plane_cap *plane = &dm->dc->caps.planes[i]; 4320 4321 /* Do not create overlay if MPO disabled */ 4322 if (amdgpu_dc_debug_mask & DC_DISABLE_MPO) 4323 break; 4324 4325 if (plane->type != DC_PLANE_TYPE_DCN_UNIVERSAL) 4326 continue; 4327 4328 if (!plane->blends_with_above || !plane->blends_with_below) 4329 continue; 4330 4331 if (!plane->pixel_format_support.argb8888) 4332 continue; 4333 4334 if (initialize_plane(dm, NULL, primary_planes + i, 4335 DRM_PLANE_TYPE_OVERLAY, plane)) { 4336 DRM_ERROR("KMS: Failed to initialize overlay plane\n"); 4337 goto fail; 4338 } 4339 4340 /* Only create one overlay plane. */ 4341 break; 4342 } 4343 4344 for (i = 0; i < dm->dc->caps.max_streams; i++) 4345 if (amdgpu_dm_crtc_init(dm, mode_info->planes[i], i)) { 4346 DRM_ERROR("KMS: Failed to initialize crtc\n"); 4347 goto fail; 4348 } 4349 4350 /* Use Outbox interrupt */ 4351 switch (adev->ip_versions[DCE_HWIP][0]) { 4352 case IP_VERSION(3, 0, 0): 4353 case IP_VERSION(3, 1, 2): 4354 case IP_VERSION(3, 1, 3): 4355 case IP_VERSION(3, 1, 4): 4356 case IP_VERSION(3, 1, 5): 4357 case IP_VERSION(3, 1, 6): 4358 case IP_VERSION(3, 2, 0): 4359 case IP_VERSION(3, 2, 1): 4360 case IP_VERSION(2, 1, 0): 4361 if (register_outbox_irq_handlers(dm->adev)) { 4362 DRM_ERROR("DM: Failed to initialize IRQ\n"); 4363 goto fail; 4364 } 4365 break; 4366 default: 4367 DRM_DEBUG_KMS("Unsupported DCN IP version for outbox: 0x%X\n", 4368 adev->ip_versions[DCE_HWIP][0]); 4369 } 4370 4371 /* Determine whether to enable PSR support by default. */ 4372 if (!(amdgpu_dc_debug_mask & DC_DISABLE_PSR)) { 4373 switch (adev->ip_versions[DCE_HWIP][0]) { 4374 case IP_VERSION(3, 1, 2): 4375 case IP_VERSION(3, 1, 3): 4376 case IP_VERSION(3, 1, 4): 4377 case IP_VERSION(3, 1, 5): 4378 case IP_VERSION(3, 1, 6): 4379 case IP_VERSION(3, 2, 0): 4380 case IP_VERSION(3, 2, 1): 4381 psr_feature_enabled = true; 4382 break; 4383 default: 4384 psr_feature_enabled = amdgpu_dc_feature_mask & DC_PSR_MASK; 4385 break; 4386 } 4387 } 4388 4389 /* loops over all connectors on the board */ 4390 for (i = 0; i < link_cnt; i++) { 4391 struct dc_link *link = NULL; 4392 4393 if (i > AMDGPU_DM_MAX_DISPLAY_INDEX) { 4394 DRM_ERROR( 4395 "KMS: Cannot support more than %d display indexes\n", 4396 AMDGPU_DM_MAX_DISPLAY_INDEX); 4397 continue; 4398 } 4399 4400 aconnector = kzalloc(sizeof(*aconnector), GFP_KERNEL); 4401 if (!aconnector) 4402 goto fail; 4403 4404 aencoder = kzalloc(sizeof(*aencoder), GFP_KERNEL); 4405 if (!aencoder) 4406 goto fail; 4407 4408 if (amdgpu_dm_encoder_init(dm->ddev, aencoder, i)) { 4409 DRM_ERROR("KMS: Failed to initialize encoder\n"); 4410 goto fail; 4411 } 4412 4413 if (amdgpu_dm_connector_init(dm, aconnector, i, aencoder)) { 4414 DRM_ERROR("KMS: Failed to initialize connector\n"); 4415 goto fail; 4416 } 4417 4418 link = dc_get_link_at_index(dm->dc, i); 4419 4420 if (!dc_link_detect_sink(link, &new_connection_type)) 4421 DRM_ERROR("KMS: Failed to detect connector\n"); 4422 4423 if (aconnector->base.force && new_connection_type == dc_connection_none) { 4424 emulated_link_detect(link); 4425 amdgpu_dm_update_connector_after_detect(aconnector); 4426 } else { 4427 bool ret = false; 4428 4429 mutex_lock(&dm->dc_lock); 4430 ret = dc_link_detect(link, DETECT_REASON_BOOT); 4431 mutex_unlock(&dm->dc_lock); 4432 4433 if (ret) { 4434 amdgpu_dm_update_connector_after_detect(aconnector); 4435 register_backlight_device(dm, link); 4436 4437 if (dm->num_of_edps) 4438 update_connector_ext_caps(aconnector); 4439 4440 if (psr_feature_enabled) 4441 amdgpu_dm_set_psr_caps(link); 4442 4443 /* TODO: Fix vblank control helpers to delay PSR entry to allow this when 4444 * PSR is also supported. 4445 */ 4446 if (link->psr_settings.psr_feature_enabled) 4447 adev_to_drm(adev)->vblank_disable_immediate = false; 4448 } 4449 } 4450 amdgpu_set_panel_orientation(&aconnector->base); 4451 } 4452 4453 /* If we didn't find a panel, notify the acpi video detection */ 4454 if (dm->adev->flags & AMD_IS_APU && dm->num_of_edps == 0) 4455 acpi_video_report_nolcd(); 4456 4457 /* Software is initialized. Now we can register interrupt handlers. */ 4458 switch (adev->asic_type) { 4459 #if defined(CONFIG_DRM_AMD_DC_SI) 4460 case CHIP_TAHITI: 4461 case CHIP_PITCAIRN: 4462 case CHIP_VERDE: 4463 case CHIP_OLAND: 4464 if (dce60_register_irq_handlers(dm->adev)) { 4465 DRM_ERROR("DM: Failed to initialize IRQ\n"); 4466 goto fail; 4467 } 4468 break; 4469 #endif 4470 case CHIP_BONAIRE: 4471 case CHIP_HAWAII: 4472 case CHIP_KAVERI: 4473 case CHIP_KABINI: 4474 case CHIP_MULLINS: 4475 case CHIP_TONGA: 4476 case CHIP_FIJI: 4477 case CHIP_CARRIZO: 4478 case CHIP_STONEY: 4479 case CHIP_POLARIS11: 4480 case CHIP_POLARIS10: 4481 case CHIP_POLARIS12: 4482 case CHIP_VEGAM: 4483 case CHIP_VEGA10: 4484 case CHIP_VEGA12: 4485 case CHIP_VEGA20: 4486 if (dce110_register_irq_handlers(dm->adev)) { 4487 DRM_ERROR("DM: Failed to initialize IRQ\n"); 4488 goto fail; 4489 } 4490 break; 4491 default: 4492 switch (adev->ip_versions[DCE_HWIP][0]) { 4493 case IP_VERSION(1, 0, 0): 4494 case IP_VERSION(1, 0, 1): 4495 case IP_VERSION(2, 0, 2): 4496 case IP_VERSION(2, 0, 3): 4497 case IP_VERSION(2, 0, 0): 4498 case IP_VERSION(2, 1, 0): 4499 case IP_VERSION(3, 0, 0): 4500 case IP_VERSION(3, 0, 2): 4501 case IP_VERSION(3, 0, 3): 4502 case IP_VERSION(3, 0, 1): 4503 case IP_VERSION(3, 1, 2): 4504 case IP_VERSION(3, 1, 3): 4505 case IP_VERSION(3, 1, 4): 4506 case IP_VERSION(3, 1, 5): 4507 case IP_VERSION(3, 1, 6): 4508 case IP_VERSION(3, 2, 0): 4509 case IP_VERSION(3, 2, 1): 4510 if (dcn10_register_irq_handlers(dm->adev)) { 4511 DRM_ERROR("DM: Failed to initialize IRQ\n"); 4512 goto fail; 4513 } 4514 break; 4515 default: 4516 DRM_ERROR("Unsupported DCE IP versions: 0x%X\n", 4517 adev->ip_versions[DCE_HWIP][0]); 4518 goto fail; 4519 } 4520 break; 4521 } 4522 4523 return 0; 4524 fail: 4525 kfree(aencoder); 4526 kfree(aconnector); 4527 4528 return -EINVAL; 4529 } 4530 4531 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm) 4532 { 4533 drm_atomic_private_obj_fini(&dm->atomic_obj); 4534 } 4535 4536 /****************************************************************************** 4537 * amdgpu_display_funcs functions 4538 *****************************************************************************/ 4539 4540 /* 4541 * dm_bandwidth_update - program display watermarks 4542 * 4543 * @adev: amdgpu_device pointer 4544 * 4545 * Calculate and program the display watermarks and line buffer allocation. 4546 */ 4547 static void dm_bandwidth_update(struct amdgpu_device *adev) 4548 { 4549 /* TODO: implement later */ 4550 } 4551 4552 static const struct amdgpu_display_funcs dm_display_funcs = { 4553 .bandwidth_update = dm_bandwidth_update, /* called unconditionally */ 4554 .vblank_get_counter = dm_vblank_get_counter,/* called unconditionally */ 4555 .backlight_set_level = NULL, /* never called for DC */ 4556 .backlight_get_level = NULL, /* never called for DC */ 4557 .hpd_sense = NULL,/* called unconditionally */ 4558 .hpd_set_polarity = NULL, /* called unconditionally */ 4559 .hpd_get_gpio_reg = NULL, /* VBIOS parsing. DAL does it. */ 4560 .page_flip_get_scanoutpos = 4561 dm_crtc_get_scanoutpos,/* called unconditionally */ 4562 .add_encoder = NULL, /* VBIOS parsing. DAL does it. */ 4563 .add_connector = NULL, /* VBIOS parsing. DAL does it. */ 4564 }; 4565 4566 #if defined(CONFIG_DEBUG_KERNEL_DC) 4567 4568 static ssize_t s3_debug_store(struct device *device, 4569 struct device_attribute *attr, 4570 const char *buf, 4571 size_t count) 4572 { 4573 int ret; 4574 int s3_state; 4575 struct drm_device *drm_dev = dev_get_drvdata(device); 4576 struct amdgpu_device *adev = drm_to_adev(drm_dev); 4577 4578 ret = kstrtoint(buf, 0, &s3_state); 4579 4580 if (ret == 0) { 4581 if (s3_state) { 4582 dm_resume(adev); 4583 drm_kms_helper_hotplug_event(adev_to_drm(adev)); 4584 } else 4585 dm_suspend(adev); 4586 } 4587 4588 return ret == 0 ? count : 0; 4589 } 4590 4591 DEVICE_ATTR_WO(s3_debug); 4592 4593 #endif 4594 4595 static int dm_early_init(void *handle) 4596 { 4597 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 4598 struct amdgpu_mode_info *mode_info = &adev->mode_info; 4599 struct atom_context *ctx = mode_info->atom_context; 4600 int index = GetIndexIntoMasterTable(DATA, Object_Header); 4601 u16 data_offset; 4602 4603 /* if there is no object header, skip DM */ 4604 if (!amdgpu_atom_parse_data_header(ctx, index, NULL, NULL, NULL, &data_offset)) { 4605 adev->harvest_ip_mask |= AMD_HARVEST_IP_DMU_MASK; 4606 dev_info(adev->dev, "No object header, skipping DM\n"); 4607 return -ENOENT; 4608 } 4609 4610 switch (adev->asic_type) { 4611 #if defined(CONFIG_DRM_AMD_DC_SI) 4612 case CHIP_TAHITI: 4613 case CHIP_PITCAIRN: 4614 case CHIP_VERDE: 4615 adev->mode_info.num_crtc = 6; 4616 adev->mode_info.num_hpd = 6; 4617 adev->mode_info.num_dig = 6; 4618 break; 4619 case CHIP_OLAND: 4620 adev->mode_info.num_crtc = 2; 4621 adev->mode_info.num_hpd = 2; 4622 adev->mode_info.num_dig = 2; 4623 break; 4624 #endif 4625 case CHIP_BONAIRE: 4626 case CHIP_HAWAII: 4627 adev->mode_info.num_crtc = 6; 4628 adev->mode_info.num_hpd = 6; 4629 adev->mode_info.num_dig = 6; 4630 break; 4631 case CHIP_KAVERI: 4632 adev->mode_info.num_crtc = 4; 4633 adev->mode_info.num_hpd = 6; 4634 adev->mode_info.num_dig = 7; 4635 break; 4636 case CHIP_KABINI: 4637 case CHIP_MULLINS: 4638 adev->mode_info.num_crtc = 2; 4639 adev->mode_info.num_hpd = 6; 4640 adev->mode_info.num_dig = 6; 4641 break; 4642 case CHIP_FIJI: 4643 case CHIP_TONGA: 4644 adev->mode_info.num_crtc = 6; 4645 adev->mode_info.num_hpd = 6; 4646 adev->mode_info.num_dig = 7; 4647 break; 4648 case CHIP_CARRIZO: 4649 adev->mode_info.num_crtc = 3; 4650 adev->mode_info.num_hpd = 6; 4651 adev->mode_info.num_dig = 9; 4652 break; 4653 case CHIP_STONEY: 4654 adev->mode_info.num_crtc = 2; 4655 adev->mode_info.num_hpd = 6; 4656 adev->mode_info.num_dig = 9; 4657 break; 4658 case CHIP_POLARIS11: 4659 case CHIP_POLARIS12: 4660 adev->mode_info.num_crtc = 5; 4661 adev->mode_info.num_hpd = 5; 4662 adev->mode_info.num_dig = 5; 4663 break; 4664 case CHIP_POLARIS10: 4665 case CHIP_VEGAM: 4666 adev->mode_info.num_crtc = 6; 4667 adev->mode_info.num_hpd = 6; 4668 adev->mode_info.num_dig = 6; 4669 break; 4670 case CHIP_VEGA10: 4671 case CHIP_VEGA12: 4672 case CHIP_VEGA20: 4673 adev->mode_info.num_crtc = 6; 4674 adev->mode_info.num_hpd = 6; 4675 adev->mode_info.num_dig = 6; 4676 break; 4677 default: 4678 4679 switch (adev->ip_versions[DCE_HWIP][0]) { 4680 case IP_VERSION(2, 0, 2): 4681 case IP_VERSION(3, 0, 0): 4682 adev->mode_info.num_crtc = 6; 4683 adev->mode_info.num_hpd = 6; 4684 adev->mode_info.num_dig = 6; 4685 break; 4686 case IP_VERSION(2, 0, 0): 4687 case IP_VERSION(3, 0, 2): 4688 adev->mode_info.num_crtc = 5; 4689 adev->mode_info.num_hpd = 5; 4690 adev->mode_info.num_dig = 5; 4691 break; 4692 case IP_VERSION(2, 0, 3): 4693 case IP_VERSION(3, 0, 3): 4694 adev->mode_info.num_crtc = 2; 4695 adev->mode_info.num_hpd = 2; 4696 adev->mode_info.num_dig = 2; 4697 break; 4698 case IP_VERSION(1, 0, 0): 4699 case IP_VERSION(1, 0, 1): 4700 case IP_VERSION(3, 0, 1): 4701 case IP_VERSION(2, 1, 0): 4702 case IP_VERSION(3, 1, 2): 4703 case IP_VERSION(3, 1, 3): 4704 case IP_VERSION(3, 1, 4): 4705 case IP_VERSION(3, 1, 5): 4706 case IP_VERSION(3, 1, 6): 4707 case IP_VERSION(3, 2, 0): 4708 case IP_VERSION(3, 2, 1): 4709 adev->mode_info.num_crtc = 4; 4710 adev->mode_info.num_hpd = 4; 4711 adev->mode_info.num_dig = 4; 4712 break; 4713 default: 4714 DRM_ERROR("Unsupported DCE IP versions: 0x%x\n", 4715 adev->ip_versions[DCE_HWIP][0]); 4716 return -EINVAL; 4717 } 4718 break; 4719 } 4720 4721 amdgpu_dm_set_irq_funcs(adev); 4722 4723 if (adev->mode_info.funcs == NULL) 4724 adev->mode_info.funcs = &dm_display_funcs; 4725 4726 /* 4727 * Note: Do NOT change adev->audio_endpt_rreg and 4728 * adev->audio_endpt_wreg because they are initialised in 4729 * amdgpu_device_init() 4730 */ 4731 #if defined(CONFIG_DEBUG_KERNEL_DC) 4732 device_create_file( 4733 adev_to_drm(adev)->dev, 4734 &dev_attr_s3_debug); 4735 #endif 4736 4737 return 0; 4738 } 4739 4740 static bool modereset_required(struct drm_crtc_state *crtc_state) 4741 { 4742 return !crtc_state->active && drm_atomic_crtc_needs_modeset(crtc_state); 4743 } 4744 4745 static void amdgpu_dm_encoder_destroy(struct drm_encoder *encoder) 4746 { 4747 drm_encoder_cleanup(encoder); 4748 kfree(encoder); 4749 } 4750 4751 static const struct drm_encoder_funcs amdgpu_dm_encoder_funcs = { 4752 .destroy = amdgpu_dm_encoder_destroy, 4753 }; 4754 4755 static int 4756 fill_plane_color_attributes(const struct drm_plane_state *plane_state, 4757 const enum surface_pixel_format format, 4758 enum dc_color_space *color_space) 4759 { 4760 bool full_range; 4761 4762 *color_space = COLOR_SPACE_SRGB; 4763 4764 /* DRM color properties only affect non-RGB formats. */ 4765 if (format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) 4766 return 0; 4767 4768 full_range = (plane_state->color_range == DRM_COLOR_YCBCR_FULL_RANGE); 4769 4770 switch (plane_state->color_encoding) { 4771 case DRM_COLOR_YCBCR_BT601: 4772 if (full_range) 4773 *color_space = COLOR_SPACE_YCBCR601; 4774 else 4775 *color_space = COLOR_SPACE_YCBCR601_LIMITED; 4776 break; 4777 4778 case DRM_COLOR_YCBCR_BT709: 4779 if (full_range) 4780 *color_space = COLOR_SPACE_YCBCR709; 4781 else 4782 *color_space = COLOR_SPACE_YCBCR709_LIMITED; 4783 break; 4784 4785 case DRM_COLOR_YCBCR_BT2020: 4786 if (full_range) 4787 *color_space = COLOR_SPACE_2020_YCBCR; 4788 else 4789 return -EINVAL; 4790 break; 4791 4792 default: 4793 return -EINVAL; 4794 } 4795 4796 return 0; 4797 } 4798 4799 static int 4800 fill_dc_plane_info_and_addr(struct amdgpu_device *adev, 4801 const struct drm_plane_state *plane_state, 4802 const u64 tiling_flags, 4803 struct dc_plane_info *plane_info, 4804 struct dc_plane_address *address, 4805 bool tmz_surface, 4806 bool force_disable_dcc) 4807 { 4808 const struct drm_framebuffer *fb = plane_state->fb; 4809 const struct amdgpu_framebuffer *afb = 4810 to_amdgpu_framebuffer(plane_state->fb); 4811 int ret; 4812 4813 memset(plane_info, 0, sizeof(*plane_info)); 4814 4815 switch (fb->format->format) { 4816 case DRM_FORMAT_C8: 4817 plane_info->format = 4818 SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS; 4819 break; 4820 case DRM_FORMAT_RGB565: 4821 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_RGB565; 4822 break; 4823 case DRM_FORMAT_XRGB8888: 4824 case DRM_FORMAT_ARGB8888: 4825 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888; 4826 break; 4827 case DRM_FORMAT_XRGB2101010: 4828 case DRM_FORMAT_ARGB2101010: 4829 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010; 4830 break; 4831 case DRM_FORMAT_XBGR2101010: 4832 case DRM_FORMAT_ABGR2101010: 4833 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010; 4834 break; 4835 case DRM_FORMAT_XBGR8888: 4836 case DRM_FORMAT_ABGR8888: 4837 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR8888; 4838 break; 4839 case DRM_FORMAT_NV21: 4840 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr; 4841 break; 4842 case DRM_FORMAT_NV12: 4843 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb; 4844 break; 4845 case DRM_FORMAT_P010: 4846 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb; 4847 break; 4848 case DRM_FORMAT_XRGB16161616F: 4849 case DRM_FORMAT_ARGB16161616F: 4850 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F; 4851 break; 4852 case DRM_FORMAT_XBGR16161616F: 4853 case DRM_FORMAT_ABGR16161616F: 4854 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F; 4855 break; 4856 case DRM_FORMAT_XRGB16161616: 4857 case DRM_FORMAT_ARGB16161616: 4858 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616; 4859 break; 4860 case DRM_FORMAT_XBGR16161616: 4861 case DRM_FORMAT_ABGR16161616: 4862 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616; 4863 break; 4864 default: 4865 DRM_ERROR( 4866 "Unsupported screen format %p4cc\n", 4867 &fb->format->format); 4868 return -EINVAL; 4869 } 4870 4871 switch (plane_state->rotation & DRM_MODE_ROTATE_MASK) { 4872 case DRM_MODE_ROTATE_0: 4873 plane_info->rotation = ROTATION_ANGLE_0; 4874 break; 4875 case DRM_MODE_ROTATE_90: 4876 plane_info->rotation = ROTATION_ANGLE_90; 4877 break; 4878 case DRM_MODE_ROTATE_180: 4879 plane_info->rotation = ROTATION_ANGLE_180; 4880 break; 4881 case DRM_MODE_ROTATE_270: 4882 plane_info->rotation = ROTATION_ANGLE_270; 4883 break; 4884 default: 4885 plane_info->rotation = ROTATION_ANGLE_0; 4886 break; 4887 } 4888 4889 4890 plane_info->visible = true; 4891 plane_info->stereo_format = PLANE_STEREO_FORMAT_NONE; 4892 4893 plane_info->layer_index = plane_state->normalized_zpos; 4894 4895 ret = fill_plane_color_attributes(plane_state, plane_info->format, 4896 &plane_info->color_space); 4897 if (ret) 4898 return ret; 4899 4900 ret = fill_plane_buffer_attributes(adev, afb, plane_info->format, 4901 plane_info->rotation, tiling_flags, 4902 &plane_info->tiling_info, 4903 &plane_info->plane_size, 4904 &plane_info->dcc, address, 4905 tmz_surface, force_disable_dcc); 4906 if (ret) 4907 return ret; 4908 4909 fill_blending_from_plane_state( 4910 plane_state, &plane_info->per_pixel_alpha, &plane_info->pre_multiplied_alpha, 4911 &plane_info->global_alpha, &plane_info->global_alpha_value); 4912 4913 return 0; 4914 } 4915 4916 static int fill_dc_plane_attributes(struct amdgpu_device *adev, 4917 struct dc_plane_state *dc_plane_state, 4918 struct drm_plane_state *plane_state, 4919 struct drm_crtc_state *crtc_state) 4920 { 4921 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state); 4922 struct amdgpu_framebuffer *afb = (struct amdgpu_framebuffer *)plane_state->fb; 4923 struct dc_scaling_info scaling_info; 4924 struct dc_plane_info plane_info; 4925 int ret; 4926 bool force_disable_dcc = false; 4927 4928 ret = fill_dc_scaling_info(adev, plane_state, &scaling_info); 4929 if (ret) 4930 return ret; 4931 4932 dc_plane_state->src_rect = scaling_info.src_rect; 4933 dc_plane_state->dst_rect = scaling_info.dst_rect; 4934 dc_plane_state->clip_rect = scaling_info.clip_rect; 4935 dc_plane_state->scaling_quality = scaling_info.scaling_quality; 4936 4937 force_disable_dcc = adev->asic_type == CHIP_RAVEN && adev->in_suspend; 4938 ret = fill_dc_plane_info_and_addr(adev, plane_state, 4939 afb->tiling_flags, 4940 &plane_info, 4941 &dc_plane_state->address, 4942 afb->tmz_surface, 4943 force_disable_dcc); 4944 if (ret) 4945 return ret; 4946 4947 dc_plane_state->format = plane_info.format; 4948 dc_plane_state->color_space = plane_info.color_space; 4949 dc_plane_state->format = plane_info.format; 4950 dc_plane_state->plane_size = plane_info.plane_size; 4951 dc_plane_state->rotation = plane_info.rotation; 4952 dc_plane_state->horizontal_mirror = plane_info.horizontal_mirror; 4953 dc_plane_state->stereo_format = plane_info.stereo_format; 4954 dc_plane_state->tiling_info = plane_info.tiling_info; 4955 dc_plane_state->visible = plane_info.visible; 4956 dc_plane_state->per_pixel_alpha = plane_info.per_pixel_alpha; 4957 dc_plane_state->pre_multiplied_alpha = plane_info.pre_multiplied_alpha; 4958 dc_plane_state->global_alpha = plane_info.global_alpha; 4959 dc_plane_state->global_alpha_value = plane_info.global_alpha_value; 4960 dc_plane_state->dcc = plane_info.dcc; 4961 dc_plane_state->layer_index = plane_info.layer_index; 4962 dc_plane_state->flip_int_enabled = true; 4963 4964 /* 4965 * Always set input transfer function, since plane state is refreshed 4966 * every time. 4967 */ 4968 ret = amdgpu_dm_update_plane_color_mgmt(dm_crtc_state, dc_plane_state); 4969 if (ret) 4970 return ret; 4971 4972 return 0; 4973 } 4974 4975 static inline void fill_dc_dirty_rect(struct drm_plane *plane, 4976 struct rect *dirty_rect, int32_t x, 4977 int32_t y, int32_t width, int32_t height, 4978 int *i, bool ffu) 4979 { 4980 WARN_ON(*i >= DC_MAX_DIRTY_RECTS); 4981 4982 dirty_rect->x = x; 4983 dirty_rect->y = y; 4984 dirty_rect->width = width; 4985 dirty_rect->height = height; 4986 4987 if (ffu) 4988 drm_dbg(plane->dev, 4989 "[PLANE:%d] PSR FFU dirty rect size (%d, %d)\n", 4990 plane->base.id, width, height); 4991 else 4992 drm_dbg(plane->dev, 4993 "[PLANE:%d] PSR SU dirty rect at (%d, %d) size (%d, %d)", 4994 plane->base.id, x, y, width, height); 4995 4996 (*i)++; 4997 } 4998 4999 /** 5000 * fill_dc_dirty_rects() - Fill DC dirty regions for PSR selective updates 5001 * 5002 * @plane: DRM plane containing dirty regions that need to be flushed to the eDP 5003 * remote fb 5004 * @old_plane_state: Old state of @plane 5005 * @new_plane_state: New state of @plane 5006 * @crtc_state: New state of CRTC connected to the @plane 5007 * @flip_addrs: DC flip tracking struct, which also tracts dirty rects 5008 * 5009 * For PSR SU, DC informs the DMUB uController of dirty rectangle regions 5010 * (referred to as "damage clips" in DRM nomenclature) that require updating on 5011 * the eDP remote buffer. The responsibility of specifying the dirty regions is 5012 * amdgpu_dm's. 5013 * 5014 * A damage-aware DRM client should fill the FB_DAMAGE_CLIPS property on the 5015 * plane with regions that require flushing to the eDP remote buffer. In 5016 * addition, certain use cases - such as cursor and multi-plane overlay (MPO) - 5017 * implicitly provide damage clips without any client support via the plane 5018 * bounds. 5019 */ 5020 static void fill_dc_dirty_rects(struct drm_plane *plane, 5021 struct drm_plane_state *old_plane_state, 5022 struct drm_plane_state *new_plane_state, 5023 struct drm_crtc_state *crtc_state, 5024 struct dc_flip_addrs *flip_addrs) 5025 { 5026 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state); 5027 struct rect *dirty_rects = flip_addrs->dirty_rects; 5028 uint32_t num_clips; 5029 struct drm_mode_rect *clips; 5030 bool bb_changed; 5031 bool fb_changed; 5032 u32 i = 0; 5033 5034 /* 5035 * Cursor plane has it's own dirty rect update interface. See 5036 * dcn10_dmub_update_cursor_data and dmub_cmd_update_cursor_info_data 5037 */ 5038 if (plane->type == DRM_PLANE_TYPE_CURSOR) 5039 return; 5040 5041 num_clips = drm_plane_get_damage_clips_count(new_plane_state); 5042 clips = drm_plane_get_damage_clips(new_plane_state); 5043 5044 if (!dm_crtc_state->mpo_requested) { 5045 if (!num_clips || num_clips > DC_MAX_DIRTY_RECTS) 5046 goto ffu; 5047 5048 for (; flip_addrs->dirty_rect_count < num_clips; clips++) 5049 fill_dc_dirty_rect(new_plane_state->plane, 5050 &dirty_rects[flip_addrs->dirty_rect_count], 5051 clips->x1, clips->y1, 5052 clips->x2 - clips->x1, clips->y2 - clips->y1, 5053 &flip_addrs->dirty_rect_count, 5054 false); 5055 return; 5056 } 5057 5058 /* 5059 * MPO is requested. Add entire plane bounding box to dirty rects if 5060 * flipped to or damaged. 5061 * 5062 * If plane is moved or resized, also add old bounding box to dirty 5063 * rects. 5064 */ 5065 fb_changed = old_plane_state->fb->base.id != 5066 new_plane_state->fb->base.id; 5067 bb_changed = (old_plane_state->crtc_x != new_plane_state->crtc_x || 5068 old_plane_state->crtc_y != new_plane_state->crtc_y || 5069 old_plane_state->crtc_w != new_plane_state->crtc_w || 5070 old_plane_state->crtc_h != new_plane_state->crtc_h); 5071 5072 drm_dbg(plane->dev, 5073 "[PLANE:%d] PSR bb_changed:%d fb_changed:%d num_clips:%d\n", 5074 new_plane_state->plane->base.id, 5075 bb_changed, fb_changed, num_clips); 5076 5077 if ((num_clips + (bb_changed ? 2 : 0)) > DC_MAX_DIRTY_RECTS) 5078 goto ffu; 5079 5080 if (bb_changed) { 5081 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[i], 5082 new_plane_state->crtc_x, 5083 new_plane_state->crtc_y, 5084 new_plane_state->crtc_w, 5085 new_plane_state->crtc_h, &i, false); 5086 5087 /* Add old plane bounding-box if plane is moved or resized */ 5088 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[i], 5089 old_plane_state->crtc_x, 5090 old_plane_state->crtc_y, 5091 old_plane_state->crtc_w, 5092 old_plane_state->crtc_h, &i, false); 5093 } 5094 5095 if (num_clips) { 5096 for (; i < num_clips; clips++) 5097 fill_dc_dirty_rect(new_plane_state->plane, 5098 &dirty_rects[i], clips->x1, 5099 clips->y1, clips->x2 - clips->x1, 5100 clips->y2 - clips->y1, &i, false); 5101 } else if (fb_changed && !bb_changed) { 5102 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[i], 5103 new_plane_state->crtc_x, 5104 new_plane_state->crtc_y, 5105 new_plane_state->crtc_w, 5106 new_plane_state->crtc_h, &i, false); 5107 } 5108 5109 flip_addrs->dirty_rect_count = i; 5110 return; 5111 5112 ffu: 5113 fill_dc_dirty_rect(new_plane_state->plane, &dirty_rects[0], 0, 0, 5114 dm_crtc_state->base.mode.crtc_hdisplay, 5115 dm_crtc_state->base.mode.crtc_vdisplay, 5116 &flip_addrs->dirty_rect_count, true); 5117 } 5118 5119 static void update_stream_scaling_settings(const struct drm_display_mode *mode, 5120 const struct dm_connector_state *dm_state, 5121 struct dc_stream_state *stream) 5122 { 5123 enum amdgpu_rmx_type rmx_type; 5124 5125 struct rect src = { 0 }; /* viewport in composition space*/ 5126 struct rect dst = { 0 }; /* stream addressable area */ 5127 5128 /* no mode. nothing to be done */ 5129 if (!mode) 5130 return; 5131 5132 /* Full screen scaling by default */ 5133 src.width = mode->hdisplay; 5134 src.height = mode->vdisplay; 5135 dst.width = stream->timing.h_addressable; 5136 dst.height = stream->timing.v_addressable; 5137 5138 if (dm_state) { 5139 rmx_type = dm_state->scaling; 5140 if (rmx_type == RMX_ASPECT || rmx_type == RMX_OFF) { 5141 if (src.width * dst.height < 5142 src.height * dst.width) { 5143 /* height needs less upscaling/more downscaling */ 5144 dst.width = src.width * 5145 dst.height / src.height; 5146 } else { 5147 /* width needs less upscaling/more downscaling */ 5148 dst.height = src.height * 5149 dst.width / src.width; 5150 } 5151 } else if (rmx_type == RMX_CENTER) { 5152 dst = src; 5153 } 5154 5155 dst.x = (stream->timing.h_addressable - dst.width) / 2; 5156 dst.y = (stream->timing.v_addressable - dst.height) / 2; 5157 5158 if (dm_state->underscan_enable) { 5159 dst.x += dm_state->underscan_hborder / 2; 5160 dst.y += dm_state->underscan_vborder / 2; 5161 dst.width -= dm_state->underscan_hborder; 5162 dst.height -= dm_state->underscan_vborder; 5163 } 5164 } 5165 5166 stream->src = src; 5167 stream->dst = dst; 5168 5169 DRM_DEBUG_KMS("Destination Rectangle x:%d y:%d width:%d height:%d\n", 5170 dst.x, dst.y, dst.width, dst.height); 5171 5172 } 5173 5174 static enum dc_color_depth 5175 convert_color_depth_from_display_info(const struct drm_connector *connector, 5176 bool is_y420, int requested_bpc) 5177 { 5178 u8 bpc; 5179 5180 if (is_y420) { 5181 bpc = 8; 5182 5183 /* Cap display bpc based on HDMI 2.0 HF-VSDB */ 5184 if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_48) 5185 bpc = 16; 5186 else if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_36) 5187 bpc = 12; 5188 else if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_30) 5189 bpc = 10; 5190 } else { 5191 bpc = (uint8_t)connector->display_info.bpc; 5192 /* Assume 8 bpc by default if no bpc is specified. */ 5193 bpc = bpc ? bpc : 8; 5194 } 5195 5196 if (requested_bpc > 0) { 5197 /* 5198 * Cap display bpc based on the user requested value. 5199 * 5200 * The value for state->max_bpc may not correctly updated 5201 * depending on when the connector gets added to the state 5202 * or if this was called outside of atomic check, so it 5203 * can't be used directly. 5204 */ 5205 bpc = min_t(u8, bpc, requested_bpc); 5206 5207 /* Round down to the nearest even number. */ 5208 bpc = bpc - (bpc & 1); 5209 } 5210 5211 switch (bpc) { 5212 case 0: 5213 /* 5214 * Temporary Work around, DRM doesn't parse color depth for 5215 * EDID revision before 1.4 5216 * TODO: Fix edid parsing 5217 */ 5218 return COLOR_DEPTH_888; 5219 case 6: 5220 return COLOR_DEPTH_666; 5221 case 8: 5222 return COLOR_DEPTH_888; 5223 case 10: 5224 return COLOR_DEPTH_101010; 5225 case 12: 5226 return COLOR_DEPTH_121212; 5227 case 14: 5228 return COLOR_DEPTH_141414; 5229 case 16: 5230 return COLOR_DEPTH_161616; 5231 default: 5232 return COLOR_DEPTH_UNDEFINED; 5233 } 5234 } 5235 5236 static enum dc_aspect_ratio 5237 get_aspect_ratio(const struct drm_display_mode *mode_in) 5238 { 5239 /* 1-1 mapping, since both enums follow the HDMI spec. */ 5240 return (enum dc_aspect_ratio) mode_in->picture_aspect_ratio; 5241 } 5242 5243 static enum dc_color_space 5244 get_output_color_space(const struct dc_crtc_timing *dc_crtc_timing) 5245 { 5246 enum dc_color_space color_space = COLOR_SPACE_SRGB; 5247 5248 switch (dc_crtc_timing->pixel_encoding) { 5249 case PIXEL_ENCODING_YCBCR422: 5250 case PIXEL_ENCODING_YCBCR444: 5251 case PIXEL_ENCODING_YCBCR420: 5252 { 5253 /* 5254 * 27030khz is the separation point between HDTV and SDTV 5255 * according to HDMI spec, we use YCbCr709 and YCbCr601 5256 * respectively 5257 */ 5258 if (dc_crtc_timing->pix_clk_100hz > 270300) { 5259 if (dc_crtc_timing->flags.Y_ONLY) 5260 color_space = 5261 COLOR_SPACE_YCBCR709_LIMITED; 5262 else 5263 color_space = COLOR_SPACE_YCBCR709; 5264 } else { 5265 if (dc_crtc_timing->flags.Y_ONLY) 5266 color_space = 5267 COLOR_SPACE_YCBCR601_LIMITED; 5268 else 5269 color_space = COLOR_SPACE_YCBCR601; 5270 } 5271 5272 } 5273 break; 5274 case PIXEL_ENCODING_RGB: 5275 color_space = COLOR_SPACE_SRGB; 5276 break; 5277 5278 default: 5279 WARN_ON(1); 5280 break; 5281 } 5282 5283 return color_space; 5284 } 5285 5286 static bool adjust_colour_depth_from_display_info( 5287 struct dc_crtc_timing *timing_out, 5288 const struct drm_display_info *info) 5289 { 5290 enum dc_color_depth depth = timing_out->display_color_depth; 5291 int normalized_clk; 5292 5293 do { 5294 normalized_clk = timing_out->pix_clk_100hz / 10; 5295 /* YCbCr 4:2:0 requires additional adjustment of 1/2 */ 5296 if (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420) 5297 normalized_clk /= 2; 5298 /* Adjusting pix clock following on HDMI spec based on colour depth */ 5299 switch (depth) { 5300 case COLOR_DEPTH_888: 5301 break; 5302 case COLOR_DEPTH_101010: 5303 normalized_clk = (normalized_clk * 30) / 24; 5304 break; 5305 case COLOR_DEPTH_121212: 5306 normalized_clk = (normalized_clk * 36) / 24; 5307 break; 5308 case COLOR_DEPTH_161616: 5309 normalized_clk = (normalized_clk * 48) / 24; 5310 break; 5311 default: 5312 /* The above depths are the only ones valid for HDMI. */ 5313 return false; 5314 } 5315 if (normalized_clk <= info->max_tmds_clock) { 5316 timing_out->display_color_depth = depth; 5317 return true; 5318 } 5319 } while (--depth > COLOR_DEPTH_666); 5320 return false; 5321 } 5322 5323 static void fill_stream_properties_from_drm_display_mode( 5324 struct dc_stream_state *stream, 5325 const struct drm_display_mode *mode_in, 5326 const struct drm_connector *connector, 5327 const struct drm_connector_state *connector_state, 5328 const struct dc_stream_state *old_stream, 5329 int requested_bpc) 5330 { 5331 struct dc_crtc_timing *timing_out = &stream->timing; 5332 const struct drm_display_info *info = &connector->display_info; 5333 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector); 5334 struct hdmi_vendor_infoframe hv_frame; 5335 struct hdmi_avi_infoframe avi_frame; 5336 5337 memset(&hv_frame, 0, sizeof(hv_frame)); 5338 memset(&avi_frame, 0, sizeof(avi_frame)); 5339 5340 timing_out->h_border_left = 0; 5341 timing_out->h_border_right = 0; 5342 timing_out->v_border_top = 0; 5343 timing_out->v_border_bottom = 0; 5344 /* TODO: un-hardcode */ 5345 if (drm_mode_is_420_only(info, mode_in) 5346 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) 5347 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420; 5348 else if (drm_mode_is_420_also(info, mode_in) 5349 && aconnector->force_yuv420_output) 5350 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420; 5351 else if ((connector->display_info.color_formats & DRM_COLOR_FORMAT_YCBCR444) 5352 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) 5353 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR444; 5354 else 5355 timing_out->pixel_encoding = PIXEL_ENCODING_RGB; 5356 5357 timing_out->timing_3d_format = TIMING_3D_FORMAT_NONE; 5358 timing_out->display_color_depth = convert_color_depth_from_display_info( 5359 connector, 5360 (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420), 5361 requested_bpc); 5362 timing_out->scan_type = SCANNING_TYPE_NODATA; 5363 timing_out->hdmi_vic = 0; 5364 5365 if (old_stream) { 5366 timing_out->vic = old_stream->timing.vic; 5367 timing_out->flags.HSYNC_POSITIVE_POLARITY = old_stream->timing.flags.HSYNC_POSITIVE_POLARITY; 5368 timing_out->flags.VSYNC_POSITIVE_POLARITY = old_stream->timing.flags.VSYNC_POSITIVE_POLARITY; 5369 } else { 5370 timing_out->vic = drm_match_cea_mode(mode_in); 5371 if (mode_in->flags & DRM_MODE_FLAG_PHSYNC) 5372 timing_out->flags.HSYNC_POSITIVE_POLARITY = 1; 5373 if (mode_in->flags & DRM_MODE_FLAG_PVSYNC) 5374 timing_out->flags.VSYNC_POSITIVE_POLARITY = 1; 5375 } 5376 5377 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) { 5378 drm_hdmi_avi_infoframe_from_display_mode(&avi_frame, (struct drm_connector *)connector, mode_in); 5379 timing_out->vic = avi_frame.video_code; 5380 drm_hdmi_vendor_infoframe_from_display_mode(&hv_frame, (struct drm_connector *)connector, mode_in); 5381 timing_out->hdmi_vic = hv_frame.vic; 5382 } 5383 5384 if (is_freesync_video_mode(mode_in, aconnector)) { 5385 timing_out->h_addressable = mode_in->hdisplay; 5386 timing_out->h_total = mode_in->htotal; 5387 timing_out->h_sync_width = mode_in->hsync_end - mode_in->hsync_start; 5388 timing_out->h_front_porch = mode_in->hsync_start - mode_in->hdisplay; 5389 timing_out->v_total = mode_in->vtotal; 5390 timing_out->v_addressable = mode_in->vdisplay; 5391 timing_out->v_front_porch = mode_in->vsync_start - mode_in->vdisplay; 5392 timing_out->v_sync_width = mode_in->vsync_end - mode_in->vsync_start; 5393 timing_out->pix_clk_100hz = mode_in->clock * 10; 5394 } else { 5395 timing_out->h_addressable = mode_in->crtc_hdisplay; 5396 timing_out->h_total = mode_in->crtc_htotal; 5397 timing_out->h_sync_width = mode_in->crtc_hsync_end - mode_in->crtc_hsync_start; 5398 timing_out->h_front_porch = mode_in->crtc_hsync_start - mode_in->crtc_hdisplay; 5399 timing_out->v_total = mode_in->crtc_vtotal; 5400 timing_out->v_addressable = mode_in->crtc_vdisplay; 5401 timing_out->v_front_porch = mode_in->crtc_vsync_start - mode_in->crtc_vdisplay; 5402 timing_out->v_sync_width = mode_in->crtc_vsync_end - mode_in->crtc_vsync_start; 5403 timing_out->pix_clk_100hz = mode_in->crtc_clock * 10; 5404 } 5405 5406 timing_out->aspect_ratio = get_aspect_ratio(mode_in); 5407 5408 stream->out_transfer_func->type = TF_TYPE_PREDEFINED; 5409 stream->out_transfer_func->tf = TRANSFER_FUNCTION_SRGB; 5410 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) { 5411 if (!adjust_colour_depth_from_display_info(timing_out, info) && 5412 drm_mode_is_420_also(info, mode_in) && 5413 timing_out->pixel_encoding != PIXEL_ENCODING_YCBCR420) { 5414 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420; 5415 adjust_colour_depth_from_display_info(timing_out, info); 5416 } 5417 } 5418 5419 stream->output_color_space = get_output_color_space(timing_out); 5420 } 5421 5422 static void fill_audio_info(struct audio_info *audio_info, 5423 const struct drm_connector *drm_connector, 5424 const struct dc_sink *dc_sink) 5425 { 5426 int i = 0; 5427 int cea_revision = 0; 5428 const struct dc_edid_caps *edid_caps = &dc_sink->edid_caps; 5429 5430 audio_info->manufacture_id = edid_caps->manufacturer_id; 5431 audio_info->product_id = edid_caps->product_id; 5432 5433 cea_revision = drm_connector->display_info.cea_rev; 5434 5435 strscpy(audio_info->display_name, 5436 edid_caps->display_name, 5437 AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS); 5438 5439 if (cea_revision >= 3) { 5440 audio_info->mode_count = edid_caps->audio_mode_count; 5441 5442 for (i = 0; i < audio_info->mode_count; ++i) { 5443 audio_info->modes[i].format_code = 5444 (enum audio_format_code) 5445 (edid_caps->audio_modes[i].format_code); 5446 audio_info->modes[i].channel_count = 5447 edid_caps->audio_modes[i].channel_count; 5448 audio_info->modes[i].sample_rates.all = 5449 edid_caps->audio_modes[i].sample_rate; 5450 audio_info->modes[i].sample_size = 5451 edid_caps->audio_modes[i].sample_size; 5452 } 5453 } 5454 5455 audio_info->flags.all = edid_caps->speaker_flags; 5456 5457 /* TODO: We only check for the progressive mode, check for interlace mode too */ 5458 if (drm_connector->latency_present[0]) { 5459 audio_info->video_latency = drm_connector->video_latency[0]; 5460 audio_info->audio_latency = drm_connector->audio_latency[0]; 5461 } 5462 5463 /* TODO: For DP, video and audio latency should be calculated from DPCD caps */ 5464 5465 } 5466 5467 static void 5468 copy_crtc_timing_for_drm_display_mode(const struct drm_display_mode *src_mode, 5469 struct drm_display_mode *dst_mode) 5470 { 5471 dst_mode->crtc_hdisplay = src_mode->crtc_hdisplay; 5472 dst_mode->crtc_vdisplay = src_mode->crtc_vdisplay; 5473 dst_mode->crtc_clock = src_mode->crtc_clock; 5474 dst_mode->crtc_hblank_start = src_mode->crtc_hblank_start; 5475 dst_mode->crtc_hblank_end = src_mode->crtc_hblank_end; 5476 dst_mode->crtc_hsync_start = src_mode->crtc_hsync_start; 5477 dst_mode->crtc_hsync_end = src_mode->crtc_hsync_end; 5478 dst_mode->crtc_htotal = src_mode->crtc_htotal; 5479 dst_mode->crtc_hskew = src_mode->crtc_hskew; 5480 dst_mode->crtc_vblank_start = src_mode->crtc_vblank_start; 5481 dst_mode->crtc_vblank_end = src_mode->crtc_vblank_end; 5482 dst_mode->crtc_vsync_start = src_mode->crtc_vsync_start; 5483 dst_mode->crtc_vsync_end = src_mode->crtc_vsync_end; 5484 dst_mode->crtc_vtotal = src_mode->crtc_vtotal; 5485 } 5486 5487 static void 5488 decide_crtc_timing_for_drm_display_mode(struct drm_display_mode *drm_mode, 5489 const struct drm_display_mode *native_mode, 5490 bool scale_enabled) 5491 { 5492 if (scale_enabled) { 5493 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode); 5494 } else if (native_mode->clock == drm_mode->clock && 5495 native_mode->htotal == drm_mode->htotal && 5496 native_mode->vtotal == drm_mode->vtotal) { 5497 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode); 5498 } else { 5499 /* no scaling nor amdgpu inserted, no need to patch */ 5500 } 5501 } 5502 5503 static struct dc_sink * 5504 create_fake_sink(struct amdgpu_dm_connector *aconnector) 5505 { 5506 struct dc_sink_init_data sink_init_data = { 0 }; 5507 struct dc_sink *sink = NULL; 5508 5509 sink_init_data.link = aconnector->dc_link; 5510 sink_init_data.sink_signal = aconnector->dc_link->connector_signal; 5511 5512 sink = dc_sink_create(&sink_init_data); 5513 if (!sink) { 5514 DRM_ERROR("Failed to create sink!\n"); 5515 return NULL; 5516 } 5517 sink->sink_signal = SIGNAL_TYPE_VIRTUAL; 5518 5519 return sink; 5520 } 5521 5522 static void set_multisync_trigger_params( 5523 struct dc_stream_state *stream) 5524 { 5525 struct dc_stream_state *master = NULL; 5526 5527 if (stream->triggered_crtc_reset.enabled) { 5528 master = stream->triggered_crtc_reset.event_source; 5529 stream->triggered_crtc_reset.event = 5530 master->timing.flags.VSYNC_POSITIVE_POLARITY ? 5531 CRTC_EVENT_VSYNC_RISING : CRTC_EVENT_VSYNC_FALLING; 5532 stream->triggered_crtc_reset.delay = TRIGGER_DELAY_NEXT_PIXEL; 5533 } 5534 } 5535 5536 static void set_master_stream(struct dc_stream_state *stream_set[], 5537 int stream_count) 5538 { 5539 int j, highest_rfr = 0, master_stream = 0; 5540 5541 for (j = 0; j < stream_count; j++) { 5542 if (stream_set[j] && stream_set[j]->triggered_crtc_reset.enabled) { 5543 int refresh_rate = 0; 5544 5545 refresh_rate = (stream_set[j]->timing.pix_clk_100hz*100)/ 5546 (stream_set[j]->timing.h_total*stream_set[j]->timing.v_total); 5547 if (refresh_rate > highest_rfr) { 5548 highest_rfr = refresh_rate; 5549 master_stream = j; 5550 } 5551 } 5552 } 5553 for (j = 0; j < stream_count; j++) { 5554 if (stream_set[j]) 5555 stream_set[j]->triggered_crtc_reset.event_source = stream_set[master_stream]; 5556 } 5557 } 5558 5559 static void dm_enable_per_frame_crtc_master_sync(struct dc_state *context) 5560 { 5561 int i = 0; 5562 struct dc_stream_state *stream; 5563 5564 if (context->stream_count < 2) 5565 return; 5566 for (i = 0; i < context->stream_count ; i++) { 5567 if (!context->streams[i]) 5568 continue; 5569 /* 5570 * TODO: add a function to read AMD VSDB bits and set 5571 * crtc_sync_master.multi_sync_enabled flag 5572 * For now it's set to false 5573 */ 5574 } 5575 5576 set_master_stream(context->streams, context->stream_count); 5577 5578 for (i = 0; i < context->stream_count ; i++) { 5579 stream = context->streams[i]; 5580 5581 if (!stream) 5582 continue; 5583 5584 set_multisync_trigger_params(stream); 5585 } 5586 } 5587 5588 /** 5589 * DOC: FreeSync Video 5590 * 5591 * When a userspace application wants to play a video, the content follows a 5592 * standard format definition that usually specifies the FPS for that format. 5593 * The below list illustrates some video format and the expected FPS, 5594 * respectively: 5595 * 5596 * - TV/NTSC (23.976 FPS) 5597 * - Cinema (24 FPS) 5598 * - TV/PAL (25 FPS) 5599 * - TV/NTSC (29.97 FPS) 5600 * - TV/NTSC (30 FPS) 5601 * - Cinema HFR (48 FPS) 5602 * - TV/PAL (50 FPS) 5603 * - Commonly used (60 FPS) 5604 * - Multiples of 24 (48,72,96 FPS) 5605 * 5606 * The list of standards video format is not huge and can be added to the 5607 * connector modeset list beforehand. With that, userspace can leverage 5608 * FreeSync to extends the front porch in order to attain the target refresh 5609 * rate. Such a switch will happen seamlessly, without screen blanking or 5610 * reprogramming of the output in any other way. If the userspace requests a 5611 * modesetting change compatible with FreeSync modes that only differ in the 5612 * refresh rate, DC will skip the full update and avoid blink during the 5613 * transition. For example, the video player can change the modesetting from 5614 * 60Hz to 30Hz for playing TV/NTSC content when it goes full screen without 5615 * causing any display blink. This same concept can be applied to a mode 5616 * setting change. 5617 */ 5618 static struct drm_display_mode * 5619 get_highest_refresh_rate_mode(struct amdgpu_dm_connector *aconnector, 5620 bool use_probed_modes) 5621 { 5622 struct drm_display_mode *m, *m_pref = NULL; 5623 u16 current_refresh, highest_refresh; 5624 struct list_head *list_head = use_probed_modes ? 5625 &aconnector->base.probed_modes : 5626 &aconnector->base.modes; 5627 5628 if (aconnector->freesync_vid_base.clock != 0) 5629 return &aconnector->freesync_vid_base; 5630 5631 /* Find the preferred mode */ 5632 list_for_each_entry(m, list_head, head) { 5633 if (m->type & DRM_MODE_TYPE_PREFERRED) { 5634 m_pref = m; 5635 break; 5636 } 5637 } 5638 5639 if (!m_pref) { 5640 /* Probably an EDID with no preferred mode. Fallback to first entry */ 5641 m_pref = list_first_entry_or_null( 5642 &aconnector->base.modes, struct drm_display_mode, head); 5643 if (!m_pref) { 5644 DRM_DEBUG_DRIVER("No preferred mode found in EDID\n"); 5645 return NULL; 5646 } 5647 } 5648 5649 highest_refresh = drm_mode_vrefresh(m_pref); 5650 5651 /* 5652 * Find the mode with highest refresh rate with same resolution. 5653 * For some monitors, preferred mode is not the mode with highest 5654 * supported refresh rate. 5655 */ 5656 list_for_each_entry(m, list_head, head) { 5657 current_refresh = drm_mode_vrefresh(m); 5658 5659 if (m->hdisplay == m_pref->hdisplay && 5660 m->vdisplay == m_pref->vdisplay && 5661 highest_refresh < current_refresh) { 5662 highest_refresh = current_refresh; 5663 m_pref = m; 5664 } 5665 } 5666 5667 drm_mode_copy(&aconnector->freesync_vid_base, m_pref); 5668 return m_pref; 5669 } 5670 5671 static bool is_freesync_video_mode(const struct drm_display_mode *mode, 5672 struct amdgpu_dm_connector *aconnector) 5673 { 5674 struct drm_display_mode *high_mode; 5675 int timing_diff; 5676 5677 high_mode = get_highest_refresh_rate_mode(aconnector, false); 5678 if (!high_mode || !mode) 5679 return false; 5680 5681 timing_diff = high_mode->vtotal - mode->vtotal; 5682 5683 if (high_mode->clock == 0 || high_mode->clock != mode->clock || 5684 high_mode->hdisplay != mode->hdisplay || 5685 high_mode->vdisplay != mode->vdisplay || 5686 high_mode->hsync_start != mode->hsync_start || 5687 high_mode->hsync_end != mode->hsync_end || 5688 high_mode->htotal != mode->htotal || 5689 high_mode->hskew != mode->hskew || 5690 high_mode->vscan != mode->vscan || 5691 high_mode->vsync_start - mode->vsync_start != timing_diff || 5692 high_mode->vsync_end - mode->vsync_end != timing_diff) 5693 return false; 5694 else 5695 return true; 5696 } 5697 5698 #if defined(CONFIG_DRM_AMD_DC_DCN) 5699 static void update_dsc_caps(struct amdgpu_dm_connector *aconnector, 5700 struct dc_sink *sink, struct dc_stream_state *stream, 5701 struct dsc_dec_dpcd_caps *dsc_caps) 5702 { 5703 stream->timing.flags.DSC = 0; 5704 dsc_caps->is_dsc_supported = false; 5705 5706 if (aconnector->dc_link && (sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT || 5707 sink->sink_signal == SIGNAL_TYPE_EDP)) { 5708 if (sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_NONE || 5709 sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_DP_HDMI_CONVERTER) 5710 dc_dsc_parse_dsc_dpcd(aconnector->dc_link->ctx->dc, 5711 aconnector->dc_link->dpcd_caps.dsc_caps.dsc_basic_caps.raw, 5712 aconnector->dc_link->dpcd_caps.dsc_caps.dsc_branch_decoder_caps.raw, 5713 dsc_caps); 5714 } 5715 } 5716 5717 5718 static void apply_dsc_policy_for_edp(struct amdgpu_dm_connector *aconnector, 5719 struct dc_sink *sink, struct dc_stream_state *stream, 5720 struct dsc_dec_dpcd_caps *dsc_caps, 5721 uint32_t max_dsc_target_bpp_limit_override) 5722 { 5723 const struct dc_link_settings *verified_link_cap = NULL; 5724 u32 link_bw_in_kbps; 5725 u32 edp_min_bpp_x16, edp_max_bpp_x16; 5726 struct dc *dc = sink->ctx->dc; 5727 struct dc_dsc_bw_range bw_range = {0}; 5728 struct dc_dsc_config dsc_cfg = {0}; 5729 5730 verified_link_cap = dc_link_get_link_cap(stream->link); 5731 link_bw_in_kbps = dc_link_bandwidth_kbps(stream->link, verified_link_cap); 5732 edp_min_bpp_x16 = 8 * 16; 5733 edp_max_bpp_x16 = 8 * 16; 5734 5735 if (edp_max_bpp_x16 > dsc_caps->edp_max_bits_per_pixel) 5736 edp_max_bpp_x16 = dsc_caps->edp_max_bits_per_pixel; 5737 5738 if (edp_max_bpp_x16 < edp_min_bpp_x16) 5739 edp_min_bpp_x16 = edp_max_bpp_x16; 5740 5741 if (dc_dsc_compute_bandwidth_range(dc->res_pool->dscs[0], 5742 dc->debug.dsc_min_slice_height_override, 5743 edp_min_bpp_x16, edp_max_bpp_x16, 5744 dsc_caps, 5745 &stream->timing, 5746 &bw_range)) { 5747 5748 if (bw_range.max_kbps < link_bw_in_kbps) { 5749 if (dc_dsc_compute_config(dc->res_pool->dscs[0], 5750 dsc_caps, 5751 dc->debug.dsc_min_slice_height_override, 5752 max_dsc_target_bpp_limit_override, 5753 0, 5754 &stream->timing, 5755 &dsc_cfg)) { 5756 stream->timing.dsc_cfg = dsc_cfg; 5757 stream->timing.flags.DSC = 1; 5758 stream->timing.dsc_cfg.bits_per_pixel = edp_max_bpp_x16; 5759 } 5760 return; 5761 } 5762 } 5763 5764 if (dc_dsc_compute_config(dc->res_pool->dscs[0], 5765 dsc_caps, 5766 dc->debug.dsc_min_slice_height_override, 5767 max_dsc_target_bpp_limit_override, 5768 link_bw_in_kbps, 5769 &stream->timing, 5770 &dsc_cfg)) { 5771 stream->timing.dsc_cfg = dsc_cfg; 5772 stream->timing.flags.DSC = 1; 5773 } 5774 } 5775 5776 5777 static void apply_dsc_policy_for_stream(struct amdgpu_dm_connector *aconnector, 5778 struct dc_sink *sink, struct dc_stream_state *stream, 5779 struct dsc_dec_dpcd_caps *dsc_caps) 5780 { 5781 struct drm_connector *drm_connector = &aconnector->base; 5782 u32 link_bandwidth_kbps; 5783 struct dc *dc = sink->ctx->dc; 5784 u32 max_supported_bw_in_kbps, timing_bw_in_kbps; 5785 u32 dsc_max_supported_bw_in_kbps; 5786 u32 max_dsc_target_bpp_limit_override = 5787 drm_connector->display_info.max_dsc_bpp; 5788 5789 link_bandwidth_kbps = dc_link_bandwidth_kbps(aconnector->dc_link, 5790 dc_link_get_link_cap(aconnector->dc_link)); 5791 5792 /* Set DSC policy according to dsc_clock_en */ 5793 dc_dsc_policy_set_enable_dsc_when_not_needed( 5794 aconnector->dsc_settings.dsc_force_enable == DSC_CLK_FORCE_ENABLE); 5795 5796 if (aconnector->dc_link && sink->sink_signal == SIGNAL_TYPE_EDP && 5797 !aconnector->dc_link->panel_config.dsc.disable_dsc_edp && 5798 dc->caps.edp_dsc_support && aconnector->dsc_settings.dsc_force_enable != DSC_CLK_FORCE_DISABLE) { 5799 5800 apply_dsc_policy_for_edp(aconnector, sink, stream, dsc_caps, max_dsc_target_bpp_limit_override); 5801 5802 } else if (aconnector->dc_link && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT) { 5803 if (sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_NONE) { 5804 if (dc_dsc_compute_config(aconnector->dc_link->ctx->dc->res_pool->dscs[0], 5805 dsc_caps, 5806 aconnector->dc_link->ctx->dc->debug.dsc_min_slice_height_override, 5807 max_dsc_target_bpp_limit_override, 5808 link_bandwidth_kbps, 5809 &stream->timing, 5810 &stream->timing.dsc_cfg)) { 5811 stream->timing.flags.DSC = 1; 5812 DRM_DEBUG_DRIVER("%s: [%s] DSC is selected from SST RX\n", __func__, drm_connector->name); 5813 } 5814 } else if (sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_DP_HDMI_CONVERTER) { 5815 timing_bw_in_kbps = dc_bandwidth_in_kbps_from_timing(&stream->timing); 5816 max_supported_bw_in_kbps = link_bandwidth_kbps; 5817 dsc_max_supported_bw_in_kbps = link_bandwidth_kbps; 5818 5819 if (timing_bw_in_kbps > max_supported_bw_in_kbps && 5820 max_supported_bw_in_kbps > 0 && 5821 dsc_max_supported_bw_in_kbps > 0) 5822 if (dc_dsc_compute_config(aconnector->dc_link->ctx->dc->res_pool->dscs[0], 5823 dsc_caps, 5824 aconnector->dc_link->ctx->dc->debug.dsc_min_slice_height_override, 5825 max_dsc_target_bpp_limit_override, 5826 dsc_max_supported_bw_in_kbps, 5827 &stream->timing, 5828 &stream->timing.dsc_cfg)) { 5829 stream->timing.flags.DSC = 1; 5830 DRM_DEBUG_DRIVER("%s: [%s] DSC is selected from DP-HDMI PCON\n", 5831 __func__, drm_connector->name); 5832 } 5833 } 5834 } 5835 5836 /* Overwrite the stream flag if DSC is enabled through debugfs */ 5837 if (aconnector->dsc_settings.dsc_force_enable == DSC_CLK_FORCE_ENABLE) 5838 stream->timing.flags.DSC = 1; 5839 5840 if (stream->timing.flags.DSC && aconnector->dsc_settings.dsc_num_slices_h) 5841 stream->timing.dsc_cfg.num_slices_h = aconnector->dsc_settings.dsc_num_slices_h; 5842 5843 if (stream->timing.flags.DSC && aconnector->dsc_settings.dsc_num_slices_v) 5844 stream->timing.dsc_cfg.num_slices_v = aconnector->dsc_settings.dsc_num_slices_v; 5845 5846 if (stream->timing.flags.DSC && aconnector->dsc_settings.dsc_bits_per_pixel) 5847 stream->timing.dsc_cfg.bits_per_pixel = aconnector->dsc_settings.dsc_bits_per_pixel; 5848 } 5849 #endif /* CONFIG_DRM_AMD_DC_DCN */ 5850 5851 static struct dc_stream_state * 5852 create_stream_for_sink(struct amdgpu_dm_connector *aconnector, 5853 const struct drm_display_mode *drm_mode, 5854 const struct dm_connector_state *dm_state, 5855 const struct dc_stream_state *old_stream, 5856 int requested_bpc) 5857 { 5858 struct drm_display_mode *preferred_mode = NULL; 5859 struct drm_connector *drm_connector; 5860 const struct drm_connector_state *con_state = 5861 dm_state ? &dm_state->base : NULL; 5862 struct dc_stream_state *stream = NULL; 5863 struct drm_display_mode mode = *drm_mode; 5864 struct drm_display_mode saved_mode; 5865 struct drm_display_mode *freesync_mode = NULL; 5866 bool native_mode_found = false; 5867 bool recalculate_timing = false; 5868 bool scale = dm_state ? (dm_state->scaling != RMX_OFF) : false; 5869 int mode_refresh; 5870 int preferred_refresh = 0; 5871 #if defined(CONFIG_DRM_AMD_DC_DCN) 5872 struct dsc_dec_dpcd_caps dsc_caps; 5873 #endif 5874 5875 struct dc_sink *sink = NULL; 5876 5877 memset(&saved_mode, 0, sizeof(saved_mode)); 5878 5879 if (aconnector == NULL) { 5880 DRM_ERROR("aconnector is NULL!\n"); 5881 return stream; 5882 } 5883 5884 drm_connector = &aconnector->base; 5885 5886 if (!aconnector->dc_sink) { 5887 sink = create_fake_sink(aconnector); 5888 if (!sink) 5889 return stream; 5890 } else { 5891 sink = aconnector->dc_sink; 5892 dc_sink_retain(sink); 5893 } 5894 5895 stream = dc_create_stream_for_sink(sink); 5896 5897 if (stream == NULL) { 5898 DRM_ERROR("Failed to create stream for sink!\n"); 5899 goto finish; 5900 } 5901 5902 stream->dm_stream_context = aconnector; 5903 5904 stream->timing.flags.LTE_340MCSC_SCRAMBLE = 5905 drm_connector->display_info.hdmi.scdc.scrambling.low_rates; 5906 5907 list_for_each_entry(preferred_mode, &aconnector->base.modes, head) { 5908 /* Search for preferred mode */ 5909 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED) { 5910 native_mode_found = true; 5911 break; 5912 } 5913 } 5914 if (!native_mode_found) 5915 preferred_mode = list_first_entry_or_null( 5916 &aconnector->base.modes, 5917 struct drm_display_mode, 5918 head); 5919 5920 mode_refresh = drm_mode_vrefresh(&mode); 5921 5922 if (preferred_mode == NULL) { 5923 /* 5924 * This may not be an error, the use case is when we have no 5925 * usermode calls to reset and set mode upon hotplug. In this 5926 * case, we call set mode ourselves to restore the previous mode 5927 * and the modelist may not be filled in time. 5928 */ 5929 DRM_DEBUG_DRIVER("No preferred mode found\n"); 5930 } else { 5931 recalculate_timing = is_freesync_video_mode(&mode, aconnector); 5932 if (recalculate_timing) { 5933 freesync_mode = get_highest_refresh_rate_mode(aconnector, false); 5934 drm_mode_copy(&saved_mode, &mode); 5935 drm_mode_copy(&mode, freesync_mode); 5936 } else { 5937 decide_crtc_timing_for_drm_display_mode( 5938 &mode, preferred_mode, scale); 5939 5940 preferred_refresh = drm_mode_vrefresh(preferred_mode); 5941 } 5942 } 5943 5944 if (recalculate_timing) 5945 drm_mode_set_crtcinfo(&saved_mode, 0); 5946 else if (!dm_state) 5947 drm_mode_set_crtcinfo(&mode, 0); 5948 5949 /* 5950 * If scaling is enabled and refresh rate didn't change 5951 * we copy the vic and polarities of the old timings 5952 */ 5953 if (!scale || mode_refresh != preferred_refresh) 5954 fill_stream_properties_from_drm_display_mode( 5955 stream, &mode, &aconnector->base, con_state, NULL, 5956 requested_bpc); 5957 else 5958 fill_stream_properties_from_drm_display_mode( 5959 stream, &mode, &aconnector->base, con_state, old_stream, 5960 requested_bpc); 5961 5962 if (aconnector->timing_changed) { 5963 DC_LOG_DEBUG("%s: overriding timing for automated test, bpc %d, changing to %d\n", 5964 __func__, 5965 stream->timing.display_color_depth, 5966 aconnector->timing_requested->display_color_depth); 5967 stream->timing = *aconnector->timing_requested; 5968 } 5969 5970 #if defined(CONFIG_DRM_AMD_DC_DCN) 5971 /* SST DSC determination policy */ 5972 update_dsc_caps(aconnector, sink, stream, &dsc_caps); 5973 if (aconnector->dsc_settings.dsc_force_enable != DSC_CLK_FORCE_DISABLE && dsc_caps.is_dsc_supported) 5974 apply_dsc_policy_for_stream(aconnector, sink, stream, &dsc_caps); 5975 #endif 5976 5977 update_stream_scaling_settings(&mode, dm_state, stream); 5978 5979 fill_audio_info( 5980 &stream->audio_info, 5981 drm_connector, 5982 sink); 5983 5984 update_stream_signal(stream, sink); 5985 5986 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) 5987 mod_build_hf_vsif_infopacket(stream, &stream->vsp_infopacket); 5988 5989 if (stream->link->psr_settings.psr_feature_enabled) { 5990 // 5991 // should decide stream support vsc sdp colorimetry capability 5992 // before building vsc info packet 5993 // 5994 stream->use_vsc_sdp_for_colorimetry = false; 5995 if (aconnector->dc_sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST) { 5996 stream->use_vsc_sdp_for_colorimetry = 5997 aconnector->dc_sink->is_vsc_sdp_colorimetry_supported; 5998 } else { 5999 if (stream->link->dpcd_caps.dprx_feature.bits.VSC_SDP_COLORIMETRY_SUPPORTED) 6000 stream->use_vsc_sdp_for_colorimetry = true; 6001 } 6002 mod_build_vsc_infopacket(stream, &stream->vsc_infopacket, stream->output_color_space); 6003 aconnector->psr_skip_count = AMDGPU_DM_PSR_ENTRY_DELAY; 6004 6005 } 6006 finish: 6007 dc_sink_release(sink); 6008 6009 return stream; 6010 } 6011 6012 static enum drm_connector_status 6013 amdgpu_dm_connector_detect(struct drm_connector *connector, bool force) 6014 { 6015 bool connected; 6016 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector); 6017 6018 /* 6019 * Notes: 6020 * 1. This interface is NOT called in context of HPD irq. 6021 * 2. This interface *is called* in context of user-mode ioctl. Which 6022 * makes it a bad place for *any* MST-related activity. 6023 */ 6024 6025 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED && 6026 !aconnector->fake_enable) 6027 connected = (aconnector->dc_sink != NULL); 6028 else 6029 connected = (aconnector->base.force == DRM_FORCE_ON || 6030 aconnector->base.force == DRM_FORCE_ON_DIGITAL); 6031 6032 update_subconnector_property(aconnector); 6033 6034 return (connected ? connector_status_connected : 6035 connector_status_disconnected); 6036 } 6037 6038 int amdgpu_dm_connector_atomic_set_property(struct drm_connector *connector, 6039 struct drm_connector_state *connector_state, 6040 struct drm_property *property, 6041 uint64_t val) 6042 { 6043 struct drm_device *dev = connector->dev; 6044 struct amdgpu_device *adev = drm_to_adev(dev); 6045 struct dm_connector_state *dm_old_state = 6046 to_dm_connector_state(connector->state); 6047 struct dm_connector_state *dm_new_state = 6048 to_dm_connector_state(connector_state); 6049 6050 int ret = -EINVAL; 6051 6052 if (property == dev->mode_config.scaling_mode_property) { 6053 enum amdgpu_rmx_type rmx_type; 6054 6055 switch (val) { 6056 case DRM_MODE_SCALE_CENTER: 6057 rmx_type = RMX_CENTER; 6058 break; 6059 case DRM_MODE_SCALE_ASPECT: 6060 rmx_type = RMX_ASPECT; 6061 break; 6062 case DRM_MODE_SCALE_FULLSCREEN: 6063 rmx_type = RMX_FULL; 6064 break; 6065 case DRM_MODE_SCALE_NONE: 6066 default: 6067 rmx_type = RMX_OFF; 6068 break; 6069 } 6070 6071 if (dm_old_state->scaling == rmx_type) 6072 return 0; 6073 6074 dm_new_state->scaling = rmx_type; 6075 ret = 0; 6076 } else if (property == adev->mode_info.underscan_hborder_property) { 6077 dm_new_state->underscan_hborder = val; 6078 ret = 0; 6079 } else if (property == adev->mode_info.underscan_vborder_property) { 6080 dm_new_state->underscan_vborder = val; 6081 ret = 0; 6082 } else if (property == adev->mode_info.underscan_property) { 6083 dm_new_state->underscan_enable = val; 6084 ret = 0; 6085 } else if (property == adev->mode_info.abm_level_property) { 6086 dm_new_state->abm_level = val; 6087 ret = 0; 6088 } 6089 6090 return ret; 6091 } 6092 6093 int amdgpu_dm_connector_atomic_get_property(struct drm_connector *connector, 6094 const struct drm_connector_state *state, 6095 struct drm_property *property, 6096 uint64_t *val) 6097 { 6098 struct drm_device *dev = connector->dev; 6099 struct amdgpu_device *adev = drm_to_adev(dev); 6100 struct dm_connector_state *dm_state = 6101 to_dm_connector_state(state); 6102 int ret = -EINVAL; 6103 6104 if (property == dev->mode_config.scaling_mode_property) { 6105 switch (dm_state->scaling) { 6106 case RMX_CENTER: 6107 *val = DRM_MODE_SCALE_CENTER; 6108 break; 6109 case RMX_ASPECT: 6110 *val = DRM_MODE_SCALE_ASPECT; 6111 break; 6112 case RMX_FULL: 6113 *val = DRM_MODE_SCALE_FULLSCREEN; 6114 break; 6115 case RMX_OFF: 6116 default: 6117 *val = DRM_MODE_SCALE_NONE; 6118 break; 6119 } 6120 ret = 0; 6121 } else if (property == adev->mode_info.underscan_hborder_property) { 6122 *val = dm_state->underscan_hborder; 6123 ret = 0; 6124 } else if (property == adev->mode_info.underscan_vborder_property) { 6125 *val = dm_state->underscan_vborder; 6126 ret = 0; 6127 } else if (property == adev->mode_info.underscan_property) { 6128 *val = dm_state->underscan_enable; 6129 ret = 0; 6130 } else if (property == adev->mode_info.abm_level_property) { 6131 *val = dm_state->abm_level; 6132 ret = 0; 6133 } 6134 6135 return ret; 6136 } 6137 6138 static void amdgpu_dm_connector_unregister(struct drm_connector *connector) 6139 { 6140 struct amdgpu_dm_connector *amdgpu_dm_connector = to_amdgpu_dm_connector(connector); 6141 6142 drm_dp_aux_unregister(&amdgpu_dm_connector->dm_dp_aux.aux); 6143 } 6144 6145 static void amdgpu_dm_connector_destroy(struct drm_connector *connector) 6146 { 6147 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector); 6148 const struct dc_link *link = aconnector->dc_link; 6149 struct amdgpu_device *adev = drm_to_adev(connector->dev); 6150 struct amdgpu_display_manager *dm = &adev->dm; 6151 int i; 6152 6153 /* 6154 * Call only if mst_mgr was initialized before since it's not done 6155 * for all connector types. 6156 */ 6157 if (aconnector->mst_mgr.dev) 6158 drm_dp_mst_topology_mgr_destroy(&aconnector->mst_mgr); 6159 6160 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\ 6161 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE) 6162 for (i = 0; i < dm->num_of_edps; i++) { 6163 if ((link == dm->backlight_link[i]) && dm->backlight_dev[i]) { 6164 backlight_device_unregister(dm->backlight_dev[i]); 6165 dm->backlight_dev[i] = NULL; 6166 } 6167 } 6168 #endif 6169 6170 if (aconnector->dc_em_sink) 6171 dc_sink_release(aconnector->dc_em_sink); 6172 aconnector->dc_em_sink = NULL; 6173 if (aconnector->dc_sink) 6174 dc_sink_release(aconnector->dc_sink); 6175 aconnector->dc_sink = NULL; 6176 6177 drm_dp_cec_unregister_connector(&aconnector->dm_dp_aux.aux); 6178 drm_connector_unregister(connector); 6179 drm_connector_cleanup(connector); 6180 if (aconnector->i2c) { 6181 i2c_del_adapter(&aconnector->i2c->base); 6182 kfree(aconnector->i2c); 6183 } 6184 kfree(aconnector->dm_dp_aux.aux.name); 6185 6186 kfree(connector); 6187 } 6188 6189 void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector) 6190 { 6191 struct dm_connector_state *state = 6192 to_dm_connector_state(connector->state); 6193 6194 if (connector->state) 6195 __drm_atomic_helper_connector_destroy_state(connector->state); 6196 6197 kfree(state); 6198 6199 state = kzalloc(sizeof(*state), GFP_KERNEL); 6200 6201 if (state) { 6202 state->scaling = RMX_OFF; 6203 state->underscan_enable = false; 6204 state->underscan_hborder = 0; 6205 state->underscan_vborder = 0; 6206 state->base.max_requested_bpc = 8; 6207 state->vcpi_slots = 0; 6208 state->pbn = 0; 6209 6210 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) 6211 state->abm_level = amdgpu_dm_abm_level; 6212 6213 __drm_atomic_helper_connector_reset(connector, &state->base); 6214 } 6215 } 6216 6217 struct drm_connector_state * 6218 amdgpu_dm_connector_atomic_duplicate_state(struct drm_connector *connector) 6219 { 6220 struct dm_connector_state *state = 6221 to_dm_connector_state(connector->state); 6222 6223 struct dm_connector_state *new_state = 6224 kmemdup(state, sizeof(*state), GFP_KERNEL); 6225 6226 if (!new_state) 6227 return NULL; 6228 6229 __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base); 6230 6231 new_state->freesync_capable = state->freesync_capable; 6232 new_state->abm_level = state->abm_level; 6233 new_state->scaling = state->scaling; 6234 new_state->underscan_enable = state->underscan_enable; 6235 new_state->underscan_hborder = state->underscan_hborder; 6236 new_state->underscan_vborder = state->underscan_vborder; 6237 new_state->vcpi_slots = state->vcpi_slots; 6238 new_state->pbn = state->pbn; 6239 return &new_state->base; 6240 } 6241 6242 static int 6243 amdgpu_dm_connector_late_register(struct drm_connector *connector) 6244 { 6245 struct amdgpu_dm_connector *amdgpu_dm_connector = 6246 to_amdgpu_dm_connector(connector); 6247 int r; 6248 6249 if ((connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) || 6250 (connector->connector_type == DRM_MODE_CONNECTOR_eDP)) { 6251 amdgpu_dm_connector->dm_dp_aux.aux.dev = connector->kdev; 6252 r = drm_dp_aux_register(&amdgpu_dm_connector->dm_dp_aux.aux); 6253 if (r) 6254 return r; 6255 } 6256 6257 #if defined(CONFIG_DEBUG_FS) 6258 connector_debugfs_init(amdgpu_dm_connector); 6259 #endif 6260 6261 return 0; 6262 } 6263 6264 static const struct drm_connector_funcs amdgpu_dm_connector_funcs = { 6265 .reset = amdgpu_dm_connector_funcs_reset, 6266 .detect = amdgpu_dm_connector_detect, 6267 .fill_modes = drm_helper_probe_single_connector_modes, 6268 .destroy = amdgpu_dm_connector_destroy, 6269 .atomic_duplicate_state = amdgpu_dm_connector_atomic_duplicate_state, 6270 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 6271 .atomic_set_property = amdgpu_dm_connector_atomic_set_property, 6272 .atomic_get_property = amdgpu_dm_connector_atomic_get_property, 6273 .late_register = amdgpu_dm_connector_late_register, 6274 .early_unregister = amdgpu_dm_connector_unregister 6275 }; 6276 6277 static int get_modes(struct drm_connector *connector) 6278 { 6279 return amdgpu_dm_connector_get_modes(connector); 6280 } 6281 6282 static void create_eml_sink(struct amdgpu_dm_connector *aconnector) 6283 { 6284 struct dc_sink_init_data init_params = { 6285 .link = aconnector->dc_link, 6286 .sink_signal = SIGNAL_TYPE_VIRTUAL 6287 }; 6288 struct edid *edid; 6289 6290 if (!aconnector->base.edid_blob_ptr) { 6291 DRM_ERROR("No EDID firmware found on connector: %s ,forcing to OFF!\n", 6292 aconnector->base.name); 6293 6294 aconnector->base.force = DRM_FORCE_OFF; 6295 aconnector->base.override_edid = false; 6296 return; 6297 } 6298 6299 edid = (struct edid *) aconnector->base.edid_blob_ptr->data; 6300 6301 aconnector->edid = edid; 6302 6303 aconnector->dc_em_sink = dc_link_add_remote_sink( 6304 aconnector->dc_link, 6305 (uint8_t *)edid, 6306 (edid->extensions + 1) * EDID_LENGTH, 6307 &init_params); 6308 6309 if (aconnector->base.force == DRM_FORCE_ON) { 6310 aconnector->dc_sink = aconnector->dc_link->local_sink ? 6311 aconnector->dc_link->local_sink : 6312 aconnector->dc_em_sink; 6313 dc_sink_retain(aconnector->dc_sink); 6314 } 6315 } 6316 6317 static void handle_edid_mgmt(struct amdgpu_dm_connector *aconnector) 6318 { 6319 struct dc_link *link = (struct dc_link *)aconnector->dc_link; 6320 6321 /* 6322 * In case of headless boot with force on for DP managed connector 6323 * Those settings have to be != 0 to get initial modeset 6324 */ 6325 if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT) { 6326 link->verified_link_cap.lane_count = LANE_COUNT_FOUR; 6327 link->verified_link_cap.link_rate = LINK_RATE_HIGH2; 6328 } 6329 6330 6331 aconnector->base.override_edid = true; 6332 create_eml_sink(aconnector); 6333 } 6334 6335 struct dc_stream_state * 6336 create_validate_stream_for_sink(struct amdgpu_dm_connector *aconnector, 6337 const struct drm_display_mode *drm_mode, 6338 const struct dm_connector_state *dm_state, 6339 const struct dc_stream_state *old_stream) 6340 { 6341 struct drm_connector *connector = &aconnector->base; 6342 struct amdgpu_device *adev = drm_to_adev(connector->dev); 6343 struct dc_stream_state *stream; 6344 const struct drm_connector_state *drm_state = dm_state ? &dm_state->base : NULL; 6345 int requested_bpc = drm_state ? drm_state->max_requested_bpc : 8; 6346 enum dc_status dc_result = DC_OK; 6347 6348 do { 6349 stream = create_stream_for_sink(aconnector, drm_mode, 6350 dm_state, old_stream, 6351 requested_bpc); 6352 if (stream == NULL) { 6353 DRM_ERROR("Failed to create stream for sink!\n"); 6354 break; 6355 } 6356 6357 dc_result = dc_validate_stream(adev->dm.dc, stream); 6358 if (dc_result == DC_OK && stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST) 6359 dc_result = dm_dp_mst_is_port_support_mode(aconnector, stream); 6360 6361 if (dc_result != DC_OK) { 6362 DRM_DEBUG_KMS("Mode %dx%d (clk %d) failed DC validation with error %d (%s)\n", 6363 drm_mode->hdisplay, 6364 drm_mode->vdisplay, 6365 drm_mode->clock, 6366 dc_result, 6367 dc_status_to_str(dc_result)); 6368 6369 dc_stream_release(stream); 6370 stream = NULL; 6371 requested_bpc -= 2; /* lower bpc to retry validation */ 6372 } 6373 6374 } while (stream == NULL && requested_bpc >= 6); 6375 6376 if (dc_result == DC_FAIL_ENC_VALIDATE && !aconnector->force_yuv420_output) { 6377 DRM_DEBUG_KMS("Retry forcing YCbCr420 encoding\n"); 6378 6379 aconnector->force_yuv420_output = true; 6380 stream = create_validate_stream_for_sink(aconnector, drm_mode, 6381 dm_state, old_stream); 6382 aconnector->force_yuv420_output = false; 6383 } 6384 6385 return stream; 6386 } 6387 6388 enum drm_mode_status amdgpu_dm_connector_mode_valid(struct drm_connector *connector, 6389 struct drm_display_mode *mode) 6390 { 6391 int result = MODE_ERROR; 6392 struct dc_sink *dc_sink; 6393 /* TODO: Unhardcode stream count */ 6394 struct dc_stream_state *stream; 6395 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector); 6396 6397 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) || 6398 (mode->flags & DRM_MODE_FLAG_DBLSCAN)) 6399 return result; 6400 6401 /* 6402 * Only run this the first time mode_valid is called to initilialize 6403 * EDID mgmt 6404 */ 6405 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED && 6406 !aconnector->dc_em_sink) 6407 handle_edid_mgmt(aconnector); 6408 6409 dc_sink = to_amdgpu_dm_connector(connector)->dc_sink; 6410 6411 if (dc_sink == NULL && aconnector->base.force != DRM_FORCE_ON_DIGITAL && 6412 aconnector->base.force != DRM_FORCE_ON) { 6413 DRM_ERROR("dc_sink is NULL!\n"); 6414 goto fail; 6415 } 6416 6417 stream = create_validate_stream_for_sink(aconnector, mode, NULL, NULL); 6418 if (stream) { 6419 dc_stream_release(stream); 6420 result = MODE_OK; 6421 } 6422 6423 fail: 6424 /* TODO: error handling*/ 6425 return result; 6426 } 6427 6428 static int fill_hdr_info_packet(const struct drm_connector_state *state, 6429 struct dc_info_packet *out) 6430 { 6431 struct hdmi_drm_infoframe frame; 6432 unsigned char buf[30]; /* 26 + 4 */ 6433 ssize_t len; 6434 int ret, i; 6435 6436 memset(out, 0, sizeof(*out)); 6437 6438 if (!state->hdr_output_metadata) 6439 return 0; 6440 6441 ret = drm_hdmi_infoframe_set_hdr_metadata(&frame, state); 6442 if (ret) 6443 return ret; 6444 6445 len = hdmi_drm_infoframe_pack_only(&frame, buf, sizeof(buf)); 6446 if (len < 0) 6447 return (int)len; 6448 6449 /* Static metadata is a fixed 26 bytes + 4 byte header. */ 6450 if (len != 30) 6451 return -EINVAL; 6452 6453 /* Prepare the infopacket for DC. */ 6454 switch (state->connector->connector_type) { 6455 case DRM_MODE_CONNECTOR_HDMIA: 6456 out->hb0 = 0x87; /* type */ 6457 out->hb1 = 0x01; /* version */ 6458 out->hb2 = 0x1A; /* length */ 6459 out->sb[0] = buf[3]; /* checksum */ 6460 i = 1; 6461 break; 6462 6463 case DRM_MODE_CONNECTOR_DisplayPort: 6464 case DRM_MODE_CONNECTOR_eDP: 6465 out->hb0 = 0x00; /* sdp id, zero */ 6466 out->hb1 = 0x87; /* type */ 6467 out->hb2 = 0x1D; /* payload len - 1 */ 6468 out->hb3 = (0x13 << 2); /* sdp version */ 6469 out->sb[0] = 0x01; /* version */ 6470 out->sb[1] = 0x1A; /* length */ 6471 i = 2; 6472 break; 6473 6474 default: 6475 return -EINVAL; 6476 } 6477 6478 memcpy(&out->sb[i], &buf[4], 26); 6479 out->valid = true; 6480 6481 print_hex_dump(KERN_DEBUG, "HDR SB:", DUMP_PREFIX_NONE, 16, 1, out->sb, 6482 sizeof(out->sb), false); 6483 6484 return 0; 6485 } 6486 6487 static int 6488 amdgpu_dm_connector_atomic_check(struct drm_connector *conn, 6489 struct drm_atomic_state *state) 6490 { 6491 struct drm_connector_state *new_con_state = 6492 drm_atomic_get_new_connector_state(state, conn); 6493 struct drm_connector_state *old_con_state = 6494 drm_atomic_get_old_connector_state(state, conn); 6495 struct drm_crtc *crtc = new_con_state->crtc; 6496 struct drm_crtc_state *new_crtc_state; 6497 struct amdgpu_dm_connector *aconn = to_amdgpu_dm_connector(conn); 6498 int ret; 6499 6500 trace_amdgpu_dm_connector_atomic_check(new_con_state); 6501 6502 if (conn->connector_type == DRM_MODE_CONNECTOR_DisplayPort) { 6503 ret = drm_dp_mst_root_conn_atomic_check(new_con_state, &aconn->mst_mgr); 6504 if (ret < 0) 6505 return ret; 6506 } 6507 6508 if (!crtc) 6509 return 0; 6510 6511 if (!drm_connector_atomic_hdr_metadata_equal(old_con_state, new_con_state)) { 6512 struct dc_info_packet hdr_infopacket; 6513 6514 ret = fill_hdr_info_packet(new_con_state, &hdr_infopacket); 6515 if (ret) 6516 return ret; 6517 6518 new_crtc_state = drm_atomic_get_crtc_state(state, crtc); 6519 if (IS_ERR(new_crtc_state)) 6520 return PTR_ERR(new_crtc_state); 6521 6522 /* 6523 * DC considers the stream backends changed if the 6524 * static metadata changes. Forcing the modeset also 6525 * gives a simple way for userspace to switch from 6526 * 8bpc to 10bpc when setting the metadata to enter 6527 * or exit HDR. 6528 * 6529 * Changing the static metadata after it's been 6530 * set is permissible, however. So only force a 6531 * modeset if we're entering or exiting HDR. 6532 */ 6533 new_crtc_state->mode_changed = 6534 !old_con_state->hdr_output_metadata || 6535 !new_con_state->hdr_output_metadata; 6536 } 6537 6538 return 0; 6539 } 6540 6541 static const struct drm_connector_helper_funcs 6542 amdgpu_dm_connector_helper_funcs = { 6543 /* 6544 * If hotplugging a second bigger display in FB Con mode, bigger resolution 6545 * modes will be filtered by drm_mode_validate_size(), and those modes 6546 * are missing after user start lightdm. So we need to renew modes list. 6547 * in get_modes call back, not just return the modes count 6548 */ 6549 .get_modes = get_modes, 6550 .mode_valid = amdgpu_dm_connector_mode_valid, 6551 .atomic_check = amdgpu_dm_connector_atomic_check, 6552 }; 6553 6554 static void dm_encoder_helper_disable(struct drm_encoder *encoder) 6555 { 6556 6557 } 6558 6559 int convert_dc_color_depth_into_bpc(enum dc_color_depth display_color_depth) 6560 { 6561 switch (display_color_depth) { 6562 case COLOR_DEPTH_666: 6563 return 6; 6564 case COLOR_DEPTH_888: 6565 return 8; 6566 case COLOR_DEPTH_101010: 6567 return 10; 6568 case COLOR_DEPTH_121212: 6569 return 12; 6570 case COLOR_DEPTH_141414: 6571 return 14; 6572 case COLOR_DEPTH_161616: 6573 return 16; 6574 default: 6575 break; 6576 } 6577 return 0; 6578 } 6579 6580 static int dm_encoder_helper_atomic_check(struct drm_encoder *encoder, 6581 struct drm_crtc_state *crtc_state, 6582 struct drm_connector_state *conn_state) 6583 { 6584 struct drm_atomic_state *state = crtc_state->state; 6585 struct drm_connector *connector = conn_state->connector; 6586 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector); 6587 struct dm_connector_state *dm_new_connector_state = to_dm_connector_state(conn_state); 6588 const struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode; 6589 struct drm_dp_mst_topology_mgr *mst_mgr; 6590 struct drm_dp_mst_port *mst_port; 6591 struct drm_dp_mst_topology_state *mst_state; 6592 enum dc_color_depth color_depth; 6593 int clock, bpp = 0; 6594 bool is_y420 = false; 6595 6596 if (!aconnector->port) 6597 return 0; 6598 6599 mst_port = aconnector->port; 6600 mst_mgr = &aconnector->mst_port->mst_mgr; 6601 6602 if (!crtc_state->connectors_changed && !crtc_state->mode_changed) 6603 return 0; 6604 6605 mst_state = drm_atomic_get_mst_topology_state(state, mst_mgr); 6606 if (IS_ERR(mst_state)) 6607 return PTR_ERR(mst_state); 6608 6609 if (!mst_state->pbn_div) 6610 mst_state->pbn_div = dm_mst_get_pbn_divider(aconnector->mst_port->dc_link); 6611 6612 if (!state->duplicated) { 6613 int max_bpc = conn_state->max_requested_bpc; 6614 6615 is_y420 = drm_mode_is_420_also(&connector->display_info, adjusted_mode) && 6616 aconnector->force_yuv420_output; 6617 color_depth = convert_color_depth_from_display_info(connector, 6618 is_y420, 6619 max_bpc); 6620 bpp = convert_dc_color_depth_into_bpc(color_depth) * 3; 6621 clock = adjusted_mode->clock; 6622 dm_new_connector_state->pbn = drm_dp_calc_pbn_mode(clock, bpp, false); 6623 } 6624 6625 dm_new_connector_state->vcpi_slots = 6626 drm_dp_atomic_find_time_slots(state, mst_mgr, mst_port, 6627 dm_new_connector_state->pbn); 6628 if (dm_new_connector_state->vcpi_slots < 0) { 6629 DRM_DEBUG_ATOMIC("failed finding vcpi slots: %d\n", (int)dm_new_connector_state->vcpi_slots); 6630 return dm_new_connector_state->vcpi_slots; 6631 } 6632 return 0; 6633 } 6634 6635 const struct drm_encoder_helper_funcs amdgpu_dm_encoder_helper_funcs = { 6636 .disable = dm_encoder_helper_disable, 6637 .atomic_check = dm_encoder_helper_atomic_check 6638 }; 6639 6640 #if defined(CONFIG_DRM_AMD_DC_DCN) 6641 static int dm_update_mst_vcpi_slots_for_dsc(struct drm_atomic_state *state, 6642 struct dc_state *dc_state, 6643 struct dsc_mst_fairness_vars *vars) 6644 { 6645 struct dc_stream_state *stream = NULL; 6646 struct drm_connector *connector; 6647 struct drm_connector_state *new_con_state; 6648 struct amdgpu_dm_connector *aconnector; 6649 struct dm_connector_state *dm_conn_state; 6650 int i, j, ret; 6651 int vcpi, pbn_div, pbn, slot_num = 0; 6652 6653 for_each_new_connector_in_state(state, connector, new_con_state, i) { 6654 6655 aconnector = to_amdgpu_dm_connector(connector); 6656 6657 if (!aconnector->port) 6658 continue; 6659 6660 if (!new_con_state || !new_con_state->crtc) 6661 continue; 6662 6663 dm_conn_state = to_dm_connector_state(new_con_state); 6664 6665 for (j = 0; j < dc_state->stream_count; j++) { 6666 stream = dc_state->streams[j]; 6667 if (!stream) 6668 continue; 6669 6670 if ((struct amdgpu_dm_connector *)stream->dm_stream_context == aconnector) 6671 break; 6672 6673 stream = NULL; 6674 } 6675 6676 if (!stream) 6677 continue; 6678 6679 pbn_div = dm_mst_get_pbn_divider(stream->link); 6680 /* pbn is calculated by compute_mst_dsc_configs_for_state*/ 6681 for (j = 0; j < dc_state->stream_count; j++) { 6682 if (vars[j].aconnector == aconnector) { 6683 pbn = vars[j].pbn; 6684 break; 6685 } 6686 } 6687 6688 if (j == dc_state->stream_count) 6689 continue; 6690 6691 slot_num = DIV_ROUND_UP(pbn, pbn_div); 6692 6693 if (stream->timing.flags.DSC != 1) { 6694 dm_conn_state->pbn = pbn; 6695 dm_conn_state->vcpi_slots = slot_num; 6696 6697 ret = drm_dp_mst_atomic_enable_dsc(state, aconnector->port, 6698 dm_conn_state->pbn, false); 6699 if (ret < 0) 6700 return ret; 6701 6702 continue; 6703 } 6704 6705 vcpi = drm_dp_mst_atomic_enable_dsc(state, aconnector->port, pbn, true); 6706 if (vcpi < 0) 6707 return vcpi; 6708 6709 dm_conn_state->pbn = pbn; 6710 dm_conn_state->vcpi_slots = vcpi; 6711 } 6712 return 0; 6713 } 6714 #endif 6715 6716 static int to_drm_connector_type(enum amd_signal_type st) 6717 { 6718 switch (st) { 6719 case SIGNAL_TYPE_HDMI_TYPE_A: 6720 return DRM_MODE_CONNECTOR_HDMIA; 6721 case SIGNAL_TYPE_EDP: 6722 return DRM_MODE_CONNECTOR_eDP; 6723 case SIGNAL_TYPE_LVDS: 6724 return DRM_MODE_CONNECTOR_LVDS; 6725 case SIGNAL_TYPE_RGB: 6726 return DRM_MODE_CONNECTOR_VGA; 6727 case SIGNAL_TYPE_DISPLAY_PORT: 6728 case SIGNAL_TYPE_DISPLAY_PORT_MST: 6729 return DRM_MODE_CONNECTOR_DisplayPort; 6730 case SIGNAL_TYPE_DVI_DUAL_LINK: 6731 case SIGNAL_TYPE_DVI_SINGLE_LINK: 6732 return DRM_MODE_CONNECTOR_DVID; 6733 case SIGNAL_TYPE_VIRTUAL: 6734 return DRM_MODE_CONNECTOR_VIRTUAL; 6735 6736 default: 6737 return DRM_MODE_CONNECTOR_Unknown; 6738 } 6739 } 6740 6741 static struct drm_encoder *amdgpu_dm_connector_to_encoder(struct drm_connector *connector) 6742 { 6743 struct drm_encoder *encoder; 6744 6745 /* There is only one encoder per connector */ 6746 drm_connector_for_each_possible_encoder(connector, encoder) 6747 return encoder; 6748 6749 return NULL; 6750 } 6751 6752 static void amdgpu_dm_get_native_mode(struct drm_connector *connector) 6753 { 6754 struct drm_encoder *encoder; 6755 struct amdgpu_encoder *amdgpu_encoder; 6756 6757 encoder = amdgpu_dm_connector_to_encoder(connector); 6758 6759 if (encoder == NULL) 6760 return; 6761 6762 amdgpu_encoder = to_amdgpu_encoder(encoder); 6763 6764 amdgpu_encoder->native_mode.clock = 0; 6765 6766 if (!list_empty(&connector->probed_modes)) { 6767 struct drm_display_mode *preferred_mode = NULL; 6768 6769 list_for_each_entry(preferred_mode, 6770 &connector->probed_modes, 6771 head) { 6772 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED) 6773 amdgpu_encoder->native_mode = *preferred_mode; 6774 6775 break; 6776 } 6777 6778 } 6779 } 6780 6781 static struct drm_display_mode * 6782 amdgpu_dm_create_common_mode(struct drm_encoder *encoder, 6783 char *name, 6784 int hdisplay, int vdisplay) 6785 { 6786 struct drm_device *dev = encoder->dev; 6787 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder); 6788 struct drm_display_mode *mode = NULL; 6789 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode; 6790 6791 mode = drm_mode_duplicate(dev, native_mode); 6792 6793 if (mode == NULL) 6794 return NULL; 6795 6796 mode->hdisplay = hdisplay; 6797 mode->vdisplay = vdisplay; 6798 mode->type &= ~DRM_MODE_TYPE_PREFERRED; 6799 strscpy(mode->name, name, DRM_DISPLAY_MODE_LEN); 6800 6801 return mode; 6802 6803 } 6804 6805 static void amdgpu_dm_connector_add_common_modes(struct drm_encoder *encoder, 6806 struct drm_connector *connector) 6807 { 6808 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder); 6809 struct drm_display_mode *mode = NULL; 6810 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode; 6811 struct amdgpu_dm_connector *amdgpu_dm_connector = 6812 to_amdgpu_dm_connector(connector); 6813 int i; 6814 int n; 6815 struct mode_size { 6816 char name[DRM_DISPLAY_MODE_LEN]; 6817 int w; 6818 int h; 6819 } common_modes[] = { 6820 { "640x480", 640, 480}, 6821 { "800x600", 800, 600}, 6822 { "1024x768", 1024, 768}, 6823 { "1280x720", 1280, 720}, 6824 { "1280x800", 1280, 800}, 6825 {"1280x1024", 1280, 1024}, 6826 { "1440x900", 1440, 900}, 6827 {"1680x1050", 1680, 1050}, 6828 {"1600x1200", 1600, 1200}, 6829 {"1920x1080", 1920, 1080}, 6830 {"1920x1200", 1920, 1200} 6831 }; 6832 6833 n = ARRAY_SIZE(common_modes); 6834 6835 for (i = 0; i < n; i++) { 6836 struct drm_display_mode *curmode = NULL; 6837 bool mode_existed = false; 6838 6839 if (common_modes[i].w > native_mode->hdisplay || 6840 common_modes[i].h > native_mode->vdisplay || 6841 (common_modes[i].w == native_mode->hdisplay && 6842 common_modes[i].h == native_mode->vdisplay)) 6843 continue; 6844 6845 list_for_each_entry(curmode, &connector->probed_modes, head) { 6846 if (common_modes[i].w == curmode->hdisplay && 6847 common_modes[i].h == curmode->vdisplay) { 6848 mode_existed = true; 6849 break; 6850 } 6851 } 6852 6853 if (mode_existed) 6854 continue; 6855 6856 mode = amdgpu_dm_create_common_mode(encoder, 6857 common_modes[i].name, common_modes[i].w, 6858 common_modes[i].h); 6859 if (!mode) 6860 continue; 6861 6862 drm_mode_probed_add(connector, mode); 6863 amdgpu_dm_connector->num_modes++; 6864 } 6865 } 6866 6867 static void amdgpu_set_panel_orientation(struct drm_connector *connector) 6868 { 6869 struct drm_encoder *encoder; 6870 struct amdgpu_encoder *amdgpu_encoder; 6871 const struct drm_display_mode *native_mode; 6872 6873 if (connector->connector_type != DRM_MODE_CONNECTOR_eDP && 6874 connector->connector_type != DRM_MODE_CONNECTOR_LVDS) 6875 return; 6876 6877 mutex_lock(&connector->dev->mode_config.mutex); 6878 amdgpu_dm_connector_get_modes(connector); 6879 mutex_unlock(&connector->dev->mode_config.mutex); 6880 6881 encoder = amdgpu_dm_connector_to_encoder(connector); 6882 if (!encoder) 6883 return; 6884 6885 amdgpu_encoder = to_amdgpu_encoder(encoder); 6886 6887 native_mode = &amdgpu_encoder->native_mode; 6888 if (native_mode->hdisplay == 0 || native_mode->vdisplay == 0) 6889 return; 6890 6891 drm_connector_set_panel_orientation_with_quirk(connector, 6892 DRM_MODE_PANEL_ORIENTATION_UNKNOWN, 6893 native_mode->hdisplay, 6894 native_mode->vdisplay); 6895 } 6896 6897 static void amdgpu_dm_connector_ddc_get_modes(struct drm_connector *connector, 6898 struct edid *edid) 6899 { 6900 struct amdgpu_dm_connector *amdgpu_dm_connector = 6901 to_amdgpu_dm_connector(connector); 6902 6903 if (edid) { 6904 /* empty probed_modes */ 6905 INIT_LIST_HEAD(&connector->probed_modes); 6906 amdgpu_dm_connector->num_modes = 6907 drm_add_edid_modes(connector, edid); 6908 6909 /* sorting the probed modes before calling function 6910 * amdgpu_dm_get_native_mode() since EDID can have 6911 * more than one preferred mode. The modes that are 6912 * later in the probed mode list could be of higher 6913 * and preferred resolution. For example, 3840x2160 6914 * resolution in base EDID preferred timing and 4096x2160 6915 * preferred resolution in DID extension block later. 6916 */ 6917 drm_mode_sort(&connector->probed_modes); 6918 amdgpu_dm_get_native_mode(connector); 6919 6920 /* Freesync capabilities are reset by calling 6921 * drm_add_edid_modes() and need to be 6922 * restored here. 6923 */ 6924 amdgpu_dm_update_freesync_caps(connector, edid); 6925 } else { 6926 amdgpu_dm_connector->num_modes = 0; 6927 } 6928 } 6929 6930 static bool is_duplicate_mode(struct amdgpu_dm_connector *aconnector, 6931 struct drm_display_mode *mode) 6932 { 6933 struct drm_display_mode *m; 6934 6935 list_for_each_entry(m, &aconnector->base.probed_modes, head) { 6936 if (drm_mode_equal(m, mode)) 6937 return true; 6938 } 6939 6940 return false; 6941 } 6942 6943 static uint add_fs_modes(struct amdgpu_dm_connector *aconnector) 6944 { 6945 const struct drm_display_mode *m; 6946 struct drm_display_mode *new_mode; 6947 uint i; 6948 u32 new_modes_count = 0; 6949 6950 /* Standard FPS values 6951 * 6952 * 23.976 - TV/NTSC 6953 * 24 - Cinema 6954 * 25 - TV/PAL 6955 * 29.97 - TV/NTSC 6956 * 30 - TV/NTSC 6957 * 48 - Cinema HFR 6958 * 50 - TV/PAL 6959 * 60 - Commonly used 6960 * 48,72,96,120 - Multiples of 24 6961 */ 6962 static const u32 common_rates[] = { 6963 23976, 24000, 25000, 29970, 30000, 6964 48000, 50000, 60000, 72000, 96000, 120000 6965 }; 6966 6967 /* 6968 * Find mode with highest refresh rate with the same resolution 6969 * as the preferred mode. Some monitors report a preferred mode 6970 * with lower resolution than the highest refresh rate supported. 6971 */ 6972 6973 m = get_highest_refresh_rate_mode(aconnector, true); 6974 if (!m) 6975 return 0; 6976 6977 for (i = 0; i < ARRAY_SIZE(common_rates); i++) { 6978 u64 target_vtotal, target_vtotal_diff; 6979 u64 num, den; 6980 6981 if (drm_mode_vrefresh(m) * 1000 < common_rates[i]) 6982 continue; 6983 6984 if (common_rates[i] < aconnector->min_vfreq * 1000 || 6985 common_rates[i] > aconnector->max_vfreq * 1000) 6986 continue; 6987 6988 num = (unsigned long long)m->clock * 1000 * 1000; 6989 den = common_rates[i] * (unsigned long long)m->htotal; 6990 target_vtotal = div_u64(num, den); 6991 target_vtotal_diff = target_vtotal - m->vtotal; 6992 6993 /* Check for illegal modes */ 6994 if (m->vsync_start + target_vtotal_diff < m->vdisplay || 6995 m->vsync_end + target_vtotal_diff < m->vsync_start || 6996 m->vtotal + target_vtotal_diff < m->vsync_end) 6997 continue; 6998 6999 new_mode = drm_mode_duplicate(aconnector->base.dev, m); 7000 if (!new_mode) 7001 goto out; 7002 7003 new_mode->vtotal += (u16)target_vtotal_diff; 7004 new_mode->vsync_start += (u16)target_vtotal_diff; 7005 new_mode->vsync_end += (u16)target_vtotal_diff; 7006 new_mode->type &= ~DRM_MODE_TYPE_PREFERRED; 7007 new_mode->type |= DRM_MODE_TYPE_DRIVER; 7008 7009 if (!is_duplicate_mode(aconnector, new_mode)) { 7010 drm_mode_probed_add(&aconnector->base, new_mode); 7011 new_modes_count += 1; 7012 } else 7013 drm_mode_destroy(aconnector->base.dev, new_mode); 7014 } 7015 out: 7016 return new_modes_count; 7017 } 7018 7019 static void amdgpu_dm_connector_add_freesync_modes(struct drm_connector *connector, 7020 struct edid *edid) 7021 { 7022 struct amdgpu_dm_connector *amdgpu_dm_connector = 7023 to_amdgpu_dm_connector(connector); 7024 7025 if (!edid) 7026 return; 7027 7028 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10) 7029 amdgpu_dm_connector->num_modes += 7030 add_fs_modes(amdgpu_dm_connector); 7031 } 7032 7033 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector) 7034 { 7035 struct amdgpu_dm_connector *amdgpu_dm_connector = 7036 to_amdgpu_dm_connector(connector); 7037 struct drm_encoder *encoder; 7038 struct edid *edid = amdgpu_dm_connector->edid; 7039 7040 encoder = amdgpu_dm_connector_to_encoder(connector); 7041 7042 if (!drm_edid_is_valid(edid)) { 7043 amdgpu_dm_connector->num_modes = 7044 drm_add_modes_noedid(connector, 640, 480); 7045 } else { 7046 amdgpu_dm_connector_ddc_get_modes(connector, edid); 7047 amdgpu_dm_connector_add_common_modes(encoder, connector); 7048 amdgpu_dm_connector_add_freesync_modes(connector, edid); 7049 } 7050 amdgpu_dm_fbc_init(connector); 7051 7052 return amdgpu_dm_connector->num_modes; 7053 } 7054 7055 void amdgpu_dm_connector_init_helper(struct amdgpu_display_manager *dm, 7056 struct amdgpu_dm_connector *aconnector, 7057 int connector_type, 7058 struct dc_link *link, 7059 int link_index) 7060 { 7061 struct amdgpu_device *adev = drm_to_adev(dm->ddev); 7062 7063 /* 7064 * Some of the properties below require access to state, like bpc. 7065 * Allocate some default initial connector state with our reset helper. 7066 */ 7067 if (aconnector->base.funcs->reset) 7068 aconnector->base.funcs->reset(&aconnector->base); 7069 7070 aconnector->connector_id = link_index; 7071 aconnector->dc_link = link; 7072 aconnector->base.interlace_allowed = false; 7073 aconnector->base.doublescan_allowed = false; 7074 aconnector->base.stereo_allowed = false; 7075 aconnector->base.dpms = DRM_MODE_DPMS_OFF; 7076 aconnector->hpd.hpd = AMDGPU_HPD_NONE; /* not used */ 7077 aconnector->audio_inst = -1; 7078 rw_init(&aconnector->hpd_lock, "dmhpd"); 7079 rw_init(&aconnector->handle_mst_msg_ready, "dmmr"); 7080 7081 /* 7082 * configure support HPD hot plug connector_>polled default value is 0 7083 * which means HPD hot plug not supported 7084 */ 7085 switch (connector_type) { 7086 case DRM_MODE_CONNECTOR_HDMIA: 7087 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD; 7088 aconnector->base.ycbcr_420_allowed = 7089 link->link_enc->features.hdmi_ycbcr420_supported ? true : false; 7090 break; 7091 case DRM_MODE_CONNECTOR_DisplayPort: 7092 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD; 7093 link->link_enc = link_enc_cfg_get_link_enc(link); 7094 ASSERT(link->link_enc); 7095 if (link->link_enc) 7096 aconnector->base.ycbcr_420_allowed = 7097 link->link_enc->features.dp_ycbcr420_supported ? true : false; 7098 break; 7099 case DRM_MODE_CONNECTOR_DVID: 7100 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD; 7101 break; 7102 default: 7103 break; 7104 } 7105 7106 drm_object_attach_property(&aconnector->base.base, 7107 dm->ddev->mode_config.scaling_mode_property, 7108 DRM_MODE_SCALE_NONE); 7109 7110 drm_object_attach_property(&aconnector->base.base, 7111 adev->mode_info.underscan_property, 7112 UNDERSCAN_OFF); 7113 drm_object_attach_property(&aconnector->base.base, 7114 adev->mode_info.underscan_hborder_property, 7115 0); 7116 drm_object_attach_property(&aconnector->base.base, 7117 adev->mode_info.underscan_vborder_property, 7118 0); 7119 7120 if (!aconnector->mst_port) 7121 drm_connector_attach_max_bpc_property(&aconnector->base, 8, 16); 7122 7123 /* This defaults to the max in the range, but we want 8bpc for non-edp. */ 7124 aconnector->base.state->max_bpc = (connector_type == DRM_MODE_CONNECTOR_eDP) ? 16 : 8; 7125 aconnector->base.state->max_requested_bpc = aconnector->base.state->max_bpc; 7126 7127 if (connector_type == DRM_MODE_CONNECTOR_eDP && 7128 (dc_is_dmcu_initialized(adev->dm.dc) || adev->dm.dc->ctx->dmub_srv)) { 7129 drm_object_attach_property(&aconnector->base.base, 7130 adev->mode_info.abm_level_property, 0); 7131 } 7132 7133 if (connector_type == DRM_MODE_CONNECTOR_HDMIA || 7134 connector_type == DRM_MODE_CONNECTOR_DisplayPort || 7135 connector_type == DRM_MODE_CONNECTOR_eDP) { 7136 drm_connector_attach_hdr_output_metadata_property(&aconnector->base); 7137 7138 if (!aconnector->mst_port) 7139 drm_connector_attach_vrr_capable_property(&aconnector->base); 7140 7141 #ifdef CONFIG_DRM_AMD_DC_HDCP 7142 if (adev->dm.hdcp_workqueue) 7143 drm_connector_attach_content_protection_property(&aconnector->base, true); 7144 #endif 7145 } 7146 } 7147 7148 static int amdgpu_dm_i2c_xfer(struct i2c_adapter *i2c_adap, 7149 struct i2c_msg *msgs, int num) 7150 { 7151 struct amdgpu_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap); 7152 struct ddc_service *ddc_service = i2c->ddc_service; 7153 struct i2c_command cmd; 7154 int i; 7155 int result = -EIO; 7156 7157 cmd.payloads = kcalloc(num, sizeof(struct i2c_payload), GFP_KERNEL); 7158 7159 if (!cmd.payloads) 7160 return result; 7161 7162 cmd.number_of_payloads = num; 7163 cmd.engine = I2C_COMMAND_ENGINE_DEFAULT; 7164 cmd.speed = 100; 7165 7166 for (i = 0; i < num; i++) { 7167 cmd.payloads[i].write = !(msgs[i].flags & I2C_M_RD); 7168 cmd.payloads[i].address = msgs[i].addr; 7169 cmd.payloads[i].length = msgs[i].len; 7170 cmd.payloads[i].data = msgs[i].buf; 7171 } 7172 7173 if (dc_submit_i2c( 7174 ddc_service->ctx->dc, 7175 ddc_service->link->link_index, 7176 &cmd)) 7177 result = num; 7178 7179 kfree(cmd.payloads); 7180 return result; 7181 } 7182 7183 static u32 amdgpu_dm_i2c_func(struct i2c_adapter *adap) 7184 { 7185 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 7186 } 7187 7188 static const struct i2c_algorithm amdgpu_dm_i2c_algo = { 7189 .master_xfer = amdgpu_dm_i2c_xfer, 7190 .functionality = amdgpu_dm_i2c_func, 7191 }; 7192 7193 static struct amdgpu_i2c_adapter * 7194 create_i2c(struct ddc_service *ddc_service, 7195 int link_index, 7196 int *res) 7197 { 7198 struct amdgpu_device *adev = ddc_service->ctx->driver_context; 7199 struct amdgpu_i2c_adapter *i2c; 7200 7201 i2c = kzalloc(sizeof(struct amdgpu_i2c_adapter), GFP_KERNEL); 7202 if (!i2c) 7203 return NULL; 7204 #ifdef notyet 7205 i2c->base.owner = THIS_MODULE; 7206 i2c->base.class = I2C_CLASS_DDC; 7207 i2c->base.dev.parent = &adev->pdev->dev; 7208 #endif 7209 i2c->base.algo = &amdgpu_dm_i2c_algo; 7210 snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c hw bus %d", link_index); 7211 i2c_set_adapdata(&i2c->base, i2c); 7212 i2c->ddc_service = ddc_service; 7213 7214 return i2c; 7215 } 7216 7217 7218 /* 7219 * Note: this function assumes that dc_link_detect() was called for the 7220 * dc_link which will be represented by this aconnector. 7221 */ 7222 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm, 7223 struct amdgpu_dm_connector *aconnector, 7224 u32 link_index, 7225 struct amdgpu_encoder *aencoder) 7226 { 7227 int res = 0; 7228 int connector_type; 7229 struct dc *dc = dm->dc; 7230 struct dc_link *link = dc_get_link_at_index(dc, link_index); 7231 struct amdgpu_i2c_adapter *i2c; 7232 7233 link->priv = aconnector; 7234 7235 7236 i2c = create_i2c(link->ddc, link->link_index, &res); 7237 if (!i2c) { 7238 DRM_ERROR("Failed to create i2c adapter data\n"); 7239 return -ENOMEM; 7240 } 7241 7242 aconnector->i2c = i2c; 7243 res = i2c_add_adapter(&i2c->base); 7244 7245 if (res) { 7246 DRM_ERROR("Failed to register hw i2c %d\n", link->link_index); 7247 goto out_free; 7248 } 7249 7250 connector_type = to_drm_connector_type(link->connector_signal); 7251 7252 res = drm_connector_init_with_ddc( 7253 dm->ddev, 7254 &aconnector->base, 7255 &amdgpu_dm_connector_funcs, 7256 connector_type, 7257 &i2c->base); 7258 7259 if (res) { 7260 DRM_ERROR("connector_init failed\n"); 7261 aconnector->connector_id = -1; 7262 goto out_free; 7263 } 7264 7265 drm_connector_helper_add( 7266 &aconnector->base, 7267 &amdgpu_dm_connector_helper_funcs); 7268 7269 amdgpu_dm_connector_init_helper( 7270 dm, 7271 aconnector, 7272 connector_type, 7273 link, 7274 link_index); 7275 7276 drm_connector_attach_encoder( 7277 &aconnector->base, &aencoder->base); 7278 7279 if (connector_type == DRM_MODE_CONNECTOR_DisplayPort 7280 || connector_type == DRM_MODE_CONNECTOR_eDP) 7281 amdgpu_dm_initialize_dp_connector(dm, aconnector, link->link_index); 7282 7283 out_free: 7284 if (res) { 7285 kfree(i2c); 7286 aconnector->i2c = NULL; 7287 } 7288 return res; 7289 } 7290 7291 int amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device *adev) 7292 { 7293 switch (adev->mode_info.num_crtc) { 7294 case 1: 7295 return 0x1; 7296 case 2: 7297 return 0x3; 7298 case 3: 7299 return 0x7; 7300 case 4: 7301 return 0xf; 7302 case 5: 7303 return 0x1f; 7304 case 6: 7305 default: 7306 return 0x3f; 7307 } 7308 } 7309 7310 static int amdgpu_dm_encoder_init(struct drm_device *dev, 7311 struct amdgpu_encoder *aencoder, 7312 uint32_t link_index) 7313 { 7314 struct amdgpu_device *adev = drm_to_adev(dev); 7315 7316 int res = drm_encoder_init(dev, 7317 &aencoder->base, 7318 &amdgpu_dm_encoder_funcs, 7319 DRM_MODE_ENCODER_TMDS, 7320 NULL); 7321 7322 aencoder->base.possible_crtcs = amdgpu_dm_get_encoder_crtc_mask(adev); 7323 7324 if (!res) 7325 aencoder->encoder_id = link_index; 7326 else 7327 aencoder->encoder_id = -1; 7328 7329 drm_encoder_helper_add(&aencoder->base, &amdgpu_dm_encoder_helper_funcs); 7330 7331 return res; 7332 } 7333 7334 static void manage_dm_interrupts(struct amdgpu_device *adev, 7335 struct amdgpu_crtc *acrtc, 7336 bool enable) 7337 { 7338 /* 7339 * We have no guarantee that the frontend index maps to the same 7340 * backend index - some even map to more than one. 7341 * 7342 * TODO: Use a different interrupt or check DC itself for the mapping. 7343 */ 7344 int irq_type = 7345 amdgpu_display_crtc_idx_to_irq_type( 7346 adev, 7347 acrtc->crtc_id); 7348 7349 if (enable) { 7350 drm_crtc_vblank_on(&acrtc->base); 7351 amdgpu_irq_get( 7352 adev, 7353 &adev->pageflip_irq, 7354 irq_type); 7355 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY) 7356 amdgpu_irq_get( 7357 adev, 7358 &adev->vline0_irq, 7359 irq_type); 7360 #endif 7361 } else { 7362 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY) 7363 amdgpu_irq_put( 7364 adev, 7365 &adev->vline0_irq, 7366 irq_type); 7367 #endif 7368 amdgpu_irq_put( 7369 adev, 7370 &adev->pageflip_irq, 7371 irq_type); 7372 drm_crtc_vblank_off(&acrtc->base); 7373 } 7374 } 7375 7376 static void dm_update_pflip_irq_state(struct amdgpu_device *adev, 7377 struct amdgpu_crtc *acrtc) 7378 { 7379 int irq_type = 7380 amdgpu_display_crtc_idx_to_irq_type(adev, acrtc->crtc_id); 7381 7382 /** 7383 * This reads the current state for the IRQ and force reapplies 7384 * the setting to hardware. 7385 */ 7386 amdgpu_irq_update(adev, &adev->pageflip_irq, irq_type); 7387 } 7388 7389 static bool 7390 is_scaling_state_different(const struct dm_connector_state *dm_state, 7391 const struct dm_connector_state *old_dm_state) 7392 { 7393 if (dm_state->scaling != old_dm_state->scaling) 7394 return true; 7395 if (!dm_state->underscan_enable && old_dm_state->underscan_enable) { 7396 if (old_dm_state->underscan_hborder != 0 && old_dm_state->underscan_vborder != 0) 7397 return true; 7398 } else if (dm_state->underscan_enable && !old_dm_state->underscan_enable) { 7399 if (dm_state->underscan_hborder != 0 && dm_state->underscan_vborder != 0) 7400 return true; 7401 } else if (dm_state->underscan_hborder != old_dm_state->underscan_hborder || 7402 dm_state->underscan_vborder != old_dm_state->underscan_vborder) 7403 return true; 7404 return false; 7405 } 7406 7407 #ifdef CONFIG_DRM_AMD_DC_HDCP 7408 static bool is_content_protection_different(struct drm_crtc_state *new_crtc_state, 7409 struct drm_crtc_state *old_crtc_state, 7410 struct drm_connector_state *new_conn_state, 7411 struct drm_connector_state *old_conn_state, 7412 const struct drm_connector *connector, 7413 struct hdcp_workqueue *hdcp_w) 7414 { 7415 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector); 7416 struct dm_connector_state *dm_con_state = to_dm_connector_state(connector->state); 7417 7418 pr_debug("[HDCP_DM] connector->index: %x connect_status: %x dpms: %x\n", 7419 connector->index, connector->status, connector->dpms); 7420 pr_debug("[HDCP_DM] state protection old: %x new: %x\n", 7421 old_conn_state->content_protection, new_conn_state->content_protection); 7422 7423 if (old_crtc_state) 7424 pr_debug("[HDCP_DM] old crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n", 7425 old_crtc_state->enable, 7426 old_crtc_state->active, 7427 old_crtc_state->mode_changed, 7428 old_crtc_state->active_changed, 7429 old_crtc_state->connectors_changed); 7430 7431 if (new_crtc_state) 7432 pr_debug("[HDCP_DM] NEW crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n", 7433 new_crtc_state->enable, 7434 new_crtc_state->active, 7435 new_crtc_state->mode_changed, 7436 new_crtc_state->active_changed, 7437 new_crtc_state->connectors_changed); 7438 7439 /* hdcp content type change */ 7440 if (old_conn_state->hdcp_content_type != new_conn_state->hdcp_content_type && 7441 new_conn_state->content_protection != DRM_MODE_CONTENT_PROTECTION_UNDESIRED) { 7442 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED; 7443 pr_debug("[HDCP_DM] Type0/1 change %s :true\n", __func__); 7444 return true; 7445 } 7446 7447 /* CP is being re enabled, ignore this */ 7448 if (old_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED && 7449 new_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED) { 7450 if (new_crtc_state && new_crtc_state->mode_changed) { 7451 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED; 7452 pr_debug("[HDCP_DM] ENABLED->DESIRED & mode_changed %s :true\n", __func__); 7453 return true; 7454 }; 7455 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_ENABLED; 7456 pr_debug("[HDCP_DM] ENABLED -> DESIRED %s :false\n", __func__); 7457 return false; 7458 } 7459 7460 /* S3 resume case, since old state will always be 0 (UNDESIRED) and the restored state will be ENABLED 7461 * 7462 * Handles: UNDESIRED -> ENABLED 7463 */ 7464 if (old_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_UNDESIRED && 7465 new_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED) 7466 new_conn_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED; 7467 7468 /* Stream removed and re-enabled 7469 * 7470 * Can sometimes overlap with the HPD case, 7471 * thus set update_hdcp to false to avoid 7472 * setting HDCP multiple times. 7473 * 7474 * Handles: DESIRED -> DESIRED (Special case) 7475 */ 7476 if (!(old_conn_state->crtc && old_conn_state->crtc->enabled) && 7477 new_conn_state->crtc && new_conn_state->crtc->enabled && 7478 connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED) { 7479 dm_con_state->update_hdcp = false; 7480 pr_debug("[HDCP_DM] DESIRED->DESIRED (Stream removed and re-enabled) %s :true\n", 7481 __func__); 7482 return true; 7483 } 7484 7485 /* Hot-plug, headless s3, dpms 7486 * 7487 * Only start HDCP if the display is connected/enabled. 7488 * update_hdcp flag will be set to false until the next 7489 * HPD comes in. 7490 * 7491 * Handles: DESIRED -> DESIRED (Special case) 7492 */ 7493 if (dm_con_state->update_hdcp && 7494 new_conn_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED && 7495 connector->dpms == DRM_MODE_DPMS_ON && aconnector->dc_sink != NULL) { 7496 dm_con_state->update_hdcp = false; 7497 pr_debug("[HDCP_DM] DESIRED->DESIRED (Hot-plug, headless s3, dpms) %s :true\n", 7498 __func__); 7499 return true; 7500 } 7501 7502 if (old_conn_state->content_protection == new_conn_state->content_protection) { 7503 if (new_conn_state->content_protection >= DRM_MODE_CONTENT_PROTECTION_DESIRED) { 7504 if (new_crtc_state && new_crtc_state->mode_changed) { 7505 pr_debug("[HDCP_DM] DESIRED->DESIRED or ENABLE->ENABLE mode_change %s :true\n", 7506 __func__); 7507 return true; 7508 }; 7509 pr_debug("[HDCP_DM] DESIRED->DESIRED & ENABLE->ENABLE %s :false\n", 7510 __func__); 7511 return false; 7512 }; 7513 7514 pr_debug("[HDCP_DM] UNDESIRED->UNDESIRED %s :false\n", __func__); 7515 return false; 7516 } 7517 7518 if (new_conn_state->content_protection != DRM_MODE_CONTENT_PROTECTION_ENABLED) { 7519 pr_debug("[HDCP_DM] UNDESIRED->DESIRED or DESIRED->UNDESIRED or ENABLED->UNDESIRED %s :true\n", 7520 __func__); 7521 return true; 7522 } 7523 7524 pr_debug("[HDCP_DM] DESIRED->ENABLED %s :false\n", __func__); 7525 return false; 7526 } 7527 #endif 7528 7529 static void remove_stream(struct amdgpu_device *adev, 7530 struct amdgpu_crtc *acrtc, 7531 struct dc_stream_state *stream) 7532 { 7533 /* this is the update mode case */ 7534 7535 acrtc->otg_inst = -1; 7536 acrtc->enabled = false; 7537 } 7538 7539 static void prepare_flip_isr(struct amdgpu_crtc *acrtc) 7540 { 7541 7542 assert_spin_locked(&acrtc->base.dev->event_lock); 7543 WARN_ON(acrtc->event); 7544 7545 acrtc->event = acrtc->base.state->event; 7546 7547 /* Set the flip status */ 7548 acrtc->pflip_status = AMDGPU_FLIP_SUBMITTED; 7549 7550 /* Mark this event as consumed */ 7551 acrtc->base.state->event = NULL; 7552 7553 DC_LOG_PFLIP("crtc:%d, pflip_stat:AMDGPU_FLIP_SUBMITTED\n", 7554 acrtc->crtc_id); 7555 } 7556 7557 static void update_freesync_state_on_stream( 7558 struct amdgpu_display_manager *dm, 7559 struct dm_crtc_state *new_crtc_state, 7560 struct dc_stream_state *new_stream, 7561 struct dc_plane_state *surface, 7562 u32 flip_timestamp_in_us) 7563 { 7564 struct mod_vrr_params vrr_params; 7565 struct dc_info_packet vrr_infopacket = {0}; 7566 struct amdgpu_device *adev = dm->adev; 7567 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(new_crtc_state->base.crtc); 7568 unsigned long flags; 7569 bool pack_sdp_v1_3 = false; 7570 7571 if (!new_stream) 7572 return; 7573 7574 /* 7575 * TODO: Determine why min/max totals and vrefresh can be 0 here. 7576 * For now it's sufficient to just guard against these conditions. 7577 */ 7578 7579 if (!new_stream->timing.h_total || !new_stream->timing.v_total) 7580 return; 7581 7582 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags); 7583 vrr_params = acrtc->dm_irq_params.vrr_params; 7584 7585 if (surface) { 7586 mod_freesync_handle_preflip( 7587 dm->freesync_module, 7588 surface, 7589 new_stream, 7590 flip_timestamp_in_us, 7591 &vrr_params); 7592 7593 if (adev->family < AMDGPU_FAMILY_AI && 7594 amdgpu_dm_vrr_active(new_crtc_state)) { 7595 mod_freesync_handle_v_update(dm->freesync_module, 7596 new_stream, &vrr_params); 7597 7598 /* Need to call this before the frame ends. */ 7599 dc_stream_adjust_vmin_vmax(dm->dc, 7600 new_crtc_state->stream, 7601 &vrr_params.adjust); 7602 } 7603 } 7604 7605 mod_freesync_build_vrr_infopacket( 7606 dm->freesync_module, 7607 new_stream, 7608 &vrr_params, 7609 PACKET_TYPE_VRR, 7610 TRANSFER_FUNC_UNKNOWN, 7611 &vrr_infopacket, 7612 pack_sdp_v1_3); 7613 7614 new_crtc_state->freesync_vrr_info_changed |= 7615 (memcmp(&new_crtc_state->vrr_infopacket, 7616 &vrr_infopacket, 7617 sizeof(vrr_infopacket)) != 0); 7618 7619 acrtc->dm_irq_params.vrr_params = vrr_params; 7620 new_crtc_state->vrr_infopacket = vrr_infopacket; 7621 7622 new_stream->vrr_infopacket = vrr_infopacket; 7623 7624 if (new_crtc_state->freesync_vrr_info_changed) 7625 DRM_DEBUG_KMS("VRR packet update: crtc=%u enabled=%d state=%d", 7626 new_crtc_state->base.crtc->base.id, 7627 (int)new_crtc_state->base.vrr_enabled, 7628 (int)vrr_params.state); 7629 7630 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags); 7631 } 7632 7633 static void update_stream_irq_parameters( 7634 struct amdgpu_display_manager *dm, 7635 struct dm_crtc_state *new_crtc_state) 7636 { 7637 struct dc_stream_state *new_stream = new_crtc_state->stream; 7638 struct mod_vrr_params vrr_params; 7639 struct mod_freesync_config config = new_crtc_state->freesync_config; 7640 struct amdgpu_device *adev = dm->adev; 7641 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(new_crtc_state->base.crtc); 7642 unsigned long flags; 7643 7644 if (!new_stream) 7645 return; 7646 7647 /* 7648 * TODO: Determine why min/max totals and vrefresh can be 0 here. 7649 * For now it's sufficient to just guard against these conditions. 7650 */ 7651 if (!new_stream->timing.h_total || !new_stream->timing.v_total) 7652 return; 7653 7654 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags); 7655 vrr_params = acrtc->dm_irq_params.vrr_params; 7656 7657 if (new_crtc_state->vrr_supported && 7658 config.min_refresh_in_uhz && 7659 config.max_refresh_in_uhz) { 7660 /* 7661 * if freesync compatible mode was set, config.state will be set 7662 * in atomic check 7663 */ 7664 if (config.state == VRR_STATE_ACTIVE_FIXED && config.fixed_refresh_in_uhz && 7665 (!drm_atomic_crtc_needs_modeset(&new_crtc_state->base) || 7666 new_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED)) { 7667 vrr_params.max_refresh_in_uhz = config.max_refresh_in_uhz; 7668 vrr_params.min_refresh_in_uhz = config.min_refresh_in_uhz; 7669 vrr_params.fixed_refresh_in_uhz = config.fixed_refresh_in_uhz; 7670 vrr_params.state = VRR_STATE_ACTIVE_FIXED; 7671 } else { 7672 config.state = new_crtc_state->base.vrr_enabled ? 7673 VRR_STATE_ACTIVE_VARIABLE : 7674 VRR_STATE_INACTIVE; 7675 } 7676 } else { 7677 config.state = VRR_STATE_UNSUPPORTED; 7678 } 7679 7680 mod_freesync_build_vrr_params(dm->freesync_module, 7681 new_stream, 7682 &config, &vrr_params); 7683 7684 new_crtc_state->freesync_config = config; 7685 /* Copy state for access from DM IRQ handler */ 7686 acrtc->dm_irq_params.freesync_config = config; 7687 acrtc->dm_irq_params.active_planes = new_crtc_state->active_planes; 7688 acrtc->dm_irq_params.vrr_params = vrr_params; 7689 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags); 7690 } 7691 7692 static void amdgpu_dm_handle_vrr_transition(struct dm_crtc_state *old_state, 7693 struct dm_crtc_state *new_state) 7694 { 7695 bool old_vrr_active = amdgpu_dm_vrr_active(old_state); 7696 bool new_vrr_active = amdgpu_dm_vrr_active(new_state); 7697 7698 if (!old_vrr_active && new_vrr_active) { 7699 /* Transition VRR inactive -> active: 7700 * While VRR is active, we must not disable vblank irq, as a 7701 * reenable after disable would compute bogus vblank/pflip 7702 * timestamps if it likely happened inside display front-porch. 7703 * 7704 * We also need vupdate irq for the actual core vblank handling 7705 * at end of vblank. 7706 */ 7707 WARN_ON(dm_set_vupdate_irq(new_state->base.crtc, true) != 0); 7708 WARN_ON(drm_crtc_vblank_get(new_state->base.crtc) != 0); 7709 DRM_DEBUG_DRIVER("%s: crtc=%u VRR off->on: Get vblank ref\n", 7710 __func__, new_state->base.crtc->base.id); 7711 } else if (old_vrr_active && !new_vrr_active) { 7712 /* Transition VRR active -> inactive: 7713 * Allow vblank irq disable again for fixed refresh rate. 7714 */ 7715 WARN_ON(dm_set_vupdate_irq(new_state->base.crtc, false) != 0); 7716 drm_crtc_vblank_put(new_state->base.crtc); 7717 DRM_DEBUG_DRIVER("%s: crtc=%u VRR on->off: Drop vblank ref\n", 7718 __func__, new_state->base.crtc->base.id); 7719 } 7720 } 7721 7722 static void amdgpu_dm_commit_cursors(struct drm_atomic_state *state) 7723 { 7724 struct drm_plane *plane; 7725 struct drm_plane_state *old_plane_state; 7726 int i; 7727 7728 /* 7729 * TODO: Make this per-stream so we don't issue redundant updates for 7730 * commits with multiple streams. 7731 */ 7732 for_each_old_plane_in_state(state, plane, old_plane_state, i) 7733 if (plane->type == DRM_PLANE_TYPE_CURSOR) 7734 handle_cursor_update(plane, old_plane_state); 7735 } 7736 7737 static inline uint32_t get_mem_type(struct drm_framebuffer *fb) 7738 { 7739 struct amdgpu_bo *abo = gem_to_amdgpu_bo(fb->obj[0]); 7740 7741 return abo->tbo.resource ? abo->tbo.resource->mem_type : 0; 7742 } 7743 7744 static void amdgpu_dm_commit_planes(struct drm_atomic_state *state, 7745 struct dc_state *dc_state, 7746 struct drm_device *dev, 7747 struct amdgpu_display_manager *dm, 7748 struct drm_crtc *pcrtc, 7749 bool wait_for_vblank) 7750 { 7751 u32 i; 7752 u64 timestamp_ns; 7753 struct drm_plane *plane; 7754 struct drm_plane_state *old_plane_state, *new_plane_state; 7755 struct amdgpu_crtc *acrtc_attach = to_amdgpu_crtc(pcrtc); 7756 struct drm_crtc_state *new_pcrtc_state = 7757 drm_atomic_get_new_crtc_state(state, pcrtc); 7758 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(new_pcrtc_state); 7759 struct dm_crtc_state *dm_old_crtc_state = 7760 to_dm_crtc_state(drm_atomic_get_old_crtc_state(state, pcrtc)); 7761 int planes_count = 0, vpos, hpos; 7762 unsigned long flags; 7763 u32 target_vblank, last_flip_vblank; 7764 bool vrr_active = amdgpu_dm_vrr_active(acrtc_state); 7765 bool cursor_update = false; 7766 bool pflip_present = false; 7767 struct { 7768 struct dc_surface_update surface_updates[MAX_SURFACES]; 7769 struct dc_plane_info plane_infos[MAX_SURFACES]; 7770 struct dc_scaling_info scaling_infos[MAX_SURFACES]; 7771 struct dc_flip_addrs flip_addrs[MAX_SURFACES]; 7772 struct dc_stream_update stream_update; 7773 } *bundle; 7774 7775 bundle = kzalloc(sizeof(*bundle), GFP_KERNEL); 7776 7777 if (!bundle) { 7778 dm_error("Failed to allocate update bundle\n"); 7779 goto cleanup; 7780 } 7781 7782 /* 7783 * Disable the cursor first if we're disabling all the planes. 7784 * It'll remain on the screen after the planes are re-enabled 7785 * if we don't. 7786 */ 7787 if (acrtc_state->active_planes == 0) 7788 amdgpu_dm_commit_cursors(state); 7789 7790 /* update planes when needed */ 7791 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) { 7792 struct drm_crtc *crtc = new_plane_state->crtc; 7793 struct drm_crtc_state *new_crtc_state; 7794 struct drm_framebuffer *fb = new_plane_state->fb; 7795 struct amdgpu_framebuffer *afb = (struct amdgpu_framebuffer *)fb; 7796 bool plane_needs_flip; 7797 struct dc_plane_state *dc_plane; 7798 struct dm_plane_state *dm_new_plane_state = to_dm_plane_state(new_plane_state); 7799 7800 /* Cursor plane is handled after stream updates */ 7801 if (plane->type == DRM_PLANE_TYPE_CURSOR) { 7802 if ((fb && crtc == pcrtc) || 7803 (old_plane_state->fb && old_plane_state->crtc == pcrtc)) 7804 cursor_update = true; 7805 7806 continue; 7807 } 7808 7809 if (!fb || !crtc || pcrtc != crtc) 7810 continue; 7811 7812 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc); 7813 if (!new_crtc_state->active) 7814 continue; 7815 7816 dc_plane = dm_new_plane_state->dc_state; 7817 if (!dc_plane) 7818 continue; 7819 7820 bundle->surface_updates[planes_count].surface = dc_plane; 7821 if (new_pcrtc_state->color_mgmt_changed) { 7822 bundle->surface_updates[planes_count].gamma = dc_plane->gamma_correction; 7823 bundle->surface_updates[planes_count].in_transfer_func = dc_plane->in_transfer_func; 7824 bundle->surface_updates[planes_count].gamut_remap_matrix = &dc_plane->gamut_remap_matrix; 7825 } 7826 7827 fill_dc_scaling_info(dm->adev, new_plane_state, 7828 &bundle->scaling_infos[planes_count]); 7829 7830 bundle->surface_updates[planes_count].scaling_info = 7831 &bundle->scaling_infos[planes_count]; 7832 7833 plane_needs_flip = old_plane_state->fb && new_plane_state->fb; 7834 7835 pflip_present = pflip_present || plane_needs_flip; 7836 7837 if (!plane_needs_flip) { 7838 planes_count += 1; 7839 continue; 7840 } 7841 7842 fill_dc_plane_info_and_addr( 7843 dm->adev, new_plane_state, 7844 afb->tiling_flags, 7845 &bundle->plane_infos[planes_count], 7846 &bundle->flip_addrs[planes_count].address, 7847 afb->tmz_surface, false); 7848 7849 drm_dbg_state(state->dev, "plane: id=%d dcc_en=%d\n", 7850 new_plane_state->plane->index, 7851 bundle->plane_infos[planes_count].dcc.enable); 7852 7853 bundle->surface_updates[planes_count].plane_info = 7854 &bundle->plane_infos[planes_count]; 7855 7856 if (acrtc_state->stream->link->psr_settings.psr_feature_enabled) 7857 fill_dc_dirty_rects(plane, old_plane_state, 7858 new_plane_state, new_crtc_state, 7859 &bundle->flip_addrs[planes_count]); 7860 7861 /* 7862 * Only allow immediate flips for fast updates that don't 7863 * change memory domain, FB pitch, DCC state, rotation or 7864 * mirroring. 7865 * 7866 * dm_crtc_helper_atomic_check() only accepts async flips with 7867 * fast updates. 7868 */ 7869 if (crtc->state->async_flip && 7870 (acrtc_state->update_type != UPDATE_TYPE_FAST || 7871 get_mem_type(old_plane_state->fb) != get_mem_type(fb))) 7872 drm_warn_once(state->dev, 7873 "[PLANE:%d:%s] async flip with non-fast update\n", 7874 plane->base.id, plane->name); 7875 7876 bundle->flip_addrs[planes_count].flip_immediate = 7877 crtc->state->async_flip && 7878 acrtc_state->update_type == UPDATE_TYPE_FAST && 7879 get_mem_type(old_plane_state->fb) == get_mem_type(fb); 7880 7881 timestamp_ns = ktime_get_ns(); 7882 bundle->flip_addrs[planes_count].flip_timestamp_in_us = div_u64(timestamp_ns, 1000); 7883 bundle->surface_updates[planes_count].flip_addr = &bundle->flip_addrs[planes_count]; 7884 bundle->surface_updates[planes_count].surface = dc_plane; 7885 7886 if (!bundle->surface_updates[planes_count].surface) { 7887 DRM_ERROR("No surface for CRTC: id=%d\n", 7888 acrtc_attach->crtc_id); 7889 continue; 7890 } 7891 7892 if (plane == pcrtc->primary) 7893 update_freesync_state_on_stream( 7894 dm, 7895 acrtc_state, 7896 acrtc_state->stream, 7897 dc_plane, 7898 bundle->flip_addrs[planes_count].flip_timestamp_in_us); 7899 7900 drm_dbg_state(state->dev, "%s Flipping to hi: 0x%x, low: 0x%x\n", 7901 __func__, 7902 bundle->flip_addrs[planes_count].address.grph.addr.high_part, 7903 bundle->flip_addrs[planes_count].address.grph.addr.low_part); 7904 7905 planes_count += 1; 7906 7907 } 7908 7909 if (pflip_present) { 7910 if (!vrr_active) { 7911 /* Use old throttling in non-vrr fixed refresh rate mode 7912 * to keep flip scheduling based on target vblank counts 7913 * working in a backwards compatible way, e.g., for 7914 * clients using the GLX_OML_sync_control extension or 7915 * DRI3/Present extension with defined target_msc. 7916 */ 7917 last_flip_vblank = amdgpu_get_vblank_counter_kms(pcrtc); 7918 } else { 7919 /* For variable refresh rate mode only: 7920 * Get vblank of last completed flip to avoid > 1 vrr 7921 * flips per video frame by use of throttling, but allow 7922 * flip programming anywhere in the possibly large 7923 * variable vrr vblank interval for fine-grained flip 7924 * timing control and more opportunity to avoid stutter 7925 * on late submission of flips. 7926 */ 7927 spin_lock_irqsave(&pcrtc->dev->event_lock, flags); 7928 last_flip_vblank = acrtc_attach->dm_irq_params.last_flip_vblank; 7929 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags); 7930 } 7931 7932 target_vblank = last_flip_vblank + wait_for_vblank; 7933 7934 /* 7935 * Wait until we're out of the vertical blank period before the one 7936 * targeted by the flip 7937 */ 7938 while ((acrtc_attach->enabled && 7939 (amdgpu_display_get_crtc_scanoutpos(dm->ddev, acrtc_attach->crtc_id, 7940 0, &vpos, &hpos, NULL, 7941 NULL, &pcrtc->hwmode) 7942 & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) == 7943 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) && 7944 (int)(target_vblank - 7945 amdgpu_get_vblank_counter_kms(pcrtc)) > 0)) { 7946 usleep_range(1000, 1100); 7947 } 7948 7949 /** 7950 * Prepare the flip event for the pageflip interrupt to handle. 7951 * 7952 * This only works in the case where we've already turned on the 7953 * appropriate hardware blocks (eg. HUBP) so in the transition case 7954 * from 0 -> n planes we have to skip a hardware generated event 7955 * and rely on sending it from software. 7956 */ 7957 if (acrtc_attach->base.state->event && 7958 acrtc_state->active_planes > 0) { 7959 drm_crtc_vblank_get(pcrtc); 7960 7961 spin_lock_irqsave(&pcrtc->dev->event_lock, flags); 7962 7963 WARN_ON(acrtc_attach->pflip_status != AMDGPU_FLIP_NONE); 7964 prepare_flip_isr(acrtc_attach); 7965 7966 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags); 7967 } 7968 7969 if (acrtc_state->stream) { 7970 if (acrtc_state->freesync_vrr_info_changed) 7971 bundle->stream_update.vrr_infopacket = 7972 &acrtc_state->stream->vrr_infopacket; 7973 } 7974 } else if (cursor_update && acrtc_state->active_planes > 0 && 7975 acrtc_attach->base.state->event) { 7976 drm_crtc_vblank_get(pcrtc); 7977 7978 spin_lock_irqsave(&pcrtc->dev->event_lock, flags); 7979 7980 acrtc_attach->event = acrtc_attach->base.state->event; 7981 acrtc_attach->base.state->event = NULL; 7982 7983 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags); 7984 } 7985 7986 /* Update the planes if changed or disable if we don't have any. */ 7987 if ((planes_count || acrtc_state->active_planes == 0) && 7988 acrtc_state->stream) { 7989 /* 7990 * If PSR or idle optimizations are enabled then flush out 7991 * any pending work before hardware programming. 7992 */ 7993 if (dm->vblank_control_workqueue) 7994 flush_workqueue(dm->vblank_control_workqueue); 7995 7996 bundle->stream_update.stream = acrtc_state->stream; 7997 if (new_pcrtc_state->mode_changed) { 7998 bundle->stream_update.src = acrtc_state->stream->src; 7999 bundle->stream_update.dst = acrtc_state->stream->dst; 8000 } 8001 8002 if (new_pcrtc_state->color_mgmt_changed) { 8003 /* 8004 * TODO: This isn't fully correct since we've actually 8005 * already modified the stream in place. 8006 */ 8007 bundle->stream_update.gamut_remap = 8008 &acrtc_state->stream->gamut_remap_matrix; 8009 bundle->stream_update.output_csc_transform = 8010 &acrtc_state->stream->csc_color_matrix; 8011 bundle->stream_update.out_transfer_func = 8012 acrtc_state->stream->out_transfer_func; 8013 } 8014 8015 acrtc_state->stream->abm_level = acrtc_state->abm_level; 8016 if (acrtc_state->abm_level != dm_old_crtc_state->abm_level) 8017 bundle->stream_update.abm_level = &acrtc_state->abm_level; 8018 8019 mutex_lock(&dm->dc_lock); 8020 if ((acrtc_state->update_type > UPDATE_TYPE_FAST) && 8021 acrtc_state->stream->link->psr_settings.psr_allow_active) 8022 amdgpu_dm_psr_disable(acrtc_state->stream); 8023 mutex_unlock(&dm->dc_lock); 8024 8025 /* 8026 * If FreeSync state on the stream has changed then we need to 8027 * re-adjust the min/max bounds now that DC doesn't handle this 8028 * as part of commit. 8029 */ 8030 if (is_dc_timing_adjust_needed(dm_old_crtc_state, acrtc_state)) { 8031 spin_lock_irqsave(&pcrtc->dev->event_lock, flags); 8032 dc_stream_adjust_vmin_vmax( 8033 dm->dc, acrtc_state->stream, 8034 &acrtc_attach->dm_irq_params.vrr_params.adjust); 8035 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags); 8036 } 8037 mutex_lock(&dm->dc_lock); 8038 update_planes_and_stream_adapter(dm->dc, 8039 acrtc_state->update_type, 8040 planes_count, 8041 acrtc_state->stream, 8042 &bundle->stream_update, 8043 bundle->surface_updates); 8044 8045 /** 8046 * Enable or disable the interrupts on the backend. 8047 * 8048 * Most pipes are put into power gating when unused. 8049 * 8050 * When power gating is enabled on a pipe we lose the 8051 * interrupt enablement state when power gating is disabled. 8052 * 8053 * So we need to update the IRQ control state in hardware 8054 * whenever the pipe turns on (since it could be previously 8055 * power gated) or off (since some pipes can't be power gated 8056 * on some ASICs). 8057 */ 8058 if (dm_old_crtc_state->active_planes != acrtc_state->active_planes) 8059 dm_update_pflip_irq_state(drm_to_adev(dev), 8060 acrtc_attach); 8061 8062 if ((acrtc_state->update_type > UPDATE_TYPE_FAST) && 8063 acrtc_state->stream->link->psr_settings.psr_version != DC_PSR_VERSION_UNSUPPORTED && 8064 !acrtc_state->stream->link->psr_settings.psr_feature_enabled) 8065 amdgpu_dm_link_setup_psr(acrtc_state->stream); 8066 8067 /* Decrement skip count when PSR is enabled and we're doing fast updates. */ 8068 if (acrtc_state->update_type == UPDATE_TYPE_FAST && 8069 acrtc_state->stream->link->psr_settings.psr_feature_enabled) { 8070 struct amdgpu_dm_connector *aconn = 8071 (struct amdgpu_dm_connector *)acrtc_state->stream->dm_stream_context; 8072 8073 if (aconn->psr_skip_count > 0) 8074 aconn->psr_skip_count--; 8075 8076 /* Allow PSR when skip count is 0. */ 8077 acrtc_attach->dm_irq_params.allow_psr_entry = !aconn->psr_skip_count; 8078 8079 /* 8080 * If sink supports PSR SU, there is no need to rely on 8081 * a vblank event disable request to enable PSR. PSR SU 8082 * can be enabled immediately once OS demonstrates an 8083 * adequate number of fast atomic commits to notify KMD 8084 * of update events. See `vblank_control_worker()`. 8085 */ 8086 if (acrtc_state->stream->link->psr_settings.psr_version >= DC_PSR_VERSION_SU_1 && 8087 acrtc_attach->dm_irq_params.allow_psr_entry && 8088 !acrtc_state->stream->link->psr_settings.psr_allow_active) 8089 amdgpu_dm_psr_enable(acrtc_state->stream); 8090 } else { 8091 acrtc_attach->dm_irq_params.allow_psr_entry = false; 8092 } 8093 8094 mutex_unlock(&dm->dc_lock); 8095 } 8096 8097 /* 8098 * Update cursor state *after* programming all the planes. 8099 * This avoids redundant programming in the case where we're going 8100 * to be disabling a single plane - those pipes are being disabled. 8101 */ 8102 if (acrtc_state->active_planes) 8103 amdgpu_dm_commit_cursors(state); 8104 8105 cleanup: 8106 kfree(bundle); 8107 } 8108 8109 static void amdgpu_dm_commit_audio(struct drm_device *dev, 8110 struct drm_atomic_state *state) 8111 { 8112 struct amdgpu_device *adev = drm_to_adev(dev); 8113 struct amdgpu_dm_connector *aconnector; 8114 struct drm_connector *connector; 8115 struct drm_connector_state *old_con_state, *new_con_state; 8116 struct drm_crtc_state *new_crtc_state; 8117 struct dm_crtc_state *new_dm_crtc_state; 8118 const struct dc_stream_status *status; 8119 int i, inst; 8120 8121 /* Notify device removals. */ 8122 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) { 8123 if (old_con_state->crtc != new_con_state->crtc) { 8124 /* CRTC changes require notification. */ 8125 goto notify; 8126 } 8127 8128 if (!new_con_state->crtc) 8129 continue; 8130 8131 new_crtc_state = drm_atomic_get_new_crtc_state( 8132 state, new_con_state->crtc); 8133 8134 if (!new_crtc_state) 8135 continue; 8136 8137 if (!drm_atomic_crtc_needs_modeset(new_crtc_state)) 8138 continue; 8139 8140 notify: 8141 aconnector = to_amdgpu_dm_connector(connector); 8142 8143 mutex_lock(&adev->dm.audio_lock); 8144 inst = aconnector->audio_inst; 8145 aconnector->audio_inst = -1; 8146 mutex_unlock(&adev->dm.audio_lock); 8147 8148 amdgpu_dm_audio_eld_notify(adev, inst); 8149 } 8150 8151 /* Notify audio device additions. */ 8152 for_each_new_connector_in_state(state, connector, new_con_state, i) { 8153 if (!new_con_state->crtc) 8154 continue; 8155 8156 new_crtc_state = drm_atomic_get_new_crtc_state( 8157 state, new_con_state->crtc); 8158 8159 if (!new_crtc_state) 8160 continue; 8161 8162 if (!drm_atomic_crtc_needs_modeset(new_crtc_state)) 8163 continue; 8164 8165 new_dm_crtc_state = to_dm_crtc_state(new_crtc_state); 8166 if (!new_dm_crtc_state->stream) 8167 continue; 8168 8169 status = dc_stream_get_status(new_dm_crtc_state->stream); 8170 if (!status) 8171 continue; 8172 8173 aconnector = to_amdgpu_dm_connector(connector); 8174 8175 mutex_lock(&adev->dm.audio_lock); 8176 inst = status->audio_inst; 8177 aconnector->audio_inst = inst; 8178 mutex_unlock(&adev->dm.audio_lock); 8179 8180 amdgpu_dm_audio_eld_notify(adev, inst); 8181 } 8182 } 8183 8184 /* 8185 * amdgpu_dm_crtc_copy_transient_flags - copy mirrored flags from DRM to DC 8186 * @crtc_state: the DRM CRTC state 8187 * @stream_state: the DC stream state. 8188 * 8189 * Copy the mirrored transient state flags from DRM, to DC. It is used to bring 8190 * a dc_stream_state's flags in sync with a drm_crtc_state's flags. 8191 */ 8192 static void amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state *crtc_state, 8193 struct dc_stream_state *stream_state) 8194 { 8195 stream_state->mode_changed = drm_atomic_crtc_needs_modeset(crtc_state); 8196 } 8197 8198 /** 8199 * amdgpu_dm_atomic_commit_tail() - AMDgpu DM's commit tail implementation. 8200 * @state: The atomic state to commit 8201 * 8202 * This will tell DC to commit the constructed DC state from atomic_check, 8203 * programming the hardware. Any failures here implies a hardware failure, since 8204 * atomic check should have filtered anything non-kosher. 8205 */ 8206 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state) 8207 { 8208 struct drm_device *dev = state->dev; 8209 struct amdgpu_device *adev = drm_to_adev(dev); 8210 struct amdgpu_display_manager *dm = &adev->dm; 8211 struct dm_atomic_state *dm_state; 8212 struct dc_state *dc_state = NULL, *dc_state_temp = NULL; 8213 u32 i, j; 8214 struct drm_crtc *crtc; 8215 struct drm_crtc_state *old_crtc_state, *new_crtc_state; 8216 unsigned long flags; 8217 bool wait_for_vblank = true; 8218 struct drm_connector *connector; 8219 struct drm_connector_state *old_con_state, *new_con_state; 8220 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state; 8221 int crtc_disable_count = 0; 8222 bool mode_set_reset_required = false; 8223 int r; 8224 8225 trace_amdgpu_dm_atomic_commit_tail_begin(state); 8226 8227 r = drm_atomic_helper_wait_for_fences(dev, state, false); 8228 if (unlikely(r)) 8229 DRM_ERROR("Waiting for fences timed out!"); 8230 8231 drm_atomic_helper_update_legacy_modeset_state(dev, state); 8232 drm_dp_mst_atomic_wait_for_dependencies(state); 8233 8234 dm_state = dm_atomic_get_new_state(state); 8235 if (dm_state && dm_state->context) { 8236 dc_state = dm_state->context; 8237 } else { 8238 /* No state changes, retain current state. */ 8239 dc_state_temp = dc_create_state(dm->dc); 8240 ASSERT(dc_state_temp); 8241 dc_state = dc_state_temp; 8242 dc_resource_state_copy_construct_current(dm->dc, dc_state); 8243 } 8244 8245 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, 8246 new_crtc_state, i) { 8247 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc); 8248 8249 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 8250 8251 if (old_crtc_state->active && 8252 (!new_crtc_state->active || 8253 drm_atomic_crtc_needs_modeset(new_crtc_state))) { 8254 manage_dm_interrupts(adev, acrtc, false); 8255 dc_stream_release(dm_old_crtc_state->stream); 8256 } 8257 } 8258 8259 drm_atomic_helper_calc_timestamping_constants(state); 8260 8261 /* update changed items */ 8262 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 8263 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc); 8264 8265 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 8266 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 8267 8268 drm_dbg_state(state->dev, 8269 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, planes_changed:%d, mode_changed:%d,active_changed:%d,connectors_changed:%d\n", 8270 acrtc->crtc_id, 8271 new_crtc_state->enable, 8272 new_crtc_state->active, 8273 new_crtc_state->planes_changed, 8274 new_crtc_state->mode_changed, 8275 new_crtc_state->active_changed, 8276 new_crtc_state->connectors_changed); 8277 8278 /* Disable cursor if disabling crtc */ 8279 if (old_crtc_state->active && !new_crtc_state->active) { 8280 struct dc_cursor_position position; 8281 8282 memset(&position, 0, sizeof(position)); 8283 mutex_lock(&dm->dc_lock); 8284 dc_stream_set_cursor_position(dm_old_crtc_state->stream, &position); 8285 mutex_unlock(&dm->dc_lock); 8286 } 8287 8288 /* Copy all transient state flags into dc state */ 8289 if (dm_new_crtc_state->stream) { 8290 amdgpu_dm_crtc_copy_transient_flags(&dm_new_crtc_state->base, 8291 dm_new_crtc_state->stream); 8292 } 8293 8294 /* handles headless hotplug case, updating new_state and 8295 * aconnector as needed 8296 */ 8297 8298 if (modeset_required(new_crtc_state, dm_new_crtc_state->stream, dm_old_crtc_state->stream)) { 8299 8300 DRM_DEBUG_ATOMIC("Atomic commit: SET crtc id %d: [%p]\n", acrtc->crtc_id, acrtc); 8301 8302 if (!dm_new_crtc_state->stream) { 8303 /* 8304 * this could happen because of issues with 8305 * userspace notifications delivery. 8306 * In this case userspace tries to set mode on 8307 * display which is disconnected in fact. 8308 * dc_sink is NULL in this case on aconnector. 8309 * We expect reset mode will come soon. 8310 * 8311 * This can also happen when unplug is done 8312 * during resume sequence ended 8313 * 8314 * In this case, we want to pretend we still 8315 * have a sink to keep the pipe running so that 8316 * hw state is consistent with the sw state 8317 */ 8318 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n", 8319 __func__, acrtc->base.base.id); 8320 continue; 8321 } 8322 8323 if (dm_old_crtc_state->stream) 8324 remove_stream(adev, acrtc, dm_old_crtc_state->stream); 8325 8326 pm_runtime_get_noresume(dev->dev); 8327 8328 acrtc->enabled = true; 8329 acrtc->hw_mode = new_crtc_state->mode; 8330 crtc->hwmode = new_crtc_state->mode; 8331 mode_set_reset_required = true; 8332 } else if (modereset_required(new_crtc_state)) { 8333 DRM_DEBUG_ATOMIC("Atomic commit: RESET. crtc id %d:[%p]\n", acrtc->crtc_id, acrtc); 8334 /* i.e. reset mode */ 8335 if (dm_old_crtc_state->stream) 8336 remove_stream(adev, acrtc, dm_old_crtc_state->stream); 8337 8338 mode_set_reset_required = true; 8339 } 8340 } /* for_each_crtc_in_state() */ 8341 8342 if (dc_state) { 8343 /* if there mode set or reset, disable eDP PSR */ 8344 if (mode_set_reset_required) { 8345 if (dm->vblank_control_workqueue) 8346 flush_workqueue(dm->vblank_control_workqueue); 8347 8348 amdgpu_dm_psr_disable_all(dm); 8349 } 8350 8351 dm_enable_per_frame_crtc_master_sync(dc_state); 8352 mutex_lock(&dm->dc_lock); 8353 WARN_ON(!dc_commit_state(dm->dc, dc_state)); 8354 8355 /* Allow idle optimization when vblank count is 0 for display off */ 8356 if (dm->active_vblank_irq_count == 0) 8357 dc_allow_idle_optimizations(dm->dc, true); 8358 mutex_unlock(&dm->dc_lock); 8359 } 8360 8361 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { 8362 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc); 8363 8364 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 8365 8366 if (dm_new_crtc_state->stream != NULL) { 8367 const struct dc_stream_status *status = 8368 dc_stream_get_status(dm_new_crtc_state->stream); 8369 8370 if (!status) 8371 status = dc_stream_get_status_from_state(dc_state, 8372 dm_new_crtc_state->stream); 8373 if (!status) 8374 DC_ERR("got no status for stream %p on acrtc%p\n", dm_new_crtc_state->stream, acrtc); 8375 else 8376 acrtc->otg_inst = status->primary_otg_inst; 8377 } 8378 } 8379 #ifdef CONFIG_DRM_AMD_DC_HDCP 8380 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) { 8381 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state); 8382 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc); 8383 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector); 8384 8385 if (!adev->dm.hdcp_workqueue) 8386 continue; 8387 8388 pr_debug("[HDCP_DM] -------------- i : %x ----------\n", i); 8389 8390 if (!connector) 8391 continue; 8392 8393 pr_debug("[HDCP_DM] connector->index: %x connect_status: %x dpms: %x\n", 8394 connector->index, connector->status, connector->dpms); 8395 pr_debug("[HDCP_DM] state protection old: %x new: %x\n", 8396 old_con_state->content_protection, new_con_state->content_protection); 8397 8398 if (aconnector->dc_sink) { 8399 if (aconnector->dc_sink->sink_signal != SIGNAL_TYPE_VIRTUAL && 8400 aconnector->dc_sink->sink_signal != SIGNAL_TYPE_NONE) { 8401 pr_debug("[HDCP_DM] pipe_ctx dispname=%s\n", 8402 aconnector->dc_sink->edid_caps.display_name); 8403 } 8404 } 8405 8406 new_crtc_state = NULL; 8407 old_crtc_state = NULL; 8408 8409 if (acrtc) { 8410 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base); 8411 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base); 8412 } 8413 8414 if (old_crtc_state) 8415 pr_debug("old crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n", 8416 old_crtc_state->enable, 8417 old_crtc_state->active, 8418 old_crtc_state->mode_changed, 8419 old_crtc_state->active_changed, 8420 old_crtc_state->connectors_changed); 8421 8422 if (new_crtc_state) 8423 pr_debug("NEW crtc en: %x a: %x m: %x a-chg: %x c-chg: %x\n", 8424 new_crtc_state->enable, 8425 new_crtc_state->active, 8426 new_crtc_state->mode_changed, 8427 new_crtc_state->active_changed, 8428 new_crtc_state->connectors_changed); 8429 } 8430 8431 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) { 8432 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state); 8433 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc); 8434 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector); 8435 8436 if (!adev->dm.hdcp_workqueue) 8437 continue; 8438 8439 new_crtc_state = NULL; 8440 old_crtc_state = NULL; 8441 8442 if (acrtc) { 8443 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base); 8444 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base); 8445 } 8446 8447 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 8448 8449 if (dm_new_crtc_state && dm_new_crtc_state->stream == NULL && 8450 connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED) { 8451 hdcp_reset_display(adev->dm.hdcp_workqueue, aconnector->dc_link->link_index); 8452 new_con_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED; 8453 dm_new_con_state->update_hdcp = true; 8454 continue; 8455 } 8456 8457 if (is_content_protection_different(new_crtc_state, old_crtc_state, new_con_state, 8458 old_con_state, connector, adev->dm.hdcp_workqueue)) { 8459 /* when display is unplugged from mst hub, connctor will 8460 * be destroyed within dm_dp_mst_connector_destroy. connector 8461 * hdcp perperties, like type, undesired, desired, enabled, 8462 * will be lost. So, save hdcp properties into hdcp_work within 8463 * amdgpu_dm_atomic_commit_tail. if the same display is 8464 * plugged back with same display index, its hdcp properties 8465 * will be retrieved from hdcp_work within dm_dp_mst_get_modes 8466 */ 8467 8468 bool enable_encryption = false; 8469 8470 if (new_con_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED) 8471 enable_encryption = true; 8472 8473 if (aconnector->dc_link && aconnector->dc_sink && 8474 aconnector->dc_link->type == dc_connection_mst_branch) { 8475 struct hdcp_workqueue *hdcp_work = adev->dm.hdcp_workqueue; 8476 struct hdcp_workqueue *hdcp_w = 8477 &hdcp_work[aconnector->dc_link->link_index]; 8478 8479 hdcp_w->hdcp_content_type[connector->index] = 8480 new_con_state->hdcp_content_type; 8481 hdcp_w->content_protection[connector->index] = 8482 new_con_state->content_protection; 8483 } 8484 8485 if (new_crtc_state && new_crtc_state->mode_changed && 8486 new_con_state->content_protection >= DRM_MODE_CONTENT_PROTECTION_DESIRED) 8487 enable_encryption = true; 8488 8489 DRM_INFO("[HDCP_DM] hdcp_update_display enable_encryption = %x\n", enable_encryption); 8490 8491 hdcp_update_display( 8492 adev->dm.hdcp_workqueue, aconnector->dc_link->link_index, aconnector, 8493 new_con_state->hdcp_content_type, enable_encryption); 8494 } 8495 } 8496 #endif 8497 8498 /* Handle connector state changes */ 8499 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) { 8500 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state); 8501 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state); 8502 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc); 8503 struct dc_surface_update dummy_updates[MAX_SURFACES]; 8504 struct dc_stream_update stream_update; 8505 struct dc_info_packet hdr_packet; 8506 struct dc_stream_status *status = NULL; 8507 bool abm_changed, hdr_changed, scaling_changed; 8508 8509 memset(&dummy_updates, 0, sizeof(dummy_updates)); 8510 memset(&stream_update, 0, sizeof(stream_update)); 8511 8512 if (acrtc) { 8513 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base); 8514 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base); 8515 } 8516 8517 /* Skip any modesets/resets */ 8518 if (!acrtc || drm_atomic_crtc_needs_modeset(new_crtc_state)) 8519 continue; 8520 8521 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 8522 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 8523 8524 scaling_changed = is_scaling_state_different(dm_new_con_state, 8525 dm_old_con_state); 8526 8527 abm_changed = dm_new_crtc_state->abm_level != 8528 dm_old_crtc_state->abm_level; 8529 8530 hdr_changed = 8531 !drm_connector_atomic_hdr_metadata_equal(old_con_state, new_con_state); 8532 8533 if (!scaling_changed && !abm_changed && !hdr_changed) 8534 continue; 8535 8536 stream_update.stream = dm_new_crtc_state->stream; 8537 if (scaling_changed) { 8538 update_stream_scaling_settings(&dm_new_con_state->base.crtc->mode, 8539 dm_new_con_state, dm_new_crtc_state->stream); 8540 8541 stream_update.src = dm_new_crtc_state->stream->src; 8542 stream_update.dst = dm_new_crtc_state->stream->dst; 8543 } 8544 8545 if (abm_changed) { 8546 dm_new_crtc_state->stream->abm_level = dm_new_crtc_state->abm_level; 8547 8548 stream_update.abm_level = &dm_new_crtc_state->abm_level; 8549 } 8550 8551 if (hdr_changed) { 8552 fill_hdr_info_packet(new_con_state, &hdr_packet); 8553 stream_update.hdr_static_metadata = &hdr_packet; 8554 } 8555 8556 status = dc_stream_get_status(dm_new_crtc_state->stream); 8557 8558 if (WARN_ON(!status)) 8559 continue; 8560 8561 WARN_ON(!status->plane_count); 8562 8563 /* 8564 * TODO: DC refuses to perform stream updates without a dc_surface_update. 8565 * Here we create an empty update on each plane. 8566 * To fix this, DC should permit updating only stream properties. 8567 */ 8568 for (j = 0; j < status->plane_count; j++) 8569 dummy_updates[j].surface = status->plane_states[0]; 8570 8571 8572 mutex_lock(&dm->dc_lock); 8573 dc_update_planes_and_stream(dm->dc, 8574 dummy_updates, 8575 status->plane_count, 8576 dm_new_crtc_state->stream, 8577 &stream_update); 8578 mutex_unlock(&dm->dc_lock); 8579 } 8580 8581 /** 8582 * Enable interrupts for CRTCs that are newly enabled or went through 8583 * a modeset. It was intentionally deferred until after the front end 8584 * state was modified to wait until the OTG was on and so the IRQ 8585 * handlers didn't access stale or invalid state. 8586 */ 8587 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 8588 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc); 8589 #ifdef CONFIG_DEBUG_FS 8590 enum amdgpu_dm_pipe_crc_source cur_crc_src; 8591 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY) 8592 struct crc_rd_work *crc_rd_wrk; 8593 #endif 8594 #endif 8595 /* Count number of newly disabled CRTCs for dropping PM refs later. */ 8596 if (old_crtc_state->active && !new_crtc_state->active) 8597 crtc_disable_count++; 8598 8599 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 8600 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 8601 8602 /* For freesync config update on crtc state and params for irq */ 8603 update_stream_irq_parameters(dm, dm_new_crtc_state); 8604 8605 #ifdef CONFIG_DEBUG_FS 8606 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY) 8607 crc_rd_wrk = dm->crc_rd_wrk; 8608 #endif 8609 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags); 8610 cur_crc_src = acrtc->dm_irq_params.crc_src; 8611 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags); 8612 #endif 8613 8614 if (new_crtc_state->active && 8615 (!old_crtc_state->active || 8616 drm_atomic_crtc_needs_modeset(new_crtc_state))) { 8617 dc_stream_retain(dm_new_crtc_state->stream); 8618 acrtc->dm_irq_params.stream = dm_new_crtc_state->stream; 8619 manage_dm_interrupts(adev, acrtc, true); 8620 } 8621 /* Handle vrr on->off / off->on transitions */ 8622 amdgpu_dm_handle_vrr_transition(dm_old_crtc_state, dm_new_crtc_state); 8623 8624 #ifdef CONFIG_DEBUG_FS 8625 if (new_crtc_state->active && 8626 (!old_crtc_state->active || 8627 drm_atomic_crtc_needs_modeset(new_crtc_state))) { 8628 /** 8629 * Frontend may have changed so reapply the CRC capture 8630 * settings for the stream. 8631 */ 8632 if (amdgpu_dm_is_valid_crc_source(cur_crc_src)) { 8633 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY) 8634 if (amdgpu_dm_crc_window_is_activated(crtc)) { 8635 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags); 8636 acrtc->dm_irq_params.crc_window.update_win = true; 8637 acrtc->dm_irq_params.crc_window.skip_frame_cnt = 2; 8638 spin_lock_irq(&crc_rd_wrk->crc_rd_work_lock); 8639 crc_rd_wrk->crtc = crtc; 8640 spin_unlock_irq(&crc_rd_wrk->crc_rd_work_lock); 8641 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags); 8642 } 8643 #endif 8644 if (amdgpu_dm_crtc_configure_crc_source( 8645 crtc, dm_new_crtc_state, cur_crc_src)) 8646 DRM_DEBUG_DRIVER("Failed to configure crc source"); 8647 } 8648 } 8649 #endif 8650 } 8651 8652 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j) 8653 if (new_crtc_state->async_flip) 8654 wait_for_vblank = false; 8655 8656 /* update planes when needed per crtc*/ 8657 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j) { 8658 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 8659 8660 if (dm_new_crtc_state->stream) 8661 amdgpu_dm_commit_planes(state, dc_state, dev, 8662 dm, crtc, wait_for_vblank); 8663 } 8664 8665 /* Update audio instances for each connector. */ 8666 amdgpu_dm_commit_audio(dev, state); 8667 8668 /* restore the backlight level */ 8669 for (i = 0; i < dm->num_of_edps; i++) { 8670 if (dm->backlight_dev[i] && 8671 (dm->actual_brightness[i] != dm->brightness[i])) 8672 amdgpu_dm_backlight_set_level(dm, i, dm->brightness[i]); 8673 } 8674 8675 /* 8676 * send vblank event on all events not handled in flip and 8677 * mark consumed event for drm_atomic_helper_commit_hw_done 8678 */ 8679 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags); 8680 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { 8681 8682 if (new_crtc_state->event) 8683 drm_send_event_locked(dev, &new_crtc_state->event->base); 8684 8685 new_crtc_state->event = NULL; 8686 } 8687 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags); 8688 8689 /* Signal HW programming completion */ 8690 drm_atomic_helper_commit_hw_done(state); 8691 8692 if (wait_for_vblank) 8693 drm_atomic_helper_wait_for_flip_done(dev, state); 8694 8695 drm_atomic_helper_cleanup_planes(dev, state); 8696 8697 /* return the stolen vga memory back to VRAM */ 8698 if (!adev->mman.keep_stolen_vga_memory) 8699 amdgpu_bo_free_kernel(&adev->mman.stolen_vga_memory, NULL, NULL); 8700 amdgpu_bo_free_kernel(&adev->mman.stolen_extended_memory, NULL, NULL); 8701 8702 /* 8703 * Finally, drop a runtime PM reference for each newly disabled CRTC, 8704 * so we can put the GPU into runtime suspend if we're not driving any 8705 * displays anymore 8706 */ 8707 for (i = 0; i < crtc_disable_count; i++) 8708 pm_runtime_put_autosuspend(dev->dev); 8709 pm_runtime_mark_last_busy(dev->dev); 8710 8711 if (dc_state_temp) 8712 dc_release_state(dc_state_temp); 8713 } 8714 8715 static int dm_force_atomic_commit(struct drm_connector *connector) 8716 { 8717 int ret = 0; 8718 struct drm_device *ddev = connector->dev; 8719 struct drm_atomic_state *state = drm_atomic_state_alloc(ddev); 8720 struct amdgpu_crtc *disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc); 8721 struct drm_plane *plane = disconnected_acrtc->base.primary; 8722 struct drm_connector_state *conn_state; 8723 struct drm_crtc_state *crtc_state; 8724 struct drm_plane_state *plane_state; 8725 8726 if (!state) 8727 return -ENOMEM; 8728 8729 state->acquire_ctx = ddev->mode_config.acquire_ctx; 8730 8731 /* Construct an atomic state to restore previous display setting */ 8732 8733 /* 8734 * Attach connectors to drm_atomic_state 8735 */ 8736 conn_state = drm_atomic_get_connector_state(state, connector); 8737 8738 ret = PTR_ERR_OR_ZERO(conn_state); 8739 if (ret) 8740 goto out; 8741 8742 /* Attach crtc to drm_atomic_state*/ 8743 crtc_state = drm_atomic_get_crtc_state(state, &disconnected_acrtc->base); 8744 8745 ret = PTR_ERR_OR_ZERO(crtc_state); 8746 if (ret) 8747 goto out; 8748 8749 /* force a restore */ 8750 crtc_state->mode_changed = true; 8751 8752 /* Attach plane to drm_atomic_state */ 8753 plane_state = drm_atomic_get_plane_state(state, plane); 8754 8755 ret = PTR_ERR_OR_ZERO(plane_state); 8756 if (ret) 8757 goto out; 8758 8759 /* Call commit internally with the state we just constructed */ 8760 ret = drm_atomic_commit(state); 8761 8762 out: 8763 drm_atomic_state_put(state); 8764 if (ret) 8765 DRM_ERROR("Restoring old state failed with %i\n", ret); 8766 8767 return ret; 8768 } 8769 8770 /* 8771 * This function handles all cases when set mode does not come upon hotplug. 8772 * This includes when a display is unplugged then plugged back into the 8773 * same port and when running without usermode desktop manager supprot 8774 */ 8775 void dm_restore_drm_connector_state(struct drm_device *dev, 8776 struct drm_connector *connector) 8777 { 8778 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector); 8779 struct amdgpu_crtc *disconnected_acrtc; 8780 struct dm_crtc_state *acrtc_state; 8781 8782 if (!aconnector->dc_sink || !connector->state || !connector->encoder) 8783 return; 8784 8785 disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc); 8786 if (!disconnected_acrtc) 8787 return; 8788 8789 acrtc_state = to_dm_crtc_state(disconnected_acrtc->base.state); 8790 if (!acrtc_state->stream) 8791 return; 8792 8793 /* 8794 * If the previous sink is not released and different from the current, 8795 * we deduce we are in a state where we can not rely on usermode call 8796 * to turn on the display, so we do it here 8797 */ 8798 if (acrtc_state->stream->sink != aconnector->dc_sink) 8799 dm_force_atomic_commit(&aconnector->base); 8800 } 8801 8802 /* 8803 * Grabs all modesetting locks to serialize against any blocking commits, 8804 * Waits for completion of all non blocking commits. 8805 */ 8806 static int do_aquire_global_lock(struct drm_device *dev, 8807 struct drm_atomic_state *state) 8808 { 8809 struct drm_crtc *crtc; 8810 struct drm_crtc_commit *commit; 8811 long ret; 8812 8813 /* 8814 * Adding all modeset locks to aquire_ctx will 8815 * ensure that when the framework release it the 8816 * extra locks we are locking here will get released to 8817 */ 8818 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx); 8819 if (ret) 8820 return ret; 8821 8822 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 8823 spin_lock(&crtc->commit_lock); 8824 commit = list_first_entry_or_null(&crtc->commit_list, 8825 struct drm_crtc_commit, commit_entry); 8826 if (commit) 8827 drm_crtc_commit_get(commit); 8828 spin_unlock(&crtc->commit_lock); 8829 8830 if (!commit) 8831 continue; 8832 8833 /* 8834 * Make sure all pending HW programming completed and 8835 * page flips done 8836 */ 8837 ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ); 8838 8839 if (ret > 0) 8840 ret = wait_for_completion_interruptible_timeout( 8841 &commit->flip_done, 10*HZ); 8842 8843 if (ret == 0) 8844 DRM_ERROR("[CRTC:%d:%s] hw_done or flip_done timed out\n", 8845 crtc->base.id, crtc->name); 8846 8847 drm_crtc_commit_put(commit); 8848 } 8849 8850 return ret < 0 ? ret : 0; 8851 } 8852 8853 static void get_freesync_config_for_crtc( 8854 struct dm_crtc_state *new_crtc_state, 8855 struct dm_connector_state *new_con_state) 8856 { 8857 struct mod_freesync_config config = {0}; 8858 struct amdgpu_dm_connector *aconnector = 8859 to_amdgpu_dm_connector(new_con_state->base.connector); 8860 struct drm_display_mode *mode = &new_crtc_state->base.mode; 8861 int vrefresh = drm_mode_vrefresh(mode); 8862 bool fs_vid_mode = false; 8863 8864 new_crtc_state->vrr_supported = new_con_state->freesync_capable && 8865 vrefresh >= aconnector->min_vfreq && 8866 vrefresh <= aconnector->max_vfreq; 8867 8868 if (new_crtc_state->vrr_supported) { 8869 new_crtc_state->stream->ignore_msa_timing_param = true; 8870 fs_vid_mode = new_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED; 8871 8872 config.min_refresh_in_uhz = aconnector->min_vfreq * 1000000; 8873 config.max_refresh_in_uhz = aconnector->max_vfreq * 1000000; 8874 config.vsif_supported = true; 8875 config.btr = true; 8876 8877 if (fs_vid_mode) { 8878 config.state = VRR_STATE_ACTIVE_FIXED; 8879 config.fixed_refresh_in_uhz = new_crtc_state->freesync_config.fixed_refresh_in_uhz; 8880 goto out; 8881 } else if (new_crtc_state->base.vrr_enabled) { 8882 config.state = VRR_STATE_ACTIVE_VARIABLE; 8883 } else { 8884 config.state = VRR_STATE_INACTIVE; 8885 } 8886 } 8887 out: 8888 new_crtc_state->freesync_config = config; 8889 } 8890 8891 static void reset_freesync_config_for_crtc( 8892 struct dm_crtc_state *new_crtc_state) 8893 { 8894 new_crtc_state->vrr_supported = false; 8895 8896 memset(&new_crtc_state->vrr_infopacket, 0, 8897 sizeof(new_crtc_state->vrr_infopacket)); 8898 } 8899 8900 static bool 8901 is_timing_unchanged_for_freesync(struct drm_crtc_state *old_crtc_state, 8902 struct drm_crtc_state *new_crtc_state) 8903 { 8904 const struct drm_display_mode *old_mode, *new_mode; 8905 8906 if (!old_crtc_state || !new_crtc_state) 8907 return false; 8908 8909 old_mode = &old_crtc_state->mode; 8910 new_mode = &new_crtc_state->mode; 8911 8912 if (old_mode->clock == new_mode->clock && 8913 old_mode->hdisplay == new_mode->hdisplay && 8914 old_mode->vdisplay == new_mode->vdisplay && 8915 old_mode->htotal == new_mode->htotal && 8916 old_mode->vtotal != new_mode->vtotal && 8917 old_mode->hsync_start == new_mode->hsync_start && 8918 old_mode->vsync_start != new_mode->vsync_start && 8919 old_mode->hsync_end == new_mode->hsync_end && 8920 old_mode->vsync_end != new_mode->vsync_end && 8921 old_mode->hskew == new_mode->hskew && 8922 old_mode->vscan == new_mode->vscan && 8923 (old_mode->vsync_end - old_mode->vsync_start) == 8924 (new_mode->vsync_end - new_mode->vsync_start)) 8925 return true; 8926 8927 return false; 8928 } 8929 8930 static void set_freesync_fixed_config(struct dm_crtc_state *dm_new_crtc_state) 8931 { 8932 u64 num, den, res; 8933 struct drm_crtc_state *new_crtc_state = &dm_new_crtc_state->base; 8934 8935 dm_new_crtc_state->freesync_config.state = VRR_STATE_ACTIVE_FIXED; 8936 8937 num = (unsigned long long)new_crtc_state->mode.clock * 1000 * 1000000; 8938 den = (unsigned long long)new_crtc_state->mode.htotal * 8939 (unsigned long long)new_crtc_state->mode.vtotal; 8940 8941 res = div_u64(num, den); 8942 dm_new_crtc_state->freesync_config.fixed_refresh_in_uhz = res; 8943 } 8944 8945 static int dm_update_crtc_state(struct amdgpu_display_manager *dm, 8946 struct drm_atomic_state *state, 8947 struct drm_crtc *crtc, 8948 struct drm_crtc_state *old_crtc_state, 8949 struct drm_crtc_state *new_crtc_state, 8950 bool enable, 8951 bool *lock_and_validation_needed) 8952 { 8953 struct dm_atomic_state *dm_state = NULL; 8954 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state; 8955 struct dc_stream_state *new_stream; 8956 int ret = 0; 8957 8958 /* 8959 * TODO Move this code into dm_crtc_atomic_check once we get rid of dc_validation_set 8960 * update changed items 8961 */ 8962 struct amdgpu_crtc *acrtc = NULL; 8963 struct amdgpu_dm_connector *aconnector = NULL; 8964 struct drm_connector_state *drm_new_conn_state = NULL, *drm_old_conn_state = NULL; 8965 struct dm_connector_state *dm_new_conn_state = NULL, *dm_old_conn_state = NULL; 8966 8967 new_stream = NULL; 8968 8969 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 8970 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 8971 acrtc = to_amdgpu_crtc(crtc); 8972 aconnector = amdgpu_dm_find_first_crtc_matching_connector(state, crtc); 8973 8974 /* TODO This hack should go away */ 8975 if (aconnector && enable) { 8976 /* Make sure fake sink is created in plug-in scenario */ 8977 drm_new_conn_state = drm_atomic_get_new_connector_state(state, 8978 &aconnector->base); 8979 drm_old_conn_state = drm_atomic_get_old_connector_state(state, 8980 &aconnector->base); 8981 8982 if (IS_ERR(drm_new_conn_state)) { 8983 ret = PTR_ERR_OR_ZERO(drm_new_conn_state); 8984 goto fail; 8985 } 8986 8987 dm_new_conn_state = to_dm_connector_state(drm_new_conn_state); 8988 dm_old_conn_state = to_dm_connector_state(drm_old_conn_state); 8989 8990 if (!drm_atomic_crtc_needs_modeset(new_crtc_state)) 8991 goto skip_modeset; 8992 8993 new_stream = create_validate_stream_for_sink(aconnector, 8994 &new_crtc_state->mode, 8995 dm_new_conn_state, 8996 dm_old_crtc_state->stream); 8997 8998 /* 8999 * we can have no stream on ACTION_SET if a display 9000 * was disconnected during S3, in this case it is not an 9001 * error, the OS will be updated after detection, and 9002 * will do the right thing on next atomic commit 9003 */ 9004 9005 if (!new_stream) { 9006 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n", 9007 __func__, acrtc->base.base.id); 9008 ret = -ENOMEM; 9009 goto fail; 9010 } 9011 9012 /* 9013 * TODO: Check VSDB bits to decide whether this should 9014 * be enabled or not. 9015 */ 9016 new_stream->triggered_crtc_reset.enabled = 9017 dm->force_timing_sync; 9018 9019 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level; 9020 9021 ret = fill_hdr_info_packet(drm_new_conn_state, 9022 &new_stream->hdr_static_metadata); 9023 if (ret) 9024 goto fail; 9025 9026 /* 9027 * If we already removed the old stream from the context 9028 * (and set the new stream to NULL) then we can't reuse 9029 * the old stream even if the stream and scaling are unchanged. 9030 * We'll hit the BUG_ON and black screen. 9031 * 9032 * TODO: Refactor this function to allow this check to work 9033 * in all conditions. 9034 */ 9035 if (dm_new_crtc_state->stream && 9036 is_timing_unchanged_for_freesync(new_crtc_state, old_crtc_state)) 9037 goto skip_modeset; 9038 9039 if (dm_new_crtc_state->stream && 9040 dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) && 9041 dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream)) { 9042 new_crtc_state->mode_changed = false; 9043 DRM_DEBUG_DRIVER("Mode change not required, setting mode_changed to %d", 9044 new_crtc_state->mode_changed); 9045 } 9046 } 9047 9048 /* mode_changed flag may get updated above, need to check again */ 9049 if (!drm_atomic_crtc_needs_modeset(new_crtc_state)) 9050 goto skip_modeset; 9051 9052 drm_dbg_state(state->dev, 9053 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, planes_changed:%d, mode_changed:%d,active_changed:%d,connectors_changed:%d\n", 9054 acrtc->crtc_id, 9055 new_crtc_state->enable, 9056 new_crtc_state->active, 9057 new_crtc_state->planes_changed, 9058 new_crtc_state->mode_changed, 9059 new_crtc_state->active_changed, 9060 new_crtc_state->connectors_changed); 9061 9062 /* Remove stream for any changed/disabled CRTC */ 9063 if (!enable) { 9064 9065 if (!dm_old_crtc_state->stream) 9066 goto skip_modeset; 9067 9068 /* Unset freesync video if it was active before */ 9069 if (dm_old_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED) { 9070 dm_new_crtc_state->freesync_config.state = VRR_STATE_INACTIVE; 9071 dm_new_crtc_state->freesync_config.fixed_refresh_in_uhz = 0; 9072 } 9073 9074 /* Now check if we should set freesync video mode */ 9075 if (dm_new_crtc_state->stream && 9076 dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) && 9077 dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream) && 9078 is_timing_unchanged_for_freesync(new_crtc_state, 9079 old_crtc_state)) { 9080 new_crtc_state->mode_changed = false; 9081 DRM_DEBUG_DRIVER( 9082 "Mode change not required for front porch change, setting mode_changed to %d", 9083 new_crtc_state->mode_changed); 9084 9085 set_freesync_fixed_config(dm_new_crtc_state); 9086 9087 goto skip_modeset; 9088 } else if (aconnector && 9089 is_freesync_video_mode(&new_crtc_state->mode, 9090 aconnector)) { 9091 struct drm_display_mode *high_mode; 9092 9093 high_mode = get_highest_refresh_rate_mode(aconnector, false); 9094 if (!drm_mode_equal(&new_crtc_state->mode, high_mode)) 9095 set_freesync_fixed_config(dm_new_crtc_state); 9096 } 9097 9098 ret = dm_atomic_get_state(state, &dm_state); 9099 if (ret) 9100 goto fail; 9101 9102 DRM_DEBUG_DRIVER("Disabling DRM crtc: %d\n", 9103 crtc->base.id); 9104 9105 /* i.e. reset mode */ 9106 if (dc_remove_stream_from_ctx( 9107 dm->dc, 9108 dm_state->context, 9109 dm_old_crtc_state->stream) != DC_OK) { 9110 ret = -EINVAL; 9111 goto fail; 9112 } 9113 9114 dc_stream_release(dm_old_crtc_state->stream); 9115 dm_new_crtc_state->stream = NULL; 9116 9117 reset_freesync_config_for_crtc(dm_new_crtc_state); 9118 9119 *lock_and_validation_needed = true; 9120 9121 } else {/* Add stream for any updated/enabled CRTC */ 9122 /* 9123 * Quick fix to prevent NULL pointer on new_stream when 9124 * added MST connectors not found in existing crtc_state in the chained mode 9125 * TODO: need to dig out the root cause of that 9126 */ 9127 if (!aconnector) 9128 goto skip_modeset; 9129 9130 if (modereset_required(new_crtc_state)) 9131 goto skip_modeset; 9132 9133 if (modeset_required(new_crtc_state, new_stream, 9134 dm_old_crtc_state->stream)) { 9135 9136 WARN_ON(dm_new_crtc_state->stream); 9137 9138 ret = dm_atomic_get_state(state, &dm_state); 9139 if (ret) 9140 goto fail; 9141 9142 dm_new_crtc_state->stream = new_stream; 9143 9144 dc_stream_retain(new_stream); 9145 9146 DRM_DEBUG_ATOMIC("Enabling DRM crtc: %d\n", 9147 crtc->base.id); 9148 9149 if (dc_add_stream_to_ctx( 9150 dm->dc, 9151 dm_state->context, 9152 dm_new_crtc_state->stream) != DC_OK) { 9153 ret = -EINVAL; 9154 goto fail; 9155 } 9156 9157 *lock_and_validation_needed = true; 9158 } 9159 } 9160 9161 skip_modeset: 9162 /* Release extra reference */ 9163 if (new_stream) 9164 dc_stream_release(new_stream); 9165 9166 /* 9167 * We want to do dc stream updates that do not require a 9168 * full modeset below. 9169 */ 9170 if (!(enable && aconnector && new_crtc_state->active)) 9171 return 0; 9172 /* 9173 * Given above conditions, the dc state cannot be NULL because: 9174 * 1. We're in the process of enabling CRTCs (just been added 9175 * to the dc context, or already is on the context) 9176 * 2. Has a valid connector attached, and 9177 * 3. Is currently active and enabled. 9178 * => The dc stream state currently exists. 9179 */ 9180 BUG_ON(dm_new_crtc_state->stream == NULL); 9181 9182 /* Scaling or underscan settings */ 9183 if (is_scaling_state_different(dm_old_conn_state, dm_new_conn_state) || 9184 drm_atomic_crtc_needs_modeset(new_crtc_state)) 9185 update_stream_scaling_settings( 9186 &new_crtc_state->mode, dm_new_conn_state, dm_new_crtc_state->stream); 9187 9188 /* ABM settings */ 9189 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level; 9190 9191 /* 9192 * Color management settings. We also update color properties 9193 * when a modeset is needed, to ensure it gets reprogrammed. 9194 */ 9195 if (dm_new_crtc_state->base.color_mgmt_changed || 9196 drm_atomic_crtc_needs_modeset(new_crtc_state)) { 9197 ret = amdgpu_dm_update_crtc_color_mgmt(dm_new_crtc_state); 9198 if (ret) 9199 goto fail; 9200 } 9201 9202 /* Update Freesync settings. */ 9203 get_freesync_config_for_crtc(dm_new_crtc_state, 9204 dm_new_conn_state); 9205 9206 return ret; 9207 9208 fail: 9209 if (new_stream) 9210 dc_stream_release(new_stream); 9211 return ret; 9212 } 9213 9214 static bool should_reset_plane(struct drm_atomic_state *state, 9215 struct drm_plane *plane, 9216 struct drm_plane_state *old_plane_state, 9217 struct drm_plane_state *new_plane_state) 9218 { 9219 struct drm_plane *other; 9220 struct drm_plane_state *old_other_state, *new_other_state; 9221 struct drm_crtc_state *new_crtc_state; 9222 int i; 9223 9224 /* 9225 * TODO: Remove this hack once the checks below are sufficient 9226 * enough to determine when we need to reset all the planes on 9227 * the stream. 9228 */ 9229 if (state->allow_modeset) 9230 return true; 9231 9232 /* Exit early if we know that we're adding or removing the plane. */ 9233 if (old_plane_state->crtc != new_plane_state->crtc) 9234 return true; 9235 9236 /* old crtc == new_crtc == NULL, plane not in context. */ 9237 if (!new_plane_state->crtc) 9238 return false; 9239 9240 new_crtc_state = 9241 drm_atomic_get_new_crtc_state(state, new_plane_state->crtc); 9242 9243 if (!new_crtc_state) 9244 return true; 9245 9246 /* CRTC Degamma changes currently require us to recreate planes. */ 9247 if (new_crtc_state->color_mgmt_changed) 9248 return true; 9249 9250 if (drm_atomic_crtc_needs_modeset(new_crtc_state)) 9251 return true; 9252 9253 /* 9254 * If there are any new primary or overlay planes being added or 9255 * removed then the z-order can potentially change. To ensure 9256 * correct z-order and pipe acquisition the current DC architecture 9257 * requires us to remove and recreate all existing planes. 9258 * 9259 * TODO: Come up with a more elegant solution for this. 9260 */ 9261 for_each_oldnew_plane_in_state(state, other, old_other_state, new_other_state, i) { 9262 struct amdgpu_framebuffer *old_afb, *new_afb; 9263 9264 if (other->type == DRM_PLANE_TYPE_CURSOR) 9265 continue; 9266 9267 if (old_other_state->crtc != new_plane_state->crtc && 9268 new_other_state->crtc != new_plane_state->crtc) 9269 continue; 9270 9271 if (old_other_state->crtc != new_other_state->crtc) 9272 return true; 9273 9274 /* Src/dst size and scaling updates. */ 9275 if (old_other_state->src_w != new_other_state->src_w || 9276 old_other_state->src_h != new_other_state->src_h || 9277 old_other_state->crtc_w != new_other_state->crtc_w || 9278 old_other_state->crtc_h != new_other_state->crtc_h) 9279 return true; 9280 9281 /* Rotation / mirroring updates. */ 9282 if (old_other_state->rotation != new_other_state->rotation) 9283 return true; 9284 9285 /* Blending updates. */ 9286 if (old_other_state->pixel_blend_mode != 9287 new_other_state->pixel_blend_mode) 9288 return true; 9289 9290 /* Alpha updates. */ 9291 if (old_other_state->alpha != new_other_state->alpha) 9292 return true; 9293 9294 /* Colorspace changes. */ 9295 if (old_other_state->color_range != new_other_state->color_range || 9296 old_other_state->color_encoding != new_other_state->color_encoding) 9297 return true; 9298 9299 /* Framebuffer checks fall at the end. */ 9300 if (!old_other_state->fb || !new_other_state->fb) 9301 continue; 9302 9303 /* Pixel format changes can require bandwidth updates. */ 9304 if (old_other_state->fb->format != new_other_state->fb->format) 9305 return true; 9306 9307 old_afb = (struct amdgpu_framebuffer *)old_other_state->fb; 9308 new_afb = (struct amdgpu_framebuffer *)new_other_state->fb; 9309 9310 /* Tiling and DCC changes also require bandwidth updates. */ 9311 if (old_afb->tiling_flags != new_afb->tiling_flags || 9312 old_afb->base.modifier != new_afb->base.modifier) 9313 return true; 9314 } 9315 9316 return false; 9317 } 9318 9319 static int dm_check_cursor_fb(struct amdgpu_crtc *new_acrtc, 9320 struct drm_plane_state *new_plane_state, 9321 struct drm_framebuffer *fb) 9322 { 9323 struct amdgpu_device *adev = drm_to_adev(new_acrtc->base.dev); 9324 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(fb); 9325 unsigned int pitch; 9326 bool linear; 9327 9328 if (fb->width > new_acrtc->max_cursor_width || 9329 fb->height > new_acrtc->max_cursor_height) { 9330 DRM_DEBUG_ATOMIC("Bad cursor FB size %dx%d\n", 9331 new_plane_state->fb->width, 9332 new_plane_state->fb->height); 9333 return -EINVAL; 9334 } 9335 if (new_plane_state->src_w != fb->width << 16 || 9336 new_plane_state->src_h != fb->height << 16) { 9337 DRM_DEBUG_ATOMIC("Cropping not supported for cursor plane\n"); 9338 return -EINVAL; 9339 } 9340 9341 /* Pitch in pixels */ 9342 pitch = fb->pitches[0] / fb->format->cpp[0]; 9343 9344 if (fb->width != pitch) { 9345 DRM_DEBUG_ATOMIC("Cursor FB width %d doesn't match pitch %d", 9346 fb->width, pitch); 9347 return -EINVAL; 9348 } 9349 9350 switch (pitch) { 9351 case 64: 9352 case 128: 9353 case 256: 9354 /* FB pitch is supported by cursor plane */ 9355 break; 9356 default: 9357 DRM_DEBUG_ATOMIC("Bad cursor FB pitch %d px\n", pitch); 9358 return -EINVAL; 9359 } 9360 9361 /* Core DRM takes care of checking FB modifiers, so we only need to 9362 * check tiling flags when the FB doesn't have a modifier. 9363 */ 9364 if (!(fb->flags & DRM_MODE_FB_MODIFIERS)) { 9365 if (adev->family < AMDGPU_FAMILY_AI) { 9366 linear = AMDGPU_TILING_GET(afb->tiling_flags, ARRAY_MODE) != DC_ARRAY_2D_TILED_THIN1 && 9367 AMDGPU_TILING_GET(afb->tiling_flags, ARRAY_MODE) != DC_ARRAY_1D_TILED_THIN1 && 9368 AMDGPU_TILING_GET(afb->tiling_flags, MICRO_TILE_MODE) == 0; 9369 } else { 9370 linear = AMDGPU_TILING_GET(afb->tiling_flags, SWIZZLE_MODE) == 0; 9371 } 9372 if (!linear) { 9373 DRM_DEBUG_ATOMIC("Cursor FB not linear"); 9374 return -EINVAL; 9375 } 9376 } 9377 9378 return 0; 9379 } 9380 9381 static int dm_update_plane_state(struct dc *dc, 9382 struct drm_atomic_state *state, 9383 struct drm_plane *plane, 9384 struct drm_plane_state *old_plane_state, 9385 struct drm_plane_state *new_plane_state, 9386 bool enable, 9387 bool *lock_and_validation_needed) 9388 { 9389 9390 struct dm_atomic_state *dm_state = NULL; 9391 struct drm_crtc *new_plane_crtc, *old_plane_crtc; 9392 struct drm_crtc_state *old_crtc_state, *new_crtc_state; 9393 struct dm_crtc_state *dm_new_crtc_state, *dm_old_crtc_state; 9394 struct dm_plane_state *dm_new_plane_state, *dm_old_plane_state; 9395 struct amdgpu_crtc *new_acrtc; 9396 bool needs_reset; 9397 int ret = 0; 9398 9399 9400 new_plane_crtc = new_plane_state->crtc; 9401 old_plane_crtc = old_plane_state->crtc; 9402 dm_new_plane_state = to_dm_plane_state(new_plane_state); 9403 dm_old_plane_state = to_dm_plane_state(old_plane_state); 9404 9405 if (plane->type == DRM_PLANE_TYPE_CURSOR) { 9406 if (!enable || !new_plane_crtc || 9407 drm_atomic_plane_disabling(plane->state, new_plane_state)) 9408 return 0; 9409 9410 new_acrtc = to_amdgpu_crtc(new_plane_crtc); 9411 9412 if (new_plane_state->src_x != 0 || new_plane_state->src_y != 0) { 9413 DRM_DEBUG_ATOMIC("Cropping not supported for cursor plane\n"); 9414 return -EINVAL; 9415 } 9416 9417 if (new_plane_state->fb) { 9418 ret = dm_check_cursor_fb(new_acrtc, new_plane_state, 9419 new_plane_state->fb); 9420 if (ret) 9421 return ret; 9422 } 9423 9424 return 0; 9425 } 9426 9427 needs_reset = should_reset_plane(state, plane, old_plane_state, 9428 new_plane_state); 9429 9430 /* Remove any changed/removed planes */ 9431 if (!enable) { 9432 if (!needs_reset) 9433 return 0; 9434 9435 if (!old_plane_crtc) 9436 return 0; 9437 9438 old_crtc_state = drm_atomic_get_old_crtc_state( 9439 state, old_plane_crtc); 9440 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 9441 9442 if (!dm_old_crtc_state->stream) 9443 return 0; 9444 9445 DRM_DEBUG_ATOMIC("Disabling DRM plane: %d on DRM crtc %d\n", 9446 plane->base.id, old_plane_crtc->base.id); 9447 9448 ret = dm_atomic_get_state(state, &dm_state); 9449 if (ret) 9450 return ret; 9451 9452 if (!dc_remove_plane_from_context( 9453 dc, 9454 dm_old_crtc_state->stream, 9455 dm_old_plane_state->dc_state, 9456 dm_state->context)) { 9457 9458 return -EINVAL; 9459 } 9460 9461 if (dm_old_plane_state->dc_state) 9462 dc_plane_state_release(dm_old_plane_state->dc_state); 9463 9464 dm_new_plane_state->dc_state = NULL; 9465 9466 *lock_and_validation_needed = true; 9467 9468 } else { /* Add new planes */ 9469 struct dc_plane_state *dc_new_plane_state; 9470 9471 if (drm_atomic_plane_disabling(plane->state, new_plane_state)) 9472 return 0; 9473 9474 if (!new_plane_crtc) 9475 return 0; 9476 9477 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_crtc); 9478 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 9479 9480 if (!dm_new_crtc_state->stream) 9481 return 0; 9482 9483 if (!needs_reset) 9484 return 0; 9485 9486 ret = dm_plane_helper_check_state(new_plane_state, new_crtc_state); 9487 if (ret) 9488 return ret; 9489 9490 WARN_ON(dm_new_plane_state->dc_state); 9491 9492 dc_new_plane_state = dc_create_plane_state(dc); 9493 if (!dc_new_plane_state) 9494 return -ENOMEM; 9495 9496 DRM_DEBUG_ATOMIC("Enabling DRM plane: %d on DRM crtc %d\n", 9497 plane->base.id, new_plane_crtc->base.id); 9498 9499 ret = fill_dc_plane_attributes( 9500 drm_to_adev(new_plane_crtc->dev), 9501 dc_new_plane_state, 9502 new_plane_state, 9503 new_crtc_state); 9504 if (ret) { 9505 dc_plane_state_release(dc_new_plane_state); 9506 return ret; 9507 } 9508 9509 ret = dm_atomic_get_state(state, &dm_state); 9510 if (ret) { 9511 dc_plane_state_release(dc_new_plane_state); 9512 return ret; 9513 } 9514 9515 /* 9516 * Any atomic check errors that occur after this will 9517 * not need a release. The plane state will be attached 9518 * to the stream, and therefore part of the atomic 9519 * state. It'll be released when the atomic state is 9520 * cleaned. 9521 */ 9522 if (!dc_add_plane_to_context( 9523 dc, 9524 dm_new_crtc_state->stream, 9525 dc_new_plane_state, 9526 dm_state->context)) { 9527 9528 dc_plane_state_release(dc_new_plane_state); 9529 return -EINVAL; 9530 } 9531 9532 dm_new_plane_state->dc_state = dc_new_plane_state; 9533 9534 dm_new_crtc_state->mpo_requested |= (plane->type == DRM_PLANE_TYPE_OVERLAY); 9535 9536 /* Tell DC to do a full surface update every time there 9537 * is a plane change. Inefficient, but works for now. 9538 */ 9539 dm_new_plane_state->dc_state->update_flags.bits.full_update = 1; 9540 9541 *lock_and_validation_needed = true; 9542 } 9543 9544 9545 return ret; 9546 } 9547 9548 static void dm_get_oriented_plane_size(struct drm_plane_state *plane_state, 9549 int *src_w, int *src_h) 9550 { 9551 switch (plane_state->rotation & DRM_MODE_ROTATE_MASK) { 9552 case DRM_MODE_ROTATE_90: 9553 case DRM_MODE_ROTATE_270: 9554 *src_w = plane_state->src_h >> 16; 9555 *src_h = plane_state->src_w >> 16; 9556 break; 9557 case DRM_MODE_ROTATE_0: 9558 case DRM_MODE_ROTATE_180: 9559 default: 9560 *src_w = plane_state->src_w >> 16; 9561 *src_h = plane_state->src_h >> 16; 9562 break; 9563 } 9564 } 9565 9566 static int dm_check_crtc_cursor(struct drm_atomic_state *state, 9567 struct drm_crtc *crtc, 9568 struct drm_crtc_state *new_crtc_state) 9569 { 9570 struct drm_plane *cursor = crtc->cursor, *underlying; 9571 struct drm_plane_state *new_cursor_state, *new_underlying_state; 9572 int i; 9573 int cursor_scale_w, cursor_scale_h, underlying_scale_w, underlying_scale_h; 9574 int cursor_src_w, cursor_src_h; 9575 int underlying_src_w, underlying_src_h; 9576 9577 /* On DCE and DCN there is no dedicated hardware cursor plane. We get a 9578 * cursor per pipe but it's going to inherit the scaling and 9579 * positioning from the underlying pipe. Check the cursor plane's 9580 * blending properties match the underlying planes'. 9581 */ 9582 9583 new_cursor_state = drm_atomic_get_new_plane_state(state, cursor); 9584 if (!new_cursor_state || !new_cursor_state->fb) 9585 return 0; 9586 9587 dm_get_oriented_plane_size(new_cursor_state, &cursor_src_w, &cursor_src_h); 9588 cursor_scale_w = new_cursor_state->crtc_w * 1000 / cursor_src_w; 9589 cursor_scale_h = new_cursor_state->crtc_h * 1000 / cursor_src_h; 9590 9591 for_each_new_plane_in_state_reverse(state, underlying, new_underlying_state, i) { 9592 /* Narrow down to non-cursor planes on the same CRTC as the cursor */ 9593 if (new_underlying_state->crtc != crtc || underlying == crtc->cursor) 9594 continue; 9595 9596 /* Ignore disabled planes */ 9597 if (!new_underlying_state->fb) 9598 continue; 9599 9600 dm_get_oriented_plane_size(new_underlying_state, 9601 &underlying_src_w, &underlying_src_h); 9602 underlying_scale_w = new_underlying_state->crtc_w * 1000 / underlying_src_w; 9603 underlying_scale_h = new_underlying_state->crtc_h * 1000 / underlying_src_h; 9604 9605 if (cursor_scale_w != underlying_scale_w || 9606 cursor_scale_h != underlying_scale_h) { 9607 drm_dbg_atomic(crtc->dev, 9608 "Cursor [PLANE:%d:%s] scaling doesn't match underlying [PLANE:%d:%s]\n", 9609 cursor->base.id, cursor->name, underlying->base.id, underlying->name); 9610 return -EINVAL; 9611 } 9612 9613 /* If this plane covers the whole CRTC, no need to check planes underneath */ 9614 if (new_underlying_state->crtc_x <= 0 && 9615 new_underlying_state->crtc_y <= 0 && 9616 new_underlying_state->crtc_x + new_underlying_state->crtc_w >= new_crtc_state->mode.hdisplay && 9617 new_underlying_state->crtc_y + new_underlying_state->crtc_h >= new_crtc_state->mode.vdisplay) 9618 break; 9619 } 9620 9621 return 0; 9622 } 9623 9624 #if defined(CONFIG_DRM_AMD_DC_DCN) 9625 static int add_affected_mst_dsc_crtcs(struct drm_atomic_state *state, struct drm_crtc *crtc) 9626 { 9627 struct drm_connector *connector; 9628 struct drm_connector_state *conn_state, *old_conn_state; 9629 struct amdgpu_dm_connector *aconnector = NULL; 9630 int i; 9631 9632 for_each_oldnew_connector_in_state(state, connector, old_conn_state, conn_state, i) { 9633 if (!conn_state->crtc) 9634 conn_state = old_conn_state; 9635 9636 if (conn_state->crtc != crtc) 9637 continue; 9638 9639 aconnector = to_amdgpu_dm_connector(connector); 9640 if (!aconnector->port || !aconnector->mst_port) 9641 aconnector = NULL; 9642 else 9643 break; 9644 } 9645 9646 if (!aconnector) 9647 return 0; 9648 9649 return drm_dp_mst_add_affected_dsc_crtcs(state, &aconnector->mst_port->mst_mgr); 9650 } 9651 #endif 9652 9653 /** 9654 * amdgpu_dm_atomic_check() - Atomic check implementation for AMDgpu DM. 9655 * 9656 * @dev: The DRM device 9657 * @state: The atomic state to commit 9658 * 9659 * Validate that the given atomic state is programmable by DC into hardware. 9660 * This involves constructing a &struct dc_state reflecting the new hardware 9661 * state we wish to commit, then querying DC to see if it is programmable. It's 9662 * important not to modify the existing DC state. Otherwise, atomic_check 9663 * may unexpectedly commit hardware changes. 9664 * 9665 * When validating the DC state, it's important that the right locks are 9666 * acquired. For full updates case which removes/adds/updates streams on one 9667 * CRTC while flipping on another CRTC, acquiring global lock will guarantee 9668 * that any such full update commit will wait for completion of any outstanding 9669 * flip using DRMs synchronization events. 9670 * 9671 * Note that DM adds the affected connectors for all CRTCs in state, when that 9672 * might not seem necessary. This is because DC stream creation requires the 9673 * DC sink, which is tied to the DRM connector state. Cleaning this up should 9674 * be possible but non-trivial - a possible TODO item. 9675 * 9676 * Return: -Error code if validation failed. 9677 */ 9678 static int amdgpu_dm_atomic_check(struct drm_device *dev, 9679 struct drm_atomic_state *state) 9680 { 9681 struct amdgpu_device *adev = drm_to_adev(dev); 9682 struct dm_atomic_state *dm_state = NULL; 9683 struct dc *dc = adev->dm.dc; 9684 struct drm_connector *connector; 9685 struct drm_connector_state *old_con_state, *new_con_state; 9686 struct drm_crtc *crtc; 9687 struct drm_crtc_state *old_crtc_state, *new_crtc_state; 9688 struct drm_plane *plane; 9689 struct drm_plane_state *old_plane_state, *new_plane_state; 9690 enum dc_status status; 9691 int ret, i; 9692 bool lock_and_validation_needed = false; 9693 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state; 9694 #if defined(CONFIG_DRM_AMD_DC_DCN) 9695 struct drm_dp_mst_topology_mgr *mgr; 9696 struct drm_dp_mst_topology_state *mst_state; 9697 struct dsc_mst_fairness_vars vars[MAX_PIPES]; 9698 #endif 9699 9700 trace_amdgpu_dm_atomic_check_begin(state); 9701 9702 ret = drm_atomic_helper_check_modeset(dev, state); 9703 if (ret) { 9704 DRM_DEBUG_DRIVER("drm_atomic_helper_check_modeset() failed\n"); 9705 goto fail; 9706 } 9707 9708 /* Check connector changes */ 9709 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) { 9710 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state); 9711 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state); 9712 9713 /* Skip connectors that are disabled or part of modeset already. */ 9714 if (!new_con_state->crtc) 9715 continue; 9716 9717 new_crtc_state = drm_atomic_get_crtc_state(state, new_con_state->crtc); 9718 if (IS_ERR(new_crtc_state)) { 9719 DRM_DEBUG_DRIVER("drm_atomic_get_crtc_state() failed\n"); 9720 ret = PTR_ERR(new_crtc_state); 9721 goto fail; 9722 } 9723 9724 if (dm_old_con_state->abm_level != dm_new_con_state->abm_level || 9725 dm_old_con_state->scaling != dm_new_con_state->scaling) 9726 new_crtc_state->connectors_changed = true; 9727 } 9728 9729 #if defined(CONFIG_DRM_AMD_DC_DCN) 9730 if (dc_resource_is_dsc_encoding_supported(dc)) { 9731 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 9732 if (drm_atomic_crtc_needs_modeset(new_crtc_state)) { 9733 ret = add_affected_mst_dsc_crtcs(state, crtc); 9734 if (ret) { 9735 DRM_DEBUG_DRIVER("add_affected_mst_dsc_crtcs() failed\n"); 9736 goto fail; 9737 } 9738 } 9739 } 9740 } 9741 #endif 9742 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 9743 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 9744 9745 if (!drm_atomic_crtc_needs_modeset(new_crtc_state) && 9746 !new_crtc_state->color_mgmt_changed && 9747 old_crtc_state->vrr_enabled == new_crtc_state->vrr_enabled && 9748 dm_old_crtc_state->dsc_force_changed == false) 9749 continue; 9750 9751 ret = amdgpu_dm_verify_lut_sizes(new_crtc_state); 9752 if (ret) { 9753 DRM_DEBUG_DRIVER("amdgpu_dm_verify_lut_sizes() failed\n"); 9754 goto fail; 9755 } 9756 9757 if (!new_crtc_state->enable) 9758 continue; 9759 9760 ret = drm_atomic_add_affected_connectors(state, crtc); 9761 if (ret) { 9762 DRM_DEBUG_DRIVER("drm_atomic_add_affected_connectors() failed\n"); 9763 goto fail; 9764 } 9765 9766 ret = drm_atomic_add_affected_planes(state, crtc); 9767 if (ret) { 9768 DRM_DEBUG_DRIVER("drm_atomic_add_affected_planes() failed\n"); 9769 goto fail; 9770 } 9771 9772 if (dm_old_crtc_state->dsc_force_changed) 9773 new_crtc_state->mode_changed = true; 9774 } 9775 9776 /* 9777 * Add all primary and overlay planes on the CRTC to the state 9778 * whenever a plane is enabled to maintain correct z-ordering 9779 * and to enable fast surface updates. 9780 */ 9781 drm_for_each_crtc(crtc, dev) { 9782 bool modified = false; 9783 9784 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) { 9785 if (plane->type == DRM_PLANE_TYPE_CURSOR) 9786 continue; 9787 9788 if (new_plane_state->crtc == crtc || 9789 old_plane_state->crtc == crtc) { 9790 modified = true; 9791 break; 9792 } 9793 } 9794 9795 if (!modified) 9796 continue; 9797 9798 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) { 9799 if (plane->type == DRM_PLANE_TYPE_CURSOR) 9800 continue; 9801 9802 new_plane_state = 9803 drm_atomic_get_plane_state(state, plane); 9804 9805 if (IS_ERR(new_plane_state)) { 9806 ret = PTR_ERR(new_plane_state); 9807 DRM_DEBUG_DRIVER("new_plane_state is BAD\n"); 9808 goto fail; 9809 } 9810 } 9811 } 9812 9813 /* 9814 * DC consults the zpos (layer_index in DC terminology) to determine the 9815 * hw plane on which to enable the hw cursor (see 9816 * `dcn10_can_pipe_disable_cursor`). By now, all modified planes are in 9817 * atomic state, so call drm helper to normalize zpos. 9818 */ 9819 ret = drm_atomic_normalize_zpos(dev, state); 9820 if (ret) { 9821 drm_dbg(dev, "drm_atomic_normalize_zpos() failed\n"); 9822 goto fail; 9823 } 9824 9825 /* Remove exiting planes if they are modified */ 9826 for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) { 9827 if (old_plane_state->fb && new_plane_state->fb && 9828 get_mem_type(old_plane_state->fb) != 9829 get_mem_type(new_plane_state->fb)) 9830 lock_and_validation_needed = true; 9831 9832 ret = dm_update_plane_state(dc, state, plane, 9833 old_plane_state, 9834 new_plane_state, 9835 false, 9836 &lock_and_validation_needed); 9837 if (ret) { 9838 DRM_DEBUG_DRIVER("dm_update_plane_state() failed\n"); 9839 goto fail; 9840 } 9841 } 9842 9843 /* Disable all crtcs which require disable */ 9844 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 9845 ret = dm_update_crtc_state(&adev->dm, state, crtc, 9846 old_crtc_state, 9847 new_crtc_state, 9848 false, 9849 &lock_and_validation_needed); 9850 if (ret) { 9851 DRM_DEBUG_DRIVER("DISABLE: dm_update_crtc_state() failed\n"); 9852 goto fail; 9853 } 9854 } 9855 9856 /* Enable all crtcs which require enable */ 9857 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 9858 ret = dm_update_crtc_state(&adev->dm, state, crtc, 9859 old_crtc_state, 9860 new_crtc_state, 9861 true, 9862 &lock_and_validation_needed); 9863 if (ret) { 9864 DRM_DEBUG_DRIVER("ENABLE: dm_update_crtc_state() failed\n"); 9865 goto fail; 9866 } 9867 } 9868 9869 /* Add new/modified planes */ 9870 for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) { 9871 ret = dm_update_plane_state(dc, state, plane, 9872 old_plane_state, 9873 new_plane_state, 9874 true, 9875 &lock_and_validation_needed); 9876 if (ret) { 9877 DRM_DEBUG_DRIVER("dm_update_plane_state() failed\n"); 9878 goto fail; 9879 } 9880 } 9881 9882 #if defined(CONFIG_DRM_AMD_DC_DCN) 9883 if (dc_resource_is_dsc_encoding_supported(dc)) { 9884 ret = pre_validate_dsc(state, &dm_state, vars); 9885 if (ret != 0) 9886 goto fail; 9887 } 9888 #endif 9889 9890 /* Run this here since we want to validate the streams we created */ 9891 ret = drm_atomic_helper_check_planes(dev, state); 9892 if (ret) { 9893 DRM_DEBUG_DRIVER("drm_atomic_helper_check_planes() failed\n"); 9894 goto fail; 9895 } 9896 9897 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { 9898 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 9899 if (dm_new_crtc_state->mpo_requested) 9900 DRM_DEBUG_DRIVER("MPO enablement requested on crtc:[%p]\n", crtc); 9901 } 9902 9903 /* Check cursor planes scaling */ 9904 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { 9905 ret = dm_check_crtc_cursor(state, crtc, new_crtc_state); 9906 if (ret) { 9907 DRM_DEBUG_DRIVER("dm_check_crtc_cursor() failed\n"); 9908 goto fail; 9909 } 9910 } 9911 9912 if (state->legacy_cursor_update) { 9913 /* 9914 * This is a fast cursor update coming from the plane update 9915 * helper, check if it can be done asynchronously for better 9916 * performance. 9917 */ 9918 state->async_update = 9919 !drm_atomic_helper_async_check(dev, state); 9920 9921 /* 9922 * Skip the remaining global validation if this is an async 9923 * update. Cursor updates can be done without affecting 9924 * state or bandwidth calcs and this avoids the performance 9925 * penalty of locking the private state object and 9926 * allocating a new dc_state. 9927 */ 9928 if (state->async_update) 9929 return 0; 9930 } 9931 9932 /* Check scaling and underscan changes*/ 9933 /* TODO Removed scaling changes validation due to inability to commit 9934 * new stream into context w\o causing full reset. Need to 9935 * decide how to handle. 9936 */ 9937 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) { 9938 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state); 9939 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state); 9940 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc); 9941 9942 /* Skip any modesets/resets */ 9943 if (!acrtc || drm_atomic_crtc_needs_modeset( 9944 drm_atomic_get_new_crtc_state(state, &acrtc->base))) 9945 continue; 9946 9947 /* Skip any thing not scale or underscan changes */ 9948 if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state)) 9949 continue; 9950 9951 lock_and_validation_needed = true; 9952 } 9953 9954 #if defined(CONFIG_DRM_AMD_DC_DCN) 9955 /* set the slot info for each mst_state based on the link encoding format */ 9956 for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) { 9957 struct amdgpu_dm_connector *aconnector; 9958 struct drm_connector *connector; 9959 struct drm_connector_list_iter iter; 9960 u8 link_coding_cap; 9961 9962 drm_connector_list_iter_begin(dev, &iter); 9963 drm_for_each_connector_iter(connector, &iter) { 9964 if (connector->index == mst_state->mgr->conn_base_id) { 9965 aconnector = to_amdgpu_dm_connector(connector); 9966 link_coding_cap = dc_link_dp_mst_decide_link_encoding_format(aconnector->dc_link); 9967 drm_dp_mst_update_slots(mst_state, link_coding_cap); 9968 9969 break; 9970 } 9971 } 9972 drm_connector_list_iter_end(&iter); 9973 } 9974 #endif 9975 9976 /** 9977 * Streams and planes are reset when there are changes that affect 9978 * bandwidth. Anything that affects bandwidth needs to go through 9979 * DC global validation to ensure that the configuration can be applied 9980 * to hardware. 9981 * 9982 * We have to currently stall out here in atomic_check for outstanding 9983 * commits to finish in this case because our IRQ handlers reference 9984 * DRM state directly - we can end up disabling interrupts too early 9985 * if we don't. 9986 * 9987 * TODO: Remove this stall and drop DM state private objects. 9988 */ 9989 if (lock_and_validation_needed) { 9990 ret = dm_atomic_get_state(state, &dm_state); 9991 if (ret) { 9992 DRM_DEBUG_DRIVER("dm_atomic_get_state() failed\n"); 9993 goto fail; 9994 } 9995 9996 ret = do_aquire_global_lock(dev, state); 9997 if (ret) { 9998 DRM_DEBUG_DRIVER("do_aquire_global_lock() failed\n"); 9999 goto fail; 10000 } 10001 10002 #if defined(CONFIG_DRM_AMD_DC_DCN) 10003 ret = compute_mst_dsc_configs_for_state(state, dm_state->context, vars); 10004 if (ret) { 10005 DRM_DEBUG_DRIVER("compute_mst_dsc_configs_for_state() failed\n"); 10006 ret = -EINVAL; 10007 goto fail; 10008 } 10009 10010 ret = dm_update_mst_vcpi_slots_for_dsc(state, dm_state->context, vars); 10011 if (ret) { 10012 DRM_DEBUG_DRIVER("dm_update_mst_vcpi_slots_for_dsc() failed\n"); 10013 goto fail; 10014 } 10015 #endif 10016 10017 /* 10018 * Perform validation of MST topology in the state: 10019 * We need to perform MST atomic check before calling 10020 * dc_validate_global_state(), or there is a chance 10021 * to get stuck in an infinite loop and hang eventually. 10022 */ 10023 ret = drm_dp_mst_atomic_check(state); 10024 if (ret) { 10025 DRM_DEBUG_DRIVER("drm_dp_mst_atomic_check() failed\n"); 10026 goto fail; 10027 } 10028 status = dc_validate_global_state(dc, dm_state->context, true); 10029 if (status != DC_OK) { 10030 DRM_DEBUG_DRIVER("DC global validation failure: %s (%d)", 10031 dc_status_to_str(status), status); 10032 ret = -EINVAL; 10033 goto fail; 10034 } 10035 } else { 10036 /* 10037 * The commit is a fast update. Fast updates shouldn't change 10038 * the DC context, affect global validation, and can have their 10039 * commit work done in parallel with other commits not touching 10040 * the same resource. If we have a new DC context as part of 10041 * the DM atomic state from validation we need to free it and 10042 * retain the existing one instead. 10043 * 10044 * Furthermore, since the DM atomic state only contains the DC 10045 * context and can safely be annulled, we can free the state 10046 * and clear the associated private object now to free 10047 * some memory and avoid a possible use-after-free later. 10048 */ 10049 10050 for (i = 0; i < state->num_private_objs; i++) { 10051 struct drm_private_obj *obj = state->private_objs[i].ptr; 10052 10053 if (obj->funcs == adev->dm.atomic_obj.funcs) { 10054 int j = state->num_private_objs-1; 10055 10056 dm_atomic_destroy_state(obj, 10057 state->private_objs[i].state); 10058 10059 /* If i is not at the end of the array then the 10060 * last element needs to be moved to where i was 10061 * before the array can safely be truncated. 10062 */ 10063 if (i != j) 10064 state->private_objs[i] = 10065 state->private_objs[j]; 10066 10067 state->private_objs[j].ptr = NULL; 10068 state->private_objs[j].state = NULL; 10069 state->private_objs[j].old_state = NULL; 10070 state->private_objs[j].new_state = NULL; 10071 10072 state->num_private_objs = j; 10073 break; 10074 } 10075 } 10076 } 10077 10078 /* Store the overall update type for use later in atomic check. */ 10079 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { 10080 struct dm_crtc_state *dm_new_crtc_state = 10081 to_dm_crtc_state(new_crtc_state); 10082 10083 /* 10084 * Only allow async flips for fast updates that don't change 10085 * the FB pitch, the DCC state, rotation, etc. 10086 */ 10087 if (new_crtc_state->async_flip && lock_and_validation_needed) { 10088 drm_dbg_atomic(crtc->dev, 10089 "[CRTC:%d:%s] async flips are only supported for fast updates\n", 10090 crtc->base.id, crtc->name); 10091 ret = -EINVAL; 10092 goto fail; 10093 } 10094 10095 dm_new_crtc_state->update_type = lock_and_validation_needed ? 10096 UPDATE_TYPE_FULL : UPDATE_TYPE_FAST; 10097 } 10098 10099 /* Must be success */ 10100 WARN_ON(ret); 10101 10102 trace_amdgpu_dm_atomic_check_finish(state, ret); 10103 10104 return ret; 10105 10106 fail: 10107 if (ret == -EDEADLK) 10108 DRM_DEBUG_DRIVER("Atomic check stopped to avoid deadlock.\n"); 10109 else if (ret == -EINTR || ret == -EAGAIN || ret == -ERESTARTSYS) 10110 DRM_DEBUG_DRIVER("Atomic check stopped due to signal.\n"); 10111 else 10112 DRM_DEBUG_DRIVER("Atomic check failed with err: %d\n", ret); 10113 10114 trace_amdgpu_dm_atomic_check_finish(state, ret); 10115 10116 return ret; 10117 } 10118 10119 static bool is_dp_capable_without_timing_msa(struct dc *dc, 10120 struct amdgpu_dm_connector *amdgpu_dm_connector) 10121 { 10122 u8 dpcd_data; 10123 bool capable = false; 10124 10125 if (amdgpu_dm_connector->dc_link && 10126 dm_helpers_dp_read_dpcd( 10127 NULL, 10128 amdgpu_dm_connector->dc_link, 10129 DP_DOWN_STREAM_PORT_COUNT, 10130 &dpcd_data, 10131 sizeof(dpcd_data))) { 10132 capable = (dpcd_data & DP_MSA_TIMING_PAR_IGNORED) ? true:false; 10133 } 10134 10135 return capable; 10136 } 10137 10138 static bool dm_edid_parser_send_cea(struct amdgpu_display_manager *dm, 10139 unsigned int offset, 10140 unsigned int total_length, 10141 u8 *data, 10142 unsigned int length, 10143 struct amdgpu_hdmi_vsdb_info *vsdb) 10144 { 10145 bool res; 10146 union dmub_rb_cmd cmd; 10147 struct dmub_cmd_send_edid_cea *input; 10148 struct dmub_cmd_edid_cea_output *output; 10149 10150 if (length > DMUB_EDID_CEA_DATA_CHUNK_BYTES) 10151 return false; 10152 10153 memset(&cmd, 0, sizeof(cmd)); 10154 10155 input = &cmd.edid_cea.data.input; 10156 10157 cmd.edid_cea.header.type = DMUB_CMD__EDID_CEA; 10158 cmd.edid_cea.header.sub_type = 0; 10159 cmd.edid_cea.header.payload_bytes = 10160 sizeof(cmd.edid_cea) - sizeof(cmd.edid_cea.header); 10161 input->offset = offset; 10162 input->length = length; 10163 input->cea_total_length = total_length; 10164 memcpy(input->payload, data, length); 10165 10166 res = dc_dmub_srv_cmd_with_reply_data(dm->dc->ctx->dmub_srv, &cmd); 10167 if (!res) { 10168 DRM_ERROR("EDID CEA parser failed\n"); 10169 return false; 10170 } 10171 10172 output = &cmd.edid_cea.data.output; 10173 10174 if (output->type == DMUB_CMD__EDID_CEA_ACK) { 10175 if (!output->ack.success) { 10176 DRM_ERROR("EDID CEA ack failed at offset %d\n", 10177 output->ack.offset); 10178 } 10179 } else if (output->type == DMUB_CMD__EDID_CEA_AMD_VSDB) { 10180 if (!output->amd_vsdb.vsdb_found) 10181 return false; 10182 10183 vsdb->freesync_supported = output->amd_vsdb.freesync_supported; 10184 vsdb->amd_vsdb_version = output->amd_vsdb.amd_vsdb_version; 10185 vsdb->min_refresh_rate_hz = output->amd_vsdb.min_frame_rate; 10186 vsdb->max_refresh_rate_hz = output->amd_vsdb.max_frame_rate; 10187 } else { 10188 if (output->type != 0) 10189 DRM_WARN("Unknown EDID CEA parser results\n"); 10190 return false; 10191 } 10192 10193 return true; 10194 } 10195 10196 static bool parse_edid_cea_dmcu(struct amdgpu_display_manager *dm, 10197 u8 *edid_ext, int len, 10198 struct amdgpu_hdmi_vsdb_info *vsdb_info) 10199 { 10200 int i; 10201 10202 /* send extension block to DMCU for parsing */ 10203 for (i = 0; i < len; i += 8) { 10204 bool res; 10205 int offset; 10206 10207 /* send 8 bytes a time */ 10208 if (!dc_edid_parser_send_cea(dm->dc, i, len, &edid_ext[i], 8)) 10209 return false; 10210 10211 if (i+8 == len) { 10212 /* EDID block sent completed, expect result */ 10213 int version, min_rate, max_rate; 10214 10215 res = dc_edid_parser_recv_amd_vsdb(dm->dc, &version, &min_rate, &max_rate); 10216 if (res) { 10217 /* amd vsdb found */ 10218 vsdb_info->freesync_supported = 1; 10219 vsdb_info->amd_vsdb_version = version; 10220 vsdb_info->min_refresh_rate_hz = min_rate; 10221 vsdb_info->max_refresh_rate_hz = max_rate; 10222 return true; 10223 } 10224 /* not amd vsdb */ 10225 return false; 10226 } 10227 10228 /* check for ack*/ 10229 res = dc_edid_parser_recv_cea_ack(dm->dc, &offset); 10230 if (!res) 10231 return false; 10232 } 10233 10234 return false; 10235 } 10236 10237 static bool parse_edid_cea_dmub(struct amdgpu_display_manager *dm, 10238 u8 *edid_ext, int len, 10239 struct amdgpu_hdmi_vsdb_info *vsdb_info) 10240 { 10241 int i; 10242 10243 /* send extension block to DMCU for parsing */ 10244 for (i = 0; i < len; i += 8) { 10245 /* send 8 bytes a time */ 10246 if (!dm_edid_parser_send_cea(dm, i, len, &edid_ext[i], 8, vsdb_info)) 10247 return false; 10248 } 10249 10250 return vsdb_info->freesync_supported; 10251 } 10252 10253 static bool parse_edid_cea(struct amdgpu_dm_connector *aconnector, 10254 u8 *edid_ext, int len, 10255 struct amdgpu_hdmi_vsdb_info *vsdb_info) 10256 { 10257 struct amdgpu_device *adev = drm_to_adev(aconnector->base.dev); 10258 10259 if (adev->dm.dmub_srv) 10260 return parse_edid_cea_dmub(&adev->dm, edid_ext, len, vsdb_info); 10261 else 10262 return parse_edid_cea_dmcu(&adev->dm, edid_ext, len, vsdb_info); 10263 } 10264 10265 static int parse_hdmi_amd_vsdb(struct amdgpu_dm_connector *aconnector, 10266 struct edid *edid, struct amdgpu_hdmi_vsdb_info *vsdb_info) 10267 { 10268 u8 *edid_ext = NULL; 10269 int i; 10270 bool valid_vsdb_found = false; 10271 10272 /*----- drm_find_cea_extension() -----*/ 10273 /* No EDID or EDID extensions */ 10274 if (edid == NULL || edid->extensions == 0) 10275 return -ENODEV; 10276 10277 /* Find CEA extension */ 10278 for (i = 0; i < edid->extensions; i++) { 10279 edid_ext = (uint8_t *)edid + EDID_LENGTH * (i + 1); 10280 if (edid_ext[0] == CEA_EXT) 10281 break; 10282 } 10283 10284 if (i == edid->extensions) 10285 return -ENODEV; 10286 10287 /*----- cea_db_offsets() -----*/ 10288 if (edid_ext[0] != CEA_EXT) 10289 return -ENODEV; 10290 10291 valid_vsdb_found = parse_edid_cea(aconnector, edid_ext, EDID_LENGTH, vsdb_info); 10292 10293 return valid_vsdb_found ? i : -ENODEV; 10294 } 10295 10296 /** 10297 * amdgpu_dm_update_freesync_caps - Update Freesync capabilities 10298 * 10299 * @connector: Connector to query. 10300 * @edid: EDID from monitor 10301 * 10302 * Amdgpu supports Freesync in DP and HDMI displays, and it is required to keep 10303 * track of some of the display information in the internal data struct used by 10304 * amdgpu_dm. This function checks which type of connector we need to set the 10305 * FreeSync parameters. 10306 */ 10307 void amdgpu_dm_update_freesync_caps(struct drm_connector *connector, 10308 struct edid *edid) 10309 { 10310 int i = 0; 10311 struct detailed_timing *timing; 10312 struct detailed_non_pixel *data; 10313 struct detailed_data_monitor_range *range; 10314 struct amdgpu_dm_connector *amdgpu_dm_connector = 10315 to_amdgpu_dm_connector(connector); 10316 struct dm_connector_state *dm_con_state = NULL; 10317 struct dc_sink *sink; 10318 10319 struct drm_device *dev = connector->dev; 10320 struct amdgpu_device *adev = drm_to_adev(dev); 10321 struct amdgpu_hdmi_vsdb_info vsdb_info = {0}; 10322 bool freesync_capable = false; 10323 10324 if (!connector->state) { 10325 DRM_ERROR("%s - Connector has no state", __func__); 10326 goto update; 10327 } 10328 10329 sink = amdgpu_dm_connector->dc_sink ? 10330 amdgpu_dm_connector->dc_sink : 10331 amdgpu_dm_connector->dc_em_sink; 10332 10333 if (!edid || !sink) { 10334 dm_con_state = to_dm_connector_state(connector->state); 10335 10336 amdgpu_dm_connector->min_vfreq = 0; 10337 amdgpu_dm_connector->max_vfreq = 0; 10338 amdgpu_dm_connector->pixel_clock_mhz = 0; 10339 connector->display_info.monitor_range.min_vfreq = 0; 10340 connector->display_info.monitor_range.max_vfreq = 0; 10341 freesync_capable = false; 10342 10343 goto update; 10344 } 10345 10346 dm_con_state = to_dm_connector_state(connector->state); 10347 10348 if (!adev->dm.freesync_module) 10349 goto update; 10350 10351 if (sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT 10352 || sink->sink_signal == SIGNAL_TYPE_EDP) { 10353 bool edid_check_required = false; 10354 10355 if (edid) { 10356 edid_check_required = is_dp_capable_without_timing_msa( 10357 adev->dm.dc, 10358 amdgpu_dm_connector); 10359 } 10360 10361 if (edid_check_required == true && (edid->version > 1 || 10362 (edid->version == 1 && edid->revision > 1))) { 10363 for (i = 0; i < 4; i++) { 10364 10365 timing = &edid->detailed_timings[i]; 10366 data = &timing->data.other_data; 10367 range = &data->data.range; 10368 /* 10369 * Check if monitor has continuous frequency mode 10370 */ 10371 if (data->type != EDID_DETAIL_MONITOR_RANGE) 10372 continue; 10373 /* 10374 * Check for flag range limits only. If flag == 1 then 10375 * no additional timing information provided. 10376 * Default GTF, GTF Secondary curve and CVT are not 10377 * supported 10378 */ 10379 if (range->flags != 1) 10380 continue; 10381 10382 amdgpu_dm_connector->min_vfreq = range->min_vfreq; 10383 amdgpu_dm_connector->max_vfreq = range->max_vfreq; 10384 amdgpu_dm_connector->pixel_clock_mhz = 10385 range->pixel_clock_mhz * 10; 10386 10387 connector->display_info.monitor_range.min_vfreq = range->min_vfreq; 10388 connector->display_info.monitor_range.max_vfreq = range->max_vfreq; 10389 10390 break; 10391 } 10392 10393 if (amdgpu_dm_connector->max_vfreq - 10394 amdgpu_dm_connector->min_vfreq > 10) { 10395 10396 freesync_capable = true; 10397 } 10398 } 10399 } else if (edid && sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A) { 10400 i = parse_hdmi_amd_vsdb(amdgpu_dm_connector, edid, &vsdb_info); 10401 if (i >= 0 && vsdb_info.freesync_supported) { 10402 timing = &edid->detailed_timings[i]; 10403 data = &timing->data.other_data; 10404 10405 amdgpu_dm_connector->min_vfreq = vsdb_info.min_refresh_rate_hz; 10406 amdgpu_dm_connector->max_vfreq = vsdb_info.max_refresh_rate_hz; 10407 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10) 10408 freesync_capable = true; 10409 10410 connector->display_info.monitor_range.min_vfreq = vsdb_info.min_refresh_rate_hz; 10411 connector->display_info.monitor_range.max_vfreq = vsdb_info.max_refresh_rate_hz; 10412 } 10413 } 10414 10415 update: 10416 if (dm_con_state) 10417 dm_con_state->freesync_capable = freesync_capable; 10418 10419 if (connector->vrr_capable_property) 10420 drm_connector_set_vrr_capable_property(connector, 10421 freesync_capable); 10422 } 10423 10424 void amdgpu_dm_trigger_timing_sync(struct drm_device *dev) 10425 { 10426 struct amdgpu_device *adev = drm_to_adev(dev); 10427 struct dc *dc = adev->dm.dc; 10428 int i; 10429 10430 mutex_lock(&adev->dm.dc_lock); 10431 if (dc->current_state) { 10432 for (i = 0; i < dc->current_state->stream_count; ++i) 10433 dc->current_state->streams[i] 10434 ->triggered_crtc_reset.enabled = 10435 adev->dm.force_timing_sync; 10436 10437 dm_enable_per_frame_crtc_master_sync(dc->current_state); 10438 dc_trigger_sync(dc, dc->current_state); 10439 } 10440 mutex_unlock(&adev->dm.dc_lock); 10441 } 10442 10443 void dm_write_reg_func(const struct dc_context *ctx, uint32_t address, 10444 u32 value, const char *func_name) 10445 { 10446 #ifdef DM_CHECK_ADDR_0 10447 if (address == 0) { 10448 DC_ERR("invalid register write. address = 0"); 10449 return; 10450 } 10451 #endif 10452 cgs_write_register(ctx->cgs_device, address, value); 10453 trace_amdgpu_dc_wreg(&ctx->perf_trace->write_count, address, value); 10454 } 10455 10456 uint32_t dm_read_reg_func(const struct dc_context *ctx, uint32_t address, 10457 const char *func_name) 10458 { 10459 u32 value; 10460 #ifdef DM_CHECK_ADDR_0 10461 if (address == 0) { 10462 DC_ERR("invalid register read; address = 0\n"); 10463 return 0; 10464 } 10465 #endif 10466 10467 if (ctx->dmub_srv && 10468 ctx->dmub_srv->reg_helper_offload.gather_in_progress && 10469 !ctx->dmub_srv->reg_helper_offload.should_burst_write) { 10470 ASSERT(false); 10471 return 0; 10472 } 10473 10474 value = cgs_read_register(ctx->cgs_device, address); 10475 10476 trace_amdgpu_dc_rreg(&ctx->perf_trace->read_count, address, value); 10477 10478 return value; 10479 } 10480 10481 int amdgpu_dm_process_dmub_aux_transfer_sync( 10482 struct dc_context *ctx, 10483 unsigned int link_index, 10484 struct aux_payload *payload, 10485 enum aux_return_code_type *operation_result) 10486 { 10487 struct amdgpu_device *adev = ctx->driver_context; 10488 struct dmub_notification *p_notify = adev->dm.dmub_notify; 10489 int ret = -1; 10490 10491 mutex_lock(&adev->dm.dpia_aux_lock); 10492 if (!dc_process_dmub_aux_transfer_async(ctx->dc, link_index, payload)) { 10493 *operation_result = AUX_RET_ERROR_ENGINE_ACQUIRE; 10494 goto out; 10495 } 10496 10497 if (!wait_for_completion_timeout(&adev->dm.dmub_aux_transfer_done, 10 * HZ)) { 10498 DRM_ERROR("wait_for_completion_timeout timeout!"); 10499 *operation_result = AUX_RET_ERROR_TIMEOUT; 10500 goto out; 10501 } 10502 10503 if (p_notify->result != AUX_RET_SUCCESS) { 10504 /* 10505 * Transient states before tunneling is enabled could 10506 * lead to this error. We can ignore this for now. 10507 */ 10508 if (p_notify->result != AUX_RET_ERROR_PROTOCOL_ERROR) { 10509 DRM_WARN("DPIA AUX failed on 0x%x(%d), error %d\n", 10510 payload->address, payload->length, 10511 p_notify->result); 10512 } 10513 *operation_result = AUX_RET_ERROR_INVALID_REPLY; 10514 goto out; 10515 } 10516 10517 10518 payload->reply[0] = adev->dm.dmub_notify->aux_reply.command; 10519 if (!payload->write && p_notify->aux_reply.length && 10520 (payload->reply[0] == AUX_TRANSACTION_REPLY_AUX_ACK)) { 10521 10522 if (payload->length != p_notify->aux_reply.length) { 10523 DRM_WARN("invalid read length %d from DPIA AUX 0x%x(%d)!\n", 10524 p_notify->aux_reply.length, 10525 payload->address, payload->length); 10526 *operation_result = AUX_RET_ERROR_INVALID_REPLY; 10527 goto out; 10528 } 10529 10530 memcpy(payload->data, p_notify->aux_reply.data, 10531 p_notify->aux_reply.length); 10532 } 10533 10534 /* success */ 10535 ret = p_notify->aux_reply.length; 10536 *operation_result = p_notify->result; 10537 out: 10538 reinit_completion(&adev->dm.dmub_aux_transfer_done); 10539 mutex_unlock(&adev->dm.dpia_aux_lock); 10540 return ret; 10541 } 10542 10543 int amdgpu_dm_process_dmub_set_config_sync( 10544 struct dc_context *ctx, 10545 unsigned int link_index, 10546 struct set_config_cmd_payload *payload, 10547 enum set_config_status *operation_result) 10548 { 10549 struct amdgpu_device *adev = ctx->driver_context; 10550 bool is_cmd_complete; 10551 int ret; 10552 10553 mutex_lock(&adev->dm.dpia_aux_lock); 10554 is_cmd_complete = dc_process_dmub_set_config_async(ctx->dc, 10555 link_index, payload, adev->dm.dmub_notify); 10556 10557 if (is_cmd_complete || wait_for_completion_timeout(&adev->dm.dmub_aux_transfer_done, 10 * HZ)) { 10558 ret = 0; 10559 *operation_result = adev->dm.dmub_notify->sc_status; 10560 } else { 10561 DRM_ERROR("wait_for_completion_timeout timeout!"); 10562 ret = -1; 10563 *operation_result = SET_CONFIG_UNKNOWN_ERROR; 10564 } 10565 10566 if (!is_cmd_complete) 10567 reinit_completion(&adev->dm.dmub_aux_transfer_done); 10568 mutex_unlock(&adev->dm.dpia_aux_lock); 10569 return ret; 10570 } 10571 10572 /* 10573 * Check whether seamless boot is supported. 10574 * 10575 * So far we only support seamless boot on CHIP_VANGOGH. 10576 * If everything goes well, we may consider expanding 10577 * seamless boot to other ASICs. 10578 */ 10579 bool check_seamless_boot_capability(struct amdgpu_device *adev) 10580 { 10581 switch (adev->asic_type) { 10582 case CHIP_VANGOGH: 10583 if (!adev->mman.keep_stolen_vga_memory) 10584 return true; 10585 break; 10586 default: 10587 break; 10588 } 10589 10590 return false; 10591 } 10592