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/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 #define FIRMWARE_NAVI12_DMCU "amdgpu/navi12_dmcu.bin"
102 MODULE_FIRMWARE(FIRMWARE_NAVI12_DMCU);
104 /* Number of bytes in PSP header for firmware. */
105 #define PSP_HEADER_BYTES 0x100
107 /* Number of bytes in PSP footer for firmware. */
108 #define PSP_FOOTER_BYTES 0x100
113 * The AMDgpu display manager, **amdgpu_dm** (or even simpler,
114 * **dm**) sits between DRM and DC. It acts as a liason, converting DRM
115 * requests into DC requests, and DC responses into DRM responses.
117 * The root control structure is &struct amdgpu_display_manager.
120 /* basic init/fini API */
121 static int amdgpu_dm_init(struct amdgpu_device *adev);
122 static void amdgpu_dm_fini(struct amdgpu_device *adev);
125 * initializes drm_device display related structures, based on the information
126 * provided by DAL. The drm strcutures are: drm_crtc, drm_connector,
127 * drm_encoder, drm_mode_config
129 * Returns 0 on success
131 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev);
132 /* removes and deallocates the drm structures, created by the above function */
133 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm);
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 =
387 amdgpu_get_vblank_counter_kms(&amdgpu_crtc->base);
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_VBL("crtc:%d, vupdate-vrr:%d\n",
412 amdgpu_dm_vrr_active(acrtc_state));
414 /* Core vblank handling is done here after end of front-porch in
415 * vrr mode, as vblank timestamping will give valid results
416 * while now done after front-porch. This will also deliver
417 * page-flip completion events that have been queued to us
418 * if a pageflip happened inside front-porch.
420 if (amdgpu_dm_vrr_active(acrtc_state)) {
421 drm_crtc_handle_vblank(&acrtc->base);
423 /* BTR processing for pre-DCE12 ASICs */
424 if (acrtc_state->stream &&
425 adev->family < AMDGPU_FAMILY_AI) {
426 spin_lock_irqsave(&adev->ddev->event_lock, flags);
427 mod_freesync_handle_v_update(
428 adev->dm.freesync_module,
430 &acrtc_state->vrr_params);
432 dc_stream_adjust_vmin_vmax(
435 &acrtc_state->vrr_params.adjust);
436 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
443 * dm_crtc_high_irq() - Handles CRTC interrupt
444 * @interrupt_params: used for determining the CRTC instance
446 * Handles the CRTC/VSYNC interrupt by notfying DRM's VBLANK
449 static void dm_crtc_high_irq(void *interrupt_params)
451 struct common_irq_params *irq_params = interrupt_params;
452 struct amdgpu_device *adev = irq_params->adev;
453 struct amdgpu_crtc *acrtc;
454 struct dm_crtc_state *acrtc_state;
457 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VBLANK);
461 acrtc_state = to_dm_crtc_state(acrtc->base.state);
463 DRM_DEBUG_VBL("crtc:%d, vupdate-vrr:%d, planes:%d\n", acrtc->crtc_id,
464 amdgpu_dm_vrr_active(acrtc_state),
465 acrtc_state->active_planes);
468 * Core vblank handling at start of front-porch is only possible
469 * in non-vrr mode, as only there vblank timestamping will give
470 * valid results while done in front-porch. Otherwise defer it
471 * to dm_vupdate_high_irq after end of front-porch.
473 if (!amdgpu_dm_vrr_active(acrtc_state))
474 drm_crtc_handle_vblank(&acrtc->base);
477 * Following stuff must happen at start of vblank, for crc
478 * computation and below-the-range btr support in vrr mode.
480 amdgpu_dm_crtc_handle_crc_irq(&acrtc->base);
482 /* BTR updates need to happen before VUPDATE on Vega and above. */
483 if (adev->family < AMDGPU_FAMILY_AI)
486 spin_lock_irqsave(&adev->ddev->event_lock, flags);
488 if (acrtc_state->stream && acrtc_state->vrr_params.supported &&
489 acrtc_state->freesync_config.state == VRR_STATE_ACTIVE_VARIABLE) {
490 mod_freesync_handle_v_update(adev->dm.freesync_module,
492 &acrtc_state->vrr_params);
494 dc_stream_adjust_vmin_vmax(adev->dm.dc, acrtc_state->stream,
495 &acrtc_state->vrr_params.adjust);
499 * If there aren't any active_planes then DCH HUBP may be clock-gated.
500 * In that case, pageflip completion interrupts won't fire and pageflip
501 * completion events won't get delivered. Prevent this by sending
502 * pending pageflip events from here if a flip is still pending.
504 * If any planes are enabled, use dm_pflip_high_irq() instead, to
505 * avoid race conditions between flip programming and completion,
506 * which could cause too early flip completion events.
508 if (adev->family >= AMDGPU_FAMILY_RV &&
509 acrtc->pflip_status == AMDGPU_FLIP_SUBMITTED &&
510 acrtc_state->active_planes == 0) {
512 drm_crtc_send_vblank_event(&acrtc->base, acrtc->event);
514 drm_crtc_vblank_put(&acrtc->base);
516 acrtc->pflip_status = AMDGPU_FLIP_NONE;
519 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
522 static int dm_set_clockgating_state(void *handle,
523 enum amd_clockgating_state state)
528 static int dm_set_powergating_state(void *handle,
529 enum amd_powergating_state state)
534 /* Prototypes of private functions */
535 static int dm_early_init(void* handle);
537 /* Allocate memory for FBC compressed data */
538 static void amdgpu_dm_fbc_init(struct drm_connector *connector)
540 struct drm_device *dev = connector->dev;
541 struct amdgpu_device *adev = dev->dev_private;
542 struct dm_comressor_info *compressor = &adev->dm.compressor;
543 struct amdgpu_dm_connector *aconn = to_amdgpu_dm_connector(connector);
544 struct drm_display_mode *mode;
545 unsigned long max_size = 0;
547 if (adev->dm.dc->fbc_compressor == NULL)
550 if (aconn->dc_link->connector_signal != SIGNAL_TYPE_EDP)
553 if (compressor->bo_ptr)
557 list_for_each_entry(mode, &connector->modes, head) {
558 if (max_size < mode->htotal * mode->vtotal)
559 max_size = mode->htotal * mode->vtotal;
563 int r = amdgpu_bo_create_kernel(adev, max_size * 4, PAGE_SIZE,
564 AMDGPU_GEM_DOMAIN_GTT, &compressor->bo_ptr,
565 &compressor->gpu_addr, &compressor->cpu_addr);
568 DRM_ERROR("DM: Failed to initialize FBC\n");
570 adev->dm.dc->ctx->fbc_gpu_addr = compressor->gpu_addr;
571 DRM_INFO("DM: FBC alloc %lu\n", max_size*4);
578 static int amdgpu_dm_audio_component_get_eld(struct device *kdev, int port,
579 int pipe, bool *enabled,
580 unsigned char *buf, int max_bytes)
582 struct drm_device *dev = dev_get_drvdata(kdev);
583 struct amdgpu_device *adev = dev->dev_private;
584 struct drm_connector *connector;
585 struct drm_connector_list_iter conn_iter;
586 struct amdgpu_dm_connector *aconnector;
591 mutex_lock(&adev->dm.audio_lock);
593 drm_connector_list_iter_begin(dev, &conn_iter);
594 drm_for_each_connector_iter(connector, &conn_iter) {
595 aconnector = to_amdgpu_dm_connector(connector);
596 if (aconnector->audio_inst != port)
600 ret = drm_eld_size(connector->eld);
601 memcpy(buf, connector->eld, min(max_bytes, ret));
605 drm_connector_list_iter_end(&conn_iter);
607 mutex_unlock(&adev->dm.audio_lock);
609 DRM_DEBUG_KMS("Get ELD : idx=%d ret=%d en=%d\n", port, ret, *enabled);
614 static const struct drm_audio_component_ops amdgpu_dm_audio_component_ops = {
615 .get_eld = amdgpu_dm_audio_component_get_eld,
618 static int amdgpu_dm_audio_component_bind(struct device *kdev,
619 struct device *hda_kdev, void *data)
621 struct drm_device *dev = dev_get_drvdata(kdev);
622 struct amdgpu_device *adev = dev->dev_private;
623 struct drm_audio_component *acomp = data;
625 acomp->ops = &amdgpu_dm_audio_component_ops;
627 adev->dm.audio_component = acomp;
632 static void amdgpu_dm_audio_component_unbind(struct device *kdev,
633 struct device *hda_kdev, void *data)
635 struct drm_device *dev = dev_get_drvdata(kdev);
636 struct amdgpu_device *adev = dev->dev_private;
637 struct drm_audio_component *acomp = data;
641 adev->dm.audio_component = NULL;
644 static const struct component_ops amdgpu_dm_audio_component_bind_ops = {
645 .bind = amdgpu_dm_audio_component_bind,
646 .unbind = amdgpu_dm_audio_component_unbind,
649 static int amdgpu_dm_audio_init(struct amdgpu_device *adev)
656 adev->mode_info.audio.enabled = true;
658 adev->mode_info.audio.num_pins = adev->dm.dc->res_pool->audio_count;
660 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
661 adev->mode_info.audio.pin[i].channels = -1;
662 adev->mode_info.audio.pin[i].rate = -1;
663 adev->mode_info.audio.pin[i].bits_per_sample = -1;
664 adev->mode_info.audio.pin[i].status_bits = 0;
665 adev->mode_info.audio.pin[i].category_code = 0;
666 adev->mode_info.audio.pin[i].connected = false;
667 adev->mode_info.audio.pin[i].id =
668 adev->dm.dc->res_pool->audios[i]->inst;
669 adev->mode_info.audio.pin[i].offset = 0;
672 ret = component_add(adev->dev, &amdgpu_dm_audio_component_bind_ops);
676 adev->dm.audio_registered = true;
681 static void amdgpu_dm_audio_fini(struct amdgpu_device *adev)
686 if (!adev->mode_info.audio.enabled)
689 if (adev->dm.audio_registered) {
690 component_del(adev->dev, &amdgpu_dm_audio_component_bind_ops);
691 adev->dm.audio_registered = false;
694 /* TODO: Disable audio? */
696 adev->mode_info.audio.enabled = false;
699 void amdgpu_dm_audio_eld_notify(struct amdgpu_device *adev, int pin)
701 struct drm_audio_component *acomp = adev->dm.audio_component;
703 if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify) {
704 DRM_DEBUG_KMS("Notify ELD: %d\n", pin);
706 acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr,
711 static int dm_dmub_hw_init(struct amdgpu_device *adev)
713 const struct dmcub_firmware_header_v1_0 *hdr;
714 struct dmub_srv *dmub_srv = adev->dm.dmub_srv;
715 struct dmub_srv_fb_info *fb_info = adev->dm.dmub_fb_info;
716 const struct firmware *dmub_fw = adev->dm.dmub_fw;
717 struct dmcu *dmcu = adev->dm.dc->res_pool->dmcu;
718 struct abm *abm = adev->dm.dc->res_pool->abm;
719 struct dmub_srv_hw_params hw_params;
720 enum dmub_status status;
721 const unsigned char *fw_inst_const, *fw_bss_data;
722 uint32_t i, fw_inst_const_size, fw_bss_data_size;
726 /* DMUB isn't supported on the ASIC. */
730 DRM_ERROR("No framebuffer info for DMUB service.\n");
735 /* Firmware required for DMUB support. */
736 DRM_ERROR("No firmware provided for DMUB.\n");
740 status = dmub_srv_has_hw_support(dmub_srv, &has_hw_support);
741 if (status != DMUB_STATUS_OK) {
742 DRM_ERROR("Error checking HW support for DMUB: %d\n", status);
746 if (!has_hw_support) {
747 DRM_INFO("DMUB unsupported on ASIC\n");
751 hdr = (const struct dmcub_firmware_header_v1_0 *)dmub_fw->data;
753 fw_inst_const = dmub_fw->data +
754 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
757 fw_bss_data = dmub_fw->data +
758 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
759 le32_to_cpu(hdr->inst_const_bytes);
761 /* Copy firmware and bios info into FB memory. */
762 fw_inst_const_size = le32_to_cpu(hdr->inst_const_bytes) -
763 PSP_HEADER_BYTES - PSP_FOOTER_BYTES;
765 fw_bss_data_size = le32_to_cpu(hdr->bss_data_bytes);
767 /* if adev->firmware.load_type == AMDGPU_FW_LOAD_PSP,
768 * amdgpu_ucode_init_single_fw will load dmub firmware
769 * fw_inst_const part to cw0; otherwise, the firmware back door load
770 * will be done by dm_dmub_hw_init
772 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
773 memcpy(fb_info->fb[DMUB_WINDOW_0_INST_CONST].cpu_addr, fw_inst_const,
777 if (fw_bss_data_size)
778 memcpy(fb_info->fb[DMUB_WINDOW_2_BSS_DATA].cpu_addr,
779 fw_bss_data, fw_bss_data_size);
781 /* Copy firmware bios info into FB memory. */
782 memcpy(fb_info->fb[DMUB_WINDOW_3_VBIOS].cpu_addr, adev->bios,
785 /* Reset regions that need to be reset. */
786 memset(fb_info->fb[DMUB_WINDOW_4_MAILBOX].cpu_addr, 0,
787 fb_info->fb[DMUB_WINDOW_4_MAILBOX].size);
789 memset(fb_info->fb[DMUB_WINDOW_5_TRACEBUFF].cpu_addr, 0,
790 fb_info->fb[DMUB_WINDOW_5_TRACEBUFF].size);
792 memset(fb_info->fb[DMUB_WINDOW_6_FW_STATE].cpu_addr, 0,
793 fb_info->fb[DMUB_WINDOW_6_FW_STATE].size);
795 /* Initialize hardware. */
796 memset(&hw_params, 0, sizeof(hw_params));
797 hw_params.fb_base = adev->gmc.fb_start;
798 hw_params.fb_offset = adev->gmc.aper_base;
800 /* backdoor load firmware and trigger dmub running */
801 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
802 hw_params.load_inst_const = true;
805 hw_params.psp_version = dmcu->psp_version;
807 for (i = 0; i < fb_info->num_fb; ++i)
808 hw_params.fb[i] = &fb_info->fb[i];
810 status = dmub_srv_hw_init(dmub_srv, &hw_params);
811 if (status != DMUB_STATUS_OK) {
812 DRM_ERROR("Error initializing DMUB HW: %d\n", status);
816 /* Wait for firmware load to finish. */
817 status = dmub_srv_wait_for_auto_load(dmub_srv, 100000);
818 if (status != DMUB_STATUS_OK)
819 DRM_WARN("Wait for DMUB auto-load failed: %d\n", status);
821 /* Init DMCU and ABM if available. */
823 dmcu->funcs->dmcu_init(dmcu);
824 abm->dmcu_is_running = dmcu->funcs->is_dmcu_initialized(dmcu);
827 adev->dm.dc->ctx->dmub_srv = dc_dmub_srv_create(adev->dm.dc, dmub_srv);
828 if (!adev->dm.dc->ctx->dmub_srv) {
829 DRM_ERROR("Couldn't allocate DC DMUB server!\n");
833 DRM_INFO("DMUB hardware initialized: version=0x%08X\n",
834 adev->dm.dmcub_fw_version);
839 static int amdgpu_dm_init(struct amdgpu_device *adev)
841 struct dc_init_data init_data;
842 #ifdef CONFIG_DRM_AMD_DC_HDCP
843 struct dc_callback_init init_params;
847 adev->dm.ddev = adev->ddev;
848 adev->dm.adev = adev;
850 /* Zero all the fields */
851 memset(&init_data, 0, sizeof(init_data));
852 #ifdef CONFIG_DRM_AMD_DC_HDCP
853 memset(&init_params, 0, sizeof(init_params));
856 mutex_init(&adev->dm.dc_lock);
857 mutex_init(&adev->dm.audio_lock);
859 if(amdgpu_dm_irq_init(adev)) {
860 DRM_ERROR("amdgpu: failed to initialize DM IRQ support.\n");
864 init_data.asic_id.chip_family = adev->family;
866 init_data.asic_id.pci_revision_id = adev->pdev->revision;
867 init_data.asic_id.hw_internal_rev = adev->external_rev_id;
869 init_data.asic_id.vram_width = adev->gmc.vram_width;
870 /* TODO: initialize init_data.asic_id.vram_type here!!!! */
871 init_data.asic_id.atombios_base_address =
872 adev->mode_info.atom_context->bios;
874 init_data.driver = adev;
876 adev->dm.cgs_device = amdgpu_cgs_create_device(adev);
878 if (!adev->dm.cgs_device) {
879 DRM_ERROR("amdgpu: failed to create cgs device.\n");
883 init_data.cgs_device = adev->dm.cgs_device;
885 init_data.dce_environment = DCE_ENV_PRODUCTION_DRV;
887 switch (adev->asic_type) {
892 init_data.flags.gpu_vm_support = true;
898 if (amdgpu_dc_feature_mask & DC_FBC_MASK)
899 init_data.flags.fbc_support = true;
901 if (amdgpu_dc_feature_mask & DC_MULTI_MON_PP_MCLK_SWITCH_MASK)
902 init_data.flags.multi_mon_pp_mclk_switch = true;
904 if (amdgpu_dc_feature_mask & DC_DISABLE_FRACTIONAL_PWM_MASK)
905 init_data.flags.disable_fractional_pwm = true;
907 init_data.flags.power_down_display_on_boot = true;
909 init_data.soc_bounding_box = adev->dm.soc_bounding_box;
911 /* Display Core create. */
912 adev->dm.dc = dc_create(&init_data);
915 DRM_INFO("Display Core initialized with v%s!\n", DC_VER);
917 DRM_INFO("Display Core failed to initialize with v%s!\n", DC_VER);
921 if (amdgpu_dc_debug_mask & DC_DISABLE_PIPE_SPLIT) {
922 adev->dm.dc->debug.force_single_disp_pipe_split = false;
923 adev->dm.dc->debug.pipe_split_policy = MPC_SPLIT_AVOID;
926 if (adev->asic_type != CHIP_CARRIZO && adev->asic_type != CHIP_STONEY)
927 adev->dm.dc->debug.disable_stutter = amdgpu_pp_feature_mask & PP_STUTTER_MODE ? false : true;
929 if (amdgpu_dc_debug_mask & DC_DISABLE_STUTTER)
930 adev->dm.dc->debug.disable_stutter = true;
932 if (amdgpu_dc_debug_mask & DC_DISABLE_DSC)
933 adev->dm.dc->debug.disable_dsc = true;
935 if (amdgpu_dc_debug_mask & DC_DISABLE_CLOCK_GATING)
936 adev->dm.dc->debug.disable_clock_gate = true;
938 r = dm_dmub_hw_init(adev);
940 DRM_ERROR("DMUB interface failed to initialize: status=%d\n", r);
944 dc_hardware_init(adev->dm.dc);
946 adev->dm.freesync_module = mod_freesync_create(adev->dm.dc);
947 if (!adev->dm.freesync_module) {
949 "amdgpu: failed to initialize freesync_module.\n");
951 DRM_DEBUG_DRIVER("amdgpu: freesync_module init done %p.\n",
952 adev->dm.freesync_module);
954 amdgpu_dm_init_color_mod();
956 #ifdef CONFIG_DRM_AMD_DC_HDCP
957 if (adev->asic_type >= CHIP_RAVEN) {
958 adev->dm.hdcp_workqueue = hdcp_create_workqueue(adev, &init_params.cp_psp, adev->dm.dc);
960 if (!adev->dm.hdcp_workqueue)
961 DRM_ERROR("amdgpu: failed to initialize hdcp_workqueue.\n");
963 DRM_DEBUG_DRIVER("amdgpu: hdcp_workqueue init done %p.\n", adev->dm.hdcp_workqueue);
965 dc_init_callbacks(adev->dm.dc, &init_params);
968 if (amdgpu_dm_initialize_drm_device(adev)) {
970 "amdgpu: failed to initialize sw for display support.\n");
974 /* Update the actual used number of crtc */
975 adev->mode_info.num_crtc = adev->dm.display_indexes_num;
977 /* TODO: Add_display_info? */
979 /* TODO use dynamic cursor width */
980 adev->ddev->mode_config.cursor_width = adev->dm.dc->caps.max_cursor_size;
981 adev->ddev->mode_config.cursor_height = adev->dm.dc->caps.max_cursor_size;
983 if (drm_vblank_init(adev->ddev, adev->dm.display_indexes_num)) {
985 "amdgpu: failed to initialize sw for display support.\n");
989 DRM_DEBUG_DRIVER("KMS initialized.\n");
993 amdgpu_dm_fini(adev);
998 static void amdgpu_dm_fini(struct amdgpu_device *adev)
1000 amdgpu_dm_audio_fini(adev);
1002 amdgpu_dm_destroy_drm_device(&adev->dm);
1004 #ifdef CONFIG_DRM_AMD_DC_HDCP
1005 if (adev->dm.hdcp_workqueue) {
1006 hdcp_destroy(adev->dm.hdcp_workqueue);
1007 adev->dm.hdcp_workqueue = NULL;
1011 dc_deinit_callbacks(adev->dm.dc);
1013 if (adev->dm.dc->ctx->dmub_srv) {
1014 dc_dmub_srv_destroy(&adev->dm.dc->ctx->dmub_srv);
1015 adev->dm.dc->ctx->dmub_srv = NULL;
1018 if (adev->dm.dmub_bo)
1019 amdgpu_bo_free_kernel(&adev->dm.dmub_bo,
1020 &adev->dm.dmub_bo_gpu_addr,
1021 &adev->dm.dmub_bo_cpu_addr);
1023 /* DC Destroy TODO: Replace destroy DAL */
1025 dc_destroy(&adev->dm.dc);
1027 * TODO: pageflip, vlank interrupt
1029 * amdgpu_dm_irq_fini(adev);
1032 if (adev->dm.cgs_device) {
1033 amdgpu_cgs_destroy_device(adev->dm.cgs_device);
1034 adev->dm.cgs_device = NULL;
1036 if (adev->dm.freesync_module) {
1037 mod_freesync_destroy(adev->dm.freesync_module);
1038 adev->dm.freesync_module = NULL;
1041 mutex_destroy(&adev->dm.audio_lock);
1042 mutex_destroy(&adev->dm.dc_lock);
1047 static int load_dmcu_fw(struct amdgpu_device *adev)
1049 const char *fw_name_dmcu = NULL;
1051 const struct dmcu_firmware_header_v1_0 *hdr;
1053 switch(adev->asic_type) {
1063 case CHIP_POLARIS11:
1064 case CHIP_POLARIS10:
1065 case CHIP_POLARIS12:
1075 fw_name_dmcu = FIRMWARE_NAVI12_DMCU;
1078 if (ASICREV_IS_PICASSO(adev->external_rev_id))
1079 fw_name_dmcu = FIRMWARE_RAVEN_DMCU;
1080 else if (ASICREV_IS_RAVEN2(adev->external_rev_id))
1081 fw_name_dmcu = FIRMWARE_RAVEN_DMCU;
1086 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type);
1090 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1091 DRM_DEBUG_KMS("dm: DMCU firmware not supported on direct or SMU loading\n");
1095 r = request_firmware_direct(&adev->dm.fw_dmcu, fw_name_dmcu, adev->dev);
1097 /* DMCU firmware is not necessary, so don't raise a fuss if it's missing */
1098 DRM_DEBUG_KMS("dm: DMCU firmware not found\n");
1099 adev->dm.fw_dmcu = NULL;
1103 dev_err(adev->dev, "amdgpu_dm: Can't load firmware \"%s\"\n",
1108 r = amdgpu_ucode_validate(adev->dm.fw_dmcu);
1110 dev_err(adev->dev, "amdgpu_dm: Can't validate firmware \"%s\"\n",
1112 release_firmware(adev->dm.fw_dmcu);
1113 adev->dm.fw_dmcu = NULL;
1117 hdr = (const struct dmcu_firmware_header_v1_0 *)adev->dm.fw_dmcu->data;
1118 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].ucode_id = AMDGPU_UCODE_ID_DMCU_ERAM;
1119 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].fw = adev->dm.fw_dmcu;
1120 adev->firmware.fw_size +=
1121 ALIGN(le32_to_cpu(hdr->header.ucode_size_bytes) - le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE);
1123 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].ucode_id = AMDGPU_UCODE_ID_DMCU_INTV;
1124 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].fw = adev->dm.fw_dmcu;
1125 adev->firmware.fw_size +=
1126 ALIGN(le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE);
1128 adev->dm.dmcu_fw_version = le32_to_cpu(hdr->header.ucode_version);
1130 DRM_DEBUG_KMS("PSP loading DMCU firmware\n");
1135 static uint32_t amdgpu_dm_dmub_reg_read(void *ctx, uint32_t address)
1137 struct amdgpu_device *adev = ctx;
1139 return dm_read_reg(adev->dm.dc->ctx, address);
1142 static void amdgpu_dm_dmub_reg_write(void *ctx, uint32_t address,
1145 struct amdgpu_device *adev = ctx;
1147 return dm_write_reg(adev->dm.dc->ctx, address, value);
1150 static int dm_dmub_sw_init(struct amdgpu_device *adev)
1152 struct dmub_srv_create_params create_params;
1153 struct dmub_srv_region_params region_params;
1154 struct dmub_srv_region_info region_info;
1155 struct dmub_srv_fb_params fb_params;
1156 struct dmub_srv_fb_info *fb_info;
1157 struct dmub_srv *dmub_srv;
1158 const struct dmcub_firmware_header_v1_0 *hdr;
1159 const char *fw_name_dmub;
1160 enum dmub_asic dmub_asic;
1161 enum dmub_status status;
1164 switch (adev->asic_type) {
1166 dmub_asic = DMUB_ASIC_DCN21;
1167 fw_name_dmub = FIRMWARE_RENOIR_DMUB;
1171 /* ASIC doesn't support DMUB. */
1175 r = request_firmware_direct(&adev->dm.dmub_fw, fw_name_dmub, adev->dev);
1177 DRM_ERROR("DMUB firmware loading failed: %d\n", r);
1181 r = amdgpu_ucode_validate(adev->dm.dmub_fw);
1183 DRM_ERROR("Couldn't validate DMUB firmware: %d\n", r);
1187 hdr = (const struct dmcub_firmware_header_v1_0 *)adev->dm.dmub_fw->data;
1189 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
1190 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCUB].ucode_id =
1191 AMDGPU_UCODE_ID_DMCUB;
1192 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCUB].fw =
1194 adev->firmware.fw_size +=
1195 ALIGN(le32_to_cpu(hdr->inst_const_bytes), PAGE_SIZE);
1197 DRM_INFO("Loading DMUB firmware via PSP: version=0x%08X\n",
1198 adev->dm.dmcub_fw_version);
1201 adev->dm.dmcub_fw_version = le32_to_cpu(hdr->header.ucode_version);
1203 adev->dm.dmub_srv = kzalloc(sizeof(*adev->dm.dmub_srv), GFP_KERNEL);
1204 dmub_srv = adev->dm.dmub_srv;
1207 DRM_ERROR("Failed to allocate DMUB service!\n");
1211 memset(&create_params, 0, sizeof(create_params));
1212 create_params.user_ctx = adev;
1213 create_params.funcs.reg_read = amdgpu_dm_dmub_reg_read;
1214 create_params.funcs.reg_write = amdgpu_dm_dmub_reg_write;
1215 create_params.asic = dmub_asic;
1217 /* Create the DMUB service. */
1218 status = dmub_srv_create(dmub_srv, &create_params);
1219 if (status != DMUB_STATUS_OK) {
1220 DRM_ERROR("Error creating DMUB service: %d\n", status);
1224 /* Calculate the size of all the regions for the DMUB service. */
1225 memset(®ion_params, 0, sizeof(region_params));
1227 region_params.inst_const_size = le32_to_cpu(hdr->inst_const_bytes) -
1228 PSP_HEADER_BYTES - PSP_FOOTER_BYTES;
1229 region_params.bss_data_size = le32_to_cpu(hdr->bss_data_bytes);
1230 region_params.vbios_size = adev->bios_size;
1231 region_params.fw_bss_data =
1232 adev->dm.dmub_fw->data +
1233 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
1234 le32_to_cpu(hdr->inst_const_bytes);
1235 region_params.fw_inst_const =
1236 adev->dm.dmub_fw->data +
1237 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
1240 status = dmub_srv_calc_region_info(dmub_srv, ®ion_params,
1243 if (status != DMUB_STATUS_OK) {
1244 DRM_ERROR("Error calculating DMUB region info: %d\n", status);
1249 * Allocate a framebuffer based on the total size of all the regions.
1250 * TODO: Move this into GART.
1252 r = amdgpu_bo_create_kernel(adev, region_info.fb_size, PAGE_SIZE,
1253 AMDGPU_GEM_DOMAIN_VRAM, &adev->dm.dmub_bo,
1254 &adev->dm.dmub_bo_gpu_addr,
1255 &adev->dm.dmub_bo_cpu_addr);
1259 /* Rebase the regions on the framebuffer address. */
1260 memset(&fb_params, 0, sizeof(fb_params));
1261 fb_params.cpu_addr = adev->dm.dmub_bo_cpu_addr;
1262 fb_params.gpu_addr = adev->dm.dmub_bo_gpu_addr;
1263 fb_params.region_info = ®ion_info;
1265 adev->dm.dmub_fb_info =
1266 kzalloc(sizeof(*adev->dm.dmub_fb_info), GFP_KERNEL);
1267 fb_info = adev->dm.dmub_fb_info;
1271 "Failed to allocate framebuffer info for DMUB service!\n");
1275 status = dmub_srv_calc_fb_info(dmub_srv, &fb_params, fb_info);
1276 if (status != DMUB_STATUS_OK) {
1277 DRM_ERROR("Error calculating DMUB FB info: %d\n", status);
1284 static int dm_sw_init(void *handle)
1286 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1289 r = dm_dmub_sw_init(adev);
1293 return load_dmcu_fw(adev);
1296 static int dm_sw_fini(void *handle)
1298 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1300 kfree(adev->dm.dmub_fb_info);
1301 adev->dm.dmub_fb_info = NULL;
1303 if (adev->dm.dmub_srv) {
1304 dmub_srv_destroy(adev->dm.dmub_srv);
1305 adev->dm.dmub_srv = NULL;
1308 if (adev->dm.dmub_fw) {
1309 release_firmware(adev->dm.dmub_fw);
1310 adev->dm.dmub_fw = NULL;
1313 if(adev->dm.fw_dmcu) {
1314 release_firmware(adev->dm.fw_dmcu);
1315 adev->dm.fw_dmcu = NULL;
1321 static int detect_mst_link_for_all_connectors(struct drm_device *dev)
1323 struct amdgpu_dm_connector *aconnector;
1324 struct drm_connector *connector;
1325 struct drm_connector_list_iter iter;
1328 drm_connector_list_iter_begin(dev, &iter);
1329 drm_for_each_connector_iter(connector, &iter) {
1330 aconnector = to_amdgpu_dm_connector(connector);
1331 if (aconnector->dc_link->type == dc_connection_mst_branch &&
1332 aconnector->mst_mgr.aux) {
1333 DRM_DEBUG_DRIVER("DM_MST: starting TM on aconnector: %p [id: %d]\n",
1335 aconnector->base.base.id);
1337 ret = drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true);
1339 DRM_ERROR("DM_MST: Failed to start MST\n");
1340 aconnector->dc_link->type =
1341 dc_connection_single;
1346 drm_connector_list_iter_end(&iter);
1351 static int dm_late_init(void *handle)
1353 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1355 struct dmcu_iram_parameters params;
1356 unsigned int linear_lut[16];
1358 struct dmcu *dmcu = NULL;
1361 if (!adev->dm.fw_dmcu)
1362 return detect_mst_link_for_all_connectors(adev->ddev);
1364 dmcu = adev->dm.dc->res_pool->dmcu;
1366 for (i = 0; i < 16; i++)
1367 linear_lut[i] = 0xFFFF * i / 15;
1370 params.backlight_ramping_start = 0xCCCC;
1371 params.backlight_ramping_reduction = 0xCCCCCCCC;
1372 params.backlight_lut_array_size = 16;
1373 params.backlight_lut_array = linear_lut;
1375 /* Min backlight level after ABM reduction, Don't allow below 1%
1376 * 0xFFFF x 0.01 = 0x28F
1378 params.min_abm_backlight = 0x28F;
1380 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);
1422 static int amdgpu_dm_smu_write_watermarks_table(struct amdgpu_device *adev)
1424 struct smu_context *smu = &adev->smu;
1427 if (!is_support_sw_smu(adev))
1430 /* This interface is for dGPU Navi1x.Linux dc-pplib interface depends
1431 * on window driver dc implementation.
1432 * For Navi1x, clock settings of dcn watermarks are fixed. the settings
1433 * should be passed to smu during boot up and resume from s3.
1434 * boot up: dc calculate dcn watermark clock settings within dc_create,
1435 * dcn20_resource_construct
1436 * then call pplib functions below to pass the settings to smu:
1437 * smu_set_watermarks_for_clock_ranges
1438 * smu_set_watermarks_table
1439 * navi10_set_watermarks_table
1440 * smu_write_watermarks_table
1442 * For Renoir, clock settings of dcn watermark are also fixed values.
1443 * dc has implemented different flow for window driver:
1444 * dc_hardware_init / dc_set_power_state
1449 * smu_set_watermarks_for_clock_ranges
1450 * renoir_set_watermarks_table
1451 * smu_write_watermarks_table
1454 * dc_hardware_init -> amdgpu_dm_init
1455 * dc_set_power_state --> dm_resume
1457 * therefore, this function apply to navi10/12/14 but not Renoir
1460 switch(adev->asic_type) {
1469 mutex_lock(&smu->mutex);
1471 /* pass data to smu controller */
1472 if ((smu->watermarks_bitmap & WATERMARKS_EXIST) &&
1473 !(smu->watermarks_bitmap & WATERMARKS_LOADED)) {
1474 ret = smu_write_watermarks_table(smu);
1477 mutex_unlock(&smu->mutex);
1478 DRM_ERROR("Failed to update WMTABLE!\n");
1481 smu->watermarks_bitmap |= WATERMARKS_LOADED;
1484 mutex_unlock(&smu->mutex);
1490 * dm_hw_init() - Initialize DC device
1491 * @handle: The base driver device containing the amdgpu_dm device.
1493 * Initialize the &struct amdgpu_display_manager device. This involves calling
1494 * the initializers of each DM component, then populating the struct with them.
1496 * Although the function implies hardware initialization, both hardware and
1497 * software are initialized here. Splitting them out to their relevant init
1498 * hooks is a future TODO item.
1500 * Some notable things that are initialized here:
1502 * - Display Core, both software and hardware
1503 * - DC modules that we need (freesync and color management)
1504 * - DRM software states
1505 * - Interrupt sources and handlers
1507 * - Debug FS entries, if enabled
1509 static int dm_hw_init(void *handle)
1511 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1512 /* Create DAL display manager */
1513 amdgpu_dm_init(adev);
1514 amdgpu_dm_hpd_init(adev);
1520 * dm_hw_fini() - Teardown DC device
1521 * @handle: The base driver device containing the amdgpu_dm device.
1523 * Teardown components within &struct amdgpu_display_manager that require
1524 * cleanup. This involves cleaning up the DRM device, DC, and any modules that
1525 * were loaded. Also flush IRQ workqueues and disable them.
1527 static int dm_hw_fini(void *handle)
1529 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1531 amdgpu_dm_hpd_fini(adev);
1533 amdgpu_dm_irq_fini(adev);
1534 amdgpu_dm_fini(adev);
1539 static int dm_enable_vblank(struct drm_crtc *crtc);
1540 static void dm_disable_vblank(struct drm_crtc *crtc);
1542 static void dm_gpureset_toggle_interrupts(struct amdgpu_device *adev,
1543 struct dc_state *state, bool enable)
1545 enum dc_irq_source irq_source;
1546 struct amdgpu_crtc *acrtc;
1550 for (i = 0; i < state->stream_count; i++) {
1551 acrtc = get_crtc_by_otg_inst(
1552 adev, state->stream_status[i].primary_otg_inst);
1554 if (acrtc && state->stream_status[i].plane_count != 0) {
1555 irq_source = IRQ_TYPE_PFLIP + acrtc->otg_inst;
1556 rc = dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY;
1557 DRM_DEBUG("crtc %d - vupdate irq %sabling: r=%d\n",
1558 acrtc->crtc_id, enable ? "en" : "dis", rc);
1560 DRM_WARN("Failed to %s pflip interrupts\n",
1561 enable ? "enable" : "disable");
1564 rc = dm_enable_vblank(&acrtc->base);
1566 DRM_WARN("Failed to enable vblank interrupts\n");
1568 dm_disable_vblank(&acrtc->base);
1576 enum dc_status amdgpu_dm_commit_zero_streams(struct dc *dc)
1578 struct dc_state *context = NULL;
1579 enum dc_status res = DC_ERROR_UNEXPECTED;
1581 struct dc_stream_state *del_streams[MAX_PIPES];
1582 int del_streams_count = 0;
1584 memset(del_streams, 0, sizeof(del_streams));
1586 context = dc_create_state(dc);
1587 if (context == NULL)
1588 goto context_alloc_fail;
1590 dc_resource_state_copy_construct_current(dc, context);
1592 /* First remove from context all streams */
1593 for (i = 0; i < context->stream_count; i++) {
1594 struct dc_stream_state *stream = context->streams[i];
1596 del_streams[del_streams_count++] = stream;
1599 /* Remove all planes for removed streams and then remove the streams */
1600 for (i = 0; i < del_streams_count; i++) {
1601 if (!dc_rem_all_planes_for_stream(dc, del_streams[i], context)) {
1602 res = DC_FAIL_DETACH_SURFACES;
1606 res = dc_remove_stream_from_ctx(dc, context, del_streams[i]);
1612 res = dc_validate_global_state(dc, context, false);
1615 DRM_ERROR("%s:resource validation failed, dc_status:%d\n", __func__, res);
1619 res = dc_commit_state(dc, context);
1622 dc_release_state(context);
1628 static int dm_suspend(void *handle)
1630 struct amdgpu_device *adev = handle;
1631 struct amdgpu_display_manager *dm = &adev->dm;
1634 if (adev->in_gpu_reset) {
1635 mutex_lock(&dm->dc_lock);
1636 dm->cached_dc_state = dc_copy_state(dm->dc->current_state);
1638 dm_gpureset_toggle_interrupts(adev, dm->cached_dc_state, false);
1640 amdgpu_dm_commit_zero_streams(dm->dc);
1642 amdgpu_dm_irq_suspend(adev);
1647 WARN_ON(adev->dm.cached_state);
1648 adev->dm.cached_state = drm_atomic_helper_suspend(adev->ddev);
1650 s3_handle_mst(adev->ddev, true);
1652 amdgpu_dm_irq_suspend(adev);
1655 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D3);
1660 static struct amdgpu_dm_connector *
1661 amdgpu_dm_find_first_crtc_matching_connector(struct drm_atomic_state *state,
1662 struct drm_crtc *crtc)
1665 struct drm_connector_state *new_con_state;
1666 struct drm_connector *connector;
1667 struct drm_crtc *crtc_from_state;
1669 for_each_new_connector_in_state(state, connector, new_con_state, i) {
1670 crtc_from_state = new_con_state->crtc;
1672 if (crtc_from_state == crtc)
1673 return to_amdgpu_dm_connector(connector);
1679 static void emulated_link_detect(struct dc_link *link)
1681 struct dc_sink_init_data sink_init_data = { 0 };
1682 struct display_sink_capability sink_caps = { 0 };
1683 enum dc_edid_status edid_status;
1684 struct dc_context *dc_ctx = link->ctx;
1685 struct dc_sink *sink = NULL;
1686 struct dc_sink *prev_sink = NULL;
1688 link->type = dc_connection_none;
1689 prev_sink = link->local_sink;
1691 if (prev_sink != NULL)
1692 dc_sink_retain(prev_sink);
1694 switch (link->connector_signal) {
1695 case SIGNAL_TYPE_HDMI_TYPE_A: {
1696 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
1697 sink_caps.signal = SIGNAL_TYPE_HDMI_TYPE_A;
1701 case SIGNAL_TYPE_DVI_SINGLE_LINK: {
1702 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
1703 sink_caps.signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
1707 case SIGNAL_TYPE_DVI_DUAL_LINK: {
1708 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
1709 sink_caps.signal = SIGNAL_TYPE_DVI_DUAL_LINK;
1713 case SIGNAL_TYPE_LVDS: {
1714 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
1715 sink_caps.signal = SIGNAL_TYPE_LVDS;
1719 case SIGNAL_TYPE_EDP: {
1720 sink_caps.transaction_type =
1721 DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
1722 sink_caps.signal = SIGNAL_TYPE_EDP;
1726 case SIGNAL_TYPE_DISPLAY_PORT: {
1727 sink_caps.transaction_type =
1728 DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
1729 sink_caps.signal = SIGNAL_TYPE_VIRTUAL;
1734 DC_ERROR("Invalid connector type! signal:%d\n",
1735 link->connector_signal);
1739 sink_init_data.link = link;
1740 sink_init_data.sink_signal = sink_caps.signal;
1742 sink = dc_sink_create(&sink_init_data);
1744 DC_ERROR("Failed to create sink!\n");
1748 /* dc_sink_create returns a new reference */
1749 link->local_sink = sink;
1751 edid_status = dm_helpers_read_local_edid(
1756 if (edid_status != EDID_OK)
1757 DC_ERROR("Failed to read EDID");
1761 static void dm_gpureset_commit_state(struct dc_state *dc_state,
1762 struct amdgpu_display_manager *dm)
1765 struct dc_surface_update surface_updates[MAX_SURFACES];
1766 struct dc_plane_info plane_infos[MAX_SURFACES];
1767 struct dc_scaling_info scaling_infos[MAX_SURFACES];
1768 struct dc_flip_addrs flip_addrs[MAX_SURFACES];
1769 struct dc_stream_update stream_update;
1773 bundle = kzalloc(sizeof(*bundle), GFP_KERNEL);
1776 dm_error("Failed to allocate update bundle\n");
1780 for (k = 0; k < dc_state->stream_count; k++) {
1781 bundle->stream_update.stream = dc_state->streams[k];
1783 for (m = 0; m < dc_state->stream_status->plane_count; m++) {
1784 bundle->surface_updates[m].surface =
1785 dc_state->stream_status->plane_states[m];
1786 bundle->surface_updates[m].surface->force_full_update =
1789 dc_commit_updates_for_stream(
1790 dm->dc, bundle->surface_updates,
1791 dc_state->stream_status->plane_count,
1792 dc_state->streams[k], &bundle->stream_update, dc_state);
1801 static int dm_resume(void *handle)
1803 struct amdgpu_device *adev = handle;
1804 struct drm_device *ddev = adev->ddev;
1805 struct amdgpu_display_manager *dm = &adev->dm;
1806 struct amdgpu_dm_connector *aconnector;
1807 struct drm_connector *connector;
1808 struct drm_connector_list_iter iter;
1809 struct drm_crtc *crtc;
1810 struct drm_crtc_state *new_crtc_state;
1811 struct dm_crtc_state *dm_new_crtc_state;
1812 struct drm_plane *plane;
1813 struct drm_plane_state *new_plane_state;
1814 struct dm_plane_state *dm_new_plane_state;
1815 struct dm_atomic_state *dm_state = to_dm_atomic_state(dm->atomic_obj.state);
1816 enum dc_connection_type new_connection_type = dc_connection_none;
1817 struct dc_state *dc_state;
1820 if (adev->in_gpu_reset) {
1821 dc_state = dm->cached_dc_state;
1823 r = dm_dmub_hw_init(adev);
1825 DRM_ERROR("DMUB interface failed to initialize: status=%d\n", r);
1827 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0);
1830 amdgpu_dm_irq_resume_early(adev);
1832 for (i = 0; i < dc_state->stream_count; i++) {
1833 dc_state->streams[i]->mode_changed = true;
1834 for (j = 0; j < dc_state->stream_status->plane_count; j++) {
1835 dc_state->stream_status->plane_states[j]->update_flags.raw
1840 WARN_ON(!dc_commit_state(dm->dc, dc_state));
1842 dm_gpureset_commit_state(dm->cached_dc_state, dm);
1844 dm_gpureset_toggle_interrupts(adev, dm->cached_dc_state, true);
1846 dc_release_state(dm->cached_dc_state);
1847 dm->cached_dc_state = NULL;
1849 amdgpu_dm_irq_resume_late(adev);
1851 mutex_unlock(&dm->dc_lock);
1855 /* Recreate dc_state - DC invalidates it when setting power state to S3. */
1856 dc_release_state(dm_state->context);
1857 dm_state->context = dc_create_state(dm->dc);
1858 /* TODO: Remove dc_state->dccg, use dc->dccg directly. */
1859 dc_resource_state_construct(dm->dc, dm_state->context);
1861 /* Before powering on DC we need to re-initialize DMUB. */
1862 r = dm_dmub_hw_init(adev);
1864 DRM_ERROR("DMUB interface failed to initialize: status=%d\n", r);
1866 /* power on hardware */
1867 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0);
1869 /* program HPD filter */
1873 * early enable HPD Rx IRQ, should be done before set mode as short
1874 * pulse interrupts are used for MST
1876 amdgpu_dm_irq_resume_early(adev);
1878 /* On resume we need to rewrite the MSTM control bits to enable MST*/
1879 s3_handle_mst(ddev, false);
1882 drm_connector_list_iter_begin(ddev, &iter);
1883 drm_for_each_connector_iter(connector, &iter) {
1884 aconnector = to_amdgpu_dm_connector(connector);
1887 * this is the case when traversing through already created
1888 * MST connectors, should be skipped
1890 if (aconnector->mst_port)
1893 mutex_lock(&aconnector->hpd_lock);
1894 if (!dc_link_detect_sink(aconnector->dc_link, &new_connection_type))
1895 DRM_ERROR("KMS: Failed to detect connector\n");
1897 if (aconnector->base.force && new_connection_type == dc_connection_none)
1898 emulated_link_detect(aconnector->dc_link);
1900 dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
1902 if (aconnector->fake_enable && aconnector->dc_link->local_sink)
1903 aconnector->fake_enable = false;
1905 if (aconnector->dc_sink)
1906 dc_sink_release(aconnector->dc_sink);
1907 aconnector->dc_sink = NULL;
1908 amdgpu_dm_update_connector_after_detect(aconnector);
1909 mutex_unlock(&aconnector->hpd_lock);
1911 drm_connector_list_iter_end(&iter);
1913 /* Force mode set in atomic commit */
1914 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i)
1915 new_crtc_state->active_changed = true;
1918 * atomic_check is expected to create the dc states. We need to release
1919 * them here, since they were duplicated as part of the suspend
1922 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) {
1923 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
1924 if (dm_new_crtc_state->stream) {
1925 WARN_ON(kref_read(&dm_new_crtc_state->stream->refcount) > 1);
1926 dc_stream_release(dm_new_crtc_state->stream);
1927 dm_new_crtc_state->stream = NULL;
1931 for_each_new_plane_in_state(dm->cached_state, plane, new_plane_state, i) {
1932 dm_new_plane_state = to_dm_plane_state(new_plane_state);
1933 if (dm_new_plane_state->dc_state) {
1934 WARN_ON(kref_read(&dm_new_plane_state->dc_state->refcount) > 1);
1935 dc_plane_state_release(dm_new_plane_state->dc_state);
1936 dm_new_plane_state->dc_state = NULL;
1940 drm_atomic_helper_resume(ddev, dm->cached_state);
1942 dm->cached_state = NULL;
1944 amdgpu_dm_irq_resume_late(adev);
1946 amdgpu_dm_smu_write_watermarks_table(adev);
1954 * DM (and consequently DC) is registered in the amdgpu base driver as a IP
1955 * block. When CONFIG_DRM_AMD_DC is enabled, the DM device IP block is added to
1956 * the base driver's device list to be initialized and torn down accordingly.
1958 * The functions to do so are provided as hooks in &struct amd_ip_funcs.
1961 static const struct amd_ip_funcs amdgpu_dm_funcs = {
1963 .early_init = dm_early_init,
1964 .late_init = dm_late_init,
1965 .sw_init = dm_sw_init,
1966 .sw_fini = dm_sw_fini,
1967 .hw_init = dm_hw_init,
1968 .hw_fini = dm_hw_fini,
1969 .suspend = dm_suspend,
1970 .resume = dm_resume,
1971 .is_idle = dm_is_idle,
1972 .wait_for_idle = dm_wait_for_idle,
1973 .check_soft_reset = dm_check_soft_reset,
1974 .soft_reset = dm_soft_reset,
1975 .set_clockgating_state = dm_set_clockgating_state,
1976 .set_powergating_state = dm_set_powergating_state,
1979 const struct amdgpu_ip_block_version dm_ip_block =
1981 .type = AMD_IP_BLOCK_TYPE_DCE,
1985 .funcs = &amdgpu_dm_funcs,
1995 static const struct drm_mode_config_funcs amdgpu_dm_mode_funcs = {
1996 .fb_create = amdgpu_display_user_framebuffer_create,
1997 .output_poll_changed = drm_fb_helper_output_poll_changed,
1998 .atomic_check = amdgpu_dm_atomic_check,
1999 .atomic_commit = amdgpu_dm_atomic_commit,
2002 static struct drm_mode_config_helper_funcs amdgpu_dm_mode_config_helperfuncs = {
2003 .atomic_commit_tail = amdgpu_dm_atomic_commit_tail
2006 static void update_connector_ext_caps(struct amdgpu_dm_connector *aconnector)
2008 u32 max_cll, min_cll, max, min, q, r;
2009 struct amdgpu_dm_backlight_caps *caps;
2010 struct amdgpu_display_manager *dm;
2011 struct drm_connector *conn_base;
2012 struct amdgpu_device *adev;
2013 static const u8 pre_computed_values[] = {
2014 50, 51, 52, 53, 55, 56, 57, 58, 59, 61, 62, 63, 65, 66, 68, 69,
2015 71, 72, 74, 75, 77, 79, 81, 82, 84, 86, 88, 90, 92, 94, 96, 98};
2017 if (!aconnector || !aconnector->dc_link)
2020 conn_base = &aconnector->base;
2021 adev = conn_base->dev->dev_private;
2023 caps = &dm->backlight_caps;
2024 caps->ext_caps = &aconnector->dc_link->dpcd_sink_ext_caps;
2025 caps->aux_support = false;
2026 max_cll = conn_base->hdr_sink_metadata.hdmi_type1.max_cll;
2027 min_cll = conn_base->hdr_sink_metadata.hdmi_type1.min_cll;
2029 if (caps->ext_caps->bits.oled == 1 ||
2030 caps->ext_caps->bits.sdr_aux_backlight_control == 1 ||
2031 caps->ext_caps->bits.hdr_aux_backlight_control == 1)
2032 caps->aux_support = true;
2034 /* From the specification (CTA-861-G), for calculating the maximum
2035 * luminance we need to use:
2036 * Luminance = 50*2**(CV/32)
2037 * Where CV is a one-byte value.
2038 * For calculating this expression we may need float point precision;
2039 * to avoid this complexity level, we take advantage that CV is divided
2040 * by a constant. From the Euclids division algorithm, we know that CV
2041 * can be written as: CV = 32*q + r. Next, we replace CV in the
2042 * Luminance expression and get 50*(2**q)*(2**(r/32)), hence we just
2043 * need to pre-compute the value of r/32. For pre-computing the values
2044 * We just used the following Ruby line:
2045 * (0...32).each {|cv| puts (50*2**(cv/32.0)).round}
2046 * The results of the above expressions can be verified at
2047 * pre_computed_values.
2051 max = (1 << q) * pre_computed_values[r];
2053 // min luminance: maxLum * (CV/255)^2 / 100
2054 q = DIV_ROUND_CLOSEST(min_cll, 255);
2055 min = max * DIV_ROUND_CLOSEST((q * q), 100);
2057 caps->aux_max_input_signal = max;
2058 caps->aux_min_input_signal = min;
2061 void amdgpu_dm_update_connector_after_detect(
2062 struct amdgpu_dm_connector *aconnector)
2064 struct drm_connector *connector = &aconnector->base;
2065 struct drm_device *dev = connector->dev;
2066 struct dc_sink *sink;
2068 /* MST handled by drm_mst framework */
2069 if (aconnector->mst_mgr.mst_state == true)
2073 sink = aconnector->dc_link->local_sink;
2075 dc_sink_retain(sink);
2078 * Edid mgmt connector gets first update only in mode_valid hook and then
2079 * the connector sink is set to either fake or physical sink depends on link status.
2080 * Skip if already done during boot.
2082 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED
2083 && aconnector->dc_em_sink) {
2086 * For S3 resume with headless use eml_sink to fake stream
2087 * because on resume connector->sink is set to NULL
2089 mutex_lock(&dev->mode_config.mutex);
2092 if (aconnector->dc_sink) {
2093 amdgpu_dm_update_freesync_caps(connector, NULL);
2095 * retain and release below are used to
2096 * bump up refcount for sink because the link doesn't point
2097 * to it anymore after disconnect, so on next crtc to connector
2098 * reshuffle by UMD we will get into unwanted dc_sink release
2100 dc_sink_release(aconnector->dc_sink);
2102 aconnector->dc_sink = sink;
2103 dc_sink_retain(aconnector->dc_sink);
2104 amdgpu_dm_update_freesync_caps(connector,
2107 amdgpu_dm_update_freesync_caps(connector, NULL);
2108 if (!aconnector->dc_sink) {
2109 aconnector->dc_sink = aconnector->dc_em_sink;
2110 dc_sink_retain(aconnector->dc_sink);
2114 mutex_unlock(&dev->mode_config.mutex);
2117 dc_sink_release(sink);
2122 * TODO: temporary guard to look for proper fix
2123 * if this sink is MST sink, we should not do anything
2125 if (sink && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
2126 dc_sink_release(sink);
2130 if (aconnector->dc_sink == sink) {
2132 * We got a DP short pulse (Link Loss, DP CTS, etc...).
2135 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: dc_sink didn't change.\n",
2136 aconnector->connector_id);
2138 dc_sink_release(sink);
2142 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: Old sink=%p New sink=%p\n",
2143 aconnector->connector_id, aconnector->dc_sink, sink);
2145 mutex_lock(&dev->mode_config.mutex);
2148 * 1. Update status of the drm connector
2149 * 2. Send an event and let userspace tell us what to do
2153 * TODO: check if we still need the S3 mode update workaround.
2154 * If yes, put it here.
2156 if (aconnector->dc_sink)
2157 amdgpu_dm_update_freesync_caps(connector, NULL);
2159 aconnector->dc_sink = sink;
2160 dc_sink_retain(aconnector->dc_sink);
2161 if (sink->dc_edid.length == 0) {
2162 aconnector->edid = NULL;
2163 if (aconnector->dc_link->aux_mode) {
2164 drm_dp_cec_unset_edid(
2165 &aconnector->dm_dp_aux.aux);
2169 (struct edid *)sink->dc_edid.raw_edid;
2171 drm_connector_update_edid_property(connector,
2174 if (aconnector->dc_link->aux_mode)
2175 drm_dp_cec_set_edid(&aconnector->dm_dp_aux.aux,
2179 amdgpu_dm_update_freesync_caps(connector, aconnector->edid);
2180 update_connector_ext_caps(aconnector);
2182 drm_dp_cec_unset_edid(&aconnector->dm_dp_aux.aux);
2183 amdgpu_dm_update_freesync_caps(connector, NULL);
2184 drm_connector_update_edid_property(connector, NULL);
2185 aconnector->num_modes = 0;
2186 dc_sink_release(aconnector->dc_sink);
2187 aconnector->dc_sink = NULL;
2188 aconnector->edid = NULL;
2189 #ifdef CONFIG_DRM_AMD_DC_HDCP
2190 /* Set CP to DESIRED if it was ENABLED, so we can re-enable it again on hotplug */
2191 if (connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED)
2192 connector->state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
2196 mutex_unlock(&dev->mode_config.mutex);
2199 dc_sink_release(sink);
2202 static void handle_hpd_irq(void *param)
2204 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
2205 struct drm_connector *connector = &aconnector->base;
2206 struct drm_device *dev = connector->dev;
2207 enum dc_connection_type new_connection_type = dc_connection_none;
2208 #ifdef CONFIG_DRM_AMD_DC_HDCP
2209 struct amdgpu_device *adev = dev->dev_private;
2213 * In case of failure or MST no need to update connector status or notify the OS
2214 * since (for MST case) MST does this in its own context.
2216 mutex_lock(&aconnector->hpd_lock);
2218 #ifdef CONFIG_DRM_AMD_DC_HDCP
2219 if (adev->dm.hdcp_workqueue)
2220 hdcp_reset_display(adev->dm.hdcp_workqueue, aconnector->dc_link->link_index);
2222 if (aconnector->fake_enable)
2223 aconnector->fake_enable = false;
2225 if (!dc_link_detect_sink(aconnector->dc_link, &new_connection_type))
2226 DRM_ERROR("KMS: Failed to detect connector\n");
2228 if (aconnector->base.force && new_connection_type == dc_connection_none) {
2229 emulated_link_detect(aconnector->dc_link);
2232 drm_modeset_lock_all(dev);
2233 dm_restore_drm_connector_state(dev, connector);
2234 drm_modeset_unlock_all(dev);
2236 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
2237 drm_kms_helper_hotplug_event(dev);
2239 } else if (dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD)) {
2240 amdgpu_dm_update_connector_after_detect(aconnector);
2243 drm_modeset_lock_all(dev);
2244 dm_restore_drm_connector_state(dev, connector);
2245 drm_modeset_unlock_all(dev);
2247 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
2248 drm_kms_helper_hotplug_event(dev);
2250 mutex_unlock(&aconnector->hpd_lock);
2254 static void dm_handle_hpd_rx_irq(struct amdgpu_dm_connector *aconnector)
2256 uint8_t esi[DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI] = { 0 };
2258 bool new_irq_handled = false;
2260 int dpcd_bytes_to_read;
2262 const int max_process_count = 30;
2263 int process_count = 0;
2265 const struct dc_link_status *link_status = dc_link_get_status(aconnector->dc_link);
2267 if (link_status->dpcd_caps->dpcd_rev.raw < 0x12) {
2268 dpcd_bytes_to_read = DP_LANE0_1_STATUS - DP_SINK_COUNT;
2269 /* DPCD 0x200 - 0x201 for downstream IRQ */
2270 dpcd_addr = DP_SINK_COUNT;
2272 dpcd_bytes_to_read = DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI;
2273 /* DPCD 0x2002 - 0x2005 for downstream IRQ */
2274 dpcd_addr = DP_SINK_COUNT_ESI;
2277 dret = drm_dp_dpcd_read(
2278 &aconnector->dm_dp_aux.aux,
2281 dpcd_bytes_to_read);
2283 while (dret == dpcd_bytes_to_read &&
2284 process_count < max_process_count) {
2290 DRM_DEBUG_DRIVER("ESI %02x %02x %02x\n", esi[0], esi[1], esi[2]);
2291 /* handle HPD short pulse irq */
2292 if (aconnector->mst_mgr.mst_state)
2294 &aconnector->mst_mgr,
2298 if (new_irq_handled) {
2299 /* ACK at DPCD to notify down stream */
2300 const int ack_dpcd_bytes_to_write =
2301 dpcd_bytes_to_read - 1;
2303 for (retry = 0; retry < 3; retry++) {
2306 wret = drm_dp_dpcd_write(
2307 &aconnector->dm_dp_aux.aux,
2310 ack_dpcd_bytes_to_write);
2311 if (wret == ack_dpcd_bytes_to_write)
2315 /* check if there is new irq to be handled */
2316 dret = drm_dp_dpcd_read(
2317 &aconnector->dm_dp_aux.aux,
2320 dpcd_bytes_to_read);
2322 new_irq_handled = false;
2328 if (process_count == max_process_count)
2329 DRM_DEBUG_DRIVER("Loop exceeded max iterations\n");
2332 static void handle_hpd_rx_irq(void *param)
2334 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
2335 struct drm_connector *connector = &aconnector->base;
2336 struct drm_device *dev = connector->dev;
2337 struct dc_link *dc_link = aconnector->dc_link;
2338 bool is_mst_root_connector = aconnector->mst_mgr.mst_state;
2339 enum dc_connection_type new_connection_type = dc_connection_none;
2340 #ifdef CONFIG_DRM_AMD_DC_HDCP
2341 union hpd_irq_data hpd_irq_data;
2342 struct amdgpu_device *adev = dev->dev_private;
2344 memset(&hpd_irq_data, 0, sizeof(hpd_irq_data));
2348 * TODO:Temporary add mutex to protect hpd interrupt not have a gpio
2349 * conflict, after implement i2c helper, this mutex should be
2352 if (dc_link->type != dc_connection_mst_branch)
2353 mutex_lock(&aconnector->hpd_lock);
2356 #ifdef CONFIG_DRM_AMD_DC_HDCP
2357 if (dc_link_handle_hpd_rx_irq(dc_link, &hpd_irq_data, NULL) &&
2359 if (dc_link_handle_hpd_rx_irq(dc_link, NULL, NULL) &&
2361 !is_mst_root_connector) {
2362 /* Downstream Port status changed. */
2363 if (!dc_link_detect_sink(dc_link, &new_connection_type))
2364 DRM_ERROR("KMS: Failed to detect connector\n");
2366 if (aconnector->base.force && new_connection_type == dc_connection_none) {
2367 emulated_link_detect(dc_link);
2369 if (aconnector->fake_enable)
2370 aconnector->fake_enable = false;
2372 amdgpu_dm_update_connector_after_detect(aconnector);
2375 drm_modeset_lock_all(dev);
2376 dm_restore_drm_connector_state(dev, connector);
2377 drm_modeset_unlock_all(dev);
2379 drm_kms_helper_hotplug_event(dev);
2380 } else if (dc_link_detect(dc_link, DETECT_REASON_HPDRX)) {
2382 if (aconnector->fake_enable)
2383 aconnector->fake_enable = false;
2385 amdgpu_dm_update_connector_after_detect(aconnector);
2388 drm_modeset_lock_all(dev);
2389 dm_restore_drm_connector_state(dev, connector);
2390 drm_modeset_unlock_all(dev);
2392 drm_kms_helper_hotplug_event(dev);
2395 #ifdef CONFIG_DRM_AMD_DC_HDCP
2396 if (hpd_irq_data.bytes.device_service_irq.bits.CP_IRQ) {
2397 if (adev->dm.hdcp_workqueue)
2398 hdcp_handle_cpirq(adev->dm.hdcp_workqueue, aconnector->base.index);
2401 if ((dc_link->cur_link_settings.lane_count != LANE_COUNT_UNKNOWN) ||
2402 (dc_link->type == dc_connection_mst_branch))
2403 dm_handle_hpd_rx_irq(aconnector);
2405 if (dc_link->type != dc_connection_mst_branch) {
2406 drm_dp_cec_irq(&aconnector->dm_dp_aux.aux);
2407 mutex_unlock(&aconnector->hpd_lock);
2411 static void register_hpd_handlers(struct amdgpu_device *adev)
2413 struct drm_device *dev = adev->ddev;
2414 struct drm_connector *connector;
2415 struct amdgpu_dm_connector *aconnector;
2416 const struct dc_link *dc_link;
2417 struct dc_interrupt_params int_params = {0};
2419 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
2420 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
2422 list_for_each_entry(connector,
2423 &dev->mode_config.connector_list, head) {
2425 aconnector = to_amdgpu_dm_connector(connector);
2426 dc_link = aconnector->dc_link;
2428 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd) {
2429 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
2430 int_params.irq_source = dc_link->irq_source_hpd;
2432 amdgpu_dm_irq_register_interrupt(adev, &int_params,
2434 (void *) aconnector);
2437 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd_rx) {
2439 /* Also register for DP short pulse (hpd_rx). */
2440 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
2441 int_params.irq_source = dc_link->irq_source_hpd_rx;
2443 amdgpu_dm_irq_register_interrupt(adev, &int_params,
2445 (void *) aconnector);
2450 /* Register IRQ sources and initialize IRQ callbacks */
2451 static int dce110_register_irq_handlers(struct amdgpu_device *adev)
2453 struct dc *dc = adev->dm.dc;
2454 struct common_irq_params *c_irq_params;
2455 struct dc_interrupt_params int_params = {0};
2458 unsigned client_id = AMDGPU_IRQ_CLIENTID_LEGACY;
2460 if (adev->asic_type >= CHIP_VEGA10)
2461 client_id = SOC15_IH_CLIENTID_DCE;
2463 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
2464 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
2467 * Actions of amdgpu_irq_add_id():
2468 * 1. Register a set() function with base driver.
2469 * Base driver will call set() function to enable/disable an
2470 * interrupt in DC hardware.
2471 * 2. Register amdgpu_dm_irq_handler().
2472 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
2473 * coming from DC hardware.
2474 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
2475 * for acknowledging and handling. */
2477 /* Use VBLANK interrupt */
2478 for (i = VISLANDS30_IV_SRCID_D1_VERTICAL_INTERRUPT0; i <= VISLANDS30_IV_SRCID_D6_VERTICAL_INTERRUPT0; i++) {
2479 r = amdgpu_irq_add_id(adev, client_id, i, &adev->crtc_irq);
2481 DRM_ERROR("Failed to add crtc irq id!\n");
2485 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
2486 int_params.irq_source =
2487 dc_interrupt_to_irq_source(dc, i, 0);
2489 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
2491 c_irq_params->adev = adev;
2492 c_irq_params->irq_src = int_params.irq_source;
2494 amdgpu_dm_irq_register_interrupt(adev, &int_params,
2495 dm_crtc_high_irq, c_irq_params);
2498 /* Use VUPDATE interrupt */
2499 for (i = VISLANDS30_IV_SRCID_D1_V_UPDATE_INT; i <= VISLANDS30_IV_SRCID_D6_V_UPDATE_INT; i += 2) {
2500 r = amdgpu_irq_add_id(adev, client_id, i, &adev->vupdate_irq);
2502 DRM_ERROR("Failed to add vupdate irq id!\n");
2506 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
2507 int_params.irq_source =
2508 dc_interrupt_to_irq_source(dc, i, 0);
2510 c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1];
2512 c_irq_params->adev = adev;
2513 c_irq_params->irq_src = int_params.irq_source;
2515 amdgpu_dm_irq_register_interrupt(adev, &int_params,
2516 dm_vupdate_high_irq, c_irq_params);
2519 /* Use GRPH_PFLIP interrupt */
2520 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
2521 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
2522 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq);
2524 DRM_ERROR("Failed to add page flip irq id!\n");
2528 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
2529 int_params.irq_source =
2530 dc_interrupt_to_irq_source(dc, i, 0);
2532 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
2534 c_irq_params->adev = adev;
2535 c_irq_params->irq_src = int_params.irq_source;
2537 amdgpu_dm_irq_register_interrupt(adev, &int_params,
2538 dm_pflip_high_irq, c_irq_params);
2543 r = amdgpu_irq_add_id(adev, client_id,
2544 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
2546 DRM_ERROR("Failed to add hpd irq id!\n");
2550 register_hpd_handlers(adev);
2555 #if defined(CONFIG_DRM_AMD_DC_DCN)
2556 /* Register IRQ sources and initialize IRQ callbacks */
2557 static int dcn10_register_irq_handlers(struct amdgpu_device *adev)
2559 struct dc *dc = adev->dm.dc;
2560 struct common_irq_params *c_irq_params;
2561 struct dc_interrupt_params int_params = {0};
2565 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
2566 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
2569 * Actions of amdgpu_irq_add_id():
2570 * 1. Register a set() function with base driver.
2571 * Base driver will call set() function to enable/disable an
2572 * interrupt in DC hardware.
2573 * 2. Register amdgpu_dm_irq_handler().
2574 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
2575 * coming from DC hardware.
2576 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
2577 * for acknowledging and handling.
2580 /* Use VSTARTUP interrupt */
2581 for (i = DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP;
2582 i <= DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP + adev->mode_info.num_crtc - 1;
2584 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->crtc_irq);
2587 DRM_ERROR("Failed to add crtc irq id!\n");
2591 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
2592 int_params.irq_source =
2593 dc_interrupt_to_irq_source(dc, i, 0);
2595 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
2597 c_irq_params->adev = adev;
2598 c_irq_params->irq_src = int_params.irq_source;
2600 amdgpu_dm_irq_register_interrupt(
2601 adev, &int_params, dm_crtc_high_irq, c_irq_params);
2604 /* Use VUPDATE_NO_LOCK interrupt on DCN, which seems to correspond to
2605 * the regular VUPDATE interrupt on DCE. We want DC_IRQ_SOURCE_VUPDATEx
2606 * to trigger at end of each vblank, regardless of state of the lock,
2607 * matching DCE behaviour.
2609 for (i = DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT;
2610 i <= DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT + adev->mode_info.num_crtc - 1;
2612 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->vupdate_irq);
2615 DRM_ERROR("Failed to add vupdate irq id!\n");
2619 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
2620 int_params.irq_source =
2621 dc_interrupt_to_irq_source(dc, i, 0);
2623 c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1];
2625 c_irq_params->adev = adev;
2626 c_irq_params->irq_src = int_params.irq_source;
2628 amdgpu_dm_irq_register_interrupt(adev, &int_params,
2629 dm_vupdate_high_irq, c_irq_params);
2632 /* Use GRPH_PFLIP interrupt */
2633 for (i = DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT;
2634 i <= DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT + adev->mode_info.num_crtc - 1;
2636 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->pageflip_irq);
2638 DRM_ERROR("Failed to add page flip irq id!\n");
2642 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
2643 int_params.irq_source =
2644 dc_interrupt_to_irq_source(dc, i, 0);
2646 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
2648 c_irq_params->adev = adev;
2649 c_irq_params->irq_src = int_params.irq_source;
2651 amdgpu_dm_irq_register_interrupt(adev, &int_params,
2652 dm_pflip_high_irq, c_irq_params);
2657 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DC_HPD1_INT,
2660 DRM_ERROR("Failed to add hpd irq id!\n");
2664 register_hpd_handlers(adev);
2671 * Acquires the lock for the atomic state object and returns
2672 * the new atomic state.
2674 * This should only be called during atomic check.
2676 static int dm_atomic_get_state(struct drm_atomic_state *state,
2677 struct dm_atomic_state **dm_state)
2679 struct drm_device *dev = state->dev;
2680 struct amdgpu_device *adev = dev->dev_private;
2681 struct amdgpu_display_manager *dm = &adev->dm;
2682 struct drm_private_state *priv_state;
2687 priv_state = drm_atomic_get_private_obj_state(state, &dm->atomic_obj);
2688 if (IS_ERR(priv_state))
2689 return PTR_ERR(priv_state);
2691 *dm_state = to_dm_atomic_state(priv_state);
2696 struct dm_atomic_state *
2697 dm_atomic_get_new_state(struct drm_atomic_state *state)
2699 struct drm_device *dev = state->dev;
2700 struct amdgpu_device *adev = dev->dev_private;
2701 struct amdgpu_display_manager *dm = &adev->dm;
2702 struct drm_private_obj *obj;
2703 struct drm_private_state *new_obj_state;
2706 for_each_new_private_obj_in_state(state, obj, new_obj_state, i) {
2707 if (obj->funcs == dm->atomic_obj.funcs)
2708 return to_dm_atomic_state(new_obj_state);
2714 struct dm_atomic_state *
2715 dm_atomic_get_old_state(struct drm_atomic_state *state)
2717 struct drm_device *dev = state->dev;
2718 struct amdgpu_device *adev = dev->dev_private;
2719 struct amdgpu_display_manager *dm = &adev->dm;
2720 struct drm_private_obj *obj;
2721 struct drm_private_state *old_obj_state;
2724 for_each_old_private_obj_in_state(state, obj, old_obj_state, i) {
2725 if (obj->funcs == dm->atomic_obj.funcs)
2726 return to_dm_atomic_state(old_obj_state);
2732 static struct drm_private_state *
2733 dm_atomic_duplicate_state(struct drm_private_obj *obj)
2735 struct dm_atomic_state *old_state, *new_state;
2737 new_state = kzalloc(sizeof(*new_state), GFP_KERNEL);
2741 __drm_atomic_helper_private_obj_duplicate_state(obj, &new_state->base);
2743 old_state = to_dm_atomic_state(obj->state);
2745 if (old_state && old_state->context)
2746 new_state->context = dc_copy_state(old_state->context);
2748 if (!new_state->context) {
2753 return &new_state->base;
2756 static void dm_atomic_destroy_state(struct drm_private_obj *obj,
2757 struct drm_private_state *state)
2759 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
2761 if (dm_state && dm_state->context)
2762 dc_release_state(dm_state->context);
2767 static struct drm_private_state_funcs dm_atomic_state_funcs = {
2768 .atomic_duplicate_state = dm_atomic_duplicate_state,
2769 .atomic_destroy_state = dm_atomic_destroy_state,
2772 static int amdgpu_dm_mode_config_init(struct amdgpu_device *adev)
2774 struct dm_atomic_state *state;
2777 adev->mode_info.mode_config_initialized = true;
2779 adev->ddev->mode_config.funcs = (void *)&amdgpu_dm_mode_funcs;
2780 adev->ddev->mode_config.helper_private = &amdgpu_dm_mode_config_helperfuncs;
2782 adev->ddev->mode_config.max_width = 16384;
2783 adev->ddev->mode_config.max_height = 16384;
2785 adev->ddev->mode_config.preferred_depth = 24;
2786 adev->ddev->mode_config.prefer_shadow = 1;
2787 /* indicates support for immediate flip */
2788 adev->ddev->mode_config.async_page_flip = true;
2790 adev->ddev->mode_config.fb_base = adev->gmc.aper_base;
2792 state = kzalloc(sizeof(*state), GFP_KERNEL);
2796 state->context = dc_create_state(adev->dm.dc);
2797 if (!state->context) {
2802 dc_resource_state_copy_construct_current(adev->dm.dc, state->context);
2804 drm_atomic_private_obj_init(adev->ddev,
2805 &adev->dm.atomic_obj,
2807 &dm_atomic_state_funcs);
2809 r = amdgpu_display_modeset_create_props(adev);
2813 r = amdgpu_dm_audio_init(adev);
2820 #define AMDGPU_DM_DEFAULT_MIN_BACKLIGHT 12
2821 #define AMDGPU_DM_DEFAULT_MAX_BACKLIGHT 255
2822 #define AUX_BL_DEFAULT_TRANSITION_TIME_MS 50
2824 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
2825 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
2827 static void amdgpu_dm_update_backlight_caps(struct amdgpu_display_manager *dm)
2829 #if defined(CONFIG_ACPI)
2830 struct amdgpu_dm_backlight_caps caps;
2832 if (dm->backlight_caps.caps_valid)
2835 amdgpu_acpi_get_backlight_caps(dm->adev, &caps);
2836 if (caps.caps_valid) {
2837 dm->backlight_caps.caps_valid = true;
2838 if (caps.aux_support)
2840 dm->backlight_caps.min_input_signal = caps.min_input_signal;
2841 dm->backlight_caps.max_input_signal = caps.max_input_signal;
2843 dm->backlight_caps.min_input_signal =
2844 AMDGPU_DM_DEFAULT_MIN_BACKLIGHT;
2845 dm->backlight_caps.max_input_signal =
2846 AMDGPU_DM_DEFAULT_MAX_BACKLIGHT;
2849 if (dm->backlight_caps.aux_support)
2852 dm->backlight_caps.min_input_signal = AMDGPU_DM_DEFAULT_MIN_BACKLIGHT;
2853 dm->backlight_caps.max_input_signal = AMDGPU_DM_DEFAULT_MAX_BACKLIGHT;
2857 static int set_backlight_via_aux(struct dc_link *link, uint32_t brightness)
2864 rc = dc_link_set_backlight_level_nits(link, true, brightness,
2865 AUX_BL_DEFAULT_TRANSITION_TIME_MS);
2870 static u32 convert_brightness(const struct amdgpu_dm_backlight_caps *caps,
2871 const uint32_t user_brightness)
2873 u32 min, max, conversion_pace;
2874 u32 brightness = user_brightness;
2879 if (!caps->aux_support) {
2880 max = caps->max_input_signal;
2881 min = caps->min_input_signal;
2883 * The brightness input is in the range 0-255
2884 * It needs to be rescaled to be between the
2885 * requested min and max input signal
2886 * It also needs to be scaled up by 0x101 to
2887 * match the DC interface which has a range of
2890 conversion_pace = 0x101;
2895 / AMDGPU_MAX_BL_LEVEL
2896 + min * conversion_pace;
2899 * We are doing a linear interpolation here, which is OK but
2900 * does not provide the optimal result. We probably want
2901 * something close to the Perceptual Quantizer (PQ) curve.
2903 max = caps->aux_max_input_signal;
2904 min = caps->aux_min_input_signal;
2906 brightness = (AMDGPU_MAX_BL_LEVEL - user_brightness) * min
2907 + user_brightness * max;
2908 // Multiple the value by 1000 since we use millinits
2910 brightness = DIV_ROUND_CLOSEST(brightness, AMDGPU_MAX_BL_LEVEL);
2917 static int amdgpu_dm_backlight_update_status(struct backlight_device *bd)
2919 struct amdgpu_display_manager *dm = bl_get_data(bd);
2920 struct amdgpu_dm_backlight_caps caps;
2921 struct dc_link *link = NULL;
2925 amdgpu_dm_update_backlight_caps(dm);
2926 caps = dm->backlight_caps;
2928 link = (struct dc_link *)dm->backlight_link;
2930 brightness = convert_brightness(&caps, bd->props.brightness);
2931 // Change brightness based on AUX property
2932 if (caps.aux_support)
2933 return set_backlight_via_aux(link, brightness);
2935 rc = dc_link_set_backlight_level(dm->backlight_link, brightness, 0);
2940 static int amdgpu_dm_backlight_get_brightness(struct backlight_device *bd)
2942 struct amdgpu_display_manager *dm = bl_get_data(bd);
2943 int ret = dc_link_get_backlight_level(dm->backlight_link);
2945 if (ret == DC_ERROR_UNEXPECTED)
2946 return bd->props.brightness;
2950 static const struct backlight_ops amdgpu_dm_backlight_ops = {
2951 .options = BL_CORE_SUSPENDRESUME,
2952 .get_brightness = amdgpu_dm_backlight_get_brightness,
2953 .update_status = amdgpu_dm_backlight_update_status,
2957 amdgpu_dm_register_backlight_device(struct amdgpu_display_manager *dm)
2960 struct backlight_properties props = { 0 };
2962 amdgpu_dm_update_backlight_caps(dm);
2964 props.max_brightness = AMDGPU_MAX_BL_LEVEL;
2965 props.brightness = AMDGPU_MAX_BL_LEVEL;
2966 props.type = BACKLIGHT_RAW;
2968 snprintf(bl_name, sizeof(bl_name), "amdgpu_bl%d",
2969 dm->adev->ddev->primary->index);
2971 dm->backlight_dev = backlight_device_register(bl_name,
2972 dm->adev->ddev->dev,
2974 &amdgpu_dm_backlight_ops,
2977 if (IS_ERR(dm->backlight_dev))
2978 DRM_ERROR("DM: Backlight registration failed!\n");
2980 DRM_DEBUG_DRIVER("DM: Registered Backlight device: %s\n", bl_name);
2985 static int initialize_plane(struct amdgpu_display_manager *dm,
2986 struct amdgpu_mode_info *mode_info, int plane_id,
2987 enum drm_plane_type plane_type,
2988 const struct dc_plane_cap *plane_cap)
2990 struct drm_plane *plane;
2991 unsigned long possible_crtcs;
2994 plane = kzalloc(sizeof(struct drm_plane), GFP_KERNEL);
2996 DRM_ERROR("KMS: Failed to allocate plane\n");
2999 plane->type = plane_type;
3002 * HACK: IGT tests expect that the primary plane for a CRTC
3003 * can only have one possible CRTC. Only expose support for
3004 * any CRTC if they're not going to be used as a primary plane
3005 * for a CRTC - like overlay or underlay planes.
3007 possible_crtcs = 1 << plane_id;
3008 if (plane_id >= dm->dc->caps.max_streams)
3009 possible_crtcs = 0xff;
3011 ret = amdgpu_dm_plane_init(dm, plane, possible_crtcs, plane_cap);
3014 DRM_ERROR("KMS: Failed to initialize plane\n");
3020 mode_info->planes[plane_id] = plane;
3026 static void register_backlight_device(struct amdgpu_display_manager *dm,
3027 struct dc_link *link)
3029 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
3030 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
3032 if ((link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) &&
3033 link->type != dc_connection_none) {
3035 * Event if registration failed, we should continue with
3036 * DM initialization because not having a backlight control
3037 * is better then a black screen.
3039 amdgpu_dm_register_backlight_device(dm);
3041 if (dm->backlight_dev)
3042 dm->backlight_link = link;
3049 * In this architecture, the association
3050 * connector -> encoder -> crtc
3051 * id not really requried. The crtc and connector will hold the
3052 * display_index as an abstraction to use with DAL component
3054 * Returns 0 on success
3056 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev)
3058 struct amdgpu_display_manager *dm = &adev->dm;
3060 struct amdgpu_dm_connector *aconnector = NULL;
3061 struct amdgpu_encoder *aencoder = NULL;
3062 struct amdgpu_mode_info *mode_info = &adev->mode_info;
3064 int32_t primary_planes;
3065 enum dc_connection_type new_connection_type = dc_connection_none;
3066 const struct dc_plane_cap *plane;
3068 link_cnt = dm->dc->caps.max_links;
3069 if (amdgpu_dm_mode_config_init(dm->adev)) {
3070 DRM_ERROR("DM: Failed to initialize mode config\n");
3074 /* There is one primary plane per CRTC */
3075 primary_planes = dm->dc->caps.max_streams;
3076 ASSERT(primary_planes <= AMDGPU_MAX_PLANES);
3079 * Initialize primary planes, implicit planes for legacy IOCTLS.
3080 * Order is reversed to match iteration order in atomic check.
3082 for (i = (primary_planes - 1); i >= 0; i--) {
3083 plane = &dm->dc->caps.planes[i];
3085 if (initialize_plane(dm, mode_info, i,
3086 DRM_PLANE_TYPE_PRIMARY, plane)) {
3087 DRM_ERROR("KMS: Failed to initialize primary plane\n");
3093 * Initialize overlay planes, index starting after primary planes.
3094 * These planes have a higher DRM index than the primary planes since
3095 * they should be considered as having a higher z-order.
3096 * Order is reversed to match iteration order in atomic check.
3098 * Only support DCN for now, and only expose one so we don't encourage
3099 * userspace to use up all the pipes.
3101 for (i = 0; i < dm->dc->caps.max_planes; ++i) {
3102 struct dc_plane_cap *plane = &dm->dc->caps.planes[i];
3104 if (plane->type != DC_PLANE_TYPE_DCN_UNIVERSAL)
3107 if (!plane->blends_with_above || !plane->blends_with_below)
3110 if (!plane->pixel_format_support.argb8888)
3113 if (initialize_plane(dm, NULL, primary_planes + i,
3114 DRM_PLANE_TYPE_OVERLAY, plane)) {
3115 DRM_ERROR("KMS: Failed to initialize overlay plane\n");
3119 /* Only create one overlay plane. */
3123 for (i = 0; i < dm->dc->caps.max_streams; i++)
3124 if (amdgpu_dm_crtc_init(dm, mode_info->planes[i], i)) {
3125 DRM_ERROR("KMS: Failed to initialize crtc\n");
3129 dm->display_indexes_num = dm->dc->caps.max_streams;
3131 /* loops over all connectors on the board */
3132 for (i = 0; i < link_cnt; i++) {
3133 struct dc_link *link = NULL;
3135 if (i > AMDGPU_DM_MAX_DISPLAY_INDEX) {
3137 "KMS: Cannot support more than %d display indexes\n",
3138 AMDGPU_DM_MAX_DISPLAY_INDEX);
3142 aconnector = kzalloc(sizeof(*aconnector), GFP_KERNEL);
3146 aencoder = kzalloc(sizeof(*aencoder), GFP_KERNEL);
3150 if (amdgpu_dm_encoder_init(dm->ddev, aencoder, i)) {
3151 DRM_ERROR("KMS: Failed to initialize encoder\n");
3155 if (amdgpu_dm_connector_init(dm, aconnector, i, aencoder)) {
3156 DRM_ERROR("KMS: Failed to initialize connector\n");
3160 link = dc_get_link_at_index(dm->dc, i);
3162 if (!dc_link_detect_sink(link, &new_connection_type))
3163 DRM_ERROR("KMS: Failed to detect connector\n");
3165 if (aconnector->base.force && new_connection_type == dc_connection_none) {
3166 emulated_link_detect(link);
3167 amdgpu_dm_update_connector_after_detect(aconnector);
3169 } else if (dc_link_detect(link, DETECT_REASON_BOOT)) {
3170 amdgpu_dm_update_connector_after_detect(aconnector);
3171 register_backlight_device(dm, link);
3172 if (amdgpu_dc_feature_mask & DC_PSR_MASK)
3173 amdgpu_dm_set_psr_caps(link);
3179 /* Software is initialized. Now we can register interrupt handlers. */
3180 switch (adev->asic_type) {
3190 case CHIP_POLARIS11:
3191 case CHIP_POLARIS10:
3192 case CHIP_POLARIS12:
3197 if (dce110_register_irq_handlers(dm->adev)) {
3198 DRM_ERROR("DM: Failed to initialize IRQ\n");
3202 #if defined(CONFIG_DRM_AMD_DC_DCN)
3208 if (dcn10_register_irq_handlers(dm->adev)) {
3209 DRM_ERROR("DM: Failed to initialize IRQ\n");
3215 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type);
3219 /* No userspace support. */
3220 dm->dc->debug.disable_tri_buf = true;
3230 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm)
3232 drm_mode_config_cleanup(dm->ddev);
3233 drm_atomic_private_obj_fini(&dm->atomic_obj);
3237 /******************************************************************************
3238 * amdgpu_display_funcs functions
3239 *****************************************************************************/
3242 * dm_bandwidth_update - program display watermarks
3244 * @adev: amdgpu_device pointer
3246 * Calculate and program the display watermarks and line buffer allocation.
3248 static void dm_bandwidth_update(struct amdgpu_device *adev)
3250 /* TODO: implement later */
3253 static const struct amdgpu_display_funcs dm_display_funcs = {
3254 .bandwidth_update = dm_bandwidth_update, /* called unconditionally */
3255 .vblank_get_counter = dm_vblank_get_counter,/* called unconditionally */
3256 .backlight_set_level = NULL, /* never called for DC */
3257 .backlight_get_level = NULL, /* never called for DC */
3258 .hpd_sense = NULL,/* called unconditionally */
3259 .hpd_set_polarity = NULL, /* called unconditionally */
3260 .hpd_get_gpio_reg = NULL, /* VBIOS parsing. DAL does it. */
3261 .page_flip_get_scanoutpos =
3262 dm_crtc_get_scanoutpos,/* called unconditionally */
3263 .add_encoder = NULL, /* VBIOS parsing. DAL does it. */
3264 .add_connector = NULL, /* VBIOS parsing. DAL does it. */
3267 #if defined(CONFIG_DEBUG_KERNEL_DC)
3269 static ssize_t s3_debug_store(struct device *device,
3270 struct device_attribute *attr,
3276 struct drm_device *drm_dev = dev_get_drvdata(device);
3277 struct amdgpu_device *adev = drm_dev->dev_private;
3279 ret = kstrtoint(buf, 0, &s3_state);
3284 drm_kms_helper_hotplug_event(adev->ddev);
3289 return ret == 0 ? count : 0;
3292 DEVICE_ATTR_WO(s3_debug);
3296 static int dm_early_init(void *handle)
3298 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3300 switch (adev->asic_type) {
3303 adev->mode_info.num_crtc = 6;
3304 adev->mode_info.num_hpd = 6;
3305 adev->mode_info.num_dig = 6;
3308 adev->mode_info.num_crtc = 4;
3309 adev->mode_info.num_hpd = 6;
3310 adev->mode_info.num_dig = 7;
3314 adev->mode_info.num_crtc = 2;
3315 adev->mode_info.num_hpd = 6;
3316 adev->mode_info.num_dig = 6;
3320 adev->mode_info.num_crtc = 6;
3321 adev->mode_info.num_hpd = 6;
3322 adev->mode_info.num_dig = 7;
3325 adev->mode_info.num_crtc = 3;
3326 adev->mode_info.num_hpd = 6;
3327 adev->mode_info.num_dig = 9;
3330 adev->mode_info.num_crtc = 2;
3331 adev->mode_info.num_hpd = 6;
3332 adev->mode_info.num_dig = 9;
3334 case CHIP_POLARIS11:
3335 case CHIP_POLARIS12:
3336 adev->mode_info.num_crtc = 5;
3337 adev->mode_info.num_hpd = 5;
3338 adev->mode_info.num_dig = 5;
3340 case CHIP_POLARIS10:
3342 adev->mode_info.num_crtc = 6;
3343 adev->mode_info.num_hpd = 6;
3344 adev->mode_info.num_dig = 6;
3349 adev->mode_info.num_crtc = 6;
3350 adev->mode_info.num_hpd = 6;
3351 adev->mode_info.num_dig = 6;
3353 #if defined(CONFIG_DRM_AMD_DC_DCN)
3355 adev->mode_info.num_crtc = 4;
3356 adev->mode_info.num_hpd = 4;
3357 adev->mode_info.num_dig = 4;
3362 adev->mode_info.num_crtc = 6;
3363 adev->mode_info.num_hpd = 6;
3364 adev->mode_info.num_dig = 6;
3367 adev->mode_info.num_crtc = 5;
3368 adev->mode_info.num_hpd = 5;
3369 adev->mode_info.num_dig = 5;
3372 adev->mode_info.num_crtc = 4;
3373 adev->mode_info.num_hpd = 4;
3374 adev->mode_info.num_dig = 4;
3377 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type);
3381 amdgpu_dm_set_irq_funcs(adev);
3383 if (adev->mode_info.funcs == NULL)
3384 adev->mode_info.funcs = &dm_display_funcs;
3387 * Note: Do NOT change adev->audio_endpt_rreg and
3388 * adev->audio_endpt_wreg because they are initialised in
3389 * amdgpu_device_init()
3391 #if defined(CONFIG_DEBUG_KERNEL_DC)
3394 &dev_attr_s3_debug);
3400 static bool modeset_required(struct drm_crtc_state *crtc_state,
3401 struct dc_stream_state *new_stream,
3402 struct dc_stream_state *old_stream)
3404 if (!drm_atomic_crtc_needs_modeset(crtc_state))
3407 if (!crtc_state->enable)
3410 return crtc_state->active;
3413 static bool modereset_required(struct drm_crtc_state *crtc_state)
3415 if (!drm_atomic_crtc_needs_modeset(crtc_state))
3418 return !crtc_state->enable || !crtc_state->active;
3421 static void amdgpu_dm_encoder_destroy(struct drm_encoder *encoder)
3423 drm_encoder_cleanup(encoder);
3427 static const struct drm_encoder_funcs amdgpu_dm_encoder_funcs = {
3428 .destroy = amdgpu_dm_encoder_destroy,
3432 static int fill_dc_scaling_info(const struct drm_plane_state *state,
3433 struct dc_scaling_info *scaling_info)
3435 int scale_w, scale_h;
3437 memset(scaling_info, 0, sizeof(*scaling_info));
3439 /* Source is fixed 16.16 but we ignore mantissa for now... */
3440 scaling_info->src_rect.x = state->src_x >> 16;
3441 scaling_info->src_rect.y = state->src_y >> 16;
3443 scaling_info->src_rect.width = state->src_w >> 16;
3444 if (scaling_info->src_rect.width == 0)
3447 scaling_info->src_rect.height = state->src_h >> 16;
3448 if (scaling_info->src_rect.height == 0)
3451 scaling_info->dst_rect.x = state->crtc_x;
3452 scaling_info->dst_rect.y = state->crtc_y;
3454 if (state->crtc_w == 0)
3457 scaling_info->dst_rect.width = state->crtc_w;
3459 if (state->crtc_h == 0)
3462 scaling_info->dst_rect.height = state->crtc_h;
3464 /* DRM doesn't specify clipping on destination output. */
3465 scaling_info->clip_rect = scaling_info->dst_rect;
3467 /* TODO: Validate scaling per-format with DC plane caps */
3468 scale_w = scaling_info->dst_rect.width * 1000 /
3469 scaling_info->src_rect.width;
3471 if (scale_w < 250 || scale_w > 16000)
3474 scale_h = scaling_info->dst_rect.height * 1000 /
3475 scaling_info->src_rect.height;
3477 if (scale_h < 250 || scale_h > 16000)
3481 * The "scaling_quality" can be ignored for now, quality = 0 has DC
3482 * assume reasonable defaults based on the format.
3488 static int get_fb_info(const struct amdgpu_framebuffer *amdgpu_fb,
3489 uint64_t *tiling_flags, bool *tmz_surface)
3491 struct amdgpu_bo *rbo = gem_to_amdgpu_bo(amdgpu_fb->base.obj[0]);
3492 int r = amdgpu_bo_reserve(rbo, false);
3495 /* Don't show error message when returning -ERESTARTSYS */
3496 if (r != -ERESTARTSYS)
3497 DRM_ERROR("Unable to reserve buffer: %d\n", r);
3502 amdgpu_bo_get_tiling_flags(rbo, tiling_flags);
3505 *tmz_surface = amdgpu_bo_encrypted(rbo);
3507 amdgpu_bo_unreserve(rbo);
3512 static inline uint64_t get_dcc_address(uint64_t address, uint64_t tiling_flags)
3514 uint32_t offset = AMDGPU_TILING_GET(tiling_flags, DCC_OFFSET_256B);
3516 return offset ? (address + offset * 256) : 0;
3520 fill_plane_dcc_attributes(struct amdgpu_device *adev,
3521 const struct amdgpu_framebuffer *afb,
3522 const enum surface_pixel_format format,
3523 const enum dc_rotation_angle rotation,
3524 const struct plane_size *plane_size,
3525 const union dc_tiling_info *tiling_info,
3526 const uint64_t info,
3527 struct dc_plane_dcc_param *dcc,
3528 struct dc_plane_address *address,
3529 bool force_disable_dcc)
3531 struct dc *dc = adev->dm.dc;
3532 struct dc_dcc_surface_param input;
3533 struct dc_surface_dcc_cap output;
3534 uint32_t offset = AMDGPU_TILING_GET(info, DCC_OFFSET_256B);
3535 uint32_t i64b = AMDGPU_TILING_GET(info, DCC_INDEPENDENT_64B) != 0;
3536 uint64_t dcc_address;
3538 memset(&input, 0, sizeof(input));
3539 memset(&output, 0, sizeof(output));
3541 if (force_disable_dcc)
3547 if (format >= SURFACE_PIXEL_FORMAT_VIDEO_BEGIN)
3550 if (!dc->cap_funcs.get_dcc_compression_cap)
3553 input.format = format;
3554 input.surface_size.width = plane_size->surface_size.width;
3555 input.surface_size.height = plane_size->surface_size.height;
3556 input.swizzle_mode = tiling_info->gfx9.swizzle;
3558 if (rotation == ROTATION_ANGLE_0 || rotation == ROTATION_ANGLE_180)
3559 input.scan = SCAN_DIRECTION_HORIZONTAL;
3560 else if (rotation == ROTATION_ANGLE_90 || rotation == ROTATION_ANGLE_270)
3561 input.scan = SCAN_DIRECTION_VERTICAL;
3563 if (!dc->cap_funcs.get_dcc_compression_cap(dc, &input, &output))
3566 if (!output.capable)
3569 if (i64b == 0 && output.grph.rgb.independent_64b_blks != 0)
3574 AMDGPU_TILING_GET(info, DCC_PITCH_MAX) + 1;
3575 dcc->independent_64b_blks = i64b;
3577 dcc_address = get_dcc_address(afb->address, info);
3578 address->grph.meta_addr.low_part = lower_32_bits(dcc_address);
3579 address->grph.meta_addr.high_part = upper_32_bits(dcc_address);
3585 fill_plane_buffer_attributes(struct amdgpu_device *adev,
3586 const struct amdgpu_framebuffer *afb,
3587 const enum surface_pixel_format format,
3588 const enum dc_rotation_angle rotation,
3589 const uint64_t tiling_flags,
3590 union dc_tiling_info *tiling_info,
3591 struct plane_size *plane_size,
3592 struct dc_plane_dcc_param *dcc,
3593 struct dc_plane_address *address,
3595 bool force_disable_dcc)
3597 const struct drm_framebuffer *fb = &afb->base;
3600 memset(tiling_info, 0, sizeof(*tiling_info));
3601 memset(plane_size, 0, sizeof(*plane_size));
3602 memset(dcc, 0, sizeof(*dcc));
3603 memset(address, 0, sizeof(*address));
3605 address->tmz_surface = tmz_surface;
3607 if (format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) {
3608 plane_size->surface_size.x = 0;
3609 plane_size->surface_size.y = 0;
3610 plane_size->surface_size.width = fb->width;
3611 plane_size->surface_size.height = fb->height;
3612 plane_size->surface_pitch =
3613 fb->pitches[0] / fb->format->cpp[0];
3615 address->type = PLN_ADDR_TYPE_GRAPHICS;
3616 address->grph.addr.low_part = lower_32_bits(afb->address);
3617 address->grph.addr.high_part = upper_32_bits(afb->address);
3618 } else if (format < SURFACE_PIXEL_FORMAT_INVALID) {
3619 uint64_t chroma_addr = afb->address + fb->offsets[1];
3621 plane_size->surface_size.x = 0;
3622 plane_size->surface_size.y = 0;
3623 plane_size->surface_size.width = fb->width;
3624 plane_size->surface_size.height = fb->height;
3625 plane_size->surface_pitch =
3626 fb->pitches[0] / fb->format->cpp[0];
3628 plane_size->chroma_size.x = 0;
3629 plane_size->chroma_size.y = 0;
3630 /* TODO: set these based on surface format */
3631 plane_size->chroma_size.width = fb->width / 2;
3632 plane_size->chroma_size.height = fb->height / 2;
3634 plane_size->chroma_pitch =
3635 fb->pitches[1] / fb->format->cpp[1];
3637 address->type = PLN_ADDR_TYPE_VIDEO_PROGRESSIVE;
3638 address->video_progressive.luma_addr.low_part =
3639 lower_32_bits(afb->address);
3640 address->video_progressive.luma_addr.high_part =
3641 upper_32_bits(afb->address);
3642 address->video_progressive.chroma_addr.low_part =
3643 lower_32_bits(chroma_addr);
3644 address->video_progressive.chroma_addr.high_part =
3645 upper_32_bits(chroma_addr);
3648 /* Fill GFX8 params */
3649 if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == DC_ARRAY_2D_TILED_THIN1) {
3650 unsigned int bankw, bankh, mtaspect, tile_split, num_banks;
3652 bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
3653 bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
3654 mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
3655 tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT);
3656 num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
3658 /* XXX fix me for VI */
3659 tiling_info->gfx8.num_banks = num_banks;
3660 tiling_info->gfx8.array_mode =
3661 DC_ARRAY_2D_TILED_THIN1;
3662 tiling_info->gfx8.tile_split = tile_split;
3663 tiling_info->gfx8.bank_width = bankw;
3664 tiling_info->gfx8.bank_height = bankh;
3665 tiling_info->gfx8.tile_aspect = mtaspect;
3666 tiling_info->gfx8.tile_mode =
3667 DC_ADDR_SURF_MICRO_TILING_DISPLAY;
3668 } else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE)
3669 == DC_ARRAY_1D_TILED_THIN1) {
3670 tiling_info->gfx8.array_mode = DC_ARRAY_1D_TILED_THIN1;
3673 tiling_info->gfx8.pipe_config =
3674 AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
3676 if (adev->asic_type == CHIP_VEGA10 ||
3677 adev->asic_type == CHIP_VEGA12 ||
3678 adev->asic_type == CHIP_VEGA20 ||
3679 adev->asic_type == CHIP_NAVI10 ||
3680 adev->asic_type == CHIP_NAVI14 ||
3681 adev->asic_type == CHIP_NAVI12 ||
3682 adev->asic_type == CHIP_RENOIR ||
3683 adev->asic_type == CHIP_RAVEN) {
3684 /* Fill GFX9 params */
3685 tiling_info->gfx9.num_pipes =
3686 adev->gfx.config.gb_addr_config_fields.num_pipes;
3687 tiling_info->gfx9.num_banks =
3688 adev->gfx.config.gb_addr_config_fields.num_banks;
3689 tiling_info->gfx9.pipe_interleave =
3690 adev->gfx.config.gb_addr_config_fields.pipe_interleave_size;
3691 tiling_info->gfx9.num_shader_engines =
3692 adev->gfx.config.gb_addr_config_fields.num_se;
3693 tiling_info->gfx9.max_compressed_frags =
3694 adev->gfx.config.gb_addr_config_fields.max_compress_frags;
3695 tiling_info->gfx9.num_rb_per_se =
3696 adev->gfx.config.gb_addr_config_fields.num_rb_per_se;
3697 tiling_info->gfx9.swizzle =
3698 AMDGPU_TILING_GET(tiling_flags, SWIZZLE_MODE);
3699 tiling_info->gfx9.shaderEnable = 1;
3701 ret = fill_plane_dcc_attributes(adev, afb, format, rotation,
3702 plane_size, tiling_info,
3703 tiling_flags, dcc, address,
3713 fill_blending_from_plane_state(const struct drm_plane_state *plane_state,
3714 bool *per_pixel_alpha, bool *global_alpha,
3715 int *global_alpha_value)
3717 *per_pixel_alpha = false;
3718 *global_alpha = false;
3719 *global_alpha_value = 0xff;
3721 if (plane_state->plane->type != DRM_PLANE_TYPE_OVERLAY)
3724 if (plane_state->pixel_blend_mode == DRM_MODE_BLEND_PREMULTI) {
3725 static const uint32_t alpha_formats[] = {
3726 DRM_FORMAT_ARGB8888,
3727 DRM_FORMAT_RGBA8888,
3728 DRM_FORMAT_ABGR8888,
3730 uint32_t format = plane_state->fb->format->format;
3733 for (i = 0; i < ARRAY_SIZE(alpha_formats); ++i) {
3734 if (format == alpha_formats[i]) {
3735 *per_pixel_alpha = true;
3741 if (plane_state->alpha < 0xffff) {
3742 *global_alpha = true;
3743 *global_alpha_value = plane_state->alpha >> 8;
3748 fill_plane_color_attributes(const struct drm_plane_state *plane_state,
3749 const enum surface_pixel_format format,
3750 enum dc_color_space *color_space)
3754 *color_space = COLOR_SPACE_SRGB;
3756 /* DRM color properties only affect non-RGB formats. */
3757 if (format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN)
3760 full_range = (plane_state->color_range == DRM_COLOR_YCBCR_FULL_RANGE);
3762 switch (plane_state->color_encoding) {
3763 case DRM_COLOR_YCBCR_BT601:
3765 *color_space = COLOR_SPACE_YCBCR601;
3767 *color_space = COLOR_SPACE_YCBCR601_LIMITED;
3770 case DRM_COLOR_YCBCR_BT709:
3772 *color_space = COLOR_SPACE_YCBCR709;
3774 *color_space = COLOR_SPACE_YCBCR709_LIMITED;
3777 case DRM_COLOR_YCBCR_BT2020:
3779 *color_space = COLOR_SPACE_2020_YCBCR;
3792 fill_dc_plane_info_and_addr(struct amdgpu_device *adev,
3793 const struct drm_plane_state *plane_state,
3794 const uint64_t tiling_flags,
3795 struct dc_plane_info *plane_info,
3796 struct dc_plane_address *address,
3798 bool force_disable_dcc)
3800 const struct drm_framebuffer *fb = plane_state->fb;
3801 const struct amdgpu_framebuffer *afb =
3802 to_amdgpu_framebuffer(plane_state->fb);
3803 struct drm_format_name_buf format_name;
3806 memset(plane_info, 0, sizeof(*plane_info));
3808 switch (fb->format->format) {
3810 plane_info->format =
3811 SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS;
3813 case DRM_FORMAT_RGB565:
3814 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_RGB565;
3816 case DRM_FORMAT_XRGB8888:
3817 case DRM_FORMAT_ARGB8888:
3818 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888;
3820 case DRM_FORMAT_XRGB2101010:
3821 case DRM_FORMAT_ARGB2101010:
3822 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010;
3824 case DRM_FORMAT_XBGR2101010:
3825 case DRM_FORMAT_ABGR2101010:
3826 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010;
3828 case DRM_FORMAT_XBGR8888:
3829 case DRM_FORMAT_ABGR8888:
3830 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR8888;
3832 case DRM_FORMAT_NV21:
3833 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr;
3835 case DRM_FORMAT_NV12:
3836 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb;
3838 case DRM_FORMAT_P010:
3839 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb;
3841 case DRM_FORMAT_XRGB16161616F:
3842 case DRM_FORMAT_ARGB16161616F:
3843 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F;
3845 case DRM_FORMAT_XBGR16161616F:
3846 case DRM_FORMAT_ABGR16161616F:
3847 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F;
3851 "Unsupported screen format %s\n",
3852 drm_get_format_name(fb->format->format, &format_name));
3856 switch (plane_state->rotation & DRM_MODE_ROTATE_MASK) {
3857 case DRM_MODE_ROTATE_0:
3858 plane_info->rotation = ROTATION_ANGLE_0;
3860 case DRM_MODE_ROTATE_90:
3861 plane_info->rotation = ROTATION_ANGLE_90;
3863 case DRM_MODE_ROTATE_180:
3864 plane_info->rotation = ROTATION_ANGLE_180;
3866 case DRM_MODE_ROTATE_270:
3867 plane_info->rotation = ROTATION_ANGLE_270;
3870 plane_info->rotation = ROTATION_ANGLE_0;
3874 plane_info->visible = true;
3875 plane_info->stereo_format = PLANE_STEREO_FORMAT_NONE;
3877 plane_info->layer_index = 0;
3879 ret = fill_plane_color_attributes(plane_state, plane_info->format,
3880 &plane_info->color_space);
3884 ret = fill_plane_buffer_attributes(adev, afb, plane_info->format,
3885 plane_info->rotation, tiling_flags,
3886 &plane_info->tiling_info,
3887 &plane_info->plane_size,
3888 &plane_info->dcc, address, tmz_surface,
3893 fill_blending_from_plane_state(
3894 plane_state, &plane_info->per_pixel_alpha,
3895 &plane_info->global_alpha, &plane_info->global_alpha_value);
3900 static int fill_dc_plane_attributes(struct amdgpu_device *adev,
3901 struct dc_plane_state *dc_plane_state,
3902 struct drm_plane_state *plane_state,
3903 struct drm_crtc_state *crtc_state)
3905 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state);
3906 const struct amdgpu_framebuffer *amdgpu_fb =
3907 to_amdgpu_framebuffer(plane_state->fb);
3908 struct dc_scaling_info scaling_info;
3909 struct dc_plane_info plane_info;
3910 uint64_t tiling_flags;
3912 bool tmz_surface = false;
3913 bool force_disable_dcc = false;
3915 ret = fill_dc_scaling_info(plane_state, &scaling_info);
3919 dc_plane_state->src_rect = scaling_info.src_rect;
3920 dc_plane_state->dst_rect = scaling_info.dst_rect;
3921 dc_plane_state->clip_rect = scaling_info.clip_rect;
3922 dc_plane_state->scaling_quality = scaling_info.scaling_quality;
3924 ret = get_fb_info(amdgpu_fb, &tiling_flags, &tmz_surface);
3928 force_disable_dcc = adev->asic_type == CHIP_RAVEN && adev->in_suspend;
3929 ret = fill_dc_plane_info_and_addr(adev, plane_state, tiling_flags,
3931 &dc_plane_state->address,
3937 dc_plane_state->format = plane_info.format;
3938 dc_plane_state->color_space = plane_info.color_space;
3939 dc_plane_state->format = plane_info.format;
3940 dc_plane_state->plane_size = plane_info.plane_size;
3941 dc_plane_state->rotation = plane_info.rotation;
3942 dc_plane_state->horizontal_mirror = plane_info.horizontal_mirror;
3943 dc_plane_state->stereo_format = plane_info.stereo_format;
3944 dc_plane_state->tiling_info = plane_info.tiling_info;
3945 dc_plane_state->visible = plane_info.visible;
3946 dc_plane_state->per_pixel_alpha = plane_info.per_pixel_alpha;
3947 dc_plane_state->global_alpha = plane_info.global_alpha;
3948 dc_plane_state->global_alpha_value = plane_info.global_alpha_value;
3949 dc_plane_state->dcc = plane_info.dcc;
3950 dc_plane_state->layer_index = plane_info.layer_index; // Always returns 0
3953 * Always set input transfer function, since plane state is refreshed
3956 ret = amdgpu_dm_update_plane_color_mgmt(dm_crtc_state, dc_plane_state);
3963 static void update_stream_scaling_settings(const struct drm_display_mode *mode,
3964 const struct dm_connector_state *dm_state,
3965 struct dc_stream_state *stream)
3967 enum amdgpu_rmx_type rmx_type;
3969 struct rect src = { 0 }; /* viewport in composition space*/
3970 struct rect dst = { 0 }; /* stream addressable area */
3972 /* no mode. nothing to be done */
3976 /* Full screen scaling by default */
3977 src.width = mode->hdisplay;
3978 src.height = mode->vdisplay;
3979 dst.width = stream->timing.h_addressable;
3980 dst.height = stream->timing.v_addressable;
3983 rmx_type = dm_state->scaling;
3984 if (rmx_type == RMX_ASPECT || rmx_type == RMX_OFF) {
3985 if (src.width * dst.height <
3986 src.height * dst.width) {
3987 /* height needs less upscaling/more downscaling */
3988 dst.width = src.width *
3989 dst.height / src.height;
3991 /* width needs less upscaling/more downscaling */
3992 dst.height = src.height *
3993 dst.width / src.width;
3995 } else if (rmx_type == RMX_CENTER) {
3999 dst.x = (stream->timing.h_addressable - dst.width) / 2;
4000 dst.y = (stream->timing.v_addressable - dst.height) / 2;
4002 if (dm_state->underscan_enable) {
4003 dst.x += dm_state->underscan_hborder / 2;
4004 dst.y += dm_state->underscan_vborder / 2;
4005 dst.width -= dm_state->underscan_hborder;
4006 dst.height -= dm_state->underscan_vborder;
4013 DRM_DEBUG_DRIVER("Destination Rectangle x:%d y:%d width:%d height:%d\n",
4014 dst.x, dst.y, dst.width, dst.height);
4018 static enum dc_color_depth
4019 convert_color_depth_from_display_info(const struct drm_connector *connector,
4020 bool is_y420, int requested_bpc)
4027 /* Cap display bpc based on HDMI 2.0 HF-VSDB */
4028 if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_48)
4030 else if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_36)
4032 else if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_30)
4035 bpc = (uint8_t)connector->display_info.bpc;
4036 /* Assume 8 bpc by default if no bpc is specified. */
4037 bpc = bpc ? bpc : 8;
4040 if (requested_bpc > 0) {
4042 * Cap display bpc based on the user requested value.
4044 * The value for state->max_bpc may not correctly updated
4045 * depending on when the connector gets added to the state
4046 * or if this was called outside of atomic check, so it
4047 * can't be used directly.
4049 bpc = min_t(u8, bpc, requested_bpc);
4051 /* Round down to the nearest even number. */
4052 bpc = bpc - (bpc & 1);
4058 * Temporary Work around, DRM doesn't parse color depth for
4059 * EDID revision before 1.4
4060 * TODO: Fix edid parsing
4062 return COLOR_DEPTH_888;
4064 return COLOR_DEPTH_666;
4066 return COLOR_DEPTH_888;
4068 return COLOR_DEPTH_101010;
4070 return COLOR_DEPTH_121212;
4072 return COLOR_DEPTH_141414;
4074 return COLOR_DEPTH_161616;
4076 return COLOR_DEPTH_UNDEFINED;
4080 static enum dc_aspect_ratio
4081 get_aspect_ratio(const struct drm_display_mode *mode_in)
4083 /* 1-1 mapping, since both enums follow the HDMI spec. */
4084 return (enum dc_aspect_ratio) mode_in->picture_aspect_ratio;
4087 static enum dc_color_space
4088 get_output_color_space(const struct dc_crtc_timing *dc_crtc_timing)
4090 enum dc_color_space color_space = COLOR_SPACE_SRGB;
4092 switch (dc_crtc_timing->pixel_encoding) {
4093 case PIXEL_ENCODING_YCBCR422:
4094 case PIXEL_ENCODING_YCBCR444:
4095 case PIXEL_ENCODING_YCBCR420:
4098 * 27030khz is the separation point between HDTV and SDTV
4099 * according to HDMI spec, we use YCbCr709 and YCbCr601
4102 if (dc_crtc_timing->pix_clk_100hz > 270300) {
4103 if (dc_crtc_timing->flags.Y_ONLY)
4105 COLOR_SPACE_YCBCR709_LIMITED;
4107 color_space = COLOR_SPACE_YCBCR709;
4109 if (dc_crtc_timing->flags.Y_ONLY)
4111 COLOR_SPACE_YCBCR601_LIMITED;
4113 color_space = COLOR_SPACE_YCBCR601;
4118 case PIXEL_ENCODING_RGB:
4119 color_space = COLOR_SPACE_SRGB;
4130 static bool adjust_colour_depth_from_display_info(
4131 struct dc_crtc_timing *timing_out,
4132 const struct drm_display_info *info)
4134 enum dc_color_depth depth = timing_out->display_color_depth;
4137 normalized_clk = timing_out->pix_clk_100hz / 10;
4138 /* YCbCr 4:2:0 requires additional adjustment of 1/2 */
4139 if (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420)
4140 normalized_clk /= 2;
4141 /* Adjusting pix clock following on HDMI spec based on colour depth */
4143 case COLOR_DEPTH_888:
4145 case COLOR_DEPTH_101010:
4146 normalized_clk = (normalized_clk * 30) / 24;
4148 case COLOR_DEPTH_121212:
4149 normalized_clk = (normalized_clk * 36) / 24;
4151 case COLOR_DEPTH_161616:
4152 normalized_clk = (normalized_clk * 48) / 24;
4155 /* The above depths are the only ones valid for HDMI. */
4158 if (normalized_clk <= info->max_tmds_clock) {
4159 timing_out->display_color_depth = depth;
4162 } while (--depth > COLOR_DEPTH_666);
4166 static void fill_stream_properties_from_drm_display_mode(
4167 struct dc_stream_state *stream,
4168 const struct drm_display_mode *mode_in,
4169 const struct drm_connector *connector,
4170 const struct drm_connector_state *connector_state,
4171 const struct dc_stream_state *old_stream,
4174 struct dc_crtc_timing *timing_out = &stream->timing;
4175 const struct drm_display_info *info = &connector->display_info;
4176 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
4177 struct hdmi_vendor_infoframe hv_frame;
4178 struct hdmi_avi_infoframe avi_frame;
4180 memset(&hv_frame, 0, sizeof(hv_frame));
4181 memset(&avi_frame, 0, sizeof(avi_frame));
4183 timing_out->h_border_left = 0;
4184 timing_out->h_border_right = 0;
4185 timing_out->v_border_top = 0;
4186 timing_out->v_border_bottom = 0;
4187 /* TODO: un-hardcode */
4188 if (drm_mode_is_420_only(info, mode_in)
4189 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
4190 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
4191 else if (drm_mode_is_420_also(info, mode_in)
4192 && aconnector->force_yuv420_output)
4193 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
4194 else if ((connector->display_info.color_formats & DRM_COLOR_FORMAT_YCRCB444)
4195 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
4196 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR444;
4198 timing_out->pixel_encoding = PIXEL_ENCODING_RGB;
4200 timing_out->timing_3d_format = TIMING_3D_FORMAT_NONE;
4201 timing_out->display_color_depth = convert_color_depth_from_display_info(
4203 (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420),
4205 timing_out->scan_type = SCANNING_TYPE_NODATA;
4206 timing_out->hdmi_vic = 0;
4209 timing_out->vic = old_stream->timing.vic;
4210 timing_out->flags.HSYNC_POSITIVE_POLARITY = old_stream->timing.flags.HSYNC_POSITIVE_POLARITY;
4211 timing_out->flags.VSYNC_POSITIVE_POLARITY = old_stream->timing.flags.VSYNC_POSITIVE_POLARITY;
4213 timing_out->vic = drm_match_cea_mode(mode_in);
4214 if (mode_in->flags & DRM_MODE_FLAG_PHSYNC)
4215 timing_out->flags.HSYNC_POSITIVE_POLARITY = 1;
4216 if (mode_in->flags & DRM_MODE_FLAG_PVSYNC)
4217 timing_out->flags.VSYNC_POSITIVE_POLARITY = 1;
4220 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) {
4221 drm_hdmi_avi_infoframe_from_display_mode(&avi_frame, (struct drm_connector *)connector, mode_in);
4222 timing_out->vic = avi_frame.video_code;
4223 drm_hdmi_vendor_infoframe_from_display_mode(&hv_frame, (struct drm_connector *)connector, mode_in);
4224 timing_out->hdmi_vic = hv_frame.vic;
4227 timing_out->h_addressable = mode_in->crtc_hdisplay;
4228 timing_out->h_total = mode_in->crtc_htotal;
4229 timing_out->h_sync_width =
4230 mode_in->crtc_hsync_end - mode_in->crtc_hsync_start;
4231 timing_out->h_front_porch =
4232 mode_in->crtc_hsync_start - mode_in->crtc_hdisplay;
4233 timing_out->v_total = mode_in->crtc_vtotal;
4234 timing_out->v_addressable = mode_in->crtc_vdisplay;
4235 timing_out->v_front_porch =
4236 mode_in->crtc_vsync_start - mode_in->crtc_vdisplay;
4237 timing_out->v_sync_width =
4238 mode_in->crtc_vsync_end - mode_in->crtc_vsync_start;
4239 timing_out->pix_clk_100hz = mode_in->crtc_clock * 10;
4240 timing_out->aspect_ratio = get_aspect_ratio(mode_in);
4242 stream->output_color_space = get_output_color_space(timing_out);
4244 stream->out_transfer_func->type = TF_TYPE_PREDEFINED;
4245 stream->out_transfer_func->tf = TRANSFER_FUNCTION_SRGB;
4246 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) {
4247 if (!adjust_colour_depth_from_display_info(timing_out, info) &&
4248 drm_mode_is_420_also(info, mode_in) &&
4249 timing_out->pixel_encoding != PIXEL_ENCODING_YCBCR420) {
4250 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
4251 adjust_colour_depth_from_display_info(timing_out, info);
4256 static void fill_audio_info(struct audio_info *audio_info,
4257 const struct drm_connector *drm_connector,
4258 const struct dc_sink *dc_sink)
4261 int cea_revision = 0;
4262 const struct dc_edid_caps *edid_caps = &dc_sink->edid_caps;
4264 audio_info->manufacture_id = edid_caps->manufacturer_id;
4265 audio_info->product_id = edid_caps->product_id;
4267 cea_revision = drm_connector->display_info.cea_rev;
4269 strscpy(audio_info->display_name,
4270 edid_caps->display_name,
4271 AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS);
4273 if (cea_revision >= 3) {
4274 audio_info->mode_count = edid_caps->audio_mode_count;
4276 for (i = 0; i < audio_info->mode_count; ++i) {
4277 audio_info->modes[i].format_code =
4278 (enum audio_format_code)
4279 (edid_caps->audio_modes[i].format_code);
4280 audio_info->modes[i].channel_count =
4281 edid_caps->audio_modes[i].channel_count;
4282 audio_info->modes[i].sample_rates.all =
4283 edid_caps->audio_modes[i].sample_rate;
4284 audio_info->modes[i].sample_size =
4285 edid_caps->audio_modes[i].sample_size;
4289 audio_info->flags.all = edid_caps->speaker_flags;
4291 /* TODO: We only check for the progressive mode, check for interlace mode too */
4292 if (drm_connector->latency_present[0]) {
4293 audio_info->video_latency = drm_connector->video_latency[0];
4294 audio_info->audio_latency = drm_connector->audio_latency[0];
4297 /* TODO: For DP, video and audio latency should be calculated from DPCD caps */
4302 copy_crtc_timing_for_drm_display_mode(const struct drm_display_mode *src_mode,
4303 struct drm_display_mode *dst_mode)
4305 dst_mode->crtc_hdisplay = src_mode->crtc_hdisplay;
4306 dst_mode->crtc_vdisplay = src_mode->crtc_vdisplay;
4307 dst_mode->crtc_clock = src_mode->crtc_clock;
4308 dst_mode->crtc_hblank_start = src_mode->crtc_hblank_start;
4309 dst_mode->crtc_hblank_end = src_mode->crtc_hblank_end;
4310 dst_mode->crtc_hsync_start = src_mode->crtc_hsync_start;
4311 dst_mode->crtc_hsync_end = src_mode->crtc_hsync_end;
4312 dst_mode->crtc_htotal = src_mode->crtc_htotal;
4313 dst_mode->crtc_hskew = src_mode->crtc_hskew;
4314 dst_mode->crtc_vblank_start = src_mode->crtc_vblank_start;
4315 dst_mode->crtc_vblank_end = src_mode->crtc_vblank_end;
4316 dst_mode->crtc_vsync_start = src_mode->crtc_vsync_start;
4317 dst_mode->crtc_vsync_end = src_mode->crtc_vsync_end;
4318 dst_mode->crtc_vtotal = src_mode->crtc_vtotal;
4322 decide_crtc_timing_for_drm_display_mode(struct drm_display_mode *drm_mode,
4323 const struct drm_display_mode *native_mode,
4326 if (scale_enabled) {
4327 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
4328 } else if (native_mode->clock == drm_mode->clock &&
4329 native_mode->htotal == drm_mode->htotal &&
4330 native_mode->vtotal == drm_mode->vtotal) {
4331 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
4333 /* no scaling nor amdgpu inserted, no need to patch */
4337 static struct dc_sink *
4338 create_fake_sink(struct amdgpu_dm_connector *aconnector)
4340 struct dc_sink_init_data sink_init_data = { 0 };
4341 struct dc_sink *sink = NULL;
4342 sink_init_data.link = aconnector->dc_link;
4343 sink_init_data.sink_signal = aconnector->dc_link->connector_signal;
4345 sink = dc_sink_create(&sink_init_data);
4347 DRM_ERROR("Failed to create sink!\n");
4350 sink->sink_signal = SIGNAL_TYPE_VIRTUAL;
4355 static void set_multisync_trigger_params(
4356 struct dc_stream_state *stream)
4358 if (stream->triggered_crtc_reset.enabled) {
4359 stream->triggered_crtc_reset.event = CRTC_EVENT_VSYNC_RISING;
4360 stream->triggered_crtc_reset.delay = TRIGGER_DELAY_NEXT_LINE;
4364 static void set_master_stream(struct dc_stream_state *stream_set[],
4367 int j, highest_rfr = 0, master_stream = 0;
4369 for (j = 0; j < stream_count; j++) {
4370 if (stream_set[j] && stream_set[j]->triggered_crtc_reset.enabled) {
4371 int refresh_rate = 0;
4373 refresh_rate = (stream_set[j]->timing.pix_clk_100hz*100)/
4374 (stream_set[j]->timing.h_total*stream_set[j]->timing.v_total);
4375 if (refresh_rate > highest_rfr) {
4376 highest_rfr = refresh_rate;
4381 for (j = 0; j < stream_count; j++) {
4383 stream_set[j]->triggered_crtc_reset.event_source = stream_set[master_stream];
4387 static void dm_enable_per_frame_crtc_master_sync(struct dc_state *context)
4391 if (context->stream_count < 2)
4393 for (i = 0; i < context->stream_count ; i++) {
4394 if (!context->streams[i])
4397 * TODO: add a function to read AMD VSDB bits and set
4398 * crtc_sync_master.multi_sync_enabled flag
4399 * For now it's set to false
4401 set_multisync_trigger_params(context->streams[i]);
4403 set_master_stream(context->streams, context->stream_count);
4406 static struct dc_stream_state *
4407 create_stream_for_sink(struct amdgpu_dm_connector *aconnector,
4408 const struct drm_display_mode *drm_mode,
4409 const struct dm_connector_state *dm_state,
4410 const struct dc_stream_state *old_stream,
4413 struct drm_display_mode *preferred_mode = NULL;
4414 struct drm_connector *drm_connector;
4415 const struct drm_connector_state *con_state =
4416 dm_state ? &dm_state->base : NULL;
4417 struct dc_stream_state *stream = NULL;
4418 struct drm_display_mode mode = *drm_mode;
4419 bool native_mode_found = false;
4420 bool scale = dm_state ? (dm_state->scaling != RMX_OFF) : false;
4422 int preferred_refresh = 0;
4423 #if defined(CONFIG_DRM_AMD_DC_DCN)
4424 struct dsc_dec_dpcd_caps dsc_caps;
4426 uint32_t link_bandwidth_kbps;
4428 struct dc_sink *sink = NULL;
4429 if (aconnector == NULL) {
4430 DRM_ERROR("aconnector is NULL!\n");
4434 drm_connector = &aconnector->base;
4436 if (!aconnector->dc_sink) {
4437 sink = create_fake_sink(aconnector);
4441 sink = aconnector->dc_sink;
4442 dc_sink_retain(sink);
4445 stream = dc_create_stream_for_sink(sink);
4447 if (stream == NULL) {
4448 DRM_ERROR("Failed to create stream for sink!\n");
4452 stream->dm_stream_context = aconnector;
4454 stream->timing.flags.LTE_340MCSC_SCRAMBLE =
4455 drm_connector->display_info.hdmi.scdc.scrambling.low_rates;
4457 list_for_each_entry(preferred_mode, &aconnector->base.modes, head) {
4458 /* Search for preferred mode */
4459 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED) {
4460 native_mode_found = true;
4464 if (!native_mode_found)
4465 preferred_mode = list_first_entry_or_null(
4466 &aconnector->base.modes,
4467 struct drm_display_mode,
4470 mode_refresh = drm_mode_vrefresh(&mode);
4472 if (preferred_mode == NULL) {
4474 * This may not be an error, the use case is when we have no
4475 * usermode calls to reset and set mode upon hotplug. In this
4476 * case, we call set mode ourselves to restore the previous mode
4477 * and the modelist may not be filled in in time.
4479 DRM_DEBUG_DRIVER("No preferred mode found\n");
4481 decide_crtc_timing_for_drm_display_mode(
4482 &mode, preferred_mode,
4483 dm_state ? (dm_state->scaling != RMX_OFF) : false);
4484 preferred_refresh = drm_mode_vrefresh(preferred_mode);
4488 drm_mode_set_crtcinfo(&mode, 0);
4491 * If scaling is enabled and refresh rate didn't change
4492 * we copy the vic and polarities of the old timings
4494 if (!scale || mode_refresh != preferred_refresh)
4495 fill_stream_properties_from_drm_display_mode(stream,
4496 &mode, &aconnector->base, con_state, NULL, requested_bpc);
4498 fill_stream_properties_from_drm_display_mode(stream,
4499 &mode, &aconnector->base, con_state, old_stream, requested_bpc);
4501 stream->timing.flags.DSC = 0;
4503 if (aconnector->dc_link && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT) {
4504 #if defined(CONFIG_DRM_AMD_DC_DCN)
4505 dc_dsc_parse_dsc_dpcd(aconnector->dc_link->ctx->dc,
4506 aconnector->dc_link->dpcd_caps.dsc_caps.dsc_basic_caps.raw,
4507 aconnector->dc_link->dpcd_caps.dsc_caps.dsc_ext_caps.raw,
4510 link_bandwidth_kbps = dc_link_bandwidth_kbps(aconnector->dc_link,
4511 dc_link_get_link_cap(aconnector->dc_link));
4513 #if defined(CONFIG_DRM_AMD_DC_DCN)
4514 if (dsc_caps.is_dsc_supported)
4515 if (dc_dsc_compute_config(aconnector->dc_link->ctx->dc->res_pool->dscs[0],
4517 aconnector->dc_link->ctx->dc->debug.dsc_min_slice_height_override,
4518 link_bandwidth_kbps,
4520 &stream->timing.dsc_cfg))
4521 stream->timing.flags.DSC = 1;
4525 update_stream_scaling_settings(&mode, dm_state, stream);
4528 &stream->audio_info,
4532 update_stream_signal(stream, sink);
4534 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
4535 mod_build_hf_vsif_infopacket(stream, &stream->vsp_infopacket, false, false);
4536 if (stream->link->psr_settings.psr_feature_enabled) {
4537 struct dc *core_dc = stream->link->ctx->dc;
4539 if (dc_is_dmcu_initialized(core_dc)) {
4541 // should decide stream support vsc sdp colorimetry capability
4542 // before building vsc info packet
4544 stream->use_vsc_sdp_for_colorimetry = false;
4545 if (aconnector->dc_sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
4546 stream->use_vsc_sdp_for_colorimetry =
4547 aconnector->dc_sink->is_vsc_sdp_colorimetry_supported;
4549 if (stream->link->dpcd_caps.dpcd_rev.raw >= 0x14 &&
4550 stream->link->dpcd_caps.dprx_feature.bits.VSC_SDP_COLORIMETRY_SUPPORTED) {
4551 stream->use_vsc_sdp_for_colorimetry = true;
4554 mod_build_vsc_infopacket(stream, &stream->vsc_infopacket);
4558 dc_sink_release(sink);
4563 static void amdgpu_dm_crtc_destroy(struct drm_crtc *crtc)
4565 drm_crtc_cleanup(crtc);
4569 static void dm_crtc_destroy_state(struct drm_crtc *crtc,
4570 struct drm_crtc_state *state)
4572 struct dm_crtc_state *cur = to_dm_crtc_state(state);
4574 /* TODO Destroy dc_stream objects are stream object is flattened */
4576 dc_stream_release(cur->stream);
4579 __drm_atomic_helper_crtc_destroy_state(state);
4585 static void dm_crtc_reset_state(struct drm_crtc *crtc)
4587 struct dm_crtc_state *state;
4590 dm_crtc_destroy_state(crtc, crtc->state);
4592 state = kzalloc(sizeof(*state), GFP_KERNEL);
4593 if (WARN_ON(!state))
4596 crtc->state = &state->base;
4597 crtc->state->crtc = crtc;
4601 static struct drm_crtc_state *
4602 dm_crtc_duplicate_state(struct drm_crtc *crtc)
4604 struct dm_crtc_state *state, *cur;
4606 cur = to_dm_crtc_state(crtc->state);
4608 if (WARN_ON(!crtc->state))
4611 state = kzalloc(sizeof(*state), GFP_KERNEL);
4615 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
4618 state->stream = cur->stream;
4619 dc_stream_retain(state->stream);
4622 state->active_planes = cur->active_planes;
4623 state->interrupts_enabled = cur->interrupts_enabled;
4624 state->vrr_params = cur->vrr_params;
4625 state->vrr_infopacket = cur->vrr_infopacket;
4626 state->abm_level = cur->abm_level;
4627 state->vrr_supported = cur->vrr_supported;
4628 state->freesync_config = cur->freesync_config;
4629 state->crc_src = cur->crc_src;
4630 state->cm_has_degamma = cur->cm_has_degamma;
4631 state->cm_is_degamma_srgb = cur->cm_is_degamma_srgb;
4633 /* TODO Duplicate dc_stream after objects are stream object is flattened */
4635 return &state->base;
4638 static inline int dm_set_vupdate_irq(struct drm_crtc *crtc, bool enable)
4640 enum dc_irq_source irq_source;
4641 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4642 struct amdgpu_device *adev = crtc->dev->dev_private;
4645 irq_source = IRQ_TYPE_VUPDATE + acrtc->otg_inst;
4647 rc = dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY;
4649 DRM_DEBUG_DRIVER("crtc %d - vupdate irq %sabling: r=%d\n",
4650 acrtc->crtc_id, enable ? "en" : "dis", rc);
4654 static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
4656 enum dc_irq_source irq_source;
4657 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4658 struct amdgpu_device *adev = crtc->dev->dev_private;
4659 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
4663 /* vblank irq on -> Only need vupdate irq in vrr mode */
4664 if (amdgpu_dm_vrr_active(acrtc_state))
4665 rc = dm_set_vupdate_irq(crtc, true);
4667 /* vblank irq off -> vupdate irq off */
4668 rc = dm_set_vupdate_irq(crtc, false);
4674 irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
4675 return dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY;
4678 static int dm_enable_vblank(struct drm_crtc *crtc)
4680 return dm_set_vblank(crtc, true);
4683 static void dm_disable_vblank(struct drm_crtc *crtc)
4685 dm_set_vblank(crtc, false);
4688 /* Implemented only the options currently availible for the driver */
4689 static const struct drm_crtc_funcs amdgpu_dm_crtc_funcs = {
4690 .reset = dm_crtc_reset_state,
4691 .destroy = amdgpu_dm_crtc_destroy,
4692 .gamma_set = drm_atomic_helper_legacy_gamma_set,
4693 .set_config = drm_atomic_helper_set_config,
4694 .page_flip = drm_atomic_helper_page_flip,
4695 .atomic_duplicate_state = dm_crtc_duplicate_state,
4696 .atomic_destroy_state = dm_crtc_destroy_state,
4697 .set_crc_source = amdgpu_dm_crtc_set_crc_source,
4698 .verify_crc_source = amdgpu_dm_crtc_verify_crc_source,
4699 .get_crc_sources = amdgpu_dm_crtc_get_crc_sources,
4700 .get_vblank_counter = amdgpu_get_vblank_counter_kms,
4701 .enable_vblank = dm_enable_vblank,
4702 .disable_vblank = dm_disable_vblank,
4703 .get_vblank_timestamp = drm_crtc_vblank_helper_get_vblank_timestamp,
4706 static enum drm_connector_status
4707 amdgpu_dm_connector_detect(struct drm_connector *connector, bool force)
4710 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
4714 * 1. This interface is NOT called in context of HPD irq.
4715 * 2. This interface *is called* in context of user-mode ioctl. Which
4716 * makes it a bad place for *any* MST-related activity.
4719 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED &&
4720 !aconnector->fake_enable)
4721 connected = (aconnector->dc_sink != NULL);
4723 connected = (aconnector->base.force == DRM_FORCE_ON);
4725 return (connected ? connector_status_connected :
4726 connector_status_disconnected);
4729 int amdgpu_dm_connector_atomic_set_property(struct drm_connector *connector,
4730 struct drm_connector_state *connector_state,
4731 struct drm_property *property,
4734 struct drm_device *dev = connector->dev;
4735 struct amdgpu_device *adev = dev->dev_private;
4736 struct dm_connector_state *dm_old_state =
4737 to_dm_connector_state(connector->state);
4738 struct dm_connector_state *dm_new_state =
4739 to_dm_connector_state(connector_state);
4743 if (property == dev->mode_config.scaling_mode_property) {
4744 enum amdgpu_rmx_type rmx_type;
4747 case DRM_MODE_SCALE_CENTER:
4748 rmx_type = RMX_CENTER;
4750 case DRM_MODE_SCALE_ASPECT:
4751 rmx_type = RMX_ASPECT;
4753 case DRM_MODE_SCALE_FULLSCREEN:
4754 rmx_type = RMX_FULL;
4756 case DRM_MODE_SCALE_NONE:
4762 if (dm_old_state->scaling == rmx_type)
4765 dm_new_state->scaling = rmx_type;
4767 } else if (property == adev->mode_info.underscan_hborder_property) {
4768 dm_new_state->underscan_hborder = val;
4770 } else if (property == adev->mode_info.underscan_vborder_property) {
4771 dm_new_state->underscan_vborder = val;
4773 } else if (property == adev->mode_info.underscan_property) {
4774 dm_new_state->underscan_enable = val;
4776 } else if (property == adev->mode_info.abm_level_property) {
4777 dm_new_state->abm_level = val;
4784 int amdgpu_dm_connector_atomic_get_property(struct drm_connector *connector,
4785 const struct drm_connector_state *state,
4786 struct drm_property *property,
4789 struct drm_device *dev = connector->dev;
4790 struct amdgpu_device *adev = dev->dev_private;
4791 struct dm_connector_state *dm_state =
4792 to_dm_connector_state(state);
4795 if (property == dev->mode_config.scaling_mode_property) {
4796 switch (dm_state->scaling) {
4798 *val = DRM_MODE_SCALE_CENTER;
4801 *val = DRM_MODE_SCALE_ASPECT;
4804 *val = DRM_MODE_SCALE_FULLSCREEN;
4808 *val = DRM_MODE_SCALE_NONE;
4812 } else if (property == adev->mode_info.underscan_hborder_property) {
4813 *val = dm_state->underscan_hborder;
4815 } else if (property == adev->mode_info.underscan_vborder_property) {
4816 *val = dm_state->underscan_vborder;
4818 } else if (property == adev->mode_info.underscan_property) {
4819 *val = dm_state->underscan_enable;
4821 } else if (property == adev->mode_info.abm_level_property) {
4822 *val = dm_state->abm_level;
4829 static void amdgpu_dm_connector_unregister(struct drm_connector *connector)
4831 struct amdgpu_dm_connector *amdgpu_dm_connector = to_amdgpu_dm_connector(connector);
4833 drm_dp_aux_unregister(&amdgpu_dm_connector->dm_dp_aux.aux);
4836 static void amdgpu_dm_connector_destroy(struct drm_connector *connector)
4838 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
4839 const struct dc_link *link = aconnector->dc_link;
4840 struct amdgpu_device *adev = connector->dev->dev_private;
4841 struct amdgpu_display_manager *dm = &adev->dm;
4843 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
4844 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
4846 if ((link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) &&
4847 link->type != dc_connection_none &&
4848 dm->backlight_dev) {
4849 backlight_device_unregister(dm->backlight_dev);
4850 dm->backlight_dev = NULL;
4854 if (aconnector->dc_em_sink)
4855 dc_sink_release(aconnector->dc_em_sink);
4856 aconnector->dc_em_sink = NULL;
4857 if (aconnector->dc_sink)
4858 dc_sink_release(aconnector->dc_sink);
4859 aconnector->dc_sink = NULL;
4861 drm_dp_cec_unregister_connector(&aconnector->dm_dp_aux.aux);
4862 drm_connector_unregister(connector);
4863 drm_connector_cleanup(connector);
4864 if (aconnector->i2c) {
4865 i2c_del_adapter(&aconnector->i2c->base);
4866 kfree(aconnector->i2c);
4868 kfree(aconnector->dm_dp_aux.aux.name);
4873 void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector)
4875 struct dm_connector_state *state =
4876 to_dm_connector_state(connector->state);
4878 if (connector->state)
4879 __drm_atomic_helper_connector_destroy_state(connector->state);
4883 state = kzalloc(sizeof(*state), GFP_KERNEL);
4886 state->scaling = RMX_OFF;
4887 state->underscan_enable = false;
4888 state->underscan_hborder = 0;
4889 state->underscan_vborder = 0;
4890 state->base.max_requested_bpc = 8;
4891 state->vcpi_slots = 0;
4893 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
4894 state->abm_level = amdgpu_dm_abm_level;
4896 __drm_atomic_helper_connector_reset(connector, &state->base);
4900 struct drm_connector_state *
4901 amdgpu_dm_connector_atomic_duplicate_state(struct drm_connector *connector)
4903 struct dm_connector_state *state =
4904 to_dm_connector_state(connector->state);
4906 struct dm_connector_state *new_state =
4907 kmemdup(state, sizeof(*state), GFP_KERNEL);
4912 __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);
4914 new_state->freesync_capable = state->freesync_capable;
4915 new_state->abm_level = state->abm_level;
4916 new_state->scaling = state->scaling;
4917 new_state->underscan_enable = state->underscan_enable;
4918 new_state->underscan_hborder = state->underscan_hborder;
4919 new_state->underscan_vborder = state->underscan_vborder;
4920 new_state->vcpi_slots = state->vcpi_slots;
4921 new_state->pbn = state->pbn;
4922 return &new_state->base;
4926 amdgpu_dm_connector_late_register(struct drm_connector *connector)
4928 struct amdgpu_dm_connector *amdgpu_dm_connector =
4929 to_amdgpu_dm_connector(connector);
4932 if ((connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
4933 (connector->connector_type == DRM_MODE_CONNECTOR_eDP)) {
4934 amdgpu_dm_connector->dm_dp_aux.aux.dev = connector->kdev;
4935 r = drm_dp_aux_register(&amdgpu_dm_connector->dm_dp_aux.aux);
4940 #if defined(CONFIG_DEBUG_FS)
4941 connector_debugfs_init(amdgpu_dm_connector);
4947 static const struct drm_connector_funcs amdgpu_dm_connector_funcs = {
4948 .reset = amdgpu_dm_connector_funcs_reset,
4949 .detect = amdgpu_dm_connector_detect,
4950 .fill_modes = drm_helper_probe_single_connector_modes,
4951 .destroy = amdgpu_dm_connector_destroy,
4952 .atomic_duplicate_state = amdgpu_dm_connector_atomic_duplicate_state,
4953 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
4954 .atomic_set_property = amdgpu_dm_connector_atomic_set_property,
4955 .atomic_get_property = amdgpu_dm_connector_atomic_get_property,
4956 .late_register = amdgpu_dm_connector_late_register,
4957 .early_unregister = amdgpu_dm_connector_unregister
4960 static int get_modes(struct drm_connector *connector)
4962 return amdgpu_dm_connector_get_modes(connector);
4965 static void create_eml_sink(struct amdgpu_dm_connector *aconnector)
4967 struct dc_sink_init_data init_params = {
4968 .link = aconnector->dc_link,
4969 .sink_signal = SIGNAL_TYPE_VIRTUAL
4973 if (!aconnector->base.edid_blob_ptr) {
4974 DRM_ERROR("No EDID firmware found on connector: %s ,forcing to OFF!\n",
4975 aconnector->base.name);
4977 aconnector->base.force = DRM_FORCE_OFF;
4978 aconnector->base.override_edid = false;
4982 edid = (struct edid *) aconnector->base.edid_blob_ptr->data;
4984 aconnector->edid = edid;
4986 aconnector->dc_em_sink = dc_link_add_remote_sink(
4987 aconnector->dc_link,
4989 (edid->extensions + 1) * EDID_LENGTH,
4992 if (aconnector->base.force == DRM_FORCE_ON) {
4993 aconnector->dc_sink = aconnector->dc_link->local_sink ?
4994 aconnector->dc_link->local_sink :
4995 aconnector->dc_em_sink;
4996 dc_sink_retain(aconnector->dc_sink);
5000 static void handle_edid_mgmt(struct amdgpu_dm_connector *aconnector)
5002 struct dc_link *link = (struct dc_link *)aconnector->dc_link;
5005 * In case of headless boot with force on for DP managed connector
5006 * Those settings have to be != 0 to get initial modeset
5008 if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT) {
5009 link->verified_link_cap.lane_count = LANE_COUNT_FOUR;
5010 link->verified_link_cap.link_rate = LINK_RATE_HIGH2;
5014 aconnector->base.override_edid = true;
5015 create_eml_sink(aconnector);
5018 static struct dc_stream_state *
5019 create_validate_stream_for_sink(struct amdgpu_dm_connector *aconnector,
5020 const struct drm_display_mode *drm_mode,
5021 const struct dm_connector_state *dm_state,
5022 const struct dc_stream_state *old_stream)
5024 struct drm_connector *connector = &aconnector->base;
5025 struct amdgpu_device *adev = connector->dev->dev_private;
5026 struct dc_stream_state *stream;
5027 const struct drm_connector_state *drm_state = dm_state ? &dm_state->base : NULL;
5028 int requested_bpc = drm_state ? drm_state->max_requested_bpc : 8;
5029 enum dc_status dc_result = DC_OK;
5032 stream = create_stream_for_sink(aconnector, drm_mode,
5033 dm_state, old_stream,
5035 if (stream == NULL) {
5036 DRM_ERROR("Failed to create stream for sink!\n");
5040 dc_result = dc_validate_stream(adev->dm.dc, stream);
5042 if (dc_result != DC_OK) {
5043 DRM_DEBUG_KMS("Mode %dx%d (clk %d) failed DC validation with error %d\n",
5049 dc_stream_release(stream);
5051 requested_bpc -= 2; /* lower bpc to retry validation */
5054 } while (stream == NULL && requested_bpc >= 6);
5059 enum drm_mode_status amdgpu_dm_connector_mode_valid(struct drm_connector *connector,
5060 struct drm_display_mode *mode)
5062 int result = MODE_ERROR;
5063 struct dc_sink *dc_sink;
5064 /* TODO: Unhardcode stream count */
5065 struct dc_stream_state *stream;
5066 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
5068 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
5069 (mode->flags & DRM_MODE_FLAG_DBLSCAN))
5073 * Only run this the first time mode_valid is called to initilialize
5076 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED &&
5077 !aconnector->dc_em_sink)
5078 handle_edid_mgmt(aconnector);
5080 dc_sink = to_amdgpu_dm_connector(connector)->dc_sink;
5082 if (dc_sink == NULL) {
5083 DRM_ERROR("dc_sink is NULL!\n");
5087 stream = create_validate_stream_for_sink(aconnector, mode, NULL, NULL);
5089 dc_stream_release(stream);
5094 /* TODO: error handling*/
5098 static int fill_hdr_info_packet(const struct drm_connector_state *state,
5099 struct dc_info_packet *out)
5101 struct hdmi_drm_infoframe frame;
5102 unsigned char buf[30]; /* 26 + 4 */
5106 memset(out, 0, sizeof(*out));
5108 if (!state->hdr_output_metadata)
5111 ret = drm_hdmi_infoframe_set_hdr_metadata(&frame, state);
5115 len = hdmi_drm_infoframe_pack_only(&frame, buf, sizeof(buf));
5119 /* Static metadata is a fixed 26 bytes + 4 byte header. */
5123 /* Prepare the infopacket for DC. */
5124 switch (state->connector->connector_type) {
5125 case DRM_MODE_CONNECTOR_HDMIA:
5126 out->hb0 = 0x87; /* type */
5127 out->hb1 = 0x01; /* version */
5128 out->hb2 = 0x1A; /* length */
5129 out->sb[0] = buf[3]; /* checksum */
5133 case DRM_MODE_CONNECTOR_DisplayPort:
5134 case DRM_MODE_CONNECTOR_eDP:
5135 out->hb0 = 0x00; /* sdp id, zero */
5136 out->hb1 = 0x87; /* type */
5137 out->hb2 = 0x1D; /* payload len - 1 */
5138 out->hb3 = (0x13 << 2); /* sdp version */
5139 out->sb[0] = 0x01; /* version */
5140 out->sb[1] = 0x1A; /* length */
5148 memcpy(&out->sb[i], &buf[4], 26);
5151 print_hex_dump(KERN_DEBUG, "HDR SB:", DUMP_PREFIX_NONE, 16, 1, out->sb,
5152 sizeof(out->sb), false);
5158 is_hdr_metadata_different(const struct drm_connector_state *old_state,
5159 const struct drm_connector_state *new_state)
5161 struct drm_property_blob *old_blob = old_state->hdr_output_metadata;
5162 struct drm_property_blob *new_blob = new_state->hdr_output_metadata;
5164 if (old_blob != new_blob) {
5165 if (old_blob && new_blob &&
5166 old_blob->length == new_blob->length)
5167 return memcmp(old_blob->data, new_blob->data,
5177 amdgpu_dm_connector_atomic_check(struct drm_connector *conn,
5178 struct drm_atomic_state *state)
5180 struct drm_connector_state *new_con_state =
5181 drm_atomic_get_new_connector_state(state, conn);
5182 struct drm_connector_state *old_con_state =
5183 drm_atomic_get_old_connector_state(state, conn);
5184 struct drm_crtc *crtc = new_con_state->crtc;
5185 struct drm_crtc_state *new_crtc_state;
5191 if (is_hdr_metadata_different(old_con_state, new_con_state)) {
5192 struct dc_info_packet hdr_infopacket;
5194 ret = fill_hdr_info_packet(new_con_state, &hdr_infopacket);
5198 new_crtc_state = drm_atomic_get_crtc_state(state, crtc);
5199 if (IS_ERR(new_crtc_state))
5200 return PTR_ERR(new_crtc_state);
5203 * DC considers the stream backends changed if the
5204 * static metadata changes. Forcing the modeset also
5205 * gives a simple way for userspace to switch from
5206 * 8bpc to 10bpc when setting the metadata to enter
5209 * Changing the static metadata after it's been
5210 * set is permissible, however. So only force a
5211 * modeset if we're entering or exiting HDR.
5213 new_crtc_state->mode_changed =
5214 !old_con_state->hdr_output_metadata ||
5215 !new_con_state->hdr_output_metadata;
5221 static const struct drm_connector_helper_funcs
5222 amdgpu_dm_connector_helper_funcs = {
5224 * If hotplugging a second bigger display in FB Con mode, bigger resolution
5225 * modes will be filtered by drm_mode_validate_size(), and those modes
5226 * are missing after user start lightdm. So we need to renew modes list.
5227 * in get_modes call back, not just return the modes count
5229 .get_modes = get_modes,
5230 .mode_valid = amdgpu_dm_connector_mode_valid,
5231 .atomic_check = amdgpu_dm_connector_atomic_check,
5234 static void dm_crtc_helper_disable(struct drm_crtc *crtc)
5238 static bool does_crtc_have_active_cursor(struct drm_crtc_state *new_crtc_state)
5240 struct drm_device *dev = new_crtc_state->crtc->dev;
5241 struct drm_plane *plane;
5243 drm_for_each_plane_mask(plane, dev, new_crtc_state->plane_mask) {
5244 if (plane->type == DRM_PLANE_TYPE_CURSOR)
5251 static int count_crtc_active_planes(struct drm_crtc_state *new_crtc_state)
5253 struct drm_atomic_state *state = new_crtc_state->state;
5254 struct drm_plane *plane;
5257 drm_for_each_plane_mask(plane, state->dev, new_crtc_state->plane_mask) {
5258 struct drm_plane_state *new_plane_state;
5260 /* Cursor planes are "fake". */
5261 if (plane->type == DRM_PLANE_TYPE_CURSOR)
5264 new_plane_state = drm_atomic_get_new_plane_state(state, plane);
5266 if (!new_plane_state) {
5268 * The plane is enable on the CRTC and hasn't changed
5269 * state. This means that it previously passed
5270 * validation and is therefore enabled.
5276 /* We need a framebuffer to be considered enabled. */
5277 num_active += (new_plane_state->fb != NULL);
5284 * Sets whether interrupts should be enabled on a specific CRTC.
5285 * We require that the stream be enabled and that there exist active
5286 * DC planes on the stream.
5289 dm_update_crtc_interrupt_state(struct drm_crtc *crtc,
5290 struct drm_crtc_state *new_crtc_state)
5292 struct dm_crtc_state *dm_new_crtc_state =
5293 to_dm_crtc_state(new_crtc_state);
5295 dm_new_crtc_state->active_planes = 0;
5296 dm_new_crtc_state->interrupts_enabled = false;
5298 if (!dm_new_crtc_state->stream)
5301 dm_new_crtc_state->active_planes =
5302 count_crtc_active_planes(new_crtc_state);
5304 dm_new_crtc_state->interrupts_enabled =
5305 dm_new_crtc_state->active_planes > 0;
5308 static int dm_crtc_helper_atomic_check(struct drm_crtc *crtc,
5309 struct drm_crtc_state *state)
5311 struct amdgpu_device *adev = crtc->dev->dev_private;
5312 struct dc *dc = adev->dm.dc;
5313 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(state);
5317 * Update interrupt state for the CRTC. This needs to happen whenever
5318 * the CRTC has changed or whenever any of its planes have changed.
5319 * Atomic check satisfies both of these requirements since the CRTC
5320 * is added to the state by DRM during drm_atomic_helper_check_planes.
5322 dm_update_crtc_interrupt_state(crtc, state);
5324 if (unlikely(!dm_crtc_state->stream &&
5325 modeset_required(state, NULL, dm_crtc_state->stream))) {
5330 /* In some use cases, like reset, no stream is attached */
5331 if (!dm_crtc_state->stream)
5335 * We want at least one hardware plane enabled to use
5336 * the stream with a cursor enabled.
5338 if (state->enable && state->active &&
5339 does_crtc_have_active_cursor(state) &&
5340 dm_crtc_state->active_planes == 0)
5343 if (dc_validate_stream(dc, dm_crtc_state->stream) == DC_OK)
5349 static bool dm_crtc_helper_mode_fixup(struct drm_crtc *crtc,
5350 const struct drm_display_mode *mode,
5351 struct drm_display_mode *adjusted_mode)
5356 static const struct drm_crtc_helper_funcs amdgpu_dm_crtc_helper_funcs = {
5357 .disable = dm_crtc_helper_disable,
5358 .atomic_check = dm_crtc_helper_atomic_check,
5359 .mode_fixup = dm_crtc_helper_mode_fixup,
5360 .get_scanout_position = amdgpu_crtc_get_scanout_position,
5363 static void dm_encoder_helper_disable(struct drm_encoder *encoder)
5368 static int convert_dc_color_depth_into_bpc (enum dc_color_depth display_color_depth)
5370 switch (display_color_depth) {
5371 case COLOR_DEPTH_666:
5373 case COLOR_DEPTH_888:
5375 case COLOR_DEPTH_101010:
5377 case COLOR_DEPTH_121212:
5379 case COLOR_DEPTH_141414:
5381 case COLOR_DEPTH_161616:
5389 static int dm_encoder_helper_atomic_check(struct drm_encoder *encoder,
5390 struct drm_crtc_state *crtc_state,
5391 struct drm_connector_state *conn_state)
5393 struct drm_atomic_state *state = crtc_state->state;
5394 struct drm_connector *connector = conn_state->connector;
5395 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
5396 struct dm_connector_state *dm_new_connector_state = to_dm_connector_state(conn_state);
5397 const struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
5398 struct drm_dp_mst_topology_mgr *mst_mgr;
5399 struct drm_dp_mst_port *mst_port;
5400 enum dc_color_depth color_depth;
5402 bool is_y420 = false;
5404 if (!aconnector->port || !aconnector->dc_sink)
5407 mst_port = aconnector->port;
5408 mst_mgr = &aconnector->mst_port->mst_mgr;
5410 if (!crtc_state->connectors_changed && !crtc_state->mode_changed)
5413 if (!state->duplicated) {
5414 int max_bpc = conn_state->max_requested_bpc;
5415 is_y420 = drm_mode_is_420_also(&connector->display_info, adjusted_mode) &&
5416 aconnector->force_yuv420_output;
5417 color_depth = convert_color_depth_from_display_info(connector,
5420 bpp = convert_dc_color_depth_into_bpc(color_depth) * 3;
5421 clock = adjusted_mode->clock;
5422 dm_new_connector_state->pbn = drm_dp_calc_pbn_mode(clock, bpp, false);
5424 dm_new_connector_state->vcpi_slots = drm_dp_atomic_find_vcpi_slots(state,
5427 dm_new_connector_state->pbn,
5429 if (dm_new_connector_state->vcpi_slots < 0) {
5430 DRM_DEBUG_ATOMIC("failed finding vcpi slots: %d\n", (int)dm_new_connector_state->vcpi_slots);
5431 return dm_new_connector_state->vcpi_slots;
5436 const struct drm_encoder_helper_funcs amdgpu_dm_encoder_helper_funcs = {
5437 .disable = dm_encoder_helper_disable,
5438 .atomic_check = dm_encoder_helper_atomic_check
5441 #if defined(CONFIG_DRM_AMD_DC_DCN)
5442 static int dm_update_mst_vcpi_slots_for_dsc(struct drm_atomic_state *state,
5443 struct dc_state *dc_state)
5445 struct dc_stream_state *stream = NULL;
5446 struct drm_connector *connector;
5447 struct drm_connector_state *new_con_state, *old_con_state;
5448 struct amdgpu_dm_connector *aconnector;
5449 struct dm_connector_state *dm_conn_state;
5450 int i, j, clock, bpp;
5451 int vcpi, pbn_div, pbn = 0;
5453 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
5455 aconnector = to_amdgpu_dm_connector(connector);
5457 if (!aconnector->port)
5460 if (!new_con_state || !new_con_state->crtc)
5463 dm_conn_state = to_dm_connector_state(new_con_state);
5465 for (j = 0; j < dc_state->stream_count; j++) {
5466 stream = dc_state->streams[j];
5470 if ((struct amdgpu_dm_connector*)stream->dm_stream_context == aconnector)
5479 if (stream->timing.flags.DSC != 1) {
5480 drm_dp_mst_atomic_enable_dsc(state,
5488 pbn_div = dm_mst_get_pbn_divider(stream->link);
5489 bpp = stream->timing.dsc_cfg.bits_per_pixel;
5490 clock = stream->timing.pix_clk_100hz / 10;
5491 pbn = drm_dp_calc_pbn_mode(clock, bpp, true);
5492 vcpi = drm_dp_mst_atomic_enable_dsc(state,
5499 dm_conn_state->pbn = pbn;
5500 dm_conn_state->vcpi_slots = vcpi;
5506 static void dm_drm_plane_reset(struct drm_plane *plane)
5508 struct dm_plane_state *amdgpu_state = NULL;
5511 plane->funcs->atomic_destroy_state(plane, plane->state);
5513 amdgpu_state = kzalloc(sizeof(*amdgpu_state), GFP_KERNEL);
5514 WARN_ON(amdgpu_state == NULL);
5517 __drm_atomic_helper_plane_reset(plane, &amdgpu_state->base);
5520 static struct drm_plane_state *
5521 dm_drm_plane_duplicate_state(struct drm_plane *plane)
5523 struct dm_plane_state *dm_plane_state, *old_dm_plane_state;
5525 old_dm_plane_state = to_dm_plane_state(plane->state);
5526 dm_plane_state = kzalloc(sizeof(*dm_plane_state), GFP_KERNEL);
5527 if (!dm_plane_state)
5530 __drm_atomic_helper_plane_duplicate_state(plane, &dm_plane_state->base);
5532 if (old_dm_plane_state->dc_state) {
5533 dm_plane_state->dc_state = old_dm_plane_state->dc_state;
5534 dc_plane_state_retain(dm_plane_state->dc_state);
5537 return &dm_plane_state->base;
5540 void dm_drm_plane_destroy_state(struct drm_plane *plane,
5541 struct drm_plane_state *state)
5543 struct dm_plane_state *dm_plane_state = to_dm_plane_state(state);
5545 if (dm_plane_state->dc_state)
5546 dc_plane_state_release(dm_plane_state->dc_state);
5548 drm_atomic_helper_plane_destroy_state(plane, state);
5551 static const struct drm_plane_funcs dm_plane_funcs = {
5552 .update_plane = drm_atomic_helper_update_plane,
5553 .disable_plane = drm_atomic_helper_disable_plane,
5554 .destroy = drm_primary_helper_destroy,
5555 .reset = dm_drm_plane_reset,
5556 .atomic_duplicate_state = dm_drm_plane_duplicate_state,
5557 .atomic_destroy_state = dm_drm_plane_destroy_state,
5560 static int dm_plane_helper_prepare_fb(struct drm_plane *plane,
5561 struct drm_plane_state *new_state)
5563 struct amdgpu_framebuffer *afb;
5564 struct drm_gem_object *obj;
5565 struct amdgpu_device *adev;
5566 struct amdgpu_bo *rbo;
5567 struct dm_plane_state *dm_plane_state_new, *dm_plane_state_old;
5568 struct list_head list;
5569 struct ttm_validate_buffer tv;
5570 struct ww_acquire_ctx ticket;
5571 uint64_t tiling_flags;
5574 bool tmz_surface = false;
5575 bool force_disable_dcc = false;
5577 dm_plane_state_old = to_dm_plane_state(plane->state);
5578 dm_plane_state_new = to_dm_plane_state(new_state);
5580 if (!new_state->fb) {
5581 DRM_DEBUG_DRIVER("No FB bound\n");
5585 afb = to_amdgpu_framebuffer(new_state->fb);
5586 obj = new_state->fb->obj[0];
5587 rbo = gem_to_amdgpu_bo(obj);
5588 adev = amdgpu_ttm_adev(rbo->tbo.bdev);
5589 INIT_LIST_HEAD(&list);
5593 list_add(&tv.head, &list);
5595 r = ttm_eu_reserve_buffers(&ticket, &list, false, NULL);
5597 dev_err(adev->dev, "fail to reserve bo (%d)\n", r);
5601 if (plane->type != DRM_PLANE_TYPE_CURSOR)
5602 domain = amdgpu_display_supported_domains(adev, rbo->flags);
5604 domain = AMDGPU_GEM_DOMAIN_VRAM;
5606 r = amdgpu_bo_pin(rbo, domain);
5607 if (unlikely(r != 0)) {
5608 if (r != -ERESTARTSYS)
5609 DRM_ERROR("Failed to pin framebuffer with error %d\n", r);
5610 ttm_eu_backoff_reservation(&ticket, &list);
5614 r = amdgpu_ttm_alloc_gart(&rbo->tbo);
5615 if (unlikely(r != 0)) {
5616 amdgpu_bo_unpin(rbo);
5617 ttm_eu_backoff_reservation(&ticket, &list);
5618 DRM_ERROR("%p bind failed\n", rbo);
5622 amdgpu_bo_get_tiling_flags(rbo, &tiling_flags);
5624 tmz_surface = amdgpu_bo_encrypted(rbo);
5626 ttm_eu_backoff_reservation(&ticket, &list);
5628 afb->address = amdgpu_bo_gpu_offset(rbo);
5632 if (dm_plane_state_new->dc_state &&
5633 dm_plane_state_old->dc_state != dm_plane_state_new->dc_state) {
5634 struct dc_plane_state *plane_state = dm_plane_state_new->dc_state;
5636 force_disable_dcc = adev->asic_type == CHIP_RAVEN && adev->in_suspend;
5637 fill_plane_buffer_attributes(
5638 adev, afb, plane_state->format, plane_state->rotation,
5639 tiling_flags, &plane_state->tiling_info,
5640 &plane_state->plane_size, &plane_state->dcc,
5641 &plane_state->address, tmz_surface,
5648 static void dm_plane_helper_cleanup_fb(struct drm_plane *plane,
5649 struct drm_plane_state *old_state)
5651 struct amdgpu_bo *rbo;
5657 rbo = gem_to_amdgpu_bo(old_state->fb->obj[0]);
5658 r = amdgpu_bo_reserve(rbo, false);
5660 DRM_ERROR("failed to reserve rbo before unpin\n");
5664 amdgpu_bo_unpin(rbo);
5665 amdgpu_bo_unreserve(rbo);
5666 amdgpu_bo_unref(&rbo);
5669 static int dm_plane_atomic_check(struct drm_plane *plane,
5670 struct drm_plane_state *state)
5672 struct amdgpu_device *adev = plane->dev->dev_private;
5673 struct dc *dc = adev->dm.dc;
5674 struct dm_plane_state *dm_plane_state;
5675 struct dc_scaling_info scaling_info;
5678 dm_plane_state = to_dm_plane_state(state);
5680 if (!dm_plane_state->dc_state)
5683 ret = fill_dc_scaling_info(state, &scaling_info);
5687 if (dc_validate_plane(dc, dm_plane_state->dc_state) == DC_OK)
5693 static int dm_plane_atomic_async_check(struct drm_plane *plane,
5694 struct drm_plane_state *new_plane_state)
5696 /* Only support async updates on cursor planes. */
5697 if (plane->type != DRM_PLANE_TYPE_CURSOR)
5703 static void dm_plane_atomic_async_update(struct drm_plane *plane,
5704 struct drm_plane_state *new_state)
5706 struct drm_plane_state *old_state =
5707 drm_atomic_get_old_plane_state(new_state->state, plane);
5709 swap(plane->state->fb, new_state->fb);
5711 plane->state->src_x = new_state->src_x;
5712 plane->state->src_y = new_state->src_y;
5713 plane->state->src_w = new_state->src_w;
5714 plane->state->src_h = new_state->src_h;
5715 plane->state->crtc_x = new_state->crtc_x;
5716 plane->state->crtc_y = new_state->crtc_y;
5717 plane->state->crtc_w = new_state->crtc_w;
5718 plane->state->crtc_h = new_state->crtc_h;
5720 handle_cursor_update(plane, old_state);
5723 static const struct drm_plane_helper_funcs dm_plane_helper_funcs = {
5724 .prepare_fb = dm_plane_helper_prepare_fb,
5725 .cleanup_fb = dm_plane_helper_cleanup_fb,
5726 .atomic_check = dm_plane_atomic_check,
5727 .atomic_async_check = dm_plane_atomic_async_check,
5728 .atomic_async_update = dm_plane_atomic_async_update
5732 * TODO: these are currently initialized to rgb formats only.
5733 * For future use cases we should either initialize them dynamically based on
5734 * plane capabilities, or initialize this array to all formats, so internal drm
5735 * check will succeed, and let DC implement proper check
5737 static const uint32_t rgb_formats[] = {
5738 DRM_FORMAT_XRGB8888,
5739 DRM_FORMAT_ARGB8888,
5740 DRM_FORMAT_RGBA8888,
5741 DRM_FORMAT_XRGB2101010,
5742 DRM_FORMAT_XBGR2101010,
5743 DRM_FORMAT_ARGB2101010,
5744 DRM_FORMAT_ABGR2101010,
5745 DRM_FORMAT_XBGR8888,
5746 DRM_FORMAT_ABGR8888,
5750 static const uint32_t overlay_formats[] = {
5751 DRM_FORMAT_XRGB8888,
5752 DRM_FORMAT_ARGB8888,
5753 DRM_FORMAT_RGBA8888,
5754 DRM_FORMAT_XBGR8888,
5755 DRM_FORMAT_ABGR8888,
5759 static const u32 cursor_formats[] = {
5763 static int get_plane_formats(const struct drm_plane *plane,
5764 const struct dc_plane_cap *plane_cap,
5765 uint32_t *formats, int max_formats)
5767 int i, num_formats = 0;
5770 * TODO: Query support for each group of formats directly from
5771 * DC plane caps. This will require adding more formats to the
5775 switch (plane->type) {
5776 case DRM_PLANE_TYPE_PRIMARY:
5777 for (i = 0; i < ARRAY_SIZE(rgb_formats); ++i) {
5778 if (num_formats >= max_formats)
5781 formats[num_formats++] = rgb_formats[i];
5784 if (plane_cap && plane_cap->pixel_format_support.nv12)
5785 formats[num_formats++] = DRM_FORMAT_NV12;
5786 if (plane_cap && plane_cap->pixel_format_support.p010)
5787 formats[num_formats++] = DRM_FORMAT_P010;
5788 if (plane_cap && plane_cap->pixel_format_support.fp16) {
5789 formats[num_formats++] = DRM_FORMAT_XRGB16161616F;
5790 formats[num_formats++] = DRM_FORMAT_ARGB16161616F;
5791 formats[num_formats++] = DRM_FORMAT_XBGR16161616F;
5792 formats[num_formats++] = DRM_FORMAT_ABGR16161616F;
5796 case DRM_PLANE_TYPE_OVERLAY:
5797 for (i = 0; i < ARRAY_SIZE(overlay_formats); ++i) {
5798 if (num_formats >= max_formats)
5801 formats[num_formats++] = overlay_formats[i];
5805 case DRM_PLANE_TYPE_CURSOR:
5806 for (i = 0; i < ARRAY_SIZE(cursor_formats); ++i) {
5807 if (num_formats >= max_formats)
5810 formats[num_formats++] = cursor_formats[i];
5818 static int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
5819 struct drm_plane *plane,
5820 unsigned long possible_crtcs,
5821 const struct dc_plane_cap *plane_cap)
5823 uint32_t formats[32];
5827 num_formats = get_plane_formats(plane, plane_cap, formats,
5828 ARRAY_SIZE(formats));
5830 res = drm_universal_plane_init(dm->adev->ddev, plane, possible_crtcs,
5831 &dm_plane_funcs, formats, num_formats,
5832 NULL, plane->type, NULL);
5836 if (plane->type == DRM_PLANE_TYPE_OVERLAY &&
5837 plane_cap && plane_cap->per_pixel_alpha) {
5838 unsigned int blend_caps = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
5839 BIT(DRM_MODE_BLEND_PREMULTI);
5841 drm_plane_create_alpha_property(plane);
5842 drm_plane_create_blend_mode_property(plane, blend_caps);
5845 if (plane->type == DRM_PLANE_TYPE_PRIMARY &&
5847 (plane_cap->pixel_format_support.nv12 ||
5848 plane_cap->pixel_format_support.p010)) {
5849 /* This only affects YUV formats. */
5850 drm_plane_create_color_properties(
5852 BIT(DRM_COLOR_YCBCR_BT601) |
5853 BIT(DRM_COLOR_YCBCR_BT709) |
5854 BIT(DRM_COLOR_YCBCR_BT2020),
5855 BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) |
5856 BIT(DRM_COLOR_YCBCR_FULL_RANGE),
5857 DRM_COLOR_YCBCR_BT709, DRM_COLOR_YCBCR_LIMITED_RANGE);
5860 drm_plane_helper_add(plane, &dm_plane_helper_funcs);
5862 /* Create (reset) the plane state */
5863 if (plane->funcs->reset)
5864 plane->funcs->reset(plane);
5869 static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
5870 struct drm_plane *plane,
5871 uint32_t crtc_index)
5873 struct amdgpu_crtc *acrtc = NULL;
5874 struct drm_plane *cursor_plane;
5878 cursor_plane = kzalloc(sizeof(*cursor_plane), GFP_KERNEL);
5882 cursor_plane->type = DRM_PLANE_TYPE_CURSOR;
5883 res = amdgpu_dm_plane_init(dm, cursor_plane, 0, NULL);
5885 acrtc = kzalloc(sizeof(struct amdgpu_crtc), GFP_KERNEL);
5889 res = drm_crtc_init_with_planes(
5894 &amdgpu_dm_crtc_funcs, NULL);
5899 drm_crtc_helper_add(&acrtc->base, &amdgpu_dm_crtc_helper_funcs);
5901 /* Create (reset) the plane state */
5902 if (acrtc->base.funcs->reset)
5903 acrtc->base.funcs->reset(&acrtc->base);
5905 acrtc->max_cursor_width = dm->adev->dm.dc->caps.max_cursor_size;
5906 acrtc->max_cursor_height = dm->adev->dm.dc->caps.max_cursor_size;
5908 acrtc->crtc_id = crtc_index;
5909 acrtc->base.enabled = false;
5910 acrtc->otg_inst = -1;
5912 dm->adev->mode_info.crtcs[crtc_index] = acrtc;
5913 drm_crtc_enable_color_mgmt(&acrtc->base, MAX_COLOR_LUT_ENTRIES,
5914 true, MAX_COLOR_LUT_ENTRIES);
5915 drm_mode_crtc_set_gamma_size(&acrtc->base, MAX_COLOR_LEGACY_LUT_ENTRIES);
5921 kfree(cursor_plane);
5926 static int to_drm_connector_type(enum signal_type st)
5929 case SIGNAL_TYPE_HDMI_TYPE_A:
5930 return DRM_MODE_CONNECTOR_HDMIA;
5931 case SIGNAL_TYPE_EDP:
5932 return DRM_MODE_CONNECTOR_eDP;
5933 case SIGNAL_TYPE_LVDS:
5934 return DRM_MODE_CONNECTOR_LVDS;
5935 case SIGNAL_TYPE_RGB:
5936 return DRM_MODE_CONNECTOR_VGA;
5937 case SIGNAL_TYPE_DISPLAY_PORT:
5938 case SIGNAL_TYPE_DISPLAY_PORT_MST:
5939 return DRM_MODE_CONNECTOR_DisplayPort;
5940 case SIGNAL_TYPE_DVI_DUAL_LINK:
5941 case SIGNAL_TYPE_DVI_SINGLE_LINK:
5942 return DRM_MODE_CONNECTOR_DVID;
5943 case SIGNAL_TYPE_VIRTUAL:
5944 return DRM_MODE_CONNECTOR_VIRTUAL;
5947 return DRM_MODE_CONNECTOR_Unknown;
5951 static struct drm_encoder *amdgpu_dm_connector_to_encoder(struct drm_connector *connector)
5953 struct drm_encoder *encoder;
5955 /* There is only one encoder per connector */
5956 drm_connector_for_each_possible_encoder(connector, encoder)
5962 static void amdgpu_dm_get_native_mode(struct drm_connector *connector)
5964 struct drm_encoder *encoder;
5965 struct amdgpu_encoder *amdgpu_encoder;
5967 encoder = amdgpu_dm_connector_to_encoder(connector);
5969 if (encoder == NULL)
5972 amdgpu_encoder = to_amdgpu_encoder(encoder);
5974 amdgpu_encoder->native_mode.clock = 0;
5976 if (!list_empty(&connector->probed_modes)) {
5977 struct drm_display_mode *preferred_mode = NULL;
5979 list_for_each_entry(preferred_mode,
5980 &connector->probed_modes,
5982 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED)
5983 amdgpu_encoder->native_mode = *preferred_mode;
5991 static struct drm_display_mode *
5992 amdgpu_dm_create_common_mode(struct drm_encoder *encoder,
5994 int hdisplay, int vdisplay)
5996 struct drm_device *dev = encoder->dev;
5997 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
5998 struct drm_display_mode *mode = NULL;
5999 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
6001 mode = drm_mode_duplicate(dev, native_mode);
6006 mode->hdisplay = hdisplay;
6007 mode->vdisplay = vdisplay;
6008 mode->type &= ~DRM_MODE_TYPE_PREFERRED;
6009 strscpy(mode->name, name, DRM_DISPLAY_MODE_LEN);
6015 static void amdgpu_dm_connector_add_common_modes(struct drm_encoder *encoder,
6016 struct drm_connector *connector)
6018 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
6019 struct drm_display_mode *mode = NULL;
6020 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
6021 struct amdgpu_dm_connector *amdgpu_dm_connector =
6022 to_amdgpu_dm_connector(connector);
6026 char name[DRM_DISPLAY_MODE_LEN];
6029 } common_modes[] = {
6030 { "640x480", 640, 480},
6031 { "800x600", 800, 600},
6032 { "1024x768", 1024, 768},
6033 { "1280x720", 1280, 720},
6034 { "1280x800", 1280, 800},
6035 {"1280x1024", 1280, 1024},
6036 { "1440x900", 1440, 900},
6037 {"1680x1050", 1680, 1050},
6038 {"1600x1200", 1600, 1200},
6039 {"1920x1080", 1920, 1080},
6040 {"1920x1200", 1920, 1200}
6043 n = ARRAY_SIZE(common_modes);
6045 for (i = 0; i < n; i++) {
6046 struct drm_display_mode *curmode = NULL;
6047 bool mode_existed = false;
6049 if (common_modes[i].w > native_mode->hdisplay ||
6050 common_modes[i].h > native_mode->vdisplay ||
6051 (common_modes[i].w == native_mode->hdisplay &&
6052 common_modes[i].h == native_mode->vdisplay))
6055 list_for_each_entry(curmode, &connector->probed_modes, head) {
6056 if (common_modes[i].w == curmode->hdisplay &&
6057 common_modes[i].h == curmode->vdisplay) {
6058 mode_existed = true;
6066 mode = amdgpu_dm_create_common_mode(encoder,
6067 common_modes[i].name, common_modes[i].w,
6069 drm_mode_probed_add(connector, mode);
6070 amdgpu_dm_connector->num_modes++;
6074 static void amdgpu_dm_connector_ddc_get_modes(struct drm_connector *connector,
6077 struct amdgpu_dm_connector *amdgpu_dm_connector =
6078 to_amdgpu_dm_connector(connector);
6081 /* empty probed_modes */
6082 INIT_LIST_HEAD(&connector->probed_modes);
6083 amdgpu_dm_connector->num_modes =
6084 drm_add_edid_modes(connector, edid);
6086 /* sorting the probed modes before calling function
6087 * amdgpu_dm_get_native_mode() since EDID can have
6088 * more than one preferred mode. The modes that are
6089 * later in the probed mode list could be of higher
6090 * and preferred resolution. For example, 3840x2160
6091 * resolution in base EDID preferred timing and 4096x2160
6092 * preferred resolution in DID extension block later.
6094 drm_mode_sort(&connector->probed_modes);
6095 amdgpu_dm_get_native_mode(connector);
6097 amdgpu_dm_connector->num_modes = 0;
6101 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector)
6103 struct amdgpu_dm_connector *amdgpu_dm_connector =
6104 to_amdgpu_dm_connector(connector);
6105 struct drm_encoder *encoder;
6106 struct edid *edid = amdgpu_dm_connector->edid;
6108 encoder = amdgpu_dm_connector_to_encoder(connector);
6110 if (!edid || !drm_edid_is_valid(edid)) {
6111 amdgpu_dm_connector->num_modes =
6112 drm_add_modes_noedid(connector, 640, 480);
6114 amdgpu_dm_connector_ddc_get_modes(connector, edid);
6115 amdgpu_dm_connector_add_common_modes(encoder, connector);
6117 amdgpu_dm_fbc_init(connector);
6119 return amdgpu_dm_connector->num_modes;
6122 void amdgpu_dm_connector_init_helper(struct amdgpu_display_manager *dm,
6123 struct amdgpu_dm_connector *aconnector,
6125 struct dc_link *link,
6128 struct amdgpu_device *adev = dm->ddev->dev_private;
6131 * Some of the properties below require access to state, like bpc.
6132 * Allocate some default initial connector state with our reset helper.
6134 if (aconnector->base.funcs->reset)
6135 aconnector->base.funcs->reset(&aconnector->base);
6137 aconnector->connector_id = link_index;
6138 aconnector->dc_link = link;
6139 aconnector->base.interlace_allowed = false;
6140 aconnector->base.doublescan_allowed = false;
6141 aconnector->base.stereo_allowed = false;
6142 aconnector->base.dpms = DRM_MODE_DPMS_OFF;
6143 aconnector->hpd.hpd = AMDGPU_HPD_NONE; /* not used */
6144 aconnector->audio_inst = -1;
6145 mutex_init(&aconnector->hpd_lock);
6148 * configure support HPD hot plug connector_>polled default value is 0
6149 * which means HPD hot plug not supported
6151 switch (connector_type) {
6152 case DRM_MODE_CONNECTOR_HDMIA:
6153 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
6154 aconnector->base.ycbcr_420_allowed =
6155 link->link_enc->features.hdmi_ycbcr420_supported ? true : false;
6157 case DRM_MODE_CONNECTOR_DisplayPort:
6158 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
6159 aconnector->base.ycbcr_420_allowed =
6160 link->link_enc->features.dp_ycbcr420_supported ? true : false;
6162 case DRM_MODE_CONNECTOR_DVID:
6163 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
6169 drm_object_attach_property(&aconnector->base.base,
6170 dm->ddev->mode_config.scaling_mode_property,
6171 DRM_MODE_SCALE_NONE);
6173 drm_object_attach_property(&aconnector->base.base,
6174 adev->mode_info.underscan_property,
6176 drm_object_attach_property(&aconnector->base.base,
6177 adev->mode_info.underscan_hborder_property,
6179 drm_object_attach_property(&aconnector->base.base,
6180 adev->mode_info.underscan_vborder_property,
6183 if (!aconnector->mst_port)
6184 drm_connector_attach_max_bpc_property(&aconnector->base, 8, 16);
6186 /* This defaults to the max in the range, but we want 8bpc for non-edp. */
6187 aconnector->base.state->max_bpc = (connector_type == DRM_MODE_CONNECTOR_eDP) ? 16 : 8;
6188 aconnector->base.state->max_requested_bpc = aconnector->base.state->max_bpc;
6190 if (connector_type == DRM_MODE_CONNECTOR_eDP &&
6191 dc_is_dmcu_initialized(adev->dm.dc)) {
6192 drm_object_attach_property(&aconnector->base.base,
6193 adev->mode_info.abm_level_property, 0);
6196 if (connector_type == DRM_MODE_CONNECTOR_HDMIA ||
6197 connector_type == DRM_MODE_CONNECTOR_DisplayPort ||
6198 connector_type == DRM_MODE_CONNECTOR_eDP) {
6199 drm_object_attach_property(
6200 &aconnector->base.base,
6201 dm->ddev->mode_config.hdr_output_metadata_property, 0);
6203 if (!aconnector->mst_port)
6204 drm_connector_attach_vrr_capable_property(&aconnector->base);
6206 #ifdef CONFIG_DRM_AMD_DC_HDCP
6207 if (adev->dm.hdcp_workqueue)
6208 drm_connector_attach_content_protection_property(&aconnector->base, true);
6213 static int amdgpu_dm_i2c_xfer(struct i2c_adapter *i2c_adap,
6214 struct i2c_msg *msgs, int num)
6216 struct amdgpu_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
6217 struct ddc_service *ddc_service = i2c->ddc_service;
6218 struct i2c_command cmd;
6222 cmd.payloads = kcalloc(num, sizeof(struct i2c_payload), GFP_KERNEL);
6227 cmd.number_of_payloads = num;
6228 cmd.engine = I2C_COMMAND_ENGINE_DEFAULT;
6231 for (i = 0; i < num; i++) {
6232 cmd.payloads[i].write = !(msgs[i].flags & I2C_M_RD);
6233 cmd.payloads[i].address = msgs[i].addr;
6234 cmd.payloads[i].length = msgs[i].len;
6235 cmd.payloads[i].data = msgs[i].buf;
6239 ddc_service->ctx->dc,
6240 ddc_service->ddc_pin->hw_info.ddc_channel,
6244 kfree(cmd.payloads);
6248 static u32 amdgpu_dm_i2c_func(struct i2c_adapter *adap)
6250 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
6253 static const struct i2c_algorithm amdgpu_dm_i2c_algo = {
6254 .master_xfer = amdgpu_dm_i2c_xfer,
6255 .functionality = amdgpu_dm_i2c_func,
6258 static struct amdgpu_i2c_adapter *
6259 create_i2c(struct ddc_service *ddc_service,
6263 struct amdgpu_device *adev = ddc_service->ctx->driver_context;
6264 struct amdgpu_i2c_adapter *i2c;
6266 i2c = kzalloc(sizeof(struct amdgpu_i2c_adapter), GFP_KERNEL);
6269 i2c->base.owner = THIS_MODULE;
6270 i2c->base.class = I2C_CLASS_DDC;
6271 i2c->base.dev.parent = &adev->pdev->dev;
6272 i2c->base.algo = &amdgpu_dm_i2c_algo;
6273 snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c hw bus %d", link_index);
6274 i2c_set_adapdata(&i2c->base, i2c);
6275 i2c->ddc_service = ddc_service;
6276 i2c->ddc_service->ddc_pin->hw_info.ddc_channel = link_index;
6283 * Note: this function assumes that dc_link_detect() was called for the
6284 * dc_link which will be represented by this aconnector.
6286 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
6287 struct amdgpu_dm_connector *aconnector,
6288 uint32_t link_index,
6289 struct amdgpu_encoder *aencoder)
6293 struct dc *dc = dm->dc;
6294 struct dc_link *link = dc_get_link_at_index(dc, link_index);
6295 struct amdgpu_i2c_adapter *i2c;
6297 link->priv = aconnector;
6299 DRM_DEBUG_DRIVER("%s()\n", __func__);
6301 i2c = create_i2c(link->ddc, link->link_index, &res);
6303 DRM_ERROR("Failed to create i2c adapter data\n");
6307 aconnector->i2c = i2c;
6308 res = i2c_add_adapter(&i2c->base);
6311 DRM_ERROR("Failed to register hw i2c %d\n", link->link_index);
6315 connector_type = to_drm_connector_type(link->connector_signal);
6317 res = drm_connector_init_with_ddc(
6320 &amdgpu_dm_connector_funcs,
6325 DRM_ERROR("connector_init failed\n");
6326 aconnector->connector_id = -1;
6330 drm_connector_helper_add(
6332 &amdgpu_dm_connector_helper_funcs);
6334 amdgpu_dm_connector_init_helper(
6341 drm_connector_attach_encoder(
6342 &aconnector->base, &aencoder->base);
6344 if (connector_type == DRM_MODE_CONNECTOR_DisplayPort
6345 || connector_type == DRM_MODE_CONNECTOR_eDP)
6346 amdgpu_dm_initialize_dp_connector(dm, aconnector, link->link_index);
6351 aconnector->i2c = NULL;
6356 int amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device *adev)
6358 switch (adev->mode_info.num_crtc) {
6375 static int amdgpu_dm_encoder_init(struct drm_device *dev,
6376 struct amdgpu_encoder *aencoder,
6377 uint32_t link_index)
6379 struct amdgpu_device *adev = dev->dev_private;
6381 int res = drm_encoder_init(dev,
6383 &amdgpu_dm_encoder_funcs,
6384 DRM_MODE_ENCODER_TMDS,
6387 aencoder->base.possible_crtcs = amdgpu_dm_get_encoder_crtc_mask(adev);
6390 aencoder->encoder_id = link_index;
6392 aencoder->encoder_id = -1;
6394 drm_encoder_helper_add(&aencoder->base, &amdgpu_dm_encoder_helper_funcs);
6399 static void manage_dm_interrupts(struct amdgpu_device *adev,
6400 struct amdgpu_crtc *acrtc,
6404 * this is not correct translation but will work as soon as VBLANK
6405 * constant is the same as PFLIP
6408 amdgpu_display_crtc_idx_to_irq_type(
6413 drm_crtc_vblank_on(&acrtc->base);
6416 &adev->pageflip_irq,
6422 &adev->pageflip_irq,
6424 drm_crtc_vblank_off(&acrtc->base);
6429 is_scaling_state_different(const struct dm_connector_state *dm_state,
6430 const struct dm_connector_state *old_dm_state)
6432 if (dm_state->scaling != old_dm_state->scaling)
6434 if (!dm_state->underscan_enable && old_dm_state->underscan_enable) {
6435 if (old_dm_state->underscan_hborder != 0 && old_dm_state->underscan_vborder != 0)
6437 } else if (dm_state->underscan_enable && !old_dm_state->underscan_enable) {
6438 if (dm_state->underscan_hborder != 0 && dm_state->underscan_vborder != 0)
6440 } else if (dm_state->underscan_hborder != old_dm_state->underscan_hborder ||
6441 dm_state->underscan_vborder != old_dm_state->underscan_vborder)
6446 #ifdef CONFIG_DRM_AMD_DC_HDCP
6447 static bool is_content_protection_different(struct drm_connector_state *state,
6448 const struct drm_connector_state *old_state,
6449 const struct drm_connector *connector, struct hdcp_workqueue *hdcp_w)
6451 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
6453 if (old_state->hdcp_content_type != state->hdcp_content_type &&
6454 state->content_protection != DRM_MODE_CONTENT_PROTECTION_UNDESIRED) {
6455 state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
6459 /* CP is being re enabled, ignore this */
6460 if (old_state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED &&
6461 state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED) {
6462 state->content_protection = DRM_MODE_CONTENT_PROTECTION_ENABLED;
6466 /* S3 resume case, since old state will always be 0 (UNDESIRED) and the restored state will be ENABLED */
6467 if (old_state->content_protection == DRM_MODE_CONTENT_PROTECTION_UNDESIRED &&
6468 state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED)
6469 state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
6471 /* Check if something is connected/enabled, otherwise we start hdcp but nothing is connected/enabled
6472 * hot-plug, headless s3, dpms
6474 if (state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED && connector->dpms == DRM_MODE_DPMS_ON &&
6475 aconnector->dc_sink != NULL)
6478 if (old_state->content_protection == state->content_protection)
6481 if (state->content_protection == DRM_MODE_CONTENT_PROTECTION_UNDESIRED)
6488 static void remove_stream(struct amdgpu_device *adev,
6489 struct amdgpu_crtc *acrtc,
6490 struct dc_stream_state *stream)
6492 /* this is the update mode case */
6494 acrtc->otg_inst = -1;
6495 acrtc->enabled = false;
6498 static int get_cursor_position(struct drm_plane *plane, struct drm_crtc *crtc,
6499 struct dc_cursor_position *position)
6501 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
6503 int xorigin = 0, yorigin = 0;
6505 position->enable = false;
6509 if (!crtc || !plane->state->fb)
6512 if ((plane->state->crtc_w > amdgpu_crtc->max_cursor_width) ||
6513 (plane->state->crtc_h > amdgpu_crtc->max_cursor_height)) {
6514 DRM_ERROR("%s: bad cursor width or height %d x %d\n",
6516 plane->state->crtc_w,
6517 plane->state->crtc_h);
6521 x = plane->state->crtc_x;
6522 y = plane->state->crtc_y;
6524 if (x <= -amdgpu_crtc->max_cursor_width ||
6525 y <= -amdgpu_crtc->max_cursor_height)
6529 xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
6533 yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
6536 position->enable = true;
6537 position->translate_by_source = true;
6540 position->x_hotspot = xorigin;
6541 position->y_hotspot = yorigin;
6546 static void handle_cursor_update(struct drm_plane *plane,
6547 struct drm_plane_state *old_plane_state)
6549 struct amdgpu_device *adev = plane->dev->dev_private;
6550 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(plane->state->fb);
6551 struct drm_crtc *crtc = afb ? plane->state->crtc : old_plane_state->crtc;
6552 struct dm_crtc_state *crtc_state = crtc ? to_dm_crtc_state(crtc->state) : NULL;
6553 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
6554 uint64_t address = afb ? afb->address : 0;
6555 struct dc_cursor_position position;
6556 struct dc_cursor_attributes attributes;
6559 if (!plane->state->fb && !old_plane_state->fb)
6562 DRM_DEBUG_DRIVER("%s: crtc_id=%d with size %d to %d\n",
6564 amdgpu_crtc->crtc_id,
6565 plane->state->crtc_w,
6566 plane->state->crtc_h);
6568 ret = get_cursor_position(plane, crtc, &position);
6572 if (!position.enable) {
6573 /* turn off cursor */
6574 if (crtc_state && crtc_state->stream) {
6575 mutex_lock(&adev->dm.dc_lock);
6576 dc_stream_set_cursor_position(crtc_state->stream,
6578 mutex_unlock(&adev->dm.dc_lock);
6583 amdgpu_crtc->cursor_width = plane->state->crtc_w;
6584 amdgpu_crtc->cursor_height = plane->state->crtc_h;
6586 memset(&attributes, 0, sizeof(attributes));
6587 attributes.address.high_part = upper_32_bits(address);
6588 attributes.address.low_part = lower_32_bits(address);
6589 attributes.width = plane->state->crtc_w;
6590 attributes.height = plane->state->crtc_h;
6591 attributes.color_format = CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA;
6592 attributes.rotation_angle = 0;
6593 attributes.attribute_flags.value = 0;
6595 attributes.pitch = attributes.width;
6597 if (crtc_state->stream) {
6598 mutex_lock(&adev->dm.dc_lock);
6599 if (!dc_stream_set_cursor_attributes(crtc_state->stream,
6601 DRM_ERROR("DC failed to set cursor attributes\n");
6603 if (!dc_stream_set_cursor_position(crtc_state->stream,
6605 DRM_ERROR("DC failed to set cursor position\n");
6606 mutex_unlock(&adev->dm.dc_lock);
6610 static void prepare_flip_isr(struct amdgpu_crtc *acrtc)
6613 assert_spin_locked(&acrtc->base.dev->event_lock);
6614 WARN_ON(acrtc->event);
6616 acrtc->event = acrtc->base.state->event;
6618 /* Set the flip status */
6619 acrtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
6621 /* Mark this event as consumed */
6622 acrtc->base.state->event = NULL;
6624 DRM_DEBUG_DRIVER("crtc:%d, pflip_stat:AMDGPU_FLIP_SUBMITTED\n",
6628 static void update_freesync_state_on_stream(
6629 struct amdgpu_display_manager *dm,
6630 struct dm_crtc_state *new_crtc_state,
6631 struct dc_stream_state *new_stream,
6632 struct dc_plane_state *surface,
6633 u32 flip_timestamp_in_us)
6635 struct mod_vrr_params vrr_params;
6636 struct dc_info_packet vrr_infopacket = {0};
6637 struct amdgpu_device *adev = dm->adev;
6638 unsigned long flags;
6644 * TODO: Determine why min/max totals and vrefresh can be 0 here.
6645 * For now it's sufficient to just guard against these conditions.
6648 if (!new_stream->timing.h_total || !new_stream->timing.v_total)
6651 spin_lock_irqsave(&adev->ddev->event_lock, flags);
6652 vrr_params = new_crtc_state->vrr_params;
6655 mod_freesync_handle_preflip(
6656 dm->freesync_module,
6659 flip_timestamp_in_us,
6662 if (adev->family < AMDGPU_FAMILY_AI &&
6663 amdgpu_dm_vrr_active(new_crtc_state)) {
6664 mod_freesync_handle_v_update(dm->freesync_module,
6665 new_stream, &vrr_params);
6667 /* Need to call this before the frame ends. */
6668 dc_stream_adjust_vmin_vmax(dm->dc,
6669 new_crtc_state->stream,
6670 &vrr_params.adjust);
6674 mod_freesync_build_vrr_infopacket(
6675 dm->freesync_module,
6679 TRANSFER_FUNC_UNKNOWN,
6682 new_crtc_state->freesync_timing_changed |=
6683 (memcmp(&new_crtc_state->vrr_params.adjust,
6685 sizeof(vrr_params.adjust)) != 0);
6687 new_crtc_state->freesync_vrr_info_changed |=
6688 (memcmp(&new_crtc_state->vrr_infopacket,
6690 sizeof(vrr_infopacket)) != 0);
6692 new_crtc_state->vrr_params = vrr_params;
6693 new_crtc_state->vrr_infopacket = vrr_infopacket;
6695 new_stream->adjust = new_crtc_state->vrr_params.adjust;
6696 new_stream->vrr_infopacket = vrr_infopacket;
6698 if (new_crtc_state->freesync_vrr_info_changed)
6699 DRM_DEBUG_KMS("VRR packet update: crtc=%u enabled=%d state=%d",
6700 new_crtc_state->base.crtc->base.id,
6701 (int)new_crtc_state->base.vrr_enabled,
6702 (int)vrr_params.state);
6704 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
6707 static void pre_update_freesync_state_on_stream(
6708 struct amdgpu_display_manager *dm,
6709 struct dm_crtc_state *new_crtc_state)
6711 struct dc_stream_state *new_stream = new_crtc_state->stream;
6712 struct mod_vrr_params vrr_params;
6713 struct mod_freesync_config config = new_crtc_state->freesync_config;
6714 struct amdgpu_device *adev = dm->adev;
6715 unsigned long flags;
6721 * TODO: Determine why min/max totals and vrefresh can be 0 here.
6722 * For now it's sufficient to just guard against these conditions.
6724 if (!new_stream->timing.h_total || !new_stream->timing.v_total)
6727 spin_lock_irqsave(&adev->ddev->event_lock, flags);
6728 vrr_params = new_crtc_state->vrr_params;
6730 if (new_crtc_state->vrr_supported &&
6731 config.min_refresh_in_uhz &&
6732 config.max_refresh_in_uhz) {
6733 config.state = new_crtc_state->base.vrr_enabled ?
6734 VRR_STATE_ACTIVE_VARIABLE :
6737 config.state = VRR_STATE_UNSUPPORTED;
6740 mod_freesync_build_vrr_params(dm->freesync_module,
6742 &config, &vrr_params);
6744 new_crtc_state->freesync_timing_changed |=
6745 (memcmp(&new_crtc_state->vrr_params.adjust,
6747 sizeof(vrr_params.adjust)) != 0);
6749 new_crtc_state->vrr_params = vrr_params;
6750 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
6753 static void amdgpu_dm_handle_vrr_transition(struct dm_crtc_state *old_state,
6754 struct dm_crtc_state *new_state)
6756 bool old_vrr_active = amdgpu_dm_vrr_active(old_state);
6757 bool new_vrr_active = amdgpu_dm_vrr_active(new_state);
6759 if (!old_vrr_active && new_vrr_active) {
6760 /* Transition VRR inactive -> active:
6761 * While VRR is active, we must not disable vblank irq, as a
6762 * reenable after disable would compute bogus vblank/pflip
6763 * timestamps if it likely happened inside display front-porch.
6765 * We also need vupdate irq for the actual core vblank handling
6768 dm_set_vupdate_irq(new_state->base.crtc, true);
6769 drm_crtc_vblank_get(new_state->base.crtc);
6770 DRM_DEBUG_DRIVER("%s: crtc=%u VRR off->on: Get vblank ref\n",
6771 __func__, new_state->base.crtc->base.id);
6772 } else if (old_vrr_active && !new_vrr_active) {
6773 /* Transition VRR active -> inactive:
6774 * Allow vblank irq disable again for fixed refresh rate.
6776 dm_set_vupdate_irq(new_state->base.crtc, false);
6777 drm_crtc_vblank_put(new_state->base.crtc);
6778 DRM_DEBUG_DRIVER("%s: crtc=%u VRR on->off: Drop vblank ref\n",
6779 __func__, new_state->base.crtc->base.id);
6783 static void amdgpu_dm_commit_cursors(struct drm_atomic_state *state)
6785 struct drm_plane *plane;
6786 struct drm_plane_state *old_plane_state, *new_plane_state;
6790 * TODO: Make this per-stream so we don't issue redundant updates for
6791 * commits with multiple streams.
6793 for_each_oldnew_plane_in_state(state, plane, old_plane_state,
6795 if (plane->type == DRM_PLANE_TYPE_CURSOR)
6796 handle_cursor_update(plane, old_plane_state);
6799 static void amdgpu_dm_commit_planes(struct drm_atomic_state *state,
6800 struct dc_state *dc_state,
6801 struct drm_device *dev,
6802 struct amdgpu_display_manager *dm,
6803 struct drm_crtc *pcrtc,
6804 bool wait_for_vblank)
6807 uint64_t timestamp_ns;
6808 struct drm_plane *plane;
6809 struct drm_plane_state *old_plane_state, *new_plane_state;
6810 struct amdgpu_crtc *acrtc_attach = to_amdgpu_crtc(pcrtc);
6811 struct drm_crtc_state *new_pcrtc_state =
6812 drm_atomic_get_new_crtc_state(state, pcrtc);
6813 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(new_pcrtc_state);
6814 struct dm_crtc_state *dm_old_crtc_state =
6815 to_dm_crtc_state(drm_atomic_get_old_crtc_state(state, pcrtc));
6816 int planes_count = 0, vpos, hpos;
6818 unsigned long flags;
6819 struct amdgpu_bo *abo;
6820 uint64_t tiling_flags;
6821 bool tmz_surface = false;
6822 uint32_t target_vblank, last_flip_vblank;
6823 bool vrr_active = amdgpu_dm_vrr_active(acrtc_state);
6824 bool pflip_present = false;
6826 struct dc_surface_update surface_updates[MAX_SURFACES];
6827 struct dc_plane_info plane_infos[MAX_SURFACES];
6828 struct dc_scaling_info scaling_infos[MAX_SURFACES];
6829 struct dc_flip_addrs flip_addrs[MAX_SURFACES];
6830 struct dc_stream_update stream_update;
6833 bundle = kzalloc(sizeof(*bundle), GFP_KERNEL);
6836 dm_error("Failed to allocate update bundle\n");
6841 * Disable the cursor first if we're disabling all the planes.
6842 * It'll remain on the screen after the planes are re-enabled
6845 if (acrtc_state->active_planes == 0)
6846 amdgpu_dm_commit_cursors(state);
6848 /* update planes when needed */
6849 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
6850 struct drm_crtc *crtc = new_plane_state->crtc;
6851 struct drm_crtc_state *new_crtc_state;
6852 struct drm_framebuffer *fb = new_plane_state->fb;
6853 bool plane_needs_flip;
6854 struct dc_plane_state *dc_plane;
6855 struct dm_plane_state *dm_new_plane_state = to_dm_plane_state(new_plane_state);
6857 /* Cursor plane is handled after stream updates */
6858 if (plane->type == DRM_PLANE_TYPE_CURSOR)
6861 if (!fb || !crtc || pcrtc != crtc)
6864 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
6865 if (!new_crtc_state->active)
6868 dc_plane = dm_new_plane_state->dc_state;
6870 bundle->surface_updates[planes_count].surface = dc_plane;
6871 if (new_pcrtc_state->color_mgmt_changed) {
6872 bundle->surface_updates[planes_count].gamma = dc_plane->gamma_correction;
6873 bundle->surface_updates[planes_count].in_transfer_func = dc_plane->in_transfer_func;
6874 bundle->surface_updates[planes_count].gamut_remap_matrix = &dc_plane->gamut_remap_matrix;
6877 fill_dc_scaling_info(new_plane_state,
6878 &bundle->scaling_infos[planes_count]);
6880 bundle->surface_updates[planes_count].scaling_info =
6881 &bundle->scaling_infos[planes_count];
6883 plane_needs_flip = old_plane_state->fb && new_plane_state->fb;
6885 pflip_present = pflip_present || plane_needs_flip;
6887 if (!plane_needs_flip) {
6892 abo = gem_to_amdgpu_bo(fb->obj[0]);
6895 * Wait for all fences on this FB. Do limited wait to avoid
6896 * deadlock during GPU reset when this fence will not signal
6897 * but we hold reservation lock for the BO.
6899 r = dma_resv_wait_timeout_rcu(abo->tbo.base.resv, true,
6901 msecs_to_jiffies(5000));
6902 if (unlikely(r <= 0))
6903 DRM_ERROR("Waiting for fences timed out!");
6906 * TODO This might fail and hence better not used, wait
6907 * explicitly on fences instead
6908 * and in general should be called for
6909 * blocking commit to as per framework helpers
6911 r = amdgpu_bo_reserve(abo, true);
6912 if (unlikely(r != 0))
6913 DRM_ERROR("failed to reserve buffer before flip\n");
6915 amdgpu_bo_get_tiling_flags(abo, &tiling_flags);
6917 tmz_surface = amdgpu_bo_encrypted(abo);
6919 amdgpu_bo_unreserve(abo);
6921 fill_dc_plane_info_and_addr(
6922 dm->adev, new_plane_state, tiling_flags,
6923 &bundle->plane_infos[planes_count],
6924 &bundle->flip_addrs[planes_count].address,
6928 DRM_DEBUG_DRIVER("plane: id=%d dcc_en=%d\n",
6929 new_plane_state->plane->index,
6930 bundle->plane_infos[planes_count].dcc.enable);
6932 bundle->surface_updates[planes_count].plane_info =
6933 &bundle->plane_infos[planes_count];
6936 * Only allow immediate flips for fast updates that don't
6937 * change FB pitch, DCC state, rotation or mirroing.
6939 bundle->flip_addrs[planes_count].flip_immediate =
6940 crtc->state->async_flip &&
6941 acrtc_state->update_type == UPDATE_TYPE_FAST;
6943 timestamp_ns = ktime_get_ns();
6944 bundle->flip_addrs[planes_count].flip_timestamp_in_us = div_u64(timestamp_ns, 1000);
6945 bundle->surface_updates[planes_count].flip_addr = &bundle->flip_addrs[planes_count];
6946 bundle->surface_updates[planes_count].surface = dc_plane;
6948 if (!bundle->surface_updates[planes_count].surface) {
6949 DRM_ERROR("No surface for CRTC: id=%d\n",
6950 acrtc_attach->crtc_id);
6954 if (plane == pcrtc->primary)
6955 update_freesync_state_on_stream(
6958 acrtc_state->stream,
6960 bundle->flip_addrs[planes_count].flip_timestamp_in_us);
6962 DRM_DEBUG_DRIVER("%s Flipping to hi: 0x%x, low: 0x%x\n",
6964 bundle->flip_addrs[planes_count].address.grph.addr.high_part,
6965 bundle->flip_addrs[planes_count].address.grph.addr.low_part);
6971 if (pflip_present) {
6973 /* Use old throttling in non-vrr fixed refresh rate mode
6974 * to keep flip scheduling based on target vblank counts
6975 * working in a backwards compatible way, e.g., for
6976 * clients using the GLX_OML_sync_control extension or
6977 * DRI3/Present extension with defined target_msc.
6979 last_flip_vblank = amdgpu_get_vblank_counter_kms(pcrtc);
6982 /* For variable refresh rate mode only:
6983 * Get vblank of last completed flip to avoid > 1 vrr
6984 * flips per video frame by use of throttling, but allow
6985 * flip programming anywhere in the possibly large
6986 * variable vrr vblank interval for fine-grained flip
6987 * timing control and more opportunity to avoid stutter
6988 * on late submission of flips.
6990 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
6991 last_flip_vblank = acrtc_attach->last_flip_vblank;
6992 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
6995 target_vblank = last_flip_vblank + wait_for_vblank;
6998 * Wait until we're out of the vertical blank period before the one
6999 * targeted by the flip
7001 while ((acrtc_attach->enabled &&
7002 (amdgpu_display_get_crtc_scanoutpos(dm->ddev, acrtc_attach->crtc_id,
7003 0, &vpos, &hpos, NULL,
7004 NULL, &pcrtc->hwmode)
7005 & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
7006 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
7007 (int)(target_vblank -
7008 amdgpu_get_vblank_counter_kms(pcrtc)) > 0)) {
7009 usleep_range(1000, 1100);
7012 if (acrtc_attach->base.state->event) {
7013 drm_crtc_vblank_get(pcrtc);
7015 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
7017 WARN_ON(acrtc_attach->pflip_status != AMDGPU_FLIP_NONE);
7018 prepare_flip_isr(acrtc_attach);
7020 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
7023 if (acrtc_state->stream) {
7024 if (acrtc_state->freesync_vrr_info_changed)
7025 bundle->stream_update.vrr_infopacket =
7026 &acrtc_state->stream->vrr_infopacket;
7030 /* Update the planes if changed or disable if we don't have any. */
7031 if ((planes_count || acrtc_state->active_planes == 0) &&
7032 acrtc_state->stream) {
7033 bundle->stream_update.stream = acrtc_state->stream;
7034 if (new_pcrtc_state->mode_changed) {
7035 bundle->stream_update.src = acrtc_state->stream->src;
7036 bundle->stream_update.dst = acrtc_state->stream->dst;
7039 if (new_pcrtc_state->color_mgmt_changed) {
7041 * TODO: This isn't fully correct since we've actually
7042 * already modified the stream in place.
7044 bundle->stream_update.gamut_remap =
7045 &acrtc_state->stream->gamut_remap_matrix;
7046 bundle->stream_update.output_csc_transform =
7047 &acrtc_state->stream->csc_color_matrix;
7048 bundle->stream_update.out_transfer_func =
7049 acrtc_state->stream->out_transfer_func;
7052 acrtc_state->stream->abm_level = acrtc_state->abm_level;
7053 if (acrtc_state->abm_level != dm_old_crtc_state->abm_level)
7054 bundle->stream_update.abm_level = &acrtc_state->abm_level;
7057 * If FreeSync state on the stream has changed then we need to
7058 * re-adjust the min/max bounds now that DC doesn't handle this
7059 * as part of commit.
7061 if (amdgpu_dm_vrr_active(dm_old_crtc_state) !=
7062 amdgpu_dm_vrr_active(acrtc_state)) {
7063 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
7064 dc_stream_adjust_vmin_vmax(
7065 dm->dc, acrtc_state->stream,
7066 &acrtc_state->vrr_params.adjust);
7067 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
7069 mutex_lock(&dm->dc_lock);
7070 if ((acrtc_state->update_type > UPDATE_TYPE_FAST) &&
7071 acrtc_state->stream->link->psr_settings.psr_allow_active)
7072 amdgpu_dm_psr_disable(acrtc_state->stream);
7074 dc_commit_updates_for_stream(dm->dc,
7075 bundle->surface_updates,
7077 acrtc_state->stream,
7078 &bundle->stream_update,
7081 if ((acrtc_state->update_type > UPDATE_TYPE_FAST) &&
7082 acrtc_state->stream->link->psr_settings.psr_version != DC_PSR_VERSION_UNSUPPORTED &&
7083 !acrtc_state->stream->link->psr_settings.psr_feature_enabled)
7084 amdgpu_dm_link_setup_psr(acrtc_state->stream);
7085 else if ((acrtc_state->update_type == UPDATE_TYPE_FAST) &&
7086 acrtc_state->stream->link->psr_settings.psr_feature_enabled &&
7087 !acrtc_state->stream->link->psr_settings.psr_allow_active) {
7088 amdgpu_dm_psr_enable(acrtc_state->stream);
7091 mutex_unlock(&dm->dc_lock);
7095 * Update cursor state *after* programming all the planes.
7096 * This avoids redundant programming in the case where we're going
7097 * to be disabling a single plane - those pipes are being disabled.
7099 if (acrtc_state->active_planes)
7100 amdgpu_dm_commit_cursors(state);
7106 static void amdgpu_dm_commit_audio(struct drm_device *dev,
7107 struct drm_atomic_state *state)
7109 struct amdgpu_device *adev = dev->dev_private;
7110 struct amdgpu_dm_connector *aconnector;
7111 struct drm_connector *connector;
7112 struct drm_connector_state *old_con_state, *new_con_state;
7113 struct drm_crtc_state *new_crtc_state;
7114 struct dm_crtc_state *new_dm_crtc_state;
7115 const struct dc_stream_status *status;
7118 /* Notify device removals. */
7119 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
7120 if (old_con_state->crtc != new_con_state->crtc) {
7121 /* CRTC changes require notification. */
7125 if (!new_con_state->crtc)
7128 new_crtc_state = drm_atomic_get_new_crtc_state(
7129 state, new_con_state->crtc);
7131 if (!new_crtc_state)
7134 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
7138 aconnector = to_amdgpu_dm_connector(connector);
7140 mutex_lock(&adev->dm.audio_lock);
7141 inst = aconnector->audio_inst;
7142 aconnector->audio_inst = -1;
7143 mutex_unlock(&adev->dm.audio_lock);
7145 amdgpu_dm_audio_eld_notify(adev, inst);
7148 /* Notify audio device additions. */
7149 for_each_new_connector_in_state(state, connector, new_con_state, i) {
7150 if (!new_con_state->crtc)
7153 new_crtc_state = drm_atomic_get_new_crtc_state(
7154 state, new_con_state->crtc);
7156 if (!new_crtc_state)
7159 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
7162 new_dm_crtc_state = to_dm_crtc_state(new_crtc_state);
7163 if (!new_dm_crtc_state->stream)
7166 status = dc_stream_get_status(new_dm_crtc_state->stream);
7170 aconnector = to_amdgpu_dm_connector(connector);
7172 mutex_lock(&adev->dm.audio_lock);
7173 inst = status->audio_inst;
7174 aconnector->audio_inst = inst;
7175 mutex_unlock(&adev->dm.audio_lock);
7177 amdgpu_dm_audio_eld_notify(adev, inst);
7182 * Enable interrupts on CRTCs that are newly active, undergone
7183 * a modeset, or have active planes again.
7185 * Done in two passes, based on the for_modeset flag:
7186 * Pass 1: For CRTCs going through modeset
7187 * Pass 2: For CRTCs going from 0 to n active planes
7189 * Interrupts can only be enabled after the planes are programmed,
7190 * so this requires a two-pass approach since we don't want to
7191 * just defer the interrupts until after commit planes every time.
7193 static void amdgpu_dm_enable_crtc_interrupts(struct drm_device *dev,
7194 struct drm_atomic_state *state,
7197 struct amdgpu_device *adev = dev->dev_private;
7198 struct drm_crtc *crtc;
7199 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
7201 #ifdef CONFIG_DEBUG_FS
7202 enum amdgpu_dm_pipe_crc_source source;
7205 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
7206 new_crtc_state, i) {
7207 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
7208 struct dm_crtc_state *dm_new_crtc_state =
7209 to_dm_crtc_state(new_crtc_state);
7210 struct dm_crtc_state *dm_old_crtc_state =
7211 to_dm_crtc_state(old_crtc_state);
7212 bool modeset = drm_atomic_crtc_needs_modeset(new_crtc_state);
7215 run_pass = (for_modeset && modeset) ||
7216 (!for_modeset && !modeset &&
7217 !dm_old_crtc_state->interrupts_enabled);
7222 if (!dm_new_crtc_state->interrupts_enabled)
7225 manage_dm_interrupts(adev, acrtc, true);
7227 #ifdef CONFIG_DEBUG_FS
7228 /* The stream has changed so CRC capture needs to re-enabled. */
7229 source = dm_new_crtc_state->crc_src;
7230 if (amdgpu_dm_is_valid_crc_source(source)) {
7231 amdgpu_dm_crtc_configure_crc_source(
7232 crtc, dm_new_crtc_state,
7233 dm_new_crtc_state->crc_src);
7240 * amdgpu_dm_crtc_copy_transient_flags - copy mirrored flags from DRM to DC
7241 * @crtc_state: the DRM CRTC state
7242 * @stream_state: the DC stream state.
7244 * Copy the mirrored transient state flags from DRM, to DC. It is used to bring
7245 * a dc_stream_state's flags in sync with a drm_crtc_state's flags.
7247 static void amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state *crtc_state,
7248 struct dc_stream_state *stream_state)
7250 stream_state->mode_changed = drm_atomic_crtc_needs_modeset(crtc_state);
7253 static int amdgpu_dm_atomic_commit(struct drm_device *dev,
7254 struct drm_atomic_state *state,
7257 struct drm_crtc *crtc;
7258 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
7259 struct amdgpu_device *adev = dev->dev_private;
7263 * We evade vblank and pflip interrupts on CRTCs that are undergoing
7264 * a modeset, being disabled, or have no active planes.
7266 * It's done in atomic commit rather than commit tail for now since
7267 * some of these interrupt handlers access the current CRTC state and
7268 * potentially the stream pointer itself.
7270 * Since the atomic state is swapped within atomic commit and not within
7271 * commit tail this would leave to new state (that hasn't been committed yet)
7272 * being accesssed from within the handlers.
7274 * TODO: Fix this so we can do this in commit tail and not have to block
7277 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
7278 struct dm_crtc_state *dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
7279 struct dm_crtc_state *dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
7280 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
7282 if (dm_old_crtc_state->interrupts_enabled &&
7283 (!dm_new_crtc_state->interrupts_enabled ||
7284 drm_atomic_crtc_needs_modeset(new_crtc_state)))
7285 manage_dm_interrupts(adev, acrtc, false);
7288 * Add check here for SoC's that support hardware cursor plane, to
7289 * unset legacy_cursor_update
7292 return drm_atomic_helper_commit(dev, state, nonblock);
7294 /*TODO Handle EINTR, reenable IRQ*/
7298 * amdgpu_dm_atomic_commit_tail() - AMDgpu DM's commit tail implementation.
7299 * @state: The atomic state to commit
7301 * This will tell DC to commit the constructed DC state from atomic_check,
7302 * programming the hardware. Any failures here implies a hardware failure, since
7303 * atomic check should have filtered anything non-kosher.
7305 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state)
7307 struct drm_device *dev = state->dev;
7308 struct amdgpu_device *adev = dev->dev_private;
7309 struct amdgpu_display_manager *dm = &adev->dm;
7310 struct dm_atomic_state *dm_state;
7311 struct dc_state *dc_state = NULL, *dc_state_temp = NULL;
7313 struct drm_crtc *crtc;
7314 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
7315 unsigned long flags;
7316 bool wait_for_vblank = true;
7317 struct drm_connector *connector;
7318 struct drm_connector_state *old_con_state, *new_con_state;
7319 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
7320 int crtc_disable_count = 0;
7322 drm_atomic_helper_update_legacy_modeset_state(dev, state);
7324 dm_state = dm_atomic_get_new_state(state);
7325 if (dm_state && dm_state->context) {
7326 dc_state = dm_state->context;
7328 /* No state changes, retain current state. */
7329 dc_state_temp = dc_create_state(dm->dc);
7330 ASSERT(dc_state_temp);
7331 dc_state = dc_state_temp;
7332 dc_resource_state_copy_construct_current(dm->dc, dc_state);
7335 /* update changed items */
7336 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
7337 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
7339 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
7340 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
7343 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
7344 "planes_changed:%d, mode_changed:%d,active_changed:%d,"
7345 "connectors_changed:%d\n",
7347 new_crtc_state->enable,
7348 new_crtc_state->active,
7349 new_crtc_state->planes_changed,
7350 new_crtc_state->mode_changed,
7351 new_crtc_state->active_changed,
7352 new_crtc_state->connectors_changed);
7354 /* Copy all transient state flags into dc state */
7355 if (dm_new_crtc_state->stream) {
7356 amdgpu_dm_crtc_copy_transient_flags(&dm_new_crtc_state->base,
7357 dm_new_crtc_state->stream);
7360 /* handles headless hotplug case, updating new_state and
7361 * aconnector as needed
7364 if (modeset_required(new_crtc_state, dm_new_crtc_state->stream, dm_old_crtc_state->stream)) {
7366 DRM_DEBUG_DRIVER("Atomic commit: SET crtc id %d: [%p]\n", acrtc->crtc_id, acrtc);
7368 if (!dm_new_crtc_state->stream) {
7370 * this could happen because of issues with
7371 * userspace notifications delivery.
7372 * In this case userspace tries to set mode on
7373 * display which is disconnected in fact.
7374 * dc_sink is NULL in this case on aconnector.
7375 * We expect reset mode will come soon.
7377 * This can also happen when unplug is done
7378 * during resume sequence ended
7380 * In this case, we want to pretend we still
7381 * have a sink to keep the pipe running so that
7382 * hw state is consistent with the sw state
7384 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
7385 __func__, acrtc->base.base.id);
7389 if (dm_old_crtc_state->stream)
7390 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
7392 pm_runtime_get_noresume(dev->dev);
7394 acrtc->enabled = true;
7395 acrtc->hw_mode = new_crtc_state->mode;
7396 crtc->hwmode = new_crtc_state->mode;
7397 } else if (modereset_required(new_crtc_state)) {
7398 DRM_DEBUG_DRIVER("Atomic commit: RESET. crtc id %d:[%p]\n", acrtc->crtc_id, acrtc);
7399 /* i.e. reset mode */
7400 if (dm_old_crtc_state->stream) {
7401 if (dm_old_crtc_state->stream->link->psr_settings.psr_allow_active)
7402 amdgpu_dm_psr_disable(dm_old_crtc_state->stream);
7404 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
7407 } /* for_each_crtc_in_state() */
7410 dm_enable_per_frame_crtc_master_sync(dc_state);
7411 mutex_lock(&dm->dc_lock);
7412 WARN_ON(!dc_commit_state(dm->dc, dc_state));
7413 mutex_unlock(&dm->dc_lock);
7416 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
7417 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
7419 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
7421 if (dm_new_crtc_state->stream != NULL) {
7422 const struct dc_stream_status *status =
7423 dc_stream_get_status(dm_new_crtc_state->stream);
7426 status = dc_stream_get_status_from_state(dc_state,
7427 dm_new_crtc_state->stream);
7430 DC_ERR("got no status for stream %p on acrtc%p\n", dm_new_crtc_state->stream, acrtc);
7432 acrtc->otg_inst = status->primary_otg_inst;
7435 #ifdef CONFIG_DRM_AMD_DC_HDCP
7436 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
7437 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
7438 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
7439 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
7441 new_crtc_state = NULL;
7444 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
7446 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
7448 if (dm_new_crtc_state && dm_new_crtc_state->stream == NULL &&
7449 connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
7450 hdcp_reset_display(adev->dm.hdcp_workqueue, aconnector->dc_link->link_index);
7451 new_con_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
7455 if (is_content_protection_different(new_con_state, old_con_state, connector, adev->dm.hdcp_workqueue))
7456 hdcp_update_display(
7457 adev->dm.hdcp_workqueue, aconnector->dc_link->link_index, aconnector,
7458 new_con_state->hdcp_content_type,
7459 new_con_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED ? true
7464 /* Handle connector state changes */
7465 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
7466 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
7467 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
7468 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
7469 struct dc_surface_update dummy_updates[MAX_SURFACES];
7470 struct dc_stream_update stream_update;
7471 struct dc_info_packet hdr_packet;
7472 struct dc_stream_status *status = NULL;
7473 bool abm_changed, hdr_changed, scaling_changed;
7475 memset(&dummy_updates, 0, sizeof(dummy_updates));
7476 memset(&stream_update, 0, sizeof(stream_update));
7479 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
7480 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
7483 /* Skip any modesets/resets */
7484 if (!acrtc || drm_atomic_crtc_needs_modeset(new_crtc_state))
7487 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
7488 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
7490 scaling_changed = is_scaling_state_different(dm_new_con_state,
7493 abm_changed = dm_new_crtc_state->abm_level !=
7494 dm_old_crtc_state->abm_level;
7497 is_hdr_metadata_different(old_con_state, new_con_state);
7499 if (!scaling_changed && !abm_changed && !hdr_changed)
7502 stream_update.stream = dm_new_crtc_state->stream;
7503 if (scaling_changed) {
7504 update_stream_scaling_settings(&dm_new_con_state->base.crtc->mode,
7505 dm_new_con_state, dm_new_crtc_state->stream);
7507 stream_update.src = dm_new_crtc_state->stream->src;
7508 stream_update.dst = dm_new_crtc_state->stream->dst;
7512 dm_new_crtc_state->stream->abm_level = dm_new_crtc_state->abm_level;
7514 stream_update.abm_level = &dm_new_crtc_state->abm_level;
7518 fill_hdr_info_packet(new_con_state, &hdr_packet);
7519 stream_update.hdr_static_metadata = &hdr_packet;
7522 status = dc_stream_get_status(dm_new_crtc_state->stream);
7524 WARN_ON(!status->plane_count);
7527 * TODO: DC refuses to perform stream updates without a dc_surface_update.
7528 * Here we create an empty update on each plane.
7529 * To fix this, DC should permit updating only stream properties.
7531 for (j = 0; j < status->plane_count; j++)
7532 dummy_updates[j].surface = status->plane_states[0];
7535 mutex_lock(&dm->dc_lock);
7536 dc_commit_updates_for_stream(dm->dc,
7538 status->plane_count,
7539 dm_new_crtc_state->stream,
7542 mutex_unlock(&dm->dc_lock);
7545 /* Count number of newly disabled CRTCs for dropping PM refs later. */
7546 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
7547 new_crtc_state, i) {
7548 if (old_crtc_state->active && !new_crtc_state->active)
7549 crtc_disable_count++;
7551 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
7552 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
7554 /* Update freesync active state. */
7555 pre_update_freesync_state_on_stream(dm, dm_new_crtc_state);
7557 /* Handle vrr on->off / off->on transitions */
7558 amdgpu_dm_handle_vrr_transition(dm_old_crtc_state,
7562 /* Enable interrupts for CRTCs going through a modeset. */
7563 amdgpu_dm_enable_crtc_interrupts(dev, state, true);
7565 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j)
7566 if (new_crtc_state->async_flip)
7567 wait_for_vblank = false;
7569 /* update planes when needed per crtc*/
7570 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j) {
7571 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
7573 if (dm_new_crtc_state->stream)
7574 amdgpu_dm_commit_planes(state, dc_state, dev,
7575 dm, crtc, wait_for_vblank);
7578 /* Enable interrupts for CRTCs going from 0 to n active planes. */
7579 amdgpu_dm_enable_crtc_interrupts(dev, state, false);
7581 /* Update audio instances for each connector. */
7582 amdgpu_dm_commit_audio(dev, state);
7585 * send vblank event on all events not handled in flip and
7586 * mark consumed event for drm_atomic_helper_commit_hw_done
7588 spin_lock_irqsave(&adev->ddev->event_lock, flags);
7589 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
7591 if (new_crtc_state->event)
7592 drm_send_event_locked(dev, &new_crtc_state->event->base);
7594 new_crtc_state->event = NULL;
7596 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
7598 /* Signal HW programming completion */
7599 drm_atomic_helper_commit_hw_done(state);
7601 if (wait_for_vblank)
7602 drm_atomic_helper_wait_for_flip_done(dev, state);
7604 drm_atomic_helper_cleanup_planes(dev, state);
7607 * Finally, drop a runtime PM reference for each newly disabled CRTC,
7608 * so we can put the GPU into runtime suspend if we're not driving any
7611 for (i = 0; i < crtc_disable_count; i++)
7612 pm_runtime_put_autosuspend(dev->dev);
7613 pm_runtime_mark_last_busy(dev->dev);
7616 dc_release_state(dc_state_temp);
7620 static int dm_force_atomic_commit(struct drm_connector *connector)
7623 struct drm_device *ddev = connector->dev;
7624 struct drm_atomic_state *state = drm_atomic_state_alloc(ddev);
7625 struct amdgpu_crtc *disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
7626 struct drm_plane *plane = disconnected_acrtc->base.primary;
7627 struct drm_connector_state *conn_state;
7628 struct drm_crtc_state *crtc_state;
7629 struct drm_plane_state *plane_state;
7634 state->acquire_ctx = ddev->mode_config.acquire_ctx;
7636 /* Construct an atomic state to restore previous display setting */
7639 * Attach connectors to drm_atomic_state
7641 conn_state = drm_atomic_get_connector_state(state, connector);
7643 ret = PTR_ERR_OR_ZERO(conn_state);
7647 /* Attach crtc to drm_atomic_state*/
7648 crtc_state = drm_atomic_get_crtc_state(state, &disconnected_acrtc->base);
7650 ret = PTR_ERR_OR_ZERO(crtc_state);
7654 /* force a restore */
7655 crtc_state->mode_changed = true;
7657 /* Attach plane to drm_atomic_state */
7658 plane_state = drm_atomic_get_plane_state(state, plane);
7660 ret = PTR_ERR_OR_ZERO(plane_state);
7665 /* Call commit internally with the state we just constructed */
7666 ret = drm_atomic_commit(state);
7671 DRM_ERROR("Restoring old state failed with %i\n", ret);
7672 drm_atomic_state_put(state);
7678 * This function handles all cases when set mode does not come upon hotplug.
7679 * This includes when a display is unplugged then plugged back into the
7680 * same port and when running without usermode desktop manager supprot
7682 void dm_restore_drm_connector_state(struct drm_device *dev,
7683 struct drm_connector *connector)
7685 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
7686 struct amdgpu_crtc *disconnected_acrtc;
7687 struct dm_crtc_state *acrtc_state;
7689 if (!aconnector->dc_sink || !connector->state || !connector->encoder)
7692 disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
7693 if (!disconnected_acrtc)
7696 acrtc_state = to_dm_crtc_state(disconnected_acrtc->base.state);
7697 if (!acrtc_state->stream)
7701 * If the previous sink is not released and different from the current,
7702 * we deduce we are in a state where we can not rely on usermode call
7703 * to turn on the display, so we do it here
7705 if (acrtc_state->stream->sink != aconnector->dc_sink)
7706 dm_force_atomic_commit(&aconnector->base);
7710 * Grabs all modesetting locks to serialize against any blocking commits,
7711 * Waits for completion of all non blocking commits.
7713 static int do_aquire_global_lock(struct drm_device *dev,
7714 struct drm_atomic_state *state)
7716 struct drm_crtc *crtc;
7717 struct drm_crtc_commit *commit;
7721 * Adding all modeset locks to aquire_ctx will
7722 * ensure that when the framework release it the
7723 * extra locks we are locking here will get released to
7725 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
7729 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7730 spin_lock(&crtc->commit_lock);
7731 commit = list_first_entry_or_null(&crtc->commit_list,
7732 struct drm_crtc_commit, commit_entry);
7734 drm_crtc_commit_get(commit);
7735 spin_unlock(&crtc->commit_lock);
7741 * Make sure all pending HW programming completed and
7744 ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ);
7747 ret = wait_for_completion_interruptible_timeout(
7748 &commit->flip_done, 10*HZ);
7751 DRM_ERROR("[CRTC:%d:%s] hw_done or flip_done "
7752 "timed out\n", crtc->base.id, crtc->name);
7754 drm_crtc_commit_put(commit);
7757 return ret < 0 ? ret : 0;
7760 static void get_freesync_config_for_crtc(
7761 struct dm_crtc_state *new_crtc_state,
7762 struct dm_connector_state *new_con_state)
7764 struct mod_freesync_config config = {0};
7765 struct amdgpu_dm_connector *aconnector =
7766 to_amdgpu_dm_connector(new_con_state->base.connector);
7767 struct drm_display_mode *mode = &new_crtc_state->base.mode;
7768 int vrefresh = drm_mode_vrefresh(mode);
7770 new_crtc_state->vrr_supported = new_con_state->freesync_capable &&
7771 vrefresh >= aconnector->min_vfreq &&
7772 vrefresh <= aconnector->max_vfreq;
7774 if (new_crtc_state->vrr_supported) {
7775 new_crtc_state->stream->ignore_msa_timing_param = true;
7776 config.state = new_crtc_state->base.vrr_enabled ?
7777 VRR_STATE_ACTIVE_VARIABLE :
7779 config.min_refresh_in_uhz =
7780 aconnector->min_vfreq * 1000000;
7781 config.max_refresh_in_uhz =
7782 aconnector->max_vfreq * 1000000;
7783 config.vsif_supported = true;
7787 new_crtc_state->freesync_config = config;
7790 static void reset_freesync_config_for_crtc(
7791 struct dm_crtc_state *new_crtc_state)
7793 new_crtc_state->vrr_supported = false;
7795 memset(&new_crtc_state->vrr_params, 0,
7796 sizeof(new_crtc_state->vrr_params));
7797 memset(&new_crtc_state->vrr_infopacket, 0,
7798 sizeof(new_crtc_state->vrr_infopacket));
7801 static int dm_update_crtc_state(struct amdgpu_display_manager *dm,
7802 struct drm_atomic_state *state,
7803 struct drm_crtc *crtc,
7804 struct drm_crtc_state *old_crtc_state,
7805 struct drm_crtc_state *new_crtc_state,
7807 bool *lock_and_validation_needed)
7809 struct dm_atomic_state *dm_state = NULL;
7810 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
7811 struct dc_stream_state *new_stream;
7815 * TODO Move this code into dm_crtc_atomic_check once we get rid of dc_validation_set
7816 * update changed items
7818 struct amdgpu_crtc *acrtc = NULL;
7819 struct amdgpu_dm_connector *aconnector = NULL;
7820 struct drm_connector_state *drm_new_conn_state = NULL, *drm_old_conn_state = NULL;
7821 struct dm_connector_state *dm_new_conn_state = NULL, *dm_old_conn_state = NULL;
7825 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
7826 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
7827 acrtc = to_amdgpu_crtc(crtc);
7828 aconnector = amdgpu_dm_find_first_crtc_matching_connector(state, crtc);
7830 /* TODO This hack should go away */
7831 if (aconnector && enable) {
7832 /* Make sure fake sink is created in plug-in scenario */
7833 drm_new_conn_state = drm_atomic_get_new_connector_state(state,
7835 drm_old_conn_state = drm_atomic_get_old_connector_state(state,
7838 if (IS_ERR(drm_new_conn_state)) {
7839 ret = PTR_ERR_OR_ZERO(drm_new_conn_state);
7843 dm_new_conn_state = to_dm_connector_state(drm_new_conn_state);
7844 dm_old_conn_state = to_dm_connector_state(drm_old_conn_state);
7846 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
7849 new_stream = create_validate_stream_for_sink(aconnector,
7850 &new_crtc_state->mode,
7852 dm_old_crtc_state->stream);
7855 * we can have no stream on ACTION_SET if a display
7856 * was disconnected during S3, in this case it is not an
7857 * error, the OS will be updated after detection, and
7858 * will do the right thing on next atomic commit
7862 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
7863 __func__, acrtc->base.base.id);
7868 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
7870 ret = fill_hdr_info_packet(drm_new_conn_state,
7871 &new_stream->hdr_static_metadata);
7876 * If we already removed the old stream from the context
7877 * (and set the new stream to NULL) then we can't reuse
7878 * the old stream even if the stream and scaling are unchanged.
7879 * We'll hit the BUG_ON and black screen.
7881 * TODO: Refactor this function to allow this check to work
7882 * in all conditions.
7884 if (dm_new_crtc_state->stream &&
7885 dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) &&
7886 dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream)) {
7887 new_crtc_state->mode_changed = false;
7888 DRM_DEBUG_DRIVER("Mode change not required, setting mode_changed to %d",
7889 new_crtc_state->mode_changed);
7893 /* mode_changed flag may get updated above, need to check again */
7894 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
7898 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
7899 "planes_changed:%d, mode_changed:%d,active_changed:%d,"
7900 "connectors_changed:%d\n",
7902 new_crtc_state->enable,
7903 new_crtc_state->active,
7904 new_crtc_state->planes_changed,
7905 new_crtc_state->mode_changed,
7906 new_crtc_state->active_changed,
7907 new_crtc_state->connectors_changed);
7909 /* Remove stream for any changed/disabled CRTC */
7912 if (!dm_old_crtc_state->stream)
7915 ret = dm_atomic_get_state(state, &dm_state);
7919 DRM_DEBUG_DRIVER("Disabling DRM crtc: %d\n",
7922 /* i.e. reset mode */
7923 if (dc_remove_stream_from_ctx(
7926 dm_old_crtc_state->stream) != DC_OK) {
7931 dc_stream_release(dm_old_crtc_state->stream);
7932 dm_new_crtc_state->stream = NULL;
7934 reset_freesync_config_for_crtc(dm_new_crtc_state);
7936 *lock_and_validation_needed = true;
7938 } else {/* Add stream for any updated/enabled CRTC */
7940 * Quick fix to prevent NULL pointer on new_stream when
7941 * added MST connectors not found in existing crtc_state in the chained mode
7942 * TODO: need to dig out the root cause of that
7944 if (!aconnector || (!aconnector->dc_sink && aconnector->mst_port))
7947 if (modereset_required(new_crtc_state))
7950 if (modeset_required(new_crtc_state, new_stream,
7951 dm_old_crtc_state->stream)) {
7953 WARN_ON(dm_new_crtc_state->stream);
7955 ret = dm_atomic_get_state(state, &dm_state);
7959 dm_new_crtc_state->stream = new_stream;
7961 dc_stream_retain(new_stream);
7963 DRM_DEBUG_DRIVER("Enabling DRM crtc: %d\n",
7966 if (dc_add_stream_to_ctx(
7969 dm_new_crtc_state->stream) != DC_OK) {
7974 *lock_and_validation_needed = true;
7979 /* Release extra reference */
7981 dc_stream_release(new_stream);
7984 * We want to do dc stream updates that do not require a
7985 * full modeset below.
7987 if (!(enable && aconnector && new_crtc_state->enable &&
7988 new_crtc_state->active))
7991 * Given above conditions, the dc state cannot be NULL because:
7992 * 1. We're in the process of enabling CRTCs (just been added
7993 * to the dc context, or already is on the context)
7994 * 2. Has a valid connector attached, and
7995 * 3. Is currently active and enabled.
7996 * => The dc stream state currently exists.
7998 BUG_ON(dm_new_crtc_state->stream == NULL);
8000 /* Scaling or underscan settings */
8001 if (is_scaling_state_different(dm_old_conn_state, dm_new_conn_state))
8002 update_stream_scaling_settings(
8003 &new_crtc_state->mode, dm_new_conn_state, dm_new_crtc_state->stream);
8006 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
8009 * Color management settings. We also update color properties
8010 * when a modeset is needed, to ensure it gets reprogrammed.
8012 if (dm_new_crtc_state->base.color_mgmt_changed ||
8013 drm_atomic_crtc_needs_modeset(new_crtc_state)) {
8014 ret = amdgpu_dm_update_crtc_color_mgmt(dm_new_crtc_state);
8019 /* Update Freesync settings. */
8020 get_freesync_config_for_crtc(dm_new_crtc_state,
8027 dc_stream_release(new_stream);
8031 static bool should_reset_plane(struct drm_atomic_state *state,
8032 struct drm_plane *plane,
8033 struct drm_plane_state *old_plane_state,
8034 struct drm_plane_state *new_plane_state)
8036 struct drm_plane *other;
8037 struct drm_plane_state *old_other_state, *new_other_state;
8038 struct drm_crtc_state *new_crtc_state;
8042 * TODO: Remove this hack once the checks below are sufficient
8043 * enough to determine when we need to reset all the planes on
8046 if (state->allow_modeset)
8049 /* Exit early if we know that we're adding or removing the plane. */
8050 if (old_plane_state->crtc != new_plane_state->crtc)
8053 /* old crtc == new_crtc == NULL, plane not in context. */
8054 if (!new_plane_state->crtc)
8058 drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
8060 if (!new_crtc_state)
8063 /* CRTC Degamma changes currently require us to recreate planes. */
8064 if (new_crtc_state->color_mgmt_changed)
8067 if (drm_atomic_crtc_needs_modeset(new_crtc_state))
8071 * If there are any new primary or overlay planes being added or
8072 * removed then the z-order can potentially change. To ensure
8073 * correct z-order and pipe acquisition the current DC architecture
8074 * requires us to remove and recreate all existing planes.
8076 * TODO: Come up with a more elegant solution for this.
8078 for_each_oldnew_plane_in_state(state, other, old_other_state, new_other_state, i) {
8079 if (other->type == DRM_PLANE_TYPE_CURSOR)
8082 if (old_other_state->crtc != new_plane_state->crtc &&
8083 new_other_state->crtc != new_plane_state->crtc)
8086 if (old_other_state->crtc != new_other_state->crtc)
8089 /* TODO: Remove this once we can handle fast format changes. */
8090 if (old_other_state->fb && new_other_state->fb &&
8091 old_other_state->fb->format != new_other_state->fb->format)
8098 static int dm_update_plane_state(struct dc *dc,
8099 struct drm_atomic_state *state,
8100 struct drm_plane *plane,
8101 struct drm_plane_state *old_plane_state,
8102 struct drm_plane_state *new_plane_state,
8104 bool *lock_and_validation_needed)
8107 struct dm_atomic_state *dm_state = NULL;
8108 struct drm_crtc *new_plane_crtc, *old_plane_crtc;
8109 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
8110 struct dm_crtc_state *dm_new_crtc_state, *dm_old_crtc_state;
8111 struct dm_plane_state *dm_new_plane_state, *dm_old_plane_state;
8112 struct amdgpu_crtc *new_acrtc;
8117 new_plane_crtc = new_plane_state->crtc;
8118 old_plane_crtc = old_plane_state->crtc;
8119 dm_new_plane_state = to_dm_plane_state(new_plane_state);
8120 dm_old_plane_state = to_dm_plane_state(old_plane_state);
8122 /*TODO Implement better atomic check for cursor plane */
8123 if (plane->type == DRM_PLANE_TYPE_CURSOR) {
8124 if (!enable || !new_plane_crtc ||
8125 drm_atomic_plane_disabling(plane->state, new_plane_state))
8128 new_acrtc = to_amdgpu_crtc(new_plane_crtc);
8130 if ((new_plane_state->crtc_w > new_acrtc->max_cursor_width) ||
8131 (new_plane_state->crtc_h > new_acrtc->max_cursor_height)) {
8132 DRM_DEBUG_ATOMIC("Bad cursor size %d x %d\n",
8133 new_plane_state->crtc_w, new_plane_state->crtc_h);
8140 needs_reset = should_reset_plane(state, plane, old_plane_state,
8143 /* Remove any changed/removed planes */
8148 if (!old_plane_crtc)
8151 old_crtc_state = drm_atomic_get_old_crtc_state(
8152 state, old_plane_crtc);
8153 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
8155 if (!dm_old_crtc_state->stream)
8158 DRM_DEBUG_ATOMIC("Disabling DRM plane: %d on DRM crtc %d\n",
8159 plane->base.id, old_plane_crtc->base.id);
8161 ret = dm_atomic_get_state(state, &dm_state);
8165 if (!dc_remove_plane_from_context(
8167 dm_old_crtc_state->stream,
8168 dm_old_plane_state->dc_state,
8169 dm_state->context)) {
8176 dc_plane_state_release(dm_old_plane_state->dc_state);
8177 dm_new_plane_state->dc_state = NULL;
8179 *lock_and_validation_needed = true;
8181 } else { /* Add new planes */
8182 struct dc_plane_state *dc_new_plane_state;
8184 if (drm_atomic_plane_disabling(plane->state, new_plane_state))
8187 if (!new_plane_crtc)
8190 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_crtc);
8191 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
8193 if (!dm_new_crtc_state->stream)
8199 WARN_ON(dm_new_plane_state->dc_state);
8201 dc_new_plane_state = dc_create_plane_state(dc);
8202 if (!dc_new_plane_state)
8205 DRM_DEBUG_DRIVER("Enabling DRM plane: %d on DRM crtc %d\n",
8206 plane->base.id, new_plane_crtc->base.id);
8208 ret = fill_dc_plane_attributes(
8209 new_plane_crtc->dev->dev_private,
8214 dc_plane_state_release(dc_new_plane_state);
8218 ret = dm_atomic_get_state(state, &dm_state);
8220 dc_plane_state_release(dc_new_plane_state);
8225 * Any atomic check errors that occur after this will
8226 * not need a release. The plane state will be attached
8227 * to the stream, and therefore part of the atomic
8228 * state. It'll be released when the atomic state is
8231 if (!dc_add_plane_to_context(
8233 dm_new_crtc_state->stream,
8235 dm_state->context)) {
8237 dc_plane_state_release(dc_new_plane_state);
8241 dm_new_plane_state->dc_state = dc_new_plane_state;
8243 /* Tell DC to do a full surface update every time there
8244 * is a plane change. Inefficient, but works for now.
8246 dm_new_plane_state->dc_state->update_flags.bits.full_update = 1;
8248 *lock_and_validation_needed = true;
8256 dm_determine_update_type_for_commit(struct amdgpu_display_manager *dm,
8257 struct drm_atomic_state *state,
8258 enum surface_update_type *out_type)
8260 struct dc *dc = dm->dc;
8261 struct dm_atomic_state *dm_state = NULL, *old_dm_state = NULL;
8262 int i, j, num_plane, ret = 0;
8263 struct drm_plane_state *old_plane_state, *new_plane_state;
8264 struct dm_plane_state *new_dm_plane_state, *old_dm_plane_state;
8265 struct drm_crtc *new_plane_crtc;
8266 struct drm_plane *plane;
8268 struct drm_crtc *crtc;
8269 struct drm_crtc_state *new_crtc_state, *old_crtc_state;
8270 struct dm_crtc_state *new_dm_crtc_state, *old_dm_crtc_state;
8271 struct dc_stream_status *status = NULL;
8272 enum surface_update_type update_type = UPDATE_TYPE_FAST;
8273 struct surface_info_bundle {
8274 struct dc_surface_update surface_updates[MAX_SURFACES];
8275 struct dc_plane_info plane_infos[MAX_SURFACES];
8276 struct dc_scaling_info scaling_infos[MAX_SURFACES];
8277 struct dc_flip_addrs flip_addrs[MAX_SURFACES];
8278 struct dc_stream_update stream_update;
8281 bundle = kzalloc(sizeof(*bundle), GFP_KERNEL);
8284 DRM_ERROR("Failed to allocate update bundle\n");
8285 /* Set type to FULL to avoid crashing in DC*/
8286 update_type = UPDATE_TYPE_FULL;
8290 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
8292 memset(bundle, 0, sizeof(struct surface_info_bundle));
8294 new_dm_crtc_state = to_dm_crtc_state(new_crtc_state);
8295 old_dm_crtc_state = to_dm_crtc_state(old_crtc_state);
8298 if (new_dm_crtc_state->stream != old_dm_crtc_state->stream) {
8299 update_type = UPDATE_TYPE_FULL;
8303 if (!new_dm_crtc_state->stream)
8306 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, j) {
8307 const struct amdgpu_framebuffer *amdgpu_fb =
8308 to_amdgpu_framebuffer(new_plane_state->fb);
8309 struct dc_plane_info *plane_info = &bundle->plane_infos[num_plane];
8310 struct dc_flip_addrs *flip_addr = &bundle->flip_addrs[num_plane];
8311 struct dc_scaling_info *scaling_info = &bundle->scaling_infos[num_plane];
8312 uint64_t tiling_flags;
8313 bool tmz_surface = false;
8315 new_plane_crtc = new_plane_state->crtc;
8316 new_dm_plane_state = to_dm_plane_state(new_plane_state);
8317 old_dm_plane_state = to_dm_plane_state(old_plane_state);
8319 if (plane->type == DRM_PLANE_TYPE_CURSOR)
8322 if (new_dm_plane_state->dc_state != old_dm_plane_state->dc_state) {
8323 update_type = UPDATE_TYPE_FULL;
8327 if (crtc != new_plane_crtc)
8330 bundle->surface_updates[num_plane].surface =
8331 new_dm_plane_state->dc_state;
8333 if (new_crtc_state->mode_changed) {
8334 bundle->stream_update.dst = new_dm_crtc_state->stream->dst;
8335 bundle->stream_update.src = new_dm_crtc_state->stream->src;
8338 if (new_crtc_state->color_mgmt_changed) {
8339 bundle->surface_updates[num_plane].gamma =
8340 new_dm_plane_state->dc_state->gamma_correction;
8341 bundle->surface_updates[num_plane].in_transfer_func =
8342 new_dm_plane_state->dc_state->in_transfer_func;
8343 bundle->surface_updates[num_plane].gamut_remap_matrix =
8344 &new_dm_plane_state->dc_state->gamut_remap_matrix;
8345 bundle->stream_update.gamut_remap =
8346 &new_dm_crtc_state->stream->gamut_remap_matrix;
8347 bundle->stream_update.output_csc_transform =
8348 &new_dm_crtc_state->stream->csc_color_matrix;
8349 bundle->stream_update.out_transfer_func =
8350 new_dm_crtc_state->stream->out_transfer_func;
8353 ret = fill_dc_scaling_info(new_plane_state,
8358 bundle->surface_updates[num_plane].scaling_info = scaling_info;
8361 ret = get_fb_info(amdgpu_fb, &tiling_flags, &tmz_surface);
8365 ret = fill_dc_plane_info_and_addr(
8366 dm->adev, new_plane_state, tiling_flags,
8368 &flip_addr->address, tmz_surface,
8373 bundle->surface_updates[num_plane].plane_info = plane_info;
8374 bundle->surface_updates[num_plane].flip_addr = flip_addr;
8383 ret = dm_atomic_get_state(state, &dm_state);
8387 old_dm_state = dm_atomic_get_old_state(state);
8388 if (!old_dm_state) {
8393 status = dc_stream_get_status_from_state(old_dm_state->context,
8394 new_dm_crtc_state->stream);
8395 bundle->stream_update.stream = new_dm_crtc_state->stream;
8397 * TODO: DC modifies the surface during this call so we need
8398 * to lock here - find a way to do this without locking.
8400 mutex_lock(&dm->dc_lock);
8401 update_type = dc_check_update_surfaces_for_stream(
8402 dc, bundle->surface_updates, num_plane,
8403 &bundle->stream_update, status);
8404 mutex_unlock(&dm->dc_lock);
8406 if (update_type > UPDATE_TYPE_MED) {
8407 update_type = UPDATE_TYPE_FULL;
8415 *out_type = update_type;
8419 static int add_affected_mst_dsc_crtcs(struct drm_atomic_state *state, struct drm_crtc *crtc)
8421 struct drm_connector *connector;
8422 struct drm_connector_state *conn_state;
8423 struct amdgpu_dm_connector *aconnector = NULL;
8425 for_each_new_connector_in_state(state, connector, conn_state, i) {
8426 if (conn_state->crtc != crtc)
8429 aconnector = to_amdgpu_dm_connector(connector);
8430 if (!aconnector->port || !aconnector->mst_port)
8439 return drm_dp_mst_add_affected_dsc_crtcs(state, &aconnector->mst_port->mst_mgr);
8443 * amdgpu_dm_atomic_check() - Atomic check implementation for AMDgpu DM.
8444 * @dev: The DRM device
8445 * @state: The atomic state to commit
8447 * Validate that the given atomic state is programmable by DC into hardware.
8448 * This involves constructing a &struct dc_state reflecting the new hardware
8449 * state we wish to commit, then querying DC to see if it is programmable. It's
8450 * important not to modify the existing DC state. Otherwise, atomic_check
8451 * may unexpectedly commit hardware changes.
8453 * When validating the DC state, it's important that the right locks are
8454 * acquired. For full updates case which removes/adds/updates streams on one
8455 * CRTC while flipping on another CRTC, acquiring global lock will guarantee
8456 * that any such full update commit will wait for completion of any outstanding
8457 * flip using DRMs synchronization events. See
8458 * dm_determine_update_type_for_commit()
8460 * Note that DM adds the affected connectors for all CRTCs in state, when that
8461 * might not seem necessary. This is because DC stream creation requires the
8462 * DC sink, which is tied to the DRM connector state. Cleaning this up should
8463 * be possible but non-trivial - a possible TODO item.
8465 * Return: -Error code if validation failed.
8467 static int amdgpu_dm_atomic_check(struct drm_device *dev,
8468 struct drm_atomic_state *state)
8470 struct amdgpu_device *adev = dev->dev_private;
8471 struct dm_atomic_state *dm_state = NULL;
8472 struct dc *dc = adev->dm.dc;
8473 struct drm_connector *connector;
8474 struct drm_connector_state *old_con_state, *new_con_state;
8475 struct drm_crtc *crtc;
8476 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
8477 struct drm_plane *plane;
8478 struct drm_plane_state *old_plane_state, *new_plane_state;
8479 enum surface_update_type update_type = UPDATE_TYPE_FAST;
8480 enum surface_update_type overall_update_type = UPDATE_TYPE_FAST;
8485 * This bool will be set for true for any modeset/reset
8486 * or plane update which implies non fast surface update.
8488 bool lock_and_validation_needed = false;
8490 ret = drm_atomic_helper_check_modeset(dev, state);
8494 if (adev->asic_type >= CHIP_NAVI10) {
8495 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
8496 if (drm_atomic_crtc_needs_modeset(new_crtc_state)) {
8497 ret = add_affected_mst_dsc_crtcs(state, crtc);
8504 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
8505 if (!drm_atomic_crtc_needs_modeset(new_crtc_state) &&
8506 !new_crtc_state->color_mgmt_changed &&
8507 old_crtc_state->vrr_enabled == new_crtc_state->vrr_enabled)
8510 if (!new_crtc_state->enable)
8513 ret = drm_atomic_add_affected_connectors(state, crtc);
8517 ret = drm_atomic_add_affected_planes(state, crtc);
8523 * Add all primary and overlay planes on the CRTC to the state
8524 * whenever a plane is enabled to maintain correct z-ordering
8525 * and to enable fast surface updates.
8527 drm_for_each_crtc(crtc, dev) {
8528 bool modified = false;
8530 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
8531 if (plane->type == DRM_PLANE_TYPE_CURSOR)
8534 if (new_plane_state->crtc == crtc ||
8535 old_plane_state->crtc == crtc) {
8544 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
8545 if (plane->type == DRM_PLANE_TYPE_CURSOR)
8549 drm_atomic_get_plane_state(state, plane);
8551 if (IS_ERR(new_plane_state)) {
8552 ret = PTR_ERR(new_plane_state);
8558 /* Remove exiting planes if they are modified */
8559 for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) {
8560 ret = dm_update_plane_state(dc, state, plane,
8564 &lock_and_validation_needed);
8569 /* Disable all crtcs which require disable */
8570 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
8571 ret = dm_update_crtc_state(&adev->dm, state, crtc,
8575 &lock_and_validation_needed);
8580 /* Enable all crtcs which require enable */
8581 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
8582 ret = dm_update_crtc_state(&adev->dm, state, crtc,
8586 &lock_and_validation_needed);
8591 /* Add new/modified planes */
8592 for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) {
8593 ret = dm_update_plane_state(dc, state, plane,
8597 &lock_and_validation_needed);
8602 /* Run this here since we want to validate the streams we created */
8603 ret = drm_atomic_helper_check_planes(dev, state);
8607 if (state->legacy_cursor_update) {
8609 * This is a fast cursor update coming from the plane update
8610 * helper, check if it can be done asynchronously for better
8613 state->async_update =
8614 !drm_atomic_helper_async_check(dev, state);
8617 * Skip the remaining global validation if this is an async
8618 * update. Cursor updates can be done without affecting
8619 * state or bandwidth calcs and this avoids the performance
8620 * penalty of locking the private state object and
8621 * allocating a new dc_state.
8623 if (state->async_update)
8627 /* Check scaling and underscan changes*/
8628 /* TODO Removed scaling changes validation due to inability to commit
8629 * new stream into context w\o causing full reset. Need to
8630 * decide how to handle.
8632 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
8633 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
8634 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
8635 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
8637 /* Skip any modesets/resets */
8638 if (!acrtc || drm_atomic_crtc_needs_modeset(
8639 drm_atomic_get_new_crtc_state(state, &acrtc->base)))
8642 /* Skip any thing not scale or underscan changes */
8643 if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state))
8646 overall_update_type = UPDATE_TYPE_FULL;
8647 lock_and_validation_needed = true;
8650 ret = dm_determine_update_type_for_commit(&adev->dm, state, &update_type);
8654 if (overall_update_type < update_type)
8655 overall_update_type = update_type;
8658 * lock_and_validation_needed was an old way to determine if we need to set
8659 * the global lock. Leaving it in to check if we broke any corner cases
8660 * lock_and_validation_needed true = UPDATE_TYPE_FULL or UPDATE_TYPE_MED
8661 * lock_and_validation_needed false = UPDATE_TYPE_FAST
8663 if (lock_and_validation_needed && overall_update_type <= UPDATE_TYPE_FAST)
8664 WARN(1, "Global lock should be Set, overall_update_type should be UPDATE_TYPE_MED or UPDATE_TYPE_FULL");
8666 if (overall_update_type > UPDATE_TYPE_FAST) {
8667 ret = dm_atomic_get_state(state, &dm_state);
8671 ret = do_aquire_global_lock(dev, state);
8675 #if defined(CONFIG_DRM_AMD_DC_DCN)
8676 if (!compute_mst_dsc_configs_for_state(state, dm_state->context))
8679 ret = dm_update_mst_vcpi_slots_for_dsc(state, dm_state->context);
8685 * Perform validation of MST topology in the state:
8686 * We need to perform MST atomic check before calling
8687 * dc_validate_global_state(), or there is a chance
8688 * to get stuck in an infinite loop and hang eventually.
8690 ret = drm_dp_mst_atomic_check(state);
8694 if (dc_validate_global_state(dc, dm_state->context, false) != DC_OK) {
8700 * The commit is a fast update. Fast updates shouldn't change
8701 * the DC context, affect global validation, and can have their
8702 * commit work done in parallel with other commits not touching
8703 * the same resource. If we have a new DC context as part of
8704 * the DM atomic state from validation we need to free it and
8705 * retain the existing one instead.
8707 struct dm_atomic_state *new_dm_state, *old_dm_state;
8709 new_dm_state = dm_atomic_get_new_state(state);
8710 old_dm_state = dm_atomic_get_old_state(state);
8712 if (new_dm_state && old_dm_state) {
8713 if (new_dm_state->context)
8714 dc_release_state(new_dm_state->context);
8716 new_dm_state->context = old_dm_state->context;
8718 if (old_dm_state->context)
8719 dc_retain_state(old_dm_state->context);
8723 /* Store the overall update type for use later in atomic check. */
8724 for_each_new_crtc_in_state (state, crtc, new_crtc_state, i) {
8725 struct dm_crtc_state *dm_new_crtc_state =
8726 to_dm_crtc_state(new_crtc_state);
8728 dm_new_crtc_state->update_type = (int)overall_update_type;
8731 /* Must be success */
8736 if (ret == -EDEADLK)
8737 DRM_DEBUG_DRIVER("Atomic check stopped to avoid deadlock.\n");
8738 else if (ret == -EINTR || ret == -EAGAIN || ret == -ERESTARTSYS)
8739 DRM_DEBUG_DRIVER("Atomic check stopped due to signal.\n");
8741 DRM_DEBUG_DRIVER("Atomic check failed with err: %d \n", ret);
8746 static bool is_dp_capable_without_timing_msa(struct dc *dc,
8747 struct amdgpu_dm_connector *amdgpu_dm_connector)
8750 bool capable = false;
8752 if (amdgpu_dm_connector->dc_link &&
8753 dm_helpers_dp_read_dpcd(
8755 amdgpu_dm_connector->dc_link,
8756 DP_DOWN_STREAM_PORT_COUNT,
8758 sizeof(dpcd_data))) {
8759 capable = (dpcd_data & DP_MSA_TIMING_PAR_IGNORED) ? true:false;
8764 void amdgpu_dm_update_freesync_caps(struct drm_connector *connector,
8768 bool edid_check_required;
8769 struct detailed_timing *timing;
8770 struct detailed_non_pixel *data;
8771 struct detailed_data_monitor_range *range;
8772 struct amdgpu_dm_connector *amdgpu_dm_connector =
8773 to_amdgpu_dm_connector(connector);
8774 struct dm_connector_state *dm_con_state = NULL;
8776 struct drm_device *dev = connector->dev;
8777 struct amdgpu_device *adev = dev->dev_private;
8778 bool freesync_capable = false;
8780 if (!connector->state) {
8781 DRM_ERROR("%s - Connector has no state", __func__);
8786 dm_con_state = to_dm_connector_state(connector->state);
8788 amdgpu_dm_connector->min_vfreq = 0;
8789 amdgpu_dm_connector->max_vfreq = 0;
8790 amdgpu_dm_connector->pixel_clock_mhz = 0;
8795 dm_con_state = to_dm_connector_state(connector->state);
8797 edid_check_required = false;
8798 if (!amdgpu_dm_connector->dc_sink) {
8799 DRM_ERROR("dc_sink NULL, could not add free_sync module.\n");
8802 if (!adev->dm.freesync_module)
8805 * if edid non zero restrict freesync only for dp and edp
8808 if (amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT
8809 || amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_EDP) {
8810 edid_check_required = is_dp_capable_without_timing_msa(
8812 amdgpu_dm_connector);
8815 if (edid_check_required == true && (edid->version > 1 ||
8816 (edid->version == 1 && edid->revision > 1))) {
8817 for (i = 0; i < 4; i++) {
8819 timing = &edid->detailed_timings[i];
8820 data = &timing->data.other_data;
8821 range = &data->data.range;
8823 * Check if monitor has continuous frequency mode
8825 if (data->type != EDID_DETAIL_MONITOR_RANGE)
8828 * Check for flag range limits only. If flag == 1 then
8829 * no additional timing information provided.
8830 * Default GTF, GTF Secondary curve and CVT are not
8833 if (range->flags != 1)
8836 amdgpu_dm_connector->min_vfreq = range->min_vfreq;
8837 amdgpu_dm_connector->max_vfreq = range->max_vfreq;
8838 amdgpu_dm_connector->pixel_clock_mhz =
8839 range->pixel_clock_mhz * 10;
8843 if (amdgpu_dm_connector->max_vfreq -
8844 amdgpu_dm_connector->min_vfreq > 10) {
8846 freesync_capable = true;
8852 dm_con_state->freesync_capable = freesync_capable;
8854 if (connector->vrr_capable_property)
8855 drm_connector_set_vrr_capable_property(connector,
8859 static void amdgpu_dm_set_psr_caps(struct dc_link *link)
8861 uint8_t dpcd_data[EDP_PSR_RECEIVER_CAP_SIZE];
8863 if (!(link->connector_signal & SIGNAL_TYPE_EDP))
8865 if (link->type == dc_connection_none)
8867 if (dm_helpers_dp_read_dpcd(NULL, link, DP_PSR_SUPPORT,
8868 dpcd_data, sizeof(dpcd_data))) {
8869 link->dpcd_caps.psr_caps.psr_version = dpcd_data[0];
8871 if (dpcd_data[0] == 0) {
8872 link->psr_settings.psr_version = DC_PSR_VERSION_UNSUPPORTED;
8873 link->psr_settings.psr_feature_enabled = false;
8875 link->psr_settings.psr_version = DC_PSR_VERSION_1;
8876 link->psr_settings.psr_feature_enabled = true;
8879 DRM_INFO("PSR support:%d\n", link->psr_settings.psr_feature_enabled);
8884 * amdgpu_dm_link_setup_psr() - configure psr link
8885 * @stream: stream state
8887 * Return: true if success
8889 static bool amdgpu_dm_link_setup_psr(struct dc_stream_state *stream)
8891 struct dc_link *link = NULL;
8892 struct psr_config psr_config = {0};
8893 struct psr_context psr_context = {0};
8899 link = stream->link;
8901 psr_config.psr_version = link->dpcd_caps.psr_caps.psr_version;
8903 if (psr_config.psr_version > 0) {
8904 psr_config.psr_exit_link_training_required = 0x1;
8905 psr_config.psr_frame_capture_indication_req = 0;
8906 psr_config.psr_rfb_setup_time = 0x37;
8907 psr_config.psr_sdp_transmit_line_num_deadline = 0x20;
8908 psr_config.allow_smu_optimizations = 0x0;
8910 ret = dc_link_setup_psr(link, stream, &psr_config, &psr_context);
8913 DRM_DEBUG_DRIVER("PSR link: %d\n", link->psr_settings.psr_feature_enabled);
8919 * amdgpu_dm_psr_enable() - enable psr f/w
8920 * @stream: stream state
8922 * Return: true if success
8924 bool amdgpu_dm_psr_enable(struct dc_stream_state *stream)
8926 struct dc_link *link = stream->link;
8927 unsigned int vsync_rate_hz = 0;
8928 struct dc_static_screen_params params = {0};
8929 /* Calculate number of static frames before generating interrupt to
8932 // Init fail safe of 2 frames static
8933 unsigned int num_frames_static = 2;
8935 DRM_DEBUG_DRIVER("Enabling psr...\n");
8937 vsync_rate_hz = div64_u64(div64_u64((
8938 stream->timing.pix_clk_100hz * 100),
8939 stream->timing.v_total),
8940 stream->timing.h_total);
8943 * Calculate number of frames such that at least 30 ms of time has
8946 if (vsync_rate_hz != 0) {
8947 unsigned int frame_time_microsec = 1000000 / vsync_rate_hz;
8948 num_frames_static = (30000 / frame_time_microsec) + 1;
8951 params.triggers.cursor_update = true;
8952 params.triggers.overlay_update = true;
8953 params.triggers.surface_update = true;
8954 params.num_frames = num_frames_static;
8956 dc_stream_set_static_screen_params(link->ctx->dc,
8960 return dc_link_set_psr_allow_active(link, true, false);
8964 * amdgpu_dm_psr_disable() - disable psr f/w
8965 * @stream: stream state
8967 * Return: true if success
8969 static bool amdgpu_dm_psr_disable(struct dc_stream_state *stream)
8972 DRM_DEBUG_DRIVER("Disabling psr...\n");
8974 return dc_link_set_psr_allow_active(stream->link, false, true);