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"
42 #if defined(CONFIG_DEBUG_FS)
43 #include "amdgpu_dm_debugfs.h"
46 #include "ivsrcid/ivsrcid_vislands30.h"
48 #include <linux/module.h>
49 #include <linux/moduleparam.h>
50 #include <linux/version.h>
51 #include <linux/types.h>
52 #include <linux/pm_runtime.h>
55 #include <drm/drm_atomic.h>
56 #include <drm/drm_atomic_helper.h>
57 #include <drm/drm_dp_mst_helper.h>
58 #include <drm/drm_fb_helper.h>
59 #include <drm/drm_edid.h>
61 #include "modules/inc/mod_freesync.h"
64 #include "ivsrcid/irqsrcs_dcn_1_0.h"
66 #include "dcn/dcn_1_0_offset.h"
67 #include "dcn/dcn_1_0_sh_mask.h"
68 #include "soc15_hw_ip.h"
69 #include "vega10_ip_offset.h"
71 #include "soc15_common.h"
74 #include "modules/inc/mod_freesync.h"
76 #include "i2caux_interface.h"
78 /* basic init/fini API */
79 static int amdgpu_dm_init(struct amdgpu_device *adev);
80 static void amdgpu_dm_fini(struct amdgpu_device *adev);
82 /* initializes drm_device display related structures, based on the information
83 * provided by DAL. The drm strcutures are: drm_crtc, drm_connector,
84 * drm_encoder, drm_mode_config
86 * Returns 0 on success
88 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev);
89 /* removes and deallocates the drm structures, created by the above function */
90 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm);
93 amdgpu_dm_update_connector_after_detect(struct amdgpu_dm_connector *aconnector);
95 static int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
96 struct amdgpu_plane *aplane,
97 unsigned long possible_crtcs);
98 static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
99 struct drm_plane *plane,
100 uint32_t link_index);
101 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
102 struct amdgpu_dm_connector *amdgpu_dm_connector,
104 struct amdgpu_encoder *amdgpu_encoder);
105 static int amdgpu_dm_encoder_init(struct drm_device *dev,
106 struct amdgpu_encoder *aencoder,
107 uint32_t link_index);
109 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector);
111 static int amdgpu_dm_atomic_commit(struct drm_device *dev,
112 struct drm_atomic_state *state,
115 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state);
117 static int amdgpu_dm_atomic_check(struct drm_device *dev,
118 struct drm_atomic_state *state);
123 static const enum drm_plane_type dm_plane_type_default[AMDGPU_MAX_PLANES] = {
124 DRM_PLANE_TYPE_PRIMARY,
125 DRM_PLANE_TYPE_PRIMARY,
126 DRM_PLANE_TYPE_PRIMARY,
127 DRM_PLANE_TYPE_PRIMARY,
128 DRM_PLANE_TYPE_PRIMARY,
129 DRM_PLANE_TYPE_PRIMARY,
132 static const enum drm_plane_type dm_plane_type_carizzo[AMDGPU_MAX_PLANES] = {
133 DRM_PLANE_TYPE_PRIMARY,
134 DRM_PLANE_TYPE_PRIMARY,
135 DRM_PLANE_TYPE_PRIMARY,
136 DRM_PLANE_TYPE_OVERLAY,/* YUV Capable Underlay */
139 static const enum drm_plane_type dm_plane_type_stoney[AMDGPU_MAX_PLANES] = {
140 DRM_PLANE_TYPE_PRIMARY,
141 DRM_PLANE_TYPE_PRIMARY,
142 DRM_PLANE_TYPE_OVERLAY, /* YUV Capable Underlay */
146 * dm_vblank_get_counter
149 * Get counter for number of vertical blanks
152 * struct amdgpu_device *adev - [in] desired amdgpu device
153 * int disp_idx - [in] which CRTC to get the counter from
156 * Counter for vertical blanks
158 static u32 dm_vblank_get_counter(struct amdgpu_device *adev, int crtc)
160 if (crtc >= adev->mode_info.num_crtc)
163 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
164 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(
168 if (acrtc_state->stream == NULL) {
169 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
174 return dc_stream_get_vblank_counter(acrtc_state->stream);
178 static int dm_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
179 u32 *vbl, u32 *position)
181 uint32_t v_blank_start, v_blank_end, h_position, v_position;
183 if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
186 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
187 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(
190 if (acrtc_state->stream == NULL) {
191 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
197 * TODO rework base driver to use values directly.
198 * for now parse it back into reg-format
200 dc_stream_get_scanoutpos(acrtc_state->stream,
206 *position = v_position | (h_position << 16);
207 *vbl = v_blank_start | (v_blank_end << 16);
213 static bool dm_is_idle(void *handle)
219 static int dm_wait_for_idle(void *handle)
225 static bool dm_check_soft_reset(void *handle)
230 static int dm_soft_reset(void *handle)
236 static struct amdgpu_crtc *
237 get_crtc_by_otg_inst(struct amdgpu_device *adev,
240 struct drm_device *dev = adev->ddev;
241 struct drm_crtc *crtc;
242 struct amdgpu_crtc *amdgpu_crtc;
245 * following if is check inherited from both functions where this one is
246 * used now. Need to be checked why it could happen.
248 if (otg_inst == -1) {
250 return adev->mode_info.crtcs[0];
253 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
254 amdgpu_crtc = to_amdgpu_crtc(crtc);
256 if (amdgpu_crtc->otg_inst == otg_inst)
263 static void dm_pflip_high_irq(void *interrupt_params)
265 struct amdgpu_crtc *amdgpu_crtc;
266 struct common_irq_params *irq_params = interrupt_params;
267 struct amdgpu_device *adev = irq_params->adev;
270 amdgpu_crtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_PFLIP);
272 /* IRQ could occur when in initial stage */
273 /*TODO work and BO cleanup */
274 if (amdgpu_crtc == NULL) {
275 DRM_DEBUG_DRIVER("CRTC is null, returning.\n");
279 spin_lock_irqsave(&adev->ddev->event_lock, flags);
281 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED){
282 DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d !=AMDGPU_FLIP_SUBMITTED(%d) on crtc:%d[%p] \n",
283 amdgpu_crtc->pflip_status,
284 AMDGPU_FLIP_SUBMITTED,
285 amdgpu_crtc->crtc_id,
287 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
292 /* wakeup usersapce */
293 if (amdgpu_crtc->event) {
294 /* Update to correct count/ts if racing with vblank irq */
295 drm_crtc_accurate_vblank_count(&amdgpu_crtc->base);
297 drm_crtc_send_vblank_event(&amdgpu_crtc->base, amdgpu_crtc->event);
299 /* page flip completed. clean up */
300 amdgpu_crtc->event = NULL;
305 amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
306 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
308 DRM_DEBUG_DRIVER("%s - crtc :%d[%p], pflip_stat:AMDGPU_FLIP_NONE\n",
309 __func__, amdgpu_crtc->crtc_id, amdgpu_crtc);
311 drm_crtc_vblank_put(&amdgpu_crtc->base);
314 static void dm_crtc_high_irq(void *interrupt_params)
316 struct common_irq_params *irq_params = interrupt_params;
317 struct amdgpu_device *adev = irq_params->adev;
318 uint8_t crtc_index = 0;
319 struct amdgpu_crtc *acrtc;
321 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VBLANK);
324 crtc_index = acrtc->crtc_id;
326 drm_handle_vblank(adev->ddev, crtc_index);
327 amdgpu_dm_crtc_handle_crc_irq(&acrtc->base);
330 static int dm_set_clockgating_state(void *handle,
331 enum amd_clockgating_state state)
336 static int dm_set_powergating_state(void *handle,
337 enum amd_powergating_state state)
342 /* Prototypes of private functions */
343 static int dm_early_init(void* handle);
345 static void hotplug_notify_work_func(struct work_struct *work)
347 struct amdgpu_display_manager *dm = container_of(work, struct amdgpu_display_manager, mst_hotplug_work);
348 struct drm_device *dev = dm->ddev;
350 drm_kms_helper_hotplug_event(dev);
353 /* Allocate memory for FBC compressed data */
354 static void amdgpu_dm_fbc_init(struct drm_connector *connector)
356 struct drm_device *dev = connector->dev;
357 struct amdgpu_device *adev = dev->dev_private;
358 struct dm_comressor_info *compressor = &adev->dm.compressor;
359 struct amdgpu_dm_connector *aconn = to_amdgpu_dm_connector(connector);
360 struct drm_display_mode *mode;
361 unsigned long max_size = 0;
363 if (adev->dm.dc->fbc_compressor == NULL)
366 if (aconn->dc_link->connector_signal != SIGNAL_TYPE_EDP)
369 if (compressor->bo_ptr)
373 list_for_each_entry(mode, &connector->modes, head) {
374 if (max_size < mode->htotal * mode->vtotal)
375 max_size = mode->htotal * mode->vtotal;
379 int r = amdgpu_bo_create_kernel(adev, max_size * 4, PAGE_SIZE,
380 AMDGPU_GEM_DOMAIN_GTT, &compressor->bo_ptr,
381 &compressor->gpu_addr, &compressor->cpu_addr);
384 DRM_ERROR("DM: Failed to initialize FBC\n");
386 adev->dm.dc->ctx->fbc_gpu_addr = compressor->gpu_addr;
387 DRM_INFO("DM: FBC alloc %lu\n", max_size*4);
397 * Returns 0 on success
399 static int amdgpu_dm_init(struct amdgpu_device *adev)
401 struct dc_init_data init_data;
402 adev->dm.ddev = adev->ddev;
403 adev->dm.adev = adev;
405 /* Zero all the fields */
406 memset(&init_data, 0, sizeof(init_data));
408 if(amdgpu_dm_irq_init(adev)) {
409 DRM_ERROR("amdgpu: failed to initialize DM IRQ support.\n");
413 init_data.asic_id.chip_family = adev->family;
415 init_data.asic_id.pci_revision_id = adev->rev_id;
416 init_data.asic_id.hw_internal_rev = adev->external_rev_id;
418 init_data.asic_id.vram_width = adev->gmc.vram_width;
419 /* TODO: initialize init_data.asic_id.vram_type here!!!! */
420 init_data.asic_id.atombios_base_address =
421 adev->mode_info.atom_context->bios;
423 init_data.driver = adev;
425 adev->dm.cgs_device = amdgpu_cgs_create_device(adev);
427 if (!adev->dm.cgs_device) {
428 DRM_ERROR("amdgpu: failed to create cgs device.\n");
432 init_data.cgs_device = adev->dm.cgs_device;
436 init_data.dce_environment = DCE_ENV_PRODUCTION_DRV;
439 * TODO debug why this doesn't work on Raven
441 if (adev->flags & AMD_IS_APU &&
442 adev->asic_type >= CHIP_CARRIZO &&
443 adev->asic_type < CHIP_RAVEN)
444 init_data.flags.gpu_vm_support = true;
446 /* Display Core create. */
447 adev->dm.dc = dc_create(&init_data);
450 DRM_INFO("Display Core initialized with v%s!\n", DC_VER);
452 DRM_INFO("Display Core failed to initialize with v%s!\n", DC_VER);
456 INIT_WORK(&adev->dm.mst_hotplug_work, hotplug_notify_work_func);
458 adev->dm.freesync_module = mod_freesync_create(adev->dm.dc);
459 if (!adev->dm.freesync_module) {
461 "amdgpu: failed to initialize freesync_module.\n");
463 DRM_DEBUG_DRIVER("amdgpu: freesync_module init done %p.\n",
464 adev->dm.freesync_module);
466 amdgpu_dm_init_color_mod();
468 if (amdgpu_dm_initialize_drm_device(adev)) {
470 "amdgpu: failed to initialize sw for display support.\n");
474 /* Update the actual used number of crtc */
475 adev->mode_info.num_crtc = adev->dm.display_indexes_num;
477 /* TODO: Add_display_info? */
479 /* TODO use dynamic cursor width */
480 adev->ddev->mode_config.cursor_width = adev->dm.dc->caps.max_cursor_size;
481 adev->ddev->mode_config.cursor_height = adev->dm.dc->caps.max_cursor_size;
483 if (drm_vblank_init(adev->ddev, adev->dm.display_indexes_num)) {
485 "amdgpu: failed to initialize sw for display support.\n");
489 DRM_DEBUG_DRIVER("KMS initialized.\n");
493 amdgpu_dm_fini(adev);
498 static void amdgpu_dm_fini(struct amdgpu_device *adev)
500 amdgpu_dm_destroy_drm_device(&adev->dm);
502 * TODO: pageflip, vlank interrupt
504 * amdgpu_dm_irq_fini(adev);
507 if (adev->dm.cgs_device) {
508 amdgpu_cgs_destroy_device(adev->dm.cgs_device);
509 adev->dm.cgs_device = NULL;
511 if (adev->dm.freesync_module) {
512 mod_freesync_destroy(adev->dm.freesync_module);
513 adev->dm.freesync_module = NULL;
515 /* DC Destroy TODO: Replace destroy DAL */
517 dc_destroy(&adev->dm.dc);
521 static int dm_sw_init(void *handle)
526 static int dm_sw_fini(void *handle)
531 static int detect_mst_link_for_all_connectors(struct drm_device *dev)
533 struct amdgpu_dm_connector *aconnector;
534 struct drm_connector *connector;
537 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
539 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
540 aconnector = to_amdgpu_dm_connector(connector);
541 if (aconnector->dc_link->type == dc_connection_mst_branch &&
542 aconnector->mst_mgr.aux) {
543 DRM_DEBUG_DRIVER("DM_MST: starting TM on aconnector: %p [id: %d]\n",
544 aconnector, aconnector->base.base.id);
546 ret = drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true);
548 DRM_ERROR("DM_MST: Failed to start MST\n");
549 ((struct dc_link *)aconnector->dc_link)->type = dc_connection_single;
555 drm_modeset_unlock(&dev->mode_config.connection_mutex);
559 static int dm_late_init(void *handle)
561 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
563 return detect_mst_link_for_all_connectors(adev->ddev);
566 static void s3_handle_mst(struct drm_device *dev, bool suspend)
568 struct amdgpu_dm_connector *aconnector;
569 struct drm_connector *connector;
571 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
573 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
574 aconnector = to_amdgpu_dm_connector(connector);
575 if (aconnector->dc_link->type == dc_connection_mst_branch &&
576 !aconnector->mst_port) {
579 drm_dp_mst_topology_mgr_suspend(&aconnector->mst_mgr);
581 drm_dp_mst_topology_mgr_resume(&aconnector->mst_mgr);
585 drm_modeset_unlock(&dev->mode_config.connection_mutex);
588 static int dm_hw_init(void *handle)
590 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
591 /* Create DAL display manager */
592 amdgpu_dm_init(adev);
593 amdgpu_dm_hpd_init(adev);
598 static int dm_hw_fini(void *handle)
600 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
602 amdgpu_dm_hpd_fini(adev);
604 amdgpu_dm_irq_fini(adev);
605 amdgpu_dm_fini(adev);
609 static int dm_suspend(void *handle)
611 struct amdgpu_device *adev = handle;
612 struct amdgpu_display_manager *dm = &adev->dm;
615 s3_handle_mst(adev->ddev, true);
617 amdgpu_dm_irq_suspend(adev);
619 WARN_ON(adev->dm.cached_state);
620 adev->dm.cached_state = drm_atomic_helper_suspend(adev->ddev);
622 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D3);
627 static struct amdgpu_dm_connector *
628 amdgpu_dm_find_first_crtc_matching_connector(struct drm_atomic_state *state,
629 struct drm_crtc *crtc)
632 struct drm_connector_state *new_con_state;
633 struct drm_connector *connector;
634 struct drm_crtc *crtc_from_state;
636 for_each_new_connector_in_state(state, connector, new_con_state, i) {
637 crtc_from_state = new_con_state->crtc;
639 if (crtc_from_state == crtc)
640 return to_amdgpu_dm_connector(connector);
646 static int dm_resume(void *handle)
648 struct amdgpu_device *adev = handle;
649 struct drm_device *ddev = adev->ddev;
650 struct amdgpu_display_manager *dm = &adev->dm;
651 struct amdgpu_dm_connector *aconnector;
652 struct drm_connector *connector;
653 struct drm_crtc *crtc;
654 struct drm_crtc_state *new_crtc_state;
655 struct dm_crtc_state *dm_new_crtc_state;
656 struct drm_plane *plane;
657 struct drm_plane_state *new_plane_state;
658 struct dm_plane_state *dm_new_plane_state;
662 /* power on hardware */
663 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0);
665 /* program HPD filter */
668 /* On resume we need to rewrite the MSTM control bits to enamble MST*/
669 s3_handle_mst(ddev, false);
672 * early enable HPD Rx IRQ, should be done before set mode as short
673 * pulse interrupts are used for MST
675 amdgpu_dm_irq_resume_early(adev);
678 list_for_each_entry(connector, &ddev->mode_config.connector_list, head) {
679 aconnector = to_amdgpu_dm_connector(connector);
682 * this is the case when traversing through already created
683 * MST connectors, should be skipped
685 if (aconnector->mst_port)
688 mutex_lock(&aconnector->hpd_lock);
689 dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
691 if (aconnector->fake_enable && aconnector->dc_link->local_sink)
692 aconnector->fake_enable = false;
694 aconnector->dc_sink = NULL;
695 amdgpu_dm_update_connector_after_detect(aconnector);
696 mutex_unlock(&aconnector->hpd_lock);
699 /* Force mode set in atomic comit */
700 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i)
701 new_crtc_state->active_changed = true;
704 * atomic_check is expected to create the dc states. We need to release
705 * them here, since they were duplicated as part of the suspend
708 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) {
709 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
710 if (dm_new_crtc_state->stream) {
711 WARN_ON(kref_read(&dm_new_crtc_state->stream->refcount) > 1);
712 dc_stream_release(dm_new_crtc_state->stream);
713 dm_new_crtc_state->stream = NULL;
717 for_each_new_plane_in_state(dm->cached_state, plane, new_plane_state, i) {
718 dm_new_plane_state = to_dm_plane_state(new_plane_state);
719 if (dm_new_plane_state->dc_state) {
720 WARN_ON(kref_read(&dm_new_plane_state->dc_state->refcount) > 1);
721 dc_plane_state_release(dm_new_plane_state->dc_state);
722 dm_new_plane_state->dc_state = NULL;
726 ret = drm_atomic_helper_resume(ddev, dm->cached_state);
728 dm->cached_state = NULL;
730 amdgpu_dm_irq_resume_late(adev);
735 static const struct amd_ip_funcs amdgpu_dm_funcs = {
737 .early_init = dm_early_init,
738 .late_init = dm_late_init,
739 .sw_init = dm_sw_init,
740 .sw_fini = dm_sw_fini,
741 .hw_init = dm_hw_init,
742 .hw_fini = dm_hw_fini,
743 .suspend = dm_suspend,
745 .is_idle = dm_is_idle,
746 .wait_for_idle = dm_wait_for_idle,
747 .check_soft_reset = dm_check_soft_reset,
748 .soft_reset = dm_soft_reset,
749 .set_clockgating_state = dm_set_clockgating_state,
750 .set_powergating_state = dm_set_powergating_state,
753 const struct amdgpu_ip_block_version dm_ip_block =
755 .type = AMD_IP_BLOCK_TYPE_DCE,
759 .funcs = &amdgpu_dm_funcs,
763 static struct drm_atomic_state *
764 dm_atomic_state_alloc(struct drm_device *dev)
766 struct dm_atomic_state *state = kzalloc(sizeof(*state), GFP_KERNEL);
771 if (drm_atomic_state_init(dev, &state->base) < 0)
782 dm_atomic_state_clear(struct drm_atomic_state *state)
784 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
786 if (dm_state->context) {
787 dc_release_state(dm_state->context);
788 dm_state->context = NULL;
791 drm_atomic_state_default_clear(state);
795 dm_atomic_state_alloc_free(struct drm_atomic_state *state)
797 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
798 drm_atomic_state_default_release(state);
802 static const struct drm_mode_config_funcs amdgpu_dm_mode_funcs = {
803 .fb_create = amdgpu_display_user_framebuffer_create,
804 .output_poll_changed = drm_fb_helper_output_poll_changed,
805 .atomic_check = amdgpu_dm_atomic_check,
806 .atomic_commit = amdgpu_dm_atomic_commit,
807 .atomic_state_alloc = dm_atomic_state_alloc,
808 .atomic_state_clear = dm_atomic_state_clear,
809 .atomic_state_free = dm_atomic_state_alloc_free
812 static struct drm_mode_config_helper_funcs amdgpu_dm_mode_config_helperfuncs = {
813 .atomic_commit_tail = amdgpu_dm_atomic_commit_tail
817 amdgpu_dm_update_connector_after_detect(struct amdgpu_dm_connector *aconnector)
819 struct drm_connector *connector = &aconnector->base;
820 struct drm_device *dev = connector->dev;
821 struct dc_sink *sink;
823 /* MST handled by drm_mst framework */
824 if (aconnector->mst_mgr.mst_state == true)
828 sink = aconnector->dc_link->local_sink;
830 /* Edid mgmt connector gets first update only in mode_valid hook and then
831 * the connector sink is set to either fake or physical sink depends on link status.
832 * don't do it here if u are during boot
834 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED
835 && aconnector->dc_em_sink) {
837 /* For S3 resume with headless use eml_sink to fake stream
838 * because on resume connecotr->sink is set ti NULL
840 mutex_lock(&dev->mode_config.mutex);
843 if (aconnector->dc_sink) {
844 amdgpu_dm_remove_sink_from_freesync_module(
846 /* retain and release bellow are used for
847 * bump up refcount for sink because the link don't point
848 * to it anymore after disconnect so on next crtc to connector
849 * reshuffle by UMD we will get into unwanted dc_sink release
851 if (aconnector->dc_sink != aconnector->dc_em_sink)
852 dc_sink_release(aconnector->dc_sink);
854 aconnector->dc_sink = sink;
855 amdgpu_dm_add_sink_to_freesync_module(
856 connector, aconnector->edid);
858 amdgpu_dm_remove_sink_from_freesync_module(connector);
859 if (!aconnector->dc_sink)
860 aconnector->dc_sink = aconnector->dc_em_sink;
861 else if (aconnector->dc_sink != aconnector->dc_em_sink)
862 dc_sink_retain(aconnector->dc_sink);
865 mutex_unlock(&dev->mode_config.mutex);
870 * TODO: temporary guard to look for proper fix
871 * if this sink is MST sink, we should not do anything
873 if (sink && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
876 if (aconnector->dc_sink == sink) {
877 /* We got a DP short pulse (Link Loss, DP CTS, etc...).
879 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: dc_sink didn't change.\n",
880 aconnector->connector_id);
884 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: Old sink=%p New sink=%p\n",
885 aconnector->connector_id, aconnector->dc_sink, sink);
887 mutex_lock(&dev->mode_config.mutex);
889 /* 1. Update status of the drm connector
890 * 2. Send an event and let userspace tell us what to do */
892 /* TODO: check if we still need the S3 mode update workaround.
893 * If yes, put it here. */
894 if (aconnector->dc_sink)
895 amdgpu_dm_remove_sink_from_freesync_module(
898 aconnector->dc_sink = sink;
899 if (sink->dc_edid.length == 0) {
900 aconnector->edid = NULL;
903 (struct edid *) sink->dc_edid.raw_edid;
906 drm_connector_update_edid_property(connector,
909 amdgpu_dm_add_sink_to_freesync_module(connector, aconnector->edid);
912 amdgpu_dm_remove_sink_from_freesync_module(connector);
913 drm_connector_update_edid_property(connector, NULL);
914 aconnector->num_modes = 0;
915 aconnector->dc_sink = NULL;
916 aconnector->edid = NULL;
919 mutex_unlock(&dev->mode_config.mutex);
922 static void handle_hpd_irq(void *param)
924 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
925 struct drm_connector *connector = &aconnector->base;
926 struct drm_device *dev = connector->dev;
928 /* In case of failure or MST no need to update connector status or notify the OS
929 * since (for MST case) MST does this in it's own context.
931 mutex_lock(&aconnector->hpd_lock);
933 if (aconnector->fake_enable)
934 aconnector->fake_enable = false;
936 if (dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD)) {
937 amdgpu_dm_update_connector_after_detect(aconnector);
940 drm_modeset_lock_all(dev);
941 dm_restore_drm_connector_state(dev, connector);
942 drm_modeset_unlock_all(dev);
944 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
945 drm_kms_helper_hotplug_event(dev);
947 mutex_unlock(&aconnector->hpd_lock);
951 static void dm_handle_hpd_rx_irq(struct amdgpu_dm_connector *aconnector)
953 uint8_t esi[DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI] = { 0 };
955 bool new_irq_handled = false;
957 int dpcd_bytes_to_read;
959 const int max_process_count = 30;
960 int process_count = 0;
962 const struct dc_link_status *link_status = dc_link_get_status(aconnector->dc_link);
964 if (link_status->dpcd_caps->dpcd_rev.raw < 0x12) {
965 dpcd_bytes_to_read = DP_LANE0_1_STATUS - DP_SINK_COUNT;
966 /* DPCD 0x200 - 0x201 for downstream IRQ */
967 dpcd_addr = DP_SINK_COUNT;
969 dpcd_bytes_to_read = DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI;
970 /* DPCD 0x2002 - 0x2005 for downstream IRQ */
971 dpcd_addr = DP_SINK_COUNT_ESI;
974 dret = drm_dp_dpcd_read(
975 &aconnector->dm_dp_aux.aux,
980 while (dret == dpcd_bytes_to_read &&
981 process_count < max_process_count) {
987 DRM_DEBUG_DRIVER("ESI %02x %02x %02x\n", esi[0], esi[1], esi[2]);
988 /* handle HPD short pulse irq */
989 if (aconnector->mst_mgr.mst_state)
991 &aconnector->mst_mgr,
995 if (new_irq_handled) {
996 /* ACK at DPCD to notify down stream */
997 const int ack_dpcd_bytes_to_write =
998 dpcd_bytes_to_read - 1;
1000 for (retry = 0; retry < 3; retry++) {
1003 wret = drm_dp_dpcd_write(
1004 &aconnector->dm_dp_aux.aux,
1007 ack_dpcd_bytes_to_write);
1008 if (wret == ack_dpcd_bytes_to_write)
1012 /* check if there is new irq to be handle */
1013 dret = drm_dp_dpcd_read(
1014 &aconnector->dm_dp_aux.aux,
1017 dpcd_bytes_to_read);
1019 new_irq_handled = false;
1025 if (process_count == max_process_count)
1026 DRM_DEBUG_DRIVER("Loop exceeded max iterations\n");
1029 static void handle_hpd_rx_irq(void *param)
1031 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
1032 struct drm_connector *connector = &aconnector->base;
1033 struct drm_device *dev = connector->dev;
1034 struct dc_link *dc_link = aconnector->dc_link;
1035 bool is_mst_root_connector = aconnector->mst_mgr.mst_state;
1037 /* TODO:Temporary add mutex to protect hpd interrupt not have a gpio
1038 * conflict, after implement i2c helper, this mutex should be
1041 if (dc_link->type != dc_connection_mst_branch)
1042 mutex_lock(&aconnector->hpd_lock);
1044 if (dc_link_handle_hpd_rx_irq(dc_link, NULL, NULL) &&
1045 !is_mst_root_connector) {
1046 /* Downstream Port status changed. */
1047 if (dc_link_detect(dc_link, DETECT_REASON_HPDRX)) {
1049 if (aconnector->fake_enable)
1050 aconnector->fake_enable = false;
1052 amdgpu_dm_update_connector_after_detect(aconnector);
1055 drm_modeset_lock_all(dev);
1056 dm_restore_drm_connector_state(dev, connector);
1057 drm_modeset_unlock_all(dev);
1059 drm_kms_helper_hotplug_event(dev);
1062 if ((dc_link->cur_link_settings.lane_count != LANE_COUNT_UNKNOWN) ||
1063 (dc_link->type == dc_connection_mst_branch))
1064 dm_handle_hpd_rx_irq(aconnector);
1066 if (dc_link->type != dc_connection_mst_branch)
1067 mutex_unlock(&aconnector->hpd_lock);
1070 static void register_hpd_handlers(struct amdgpu_device *adev)
1072 struct drm_device *dev = adev->ddev;
1073 struct drm_connector *connector;
1074 struct amdgpu_dm_connector *aconnector;
1075 const struct dc_link *dc_link;
1076 struct dc_interrupt_params int_params = {0};
1078 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
1079 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
1081 list_for_each_entry(connector,
1082 &dev->mode_config.connector_list, head) {
1084 aconnector = to_amdgpu_dm_connector(connector);
1085 dc_link = aconnector->dc_link;
1087 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd) {
1088 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
1089 int_params.irq_source = dc_link->irq_source_hpd;
1091 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1093 (void *) aconnector);
1096 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd_rx) {
1098 /* Also register for DP short pulse (hpd_rx). */
1099 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
1100 int_params.irq_source = dc_link->irq_source_hpd_rx;
1102 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1104 (void *) aconnector);
1109 /* Register IRQ sources and initialize IRQ callbacks */
1110 static int dce110_register_irq_handlers(struct amdgpu_device *adev)
1112 struct dc *dc = adev->dm.dc;
1113 struct common_irq_params *c_irq_params;
1114 struct dc_interrupt_params int_params = {0};
1117 unsigned client_id = AMDGPU_IH_CLIENTID_LEGACY;
1119 if (adev->asic_type == CHIP_VEGA10 ||
1120 adev->asic_type == CHIP_VEGA12 ||
1121 adev->asic_type == CHIP_VEGA20 ||
1122 adev->asic_type == CHIP_RAVEN)
1123 client_id = SOC15_IH_CLIENTID_DCE;
1125 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
1126 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
1128 /* Actions of amdgpu_irq_add_id():
1129 * 1. Register a set() function with base driver.
1130 * Base driver will call set() function to enable/disable an
1131 * interrupt in DC hardware.
1132 * 2. Register amdgpu_dm_irq_handler().
1133 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
1134 * coming from DC hardware.
1135 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
1136 * for acknowledging and handling. */
1138 /* Use VBLANK interrupt */
1139 for (i = VISLANDS30_IV_SRCID_D1_VERTICAL_INTERRUPT0; i <= VISLANDS30_IV_SRCID_D6_VERTICAL_INTERRUPT0; i++) {
1140 r = amdgpu_irq_add_id(adev, client_id, i, &adev->crtc_irq);
1142 DRM_ERROR("Failed to add crtc irq id!\n");
1146 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1147 int_params.irq_source =
1148 dc_interrupt_to_irq_source(dc, i, 0);
1150 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
1152 c_irq_params->adev = adev;
1153 c_irq_params->irq_src = int_params.irq_source;
1155 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1156 dm_crtc_high_irq, c_irq_params);
1159 /* Use GRPH_PFLIP interrupt */
1160 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
1161 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
1162 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq);
1164 DRM_ERROR("Failed to add page flip irq id!\n");
1168 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1169 int_params.irq_source =
1170 dc_interrupt_to_irq_source(dc, i, 0);
1172 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
1174 c_irq_params->adev = adev;
1175 c_irq_params->irq_src = int_params.irq_source;
1177 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1178 dm_pflip_high_irq, c_irq_params);
1183 r = amdgpu_irq_add_id(adev, client_id,
1184 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
1186 DRM_ERROR("Failed to add hpd irq id!\n");
1190 register_hpd_handlers(adev);
1196 /* Register IRQ sources and initialize IRQ callbacks */
1197 static int dcn10_register_irq_handlers(struct amdgpu_device *adev)
1199 struct dc *dc = adev->dm.dc;
1200 struct common_irq_params *c_irq_params;
1201 struct dc_interrupt_params int_params = {0};
1205 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
1206 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
1208 /* Actions of amdgpu_irq_add_id():
1209 * 1. Register a set() function with base driver.
1210 * Base driver will call set() function to enable/disable an
1211 * interrupt in DC hardware.
1212 * 2. Register amdgpu_dm_irq_handler().
1213 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
1214 * coming from DC hardware.
1215 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
1216 * for acknowledging and handling.
1219 /* Use VSTARTUP interrupt */
1220 for (i = DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP;
1221 i <= DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP + adev->mode_info.num_crtc - 1;
1223 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->crtc_irq);
1226 DRM_ERROR("Failed to add crtc irq id!\n");
1230 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1231 int_params.irq_source =
1232 dc_interrupt_to_irq_source(dc, i, 0);
1234 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
1236 c_irq_params->adev = adev;
1237 c_irq_params->irq_src = int_params.irq_source;
1239 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1240 dm_crtc_high_irq, c_irq_params);
1243 /* Use GRPH_PFLIP interrupt */
1244 for (i = DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT;
1245 i <= DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT + adev->mode_info.num_crtc - 1;
1247 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->pageflip_irq);
1249 DRM_ERROR("Failed to add page flip irq id!\n");
1253 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
1254 int_params.irq_source =
1255 dc_interrupt_to_irq_source(dc, i, 0);
1257 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
1259 c_irq_params->adev = adev;
1260 c_irq_params->irq_src = int_params.irq_source;
1262 amdgpu_dm_irq_register_interrupt(adev, &int_params,
1263 dm_pflip_high_irq, c_irq_params);
1268 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DC_HPD1_INT,
1271 DRM_ERROR("Failed to add hpd irq id!\n");
1275 register_hpd_handlers(adev);
1281 static int amdgpu_dm_mode_config_init(struct amdgpu_device *adev)
1285 adev->mode_info.mode_config_initialized = true;
1287 adev->ddev->mode_config.funcs = (void *)&amdgpu_dm_mode_funcs;
1288 adev->ddev->mode_config.helper_private = &amdgpu_dm_mode_config_helperfuncs;
1290 adev->ddev->mode_config.max_width = 16384;
1291 adev->ddev->mode_config.max_height = 16384;
1293 adev->ddev->mode_config.preferred_depth = 24;
1294 adev->ddev->mode_config.prefer_shadow = 1;
1295 /* indicate support of immediate flip */
1296 adev->ddev->mode_config.async_page_flip = true;
1298 adev->ddev->mode_config.fb_base = adev->gmc.aper_base;
1300 r = amdgpu_display_modeset_create_props(adev);
1307 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
1308 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
1310 static int amdgpu_dm_backlight_update_status(struct backlight_device *bd)
1312 struct amdgpu_display_manager *dm = bl_get_data(bd);
1314 if (dc_link_set_backlight_level(dm->backlight_link,
1315 bd->props.brightness, 0, 0))
1321 static int amdgpu_dm_backlight_get_brightness(struct backlight_device *bd)
1323 struct amdgpu_display_manager *dm = bl_get_data(bd);
1324 int ret = dc_link_get_backlight_level(dm->backlight_link);
1326 if (ret == DC_ERROR_UNEXPECTED)
1327 return bd->props.brightness;
1331 static const struct backlight_ops amdgpu_dm_backlight_ops = {
1332 .get_brightness = amdgpu_dm_backlight_get_brightness,
1333 .update_status = amdgpu_dm_backlight_update_status,
1337 amdgpu_dm_register_backlight_device(struct amdgpu_display_manager *dm)
1340 struct backlight_properties props = { 0 };
1342 props.max_brightness = AMDGPU_MAX_BL_LEVEL;
1343 props.brightness = AMDGPU_MAX_BL_LEVEL;
1344 props.type = BACKLIGHT_RAW;
1346 snprintf(bl_name, sizeof(bl_name), "amdgpu_bl%d",
1347 dm->adev->ddev->primary->index);
1349 dm->backlight_dev = backlight_device_register(bl_name,
1350 dm->adev->ddev->dev,
1352 &amdgpu_dm_backlight_ops,
1355 if (IS_ERR(dm->backlight_dev))
1356 DRM_ERROR("DM: Backlight registration failed!\n");
1358 DRM_DEBUG_DRIVER("DM: Registered Backlight device: %s\n", bl_name);
1363 static int initialize_plane(struct amdgpu_display_manager *dm,
1364 struct amdgpu_mode_info *mode_info,
1367 struct amdgpu_plane *plane;
1368 unsigned long possible_crtcs;
1371 plane = kzalloc(sizeof(struct amdgpu_plane), GFP_KERNEL);
1372 mode_info->planes[plane_id] = plane;
1375 DRM_ERROR("KMS: Failed to allocate plane\n");
1378 plane->base.type = mode_info->plane_type[plane_id];
1381 * HACK: IGT tests expect that each plane can only have one
1382 * one possible CRTC. For now, set one CRTC for each
1383 * plane that is not an underlay, but still allow multiple
1384 * CRTCs for underlay planes.
1386 possible_crtcs = 1 << plane_id;
1387 if (plane_id >= dm->dc->caps.max_streams)
1388 possible_crtcs = 0xff;
1390 ret = amdgpu_dm_plane_init(dm, mode_info->planes[plane_id], possible_crtcs);
1393 DRM_ERROR("KMS: Failed to initialize plane\n");
1401 static void register_backlight_device(struct amdgpu_display_manager *dm,
1402 struct dc_link *link)
1404 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
1405 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
1407 if ((link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) &&
1408 link->type != dc_connection_none) {
1409 /* Event if registration failed, we should continue with
1410 * DM initialization because not having a backlight control
1411 * is better then a black screen.
1413 amdgpu_dm_register_backlight_device(dm);
1415 if (dm->backlight_dev)
1416 dm->backlight_link = link;
1422 /* In this architecture, the association
1423 * connector -> encoder -> crtc
1424 * id not really requried. The crtc and connector will hold the
1425 * display_index as an abstraction to use with DAL component
1427 * Returns 0 on success
1429 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev)
1431 struct amdgpu_display_manager *dm = &adev->dm;
1433 struct amdgpu_dm_connector *aconnector = NULL;
1434 struct amdgpu_encoder *aencoder = NULL;
1435 struct amdgpu_mode_info *mode_info = &adev->mode_info;
1437 int32_t total_overlay_planes, total_primary_planes;
1439 link_cnt = dm->dc->caps.max_links;
1440 if (amdgpu_dm_mode_config_init(dm->adev)) {
1441 DRM_ERROR("DM: Failed to initialize mode config\n");
1445 /* Identify the number of planes to be initialized */
1446 total_overlay_planes = dm->dc->caps.max_slave_planes;
1447 total_primary_planes = dm->dc->caps.max_planes - dm->dc->caps.max_slave_planes;
1449 /* First initialize overlay planes, index starting after primary planes */
1450 for (i = (total_overlay_planes - 1); i >= 0; i--) {
1451 if (initialize_plane(dm, mode_info, (total_primary_planes + i))) {
1452 DRM_ERROR("KMS: Failed to initialize overlay plane\n");
1457 /* Initialize primary planes */
1458 for (i = (total_primary_planes - 1); i >= 0; i--) {
1459 if (initialize_plane(dm, mode_info, i)) {
1460 DRM_ERROR("KMS: Failed to initialize primary plane\n");
1465 for (i = 0; i < dm->dc->caps.max_streams; i++)
1466 if (amdgpu_dm_crtc_init(dm, &mode_info->planes[i]->base, i)) {
1467 DRM_ERROR("KMS: Failed to initialize crtc\n");
1471 dm->display_indexes_num = dm->dc->caps.max_streams;
1473 /* loops over all connectors on the board */
1474 for (i = 0; i < link_cnt; i++) {
1475 struct dc_link *link = NULL;
1477 if (i > AMDGPU_DM_MAX_DISPLAY_INDEX) {
1479 "KMS: Cannot support more than %d display indexes\n",
1480 AMDGPU_DM_MAX_DISPLAY_INDEX);
1484 aconnector = kzalloc(sizeof(*aconnector), GFP_KERNEL);
1488 aencoder = kzalloc(sizeof(*aencoder), GFP_KERNEL);
1492 if (amdgpu_dm_encoder_init(dm->ddev, aencoder, i)) {
1493 DRM_ERROR("KMS: Failed to initialize encoder\n");
1497 if (amdgpu_dm_connector_init(dm, aconnector, i, aencoder)) {
1498 DRM_ERROR("KMS: Failed to initialize connector\n");
1502 link = dc_get_link_at_index(dm->dc, i);
1504 if (dc_link_detect(link, DETECT_REASON_BOOT)) {
1505 amdgpu_dm_update_connector_after_detect(aconnector);
1506 register_backlight_device(dm, link);
1512 /* Software is initialized. Now we can register interrupt handlers. */
1513 switch (adev->asic_type) {
1523 case CHIP_POLARIS11:
1524 case CHIP_POLARIS10:
1525 case CHIP_POLARIS12:
1530 if (dce110_register_irq_handlers(dm->adev)) {
1531 DRM_ERROR("DM: Failed to initialize IRQ\n");
1537 if (dcn10_register_irq_handlers(dm->adev)) {
1538 DRM_ERROR("DM: Failed to initialize IRQ\n");
1544 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type);
1548 if (adev->asic_type != CHIP_CARRIZO && adev->asic_type != CHIP_STONEY)
1549 dm->dc->debug.disable_stutter = amdgpu_pp_feature_mask & PP_STUTTER_MODE ? false : true;
1555 for (i = 0; i < dm->dc->caps.max_planes; i++)
1556 kfree(mode_info->planes[i]);
1560 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm)
1562 drm_mode_config_cleanup(dm->ddev);
1566 /******************************************************************************
1567 * amdgpu_display_funcs functions
1568 *****************************************************************************/
1571 * dm_bandwidth_update - program display watermarks
1573 * @adev: amdgpu_device pointer
1575 * Calculate and program the display watermarks and line buffer allocation.
1577 static void dm_bandwidth_update(struct amdgpu_device *adev)
1579 /* TODO: implement later */
1582 static int amdgpu_notify_freesync(struct drm_device *dev, void *data,
1583 struct drm_file *filp)
1585 struct mod_freesync_params freesync_params;
1586 uint8_t num_streams;
1589 struct amdgpu_device *adev = dev->dev_private;
1592 /* Get freesync enable flag from DRM */
1594 num_streams = dc_get_current_stream_count(adev->dm.dc);
1596 for (i = 0; i < num_streams; i++) {
1597 struct dc_stream_state *stream;
1598 stream = dc_get_stream_at_index(adev->dm.dc, i);
1600 mod_freesync_update_state(adev->dm.freesync_module,
1601 &stream, 1, &freesync_params);
1607 static const struct amdgpu_display_funcs dm_display_funcs = {
1608 .bandwidth_update = dm_bandwidth_update, /* called unconditionally */
1609 .vblank_get_counter = dm_vblank_get_counter,/* called unconditionally */
1610 .backlight_set_level = NULL, /* never called for DC */
1611 .backlight_get_level = NULL, /* never called for DC */
1612 .hpd_sense = NULL,/* called unconditionally */
1613 .hpd_set_polarity = NULL, /* called unconditionally */
1614 .hpd_get_gpio_reg = NULL, /* VBIOS parsing. DAL does it. */
1615 .page_flip_get_scanoutpos =
1616 dm_crtc_get_scanoutpos,/* called unconditionally */
1617 .add_encoder = NULL, /* VBIOS parsing. DAL does it. */
1618 .add_connector = NULL, /* VBIOS parsing. DAL does it. */
1619 .notify_freesync = amdgpu_notify_freesync,
1623 #if defined(CONFIG_DEBUG_KERNEL_DC)
1625 static ssize_t s3_debug_store(struct device *device,
1626 struct device_attribute *attr,
1632 struct pci_dev *pdev = to_pci_dev(device);
1633 struct drm_device *drm_dev = pci_get_drvdata(pdev);
1634 struct amdgpu_device *adev = drm_dev->dev_private;
1636 ret = kstrtoint(buf, 0, &s3_state);
1641 drm_kms_helper_hotplug_event(adev->ddev);
1646 return ret == 0 ? count : 0;
1649 DEVICE_ATTR_WO(s3_debug);
1653 static int dm_early_init(void *handle)
1655 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1657 switch (adev->asic_type) {
1660 adev->mode_info.num_crtc = 6;
1661 adev->mode_info.num_hpd = 6;
1662 adev->mode_info.num_dig = 6;
1663 adev->mode_info.plane_type = dm_plane_type_default;
1666 adev->mode_info.num_crtc = 4;
1667 adev->mode_info.num_hpd = 6;
1668 adev->mode_info.num_dig = 7;
1669 adev->mode_info.plane_type = dm_plane_type_default;
1673 adev->mode_info.num_crtc = 2;
1674 adev->mode_info.num_hpd = 6;
1675 adev->mode_info.num_dig = 6;
1676 adev->mode_info.plane_type = dm_plane_type_default;
1680 adev->mode_info.num_crtc = 6;
1681 adev->mode_info.num_hpd = 6;
1682 adev->mode_info.num_dig = 7;
1683 adev->mode_info.plane_type = dm_plane_type_default;
1686 adev->mode_info.num_crtc = 3;
1687 adev->mode_info.num_hpd = 6;
1688 adev->mode_info.num_dig = 9;
1689 adev->mode_info.plane_type = dm_plane_type_carizzo;
1692 adev->mode_info.num_crtc = 2;
1693 adev->mode_info.num_hpd = 6;
1694 adev->mode_info.num_dig = 9;
1695 adev->mode_info.plane_type = dm_plane_type_stoney;
1697 case CHIP_POLARIS11:
1698 case CHIP_POLARIS12:
1699 adev->mode_info.num_crtc = 5;
1700 adev->mode_info.num_hpd = 5;
1701 adev->mode_info.num_dig = 5;
1702 adev->mode_info.plane_type = dm_plane_type_default;
1704 case CHIP_POLARIS10:
1706 adev->mode_info.num_crtc = 6;
1707 adev->mode_info.num_hpd = 6;
1708 adev->mode_info.num_dig = 6;
1709 adev->mode_info.plane_type = dm_plane_type_default;
1714 adev->mode_info.num_crtc = 6;
1715 adev->mode_info.num_hpd = 6;
1716 adev->mode_info.num_dig = 6;
1717 adev->mode_info.plane_type = dm_plane_type_default;
1721 adev->mode_info.num_crtc = 4;
1722 adev->mode_info.num_hpd = 4;
1723 adev->mode_info.num_dig = 4;
1724 adev->mode_info.plane_type = dm_plane_type_default;
1728 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type);
1732 amdgpu_dm_set_irq_funcs(adev);
1734 if (adev->mode_info.funcs == NULL)
1735 adev->mode_info.funcs = &dm_display_funcs;
1737 /* Note: Do NOT change adev->audio_endpt_rreg and
1738 * adev->audio_endpt_wreg because they are initialised in
1739 * amdgpu_device_init() */
1740 #if defined(CONFIG_DEBUG_KERNEL_DC)
1743 &dev_attr_s3_debug);
1749 static bool modeset_required(struct drm_crtc_state *crtc_state,
1750 struct dc_stream_state *new_stream,
1751 struct dc_stream_state *old_stream)
1753 if (!drm_atomic_crtc_needs_modeset(crtc_state))
1756 if (!crtc_state->enable)
1759 return crtc_state->active;
1762 static bool modereset_required(struct drm_crtc_state *crtc_state)
1764 if (!drm_atomic_crtc_needs_modeset(crtc_state))
1767 return !crtc_state->enable || !crtc_state->active;
1770 static void amdgpu_dm_encoder_destroy(struct drm_encoder *encoder)
1772 drm_encoder_cleanup(encoder);
1776 static const struct drm_encoder_funcs amdgpu_dm_encoder_funcs = {
1777 .destroy = amdgpu_dm_encoder_destroy,
1780 static bool fill_rects_from_plane_state(const struct drm_plane_state *state,
1781 struct dc_plane_state *plane_state)
1783 plane_state->src_rect.x = state->src_x >> 16;
1784 plane_state->src_rect.y = state->src_y >> 16;
1785 /*we ignore for now mantissa and do not to deal with floating pixels :(*/
1786 plane_state->src_rect.width = state->src_w >> 16;
1788 if (plane_state->src_rect.width == 0)
1791 plane_state->src_rect.height = state->src_h >> 16;
1792 if (plane_state->src_rect.height == 0)
1795 plane_state->dst_rect.x = state->crtc_x;
1796 plane_state->dst_rect.y = state->crtc_y;
1798 if (state->crtc_w == 0)
1801 plane_state->dst_rect.width = state->crtc_w;
1803 if (state->crtc_h == 0)
1806 plane_state->dst_rect.height = state->crtc_h;
1808 plane_state->clip_rect = plane_state->dst_rect;
1810 switch (state->rotation & DRM_MODE_ROTATE_MASK) {
1811 case DRM_MODE_ROTATE_0:
1812 plane_state->rotation = ROTATION_ANGLE_0;
1814 case DRM_MODE_ROTATE_90:
1815 plane_state->rotation = ROTATION_ANGLE_90;
1817 case DRM_MODE_ROTATE_180:
1818 plane_state->rotation = ROTATION_ANGLE_180;
1820 case DRM_MODE_ROTATE_270:
1821 plane_state->rotation = ROTATION_ANGLE_270;
1824 plane_state->rotation = ROTATION_ANGLE_0;
1830 static int get_fb_info(const struct amdgpu_framebuffer *amdgpu_fb,
1831 uint64_t *tiling_flags)
1833 struct amdgpu_bo *rbo = gem_to_amdgpu_bo(amdgpu_fb->base.obj[0]);
1834 int r = amdgpu_bo_reserve(rbo, false);
1837 // Don't show error msg. when return -ERESTARTSYS
1838 if (r != -ERESTARTSYS)
1839 DRM_ERROR("Unable to reserve buffer: %d\n", r);
1844 amdgpu_bo_get_tiling_flags(rbo, tiling_flags);
1846 amdgpu_bo_unreserve(rbo);
1851 static int fill_plane_attributes_from_fb(struct amdgpu_device *adev,
1852 struct dc_plane_state *plane_state,
1853 const struct amdgpu_framebuffer *amdgpu_fb)
1855 uint64_t tiling_flags;
1856 unsigned int awidth;
1857 const struct drm_framebuffer *fb = &amdgpu_fb->base;
1859 struct drm_format_name_buf format_name;
1868 switch (fb->format->format) {
1870 plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS;
1872 case DRM_FORMAT_RGB565:
1873 plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_RGB565;
1875 case DRM_FORMAT_XRGB8888:
1876 case DRM_FORMAT_ARGB8888:
1877 plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888;
1879 case DRM_FORMAT_XRGB2101010:
1880 case DRM_FORMAT_ARGB2101010:
1881 plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010;
1883 case DRM_FORMAT_XBGR2101010:
1884 case DRM_FORMAT_ABGR2101010:
1885 plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010;
1887 case DRM_FORMAT_NV21:
1888 plane_state->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr;
1890 case DRM_FORMAT_NV12:
1891 plane_state->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb;
1894 DRM_ERROR("Unsupported screen format %s\n",
1895 drm_get_format_name(fb->format->format, &format_name));
1899 if (plane_state->format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) {
1900 plane_state->address.type = PLN_ADDR_TYPE_GRAPHICS;
1901 plane_state->plane_size.grph.surface_size.x = 0;
1902 plane_state->plane_size.grph.surface_size.y = 0;
1903 plane_state->plane_size.grph.surface_size.width = fb->width;
1904 plane_state->plane_size.grph.surface_size.height = fb->height;
1905 plane_state->plane_size.grph.surface_pitch =
1906 fb->pitches[0] / fb->format->cpp[0];
1907 /* TODO: unhardcode */
1908 plane_state->color_space = COLOR_SPACE_SRGB;
1911 awidth = ALIGN(fb->width, 64);
1912 plane_state->address.type = PLN_ADDR_TYPE_VIDEO_PROGRESSIVE;
1913 plane_state->plane_size.video.luma_size.x = 0;
1914 plane_state->plane_size.video.luma_size.y = 0;
1915 plane_state->plane_size.video.luma_size.width = awidth;
1916 plane_state->plane_size.video.luma_size.height = fb->height;
1917 /* TODO: unhardcode */
1918 plane_state->plane_size.video.luma_pitch = awidth;
1920 plane_state->plane_size.video.chroma_size.x = 0;
1921 plane_state->plane_size.video.chroma_size.y = 0;
1922 plane_state->plane_size.video.chroma_size.width = awidth;
1923 plane_state->plane_size.video.chroma_size.height = fb->height;
1924 plane_state->plane_size.video.chroma_pitch = awidth / 2;
1926 /* TODO: unhardcode */
1927 plane_state->color_space = COLOR_SPACE_YCBCR709;
1930 memset(&plane_state->tiling_info, 0, sizeof(plane_state->tiling_info));
1932 /* Fill GFX8 params */
1933 if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == DC_ARRAY_2D_TILED_THIN1) {
1934 unsigned int bankw, bankh, mtaspect, tile_split, num_banks;
1936 bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
1937 bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
1938 mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
1939 tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT);
1940 num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
1942 /* XXX fix me for VI */
1943 plane_state->tiling_info.gfx8.num_banks = num_banks;
1944 plane_state->tiling_info.gfx8.array_mode =
1945 DC_ARRAY_2D_TILED_THIN1;
1946 plane_state->tiling_info.gfx8.tile_split = tile_split;
1947 plane_state->tiling_info.gfx8.bank_width = bankw;
1948 plane_state->tiling_info.gfx8.bank_height = bankh;
1949 plane_state->tiling_info.gfx8.tile_aspect = mtaspect;
1950 plane_state->tiling_info.gfx8.tile_mode =
1951 DC_ADDR_SURF_MICRO_TILING_DISPLAY;
1952 } else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE)
1953 == DC_ARRAY_1D_TILED_THIN1) {
1954 plane_state->tiling_info.gfx8.array_mode = DC_ARRAY_1D_TILED_THIN1;
1957 plane_state->tiling_info.gfx8.pipe_config =
1958 AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
1960 if (adev->asic_type == CHIP_VEGA10 ||
1961 adev->asic_type == CHIP_VEGA12 ||
1962 adev->asic_type == CHIP_VEGA20 ||
1963 adev->asic_type == CHIP_RAVEN) {
1964 /* Fill GFX9 params */
1965 plane_state->tiling_info.gfx9.num_pipes =
1966 adev->gfx.config.gb_addr_config_fields.num_pipes;
1967 plane_state->tiling_info.gfx9.num_banks =
1968 adev->gfx.config.gb_addr_config_fields.num_banks;
1969 plane_state->tiling_info.gfx9.pipe_interleave =
1970 adev->gfx.config.gb_addr_config_fields.pipe_interleave_size;
1971 plane_state->tiling_info.gfx9.num_shader_engines =
1972 adev->gfx.config.gb_addr_config_fields.num_se;
1973 plane_state->tiling_info.gfx9.max_compressed_frags =
1974 adev->gfx.config.gb_addr_config_fields.max_compress_frags;
1975 plane_state->tiling_info.gfx9.num_rb_per_se =
1976 adev->gfx.config.gb_addr_config_fields.num_rb_per_se;
1977 plane_state->tiling_info.gfx9.swizzle =
1978 AMDGPU_TILING_GET(tiling_flags, SWIZZLE_MODE);
1979 plane_state->tiling_info.gfx9.shaderEnable = 1;
1982 plane_state->visible = true;
1983 plane_state->scaling_quality.h_taps_c = 0;
1984 plane_state->scaling_quality.v_taps_c = 0;
1986 /* is this needed? is plane_state zeroed at allocation? */
1987 plane_state->scaling_quality.h_taps = 0;
1988 plane_state->scaling_quality.v_taps = 0;
1989 plane_state->stereo_format = PLANE_STEREO_FORMAT_NONE;
1995 static int fill_plane_attributes(struct amdgpu_device *adev,
1996 struct dc_plane_state *dc_plane_state,
1997 struct drm_plane_state *plane_state,
1998 struct drm_crtc_state *crtc_state)
2000 const struct amdgpu_framebuffer *amdgpu_fb =
2001 to_amdgpu_framebuffer(plane_state->fb);
2002 const struct drm_crtc *crtc = plane_state->crtc;
2005 if (!fill_rects_from_plane_state(plane_state, dc_plane_state))
2008 ret = fill_plane_attributes_from_fb(
2009 crtc->dev->dev_private,
2017 * Always set input transfer function, since plane state is refreshed
2020 ret = amdgpu_dm_set_degamma_lut(crtc_state, dc_plane_state);
2022 dc_transfer_func_release(dc_plane_state->in_transfer_func);
2023 dc_plane_state->in_transfer_func = NULL;
2029 /*****************************************************************************/
2031 static void update_stream_scaling_settings(const struct drm_display_mode *mode,
2032 const struct dm_connector_state *dm_state,
2033 struct dc_stream_state *stream)
2035 enum amdgpu_rmx_type rmx_type;
2037 struct rect src = { 0 }; /* viewport in composition space*/
2038 struct rect dst = { 0 }; /* stream addressable area */
2040 /* no mode. nothing to be done */
2044 /* Full screen scaling by default */
2045 src.width = mode->hdisplay;
2046 src.height = mode->vdisplay;
2047 dst.width = stream->timing.h_addressable;
2048 dst.height = stream->timing.v_addressable;
2051 rmx_type = dm_state->scaling;
2052 if (rmx_type == RMX_ASPECT || rmx_type == RMX_OFF) {
2053 if (src.width * dst.height <
2054 src.height * dst.width) {
2055 /* height needs less upscaling/more downscaling */
2056 dst.width = src.width *
2057 dst.height / src.height;
2059 /* width needs less upscaling/more downscaling */
2060 dst.height = src.height *
2061 dst.width / src.width;
2063 } else if (rmx_type == RMX_CENTER) {
2067 dst.x = (stream->timing.h_addressable - dst.width) / 2;
2068 dst.y = (stream->timing.v_addressable - dst.height) / 2;
2070 if (dm_state->underscan_enable) {
2071 dst.x += dm_state->underscan_hborder / 2;
2072 dst.y += dm_state->underscan_vborder / 2;
2073 dst.width -= dm_state->underscan_hborder;
2074 dst.height -= dm_state->underscan_vborder;
2081 DRM_DEBUG_DRIVER("Destination Rectangle x:%d y:%d width:%d height:%d\n",
2082 dst.x, dst.y, dst.width, dst.height);
2086 static enum dc_color_depth
2087 convert_color_depth_from_display_info(const struct drm_connector *connector)
2089 uint32_t bpc = connector->display_info.bpc;
2093 /* Temporary Work around, DRM don't parse color depth for
2094 * EDID revision before 1.4
2095 * TODO: Fix edid parsing
2097 return COLOR_DEPTH_888;
2099 return COLOR_DEPTH_666;
2101 return COLOR_DEPTH_888;
2103 return COLOR_DEPTH_101010;
2105 return COLOR_DEPTH_121212;
2107 return COLOR_DEPTH_141414;
2109 return COLOR_DEPTH_161616;
2111 return COLOR_DEPTH_UNDEFINED;
2115 static enum dc_aspect_ratio
2116 get_aspect_ratio(const struct drm_display_mode *mode_in)
2118 /* 1-1 mapping, since both enums follow the HDMI spec. */
2119 return (enum dc_aspect_ratio) mode_in->picture_aspect_ratio;
2122 static enum dc_color_space
2123 get_output_color_space(const struct dc_crtc_timing *dc_crtc_timing)
2125 enum dc_color_space color_space = COLOR_SPACE_SRGB;
2127 switch (dc_crtc_timing->pixel_encoding) {
2128 case PIXEL_ENCODING_YCBCR422:
2129 case PIXEL_ENCODING_YCBCR444:
2130 case PIXEL_ENCODING_YCBCR420:
2133 * 27030khz is the separation point between HDTV and SDTV
2134 * according to HDMI spec, we use YCbCr709 and YCbCr601
2137 if (dc_crtc_timing->pix_clk_khz > 27030) {
2138 if (dc_crtc_timing->flags.Y_ONLY)
2140 COLOR_SPACE_YCBCR709_LIMITED;
2142 color_space = COLOR_SPACE_YCBCR709;
2144 if (dc_crtc_timing->flags.Y_ONLY)
2146 COLOR_SPACE_YCBCR601_LIMITED;
2148 color_space = COLOR_SPACE_YCBCR601;
2153 case PIXEL_ENCODING_RGB:
2154 color_space = COLOR_SPACE_SRGB;
2165 static void reduce_mode_colour_depth(struct dc_crtc_timing *timing_out)
2167 if (timing_out->display_color_depth <= COLOR_DEPTH_888)
2170 timing_out->display_color_depth--;
2173 static void adjust_colour_depth_from_display_info(struct dc_crtc_timing *timing_out,
2174 const struct drm_display_info *info)
2177 if (timing_out->display_color_depth <= COLOR_DEPTH_888)
2180 normalized_clk = timing_out->pix_clk_khz;
2181 /* YCbCr 4:2:0 requires additional adjustment of 1/2 */
2182 if (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420)
2183 normalized_clk /= 2;
2184 /* Adjusting pix clock following on HDMI spec based on colour depth */
2185 switch (timing_out->display_color_depth) {
2186 case COLOR_DEPTH_101010:
2187 normalized_clk = (normalized_clk * 30) / 24;
2189 case COLOR_DEPTH_121212:
2190 normalized_clk = (normalized_clk * 36) / 24;
2192 case COLOR_DEPTH_161616:
2193 normalized_clk = (normalized_clk * 48) / 24;
2198 if (normalized_clk <= info->max_tmds_clock)
2200 reduce_mode_colour_depth(timing_out);
2202 } while (timing_out->display_color_depth > COLOR_DEPTH_888);
2205 /*****************************************************************************/
2208 fill_stream_properties_from_drm_display_mode(struct dc_stream_state *stream,
2209 const struct drm_display_mode *mode_in,
2210 const struct drm_connector *connector)
2212 struct dc_crtc_timing *timing_out = &stream->timing;
2213 const struct drm_display_info *info = &connector->display_info;
2215 memset(timing_out, 0, sizeof(struct dc_crtc_timing));
2217 timing_out->h_border_left = 0;
2218 timing_out->h_border_right = 0;
2219 timing_out->v_border_top = 0;
2220 timing_out->v_border_bottom = 0;
2221 /* TODO: un-hardcode */
2222 if (drm_mode_is_420_only(info, mode_in)
2223 && stream->sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A)
2224 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
2225 else if ((connector->display_info.color_formats & DRM_COLOR_FORMAT_YCRCB444)
2226 && stream->sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A)
2227 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR444;
2229 timing_out->pixel_encoding = PIXEL_ENCODING_RGB;
2231 timing_out->timing_3d_format = TIMING_3D_FORMAT_NONE;
2232 timing_out->display_color_depth = convert_color_depth_from_display_info(
2234 timing_out->scan_type = SCANNING_TYPE_NODATA;
2235 timing_out->hdmi_vic = 0;
2236 timing_out->vic = drm_match_cea_mode(mode_in);
2238 timing_out->h_addressable = mode_in->crtc_hdisplay;
2239 timing_out->h_total = mode_in->crtc_htotal;
2240 timing_out->h_sync_width =
2241 mode_in->crtc_hsync_end - mode_in->crtc_hsync_start;
2242 timing_out->h_front_porch =
2243 mode_in->crtc_hsync_start - mode_in->crtc_hdisplay;
2244 timing_out->v_total = mode_in->crtc_vtotal;
2245 timing_out->v_addressable = mode_in->crtc_vdisplay;
2246 timing_out->v_front_porch =
2247 mode_in->crtc_vsync_start - mode_in->crtc_vdisplay;
2248 timing_out->v_sync_width =
2249 mode_in->crtc_vsync_end - mode_in->crtc_vsync_start;
2250 timing_out->pix_clk_khz = mode_in->crtc_clock;
2251 timing_out->aspect_ratio = get_aspect_ratio(mode_in);
2252 if (mode_in->flags & DRM_MODE_FLAG_PHSYNC)
2253 timing_out->flags.HSYNC_POSITIVE_POLARITY = 1;
2254 if (mode_in->flags & DRM_MODE_FLAG_PVSYNC)
2255 timing_out->flags.VSYNC_POSITIVE_POLARITY = 1;
2257 stream->output_color_space = get_output_color_space(timing_out);
2259 stream->out_transfer_func->type = TF_TYPE_PREDEFINED;
2260 stream->out_transfer_func->tf = TRANSFER_FUNCTION_SRGB;
2261 if (stream->sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A)
2262 adjust_colour_depth_from_display_info(timing_out, info);
2265 static void fill_audio_info(struct audio_info *audio_info,
2266 const struct drm_connector *drm_connector,
2267 const struct dc_sink *dc_sink)
2270 int cea_revision = 0;
2271 const struct dc_edid_caps *edid_caps = &dc_sink->edid_caps;
2273 audio_info->manufacture_id = edid_caps->manufacturer_id;
2274 audio_info->product_id = edid_caps->product_id;
2276 cea_revision = drm_connector->display_info.cea_rev;
2278 strncpy(audio_info->display_name,
2279 edid_caps->display_name,
2280 AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS - 1);
2282 if (cea_revision >= 3) {
2283 audio_info->mode_count = edid_caps->audio_mode_count;
2285 for (i = 0; i < audio_info->mode_count; ++i) {
2286 audio_info->modes[i].format_code =
2287 (enum audio_format_code)
2288 (edid_caps->audio_modes[i].format_code);
2289 audio_info->modes[i].channel_count =
2290 edid_caps->audio_modes[i].channel_count;
2291 audio_info->modes[i].sample_rates.all =
2292 edid_caps->audio_modes[i].sample_rate;
2293 audio_info->modes[i].sample_size =
2294 edid_caps->audio_modes[i].sample_size;
2298 audio_info->flags.all = edid_caps->speaker_flags;
2300 /* TODO: We only check for the progressive mode, check for interlace mode too */
2301 if (drm_connector->latency_present[0]) {
2302 audio_info->video_latency = drm_connector->video_latency[0];
2303 audio_info->audio_latency = drm_connector->audio_latency[0];
2306 /* TODO: For DP, video and audio latency should be calculated from DPCD caps */
2311 copy_crtc_timing_for_drm_display_mode(const struct drm_display_mode *src_mode,
2312 struct drm_display_mode *dst_mode)
2314 dst_mode->crtc_hdisplay = src_mode->crtc_hdisplay;
2315 dst_mode->crtc_vdisplay = src_mode->crtc_vdisplay;
2316 dst_mode->crtc_clock = src_mode->crtc_clock;
2317 dst_mode->crtc_hblank_start = src_mode->crtc_hblank_start;
2318 dst_mode->crtc_hblank_end = src_mode->crtc_hblank_end;
2319 dst_mode->crtc_hsync_start = src_mode->crtc_hsync_start;
2320 dst_mode->crtc_hsync_end = src_mode->crtc_hsync_end;
2321 dst_mode->crtc_htotal = src_mode->crtc_htotal;
2322 dst_mode->crtc_hskew = src_mode->crtc_hskew;
2323 dst_mode->crtc_vblank_start = src_mode->crtc_vblank_start;
2324 dst_mode->crtc_vblank_end = src_mode->crtc_vblank_end;
2325 dst_mode->crtc_vsync_start = src_mode->crtc_vsync_start;
2326 dst_mode->crtc_vsync_end = src_mode->crtc_vsync_end;
2327 dst_mode->crtc_vtotal = src_mode->crtc_vtotal;
2331 decide_crtc_timing_for_drm_display_mode(struct drm_display_mode *drm_mode,
2332 const struct drm_display_mode *native_mode,
2335 if (scale_enabled) {
2336 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
2337 } else if (native_mode->clock == drm_mode->clock &&
2338 native_mode->htotal == drm_mode->htotal &&
2339 native_mode->vtotal == drm_mode->vtotal) {
2340 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
2342 /* no scaling nor amdgpu inserted, no need to patch */
2346 static struct dc_sink *
2347 create_fake_sink(struct amdgpu_dm_connector *aconnector)
2349 struct dc_sink_init_data sink_init_data = { 0 };
2350 struct dc_sink *sink = NULL;
2351 sink_init_data.link = aconnector->dc_link;
2352 sink_init_data.sink_signal = aconnector->dc_link->connector_signal;
2354 sink = dc_sink_create(&sink_init_data);
2356 DRM_ERROR("Failed to create sink!\n");
2359 sink->sink_signal = SIGNAL_TYPE_VIRTUAL;
2364 static void set_multisync_trigger_params(
2365 struct dc_stream_state *stream)
2367 if (stream->triggered_crtc_reset.enabled) {
2368 stream->triggered_crtc_reset.event = CRTC_EVENT_VSYNC_RISING;
2369 stream->triggered_crtc_reset.delay = TRIGGER_DELAY_NEXT_LINE;
2373 static void set_master_stream(struct dc_stream_state *stream_set[],
2376 int j, highest_rfr = 0, master_stream = 0;
2378 for (j = 0; j < stream_count; j++) {
2379 if (stream_set[j] && stream_set[j]->triggered_crtc_reset.enabled) {
2380 int refresh_rate = 0;
2382 refresh_rate = (stream_set[j]->timing.pix_clk_khz*1000)/
2383 (stream_set[j]->timing.h_total*stream_set[j]->timing.v_total);
2384 if (refresh_rate > highest_rfr) {
2385 highest_rfr = refresh_rate;
2390 for (j = 0; j < stream_count; j++) {
2392 stream_set[j]->triggered_crtc_reset.event_source = stream_set[master_stream];
2396 static void dm_enable_per_frame_crtc_master_sync(struct dc_state *context)
2400 if (context->stream_count < 2)
2402 for (i = 0; i < context->stream_count ; i++) {
2403 if (!context->streams[i])
2405 /* TODO: add a function to read AMD VSDB bits and will set
2406 * crtc_sync_master.multi_sync_enabled flag
2407 * For now its set to false
2409 set_multisync_trigger_params(context->streams[i]);
2411 set_master_stream(context->streams, context->stream_count);
2414 static struct dc_stream_state *
2415 create_stream_for_sink(struct amdgpu_dm_connector *aconnector,
2416 const struct drm_display_mode *drm_mode,
2417 const struct dm_connector_state *dm_state)
2419 struct drm_display_mode *preferred_mode = NULL;
2420 struct drm_connector *drm_connector;
2421 struct dc_stream_state *stream = NULL;
2422 struct drm_display_mode mode = *drm_mode;
2423 bool native_mode_found = false;
2424 struct dc_sink *sink = NULL;
2425 if (aconnector == NULL) {
2426 DRM_ERROR("aconnector is NULL!\n");
2430 drm_connector = &aconnector->base;
2432 if (!aconnector->dc_sink) {
2434 * Create dc_sink when necessary to MST
2435 * Don't apply fake_sink to MST
2437 if (aconnector->mst_port) {
2438 dm_dp_mst_dc_sink_create(drm_connector);
2442 sink = create_fake_sink(aconnector);
2446 sink = aconnector->dc_sink;
2449 stream = dc_create_stream_for_sink(sink);
2451 if (stream == NULL) {
2452 DRM_ERROR("Failed to create stream for sink!\n");
2456 list_for_each_entry(preferred_mode, &aconnector->base.modes, head) {
2457 /* Search for preferred mode */
2458 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED) {
2459 native_mode_found = true;
2463 if (!native_mode_found)
2464 preferred_mode = list_first_entry_or_null(
2465 &aconnector->base.modes,
2466 struct drm_display_mode,
2469 if (preferred_mode == NULL) {
2470 /* This may not be an error, the use case is when we we have no
2471 * usermode calls to reset and set mode upon hotplug. In this
2472 * case, we call set mode ourselves to restore the previous mode
2473 * and the modelist may not be filled in in time.
2475 DRM_DEBUG_DRIVER("No preferred mode found\n");
2477 decide_crtc_timing_for_drm_display_mode(
2478 &mode, preferred_mode,
2479 dm_state ? (dm_state->scaling != RMX_OFF) : false);
2483 drm_mode_set_crtcinfo(&mode, 0);
2485 fill_stream_properties_from_drm_display_mode(stream,
2486 &mode, &aconnector->base);
2487 update_stream_scaling_settings(&mode, dm_state, stream);
2490 &stream->audio_info,
2494 update_stream_signal(stream);
2496 if (dm_state && dm_state->freesync_capable)
2497 stream->ignore_msa_timing_param = true;
2499 if (sink && sink->sink_signal == SIGNAL_TYPE_VIRTUAL)
2500 dc_sink_release(sink);
2505 static void amdgpu_dm_crtc_destroy(struct drm_crtc *crtc)
2507 drm_crtc_cleanup(crtc);
2511 static void dm_crtc_destroy_state(struct drm_crtc *crtc,
2512 struct drm_crtc_state *state)
2514 struct dm_crtc_state *cur = to_dm_crtc_state(state);
2516 /* TODO Destroy dc_stream objects are stream object is flattened */
2518 dc_stream_release(cur->stream);
2521 __drm_atomic_helper_crtc_destroy_state(state);
2527 static void dm_crtc_reset_state(struct drm_crtc *crtc)
2529 struct dm_crtc_state *state;
2532 dm_crtc_destroy_state(crtc, crtc->state);
2534 state = kzalloc(sizeof(*state), GFP_KERNEL);
2535 if (WARN_ON(!state))
2538 crtc->state = &state->base;
2539 crtc->state->crtc = crtc;
2543 static struct drm_crtc_state *
2544 dm_crtc_duplicate_state(struct drm_crtc *crtc)
2546 struct dm_crtc_state *state, *cur;
2548 cur = to_dm_crtc_state(crtc->state);
2550 if (WARN_ON(!crtc->state))
2553 state = kzalloc(sizeof(*state), GFP_KERNEL);
2557 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
2560 state->stream = cur->stream;
2561 dc_stream_retain(state->stream);
2564 /* TODO Duplicate dc_stream after objects are stream object is flattened */
2566 return &state->base;
2570 static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
2572 enum dc_irq_source irq_source;
2573 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
2574 struct amdgpu_device *adev = crtc->dev->dev_private;
2576 irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
2577 return dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY;
2580 static int dm_enable_vblank(struct drm_crtc *crtc)
2582 return dm_set_vblank(crtc, true);
2585 static void dm_disable_vblank(struct drm_crtc *crtc)
2587 dm_set_vblank(crtc, false);
2590 /* Implemented only the options currently availible for the driver */
2591 static const struct drm_crtc_funcs amdgpu_dm_crtc_funcs = {
2592 .reset = dm_crtc_reset_state,
2593 .destroy = amdgpu_dm_crtc_destroy,
2594 .gamma_set = drm_atomic_helper_legacy_gamma_set,
2595 .set_config = drm_atomic_helper_set_config,
2596 .page_flip = drm_atomic_helper_page_flip,
2597 .atomic_duplicate_state = dm_crtc_duplicate_state,
2598 .atomic_destroy_state = dm_crtc_destroy_state,
2599 .set_crc_source = amdgpu_dm_crtc_set_crc_source,
2600 .enable_vblank = dm_enable_vblank,
2601 .disable_vblank = dm_disable_vblank,
2604 static enum drm_connector_status
2605 amdgpu_dm_connector_detect(struct drm_connector *connector, bool force)
2608 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
2611 * 1. This interface is NOT called in context of HPD irq.
2612 * 2. This interface *is called* in context of user-mode ioctl. Which
2613 * makes it a bad place for *any* MST-related activit. */
2615 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED &&
2616 !aconnector->fake_enable)
2617 connected = (aconnector->dc_sink != NULL);
2619 connected = (aconnector->base.force == DRM_FORCE_ON);
2621 return (connected ? connector_status_connected :
2622 connector_status_disconnected);
2625 int amdgpu_dm_connector_atomic_set_property(struct drm_connector *connector,
2626 struct drm_connector_state *connector_state,
2627 struct drm_property *property,
2630 struct drm_device *dev = connector->dev;
2631 struct amdgpu_device *adev = dev->dev_private;
2632 struct dm_connector_state *dm_old_state =
2633 to_dm_connector_state(connector->state);
2634 struct dm_connector_state *dm_new_state =
2635 to_dm_connector_state(connector_state);
2639 if (property == dev->mode_config.scaling_mode_property) {
2640 enum amdgpu_rmx_type rmx_type;
2643 case DRM_MODE_SCALE_CENTER:
2644 rmx_type = RMX_CENTER;
2646 case DRM_MODE_SCALE_ASPECT:
2647 rmx_type = RMX_ASPECT;
2649 case DRM_MODE_SCALE_FULLSCREEN:
2650 rmx_type = RMX_FULL;
2652 case DRM_MODE_SCALE_NONE:
2658 if (dm_old_state->scaling == rmx_type)
2661 dm_new_state->scaling = rmx_type;
2663 } else if (property == adev->mode_info.underscan_hborder_property) {
2664 dm_new_state->underscan_hborder = val;
2666 } else if (property == adev->mode_info.underscan_vborder_property) {
2667 dm_new_state->underscan_vborder = val;
2669 } else if (property == adev->mode_info.underscan_property) {
2670 dm_new_state->underscan_enable = val;
2677 int amdgpu_dm_connector_atomic_get_property(struct drm_connector *connector,
2678 const struct drm_connector_state *state,
2679 struct drm_property *property,
2682 struct drm_device *dev = connector->dev;
2683 struct amdgpu_device *adev = dev->dev_private;
2684 struct dm_connector_state *dm_state =
2685 to_dm_connector_state(state);
2688 if (property == dev->mode_config.scaling_mode_property) {
2689 switch (dm_state->scaling) {
2691 *val = DRM_MODE_SCALE_CENTER;
2694 *val = DRM_MODE_SCALE_ASPECT;
2697 *val = DRM_MODE_SCALE_FULLSCREEN;
2701 *val = DRM_MODE_SCALE_NONE;
2705 } else if (property == adev->mode_info.underscan_hborder_property) {
2706 *val = dm_state->underscan_hborder;
2708 } else if (property == adev->mode_info.underscan_vborder_property) {
2709 *val = dm_state->underscan_vborder;
2711 } else if (property == adev->mode_info.underscan_property) {
2712 *val = dm_state->underscan_enable;
2718 static void amdgpu_dm_connector_destroy(struct drm_connector *connector)
2720 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
2721 const struct dc_link *link = aconnector->dc_link;
2722 struct amdgpu_device *adev = connector->dev->dev_private;
2723 struct amdgpu_display_manager *dm = &adev->dm;
2725 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
2726 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
2728 if ((link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) &&
2729 link->type != dc_connection_none &&
2730 dm->backlight_dev) {
2731 backlight_device_unregister(dm->backlight_dev);
2732 dm->backlight_dev = NULL;
2735 drm_connector_unregister(connector);
2736 drm_connector_cleanup(connector);
2740 void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector)
2742 struct dm_connector_state *state =
2743 to_dm_connector_state(connector->state);
2745 if (connector->state)
2746 __drm_atomic_helper_connector_destroy_state(connector->state);
2750 state = kzalloc(sizeof(*state), GFP_KERNEL);
2753 state->scaling = RMX_OFF;
2754 state->underscan_enable = false;
2755 state->underscan_hborder = 0;
2756 state->underscan_vborder = 0;
2758 __drm_atomic_helper_connector_reset(connector, &state->base);
2762 struct drm_connector_state *
2763 amdgpu_dm_connector_atomic_duplicate_state(struct drm_connector *connector)
2765 struct dm_connector_state *state =
2766 to_dm_connector_state(connector->state);
2768 struct dm_connector_state *new_state =
2769 kmemdup(state, sizeof(*state), GFP_KERNEL);
2772 __drm_atomic_helper_connector_duplicate_state(connector,
2774 return &new_state->base;
2780 static const struct drm_connector_funcs amdgpu_dm_connector_funcs = {
2781 .reset = amdgpu_dm_connector_funcs_reset,
2782 .detect = amdgpu_dm_connector_detect,
2783 .fill_modes = drm_helper_probe_single_connector_modes,
2784 .destroy = amdgpu_dm_connector_destroy,
2785 .atomic_duplicate_state = amdgpu_dm_connector_atomic_duplicate_state,
2786 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
2787 .atomic_set_property = amdgpu_dm_connector_atomic_set_property,
2788 .atomic_get_property = amdgpu_dm_connector_atomic_get_property
2791 static struct drm_encoder *best_encoder(struct drm_connector *connector)
2793 int enc_id = connector->encoder_ids[0];
2794 struct drm_mode_object *obj;
2795 struct drm_encoder *encoder;
2797 DRM_DEBUG_DRIVER("Finding the best encoder\n");
2799 /* pick the encoder ids */
2801 obj = drm_mode_object_find(connector->dev, NULL, enc_id, DRM_MODE_OBJECT_ENCODER);
2803 DRM_ERROR("Couldn't find a matching encoder for our connector\n");
2806 encoder = obj_to_encoder(obj);
2809 DRM_ERROR("No encoder id\n");
2813 static int get_modes(struct drm_connector *connector)
2815 return amdgpu_dm_connector_get_modes(connector);
2818 static void create_eml_sink(struct amdgpu_dm_connector *aconnector)
2820 struct dc_sink_init_data init_params = {
2821 .link = aconnector->dc_link,
2822 .sink_signal = SIGNAL_TYPE_VIRTUAL
2826 if (!aconnector->base.edid_blob_ptr) {
2827 DRM_ERROR("No EDID firmware found on connector: %s ,forcing to OFF!\n",
2828 aconnector->base.name);
2830 aconnector->base.force = DRM_FORCE_OFF;
2831 aconnector->base.override_edid = false;
2835 edid = (struct edid *) aconnector->base.edid_blob_ptr->data;
2837 aconnector->edid = edid;
2839 aconnector->dc_em_sink = dc_link_add_remote_sink(
2840 aconnector->dc_link,
2842 (edid->extensions + 1) * EDID_LENGTH,
2845 if (aconnector->base.force == DRM_FORCE_ON)
2846 aconnector->dc_sink = aconnector->dc_link->local_sink ?
2847 aconnector->dc_link->local_sink :
2848 aconnector->dc_em_sink;
2851 static void handle_edid_mgmt(struct amdgpu_dm_connector *aconnector)
2853 struct dc_link *link = (struct dc_link *)aconnector->dc_link;
2855 /* In case of headless boot with force on for DP managed connector
2856 * Those settings have to be != 0 to get initial modeset
2858 if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT) {
2859 link->verified_link_cap.lane_count = LANE_COUNT_FOUR;
2860 link->verified_link_cap.link_rate = LINK_RATE_HIGH2;
2864 aconnector->base.override_edid = true;
2865 create_eml_sink(aconnector);
2868 enum drm_mode_status amdgpu_dm_connector_mode_valid(struct drm_connector *connector,
2869 struct drm_display_mode *mode)
2871 int result = MODE_ERROR;
2872 struct dc_sink *dc_sink;
2873 struct amdgpu_device *adev = connector->dev->dev_private;
2874 /* TODO: Unhardcode stream count */
2875 struct dc_stream_state *stream;
2876 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
2877 enum dc_status dc_result = DC_OK;
2879 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
2880 (mode->flags & DRM_MODE_FLAG_DBLSCAN))
2883 /* Only run this the first time mode_valid is called to initilialize
2886 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED &&
2887 !aconnector->dc_em_sink)
2888 handle_edid_mgmt(aconnector);
2890 dc_sink = to_amdgpu_dm_connector(connector)->dc_sink;
2892 if (dc_sink == NULL) {
2893 DRM_ERROR("dc_sink is NULL!\n");
2897 stream = create_stream_for_sink(aconnector, mode, NULL);
2898 if (stream == NULL) {
2899 DRM_ERROR("Failed to create stream for sink!\n");
2903 dc_result = dc_validate_stream(adev->dm.dc, stream);
2905 if (dc_result == DC_OK)
2908 DRM_DEBUG_KMS("Mode %dx%d (clk %d) failed DC validation with error %d\n",
2914 dc_stream_release(stream);
2917 /* TODO: error handling*/
2921 static const struct drm_connector_helper_funcs
2922 amdgpu_dm_connector_helper_funcs = {
2924 * If hotplug a second bigger display in FB Con mode, bigger resolution
2925 * modes will be filtered by drm_mode_validate_size(), and those modes
2926 * is missing after user start lightdm. So we need to renew modes list.
2927 * in get_modes call back, not just return the modes count
2929 .get_modes = get_modes,
2930 .mode_valid = amdgpu_dm_connector_mode_valid,
2931 .best_encoder = best_encoder
2934 static void dm_crtc_helper_disable(struct drm_crtc *crtc)
2938 static int dm_crtc_helper_atomic_check(struct drm_crtc *crtc,
2939 struct drm_crtc_state *state)
2941 struct amdgpu_device *adev = crtc->dev->dev_private;
2942 struct dc *dc = adev->dm.dc;
2943 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(state);
2946 if (unlikely(!dm_crtc_state->stream &&
2947 modeset_required(state, NULL, dm_crtc_state->stream))) {
2952 /* In some use cases, like reset, no stream is attached */
2953 if (!dm_crtc_state->stream)
2956 if (dc_validate_stream(dc, dm_crtc_state->stream) == DC_OK)
2962 static bool dm_crtc_helper_mode_fixup(struct drm_crtc *crtc,
2963 const struct drm_display_mode *mode,
2964 struct drm_display_mode *adjusted_mode)
2969 static const struct drm_crtc_helper_funcs amdgpu_dm_crtc_helper_funcs = {
2970 .disable = dm_crtc_helper_disable,
2971 .atomic_check = dm_crtc_helper_atomic_check,
2972 .mode_fixup = dm_crtc_helper_mode_fixup
2975 static void dm_encoder_helper_disable(struct drm_encoder *encoder)
2980 static int dm_encoder_helper_atomic_check(struct drm_encoder *encoder,
2981 struct drm_crtc_state *crtc_state,
2982 struct drm_connector_state *conn_state)
2987 const struct drm_encoder_helper_funcs amdgpu_dm_encoder_helper_funcs = {
2988 .disable = dm_encoder_helper_disable,
2989 .atomic_check = dm_encoder_helper_atomic_check
2992 static void dm_drm_plane_reset(struct drm_plane *plane)
2994 struct dm_plane_state *amdgpu_state = NULL;
2997 plane->funcs->atomic_destroy_state(plane, plane->state);
2999 amdgpu_state = kzalloc(sizeof(*amdgpu_state), GFP_KERNEL);
3000 WARN_ON(amdgpu_state == NULL);
3003 plane->state = &amdgpu_state->base;
3004 plane->state->plane = plane;
3005 plane->state->rotation = DRM_MODE_ROTATE_0;
3009 static struct drm_plane_state *
3010 dm_drm_plane_duplicate_state(struct drm_plane *plane)
3012 struct dm_plane_state *dm_plane_state, *old_dm_plane_state;
3014 old_dm_plane_state = to_dm_plane_state(plane->state);
3015 dm_plane_state = kzalloc(sizeof(*dm_plane_state), GFP_KERNEL);
3016 if (!dm_plane_state)
3019 __drm_atomic_helper_plane_duplicate_state(plane, &dm_plane_state->base);
3021 if (old_dm_plane_state->dc_state) {
3022 dm_plane_state->dc_state = old_dm_plane_state->dc_state;
3023 dc_plane_state_retain(dm_plane_state->dc_state);
3026 return &dm_plane_state->base;
3029 void dm_drm_plane_destroy_state(struct drm_plane *plane,
3030 struct drm_plane_state *state)
3032 struct dm_plane_state *dm_plane_state = to_dm_plane_state(state);
3034 if (dm_plane_state->dc_state)
3035 dc_plane_state_release(dm_plane_state->dc_state);
3037 drm_atomic_helper_plane_destroy_state(plane, state);
3040 static const struct drm_plane_funcs dm_plane_funcs = {
3041 .update_plane = drm_atomic_helper_update_plane,
3042 .disable_plane = drm_atomic_helper_disable_plane,
3043 .destroy = drm_plane_cleanup,
3044 .reset = dm_drm_plane_reset,
3045 .atomic_duplicate_state = dm_drm_plane_duplicate_state,
3046 .atomic_destroy_state = dm_drm_plane_destroy_state,
3049 static int dm_plane_helper_prepare_fb(struct drm_plane *plane,
3050 struct drm_plane_state *new_state)
3052 struct amdgpu_framebuffer *afb;
3053 struct drm_gem_object *obj;
3054 struct amdgpu_device *adev;
3055 struct amdgpu_bo *rbo;
3056 uint64_t chroma_addr = 0;
3057 struct dm_plane_state *dm_plane_state_new, *dm_plane_state_old;
3058 unsigned int awidth;
3062 dm_plane_state_old = to_dm_plane_state(plane->state);
3063 dm_plane_state_new = to_dm_plane_state(new_state);
3065 if (!new_state->fb) {
3066 DRM_DEBUG_DRIVER("No FB bound\n");
3070 afb = to_amdgpu_framebuffer(new_state->fb);
3071 obj = new_state->fb->obj[0];
3072 rbo = gem_to_amdgpu_bo(obj);
3073 adev = amdgpu_ttm_adev(rbo->tbo.bdev);
3074 r = amdgpu_bo_reserve(rbo, false);
3075 if (unlikely(r != 0))
3078 if (plane->type != DRM_PLANE_TYPE_CURSOR)
3079 domain = amdgpu_display_supported_domains(adev);
3081 domain = AMDGPU_GEM_DOMAIN_VRAM;
3083 r = amdgpu_bo_pin(rbo, domain);
3084 if (unlikely(r != 0)) {
3085 if (r != -ERESTARTSYS)
3086 DRM_ERROR("Failed to pin framebuffer with error %d\n", r);
3087 amdgpu_bo_unreserve(rbo);
3091 r = amdgpu_ttm_alloc_gart(&rbo->tbo);
3092 if (unlikely(r != 0)) {
3093 amdgpu_bo_unpin(rbo);
3094 amdgpu_bo_unreserve(rbo);
3095 DRM_ERROR("%p bind failed\n", rbo);
3098 amdgpu_bo_unreserve(rbo);
3100 afb->address = amdgpu_bo_gpu_offset(rbo);
3104 if (dm_plane_state_new->dc_state &&
3105 dm_plane_state_old->dc_state != dm_plane_state_new->dc_state) {
3106 struct dc_plane_state *plane_state = dm_plane_state_new->dc_state;
3108 if (plane_state->format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) {
3109 plane_state->address.grph.addr.low_part = lower_32_bits(afb->address);
3110 plane_state->address.grph.addr.high_part = upper_32_bits(afb->address);
3112 awidth = ALIGN(new_state->fb->width, 64);
3113 plane_state->address.type = PLN_ADDR_TYPE_VIDEO_PROGRESSIVE;
3114 plane_state->address.video_progressive.luma_addr.low_part
3115 = lower_32_bits(afb->address);
3116 plane_state->address.video_progressive.luma_addr.high_part
3117 = upper_32_bits(afb->address);
3118 chroma_addr = afb->address + (u64)awidth * new_state->fb->height;
3119 plane_state->address.video_progressive.chroma_addr.low_part
3120 = lower_32_bits(chroma_addr);
3121 plane_state->address.video_progressive.chroma_addr.high_part
3122 = upper_32_bits(chroma_addr);
3129 static void dm_plane_helper_cleanup_fb(struct drm_plane *plane,
3130 struct drm_plane_state *old_state)
3132 struct amdgpu_bo *rbo;
3138 rbo = gem_to_amdgpu_bo(old_state->fb->obj[0]);
3139 r = amdgpu_bo_reserve(rbo, false);
3141 DRM_ERROR("failed to reserve rbo before unpin\n");
3145 amdgpu_bo_unpin(rbo);
3146 amdgpu_bo_unreserve(rbo);
3147 amdgpu_bo_unref(&rbo);
3150 static int dm_plane_atomic_check(struct drm_plane *plane,
3151 struct drm_plane_state *state)
3153 struct amdgpu_device *adev = plane->dev->dev_private;
3154 struct dc *dc = adev->dm.dc;
3155 struct dm_plane_state *dm_plane_state = to_dm_plane_state(state);
3157 if (!dm_plane_state->dc_state)
3160 if (!fill_rects_from_plane_state(state, dm_plane_state->dc_state))
3163 if (dc_validate_plane(dc, dm_plane_state->dc_state) == DC_OK)
3169 static const struct drm_plane_helper_funcs dm_plane_helper_funcs = {
3170 .prepare_fb = dm_plane_helper_prepare_fb,
3171 .cleanup_fb = dm_plane_helper_cleanup_fb,
3172 .atomic_check = dm_plane_atomic_check,
3176 * TODO: these are currently initialized to rgb formats only.
3177 * For future use cases we should either initialize them dynamically based on
3178 * plane capabilities, or initialize this array to all formats, so internal drm
3179 * check will succeed, and let DC to implement proper check
3181 static const uint32_t rgb_formats[] = {
3183 DRM_FORMAT_XRGB8888,
3184 DRM_FORMAT_ARGB8888,
3185 DRM_FORMAT_RGBA8888,
3186 DRM_FORMAT_XRGB2101010,
3187 DRM_FORMAT_XBGR2101010,
3188 DRM_FORMAT_ARGB2101010,
3189 DRM_FORMAT_ABGR2101010,
3192 static const uint32_t yuv_formats[] = {
3197 static const u32 cursor_formats[] = {
3201 static int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
3202 struct amdgpu_plane *aplane,
3203 unsigned long possible_crtcs)
3207 switch (aplane->base.type) {
3208 case DRM_PLANE_TYPE_PRIMARY:
3209 res = drm_universal_plane_init(
3215 ARRAY_SIZE(rgb_formats),
3216 NULL, aplane->base.type, NULL);
3218 case DRM_PLANE_TYPE_OVERLAY:
3219 res = drm_universal_plane_init(
3225 ARRAY_SIZE(yuv_formats),
3226 NULL, aplane->base.type, NULL);
3228 case DRM_PLANE_TYPE_CURSOR:
3229 res = drm_universal_plane_init(
3235 ARRAY_SIZE(cursor_formats),
3236 NULL, aplane->base.type, NULL);
3240 drm_plane_helper_add(&aplane->base, &dm_plane_helper_funcs);
3242 /* Create (reset) the plane state */
3243 if (aplane->base.funcs->reset)
3244 aplane->base.funcs->reset(&aplane->base);
3250 static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
3251 struct drm_plane *plane,
3252 uint32_t crtc_index)
3254 struct amdgpu_crtc *acrtc = NULL;
3255 struct amdgpu_plane *cursor_plane;
3259 cursor_plane = kzalloc(sizeof(*cursor_plane), GFP_KERNEL);
3263 cursor_plane->base.type = DRM_PLANE_TYPE_CURSOR;
3264 res = amdgpu_dm_plane_init(dm, cursor_plane, 0);
3266 acrtc = kzalloc(sizeof(struct amdgpu_crtc), GFP_KERNEL);
3270 res = drm_crtc_init_with_planes(
3274 &cursor_plane->base,
3275 &amdgpu_dm_crtc_funcs, NULL);
3280 drm_crtc_helper_add(&acrtc->base, &amdgpu_dm_crtc_helper_funcs);
3282 /* Create (reset) the plane state */
3283 if (acrtc->base.funcs->reset)
3284 acrtc->base.funcs->reset(&acrtc->base);
3286 acrtc->max_cursor_width = dm->adev->dm.dc->caps.max_cursor_size;
3287 acrtc->max_cursor_height = dm->adev->dm.dc->caps.max_cursor_size;
3289 acrtc->crtc_id = crtc_index;
3290 acrtc->base.enabled = false;
3292 dm->adev->mode_info.crtcs[crtc_index] = acrtc;
3293 drm_crtc_enable_color_mgmt(&acrtc->base, MAX_COLOR_LUT_ENTRIES,
3294 true, MAX_COLOR_LUT_ENTRIES);
3295 drm_mode_crtc_set_gamma_size(&acrtc->base, MAX_COLOR_LEGACY_LUT_ENTRIES);
3301 kfree(cursor_plane);
3306 static int to_drm_connector_type(enum signal_type st)
3309 case SIGNAL_TYPE_HDMI_TYPE_A:
3310 return DRM_MODE_CONNECTOR_HDMIA;
3311 case SIGNAL_TYPE_EDP:
3312 return DRM_MODE_CONNECTOR_eDP;
3313 case SIGNAL_TYPE_RGB:
3314 return DRM_MODE_CONNECTOR_VGA;
3315 case SIGNAL_TYPE_DISPLAY_PORT:
3316 case SIGNAL_TYPE_DISPLAY_PORT_MST:
3317 return DRM_MODE_CONNECTOR_DisplayPort;
3318 case SIGNAL_TYPE_DVI_DUAL_LINK:
3319 case SIGNAL_TYPE_DVI_SINGLE_LINK:
3320 return DRM_MODE_CONNECTOR_DVID;
3321 case SIGNAL_TYPE_VIRTUAL:
3322 return DRM_MODE_CONNECTOR_VIRTUAL;
3325 return DRM_MODE_CONNECTOR_Unknown;
3329 static void amdgpu_dm_get_native_mode(struct drm_connector *connector)
3331 const struct drm_connector_helper_funcs *helper =
3332 connector->helper_private;
3333 struct drm_encoder *encoder;
3334 struct amdgpu_encoder *amdgpu_encoder;
3336 encoder = helper->best_encoder(connector);
3338 if (encoder == NULL)
3341 amdgpu_encoder = to_amdgpu_encoder(encoder);
3343 amdgpu_encoder->native_mode.clock = 0;
3345 if (!list_empty(&connector->probed_modes)) {
3346 struct drm_display_mode *preferred_mode = NULL;
3348 list_for_each_entry(preferred_mode,
3349 &connector->probed_modes,
3351 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED)
3352 amdgpu_encoder->native_mode = *preferred_mode;
3360 static struct drm_display_mode *
3361 amdgpu_dm_create_common_mode(struct drm_encoder *encoder,
3363 int hdisplay, int vdisplay)
3365 struct drm_device *dev = encoder->dev;
3366 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3367 struct drm_display_mode *mode = NULL;
3368 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
3370 mode = drm_mode_duplicate(dev, native_mode);
3375 mode->hdisplay = hdisplay;
3376 mode->vdisplay = vdisplay;
3377 mode->type &= ~DRM_MODE_TYPE_PREFERRED;
3378 strncpy(mode->name, name, DRM_DISPLAY_MODE_LEN);
3384 static void amdgpu_dm_connector_add_common_modes(struct drm_encoder *encoder,
3385 struct drm_connector *connector)
3387 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3388 struct drm_display_mode *mode = NULL;
3389 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
3390 struct amdgpu_dm_connector *amdgpu_dm_connector =
3391 to_amdgpu_dm_connector(connector);
3395 char name[DRM_DISPLAY_MODE_LEN];
3398 } common_modes[] = {
3399 { "640x480", 640, 480},
3400 { "800x600", 800, 600},
3401 { "1024x768", 1024, 768},
3402 { "1280x720", 1280, 720},
3403 { "1280x800", 1280, 800},
3404 {"1280x1024", 1280, 1024},
3405 { "1440x900", 1440, 900},
3406 {"1680x1050", 1680, 1050},
3407 {"1600x1200", 1600, 1200},
3408 {"1920x1080", 1920, 1080},
3409 {"1920x1200", 1920, 1200}
3412 n = ARRAY_SIZE(common_modes);
3414 for (i = 0; i < n; i++) {
3415 struct drm_display_mode *curmode = NULL;
3416 bool mode_existed = false;
3418 if (common_modes[i].w > native_mode->hdisplay ||
3419 common_modes[i].h > native_mode->vdisplay ||
3420 (common_modes[i].w == native_mode->hdisplay &&
3421 common_modes[i].h == native_mode->vdisplay))
3424 list_for_each_entry(curmode, &connector->probed_modes, head) {
3425 if (common_modes[i].w == curmode->hdisplay &&
3426 common_modes[i].h == curmode->vdisplay) {
3427 mode_existed = true;
3435 mode = amdgpu_dm_create_common_mode(encoder,
3436 common_modes[i].name, common_modes[i].w,
3438 drm_mode_probed_add(connector, mode);
3439 amdgpu_dm_connector->num_modes++;
3443 static void amdgpu_dm_connector_ddc_get_modes(struct drm_connector *connector,
3446 struct amdgpu_dm_connector *amdgpu_dm_connector =
3447 to_amdgpu_dm_connector(connector);
3450 /* empty probed_modes */
3451 INIT_LIST_HEAD(&connector->probed_modes);
3452 amdgpu_dm_connector->num_modes =
3453 drm_add_edid_modes(connector, edid);
3455 amdgpu_dm_get_native_mode(connector);
3457 amdgpu_dm_connector->num_modes = 0;
3461 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector)
3463 const struct drm_connector_helper_funcs *helper =
3464 connector->helper_private;
3465 struct amdgpu_dm_connector *amdgpu_dm_connector =
3466 to_amdgpu_dm_connector(connector);
3467 struct drm_encoder *encoder;
3468 struct edid *edid = amdgpu_dm_connector->edid;
3470 encoder = helper->best_encoder(connector);
3472 if (!edid || !drm_edid_is_valid(edid)) {
3473 drm_add_modes_noedid(connector, 640, 480);
3475 amdgpu_dm_connector_ddc_get_modes(connector, edid);
3476 amdgpu_dm_connector_add_common_modes(encoder, connector);
3478 amdgpu_dm_fbc_init(connector);
3480 return amdgpu_dm_connector->num_modes;
3483 void amdgpu_dm_connector_init_helper(struct amdgpu_display_manager *dm,
3484 struct amdgpu_dm_connector *aconnector,
3486 struct dc_link *link,
3489 struct amdgpu_device *adev = dm->ddev->dev_private;
3491 aconnector->connector_id = link_index;
3492 aconnector->dc_link = link;
3493 aconnector->base.interlace_allowed = false;
3494 aconnector->base.doublescan_allowed = false;
3495 aconnector->base.stereo_allowed = false;
3496 aconnector->base.dpms = DRM_MODE_DPMS_OFF;
3497 aconnector->hpd.hpd = AMDGPU_HPD_NONE; /* not used */
3498 mutex_init(&aconnector->hpd_lock);
3500 /* configure support HPD hot plug connector_>polled default value is 0
3501 * which means HPD hot plug not supported
3503 switch (connector_type) {
3504 case DRM_MODE_CONNECTOR_HDMIA:
3505 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
3506 aconnector->base.ycbcr_420_allowed =
3507 link->link_enc->features.ycbcr420_supported ? true : false;
3509 case DRM_MODE_CONNECTOR_DisplayPort:
3510 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
3511 aconnector->base.ycbcr_420_allowed =
3512 link->link_enc->features.ycbcr420_supported ? true : false;
3514 case DRM_MODE_CONNECTOR_DVID:
3515 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
3521 drm_object_attach_property(&aconnector->base.base,
3522 dm->ddev->mode_config.scaling_mode_property,
3523 DRM_MODE_SCALE_NONE);
3525 drm_object_attach_property(&aconnector->base.base,
3526 adev->mode_info.underscan_property,
3528 drm_object_attach_property(&aconnector->base.base,
3529 adev->mode_info.underscan_hborder_property,
3531 drm_object_attach_property(&aconnector->base.base,
3532 adev->mode_info.underscan_vborder_property,
3537 static int amdgpu_dm_i2c_xfer(struct i2c_adapter *i2c_adap,
3538 struct i2c_msg *msgs, int num)
3540 struct amdgpu_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
3541 struct ddc_service *ddc_service = i2c->ddc_service;
3542 struct i2c_command cmd;
3546 cmd.payloads = kcalloc(num, sizeof(struct i2c_payload), GFP_KERNEL);
3551 cmd.number_of_payloads = num;
3552 cmd.engine = I2C_COMMAND_ENGINE_DEFAULT;
3555 for (i = 0; i < num; i++) {
3556 cmd.payloads[i].write = !(msgs[i].flags & I2C_M_RD);
3557 cmd.payloads[i].address = msgs[i].addr;
3558 cmd.payloads[i].length = msgs[i].len;
3559 cmd.payloads[i].data = msgs[i].buf;
3562 if (dal_i2caux_submit_i2c_command(
3563 ddc_service->ctx->i2caux,
3564 ddc_service->ddc_pin,
3568 kfree(cmd.payloads);
3572 static u32 amdgpu_dm_i2c_func(struct i2c_adapter *adap)
3574 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
3577 static const struct i2c_algorithm amdgpu_dm_i2c_algo = {
3578 .master_xfer = amdgpu_dm_i2c_xfer,
3579 .functionality = amdgpu_dm_i2c_func,
3582 static struct amdgpu_i2c_adapter *
3583 create_i2c(struct ddc_service *ddc_service,
3587 struct amdgpu_device *adev = ddc_service->ctx->driver_context;
3588 struct amdgpu_i2c_adapter *i2c;
3590 i2c = kzalloc(sizeof(struct amdgpu_i2c_adapter), GFP_KERNEL);
3593 i2c->base.owner = THIS_MODULE;
3594 i2c->base.class = I2C_CLASS_DDC;
3595 i2c->base.dev.parent = &adev->pdev->dev;
3596 i2c->base.algo = &amdgpu_dm_i2c_algo;
3597 snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c hw bus %d", link_index);
3598 i2c_set_adapdata(&i2c->base, i2c);
3599 i2c->ddc_service = ddc_service;
3605 /* Note: this function assumes that dc_link_detect() was called for the
3606 * dc_link which will be represented by this aconnector.
3608 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
3609 struct amdgpu_dm_connector *aconnector,
3610 uint32_t link_index,
3611 struct amdgpu_encoder *aencoder)
3615 struct dc *dc = dm->dc;
3616 struct dc_link *link = dc_get_link_at_index(dc, link_index);
3617 struct amdgpu_i2c_adapter *i2c;
3619 link->priv = aconnector;
3621 DRM_DEBUG_DRIVER("%s()\n", __func__);
3623 i2c = create_i2c(link->ddc, link->link_index, &res);
3625 DRM_ERROR("Failed to create i2c adapter data\n");
3629 aconnector->i2c = i2c;
3630 res = i2c_add_adapter(&i2c->base);
3633 DRM_ERROR("Failed to register hw i2c %d\n", link->link_index);
3637 connector_type = to_drm_connector_type(link->connector_signal);
3639 res = drm_connector_init(
3642 &amdgpu_dm_connector_funcs,
3646 DRM_ERROR("connector_init failed\n");
3647 aconnector->connector_id = -1;
3651 drm_connector_helper_add(
3653 &amdgpu_dm_connector_helper_funcs);
3655 if (aconnector->base.funcs->reset)
3656 aconnector->base.funcs->reset(&aconnector->base);
3658 amdgpu_dm_connector_init_helper(
3665 drm_connector_attach_encoder(
3666 &aconnector->base, &aencoder->base);
3668 drm_connector_register(&aconnector->base);
3669 #if defined(CONFIG_DEBUG_FS)
3670 res = connector_debugfs_init(aconnector);
3672 DRM_ERROR("Failed to create debugfs for connector");
3677 if (connector_type == DRM_MODE_CONNECTOR_DisplayPort
3678 || connector_type == DRM_MODE_CONNECTOR_eDP)
3679 amdgpu_dm_initialize_dp_connector(dm, aconnector);
3684 aconnector->i2c = NULL;
3689 int amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device *adev)
3691 switch (adev->mode_info.num_crtc) {
3708 static int amdgpu_dm_encoder_init(struct drm_device *dev,
3709 struct amdgpu_encoder *aencoder,
3710 uint32_t link_index)
3712 struct amdgpu_device *adev = dev->dev_private;
3714 int res = drm_encoder_init(dev,
3716 &amdgpu_dm_encoder_funcs,
3717 DRM_MODE_ENCODER_TMDS,
3720 aencoder->base.possible_crtcs = amdgpu_dm_get_encoder_crtc_mask(adev);
3723 aencoder->encoder_id = link_index;
3725 aencoder->encoder_id = -1;
3727 drm_encoder_helper_add(&aencoder->base, &amdgpu_dm_encoder_helper_funcs);
3732 static void manage_dm_interrupts(struct amdgpu_device *adev,
3733 struct amdgpu_crtc *acrtc,
3737 * this is not correct translation but will work as soon as VBLANK
3738 * constant is the same as PFLIP
3741 amdgpu_display_crtc_idx_to_irq_type(
3746 drm_crtc_vblank_on(&acrtc->base);
3749 &adev->pageflip_irq,
3755 &adev->pageflip_irq,
3757 drm_crtc_vblank_off(&acrtc->base);
3762 is_scaling_state_different(const struct dm_connector_state *dm_state,
3763 const struct dm_connector_state *old_dm_state)
3765 if (dm_state->scaling != old_dm_state->scaling)
3767 if (!dm_state->underscan_enable && old_dm_state->underscan_enable) {
3768 if (old_dm_state->underscan_hborder != 0 && old_dm_state->underscan_vborder != 0)
3770 } else if (dm_state->underscan_enable && !old_dm_state->underscan_enable) {
3771 if (dm_state->underscan_hborder != 0 && dm_state->underscan_vborder != 0)
3773 } else if (dm_state->underscan_hborder != old_dm_state->underscan_hborder ||
3774 dm_state->underscan_vborder != old_dm_state->underscan_vborder)
3779 static void remove_stream(struct amdgpu_device *adev,
3780 struct amdgpu_crtc *acrtc,
3781 struct dc_stream_state *stream)
3783 /* this is the update mode case */
3784 if (adev->dm.freesync_module)
3785 mod_freesync_remove_stream(adev->dm.freesync_module, stream);
3787 acrtc->otg_inst = -1;
3788 acrtc->enabled = false;
3791 static int get_cursor_position(struct drm_plane *plane, struct drm_crtc *crtc,
3792 struct dc_cursor_position *position)
3794 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
3796 int xorigin = 0, yorigin = 0;
3798 if (!crtc || !plane->state->fb) {
3799 position->enable = false;
3805 if ((plane->state->crtc_w > amdgpu_crtc->max_cursor_width) ||
3806 (plane->state->crtc_h > amdgpu_crtc->max_cursor_height)) {
3807 DRM_ERROR("%s: bad cursor width or height %d x %d\n",
3809 plane->state->crtc_w,
3810 plane->state->crtc_h);
3814 x = plane->state->crtc_x;
3815 y = plane->state->crtc_y;
3816 /* avivo cursor are offset into the total surface */
3817 x += crtc->primary->state->src_x >> 16;
3818 y += crtc->primary->state->src_y >> 16;
3820 xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
3824 yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
3827 position->enable = true;
3830 position->x_hotspot = xorigin;
3831 position->y_hotspot = yorigin;
3836 static void handle_cursor_update(struct drm_plane *plane,
3837 struct drm_plane_state *old_plane_state)
3839 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(plane->state->fb);
3840 struct drm_crtc *crtc = afb ? plane->state->crtc : old_plane_state->crtc;
3841 struct dm_crtc_state *crtc_state = crtc ? to_dm_crtc_state(crtc->state) : NULL;
3842 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
3843 uint64_t address = afb ? afb->address : 0;
3844 struct dc_cursor_position position;
3845 struct dc_cursor_attributes attributes;
3848 if (!plane->state->fb && !old_plane_state->fb)
3851 DRM_DEBUG_DRIVER("%s: crtc_id=%d with size %d to %d\n",
3853 amdgpu_crtc->crtc_id,
3854 plane->state->crtc_w,
3855 plane->state->crtc_h);
3857 ret = get_cursor_position(plane, crtc, &position);
3861 if (!position.enable) {
3862 /* turn off cursor */
3863 if (crtc_state && crtc_state->stream)
3864 dc_stream_set_cursor_position(crtc_state->stream,
3869 amdgpu_crtc->cursor_width = plane->state->crtc_w;
3870 amdgpu_crtc->cursor_height = plane->state->crtc_h;
3872 attributes.address.high_part = upper_32_bits(address);
3873 attributes.address.low_part = lower_32_bits(address);
3874 attributes.width = plane->state->crtc_w;
3875 attributes.height = plane->state->crtc_h;
3876 attributes.color_format = CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA;
3877 attributes.rotation_angle = 0;
3878 attributes.attribute_flags.value = 0;
3880 attributes.pitch = attributes.width;
3882 if (crtc_state->stream) {
3883 if (!dc_stream_set_cursor_attributes(crtc_state->stream,
3885 DRM_ERROR("DC failed to set cursor attributes\n");
3887 if (!dc_stream_set_cursor_position(crtc_state->stream,
3889 DRM_ERROR("DC failed to set cursor position\n");
3893 static void prepare_flip_isr(struct amdgpu_crtc *acrtc)
3896 assert_spin_locked(&acrtc->base.dev->event_lock);
3897 WARN_ON(acrtc->event);
3899 acrtc->event = acrtc->base.state->event;
3901 /* Set the flip status */
3902 acrtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
3904 /* Mark this event as consumed */
3905 acrtc->base.state->event = NULL;
3907 DRM_DEBUG_DRIVER("crtc:%d, pflip_stat:AMDGPU_FLIP_SUBMITTED\n",
3914 * Waits on all BO's fences and for proper vblank count
3916 static void amdgpu_dm_do_flip(struct drm_crtc *crtc,
3917 struct drm_framebuffer *fb,
3919 struct dc_state *state)
3921 unsigned long flags;
3922 uint32_t target_vblank;
3924 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
3925 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(fb);
3926 struct amdgpu_bo *abo = gem_to_amdgpu_bo(fb->obj[0]);
3927 struct amdgpu_device *adev = crtc->dev->dev_private;
3928 bool async_flip = (crtc->state->pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC) != 0;
3929 struct dc_flip_addrs addr = { {0} };
3930 /* TODO eliminate or rename surface_update */
3931 struct dc_surface_update surface_updates[1] = { {0} };
3932 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
3935 /* Prepare wait for target vblank early - before the fence-waits */
3936 target_vblank = target - (uint32_t)drm_crtc_vblank_count(crtc) +
3937 amdgpu_get_vblank_counter_kms(crtc->dev, acrtc->crtc_id);
3939 /* TODO This might fail and hence better not used, wait
3940 * explicitly on fences instead
3941 * and in general should be called for
3942 * blocking commit to as per framework helpers
3944 r = amdgpu_bo_reserve(abo, true);
3945 if (unlikely(r != 0)) {
3946 DRM_ERROR("failed to reserve buffer before flip\n");
3950 /* Wait for all fences on this FB */
3951 WARN_ON(reservation_object_wait_timeout_rcu(abo->tbo.resv, true, false,
3952 MAX_SCHEDULE_TIMEOUT) < 0);
3954 amdgpu_bo_unreserve(abo);
3956 /* Wait until we're out of the vertical blank period before the one
3957 * targeted by the flip
3959 while ((acrtc->enabled &&
3960 (amdgpu_display_get_crtc_scanoutpos(adev->ddev, acrtc->crtc_id,
3961 0, &vpos, &hpos, NULL,
3962 NULL, &crtc->hwmode)
3963 & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
3964 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
3965 (int)(target_vblank -
3966 amdgpu_get_vblank_counter_kms(adev->ddev, acrtc->crtc_id)) > 0)) {
3967 usleep_range(1000, 1100);
3971 spin_lock_irqsave(&crtc->dev->event_lock, flags);
3973 WARN_ON(acrtc->pflip_status != AMDGPU_FLIP_NONE);
3974 WARN_ON(!acrtc_state->stream);
3976 addr.address.grph.addr.low_part = lower_32_bits(afb->address);
3977 addr.address.grph.addr.high_part = upper_32_bits(afb->address);
3978 addr.flip_immediate = async_flip;
3981 if (acrtc->base.state->event)
3982 prepare_flip_isr(acrtc);
3984 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
3986 surface_updates->surface = dc_stream_get_status(acrtc_state->stream)->plane_states[0];
3987 surface_updates->flip_addr = &addr;
3989 dc_commit_updates_for_stream(adev->dm.dc,
3992 acrtc_state->stream,
3994 &surface_updates->surface,
3997 DRM_DEBUG_DRIVER("%s Flipping to hi: 0x%x, low: 0x%x \n",
3999 addr.address.grph.addr.high_part,
4000 addr.address.grph.addr.low_part);
4004 * TODO this whole function needs to go
4006 * dc_surface_update is needlessly complex. See if we can just replace this
4007 * with a dc_plane_state and follow the atomic model a bit more closely here.
4009 static bool commit_planes_to_stream(
4011 struct dc_plane_state **plane_states,
4012 uint8_t new_plane_count,
4013 struct dm_crtc_state *dm_new_crtc_state,
4014 struct dm_crtc_state *dm_old_crtc_state,
4015 struct dc_state *state)
4017 /* no need to dynamically allocate this. it's pretty small */
4018 struct dc_surface_update updates[MAX_SURFACES];
4019 struct dc_flip_addrs *flip_addr;
4020 struct dc_plane_info *plane_info;
4021 struct dc_scaling_info *scaling_info;
4023 struct dc_stream_state *dc_stream = dm_new_crtc_state->stream;
4024 struct dc_stream_update *stream_update =
4025 kzalloc(sizeof(struct dc_stream_update), GFP_KERNEL);
4027 if (!stream_update) {
4028 BREAK_TO_DEBUGGER();
4032 flip_addr = kcalloc(MAX_SURFACES, sizeof(struct dc_flip_addrs),
4034 plane_info = kcalloc(MAX_SURFACES, sizeof(struct dc_plane_info),
4036 scaling_info = kcalloc(MAX_SURFACES, sizeof(struct dc_scaling_info),
4039 if (!flip_addr || !plane_info || !scaling_info) {
4042 kfree(scaling_info);
4043 kfree(stream_update);
4047 memset(updates, 0, sizeof(updates));
4049 stream_update->src = dc_stream->src;
4050 stream_update->dst = dc_stream->dst;
4051 stream_update->out_transfer_func = dc_stream->out_transfer_func;
4053 for (i = 0; i < new_plane_count; i++) {
4054 updates[i].surface = plane_states[i];
4056 (struct dc_gamma *)plane_states[i]->gamma_correction;
4057 updates[i].in_transfer_func = plane_states[i]->in_transfer_func;
4058 flip_addr[i].address = plane_states[i]->address;
4059 flip_addr[i].flip_immediate = plane_states[i]->flip_immediate;
4060 plane_info[i].color_space = plane_states[i]->color_space;
4061 plane_info[i].format = plane_states[i]->format;
4062 plane_info[i].plane_size = plane_states[i]->plane_size;
4063 plane_info[i].rotation = plane_states[i]->rotation;
4064 plane_info[i].horizontal_mirror = plane_states[i]->horizontal_mirror;
4065 plane_info[i].stereo_format = plane_states[i]->stereo_format;
4066 plane_info[i].tiling_info = plane_states[i]->tiling_info;
4067 plane_info[i].visible = plane_states[i]->visible;
4068 plane_info[i].per_pixel_alpha = plane_states[i]->per_pixel_alpha;
4069 plane_info[i].dcc = plane_states[i]->dcc;
4070 scaling_info[i].scaling_quality = plane_states[i]->scaling_quality;
4071 scaling_info[i].src_rect = plane_states[i]->src_rect;
4072 scaling_info[i].dst_rect = plane_states[i]->dst_rect;
4073 scaling_info[i].clip_rect = plane_states[i]->clip_rect;
4075 updates[i].flip_addr = &flip_addr[i];
4076 updates[i].plane_info = &plane_info[i];
4077 updates[i].scaling_info = &scaling_info[i];
4080 dc_commit_updates_for_stream(
4084 dc_stream, stream_update, plane_states, state);
4088 kfree(scaling_info);
4089 kfree(stream_update);
4093 static void amdgpu_dm_commit_planes(struct drm_atomic_state *state,
4094 struct drm_device *dev,
4095 struct amdgpu_display_manager *dm,
4096 struct drm_crtc *pcrtc,
4097 bool *wait_for_vblank)
4100 struct drm_plane *plane;
4101 struct drm_plane_state *old_plane_state, *new_plane_state;
4102 struct dc_stream_state *dc_stream_attach;
4103 struct dc_plane_state *plane_states_constructed[MAX_SURFACES];
4104 struct amdgpu_crtc *acrtc_attach = to_amdgpu_crtc(pcrtc);
4105 struct drm_crtc_state *new_pcrtc_state =
4106 drm_atomic_get_new_crtc_state(state, pcrtc);
4107 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(new_pcrtc_state);
4108 struct dm_crtc_state *dm_old_crtc_state =
4109 to_dm_crtc_state(drm_atomic_get_old_crtc_state(state, pcrtc));
4110 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
4111 int planes_count = 0;
4112 unsigned long flags;
4114 /* update planes when needed */
4115 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
4116 struct drm_crtc *crtc = new_plane_state->crtc;
4117 struct drm_crtc_state *new_crtc_state;
4118 struct drm_framebuffer *fb = new_plane_state->fb;
4120 struct dm_plane_state *dm_new_plane_state = to_dm_plane_state(new_plane_state);
4122 if (plane->type == DRM_PLANE_TYPE_CURSOR) {
4123 handle_cursor_update(plane, old_plane_state);
4127 if (!fb || !crtc || pcrtc != crtc)
4130 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
4131 if (!new_crtc_state->active)
4134 pflip_needed = !state->allow_modeset;
4136 spin_lock_irqsave(&crtc->dev->event_lock, flags);
4137 if (acrtc_attach->pflip_status != AMDGPU_FLIP_NONE) {
4138 DRM_ERROR("%s: acrtc %d, already busy\n",
4140 acrtc_attach->crtc_id);
4141 /* In commit tail framework this cannot happen */
4144 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
4146 if (!pflip_needed || plane->type == DRM_PLANE_TYPE_OVERLAY) {
4147 WARN_ON(!dm_new_plane_state->dc_state);
4149 plane_states_constructed[planes_count] = dm_new_plane_state->dc_state;
4151 dc_stream_attach = acrtc_state->stream;
4154 } else if (new_crtc_state->planes_changed) {
4155 /* Assume even ONE crtc with immediate flip means
4156 * entire can't wait for VBLANK
4157 * TODO Check if it's correct
4160 new_pcrtc_state->pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC ?
4163 /* TODO: Needs rework for multiplane flip */
4164 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
4165 drm_crtc_vblank_get(crtc);
4170 (uint32_t)drm_crtc_vblank_count(crtc) + *wait_for_vblank,
4177 unsigned long flags;
4179 if (new_pcrtc_state->event) {
4181 drm_crtc_vblank_get(pcrtc);
4183 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
4184 prepare_flip_isr(acrtc_attach);
4185 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
4189 if (false == commit_planes_to_stream(dm->dc,
4190 plane_states_constructed,
4195 dm_error("%s: Failed to attach plane!\n", __func__);
4197 /*TODO BUG Here should go disable planes on CRTC. */
4202 * amdgpu_dm_crtc_copy_transient_flags - copy mirrored flags from DRM to DC
4203 * @crtc_state: the DRM CRTC state
4204 * @stream_state: the DC stream state.
4206 * Copy the mirrored transient state flags from DRM, to DC. It is used to bring
4207 * a dc_stream_state's flags in sync with a drm_crtc_state's flags.
4209 static void amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state *crtc_state,
4210 struct dc_stream_state *stream_state)
4212 stream_state->mode_changed = crtc_state->mode_changed;
4215 static int amdgpu_dm_atomic_commit(struct drm_device *dev,
4216 struct drm_atomic_state *state,
4219 struct drm_crtc *crtc;
4220 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
4221 struct amdgpu_device *adev = dev->dev_private;
4225 * We evade vblanks and pflips on crtc that
4226 * should be changed. We do it here to flush & disable
4227 * interrupts before drm_swap_state is called in drm_atomic_helper_commit
4228 * it will update crtc->dm_crtc_state->stream pointer which is used in
4231 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
4232 struct dm_crtc_state *dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
4233 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4235 if (drm_atomic_crtc_needs_modeset(new_crtc_state) && dm_old_crtc_state->stream)
4236 manage_dm_interrupts(adev, acrtc, false);
4238 /* Add check here for SoC's that support hardware cursor plane, to
4239 * unset legacy_cursor_update */
4241 return drm_atomic_helper_commit(dev, state, nonblock);
4243 /*TODO Handle EINTR, reenable IRQ*/
4246 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state)
4248 struct drm_device *dev = state->dev;
4249 struct amdgpu_device *adev = dev->dev_private;
4250 struct amdgpu_display_manager *dm = &adev->dm;
4251 struct dm_atomic_state *dm_state;
4253 struct drm_crtc *crtc;
4254 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
4255 unsigned long flags;
4256 bool wait_for_vblank = true;
4257 struct drm_connector *connector;
4258 struct drm_connector_state *old_con_state, *new_con_state;
4259 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
4260 int crtc_disable_count = 0;
4262 drm_atomic_helper_update_legacy_modeset_state(dev, state);
4264 dm_state = to_dm_atomic_state(state);
4266 /* update changed items */
4267 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
4268 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4270 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4271 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
4274 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
4275 "planes_changed:%d, mode_changed:%d,active_changed:%d,"
4276 "connectors_changed:%d\n",
4278 new_crtc_state->enable,
4279 new_crtc_state->active,
4280 new_crtc_state->planes_changed,
4281 new_crtc_state->mode_changed,
4282 new_crtc_state->active_changed,
4283 new_crtc_state->connectors_changed);
4285 /* Copy all transient state flags into dc state */
4286 if (dm_new_crtc_state->stream) {
4287 amdgpu_dm_crtc_copy_transient_flags(&dm_new_crtc_state->base,
4288 dm_new_crtc_state->stream);
4291 /* handles headless hotplug case, updating new_state and
4292 * aconnector as needed
4295 if (modeset_required(new_crtc_state, dm_new_crtc_state->stream, dm_old_crtc_state->stream)) {
4297 DRM_DEBUG_DRIVER("Atomic commit: SET crtc id %d: [%p]\n", acrtc->crtc_id, acrtc);
4299 if (!dm_new_crtc_state->stream) {
4301 * this could happen because of issues with
4302 * userspace notifications delivery.
4303 * In this case userspace tries to set mode on
4304 * display which is disconnect in fact.
4305 * dc_sink in NULL in this case on aconnector.
4306 * We expect reset mode will come soon.
4308 * This can also happen when unplug is done
4309 * during resume sequence ended
4311 * In this case, we want to pretend we still
4312 * have a sink to keep the pipe running so that
4313 * hw state is consistent with the sw state
4315 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
4316 __func__, acrtc->base.base.id);
4320 if (dm_old_crtc_state->stream)
4321 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
4323 pm_runtime_get_noresume(dev->dev);
4325 acrtc->enabled = true;
4326 acrtc->hw_mode = new_crtc_state->mode;
4327 crtc->hwmode = new_crtc_state->mode;
4328 } else if (modereset_required(new_crtc_state)) {
4329 DRM_DEBUG_DRIVER("Atomic commit: RESET. crtc id %d:[%p]\n", acrtc->crtc_id, acrtc);
4331 /* i.e. reset mode */
4332 if (dm_old_crtc_state->stream)
4333 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
4335 } /* for_each_crtc_in_state() */
4338 * Add streams after required streams from new and replaced streams
4339 * are removed from freesync module
4341 if (adev->dm.freesync_module) {
4342 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
4343 new_crtc_state, i) {
4344 struct amdgpu_dm_connector *aconnector = NULL;
4345 struct dm_connector_state *dm_new_con_state = NULL;
4346 struct amdgpu_crtc *acrtc = NULL;
4347 bool modeset_needed;
4349 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4350 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
4351 modeset_needed = modeset_required(
4353 dm_new_crtc_state->stream,
4354 dm_old_crtc_state->stream);
4355 /* We add stream to freesync if:
4356 * 1. Said stream is not null, and
4357 * 2. A modeset is requested. This means that the
4358 * stream was removed previously, and needs to be
4361 if (dm_new_crtc_state->stream == NULL ||
4365 acrtc = to_amdgpu_crtc(crtc);
4368 amdgpu_dm_find_first_crtc_matching_connector(
4371 DRM_DEBUG_DRIVER("Atomic commit: Failed to "
4372 "find connector for acrtc "
4373 "id:%d skipping freesync "
4379 mod_freesync_add_stream(adev->dm.freesync_module,
4380 dm_new_crtc_state->stream,
4382 new_con_state = drm_atomic_get_new_connector_state(
4383 state, &aconnector->base);
4384 dm_new_con_state = to_dm_connector_state(new_con_state);
4386 mod_freesync_set_user_enable(adev->dm.freesync_module,
4387 &dm_new_crtc_state->stream,
4389 &dm_new_con_state->user_enable);
4393 if (dm_state->context) {
4394 dm_enable_per_frame_crtc_master_sync(dm_state->context);
4395 WARN_ON(!dc_commit_state(dm->dc, dm_state->context));
4398 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
4399 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4401 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4403 if (dm_new_crtc_state->stream != NULL) {
4404 const struct dc_stream_status *status =
4405 dc_stream_get_status(dm_new_crtc_state->stream);
4408 DC_ERR("got no status for stream %p on acrtc%p\n", dm_new_crtc_state->stream, acrtc);
4410 acrtc->otg_inst = status->primary_otg_inst;
4414 /* Handle scaling and underscan changes*/
4415 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
4416 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
4417 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
4418 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
4419 struct dc_stream_status *status = NULL;
4422 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
4423 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
4426 /* Skip any modesets/resets */
4427 if (!acrtc || drm_atomic_crtc_needs_modeset(new_crtc_state))
4430 /* Skip any thing not scale or underscan changes */
4431 if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state))
4434 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4436 update_stream_scaling_settings(&dm_new_con_state->base.crtc->mode,
4437 dm_new_con_state, (struct dc_stream_state *)dm_new_crtc_state->stream);
4439 if (!dm_new_crtc_state->stream)
4442 status = dc_stream_get_status(dm_new_crtc_state->stream);
4444 WARN_ON(!status->plane_count);
4446 /*TODO How it works with MPO ?*/
4447 if (!commit_planes_to_stream(
4449 status->plane_states,
4450 status->plane_count,
4452 to_dm_crtc_state(old_crtc_state),
4454 dm_error("%s: Failed to update stream scaling!\n", __func__);
4457 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
4458 new_crtc_state, i) {
4460 * loop to enable interrupts on newly arrived crtc
4462 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4463 bool modeset_needed;
4465 if (old_crtc_state->active && !new_crtc_state->active)
4466 crtc_disable_count++;
4468 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4469 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
4470 modeset_needed = modeset_required(
4472 dm_new_crtc_state->stream,
4473 dm_old_crtc_state->stream);
4475 if (dm_new_crtc_state->stream == NULL || !modeset_needed)
4478 if (adev->dm.freesync_module)
4479 mod_freesync_notify_mode_change(
4480 adev->dm.freesync_module,
4481 &dm_new_crtc_state->stream, 1);
4483 manage_dm_interrupts(adev, acrtc, true);
4486 /* update planes when needed per crtc*/
4487 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j) {
4488 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4490 if (dm_new_crtc_state->stream)
4491 amdgpu_dm_commit_planes(state, dev, dm, crtc, &wait_for_vblank);
4496 * send vblank event on all events not handled in flip and
4497 * mark consumed event for drm_atomic_helper_commit_hw_done
4499 spin_lock_irqsave(&adev->ddev->event_lock, flags);
4500 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
4502 if (new_crtc_state->event)
4503 drm_send_event_locked(dev, &new_crtc_state->event->base);
4505 new_crtc_state->event = NULL;
4507 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
4509 /* Signal HW programming completion */
4510 drm_atomic_helper_commit_hw_done(state);
4512 if (wait_for_vblank)
4513 drm_atomic_helper_wait_for_flip_done(dev, state);
4515 drm_atomic_helper_cleanup_planes(dev, state);
4517 /* Finally, drop a runtime PM reference for each newly disabled CRTC,
4518 * so we can put the GPU into runtime suspend if we're not driving any
4521 for (i = 0; i < crtc_disable_count; i++)
4522 pm_runtime_put_autosuspend(dev->dev);
4523 pm_runtime_mark_last_busy(dev->dev);
4527 static int dm_force_atomic_commit(struct drm_connector *connector)
4530 struct drm_device *ddev = connector->dev;
4531 struct drm_atomic_state *state = drm_atomic_state_alloc(ddev);
4532 struct amdgpu_crtc *disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
4533 struct drm_plane *plane = disconnected_acrtc->base.primary;
4534 struct drm_connector_state *conn_state;
4535 struct drm_crtc_state *crtc_state;
4536 struct drm_plane_state *plane_state;
4541 state->acquire_ctx = ddev->mode_config.acquire_ctx;
4543 /* Construct an atomic state to restore previous display setting */
4546 * Attach connectors to drm_atomic_state
4548 conn_state = drm_atomic_get_connector_state(state, connector);
4550 ret = PTR_ERR_OR_ZERO(conn_state);
4554 /* Attach crtc to drm_atomic_state*/
4555 crtc_state = drm_atomic_get_crtc_state(state, &disconnected_acrtc->base);
4557 ret = PTR_ERR_OR_ZERO(crtc_state);
4561 /* force a restore */
4562 crtc_state->mode_changed = true;
4564 /* Attach plane to drm_atomic_state */
4565 plane_state = drm_atomic_get_plane_state(state, plane);
4567 ret = PTR_ERR_OR_ZERO(plane_state);
4572 /* Call commit internally with the state we just constructed */
4573 ret = drm_atomic_commit(state);
4578 DRM_ERROR("Restoring old state failed with %i\n", ret);
4579 drm_atomic_state_put(state);
4585 * This functions handle all cases when set mode does not come upon hotplug.
4586 * This include when the same display is unplugged then plugged back into the
4587 * same port and when we are running without usermode desktop manager supprot
4589 void dm_restore_drm_connector_state(struct drm_device *dev,
4590 struct drm_connector *connector)
4592 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
4593 struct amdgpu_crtc *disconnected_acrtc;
4594 struct dm_crtc_state *acrtc_state;
4596 if (!aconnector->dc_sink || !connector->state || !connector->encoder)
4599 disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
4600 if (!disconnected_acrtc)
4603 acrtc_state = to_dm_crtc_state(disconnected_acrtc->base.state);
4604 if (!acrtc_state->stream)
4608 * If the previous sink is not released and different from the current,
4609 * we deduce we are in a state where we can not rely on usermode call
4610 * to turn on the display, so we do it here
4612 if (acrtc_state->stream->sink != aconnector->dc_sink)
4613 dm_force_atomic_commit(&aconnector->base);
4617 * Grabs all modesetting locks to serialize against any blocking commits,
4618 * Waits for completion of all non blocking commits.
4620 static int do_aquire_global_lock(struct drm_device *dev,
4621 struct drm_atomic_state *state)
4623 struct drm_crtc *crtc;
4624 struct drm_crtc_commit *commit;
4627 /* Adding all modeset locks to aquire_ctx will
4628 * ensure that when the framework release it the
4629 * extra locks we are locking here will get released to
4631 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
4635 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
4636 spin_lock(&crtc->commit_lock);
4637 commit = list_first_entry_or_null(&crtc->commit_list,
4638 struct drm_crtc_commit, commit_entry);
4640 drm_crtc_commit_get(commit);
4641 spin_unlock(&crtc->commit_lock);
4646 /* Make sure all pending HW programming completed and
4649 ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ);
4652 ret = wait_for_completion_interruptible_timeout(
4653 &commit->flip_done, 10*HZ);
4656 DRM_ERROR("[CRTC:%d:%s] hw_done or flip_done "
4657 "timed out\n", crtc->base.id, crtc->name);
4659 drm_crtc_commit_put(commit);
4662 return ret < 0 ? ret : 0;
4665 static int dm_update_crtcs_state(struct dc *dc,
4666 struct drm_atomic_state *state,
4668 bool *lock_and_validation_needed)
4670 struct drm_crtc *crtc;
4671 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
4673 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
4674 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
4675 struct dc_stream_state *new_stream;
4678 /*TODO Move this code into dm_crtc_atomic_check once we get rid of dc_validation_set */
4679 /* update changed items */
4680 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
4681 struct amdgpu_crtc *acrtc = NULL;
4682 struct amdgpu_dm_connector *aconnector = NULL;
4683 struct drm_connector_state *drm_new_conn_state = NULL, *drm_old_conn_state = NULL;
4684 struct dm_connector_state *dm_new_conn_state = NULL, *dm_old_conn_state = NULL;
4685 struct drm_plane_state *new_plane_state = NULL;
4689 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
4690 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4691 acrtc = to_amdgpu_crtc(crtc);
4693 new_plane_state = drm_atomic_get_new_plane_state(state, new_crtc_state->crtc->primary);
4695 if (new_crtc_state->enable && new_plane_state && !new_plane_state->fb) {
4700 aconnector = amdgpu_dm_find_first_crtc_matching_connector(state, crtc);
4702 /* TODO This hack should go away */
4703 if (aconnector && enable) {
4704 // Make sure fake sink is created in plug-in scenario
4705 drm_new_conn_state = drm_atomic_get_new_connector_state(state,
4707 drm_old_conn_state = drm_atomic_get_old_connector_state(state,
4710 if (IS_ERR(drm_new_conn_state)) {
4711 ret = PTR_ERR_OR_ZERO(drm_new_conn_state);
4715 dm_new_conn_state = to_dm_connector_state(drm_new_conn_state);
4716 dm_old_conn_state = to_dm_connector_state(drm_old_conn_state);
4718 new_stream = create_stream_for_sink(aconnector,
4719 &new_crtc_state->mode,
4723 * we can have no stream on ACTION_SET if a display
4724 * was disconnected during S3, in this case it not and
4725 * error, the OS will be updated after detection, and
4726 * do the right thing on next atomic commit
4730 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
4731 __func__, acrtc->base.base.id);
4735 if (dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) &&
4736 dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream)) {
4737 new_crtc_state->mode_changed = false;
4738 DRM_DEBUG_DRIVER("Mode change not required, setting mode_changed to %d",
4739 new_crtc_state->mode_changed);
4743 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
4747 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
4748 "planes_changed:%d, mode_changed:%d,active_changed:%d,"
4749 "connectors_changed:%d\n",
4751 new_crtc_state->enable,
4752 new_crtc_state->active,
4753 new_crtc_state->planes_changed,
4754 new_crtc_state->mode_changed,
4755 new_crtc_state->active_changed,
4756 new_crtc_state->connectors_changed);
4758 /* Remove stream for any changed/disabled CRTC */
4761 if (!dm_old_crtc_state->stream)
4764 DRM_DEBUG_DRIVER("Disabling DRM crtc: %d\n",
4767 /* i.e. reset mode */
4768 if (dc_remove_stream_from_ctx(
4771 dm_old_crtc_state->stream) != DC_OK) {
4776 dc_stream_release(dm_old_crtc_state->stream);
4777 dm_new_crtc_state->stream = NULL;
4779 *lock_and_validation_needed = true;
4781 } else {/* Add stream for any updated/enabled CRTC */
4783 * Quick fix to prevent NULL pointer on new_stream when
4784 * added MST connectors not found in existing crtc_state in the chained mode
4785 * TODO: need to dig out the root cause of that
4787 if (!aconnector || (!aconnector->dc_sink && aconnector->mst_port))
4790 if (modereset_required(new_crtc_state))
4793 if (modeset_required(new_crtc_state, new_stream,
4794 dm_old_crtc_state->stream)) {
4796 WARN_ON(dm_new_crtc_state->stream);
4798 dm_new_crtc_state->stream = new_stream;
4800 dc_stream_retain(new_stream);
4802 DRM_DEBUG_DRIVER("Enabling DRM crtc: %d\n",
4805 if (dc_add_stream_to_ctx(
4808 dm_new_crtc_state->stream) != DC_OK) {
4813 *lock_and_validation_needed = true;
4818 /* Release extra reference */
4820 dc_stream_release(new_stream);
4823 * We want to do dc stream updates that do not require a
4824 * full modeset below.
4826 if (!(enable && aconnector && new_crtc_state->enable &&
4827 new_crtc_state->active))
4830 * Given above conditions, the dc state cannot be NULL because:
4831 * 1. We're in the process of enabling CRTCs (just been added
4832 * to the dc context, or already is on the context)
4833 * 2. Has a valid connector attached, and
4834 * 3. Is currently active and enabled.
4835 * => The dc stream state currently exists.
4837 BUG_ON(dm_new_crtc_state->stream == NULL);
4839 /* Scaling or underscan settings */
4840 if (is_scaling_state_different(dm_old_conn_state, dm_new_conn_state))
4841 update_stream_scaling_settings(
4842 &new_crtc_state->mode, dm_new_conn_state, dm_new_crtc_state->stream);
4845 * Color management settings. We also update color properties
4846 * when a modeset is needed, to ensure it gets reprogrammed.
4848 if (dm_new_crtc_state->base.color_mgmt_changed ||
4849 drm_atomic_crtc_needs_modeset(new_crtc_state)) {
4850 ret = amdgpu_dm_set_regamma_lut(dm_new_crtc_state);
4853 amdgpu_dm_set_ctm(dm_new_crtc_state);
4861 dc_stream_release(new_stream);
4865 static int dm_update_planes_state(struct dc *dc,
4866 struct drm_atomic_state *state,
4868 bool *lock_and_validation_needed)
4870 struct drm_crtc *new_plane_crtc, *old_plane_crtc;
4871 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
4872 struct drm_plane *plane;
4873 struct drm_plane_state *old_plane_state, *new_plane_state;
4874 struct dm_crtc_state *dm_new_crtc_state, *dm_old_crtc_state;
4875 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
4876 struct dm_plane_state *dm_new_plane_state, *dm_old_plane_state;
4878 /* TODO return page_flip_needed() function */
4879 bool pflip_needed = !state->allow_modeset;
4883 /* Add new planes, in reverse order as DC expectation */
4884 for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) {
4885 new_plane_crtc = new_plane_state->crtc;
4886 old_plane_crtc = old_plane_state->crtc;
4887 dm_new_plane_state = to_dm_plane_state(new_plane_state);
4888 dm_old_plane_state = to_dm_plane_state(old_plane_state);
4890 /*TODO Implement atomic check for cursor plane */
4891 if (plane->type == DRM_PLANE_TYPE_CURSOR)
4894 /* Remove any changed/removed planes */
4897 plane->type != DRM_PLANE_TYPE_OVERLAY)
4900 if (!old_plane_crtc)
4903 old_crtc_state = drm_atomic_get_old_crtc_state(
4904 state, old_plane_crtc);
4905 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
4907 if (!dm_old_crtc_state->stream)
4910 DRM_DEBUG_ATOMIC("Disabling DRM plane: %d on DRM crtc %d\n",
4911 plane->base.id, old_plane_crtc->base.id);
4913 if (!dc_remove_plane_from_context(
4915 dm_old_crtc_state->stream,
4916 dm_old_plane_state->dc_state,
4917 dm_state->context)) {
4924 dc_plane_state_release(dm_old_plane_state->dc_state);
4925 dm_new_plane_state->dc_state = NULL;
4927 *lock_and_validation_needed = true;
4929 } else { /* Add new planes */
4930 struct dc_plane_state *dc_new_plane_state;
4932 if (drm_atomic_plane_disabling(plane->state, new_plane_state))
4935 if (!new_plane_crtc)
4938 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_crtc);
4939 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
4941 if (!dm_new_crtc_state->stream)
4945 plane->type != DRM_PLANE_TYPE_OVERLAY)
4948 WARN_ON(dm_new_plane_state->dc_state);
4950 dc_new_plane_state = dc_create_plane_state(dc);
4951 if (!dc_new_plane_state)
4954 DRM_DEBUG_DRIVER("Enabling DRM plane: %d on DRM crtc %d\n",
4955 plane->base.id, new_plane_crtc->base.id);
4957 ret = fill_plane_attributes(
4958 new_plane_crtc->dev->dev_private,
4963 dc_plane_state_release(dc_new_plane_state);
4968 * Any atomic check errors that occur after this will
4969 * not need a release. The plane state will be attached
4970 * to the stream, and therefore part of the atomic
4971 * state. It'll be released when the atomic state is
4974 if (!dc_add_plane_to_context(
4976 dm_new_crtc_state->stream,
4978 dm_state->context)) {
4980 dc_plane_state_release(dc_new_plane_state);
4984 dm_new_plane_state->dc_state = dc_new_plane_state;
4986 /* Tell DC to do a full surface update every time there
4987 * is a plane change. Inefficient, but works for now.
4989 dm_new_plane_state->dc_state->update_flags.bits.full_update = 1;
4991 *lock_and_validation_needed = true;
4999 static int amdgpu_dm_atomic_check(struct drm_device *dev,
5000 struct drm_atomic_state *state)
5002 struct amdgpu_device *adev = dev->dev_private;
5003 struct dc *dc = adev->dm.dc;
5004 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
5005 struct drm_connector *connector;
5006 struct drm_connector_state *old_con_state, *new_con_state;
5007 struct drm_crtc *crtc;
5008 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
5012 * This bool will be set for true for any modeset/reset
5013 * or plane update which implies non fast surface update.
5015 bool lock_and_validation_needed = false;
5017 ret = drm_atomic_helper_check_modeset(dev, state);
5021 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
5022 if (!drm_atomic_crtc_needs_modeset(new_crtc_state) &&
5023 !new_crtc_state->color_mgmt_changed)
5026 if (!new_crtc_state->enable)
5029 ret = drm_atomic_add_affected_connectors(state, crtc);
5033 ret = drm_atomic_add_affected_planes(state, crtc);
5038 dm_state->context = dc_create_state();
5039 ASSERT(dm_state->context);
5040 dc_resource_state_copy_construct_current(dc, dm_state->context);
5042 /* Remove exiting planes if they are modified */
5043 ret = dm_update_planes_state(dc, state, false, &lock_and_validation_needed);
5048 /* Disable all crtcs which require disable */
5049 ret = dm_update_crtcs_state(dc, state, false, &lock_and_validation_needed);
5054 /* Enable all crtcs which require enable */
5055 ret = dm_update_crtcs_state(dc, state, true, &lock_and_validation_needed);
5060 /* Add new/modified planes */
5061 ret = dm_update_planes_state(dc, state, true, &lock_and_validation_needed);
5066 /* Run this here since we want to validate the streams we created */
5067 ret = drm_atomic_helper_check_planes(dev, state);
5071 /* Check scaling and underscan changes*/
5072 /*TODO Removed scaling changes validation due to inability to commit
5073 * new stream into context w\o causing full reset. Need to
5074 * decide how to handle.
5076 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
5077 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
5078 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
5079 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
5081 /* Skip any modesets/resets */
5082 if (!acrtc || drm_atomic_crtc_needs_modeset(
5083 drm_atomic_get_new_crtc_state(state, &acrtc->base)))
5086 /* Skip any thing not scale or underscan changes */
5087 if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state))
5090 lock_and_validation_needed = true;
5094 * For full updates case when
5095 * removing/adding/updating streams on once CRTC while flipping
5097 * acquiring global lock will guarantee that any such full
5099 * will wait for completion of any outstanding flip using DRMs
5100 * synchronization events.
5103 if (lock_and_validation_needed) {
5105 ret = do_aquire_global_lock(dev, state);
5109 if (dc_validate_global_state(dc, dm_state->context) != DC_OK) {
5115 /* Must be success */
5120 if (ret == -EDEADLK)
5121 DRM_DEBUG_DRIVER("Atomic check stopped to avoid deadlock.\n");
5122 else if (ret == -EINTR || ret == -EAGAIN || ret == -ERESTARTSYS)
5123 DRM_DEBUG_DRIVER("Atomic check stopped due to signal.\n");
5125 DRM_DEBUG_DRIVER("Atomic check failed with err: %d \n", ret);
5130 static bool is_dp_capable_without_timing_msa(struct dc *dc,
5131 struct amdgpu_dm_connector *amdgpu_dm_connector)
5134 bool capable = false;
5136 if (amdgpu_dm_connector->dc_link &&
5137 dm_helpers_dp_read_dpcd(
5139 amdgpu_dm_connector->dc_link,
5140 DP_DOWN_STREAM_PORT_COUNT,
5142 sizeof(dpcd_data))) {
5143 capable = (dpcd_data & DP_MSA_TIMING_PAR_IGNORED) ? true:false;
5148 void amdgpu_dm_add_sink_to_freesync_module(struct drm_connector *connector,
5152 bool edid_check_required;
5153 struct detailed_timing *timing;
5154 struct detailed_non_pixel *data;
5155 struct detailed_data_monitor_range *range;
5156 struct amdgpu_dm_connector *amdgpu_dm_connector =
5157 to_amdgpu_dm_connector(connector);
5158 struct dm_connector_state *dm_con_state;
5160 struct drm_device *dev = connector->dev;
5161 struct amdgpu_device *adev = dev->dev_private;
5163 if (!connector->state) {
5164 DRM_ERROR("%s - Connector has no state", __func__);
5168 dm_con_state = to_dm_connector_state(connector->state);
5170 edid_check_required = false;
5171 if (!amdgpu_dm_connector->dc_sink) {
5172 DRM_ERROR("dc_sink NULL, could not add free_sync module.\n");
5175 if (!adev->dm.freesync_module)
5178 * if edid non zero restrict freesync only for dp and edp
5181 if (amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT
5182 || amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_EDP) {
5183 edid_check_required = is_dp_capable_without_timing_msa(
5185 amdgpu_dm_connector);
5188 dm_con_state->freesync_capable = false;
5189 if (edid_check_required == true && (edid->version > 1 ||
5190 (edid->version == 1 && edid->revision > 1))) {
5191 for (i = 0; i < 4; i++) {
5193 timing = &edid->detailed_timings[i];
5194 data = &timing->data.other_data;
5195 range = &data->data.range;
5197 * Check if monitor has continuous frequency mode
5199 if (data->type != EDID_DETAIL_MONITOR_RANGE)
5202 * Check for flag range limits only. If flag == 1 then
5203 * no additional timing information provided.
5204 * Default GTF, GTF Secondary curve and CVT are not
5207 if (range->flags != 1)
5210 amdgpu_dm_connector->min_vfreq = range->min_vfreq;
5211 amdgpu_dm_connector->max_vfreq = range->max_vfreq;
5212 amdgpu_dm_connector->pixel_clock_mhz =
5213 range->pixel_clock_mhz * 10;
5217 if (amdgpu_dm_connector->max_vfreq -
5218 amdgpu_dm_connector->min_vfreq > 10) {
5219 amdgpu_dm_connector->caps.supported = true;
5220 amdgpu_dm_connector->caps.min_refresh_in_micro_hz =
5221 amdgpu_dm_connector->min_vfreq * 1000000;
5222 amdgpu_dm_connector->caps.max_refresh_in_micro_hz =
5223 amdgpu_dm_connector->max_vfreq * 1000000;
5224 dm_con_state->freesync_capable = true;
5229 * TODO figure out how to notify user-mode or DRM of freesync caps
5230 * once we figure out how to deal with freesync in an upstreamable
5236 void amdgpu_dm_remove_sink_from_freesync_module(struct drm_connector *connector)
5239 * TODO fill in once we figure out how to deal with freesync in
5240 * an upstreamable fashion