1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright 2022 Advanced Micro Devices, Inc. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be included in 13 * all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 21 * OTHER DEALINGS IN THE SOFTWARE. 22 * 23 * Authors: AMD 24 * 25 */ 26 #include <drm/drm_vblank.h> 27 #include <drm/drm_atomic_helper.h> 28 29 #include "dc.h" 30 #include "amdgpu.h" 31 #include "amdgpu_dm_psr.h" 32 #include "amdgpu_dm_replay.h" 33 #include "amdgpu_dm_crtc.h" 34 #include "amdgpu_dm_plane.h" 35 #include "amdgpu_dm_trace.h" 36 #include "amdgpu_dm_debugfs.h" 37 38 #define HPD_DETECTION_PERIOD_uS 5000000 39 #define HPD_DETECTION_TIME_uS 100000 40 41 void amdgpu_dm_crtc_handle_vblank(struct amdgpu_crtc *acrtc) 42 { 43 struct drm_crtc *crtc = &acrtc->base; 44 struct drm_device *dev = crtc->dev; 45 unsigned long flags; 46 47 drm_crtc_handle_vblank(crtc); 48 49 spin_lock_irqsave(&dev->event_lock, flags); 50 51 /* Send completion event for cursor-only commits */ 52 if (acrtc->event && acrtc->pflip_status != AMDGPU_FLIP_SUBMITTED) { 53 drm_crtc_send_vblank_event(crtc, acrtc->event); 54 drm_crtc_vblank_put(crtc); 55 acrtc->event = NULL; 56 } 57 58 spin_unlock_irqrestore(&dev->event_lock, flags); 59 } 60 61 bool amdgpu_dm_crtc_modeset_required(struct drm_crtc_state *crtc_state, 62 struct dc_stream_state *new_stream, 63 struct dc_stream_state *old_stream) 64 { 65 return crtc_state->active && drm_atomic_crtc_needs_modeset(crtc_state); 66 } 67 68 bool amdgpu_dm_crtc_vrr_active_irq(struct amdgpu_crtc *acrtc) 69 70 { 71 return acrtc->dm_irq_params.freesync_config.state == 72 VRR_STATE_ACTIVE_VARIABLE || 73 acrtc->dm_irq_params.freesync_config.state == 74 VRR_STATE_ACTIVE_FIXED; 75 } 76 77 int amdgpu_dm_crtc_set_vupdate_irq(struct drm_crtc *crtc, bool enable) 78 { 79 enum dc_irq_source irq_source; 80 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc); 81 struct amdgpu_device *adev = drm_to_adev(crtc->dev); 82 int rc; 83 84 if (acrtc->otg_inst == -1) 85 return 0; 86 87 irq_source = IRQ_TYPE_VUPDATE + acrtc->otg_inst; 88 89 rc = dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY; 90 91 DRM_DEBUG_VBL("crtc %d - vupdate irq %sabling: r=%d\n", 92 acrtc->crtc_id, enable ? "en" : "dis", rc); 93 return rc; 94 } 95 96 bool amdgpu_dm_crtc_vrr_active(struct dm_crtc_state *dm_state) 97 { 98 return dm_state->freesync_config.state == VRR_STATE_ACTIVE_VARIABLE || 99 dm_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED; 100 } 101 102 /** 103 * amdgpu_dm_crtc_set_panel_sr_feature() - Manage panel self-refresh features. 104 * 105 * @vblank_work: is a pointer to a struct vblank_control_work object. 106 * @vblank_enabled: indicates whether the DRM vblank counter is currently 107 * enabled (true) or disabled (false). 108 * @allow_sr_entry: represents whether entry into the self-refresh mode is 109 * allowed (true) or not allowed (false). 110 * 111 * The DRM vblank counter enable/disable action is used as the trigger to enable 112 * or disable various panel self-refresh features: 113 * 114 * Panel Replay and PSR SU 115 * - Enable when: 116 * - vblank counter is disabled 117 * - entry is allowed: usermode demonstrates an adequate number of fast 118 * commits) 119 * - CRC capture window isn't active 120 * - Keep enabled even when vblank counter gets enabled 121 * 122 * PSR1 123 * - Enable condition same as above 124 * - Disable when vblank counter is enabled 125 */ 126 static void amdgpu_dm_crtc_set_panel_sr_feature( 127 struct vblank_control_work *vblank_work, 128 bool vblank_enabled, bool allow_sr_entry) 129 { 130 struct dc_link *link = vblank_work->stream->link; 131 bool is_sr_active = (link->replay_settings.replay_allow_active || 132 link->psr_settings.psr_allow_active); 133 bool is_crc_window_active = false; 134 135 #ifdef CONFIG_DRM_AMD_SECURE_DISPLAY 136 is_crc_window_active = 137 amdgpu_dm_crc_window_is_activated(&vblank_work->acrtc->base); 138 #endif 139 140 if (link->replay_settings.replay_feature_enabled && 141 allow_sr_entry && !is_sr_active && !is_crc_window_active) { 142 amdgpu_dm_replay_enable(vblank_work->stream, true); 143 } else if (vblank_enabled) { 144 if (link->psr_settings.psr_version < DC_PSR_VERSION_SU_1 && is_sr_active) 145 amdgpu_dm_psr_disable(vblank_work->stream); 146 } else if (link->psr_settings.psr_feature_enabled && 147 allow_sr_entry && !is_sr_active && !is_crc_window_active) { 148 149 struct amdgpu_dm_connector *aconn = 150 (struct amdgpu_dm_connector *) vblank_work->stream->dm_stream_context; 151 152 if (!aconn->disallow_edp_enter_psr) { 153 struct amdgpu_display_manager *dm = vblank_work->dm; 154 155 amdgpu_dm_psr_enable(vblank_work->stream); 156 if (dm->idle_workqueue && 157 (dm->dc->config.disable_ips == DMUB_IPS_ENABLE) && 158 dm->dc->idle_optimizations_allowed && 159 dm->idle_workqueue->enable && 160 !dm->idle_workqueue->running) 161 schedule_work(&dm->idle_workqueue->work); 162 } 163 } 164 } 165 166 bool amdgpu_dm_is_headless(struct amdgpu_device *adev) 167 { 168 struct drm_connector *connector; 169 struct drm_connector_list_iter iter; 170 struct drm_device *dev; 171 bool is_headless = true; 172 173 if (adev == NULL) 174 return true; 175 176 dev = adev->dm.ddev; 177 178 drm_connector_list_iter_begin(dev, &iter); 179 drm_for_each_connector_iter(connector, &iter) { 180 181 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK) 182 continue; 183 184 if (connector->status == connector_status_connected) { 185 is_headless = false; 186 break; 187 } 188 } 189 drm_connector_list_iter_end(&iter); 190 return is_headless; 191 } 192 193 static void amdgpu_dm_idle_worker(struct work_struct *work) 194 { 195 struct idle_workqueue *idle_work; 196 197 idle_work = container_of(work, struct idle_workqueue, work); 198 idle_work->dm->idle_workqueue->running = true; 199 200 while (idle_work->enable) { 201 fsleep(HPD_DETECTION_PERIOD_uS); 202 mutex_lock(&idle_work->dm->dc_lock); 203 if (!idle_work->dm->dc->idle_optimizations_allowed) { 204 mutex_unlock(&idle_work->dm->dc_lock); 205 break; 206 } 207 dc_allow_idle_optimizations(idle_work->dm->dc, false); 208 209 mutex_unlock(&idle_work->dm->dc_lock); 210 fsleep(HPD_DETECTION_TIME_uS); 211 mutex_lock(&idle_work->dm->dc_lock); 212 213 if (!amdgpu_dm_is_headless(idle_work->dm->adev) && 214 !amdgpu_dm_psr_is_active_allowed(idle_work->dm)) { 215 mutex_unlock(&idle_work->dm->dc_lock); 216 break; 217 } 218 219 if (idle_work->enable) 220 dc_allow_idle_optimizations(idle_work->dm->dc, true); 221 mutex_unlock(&idle_work->dm->dc_lock); 222 } 223 idle_work->dm->idle_workqueue->running = false; 224 } 225 226 struct idle_workqueue *idle_create_workqueue(struct amdgpu_device *adev) 227 { 228 struct idle_workqueue *idle_work; 229 230 idle_work = kzalloc(sizeof(*idle_work), GFP_KERNEL); 231 if (ZERO_OR_NULL_PTR(idle_work)) 232 return NULL; 233 234 idle_work->dm = &adev->dm; 235 idle_work->enable = false; 236 idle_work->running = false; 237 INIT_WORK(&idle_work->work, amdgpu_dm_idle_worker); 238 239 return idle_work; 240 } 241 242 static void amdgpu_dm_crtc_vblank_control_worker(struct work_struct *work) 243 { 244 struct vblank_control_work *vblank_work = 245 container_of(work, struct vblank_control_work, work); 246 struct amdgpu_display_manager *dm = vblank_work->dm; 247 248 mutex_lock(&dm->dc_lock); 249 250 if (vblank_work->enable) 251 dm->active_vblank_irq_count++; 252 else if (dm->active_vblank_irq_count) 253 dm->active_vblank_irq_count--; 254 255 if (dm->active_vblank_irq_count > 0) { 256 DRM_DEBUG_KMS("Allow idle optimizations (MALL): false\n"); 257 dc_allow_idle_optimizations(dm->dc, false); 258 } 259 260 /* 261 * Control PSR based on vblank requirements from OS 262 * 263 * If panel supports PSR SU, there's no need to disable PSR when OS is 264 * submitting fast atomic commits (we infer this by whether the OS 265 * requests vblank events). Fast atomic commits will simply trigger a 266 * full-frame-update (FFU); a specific case of selective-update (SU) 267 * where the SU region is the full hactive*vactive region. See 268 * fill_dc_dirty_rects(). 269 */ 270 if (vblank_work->stream && vblank_work->stream->link) { 271 amdgpu_dm_crtc_set_panel_sr_feature( 272 vblank_work, vblank_work->enable, 273 vblank_work->acrtc->dm_irq_params.allow_psr_entry || 274 vblank_work->stream->link->replay_settings.replay_feature_enabled); 275 } 276 277 if (dm->active_vblank_irq_count == 0) { 278 DRM_DEBUG_KMS("Allow idle optimizations (MALL): true\n"); 279 dc_allow_idle_optimizations(dm->dc, true); 280 } 281 282 mutex_unlock(&dm->dc_lock); 283 284 dc_stream_release(vblank_work->stream); 285 286 kfree(vblank_work); 287 } 288 289 static inline int amdgpu_dm_crtc_set_vblank(struct drm_crtc *crtc, bool enable) 290 { 291 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc); 292 struct amdgpu_device *adev = drm_to_adev(crtc->dev); 293 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state); 294 struct amdgpu_display_manager *dm = &adev->dm; 295 struct vblank_control_work *work; 296 int irq_type; 297 int rc = 0; 298 299 if (acrtc->otg_inst == -1) 300 goto skip; 301 302 irq_type = amdgpu_display_crtc_idx_to_irq_type(adev, acrtc->crtc_id); 303 304 if (enable) { 305 /* vblank irq on -> Only need vupdate irq in vrr mode */ 306 if (amdgpu_dm_crtc_vrr_active(acrtc_state)) 307 rc = amdgpu_dm_crtc_set_vupdate_irq(crtc, true); 308 } else { 309 /* vblank irq off -> vupdate irq off */ 310 rc = amdgpu_dm_crtc_set_vupdate_irq(crtc, false); 311 } 312 313 if (rc) 314 return rc; 315 316 /* crtc vblank or vstartup interrupt */ 317 if (enable) { 318 rc = amdgpu_irq_get(adev, &adev->crtc_irq, irq_type); 319 drm_dbg_vbl(crtc->dev, "Get crtc_irq ret=%d\n", rc); 320 } else { 321 rc = amdgpu_irq_put(adev, &adev->crtc_irq, irq_type); 322 drm_dbg_vbl(crtc->dev, "Put crtc_irq ret=%d\n", rc); 323 } 324 325 if (rc) 326 return rc; 327 328 /* 329 * hubp surface flip interrupt 330 * 331 * We have no guarantee that the frontend index maps to the same 332 * backend index - some even map to more than one. 333 * 334 * TODO: Use a different interrupt or check DC itself for the mapping. 335 */ 336 if (enable) { 337 rc = amdgpu_irq_get(adev, &adev->pageflip_irq, irq_type); 338 drm_dbg_vbl(crtc->dev, "Get pageflip_irq ret=%d\n", rc); 339 } else { 340 rc = amdgpu_irq_put(adev, &adev->pageflip_irq, irq_type); 341 drm_dbg_vbl(crtc->dev, "Put pageflip_irq ret=%d\n", rc); 342 } 343 344 if (rc) 345 return rc; 346 347 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY) 348 /* crtc vline0 interrupt, only available on DCN+ */ 349 if (amdgpu_ip_version(adev, DCE_HWIP, 0) != 0) { 350 if (enable) { 351 rc = amdgpu_irq_get(adev, &adev->vline0_irq, irq_type); 352 drm_dbg_vbl(crtc->dev, "Get vline0_irq ret=%d\n", rc); 353 } else { 354 rc = amdgpu_irq_put(adev, &adev->vline0_irq, irq_type); 355 drm_dbg_vbl(crtc->dev, "Put vline0_irq ret=%d\n", rc); 356 } 357 358 if (rc) 359 return rc; 360 } 361 #endif 362 skip: 363 if (amdgpu_in_reset(adev)) 364 return 0; 365 366 if (dm->vblank_control_workqueue) { 367 work = kzalloc(sizeof(*work), GFP_ATOMIC); 368 if (!work) 369 return -ENOMEM; 370 371 INIT_WORK(&work->work, amdgpu_dm_crtc_vblank_control_worker); 372 work->dm = dm; 373 work->acrtc = acrtc; 374 work->enable = enable; 375 376 if (acrtc_state->stream) { 377 dc_stream_retain(acrtc_state->stream); 378 work->stream = acrtc_state->stream; 379 } 380 381 queue_work(dm->vblank_control_workqueue, &work->work); 382 } 383 384 return 0; 385 } 386 387 int amdgpu_dm_crtc_enable_vblank(struct drm_crtc *crtc) 388 { 389 return amdgpu_dm_crtc_set_vblank(crtc, true); 390 } 391 392 void amdgpu_dm_crtc_disable_vblank(struct drm_crtc *crtc) 393 { 394 amdgpu_dm_crtc_set_vblank(crtc, false); 395 } 396 397 static void amdgpu_dm_crtc_destroy_state(struct drm_crtc *crtc, 398 struct drm_crtc_state *state) 399 { 400 struct dm_crtc_state *cur = to_dm_crtc_state(state); 401 402 /* TODO Destroy dc_stream objects are stream object is flattened */ 403 if (cur->stream) 404 dc_stream_release(cur->stream); 405 406 407 __drm_atomic_helper_crtc_destroy_state(state); 408 409 410 kfree(state); 411 } 412 413 static struct drm_crtc_state *amdgpu_dm_crtc_duplicate_state(struct drm_crtc *crtc) 414 { 415 struct dm_crtc_state *state, *cur; 416 417 cur = to_dm_crtc_state(crtc->state); 418 419 if (WARN_ON(!crtc->state)) 420 return NULL; 421 422 state = kzalloc(sizeof(*state), GFP_KERNEL); 423 if (!state) 424 return NULL; 425 426 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base); 427 428 if (cur->stream) { 429 state->stream = cur->stream; 430 dc_stream_retain(state->stream); 431 } 432 433 state->active_planes = cur->active_planes; 434 state->vrr_infopacket = cur->vrr_infopacket; 435 state->abm_level = cur->abm_level; 436 state->vrr_supported = cur->vrr_supported; 437 state->freesync_config = cur->freesync_config; 438 state->cm_has_degamma = cur->cm_has_degamma; 439 state->cm_is_degamma_srgb = cur->cm_is_degamma_srgb; 440 state->regamma_tf = cur->regamma_tf; 441 state->crc_skip_count = cur->crc_skip_count; 442 state->mpo_requested = cur->mpo_requested; 443 state->cursor_mode = cur->cursor_mode; 444 /* TODO Duplicate dc_stream after objects are stream object is flattened */ 445 446 return &state->base; 447 } 448 449 static void amdgpu_dm_crtc_destroy(struct drm_crtc *crtc) 450 { 451 drm_crtc_cleanup(crtc); 452 kfree(crtc); 453 } 454 455 static void amdgpu_dm_crtc_reset_state(struct drm_crtc *crtc) 456 { 457 struct dm_crtc_state *state; 458 459 if (crtc->state) 460 amdgpu_dm_crtc_destroy_state(crtc, crtc->state); 461 462 state = kzalloc(sizeof(*state), GFP_KERNEL); 463 if (WARN_ON(!state)) 464 return; 465 466 __drm_atomic_helper_crtc_reset(crtc, &state->base); 467 } 468 469 #ifdef CONFIG_DEBUG_FS 470 static int amdgpu_dm_crtc_late_register(struct drm_crtc *crtc) 471 { 472 crtc_debugfs_init(crtc); 473 474 return 0; 475 } 476 #endif 477 478 #ifdef AMD_PRIVATE_COLOR 479 /** 480 * dm_crtc_additional_color_mgmt - enable additional color properties 481 * @crtc: DRM CRTC 482 * 483 * This function lets the driver enable post-blending CRTC regamma transfer 484 * function property in addition to DRM CRTC gamma LUT. Default value means 485 * linear transfer function, which is the default CRTC gamma LUT behaviour 486 * without this property. 487 */ 488 static void 489 dm_crtc_additional_color_mgmt(struct drm_crtc *crtc) 490 { 491 struct amdgpu_device *adev = drm_to_adev(crtc->dev); 492 493 if (adev->dm.dc->caps.color.mpc.ogam_ram) 494 drm_object_attach_property(&crtc->base, 495 adev->mode_info.regamma_tf_property, 496 AMDGPU_TRANSFER_FUNCTION_DEFAULT); 497 } 498 499 static int 500 amdgpu_dm_atomic_crtc_set_property(struct drm_crtc *crtc, 501 struct drm_crtc_state *state, 502 struct drm_property *property, 503 uint64_t val) 504 { 505 struct amdgpu_device *adev = drm_to_adev(crtc->dev); 506 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(state); 507 508 if (property == adev->mode_info.regamma_tf_property) { 509 if (acrtc_state->regamma_tf != val) { 510 acrtc_state->regamma_tf = val; 511 acrtc_state->base.color_mgmt_changed |= 1; 512 } 513 } else { 514 drm_dbg_atomic(crtc->dev, 515 "[CRTC:%d:%s] unknown property [PROP:%d:%s]]\n", 516 crtc->base.id, crtc->name, 517 property->base.id, property->name); 518 return -EINVAL; 519 } 520 521 return 0; 522 } 523 524 static int 525 amdgpu_dm_atomic_crtc_get_property(struct drm_crtc *crtc, 526 const struct drm_crtc_state *state, 527 struct drm_property *property, 528 uint64_t *val) 529 { 530 struct amdgpu_device *adev = drm_to_adev(crtc->dev); 531 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(state); 532 533 if (property == adev->mode_info.regamma_tf_property) 534 *val = acrtc_state->regamma_tf; 535 else 536 return -EINVAL; 537 538 return 0; 539 } 540 #endif 541 542 /* Implemented only the options currently available for the driver */ 543 static const struct drm_crtc_funcs amdgpu_dm_crtc_funcs = { 544 .reset = amdgpu_dm_crtc_reset_state, 545 .destroy = amdgpu_dm_crtc_destroy, 546 .set_config = drm_atomic_helper_set_config, 547 .page_flip = drm_atomic_helper_page_flip, 548 .atomic_duplicate_state = amdgpu_dm_crtc_duplicate_state, 549 .atomic_destroy_state = amdgpu_dm_crtc_destroy_state, 550 .set_crc_source = amdgpu_dm_crtc_set_crc_source, 551 .verify_crc_source = amdgpu_dm_crtc_verify_crc_source, 552 .get_crc_sources = amdgpu_dm_crtc_get_crc_sources, 553 .get_vblank_counter = amdgpu_get_vblank_counter_kms, 554 .enable_vblank = amdgpu_dm_crtc_enable_vblank, 555 .disable_vblank = amdgpu_dm_crtc_disable_vblank, 556 .get_vblank_timestamp = drm_crtc_vblank_helper_get_vblank_timestamp, 557 #if defined(CONFIG_DEBUG_FS) 558 .late_register = amdgpu_dm_crtc_late_register, 559 #endif 560 #ifdef AMD_PRIVATE_COLOR 561 .atomic_set_property = amdgpu_dm_atomic_crtc_set_property, 562 .atomic_get_property = amdgpu_dm_atomic_crtc_get_property, 563 #endif 564 }; 565 566 static void amdgpu_dm_crtc_helper_disable(struct drm_crtc *crtc) 567 { 568 } 569 570 static int amdgpu_dm_crtc_count_crtc_active_planes(struct drm_crtc_state *new_crtc_state) 571 { 572 struct drm_atomic_state *state = new_crtc_state->state; 573 struct drm_plane *plane; 574 int num_active = 0; 575 576 drm_for_each_plane_mask(plane, state->dev, new_crtc_state->plane_mask) { 577 struct drm_plane_state *new_plane_state; 578 579 /* Cursor planes are "fake". */ 580 if (plane->type == DRM_PLANE_TYPE_CURSOR) 581 continue; 582 583 new_plane_state = drm_atomic_get_new_plane_state(state, plane); 584 585 if (!new_plane_state) { 586 /* 587 * The plane is enable on the CRTC and hasn't changed 588 * state. This means that it previously passed 589 * validation and is therefore enabled. 590 */ 591 num_active += 1; 592 continue; 593 } 594 595 /* We need a framebuffer to be considered enabled. */ 596 num_active += (new_plane_state->fb != NULL); 597 } 598 599 return num_active; 600 } 601 602 static void amdgpu_dm_crtc_update_crtc_active_planes(struct drm_crtc *crtc, 603 struct drm_crtc_state *new_crtc_state) 604 { 605 struct dm_crtc_state *dm_new_crtc_state = 606 to_dm_crtc_state(new_crtc_state); 607 608 dm_new_crtc_state->active_planes = 0; 609 610 if (!dm_new_crtc_state->stream) 611 return; 612 613 dm_new_crtc_state->active_planes = 614 amdgpu_dm_crtc_count_crtc_active_planes(new_crtc_state); 615 } 616 617 static bool amdgpu_dm_crtc_helper_mode_fixup(struct drm_crtc *crtc, 618 const struct drm_display_mode *mode, 619 struct drm_display_mode *adjusted_mode) 620 { 621 return true; 622 } 623 624 static int amdgpu_dm_crtc_helper_atomic_check(struct drm_crtc *crtc, 625 struct drm_atomic_state *state) 626 { 627 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, 628 crtc); 629 struct amdgpu_device *adev = drm_to_adev(crtc->dev); 630 struct dc *dc = adev->dm.dc; 631 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state); 632 int ret = -EINVAL; 633 634 trace_amdgpu_dm_crtc_atomic_check(crtc_state); 635 636 amdgpu_dm_crtc_update_crtc_active_planes(crtc, crtc_state); 637 638 if (WARN_ON(unlikely(!dm_crtc_state->stream && 639 amdgpu_dm_crtc_modeset_required(crtc_state, NULL, dm_crtc_state->stream)))) { 640 return ret; 641 } 642 643 /* 644 * We require the primary plane to be enabled whenever the CRTC is, otherwise 645 * drm_mode_cursor_universal may end up trying to enable the cursor plane while all other 646 * planes are disabled, which is not supported by the hardware. And there is legacy 647 * userspace which stops using the HW cursor altogether in response to the resulting EINVAL. 648 */ 649 if (crtc_state->enable && 650 !(crtc_state->plane_mask & drm_plane_mask(crtc->primary))) { 651 DRM_DEBUG_ATOMIC("Can't enable a CRTC without enabling the primary plane\n"); 652 return -EINVAL; 653 } 654 655 /* 656 * Only allow async flips for fast updates that don't change the FB 657 * pitch, the DCC state, rotation, etc. 658 */ 659 if (crtc_state->async_flip && 660 dm_crtc_state->update_type != UPDATE_TYPE_FAST) { 661 drm_dbg_atomic(crtc->dev, 662 "[CRTC:%d:%s] async flips are only supported for fast updates\n", 663 crtc->base.id, crtc->name); 664 return -EINVAL; 665 } 666 667 /* In some use cases, like reset, no stream is attached */ 668 if (!dm_crtc_state->stream) 669 return 0; 670 671 if (dc_validate_stream(dc, dm_crtc_state->stream) == DC_OK) 672 return 0; 673 674 DRM_DEBUG_ATOMIC("Failed DC stream validation\n"); 675 return ret; 676 } 677 678 static const struct drm_crtc_helper_funcs amdgpu_dm_crtc_helper_funcs = { 679 .disable = amdgpu_dm_crtc_helper_disable, 680 .atomic_check = amdgpu_dm_crtc_helper_atomic_check, 681 .mode_fixup = amdgpu_dm_crtc_helper_mode_fixup, 682 .get_scanout_position = amdgpu_crtc_get_scanout_position, 683 }; 684 685 int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm, 686 struct drm_plane *plane, 687 uint32_t crtc_index) 688 { 689 struct amdgpu_crtc *acrtc = NULL; 690 struct drm_plane *cursor_plane; 691 bool is_dcn; 692 int res = -ENOMEM; 693 694 cursor_plane = kzalloc(sizeof(*cursor_plane), GFP_KERNEL); 695 if (!cursor_plane) 696 goto fail; 697 698 cursor_plane->type = DRM_PLANE_TYPE_CURSOR; 699 res = amdgpu_dm_plane_init(dm, cursor_plane, 0, NULL); 700 701 acrtc = kzalloc(sizeof(struct amdgpu_crtc), GFP_KERNEL); 702 if (!acrtc) 703 goto fail; 704 705 res = drm_crtc_init_with_planes( 706 dm->ddev, 707 &acrtc->base, 708 plane, 709 cursor_plane, 710 &amdgpu_dm_crtc_funcs, NULL); 711 712 if (res) 713 goto fail; 714 715 drm_crtc_helper_add(&acrtc->base, &amdgpu_dm_crtc_helper_funcs); 716 717 /* Create (reset) the plane state */ 718 if (acrtc->base.funcs->reset) 719 acrtc->base.funcs->reset(&acrtc->base); 720 721 acrtc->max_cursor_width = dm->adev->dm.dc->caps.max_cursor_size; 722 acrtc->max_cursor_height = dm->adev->dm.dc->caps.max_cursor_size; 723 724 acrtc->crtc_id = crtc_index; 725 acrtc->base.enabled = false; 726 acrtc->otg_inst = -1; 727 728 dm->adev->mode_info.crtcs[crtc_index] = acrtc; 729 730 /* Don't enable DRM CRTC degamma property for DCE since it doesn't 731 * support programmable degamma anywhere. 732 */ 733 is_dcn = dm->adev->dm.dc->caps.color.dpp.dcn_arch; 734 drm_crtc_enable_color_mgmt(&acrtc->base, is_dcn ? MAX_COLOR_LUT_ENTRIES : 0, 735 true, MAX_COLOR_LUT_ENTRIES); 736 737 drm_mode_crtc_set_gamma_size(&acrtc->base, MAX_COLOR_LEGACY_LUT_ENTRIES); 738 739 #ifdef AMD_PRIVATE_COLOR 740 dm_crtc_additional_color_mgmt(&acrtc->base); 741 #endif 742 return 0; 743 744 fail: 745 kfree(acrtc); 746 kfree(cursor_plane); 747 return res; 748 } 749 750