2 * Copyright 2015 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
26 /* The caprices of the preprocessor require that this be declared right here */
27 #define CREATE_TRACE_POINTS
29 #include "dm_services_types.h"
31 #include "dc/inc/core_types.h"
32 #include "dal_asic_id.h"
33 #include "dmub/inc/dmub_srv.h"
34 #include "dc/inc/hw/dmcu.h"
35 #include "dc/inc/hw/abm.h"
36 #include "dc/dc_dmub_srv.h"
40 #include "amdgpu_display.h"
41 #include "amdgpu_ucode.h"
43 #include "amdgpu_dm.h"
44 #ifdef CONFIG_DRM_AMD_DC_HDCP
45 #include "amdgpu_dm_hdcp.h"
46 #include <drm/drm_hdcp.h>
48 #include "amdgpu_pm.h"
50 #include "amd_shared.h"
51 #include "amdgpu_dm_irq.h"
52 #include "dm_helpers.h"
53 #include "amdgpu_dm_mst_types.h"
54 #if defined(CONFIG_DEBUG_FS)
55 #include "amdgpu_dm_debugfs.h"
58 #include "ivsrcid/ivsrcid_vislands30.h"
60 #include <linux/module.h>
61 #include <linux/moduleparam.h>
62 #include <linux/version.h>
63 #include <linux/types.h>
64 #include <linux/pm_runtime.h>
65 #include <linux/pci.h>
66 #include <linux/firmware.h>
67 #include <linux/component.h>
69 #include <drm/drm_atomic.h>
70 #include <drm/drm_atomic_uapi.h>
71 #include <drm/drm_atomic_helper.h>
72 #include <drm/drm_dp_mst_helper.h>
73 #include <drm/drm_fb_helper.h>
74 #include <drm/drm_fourcc.h>
75 #include <drm/drm_edid.h>
76 #include <drm/drm_vblank.h>
77 #include <drm/drm_audio_component.h>
78 #include <drm/drm_hdcp.h>
80 #if defined(CONFIG_DRM_AMD_DC_DCN)
81 #include "ivsrcid/dcn/irqsrcs_dcn_1_0.h"
83 #include "dcn/dcn_1_0_offset.h"
84 #include "dcn/dcn_1_0_sh_mask.h"
85 #include "soc15_hw_ip.h"
86 #include "vega10_ip_offset.h"
88 #include "soc15_common.h"
91 #include "modules/inc/mod_freesync.h"
92 #include "modules/power/power_helpers.h"
93 #include "modules/inc/mod_info_packet.h"
95 #define FIRMWARE_RENOIR_DMUB "amdgpu/renoir_dmcub.bin"
96 MODULE_FIRMWARE(FIRMWARE_RENOIR_DMUB);
98 #define FIRMWARE_RAVEN_DMCU "amdgpu/raven_dmcu.bin"
99 MODULE_FIRMWARE(FIRMWARE_RAVEN_DMCU);
101 /* Number of bytes in PSP header for firmware. */
102 #define PSP_HEADER_BYTES 0x100
104 /* Number of bytes in PSP footer for firmware. */
105 #define PSP_FOOTER_BYTES 0x100
110 * The AMDgpu display manager, **amdgpu_dm** (or even simpler,
111 * **dm**) sits between DRM and DC. It acts as a liason, converting DRM
112 * requests into DC requests, and DC responses into DRM responses.
114 * The root control structure is &struct amdgpu_display_manager.
117 /* basic init/fini API */
118 static int amdgpu_dm_init(struct amdgpu_device *adev);
119 static void amdgpu_dm_fini(struct amdgpu_device *adev);
122 * initializes drm_device display related structures, based on the information
123 * provided by DAL. The drm strcutures are: drm_crtc, drm_connector,
124 * drm_encoder, drm_mode_config
126 * Returns 0 on success
128 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev);
129 /* removes and deallocates the drm structures, created by the above function */
130 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm);
133 amdgpu_dm_update_connector_after_detect(struct amdgpu_dm_connector *aconnector);
135 static int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
136 struct drm_plane *plane,
137 unsigned long possible_crtcs,
138 const struct dc_plane_cap *plane_cap);
139 static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
140 struct drm_plane *plane,
141 uint32_t link_index);
142 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
143 struct amdgpu_dm_connector *amdgpu_dm_connector,
145 struct amdgpu_encoder *amdgpu_encoder);
146 static int amdgpu_dm_encoder_init(struct drm_device *dev,
147 struct amdgpu_encoder *aencoder,
148 uint32_t link_index);
150 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector);
152 static int amdgpu_dm_atomic_commit(struct drm_device *dev,
153 struct drm_atomic_state *state,
156 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state);
158 static int amdgpu_dm_atomic_check(struct drm_device *dev,
159 struct drm_atomic_state *state);
161 static void handle_cursor_update(struct drm_plane *plane,
162 struct drm_plane_state *old_plane_state);
164 static void amdgpu_dm_set_psr_caps(struct dc_link *link);
165 static bool amdgpu_dm_psr_enable(struct dc_stream_state *stream);
166 static bool amdgpu_dm_link_setup_psr(struct dc_stream_state *stream);
167 static bool amdgpu_dm_psr_disable(struct dc_stream_state *stream);
171 * dm_vblank_get_counter
174 * Get counter for number of vertical blanks
177 * struct amdgpu_device *adev - [in] desired amdgpu device
178 * int disp_idx - [in] which CRTC to get the counter from
181 * Counter for vertical blanks
183 static u32 dm_vblank_get_counter(struct amdgpu_device *adev, int crtc)
185 if (crtc >= adev->mode_info.num_crtc)
188 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
189 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(
193 if (acrtc_state->stream == NULL) {
194 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
199 return dc_stream_get_vblank_counter(acrtc_state->stream);
203 static int dm_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
204 u32 *vbl, u32 *position)
206 uint32_t v_blank_start, v_blank_end, h_position, v_position;
208 if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
211 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
212 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(
215 if (acrtc_state->stream == NULL) {
216 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
222 * TODO rework base driver to use values directly.
223 * for now parse it back into reg-format
225 dc_stream_get_scanoutpos(acrtc_state->stream,
231 *position = v_position | (h_position << 16);
232 *vbl = v_blank_start | (v_blank_end << 16);
238 static bool dm_is_idle(void *handle)
244 static int dm_wait_for_idle(void *handle)
250 static bool dm_check_soft_reset(void *handle)
255 static int dm_soft_reset(void *handle)
261 static struct amdgpu_crtc *
262 get_crtc_by_otg_inst(struct amdgpu_device *adev,
265 struct drm_device *dev = adev->ddev;
266 struct drm_crtc *crtc;
267 struct amdgpu_crtc *amdgpu_crtc;
269 if (otg_inst == -1) {
271 return adev->mode_info.crtcs[0];
274 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
275 amdgpu_crtc = to_amdgpu_crtc(crtc);
277 if (amdgpu_crtc->otg_inst == otg_inst)
284 static inline bool amdgpu_dm_vrr_active(struct dm_crtc_state *dm_state)
286 return dm_state->freesync_config.state == VRR_STATE_ACTIVE_VARIABLE ||
287 dm_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED;
291 * dm_pflip_high_irq() - Handle pageflip interrupt
292 * @interrupt_params: ignored
294 * Handles the pageflip interrupt by notifying all interested parties
295 * that the pageflip has been completed.
297 static void dm_pflip_high_irq(void *interrupt_params)
299 struct amdgpu_crtc *amdgpu_crtc;
300 struct common_irq_params *irq_params = interrupt_params;
301 struct amdgpu_device *adev = irq_params->adev;
303 struct drm_pending_vblank_event *e;
304 struct dm_crtc_state *acrtc_state;
305 uint32_t vpos, hpos, v_blank_start, v_blank_end;
308 amdgpu_crtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_PFLIP);
310 /* IRQ could occur when in initial stage */
311 /* TODO work and BO cleanup */
312 if (amdgpu_crtc == NULL) {
313 DRM_DEBUG_DRIVER("CRTC is null, returning.\n");
317 spin_lock_irqsave(&adev->ddev->event_lock, flags);
319 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED){
320 DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d !=AMDGPU_FLIP_SUBMITTED(%d) on crtc:%d[%p] \n",
321 amdgpu_crtc->pflip_status,
322 AMDGPU_FLIP_SUBMITTED,
323 amdgpu_crtc->crtc_id,
325 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
329 /* page flip completed. */
330 e = amdgpu_crtc->event;
331 amdgpu_crtc->event = NULL;
336 acrtc_state = to_dm_crtc_state(amdgpu_crtc->base.state);
337 vrr_active = amdgpu_dm_vrr_active(acrtc_state);
339 /* Fixed refresh rate, or VRR scanout position outside front-porch? */
341 !dc_stream_get_scanoutpos(acrtc_state->stream, &v_blank_start,
342 &v_blank_end, &hpos, &vpos) ||
343 (vpos < v_blank_start)) {
344 /* Update to correct count and vblank timestamp if racing with
345 * vblank irq. This also updates to the correct vblank timestamp
346 * even in VRR mode, as scanout is past the front-porch atm.
348 drm_crtc_accurate_vblank_count(&amdgpu_crtc->base);
350 /* Wake up userspace by sending the pageflip event with proper
351 * count and timestamp of vblank of flip completion.
354 drm_crtc_send_vblank_event(&amdgpu_crtc->base, e);
356 /* Event sent, so done with vblank for this flip */
357 drm_crtc_vblank_put(&amdgpu_crtc->base);
360 /* VRR active and inside front-porch: vblank count and
361 * timestamp for pageflip event will only be up to date after
362 * drm_crtc_handle_vblank() has been executed from late vblank
363 * irq handler after start of back-porch (vline 0). We queue the
364 * pageflip event for send-out by drm_crtc_handle_vblank() with
365 * updated timestamp and count, once it runs after us.
367 * We need to open-code this instead of using the helper
368 * drm_crtc_arm_vblank_event(), as that helper would
369 * call drm_crtc_accurate_vblank_count(), which we must
370 * not call in VRR mode while we are in front-porch!
373 /* sequence will be replaced by real count during send-out. */
374 e->sequence = drm_crtc_vblank_count(&amdgpu_crtc->base);
375 e->pipe = amdgpu_crtc->crtc_id;
377 list_add_tail(&e->base.link, &adev->ddev->vblank_event_list);
381 /* Keep track of vblank of this flip for flip throttling. We use the
382 * cooked hw counter, as that one incremented at start of this vblank
383 * of pageflip completion, so last_flip_vblank is the forbidden count
384 * for queueing new pageflips if vsync + VRR is enabled.
386 amdgpu_crtc->last_flip_vblank = amdgpu_get_vblank_counter_kms(adev->ddev,
387 amdgpu_crtc->crtc_id);
389 amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
390 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
392 DRM_DEBUG_DRIVER("crtc:%d[%p], pflip_stat:AMDGPU_FLIP_NONE, vrr[%d]-fp %d\n",
393 amdgpu_crtc->crtc_id, amdgpu_crtc,
394 vrr_active, (int) !e);
397 static void dm_vupdate_high_irq(void *interrupt_params)
399 struct common_irq_params *irq_params = interrupt_params;
400 struct amdgpu_device *adev = irq_params->adev;
401 struct amdgpu_crtc *acrtc;
402 struct dm_crtc_state *acrtc_state;
405 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VUPDATE);
408 acrtc_state = to_dm_crtc_state(acrtc->base.state);
410 DRM_DEBUG_DRIVER("crtc:%d, vupdate-vrr:%d\n", acrtc->crtc_id,
411 amdgpu_dm_vrr_active(acrtc_state));
413 /* Core vblank handling is done here after end of front-porch in
414 * vrr mode, as vblank timestamping will give valid results
415 * while now done after front-porch. This will also deliver
416 * page-flip completion events that have been queued to us
417 * if a pageflip happened inside front-porch.
419 if (amdgpu_dm_vrr_active(acrtc_state)) {
420 drm_crtc_handle_vblank(&acrtc->base);
422 /* BTR processing for pre-DCE12 ASICs */
423 if (acrtc_state->stream &&
424 adev->family < AMDGPU_FAMILY_AI) {
425 spin_lock_irqsave(&adev->ddev->event_lock, flags);
426 mod_freesync_handle_v_update(
427 adev->dm.freesync_module,
429 &acrtc_state->vrr_params);
431 dc_stream_adjust_vmin_vmax(
434 &acrtc_state->vrr_params.adjust);
435 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
442 * dm_crtc_high_irq() - Handles CRTC interrupt
443 * @interrupt_params: ignored
445 * Handles the CRTC/VSYNC interrupt by notfying DRM's VBLANK
448 static void dm_crtc_high_irq(void *interrupt_params)
450 struct common_irq_params *irq_params = interrupt_params;
451 struct amdgpu_device *adev = irq_params->adev;
452 struct amdgpu_crtc *acrtc;
453 struct dm_crtc_state *acrtc_state;
456 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VBLANK);
459 acrtc_state = to_dm_crtc_state(acrtc->base.state);
461 DRM_DEBUG_DRIVER("crtc:%d, vupdate-vrr:%d\n", acrtc->crtc_id,
462 amdgpu_dm_vrr_active(acrtc_state));
464 /* Core vblank handling at start of front-porch is only possible
465 * in non-vrr mode, as only there vblank timestamping will give
466 * valid results while done in front-porch. Otherwise defer it
467 * to dm_vupdate_high_irq after end of front-porch.
469 if (!amdgpu_dm_vrr_active(acrtc_state))
470 drm_crtc_handle_vblank(&acrtc->base);
472 /* Following stuff must happen at start of vblank, for crc
473 * computation and below-the-range btr support in vrr mode.
475 amdgpu_dm_crtc_handle_crc_irq(&acrtc->base);
477 if (acrtc_state->stream && adev->family >= AMDGPU_FAMILY_AI &&
478 acrtc_state->vrr_params.supported &&
479 acrtc_state->freesync_config.state == VRR_STATE_ACTIVE_VARIABLE) {
480 spin_lock_irqsave(&adev->ddev->event_lock, flags);
481 mod_freesync_handle_v_update(
482 adev->dm.freesync_module,
484 &acrtc_state->vrr_params);
486 dc_stream_adjust_vmin_vmax(
489 &acrtc_state->vrr_params.adjust);
490 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
495 #if defined(CONFIG_DRM_AMD_DC_DCN)
497 * dm_dcn_crtc_high_irq() - Handles VStartup interrupt for DCN generation ASICs
498 * @interrupt params - interrupt parameters
500 * Notify DRM's vblank event handler at VSTARTUP
502 * Unlike DCE hardware, we trigger the handler at VSTARTUP. at which:
503 * * We are close enough to VUPDATE - the point of no return for hw
504 * * We are in the fixed portion of variable front porch when vrr is enabled
505 * * We are before VUPDATE, where double-buffered vrr registers are swapped
507 * It is therefore the correct place to signal vblank, send user flip events,
510 static void dm_dcn_crtc_high_irq(void *interrupt_params)
512 struct common_irq_params *irq_params = interrupt_params;
513 struct amdgpu_device *adev = irq_params->adev;
514 struct amdgpu_crtc *acrtc;
515 struct dm_crtc_state *acrtc_state;
518 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VBLANK);
523 acrtc_state = to_dm_crtc_state(acrtc->base.state);
525 DRM_DEBUG_DRIVER("crtc:%d, vupdate-vrr:%d\n", acrtc->crtc_id,
526 amdgpu_dm_vrr_active(acrtc_state));
528 amdgpu_dm_crtc_handle_crc_irq(&acrtc->base);
529 drm_crtc_handle_vblank(&acrtc->base);
531 spin_lock_irqsave(&adev->ddev->event_lock, flags);
533 if (acrtc_state->vrr_params.supported &&
534 acrtc_state->freesync_config.state == VRR_STATE_ACTIVE_VARIABLE) {
535 mod_freesync_handle_v_update(
536 adev->dm.freesync_module,
538 &acrtc_state->vrr_params);
540 dc_stream_adjust_vmin_vmax(
543 &acrtc_state->vrr_params.adjust);
546 if (acrtc->pflip_status == AMDGPU_FLIP_SUBMITTED) {
548 drm_crtc_send_vblank_event(&acrtc->base, acrtc->event);
550 drm_crtc_vblank_put(&acrtc->base);
552 acrtc->pflip_status = AMDGPU_FLIP_NONE;
555 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
559 static int dm_set_clockgating_state(void *handle,
560 enum amd_clockgating_state state)
565 static int dm_set_powergating_state(void *handle,
566 enum amd_powergating_state state)
571 /* Prototypes of private functions */
572 static int dm_early_init(void* handle);
574 /* Allocate memory for FBC compressed data */
575 static void amdgpu_dm_fbc_init(struct drm_connector *connector)
577 struct drm_device *dev = connector->dev;
578 struct amdgpu_device *adev = dev->dev_private;
579 struct dm_comressor_info *compressor = &adev->dm.compressor;
580 struct amdgpu_dm_connector *aconn = to_amdgpu_dm_connector(connector);
581 struct drm_display_mode *mode;
582 unsigned long max_size = 0;
584 if (adev->dm.dc->fbc_compressor == NULL)
587 if (aconn->dc_link->connector_signal != SIGNAL_TYPE_EDP)
590 if (compressor->bo_ptr)
594 list_for_each_entry(mode, &connector->modes, head) {
595 if (max_size < mode->htotal * mode->vtotal)
596 max_size = mode->htotal * mode->vtotal;
600 int r = amdgpu_bo_create_kernel(adev, max_size * 4, PAGE_SIZE,
601 AMDGPU_GEM_DOMAIN_GTT, &compressor->bo_ptr,
602 &compressor->gpu_addr, &compressor->cpu_addr);
605 DRM_ERROR("DM: Failed to initialize FBC\n");
607 adev->dm.dc->ctx->fbc_gpu_addr = compressor->gpu_addr;
608 DRM_INFO("DM: FBC alloc %lu\n", max_size*4);
615 static int amdgpu_dm_audio_component_get_eld(struct device *kdev, int port,
616 int pipe, bool *enabled,
617 unsigned char *buf, int max_bytes)
619 struct drm_device *dev = dev_get_drvdata(kdev);
620 struct amdgpu_device *adev = dev->dev_private;
621 struct drm_connector *connector;
622 struct drm_connector_list_iter conn_iter;
623 struct amdgpu_dm_connector *aconnector;
628 mutex_lock(&adev->dm.audio_lock);
630 drm_connector_list_iter_begin(dev, &conn_iter);
631 drm_for_each_connector_iter(connector, &conn_iter) {
632 aconnector = to_amdgpu_dm_connector(connector);
633 if (aconnector->audio_inst != port)
637 ret = drm_eld_size(connector->eld);
638 memcpy(buf, connector->eld, min(max_bytes, ret));
642 drm_connector_list_iter_end(&conn_iter);
644 mutex_unlock(&adev->dm.audio_lock);
646 DRM_DEBUG_KMS("Get ELD : idx=%d ret=%d en=%d\n", port, ret, *enabled);
651 static const struct drm_audio_component_ops amdgpu_dm_audio_component_ops = {
652 .get_eld = amdgpu_dm_audio_component_get_eld,
655 static int amdgpu_dm_audio_component_bind(struct device *kdev,
656 struct device *hda_kdev, void *data)
658 struct drm_device *dev = dev_get_drvdata(kdev);
659 struct amdgpu_device *adev = dev->dev_private;
660 struct drm_audio_component *acomp = data;
662 acomp->ops = &amdgpu_dm_audio_component_ops;
664 adev->dm.audio_component = acomp;
669 static void amdgpu_dm_audio_component_unbind(struct device *kdev,
670 struct device *hda_kdev, void *data)
672 struct drm_device *dev = dev_get_drvdata(kdev);
673 struct amdgpu_device *adev = dev->dev_private;
674 struct drm_audio_component *acomp = data;
678 adev->dm.audio_component = NULL;
681 static const struct component_ops amdgpu_dm_audio_component_bind_ops = {
682 .bind = amdgpu_dm_audio_component_bind,
683 .unbind = amdgpu_dm_audio_component_unbind,
686 static int amdgpu_dm_audio_init(struct amdgpu_device *adev)
693 adev->mode_info.audio.enabled = true;
695 adev->mode_info.audio.num_pins = adev->dm.dc->res_pool->audio_count;
697 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
698 adev->mode_info.audio.pin[i].channels = -1;
699 adev->mode_info.audio.pin[i].rate = -1;
700 adev->mode_info.audio.pin[i].bits_per_sample = -1;
701 adev->mode_info.audio.pin[i].status_bits = 0;
702 adev->mode_info.audio.pin[i].category_code = 0;
703 adev->mode_info.audio.pin[i].connected = false;
704 adev->mode_info.audio.pin[i].id =
705 adev->dm.dc->res_pool->audios[i]->inst;
706 adev->mode_info.audio.pin[i].offset = 0;
709 ret = component_add(adev->dev, &amdgpu_dm_audio_component_bind_ops);
713 adev->dm.audio_registered = true;
718 static void amdgpu_dm_audio_fini(struct amdgpu_device *adev)
723 if (!adev->mode_info.audio.enabled)
726 if (adev->dm.audio_registered) {
727 component_del(adev->dev, &amdgpu_dm_audio_component_bind_ops);
728 adev->dm.audio_registered = false;
731 /* TODO: Disable audio? */
733 adev->mode_info.audio.enabled = false;
736 void amdgpu_dm_audio_eld_notify(struct amdgpu_device *adev, int pin)
738 struct drm_audio_component *acomp = adev->dm.audio_component;
740 if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify) {
741 DRM_DEBUG_KMS("Notify ELD: %d\n", pin);
743 acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr,
748 static int dm_dmub_hw_init(struct amdgpu_device *adev)
750 const struct dmcub_firmware_header_v1_0 *hdr;
751 struct dmub_srv *dmub_srv = adev->dm.dmub_srv;
752 struct dmub_srv_fb_info *fb_info = adev->dm.dmub_fb_info;
753 const struct firmware *dmub_fw = adev->dm.dmub_fw;
754 struct dmcu *dmcu = adev->dm.dc->res_pool->dmcu;
755 struct abm *abm = adev->dm.dc->res_pool->abm;
756 struct dmub_srv_hw_params hw_params;
757 enum dmub_status status;
758 const unsigned char *fw_inst_const, *fw_bss_data;
759 uint32_t i, fw_inst_const_size, fw_bss_data_size;
763 /* DMUB isn't supported on the ASIC. */
767 DRM_ERROR("No framebuffer info for DMUB service.\n");
772 /* Firmware required for DMUB support. */
773 DRM_ERROR("No firmware provided for DMUB.\n");
777 status = dmub_srv_has_hw_support(dmub_srv, &has_hw_support);
778 if (status != DMUB_STATUS_OK) {
779 DRM_ERROR("Error checking HW support for DMUB: %d\n", status);
783 if (!has_hw_support) {
784 DRM_INFO("DMUB unsupported on ASIC\n");
788 hdr = (const struct dmcub_firmware_header_v1_0 *)dmub_fw->data;
790 fw_inst_const = dmub_fw->data +
791 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
794 fw_bss_data = dmub_fw->data +
795 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
796 le32_to_cpu(hdr->inst_const_bytes);
798 /* Copy firmware and bios info into FB memory. */
799 fw_inst_const_size = le32_to_cpu(hdr->inst_const_bytes) -
800 PSP_HEADER_BYTES - PSP_FOOTER_BYTES;
802 fw_bss_data_size = le32_to_cpu(hdr->bss_data_bytes);
804 memcpy(fb_info->fb[DMUB_WINDOW_0_INST_CONST].cpu_addr, fw_inst_const,
806 memcpy(fb_info->fb[DMUB_WINDOW_2_BSS_DATA].cpu_addr, fw_bss_data,
808 memcpy(fb_info->fb[DMUB_WINDOW_3_VBIOS].cpu_addr, adev->bios,
811 /* Reset regions that need to be reset. */
812 memset(fb_info->fb[DMUB_WINDOW_4_MAILBOX].cpu_addr, 0,
813 fb_info->fb[DMUB_WINDOW_4_MAILBOX].size);
815 memset(fb_info->fb[DMUB_WINDOW_5_TRACEBUFF].cpu_addr, 0,
816 fb_info->fb[DMUB_WINDOW_5_TRACEBUFF].size);
818 memset(fb_info->fb[DMUB_WINDOW_6_FW_STATE].cpu_addr, 0,
819 fb_info->fb[DMUB_WINDOW_6_FW_STATE].size);
821 /* Initialize hardware. */
822 memset(&hw_params, 0, sizeof(hw_params));
823 hw_params.fb_base = adev->gmc.fb_start;
824 hw_params.fb_offset = adev->gmc.aper_base;
827 hw_params.psp_version = dmcu->psp_version;
829 for (i = 0; i < fb_info->num_fb; ++i)
830 hw_params.fb[i] = &fb_info->fb[i];
832 status = dmub_srv_hw_init(dmub_srv, &hw_params);
833 if (status != DMUB_STATUS_OK) {
834 DRM_ERROR("Error initializing DMUB HW: %d\n", status);
838 /* Wait for firmware load to finish. */
839 status = dmub_srv_wait_for_auto_load(dmub_srv, 100000);
840 if (status != DMUB_STATUS_OK)
841 DRM_WARN("Wait for DMUB auto-load failed: %d\n", status);
843 /* Init DMCU and ABM if available. */
845 dmcu->funcs->dmcu_init(dmcu);
846 abm->dmcu_is_running = dmcu->funcs->is_dmcu_initialized(dmcu);
849 adev->dm.dc->ctx->dmub_srv = dc_dmub_srv_create(adev->dm.dc, dmub_srv);
850 if (!adev->dm.dc->ctx->dmub_srv) {
851 DRM_ERROR("Couldn't allocate DC DMUB server!\n");
855 DRM_INFO("DMUB hardware initialized: version=0x%08X\n",
856 adev->dm.dmcub_fw_version);
861 static int amdgpu_dm_init(struct amdgpu_device *adev)
863 struct dc_init_data init_data;
864 #ifdef CONFIG_DRM_AMD_DC_HDCP
865 struct dc_callback_init init_params;
869 adev->dm.ddev = adev->ddev;
870 adev->dm.adev = adev;
872 /* Zero all the fields */
873 memset(&init_data, 0, sizeof(init_data));
874 #ifdef CONFIG_DRM_AMD_DC_HDCP
875 memset(&init_params, 0, sizeof(init_params));
878 mutex_init(&adev->dm.dc_lock);
879 mutex_init(&adev->dm.audio_lock);
881 if(amdgpu_dm_irq_init(adev)) {
882 DRM_ERROR("amdgpu: failed to initialize DM IRQ support.\n");
886 init_data.asic_id.chip_family = adev->family;
888 init_data.asic_id.pci_revision_id = adev->rev_id;
889 init_data.asic_id.hw_internal_rev = adev->external_rev_id;
891 init_data.asic_id.vram_width = adev->gmc.vram_width;
892 /* TODO: initialize init_data.asic_id.vram_type here!!!! */
893 init_data.asic_id.atombios_base_address =
894 adev->mode_info.atom_context->bios;
896 init_data.driver = adev;
898 adev->dm.cgs_device = amdgpu_cgs_create_device(adev);
900 if (!adev->dm.cgs_device) {
901 DRM_ERROR("amdgpu: failed to create cgs device.\n");
905 init_data.cgs_device = adev->dm.cgs_device;
907 init_data.dce_environment = DCE_ENV_PRODUCTION_DRV;
910 * TODO debug why this doesn't work on Raven
912 if (adev->flags & AMD_IS_APU &&
913 adev->asic_type >= CHIP_CARRIZO &&
914 adev->asic_type < CHIP_RAVEN)
915 init_data.flags.gpu_vm_support = true;
917 if (amdgpu_dc_feature_mask & DC_FBC_MASK)
918 init_data.flags.fbc_support = true;
920 if (amdgpu_dc_feature_mask & DC_MULTI_MON_PP_MCLK_SWITCH_MASK)
921 init_data.flags.multi_mon_pp_mclk_switch = true;
923 if (amdgpu_dc_feature_mask & DC_DISABLE_FRACTIONAL_PWM_MASK)
924 init_data.flags.disable_fractional_pwm = true;
926 init_data.flags.power_down_display_on_boot = true;
928 init_data.soc_bounding_box = adev->dm.soc_bounding_box;
930 /* Display Core create. */
931 adev->dm.dc = dc_create(&init_data);
934 DRM_INFO("Display Core initialized with v%s!\n", DC_VER);
936 DRM_INFO("Display Core failed to initialize with v%s!\n", DC_VER);
940 dc_hardware_init(adev->dm.dc);
942 r = dm_dmub_hw_init(adev);
944 DRM_ERROR("DMUB interface failed to initialize: status=%d\n", r);
948 adev->dm.freesync_module = mod_freesync_create(adev->dm.dc);
949 if (!adev->dm.freesync_module) {
951 "amdgpu: failed to initialize freesync_module.\n");
953 DRM_DEBUG_DRIVER("amdgpu: freesync_module init done %p.\n",
954 adev->dm.freesync_module);
956 amdgpu_dm_init_color_mod();
958 #ifdef CONFIG_DRM_AMD_DC_HDCP
959 if (adev->asic_type >= CHIP_RAVEN) {
960 adev->dm.hdcp_workqueue = hdcp_create_workqueue(&adev->psp, &init_params.cp_psp, adev->dm.dc);
962 if (!adev->dm.hdcp_workqueue)
963 DRM_ERROR("amdgpu: failed to initialize hdcp_workqueue.\n");
965 DRM_DEBUG_DRIVER("amdgpu: hdcp_workqueue init done %p.\n", adev->dm.hdcp_workqueue);
967 dc_init_callbacks(adev->dm.dc, &init_params);
970 if (amdgpu_dm_initialize_drm_device(adev)) {
972 "amdgpu: failed to initialize sw for display support.\n");
976 /* Update the actual used number of crtc */
977 adev->mode_info.num_crtc = adev->dm.display_indexes_num;
979 /* TODO: Add_display_info? */
981 /* TODO use dynamic cursor width */
982 adev->ddev->mode_config.cursor_width = adev->dm.dc->caps.max_cursor_size;
983 adev->ddev->mode_config.cursor_height = adev->dm.dc->caps.max_cursor_size;
985 if (drm_vblank_init(adev->ddev, adev->dm.display_indexes_num)) {
987 "amdgpu: failed to initialize sw for display support.\n");
991 #if defined(CONFIG_DEBUG_FS)
992 if (dtn_debugfs_init(adev))
993 DRM_ERROR("amdgpu: failed initialize dtn debugfs support.\n");
996 DRM_DEBUG_DRIVER("KMS initialized.\n");
1000 amdgpu_dm_fini(adev);
1005 static void amdgpu_dm_fini(struct amdgpu_device *adev)
1007 amdgpu_dm_audio_fini(adev);
1009 amdgpu_dm_destroy_drm_device(&adev->dm);
1011 #ifdef CONFIG_DRM_AMD_DC_HDCP
1012 if (adev->dm.hdcp_workqueue) {
1013 hdcp_destroy(adev->dm.hdcp_workqueue);
1014 adev->dm.hdcp_workqueue = NULL;
1018 dc_deinit_callbacks(adev->dm.dc);
1020 if (adev->dm.dc->ctx->dmub_srv) {
1021 dc_dmub_srv_destroy(&adev->dm.dc->ctx->dmub_srv);
1022 adev->dm.dc->ctx->dmub_srv = NULL;
1025 if (adev->dm.dmub_bo)
1026 amdgpu_bo_free_kernel(&adev->dm.dmub_bo,
1027 &adev->dm.dmub_bo_gpu_addr,
1028 &adev->dm.dmub_bo_cpu_addr);
1030 /* DC Destroy TODO: Replace destroy DAL */
1032 dc_destroy(&adev->dm.dc);
1034 * TODO: pageflip, vlank interrupt
1036 * amdgpu_dm_irq_fini(adev);
1039 if (adev->dm.cgs_device) {
1040 amdgpu_cgs_destroy_device(adev->dm.cgs_device);
1041 adev->dm.cgs_device = NULL;
1043 if (adev->dm.freesync_module) {
1044 mod_freesync_destroy(adev->dm.freesync_module);
1045 adev->dm.freesync_module = NULL;
1048 mutex_destroy(&adev->dm.audio_lock);
1049 mutex_destroy(&adev->dm.dc_lock);
1054 static int load_dmcu_fw(struct amdgpu_device *adev)
1056 const char *fw_name_dmcu = NULL;
1058 const struct dmcu_firmware_header_v1_0 *hdr;
1060 switch(adev->asic_type) {
1070 case CHIP_POLARIS11:
1071 case CHIP_POLARIS10:
1072 case CHIP_POLARIS12:
1083 if (ASICREV_IS_PICASSO(adev->external_rev_id))
1084 fw_name_dmcu = FIRMWARE_RAVEN_DMCU;
1085 else if (ASICREV_IS_RAVEN2(adev->external_rev_id))
1086 fw_name_dmcu = FIRMWARE_RAVEN_DMCU;
1091 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type);
1095 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1096 DRM_DEBUG_KMS("dm: DMCU firmware not supported on direct or SMU loading\n");
1100 r = request_firmware_direct(&adev->dm.fw_dmcu, fw_name_dmcu, adev->dev);
1102 /* DMCU firmware is not necessary, so don't raise a fuss if it's missing */
1103 DRM_DEBUG_KMS("dm: DMCU firmware not found\n");
1104 adev->dm.fw_dmcu = NULL;
1108 dev_err(adev->dev, "amdgpu_dm: Can't load firmware \"%s\"\n",
1113 r = amdgpu_ucode_validate(adev->dm.fw_dmcu);
1115 dev_err(adev->dev, "amdgpu_dm: Can't validate firmware \"%s\"\n",
1117 release_firmware(adev->dm.fw_dmcu);
1118 adev->dm.fw_dmcu = NULL;
1122 hdr = (const struct dmcu_firmware_header_v1_0 *)adev->dm.fw_dmcu->data;
1123 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].ucode_id = AMDGPU_UCODE_ID_DMCU_ERAM;
1124 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].fw = adev->dm.fw_dmcu;
1125 adev->firmware.fw_size +=
1126 ALIGN(le32_to_cpu(hdr->header.ucode_size_bytes) - le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE);
1128 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].ucode_id = AMDGPU_UCODE_ID_DMCU_INTV;
1129 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].fw = adev->dm.fw_dmcu;
1130 adev->firmware.fw_size +=
1131 ALIGN(le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE);
1133 adev->dm.dmcu_fw_version = le32_to_cpu(hdr->header.ucode_version);
1135 DRM_DEBUG_KMS("PSP loading DMCU firmware\n");
1140 static uint32_t amdgpu_dm_dmub_reg_read(void *ctx, uint32_t address)
1142 struct amdgpu_device *adev = ctx;
1144 return dm_read_reg(adev->dm.dc->ctx, address);
1147 static void amdgpu_dm_dmub_reg_write(void *ctx, uint32_t address,
1150 struct amdgpu_device *adev = ctx;
1152 return dm_write_reg(adev->dm.dc->ctx, address, value);
1155 static int dm_dmub_sw_init(struct amdgpu_device *adev)
1157 struct dmub_srv_create_params create_params;
1158 struct dmub_srv_region_params region_params;
1159 struct dmub_srv_region_info region_info;
1160 struct dmub_srv_fb_params fb_params;
1161 struct dmub_srv_fb_info *fb_info;
1162 struct dmub_srv *dmub_srv;
1163 const struct dmcub_firmware_header_v1_0 *hdr;
1164 const char *fw_name_dmub;
1165 enum dmub_asic dmub_asic;
1166 enum dmub_status status;
1169 switch (adev->asic_type) {
1171 dmub_asic = DMUB_ASIC_DCN21;
1172 fw_name_dmub = FIRMWARE_RENOIR_DMUB;
1176 /* ASIC doesn't support DMUB. */
1180 r = request_firmware_direct(&adev->dm.dmub_fw, fw_name_dmub, adev->dev);
1182 DRM_ERROR("DMUB firmware loading failed: %d\n", r);
1186 r = amdgpu_ucode_validate(adev->dm.dmub_fw);
1188 DRM_ERROR("Couldn't validate DMUB firmware: %d\n", r);
1192 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1193 DRM_WARN("Only PSP firmware loading is supported for DMUB\n");
1197 hdr = (const struct dmcub_firmware_header_v1_0 *)adev->dm.dmub_fw->data;
1198 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCUB].ucode_id =
1199 AMDGPU_UCODE_ID_DMCUB;
1200 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCUB].fw = adev->dm.dmub_fw;
1201 adev->firmware.fw_size +=
1202 ALIGN(le32_to_cpu(hdr->inst_const_bytes), PAGE_SIZE);
1204 adev->dm.dmcub_fw_version = le32_to_cpu(hdr->header.ucode_version);
1206 DRM_INFO("Loading DMUB firmware via PSP: version=0x%08X\n",
1207 adev->dm.dmcub_fw_version);
1209 adev->dm.dmub_srv = kzalloc(sizeof(*adev->dm.dmub_srv), GFP_KERNEL);
1210 dmub_srv = adev->dm.dmub_srv;
1213 DRM_ERROR("Failed to allocate DMUB service!\n");
1217 memset(&create_params, 0, sizeof(create_params));
1218 create_params.user_ctx = adev;
1219 create_params.funcs.reg_read = amdgpu_dm_dmub_reg_read;
1220 create_params.funcs.reg_write = amdgpu_dm_dmub_reg_write;
1221 create_params.asic = dmub_asic;
1223 /* Create the DMUB service. */
1224 status = dmub_srv_create(dmub_srv, &create_params);
1225 if (status != DMUB_STATUS_OK) {
1226 DRM_ERROR("Error creating DMUB service: %d\n", status);
1230 /* Calculate the size of all the regions for the DMUB service. */
1231 memset(®ion_params, 0, sizeof(region_params));
1233 region_params.inst_const_size = le32_to_cpu(hdr->inst_const_bytes) -
1234 PSP_HEADER_BYTES - PSP_FOOTER_BYTES;
1235 region_params.bss_data_size = le32_to_cpu(hdr->bss_data_bytes);
1236 region_params.vbios_size = adev->bios_size;
1237 region_params.fw_bss_data =
1238 adev->dm.dmub_fw->data +
1239 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
1240 le32_to_cpu(hdr->inst_const_bytes);
1242 status = dmub_srv_calc_region_info(dmub_srv, ®ion_params,
1245 if (status != DMUB_STATUS_OK) {
1246 DRM_ERROR("Error calculating DMUB region info: %d\n", status);
1251 * Allocate a framebuffer based on the total size of all the regions.
1252 * TODO: Move this into GART.
1254 r = amdgpu_bo_create_kernel(adev, region_info.fb_size, PAGE_SIZE,
1255 AMDGPU_GEM_DOMAIN_VRAM, &adev->dm.dmub_bo,
1256 &adev->dm.dmub_bo_gpu_addr,
1257 &adev->dm.dmub_bo_cpu_addr);
1261 /* Rebase the regions on the framebuffer address. */
1262 memset(&fb_params, 0, sizeof(fb_params));
1263 fb_params.cpu_addr = adev->dm.dmub_bo_cpu_addr;
1264 fb_params.gpu_addr = adev->dm.dmub_bo_gpu_addr;
1265 fb_params.region_info = ®ion_info;
1267 adev->dm.dmub_fb_info =
1268 kzalloc(sizeof(*adev->dm.dmub_fb_info), GFP_KERNEL);
1269 fb_info = adev->dm.dmub_fb_info;
1273 "Failed to allocate framebuffer info for DMUB service!\n");
1277 status = dmub_srv_calc_fb_info(dmub_srv, &fb_params, fb_info);
1278 if (status != DMUB_STATUS_OK) {
1279 DRM_ERROR("Error calculating DMUB FB info: %d\n", status);
1286 static int dm_sw_init(void *handle)
1288 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1291 r = dm_dmub_sw_init(adev);
1295 return load_dmcu_fw(adev);
1298 static int dm_sw_fini(void *handle)
1300 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1302 kfree(adev->dm.dmub_fb_info);
1303 adev->dm.dmub_fb_info = NULL;
1305 if (adev->dm.dmub_srv) {
1306 dmub_srv_destroy(adev->dm.dmub_srv);
1307 adev->dm.dmub_srv = NULL;
1310 if (adev->dm.dmub_fw) {
1311 release_firmware(adev->dm.dmub_fw);
1312 adev->dm.dmub_fw = NULL;
1315 if(adev->dm.fw_dmcu) {
1316 release_firmware(adev->dm.fw_dmcu);
1317 adev->dm.fw_dmcu = NULL;
1323 static int detect_mst_link_for_all_connectors(struct drm_device *dev)
1325 struct amdgpu_dm_connector *aconnector;
1326 struct drm_connector *connector;
1327 struct drm_connector_list_iter iter;
1330 drm_connector_list_iter_begin(dev, &iter);
1331 drm_for_each_connector_iter(connector, &iter) {
1332 aconnector = to_amdgpu_dm_connector(connector);
1333 if (aconnector->dc_link->type == dc_connection_mst_branch &&
1334 aconnector->mst_mgr.aux) {
1335 DRM_DEBUG_DRIVER("DM_MST: starting TM on aconnector: %p [id: %d]\n",
1337 aconnector->base.base.id);
1339 ret = drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true);
1341 DRM_ERROR("DM_MST: Failed to start MST\n");
1342 aconnector->dc_link->type =
1343 dc_connection_single;
1348 drm_connector_list_iter_end(&iter);
1353 static int dm_late_init(void *handle)
1355 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1357 struct dmcu_iram_parameters params;
1358 unsigned int linear_lut[16];
1360 struct dmcu *dmcu = adev->dm.dc->res_pool->dmcu;
1363 for (i = 0; i < 16; i++)
1364 linear_lut[i] = 0xFFFF * i / 15;
1367 params.backlight_ramping_start = 0xCCCC;
1368 params.backlight_ramping_reduction = 0xCCCCCCCC;
1369 params.backlight_lut_array_size = 16;
1370 params.backlight_lut_array = linear_lut;
1372 /* Min backlight level after ABM reduction, Don't allow below 1%
1373 * 0xFFFF x 0.01 = 0x28F
1375 params.min_abm_backlight = 0x28F;
1377 /* todo will enable for navi10 */
1378 if (adev->asic_type <= CHIP_RAVEN) {
1379 ret = dmcu_load_iram(dmcu, params);
1385 return detect_mst_link_for_all_connectors(adev->ddev);
1388 static void s3_handle_mst(struct drm_device *dev, bool suspend)
1390 struct amdgpu_dm_connector *aconnector;
1391 struct drm_connector *connector;
1392 struct drm_connector_list_iter iter;
1393 struct drm_dp_mst_topology_mgr *mgr;
1395 bool need_hotplug = false;
1397 drm_connector_list_iter_begin(dev, &iter);
1398 drm_for_each_connector_iter(connector, &iter) {
1399 aconnector = to_amdgpu_dm_connector(connector);
1400 if (aconnector->dc_link->type != dc_connection_mst_branch ||
1401 aconnector->mst_port)
1404 mgr = &aconnector->mst_mgr;
1407 drm_dp_mst_topology_mgr_suspend(mgr);
1409 ret = drm_dp_mst_topology_mgr_resume(mgr, true);
1411 drm_dp_mst_topology_mgr_set_mst(mgr, false);
1412 need_hotplug = true;
1416 drm_connector_list_iter_end(&iter);
1419 drm_kms_helper_hotplug_event(dev);
1423 * dm_hw_init() - Initialize DC device
1424 * @handle: The base driver device containing the amdgpu_dm device.
1426 * Initialize the &struct amdgpu_display_manager device. This involves calling
1427 * the initializers of each DM component, then populating the struct with them.
1429 * Although the function implies hardware initialization, both hardware and
1430 * software are initialized here. Splitting them out to their relevant init
1431 * hooks is a future TODO item.
1433 * Some notable things that are initialized here:
1435 * - Display Core, both software and hardware
1436 * - DC modules that we need (freesync and color management)
1437 * - DRM software states
1438 * - Interrupt sources and handlers
1440 * - Debug FS entries, if enabled
1442 static int dm_hw_init(void *handle)
1444 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1445 /* Create DAL display manager */
1446 amdgpu_dm_init(adev);
1447 amdgpu_dm_hpd_init(adev);
1453 * dm_hw_fini() - Teardown DC device
1454 * @handle: The base driver device containing the amdgpu_dm device.
1456 * Teardown components within &struct amdgpu_display_manager that require
1457 * cleanup. This involves cleaning up the DRM device, DC, and any modules that
1458 * were loaded. Also flush IRQ workqueues and disable them.
1460 static int dm_hw_fini(void *handle)
1462 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1464 amdgpu_dm_hpd_fini(adev);
1466 amdgpu_dm_irq_fini(adev);
1467 amdgpu_dm_fini(adev);
1471 static int dm_suspend(void *handle)
1473 struct amdgpu_device *adev = handle;
1474 struct amdgpu_display_manager *dm = &adev->dm;
1477 WARN_ON(adev->dm.cached_state);
1478 adev->dm.cached_state = drm_atomic_helper_suspend(adev->ddev);
1480 s3_handle_mst(adev->ddev, true);
1482 amdgpu_dm_irq_suspend(adev);
1485 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D3);
1490 static struct amdgpu_dm_connector *
1491 amdgpu_dm_find_first_crtc_matching_connector(struct drm_atomic_state *state,
1492 struct drm_crtc *crtc)
1495 struct drm_connector_state *new_con_state;
1496 struct drm_connector *connector;
1497 struct drm_crtc *crtc_from_state;
1499 for_each_new_connector_in_state(state, connector, new_con_state, i) {
1500 crtc_from_state = new_con_state->crtc;
1502 if (crtc_from_state == crtc)
1503 return to_amdgpu_dm_connector(connector);
1509 static void emulated_link_detect(struct dc_link *link)
1511 struct dc_sink_init_data sink_init_data = { 0 };
1512 struct display_sink_capability sink_caps = { 0 };
1513 enum dc_edid_status edid_status;
1514 struct dc_context *dc_ctx = link->ctx;
1515 struct dc_sink *sink = NULL;
1516 struct dc_sink *prev_sink = NULL;
1518 link->type = dc_connection_none;
1519 prev_sink = link->local_sink;
1521 if (prev_sink != NULL)
1522 dc_sink_retain(prev_sink);
1524 switch (link->connector_signal) {
1525 case SIGNAL_TYPE_HDMI_TYPE_A: {
1526 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
1527 sink_caps.signal = SIGNAL_TYPE_HDMI_TYPE_A;
1531 case SIGNAL_TYPE_DVI_SINGLE_LINK: {
1532 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
1533 sink_caps.signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
1537 case SIGNAL_TYPE_DVI_DUAL_LINK: {
1538 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
1539 sink_caps.signal = SIGNAL_TYPE_DVI_DUAL_LINK;
1543 case SIGNAL_TYPE_LVDS: {
1544 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
1545 sink_caps.signal = SIGNAL_TYPE_LVDS;
1549 case SIGNAL_TYPE_EDP: {
1550 sink_caps.transaction_type =
1551 DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
1552 sink_caps.signal = SIGNAL_TYPE_EDP;
1556 case SIGNAL_TYPE_DISPLAY_PORT: {
1557 sink_caps.transaction_type =
1558 DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
1559 sink_caps.signal = SIGNAL_TYPE_VIRTUAL;
1564 DC_ERROR("Invalid connector type! signal:%d\n",
1565 link->connector_signal);
1569 sink_init_data.link = link;
1570 sink_init_data.sink_signal = sink_caps.signal;
1572 sink = dc_sink_create(&sink_init_data);
1574 DC_ERROR("Failed to create sink!\n");
1578 /* dc_sink_create returns a new reference */
1579 link->local_sink = sink;
1581 edid_status = dm_helpers_read_local_edid(
1586 if (edid_status != EDID_OK)
1587 DC_ERROR("Failed to read EDID");
1591 static int dm_resume(void *handle)
1593 struct amdgpu_device *adev = handle;
1594 struct drm_device *ddev = adev->ddev;
1595 struct amdgpu_display_manager *dm = &adev->dm;
1596 struct amdgpu_dm_connector *aconnector;
1597 struct drm_connector *connector;
1598 struct drm_connector_list_iter iter;
1599 struct drm_crtc *crtc;
1600 struct drm_crtc_state *new_crtc_state;
1601 struct dm_crtc_state *dm_new_crtc_state;
1602 struct drm_plane *plane;
1603 struct drm_plane_state *new_plane_state;
1604 struct dm_plane_state *dm_new_plane_state;
1605 struct dm_atomic_state *dm_state = to_dm_atomic_state(dm->atomic_obj.state);
1606 enum dc_connection_type new_connection_type = dc_connection_none;
1609 /* Recreate dc_state - DC invalidates it when setting power state to S3. */
1610 dc_release_state(dm_state->context);
1611 dm_state->context = dc_create_state(dm->dc);
1612 /* TODO: Remove dc_state->dccg, use dc->dccg directly. */
1613 dc_resource_state_construct(dm->dc, dm_state->context);
1615 /* Before powering on DC we need to re-initialize DMUB. */
1616 r = dm_dmub_hw_init(adev);
1618 DRM_ERROR("DMUB interface failed to initialize: status=%d\n", r);
1620 /* power on hardware */
1621 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0);
1623 /* program HPD filter */
1627 * early enable HPD Rx IRQ, should be done before set mode as short
1628 * pulse interrupts are used for MST
1630 amdgpu_dm_irq_resume_early(adev);
1632 /* On resume we need to rewrite the MSTM control bits to enable MST*/
1633 s3_handle_mst(ddev, false);
1636 drm_connector_list_iter_begin(ddev, &iter);
1637 drm_for_each_connector_iter(connector, &iter) {
1638 aconnector = to_amdgpu_dm_connector(connector);
1641 * this is the case when traversing through already created
1642 * MST connectors, should be skipped
1644 if (aconnector->mst_port)
1647 mutex_lock(&aconnector->hpd_lock);
1648 if (!dc_link_detect_sink(aconnector->dc_link, &new_connection_type))
1649 DRM_ERROR("KMS: Failed to detect connector\n");
1651 if (aconnector->base.force && new_connection_type == dc_connection_none)
1652 emulated_link_detect(aconnector->dc_link);
1654 dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
1656 if (aconnector->fake_enable && aconnector->dc_link->local_sink)
1657 aconnector->fake_enable = false;
1659 if (aconnector->dc_sink)
1660 dc_sink_release(aconnector->dc_sink);
1661 aconnector->dc_sink = NULL;
1662 amdgpu_dm_update_connector_after_detect(aconnector);
1663 mutex_unlock(&aconnector->hpd_lock);
1665 drm_connector_list_iter_end(&iter);
1667 /* Force mode set in atomic commit */
1668 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i)
1669 new_crtc_state->active_changed = true;
1672 * atomic_check is expected to create the dc states. We need to release
1673 * them here, since they were duplicated as part of the suspend
1676 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) {
1677 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
1678 if (dm_new_crtc_state->stream) {
1679 WARN_ON(kref_read(&dm_new_crtc_state->stream->refcount) > 1);
1680 dc_stream_release(dm_new_crtc_state->stream);
1681 dm_new_crtc_state->stream = NULL;
1685 for_each_new_plane_in_state(dm->cached_state, plane, new_plane_state, i) {
1686 dm_new_plane_state = to_dm_plane_state(new_plane_state);
1687 if (dm_new_plane_state->dc_state) {
1688 WARN_ON(kref_read(&dm_new_plane_state->dc_state->refcount) > 1);
1689 dc_plane_state_release(dm_new_plane_state->dc_state);
1690 dm_new_plane_state->dc_state = NULL;
1694 drm_atomic_helper_resume(ddev, dm->cached_state);
1696 dm->cached_state = NULL;
1698 amdgpu_dm_irq_resume_late(adev);
1706 * DM (and consequently DC) is registered in the amdgpu base driver as a IP
1707 * block. When CONFIG_DRM_AMD_DC is enabled, the DM device IP block is added to
1708 * the base driver's device list to be initialized and torn down accordingly.
1710 * The functions to do so are provided as hooks in &struct amd_ip_funcs.
1713 static const struct amd_ip_funcs amdgpu_dm_funcs = {
1715 .early_init = dm_early_init,
1716 .late_init = dm_late_init,
1717 .sw_init = dm_sw_init,
1718 .sw_fini = dm_sw_fini,
1719 .hw_init = dm_hw_init,
1720 .hw_fini = dm_hw_fini,
1721 .suspend = dm_suspend,
1722 .resume = dm_resume,
1723 .is_idle = dm_is_idle,
1724 .wait_for_idle = dm_wait_for_idle,
1725 .check_soft_reset = dm_check_soft_reset,
1726 .soft_reset = dm_soft_reset,
1727 .set_clockgating_state = dm_set_clockgating_state,
1728 .set_powergating_state = dm_set_powergating_state,
1731 const struct amdgpu_ip_block_version dm_ip_block =
1733 .type = AMD_IP_BLOCK_TYPE_DCE,
1737 .funcs = &amdgpu_dm_funcs,
1747 static const struct drm_mode_config_funcs amdgpu_dm_mode_funcs = {
1748 .fb_create = amdgpu_display_user_framebuffer_create,
1749 .output_poll_changed = drm_fb_helper_output_poll_changed,
1750 .atomic_check = amdgpu_dm_atomic_check,
1751 .atomic_commit = amdgpu_dm_atomic_commit,
1754 static struct drm_mode_config_helper_funcs amdgpu_dm_mode_config_helperfuncs = {
1755 .atomic_commit_tail = amdgpu_dm_atomic_commit_tail
1759 amdgpu_dm_update_connector_after_detect(struct amdgpu_dm_connector *aconnector)
1761 struct drm_connector *connector = &aconnector->base;
1762 struct drm_device *dev = connector->dev;
1763 struct dc_sink *sink;
1765 /* MST handled by drm_mst framework */
1766 if (aconnector->mst_mgr.mst_state == true)
1770 sink = aconnector->dc_link->local_sink;
1772 dc_sink_retain(sink);
1775 * Edid mgmt connector gets first update only in mode_valid hook and then
1776 * the connector sink is set to either fake or physical sink depends on link status.
1777 * Skip if already done during boot.
1779 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED
1780 && aconnector->dc_em_sink) {
1783 * For S3 resume with headless use eml_sink to fake stream
1784 * because on resume connector->sink is set to NULL
1786 mutex_lock(&dev->mode_config.mutex);
1789 if (aconnector->dc_sink) {
1790 amdgpu_dm_update_freesync_caps(connector, NULL);
1792 * retain and release below are used to
1793 * bump up refcount for sink because the link doesn't point
1794 * to it anymore after disconnect, so on next crtc to connector
1795 * reshuffle by UMD we will get into unwanted dc_sink release
1797 dc_sink_release(aconnector->dc_sink);
1799 aconnector->dc_sink = sink;
1800 dc_sink_retain(aconnector->dc_sink);
1801 amdgpu_dm_update_freesync_caps(connector,
1804 amdgpu_dm_update_freesync_caps(connector, NULL);
1805 if (!aconnector->dc_sink) {
1806 aconnector->dc_sink = aconnector->dc_em_sink;
1807 dc_sink_retain(aconnector->dc_sink);
1811 mutex_unlock(&dev->mode_config.mutex);
1814 dc_sink_release(sink);
1819 * TODO: temporary guard to look for proper fix
1820 * if this sink is MST sink, we should not do anything
1822 if (sink && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
1823 dc_sink_release(sink);
1827 if (aconnector->dc_sink == sink) {
1829 * We got a DP short pulse (Link Loss, DP CTS, etc...).
1832 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: dc_sink didn't change.\n",
1833 aconnector->connector_id);
1835 dc_sink_release(sink);
1839 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: Old sink=%p New sink=%p\n",
1840 aconnector->connector_id, aconnector->dc_sink, sink);
1842 mutex_lock(&dev->mode_config.mutex);
1845 * 1. Update status of the drm connector
1846 * 2. Send an event and let userspace tell us what to do
1850 * TODO: check if we still need the S3 mode update workaround.
1851 * If yes, put it here.
1853 if (aconnector->dc_sink)
1854 amdgpu_dm_update_freesync_caps(connector, NULL);
1856 aconnector->dc_sink = sink;
1857 dc_sink_retain(aconnector->dc_sink);
1858 if (sink->dc_edid.length == 0) {
1859 aconnector->edid = NULL;
1860 drm_dp_cec_unset_edid(&aconnector->dm_dp_aux.aux);
1863 (struct edid *) sink->dc_edid.raw_edid;
1866 drm_connector_update_edid_property(connector,
1868 drm_dp_cec_set_edid(&aconnector->dm_dp_aux.aux,
1871 amdgpu_dm_update_freesync_caps(connector, aconnector->edid);
1874 drm_dp_cec_unset_edid(&aconnector->dm_dp_aux.aux);
1875 amdgpu_dm_update_freesync_caps(connector, NULL);
1876 drm_connector_update_edid_property(connector, NULL);
1877 aconnector->num_modes = 0;
1878 dc_sink_release(aconnector->dc_sink);
1879 aconnector->dc_sink = NULL;
1880 aconnector->edid = NULL;
1881 #ifdef CONFIG_DRM_AMD_DC_HDCP
1882 /* Set CP to DESIRED if it was ENABLED, so we can re-enable it again on hotplug */
1883 if (connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED)
1884 connector->state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
1888 mutex_unlock(&dev->mode_config.mutex);
1891 dc_sink_release(sink);
1894 static void handle_hpd_irq(void *param)
1896 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
1897 struct drm_connector *connector = &aconnector->base;
1898 struct drm_device *dev = connector->dev;
1899 enum dc_connection_type new_connection_type = dc_connection_none;
1900 #ifdef CONFIG_DRM_AMD_DC_HDCP
1901 struct amdgpu_device *adev = dev->dev_private;
1905 * In case of failure or MST no need to update connector status or notify the OS
1906 * since (for MST case) MST does this in its own context.
1908 mutex_lock(&aconnector->hpd_lock);
1910 #ifdef CONFIG_DRM_AMD_DC_HDCP
1911 if (adev->asic_type >= CHIP_RAVEN)
1912 hdcp_reset_display(adev->dm.hdcp_workqueue, aconnector->dc_link->link_index);
1914 if (aconnector->fake_enable)
1915 aconnector->fake_enable = false;
1917 if (!dc_link_detect_sink(aconnector->dc_link, &new_connection_type))
1918 DRM_ERROR("KMS: Failed to detect connector\n");
1920 if (aconnector->base.force && new_connection_type == dc_connection_none) {
1921 emulated_link_detect(aconnector->dc_link);
1924 drm_modeset_lock_all(dev);
1925 dm_restore_drm_connector_state(dev, connector);
1926 drm_modeset_unlock_all(dev);
1928 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
1929 drm_kms_helper_hotplug_event(dev);
1931 } else if (dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD)) {
1932 amdgpu_dm_update_connector_after_detect(aconnector);
1935 drm_modeset_lock_all(dev);
1936 dm_restore_drm_connector_state(dev, connector);
1937 drm_modeset_unlock_all(dev);
1939 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
1940 drm_kms_helper_hotplug_event(dev);
1942 mutex_unlock(&aconnector->hpd_lock);
1946 static void dm_handle_hpd_rx_irq(struct amdgpu_dm_connector *aconnector)
1948 uint8_t esi[DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI] = { 0 };
1950 bool new_irq_handled = false;
1952 int dpcd_bytes_to_read;
1954 const int max_process_count = 30;
1955 int process_count = 0;
1957 const struct dc_link_status *link_status = dc_link_get_status(aconnector->dc_link);
1959 if (link_status->dpcd_caps->dpcd_rev.raw < 0x12) {
1960 dpcd_bytes_to_read = DP_LANE0_1_STATUS - DP_SINK_COUNT;
1961 /* DPCD 0x200 - 0x201 for downstream IRQ */
1962 dpcd_addr = DP_SINK_COUNT;
1964 dpcd_bytes_to_read = DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI;
1965 /* DPCD 0x2002 - 0x2005 for downstream IRQ */
1966 dpcd_addr = DP_SINK_COUNT_ESI;
1969 dret = drm_dp_dpcd_read(
1970 &aconnector->dm_dp_aux.aux,
1973 dpcd_bytes_to_read);
1975 while (dret == dpcd_bytes_to_read &&
1976 process_count < max_process_count) {
1982 DRM_DEBUG_DRIVER("ESI %02x %02x %02x\n", esi[0], esi[1], esi[2]);
1983 /* handle HPD short pulse irq */
1984 if (aconnector->mst_mgr.mst_state)
1986 &aconnector->mst_mgr,
1990 if (new_irq_handled) {
1991 /* ACK at DPCD to notify down stream */
1992 const int ack_dpcd_bytes_to_write =
1993 dpcd_bytes_to_read - 1;
1995 for (retry = 0; retry < 3; retry++) {
1998 wret = drm_dp_dpcd_write(
1999 &aconnector->dm_dp_aux.aux,
2002 ack_dpcd_bytes_to_write);
2003 if (wret == ack_dpcd_bytes_to_write)
2007 /* check if there is new irq to be handled */
2008 dret = drm_dp_dpcd_read(
2009 &aconnector->dm_dp_aux.aux,
2012 dpcd_bytes_to_read);
2014 new_irq_handled = false;
2020 if (process_count == max_process_count)
2021 DRM_DEBUG_DRIVER("Loop exceeded max iterations\n");
2024 static void handle_hpd_rx_irq(void *param)
2026 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
2027 struct drm_connector *connector = &aconnector->base;
2028 struct drm_device *dev = connector->dev;
2029 struct dc_link *dc_link = aconnector->dc_link;
2030 bool is_mst_root_connector = aconnector->mst_mgr.mst_state;
2031 enum dc_connection_type new_connection_type = dc_connection_none;
2032 #ifdef CONFIG_DRM_AMD_DC_HDCP
2033 union hpd_irq_data hpd_irq_data;
2034 struct amdgpu_device *adev = dev->dev_private;
2036 memset(&hpd_irq_data, 0, sizeof(hpd_irq_data));
2040 * TODO:Temporary add mutex to protect hpd interrupt not have a gpio
2041 * conflict, after implement i2c helper, this mutex should be
2044 if (dc_link->type != dc_connection_mst_branch)
2045 mutex_lock(&aconnector->hpd_lock);
2048 #ifdef CONFIG_DRM_AMD_DC_HDCP
2049 if (dc_link_handle_hpd_rx_irq(dc_link, &hpd_irq_data, NULL) &&
2051 if (dc_link_handle_hpd_rx_irq(dc_link, NULL, NULL) &&
2053 !is_mst_root_connector) {
2054 /* Downstream Port status changed. */
2055 if (!dc_link_detect_sink(dc_link, &new_connection_type))
2056 DRM_ERROR("KMS: Failed to detect connector\n");
2058 if (aconnector->base.force && new_connection_type == dc_connection_none) {
2059 emulated_link_detect(dc_link);
2061 if (aconnector->fake_enable)
2062 aconnector->fake_enable = false;
2064 amdgpu_dm_update_connector_after_detect(aconnector);
2067 drm_modeset_lock_all(dev);
2068 dm_restore_drm_connector_state(dev, connector);
2069 drm_modeset_unlock_all(dev);
2071 drm_kms_helper_hotplug_event(dev);
2072 } else if (dc_link_detect(dc_link, DETECT_REASON_HPDRX)) {
2074 if (aconnector->fake_enable)
2075 aconnector->fake_enable = false;
2077 amdgpu_dm_update_connector_after_detect(aconnector);
2080 drm_modeset_lock_all(dev);
2081 dm_restore_drm_connector_state(dev, connector);
2082 drm_modeset_unlock_all(dev);
2084 drm_kms_helper_hotplug_event(dev);
2087 #ifdef CONFIG_DRM_AMD_DC_HDCP
2088 if (hpd_irq_data.bytes.device_service_irq.bits.CP_IRQ)
2089 hdcp_handle_cpirq(adev->dm.hdcp_workqueue, aconnector->base.index);
2091 if ((dc_link->cur_link_settings.lane_count != LANE_COUNT_UNKNOWN) ||
2092 (dc_link->type == dc_connection_mst_branch))
2093 dm_handle_hpd_rx_irq(aconnector);
2095 if (dc_link->type != dc_connection_mst_branch) {
2096 drm_dp_cec_irq(&aconnector->dm_dp_aux.aux);
2097 mutex_unlock(&aconnector->hpd_lock);
2101 static void register_hpd_handlers(struct amdgpu_device *adev)
2103 struct drm_device *dev = adev->ddev;
2104 struct drm_connector *connector;
2105 struct amdgpu_dm_connector *aconnector;
2106 const struct dc_link *dc_link;
2107 struct dc_interrupt_params int_params = {0};
2109 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
2110 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
2112 list_for_each_entry(connector,
2113 &dev->mode_config.connector_list, head) {
2115 aconnector = to_amdgpu_dm_connector(connector);
2116 dc_link = aconnector->dc_link;
2118 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd) {
2119 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
2120 int_params.irq_source = dc_link->irq_source_hpd;
2122 amdgpu_dm_irq_register_interrupt(adev, &int_params,
2124 (void *) aconnector);
2127 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd_rx) {
2129 /* Also register for DP short pulse (hpd_rx). */
2130 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
2131 int_params.irq_source = dc_link->irq_source_hpd_rx;
2133 amdgpu_dm_irq_register_interrupt(adev, &int_params,
2135 (void *) aconnector);
2140 /* Register IRQ sources and initialize IRQ callbacks */
2141 static int dce110_register_irq_handlers(struct amdgpu_device *adev)
2143 struct dc *dc = adev->dm.dc;
2144 struct common_irq_params *c_irq_params;
2145 struct dc_interrupt_params int_params = {0};
2148 unsigned client_id = AMDGPU_IRQ_CLIENTID_LEGACY;
2150 if (adev->asic_type >= CHIP_VEGA10)
2151 client_id = SOC15_IH_CLIENTID_DCE;
2153 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
2154 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
2157 * Actions of amdgpu_irq_add_id():
2158 * 1. Register a set() function with base driver.
2159 * Base driver will call set() function to enable/disable an
2160 * interrupt in DC hardware.
2161 * 2. Register amdgpu_dm_irq_handler().
2162 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
2163 * coming from DC hardware.
2164 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
2165 * for acknowledging and handling. */
2167 /* Use VBLANK interrupt */
2168 for (i = VISLANDS30_IV_SRCID_D1_VERTICAL_INTERRUPT0; i <= VISLANDS30_IV_SRCID_D6_VERTICAL_INTERRUPT0; i++) {
2169 r = amdgpu_irq_add_id(adev, client_id, i, &adev->crtc_irq);
2171 DRM_ERROR("Failed to add crtc irq id!\n");
2175 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
2176 int_params.irq_source =
2177 dc_interrupt_to_irq_source(dc, i, 0);
2179 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
2181 c_irq_params->adev = adev;
2182 c_irq_params->irq_src = int_params.irq_source;
2184 amdgpu_dm_irq_register_interrupt(adev, &int_params,
2185 dm_crtc_high_irq, c_irq_params);
2188 /* Use VUPDATE interrupt */
2189 for (i = VISLANDS30_IV_SRCID_D1_V_UPDATE_INT; i <= VISLANDS30_IV_SRCID_D6_V_UPDATE_INT; i += 2) {
2190 r = amdgpu_irq_add_id(adev, client_id, i, &adev->vupdate_irq);
2192 DRM_ERROR("Failed to add vupdate irq id!\n");
2196 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
2197 int_params.irq_source =
2198 dc_interrupt_to_irq_source(dc, i, 0);
2200 c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1];
2202 c_irq_params->adev = adev;
2203 c_irq_params->irq_src = int_params.irq_source;
2205 amdgpu_dm_irq_register_interrupt(adev, &int_params,
2206 dm_vupdate_high_irq, c_irq_params);
2209 /* Use GRPH_PFLIP interrupt */
2210 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
2211 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
2212 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq);
2214 DRM_ERROR("Failed to add page flip irq id!\n");
2218 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
2219 int_params.irq_source =
2220 dc_interrupt_to_irq_source(dc, i, 0);
2222 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
2224 c_irq_params->adev = adev;
2225 c_irq_params->irq_src = int_params.irq_source;
2227 amdgpu_dm_irq_register_interrupt(adev, &int_params,
2228 dm_pflip_high_irq, c_irq_params);
2233 r = amdgpu_irq_add_id(adev, client_id,
2234 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
2236 DRM_ERROR("Failed to add hpd irq id!\n");
2240 register_hpd_handlers(adev);
2245 #if defined(CONFIG_DRM_AMD_DC_DCN)
2246 /* Register IRQ sources and initialize IRQ callbacks */
2247 static int dcn10_register_irq_handlers(struct amdgpu_device *adev)
2249 struct dc *dc = adev->dm.dc;
2250 struct common_irq_params *c_irq_params;
2251 struct dc_interrupt_params int_params = {0};
2255 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
2256 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
2259 * Actions of amdgpu_irq_add_id():
2260 * 1. Register a set() function with base driver.
2261 * Base driver will call set() function to enable/disable an
2262 * interrupt in DC hardware.
2263 * 2. Register amdgpu_dm_irq_handler().
2264 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
2265 * coming from DC hardware.
2266 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
2267 * for acknowledging and handling.
2270 /* Use VSTARTUP interrupt */
2271 for (i = DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP;
2272 i <= DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP + adev->mode_info.num_crtc - 1;
2274 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->crtc_irq);
2277 DRM_ERROR("Failed to add crtc irq id!\n");
2281 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
2282 int_params.irq_source =
2283 dc_interrupt_to_irq_source(dc, i, 0);
2285 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
2287 c_irq_params->adev = adev;
2288 c_irq_params->irq_src = int_params.irq_source;
2290 amdgpu_dm_irq_register_interrupt(adev, &int_params,
2291 dm_dcn_crtc_high_irq, c_irq_params);
2294 /* Use GRPH_PFLIP interrupt */
2295 for (i = DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT;
2296 i <= DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT + adev->mode_info.num_crtc - 1;
2298 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->pageflip_irq);
2300 DRM_ERROR("Failed to add page flip irq id!\n");
2304 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
2305 int_params.irq_source =
2306 dc_interrupt_to_irq_source(dc, i, 0);
2308 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
2310 c_irq_params->adev = adev;
2311 c_irq_params->irq_src = int_params.irq_source;
2313 amdgpu_dm_irq_register_interrupt(adev, &int_params,
2314 dm_pflip_high_irq, c_irq_params);
2319 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DC_HPD1_INT,
2322 DRM_ERROR("Failed to add hpd irq id!\n");
2326 register_hpd_handlers(adev);
2333 * Acquires the lock for the atomic state object and returns
2334 * the new atomic state.
2336 * This should only be called during atomic check.
2338 static int dm_atomic_get_state(struct drm_atomic_state *state,
2339 struct dm_atomic_state **dm_state)
2341 struct drm_device *dev = state->dev;
2342 struct amdgpu_device *adev = dev->dev_private;
2343 struct amdgpu_display_manager *dm = &adev->dm;
2344 struct drm_private_state *priv_state;
2349 priv_state = drm_atomic_get_private_obj_state(state, &dm->atomic_obj);
2350 if (IS_ERR(priv_state))
2351 return PTR_ERR(priv_state);
2353 *dm_state = to_dm_atomic_state(priv_state);
2358 struct dm_atomic_state *
2359 dm_atomic_get_new_state(struct drm_atomic_state *state)
2361 struct drm_device *dev = state->dev;
2362 struct amdgpu_device *adev = dev->dev_private;
2363 struct amdgpu_display_manager *dm = &adev->dm;
2364 struct drm_private_obj *obj;
2365 struct drm_private_state *new_obj_state;
2368 for_each_new_private_obj_in_state(state, obj, new_obj_state, i) {
2369 if (obj->funcs == dm->atomic_obj.funcs)
2370 return to_dm_atomic_state(new_obj_state);
2376 struct dm_atomic_state *
2377 dm_atomic_get_old_state(struct drm_atomic_state *state)
2379 struct drm_device *dev = state->dev;
2380 struct amdgpu_device *adev = dev->dev_private;
2381 struct amdgpu_display_manager *dm = &adev->dm;
2382 struct drm_private_obj *obj;
2383 struct drm_private_state *old_obj_state;
2386 for_each_old_private_obj_in_state(state, obj, old_obj_state, i) {
2387 if (obj->funcs == dm->atomic_obj.funcs)
2388 return to_dm_atomic_state(old_obj_state);
2394 static struct drm_private_state *
2395 dm_atomic_duplicate_state(struct drm_private_obj *obj)
2397 struct dm_atomic_state *old_state, *new_state;
2399 new_state = kzalloc(sizeof(*new_state), GFP_KERNEL);
2403 __drm_atomic_helper_private_obj_duplicate_state(obj, &new_state->base);
2405 old_state = to_dm_atomic_state(obj->state);
2407 if (old_state && old_state->context)
2408 new_state->context = dc_copy_state(old_state->context);
2410 if (!new_state->context) {
2415 return &new_state->base;
2418 static void dm_atomic_destroy_state(struct drm_private_obj *obj,
2419 struct drm_private_state *state)
2421 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
2423 if (dm_state && dm_state->context)
2424 dc_release_state(dm_state->context);
2429 static struct drm_private_state_funcs dm_atomic_state_funcs = {
2430 .atomic_duplicate_state = dm_atomic_duplicate_state,
2431 .atomic_destroy_state = dm_atomic_destroy_state,
2434 static int amdgpu_dm_mode_config_init(struct amdgpu_device *adev)
2436 struct dm_atomic_state *state;
2439 adev->mode_info.mode_config_initialized = true;
2441 adev->ddev->mode_config.funcs = (void *)&amdgpu_dm_mode_funcs;
2442 adev->ddev->mode_config.helper_private = &amdgpu_dm_mode_config_helperfuncs;
2444 adev->ddev->mode_config.max_width = 16384;
2445 adev->ddev->mode_config.max_height = 16384;
2447 adev->ddev->mode_config.preferred_depth = 24;
2448 adev->ddev->mode_config.prefer_shadow = 1;
2449 /* indicates support for immediate flip */
2450 adev->ddev->mode_config.async_page_flip = true;
2452 adev->ddev->mode_config.fb_base = adev->gmc.aper_base;
2454 state = kzalloc(sizeof(*state), GFP_KERNEL);
2458 state->context = dc_create_state(adev->dm.dc);
2459 if (!state->context) {
2464 dc_resource_state_copy_construct_current(adev->dm.dc, state->context);
2466 drm_atomic_private_obj_init(adev->ddev,
2467 &adev->dm.atomic_obj,
2469 &dm_atomic_state_funcs);
2471 r = amdgpu_display_modeset_create_props(adev);
2475 r = amdgpu_dm_audio_init(adev);
2482 #define AMDGPU_DM_DEFAULT_MIN_BACKLIGHT 12
2483 #define AMDGPU_DM_DEFAULT_MAX_BACKLIGHT 255
2485 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
2486 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
2488 static void amdgpu_dm_update_backlight_caps(struct amdgpu_display_manager *dm)
2490 #if defined(CONFIG_ACPI)
2491 struct amdgpu_dm_backlight_caps caps;
2493 if (dm->backlight_caps.caps_valid)
2496 amdgpu_acpi_get_backlight_caps(dm->adev, &caps);
2497 if (caps.caps_valid) {
2498 dm->backlight_caps.min_input_signal = caps.min_input_signal;
2499 dm->backlight_caps.max_input_signal = caps.max_input_signal;
2500 dm->backlight_caps.caps_valid = true;
2502 dm->backlight_caps.min_input_signal =
2503 AMDGPU_DM_DEFAULT_MIN_BACKLIGHT;
2504 dm->backlight_caps.max_input_signal =
2505 AMDGPU_DM_DEFAULT_MAX_BACKLIGHT;
2508 dm->backlight_caps.min_input_signal = AMDGPU_DM_DEFAULT_MIN_BACKLIGHT;
2509 dm->backlight_caps.max_input_signal = AMDGPU_DM_DEFAULT_MAX_BACKLIGHT;
2513 static int amdgpu_dm_backlight_update_status(struct backlight_device *bd)
2515 struct amdgpu_display_manager *dm = bl_get_data(bd);
2516 struct amdgpu_dm_backlight_caps caps;
2517 uint32_t brightness = bd->props.brightness;
2519 amdgpu_dm_update_backlight_caps(dm);
2520 caps = dm->backlight_caps;
2522 * The brightness input is in the range 0-255
2523 * It needs to be rescaled to be between the
2524 * requested min and max input signal
2526 * It also needs to be scaled up by 0x101 to
2527 * match the DC interface which has a range of
2533 * (caps.max_input_signal - caps.min_input_signal)
2534 / AMDGPU_MAX_BL_LEVEL
2535 + caps.min_input_signal * 0x101;
2537 if (dc_link_set_backlight_level(dm->backlight_link,
2544 static int amdgpu_dm_backlight_get_brightness(struct backlight_device *bd)
2546 struct amdgpu_display_manager *dm = bl_get_data(bd);
2547 int ret = dc_link_get_backlight_level(dm->backlight_link);
2549 if (ret == DC_ERROR_UNEXPECTED)
2550 return bd->props.brightness;
2554 static const struct backlight_ops amdgpu_dm_backlight_ops = {
2555 .options = BL_CORE_SUSPENDRESUME,
2556 .get_brightness = amdgpu_dm_backlight_get_brightness,
2557 .update_status = amdgpu_dm_backlight_update_status,
2561 amdgpu_dm_register_backlight_device(struct amdgpu_display_manager *dm)
2564 struct backlight_properties props = { 0 };
2566 amdgpu_dm_update_backlight_caps(dm);
2568 props.max_brightness = AMDGPU_MAX_BL_LEVEL;
2569 props.brightness = AMDGPU_MAX_BL_LEVEL;
2570 props.type = BACKLIGHT_RAW;
2572 snprintf(bl_name, sizeof(bl_name), "amdgpu_bl%d",
2573 dm->adev->ddev->primary->index);
2575 dm->backlight_dev = backlight_device_register(bl_name,
2576 dm->adev->ddev->dev,
2578 &amdgpu_dm_backlight_ops,
2581 if (IS_ERR(dm->backlight_dev))
2582 DRM_ERROR("DM: Backlight registration failed!\n");
2584 DRM_DEBUG_DRIVER("DM: Registered Backlight device: %s\n", bl_name);
2589 static int initialize_plane(struct amdgpu_display_manager *dm,
2590 struct amdgpu_mode_info *mode_info, int plane_id,
2591 enum drm_plane_type plane_type,
2592 const struct dc_plane_cap *plane_cap)
2594 struct drm_plane *plane;
2595 unsigned long possible_crtcs;
2598 plane = kzalloc(sizeof(struct drm_plane), GFP_KERNEL);
2600 DRM_ERROR("KMS: Failed to allocate plane\n");
2603 plane->type = plane_type;
2606 * HACK: IGT tests expect that the primary plane for a CRTC
2607 * can only have one possible CRTC. Only expose support for
2608 * any CRTC if they're not going to be used as a primary plane
2609 * for a CRTC - like overlay or underlay planes.
2611 possible_crtcs = 1 << plane_id;
2612 if (plane_id >= dm->dc->caps.max_streams)
2613 possible_crtcs = 0xff;
2615 ret = amdgpu_dm_plane_init(dm, plane, possible_crtcs, plane_cap);
2618 DRM_ERROR("KMS: Failed to initialize plane\n");
2624 mode_info->planes[plane_id] = plane;
2630 static void register_backlight_device(struct amdgpu_display_manager *dm,
2631 struct dc_link *link)
2633 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
2634 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
2636 if ((link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) &&
2637 link->type != dc_connection_none) {
2639 * Event if registration failed, we should continue with
2640 * DM initialization because not having a backlight control
2641 * is better then a black screen.
2643 amdgpu_dm_register_backlight_device(dm);
2645 if (dm->backlight_dev)
2646 dm->backlight_link = link;
2653 * In this architecture, the association
2654 * connector -> encoder -> crtc
2655 * id not really requried. The crtc and connector will hold the
2656 * display_index as an abstraction to use with DAL component
2658 * Returns 0 on success
2660 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev)
2662 struct amdgpu_display_manager *dm = &adev->dm;
2664 struct amdgpu_dm_connector *aconnector = NULL;
2665 struct amdgpu_encoder *aencoder = NULL;
2666 struct amdgpu_mode_info *mode_info = &adev->mode_info;
2668 int32_t primary_planes;
2669 enum dc_connection_type new_connection_type = dc_connection_none;
2670 const struct dc_plane_cap *plane;
2672 link_cnt = dm->dc->caps.max_links;
2673 if (amdgpu_dm_mode_config_init(dm->adev)) {
2674 DRM_ERROR("DM: Failed to initialize mode config\n");
2678 /* There is one primary plane per CRTC */
2679 primary_planes = dm->dc->caps.max_streams;
2680 ASSERT(primary_planes <= AMDGPU_MAX_PLANES);
2683 * Initialize primary planes, implicit planes for legacy IOCTLS.
2684 * Order is reversed to match iteration order in atomic check.
2686 for (i = (primary_planes - 1); i >= 0; i--) {
2687 plane = &dm->dc->caps.planes[i];
2689 if (initialize_plane(dm, mode_info, i,
2690 DRM_PLANE_TYPE_PRIMARY, plane)) {
2691 DRM_ERROR("KMS: Failed to initialize primary plane\n");
2697 * Initialize overlay planes, index starting after primary planes.
2698 * These planes have a higher DRM index than the primary planes since
2699 * they should be considered as having a higher z-order.
2700 * Order is reversed to match iteration order in atomic check.
2702 * Only support DCN for now, and only expose one so we don't encourage
2703 * userspace to use up all the pipes.
2705 for (i = 0; i < dm->dc->caps.max_planes; ++i) {
2706 struct dc_plane_cap *plane = &dm->dc->caps.planes[i];
2708 if (plane->type != DC_PLANE_TYPE_DCN_UNIVERSAL)
2711 if (!plane->blends_with_above || !plane->blends_with_below)
2714 if (!plane->pixel_format_support.argb8888)
2717 if (initialize_plane(dm, NULL, primary_planes + i,
2718 DRM_PLANE_TYPE_OVERLAY, plane)) {
2719 DRM_ERROR("KMS: Failed to initialize overlay plane\n");
2723 /* Only create one overlay plane. */
2727 for (i = 0; i < dm->dc->caps.max_streams; i++)
2728 if (amdgpu_dm_crtc_init(dm, mode_info->planes[i], i)) {
2729 DRM_ERROR("KMS: Failed to initialize crtc\n");
2733 dm->display_indexes_num = dm->dc->caps.max_streams;
2735 /* loops over all connectors on the board */
2736 for (i = 0; i < link_cnt; i++) {
2737 struct dc_link *link = NULL;
2739 if (i > AMDGPU_DM_MAX_DISPLAY_INDEX) {
2741 "KMS: Cannot support more than %d display indexes\n",
2742 AMDGPU_DM_MAX_DISPLAY_INDEX);
2746 aconnector = kzalloc(sizeof(*aconnector), GFP_KERNEL);
2750 aencoder = kzalloc(sizeof(*aencoder), GFP_KERNEL);
2754 if (amdgpu_dm_encoder_init(dm->ddev, aencoder, i)) {
2755 DRM_ERROR("KMS: Failed to initialize encoder\n");
2759 if (amdgpu_dm_connector_init(dm, aconnector, i, aencoder)) {
2760 DRM_ERROR("KMS: Failed to initialize connector\n");
2764 link = dc_get_link_at_index(dm->dc, i);
2766 if (!dc_link_detect_sink(link, &new_connection_type))
2767 DRM_ERROR("KMS: Failed to detect connector\n");
2769 if (aconnector->base.force && new_connection_type == dc_connection_none) {
2770 emulated_link_detect(link);
2771 amdgpu_dm_update_connector_after_detect(aconnector);
2773 } else if (dc_link_detect(link, DETECT_REASON_BOOT)) {
2774 amdgpu_dm_update_connector_after_detect(aconnector);
2775 register_backlight_device(dm, link);
2776 if (amdgpu_dc_feature_mask & DC_PSR_MASK)
2777 amdgpu_dm_set_psr_caps(link);
2783 /* Software is initialized. Now we can register interrupt handlers. */
2784 switch (adev->asic_type) {
2794 case CHIP_POLARIS11:
2795 case CHIP_POLARIS10:
2796 case CHIP_POLARIS12:
2801 if (dce110_register_irq_handlers(dm->adev)) {
2802 DRM_ERROR("DM: Failed to initialize IRQ\n");
2806 #if defined(CONFIG_DRM_AMD_DC_DCN)
2812 if (dcn10_register_irq_handlers(dm->adev)) {
2813 DRM_ERROR("DM: Failed to initialize IRQ\n");
2819 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type);
2823 if (adev->asic_type != CHIP_CARRIZO && adev->asic_type != CHIP_STONEY)
2824 dm->dc->debug.disable_stutter = amdgpu_pp_feature_mask & PP_STUTTER_MODE ? false : true;
2834 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm)
2836 drm_mode_config_cleanup(dm->ddev);
2837 drm_atomic_private_obj_fini(&dm->atomic_obj);
2841 /******************************************************************************
2842 * amdgpu_display_funcs functions
2843 *****************************************************************************/
2846 * dm_bandwidth_update - program display watermarks
2848 * @adev: amdgpu_device pointer
2850 * Calculate and program the display watermarks and line buffer allocation.
2852 static void dm_bandwidth_update(struct amdgpu_device *adev)
2854 /* TODO: implement later */
2857 static const struct amdgpu_display_funcs dm_display_funcs = {
2858 .bandwidth_update = dm_bandwidth_update, /* called unconditionally */
2859 .vblank_get_counter = dm_vblank_get_counter,/* called unconditionally */
2860 .backlight_set_level = NULL, /* never called for DC */
2861 .backlight_get_level = NULL, /* never called for DC */
2862 .hpd_sense = NULL,/* called unconditionally */
2863 .hpd_set_polarity = NULL, /* called unconditionally */
2864 .hpd_get_gpio_reg = NULL, /* VBIOS parsing. DAL does it. */
2865 .page_flip_get_scanoutpos =
2866 dm_crtc_get_scanoutpos,/* called unconditionally */
2867 .add_encoder = NULL, /* VBIOS parsing. DAL does it. */
2868 .add_connector = NULL, /* VBIOS parsing. DAL does it. */
2871 #if defined(CONFIG_DEBUG_KERNEL_DC)
2873 static ssize_t s3_debug_store(struct device *device,
2874 struct device_attribute *attr,
2880 struct drm_device *drm_dev = dev_get_drvdata(device);
2881 struct amdgpu_device *adev = drm_dev->dev_private;
2883 ret = kstrtoint(buf, 0, &s3_state);
2888 drm_kms_helper_hotplug_event(adev->ddev);
2893 return ret == 0 ? count : 0;
2896 DEVICE_ATTR_WO(s3_debug);
2900 static int dm_early_init(void *handle)
2902 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2904 switch (adev->asic_type) {
2907 adev->mode_info.num_crtc = 6;
2908 adev->mode_info.num_hpd = 6;
2909 adev->mode_info.num_dig = 6;
2912 adev->mode_info.num_crtc = 4;
2913 adev->mode_info.num_hpd = 6;
2914 adev->mode_info.num_dig = 7;
2918 adev->mode_info.num_crtc = 2;
2919 adev->mode_info.num_hpd = 6;
2920 adev->mode_info.num_dig = 6;
2924 adev->mode_info.num_crtc = 6;
2925 adev->mode_info.num_hpd = 6;
2926 adev->mode_info.num_dig = 7;
2929 adev->mode_info.num_crtc = 3;
2930 adev->mode_info.num_hpd = 6;
2931 adev->mode_info.num_dig = 9;
2934 adev->mode_info.num_crtc = 2;
2935 adev->mode_info.num_hpd = 6;
2936 adev->mode_info.num_dig = 9;
2938 case CHIP_POLARIS11:
2939 case CHIP_POLARIS12:
2940 adev->mode_info.num_crtc = 5;
2941 adev->mode_info.num_hpd = 5;
2942 adev->mode_info.num_dig = 5;
2944 case CHIP_POLARIS10:
2946 adev->mode_info.num_crtc = 6;
2947 adev->mode_info.num_hpd = 6;
2948 adev->mode_info.num_dig = 6;
2953 adev->mode_info.num_crtc = 6;
2954 adev->mode_info.num_hpd = 6;
2955 adev->mode_info.num_dig = 6;
2957 #if defined(CONFIG_DRM_AMD_DC_DCN)
2959 adev->mode_info.num_crtc = 4;
2960 adev->mode_info.num_hpd = 4;
2961 adev->mode_info.num_dig = 4;
2966 adev->mode_info.num_crtc = 6;
2967 adev->mode_info.num_hpd = 6;
2968 adev->mode_info.num_dig = 6;
2971 adev->mode_info.num_crtc = 5;
2972 adev->mode_info.num_hpd = 5;
2973 adev->mode_info.num_dig = 5;
2976 adev->mode_info.num_crtc = 4;
2977 adev->mode_info.num_hpd = 4;
2978 adev->mode_info.num_dig = 4;
2981 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type);
2985 amdgpu_dm_set_irq_funcs(adev);
2987 if (adev->mode_info.funcs == NULL)
2988 adev->mode_info.funcs = &dm_display_funcs;
2991 * Note: Do NOT change adev->audio_endpt_rreg and
2992 * adev->audio_endpt_wreg because they are initialised in
2993 * amdgpu_device_init()
2995 #if defined(CONFIG_DEBUG_KERNEL_DC)
2998 &dev_attr_s3_debug);
3004 static bool modeset_required(struct drm_crtc_state *crtc_state,
3005 struct dc_stream_state *new_stream,
3006 struct dc_stream_state *old_stream)
3008 if (!drm_atomic_crtc_needs_modeset(crtc_state))
3011 if (!crtc_state->enable)
3014 return crtc_state->active;
3017 static bool modereset_required(struct drm_crtc_state *crtc_state)
3019 if (!drm_atomic_crtc_needs_modeset(crtc_state))
3022 return !crtc_state->enable || !crtc_state->active;
3025 static void amdgpu_dm_encoder_destroy(struct drm_encoder *encoder)
3027 drm_encoder_cleanup(encoder);
3031 static const struct drm_encoder_funcs amdgpu_dm_encoder_funcs = {
3032 .destroy = amdgpu_dm_encoder_destroy,
3036 static int fill_dc_scaling_info(const struct drm_plane_state *state,
3037 struct dc_scaling_info *scaling_info)
3039 int scale_w, scale_h;
3041 memset(scaling_info, 0, sizeof(*scaling_info));
3043 /* Source is fixed 16.16 but we ignore mantissa for now... */
3044 scaling_info->src_rect.x = state->src_x >> 16;
3045 scaling_info->src_rect.y = state->src_y >> 16;
3047 scaling_info->src_rect.width = state->src_w >> 16;
3048 if (scaling_info->src_rect.width == 0)
3051 scaling_info->src_rect.height = state->src_h >> 16;
3052 if (scaling_info->src_rect.height == 0)
3055 scaling_info->dst_rect.x = state->crtc_x;
3056 scaling_info->dst_rect.y = state->crtc_y;
3058 if (state->crtc_w == 0)
3061 scaling_info->dst_rect.width = state->crtc_w;
3063 if (state->crtc_h == 0)
3066 scaling_info->dst_rect.height = state->crtc_h;
3068 /* DRM doesn't specify clipping on destination output. */
3069 scaling_info->clip_rect = scaling_info->dst_rect;
3071 /* TODO: Validate scaling per-format with DC plane caps */
3072 scale_w = scaling_info->dst_rect.width * 1000 /
3073 scaling_info->src_rect.width;
3075 if (scale_w < 250 || scale_w > 16000)
3078 scale_h = scaling_info->dst_rect.height * 1000 /
3079 scaling_info->src_rect.height;
3081 if (scale_h < 250 || scale_h > 16000)
3085 * The "scaling_quality" can be ignored for now, quality = 0 has DC
3086 * assume reasonable defaults based on the format.
3092 static int get_fb_info(const struct amdgpu_framebuffer *amdgpu_fb,
3093 uint64_t *tiling_flags)
3095 struct amdgpu_bo *rbo = gem_to_amdgpu_bo(amdgpu_fb->base.obj[0]);
3096 int r = amdgpu_bo_reserve(rbo, false);
3099 /* Don't show error message when returning -ERESTARTSYS */
3100 if (r != -ERESTARTSYS)
3101 DRM_ERROR("Unable to reserve buffer: %d\n", r);
3106 amdgpu_bo_get_tiling_flags(rbo, tiling_flags);
3108 amdgpu_bo_unreserve(rbo);
3113 static inline uint64_t get_dcc_address(uint64_t address, uint64_t tiling_flags)
3115 uint32_t offset = AMDGPU_TILING_GET(tiling_flags, DCC_OFFSET_256B);
3117 return offset ? (address + offset * 256) : 0;
3121 fill_plane_dcc_attributes(struct amdgpu_device *adev,
3122 const struct amdgpu_framebuffer *afb,
3123 const enum surface_pixel_format format,
3124 const enum dc_rotation_angle rotation,
3125 const struct plane_size *plane_size,
3126 const union dc_tiling_info *tiling_info,
3127 const uint64_t info,
3128 struct dc_plane_dcc_param *dcc,
3129 struct dc_plane_address *address)
3131 struct dc *dc = adev->dm.dc;
3132 struct dc_dcc_surface_param input;
3133 struct dc_surface_dcc_cap output;
3134 uint32_t offset = AMDGPU_TILING_GET(info, DCC_OFFSET_256B);
3135 uint32_t i64b = AMDGPU_TILING_GET(info, DCC_INDEPENDENT_64B) != 0;
3136 uint64_t dcc_address;
3138 memset(&input, 0, sizeof(input));
3139 memset(&output, 0, sizeof(output));
3144 if (format >= SURFACE_PIXEL_FORMAT_VIDEO_BEGIN)
3147 if (!dc->cap_funcs.get_dcc_compression_cap)
3150 input.format = format;
3151 input.surface_size.width = plane_size->surface_size.width;
3152 input.surface_size.height = plane_size->surface_size.height;
3153 input.swizzle_mode = tiling_info->gfx9.swizzle;
3155 if (rotation == ROTATION_ANGLE_0 || rotation == ROTATION_ANGLE_180)
3156 input.scan = SCAN_DIRECTION_HORIZONTAL;
3157 else if (rotation == ROTATION_ANGLE_90 || rotation == ROTATION_ANGLE_270)
3158 input.scan = SCAN_DIRECTION_VERTICAL;
3160 if (!dc->cap_funcs.get_dcc_compression_cap(dc, &input, &output))
3163 if (!output.capable)
3166 if (i64b == 0 && output.grph.rgb.independent_64b_blks != 0)
3171 AMDGPU_TILING_GET(info, DCC_PITCH_MAX) + 1;
3172 dcc->independent_64b_blks = i64b;
3174 dcc_address = get_dcc_address(afb->address, info);
3175 address->grph.meta_addr.low_part = lower_32_bits(dcc_address);
3176 address->grph.meta_addr.high_part = upper_32_bits(dcc_address);
3182 fill_plane_buffer_attributes(struct amdgpu_device *adev,
3183 const struct amdgpu_framebuffer *afb,
3184 const enum surface_pixel_format format,
3185 const enum dc_rotation_angle rotation,
3186 const uint64_t tiling_flags,
3187 union dc_tiling_info *tiling_info,
3188 struct plane_size *plane_size,
3189 struct dc_plane_dcc_param *dcc,
3190 struct dc_plane_address *address)
3192 const struct drm_framebuffer *fb = &afb->base;
3195 memset(tiling_info, 0, sizeof(*tiling_info));
3196 memset(plane_size, 0, sizeof(*plane_size));
3197 memset(dcc, 0, sizeof(*dcc));
3198 memset(address, 0, sizeof(*address));
3200 if (format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) {
3201 plane_size->surface_size.x = 0;
3202 plane_size->surface_size.y = 0;
3203 plane_size->surface_size.width = fb->width;
3204 plane_size->surface_size.height = fb->height;
3205 plane_size->surface_pitch =
3206 fb->pitches[0] / fb->format->cpp[0];
3208 address->type = PLN_ADDR_TYPE_GRAPHICS;
3209 address->grph.addr.low_part = lower_32_bits(afb->address);
3210 address->grph.addr.high_part = upper_32_bits(afb->address);
3211 } else if (format < SURFACE_PIXEL_FORMAT_INVALID) {
3212 uint64_t chroma_addr = afb->address + fb->offsets[1];
3214 plane_size->surface_size.x = 0;
3215 plane_size->surface_size.y = 0;
3216 plane_size->surface_size.width = fb->width;
3217 plane_size->surface_size.height = fb->height;
3218 plane_size->surface_pitch =
3219 fb->pitches[0] / fb->format->cpp[0];
3221 plane_size->chroma_size.x = 0;
3222 plane_size->chroma_size.y = 0;
3223 /* TODO: set these based on surface format */
3224 plane_size->chroma_size.width = fb->width / 2;
3225 plane_size->chroma_size.height = fb->height / 2;
3227 plane_size->chroma_pitch =
3228 fb->pitches[1] / fb->format->cpp[1];
3230 address->type = PLN_ADDR_TYPE_VIDEO_PROGRESSIVE;
3231 address->video_progressive.luma_addr.low_part =
3232 lower_32_bits(afb->address);
3233 address->video_progressive.luma_addr.high_part =
3234 upper_32_bits(afb->address);
3235 address->video_progressive.chroma_addr.low_part =
3236 lower_32_bits(chroma_addr);
3237 address->video_progressive.chroma_addr.high_part =
3238 upper_32_bits(chroma_addr);
3241 /* Fill GFX8 params */
3242 if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == DC_ARRAY_2D_TILED_THIN1) {
3243 unsigned int bankw, bankh, mtaspect, tile_split, num_banks;
3245 bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
3246 bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
3247 mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
3248 tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT);
3249 num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
3251 /* XXX fix me for VI */
3252 tiling_info->gfx8.num_banks = num_banks;
3253 tiling_info->gfx8.array_mode =
3254 DC_ARRAY_2D_TILED_THIN1;
3255 tiling_info->gfx8.tile_split = tile_split;
3256 tiling_info->gfx8.bank_width = bankw;
3257 tiling_info->gfx8.bank_height = bankh;
3258 tiling_info->gfx8.tile_aspect = mtaspect;
3259 tiling_info->gfx8.tile_mode =
3260 DC_ADDR_SURF_MICRO_TILING_DISPLAY;
3261 } else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE)
3262 == DC_ARRAY_1D_TILED_THIN1) {
3263 tiling_info->gfx8.array_mode = DC_ARRAY_1D_TILED_THIN1;
3266 tiling_info->gfx8.pipe_config =
3267 AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
3269 if (adev->asic_type == CHIP_VEGA10 ||
3270 adev->asic_type == CHIP_VEGA12 ||
3271 adev->asic_type == CHIP_VEGA20 ||
3272 adev->asic_type == CHIP_NAVI10 ||
3273 adev->asic_type == CHIP_NAVI14 ||
3274 adev->asic_type == CHIP_NAVI12 ||
3275 adev->asic_type == CHIP_RENOIR ||
3276 adev->asic_type == CHIP_RAVEN) {
3277 /* Fill GFX9 params */
3278 tiling_info->gfx9.num_pipes =
3279 adev->gfx.config.gb_addr_config_fields.num_pipes;
3280 tiling_info->gfx9.num_banks =
3281 adev->gfx.config.gb_addr_config_fields.num_banks;
3282 tiling_info->gfx9.pipe_interleave =
3283 adev->gfx.config.gb_addr_config_fields.pipe_interleave_size;
3284 tiling_info->gfx9.num_shader_engines =
3285 adev->gfx.config.gb_addr_config_fields.num_se;
3286 tiling_info->gfx9.max_compressed_frags =
3287 adev->gfx.config.gb_addr_config_fields.max_compress_frags;
3288 tiling_info->gfx9.num_rb_per_se =
3289 adev->gfx.config.gb_addr_config_fields.num_rb_per_se;
3290 tiling_info->gfx9.swizzle =
3291 AMDGPU_TILING_GET(tiling_flags, SWIZZLE_MODE);
3292 tiling_info->gfx9.shaderEnable = 1;
3294 ret = fill_plane_dcc_attributes(adev, afb, format, rotation,
3295 plane_size, tiling_info,
3296 tiling_flags, dcc, address);
3305 fill_blending_from_plane_state(const struct drm_plane_state *plane_state,
3306 bool *per_pixel_alpha, bool *global_alpha,
3307 int *global_alpha_value)
3309 *per_pixel_alpha = false;
3310 *global_alpha = false;
3311 *global_alpha_value = 0xff;
3313 if (plane_state->plane->type != DRM_PLANE_TYPE_OVERLAY)
3316 if (plane_state->pixel_blend_mode == DRM_MODE_BLEND_PREMULTI) {
3317 static const uint32_t alpha_formats[] = {
3318 DRM_FORMAT_ARGB8888,
3319 DRM_FORMAT_RGBA8888,
3320 DRM_FORMAT_ABGR8888,
3322 uint32_t format = plane_state->fb->format->format;
3325 for (i = 0; i < ARRAY_SIZE(alpha_formats); ++i) {
3326 if (format == alpha_formats[i]) {
3327 *per_pixel_alpha = true;
3333 if (plane_state->alpha < 0xffff) {
3334 *global_alpha = true;
3335 *global_alpha_value = plane_state->alpha >> 8;
3340 fill_plane_color_attributes(const struct drm_plane_state *plane_state,
3341 const enum surface_pixel_format format,
3342 enum dc_color_space *color_space)
3346 *color_space = COLOR_SPACE_SRGB;
3348 /* DRM color properties only affect non-RGB formats. */
3349 if (format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN)
3352 full_range = (plane_state->color_range == DRM_COLOR_YCBCR_FULL_RANGE);
3354 switch (plane_state->color_encoding) {
3355 case DRM_COLOR_YCBCR_BT601:
3357 *color_space = COLOR_SPACE_YCBCR601;
3359 *color_space = COLOR_SPACE_YCBCR601_LIMITED;
3362 case DRM_COLOR_YCBCR_BT709:
3364 *color_space = COLOR_SPACE_YCBCR709;
3366 *color_space = COLOR_SPACE_YCBCR709_LIMITED;
3369 case DRM_COLOR_YCBCR_BT2020:
3371 *color_space = COLOR_SPACE_2020_YCBCR;
3384 fill_dc_plane_info_and_addr(struct amdgpu_device *adev,
3385 const struct drm_plane_state *plane_state,
3386 const uint64_t tiling_flags,
3387 struct dc_plane_info *plane_info,
3388 struct dc_plane_address *address)
3390 const struct drm_framebuffer *fb = plane_state->fb;
3391 const struct amdgpu_framebuffer *afb =
3392 to_amdgpu_framebuffer(plane_state->fb);
3393 struct drm_format_name_buf format_name;
3396 memset(plane_info, 0, sizeof(*plane_info));
3398 switch (fb->format->format) {
3400 plane_info->format =
3401 SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS;
3403 case DRM_FORMAT_RGB565:
3404 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_RGB565;
3406 case DRM_FORMAT_XRGB8888:
3407 case DRM_FORMAT_ARGB8888:
3408 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888;
3410 case DRM_FORMAT_XRGB2101010:
3411 case DRM_FORMAT_ARGB2101010:
3412 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010;
3414 case DRM_FORMAT_XBGR2101010:
3415 case DRM_FORMAT_ABGR2101010:
3416 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010;
3418 case DRM_FORMAT_XBGR8888:
3419 case DRM_FORMAT_ABGR8888:
3420 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR8888;
3422 case DRM_FORMAT_NV21:
3423 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr;
3425 case DRM_FORMAT_NV12:
3426 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb;
3430 "Unsupported screen format %s\n",
3431 drm_get_format_name(fb->format->format, &format_name));
3435 switch (plane_state->rotation & DRM_MODE_ROTATE_MASK) {
3436 case DRM_MODE_ROTATE_0:
3437 plane_info->rotation = ROTATION_ANGLE_0;
3439 case DRM_MODE_ROTATE_90:
3440 plane_info->rotation = ROTATION_ANGLE_90;
3442 case DRM_MODE_ROTATE_180:
3443 plane_info->rotation = ROTATION_ANGLE_180;
3445 case DRM_MODE_ROTATE_270:
3446 plane_info->rotation = ROTATION_ANGLE_270;
3449 plane_info->rotation = ROTATION_ANGLE_0;
3453 plane_info->visible = true;
3454 plane_info->stereo_format = PLANE_STEREO_FORMAT_NONE;
3456 plane_info->layer_index = 0;
3458 ret = fill_plane_color_attributes(plane_state, plane_info->format,
3459 &plane_info->color_space);
3463 ret = fill_plane_buffer_attributes(adev, afb, plane_info->format,
3464 plane_info->rotation, tiling_flags,
3465 &plane_info->tiling_info,
3466 &plane_info->plane_size,
3467 &plane_info->dcc, address);
3471 fill_blending_from_plane_state(
3472 plane_state, &plane_info->per_pixel_alpha,
3473 &plane_info->global_alpha, &plane_info->global_alpha_value);
3478 static int fill_dc_plane_attributes(struct amdgpu_device *adev,
3479 struct dc_plane_state *dc_plane_state,
3480 struct drm_plane_state *plane_state,
3481 struct drm_crtc_state *crtc_state)
3483 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state);
3484 const struct amdgpu_framebuffer *amdgpu_fb =
3485 to_amdgpu_framebuffer(plane_state->fb);
3486 struct dc_scaling_info scaling_info;
3487 struct dc_plane_info plane_info;
3488 uint64_t tiling_flags;
3491 ret = fill_dc_scaling_info(plane_state, &scaling_info);
3495 dc_plane_state->src_rect = scaling_info.src_rect;
3496 dc_plane_state->dst_rect = scaling_info.dst_rect;
3497 dc_plane_state->clip_rect = scaling_info.clip_rect;
3498 dc_plane_state->scaling_quality = scaling_info.scaling_quality;
3500 ret = get_fb_info(amdgpu_fb, &tiling_flags);
3504 ret = fill_dc_plane_info_and_addr(adev, plane_state, tiling_flags,
3506 &dc_plane_state->address);
3510 dc_plane_state->format = plane_info.format;
3511 dc_plane_state->color_space = plane_info.color_space;
3512 dc_plane_state->format = plane_info.format;
3513 dc_plane_state->plane_size = plane_info.plane_size;
3514 dc_plane_state->rotation = plane_info.rotation;
3515 dc_plane_state->horizontal_mirror = plane_info.horizontal_mirror;
3516 dc_plane_state->stereo_format = plane_info.stereo_format;
3517 dc_plane_state->tiling_info = plane_info.tiling_info;
3518 dc_plane_state->visible = plane_info.visible;
3519 dc_plane_state->per_pixel_alpha = plane_info.per_pixel_alpha;
3520 dc_plane_state->global_alpha = plane_info.global_alpha;
3521 dc_plane_state->global_alpha_value = plane_info.global_alpha_value;
3522 dc_plane_state->dcc = plane_info.dcc;
3523 dc_plane_state->layer_index = plane_info.layer_index; // Always returns 0
3526 * Always set input transfer function, since plane state is refreshed
3529 ret = amdgpu_dm_update_plane_color_mgmt(dm_crtc_state, dc_plane_state);
3536 static void update_stream_scaling_settings(const struct drm_display_mode *mode,
3537 const struct dm_connector_state *dm_state,
3538 struct dc_stream_state *stream)
3540 enum amdgpu_rmx_type rmx_type;
3542 struct rect src = { 0 }; /* viewport in composition space*/
3543 struct rect dst = { 0 }; /* stream addressable area */
3545 /* no mode. nothing to be done */
3549 /* Full screen scaling by default */
3550 src.width = mode->hdisplay;
3551 src.height = mode->vdisplay;
3552 dst.width = stream->timing.h_addressable;
3553 dst.height = stream->timing.v_addressable;
3556 rmx_type = dm_state->scaling;
3557 if (rmx_type == RMX_ASPECT || rmx_type == RMX_OFF) {
3558 if (src.width * dst.height <
3559 src.height * dst.width) {
3560 /* height needs less upscaling/more downscaling */
3561 dst.width = src.width *
3562 dst.height / src.height;
3564 /* width needs less upscaling/more downscaling */
3565 dst.height = src.height *
3566 dst.width / src.width;
3568 } else if (rmx_type == RMX_CENTER) {
3572 dst.x = (stream->timing.h_addressable - dst.width) / 2;
3573 dst.y = (stream->timing.v_addressable - dst.height) / 2;
3575 if (dm_state->underscan_enable) {
3576 dst.x += dm_state->underscan_hborder / 2;
3577 dst.y += dm_state->underscan_vborder / 2;
3578 dst.width -= dm_state->underscan_hborder;
3579 dst.height -= dm_state->underscan_vborder;
3586 DRM_DEBUG_DRIVER("Destination Rectangle x:%d y:%d width:%d height:%d\n",
3587 dst.x, dst.y, dst.width, dst.height);
3591 static enum dc_color_depth
3592 convert_color_depth_from_display_info(const struct drm_connector *connector,
3593 const struct drm_connector_state *state,
3601 /* Cap display bpc based on HDMI 2.0 HF-VSDB */
3602 if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_48)
3604 else if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_36)
3606 else if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_30)
3609 bpc = (uint8_t)connector->display_info.bpc;
3610 /* Assume 8 bpc by default if no bpc is specified. */
3611 bpc = bpc ? bpc : 8;
3615 state = connector->state;
3619 * Cap display bpc based on the user requested value.
3621 * The value for state->max_bpc may not correctly updated
3622 * depending on when the connector gets added to the state
3623 * or if this was called outside of atomic check, so it
3624 * can't be used directly.
3626 bpc = min(bpc, state->max_requested_bpc);
3628 /* Round down to the nearest even number. */
3629 bpc = bpc - (bpc & 1);
3635 * Temporary Work around, DRM doesn't parse color depth for
3636 * EDID revision before 1.4
3637 * TODO: Fix edid parsing
3639 return COLOR_DEPTH_888;
3641 return COLOR_DEPTH_666;
3643 return COLOR_DEPTH_888;
3645 return COLOR_DEPTH_101010;
3647 return COLOR_DEPTH_121212;
3649 return COLOR_DEPTH_141414;
3651 return COLOR_DEPTH_161616;
3653 return COLOR_DEPTH_UNDEFINED;
3657 static enum dc_aspect_ratio
3658 get_aspect_ratio(const struct drm_display_mode *mode_in)
3660 /* 1-1 mapping, since both enums follow the HDMI spec. */
3661 return (enum dc_aspect_ratio) mode_in->picture_aspect_ratio;
3664 static enum dc_color_space
3665 get_output_color_space(const struct dc_crtc_timing *dc_crtc_timing)
3667 enum dc_color_space color_space = COLOR_SPACE_SRGB;
3669 switch (dc_crtc_timing->pixel_encoding) {
3670 case PIXEL_ENCODING_YCBCR422:
3671 case PIXEL_ENCODING_YCBCR444:
3672 case PIXEL_ENCODING_YCBCR420:
3675 * 27030khz is the separation point between HDTV and SDTV
3676 * according to HDMI spec, we use YCbCr709 and YCbCr601
3679 if (dc_crtc_timing->pix_clk_100hz > 270300) {
3680 if (dc_crtc_timing->flags.Y_ONLY)
3682 COLOR_SPACE_YCBCR709_LIMITED;
3684 color_space = COLOR_SPACE_YCBCR709;
3686 if (dc_crtc_timing->flags.Y_ONLY)
3688 COLOR_SPACE_YCBCR601_LIMITED;
3690 color_space = COLOR_SPACE_YCBCR601;
3695 case PIXEL_ENCODING_RGB:
3696 color_space = COLOR_SPACE_SRGB;
3707 static void reduce_mode_colour_depth(struct dc_crtc_timing *timing_out)
3709 if (timing_out->display_color_depth <= COLOR_DEPTH_888)
3712 timing_out->display_color_depth--;
3715 static void adjust_colour_depth_from_display_info(struct dc_crtc_timing *timing_out,
3716 const struct drm_display_info *info)
3719 if (timing_out->display_color_depth <= COLOR_DEPTH_888)
3722 normalized_clk = timing_out->pix_clk_100hz / 10;
3723 /* YCbCr 4:2:0 requires additional adjustment of 1/2 */
3724 if (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420)
3725 normalized_clk /= 2;
3726 /* Adjusting pix clock following on HDMI spec based on colour depth */
3727 switch (timing_out->display_color_depth) {
3728 case COLOR_DEPTH_101010:
3729 normalized_clk = (normalized_clk * 30) / 24;
3731 case COLOR_DEPTH_121212:
3732 normalized_clk = (normalized_clk * 36) / 24;
3734 case COLOR_DEPTH_161616:
3735 normalized_clk = (normalized_clk * 48) / 24;
3740 if (normalized_clk <= info->max_tmds_clock)
3742 reduce_mode_colour_depth(timing_out);
3744 } while (timing_out->display_color_depth > COLOR_DEPTH_888);
3748 static void fill_stream_properties_from_drm_display_mode(
3749 struct dc_stream_state *stream,
3750 const struct drm_display_mode *mode_in,
3751 const struct drm_connector *connector,
3752 const struct drm_connector_state *connector_state,
3753 const struct dc_stream_state *old_stream)
3755 struct dc_crtc_timing *timing_out = &stream->timing;
3756 const struct drm_display_info *info = &connector->display_info;
3757 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
3758 struct hdmi_vendor_infoframe hv_frame;
3759 struct hdmi_avi_infoframe avi_frame;
3761 memset(&hv_frame, 0, sizeof(hv_frame));
3762 memset(&avi_frame, 0, sizeof(avi_frame));
3764 timing_out->h_border_left = 0;
3765 timing_out->h_border_right = 0;
3766 timing_out->v_border_top = 0;
3767 timing_out->v_border_bottom = 0;
3768 /* TODO: un-hardcode */
3769 if (drm_mode_is_420_only(info, mode_in)
3770 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
3771 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
3772 else if (drm_mode_is_420_also(info, mode_in)
3773 && aconnector->force_yuv420_output)
3774 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
3775 else if ((connector->display_info.color_formats & DRM_COLOR_FORMAT_YCRCB444)
3776 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
3777 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR444;
3779 timing_out->pixel_encoding = PIXEL_ENCODING_RGB;
3781 timing_out->timing_3d_format = TIMING_3D_FORMAT_NONE;
3782 timing_out->display_color_depth = convert_color_depth_from_display_info(
3783 connector, connector_state,
3784 (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420));
3785 timing_out->scan_type = SCANNING_TYPE_NODATA;
3786 timing_out->hdmi_vic = 0;
3789 timing_out->vic = old_stream->timing.vic;
3790 timing_out->flags.HSYNC_POSITIVE_POLARITY = old_stream->timing.flags.HSYNC_POSITIVE_POLARITY;
3791 timing_out->flags.VSYNC_POSITIVE_POLARITY = old_stream->timing.flags.VSYNC_POSITIVE_POLARITY;
3793 timing_out->vic = drm_match_cea_mode(mode_in);
3794 if (mode_in->flags & DRM_MODE_FLAG_PHSYNC)
3795 timing_out->flags.HSYNC_POSITIVE_POLARITY = 1;
3796 if (mode_in->flags & DRM_MODE_FLAG_PVSYNC)
3797 timing_out->flags.VSYNC_POSITIVE_POLARITY = 1;
3800 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) {
3801 drm_hdmi_avi_infoframe_from_display_mode(&avi_frame, (struct drm_connector *)connector, mode_in);
3802 timing_out->vic = avi_frame.video_code;
3803 drm_hdmi_vendor_infoframe_from_display_mode(&hv_frame, (struct drm_connector *)connector, mode_in);
3804 timing_out->hdmi_vic = hv_frame.vic;
3807 timing_out->h_addressable = mode_in->crtc_hdisplay;
3808 timing_out->h_total = mode_in->crtc_htotal;
3809 timing_out->h_sync_width =
3810 mode_in->crtc_hsync_end - mode_in->crtc_hsync_start;
3811 timing_out->h_front_porch =
3812 mode_in->crtc_hsync_start - mode_in->crtc_hdisplay;
3813 timing_out->v_total = mode_in->crtc_vtotal;
3814 timing_out->v_addressable = mode_in->crtc_vdisplay;
3815 timing_out->v_front_porch =
3816 mode_in->crtc_vsync_start - mode_in->crtc_vdisplay;
3817 timing_out->v_sync_width =
3818 mode_in->crtc_vsync_end - mode_in->crtc_vsync_start;
3819 timing_out->pix_clk_100hz = mode_in->crtc_clock * 10;
3820 timing_out->aspect_ratio = get_aspect_ratio(mode_in);
3822 stream->output_color_space = get_output_color_space(timing_out);
3824 stream->out_transfer_func->type = TF_TYPE_PREDEFINED;
3825 stream->out_transfer_func->tf = TRANSFER_FUNCTION_SRGB;
3826 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
3827 adjust_colour_depth_from_display_info(timing_out, info);
3830 static void fill_audio_info(struct audio_info *audio_info,
3831 const struct drm_connector *drm_connector,
3832 const struct dc_sink *dc_sink)
3835 int cea_revision = 0;
3836 const struct dc_edid_caps *edid_caps = &dc_sink->edid_caps;
3838 audio_info->manufacture_id = edid_caps->manufacturer_id;
3839 audio_info->product_id = edid_caps->product_id;
3841 cea_revision = drm_connector->display_info.cea_rev;
3843 strscpy(audio_info->display_name,
3844 edid_caps->display_name,
3845 AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS);
3847 if (cea_revision >= 3) {
3848 audio_info->mode_count = edid_caps->audio_mode_count;
3850 for (i = 0; i < audio_info->mode_count; ++i) {
3851 audio_info->modes[i].format_code =
3852 (enum audio_format_code)
3853 (edid_caps->audio_modes[i].format_code);
3854 audio_info->modes[i].channel_count =
3855 edid_caps->audio_modes[i].channel_count;
3856 audio_info->modes[i].sample_rates.all =
3857 edid_caps->audio_modes[i].sample_rate;
3858 audio_info->modes[i].sample_size =
3859 edid_caps->audio_modes[i].sample_size;
3863 audio_info->flags.all = edid_caps->speaker_flags;
3865 /* TODO: We only check for the progressive mode, check for interlace mode too */
3866 if (drm_connector->latency_present[0]) {
3867 audio_info->video_latency = drm_connector->video_latency[0];
3868 audio_info->audio_latency = drm_connector->audio_latency[0];
3871 /* TODO: For DP, video and audio latency should be calculated from DPCD caps */
3876 copy_crtc_timing_for_drm_display_mode(const struct drm_display_mode *src_mode,
3877 struct drm_display_mode *dst_mode)
3879 dst_mode->crtc_hdisplay = src_mode->crtc_hdisplay;
3880 dst_mode->crtc_vdisplay = src_mode->crtc_vdisplay;
3881 dst_mode->crtc_clock = src_mode->crtc_clock;
3882 dst_mode->crtc_hblank_start = src_mode->crtc_hblank_start;
3883 dst_mode->crtc_hblank_end = src_mode->crtc_hblank_end;
3884 dst_mode->crtc_hsync_start = src_mode->crtc_hsync_start;
3885 dst_mode->crtc_hsync_end = src_mode->crtc_hsync_end;
3886 dst_mode->crtc_htotal = src_mode->crtc_htotal;
3887 dst_mode->crtc_hskew = src_mode->crtc_hskew;
3888 dst_mode->crtc_vblank_start = src_mode->crtc_vblank_start;
3889 dst_mode->crtc_vblank_end = src_mode->crtc_vblank_end;
3890 dst_mode->crtc_vsync_start = src_mode->crtc_vsync_start;
3891 dst_mode->crtc_vsync_end = src_mode->crtc_vsync_end;
3892 dst_mode->crtc_vtotal = src_mode->crtc_vtotal;
3896 decide_crtc_timing_for_drm_display_mode(struct drm_display_mode *drm_mode,
3897 const struct drm_display_mode *native_mode,
3900 if (scale_enabled) {
3901 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
3902 } else if (native_mode->clock == drm_mode->clock &&
3903 native_mode->htotal == drm_mode->htotal &&
3904 native_mode->vtotal == drm_mode->vtotal) {
3905 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
3907 /* no scaling nor amdgpu inserted, no need to patch */
3911 static struct dc_sink *
3912 create_fake_sink(struct amdgpu_dm_connector *aconnector)
3914 struct dc_sink_init_data sink_init_data = { 0 };
3915 struct dc_sink *sink = NULL;
3916 sink_init_data.link = aconnector->dc_link;
3917 sink_init_data.sink_signal = aconnector->dc_link->connector_signal;
3919 sink = dc_sink_create(&sink_init_data);
3921 DRM_ERROR("Failed to create sink!\n");
3924 sink->sink_signal = SIGNAL_TYPE_VIRTUAL;
3929 static void set_multisync_trigger_params(
3930 struct dc_stream_state *stream)
3932 if (stream->triggered_crtc_reset.enabled) {
3933 stream->triggered_crtc_reset.event = CRTC_EVENT_VSYNC_RISING;
3934 stream->triggered_crtc_reset.delay = TRIGGER_DELAY_NEXT_LINE;
3938 static void set_master_stream(struct dc_stream_state *stream_set[],
3941 int j, highest_rfr = 0, master_stream = 0;
3943 for (j = 0; j < stream_count; j++) {
3944 if (stream_set[j] && stream_set[j]->triggered_crtc_reset.enabled) {
3945 int refresh_rate = 0;
3947 refresh_rate = (stream_set[j]->timing.pix_clk_100hz*100)/
3948 (stream_set[j]->timing.h_total*stream_set[j]->timing.v_total);
3949 if (refresh_rate > highest_rfr) {
3950 highest_rfr = refresh_rate;
3955 for (j = 0; j < stream_count; j++) {
3957 stream_set[j]->triggered_crtc_reset.event_source = stream_set[master_stream];
3961 static void dm_enable_per_frame_crtc_master_sync(struct dc_state *context)
3965 if (context->stream_count < 2)
3967 for (i = 0; i < context->stream_count ; i++) {
3968 if (!context->streams[i])
3971 * TODO: add a function to read AMD VSDB bits and set
3972 * crtc_sync_master.multi_sync_enabled flag
3973 * For now it's set to false
3975 set_multisync_trigger_params(context->streams[i]);
3977 set_master_stream(context->streams, context->stream_count);
3980 static struct dc_stream_state *
3981 create_stream_for_sink(struct amdgpu_dm_connector *aconnector,
3982 const struct drm_display_mode *drm_mode,
3983 const struct dm_connector_state *dm_state,
3984 const struct dc_stream_state *old_stream)
3986 struct drm_display_mode *preferred_mode = NULL;
3987 struct drm_connector *drm_connector;
3988 const struct drm_connector_state *con_state =
3989 dm_state ? &dm_state->base : NULL;
3990 struct dc_stream_state *stream = NULL;
3991 struct drm_display_mode mode = *drm_mode;
3992 bool native_mode_found = false;
3993 bool scale = dm_state ? (dm_state->scaling != RMX_OFF) : false;
3995 int preferred_refresh = 0;
3996 #if defined(CONFIG_DRM_AMD_DC_DCN)
3997 struct dsc_dec_dpcd_caps dsc_caps;
3999 uint32_t link_bandwidth_kbps;
4001 struct dc_sink *sink = NULL;
4002 if (aconnector == NULL) {
4003 DRM_ERROR("aconnector is NULL!\n");
4007 drm_connector = &aconnector->base;
4009 if (!aconnector->dc_sink) {
4010 sink = create_fake_sink(aconnector);
4014 sink = aconnector->dc_sink;
4015 dc_sink_retain(sink);
4018 stream = dc_create_stream_for_sink(sink);
4020 if (stream == NULL) {
4021 DRM_ERROR("Failed to create stream for sink!\n");
4025 stream->dm_stream_context = aconnector;
4027 stream->timing.flags.LTE_340MCSC_SCRAMBLE =
4028 drm_connector->display_info.hdmi.scdc.scrambling.low_rates;
4030 list_for_each_entry(preferred_mode, &aconnector->base.modes, head) {
4031 /* Search for preferred mode */
4032 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED) {
4033 native_mode_found = true;
4037 if (!native_mode_found)
4038 preferred_mode = list_first_entry_or_null(
4039 &aconnector->base.modes,
4040 struct drm_display_mode,
4043 mode_refresh = drm_mode_vrefresh(&mode);
4045 if (preferred_mode == NULL) {
4047 * This may not be an error, the use case is when we have no
4048 * usermode calls to reset and set mode upon hotplug. In this
4049 * case, we call set mode ourselves to restore the previous mode
4050 * and the modelist may not be filled in in time.
4052 DRM_DEBUG_DRIVER("No preferred mode found\n");
4054 decide_crtc_timing_for_drm_display_mode(
4055 &mode, preferred_mode,
4056 dm_state ? (dm_state->scaling != RMX_OFF) : false);
4057 preferred_refresh = drm_mode_vrefresh(preferred_mode);
4061 drm_mode_set_crtcinfo(&mode, 0);
4064 * If scaling is enabled and refresh rate didn't change
4065 * we copy the vic and polarities of the old timings
4067 if (!scale || mode_refresh != preferred_refresh)
4068 fill_stream_properties_from_drm_display_mode(stream,
4069 &mode, &aconnector->base, con_state, NULL);
4071 fill_stream_properties_from_drm_display_mode(stream,
4072 &mode, &aconnector->base, con_state, old_stream);
4074 stream->timing.flags.DSC = 0;
4076 if (aconnector->dc_link && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT) {
4077 #if defined(CONFIG_DRM_AMD_DC_DCN)
4078 dc_dsc_parse_dsc_dpcd(aconnector->dc_link->dpcd_caps.dsc_caps.dsc_basic_caps.raw,
4079 aconnector->dc_link->dpcd_caps.dsc_caps.dsc_ext_caps.raw,
4082 link_bandwidth_kbps = dc_link_bandwidth_kbps(aconnector->dc_link,
4083 dc_link_get_link_cap(aconnector->dc_link));
4085 #if defined(CONFIG_DRM_AMD_DC_DCN)
4086 if (dsc_caps.is_dsc_supported)
4087 if (dc_dsc_compute_config(aconnector->dc_link->ctx->dc->res_pool->dscs[0],
4089 aconnector->dc_link->ctx->dc->debug.dsc_min_slice_height_override,
4090 link_bandwidth_kbps,
4092 &stream->timing.dsc_cfg))
4093 stream->timing.flags.DSC = 1;
4097 update_stream_scaling_settings(&mode, dm_state, stream);
4100 &stream->audio_info,
4104 update_stream_signal(stream, sink);
4106 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
4107 mod_build_hf_vsif_infopacket(stream, &stream->vsp_infopacket, false, false);
4108 if (stream->link->psr_feature_enabled) {
4109 struct dc *core_dc = stream->link->ctx->dc;
4111 if (dc_is_dmcu_initialized(core_dc)) {
4112 struct dmcu *dmcu = core_dc->res_pool->dmcu;
4114 stream->psr_version = dmcu->dmcu_version.psr_version;
4115 mod_build_vsc_infopacket(stream,
4116 &stream->vsc_infopacket,
4117 &stream->use_vsc_sdp_for_colorimetry);
4121 dc_sink_release(sink);
4126 static void amdgpu_dm_crtc_destroy(struct drm_crtc *crtc)
4128 drm_crtc_cleanup(crtc);
4132 static void dm_crtc_destroy_state(struct drm_crtc *crtc,
4133 struct drm_crtc_state *state)
4135 struct dm_crtc_state *cur = to_dm_crtc_state(state);
4137 /* TODO Destroy dc_stream objects are stream object is flattened */
4139 dc_stream_release(cur->stream);
4142 __drm_atomic_helper_crtc_destroy_state(state);
4148 static void dm_crtc_reset_state(struct drm_crtc *crtc)
4150 struct dm_crtc_state *state;
4153 dm_crtc_destroy_state(crtc, crtc->state);
4155 state = kzalloc(sizeof(*state), GFP_KERNEL);
4156 if (WARN_ON(!state))
4159 crtc->state = &state->base;
4160 crtc->state->crtc = crtc;
4164 static struct drm_crtc_state *
4165 dm_crtc_duplicate_state(struct drm_crtc *crtc)
4167 struct dm_crtc_state *state, *cur;
4169 cur = to_dm_crtc_state(crtc->state);
4171 if (WARN_ON(!crtc->state))
4174 state = kzalloc(sizeof(*state), GFP_KERNEL);
4178 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
4181 state->stream = cur->stream;
4182 dc_stream_retain(state->stream);
4185 state->active_planes = cur->active_planes;
4186 state->interrupts_enabled = cur->interrupts_enabled;
4187 state->vrr_params = cur->vrr_params;
4188 state->vrr_infopacket = cur->vrr_infopacket;
4189 state->abm_level = cur->abm_level;
4190 state->vrr_supported = cur->vrr_supported;
4191 state->freesync_config = cur->freesync_config;
4192 state->crc_src = cur->crc_src;
4193 state->cm_has_degamma = cur->cm_has_degamma;
4194 state->cm_is_degamma_srgb = cur->cm_is_degamma_srgb;
4196 /* TODO Duplicate dc_stream after objects are stream object is flattened */
4198 return &state->base;
4201 static inline int dm_set_vupdate_irq(struct drm_crtc *crtc, bool enable)
4203 enum dc_irq_source irq_source;
4204 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4205 struct amdgpu_device *adev = crtc->dev->dev_private;
4208 /* Do not set vupdate for DCN hardware */
4209 if (adev->family > AMDGPU_FAMILY_AI)
4212 irq_source = IRQ_TYPE_VUPDATE + acrtc->otg_inst;
4214 rc = dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY;
4216 DRM_DEBUG_DRIVER("crtc %d - vupdate irq %sabling: r=%d\n",
4217 acrtc->crtc_id, enable ? "en" : "dis", rc);
4221 static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
4223 enum dc_irq_source irq_source;
4224 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4225 struct amdgpu_device *adev = crtc->dev->dev_private;
4226 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
4230 /* vblank irq on -> Only need vupdate irq in vrr mode */
4231 if (amdgpu_dm_vrr_active(acrtc_state))
4232 rc = dm_set_vupdate_irq(crtc, true);
4234 /* vblank irq off -> vupdate irq off */
4235 rc = dm_set_vupdate_irq(crtc, false);
4241 irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
4242 return dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY;
4245 static int dm_enable_vblank(struct drm_crtc *crtc)
4247 return dm_set_vblank(crtc, true);
4250 static void dm_disable_vblank(struct drm_crtc *crtc)
4252 dm_set_vblank(crtc, false);
4255 /* Implemented only the options currently availible for the driver */
4256 static const struct drm_crtc_funcs amdgpu_dm_crtc_funcs = {
4257 .reset = dm_crtc_reset_state,
4258 .destroy = amdgpu_dm_crtc_destroy,
4259 .gamma_set = drm_atomic_helper_legacy_gamma_set,
4260 .set_config = drm_atomic_helper_set_config,
4261 .page_flip = drm_atomic_helper_page_flip,
4262 .atomic_duplicate_state = dm_crtc_duplicate_state,
4263 .atomic_destroy_state = dm_crtc_destroy_state,
4264 .set_crc_source = amdgpu_dm_crtc_set_crc_source,
4265 .verify_crc_source = amdgpu_dm_crtc_verify_crc_source,
4266 .get_crc_sources = amdgpu_dm_crtc_get_crc_sources,
4267 .enable_vblank = dm_enable_vblank,
4268 .disable_vblank = dm_disable_vblank,
4271 static enum drm_connector_status
4272 amdgpu_dm_connector_detect(struct drm_connector *connector, bool force)
4275 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
4279 * 1. This interface is NOT called in context of HPD irq.
4280 * 2. This interface *is called* in context of user-mode ioctl. Which
4281 * makes it a bad place for *any* MST-related activity.
4284 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED &&
4285 !aconnector->fake_enable)
4286 connected = (aconnector->dc_sink != NULL);
4288 connected = (aconnector->base.force == DRM_FORCE_ON);
4290 return (connected ? connector_status_connected :
4291 connector_status_disconnected);
4294 int amdgpu_dm_connector_atomic_set_property(struct drm_connector *connector,
4295 struct drm_connector_state *connector_state,
4296 struct drm_property *property,
4299 struct drm_device *dev = connector->dev;
4300 struct amdgpu_device *adev = dev->dev_private;
4301 struct dm_connector_state *dm_old_state =
4302 to_dm_connector_state(connector->state);
4303 struct dm_connector_state *dm_new_state =
4304 to_dm_connector_state(connector_state);
4308 if (property == dev->mode_config.scaling_mode_property) {
4309 enum amdgpu_rmx_type rmx_type;
4312 case DRM_MODE_SCALE_CENTER:
4313 rmx_type = RMX_CENTER;
4315 case DRM_MODE_SCALE_ASPECT:
4316 rmx_type = RMX_ASPECT;
4318 case DRM_MODE_SCALE_FULLSCREEN:
4319 rmx_type = RMX_FULL;
4321 case DRM_MODE_SCALE_NONE:
4327 if (dm_old_state->scaling == rmx_type)
4330 dm_new_state->scaling = rmx_type;
4332 } else if (property == adev->mode_info.underscan_hborder_property) {
4333 dm_new_state->underscan_hborder = val;
4335 } else if (property == adev->mode_info.underscan_vborder_property) {
4336 dm_new_state->underscan_vborder = val;
4338 } else if (property == adev->mode_info.underscan_property) {
4339 dm_new_state->underscan_enable = val;
4341 } else if (property == adev->mode_info.abm_level_property) {
4342 dm_new_state->abm_level = val;
4349 int amdgpu_dm_connector_atomic_get_property(struct drm_connector *connector,
4350 const struct drm_connector_state *state,
4351 struct drm_property *property,
4354 struct drm_device *dev = connector->dev;
4355 struct amdgpu_device *adev = dev->dev_private;
4356 struct dm_connector_state *dm_state =
4357 to_dm_connector_state(state);
4360 if (property == dev->mode_config.scaling_mode_property) {
4361 switch (dm_state->scaling) {
4363 *val = DRM_MODE_SCALE_CENTER;
4366 *val = DRM_MODE_SCALE_ASPECT;
4369 *val = DRM_MODE_SCALE_FULLSCREEN;
4373 *val = DRM_MODE_SCALE_NONE;
4377 } else if (property == adev->mode_info.underscan_hborder_property) {
4378 *val = dm_state->underscan_hborder;
4380 } else if (property == adev->mode_info.underscan_vborder_property) {
4381 *val = dm_state->underscan_vborder;
4383 } else if (property == adev->mode_info.underscan_property) {
4384 *val = dm_state->underscan_enable;
4386 } else if (property == adev->mode_info.abm_level_property) {
4387 *val = dm_state->abm_level;
4394 static void amdgpu_dm_connector_unregister(struct drm_connector *connector)
4396 struct amdgpu_dm_connector *amdgpu_dm_connector = to_amdgpu_dm_connector(connector);
4398 drm_dp_aux_unregister(&amdgpu_dm_connector->dm_dp_aux.aux);
4401 static void amdgpu_dm_connector_destroy(struct drm_connector *connector)
4403 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
4404 const struct dc_link *link = aconnector->dc_link;
4405 struct amdgpu_device *adev = connector->dev->dev_private;
4406 struct amdgpu_display_manager *dm = &adev->dm;
4408 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
4409 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
4411 if ((link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) &&
4412 link->type != dc_connection_none &&
4413 dm->backlight_dev) {
4414 backlight_device_unregister(dm->backlight_dev);
4415 dm->backlight_dev = NULL;
4419 if (aconnector->dc_em_sink)
4420 dc_sink_release(aconnector->dc_em_sink);
4421 aconnector->dc_em_sink = NULL;
4422 if (aconnector->dc_sink)
4423 dc_sink_release(aconnector->dc_sink);
4424 aconnector->dc_sink = NULL;
4426 drm_dp_cec_unregister_connector(&aconnector->dm_dp_aux.aux);
4427 drm_connector_unregister(connector);
4428 drm_connector_cleanup(connector);
4429 if (aconnector->i2c) {
4430 i2c_del_adapter(&aconnector->i2c->base);
4431 kfree(aconnector->i2c);
4437 void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector)
4439 struct dm_connector_state *state =
4440 to_dm_connector_state(connector->state);
4442 if (connector->state)
4443 __drm_atomic_helper_connector_destroy_state(connector->state);
4447 state = kzalloc(sizeof(*state), GFP_KERNEL);
4450 state->scaling = RMX_OFF;
4451 state->underscan_enable = false;
4452 state->underscan_hborder = 0;
4453 state->underscan_vborder = 0;
4454 state->base.max_requested_bpc = 8;
4455 state->vcpi_slots = 0;
4457 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
4458 state->abm_level = amdgpu_dm_abm_level;
4460 __drm_atomic_helper_connector_reset(connector, &state->base);
4464 struct drm_connector_state *
4465 amdgpu_dm_connector_atomic_duplicate_state(struct drm_connector *connector)
4467 struct dm_connector_state *state =
4468 to_dm_connector_state(connector->state);
4470 struct dm_connector_state *new_state =
4471 kmemdup(state, sizeof(*state), GFP_KERNEL);
4476 __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);
4478 new_state->freesync_capable = state->freesync_capable;
4479 new_state->abm_level = state->abm_level;
4480 new_state->scaling = state->scaling;
4481 new_state->underscan_enable = state->underscan_enable;
4482 new_state->underscan_hborder = state->underscan_hborder;
4483 new_state->underscan_vborder = state->underscan_vborder;
4484 new_state->vcpi_slots = state->vcpi_slots;
4485 new_state->pbn = state->pbn;
4486 return &new_state->base;
4489 static const struct drm_connector_funcs amdgpu_dm_connector_funcs = {
4490 .reset = amdgpu_dm_connector_funcs_reset,
4491 .detect = amdgpu_dm_connector_detect,
4492 .fill_modes = drm_helper_probe_single_connector_modes,
4493 .destroy = amdgpu_dm_connector_destroy,
4494 .atomic_duplicate_state = amdgpu_dm_connector_atomic_duplicate_state,
4495 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
4496 .atomic_set_property = amdgpu_dm_connector_atomic_set_property,
4497 .atomic_get_property = amdgpu_dm_connector_atomic_get_property,
4498 .early_unregister = amdgpu_dm_connector_unregister
4501 static int get_modes(struct drm_connector *connector)
4503 return amdgpu_dm_connector_get_modes(connector);
4506 static void create_eml_sink(struct amdgpu_dm_connector *aconnector)
4508 struct dc_sink_init_data init_params = {
4509 .link = aconnector->dc_link,
4510 .sink_signal = SIGNAL_TYPE_VIRTUAL
4514 if (!aconnector->base.edid_blob_ptr) {
4515 DRM_ERROR("No EDID firmware found on connector: %s ,forcing to OFF!\n",
4516 aconnector->base.name);
4518 aconnector->base.force = DRM_FORCE_OFF;
4519 aconnector->base.override_edid = false;
4523 edid = (struct edid *) aconnector->base.edid_blob_ptr->data;
4525 aconnector->edid = edid;
4527 aconnector->dc_em_sink = dc_link_add_remote_sink(
4528 aconnector->dc_link,
4530 (edid->extensions + 1) * EDID_LENGTH,
4533 if (aconnector->base.force == DRM_FORCE_ON) {
4534 aconnector->dc_sink = aconnector->dc_link->local_sink ?
4535 aconnector->dc_link->local_sink :
4536 aconnector->dc_em_sink;
4537 dc_sink_retain(aconnector->dc_sink);
4541 static void handle_edid_mgmt(struct amdgpu_dm_connector *aconnector)
4543 struct dc_link *link = (struct dc_link *)aconnector->dc_link;
4546 * In case of headless boot with force on for DP managed connector
4547 * Those settings have to be != 0 to get initial modeset
4549 if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT) {
4550 link->verified_link_cap.lane_count = LANE_COUNT_FOUR;
4551 link->verified_link_cap.link_rate = LINK_RATE_HIGH2;
4555 aconnector->base.override_edid = true;
4556 create_eml_sink(aconnector);
4559 enum drm_mode_status amdgpu_dm_connector_mode_valid(struct drm_connector *connector,
4560 struct drm_display_mode *mode)
4562 int result = MODE_ERROR;
4563 struct dc_sink *dc_sink;
4564 struct amdgpu_device *adev = connector->dev->dev_private;
4565 /* TODO: Unhardcode stream count */
4566 struct dc_stream_state *stream;
4567 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
4568 enum dc_status dc_result = DC_OK;
4570 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
4571 (mode->flags & DRM_MODE_FLAG_DBLSCAN))
4575 * Only run this the first time mode_valid is called to initilialize
4578 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED &&
4579 !aconnector->dc_em_sink)
4580 handle_edid_mgmt(aconnector);
4582 dc_sink = to_amdgpu_dm_connector(connector)->dc_sink;
4584 if (dc_sink == NULL) {
4585 DRM_ERROR("dc_sink is NULL!\n");
4589 stream = create_stream_for_sink(aconnector, mode, NULL, NULL);
4590 if (stream == NULL) {
4591 DRM_ERROR("Failed to create stream for sink!\n");
4595 dc_result = dc_validate_stream(adev->dm.dc, stream);
4597 if (dc_result == DC_OK)
4600 DRM_DEBUG_KMS("Mode %dx%d (clk %d) failed DC validation with error %d\n",
4606 dc_stream_release(stream);
4609 /* TODO: error handling*/
4613 static int fill_hdr_info_packet(const struct drm_connector_state *state,
4614 struct dc_info_packet *out)
4616 struct hdmi_drm_infoframe frame;
4617 unsigned char buf[30]; /* 26 + 4 */
4621 memset(out, 0, sizeof(*out));
4623 if (!state->hdr_output_metadata)
4626 ret = drm_hdmi_infoframe_set_hdr_metadata(&frame, state);
4630 len = hdmi_drm_infoframe_pack_only(&frame, buf, sizeof(buf));
4634 /* Static metadata is a fixed 26 bytes + 4 byte header. */
4638 /* Prepare the infopacket for DC. */
4639 switch (state->connector->connector_type) {
4640 case DRM_MODE_CONNECTOR_HDMIA:
4641 out->hb0 = 0x87; /* type */
4642 out->hb1 = 0x01; /* version */
4643 out->hb2 = 0x1A; /* length */
4644 out->sb[0] = buf[3]; /* checksum */
4648 case DRM_MODE_CONNECTOR_DisplayPort:
4649 case DRM_MODE_CONNECTOR_eDP:
4650 out->hb0 = 0x00; /* sdp id, zero */
4651 out->hb1 = 0x87; /* type */
4652 out->hb2 = 0x1D; /* payload len - 1 */
4653 out->hb3 = (0x13 << 2); /* sdp version */
4654 out->sb[0] = 0x01; /* version */
4655 out->sb[1] = 0x1A; /* length */
4663 memcpy(&out->sb[i], &buf[4], 26);
4666 print_hex_dump(KERN_DEBUG, "HDR SB:", DUMP_PREFIX_NONE, 16, 1, out->sb,
4667 sizeof(out->sb), false);
4673 is_hdr_metadata_different(const struct drm_connector_state *old_state,
4674 const struct drm_connector_state *new_state)
4676 struct drm_property_blob *old_blob = old_state->hdr_output_metadata;
4677 struct drm_property_blob *new_blob = new_state->hdr_output_metadata;
4679 if (old_blob != new_blob) {
4680 if (old_blob && new_blob &&
4681 old_blob->length == new_blob->length)
4682 return memcmp(old_blob->data, new_blob->data,
4692 amdgpu_dm_connector_atomic_check(struct drm_connector *conn,
4693 struct drm_atomic_state *state)
4695 struct drm_connector_state *new_con_state =
4696 drm_atomic_get_new_connector_state(state, conn);
4697 struct drm_connector_state *old_con_state =
4698 drm_atomic_get_old_connector_state(state, conn);
4699 struct drm_crtc *crtc = new_con_state->crtc;
4700 struct drm_crtc_state *new_crtc_state;
4706 if (is_hdr_metadata_different(old_con_state, new_con_state)) {
4707 struct dc_info_packet hdr_infopacket;
4709 ret = fill_hdr_info_packet(new_con_state, &hdr_infopacket);
4713 new_crtc_state = drm_atomic_get_crtc_state(state, crtc);
4714 if (IS_ERR(new_crtc_state))
4715 return PTR_ERR(new_crtc_state);
4718 * DC considers the stream backends changed if the
4719 * static metadata changes. Forcing the modeset also
4720 * gives a simple way for userspace to switch from
4721 * 8bpc to 10bpc when setting the metadata to enter
4724 * Changing the static metadata after it's been
4725 * set is permissible, however. So only force a
4726 * modeset if we're entering or exiting HDR.
4728 new_crtc_state->mode_changed =
4729 !old_con_state->hdr_output_metadata ||
4730 !new_con_state->hdr_output_metadata;
4736 static const struct drm_connector_helper_funcs
4737 amdgpu_dm_connector_helper_funcs = {
4739 * If hotplugging a second bigger display in FB Con mode, bigger resolution
4740 * modes will be filtered by drm_mode_validate_size(), and those modes
4741 * are missing after user start lightdm. So we need to renew modes list.
4742 * in get_modes call back, not just return the modes count
4744 .get_modes = get_modes,
4745 .mode_valid = amdgpu_dm_connector_mode_valid,
4746 .atomic_check = amdgpu_dm_connector_atomic_check,
4749 static void dm_crtc_helper_disable(struct drm_crtc *crtc)
4753 static bool does_crtc_have_active_cursor(struct drm_crtc_state *new_crtc_state)
4755 struct drm_device *dev = new_crtc_state->crtc->dev;
4756 struct drm_plane *plane;
4758 drm_for_each_plane_mask(plane, dev, new_crtc_state->plane_mask) {
4759 if (plane->type == DRM_PLANE_TYPE_CURSOR)
4766 static int count_crtc_active_planes(struct drm_crtc_state *new_crtc_state)
4768 struct drm_atomic_state *state = new_crtc_state->state;
4769 struct drm_plane *plane;
4772 drm_for_each_plane_mask(plane, state->dev, new_crtc_state->plane_mask) {
4773 struct drm_plane_state *new_plane_state;
4775 /* Cursor planes are "fake". */
4776 if (plane->type == DRM_PLANE_TYPE_CURSOR)
4779 new_plane_state = drm_atomic_get_new_plane_state(state, plane);
4781 if (!new_plane_state) {
4783 * The plane is enable on the CRTC and hasn't changed
4784 * state. This means that it previously passed
4785 * validation and is therefore enabled.
4791 /* We need a framebuffer to be considered enabled. */
4792 num_active += (new_plane_state->fb != NULL);
4799 * Sets whether interrupts should be enabled on a specific CRTC.
4800 * We require that the stream be enabled and that there exist active
4801 * DC planes on the stream.
4804 dm_update_crtc_interrupt_state(struct drm_crtc *crtc,
4805 struct drm_crtc_state *new_crtc_state)
4807 struct dm_crtc_state *dm_new_crtc_state =
4808 to_dm_crtc_state(new_crtc_state);
4810 dm_new_crtc_state->active_planes = 0;
4811 dm_new_crtc_state->interrupts_enabled = false;
4813 if (!dm_new_crtc_state->stream)
4816 dm_new_crtc_state->active_planes =
4817 count_crtc_active_planes(new_crtc_state);
4819 dm_new_crtc_state->interrupts_enabled =
4820 dm_new_crtc_state->active_planes > 0;
4823 static int dm_crtc_helper_atomic_check(struct drm_crtc *crtc,
4824 struct drm_crtc_state *state)
4826 struct amdgpu_device *adev = crtc->dev->dev_private;
4827 struct dc *dc = adev->dm.dc;
4828 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(state);
4832 * Update interrupt state for the CRTC. This needs to happen whenever
4833 * the CRTC has changed or whenever any of its planes have changed.
4834 * Atomic check satisfies both of these requirements since the CRTC
4835 * is added to the state by DRM during drm_atomic_helper_check_planes.
4837 dm_update_crtc_interrupt_state(crtc, state);
4839 if (unlikely(!dm_crtc_state->stream &&
4840 modeset_required(state, NULL, dm_crtc_state->stream))) {
4845 /* In some use cases, like reset, no stream is attached */
4846 if (!dm_crtc_state->stream)
4850 * We want at least one hardware plane enabled to use
4851 * the stream with a cursor enabled.
4853 if (state->enable && state->active &&
4854 does_crtc_have_active_cursor(state) &&
4855 dm_crtc_state->active_planes == 0)
4858 if (dc_validate_stream(dc, dm_crtc_state->stream) == DC_OK)
4864 static bool dm_crtc_helper_mode_fixup(struct drm_crtc *crtc,
4865 const struct drm_display_mode *mode,
4866 struct drm_display_mode *adjusted_mode)
4871 static const struct drm_crtc_helper_funcs amdgpu_dm_crtc_helper_funcs = {
4872 .disable = dm_crtc_helper_disable,
4873 .atomic_check = dm_crtc_helper_atomic_check,
4874 .mode_fixup = dm_crtc_helper_mode_fixup
4877 static void dm_encoder_helper_disable(struct drm_encoder *encoder)
4882 static int convert_dc_color_depth_into_bpc (enum dc_color_depth display_color_depth)
4884 switch (display_color_depth) {
4885 case COLOR_DEPTH_666:
4887 case COLOR_DEPTH_888:
4889 case COLOR_DEPTH_101010:
4891 case COLOR_DEPTH_121212:
4893 case COLOR_DEPTH_141414:
4895 case COLOR_DEPTH_161616:
4903 static int dm_encoder_helper_atomic_check(struct drm_encoder *encoder,
4904 struct drm_crtc_state *crtc_state,
4905 struct drm_connector_state *conn_state)
4907 struct drm_atomic_state *state = crtc_state->state;
4908 struct drm_connector *connector = conn_state->connector;
4909 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
4910 struct dm_connector_state *dm_new_connector_state = to_dm_connector_state(conn_state);
4911 const struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
4912 struct drm_dp_mst_topology_mgr *mst_mgr;
4913 struct drm_dp_mst_port *mst_port;
4914 enum dc_color_depth color_depth;
4916 bool is_y420 = false;
4918 if (!aconnector->port || !aconnector->dc_sink)
4921 mst_port = aconnector->port;
4922 mst_mgr = &aconnector->mst_port->mst_mgr;
4924 if (!crtc_state->connectors_changed && !crtc_state->mode_changed)
4927 if (!state->duplicated) {
4928 is_y420 = drm_mode_is_420_also(&connector->display_info, adjusted_mode) &&
4929 aconnector->force_yuv420_output;
4930 color_depth = convert_color_depth_from_display_info(connector, conn_state,
4932 bpp = convert_dc_color_depth_into_bpc(color_depth) * 3;
4933 clock = adjusted_mode->clock;
4934 dm_new_connector_state->pbn = drm_dp_calc_pbn_mode(clock, bpp);
4936 dm_new_connector_state->vcpi_slots = drm_dp_atomic_find_vcpi_slots(state,
4939 dm_new_connector_state->pbn);
4940 if (dm_new_connector_state->vcpi_slots < 0) {
4941 DRM_DEBUG_ATOMIC("failed finding vcpi slots: %d\n", (int)dm_new_connector_state->vcpi_slots);
4942 return dm_new_connector_state->vcpi_slots;
4947 const struct drm_encoder_helper_funcs amdgpu_dm_encoder_helper_funcs = {
4948 .disable = dm_encoder_helper_disable,
4949 .atomic_check = dm_encoder_helper_atomic_check
4952 static void dm_drm_plane_reset(struct drm_plane *plane)
4954 struct dm_plane_state *amdgpu_state = NULL;
4957 plane->funcs->atomic_destroy_state(plane, plane->state);
4959 amdgpu_state = kzalloc(sizeof(*amdgpu_state), GFP_KERNEL);
4960 WARN_ON(amdgpu_state == NULL);
4963 __drm_atomic_helper_plane_reset(plane, &amdgpu_state->base);
4966 static struct drm_plane_state *
4967 dm_drm_plane_duplicate_state(struct drm_plane *plane)
4969 struct dm_plane_state *dm_plane_state, *old_dm_plane_state;
4971 old_dm_plane_state = to_dm_plane_state(plane->state);
4972 dm_plane_state = kzalloc(sizeof(*dm_plane_state), GFP_KERNEL);
4973 if (!dm_plane_state)
4976 __drm_atomic_helper_plane_duplicate_state(plane, &dm_plane_state->base);
4978 if (old_dm_plane_state->dc_state) {
4979 dm_plane_state->dc_state = old_dm_plane_state->dc_state;
4980 dc_plane_state_retain(dm_plane_state->dc_state);
4983 return &dm_plane_state->base;
4986 void dm_drm_plane_destroy_state(struct drm_plane *plane,
4987 struct drm_plane_state *state)
4989 struct dm_plane_state *dm_plane_state = to_dm_plane_state(state);
4991 if (dm_plane_state->dc_state)
4992 dc_plane_state_release(dm_plane_state->dc_state);
4994 drm_atomic_helper_plane_destroy_state(plane, state);
4997 static const struct drm_plane_funcs dm_plane_funcs = {
4998 .update_plane = drm_atomic_helper_update_plane,
4999 .disable_plane = drm_atomic_helper_disable_plane,
5000 .destroy = drm_primary_helper_destroy,
5001 .reset = dm_drm_plane_reset,
5002 .atomic_duplicate_state = dm_drm_plane_duplicate_state,
5003 .atomic_destroy_state = dm_drm_plane_destroy_state,
5006 static int dm_plane_helper_prepare_fb(struct drm_plane *plane,
5007 struct drm_plane_state *new_state)
5009 struct amdgpu_framebuffer *afb;
5010 struct drm_gem_object *obj;
5011 struct amdgpu_device *adev;
5012 struct amdgpu_bo *rbo;
5013 struct dm_plane_state *dm_plane_state_new, *dm_plane_state_old;
5014 struct list_head list;
5015 struct ttm_validate_buffer tv;
5016 struct ww_acquire_ctx ticket;
5017 uint64_t tiling_flags;
5021 dm_plane_state_old = to_dm_plane_state(plane->state);
5022 dm_plane_state_new = to_dm_plane_state(new_state);
5024 if (!new_state->fb) {
5025 DRM_DEBUG_DRIVER("No FB bound\n");
5029 afb = to_amdgpu_framebuffer(new_state->fb);
5030 obj = new_state->fb->obj[0];
5031 rbo = gem_to_amdgpu_bo(obj);
5032 adev = amdgpu_ttm_adev(rbo->tbo.bdev);
5033 INIT_LIST_HEAD(&list);
5037 list_add(&tv.head, &list);
5039 r = ttm_eu_reserve_buffers(&ticket, &list, false, NULL);
5041 dev_err(adev->dev, "fail to reserve bo (%d)\n", r);
5045 if (plane->type != DRM_PLANE_TYPE_CURSOR)
5046 domain = amdgpu_display_supported_domains(adev, rbo->flags);
5048 domain = AMDGPU_GEM_DOMAIN_VRAM;
5050 r = amdgpu_bo_pin(rbo, domain);
5051 if (unlikely(r != 0)) {
5052 if (r != -ERESTARTSYS)
5053 DRM_ERROR("Failed to pin framebuffer with error %d\n", r);
5054 ttm_eu_backoff_reservation(&ticket, &list);
5058 r = amdgpu_ttm_alloc_gart(&rbo->tbo);
5059 if (unlikely(r != 0)) {
5060 amdgpu_bo_unpin(rbo);
5061 ttm_eu_backoff_reservation(&ticket, &list);
5062 DRM_ERROR("%p bind failed\n", rbo);
5066 amdgpu_bo_get_tiling_flags(rbo, &tiling_flags);
5068 ttm_eu_backoff_reservation(&ticket, &list);
5070 afb->address = amdgpu_bo_gpu_offset(rbo);
5074 if (dm_plane_state_new->dc_state &&
5075 dm_plane_state_old->dc_state != dm_plane_state_new->dc_state) {
5076 struct dc_plane_state *plane_state = dm_plane_state_new->dc_state;
5078 fill_plane_buffer_attributes(
5079 adev, afb, plane_state->format, plane_state->rotation,
5080 tiling_flags, &plane_state->tiling_info,
5081 &plane_state->plane_size, &plane_state->dcc,
5082 &plane_state->address);
5088 static void dm_plane_helper_cleanup_fb(struct drm_plane *plane,
5089 struct drm_plane_state *old_state)
5091 struct amdgpu_bo *rbo;
5097 rbo = gem_to_amdgpu_bo(old_state->fb->obj[0]);
5098 r = amdgpu_bo_reserve(rbo, false);
5100 DRM_ERROR("failed to reserve rbo before unpin\n");
5104 amdgpu_bo_unpin(rbo);
5105 amdgpu_bo_unreserve(rbo);
5106 amdgpu_bo_unref(&rbo);
5109 static int dm_plane_atomic_check(struct drm_plane *plane,
5110 struct drm_plane_state *state)
5112 struct amdgpu_device *adev = plane->dev->dev_private;
5113 struct dc *dc = adev->dm.dc;
5114 struct dm_plane_state *dm_plane_state;
5115 struct dc_scaling_info scaling_info;
5118 dm_plane_state = to_dm_plane_state(state);
5120 if (!dm_plane_state->dc_state)
5123 ret = fill_dc_scaling_info(state, &scaling_info);
5127 if (dc_validate_plane(dc, dm_plane_state->dc_state) == DC_OK)
5133 static int dm_plane_atomic_async_check(struct drm_plane *plane,
5134 struct drm_plane_state *new_plane_state)
5136 /* Only support async updates on cursor planes. */
5137 if (plane->type != DRM_PLANE_TYPE_CURSOR)
5143 static void dm_plane_atomic_async_update(struct drm_plane *plane,
5144 struct drm_plane_state *new_state)
5146 struct drm_plane_state *old_state =
5147 drm_atomic_get_old_plane_state(new_state->state, plane);
5149 swap(plane->state->fb, new_state->fb);
5151 plane->state->src_x = new_state->src_x;
5152 plane->state->src_y = new_state->src_y;
5153 plane->state->src_w = new_state->src_w;
5154 plane->state->src_h = new_state->src_h;
5155 plane->state->crtc_x = new_state->crtc_x;
5156 plane->state->crtc_y = new_state->crtc_y;
5157 plane->state->crtc_w = new_state->crtc_w;
5158 plane->state->crtc_h = new_state->crtc_h;
5160 handle_cursor_update(plane, old_state);
5163 static const struct drm_plane_helper_funcs dm_plane_helper_funcs = {
5164 .prepare_fb = dm_plane_helper_prepare_fb,
5165 .cleanup_fb = dm_plane_helper_cleanup_fb,
5166 .atomic_check = dm_plane_atomic_check,
5167 .atomic_async_check = dm_plane_atomic_async_check,
5168 .atomic_async_update = dm_plane_atomic_async_update
5172 * TODO: these are currently initialized to rgb formats only.
5173 * For future use cases we should either initialize them dynamically based on
5174 * plane capabilities, or initialize this array to all formats, so internal drm
5175 * check will succeed, and let DC implement proper check
5177 static const uint32_t rgb_formats[] = {
5178 DRM_FORMAT_XRGB8888,
5179 DRM_FORMAT_ARGB8888,
5180 DRM_FORMAT_RGBA8888,
5181 DRM_FORMAT_XRGB2101010,
5182 DRM_FORMAT_XBGR2101010,
5183 DRM_FORMAT_ARGB2101010,
5184 DRM_FORMAT_ABGR2101010,
5185 DRM_FORMAT_XBGR8888,
5186 DRM_FORMAT_ABGR8888,
5190 static const uint32_t overlay_formats[] = {
5191 DRM_FORMAT_XRGB8888,
5192 DRM_FORMAT_ARGB8888,
5193 DRM_FORMAT_RGBA8888,
5194 DRM_FORMAT_XBGR8888,
5195 DRM_FORMAT_ABGR8888,
5199 static const u32 cursor_formats[] = {
5203 static int get_plane_formats(const struct drm_plane *plane,
5204 const struct dc_plane_cap *plane_cap,
5205 uint32_t *formats, int max_formats)
5207 int i, num_formats = 0;
5210 * TODO: Query support for each group of formats directly from
5211 * DC plane caps. This will require adding more formats to the
5215 switch (plane->type) {
5216 case DRM_PLANE_TYPE_PRIMARY:
5217 for (i = 0; i < ARRAY_SIZE(rgb_formats); ++i) {
5218 if (num_formats >= max_formats)
5221 formats[num_formats++] = rgb_formats[i];
5224 if (plane_cap && plane_cap->pixel_format_support.nv12)
5225 formats[num_formats++] = DRM_FORMAT_NV12;
5228 case DRM_PLANE_TYPE_OVERLAY:
5229 for (i = 0; i < ARRAY_SIZE(overlay_formats); ++i) {
5230 if (num_formats >= max_formats)
5233 formats[num_formats++] = overlay_formats[i];
5237 case DRM_PLANE_TYPE_CURSOR:
5238 for (i = 0; i < ARRAY_SIZE(cursor_formats); ++i) {
5239 if (num_formats >= max_formats)
5242 formats[num_formats++] = cursor_formats[i];
5250 static int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
5251 struct drm_plane *plane,
5252 unsigned long possible_crtcs,
5253 const struct dc_plane_cap *plane_cap)
5255 uint32_t formats[32];
5259 num_formats = get_plane_formats(plane, plane_cap, formats,
5260 ARRAY_SIZE(formats));
5262 res = drm_universal_plane_init(dm->adev->ddev, plane, possible_crtcs,
5263 &dm_plane_funcs, formats, num_formats,
5264 NULL, plane->type, NULL);
5268 if (plane->type == DRM_PLANE_TYPE_OVERLAY &&
5269 plane_cap && plane_cap->per_pixel_alpha) {
5270 unsigned int blend_caps = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
5271 BIT(DRM_MODE_BLEND_PREMULTI);
5273 drm_plane_create_alpha_property(plane);
5274 drm_plane_create_blend_mode_property(plane, blend_caps);
5277 if (plane->type == DRM_PLANE_TYPE_PRIMARY &&
5278 plane_cap && plane_cap->pixel_format_support.nv12) {
5279 /* This only affects YUV formats. */
5280 drm_plane_create_color_properties(
5282 BIT(DRM_COLOR_YCBCR_BT601) |
5283 BIT(DRM_COLOR_YCBCR_BT709),
5284 BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) |
5285 BIT(DRM_COLOR_YCBCR_FULL_RANGE),
5286 DRM_COLOR_YCBCR_BT709, DRM_COLOR_YCBCR_LIMITED_RANGE);
5289 drm_plane_helper_add(plane, &dm_plane_helper_funcs);
5291 /* Create (reset) the plane state */
5292 if (plane->funcs->reset)
5293 plane->funcs->reset(plane);
5298 static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
5299 struct drm_plane *plane,
5300 uint32_t crtc_index)
5302 struct amdgpu_crtc *acrtc = NULL;
5303 struct drm_plane *cursor_plane;
5307 cursor_plane = kzalloc(sizeof(*cursor_plane), GFP_KERNEL);
5311 cursor_plane->type = DRM_PLANE_TYPE_CURSOR;
5312 res = amdgpu_dm_plane_init(dm, cursor_plane, 0, NULL);
5314 acrtc = kzalloc(sizeof(struct amdgpu_crtc), GFP_KERNEL);
5318 res = drm_crtc_init_with_planes(
5323 &amdgpu_dm_crtc_funcs, NULL);
5328 drm_crtc_helper_add(&acrtc->base, &amdgpu_dm_crtc_helper_funcs);
5330 /* Create (reset) the plane state */
5331 if (acrtc->base.funcs->reset)
5332 acrtc->base.funcs->reset(&acrtc->base);
5334 acrtc->max_cursor_width = dm->adev->dm.dc->caps.max_cursor_size;
5335 acrtc->max_cursor_height = dm->adev->dm.dc->caps.max_cursor_size;
5337 acrtc->crtc_id = crtc_index;
5338 acrtc->base.enabled = false;
5339 acrtc->otg_inst = -1;
5341 dm->adev->mode_info.crtcs[crtc_index] = acrtc;
5342 drm_crtc_enable_color_mgmt(&acrtc->base, MAX_COLOR_LUT_ENTRIES,
5343 true, MAX_COLOR_LUT_ENTRIES);
5344 drm_mode_crtc_set_gamma_size(&acrtc->base, MAX_COLOR_LEGACY_LUT_ENTRIES);
5350 kfree(cursor_plane);
5355 static int to_drm_connector_type(enum signal_type st)
5358 case SIGNAL_TYPE_HDMI_TYPE_A:
5359 return DRM_MODE_CONNECTOR_HDMIA;
5360 case SIGNAL_TYPE_EDP:
5361 return DRM_MODE_CONNECTOR_eDP;
5362 case SIGNAL_TYPE_LVDS:
5363 return DRM_MODE_CONNECTOR_LVDS;
5364 case SIGNAL_TYPE_RGB:
5365 return DRM_MODE_CONNECTOR_VGA;
5366 case SIGNAL_TYPE_DISPLAY_PORT:
5367 case SIGNAL_TYPE_DISPLAY_PORT_MST:
5368 return DRM_MODE_CONNECTOR_DisplayPort;
5369 case SIGNAL_TYPE_DVI_DUAL_LINK:
5370 case SIGNAL_TYPE_DVI_SINGLE_LINK:
5371 return DRM_MODE_CONNECTOR_DVID;
5372 case SIGNAL_TYPE_VIRTUAL:
5373 return DRM_MODE_CONNECTOR_VIRTUAL;
5376 return DRM_MODE_CONNECTOR_Unknown;
5380 static struct drm_encoder *amdgpu_dm_connector_to_encoder(struct drm_connector *connector)
5382 struct drm_encoder *encoder;
5384 /* There is only one encoder per connector */
5385 drm_connector_for_each_possible_encoder(connector, encoder)
5391 static void amdgpu_dm_get_native_mode(struct drm_connector *connector)
5393 struct drm_encoder *encoder;
5394 struct amdgpu_encoder *amdgpu_encoder;
5396 encoder = amdgpu_dm_connector_to_encoder(connector);
5398 if (encoder == NULL)
5401 amdgpu_encoder = to_amdgpu_encoder(encoder);
5403 amdgpu_encoder->native_mode.clock = 0;
5405 if (!list_empty(&connector->probed_modes)) {
5406 struct drm_display_mode *preferred_mode = NULL;
5408 list_for_each_entry(preferred_mode,
5409 &connector->probed_modes,
5411 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED)
5412 amdgpu_encoder->native_mode = *preferred_mode;
5420 static struct drm_display_mode *
5421 amdgpu_dm_create_common_mode(struct drm_encoder *encoder,
5423 int hdisplay, int vdisplay)
5425 struct drm_device *dev = encoder->dev;
5426 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
5427 struct drm_display_mode *mode = NULL;
5428 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
5430 mode = drm_mode_duplicate(dev, native_mode);
5435 mode->hdisplay = hdisplay;
5436 mode->vdisplay = vdisplay;
5437 mode->type &= ~DRM_MODE_TYPE_PREFERRED;
5438 strscpy(mode->name, name, DRM_DISPLAY_MODE_LEN);
5444 static void amdgpu_dm_connector_add_common_modes(struct drm_encoder *encoder,
5445 struct drm_connector *connector)
5447 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
5448 struct drm_display_mode *mode = NULL;
5449 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
5450 struct amdgpu_dm_connector *amdgpu_dm_connector =
5451 to_amdgpu_dm_connector(connector);
5455 char name[DRM_DISPLAY_MODE_LEN];
5458 } common_modes[] = {
5459 { "640x480", 640, 480},
5460 { "800x600", 800, 600},
5461 { "1024x768", 1024, 768},
5462 { "1280x720", 1280, 720},
5463 { "1280x800", 1280, 800},
5464 {"1280x1024", 1280, 1024},
5465 { "1440x900", 1440, 900},
5466 {"1680x1050", 1680, 1050},
5467 {"1600x1200", 1600, 1200},
5468 {"1920x1080", 1920, 1080},
5469 {"1920x1200", 1920, 1200}
5472 n = ARRAY_SIZE(common_modes);
5474 for (i = 0; i < n; i++) {
5475 struct drm_display_mode *curmode = NULL;
5476 bool mode_existed = false;
5478 if (common_modes[i].w > native_mode->hdisplay ||
5479 common_modes[i].h > native_mode->vdisplay ||
5480 (common_modes[i].w == native_mode->hdisplay &&
5481 common_modes[i].h == native_mode->vdisplay))
5484 list_for_each_entry(curmode, &connector->probed_modes, head) {
5485 if (common_modes[i].w == curmode->hdisplay &&
5486 common_modes[i].h == curmode->vdisplay) {
5487 mode_existed = true;
5495 mode = amdgpu_dm_create_common_mode(encoder,
5496 common_modes[i].name, common_modes[i].w,
5498 drm_mode_probed_add(connector, mode);
5499 amdgpu_dm_connector->num_modes++;
5503 static void amdgpu_dm_connector_ddc_get_modes(struct drm_connector *connector,
5506 struct amdgpu_dm_connector *amdgpu_dm_connector =
5507 to_amdgpu_dm_connector(connector);
5510 /* empty probed_modes */
5511 INIT_LIST_HEAD(&connector->probed_modes);
5512 amdgpu_dm_connector->num_modes =
5513 drm_add_edid_modes(connector, edid);
5515 /* sorting the probed modes before calling function
5516 * amdgpu_dm_get_native_mode() since EDID can have
5517 * more than one preferred mode. The modes that are
5518 * later in the probed mode list could be of higher
5519 * and preferred resolution. For example, 3840x2160
5520 * resolution in base EDID preferred timing and 4096x2160
5521 * preferred resolution in DID extension block later.
5523 drm_mode_sort(&connector->probed_modes);
5524 amdgpu_dm_get_native_mode(connector);
5526 amdgpu_dm_connector->num_modes = 0;
5530 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector)
5532 struct amdgpu_dm_connector *amdgpu_dm_connector =
5533 to_amdgpu_dm_connector(connector);
5534 struct drm_encoder *encoder;
5535 struct edid *edid = amdgpu_dm_connector->edid;
5537 encoder = amdgpu_dm_connector_to_encoder(connector);
5539 if (!edid || !drm_edid_is_valid(edid)) {
5540 amdgpu_dm_connector->num_modes =
5541 drm_add_modes_noedid(connector, 640, 480);
5543 amdgpu_dm_connector_ddc_get_modes(connector, edid);
5544 amdgpu_dm_connector_add_common_modes(encoder, connector);
5546 amdgpu_dm_fbc_init(connector);
5548 return amdgpu_dm_connector->num_modes;
5551 void amdgpu_dm_connector_init_helper(struct amdgpu_display_manager *dm,
5552 struct amdgpu_dm_connector *aconnector,
5554 struct dc_link *link,
5557 struct amdgpu_device *adev = dm->ddev->dev_private;
5560 * Some of the properties below require access to state, like bpc.
5561 * Allocate some default initial connector state with our reset helper.
5563 if (aconnector->base.funcs->reset)
5564 aconnector->base.funcs->reset(&aconnector->base);
5566 aconnector->connector_id = link_index;
5567 aconnector->dc_link = link;
5568 aconnector->base.interlace_allowed = false;
5569 aconnector->base.doublescan_allowed = false;
5570 aconnector->base.stereo_allowed = false;
5571 aconnector->base.dpms = DRM_MODE_DPMS_OFF;
5572 aconnector->hpd.hpd = AMDGPU_HPD_NONE; /* not used */
5573 aconnector->audio_inst = -1;
5574 mutex_init(&aconnector->hpd_lock);
5577 * configure support HPD hot plug connector_>polled default value is 0
5578 * which means HPD hot plug not supported
5580 switch (connector_type) {
5581 case DRM_MODE_CONNECTOR_HDMIA:
5582 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
5583 aconnector->base.ycbcr_420_allowed =
5584 link->link_enc->features.hdmi_ycbcr420_supported ? true : false;
5586 case DRM_MODE_CONNECTOR_DisplayPort:
5587 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
5588 aconnector->base.ycbcr_420_allowed =
5589 link->link_enc->features.dp_ycbcr420_supported ? true : false;
5591 case DRM_MODE_CONNECTOR_DVID:
5592 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
5598 drm_object_attach_property(&aconnector->base.base,
5599 dm->ddev->mode_config.scaling_mode_property,
5600 DRM_MODE_SCALE_NONE);
5602 drm_object_attach_property(&aconnector->base.base,
5603 adev->mode_info.underscan_property,
5605 drm_object_attach_property(&aconnector->base.base,
5606 adev->mode_info.underscan_hborder_property,
5608 drm_object_attach_property(&aconnector->base.base,
5609 adev->mode_info.underscan_vborder_property,
5612 drm_connector_attach_max_bpc_property(&aconnector->base, 8, 16);
5614 /* This defaults to the max in the range, but we want 8bpc for non-edp. */
5615 aconnector->base.state->max_bpc = (connector_type == DRM_MODE_CONNECTOR_eDP) ? 16 : 8;
5616 aconnector->base.state->max_requested_bpc = aconnector->base.state->max_bpc;
5618 if (connector_type == DRM_MODE_CONNECTOR_eDP &&
5619 dc_is_dmcu_initialized(adev->dm.dc)) {
5620 drm_object_attach_property(&aconnector->base.base,
5621 adev->mode_info.abm_level_property, 0);
5624 if (connector_type == DRM_MODE_CONNECTOR_HDMIA ||
5625 connector_type == DRM_MODE_CONNECTOR_DisplayPort ||
5626 connector_type == DRM_MODE_CONNECTOR_eDP) {
5627 drm_object_attach_property(
5628 &aconnector->base.base,
5629 dm->ddev->mode_config.hdr_output_metadata_property, 0);
5631 drm_connector_attach_vrr_capable_property(
5633 #ifdef CONFIG_DRM_AMD_DC_HDCP
5634 if (adev->asic_type >= CHIP_RAVEN)
5635 drm_connector_attach_content_protection_property(&aconnector->base, true);
5640 static int amdgpu_dm_i2c_xfer(struct i2c_adapter *i2c_adap,
5641 struct i2c_msg *msgs, int num)
5643 struct amdgpu_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
5644 struct ddc_service *ddc_service = i2c->ddc_service;
5645 struct i2c_command cmd;
5649 cmd.payloads = kcalloc(num, sizeof(struct i2c_payload), GFP_KERNEL);
5654 cmd.number_of_payloads = num;
5655 cmd.engine = I2C_COMMAND_ENGINE_DEFAULT;
5658 for (i = 0; i < num; i++) {
5659 cmd.payloads[i].write = !(msgs[i].flags & I2C_M_RD);
5660 cmd.payloads[i].address = msgs[i].addr;
5661 cmd.payloads[i].length = msgs[i].len;
5662 cmd.payloads[i].data = msgs[i].buf;
5666 ddc_service->ctx->dc,
5667 ddc_service->ddc_pin->hw_info.ddc_channel,
5671 kfree(cmd.payloads);
5675 static u32 amdgpu_dm_i2c_func(struct i2c_adapter *adap)
5677 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
5680 static const struct i2c_algorithm amdgpu_dm_i2c_algo = {
5681 .master_xfer = amdgpu_dm_i2c_xfer,
5682 .functionality = amdgpu_dm_i2c_func,
5685 static struct amdgpu_i2c_adapter *
5686 create_i2c(struct ddc_service *ddc_service,
5690 struct amdgpu_device *adev = ddc_service->ctx->driver_context;
5691 struct amdgpu_i2c_adapter *i2c;
5693 i2c = kzalloc(sizeof(struct amdgpu_i2c_adapter), GFP_KERNEL);
5696 i2c->base.owner = THIS_MODULE;
5697 i2c->base.class = I2C_CLASS_DDC;
5698 i2c->base.dev.parent = &adev->pdev->dev;
5699 i2c->base.algo = &amdgpu_dm_i2c_algo;
5700 snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c hw bus %d", link_index);
5701 i2c_set_adapdata(&i2c->base, i2c);
5702 i2c->ddc_service = ddc_service;
5703 i2c->ddc_service->ddc_pin->hw_info.ddc_channel = link_index;
5710 * Note: this function assumes that dc_link_detect() was called for the
5711 * dc_link which will be represented by this aconnector.
5713 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
5714 struct amdgpu_dm_connector *aconnector,
5715 uint32_t link_index,
5716 struct amdgpu_encoder *aencoder)
5720 struct dc *dc = dm->dc;
5721 struct dc_link *link = dc_get_link_at_index(dc, link_index);
5722 struct amdgpu_i2c_adapter *i2c;
5724 link->priv = aconnector;
5726 DRM_DEBUG_DRIVER("%s()\n", __func__);
5728 i2c = create_i2c(link->ddc, link->link_index, &res);
5730 DRM_ERROR("Failed to create i2c adapter data\n");
5734 aconnector->i2c = i2c;
5735 res = i2c_add_adapter(&i2c->base);
5738 DRM_ERROR("Failed to register hw i2c %d\n", link->link_index);
5742 connector_type = to_drm_connector_type(link->connector_signal);
5744 res = drm_connector_init_with_ddc(
5747 &amdgpu_dm_connector_funcs,
5752 DRM_ERROR("connector_init failed\n");
5753 aconnector->connector_id = -1;
5757 drm_connector_helper_add(
5759 &amdgpu_dm_connector_helper_funcs);
5761 amdgpu_dm_connector_init_helper(
5768 drm_connector_attach_encoder(
5769 &aconnector->base, &aencoder->base);
5771 drm_connector_register(&aconnector->base);
5772 #if defined(CONFIG_DEBUG_FS)
5773 connector_debugfs_init(aconnector);
5774 aconnector->debugfs_dpcd_address = 0;
5775 aconnector->debugfs_dpcd_size = 0;
5778 if (connector_type == DRM_MODE_CONNECTOR_DisplayPort
5779 || connector_type == DRM_MODE_CONNECTOR_eDP)
5780 amdgpu_dm_initialize_dp_connector(dm, aconnector);
5785 aconnector->i2c = NULL;
5790 int amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device *adev)
5792 switch (adev->mode_info.num_crtc) {
5809 static int amdgpu_dm_encoder_init(struct drm_device *dev,
5810 struct amdgpu_encoder *aencoder,
5811 uint32_t link_index)
5813 struct amdgpu_device *adev = dev->dev_private;
5815 int res = drm_encoder_init(dev,
5817 &amdgpu_dm_encoder_funcs,
5818 DRM_MODE_ENCODER_TMDS,
5821 aencoder->base.possible_crtcs = amdgpu_dm_get_encoder_crtc_mask(adev);
5824 aencoder->encoder_id = link_index;
5826 aencoder->encoder_id = -1;
5828 drm_encoder_helper_add(&aencoder->base, &amdgpu_dm_encoder_helper_funcs);
5833 static void manage_dm_interrupts(struct amdgpu_device *adev,
5834 struct amdgpu_crtc *acrtc,
5838 * this is not correct translation but will work as soon as VBLANK
5839 * constant is the same as PFLIP
5842 amdgpu_display_crtc_idx_to_irq_type(
5847 drm_crtc_vblank_on(&acrtc->base);
5850 &adev->pageflip_irq,
5856 &adev->pageflip_irq,
5858 drm_crtc_vblank_off(&acrtc->base);
5863 is_scaling_state_different(const struct dm_connector_state *dm_state,
5864 const struct dm_connector_state *old_dm_state)
5866 if (dm_state->scaling != old_dm_state->scaling)
5868 if (!dm_state->underscan_enable && old_dm_state->underscan_enable) {
5869 if (old_dm_state->underscan_hborder != 0 && old_dm_state->underscan_vborder != 0)
5871 } else if (dm_state->underscan_enable && !old_dm_state->underscan_enable) {
5872 if (dm_state->underscan_hborder != 0 && dm_state->underscan_vborder != 0)
5874 } else if (dm_state->underscan_hborder != old_dm_state->underscan_hborder ||
5875 dm_state->underscan_vborder != old_dm_state->underscan_vborder)
5880 #ifdef CONFIG_DRM_AMD_DC_HDCP
5881 static bool is_content_protection_different(struct drm_connector_state *state,
5882 const struct drm_connector_state *old_state,
5883 const struct drm_connector *connector, struct hdcp_workqueue *hdcp_w)
5885 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
5887 if (old_state->hdcp_content_type != state->hdcp_content_type &&
5888 state->content_protection != DRM_MODE_CONTENT_PROTECTION_UNDESIRED) {
5889 state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
5893 /* CP is being re enabled, ignore this */
5894 if (old_state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED &&
5895 state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED) {
5896 state->content_protection = DRM_MODE_CONTENT_PROTECTION_ENABLED;
5900 /* S3 resume case, since old state will always be 0 (UNDESIRED) and the restored state will be ENABLED */
5901 if (old_state->content_protection == DRM_MODE_CONTENT_PROTECTION_UNDESIRED &&
5902 state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED)
5903 state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
5905 /* Check if something is connected/enabled, otherwise we start hdcp but nothing is connected/enabled
5906 * hot-plug, headless s3, dpms
5908 if (state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED && connector->dpms == DRM_MODE_DPMS_ON &&
5909 aconnector->dc_sink != NULL)
5912 if (old_state->content_protection == state->content_protection)
5915 if (state->content_protection == DRM_MODE_CONTENT_PROTECTION_UNDESIRED)
5922 static void remove_stream(struct amdgpu_device *adev,
5923 struct amdgpu_crtc *acrtc,
5924 struct dc_stream_state *stream)
5926 /* this is the update mode case */
5928 acrtc->otg_inst = -1;
5929 acrtc->enabled = false;
5932 static int get_cursor_position(struct drm_plane *plane, struct drm_crtc *crtc,
5933 struct dc_cursor_position *position)
5935 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
5937 int xorigin = 0, yorigin = 0;
5939 position->enable = false;
5943 if (!crtc || !plane->state->fb)
5946 if ((plane->state->crtc_w > amdgpu_crtc->max_cursor_width) ||
5947 (plane->state->crtc_h > amdgpu_crtc->max_cursor_height)) {
5948 DRM_ERROR("%s: bad cursor width or height %d x %d\n",
5950 plane->state->crtc_w,
5951 plane->state->crtc_h);
5955 x = plane->state->crtc_x;
5956 y = plane->state->crtc_y;
5958 if (x <= -amdgpu_crtc->max_cursor_width ||
5959 y <= -amdgpu_crtc->max_cursor_height)
5962 if (crtc->primary->state) {
5963 /* avivo cursor are offset into the total surface */
5964 x += crtc->primary->state->src_x >> 16;
5965 y += crtc->primary->state->src_y >> 16;
5969 xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
5973 yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
5976 position->enable = true;
5979 position->x_hotspot = xorigin;
5980 position->y_hotspot = yorigin;
5985 static void handle_cursor_update(struct drm_plane *plane,
5986 struct drm_plane_state *old_plane_state)
5988 struct amdgpu_device *adev = plane->dev->dev_private;
5989 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(plane->state->fb);
5990 struct drm_crtc *crtc = afb ? plane->state->crtc : old_plane_state->crtc;
5991 struct dm_crtc_state *crtc_state = crtc ? to_dm_crtc_state(crtc->state) : NULL;
5992 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
5993 uint64_t address = afb ? afb->address : 0;
5994 struct dc_cursor_position position;
5995 struct dc_cursor_attributes attributes;
5998 if (!plane->state->fb && !old_plane_state->fb)
6001 DRM_DEBUG_DRIVER("%s: crtc_id=%d with size %d to %d\n",
6003 amdgpu_crtc->crtc_id,
6004 plane->state->crtc_w,
6005 plane->state->crtc_h);
6007 ret = get_cursor_position(plane, crtc, &position);
6011 if (!position.enable) {
6012 /* turn off cursor */
6013 if (crtc_state && crtc_state->stream) {
6014 mutex_lock(&adev->dm.dc_lock);
6015 dc_stream_set_cursor_position(crtc_state->stream,
6017 mutex_unlock(&adev->dm.dc_lock);
6022 amdgpu_crtc->cursor_width = plane->state->crtc_w;
6023 amdgpu_crtc->cursor_height = plane->state->crtc_h;
6025 memset(&attributes, 0, sizeof(attributes));
6026 attributes.address.high_part = upper_32_bits(address);
6027 attributes.address.low_part = lower_32_bits(address);
6028 attributes.width = plane->state->crtc_w;
6029 attributes.height = plane->state->crtc_h;
6030 attributes.color_format = CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA;
6031 attributes.rotation_angle = 0;
6032 attributes.attribute_flags.value = 0;
6034 attributes.pitch = attributes.width;
6036 if (crtc_state->stream) {
6037 mutex_lock(&adev->dm.dc_lock);
6038 if (!dc_stream_set_cursor_attributes(crtc_state->stream,
6040 DRM_ERROR("DC failed to set cursor attributes\n");
6042 if (!dc_stream_set_cursor_position(crtc_state->stream,
6044 DRM_ERROR("DC failed to set cursor position\n");
6045 mutex_unlock(&adev->dm.dc_lock);
6049 static void prepare_flip_isr(struct amdgpu_crtc *acrtc)
6052 assert_spin_locked(&acrtc->base.dev->event_lock);
6053 WARN_ON(acrtc->event);
6055 acrtc->event = acrtc->base.state->event;
6057 /* Set the flip status */
6058 acrtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
6060 /* Mark this event as consumed */
6061 acrtc->base.state->event = NULL;
6063 DRM_DEBUG_DRIVER("crtc:%d, pflip_stat:AMDGPU_FLIP_SUBMITTED\n",
6067 static void update_freesync_state_on_stream(
6068 struct amdgpu_display_manager *dm,
6069 struct dm_crtc_state *new_crtc_state,
6070 struct dc_stream_state *new_stream,
6071 struct dc_plane_state *surface,
6072 u32 flip_timestamp_in_us)
6074 struct mod_vrr_params vrr_params;
6075 struct dc_info_packet vrr_infopacket = {0};
6076 struct amdgpu_device *adev = dm->adev;
6077 unsigned long flags;
6083 * TODO: Determine why min/max totals and vrefresh can be 0 here.
6084 * For now it's sufficient to just guard against these conditions.
6087 if (!new_stream->timing.h_total || !new_stream->timing.v_total)
6090 spin_lock_irqsave(&adev->ddev->event_lock, flags);
6091 vrr_params = new_crtc_state->vrr_params;
6094 mod_freesync_handle_preflip(
6095 dm->freesync_module,
6098 flip_timestamp_in_us,
6101 if (adev->family < AMDGPU_FAMILY_AI &&
6102 amdgpu_dm_vrr_active(new_crtc_state)) {
6103 mod_freesync_handle_v_update(dm->freesync_module,
6104 new_stream, &vrr_params);
6106 /* Need to call this before the frame ends. */
6107 dc_stream_adjust_vmin_vmax(dm->dc,
6108 new_crtc_state->stream,
6109 &vrr_params.adjust);
6113 mod_freesync_build_vrr_infopacket(
6114 dm->freesync_module,
6118 TRANSFER_FUNC_UNKNOWN,
6121 new_crtc_state->freesync_timing_changed |=
6122 (memcmp(&new_crtc_state->vrr_params.adjust,
6124 sizeof(vrr_params.adjust)) != 0);
6126 new_crtc_state->freesync_vrr_info_changed |=
6127 (memcmp(&new_crtc_state->vrr_infopacket,
6129 sizeof(vrr_infopacket)) != 0);
6131 new_crtc_state->vrr_params = vrr_params;
6132 new_crtc_state->vrr_infopacket = vrr_infopacket;
6134 new_stream->adjust = new_crtc_state->vrr_params.adjust;
6135 new_stream->vrr_infopacket = vrr_infopacket;
6137 if (new_crtc_state->freesync_vrr_info_changed)
6138 DRM_DEBUG_KMS("VRR packet update: crtc=%u enabled=%d state=%d",
6139 new_crtc_state->base.crtc->base.id,
6140 (int)new_crtc_state->base.vrr_enabled,
6141 (int)vrr_params.state);
6143 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
6146 static void pre_update_freesync_state_on_stream(
6147 struct amdgpu_display_manager *dm,
6148 struct dm_crtc_state *new_crtc_state)
6150 struct dc_stream_state *new_stream = new_crtc_state->stream;
6151 struct mod_vrr_params vrr_params;
6152 struct mod_freesync_config config = new_crtc_state->freesync_config;
6153 struct amdgpu_device *adev = dm->adev;
6154 unsigned long flags;
6160 * TODO: Determine why min/max totals and vrefresh can be 0 here.
6161 * For now it's sufficient to just guard against these conditions.
6163 if (!new_stream->timing.h_total || !new_stream->timing.v_total)
6166 spin_lock_irqsave(&adev->ddev->event_lock, flags);
6167 vrr_params = new_crtc_state->vrr_params;
6169 if (new_crtc_state->vrr_supported &&
6170 config.min_refresh_in_uhz &&
6171 config.max_refresh_in_uhz) {
6172 config.state = new_crtc_state->base.vrr_enabled ?
6173 VRR_STATE_ACTIVE_VARIABLE :
6176 config.state = VRR_STATE_UNSUPPORTED;
6179 mod_freesync_build_vrr_params(dm->freesync_module,
6181 &config, &vrr_params);
6183 new_crtc_state->freesync_timing_changed |=
6184 (memcmp(&new_crtc_state->vrr_params.adjust,
6186 sizeof(vrr_params.adjust)) != 0);
6188 new_crtc_state->vrr_params = vrr_params;
6189 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
6192 static void amdgpu_dm_handle_vrr_transition(struct dm_crtc_state *old_state,
6193 struct dm_crtc_state *new_state)
6195 bool old_vrr_active = amdgpu_dm_vrr_active(old_state);
6196 bool new_vrr_active = amdgpu_dm_vrr_active(new_state);
6198 if (!old_vrr_active && new_vrr_active) {
6199 /* Transition VRR inactive -> active:
6200 * While VRR is active, we must not disable vblank irq, as a
6201 * reenable after disable would compute bogus vblank/pflip
6202 * timestamps if it likely happened inside display front-porch.
6204 * We also need vupdate irq for the actual core vblank handling
6207 dm_set_vupdate_irq(new_state->base.crtc, true);
6208 drm_crtc_vblank_get(new_state->base.crtc);
6209 DRM_DEBUG_DRIVER("%s: crtc=%u VRR off->on: Get vblank ref\n",
6210 __func__, new_state->base.crtc->base.id);
6211 } else if (old_vrr_active && !new_vrr_active) {
6212 /* Transition VRR active -> inactive:
6213 * Allow vblank irq disable again for fixed refresh rate.
6215 dm_set_vupdate_irq(new_state->base.crtc, false);
6216 drm_crtc_vblank_put(new_state->base.crtc);
6217 DRM_DEBUG_DRIVER("%s: crtc=%u VRR on->off: Drop vblank ref\n",
6218 __func__, new_state->base.crtc->base.id);
6222 static void amdgpu_dm_commit_cursors(struct drm_atomic_state *state)
6224 struct drm_plane *plane;
6225 struct drm_plane_state *old_plane_state, *new_plane_state;
6229 * TODO: Make this per-stream so we don't issue redundant updates for
6230 * commits with multiple streams.
6232 for_each_oldnew_plane_in_state(state, plane, old_plane_state,
6234 if (plane->type == DRM_PLANE_TYPE_CURSOR)
6235 handle_cursor_update(plane, old_plane_state);
6238 static void amdgpu_dm_commit_planes(struct drm_atomic_state *state,
6239 struct dc_state *dc_state,
6240 struct drm_device *dev,
6241 struct amdgpu_display_manager *dm,
6242 struct drm_crtc *pcrtc,
6243 bool wait_for_vblank)
6246 uint64_t timestamp_ns;
6247 struct drm_plane *plane;
6248 struct drm_plane_state *old_plane_state, *new_plane_state;
6249 struct amdgpu_crtc *acrtc_attach = to_amdgpu_crtc(pcrtc);
6250 struct drm_crtc_state *new_pcrtc_state =
6251 drm_atomic_get_new_crtc_state(state, pcrtc);
6252 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(new_pcrtc_state);
6253 struct dm_crtc_state *dm_old_crtc_state =
6254 to_dm_crtc_state(drm_atomic_get_old_crtc_state(state, pcrtc));
6255 int planes_count = 0, vpos, hpos;
6257 unsigned long flags;
6258 struct amdgpu_bo *abo;
6259 uint64_t tiling_flags;
6260 uint32_t target_vblank, last_flip_vblank;
6261 bool vrr_active = amdgpu_dm_vrr_active(acrtc_state);
6262 bool pflip_present = false;
6263 bool swizzle = true;
6265 struct dc_surface_update surface_updates[MAX_SURFACES];
6266 struct dc_plane_info plane_infos[MAX_SURFACES];
6267 struct dc_scaling_info scaling_infos[MAX_SURFACES];
6268 struct dc_flip_addrs flip_addrs[MAX_SURFACES];
6269 struct dc_stream_update stream_update;
6272 bundle = kzalloc(sizeof(*bundle), GFP_KERNEL);
6275 dm_error("Failed to allocate update bundle\n");
6280 * Disable the cursor first if we're disabling all the planes.
6281 * It'll remain on the screen after the planes are re-enabled
6284 if (acrtc_state->active_planes == 0)
6285 amdgpu_dm_commit_cursors(state);
6287 /* update planes when needed */
6288 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
6289 struct drm_crtc *crtc = new_plane_state->crtc;
6290 struct drm_crtc_state *new_crtc_state;
6291 struct drm_framebuffer *fb = new_plane_state->fb;
6292 bool plane_needs_flip;
6293 struct dc_plane_state *dc_plane;
6294 struct dm_plane_state *dm_new_plane_state = to_dm_plane_state(new_plane_state);
6296 /* Cursor plane is handled after stream updates */
6297 if (plane->type == DRM_PLANE_TYPE_CURSOR)
6300 if (!fb || !crtc || pcrtc != crtc)
6303 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
6304 if (!new_crtc_state->active)
6307 dc_plane = dm_new_plane_state->dc_state;
6309 if (dc_plane && !dc_plane->tiling_info.gfx9.swizzle)
6312 bundle->surface_updates[planes_count].surface = dc_plane;
6313 if (new_pcrtc_state->color_mgmt_changed) {
6314 bundle->surface_updates[planes_count].gamma = dc_plane->gamma_correction;
6315 bundle->surface_updates[planes_count].in_transfer_func = dc_plane->in_transfer_func;
6318 fill_dc_scaling_info(new_plane_state,
6319 &bundle->scaling_infos[planes_count]);
6321 bundle->surface_updates[planes_count].scaling_info =
6322 &bundle->scaling_infos[planes_count];
6324 plane_needs_flip = old_plane_state->fb && new_plane_state->fb;
6326 pflip_present = pflip_present || plane_needs_flip;
6328 if (!plane_needs_flip) {
6333 abo = gem_to_amdgpu_bo(fb->obj[0]);
6336 * Wait for all fences on this FB. Do limited wait to avoid
6337 * deadlock during GPU reset when this fence will not signal
6338 * but we hold reservation lock for the BO.
6340 r = dma_resv_wait_timeout_rcu(abo->tbo.base.resv, true,
6342 msecs_to_jiffies(5000));
6343 if (unlikely(r <= 0))
6344 DRM_ERROR("Waiting for fences timed out!");
6347 * TODO This might fail and hence better not used, wait
6348 * explicitly on fences instead
6349 * and in general should be called for
6350 * blocking commit to as per framework helpers
6352 r = amdgpu_bo_reserve(abo, true);
6353 if (unlikely(r != 0))
6354 DRM_ERROR("failed to reserve buffer before flip\n");
6356 amdgpu_bo_get_tiling_flags(abo, &tiling_flags);
6358 amdgpu_bo_unreserve(abo);
6360 fill_dc_plane_info_and_addr(
6361 dm->adev, new_plane_state, tiling_flags,
6362 &bundle->plane_infos[planes_count],
6363 &bundle->flip_addrs[planes_count].address);
6365 bundle->surface_updates[planes_count].plane_info =
6366 &bundle->plane_infos[planes_count];
6369 * Only allow immediate flips for fast updates that don't
6370 * change FB pitch, DCC state, rotation or mirroing.
6372 bundle->flip_addrs[planes_count].flip_immediate =
6373 crtc->state->async_flip &&
6374 acrtc_state->update_type == UPDATE_TYPE_FAST;
6376 timestamp_ns = ktime_get_ns();
6377 bundle->flip_addrs[planes_count].flip_timestamp_in_us = div_u64(timestamp_ns, 1000);
6378 bundle->surface_updates[planes_count].flip_addr = &bundle->flip_addrs[planes_count];
6379 bundle->surface_updates[planes_count].surface = dc_plane;
6381 if (!bundle->surface_updates[planes_count].surface) {
6382 DRM_ERROR("No surface for CRTC: id=%d\n",
6383 acrtc_attach->crtc_id);
6387 if (plane == pcrtc->primary)
6388 update_freesync_state_on_stream(
6391 acrtc_state->stream,
6393 bundle->flip_addrs[planes_count].flip_timestamp_in_us);
6395 DRM_DEBUG_DRIVER("%s Flipping to hi: 0x%x, low: 0x%x\n",
6397 bundle->flip_addrs[planes_count].address.grph.addr.high_part,
6398 bundle->flip_addrs[planes_count].address.grph.addr.low_part);
6404 if (pflip_present) {
6406 /* Use old throttling in non-vrr fixed refresh rate mode
6407 * to keep flip scheduling based on target vblank counts
6408 * working in a backwards compatible way, e.g., for
6409 * clients using the GLX_OML_sync_control extension or
6410 * DRI3/Present extension with defined target_msc.
6412 last_flip_vblank = amdgpu_get_vblank_counter_kms(dm->ddev, acrtc_attach->crtc_id);
6415 /* For variable refresh rate mode only:
6416 * Get vblank of last completed flip to avoid > 1 vrr
6417 * flips per video frame by use of throttling, but allow
6418 * flip programming anywhere in the possibly large
6419 * variable vrr vblank interval for fine-grained flip
6420 * timing control and more opportunity to avoid stutter
6421 * on late submission of flips.
6423 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
6424 last_flip_vblank = acrtc_attach->last_flip_vblank;
6425 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
6428 target_vblank = last_flip_vblank + wait_for_vblank;
6431 * Wait until we're out of the vertical blank period before the one
6432 * targeted by the flip
6434 while ((acrtc_attach->enabled &&
6435 (amdgpu_display_get_crtc_scanoutpos(dm->ddev, acrtc_attach->crtc_id,
6436 0, &vpos, &hpos, NULL,
6437 NULL, &pcrtc->hwmode)
6438 & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
6439 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
6440 (int)(target_vblank -
6441 amdgpu_get_vblank_counter_kms(dm->ddev, acrtc_attach->crtc_id)) > 0)) {
6442 usleep_range(1000, 1100);
6445 if (acrtc_attach->base.state->event) {
6446 drm_crtc_vblank_get(pcrtc);
6448 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
6450 WARN_ON(acrtc_attach->pflip_status != AMDGPU_FLIP_NONE);
6451 prepare_flip_isr(acrtc_attach);
6453 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
6456 if (acrtc_state->stream) {
6457 if (acrtc_state->freesync_vrr_info_changed)
6458 bundle->stream_update.vrr_infopacket =
6459 &acrtc_state->stream->vrr_infopacket;
6463 /* Update the planes if changed or disable if we don't have any. */
6464 if ((planes_count || acrtc_state->active_planes == 0) &&
6465 acrtc_state->stream) {
6466 bundle->stream_update.stream = acrtc_state->stream;
6467 if (new_pcrtc_state->mode_changed) {
6468 bundle->stream_update.src = acrtc_state->stream->src;
6469 bundle->stream_update.dst = acrtc_state->stream->dst;
6472 if (new_pcrtc_state->color_mgmt_changed) {
6474 * TODO: This isn't fully correct since we've actually
6475 * already modified the stream in place.
6477 bundle->stream_update.gamut_remap =
6478 &acrtc_state->stream->gamut_remap_matrix;
6479 bundle->stream_update.output_csc_transform =
6480 &acrtc_state->stream->csc_color_matrix;
6481 bundle->stream_update.out_transfer_func =
6482 acrtc_state->stream->out_transfer_func;
6485 acrtc_state->stream->abm_level = acrtc_state->abm_level;
6486 if (acrtc_state->abm_level != dm_old_crtc_state->abm_level)
6487 bundle->stream_update.abm_level = &acrtc_state->abm_level;
6490 * If FreeSync state on the stream has changed then we need to
6491 * re-adjust the min/max bounds now that DC doesn't handle this
6492 * as part of commit.
6494 if (amdgpu_dm_vrr_active(dm_old_crtc_state) !=
6495 amdgpu_dm_vrr_active(acrtc_state)) {
6496 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
6497 dc_stream_adjust_vmin_vmax(
6498 dm->dc, acrtc_state->stream,
6499 &acrtc_state->vrr_params.adjust);
6500 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
6502 mutex_lock(&dm->dc_lock);
6503 if ((acrtc_state->update_type > UPDATE_TYPE_FAST) &&
6504 acrtc_state->stream->link->psr_allow_active)
6505 amdgpu_dm_psr_disable(acrtc_state->stream);
6507 dc_commit_updates_for_stream(dm->dc,
6508 bundle->surface_updates,
6510 acrtc_state->stream,
6511 &bundle->stream_update,
6514 if ((acrtc_state->update_type > UPDATE_TYPE_FAST) &&
6515 acrtc_state->stream->psr_version &&
6516 !acrtc_state->stream->link->psr_feature_enabled)
6517 amdgpu_dm_link_setup_psr(acrtc_state->stream);
6518 else if ((acrtc_state->update_type == UPDATE_TYPE_FAST) &&
6519 acrtc_state->stream->link->psr_feature_enabled &&
6520 !acrtc_state->stream->link->psr_allow_active &&
6522 amdgpu_dm_psr_enable(acrtc_state->stream);
6525 mutex_unlock(&dm->dc_lock);
6529 * Update cursor state *after* programming all the planes.
6530 * This avoids redundant programming in the case where we're going
6531 * to be disabling a single plane - those pipes are being disabled.
6533 if (acrtc_state->active_planes)
6534 amdgpu_dm_commit_cursors(state);
6540 static void amdgpu_dm_commit_audio(struct drm_device *dev,
6541 struct drm_atomic_state *state)
6543 struct amdgpu_device *adev = dev->dev_private;
6544 struct amdgpu_dm_connector *aconnector;
6545 struct drm_connector *connector;
6546 struct drm_connector_state *old_con_state, *new_con_state;
6547 struct drm_crtc_state *new_crtc_state;
6548 struct dm_crtc_state *new_dm_crtc_state;
6549 const struct dc_stream_status *status;
6552 /* Notify device removals. */
6553 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
6554 if (old_con_state->crtc != new_con_state->crtc) {
6555 /* CRTC changes require notification. */
6559 if (!new_con_state->crtc)
6562 new_crtc_state = drm_atomic_get_new_crtc_state(
6563 state, new_con_state->crtc);
6565 if (!new_crtc_state)
6568 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
6572 aconnector = to_amdgpu_dm_connector(connector);
6574 mutex_lock(&adev->dm.audio_lock);
6575 inst = aconnector->audio_inst;
6576 aconnector->audio_inst = -1;
6577 mutex_unlock(&adev->dm.audio_lock);
6579 amdgpu_dm_audio_eld_notify(adev, inst);
6582 /* Notify audio device additions. */
6583 for_each_new_connector_in_state(state, connector, new_con_state, i) {
6584 if (!new_con_state->crtc)
6587 new_crtc_state = drm_atomic_get_new_crtc_state(
6588 state, new_con_state->crtc);
6590 if (!new_crtc_state)
6593 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
6596 new_dm_crtc_state = to_dm_crtc_state(new_crtc_state);
6597 if (!new_dm_crtc_state->stream)
6600 status = dc_stream_get_status(new_dm_crtc_state->stream);
6604 aconnector = to_amdgpu_dm_connector(connector);
6606 mutex_lock(&adev->dm.audio_lock);
6607 inst = status->audio_inst;
6608 aconnector->audio_inst = inst;
6609 mutex_unlock(&adev->dm.audio_lock);
6611 amdgpu_dm_audio_eld_notify(adev, inst);
6616 * Enable interrupts on CRTCs that are newly active, undergone
6617 * a modeset, or have active planes again.
6619 * Done in two passes, based on the for_modeset flag:
6620 * Pass 1: For CRTCs going through modeset
6621 * Pass 2: For CRTCs going from 0 to n active planes
6623 * Interrupts can only be enabled after the planes are programmed,
6624 * so this requires a two-pass approach since we don't want to
6625 * just defer the interrupts until after commit planes every time.
6627 static void amdgpu_dm_enable_crtc_interrupts(struct drm_device *dev,
6628 struct drm_atomic_state *state,
6631 struct amdgpu_device *adev = dev->dev_private;
6632 struct drm_crtc *crtc;
6633 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
6635 #ifdef CONFIG_DEBUG_FS
6636 enum amdgpu_dm_pipe_crc_source source;
6639 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
6640 new_crtc_state, i) {
6641 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
6642 struct dm_crtc_state *dm_new_crtc_state =
6643 to_dm_crtc_state(new_crtc_state);
6644 struct dm_crtc_state *dm_old_crtc_state =
6645 to_dm_crtc_state(old_crtc_state);
6646 bool modeset = drm_atomic_crtc_needs_modeset(new_crtc_state);
6649 run_pass = (for_modeset && modeset) ||
6650 (!for_modeset && !modeset &&
6651 !dm_old_crtc_state->interrupts_enabled);
6656 if (!dm_new_crtc_state->interrupts_enabled)
6659 manage_dm_interrupts(adev, acrtc, true);
6661 #ifdef CONFIG_DEBUG_FS
6662 /* The stream has changed so CRC capture needs to re-enabled. */
6663 source = dm_new_crtc_state->crc_src;
6664 if (amdgpu_dm_is_valid_crc_source(source)) {
6665 amdgpu_dm_crtc_configure_crc_source(
6666 crtc, dm_new_crtc_state,
6667 dm_new_crtc_state->crc_src);
6674 * amdgpu_dm_crtc_copy_transient_flags - copy mirrored flags from DRM to DC
6675 * @crtc_state: the DRM CRTC state
6676 * @stream_state: the DC stream state.
6678 * Copy the mirrored transient state flags from DRM, to DC. It is used to bring
6679 * a dc_stream_state's flags in sync with a drm_crtc_state's flags.
6681 static void amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state *crtc_state,
6682 struct dc_stream_state *stream_state)
6684 stream_state->mode_changed = drm_atomic_crtc_needs_modeset(crtc_state);
6687 static int amdgpu_dm_atomic_commit(struct drm_device *dev,
6688 struct drm_atomic_state *state,
6691 struct drm_crtc *crtc;
6692 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
6693 struct amdgpu_device *adev = dev->dev_private;
6697 * We evade vblank and pflip interrupts on CRTCs that are undergoing
6698 * a modeset, being disabled, or have no active planes.
6700 * It's done in atomic commit rather than commit tail for now since
6701 * some of these interrupt handlers access the current CRTC state and
6702 * potentially the stream pointer itself.
6704 * Since the atomic state is swapped within atomic commit and not within
6705 * commit tail this would leave to new state (that hasn't been committed yet)
6706 * being accesssed from within the handlers.
6708 * TODO: Fix this so we can do this in commit tail and not have to block
6711 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
6712 struct dm_crtc_state *dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
6713 struct dm_crtc_state *dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
6714 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
6716 if (dm_old_crtc_state->interrupts_enabled &&
6717 (!dm_new_crtc_state->interrupts_enabled ||
6718 drm_atomic_crtc_needs_modeset(new_crtc_state)))
6719 manage_dm_interrupts(adev, acrtc, false);
6722 * Add check here for SoC's that support hardware cursor plane, to
6723 * unset legacy_cursor_update
6726 return drm_atomic_helper_commit(dev, state, nonblock);
6728 /*TODO Handle EINTR, reenable IRQ*/
6732 * amdgpu_dm_atomic_commit_tail() - AMDgpu DM's commit tail implementation.
6733 * @state: The atomic state to commit
6735 * This will tell DC to commit the constructed DC state from atomic_check,
6736 * programming the hardware. Any failures here implies a hardware failure, since
6737 * atomic check should have filtered anything non-kosher.
6739 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state)
6741 struct drm_device *dev = state->dev;
6742 struct amdgpu_device *adev = dev->dev_private;
6743 struct amdgpu_display_manager *dm = &adev->dm;
6744 struct dm_atomic_state *dm_state;
6745 struct dc_state *dc_state = NULL, *dc_state_temp = NULL;
6747 struct drm_crtc *crtc;
6748 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
6749 unsigned long flags;
6750 bool wait_for_vblank = true;
6751 struct drm_connector *connector;
6752 struct drm_connector_state *old_con_state, *new_con_state;
6753 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
6754 int crtc_disable_count = 0;
6756 drm_atomic_helper_update_legacy_modeset_state(dev, state);
6758 dm_state = dm_atomic_get_new_state(state);
6759 if (dm_state && dm_state->context) {
6760 dc_state = dm_state->context;
6762 /* No state changes, retain current state. */
6763 dc_state_temp = dc_create_state(dm->dc);
6764 ASSERT(dc_state_temp);
6765 dc_state = dc_state_temp;
6766 dc_resource_state_copy_construct_current(dm->dc, dc_state);
6769 /* update changed items */
6770 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
6771 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
6773 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
6774 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
6777 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
6778 "planes_changed:%d, mode_changed:%d,active_changed:%d,"
6779 "connectors_changed:%d\n",
6781 new_crtc_state->enable,
6782 new_crtc_state->active,
6783 new_crtc_state->planes_changed,
6784 new_crtc_state->mode_changed,
6785 new_crtc_state->active_changed,
6786 new_crtc_state->connectors_changed);
6788 /* Copy all transient state flags into dc state */
6789 if (dm_new_crtc_state->stream) {
6790 amdgpu_dm_crtc_copy_transient_flags(&dm_new_crtc_state->base,
6791 dm_new_crtc_state->stream);
6794 /* handles headless hotplug case, updating new_state and
6795 * aconnector as needed
6798 if (modeset_required(new_crtc_state, dm_new_crtc_state->stream, dm_old_crtc_state->stream)) {
6800 DRM_DEBUG_DRIVER("Atomic commit: SET crtc id %d: [%p]\n", acrtc->crtc_id, acrtc);
6802 if (!dm_new_crtc_state->stream) {
6804 * this could happen because of issues with
6805 * userspace notifications delivery.
6806 * In this case userspace tries to set mode on
6807 * display which is disconnected in fact.
6808 * dc_sink is NULL in this case on aconnector.
6809 * We expect reset mode will come soon.
6811 * This can also happen when unplug is done
6812 * during resume sequence ended
6814 * In this case, we want to pretend we still
6815 * have a sink to keep the pipe running so that
6816 * hw state is consistent with the sw state
6818 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
6819 __func__, acrtc->base.base.id);
6823 if (dm_old_crtc_state->stream)
6824 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
6826 pm_runtime_get_noresume(dev->dev);
6828 acrtc->enabled = true;
6829 acrtc->hw_mode = new_crtc_state->mode;
6830 crtc->hwmode = new_crtc_state->mode;
6831 } else if (modereset_required(new_crtc_state)) {
6832 DRM_DEBUG_DRIVER("Atomic commit: RESET. crtc id %d:[%p]\n", acrtc->crtc_id, acrtc);
6833 /* i.e. reset mode */
6834 if (dm_old_crtc_state->stream) {
6835 if (dm_old_crtc_state->stream->link->psr_allow_active)
6836 amdgpu_dm_psr_disable(dm_old_crtc_state->stream);
6838 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
6841 } /* for_each_crtc_in_state() */
6844 dm_enable_per_frame_crtc_master_sync(dc_state);
6845 mutex_lock(&dm->dc_lock);
6846 WARN_ON(!dc_commit_state(dm->dc, dc_state));
6847 mutex_unlock(&dm->dc_lock);
6850 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
6851 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
6853 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
6855 if (dm_new_crtc_state->stream != NULL) {
6856 const struct dc_stream_status *status =
6857 dc_stream_get_status(dm_new_crtc_state->stream);
6860 status = dc_stream_get_status_from_state(dc_state,
6861 dm_new_crtc_state->stream);
6864 DC_ERR("got no status for stream %p on acrtc%p\n", dm_new_crtc_state->stream, acrtc);
6866 acrtc->otg_inst = status->primary_otg_inst;
6869 #ifdef CONFIG_DRM_AMD_DC_HDCP
6870 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
6871 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
6872 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
6873 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
6875 new_crtc_state = NULL;
6878 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
6880 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
6882 if (dm_new_crtc_state && dm_new_crtc_state->stream == NULL &&
6883 connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
6884 hdcp_reset_display(adev->dm.hdcp_workqueue, aconnector->dc_link->link_index);
6885 new_con_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
6889 if (is_content_protection_different(new_con_state, old_con_state, connector, adev->dm.hdcp_workqueue))
6890 hdcp_update_display(
6891 adev->dm.hdcp_workqueue, aconnector->dc_link->link_index, aconnector,
6892 new_con_state->hdcp_content_type,
6893 new_con_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED ? true
6898 /* Handle connector state changes */
6899 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
6900 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
6901 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
6902 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
6903 struct dc_surface_update dummy_updates[MAX_SURFACES];
6904 struct dc_stream_update stream_update;
6905 struct dc_info_packet hdr_packet;
6906 struct dc_stream_status *status = NULL;
6907 bool abm_changed, hdr_changed, scaling_changed;
6909 memset(&dummy_updates, 0, sizeof(dummy_updates));
6910 memset(&stream_update, 0, sizeof(stream_update));
6913 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
6914 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
6917 /* Skip any modesets/resets */
6918 if (!acrtc || drm_atomic_crtc_needs_modeset(new_crtc_state))
6921 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
6922 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
6924 scaling_changed = is_scaling_state_different(dm_new_con_state,
6927 abm_changed = dm_new_crtc_state->abm_level !=
6928 dm_old_crtc_state->abm_level;
6931 is_hdr_metadata_different(old_con_state, new_con_state);
6933 if (!scaling_changed && !abm_changed && !hdr_changed)
6936 stream_update.stream = dm_new_crtc_state->stream;
6937 if (scaling_changed) {
6938 update_stream_scaling_settings(&dm_new_con_state->base.crtc->mode,
6939 dm_new_con_state, dm_new_crtc_state->stream);
6941 stream_update.src = dm_new_crtc_state->stream->src;
6942 stream_update.dst = dm_new_crtc_state->stream->dst;
6946 dm_new_crtc_state->stream->abm_level = dm_new_crtc_state->abm_level;
6948 stream_update.abm_level = &dm_new_crtc_state->abm_level;
6952 fill_hdr_info_packet(new_con_state, &hdr_packet);
6953 stream_update.hdr_static_metadata = &hdr_packet;
6956 status = dc_stream_get_status(dm_new_crtc_state->stream);
6958 WARN_ON(!status->plane_count);
6961 * TODO: DC refuses to perform stream updates without a dc_surface_update.
6962 * Here we create an empty update on each plane.
6963 * To fix this, DC should permit updating only stream properties.
6965 for (j = 0; j < status->plane_count; j++)
6966 dummy_updates[j].surface = status->plane_states[0];
6969 mutex_lock(&dm->dc_lock);
6970 dc_commit_updates_for_stream(dm->dc,
6972 status->plane_count,
6973 dm_new_crtc_state->stream,
6976 mutex_unlock(&dm->dc_lock);
6979 /* Count number of newly disabled CRTCs for dropping PM refs later. */
6980 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
6981 new_crtc_state, i) {
6982 if (old_crtc_state->active && !new_crtc_state->active)
6983 crtc_disable_count++;
6985 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
6986 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
6988 /* Update freesync active state. */
6989 pre_update_freesync_state_on_stream(dm, dm_new_crtc_state);
6991 /* Handle vrr on->off / off->on transitions */
6992 amdgpu_dm_handle_vrr_transition(dm_old_crtc_state,
6996 /* Enable interrupts for CRTCs going through a modeset. */
6997 amdgpu_dm_enable_crtc_interrupts(dev, state, true);
6999 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j)
7000 if (new_crtc_state->async_flip)
7001 wait_for_vblank = false;
7003 /* update planes when needed per crtc*/
7004 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j) {
7005 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
7007 if (dm_new_crtc_state->stream)
7008 amdgpu_dm_commit_planes(state, dc_state, dev,
7009 dm, crtc, wait_for_vblank);
7012 /* Enable interrupts for CRTCs going from 0 to n active planes. */
7013 amdgpu_dm_enable_crtc_interrupts(dev, state, false);
7015 /* Update audio instances for each connector. */
7016 amdgpu_dm_commit_audio(dev, state);
7019 * send vblank event on all events not handled in flip and
7020 * mark consumed event for drm_atomic_helper_commit_hw_done
7022 spin_lock_irqsave(&adev->ddev->event_lock, flags);
7023 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
7025 if (new_crtc_state->event)
7026 drm_send_event_locked(dev, &new_crtc_state->event->base);
7028 new_crtc_state->event = NULL;
7030 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
7032 /* Signal HW programming completion */
7033 drm_atomic_helper_commit_hw_done(state);
7035 if (wait_for_vblank)
7036 drm_atomic_helper_wait_for_flip_done(dev, state);
7038 drm_atomic_helper_cleanup_planes(dev, state);
7041 * Finally, drop a runtime PM reference for each newly disabled CRTC,
7042 * so we can put the GPU into runtime suspend if we're not driving any
7045 for (i = 0; i < crtc_disable_count; i++)
7046 pm_runtime_put_autosuspend(dev->dev);
7047 pm_runtime_mark_last_busy(dev->dev);
7050 dc_release_state(dc_state_temp);
7054 static int dm_force_atomic_commit(struct drm_connector *connector)
7057 struct drm_device *ddev = connector->dev;
7058 struct drm_atomic_state *state = drm_atomic_state_alloc(ddev);
7059 struct amdgpu_crtc *disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
7060 struct drm_plane *plane = disconnected_acrtc->base.primary;
7061 struct drm_connector_state *conn_state;
7062 struct drm_crtc_state *crtc_state;
7063 struct drm_plane_state *plane_state;
7068 state->acquire_ctx = ddev->mode_config.acquire_ctx;
7070 /* Construct an atomic state to restore previous display setting */
7073 * Attach connectors to drm_atomic_state
7075 conn_state = drm_atomic_get_connector_state(state, connector);
7077 ret = PTR_ERR_OR_ZERO(conn_state);
7081 /* Attach crtc to drm_atomic_state*/
7082 crtc_state = drm_atomic_get_crtc_state(state, &disconnected_acrtc->base);
7084 ret = PTR_ERR_OR_ZERO(crtc_state);
7088 /* force a restore */
7089 crtc_state->mode_changed = true;
7091 /* Attach plane to drm_atomic_state */
7092 plane_state = drm_atomic_get_plane_state(state, plane);
7094 ret = PTR_ERR_OR_ZERO(plane_state);
7099 /* Call commit internally with the state we just constructed */
7100 ret = drm_atomic_commit(state);
7105 DRM_ERROR("Restoring old state failed with %i\n", ret);
7106 drm_atomic_state_put(state);
7112 * This function handles all cases when set mode does not come upon hotplug.
7113 * This includes when a display is unplugged then plugged back into the
7114 * same port and when running without usermode desktop manager supprot
7116 void dm_restore_drm_connector_state(struct drm_device *dev,
7117 struct drm_connector *connector)
7119 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
7120 struct amdgpu_crtc *disconnected_acrtc;
7121 struct dm_crtc_state *acrtc_state;
7123 if (!aconnector->dc_sink || !connector->state || !connector->encoder)
7126 disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
7127 if (!disconnected_acrtc)
7130 acrtc_state = to_dm_crtc_state(disconnected_acrtc->base.state);
7131 if (!acrtc_state->stream)
7135 * If the previous sink is not released and different from the current,
7136 * we deduce we are in a state where we can not rely on usermode call
7137 * to turn on the display, so we do it here
7139 if (acrtc_state->stream->sink != aconnector->dc_sink)
7140 dm_force_atomic_commit(&aconnector->base);
7144 * Grabs all modesetting locks to serialize against any blocking commits,
7145 * Waits for completion of all non blocking commits.
7147 static int do_aquire_global_lock(struct drm_device *dev,
7148 struct drm_atomic_state *state)
7150 struct drm_crtc *crtc;
7151 struct drm_crtc_commit *commit;
7155 * Adding all modeset locks to aquire_ctx will
7156 * ensure that when the framework release it the
7157 * extra locks we are locking here will get released to
7159 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
7163 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7164 spin_lock(&crtc->commit_lock);
7165 commit = list_first_entry_or_null(&crtc->commit_list,
7166 struct drm_crtc_commit, commit_entry);
7168 drm_crtc_commit_get(commit);
7169 spin_unlock(&crtc->commit_lock);
7175 * Make sure all pending HW programming completed and
7178 ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ);
7181 ret = wait_for_completion_interruptible_timeout(
7182 &commit->flip_done, 10*HZ);
7185 DRM_ERROR("[CRTC:%d:%s] hw_done or flip_done "
7186 "timed out\n", crtc->base.id, crtc->name);
7188 drm_crtc_commit_put(commit);
7191 return ret < 0 ? ret : 0;
7194 static void get_freesync_config_for_crtc(
7195 struct dm_crtc_state *new_crtc_state,
7196 struct dm_connector_state *new_con_state)
7198 struct mod_freesync_config config = {0};
7199 struct amdgpu_dm_connector *aconnector =
7200 to_amdgpu_dm_connector(new_con_state->base.connector);
7201 struct drm_display_mode *mode = &new_crtc_state->base.mode;
7202 int vrefresh = drm_mode_vrefresh(mode);
7204 new_crtc_state->vrr_supported = new_con_state->freesync_capable &&
7205 vrefresh >= aconnector->min_vfreq &&
7206 vrefresh <= aconnector->max_vfreq;
7208 if (new_crtc_state->vrr_supported) {
7209 new_crtc_state->stream->ignore_msa_timing_param = true;
7210 config.state = new_crtc_state->base.vrr_enabled ?
7211 VRR_STATE_ACTIVE_VARIABLE :
7213 config.min_refresh_in_uhz =
7214 aconnector->min_vfreq * 1000000;
7215 config.max_refresh_in_uhz =
7216 aconnector->max_vfreq * 1000000;
7217 config.vsif_supported = true;
7221 new_crtc_state->freesync_config = config;
7224 static void reset_freesync_config_for_crtc(
7225 struct dm_crtc_state *new_crtc_state)
7227 new_crtc_state->vrr_supported = false;
7229 memset(&new_crtc_state->vrr_params, 0,
7230 sizeof(new_crtc_state->vrr_params));
7231 memset(&new_crtc_state->vrr_infopacket, 0,
7232 sizeof(new_crtc_state->vrr_infopacket));
7235 static int dm_update_crtc_state(struct amdgpu_display_manager *dm,
7236 struct drm_atomic_state *state,
7237 struct drm_crtc *crtc,
7238 struct drm_crtc_state *old_crtc_state,
7239 struct drm_crtc_state *new_crtc_state,
7241 bool *lock_and_validation_needed)
7243 struct dm_atomic_state *dm_state = NULL;
7244 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
7245 struct dc_stream_state *new_stream;
7249 * TODO Move this code into dm_crtc_atomic_check once we get rid of dc_validation_set
7250 * update changed items
7252 struct amdgpu_crtc *acrtc = NULL;
7253 struct amdgpu_dm_connector *aconnector = NULL;
7254 struct drm_connector_state *drm_new_conn_state = NULL, *drm_old_conn_state = NULL;
7255 struct dm_connector_state *dm_new_conn_state = NULL, *dm_old_conn_state = NULL;
7259 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
7260 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
7261 acrtc = to_amdgpu_crtc(crtc);
7262 aconnector = amdgpu_dm_find_first_crtc_matching_connector(state, crtc);
7264 /* TODO This hack should go away */
7265 if (aconnector && enable) {
7266 /* Make sure fake sink is created in plug-in scenario */
7267 drm_new_conn_state = drm_atomic_get_new_connector_state(state,
7269 drm_old_conn_state = drm_atomic_get_old_connector_state(state,
7272 if (IS_ERR(drm_new_conn_state)) {
7273 ret = PTR_ERR_OR_ZERO(drm_new_conn_state);
7277 dm_new_conn_state = to_dm_connector_state(drm_new_conn_state);
7278 dm_old_conn_state = to_dm_connector_state(drm_old_conn_state);
7280 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
7283 new_stream = create_stream_for_sink(aconnector,
7284 &new_crtc_state->mode,
7286 dm_old_crtc_state->stream);
7289 * we can have no stream on ACTION_SET if a display
7290 * was disconnected during S3, in this case it is not an
7291 * error, the OS will be updated after detection, and
7292 * will do the right thing on next atomic commit
7296 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
7297 __func__, acrtc->base.base.id);
7302 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
7304 ret = fill_hdr_info_packet(drm_new_conn_state,
7305 &new_stream->hdr_static_metadata);
7310 * If we already removed the old stream from the context
7311 * (and set the new stream to NULL) then we can't reuse
7312 * the old stream even if the stream and scaling are unchanged.
7313 * We'll hit the BUG_ON and black screen.
7315 * TODO: Refactor this function to allow this check to work
7316 * in all conditions.
7318 if (dm_new_crtc_state->stream &&
7319 dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) &&
7320 dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream)) {
7321 new_crtc_state->mode_changed = false;
7322 DRM_DEBUG_DRIVER("Mode change not required, setting mode_changed to %d",
7323 new_crtc_state->mode_changed);
7327 /* mode_changed flag may get updated above, need to check again */
7328 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
7332 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
7333 "planes_changed:%d, mode_changed:%d,active_changed:%d,"
7334 "connectors_changed:%d\n",
7336 new_crtc_state->enable,
7337 new_crtc_state->active,
7338 new_crtc_state->planes_changed,
7339 new_crtc_state->mode_changed,
7340 new_crtc_state->active_changed,
7341 new_crtc_state->connectors_changed);
7343 /* Remove stream for any changed/disabled CRTC */
7346 if (!dm_old_crtc_state->stream)
7349 ret = dm_atomic_get_state(state, &dm_state);
7353 DRM_DEBUG_DRIVER("Disabling DRM crtc: %d\n",
7356 /* i.e. reset mode */
7357 if (dc_remove_stream_from_ctx(
7360 dm_old_crtc_state->stream) != DC_OK) {
7365 dc_stream_release(dm_old_crtc_state->stream);
7366 dm_new_crtc_state->stream = NULL;
7368 reset_freesync_config_for_crtc(dm_new_crtc_state);
7370 *lock_and_validation_needed = true;
7372 } else {/* Add stream for any updated/enabled CRTC */
7374 * Quick fix to prevent NULL pointer on new_stream when
7375 * added MST connectors not found in existing crtc_state in the chained mode
7376 * TODO: need to dig out the root cause of that
7378 if (!aconnector || (!aconnector->dc_sink && aconnector->mst_port))
7381 if (modereset_required(new_crtc_state))
7384 if (modeset_required(new_crtc_state, new_stream,
7385 dm_old_crtc_state->stream)) {
7387 WARN_ON(dm_new_crtc_state->stream);
7389 ret = dm_atomic_get_state(state, &dm_state);
7393 dm_new_crtc_state->stream = new_stream;
7395 dc_stream_retain(new_stream);
7397 DRM_DEBUG_DRIVER("Enabling DRM crtc: %d\n",
7400 if (dc_add_stream_to_ctx(
7403 dm_new_crtc_state->stream) != DC_OK) {
7408 *lock_and_validation_needed = true;
7413 /* Release extra reference */
7415 dc_stream_release(new_stream);
7418 * We want to do dc stream updates that do not require a
7419 * full modeset below.
7421 if (!(enable && aconnector && new_crtc_state->enable &&
7422 new_crtc_state->active))
7425 * Given above conditions, the dc state cannot be NULL because:
7426 * 1. We're in the process of enabling CRTCs (just been added
7427 * to the dc context, or already is on the context)
7428 * 2. Has a valid connector attached, and
7429 * 3. Is currently active and enabled.
7430 * => The dc stream state currently exists.
7432 BUG_ON(dm_new_crtc_state->stream == NULL);
7434 /* Scaling or underscan settings */
7435 if (is_scaling_state_different(dm_old_conn_state, dm_new_conn_state))
7436 update_stream_scaling_settings(
7437 &new_crtc_state->mode, dm_new_conn_state, dm_new_crtc_state->stream);
7440 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
7443 * Color management settings. We also update color properties
7444 * when a modeset is needed, to ensure it gets reprogrammed.
7446 if (dm_new_crtc_state->base.color_mgmt_changed ||
7447 drm_atomic_crtc_needs_modeset(new_crtc_state)) {
7448 ret = amdgpu_dm_update_crtc_color_mgmt(dm_new_crtc_state);
7453 /* Update Freesync settings. */
7454 get_freesync_config_for_crtc(dm_new_crtc_state,
7461 dc_stream_release(new_stream);
7465 static bool should_reset_plane(struct drm_atomic_state *state,
7466 struct drm_plane *plane,
7467 struct drm_plane_state *old_plane_state,
7468 struct drm_plane_state *new_plane_state)
7470 struct drm_plane *other;
7471 struct drm_plane_state *old_other_state, *new_other_state;
7472 struct drm_crtc_state *new_crtc_state;
7476 * TODO: Remove this hack once the checks below are sufficient
7477 * enough to determine when we need to reset all the planes on
7480 if (state->allow_modeset)
7483 /* Exit early if we know that we're adding or removing the plane. */
7484 if (old_plane_state->crtc != new_plane_state->crtc)
7487 /* old crtc == new_crtc == NULL, plane not in context. */
7488 if (!new_plane_state->crtc)
7492 drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
7494 if (!new_crtc_state)
7497 /* CRTC Degamma changes currently require us to recreate planes. */
7498 if (new_crtc_state->color_mgmt_changed)
7501 if (drm_atomic_crtc_needs_modeset(new_crtc_state))
7505 * If there are any new primary or overlay planes being added or
7506 * removed then the z-order can potentially change. To ensure
7507 * correct z-order and pipe acquisition the current DC architecture
7508 * requires us to remove and recreate all existing planes.
7510 * TODO: Come up with a more elegant solution for this.
7512 for_each_oldnew_plane_in_state(state, other, old_other_state, new_other_state, i) {
7513 if (other->type == DRM_PLANE_TYPE_CURSOR)
7516 if (old_other_state->crtc != new_plane_state->crtc &&
7517 new_other_state->crtc != new_plane_state->crtc)
7520 if (old_other_state->crtc != new_other_state->crtc)
7523 /* TODO: Remove this once we can handle fast format changes. */
7524 if (old_other_state->fb && new_other_state->fb &&
7525 old_other_state->fb->format != new_other_state->fb->format)
7532 static int dm_update_plane_state(struct dc *dc,
7533 struct drm_atomic_state *state,
7534 struct drm_plane *plane,
7535 struct drm_plane_state *old_plane_state,
7536 struct drm_plane_state *new_plane_state,
7538 bool *lock_and_validation_needed)
7541 struct dm_atomic_state *dm_state = NULL;
7542 struct drm_crtc *new_plane_crtc, *old_plane_crtc;
7543 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
7544 struct dm_crtc_state *dm_new_crtc_state, *dm_old_crtc_state;
7545 struct dm_plane_state *dm_new_plane_state, *dm_old_plane_state;
7550 new_plane_crtc = new_plane_state->crtc;
7551 old_plane_crtc = old_plane_state->crtc;
7552 dm_new_plane_state = to_dm_plane_state(new_plane_state);
7553 dm_old_plane_state = to_dm_plane_state(old_plane_state);
7555 /*TODO Implement atomic check for cursor plane */
7556 if (plane->type == DRM_PLANE_TYPE_CURSOR)
7559 needs_reset = should_reset_plane(state, plane, old_plane_state,
7562 /* Remove any changed/removed planes */
7567 if (!old_plane_crtc)
7570 old_crtc_state = drm_atomic_get_old_crtc_state(
7571 state, old_plane_crtc);
7572 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
7574 if (!dm_old_crtc_state->stream)
7577 DRM_DEBUG_ATOMIC("Disabling DRM plane: %d on DRM crtc %d\n",
7578 plane->base.id, old_plane_crtc->base.id);
7580 ret = dm_atomic_get_state(state, &dm_state);
7584 if (!dc_remove_plane_from_context(
7586 dm_old_crtc_state->stream,
7587 dm_old_plane_state->dc_state,
7588 dm_state->context)) {
7595 dc_plane_state_release(dm_old_plane_state->dc_state);
7596 dm_new_plane_state->dc_state = NULL;
7598 *lock_and_validation_needed = true;
7600 } else { /* Add new planes */
7601 struct dc_plane_state *dc_new_plane_state;
7603 if (drm_atomic_plane_disabling(plane->state, new_plane_state))
7606 if (!new_plane_crtc)
7609 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_crtc);
7610 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
7612 if (!dm_new_crtc_state->stream)
7618 WARN_ON(dm_new_plane_state->dc_state);
7620 dc_new_plane_state = dc_create_plane_state(dc);
7621 if (!dc_new_plane_state)
7624 DRM_DEBUG_DRIVER("Enabling DRM plane: %d on DRM crtc %d\n",
7625 plane->base.id, new_plane_crtc->base.id);
7627 ret = fill_dc_plane_attributes(
7628 new_plane_crtc->dev->dev_private,
7633 dc_plane_state_release(dc_new_plane_state);
7637 ret = dm_atomic_get_state(state, &dm_state);
7639 dc_plane_state_release(dc_new_plane_state);
7644 * Any atomic check errors that occur after this will
7645 * not need a release. The plane state will be attached
7646 * to the stream, and therefore part of the atomic
7647 * state. It'll be released when the atomic state is
7650 if (!dc_add_plane_to_context(
7652 dm_new_crtc_state->stream,
7654 dm_state->context)) {
7656 dc_plane_state_release(dc_new_plane_state);
7660 dm_new_plane_state->dc_state = dc_new_plane_state;
7662 /* Tell DC to do a full surface update every time there
7663 * is a plane change. Inefficient, but works for now.
7665 dm_new_plane_state->dc_state->update_flags.bits.full_update = 1;
7667 *lock_and_validation_needed = true;
7675 dm_determine_update_type_for_commit(struct amdgpu_display_manager *dm,
7676 struct drm_atomic_state *state,
7677 enum surface_update_type *out_type)
7679 struct dc *dc = dm->dc;
7680 struct dm_atomic_state *dm_state = NULL, *old_dm_state = NULL;
7681 int i, j, num_plane, ret = 0;
7682 struct drm_plane_state *old_plane_state, *new_plane_state;
7683 struct dm_plane_state *new_dm_plane_state, *old_dm_plane_state;
7684 struct drm_crtc *new_plane_crtc;
7685 struct drm_plane *plane;
7687 struct drm_crtc *crtc;
7688 struct drm_crtc_state *new_crtc_state, *old_crtc_state;
7689 struct dm_crtc_state *new_dm_crtc_state, *old_dm_crtc_state;
7690 struct dc_stream_status *status = NULL;
7692 struct dc_surface_update *updates;
7693 enum surface_update_type update_type = UPDATE_TYPE_FAST;
7695 updates = kcalloc(MAX_SURFACES, sizeof(*updates), GFP_KERNEL);
7698 DRM_ERROR("Failed to allocate plane updates\n");
7699 /* Set type to FULL to avoid crashing in DC*/
7700 update_type = UPDATE_TYPE_FULL;
7704 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
7705 struct dc_scaling_info scaling_info;
7706 struct dc_stream_update stream_update;
7708 memset(&stream_update, 0, sizeof(stream_update));
7710 new_dm_crtc_state = to_dm_crtc_state(new_crtc_state);
7711 old_dm_crtc_state = to_dm_crtc_state(old_crtc_state);
7714 if (new_dm_crtc_state->stream != old_dm_crtc_state->stream) {
7715 update_type = UPDATE_TYPE_FULL;
7719 if (!new_dm_crtc_state->stream)
7722 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, j) {
7723 const struct amdgpu_framebuffer *amdgpu_fb =
7724 to_amdgpu_framebuffer(new_plane_state->fb);
7725 struct dc_plane_info plane_info;
7726 struct dc_flip_addrs flip_addr;
7727 uint64_t tiling_flags;
7729 new_plane_crtc = new_plane_state->crtc;
7730 new_dm_plane_state = to_dm_plane_state(new_plane_state);
7731 old_dm_plane_state = to_dm_plane_state(old_plane_state);
7733 if (plane->type == DRM_PLANE_TYPE_CURSOR)
7736 if (new_dm_plane_state->dc_state != old_dm_plane_state->dc_state) {
7737 update_type = UPDATE_TYPE_FULL;
7741 if (crtc != new_plane_crtc)
7744 updates[num_plane].surface = new_dm_plane_state->dc_state;
7746 if (new_crtc_state->mode_changed) {
7747 stream_update.dst = new_dm_crtc_state->stream->dst;
7748 stream_update.src = new_dm_crtc_state->stream->src;
7751 if (new_crtc_state->color_mgmt_changed) {
7752 updates[num_plane].gamma =
7753 new_dm_plane_state->dc_state->gamma_correction;
7754 updates[num_plane].in_transfer_func =
7755 new_dm_plane_state->dc_state->in_transfer_func;
7756 stream_update.gamut_remap =
7757 &new_dm_crtc_state->stream->gamut_remap_matrix;
7758 stream_update.output_csc_transform =
7759 &new_dm_crtc_state->stream->csc_color_matrix;
7760 stream_update.out_transfer_func =
7761 new_dm_crtc_state->stream->out_transfer_func;
7764 ret = fill_dc_scaling_info(new_plane_state,
7769 updates[num_plane].scaling_info = &scaling_info;
7772 ret = get_fb_info(amdgpu_fb, &tiling_flags);
7776 memset(&flip_addr, 0, sizeof(flip_addr));
7778 ret = fill_dc_plane_info_and_addr(
7779 dm->adev, new_plane_state, tiling_flags,
7781 &flip_addr.address);
7785 updates[num_plane].plane_info = &plane_info;
7786 updates[num_plane].flip_addr = &flip_addr;
7795 ret = dm_atomic_get_state(state, &dm_state);
7799 old_dm_state = dm_atomic_get_old_state(state);
7800 if (!old_dm_state) {
7805 status = dc_stream_get_status_from_state(old_dm_state->context,
7806 new_dm_crtc_state->stream);
7807 stream_update.stream = new_dm_crtc_state->stream;
7809 * TODO: DC modifies the surface during this call so we need
7810 * to lock here - find a way to do this without locking.
7812 mutex_lock(&dm->dc_lock);
7813 update_type = dc_check_update_surfaces_for_stream(dc, updates, num_plane,
7814 &stream_update, status);
7815 mutex_unlock(&dm->dc_lock);
7817 if (update_type > UPDATE_TYPE_MED) {
7818 update_type = UPDATE_TYPE_FULL;
7826 *out_type = update_type;
7831 * amdgpu_dm_atomic_check() - Atomic check implementation for AMDgpu DM.
7832 * @dev: The DRM device
7833 * @state: The atomic state to commit
7835 * Validate that the given atomic state is programmable by DC into hardware.
7836 * This involves constructing a &struct dc_state reflecting the new hardware
7837 * state we wish to commit, then querying DC to see if it is programmable. It's
7838 * important not to modify the existing DC state. Otherwise, atomic_check
7839 * may unexpectedly commit hardware changes.
7841 * When validating the DC state, it's important that the right locks are
7842 * acquired. For full updates case which removes/adds/updates streams on one
7843 * CRTC while flipping on another CRTC, acquiring global lock will guarantee
7844 * that any such full update commit will wait for completion of any outstanding
7845 * flip using DRMs synchronization events. See
7846 * dm_determine_update_type_for_commit()
7848 * Note that DM adds the affected connectors for all CRTCs in state, when that
7849 * might not seem necessary. This is because DC stream creation requires the
7850 * DC sink, which is tied to the DRM connector state. Cleaning this up should
7851 * be possible but non-trivial - a possible TODO item.
7853 * Return: -Error code if validation failed.
7855 static int amdgpu_dm_atomic_check(struct drm_device *dev,
7856 struct drm_atomic_state *state)
7858 struct amdgpu_device *adev = dev->dev_private;
7859 struct dm_atomic_state *dm_state = NULL;
7860 struct dc *dc = adev->dm.dc;
7861 struct drm_connector *connector;
7862 struct drm_connector_state *old_con_state, *new_con_state;
7863 struct drm_crtc *crtc;
7864 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
7865 struct drm_plane *plane;
7866 struct drm_plane_state *old_plane_state, *new_plane_state;
7867 enum surface_update_type update_type = UPDATE_TYPE_FAST;
7868 enum surface_update_type overall_update_type = UPDATE_TYPE_FAST;
7873 * This bool will be set for true for any modeset/reset
7874 * or plane update which implies non fast surface update.
7876 bool lock_and_validation_needed = false;
7878 ret = drm_atomic_helper_check_modeset(dev, state);
7882 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
7883 if (!drm_atomic_crtc_needs_modeset(new_crtc_state) &&
7884 !new_crtc_state->color_mgmt_changed &&
7885 old_crtc_state->vrr_enabled == new_crtc_state->vrr_enabled)
7888 if (!new_crtc_state->enable)
7891 ret = drm_atomic_add_affected_connectors(state, crtc);
7895 ret = drm_atomic_add_affected_planes(state, crtc);
7901 * Add all primary and overlay planes on the CRTC to the state
7902 * whenever a plane is enabled to maintain correct z-ordering
7903 * and to enable fast surface updates.
7905 drm_for_each_crtc(crtc, dev) {
7906 bool modified = false;
7908 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
7909 if (plane->type == DRM_PLANE_TYPE_CURSOR)
7912 if (new_plane_state->crtc == crtc ||
7913 old_plane_state->crtc == crtc) {
7922 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
7923 if (plane->type == DRM_PLANE_TYPE_CURSOR)
7927 drm_atomic_get_plane_state(state, plane);
7929 if (IS_ERR(new_plane_state)) {
7930 ret = PTR_ERR(new_plane_state);
7936 /* Remove exiting planes if they are modified */
7937 for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) {
7938 ret = dm_update_plane_state(dc, state, plane,
7942 &lock_and_validation_needed);
7947 /* Disable all crtcs which require disable */
7948 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
7949 ret = dm_update_crtc_state(&adev->dm, state, crtc,
7953 &lock_and_validation_needed);
7958 /* Enable all crtcs which require enable */
7959 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
7960 ret = dm_update_crtc_state(&adev->dm, state, crtc,
7964 &lock_and_validation_needed);
7969 /* Add new/modified planes */
7970 for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) {
7971 ret = dm_update_plane_state(dc, state, plane,
7975 &lock_and_validation_needed);
7980 /* Run this here since we want to validate the streams we created */
7981 ret = drm_atomic_helper_check_planes(dev, state);
7985 /* Perform validation of MST topology in the state*/
7986 ret = drm_dp_mst_atomic_check(state);
7990 if (state->legacy_cursor_update) {
7992 * This is a fast cursor update coming from the plane update
7993 * helper, check if it can be done asynchronously for better
7996 state->async_update =
7997 !drm_atomic_helper_async_check(dev, state);
8000 * Skip the remaining global validation if this is an async
8001 * update. Cursor updates can be done without affecting
8002 * state or bandwidth calcs and this avoids the performance
8003 * penalty of locking the private state object and
8004 * allocating a new dc_state.
8006 if (state->async_update)
8010 /* Check scaling and underscan changes*/
8011 /* TODO Removed scaling changes validation due to inability to commit
8012 * new stream into context w\o causing full reset. Need to
8013 * decide how to handle.
8015 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
8016 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
8017 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
8018 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
8020 /* Skip any modesets/resets */
8021 if (!acrtc || drm_atomic_crtc_needs_modeset(
8022 drm_atomic_get_new_crtc_state(state, &acrtc->base)))
8025 /* Skip any thing not scale or underscan changes */
8026 if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state))
8029 overall_update_type = UPDATE_TYPE_FULL;
8030 lock_and_validation_needed = true;
8033 ret = dm_determine_update_type_for_commit(&adev->dm, state, &update_type);
8037 if (overall_update_type < update_type)
8038 overall_update_type = update_type;
8041 * lock_and_validation_needed was an old way to determine if we need to set
8042 * the global lock. Leaving it in to check if we broke any corner cases
8043 * lock_and_validation_needed true = UPDATE_TYPE_FULL or UPDATE_TYPE_MED
8044 * lock_and_validation_needed false = UPDATE_TYPE_FAST
8046 if (lock_and_validation_needed && overall_update_type <= UPDATE_TYPE_FAST)
8047 WARN(1, "Global lock should be Set, overall_update_type should be UPDATE_TYPE_MED or UPDATE_TYPE_FULL");
8049 if (overall_update_type > UPDATE_TYPE_FAST) {
8050 ret = dm_atomic_get_state(state, &dm_state);
8054 ret = do_aquire_global_lock(dev, state);
8058 if (dc_validate_global_state(dc, dm_state->context, false) != DC_OK) {
8064 * The commit is a fast update. Fast updates shouldn't change
8065 * the DC context, affect global validation, and can have their
8066 * commit work done in parallel with other commits not touching
8067 * the same resource. If we have a new DC context as part of
8068 * the DM atomic state from validation we need to free it and
8069 * retain the existing one instead.
8071 struct dm_atomic_state *new_dm_state, *old_dm_state;
8073 new_dm_state = dm_atomic_get_new_state(state);
8074 old_dm_state = dm_atomic_get_old_state(state);
8076 if (new_dm_state && old_dm_state) {
8077 if (new_dm_state->context)
8078 dc_release_state(new_dm_state->context);
8080 new_dm_state->context = old_dm_state->context;
8082 if (old_dm_state->context)
8083 dc_retain_state(old_dm_state->context);
8087 /* Store the overall update type for use later in atomic check. */
8088 for_each_new_crtc_in_state (state, crtc, new_crtc_state, i) {
8089 struct dm_crtc_state *dm_new_crtc_state =
8090 to_dm_crtc_state(new_crtc_state);
8092 dm_new_crtc_state->update_type = (int)overall_update_type;
8095 /* Must be success */
8100 if (ret == -EDEADLK)
8101 DRM_DEBUG_DRIVER("Atomic check stopped to avoid deadlock.\n");
8102 else if (ret == -EINTR || ret == -EAGAIN || ret == -ERESTARTSYS)
8103 DRM_DEBUG_DRIVER("Atomic check stopped due to signal.\n");
8105 DRM_DEBUG_DRIVER("Atomic check failed with err: %d \n", ret);
8110 static bool is_dp_capable_without_timing_msa(struct dc *dc,
8111 struct amdgpu_dm_connector *amdgpu_dm_connector)
8114 bool capable = false;
8116 if (amdgpu_dm_connector->dc_link &&
8117 dm_helpers_dp_read_dpcd(
8119 amdgpu_dm_connector->dc_link,
8120 DP_DOWN_STREAM_PORT_COUNT,
8122 sizeof(dpcd_data))) {
8123 capable = (dpcd_data & DP_MSA_TIMING_PAR_IGNORED) ? true:false;
8128 void amdgpu_dm_update_freesync_caps(struct drm_connector *connector,
8132 bool edid_check_required;
8133 struct detailed_timing *timing;
8134 struct detailed_non_pixel *data;
8135 struct detailed_data_monitor_range *range;
8136 struct amdgpu_dm_connector *amdgpu_dm_connector =
8137 to_amdgpu_dm_connector(connector);
8138 struct dm_connector_state *dm_con_state = NULL;
8140 struct drm_device *dev = connector->dev;
8141 struct amdgpu_device *adev = dev->dev_private;
8142 bool freesync_capable = false;
8144 if (!connector->state) {
8145 DRM_ERROR("%s - Connector has no state", __func__);
8150 dm_con_state = to_dm_connector_state(connector->state);
8152 amdgpu_dm_connector->min_vfreq = 0;
8153 amdgpu_dm_connector->max_vfreq = 0;
8154 amdgpu_dm_connector->pixel_clock_mhz = 0;
8159 dm_con_state = to_dm_connector_state(connector->state);
8161 edid_check_required = false;
8162 if (!amdgpu_dm_connector->dc_sink) {
8163 DRM_ERROR("dc_sink NULL, could not add free_sync module.\n");
8166 if (!adev->dm.freesync_module)
8169 * if edid non zero restrict freesync only for dp and edp
8172 if (amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT
8173 || amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_EDP) {
8174 edid_check_required = is_dp_capable_without_timing_msa(
8176 amdgpu_dm_connector);
8179 if (edid_check_required == true && (edid->version > 1 ||
8180 (edid->version == 1 && edid->revision > 1))) {
8181 for (i = 0; i < 4; i++) {
8183 timing = &edid->detailed_timings[i];
8184 data = &timing->data.other_data;
8185 range = &data->data.range;
8187 * Check if monitor has continuous frequency mode
8189 if (data->type != EDID_DETAIL_MONITOR_RANGE)
8192 * Check for flag range limits only. If flag == 1 then
8193 * no additional timing information provided.
8194 * Default GTF, GTF Secondary curve and CVT are not
8197 if (range->flags != 1)
8200 amdgpu_dm_connector->min_vfreq = range->min_vfreq;
8201 amdgpu_dm_connector->max_vfreq = range->max_vfreq;
8202 amdgpu_dm_connector->pixel_clock_mhz =
8203 range->pixel_clock_mhz * 10;
8207 if (amdgpu_dm_connector->max_vfreq -
8208 amdgpu_dm_connector->min_vfreq > 10) {
8210 freesync_capable = true;
8216 dm_con_state->freesync_capable = freesync_capable;
8218 if (connector->vrr_capable_property)
8219 drm_connector_set_vrr_capable_property(connector,
8223 static void amdgpu_dm_set_psr_caps(struct dc_link *link)
8225 uint8_t dpcd_data[EDP_PSR_RECEIVER_CAP_SIZE];
8227 if (!(link->connector_signal & SIGNAL_TYPE_EDP))
8229 if (link->type == dc_connection_none)
8231 if (dm_helpers_dp_read_dpcd(NULL, link, DP_PSR_SUPPORT,
8232 dpcd_data, sizeof(dpcd_data))) {
8233 link->psr_feature_enabled = dpcd_data[0] ? true:false;
8234 DRM_INFO("PSR support:%d\n", link->psr_feature_enabled);
8239 * amdgpu_dm_link_setup_psr() - configure psr link
8240 * @stream: stream state
8242 * Return: true if success
8244 static bool amdgpu_dm_link_setup_psr(struct dc_stream_state *stream)
8246 struct dc_link *link = NULL;
8247 struct psr_config psr_config = {0};
8248 struct psr_context psr_context = {0};
8249 struct dc *dc = NULL;
8255 link = stream->link;
8258 psr_config.psr_version = dc->res_pool->dmcu->dmcu_version.psr_version;
8260 if (psr_config.psr_version > 0) {
8261 psr_config.psr_exit_link_training_required = 0x1;
8262 psr_config.psr_frame_capture_indication_req = 0;
8263 psr_config.psr_rfb_setup_time = 0x37;
8264 psr_config.psr_sdp_transmit_line_num_deadline = 0x20;
8265 psr_config.allow_smu_optimizations = 0x0;
8267 ret = dc_link_setup_psr(link, stream, &psr_config, &psr_context);
8270 DRM_DEBUG_DRIVER("PSR link: %d\n", link->psr_feature_enabled);
8276 * amdgpu_dm_psr_enable() - enable psr f/w
8277 * @stream: stream state
8279 * Return: true if success
8281 bool amdgpu_dm_psr_enable(struct dc_stream_state *stream)
8283 struct dc_link *link = stream->link;
8284 struct dc_static_screen_events triggers = {0};
8286 DRM_DEBUG_DRIVER("Enabling psr...\n");
8288 triggers.cursor_update = true;
8289 triggers.overlay_update = true;
8290 triggers.surface_update = true;
8292 dc_stream_set_static_screen_events(link->ctx->dc,
8296 return dc_link_set_psr_allow_active(link, true, false);
8300 * amdgpu_dm_psr_disable() - disable psr f/w
8301 * @stream: stream state
8303 * Return: true if success
8305 static bool amdgpu_dm_psr_disable(struct dc_stream_state *stream)
8308 DRM_DEBUG_DRIVER("Disabling psr...\n");
8310 return dc_link_set_psr_allow_active(stream->link, false, true);