Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drm/amd/display: Create a file dedicated for CRTC

[Why]
The amdgpu_dm file contains most of the code that works as an interface
between DRM API and DC. As a result, this file becomes very large since
it comprises multiple abstractions such as CRTC manipulation.

[How]
This commit extracts the CRTC code to its specific file named
amdgpu_dm_crtc. This change does not change anything inside the
functions; the only exception is converting some static functions to a
global function.

v2: fix ifdef merge mix up (Alex)

Tested-by: Daniel Wheeler <daniel.wheeler@amd.com>
Reviewed-by: Harry Wentland <Harry.Wentland@amd.com>
Acked-by: Alan Liu <HaoPing.Liu@amd.com>
Signed-off-by: Rodrigo Siqueira <Rodrigo.Siqueira@amd.com>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com>

authored by

Rodrigo Siqueira and committed by
Alex Deucher
473683a0 5d945cbc

+516 -433
+1
drivers/gpu/drm/amd/display/amdgpu_dm/Makefile
··· 28 28 AMDGPUDM = \ 29 29 amdgpu_dm.o \ 30 30 amdgpu_dm_plane.o \ 31 + amdgpu_dm_crtc.o \ 31 32 amdgpu_dm_irq.o \ 32 33 amdgpu_dm_mst_types.o \ 33 34 amdgpu_dm_color.o
+1 -433
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c
··· 47 47 #include "atom.h" 48 48 #include "amdgpu_dm.h" 49 49 #include "amdgpu_dm_plane.h" 50 + #include "amdgpu_dm_crtc.h" 50 51 #ifdef CONFIG_DRM_AMD_DC_HDCP 51 52 #include "amdgpu_dm_hdcp.h" 52 53 #include <drm/display/drm_hdcp_helper.h> ··· 208 207 /* removes and deallocates the drm structures, created by the above function */ 209 208 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm); 210 209 211 - static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm, 212 - struct drm_plane *plane, 213 - uint32_t link_index); 214 210 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm, 215 211 struct amdgpu_dm_connector *amdgpu_dm_connector, 216 212 uint32_t link_index, ··· 336 338 return NULL; 337 339 } 338 340 339 - static inline bool amdgpu_dm_vrr_active_irq(struct amdgpu_crtc *acrtc) 340 - { 341 - return acrtc->dm_irq_params.freesync_config.state == 342 - VRR_STATE_ACTIVE_VARIABLE || 343 - acrtc->dm_irq_params.freesync_config.state == 344 - VRR_STATE_ACTIVE_FIXED; 345 - } 346 - 347 - static inline bool amdgpu_dm_vrr_active(struct dm_crtc_state *dm_state) 348 - { 349 - return dm_state->freesync_config.state == VRR_STATE_ACTIVE_VARIABLE || 350 - dm_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED; 351 - } 352 - 353 341 static inline bool is_dc_timing_adjust_needed(struct dm_crtc_state *old_state, 354 342 struct dm_crtc_state *new_state) 355 343 { ··· 449 465 DC_LOG_PFLIP("crtc:%d[%p], pflip_stat:AMDGPU_FLIP_NONE, vrr[%d]-fp %d\n", 450 466 amdgpu_crtc->crtc_id, amdgpu_crtc, 451 467 vrr_active, (int) !e); 452 - } 453 - 454 - static void dm_crtc_handle_vblank(struct amdgpu_crtc *acrtc) 455 - { 456 - struct drm_crtc *crtc = &acrtc->base; 457 - struct drm_device *dev = crtc->dev; 458 - unsigned long flags; 459 - 460 - drm_crtc_handle_vblank(crtc); 461 - 462 - spin_lock_irqsave(&dev->event_lock, flags); 463 - 464 - /* Send completion event for cursor-only commits */ 465 - if (acrtc->event && acrtc->pflip_status != AMDGPU_FLIP_SUBMITTED) { 466 - drm_crtc_send_vblank_event(crtc, acrtc->event); 467 - drm_crtc_vblank_put(crtc); 468 - acrtc->event = NULL; 469 - } 470 - 471 - spin_unlock_irqrestore(&dev->event_lock, flags); 472 468 } 473 469 474 470 static void dm_vupdate_high_irq(void *interrupt_params) ··· 1226 1262 1227 1263 pa_config->is_hvm_enabled = 0; 1228 1264 1229 - } 1230 - 1231 - static void vblank_control_worker(struct work_struct *work) 1232 - { 1233 - struct vblank_control_work *vblank_work = 1234 - container_of(work, struct vblank_control_work, work); 1235 - struct amdgpu_display_manager *dm = vblank_work->dm; 1236 - 1237 - mutex_lock(&dm->dc_lock); 1238 - 1239 - if (vblank_work->enable) 1240 - dm->active_vblank_irq_count++; 1241 - else if(dm->active_vblank_irq_count) 1242 - dm->active_vblank_irq_count--; 1243 - 1244 - dc_allow_idle_optimizations(dm->dc, dm->active_vblank_irq_count == 0); 1245 - 1246 - DRM_DEBUG_KMS("Allow idle optimizations (MALL): %d\n", dm->active_vblank_irq_count == 0); 1247 - 1248 - /* 1249 - * Control PSR based on vblank requirements from OS 1250 - * 1251 - * If panel supports PSR SU, there's no need to disable PSR when OS is 1252 - * submitting fast atomic commits (we infer this by whether the OS 1253 - * requests vblank events). Fast atomic commits will simply trigger a 1254 - * full-frame-update (FFU); a specific case of selective-update (SU) 1255 - * where the SU region is the full hactive*vactive region. See 1256 - * fill_dc_dirty_rects(). 1257 - */ 1258 - if (vblank_work->stream && vblank_work->stream->link) { 1259 - if (vblank_work->enable) { 1260 - if (vblank_work->stream->link->psr_settings.psr_version < DC_PSR_VERSION_SU_1 && 1261 - vblank_work->stream->link->psr_settings.psr_allow_active) 1262 - amdgpu_dm_psr_disable(vblank_work->stream); 1263 - } else if (vblank_work->stream->link->psr_settings.psr_feature_enabled && 1264 - !vblank_work->stream->link->psr_settings.psr_allow_active && 1265 - vblank_work->acrtc->dm_irq_params.allow_psr_entry) { 1266 - amdgpu_dm_psr_enable(vblank_work->stream); 1267 - } 1268 - } 1269 - 1270 - mutex_unlock(&dm->dc_lock); 1271 - 1272 - dc_stream_release(vblank_work->stream); 1273 - 1274 - kfree(vblank_work); 1275 1265 } 1276 1266 1277 1267 static void dm_handle_hpd_rx_offload_work(struct work_struct *work) ··· 2302 2384 return 0; 2303 2385 } 2304 2386 2305 - 2306 - static int dm_enable_vblank(struct drm_crtc *crtc); 2307 - static void dm_disable_vblank(struct drm_crtc *crtc); 2308 2387 2309 2388 static void dm_gpureset_toggle_interrupts(struct amdgpu_device *adev, 2310 2389 struct dc_state *state, bool enable) ··· 4594 4679 return 0; 4595 4680 } 4596 4681 4597 - static bool modeset_required(struct drm_crtc_state *crtc_state, 4598 - struct dc_stream_state *new_stream, 4599 - struct dc_stream_state *old_stream) 4600 - { 4601 - return crtc_state->active && drm_atomic_crtc_needs_modeset(crtc_state); 4602 - } 4603 - 4604 4682 static bool modereset_required(struct drm_crtc_state *crtc_state) 4605 4683 { 4606 4684 return !crtc_state->active && drm_atomic_crtc_needs_modeset(crtc_state); ··· 5824 5916 return stream; 5825 5917 } 5826 5918 5827 - static void amdgpu_dm_crtc_destroy(struct drm_crtc *crtc) 5828 - { 5829 - drm_crtc_cleanup(crtc); 5830 - kfree(crtc); 5831 - } 5832 - 5833 - static void dm_crtc_destroy_state(struct drm_crtc *crtc, 5834 - struct drm_crtc_state *state) 5835 - { 5836 - struct dm_crtc_state *cur = to_dm_crtc_state(state); 5837 - 5838 - /* TODO Destroy dc_stream objects are stream object is flattened */ 5839 - if (cur->stream) 5840 - dc_stream_release(cur->stream); 5841 - 5842 - 5843 - __drm_atomic_helper_crtc_destroy_state(state); 5844 - 5845 - 5846 - kfree(state); 5847 - } 5848 - 5849 - static void dm_crtc_reset_state(struct drm_crtc *crtc) 5850 - { 5851 - struct dm_crtc_state *state; 5852 - 5853 - if (crtc->state) 5854 - dm_crtc_destroy_state(crtc, crtc->state); 5855 - 5856 - state = kzalloc(sizeof(*state), GFP_KERNEL); 5857 - if (WARN_ON(!state)) 5858 - return; 5859 - 5860 - __drm_atomic_helper_crtc_reset(crtc, &state->base); 5861 - } 5862 - 5863 - static struct drm_crtc_state * 5864 - dm_crtc_duplicate_state(struct drm_crtc *crtc) 5865 - { 5866 - struct dm_crtc_state *state, *cur; 5867 - 5868 - cur = to_dm_crtc_state(crtc->state); 5869 - 5870 - if (WARN_ON(!crtc->state)) 5871 - return NULL; 5872 - 5873 - state = kzalloc(sizeof(*state), GFP_KERNEL); 5874 - if (!state) 5875 - return NULL; 5876 - 5877 - __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base); 5878 - 5879 - if (cur->stream) { 5880 - state->stream = cur->stream; 5881 - dc_stream_retain(state->stream); 5882 - } 5883 - 5884 - state->active_planes = cur->active_planes; 5885 - state->vrr_infopacket = cur->vrr_infopacket; 5886 - state->abm_level = cur->abm_level; 5887 - state->vrr_supported = cur->vrr_supported; 5888 - state->freesync_config = cur->freesync_config; 5889 - state->cm_has_degamma = cur->cm_has_degamma; 5890 - state->cm_is_degamma_srgb = cur->cm_is_degamma_srgb; 5891 - state->mpo_requested = cur->mpo_requested; 5892 - /* TODO Duplicate dc_stream after objects are stream object is flattened */ 5893 - 5894 - return &state->base; 5895 - } 5896 - 5897 - #ifdef CONFIG_DEBUG_FS 5898 - static int amdgpu_dm_crtc_late_register(struct drm_crtc *crtc) 5899 - { 5900 - crtc_debugfs_init(crtc); 5901 - 5902 - return 0; 5903 - } 5904 - #endif 5905 - 5906 - static inline int dm_set_vupdate_irq(struct drm_crtc *crtc, bool enable) 5907 - { 5908 - enum dc_irq_source irq_source; 5909 - struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc); 5910 - struct amdgpu_device *adev = drm_to_adev(crtc->dev); 5911 - int rc; 5912 - 5913 - irq_source = IRQ_TYPE_VUPDATE + acrtc->otg_inst; 5914 - 5915 - rc = dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY; 5916 - 5917 - DRM_DEBUG_VBL("crtc %d - vupdate irq %sabling: r=%d\n", 5918 - acrtc->crtc_id, enable ? "en" : "dis", rc); 5919 - return rc; 5920 - } 5921 - 5922 - static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable) 5923 - { 5924 - enum dc_irq_source irq_source; 5925 - struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc); 5926 - struct amdgpu_device *adev = drm_to_adev(crtc->dev); 5927 - struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state); 5928 - struct amdgpu_display_manager *dm = &adev->dm; 5929 - struct vblank_control_work *work; 5930 - int rc = 0; 5931 - 5932 - if (enable) { 5933 - /* vblank irq on -> Only need vupdate irq in vrr mode */ 5934 - if (amdgpu_dm_vrr_active(acrtc_state)) 5935 - rc = dm_set_vupdate_irq(crtc, true); 5936 - } else { 5937 - /* vblank irq off -> vupdate irq off */ 5938 - rc = dm_set_vupdate_irq(crtc, false); 5939 - } 5940 - 5941 - if (rc) 5942 - return rc; 5943 - 5944 - irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst; 5945 - 5946 - if (!dc_interrupt_set(adev->dm.dc, irq_source, enable)) 5947 - return -EBUSY; 5948 - 5949 - if (amdgpu_in_reset(adev)) 5950 - return 0; 5951 - 5952 - if (dm->vblank_control_workqueue) { 5953 - work = kzalloc(sizeof(*work), GFP_ATOMIC); 5954 - if (!work) 5955 - return -ENOMEM; 5956 - 5957 - INIT_WORK(&work->work, vblank_control_worker); 5958 - work->dm = dm; 5959 - work->acrtc = acrtc; 5960 - work->enable = enable; 5961 - 5962 - if (acrtc_state->stream) { 5963 - dc_stream_retain(acrtc_state->stream); 5964 - work->stream = acrtc_state->stream; 5965 - } 5966 - 5967 - queue_work(dm->vblank_control_workqueue, &work->work); 5968 - } 5969 - 5970 - return 0; 5971 - } 5972 - 5973 - static int dm_enable_vblank(struct drm_crtc *crtc) 5974 - { 5975 - return dm_set_vblank(crtc, true); 5976 - } 5977 - 5978 - static void dm_disable_vblank(struct drm_crtc *crtc) 5979 - { 5980 - dm_set_vblank(crtc, false); 5981 - } 5982 - 5983 - /* Implemented only the options currently available for the driver */ 5984 - static const struct drm_crtc_funcs amdgpu_dm_crtc_funcs = { 5985 - .reset = dm_crtc_reset_state, 5986 - .destroy = amdgpu_dm_crtc_destroy, 5987 - .set_config = drm_atomic_helper_set_config, 5988 - .page_flip = drm_atomic_helper_page_flip, 5989 - .atomic_duplicate_state = dm_crtc_duplicate_state, 5990 - .atomic_destroy_state = dm_crtc_destroy_state, 5991 - .set_crc_source = amdgpu_dm_crtc_set_crc_source, 5992 - .verify_crc_source = amdgpu_dm_crtc_verify_crc_source, 5993 - .get_crc_sources = amdgpu_dm_crtc_get_crc_sources, 5994 - .get_vblank_counter = amdgpu_get_vblank_counter_kms, 5995 - .enable_vblank = dm_enable_vblank, 5996 - .disable_vblank = dm_disable_vblank, 5997 - .get_vblank_timestamp = drm_crtc_vblank_helper_get_vblank_timestamp, 5998 - #if defined(CONFIG_DEBUG_FS) 5999 - .late_register = amdgpu_dm_crtc_late_register, 6000 - #endif 6001 - }; 6002 - 6003 5919 static enum drm_connector_status 6004 5920 amdgpu_dm_connector_detect(struct drm_connector *connector, bool force) 6005 5921 { ··· 6359 6627 .atomic_check = amdgpu_dm_connector_atomic_check, 6360 6628 }; 6361 6629 6362 - static void dm_crtc_helper_disable(struct drm_crtc *crtc) 6363 - { 6364 - } 6365 - 6366 - static int count_crtc_active_planes(struct drm_crtc_state *new_crtc_state) 6367 - { 6368 - struct drm_atomic_state *state = new_crtc_state->state; 6369 - struct drm_plane *plane; 6370 - int num_active = 0; 6371 - 6372 - drm_for_each_plane_mask(plane, state->dev, new_crtc_state->plane_mask) { 6373 - struct drm_plane_state *new_plane_state; 6374 - 6375 - /* Cursor planes are "fake". */ 6376 - if (plane->type == DRM_PLANE_TYPE_CURSOR) 6377 - continue; 6378 - 6379 - new_plane_state = drm_atomic_get_new_plane_state(state, plane); 6380 - 6381 - if (!new_plane_state) { 6382 - /* 6383 - * The plane is enable on the CRTC and hasn't changed 6384 - * state. This means that it previously passed 6385 - * validation and is therefore enabled. 6386 - */ 6387 - num_active += 1; 6388 - continue; 6389 - } 6390 - 6391 - /* We need a framebuffer to be considered enabled. */ 6392 - num_active += (new_plane_state->fb != NULL); 6393 - } 6394 - 6395 - return num_active; 6396 - } 6397 - 6398 - static void dm_update_crtc_active_planes(struct drm_crtc *crtc, 6399 - struct drm_crtc_state *new_crtc_state) 6400 - { 6401 - struct dm_crtc_state *dm_new_crtc_state = 6402 - to_dm_crtc_state(new_crtc_state); 6403 - 6404 - dm_new_crtc_state->active_planes = 0; 6405 - 6406 - if (!dm_new_crtc_state->stream) 6407 - return; 6408 - 6409 - dm_new_crtc_state->active_planes = 6410 - count_crtc_active_planes(new_crtc_state); 6411 - } 6412 - 6413 - static int dm_crtc_helper_atomic_check(struct drm_crtc *crtc, 6414 - struct drm_atomic_state *state) 6415 - { 6416 - struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, 6417 - crtc); 6418 - struct amdgpu_device *adev = drm_to_adev(crtc->dev); 6419 - struct dc *dc = adev->dm.dc; 6420 - struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state); 6421 - int ret = -EINVAL; 6422 - 6423 - trace_amdgpu_dm_crtc_atomic_check(crtc_state); 6424 - 6425 - dm_update_crtc_active_planes(crtc, crtc_state); 6426 - 6427 - if (WARN_ON(unlikely(!dm_crtc_state->stream && 6428 - modeset_required(crtc_state, NULL, dm_crtc_state->stream)))) { 6429 - return ret; 6430 - } 6431 - 6432 - /* 6433 - * We require the primary plane to be enabled whenever the CRTC is, otherwise 6434 - * drm_mode_cursor_universal may end up trying to enable the cursor plane while all other 6435 - * planes are disabled, which is not supported by the hardware. And there is legacy 6436 - * userspace which stops using the HW cursor altogether in response to the resulting EINVAL. 6437 - */ 6438 - if (crtc_state->enable && 6439 - !(crtc_state->plane_mask & drm_plane_mask(crtc->primary))) { 6440 - DRM_DEBUG_ATOMIC("Can't enable a CRTC without enabling the primary plane\n"); 6441 - return -EINVAL; 6442 - } 6443 - 6444 - /* In some use cases, like reset, no stream is attached */ 6445 - if (!dm_crtc_state->stream) 6446 - return 0; 6447 - 6448 - if (dc_validate_stream(dc, dm_crtc_state->stream) == DC_OK) 6449 - return 0; 6450 - 6451 - DRM_DEBUG_ATOMIC("Failed DC stream validation\n"); 6452 - return ret; 6453 - } 6454 - 6455 - static bool dm_crtc_helper_mode_fixup(struct drm_crtc *crtc, 6456 - const struct drm_display_mode *mode, 6457 - struct drm_display_mode *adjusted_mode) 6458 - { 6459 - return true; 6460 - } 6461 - 6462 - static const struct drm_crtc_helper_funcs amdgpu_dm_crtc_helper_funcs = { 6463 - .disable = dm_crtc_helper_disable, 6464 - .atomic_check = dm_crtc_helper_atomic_check, 6465 - .mode_fixup = dm_crtc_helper_mode_fixup, 6466 - .get_scanout_position = amdgpu_crtc_get_scanout_position, 6467 - }; 6468 - 6469 6630 static void dm_encoder_helper_disable(struct drm_encoder *encoder) 6470 6631 { 6471 6632 ··· 6515 6890 return 0; 6516 6891 } 6517 6892 #endif 6518 - 6519 - static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm, 6520 - struct drm_plane *plane, 6521 - uint32_t crtc_index) 6522 - { 6523 - struct amdgpu_crtc *acrtc = NULL; 6524 - struct drm_plane *cursor_plane; 6525 - 6526 - int res = -ENOMEM; 6527 - 6528 - cursor_plane = kzalloc(sizeof(*cursor_plane), GFP_KERNEL); 6529 - if (!cursor_plane) 6530 - goto fail; 6531 - 6532 - cursor_plane->type = DRM_PLANE_TYPE_CURSOR; 6533 - res = amdgpu_dm_plane_init(dm, cursor_plane, 0, NULL); 6534 - 6535 - acrtc = kzalloc(sizeof(struct amdgpu_crtc), GFP_KERNEL); 6536 - if (!acrtc) 6537 - goto fail; 6538 - 6539 - res = drm_crtc_init_with_planes( 6540 - dm->ddev, 6541 - &acrtc->base, 6542 - plane, 6543 - cursor_plane, 6544 - &amdgpu_dm_crtc_funcs, NULL); 6545 - 6546 - if (res) 6547 - goto fail; 6548 - 6549 - drm_crtc_helper_add(&acrtc->base, &amdgpu_dm_crtc_helper_funcs); 6550 - 6551 - /* Create (reset) the plane state */ 6552 - if (acrtc->base.funcs->reset) 6553 - acrtc->base.funcs->reset(&acrtc->base); 6554 - 6555 - acrtc->max_cursor_width = dm->adev->dm.dc->caps.max_cursor_size; 6556 - acrtc->max_cursor_height = dm->adev->dm.dc->caps.max_cursor_size; 6557 - 6558 - acrtc->crtc_id = crtc_index; 6559 - acrtc->base.enabled = false; 6560 - acrtc->otg_inst = -1; 6561 - 6562 - dm->adev->mode_info.crtcs[crtc_index] = acrtc; 6563 - drm_crtc_enable_color_mgmt(&acrtc->base, MAX_COLOR_LUT_ENTRIES, 6564 - true, MAX_COLOR_LUT_ENTRIES); 6565 - drm_mode_crtc_set_gamma_size(&acrtc->base, MAX_COLOR_LEGACY_LUT_ENTRIES); 6566 - 6567 - return 0; 6568 - 6569 - fail: 6570 - kfree(acrtc); 6571 - kfree(cursor_plane); 6572 - return res; 6573 - } 6574 - 6575 6893 6576 6894 static int to_drm_connector_type(enum signal_type st) 6577 6895 {
+463
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c
··· 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_crtc.h" 33 + #include "amdgpu_dm_plane.h" 34 + #include "amdgpu_dm_trace.h" 35 + #include "amdgpu_dm_debugfs.h" 36 + 37 + void dm_crtc_handle_vblank(struct amdgpu_crtc *acrtc) 38 + { 39 + struct drm_crtc *crtc = &acrtc->base; 40 + struct drm_device *dev = crtc->dev; 41 + unsigned long flags; 42 + 43 + drm_crtc_handle_vblank(crtc); 44 + 45 + spin_lock_irqsave(&dev->event_lock, flags); 46 + 47 + /* Send completion event for cursor-only commits */ 48 + if (acrtc->event && acrtc->pflip_status != AMDGPU_FLIP_SUBMITTED) { 49 + drm_crtc_send_vblank_event(crtc, acrtc->event); 50 + drm_crtc_vblank_put(crtc); 51 + acrtc->event = NULL; 52 + } 53 + 54 + spin_unlock_irqrestore(&dev->event_lock, flags); 55 + } 56 + 57 + bool modeset_required(struct drm_crtc_state *crtc_state, 58 + struct dc_stream_state *new_stream, 59 + struct dc_stream_state *old_stream) 60 + { 61 + return crtc_state->active && drm_atomic_crtc_needs_modeset(crtc_state); 62 + } 63 + 64 + bool amdgpu_dm_vrr_active_irq(struct amdgpu_crtc *acrtc) 65 + 66 + { 67 + return acrtc->dm_irq_params.freesync_config.state == 68 + VRR_STATE_ACTIVE_VARIABLE || 69 + acrtc->dm_irq_params.freesync_config.state == 70 + VRR_STATE_ACTIVE_FIXED; 71 + } 72 + 73 + int dm_set_vupdate_irq(struct drm_crtc *crtc, bool enable) 74 + { 75 + enum dc_irq_source irq_source; 76 + struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc); 77 + struct amdgpu_device *adev = drm_to_adev(crtc->dev); 78 + int rc; 79 + 80 + irq_source = IRQ_TYPE_VUPDATE + acrtc->otg_inst; 81 + 82 + rc = dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY; 83 + 84 + DRM_DEBUG_VBL("crtc %d - vupdate irq %sabling: r=%d\n", 85 + acrtc->crtc_id, enable ? "en" : "dis", rc); 86 + return rc; 87 + } 88 + 89 + bool amdgpu_dm_vrr_active(struct dm_crtc_state *dm_state) 90 + { 91 + return dm_state->freesync_config.state == VRR_STATE_ACTIVE_VARIABLE || 92 + dm_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED; 93 + } 94 + 95 + static void vblank_control_worker(struct work_struct *work) 96 + { 97 + struct vblank_control_work *vblank_work = 98 + container_of(work, struct vblank_control_work, work); 99 + struct amdgpu_display_manager *dm = vblank_work->dm; 100 + 101 + mutex_lock(&dm->dc_lock); 102 + 103 + if (vblank_work->enable) 104 + dm->active_vblank_irq_count++; 105 + else if (dm->active_vblank_irq_count) 106 + dm->active_vblank_irq_count--; 107 + 108 + dc_allow_idle_optimizations( 109 + dm->dc, dm->active_vblank_irq_count == 0 ? true : false); 110 + 111 + DRM_DEBUG_KMS("Allow idle optimizations (MALL): %d\n", dm->active_vblank_irq_count == 0); 112 + 113 + /* 114 + * Control PSR based on vblank requirements from OS 115 + * 116 + * If panel supports PSR SU, there's no need to disable PSR when OS is 117 + * submitting fast atomic commits (we infer this by whether the OS 118 + * requests vblank events). Fast atomic commits will simply trigger a 119 + * full-frame-update (FFU); a specific case of selective-update (SU) 120 + * where the SU region is the full hactive*vactive region. See 121 + * fill_dc_dirty_rects(). 122 + */ 123 + if (vblank_work->stream && vblank_work->stream->link) { 124 + if (vblank_work->enable) { 125 + if (vblank_work->stream->link->psr_settings.psr_version < DC_PSR_VERSION_SU_1 && 126 + vblank_work->stream->link->psr_settings.psr_allow_active) 127 + amdgpu_dm_psr_disable(vblank_work->stream); 128 + } else if (vblank_work->stream->link->psr_settings.psr_feature_enabled && 129 + !vblank_work->stream->link->psr_settings.psr_allow_active && 130 + vblank_work->acrtc->dm_irq_params.allow_psr_entry) { 131 + amdgpu_dm_psr_enable(vblank_work->stream); 132 + } 133 + } 134 + 135 + mutex_unlock(&dm->dc_lock); 136 + 137 + dc_stream_release(vblank_work->stream); 138 + 139 + kfree(vblank_work); 140 + } 141 + 142 + static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable) 143 + { 144 + enum dc_irq_source irq_source; 145 + struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc); 146 + struct amdgpu_device *adev = drm_to_adev(crtc->dev); 147 + struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state); 148 + struct amdgpu_display_manager *dm = &adev->dm; 149 + struct vblank_control_work *work; 150 + int rc = 0; 151 + 152 + if (enable) { 153 + /* vblank irq on -> Only need vupdate irq in vrr mode */ 154 + if (amdgpu_dm_vrr_active(acrtc_state)) 155 + rc = dm_set_vupdate_irq(crtc, true); 156 + } else { 157 + /* vblank irq off -> vupdate irq off */ 158 + rc = dm_set_vupdate_irq(crtc, false); 159 + } 160 + 161 + if (rc) 162 + return rc; 163 + 164 + irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst; 165 + 166 + if (!dc_interrupt_set(adev->dm.dc, irq_source, enable)) 167 + return -EBUSY; 168 + 169 + if (amdgpu_in_reset(adev)) 170 + return 0; 171 + 172 + if (dm->vblank_control_workqueue) { 173 + work = kzalloc(sizeof(*work), GFP_ATOMIC); 174 + if (!work) 175 + return -ENOMEM; 176 + 177 + INIT_WORK(&work->work, vblank_control_worker); 178 + work->dm = dm; 179 + work->acrtc = acrtc; 180 + work->enable = enable; 181 + 182 + if (acrtc_state->stream) { 183 + dc_stream_retain(acrtc_state->stream); 184 + work->stream = acrtc_state->stream; 185 + } 186 + 187 + queue_work(dm->vblank_control_workqueue, &work->work); 188 + } 189 + 190 + return 0; 191 + } 192 + 193 + int dm_enable_vblank(struct drm_crtc *crtc) 194 + { 195 + return dm_set_vblank(crtc, true); 196 + } 197 + 198 + void dm_disable_vblank(struct drm_crtc *crtc) 199 + { 200 + dm_set_vblank(crtc, false); 201 + } 202 + 203 + static void dm_crtc_destroy_state(struct drm_crtc *crtc, 204 + struct drm_crtc_state *state) 205 + { 206 + struct dm_crtc_state *cur = to_dm_crtc_state(state); 207 + 208 + /* TODO Destroy dc_stream objects are stream object is flattened */ 209 + if (cur->stream) 210 + dc_stream_release(cur->stream); 211 + 212 + 213 + __drm_atomic_helper_crtc_destroy_state(state); 214 + 215 + 216 + kfree(state); 217 + } 218 + 219 + static struct drm_crtc_state *dm_crtc_duplicate_state(struct drm_crtc *crtc) 220 + { 221 + struct dm_crtc_state *state, *cur; 222 + 223 + cur = to_dm_crtc_state(crtc->state); 224 + 225 + if (WARN_ON(!crtc->state)) 226 + return NULL; 227 + 228 + state = kzalloc(sizeof(*state), GFP_KERNEL); 229 + if (!state) 230 + return NULL; 231 + 232 + __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base); 233 + 234 + if (cur->stream) { 235 + state->stream = cur->stream; 236 + dc_stream_retain(state->stream); 237 + } 238 + 239 + state->active_planes = cur->active_planes; 240 + state->vrr_infopacket = cur->vrr_infopacket; 241 + state->abm_level = cur->abm_level; 242 + state->vrr_supported = cur->vrr_supported; 243 + state->freesync_config = cur->freesync_config; 244 + state->cm_has_degamma = cur->cm_has_degamma; 245 + state->cm_is_degamma_srgb = cur->cm_is_degamma_srgb; 246 + state->mpo_requested = cur->mpo_requested; 247 + /* TODO Duplicate dc_stream after objects are stream object is flattened */ 248 + 249 + return &state->base; 250 + } 251 + 252 + static void amdgpu_dm_crtc_destroy(struct drm_crtc *crtc) 253 + { 254 + drm_crtc_cleanup(crtc); 255 + kfree(crtc); 256 + } 257 + 258 + static void dm_crtc_reset_state(struct drm_crtc *crtc) 259 + { 260 + struct dm_crtc_state *state; 261 + 262 + if (crtc->state) 263 + dm_crtc_destroy_state(crtc, crtc->state); 264 + 265 + state = kzalloc(sizeof(*state), GFP_KERNEL); 266 + if (WARN_ON(!state)) 267 + return; 268 + 269 + __drm_atomic_helper_crtc_reset(crtc, &state->base); 270 + } 271 + 272 + #ifdef CONFIG_DEBUG_FS 273 + static int amdgpu_dm_crtc_late_register(struct drm_crtc *crtc) 274 + { 275 + crtc_debugfs_init(crtc); 276 + 277 + return 0; 278 + } 279 + #endif 280 + 281 + /* Implemented only the options currently available for the driver */ 282 + static const struct drm_crtc_funcs amdgpu_dm_crtc_funcs = { 283 + .reset = dm_crtc_reset_state, 284 + .destroy = amdgpu_dm_crtc_destroy, 285 + .set_config = drm_atomic_helper_set_config, 286 + .page_flip = drm_atomic_helper_page_flip, 287 + .atomic_duplicate_state = dm_crtc_duplicate_state, 288 + .atomic_destroy_state = dm_crtc_destroy_state, 289 + .set_crc_source = amdgpu_dm_crtc_set_crc_source, 290 + .verify_crc_source = amdgpu_dm_crtc_verify_crc_source, 291 + .get_crc_sources = amdgpu_dm_crtc_get_crc_sources, 292 + .get_vblank_counter = amdgpu_get_vblank_counter_kms, 293 + .enable_vblank = dm_enable_vblank, 294 + .disable_vblank = dm_disable_vblank, 295 + .get_vblank_timestamp = drm_crtc_vblank_helper_get_vblank_timestamp, 296 + #if defined(CONFIG_DEBUG_FS) 297 + .late_register = amdgpu_dm_crtc_late_register, 298 + #endif 299 + }; 300 + 301 + static void dm_crtc_helper_disable(struct drm_crtc *crtc) 302 + { 303 + } 304 + 305 + static int count_crtc_active_planes(struct drm_crtc_state *new_crtc_state) 306 + { 307 + struct drm_atomic_state *state = new_crtc_state->state; 308 + struct drm_plane *plane; 309 + int num_active = 0; 310 + 311 + drm_for_each_plane_mask(plane, state->dev, new_crtc_state->plane_mask) { 312 + struct drm_plane_state *new_plane_state; 313 + 314 + /* Cursor planes are "fake". */ 315 + if (plane->type == DRM_PLANE_TYPE_CURSOR) 316 + continue; 317 + 318 + new_plane_state = drm_atomic_get_new_plane_state(state, plane); 319 + 320 + if (!new_plane_state) { 321 + /* 322 + * The plane is enable on the CRTC and hasn't changed 323 + * state. This means that it previously passed 324 + * validation and is therefore enabled. 325 + */ 326 + num_active += 1; 327 + continue; 328 + } 329 + 330 + /* We need a framebuffer to be considered enabled. */ 331 + num_active += (new_plane_state->fb != NULL); 332 + } 333 + 334 + return num_active; 335 + } 336 + 337 + static void dm_update_crtc_active_planes(struct drm_crtc *crtc, 338 + struct drm_crtc_state *new_crtc_state) 339 + { 340 + struct dm_crtc_state *dm_new_crtc_state = 341 + to_dm_crtc_state(new_crtc_state); 342 + 343 + dm_new_crtc_state->active_planes = 0; 344 + 345 + if (!dm_new_crtc_state->stream) 346 + return; 347 + 348 + dm_new_crtc_state->active_planes = 349 + count_crtc_active_planes(new_crtc_state); 350 + } 351 + 352 + static bool dm_crtc_helper_mode_fixup(struct drm_crtc *crtc, 353 + const struct drm_display_mode *mode, 354 + struct drm_display_mode *adjusted_mode) 355 + { 356 + return true; 357 + } 358 + 359 + static int dm_crtc_helper_atomic_check(struct drm_crtc *crtc, 360 + struct drm_atomic_state *state) 361 + { 362 + struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, 363 + crtc); 364 + struct amdgpu_device *adev = drm_to_adev(crtc->dev); 365 + struct dc *dc = adev->dm.dc; 366 + struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state); 367 + int ret = -EINVAL; 368 + 369 + trace_amdgpu_dm_crtc_atomic_check(crtc_state); 370 + 371 + dm_update_crtc_active_planes(crtc, crtc_state); 372 + 373 + if (WARN_ON(unlikely(!dm_crtc_state->stream && 374 + modeset_required(crtc_state, NULL, dm_crtc_state->stream)))) { 375 + return ret; 376 + } 377 + 378 + /* 379 + * We require the primary plane to be enabled whenever the CRTC is, otherwise 380 + * drm_mode_cursor_universal may end up trying to enable the cursor plane while all other 381 + * planes are disabled, which is not supported by the hardware. And there is legacy 382 + * userspace which stops using the HW cursor altogether in response to the resulting EINVAL. 383 + */ 384 + if (crtc_state->enable && 385 + !(crtc_state->plane_mask & drm_plane_mask(crtc->primary))) { 386 + DRM_DEBUG_ATOMIC("Can't enable a CRTC without enabling the primary plane\n"); 387 + return -EINVAL; 388 + } 389 + 390 + /* In some use cases, like reset, no stream is attached */ 391 + if (!dm_crtc_state->stream) 392 + return 0; 393 + 394 + if (dc_validate_stream(dc, dm_crtc_state->stream) == DC_OK) 395 + return 0; 396 + 397 + DRM_DEBUG_ATOMIC("Failed DC stream validation\n"); 398 + return ret; 399 + } 400 + 401 + static const struct drm_crtc_helper_funcs amdgpu_dm_crtc_helper_funcs = { 402 + .disable = dm_crtc_helper_disable, 403 + .atomic_check = dm_crtc_helper_atomic_check, 404 + .mode_fixup = dm_crtc_helper_mode_fixup, 405 + .get_scanout_position = amdgpu_crtc_get_scanout_position, 406 + }; 407 + 408 + int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm, 409 + struct drm_plane *plane, 410 + uint32_t crtc_index) 411 + { 412 + struct amdgpu_crtc *acrtc = NULL; 413 + struct drm_plane *cursor_plane; 414 + 415 + int res = -ENOMEM; 416 + 417 + cursor_plane = kzalloc(sizeof(*cursor_plane), GFP_KERNEL); 418 + if (!cursor_plane) 419 + goto fail; 420 + 421 + cursor_plane->type = DRM_PLANE_TYPE_CURSOR; 422 + res = amdgpu_dm_plane_init(dm, cursor_plane, 0, NULL); 423 + 424 + acrtc = kzalloc(sizeof(struct amdgpu_crtc), GFP_KERNEL); 425 + if (!acrtc) 426 + goto fail; 427 + 428 + res = drm_crtc_init_with_planes( 429 + dm->ddev, 430 + &acrtc->base, 431 + plane, 432 + cursor_plane, 433 + &amdgpu_dm_crtc_funcs, NULL); 434 + 435 + if (res) 436 + goto fail; 437 + 438 + drm_crtc_helper_add(&acrtc->base, &amdgpu_dm_crtc_helper_funcs); 439 + 440 + /* Create (reset) the plane state */ 441 + if (acrtc->base.funcs->reset) 442 + acrtc->base.funcs->reset(&acrtc->base); 443 + 444 + acrtc->max_cursor_width = dm->adev->dm.dc->caps.max_cursor_size; 445 + acrtc->max_cursor_height = dm->adev->dm.dc->caps.max_cursor_size; 446 + 447 + acrtc->crtc_id = crtc_index; 448 + acrtc->base.enabled = false; 449 + acrtc->otg_inst = -1; 450 + 451 + dm->adev->mode_info.crtcs[crtc_index] = acrtc; 452 + drm_crtc_enable_color_mgmt(&acrtc->base, MAX_COLOR_LUT_ENTRIES, 453 + true, MAX_COLOR_LUT_ENTRIES); 454 + drm_mode_crtc_set_gamma_size(&acrtc->base, MAX_COLOR_LEGACY_LUT_ENTRIES); 455 + 456 + return 0; 457 + 458 + fail: 459 + kfree(acrtc); 460 + kfree(cursor_plane); 461 + return res; 462 + } 463 +
+51
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.h
··· 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 + 27 + #ifndef __AMDGPU_DM_CRTC_H__ 28 + #define __AMDGPU_DM_CRTC_H__ 29 + 30 + void dm_crtc_handle_vblank(struct amdgpu_crtc *acrtc); 31 + 32 + bool modeset_required(struct drm_crtc_state *crtc_state, 33 + struct dc_stream_state *new_stream, 34 + struct dc_stream_state *old_stream); 35 + 36 + int dm_set_vupdate_irq(struct drm_crtc *crtc, bool enable); 37 + 38 + bool amdgpu_dm_vrr_active_irq(struct amdgpu_crtc *acrtc); 39 + 40 + bool amdgpu_dm_vrr_active(struct dm_crtc_state *dm_state); 41 + 42 + int dm_enable_vblank(struct drm_crtc *crtc); 43 + 44 + void dm_disable_vblank(struct drm_crtc *crtc); 45 + 46 + int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm, 47 + struct drm_plane *plane, 48 + uint32_t link_index); 49 + 50 + #endif 51 +