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