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 #include "dm_services_types.h"
28 #include "dc/inc/core_types.h"
32 #include "amdgpu_display.h"
34 #include "amdgpu_dm.h"
35 #include "amdgpu_pm.h"
37 #include "amd_shared.h"
38 #include "amdgpu_dm_irq.h"
39 #include "dm_helpers.h"
40 #include "dm_services_types.h"
41 #include "amdgpu_dm_mst_types.h"
43 #include "ivsrcid/ivsrcid_vislands30.h"
45 #include <linux/module.h>
46 #include <linux/moduleparam.h>
47 #include <linux/version.h>
48 #include <linux/types.h>
51 #include <drm/drm_atomic.h>
52 #include <drm/drm_atomic_helper.h>
53 #include <drm/drm_dp_mst_helper.h>
54 #include <drm/drm_fb_helper.h>
55 #include <drm/drm_edid.h>
57 #include "modules/inc/mod_freesync.h"
59 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
60 #include "ivsrcid/irqsrcs_dcn_1_0.h"
62 #include "dcn/dcn_1_0_offset.h"
63 #include "dcn/dcn_1_0_sh_mask.h"
64 #include "soc15_hw_ip.h"
65 #include "vega10_ip_offset.h"
67 #include "soc15_common.h"
70 #include "modules/inc/mod_freesync.h"
72 #include "i2caux_interface.h"
74 /* basic init/fini API */
75 static int amdgpu_dm_init(struct amdgpu_device *adev);
76 static void amdgpu_dm_fini(struct amdgpu_device *adev);
78 /* initializes drm_device display related structures, based on the information
79 * provided by DAL. The drm strcutures are: drm_crtc, drm_connector,
80 * drm_encoder, drm_mode_config
82 * Returns 0 on success
84 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev);
85 /* removes and deallocates the drm structures, created by the above function */
86 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm);
89 amdgpu_dm_update_connector_after_detect(struct amdgpu_dm_connector *aconnector);
91 static int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
92 struct amdgpu_plane *aplane,
93 unsigned long possible_crtcs);
94 static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
95 struct drm_plane *plane,
97 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
98 struct amdgpu_dm_connector *amdgpu_dm_connector,
100 struct amdgpu_encoder *amdgpu_encoder);
101 static int amdgpu_dm_encoder_init(struct drm_device *dev,
102 struct amdgpu_encoder *aencoder,
103 uint32_t link_index);
105 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector);
107 static int amdgpu_dm_atomic_commit(struct drm_device *dev,
108 struct drm_atomic_state *state,
111 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state);
113 static int amdgpu_dm_atomic_check(struct drm_device *dev,
114 struct drm_atomic_state *state);
119 static const enum drm_plane_type dm_plane_type_default[AMDGPU_MAX_PLANES] = {
120 DRM_PLANE_TYPE_PRIMARY,
121 DRM_PLANE_TYPE_PRIMARY,
122 DRM_PLANE_TYPE_PRIMARY,
123 DRM_PLANE_TYPE_PRIMARY,
124 DRM_PLANE_TYPE_PRIMARY,
125 DRM_PLANE_TYPE_PRIMARY,
128 static const enum drm_plane_type dm_plane_type_carizzo[AMDGPU_MAX_PLANES] = {
129 DRM_PLANE_TYPE_PRIMARY,
130 DRM_PLANE_TYPE_PRIMARY,
131 DRM_PLANE_TYPE_PRIMARY,
132 DRM_PLANE_TYPE_OVERLAY,/* YUV Capable Underlay */
135 static const enum drm_plane_type dm_plane_type_stoney[AMDGPU_MAX_PLANES] = {
136 DRM_PLANE_TYPE_PRIMARY,
137 DRM_PLANE_TYPE_PRIMARY,
138 DRM_PLANE_TYPE_OVERLAY, /* YUV Capable Underlay */
142 * dm_vblank_get_counter
145 * Get counter for number of vertical blanks
148 * struct amdgpu_device *adev - [in] desired amdgpu device
149 * int disp_idx - [in] which CRTC to get the counter from
152 * Counter for vertical blanks
154 static u32 dm_vblank_get_counter(struct amdgpu_device *adev, int crtc)
156 if (crtc >= adev->mode_info.num_crtc)
159 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
160 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(
164 if (acrtc_state->stream == NULL) {
165 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
170 return dc_stream_get_vblank_counter(acrtc_state->stream);
174 static int dm_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
175 u32 *vbl, u32 *position)
177 uint32_t v_blank_start, v_blank_end, h_position, v_position;
179 if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
182 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
183 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(
186 if (acrtc_state->stream == NULL) {
187 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
193 * TODO rework base driver to use values directly.
194 * for now parse it back into reg-format
196 dc_stream_get_scanoutpos(acrtc_state->stream,
202 *position = v_position | (h_position << 16);
203 *vbl = v_blank_start | (v_blank_end << 16);
209 static bool dm_is_idle(void *handle)
215 static int dm_wait_for_idle(void *handle)
221 static bool dm_check_soft_reset(void *handle)
226 static int dm_soft_reset(void *handle)
232 static struct amdgpu_crtc *
233 get_crtc_by_otg_inst(struct amdgpu_device *adev,
236 struct drm_device *dev = adev->ddev;
237 struct drm_crtc *crtc;
238 struct amdgpu_crtc *amdgpu_crtc;
241 * following if is check inherited from both functions where this one is
242 * used now. Need to be checked why it could happen.
244 if (otg_inst == -1) {
246 return adev->mode_info.crtcs[0];
249 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
250 amdgpu_crtc = to_amdgpu_crtc(crtc);
252 if (amdgpu_crtc->otg_inst == otg_inst)
259 static void dm_pflip_high_irq(void *interrupt_params)
261 struct amdgpu_crtc *amdgpu_crtc;
262 struct common_irq_params *irq_params = interrupt_params;
263 struct amdgpu_device *adev = irq_params->adev;
266 amdgpu_crtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_PFLIP);
268 /* IRQ could occur when in initial stage */
269 /*TODO work and BO cleanup */
270 if (amdgpu_crtc == NULL) {
271 DRM_DEBUG_DRIVER("CRTC is null, returning.\n");
275 spin_lock_irqsave(&adev->ddev->event_lock, flags);
277 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED){
278 DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d !=AMDGPU_FLIP_SUBMITTED(%d) on crtc:%d[%p] \n",
279 amdgpu_crtc->pflip_status,
280 AMDGPU_FLIP_SUBMITTED,
281 amdgpu_crtc->crtc_id,
283 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
288 /* wakeup usersapce */
289 if (amdgpu_crtc->event) {
290 /* Update to correct count/ts if racing with vblank irq */
291 drm_crtc_accurate_vblank_count(&amdgpu_crtc->base);
293 drm_crtc_send_vblank_event(&amdgpu_crtc->base, amdgpu_crtc->event);
295 /* page flip completed. clean up */
296 amdgpu_crtc->event = NULL;
301 amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
302 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
304 DRM_DEBUG_DRIVER("%s - crtc :%d[%p], pflip_stat:AMDGPU_FLIP_NONE\n",
305 __func__, amdgpu_crtc->crtc_id, amdgpu_crtc);
307 drm_crtc_vblank_put(&amdgpu_crtc->base);
310 static void dm_crtc_high_irq(void *interrupt_params)
312 struct common_irq_params *irq_params = interrupt_params;
313 struct amdgpu_device *adev = irq_params->adev;
314 uint8_t crtc_index = 0;
315 struct amdgpu_crtc *acrtc;
317 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VBLANK);
320 crtc_index = acrtc->crtc_id;
322 drm_handle_vblank(adev->ddev, crtc_index);
323 amdgpu_dm_crtc_handle_crc_irq(&acrtc->base);
326 static int dm_set_clockgating_state(void *handle,
327 enum amd_clockgating_state state)
332 static int dm_set_powergating_state(void *handle,
333 enum amd_powergating_state state)
338 /* Prototypes of private functions */
339 static int dm_early_init(void* handle);
341 static void hotplug_notify_work_func(struct work_struct *work)
343 struct amdgpu_display_manager *dm = container_of(work, struct amdgpu_display_manager, mst_hotplug_work);
344 struct drm_device *dev = dm->ddev;
346 drm_kms_helper_hotplug_event(dev);
349 #if defined(CONFIG_DRM_AMD_DC_FBC)
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_VRAM, &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);
395 * Returns 0 on success
397 static int amdgpu_dm_init(struct amdgpu_device *adev)
399 struct dc_init_data init_data;
400 adev->dm.ddev = adev->ddev;
401 adev->dm.adev = adev;
403 /* Zero all the fields */
404 memset(&init_data, 0, sizeof(init_data));
406 if(amdgpu_dm_irq_init(adev)) {
407 DRM_ERROR("amdgpu: failed to initialize DM IRQ support.\n");
411 init_data.asic_id.chip_family = adev->family;
413 init_data.asic_id.pci_revision_id = adev->rev_id;
414 init_data.asic_id.hw_internal_rev = adev->external_rev_id;
416 init_data.asic_id.vram_width = adev->gmc.vram_width;
417 /* TODO: initialize init_data.asic_id.vram_type here!!!! */
418 init_data.asic_id.atombios_base_address =
419 adev->mode_info.atom_context->bios;
421 init_data.driver = adev;
423 adev->dm.cgs_device = amdgpu_cgs_create_device(adev);
425 if (!adev->dm.cgs_device) {
426 DRM_ERROR("amdgpu: failed to create cgs device.\n");
430 init_data.cgs_device = adev->dm.cgs_device;
434 init_data.dce_environment = DCE_ENV_PRODUCTION_DRV;
437 init_data.log_mask = DC_DEFAULT_LOG_MASK;
439 init_data.log_mask = DC_MIN_LOG_MASK;
442 * TODO debug why this doesn't work on Raven
444 if (adev->flags & AMD_IS_APU &&
445 adev->asic_type >= CHIP_CARRIZO &&
446 adev->asic_type < CHIP_RAVEN)
447 init_data.flags.gpu_vm_support = true;
449 /* Display Core create. */
450 adev->dm.dc = dc_create(&init_data);
453 DRM_INFO("Display Core initialized with v%s!\n", DC_VER);
455 DRM_INFO("Display Core failed to initialize with v%s!\n", DC_VER);
459 INIT_WORK(&adev->dm.mst_hotplug_work, hotplug_notify_work_func);
461 adev->dm.freesync_module = mod_freesync_create(adev->dm.dc);
462 if (!adev->dm.freesync_module) {
464 "amdgpu: failed to initialize freesync_module.\n");
466 DRM_DEBUG_DRIVER("amdgpu: freesync_module init done %p.\n",
467 adev->dm.freesync_module);
469 amdgpu_dm_init_color_mod();
471 if (amdgpu_dm_initialize_drm_device(adev)) {
473 "amdgpu: failed to initialize sw for display support.\n");
477 /* Update the actual used number of crtc */
478 adev->mode_info.num_crtc = adev->dm.display_indexes_num;
480 /* TODO: Add_display_info? */
482 /* TODO use dynamic cursor width */
483 adev->ddev->mode_config.cursor_width = adev->dm.dc->caps.max_cursor_size;
484 adev->ddev->mode_config.cursor_height = adev->dm.dc->caps.max_cursor_size;
486 if (drm_vblank_init(adev->ddev, adev->dm.display_indexes_num)) {
488 "amdgpu: failed to initialize sw for display support.\n");
492 DRM_DEBUG_DRIVER("KMS initialized.\n");
496 amdgpu_dm_fini(adev);
501 static void amdgpu_dm_fini(struct amdgpu_device *adev)
503 amdgpu_dm_destroy_drm_device(&adev->dm);
505 * TODO: pageflip, vlank interrupt
507 * amdgpu_dm_irq_fini(adev);
510 if (adev->dm.cgs_device) {
511 amdgpu_cgs_destroy_device(adev->dm.cgs_device);
512 adev->dm.cgs_device = NULL;
514 if (adev->dm.freesync_module) {
515 mod_freesync_destroy(adev->dm.freesync_module);
516 adev->dm.freesync_module = NULL;
518 /* DC Destroy TODO: Replace destroy DAL */
520 dc_destroy(&adev->dm.dc);
524 static int dm_sw_init(void *handle)
529 static int dm_sw_fini(void *handle)
534 static int detect_mst_link_for_all_connectors(struct drm_device *dev)
536 struct amdgpu_dm_connector *aconnector;
537 struct drm_connector *connector;
540 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
542 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
543 aconnector = to_amdgpu_dm_connector(connector);
544 if (aconnector->dc_link->type == dc_connection_mst_branch &&
545 aconnector->mst_mgr.aux) {
546 DRM_DEBUG_DRIVER("DM_MST: starting TM on aconnector: %p [id: %d]\n",
547 aconnector, aconnector->base.base.id);
549 ret = drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true);
551 DRM_ERROR("DM_MST: Failed to start MST\n");
552 ((struct dc_link *)aconnector->dc_link)->type = dc_connection_single;
558 drm_modeset_unlock(&dev->mode_config.connection_mutex);
562 static int dm_late_init(void *handle)
564 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
566 return detect_mst_link_for_all_connectors(adev->ddev);
569 static void s3_handle_mst(struct drm_device *dev, bool suspend)
571 struct amdgpu_dm_connector *aconnector;
572 struct drm_connector *connector;
574 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
576 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
577 aconnector = to_amdgpu_dm_connector(connector);
578 if (aconnector->dc_link->type == dc_connection_mst_branch &&
579 !aconnector->mst_port) {
582 drm_dp_mst_topology_mgr_suspend(&aconnector->mst_mgr);
584 drm_dp_mst_topology_mgr_resume(&aconnector->mst_mgr);
588 drm_modeset_unlock(&dev->mode_config.connection_mutex);
591 static int dm_hw_init(void *handle)
593 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
594 /* Create DAL display manager */
595 amdgpu_dm_init(adev);
596 amdgpu_dm_hpd_init(adev);
601 static int dm_hw_fini(void *handle)
603 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
605 amdgpu_dm_hpd_fini(adev);
607 amdgpu_dm_irq_fini(adev);
608 amdgpu_dm_fini(adev);
612 static int dm_suspend(void *handle)
614 struct amdgpu_device *adev = handle;
615 struct amdgpu_display_manager *dm = &adev->dm;
618 s3_handle_mst(adev->ddev, true);
620 amdgpu_dm_irq_suspend(adev);
622 WARN_ON(adev->dm.cached_state);
623 adev->dm.cached_state = drm_atomic_helper_suspend(adev->ddev);
625 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D3);
630 static struct amdgpu_dm_connector *
631 amdgpu_dm_find_first_crtc_matching_connector(struct drm_atomic_state *state,
632 struct drm_crtc *crtc)
635 struct drm_connector_state *new_con_state;
636 struct drm_connector *connector;
637 struct drm_crtc *crtc_from_state;
639 for_each_new_connector_in_state(state, connector, new_con_state, i) {
640 crtc_from_state = new_con_state->crtc;
642 if (crtc_from_state == crtc)
643 return to_amdgpu_dm_connector(connector);
649 static int dm_resume(void *handle)
651 struct amdgpu_device *adev = handle;
652 struct amdgpu_display_manager *dm = &adev->dm;
655 /* power on hardware */
656 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0);
658 ret = amdgpu_dm_display_resume(adev);
662 int amdgpu_dm_display_resume(struct amdgpu_device *adev)
664 struct drm_device *ddev = adev->ddev;
665 struct amdgpu_display_manager *dm = &adev->dm;
666 struct amdgpu_dm_connector *aconnector;
667 struct drm_connector *connector;
668 struct drm_crtc *crtc;
669 struct drm_crtc_state *new_crtc_state;
670 struct dm_crtc_state *dm_new_crtc_state;
671 struct drm_plane *plane;
672 struct drm_plane_state *new_plane_state;
673 struct dm_plane_state *dm_new_plane_state;
678 /* program HPD filter */
681 /* On resume we need to rewrite the MSTM control bits to enamble MST*/
682 s3_handle_mst(ddev, false);
685 * early enable HPD Rx IRQ, should be done before set mode as short
686 * pulse interrupts are used for MST
688 amdgpu_dm_irq_resume_early(adev);
691 list_for_each_entry(connector,
692 &ddev->mode_config.connector_list, head) {
693 aconnector = to_amdgpu_dm_connector(connector);
696 * this is the case when traversing through already created
697 * MST connectors, should be skipped
699 if (aconnector->mst_port)
702 mutex_lock(&aconnector->hpd_lock);
703 dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
705 if (aconnector->fake_enable && aconnector->dc_link->local_sink)
706 aconnector->fake_enable = false;
708 aconnector->dc_sink = NULL;
709 amdgpu_dm_update_connector_after_detect(aconnector);
710 mutex_unlock(&aconnector->hpd_lock);
713 /* Force mode set in atomic comit */
714 for_each_new_crtc_in_state(adev->dm.cached_state, crtc, new_crtc_state, i)
715 new_crtc_state->active_changed = true;
718 * atomic_check is expected to create the dc states. We need to release
719 * them here, since they were duplicated as part of the suspend
722 for_each_new_crtc_in_state(adev->dm.cached_state, crtc, new_crtc_state, i) {
723 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
724 if (dm_new_crtc_state->stream) {
725 WARN_ON(kref_read(&dm_new_crtc_state->stream->refcount) > 1);
726 dc_stream_release(dm_new_crtc_state->stream);
727 dm_new_crtc_state->stream = NULL;
731 for_each_new_plane_in_state(adev->dm.cached_state, plane, new_plane_state, i) {
732 dm_new_plane_state = to_dm_plane_state(new_plane_state);
733 if (dm_new_plane_state->dc_state) {
734 WARN_ON(kref_read(&dm_new_plane_state->dc_state->refcount) > 1);
735 dc_plane_state_release(dm_new_plane_state->dc_state);
736 dm_new_plane_state->dc_state = NULL;
740 ret = drm_atomic_helper_resume(ddev, adev->dm.cached_state);
742 adev->dm.cached_state = NULL;
744 amdgpu_dm_irq_resume_late(adev);
749 static const struct amd_ip_funcs amdgpu_dm_funcs = {
751 .early_init = dm_early_init,
752 .late_init = dm_late_init,
753 .sw_init = dm_sw_init,
754 .sw_fini = dm_sw_fini,
755 .hw_init = dm_hw_init,
756 .hw_fini = dm_hw_fini,
757 .suspend = dm_suspend,
759 .is_idle = dm_is_idle,
760 .wait_for_idle = dm_wait_for_idle,
761 .check_soft_reset = dm_check_soft_reset,
762 .soft_reset = dm_soft_reset,
763 .set_clockgating_state = dm_set_clockgating_state,
764 .set_powergating_state = dm_set_powergating_state,
767 const struct amdgpu_ip_block_version dm_ip_block =
769 .type = AMD_IP_BLOCK_TYPE_DCE,
773 .funcs = &amdgpu_dm_funcs,
777 static struct drm_atomic_state *
778 dm_atomic_state_alloc(struct drm_device *dev)
780 struct dm_atomic_state *state = kzalloc(sizeof(*state), GFP_KERNEL);
785 if (drm_atomic_state_init(dev, &state->base) < 0)
796 dm_atomic_state_clear(struct drm_atomic_state *state)
798 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
800 if (dm_state->context) {
801 dc_release_state(dm_state->context);
802 dm_state->context = NULL;
805 drm_atomic_state_default_clear(state);
809 dm_atomic_state_alloc_free(struct drm_atomic_state *state)
811 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
812 drm_atomic_state_default_release(state);
816 static const struct drm_mode_config_funcs amdgpu_dm_mode_funcs = {
817 .fb_create = amdgpu_display_user_framebuffer_create,
818 .output_poll_changed = drm_fb_helper_output_poll_changed,
819 .atomic_check = amdgpu_dm_atomic_check,
820 .atomic_commit = amdgpu_dm_atomic_commit,
821 .atomic_state_alloc = dm_atomic_state_alloc,
822 .atomic_state_clear = dm_atomic_state_clear,
823 .atomic_state_free = dm_atomic_state_alloc_free
826 static struct drm_mode_config_helper_funcs amdgpu_dm_mode_config_helperfuncs = {
827 .atomic_commit_tail = amdgpu_dm_atomic_commit_tail
831 amdgpu_dm_update_connector_after_detect(struct amdgpu_dm_connector *aconnector)
833 struct drm_connector *connector = &aconnector->base;
834 struct drm_device *dev = connector->dev;
835 struct dc_sink *sink;
837 /* MST handled by drm_mst framework */
838 if (aconnector->mst_mgr.mst_state == true)
842 sink = aconnector->dc_link->local_sink;
844 /* Edid mgmt connector gets first update only in mode_valid hook and then
845 * the connector sink is set to either fake or physical sink depends on link status.
846 * don't do it here if u are during boot
848 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED
849 && aconnector->dc_em_sink) {
851 /* For S3 resume with headless use eml_sink to fake stream
852 * because on resume connecotr->sink is set ti NULL
854 mutex_lock(&dev->mode_config.mutex);
857 if (aconnector->dc_sink) {
858 amdgpu_dm_remove_sink_from_freesync_module(
860 /* retain and release bellow are used for
861 * bump up refcount for sink because the link don't point
862 * to it anymore after disconnect so on next crtc to connector
863 * reshuffle by UMD we will get into unwanted dc_sink release
865 if (aconnector->dc_sink != aconnector->dc_em_sink)
866 dc_sink_release(aconnector->dc_sink);
868 aconnector->dc_sink = sink;
869 amdgpu_dm_add_sink_to_freesync_module(
870 connector, aconnector->edid);
872 amdgpu_dm_remove_sink_from_freesync_module(connector);
873 if (!aconnector->dc_sink)
874 aconnector->dc_sink = aconnector->dc_em_sink;
875 else if (aconnector->dc_sink != aconnector->dc_em_sink)
876 dc_sink_retain(aconnector->dc_sink);
879 mutex_unlock(&dev->mode_config.mutex);
884 * TODO: temporary guard to look for proper fix
885 * if this sink is MST sink, we should not do anything
887 if (sink && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
890 if (aconnector->dc_sink == sink) {
891 /* We got a DP short pulse (Link Loss, DP CTS, etc...).
893 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: dc_sink didn't change.\n",
894 aconnector->connector_id);
898 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: Old sink=%p New sink=%p\n",
899 aconnector->connector_id, aconnector->dc_sink, sink);
901 mutex_lock(&dev->mode_config.mutex);
903 /* 1. Update status of the drm connector
904 * 2. Send an event and let userspace tell us what to do */
906 /* TODO: check if we still need the S3 mode update workaround.
907 * If yes, put it here. */
908 if (aconnector->dc_sink)
909 amdgpu_dm_remove_sink_from_freesync_module(
912 aconnector->dc_sink = sink;
913 if (sink->dc_edid.length == 0) {
914 aconnector->edid = NULL;
917 (struct edid *) sink->dc_edid.raw_edid;
920 drm_mode_connector_update_edid_property(connector,
923 amdgpu_dm_add_sink_to_freesync_module(connector, aconnector->edid);
926 amdgpu_dm_remove_sink_from_freesync_module(connector);
927 drm_mode_connector_update_edid_property(connector, NULL);
928 aconnector->num_modes = 0;
929 aconnector->dc_sink = NULL;
932 mutex_unlock(&dev->mode_config.mutex);
935 static void handle_hpd_irq(void *param)
937 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
938 struct drm_connector *connector = &aconnector->base;
939 struct drm_device *dev = connector->dev;
941 /* In case of failure or MST no need to update connector status or notify the OS
942 * since (for MST case) MST does this in it's own context.
944 mutex_lock(&aconnector->hpd_lock);
946 if (aconnector->fake_enable)
947 aconnector->fake_enable = false;
949 if (dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD)) {
950 amdgpu_dm_update_connector_after_detect(aconnector);
953 drm_modeset_lock_all(dev);
954 dm_restore_drm_connector_state(dev, connector);
955 drm_modeset_unlock_all(dev);
957 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
958 drm_kms_helper_hotplug_event(dev);
960 mutex_unlock(&aconnector->hpd_lock);
964 static void dm_handle_hpd_rx_irq(struct amdgpu_dm_connector *aconnector)
966 uint8_t esi[DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI] = { 0 };
968 bool new_irq_handled = false;
970 int dpcd_bytes_to_read;
972 const int max_process_count = 30;
973 int process_count = 0;
975 const struct dc_link_status *link_status = dc_link_get_status(aconnector->dc_link);
977 if (link_status->dpcd_caps->dpcd_rev.raw < 0x12) {
978 dpcd_bytes_to_read = DP_LANE0_1_STATUS - DP_SINK_COUNT;
979 /* DPCD 0x200 - 0x201 for downstream IRQ */
980 dpcd_addr = DP_SINK_COUNT;
982 dpcd_bytes_to_read = DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI;
983 /* DPCD 0x2002 - 0x2005 for downstream IRQ */
984 dpcd_addr = DP_SINK_COUNT_ESI;
987 dret = drm_dp_dpcd_read(
988 &aconnector->dm_dp_aux.aux,
993 while (dret == dpcd_bytes_to_read &&
994 process_count < max_process_count) {
1000 DRM_DEBUG_DRIVER("ESI %02x %02x %02x\n", esi[0], esi[1], esi[2]);
1001 /* handle HPD short pulse irq */
1002 if (aconnector->mst_mgr.mst_state)
1004 &aconnector->mst_mgr,
1008 if (new_irq_handled) {
1009 /* ACK at DPCD to notify down stream */
1010 const int ack_dpcd_bytes_to_write =
1011 dpcd_bytes_to_read - 1;
1013 for (retry = 0; retry < 3; retry++) {
1016 wret = drm_dp_dpcd_write(
1017 &aconnector->dm_dp_aux.aux,
1020 ack_dpcd_bytes_to_write);
1021 if (wret == ack_dpcd_bytes_to_write)
1025 /* check if there is new irq to be handle */
1026 dret = drm_dp_dpcd_read(
1027 &aconnector->dm_dp_aux.aux,
1030 dpcd_bytes_to_read);
1032 new_irq_handled = false;
1038 if (process_count == max_process_count)
1039 DRM_DEBUG_DRIVER("Loop exceeded max iterations\n");
1042 static void handle_hpd_rx_irq(void *param)
1044 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
1045 struct drm_connector *connector = &aconnector->base;
1046 struct drm_device *dev = connector->dev;
1047 struct dc_link *dc_link = aconnector->dc_link;
1048 bool is_mst_root_connector = aconnector->mst_mgr.mst_state;
1050 /* TODO:Temporary add mutex to protect hpd interrupt not have a gpio
1051 * conflict, after implement i2c helper, this mutex should be
1054 if (dc_link->type != dc_connection_mst_branch)
1055 mutex_lock(&aconnector->hpd_lock);
1057 if (dc_link_handle_hpd_rx_irq(dc_link, NULL) &&
1058 !is_mst_root_connector) {
1059 /* Downstream Port status changed. */
1060 if (dc_link_detect(dc_link, DETECT_REASON_HPDRX)) {
1061 amdgpu_dm_update_connector_after_detect(aconnector);
1064 drm_modeset_lock_all(dev);
1065 dm_restore_drm_connector_state(dev, connector);
1066 drm_modeset_unlock_all(dev);
1068 drm_kms_helper_hotplug_event(dev);
1071 if ((dc_link->cur_link_settings.lane_count != LANE_COUNT_UNKNOWN) ||
1072 (dc_link->type == dc_connection_mst_branch))
1073 dm_handle_hpd_rx_irq(aconnector);
1075 if (dc_link->type != dc_connection_mst_branch)
1076 mutex_unlock(&aconnector->hpd_lock);
1079 static void register_hpd_handlers(struct amdgpu_device *adev)
1081 struct drm_device *dev = adev->ddev;
1082 struct drm_connector *connector;
1083 struct amdgpu_dm_connector *aconnector;
1084 const struct dc_link *dc_link;
1085 struct dc_interrupt_params int_params = {0};
1087 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
1088 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
1090 list_for_each_entry(connector,
1091 &dev->mode_config.connector_list, head) {
1093 aconnector = to_amdgpu_dm_connector(connector);
1094 dc_link = aconnector->dc_link;
1096 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd) {
1097 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
1098 int_params.irq_source = dc_link->irq_source_hpd;
1100 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1102 (void *) aconnector);
1105 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd_rx) {
1107 /* Also register for DP short pulse (hpd_rx). */
1108 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
1109 int_params.irq_source = dc_link->irq_source_hpd_rx;
1111 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1113 (void *) aconnector);
1118 /* Register IRQ sources and initialize IRQ callbacks */
1119 static int dce110_register_irq_handlers(struct amdgpu_device *adev)
1121 struct dc *dc = adev->dm.dc;
1122 struct common_irq_params *c_irq_params;
1123 struct dc_interrupt_params int_params = {0};
1126 unsigned client_id = AMDGPU_IH_CLIENTID_LEGACY;
1128 if (adev->asic_type == CHIP_VEGA10 ||
1129 adev->asic_type == CHIP_RAVEN)
1130 client_id = AMDGPU_IH_CLIENTID_DCE;
1132 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
1133 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
1135 /* Actions of amdgpu_irq_add_id():
1136 * 1. Register a set() function with base driver.
1137 * Base driver will call set() function to enable/disable an
1138 * interrupt in DC hardware.
1139 * 2. Register amdgpu_dm_irq_handler().
1140 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
1141 * coming from DC hardware.
1142 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
1143 * for acknowledging and handling. */
1145 /* Use VBLANK interrupt */
1146 for (i = VISLANDS30_IV_SRCID_D1_VERTICAL_INTERRUPT0; i <= VISLANDS30_IV_SRCID_D6_VERTICAL_INTERRUPT0; i++) {
1147 r = amdgpu_irq_add_id(adev, client_id, i, &adev->crtc_irq);
1149 DRM_ERROR("Failed to add crtc irq id!\n");
1153 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1154 int_params.irq_source =
1155 dc_interrupt_to_irq_source(dc, i, 0);
1157 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
1159 c_irq_params->adev = adev;
1160 c_irq_params->irq_src = int_params.irq_source;
1162 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1163 dm_crtc_high_irq, c_irq_params);
1166 /* Use GRPH_PFLIP interrupt */
1167 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
1168 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
1169 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq);
1171 DRM_ERROR("Failed to add page flip irq id!\n");
1175 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1176 int_params.irq_source =
1177 dc_interrupt_to_irq_source(dc, i, 0);
1179 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
1181 c_irq_params->adev = adev;
1182 c_irq_params->irq_src = int_params.irq_source;
1184 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1185 dm_pflip_high_irq, c_irq_params);
1190 r = amdgpu_irq_add_id(adev, client_id,
1191 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
1193 DRM_ERROR("Failed to add hpd irq id!\n");
1197 register_hpd_handlers(adev);
1202 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
1203 /* Register IRQ sources and initialize IRQ callbacks */
1204 static int dcn10_register_irq_handlers(struct amdgpu_device *adev)
1206 struct dc *dc = adev->dm.dc;
1207 struct common_irq_params *c_irq_params;
1208 struct dc_interrupt_params int_params = {0};
1212 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
1213 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
1215 /* Actions of amdgpu_irq_add_id():
1216 * 1. Register a set() function with base driver.
1217 * Base driver will call set() function to enable/disable an
1218 * interrupt in DC hardware.
1219 * 2. Register amdgpu_dm_irq_handler().
1220 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
1221 * coming from DC hardware.
1222 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
1223 * for acknowledging and handling.
1226 /* Use VSTARTUP interrupt */
1227 for (i = DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP;
1228 i <= DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP + adev->mode_info.num_crtc - 1;
1230 r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_DCE, i, &adev->crtc_irq);
1233 DRM_ERROR("Failed to add crtc irq id!\n");
1237 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1238 int_params.irq_source =
1239 dc_interrupt_to_irq_source(dc, i, 0);
1241 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
1243 c_irq_params->adev = adev;
1244 c_irq_params->irq_src = int_params.irq_source;
1246 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1247 dm_crtc_high_irq, c_irq_params);
1250 /* Use GRPH_PFLIP interrupt */
1251 for (i = DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT;
1252 i <= DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT + adev->mode_info.num_crtc - 1;
1254 r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_DCE, i, &adev->pageflip_irq);
1256 DRM_ERROR("Failed to add page flip irq id!\n");
1260 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1261 int_params.irq_source =
1262 dc_interrupt_to_irq_source(dc, i, 0);
1264 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
1266 c_irq_params->adev = adev;
1267 c_irq_params->irq_src = int_params.irq_source;
1269 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1270 dm_pflip_high_irq, c_irq_params);
1275 r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_DCE, DCN_1_0__SRCID__DC_HPD1_INT,
1278 DRM_ERROR("Failed to add hpd irq id!\n");
1282 register_hpd_handlers(adev);
1288 static int amdgpu_dm_mode_config_init(struct amdgpu_device *adev)
1292 adev->mode_info.mode_config_initialized = true;
1294 adev->ddev->mode_config.funcs = (void *)&amdgpu_dm_mode_funcs;
1295 adev->ddev->mode_config.helper_private = &amdgpu_dm_mode_config_helperfuncs;
1297 adev->ddev->mode_config.max_width = 16384;
1298 adev->ddev->mode_config.max_height = 16384;
1300 adev->ddev->mode_config.preferred_depth = 24;
1301 adev->ddev->mode_config.prefer_shadow = 1;
1302 /* indicate support of immediate flip */
1303 adev->ddev->mode_config.async_page_flip = true;
1305 adev->ddev->mode_config.fb_base = adev->gmc.aper_base;
1307 r = amdgpu_display_modeset_create_props(adev);
1314 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
1315 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
1317 static int amdgpu_dm_backlight_update_status(struct backlight_device *bd)
1319 struct amdgpu_display_manager *dm = bl_get_data(bd);
1321 if (dc_link_set_backlight_level(dm->backlight_link,
1322 bd->props.brightness, 0, 0))
1328 static int amdgpu_dm_backlight_get_brightness(struct backlight_device *bd)
1330 return bd->props.brightness;
1333 static const struct backlight_ops amdgpu_dm_backlight_ops = {
1334 .get_brightness = amdgpu_dm_backlight_get_brightness,
1335 .update_status = amdgpu_dm_backlight_update_status,
1339 amdgpu_dm_register_backlight_device(struct amdgpu_display_manager *dm)
1342 struct backlight_properties props = { 0 };
1344 props.max_brightness = AMDGPU_MAX_BL_LEVEL;
1345 props.type = BACKLIGHT_RAW;
1347 snprintf(bl_name, sizeof(bl_name), "amdgpu_bl%d",
1348 dm->adev->ddev->primary->index);
1350 dm->backlight_dev = backlight_device_register(bl_name,
1351 dm->adev->ddev->dev,
1353 &amdgpu_dm_backlight_ops,
1356 if (IS_ERR(dm->backlight_dev))
1357 DRM_ERROR("DM: Backlight registration failed!\n");
1359 DRM_DEBUG_DRIVER("DM: Registered Backlight device: %s\n", bl_name);
1364 /* In this architecture, the association
1365 * connector -> encoder -> crtc
1366 * id not really requried. The crtc and connector will hold the
1367 * display_index as an abstraction to use with DAL component
1369 * Returns 0 on success
1371 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev)
1373 struct amdgpu_display_manager *dm = &adev->dm;
1375 struct amdgpu_dm_connector *aconnector = NULL;
1376 struct amdgpu_encoder *aencoder = NULL;
1377 struct amdgpu_mode_info *mode_info = &adev->mode_info;
1379 unsigned long possible_crtcs;
1381 link_cnt = dm->dc->caps.max_links;
1382 if (amdgpu_dm_mode_config_init(dm->adev)) {
1383 DRM_ERROR("DM: Failed to initialize mode config\n");
1387 for (i = 0; i < dm->dc->caps.max_planes; i++) {
1388 struct amdgpu_plane *plane;
1390 plane = kzalloc(sizeof(struct amdgpu_plane), GFP_KERNEL);
1391 mode_info->planes[i] = plane;
1394 DRM_ERROR("KMS: Failed to allocate plane\n");
1397 plane->base.type = mode_info->plane_type[i];
1400 * HACK: IGT tests expect that each plane can only have one
1401 * one possible CRTC. For now, set one CRTC for each
1402 * plane that is not an underlay, but still allow multiple
1403 * CRTCs for underlay planes.
1405 possible_crtcs = 1 << i;
1406 if (i >= dm->dc->caps.max_streams)
1407 possible_crtcs = 0xff;
1409 if (amdgpu_dm_plane_init(dm, mode_info->planes[i], possible_crtcs)) {
1410 DRM_ERROR("KMS: Failed to initialize plane\n");
1415 for (i = 0; i < dm->dc->caps.max_streams; i++)
1416 if (amdgpu_dm_crtc_init(dm, &mode_info->planes[i]->base, i)) {
1417 DRM_ERROR("KMS: Failed to initialize crtc\n");
1421 dm->display_indexes_num = dm->dc->caps.max_streams;
1423 /* loops over all connectors on the board */
1424 for (i = 0; i < link_cnt; i++) {
1426 if (i > AMDGPU_DM_MAX_DISPLAY_INDEX) {
1428 "KMS: Cannot support more than %d display indexes\n",
1429 AMDGPU_DM_MAX_DISPLAY_INDEX);
1433 aconnector = kzalloc(sizeof(*aconnector), GFP_KERNEL);
1437 aencoder = kzalloc(sizeof(*aencoder), GFP_KERNEL);
1441 if (amdgpu_dm_encoder_init(dm->ddev, aencoder, i)) {
1442 DRM_ERROR("KMS: Failed to initialize encoder\n");
1446 if (amdgpu_dm_connector_init(dm, aconnector, i, aencoder)) {
1447 DRM_ERROR("KMS: Failed to initialize connector\n");
1451 if (dc_link_detect(dc_get_link_at_index(dm->dc, i),
1452 DETECT_REASON_BOOT))
1453 amdgpu_dm_update_connector_after_detect(aconnector);
1456 /* Software is initialized. Now we can register interrupt handlers. */
1457 switch (adev->asic_type) {
1467 case CHIP_POLARIS11:
1468 case CHIP_POLARIS10:
1469 case CHIP_POLARIS12:
1471 if (dce110_register_irq_handlers(dm->adev)) {
1472 DRM_ERROR("DM: Failed to initialize IRQ\n");
1476 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
1478 if (dcn10_register_irq_handlers(dm->adev)) {
1479 DRM_ERROR("DM: Failed to initialize IRQ\n");
1483 * Temporary disable until pplib/smu interaction is implemented
1485 dm->dc->debug.disable_stutter = true;
1489 DRM_ERROR("Usupported ASIC type: 0x%X\n", adev->asic_type);
1497 for (i = 0; i < dm->dc->caps.max_planes; i++)
1498 kfree(mode_info->planes[i]);
1502 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm)
1504 drm_mode_config_cleanup(dm->ddev);
1508 /******************************************************************************
1509 * amdgpu_display_funcs functions
1510 *****************************************************************************/
1513 * dm_bandwidth_update - program display watermarks
1515 * @adev: amdgpu_device pointer
1517 * Calculate and program the display watermarks and line buffer allocation.
1519 static void dm_bandwidth_update(struct amdgpu_device *adev)
1521 /* TODO: implement later */
1524 static void dm_set_backlight_level(struct amdgpu_encoder *amdgpu_encoder,
1527 /* TODO: translate amdgpu_encoder to display_index and call DAL */
1530 static u8 dm_get_backlight_level(struct amdgpu_encoder *amdgpu_encoder)
1532 /* TODO: translate amdgpu_encoder to display_index and call DAL */
1536 static int amdgpu_notify_freesync(struct drm_device *dev, void *data,
1537 struct drm_file *filp)
1539 struct mod_freesync_params freesync_params;
1540 uint8_t num_streams;
1543 struct amdgpu_device *adev = dev->dev_private;
1546 /* Get freesync enable flag from DRM */
1548 num_streams = dc_get_current_stream_count(adev->dm.dc);
1550 for (i = 0; i < num_streams; i++) {
1551 struct dc_stream_state *stream;
1552 stream = dc_get_stream_at_index(adev->dm.dc, i);
1554 mod_freesync_update_state(adev->dm.freesync_module,
1555 &stream, 1, &freesync_params);
1561 static const struct amdgpu_display_funcs dm_display_funcs = {
1562 .bandwidth_update = dm_bandwidth_update, /* called unconditionally */
1563 .vblank_get_counter = dm_vblank_get_counter,/* called unconditionally */
1564 .backlight_set_level =
1565 dm_set_backlight_level,/* called unconditionally */
1566 .backlight_get_level =
1567 dm_get_backlight_level,/* called unconditionally */
1568 .hpd_sense = NULL,/* called unconditionally */
1569 .hpd_set_polarity = NULL, /* called unconditionally */
1570 .hpd_get_gpio_reg = NULL, /* VBIOS parsing. DAL does it. */
1571 .page_flip_get_scanoutpos =
1572 dm_crtc_get_scanoutpos,/* called unconditionally */
1573 .add_encoder = NULL, /* VBIOS parsing. DAL does it. */
1574 .add_connector = NULL, /* VBIOS parsing. DAL does it. */
1575 .notify_freesync = amdgpu_notify_freesync,
1579 #if defined(CONFIG_DEBUG_KERNEL_DC)
1581 static ssize_t s3_debug_store(struct device *device,
1582 struct device_attribute *attr,
1588 struct pci_dev *pdev = to_pci_dev(device);
1589 struct drm_device *drm_dev = pci_get_drvdata(pdev);
1590 struct amdgpu_device *adev = drm_dev->dev_private;
1592 ret = kstrtoint(buf, 0, &s3_state);
1597 amdgpu_dm_display_resume(adev);
1598 drm_kms_helper_hotplug_event(adev->ddev);
1603 return ret == 0 ? count : 0;
1606 DEVICE_ATTR_WO(s3_debug);
1610 static int dm_early_init(void *handle)
1612 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1614 switch (adev->asic_type) {
1617 adev->mode_info.num_crtc = 6;
1618 adev->mode_info.num_hpd = 6;
1619 adev->mode_info.num_dig = 6;
1620 adev->mode_info.plane_type = dm_plane_type_default;
1623 adev->mode_info.num_crtc = 4;
1624 adev->mode_info.num_hpd = 6;
1625 adev->mode_info.num_dig = 7;
1626 adev->mode_info.plane_type = dm_plane_type_default;
1630 adev->mode_info.num_crtc = 2;
1631 adev->mode_info.num_hpd = 6;
1632 adev->mode_info.num_dig = 6;
1633 adev->mode_info.plane_type = dm_plane_type_default;
1637 adev->mode_info.num_crtc = 6;
1638 adev->mode_info.num_hpd = 6;
1639 adev->mode_info.num_dig = 7;
1640 adev->mode_info.plane_type = dm_plane_type_default;
1643 adev->mode_info.num_crtc = 3;
1644 adev->mode_info.num_hpd = 6;
1645 adev->mode_info.num_dig = 9;
1646 adev->mode_info.plane_type = dm_plane_type_carizzo;
1649 adev->mode_info.num_crtc = 2;
1650 adev->mode_info.num_hpd = 6;
1651 adev->mode_info.num_dig = 9;
1652 adev->mode_info.plane_type = dm_plane_type_stoney;
1654 case CHIP_POLARIS11:
1655 case CHIP_POLARIS12:
1656 adev->mode_info.num_crtc = 5;
1657 adev->mode_info.num_hpd = 5;
1658 adev->mode_info.num_dig = 5;
1659 adev->mode_info.plane_type = dm_plane_type_default;
1661 case CHIP_POLARIS10:
1662 adev->mode_info.num_crtc = 6;
1663 adev->mode_info.num_hpd = 6;
1664 adev->mode_info.num_dig = 6;
1665 adev->mode_info.plane_type = dm_plane_type_default;
1668 adev->mode_info.num_crtc = 6;
1669 adev->mode_info.num_hpd = 6;
1670 adev->mode_info.num_dig = 6;
1671 adev->mode_info.plane_type = dm_plane_type_default;
1673 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
1675 adev->mode_info.num_crtc = 4;
1676 adev->mode_info.num_hpd = 4;
1677 adev->mode_info.num_dig = 4;
1678 adev->mode_info.plane_type = dm_plane_type_default;
1682 DRM_ERROR("Usupported ASIC type: 0x%X\n", adev->asic_type);
1686 amdgpu_dm_set_irq_funcs(adev);
1688 if (adev->mode_info.funcs == NULL)
1689 adev->mode_info.funcs = &dm_display_funcs;
1691 /* Note: Do NOT change adev->audio_endpt_rreg and
1692 * adev->audio_endpt_wreg because they are initialised in
1693 * amdgpu_device_init() */
1694 #if defined(CONFIG_DEBUG_KERNEL_DC)
1697 &dev_attr_s3_debug);
1703 static bool modeset_required(struct drm_crtc_state *crtc_state,
1704 struct dc_stream_state *new_stream,
1705 struct dc_stream_state *old_stream)
1707 if (!drm_atomic_crtc_needs_modeset(crtc_state))
1710 if (!crtc_state->enable)
1713 return crtc_state->active;
1716 static bool modereset_required(struct drm_crtc_state *crtc_state)
1718 if (!drm_atomic_crtc_needs_modeset(crtc_state))
1721 return !crtc_state->enable || !crtc_state->active;
1724 static void amdgpu_dm_encoder_destroy(struct drm_encoder *encoder)
1726 drm_encoder_cleanup(encoder);
1730 static const struct drm_encoder_funcs amdgpu_dm_encoder_funcs = {
1731 .destroy = amdgpu_dm_encoder_destroy,
1734 static bool fill_rects_from_plane_state(const struct drm_plane_state *state,
1735 struct dc_plane_state *plane_state)
1737 plane_state->src_rect.x = state->src_x >> 16;
1738 plane_state->src_rect.y = state->src_y >> 16;
1739 /*we ignore for now mantissa and do not to deal with floating pixels :(*/
1740 plane_state->src_rect.width = state->src_w >> 16;
1742 if (plane_state->src_rect.width == 0)
1745 plane_state->src_rect.height = state->src_h >> 16;
1746 if (plane_state->src_rect.height == 0)
1749 plane_state->dst_rect.x = state->crtc_x;
1750 plane_state->dst_rect.y = state->crtc_y;
1752 if (state->crtc_w == 0)
1755 plane_state->dst_rect.width = state->crtc_w;
1757 if (state->crtc_h == 0)
1760 plane_state->dst_rect.height = state->crtc_h;
1762 plane_state->clip_rect = plane_state->dst_rect;
1764 switch (state->rotation & DRM_MODE_ROTATE_MASK) {
1765 case DRM_MODE_ROTATE_0:
1766 plane_state->rotation = ROTATION_ANGLE_0;
1768 case DRM_MODE_ROTATE_90:
1769 plane_state->rotation = ROTATION_ANGLE_90;
1771 case DRM_MODE_ROTATE_180:
1772 plane_state->rotation = ROTATION_ANGLE_180;
1774 case DRM_MODE_ROTATE_270:
1775 plane_state->rotation = ROTATION_ANGLE_270;
1778 plane_state->rotation = ROTATION_ANGLE_0;
1784 static int get_fb_info(const struct amdgpu_framebuffer *amdgpu_fb,
1785 uint64_t *tiling_flags)
1787 struct amdgpu_bo *rbo = gem_to_amdgpu_bo(amdgpu_fb->obj);
1788 int r = amdgpu_bo_reserve(rbo, false);
1791 // Don't show error msg. when return -ERESTARTSYS
1792 if (r != -ERESTARTSYS)
1793 DRM_ERROR("Unable to reserve buffer: %d\n", r);
1798 amdgpu_bo_get_tiling_flags(rbo, tiling_flags);
1800 amdgpu_bo_unreserve(rbo);
1805 static int fill_plane_attributes_from_fb(struct amdgpu_device *adev,
1806 struct dc_plane_state *plane_state,
1807 const struct amdgpu_framebuffer *amdgpu_fb)
1809 uint64_t tiling_flags;
1810 unsigned int awidth;
1811 const struct drm_framebuffer *fb = &amdgpu_fb->base;
1813 struct drm_format_name_buf format_name;
1822 switch (fb->format->format) {
1824 plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS;
1826 case DRM_FORMAT_RGB565:
1827 plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_RGB565;
1829 case DRM_FORMAT_XRGB8888:
1830 case DRM_FORMAT_ARGB8888:
1831 plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888;
1833 case DRM_FORMAT_XRGB2101010:
1834 case DRM_FORMAT_ARGB2101010:
1835 plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010;
1837 case DRM_FORMAT_XBGR2101010:
1838 case DRM_FORMAT_ABGR2101010:
1839 plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010;
1841 case DRM_FORMAT_NV21:
1842 plane_state->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr;
1844 case DRM_FORMAT_NV12:
1845 plane_state->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb;
1848 DRM_ERROR("Unsupported screen format %s\n",
1849 drm_get_format_name(fb->format->format, &format_name));
1853 if (plane_state->format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) {
1854 plane_state->address.type = PLN_ADDR_TYPE_GRAPHICS;
1855 plane_state->plane_size.grph.surface_size.x = 0;
1856 plane_state->plane_size.grph.surface_size.y = 0;
1857 plane_state->plane_size.grph.surface_size.width = fb->width;
1858 plane_state->plane_size.grph.surface_size.height = fb->height;
1859 plane_state->plane_size.grph.surface_pitch =
1860 fb->pitches[0] / fb->format->cpp[0];
1861 /* TODO: unhardcode */
1862 plane_state->color_space = COLOR_SPACE_SRGB;
1865 awidth = ALIGN(fb->width, 64);
1866 plane_state->address.type = PLN_ADDR_TYPE_VIDEO_PROGRESSIVE;
1867 plane_state->plane_size.video.luma_size.x = 0;
1868 plane_state->plane_size.video.luma_size.y = 0;
1869 plane_state->plane_size.video.luma_size.width = awidth;
1870 plane_state->plane_size.video.luma_size.height = fb->height;
1871 /* TODO: unhardcode */
1872 plane_state->plane_size.video.luma_pitch = awidth;
1874 plane_state->plane_size.video.chroma_size.x = 0;
1875 plane_state->plane_size.video.chroma_size.y = 0;
1876 plane_state->plane_size.video.chroma_size.width = awidth;
1877 plane_state->plane_size.video.chroma_size.height = fb->height;
1878 plane_state->plane_size.video.chroma_pitch = awidth / 2;
1880 /* TODO: unhardcode */
1881 plane_state->color_space = COLOR_SPACE_YCBCR709;
1884 memset(&plane_state->tiling_info, 0, sizeof(plane_state->tiling_info));
1886 /* Fill GFX8 params */
1887 if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == DC_ARRAY_2D_TILED_THIN1) {
1888 unsigned int bankw, bankh, mtaspect, tile_split, num_banks;
1890 bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
1891 bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
1892 mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
1893 tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT);
1894 num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
1896 /* XXX fix me for VI */
1897 plane_state->tiling_info.gfx8.num_banks = num_banks;
1898 plane_state->tiling_info.gfx8.array_mode =
1899 DC_ARRAY_2D_TILED_THIN1;
1900 plane_state->tiling_info.gfx8.tile_split = tile_split;
1901 plane_state->tiling_info.gfx8.bank_width = bankw;
1902 plane_state->tiling_info.gfx8.bank_height = bankh;
1903 plane_state->tiling_info.gfx8.tile_aspect = mtaspect;
1904 plane_state->tiling_info.gfx8.tile_mode =
1905 DC_ADDR_SURF_MICRO_TILING_DISPLAY;
1906 } else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE)
1907 == DC_ARRAY_1D_TILED_THIN1) {
1908 plane_state->tiling_info.gfx8.array_mode = DC_ARRAY_1D_TILED_THIN1;
1911 plane_state->tiling_info.gfx8.pipe_config =
1912 AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
1914 if (adev->asic_type == CHIP_VEGA10 ||
1915 adev->asic_type == CHIP_RAVEN) {
1916 /* Fill GFX9 params */
1917 plane_state->tiling_info.gfx9.num_pipes =
1918 adev->gfx.config.gb_addr_config_fields.num_pipes;
1919 plane_state->tiling_info.gfx9.num_banks =
1920 adev->gfx.config.gb_addr_config_fields.num_banks;
1921 plane_state->tiling_info.gfx9.pipe_interleave =
1922 adev->gfx.config.gb_addr_config_fields.pipe_interleave_size;
1923 plane_state->tiling_info.gfx9.num_shader_engines =
1924 adev->gfx.config.gb_addr_config_fields.num_se;
1925 plane_state->tiling_info.gfx9.max_compressed_frags =
1926 adev->gfx.config.gb_addr_config_fields.max_compress_frags;
1927 plane_state->tiling_info.gfx9.num_rb_per_se =
1928 adev->gfx.config.gb_addr_config_fields.num_rb_per_se;
1929 plane_state->tiling_info.gfx9.swizzle =
1930 AMDGPU_TILING_GET(tiling_flags, SWIZZLE_MODE);
1931 plane_state->tiling_info.gfx9.shaderEnable = 1;
1934 plane_state->visible = true;
1935 plane_state->scaling_quality.h_taps_c = 0;
1936 plane_state->scaling_quality.v_taps_c = 0;
1938 /* is this needed? is plane_state zeroed at allocation? */
1939 plane_state->scaling_quality.h_taps = 0;
1940 plane_state->scaling_quality.v_taps = 0;
1941 plane_state->stereo_format = PLANE_STEREO_FORMAT_NONE;
1947 static int fill_plane_attributes(struct amdgpu_device *adev,
1948 struct dc_plane_state *dc_plane_state,
1949 struct drm_plane_state *plane_state,
1950 struct drm_crtc_state *crtc_state)
1952 const struct amdgpu_framebuffer *amdgpu_fb =
1953 to_amdgpu_framebuffer(plane_state->fb);
1954 const struct drm_crtc *crtc = plane_state->crtc;
1955 struct dc_transfer_func *input_tf;
1958 if (!fill_rects_from_plane_state(plane_state, dc_plane_state))
1961 ret = fill_plane_attributes_from_fb(
1962 crtc->dev->dev_private,
1969 input_tf = dc_create_transfer_func();
1971 if (input_tf == NULL)
1974 dc_plane_state->in_transfer_func = input_tf;
1977 * Always set input transfer function, since plane state is refreshed
1980 ret = amdgpu_dm_set_degamma_lut(crtc_state, dc_plane_state);
1985 /*****************************************************************************/
1987 static void update_stream_scaling_settings(const struct drm_display_mode *mode,
1988 const struct dm_connector_state *dm_state,
1989 struct dc_stream_state *stream)
1991 enum amdgpu_rmx_type rmx_type;
1993 struct rect src = { 0 }; /* viewport in composition space*/
1994 struct rect dst = { 0 }; /* stream addressable area */
1996 /* no mode. nothing to be done */
2000 /* Full screen scaling by default */
2001 src.width = mode->hdisplay;
2002 src.height = mode->vdisplay;
2003 dst.width = stream->timing.h_addressable;
2004 dst.height = stream->timing.v_addressable;
2007 rmx_type = dm_state->scaling;
2008 if (rmx_type == RMX_ASPECT || rmx_type == RMX_OFF) {
2009 if (src.width * dst.height <
2010 src.height * dst.width) {
2011 /* height needs less upscaling/more downscaling */
2012 dst.width = src.width *
2013 dst.height / src.height;
2015 /* width needs less upscaling/more downscaling */
2016 dst.height = src.height *
2017 dst.width / src.width;
2019 } else if (rmx_type == RMX_CENTER) {
2023 dst.x = (stream->timing.h_addressable - dst.width) / 2;
2024 dst.y = (stream->timing.v_addressable - dst.height) / 2;
2026 if (dm_state->underscan_enable) {
2027 dst.x += dm_state->underscan_hborder / 2;
2028 dst.y += dm_state->underscan_vborder / 2;
2029 dst.width -= dm_state->underscan_hborder;
2030 dst.height -= dm_state->underscan_vborder;
2037 DRM_DEBUG_DRIVER("Destination Rectangle x:%d y:%d width:%d height:%d\n",
2038 dst.x, dst.y, dst.width, dst.height);
2042 static enum dc_color_depth
2043 convert_color_depth_from_display_info(const struct drm_connector *connector)
2045 uint32_t bpc = connector->display_info.bpc;
2047 /* Limited color depth to 8bit
2048 * TODO: Still need to handle deep color
2055 /* Temporary Work around, DRM don't parse color depth for
2056 * EDID revision before 1.4
2057 * TODO: Fix edid parsing
2059 return COLOR_DEPTH_888;
2061 return COLOR_DEPTH_666;
2063 return COLOR_DEPTH_888;
2065 return COLOR_DEPTH_101010;
2067 return COLOR_DEPTH_121212;
2069 return COLOR_DEPTH_141414;
2071 return COLOR_DEPTH_161616;
2073 return COLOR_DEPTH_UNDEFINED;
2077 static enum dc_aspect_ratio
2078 get_aspect_ratio(const struct drm_display_mode *mode_in)
2080 int32_t width = mode_in->crtc_hdisplay * 9;
2081 int32_t height = mode_in->crtc_vdisplay * 16;
2083 if ((width - height) < 10 && (width - height) > -10)
2084 return ASPECT_RATIO_16_9;
2086 return ASPECT_RATIO_4_3;
2089 static enum dc_color_space
2090 get_output_color_space(const struct dc_crtc_timing *dc_crtc_timing)
2092 enum dc_color_space color_space = COLOR_SPACE_SRGB;
2094 switch (dc_crtc_timing->pixel_encoding) {
2095 case PIXEL_ENCODING_YCBCR422:
2096 case PIXEL_ENCODING_YCBCR444:
2097 case PIXEL_ENCODING_YCBCR420:
2100 * 27030khz is the separation point between HDTV and SDTV
2101 * according to HDMI spec, we use YCbCr709 and YCbCr601
2104 if (dc_crtc_timing->pix_clk_khz > 27030) {
2105 if (dc_crtc_timing->flags.Y_ONLY)
2107 COLOR_SPACE_YCBCR709_LIMITED;
2109 color_space = COLOR_SPACE_YCBCR709;
2111 if (dc_crtc_timing->flags.Y_ONLY)
2113 COLOR_SPACE_YCBCR601_LIMITED;
2115 color_space = COLOR_SPACE_YCBCR601;
2120 case PIXEL_ENCODING_RGB:
2121 color_space = COLOR_SPACE_SRGB;
2132 /*****************************************************************************/
2135 fill_stream_properties_from_drm_display_mode(struct dc_stream_state *stream,
2136 const struct drm_display_mode *mode_in,
2137 const struct drm_connector *connector)
2139 struct dc_crtc_timing *timing_out = &stream->timing;
2140 struct dc_transfer_func *tf = dc_create_transfer_func();
2142 memset(timing_out, 0, sizeof(struct dc_crtc_timing));
2144 timing_out->h_border_left = 0;
2145 timing_out->h_border_right = 0;
2146 timing_out->v_border_top = 0;
2147 timing_out->v_border_bottom = 0;
2148 /* TODO: un-hardcode */
2150 if ((connector->display_info.color_formats & DRM_COLOR_FORMAT_YCRCB444)
2151 && stream->sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A)
2152 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR444;
2154 timing_out->pixel_encoding = PIXEL_ENCODING_RGB;
2156 timing_out->timing_3d_format = TIMING_3D_FORMAT_NONE;
2157 timing_out->display_color_depth = convert_color_depth_from_display_info(
2159 timing_out->scan_type = SCANNING_TYPE_NODATA;
2160 timing_out->hdmi_vic = 0;
2161 timing_out->vic = drm_match_cea_mode(mode_in);
2163 timing_out->h_addressable = mode_in->crtc_hdisplay;
2164 timing_out->h_total = mode_in->crtc_htotal;
2165 timing_out->h_sync_width =
2166 mode_in->crtc_hsync_end - mode_in->crtc_hsync_start;
2167 timing_out->h_front_porch =
2168 mode_in->crtc_hsync_start - mode_in->crtc_hdisplay;
2169 timing_out->v_total = mode_in->crtc_vtotal;
2170 timing_out->v_addressable = mode_in->crtc_vdisplay;
2171 timing_out->v_front_porch =
2172 mode_in->crtc_vsync_start - mode_in->crtc_vdisplay;
2173 timing_out->v_sync_width =
2174 mode_in->crtc_vsync_end - mode_in->crtc_vsync_start;
2175 timing_out->pix_clk_khz = mode_in->crtc_clock;
2176 timing_out->aspect_ratio = get_aspect_ratio(mode_in);
2177 if (mode_in->flags & DRM_MODE_FLAG_PHSYNC)
2178 timing_out->flags.HSYNC_POSITIVE_POLARITY = 1;
2179 if (mode_in->flags & DRM_MODE_FLAG_PVSYNC)
2180 timing_out->flags.VSYNC_POSITIVE_POLARITY = 1;
2182 stream->output_color_space = get_output_color_space(timing_out);
2184 tf->type = TF_TYPE_PREDEFINED;
2185 tf->tf = TRANSFER_FUNCTION_SRGB;
2186 stream->out_transfer_func = tf;
2189 static void fill_audio_info(struct audio_info *audio_info,
2190 const struct drm_connector *drm_connector,
2191 const struct dc_sink *dc_sink)
2194 int cea_revision = 0;
2195 const struct dc_edid_caps *edid_caps = &dc_sink->edid_caps;
2197 audio_info->manufacture_id = edid_caps->manufacturer_id;
2198 audio_info->product_id = edid_caps->product_id;
2200 cea_revision = drm_connector->display_info.cea_rev;
2202 strncpy(audio_info->display_name,
2203 edid_caps->display_name,
2204 AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS - 1);
2206 if (cea_revision >= 3) {
2207 audio_info->mode_count = edid_caps->audio_mode_count;
2209 for (i = 0; i < audio_info->mode_count; ++i) {
2210 audio_info->modes[i].format_code =
2211 (enum audio_format_code)
2212 (edid_caps->audio_modes[i].format_code);
2213 audio_info->modes[i].channel_count =
2214 edid_caps->audio_modes[i].channel_count;
2215 audio_info->modes[i].sample_rates.all =
2216 edid_caps->audio_modes[i].sample_rate;
2217 audio_info->modes[i].sample_size =
2218 edid_caps->audio_modes[i].sample_size;
2222 audio_info->flags.all = edid_caps->speaker_flags;
2224 /* TODO: We only check for the progressive mode, check for interlace mode too */
2225 if (drm_connector->latency_present[0]) {
2226 audio_info->video_latency = drm_connector->video_latency[0];
2227 audio_info->audio_latency = drm_connector->audio_latency[0];
2230 /* TODO: For DP, video and audio latency should be calculated from DPCD caps */
2235 copy_crtc_timing_for_drm_display_mode(const struct drm_display_mode *src_mode,
2236 struct drm_display_mode *dst_mode)
2238 dst_mode->crtc_hdisplay = src_mode->crtc_hdisplay;
2239 dst_mode->crtc_vdisplay = src_mode->crtc_vdisplay;
2240 dst_mode->crtc_clock = src_mode->crtc_clock;
2241 dst_mode->crtc_hblank_start = src_mode->crtc_hblank_start;
2242 dst_mode->crtc_hblank_end = src_mode->crtc_hblank_end;
2243 dst_mode->crtc_hsync_start = src_mode->crtc_hsync_start;
2244 dst_mode->crtc_hsync_end = src_mode->crtc_hsync_end;
2245 dst_mode->crtc_htotal = src_mode->crtc_htotal;
2246 dst_mode->crtc_hskew = src_mode->crtc_hskew;
2247 dst_mode->crtc_vblank_start = src_mode->crtc_vblank_start;
2248 dst_mode->crtc_vblank_end = src_mode->crtc_vblank_end;
2249 dst_mode->crtc_vsync_start = src_mode->crtc_vsync_start;
2250 dst_mode->crtc_vsync_end = src_mode->crtc_vsync_end;
2251 dst_mode->crtc_vtotal = src_mode->crtc_vtotal;
2255 decide_crtc_timing_for_drm_display_mode(struct drm_display_mode *drm_mode,
2256 const struct drm_display_mode *native_mode,
2259 if (scale_enabled) {
2260 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
2261 } else if (native_mode->clock == drm_mode->clock &&
2262 native_mode->htotal == drm_mode->htotal &&
2263 native_mode->vtotal == drm_mode->vtotal) {
2264 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
2266 /* no scaling nor amdgpu inserted, no need to patch */
2270 static int create_fake_sink(struct amdgpu_dm_connector *aconnector)
2272 struct dc_sink *sink = NULL;
2273 struct dc_sink_init_data sink_init_data = { 0 };
2275 sink_init_data.link = aconnector->dc_link;
2276 sink_init_data.sink_signal = aconnector->dc_link->connector_signal;
2278 sink = dc_sink_create(&sink_init_data);
2280 DRM_ERROR("Failed to create sink!\n");
2284 sink->sink_signal = SIGNAL_TYPE_VIRTUAL;
2285 aconnector->fake_enable = true;
2287 aconnector->dc_sink = sink;
2288 aconnector->dc_link->local_sink = sink;
2293 static void set_multisync_trigger_params(
2294 struct dc_stream_state *stream)
2296 if (stream->triggered_crtc_reset.enabled) {
2297 stream->triggered_crtc_reset.event = CRTC_EVENT_VSYNC_RISING;
2298 stream->triggered_crtc_reset.delay = TRIGGER_DELAY_NEXT_LINE;
2302 static void set_master_stream(struct dc_stream_state *stream_set[],
2305 int j, highest_rfr = 0, master_stream = 0;
2307 for (j = 0; j < stream_count; j++) {
2308 if (stream_set[j] && stream_set[j]->triggered_crtc_reset.enabled) {
2309 int refresh_rate = 0;
2311 refresh_rate = (stream_set[j]->timing.pix_clk_khz*1000)/
2312 (stream_set[j]->timing.h_total*stream_set[j]->timing.v_total);
2313 if (refresh_rate > highest_rfr) {
2314 highest_rfr = refresh_rate;
2319 for (j = 0; j < stream_count; j++) {
2321 stream_set[j]->triggered_crtc_reset.event_source = stream_set[master_stream];
2325 static void dm_enable_per_frame_crtc_master_sync(struct dc_state *context)
2329 if (context->stream_count < 2)
2331 for (i = 0; i < context->stream_count ; i++) {
2332 if (!context->streams[i])
2334 /* TODO: add a function to read AMD VSDB bits and will set
2335 * crtc_sync_master.multi_sync_enabled flag
2336 * For now its set to false
2338 set_multisync_trigger_params(context->streams[i]);
2340 set_master_stream(context->streams, context->stream_count);
2343 static struct dc_stream_state *
2344 create_stream_for_sink(struct amdgpu_dm_connector *aconnector,
2345 const struct drm_display_mode *drm_mode,
2346 const struct dm_connector_state *dm_state)
2348 struct drm_display_mode *preferred_mode = NULL;
2349 struct drm_connector *drm_connector;
2350 struct dc_stream_state *stream = NULL;
2351 struct drm_display_mode mode = *drm_mode;
2352 bool native_mode_found = false;
2354 if (aconnector == NULL) {
2355 DRM_ERROR("aconnector is NULL!\n");
2359 drm_connector = &aconnector->base;
2361 if (!aconnector->dc_sink) {
2363 * Create dc_sink when necessary to MST
2364 * Don't apply fake_sink to MST
2366 if (aconnector->mst_port) {
2367 dm_dp_mst_dc_sink_create(drm_connector);
2371 if (create_fake_sink(aconnector))
2375 stream = dc_create_stream_for_sink(aconnector->dc_sink);
2377 if (stream == NULL) {
2378 DRM_ERROR("Failed to create stream for sink!\n");
2382 list_for_each_entry(preferred_mode, &aconnector->base.modes, head) {
2383 /* Search for preferred mode */
2384 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED) {
2385 native_mode_found = true;
2389 if (!native_mode_found)
2390 preferred_mode = list_first_entry_or_null(
2391 &aconnector->base.modes,
2392 struct drm_display_mode,
2395 if (preferred_mode == NULL) {
2396 /* This may not be an error, the use case is when we we have no
2397 * usermode calls to reset and set mode upon hotplug. In this
2398 * case, we call set mode ourselves to restore the previous mode
2399 * and the modelist may not be filled in in time.
2401 DRM_DEBUG_DRIVER("No preferred mode found\n");
2403 decide_crtc_timing_for_drm_display_mode(
2404 &mode, preferred_mode,
2405 dm_state ? (dm_state->scaling != RMX_OFF) : false);
2409 drm_mode_set_crtcinfo(&mode, 0);
2411 fill_stream_properties_from_drm_display_mode(stream,
2412 &mode, &aconnector->base);
2413 update_stream_scaling_settings(&mode, dm_state, stream);
2416 &stream->audio_info,
2418 aconnector->dc_sink);
2420 update_stream_signal(stream);
2425 static void amdgpu_dm_crtc_destroy(struct drm_crtc *crtc)
2427 drm_crtc_cleanup(crtc);
2431 static void dm_crtc_destroy_state(struct drm_crtc *crtc,
2432 struct drm_crtc_state *state)
2434 struct dm_crtc_state *cur = to_dm_crtc_state(state);
2436 /* TODO Destroy dc_stream objects are stream object is flattened */
2438 dc_stream_release(cur->stream);
2441 __drm_atomic_helper_crtc_destroy_state(state);
2447 static void dm_crtc_reset_state(struct drm_crtc *crtc)
2449 struct dm_crtc_state *state;
2452 dm_crtc_destroy_state(crtc, crtc->state);
2454 state = kzalloc(sizeof(*state), GFP_KERNEL);
2455 if (WARN_ON(!state))
2458 crtc->state = &state->base;
2459 crtc->state->crtc = crtc;
2463 static struct drm_crtc_state *
2464 dm_crtc_duplicate_state(struct drm_crtc *crtc)
2466 struct dm_crtc_state *state, *cur;
2468 cur = to_dm_crtc_state(crtc->state);
2470 if (WARN_ON(!crtc->state))
2473 state = kzalloc(sizeof(*state), GFP_KERNEL);
2477 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
2480 state->stream = cur->stream;
2481 dc_stream_retain(state->stream);
2484 /* TODO Duplicate dc_stream after objects are stream object is flattened */
2486 return &state->base;
2489 /* Implemented only the options currently availible for the driver */
2490 static const struct drm_crtc_funcs amdgpu_dm_crtc_funcs = {
2491 .reset = dm_crtc_reset_state,
2492 .destroy = amdgpu_dm_crtc_destroy,
2493 .gamma_set = drm_atomic_helper_legacy_gamma_set,
2494 .set_config = drm_atomic_helper_set_config,
2495 .page_flip = drm_atomic_helper_page_flip,
2496 .atomic_duplicate_state = dm_crtc_duplicate_state,
2497 .atomic_destroy_state = dm_crtc_destroy_state,
2498 .set_crc_source = amdgpu_dm_crtc_set_crc_source,
2501 static enum drm_connector_status
2502 amdgpu_dm_connector_detect(struct drm_connector *connector, bool force)
2505 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
2508 * 1. This interface is NOT called in context of HPD irq.
2509 * 2. This interface *is called* in context of user-mode ioctl. Which
2510 * makes it a bad place for *any* MST-related activit. */
2512 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED &&
2513 !aconnector->fake_enable)
2514 connected = (aconnector->dc_sink != NULL);
2516 connected = (aconnector->base.force == DRM_FORCE_ON);
2518 return (connected ? connector_status_connected :
2519 connector_status_disconnected);
2522 int amdgpu_dm_connector_atomic_set_property(struct drm_connector *connector,
2523 struct drm_connector_state *connector_state,
2524 struct drm_property *property,
2527 struct drm_device *dev = connector->dev;
2528 struct amdgpu_device *adev = dev->dev_private;
2529 struct dm_connector_state *dm_old_state =
2530 to_dm_connector_state(connector->state);
2531 struct dm_connector_state *dm_new_state =
2532 to_dm_connector_state(connector_state);
2536 if (property == dev->mode_config.scaling_mode_property) {
2537 enum amdgpu_rmx_type rmx_type;
2540 case DRM_MODE_SCALE_CENTER:
2541 rmx_type = RMX_CENTER;
2543 case DRM_MODE_SCALE_ASPECT:
2544 rmx_type = RMX_ASPECT;
2546 case DRM_MODE_SCALE_FULLSCREEN:
2547 rmx_type = RMX_FULL;
2549 case DRM_MODE_SCALE_NONE:
2555 if (dm_old_state->scaling == rmx_type)
2558 dm_new_state->scaling = rmx_type;
2560 } else if (property == adev->mode_info.underscan_hborder_property) {
2561 dm_new_state->underscan_hborder = val;
2563 } else if (property == adev->mode_info.underscan_vborder_property) {
2564 dm_new_state->underscan_vborder = val;
2566 } else if (property == adev->mode_info.underscan_property) {
2567 dm_new_state->underscan_enable = val;
2574 int amdgpu_dm_connector_atomic_get_property(struct drm_connector *connector,
2575 const struct drm_connector_state *state,
2576 struct drm_property *property,
2579 struct drm_device *dev = connector->dev;
2580 struct amdgpu_device *adev = dev->dev_private;
2581 struct dm_connector_state *dm_state =
2582 to_dm_connector_state(state);
2585 if (property == dev->mode_config.scaling_mode_property) {
2586 switch (dm_state->scaling) {
2588 *val = DRM_MODE_SCALE_CENTER;
2591 *val = DRM_MODE_SCALE_ASPECT;
2594 *val = DRM_MODE_SCALE_FULLSCREEN;
2598 *val = DRM_MODE_SCALE_NONE;
2602 } else if (property == adev->mode_info.underscan_hborder_property) {
2603 *val = dm_state->underscan_hborder;
2605 } else if (property == adev->mode_info.underscan_vborder_property) {
2606 *val = dm_state->underscan_vborder;
2608 } else if (property == adev->mode_info.underscan_property) {
2609 *val = dm_state->underscan_enable;
2615 static void amdgpu_dm_connector_destroy(struct drm_connector *connector)
2617 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
2618 const struct dc_link *link = aconnector->dc_link;
2619 struct amdgpu_device *adev = connector->dev->dev_private;
2620 struct amdgpu_display_manager *dm = &adev->dm;
2621 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
2622 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
2624 if (link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) {
2625 amdgpu_dm_register_backlight_device(dm);
2627 if (dm->backlight_dev) {
2628 backlight_device_unregister(dm->backlight_dev);
2629 dm->backlight_dev = NULL;
2634 drm_connector_unregister(connector);
2635 drm_connector_cleanup(connector);
2639 void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector)
2641 struct dm_connector_state *state =
2642 to_dm_connector_state(connector->state);
2646 state = kzalloc(sizeof(*state), GFP_KERNEL);
2649 state->scaling = RMX_OFF;
2650 state->underscan_enable = false;
2651 state->underscan_hborder = 0;
2652 state->underscan_vborder = 0;
2654 connector->state = &state->base;
2655 connector->state->connector = connector;
2659 struct drm_connector_state *
2660 amdgpu_dm_connector_atomic_duplicate_state(struct drm_connector *connector)
2662 struct dm_connector_state *state =
2663 to_dm_connector_state(connector->state);
2665 struct dm_connector_state *new_state =
2666 kmemdup(state, sizeof(*state), GFP_KERNEL);
2669 __drm_atomic_helper_connector_duplicate_state(connector,
2671 return &new_state->base;
2677 static const struct drm_connector_funcs amdgpu_dm_connector_funcs = {
2678 .reset = amdgpu_dm_connector_funcs_reset,
2679 .detect = amdgpu_dm_connector_detect,
2680 .fill_modes = drm_helper_probe_single_connector_modes,
2681 .destroy = amdgpu_dm_connector_destroy,
2682 .atomic_duplicate_state = amdgpu_dm_connector_atomic_duplicate_state,
2683 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
2684 .atomic_set_property = amdgpu_dm_connector_atomic_set_property,
2685 .atomic_get_property = amdgpu_dm_connector_atomic_get_property
2688 static struct drm_encoder *best_encoder(struct drm_connector *connector)
2690 int enc_id = connector->encoder_ids[0];
2691 struct drm_mode_object *obj;
2692 struct drm_encoder *encoder;
2694 DRM_DEBUG_DRIVER("Finding the best encoder\n");
2696 /* pick the encoder ids */
2698 obj = drm_mode_object_find(connector->dev, NULL, enc_id, DRM_MODE_OBJECT_ENCODER);
2700 DRM_ERROR("Couldn't find a matching encoder for our connector\n");
2703 encoder = obj_to_encoder(obj);
2706 DRM_ERROR("No encoder id\n");
2710 static int get_modes(struct drm_connector *connector)
2712 return amdgpu_dm_connector_get_modes(connector);
2715 static void create_eml_sink(struct amdgpu_dm_connector *aconnector)
2717 struct dc_sink_init_data init_params = {
2718 .link = aconnector->dc_link,
2719 .sink_signal = SIGNAL_TYPE_VIRTUAL
2723 if (!aconnector->base.edid_blob_ptr) {
2724 DRM_ERROR("No EDID firmware found on connector: %s ,forcing to OFF!\n",
2725 aconnector->base.name);
2727 aconnector->base.force = DRM_FORCE_OFF;
2728 aconnector->base.override_edid = false;
2732 edid = (struct edid *) aconnector->base.edid_blob_ptr->data;
2734 aconnector->edid = edid;
2736 aconnector->dc_em_sink = dc_link_add_remote_sink(
2737 aconnector->dc_link,
2739 (edid->extensions + 1) * EDID_LENGTH,
2742 if (aconnector->base.force == DRM_FORCE_ON)
2743 aconnector->dc_sink = aconnector->dc_link->local_sink ?
2744 aconnector->dc_link->local_sink :
2745 aconnector->dc_em_sink;
2748 static void handle_edid_mgmt(struct amdgpu_dm_connector *aconnector)
2750 struct dc_link *link = (struct dc_link *)aconnector->dc_link;
2752 /* In case of headless boot with force on for DP managed connector
2753 * Those settings have to be != 0 to get initial modeset
2755 if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT) {
2756 link->verified_link_cap.lane_count = LANE_COUNT_FOUR;
2757 link->verified_link_cap.link_rate = LINK_RATE_HIGH2;
2761 aconnector->base.override_edid = true;
2762 create_eml_sink(aconnector);
2765 int amdgpu_dm_connector_mode_valid(struct drm_connector *connector,
2766 struct drm_display_mode *mode)
2768 int result = MODE_ERROR;
2769 struct dc_sink *dc_sink;
2770 struct amdgpu_device *adev = connector->dev->dev_private;
2771 /* TODO: Unhardcode stream count */
2772 struct dc_stream_state *stream;
2773 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
2774 enum dc_status dc_result = DC_OK;
2776 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
2777 (mode->flags & DRM_MODE_FLAG_DBLSCAN))
2780 /* Only run this the first time mode_valid is called to initilialize
2783 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED &&
2784 !aconnector->dc_em_sink)
2785 handle_edid_mgmt(aconnector);
2787 dc_sink = to_amdgpu_dm_connector(connector)->dc_sink;
2789 if (dc_sink == NULL) {
2790 DRM_ERROR("dc_sink is NULL!\n");
2794 stream = create_stream_for_sink(aconnector, mode, NULL);
2795 if (stream == NULL) {
2796 DRM_ERROR("Failed to create stream for sink!\n");
2800 dc_result = dc_validate_stream(adev->dm.dc, stream);
2802 if (dc_result == DC_OK)
2805 DRM_DEBUG_KMS("Mode %dx%d (clk %d) failed DC validation with error %d\n",
2811 dc_stream_release(stream);
2814 /* TODO: error handling*/
2818 static const struct drm_connector_helper_funcs
2819 amdgpu_dm_connector_helper_funcs = {
2821 * If hotplug a second bigger display in FB Con mode, bigger resolution
2822 * modes will be filtered by drm_mode_validate_size(), and those modes
2823 * is missing after user start lightdm. So we need to renew modes list.
2824 * in get_modes call back, not just return the modes count
2826 .get_modes = get_modes,
2827 .mode_valid = amdgpu_dm_connector_mode_valid,
2828 .best_encoder = best_encoder
2831 static void dm_crtc_helper_disable(struct drm_crtc *crtc)
2835 static int dm_crtc_helper_atomic_check(struct drm_crtc *crtc,
2836 struct drm_crtc_state *state)
2838 struct amdgpu_device *adev = crtc->dev->dev_private;
2839 struct dc *dc = adev->dm.dc;
2840 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(state);
2843 if (unlikely(!dm_crtc_state->stream &&
2844 modeset_required(state, NULL, dm_crtc_state->stream))) {
2849 /* In some use cases, like reset, no stream is attached */
2850 if (!dm_crtc_state->stream)
2853 if (dc_validate_stream(dc, dm_crtc_state->stream) == DC_OK)
2859 static bool dm_crtc_helper_mode_fixup(struct drm_crtc *crtc,
2860 const struct drm_display_mode *mode,
2861 struct drm_display_mode *adjusted_mode)
2866 static const struct drm_crtc_helper_funcs amdgpu_dm_crtc_helper_funcs = {
2867 .disable = dm_crtc_helper_disable,
2868 .atomic_check = dm_crtc_helper_atomic_check,
2869 .mode_fixup = dm_crtc_helper_mode_fixup
2872 static void dm_encoder_helper_disable(struct drm_encoder *encoder)
2877 static int dm_encoder_helper_atomic_check(struct drm_encoder *encoder,
2878 struct drm_crtc_state *crtc_state,
2879 struct drm_connector_state *conn_state)
2884 const struct drm_encoder_helper_funcs amdgpu_dm_encoder_helper_funcs = {
2885 .disable = dm_encoder_helper_disable,
2886 .atomic_check = dm_encoder_helper_atomic_check
2889 static void dm_drm_plane_reset(struct drm_plane *plane)
2891 struct dm_plane_state *amdgpu_state = NULL;
2894 plane->funcs->atomic_destroy_state(plane, plane->state);
2896 amdgpu_state = kzalloc(sizeof(*amdgpu_state), GFP_KERNEL);
2897 WARN_ON(amdgpu_state == NULL);
2900 plane->state = &amdgpu_state->base;
2901 plane->state->plane = plane;
2902 plane->state->rotation = DRM_MODE_ROTATE_0;
2906 static struct drm_plane_state *
2907 dm_drm_plane_duplicate_state(struct drm_plane *plane)
2909 struct dm_plane_state *dm_plane_state, *old_dm_plane_state;
2911 old_dm_plane_state = to_dm_plane_state(plane->state);
2912 dm_plane_state = kzalloc(sizeof(*dm_plane_state), GFP_KERNEL);
2913 if (!dm_plane_state)
2916 __drm_atomic_helper_plane_duplicate_state(plane, &dm_plane_state->base);
2918 if (old_dm_plane_state->dc_state) {
2919 dm_plane_state->dc_state = old_dm_plane_state->dc_state;
2920 dc_plane_state_retain(dm_plane_state->dc_state);
2923 return &dm_plane_state->base;
2926 void dm_drm_plane_destroy_state(struct drm_plane *plane,
2927 struct drm_plane_state *state)
2929 struct dm_plane_state *dm_plane_state = to_dm_plane_state(state);
2931 if (dm_plane_state->dc_state)
2932 dc_plane_state_release(dm_plane_state->dc_state);
2934 drm_atomic_helper_plane_destroy_state(plane, state);
2937 static const struct drm_plane_funcs dm_plane_funcs = {
2938 .update_plane = drm_atomic_helper_update_plane,
2939 .disable_plane = drm_atomic_helper_disable_plane,
2940 .destroy = drm_plane_cleanup,
2941 .reset = dm_drm_plane_reset,
2942 .atomic_duplicate_state = dm_drm_plane_duplicate_state,
2943 .atomic_destroy_state = dm_drm_plane_destroy_state,
2946 static int dm_plane_helper_prepare_fb(struct drm_plane *plane,
2947 struct drm_plane_state *new_state)
2949 struct amdgpu_framebuffer *afb;
2950 struct drm_gem_object *obj;
2951 struct amdgpu_device *adev;
2952 struct amdgpu_bo *rbo;
2953 uint64_t chroma_addr = 0;
2954 struct dm_plane_state *dm_plane_state_new, *dm_plane_state_old;
2955 unsigned int awidth;
2959 dm_plane_state_old = to_dm_plane_state(plane->state);
2960 dm_plane_state_new = to_dm_plane_state(new_state);
2962 if (!new_state->fb) {
2963 DRM_DEBUG_DRIVER("No FB bound\n");
2967 afb = to_amdgpu_framebuffer(new_state->fb);
2970 rbo = gem_to_amdgpu_bo(obj);
2971 adev = amdgpu_ttm_adev(rbo->tbo.bdev);
2972 r = amdgpu_bo_reserve(rbo, false);
2973 if (unlikely(r != 0))
2976 if (plane->type != DRM_PLANE_TYPE_CURSOR)
2977 domain = amdgpu_display_framebuffer_domains(adev);
2979 domain = AMDGPU_GEM_DOMAIN_VRAM;
2981 r = amdgpu_bo_pin(rbo, domain, &afb->address);
2983 amdgpu_bo_unreserve(rbo);
2985 if (unlikely(r != 0)) {
2986 if (r != -ERESTARTSYS)
2987 DRM_ERROR("Failed to pin framebuffer with error %d\n", r);
2993 if (dm_plane_state_new->dc_state &&
2994 dm_plane_state_old->dc_state != dm_plane_state_new->dc_state) {
2995 struct dc_plane_state *plane_state = dm_plane_state_new->dc_state;
2997 if (plane_state->format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) {
2998 plane_state->address.grph.addr.low_part = lower_32_bits(afb->address);
2999 plane_state->address.grph.addr.high_part = upper_32_bits(afb->address);
3001 awidth = ALIGN(new_state->fb->width, 64);
3002 plane_state->address.type = PLN_ADDR_TYPE_VIDEO_PROGRESSIVE;
3003 plane_state->address.video_progressive.luma_addr.low_part
3004 = lower_32_bits(afb->address);
3005 plane_state->address.video_progressive.luma_addr.high_part
3006 = upper_32_bits(afb->address);
3007 chroma_addr = afb->address + (u64)awidth * new_state->fb->height;
3008 plane_state->address.video_progressive.chroma_addr.low_part
3009 = lower_32_bits(chroma_addr);
3010 plane_state->address.video_progressive.chroma_addr.high_part
3011 = upper_32_bits(chroma_addr);
3015 /* It's a hack for s3 since in 4.9 kernel filter out cursor buffer
3016 * prepare and cleanup in drm_atomic_helper_prepare_planes
3017 * and drm_atomic_helper_cleanup_planes because fb doens't in s3.
3018 * IN 4.10 kernel this code should be removed and amdgpu_device_suspend
3019 * code touching fram buffers should be avoided for DC.
3021 if (plane->type == DRM_PLANE_TYPE_CURSOR) {
3022 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(new_state->crtc);
3024 acrtc->cursor_bo = obj;
3029 static void dm_plane_helper_cleanup_fb(struct drm_plane *plane,
3030 struct drm_plane_state *old_state)
3032 struct amdgpu_bo *rbo;
3033 struct amdgpu_framebuffer *afb;
3039 afb = to_amdgpu_framebuffer(old_state->fb);
3040 rbo = gem_to_amdgpu_bo(afb->obj);
3041 r = amdgpu_bo_reserve(rbo, false);
3043 DRM_ERROR("failed to reserve rbo before unpin\n");
3047 amdgpu_bo_unpin(rbo);
3048 amdgpu_bo_unreserve(rbo);
3049 amdgpu_bo_unref(&rbo);
3052 static int dm_plane_atomic_check(struct drm_plane *plane,
3053 struct drm_plane_state *state)
3055 struct amdgpu_device *adev = plane->dev->dev_private;
3056 struct dc *dc = adev->dm.dc;
3057 struct dm_plane_state *dm_plane_state = to_dm_plane_state(state);
3059 if (!dm_plane_state->dc_state)
3062 if (dc_validate_plane(dc, dm_plane_state->dc_state) == DC_OK)
3068 static const struct drm_plane_helper_funcs dm_plane_helper_funcs = {
3069 .prepare_fb = dm_plane_helper_prepare_fb,
3070 .cleanup_fb = dm_plane_helper_cleanup_fb,
3071 .atomic_check = dm_plane_atomic_check,
3075 * TODO: these are currently initialized to rgb formats only.
3076 * For future use cases we should either initialize them dynamically based on
3077 * plane capabilities, or initialize this array to all formats, so internal drm
3078 * check will succeed, and let DC to implement proper check
3080 static const uint32_t rgb_formats[] = {
3082 DRM_FORMAT_XRGB8888,
3083 DRM_FORMAT_ARGB8888,
3084 DRM_FORMAT_RGBA8888,
3085 DRM_FORMAT_XRGB2101010,
3086 DRM_FORMAT_XBGR2101010,
3087 DRM_FORMAT_ARGB2101010,
3088 DRM_FORMAT_ABGR2101010,
3091 static const uint32_t yuv_formats[] = {
3096 static const u32 cursor_formats[] = {
3100 static int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
3101 struct amdgpu_plane *aplane,
3102 unsigned long possible_crtcs)
3106 switch (aplane->base.type) {
3107 case DRM_PLANE_TYPE_PRIMARY:
3108 aplane->base.format_default = true;
3110 res = drm_universal_plane_init(
3116 ARRAY_SIZE(rgb_formats),
3117 NULL, aplane->base.type, NULL);
3119 case DRM_PLANE_TYPE_OVERLAY:
3120 res = drm_universal_plane_init(
3126 ARRAY_SIZE(yuv_formats),
3127 NULL, aplane->base.type, NULL);
3129 case DRM_PLANE_TYPE_CURSOR:
3130 res = drm_universal_plane_init(
3136 ARRAY_SIZE(cursor_formats),
3137 NULL, aplane->base.type, NULL);
3141 drm_plane_helper_add(&aplane->base, &dm_plane_helper_funcs);
3143 /* Create (reset) the plane state */
3144 if (aplane->base.funcs->reset)
3145 aplane->base.funcs->reset(&aplane->base);
3151 static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
3152 struct drm_plane *plane,
3153 uint32_t crtc_index)
3155 struct amdgpu_crtc *acrtc = NULL;
3156 struct amdgpu_plane *cursor_plane;
3160 cursor_plane = kzalloc(sizeof(*cursor_plane), GFP_KERNEL);
3164 cursor_plane->base.type = DRM_PLANE_TYPE_CURSOR;
3165 res = amdgpu_dm_plane_init(dm, cursor_plane, 0);
3167 acrtc = kzalloc(sizeof(struct amdgpu_crtc), GFP_KERNEL);
3171 res = drm_crtc_init_with_planes(
3175 &cursor_plane->base,
3176 &amdgpu_dm_crtc_funcs, NULL);
3181 drm_crtc_helper_add(&acrtc->base, &amdgpu_dm_crtc_helper_funcs);
3183 /* Create (reset) the plane state */
3184 if (acrtc->base.funcs->reset)
3185 acrtc->base.funcs->reset(&acrtc->base);
3187 acrtc->max_cursor_width = dm->adev->dm.dc->caps.max_cursor_size;
3188 acrtc->max_cursor_height = dm->adev->dm.dc->caps.max_cursor_size;
3190 acrtc->crtc_id = crtc_index;
3191 acrtc->base.enabled = false;
3193 dm->adev->mode_info.crtcs[crtc_index] = acrtc;
3194 drm_crtc_enable_color_mgmt(&acrtc->base, MAX_COLOR_LUT_ENTRIES,
3195 true, MAX_COLOR_LUT_ENTRIES);
3196 drm_mode_crtc_set_gamma_size(&acrtc->base, MAX_COLOR_LUT_ENTRIES);
3202 kfree(cursor_plane);
3207 static int to_drm_connector_type(enum signal_type st)
3210 case SIGNAL_TYPE_HDMI_TYPE_A:
3211 return DRM_MODE_CONNECTOR_HDMIA;
3212 case SIGNAL_TYPE_EDP:
3213 return DRM_MODE_CONNECTOR_eDP;
3214 case SIGNAL_TYPE_RGB:
3215 return DRM_MODE_CONNECTOR_VGA;
3216 case SIGNAL_TYPE_DISPLAY_PORT:
3217 case SIGNAL_TYPE_DISPLAY_PORT_MST:
3218 return DRM_MODE_CONNECTOR_DisplayPort;
3219 case SIGNAL_TYPE_DVI_DUAL_LINK:
3220 case SIGNAL_TYPE_DVI_SINGLE_LINK:
3221 return DRM_MODE_CONNECTOR_DVID;
3222 case SIGNAL_TYPE_VIRTUAL:
3223 return DRM_MODE_CONNECTOR_VIRTUAL;
3226 return DRM_MODE_CONNECTOR_Unknown;
3230 static void amdgpu_dm_get_native_mode(struct drm_connector *connector)
3232 const struct drm_connector_helper_funcs *helper =
3233 connector->helper_private;
3234 struct drm_encoder *encoder;
3235 struct amdgpu_encoder *amdgpu_encoder;
3237 encoder = helper->best_encoder(connector);
3239 if (encoder == NULL)
3242 amdgpu_encoder = to_amdgpu_encoder(encoder);
3244 amdgpu_encoder->native_mode.clock = 0;
3246 if (!list_empty(&connector->probed_modes)) {
3247 struct drm_display_mode *preferred_mode = NULL;
3249 list_for_each_entry(preferred_mode,
3250 &connector->probed_modes,
3252 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED)
3253 amdgpu_encoder->native_mode = *preferred_mode;
3261 static struct drm_display_mode *
3262 amdgpu_dm_create_common_mode(struct drm_encoder *encoder,
3264 int hdisplay, int vdisplay)
3266 struct drm_device *dev = encoder->dev;
3267 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3268 struct drm_display_mode *mode = NULL;
3269 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
3271 mode = drm_mode_duplicate(dev, native_mode);
3276 mode->hdisplay = hdisplay;
3277 mode->vdisplay = vdisplay;
3278 mode->type &= ~DRM_MODE_TYPE_PREFERRED;
3279 strncpy(mode->name, name, DRM_DISPLAY_MODE_LEN);
3285 static void amdgpu_dm_connector_add_common_modes(struct drm_encoder *encoder,
3286 struct drm_connector *connector)
3288 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3289 struct drm_display_mode *mode = NULL;
3290 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
3291 struct amdgpu_dm_connector *amdgpu_dm_connector =
3292 to_amdgpu_dm_connector(connector);
3296 char name[DRM_DISPLAY_MODE_LEN];
3299 } common_modes[] = {
3300 { "640x480", 640, 480},
3301 { "800x600", 800, 600},
3302 { "1024x768", 1024, 768},
3303 { "1280x720", 1280, 720},
3304 { "1280x800", 1280, 800},
3305 {"1280x1024", 1280, 1024},
3306 { "1440x900", 1440, 900},
3307 {"1680x1050", 1680, 1050},
3308 {"1600x1200", 1600, 1200},
3309 {"1920x1080", 1920, 1080},
3310 {"1920x1200", 1920, 1200}
3313 n = ARRAY_SIZE(common_modes);
3315 for (i = 0; i < n; i++) {
3316 struct drm_display_mode *curmode = NULL;
3317 bool mode_existed = false;
3319 if (common_modes[i].w > native_mode->hdisplay ||
3320 common_modes[i].h > native_mode->vdisplay ||
3321 (common_modes[i].w == native_mode->hdisplay &&
3322 common_modes[i].h == native_mode->vdisplay))
3325 list_for_each_entry(curmode, &connector->probed_modes, head) {
3326 if (common_modes[i].w == curmode->hdisplay &&
3327 common_modes[i].h == curmode->vdisplay) {
3328 mode_existed = true;
3336 mode = amdgpu_dm_create_common_mode(encoder,
3337 common_modes[i].name, common_modes[i].w,
3339 drm_mode_probed_add(connector, mode);
3340 amdgpu_dm_connector->num_modes++;
3344 static void amdgpu_dm_connector_ddc_get_modes(struct drm_connector *connector,
3347 struct amdgpu_dm_connector *amdgpu_dm_connector =
3348 to_amdgpu_dm_connector(connector);
3351 /* empty probed_modes */
3352 INIT_LIST_HEAD(&connector->probed_modes);
3353 amdgpu_dm_connector->num_modes =
3354 drm_add_edid_modes(connector, edid);
3356 amdgpu_dm_get_native_mode(connector);
3358 amdgpu_dm_connector->num_modes = 0;
3362 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector)
3364 const struct drm_connector_helper_funcs *helper =
3365 connector->helper_private;
3366 struct amdgpu_dm_connector *amdgpu_dm_connector =
3367 to_amdgpu_dm_connector(connector);
3368 struct drm_encoder *encoder;
3369 struct edid *edid = amdgpu_dm_connector->edid;
3371 encoder = helper->best_encoder(connector);
3372 amdgpu_dm_connector_ddc_get_modes(connector, edid);
3373 amdgpu_dm_connector_add_common_modes(encoder, connector);
3375 #if defined(CONFIG_DRM_AMD_DC_FBC)
3376 amdgpu_dm_fbc_init(connector);
3378 return amdgpu_dm_connector->num_modes;
3381 void amdgpu_dm_connector_init_helper(struct amdgpu_display_manager *dm,
3382 struct amdgpu_dm_connector *aconnector,
3384 struct dc_link *link,
3387 struct amdgpu_device *adev = dm->ddev->dev_private;
3389 aconnector->connector_id = link_index;
3390 aconnector->dc_link = link;
3391 aconnector->base.interlace_allowed = false;
3392 aconnector->base.doublescan_allowed = false;
3393 aconnector->base.stereo_allowed = false;
3394 aconnector->base.dpms = DRM_MODE_DPMS_OFF;
3395 aconnector->hpd.hpd = AMDGPU_HPD_NONE; /* not used */
3397 mutex_init(&aconnector->hpd_lock);
3399 /* configure support HPD hot plug connector_>polled default value is 0
3400 * which means HPD hot plug not supported
3402 switch (connector_type) {
3403 case DRM_MODE_CONNECTOR_HDMIA:
3404 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
3406 case DRM_MODE_CONNECTOR_DisplayPort:
3407 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
3409 case DRM_MODE_CONNECTOR_DVID:
3410 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
3416 drm_object_attach_property(&aconnector->base.base,
3417 dm->ddev->mode_config.scaling_mode_property,
3418 DRM_MODE_SCALE_NONE);
3420 drm_object_attach_property(&aconnector->base.base,
3421 adev->mode_info.underscan_property,
3423 drm_object_attach_property(&aconnector->base.base,
3424 adev->mode_info.underscan_hborder_property,
3426 drm_object_attach_property(&aconnector->base.base,
3427 adev->mode_info.underscan_vborder_property,
3432 static int amdgpu_dm_i2c_xfer(struct i2c_adapter *i2c_adap,
3433 struct i2c_msg *msgs, int num)
3435 struct amdgpu_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
3436 struct ddc_service *ddc_service = i2c->ddc_service;
3437 struct i2c_command cmd;
3441 cmd.payloads = kcalloc(num, sizeof(struct i2c_payload), GFP_KERNEL);
3446 cmd.number_of_payloads = num;
3447 cmd.engine = I2C_COMMAND_ENGINE_DEFAULT;
3450 for (i = 0; i < num; i++) {
3451 cmd.payloads[i].write = !(msgs[i].flags & I2C_M_RD);
3452 cmd.payloads[i].address = msgs[i].addr;
3453 cmd.payloads[i].length = msgs[i].len;
3454 cmd.payloads[i].data = msgs[i].buf;
3457 if (dal_i2caux_submit_i2c_command(
3458 ddc_service->ctx->i2caux,
3459 ddc_service->ddc_pin,
3463 kfree(cmd.payloads);
3467 static u32 amdgpu_dm_i2c_func(struct i2c_adapter *adap)
3469 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
3472 static const struct i2c_algorithm amdgpu_dm_i2c_algo = {
3473 .master_xfer = amdgpu_dm_i2c_xfer,
3474 .functionality = amdgpu_dm_i2c_func,
3477 static struct amdgpu_i2c_adapter *
3478 create_i2c(struct ddc_service *ddc_service,
3482 struct amdgpu_device *adev = ddc_service->ctx->driver_context;
3483 struct amdgpu_i2c_adapter *i2c;
3485 i2c = kzalloc(sizeof(struct amdgpu_i2c_adapter), GFP_KERNEL);
3488 i2c->base.owner = THIS_MODULE;
3489 i2c->base.class = I2C_CLASS_DDC;
3490 i2c->base.dev.parent = &adev->pdev->dev;
3491 i2c->base.algo = &amdgpu_dm_i2c_algo;
3492 snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c hw bus %d", link_index);
3493 i2c_set_adapdata(&i2c->base, i2c);
3494 i2c->ddc_service = ddc_service;
3499 /* Note: this function assumes that dc_link_detect() was called for the
3500 * dc_link which will be represented by this aconnector.
3502 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
3503 struct amdgpu_dm_connector *aconnector,
3504 uint32_t link_index,
3505 struct amdgpu_encoder *aencoder)
3509 struct dc *dc = dm->dc;
3510 struct dc_link *link = dc_get_link_at_index(dc, link_index);
3511 struct amdgpu_i2c_adapter *i2c;
3513 link->priv = aconnector;
3515 DRM_DEBUG_DRIVER("%s()\n", __func__);
3517 i2c = create_i2c(link->ddc, link->link_index, &res);
3519 DRM_ERROR("Failed to create i2c adapter data\n");
3523 aconnector->i2c = i2c;
3524 res = i2c_add_adapter(&i2c->base);
3527 DRM_ERROR("Failed to register hw i2c %d\n", link->link_index);
3531 connector_type = to_drm_connector_type(link->connector_signal);
3533 res = drm_connector_init(
3536 &amdgpu_dm_connector_funcs,
3540 DRM_ERROR("connector_init failed\n");
3541 aconnector->connector_id = -1;
3545 drm_connector_helper_add(
3547 &amdgpu_dm_connector_helper_funcs);
3549 if (aconnector->base.funcs->reset)
3550 aconnector->base.funcs->reset(&aconnector->base);
3552 amdgpu_dm_connector_init_helper(
3559 drm_mode_connector_attach_encoder(
3560 &aconnector->base, &aencoder->base);
3562 drm_connector_register(&aconnector->base);
3564 if (connector_type == DRM_MODE_CONNECTOR_DisplayPort
3565 || connector_type == DRM_MODE_CONNECTOR_eDP)
3566 amdgpu_dm_initialize_dp_connector(dm, aconnector);
3568 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
3569 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
3571 /* NOTE: this currently will create backlight device even if a panel
3572 * is not connected to the eDP/LVDS connector.
3574 * This is less than ideal but we don't have sink information at this
3575 * stage since detection happens after. We can't do detection earlier
3576 * since MST detection needs connectors to be created first.
3578 if (link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) {
3579 /* Event if registration failed, we should continue with
3580 * DM initialization because not having a backlight control
3581 * is better then a black screen.
3583 amdgpu_dm_register_backlight_device(dm);
3585 if (dm->backlight_dev)
3586 dm->backlight_link = link;
3593 aconnector->i2c = NULL;
3598 int amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device *adev)
3600 switch (adev->mode_info.num_crtc) {
3617 static int amdgpu_dm_encoder_init(struct drm_device *dev,
3618 struct amdgpu_encoder *aencoder,
3619 uint32_t link_index)
3621 struct amdgpu_device *adev = dev->dev_private;
3623 int res = drm_encoder_init(dev,
3625 &amdgpu_dm_encoder_funcs,
3626 DRM_MODE_ENCODER_TMDS,
3629 aencoder->base.possible_crtcs = amdgpu_dm_get_encoder_crtc_mask(adev);
3632 aencoder->encoder_id = link_index;
3634 aencoder->encoder_id = -1;
3636 drm_encoder_helper_add(&aencoder->base, &amdgpu_dm_encoder_helper_funcs);
3641 static void manage_dm_interrupts(struct amdgpu_device *adev,
3642 struct amdgpu_crtc *acrtc,
3646 * this is not correct translation but will work as soon as VBLANK
3647 * constant is the same as PFLIP
3650 amdgpu_display_crtc_idx_to_irq_type(
3655 drm_crtc_vblank_on(&acrtc->base);
3658 &adev->pageflip_irq,
3664 &adev->pageflip_irq,
3666 drm_crtc_vblank_off(&acrtc->base);
3671 is_scaling_state_different(const struct dm_connector_state *dm_state,
3672 const struct dm_connector_state *old_dm_state)
3674 if (dm_state->scaling != old_dm_state->scaling)
3676 if (!dm_state->underscan_enable && old_dm_state->underscan_enable) {
3677 if (old_dm_state->underscan_hborder != 0 && old_dm_state->underscan_vborder != 0)
3679 } else if (dm_state->underscan_enable && !old_dm_state->underscan_enable) {
3680 if (dm_state->underscan_hborder != 0 && dm_state->underscan_vborder != 0)
3682 } else if (dm_state->underscan_hborder != old_dm_state->underscan_hborder ||
3683 dm_state->underscan_vborder != old_dm_state->underscan_vborder)
3688 static void remove_stream(struct amdgpu_device *adev,
3689 struct amdgpu_crtc *acrtc,
3690 struct dc_stream_state *stream)
3692 /* this is the update mode case */
3693 if (adev->dm.freesync_module)
3694 mod_freesync_remove_stream(adev->dm.freesync_module, stream);
3696 acrtc->otg_inst = -1;
3697 acrtc->enabled = false;
3700 static int get_cursor_position(struct drm_plane *plane, struct drm_crtc *crtc,
3701 struct dc_cursor_position *position)
3703 struct amdgpu_crtc *amdgpu_crtc = amdgpu_crtc = to_amdgpu_crtc(crtc);
3705 int xorigin = 0, yorigin = 0;
3707 if (!crtc || !plane->state->fb) {
3708 position->enable = false;
3714 if ((plane->state->crtc_w > amdgpu_crtc->max_cursor_width) ||
3715 (plane->state->crtc_h > amdgpu_crtc->max_cursor_height)) {
3716 DRM_ERROR("%s: bad cursor width or height %d x %d\n",
3718 plane->state->crtc_w,
3719 plane->state->crtc_h);
3723 x = plane->state->crtc_x;
3724 y = plane->state->crtc_y;
3725 /* avivo cursor are offset into the total surface */
3726 x += crtc->primary->state->src_x >> 16;
3727 y += crtc->primary->state->src_y >> 16;
3729 xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
3733 yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
3736 position->enable = true;
3739 position->x_hotspot = xorigin;
3740 position->y_hotspot = yorigin;
3745 static void handle_cursor_update(struct drm_plane *plane,
3746 struct drm_plane_state *old_plane_state)
3748 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(plane->state->fb);
3749 struct drm_crtc *crtc = afb ? plane->state->crtc : old_plane_state->crtc;
3750 struct dm_crtc_state *crtc_state = crtc ? to_dm_crtc_state(crtc->state) : NULL;
3751 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
3752 uint64_t address = afb ? afb->address : 0;
3753 struct dc_cursor_position position;
3754 struct dc_cursor_attributes attributes;
3757 if (!plane->state->fb && !old_plane_state->fb)
3760 DRM_DEBUG_DRIVER("%s: crtc_id=%d with size %d to %d\n",
3762 amdgpu_crtc->crtc_id,
3763 plane->state->crtc_w,
3764 plane->state->crtc_h);
3766 ret = get_cursor_position(plane, crtc, &position);
3770 if (!position.enable) {
3771 /* turn off cursor */
3772 if (crtc_state && crtc_state->stream)
3773 dc_stream_set_cursor_position(crtc_state->stream,
3778 amdgpu_crtc->cursor_width = plane->state->crtc_w;
3779 amdgpu_crtc->cursor_height = plane->state->crtc_h;
3781 attributes.address.high_part = upper_32_bits(address);
3782 attributes.address.low_part = lower_32_bits(address);
3783 attributes.width = plane->state->crtc_w;
3784 attributes.height = plane->state->crtc_h;
3785 attributes.color_format = CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA;
3786 attributes.rotation_angle = 0;
3787 attributes.attribute_flags.value = 0;
3789 attributes.pitch = attributes.width;
3791 if (crtc_state->stream) {
3792 if (!dc_stream_set_cursor_attributes(crtc_state->stream,
3794 DRM_ERROR("DC failed to set cursor attributes\n");
3796 if (!dc_stream_set_cursor_position(crtc_state->stream,
3798 DRM_ERROR("DC failed to set cursor position\n");
3802 static void prepare_flip_isr(struct amdgpu_crtc *acrtc)
3805 assert_spin_locked(&acrtc->base.dev->event_lock);
3806 WARN_ON(acrtc->event);
3808 acrtc->event = acrtc->base.state->event;
3810 /* Set the flip status */
3811 acrtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
3813 /* Mark this event as consumed */
3814 acrtc->base.state->event = NULL;
3816 DRM_DEBUG_DRIVER("crtc:%d, pflip_stat:AMDGPU_FLIP_SUBMITTED\n",
3823 * Waits on all BO's fences and for proper vblank count
3825 static void amdgpu_dm_do_flip(struct drm_crtc *crtc,
3826 struct drm_framebuffer *fb,
3828 struct dc_state *state)
3830 unsigned long flags;
3831 uint32_t target_vblank;
3833 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
3834 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(fb);
3835 struct amdgpu_bo *abo = gem_to_amdgpu_bo(afb->obj);
3836 struct amdgpu_device *adev = crtc->dev->dev_private;
3837 bool async_flip = (crtc->state->pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC) != 0;
3838 struct dc_flip_addrs addr = { {0} };
3839 /* TODO eliminate or rename surface_update */
3840 struct dc_surface_update surface_updates[1] = { {0} };
3841 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
3844 /* Prepare wait for target vblank early - before the fence-waits */
3845 target_vblank = target - (uint32_t)drm_crtc_vblank_count(crtc) +
3846 amdgpu_get_vblank_counter_kms(crtc->dev, acrtc->crtc_id);
3848 /* TODO This might fail and hence better not used, wait
3849 * explicitly on fences instead
3850 * and in general should be called for
3851 * blocking commit to as per framework helpers
3853 r = amdgpu_bo_reserve(abo, true);
3854 if (unlikely(r != 0)) {
3855 DRM_ERROR("failed to reserve buffer before flip\n");
3859 /* Wait for all fences on this FB */
3860 WARN_ON(reservation_object_wait_timeout_rcu(abo->tbo.resv, true, false,
3861 MAX_SCHEDULE_TIMEOUT) < 0);
3863 amdgpu_bo_unreserve(abo);
3865 /* Wait until we're out of the vertical blank period before the one
3866 * targeted by the flip
3868 while ((acrtc->enabled &&
3869 (amdgpu_display_get_crtc_scanoutpos(adev->ddev, acrtc->crtc_id,
3870 0, &vpos, &hpos, NULL,
3871 NULL, &crtc->hwmode)
3872 & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
3873 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
3874 (int)(target_vblank -
3875 amdgpu_get_vblank_counter_kms(adev->ddev, acrtc->crtc_id)) > 0)) {
3876 usleep_range(1000, 1100);
3880 spin_lock_irqsave(&crtc->dev->event_lock, flags);
3881 /* update crtc fb */
3882 crtc->primary->fb = fb;
3884 WARN_ON(acrtc->pflip_status != AMDGPU_FLIP_NONE);
3885 WARN_ON(!acrtc_state->stream);
3887 addr.address.grph.addr.low_part = lower_32_bits(afb->address);
3888 addr.address.grph.addr.high_part = upper_32_bits(afb->address);
3889 addr.flip_immediate = async_flip;
3892 if (acrtc->base.state->event)
3893 prepare_flip_isr(acrtc);
3895 surface_updates->surface = dc_stream_get_status(acrtc_state->stream)->plane_states[0];
3896 surface_updates->flip_addr = &addr;
3899 dc_commit_updates_for_stream(adev->dm.dc,
3902 acrtc_state->stream,
3904 &surface_updates->surface,
3907 DRM_DEBUG_DRIVER("%s Flipping to hi: 0x%x, low: 0x%x \n",
3909 addr.address.grph.addr.high_part,
3910 addr.address.grph.addr.low_part);
3913 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
3916 static void amdgpu_dm_commit_planes(struct drm_atomic_state *state,
3917 struct drm_device *dev,
3918 struct amdgpu_display_manager *dm,
3919 struct drm_crtc *pcrtc,
3920 bool *wait_for_vblank)
3923 struct drm_plane *plane;
3924 struct drm_plane_state *old_plane_state, *new_plane_state;
3925 struct dc_stream_state *dc_stream_attach;
3926 struct dc_plane_state *plane_states_constructed[MAX_SURFACES];
3927 struct amdgpu_crtc *acrtc_attach = to_amdgpu_crtc(pcrtc);
3928 struct drm_crtc_state *new_pcrtc_state =
3929 drm_atomic_get_new_crtc_state(state, pcrtc);
3930 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(new_pcrtc_state);
3931 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
3932 int planes_count = 0;
3933 unsigned long flags;
3935 /* update planes when needed */
3936 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
3937 struct drm_crtc *crtc = new_plane_state->crtc;
3938 struct drm_crtc_state *new_crtc_state;
3939 struct drm_framebuffer *fb = new_plane_state->fb;
3941 struct dm_plane_state *dm_new_plane_state = to_dm_plane_state(new_plane_state);
3943 if (plane->type == DRM_PLANE_TYPE_CURSOR) {
3944 handle_cursor_update(plane, old_plane_state);
3948 if (!fb || !crtc || pcrtc != crtc)
3951 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
3952 if (!new_crtc_state->active)
3955 pflip_needed = !state->allow_modeset;
3957 spin_lock_irqsave(&crtc->dev->event_lock, flags);
3958 if (acrtc_attach->pflip_status != AMDGPU_FLIP_NONE) {
3959 DRM_ERROR("%s: acrtc %d, already busy\n",
3961 acrtc_attach->crtc_id);
3962 /* In commit tail framework this cannot happen */
3965 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
3967 if (!pflip_needed) {
3968 WARN_ON(!dm_new_plane_state->dc_state);
3970 plane_states_constructed[planes_count] = dm_new_plane_state->dc_state;
3972 dc_stream_attach = acrtc_state->stream;
3975 } else if (new_crtc_state->planes_changed) {
3976 /* Assume even ONE crtc with immediate flip means
3977 * entire can't wait for VBLANK
3978 * TODO Check if it's correct
3981 new_pcrtc_state->pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC ?
3984 /* TODO: Needs rework for multiplane flip */
3985 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
3986 drm_crtc_vblank_get(crtc);
3991 (uint32_t)drm_crtc_vblank_count(crtc) + *wait_for_vblank,
3998 unsigned long flags;
4000 if (new_pcrtc_state->event) {
4002 drm_crtc_vblank_get(pcrtc);
4004 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
4005 prepare_flip_isr(acrtc_attach);
4006 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
4009 if (false == dc_commit_planes_to_stream(dm->dc,
4010 plane_states_constructed,
4014 dm_error("%s: Failed to attach plane!\n", __func__);
4016 /*TODO BUG Here should go disable planes on CRTC. */
4021 * amdgpu_dm_crtc_copy_transient_flags - copy mirrored flags from DRM to DC
4022 * @crtc_state: the DRM CRTC state
4023 * @stream_state: the DC stream state.
4025 * Copy the mirrored transient state flags from DRM, to DC. It is used to bring
4026 * a dc_stream_state's flags in sync with a drm_crtc_state's flags.
4028 static void amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state *crtc_state,
4029 struct dc_stream_state *stream_state)
4031 stream_state->mode_changed = crtc_state->mode_changed;
4034 static int amdgpu_dm_atomic_commit(struct drm_device *dev,
4035 struct drm_atomic_state *state,
4038 struct drm_crtc *crtc;
4039 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
4040 struct amdgpu_device *adev = dev->dev_private;
4044 * We evade vblanks and pflips on crtc that
4045 * should be changed. We do it here to flush & disable
4046 * interrupts before drm_swap_state is called in drm_atomic_helper_commit
4047 * it will update crtc->dm_crtc_state->stream pointer which is used in
4050 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
4051 struct dm_crtc_state *dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
4052 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4054 if (drm_atomic_crtc_needs_modeset(new_crtc_state) && dm_old_crtc_state->stream)
4055 manage_dm_interrupts(adev, acrtc, false);
4057 /* Add check here for SoC's that support hardware cursor plane, to
4058 * unset legacy_cursor_update */
4060 return drm_atomic_helper_commit(dev, state, nonblock);
4062 /*TODO Handle EINTR, reenable IRQ*/
4065 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state)
4067 struct drm_device *dev = state->dev;
4068 struct amdgpu_device *adev = dev->dev_private;
4069 struct amdgpu_display_manager *dm = &adev->dm;
4070 struct dm_atomic_state *dm_state;
4072 struct drm_crtc *crtc;
4073 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
4074 unsigned long flags;
4075 bool wait_for_vblank = true;
4076 struct drm_connector *connector;
4077 struct drm_connector_state *old_con_state, *new_con_state;
4078 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
4080 drm_atomic_helper_update_legacy_modeset_state(dev, state);
4082 dm_state = to_dm_atomic_state(state);
4084 /* update changed items */
4085 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
4086 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4088 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4089 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
4092 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
4093 "planes_changed:%d, mode_changed:%d,active_changed:%d,"
4094 "connectors_changed:%d\n",
4096 new_crtc_state->enable,
4097 new_crtc_state->active,
4098 new_crtc_state->planes_changed,
4099 new_crtc_state->mode_changed,
4100 new_crtc_state->active_changed,
4101 new_crtc_state->connectors_changed);
4103 /* Copy all transient state flags into dc state */
4104 if (dm_new_crtc_state->stream) {
4105 amdgpu_dm_crtc_copy_transient_flags(&dm_new_crtc_state->base,
4106 dm_new_crtc_state->stream);
4109 /* handles headless hotplug case, updating new_state and
4110 * aconnector as needed
4113 if (modeset_required(new_crtc_state, dm_new_crtc_state->stream, dm_old_crtc_state->stream)) {
4115 DRM_DEBUG_DRIVER("Atomic commit: SET crtc id %d: [%p]\n", acrtc->crtc_id, acrtc);
4117 if (!dm_new_crtc_state->stream) {
4119 * this could happen because of issues with
4120 * userspace notifications delivery.
4121 * In this case userspace tries to set mode on
4122 * display which is disconnect in fact.
4123 * dc_sink in NULL in this case on aconnector.
4124 * We expect reset mode will come soon.
4126 * This can also happen when unplug is done
4127 * during resume sequence ended
4129 * In this case, we want to pretend we still
4130 * have a sink to keep the pipe running so that
4131 * hw state is consistent with the sw state
4133 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
4134 __func__, acrtc->base.base.id);
4138 if (dm_old_crtc_state->stream)
4139 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
4141 acrtc->enabled = true;
4142 acrtc->hw_mode = new_crtc_state->mode;
4143 crtc->hwmode = new_crtc_state->mode;
4144 } else if (modereset_required(new_crtc_state)) {
4145 DRM_DEBUG_DRIVER("Atomic commit: RESET. crtc id %d:[%p]\n", acrtc->crtc_id, acrtc);
4147 /* i.e. reset mode */
4148 if (dm_old_crtc_state->stream)
4149 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
4151 } /* for_each_crtc_in_state() */
4154 * Add streams after required streams from new and replaced streams
4155 * are removed from freesync module
4157 if (adev->dm.freesync_module) {
4158 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
4159 new_crtc_state, i) {
4160 struct amdgpu_dm_connector *aconnector = NULL;
4161 struct dm_connector_state *dm_new_con_state = NULL;
4162 struct amdgpu_crtc *acrtc = NULL;
4163 bool modeset_needed;
4165 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4166 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
4167 modeset_needed = modeset_required(
4169 dm_new_crtc_state->stream,
4170 dm_old_crtc_state->stream);
4171 /* We add stream to freesync if:
4172 * 1. Said stream is not null, and
4173 * 2. A modeset is requested. This means that the
4174 * stream was removed previously, and needs to be
4177 if (dm_new_crtc_state->stream == NULL ||
4181 acrtc = to_amdgpu_crtc(crtc);
4184 amdgpu_dm_find_first_crtc_matching_connector(
4187 DRM_DEBUG_DRIVER("Atomic commit: Failed to "
4188 "find connector for acrtc "
4189 "id:%d skipping freesync "
4195 mod_freesync_add_stream(adev->dm.freesync_module,
4196 dm_new_crtc_state->stream,
4198 new_con_state = drm_atomic_get_new_connector_state(
4199 state, &aconnector->base);
4200 dm_new_con_state = to_dm_connector_state(new_con_state);
4202 mod_freesync_set_user_enable(adev->dm.freesync_module,
4203 &dm_new_crtc_state->stream,
4205 &dm_new_con_state->user_enable);
4209 if (dm_state->context) {
4210 dm_enable_per_frame_crtc_master_sync(dm_state->context);
4211 WARN_ON(!dc_commit_state(dm->dc, dm_state->context));
4214 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
4215 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4217 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4219 if (dm_new_crtc_state->stream != NULL) {
4220 const struct dc_stream_status *status =
4221 dc_stream_get_status(dm_new_crtc_state->stream);
4224 DC_ERR("got no status for stream %p on acrtc%p\n", dm_new_crtc_state->stream, acrtc);
4226 acrtc->otg_inst = status->primary_otg_inst;
4230 /* Handle scaling and underscan changes*/
4231 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
4232 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
4233 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
4234 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
4235 struct dc_stream_status *status = NULL;
4238 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
4240 /* Skip any modesets/resets */
4241 if (!acrtc || drm_atomic_crtc_needs_modeset(new_crtc_state))
4244 /* Skip any thing not scale or underscan changes */
4245 if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state))
4248 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4250 update_stream_scaling_settings(&dm_new_con_state->base.crtc->mode,
4251 dm_new_con_state, (struct dc_stream_state *)dm_new_crtc_state->stream);
4253 if (!dm_new_crtc_state->stream)
4256 status = dc_stream_get_status(dm_new_crtc_state->stream);
4258 WARN_ON(!status->plane_count);
4260 /*TODO How it works with MPO ?*/
4261 if (!dc_commit_planes_to_stream(
4263 status->plane_states,
4264 status->plane_count,
4265 dm_new_crtc_state->stream,
4267 dm_error("%s: Failed to update stream scaling!\n", __func__);
4270 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
4271 new_crtc_state, i) {
4273 * loop to enable interrupts on newly arrived crtc
4275 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4276 bool modeset_needed;
4278 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4279 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
4280 modeset_needed = modeset_required(
4282 dm_new_crtc_state->stream,
4283 dm_old_crtc_state->stream);
4285 if (dm_new_crtc_state->stream == NULL || !modeset_needed)
4288 if (adev->dm.freesync_module)
4289 mod_freesync_notify_mode_change(
4290 adev->dm.freesync_module,
4291 &dm_new_crtc_state->stream, 1);
4293 manage_dm_interrupts(adev, acrtc, true);
4296 /* update planes when needed per crtc*/
4297 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j) {
4298 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4300 if (dm_new_crtc_state->stream)
4301 amdgpu_dm_commit_planes(state, dev, dm, crtc, &wait_for_vblank);
4306 * send vblank event on all events not handled in flip and
4307 * mark consumed event for drm_atomic_helper_commit_hw_done
4309 spin_lock_irqsave(&adev->ddev->event_lock, flags);
4310 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
4312 if (new_crtc_state->event)
4313 drm_send_event_locked(dev, &new_crtc_state->event->base);
4315 new_crtc_state->event = NULL;
4317 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
4319 /* Signal HW programming completion */
4320 drm_atomic_helper_commit_hw_done(state);
4322 if (wait_for_vblank)
4323 drm_atomic_helper_wait_for_flip_done(dev, state);
4325 drm_atomic_helper_cleanup_planes(dev, state);
4329 static int dm_force_atomic_commit(struct drm_connector *connector)
4332 struct drm_device *ddev = connector->dev;
4333 struct drm_atomic_state *state = drm_atomic_state_alloc(ddev);
4334 struct amdgpu_crtc *disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
4335 struct drm_plane *plane = disconnected_acrtc->base.primary;
4336 struct drm_connector_state *conn_state;
4337 struct drm_crtc_state *crtc_state;
4338 struct drm_plane_state *plane_state;
4343 state->acquire_ctx = ddev->mode_config.acquire_ctx;
4345 /* Construct an atomic state to restore previous display setting */
4348 * Attach connectors to drm_atomic_state
4350 conn_state = drm_atomic_get_connector_state(state, connector);
4352 ret = PTR_ERR_OR_ZERO(conn_state);
4356 /* Attach crtc to drm_atomic_state*/
4357 crtc_state = drm_atomic_get_crtc_state(state, &disconnected_acrtc->base);
4359 ret = PTR_ERR_OR_ZERO(crtc_state);
4363 /* force a restore */
4364 crtc_state->mode_changed = true;
4366 /* Attach plane to drm_atomic_state */
4367 plane_state = drm_atomic_get_plane_state(state, plane);
4369 ret = PTR_ERR_OR_ZERO(plane_state);
4374 /* Call commit internally with the state we just constructed */
4375 ret = drm_atomic_commit(state);
4380 DRM_ERROR("Restoring old state failed with %i\n", ret);
4381 drm_atomic_state_put(state);
4387 * This functions handle all cases when set mode does not come upon hotplug.
4388 * This include when the same display is unplugged then plugged back into the
4389 * same port and when we are running without usermode desktop manager supprot
4391 void dm_restore_drm_connector_state(struct drm_device *dev,
4392 struct drm_connector *connector)
4394 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
4395 struct amdgpu_crtc *disconnected_acrtc;
4396 struct dm_crtc_state *acrtc_state;
4398 if (!aconnector->dc_sink || !connector->state || !connector->encoder)
4401 disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
4402 if (!disconnected_acrtc)
4405 acrtc_state = to_dm_crtc_state(disconnected_acrtc->base.state);
4406 if (!acrtc_state->stream)
4410 * If the previous sink is not released and different from the current,
4411 * we deduce we are in a state where we can not rely on usermode call
4412 * to turn on the display, so we do it here
4414 if (acrtc_state->stream->sink != aconnector->dc_sink)
4415 dm_force_atomic_commit(&aconnector->base);
4419 * Grabs all modesetting locks to serialize against any blocking commits,
4420 * Waits for completion of all non blocking commits.
4422 static int do_aquire_global_lock(struct drm_device *dev,
4423 struct drm_atomic_state *state)
4425 struct drm_crtc *crtc;
4426 struct drm_crtc_commit *commit;
4429 /* Adding all modeset locks to aquire_ctx will
4430 * ensure that when the framework release it the
4431 * extra locks we are locking here will get released to
4433 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
4437 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
4438 spin_lock(&crtc->commit_lock);
4439 commit = list_first_entry_or_null(&crtc->commit_list,
4440 struct drm_crtc_commit, commit_entry);
4442 drm_crtc_commit_get(commit);
4443 spin_unlock(&crtc->commit_lock);
4448 /* Make sure all pending HW programming completed and
4451 ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ);
4454 ret = wait_for_completion_interruptible_timeout(
4455 &commit->flip_done, 10*HZ);
4458 DRM_ERROR("[CRTC:%d:%s] hw_done or flip_done "
4459 "timed out\n", crtc->base.id, crtc->name);
4461 drm_crtc_commit_put(commit);
4464 return ret < 0 ? ret : 0;
4467 static int dm_update_crtcs_state(struct dc *dc,
4468 struct drm_atomic_state *state,
4470 bool *lock_and_validation_needed)
4472 struct drm_crtc *crtc;
4473 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
4475 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
4476 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
4477 struct dc_stream_state *new_stream;
4480 /*TODO Move this code into dm_crtc_atomic_check once we get rid of dc_validation_set */
4481 /* update changed items */
4482 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
4483 struct amdgpu_crtc *acrtc = NULL;
4484 struct amdgpu_dm_connector *aconnector = NULL;
4485 struct drm_connector_state *new_con_state = NULL;
4486 struct dm_connector_state *dm_conn_state = NULL;
4490 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
4491 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4492 acrtc = to_amdgpu_crtc(crtc);
4494 aconnector = amdgpu_dm_find_first_crtc_matching_connector(state, crtc);
4496 /* TODO This hack should go away */
4497 if (aconnector && enable) {
4498 // Make sure fake sink is created in plug-in scenario
4499 new_con_state = drm_atomic_get_connector_state(state,
4502 if (IS_ERR(new_con_state)) {
4503 ret = PTR_ERR_OR_ZERO(new_con_state);
4507 dm_conn_state = to_dm_connector_state(new_con_state);
4509 new_stream = create_stream_for_sink(aconnector,
4510 &new_crtc_state->mode,
4514 * we can have no stream on ACTION_SET if a display
4515 * was disconnected during S3, in this case it not and
4516 * error, the OS will be updated after detection, and
4517 * do the right thing on next atomic commit
4521 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
4522 __func__, acrtc->base.base.id);
4526 if (dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) &&
4527 dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream)) {
4528 new_crtc_state->mode_changed = false;
4529 DRM_DEBUG_DRIVER("Mode change not required, setting mode_changed to %d",
4530 new_crtc_state->mode_changed);
4534 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
4538 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
4539 "planes_changed:%d, mode_changed:%d,active_changed:%d,"
4540 "connectors_changed:%d\n",
4542 new_crtc_state->enable,
4543 new_crtc_state->active,
4544 new_crtc_state->planes_changed,
4545 new_crtc_state->mode_changed,
4546 new_crtc_state->active_changed,
4547 new_crtc_state->connectors_changed);
4549 /* Remove stream for any changed/disabled CRTC */
4552 if (!dm_old_crtc_state->stream)
4555 DRM_DEBUG_DRIVER("Disabling DRM crtc: %d\n",
4558 /* i.e. reset mode */
4559 if (dc_remove_stream_from_ctx(
4562 dm_old_crtc_state->stream) != DC_OK) {
4567 dc_stream_release(dm_old_crtc_state->stream);
4568 dm_new_crtc_state->stream = NULL;
4570 *lock_and_validation_needed = true;
4572 } else {/* Add stream for any updated/enabled CRTC */
4574 * Quick fix to prevent NULL pointer on new_stream when
4575 * added MST connectors not found in existing crtc_state in the chained mode
4576 * TODO: need to dig out the root cause of that
4578 if (!aconnector || (!aconnector->dc_sink && aconnector->mst_port))
4581 if (modereset_required(new_crtc_state))
4584 if (modeset_required(new_crtc_state, new_stream,
4585 dm_old_crtc_state->stream)) {
4587 WARN_ON(dm_new_crtc_state->stream);
4589 dm_new_crtc_state->stream = new_stream;
4591 dc_stream_retain(new_stream);
4593 DRM_DEBUG_DRIVER("Enabling DRM crtc: %d\n",
4596 if (dc_add_stream_to_ctx(
4599 dm_new_crtc_state->stream) != DC_OK) {
4604 *lock_and_validation_needed = true;
4609 /* Release extra reference */
4611 dc_stream_release(new_stream);
4614 * We want to do dc stream updates that do not require a
4615 * full modeset below.
4617 if (!enable || !aconnector || modereset_required(new_crtc_state))
4620 * Given above conditions, the dc state cannot be NULL because:
4621 * 1. We're attempting to enable a CRTC. Which has a...
4622 * 2. Valid connector attached, and
4623 * 3. User does not want to reset it (disable or mark inactive,
4624 * which can happen on a CRTC that's already disabled).
4625 * => It currently exists.
4627 BUG_ON(dm_new_crtc_state->stream == NULL);
4629 /* Color managment settings */
4630 if (dm_new_crtc_state->base.color_mgmt_changed) {
4631 ret = amdgpu_dm_set_regamma_lut(dm_new_crtc_state);
4634 amdgpu_dm_set_ctm(dm_new_crtc_state);
4642 dc_stream_release(new_stream);
4646 static int dm_update_planes_state(struct dc *dc,
4647 struct drm_atomic_state *state,
4649 bool *lock_and_validation_needed)
4651 struct drm_crtc *new_plane_crtc, *old_plane_crtc;
4652 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
4653 struct drm_plane *plane;
4654 struct drm_plane_state *old_plane_state, *new_plane_state;
4655 struct dm_crtc_state *dm_new_crtc_state, *dm_old_crtc_state;
4656 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
4657 struct dm_plane_state *dm_new_plane_state, *dm_old_plane_state;
4659 /* TODO return page_flip_needed() function */
4660 bool pflip_needed = !state->allow_modeset;
4666 /* Add new planes */
4667 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
4668 new_plane_crtc = new_plane_state->crtc;
4669 old_plane_crtc = old_plane_state->crtc;
4670 dm_new_plane_state = to_dm_plane_state(new_plane_state);
4671 dm_old_plane_state = to_dm_plane_state(old_plane_state);
4673 /*TODO Implement atomic check for cursor plane */
4674 if (plane->type == DRM_PLANE_TYPE_CURSOR)
4677 /* Remove any changed/removed planes */
4680 if (!old_plane_crtc)
4683 old_crtc_state = drm_atomic_get_old_crtc_state(
4684 state, old_plane_crtc);
4685 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
4687 if (!dm_old_crtc_state->stream)
4690 DRM_DEBUG_DRIVER("Disabling DRM plane: %d on DRM crtc %d\n",
4691 plane->base.id, old_plane_crtc->base.id);
4693 if (!dc_remove_plane_from_context(
4695 dm_old_crtc_state->stream,
4696 dm_old_plane_state->dc_state,
4697 dm_state->context)) {
4704 dc_plane_state_release(dm_old_plane_state->dc_state);
4705 dm_new_plane_state->dc_state = NULL;
4707 *lock_and_validation_needed = true;
4709 } else { /* Add new planes */
4711 if (drm_atomic_plane_disabling(plane->state, new_plane_state))
4714 if (!new_plane_crtc)
4717 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_crtc);
4718 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4720 if (!dm_new_crtc_state->stream)
4724 WARN_ON(dm_new_plane_state->dc_state);
4726 dm_new_plane_state->dc_state = dc_create_plane_state(dc);
4728 DRM_DEBUG_DRIVER("Enabling DRM plane: %d on DRM crtc %d\n",
4729 plane->base.id, new_plane_crtc->base.id);
4731 if (!dm_new_plane_state->dc_state) {
4736 ret = fill_plane_attributes(
4737 new_plane_crtc->dev->dev_private,
4738 dm_new_plane_state->dc_state,
4744 if (!dc_add_plane_to_context(
4746 dm_new_crtc_state->stream,
4747 dm_new_plane_state->dc_state,
4748 dm_state->context)) {
4754 /* Tell DC to do a full surface update every time there
4755 * is a plane change. Inefficient, but works for now.
4757 dm_new_plane_state->dc_state->update_flags.bits.full_update = 1;
4759 *lock_and_validation_needed = true;
4767 static int amdgpu_dm_atomic_check(struct drm_device *dev,
4768 struct drm_atomic_state *state)
4770 struct amdgpu_device *adev = dev->dev_private;
4771 struct dc *dc = adev->dm.dc;
4772 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
4773 struct drm_connector *connector;
4774 struct drm_connector_state *old_con_state, *new_con_state;
4775 struct drm_crtc *crtc;
4776 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
4780 * This bool will be set for true for any modeset/reset
4781 * or plane update which implies non fast surface update.
4783 bool lock_and_validation_needed = false;
4785 ret = drm_atomic_helper_check_modeset(dev, state);
4789 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
4790 if (!drm_atomic_crtc_needs_modeset(new_crtc_state) &&
4791 !new_crtc_state->color_mgmt_changed)
4794 if (!new_crtc_state->enable)
4797 ret = drm_atomic_add_affected_connectors(state, crtc);
4801 ret = drm_atomic_add_affected_planes(state, crtc);
4806 dm_state->context = dc_create_state();
4807 ASSERT(dm_state->context);
4808 dc_resource_state_copy_construct_current(dc, dm_state->context);
4810 /* Remove exiting planes if they are modified */
4811 ret = dm_update_planes_state(dc, state, false, &lock_and_validation_needed);
4816 /* Disable all crtcs which require disable */
4817 ret = dm_update_crtcs_state(dc, state, false, &lock_and_validation_needed);
4822 /* Enable all crtcs which require enable */
4823 ret = dm_update_crtcs_state(dc, state, true, &lock_and_validation_needed);
4828 /* Add new/modified planes */
4829 ret = dm_update_planes_state(dc, state, true, &lock_and_validation_needed);
4834 /* Run this here since we want to validate the streams we created */
4835 ret = drm_atomic_helper_check_planes(dev, state);
4839 /* Check scaling and underscan changes*/
4840 /*TODO Removed scaling changes validation due to inability to commit
4841 * new stream into context w\o causing full reset. Need to
4842 * decide how to handle.
4844 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
4845 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
4846 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
4847 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
4849 /* Skip any modesets/resets */
4850 if (!acrtc || drm_atomic_crtc_needs_modeset(
4851 drm_atomic_get_new_crtc_state(state, &acrtc->base)))
4854 /* Skip any thing not scale or underscan changes */
4855 if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state))
4858 lock_and_validation_needed = true;
4862 * For full updates case when
4863 * removing/adding/updating streams on once CRTC while flipping
4865 * acquiring global lock will guarantee that any such full
4867 * will wait for completion of any outstanding flip using DRMs
4868 * synchronization events.
4871 if (lock_and_validation_needed) {
4873 ret = do_aquire_global_lock(dev, state);
4877 if (dc_validate_global_state(dc, dm_state->context) != DC_OK) {
4883 /* Must be success */
4888 if (ret == -EDEADLK)
4889 DRM_DEBUG_DRIVER("Atomic check stopped to avoid deadlock.\n");
4890 else if (ret == -EINTR || ret == -EAGAIN || ret == -ERESTARTSYS)
4891 DRM_DEBUG_DRIVER("Atomic check stopped due to signal.\n");
4893 DRM_DEBUG_DRIVER("Atomic check failed with err: %d \n", ret);
4898 static bool is_dp_capable_without_timing_msa(struct dc *dc,
4899 struct amdgpu_dm_connector *amdgpu_dm_connector)
4902 bool capable = false;
4904 if (amdgpu_dm_connector->dc_link &&
4905 dm_helpers_dp_read_dpcd(
4907 amdgpu_dm_connector->dc_link,
4908 DP_DOWN_STREAM_PORT_COUNT,
4910 sizeof(dpcd_data))) {
4911 capable = (dpcd_data & DP_MSA_TIMING_PAR_IGNORED) ? true:false;
4916 void amdgpu_dm_add_sink_to_freesync_module(struct drm_connector *connector,
4920 uint64_t val_capable;
4921 bool edid_check_required;
4922 struct detailed_timing *timing;
4923 struct detailed_non_pixel *data;
4924 struct detailed_data_monitor_range *range;
4925 struct amdgpu_dm_connector *amdgpu_dm_connector =
4926 to_amdgpu_dm_connector(connector);
4928 struct drm_device *dev = connector->dev;
4929 struct amdgpu_device *adev = dev->dev_private;
4931 edid_check_required = false;
4932 if (!amdgpu_dm_connector->dc_sink) {
4933 DRM_ERROR("dc_sink NULL, could not add free_sync module.\n");
4936 if (!adev->dm.freesync_module)
4939 * if edid non zero restrict freesync only for dp and edp
4942 if (amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT
4943 || amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_EDP) {
4944 edid_check_required = is_dp_capable_without_timing_msa(
4946 amdgpu_dm_connector);
4950 if (edid_check_required == true && (edid->version > 1 ||
4951 (edid->version == 1 && edid->revision > 1))) {
4952 for (i = 0; i < 4; i++) {
4954 timing = &edid->detailed_timings[i];
4955 data = &timing->data.other_data;
4956 range = &data->data.range;
4958 * Check if monitor has continuous frequency mode
4960 if (data->type != EDID_DETAIL_MONITOR_RANGE)
4963 * Check for flag range limits only. If flag == 1 then
4964 * no additional timing information provided.
4965 * Default GTF, GTF Secondary curve and CVT are not
4968 if (range->flags != 1)
4971 amdgpu_dm_connector->min_vfreq = range->min_vfreq;
4972 amdgpu_dm_connector->max_vfreq = range->max_vfreq;
4973 amdgpu_dm_connector->pixel_clock_mhz =
4974 range->pixel_clock_mhz * 10;
4978 if (amdgpu_dm_connector->max_vfreq -
4979 amdgpu_dm_connector->min_vfreq > 10) {
4980 amdgpu_dm_connector->caps.supported = true;
4981 amdgpu_dm_connector->caps.min_refresh_in_micro_hz =
4982 amdgpu_dm_connector->min_vfreq * 1000000;
4983 amdgpu_dm_connector->caps.max_refresh_in_micro_hz =
4984 amdgpu_dm_connector->max_vfreq * 1000000;
4990 * TODO figure out how to notify user-mode or DRM of freesync caps
4991 * once we figure out how to deal with freesync in an upstreamable
4997 void amdgpu_dm_remove_sink_from_freesync_module(struct drm_connector *connector)
5000 * TODO fill in once we figure out how to deal with freesync in
5001 * an upstreamable fashion