2 * Copyright 2015 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
26 /* The caprices of the preprocessor require that this be declared right here */
27 #define CREATE_TRACE_POINTS
29 #include "dm_services_types.h"
31 #include "dc/inc/core_types.h"
35 #include "amdgpu_display.h"
36 #include "amdgpu_ucode.h"
38 #include "amdgpu_dm.h"
39 #include "amdgpu_pm.h"
41 #include "amd_shared.h"
42 #include "amdgpu_dm_irq.h"
43 #include "dm_helpers.h"
44 #include "amdgpu_dm_mst_types.h"
45 #if defined(CONFIG_DEBUG_FS)
46 #include "amdgpu_dm_debugfs.h"
49 #include "ivsrcid/ivsrcid_vislands30.h"
51 #include <linux/module.h>
52 #include <linux/moduleparam.h>
53 #include <linux/version.h>
54 #include <linux/types.h>
55 #include <linux/pm_runtime.h>
56 #include <linux/firmware.h>
59 #include <drm/drm_atomic.h>
60 #include <drm/drm_atomic_uapi.h>
61 #include <drm/drm_atomic_helper.h>
62 #include <drm/drm_dp_mst_helper.h>
63 #include <drm/drm_fb_helper.h>
64 #include <drm/drm_edid.h>
66 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
67 #include "ivsrcid/irqsrcs_dcn_1_0.h"
69 #include "dcn/dcn_1_0_offset.h"
70 #include "dcn/dcn_1_0_sh_mask.h"
71 #include "soc15_hw_ip.h"
72 #include "vega10_ip_offset.h"
74 #include "soc15_common.h"
77 #include "modules/inc/mod_freesync.h"
78 #include "modules/power/power_helpers.h"
79 #include "modules/inc/mod_info_packet.h"
81 #define FIRMWARE_RAVEN_DMCU "amdgpu/raven_dmcu.bin"
82 MODULE_FIRMWARE(FIRMWARE_RAVEN_DMCU);
87 * The AMDgpu display manager, **amdgpu_dm** (or even simpler,
88 * **dm**) sits between DRM and DC. It acts as a liason, converting DRM
89 * requests into DC requests, and DC responses into DRM responses.
91 * The root control structure is &struct amdgpu_display_manager.
94 /* basic init/fini API */
95 static int amdgpu_dm_init(struct amdgpu_device *adev);
96 static void amdgpu_dm_fini(struct amdgpu_device *adev);
99 * initializes drm_device display related structures, based on the information
100 * provided by DAL. The drm strcutures are: drm_crtc, drm_connector,
101 * drm_encoder, drm_mode_config
103 * Returns 0 on success
105 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev);
106 /* removes and deallocates the drm structures, created by the above function */
107 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm);
110 amdgpu_dm_update_connector_after_detect(struct amdgpu_dm_connector *aconnector);
112 static int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
113 struct drm_plane *plane,
114 unsigned long possible_crtcs,
115 const struct dc_plane_cap *plane_cap);
116 static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
117 struct drm_plane *plane,
118 uint32_t link_index);
119 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
120 struct amdgpu_dm_connector *amdgpu_dm_connector,
122 struct amdgpu_encoder *amdgpu_encoder);
123 static int amdgpu_dm_encoder_init(struct drm_device *dev,
124 struct amdgpu_encoder *aencoder,
125 uint32_t link_index);
127 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector);
129 static int amdgpu_dm_atomic_commit(struct drm_device *dev,
130 struct drm_atomic_state *state,
133 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state);
135 static int amdgpu_dm_atomic_check(struct drm_device *dev,
136 struct drm_atomic_state *state);
138 static void handle_cursor_update(struct drm_plane *plane,
139 struct drm_plane_state *old_plane_state);
142 * dm_vblank_get_counter
145 * Get counter for number of vertical blanks
148 * struct amdgpu_device *adev - [in] desired amdgpu device
149 * int disp_idx - [in] which CRTC to get the counter from
152 * Counter for vertical blanks
154 static u32 dm_vblank_get_counter(struct amdgpu_device *adev, int crtc)
156 if (crtc >= adev->mode_info.num_crtc)
159 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
160 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(
164 if (acrtc_state->stream == NULL) {
165 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
170 return dc_stream_get_vblank_counter(acrtc_state->stream);
174 static int dm_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
175 u32 *vbl, u32 *position)
177 uint32_t v_blank_start, v_blank_end, h_position, v_position;
179 if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
182 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
183 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(
186 if (acrtc_state->stream == NULL) {
187 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
193 * TODO rework base driver to use values directly.
194 * for now parse it back into reg-format
196 dc_stream_get_scanoutpos(acrtc_state->stream,
202 *position = v_position | (h_position << 16);
203 *vbl = v_blank_start | (v_blank_end << 16);
209 static bool dm_is_idle(void *handle)
215 static int dm_wait_for_idle(void *handle)
221 static bool dm_check_soft_reset(void *handle)
226 static int dm_soft_reset(void *handle)
232 static struct amdgpu_crtc *
233 get_crtc_by_otg_inst(struct amdgpu_device *adev,
236 struct drm_device *dev = adev->ddev;
237 struct drm_crtc *crtc;
238 struct amdgpu_crtc *amdgpu_crtc;
240 if (otg_inst == -1) {
242 return adev->mode_info.crtcs[0];
245 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
246 amdgpu_crtc = to_amdgpu_crtc(crtc);
248 if (amdgpu_crtc->otg_inst == otg_inst)
255 static inline bool amdgpu_dm_vrr_active(struct dm_crtc_state *dm_state)
257 return dm_state->freesync_config.state == VRR_STATE_ACTIVE_VARIABLE ||
258 dm_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED;
261 static void dm_pflip_high_irq(void *interrupt_params)
263 struct amdgpu_crtc *amdgpu_crtc;
264 struct common_irq_params *irq_params = interrupt_params;
265 struct amdgpu_device *adev = irq_params->adev;
267 struct drm_pending_vblank_event *e;
268 struct dm_crtc_state *acrtc_state;
269 uint32_t vpos, hpos, v_blank_start, v_blank_end;
272 amdgpu_crtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_PFLIP);
274 /* IRQ could occur when in initial stage */
275 /* TODO work and BO cleanup */
276 if (amdgpu_crtc == NULL) {
277 DRM_DEBUG_DRIVER("CRTC is null, returning.\n");
281 spin_lock_irqsave(&adev->ddev->event_lock, flags);
283 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED){
284 DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d !=AMDGPU_FLIP_SUBMITTED(%d) on crtc:%d[%p] \n",
285 amdgpu_crtc->pflip_status,
286 AMDGPU_FLIP_SUBMITTED,
287 amdgpu_crtc->crtc_id,
289 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
293 /* page flip completed. */
294 e = amdgpu_crtc->event;
295 amdgpu_crtc->event = NULL;
300 acrtc_state = to_dm_crtc_state(amdgpu_crtc->base.state);
301 vrr_active = amdgpu_dm_vrr_active(acrtc_state);
303 /* Fixed refresh rate, or VRR scanout position outside front-porch? */
305 !dc_stream_get_scanoutpos(acrtc_state->stream, &v_blank_start,
306 &v_blank_end, &hpos, &vpos) ||
307 (vpos < v_blank_start)) {
308 /* Update to correct count and vblank timestamp if racing with
309 * vblank irq. This also updates to the correct vblank timestamp
310 * even in VRR mode, as scanout is past the front-porch atm.
312 drm_crtc_accurate_vblank_count(&amdgpu_crtc->base);
314 /* Wake up userspace by sending the pageflip event with proper
315 * count and timestamp of vblank of flip completion.
318 drm_crtc_send_vblank_event(&amdgpu_crtc->base, e);
320 /* Event sent, so done with vblank for this flip */
321 drm_crtc_vblank_put(&amdgpu_crtc->base);
324 /* VRR active and inside front-porch: vblank count and
325 * timestamp for pageflip event will only be up to date after
326 * drm_crtc_handle_vblank() has been executed from late vblank
327 * irq handler after start of back-porch (vline 0). We queue the
328 * pageflip event for send-out by drm_crtc_handle_vblank() with
329 * updated timestamp and count, once it runs after us.
331 * We need to open-code this instead of using the helper
332 * drm_crtc_arm_vblank_event(), as that helper would
333 * call drm_crtc_accurate_vblank_count(), which we must
334 * not call in VRR mode while we are in front-porch!
337 /* sequence will be replaced by real count during send-out. */
338 e->sequence = drm_crtc_vblank_count(&amdgpu_crtc->base);
339 e->pipe = amdgpu_crtc->crtc_id;
341 list_add_tail(&e->base.link, &adev->ddev->vblank_event_list);
345 /* Keep track of vblank of this flip for flip throttling. We use the
346 * cooked hw counter, as that one incremented at start of this vblank
347 * of pageflip completion, so last_flip_vblank is the forbidden count
348 * for queueing new pageflips if vsync + VRR is enabled.
350 amdgpu_crtc->last_flip_vblank = amdgpu_get_vblank_counter_kms(adev->ddev,
351 amdgpu_crtc->crtc_id);
353 amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
354 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
356 DRM_DEBUG_DRIVER("crtc:%d[%p], pflip_stat:AMDGPU_FLIP_NONE, vrr[%d]-fp %d\n",
357 amdgpu_crtc->crtc_id, amdgpu_crtc,
358 vrr_active, (int) !e);
361 static void dm_vupdate_high_irq(void *interrupt_params)
363 struct common_irq_params *irq_params = interrupt_params;
364 struct amdgpu_device *adev = irq_params->adev;
365 struct amdgpu_crtc *acrtc;
366 struct dm_crtc_state *acrtc_state;
369 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VUPDATE);
372 acrtc_state = to_dm_crtc_state(acrtc->base.state);
374 DRM_DEBUG_DRIVER("crtc:%d, vupdate-vrr:%d\n", acrtc->crtc_id,
375 amdgpu_dm_vrr_active(acrtc_state));
377 /* Core vblank handling is done here after end of front-porch in
378 * vrr mode, as vblank timestamping will give valid results
379 * while now done after front-porch. This will also deliver
380 * page-flip completion events that have been queued to us
381 * if a pageflip happened inside front-porch.
383 if (amdgpu_dm_vrr_active(acrtc_state)) {
384 drm_crtc_handle_vblank(&acrtc->base);
386 /* BTR processing for pre-DCE12 ASICs */
387 if (acrtc_state->stream &&
388 adev->family < AMDGPU_FAMILY_AI) {
389 spin_lock_irqsave(&adev->ddev->event_lock, flags);
390 mod_freesync_handle_v_update(
391 adev->dm.freesync_module,
393 &acrtc_state->vrr_params);
395 dc_stream_adjust_vmin_vmax(
398 &acrtc_state->vrr_params.adjust);
399 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
405 static void dm_crtc_high_irq(void *interrupt_params)
407 struct common_irq_params *irq_params = interrupt_params;
408 struct amdgpu_device *adev = irq_params->adev;
409 struct amdgpu_crtc *acrtc;
410 struct dm_crtc_state *acrtc_state;
413 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VBLANK);
416 acrtc_state = to_dm_crtc_state(acrtc->base.state);
418 DRM_DEBUG_DRIVER("crtc:%d, vupdate-vrr:%d\n", acrtc->crtc_id,
419 amdgpu_dm_vrr_active(acrtc_state));
421 /* Core vblank handling at start of front-porch is only possible
422 * in non-vrr mode, as only there vblank timestamping will give
423 * valid results while done in front-porch. Otherwise defer it
424 * to dm_vupdate_high_irq after end of front-porch.
426 if (!amdgpu_dm_vrr_active(acrtc_state))
427 drm_crtc_handle_vblank(&acrtc->base);
429 /* Following stuff must happen at start of vblank, for crc
430 * computation and below-the-range btr support in vrr mode.
432 amdgpu_dm_crtc_handle_crc_irq(&acrtc->base);
434 if (acrtc_state->stream && adev->family >= AMDGPU_FAMILY_AI &&
435 acrtc_state->vrr_params.supported &&
436 acrtc_state->freesync_config.state == VRR_STATE_ACTIVE_VARIABLE) {
437 spin_lock_irqsave(&adev->ddev->event_lock, flags);
438 mod_freesync_handle_v_update(
439 adev->dm.freesync_module,
441 &acrtc_state->vrr_params);
443 dc_stream_adjust_vmin_vmax(
446 &acrtc_state->vrr_params.adjust);
447 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
452 static int dm_set_clockgating_state(void *handle,
453 enum amd_clockgating_state state)
458 static int dm_set_powergating_state(void *handle,
459 enum amd_powergating_state state)
464 /* Prototypes of private functions */
465 static int dm_early_init(void* handle);
467 /* Allocate memory for FBC compressed data */
468 static void amdgpu_dm_fbc_init(struct drm_connector *connector)
470 struct drm_device *dev = connector->dev;
471 struct amdgpu_device *adev = dev->dev_private;
472 struct dm_comressor_info *compressor = &adev->dm.compressor;
473 struct amdgpu_dm_connector *aconn = to_amdgpu_dm_connector(connector);
474 struct drm_display_mode *mode;
475 unsigned long max_size = 0;
477 if (adev->dm.dc->fbc_compressor == NULL)
480 if (aconn->dc_link->connector_signal != SIGNAL_TYPE_EDP)
483 if (compressor->bo_ptr)
487 list_for_each_entry(mode, &connector->modes, head) {
488 if (max_size < mode->htotal * mode->vtotal)
489 max_size = mode->htotal * mode->vtotal;
493 int r = amdgpu_bo_create_kernel(adev, max_size * 4, PAGE_SIZE,
494 AMDGPU_GEM_DOMAIN_GTT, &compressor->bo_ptr,
495 &compressor->gpu_addr, &compressor->cpu_addr);
498 DRM_ERROR("DM: Failed to initialize FBC\n");
500 adev->dm.dc->ctx->fbc_gpu_addr = compressor->gpu_addr;
501 DRM_INFO("DM: FBC alloc %lu\n", max_size*4);
508 static int amdgpu_dm_init(struct amdgpu_device *adev)
510 struct dc_init_data init_data;
511 adev->dm.ddev = adev->ddev;
512 adev->dm.adev = adev;
514 /* Zero all the fields */
515 memset(&init_data, 0, sizeof(init_data));
517 mutex_init(&adev->dm.dc_lock);
519 if(amdgpu_dm_irq_init(adev)) {
520 DRM_ERROR("amdgpu: failed to initialize DM IRQ support.\n");
524 init_data.asic_id.chip_family = adev->family;
526 init_data.asic_id.pci_revision_id = adev->rev_id;
527 init_data.asic_id.hw_internal_rev = adev->external_rev_id;
529 init_data.asic_id.vram_width = adev->gmc.vram_width;
530 /* TODO: initialize init_data.asic_id.vram_type here!!!! */
531 init_data.asic_id.atombios_base_address =
532 adev->mode_info.atom_context->bios;
534 init_data.driver = adev;
536 adev->dm.cgs_device = amdgpu_cgs_create_device(adev);
538 if (!adev->dm.cgs_device) {
539 DRM_ERROR("amdgpu: failed to create cgs device.\n");
543 init_data.cgs_device = adev->dm.cgs_device;
545 init_data.dce_environment = DCE_ENV_PRODUCTION_DRV;
548 * TODO debug why this doesn't work on Raven
550 if (adev->flags & AMD_IS_APU &&
551 adev->asic_type >= CHIP_CARRIZO &&
552 adev->asic_type < CHIP_RAVEN)
553 init_data.flags.gpu_vm_support = true;
555 if (amdgpu_dc_feature_mask & DC_FBC_MASK)
556 init_data.flags.fbc_support = true;
558 init_data.flags.power_down_display_on_boot = true;
560 /* Display Core create. */
561 adev->dm.dc = dc_create(&init_data);
564 DRM_INFO("Display Core initialized with v%s!\n", DC_VER);
566 DRM_INFO("Display Core failed to initialize with v%s!\n", DC_VER);
570 adev->dm.freesync_module = mod_freesync_create(adev->dm.dc);
571 if (!adev->dm.freesync_module) {
573 "amdgpu: failed to initialize freesync_module.\n");
575 DRM_DEBUG_DRIVER("amdgpu: freesync_module init done %p.\n",
576 adev->dm.freesync_module);
578 amdgpu_dm_init_color_mod();
580 if (amdgpu_dm_initialize_drm_device(adev)) {
582 "amdgpu: failed to initialize sw for display support.\n");
586 /* Update the actual used number of crtc */
587 adev->mode_info.num_crtc = adev->dm.display_indexes_num;
589 /* TODO: Add_display_info? */
591 /* TODO use dynamic cursor width */
592 adev->ddev->mode_config.cursor_width = adev->dm.dc->caps.max_cursor_size;
593 adev->ddev->mode_config.cursor_height = adev->dm.dc->caps.max_cursor_size;
595 if (drm_vblank_init(adev->ddev, adev->dm.display_indexes_num)) {
597 "amdgpu: failed to initialize sw for display support.\n");
601 #if defined(CONFIG_DEBUG_FS)
602 if (dtn_debugfs_init(adev))
603 DRM_ERROR("amdgpu: failed initialize dtn debugfs support.\n");
606 DRM_DEBUG_DRIVER("KMS initialized.\n");
610 amdgpu_dm_fini(adev);
615 static void amdgpu_dm_fini(struct amdgpu_device *adev)
617 amdgpu_dm_destroy_drm_device(&adev->dm);
619 * TODO: pageflip, vlank interrupt
621 * amdgpu_dm_irq_fini(adev);
624 if (adev->dm.cgs_device) {
625 amdgpu_cgs_destroy_device(adev->dm.cgs_device);
626 adev->dm.cgs_device = NULL;
628 if (adev->dm.freesync_module) {
629 mod_freesync_destroy(adev->dm.freesync_module);
630 adev->dm.freesync_module = NULL;
632 /* DC Destroy TODO: Replace destroy DAL */
634 dc_destroy(&adev->dm.dc);
636 mutex_destroy(&adev->dm.dc_lock);
641 static int load_dmcu_fw(struct amdgpu_device *adev)
643 const char *fw_name_dmcu;
645 const struct dmcu_firmware_header_v1_0 *hdr;
647 switch(adev->asic_type) {
666 fw_name_dmcu = FIRMWARE_RAVEN_DMCU;
669 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type);
673 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
674 DRM_DEBUG_KMS("dm: DMCU firmware not supported on direct or SMU loading\n");
678 r = request_firmware_direct(&adev->dm.fw_dmcu, fw_name_dmcu, adev->dev);
680 /* DMCU firmware is not necessary, so don't raise a fuss if it's missing */
681 DRM_DEBUG_KMS("dm: DMCU firmware not found\n");
682 adev->dm.fw_dmcu = NULL;
686 dev_err(adev->dev, "amdgpu_dm: Can't load firmware \"%s\"\n",
691 r = amdgpu_ucode_validate(adev->dm.fw_dmcu);
693 dev_err(adev->dev, "amdgpu_dm: Can't validate firmware \"%s\"\n",
695 release_firmware(adev->dm.fw_dmcu);
696 adev->dm.fw_dmcu = NULL;
700 hdr = (const struct dmcu_firmware_header_v1_0 *)adev->dm.fw_dmcu->data;
701 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].ucode_id = AMDGPU_UCODE_ID_DMCU_ERAM;
702 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].fw = adev->dm.fw_dmcu;
703 adev->firmware.fw_size +=
704 ALIGN(le32_to_cpu(hdr->header.ucode_size_bytes) - le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE);
706 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].ucode_id = AMDGPU_UCODE_ID_DMCU_INTV;
707 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].fw = adev->dm.fw_dmcu;
708 adev->firmware.fw_size +=
709 ALIGN(le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE);
711 adev->dm.dmcu_fw_version = le32_to_cpu(hdr->header.ucode_version);
713 DRM_DEBUG_KMS("PSP loading DMCU firmware\n");
718 static int dm_sw_init(void *handle)
720 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
722 return load_dmcu_fw(adev);
725 static int dm_sw_fini(void *handle)
727 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
729 if(adev->dm.fw_dmcu) {
730 release_firmware(adev->dm.fw_dmcu);
731 adev->dm.fw_dmcu = NULL;
737 static int detect_mst_link_for_all_connectors(struct drm_device *dev)
739 struct amdgpu_dm_connector *aconnector;
740 struct drm_connector *connector;
743 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
745 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
746 aconnector = to_amdgpu_dm_connector(connector);
747 if (aconnector->dc_link->type == dc_connection_mst_branch &&
748 aconnector->mst_mgr.aux) {
749 DRM_DEBUG_DRIVER("DM_MST: starting TM on aconnector: %p [id: %d]\n",
750 aconnector, aconnector->base.base.id);
752 ret = drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true);
754 DRM_ERROR("DM_MST: Failed to start MST\n");
755 ((struct dc_link *)aconnector->dc_link)->type = dc_connection_single;
761 drm_modeset_unlock(&dev->mode_config.connection_mutex);
765 static int dm_late_init(void *handle)
767 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
769 struct dmcu_iram_parameters params;
770 unsigned int linear_lut[16];
772 struct dmcu *dmcu = adev->dm.dc->res_pool->dmcu;
775 for (i = 0; i < 16; i++)
776 linear_lut[i] = 0xFFFF * i / 15;
779 params.backlight_ramping_start = 0xCCCC;
780 params.backlight_ramping_reduction = 0xCCCCCCCC;
781 params.backlight_lut_array_size = 16;
782 params.backlight_lut_array = linear_lut;
784 ret = dmcu_load_iram(dmcu, params);
789 return detect_mst_link_for_all_connectors(adev->ddev);
792 static void s3_handle_mst(struct drm_device *dev, bool suspend)
794 struct amdgpu_dm_connector *aconnector;
795 struct drm_connector *connector;
796 struct drm_dp_mst_topology_mgr *mgr;
798 bool need_hotplug = false;
800 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
802 list_for_each_entry(connector, &dev->mode_config.connector_list,
804 aconnector = to_amdgpu_dm_connector(connector);
805 if (aconnector->dc_link->type != dc_connection_mst_branch ||
806 aconnector->mst_port)
809 mgr = &aconnector->mst_mgr;
812 drm_dp_mst_topology_mgr_suspend(mgr);
814 ret = drm_dp_mst_topology_mgr_resume(mgr);
816 drm_dp_mst_topology_mgr_set_mst(mgr, false);
822 drm_modeset_unlock(&dev->mode_config.connection_mutex);
825 drm_kms_helper_hotplug_event(dev);
829 * dm_hw_init() - Initialize DC device
830 * @handle: The base driver device containing the amdpgu_dm device.
832 * Initialize the &struct amdgpu_display_manager device. This involves calling
833 * the initializers of each DM component, then populating the struct with them.
835 * Although the function implies hardware initialization, both hardware and
836 * software are initialized here. Splitting them out to their relevant init
837 * hooks is a future TODO item.
839 * Some notable things that are initialized here:
841 * - Display Core, both software and hardware
842 * - DC modules that we need (freesync and color management)
843 * - DRM software states
844 * - Interrupt sources and handlers
846 * - Debug FS entries, if enabled
848 static int dm_hw_init(void *handle)
850 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
851 /* Create DAL display manager */
852 amdgpu_dm_init(adev);
853 amdgpu_dm_hpd_init(adev);
859 * dm_hw_fini() - Teardown DC device
860 * @handle: The base driver device containing the amdpgu_dm device.
862 * Teardown components within &struct amdgpu_display_manager that require
863 * cleanup. This involves cleaning up the DRM device, DC, and any modules that
864 * were loaded. Also flush IRQ workqueues and disable them.
866 static int dm_hw_fini(void *handle)
868 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
870 amdgpu_dm_hpd_fini(adev);
872 amdgpu_dm_irq_fini(adev);
873 amdgpu_dm_fini(adev);
877 static int dm_suspend(void *handle)
879 struct amdgpu_device *adev = handle;
880 struct amdgpu_display_manager *dm = &adev->dm;
883 WARN_ON(adev->dm.cached_state);
884 adev->dm.cached_state = drm_atomic_helper_suspend(adev->ddev);
886 s3_handle_mst(adev->ddev, true);
888 amdgpu_dm_irq_suspend(adev);
891 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D3);
896 static struct amdgpu_dm_connector *
897 amdgpu_dm_find_first_crtc_matching_connector(struct drm_atomic_state *state,
898 struct drm_crtc *crtc)
901 struct drm_connector_state *new_con_state;
902 struct drm_connector *connector;
903 struct drm_crtc *crtc_from_state;
905 for_each_new_connector_in_state(state, connector, new_con_state, i) {
906 crtc_from_state = new_con_state->crtc;
908 if (crtc_from_state == crtc)
909 return to_amdgpu_dm_connector(connector);
915 static void emulated_link_detect(struct dc_link *link)
917 struct dc_sink_init_data sink_init_data = { 0 };
918 struct display_sink_capability sink_caps = { 0 };
919 enum dc_edid_status edid_status;
920 struct dc_context *dc_ctx = link->ctx;
921 struct dc_sink *sink = NULL;
922 struct dc_sink *prev_sink = NULL;
924 link->type = dc_connection_none;
925 prev_sink = link->local_sink;
927 if (prev_sink != NULL)
928 dc_sink_retain(prev_sink);
930 switch (link->connector_signal) {
931 case SIGNAL_TYPE_HDMI_TYPE_A: {
932 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
933 sink_caps.signal = SIGNAL_TYPE_HDMI_TYPE_A;
937 case SIGNAL_TYPE_DVI_SINGLE_LINK: {
938 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
939 sink_caps.signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
943 case SIGNAL_TYPE_DVI_DUAL_LINK: {
944 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
945 sink_caps.signal = SIGNAL_TYPE_DVI_DUAL_LINK;
949 case SIGNAL_TYPE_LVDS: {
950 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
951 sink_caps.signal = SIGNAL_TYPE_LVDS;
955 case SIGNAL_TYPE_EDP: {
956 sink_caps.transaction_type =
957 DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
958 sink_caps.signal = SIGNAL_TYPE_EDP;
962 case SIGNAL_TYPE_DISPLAY_PORT: {
963 sink_caps.transaction_type =
964 DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
965 sink_caps.signal = SIGNAL_TYPE_VIRTUAL;
970 DC_ERROR("Invalid connector type! signal:%d\n",
971 link->connector_signal);
975 sink_init_data.link = link;
976 sink_init_data.sink_signal = sink_caps.signal;
978 sink = dc_sink_create(&sink_init_data);
980 DC_ERROR("Failed to create sink!\n");
984 /* dc_sink_create returns a new reference */
985 link->local_sink = sink;
987 edid_status = dm_helpers_read_local_edid(
992 if (edid_status != EDID_OK)
993 DC_ERROR("Failed to read EDID");
997 static int dm_resume(void *handle)
999 struct amdgpu_device *adev = handle;
1000 struct drm_device *ddev = adev->ddev;
1001 struct amdgpu_display_manager *dm = &adev->dm;
1002 struct amdgpu_dm_connector *aconnector;
1003 struct drm_connector *connector;
1004 struct drm_crtc *crtc;
1005 struct drm_crtc_state *new_crtc_state;
1006 struct dm_crtc_state *dm_new_crtc_state;
1007 struct drm_plane *plane;
1008 struct drm_plane_state *new_plane_state;
1009 struct dm_plane_state *dm_new_plane_state;
1010 struct dm_atomic_state *dm_state = to_dm_atomic_state(dm->atomic_obj.state);
1011 enum dc_connection_type new_connection_type = dc_connection_none;
1014 /* Recreate dc_state - DC invalidates it when setting power state to S3. */
1015 dc_release_state(dm_state->context);
1016 dm_state->context = dc_create_state(dm->dc);
1017 /* TODO: Remove dc_state->dccg, use dc->dccg directly. */
1018 dc_resource_state_construct(dm->dc, dm_state->context);
1020 /* power on hardware */
1021 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0);
1023 /* program HPD filter */
1026 /* On resume we need to rewrite the MSTM control bits to enamble MST*/
1027 s3_handle_mst(ddev, false);
1030 * early enable HPD Rx IRQ, should be done before set mode as short
1031 * pulse interrupts are used for MST
1033 amdgpu_dm_irq_resume_early(adev);
1036 list_for_each_entry(connector, &ddev->mode_config.connector_list, head) {
1037 aconnector = to_amdgpu_dm_connector(connector);
1040 * this is the case when traversing through already created
1041 * MST connectors, should be skipped
1043 if (aconnector->mst_port)
1046 mutex_lock(&aconnector->hpd_lock);
1047 if (!dc_link_detect_sink(aconnector->dc_link, &new_connection_type))
1048 DRM_ERROR("KMS: Failed to detect connector\n");
1050 if (aconnector->base.force && new_connection_type == dc_connection_none)
1051 emulated_link_detect(aconnector->dc_link);
1053 dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
1055 if (aconnector->fake_enable && aconnector->dc_link->local_sink)
1056 aconnector->fake_enable = false;
1058 if (aconnector->dc_sink)
1059 dc_sink_release(aconnector->dc_sink);
1060 aconnector->dc_sink = NULL;
1061 amdgpu_dm_update_connector_after_detect(aconnector);
1062 mutex_unlock(&aconnector->hpd_lock);
1065 /* Force mode set in atomic commit */
1066 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i)
1067 new_crtc_state->active_changed = true;
1070 * atomic_check is expected to create the dc states. We need to release
1071 * them here, since they were duplicated as part of the suspend
1074 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) {
1075 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
1076 if (dm_new_crtc_state->stream) {
1077 WARN_ON(kref_read(&dm_new_crtc_state->stream->refcount) > 1);
1078 dc_stream_release(dm_new_crtc_state->stream);
1079 dm_new_crtc_state->stream = NULL;
1083 for_each_new_plane_in_state(dm->cached_state, plane, new_plane_state, i) {
1084 dm_new_plane_state = to_dm_plane_state(new_plane_state);
1085 if (dm_new_plane_state->dc_state) {
1086 WARN_ON(kref_read(&dm_new_plane_state->dc_state->refcount) > 1);
1087 dc_plane_state_release(dm_new_plane_state->dc_state);
1088 dm_new_plane_state->dc_state = NULL;
1092 drm_atomic_helper_resume(ddev, dm->cached_state);
1094 dm->cached_state = NULL;
1096 amdgpu_dm_irq_resume_late(adev);
1104 * DM (and consequently DC) is registered in the amdgpu base driver as a IP
1105 * block. When CONFIG_DRM_AMD_DC is enabled, the DM device IP block is added to
1106 * the base driver's device list to be initialized and torn down accordingly.
1108 * The functions to do so are provided as hooks in &struct amd_ip_funcs.
1111 static const struct amd_ip_funcs amdgpu_dm_funcs = {
1113 .early_init = dm_early_init,
1114 .late_init = dm_late_init,
1115 .sw_init = dm_sw_init,
1116 .sw_fini = dm_sw_fini,
1117 .hw_init = dm_hw_init,
1118 .hw_fini = dm_hw_fini,
1119 .suspend = dm_suspend,
1120 .resume = dm_resume,
1121 .is_idle = dm_is_idle,
1122 .wait_for_idle = dm_wait_for_idle,
1123 .check_soft_reset = dm_check_soft_reset,
1124 .soft_reset = dm_soft_reset,
1125 .set_clockgating_state = dm_set_clockgating_state,
1126 .set_powergating_state = dm_set_powergating_state,
1129 const struct amdgpu_ip_block_version dm_ip_block =
1131 .type = AMD_IP_BLOCK_TYPE_DCE,
1135 .funcs = &amdgpu_dm_funcs,
1145 static const struct drm_mode_config_funcs amdgpu_dm_mode_funcs = {
1146 .fb_create = amdgpu_display_user_framebuffer_create,
1147 .output_poll_changed = drm_fb_helper_output_poll_changed,
1148 .atomic_check = amdgpu_dm_atomic_check,
1149 .atomic_commit = amdgpu_dm_atomic_commit,
1152 static struct drm_mode_config_helper_funcs amdgpu_dm_mode_config_helperfuncs = {
1153 .atomic_commit_tail = amdgpu_dm_atomic_commit_tail
1157 amdgpu_dm_update_connector_after_detect(struct amdgpu_dm_connector *aconnector)
1159 struct drm_connector *connector = &aconnector->base;
1160 struct drm_device *dev = connector->dev;
1161 struct dc_sink *sink;
1163 /* MST handled by drm_mst framework */
1164 if (aconnector->mst_mgr.mst_state == true)
1168 sink = aconnector->dc_link->local_sink;
1170 dc_sink_retain(sink);
1173 * Edid mgmt connector gets first update only in mode_valid hook and then
1174 * the connector sink is set to either fake or physical sink depends on link status.
1175 * Skip if already done during boot.
1177 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED
1178 && aconnector->dc_em_sink) {
1181 * For S3 resume with headless use eml_sink to fake stream
1182 * because on resume connector->sink is set to NULL
1184 mutex_lock(&dev->mode_config.mutex);
1187 if (aconnector->dc_sink) {
1188 amdgpu_dm_update_freesync_caps(connector, NULL);
1190 * retain and release below are used to
1191 * bump up refcount for sink because the link doesn't point
1192 * to it anymore after disconnect, so on next crtc to connector
1193 * reshuffle by UMD we will get into unwanted dc_sink release
1195 dc_sink_release(aconnector->dc_sink);
1197 aconnector->dc_sink = sink;
1198 dc_sink_retain(aconnector->dc_sink);
1199 amdgpu_dm_update_freesync_caps(connector,
1202 amdgpu_dm_update_freesync_caps(connector, NULL);
1203 if (!aconnector->dc_sink) {
1204 aconnector->dc_sink = aconnector->dc_em_sink;
1205 dc_sink_retain(aconnector->dc_sink);
1209 mutex_unlock(&dev->mode_config.mutex);
1212 dc_sink_release(sink);
1217 * TODO: temporary guard to look for proper fix
1218 * if this sink is MST sink, we should not do anything
1220 if (sink && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
1221 dc_sink_release(sink);
1225 if (aconnector->dc_sink == sink) {
1227 * We got a DP short pulse (Link Loss, DP CTS, etc...).
1230 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: dc_sink didn't change.\n",
1231 aconnector->connector_id);
1233 dc_sink_release(sink);
1237 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: Old sink=%p New sink=%p\n",
1238 aconnector->connector_id, aconnector->dc_sink, sink);
1240 mutex_lock(&dev->mode_config.mutex);
1243 * 1. Update status of the drm connector
1244 * 2. Send an event and let userspace tell us what to do
1248 * TODO: check if we still need the S3 mode update workaround.
1249 * If yes, put it here.
1251 if (aconnector->dc_sink)
1252 amdgpu_dm_update_freesync_caps(connector, NULL);
1254 aconnector->dc_sink = sink;
1255 dc_sink_retain(aconnector->dc_sink);
1256 if (sink->dc_edid.length == 0) {
1257 aconnector->edid = NULL;
1258 drm_dp_cec_unset_edid(&aconnector->dm_dp_aux.aux);
1261 (struct edid *) sink->dc_edid.raw_edid;
1264 drm_connector_update_edid_property(connector,
1266 drm_dp_cec_set_edid(&aconnector->dm_dp_aux.aux,
1269 amdgpu_dm_update_freesync_caps(connector, aconnector->edid);
1272 drm_dp_cec_unset_edid(&aconnector->dm_dp_aux.aux);
1273 amdgpu_dm_update_freesync_caps(connector, NULL);
1274 drm_connector_update_edid_property(connector, NULL);
1275 aconnector->num_modes = 0;
1276 dc_sink_release(aconnector->dc_sink);
1277 aconnector->dc_sink = NULL;
1278 aconnector->edid = NULL;
1281 mutex_unlock(&dev->mode_config.mutex);
1284 dc_sink_release(sink);
1287 static void handle_hpd_irq(void *param)
1289 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
1290 struct drm_connector *connector = &aconnector->base;
1291 struct drm_device *dev = connector->dev;
1292 enum dc_connection_type new_connection_type = dc_connection_none;
1295 * In case of failure or MST no need to update connector status or notify the OS
1296 * since (for MST case) MST does this in its own context.
1298 mutex_lock(&aconnector->hpd_lock);
1300 if (aconnector->fake_enable)
1301 aconnector->fake_enable = false;
1303 if (!dc_link_detect_sink(aconnector->dc_link, &new_connection_type))
1304 DRM_ERROR("KMS: Failed to detect connector\n");
1306 if (aconnector->base.force && new_connection_type == dc_connection_none) {
1307 emulated_link_detect(aconnector->dc_link);
1310 drm_modeset_lock_all(dev);
1311 dm_restore_drm_connector_state(dev, connector);
1312 drm_modeset_unlock_all(dev);
1314 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
1315 drm_kms_helper_hotplug_event(dev);
1317 } else if (dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD)) {
1318 amdgpu_dm_update_connector_after_detect(aconnector);
1321 drm_modeset_lock_all(dev);
1322 dm_restore_drm_connector_state(dev, connector);
1323 drm_modeset_unlock_all(dev);
1325 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
1326 drm_kms_helper_hotplug_event(dev);
1328 mutex_unlock(&aconnector->hpd_lock);
1332 static void dm_handle_hpd_rx_irq(struct amdgpu_dm_connector *aconnector)
1334 uint8_t esi[DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI] = { 0 };
1336 bool new_irq_handled = false;
1338 int dpcd_bytes_to_read;
1340 const int max_process_count = 30;
1341 int process_count = 0;
1343 const struct dc_link_status *link_status = dc_link_get_status(aconnector->dc_link);
1345 if (link_status->dpcd_caps->dpcd_rev.raw < 0x12) {
1346 dpcd_bytes_to_read = DP_LANE0_1_STATUS - DP_SINK_COUNT;
1347 /* DPCD 0x200 - 0x201 for downstream IRQ */
1348 dpcd_addr = DP_SINK_COUNT;
1350 dpcd_bytes_to_read = DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI;
1351 /* DPCD 0x2002 - 0x2005 for downstream IRQ */
1352 dpcd_addr = DP_SINK_COUNT_ESI;
1355 dret = drm_dp_dpcd_read(
1356 &aconnector->dm_dp_aux.aux,
1359 dpcd_bytes_to_read);
1361 while (dret == dpcd_bytes_to_read &&
1362 process_count < max_process_count) {
1368 DRM_DEBUG_DRIVER("ESI %02x %02x %02x\n", esi[0], esi[1], esi[2]);
1369 /* handle HPD short pulse irq */
1370 if (aconnector->mst_mgr.mst_state)
1372 &aconnector->mst_mgr,
1376 if (new_irq_handled) {
1377 /* ACK at DPCD to notify down stream */
1378 const int ack_dpcd_bytes_to_write =
1379 dpcd_bytes_to_read - 1;
1381 for (retry = 0; retry < 3; retry++) {
1384 wret = drm_dp_dpcd_write(
1385 &aconnector->dm_dp_aux.aux,
1388 ack_dpcd_bytes_to_write);
1389 if (wret == ack_dpcd_bytes_to_write)
1393 /* check if there is new irq to be handled */
1394 dret = drm_dp_dpcd_read(
1395 &aconnector->dm_dp_aux.aux,
1398 dpcd_bytes_to_read);
1400 new_irq_handled = false;
1406 if (process_count == max_process_count)
1407 DRM_DEBUG_DRIVER("Loop exceeded max iterations\n");
1410 static void handle_hpd_rx_irq(void *param)
1412 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
1413 struct drm_connector *connector = &aconnector->base;
1414 struct drm_device *dev = connector->dev;
1415 struct dc_link *dc_link = aconnector->dc_link;
1416 bool is_mst_root_connector = aconnector->mst_mgr.mst_state;
1417 enum dc_connection_type new_connection_type = dc_connection_none;
1420 * TODO:Temporary add mutex to protect hpd interrupt not have a gpio
1421 * conflict, after implement i2c helper, this mutex should be
1424 if (dc_link->type != dc_connection_mst_branch)
1425 mutex_lock(&aconnector->hpd_lock);
1427 if (dc_link_handle_hpd_rx_irq(dc_link, NULL, NULL) &&
1428 !is_mst_root_connector) {
1429 /* Downstream Port status changed. */
1430 if (!dc_link_detect_sink(dc_link, &new_connection_type))
1431 DRM_ERROR("KMS: Failed to detect connector\n");
1433 if (aconnector->base.force && new_connection_type == dc_connection_none) {
1434 emulated_link_detect(dc_link);
1436 if (aconnector->fake_enable)
1437 aconnector->fake_enable = false;
1439 amdgpu_dm_update_connector_after_detect(aconnector);
1442 drm_modeset_lock_all(dev);
1443 dm_restore_drm_connector_state(dev, connector);
1444 drm_modeset_unlock_all(dev);
1446 drm_kms_helper_hotplug_event(dev);
1447 } else if (dc_link_detect(dc_link, DETECT_REASON_HPDRX)) {
1449 if (aconnector->fake_enable)
1450 aconnector->fake_enable = false;
1452 amdgpu_dm_update_connector_after_detect(aconnector);
1455 drm_modeset_lock_all(dev);
1456 dm_restore_drm_connector_state(dev, connector);
1457 drm_modeset_unlock_all(dev);
1459 drm_kms_helper_hotplug_event(dev);
1462 if ((dc_link->cur_link_settings.lane_count != LANE_COUNT_UNKNOWN) ||
1463 (dc_link->type == dc_connection_mst_branch))
1464 dm_handle_hpd_rx_irq(aconnector);
1466 if (dc_link->type != dc_connection_mst_branch) {
1467 drm_dp_cec_irq(&aconnector->dm_dp_aux.aux);
1468 mutex_unlock(&aconnector->hpd_lock);
1472 static void register_hpd_handlers(struct amdgpu_device *adev)
1474 struct drm_device *dev = adev->ddev;
1475 struct drm_connector *connector;
1476 struct amdgpu_dm_connector *aconnector;
1477 const struct dc_link *dc_link;
1478 struct dc_interrupt_params int_params = {0};
1480 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
1481 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
1483 list_for_each_entry(connector,
1484 &dev->mode_config.connector_list, head) {
1486 aconnector = to_amdgpu_dm_connector(connector);
1487 dc_link = aconnector->dc_link;
1489 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd) {
1490 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
1491 int_params.irq_source = dc_link->irq_source_hpd;
1493 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1495 (void *) aconnector);
1498 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd_rx) {
1500 /* Also register for DP short pulse (hpd_rx). */
1501 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
1502 int_params.irq_source = dc_link->irq_source_hpd_rx;
1504 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1506 (void *) aconnector);
1511 /* Register IRQ sources and initialize IRQ callbacks */
1512 static int dce110_register_irq_handlers(struct amdgpu_device *adev)
1514 struct dc *dc = adev->dm.dc;
1515 struct common_irq_params *c_irq_params;
1516 struct dc_interrupt_params int_params = {0};
1519 unsigned client_id = AMDGPU_IRQ_CLIENTID_LEGACY;
1521 if (adev->asic_type == CHIP_VEGA10 ||
1522 adev->asic_type == CHIP_VEGA12 ||
1523 adev->asic_type == CHIP_VEGA20 ||
1524 adev->asic_type == CHIP_RAVEN)
1525 client_id = SOC15_IH_CLIENTID_DCE;
1527 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
1528 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
1531 * Actions of amdgpu_irq_add_id():
1532 * 1. Register a set() function with base driver.
1533 * Base driver will call set() function to enable/disable an
1534 * interrupt in DC hardware.
1535 * 2. Register amdgpu_dm_irq_handler().
1536 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
1537 * coming from DC hardware.
1538 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
1539 * for acknowledging and handling. */
1541 /* Use VBLANK interrupt */
1542 for (i = VISLANDS30_IV_SRCID_D1_VERTICAL_INTERRUPT0; i <= VISLANDS30_IV_SRCID_D6_VERTICAL_INTERRUPT0; i++) {
1543 r = amdgpu_irq_add_id(adev, client_id, i, &adev->crtc_irq);
1545 DRM_ERROR("Failed to add crtc irq id!\n");
1549 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1550 int_params.irq_source =
1551 dc_interrupt_to_irq_source(dc, i, 0);
1553 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
1555 c_irq_params->adev = adev;
1556 c_irq_params->irq_src = int_params.irq_source;
1558 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1559 dm_crtc_high_irq, c_irq_params);
1562 /* Use VUPDATE interrupt */
1563 for (i = VISLANDS30_IV_SRCID_D1_V_UPDATE_INT; i <= VISLANDS30_IV_SRCID_D6_V_UPDATE_INT; i += 2) {
1564 r = amdgpu_irq_add_id(adev, client_id, i, &adev->vupdate_irq);
1566 DRM_ERROR("Failed to add vupdate irq id!\n");
1570 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1571 int_params.irq_source =
1572 dc_interrupt_to_irq_source(dc, i, 0);
1574 c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1];
1576 c_irq_params->adev = adev;
1577 c_irq_params->irq_src = int_params.irq_source;
1579 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1580 dm_vupdate_high_irq, c_irq_params);
1583 /* Use GRPH_PFLIP interrupt */
1584 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
1585 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
1586 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq);
1588 DRM_ERROR("Failed to add page flip irq id!\n");
1592 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1593 int_params.irq_source =
1594 dc_interrupt_to_irq_source(dc, i, 0);
1596 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
1598 c_irq_params->adev = adev;
1599 c_irq_params->irq_src = int_params.irq_source;
1601 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1602 dm_pflip_high_irq, c_irq_params);
1607 r = amdgpu_irq_add_id(adev, client_id,
1608 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
1610 DRM_ERROR("Failed to add hpd irq id!\n");
1614 register_hpd_handlers(adev);
1619 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
1620 /* Register IRQ sources and initialize IRQ callbacks */
1621 static int dcn10_register_irq_handlers(struct amdgpu_device *adev)
1623 struct dc *dc = adev->dm.dc;
1624 struct common_irq_params *c_irq_params;
1625 struct dc_interrupt_params int_params = {0};
1629 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
1630 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
1633 * Actions of amdgpu_irq_add_id():
1634 * 1. Register a set() function with base driver.
1635 * Base driver will call set() function to enable/disable an
1636 * interrupt in DC hardware.
1637 * 2. Register amdgpu_dm_irq_handler().
1638 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
1639 * coming from DC hardware.
1640 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
1641 * for acknowledging and handling.
1644 /* Use VSTARTUP interrupt */
1645 for (i = DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP;
1646 i <= DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP + adev->mode_info.num_crtc - 1;
1648 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->crtc_irq);
1651 DRM_ERROR("Failed to add crtc irq id!\n");
1655 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1656 int_params.irq_source =
1657 dc_interrupt_to_irq_source(dc, i, 0);
1659 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
1661 c_irq_params->adev = adev;
1662 c_irq_params->irq_src = int_params.irq_source;
1664 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1665 dm_crtc_high_irq, c_irq_params);
1668 /* Use VUPDATE_NO_LOCK interrupt on DCN, which seems to correspond to
1669 * the regular VUPDATE interrupt on DCE. We want DC_IRQ_SOURCE_VUPDATEx
1670 * to trigger at end of each vblank, regardless of state of the lock,
1671 * matching DCE behaviour.
1673 for (i = DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT;
1674 i <= DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT + adev->mode_info.num_crtc - 1;
1676 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->vupdate_irq);
1679 DRM_ERROR("Failed to add vupdate irq id!\n");
1683 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1684 int_params.irq_source =
1685 dc_interrupt_to_irq_source(dc, i, 0);
1687 c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1];
1689 c_irq_params->adev = adev;
1690 c_irq_params->irq_src = int_params.irq_source;
1692 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1693 dm_vupdate_high_irq, c_irq_params);
1696 /* Use GRPH_PFLIP interrupt */
1697 for (i = DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT;
1698 i <= DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT + adev->mode_info.num_crtc - 1;
1700 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->pageflip_irq);
1702 DRM_ERROR("Failed to add page flip irq id!\n");
1706 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1707 int_params.irq_source =
1708 dc_interrupt_to_irq_source(dc, i, 0);
1710 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
1712 c_irq_params->adev = adev;
1713 c_irq_params->irq_src = int_params.irq_source;
1715 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1716 dm_pflip_high_irq, c_irq_params);
1721 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DC_HPD1_INT,
1724 DRM_ERROR("Failed to add hpd irq id!\n");
1728 register_hpd_handlers(adev);
1735 * Acquires the lock for the atomic state object and returns
1736 * the new atomic state.
1738 * This should only be called during atomic check.
1740 static int dm_atomic_get_state(struct drm_atomic_state *state,
1741 struct dm_atomic_state **dm_state)
1743 struct drm_device *dev = state->dev;
1744 struct amdgpu_device *adev = dev->dev_private;
1745 struct amdgpu_display_manager *dm = &adev->dm;
1746 struct drm_private_state *priv_state;
1751 priv_state = drm_atomic_get_private_obj_state(state, &dm->atomic_obj);
1752 if (IS_ERR(priv_state))
1753 return PTR_ERR(priv_state);
1755 *dm_state = to_dm_atomic_state(priv_state);
1760 struct dm_atomic_state *
1761 dm_atomic_get_new_state(struct drm_atomic_state *state)
1763 struct drm_device *dev = state->dev;
1764 struct amdgpu_device *adev = dev->dev_private;
1765 struct amdgpu_display_manager *dm = &adev->dm;
1766 struct drm_private_obj *obj;
1767 struct drm_private_state *new_obj_state;
1770 for_each_new_private_obj_in_state(state, obj, new_obj_state, i) {
1771 if (obj->funcs == dm->atomic_obj.funcs)
1772 return to_dm_atomic_state(new_obj_state);
1778 struct dm_atomic_state *
1779 dm_atomic_get_old_state(struct drm_atomic_state *state)
1781 struct drm_device *dev = state->dev;
1782 struct amdgpu_device *adev = dev->dev_private;
1783 struct amdgpu_display_manager *dm = &adev->dm;
1784 struct drm_private_obj *obj;
1785 struct drm_private_state *old_obj_state;
1788 for_each_old_private_obj_in_state(state, obj, old_obj_state, i) {
1789 if (obj->funcs == dm->atomic_obj.funcs)
1790 return to_dm_atomic_state(old_obj_state);
1796 static struct drm_private_state *
1797 dm_atomic_duplicate_state(struct drm_private_obj *obj)
1799 struct dm_atomic_state *old_state, *new_state;
1801 new_state = kzalloc(sizeof(*new_state), GFP_KERNEL);
1805 __drm_atomic_helper_private_obj_duplicate_state(obj, &new_state->base);
1807 old_state = to_dm_atomic_state(obj->state);
1809 if (old_state && old_state->context)
1810 new_state->context = dc_copy_state(old_state->context);
1812 if (!new_state->context) {
1817 return &new_state->base;
1820 static void dm_atomic_destroy_state(struct drm_private_obj *obj,
1821 struct drm_private_state *state)
1823 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
1825 if (dm_state && dm_state->context)
1826 dc_release_state(dm_state->context);
1831 static struct drm_private_state_funcs dm_atomic_state_funcs = {
1832 .atomic_duplicate_state = dm_atomic_duplicate_state,
1833 .atomic_destroy_state = dm_atomic_destroy_state,
1836 static int amdgpu_dm_mode_config_init(struct amdgpu_device *adev)
1838 struct dm_atomic_state *state;
1841 adev->mode_info.mode_config_initialized = true;
1843 adev->ddev->mode_config.funcs = (void *)&amdgpu_dm_mode_funcs;
1844 adev->ddev->mode_config.helper_private = &amdgpu_dm_mode_config_helperfuncs;
1846 adev->ddev->mode_config.max_width = 16384;
1847 adev->ddev->mode_config.max_height = 16384;
1849 adev->ddev->mode_config.preferred_depth = 24;
1850 adev->ddev->mode_config.prefer_shadow = 1;
1851 /* indicates support for immediate flip */
1852 adev->ddev->mode_config.async_page_flip = true;
1854 adev->ddev->mode_config.fb_base = adev->gmc.aper_base;
1856 state = kzalloc(sizeof(*state), GFP_KERNEL);
1860 state->context = dc_create_state(adev->dm.dc);
1861 if (!state->context) {
1866 dc_resource_state_copy_construct_current(adev->dm.dc, state->context);
1868 drm_atomic_private_obj_init(adev->ddev,
1869 &adev->dm.atomic_obj,
1871 &dm_atomic_state_funcs);
1873 r = amdgpu_display_modeset_create_props(adev);
1880 #define AMDGPU_DM_DEFAULT_MIN_BACKLIGHT 12
1881 #define AMDGPU_DM_DEFAULT_MAX_BACKLIGHT 255
1883 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
1884 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
1886 static void amdgpu_dm_update_backlight_caps(struct amdgpu_display_manager *dm)
1888 #if defined(CONFIG_ACPI)
1889 struct amdgpu_dm_backlight_caps caps;
1891 if (dm->backlight_caps.caps_valid)
1894 amdgpu_acpi_get_backlight_caps(dm->adev, &caps);
1895 if (caps.caps_valid) {
1896 dm->backlight_caps.min_input_signal = caps.min_input_signal;
1897 dm->backlight_caps.max_input_signal = caps.max_input_signal;
1898 dm->backlight_caps.caps_valid = true;
1900 dm->backlight_caps.min_input_signal =
1901 AMDGPU_DM_DEFAULT_MIN_BACKLIGHT;
1902 dm->backlight_caps.max_input_signal =
1903 AMDGPU_DM_DEFAULT_MAX_BACKLIGHT;
1906 dm->backlight_caps.min_input_signal = AMDGPU_DM_DEFAULT_MIN_BACKLIGHT;
1907 dm->backlight_caps.max_input_signal = AMDGPU_DM_DEFAULT_MAX_BACKLIGHT;
1911 static int amdgpu_dm_backlight_update_status(struct backlight_device *bd)
1913 struct amdgpu_display_manager *dm = bl_get_data(bd);
1914 struct amdgpu_dm_backlight_caps caps;
1915 uint32_t brightness = bd->props.brightness;
1917 amdgpu_dm_update_backlight_caps(dm);
1918 caps = dm->backlight_caps;
1920 * The brightness input is in the range 0-255
1921 * It needs to be rescaled to be between the
1922 * requested min and max input signal
1924 * It also needs to be scaled up by 0x101 to
1925 * match the DC interface which has a range of
1931 * (caps.max_input_signal - caps.min_input_signal)
1932 / AMDGPU_MAX_BL_LEVEL
1933 + caps.min_input_signal * 0x101;
1935 if (dc_link_set_backlight_level(dm->backlight_link,
1942 static int amdgpu_dm_backlight_get_brightness(struct backlight_device *bd)
1944 struct amdgpu_display_manager *dm = bl_get_data(bd);
1945 int ret = dc_link_get_backlight_level(dm->backlight_link);
1947 if (ret == DC_ERROR_UNEXPECTED)
1948 return bd->props.brightness;
1952 static const struct backlight_ops amdgpu_dm_backlight_ops = {
1953 .get_brightness = amdgpu_dm_backlight_get_brightness,
1954 .update_status = amdgpu_dm_backlight_update_status,
1958 amdgpu_dm_register_backlight_device(struct amdgpu_display_manager *dm)
1961 struct backlight_properties props = { 0 };
1963 amdgpu_dm_update_backlight_caps(dm);
1965 props.max_brightness = AMDGPU_MAX_BL_LEVEL;
1966 props.brightness = AMDGPU_MAX_BL_LEVEL;
1967 props.type = BACKLIGHT_RAW;
1969 snprintf(bl_name, sizeof(bl_name), "amdgpu_bl%d",
1970 dm->adev->ddev->primary->index);
1972 dm->backlight_dev = backlight_device_register(bl_name,
1973 dm->adev->ddev->dev,
1975 &amdgpu_dm_backlight_ops,
1978 if (IS_ERR(dm->backlight_dev))
1979 DRM_ERROR("DM: Backlight registration failed!\n");
1981 DRM_DEBUG_DRIVER("DM: Registered Backlight device: %s\n", bl_name);
1986 static int initialize_plane(struct amdgpu_display_manager *dm,
1987 struct amdgpu_mode_info *mode_info, int plane_id,
1988 enum drm_plane_type plane_type,
1989 const struct dc_plane_cap *plane_cap)
1991 struct drm_plane *plane;
1992 unsigned long possible_crtcs;
1995 plane = kzalloc(sizeof(struct drm_plane), GFP_KERNEL);
1997 DRM_ERROR("KMS: Failed to allocate plane\n");
2000 plane->type = plane_type;
2003 * HACK: IGT tests expect that the primary plane for a CRTC
2004 * can only have one possible CRTC. Only expose support for
2005 * any CRTC if they're not going to be used as a primary plane
2006 * for a CRTC - like overlay or underlay planes.
2008 possible_crtcs = 1 << plane_id;
2009 if (plane_id >= dm->dc->caps.max_streams)
2010 possible_crtcs = 0xff;
2012 ret = amdgpu_dm_plane_init(dm, plane, possible_crtcs, plane_cap);
2015 DRM_ERROR("KMS: Failed to initialize plane\n");
2021 mode_info->planes[plane_id] = plane;
2027 static void register_backlight_device(struct amdgpu_display_manager *dm,
2028 struct dc_link *link)
2030 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
2031 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
2033 if ((link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) &&
2034 link->type != dc_connection_none) {
2036 * Event if registration failed, we should continue with
2037 * DM initialization because not having a backlight control
2038 * is better then a black screen.
2040 amdgpu_dm_register_backlight_device(dm);
2042 if (dm->backlight_dev)
2043 dm->backlight_link = link;
2050 * In this architecture, the association
2051 * connector -> encoder -> crtc
2052 * id not really requried. The crtc and connector will hold the
2053 * display_index as an abstraction to use with DAL component
2055 * Returns 0 on success
2057 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev)
2059 struct amdgpu_display_manager *dm = &adev->dm;
2061 struct amdgpu_dm_connector *aconnector = NULL;
2062 struct amdgpu_encoder *aencoder = NULL;
2063 struct amdgpu_mode_info *mode_info = &adev->mode_info;
2065 int32_t primary_planes;
2066 enum dc_connection_type new_connection_type = dc_connection_none;
2067 const struct dc_plane_cap *plane;
2069 link_cnt = dm->dc->caps.max_links;
2070 if (amdgpu_dm_mode_config_init(dm->adev)) {
2071 DRM_ERROR("DM: Failed to initialize mode config\n");
2075 /* There is one primary plane per CRTC */
2076 primary_planes = dm->dc->caps.max_streams;
2077 ASSERT(primary_planes <= AMDGPU_MAX_PLANES);
2080 * Initialize primary planes, implicit planes for legacy IOCTLS.
2081 * Order is reversed to match iteration order in atomic check.
2083 for (i = (primary_planes - 1); i >= 0; i--) {
2084 plane = &dm->dc->caps.planes[i];
2086 if (initialize_plane(dm, mode_info, i,
2087 DRM_PLANE_TYPE_PRIMARY, plane)) {
2088 DRM_ERROR("KMS: Failed to initialize primary plane\n");
2094 * Initialize overlay planes, index starting after primary planes.
2095 * These planes have a higher DRM index than the primary planes since
2096 * they should be considered as having a higher z-order.
2097 * Order is reversed to match iteration order in atomic check.
2099 * Only support DCN for now, and only expose one so we don't encourage
2100 * userspace to use up all the pipes.
2102 for (i = 0; i < dm->dc->caps.max_planes; ++i) {
2103 struct dc_plane_cap *plane = &dm->dc->caps.planes[i];
2105 if (plane->type != DC_PLANE_TYPE_DCN_UNIVERSAL)
2108 if (!plane->blends_with_above || !plane->blends_with_below)
2111 if (!plane->pixel_format_support.argb8888)
2114 if (initialize_plane(dm, NULL, primary_planes + i,
2115 DRM_PLANE_TYPE_OVERLAY, plane)) {
2116 DRM_ERROR("KMS: Failed to initialize overlay plane\n");
2120 /* Only create one overlay plane. */
2124 for (i = 0; i < dm->dc->caps.max_streams; i++)
2125 if (amdgpu_dm_crtc_init(dm, mode_info->planes[i], i)) {
2126 DRM_ERROR("KMS: Failed to initialize crtc\n");
2130 dm->display_indexes_num = dm->dc->caps.max_streams;
2132 /* loops over all connectors on the board */
2133 for (i = 0; i < link_cnt; i++) {
2134 struct dc_link *link = NULL;
2136 if (i > AMDGPU_DM_MAX_DISPLAY_INDEX) {
2138 "KMS: Cannot support more than %d display indexes\n",
2139 AMDGPU_DM_MAX_DISPLAY_INDEX);
2143 aconnector = kzalloc(sizeof(*aconnector), GFP_KERNEL);
2147 aencoder = kzalloc(sizeof(*aencoder), GFP_KERNEL);
2151 if (amdgpu_dm_encoder_init(dm->ddev, aencoder, i)) {
2152 DRM_ERROR("KMS: Failed to initialize encoder\n");
2156 if (amdgpu_dm_connector_init(dm, aconnector, i, aencoder)) {
2157 DRM_ERROR("KMS: Failed to initialize connector\n");
2161 link = dc_get_link_at_index(dm->dc, i);
2163 if (!dc_link_detect_sink(link, &new_connection_type))
2164 DRM_ERROR("KMS: Failed to detect connector\n");
2166 if (aconnector->base.force && new_connection_type == dc_connection_none) {
2167 emulated_link_detect(link);
2168 amdgpu_dm_update_connector_after_detect(aconnector);
2170 } else if (dc_link_detect(link, DETECT_REASON_BOOT)) {
2171 amdgpu_dm_update_connector_after_detect(aconnector);
2172 register_backlight_device(dm, link);
2178 /* Software is initialized. Now we can register interrupt handlers. */
2179 switch (adev->asic_type) {
2189 case CHIP_POLARIS11:
2190 case CHIP_POLARIS10:
2191 case CHIP_POLARIS12:
2196 if (dce110_register_irq_handlers(dm->adev)) {
2197 DRM_ERROR("DM: Failed to initialize IRQ\n");
2201 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
2203 if (dcn10_register_irq_handlers(dm->adev)) {
2204 DRM_ERROR("DM: Failed to initialize IRQ\n");
2210 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type);
2214 if (adev->asic_type != CHIP_CARRIZO && adev->asic_type != CHIP_STONEY)
2215 dm->dc->debug.disable_stutter = amdgpu_pp_feature_mask & PP_STUTTER_MODE ? false : true;
2225 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm)
2227 drm_mode_config_cleanup(dm->ddev);
2228 drm_atomic_private_obj_fini(&dm->atomic_obj);
2232 /******************************************************************************
2233 * amdgpu_display_funcs functions
2234 *****************************************************************************/
2237 * dm_bandwidth_update - program display watermarks
2239 * @adev: amdgpu_device pointer
2241 * Calculate and program the display watermarks and line buffer allocation.
2243 static void dm_bandwidth_update(struct amdgpu_device *adev)
2245 /* TODO: implement later */
2248 static const struct amdgpu_display_funcs dm_display_funcs = {
2249 .bandwidth_update = dm_bandwidth_update, /* called unconditionally */
2250 .vblank_get_counter = dm_vblank_get_counter,/* called unconditionally */
2251 .backlight_set_level = NULL, /* never called for DC */
2252 .backlight_get_level = NULL, /* never called for DC */
2253 .hpd_sense = NULL,/* called unconditionally */
2254 .hpd_set_polarity = NULL, /* called unconditionally */
2255 .hpd_get_gpio_reg = NULL, /* VBIOS parsing. DAL does it. */
2256 .page_flip_get_scanoutpos =
2257 dm_crtc_get_scanoutpos,/* called unconditionally */
2258 .add_encoder = NULL, /* VBIOS parsing. DAL does it. */
2259 .add_connector = NULL, /* VBIOS parsing. DAL does it. */
2262 #if defined(CONFIG_DEBUG_KERNEL_DC)
2264 static ssize_t s3_debug_store(struct device *device,
2265 struct device_attribute *attr,
2271 struct pci_dev *pdev = to_pci_dev(device);
2272 struct drm_device *drm_dev = pci_get_drvdata(pdev);
2273 struct amdgpu_device *adev = drm_dev->dev_private;
2275 ret = kstrtoint(buf, 0, &s3_state);
2280 drm_kms_helper_hotplug_event(adev->ddev);
2285 return ret == 0 ? count : 0;
2288 DEVICE_ATTR_WO(s3_debug);
2292 static int dm_early_init(void *handle)
2294 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2296 switch (adev->asic_type) {
2299 adev->mode_info.num_crtc = 6;
2300 adev->mode_info.num_hpd = 6;
2301 adev->mode_info.num_dig = 6;
2304 adev->mode_info.num_crtc = 4;
2305 adev->mode_info.num_hpd = 6;
2306 adev->mode_info.num_dig = 7;
2310 adev->mode_info.num_crtc = 2;
2311 adev->mode_info.num_hpd = 6;
2312 adev->mode_info.num_dig = 6;
2316 adev->mode_info.num_crtc = 6;
2317 adev->mode_info.num_hpd = 6;
2318 adev->mode_info.num_dig = 7;
2321 adev->mode_info.num_crtc = 3;
2322 adev->mode_info.num_hpd = 6;
2323 adev->mode_info.num_dig = 9;
2326 adev->mode_info.num_crtc = 2;
2327 adev->mode_info.num_hpd = 6;
2328 adev->mode_info.num_dig = 9;
2330 case CHIP_POLARIS11:
2331 case CHIP_POLARIS12:
2332 adev->mode_info.num_crtc = 5;
2333 adev->mode_info.num_hpd = 5;
2334 adev->mode_info.num_dig = 5;
2336 case CHIP_POLARIS10:
2338 adev->mode_info.num_crtc = 6;
2339 adev->mode_info.num_hpd = 6;
2340 adev->mode_info.num_dig = 6;
2345 adev->mode_info.num_crtc = 6;
2346 adev->mode_info.num_hpd = 6;
2347 adev->mode_info.num_dig = 6;
2349 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
2351 adev->mode_info.num_crtc = 4;
2352 adev->mode_info.num_hpd = 4;
2353 adev->mode_info.num_dig = 4;
2357 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type);
2361 amdgpu_dm_set_irq_funcs(adev);
2363 if (adev->mode_info.funcs == NULL)
2364 adev->mode_info.funcs = &dm_display_funcs;
2367 * Note: Do NOT change adev->audio_endpt_rreg and
2368 * adev->audio_endpt_wreg because they are initialised in
2369 * amdgpu_device_init()
2371 #if defined(CONFIG_DEBUG_KERNEL_DC)
2374 &dev_attr_s3_debug);
2380 static bool modeset_required(struct drm_crtc_state *crtc_state,
2381 struct dc_stream_state *new_stream,
2382 struct dc_stream_state *old_stream)
2384 if (!drm_atomic_crtc_needs_modeset(crtc_state))
2387 if (!crtc_state->enable)
2390 return crtc_state->active;
2393 static bool modereset_required(struct drm_crtc_state *crtc_state)
2395 if (!drm_atomic_crtc_needs_modeset(crtc_state))
2398 return !crtc_state->enable || !crtc_state->active;
2401 static void amdgpu_dm_encoder_destroy(struct drm_encoder *encoder)
2403 drm_encoder_cleanup(encoder);
2407 static const struct drm_encoder_funcs amdgpu_dm_encoder_funcs = {
2408 .destroy = amdgpu_dm_encoder_destroy,
2412 static int fill_dc_scaling_info(const struct drm_plane_state *state,
2413 struct dc_scaling_info *scaling_info)
2415 int scale_w, scale_h;
2417 memset(scaling_info, 0, sizeof(*scaling_info));
2419 /* Source is fixed 16.16 but we ignore mantissa for now... */
2420 scaling_info->src_rect.x = state->src_x >> 16;
2421 scaling_info->src_rect.y = state->src_y >> 16;
2423 scaling_info->src_rect.width = state->src_w >> 16;
2424 if (scaling_info->src_rect.width == 0)
2427 scaling_info->src_rect.height = state->src_h >> 16;
2428 if (scaling_info->src_rect.height == 0)
2431 scaling_info->dst_rect.x = state->crtc_x;
2432 scaling_info->dst_rect.y = state->crtc_y;
2434 if (state->crtc_w == 0)
2437 scaling_info->dst_rect.width = state->crtc_w;
2439 if (state->crtc_h == 0)
2442 scaling_info->dst_rect.height = state->crtc_h;
2444 /* DRM doesn't specify clipping on destination output. */
2445 scaling_info->clip_rect = scaling_info->dst_rect;
2447 /* TODO: Validate scaling per-format with DC plane caps */
2448 scale_w = scaling_info->dst_rect.width * 1000 /
2449 scaling_info->src_rect.width;
2451 if (scale_w < 250 || scale_w > 16000)
2454 scale_h = scaling_info->dst_rect.height * 1000 /
2455 scaling_info->src_rect.height;
2457 if (scale_h < 250 || scale_h > 16000)
2461 * The "scaling_quality" can be ignored for now, quality = 0 has DC
2462 * assume reasonable defaults based on the format.
2468 static int get_fb_info(const struct amdgpu_framebuffer *amdgpu_fb,
2469 uint64_t *tiling_flags)
2471 struct amdgpu_bo *rbo = gem_to_amdgpu_bo(amdgpu_fb->base.obj[0]);
2472 int r = amdgpu_bo_reserve(rbo, false);
2475 /* Don't show error message when returning -ERESTARTSYS */
2476 if (r != -ERESTARTSYS)
2477 DRM_ERROR("Unable to reserve buffer: %d\n", r);
2482 amdgpu_bo_get_tiling_flags(rbo, tiling_flags);
2484 amdgpu_bo_unreserve(rbo);
2489 static inline uint64_t get_dcc_address(uint64_t address, uint64_t tiling_flags)
2491 uint32_t offset = AMDGPU_TILING_GET(tiling_flags, DCC_OFFSET_256B);
2493 return offset ? (address + offset * 256) : 0;
2497 fill_plane_dcc_attributes(struct amdgpu_device *adev,
2498 const struct amdgpu_framebuffer *afb,
2499 const enum surface_pixel_format format,
2500 const enum dc_rotation_angle rotation,
2501 const union plane_size *plane_size,
2502 const union dc_tiling_info *tiling_info,
2503 const uint64_t info,
2504 struct dc_plane_dcc_param *dcc,
2505 struct dc_plane_address *address)
2507 struct dc *dc = adev->dm.dc;
2508 struct dc_dcc_surface_param input;
2509 struct dc_surface_dcc_cap output;
2510 uint32_t offset = AMDGPU_TILING_GET(info, DCC_OFFSET_256B);
2511 uint32_t i64b = AMDGPU_TILING_GET(info, DCC_INDEPENDENT_64B) != 0;
2512 uint64_t dcc_address;
2514 memset(&input, 0, sizeof(input));
2515 memset(&output, 0, sizeof(output));
2520 if (format >= SURFACE_PIXEL_FORMAT_VIDEO_BEGIN)
2523 if (!dc->cap_funcs.get_dcc_compression_cap)
2526 input.format = format;
2527 input.surface_size.width = plane_size->grph.surface_size.width;
2528 input.surface_size.height = plane_size->grph.surface_size.height;
2529 input.swizzle_mode = tiling_info->gfx9.swizzle;
2531 if (rotation == ROTATION_ANGLE_0 || rotation == ROTATION_ANGLE_180)
2532 input.scan = SCAN_DIRECTION_HORIZONTAL;
2533 else if (rotation == ROTATION_ANGLE_90 || rotation == ROTATION_ANGLE_270)
2534 input.scan = SCAN_DIRECTION_VERTICAL;
2536 if (!dc->cap_funcs.get_dcc_compression_cap(dc, &input, &output))
2539 if (!output.capable)
2542 if (i64b == 0 && output.grph.rgb.independent_64b_blks != 0)
2546 dcc->grph.meta_pitch =
2547 AMDGPU_TILING_GET(info, DCC_PITCH_MAX) + 1;
2548 dcc->grph.independent_64b_blks = i64b;
2550 dcc_address = get_dcc_address(afb->address, info);
2551 address->grph.meta_addr.low_part = lower_32_bits(dcc_address);
2552 address->grph.meta_addr.high_part = upper_32_bits(dcc_address);
2558 fill_plane_buffer_attributes(struct amdgpu_device *adev,
2559 const struct amdgpu_framebuffer *afb,
2560 const enum surface_pixel_format format,
2561 const enum dc_rotation_angle rotation,
2562 const uint64_t tiling_flags,
2563 union dc_tiling_info *tiling_info,
2564 union plane_size *plane_size,
2565 struct dc_plane_dcc_param *dcc,
2566 struct dc_plane_address *address)
2568 const struct drm_framebuffer *fb = &afb->base;
2571 memset(tiling_info, 0, sizeof(*tiling_info));
2572 memset(plane_size, 0, sizeof(*plane_size));
2573 memset(dcc, 0, sizeof(*dcc));
2574 memset(address, 0, sizeof(*address));
2576 if (format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) {
2577 plane_size->grph.surface_size.x = 0;
2578 plane_size->grph.surface_size.y = 0;
2579 plane_size->grph.surface_size.width = fb->width;
2580 plane_size->grph.surface_size.height = fb->height;
2581 plane_size->grph.surface_pitch =
2582 fb->pitches[0] / fb->format->cpp[0];
2584 address->type = PLN_ADDR_TYPE_GRAPHICS;
2585 address->grph.addr.low_part = lower_32_bits(afb->address);
2586 address->grph.addr.high_part = upper_32_bits(afb->address);
2588 uint64_t chroma_addr = afb->address + fb->offsets[1];
2590 plane_size->video.luma_size.x = 0;
2591 plane_size->video.luma_size.y = 0;
2592 plane_size->video.luma_size.width = fb->width;
2593 plane_size->video.luma_size.height = fb->height;
2594 plane_size->video.luma_pitch =
2595 fb->pitches[0] / fb->format->cpp[0];
2597 plane_size->video.chroma_size.x = 0;
2598 plane_size->video.chroma_size.y = 0;
2599 /* TODO: set these based on surface format */
2600 plane_size->video.chroma_size.width = fb->width / 2;
2601 plane_size->video.chroma_size.height = fb->height / 2;
2603 plane_size->video.chroma_pitch =
2604 fb->pitches[1] / fb->format->cpp[1];
2606 address->type = PLN_ADDR_TYPE_VIDEO_PROGRESSIVE;
2607 address->video_progressive.luma_addr.low_part =
2608 lower_32_bits(afb->address);
2609 address->video_progressive.luma_addr.high_part =
2610 upper_32_bits(afb->address);
2611 address->video_progressive.chroma_addr.low_part =
2612 lower_32_bits(chroma_addr);
2613 address->video_progressive.chroma_addr.high_part =
2614 upper_32_bits(chroma_addr);
2617 /* Fill GFX8 params */
2618 if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == DC_ARRAY_2D_TILED_THIN1) {
2619 unsigned int bankw, bankh, mtaspect, tile_split, num_banks;
2621 bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
2622 bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
2623 mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
2624 tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT);
2625 num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
2627 /* XXX fix me for VI */
2628 tiling_info->gfx8.num_banks = num_banks;
2629 tiling_info->gfx8.array_mode =
2630 DC_ARRAY_2D_TILED_THIN1;
2631 tiling_info->gfx8.tile_split = tile_split;
2632 tiling_info->gfx8.bank_width = bankw;
2633 tiling_info->gfx8.bank_height = bankh;
2634 tiling_info->gfx8.tile_aspect = mtaspect;
2635 tiling_info->gfx8.tile_mode =
2636 DC_ADDR_SURF_MICRO_TILING_DISPLAY;
2637 } else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE)
2638 == DC_ARRAY_1D_TILED_THIN1) {
2639 tiling_info->gfx8.array_mode = DC_ARRAY_1D_TILED_THIN1;
2642 tiling_info->gfx8.pipe_config =
2643 AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
2645 if (adev->asic_type == CHIP_VEGA10 ||
2646 adev->asic_type == CHIP_VEGA12 ||
2647 adev->asic_type == CHIP_VEGA20 ||
2648 adev->asic_type == CHIP_RAVEN) {
2649 /* Fill GFX9 params */
2650 tiling_info->gfx9.num_pipes =
2651 adev->gfx.config.gb_addr_config_fields.num_pipes;
2652 tiling_info->gfx9.num_banks =
2653 adev->gfx.config.gb_addr_config_fields.num_banks;
2654 tiling_info->gfx9.pipe_interleave =
2655 adev->gfx.config.gb_addr_config_fields.pipe_interleave_size;
2656 tiling_info->gfx9.num_shader_engines =
2657 adev->gfx.config.gb_addr_config_fields.num_se;
2658 tiling_info->gfx9.max_compressed_frags =
2659 adev->gfx.config.gb_addr_config_fields.max_compress_frags;
2660 tiling_info->gfx9.num_rb_per_se =
2661 adev->gfx.config.gb_addr_config_fields.num_rb_per_se;
2662 tiling_info->gfx9.swizzle =
2663 AMDGPU_TILING_GET(tiling_flags, SWIZZLE_MODE);
2664 tiling_info->gfx9.shaderEnable = 1;
2666 ret = fill_plane_dcc_attributes(adev, afb, format, rotation,
2667 plane_size, tiling_info,
2668 tiling_flags, dcc, address);
2677 fill_blending_from_plane_state(const struct drm_plane_state *plane_state,
2678 bool *per_pixel_alpha, bool *global_alpha,
2679 int *global_alpha_value)
2681 *per_pixel_alpha = false;
2682 *global_alpha = false;
2683 *global_alpha_value = 0xff;
2685 if (plane_state->plane->type != DRM_PLANE_TYPE_OVERLAY)
2688 if (plane_state->pixel_blend_mode == DRM_MODE_BLEND_PREMULTI) {
2689 static const uint32_t alpha_formats[] = {
2690 DRM_FORMAT_ARGB8888,
2691 DRM_FORMAT_RGBA8888,
2692 DRM_FORMAT_ABGR8888,
2694 uint32_t format = plane_state->fb->format->format;
2697 for (i = 0; i < ARRAY_SIZE(alpha_formats); ++i) {
2698 if (format == alpha_formats[i]) {
2699 *per_pixel_alpha = true;
2705 if (plane_state->alpha < 0xffff) {
2706 *global_alpha = true;
2707 *global_alpha_value = plane_state->alpha >> 8;
2712 fill_plane_color_attributes(const struct drm_plane_state *plane_state,
2713 const enum surface_pixel_format format,
2714 enum dc_color_space *color_space)
2718 *color_space = COLOR_SPACE_SRGB;
2720 /* DRM color properties only affect non-RGB formats. */
2721 if (format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN)
2724 full_range = (plane_state->color_range == DRM_COLOR_YCBCR_FULL_RANGE);
2726 switch (plane_state->color_encoding) {
2727 case DRM_COLOR_YCBCR_BT601:
2729 *color_space = COLOR_SPACE_YCBCR601;
2731 *color_space = COLOR_SPACE_YCBCR601_LIMITED;
2734 case DRM_COLOR_YCBCR_BT709:
2736 *color_space = COLOR_SPACE_YCBCR709;
2738 *color_space = COLOR_SPACE_YCBCR709_LIMITED;
2741 case DRM_COLOR_YCBCR_BT2020:
2743 *color_space = COLOR_SPACE_2020_YCBCR;
2756 fill_dc_plane_info_and_addr(struct amdgpu_device *adev,
2757 const struct drm_plane_state *plane_state,
2758 const uint64_t tiling_flags,
2759 struct dc_plane_info *plane_info,
2760 struct dc_plane_address *address)
2762 const struct drm_framebuffer *fb = plane_state->fb;
2763 const struct amdgpu_framebuffer *afb =
2764 to_amdgpu_framebuffer(plane_state->fb);
2765 struct drm_format_name_buf format_name;
2768 memset(plane_info, 0, sizeof(*plane_info));
2770 switch (fb->format->format) {
2772 plane_info->format =
2773 SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS;
2775 case DRM_FORMAT_RGB565:
2776 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_RGB565;
2778 case DRM_FORMAT_XRGB8888:
2779 case DRM_FORMAT_ARGB8888:
2780 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888;
2782 case DRM_FORMAT_XRGB2101010:
2783 case DRM_FORMAT_ARGB2101010:
2784 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010;
2786 case DRM_FORMAT_XBGR2101010:
2787 case DRM_FORMAT_ABGR2101010:
2788 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010;
2790 case DRM_FORMAT_XBGR8888:
2791 case DRM_FORMAT_ABGR8888:
2792 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR8888;
2794 case DRM_FORMAT_NV21:
2795 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr;
2797 case DRM_FORMAT_NV12:
2798 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb;
2802 "Unsupported screen format %s\n",
2803 drm_get_format_name(fb->format->format, &format_name));
2807 switch (plane_state->rotation & DRM_MODE_ROTATE_MASK) {
2808 case DRM_MODE_ROTATE_0:
2809 plane_info->rotation = ROTATION_ANGLE_0;
2811 case DRM_MODE_ROTATE_90:
2812 plane_info->rotation = ROTATION_ANGLE_90;
2814 case DRM_MODE_ROTATE_180:
2815 plane_info->rotation = ROTATION_ANGLE_180;
2817 case DRM_MODE_ROTATE_270:
2818 plane_info->rotation = ROTATION_ANGLE_270;
2821 plane_info->rotation = ROTATION_ANGLE_0;
2825 plane_info->visible = true;
2826 plane_info->stereo_format = PLANE_STEREO_FORMAT_NONE;
2828 ret = fill_plane_color_attributes(plane_state, plane_info->format,
2829 &plane_info->color_space);
2833 ret = fill_plane_buffer_attributes(adev, afb, plane_info->format,
2834 plane_info->rotation, tiling_flags,
2835 &plane_info->tiling_info,
2836 &plane_info->plane_size,
2837 &plane_info->dcc, address);
2841 fill_blending_from_plane_state(
2842 plane_state, &plane_info->per_pixel_alpha,
2843 &plane_info->global_alpha, &plane_info->global_alpha_value);
2848 static int fill_dc_plane_attributes(struct amdgpu_device *adev,
2849 struct dc_plane_state *dc_plane_state,
2850 struct drm_plane_state *plane_state,
2851 struct drm_crtc_state *crtc_state)
2853 const struct amdgpu_framebuffer *amdgpu_fb =
2854 to_amdgpu_framebuffer(plane_state->fb);
2855 struct dc_scaling_info scaling_info;
2856 struct dc_plane_info plane_info;
2857 uint64_t tiling_flags;
2860 ret = fill_dc_scaling_info(plane_state, &scaling_info);
2864 dc_plane_state->src_rect = scaling_info.src_rect;
2865 dc_plane_state->dst_rect = scaling_info.dst_rect;
2866 dc_plane_state->clip_rect = scaling_info.clip_rect;
2867 dc_plane_state->scaling_quality = scaling_info.scaling_quality;
2869 ret = get_fb_info(amdgpu_fb, &tiling_flags);
2873 ret = fill_dc_plane_info_and_addr(adev, plane_state, tiling_flags,
2875 &dc_plane_state->address);
2879 dc_plane_state->format = plane_info.format;
2880 dc_plane_state->color_space = plane_info.color_space;
2881 dc_plane_state->format = plane_info.format;
2882 dc_plane_state->plane_size = plane_info.plane_size;
2883 dc_plane_state->rotation = plane_info.rotation;
2884 dc_plane_state->horizontal_mirror = plane_info.horizontal_mirror;
2885 dc_plane_state->stereo_format = plane_info.stereo_format;
2886 dc_plane_state->tiling_info = plane_info.tiling_info;
2887 dc_plane_state->visible = plane_info.visible;
2888 dc_plane_state->per_pixel_alpha = plane_info.per_pixel_alpha;
2889 dc_plane_state->global_alpha = plane_info.global_alpha;
2890 dc_plane_state->global_alpha_value = plane_info.global_alpha_value;
2891 dc_plane_state->dcc = plane_info.dcc;
2894 * Always set input transfer function, since plane state is refreshed
2897 ret = amdgpu_dm_set_degamma_lut(crtc_state, dc_plane_state);
2899 dc_transfer_func_release(dc_plane_state->in_transfer_func);
2900 dc_plane_state->in_transfer_func = NULL;
2906 static void update_stream_scaling_settings(const struct drm_display_mode *mode,
2907 const struct dm_connector_state *dm_state,
2908 struct dc_stream_state *stream)
2910 enum amdgpu_rmx_type rmx_type;
2912 struct rect src = { 0 }; /* viewport in composition space*/
2913 struct rect dst = { 0 }; /* stream addressable area */
2915 /* no mode. nothing to be done */
2919 /* Full screen scaling by default */
2920 src.width = mode->hdisplay;
2921 src.height = mode->vdisplay;
2922 dst.width = stream->timing.h_addressable;
2923 dst.height = stream->timing.v_addressable;
2926 rmx_type = dm_state->scaling;
2927 if (rmx_type == RMX_ASPECT || rmx_type == RMX_OFF) {
2928 if (src.width * dst.height <
2929 src.height * dst.width) {
2930 /* height needs less upscaling/more downscaling */
2931 dst.width = src.width *
2932 dst.height / src.height;
2934 /* width needs less upscaling/more downscaling */
2935 dst.height = src.height *
2936 dst.width / src.width;
2938 } else if (rmx_type == RMX_CENTER) {
2942 dst.x = (stream->timing.h_addressable - dst.width) / 2;
2943 dst.y = (stream->timing.v_addressable - dst.height) / 2;
2945 if (dm_state->underscan_enable) {
2946 dst.x += dm_state->underscan_hborder / 2;
2947 dst.y += dm_state->underscan_vborder / 2;
2948 dst.width -= dm_state->underscan_hborder;
2949 dst.height -= dm_state->underscan_vborder;
2956 DRM_DEBUG_DRIVER("Destination Rectangle x:%d y:%d width:%d height:%d\n",
2957 dst.x, dst.y, dst.width, dst.height);
2961 static enum dc_color_depth
2962 convert_color_depth_from_display_info(const struct drm_connector *connector)
2964 struct dm_connector_state *dm_conn_state =
2965 to_dm_connector_state(connector->state);
2966 uint32_t bpc = connector->display_info.bpc;
2968 /* TODO: Remove this when there's support for max_bpc in drm */
2969 if (dm_conn_state && bpc > dm_conn_state->max_bpc)
2970 /* Round down to nearest even number. */
2971 bpc = dm_conn_state->max_bpc - (dm_conn_state->max_bpc & 1);
2976 * Temporary Work around, DRM doesn't parse color depth for
2977 * EDID revision before 1.4
2978 * TODO: Fix edid parsing
2980 return COLOR_DEPTH_888;
2982 return COLOR_DEPTH_666;
2984 return COLOR_DEPTH_888;
2986 return COLOR_DEPTH_101010;
2988 return COLOR_DEPTH_121212;
2990 return COLOR_DEPTH_141414;
2992 return COLOR_DEPTH_161616;
2994 return COLOR_DEPTH_UNDEFINED;
2998 static enum dc_aspect_ratio
2999 get_aspect_ratio(const struct drm_display_mode *mode_in)
3001 /* 1-1 mapping, since both enums follow the HDMI spec. */
3002 return (enum dc_aspect_ratio) mode_in->picture_aspect_ratio;
3005 static enum dc_color_space
3006 get_output_color_space(const struct dc_crtc_timing *dc_crtc_timing)
3008 enum dc_color_space color_space = COLOR_SPACE_SRGB;
3010 switch (dc_crtc_timing->pixel_encoding) {
3011 case PIXEL_ENCODING_YCBCR422:
3012 case PIXEL_ENCODING_YCBCR444:
3013 case PIXEL_ENCODING_YCBCR420:
3016 * 27030khz is the separation point between HDTV and SDTV
3017 * according to HDMI spec, we use YCbCr709 and YCbCr601
3020 if (dc_crtc_timing->pix_clk_100hz > 270300) {
3021 if (dc_crtc_timing->flags.Y_ONLY)
3023 COLOR_SPACE_YCBCR709_LIMITED;
3025 color_space = COLOR_SPACE_YCBCR709;
3027 if (dc_crtc_timing->flags.Y_ONLY)
3029 COLOR_SPACE_YCBCR601_LIMITED;
3031 color_space = COLOR_SPACE_YCBCR601;
3036 case PIXEL_ENCODING_RGB:
3037 color_space = COLOR_SPACE_SRGB;
3048 static void reduce_mode_colour_depth(struct dc_crtc_timing *timing_out)
3050 if (timing_out->display_color_depth <= COLOR_DEPTH_888)
3053 timing_out->display_color_depth--;
3056 static void adjust_colour_depth_from_display_info(struct dc_crtc_timing *timing_out,
3057 const struct drm_display_info *info)
3060 if (timing_out->display_color_depth <= COLOR_DEPTH_888)
3063 normalized_clk = timing_out->pix_clk_100hz / 10;
3064 /* YCbCr 4:2:0 requires additional adjustment of 1/2 */
3065 if (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420)
3066 normalized_clk /= 2;
3067 /* Adjusting pix clock following on HDMI spec based on colour depth */
3068 switch (timing_out->display_color_depth) {
3069 case COLOR_DEPTH_101010:
3070 normalized_clk = (normalized_clk * 30) / 24;
3072 case COLOR_DEPTH_121212:
3073 normalized_clk = (normalized_clk * 36) / 24;
3075 case COLOR_DEPTH_161616:
3076 normalized_clk = (normalized_clk * 48) / 24;
3081 if (normalized_clk <= info->max_tmds_clock)
3083 reduce_mode_colour_depth(timing_out);
3085 } while (timing_out->display_color_depth > COLOR_DEPTH_888);
3090 fill_stream_properties_from_drm_display_mode(struct dc_stream_state *stream,
3091 const struct drm_display_mode *mode_in,
3092 const struct drm_connector *connector,
3093 const struct dc_stream_state *old_stream)
3095 struct dc_crtc_timing *timing_out = &stream->timing;
3096 const struct drm_display_info *info = &connector->display_info;
3098 memset(timing_out, 0, sizeof(struct dc_crtc_timing));
3100 timing_out->h_border_left = 0;
3101 timing_out->h_border_right = 0;
3102 timing_out->v_border_top = 0;
3103 timing_out->v_border_bottom = 0;
3104 /* TODO: un-hardcode */
3105 if (drm_mode_is_420_only(info, mode_in)
3106 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
3107 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
3108 else if ((connector->display_info.color_formats & DRM_COLOR_FORMAT_YCRCB444)
3109 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
3110 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR444;
3112 timing_out->pixel_encoding = PIXEL_ENCODING_RGB;
3114 timing_out->timing_3d_format = TIMING_3D_FORMAT_NONE;
3115 timing_out->display_color_depth = convert_color_depth_from_display_info(
3117 timing_out->scan_type = SCANNING_TYPE_NODATA;
3118 timing_out->hdmi_vic = 0;
3121 timing_out->vic = old_stream->timing.vic;
3122 timing_out->flags.HSYNC_POSITIVE_POLARITY = old_stream->timing.flags.HSYNC_POSITIVE_POLARITY;
3123 timing_out->flags.VSYNC_POSITIVE_POLARITY = old_stream->timing.flags.VSYNC_POSITIVE_POLARITY;
3125 timing_out->vic = drm_match_cea_mode(mode_in);
3126 if (mode_in->flags & DRM_MODE_FLAG_PHSYNC)
3127 timing_out->flags.HSYNC_POSITIVE_POLARITY = 1;
3128 if (mode_in->flags & DRM_MODE_FLAG_PVSYNC)
3129 timing_out->flags.VSYNC_POSITIVE_POLARITY = 1;
3132 timing_out->h_addressable = mode_in->crtc_hdisplay;
3133 timing_out->h_total = mode_in->crtc_htotal;
3134 timing_out->h_sync_width =
3135 mode_in->crtc_hsync_end - mode_in->crtc_hsync_start;
3136 timing_out->h_front_porch =
3137 mode_in->crtc_hsync_start - mode_in->crtc_hdisplay;
3138 timing_out->v_total = mode_in->crtc_vtotal;
3139 timing_out->v_addressable = mode_in->crtc_vdisplay;
3140 timing_out->v_front_porch =
3141 mode_in->crtc_vsync_start - mode_in->crtc_vdisplay;
3142 timing_out->v_sync_width =
3143 mode_in->crtc_vsync_end - mode_in->crtc_vsync_start;
3144 timing_out->pix_clk_100hz = mode_in->crtc_clock * 10;
3145 timing_out->aspect_ratio = get_aspect_ratio(mode_in);
3147 stream->output_color_space = get_output_color_space(timing_out);
3149 stream->out_transfer_func->type = TF_TYPE_PREDEFINED;
3150 stream->out_transfer_func->tf = TRANSFER_FUNCTION_SRGB;
3151 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
3152 adjust_colour_depth_from_display_info(timing_out, info);
3155 static void fill_audio_info(struct audio_info *audio_info,
3156 const struct drm_connector *drm_connector,
3157 const struct dc_sink *dc_sink)
3160 int cea_revision = 0;
3161 const struct dc_edid_caps *edid_caps = &dc_sink->edid_caps;
3163 audio_info->manufacture_id = edid_caps->manufacturer_id;
3164 audio_info->product_id = edid_caps->product_id;
3166 cea_revision = drm_connector->display_info.cea_rev;
3168 strscpy(audio_info->display_name,
3169 edid_caps->display_name,
3170 AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS);
3172 if (cea_revision >= 3) {
3173 audio_info->mode_count = edid_caps->audio_mode_count;
3175 for (i = 0; i < audio_info->mode_count; ++i) {
3176 audio_info->modes[i].format_code =
3177 (enum audio_format_code)
3178 (edid_caps->audio_modes[i].format_code);
3179 audio_info->modes[i].channel_count =
3180 edid_caps->audio_modes[i].channel_count;
3181 audio_info->modes[i].sample_rates.all =
3182 edid_caps->audio_modes[i].sample_rate;
3183 audio_info->modes[i].sample_size =
3184 edid_caps->audio_modes[i].sample_size;
3188 audio_info->flags.all = edid_caps->speaker_flags;
3190 /* TODO: We only check for the progressive mode, check for interlace mode too */
3191 if (drm_connector->latency_present[0]) {
3192 audio_info->video_latency = drm_connector->video_latency[0];
3193 audio_info->audio_latency = drm_connector->audio_latency[0];
3196 /* TODO: For DP, video and audio latency should be calculated from DPCD caps */
3201 copy_crtc_timing_for_drm_display_mode(const struct drm_display_mode *src_mode,
3202 struct drm_display_mode *dst_mode)
3204 dst_mode->crtc_hdisplay = src_mode->crtc_hdisplay;
3205 dst_mode->crtc_vdisplay = src_mode->crtc_vdisplay;
3206 dst_mode->crtc_clock = src_mode->crtc_clock;
3207 dst_mode->crtc_hblank_start = src_mode->crtc_hblank_start;
3208 dst_mode->crtc_hblank_end = src_mode->crtc_hblank_end;
3209 dst_mode->crtc_hsync_start = src_mode->crtc_hsync_start;
3210 dst_mode->crtc_hsync_end = src_mode->crtc_hsync_end;
3211 dst_mode->crtc_htotal = src_mode->crtc_htotal;
3212 dst_mode->crtc_hskew = src_mode->crtc_hskew;
3213 dst_mode->crtc_vblank_start = src_mode->crtc_vblank_start;
3214 dst_mode->crtc_vblank_end = src_mode->crtc_vblank_end;
3215 dst_mode->crtc_vsync_start = src_mode->crtc_vsync_start;
3216 dst_mode->crtc_vsync_end = src_mode->crtc_vsync_end;
3217 dst_mode->crtc_vtotal = src_mode->crtc_vtotal;
3221 decide_crtc_timing_for_drm_display_mode(struct drm_display_mode *drm_mode,
3222 const struct drm_display_mode *native_mode,
3225 if (scale_enabled) {
3226 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
3227 } else if (native_mode->clock == drm_mode->clock &&
3228 native_mode->htotal == drm_mode->htotal &&
3229 native_mode->vtotal == drm_mode->vtotal) {
3230 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
3232 /* no scaling nor amdgpu inserted, no need to patch */
3236 static struct dc_sink *
3237 create_fake_sink(struct amdgpu_dm_connector *aconnector)
3239 struct dc_sink_init_data sink_init_data = { 0 };
3240 struct dc_sink *sink = NULL;
3241 sink_init_data.link = aconnector->dc_link;
3242 sink_init_data.sink_signal = aconnector->dc_link->connector_signal;
3244 sink = dc_sink_create(&sink_init_data);
3246 DRM_ERROR("Failed to create sink!\n");
3249 sink->sink_signal = SIGNAL_TYPE_VIRTUAL;
3254 static void set_multisync_trigger_params(
3255 struct dc_stream_state *stream)
3257 if (stream->triggered_crtc_reset.enabled) {
3258 stream->triggered_crtc_reset.event = CRTC_EVENT_VSYNC_RISING;
3259 stream->triggered_crtc_reset.delay = TRIGGER_DELAY_NEXT_LINE;
3263 static void set_master_stream(struct dc_stream_state *stream_set[],
3266 int j, highest_rfr = 0, master_stream = 0;
3268 for (j = 0; j < stream_count; j++) {
3269 if (stream_set[j] && stream_set[j]->triggered_crtc_reset.enabled) {
3270 int refresh_rate = 0;
3272 refresh_rate = (stream_set[j]->timing.pix_clk_100hz*100)/
3273 (stream_set[j]->timing.h_total*stream_set[j]->timing.v_total);
3274 if (refresh_rate > highest_rfr) {
3275 highest_rfr = refresh_rate;
3280 for (j = 0; j < stream_count; j++) {
3282 stream_set[j]->triggered_crtc_reset.event_source = stream_set[master_stream];
3286 static void dm_enable_per_frame_crtc_master_sync(struct dc_state *context)
3290 if (context->stream_count < 2)
3292 for (i = 0; i < context->stream_count ; i++) {
3293 if (!context->streams[i])
3296 * TODO: add a function to read AMD VSDB bits and set
3297 * crtc_sync_master.multi_sync_enabled flag
3298 * For now it's set to false
3300 set_multisync_trigger_params(context->streams[i]);
3302 set_master_stream(context->streams, context->stream_count);
3305 static struct dc_stream_state *
3306 create_stream_for_sink(struct amdgpu_dm_connector *aconnector,
3307 const struct drm_display_mode *drm_mode,
3308 const struct dm_connector_state *dm_state,
3309 const struct dc_stream_state *old_stream)
3311 struct drm_display_mode *preferred_mode = NULL;
3312 struct drm_connector *drm_connector;
3313 struct dc_stream_state *stream = NULL;
3314 struct drm_display_mode mode = *drm_mode;
3315 bool native_mode_found = false;
3316 bool scale = dm_state ? (dm_state->scaling != RMX_OFF) : false;
3318 int preferred_refresh = 0;
3320 struct dc_sink *sink = NULL;
3321 if (aconnector == NULL) {
3322 DRM_ERROR("aconnector is NULL!\n");
3326 drm_connector = &aconnector->base;
3328 if (!aconnector->dc_sink) {
3329 sink = create_fake_sink(aconnector);
3333 sink = aconnector->dc_sink;
3334 dc_sink_retain(sink);
3337 stream = dc_create_stream_for_sink(sink);
3339 if (stream == NULL) {
3340 DRM_ERROR("Failed to create stream for sink!\n");
3344 stream->dm_stream_context = aconnector;
3346 list_for_each_entry(preferred_mode, &aconnector->base.modes, head) {
3347 /* Search for preferred mode */
3348 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED) {
3349 native_mode_found = true;
3353 if (!native_mode_found)
3354 preferred_mode = list_first_entry_or_null(
3355 &aconnector->base.modes,
3356 struct drm_display_mode,
3359 mode_refresh = drm_mode_vrefresh(&mode);
3361 if (preferred_mode == NULL) {
3363 * This may not be an error, the use case is when we have no
3364 * usermode calls to reset and set mode upon hotplug. In this
3365 * case, we call set mode ourselves to restore the previous mode
3366 * and the modelist may not be filled in in time.
3368 DRM_DEBUG_DRIVER("No preferred mode found\n");
3370 decide_crtc_timing_for_drm_display_mode(
3371 &mode, preferred_mode,
3372 dm_state ? (dm_state->scaling != RMX_OFF) : false);
3373 preferred_refresh = drm_mode_vrefresh(preferred_mode);
3377 drm_mode_set_crtcinfo(&mode, 0);
3380 * If scaling is enabled and refresh rate didn't change
3381 * we copy the vic and polarities of the old timings
3383 if (!scale || mode_refresh != preferred_refresh)
3384 fill_stream_properties_from_drm_display_mode(stream,
3385 &mode, &aconnector->base, NULL);
3387 fill_stream_properties_from_drm_display_mode(stream,
3388 &mode, &aconnector->base, old_stream);
3390 update_stream_scaling_settings(&mode, dm_state, stream);
3393 &stream->audio_info,
3397 update_stream_signal(stream, sink);
3400 dc_sink_release(sink);
3405 static void amdgpu_dm_crtc_destroy(struct drm_crtc *crtc)
3407 drm_crtc_cleanup(crtc);
3411 static void dm_crtc_destroy_state(struct drm_crtc *crtc,
3412 struct drm_crtc_state *state)
3414 struct dm_crtc_state *cur = to_dm_crtc_state(state);
3416 /* TODO Destroy dc_stream objects are stream object is flattened */
3418 dc_stream_release(cur->stream);
3421 __drm_atomic_helper_crtc_destroy_state(state);
3427 static void dm_crtc_reset_state(struct drm_crtc *crtc)
3429 struct dm_crtc_state *state;
3432 dm_crtc_destroy_state(crtc, crtc->state);
3434 state = kzalloc(sizeof(*state), GFP_KERNEL);
3435 if (WARN_ON(!state))
3438 crtc->state = &state->base;
3439 crtc->state->crtc = crtc;
3443 static struct drm_crtc_state *
3444 dm_crtc_duplicate_state(struct drm_crtc *crtc)
3446 struct dm_crtc_state *state, *cur;
3448 cur = to_dm_crtc_state(crtc->state);
3450 if (WARN_ON(!crtc->state))
3453 state = kzalloc(sizeof(*state), GFP_KERNEL);
3457 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
3460 state->stream = cur->stream;
3461 dc_stream_retain(state->stream);
3464 state->active_planes = cur->active_planes;
3465 state->interrupts_enabled = cur->interrupts_enabled;
3466 state->vrr_params = cur->vrr_params;
3467 state->vrr_infopacket = cur->vrr_infopacket;
3468 state->abm_level = cur->abm_level;
3469 state->vrr_supported = cur->vrr_supported;
3470 state->freesync_config = cur->freesync_config;
3471 state->crc_enabled = cur->crc_enabled;
3473 /* TODO Duplicate dc_stream after objects are stream object is flattened */
3475 return &state->base;
3478 static inline int dm_set_vupdate_irq(struct drm_crtc *crtc, bool enable)
3480 enum dc_irq_source irq_source;
3481 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
3482 struct amdgpu_device *adev = crtc->dev->dev_private;
3485 irq_source = IRQ_TYPE_VUPDATE + acrtc->otg_inst;
3487 rc = dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY;
3489 DRM_DEBUG_DRIVER("crtc %d - vupdate irq %sabling: r=%d\n",
3490 acrtc->crtc_id, enable ? "en" : "dis", rc);
3494 static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
3496 enum dc_irq_source irq_source;
3497 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
3498 struct amdgpu_device *adev = crtc->dev->dev_private;
3499 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
3503 /* vblank irq on -> Only need vupdate irq in vrr mode */
3504 if (amdgpu_dm_vrr_active(acrtc_state))
3505 rc = dm_set_vupdate_irq(crtc, true);
3507 /* vblank irq off -> vupdate irq off */
3508 rc = dm_set_vupdate_irq(crtc, false);
3514 irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
3515 return dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY;
3518 static int dm_enable_vblank(struct drm_crtc *crtc)
3520 return dm_set_vblank(crtc, true);
3523 static void dm_disable_vblank(struct drm_crtc *crtc)
3525 dm_set_vblank(crtc, false);
3528 /* Implemented only the options currently availible for the driver */
3529 static const struct drm_crtc_funcs amdgpu_dm_crtc_funcs = {
3530 .reset = dm_crtc_reset_state,
3531 .destroy = amdgpu_dm_crtc_destroy,
3532 .gamma_set = drm_atomic_helper_legacy_gamma_set,
3533 .set_config = drm_atomic_helper_set_config,
3534 .page_flip = drm_atomic_helper_page_flip,
3535 .atomic_duplicate_state = dm_crtc_duplicate_state,
3536 .atomic_destroy_state = dm_crtc_destroy_state,
3537 .set_crc_source = amdgpu_dm_crtc_set_crc_source,
3538 .verify_crc_source = amdgpu_dm_crtc_verify_crc_source,
3539 .enable_vblank = dm_enable_vblank,
3540 .disable_vblank = dm_disable_vblank,
3543 static enum drm_connector_status
3544 amdgpu_dm_connector_detect(struct drm_connector *connector, bool force)
3547 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
3551 * 1. This interface is NOT called in context of HPD irq.
3552 * 2. This interface *is called* in context of user-mode ioctl. Which
3553 * makes it a bad place for *any* MST-related activity.
3556 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED &&
3557 !aconnector->fake_enable)
3558 connected = (aconnector->dc_sink != NULL);
3560 connected = (aconnector->base.force == DRM_FORCE_ON);
3562 return (connected ? connector_status_connected :
3563 connector_status_disconnected);
3566 int amdgpu_dm_connector_atomic_set_property(struct drm_connector *connector,
3567 struct drm_connector_state *connector_state,
3568 struct drm_property *property,
3571 struct drm_device *dev = connector->dev;
3572 struct amdgpu_device *adev = dev->dev_private;
3573 struct dm_connector_state *dm_old_state =
3574 to_dm_connector_state(connector->state);
3575 struct dm_connector_state *dm_new_state =
3576 to_dm_connector_state(connector_state);
3580 if (property == dev->mode_config.scaling_mode_property) {
3581 enum amdgpu_rmx_type rmx_type;
3584 case DRM_MODE_SCALE_CENTER:
3585 rmx_type = RMX_CENTER;
3587 case DRM_MODE_SCALE_ASPECT:
3588 rmx_type = RMX_ASPECT;
3590 case DRM_MODE_SCALE_FULLSCREEN:
3591 rmx_type = RMX_FULL;
3593 case DRM_MODE_SCALE_NONE:
3599 if (dm_old_state->scaling == rmx_type)
3602 dm_new_state->scaling = rmx_type;
3604 } else if (property == adev->mode_info.underscan_hborder_property) {
3605 dm_new_state->underscan_hborder = val;
3607 } else if (property == adev->mode_info.underscan_vborder_property) {
3608 dm_new_state->underscan_vborder = val;
3610 } else if (property == adev->mode_info.underscan_property) {
3611 dm_new_state->underscan_enable = val;
3613 } else if (property == adev->mode_info.max_bpc_property) {
3614 dm_new_state->max_bpc = val;
3616 } else if (property == adev->mode_info.abm_level_property) {
3617 dm_new_state->abm_level = val;
3624 int amdgpu_dm_connector_atomic_get_property(struct drm_connector *connector,
3625 const struct drm_connector_state *state,
3626 struct drm_property *property,
3629 struct drm_device *dev = connector->dev;
3630 struct amdgpu_device *adev = dev->dev_private;
3631 struct dm_connector_state *dm_state =
3632 to_dm_connector_state(state);
3635 if (property == dev->mode_config.scaling_mode_property) {
3636 switch (dm_state->scaling) {
3638 *val = DRM_MODE_SCALE_CENTER;
3641 *val = DRM_MODE_SCALE_ASPECT;
3644 *val = DRM_MODE_SCALE_FULLSCREEN;
3648 *val = DRM_MODE_SCALE_NONE;
3652 } else if (property == adev->mode_info.underscan_hborder_property) {
3653 *val = dm_state->underscan_hborder;
3655 } else if (property == adev->mode_info.underscan_vborder_property) {
3656 *val = dm_state->underscan_vborder;
3658 } else if (property == adev->mode_info.underscan_property) {
3659 *val = dm_state->underscan_enable;
3661 } else if (property == adev->mode_info.max_bpc_property) {
3662 *val = dm_state->max_bpc;
3664 } else if (property == adev->mode_info.abm_level_property) {
3665 *val = dm_state->abm_level;
3672 static void amdgpu_dm_connector_destroy(struct drm_connector *connector)
3674 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
3675 const struct dc_link *link = aconnector->dc_link;
3676 struct amdgpu_device *adev = connector->dev->dev_private;
3677 struct amdgpu_display_manager *dm = &adev->dm;
3679 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
3680 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
3682 if ((link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) &&
3683 link->type != dc_connection_none &&
3684 dm->backlight_dev) {
3685 backlight_device_unregister(dm->backlight_dev);
3686 dm->backlight_dev = NULL;
3690 if (aconnector->dc_em_sink)
3691 dc_sink_release(aconnector->dc_em_sink);
3692 aconnector->dc_em_sink = NULL;
3693 if (aconnector->dc_sink)
3694 dc_sink_release(aconnector->dc_sink);
3695 aconnector->dc_sink = NULL;
3697 drm_dp_cec_unregister_connector(&aconnector->dm_dp_aux.aux);
3698 drm_connector_unregister(connector);
3699 drm_connector_cleanup(connector);
3703 void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector)
3705 struct dm_connector_state *state =
3706 to_dm_connector_state(connector->state);
3708 if (connector->state)
3709 __drm_atomic_helper_connector_destroy_state(connector->state);
3713 state = kzalloc(sizeof(*state), GFP_KERNEL);
3716 state->scaling = RMX_OFF;
3717 state->underscan_enable = false;
3718 state->underscan_hborder = 0;
3719 state->underscan_vborder = 0;
3722 __drm_atomic_helper_connector_reset(connector, &state->base);
3726 struct drm_connector_state *
3727 amdgpu_dm_connector_atomic_duplicate_state(struct drm_connector *connector)
3729 struct dm_connector_state *state =
3730 to_dm_connector_state(connector->state);
3732 struct dm_connector_state *new_state =
3733 kmemdup(state, sizeof(*state), GFP_KERNEL);
3738 __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);
3740 new_state->freesync_capable = state->freesync_capable;
3741 new_state->abm_level = state->abm_level;
3742 new_state->scaling = state->scaling;
3743 new_state->underscan_enable = state->underscan_enable;
3744 new_state->underscan_hborder = state->underscan_hborder;
3745 new_state->underscan_vborder = state->underscan_vborder;
3746 new_state->max_bpc = state->max_bpc;
3748 return &new_state->base;
3751 static const struct drm_connector_funcs amdgpu_dm_connector_funcs = {
3752 .reset = amdgpu_dm_connector_funcs_reset,
3753 .detect = amdgpu_dm_connector_detect,
3754 .fill_modes = drm_helper_probe_single_connector_modes,
3755 .destroy = amdgpu_dm_connector_destroy,
3756 .atomic_duplicate_state = amdgpu_dm_connector_atomic_duplicate_state,
3757 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
3758 .atomic_set_property = amdgpu_dm_connector_atomic_set_property,
3759 .atomic_get_property = amdgpu_dm_connector_atomic_get_property
3762 static int get_modes(struct drm_connector *connector)
3764 return amdgpu_dm_connector_get_modes(connector);
3767 static void create_eml_sink(struct amdgpu_dm_connector *aconnector)
3769 struct dc_sink_init_data init_params = {
3770 .link = aconnector->dc_link,
3771 .sink_signal = SIGNAL_TYPE_VIRTUAL
3775 if (!aconnector->base.edid_blob_ptr) {
3776 DRM_ERROR("No EDID firmware found on connector: %s ,forcing to OFF!\n",
3777 aconnector->base.name);
3779 aconnector->base.force = DRM_FORCE_OFF;
3780 aconnector->base.override_edid = false;
3784 edid = (struct edid *) aconnector->base.edid_blob_ptr->data;
3786 aconnector->edid = edid;
3788 aconnector->dc_em_sink = dc_link_add_remote_sink(
3789 aconnector->dc_link,
3791 (edid->extensions + 1) * EDID_LENGTH,
3794 if (aconnector->base.force == DRM_FORCE_ON) {
3795 aconnector->dc_sink = aconnector->dc_link->local_sink ?
3796 aconnector->dc_link->local_sink :
3797 aconnector->dc_em_sink;
3798 dc_sink_retain(aconnector->dc_sink);
3802 static void handle_edid_mgmt(struct amdgpu_dm_connector *aconnector)
3804 struct dc_link *link = (struct dc_link *)aconnector->dc_link;
3807 * In case of headless boot with force on for DP managed connector
3808 * Those settings have to be != 0 to get initial modeset
3810 if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT) {
3811 link->verified_link_cap.lane_count = LANE_COUNT_FOUR;
3812 link->verified_link_cap.link_rate = LINK_RATE_HIGH2;
3816 aconnector->base.override_edid = true;
3817 create_eml_sink(aconnector);
3820 enum drm_mode_status amdgpu_dm_connector_mode_valid(struct drm_connector *connector,
3821 struct drm_display_mode *mode)
3823 int result = MODE_ERROR;
3824 struct dc_sink *dc_sink;
3825 struct amdgpu_device *adev = connector->dev->dev_private;
3826 /* TODO: Unhardcode stream count */
3827 struct dc_stream_state *stream;
3828 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
3829 enum dc_status dc_result = DC_OK;
3831 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
3832 (mode->flags & DRM_MODE_FLAG_DBLSCAN))
3836 * Only run this the first time mode_valid is called to initilialize
3839 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED &&
3840 !aconnector->dc_em_sink)
3841 handle_edid_mgmt(aconnector);
3843 dc_sink = to_amdgpu_dm_connector(connector)->dc_sink;
3845 if (dc_sink == NULL) {
3846 DRM_ERROR("dc_sink is NULL!\n");
3850 stream = create_stream_for_sink(aconnector, mode, NULL, NULL);
3851 if (stream == NULL) {
3852 DRM_ERROR("Failed to create stream for sink!\n");
3856 dc_result = dc_validate_stream(adev->dm.dc, stream);
3858 if (dc_result == DC_OK)
3861 DRM_DEBUG_KMS("Mode %dx%d (clk %d) failed DC validation with error %d\n",
3867 dc_stream_release(stream);
3870 /* TODO: error handling*/
3874 static int fill_hdr_info_packet(const struct drm_connector_state *state,
3875 struct dc_info_packet *out)
3877 struct hdmi_drm_infoframe frame;
3878 unsigned char buf[30]; /* 26 + 4 */
3882 memset(out, 0, sizeof(*out));
3884 if (!state->hdr_output_metadata)
3887 ret = drm_hdmi_infoframe_set_hdr_metadata(&frame, state);
3891 len = hdmi_drm_infoframe_pack_only(&frame, buf, sizeof(buf));
3895 /* Static metadata is a fixed 26 bytes + 4 byte header. */
3899 /* Prepare the infopacket for DC. */
3900 switch (state->connector->connector_type) {
3901 case DRM_MODE_CONNECTOR_HDMIA:
3902 out->hb0 = 0x87; /* type */
3903 out->hb1 = 0x01; /* version */
3904 out->hb2 = 0x1A; /* length */
3905 out->sb[0] = buf[3]; /* checksum */
3909 case DRM_MODE_CONNECTOR_DisplayPort:
3910 case DRM_MODE_CONNECTOR_eDP:
3911 out->hb0 = 0x00; /* sdp id, zero */
3912 out->hb1 = 0x87; /* type */
3913 out->hb2 = 0x1D; /* payload len - 1 */
3914 out->hb3 = (0x13 << 2); /* sdp version */
3915 out->sb[0] = 0x01; /* version */
3916 out->sb[1] = 0x1A; /* length */
3924 memcpy(&out->sb[i], &buf[4], 26);
3927 print_hex_dump(KERN_DEBUG, "HDR SB:", DUMP_PREFIX_NONE, 16, 1, out->sb,
3928 sizeof(out->sb), false);
3934 is_hdr_metadata_different(const struct drm_connector_state *old_state,
3935 const struct drm_connector_state *new_state)
3937 struct drm_property_blob *old_blob = old_state->hdr_output_metadata;
3938 struct drm_property_blob *new_blob = new_state->hdr_output_metadata;
3940 if (old_blob != new_blob) {
3941 if (old_blob && new_blob &&
3942 old_blob->length == new_blob->length)
3943 return memcmp(old_blob->data, new_blob->data,
3953 amdgpu_dm_connector_atomic_check(struct drm_connector *conn,
3954 struct drm_connector_state *new_con_state)
3956 struct drm_atomic_state *state = new_con_state->state;
3957 struct drm_connector_state *old_con_state =
3958 drm_atomic_get_old_connector_state(state, conn);
3959 struct drm_crtc *crtc = new_con_state->crtc;
3960 struct drm_crtc_state *new_crtc_state;
3966 if (is_hdr_metadata_different(old_con_state, new_con_state)) {
3967 struct dc_info_packet hdr_infopacket;
3969 ret = fill_hdr_info_packet(new_con_state, &hdr_infopacket);
3973 new_crtc_state = drm_atomic_get_crtc_state(state, crtc);
3974 if (IS_ERR(new_crtc_state))
3975 return PTR_ERR(new_crtc_state);
3978 * DC considers the stream backends changed if the
3979 * static metadata changes. Forcing the modeset also
3980 * gives a simple way for userspace to switch from
3981 * 8bpc to 10bpc when setting the metadata to enter
3984 * Changing the static metadata after it's been
3985 * set is permissible, however. So only force a
3986 * modeset if we're entering or exiting HDR.
3988 new_crtc_state->mode_changed =
3989 !old_con_state->hdr_output_metadata ||
3990 !new_con_state->hdr_output_metadata;
3996 static const struct drm_connector_helper_funcs
3997 amdgpu_dm_connector_helper_funcs = {
3999 * If hotplugging a second bigger display in FB Con mode, bigger resolution
4000 * modes will be filtered by drm_mode_validate_size(), and those modes
4001 * are missing after user start lightdm. So we need to renew modes list.
4002 * in get_modes call back, not just return the modes count
4004 .get_modes = get_modes,
4005 .mode_valid = amdgpu_dm_connector_mode_valid,
4006 .atomic_check = amdgpu_dm_connector_atomic_check,
4009 static void dm_crtc_helper_disable(struct drm_crtc *crtc)
4013 static bool does_crtc_have_active_cursor(struct drm_crtc_state *new_crtc_state)
4015 struct drm_device *dev = new_crtc_state->crtc->dev;
4016 struct drm_plane *plane;
4018 drm_for_each_plane_mask(plane, dev, new_crtc_state->plane_mask) {
4019 if (plane->type == DRM_PLANE_TYPE_CURSOR)
4026 static int count_crtc_active_planes(struct drm_crtc_state *new_crtc_state)
4028 struct drm_atomic_state *state = new_crtc_state->state;
4029 struct drm_plane *plane;
4032 drm_for_each_plane_mask(plane, state->dev, new_crtc_state->plane_mask) {
4033 struct drm_plane_state *new_plane_state;
4035 /* Cursor planes are "fake". */
4036 if (plane->type == DRM_PLANE_TYPE_CURSOR)
4039 new_plane_state = drm_atomic_get_new_plane_state(state, plane);
4041 if (!new_plane_state) {
4043 * The plane is enable on the CRTC and hasn't changed
4044 * state. This means that it previously passed
4045 * validation and is therefore enabled.
4051 /* We need a framebuffer to be considered enabled. */
4052 num_active += (new_plane_state->fb != NULL);
4059 * Sets whether interrupts should be enabled on a specific CRTC.
4060 * We require that the stream be enabled and that there exist active
4061 * DC planes on the stream.
4064 dm_update_crtc_interrupt_state(struct drm_crtc *crtc,
4065 struct drm_crtc_state *new_crtc_state)
4067 struct dm_crtc_state *dm_new_crtc_state =
4068 to_dm_crtc_state(new_crtc_state);
4070 dm_new_crtc_state->active_planes = 0;
4071 dm_new_crtc_state->interrupts_enabled = false;
4073 if (!dm_new_crtc_state->stream)
4076 dm_new_crtc_state->active_planes =
4077 count_crtc_active_planes(new_crtc_state);
4079 dm_new_crtc_state->interrupts_enabled =
4080 dm_new_crtc_state->active_planes > 0;
4083 static int dm_crtc_helper_atomic_check(struct drm_crtc *crtc,
4084 struct drm_crtc_state *state)
4086 struct amdgpu_device *adev = crtc->dev->dev_private;
4087 struct dc *dc = adev->dm.dc;
4088 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(state);
4092 * Update interrupt state for the CRTC. This needs to happen whenever
4093 * the CRTC has changed or whenever any of its planes have changed.
4094 * Atomic check satisfies both of these requirements since the CRTC
4095 * is added to the state by DRM during drm_atomic_helper_check_planes.
4097 dm_update_crtc_interrupt_state(crtc, state);
4099 if (unlikely(!dm_crtc_state->stream &&
4100 modeset_required(state, NULL, dm_crtc_state->stream))) {
4105 /* In some use cases, like reset, no stream is attached */
4106 if (!dm_crtc_state->stream)
4110 * We want at least one hardware plane enabled to use
4111 * the stream with a cursor enabled.
4113 if (state->enable && state->active &&
4114 does_crtc_have_active_cursor(state) &&
4115 dm_crtc_state->active_planes == 0)
4118 if (dc_validate_stream(dc, dm_crtc_state->stream) == DC_OK)
4124 static bool dm_crtc_helper_mode_fixup(struct drm_crtc *crtc,
4125 const struct drm_display_mode *mode,
4126 struct drm_display_mode *adjusted_mode)
4131 static const struct drm_crtc_helper_funcs amdgpu_dm_crtc_helper_funcs = {
4132 .disable = dm_crtc_helper_disable,
4133 .atomic_check = dm_crtc_helper_atomic_check,
4134 .mode_fixup = dm_crtc_helper_mode_fixup
4137 static void dm_encoder_helper_disable(struct drm_encoder *encoder)
4142 static int dm_encoder_helper_atomic_check(struct drm_encoder *encoder,
4143 struct drm_crtc_state *crtc_state,
4144 struct drm_connector_state *conn_state)
4149 const struct drm_encoder_helper_funcs amdgpu_dm_encoder_helper_funcs = {
4150 .disable = dm_encoder_helper_disable,
4151 .atomic_check = dm_encoder_helper_atomic_check
4154 static void dm_drm_plane_reset(struct drm_plane *plane)
4156 struct dm_plane_state *amdgpu_state = NULL;
4159 plane->funcs->atomic_destroy_state(plane, plane->state);
4161 amdgpu_state = kzalloc(sizeof(*amdgpu_state), GFP_KERNEL);
4162 WARN_ON(amdgpu_state == NULL);
4165 __drm_atomic_helper_plane_reset(plane, &amdgpu_state->base);
4168 static struct drm_plane_state *
4169 dm_drm_plane_duplicate_state(struct drm_plane *plane)
4171 struct dm_plane_state *dm_plane_state, *old_dm_plane_state;
4173 old_dm_plane_state = to_dm_plane_state(plane->state);
4174 dm_plane_state = kzalloc(sizeof(*dm_plane_state), GFP_KERNEL);
4175 if (!dm_plane_state)
4178 __drm_atomic_helper_plane_duplicate_state(plane, &dm_plane_state->base);
4180 if (old_dm_plane_state->dc_state) {
4181 dm_plane_state->dc_state = old_dm_plane_state->dc_state;
4182 dc_plane_state_retain(dm_plane_state->dc_state);
4185 return &dm_plane_state->base;
4188 void dm_drm_plane_destroy_state(struct drm_plane *plane,
4189 struct drm_plane_state *state)
4191 struct dm_plane_state *dm_plane_state = to_dm_plane_state(state);
4193 if (dm_plane_state->dc_state)
4194 dc_plane_state_release(dm_plane_state->dc_state);
4196 drm_atomic_helper_plane_destroy_state(plane, state);
4199 static const struct drm_plane_funcs dm_plane_funcs = {
4200 .update_plane = drm_atomic_helper_update_plane,
4201 .disable_plane = drm_atomic_helper_disable_plane,
4202 .destroy = drm_primary_helper_destroy,
4203 .reset = dm_drm_plane_reset,
4204 .atomic_duplicate_state = dm_drm_plane_duplicate_state,
4205 .atomic_destroy_state = dm_drm_plane_destroy_state,
4208 static int dm_plane_helper_prepare_fb(struct drm_plane *plane,
4209 struct drm_plane_state *new_state)
4211 struct amdgpu_framebuffer *afb;
4212 struct drm_gem_object *obj;
4213 struct amdgpu_device *adev;
4214 struct amdgpu_bo *rbo;
4215 struct dm_plane_state *dm_plane_state_new, *dm_plane_state_old;
4216 uint64_t tiling_flags;
4220 dm_plane_state_old = to_dm_plane_state(plane->state);
4221 dm_plane_state_new = to_dm_plane_state(new_state);
4223 if (!new_state->fb) {
4224 DRM_DEBUG_DRIVER("No FB bound\n");
4228 afb = to_amdgpu_framebuffer(new_state->fb);
4229 obj = new_state->fb->obj[0];
4230 rbo = gem_to_amdgpu_bo(obj);
4231 adev = amdgpu_ttm_adev(rbo->tbo.bdev);
4232 r = amdgpu_bo_reserve(rbo, false);
4233 if (unlikely(r != 0))
4236 if (plane->type != DRM_PLANE_TYPE_CURSOR)
4237 domain = amdgpu_display_supported_domains(adev);
4239 domain = AMDGPU_GEM_DOMAIN_VRAM;
4241 r = amdgpu_bo_pin(rbo, domain);
4242 if (unlikely(r != 0)) {
4243 if (r != -ERESTARTSYS)
4244 DRM_ERROR("Failed to pin framebuffer with error %d\n", r);
4245 amdgpu_bo_unreserve(rbo);
4249 r = amdgpu_ttm_alloc_gart(&rbo->tbo);
4250 if (unlikely(r != 0)) {
4251 amdgpu_bo_unpin(rbo);
4252 amdgpu_bo_unreserve(rbo);
4253 DRM_ERROR("%p bind failed\n", rbo);
4257 amdgpu_bo_get_tiling_flags(rbo, &tiling_flags);
4259 amdgpu_bo_unreserve(rbo);
4261 afb->address = amdgpu_bo_gpu_offset(rbo);
4265 if (dm_plane_state_new->dc_state &&
4266 dm_plane_state_old->dc_state != dm_plane_state_new->dc_state) {
4267 struct dc_plane_state *plane_state = dm_plane_state_new->dc_state;
4269 fill_plane_buffer_attributes(
4270 adev, afb, plane_state->format, plane_state->rotation,
4271 tiling_flags, &plane_state->tiling_info,
4272 &plane_state->plane_size, &plane_state->dcc,
4273 &plane_state->address);
4279 static void dm_plane_helper_cleanup_fb(struct drm_plane *plane,
4280 struct drm_plane_state *old_state)
4282 struct amdgpu_bo *rbo;
4288 rbo = gem_to_amdgpu_bo(old_state->fb->obj[0]);
4289 r = amdgpu_bo_reserve(rbo, false);
4291 DRM_ERROR("failed to reserve rbo before unpin\n");
4295 amdgpu_bo_unpin(rbo);
4296 amdgpu_bo_unreserve(rbo);
4297 amdgpu_bo_unref(&rbo);
4300 static int dm_plane_atomic_check(struct drm_plane *plane,
4301 struct drm_plane_state *state)
4303 struct amdgpu_device *adev = plane->dev->dev_private;
4304 struct dc *dc = adev->dm.dc;
4305 struct dm_plane_state *dm_plane_state;
4306 struct dc_scaling_info scaling_info;
4309 dm_plane_state = to_dm_plane_state(state);
4311 if (!dm_plane_state->dc_state)
4314 ret = fill_dc_scaling_info(state, &scaling_info);
4318 if (dc_validate_plane(dc, dm_plane_state->dc_state) == DC_OK)
4324 static int dm_plane_atomic_async_check(struct drm_plane *plane,
4325 struct drm_plane_state *new_plane_state)
4327 struct drm_plane_state *old_plane_state =
4328 drm_atomic_get_old_plane_state(new_plane_state->state, plane);
4330 /* Only support async updates on cursor planes. */
4331 if (plane->type != DRM_PLANE_TYPE_CURSOR)
4335 * DRM calls prepare_fb and cleanup_fb on new_plane_state for
4336 * async commits so don't allow fb changes.
4338 if (old_plane_state->fb != new_plane_state->fb)
4344 static void dm_plane_atomic_async_update(struct drm_plane *plane,
4345 struct drm_plane_state *new_state)
4347 struct drm_plane_state *old_state =
4348 drm_atomic_get_old_plane_state(new_state->state, plane);
4350 if (plane->state->fb != new_state->fb)
4351 drm_atomic_set_fb_for_plane(plane->state, new_state->fb);
4353 plane->state->src_x = new_state->src_x;
4354 plane->state->src_y = new_state->src_y;
4355 plane->state->src_w = new_state->src_w;
4356 plane->state->src_h = new_state->src_h;
4357 plane->state->crtc_x = new_state->crtc_x;
4358 plane->state->crtc_y = new_state->crtc_y;
4359 plane->state->crtc_w = new_state->crtc_w;
4360 plane->state->crtc_h = new_state->crtc_h;
4362 handle_cursor_update(plane, old_state);
4365 static const struct drm_plane_helper_funcs dm_plane_helper_funcs = {
4366 .prepare_fb = dm_plane_helper_prepare_fb,
4367 .cleanup_fb = dm_plane_helper_cleanup_fb,
4368 .atomic_check = dm_plane_atomic_check,
4369 .atomic_async_check = dm_plane_atomic_async_check,
4370 .atomic_async_update = dm_plane_atomic_async_update
4374 * TODO: these are currently initialized to rgb formats only.
4375 * For future use cases we should either initialize them dynamically based on
4376 * plane capabilities, or initialize this array to all formats, so internal drm
4377 * check will succeed, and let DC implement proper check
4379 static const uint32_t rgb_formats[] = {
4380 DRM_FORMAT_XRGB8888,
4381 DRM_FORMAT_ARGB8888,
4382 DRM_FORMAT_RGBA8888,
4383 DRM_FORMAT_XRGB2101010,
4384 DRM_FORMAT_XBGR2101010,
4385 DRM_FORMAT_ARGB2101010,
4386 DRM_FORMAT_ABGR2101010,
4387 DRM_FORMAT_XBGR8888,
4388 DRM_FORMAT_ABGR8888,
4392 static const uint32_t overlay_formats[] = {
4393 DRM_FORMAT_XRGB8888,
4394 DRM_FORMAT_ARGB8888,
4395 DRM_FORMAT_RGBA8888,
4396 DRM_FORMAT_XBGR8888,
4397 DRM_FORMAT_ABGR8888,
4401 static const u32 cursor_formats[] = {
4405 static int get_plane_formats(const struct drm_plane *plane,
4406 const struct dc_plane_cap *plane_cap,
4407 uint32_t *formats, int max_formats)
4409 int i, num_formats = 0;
4412 * TODO: Query support for each group of formats directly from
4413 * DC plane caps. This will require adding more formats to the
4417 switch (plane->type) {
4418 case DRM_PLANE_TYPE_PRIMARY:
4419 for (i = 0; i < ARRAY_SIZE(rgb_formats); ++i) {
4420 if (num_formats >= max_formats)
4423 formats[num_formats++] = rgb_formats[i];
4426 if (plane_cap && plane_cap->pixel_format_support.nv12)
4427 formats[num_formats++] = DRM_FORMAT_NV12;
4430 case DRM_PLANE_TYPE_OVERLAY:
4431 for (i = 0; i < ARRAY_SIZE(overlay_formats); ++i) {
4432 if (num_formats >= max_formats)
4435 formats[num_formats++] = overlay_formats[i];
4439 case DRM_PLANE_TYPE_CURSOR:
4440 for (i = 0; i < ARRAY_SIZE(cursor_formats); ++i) {
4441 if (num_formats >= max_formats)
4444 formats[num_formats++] = cursor_formats[i];
4452 static int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
4453 struct drm_plane *plane,
4454 unsigned long possible_crtcs,
4455 const struct dc_plane_cap *plane_cap)
4457 uint32_t formats[32];
4461 num_formats = get_plane_formats(plane, plane_cap, formats,
4462 ARRAY_SIZE(formats));
4464 res = drm_universal_plane_init(dm->adev->ddev, plane, possible_crtcs,
4465 &dm_plane_funcs, formats, num_formats,
4466 NULL, plane->type, NULL);
4470 if (plane->type == DRM_PLANE_TYPE_OVERLAY &&
4471 plane_cap && plane_cap->per_pixel_alpha) {
4472 unsigned int blend_caps = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
4473 BIT(DRM_MODE_BLEND_PREMULTI);
4475 drm_plane_create_alpha_property(plane);
4476 drm_plane_create_blend_mode_property(plane, blend_caps);
4479 if (plane->type == DRM_PLANE_TYPE_PRIMARY &&
4480 plane_cap && plane_cap->pixel_format_support.nv12) {
4481 /* This only affects YUV formats. */
4482 drm_plane_create_color_properties(
4484 BIT(DRM_COLOR_YCBCR_BT601) |
4485 BIT(DRM_COLOR_YCBCR_BT709),
4486 BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) |
4487 BIT(DRM_COLOR_YCBCR_FULL_RANGE),
4488 DRM_COLOR_YCBCR_BT709, DRM_COLOR_YCBCR_LIMITED_RANGE);
4491 drm_plane_helper_add(plane, &dm_plane_helper_funcs);
4493 /* Create (reset) the plane state */
4494 if (plane->funcs->reset)
4495 plane->funcs->reset(plane);
4500 static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
4501 struct drm_plane *plane,
4502 uint32_t crtc_index)
4504 struct amdgpu_crtc *acrtc = NULL;
4505 struct drm_plane *cursor_plane;
4509 cursor_plane = kzalloc(sizeof(*cursor_plane), GFP_KERNEL);
4513 cursor_plane->type = DRM_PLANE_TYPE_CURSOR;
4514 res = amdgpu_dm_plane_init(dm, cursor_plane, 0, NULL);
4516 acrtc = kzalloc(sizeof(struct amdgpu_crtc), GFP_KERNEL);
4520 res = drm_crtc_init_with_planes(
4525 &amdgpu_dm_crtc_funcs, NULL);
4530 drm_crtc_helper_add(&acrtc->base, &amdgpu_dm_crtc_helper_funcs);
4532 /* Create (reset) the plane state */
4533 if (acrtc->base.funcs->reset)
4534 acrtc->base.funcs->reset(&acrtc->base);
4536 acrtc->max_cursor_width = dm->adev->dm.dc->caps.max_cursor_size;
4537 acrtc->max_cursor_height = dm->adev->dm.dc->caps.max_cursor_size;
4539 acrtc->crtc_id = crtc_index;
4540 acrtc->base.enabled = false;
4541 acrtc->otg_inst = -1;
4543 dm->adev->mode_info.crtcs[crtc_index] = acrtc;
4544 drm_crtc_enable_color_mgmt(&acrtc->base, MAX_COLOR_LUT_ENTRIES,
4545 true, MAX_COLOR_LUT_ENTRIES);
4546 drm_mode_crtc_set_gamma_size(&acrtc->base, MAX_COLOR_LEGACY_LUT_ENTRIES);
4552 kfree(cursor_plane);
4557 static int to_drm_connector_type(enum signal_type st)
4560 case SIGNAL_TYPE_HDMI_TYPE_A:
4561 return DRM_MODE_CONNECTOR_HDMIA;
4562 case SIGNAL_TYPE_EDP:
4563 return DRM_MODE_CONNECTOR_eDP;
4564 case SIGNAL_TYPE_LVDS:
4565 return DRM_MODE_CONNECTOR_LVDS;
4566 case SIGNAL_TYPE_RGB:
4567 return DRM_MODE_CONNECTOR_VGA;
4568 case SIGNAL_TYPE_DISPLAY_PORT:
4569 case SIGNAL_TYPE_DISPLAY_PORT_MST:
4570 return DRM_MODE_CONNECTOR_DisplayPort;
4571 case SIGNAL_TYPE_DVI_DUAL_LINK:
4572 case SIGNAL_TYPE_DVI_SINGLE_LINK:
4573 return DRM_MODE_CONNECTOR_DVID;
4574 case SIGNAL_TYPE_VIRTUAL:
4575 return DRM_MODE_CONNECTOR_VIRTUAL;
4578 return DRM_MODE_CONNECTOR_Unknown;
4582 static struct drm_encoder *amdgpu_dm_connector_to_encoder(struct drm_connector *connector)
4584 return drm_encoder_find(connector->dev, NULL, connector->encoder_ids[0]);
4587 static void amdgpu_dm_get_native_mode(struct drm_connector *connector)
4589 struct drm_encoder *encoder;
4590 struct amdgpu_encoder *amdgpu_encoder;
4592 encoder = amdgpu_dm_connector_to_encoder(connector);
4594 if (encoder == NULL)
4597 amdgpu_encoder = to_amdgpu_encoder(encoder);
4599 amdgpu_encoder->native_mode.clock = 0;
4601 if (!list_empty(&connector->probed_modes)) {
4602 struct drm_display_mode *preferred_mode = NULL;
4604 list_for_each_entry(preferred_mode,
4605 &connector->probed_modes,
4607 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED)
4608 amdgpu_encoder->native_mode = *preferred_mode;
4616 static struct drm_display_mode *
4617 amdgpu_dm_create_common_mode(struct drm_encoder *encoder,
4619 int hdisplay, int vdisplay)
4621 struct drm_device *dev = encoder->dev;
4622 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
4623 struct drm_display_mode *mode = NULL;
4624 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
4626 mode = drm_mode_duplicate(dev, native_mode);
4631 mode->hdisplay = hdisplay;
4632 mode->vdisplay = vdisplay;
4633 mode->type &= ~DRM_MODE_TYPE_PREFERRED;
4634 strscpy(mode->name, name, DRM_DISPLAY_MODE_LEN);
4640 static void amdgpu_dm_connector_add_common_modes(struct drm_encoder *encoder,
4641 struct drm_connector *connector)
4643 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
4644 struct drm_display_mode *mode = NULL;
4645 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
4646 struct amdgpu_dm_connector *amdgpu_dm_connector =
4647 to_amdgpu_dm_connector(connector);
4651 char name[DRM_DISPLAY_MODE_LEN];
4654 } common_modes[] = {
4655 { "640x480", 640, 480},
4656 { "800x600", 800, 600},
4657 { "1024x768", 1024, 768},
4658 { "1280x720", 1280, 720},
4659 { "1280x800", 1280, 800},
4660 {"1280x1024", 1280, 1024},
4661 { "1440x900", 1440, 900},
4662 {"1680x1050", 1680, 1050},
4663 {"1600x1200", 1600, 1200},
4664 {"1920x1080", 1920, 1080},
4665 {"1920x1200", 1920, 1200}
4668 n = ARRAY_SIZE(common_modes);
4670 for (i = 0; i < n; i++) {
4671 struct drm_display_mode *curmode = NULL;
4672 bool mode_existed = false;
4674 if (common_modes[i].w > native_mode->hdisplay ||
4675 common_modes[i].h > native_mode->vdisplay ||
4676 (common_modes[i].w == native_mode->hdisplay &&
4677 common_modes[i].h == native_mode->vdisplay))
4680 list_for_each_entry(curmode, &connector->probed_modes, head) {
4681 if (common_modes[i].w == curmode->hdisplay &&
4682 common_modes[i].h == curmode->vdisplay) {
4683 mode_existed = true;
4691 mode = amdgpu_dm_create_common_mode(encoder,
4692 common_modes[i].name, common_modes[i].w,
4694 drm_mode_probed_add(connector, mode);
4695 amdgpu_dm_connector->num_modes++;
4699 static void amdgpu_dm_connector_ddc_get_modes(struct drm_connector *connector,
4702 struct amdgpu_dm_connector *amdgpu_dm_connector =
4703 to_amdgpu_dm_connector(connector);
4706 /* empty probed_modes */
4707 INIT_LIST_HEAD(&connector->probed_modes);
4708 amdgpu_dm_connector->num_modes =
4709 drm_add_edid_modes(connector, edid);
4711 amdgpu_dm_get_native_mode(connector);
4713 amdgpu_dm_connector->num_modes = 0;
4717 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector)
4719 struct amdgpu_dm_connector *amdgpu_dm_connector =
4720 to_amdgpu_dm_connector(connector);
4721 struct drm_encoder *encoder;
4722 struct edid *edid = amdgpu_dm_connector->edid;
4724 encoder = amdgpu_dm_connector_to_encoder(connector);
4726 if (!edid || !drm_edid_is_valid(edid)) {
4727 amdgpu_dm_connector->num_modes =
4728 drm_add_modes_noedid(connector, 640, 480);
4730 amdgpu_dm_connector_ddc_get_modes(connector, edid);
4731 amdgpu_dm_connector_add_common_modes(encoder, connector);
4733 amdgpu_dm_fbc_init(connector);
4735 return amdgpu_dm_connector->num_modes;
4738 void amdgpu_dm_connector_init_helper(struct amdgpu_display_manager *dm,
4739 struct amdgpu_dm_connector *aconnector,
4741 struct dc_link *link,
4744 struct amdgpu_device *adev = dm->ddev->dev_private;
4746 aconnector->connector_id = link_index;
4747 aconnector->dc_link = link;
4748 aconnector->base.interlace_allowed = false;
4749 aconnector->base.doublescan_allowed = false;
4750 aconnector->base.stereo_allowed = false;
4751 aconnector->base.dpms = DRM_MODE_DPMS_OFF;
4752 aconnector->hpd.hpd = AMDGPU_HPD_NONE; /* not used */
4753 mutex_init(&aconnector->hpd_lock);
4756 * configure support HPD hot plug connector_>polled default value is 0
4757 * which means HPD hot plug not supported
4759 switch (connector_type) {
4760 case DRM_MODE_CONNECTOR_HDMIA:
4761 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
4762 aconnector->base.ycbcr_420_allowed =
4763 link->link_enc->features.hdmi_ycbcr420_supported ? true : false;
4765 case DRM_MODE_CONNECTOR_DisplayPort:
4766 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
4767 aconnector->base.ycbcr_420_allowed =
4768 link->link_enc->features.dp_ycbcr420_supported ? true : false;
4770 case DRM_MODE_CONNECTOR_DVID:
4771 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
4777 drm_object_attach_property(&aconnector->base.base,
4778 dm->ddev->mode_config.scaling_mode_property,
4779 DRM_MODE_SCALE_NONE);
4781 drm_object_attach_property(&aconnector->base.base,
4782 adev->mode_info.underscan_property,
4784 drm_object_attach_property(&aconnector->base.base,
4785 adev->mode_info.underscan_hborder_property,
4787 drm_object_attach_property(&aconnector->base.base,
4788 adev->mode_info.underscan_vborder_property,
4790 drm_object_attach_property(&aconnector->base.base,
4791 adev->mode_info.max_bpc_property,
4794 if (connector_type == DRM_MODE_CONNECTOR_eDP &&
4795 dc_is_dmcu_initialized(adev->dm.dc)) {
4796 drm_object_attach_property(&aconnector->base.base,
4797 adev->mode_info.abm_level_property, 0);
4800 if (connector_type == DRM_MODE_CONNECTOR_HDMIA ||
4801 connector_type == DRM_MODE_CONNECTOR_DisplayPort ||
4802 connector_type == DRM_MODE_CONNECTOR_eDP) {
4803 drm_object_attach_property(
4804 &aconnector->base.base,
4805 dm->ddev->mode_config.hdr_output_metadata_property, 0);
4807 drm_connector_attach_vrr_capable_property(
4812 static int amdgpu_dm_i2c_xfer(struct i2c_adapter *i2c_adap,
4813 struct i2c_msg *msgs, int num)
4815 struct amdgpu_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
4816 struct ddc_service *ddc_service = i2c->ddc_service;
4817 struct i2c_command cmd;
4821 cmd.payloads = kcalloc(num, sizeof(struct i2c_payload), GFP_KERNEL);
4826 cmd.number_of_payloads = num;
4827 cmd.engine = I2C_COMMAND_ENGINE_DEFAULT;
4830 for (i = 0; i < num; i++) {
4831 cmd.payloads[i].write = !(msgs[i].flags & I2C_M_RD);
4832 cmd.payloads[i].address = msgs[i].addr;
4833 cmd.payloads[i].length = msgs[i].len;
4834 cmd.payloads[i].data = msgs[i].buf;
4838 ddc_service->ctx->dc,
4839 ddc_service->ddc_pin->hw_info.ddc_channel,
4843 kfree(cmd.payloads);
4847 static u32 amdgpu_dm_i2c_func(struct i2c_adapter *adap)
4849 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
4852 static const struct i2c_algorithm amdgpu_dm_i2c_algo = {
4853 .master_xfer = amdgpu_dm_i2c_xfer,
4854 .functionality = amdgpu_dm_i2c_func,
4857 static struct amdgpu_i2c_adapter *
4858 create_i2c(struct ddc_service *ddc_service,
4862 struct amdgpu_device *adev = ddc_service->ctx->driver_context;
4863 struct amdgpu_i2c_adapter *i2c;
4865 i2c = kzalloc(sizeof(struct amdgpu_i2c_adapter), GFP_KERNEL);
4868 i2c->base.owner = THIS_MODULE;
4869 i2c->base.class = I2C_CLASS_DDC;
4870 i2c->base.dev.parent = &adev->pdev->dev;
4871 i2c->base.algo = &amdgpu_dm_i2c_algo;
4872 snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c hw bus %d", link_index);
4873 i2c_set_adapdata(&i2c->base, i2c);
4874 i2c->ddc_service = ddc_service;
4875 i2c->ddc_service->ddc_pin->hw_info.ddc_channel = link_index;
4882 * Note: this function assumes that dc_link_detect() was called for the
4883 * dc_link which will be represented by this aconnector.
4885 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
4886 struct amdgpu_dm_connector *aconnector,
4887 uint32_t link_index,
4888 struct amdgpu_encoder *aencoder)
4892 struct dc *dc = dm->dc;
4893 struct dc_link *link = dc_get_link_at_index(dc, link_index);
4894 struct amdgpu_i2c_adapter *i2c;
4896 link->priv = aconnector;
4898 DRM_DEBUG_DRIVER("%s()\n", __func__);
4900 i2c = create_i2c(link->ddc, link->link_index, &res);
4902 DRM_ERROR("Failed to create i2c adapter data\n");
4906 aconnector->i2c = i2c;
4907 res = i2c_add_adapter(&i2c->base);
4910 DRM_ERROR("Failed to register hw i2c %d\n", link->link_index);
4914 connector_type = to_drm_connector_type(link->connector_signal);
4916 res = drm_connector_init(
4919 &amdgpu_dm_connector_funcs,
4923 DRM_ERROR("connector_init failed\n");
4924 aconnector->connector_id = -1;
4928 drm_connector_helper_add(
4930 &amdgpu_dm_connector_helper_funcs);
4932 if (aconnector->base.funcs->reset)
4933 aconnector->base.funcs->reset(&aconnector->base);
4935 amdgpu_dm_connector_init_helper(
4942 drm_connector_attach_encoder(
4943 &aconnector->base, &aencoder->base);
4945 drm_connector_register(&aconnector->base);
4946 #if defined(CONFIG_DEBUG_FS)
4947 res = connector_debugfs_init(aconnector);
4949 DRM_ERROR("Failed to create debugfs for connector");
4952 aconnector->debugfs_dpcd_address = 0;
4953 aconnector->debugfs_dpcd_size = 0;
4956 if (connector_type == DRM_MODE_CONNECTOR_DisplayPort
4957 || connector_type == DRM_MODE_CONNECTOR_eDP)
4958 amdgpu_dm_initialize_dp_connector(dm, aconnector);
4963 aconnector->i2c = NULL;
4968 int amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device *adev)
4970 switch (adev->mode_info.num_crtc) {
4987 static int amdgpu_dm_encoder_init(struct drm_device *dev,
4988 struct amdgpu_encoder *aencoder,
4989 uint32_t link_index)
4991 struct amdgpu_device *adev = dev->dev_private;
4993 int res = drm_encoder_init(dev,
4995 &amdgpu_dm_encoder_funcs,
4996 DRM_MODE_ENCODER_TMDS,
4999 aencoder->base.possible_crtcs = amdgpu_dm_get_encoder_crtc_mask(adev);
5002 aencoder->encoder_id = link_index;
5004 aencoder->encoder_id = -1;
5006 drm_encoder_helper_add(&aencoder->base, &amdgpu_dm_encoder_helper_funcs);
5011 static void manage_dm_interrupts(struct amdgpu_device *adev,
5012 struct amdgpu_crtc *acrtc,
5016 * this is not correct translation but will work as soon as VBLANK
5017 * constant is the same as PFLIP
5020 amdgpu_display_crtc_idx_to_irq_type(
5025 drm_crtc_vblank_on(&acrtc->base);
5028 &adev->pageflip_irq,
5034 &adev->pageflip_irq,
5036 drm_crtc_vblank_off(&acrtc->base);
5041 is_scaling_state_different(const struct dm_connector_state *dm_state,
5042 const struct dm_connector_state *old_dm_state)
5044 if (dm_state->scaling != old_dm_state->scaling)
5046 if (!dm_state->underscan_enable && old_dm_state->underscan_enable) {
5047 if (old_dm_state->underscan_hborder != 0 && old_dm_state->underscan_vborder != 0)
5049 } else if (dm_state->underscan_enable && !old_dm_state->underscan_enable) {
5050 if (dm_state->underscan_hborder != 0 && dm_state->underscan_vborder != 0)
5052 } else if (dm_state->underscan_hborder != old_dm_state->underscan_hborder ||
5053 dm_state->underscan_vborder != old_dm_state->underscan_vborder)
5058 static void remove_stream(struct amdgpu_device *adev,
5059 struct amdgpu_crtc *acrtc,
5060 struct dc_stream_state *stream)
5062 /* this is the update mode case */
5064 acrtc->otg_inst = -1;
5065 acrtc->enabled = false;
5068 static int get_cursor_position(struct drm_plane *plane, struct drm_crtc *crtc,
5069 struct dc_cursor_position *position)
5071 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
5073 int xorigin = 0, yorigin = 0;
5075 if (!crtc || !plane->state->fb) {
5076 position->enable = false;
5082 if ((plane->state->crtc_w > amdgpu_crtc->max_cursor_width) ||
5083 (plane->state->crtc_h > amdgpu_crtc->max_cursor_height)) {
5084 DRM_ERROR("%s: bad cursor width or height %d x %d\n",
5086 plane->state->crtc_w,
5087 plane->state->crtc_h);
5091 x = plane->state->crtc_x;
5092 y = plane->state->crtc_y;
5094 if (crtc->primary->state) {
5095 /* avivo cursor are offset into the total surface */
5096 x += crtc->primary->state->src_x >> 16;
5097 y += crtc->primary->state->src_y >> 16;
5101 xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
5105 yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
5108 position->enable = true;
5111 position->x_hotspot = xorigin;
5112 position->y_hotspot = yorigin;
5117 static void handle_cursor_update(struct drm_plane *plane,
5118 struct drm_plane_state *old_plane_state)
5120 struct amdgpu_device *adev = plane->dev->dev_private;
5121 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(plane->state->fb);
5122 struct drm_crtc *crtc = afb ? plane->state->crtc : old_plane_state->crtc;
5123 struct dm_crtc_state *crtc_state = crtc ? to_dm_crtc_state(crtc->state) : NULL;
5124 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
5125 uint64_t address = afb ? afb->address : 0;
5126 struct dc_cursor_position position;
5127 struct dc_cursor_attributes attributes;
5130 if (!plane->state->fb && !old_plane_state->fb)
5133 DRM_DEBUG_DRIVER("%s: crtc_id=%d with size %d to %d\n",
5135 amdgpu_crtc->crtc_id,
5136 plane->state->crtc_w,
5137 plane->state->crtc_h);
5139 ret = get_cursor_position(plane, crtc, &position);
5143 if (!position.enable) {
5144 /* turn off cursor */
5145 if (crtc_state && crtc_state->stream) {
5146 mutex_lock(&adev->dm.dc_lock);
5147 dc_stream_set_cursor_position(crtc_state->stream,
5149 mutex_unlock(&adev->dm.dc_lock);
5154 amdgpu_crtc->cursor_width = plane->state->crtc_w;
5155 amdgpu_crtc->cursor_height = plane->state->crtc_h;
5157 memset(&attributes, 0, sizeof(attributes));
5158 attributes.address.high_part = upper_32_bits(address);
5159 attributes.address.low_part = lower_32_bits(address);
5160 attributes.width = plane->state->crtc_w;
5161 attributes.height = plane->state->crtc_h;
5162 attributes.color_format = CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA;
5163 attributes.rotation_angle = 0;
5164 attributes.attribute_flags.value = 0;
5166 attributes.pitch = attributes.width;
5168 if (crtc_state->stream) {
5169 mutex_lock(&adev->dm.dc_lock);
5170 if (!dc_stream_set_cursor_attributes(crtc_state->stream,
5172 DRM_ERROR("DC failed to set cursor attributes\n");
5174 if (!dc_stream_set_cursor_position(crtc_state->stream,
5176 DRM_ERROR("DC failed to set cursor position\n");
5177 mutex_unlock(&adev->dm.dc_lock);
5181 static void prepare_flip_isr(struct amdgpu_crtc *acrtc)
5184 assert_spin_locked(&acrtc->base.dev->event_lock);
5185 WARN_ON(acrtc->event);
5187 acrtc->event = acrtc->base.state->event;
5189 /* Set the flip status */
5190 acrtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
5192 /* Mark this event as consumed */
5193 acrtc->base.state->event = NULL;
5195 DRM_DEBUG_DRIVER("crtc:%d, pflip_stat:AMDGPU_FLIP_SUBMITTED\n",
5199 static void update_freesync_state_on_stream(
5200 struct amdgpu_display_manager *dm,
5201 struct dm_crtc_state *new_crtc_state,
5202 struct dc_stream_state *new_stream,
5203 struct dc_plane_state *surface,
5204 u32 flip_timestamp_in_us)
5206 struct mod_vrr_params vrr_params;
5207 struct dc_info_packet vrr_infopacket = {0};
5208 struct amdgpu_device *adev = dm->adev;
5209 unsigned long flags;
5215 * TODO: Determine why min/max totals and vrefresh can be 0 here.
5216 * For now it's sufficient to just guard against these conditions.
5219 if (!new_stream->timing.h_total || !new_stream->timing.v_total)
5222 spin_lock_irqsave(&adev->ddev->event_lock, flags);
5223 vrr_params = new_crtc_state->vrr_params;
5226 mod_freesync_handle_preflip(
5227 dm->freesync_module,
5230 flip_timestamp_in_us,
5233 if (adev->family < AMDGPU_FAMILY_AI &&
5234 amdgpu_dm_vrr_active(new_crtc_state)) {
5235 mod_freesync_handle_v_update(dm->freesync_module,
5236 new_stream, &vrr_params);
5240 mod_freesync_build_vrr_infopacket(
5241 dm->freesync_module,
5245 TRANSFER_FUNC_UNKNOWN,
5248 new_crtc_state->freesync_timing_changed |=
5249 (memcmp(&new_crtc_state->vrr_params.adjust,
5251 sizeof(vrr_params.adjust)) != 0);
5253 new_crtc_state->freesync_vrr_info_changed |=
5254 (memcmp(&new_crtc_state->vrr_infopacket,
5256 sizeof(vrr_infopacket)) != 0);
5258 new_crtc_state->vrr_params = vrr_params;
5259 new_crtc_state->vrr_infopacket = vrr_infopacket;
5261 new_stream->adjust = new_crtc_state->vrr_params.adjust;
5262 new_stream->vrr_infopacket = vrr_infopacket;
5264 if (new_crtc_state->freesync_vrr_info_changed)
5265 DRM_DEBUG_KMS("VRR packet update: crtc=%u enabled=%d state=%d",
5266 new_crtc_state->base.crtc->base.id,
5267 (int)new_crtc_state->base.vrr_enabled,
5268 (int)vrr_params.state);
5270 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
5273 static void pre_update_freesync_state_on_stream(
5274 struct amdgpu_display_manager *dm,
5275 struct dm_crtc_state *new_crtc_state)
5277 struct dc_stream_state *new_stream = new_crtc_state->stream;
5278 struct mod_vrr_params vrr_params;
5279 struct mod_freesync_config config = new_crtc_state->freesync_config;
5280 struct amdgpu_device *adev = dm->adev;
5281 unsigned long flags;
5287 * TODO: Determine why min/max totals and vrefresh can be 0 here.
5288 * For now it's sufficient to just guard against these conditions.
5290 if (!new_stream->timing.h_total || !new_stream->timing.v_total)
5293 spin_lock_irqsave(&adev->ddev->event_lock, flags);
5294 vrr_params = new_crtc_state->vrr_params;
5296 if (new_crtc_state->vrr_supported &&
5297 config.min_refresh_in_uhz &&
5298 config.max_refresh_in_uhz) {
5299 config.state = new_crtc_state->base.vrr_enabled ?
5300 VRR_STATE_ACTIVE_VARIABLE :
5303 config.state = VRR_STATE_UNSUPPORTED;
5306 mod_freesync_build_vrr_params(dm->freesync_module,
5308 &config, &vrr_params);
5310 new_crtc_state->freesync_timing_changed |=
5311 (memcmp(&new_crtc_state->vrr_params.adjust,
5313 sizeof(vrr_params.adjust)) != 0);
5315 new_crtc_state->vrr_params = vrr_params;
5316 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
5319 static void amdgpu_dm_handle_vrr_transition(struct dm_crtc_state *old_state,
5320 struct dm_crtc_state *new_state)
5322 bool old_vrr_active = amdgpu_dm_vrr_active(old_state);
5323 bool new_vrr_active = amdgpu_dm_vrr_active(new_state);
5325 if (!old_vrr_active && new_vrr_active) {
5326 /* Transition VRR inactive -> active:
5327 * While VRR is active, we must not disable vblank irq, as a
5328 * reenable after disable would compute bogus vblank/pflip
5329 * timestamps if it likely happened inside display front-porch.
5331 * We also need vupdate irq for the actual core vblank handling
5334 dm_set_vupdate_irq(new_state->base.crtc, true);
5335 drm_crtc_vblank_get(new_state->base.crtc);
5336 DRM_DEBUG_DRIVER("%s: crtc=%u VRR off->on: Get vblank ref\n",
5337 __func__, new_state->base.crtc->base.id);
5338 } else if (old_vrr_active && !new_vrr_active) {
5339 /* Transition VRR active -> inactive:
5340 * Allow vblank irq disable again for fixed refresh rate.
5342 dm_set_vupdate_irq(new_state->base.crtc, false);
5343 drm_crtc_vblank_put(new_state->base.crtc);
5344 DRM_DEBUG_DRIVER("%s: crtc=%u VRR on->off: Drop vblank ref\n",
5345 __func__, new_state->base.crtc->base.id);
5349 static void amdgpu_dm_commit_cursors(struct drm_atomic_state *state)
5351 struct drm_plane *plane;
5352 struct drm_plane_state *old_plane_state, *new_plane_state;
5356 * TODO: Make this per-stream so we don't issue redundant updates for
5357 * commits with multiple streams.
5359 for_each_oldnew_plane_in_state(state, plane, old_plane_state,
5361 if (plane->type == DRM_PLANE_TYPE_CURSOR)
5362 handle_cursor_update(plane, old_plane_state);
5365 static void amdgpu_dm_commit_planes(struct drm_atomic_state *state,
5366 struct dc_state *dc_state,
5367 struct drm_device *dev,
5368 struct amdgpu_display_manager *dm,
5369 struct drm_crtc *pcrtc,
5370 bool wait_for_vblank)
5373 uint64_t timestamp_ns;
5374 struct drm_plane *plane;
5375 struct drm_plane_state *old_plane_state, *new_plane_state;
5376 struct amdgpu_crtc *acrtc_attach = to_amdgpu_crtc(pcrtc);
5377 struct drm_crtc_state *new_pcrtc_state =
5378 drm_atomic_get_new_crtc_state(state, pcrtc);
5379 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(new_pcrtc_state);
5380 struct dm_crtc_state *dm_old_crtc_state =
5381 to_dm_crtc_state(drm_atomic_get_old_crtc_state(state, pcrtc));
5382 int planes_count = 0, vpos, hpos;
5384 unsigned long flags;
5385 struct amdgpu_bo *abo;
5386 uint64_t tiling_flags;
5387 uint32_t target_vblank, last_flip_vblank;
5388 bool vrr_active = amdgpu_dm_vrr_active(acrtc_state);
5389 bool pflip_present = false;
5391 struct dc_surface_update surface_updates[MAX_SURFACES];
5392 struct dc_plane_info plane_infos[MAX_SURFACES];
5393 struct dc_scaling_info scaling_infos[MAX_SURFACES];
5394 struct dc_flip_addrs flip_addrs[MAX_SURFACES];
5395 struct dc_stream_update stream_update;
5398 bundle = kzalloc(sizeof(*bundle), GFP_KERNEL);
5401 dm_error("Failed to allocate update bundle\n");
5406 * Disable the cursor first if we're disabling all the planes.
5407 * It'll remain on the screen after the planes are re-enabled
5410 if (acrtc_state->active_planes == 0)
5411 amdgpu_dm_commit_cursors(state);
5413 /* update planes when needed */
5414 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
5415 struct drm_crtc *crtc = new_plane_state->crtc;
5416 struct drm_crtc_state *new_crtc_state;
5417 struct drm_framebuffer *fb = new_plane_state->fb;
5418 bool plane_needs_flip;
5419 struct dc_plane_state *dc_plane;
5420 struct dm_plane_state *dm_new_plane_state = to_dm_plane_state(new_plane_state);
5422 /* Cursor plane is handled after stream updates */
5423 if (plane->type == DRM_PLANE_TYPE_CURSOR)
5426 if (!fb || !crtc || pcrtc != crtc)
5429 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
5430 if (!new_crtc_state->active)
5433 dc_plane = dm_new_plane_state->dc_state;
5435 bundle->surface_updates[planes_count].surface = dc_plane;
5436 if (new_pcrtc_state->color_mgmt_changed) {
5437 bundle->surface_updates[planes_count].gamma = dc_plane->gamma_correction;
5438 bundle->surface_updates[planes_count].in_transfer_func = dc_plane->in_transfer_func;
5441 fill_dc_scaling_info(new_plane_state,
5442 &bundle->scaling_infos[planes_count]);
5444 bundle->surface_updates[planes_count].scaling_info =
5445 &bundle->scaling_infos[planes_count];
5447 plane_needs_flip = old_plane_state->fb && new_plane_state->fb;
5449 pflip_present = pflip_present || plane_needs_flip;
5451 if (!plane_needs_flip) {
5456 abo = gem_to_amdgpu_bo(fb->obj[0]);
5459 * Wait for all fences on this FB. Do limited wait to avoid
5460 * deadlock during GPU reset when this fence will not signal
5461 * but we hold reservation lock for the BO.
5463 r = reservation_object_wait_timeout_rcu(abo->tbo.resv, true,
5465 msecs_to_jiffies(5000));
5466 if (unlikely(r <= 0))
5467 DRM_ERROR("Waiting for fences timed out or interrupted!");
5470 * TODO This might fail and hence better not used, wait
5471 * explicitly on fences instead
5472 * and in general should be called for
5473 * blocking commit to as per framework helpers
5475 r = amdgpu_bo_reserve(abo, true);
5476 if (unlikely(r != 0))
5477 DRM_ERROR("failed to reserve buffer before flip\n");
5479 amdgpu_bo_get_tiling_flags(abo, &tiling_flags);
5481 amdgpu_bo_unreserve(abo);
5483 fill_dc_plane_info_and_addr(
5484 dm->adev, new_plane_state, tiling_flags,
5485 &bundle->plane_infos[planes_count],
5486 &bundle->flip_addrs[planes_count].address);
5488 bundle->surface_updates[planes_count].plane_info =
5489 &bundle->plane_infos[planes_count];
5491 bundle->flip_addrs[planes_count].flip_immediate =
5492 (crtc->state->pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC) != 0;
5494 timestamp_ns = ktime_get_ns();
5495 bundle->flip_addrs[planes_count].flip_timestamp_in_us = div_u64(timestamp_ns, 1000);
5496 bundle->surface_updates[planes_count].flip_addr = &bundle->flip_addrs[planes_count];
5497 bundle->surface_updates[planes_count].surface = dc_plane;
5499 if (!bundle->surface_updates[planes_count].surface) {
5500 DRM_ERROR("No surface for CRTC: id=%d\n",
5501 acrtc_attach->crtc_id);
5505 if (plane == pcrtc->primary)
5506 update_freesync_state_on_stream(
5509 acrtc_state->stream,
5511 bundle->flip_addrs[planes_count].flip_timestamp_in_us);
5513 DRM_DEBUG_DRIVER("%s Flipping to hi: 0x%x, low: 0x%x\n",
5515 bundle->flip_addrs[planes_count].address.grph.addr.high_part,
5516 bundle->flip_addrs[planes_count].address.grph.addr.low_part);
5522 if (pflip_present) {
5524 /* Use old throttling in non-vrr fixed refresh rate mode
5525 * to keep flip scheduling based on target vblank counts
5526 * working in a backwards compatible way, e.g., for
5527 * clients using the GLX_OML_sync_control extension or
5528 * DRI3/Present extension with defined target_msc.
5530 last_flip_vblank = amdgpu_get_vblank_counter_kms(dm->ddev, acrtc_attach->crtc_id);
5533 /* For variable refresh rate mode only:
5534 * Get vblank of last completed flip to avoid > 1 vrr
5535 * flips per video frame by use of throttling, but allow
5536 * flip programming anywhere in the possibly large
5537 * variable vrr vblank interval for fine-grained flip
5538 * timing control and more opportunity to avoid stutter
5539 * on late submission of flips.
5541 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
5542 last_flip_vblank = acrtc_attach->last_flip_vblank;
5543 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
5546 target_vblank = last_flip_vblank + wait_for_vblank;
5549 * Wait until we're out of the vertical blank period before the one
5550 * targeted by the flip
5552 while ((acrtc_attach->enabled &&
5553 (amdgpu_display_get_crtc_scanoutpos(dm->ddev, acrtc_attach->crtc_id,
5554 0, &vpos, &hpos, NULL,
5555 NULL, &pcrtc->hwmode)
5556 & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
5557 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
5558 (int)(target_vblank -
5559 amdgpu_get_vblank_counter_kms(dm->ddev, acrtc_attach->crtc_id)) > 0)) {
5560 usleep_range(1000, 1100);
5563 if (acrtc_attach->base.state->event) {
5564 drm_crtc_vblank_get(pcrtc);
5566 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
5568 WARN_ON(acrtc_attach->pflip_status != AMDGPU_FLIP_NONE);
5569 prepare_flip_isr(acrtc_attach);
5571 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
5574 if (acrtc_state->stream) {
5576 if (acrtc_state->freesync_timing_changed)
5577 bundle->stream_update.adjust =
5578 &acrtc_state->stream->adjust;
5580 if (acrtc_state->freesync_vrr_info_changed)
5581 bundle->stream_update.vrr_infopacket =
5582 &acrtc_state->stream->vrr_infopacket;
5586 /* Update the planes if changed or disable if we don't have any. */
5587 if (planes_count || acrtc_state->active_planes == 0) {
5588 if (new_pcrtc_state->mode_changed) {
5589 bundle->stream_update.src = acrtc_state->stream->src;
5590 bundle->stream_update.dst = acrtc_state->stream->dst;
5593 if (new_pcrtc_state->color_mgmt_changed)
5594 bundle->stream_update.out_transfer_func = acrtc_state->stream->out_transfer_func;
5596 acrtc_state->stream->abm_level = acrtc_state->abm_level;
5597 if (acrtc_state->abm_level != dm_old_crtc_state->abm_level)
5598 bundle->stream_update.abm_level = &acrtc_state->abm_level;
5600 mutex_lock(&dm->dc_lock);
5601 dc_commit_updates_for_stream(dm->dc,
5602 bundle->surface_updates,
5604 acrtc_state->stream,
5605 &bundle->stream_update,
5607 mutex_unlock(&dm->dc_lock);
5611 * Update cursor state *after* programming all the planes.
5612 * This avoids redundant programming in the case where we're going
5613 * to be disabling a single plane - those pipes are being disabled.
5615 if (acrtc_state->active_planes)
5616 amdgpu_dm_commit_cursors(state);
5623 * Enable interrupts on CRTCs that are newly active, undergone
5624 * a modeset, or have active planes again.
5626 * Done in two passes, based on the for_modeset flag:
5627 * Pass 1: For CRTCs going through modeset
5628 * Pass 2: For CRTCs going from 0 to n active planes
5630 * Interrupts can only be enabled after the planes are programmed,
5631 * so this requires a two-pass approach since we don't want to
5632 * just defer the interrupts until after commit planes every time.
5634 static void amdgpu_dm_enable_crtc_interrupts(struct drm_device *dev,
5635 struct drm_atomic_state *state,
5638 struct amdgpu_device *adev = dev->dev_private;
5639 struct drm_crtc *crtc;
5640 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
5643 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
5644 new_crtc_state, i) {
5645 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
5646 struct dm_crtc_state *dm_new_crtc_state =
5647 to_dm_crtc_state(new_crtc_state);
5648 struct dm_crtc_state *dm_old_crtc_state =
5649 to_dm_crtc_state(old_crtc_state);
5650 bool modeset = drm_atomic_crtc_needs_modeset(new_crtc_state);
5653 run_pass = (for_modeset && modeset) ||
5654 (!for_modeset && !modeset &&
5655 !dm_old_crtc_state->interrupts_enabled);
5660 if (!dm_new_crtc_state->interrupts_enabled)
5663 manage_dm_interrupts(adev, acrtc, true);
5665 #ifdef CONFIG_DEBUG_FS
5666 /* The stream has changed so CRC capture needs to re-enabled. */
5667 if (dm_new_crtc_state->crc_enabled) {
5668 dm_new_crtc_state->crc_enabled = false;
5669 amdgpu_dm_crtc_set_crc_source(crtc, "auto");
5676 * amdgpu_dm_crtc_copy_transient_flags - copy mirrored flags from DRM to DC
5677 * @crtc_state: the DRM CRTC state
5678 * @stream_state: the DC stream state.
5680 * Copy the mirrored transient state flags from DRM, to DC. It is used to bring
5681 * a dc_stream_state's flags in sync with a drm_crtc_state's flags.
5683 static void amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state *crtc_state,
5684 struct dc_stream_state *stream_state)
5686 stream_state->mode_changed = drm_atomic_crtc_needs_modeset(crtc_state);
5689 static int amdgpu_dm_atomic_commit(struct drm_device *dev,
5690 struct drm_atomic_state *state,
5693 struct drm_crtc *crtc;
5694 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
5695 struct amdgpu_device *adev = dev->dev_private;
5699 * We evade vblank and pflip interrupts on CRTCs that are undergoing
5700 * a modeset, being disabled, or have no active planes.
5702 * It's done in atomic commit rather than commit tail for now since
5703 * some of these interrupt handlers access the current CRTC state and
5704 * potentially the stream pointer itself.
5706 * Since the atomic state is swapped within atomic commit and not within
5707 * commit tail this would leave to new state (that hasn't been committed yet)
5708 * being accesssed from within the handlers.
5710 * TODO: Fix this so we can do this in commit tail and not have to block
5713 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
5714 struct dm_crtc_state *dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
5715 struct dm_crtc_state *dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
5716 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
5718 if (dm_old_crtc_state->interrupts_enabled &&
5719 (!dm_new_crtc_state->interrupts_enabled ||
5720 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
5722 * Drop the extra vblank reference added by CRC
5723 * capture if applicable.
5725 if (dm_new_crtc_state->crc_enabled)
5726 drm_crtc_vblank_put(crtc);
5729 * Only keep CRC capture enabled if there's
5730 * still a stream for the CRTC.
5732 if (!dm_new_crtc_state->stream)
5733 dm_new_crtc_state->crc_enabled = false;
5735 manage_dm_interrupts(adev, acrtc, false);
5739 * Add check here for SoC's that support hardware cursor plane, to
5740 * unset legacy_cursor_update
5743 return drm_atomic_helper_commit(dev, state, nonblock);
5745 /*TODO Handle EINTR, reenable IRQ*/
5749 * amdgpu_dm_atomic_commit_tail() - AMDgpu DM's commit tail implementation.
5750 * @state: The atomic state to commit
5752 * This will tell DC to commit the constructed DC state from atomic_check,
5753 * programming the hardware. Any failures here implies a hardware failure, since
5754 * atomic check should have filtered anything non-kosher.
5756 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state)
5758 struct drm_device *dev = state->dev;
5759 struct amdgpu_device *adev = dev->dev_private;
5760 struct amdgpu_display_manager *dm = &adev->dm;
5761 struct dm_atomic_state *dm_state;
5762 struct dc_state *dc_state = NULL, *dc_state_temp = NULL;
5764 struct drm_crtc *crtc;
5765 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
5766 unsigned long flags;
5767 bool wait_for_vblank = true;
5768 struct drm_connector *connector;
5769 struct drm_connector_state *old_con_state, *new_con_state;
5770 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
5771 int crtc_disable_count = 0;
5773 drm_atomic_helper_update_legacy_modeset_state(dev, state);
5775 dm_state = dm_atomic_get_new_state(state);
5776 if (dm_state && dm_state->context) {
5777 dc_state = dm_state->context;
5779 /* No state changes, retain current state. */
5780 dc_state_temp = dc_create_state(dm->dc);
5781 ASSERT(dc_state_temp);
5782 dc_state = dc_state_temp;
5783 dc_resource_state_copy_construct_current(dm->dc, dc_state);
5786 /* update changed items */
5787 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
5788 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
5790 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
5791 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
5794 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
5795 "planes_changed:%d, mode_changed:%d,active_changed:%d,"
5796 "connectors_changed:%d\n",
5798 new_crtc_state->enable,
5799 new_crtc_state->active,
5800 new_crtc_state->planes_changed,
5801 new_crtc_state->mode_changed,
5802 new_crtc_state->active_changed,
5803 new_crtc_state->connectors_changed);
5805 /* Copy all transient state flags into dc state */
5806 if (dm_new_crtc_state->stream) {
5807 amdgpu_dm_crtc_copy_transient_flags(&dm_new_crtc_state->base,
5808 dm_new_crtc_state->stream);
5811 /* handles headless hotplug case, updating new_state and
5812 * aconnector as needed
5815 if (modeset_required(new_crtc_state, dm_new_crtc_state->stream, dm_old_crtc_state->stream)) {
5817 DRM_DEBUG_DRIVER("Atomic commit: SET crtc id %d: [%p]\n", acrtc->crtc_id, acrtc);
5819 if (!dm_new_crtc_state->stream) {
5821 * this could happen because of issues with
5822 * userspace notifications delivery.
5823 * In this case userspace tries to set mode on
5824 * display which is disconnected in fact.
5825 * dc_sink is NULL in this case on aconnector.
5826 * We expect reset mode will come soon.
5828 * This can also happen when unplug is done
5829 * during resume sequence ended
5831 * In this case, we want to pretend we still
5832 * have a sink to keep the pipe running so that
5833 * hw state is consistent with the sw state
5835 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
5836 __func__, acrtc->base.base.id);
5840 if (dm_old_crtc_state->stream)
5841 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
5843 pm_runtime_get_noresume(dev->dev);
5845 acrtc->enabled = true;
5846 acrtc->hw_mode = new_crtc_state->mode;
5847 crtc->hwmode = new_crtc_state->mode;
5848 } else if (modereset_required(new_crtc_state)) {
5849 DRM_DEBUG_DRIVER("Atomic commit: RESET. crtc id %d:[%p]\n", acrtc->crtc_id, acrtc);
5851 /* i.e. reset mode */
5852 if (dm_old_crtc_state->stream)
5853 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
5855 } /* for_each_crtc_in_state() */
5858 dm_enable_per_frame_crtc_master_sync(dc_state);
5859 mutex_lock(&dm->dc_lock);
5860 WARN_ON(!dc_commit_state(dm->dc, dc_state));
5861 mutex_unlock(&dm->dc_lock);
5864 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
5865 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
5867 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
5869 if (dm_new_crtc_state->stream != NULL) {
5870 const struct dc_stream_status *status =
5871 dc_stream_get_status(dm_new_crtc_state->stream);
5874 status = dc_stream_get_status_from_state(dc_state,
5875 dm_new_crtc_state->stream);
5878 DC_ERR("got no status for stream %p on acrtc%p\n", dm_new_crtc_state->stream, acrtc);
5880 acrtc->otg_inst = status->primary_otg_inst;
5884 /* Handle connector state changes */
5885 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
5886 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
5887 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
5888 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
5889 struct dc_surface_update dummy_updates[MAX_SURFACES];
5890 struct dc_stream_update stream_update;
5891 struct dc_info_packet hdr_packet;
5892 struct dc_stream_status *status = NULL;
5893 bool abm_changed, hdr_changed, scaling_changed;
5895 memset(&dummy_updates, 0, sizeof(dummy_updates));
5896 memset(&stream_update, 0, sizeof(stream_update));
5899 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
5900 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
5903 /* Skip any modesets/resets */
5904 if (!acrtc || drm_atomic_crtc_needs_modeset(new_crtc_state))
5907 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
5908 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
5910 scaling_changed = is_scaling_state_different(dm_new_con_state,
5913 abm_changed = dm_new_crtc_state->abm_level !=
5914 dm_old_crtc_state->abm_level;
5917 is_hdr_metadata_different(old_con_state, new_con_state);
5919 if (!scaling_changed && !abm_changed && !hdr_changed)
5922 if (scaling_changed) {
5923 update_stream_scaling_settings(&dm_new_con_state->base.crtc->mode,
5924 dm_new_con_state, (struct dc_stream_state *)dm_new_crtc_state->stream);
5926 stream_update.src = dm_new_crtc_state->stream->src;
5927 stream_update.dst = dm_new_crtc_state->stream->dst;
5931 dm_new_crtc_state->stream->abm_level = dm_new_crtc_state->abm_level;
5933 stream_update.abm_level = &dm_new_crtc_state->abm_level;
5937 fill_hdr_info_packet(new_con_state, &hdr_packet);
5938 stream_update.hdr_static_metadata = &hdr_packet;
5941 status = dc_stream_get_status(dm_new_crtc_state->stream);
5943 WARN_ON(!status->plane_count);
5946 * TODO: DC refuses to perform stream updates without a dc_surface_update.
5947 * Here we create an empty update on each plane.
5948 * To fix this, DC should permit updating only stream properties.
5950 for (j = 0; j < status->plane_count; j++)
5951 dummy_updates[j].surface = status->plane_states[0];
5954 mutex_lock(&dm->dc_lock);
5955 dc_commit_updates_for_stream(dm->dc,
5957 status->plane_count,
5958 dm_new_crtc_state->stream,
5961 mutex_unlock(&dm->dc_lock);
5964 /* Count number of newly disabled CRTCs for dropping PM refs later. */
5965 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
5966 new_crtc_state, i) {
5967 if (old_crtc_state->active && !new_crtc_state->active)
5968 crtc_disable_count++;
5970 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
5971 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
5973 /* Update freesync active state. */
5974 pre_update_freesync_state_on_stream(dm, dm_new_crtc_state);
5976 /* Handle vrr on->off / off->on transitions */
5977 amdgpu_dm_handle_vrr_transition(dm_old_crtc_state,
5981 /* Enable interrupts for CRTCs going through a modeset. */
5982 amdgpu_dm_enable_crtc_interrupts(dev, state, true);
5984 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j)
5985 if (new_crtc_state->pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC)
5986 wait_for_vblank = false;
5988 /* update planes when needed per crtc*/
5989 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j) {
5990 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
5992 if (dm_new_crtc_state->stream)
5993 amdgpu_dm_commit_planes(state, dc_state, dev,
5994 dm, crtc, wait_for_vblank);
5997 /* Enable interrupts for CRTCs going from 0 to n active planes. */
5998 amdgpu_dm_enable_crtc_interrupts(dev, state, false);
6001 * send vblank event on all events not handled in flip and
6002 * mark consumed event for drm_atomic_helper_commit_hw_done
6004 spin_lock_irqsave(&adev->ddev->event_lock, flags);
6005 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
6007 if (new_crtc_state->event)
6008 drm_send_event_locked(dev, &new_crtc_state->event->base);
6010 new_crtc_state->event = NULL;
6012 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
6014 /* Signal HW programming completion */
6015 drm_atomic_helper_commit_hw_done(state);
6017 if (wait_for_vblank)
6018 drm_atomic_helper_wait_for_flip_done(dev, state);
6020 drm_atomic_helper_cleanup_planes(dev, state);
6023 * Finally, drop a runtime PM reference for each newly disabled CRTC,
6024 * so we can put the GPU into runtime suspend if we're not driving any
6027 for (i = 0; i < crtc_disable_count; i++)
6028 pm_runtime_put_autosuspend(dev->dev);
6029 pm_runtime_mark_last_busy(dev->dev);
6032 dc_release_state(dc_state_temp);
6036 static int dm_force_atomic_commit(struct drm_connector *connector)
6039 struct drm_device *ddev = connector->dev;
6040 struct drm_atomic_state *state = drm_atomic_state_alloc(ddev);
6041 struct amdgpu_crtc *disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
6042 struct drm_plane *plane = disconnected_acrtc->base.primary;
6043 struct drm_connector_state *conn_state;
6044 struct drm_crtc_state *crtc_state;
6045 struct drm_plane_state *plane_state;
6050 state->acquire_ctx = ddev->mode_config.acquire_ctx;
6052 /* Construct an atomic state to restore previous display setting */
6055 * Attach connectors to drm_atomic_state
6057 conn_state = drm_atomic_get_connector_state(state, connector);
6059 ret = PTR_ERR_OR_ZERO(conn_state);
6063 /* Attach crtc to drm_atomic_state*/
6064 crtc_state = drm_atomic_get_crtc_state(state, &disconnected_acrtc->base);
6066 ret = PTR_ERR_OR_ZERO(crtc_state);
6070 /* force a restore */
6071 crtc_state->mode_changed = true;
6073 /* Attach plane to drm_atomic_state */
6074 plane_state = drm_atomic_get_plane_state(state, plane);
6076 ret = PTR_ERR_OR_ZERO(plane_state);
6081 /* Call commit internally with the state we just constructed */
6082 ret = drm_atomic_commit(state);
6087 DRM_ERROR("Restoring old state failed with %i\n", ret);
6088 drm_atomic_state_put(state);
6094 * This function handles all cases when set mode does not come upon hotplug.
6095 * This includes when a display is unplugged then plugged back into the
6096 * same port and when running without usermode desktop manager supprot
6098 void dm_restore_drm_connector_state(struct drm_device *dev,
6099 struct drm_connector *connector)
6101 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
6102 struct amdgpu_crtc *disconnected_acrtc;
6103 struct dm_crtc_state *acrtc_state;
6105 if (!aconnector->dc_sink || !connector->state || !connector->encoder)
6108 disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
6109 if (!disconnected_acrtc)
6112 acrtc_state = to_dm_crtc_state(disconnected_acrtc->base.state);
6113 if (!acrtc_state->stream)
6117 * If the previous sink is not released and different from the current,
6118 * we deduce we are in a state where we can not rely on usermode call
6119 * to turn on the display, so we do it here
6121 if (acrtc_state->stream->sink != aconnector->dc_sink)
6122 dm_force_atomic_commit(&aconnector->base);
6126 * Grabs all modesetting locks to serialize against any blocking commits,
6127 * Waits for completion of all non blocking commits.
6129 static int do_aquire_global_lock(struct drm_device *dev,
6130 struct drm_atomic_state *state)
6132 struct drm_crtc *crtc;
6133 struct drm_crtc_commit *commit;
6137 * Adding all modeset locks to aquire_ctx will
6138 * ensure that when the framework release it the
6139 * extra locks we are locking here will get released to
6141 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
6145 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6146 spin_lock(&crtc->commit_lock);
6147 commit = list_first_entry_or_null(&crtc->commit_list,
6148 struct drm_crtc_commit, commit_entry);
6150 drm_crtc_commit_get(commit);
6151 spin_unlock(&crtc->commit_lock);
6157 * Make sure all pending HW programming completed and
6160 ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ);
6163 ret = wait_for_completion_interruptible_timeout(
6164 &commit->flip_done, 10*HZ);
6167 DRM_ERROR("[CRTC:%d:%s] hw_done or flip_done "
6168 "timed out\n", crtc->base.id, crtc->name);
6170 drm_crtc_commit_put(commit);
6173 return ret < 0 ? ret : 0;
6176 static void get_freesync_config_for_crtc(
6177 struct dm_crtc_state *new_crtc_state,
6178 struct dm_connector_state *new_con_state)
6180 struct mod_freesync_config config = {0};
6181 struct amdgpu_dm_connector *aconnector =
6182 to_amdgpu_dm_connector(new_con_state->base.connector);
6183 struct drm_display_mode *mode = &new_crtc_state->base.mode;
6184 int vrefresh = drm_mode_vrefresh(mode);
6186 new_crtc_state->vrr_supported = new_con_state->freesync_capable &&
6187 vrefresh >= aconnector->min_vfreq &&
6188 vrefresh <= aconnector->max_vfreq;
6190 if (new_crtc_state->vrr_supported) {
6191 new_crtc_state->stream->ignore_msa_timing_param = true;
6192 config.state = new_crtc_state->base.vrr_enabled ?
6193 VRR_STATE_ACTIVE_VARIABLE :
6195 config.min_refresh_in_uhz =
6196 aconnector->min_vfreq * 1000000;
6197 config.max_refresh_in_uhz =
6198 aconnector->max_vfreq * 1000000;
6199 config.vsif_supported = true;
6203 new_crtc_state->freesync_config = config;
6206 static void reset_freesync_config_for_crtc(
6207 struct dm_crtc_state *new_crtc_state)
6209 new_crtc_state->vrr_supported = false;
6211 memset(&new_crtc_state->vrr_params, 0,
6212 sizeof(new_crtc_state->vrr_params));
6213 memset(&new_crtc_state->vrr_infopacket, 0,
6214 sizeof(new_crtc_state->vrr_infopacket));
6217 static int dm_update_crtc_state(struct amdgpu_display_manager *dm,
6218 struct drm_atomic_state *state,
6219 struct drm_crtc *crtc,
6220 struct drm_crtc_state *old_crtc_state,
6221 struct drm_crtc_state *new_crtc_state,
6223 bool *lock_and_validation_needed)
6225 struct dm_atomic_state *dm_state = NULL;
6226 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
6227 struct dc_stream_state *new_stream;
6231 * TODO Move this code into dm_crtc_atomic_check once we get rid of dc_validation_set
6232 * update changed items
6234 struct amdgpu_crtc *acrtc = NULL;
6235 struct amdgpu_dm_connector *aconnector = NULL;
6236 struct drm_connector_state *drm_new_conn_state = NULL, *drm_old_conn_state = NULL;
6237 struct dm_connector_state *dm_new_conn_state = NULL, *dm_old_conn_state = NULL;
6241 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
6242 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
6243 acrtc = to_amdgpu_crtc(crtc);
6244 aconnector = amdgpu_dm_find_first_crtc_matching_connector(state, crtc);
6246 /* TODO This hack should go away */
6247 if (aconnector && enable) {
6248 /* Make sure fake sink is created in plug-in scenario */
6249 drm_new_conn_state = drm_atomic_get_new_connector_state(state,
6251 drm_old_conn_state = drm_atomic_get_old_connector_state(state,
6254 if (IS_ERR(drm_new_conn_state)) {
6255 ret = PTR_ERR_OR_ZERO(drm_new_conn_state);
6259 dm_new_conn_state = to_dm_connector_state(drm_new_conn_state);
6260 dm_old_conn_state = to_dm_connector_state(drm_old_conn_state);
6262 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
6265 new_stream = create_stream_for_sink(aconnector,
6266 &new_crtc_state->mode,
6268 dm_old_crtc_state->stream);
6271 * we can have no stream on ACTION_SET if a display
6272 * was disconnected during S3, in this case it is not an
6273 * error, the OS will be updated after detection, and
6274 * will do the right thing on next atomic commit
6278 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
6279 __func__, acrtc->base.base.id);
6284 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
6286 ret = fill_hdr_info_packet(drm_new_conn_state,
6287 &new_stream->hdr_static_metadata);
6291 if (dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) &&
6292 dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream)) {
6293 new_crtc_state->mode_changed = false;
6294 DRM_DEBUG_DRIVER("Mode change not required, setting mode_changed to %d",
6295 new_crtc_state->mode_changed);
6299 /* mode_changed flag may get updated above, need to check again */
6300 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
6304 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
6305 "planes_changed:%d, mode_changed:%d,active_changed:%d,"
6306 "connectors_changed:%d\n",
6308 new_crtc_state->enable,
6309 new_crtc_state->active,
6310 new_crtc_state->planes_changed,
6311 new_crtc_state->mode_changed,
6312 new_crtc_state->active_changed,
6313 new_crtc_state->connectors_changed);
6315 /* Remove stream for any changed/disabled CRTC */
6318 if (!dm_old_crtc_state->stream)
6321 ret = dm_atomic_get_state(state, &dm_state);
6325 DRM_DEBUG_DRIVER("Disabling DRM crtc: %d\n",
6328 /* i.e. reset mode */
6329 if (dc_remove_stream_from_ctx(
6332 dm_old_crtc_state->stream) != DC_OK) {
6337 dc_stream_release(dm_old_crtc_state->stream);
6338 dm_new_crtc_state->stream = NULL;
6340 reset_freesync_config_for_crtc(dm_new_crtc_state);
6342 *lock_and_validation_needed = true;
6344 } else {/* Add stream for any updated/enabled CRTC */
6346 * Quick fix to prevent NULL pointer on new_stream when
6347 * added MST connectors not found in existing crtc_state in the chained mode
6348 * TODO: need to dig out the root cause of that
6350 if (!aconnector || (!aconnector->dc_sink && aconnector->mst_port))
6353 if (modereset_required(new_crtc_state))
6356 if (modeset_required(new_crtc_state, new_stream,
6357 dm_old_crtc_state->stream)) {
6359 WARN_ON(dm_new_crtc_state->stream);
6361 ret = dm_atomic_get_state(state, &dm_state);
6365 dm_new_crtc_state->stream = new_stream;
6367 dc_stream_retain(new_stream);
6369 DRM_DEBUG_DRIVER("Enabling DRM crtc: %d\n",
6372 if (dc_add_stream_to_ctx(
6375 dm_new_crtc_state->stream) != DC_OK) {
6380 *lock_and_validation_needed = true;
6385 /* Release extra reference */
6387 dc_stream_release(new_stream);
6390 * We want to do dc stream updates that do not require a
6391 * full modeset below.
6393 if (!(enable && aconnector && new_crtc_state->enable &&
6394 new_crtc_state->active))
6397 * Given above conditions, the dc state cannot be NULL because:
6398 * 1. We're in the process of enabling CRTCs (just been added
6399 * to the dc context, or already is on the context)
6400 * 2. Has a valid connector attached, and
6401 * 3. Is currently active and enabled.
6402 * => The dc stream state currently exists.
6404 BUG_ON(dm_new_crtc_state->stream == NULL);
6406 /* Scaling or underscan settings */
6407 if (is_scaling_state_different(dm_old_conn_state, dm_new_conn_state))
6408 update_stream_scaling_settings(
6409 &new_crtc_state->mode, dm_new_conn_state, dm_new_crtc_state->stream);
6412 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
6415 * Color management settings. We also update color properties
6416 * when a modeset is needed, to ensure it gets reprogrammed.
6418 if (dm_new_crtc_state->base.color_mgmt_changed ||
6419 drm_atomic_crtc_needs_modeset(new_crtc_state)) {
6420 ret = amdgpu_dm_set_regamma_lut(dm_new_crtc_state);
6423 amdgpu_dm_set_ctm(dm_new_crtc_state);
6426 /* Update Freesync settings. */
6427 get_freesync_config_for_crtc(dm_new_crtc_state,
6434 dc_stream_release(new_stream);
6438 static bool should_reset_plane(struct drm_atomic_state *state,
6439 struct drm_plane *plane,
6440 struct drm_plane_state *old_plane_state,
6441 struct drm_plane_state *new_plane_state)
6443 struct drm_plane *other;
6444 struct drm_plane_state *old_other_state, *new_other_state;
6445 struct drm_crtc_state *new_crtc_state;
6449 * TODO: Remove this hack once the checks below are sufficient
6450 * enough to determine when we need to reset all the planes on
6453 if (state->allow_modeset)
6456 /* Exit early if we know that we're adding or removing the plane. */
6457 if (old_plane_state->crtc != new_plane_state->crtc)
6460 /* old crtc == new_crtc == NULL, plane not in context. */
6461 if (!new_plane_state->crtc)
6465 drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
6467 if (!new_crtc_state)
6470 if (drm_atomic_crtc_needs_modeset(new_crtc_state))
6474 * If there are any new primary or overlay planes being added or
6475 * removed then the z-order can potentially change. To ensure
6476 * correct z-order and pipe acquisition the current DC architecture
6477 * requires us to remove and recreate all existing planes.
6479 * TODO: Come up with a more elegant solution for this.
6481 for_each_oldnew_plane_in_state(state, other, old_other_state, new_other_state, i) {
6482 if (other->type == DRM_PLANE_TYPE_CURSOR)
6485 if (old_other_state->crtc != new_plane_state->crtc &&
6486 new_other_state->crtc != new_plane_state->crtc)
6489 if (old_other_state->crtc != new_other_state->crtc)
6492 /* TODO: Remove this once we can handle fast format changes. */
6493 if (old_other_state->fb && new_other_state->fb &&
6494 old_other_state->fb->format != new_other_state->fb->format)
6501 static int dm_update_plane_state(struct dc *dc,
6502 struct drm_atomic_state *state,
6503 struct drm_plane *plane,
6504 struct drm_plane_state *old_plane_state,
6505 struct drm_plane_state *new_plane_state,
6507 bool *lock_and_validation_needed)
6510 struct dm_atomic_state *dm_state = NULL;
6511 struct drm_crtc *new_plane_crtc, *old_plane_crtc;
6512 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
6513 struct dm_crtc_state *dm_new_crtc_state, *dm_old_crtc_state;
6514 struct dm_plane_state *dm_new_plane_state, *dm_old_plane_state;
6519 new_plane_crtc = new_plane_state->crtc;
6520 old_plane_crtc = old_plane_state->crtc;
6521 dm_new_plane_state = to_dm_plane_state(new_plane_state);
6522 dm_old_plane_state = to_dm_plane_state(old_plane_state);
6524 /*TODO Implement atomic check for cursor plane */
6525 if (plane->type == DRM_PLANE_TYPE_CURSOR)
6528 needs_reset = should_reset_plane(state, plane, old_plane_state,
6531 /* Remove any changed/removed planes */
6536 if (!old_plane_crtc)
6539 old_crtc_state = drm_atomic_get_old_crtc_state(
6540 state, old_plane_crtc);
6541 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
6543 if (!dm_old_crtc_state->stream)
6546 DRM_DEBUG_ATOMIC("Disabling DRM plane: %d on DRM crtc %d\n",
6547 plane->base.id, old_plane_crtc->base.id);
6549 ret = dm_atomic_get_state(state, &dm_state);
6553 if (!dc_remove_plane_from_context(
6555 dm_old_crtc_state->stream,
6556 dm_old_plane_state->dc_state,
6557 dm_state->context)) {
6564 dc_plane_state_release(dm_old_plane_state->dc_state);
6565 dm_new_plane_state->dc_state = NULL;
6567 *lock_and_validation_needed = true;
6569 } else { /* Add new planes */
6570 struct dc_plane_state *dc_new_plane_state;
6572 if (drm_atomic_plane_disabling(plane->state, new_plane_state))
6575 if (!new_plane_crtc)
6578 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_crtc);
6579 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
6581 if (!dm_new_crtc_state->stream)
6587 WARN_ON(dm_new_plane_state->dc_state);
6589 dc_new_plane_state = dc_create_plane_state(dc);
6590 if (!dc_new_plane_state)
6593 DRM_DEBUG_DRIVER("Enabling DRM plane: %d on DRM crtc %d\n",
6594 plane->base.id, new_plane_crtc->base.id);
6596 ret = fill_dc_plane_attributes(
6597 new_plane_crtc->dev->dev_private,
6602 dc_plane_state_release(dc_new_plane_state);
6606 ret = dm_atomic_get_state(state, &dm_state);
6608 dc_plane_state_release(dc_new_plane_state);
6613 * Any atomic check errors that occur after this will
6614 * not need a release. The plane state will be attached
6615 * to the stream, and therefore part of the atomic
6616 * state. It'll be released when the atomic state is
6619 if (!dc_add_plane_to_context(
6621 dm_new_crtc_state->stream,
6623 dm_state->context)) {
6625 dc_plane_state_release(dc_new_plane_state);
6629 dm_new_plane_state->dc_state = dc_new_plane_state;
6631 /* Tell DC to do a full surface update every time there
6632 * is a plane change. Inefficient, but works for now.
6634 dm_new_plane_state->dc_state->update_flags.bits.full_update = 1;
6636 *lock_and_validation_needed = true;
6644 dm_determine_update_type_for_commit(struct amdgpu_display_manager *dm,
6645 struct drm_atomic_state *state,
6646 enum surface_update_type *out_type)
6648 struct dc *dc = dm->dc;
6649 struct dm_atomic_state *dm_state = NULL, *old_dm_state = NULL;
6650 int i, j, num_plane, ret = 0;
6651 struct drm_plane_state *old_plane_state, *new_plane_state;
6652 struct dm_plane_state *new_dm_plane_state, *old_dm_plane_state;
6653 struct drm_crtc *new_plane_crtc, *old_plane_crtc;
6654 struct drm_plane *plane;
6656 struct drm_crtc *crtc;
6657 struct drm_crtc_state *new_crtc_state, *old_crtc_state;
6658 struct dm_crtc_state *new_dm_crtc_state, *old_dm_crtc_state;
6659 struct dc_stream_status *status = NULL;
6661 struct dc_surface_update *updates;
6662 enum surface_update_type update_type = UPDATE_TYPE_FAST;
6664 updates = kcalloc(MAX_SURFACES, sizeof(*updates), GFP_KERNEL);
6667 DRM_ERROR("Failed to allocate plane updates\n");
6668 /* Set type to FULL to avoid crashing in DC*/
6669 update_type = UPDATE_TYPE_FULL;
6673 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
6674 struct dc_scaling_info scaling_info;
6675 struct dc_stream_update stream_update;
6677 memset(&stream_update, 0, sizeof(stream_update));
6679 new_dm_crtc_state = to_dm_crtc_state(new_crtc_state);
6680 old_dm_crtc_state = to_dm_crtc_state(old_crtc_state);
6683 if (new_dm_crtc_state->stream != old_dm_crtc_state->stream) {
6684 update_type = UPDATE_TYPE_FULL;
6688 if (!new_dm_crtc_state->stream)
6691 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, j) {
6692 new_plane_crtc = new_plane_state->crtc;
6693 old_plane_crtc = old_plane_state->crtc;
6694 new_dm_plane_state = to_dm_plane_state(new_plane_state);
6695 old_dm_plane_state = to_dm_plane_state(old_plane_state);
6697 if (plane->type == DRM_PLANE_TYPE_CURSOR)
6700 if (new_dm_plane_state->dc_state != old_dm_plane_state->dc_state) {
6701 update_type = UPDATE_TYPE_FULL;
6705 if (crtc != new_plane_crtc)
6708 updates[num_plane].surface = new_dm_plane_state->dc_state;
6710 if (new_crtc_state->mode_changed) {
6711 stream_update.dst = new_dm_crtc_state->stream->dst;
6712 stream_update.src = new_dm_crtc_state->stream->src;
6715 if (new_crtc_state->color_mgmt_changed) {
6716 updates[num_plane].gamma =
6717 new_dm_plane_state->dc_state->gamma_correction;
6718 updates[num_plane].in_transfer_func =
6719 new_dm_plane_state->dc_state->in_transfer_func;
6720 stream_update.gamut_remap =
6721 &new_dm_crtc_state->stream->gamut_remap_matrix;
6722 stream_update.out_transfer_func =
6723 new_dm_crtc_state->stream->out_transfer_func;
6726 ret = fill_dc_scaling_info(new_plane_state,
6731 updates[num_plane].scaling_info = &scaling_info;
6739 ret = dm_atomic_get_state(state, &dm_state);
6743 old_dm_state = dm_atomic_get_old_state(state);
6744 if (!old_dm_state) {
6749 status = dc_stream_get_status_from_state(old_dm_state->context,
6750 new_dm_crtc_state->stream);
6753 * TODO: DC modifies the surface during this call so we need
6754 * to lock here - find a way to do this without locking.
6756 mutex_lock(&dm->dc_lock);
6757 update_type = dc_check_update_surfaces_for_stream(dc, updates, num_plane,
6758 &stream_update, status);
6759 mutex_unlock(&dm->dc_lock);
6761 if (update_type > UPDATE_TYPE_MED) {
6762 update_type = UPDATE_TYPE_FULL;
6770 *out_type = update_type;
6775 * amdgpu_dm_atomic_check() - Atomic check implementation for AMDgpu DM.
6776 * @dev: The DRM device
6777 * @state: The atomic state to commit
6779 * Validate that the given atomic state is programmable by DC into hardware.
6780 * This involves constructing a &struct dc_state reflecting the new hardware
6781 * state we wish to commit, then querying DC to see if it is programmable. It's
6782 * important not to modify the existing DC state. Otherwise, atomic_check
6783 * may unexpectedly commit hardware changes.
6785 * When validating the DC state, it's important that the right locks are
6786 * acquired. For full updates case which removes/adds/updates streams on one
6787 * CRTC while flipping on another CRTC, acquiring global lock will guarantee
6788 * that any such full update commit will wait for completion of any outstanding
6789 * flip using DRMs synchronization events. See
6790 * dm_determine_update_type_for_commit()
6792 * Note that DM adds the affected connectors for all CRTCs in state, when that
6793 * might not seem necessary. This is because DC stream creation requires the
6794 * DC sink, which is tied to the DRM connector state. Cleaning this up should
6795 * be possible but non-trivial - a possible TODO item.
6797 * Return: -Error code if validation failed.
6799 static int amdgpu_dm_atomic_check(struct drm_device *dev,
6800 struct drm_atomic_state *state)
6802 struct amdgpu_device *adev = dev->dev_private;
6803 struct dm_atomic_state *dm_state = NULL;
6804 struct dc *dc = adev->dm.dc;
6805 struct drm_connector *connector;
6806 struct drm_connector_state *old_con_state, *new_con_state;
6807 struct drm_crtc *crtc;
6808 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
6809 struct drm_plane *plane;
6810 struct drm_plane_state *old_plane_state, *new_plane_state;
6811 enum surface_update_type update_type = UPDATE_TYPE_FAST;
6812 enum surface_update_type overall_update_type = UPDATE_TYPE_FAST;
6817 * This bool will be set for true for any modeset/reset
6818 * or plane update which implies non fast surface update.
6820 bool lock_and_validation_needed = false;
6822 ret = drm_atomic_helper_check_modeset(dev, state);
6826 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
6827 if (!drm_atomic_crtc_needs_modeset(new_crtc_state) &&
6828 !new_crtc_state->color_mgmt_changed &&
6829 old_crtc_state->vrr_enabled == new_crtc_state->vrr_enabled)
6832 if (!new_crtc_state->enable)
6835 ret = drm_atomic_add_affected_connectors(state, crtc);
6839 ret = drm_atomic_add_affected_planes(state, crtc);
6845 * Add all primary and overlay planes on the CRTC to the state
6846 * whenever a plane is enabled to maintain correct z-ordering
6847 * and to enable fast surface updates.
6849 drm_for_each_crtc(crtc, dev) {
6850 bool modified = false;
6852 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
6853 if (plane->type == DRM_PLANE_TYPE_CURSOR)
6856 if (new_plane_state->crtc == crtc ||
6857 old_plane_state->crtc == crtc) {
6866 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
6867 if (plane->type == DRM_PLANE_TYPE_CURSOR)
6871 drm_atomic_get_plane_state(state, plane);
6873 if (IS_ERR(new_plane_state)) {
6874 ret = PTR_ERR(new_plane_state);
6880 /* Remove exiting planes if they are modified */
6881 for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) {
6882 ret = dm_update_plane_state(dc, state, plane,
6886 &lock_and_validation_needed);
6891 /* Disable all crtcs which require disable */
6892 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
6893 ret = dm_update_crtc_state(&adev->dm, state, crtc,
6897 &lock_and_validation_needed);
6902 /* Enable all crtcs which require enable */
6903 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
6904 ret = dm_update_crtc_state(&adev->dm, state, crtc,
6908 &lock_and_validation_needed);
6913 /* Add new/modified planes */
6914 for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) {
6915 ret = dm_update_plane_state(dc, state, plane,
6919 &lock_and_validation_needed);
6924 /* Run this here since we want to validate the streams we created */
6925 ret = drm_atomic_helper_check_planes(dev, state);
6929 /* Check scaling and underscan changes*/
6930 /* TODO Removed scaling changes validation due to inability to commit
6931 * new stream into context w\o causing full reset. Need to
6932 * decide how to handle.
6934 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
6935 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
6936 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
6937 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
6939 /* Skip any modesets/resets */
6940 if (!acrtc || drm_atomic_crtc_needs_modeset(
6941 drm_atomic_get_new_crtc_state(state, &acrtc->base)))
6944 /* Skip any thing not scale or underscan changes */
6945 if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state))
6948 overall_update_type = UPDATE_TYPE_FULL;
6949 lock_and_validation_needed = true;
6952 ret = dm_determine_update_type_for_commit(&adev->dm, state, &update_type);
6956 if (overall_update_type < update_type)
6957 overall_update_type = update_type;
6960 * lock_and_validation_needed was an old way to determine if we need to set
6961 * the global lock. Leaving it in to check if we broke any corner cases
6962 * lock_and_validation_needed true = UPDATE_TYPE_FULL or UPDATE_TYPE_MED
6963 * lock_and_validation_needed false = UPDATE_TYPE_FAST
6965 if (lock_and_validation_needed && overall_update_type <= UPDATE_TYPE_FAST)
6966 WARN(1, "Global lock should be Set, overall_update_type should be UPDATE_TYPE_MED or UPDATE_TYPE_FULL");
6968 if (overall_update_type > UPDATE_TYPE_FAST) {
6969 ret = dm_atomic_get_state(state, &dm_state);
6973 ret = do_aquire_global_lock(dev, state);
6977 if (dc_validate_global_state(dc, dm_state->context, false) != DC_OK) {
6981 } else if (state->legacy_cursor_update) {
6983 * This is a fast cursor update coming from the plane update
6984 * helper, check if it can be done asynchronously for better
6987 state->async_update = !drm_atomic_helper_async_check(dev, state);
6990 /* Must be success */
6995 if (ret == -EDEADLK)
6996 DRM_DEBUG_DRIVER("Atomic check stopped to avoid deadlock.\n");
6997 else if (ret == -EINTR || ret == -EAGAIN || ret == -ERESTARTSYS)
6998 DRM_DEBUG_DRIVER("Atomic check stopped due to signal.\n");
7000 DRM_DEBUG_DRIVER("Atomic check failed with err: %d \n", ret);
7005 static bool is_dp_capable_without_timing_msa(struct dc *dc,
7006 struct amdgpu_dm_connector *amdgpu_dm_connector)
7009 bool capable = false;
7011 if (amdgpu_dm_connector->dc_link &&
7012 dm_helpers_dp_read_dpcd(
7014 amdgpu_dm_connector->dc_link,
7015 DP_DOWN_STREAM_PORT_COUNT,
7017 sizeof(dpcd_data))) {
7018 capable = (dpcd_data & DP_MSA_TIMING_PAR_IGNORED) ? true:false;
7023 void amdgpu_dm_update_freesync_caps(struct drm_connector *connector,
7027 bool edid_check_required;
7028 struct detailed_timing *timing;
7029 struct detailed_non_pixel *data;
7030 struct detailed_data_monitor_range *range;
7031 struct amdgpu_dm_connector *amdgpu_dm_connector =
7032 to_amdgpu_dm_connector(connector);
7033 struct dm_connector_state *dm_con_state = NULL;
7035 struct drm_device *dev = connector->dev;
7036 struct amdgpu_device *adev = dev->dev_private;
7037 bool freesync_capable = false;
7039 if (!connector->state) {
7040 DRM_ERROR("%s - Connector has no state", __func__);
7045 dm_con_state = to_dm_connector_state(connector->state);
7047 amdgpu_dm_connector->min_vfreq = 0;
7048 amdgpu_dm_connector->max_vfreq = 0;
7049 amdgpu_dm_connector->pixel_clock_mhz = 0;
7054 dm_con_state = to_dm_connector_state(connector->state);
7056 edid_check_required = false;
7057 if (!amdgpu_dm_connector->dc_sink) {
7058 DRM_ERROR("dc_sink NULL, could not add free_sync module.\n");
7061 if (!adev->dm.freesync_module)
7064 * if edid non zero restrict freesync only for dp and edp
7067 if (amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT
7068 || amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_EDP) {
7069 edid_check_required = is_dp_capable_without_timing_msa(
7071 amdgpu_dm_connector);
7074 if (edid_check_required == true && (edid->version > 1 ||
7075 (edid->version == 1 && edid->revision > 1))) {
7076 for (i = 0; i < 4; i++) {
7078 timing = &edid->detailed_timings[i];
7079 data = &timing->data.other_data;
7080 range = &data->data.range;
7082 * Check if monitor has continuous frequency mode
7084 if (data->type != EDID_DETAIL_MONITOR_RANGE)
7087 * Check for flag range limits only. If flag == 1 then
7088 * no additional timing information provided.
7089 * Default GTF, GTF Secondary curve and CVT are not
7092 if (range->flags != 1)
7095 amdgpu_dm_connector->min_vfreq = range->min_vfreq;
7096 amdgpu_dm_connector->max_vfreq = range->max_vfreq;
7097 amdgpu_dm_connector->pixel_clock_mhz =
7098 range->pixel_clock_mhz * 10;
7102 if (amdgpu_dm_connector->max_vfreq -
7103 amdgpu_dm_connector->min_vfreq > 10) {
7105 freesync_capable = true;
7111 dm_con_state->freesync_capable = freesync_capable;
7113 if (connector->vrr_capable_property)
7114 drm_connector_set_vrr_capable_property(connector,