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 static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
116 struct drm_plane *plane,
117 uint32_t link_index);
118 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
119 struct amdgpu_dm_connector *amdgpu_dm_connector,
121 struct amdgpu_encoder *amdgpu_encoder);
122 static int amdgpu_dm_encoder_init(struct drm_device *dev,
123 struct amdgpu_encoder *aencoder,
124 uint32_t link_index);
126 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector);
128 static int amdgpu_dm_atomic_commit(struct drm_device *dev,
129 struct drm_atomic_state *state,
132 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state);
134 static int amdgpu_dm_atomic_check(struct drm_device *dev,
135 struct drm_atomic_state *state);
137 static void handle_cursor_update(struct drm_plane *plane,
138 struct drm_plane_state *old_plane_state);
141 * dm_vblank_get_counter
144 * Get counter for number of vertical blanks
147 * struct amdgpu_device *adev - [in] desired amdgpu device
148 * int disp_idx - [in] which CRTC to get the counter from
151 * Counter for vertical blanks
153 static u32 dm_vblank_get_counter(struct amdgpu_device *adev, int crtc)
155 if (crtc >= adev->mode_info.num_crtc)
158 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
159 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(
163 if (acrtc_state->stream == NULL) {
164 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
169 return dc_stream_get_vblank_counter(acrtc_state->stream);
173 static int dm_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
174 u32 *vbl, u32 *position)
176 uint32_t v_blank_start, v_blank_end, h_position, v_position;
178 if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
181 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
182 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(
185 if (acrtc_state->stream == NULL) {
186 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
192 * TODO rework base driver to use values directly.
193 * for now parse it back into reg-format
195 dc_stream_get_scanoutpos(acrtc_state->stream,
201 *position = v_position | (h_position << 16);
202 *vbl = v_blank_start | (v_blank_end << 16);
208 static bool dm_is_idle(void *handle)
214 static int dm_wait_for_idle(void *handle)
220 static bool dm_check_soft_reset(void *handle)
225 static int dm_soft_reset(void *handle)
231 static struct amdgpu_crtc *
232 get_crtc_by_otg_inst(struct amdgpu_device *adev,
235 struct drm_device *dev = adev->ddev;
236 struct drm_crtc *crtc;
237 struct amdgpu_crtc *amdgpu_crtc;
239 if (otg_inst == -1) {
241 return adev->mode_info.crtcs[0];
244 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
245 amdgpu_crtc = to_amdgpu_crtc(crtc);
247 if (amdgpu_crtc->otg_inst == otg_inst)
254 static void dm_pflip_high_irq(void *interrupt_params)
256 struct amdgpu_crtc *amdgpu_crtc;
257 struct common_irq_params *irq_params = interrupt_params;
258 struct amdgpu_device *adev = irq_params->adev;
261 amdgpu_crtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_PFLIP);
263 /* IRQ could occur when in initial stage */
264 /* TODO work and BO cleanup */
265 if (amdgpu_crtc == NULL) {
266 DRM_DEBUG_DRIVER("CRTC is null, returning.\n");
270 spin_lock_irqsave(&adev->ddev->event_lock, flags);
272 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED){
273 DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d !=AMDGPU_FLIP_SUBMITTED(%d) on crtc:%d[%p] \n",
274 amdgpu_crtc->pflip_status,
275 AMDGPU_FLIP_SUBMITTED,
276 amdgpu_crtc->crtc_id,
278 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
282 /* Update to correct count(s) if racing with vblank irq */
283 amdgpu_crtc->last_flip_vblank = drm_crtc_accurate_vblank_count(&amdgpu_crtc->base);
285 /* wake up userspace */
286 if (amdgpu_crtc->event) {
287 drm_crtc_send_vblank_event(&amdgpu_crtc->base, amdgpu_crtc->event);
289 /* page flip completed. clean up */
290 amdgpu_crtc->event = NULL;
295 amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
296 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
298 DRM_DEBUG_DRIVER("%s - crtc :%d[%p], pflip_stat:AMDGPU_FLIP_NONE\n",
299 __func__, amdgpu_crtc->crtc_id, amdgpu_crtc);
301 drm_crtc_vblank_put(&amdgpu_crtc->base);
304 static void dm_crtc_high_irq(void *interrupt_params)
306 struct common_irq_params *irq_params = interrupt_params;
307 struct amdgpu_device *adev = irq_params->adev;
308 struct amdgpu_crtc *acrtc;
309 struct dm_crtc_state *acrtc_state;
311 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VBLANK);
314 drm_crtc_handle_vblank(&acrtc->base);
315 amdgpu_dm_crtc_handle_crc_irq(&acrtc->base);
317 acrtc_state = to_dm_crtc_state(acrtc->base.state);
319 if (acrtc_state->stream &&
320 acrtc_state->vrr_params.supported &&
321 acrtc_state->freesync_config.state == VRR_STATE_ACTIVE_VARIABLE) {
322 mod_freesync_handle_v_update(
323 adev->dm.freesync_module,
325 &acrtc_state->vrr_params);
327 dc_stream_adjust_vmin_vmax(
330 &acrtc_state->vrr_params.adjust);
335 static int dm_set_clockgating_state(void *handle,
336 enum amd_clockgating_state state)
341 static int dm_set_powergating_state(void *handle,
342 enum amd_powergating_state state)
347 /* Prototypes of private functions */
348 static int dm_early_init(void* handle);
350 /* Allocate memory for FBC compressed data */
351 static void amdgpu_dm_fbc_init(struct drm_connector *connector)
353 struct drm_device *dev = connector->dev;
354 struct amdgpu_device *adev = dev->dev_private;
355 struct dm_comressor_info *compressor = &adev->dm.compressor;
356 struct amdgpu_dm_connector *aconn = to_amdgpu_dm_connector(connector);
357 struct drm_display_mode *mode;
358 unsigned long max_size = 0;
360 if (adev->dm.dc->fbc_compressor == NULL)
363 if (aconn->dc_link->connector_signal != SIGNAL_TYPE_EDP)
366 if (compressor->bo_ptr)
370 list_for_each_entry(mode, &connector->modes, head) {
371 if (max_size < mode->htotal * mode->vtotal)
372 max_size = mode->htotal * mode->vtotal;
376 int r = amdgpu_bo_create_kernel(adev, max_size * 4, PAGE_SIZE,
377 AMDGPU_GEM_DOMAIN_GTT, &compressor->bo_ptr,
378 &compressor->gpu_addr, &compressor->cpu_addr);
381 DRM_ERROR("DM: Failed to initialize FBC\n");
383 adev->dm.dc->ctx->fbc_gpu_addr = compressor->gpu_addr;
384 DRM_INFO("DM: FBC alloc %lu\n", max_size*4);
391 static int amdgpu_dm_init(struct amdgpu_device *adev)
393 struct dc_init_data init_data;
394 adev->dm.ddev = adev->ddev;
395 adev->dm.adev = adev;
397 /* Zero all the fields */
398 memset(&init_data, 0, sizeof(init_data));
400 mutex_init(&adev->dm.dc_lock);
402 if(amdgpu_dm_irq_init(adev)) {
403 DRM_ERROR("amdgpu: failed to initialize DM IRQ support.\n");
407 init_data.asic_id.chip_family = adev->family;
409 init_data.asic_id.pci_revision_id = adev->rev_id;
410 init_data.asic_id.hw_internal_rev = adev->external_rev_id;
412 init_data.asic_id.vram_width = adev->gmc.vram_width;
413 /* TODO: initialize init_data.asic_id.vram_type here!!!! */
414 init_data.asic_id.atombios_base_address =
415 adev->mode_info.atom_context->bios;
417 init_data.driver = adev;
419 adev->dm.cgs_device = amdgpu_cgs_create_device(adev);
421 if (!adev->dm.cgs_device) {
422 DRM_ERROR("amdgpu: failed to create cgs device.\n");
426 init_data.cgs_device = adev->dm.cgs_device;
428 init_data.dce_environment = DCE_ENV_PRODUCTION_DRV;
431 * TODO debug why this doesn't work on Raven
433 if (adev->flags & AMD_IS_APU &&
434 adev->asic_type >= CHIP_CARRIZO &&
435 adev->asic_type < CHIP_RAVEN)
436 init_data.flags.gpu_vm_support = true;
438 if (amdgpu_dc_feature_mask & DC_FBC_MASK)
439 init_data.flags.fbc_support = true;
441 /* Display Core create. */
442 adev->dm.dc = dc_create(&init_data);
445 DRM_INFO("Display Core initialized with v%s!\n", DC_VER);
447 DRM_INFO("Display Core failed to initialize with v%s!\n", DC_VER);
451 adev->dm.freesync_module = mod_freesync_create(adev->dm.dc);
452 if (!adev->dm.freesync_module) {
454 "amdgpu: failed to initialize freesync_module.\n");
456 DRM_DEBUG_DRIVER("amdgpu: freesync_module init done %p.\n",
457 adev->dm.freesync_module);
459 amdgpu_dm_init_color_mod();
461 if (amdgpu_dm_initialize_drm_device(adev)) {
463 "amdgpu: failed to initialize sw for display support.\n");
467 /* Update the actual used number of crtc */
468 adev->mode_info.num_crtc = adev->dm.display_indexes_num;
470 /* TODO: Add_display_info? */
472 /* TODO use dynamic cursor width */
473 adev->ddev->mode_config.cursor_width = adev->dm.dc->caps.max_cursor_size;
474 adev->ddev->mode_config.cursor_height = adev->dm.dc->caps.max_cursor_size;
476 if (drm_vblank_init(adev->ddev, adev->dm.display_indexes_num)) {
478 "amdgpu: failed to initialize sw for display support.\n");
482 #if defined(CONFIG_DEBUG_FS)
483 if (dtn_debugfs_init(adev))
484 DRM_ERROR("amdgpu: failed initialize dtn debugfs support.\n");
487 DRM_DEBUG_DRIVER("KMS initialized.\n");
491 amdgpu_dm_fini(adev);
496 static void amdgpu_dm_fini(struct amdgpu_device *adev)
498 amdgpu_dm_destroy_drm_device(&adev->dm);
500 * TODO: pageflip, vlank interrupt
502 * amdgpu_dm_irq_fini(adev);
505 if (adev->dm.cgs_device) {
506 amdgpu_cgs_destroy_device(adev->dm.cgs_device);
507 adev->dm.cgs_device = NULL;
509 if (adev->dm.freesync_module) {
510 mod_freesync_destroy(adev->dm.freesync_module);
511 adev->dm.freesync_module = NULL;
513 /* DC Destroy TODO: Replace destroy DAL */
515 dc_destroy(&adev->dm.dc);
517 mutex_destroy(&adev->dm.dc_lock);
522 static int load_dmcu_fw(struct amdgpu_device *adev)
524 const char *fw_name_dmcu;
526 const struct dmcu_firmware_header_v1_0 *hdr;
528 switch(adev->asic_type) {
547 fw_name_dmcu = FIRMWARE_RAVEN_DMCU;
550 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type);
554 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
555 DRM_DEBUG_KMS("dm: DMCU firmware not supported on direct or SMU loading\n");
559 r = request_firmware_direct(&adev->dm.fw_dmcu, fw_name_dmcu, adev->dev);
561 /* DMCU firmware is not necessary, so don't raise a fuss if it's missing */
562 DRM_DEBUG_KMS("dm: DMCU firmware not found\n");
563 adev->dm.fw_dmcu = NULL;
567 dev_err(adev->dev, "amdgpu_dm: Can't load firmware \"%s\"\n",
572 r = amdgpu_ucode_validate(adev->dm.fw_dmcu);
574 dev_err(adev->dev, "amdgpu_dm: Can't validate firmware \"%s\"\n",
576 release_firmware(adev->dm.fw_dmcu);
577 adev->dm.fw_dmcu = NULL;
581 hdr = (const struct dmcu_firmware_header_v1_0 *)adev->dm.fw_dmcu->data;
582 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].ucode_id = AMDGPU_UCODE_ID_DMCU_ERAM;
583 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].fw = adev->dm.fw_dmcu;
584 adev->firmware.fw_size +=
585 ALIGN(le32_to_cpu(hdr->header.ucode_size_bytes) - le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE);
587 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].ucode_id = AMDGPU_UCODE_ID_DMCU_INTV;
588 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].fw = adev->dm.fw_dmcu;
589 adev->firmware.fw_size +=
590 ALIGN(le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE);
592 adev->dm.dmcu_fw_version = le32_to_cpu(hdr->header.ucode_version);
594 DRM_DEBUG_KMS("PSP loading DMCU firmware\n");
599 static int dm_sw_init(void *handle)
601 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
603 return load_dmcu_fw(adev);
606 static int dm_sw_fini(void *handle)
608 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
610 if(adev->dm.fw_dmcu) {
611 release_firmware(adev->dm.fw_dmcu);
612 adev->dm.fw_dmcu = NULL;
618 static int detect_mst_link_for_all_connectors(struct drm_device *dev)
620 struct amdgpu_dm_connector *aconnector;
621 struct drm_connector *connector;
624 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
626 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
627 aconnector = to_amdgpu_dm_connector(connector);
628 if (aconnector->dc_link->type == dc_connection_mst_branch &&
629 aconnector->mst_mgr.aux) {
630 DRM_DEBUG_DRIVER("DM_MST: starting TM on aconnector: %p [id: %d]\n",
631 aconnector, aconnector->base.base.id);
633 ret = drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true);
635 DRM_ERROR("DM_MST: Failed to start MST\n");
636 ((struct dc_link *)aconnector->dc_link)->type = dc_connection_single;
642 drm_modeset_unlock(&dev->mode_config.connection_mutex);
646 static int dm_late_init(void *handle)
648 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
650 struct dmcu_iram_parameters params;
651 unsigned int linear_lut[16];
653 struct dmcu *dmcu = adev->dm.dc->res_pool->dmcu;
656 for (i = 0; i < 16; i++)
657 linear_lut[i] = 0xFFFF * i / 15;
660 params.backlight_ramping_start = 0xCCCC;
661 params.backlight_ramping_reduction = 0xCCCCCCCC;
662 params.backlight_lut_array_size = 16;
663 params.backlight_lut_array = linear_lut;
665 ret = dmcu_load_iram(dmcu, params);
670 return detect_mst_link_for_all_connectors(adev->ddev);
673 static void s3_handle_mst(struct drm_device *dev, bool suspend)
675 struct amdgpu_dm_connector *aconnector;
676 struct drm_connector *connector;
677 struct drm_dp_mst_topology_mgr *mgr;
679 bool need_hotplug = false;
681 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
683 list_for_each_entry(connector, &dev->mode_config.connector_list,
685 aconnector = to_amdgpu_dm_connector(connector);
686 if (aconnector->dc_link->type != dc_connection_mst_branch ||
687 aconnector->mst_port)
690 mgr = &aconnector->mst_mgr;
693 drm_dp_mst_topology_mgr_suspend(mgr);
695 ret = drm_dp_mst_topology_mgr_resume(mgr);
697 drm_dp_mst_topology_mgr_set_mst(mgr, false);
703 drm_modeset_unlock(&dev->mode_config.connection_mutex);
706 drm_kms_helper_hotplug_event(dev);
710 * dm_hw_init() - Initialize DC device
711 * @handle: The base driver device containing the amdpgu_dm device.
713 * Initialize the &struct amdgpu_display_manager device. This involves calling
714 * the initializers of each DM component, then populating the struct with them.
716 * Although the function implies hardware initialization, both hardware and
717 * software are initialized here. Splitting them out to their relevant init
718 * hooks is a future TODO item.
720 * Some notable things that are initialized here:
722 * - Display Core, both software and hardware
723 * - DC modules that we need (freesync and color management)
724 * - DRM software states
725 * - Interrupt sources and handlers
727 * - Debug FS entries, if enabled
729 static int dm_hw_init(void *handle)
731 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
732 /* Create DAL display manager */
733 amdgpu_dm_init(adev);
734 amdgpu_dm_hpd_init(adev);
740 * dm_hw_fini() - Teardown DC device
741 * @handle: The base driver device containing the amdpgu_dm device.
743 * Teardown components within &struct amdgpu_display_manager that require
744 * cleanup. This involves cleaning up the DRM device, DC, and any modules that
745 * were loaded. Also flush IRQ workqueues and disable them.
747 static int dm_hw_fini(void *handle)
749 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
751 amdgpu_dm_hpd_fini(adev);
753 amdgpu_dm_irq_fini(adev);
754 amdgpu_dm_fini(adev);
758 static int dm_suspend(void *handle)
760 struct amdgpu_device *adev = handle;
761 struct amdgpu_display_manager *dm = &adev->dm;
764 WARN_ON(adev->dm.cached_state);
765 adev->dm.cached_state = drm_atomic_helper_suspend(adev->ddev);
767 s3_handle_mst(adev->ddev, true);
769 amdgpu_dm_irq_suspend(adev);
772 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D3);
777 static struct amdgpu_dm_connector *
778 amdgpu_dm_find_first_crtc_matching_connector(struct drm_atomic_state *state,
779 struct drm_crtc *crtc)
782 struct drm_connector_state *new_con_state;
783 struct drm_connector *connector;
784 struct drm_crtc *crtc_from_state;
786 for_each_new_connector_in_state(state, connector, new_con_state, i) {
787 crtc_from_state = new_con_state->crtc;
789 if (crtc_from_state == crtc)
790 return to_amdgpu_dm_connector(connector);
796 static void emulated_link_detect(struct dc_link *link)
798 struct dc_sink_init_data sink_init_data = { 0 };
799 struct display_sink_capability sink_caps = { 0 };
800 enum dc_edid_status edid_status;
801 struct dc_context *dc_ctx = link->ctx;
802 struct dc_sink *sink = NULL;
803 struct dc_sink *prev_sink = NULL;
805 link->type = dc_connection_none;
806 prev_sink = link->local_sink;
808 if (prev_sink != NULL)
809 dc_sink_retain(prev_sink);
811 switch (link->connector_signal) {
812 case SIGNAL_TYPE_HDMI_TYPE_A: {
813 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
814 sink_caps.signal = SIGNAL_TYPE_HDMI_TYPE_A;
818 case SIGNAL_TYPE_DVI_SINGLE_LINK: {
819 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
820 sink_caps.signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
824 case SIGNAL_TYPE_DVI_DUAL_LINK: {
825 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
826 sink_caps.signal = SIGNAL_TYPE_DVI_DUAL_LINK;
830 case SIGNAL_TYPE_LVDS: {
831 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
832 sink_caps.signal = SIGNAL_TYPE_LVDS;
836 case SIGNAL_TYPE_EDP: {
837 sink_caps.transaction_type =
838 DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
839 sink_caps.signal = SIGNAL_TYPE_EDP;
843 case SIGNAL_TYPE_DISPLAY_PORT: {
844 sink_caps.transaction_type =
845 DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
846 sink_caps.signal = SIGNAL_TYPE_VIRTUAL;
851 DC_ERROR("Invalid connector type! signal:%d\n",
852 link->connector_signal);
856 sink_init_data.link = link;
857 sink_init_data.sink_signal = sink_caps.signal;
859 sink = dc_sink_create(&sink_init_data);
861 DC_ERROR("Failed to create sink!\n");
865 /* dc_sink_create returns a new reference */
866 link->local_sink = sink;
868 edid_status = dm_helpers_read_local_edid(
873 if (edid_status != EDID_OK)
874 DC_ERROR("Failed to read EDID");
878 static int dm_resume(void *handle)
880 struct amdgpu_device *adev = handle;
881 struct drm_device *ddev = adev->ddev;
882 struct amdgpu_display_manager *dm = &adev->dm;
883 struct amdgpu_dm_connector *aconnector;
884 struct drm_connector *connector;
885 struct drm_crtc *crtc;
886 struct drm_crtc_state *new_crtc_state;
887 struct dm_crtc_state *dm_new_crtc_state;
888 struct drm_plane *plane;
889 struct drm_plane_state *new_plane_state;
890 struct dm_plane_state *dm_new_plane_state;
891 enum dc_connection_type new_connection_type = dc_connection_none;
894 /* power on hardware */
895 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0);
897 /* program HPD filter */
900 /* On resume we need to rewrite the MSTM control bits to enamble MST*/
901 s3_handle_mst(ddev, false);
904 * early enable HPD Rx IRQ, should be done before set mode as short
905 * pulse interrupts are used for MST
907 amdgpu_dm_irq_resume_early(adev);
910 list_for_each_entry(connector, &ddev->mode_config.connector_list, head) {
911 aconnector = to_amdgpu_dm_connector(connector);
914 * this is the case when traversing through already created
915 * MST connectors, should be skipped
917 if (aconnector->mst_port)
920 mutex_lock(&aconnector->hpd_lock);
921 if (!dc_link_detect_sink(aconnector->dc_link, &new_connection_type))
922 DRM_ERROR("KMS: Failed to detect connector\n");
924 if (aconnector->base.force && new_connection_type == dc_connection_none)
925 emulated_link_detect(aconnector->dc_link);
927 dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
929 if (aconnector->fake_enable && aconnector->dc_link->local_sink)
930 aconnector->fake_enable = false;
932 if (aconnector->dc_sink)
933 dc_sink_release(aconnector->dc_sink);
934 aconnector->dc_sink = NULL;
935 amdgpu_dm_update_connector_after_detect(aconnector);
936 mutex_unlock(&aconnector->hpd_lock);
939 /* Force mode set in atomic commit */
940 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i)
941 new_crtc_state->active_changed = true;
944 * atomic_check is expected to create the dc states. We need to release
945 * them here, since they were duplicated as part of the suspend
948 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) {
949 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
950 if (dm_new_crtc_state->stream) {
951 WARN_ON(kref_read(&dm_new_crtc_state->stream->refcount) > 1);
952 dc_stream_release(dm_new_crtc_state->stream);
953 dm_new_crtc_state->stream = NULL;
957 for_each_new_plane_in_state(dm->cached_state, plane, new_plane_state, i) {
958 dm_new_plane_state = to_dm_plane_state(new_plane_state);
959 if (dm_new_plane_state->dc_state) {
960 WARN_ON(kref_read(&dm_new_plane_state->dc_state->refcount) > 1);
961 dc_plane_state_release(dm_new_plane_state->dc_state);
962 dm_new_plane_state->dc_state = NULL;
966 drm_atomic_helper_resume(ddev, dm->cached_state);
968 dm->cached_state = NULL;
970 amdgpu_dm_irq_resume_late(adev);
978 * DM (and consequently DC) is registered in the amdgpu base driver as a IP
979 * block. When CONFIG_DRM_AMD_DC is enabled, the DM device IP block is added to
980 * the base driver's device list to be initialized and torn down accordingly.
982 * The functions to do so are provided as hooks in &struct amd_ip_funcs.
985 static const struct amd_ip_funcs amdgpu_dm_funcs = {
987 .early_init = dm_early_init,
988 .late_init = dm_late_init,
989 .sw_init = dm_sw_init,
990 .sw_fini = dm_sw_fini,
991 .hw_init = dm_hw_init,
992 .hw_fini = dm_hw_fini,
993 .suspend = dm_suspend,
995 .is_idle = dm_is_idle,
996 .wait_for_idle = dm_wait_for_idle,
997 .check_soft_reset = dm_check_soft_reset,
998 .soft_reset = dm_soft_reset,
999 .set_clockgating_state = dm_set_clockgating_state,
1000 .set_powergating_state = dm_set_powergating_state,
1003 const struct amdgpu_ip_block_version dm_ip_block =
1005 .type = AMD_IP_BLOCK_TYPE_DCE,
1009 .funcs = &amdgpu_dm_funcs,
1019 static const struct drm_mode_config_funcs amdgpu_dm_mode_funcs = {
1020 .fb_create = amdgpu_display_user_framebuffer_create,
1021 .output_poll_changed = drm_fb_helper_output_poll_changed,
1022 .atomic_check = amdgpu_dm_atomic_check,
1023 .atomic_commit = amdgpu_dm_atomic_commit,
1026 static struct drm_mode_config_helper_funcs amdgpu_dm_mode_config_helperfuncs = {
1027 .atomic_commit_tail = amdgpu_dm_atomic_commit_tail
1031 amdgpu_dm_update_connector_after_detect(struct amdgpu_dm_connector *aconnector)
1033 struct drm_connector *connector = &aconnector->base;
1034 struct drm_device *dev = connector->dev;
1035 struct dc_sink *sink;
1037 /* MST handled by drm_mst framework */
1038 if (aconnector->mst_mgr.mst_state == true)
1042 sink = aconnector->dc_link->local_sink;
1044 dc_sink_retain(sink);
1047 * Edid mgmt connector gets first update only in mode_valid hook and then
1048 * the connector sink is set to either fake or physical sink depends on link status.
1049 * Skip if already done during boot.
1051 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED
1052 && aconnector->dc_em_sink) {
1055 * For S3 resume with headless use eml_sink to fake stream
1056 * because on resume connector->sink is set to NULL
1058 mutex_lock(&dev->mode_config.mutex);
1061 if (aconnector->dc_sink) {
1062 amdgpu_dm_update_freesync_caps(connector, NULL);
1064 * retain and release below are used to
1065 * bump up refcount for sink because the link doesn't point
1066 * to it anymore after disconnect, so on next crtc to connector
1067 * reshuffle by UMD we will get into unwanted dc_sink release
1069 dc_sink_release(aconnector->dc_sink);
1071 aconnector->dc_sink = sink;
1072 dc_sink_retain(aconnector->dc_sink);
1073 amdgpu_dm_update_freesync_caps(connector,
1076 amdgpu_dm_update_freesync_caps(connector, NULL);
1077 if (!aconnector->dc_sink) {
1078 aconnector->dc_sink = aconnector->dc_em_sink;
1079 dc_sink_retain(aconnector->dc_sink);
1083 mutex_unlock(&dev->mode_config.mutex);
1086 dc_sink_release(sink);
1091 * TODO: temporary guard to look for proper fix
1092 * if this sink is MST sink, we should not do anything
1094 if (sink && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
1095 dc_sink_release(sink);
1099 if (aconnector->dc_sink == sink) {
1101 * We got a DP short pulse (Link Loss, DP CTS, etc...).
1104 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: dc_sink didn't change.\n",
1105 aconnector->connector_id);
1107 dc_sink_release(sink);
1111 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: Old sink=%p New sink=%p\n",
1112 aconnector->connector_id, aconnector->dc_sink, sink);
1114 mutex_lock(&dev->mode_config.mutex);
1117 * 1. Update status of the drm connector
1118 * 2. Send an event and let userspace tell us what to do
1122 * TODO: check if we still need the S3 mode update workaround.
1123 * If yes, put it here.
1125 if (aconnector->dc_sink)
1126 amdgpu_dm_update_freesync_caps(connector, NULL);
1128 aconnector->dc_sink = sink;
1129 dc_sink_retain(aconnector->dc_sink);
1130 if (sink->dc_edid.length == 0) {
1131 aconnector->edid = NULL;
1132 drm_dp_cec_unset_edid(&aconnector->dm_dp_aux.aux);
1135 (struct edid *) sink->dc_edid.raw_edid;
1138 drm_connector_update_edid_property(connector,
1140 drm_dp_cec_set_edid(&aconnector->dm_dp_aux.aux,
1143 amdgpu_dm_update_freesync_caps(connector, aconnector->edid);
1146 drm_dp_cec_unset_edid(&aconnector->dm_dp_aux.aux);
1147 amdgpu_dm_update_freesync_caps(connector, NULL);
1148 drm_connector_update_edid_property(connector, NULL);
1149 aconnector->num_modes = 0;
1150 dc_sink_release(aconnector->dc_sink);
1151 aconnector->dc_sink = NULL;
1152 aconnector->edid = NULL;
1155 mutex_unlock(&dev->mode_config.mutex);
1158 dc_sink_release(sink);
1161 static void handle_hpd_irq(void *param)
1163 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
1164 struct drm_connector *connector = &aconnector->base;
1165 struct drm_device *dev = connector->dev;
1166 enum dc_connection_type new_connection_type = dc_connection_none;
1169 * In case of failure or MST no need to update connector status or notify the OS
1170 * since (for MST case) MST does this in its own context.
1172 mutex_lock(&aconnector->hpd_lock);
1174 if (aconnector->fake_enable)
1175 aconnector->fake_enable = false;
1177 if (!dc_link_detect_sink(aconnector->dc_link, &new_connection_type))
1178 DRM_ERROR("KMS: Failed to detect connector\n");
1180 if (aconnector->base.force && new_connection_type == dc_connection_none) {
1181 emulated_link_detect(aconnector->dc_link);
1184 drm_modeset_lock_all(dev);
1185 dm_restore_drm_connector_state(dev, connector);
1186 drm_modeset_unlock_all(dev);
1188 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
1189 drm_kms_helper_hotplug_event(dev);
1191 } else if (dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD)) {
1192 amdgpu_dm_update_connector_after_detect(aconnector);
1195 drm_modeset_lock_all(dev);
1196 dm_restore_drm_connector_state(dev, connector);
1197 drm_modeset_unlock_all(dev);
1199 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
1200 drm_kms_helper_hotplug_event(dev);
1202 mutex_unlock(&aconnector->hpd_lock);
1206 static void dm_handle_hpd_rx_irq(struct amdgpu_dm_connector *aconnector)
1208 uint8_t esi[DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI] = { 0 };
1210 bool new_irq_handled = false;
1212 int dpcd_bytes_to_read;
1214 const int max_process_count = 30;
1215 int process_count = 0;
1217 const struct dc_link_status *link_status = dc_link_get_status(aconnector->dc_link);
1219 if (link_status->dpcd_caps->dpcd_rev.raw < 0x12) {
1220 dpcd_bytes_to_read = DP_LANE0_1_STATUS - DP_SINK_COUNT;
1221 /* DPCD 0x200 - 0x201 for downstream IRQ */
1222 dpcd_addr = DP_SINK_COUNT;
1224 dpcd_bytes_to_read = DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI;
1225 /* DPCD 0x2002 - 0x2005 for downstream IRQ */
1226 dpcd_addr = DP_SINK_COUNT_ESI;
1229 dret = drm_dp_dpcd_read(
1230 &aconnector->dm_dp_aux.aux,
1233 dpcd_bytes_to_read);
1235 while (dret == dpcd_bytes_to_read &&
1236 process_count < max_process_count) {
1242 DRM_DEBUG_DRIVER("ESI %02x %02x %02x\n", esi[0], esi[1], esi[2]);
1243 /* handle HPD short pulse irq */
1244 if (aconnector->mst_mgr.mst_state)
1246 &aconnector->mst_mgr,
1250 if (new_irq_handled) {
1251 /* ACK at DPCD to notify down stream */
1252 const int ack_dpcd_bytes_to_write =
1253 dpcd_bytes_to_read - 1;
1255 for (retry = 0; retry < 3; retry++) {
1258 wret = drm_dp_dpcd_write(
1259 &aconnector->dm_dp_aux.aux,
1262 ack_dpcd_bytes_to_write);
1263 if (wret == ack_dpcd_bytes_to_write)
1267 /* check if there is new irq to be handled */
1268 dret = drm_dp_dpcd_read(
1269 &aconnector->dm_dp_aux.aux,
1272 dpcd_bytes_to_read);
1274 new_irq_handled = false;
1280 if (process_count == max_process_count)
1281 DRM_DEBUG_DRIVER("Loop exceeded max iterations\n");
1284 static void handle_hpd_rx_irq(void *param)
1286 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
1287 struct drm_connector *connector = &aconnector->base;
1288 struct drm_device *dev = connector->dev;
1289 struct dc_link *dc_link = aconnector->dc_link;
1290 bool is_mst_root_connector = aconnector->mst_mgr.mst_state;
1291 enum dc_connection_type new_connection_type = dc_connection_none;
1294 * TODO:Temporary add mutex to protect hpd interrupt not have a gpio
1295 * conflict, after implement i2c helper, this mutex should be
1298 if (dc_link->type != dc_connection_mst_branch)
1299 mutex_lock(&aconnector->hpd_lock);
1301 if (dc_link_handle_hpd_rx_irq(dc_link, NULL, NULL) &&
1302 !is_mst_root_connector) {
1303 /* Downstream Port status changed. */
1304 if (!dc_link_detect_sink(dc_link, &new_connection_type))
1305 DRM_ERROR("KMS: Failed to detect connector\n");
1307 if (aconnector->base.force && new_connection_type == dc_connection_none) {
1308 emulated_link_detect(dc_link);
1310 if (aconnector->fake_enable)
1311 aconnector->fake_enable = false;
1313 amdgpu_dm_update_connector_after_detect(aconnector);
1316 drm_modeset_lock_all(dev);
1317 dm_restore_drm_connector_state(dev, connector);
1318 drm_modeset_unlock_all(dev);
1320 drm_kms_helper_hotplug_event(dev);
1321 } else if (dc_link_detect(dc_link, DETECT_REASON_HPDRX)) {
1323 if (aconnector->fake_enable)
1324 aconnector->fake_enable = false;
1326 amdgpu_dm_update_connector_after_detect(aconnector);
1329 drm_modeset_lock_all(dev);
1330 dm_restore_drm_connector_state(dev, connector);
1331 drm_modeset_unlock_all(dev);
1333 drm_kms_helper_hotplug_event(dev);
1336 if ((dc_link->cur_link_settings.lane_count != LANE_COUNT_UNKNOWN) ||
1337 (dc_link->type == dc_connection_mst_branch))
1338 dm_handle_hpd_rx_irq(aconnector);
1340 if (dc_link->type != dc_connection_mst_branch) {
1341 drm_dp_cec_irq(&aconnector->dm_dp_aux.aux);
1342 mutex_unlock(&aconnector->hpd_lock);
1346 static void register_hpd_handlers(struct amdgpu_device *adev)
1348 struct drm_device *dev = adev->ddev;
1349 struct drm_connector *connector;
1350 struct amdgpu_dm_connector *aconnector;
1351 const struct dc_link *dc_link;
1352 struct dc_interrupt_params int_params = {0};
1354 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
1355 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
1357 list_for_each_entry(connector,
1358 &dev->mode_config.connector_list, head) {
1360 aconnector = to_amdgpu_dm_connector(connector);
1361 dc_link = aconnector->dc_link;
1363 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd) {
1364 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
1365 int_params.irq_source = dc_link->irq_source_hpd;
1367 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1369 (void *) aconnector);
1372 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd_rx) {
1374 /* Also register for DP short pulse (hpd_rx). */
1375 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
1376 int_params.irq_source = dc_link->irq_source_hpd_rx;
1378 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1380 (void *) aconnector);
1385 /* Register IRQ sources and initialize IRQ callbacks */
1386 static int dce110_register_irq_handlers(struct amdgpu_device *adev)
1388 struct dc *dc = adev->dm.dc;
1389 struct common_irq_params *c_irq_params;
1390 struct dc_interrupt_params int_params = {0};
1393 unsigned client_id = AMDGPU_IRQ_CLIENTID_LEGACY;
1395 if (adev->asic_type == CHIP_VEGA10 ||
1396 adev->asic_type == CHIP_VEGA12 ||
1397 adev->asic_type == CHIP_VEGA20 ||
1398 adev->asic_type == CHIP_RAVEN)
1399 client_id = SOC15_IH_CLIENTID_DCE;
1401 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
1402 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
1405 * Actions of amdgpu_irq_add_id():
1406 * 1. Register a set() function with base driver.
1407 * Base driver will call set() function to enable/disable an
1408 * interrupt in DC hardware.
1409 * 2. Register amdgpu_dm_irq_handler().
1410 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
1411 * coming from DC hardware.
1412 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
1413 * for acknowledging and handling. */
1415 /* Use VBLANK interrupt */
1416 for (i = VISLANDS30_IV_SRCID_D1_VERTICAL_INTERRUPT0; i <= VISLANDS30_IV_SRCID_D6_VERTICAL_INTERRUPT0; i++) {
1417 r = amdgpu_irq_add_id(adev, client_id, i, &adev->crtc_irq);
1419 DRM_ERROR("Failed to add crtc irq id!\n");
1423 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1424 int_params.irq_source =
1425 dc_interrupt_to_irq_source(dc, i, 0);
1427 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
1429 c_irq_params->adev = adev;
1430 c_irq_params->irq_src = int_params.irq_source;
1432 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1433 dm_crtc_high_irq, c_irq_params);
1436 /* Use GRPH_PFLIP interrupt */
1437 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
1438 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
1439 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq);
1441 DRM_ERROR("Failed to add page flip irq id!\n");
1445 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1446 int_params.irq_source =
1447 dc_interrupt_to_irq_source(dc, i, 0);
1449 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
1451 c_irq_params->adev = adev;
1452 c_irq_params->irq_src = int_params.irq_source;
1454 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1455 dm_pflip_high_irq, c_irq_params);
1460 r = amdgpu_irq_add_id(adev, client_id,
1461 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
1463 DRM_ERROR("Failed to add hpd irq id!\n");
1467 register_hpd_handlers(adev);
1472 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
1473 /* Register IRQ sources and initialize IRQ callbacks */
1474 static int dcn10_register_irq_handlers(struct amdgpu_device *adev)
1476 struct dc *dc = adev->dm.dc;
1477 struct common_irq_params *c_irq_params;
1478 struct dc_interrupt_params int_params = {0};
1482 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
1483 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
1486 * Actions of amdgpu_irq_add_id():
1487 * 1. Register a set() function with base driver.
1488 * Base driver will call set() function to enable/disable an
1489 * interrupt in DC hardware.
1490 * 2. Register amdgpu_dm_irq_handler().
1491 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
1492 * coming from DC hardware.
1493 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
1494 * for acknowledging and handling.
1497 /* Use VSTARTUP interrupt */
1498 for (i = DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP;
1499 i <= DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP + adev->mode_info.num_crtc - 1;
1501 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->crtc_irq);
1504 DRM_ERROR("Failed to add crtc irq id!\n");
1508 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1509 int_params.irq_source =
1510 dc_interrupt_to_irq_source(dc, i, 0);
1512 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
1514 c_irq_params->adev = adev;
1515 c_irq_params->irq_src = int_params.irq_source;
1517 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1518 dm_crtc_high_irq, c_irq_params);
1521 /* Use GRPH_PFLIP interrupt */
1522 for (i = DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT;
1523 i <= DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT + adev->mode_info.num_crtc - 1;
1525 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->pageflip_irq);
1527 DRM_ERROR("Failed to add page flip irq id!\n");
1531 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1532 int_params.irq_source =
1533 dc_interrupt_to_irq_source(dc, i, 0);
1535 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
1537 c_irq_params->adev = adev;
1538 c_irq_params->irq_src = int_params.irq_source;
1540 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1541 dm_pflip_high_irq, c_irq_params);
1546 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DC_HPD1_INT,
1549 DRM_ERROR("Failed to add hpd irq id!\n");
1553 register_hpd_handlers(adev);
1560 * Acquires the lock for the atomic state object and returns
1561 * the new atomic state.
1563 * This should only be called during atomic check.
1565 static int dm_atomic_get_state(struct drm_atomic_state *state,
1566 struct dm_atomic_state **dm_state)
1568 struct drm_device *dev = state->dev;
1569 struct amdgpu_device *adev = dev->dev_private;
1570 struct amdgpu_display_manager *dm = &adev->dm;
1571 struct drm_private_state *priv_state;
1576 priv_state = drm_atomic_get_private_obj_state(state, &dm->atomic_obj);
1577 if (IS_ERR(priv_state))
1578 return PTR_ERR(priv_state);
1580 *dm_state = to_dm_atomic_state(priv_state);
1585 struct dm_atomic_state *
1586 dm_atomic_get_new_state(struct drm_atomic_state *state)
1588 struct drm_device *dev = state->dev;
1589 struct amdgpu_device *adev = dev->dev_private;
1590 struct amdgpu_display_manager *dm = &adev->dm;
1591 struct drm_private_obj *obj;
1592 struct drm_private_state *new_obj_state;
1595 for_each_new_private_obj_in_state(state, obj, new_obj_state, i) {
1596 if (obj->funcs == dm->atomic_obj.funcs)
1597 return to_dm_atomic_state(new_obj_state);
1603 struct dm_atomic_state *
1604 dm_atomic_get_old_state(struct drm_atomic_state *state)
1606 struct drm_device *dev = state->dev;
1607 struct amdgpu_device *adev = dev->dev_private;
1608 struct amdgpu_display_manager *dm = &adev->dm;
1609 struct drm_private_obj *obj;
1610 struct drm_private_state *old_obj_state;
1613 for_each_old_private_obj_in_state(state, obj, old_obj_state, i) {
1614 if (obj->funcs == dm->atomic_obj.funcs)
1615 return to_dm_atomic_state(old_obj_state);
1621 static struct drm_private_state *
1622 dm_atomic_duplicate_state(struct drm_private_obj *obj)
1624 struct dm_atomic_state *old_state, *new_state;
1626 new_state = kzalloc(sizeof(*new_state), GFP_KERNEL);
1630 __drm_atomic_helper_private_obj_duplicate_state(obj, &new_state->base);
1632 new_state->context = dc_create_state();
1633 if (!new_state->context) {
1638 old_state = to_dm_atomic_state(obj->state);
1639 if (old_state && old_state->context)
1640 dc_resource_state_copy_construct(old_state->context,
1641 new_state->context);
1643 return &new_state->base;
1646 static void dm_atomic_destroy_state(struct drm_private_obj *obj,
1647 struct drm_private_state *state)
1649 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
1651 if (dm_state && dm_state->context)
1652 dc_release_state(dm_state->context);
1657 static struct drm_private_state_funcs dm_atomic_state_funcs = {
1658 .atomic_duplicate_state = dm_atomic_duplicate_state,
1659 .atomic_destroy_state = dm_atomic_destroy_state,
1662 static int amdgpu_dm_mode_config_init(struct amdgpu_device *adev)
1664 struct dm_atomic_state *state;
1667 adev->mode_info.mode_config_initialized = true;
1669 adev->ddev->mode_config.funcs = (void *)&amdgpu_dm_mode_funcs;
1670 adev->ddev->mode_config.helper_private = &amdgpu_dm_mode_config_helperfuncs;
1672 adev->ddev->mode_config.max_width = 16384;
1673 adev->ddev->mode_config.max_height = 16384;
1675 adev->ddev->mode_config.preferred_depth = 24;
1676 adev->ddev->mode_config.prefer_shadow = 1;
1677 /* indicates support for immediate flip */
1678 adev->ddev->mode_config.async_page_flip = true;
1680 adev->ddev->mode_config.fb_base = adev->gmc.aper_base;
1682 state = kzalloc(sizeof(*state), GFP_KERNEL);
1686 state->context = dc_create_state();
1687 if (!state->context) {
1692 dc_resource_state_copy_construct_current(adev->dm.dc, state->context);
1694 drm_atomic_private_obj_init(adev->ddev,
1695 &adev->dm.atomic_obj,
1697 &dm_atomic_state_funcs);
1699 r = amdgpu_display_modeset_create_props(adev);
1706 #define AMDGPU_DM_DEFAULT_MIN_BACKLIGHT 12
1707 #define AMDGPU_DM_DEFAULT_MAX_BACKLIGHT 255
1709 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
1710 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
1712 static void amdgpu_dm_update_backlight_caps(struct amdgpu_display_manager *dm)
1714 #if defined(CONFIG_ACPI)
1715 struct amdgpu_dm_backlight_caps caps;
1717 if (dm->backlight_caps.caps_valid)
1720 amdgpu_acpi_get_backlight_caps(dm->adev, &caps);
1721 if (caps.caps_valid) {
1722 dm->backlight_caps.min_input_signal = caps.min_input_signal;
1723 dm->backlight_caps.max_input_signal = caps.max_input_signal;
1724 dm->backlight_caps.caps_valid = true;
1726 dm->backlight_caps.min_input_signal =
1727 AMDGPU_DM_DEFAULT_MIN_BACKLIGHT;
1728 dm->backlight_caps.max_input_signal =
1729 AMDGPU_DM_DEFAULT_MAX_BACKLIGHT;
1732 dm->backlight_caps.min_input_signal = AMDGPU_DM_DEFAULT_MIN_BACKLIGHT;
1733 dm->backlight_caps.max_input_signal = AMDGPU_DM_DEFAULT_MAX_BACKLIGHT;
1737 static int amdgpu_dm_backlight_update_status(struct backlight_device *bd)
1739 struct amdgpu_display_manager *dm = bl_get_data(bd);
1740 struct amdgpu_dm_backlight_caps caps;
1741 uint32_t brightness = bd->props.brightness;
1743 amdgpu_dm_update_backlight_caps(dm);
1744 caps = dm->backlight_caps;
1746 * The brightness input is in the range 0-255
1747 * It needs to be rescaled to be between the
1748 * requested min and max input signal
1750 * It also needs to be scaled up by 0x101 to
1751 * match the DC interface which has a range of
1757 * (caps.max_input_signal - caps.min_input_signal)
1758 / AMDGPU_MAX_BL_LEVEL
1759 + caps.min_input_signal * 0x101;
1761 if (dc_link_set_backlight_level(dm->backlight_link,
1768 static int amdgpu_dm_backlight_get_brightness(struct backlight_device *bd)
1770 struct amdgpu_display_manager *dm = bl_get_data(bd);
1771 int ret = dc_link_get_backlight_level(dm->backlight_link);
1773 if (ret == DC_ERROR_UNEXPECTED)
1774 return bd->props.brightness;
1778 static const struct backlight_ops amdgpu_dm_backlight_ops = {
1779 .get_brightness = amdgpu_dm_backlight_get_brightness,
1780 .update_status = amdgpu_dm_backlight_update_status,
1784 amdgpu_dm_register_backlight_device(struct amdgpu_display_manager *dm)
1787 struct backlight_properties props = { 0 };
1789 amdgpu_dm_update_backlight_caps(dm);
1791 props.max_brightness = AMDGPU_MAX_BL_LEVEL;
1792 props.brightness = AMDGPU_MAX_BL_LEVEL;
1793 props.type = BACKLIGHT_RAW;
1795 snprintf(bl_name, sizeof(bl_name), "amdgpu_bl%d",
1796 dm->adev->ddev->primary->index);
1798 dm->backlight_dev = backlight_device_register(bl_name,
1799 dm->adev->ddev->dev,
1801 &amdgpu_dm_backlight_ops,
1804 if (IS_ERR(dm->backlight_dev))
1805 DRM_ERROR("DM: Backlight registration failed!\n");
1807 DRM_DEBUG_DRIVER("DM: Registered Backlight device: %s\n", bl_name);
1812 static int initialize_plane(struct amdgpu_display_manager *dm,
1813 struct amdgpu_mode_info *mode_info, int plane_id,
1814 enum drm_plane_type plane_type)
1816 struct drm_plane *plane;
1817 unsigned long possible_crtcs;
1820 plane = kzalloc(sizeof(struct drm_plane), GFP_KERNEL);
1822 DRM_ERROR("KMS: Failed to allocate plane\n");
1825 plane->type = plane_type;
1828 * HACK: IGT tests expect that the primary plane for a CRTC
1829 * can only have one possible CRTC. Only expose support for
1830 * any CRTC if they're not going to be used as a primary plane
1831 * for a CRTC - like overlay or underlay planes.
1833 possible_crtcs = 1 << plane_id;
1834 if (plane_id >= dm->dc->caps.max_streams)
1835 possible_crtcs = 0xff;
1837 ret = amdgpu_dm_plane_init(dm, plane, possible_crtcs);
1840 DRM_ERROR("KMS: Failed to initialize plane\n");
1846 mode_info->planes[plane_id] = plane;
1852 static void register_backlight_device(struct amdgpu_display_manager *dm,
1853 struct dc_link *link)
1855 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
1856 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
1858 if ((link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) &&
1859 link->type != dc_connection_none) {
1861 * Event if registration failed, we should continue with
1862 * DM initialization because not having a backlight control
1863 * is better then a black screen.
1865 amdgpu_dm_register_backlight_device(dm);
1867 if (dm->backlight_dev)
1868 dm->backlight_link = link;
1875 * In this architecture, the association
1876 * connector -> encoder -> crtc
1877 * id not really requried. The crtc and connector will hold the
1878 * display_index as an abstraction to use with DAL component
1880 * Returns 0 on success
1882 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev)
1884 struct amdgpu_display_manager *dm = &adev->dm;
1886 struct amdgpu_dm_connector *aconnector = NULL;
1887 struct amdgpu_encoder *aencoder = NULL;
1888 struct amdgpu_mode_info *mode_info = &adev->mode_info;
1890 int32_t overlay_planes, primary_planes;
1891 enum dc_connection_type new_connection_type = dc_connection_none;
1893 link_cnt = dm->dc->caps.max_links;
1894 if (amdgpu_dm_mode_config_init(dm->adev)) {
1895 DRM_ERROR("DM: Failed to initialize mode config\n");
1900 * Determine the number of overlay planes supported.
1901 * Only support DCN for now, and cap so we don't encourage
1902 * userspace to use up all the planes.
1906 for (i = 0; i < dm->dc->caps.max_planes; ++i) {
1907 struct dc_plane_cap *plane = &dm->dc->caps.planes[i];
1909 if (plane->type == DC_PLANE_TYPE_DCN_UNIVERSAL &&
1910 plane->blends_with_above && plane->blends_with_below &&
1911 plane->supports_argb8888)
1912 overlay_planes += 1;
1915 overlay_planes = min(overlay_planes, 1);
1917 /* There is one primary plane per CRTC */
1918 primary_planes = dm->dc->caps.max_streams;
1919 ASSERT(primary_planes <= AMDGPU_MAX_PLANES);
1922 * Initialize primary planes, implicit planes for legacy IOCTLS.
1923 * Order is reversed to match iteration order in atomic check.
1925 for (i = (primary_planes - 1); i >= 0; i--) {
1926 if (initialize_plane(dm, mode_info, i,
1927 DRM_PLANE_TYPE_PRIMARY)) {
1928 DRM_ERROR("KMS: Failed to initialize primary plane\n");
1934 * Initialize overlay planes, index starting after primary planes.
1935 * These planes have a higher DRM index than the primary planes since
1936 * they should be considered as having a higher z-order.
1937 * Order is reversed to match iteration order in atomic check.
1939 for (i = (overlay_planes - 1); i >= 0; i--) {
1940 if (initialize_plane(dm, NULL, primary_planes + i,
1941 DRM_PLANE_TYPE_OVERLAY)) {
1942 DRM_ERROR("KMS: Failed to initialize overlay plane\n");
1947 for (i = 0; i < dm->dc->caps.max_streams; i++)
1948 if (amdgpu_dm_crtc_init(dm, mode_info->planes[i], i)) {
1949 DRM_ERROR("KMS: Failed to initialize crtc\n");
1953 dm->display_indexes_num = dm->dc->caps.max_streams;
1955 /* loops over all connectors on the board */
1956 for (i = 0; i < link_cnt; i++) {
1957 struct dc_link *link = NULL;
1959 if (i > AMDGPU_DM_MAX_DISPLAY_INDEX) {
1961 "KMS: Cannot support more than %d display indexes\n",
1962 AMDGPU_DM_MAX_DISPLAY_INDEX);
1966 aconnector = kzalloc(sizeof(*aconnector), GFP_KERNEL);
1970 aencoder = kzalloc(sizeof(*aencoder), GFP_KERNEL);
1974 if (amdgpu_dm_encoder_init(dm->ddev, aencoder, i)) {
1975 DRM_ERROR("KMS: Failed to initialize encoder\n");
1979 if (amdgpu_dm_connector_init(dm, aconnector, i, aencoder)) {
1980 DRM_ERROR("KMS: Failed to initialize connector\n");
1984 link = dc_get_link_at_index(dm->dc, i);
1986 if (!dc_link_detect_sink(link, &new_connection_type))
1987 DRM_ERROR("KMS: Failed to detect connector\n");
1989 if (aconnector->base.force && new_connection_type == dc_connection_none) {
1990 emulated_link_detect(link);
1991 amdgpu_dm_update_connector_after_detect(aconnector);
1993 } else if (dc_link_detect(link, DETECT_REASON_BOOT)) {
1994 amdgpu_dm_update_connector_after_detect(aconnector);
1995 register_backlight_device(dm, link);
2001 /* Software is initialized. Now we can register interrupt handlers. */
2002 switch (adev->asic_type) {
2012 case CHIP_POLARIS11:
2013 case CHIP_POLARIS10:
2014 case CHIP_POLARIS12:
2019 if (dce110_register_irq_handlers(dm->adev)) {
2020 DRM_ERROR("DM: Failed to initialize IRQ\n");
2024 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
2026 if (dcn10_register_irq_handlers(dm->adev)) {
2027 DRM_ERROR("DM: Failed to initialize IRQ\n");
2033 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type);
2037 if (adev->asic_type != CHIP_CARRIZO && adev->asic_type != CHIP_STONEY)
2038 dm->dc->debug.disable_stutter = amdgpu_pp_feature_mask & PP_STUTTER_MODE ? false : true;
2048 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm)
2050 drm_mode_config_cleanup(dm->ddev);
2051 drm_atomic_private_obj_fini(&dm->atomic_obj);
2055 /******************************************************************************
2056 * amdgpu_display_funcs functions
2057 *****************************************************************************/
2060 * dm_bandwidth_update - program display watermarks
2062 * @adev: amdgpu_device pointer
2064 * Calculate and program the display watermarks and line buffer allocation.
2066 static void dm_bandwidth_update(struct amdgpu_device *adev)
2068 /* TODO: implement later */
2071 static const struct amdgpu_display_funcs dm_display_funcs = {
2072 .bandwidth_update = dm_bandwidth_update, /* called unconditionally */
2073 .vblank_get_counter = dm_vblank_get_counter,/* called unconditionally */
2074 .backlight_set_level = NULL, /* never called for DC */
2075 .backlight_get_level = NULL, /* never called for DC */
2076 .hpd_sense = NULL,/* called unconditionally */
2077 .hpd_set_polarity = NULL, /* called unconditionally */
2078 .hpd_get_gpio_reg = NULL, /* VBIOS parsing. DAL does it. */
2079 .page_flip_get_scanoutpos =
2080 dm_crtc_get_scanoutpos,/* called unconditionally */
2081 .add_encoder = NULL, /* VBIOS parsing. DAL does it. */
2082 .add_connector = NULL, /* VBIOS parsing. DAL does it. */
2085 #if defined(CONFIG_DEBUG_KERNEL_DC)
2087 static ssize_t s3_debug_store(struct device *device,
2088 struct device_attribute *attr,
2094 struct pci_dev *pdev = to_pci_dev(device);
2095 struct drm_device *drm_dev = pci_get_drvdata(pdev);
2096 struct amdgpu_device *adev = drm_dev->dev_private;
2098 ret = kstrtoint(buf, 0, &s3_state);
2103 drm_kms_helper_hotplug_event(adev->ddev);
2108 return ret == 0 ? count : 0;
2111 DEVICE_ATTR_WO(s3_debug);
2115 static int dm_early_init(void *handle)
2117 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2119 switch (adev->asic_type) {
2122 adev->mode_info.num_crtc = 6;
2123 adev->mode_info.num_hpd = 6;
2124 adev->mode_info.num_dig = 6;
2127 adev->mode_info.num_crtc = 4;
2128 adev->mode_info.num_hpd = 6;
2129 adev->mode_info.num_dig = 7;
2133 adev->mode_info.num_crtc = 2;
2134 adev->mode_info.num_hpd = 6;
2135 adev->mode_info.num_dig = 6;
2139 adev->mode_info.num_crtc = 6;
2140 adev->mode_info.num_hpd = 6;
2141 adev->mode_info.num_dig = 7;
2144 adev->mode_info.num_crtc = 3;
2145 adev->mode_info.num_hpd = 6;
2146 adev->mode_info.num_dig = 9;
2149 adev->mode_info.num_crtc = 2;
2150 adev->mode_info.num_hpd = 6;
2151 adev->mode_info.num_dig = 9;
2153 case CHIP_POLARIS11:
2154 case CHIP_POLARIS12:
2155 adev->mode_info.num_crtc = 5;
2156 adev->mode_info.num_hpd = 5;
2157 adev->mode_info.num_dig = 5;
2159 case CHIP_POLARIS10:
2161 adev->mode_info.num_crtc = 6;
2162 adev->mode_info.num_hpd = 6;
2163 adev->mode_info.num_dig = 6;
2168 adev->mode_info.num_crtc = 6;
2169 adev->mode_info.num_hpd = 6;
2170 adev->mode_info.num_dig = 6;
2172 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
2174 adev->mode_info.num_crtc = 4;
2175 adev->mode_info.num_hpd = 4;
2176 adev->mode_info.num_dig = 4;
2180 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type);
2184 amdgpu_dm_set_irq_funcs(adev);
2186 if (adev->mode_info.funcs == NULL)
2187 adev->mode_info.funcs = &dm_display_funcs;
2190 * Note: Do NOT change adev->audio_endpt_rreg and
2191 * adev->audio_endpt_wreg because they are initialised in
2192 * amdgpu_device_init()
2194 #if defined(CONFIG_DEBUG_KERNEL_DC)
2197 &dev_attr_s3_debug);
2203 static bool modeset_required(struct drm_crtc_state *crtc_state,
2204 struct dc_stream_state *new_stream,
2205 struct dc_stream_state *old_stream)
2207 if (!drm_atomic_crtc_needs_modeset(crtc_state))
2210 if (!crtc_state->enable)
2213 return crtc_state->active;
2216 static bool modereset_required(struct drm_crtc_state *crtc_state)
2218 if (!drm_atomic_crtc_needs_modeset(crtc_state))
2221 return !crtc_state->enable || !crtc_state->active;
2224 static void amdgpu_dm_encoder_destroy(struct drm_encoder *encoder)
2226 drm_encoder_cleanup(encoder);
2230 static const struct drm_encoder_funcs amdgpu_dm_encoder_funcs = {
2231 .destroy = amdgpu_dm_encoder_destroy,
2234 static bool fill_rects_from_plane_state(const struct drm_plane_state *state,
2235 struct dc_plane_state *plane_state)
2237 plane_state->src_rect.x = state->src_x >> 16;
2238 plane_state->src_rect.y = state->src_y >> 16;
2239 /* we ignore the mantissa for now and do not deal with floating pixels :( */
2240 plane_state->src_rect.width = state->src_w >> 16;
2242 if (plane_state->src_rect.width == 0)
2245 plane_state->src_rect.height = state->src_h >> 16;
2246 if (plane_state->src_rect.height == 0)
2249 plane_state->dst_rect.x = state->crtc_x;
2250 plane_state->dst_rect.y = state->crtc_y;
2252 if (state->crtc_w == 0)
2255 plane_state->dst_rect.width = state->crtc_w;
2257 if (state->crtc_h == 0)
2260 plane_state->dst_rect.height = state->crtc_h;
2262 plane_state->clip_rect = plane_state->dst_rect;
2264 switch (state->rotation & DRM_MODE_ROTATE_MASK) {
2265 case DRM_MODE_ROTATE_0:
2266 plane_state->rotation = ROTATION_ANGLE_0;
2268 case DRM_MODE_ROTATE_90:
2269 plane_state->rotation = ROTATION_ANGLE_90;
2271 case DRM_MODE_ROTATE_180:
2272 plane_state->rotation = ROTATION_ANGLE_180;
2274 case DRM_MODE_ROTATE_270:
2275 plane_state->rotation = ROTATION_ANGLE_270;
2278 plane_state->rotation = ROTATION_ANGLE_0;
2284 static int get_fb_info(const struct amdgpu_framebuffer *amdgpu_fb,
2285 uint64_t *tiling_flags)
2287 struct amdgpu_bo *rbo = gem_to_amdgpu_bo(amdgpu_fb->base.obj[0]);
2288 int r = amdgpu_bo_reserve(rbo, false);
2291 /* Don't show error message when returning -ERESTARTSYS */
2292 if (r != -ERESTARTSYS)
2293 DRM_ERROR("Unable to reserve buffer: %d\n", r);
2298 amdgpu_bo_get_tiling_flags(rbo, tiling_flags);
2300 amdgpu_bo_unreserve(rbo);
2305 static inline uint64_t get_dcc_address(uint64_t address, uint64_t tiling_flags)
2307 uint32_t offset = AMDGPU_TILING_GET(tiling_flags, DCC_OFFSET_256B);
2309 return offset ? (address + offset * 256) : 0;
2312 static int fill_plane_dcc_attributes(struct amdgpu_device *adev,
2313 const struct amdgpu_framebuffer *afb,
2314 const struct dc_plane_state *plane_state,
2315 struct dc_plane_dcc_param *dcc,
2316 struct dc_plane_address *address,
2319 struct dc *dc = adev->dm.dc;
2320 struct dc_dcc_surface_param input;
2321 struct dc_surface_dcc_cap output;
2322 uint32_t offset = AMDGPU_TILING_GET(info, DCC_OFFSET_256B);
2323 uint32_t i64b = AMDGPU_TILING_GET(info, DCC_INDEPENDENT_64B) != 0;
2324 uint64_t dcc_address;
2326 memset(&input, 0, sizeof(input));
2327 memset(&output, 0, sizeof(output));
2332 if (plane_state->address.type != PLN_ADDR_TYPE_GRAPHICS)
2335 if (!dc->cap_funcs.get_dcc_compression_cap)
2338 input.format = plane_state->format;
2339 input.surface_size.width =
2340 plane_state->plane_size.grph.surface_size.width;
2341 input.surface_size.height =
2342 plane_state->plane_size.grph.surface_size.height;
2343 input.swizzle_mode = plane_state->tiling_info.gfx9.swizzle;
2345 if (plane_state->rotation == ROTATION_ANGLE_0 ||
2346 plane_state->rotation == ROTATION_ANGLE_180)
2347 input.scan = SCAN_DIRECTION_HORIZONTAL;
2348 else if (plane_state->rotation == ROTATION_ANGLE_90 ||
2349 plane_state->rotation == ROTATION_ANGLE_270)
2350 input.scan = SCAN_DIRECTION_VERTICAL;
2352 if (!dc->cap_funcs.get_dcc_compression_cap(dc, &input, &output))
2355 if (!output.capable)
2358 if (i64b == 0 && output.grph.rgb.independent_64b_blks != 0)
2362 dcc->grph.meta_pitch =
2363 AMDGPU_TILING_GET(info, DCC_PITCH_MAX) + 1;
2364 dcc->grph.independent_64b_blks = i64b;
2366 dcc_address = get_dcc_address(afb->address, info);
2367 address->grph.meta_addr.low_part = lower_32_bits(dcc_address);
2368 address->grph.meta_addr.high_part = upper_32_bits(dcc_address);
2374 fill_plane_tiling_attributes(struct amdgpu_device *adev,
2375 const struct amdgpu_framebuffer *afb,
2376 const struct dc_plane_state *plane_state,
2377 union dc_tiling_info *tiling_info,
2378 struct dc_plane_dcc_param *dcc,
2379 struct dc_plane_address *address,
2380 uint64_t tiling_flags)
2384 memset(tiling_info, 0, sizeof(*tiling_info));
2385 memset(dcc, 0, sizeof(*dcc));
2386 memset(address, 0, sizeof(*address));
2388 if (plane_state->format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) {
2389 address->type = PLN_ADDR_TYPE_GRAPHICS;
2390 address->grph.addr.low_part = lower_32_bits(afb->address);
2391 address->grph.addr.high_part = upper_32_bits(afb->address);
2393 const struct drm_framebuffer *fb = &afb->base;
2394 uint64_t chroma_addr = afb->address + fb->offsets[1];
2396 address->type = PLN_ADDR_TYPE_VIDEO_PROGRESSIVE;
2397 address->video_progressive.luma_addr.low_part =
2398 lower_32_bits(afb->address);
2399 address->video_progressive.luma_addr.high_part =
2400 upper_32_bits(afb->address);
2401 address->video_progressive.chroma_addr.low_part =
2402 lower_32_bits(chroma_addr);
2403 address->video_progressive.chroma_addr.high_part =
2404 upper_32_bits(chroma_addr);
2407 /* Fill GFX8 params */
2408 if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == DC_ARRAY_2D_TILED_THIN1) {
2409 unsigned int bankw, bankh, mtaspect, tile_split, num_banks;
2411 bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
2412 bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
2413 mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
2414 tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT);
2415 num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
2417 /* XXX fix me for VI */
2418 tiling_info->gfx8.num_banks = num_banks;
2419 tiling_info->gfx8.array_mode =
2420 DC_ARRAY_2D_TILED_THIN1;
2421 tiling_info->gfx8.tile_split = tile_split;
2422 tiling_info->gfx8.bank_width = bankw;
2423 tiling_info->gfx8.bank_height = bankh;
2424 tiling_info->gfx8.tile_aspect = mtaspect;
2425 tiling_info->gfx8.tile_mode =
2426 DC_ADDR_SURF_MICRO_TILING_DISPLAY;
2427 } else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE)
2428 == DC_ARRAY_1D_TILED_THIN1) {
2429 tiling_info->gfx8.array_mode = DC_ARRAY_1D_TILED_THIN1;
2432 tiling_info->gfx8.pipe_config =
2433 AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
2435 if (adev->asic_type == CHIP_VEGA10 ||
2436 adev->asic_type == CHIP_VEGA12 ||
2437 adev->asic_type == CHIP_VEGA20 ||
2438 adev->asic_type == CHIP_RAVEN) {
2439 /* Fill GFX9 params */
2440 tiling_info->gfx9.num_pipes =
2441 adev->gfx.config.gb_addr_config_fields.num_pipes;
2442 tiling_info->gfx9.num_banks =
2443 adev->gfx.config.gb_addr_config_fields.num_banks;
2444 tiling_info->gfx9.pipe_interleave =
2445 adev->gfx.config.gb_addr_config_fields.pipe_interleave_size;
2446 tiling_info->gfx9.num_shader_engines =
2447 adev->gfx.config.gb_addr_config_fields.num_se;
2448 tiling_info->gfx9.max_compressed_frags =
2449 adev->gfx.config.gb_addr_config_fields.max_compress_frags;
2450 tiling_info->gfx9.num_rb_per_se =
2451 adev->gfx.config.gb_addr_config_fields.num_rb_per_se;
2452 tiling_info->gfx9.swizzle =
2453 AMDGPU_TILING_GET(tiling_flags, SWIZZLE_MODE);
2454 tiling_info->gfx9.shaderEnable = 1;
2456 ret = fill_plane_dcc_attributes(adev, afb, plane_state, dcc,
2457 address, tiling_flags);
2465 static int fill_plane_attributes_from_fb(struct amdgpu_device *adev,
2466 struct dc_plane_state *plane_state,
2467 const struct amdgpu_framebuffer *amdgpu_fb)
2469 uint64_t tiling_flags;
2470 const struct drm_framebuffer *fb = &amdgpu_fb->base;
2472 struct drm_format_name_buf format_name;
2481 switch (fb->format->format) {
2483 plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS;
2485 case DRM_FORMAT_RGB565:
2486 plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_RGB565;
2488 case DRM_FORMAT_XRGB8888:
2489 case DRM_FORMAT_ARGB8888:
2490 plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888;
2492 case DRM_FORMAT_XRGB2101010:
2493 case DRM_FORMAT_ARGB2101010:
2494 plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010;
2496 case DRM_FORMAT_XBGR2101010:
2497 case DRM_FORMAT_ABGR2101010:
2498 plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010;
2500 case DRM_FORMAT_XBGR8888:
2501 case DRM_FORMAT_ABGR8888:
2502 plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR8888;
2504 case DRM_FORMAT_NV21:
2505 plane_state->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr;
2507 case DRM_FORMAT_NV12:
2508 plane_state->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb;
2511 DRM_ERROR("Unsupported screen format %s\n",
2512 drm_get_format_name(fb->format->format, &format_name));
2516 memset(&plane_state->address, 0, sizeof(plane_state->address));
2518 if (plane_state->format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) {
2519 plane_state->plane_size.grph.surface_size.x = 0;
2520 plane_state->plane_size.grph.surface_size.y = 0;
2521 plane_state->plane_size.grph.surface_size.width = fb->width;
2522 plane_state->plane_size.grph.surface_size.height = fb->height;
2523 plane_state->plane_size.grph.surface_pitch =
2524 fb->pitches[0] / fb->format->cpp[0];
2525 /* TODO: unhardcode */
2526 plane_state->color_space = COLOR_SPACE_SRGB;
2529 plane_state->plane_size.video.luma_size.x = 0;
2530 plane_state->plane_size.video.luma_size.y = 0;
2531 plane_state->plane_size.video.luma_size.width = fb->width;
2532 plane_state->plane_size.video.luma_size.height = fb->height;
2533 plane_state->plane_size.video.luma_pitch =
2534 fb->pitches[0] / fb->format->cpp[0];
2536 plane_state->plane_size.video.chroma_size.x = 0;
2537 plane_state->plane_size.video.chroma_size.y = 0;
2538 /* TODO: set these based on surface format */
2539 plane_state->plane_size.video.chroma_size.width = fb->width / 2;
2540 plane_state->plane_size.video.chroma_size.height = fb->height / 2;
2542 plane_state->plane_size.video.chroma_pitch =
2543 fb->pitches[1] / fb->format->cpp[1];
2545 /* TODO: unhardcode */
2546 plane_state->color_space = COLOR_SPACE_YCBCR709;
2549 fill_plane_tiling_attributes(adev, amdgpu_fb, plane_state,
2550 &plane_state->tiling_info,
2552 &plane_state->address,
2555 plane_state->visible = true;
2556 plane_state->scaling_quality.h_taps_c = 0;
2557 plane_state->scaling_quality.v_taps_c = 0;
2559 /* is this needed? is plane_state zeroed at allocation? */
2560 plane_state->scaling_quality.h_taps = 0;
2561 plane_state->scaling_quality.v_taps = 0;
2562 plane_state->stereo_format = PLANE_STEREO_FORMAT_NONE;
2569 fill_blending_from_plane_state(struct drm_plane_state *plane_state,
2570 const struct dc_plane_state *dc_plane_state,
2571 bool *per_pixel_alpha, bool *global_alpha,
2572 int *global_alpha_value)
2574 *per_pixel_alpha = false;
2575 *global_alpha = false;
2576 *global_alpha_value = 0xff;
2578 if (plane_state->plane->type != DRM_PLANE_TYPE_OVERLAY)
2581 if (plane_state->pixel_blend_mode == DRM_MODE_BLEND_PREMULTI) {
2582 static const uint32_t alpha_formats[] = {
2583 DRM_FORMAT_ARGB8888,
2584 DRM_FORMAT_RGBA8888,
2585 DRM_FORMAT_ABGR8888,
2587 uint32_t format = plane_state->fb->format->format;
2590 for (i = 0; i < ARRAY_SIZE(alpha_formats); ++i) {
2591 if (format == alpha_formats[i]) {
2592 *per_pixel_alpha = true;
2598 if (plane_state->alpha < 0xffff) {
2599 *global_alpha = true;
2600 *global_alpha_value = plane_state->alpha >> 8;
2604 static int fill_plane_attributes(struct amdgpu_device *adev,
2605 struct dc_plane_state *dc_plane_state,
2606 struct drm_plane_state *plane_state,
2607 struct drm_crtc_state *crtc_state)
2609 const struct amdgpu_framebuffer *amdgpu_fb =
2610 to_amdgpu_framebuffer(plane_state->fb);
2611 const struct drm_crtc *crtc = plane_state->crtc;
2614 if (!fill_rects_from_plane_state(plane_state, dc_plane_state))
2617 ret = fill_plane_attributes_from_fb(
2618 crtc->dev->dev_private,
2626 * Always set input transfer function, since plane state is refreshed
2629 ret = amdgpu_dm_set_degamma_lut(crtc_state, dc_plane_state);
2631 dc_transfer_func_release(dc_plane_state->in_transfer_func);
2632 dc_plane_state->in_transfer_func = NULL;
2635 fill_blending_from_plane_state(plane_state, dc_plane_state,
2636 &dc_plane_state->per_pixel_alpha,
2637 &dc_plane_state->global_alpha,
2638 &dc_plane_state->global_alpha_value);
2643 static void update_stream_scaling_settings(const struct drm_display_mode *mode,
2644 const struct dm_connector_state *dm_state,
2645 struct dc_stream_state *stream)
2647 enum amdgpu_rmx_type rmx_type;
2649 struct rect src = { 0 }; /* viewport in composition space*/
2650 struct rect dst = { 0 }; /* stream addressable area */
2652 /* no mode. nothing to be done */
2656 /* Full screen scaling by default */
2657 src.width = mode->hdisplay;
2658 src.height = mode->vdisplay;
2659 dst.width = stream->timing.h_addressable;
2660 dst.height = stream->timing.v_addressable;
2663 rmx_type = dm_state->scaling;
2664 if (rmx_type == RMX_ASPECT || rmx_type == RMX_OFF) {
2665 if (src.width * dst.height <
2666 src.height * dst.width) {
2667 /* height needs less upscaling/more downscaling */
2668 dst.width = src.width *
2669 dst.height / src.height;
2671 /* width needs less upscaling/more downscaling */
2672 dst.height = src.height *
2673 dst.width / src.width;
2675 } else if (rmx_type == RMX_CENTER) {
2679 dst.x = (stream->timing.h_addressable - dst.width) / 2;
2680 dst.y = (stream->timing.v_addressable - dst.height) / 2;
2682 if (dm_state->underscan_enable) {
2683 dst.x += dm_state->underscan_hborder / 2;
2684 dst.y += dm_state->underscan_vborder / 2;
2685 dst.width -= dm_state->underscan_hborder;
2686 dst.height -= dm_state->underscan_vborder;
2693 DRM_DEBUG_DRIVER("Destination Rectangle x:%d y:%d width:%d height:%d\n",
2694 dst.x, dst.y, dst.width, dst.height);
2698 static enum dc_color_depth
2699 convert_color_depth_from_display_info(const struct drm_connector *connector)
2701 struct dm_connector_state *dm_conn_state =
2702 to_dm_connector_state(connector->state);
2703 uint32_t bpc = connector->display_info.bpc;
2705 /* TODO: Remove this when there's support for max_bpc in drm */
2706 if (dm_conn_state && bpc > dm_conn_state->max_bpc)
2707 /* Round down to nearest even number. */
2708 bpc = dm_conn_state->max_bpc - (dm_conn_state->max_bpc & 1);
2713 * Temporary Work around, DRM doesn't parse color depth for
2714 * EDID revision before 1.4
2715 * TODO: Fix edid parsing
2717 return COLOR_DEPTH_888;
2719 return COLOR_DEPTH_666;
2721 return COLOR_DEPTH_888;
2723 return COLOR_DEPTH_101010;
2725 return COLOR_DEPTH_121212;
2727 return COLOR_DEPTH_141414;
2729 return COLOR_DEPTH_161616;
2731 return COLOR_DEPTH_UNDEFINED;
2735 static enum dc_aspect_ratio
2736 get_aspect_ratio(const struct drm_display_mode *mode_in)
2738 /* 1-1 mapping, since both enums follow the HDMI spec. */
2739 return (enum dc_aspect_ratio) mode_in->picture_aspect_ratio;
2742 static enum dc_color_space
2743 get_output_color_space(const struct dc_crtc_timing *dc_crtc_timing)
2745 enum dc_color_space color_space = COLOR_SPACE_SRGB;
2747 switch (dc_crtc_timing->pixel_encoding) {
2748 case PIXEL_ENCODING_YCBCR422:
2749 case PIXEL_ENCODING_YCBCR444:
2750 case PIXEL_ENCODING_YCBCR420:
2753 * 27030khz is the separation point between HDTV and SDTV
2754 * according to HDMI spec, we use YCbCr709 and YCbCr601
2757 if (dc_crtc_timing->pix_clk_100hz > 270300) {
2758 if (dc_crtc_timing->flags.Y_ONLY)
2760 COLOR_SPACE_YCBCR709_LIMITED;
2762 color_space = COLOR_SPACE_YCBCR709;
2764 if (dc_crtc_timing->flags.Y_ONLY)
2766 COLOR_SPACE_YCBCR601_LIMITED;
2768 color_space = COLOR_SPACE_YCBCR601;
2773 case PIXEL_ENCODING_RGB:
2774 color_space = COLOR_SPACE_SRGB;
2785 static void reduce_mode_colour_depth(struct dc_crtc_timing *timing_out)
2787 if (timing_out->display_color_depth <= COLOR_DEPTH_888)
2790 timing_out->display_color_depth--;
2793 static void adjust_colour_depth_from_display_info(struct dc_crtc_timing *timing_out,
2794 const struct drm_display_info *info)
2797 if (timing_out->display_color_depth <= COLOR_DEPTH_888)
2800 normalized_clk = timing_out->pix_clk_100hz / 10;
2801 /* YCbCr 4:2:0 requires additional adjustment of 1/2 */
2802 if (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420)
2803 normalized_clk /= 2;
2804 /* Adjusting pix clock following on HDMI spec based on colour depth */
2805 switch (timing_out->display_color_depth) {
2806 case COLOR_DEPTH_101010:
2807 normalized_clk = (normalized_clk * 30) / 24;
2809 case COLOR_DEPTH_121212:
2810 normalized_clk = (normalized_clk * 36) / 24;
2812 case COLOR_DEPTH_161616:
2813 normalized_clk = (normalized_clk * 48) / 24;
2818 if (normalized_clk <= info->max_tmds_clock)
2820 reduce_mode_colour_depth(timing_out);
2822 } while (timing_out->display_color_depth > COLOR_DEPTH_888);
2827 fill_stream_properties_from_drm_display_mode(struct dc_stream_state *stream,
2828 const struct drm_display_mode *mode_in,
2829 const struct drm_connector *connector,
2830 const struct dc_stream_state *old_stream)
2832 struct dc_crtc_timing *timing_out = &stream->timing;
2833 const struct drm_display_info *info = &connector->display_info;
2835 memset(timing_out, 0, sizeof(struct dc_crtc_timing));
2837 timing_out->h_border_left = 0;
2838 timing_out->h_border_right = 0;
2839 timing_out->v_border_top = 0;
2840 timing_out->v_border_bottom = 0;
2841 /* TODO: un-hardcode */
2842 if (drm_mode_is_420_only(info, mode_in)
2843 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
2844 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
2845 else if ((connector->display_info.color_formats & DRM_COLOR_FORMAT_YCRCB444)
2846 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
2847 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR444;
2849 timing_out->pixel_encoding = PIXEL_ENCODING_RGB;
2851 timing_out->timing_3d_format = TIMING_3D_FORMAT_NONE;
2852 timing_out->display_color_depth = convert_color_depth_from_display_info(
2854 timing_out->scan_type = SCANNING_TYPE_NODATA;
2855 timing_out->hdmi_vic = 0;
2858 timing_out->vic = old_stream->timing.vic;
2859 timing_out->flags.HSYNC_POSITIVE_POLARITY = old_stream->timing.flags.HSYNC_POSITIVE_POLARITY;
2860 timing_out->flags.VSYNC_POSITIVE_POLARITY = old_stream->timing.flags.VSYNC_POSITIVE_POLARITY;
2862 timing_out->vic = drm_match_cea_mode(mode_in);
2863 if (mode_in->flags & DRM_MODE_FLAG_PHSYNC)
2864 timing_out->flags.HSYNC_POSITIVE_POLARITY = 1;
2865 if (mode_in->flags & DRM_MODE_FLAG_PVSYNC)
2866 timing_out->flags.VSYNC_POSITIVE_POLARITY = 1;
2869 timing_out->h_addressable = mode_in->crtc_hdisplay;
2870 timing_out->h_total = mode_in->crtc_htotal;
2871 timing_out->h_sync_width =
2872 mode_in->crtc_hsync_end - mode_in->crtc_hsync_start;
2873 timing_out->h_front_porch =
2874 mode_in->crtc_hsync_start - mode_in->crtc_hdisplay;
2875 timing_out->v_total = mode_in->crtc_vtotal;
2876 timing_out->v_addressable = mode_in->crtc_vdisplay;
2877 timing_out->v_front_porch =
2878 mode_in->crtc_vsync_start - mode_in->crtc_vdisplay;
2879 timing_out->v_sync_width =
2880 mode_in->crtc_vsync_end - mode_in->crtc_vsync_start;
2881 timing_out->pix_clk_100hz = mode_in->crtc_clock * 10;
2882 timing_out->aspect_ratio = get_aspect_ratio(mode_in);
2884 stream->output_color_space = get_output_color_space(timing_out);
2886 stream->out_transfer_func->type = TF_TYPE_PREDEFINED;
2887 stream->out_transfer_func->tf = TRANSFER_FUNCTION_SRGB;
2888 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
2889 adjust_colour_depth_from_display_info(timing_out, info);
2892 static void fill_audio_info(struct audio_info *audio_info,
2893 const struct drm_connector *drm_connector,
2894 const struct dc_sink *dc_sink)
2897 int cea_revision = 0;
2898 const struct dc_edid_caps *edid_caps = &dc_sink->edid_caps;
2900 audio_info->manufacture_id = edid_caps->manufacturer_id;
2901 audio_info->product_id = edid_caps->product_id;
2903 cea_revision = drm_connector->display_info.cea_rev;
2905 strscpy(audio_info->display_name,
2906 edid_caps->display_name,
2907 AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS);
2909 if (cea_revision >= 3) {
2910 audio_info->mode_count = edid_caps->audio_mode_count;
2912 for (i = 0; i < audio_info->mode_count; ++i) {
2913 audio_info->modes[i].format_code =
2914 (enum audio_format_code)
2915 (edid_caps->audio_modes[i].format_code);
2916 audio_info->modes[i].channel_count =
2917 edid_caps->audio_modes[i].channel_count;
2918 audio_info->modes[i].sample_rates.all =
2919 edid_caps->audio_modes[i].sample_rate;
2920 audio_info->modes[i].sample_size =
2921 edid_caps->audio_modes[i].sample_size;
2925 audio_info->flags.all = edid_caps->speaker_flags;
2927 /* TODO: We only check for the progressive mode, check for interlace mode too */
2928 if (drm_connector->latency_present[0]) {
2929 audio_info->video_latency = drm_connector->video_latency[0];
2930 audio_info->audio_latency = drm_connector->audio_latency[0];
2933 /* TODO: For DP, video and audio latency should be calculated from DPCD caps */
2938 copy_crtc_timing_for_drm_display_mode(const struct drm_display_mode *src_mode,
2939 struct drm_display_mode *dst_mode)
2941 dst_mode->crtc_hdisplay = src_mode->crtc_hdisplay;
2942 dst_mode->crtc_vdisplay = src_mode->crtc_vdisplay;
2943 dst_mode->crtc_clock = src_mode->crtc_clock;
2944 dst_mode->crtc_hblank_start = src_mode->crtc_hblank_start;
2945 dst_mode->crtc_hblank_end = src_mode->crtc_hblank_end;
2946 dst_mode->crtc_hsync_start = src_mode->crtc_hsync_start;
2947 dst_mode->crtc_hsync_end = src_mode->crtc_hsync_end;
2948 dst_mode->crtc_htotal = src_mode->crtc_htotal;
2949 dst_mode->crtc_hskew = src_mode->crtc_hskew;
2950 dst_mode->crtc_vblank_start = src_mode->crtc_vblank_start;
2951 dst_mode->crtc_vblank_end = src_mode->crtc_vblank_end;
2952 dst_mode->crtc_vsync_start = src_mode->crtc_vsync_start;
2953 dst_mode->crtc_vsync_end = src_mode->crtc_vsync_end;
2954 dst_mode->crtc_vtotal = src_mode->crtc_vtotal;
2958 decide_crtc_timing_for_drm_display_mode(struct drm_display_mode *drm_mode,
2959 const struct drm_display_mode *native_mode,
2962 if (scale_enabled) {
2963 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
2964 } else if (native_mode->clock == drm_mode->clock &&
2965 native_mode->htotal == drm_mode->htotal &&
2966 native_mode->vtotal == drm_mode->vtotal) {
2967 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
2969 /* no scaling nor amdgpu inserted, no need to patch */
2973 static struct dc_sink *
2974 create_fake_sink(struct amdgpu_dm_connector *aconnector)
2976 struct dc_sink_init_data sink_init_data = { 0 };
2977 struct dc_sink *sink = NULL;
2978 sink_init_data.link = aconnector->dc_link;
2979 sink_init_data.sink_signal = aconnector->dc_link->connector_signal;
2981 sink = dc_sink_create(&sink_init_data);
2983 DRM_ERROR("Failed to create sink!\n");
2986 sink->sink_signal = SIGNAL_TYPE_VIRTUAL;
2991 static void set_multisync_trigger_params(
2992 struct dc_stream_state *stream)
2994 if (stream->triggered_crtc_reset.enabled) {
2995 stream->triggered_crtc_reset.event = CRTC_EVENT_VSYNC_RISING;
2996 stream->triggered_crtc_reset.delay = TRIGGER_DELAY_NEXT_LINE;
3000 static void set_master_stream(struct dc_stream_state *stream_set[],
3003 int j, highest_rfr = 0, master_stream = 0;
3005 for (j = 0; j < stream_count; j++) {
3006 if (stream_set[j] && stream_set[j]->triggered_crtc_reset.enabled) {
3007 int refresh_rate = 0;
3009 refresh_rate = (stream_set[j]->timing.pix_clk_100hz*100)/
3010 (stream_set[j]->timing.h_total*stream_set[j]->timing.v_total);
3011 if (refresh_rate > highest_rfr) {
3012 highest_rfr = refresh_rate;
3017 for (j = 0; j < stream_count; j++) {
3019 stream_set[j]->triggered_crtc_reset.event_source = stream_set[master_stream];
3023 static void dm_enable_per_frame_crtc_master_sync(struct dc_state *context)
3027 if (context->stream_count < 2)
3029 for (i = 0; i < context->stream_count ; i++) {
3030 if (!context->streams[i])
3033 * TODO: add a function to read AMD VSDB bits and set
3034 * crtc_sync_master.multi_sync_enabled flag
3035 * For now it's set to false
3037 set_multisync_trigger_params(context->streams[i]);
3039 set_master_stream(context->streams, context->stream_count);
3042 static struct dc_stream_state *
3043 create_stream_for_sink(struct amdgpu_dm_connector *aconnector,
3044 const struct drm_display_mode *drm_mode,
3045 const struct dm_connector_state *dm_state,
3046 const struct dc_stream_state *old_stream)
3048 struct drm_display_mode *preferred_mode = NULL;
3049 struct drm_connector *drm_connector;
3050 struct dc_stream_state *stream = NULL;
3051 struct drm_display_mode mode = *drm_mode;
3052 bool native_mode_found = false;
3053 bool scale = dm_state ? (dm_state->scaling != RMX_OFF) : false;
3055 int preferred_refresh = 0;
3057 struct dc_sink *sink = NULL;
3058 if (aconnector == NULL) {
3059 DRM_ERROR("aconnector is NULL!\n");
3063 drm_connector = &aconnector->base;
3065 if (!aconnector->dc_sink) {
3066 sink = create_fake_sink(aconnector);
3070 sink = aconnector->dc_sink;
3071 dc_sink_retain(sink);
3074 stream = dc_create_stream_for_sink(sink);
3076 if (stream == NULL) {
3077 DRM_ERROR("Failed to create stream for sink!\n");
3081 stream->dm_stream_context = aconnector;
3083 list_for_each_entry(preferred_mode, &aconnector->base.modes, head) {
3084 /* Search for preferred mode */
3085 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED) {
3086 native_mode_found = true;
3090 if (!native_mode_found)
3091 preferred_mode = list_first_entry_or_null(
3092 &aconnector->base.modes,
3093 struct drm_display_mode,
3096 mode_refresh = drm_mode_vrefresh(&mode);
3098 if (preferred_mode == NULL) {
3100 * This may not be an error, the use case is when we have no
3101 * usermode calls to reset and set mode upon hotplug. In this
3102 * case, we call set mode ourselves to restore the previous mode
3103 * and the modelist may not be filled in in time.
3105 DRM_DEBUG_DRIVER("No preferred mode found\n");
3107 decide_crtc_timing_for_drm_display_mode(
3108 &mode, preferred_mode,
3109 dm_state ? (dm_state->scaling != RMX_OFF) : false);
3110 preferred_refresh = drm_mode_vrefresh(preferred_mode);
3114 drm_mode_set_crtcinfo(&mode, 0);
3117 * If scaling is enabled and refresh rate didn't change
3118 * we copy the vic and polarities of the old timings
3120 if (!scale || mode_refresh != preferred_refresh)
3121 fill_stream_properties_from_drm_display_mode(stream,
3122 &mode, &aconnector->base, NULL);
3124 fill_stream_properties_from_drm_display_mode(stream,
3125 &mode, &aconnector->base, old_stream);
3127 update_stream_scaling_settings(&mode, dm_state, stream);
3130 &stream->audio_info,
3134 update_stream_signal(stream, sink);
3137 dc_sink_release(sink);
3142 static void amdgpu_dm_crtc_destroy(struct drm_crtc *crtc)
3144 drm_crtc_cleanup(crtc);
3148 static void dm_crtc_destroy_state(struct drm_crtc *crtc,
3149 struct drm_crtc_state *state)
3151 struct dm_crtc_state *cur = to_dm_crtc_state(state);
3153 /* TODO Destroy dc_stream objects are stream object is flattened */
3155 dc_stream_release(cur->stream);
3158 __drm_atomic_helper_crtc_destroy_state(state);
3164 static void dm_crtc_reset_state(struct drm_crtc *crtc)
3166 struct dm_crtc_state *state;
3169 dm_crtc_destroy_state(crtc, crtc->state);
3171 state = kzalloc(sizeof(*state), GFP_KERNEL);
3172 if (WARN_ON(!state))
3175 crtc->state = &state->base;
3176 crtc->state->crtc = crtc;
3180 static struct drm_crtc_state *
3181 dm_crtc_duplicate_state(struct drm_crtc *crtc)
3183 struct dm_crtc_state *state, *cur;
3185 cur = to_dm_crtc_state(crtc->state);
3187 if (WARN_ON(!crtc->state))
3190 state = kzalloc(sizeof(*state), GFP_KERNEL);
3194 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
3197 state->stream = cur->stream;
3198 dc_stream_retain(state->stream);
3201 state->vrr_params = cur->vrr_params;
3202 state->vrr_infopacket = cur->vrr_infopacket;
3203 state->abm_level = cur->abm_level;
3204 state->vrr_supported = cur->vrr_supported;
3205 state->freesync_config = cur->freesync_config;
3206 state->crc_enabled = cur->crc_enabled;
3208 /* TODO Duplicate dc_stream after objects are stream object is flattened */
3210 return &state->base;
3214 static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
3216 enum dc_irq_source irq_source;
3217 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
3218 struct amdgpu_device *adev = crtc->dev->dev_private;
3220 irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
3221 return dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY;
3224 static int dm_enable_vblank(struct drm_crtc *crtc)
3226 return dm_set_vblank(crtc, true);
3229 static void dm_disable_vblank(struct drm_crtc *crtc)
3231 dm_set_vblank(crtc, false);
3234 /* Implemented only the options currently availible for the driver */
3235 static const struct drm_crtc_funcs amdgpu_dm_crtc_funcs = {
3236 .reset = dm_crtc_reset_state,
3237 .destroy = amdgpu_dm_crtc_destroy,
3238 .gamma_set = drm_atomic_helper_legacy_gamma_set,
3239 .set_config = drm_atomic_helper_set_config,
3240 .page_flip = drm_atomic_helper_page_flip,
3241 .atomic_duplicate_state = dm_crtc_duplicate_state,
3242 .atomic_destroy_state = dm_crtc_destroy_state,
3243 .set_crc_source = amdgpu_dm_crtc_set_crc_source,
3244 .verify_crc_source = amdgpu_dm_crtc_verify_crc_source,
3245 .enable_vblank = dm_enable_vblank,
3246 .disable_vblank = dm_disable_vblank,
3249 static enum drm_connector_status
3250 amdgpu_dm_connector_detect(struct drm_connector *connector, bool force)
3253 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
3257 * 1. This interface is NOT called in context of HPD irq.
3258 * 2. This interface *is called* in context of user-mode ioctl. Which
3259 * makes it a bad place for *any* MST-related activity.
3262 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED &&
3263 !aconnector->fake_enable)
3264 connected = (aconnector->dc_sink != NULL);
3266 connected = (aconnector->base.force == DRM_FORCE_ON);
3268 return (connected ? connector_status_connected :
3269 connector_status_disconnected);
3272 int amdgpu_dm_connector_atomic_set_property(struct drm_connector *connector,
3273 struct drm_connector_state *connector_state,
3274 struct drm_property *property,
3277 struct drm_device *dev = connector->dev;
3278 struct amdgpu_device *adev = dev->dev_private;
3279 struct dm_connector_state *dm_old_state =
3280 to_dm_connector_state(connector->state);
3281 struct dm_connector_state *dm_new_state =
3282 to_dm_connector_state(connector_state);
3286 if (property == dev->mode_config.scaling_mode_property) {
3287 enum amdgpu_rmx_type rmx_type;
3290 case DRM_MODE_SCALE_CENTER:
3291 rmx_type = RMX_CENTER;
3293 case DRM_MODE_SCALE_ASPECT:
3294 rmx_type = RMX_ASPECT;
3296 case DRM_MODE_SCALE_FULLSCREEN:
3297 rmx_type = RMX_FULL;
3299 case DRM_MODE_SCALE_NONE:
3305 if (dm_old_state->scaling == rmx_type)
3308 dm_new_state->scaling = rmx_type;
3310 } else if (property == adev->mode_info.underscan_hborder_property) {
3311 dm_new_state->underscan_hborder = val;
3313 } else if (property == adev->mode_info.underscan_vborder_property) {
3314 dm_new_state->underscan_vborder = val;
3316 } else if (property == adev->mode_info.underscan_property) {
3317 dm_new_state->underscan_enable = val;
3319 } else if (property == adev->mode_info.max_bpc_property) {
3320 dm_new_state->max_bpc = val;
3322 } else if (property == adev->mode_info.abm_level_property) {
3323 dm_new_state->abm_level = val;
3330 int amdgpu_dm_connector_atomic_get_property(struct drm_connector *connector,
3331 const struct drm_connector_state *state,
3332 struct drm_property *property,
3335 struct drm_device *dev = connector->dev;
3336 struct amdgpu_device *adev = dev->dev_private;
3337 struct dm_connector_state *dm_state =
3338 to_dm_connector_state(state);
3341 if (property == dev->mode_config.scaling_mode_property) {
3342 switch (dm_state->scaling) {
3344 *val = DRM_MODE_SCALE_CENTER;
3347 *val = DRM_MODE_SCALE_ASPECT;
3350 *val = DRM_MODE_SCALE_FULLSCREEN;
3354 *val = DRM_MODE_SCALE_NONE;
3358 } else if (property == adev->mode_info.underscan_hborder_property) {
3359 *val = dm_state->underscan_hborder;
3361 } else if (property == adev->mode_info.underscan_vborder_property) {
3362 *val = dm_state->underscan_vborder;
3364 } else if (property == adev->mode_info.underscan_property) {
3365 *val = dm_state->underscan_enable;
3367 } else if (property == adev->mode_info.max_bpc_property) {
3368 *val = dm_state->max_bpc;
3370 } else if (property == adev->mode_info.abm_level_property) {
3371 *val = dm_state->abm_level;
3378 static void amdgpu_dm_connector_destroy(struct drm_connector *connector)
3380 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
3381 const struct dc_link *link = aconnector->dc_link;
3382 struct amdgpu_device *adev = connector->dev->dev_private;
3383 struct amdgpu_display_manager *dm = &adev->dm;
3385 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
3386 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
3388 if ((link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) &&
3389 link->type != dc_connection_none &&
3390 dm->backlight_dev) {
3391 backlight_device_unregister(dm->backlight_dev);
3392 dm->backlight_dev = NULL;
3396 if (aconnector->dc_em_sink)
3397 dc_sink_release(aconnector->dc_em_sink);
3398 aconnector->dc_em_sink = NULL;
3399 if (aconnector->dc_sink)
3400 dc_sink_release(aconnector->dc_sink);
3401 aconnector->dc_sink = NULL;
3403 drm_dp_cec_unregister_connector(&aconnector->dm_dp_aux.aux);
3404 drm_connector_unregister(connector);
3405 drm_connector_cleanup(connector);
3409 void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector)
3411 struct dm_connector_state *state =
3412 to_dm_connector_state(connector->state);
3414 if (connector->state)
3415 __drm_atomic_helper_connector_destroy_state(connector->state);
3419 state = kzalloc(sizeof(*state), GFP_KERNEL);
3422 state->scaling = RMX_OFF;
3423 state->underscan_enable = false;
3424 state->underscan_hborder = 0;
3425 state->underscan_vborder = 0;
3428 __drm_atomic_helper_connector_reset(connector, &state->base);
3432 struct drm_connector_state *
3433 amdgpu_dm_connector_atomic_duplicate_state(struct drm_connector *connector)
3435 struct dm_connector_state *state =
3436 to_dm_connector_state(connector->state);
3438 struct dm_connector_state *new_state =
3439 kmemdup(state, sizeof(*state), GFP_KERNEL);
3444 __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);
3446 new_state->freesync_capable = state->freesync_capable;
3447 new_state->abm_level = state->abm_level;
3448 new_state->scaling = state->scaling;
3449 new_state->underscan_enable = state->underscan_enable;
3450 new_state->underscan_hborder = state->underscan_hborder;
3451 new_state->underscan_vborder = state->underscan_vborder;
3452 new_state->max_bpc = state->max_bpc;
3454 return &new_state->base;
3457 static const struct drm_connector_funcs amdgpu_dm_connector_funcs = {
3458 .reset = amdgpu_dm_connector_funcs_reset,
3459 .detect = amdgpu_dm_connector_detect,
3460 .fill_modes = drm_helper_probe_single_connector_modes,
3461 .destroy = amdgpu_dm_connector_destroy,
3462 .atomic_duplicate_state = amdgpu_dm_connector_atomic_duplicate_state,
3463 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
3464 .atomic_set_property = amdgpu_dm_connector_atomic_set_property,
3465 .atomic_get_property = amdgpu_dm_connector_atomic_get_property
3468 static int get_modes(struct drm_connector *connector)
3470 return amdgpu_dm_connector_get_modes(connector);
3473 static void create_eml_sink(struct amdgpu_dm_connector *aconnector)
3475 struct dc_sink_init_data init_params = {
3476 .link = aconnector->dc_link,
3477 .sink_signal = SIGNAL_TYPE_VIRTUAL
3481 if (!aconnector->base.edid_blob_ptr) {
3482 DRM_ERROR("No EDID firmware found on connector: %s ,forcing to OFF!\n",
3483 aconnector->base.name);
3485 aconnector->base.force = DRM_FORCE_OFF;
3486 aconnector->base.override_edid = false;
3490 edid = (struct edid *) aconnector->base.edid_blob_ptr->data;
3492 aconnector->edid = edid;
3494 aconnector->dc_em_sink = dc_link_add_remote_sink(
3495 aconnector->dc_link,
3497 (edid->extensions + 1) * EDID_LENGTH,
3500 if (aconnector->base.force == DRM_FORCE_ON) {
3501 aconnector->dc_sink = aconnector->dc_link->local_sink ?
3502 aconnector->dc_link->local_sink :
3503 aconnector->dc_em_sink;
3504 dc_sink_retain(aconnector->dc_sink);
3508 static void handle_edid_mgmt(struct amdgpu_dm_connector *aconnector)
3510 struct dc_link *link = (struct dc_link *)aconnector->dc_link;
3513 * In case of headless boot with force on for DP managed connector
3514 * Those settings have to be != 0 to get initial modeset
3516 if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT) {
3517 link->verified_link_cap.lane_count = LANE_COUNT_FOUR;
3518 link->verified_link_cap.link_rate = LINK_RATE_HIGH2;
3522 aconnector->base.override_edid = true;
3523 create_eml_sink(aconnector);
3526 enum drm_mode_status amdgpu_dm_connector_mode_valid(struct drm_connector *connector,
3527 struct drm_display_mode *mode)
3529 int result = MODE_ERROR;
3530 struct dc_sink *dc_sink;
3531 struct amdgpu_device *adev = connector->dev->dev_private;
3532 /* TODO: Unhardcode stream count */
3533 struct dc_stream_state *stream;
3534 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
3535 enum dc_status dc_result = DC_OK;
3537 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
3538 (mode->flags & DRM_MODE_FLAG_DBLSCAN))
3542 * Only run this the first time mode_valid is called to initilialize
3545 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED &&
3546 !aconnector->dc_em_sink)
3547 handle_edid_mgmt(aconnector);
3549 dc_sink = to_amdgpu_dm_connector(connector)->dc_sink;
3551 if (dc_sink == NULL) {
3552 DRM_ERROR("dc_sink is NULL!\n");
3556 stream = create_stream_for_sink(aconnector, mode, NULL, NULL);
3557 if (stream == NULL) {
3558 DRM_ERROR("Failed to create stream for sink!\n");
3562 dc_result = dc_validate_stream(adev->dm.dc, stream);
3564 if (dc_result == DC_OK)
3567 DRM_DEBUG_KMS("Mode %dx%d (clk %d) failed DC validation with error %d\n",
3573 dc_stream_release(stream);
3576 /* TODO: error handling*/
3580 static const struct drm_connector_helper_funcs
3581 amdgpu_dm_connector_helper_funcs = {
3583 * If hotplugging a second bigger display in FB Con mode, bigger resolution
3584 * modes will be filtered by drm_mode_validate_size(), and those modes
3585 * are missing after user start lightdm. So we need to renew modes list.
3586 * in get_modes call back, not just return the modes count
3588 .get_modes = get_modes,
3589 .mode_valid = amdgpu_dm_connector_mode_valid,
3592 static void dm_crtc_helper_disable(struct drm_crtc *crtc)
3596 static int dm_crtc_helper_atomic_check(struct drm_crtc *crtc,
3597 struct drm_crtc_state *state)
3599 struct amdgpu_device *adev = crtc->dev->dev_private;
3600 struct dc *dc = adev->dm.dc;
3601 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(state);
3604 if (unlikely(!dm_crtc_state->stream &&
3605 modeset_required(state, NULL, dm_crtc_state->stream))) {
3610 /* In some use cases, like reset, no stream is attached */
3611 if (!dm_crtc_state->stream)
3614 if (dc_validate_stream(dc, dm_crtc_state->stream) == DC_OK)
3620 static bool dm_crtc_helper_mode_fixup(struct drm_crtc *crtc,
3621 const struct drm_display_mode *mode,
3622 struct drm_display_mode *adjusted_mode)
3627 static const struct drm_crtc_helper_funcs amdgpu_dm_crtc_helper_funcs = {
3628 .disable = dm_crtc_helper_disable,
3629 .atomic_check = dm_crtc_helper_atomic_check,
3630 .mode_fixup = dm_crtc_helper_mode_fixup
3633 static void dm_encoder_helper_disable(struct drm_encoder *encoder)
3638 static int dm_encoder_helper_atomic_check(struct drm_encoder *encoder,
3639 struct drm_crtc_state *crtc_state,
3640 struct drm_connector_state *conn_state)
3645 const struct drm_encoder_helper_funcs amdgpu_dm_encoder_helper_funcs = {
3646 .disable = dm_encoder_helper_disable,
3647 .atomic_check = dm_encoder_helper_atomic_check
3650 static void dm_drm_plane_reset(struct drm_plane *plane)
3652 struct dm_plane_state *amdgpu_state = NULL;
3655 plane->funcs->atomic_destroy_state(plane, plane->state);
3657 amdgpu_state = kzalloc(sizeof(*amdgpu_state), GFP_KERNEL);
3658 WARN_ON(amdgpu_state == NULL);
3661 __drm_atomic_helper_plane_reset(plane, &amdgpu_state->base);
3664 static struct drm_plane_state *
3665 dm_drm_plane_duplicate_state(struct drm_plane *plane)
3667 struct dm_plane_state *dm_plane_state, *old_dm_plane_state;
3669 old_dm_plane_state = to_dm_plane_state(plane->state);
3670 dm_plane_state = kzalloc(sizeof(*dm_plane_state), GFP_KERNEL);
3671 if (!dm_plane_state)
3674 __drm_atomic_helper_plane_duplicate_state(plane, &dm_plane_state->base);
3676 if (old_dm_plane_state->dc_state) {
3677 dm_plane_state->dc_state = old_dm_plane_state->dc_state;
3678 dc_plane_state_retain(dm_plane_state->dc_state);
3681 return &dm_plane_state->base;
3684 void dm_drm_plane_destroy_state(struct drm_plane *plane,
3685 struct drm_plane_state *state)
3687 struct dm_plane_state *dm_plane_state = to_dm_plane_state(state);
3689 if (dm_plane_state->dc_state)
3690 dc_plane_state_release(dm_plane_state->dc_state);
3692 drm_atomic_helper_plane_destroy_state(plane, state);
3695 static const struct drm_plane_funcs dm_plane_funcs = {
3696 .update_plane = drm_atomic_helper_update_plane,
3697 .disable_plane = drm_atomic_helper_disable_plane,
3698 .destroy = drm_primary_helper_destroy,
3699 .reset = dm_drm_plane_reset,
3700 .atomic_duplicate_state = dm_drm_plane_duplicate_state,
3701 .atomic_destroy_state = dm_drm_plane_destroy_state,
3704 static int dm_plane_helper_prepare_fb(struct drm_plane *plane,
3705 struct drm_plane_state *new_state)
3707 struct amdgpu_framebuffer *afb;
3708 struct drm_gem_object *obj;
3709 struct amdgpu_device *adev;
3710 struct amdgpu_bo *rbo;
3711 struct dm_plane_state *dm_plane_state_new, *dm_plane_state_old;
3712 uint64_t tiling_flags;
3716 dm_plane_state_old = to_dm_plane_state(plane->state);
3717 dm_plane_state_new = to_dm_plane_state(new_state);
3719 if (!new_state->fb) {
3720 DRM_DEBUG_DRIVER("No FB bound\n");
3724 afb = to_amdgpu_framebuffer(new_state->fb);
3725 obj = new_state->fb->obj[0];
3726 rbo = gem_to_amdgpu_bo(obj);
3727 adev = amdgpu_ttm_adev(rbo->tbo.bdev);
3728 r = amdgpu_bo_reserve(rbo, false);
3729 if (unlikely(r != 0))
3732 if (plane->type != DRM_PLANE_TYPE_CURSOR)
3733 domain = amdgpu_display_supported_domains(adev);
3735 domain = AMDGPU_GEM_DOMAIN_VRAM;
3737 r = amdgpu_bo_pin(rbo, domain);
3738 if (unlikely(r != 0)) {
3739 if (r != -ERESTARTSYS)
3740 DRM_ERROR("Failed to pin framebuffer with error %d\n", r);
3741 amdgpu_bo_unreserve(rbo);
3745 r = amdgpu_ttm_alloc_gart(&rbo->tbo);
3746 if (unlikely(r != 0)) {
3747 amdgpu_bo_unpin(rbo);
3748 amdgpu_bo_unreserve(rbo);
3749 DRM_ERROR("%p bind failed\n", rbo);
3753 amdgpu_bo_get_tiling_flags(rbo, &tiling_flags);
3755 amdgpu_bo_unreserve(rbo);
3757 afb->address = amdgpu_bo_gpu_offset(rbo);
3761 if (dm_plane_state_new->dc_state &&
3762 dm_plane_state_old->dc_state != dm_plane_state_new->dc_state) {
3763 struct dc_plane_state *plane_state = dm_plane_state_new->dc_state;
3765 fill_plane_tiling_attributes(
3766 adev, afb, plane_state, &plane_state->tiling_info,
3767 &plane_state->dcc, &plane_state->address, tiling_flags);
3773 static void dm_plane_helper_cleanup_fb(struct drm_plane *plane,
3774 struct drm_plane_state *old_state)
3776 struct amdgpu_bo *rbo;
3782 rbo = gem_to_amdgpu_bo(old_state->fb->obj[0]);
3783 r = amdgpu_bo_reserve(rbo, false);
3785 DRM_ERROR("failed to reserve rbo before unpin\n");
3789 amdgpu_bo_unpin(rbo);
3790 amdgpu_bo_unreserve(rbo);
3791 amdgpu_bo_unref(&rbo);
3794 static int dm_plane_atomic_check(struct drm_plane *plane,
3795 struct drm_plane_state *state)
3797 struct amdgpu_device *adev = plane->dev->dev_private;
3798 struct dc *dc = adev->dm.dc;
3799 struct dm_plane_state *dm_plane_state = to_dm_plane_state(state);
3801 if (!dm_plane_state->dc_state)
3804 if (!fill_rects_from_plane_state(state, dm_plane_state->dc_state))
3807 if (dc_validate_plane(dc, dm_plane_state->dc_state) == DC_OK)
3813 static int dm_plane_atomic_async_check(struct drm_plane *plane,
3814 struct drm_plane_state *new_plane_state)
3816 struct drm_plane_state *old_plane_state =
3817 drm_atomic_get_old_plane_state(new_plane_state->state, plane);
3819 /* Only support async updates on cursor planes. */
3820 if (plane->type != DRM_PLANE_TYPE_CURSOR)
3824 * DRM calls prepare_fb and cleanup_fb on new_plane_state for
3825 * async commits so don't allow fb changes.
3827 if (old_plane_state->fb != new_plane_state->fb)
3833 static void dm_plane_atomic_async_update(struct drm_plane *plane,
3834 struct drm_plane_state *new_state)
3836 struct drm_plane_state *old_state =
3837 drm_atomic_get_old_plane_state(new_state->state, plane);
3839 if (plane->state->fb != new_state->fb)
3840 drm_atomic_set_fb_for_plane(plane->state, new_state->fb);
3842 plane->state->src_x = new_state->src_x;
3843 plane->state->src_y = new_state->src_y;
3844 plane->state->src_w = new_state->src_w;
3845 plane->state->src_h = new_state->src_h;
3846 plane->state->crtc_x = new_state->crtc_x;
3847 plane->state->crtc_y = new_state->crtc_y;
3848 plane->state->crtc_w = new_state->crtc_w;
3849 plane->state->crtc_h = new_state->crtc_h;
3851 handle_cursor_update(plane, old_state);
3854 static const struct drm_plane_helper_funcs dm_plane_helper_funcs = {
3855 .prepare_fb = dm_plane_helper_prepare_fb,
3856 .cleanup_fb = dm_plane_helper_cleanup_fb,
3857 .atomic_check = dm_plane_atomic_check,
3858 .atomic_async_check = dm_plane_atomic_async_check,
3859 .atomic_async_update = dm_plane_atomic_async_update
3863 * TODO: these are currently initialized to rgb formats only.
3864 * For future use cases we should either initialize them dynamically based on
3865 * plane capabilities, or initialize this array to all formats, so internal drm
3866 * check will succeed, and let DC implement proper check
3868 static const uint32_t rgb_formats[] = {
3869 DRM_FORMAT_XRGB8888,
3870 DRM_FORMAT_ARGB8888,
3871 DRM_FORMAT_RGBA8888,
3872 DRM_FORMAT_XRGB2101010,
3873 DRM_FORMAT_XBGR2101010,
3874 DRM_FORMAT_ARGB2101010,
3875 DRM_FORMAT_ABGR2101010,
3876 DRM_FORMAT_XBGR8888,
3877 DRM_FORMAT_ABGR8888,
3880 static const uint32_t overlay_formats[] = {
3881 DRM_FORMAT_XRGB8888,
3882 DRM_FORMAT_ARGB8888,
3883 DRM_FORMAT_RGBA8888,
3884 DRM_FORMAT_XBGR8888,
3885 DRM_FORMAT_ABGR8888,
3888 static const u32 cursor_formats[] = {
3892 static int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
3893 struct drm_plane *plane,
3894 unsigned long possible_crtcs)
3898 switch (plane->type) {
3899 case DRM_PLANE_TYPE_PRIMARY:
3900 res = drm_universal_plane_init(
3906 ARRAY_SIZE(rgb_formats),
3907 NULL, plane->type, NULL);
3909 case DRM_PLANE_TYPE_OVERLAY:
3910 res = drm_universal_plane_init(
3916 ARRAY_SIZE(overlay_formats),
3917 NULL, plane->type, NULL);
3919 case DRM_PLANE_TYPE_CURSOR:
3920 res = drm_universal_plane_init(
3926 ARRAY_SIZE(cursor_formats),
3927 NULL, plane->type, NULL);
3931 /* TODO: Check DC plane caps explicitly here for adding propertes */
3932 if (plane->type == DRM_PLANE_TYPE_OVERLAY) {
3933 unsigned int blend_caps = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
3934 BIT(DRM_MODE_BLEND_PREMULTI);
3936 drm_plane_create_alpha_property(plane);
3937 drm_plane_create_blend_mode_property(plane, blend_caps);
3940 drm_plane_helper_add(plane, &dm_plane_helper_funcs);
3942 /* Create (reset) the plane state */
3943 if (plane->funcs->reset)
3944 plane->funcs->reset(plane);
3950 static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
3951 struct drm_plane *plane,
3952 uint32_t crtc_index)
3954 struct amdgpu_crtc *acrtc = NULL;
3955 struct drm_plane *cursor_plane;
3959 cursor_plane = kzalloc(sizeof(*cursor_plane), GFP_KERNEL);
3963 cursor_plane->type = DRM_PLANE_TYPE_CURSOR;
3964 res = amdgpu_dm_plane_init(dm, cursor_plane, 0);
3966 acrtc = kzalloc(sizeof(struct amdgpu_crtc), GFP_KERNEL);
3970 res = drm_crtc_init_with_planes(
3975 &amdgpu_dm_crtc_funcs, NULL);
3980 drm_crtc_helper_add(&acrtc->base, &amdgpu_dm_crtc_helper_funcs);
3982 /* Create (reset) the plane state */
3983 if (acrtc->base.funcs->reset)
3984 acrtc->base.funcs->reset(&acrtc->base);
3986 acrtc->max_cursor_width = dm->adev->dm.dc->caps.max_cursor_size;
3987 acrtc->max_cursor_height = dm->adev->dm.dc->caps.max_cursor_size;
3989 acrtc->crtc_id = crtc_index;
3990 acrtc->base.enabled = false;
3991 acrtc->otg_inst = -1;
3993 dm->adev->mode_info.crtcs[crtc_index] = acrtc;
3994 drm_crtc_enable_color_mgmt(&acrtc->base, MAX_COLOR_LUT_ENTRIES,
3995 true, MAX_COLOR_LUT_ENTRIES);
3996 drm_mode_crtc_set_gamma_size(&acrtc->base, MAX_COLOR_LEGACY_LUT_ENTRIES);
4002 kfree(cursor_plane);
4007 static int to_drm_connector_type(enum signal_type st)
4010 case SIGNAL_TYPE_HDMI_TYPE_A:
4011 return DRM_MODE_CONNECTOR_HDMIA;
4012 case SIGNAL_TYPE_EDP:
4013 return DRM_MODE_CONNECTOR_eDP;
4014 case SIGNAL_TYPE_LVDS:
4015 return DRM_MODE_CONNECTOR_LVDS;
4016 case SIGNAL_TYPE_RGB:
4017 return DRM_MODE_CONNECTOR_VGA;
4018 case SIGNAL_TYPE_DISPLAY_PORT:
4019 case SIGNAL_TYPE_DISPLAY_PORT_MST:
4020 return DRM_MODE_CONNECTOR_DisplayPort;
4021 case SIGNAL_TYPE_DVI_DUAL_LINK:
4022 case SIGNAL_TYPE_DVI_SINGLE_LINK:
4023 return DRM_MODE_CONNECTOR_DVID;
4024 case SIGNAL_TYPE_VIRTUAL:
4025 return DRM_MODE_CONNECTOR_VIRTUAL;
4028 return DRM_MODE_CONNECTOR_Unknown;
4032 static struct drm_encoder *amdgpu_dm_connector_to_encoder(struct drm_connector *connector)
4034 return drm_encoder_find(connector->dev, NULL, connector->encoder_ids[0]);
4037 static void amdgpu_dm_get_native_mode(struct drm_connector *connector)
4039 struct drm_encoder *encoder;
4040 struct amdgpu_encoder *amdgpu_encoder;
4042 encoder = amdgpu_dm_connector_to_encoder(connector);
4044 if (encoder == NULL)
4047 amdgpu_encoder = to_amdgpu_encoder(encoder);
4049 amdgpu_encoder->native_mode.clock = 0;
4051 if (!list_empty(&connector->probed_modes)) {
4052 struct drm_display_mode *preferred_mode = NULL;
4054 list_for_each_entry(preferred_mode,
4055 &connector->probed_modes,
4057 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED)
4058 amdgpu_encoder->native_mode = *preferred_mode;
4066 static struct drm_display_mode *
4067 amdgpu_dm_create_common_mode(struct drm_encoder *encoder,
4069 int hdisplay, int vdisplay)
4071 struct drm_device *dev = encoder->dev;
4072 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
4073 struct drm_display_mode *mode = NULL;
4074 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
4076 mode = drm_mode_duplicate(dev, native_mode);
4081 mode->hdisplay = hdisplay;
4082 mode->vdisplay = vdisplay;
4083 mode->type &= ~DRM_MODE_TYPE_PREFERRED;
4084 strscpy(mode->name, name, DRM_DISPLAY_MODE_LEN);
4090 static void amdgpu_dm_connector_add_common_modes(struct drm_encoder *encoder,
4091 struct drm_connector *connector)
4093 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
4094 struct drm_display_mode *mode = NULL;
4095 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
4096 struct amdgpu_dm_connector *amdgpu_dm_connector =
4097 to_amdgpu_dm_connector(connector);
4101 char name[DRM_DISPLAY_MODE_LEN];
4104 } common_modes[] = {
4105 { "640x480", 640, 480},
4106 { "800x600", 800, 600},
4107 { "1024x768", 1024, 768},
4108 { "1280x720", 1280, 720},
4109 { "1280x800", 1280, 800},
4110 {"1280x1024", 1280, 1024},
4111 { "1440x900", 1440, 900},
4112 {"1680x1050", 1680, 1050},
4113 {"1600x1200", 1600, 1200},
4114 {"1920x1080", 1920, 1080},
4115 {"1920x1200", 1920, 1200}
4118 n = ARRAY_SIZE(common_modes);
4120 for (i = 0; i < n; i++) {
4121 struct drm_display_mode *curmode = NULL;
4122 bool mode_existed = false;
4124 if (common_modes[i].w > native_mode->hdisplay ||
4125 common_modes[i].h > native_mode->vdisplay ||
4126 (common_modes[i].w == native_mode->hdisplay &&
4127 common_modes[i].h == native_mode->vdisplay))
4130 list_for_each_entry(curmode, &connector->probed_modes, head) {
4131 if (common_modes[i].w == curmode->hdisplay &&
4132 common_modes[i].h == curmode->vdisplay) {
4133 mode_existed = true;
4141 mode = amdgpu_dm_create_common_mode(encoder,
4142 common_modes[i].name, common_modes[i].w,
4144 drm_mode_probed_add(connector, mode);
4145 amdgpu_dm_connector->num_modes++;
4149 static void amdgpu_dm_connector_ddc_get_modes(struct drm_connector *connector,
4152 struct amdgpu_dm_connector *amdgpu_dm_connector =
4153 to_amdgpu_dm_connector(connector);
4156 /* empty probed_modes */
4157 INIT_LIST_HEAD(&connector->probed_modes);
4158 amdgpu_dm_connector->num_modes =
4159 drm_add_edid_modes(connector, edid);
4161 amdgpu_dm_get_native_mode(connector);
4163 amdgpu_dm_connector->num_modes = 0;
4167 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector)
4169 struct amdgpu_dm_connector *amdgpu_dm_connector =
4170 to_amdgpu_dm_connector(connector);
4171 struct drm_encoder *encoder;
4172 struct edid *edid = amdgpu_dm_connector->edid;
4174 encoder = amdgpu_dm_connector_to_encoder(connector);
4176 if (!edid || !drm_edid_is_valid(edid)) {
4177 amdgpu_dm_connector->num_modes =
4178 drm_add_modes_noedid(connector, 640, 480);
4180 amdgpu_dm_connector_ddc_get_modes(connector, edid);
4181 amdgpu_dm_connector_add_common_modes(encoder, connector);
4183 amdgpu_dm_fbc_init(connector);
4185 return amdgpu_dm_connector->num_modes;
4188 void amdgpu_dm_connector_init_helper(struct amdgpu_display_manager *dm,
4189 struct amdgpu_dm_connector *aconnector,
4191 struct dc_link *link,
4194 struct amdgpu_device *adev = dm->ddev->dev_private;
4196 aconnector->connector_id = link_index;
4197 aconnector->dc_link = link;
4198 aconnector->base.interlace_allowed = false;
4199 aconnector->base.doublescan_allowed = false;
4200 aconnector->base.stereo_allowed = false;
4201 aconnector->base.dpms = DRM_MODE_DPMS_OFF;
4202 aconnector->hpd.hpd = AMDGPU_HPD_NONE; /* not used */
4203 mutex_init(&aconnector->hpd_lock);
4206 * configure support HPD hot plug connector_>polled default value is 0
4207 * which means HPD hot plug not supported
4209 switch (connector_type) {
4210 case DRM_MODE_CONNECTOR_HDMIA:
4211 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
4212 aconnector->base.ycbcr_420_allowed =
4213 link->link_enc->features.hdmi_ycbcr420_supported ? true : false;
4215 case DRM_MODE_CONNECTOR_DisplayPort:
4216 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
4217 aconnector->base.ycbcr_420_allowed =
4218 link->link_enc->features.dp_ycbcr420_supported ? true : false;
4220 case DRM_MODE_CONNECTOR_DVID:
4221 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
4227 drm_object_attach_property(&aconnector->base.base,
4228 dm->ddev->mode_config.scaling_mode_property,
4229 DRM_MODE_SCALE_NONE);
4231 drm_object_attach_property(&aconnector->base.base,
4232 adev->mode_info.underscan_property,
4234 drm_object_attach_property(&aconnector->base.base,
4235 adev->mode_info.underscan_hborder_property,
4237 drm_object_attach_property(&aconnector->base.base,
4238 adev->mode_info.underscan_vborder_property,
4240 drm_object_attach_property(&aconnector->base.base,
4241 adev->mode_info.max_bpc_property,
4244 if (connector_type == DRM_MODE_CONNECTOR_eDP &&
4245 dc_is_dmcu_initialized(adev->dm.dc)) {
4246 drm_object_attach_property(&aconnector->base.base,
4247 adev->mode_info.abm_level_property, 0);
4250 if (connector_type == DRM_MODE_CONNECTOR_HDMIA ||
4251 connector_type == DRM_MODE_CONNECTOR_DisplayPort ||
4252 connector_type == DRM_MODE_CONNECTOR_eDP) {
4253 drm_connector_attach_vrr_capable_property(
4258 static int amdgpu_dm_i2c_xfer(struct i2c_adapter *i2c_adap,
4259 struct i2c_msg *msgs, int num)
4261 struct amdgpu_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
4262 struct ddc_service *ddc_service = i2c->ddc_service;
4263 struct i2c_command cmd;
4267 cmd.payloads = kcalloc(num, sizeof(struct i2c_payload), GFP_KERNEL);
4272 cmd.number_of_payloads = num;
4273 cmd.engine = I2C_COMMAND_ENGINE_DEFAULT;
4276 for (i = 0; i < num; i++) {
4277 cmd.payloads[i].write = !(msgs[i].flags & I2C_M_RD);
4278 cmd.payloads[i].address = msgs[i].addr;
4279 cmd.payloads[i].length = msgs[i].len;
4280 cmd.payloads[i].data = msgs[i].buf;
4284 ddc_service->ctx->dc,
4285 ddc_service->ddc_pin->hw_info.ddc_channel,
4289 kfree(cmd.payloads);
4293 static u32 amdgpu_dm_i2c_func(struct i2c_adapter *adap)
4295 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
4298 static const struct i2c_algorithm amdgpu_dm_i2c_algo = {
4299 .master_xfer = amdgpu_dm_i2c_xfer,
4300 .functionality = amdgpu_dm_i2c_func,
4303 static struct amdgpu_i2c_adapter *
4304 create_i2c(struct ddc_service *ddc_service,
4308 struct amdgpu_device *adev = ddc_service->ctx->driver_context;
4309 struct amdgpu_i2c_adapter *i2c;
4311 i2c = kzalloc(sizeof(struct amdgpu_i2c_adapter), GFP_KERNEL);
4314 i2c->base.owner = THIS_MODULE;
4315 i2c->base.class = I2C_CLASS_DDC;
4316 i2c->base.dev.parent = &adev->pdev->dev;
4317 i2c->base.algo = &amdgpu_dm_i2c_algo;
4318 snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c hw bus %d", link_index);
4319 i2c_set_adapdata(&i2c->base, i2c);
4320 i2c->ddc_service = ddc_service;
4321 i2c->ddc_service->ddc_pin->hw_info.ddc_channel = link_index;
4328 * Note: this function assumes that dc_link_detect() was called for the
4329 * dc_link which will be represented by this aconnector.
4331 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
4332 struct amdgpu_dm_connector *aconnector,
4333 uint32_t link_index,
4334 struct amdgpu_encoder *aencoder)
4338 struct dc *dc = dm->dc;
4339 struct dc_link *link = dc_get_link_at_index(dc, link_index);
4340 struct amdgpu_i2c_adapter *i2c;
4342 link->priv = aconnector;
4344 DRM_DEBUG_DRIVER("%s()\n", __func__);
4346 i2c = create_i2c(link->ddc, link->link_index, &res);
4348 DRM_ERROR("Failed to create i2c adapter data\n");
4352 aconnector->i2c = i2c;
4353 res = i2c_add_adapter(&i2c->base);
4356 DRM_ERROR("Failed to register hw i2c %d\n", link->link_index);
4360 connector_type = to_drm_connector_type(link->connector_signal);
4362 res = drm_connector_init(
4365 &amdgpu_dm_connector_funcs,
4369 DRM_ERROR("connector_init failed\n");
4370 aconnector->connector_id = -1;
4374 drm_connector_helper_add(
4376 &amdgpu_dm_connector_helper_funcs);
4378 if (aconnector->base.funcs->reset)
4379 aconnector->base.funcs->reset(&aconnector->base);
4381 amdgpu_dm_connector_init_helper(
4388 drm_connector_attach_encoder(
4389 &aconnector->base, &aencoder->base);
4391 drm_connector_register(&aconnector->base);
4392 #if defined(CONFIG_DEBUG_FS)
4393 res = connector_debugfs_init(aconnector);
4395 DRM_ERROR("Failed to create debugfs for connector");
4398 aconnector->debugfs_dpcd_address = 0;
4399 aconnector->debugfs_dpcd_size = 0;
4402 if (connector_type == DRM_MODE_CONNECTOR_DisplayPort
4403 || connector_type == DRM_MODE_CONNECTOR_eDP)
4404 amdgpu_dm_initialize_dp_connector(dm, aconnector);
4409 aconnector->i2c = NULL;
4414 int amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device *adev)
4416 switch (adev->mode_info.num_crtc) {
4433 static int amdgpu_dm_encoder_init(struct drm_device *dev,
4434 struct amdgpu_encoder *aencoder,
4435 uint32_t link_index)
4437 struct amdgpu_device *adev = dev->dev_private;
4439 int res = drm_encoder_init(dev,
4441 &amdgpu_dm_encoder_funcs,
4442 DRM_MODE_ENCODER_TMDS,
4445 aencoder->base.possible_crtcs = amdgpu_dm_get_encoder_crtc_mask(adev);
4448 aencoder->encoder_id = link_index;
4450 aencoder->encoder_id = -1;
4452 drm_encoder_helper_add(&aencoder->base, &amdgpu_dm_encoder_helper_funcs);
4457 static void manage_dm_interrupts(struct amdgpu_device *adev,
4458 struct amdgpu_crtc *acrtc,
4462 * this is not correct translation but will work as soon as VBLANK
4463 * constant is the same as PFLIP
4466 amdgpu_display_crtc_idx_to_irq_type(
4471 drm_crtc_vblank_on(&acrtc->base);
4474 &adev->pageflip_irq,
4480 &adev->pageflip_irq,
4482 drm_crtc_vblank_off(&acrtc->base);
4487 is_scaling_state_different(const struct dm_connector_state *dm_state,
4488 const struct dm_connector_state *old_dm_state)
4490 if (dm_state->scaling != old_dm_state->scaling)
4492 if (!dm_state->underscan_enable && old_dm_state->underscan_enable) {
4493 if (old_dm_state->underscan_hborder != 0 && old_dm_state->underscan_vborder != 0)
4495 } else if (dm_state->underscan_enable && !old_dm_state->underscan_enable) {
4496 if (dm_state->underscan_hborder != 0 && dm_state->underscan_vborder != 0)
4498 } else if (dm_state->underscan_hborder != old_dm_state->underscan_hborder ||
4499 dm_state->underscan_vborder != old_dm_state->underscan_vborder)
4504 static void remove_stream(struct amdgpu_device *adev,
4505 struct amdgpu_crtc *acrtc,
4506 struct dc_stream_state *stream)
4508 /* this is the update mode case */
4510 acrtc->otg_inst = -1;
4511 acrtc->enabled = false;
4514 static int get_cursor_position(struct drm_plane *plane, struct drm_crtc *crtc,
4515 struct dc_cursor_position *position)
4517 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
4519 int xorigin = 0, yorigin = 0;
4521 if (!crtc || !plane->state->fb) {
4522 position->enable = false;
4528 if ((plane->state->crtc_w > amdgpu_crtc->max_cursor_width) ||
4529 (plane->state->crtc_h > amdgpu_crtc->max_cursor_height)) {
4530 DRM_ERROR("%s: bad cursor width or height %d x %d\n",
4532 plane->state->crtc_w,
4533 plane->state->crtc_h);
4537 x = plane->state->crtc_x;
4538 y = plane->state->crtc_y;
4539 /* avivo cursor are offset into the total surface */
4540 x += crtc->primary->state->src_x >> 16;
4541 y += crtc->primary->state->src_y >> 16;
4543 xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
4547 yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
4550 position->enable = true;
4553 position->x_hotspot = xorigin;
4554 position->y_hotspot = yorigin;
4559 static void handle_cursor_update(struct drm_plane *plane,
4560 struct drm_plane_state *old_plane_state)
4562 struct amdgpu_device *adev = plane->dev->dev_private;
4563 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(plane->state->fb);
4564 struct drm_crtc *crtc = afb ? plane->state->crtc : old_plane_state->crtc;
4565 struct dm_crtc_state *crtc_state = crtc ? to_dm_crtc_state(crtc->state) : NULL;
4566 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
4567 uint64_t address = afb ? afb->address : 0;
4568 struct dc_cursor_position position;
4569 struct dc_cursor_attributes attributes;
4572 if (!plane->state->fb && !old_plane_state->fb)
4575 DRM_DEBUG_DRIVER("%s: crtc_id=%d with size %d to %d\n",
4577 amdgpu_crtc->crtc_id,
4578 plane->state->crtc_w,
4579 plane->state->crtc_h);
4581 ret = get_cursor_position(plane, crtc, &position);
4585 if (!position.enable) {
4586 /* turn off cursor */
4587 if (crtc_state && crtc_state->stream) {
4588 mutex_lock(&adev->dm.dc_lock);
4589 dc_stream_set_cursor_position(crtc_state->stream,
4591 mutex_unlock(&adev->dm.dc_lock);
4596 amdgpu_crtc->cursor_width = plane->state->crtc_w;
4597 amdgpu_crtc->cursor_height = plane->state->crtc_h;
4599 attributes.address.high_part = upper_32_bits(address);
4600 attributes.address.low_part = lower_32_bits(address);
4601 attributes.width = plane->state->crtc_w;
4602 attributes.height = plane->state->crtc_h;
4603 attributes.color_format = CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA;
4604 attributes.rotation_angle = 0;
4605 attributes.attribute_flags.value = 0;
4607 attributes.pitch = attributes.width;
4609 if (crtc_state->stream) {
4610 mutex_lock(&adev->dm.dc_lock);
4611 if (!dc_stream_set_cursor_attributes(crtc_state->stream,
4613 DRM_ERROR("DC failed to set cursor attributes\n");
4615 if (!dc_stream_set_cursor_position(crtc_state->stream,
4617 DRM_ERROR("DC failed to set cursor position\n");
4618 mutex_unlock(&adev->dm.dc_lock);
4622 static void prepare_flip_isr(struct amdgpu_crtc *acrtc)
4625 assert_spin_locked(&acrtc->base.dev->event_lock);
4626 WARN_ON(acrtc->event);
4628 acrtc->event = acrtc->base.state->event;
4630 /* Set the flip status */
4631 acrtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
4633 /* Mark this event as consumed */
4634 acrtc->base.state->event = NULL;
4636 DRM_DEBUG_DRIVER("crtc:%d, pflip_stat:AMDGPU_FLIP_SUBMITTED\n",
4640 static void update_freesync_state_on_stream(
4641 struct amdgpu_display_manager *dm,
4642 struct dm_crtc_state *new_crtc_state,
4643 struct dc_stream_state *new_stream,
4644 struct dc_plane_state *surface,
4645 u32 flip_timestamp_in_us)
4647 struct mod_vrr_params vrr_params = new_crtc_state->vrr_params;
4648 struct dc_info_packet vrr_infopacket = {0};
4649 struct mod_freesync_config config = new_crtc_state->freesync_config;
4655 * TODO: Determine why min/max totals and vrefresh can be 0 here.
4656 * For now it's sufficient to just guard against these conditions.
4659 if (!new_stream->timing.h_total || !new_stream->timing.v_total)
4662 if (new_crtc_state->vrr_supported &&
4663 config.min_refresh_in_uhz &&
4664 config.max_refresh_in_uhz) {
4665 config.state = new_crtc_state->base.vrr_enabled ?
4666 VRR_STATE_ACTIVE_VARIABLE :
4669 config.state = VRR_STATE_UNSUPPORTED;
4672 mod_freesync_build_vrr_params(dm->freesync_module,
4674 &config, &vrr_params);
4677 mod_freesync_handle_preflip(
4678 dm->freesync_module,
4681 flip_timestamp_in_us,
4685 mod_freesync_build_vrr_infopacket(
4686 dm->freesync_module,
4690 TRANSFER_FUNC_UNKNOWN,
4693 new_crtc_state->freesync_timing_changed |=
4694 (memcmp(&new_crtc_state->vrr_params.adjust,
4696 sizeof(vrr_params.adjust)) != 0);
4698 new_crtc_state->freesync_vrr_info_changed |=
4699 (memcmp(&new_crtc_state->vrr_infopacket,
4701 sizeof(vrr_infopacket)) != 0);
4703 new_crtc_state->vrr_params = vrr_params;
4704 new_crtc_state->vrr_infopacket = vrr_infopacket;
4706 new_stream->adjust = new_crtc_state->vrr_params.adjust;
4707 new_stream->vrr_infopacket = vrr_infopacket;
4709 if (new_crtc_state->freesync_vrr_info_changed)
4710 DRM_DEBUG_KMS("VRR packet update: crtc=%u enabled=%d state=%d",
4711 new_crtc_state->base.crtc->base.id,
4712 (int)new_crtc_state->base.vrr_enabled,
4713 (int)vrr_params.state);
4716 static void amdgpu_dm_commit_planes(struct drm_atomic_state *state,
4717 struct dc_state *dc_state,
4718 struct drm_device *dev,
4719 struct amdgpu_display_manager *dm,
4720 struct drm_crtc *pcrtc,
4721 bool wait_for_vblank)
4724 uint64_t timestamp_ns;
4725 struct drm_plane *plane;
4726 struct drm_plane_state *old_plane_state, *new_plane_state;
4727 struct amdgpu_crtc *acrtc_attach = to_amdgpu_crtc(pcrtc);
4728 struct drm_crtc_state *new_pcrtc_state =
4729 drm_atomic_get_new_crtc_state(state, pcrtc);
4730 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(new_pcrtc_state);
4731 struct dm_crtc_state *dm_old_crtc_state =
4732 to_dm_crtc_state(drm_atomic_get_old_crtc_state(state, pcrtc));
4733 int planes_count = 0, vpos, hpos;
4734 unsigned long flags;
4735 struct amdgpu_bo *abo;
4736 uint64_t tiling_flags;
4737 uint32_t target, target_vblank;
4738 uint64_t last_flip_vblank;
4739 bool vrr_active = acrtc_state->freesync_config.state == VRR_STATE_ACTIVE_VARIABLE;
4740 bool pflip_present = false;
4743 struct dc_surface_update surface_updates[MAX_SURFACES];
4744 struct dc_plane_info plane_infos[MAX_SURFACES];
4745 struct dc_scaling_info scaling_infos[MAX_SURFACES];
4746 struct dc_flip_addrs flip_addrs[MAX_SURFACES];
4747 struct dc_stream_update stream_update;
4750 bundle = kzalloc(sizeof(*bundle), GFP_KERNEL);
4753 dm_error("Failed to allocate update bundle\n");
4757 /* update planes when needed */
4758 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
4759 struct drm_crtc *crtc = new_plane_state->crtc;
4760 struct drm_crtc_state *new_crtc_state;
4761 struct drm_framebuffer *fb = new_plane_state->fb;
4762 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(fb);
4763 bool plane_needs_flip;
4764 struct dc_plane_state *dc_plane;
4765 struct dm_plane_state *dm_new_plane_state = to_dm_plane_state(new_plane_state);
4767 /* Cursor plane is handled after stream updates */
4768 if (plane->type == DRM_PLANE_TYPE_CURSOR)
4771 if (!fb || !crtc || pcrtc != crtc)
4774 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
4775 if (!new_crtc_state->active)
4778 dc_plane = dm_new_plane_state->dc_state;
4780 bundle->surface_updates[planes_count].surface = dc_plane;
4781 if (new_pcrtc_state->color_mgmt_changed) {
4782 bundle->surface_updates[planes_count].gamma = dc_plane->gamma_correction;
4783 bundle->surface_updates[planes_count].in_transfer_func = dc_plane->in_transfer_func;
4787 bundle->scaling_infos[planes_count].scaling_quality = dc_plane->scaling_quality;
4788 bundle->scaling_infos[planes_count].src_rect = dc_plane->src_rect;
4789 bundle->scaling_infos[planes_count].dst_rect = dc_plane->dst_rect;
4790 bundle->scaling_infos[planes_count].clip_rect = dc_plane->clip_rect;
4791 bundle->surface_updates[planes_count].scaling_info = &bundle->scaling_infos[planes_count];
4794 bundle->plane_infos[planes_count].color_space = dc_plane->color_space;
4795 bundle->plane_infos[planes_count].format = dc_plane->format;
4796 bundle->plane_infos[planes_count].plane_size = dc_plane->plane_size;
4797 bundle->plane_infos[planes_count].rotation = dc_plane->rotation;
4798 bundle->plane_infos[planes_count].horizontal_mirror = dc_plane->horizontal_mirror;
4799 bundle->plane_infos[planes_count].stereo_format = dc_plane->stereo_format;
4800 bundle->plane_infos[planes_count].tiling_info = dc_plane->tiling_info;
4801 bundle->plane_infos[planes_count].visible = dc_plane->visible;
4802 bundle->plane_infos[planes_count].global_alpha = dc_plane->global_alpha;
4803 bundle->plane_infos[planes_count].global_alpha_value = dc_plane->global_alpha_value;
4804 bundle->plane_infos[planes_count].per_pixel_alpha = dc_plane->per_pixel_alpha;
4805 bundle->plane_infos[planes_count].dcc = dc_plane->dcc;
4806 bundle->surface_updates[planes_count].plane_info = &bundle->plane_infos[planes_count];
4808 plane_needs_flip = old_plane_state->fb && new_plane_state->fb;
4810 pflip_present = pflip_present || plane_needs_flip;
4812 if (!plane_needs_flip) {
4818 * TODO This might fail and hence better not used, wait
4819 * explicitly on fences instead
4820 * and in general should be called for
4821 * blocking commit to as per framework helpers
4823 abo = gem_to_amdgpu_bo(fb->obj[0]);
4824 r = amdgpu_bo_reserve(abo, true);
4825 if (unlikely(r != 0)) {
4826 DRM_ERROR("failed to reserve buffer before flip\n");
4830 /* Wait for all fences on this FB */
4831 WARN_ON(reservation_object_wait_timeout_rcu(abo->tbo.resv, true, false,
4832 MAX_SCHEDULE_TIMEOUT) < 0);
4834 amdgpu_bo_get_tiling_flags(abo, &tiling_flags);
4836 amdgpu_bo_unreserve(abo);
4838 fill_plane_tiling_attributes(dm->adev, afb, dc_plane,
4839 &bundle->plane_infos[planes_count].tiling_info,
4840 &bundle->plane_infos[planes_count].dcc,
4841 &bundle->flip_addrs[planes_count].address,
4844 bundle->flip_addrs[planes_count].flip_immediate =
4845 (crtc->state->pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC) != 0;
4847 timestamp_ns = ktime_get_ns();
4848 bundle->flip_addrs[planes_count].flip_timestamp_in_us = div_u64(timestamp_ns, 1000);
4849 bundle->surface_updates[planes_count].flip_addr = &bundle->flip_addrs[planes_count];
4850 bundle->surface_updates[planes_count].surface = dc_plane;
4852 if (!bundle->surface_updates[planes_count].surface) {
4853 DRM_ERROR("No surface for CRTC: id=%d\n",
4854 acrtc_attach->crtc_id);
4858 if (plane == pcrtc->primary)
4859 update_freesync_state_on_stream(
4862 acrtc_state->stream,
4864 bundle->flip_addrs[planes_count].flip_timestamp_in_us);
4866 DRM_DEBUG_DRIVER("%s Flipping to hi: 0x%x, low: 0x%x\n",
4868 bundle->flip_addrs[planes_count].address.grph.addr.high_part,
4869 bundle->flip_addrs[planes_count].address.grph.addr.low_part);
4875 if (pflip_present) {
4877 /* Use old throttling in non-vrr fixed refresh rate mode
4878 * to keep flip scheduling based on target vblank counts
4879 * working in a backwards compatible way, e.g., for
4880 * clients using the GLX_OML_sync_control extension or
4881 * DRI3/Present extension with defined target_msc.
4883 last_flip_vblank = drm_crtc_vblank_count(pcrtc);
4886 /* For variable refresh rate mode only:
4887 * Get vblank of last completed flip to avoid > 1 vrr
4888 * flips per video frame by use of throttling, but allow
4889 * flip programming anywhere in the possibly large
4890 * variable vrr vblank interval for fine-grained flip
4891 * timing control and more opportunity to avoid stutter
4892 * on late submission of flips.
4894 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
4895 last_flip_vblank = acrtc_attach->last_flip_vblank;
4896 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
4899 target = (uint32_t)last_flip_vblank + wait_for_vblank;
4901 /* Prepare wait for target vblank early - before the fence-waits */
4902 target_vblank = target - (uint32_t)drm_crtc_vblank_count(pcrtc) +
4903 amdgpu_get_vblank_counter_kms(pcrtc->dev, acrtc_attach->crtc_id);
4906 * Wait until we're out of the vertical blank period before the one
4907 * targeted by the flip
4909 while ((acrtc_attach->enabled &&
4910 (amdgpu_display_get_crtc_scanoutpos(dm->ddev, acrtc_attach->crtc_id,
4911 0, &vpos, &hpos, NULL,
4912 NULL, &pcrtc->hwmode)
4913 & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
4914 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
4915 (int)(target_vblank -
4916 amdgpu_get_vblank_counter_kms(dm->ddev, acrtc_attach->crtc_id)) > 0)) {
4917 usleep_range(1000, 1100);
4920 if (acrtc_attach->base.state->event) {
4921 drm_crtc_vblank_get(pcrtc);
4923 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
4925 WARN_ON(acrtc_attach->pflip_status != AMDGPU_FLIP_NONE);
4926 prepare_flip_isr(acrtc_attach);
4928 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
4931 if (acrtc_state->stream) {
4933 if (acrtc_state->freesync_timing_changed)
4934 bundle->stream_update.adjust =
4935 &acrtc_state->stream->adjust;
4937 if (acrtc_state->freesync_vrr_info_changed)
4938 bundle->stream_update.vrr_infopacket =
4939 &acrtc_state->stream->vrr_infopacket;
4944 if (new_pcrtc_state->mode_changed) {
4945 bundle->stream_update.src = acrtc_state->stream->src;
4946 bundle->stream_update.dst = acrtc_state->stream->dst;
4949 if (new_pcrtc_state->color_mgmt_changed)
4950 bundle->stream_update.out_transfer_func = acrtc_state->stream->out_transfer_func;
4952 acrtc_state->stream->abm_level = acrtc_state->abm_level;
4953 if (acrtc_state->abm_level != dm_old_crtc_state->abm_level)
4954 bundle->stream_update.abm_level = &acrtc_state->abm_level;
4956 mutex_lock(&dm->dc_lock);
4957 dc_commit_updates_for_stream(dm->dc,
4958 bundle->surface_updates,
4960 acrtc_state->stream,
4961 &bundle->stream_update,
4963 mutex_unlock(&dm->dc_lock);
4966 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
4967 if (plane->type == DRM_PLANE_TYPE_CURSOR)
4968 handle_cursor_update(plane, old_plane_state);
4975 * amdgpu_dm_crtc_copy_transient_flags - copy mirrored flags from DRM to DC
4976 * @crtc_state: the DRM CRTC state
4977 * @stream_state: the DC stream state.
4979 * Copy the mirrored transient state flags from DRM, to DC. It is used to bring
4980 * a dc_stream_state's flags in sync with a drm_crtc_state's flags.
4982 static void amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state *crtc_state,
4983 struct dc_stream_state *stream_state)
4985 stream_state->mode_changed = drm_atomic_crtc_needs_modeset(crtc_state);
4988 static int amdgpu_dm_atomic_commit(struct drm_device *dev,
4989 struct drm_atomic_state *state,
4992 struct drm_crtc *crtc;
4993 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
4994 struct amdgpu_device *adev = dev->dev_private;
4998 * We evade vblanks and pflips on crtc that
4999 * should be changed. We do it here to flush & disable
5000 * interrupts before drm_swap_state is called in drm_atomic_helper_commit
5001 * it will update crtc->dm_crtc_state->stream pointer which is used in
5004 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
5005 struct dm_crtc_state *dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
5006 struct dm_crtc_state *dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
5007 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
5009 if (drm_atomic_crtc_needs_modeset(new_crtc_state)
5010 && dm_old_crtc_state->stream) {
5012 * If the stream is removed and CRC capture was
5013 * enabled on the CRTC the extra vblank reference
5014 * needs to be dropped since CRC capture will be
5017 if (!dm_new_crtc_state->stream
5018 && dm_new_crtc_state->crc_enabled) {
5019 drm_crtc_vblank_put(crtc);
5020 dm_new_crtc_state->crc_enabled = false;
5023 manage_dm_interrupts(adev, acrtc, false);
5027 * Add check here for SoC's that support hardware cursor plane, to
5028 * unset legacy_cursor_update
5031 return drm_atomic_helper_commit(dev, state, nonblock);
5033 /*TODO Handle EINTR, reenable IRQ*/
5037 * amdgpu_dm_atomic_commit_tail() - AMDgpu DM's commit tail implementation.
5038 * @state: The atomic state to commit
5040 * This will tell DC to commit the constructed DC state from atomic_check,
5041 * programming the hardware. Any failures here implies a hardware failure, since
5042 * atomic check should have filtered anything non-kosher.
5044 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state)
5046 struct drm_device *dev = state->dev;
5047 struct amdgpu_device *adev = dev->dev_private;
5048 struct amdgpu_display_manager *dm = &adev->dm;
5049 struct dm_atomic_state *dm_state;
5050 struct dc_state *dc_state = NULL, *dc_state_temp = NULL;
5052 struct drm_crtc *crtc;
5053 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
5054 unsigned long flags;
5055 bool wait_for_vblank = true;
5056 struct drm_connector *connector;
5057 struct drm_connector_state *old_con_state, *new_con_state;
5058 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
5059 int crtc_disable_count = 0;
5061 drm_atomic_helper_update_legacy_modeset_state(dev, state);
5063 dm_state = dm_atomic_get_new_state(state);
5064 if (dm_state && dm_state->context) {
5065 dc_state = dm_state->context;
5067 /* No state changes, retain current state. */
5068 dc_state_temp = dc_create_state();
5069 ASSERT(dc_state_temp);
5070 dc_state = dc_state_temp;
5071 dc_resource_state_copy_construct_current(dm->dc, dc_state);
5074 /* update changed items */
5075 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
5076 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
5078 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
5079 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
5082 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
5083 "planes_changed:%d, mode_changed:%d,active_changed:%d,"
5084 "connectors_changed:%d\n",
5086 new_crtc_state->enable,
5087 new_crtc_state->active,
5088 new_crtc_state->planes_changed,
5089 new_crtc_state->mode_changed,
5090 new_crtc_state->active_changed,
5091 new_crtc_state->connectors_changed);
5093 /* Copy all transient state flags into dc state */
5094 if (dm_new_crtc_state->stream) {
5095 amdgpu_dm_crtc_copy_transient_flags(&dm_new_crtc_state->base,
5096 dm_new_crtc_state->stream);
5099 /* handles headless hotplug case, updating new_state and
5100 * aconnector as needed
5103 if (modeset_required(new_crtc_state, dm_new_crtc_state->stream, dm_old_crtc_state->stream)) {
5105 DRM_DEBUG_DRIVER("Atomic commit: SET crtc id %d: [%p]\n", acrtc->crtc_id, acrtc);
5107 if (!dm_new_crtc_state->stream) {
5109 * this could happen because of issues with
5110 * userspace notifications delivery.
5111 * In this case userspace tries to set mode on
5112 * display which is disconnected in fact.
5113 * dc_sink is NULL in this case on aconnector.
5114 * We expect reset mode will come soon.
5116 * This can also happen when unplug is done
5117 * during resume sequence ended
5119 * In this case, we want to pretend we still
5120 * have a sink to keep the pipe running so that
5121 * hw state is consistent with the sw state
5123 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
5124 __func__, acrtc->base.base.id);
5128 if (dm_old_crtc_state->stream)
5129 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
5131 pm_runtime_get_noresume(dev->dev);
5133 acrtc->enabled = true;
5134 acrtc->hw_mode = new_crtc_state->mode;
5135 crtc->hwmode = new_crtc_state->mode;
5136 } else if (modereset_required(new_crtc_state)) {
5137 DRM_DEBUG_DRIVER("Atomic commit: RESET. crtc id %d:[%p]\n", acrtc->crtc_id, acrtc);
5139 /* i.e. reset mode */
5140 if (dm_old_crtc_state->stream)
5141 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
5143 } /* for_each_crtc_in_state() */
5146 dm_enable_per_frame_crtc_master_sync(dc_state);
5147 mutex_lock(&dm->dc_lock);
5148 WARN_ON(!dc_commit_state(dm->dc, dc_state));
5149 mutex_unlock(&dm->dc_lock);
5152 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
5153 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
5155 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
5157 if (dm_new_crtc_state->stream != NULL) {
5158 const struct dc_stream_status *status =
5159 dc_stream_get_status(dm_new_crtc_state->stream);
5162 status = dc_stream_get_status_from_state(dc_state,
5163 dm_new_crtc_state->stream);
5166 DC_ERR("got no status for stream %p on acrtc%p\n", dm_new_crtc_state->stream, acrtc);
5168 acrtc->otg_inst = status->primary_otg_inst;
5172 /* Handle connector state changes */
5173 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
5174 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
5175 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
5176 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
5177 struct dc_surface_update dummy_updates[MAX_SURFACES];
5178 struct dc_stream_update stream_update;
5179 struct dc_stream_status *status = NULL;
5181 memset(&dummy_updates, 0, sizeof(dummy_updates));
5182 memset(&stream_update, 0, sizeof(stream_update));
5185 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
5186 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
5189 /* Skip any modesets/resets */
5190 if (!acrtc || drm_atomic_crtc_needs_modeset(new_crtc_state))
5193 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
5194 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
5196 if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state) &&
5197 (dm_new_crtc_state->abm_level == dm_old_crtc_state->abm_level))
5200 if (is_scaling_state_different(dm_new_con_state, dm_old_con_state)) {
5201 update_stream_scaling_settings(&dm_new_con_state->base.crtc->mode,
5202 dm_new_con_state, (struct dc_stream_state *)dm_new_crtc_state->stream);
5204 stream_update.src = dm_new_crtc_state->stream->src;
5205 stream_update.dst = dm_new_crtc_state->stream->dst;
5208 if (dm_new_crtc_state->abm_level != dm_old_crtc_state->abm_level) {
5209 dm_new_crtc_state->stream->abm_level = dm_new_crtc_state->abm_level;
5211 stream_update.abm_level = &dm_new_crtc_state->abm_level;
5214 status = dc_stream_get_status(dm_new_crtc_state->stream);
5216 WARN_ON(!status->plane_count);
5219 * TODO: DC refuses to perform stream updates without a dc_surface_update.
5220 * Here we create an empty update on each plane.
5221 * To fix this, DC should permit updating only stream properties.
5223 for (j = 0; j < status->plane_count; j++)
5224 dummy_updates[j].surface = status->plane_states[0];
5227 mutex_lock(&dm->dc_lock);
5228 dc_commit_updates_for_stream(dm->dc,
5230 status->plane_count,
5231 dm_new_crtc_state->stream,
5234 mutex_unlock(&dm->dc_lock);
5237 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
5238 new_crtc_state, i) {
5240 * loop to enable interrupts on newly arrived crtc
5242 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
5243 bool modeset_needed;
5245 if (old_crtc_state->active && !new_crtc_state->active)
5246 crtc_disable_count++;
5248 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
5249 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
5250 modeset_needed = modeset_required(
5252 dm_new_crtc_state->stream,
5253 dm_old_crtc_state->stream);
5255 if (dm_new_crtc_state->stream == NULL || !modeset_needed)
5258 manage_dm_interrupts(adev, acrtc, true);
5260 #ifdef CONFIG_DEBUG_FS
5261 /* The stream has changed so CRC capture needs to re-enabled. */
5262 if (dm_new_crtc_state->crc_enabled)
5263 amdgpu_dm_crtc_set_crc_source(crtc, "auto");
5267 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j)
5268 if (new_crtc_state->pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC)
5269 wait_for_vblank = false;
5271 /* update planes when needed per crtc*/
5272 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j) {
5273 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
5275 if (dm_new_crtc_state->stream)
5276 amdgpu_dm_commit_planes(state, dc_state, dev,
5277 dm, crtc, wait_for_vblank);
5282 * send vblank event on all events not handled in flip and
5283 * mark consumed event for drm_atomic_helper_commit_hw_done
5285 spin_lock_irqsave(&adev->ddev->event_lock, flags);
5286 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
5288 if (new_crtc_state->event)
5289 drm_send_event_locked(dev, &new_crtc_state->event->base);
5291 new_crtc_state->event = NULL;
5293 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
5295 /* Signal HW programming completion */
5296 drm_atomic_helper_commit_hw_done(state);
5298 if (wait_for_vblank)
5299 drm_atomic_helper_wait_for_flip_done(dev, state);
5301 drm_atomic_helper_cleanup_planes(dev, state);
5304 * Finally, drop a runtime PM reference for each newly disabled CRTC,
5305 * so we can put the GPU into runtime suspend if we're not driving any
5308 for (i = 0; i < crtc_disable_count; i++)
5309 pm_runtime_put_autosuspend(dev->dev);
5310 pm_runtime_mark_last_busy(dev->dev);
5313 dc_release_state(dc_state_temp);
5317 static int dm_force_atomic_commit(struct drm_connector *connector)
5320 struct drm_device *ddev = connector->dev;
5321 struct drm_atomic_state *state = drm_atomic_state_alloc(ddev);
5322 struct amdgpu_crtc *disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
5323 struct drm_plane *plane = disconnected_acrtc->base.primary;
5324 struct drm_connector_state *conn_state;
5325 struct drm_crtc_state *crtc_state;
5326 struct drm_plane_state *plane_state;
5331 state->acquire_ctx = ddev->mode_config.acquire_ctx;
5333 /* Construct an atomic state to restore previous display setting */
5336 * Attach connectors to drm_atomic_state
5338 conn_state = drm_atomic_get_connector_state(state, connector);
5340 ret = PTR_ERR_OR_ZERO(conn_state);
5344 /* Attach crtc to drm_atomic_state*/
5345 crtc_state = drm_atomic_get_crtc_state(state, &disconnected_acrtc->base);
5347 ret = PTR_ERR_OR_ZERO(crtc_state);
5351 /* force a restore */
5352 crtc_state->mode_changed = true;
5354 /* Attach plane to drm_atomic_state */
5355 plane_state = drm_atomic_get_plane_state(state, plane);
5357 ret = PTR_ERR_OR_ZERO(plane_state);
5362 /* Call commit internally with the state we just constructed */
5363 ret = drm_atomic_commit(state);
5368 DRM_ERROR("Restoring old state failed with %i\n", ret);
5369 drm_atomic_state_put(state);
5375 * This function handles all cases when set mode does not come upon hotplug.
5376 * This includes when a display is unplugged then plugged back into the
5377 * same port and when running without usermode desktop manager supprot
5379 void dm_restore_drm_connector_state(struct drm_device *dev,
5380 struct drm_connector *connector)
5382 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
5383 struct amdgpu_crtc *disconnected_acrtc;
5384 struct dm_crtc_state *acrtc_state;
5386 if (!aconnector->dc_sink || !connector->state || !connector->encoder)
5389 disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
5390 if (!disconnected_acrtc)
5393 acrtc_state = to_dm_crtc_state(disconnected_acrtc->base.state);
5394 if (!acrtc_state->stream)
5398 * If the previous sink is not released and different from the current,
5399 * we deduce we are in a state where we can not rely on usermode call
5400 * to turn on the display, so we do it here
5402 if (acrtc_state->stream->sink != aconnector->dc_sink)
5403 dm_force_atomic_commit(&aconnector->base);
5407 * Grabs all modesetting locks to serialize against any blocking commits,
5408 * Waits for completion of all non blocking commits.
5410 static int do_aquire_global_lock(struct drm_device *dev,
5411 struct drm_atomic_state *state)
5413 struct drm_crtc *crtc;
5414 struct drm_crtc_commit *commit;
5418 * Adding all modeset locks to aquire_ctx will
5419 * ensure that when the framework release it the
5420 * extra locks we are locking here will get released to
5422 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
5426 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5427 spin_lock(&crtc->commit_lock);
5428 commit = list_first_entry_or_null(&crtc->commit_list,
5429 struct drm_crtc_commit, commit_entry);
5431 drm_crtc_commit_get(commit);
5432 spin_unlock(&crtc->commit_lock);
5438 * Make sure all pending HW programming completed and
5441 ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ);
5444 ret = wait_for_completion_interruptible_timeout(
5445 &commit->flip_done, 10*HZ);
5448 DRM_ERROR("[CRTC:%d:%s] hw_done or flip_done "
5449 "timed out\n", crtc->base.id, crtc->name);
5451 drm_crtc_commit_put(commit);
5454 return ret < 0 ? ret : 0;
5457 static void get_freesync_config_for_crtc(
5458 struct dm_crtc_state *new_crtc_state,
5459 struct dm_connector_state *new_con_state)
5461 struct mod_freesync_config config = {0};
5462 struct amdgpu_dm_connector *aconnector =
5463 to_amdgpu_dm_connector(new_con_state->base.connector);
5464 struct drm_display_mode *mode = &new_crtc_state->base.mode;
5466 new_crtc_state->vrr_supported = new_con_state->freesync_capable &&
5467 aconnector->min_vfreq <= drm_mode_vrefresh(mode);
5469 if (new_crtc_state->vrr_supported) {
5470 new_crtc_state->stream->ignore_msa_timing_param = true;
5471 config.state = new_crtc_state->base.vrr_enabled ?
5472 VRR_STATE_ACTIVE_VARIABLE :
5474 config.min_refresh_in_uhz =
5475 aconnector->min_vfreq * 1000000;
5476 config.max_refresh_in_uhz =
5477 aconnector->max_vfreq * 1000000;
5478 config.vsif_supported = true;
5482 new_crtc_state->freesync_config = config;
5485 static void reset_freesync_config_for_crtc(
5486 struct dm_crtc_state *new_crtc_state)
5488 new_crtc_state->vrr_supported = false;
5490 memset(&new_crtc_state->vrr_params, 0,
5491 sizeof(new_crtc_state->vrr_params));
5492 memset(&new_crtc_state->vrr_infopacket, 0,
5493 sizeof(new_crtc_state->vrr_infopacket));
5496 static int dm_update_crtc_state(struct amdgpu_display_manager *dm,
5497 struct drm_atomic_state *state,
5498 struct drm_crtc *crtc,
5499 struct drm_crtc_state *old_crtc_state,
5500 struct drm_crtc_state *new_crtc_state,
5502 bool *lock_and_validation_needed)
5504 struct dm_atomic_state *dm_state = NULL;
5505 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
5506 struct dc_stream_state *new_stream;
5510 * TODO Move this code into dm_crtc_atomic_check once we get rid of dc_validation_set
5511 * update changed items
5513 struct amdgpu_crtc *acrtc = NULL;
5514 struct amdgpu_dm_connector *aconnector = NULL;
5515 struct drm_connector_state *drm_new_conn_state = NULL, *drm_old_conn_state = NULL;
5516 struct dm_connector_state *dm_new_conn_state = NULL, *dm_old_conn_state = NULL;
5517 struct drm_plane_state *new_plane_state = NULL;
5521 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
5522 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
5523 acrtc = to_amdgpu_crtc(crtc);
5525 new_plane_state = drm_atomic_get_new_plane_state(state, new_crtc_state->crtc->primary);
5527 if (new_crtc_state->enable && new_plane_state && !new_plane_state->fb) {
5532 aconnector = amdgpu_dm_find_first_crtc_matching_connector(state, crtc);
5534 /* TODO This hack should go away */
5535 if (aconnector && enable) {
5536 /* Make sure fake sink is created in plug-in scenario */
5537 drm_new_conn_state = drm_atomic_get_new_connector_state(state,
5539 drm_old_conn_state = drm_atomic_get_old_connector_state(state,
5542 if (IS_ERR(drm_new_conn_state)) {
5543 ret = PTR_ERR_OR_ZERO(drm_new_conn_state);
5547 dm_new_conn_state = to_dm_connector_state(drm_new_conn_state);
5548 dm_old_conn_state = to_dm_connector_state(drm_old_conn_state);
5550 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
5553 new_stream = create_stream_for_sink(aconnector,
5554 &new_crtc_state->mode,
5556 dm_old_crtc_state->stream);
5559 * we can have no stream on ACTION_SET if a display
5560 * was disconnected during S3, in this case it is not an
5561 * error, the OS will be updated after detection, and
5562 * will do the right thing on next atomic commit
5566 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
5567 __func__, acrtc->base.base.id);
5572 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
5574 if (dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) &&
5575 dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream)) {
5576 new_crtc_state->mode_changed = false;
5577 DRM_DEBUG_DRIVER("Mode change not required, setting mode_changed to %d",
5578 new_crtc_state->mode_changed);
5582 /* mode_changed flag may get updated above, need to check again */
5583 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
5587 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
5588 "planes_changed:%d, mode_changed:%d,active_changed:%d,"
5589 "connectors_changed:%d\n",
5591 new_crtc_state->enable,
5592 new_crtc_state->active,
5593 new_crtc_state->planes_changed,
5594 new_crtc_state->mode_changed,
5595 new_crtc_state->active_changed,
5596 new_crtc_state->connectors_changed);
5598 /* Remove stream for any changed/disabled CRTC */
5601 if (!dm_old_crtc_state->stream)
5604 ret = dm_atomic_get_state(state, &dm_state);
5608 DRM_DEBUG_DRIVER("Disabling DRM crtc: %d\n",
5611 /* i.e. reset mode */
5612 if (dc_remove_stream_from_ctx(
5615 dm_old_crtc_state->stream) != DC_OK) {
5620 dc_stream_release(dm_old_crtc_state->stream);
5621 dm_new_crtc_state->stream = NULL;
5623 reset_freesync_config_for_crtc(dm_new_crtc_state);
5625 *lock_and_validation_needed = true;
5627 } else {/* Add stream for any updated/enabled CRTC */
5629 * Quick fix to prevent NULL pointer on new_stream when
5630 * added MST connectors not found in existing crtc_state in the chained mode
5631 * TODO: need to dig out the root cause of that
5633 if (!aconnector || (!aconnector->dc_sink && aconnector->mst_port))
5636 if (modereset_required(new_crtc_state))
5639 if (modeset_required(new_crtc_state, new_stream,
5640 dm_old_crtc_state->stream)) {
5642 WARN_ON(dm_new_crtc_state->stream);
5644 ret = dm_atomic_get_state(state, &dm_state);
5648 dm_new_crtc_state->stream = new_stream;
5650 dc_stream_retain(new_stream);
5652 DRM_DEBUG_DRIVER("Enabling DRM crtc: %d\n",
5655 if (dc_add_stream_to_ctx(
5658 dm_new_crtc_state->stream) != DC_OK) {
5663 *lock_and_validation_needed = true;
5668 /* Release extra reference */
5670 dc_stream_release(new_stream);
5673 * We want to do dc stream updates that do not require a
5674 * full modeset below.
5676 if (!(enable && aconnector && new_crtc_state->enable &&
5677 new_crtc_state->active))
5680 * Given above conditions, the dc state cannot be NULL because:
5681 * 1. We're in the process of enabling CRTCs (just been added
5682 * to the dc context, or already is on the context)
5683 * 2. Has a valid connector attached, and
5684 * 3. Is currently active and enabled.
5685 * => The dc stream state currently exists.
5687 BUG_ON(dm_new_crtc_state->stream == NULL);
5689 /* Scaling or underscan settings */
5690 if (is_scaling_state_different(dm_old_conn_state, dm_new_conn_state))
5691 update_stream_scaling_settings(
5692 &new_crtc_state->mode, dm_new_conn_state, dm_new_crtc_state->stream);
5695 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
5698 * Color management settings. We also update color properties
5699 * when a modeset is needed, to ensure it gets reprogrammed.
5701 if (dm_new_crtc_state->base.color_mgmt_changed ||
5702 drm_atomic_crtc_needs_modeset(new_crtc_state)) {
5703 ret = amdgpu_dm_set_regamma_lut(dm_new_crtc_state);
5706 amdgpu_dm_set_ctm(dm_new_crtc_state);
5709 /* Update Freesync settings. */
5710 get_freesync_config_for_crtc(dm_new_crtc_state,
5717 dc_stream_release(new_stream);
5721 static int dm_update_plane_state(struct dc *dc,
5722 struct drm_atomic_state *state,
5723 struct drm_plane *plane,
5724 struct drm_plane_state *old_plane_state,
5725 struct drm_plane_state *new_plane_state,
5727 bool *lock_and_validation_needed)
5730 struct dm_atomic_state *dm_state = NULL;
5731 struct drm_crtc *new_plane_crtc, *old_plane_crtc;
5732 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
5733 struct dm_crtc_state *dm_new_crtc_state, *dm_old_crtc_state;
5734 struct dm_plane_state *dm_new_plane_state, *dm_old_plane_state;
5735 /* TODO return page_flip_needed() function */
5736 bool pflip_needed = !state->allow_modeset;
5740 new_plane_crtc = new_plane_state->crtc;
5741 old_plane_crtc = old_plane_state->crtc;
5742 dm_new_plane_state = to_dm_plane_state(new_plane_state);
5743 dm_old_plane_state = to_dm_plane_state(old_plane_state);
5745 /*TODO Implement atomic check for cursor plane */
5746 if (plane->type == DRM_PLANE_TYPE_CURSOR)
5749 /* Remove any changed/removed planes */
5752 plane->type != DRM_PLANE_TYPE_OVERLAY)
5755 if (!old_plane_crtc)
5758 old_crtc_state = drm_atomic_get_old_crtc_state(
5759 state, old_plane_crtc);
5760 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
5762 if (!dm_old_crtc_state->stream)
5765 DRM_DEBUG_ATOMIC("Disabling DRM plane: %d on DRM crtc %d\n",
5766 plane->base.id, old_plane_crtc->base.id);
5768 ret = dm_atomic_get_state(state, &dm_state);
5772 if (!dc_remove_plane_from_context(
5774 dm_old_crtc_state->stream,
5775 dm_old_plane_state->dc_state,
5776 dm_state->context)) {
5783 dc_plane_state_release(dm_old_plane_state->dc_state);
5784 dm_new_plane_state->dc_state = NULL;
5786 *lock_and_validation_needed = true;
5788 } else { /* Add new planes */
5789 struct dc_plane_state *dc_new_plane_state;
5791 if (drm_atomic_plane_disabling(plane->state, new_plane_state))
5794 if (!new_plane_crtc)
5797 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_crtc);
5798 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
5800 if (!dm_new_crtc_state->stream)
5803 if (pflip_needed && plane->type != DRM_PLANE_TYPE_OVERLAY)
5806 WARN_ON(dm_new_plane_state->dc_state);
5808 dc_new_plane_state = dc_create_plane_state(dc);
5809 if (!dc_new_plane_state)
5812 DRM_DEBUG_DRIVER("Enabling DRM plane: %d on DRM crtc %d\n",
5813 plane->base.id, new_plane_crtc->base.id);
5815 ret = fill_plane_attributes(
5816 new_plane_crtc->dev->dev_private,
5821 dc_plane_state_release(dc_new_plane_state);
5825 ret = dm_atomic_get_state(state, &dm_state);
5827 dc_plane_state_release(dc_new_plane_state);
5832 * Any atomic check errors that occur after this will
5833 * not need a release. The plane state will be attached
5834 * to the stream, and therefore part of the atomic
5835 * state. It'll be released when the atomic state is
5838 if (!dc_add_plane_to_context(
5840 dm_new_crtc_state->stream,
5842 dm_state->context)) {
5844 dc_plane_state_release(dc_new_plane_state);
5848 dm_new_plane_state->dc_state = dc_new_plane_state;
5850 /* Tell DC to do a full surface update every time there
5851 * is a plane change. Inefficient, but works for now.
5853 dm_new_plane_state->dc_state->update_flags.bits.full_update = 1;
5855 *lock_and_validation_needed = true;
5863 dm_determine_update_type_for_commit(struct dc *dc,
5864 struct drm_atomic_state *state,
5865 enum surface_update_type *out_type)
5867 struct dm_atomic_state *dm_state = NULL, *old_dm_state = NULL;
5868 int i, j, num_plane, ret = 0;
5869 struct drm_plane_state *old_plane_state, *new_plane_state;
5870 struct dm_plane_state *new_dm_plane_state, *old_dm_plane_state;
5871 struct drm_crtc *new_plane_crtc, *old_plane_crtc;
5872 struct drm_plane *plane;
5874 struct drm_crtc *crtc;
5875 struct drm_crtc_state *new_crtc_state, *old_crtc_state;
5876 struct dm_crtc_state *new_dm_crtc_state, *old_dm_crtc_state;
5877 struct dc_stream_status *status = NULL;
5879 struct dc_surface_update *updates;
5880 struct dc_plane_state *surface;
5881 enum surface_update_type update_type = UPDATE_TYPE_FAST;
5883 updates = kcalloc(MAX_SURFACES, sizeof(*updates), GFP_KERNEL);
5884 surface = kcalloc(MAX_SURFACES, sizeof(*surface), GFP_KERNEL);
5886 if (!updates || !surface) {
5887 DRM_ERROR("Plane or surface update failed to allocate");
5888 /* Set type to FULL to avoid crashing in DC*/
5889 update_type = UPDATE_TYPE_FULL;
5893 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
5894 struct dc_stream_update stream_update = { 0 };
5896 new_dm_crtc_state = to_dm_crtc_state(new_crtc_state);
5897 old_dm_crtc_state = to_dm_crtc_state(old_crtc_state);
5900 if (new_dm_crtc_state->stream != old_dm_crtc_state->stream) {
5901 update_type = UPDATE_TYPE_FULL;
5905 if (!new_dm_crtc_state->stream)
5908 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, j) {
5909 new_plane_crtc = new_plane_state->crtc;
5910 old_plane_crtc = old_plane_state->crtc;
5911 new_dm_plane_state = to_dm_plane_state(new_plane_state);
5912 old_dm_plane_state = to_dm_plane_state(old_plane_state);
5914 if (plane->type == DRM_PLANE_TYPE_CURSOR)
5917 if (new_dm_plane_state->dc_state != old_dm_plane_state->dc_state) {
5918 update_type = UPDATE_TYPE_FULL;
5922 if (!state->allow_modeset)
5925 if (crtc != new_plane_crtc)
5928 updates[num_plane].surface = &surface[num_plane];
5930 if (new_crtc_state->mode_changed) {
5931 updates[num_plane].surface->src_rect =
5932 new_dm_plane_state->dc_state->src_rect;
5933 updates[num_plane].surface->dst_rect =
5934 new_dm_plane_state->dc_state->dst_rect;
5935 updates[num_plane].surface->rotation =
5936 new_dm_plane_state->dc_state->rotation;
5937 updates[num_plane].surface->in_transfer_func =
5938 new_dm_plane_state->dc_state->in_transfer_func;
5939 stream_update.dst = new_dm_crtc_state->stream->dst;
5940 stream_update.src = new_dm_crtc_state->stream->src;
5943 if (new_crtc_state->color_mgmt_changed) {
5944 updates[num_plane].gamma =
5945 new_dm_plane_state->dc_state->gamma_correction;
5946 updates[num_plane].in_transfer_func =
5947 new_dm_plane_state->dc_state->in_transfer_func;
5948 stream_update.gamut_remap =
5949 &new_dm_crtc_state->stream->gamut_remap_matrix;
5950 stream_update.out_transfer_func =
5951 new_dm_crtc_state->stream->out_transfer_func;
5960 ret = dm_atomic_get_state(state, &dm_state);
5964 old_dm_state = dm_atomic_get_old_state(state);
5965 if (!old_dm_state) {
5970 status = dc_stream_get_status_from_state(old_dm_state->context,
5971 new_dm_crtc_state->stream);
5973 update_type = dc_check_update_surfaces_for_stream(dc, updates, num_plane,
5974 &stream_update, status);
5976 if (update_type > UPDATE_TYPE_MED) {
5977 update_type = UPDATE_TYPE_FULL;
5986 *out_type = update_type;
5991 * amdgpu_dm_atomic_check() - Atomic check implementation for AMDgpu DM.
5992 * @dev: The DRM device
5993 * @state: The atomic state to commit
5995 * Validate that the given atomic state is programmable by DC into hardware.
5996 * This involves constructing a &struct dc_state reflecting the new hardware
5997 * state we wish to commit, then querying DC to see if it is programmable. It's
5998 * important not to modify the existing DC state. Otherwise, atomic_check
5999 * may unexpectedly commit hardware changes.
6001 * When validating the DC state, it's important that the right locks are
6002 * acquired. For full updates case which removes/adds/updates streams on one
6003 * CRTC while flipping on another CRTC, acquiring global lock will guarantee
6004 * that any such full update commit will wait for completion of any outstanding
6005 * flip using DRMs synchronization events. See
6006 * dm_determine_update_type_for_commit()
6008 * Note that DM adds the affected connectors for all CRTCs in state, when that
6009 * might not seem necessary. This is because DC stream creation requires the
6010 * DC sink, which is tied to the DRM connector state. Cleaning this up should
6011 * be possible but non-trivial - a possible TODO item.
6013 * Return: -Error code if validation failed.
6015 static int amdgpu_dm_atomic_check(struct drm_device *dev,
6016 struct drm_atomic_state *state)
6018 struct amdgpu_device *adev = dev->dev_private;
6019 struct dm_atomic_state *dm_state = NULL;
6020 struct dc *dc = adev->dm.dc;
6021 struct drm_connector *connector;
6022 struct drm_connector_state *old_con_state, *new_con_state;
6023 struct drm_crtc *crtc;
6024 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
6025 struct drm_plane *plane;
6026 struct drm_plane_state *old_plane_state, *new_plane_state;
6027 enum surface_update_type update_type = UPDATE_TYPE_FAST;
6028 enum surface_update_type overall_update_type = UPDATE_TYPE_FAST;
6033 * This bool will be set for true for any modeset/reset
6034 * or plane update which implies non fast surface update.
6036 bool lock_and_validation_needed = false;
6038 ret = drm_atomic_helper_check_modeset(dev, state);
6042 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
6043 if (!drm_atomic_crtc_needs_modeset(new_crtc_state) &&
6044 !new_crtc_state->color_mgmt_changed &&
6045 old_crtc_state->vrr_enabled == new_crtc_state->vrr_enabled)
6048 if (!new_crtc_state->enable)
6051 ret = drm_atomic_add_affected_connectors(state, crtc);
6055 ret = drm_atomic_add_affected_planes(state, crtc);
6061 * Add all primary and overlay planes on the CRTC to the state
6062 * whenever a plane is enabled to maintain correct z-ordering
6063 * and to enable fast surface updates.
6065 drm_for_each_crtc(crtc, dev) {
6066 bool modified = false;
6068 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
6069 if (plane->type == DRM_PLANE_TYPE_CURSOR)
6072 if (new_plane_state->crtc == crtc ||
6073 old_plane_state->crtc == crtc) {
6082 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
6083 if (plane->type == DRM_PLANE_TYPE_CURSOR)
6087 drm_atomic_get_plane_state(state, plane);
6089 if (IS_ERR(new_plane_state)) {
6090 ret = PTR_ERR(new_plane_state);
6096 /* Remove exiting planes if they are modified */
6097 for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) {
6098 ret = dm_update_plane_state(dc, state, plane,
6102 &lock_and_validation_needed);
6107 /* Disable all crtcs which require disable */
6108 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
6109 ret = dm_update_crtc_state(&adev->dm, state, crtc,
6113 &lock_and_validation_needed);
6118 /* Enable all crtcs which require enable */
6119 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
6120 ret = dm_update_crtc_state(&adev->dm, state, crtc,
6124 &lock_and_validation_needed);
6129 /* Add new/modified planes */
6130 for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) {
6131 ret = dm_update_plane_state(dc, state, plane,
6135 &lock_and_validation_needed);
6140 /* Run this here since we want to validate the streams we created */
6141 ret = drm_atomic_helper_check_planes(dev, state);
6145 /* Check scaling and underscan changes*/
6146 /* TODO Removed scaling changes validation due to inability to commit
6147 * new stream into context w\o causing full reset. Need to
6148 * decide how to handle.
6150 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
6151 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
6152 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
6153 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
6155 /* Skip any modesets/resets */
6156 if (!acrtc || drm_atomic_crtc_needs_modeset(
6157 drm_atomic_get_new_crtc_state(state, &acrtc->base)))
6160 /* Skip any thing not scale or underscan changes */
6161 if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state))
6164 overall_update_type = UPDATE_TYPE_FULL;
6165 lock_and_validation_needed = true;
6168 ret = dm_determine_update_type_for_commit(dc, state, &update_type);
6172 if (overall_update_type < update_type)
6173 overall_update_type = update_type;
6176 * lock_and_validation_needed was an old way to determine if we need to set
6177 * the global lock. Leaving it in to check if we broke any corner cases
6178 * lock_and_validation_needed true = UPDATE_TYPE_FULL or UPDATE_TYPE_MED
6179 * lock_and_validation_needed false = UPDATE_TYPE_FAST
6181 if (lock_and_validation_needed && overall_update_type <= UPDATE_TYPE_FAST)
6182 WARN(1, "Global lock should be Set, overall_update_type should be UPDATE_TYPE_MED or UPDATE_TYPE_FULL");
6183 else if (!lock_and_validation_needed && overall_update_type > UPDATE_TYPE_FAST)
6184 WARN(1, "Global lock should NOT be set, overall_update_type should be UPDATE_TYPE_FAST");
6187 if (overall_update_type > UPDATE_TYPE_FAST) {
6188 ret = dm_atomic_get_state(state, &dm_state);
6192 ret = do_aquire_global_lock(dev, state);
6196 if (dc_validate_global_state(dc, dm_state->context) != DC_OK) {
6200 } else if (state->legacy_cursor_update) {
6202 * This is a fast cursor update coming from the plane update
6203 * helper, check if it can be done asynchronously for better
6206 state->async_update = !drm_atomic_helper_async_check(dev, state);
6209 /* Must be success */
6214 if (ret == -EDEADLK)
6215 DRM_DEBUG_DRIVER("Atomic check stopped to avoid deadlock.\n");
6216 else if (ret == -EINTR || ret == -EAGAIN || ret == -ERESTARTSYS)
6217 DRM_DEBUG_DRIVER("Atomic check stopped due to signal.\n");
6219 DRM_DEBUG_DRIVER("Atomic check failed with err: %d \n", ret);
6224 static bool is_dp_capable_without_timing_msa(struct dc *dc,
6225 struct amdgpu_dm_connector *amdgpu_dm_connector)
6228 bool capable = false;
6230 if (amdgpu_dm_connector->dc_link &&
6231 dm_helpers_dp_read_dpcd(
6233 amdgpu_dm_connector->dc_link,
6234 DP_DOWN_STREAM_PORT_COUNT,
6236 sizeof(dpcd_data))) {
6237 capable = (dpcd_data & DP_MSA_TIMING_PAR_IGNORED) ? true:false;
6242 void amdgpu_dm_update_freesync_caps(struct drm_connector *connector,
6246 bool edid_check_required;
6247 struct detailed_timing *timing;
6248 struct detailed_non_pixel *data;
6249 struct detailed_data_monitor_range *range;
6250 struct amdgpu_dm_connector *amdgpu_dm_connector =
6251 to_amdgpu_dm_connector(connector);
6252 struct dm_connector_state *dm_con_state = NULL;
6254 struct drm_device *dev = connector->dev;
6255 struct amdgpu_device *adev = dev->dev_private;
6256 bool freesync_capable = false;
6258 if (!connector->state) {
6259 DRM_ERROR("%s - Connector has no state", __func__);
6264 dm_con_state = to_dm_connector_state(connector->state);
6266 amdgpu_dm_connector->min_vfreq = 0;
6267 amdgpu_dm_connector->max_vfreq = 0;
6268 amdgpu_dm_connector->pixel_clock_mhz = 0;
6273 dm_con_state = to_dm_connector_state(connector->state);
6275 edid_check_required = false;
6276 if (!amdgpu_dm_connector->dc_sink) {
6277 DRM_ERROR("dc_sink NULL, could not add free_sync module.\n");
6280 if (!adev->dm.freesync_module)
6283 * if edid non zero restrict freesync only for dp and edp
6286 if (amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT
6287 || amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_EDP) {
6288 edid_check_required = is_dp_capable_without_timing_msa(
6290 amdgpu_dm_connector);
6293 if (edid_check_required == true && (edid->version > 1 ||
6294 (edid->version == 1 && edid->revision > 1))) {
6295 for (i = 0; i < 4; i++) {
6297 timing = &edid->detailed_timings[i];
6298 data = &timing->data.other_data;
6299 range = &data->data.range;
6301 * Check if monitor has continuous frequency mode
6303 if (data->type != EDID_DETAIL_MONITOR_RANGE)
6306 * Check for flag range limits only. If flag == 1 then
6307 * no additional timing information provided.
6308 * Default GTF, GTF Secondary curve and CVT are not
6311 if (range->flags != 1)
6314 amdgpu_dm_connector->min_vfreq = range->min_vfreq;
6315 amdgpu_dm_connector->max_vfreq = range->max_vfreq;
6316 amdgpu_dm_connector->pixel_clock_mhz =
6317 range->pixel_clock_mhz * 10;
6321 if (amdgpu_dm_connector->max_vfreq -
6322 amdgpu_dm_connector->min_vfreq > 10) {
6324 freesync_capable = true;
6330 dm_con_state->freesync_capable = freesync_capable;
6332 if (connector->vrr_capable_property)
6333 drm_connector_set_vrr_capable_property(connector,