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_link_dp.h"
32 #include "link_enc_cfg.h"
33 #include "dc/inc/core_types.h"
34 #include "dal_asic_id.h"
35 #include "dmub/dmub_srv.h"
36 #include "dc/inc/hw/dmcu.h"
37 #include "dc/inc/hw/abm.h"
38 #include "dc/dc_dmub_srv.h"
39 #include "dc/dc_edid_parser.h"
40 #include "dc/dc_stat.h"
41 #include "amdgpu_dm_trace.h"
45 #include "amdgpu_display.h"
46 #include "amdgpu_ucode.h"
48 #include "amdgpu_dm.h"
49 #ifdef CONFIG_DRM_AMD_DC_HDCP
50 #include "amdgpu_dm_hdcp.h"
51 #include <drm/display/drm_hdcp_helper.h>
53 #include "amdgpu_pm.h"
54 #include "amdgpu_atombios.h"
56 #include "amd_shared.h"
57 #include "amdgpu_dm_irq.h"
58 #include "dm_helpers.h"
59 #include "amdgpu_dm_mst_types.h"
60 #if defined(CONFIG_DEBUG_FS)
61 #include "amdgpu_dm_debugfs.h"
63 #include "amdgpu_dm_psr.h"
65 #include "ivsrcid/ivsrcid_vislands30.h"
67 #include "i2caux_interface.h"
68 #include <linux/module.h>
69 #include <linux/moduleparam.h>
70 #include <linux/types.h>
71 #include <linux/pm_runtime.h>
72 #include <linux/pci.h>
73 #include <linux/firmware.h>
74 #include <linux/component.h>
76 #include <drm/display/drm_dp_mst_helper.h>
77 #include <drm/display/drm_hdmi_helper.h>
78 #include <drm/drm_atomic.h>
79 #include <drm/drm_atomic_uapi.h>
80 #include <drm/drm_atomic_helper.h>
81 #include <drm/drm_fb_helper.h>
82 #include <drm/drm_fourcc.h>
83 #include <drm/drm_edid.h>
84 #include <drm/drm_vblank.h>
85 #include <drm/drm_audio_component.h>
87 #include "ivsrcid/dcn/irqsrcs_dcn_1_0.h"
89 #include "dcn/dcn_1_0_offset.h"
90 #include "dcn/dcn_1_0_sh_mask.h"
91 #include "soc15_hw_ip.h"
92 #include "vega10_ip_offset.h"
94 #include "soc15_common.h"
96 #include "modules/inc/mod_freesync.h"
97 #include "modules/power/power_helpers.h"
98 #include "modules/inc/mod_info_packet.h"
100 #define FIRMWARE_RENOIR_DMUB "amdgpu/renoir_dmcub.bin"
101 MODULE_FIRMWARE(FIRMWARE_RENOIR_DMUB);
102 #define FIRMWARE_SIENNA_CICHLID_DMUB "amdgpu/sienna_cichlid_dmcub.bin"
103 MODULE_FIRMWARE(FIRMWARE_SIENNA_CICHLID_DMUB);
104 #define FIRMWARE_NAVY_FLOUNDER_DMUB "amdgpu/navy_flounder_dmcub.bin"
105 MODULE_FIRMWARE(FIRMWARE_NAVY_FLOUNDER_DMUB);
106 #define FIRMWARE_GREEN_SARDINE_DMUB "amdgpu/green_sardine_dmcub.bin"
107 MODULE_FIRMWARE(FIRMWARE_GREEN_SARDINE_DMUB);
108 #define FIRMWARE_VANGOGH_DMUB "amdgpu/vangogh_dmcub.bin"
109 MODULE_FIRMWARE(FIRMWARE_VANGOGH_DMUB);
110 #define FIRMWARE_DIMGREY_CAVEFISH_DMUB "amdgpu/dimgrey_cavefish_dmcub.bin"
111 MODULE_FIRMWARE(FIRMWARE_DIMGREY_CAVEFISH_DMUB);
112 #define FIRMWARE_BEIGE_GOBY_DMUB "amdgpu/beige_goby_dmcub.bin"
113 MODULE_FIRMWARE(FIRMWARE_BEIGE_GOBY_DMUB);
114 #define FIRMWARE_YELLOW_CARP_DMUB "amdgpu/yellow_carp_dmcub.bin"
115 MODULE_FIRMWARE(FIRMWARE_YELLOW_CARP_DMUB);
116 #define FIRMWARE_DCN_315_DMUB "amdgpu/dcn_3_1_5_dmcub.bin"
117 MODULE_FIRMWARE(FIRMWARE_DCN_315_DMUB);
118 #define FIRMWARE_DCN316_DMUB "amdgpu/dcn_3_1_6_dmcub.bin"
119 MODULE_FIRMWARE(FIRMWARE_DCN316_DMUB);
121 #define FIRMWARE_RAVEN_DMCU "amdgpu/raven_dmcu.bin"
122 MODULE_FIRMWARE(FIRMWARE_RAVEN_DMCU);
124 #define FIRMWARE_NAVI12_DMCU "amdgpu/navi12_dmcu.bin"
125 MODULE_FIRMWARE(FIRMWARE_NAVI12_DMCU);
127 /* Number of bytes in PSP header for firmware. */
128 #define PSP_HEADER_BYTES 0x100
130 /* Number of bytes in PSP footer for firmware. */
131 #define PSP_FOOTER_BYTES 0x100
136 * The AMDgpu display manager, **amdgpu_dm** (or even simpler,
137 * **dm**) sits between DRM and DC. It acts as a liaison, converting DRM
138 * requests into DC requests, and DC responses into DRM responses.
140 * The root control structure is &struct amdgpu_display_manager.
143 /* basic init/fini API */
144 static int amdgpu_dm_init(struct amdgpu_device *adev);
145 static void amdgpu_dm_fini(struct amdgpu_device *adev);
146 static bool is_freesync_video_mode(const struct drm_display_mode *mode, struct amdgpu_dm_connector *aconnector);
148 static enum drm_mode_subconnector get_subconnector_type(struct dc_link *link)
150 switch (link->dpcd_caps.dongle_type) {
151 case DISPLAY_DONGLE_NONE:
152 return DRM_MODE_SUBCONNECTOR_Native;
153 case DISPLAY_DONGLE_DP_VGA_CONVERTER:
154 return DRM_MODE_SUBCONNECTOR_VGA;
155 case DISPLAY_DONGLE_DP_DVI_CONVERTER:
156 case DISPLAY_DONGLE_DP_DVI_DONGLE:
157 return DRM_MODE_SUBCONNECTOR_DVID;
158 case DISPLAY_DONGLE_DP_HDMI_CONVERTER:
159 case DISPLAY_DONGLE_DP_HDMI_DONGLE:
160 return DRM_MODE_SUBCONNECTOR_HDMIA;
161 case DISPLAY_DONGLE_DP_HDMI_MISMATCHED_DONGLE:
163 return DRM_MODE_SUBCONNECTOR_Unknown;
167 static void update_subconnector_property(struct amdgpu_dm_connector *aconnector)
169 struct dc_link *link = aconnector->dc_link;
170 struct drm_connector *connector = &aconnector->base;
171 enum drm_mode_subconnector subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
173 if (connector->connector_type != DRM_MODE_CONNECTOR_DisplayPort)
176 if (aconnector->dc_sink)
177 subconnector = get_subconnector_type(link);
179 drm_object_property_set_value(&connector->base,
180 connector->dev->mode_config.dp_subconnector_property,
185 * initializes drm_device display related structures, based on the information
186 * provided by DAL. The drm strcutures are: drm_crtc, drm_connector,
187 * drm_encoder, drm_mode_config
189 * Returns 0 on success
191 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev);
192 /* removes and deallocates the drm structures, created by the above function */
193 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm);
195 static int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
196 struct drm_plane *plane,
197 unsigned long possible_crtcs,
198 const struct dc_plane_cap *plane_cap);
199 static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
200 struct drm_plane *plane,
201 uint32_t link_index);
202 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
203 struct amdgpu_dm_connector *amdgpu_dm_connector,
205 struct amdgpu_encoder *amdgpu_encoder);
206 static int amdgpu_dm_encoder_init(struct drm_device *dev,
207 struct amdgpu_encoder *aencoder,
208 uint32_t link_index);
210 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector);
212 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state);
214 static int amdgpu_dm_atomic_check(struct drm_device *dev,
215 struct drm_atomic_state *state);
217 static void handle_cursor_update(struct drm_plane *plane,
218 struct drm_plane_state *old_plane_state);
220 static const struct drm_format_info *
221 amd_get_format_info(const struct drm_mode_fb_cmd2 *cmd);
223 static void handle_hpd_irq_helper(struct amdgpu_dm_connector *aconnector);
224 static void handle_hpd_rx_irq(void *param);
227 is_timing_unchanged_for_freesync(struct drm_crtc_state *old_crtc_state,
228 struct drm_crtc_state *new_crtc_state);
230 * dm_vblank_get_counter
233 * Get counter for number of vertical blanks
236 * struct amdgpu_device *adev - [in] desired amdgpu device
237 * int disp_idx - [in] which CRTC to get the counter from
240 * Counter for vertical blanks
242 static u32 dm_vblank_get_counter(struct amdgpu_device *adev, int crtc)
244 if (crtc >= adev->mode_info.num_crtc)
247 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
249 if (acrtc->dm_irq_params.stream == NULL) {
250 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
255 return dc_stream_get_vblank_counter(acrtc->dm_irq_params.stream);
259 static int dm_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
260 u32 *vbl, u32 *position)
262 uint32_t v_blank_start, v_blank_end, h_position, v_position;
264 if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
267 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
269 if (acrtc->dm_irq_params.stream == NULL) {
270 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
276 * TODO rework base driver to use values directly.
277 * for now parse it back into reg-format
279 dc_stream_get_scanoutpos(acrtc->dm_irq_params.stream,
285 *position = v_position | (h_position << 16);
286 *vbl = v_blank_start | (v_blank_end << 16);
292 static bool dm_is_idle(void *handle)
298 static int dm_wait_for_idle(void *handle)
304 static bool dm_check_soft_reset(void *handle)
309 static int dm_soft_reset(void *handle)
315 static struct amdgpu_crtc *
316 get_crtc_by_otg_inst(struct amdgpu_device *adev,
319 struct drm_device *dev = adev_to_drm(adev);
320 struct drm_crtc *crtc;
321 struct amdgpu_crtc *amdgpu_crtc;
323 if (WARN_ON(otg_inst == -1))
324 return adev->mode_info.crtcs[0];
326 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
327 amdgpu_crtc = to_amdgpu_crtc(crtc);
329 if (amdgpu_crtc->otg_inst == otg_inst)
336 static inline bool amdgpu_dm_vrr_active_irq(struct amdgpu_crtc *acrtc)
338 return acrtc->dm_irq_params.freesync_config.state ==
339 VRR_STATE_ACTIVE_VARIABLE ||
340 acrtc->dm_irq_params.freesync_config.state ==
341 VRR_STATE_ACTIVE_FIXED;
344 static inline bool amdgpu_dm_vrr_active(struct dm_crtc_state *dm_state)
346 return dm_state->freesync_config.state == VRR_STATE_ACTIVE_VARIABLE ||
347 dm_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED;
350 static inline bool is_dc_timing_adjust_needed(struct dm_crtc_state *old_state,
351 struct dm_crtc_state *new_state)
353 if (new_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED)
355 else if (amdgpu_dm_vrr_active(old_state) != amdgpu_dm_vrr_active(new_state))
362 * dm_pflip_high_irq() - Handle pageflip interrupt
363 * @interrupt_params: ignored
365 * Handles the pageflip interrupt by notifying all interested parties
366 * that the pageflip has been completed.
368 static void dm_pflip_high_irq(void *interrupt_params)
370 struct amdgpu_crtc *amdgpu_crtc;
371 struct common_irq_params *irq_params = interrupt_params;
372 struct amdgpu_device *adev = irq_params->adev;
374 struct drm_pending_vblank_event *e;
375 uint32_t vpos, hpos, v_blank_start, v_blank_end;
378 amdgpu_crtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_PFLIP);
380 /* IRQ could occur when in initial stage */
381 /* TODO work and BO cleanup */
382 if (amdgpu_crtc == NULL) {
383 DC_LOG_PFLIP("CRTC is null, returning.\n");
387 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
389 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED){
390 DC_LOG_PFLIP("amdgpu_crtc->pflip_status = %d !=AMDGPU_FLIP_SUBMITTED(%d) on crtc:%d[%p] \n",
391 amdgpu_crtc->pflip_status,
392 AMDGPU_FLIP_SUBMITTED,
393 amdgpu_crtc->crtc_id,
395 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
399 /* page flip completed. */
400 e = amdgpu_crtc->event;
401 amdgpu_crtc->event = NULL;
405 vrr_active = amdgpu_dm_vrr_active_irq(amdgpu_crtc);
407 /* Fixed refresh rate, or VRR scanout position outside front-porch? */
409 !dc_stream_get_scanoutpos(amdgpu_crtc->dm_irq_params.stream, &v_blank_start,
410 &v_blank_end, &hpos, &vpos) ||
411 (vpos < v_blank_start)) {
412 /* Update to correct count and vblank timestamp if racing with
413 * vblank irq. This also updates to the correct vblank timestamp
414 * even in VRR mode, as scanout is past the front-porch atm.
416 drm_crtc_accurate_vblank_count(&amdgpu_crtc->base);
418 /* Wake up userspace by sending the pageflip event with proper
419 * count and timestamp of vblank of flip completion.
422 drm_crtc_send_vblank_event(&amdgpu_crtc->base, e);
424 /* Event sent, so done with vblank for this flip */
425 drm_crtc_vblank_put(&amdgpu_crtc->base);
428 /* VRR active and inside front-porch: vblank count and
429 * timestamp for pageflip event will only be up to date after
430 * drm_crtc_handle_vblank() has been executed from late vblank
431 * irq handler after start of back-porch (vline 0). We queue the
432 * pageflip event for send-out by drm_crtc_handle_vblank() with
433 * updated timestamp and count, once it runs after us.
435 * We need to open-code this instead of using the helper
436 * drm_crtc_arm_vblank_event(), as that helper would
437 * call drm_crtc_accurate_vblank_count(), which we must
438 * not call in VRR mode while we are in front-porch!
441 /* sequence will be replaced by real count during send-out. */
442 e->sequence = drm_crtc_vblank_count(&amdgpu_crtc->base);
443 e->pipe = amdgpu_crtc->crtc_id;
445 list_add_tail(&e->base.link, &adev_to_drm(adev)->vblank_event_list);
449 /* Keep track of vblank of this flip for flip throttling. We use the
450 * cooked hw counter, as that one incremented at start of this vblank
451 * of pageflip completion, so last_flip_vblank is the forbidden count
452 * for queueing new pageflips if vsync + VRR is enabled.
454 amdgpu_crtc->dm_irq_params.last_flip_vblank =
455 amdgpu_get_vblank_counter_kms(&amdgpu_crtc->base);
457 amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
458 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
460 DC_LOG_PFLIP("crtc:%d[%p], pflip_stat:AMDGPU_FLIP_NONE, vrr[%d]-fp %d\n",
461 amdgpu_crtc->crtc_id, amdgpu_crtc,
462 vrr_active, (int) !e);
465 static void dm_vupdate_high_irq(void *interrupt_params)
467 struct common_irq_params *irq_params = interrupt_params;
468 struct amdgpu_device *adev = irq_params->adev;
469 struct amdgpu_crtc *acrtc;
470 struct drm_device *drm_dev;
471 struct drm_vblank_crtc *vblank;
472 ktime_t frame_duration_ns, previous_timestamp;
476 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VUPDATE);
479 vrr_active = amdgpu_dm_vrr_active_irq(acrtc);
480 drm_dev = acrtc->base.dev;
481 vblank = &drm_dev->vblank[acrtc->base.index];
482 previous_timestamp = atomic64_read(&irq_params->previous_timestamp);
483 frame_duration_ns = vblank->time - previous_timestamp;
485 if (frame_duration_ns > 0) {
486 trace_amdgpu_refresh_rate_track(acrtc->base.index,
488 ktime_divns(NSEC_PER_SEC, frame_duration_ns));
489 atomic64_set(&irq_params->previous_timestamp, vblank->time);
492 DC_LOG_VBLANK("crtc:%d, vupdate-vrr:%d\n",
496 /* Core vblank handling is done here after end of front-porch in
497 * vrr mode, as vblank timestamping will give valid results
498 * while now done after front-porch. This will also deliver
499 * page-flip completion events that have been queued to us
500 * if a pageflip happened inside front-porch.
503 drm_crtc_handle_vblank(&acrtc->base);
505 /* BTR processing for pre-DCE12 ASICs */
506 if (acrtc->dm_irq_params.stream &&
507 adev->family < AMDGPU_FAMILY_AI) {
508 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
509 mod_freesync_handle_v_update(
510 adev->dm.freesync_module,
511 acrtc->dm_irq_params.stream,
512 &acrtc->dm_irq_params.vrr_params);
514 dc_stream_adjust_vmin_vmax(
516 acrtc->dm_irq_params.stream,
517 &acrtc->dm_irq_params.vrr_params.adjust);
518 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
525 * dm_crtc_high_irq() - Handles CRTC interrupt
526 * @interrupt_params: used for determining the CRTC instance
528 * Handles the CRTC/VSYNC interrupt by notfying DRM's VBLANK
531 static void dm_crtc_high_irq(void *interrupt_params)
533 struct common_irq_params *irq_params = interrupt_params;
534 struct amdgpu_device *adev = irq_params->adev;
535 struct amdgpu_crtc *acrtc;
539 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VBLANK);
543 vrr_active = amdgpu_dm_vrr_active_irq(acrtc);
545 DC_LOG_VBLANK("crtc:%d, vupdate-vrr:%d, planes:%d\n", acrtc->crtc_id,
546 vrr_active, acrtc->dm_irq_params.active_planes);
549 * Core vblank handling at start of front-porch is only possible
550 * in non-vrr mode, as only there vblank timestamping will give
551 * valid results while done in front-porch. Otherwise defer it
552 * to dm_vupdate_high_irq after end of front-porch.
555 drm_crtc_handle_vblank(&acrtc->base);
558 * Following stuff must happen at start of vblank, for crc
559 * computation and below-the-range btr support in vrr mode.
561 amdgpu_dm_crtc_handle_crc_irq(&acrtc->base);
563 /* BTR updates need to happen before VUPDATE on Vega and above. */
564 if (adev->family < AMDGPU_FAMILY_AI)
567 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
569 if (acrtc->dm_irq_params.stream &&
570 acrtc->dm_irq_params.vrr_params.supported &&
571 acrtc->dm_irq_params.freesync_config.state ==
572 VRR_STATE_ACTIVE_VARIABLE) {
573 mod_freesync_handle_v_update(adev->dm.freesync_module,
574 acrtc->dm_irq_params.stream,
575 &acrtc->dm_irq_params.vrr_params);
577 dc_stream_adjust_vmin_vmax(adev->dm.dc, acrtc->dm_irq_params.stream,
578 &acrtc->dm_irq_params.vrr_params.adjust);
582 * If there aren't any active_planes then DCH HUBP may be clock-gated.
583 * In that case, pageflip completion interrupts won't fire and pageflip
584 * completion events won't get delivered. Prevent this by sending
585 * pending pageflip events from here if a flip is still pending.
587 * If any planes are enabled, use dm_pflip_high_irq() instead, to
588 * avoid race conditions between flip programming and completion,
589 * which could cause too early flip completion events.
591 if (adev->family >= AMDGPU_FAMILY_RV &&
592 acrtc->pflip_status == AMDGPU_FLIP_SUBMITTED &&
593 acrtc->dm_irq_params.active_planes == 0) {
595 drm_crtc_send_vblank_event(&acrtc->base, acrtc->event);
597 drm_crtc_vblank_put(&acrtc->base);
599 acrtc->pflip_status = AMDGPU_FLIP_NONE;
602 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
605 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
607 * dm_dcn_vertical_interrupt0_high_irq() - Handles OTG Vertical interrupt0 for
608 * DCN generation ASICs
609 * @interrupt_params: interrupt parameters
611 * Used to set crc window/read out crc value at vertical line 0 position
613 static void dm_dcn_vertical_interrupt0_high_irq(void *interrupt_params)
615 struct common_irq_params *irq_params = interrupt_params;
616 struct amdgpu_device *adev = irq_params->adev;
617 struct amdgpu_crtc *acrtc;
619 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VLINE0);
624 amdgpu_dm_crtc_handle_crc_window_irq(&acrtc->base);
626 #endif /* CONFIG_DRM_AMD_SECURE_DISPLAY */
629 * dmub_aux_setconfig_callback - Callback for AUX or SET_CONFIG command.
630 * @adev: amdgpu_device pointer
631 * @notify: dmub notification structure
633 * Dmub AUX or SET_CONFIG command completion processing callback
634 * Copies dmub notification to DM which is to be read by AUX command.
635 * issuing thread and also signals the event to wake up the thread.
637 static void dmub_aux_setconfig_callback(struct amdgpu_device *adev,
638 struct dmub_notification *notify)
640 if (adev->dm.dmub_notify)
641 memcpy(adev->dm.dmub_notify, notify, sizeof(struct dmub_notification));
642 if (notify->type == DMUB_NOTIFICATION_AUX_REPLY)
643 complete(&adev->dm.dmub_aux_transfer_done);
647 * dmub_hpd_callback - DMUB HPD interrupt processing callback.
648 * @adev: amdgpu_device pointer
649 * @notify: dmub notification structure
651 * Dmub Hpd interrupt processing callback. Gets displayindex through the
652 * ink index and calls helper to do the processing.
654 static void dmub_hpd_callback(struct amdgpu_device *adev,
655 struct dmub_notification *notify)
657 struct amdgpu_dm_connector *aconnector;
658 struct amdgpu_dm_connector *hpd_aconnector = NULL;
659 struct drm_connector *connector;
660 struct drm_connector_list_iter iter;
661 struct dc_link *link;
662 uint8_t link_index = 0;
663 struct drm_device *dev;
668 if (notify == NULL) {
669 DRM_ERROR("DMUB HPD callback notification was NULL");
673 if (notify->link_index > adev->dm.dc->link_count) {
674 DRM_ERROR("DMUB HPD index (%u)is abnormal", notify->link_index);
678 link_index = notify->link_index;
679 link = adev->dm.dc->links[link_index];
682 drm_connector_list_iter_begin(dev, &iter);
683 drm_for_each_connector_iter(connector, &iter) {
684 aconnector = to_amdgpu_dm_connector(connector);
685 if (link && aconnector->dc_link == link) {
686 DRM_INFO("DMUB HPD callback: link_index=%u\n", link_index);
687 hpd_aconnector = aconnector;
691 drm_connector_list_iter_end(&iter);
693 if (hpd_aconnector) {
694 if (notify->type == DMUB_NOTIFICATION_HPD)
695 handle_hpd_irq_helper(hpd_aconnector);
696 else if (notify->type == DMUB_NOTIFICATION_HPD_IRQ)
697 handle_hpd_rx_irq(hpd_aconnector);
702 * register_dmub_notify_callback - Sets callback for DMUB notify
703 * @adev: amdgpu_device pointer
704 * @type: Type of dmub notification
705 * @callback: Dmub interrupt callback function
706 * @dmub_int_thread_offload: offload indicator
708 * API to register a dmub callback handler for a dmub notification
709 * Also sets indicator whether callback processing to be offloaded.
710 * to dmub interrupt handling thread
711 * Return: true if successfully registered, false if there is existing registration
713 static bool register_dmub_notify_callback(struct amdgpu_device *adev,
714 enum dmub_notification_type type,
715 dmub_notify_interrupt_callback_t callback,
716 bool dmub_int_thread_offload)
718 if (callback != NULL && type < ARRAY_SIZE(adev->dm.dmub_thread_offload)) {
719 adev->dm.dmub_callback[type] = callback;
720 adev->dm.dmub_thread_offload[type] = dmub_int_thread_offload;
727 static void dm_handle_hpd_work(struct work_struct *work)
729 struct dmub_hpd_work *dmub_hpd_wrk;
731 dmub_hpd_wrk = container_of(work, struct dmub_hpd_work, handle_hpd_work);
733 if (!dmub_hpd_wrk->dmub_notify) {
734 DRM_ERROR("dmub_hpd_wrk dmub_notify is NULL");
738 if (dmub_hpd_wrk->dmub_notify->type < ARRAY_SIZE(dmub_hpd_wrk->adev->dm.dmub_callback)) {
739 dmub_hpd_wrk->adev->dm.dmub_callback[dmub_hpd_wrk->dmub_notify->type](dmub_hpd_wrk->adev,
740 dmub_hpd_wrk->dmub_notify);
743 kfree(dmub_hpd_wrk->dmub_notify);
748 #define DMUB_TRACE_MAX_READ 64
750 * dm_dmub_outbox1_low_irq() - Handles Outbox interrupt
751 * @interrupt_params: used for determining the Outbox instance
753 * Handles the Outbox Interrupt
756 static void dm_dmub_outbox1_low_irq(void *interrupt_params)
758 struct dmub_notification notify;
759 struct common_irq_params *irq_params = interrupt_params;
760 struct amdgpu_device *adev = irq_params->adev;
761 struct amdgpu_display_manager *dm = &adev->dm;
762 struct dmcub_trace_buf_entry entry = { 0 };
764 struct dmub_hpd_work *dmub_hpd_wrk;
765 struct dc_link *plink = NULL;
767 if (dc_enable_dmub_notifications(adev->dm.dc) &&
768 irq_params->irq_src == DC_IRQ_SOURCE_DMCUB_OUTBOX) {
771 dc_stat_get_dmub_notification(adev->dm.dc, ¬ify);
772 if (notify.type > ARRAY_SIZE(dm->dmub_thread_offload)) {
773 DRM_ERROR("DM: notify type %d invalid!", notify.type);
776 if (!dm->dmub_callback[notify.type]) {
777 DRM_DEBUG_DRIVER("DMUB notification skipped, no handler: type=%d\n", notify.type);
780 if (dm->dmub_thread_offload[notify.type] == true) {
781 dmub_hpd_wrk = kzalloc(sizeof(*dmub_hpd_wrk), GFP_ATOMIC);
783 DRM_ERROR("Failed to allocate dmub_hpd_wrk");
786 dmub_hpd_wrk->dmub_notify = kzalloc(sizeof(struct dmub_notification), GFP_ATOMIC);
787 if (!dmub_hpd_wrk->dmub_notify) {
789 DRM_ERROR("Failed to allocate dmub_hpd_wrk->dmub_notify");
792 INIT_WORK(&dmub_hpd_wrk->handle_hpd_work, dm_handle_hpd_work);
793 if (dmub_hpd_wrk->dmub_notify)
794 memcpy(dmub_hpd_wrk->dmub_notify, ¬ify, sizeof(struct dmub_notification));
795 dmub_hpd_wrk->adev = adev;
796 if (notify.type == DMUB_NOTIFICATION_HPD) {
797 plink = adev->dm.dc->links[notify.link_index];
800 notify.hpd_status == DP_HPD_PLUG;
803 queue_work(adev->dm.delayed_hpd_wq, &dmub_hpd_wrk->handle_hpd_work);
805 dm->dmub_callback[notify.type](adev, ¬ify);
807 } while (notify.pending_notification);
812 if (dc_dmub_srv_get_dmub_outbox0_msg(dm->dc, &entry)) {
813 trace_amdgpu_dmub_trace_high_irq(entry.trace_code, entry.tick_count,
814 entry.param0, entry.param1);
816 DRM_DEBUG_DRIVER("trace_code:%u, tick_count:%u, param0:%u, param1:%u\n",
817 entry.trace_code, entry.tick_count, entry.param0, entry.param1);
823 } while (count <= DMUB_TRACE_MAX_READ);
825 if (count > DMUB_TRACE_MAX_READ)
826 DRM_DEBUG_DRIVER("Warning : count > DMUB_TRACE_MAX_READ");
829 static int dm_set_clockgating_state(void *handle,
830 enum amd_clockgating_state state)
835 static int dm_set_powergating_state(void *handle,
836 enum amd_powergating_state state)
841 /* Prototypes of private functions */
842 static int dm_early_init(void* handle);
844 /* Allocate memory for FBC compressed data */
845 static void amdgpu_dm_fbc_init(struct drm_connector *connector)
847 struct drm_device *dev = connector->dev;
848 struct amdgpu_device *adev = drm_to_adev(dev);
849 struct dm_compressor_info *compressor = &adev->dm.compressor;
850 struct amdgpu_dm_connector *aconn = to_amdgpu_dm_connector(connector);
851 struct drm_display_mode *mode;
852 unsigned long max_size = 0;
854 if (adev->dm.dc->fbc_compressor == NULL)
857 if (aconn->dc_link->connector_signal != SIGNAL_TYPE_EDP)
860 if (compressor->bo_ptr)
864 list_for_each_entry(mode, &connector->modes, head) {
865 if (max_size < mode->htotal * mode->vtotal)
866 max_size = mode->htotal * mode->vtotal;
870 int r = amdgpu_bo_create_kernel(adev, max_size * 4, PAGE_SIZE,
871 AMDGPU_GEM_DOMAIN_GTT, &compressor->bo_ptr,
872 &compressor->gpu_addr, &compressor->cpu_addr);
875 DRM_ERROR("DM: Failed to initialize FBC\n");
877 adev->dm.dc->ctx->fbc_gpu_addr = compressor->gpu_addr;
878 DRM_INFO("DM: FBC alloc %lu\n", max_size*4);
885 static int amdgpu_dm_audio_component_get_eld(struct device *kdev, int port,
886 int pipe, bool *enabled,
887 unsigned char *buf, int max_bytes)
889 struct drm_device *dev = dev_get_drvdata(kdev);
890 struct amdgpu_device *adev = drm_to_adev(dev);
891 struct drm_connector *connector;
892 struct drm_connector_list_iter conn_iter;
893 struct amdgpu_dm_connector *aconnector;
898 mutex_lock(&adev->dm.audio_lock);
900 drm_connector_list_iter_begin(dev, &conn_iter);
901 drm_for_each_connector_iter(connector, &conn_iter) {
902 aconnector = to_amdgpu_dm_connector(connector);
903 if (aconnector->audio_inst != port)
907 ret = drm_eld_size(connector->eld);
908 memcpy(buf, connector->eld, min(max_bytes, ret));
912 drm_connector_list_iter_end(&conn_iter);
914 mutex_unlock(&adev->dm.audio_lock);
916 DRM_DEBUG_KMS("Get ELD : idx=%d ret=%d en=%d\n", port, ret, *enabled);
921 static const struct drm_audio_component_ops amdgpu_dm_audio_component_ops = {
922 .get_eld = amdgpu_dm_audio_component_get_eld,
925 static int amdgpu_dm_audio_component_bind(struct device *kdev,
926 struct device *hda_kdev, void *data)
928 struct drm_device *dev = dev_get_drvdata(kdev);
929 struct amdgpu_device *adev = drm_to_adev(dev);
930 struct drm_audio_component *acomp = data;
932 acomp->ops = &amdgpu_dm_audio_component_ops;
934 adev->dm.audio_component = acomp;
939 static void amdgpu_dm_audio_component_unbind(struct device *kdev,
940 struct device *hda_kdev, void *data)
942 struct drm_device *dev = dev_get_drvdata(kdev);
943 struct amdgpu_device *adev = drm_to_adev(dev);
944 struct drm_audio_component *acomp = data;
948 adev->dm.audio_component = NULL;
951 static const struct component_ops amdgpu_dm_audio_component_bind_ops = {
952 .bind = amdgpu_dm_audio_component_bind,
953 .unbind = amdgpu_dm_audio_component_unbind,
956 static int amdgpu_dm_audio_init(struct amdgpu_device *adev)
963 adev->mode_info.audio.enabled = true;
965 adev->mode_info.audio.num_pins = adev->dm.dc->res_pool->audio_count;
967 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
968 adev->mode_info.audio.pin[i].channels = -1;
969 adev->mode_info.audio.pin[i].rate = -1;
970 adev->mode_info.audio.pin[i].bits_per_sample = -1;
971 adev->mode_info.audio.pin[i].status_bits = 0;
972 adev->mode_info.audio.pin[i].category_code = 0;
973 adev->mode_info.audio.pin[i].connected = false;
974 adev->mode_info.audio.pin[i].id =
975 adev->dm.dc->res_pool->audios[i]->inst;
976 adev->mode_info.audio.pin[i].offset = 0;
979 ret = component_add(adev->dev, &amdgpu_dm_audio_component_bind_ops);
983 adev->dm.audio_registered = true;
988 static void amdgpu_dm_audio_fini(struct amdgpu_device *adev)
993 if (!adev->mode_info.audio.enabled)
996 if (adev->dm.audio_registered) {
997 component_del(adev->dev, &amdgpu_dm_audio_component_bind_ops);
998 adev->dm.audio_registered = false;
1001 /* TODO: Disable audio? */
1003 adev->mode_info.audio.enabled = false;
1006 static void amdgpu_dm_audio_eld_notify(struct amdgpu_device *adev, int pin)
1008 struct drm_audio_component *acomp = adev->dm.audio_component;
1010 if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify) {
1011 DRM_DEBUG_KMS("Notify ELD: %d\n", pin);
1013 acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr,
1018 static int dm_dmub_hw_init(struct amdgpu_device *adev)
1020 const struct dmcub_firmware_header_v1_0 *hdr;
1021 struct dmub_srv *dmub_srv = adev->dm.dmub_srv;
1022 struct dmub_srv_fb_info *fb_info = adev->dm.dmub_fb_info;
1023 const struct firmware *dmub_fw = adev->dm.dmub_fw;
1024 struct dmcu *dmcu = adev->dm.dc->res_pool->dmcu;
1025 struct abm *abm = adev->dm.dc->res_pool->abm;
1026 struct dmub_srv_hw_params hw_params;
1027 enum dmub_status status;
1028 const unsigned char *fw_inst_const, *fw_bss_data;
1029 uint32_t i, fw_inst_const_size, fw_bss_data_size;
1030 bool has_hw_support;
1033 /* DMUB isn't supported on the ASIC. */
1037 DRM_ERROR("No framebuffer info for DMUB service.\n");
1042 /* Firmware required for DMUB support. */
1043 DRM_ERROR("No firmware provided for DMUB.\n");
1047 status = dmub_srv_has_hw_support(dmub_srv, &has_hw_support);
1048 if (status != DMUB_STATUS_OK) {
1049 DRM_ERROR("Error checking HW support for DMUB: %d\n", status);
1053 if (!has_hw_support) {
1054 DRM_INFO("DMUB unsupported on ASIC\n");
1058 /* Reset DMCUB if it was previously running - before we overwrite its memory. */
1059 status = dmub_srv_hw_reset(dmub_srv);
1060 if (status != DMUB_STATUS_OK)
1061 DRM_WARN("Error resetting DMUB HW: %d\n", status);
1063 hdr = (const struct dmcub_firmware_header_v1_0 *)dmub_fw->data;
1065 fw_inst_const = dmub_fw->data +
1066 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
1069 fw_bss_data = dmub_fw->data +
1070 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
1071 le32_to_cpu(hdr->inst_const_bytes);
1073 /* Copy firmware and bios info into FB memory. */
1074 fw_inst_const_size = le32_to_cpu(hdr->inst_const_bytes) -
1075 PSP_HEADER_BYTES - PSP_FOOTER_BYTES;
1077 fw_bss_data_size = le32_to_cpu(hdr->bss_data_bytes);
1079 /* if adev->firmware.load_type == AMDGPU_FW_LOAD_PSP,
1080 * amdgpu_ucode_init_single_fw will load dmub firmware
1081 * fw_inst_const part to cw0; otherwise, the firmware back door load
1082 * will be done by dm_dmub_hw_init
1084 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1085 memcpy(fb_info->fb[DMUB_WINDOW_0_INST_CONST].cpu_addr, fw_inst_const,
1086 fw_inst_const_size);
1089 if (fw_bss_data_size)
1090 memcpy(fb_info->fb[DMUB_WINDOW_2_BSS_DATA].cpu_addr,
1091 fw_bss_data, fw_bss_data_size);
1093 /* Copy firmware bios info into FB memory. */
1094 memcpy(fb_info->fb[DMUB_WINDOW_3_VBIOS].cpu_addr, adev->bios,
1097 /* Reset regions that need to be reset. */
1098 memset(fb_info->fb[DMUB_WINDOW_4_MAILBOX].cpu_addr, 0,
1099 fb_info->fb[DMUB_WINDOW_4_MAILBOX].size);
1101 memset(fb_info->fb[DMUB_WINDOW_5_TRACEBUFF].cpu_addr, 0,
1102 fb_info->fb[DMUB_WINDOW_5_TRACEBUFF].size);
1104 memset(fb_info->fb[DMUB_WINDOW_6_FW_STATE].cpu_addr, 0,
1105 fb_info->fb[DMUB_WINDOW_6_FW_STATE].size);
1107 /* Initialize hardware. */
1108 memset(&hw_params, 0, sizeof(hw_params));
1109 hw_params.fb_base = adev->gmc.fb_start;
1110 hw_params.fb_offset = adev->gmc.aper_base;
1112 /* backdoor load firmware and trigger dmub running */
1113 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
1114 hw_params.load_inst_const = true;
1117 hw_params.psp_version = dmcu->psp_version;
1119 for (i = 0; i < fb_info->num_fb; ++i)
1120 hw_params.fb[i] = &fb_info->fb[i];
1122 switch (adev->ip_versions[DCE_HWIP][0]) {
1123 case IP_VERSION(3, 1, 3): /* Only for this asic hw internal rev B0 */
1124 hw_params.dpia_supported = true;
1125 hw_params.disable_dpia = adev->dm.dc->debug.dpia_debug.bits.disable_dpia;
1131 status = dmub_srv_hw_init(dmub_srv, &hw_params);
1132 if (status != DMUB_STATUS_OK) {
1133 DRM_ERROR("Error initializing DMUB HW: %d\n", status);
1137 /* Wait for firmware load to finish. */
1138 status = dmub_srv_wait_for_auto_load(dmub_srv, 100000);
1139 if (status != DMUB_STATUS_OK)
1140 DRM_WARN("Wait for DMUB auto-load failed: %d\n", status);
1142 /* Init DMCU and ABM if available. */
1144 dmcu->funcs->dmcu_init(dmcu);
1145 abm->dmcu_is_running = dmcu->funcs->is_dmcu_initialized(dmcu);
1148 if (!adev->dm.dc->ctx->dmub_srv)
1149 adev->dm.dc->ctx->dmub_srv = dc_dmub_srv_create(adev->dm.dc, dmub_srv);
1150 if (!adev->dm.dc->ctx->dmub_srv) {
1151 DRM_ERROR("Couldn't allocate DC DMUB server!\n");
1155 DRM_INFO("DMUB hardware initialized: version=0x%08X\n",
1156 adev->dm.dmcub_fw_version);
1161 static void dm_dmub_hw_resume(struct amdgpu_device *adev)
1163 struct dmub_srv *dmub_srv = adev->dm.dmub_srv;
1164 enum dmub_status status;
1168 /* DMUB isn't supported on the ASIC. */
1172 status = dmub_srv_is_hw_init(dmub_srv, &init);
1173 if (status != DMUB_STATUS_OK)
1174 DRM_WARN("DMUB hardware init check failed: %d\n", status);
1176 if (status == DMUB_STATUS_OK && init) {
1177 /* Wait for firmware load to finish. */
1178 status = dmub_srv_wait_for_auto_load(dmub_srv, 100000);
1179 if (status != DMUB_STATUS_OK)
1180 DRM_WARN("Wait for DMUB auto-load failed: %d\n", status);
1182 /* Perform the full hardware initialization. */
1183 dm_dmub_hw_init(adev);
1187 static void mmhub_read_system_context(struct amdgpu_device *adev, struct dc_phy_addr_space_config *pa_config)
1190 uint32_t logical_addr_low;
1191 uint32_t logical_addr_high;
1192 uint32_t agp_base, agp_bot, agp_top;
1193 PHYSICAL_ADDRESS_LOC page_table_start, page_table_end, page_table_base;
1195 memset(pa_config, 0, sizeof(*pa_config));
1197 logical_addr_low = min(adev->gmc.fb_start, adev->gmc.agp_start) >> 18;
1198 pt_base = amdgpu_gmc_pd_addr(adev->gart.bo);
1200 if (adev->apu_flags & AMD_APU_IS_RAVEN2)
1202 * Raven2 has a HW issue that it is unable to use the vram which
1203 * is out of MC_VM_SYSTEM_APERTURE_HIGH_ADDR. So here is the
1204 * workaround that increase system aperture high address (add 1)
1205 * to get rid of the VM fault and hardware hang.
1207 logical_addr_high = max((adev->gmc.fb_end >> 18) + 0x1, adev->gmc.agp_end >> 18);
1209 logical_addr_high = max(adev->gmc.fb_end, adev->gmc.agp_end) >> 18;
1212 agp_bot = adev->gmc.agp_start >> 24;
1213 agp_top = adev->gmc.agp_end >> 24;
1216 page_table_start.high_part = (u32)(adev->gmc.gart_start >> 44) & 0xF;
1217 page_table_start.low_part = (u32)(adev->gmc.gart_start >> 12);
1218 page_table_end.high_part = (u32)(adev->gmc.gart_end >> 44) & 0xF;
1219 page_table_end.low_part = (u32)(adev->gmc.gart_end >> 12);
1220 page_table_base.high_part = upper_32_bits(pt_base) & 0xF;
1221 page_table_base.low_part = lower_32_bits(pt_base);
1223 pa_config->system_aperture.start_addr = (uint64_t)logical_addr_low << 18;
1224 pa_config->system_aperture.end_addr = (uint64_t)logical_addr_high << 18;
1226 pa_config->system_aperture.agp_base = (uint64_t)agp_base << 24 ;
1227 pa_config->system_aperture.agp_bot = (uint64_t)agp_bot << 24;
1228 pa_config->system_aperture.agp_top = (uint64_t)agp_top << 24;
1230 pa_config->system_aperture.fb_base = adev->gmc.fb_start;
1231 pa_config->system_aperture.fb_offset = adev->gmc.aper_base;
1232 pa_config->system_aperture.fb_top = adev->gmc.fb_end;
1234 pa_config->gart_config.page_table_start_addr = page_table_start.quad_part << 12;
1235 pa_config->gart_config.page_table_end_addr = page_table_end.quad_part << 12;
1236 pa_config->gart_config.page_table_base_addr = page_table_base.quad_part;
1238 pa_config->is_hvm_enabled = 0;
1242 static void vblank_control_worker(struct work_struct *work)
1244 struct vblank_control_work *vblank_work =
1245 container_of(work, struct vblank_control_work, work);
1246 struct amdgpu_display_manager *dm = vblank_work->dm;
1248 mutex_lock(&dm->dc_lock);
1250 if (vblank_work->enable)
1251 dm->active_vblank_irq_count++;
1252 else if(dm->active_vblank_irq_count)
1253 dm->active_vblank_irq_count--;
1255 dc_allow_idle_optimizations(dm->dc, dm->active_vblank_irq_count == 0);
1257 DRM_DEBUG_KMS("Allow idle optimizations (MALL): %d\n", dm->active_vblank_irq_count == 0);
1259 /* Control PSR based on vblank requirements from OS */
1260 if (vblank_work->stream && vblank_work->stream->link) {
1261 if (vblank_work->enable) {
1262 if (vblank_work->stream->link->psr_settings.psr_allow_active)
1263 amdgpu_dm_psr_disable(vblank_work->stream);
1264 } else if (vblank_work->stream->link->psr_settings.psr_feature_enabled &&
1265 !vblank_work->stream->link->psr_settings.psr_allow_active &&
1266 vblank_work->acrtc->dm_irq_params.allow_psr_entry) {
1267 amdgpu_dm_psr_enable(vblank_work->stream);
1271 mutex_unlock(&dm->dc_lock);
1273 dc_stream_release(vblank_work->stream);
1278 static void dm_handle_hpd_rx_offload_work(struct work_struct *work)
1280 struct hpd_rx_irq_offload_work *offload_work;
1281 struct amdgpu_dm_connector *aconnector;
1282 struct dc_link *dc_link;
1283 struct amdgpu_device *adev;
1284 enum dc_connection_type new_connection_type = dc_connection_none;
1285 unsigned long flags;
1287 offload_work = container_of(work, struct hpd_rx_irq_offload_work, work);
1288 aconnector = offload_work->offload_wq->aconnector;
1291 DRM_ERROR("Can't retrieve aconnector in hpd_rx_irq_offload_work");
1295 adev = drm_to_adev(aconnector->base.dev);
1296 dc_link = aconnector->dc_link;
1298 mutex_lock(&aconnector->hpd_lock);
1299 if (!dc_link_detect_sink(dc_link, &new_connection_type))
1300 DRM_ERROR("KMS: Failed to detect connector\n");
1301 mutex_unlock(&aconnector->hpd_lock);
1303 if (new_connection_type == dc_connection_none)
1306 if (amdgpu_in_reset(adev))
1309 mutex_lock(&adev->dm.dc_lock);
1310 if (offload_work->data.bytes.device_service_irq.bits.AUTOMATED_TEST)
1311 dc_link_dp_handle_automated_test(dc_link);
1312 else if ((dc_link->connector_signal != SIGNAL_TYPE_EDP) &&
1313 hpd_rx_irq_check_link_loss_status(dc_link, &offload_work->data) &&
1314 dc_link_dp_allow_hpd_rx_irq(dc_link)) {
1315 dc_link_dp_handle_link_loss(dc_link);
1316 spin_lock_irqsave(&offload_work->offload_wq->offload_lock, flags);
1317 offload_work->offload_wq->is_handling_link_loss = false;
1318 spin_unlock_irqrestore(&offload_work->offload_wq->offload_lock, flags);
1320 mutex_unlock(&adev->dm.dc_lock);
1323 kfree(offload_work);
1327 static struct hpd_rx_irq_offload_work_queue *hpd_rx_irq_create_workqueue(struct dc *dc)
1329 int max_caps = dc->caps.max_links;
1331 struct hpd_rx_irq_offload_work_queue *hpd_rx_offload_wq = NULL;
1333 hpd_rx_offload_wq = kcalloc(max_caps, sizeof(*hpd_rx_offload_wq), GFP_KERNEL);
1335 if (!hpd_rx_offload_wq)
1339 for (i = 0; i < max_caps; i++) {
1340 hpd_rx_offload_wq[i].wq =
1341 create_singlethread_workqueue("amdgpu_dm_hpd_rx_offload_wq");
1343 if (hpd_rx_offload_wq[i].wq == NULL) {
1344 DRM_ERROR("create amdgpu_dm_hpd_rx_offload_wq fail!");
1348 spin_lock_init(&hpd_rx_offload_wq[i].offload_lock);
1351 return hpd_rx_offload_wq;
1354 struct amdgpu_stutter_quirk {
1362 static const struct amdgpu_stutter_quirk amdgpu_stutter_quirk_list[] = {
1363 /* https://bugzilla.kernel.org/show_bug.cgi?id=214417 */
1364 { 0x1002, 0x15dd, 0x1002, 0x15dd, 0xc8 },
1368 static bool dm_should_disable_stutter(struct pci_dev *pdev)
1370 const struct amdgpu_stutter_quirk *p = amdgpu_stutter_quirk_list;
1372 while (p && p->chip_device != 0) {
1373 if (pdev->vendor == p->chip_vendor &&
1374 pdev->device == p->chip_device &&
1375 pdev->subsystem_vendor == p->subsys_vendor &&
1376 pdev->subsystem_device == p->subsys_device &&
1377 pdev->revision == p->revision) {
1385 static int amdgpu_dm_init(struct amdgpu_device *adev)
1387 struct dc_init_data init_data;
1388 #ifdef CONFIG_DRM_AMD_DC_HDCP
1389 struct dc_callback_init init_params;
1393 adev->dm.ddev = adev_to_drm(adev);
1394 adev->dm.adev = adev;
1396 /* Zero all the fields */
1397 memset(&init_data, 0, sizeof(init_data));
1398 #ifdef CONFIG_DRM_AMD_DC_HDCP
1399 memset(&init_params, 0, sizeof(init_params));
1402 mutex_init(&adev->dm.dc_lock);
1403 mutex_init(&adev->dm.audio_lock);
1404 spin_lock_init(&adev->dm.vblank_lock);
1406 if(amdgpu_dm_irq_init(adev)) {
1407 DRM_ERROR("amdgpu: failed to initialize DM IRQ support.\n");
1411 init_data.asic_id.chip_family = adev->family;
1413 init_data.asic_id.pci_revision_id = adev->pdev->revision;
1414 init_data.asic_id.hw_internal_rev = adev->external_rev_id;
1415 init_data.asic_id.chip_id = adev->pdev->device;
1417 init_data.asic_id.vram_width = adev->gmc.vram_width;
1418 /* TODO: initialize init_data.asic_id.vram_type here!!!! */
1419 init_data.asic_id.atombios_base_address =
1420 adev->mode_info.atom_context->bios;
1422 init_data.driver = adev;
1424 adev->dm.cgs_device = amdgpu_cgs_create_device(adev);
1426 if (!adev->dm.cgs_device) {
1427 DRM_ERROR("amdgpu: failed to create cgs device.\n");
1431 init_data.cgs_device = adev->dm.cgs_device;
1433 init_data.dce_environment = DCE_ENV_PRODUCTION_DRV;
1435 switch (adev->ip_versions[DCE_HWIP][0]) {
1436 case IP_VERSION(2, 1, 0):
1437 switch (adev->dm.dmcub_fw_version) {
1438 case 0: /* development */
1439 case 0x1: /* linux-firmware.git hash 6d9f399 */
1440 case 0x01000000: /* linux-firmware.git hash 9a0b0f4 */
1441 init_data.flags.disable_dmcu = false;
1444 init_data.flags.disable_dmcu = true;
1447 case IP_VERSION(2, 0, 3):
1448 init_data.flags.disable_dmcu = true;
1454 switch (adev->asic_type) {
1457 init_data.flags.gpu_vm_support = true;
1460 switch (adev->ip_versions[DCE_HWIP][0]) {
1461 case IP_VERSION(1, 0, 0):
1462 case IP_VERSION(1, 0, 1):
1463 /* enable S/G on PCO and RV2 */
1464 if ((adev->apu_flags & AMD_APU_IS_RAVEN2) ||
1465 (adev->apu_flags & AMD_APU_IS_PICASSO))
1466 init_data.flags.gpu_vm_support = true;
1468 case IP_VERSION(2, 1, 0):
1469 case IP_VERSION(3, 0, 1):
1470 case IP_VERSION(3, 1, 2):
1471 case IP_VERSION(3, 1, 3):
1472 case IP_VERSION(3, 1, 5):
1473 case IP_VERSION(3, 1, 6):
1474 init_data.flags.gpu_vm_support = true;
1482 if (init_data.flags.gpu_vm_support)
1483 adev->mode_info.gpu_vm_support = true;
1485 if (amdgpu_dc_feature_mask & DC_FBC_MASK)
1486 init_data.flags.fbc_support = true;
1488 if (amdgpu_dc_feature_mask & DC_MULTI_MON_PP_MCLK_SWITCH_MASK)
1489 init_data.flags.multi_mon_pp_mclk_switch = true;
1491 if (amdgpu_dc_feature_mask & DC_DISABLE_FRACTIONAL_PWM_MASK)
1492 init_data.flags.disable_fractional_pwm = true;
1494 if (amdgpu_dc_feature_mask & DC_EDP_NO_POWER_SEQUENCING)
1495 init_data.flags.edp_no_power_sequencing = true;
1497 if (amdgpu_dc_feature_mask & DC_DISABLE_LTTPR_DP1_4A)
1498 init_data.flags.allow_lttpr_non_transparent_mode.bits.DP1_4A = true;
1499 if (amdgpu_dc_feature_mask & DC_DISABLE_LTTPR_DP2_0)
1500 init_data.flags.allow_lttpr_non_transparent_mode.bits.DP2_0 = true;
1502 init_data.flags.seamless_boot_edp_requested = false;
1504 if (check_seamless_boot_capability(adev)) {
1505 init_data.flags.seamless_boot_edp_requested = true;
1506 init_data.flags.allow_seamless_boot_optimization = true;
1507 DRM_INFO("Seamless boot condition check passed\n");
1510 INIT_LIST_HEAD(&adev->dm.da_list);
1511 /* Display Core create. */
1512 adev->dm.dc = dc_create(&init_data);
1515 DRM_INFO("Display Core initialized with v%s!\n", DC_VER);
1517 DRM_INFO("Display Core failed to initialize with v%s!\n", DC_VER);
1521 if (amdgpu_dc_debug_mask & DC_DISABLE_PIPE_SPLIT) {
1522 adev->dm.dc->debug.force_single_disp_pipe_split = false;
1523 adev->dm.dc->debug.pipe_split_policy = MPC_SPLIT_AVOID;
1526 if (adev->asic_type != CHIP_CARRIZO && adev->asic_type != CHIP_STONEY)
1527 adev->dm.dc->debug.disable_stutter = amdgpu_pp_feature_mask & PP_STUTTER_MODE ? false : true;
1528 if (dm_should_disable_stutter(adev->pdev))
1529 adev->dm.dc->debug.disable_stutter = true;
1531 if (amdgpu_dc_debug_mask & DC_DISABLE_STUTTER)
1532 adev->dm.dc->debug.disable_stutter = true;
1534 if (amdgpu_dc_debug_mask & DC_DISABLE_DSC) {
1535 adev->dm.dc->debug.disable_dsc = true;
1536 adev->dm.dc->debug.disable_dsc_edp = true;
1539 if (amdgpu_dc_debug_mask & DC_DISABLE_CLOCK_GATING)
1540 adev->dm.dc->debug.disable_clock_gate = true;
1542 r = dm_dmub_hw_init(adev);
1544 DRM_ERROR("DMUB interface failed to initialize: status=%d\n", r);
1548 dc_hardware_init(adev->dm.dc);
1550 adev->dm.hpd_rx_offload_wq = hpd_rx_irq_create_workqueue(adev->dm.dc);
1551 if (!adev->dm.hpd_rx_offload_wq) {
1552 DRM_ERROR("amdgpu: failed to create hpd rx offload workqueue.\n");
1556 if ((adev->flags & AMD_IS_APU) && (adev->asic_type >= CHIP_CARRIZO)) {
1557 struct dc_phy_addr_space_config pa_config;
1559 mmhub_read_system_context(adev, &pa_config);
1561 // Call the DC init_memory func
1562 dc_setup_system_context(adev->dm.dc, &pa_config);
1565 adev->dm.freesync_module = mod_freesync_create(adev->dm.dc);
1566 if (!adev->dm.freesync_module) {
1568 "amdgpu: failed to initialize freesync_module.\n");
1570 DRM_DEBUG_DRIVER("amdgpu: freesync_module init done %p.\n",
1571 adev->dm.freesync_module);
1573 amdgpu_dm_init_color_mod();
1575 if (adev->dm.dc->caps.max_links > 0) {
1576 adev->dm.vblank_control_workqueue =
1577 create_singlethread_workqueue("dm_vblank_control_workqueue");
1578 if (!adev->dm.vblank_control_workqueue)
1579 DRM_ERROR("amdgpu: failed to initialize vblank_workqueue.\n");
1582 #ifdef CONFIG_DRM_AMD_DC_HDCP
1583 if (adev->dm.dc->caps.max_links > 0 && adev->family >= AMDGPU_FAMILY_RV) {
1584 adev->dm.hdcp_workqueue = hdcp_create_workqueue(adev, &init_params.cp_psp, adev->dm.dc);
1586 if (!adev->dm.hdcp_workqueue)
1587 DRM_ERROR("amdgpu: failed to initialize hdcp_workqueue.\n");
1589 DRM_DEBUG_DRIVER("amdgpu: hdcp_workqueue init done %p.\n", adev->dm.hdcp_workqueue);
1591 dc_init_callbacks(adev->dm.dc, &init_params);
1594 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
1595 adev->dm.crc_rd_wrk = amdgpu_dm_crtc_secure_display_create_work();
1597 if (dc_enable_dmub_notifications(adev->dm.dc)) {
1598 init_completion(&adev->dm.dmub_aux_transfer_done);
1599 adev->dm.dmub_notify = kzalloc(sizeof(struct dmub_notification), GFP_KERNEL);
1600 if (!adev->dm.dmub_notify) {
1601 DRM_INFO("amdgpu: fail to allocate adev->dm.dmub_notify");
1605 adev->dm.delayed_hpd_wq = create_singlethread_workqueue("amdgpu_dm_hpd_wq");
1606 if (!adev->dm.delayed_hpd_wq) {
1607 DRM_ERROR("amdgpu: failed to create hpd offload workqueue.\n");
1611 amdgpu_dm_outbox_init(adev);
1612 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_AUX_REPLY,
1613 dmub_aux_setconfig_callback, false)) {
1614 DRM_ERROR("amdgpu: fail to register dmub aux callback");
1617 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_HPD, dmub_hpd_callback, true)) {
1618 DRM_ERROR("amdgpu: fail to register dmub hpd callback");
1621 if (!register_dmub_notify_callback(adev, DMUB_NOTIFICATION_HPD_IRQ, dmub_hpd_callback, true)) {
1622 DRM_ERROR("amdgpu: fail to register dmub hpd callback");
1627 if (amdgpu_dm_initialize_drm_device(adev)) {
1629 "amdgpu: failed to initialize sw for display support.\n");
1633 /* create fake encoders for MST */
1634 dm_dp_create_fake_mst_encoders(adev);
1636 /* TODO: Add_display_info? */
1638 /* TODO use dynamic cursor width */
1639 adev_to_drm(adev)->mode_config.cursor_width = adev->dm.dc->caps.max_cursor_size;
1640 adev_to_drm(adev)->mode_config.cursor_height = adev->dm.dc->caps.max_cursor_size;
1642 if (drm_vblank_init(adev_to_drm(adev), adev->dm.display_indexes_num)) {
1644 "amdgpu: failed to initialize sw for display support.\n");
1649 DRM_DEBUG_DRIVER("KMS initialized.\n");
1653 amdgpu_dm_fini(adev);
1658 static int amdgpu_dm_early_fini(void *handle)
1660 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1662 amdgpu_dm_audio_fini(adev);
1667 static void amdgpu_dm_fini(struct amdgpu_device *adev)
1671 if (adev->dm.vblank_control_workqueue) {
1672 destroy_workqueue(adev->dm.vblank_control_workqueue);
1673 adev->dm.vblank_control_workqueue = NULL;
1676 for (i = 0; i < adev->dm.display_indexes_num; i++) {
1677 drm_encoder_cleanup(&adev->dm.mst_encoders[i].base);
1680 amdgpu_dm_destroy_drm_device(&adev->dm);
1682 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
1683 if (adev->dm.crc_rd_wrk) {
1684 flush_work(&adev->dm.crc_rd_wrk->notify_ta_work);
1685 kfree(adev->dm.crc_rd_wrk);
1686 adev->dm.crc_rd_wrk = NULL;
1689 #ifdef CONFIG_DRM_AMD_DC_HDCP
1690 if (adev->dm.hdcp_workqueue) {
1691 hdcp_destroy(&adev->dev->kobj, adev->dm.hdcp_workqueue);
1692 adev->dm.hdcp_workqueue = NULL;
1696 dc_deinit_callbacks(adev->dm.dc);
1699 dc_dmub_srv_destroy(&adev->dm.dc->ctx->dmub_srv);
1701 if (dc_enable_dmub_notifications(adev->dm.dc)) {
1702 kfree(adev->dm.dmub_notify);
1703 adev->dm.dmub_notify = NULL;
1704 destroy_workqueue(adev->dm.delayed_hpd_wq);
1705 adev->dm.delayed_hpd_wq = NULL;
1708 if (adev->dm.dmub_bo)
1709 amdgpu_bo_free_kernel(&adev->dm.dmub_bo,
1710 &adev->dm.dmub_bo_gpu_addr,
1711 &adev->dm.dmub_bo_cpu_addr);
1713 if (adev->dm.hpd_rx_offload_wq) {
1714 for (i = 0; i < adev->dm.dc->caps.max_links; i++) {
1715 if (adev->dm.hpd_rx_offload_wq[i].wq) {
1716 destroy_workqueue(adev->dm.hpd_rx_offload_wq[i].wq);
1717 adev->dm.hpd_rx_offload_wq[i].wq = NULL;
1721 kfree(adev->dm.hpd_rx_offload_wq);
1722 adev->dm.hpd_rx_offload_wq = NULL;
1725 /* DC Destroy TODO: Replace destroy DAL */
1727 dc_destroy(&adev->dm.dc);
1729 * TODO: pageflip, vlank interrupt
1731 * amdgpu_dm_irq_fini(adev);
1734 if (adev->dm.cgs_device) {
1735 amdgpu_cgs_destroy_device(adev->dm.cgs_device);
1736 adev->dm.cgs_device = NULL;
1738 if (adev->dm.freesync_module) {
1739 mod_freesync_destroy(adev->dm.freesync_module);
1740 adev->dm.freesync_module = NULL;
1743 mutex_destroy(&adev->dm.audio_lock);
1744 mutex_destroy(&adev->dm.dc_lock);
1749 static int load_dmcu_fw(struct amdgpu_device *adev)
1751 const char *fw_name_dmcu = NULL;
1753 const struct dmcu_firmware_header_v1_0 *hdr;
1755 switch(adev->asic_type) {
1756 #if defined(CONFIG_DRM_AMD_DC_SI)
1771 case CHIP_POLARIS11:
1772 case CHIP_POLARIS10:
1773 case CHIP_POLARIS12:
1780 fw_name_dmcu = FIRMWARE_NAVI12_DMCU;
1783 if (ASICREV_IS_PICASSO(adev->external_rev_id))
1784 fw_name_dmcu = FIRMWARE_RAVEN_DMCU;
1785 else if (ASICREV_IS_RAVEN2(adev->external_rev_id))
1786 fw_name_dmcu = FIRMWARE_RAVEN_DMCU;
1791 switch (adev->ip_versions[DCE_HWIP][0]) {
1792 case IP_VERSION(2, 0, 2):
1793 case IP_VERSION(2, 0, 3):
1794 case IP_VERSION(2, 0, 0):
1795 case IP_VERSION(2, 1, 0):
1796 case IP_VERSION(3, 0, 0):
1797 case IP_VERSION(3, 0, 2):
1798 case IP_VERSION(3, 0, 3):
1799 case IP_VERSION(3, 0, 1):
1800 case IP_VERSION(3, 1, 2):
1801 case IP_VERSION(3, 1, 3):
1802 case IP_VERSION(3, 1, 5):
1803 case IP_VERSION(3, 1, 6):
1808 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type);
1812 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1813 DRM_DEBUG_KMS("dm: DMCU firmware not supported on direct or SMU loading\n");
1817 r = request_firmware_direct(&adev->dm.fw_dmcu, fw_name_dmcu, adev->dev);
1819 /* DMCU firmware is not necessary, so don't raise a fuss if it's missing */
1820 DRM_DEBUG_KMS("dm: DMCU firmware not found\n");
1821 adev->dm.fw_dmcu = NULL;
1825 dev_err(adev->dev, "amdgpu_dm: Can't load firmware \"%s\"\n",
1830 r = amdgpu_ucode_validate(adev->dm.fw_dmcu);
1832 dev_err(adev->dev, "amdgpu_dm: Can't validate firmware \"%s\"\n",
1834 release_firmware(adev->dm.fw_dmcu);
1835 adev->dm.fw_dmcu = NULL;
1839 hdr = (const struct dmcu_firmware_header_v1_0 *)adev->dm.fw_dmcu->data;
1840 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].ucode_id = AMDGPU_UCODE_ID_DMCU_ERAM;
1841 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].fw = adev->dm.fw_dmcu;
1842 adev->firmware.fw_size +=
1843 ALIGN(le32_to_cpu(hdr->header.ucode_size_bytes) - le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE);
1845 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].ucode_id = AMDGPU_UCODE_ID_DMCU_INTV;
1846 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].fw = adev->dm.fw_dmcu;
1847 adev->firmware.fw_size +=
1848 ALIGN(le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE);
1850 adev->dm.dmcu_fw_version = le32_to_cpu(hdr->header.ucode_version);
1852 DRM_DEBUG_KMS("PSP loading DMCU firmware\n");
1857 static uint32_t amdgpu_dm_dmub_reg_read(void *ctx, uint32_t address)
1859 struct amdgpu_device *adev = ctx;
1861 return dm_read_reg(adev->dm.dc->ctx, address);
1864 static void amdgpu_dm_dmub_reg_write(void *ctx, uint32_t address,
1867 struct amdgpu_device *adev = ctx;
1869 return dm_write_reg(adev->dm.dc->ctx, address, value);
1872 static int dm_dmub_sw_init(struct amdgpu_device *adev)
1874 struct dmub_srv_create_params create_params;
1875 struct dmub_srv_region_params region_params;
1876 struct dmub_srv_region_info region_info;
1877 struct dmub_srv_fb_params fb_params;
1878 struct dmub_srv_fb_info *fb_info;
1879 struct dmub_srv *dmub_srv;
1880 const struct dmcub_firmware_header_v1_0 *hdr;
1881 const char *fw_name_dmub;
1882 enum dmub_asic dmub_asic;
1883 enum dmub_status status;
1886 switch (adev->ip_versions[DCE_HWIP][0]) {
1887 case IP_VERSION(2, 1, 0):
1888 dmub_asic = DMUB_ASIC_DCN21;
1889 fw_name_dmub = FIRMWARE_RENOIR_DMUB;
1890 if (ASICREV_IS_GREEN_SARDINE(adev->external_rev_id))
1891 fw_name_dmub = FIRMWARE_GREEN_SARDINE_DMUB;
1893 case IP_VERSION(3, 0, 0):
1894 if (adev->ip_versions[GC_HWIP][0] == IP_VERSION(10, 3, 0)) {
1895 dmub_asic = DMUB_ASIC_DCN30;
1896 fw_name_dmub = FIRMWARE_SIENNA_CICHLID_DMUB;
1898 dmub_asic = DMUB_ASIC_DCN30;
1899 fw_name_dmub = FIRMWARE_NAVY_FLOUNDER_DMUB;
1902 case IP_VERSION(3, 0, 1):
1903 dmub_asic = DMUB_ASIC_DCN301;
1904 fw_name_dmub = FIRMWARE_VANGOGH_DMUB;
1906 case IP_VERSION(3, 0, 2):
1907 dmub_asic = DMUB_ASIC_DCN302;
1908 fw_name_dmub = FIRMWARE_DIMGREY_CAVEFISH_DMUB;
1910 case IP_VERSION(3, 0, 3):
1911 dmub_asic = DMUB_ASIC_DCN303;
1912 fw_name_dmub = FIRMWARE_BEIGE_GOBY_DMUB;
1914 case IP_VERSION(3, 1, 2):
1915 case IP_VERSION(3, 1, 3):
1916 dmub_asic = (adev->external_rev_id == YELLOW_CARP_B0) ? DMUB_ASIC_DCN31B : DMUB_ASIC_DCN31;
1917 fw_name_dmub = FIRMWARE_YELLOW_CARP_DMUB;
1919 case IP_VERSION(3, 1, 5):
1920 dmub_asic = DMUB_ASIC_DCN315;
1921 fw_name_dmub = FIRMWARE_DCN_315_DMUB;
1923 case IP_VERSION(3, 1, 6):
1924 dmub_asic = DMUB_ASIC_DCN316;
1925 fw_name_dmub = FIRMWARE_DCN316_DMUB;
1928 /* ASIC doesn't support DMUB. */
1932 r = request_firmware_direct(&adev->dm.dmub_fw, fw_name_dmub, adev->dev);
1934 DRM_ERROR("DMUB firmware loading failed: %d\n", r);
1938 r = amdgpu_ucode_validate(adev->dm.dmub_fw);
1940 DRM_ERROR("Couldn't validate DMUB firmware: %d\n", r);
1944 hdr = (const struct dmcub_firmware_header_v1_0 *)adev->dm.dmub_fw->data;
1945 adev->dm.dmcub_fw_version = le32_to_cpu(hdr->header.ucode_version);
1947 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
1948 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCUB].ucode_id =
1949 AMDGPU_UCODE_ID_DMCUB;
1950 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCUB].fw =
1952 adev->firmware.fw_size +=
1953 ALIGN(le32_to_cpu(hdr->inst_const_bytes), PAGE_SIZE);
1955 DRM_INFO("Loading DMUB firmware via PSP: version=0x%08X\n",
1956 adev->dm.dmcub_fw_version);
1960 adev->dm.dmub_srv = kzalloc(sizeof(*adev->dm.dmub_srv), GFP_KERNEL);
1961 dmub_srv = adev->dm.dmub_srv;
1964 DRM_ERROR("Failed to allocate DMUB service!\n");
1968 memset(&create_params, 0, sizeof(create_params));
1969 create_params.user_ctx = adev;
1970 create_params.funcs.reg_read = amdgpu_dm_dmub_reg_read;
1971 create_params.funcs.reg_write = amdgpu_dm_dmub_reg_write;
1972 create_params.asic = dmub_asic;
1974 /* Create the DMUB service. */
1975 status = dmub_srv_create(dmub_srv, &create_params);
1976 if (status != DMUB_STATUS_OK) {
1977 DRM_ERROR("Error creating DMUB service: %d\n", status);
1981 /* Calculate the size of all the regions for the DMUB service. */
1982 memset(®ion_params, 0, sizeof(region_params));
1984 region_params.inst_const_size = le32_to_cpu(hdr->inst_const_bytes) -
1985 PSP_HEADER_BYTES - PSP_FOOTER_BYTES;
1986 region_params.bss_data_size = le32_to_cpu(hdr->bss_data_bytes);
1987 region_params.vbios_size = adev->bios_size;
1988 region_params.fw_bss_data = region_params.bss_data_size ?
1989 adev->dm.dmub_fw->data +
1990 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
1991 le32_to_cpu(hdr->inst_const_bytes) : NULL;
1992 region_params.fw_inst_const =
1993 adev->dm.dmub_fw->data +
1994 le32_to_cpu(hdr->header.ucode_array_offset_bytes) +
1997 status = dmub_srv_calc_region_info(dmub_srv, ®ion_params,
2000 if (status != DMUB_STATUS_OK) {
2001 DRM_ERROR("Error calculating DMUB region info: %d\n", status);
2006 * Allocate a framebuffer based on the total size of all the regions.
2007 * TODO: Move this into GART.
2009 r = amdgpu_bo_create_kernel(adev, region_info.fb_size, PAGE_SIZE,
2010 AMDGPU_GEM_DOMAIN_VRAM, &adev->dm.dmub_bo,
2011 &adev->dm.dmub_bo_gpu_addr,
2012 &adev->dm.dmub_bo_cpu_addr);
2016 /* Rebase the regions on the framebuffer address. */
2017 memset(&fb_params, 0, sizeof(fb_params));
2018 fb_params.cpu_addr = adev->dm.dmub_bo_cpu_addr;
2019 fb_params.gpu_addr = adev->dm.dmub_bo_gpu_addr;
2020 fb_params.region_info = ®ion_info;
2022 adev->dm.dmub_fb_info =
2023 kzalloc(sizeof(*adev->dm.dmub_fb_info), GFP_KERNEL);
2024 fb_info = adev->dm.dmub_fb_info;
2028 "Failed to allocate framebuffer info for DMUB service!\n");
2032 status = dmub_srv_calc_fb_info(dmub_srv, &fb_params, fb_info);
2033 if (status != DMUB_STATUS_OK) {
2034 DRM_ERROR("Error calculating DMUB FB info: %d\n", status);
2041 static int dm_sw_init(void *handle)
2043 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2046 r = dm_dmub_sw_init(adev);
2050 return load_dmcu_fw(adev);
2053 static int dm_sw_fini(void *handle)
2055 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2057 kfree(adev->dm.dmub_fb_info);
2058 adev->dm.dmub_fb_info = NULL;
2060 if (adev->dm.dmub_srv) {
2061 dmub_srv_destroy(adev->dm.dmub_srv);
2062 adev->dm.dmub_srv = NULL;
2065 release_firmware(adev->dm.dmub_fw);
2066 adev->dm.dmub_fw = NULL;
2068 release_firmware(adev->dm.fw_dmcu);
2069 adev->dm.fw_dmcu = NULL;
2074 static int detect_mst_link_for_all_connectors(struct drm_device *dev)
2076 struct amdgpu_dm_connector *aconnector;
2077 struct drm_connector *connector;
2078 struct drm_connector_list_iter iter;
2081 drm_connector_list_iter_begin(dev, &iter);
2082 drm_for_each_connector_iter(connector, &iter) {
2083 aconnector = to_amdgpu_dm_connector(connector);
2084 if (aconnector->dc_link->type == dc_connection_mst_branch &&
2085 aconnector->mst_mgr.aux) {
2086 DRM_DEBUG_DRIVER("DM_MST: starting TM on aconnector: %p [id: %d]\n",
2088 aconnector->base.base.id);
2090 ret = drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true);
2092 DRM_ERROR("DM_MST: Failed to start MST\n");
2093 aconnector->dc_link->type =
2094 dc_connection_single;
2099 drm_connector_list_iter_end(&iter);
2104 static int dm_late_init(void *handle)
2106 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2108 struct dmcu_iram_parameters params;
2109 unsigned int linear_lut[16];
2111 struct dmcu *dmcu = NULL;
2113 dmcu = adev->dm.dc->res_pool->dmcu;
2115 for (i = 0; i < 16; i++)
2116 linear_lut[i] = 0xFFFF * i / 15;
2119 params.backlight_ramping_override = false;
2120 params.backlight_ramping_start = 0xCCCC;
2121 params.backlight_ramping_reduction = 0xCCCCCCCC;
2122 params.backlight_lut_array_size = 16;
2123 params.backlight_lut_array = linear_lut;
2125 /* Min backlight level after ABM reduction, Don't allow below 1%
2126 * 0xFFFF x 0.01 = 0x28F
2128 params.min_abm_backlight = 0x28F;
2129 /* In the case where abm is implemented on dmcub,
2130 * dmcu object will be null.
2131 * ABM 2.4 and up are implemented on dmcub.
2134 if (!dmcu_load_iram(dmcu, params))
2136 } else if (adev->dm.dc->ctx->dmub_srv) {
2137 struct dc_link *edp_links[MAX_NUM_EDP];
2140 get_edp_links(adev->dm.dc, edp_links, &edp_num);
2141 for (i = 0; i < edp_num; i++) {
2142 if (!dmub_init_abm_config(adev->dm.dc->res_pool, params, i))
2147 return detect_mst_link_for_all_connectors(adev_to_drm(adev));
2150 static void s3_handle_mst(struct drm_device *dev, bool suspend)
2152 struct amdgpu_dm_connector *aconnector;
2153 struct drm_connector *connector;
2154 struct drm_connector_list_iter iter;
2155 struct drm_dp_mst_topology_mgr *mgr;
2157 bool need_hotplug = false;
2159 drm_connector_list_iter_begin(dev, &iter);
2160 drm_for_each_connector_iter(connector, &iter) {
2161 aconnector = to_amdgpu_dm_connector(connector);
2162 if (aconnector->dc_link->type != dc_connection_mst_branch ||
2163 aconnector->mst_port)
2166 mgr = &aconnector->mst_mgr;
2169 drm_dp_mst_topology_mgr_suspend(mgr);
2171 ret = drm_dp_mst_topology_mgr_resume(mgr, true);
2173 drm_dp_mst_topology_mgr_set_mst(mgr, false);
2174 need_hotplug = true;
2178 drm_connector_list_iter_end(&iter);
2181 drm_kms_helper_hotplug_event(dev);
2184 static int amdgpu_dm_smu_write_watermarks_table(struct amdgpu_device *adev)
2188 /* This interface is for dGPU Navi1x.Linux dc-pplib interface depends
2189 * on window driver dc implementation.
2190 * For Navi1x, clock settings of dcn watermarks are fixed. the settings
2191 * should be passed to smu during boot up and resume from s3.
2192 * boot up: dc calculate dcn watermark clock settings within dc_create,
2193 * dcn20_resource_construct
2194 * then call pplib functions below to pass the settings to smu:
2195 * smu_set_watermarks_for_clock_ranges
2196 * smu_set_watermarks_table
2197 * navi10_set_watermarks_table
2198 * smu_write_watermarks_table
2200 * For Renoir, clock settings of dcn watermark are also fixed values.
2201 * dc has implemented different flow for window driver:
2202 * dc_hardware_init / dc_set_power_state
2207 * smu_set_watermarks_for_clock_ranges
2208 * renoir_set_watermarks_table
2209 * smu_write_watermarks_table
2212 * dc_hardware_init -> amdgpu_dm_init
2213 * dc_set_power_state --> dm_resume
2215 * therefore, this function apply to navi10/12/14 but not Renoir
2218 switch (adev->ip_versions[DCE_HWIP][0]) {
2219 case IP_VERSION(2, 0, 2):
2220 case IP_VERSION(2, 0, 0):
2226 ret = amdgpu_dpm_write_watermarks_table(adev);
2228 DRM_ERROR("Failed to update WMTABLE!\n");
2236 * dm_hw_init() - Initialize DC device
2237 * @handle: The base driver device containing the amdgpu_dm device.
2239 * Initialize the &struct amdgpu_display_manager device. This involves calling
2240 * the initializers of each DM component, then populating the struct with them.
2242 * Although the function implies hardware initialization, both hardware and
2243 * software are initialized here. Splitting them out to their relevant init
2244 * hooks is a future TODO item.
2246 * Some notable things that are initialized here:
2248 * - Display Core, both software and hardware
2249 * - DC modules that we need (freesync and color management)
2250 * - DRM software states
2251 * - Interrupt sources and handlers
2253 * - Debug FS entries, if enabled
2255 static int dm_hw_init(void *handle)
2257 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2258 /* Create DAL display manager */
2259 amdgpu_dm_init(adev);
2260 amdgpu_dm_hpd_init(adev);
2266 * dm_hw_fini() - Teardown DC device
2267 * @handle: The base driver device containing the amdgpu_dm device.
2269 * Teardown components within &struct amdgpu_display_manager that require
2270 * cleanup. This involves cleaning up the DRM device, DC, and any modules that
2271 * were loaded. Also flush IRQ workqueues and disable them.
2273 static int dm_hw_fini(void *handle)
2275 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2277 amdgpu_dm_hpd_fini(adev);
2279 amdgpu_dm_irq_fini(adev);
2280 amdgpu_dm_fini(adev);
2285 static int dm_enable_vblank(struct drm_crtc *crtc);
2286 static void dm_disable_vblank(struct drm_crtc *crtc);
2288 static void dm_gpureset_toggle_interrupts(struct amdgpu_device *adev,
2289 struct dc_state *state, bool enable)
2291 enum dc_irq_source irq_source;
2292 struct amdgpu_crtc *acrtc;
2296 for (i = 0; i < state->stream_count; i++) {
2297 acrtc = get_crtc_by_otg_inst(
2298 adev, state->stream_status[i].primary_otg_inst);
2300 if (acrtc && state->stream_status[i].plane_count != 0) {
2301 irq_source = IRQ_TYPE_PFLIP + acrtc->otg_inst;
2302 rc = dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY;
2303 DRM_DEBUG_VBL("crtc %d - vupdate irq %sabling: r=%d\n",
2304 acrtc->crtc_id, enable ? "en" : "dis", rc);
2306 DRM_WARN("Failed to %s pflip interrupts\n",
2307 enable ? "enable" : "disable");
2310 rc = dm_enable_vblank(&acrtc->base);
2312 DRM_WARN("Failed to enable vblank interrupts\n");
2314 dm_disable_vblank(&acrtc->base);
2322 static enum dc_status amdgpu_dm_commit_zero_streams(struct dc *dc)
2324 struct dc_state *context = NULL;
2325 enum dc_status res = DC_ERROR_UNEXPECTED;
2327 struct dc_stream_state *del_streams[MAX_PIPES];
2328 int del_streams_count = 0;
2330 memset(del_streams, 0, sizeof(del_streams));
2332 context = dc_create_state(dc);
2333 if (context == NULL)
2334 goto context_alloc_fail;
2336 dc_resource_state_copy_construct_current(dc, context);
2338 /* First remove from context all streams */
2339 for (i = 0; i < context->stream_count; i++) {
2340 struct dc_stream_state *stream = context->streams[i];
2342 del_streams[del_streams_count++] = stream;
2345 /* Remove all planes for removed streams and then remove the streams */
2346 for (i = 0; i < del_streams_count; i++) {
2347 if (!dc_rem_all_planes_for_stream(dc, del_streams[i], context)) {
2348 res = DC_FAIL_DETACH_SURFACES;
2352 res = dc_remove_stream_from_ctx(dc, context, del_streams[i]);
2357 res = dc_commit_state(dc, context);
2360 dc_release_state(context);
2366 static void hpd_rx_irq_work_suspend(struct amdgpu_display_manager *dm)
2370 if (dm->hpd_rx_offload_wq) {
2371 for (i = 0; i < dm->dc->caps.max_links; i++)
2372 flush_workqueue(dm->hpd_rx_offload_wq[i].wq);
2376 static int dm_suspend(void *handle)
2378 struct amdgpu_device *adev = handle;
2379 struct amdgpu_display_manager *dm = &adev->dm;
2382 if (amdgpu_in_reset(adev)) {
2383 mutex_lock(&dm->dc_lock);
2385 dc_allow_idle_optimizations(adev->dm.dc, false);
2387 dm->cached_dc_state = dc_copy_state(dm->dc->current_state);
2389 dm_gpureset_toggle_interrupts(adev, dm->cached_dc_state, false);
2391 amdgpu_dm_commit_zero_streams(dm->dc);
2393 amdgpu_dm_irq_suspend(adev);
2395 hpd_rx_irq_work_suspend(dm);
2400 WARN_ON(adev->dm.cached_state);
2401 adev->dm.cached_state = drm_atomic_helper_suspend(adev_to_drm(adev));
2403 s3_handle_mst(adev_to_drm(adev), true);
2405 amdgpu_dm_irq_suspend(adev);
2407 hpd_rx_irq_work_suspend(dm);
2409 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D3);
2414 struct amdgpu_dm_connector *
2415 amdgpu_dm_find_first_crtc_matching_connector(struct drm_atomic_state *state,
2416 struct drm_crtc *crtc)
2419 struct drm_connector_state *new_con_state;
2420 struct drm_connector *connector;
2421 struct drm_crtc *crtc_from_state;
2423 for_each_new_connector_in_state(state, connector, new_con_state, i) {
2424 crtc_from_state = new_con_state->crtc;
2426 if (crtc_from_state == crtc)
2427 return to_amdgpu_dm_connector(connector);
2433 static void emulated_link_detect(struct dc_link *link)
2435 struct dc_sink_init_data sink_init_data = { 0 };
2436 struct display_sink_capability sink_caps = { 0 };
2437 enum dc_edid_status edid_status;
2438 struct dc_context *dc_ctx = link->ctx;
2439 struct dc_sink *sink = NULL;
2440 struct dc_sink *prev_sink = NULL;
2442 link->type = dc_connection_none;
2443 prev_sink = link->local_sink;
2446 dc_sink_release(prev_sink);
2448 switch (link->connector_signal) {
2449 case SIGNAL_TYPE_HDMI_TYPE_A: {
2450 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
2451 sink_caps.signal = SIGNAL_TYPE_HDMI_TYPE_A;
2455 case SIGNAL_TYPE_DVI_SINGLE_LINK: {
2456 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
2457 sink_caps.signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
2461 case SIGNAL_TYPE_DVI_DUAL_LINK: {
2462 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
2463 sink_caps.signal = SIGNAL_TYPE_DVI_DUAL_LINK;
2467 case SIGNAL_TYPE_LVDS: {
2468 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C;
2469 sink_caps.signal = SIGNAL_TYPE_LVDS;
2473 case SIGNAL_TYPE_EDP: {
2474 sink_caps.transaction_type =
2475 DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
2476 sink_caps.signal = SIGNAL_TYPE_EDP;
2480 case SIGNAL_TYPE_DISPLAY_PORT: {
2481 sink_caps.transaction_type =
2482 DDC_TRANSACTION_TYPE_I2C_OVER_AUX;
2483 sink_caps.signal = SIGNAL_TYPE_VIRTUAL;
2488 DC_ERROR("Invalid connector type! signal:%d\n",
2489 link->connector_signal);
2493 sink_init_data.link = link;
2494 sink_init_data.sink_signal = sink_caps.signal;
2496 sink = dc_sink_create(&sink_init_data);
2498 DC_ERROR("Failed to create sink!\n");
2502 /* dc_sink_create returns a new reference */
2503 link->local_sink = sink;
2505 edid_status = dm_helpers_read_local_edid(
2510 if (edid_status != EDID_OK)
2511 DC_ERROR("Failed to read EDID");
2515 static void dm_gpureset_commit_state(struct dc_state *dc_state,
2516 struct amdgpu_display_manager *dm)
2519 struct dc_surface_update surface_updates[MAX_SURFACES];
2520 struct dc_plane_info plane_infos[MAX_SURFACES];
2521 struct dc_scaling_info scaling_infos[MAX_SURFACES];
2522 struct dc_flip_addrs flip_addrs[MAX_SURFACES];
2523 struct dc_stream_update stream_update;
2527 bundle = kzalloc(sizeof(*bundle), GFP_KERNEL);
2530 dm_error("Failed to allocate update bundle\n");
2534 for (k = 0; k < dc_state->stream_count; k++) {
2535 bundle->stream_update.stream = dc_state->streams[k];
2537 for (m = 0; m < dc_state->stream_status->plane_count; m++) {
2538 bundle->surface_updates[m].surface =
2539 dc_state->stream_status->plane_states[m];
2540 bundle->surface_updates[m].surface->force_full_update =
2543 dc_commit_updates_for_stream(
2544 dm->dc, bundle->surface_updates,
2545 dc_state->stream_status->plane_count,
2546 dc_state->streams[k], &bundle->stream_update, dc_state);
2555 static void dm_set_dpms_off(struct dc_link *link, struct dm_crtc_state *acrtc_state)
2557 struct dc_stream_state *stream_state;
2558 struct amdgpu_dm_connector *aconnector = link->priv;
2559 struct amdgpu_device *adev = drm_to_adev(aconnector->base.dev);
2560 struct dc_stream_update stream_update;
2561 bool dpms_off = true;
2563 memset(&stream_update, 0, sizeof(stream_update));
2564 stream_update.dpms_off = &dpms_off;
2566 mutex_lock(&adev->dm.dc_lock);
2567 stream_state = dc_stream_find_from_link(link);
2569 if (stream_state == NULL) {
2570 DRM_DEBUG_DRIVER("Error finding stream state associated with link!\n");
2571 mutex_unlock(&adev->dm.dc_lock);
2575 stream_update.stream = stream_state;
2576 acrtc_state->force_dpms_off = true;
2577 dc_commit_updates_for_stream(stream_state->ctx->dc, NULL, 0,
2578 stream_state, &stream_update,
2579 stream_state->ctx->dc->current_state);
2580 mutex_unlock(&adev->dm.dc_lock);
2583 static int dm_resume(void *handle)
2585 struct amdgpu_device *adev = handle;
2586 struct drm_device *ddev = adev_to_drm(adev);
2587 struct amdgpu_display_manager *dm = &adev->dm;
2588 struct amdgpu_dm_connector *aconnector;
2589 struct drm_connector *connector;
2590 struct drm_connector_list_iter iter;
2591 struct drm_crtc *crtc;
2592 struct drm_crtc_state *new_crtc_state;
2593 struct dm_crtc_state *dm_new_crtc_state;
2594 struct drm_plane *plane;
2595 struct drm_plane_state *new_plane_state;
2596 struct dm_plane_state *dm_new_plane_state;
2597 struct dm_atomic_state *dm_state = to_dm_atomic_state(dm->atomic_obj.state);
2598 enum dc_connection_type new_connection_type = dc_connection_none;
2599 struct dc_state *dc_state;
2602 if (amdgpu_in_reset(adev)) {
2603 dc_state = dm->cached_dc_state;
2606 * The dc->current_state is backed up into dm->cached_dc_state
2607 * before we commit 0 streams.
2609 * DC will clear link encoder assignments on the real state
2610 * but the changes won't propagate over to the copy we made
2611 * before the 0 streams commit.
2613 * DC expects that link encoder assignments are *not* valid
2614 * when committing a state, so as a workaround we can copy
2615 * off of the current state.
2617 * We lose the previous assignments, but we had already
2618 * commit 0 streams anyway.
2620 link_enc_cfg_copy(adev->dm.dc->current_state, dc_state);
2622 if (dc_enable_dmub_notifications(adev->dm.dc))
2623 amdgpu_dm_outbox_init(adev);
2625 r = dm_dmub_hw_init(adev);
2627 DRM_ERROR("DMUB interface failed to initialize: status=%d\n", r);
2629 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0);
2632 amdgpu_dm_irq_resume_early(adev);
2634 for (i = 0; i < dc_state->stream_count; i++) {
2635 dc_state->streams[i]->mode_changed = true;
2636 for (j = 0; j < dc_state->stream_status[i].plane_count; j++) {
2637 dc_state->stream_status[i].plane_states[j]->update_flags.raw
2642 WARN_ON(!dc_commit_state(dm->dc, dc_state));
2644 dm_gpureset_commit_state(dm->cached_dc_state, dm);
2646 dm_gpureset_toggle_interrupts(adev, dm->cached_dc_state, true);
2648 dc_release_state(dm->cached_dc_state);
2649 dm->cached_dc_state = NULL;
2651 amdgpu_dm_irq_resume_late(adev);
2653 mutex_unlock(&dm->dc_lock);
2657 /* Recreate dc_state - DC invalidates it when setting power state to S3. */
2658 dc_release_state(dm_state->context);
2659 dm_state->context = dc_create_state(dm->dc);
2660 /* TODO: Remove dc_state->dccg, use dc->dccg directly. */
2661 dc_resource_state_construct(dm->dc, dm_state->context);
2663 /* Re-enable outbox interrupts for DPIA. */
2664 if (dc_enable_dmub_notifications(adev->dm.dc))
2665 amdgpu_dm_outbox_init(adev);
2667 /* Before powering on DC we need to re-initialize DMUB. */
2668 dm_dmub_hw_resume(adev);
2670 /* power on hardware */
2671 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0);
2673 /* program HPD filter */
2677 * early enable HPD Rx IRQ, should be done before set mode as short
2678 * pulse interrupts are used for MST
2680 amdgpu_dm_irq_resume_early(adev);
2682 /* On resume we need to rewrite the MSTM control bits to enable MST*/
2683 s3_handle_mst(ddev, false);
2686 drm_connector_list_iter_begin(ddev, &iter);
2687 drm_for_each_connector_iter(connector, &iter) {
2688 aconnector = to_amdgpu_dm_connector(connector);
2691 * this is the case when traversing through already created
2692 * MST connectors, should be skipped
2694 if (aconnector->dc_link &&
2695 aconnector->dc_link->type == dc_connection_mst_branch)
2698 mutex_lock(&aconnector->hpd_lock);
2699 if (!dc_link_detect_sink(aconnector->dc_link, &new_connection_type))
2700 DRM_ERROR("KMS: Failed to detect connector\n");
2702 if (aconnector->base.force && new_connection_type == dc_connection_none)
2703 emulated_link_detect(aconnector->dc_link);
2705 dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
2707 if (aconnector->fake_enable && aconnector->dc_link->local_sink)
2708 aconnector->fake_enable = false;
2710 if (aconnector->dc_sink)
2711 dc_sink_release(aconnector->dc_sink);
2712 aconnector->dc_sink = NULL;
2713 amdgpu_dm_update_connector_after_detect(aconnector);
2714 mutex_unlock(&aconnector->hpd_lock);
2716 drm_connector_list_iter_end(&iter);
2718 /* Force mode set in atomic commit */
2719 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i)
2720 new_crtc_state->active_changed = true;
2723 * atomic_check is expected to create the dc states. We need to release
2724 * them here, since they were duplicated as part of the suspend
2727 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) {
2728 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
2729 if (dm_new_crtc_state->stream) {
2730 WARN_ON(kref_read(&dm_new_crtc_state->stream->refcount) > 1);
2731 dc_stream_release(dm_new_crtc_state->stream);
2732 dm_new_crtc_state->stream = NULL;
2736 for_each_new_plane_in_state(dm->cached_state, plane, new_plane_state, i) {
2737 dm_new_plane_state = to_dm_plane_state(new_plane_state);
2738 if (dm_new_plane_state->dc_state) {
2739 WARN_ON(kref_read(&dm_new_plane_state->dc_state->refcount) > 1);
2740 dc_plane_state_release(dm_new_plane_state->dc_state);
2741 dm_new_plane_state->dc_state = NULL;
2745 drm_atomic_helper_resume(ddev, dm->cached_state);
2747 dm->cached_state = NULL;
2749 amdgpu_dm_irq_resume_late(adev);
2751 amdgpu_dm_smu_write_watermarks_table(adev);
2759 * DM (and consequently DC) is registered in the amdgpu base driver as a IP
2760 * block. When CONFIG_DRM_AMD_DC is enabled, the DM device IP block is added to
2761 * the base driver's device list to be initialized and torn down accordingly.
2763 * The functions to do so are provided as hooks in &struct amd_ip_funcs.
2766 static const struct amd_ip_funcs amdgpu_dm_funcs = {
2768 .early_init = dm_early_init,
2769 .late_init = dm_late_init,
2770 .sw_init = dm_sw_init,
2771 .sw_fini = dm_sw_fini,
2772 .early_fini = amdgpu_dm_early_fini,
2773 .hw_init = dm_hw_init,
2774 .hw_fini = dm_hw_fini,
2775 .suspend = dm_suspend,
2776 .resume = dm_resume,
2777 .is_idle = dm_is_idle,
2778 .wait_for_idle = dm_wait_for_idle,
2779 .check_soft_reset = dm_check_soft_reset,
2780 .soft_reset = dm_soft_reset,
2781 .set_clockgating_state = dm_set_clockgating_state,
2782 .set_powergating_state = dm_set_powergating_state,
2785 const struct amdgpu_ip_block_version dm_ip_block =
2787 .type = AMD_IP_BLOCK_TYPE_DCE,
2791 .funcs = &amdgpu_dm_funcs,
2801 static const struct drm_mode_config_funcs amdgpu_dm_mode_funcs = {
2802 .fb_create = amdgpu_display_user_framebuffer_create,
2803 .get_format_info = amd_get_format_info,
2804 .output_poll_changed = drm_fb_helper_output_poll_changed,
2805 .atomic_check = amdgpu_dm_atomic_check,
2806 .atomic_commit = drm_atomic_helper_commit,
2809 static struct drm_mode_config_helper_funcs amdgpu_dm_mode_config_helperfuncs = {
2810 .atomic_commit_tail = amdgpu_dm_atomic_commit_tail
2813 static void update_connector_ext_caps(struct amdgpu_dm_connector *aconnector)
2815 u32 max_cll, min_cll, max, min, q, r;
2816 struct amdgpu_dm_backlight_caps *caps;
2817 struct amdgpu_display_manager *dm;
2818 struct drm_connector *conn_base;
2819 struct amdgpu_device *adev;
2820 struct dc_link *link = NULL;
2821 static const u8 pre_computed_values[] = {
2822 50, 51, 52, 53, 55, 56, 57, 58, 59, 61, 62, 63, 65, 66, 68, 69,
2823 71, 72, 74, 75, 77, 79, 81, 82, 84, 86, 88, 90, 92, 94, 96, 98};
2826 if (!aconnector || !aconnector->dc_link)
2829 link = aconnector->dc_link;
2830 if (link->connector_signal != SIGNAL_TYPE_EDP)
2833 conn_base = &aconnector->base;
2834 adev = drm_to_adev(conn_base->dev);
2836 for (i = 0; i < dm->num_of_edps; i++) {
2837 if (link == dm->backlight_link[i])
2840 if (i >= dm->num_of_edps)
2842 caps = &dm->backlight_caps[i];
2843 caps->ext_caps = &aconnector->dc_link->dpcd_sink_ext_caps;
2844 caps->aux_support = false;
2845 max_cll = conn_base->hdr_sink_metadata.hdmi_type1.max_cll;
2846 min_cll = conn_base->hdr_sink_metadata.hdmi_type1.min_cll;
2848 if (caps->ext_caps->bits.oled == 1 /*||
2849 caps->ext_caps->bits.sdr_aux_backlight_control == 1 ||
2850 caps->ext_caps->bits.hdr_aux_backlight_control == 1*/)
2851 caps->aux_support = true;
2853 if (amdgpu_backlight == 0)
2854 caps->aux_support = false;
2855 else if (amdgpu_backlight == 1)
2856 caps->aux_support = true;
2858 /* From the specification (CTA-861-G), for calculating the maximum
2859 * luminance we need to use:
2860 * Luminance = 50*2**(CV/32)
2861 * Where CV is a one-byte value.
2862 * For calculating this expression we may need float point precision;
2863 * to avoid this complexity level, we take advantage that CV is divided
2864 * by a constant. From the Euclids division algorithm, we know that CV
2865 * can be written as: CV = 32*q + r. Next, we replace CV in the
2866 * Luminance expression and get 50*(2**q)*(2**(r/32)), hence we just
2867 * need to pre-compute the value of r/32. For pre-computing the values
2868 * We just used the following Ruby line:
2869 * (0...32).each {|cv| puts (50*2**(cv/32.0)).round}
2870 * The results of the above expressions can be verified at
2871 * pre_computed_values.
2875 max = (1 << q) * pre_computed_values[r];
2877 // min luminance: maxLum * (CV/255)^2 / 100
2878 q = DIV_ROUND_CLOSEST(min_cll, 255);
2879 min = max * DIV_ROUND_CLOSEST((q * q), 100);
2881 caps->aux_max_input_signal = max;
2882 caps->aux_min_input_signal = min;
2885 void amdgpu_dm_update_connector_after_detect(
2886 struct amdgpu_dm_connector *aconnector)
2888 struct drm_connector *connector = &aconnector->base;
2889 struct drm_device *dev = connector->dev;
2890 struct dc_sink *sink;
2892 /* MST handled by drm_mst framework */
2893 if (aconnector->mst_mgr.mst_state == true)
2896 sink = aconnector->dc_link->local_sink;
2898 dc_sink_retain(sink);
2901 * Edid mgmt connector gets first update only in mode_valid hook and then
2902 * the connector sink is set to either fake or physical sink depends on link status.
2903 * Skip if already done during boot.
2905 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED
2906 && aconnector->dc_em_sink) {
2909 * For S3 resume with headless use eml_sink to fake stream
2910 * because on resume connector->sink is set to NULL
2912 mutex_lock(&dev->mode_config.mutex);
2915 if (aconnector->dc_sink) {
2916 amdgpu_dm_update_freesync_caps(connector, NULL);
2918 * retain and release below are used to
2919 * bump up refcount for sink because the link doesn't point
2920 * to it anymore after disconnect, so on next crtc to connector
2921 * reshuffle by UMD we will get into unwanted dc_sink release
2923 dc_sink_release(aconnector->dc_sink);
2925 aconnector->dc_sink = sink;
2926 dc_sink_retain(aconnector->dc_sink);
2927 amdgpu_dm_update_freesync_caps(connector,
2930 amdgpu_dm_update_freesync_caps(connector, NULL);
2931 if (!aconnector->dc_sink) {
2932 aconnector->dc_sink = aconnector->dc_em_sink;
2933 dc_sink_retain(aconnector->dc_sink);
2937 mutex_unlock(&dev->mode_config.mutex);
2940 dc_sink_release(sink);
2945 * TODO: temporary guard to look for proper fix
2946 * if this sink is MST sink, we should not do anything
2948 if (sink && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
2949 dc_sink_release(sink);
2953 if (aconnector->dc_sink == sink) {
2955 * We got a DP short pulse (Link Loss, DP CTS, etc...).
2958 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: dc_sink didn't change.\n",
2959 aconnector->connector_id);
2961 dc_sink_release(sink);
2965 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: Old sink=%p New sink=%p\n",
2966 aconnector->connector_id, aconnector->dc_sink, sink);
2968 mutex_lock(&dev->mode_config.mutex);
2971 * 1. Update status of the drm connector
2972 * 2. Send an event and let userspace tell us what to do
2976 * TODO: check if we still need the S3 mode update workaround.
2977 * If yes, put it here.
2979 if (aconnector->dc_sink) {
2980 amdgpu_dm_update_freesync_caps(connector, NULL);
2981 dc_sink_release(aconnector->dc_sink);
2984 aconnector->dc_sink = sink;
2985 dc_sink_retain(aconnector->dc_sink);
2986 if (sink->dc_edid.length == 0) {
2987 aconnector->edid = NULL;
2988 if (aconnector->dc_link->aux_mode) {
2989 drm_dp_cec_unset_edid(
2990 &aconnector->dm_dp_aux.aux);
2994 (struct edid *)sink->dc_edid.raw_edid;
2996 if (aconnector->dc_link->aux_mode)
2997 drm_dp_cec_set_edid(&aconnector->dm_dp_aux.aux,
3001 drm_connector_update_edid_property(connector, aconnector->edid);
3002 amdgpu_dm_update_freesync_caps(connector, aconnector->edid);
3003 update_connector_ext_caps(aconnector);
3005 drm_dp_cec_unset_edid(&aconnector->dm_dp_aux.aux);
3006 amdgpu_dm_update_freesync_caps(connector, NULL);
3007 drm_connector_update_edid_property(connector, NULL);
3008 aconnector->num_modes = 0;
3009 dc_sink_release(aconnector->dc_sink);
3010 aconnector->dc_sink = NULL;
3011 aconnector->edid = NULL;
3012 #ifdef CONFIG_DRM_AMD_DC_HDCP
3013 /* Set CP to DESIRED if it was ENABLED, so we can re-enable it again on hotplug */
3014 if (connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED)
3015 connector->state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
3019 mutex_unlock(&dev->mode_config.mutex);
3021 update_subconnector_property(aconnector);
3024 dc_sink_release(sink);
3027 static void handle_hpd_irq_helper(struct amdgpu_dm_connector *aconnector)
3029 struct drm_connector *connector = &aconnector->base;
3030 struct drm_device *dev = connector->dev;
3031 enum dc_connection_type new_connection_type = dc_connection_none;
3032 struct amdgpu_device *adev = drm_to_adev(dev);
3033 struct dm_connector_state *dm_con_state = to_dm_connector_state(connector->state);
3034 struct dm_crtc_state *dm_crtc_state = NULL;
3036 if (adev->dm.disable_hpd_irq)
3039 if (dm_con_state->base.state && dm_con_state->base.crtc)
3040 dm_crtc_state = to_dm_crtc_state(drm_atomic_get_crtc_state(
3041 dm_con_state->base.state,
3042 dm_con_state->base.crtc));
3044 * In case of failure or MST no need to update connector status or notify the OS
3045 * since (for MST case) MST does this in its own context.
3047 mutex_lock(&aconnector->hpd_lock);
3049 #ifdef CONFIG_DRM_AMD_DC_HDCP
3050 if (adev->dm.hdcp_workqueue) {
3051 hdcp_reset_display(adev->dm.hdcp_workqueue, aconnector->dc_link->link_index);
3052 dm_con_state->update_hdcp = true;
3055 if (aconnector->fake_enable)
3056 aconnector->fake_enable = false;
3058 if (!dc_link_detect_sink(aconnector->dc_link, &new_connection_type))
3059 DRM_ERROR("KMS: Failed to detect connector\n");
3061 if (aconnector->base.force && new_connection_type == dc_connection_none) {
3062 emulated_link_detect(aconnector->dc_link);
3064 drm_modeset_lock_all(dev);
3065 dm_restore_drm_connector_state(dev, connector);
3066 drm_modeset_unlock_all(dev);
3068 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
3069 drm_kms_helper_connector_hotplug_event(connector);
3071 } else if (dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD)) {
3072 if (new_connection_type == dc_connection_none &&
3073 aconnector->dc_link->type == dc_connection_none &&
3075 dm_set_dpms_off(aconnector->dc_link, dm_crtc_state);
3077 amdgpu_dm_update_connector_after_detect(aconnector);
3079 drm_modeset_lock_all(dev);
3080 dm_restore_drm_connector_state(dev, connector);
3081 drm_modeset_unlock_all(dev);
3083 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
3084 drm_kms_helper_connector_hotplug_event(connector);
3086 mutex_unlock(&aconnector->hpd_lock);
3090 static void handle_hpd_irq(void *param)
3092 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
3094 handle_hpd_irq_helper(aconnector);
3098 static void dm_handle_mst_sideband_msg(struct amdgpu_dm_connector *aconnector)
3100 uint8_t esi[DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI] = { 0 };
3102 bool new_irq_handled = false;
3104 int dpcd_bytes_to_read;
3106 const int max_process_count = 30;
3107 int process_count = 0;
3109 const struct dc_link_status *link_status = dc_link_get_status(aconnector->dc_link);
3111 if (link_status->dpcd_caps->dpcd_rev.raw < 0x12) {
3112 dpcd_bytes_to_read = DP_LANE0_1_STATUS - DP_SINK_COUNT;
3113 /* DPCD 0x200 - 0x201 for downstream IRQ */
3114 dpcd_addr = DP_SINK_COUNT;
3116 dpcd_bytes_to_read = DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI;
3117 /* DPCD 0x2002 - 0x2005 for downstream IRQ */
3118 dpcd_addr = DP_SINK_COUNT_ESI;
3121 dret = drm_dp_dpcd_read(
3122 &aconnector->dm_dp_aux.aux,
3125 dpcd_bytes_to_read);
3127 while (dret == dpcd_bytes_to_read &&
3128 process_count < max_process_count) {
3134 DRM_DEBUG_DRIVER("ESI %02x %02x %02x\n", esi[0], esi[1], esi[2]);
3135 /* handle HPD short pulse irq */
3136 if (aconnector->mst_mgr.mst_state)
3138 &aconnector->mst_mgr,
3142 if (new_irq_handled) {
3143 /* ACK at DPCD to notify down stream */
3144 const int ack_dpcd_bytes_to_write =
3145 dpcd_bytes_to_read - 1;
3147 for (retry = 0; retry < 3; retry++) {
3150 wret = drm_dp_dpcd_write(
3151 &aconnector->dm_dp_aux.aux,
3154 ack_dpcd_bytes_to_write);
3155 if (wret == ack_dpcd_bytes_to_write)
3159 /* check if there is new irq to be handled */
3160 dret = drm_dp_dpcd_read(
3161 &aconnector->dm_dp_aux.aux,
3164 dpcd_bytes_to_read);
3166 new_irq_handled = false;
3172 if (process_count == max_process_count)
3173 DRM_DEBUG_DRIVER("Loop exceeded max iterations\n");
3176 static void schedule_hpd_rx_offload_work(struct hpd_rx_irq_offload_work_queue *offload_wq,
3177 union hpd_irq_data hpd_irq_data)
3179 struct hpd_rx_irq_offload_work *offload_work =
3180 kzalloc(sizeof(*offload_work), GFP_KERNEL);
3182 if (!offload_work) {
3183 DRM_ERROR("Failed to allocate hpd_rx_irq_offload_work.\n");
3187 INIT_WORK(&offload_work->work, dm_handle_hpd_rx_offload_work);
3188 offload_work->data = hpd_irq_data;
3189 offload_work->offload_wq = offload_wq;
3191 queue_work(offload_wq->wq, &offload_work->work);
3192 DRM_DEBUG_KMS("queue work to handle hpd_rx offload work");
3195 static void handle_hpd_rx_irq(void *param)
3197 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
3198 struct drm_connector *connector = &aconnector->base;
3199 struct drm_device *dev = connector->dev;
3200 struct dc_link *dc_link = aconnector->dc_link;
3201 bool is_mst_root_connector = aconnector->mst_mgr.mst_state;
3202 bool result = false;
3203 enum dc_connection_type new_connection_type = dc_connection_none;
3204 struct amdgpu_device *adev = drm_to_adev(dev);
3205 union hpd_irq_data hpd_irq_data;
3206 bool link_loss = false;
3207 bool has_left_work = false;
3208 int idx = aconnector->base.index;
3209 struct hpd_rx_irq_offload_work_queue *offload_wq = &adev->dm.hpd_rx_offload_wq[idx];
3211 memset(&hpd_irq_data, 0, sizeof(hpd_irq_data));
3213 if (adev->dm.disable_hpd_irq)
3217 * TODO:Temporary add mutex to protect hpd interrupt not have a gpio
3218 * conflict, after implement i2c helper, this mutex should be
3221 mutex_lock(&aconnector->hpd_lock);
3223 result = dc_link_handle_hpd_rx_irq(dc_link, &hpd_irq_data,
3224 &link_loss, true, &has_left_work);
3229 if (hpd_irq_data.bytes.device_service_irq.bits.AUTOMATED_TEST) {
3230 schedule_hpd_rx_offload_work(offload_wq, hpd_irq_data);
3234 if (dc_link_dp_allow_hpd_rx_irq(dc_link)) {
3235 if (hpd_irq_data.bytes.device_service_irq.bits.UP_REQ_MSG_RDY ||
3236 hpd_irq_data.bytes.device_service_irq.bits.DOWN_REP_MSG_RDY) {
3237 dm_handle_mst_sideband_msg(aconnector);
3244 spin_lock(&offload_wq->offload_lock);
3245 skip = offload_wq->is_handling_link_loss;
3248 offload_wq->is_handling_link_loss = true;
3250 spin_unlock(&offload_wq->offload_lock);
3253 schedule_hpd_rx_offload_work(offload_wq, hpd_irq_data);
3260 if (result && !is_mst_root_connector) {
3261 /* Downstream Port status changed. */
3262 if (!dc_link_detect_sink(dc_link, &new_connection_type))
3263 DRM_ERROR("KMS: Failed to detect connector\n");
3265 if (aconnector->base.force && new_connection_type == dc_connection_none) {
3266 emulated_link_detect(dc_link);
3268 if (aconnector->fake_enable)
3269 aconnector->fake_enable = false;
3271 amdgpu_dm_update_connector_after_detect(aconnector);
3274 drm_modeset_lock_all(dev);
3275 dm_restore_drm_connector_state(dev, connector);
3276 drm_modeset_unlock_all(dev);
3278 drm_kms_helper_connector_hotplug_event(connector);
3279 } else if (dc_link_detect(dc_link, DETECT_REASON_HPDRX)) {
3281 if (aconnector->fake_enable)
3282 aconnector->fake_enable = false;
3284 amdgpu_dm_update_connector_after_detect(aconnector);
3287 drm_modeset_lock_all(dev);
3288 dm_restore_drm_connector_state(dev, connector);
3289 drm_modeset_unlock_all(dev);
3291 drm_kms_helper_connector_hotplug_event(connector);
3294 #ifdef CONFIG_DRM_AMD_DC_HDCP
3295 if (hpd_irq_data.bytes.device_service_irq.bits.CP_IRQ) {
3296 if (adev->dm.hdcp_workqueue)
3297 hdcp_handle_cpirq(adev->dm.hdcp_workqueue, aconnector->base.index);
3301 if (dc_link->type != dc_connection_mst_branch)
3302 drm_dp_cec_irq(&aconnector->dm_dp_aux.aux);
3304 mutex_unlock(&aconnector->hpd_lock);
3307 static void register_hpd_handlers(struct amdgpu_device *adev)
3309 struct drm_device *dev = adev_to_drm(adev);
3310 struct drm_connector *connector;
3311 struct amdgpu_dm_connector *aconnector;
3312 const struct dc_link *dc_link;
3313 struct dc_interrupt_params int_params = {0};
3315 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
3316 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
3318 list_for_each_entry(connector,
3319 &dev->mode_config.connector_list, head) {
3321 aconnector = to_amdgpu_dm_connector(connector);
3322 dc_link = aconnector->dc_link;
3324 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd) {
3325 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
3326 int_params.irq_source = dc_link->irq_source_hpd;
3328 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3330 (void *) aconnector);
3333 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd_rx) {
3335 /* Also register for DP short pulse (hpd_rx). */
3336 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
3337 int_params.irq_source = dc_link->irq_source_hpd_rx;
3339 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3341 (void *) aconnector);
3343 if (adev->dm.hpd_rx_offload_wq)
3344 adev->dm.hpd_rx_offload_wq[connector->index].aconnector =
3350 #if defined(CONFIG_DRM_AMD_DC_SI)
3351 /* Register IRQ sources and initialize IRQ callbacks */
3352 static int dce60_register_irq_handlers(struct amdgpu_device *adev)
3354 struct dc *dc = adev->dm.dc;
3355 struct common_irq_params *c_irq_params;
3356 struct dc_interrupt_params int_params = {0};
3359 unsigned client_id = AMDGPU_IRQ_CLIENTID_LEGACY;
3361 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
3362 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
3365 * Actions of amdgpu_irq_add_id():
3366 * 1. Register a set() function with base driver.
3367 * Base driver will call set() function to enable/disable an
3368 * interrupt in DC hardware.
3369 * 2. Register amdgpu_dm_irq_handler().
3370 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
3371 * coming from DC hardware.
3372 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
3373 * for acknowledging and handling. */
3375 /* Use VBLANK interrupt */
3376 for (i = 0; i < adev->mode_info.num_crtc; i++) {
3377 r = amdgpu_irq_add_id(adev, client_id, i+1 , &adev->crtc_irq);
3379 DRM_ERROR("Failed to add crtc irq id!\n");
3383 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3384 int_params.irq_source =
3385 dc_interrupt_to_irq_source(dc, i+1 , 0);
3387 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
3389 c_irq_params->adev = adev;
3390 c_irq_params->irq_src = int_params.irq_source;
3392 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3393 dm_crtc_high_irq, c_irq_params);
3396 /* Use GRPH_PFLIP interrupt */
3397 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
3398 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
3399 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq);
3401 DRM_ERROR("Failed to add page flip irq id!\n");
3405 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3406 int_params.irq_source =
3407 dc_interrupt_to_irq_source(dc, i, 0);
3409 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
3411 c_irq_params->adev = adev;
3412 c_irq_params->irq_src = int_params.irq_source;
3414 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3415 dm_pflip_high_irq, c_irq_params);
3420 r = amdgpu_irq_add_id(adev, client_id,
3421 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
3423 DRM_ERROR("Failed to add hpd irq id!\n");
3427 register_hpd_handlers(adev);
3433 /* Register IRQ sources and initialize IRQ callbacks */
3434 static int dce110_register_irq_handlers(struct amdgpu_device *adev)
3436 struct dc *dc = adev->dm.dc;
3437 struct common_irq_params *c_irq_params;
3438 struct dc_interrupt_params int_params = {0};
3441 unsigned client_id = AMDGPU_IRQ_CLIENTID_LEGACY;
3443 if (adev->family >= AMDGPU_FAMILY_AI)
3444 client_id = SOC15_IH_CLIENTID_DCE;
3446 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
3447 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
3450 * Actions of amdgpu_irq_add_id():
3451 * 1. Register a set() function with base driver.
3452 * Base driver will call set() function to enable/disable an
3453 * interrupt in DC hardware.
3454 * 2. Register amdgpu_dm_irq_handler().
3455 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
3456 * coming from DC hardware.
3457 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
3458 * for acknowledging and handling. */
3460 /* Use VBLANK interrupt */
3461 for (i = VISLANDS30_IV_SRCID_D1_VERTICAL_INTERRUPT0; i <= VISLANDS30_IV_SRCID_D6_VERTICAL_INTERRUPT0; i++) {
3462 r = amdgpu_irq_add_id(adev, client_id, i, &adev->crtc_irq);
3464 DRM_ERROR("Failed to add crtc irq id!\n");
3468 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3469 int_params.irq_source =
3470 dc_interrupt_to_irq_source(dc, i, 0);
3472 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
3474 c_irq_params->adev = adev;
3475 c_irq_params->irq_src = int_params.irq_source;
3477 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3478 dm_crtc_high_irq, c_irq_params);
3481 /* Use VUPDATE interrupt */
3482 for (i = VISLANDS30_IV_SRCID_D1_V_UPDATE_INT; i <= VISLANDS30_IV_SRCID_D6_V_UPDATE_INT; i += 2) {
3483 r = amdgpu_irq_add_id(adev, client_id, i, &adev->vupdate_irq);
3485 DRM_ERROR("Failed to add vupdate irq id!\n");
3489 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3490 int_params.irq_source =
3491 dc_interrupt_to_irq_source(dc, i, 0);
3493 c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1];
3495 c_irq_params->adev = adev;
3496 c_irq_params->irq_src = int_params.irq_source;
3498 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3499 dm_vupdate_high_irq, c_irq_params);
3502 /* Use GRPH_PFLIP interrupt */
3503 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
3504 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
3505 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq);
3507 DRM_ERROR("Failed to add page flip irq id!\n");
3511 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3512 int_params.irq_source =
3513 dc_interrupt_to_irq_source(dc, i, 0);
3515 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
3517 c_irq_params->adev = adev;
3518 c_irq_params->irq_src = int_params.irq_source;
3520 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3521 dm_pflip_high_irq, c_irq_params);
3526 r = amdgpu_irq_add_id(adev, client_id,
3527 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
3529 DRM_ERROR("Failed to add hpd irq id!\n");
3533 register_hpd_handlers(adev);
3538 /* Register IRQ sources and initialize IRQ callbacks */
3539 static int dcn10_register_irq_handlers(struct amdgpu_device *adev)
3541 struct dc *dc = adev->dm.dc;
3542 struct common_irq_params *c_irq_params;
3543 struct dc_interrupt_params int_params = {0};
3546 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
3547 static const unsigned int vrtl_int_srcid[] = {
3548 DCN_1_0__SRCID__OTG1_VERTICAL_INTERRUPT0_CONTROL,
3549 DCN_1_0__SRCID__OTG2_VERTICAL_INTERRUPT0_CONTROL,
3550 DCN_1_0__SRCID__OTG3_VERTICAL_INTERRUPT0_CONTROL,
3551 DCN_1_0__SRCID__OTG4_VERTICAL_INTERRUPT0_CONTROL,
3552 DCN_1_0__SRCID__OTG5_VERTICAL_INTERRUPT0_CONTROL,
3553 DCN_1_0__SRCID__OTG6_VERTICAL_INTERRUPT0_CONTROL
3557 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
3558 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
3561 * Actions of amdgpu_irq_add_id():
3562 * 1. Register a set() function with base driver.
3563 * Base driver will call set() function to enable/disable an
3564 * interrupt in DC hardware.
3565 * 2. Register amdgpu_dm_irq_handler().
3566 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
3567 * coming from DC hardware.
3568 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
3569 * for acknowledging and handling.
3572 /* Use VSTARTUP interrupt */
3573 for (i = DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP;
3574 i <= DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP + adev->mode_info.num_crtc - 1;
3576 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->crtc_irq);
3579 DRM_ERROR("Failed to add crtc irq id!\n");
3583 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3584 int_params.irq_source =
3585 dc_interrupt_to_irq_source(dc, i, 0);
3587 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
3589 c_irq_params->adev = adev;
3590 c_irq_params->irq_src = int_params.irq_source;
3592 amdgpu_dm_irq_register_interrupt(
3593 adev, &int_params, dm_crtc_high_irq, c_irq_params);
3596 /* Use otg vertical line interrupt */
3597 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
3598 for (i = 0; i <= adev->mode_info.num_crtc - 1; i++) {
3599 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE,
3600 vrtl_int_srcid[i], &adev->vline0_irq);
3603 DRM_ERROR("Failed to add vline0 irq id!\n");
3607 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3608 int_params.irq_source =
3609 dc_interrupt_to_irq_source(dc, vrtl_int_srcid[i], 0);
3611 if (int_params.irq_source == DC_IRQ_SOURCE_INVALID) {
3612 DRM_ERROR("Failed to register vline0 irq %d!\n", vrtl_int_srcid[i]);
3616 c_irq_params = &adev->dm.vline0_params[int_params.irq_source
3617 - DC_IRQ_SOURCE_DC1_VLINE0];
3619 c_irq_params->adev = adev;
3620 c_irq_params->irq_src = int_params.irq_source;
3622 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3623 dm_dcn_vertical_interrupt0_high_irq, c_irq_params);
3627 /* Use VUPDATE_NO_LOCK interrupt on DCN, which seems to correspond to
3628 * the regular VUPDATE interrupt on DCE. We want DC_IRQ_SOURCE_VUPDATEx
3629 * to trigger at end of each vblank, regardless of state of the lock,
3630 * matching DCE behaviour.
3632 for (i = DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT;
3633 i <= DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT + adev->mode_info.num_crtc - 1;
3635 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->vupdate_irq);
3638 DRM_ERROR("Failed to add vupdate irq id!\n");
3642 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3643 int_params.irq_source =
3644 dc_interrupt_to_irq_source(dc, i, 0);
3646 c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1];
3648 c_irq_params->adev = adev;
3649 c_irq_params->irq_src = int_params.irq_source;
3651 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3652 dm_vupdate_high_irq, c_irq_params);
3655 /* Use GRPH_PFLIP interrupt */
3656 for (i = DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT;
3657 i <= DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT + dc->caps.max_otg_num - 1;
3659 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->pageflip_irq);
3661 DRM_ERROR("Failed to add page flip irq id!\n");
3665 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
3666 int_params.irq_source =
3667 dc_interrupt_to_irq_source(dc, i, 0);
3669 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
3671 c_irq_params->adev = adev;
3672 c_irq_params->irq_src = int_params.irq_source;
3674 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3675 dm_pflip_high_irq, c_irq_params);
3680 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DC_HPD1_INT,
3683 DRM_ERROR("Failed to add hpd irq id!\n");
3687 register_hpd_handlers(adev);
3691 /* Register Outbox IRQ sources and initialize IRQ callbacks */
3692 static int register_outbox_irq_handlers(struct amdgpu_device *adev)
3694 struct dc *dc = adev->dm.dc;
3695 struct common_irq_params *c_irq_params;
3696 struct dc_interrupt_params int_params = {0};
3699 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
3700 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
3702 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DMCUB_OUTBOX_LOW_PRIORITY_READY_INT,
3703 &adev->dmub_outbox_irq);
3705 DRM_ERROR("Failed to add outbox irq id!\n");
3709 if (dc->ctx->dmub_srv) {
3710 i = DCN_1_0__SRCID__DMCUB_OUTBOX_LOW_PRIORITY_READY_INT;
3711 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
3712 int_params.irq_source =
3713 dc_interrupt_to_irq_source(dc, i, 0);
3715 c_irq_params = &adev->dm.dmub_outbox_params[0];
3717 c_irq_params->adev = adev;
3718 c_irq_params->irq_src = int_params.irq_source;
3720 amdgpu_dm_irq_register_interrupt(adev, &int_params,
3721 dm_dmub_outbox1_low_irq, c_irq_params);
3728 * Acquires the lock for the atomic state object and returns
3729 * the new atomic state.
3731 * This should only be called during atomic check.
3733 int dm_atomic_get_state(struct drm_atomic_state *state,
3734 struct dm_atomic_state **dm_state)
3736 struct drm_device *dev = state->dev;
3737 struct amdgpu_device *adev = drm_to_adev(dev);
3738 struct amdgpu_display_manager *dm = &adev->dm;
3739 struct drm_private_state *priv_state;
3744 priv_state = drm_atomic_get_private_obj_state(state, &dm->atomic_obj);
3745 if (IS_ERR(priv_state))
3746 return PTR_ERR(priv_state);
3748 *dm_state = to_dm_atomic_state(priv_state);
3753 static struct dm_atomic_state *
3754 dm_atomic_get_new_state(struct drm_atomic_state *state)
3756 struct drm_device *dev = state->dev;
3757 struct amdgpu_device *adev = drm_to_adev(dev);
3758 struct amdgpu_display_manager *dm = &adev->dm;
3759 struct drm_private_obj *obj;
3760 struct drm_private_state *new_obj_state;
3763 for_each_new_private_obj_in_state(state, obj, new_obj_state, i) {
3764 if (obj->funcs == dm->atomic_obj.funcs)
3765 return to_dm_atomic_state(new_obj_state);
3771 static struct drm_private_state *
3772 dm_atomic_duplicate_state(struct drm_private_obj *obj)
3774 struct dm_atomic_state *old_state, *new_state;
3776 new_state = kzalloc(sizeof(*new_state), GFP_KERNEL);
3780 __drm_atomic_helper_private_obj_duplicate_state(obj, &new_state->base);
3782 old_state = to_dm_atomic_state(obj->state);
3784 if (old_state && old_state->context)
3785 new_state->context = dc_copy_state(old_state->context);
3787 if (!new_state->context) {
3792 return &new_state->base;
3795 static void dm_atomic_destroy_state(struct drm_private_obj *obj,
3796 struct drm_private_state *state)
3798 struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
3800 if (dm_state && dm_state->context)
3801 dc_release_state(dm_state->context);
3806 static struct drm_private_state_funcs dm_atomic_state_funcs = {
3807 .atomic_duplicate_state = dm_atomic_duplicate_state,
3808 .atomic_destroy_state = dm_atomic_destroy_state,
3811 static int amdgpu_dm_mode_config_init(struct amdgpu_device *adev)
3813 struct dm_atomic_state *state;
3816 adev->mode_info.mode_config_initialized = true;
3818 adev_to_drm(adev)->mode_config.funcs = (void *)&amdgpu_dm_mode_funcs;
3819 adev_to_drm(adev)->mode_config.helper_private = &amdgpu_dm_mode_config_helperfuncs;
3821 adev_to_drm(adev)->mode_config.max_width = 16384;
3822 adev_to_drm(adev)->mode_config.max_height = 16384;
3824 adev_to_drm(adev)->mode_config.preferred_depth = 24;
3825 adev_to_drm(adev)->mode_config.prefer_shadow = 1;
3826 /* indicates support for immediate flip */
3827 adev_to_drm(adev)->mode_config.async_page_flip = true;
3829 adev_to_drm(adev)->mode_config.fb_base = adev->gmc.aper_base;
3831 state = kzalloc(sizeof(*state), GFP_KERNEL);
3835 state->context = dc_create_state(adev->dm.dc);
3836 if (!state->context) {
3841 dc_resource_state_copy_construct_current(adev->dm.dc, state->context);
3843 drm_atomic_private_obj_init(adev_to_drm(adev),
3844 &adev->dm.atomic_obj,
3846 &dm_atomic_state_funcs);
3848 r = amdgpu_display_modeset_create_props(adev);
3850 dc_release_state(state->context);
3855 r = amdgpu_dm_audio_init(adev);
3857 dc_release_state(state->context);
3865 #define AMDGPU_DM_DEFAULT_MIN_BACKLIGHT 12
3866 #define AMDGPU_DM_DEFAULT_MAX_BACKLIGHT 255
3867 #define AUX_BL_DEFAULT_TRANSITION_TIME_MS 50
3869 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
3870 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
3872 static void amdgpu_dm_update_backlight_caps(struct amdgpu_display_manager *dm,
3875 #if defined(CONFIG_ACPI)
3876 struct amdgpu_dm_backlight_caps caps;
3878 memset(&caps, 0, sizeof(caps));
3880 if (dm->backlight_caps[bl_idx].caps_valid)
3883 amdgpu_acpi_get_backlight_caps(&caps);
3884 if (caps.caps_valid) {
3885 dm->backlight_caps[bl_idx].caps_valid = true;
3886 if (caps.aux_support)
3888 dm->backlight_caps[bl_idx].min_input_signal = caps.min_input_signal;
3889 dm->backlight_caps[bl_idx].max_input_signal = caps.max_input_signal;
3891 dm->backlight_caps[bl_idx].min_input_signal =
3892 AMDGPU_DM_DEFAULT_MIN_BACKLIGHT;
3893 dm->backlight_caps[bl_idx].max_input_signal =
3894 AMDGPU_DM_DEFAULT_MAX_BACKLIGHT;
3897 if (dm->backlight_caps[bl_idx].aux_support)
3900 dm->backlight_caps[bl_idx].min_input_signal = AMDGPU_DM_DEFAULT_MIN_BACKLIGHT;
3901 dm->backlight_caps[bl_idx].max_input_signal = AMDGPU_DM_DEFAULT_MAX_BACKLIGHT;
3905 static int get_brightness_range(const struct amdgpu_dm_backlight_caps *caps,
3906 unsigned *min, unsigned *max)
3911 if (caps->aux_support) {
3912 // Firmware limits are in nits, DC API wants millinits.
3913 *max = 1000 * caps->aux_max_input_signal;
3914 *min = 1000 * caps->aux_min_input_signal;
3916 // Firmware limits are 8-bit, PWM control is 16-bit.
3917 *max = 0x101 * caps->max_input_signal;
3918 *min = 0x101 * caps->min_input_signal;
3923 static u32 convert_brightness_from_user(const struct amdgpu_dm_backlight_caps *caps,
3924 uint32_t brightness)
3928 if (!get_brightness_range(caps, &min, &max))
3931 // Rescale 0..255 to min..max
3932 return min + DIV_ROUND_CLOSEST((max - min) * brightness,
3933 AMDGPU_MAX_BL_LEVEL);
3936 static u32 convert_brightness_to_user(const struct amdgpu_dm_backlight_caps *caps,
3937 uint32_t brightness)
3941 if (!get_brightness_range(caps, &min, &max))
3944 if (brightness < min)
3946 // Rescale min..max to 0..255
3947 return DIV_ROUND_CLOSEST(AMDGPU_MAX_BL_LEVEL * (brightness - min),
3951 static void amdgpu_dm_backlight_set_level(struct amdgpu_display_manager *dm,
3953 u32 user_brightness)
3955 struct amdgpu_dm_backlight_caps caps;
3956 struct dc_link *link;
3960 amdgpu_dm_update_backlight_caps(dm, bl_idx);
3961 caps = dm->backlight_caps[bl_idx];
3963 dm->brightness[bl_idx] = user_brightness;
3964 /* update scratch register */
3966 amdgpu_atombios_scratch_regs_set_backlight_level(dm->adev, dm->brightness[bl_idx]);
3967 brightness = convert_brightness_from_user(&caps, dm->brightness[bl_idx]);
3968 link = (struct dc_link *)dm->backlight_link[bl_idx];
3970 /* Change brightness based on AUX property */
3971 if (caps.aux_support) {
3972 rc = dc_link_set_backlight_level_nits(link, true, brightness,
3973 AUX_BL_DEFAULT_TRANSITION_TIME_MS);
3975 DRM_DEBUG("DM: Failed to update backlight via AUX on eDP[%d]\n", bl_idx);
3977 rc = dc_link_set_backlight_level(link, brightness, 0);
3979 DRM_DEBUG("DM: Failed to update backlight on eDP[%d]\n", bl_idx);
3983 dm->actual_brightness[bl_idx] = user_brightness;
3986 static int amdgpu_dm_backlight_update_status(struct backlight_device *bd)
3988 struct amdgpu_display_manager *dm = bl_get_data(bd);
3991 for (i = 0; i < dm->num_of_edps; i++) {
3992 if (bd == dm->backlight_dev[i])
3995 if (i >= AMDGPU_DM_MAX_NUM_EDP)
3997 amdgpu_dm_backlight_set_level(dm, i, bd->props.brightness);
4002 static u32 amdgpu_dm_backlight_get_level(struct amdgpu_display_manager *dm,
4005 struct amdgpu_dm_backlight_caps caps;
4006 struct dc_link *link = (struct dc_link *)dm->backlight_link[bl_idx];
4008 amdgpu_dm_update_backlight_caps(dm, bl_idx);
4009 caps = dm->backlight_caps[bl_idx];
4011 if (caps.aux_support) {
4015 rc = dc_link_get_backlight_level_nits(link, &avg, &peak);
4017 return dm->brightness[bl_idx];
4018 return convert_brightness_to_user(&caps, avg);
4020 int ret = dc_link_get_backlight_level(link);
4022 if (ret == DC_ERROR_UNEXPECTED)
4023 return dm->brightness[bl_idx];
4024 return convert_brightness_to_user(&caps, ret);
4028 static int amdgpu_dm_backlight_get_brightness(struct backlight_device *bd)
4030 struct amdgpu_display_manager *dm = bl_get_data(bd);
4033 for (i = 0; i < dm->num_of_edps; i++) {
4034 if (bd == dm->backlight_dev[i])
4037 if (i >= AMDGPU_DM_MAX_NUM_EDP)
4039 return amdgpu_dm_backlight_get_level(dm, i);
4042 static const struct backlight_ops amdgpu_dm_backlight_ops = {
4043 .options = BL_CORE_SUSPENDRESUME,
4044 .get_brightness = amdgpu_dm_backlight_get_brightness,
4045 .update_status = amdgpu_dm_backlight_update_status,
4049 amdgpu_dm_register_backlight_device(struct amdgpu_display_manager *dm)
4052 struct backlight_properties props = { 0 };
4054 amdgpu_dm_update_backlight_caps(dm, dm->num_of_edps);
4055 dm->brightness[dm->num_of_edps] = AMDGPU_MAX_BL_LEVEL;
4057 props.max_brightness = AMDGPU_MAX_BL_LEVEL;
4058 props.brightness = AMDGPU_MAX_BL_LEVEL;
4059 props.type = BACKLIGHT_RAW;
4061 snprintf(bl_name, sizeof(bl_name), "amdgpu_bl%d",
4062 adev_to_drm(dm->adev)->primary->index + dm->num_of_edps);
4064 dm->backlight_dev[dm->num_of_edps] = backlight_device_register(bl_name,
4065 adev_to_drm(dm->adev)->dev,
4067 &amdgpu_dm_backlight_ops,
4070 if (IS_ERR(dm->backlight_dev[dm->num_of_edps]))
4071 DRM_ERROR("DM: Backlight registration failed!\n");
4073 DRM_DEBUG_DRIVER("DM: Registered Backlight device: %s\n", bl_name);
4077 static int initialize_plane(struct amdgpu_display_manager *dm,
4078 struct amdgpu_mode_info *mode_info, int plane_id,
4079 enum drm_plane_type plane_type,
4080 const struct dc_plane_cap *plane_cap)
4082 struct drm_plane *plane;
4083 unsigned long possible_crtcs;
4086 plane = kzalloc(sizeof(struct drm_plane), GFP_KERNEL);
4088 DRM_ERROR("KMS: Failed to allocate plane\n");
4091 plane->type = plane_type;
4094 * HACK: IGT tests expect that the primary plane for a CRTC
4095 * can only have one possible CRTC. Only expose support for
4096 * any CRTC if they're not going to be used as a primary plane
4097 * for a CRTC - like overlay or underlay planes.
4099 possible_crtcs = 1 << plane_id;
4100 if (plane_id >= dm->dc->caps.max_streams)
4101 possible_crtcs = 0xff;
4103 ret = amdgpu_dm_plane_init(dm, plane, possible_crtcs, plane_cap);
4106 DRM_ERROR("KMS: Failed to initialize plane\n");
4112 mode_info->planes[plane_id] = plane;
4118 static void register_backlight_device(struct amdgpu_display_manager *dm,
4119 struct dc_link *link)
4121 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
4122 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
4124 if ((link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) &&
4125 link->type != dc_connection_none) {
4127 * Event if registration failed, we should continue with
4128 * DM initialization because not having a backlight control
4129 * is better then a black screen.
4131 if (!dm->backlight_dev[dm->num_of_edps])
4132 amdgpu_dm_register_backlight_device(dm);
4134 if (dm->backlight_dev[dm->num_of_edps]) {
4135 dm->backlight_link[dm->num_of_edps] = link;
4144 * In this architecture, the association
4145 * connector -> encoder -> crtc
4146 * id not really requried. The crtc and connector will hold the
4147 * display_index as an abstraction to use with DAL component
4149 * Returns 0 on success
4151 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev)
4153 struct amdgpu_display_manager *dm = &adev->dm;
4155 struct amdgpu_dm_connector *aconnector = NULL;
4156 struct amdgpu_encoder *aencoder = NULL;
4157 struct amdgpu_mode_info *mode_info = &adev->mode_info;
4159 int32_t primary_planes;
4160 enum dc_connection_type new_connection_type = dc_connection_none;
4161 const struct dc_plane_cap *plane;
4162 bool psr_feature_enabled = false;
4164 dm->display_indexes_num = dm->dc->caps.max_streams;
4165 /* Update the actual used number of crtc */
4166 adev->mode_info.num_crtc = adev->dm.display_indexes_num;
4168 link_cnt = dm->dc->caps.max_links;
4169 if (amdgpu_dm_mode_config_init(dm->adev)) {
4170 DRM_ERROR("DM: Failed to initialize mode config\n");
4174 /* There is one primary plane per CRTC */
4175 primary_planes = dm->dc->caps.max_streams;
4176 ASSERT(primary_planes <= AMDGPU_MAX_PLANES);
4179 * Initialize primary planes, implicit planes for legacy IOCTLS.
4180 * Order is reversed to match iteration order in atomic check.
4182 for (i = (primary_planes - 1); i >= 0; i--) {
4183 plane = &dm->dc->caps.planes[i];
4185 if (initialize_plane(dm, mode_info, i,
4186 DRM_PLANE_TYPE_PRIMARY, plane)) {
4187 DRM_ERROR("KMS: Failed to initialize primary plane\n");
4193 * Initialize overlay planes, index starting after primary planes.
4194 * These planes have a higher DRM index than the primary planes since
4195 * they should be considered as having a higher z-order.
4196 * Order is reversed to match iteration order in atomic check.
4198 * Only support DCN for now, and only expose one so we don't encourage
4199 * userspace to use up all the pipes.
4201 for (i = 0; i < dm->dc->caps.max_planes; ++i) {
4202 struct dc_plane_cap *plane = &dm->dc->caps.planes[i];
4204 if (plane->type != DC_PLANE_TYPE_DCN_UNIVERSAL)
4207 if (!plane->blends_with_above || !plane->blends_with_below)
4210 if (!plane->pixel_format_support.argb8888)
4213 if (initialize_plane(dm, NULL, primary_planes + i,
4214 DRM_PLANE_TYPE_OVERLAY, plane)) {
4215 DRM_ERROR("KMS: Failed to initialize overlay plane\n");
4219 /* Only create one overlay plane. */
4223 for (i = 0; i < dm->dc->caps.max_streams; i++)
4224 if (amdgpu_dm_crtc_init(dm, mode_info->planes[i], i)) {
4225 DRM_ERROR("KMS: Failed to initialize crtc\n");
4229 /* Use Outbox interrupt */
4230 switch (adev->ip_versions[DCE_HWIP][0]) {
4231 case IP_VERSION(3, 0, 0):
4232 case IP_VERSION(3, 1, 2):
4233 case IP_VERSION(3, 1, 3):
4234 case IP_VERSION(3, 1, 5):
4235 case IP_VERSION(3, 1, 6):
4236 case IP_VERSION(2, 1, 0):
4237 if (register_outbox_irq_handlers(dm->adev)) {
4238 DRM_ERROR("DM: Failed to initialize IRQ\n");
4243 DRM_DEBUG_KMS("Unsupported DCN IP version for outbox: 0x%X\n",
4244 adev->ip_versions[DCE_HWIP][0]);
4247 /* Determine whether to enable PSR support by default. */
4248 if (!(amdgpu_dc_debug_mask & DC_DISABLE_PSR)) {
4249 switch (adev->ip_versions[DCE_HWIP][0]) {
4250 case IP_VERSION(3, 1, 2):
4251 case IP_VERSION(3, 1, 3):
4252 case IP_VERSION(3, 1, 5):
4253 case IP_VERSION(3, 1, 6):
4254 psr_feature_enabled = true;
4257 psr_feature_enabled = amdgpu_dc_feature_mask & DC_PSR_MASK;
4262 /* Disable vblank IRQs aggressively for power-saving. */
4263 adev_to_drm(adev)->vblank_disable_immediate = true;
4265 /* loops over all connectors on the board */
4266 for (i = 0; i < link_cnt; i++) {
4267 struct dc_link *link = NULL;
4269 if (i > AMDGPU_DM_MAX_DISPLAY_INDEX) {
4271 "KMS: Cannot support more than %d display indexes\n",
4272 AMDGPU_DM_MAX_DISPLAY_INDEX);
4276 aconnector = kzalloc(sizeof(*aconnector), GFP_KERNEL);
4280 aencoder = kzalloc(sizeof(*aencoder), GFP_KERNEL);
4284 if (amdgpu_dm_encoder_init(dm->ddev, aencoder, i)) {
4285 DRM_ERROR("KMS: Failed to initialize encoder\n");
4289 if (amdgpu_dm_connector_init(dm, aconnector, i, aencoder)) {
4290 DRM_ERROR("KMS: Failed to initialize connector\n");
4294 link = dc_get_link_at_index(dm->dc, i);
4296 if (!dc_link_detect_sink(link, &new_connection_type))
4297 DRM_ERROR("KMS: Failed to detect connector\n");
4299 if (aconnector->base.force && new_connection_type == dc_connection_none) {
4300 emulated_link_detect(link);
4301 amdgpu_dm_update_connector_after_detect(aconnector);
4303 } else if (dc_link_detect(link, DETECT_REASON_BOOT)) {
4304 amdgpu_dm_update_connector_after_detect(aconnector);
4305 register_backlight_device(dm, link);
4306 if (dm->num_of_edps)
4307 update_connector_ext_caps(aconnector);
4308 if (psr_feature_enabled)
4309 amdgpu_dm_set_psr_caps(link);
4311 /* TODO: Fix vblank control helpers to delay PSR entry to allow this when
4312 * PSR is also supported.
4314 if (link->psr_settings.psr_feature_enabled)
4315 adev_to_drm(adev)->vblank_disable_immediate = false;
4321 /* Software is initialized. Now we can register interrupt handlers. */
4322 switch (adev->asic_type) {
4323 #if defined(CONFIG_DRM_AMD_DC_SI)
4328 if (dce60_register_irq_handlers(dm->adev)) {
4329 DRM_ERROR("DM: Failed to initialize IRQ\n");
4343 case CHIP_POLARIS11:
4344 case CHIP_POLARIS10:
4345 case CHIP_POLARIS12:
4350 if (dce110_register_irq_handlers(dm->adev)) {
4351 DRM_ERROR("DM: Failed to initialize IRQ\n");
4356 switch (adev->ip_versions[DCE_HWIP][0]) {
4357 case IP_VERSION(1, 0, 0):
4358 case IP_VERSION(1, 0, 1):
4359 case IP_VERSION(2, 0, 2):
4360 case IP_VERSION(2, 0, 3):
4361 case IP_VERSION(2, 0, 0):
4362 case IP_VERSION(2, 1, 0):
4363 case IP_VERSION(3, 0, 0):
4364 case IP_VERSION(3, 0, 2):
4365 case IP_VERSION(3, 0, 3):
4366 case IP_VERSION(3, 0, 1):
4367 case IP_VERSION(3, 1, 2):
4368 case IP_VERSION(3, 1, 3):
4369 case IP_VERSION(3, 1, 5):
4370 case IP_VERSION(3, 1, 6):
4371 if (dcn10_register_irq_handlers(dm->adev)) {
4372 DRM_ERROR("DM: Failed to initialize IRQ\n");
4377 DRM_ERROR("Unsupported DCE IP versions: 0x%X\n",
4378 adev->ip_versions[DCE_HWIP][0]);
4392 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm)
4394 drm_atomic_private_obj_fini(&dm->atomic_obj);
4398 /******************************************************************************
4399 * amdgpu_display_funcs functions
4400 *****************************************************************************/
4403 * dm_bandwidth_update - program display watermarks
4405 * @adev: amdgpu_device pointer
4407 * Calculate and program the display watermarks and line buffer allocation.
4409 static void dm_bandwidth_update(struct amdgpu_device *adev)
4411 /* TODO: implement later */
4414 static const struct amdgpu_display_funcs dm_display_funcs = {
4415 .bandwidth_update = dm_bandwidth_update, /* called unconditionally */
4416 .vblank_get_counter = dm_vblank_get_counter,/* called unconditionally */
4417 .backlight_set_level = NULL, /* never called for DC */
4418 .backlight_get_level = NULL, /* never called for DC */
4419 .hpd_sense = NULL,/* called unconditionally */
4420 .hpd_set_polarity = NULL, /* called unconditionally */
4421 .hpd_get_gpio_reg = NULL, /* VBIOS parsing. DAL does it. */
4422 .page_flip_get_scanoutpos =
4423 dm_crtc_get_scanoutpos,/* called unconditionally */
4424 .add_encoder = NULL, /* VBIOS parsing. DAL does it. */
4425 .add_connector = NULL, /* VBIOS parsing. DAL does it. */
4428 #if defined(CONFIG_DEBUG_KERNEL_DC)
4430 static ssize_t s3_debug_store(struct device *device,
4431 struct device_attribute *attr,
4437 struct drm_device *drm_dev = dev_get_drvdata(device);
4438 struct amdgpu_device *adev = drm_to_adev(drm_dev);
4440 ret = kstrtoint(buf, 0, &s3_state);
4445 drm_kms_helper_hotplug_event(adev_to_drm(adev));
4450 return ret == 0 ? count : 0;
4453 DEVICE_ATTR_WO(s3_debug);
4457 static int dm_early_init(void *handle)
4459 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
4461 switch (adev->asic_type) {
4462 #if defined(CONFIG_DRM_AMD_DC_SI)
4466 adev->mode_info.num_crtc = 6;
4467 adev->mode_info.num_hpd = 6;
4468 adev->mode_info.num_dig = 6;
4471 adev->mode_info.num_crtc = 2;
4472 adev->mode_info.num_hpd = 2;
4473 adev->mode_info.num_dig = 2;
4478 adev->mode_info.num_crtc = 6;
4479 adev->mode_info.num_hpd = 6;
4480 adev->mode_info.num_dig = 6;
4483 adev->mode_info.num_crtc = 4;
4484 adev->mode_info.num_hpd = 6;
4485 adev->mode_info.num_dig = 7;
4489 adev->mode_info.num_crtc = 2;
4490 adev->mode_info.num_hpd = 6;
4491 adev->mode_info.num_dig = 6;
4495 adev->mode_info.num_crtc = 6;
4496 adev->mode_info.num_hpd = 6;
4497 adev->mode_info.num_dig = 7;
4500 adev->mode_info.num_crtc = 3;
4501 adev->mode_info.num_hpd = 6;
4502 adev->mode_info.num_dig = 9;
4505 adev->mode_info.num_crtc = 2;
4506 adev->mode_info.num_hpd = 6;
4507 adev->mode_info.num_dig = 9;
4509 case CHIP_POLARIS11:
4510 case CHIP_POLARIS12:
4511 adev->mode_info.num_crtc = 5;
4512 adev->mode_info.num_hpd = 5;
4513 adev->mode_info.num_dig = 5;
4515 case CHIP_POLARIS10:
4517 adev->mode_info.num_crtc = 6;
4518 adev->mode_info.num_hpd = 6;
4519 adev->mode_info.num_dig = 6;
4524 adev->mode_info.num_crtc = 6;
4525 adev->mode_info.num_hpd = 6;
4526 adev->mode_info.num_dig = 6;
4530 switch (adev->ip_versions[DCE_HWIP][0]) {
4531 case IP_VERSION(2, 0, 2):
4532 case IP_VERSION(3, 0, 0):
4533 adev->mode_info.num_crtc = 6;
4534 adev->mode_info.num_hpd = 6;
4535 adev->mode_info.num_dig = 6;
4537 case IP_VERSION(2, 0, 0):
4538 case IP_VERSION(3, 0, 2):
4539 adev->mode_info.num_crtc = 5;
4540 adev->mode_info.num_hpd = 5;
4541 adev->mode_info.num_dig = 5;
4543 case IP_VERSION(2, 0, 3):
4544 case IP_VERSION(3, 0, 3):
4545 adev->mode_info.num_crtc = 2;
4546 adev->mode_info.num_hpd = 2;
4547 adev->mode_info.num_dig = 2;
4549 case IP_VERSION(1, 0, 0):
4550 case IP_VERSION(1, 0, 1):
4551 case IP_VERSION(3, 0, 1):
4552 case IP_VERSION(2, 1, 0):
4553 case IP_VERSION(3, 1, 2):
4554 case IP_VERSION(3, 1, 3):
4555 case IP_VERSION(3, 1, 5):
4556 case IP_VERSION(3, 1, 6):
4557 adev->mode_info.num_crtc = 4;
4558 adev->mode_info.num_hpd = 4;
4559 adev->mode_info.num_dig = 4;
4562 DRM_ERROR("Unsupported DCE IP versions: 0x%x\n",
4563 adev->ip_versions[DCE_HWIP][0]);
4569 amdgpu_dm_set_irq_funcs(adev);
4571 if (adev->mode_info.funcs == NULL)
4572 adev->mode_info.funcs = &dm_display_funcs;
4575 * Note: Do NOT change adev->audio_endpt_rreg and
4576 * adev->audio_endpt_wreg because they are initialised in
4577 * amdgpu_device_init()
4579 #if defined(CONFIG_DEBUG_KERNEL_DC)
4581 adev_to_drm(adev)->dev,
4582 &dev_attr_s3_debug);
4588 static bool modeset_required(struct drm_crtc_state *crtc_state,
4589 struct dc_stream_state *new_stream,
4590 struct dc_stream_state *old_stream)
4592 return crtc_state->active && drm_atomic_crtc_needs_modeset(crtc_state);
4595 static bool modereset_required(struct drm_crtc_state *crtc_state)
4597 return !crtc_state->active && drm_atomic_crtc_needs_modeset(crtc_state);
4600 static void amdgpu_dm_encoder_destroy(struct drm_encoder *encoder)
4602 drm_encoder_cleanup(encoder);
4606 static const struct drm_encoder_funcs amdgpu_dm_encoder_funcs = {
4607 .destroy = amdgpu_dm_encoder_destroy,
4611 static void get_min_max_dc_plane_scaling(struct drm_device *dev,
4612 struct drm_framebuffer *fb,
4613 int *min_downscale, int *max_upscale)
4615 struct amdgpu_device *adev = drm_to_adev(dev);
4616 struct dc *dc = adev->dm.dc;
4617 /* Caps for all supported planes are the same on DCE and DCN 1 - 3 */
4618 struct dc_plane_cap *plane_cap = &dc->caps.planes[0];
4620 switch (fb->format->format) {
4621 case DRM_FORMAT_P010:
4622 case DRM_FORMAT_NV12:
4623 case DRM_FORMAT_NV21:
4624 *max_upscale = plane_cap->max_upscale_factor.nv12;
4625 *min_downscale = plane_cap->max_downscale_factor.nv12;
4628 case DRM_FORMAT_XRGB16161616F:
4629 case DRM_FORMAT_ARGB16161616F:
4630 case DRM_FORMAT_XBGR16161616F:
4631 case DRM_FORMAT_ABGR16161616F:
4632 *max_upscale = plane_cap->max_upscale_factor.fp16;
4633 *min_downscale = plane_cap->max_downscale_factor.fp16;
4637 *max_upscale = plane_cap->max_upscale_factor.argb8888;
4638 *min_downscale = plane_cap->max_downscale_factor.argb8888;
4643 * A factor of 1 in the plane_cap means to not allow scaling, ie. use a
4644 * scaling factor of 1.0 == 1000 units.
4646 if (*max_upscale == 1)
4647 *max_upscale = 1000;
4649 if (*min_downscale == 1)
4650 *min_downscale = 1000;
4654 static int fill_dc_scaling_info(struct amdgpu_device *adev,
4655 const struct drm_plane_state *state,
4656 struct dc_scaling_info *scaling_info)
4658 int scale_w, scale_h, min_downscale, max_upscale;
4660 memset(scaling_info, 0, sizeof(*scaling_info));
4662 /* Source is fixed 16.16 but we ignore mantissa for now... */
4663 scaling_info->src_rect.x = state->src_x >> 16;
4664 scaling_info->src_rect.y = state->src_y >> 16;
4667 * For reasons we don't (yet) fully understand a non-zero
4668 * src_y coordinate into an NV12 buffer can cause a
4669 * system hang on DCN1x.
4670 * To avoid hangs (and maybe be overly cautious)
4671 * let's reject both non-zero src_x and src_y.
4673 * We currently know of only one use-case to reproduce a
4674 * scenario with non-zero src_x and src_y for NV12, which
4675 * is to gesture the YouTube Android app into full screen
4678 if (((adev->ip_versions[DCE_HWIP][0] == IP_VERSION(1, 0, 0)) ||
4679 (adev->ip_versions[DCE_HWIP][0] == IP_VERSION(1, 0, 1))) &&
4680 (state->fb && state->fb->format->format == DRM_FORMAT_NV12 &&
4681 (scaling_info->src_rect.x != 0 || scaling_info->src_rect.y != 0)))
4684 scaling_info->src_rect.width = state->src_w >> 16;
4685 if (scaling_info->src_rect.width == 0)
4688 scaling_info->src_rect.height = state->src_h >> 16;
4689 if (scaling_info->src_rect.height == 0)
4692 scaling_info->dst_rect.x = state->crtc_x;
4693 scaling_info->dst_rect.y = state->crtc_y;
4695 if (state->crtc_w == 0)
4698 scaling_info->dst_rect.width = state->crtc_w;
4700 if (state->crtc_h == 0)
4703 scaling_info->dst_rect.height = state->crtc_h;
4705 /* DRM doesn't specify clipping on destination output. */
4706 scaling_info->clip_rect = scaling_info->dst_rect;
4708 /* Validate scaling per-format with DC plane caps */
4709 if (state->plane && state->plane->dev && state->fb) {
4710 get_min_max_dc_plane_scaling(state->plane->dev, state->fb,
4711 &min_downscale, &max_upscale);
4713 min_downscale = 250;
4714 max_upscale = 16000;
4717 scale_w = scaling_info->dst_rect.width * 1000 /
4718 scaling_info->src_rect.width;
4720 if (scale_w < min_downscale || scale_w > max_upscale)
4723 scale_h = scaling_info->dst_rect.height * 1000 /
4724 scaling_info->src_rect.height;
4726 if (scale_h < min_downscale || scale_h > max_upscale)
4730 * The "scaling_quality" can be ignored for now, quality = 0 has DC
4731 * assume reasonable defaults based on the format.
4738 fill_gfx8_tiling_info_from_flags(union dc_tiling_info *tiling_info,
4739 uint64_t tiling_flags)
4741 /* Fill GFX8 params */
4742 if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == DC_ARRAY_2D_TILED_THIN1) {
4743 unsigned int bankw, bankh, mtaspect, tile_split, num_banks;
4745 bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
4746 bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
4747 mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
4748 tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT);
4749 num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
4751 /* XXX fix me for VI */
4752 tiling_info->gfx8.num_banks = num_banks;
4753 tiling_info->gfx8.array_mode =
4754 DC_ARRAY_2D_TILED_THIN1;
4755 tiling_info->gfx8.tile_split = tile_split;
4756 tiling_info->gfx8.bank_width = bankw;
4757 tiling_info->gfx8.bank_height = bankh;
4758 tiling_info->gfx8.tile_aspect = mtaspect;
4759 tiling_info->gfx8.tile_mode =
4760 DC_ADDR_SURF_MICRO_TILING_DISPLAY;
4761 } else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE)
4762 == DC_ARRAY_1D_TILED_THIN1) {
4763 tiling_info->gfx8.array_mode = DC_ARRAY_1D_TILED_THIN1;
4766 tiling_info->gfx8.pipe_config =
4767 AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
4771 fill_gfx9_tiling_info_from_device(const struct amdgpu_device *adev,
4772 union dc_tiling_info *tiling_info)
4774 tiling_info->gfx9.num_pipes =
4775 adev->gfx.config.gb_addr_config_fields.num_pipes;
4776 tiling_info->gfx9.num_banks =
4777 adev->gfx.config.gb_addr_config_fields.num_banks;
4778 tiling_info->gfx9.pipe_interleave =
4779 adev->gfx.config.gb_addr_config_fields.pipe_interleave_size;
4780 tiling_info->gfx9.num_shader_engines =
4781 adev->gfx.config.gb_addr_config_fields.num_se;
4782 tiling_info->gfx9.max_compressed_frags =
4783 adev->gfx.config.gb_addr_config_fields.max_compress_frags;
4784 tiling_info->gfx9.num_rb_per_se =
4785 adev->gfx.config.gb_addr_config_fields.num_rb_per_se;
4786 tiling_info->gfx9.shaderEnable = 1;
4787 if (adev->ip_versions[GC_HWIP][0] >= IP_VERSION(10, 3, 0))
4788 tiling_info->gfx9.num_pkrs = adev->gfx.config.gb_addr_config_fields.num_pkrs;
4792 validate_dcc(struct amdgpu_device *adev,
4793 const enum surface_pixel_format format,
4794 const enum dc_rotation_angle rotation,
4795 const union dc_tiling_info *tiling_info,
4796 const struct dc_plane_dcc_param *dcc,
4797 const struct dc_plane_address *address,
4798 const struct plane_size *plane_size)
4800 struct dc *dc = adev->dm.dc;
4801 struct dc_dcc_surface_param input;
4802 struct dc_surface_dcc_cap output;
4804 memset(&input, 0, sizeof(input));
4805 memset(&output, 0, sizeof(output));
4810 if (format >= SURFACE_PIXEL_FORMAT_VIDEO_BEGIN ||
4811 !dc->cap_funcs.get_dcc_compression_cap)
4814 input.format = format;
4815 input.surface_size.width = plane_size->surface_size.width;
4816 input.surface_size.height = plane_size->surface_size.height;
4817 input.swizzle_mode = tiling_info->gfx9.swizzle;
4819 if (rotation == ROTATION_ANGLE_0 || rotation == ROTATION_ANGLE_180)
4820 input.scan = SCAN_DIRECTION_HORIZONTAL;
4821 else if (rotation == ROTATION_ANGLE_90 || rotation == ROTATION_ANGLE_270)
4822 input.scan = SCAN_DIRECTION_VERTICAL;
4824 if (!dc->cap_funcs.get_dcc_compression_cap(dc, &input, &output))
4827 if (!output.capable)
4830 if (dcc->independent_64b_blks == 0 &&
4831 output.grph.rgb.independent_64b_blks != 0)
4838 modifier_has_dcc(uint64_t modifier)
4840 return IS_AMD_FMT_MOD(modifier) && AMD_FMT_MOD_GET(DCC, modifier);
4844 modifier_gfx9_swizzle_mode(uint64_t modifier)
4846 if (modifier == DRM_FORMAT_MOD_LINEAR)
4849 return AMD_FMT_MOD_GET(TILE, modifier);
4852 static const struct drm_format_info *
4853 amd_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
4855 return amdgpu_lookup_format_info(cmd->pixel_format, cmd->modifier[0]);
4859 fill_gfx9_tiling_info_from_modifier(const struct amdgpu_device *adev,
4860 union dc_tiling_info *tiling_info,
4863 unsigned int mod_bank_xor_bits = AMD_FMT_MOD_GET(BANK_XOR_BITS, modifier);
4864 unsigned int mod_pipe_xor_bits = AMD_FMT_MOD_GET(PIPE_XOR_BITS, modifier);
4865 unsigned int pkrs_log2 = AMD_FMT_MOD_GET(PACKERS, modifier);
4866 unsigned int pipes_log2 = min(4u, mod_pipe_xor_bits);
4868 fill_gfx9_tiling_info_from_device(adev, tiling_info);
4870 if (!IS_AMD_FMT_MOD(modifier))
4873 tiling_info->gfx9.num_pipes = 1u << pipes_log2;
4874 tiling_info->gfx9.num_shader_engines = 1u << (mod_pipe_xor_bits - pipes_log2);
4876 if (adev->family >= AMDGPU_FAMILY_NV) {
4877 tiling_info->gfx9.num_pkrs = 1u << pkrs_log2;
4879 tiling_info->gfx9.num_banks = 1u << mod_bank_xor_bits;
4881 /* for DCC we know it isn't rb aligned, so rb_per_se doesn't matter. */
4885 enum dm_micro_swizzle {
4886 MICRO_SWIZZLE_Z = 0,
4887 MICRO_SWIZZLE_S = 1,
4888 MICRO_SWIZZLE_D = 2,
4892 static bool dm_plane_format_mod_supported(struct drm_plane *plane,
4896 struct amdgpu_device *adev = drm_to_adev(plane->dev);
4897 const struct drm_format_info *info = drm_format_info(format);
4900 enum dm_micro_swizzle microtile = modifier_gfx9_swizzle_mode(modifier) & 3;
4906 * We always have to allow these modifiers:
4907 * 1. Core DRM checks for LINEAR support if userspace does not provide modifiers.
4908 * 2. Not passing any modifiers is the same as explicitly passing INVALID.
4910 if (modifier == DRM_FORMAT_MOD_LINEAR ||
4911 modifier == DRM_FORMAT_MOD_INVALID) {
4915 /* Check that the modifier is on the list of the plane's supported modifiers. */
4916 for (i = 0; i < plane->modifier_count; i++) {
4917 if (modifier == plane->modifiers[i])
4920 if (i == plane->modifier_count)
4924 * For D swizzle the canonical modifier depends on the bpp, so check
4927 if (AMD_FMT_MOD_GET(TILE_VERSION, modifier) == AMD_FMT_MOD_TILE_VER_GFX9 &&
4928 adev->family >= AMDGPU_FAMILY_NV) {
4929 if (microtile == MICRO_SWIZZLE_D && info->cpp[0] == 4)
4933 if (adev->family >= AMDGPU_FAMILY_RV && microtile == MICRO_SWIZZLE_D &&
4937 if (modifier_has_dcc(modifier)) {
4938 /* Per radeonsi comments 16/64 bpp are more complicated. */
4939 if (info->cpp[0] != 4)
4941 /* We support multi-planar formats, but not when combined with
4942 * additional DCC metadata planes. */
4943 if (info->num_planes > 1)
4951 add_modifier(uint64_t **mods, uint64_t *size, uint64_t *cap, uint64_t mod)
4956 if (*cap - *size < 1) {
4957 uint64_t new_cap = *cap * 2;
4958 uint64_t *new_mods = kmalloc(new_cap * sizeof(uint64_t), GFP_KERNEL);
4966 memcpy(new_mods, *mods, sizeof(uint64_t) * *size);
4972 (*mods)[*size] = mod;
4977 add_gfx9_modifiers(const struct amdgpu_device *adev,
4978 uint64_t **mods, uint64_t *size, uint64_t *capacity)
4980 int pipes = ilog2(adev->gfx.config.gb_addr_config_fields.num_pipes);
4981 int pipe_xor_bits = min(8, pipes +
4982 ilog2(adev->gfx.config.gb_addr_config_fields.num_se));
4983 int bank_xor_bits = min(8 - pipe_xor_bits,
4984 ilog2(adev->gfx.config.gb_addr_config_fields.num_banks));
4985 int rb = ilog2(adev->gfx.config.gb_addr_config_fields.num_se) +
4986 ilog2(adev->gfx.config.gb_addr_config_fields.num_rb_per_se);
4989 if (adev->family == AMDGPU_FAMILY_RV) {
4990 /* Raven2 and later */
4991 bool has_constant_encode = adev->asic_type > CHIP_RAVEN || adev->external_rev_id >= 0x81;
4994 * No _D DCC swizzles yet because we only allow 32bpp, which
4995 * doesn't support _D on DCN
4998 if (has_constant_encode) {
4999 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5000 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S_X) |
5001 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9) |
5002 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
5003 AMD_FMT_MOD_SET(BANK_XOR_BITS, bank_xor_bits) |
5004 AMD_FMT_MOD_SET(DCC, 1) |
5005 AMD_FMT_MOD_SET(DCC_INDEPENDENT_64B, 1) |
5006 AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_64B) |
5007 AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 1));
5010 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5011 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S_X) |
5012 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9) |
5013 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
5014 AMD_FMT_MOD_SET(BANK_XOR_BITS, bank_xor_bits) |
5015 AMD_FMT_MOD_SET(DCC, 1) |
5016 AMD_FMT_MOD_SET(DCC_INDEPENDENT_64B, 1) |
5017 AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_64B) |
5018 AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 0));
5020 if (has_constant_encode) {
5021 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5022 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S_X) |
5023 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9) |
5024 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
5025 AMD_FMT_MOD_SET(BANK_XOR_BITS, bank_xor_bits) |
5026 AMD_FMT_MOD_SET(DCC, 1) |
5027 AMD_FMT_MOD_SET(DCC_RETILE, 1) |
5028 AMD_FMT_MOD_SET(DCC_INDEPENDENT_64B, 1) |
5029 AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_64B) |
5031 AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 1) |
5032 AMD_FMT_MOD_SET(RB, rb) |
5033 AMD_FMT_MOD_SET(PIPE, pipes));
5036 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5037 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S_X) |
5038 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9) |
5039 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
5040 AMD_FMT_MOD_SET(BANK_XOR_BITS, bank_xor_bits) |
5041 AMD_FMT_MOD_SET(DCC, 1) |
5042 AMD_FMT_MOD_SET(DCC_RETILE, 1) |
5043 AMD_FMT_MOD_SET(DCC_INDEPENDENT_64B, 1) |
5044 AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_64B) |
5045 AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 0) |
5046 AMD_FMT_MOD_SET(RB, rb) |
5047 AMD_FMT_MOD_SET(PIPE, pipes));
5051 * Only supported for 64bpp on Raven, will be filtered on format in
5052 * dm_plane_format_mod_supported.
5054 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5055 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_D_X) |
5056 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9) |
5057 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
5058 AMD_FMT_MOD_SET(BANK_XOR_BITS, bank_xor_bits));
5060 if (adev->family == AMDGPU_FAMILY_RV) {
5061 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5062 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S_X) |
5063 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9) |
5064 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
5065 AMD_FMT_MOD_SET(BANK_XOR_BITS, bank_xor_bits));
5069 * Only supported for 64bpp on Raven, will be filtered on format in
5070 * dm_plane_format_mod_supported.
5072 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5073 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_D) |
5074 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9));
5076 if (adev->family == AMDGPU_FAMILY_RV) {
5077 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5078 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S) |
5079 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9));
5084 add_gfx10_1_modifiers(const struct amdgpu_device *adev,
5085 uint64_t **mods, uint64_t *size, uint64_t *capacity)
5087 int pipe_xor_bits = ilog2(adev->gfx.config.gb_addr_config_fields.num_pipes);
5089 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5090 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_R_X) |
5091 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10) |
5092 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
5093 AMD_FMT_MOD_SET(DCC, 1) |
5094 AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 1) |
5095 AMD_FMT_MOD_SET(DCC_INDEPENDENT_64B, 1) |
5096 AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_64B));
5098 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5099 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_R_X) |
5100 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10) |
5101 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
5102 AMD_FMT_MOD_SET(DCC, 1) |
5103 AMD_FMT_MOD_SET(DCC_RETILE, 1) |
5104 AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 1) |
5105 AMD_FMT_MOD_SET(DCC_INDEPENDENT_64B, 1) |
5106 AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_64B));
5108 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5109 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_R_X) |
5110 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10) |
5111 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits));
5113 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5114 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S_X) |
5115 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10) |
5116 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits));
5119 /* Only supported for 64bpp, will be filtered in dm_plane_format_mod_supported */
5120 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5121 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_D) |
5122 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9));
5124 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5125 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S) |
5126 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9));
5130 add_gfx10_3_modifiers(const struct amdgpu_device *adev,
5131 uint64_t **mods, uint64_t *size, uint64_t *capacity)
5133 int pipe_xor_bits = ilog2(adev->gfx.config.gb_addr_config_fields.num_pipes);
5134 int pkrs = ilog2(adev->gfx.config.gb_addr_config_fields.num_pkrs);
5136 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5137 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_R_X) |
5138 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS) |
5139 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
5140 AMD_FMT_MOD_SET(PACKERS, pkrs) |
5141 AMD_FMT_MOD_SET(DCC, 1) |
5142 AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 1) |
5143 AMD_FMT_MOD_SET(DCC_INDEPENDENT_64B, 1) |
5144 AMD_FMT_MOD_SET(DCC_INDEPENDENT_128B, 1) |
5145 AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_64B));
5147 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5148 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_R_X) |
5149 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS) |
5150 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
5151 AMD_FMT_MOD_SET(PACKERS, pkrs) |
5152 AMD_FMT_MOD_SET(DCC, 1) |
5153 AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 1) |
5154 AMD_FMT_MOD_SET(DCC_INDEPENDENT_128B, 1) |
5155 AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_128B));
5157 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5158 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_R_X) |
5159 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS) |
5160 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
5161 AMD_FMT_MOD_SET(PACKERS, pkrs) |
5162 AMD_FMT_MOD_SET(DCC, 1) |
5163 AMD_FMT_MOD_SET(DCC_RETILE, 1) |
5164 AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 1) |
5165 AMD_FMT_MOD_SET(DCC_INDEPENDENT_64B, 1) |
5166 AMD_FMT_MOD_SET(DCC_INDEPENDENT_128B, 1) |
5167 AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_64B));
5169 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5170 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_R_X) |
5171 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS) |
5172 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
5173 AMD_FMT_MOD_SET(PACKERS, pkrs) |
5174 AMD_FMT_MOD_SET(DCC, 1) |
5175 AMD_FMT_MOD_SET(DCC_RETILE, 1) |
5176 AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, 1) |
5177 AMD_FMT_MOD_SET(DCC_INDEPENDENT_128B, 1) |
5178 AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, AMD_FMT_MOD_DCC_BLOCK_128B));
5180 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5181 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_R_X) |
5182 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS) |
5183 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
5184 AMD_FMT_MOD_SET(PACKERS, pkrs));
5186 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5187 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S_X) |
5188 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS) |
5189 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
5190 AMD_FMT_MOD_SET(PACKERS, pkrs));
5192 /* Only supported for 64bpp, will be filtered in dm_plane_format_mod_supported */
5193 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5194 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_D) |
5195 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9));
5197 add_modifier(mods, size, capacity, AMD_FMT_MOD |
5198 AMD_FMT_MOD_SET(TILE, AMD_FMT_MOD_TILE_GFX9_64K_S) |
5199 AMD_FMT_MOD_SET(TILE_VERSION, AMD_FMT_MOD_TILE_VER_GFX9));
5203 get_plane_modifiers(const struct amdgpu_device *adev, unsigned int plane_type, uint64_t **mods)
5205 uint64_t size = 0, capacity = 128;
5208 /* We have not hooked up any pre-GFX9 modifiers. */
5209 if (adev->family < AMDGPU_FAMILY_AI)
5212 *mods = kmalloc(capacity * sizeof(uint64_t), GFP_KERNEL);
5214 if (plane_type == DRM_PLANE_TYPE_CURSOR) {
5215 add_modifier(mods, &size, &capacity, DRM_FORMAT_MOD_LINEAR);
5216 add_modifier(mods, &size, &capacity, DRM_FORMAT_MOD_INVALID);
5217 return *mods ? 0 : -ENOMEM;
5220 switch (adev->family) {
5221 case AMDGPU_FAMILY_AI:
5222 case AMDGPU_FAMILY_RV:
5223 add_gfx9_modifiers(adev, mods, &size, &capacity);
5225 case AMDGPU_FAMILY_NV:
5226 case AMDGPU_FAMILY_VGH:
5227 case AMDGPU_FAMILY_YC:
5228 case AMDGPU_FAMILY_GC_10_3_6:
5229 case AMDGPU_FAMILY_GC_10_3_7:
5230 if (adev->ip_versions[GC_HWIP][0] >= IP_VERSION(10, 3, 0))
5231 add_gfx10_3_modifiers(adev, mods, &size, &capacity);
5233 add_gfx10_1_modifiers(adev, mods, &size, &capacity);
5237 add_modifier(mods, &size, &capacity, DRM_FORMAT_MOD_LINEAR);
5239 /* INVALID marks the end of the list. */
5240 add_modifier(mods, &size, &capacity, DRM_FORMAT_MOD_INVALID);
5249 fill_gfx9_plane_attributes_from_modifiers(struct amdgpu_device *adev,
5250 const struct amdgpu_framebuffer *afb,
5251 const enum surface_pixel_format format,
5252 const enum dc_rotation_angle rotation,
5253 const struct plane_size *plane_size,
5254 union dc_tiling_info *tiling_info,
5255 struct dc_plane_dcc_param *dcc,
5256 struct dc_plane_address *address,
5257 const bool force_disable_dcc)
5259 const uint64_t modifier = afb->base.modifier;
5262 fill_gfx9_tiling_info_from_modifier(adev, tiling_info, modifier);
5263 tiling_info->gfx9.swizzle = modifier_gfx9_swizzle_mode(modifier);
5265 if (modifier_has_dcc(modifier) && !force_disable_dcc) {
5266 uint64_t dcc_address = afb->address + afb->base.offsets[1];
5267 bool independent_64b_blks = AMD_FMT_MOD_GET(DCC_INDEPENDENT_64B, modifier);
5268 bool independent_128b_blks = AMD_FMT_MOD_GET(DCC_INDEPENDENT_128B, modifier);
5271 dcc->meta_pitch = afb->base.pitches[1];
5272 dcc->independent_64b_blks = independent_64b_blks;
5273 if (AMD_FMT_MOD_GET(TILE_VERSION, modifier) == AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS) {
5274 if (independent_64b_blks && independent_128b_blks)
5275 dcc->dcc_ind_blk = hubp_ind_block_64b_no_128bcl;
5276 else if (independent_128b_blks)
5277 dcc->dcc_ind_blk = hubp_ind_block_128b;
5278 else if (independent_64b_blks && !independent_128b_blks)
5279 dcc->dcc_ind_blk = hubp_ind_block_64b;
5281 dcc->dcc_ind_blk = hubp_ind_block_unconstrained;
5283 if (independent_64b_blks)
5284 dcc->dcc_ind_blk = hubp_ind_block_64b;
5286 dcc->dcc_ind_blk = hubp_ind_block_unconstrained;
5289 address->grph.meta_addr.low_part = lower_32_bits(dcc_address);
5290 address->grph.meta_addr.high_part = upper_32_bits(dcc_address);
5293 ret = validate_dcc(adev, format, rotation, tiling_info, dcc, address, plane_size);
5295 drm_dbg_kms(adev_to_drm(adev), "validate_dcc: returned error: %d\n", ret);
5301 fill_plane_buffer_attributes(struct amdgpu_device *adev,
5302 const struct amdgpu_framebuffer *afb,
5303 const enum surface_pixel_format format,
5304 const enum dc_rotation_angle rotation,
5305 const uint64_t tiling_flags,
5306 union dc_tiling_info *tiling_info,
5307 struct plane_size *plane_size,
5308 struct dc_plane_dcc_param *dcc,
5309 struct dc_plane_address *address,
5311 bool force_disable_dcc)
5313 const struct drm_framebuffer *fb = &afb->base;
5316 memset(tiling_info, 0, sizeof(*tiling_info));
5317 memset(plane_size, 0, sizeof(*plane_size));
5318 memset(dcc, 0, sizeof(*dcc));
5319 memset(address, 0, sizeof(*address));
5321 address->tmz_surface = tmz_surface;
5323 if (format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) {
5324 uint64_t addr = afb->address + fb->offsets[0];
5326 plane_size->surface_size.x = 0;
5327 plane_size->surface_size.y = 0;
5328 plane_size->surface_size.width = fb->width;
5329 plane_size->surface_size.height = fb->height;
5330 plane_size->surface_pitch =
5331 fb->pitches[0] / fb->format->cpp[0];
5333 address->type = PLN_ADDR_TYPE_GRAPHICS;
5334 address->grph.addr.low_part = lower_32_bits(addr);
5335 address->grph.addr.high_part = upper_32_bits(addr);
5336 } else if (format < SURFACE_PIXEL_FORMAT_INVALID) {
5337 uint64_t luma_addr = afb->address + fb->offsets[0];
5338 uint64_t chroma_addr = afb->address + fb->offsets[1];
5340 plane_size->surface_size.x = 0;
5341 plane_size->surface_size.y = 0;
5342 plane_size->surface_size.width = fb->width;
5343 plane_size->surface_size.height = fb->height;
5344 plane_size->surface_pitch =
5345 fb->pitches[0] / fb->format->cpp[0];
5347 plane_size->chroma_size.x = 0;
5348 plane_size->chroma_size.y = 0;
5349 /* TODO: set these based on surface format */
5350 plane_size->chroma_size.width = fb->width / 2;
5351 plane_size->chroma_size.height = fb->height / 2;
5353 plane_size->chroma_pitch =
5354 fb->pitches[1] / fb->format->cpp[1];
5356 address->type = PLN_ADDR_TYPE_VIDEO_PROGRESSIVE;
5357 address->video_progressive.luma_addr.low_part =
5358 lower_32_bits(luma_addr);
5359 address->video_progressive.luma_addr.high_part =
5360 upper_32_bits(luma_addr);
5361 address->video_progressive.chroma_addr.low_part =
5362 lower_32_bits(chroma_addr);
5363 address->video_progressive.chroma_addr.high_part =
5364 upper_32_bits(chroma_addr);
5367 if (adev->family >= AMDGPU_FAMILY_AI) {
5368 ret = fill_gfx9_plane_attributes_from_modifiers(adev, afb, format,
5369 rotation, plane_size,
5376 fill_gfx8_tiling_info_from_flags(tiling_info, tiling_flags);
5383 fill_blending_from_plane_state(const struct drm_plane_state *plane_state,
5384 bool *per_pixel_alpha, bool *global_alpha,
5385 int *global_alpha_value)
5387 *per_pixel_alpha = false;
5388 *global_alpha = false;
5389 *global_alpha_value = 0xff;
5391 if (plane_state->plane->type != DRM_PLANE_TYPE_OVERLAY)
5394 if (plane_state->pixel_blend_mode == DRM_MODE_BLEND_PREMULTI) {
5395 static const uint32_t alpha_formats[] = {
5396 DRM_FORMAT_ARGB8888,
5397 DRM_FORMAT_RGBA8888,
5398 DRM_FORMAT_ABGR8888,
5400 uint32_t format = plane_state->fb->format->format;
5403 for (i = 0; i < ARRAY_SIZE(alpha_formats); ++i) {
5404 if (format == alpha_formats[i]) {
5405 *per_pixel_alpha = true;
5411 if (plane_state->alpha < 0xffff) {
5412 *global_alpha = true;
5413 *global_alpha_value = plane_state->alpha >> 8;
5418 fill_plane_color_attributes(const struct drm_plane_state *plane_state,
5419 const enum surface_pixel_format format,
5420 enum dc_color_space *color_space)
5424 *color_space = COLOR_SPACE_SRGB;
5426 /* DRM color properties only affect non-RGB formats. */
5427 if (format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN)
5430 full_range = (plane_state->color_range == DRM_COLOR_YCBCR_FULL_RANGE);
5432 switch (plane_state->color_encoding) {
5433 case DRM_COLOR_YCBCR_BT601:
5435 *color_space = COLOR_SPACE_YCBCR601;
5437 *color_space = COLOR_SPACE_YCBCR601_LIMITED;
5440 case DRM_COLOR_YCBCR_BT709:
5442 *color_space = COLOR_SPACE_YCBCR709;
5444 *color_space = COLOR_SPACE_YCBCR709_LIMITED;
5447 case DRM_COLOR_YCBCR_BT2020:
5449 *color_space = COLOR_SPACE_2020_YCBCR;
5462 fill_dc_plane_info_and_addr(struct amdgpu_device *adev,
5463 const struct drm_plane_state *plane_state,
5464 const uint64_t tiling_flags,
5465 struct dc_plane_info *plane_info,
5466 struct dc_plane_address *address,
5468 bool force_disable_dcc)
5470 const struct drm_framebuffer *fb = plane_state->fb;
5471 const struct amdgpu_framebuffer *afb =
5472 to_amdgpu_framebuffer(plane_state->fb);
5475 memset(plane_info, 0, sizeof(*plane_info));
5477 switch (fb->format->format) {
5479 plane_info->format =
5480 SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS;
5482 case DRM_FORMAT_RGB565:
5483 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_RGB565;
5485 case DRM_FORMAT_XRGB8888:
5486 case DRM_FORMAT_ARGB8888:
5487 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888;
5489 case DRM_FORMAT_XRGB2101010:
5490 case DRM_FORMAT_ARGB2101010:
5491 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010;
5493 case DRM_FORMAT_XBGR2101010:
5494 case DRM_FORMAT_ABGR2101010:
5495 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010;
5497 case DRM_FORMAT_XBGR8888:
5498 case DRM_FORMAT_ABGR8888:
5499 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR8888;
5501 case DRM_FORMAT_NV21:
5502 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr;
5504 case DRM_FORMAT_NV12:
5505 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb;
5507 case DRM_FORMAT_P010:
5508 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_10bpc_YCrCb;
5510 case DRM_FORMAT_XRGB16161616F:
5511 case DRM_FORMAT_ARGB16161616F:
5512 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616F;
5514 case DRM_FORMAT_XBGR16161616F:
5515 case DRM_FORMAT_ABGR16161616F:
5516 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616F;
5518 case DRM_FORMAT_XRGB16161616:
5519 case DRM_FORMAT_ARGB16161616:
5520 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB16161616;
5522 case DRM_FORMAT_XBGR16161616:
5523 case DRM_FORMAT_ABGR16161616:
5524 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR16161616;
5528 "Unsupported screen format %p4cc\n",
5529 &fb->format->format);
5533 switch (plane_state->rotation & DRM_MODE_ROTATE_MASK) {
5534 case DRM_MODE_ROTATE_0:
5535 plane_info->rotation = ROTATION_ANGLE_0;
5537 case DRM_MODE_ROTATE_90:
5538 plane_info->rotation = ROTATION_ANGLE_90;
5540 case DRM_MODE_ROTATE_180:
5541 plane_info->rotation = ROTATION_ANGLE_180;
5543 case DRM_MODE_ROTATE_270:
5544 plane_info->rotation = ROTATION_ANGLE_270;
5547 plane_info->rotation = ROTATION_ANGLE_0;
5551 plane_info->visible = true;
5552 plane_info->stereo_format = PLANE_STEREO_FORMAT_NONE;
5554 plane_info->layer_index = 0;
5556 ret = fill_plane_color_attributes(plane_state, plane_info->format,
5557 &plane_info->color_space);
5561 ret = fill_plane_buffer_attributes(adev, afb, plane_info->format,
5562 plane_info->rotation, tiling_flags,
5563 &plane_info->tiling_info,
5564 &plane_info->plane_size,
5565 &plane_info->dcc, address, tmz_surface,
5570 fill_blending_from_plane_state(
5571 plane_state, &plane_info->per_pixel_alpha,
5572 &plane_info->global_alpha, &plane_info->global_alpha_value);
5577 static int fill_dc_plane_attributes(struct amdgpu_device *adev,
5578 struct dc_plane_state *dc_plane_state,
5579 struct drm_plane_state *plane_state,
5580 struct drm_crtc_state *crtc_state)
5582 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state);
5583 struct amdgpu_framebuffer *afb = (struct amdgpu_framebuffer *)plane_state->fb;
5584 struct dc_scaling_info scaling_info;
5585 struct dc_plane_info plane_info;
5587 bool force_disable_dcc = false;
5589 ret = fill_dc_scaling_info(adev, plane_state, &scaling_info);
5593 dc_plane_state->src_rect = scaling_info.src_rect;
5594 dc_plane_state->dst_rect = scaling_info.dst_rect;
5595 dc_plane_state->clip_rect = scaling_info.clip_rect;
5596 dc_plane_state->scaling_quality = scaling_info.scaling_quality;
5598 force_disable_dcc = adev->asic_type == CHIP_RAVEN && adev->in_suspend;
5599 ret = fill_dc_plane_info_and_addr(adev, plane_state,
5602 &dc_plane_state->address,
5608 dc_plane_state->format = plane_info.format;
5609 dc_plane_state->color_space = plane_info.color_space;
5610 dc_plane_state->format = plane_info.format;
5611 dc_plane_state->plane_size = plane_info.plane_size;
5612 dc_plane_state->rotation = plane_info.rotation;
5613 dc_plane_state->horizontal_mirror = plane_info.horizontal_mirror;
5614 dc_plane_state->stereo_format = plane_info.stereo_format;
5615 dc_plane_state->tiling_info = plane_info.tiling_info;
5616 dc_plane_state->visible = plane_info.visible;
5617 dc_plane_state->per_pixel_alpha = plane_info.per_pixel_alpha;
5618 dc_plane_state->global_alpha = plane_info.global_alpha;
5619 dc_plane_state->global_alpha_value = plane_info.global_alpha_value;
5620 dc_plane_state->dcc = plane_info.dcc;
5621 dc_plane_state->layer_index = plane_info.layer_index; // Always returns 0
5622 dc_plane_state->flip_int_enabled = true;
5625 * Always set input transfer function, since plane state is refreshed
5628 ret = amdgpu_dm_update_plane_color_mgmt(dm_crtc_state, dc_plane_state);
5635 static void update_stream_scaling_settings(const struct drm_display_mode *mode,
5636 const struct dm_connector_state *dm_state,
5637 struct dc_stream_state *stream)
5639 enum amdgpu_rmx_type rmx_type;
5641 struct rect src = { 0 }; /* viewport in composition space*/
5642 struct rect dst = { 0 }; /* stream addressable area */
5644 /* no mode. nothing to be done */
5648 /* Full screen scaling by default */
5649 src.width = mode->hdisplay;
5650 src.height = mode->vdisplay;
5651 dst.width = stream->timing.h_addressable;
5652 dst.height = stream->timing.v_addressable;
5655 rmx_type = dm_state->scaling;
5656 if (rmx_type == RMX_ASPECT || rmx_type == RMX_OFF) {
5657 if (src.width * dst.height <
5658 src.height * dst.width) {
5659 /* height needs less upscaling/more downscaling */
5660 dst.width = src.width *
5661 dst.height / src.height;
5663 /* width needs less upscaling/more downscaling */
5664 dst.height = src.height *
5665 dst.width / src.width;
5667 } else if (rmx_type == RMX_CENTER) {
5671 dst.x = (stream->timing.h_addressable - dst.width) / 2;
5672 dst.y = (stream->timing.v_addressable - dst.height) / 2;
5674 if (dm_state->underscan_enable) {
5675 dst.x += dm_state->underscan_hborder / 2;
5676 dst.y += dm_state->underscan_vborder / 2;
5677 dst.width -= dm_state->underscan_hborder;
5678 dst.height -= dm_state->underscan_vborder;
5685 DRM_DEBUG_KMS("Destination Rectangle x:%d y:%d width:%d height:%d\n",
5686 dst.x, dst.y, dst.width, dst.height);
5690 static enum dc_color_depth
5691 convert_color_depth_from_display_info(const struct drm_connector *connector,
5692 bool is_y420, int requested_bpc)
5699 /* Cap display bpc based on HDMI 2.0 HF-VSDB */
5700 if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_48)
5702 else if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_36)
5704 else if (connector->display_info.hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_30)
5707 bpc = (uint8_t)connector->display_info.bpc;
5708 /* Assume 8 bpc by default if no bpc is specified. */
5709 bpc = bpc ? bpc : 8;
5712 if (requested_bpc > 0) {
5714 * Cap display bpc based on the user requested value.
5716 * The value for state->max_bpc may not correctly updated
5717 * depending on when the connector gets added to the state
5718 * or if this was called outside of atomic check, so it
5719 * can't be used directly.
5721 bpc = min_t(u8, bpc, requested_bpc);
5723 /* Round down to the nearest even number. */
5724 bpc = bpc - (bpc & 1);
5730 * Temporary Work around, DRM doesn't parse color depth for
5731 * EDID revision before 1.4
5732 * TODO: Fix edid parsing
5734 return COLOR_DEPTH_888;
5736 return COLOR_DEPTH_666;
5738 return COLOR_DEPTH_888;
5740 return COLOR_DEPTH_101010;
5742 return COLOR_DEPTH_121212;
5744 return COLOR_DEPTH_141414;
5746 return COLOR_DEPTH_161616;
5748 return COLOR_DEPTH_UNDEFINED;
5752 static enum dc_aspect_ratio
5753 get_aspect_ratio(const struct drm_display_mode *mode_in)
5755 /* 1-1 mapping, since both enums follow the HDMI spec. */
5756 return (enum dc_aspect_ratio) mode_in->picture_aspect_ratio;
5759 static enum dc_color_space
5760 get_output_color_space(const struct dc_crtc_timing *dc_crtc_timing)
5762 enum dc_color_space color_space = COLOR_SPACE_SRGB;
5764 switch (dc_crtc_timing->pixel_encoding) {
5765 case PIXEL_ENCODING_YCBCR422:
5766 case PIXEL_ENCODING_YCBCR444:
5767 case PIXEL_ENCODING_YCBCR420:
5770 * 27030khz is the separation point between HDTV and SDTV
5771 * according to HDMI spec, we use YCbCr709 and YCbCr601
5774 if (dc_crtc_timing->pix_clk_100hz > 270300) {
5775 if (dc_crtc_timing->flags.Y_ONLY)
5777 COLOR_SPACE_YCBCR709_LIMITED;
5779 color_space = COLOR_SPACE_YCBCR709;
5781 if (dc_crtc_timing->flags.Y_ONLY)
5783 COLOR_SPACE_YCBCR601_LIMITED;
5785 color_space = COLOR_SPACE_YCBCR601;
5790 case PIXEL_ENCODING_RGB:
5791 color_space = COLOR_SPACE_SRGB;
5802 static bool adjust_colour_depth_from_display_info(
5803 struct dc_crtc_timing *timing_out,
5804 const struct drm_display_info *info)
5806 enum dc_color_depth depth = timing_out->display_color_depth;
5809 normalized_clk = timing_out->pix_clk_100hz / 10;
5810 /* YCbCr 4:2:0 requires additional adjustment of 1/2 */
5811 if (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420)
5812 normalized_clk /= 2;
5813 /* Adjusting pix clock following on HDMI spec based on colour depth */
5815 case COLOR_DEPTH_888:
5817 case COLOR_DEPTH_101010:
5818 normalized_clk = (normalized_clk * 30) / 24;
5820 case COLOR_DEPTH_121212:
5821 normalized_clk = (normalized_clk * 36) / 24;
5823 case COLOR_DEPTH_161616:
5824 normalized_clk = (normalized_clk * 48) / 24;
5827 /* The above depths are the only ones valid for HDMI. */
5830 if (normalized_clk <= info->max_tmds_clock) {
5831 timing_out->display_color_depth = depth;
5834 } while (--depth > COLOR_DEPTH_666);
5838 static void fill_stream_properties_from_drm_display_mode(
5839 struct dc_stream_state *stream,
5840 const struct drm_display_mode *mode_in,
5841 const struct drm_connector *connector,
5842 const struct drm_connector_state *connector_state,
5843 const struct dc_stream_state *old_stream,
5846 struct dc_crtc_timing *timing_out = &stream->timing;
5847 const struct drm_display_info *info = &connector->display_info;
5848 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
5849 struct hdmi_vendor_infoframe hv_frame;
5850 struct hdmi_avi_infoframe avi_frame;
5852 memset(&hv_frame, 0, sizeof(hv_frame));
5853 memset(&avi_frame, 0, sizeof(avi_frame));
5855 timing_out->h_border_left = 0;
5856 timing_out->h_border_right = 0;
5857 timing_out->v_border_top = 0;
5858 timing_out->v_border_bottom = 0;
5859 /* TODO: un-hardcode */
5860 if (drm_mode_is_420_only(info, mode_in)
5861 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
5862 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
5863 else if (drm_mode_is_420_also(info, mode_in)
5864 && aconnector->force_yuv420_output)
5865 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
5866 else if ((connector->display_info.color_formats & DRM_COLOR_FORMAT_YCBCR444)
5867 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
5868 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR444;
5870 timing_out->pixel_encoding = PIXEL_ENCODING_RGB;
5872 timing_out->timing_3d_format = TIMING_3D_FORMAT_NONE;
5873 timing_out->display_color_depth = convert_color_depth_from_display_info(
5875 (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420),
5877 timing_out->scan_type = SCANNING_TYPE_NODATA;
5878 timing_out->hdmi_vic = 0;
5881 timing_out->vic = old_stream->timing.vic;
5882 timing_out->flags.HSYNC_POSITIVE_POLARITY = old_stream->timing.flags.HSYNC_POSITIVE_POLARITY;
5883 timing_out->flags.VSYNC_POSITIVE_POLARITY = old_stream->timing.flags.VSYNC_POSITIVE_POLARITY;
5885 timing_out->vic = drm_match_cea_mode(mode_in);
5886 if (mode_in->flags & DRM_MODE_FLAG_PHSYNC)
5887 timing_out->flags.HSYNC_POSITIVE_POLARITY = 1;
5888 if (mode_in->flags & DRM_MODE_FLAG_PVSYNC)
5889 timing_out->flags.VSYNC_POSITIVE_POLARITY = 1;
5892 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) {
5893 drm_hdmi_avi_infoframe_from_display_mode(&avi_frame, (struct drm_connector *)connector, mode_in);
5894 timing_out->vic = avi_frame.video_code;
5895 drm_hdmi_vendor_infoframe_from_display_mode(&hv_frame, (struct drm_connector *)connector, mode_in);
5896 timing_out->hdmi_vic = hv_frame.vic;
5899 if (is_freesync_video_mode(mode_in, aconnector)) {
5900 timing_out->h_addressable = mode_in->hdisplay;
5901 timing_out->h_total = mode_in->htotal;
5902 timing_out->h_sync_width = mode_in->hsync_end - mode_in->hsync_start;
5903 timing_out->h_front_porch = mode_in->hsync_start - mode_in->hdisplay;
5904 timing_out->v_total = mode_in->vtotal;
5905 timing_out->v_addressable = mode_in->vdisplay;
5906 timing_out->v_front_porch = mode_in->vsync_start - mode_in->vdisplay;
5907 timing_out->v_sync_width = mode_in->vsync_end - mode_in->vsync_start;
5908 timing_out->pix_clk_100hz = mode_in->clock * 10;
5910 timing_out->h_addressable = mode_in->crtc_hdisplay;
5911 timing_out->h_total = mode_in->crtc_htotal;
5912 timing_out->h_sync_width = mode_in->crtc_hsync_end - mode_in->crtc_hsync_start;
5913 timing_out->h_front_porch = mode_in->crtc_hsync_start - mode_in->crtc_hdisplay;
5914 timing_out->v_total = mode_in->crtc_vtotal;
5915 timing_out->v_addressable = mode_in->crtc_vdisplay;
5916 timing_out->v_front_porch = mode_in->crtc_vsync_start - mode_in->crtc_vdisplay;
5917 timing_out->v_sync_width = mode_in->crtc_vsync_end - mode_in->crtc_vsync_start;
5918 timing_out->pix_clk_100hz = mode_in->crtc_clock * 10;
5921 timing_out->aspect_ratio = get_aspect_ratio(mode_in);
5923 stream->output_color_space = get_output_color_space(timing_out);
5925 stream->out_transfer_func->type = TF_TYPE_PREDEFINED;
5926 stream->out_transfer_func->tf = TRANSFER_FUNCTION_SRGB;
5927 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) {
5928 if (!adjust_colour_depth_from_display_info(timing_out, info) &&
5929 drm_mode_is_420_also(info, mode_in) &&
5930 timing_out->pixel_encoding != PIXEL_ENCODING_YCBCR420) {
5931 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420;
5932 adjust_colour_depth_from_display_info(timing_out, info);
5937 static void fill_audio_info(struct audio_info *audio_info,
5938 const struct drm_connector *drm_connector,
5939 const struct dc_sink *dc_sink)
5942 int cea_revision = 0;
5943 const struct dc_edid_caps *edid_caps = &dc_sink->edid_caps;
5945 audio_info->manufacture_id = edid_caps->manufacturer_id;
5946 audio_info->product_id = edid_caps->product_id;
5948 cea_revision = drm_connector->display_info.cea_rev;
5950 strscpy(audio_info->display_name,
5951 edid_caps->display_name,
5952 AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS);
5954 if (cea_revision >= 3) {
5955 audio_info->mode_count = edid_caps->audio_mode_count;
5957 for (i = 0; i < audio_info->mode_count; ++i) {
5958 audio_info->modes[i].format_code =
5959 (enum audio_format_code)
5960 (edid_caps->audio_modes[i].format_code);
5961 audio_info->modes[i].channel_count =
5962 edid_caps->audio_modes[i].channel_count;
5963 audio_info->modes[i].sample_rates.all =
5964 edid_caps->audio_modes[i].sample_rate;
5965 audio_info->modes[i].sample_size =
5966 edid_caps->audio_modes[i].sample_size;
5970 audio_info->flags.all = edid_caps->speaker_flags;
5972 /* TODO: We only check for the progressive mode, check for interlace mode too */
5973 if (drm_connector->latency_present[0]) {
5974 audio_info->video_latency = drm_connector->video_latency[0];
5975 audio_info->audio_latency = drm_connector->audio_latency[0];
5978 /* TODO: For DP, video and audio latency should be calculated from DPCD caps */
5983 copy_crtc_timing_for_drm_display_mode(const struct drm_display_mode *src_mode,
5984 struct drm_display_mode *dst_mode)
5986 dst_mode->crtc_hdisplay = src_mode->crtc_hdisplay;
5987 dst_mode->crtc_vdisplay = src_mode->crtc_vdisplay;
5988 dst_mode->crtc_clock = src_mode->crtc_clock;
5989 dst_mode->crtc_hblank_start = src_mode->crtc_hblank_start;
5990 dst_mode->crtc_hblank_end = src_mode->crtc_hblank_end;
5991 dst_mode->crtc_hsync_start = src_mode->crtc_hsync_start;
5992 dst_mode->crtc_hsync_end = src_mode->crtc_hsync_end;
5993 dst_mode->crtc_htotal = src_mode->crtc_htotal;
5994 dst_mode->crtc_hskew = src_mode->crtc_hskew;
5995 dst_mode->crtc_vblank_start = src_mode->crtc_vblank_start;
5996 dst_mode->crtc_vblank_end = src_mode->crtc_vblank_end;
5997 dst_mode->crtc_vsync_start = src_mode->crtc_vsync_start;
5998 dst_mode->crtc_vsync_end = src_mode->crtc_vsync_end;
5999 dst_mode->crtc_vtotal = src_mode->crtc_vtotal;
6003 decide_crtc_timing_for_drm_display_mode(struct drm_display_mode *drm_mode,
6004 const struct drm_display_mode *native_mode,
6007 if (scale_enabled) {
6008 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
6009 } else if (native_mode->clock == drm_mode->clock &&
6010 native_mode->htotal == drm_mode->htotal &&
6011 native_mode->vtotal == drm_mode->vtotal) {
6012 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
6014 /* no scaling nor amdgpu inserted, no need to patch */
6018 static struct dc_sink *
6019 create_fake_sink(struct amdgpu_dm_connector *aconnector)
6021 struct dc_sink_init_data sink_init_data = { 0 };
6022 struct dc_sink *sink = NULL;
6023 sink_init_data.link = aconnector->dc_link;
6024 sink_init_data.sink_signal = aconnector->dc_link->connector_signal;
6026 sink = dc_sink_create(&sink_init_data);
6028 DRM_ERROR("Failed to create sink!\n");
6031 sink->sink_signal = SIGNAL_TYPE_VIRTUAL;
6036 static void set_multisync_trigger_params(
6037 struct dc_stream_state *stream)
6039 struct dc_stream_state *master = NULL;
6041 if (stream->triggered_crtc_reset.enabled) {
6042 master = stream->triggered_crtc_reset.event_source;
6043 stream->triggered_crtc_reset.event =
6044 master->timing.flags.VSYNC_POSITIVE_POLARITY ?
6045 CRTC_EVENT_VSYNC_RISING : CRTC_EVENT_VSYNC_FALLING;
6046 stream->triggered_crtc_reset.delay = TRIGGER_DELAY_NEXT_PIXEL;
6050 static void set_master_stream(struct dc_stream_state *stream_set[],
6053 int j, highest_rfr = 0, master_stream = 0;
6055 for (j = 0; j < stream_count; j++) {
6056 if (stream_set[j] && stream_set[j]->triggered_crtc_reset.enabled) {
6057 int refresh_rate = 0;
6059 refresh_rate = (stream_set[j]->timing.pix_clk_100hz*100)/
6060 (stream_set[j]->timing.h_total*stream_set[j]->timing.v_total);
6061 if (refresh_rate > highest_rfr) {
6062 highest_rfr = refresh_rate;
6067 for (j = 0; j < stream_count; j++) {
6069 stream_set[j]->triggered_crtc_reset.event_source = stream_set[master_stream];
6073 static void dm_enable_per_frame_crtc_master_sync(struct dc_state *context)
6076 struct dc_stream_state *stream;
6078 if (context->stream_count < 2)
6080 for (i = 0; i < context->stream_count ; i++) {
6081 if (!context->streams[i])
6084 * TODO: add a function to read AMD VSDB bits and set
6085 * crtc_sync_master.multi_sync_enabled flag
6086 * For now it's set to false
6090 set_master_stream(context->streams, context->stream_count);
6092 for (i = 0; i < context->stream_count ; i++) {
6093 stream = context->streams[i];
6098 set_multisync_trigger_params(stream);
6102 #if defined(CONFIG_DRM_AMD_DC_DCN)
6103 static void update_dsc_caps(struct amdgpu_dm_connector *aconnector,
6104 struct dc_sink *sink, struct dc_stream_state *stream,
6105 struct dsc_dec_dpcd_caps *dsc_caps)
6107 stream->timing.flags.DSC = 0;
6108 dsc_caps->is_dsc_supported = false;
6110 if (aconnector->dc_link && (sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT ||
6111 sink->sink_signal == SIGNAL_TYPE_EDP)) {
6112 if (sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_NONE ||
6113 sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_DP_HDMI_CONVERTER)
6114 dc_dsc_parse_dsc_dpcd(aconnector->dc_link->ctx->dc,
6115 aconnector->dc_link->dpcd_caps.dsc_caps.dsc_basic_caps.raw,
6116 aconnector->dc_link->dpcd_caps.dsc_caps.dsc_branch_decoder_caps.raw,
6121 static void apply_dsc_policy_for_edp(struct amdgpu_dm_connector *aconnector,
6122 struct dc_sink *sink, struct dc_stream_state *stream,
6123 struct dsc_dec_dpcd_caps *dsc_caps,
6124 uint32_t max_dsc_target_bpp_limit_override)
6126 const struct dc_link_settings *verified_link_cap = NULL;
6127 uint32_t link_bw_in_kbps;
6128 uint32_t edp_min_bpp_x16, edp_max_bpp_x16;
6129 struct dc *dc = sink->ctx->dc;
6130 struct dc_dsc_bw_range bw_range = {0};
6131 struct dc_dsc_config dsc_cfg = {0};
6133 verified_link_cap = dc_link_get_link_cap(stream->link);
6134 link_bw_in_kbps = dc_link_bandwidth_kbps(stream->link, verified_link_cap);
6135 edp_min_bpp_x16 = 8 * 16;
6136 edp_max_bpp_x16 = 8 * 16;
6138 if (edp_max_bpp_x16 > dsc_caps->edp_max_bits_per_pixel)
6139 edp_max_bpp_x16 = dsc_caps->edp_max_bits_per_pixel;
6141 if (edp_max_bpp_x16 < edp_min_bpp_x16)
6142 edp_min_bpp_x16 = edp_max_bpp_x16;
6144 if (dc_dsc_compute_bandwidth_range(dc->res_pool->dscs[0],
6145 dc->debug.dsc_min_slice_height_override,
6146 edp_min_bpp_x16, edp_max_bpp_x16,
6151 if (bw_range.max_kbps < link_bw_in_kbps) {
6152 if (dc_dsc_compute_config(dc->res_pool->dscs[0],
6154 dc->debug.dsc_min_slice_height_override,
6155 max_dsc_target_bpp_limit_override,
6159 stream->timing.dsc_cfg = dsc_cfg;
6160 stream->timing.flags.DSC = 1;
6161 stream->timing.dsc_cfg.bits_per_pixel = edp_max_bpp_x16;
6167 if (dc_dsc_compute_config(dc->res_pool->dscs[0],
6169 dc->debug.dsc_min_slice_height_override,
6170 max_dsc_target_bpp_limit_override,
6174 stream->timing.dsc_cfg = dsc_cfg;
6175 stream->timing.flags.DSC = 1;
6179 static void apply_dsc_policy_for_stream(struct amdgpu_dm_connector *aconnector,
6180 struct dc_sink *sink, struct dc_stream_state *stream,
6181 struct dsc_dec_dpcd_caps *dsc_caps)
6183 struct drm_connector *drm_connector = &aconnector->base;
6184 uint32_t link_bandwidth_kbps;
6185 uint32_t max_dsc_target_bpp_limit_override = 0;
6186 struct dc *dc = sink->ctx->dc;
6187 uint32_t max_supported_bw_in_kbps, timing_bw_in_kbps;
6188 uint32_t dsc_max_supported_bw_in_kbps;
6190 link_bandwidth_kbps = dc_link_bandwidth_kbps(aconnector->dc_link,
6191 dc_link_get_link_cap(aconnector->dc_link));
6193 if (stream->link && stream->link->local_sink)
6194 max_dsc_target_bpp_limit_override =
6195 stream->link->local_sink->edid_caps.panel_patch.max_dsc_target_bpp_limit;
6197 /* Set DSC policy according to dsc_clock_en */
6198 dc_dsc_policy_set_enable_dsc_when_not_needed(
6199 aconnector->dsc_settings.dsc_force_enable == DSC_CLK_FORCE_ENABLE);
6201 if (aconnector->dc_link && sink->sink_signal == SIGNAL_TYPE_EDP && !dc->debug.disable_dsc_edp &&
6202 dc->caps.edp_dsc_support && aconnector->dsc_settings.dsc_force_enable != DSC_CLK_FORCE_DISABLE) {
6204 apply_dsc_policy_for_edp(aconnector, sink, stream, dsc_caps, max_dsc_target_bpp_limit_override);
6206 } else if (aconnector->dc_link && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT) {
6207 if (sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_NONE) {
6208 if (dc_dsc_compute_config(aconnector->dc_link->ctx->dc->res_pool->dscs[0],
6210 aconnector->dc_link->ctx->dc->debug.dsc_min_slice_height_override,
6211 max_dsc_target_bpp_limit_override,
6212 link_bandwidth_kbps,
6214 &stream->timing.dsc_cfg)) {
6215 stream->timing.flags.DSC = 1;
6216 DRM_DEBUG_DRIVER("%s: [%s] DSC is selected from SST RX\n",
6217 __func__, drm_connector->name);
6219 } else if (sink->link->dpcd_caps.dongle_type == DISPLAY_DONGLE_DP_HDMI_CONVERTER) {
6220 timing_bw_in_kbps = dc_bandwidth_in_kbps_from_timing(&stream->timing);
6221 max_supported_bw_in_kbps = link_bandwidth_kbps;
6222 dsc_max_supported_bw_in_kbps = link_bandwidth_kbps;
6224 if (timing_bw_in_kbps > max_supported_bw_in_kbps &&
6225 max_supported_bw_in_kbps > 0 &&
6226 dsc_max_supported_bw_in_kbps > 0)
6227 if (dc_dsc_compute_config(aconnector->dc_link->ctx->dc->res_pool->dscs[0],
6229 aconnector->dc_link->ctx->dc->debug.dsc_min_slice_height_override,
6230 max_dsc_target_bpp_limit_override,
6231 dsc_max_supported_bw_in_kbps,
6233 &stream->timing.dsc_cfg)) {
6234 stream->timing.flags.DSC = 1;
6235 DRM_DEBUG_DRIVER("%s: [%s] DSC is selected from DP-HDMI PCON\n",
6236 __func__, drm_connector->name);
6241 /* Overwrite the stream flag if DSC is enabled through debugfs */
6242 if (aconnector->dsc_settings.dsc_force_enable == DSC_CLK_FORCE_ENABLE)
6243 stream->timing.flags.DSC = 1;
6245 if (stream->timing.flags.DSC && aconnector->dsc_settings.dsc_num_slices_h)
6246 stream->timing.dsc_cfg.num_slices_h = aconnector->dsc_settings.dsc_num_slices_h;
6248 if (stream->timing.flags.DSC && aconnector->dsc_settings.dsc_num_slices_v)
6249 stream->timing.dsc_cfg.num_slices_v = aconnector->dsc_settings.dsc_num_slices_v;
6251 if (stream->timing.flags.DSC && aconnector->dsc_settings.dsc_bits_per_pixel)
6252 stream->timing.dsc_cfg.bits_per_pixel = aconnector->dsc_settings.dsc_bits_per_pixel;
6254 #endif /* CONFIG_DRM_AMD_DC_DCN */
6257 * DOC: FreeSync Video
6259 * When a userspace application wants to play a video, the content follows a
6260 * standard format definition that usually specifies the FPS for that format.
6261 * The below list illustrates some video format and the expected FPS,
6264 * - TV/NTSC (23.976 FPS)
6267 * - TV/NTSC (29.97 FPS)
6268 * - TV/NTSC (30 FPS)
6269 * - Cinema HFR (48 FPS)
6271 * - Commonly used (60 FPS)
6272 * - Multiples of 24 (48,72,96,120 FPS)
6274 * The list of standards video format is not huge and can be added to the
6275 * connector modeset list beforehand. With that, userspace can leverage
6276 * FreeSync to extends the front porch in order to attain the target refresh
6277 * rate. Such a switch will happen seamlessly, without screen blanking or
6278 * reprogramming of the output in any other way. If the userspace requests a
6279 * modesetting change compatible with FreeSync modes that only differ in the
6280 * refresh rate, DC will skip the full update and avoid blink during the
6281 * transition. For example, the video player can change the modesetting from
6282 * 60Hz to 30Hz for playing TV/NTSC content when it goes full screen without
6283 * causing any display blink. This same concept can be applied to a mode
6286 static struct drm_display_mode *
6287 get_highest_refresh_rate_mode(struct amdgpu_dm_connector *aconnector,
6288 bool use_probed_modes)
6290 struct drm_display_mode *m, *m_pref = NULL;
6291 u16 current_refresh, highest_refresh;
6292 struct list_head *list_head = use_probed_modes ?
6293 &aconnector->base.probed_modes :
6294 &aconnector->base.modes;
6296 if (aconnector->freesync_vid_base.clock != 0)
6297 return &aconnector->freesync_vid_base;
6299 /* Find the preferred mode */
6300 list_for_each_entry (m, list_head, head) {
6301 if (m->type & DRM_MODE_TYPE_PREFERRED) {
6308 /* Probably an EDID with no preferred mode. Fallback to first entry */
6309 m_pref = list_first_entry_or_null(
6310 &aconnector->base.modes, struct drm_display_mode, head);
6312 DRM_DEBUG_DRIVER("No preferred mode found in EDID\n");
6317 highest_refresh = drm_mode_vrefresh(m_pref);
6320 * Find the mode with highest refresh rate with same resolution.
6321 * For some monitors, preferred mode is not the mode with highest
6322 * supported refresh rate.
6324 list_for_each_entry (m, list_head, head) {
6325 current_refresh = drm_mode_vrefresh(m);
6327 if (m->hdisplay == m_pref->hdisplay &&
6328 m->vdisplay == m_pref->vdisplay &&
6329 highest_refresh < current_refresh) {
6330 highest_refresh = current_refresh;
6335 drm_mode_copy(&aconnector->freesync_vid_base, m_pref);
6339 static bool is_freesync_video_mode(const struct drm_display_mode *mode,
6340 struct amdgpu_dm_connector *aconnector)
6342 struct drm_display_mode *high_mode;
6345 high_mode = get_highest_refresh_rate_mode(aconnector, false);
6346 if (!high_mode || !mode)
6349 timing_diff = high_mode->vtotal - mode->vtotal;
6351 if (high_mode->clock == 0 || high_mode->clock != mode->clock ||
6352 high_mode->hdisplay != mode->hdisplay ||
6353 high_mode->vdisplay != mode->vdisplay ||
6354 high_mode->hsync_start != mode->hsync_start ||
6355 high_mode->hsync_end != mode->hsync_end ||
6356 high_mode->htotal != mode->htotal ||
6357 high_mode->hskew != mode->hskew ||
6358 high_mode->vscan != mode->vscan ||
6359 high_mode->vsync_start - mode->vsync_start != timing_diff ||
6360 high_mode->vsync_end - mode->vsync_end != timing_diff)
6366 static struct dc_stream_state *
6367 create_stream_for_sink(struct amdgpu_dm_connector *aconnector,
6368 const struct drm_display_mode *drm_mode,
6369 const struct dm_connector_state *dm_state,
6370 const struct dc_stream_state *old_stream,
6373 struct drm_display_mode *preferred_mode = NULL;
6374 struct drm_connector *drm_connector;
6375 const struct drm_connector_state *con_state =
6376 dm_state ? &dm_state->base : NULL;
6377 struct dc_stream_state *stream = NULL;
6378 struct drm_display_mode mode = *drm_mode;
6379 struct drm_display_mode saved_mode;
6380 struct drm_display_mode *freesync_mode = NULL;
6381 bool native_mode_found = false;
6382 bool recalculate_timing = false;
6383 bool scale = dm_state ? (dm_state->scaling != RMX_OFF) : false;
6385 int preferred_refresh = 0;
6386 #if defined(CONFIG_DRM_AMD_DC_DCN)
6387 struct dsc_dec_dpcd_caps dsc_caps;
6389 struct dc_sink *sink = NULL;
6391 memset(&saved_mode, 0, sizeof(saved_mode));
6393 if (aconnector == NULL) {
6394 DRM_ERROR("aconnector is NULL!\n");
6398 drm_connector = &aconnector->base;
6400 if (!aconnector->dc_sink) {
6401 sink = create_fake_sink(aconnector);
6405 sink = aconnector->dc_sink;
6406 dc_sink_retain(sink);
6409 stream = dc_create_stream_for_sink(sink);
6411 if (stream == NULL) {
6412 DRM_ERROR("Failed to create stream for sink!\n");
6416 stream->dm_stream_context = aconnector;
6418 stream->timing.flags.LTE_340MCSC_SCRAMBLE =
6419 drm_connector->display_info.hdmi.scdc.scrambling.low_rates;
6421 list_for_each_entry(preferred_mode, &aconnector->base.modes, head) {
6422 /* Search for preferred mode */
6423 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED) {
6424 native_mode_found = true;
6428 if (!native_mode_found)
6429 preferred_mode = list_first_entry_or_null(
6430 &aconnector->base.modes,
6431 struct drm_display_mode,
6434 mode_refresh = drm_mode_vrefresh(&mode);
6436 if (preferred_mode == NULL) {
6438 * This may not be an error, the use case is when we have no
6439 * usermode calls to reset and set mode upon hotplug. In this
6440 * case, we call set mode ourselves to restore the previous mode
6441 * and the modelist may not be filled in in time.
6443 DRM_DEBUG_DRIVER("No preferred mode found\n");
6445 recalculate_timing = is_freesync_video_mode(&mode, aconnector);
6446 if (recalculate_timing) {
6447 freesync_mode = get_highest_refresh_rate_mode(aconnector, false);
6448 drm_mode_copy(&saved_mode, &mode);
6449 drm_mode_copy(&mode, freesync_mode);
6451 decide_crtc_timing_for_drm_display_mode(
6452 &mode, preferred_mode, scale);
6454 preferred_refresh = drm_mode_vrefresh(preferred_mode);
6458 if (recalculate_timing)
6459 drm_mode_set_crtcinfo(&saved_mode, 0);
6461 drm_mode_set_crtcinfo(&mode, 0);
6464 * If scaling is enabled and refresh rate didn't change
6465 * we copy the vic and polarities of the old timings
6467 if (!scale || mode_refresh != preferred_refresh)
6468 fill_stream_properties_from_drm_display_mode(
6469 stream, &mode, &aconnector->base, con_state, NULL,
6472 fill_stream_properties_from_drm_display_mode(
6473 stream, &mode, &aconnector->base, con_state, old_stream,
6476 #if defined(CONFIG_DRM_AMD_DC_DCN)
6477 /* SST DSC determination policy */
6478 update_dsc_caps(aconnector, sink, stream, &dsc_caps);
6479 if (aconnector->dsc_settings.dsc_force_enable != DSC_CLK_FORCE_DISABLE && dsc_caps.is_dsc_supported)
6480 apply_dsc_policy_for_stream(aconnector, sink, stream, &dsc_caps);
6483 update_stream_scaling_settings(&mode, dm_state, stream);
6486 &stream->audio_info,
6490 update_stream_signal(stream, sink);
6492 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A)
6493 mod_build_hf_vsif_infopacket(stream, &stream->vsp_infopacket);
6495 if (stream->link->psr_settings.psr_feature_enabled) {
6497 // should decide stream support vsc sdp colorimetry capability
6498 // before building vsc info packet
6500 stream->use_vsc_sdp_for_colorimetry = false;
6501 if (aconnector->dc_sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
6502 stream->use_vsc_sdp_for_colorimetry =
6503 aconnector->dc_sink->is_vsc_sdp_colorimetry_supported;
6505 if (stream->link->dpcd_caps.dprx_feature.bits.VSC_SDP_COLORIMETRY_SUPPORTED)
6506 stream->use_vsc_sdp_for_colorimetry = true;
6508 mod_build_vsc_infopacket(stream, &stream->vsc_infopacket, stream->output_color_space);
6509 aconnector->psr_skip_count = AMDGPU_DM_PSR_ENTRY_DELAY;
6513 dc_sink_release(sink);
6518 static void amdgpu_dm_crtc_destroy(struct drm_crtc *crtc)
6520 drm_crtc_cleanup(crtc);
6524 static void dm_crtc_destroy_state(struct drm_crtc *crtc,
6525 struct drm_crtc_state *state)
6527 struct dm_crtc_state *cur = to_dm_crtc_state(state);
6529 /* TODO Destroy dc_stream objects are stream object is flattened */
6531 dc_stream_release(cur->stream);
6534 __drm_atomic_helper_crtc_destroy_state(state);
6540 static void dm_crtc_reset_state(struct drm_crtc *crtc)
6542 struct dm_crtc_state *state;
6545 dm_crtc_destroy_state(crtc, crtc->state);
6547 state = kzalloc(sizeof(*state), GFP_KERNEL);
6548 if (WARN_ON(!state))
6551 __drm_atomic_helper_crtc_reset(crtc, &state->base);
6554 static struct drm_crtc_state *
6555 dm_crtc_duplicate_state(struct drm_crtc *crtc)
6557 struct dm_crtc_state *state, *cur;
6559 cur = to_dm_crtc_state(crtc->state);
6561 if (WARN_ON(!crtc->state))
6564 state = kzalloc(sizeof(*state), GFP_KERNEL);
6568 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
6571 state->stream = cur->stream;
6572 dc_stream_retain(state->stream);
6575 state->active_planes = cur->active_planes;
6576 state->vrr_infopacket = cur->vrr_infopacket;
6577 state->abm_level = cur->abm_level;
6578 state->vrr_supported = cur->vrr_supported;
6579 state->freesync_config = cur->freesync_config;
6580 state->cm_has_degamma = cur->cm_has_degamma;
6581 state->cm_is_degamma_srgb = cur->cm_is_degamma_srgb;
6582 state->force_dpms_off = cur->force_dpms_off;
6583 /* TODO Duplicate dc_stream after objects are stream object is flattened */
6585 return &state->base;
6588 #ifdef CONFIG_DRM_AMD_SECURE_DISPLAY
6589 static int amdgpu_dm_crtc_late_register(struct drm_crtc *crtc)
6591 crtc_debugfs_init(crtc);
6597 static inline int dm_set_vupdate_irq(struct drm_crtc *crtc, bool enable)
6599 enum dc_irq_source irq_source;
6600 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
6601 struct amdgpu_device *adev = drm_to_adev(crtc->dev);
6604 irq_source = IRQ_TYPE_VUPDATE + acrtc->otg_inst;
6606 rc = dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY;
6608 DRM_DEBUG_VBL("crtc %d - vupdate irq %sabling: r=%d\n",
6609 acrtc->crtc_id, enable ? "en" : "dis", rc);
6613 static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
6615 enum dc_irq_source irq_source;
6616 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
6617 struct amdgpu_device *adev = drm_to_adev(crtc->dev);
6618 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
6619 struct amdgpu_display_manager *dm = &adev->dm;
6620 struct vblank_control_work *work;
6624 /* vblank irq on -> Only need vupdate irq in vrr mode */
6625 if (amdgpu_dm_vrr_active(acrtc_state))
6626 rc = dm_set_vupdate_irq(crtc, true);
6628 /* vblank irq off -> vupdate irq off */
6629 rc = dm_set_vupdate_irq(crtc, false);
6635 irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
6637 if (!dc_interrupt_set(adev->dm.dc, irq_source, enable))
6640 if (amdgpu_in_reset(adev))
6643 if (dm->vblank_control_workqueue) {
6644 work = kzalloc(sizeof(*work), GFP_ATOMIC);
6648 INIT_WORK(&work->work, vblank_control_worker);
6650 work->acrtc = acrtc;
6651 work->enable = enable;
6653 if (acrtc_state->stream) {
6654 dc_stream_retain(acrtc_state->stream);
6655 work->stream = acrtc_state->stream;
6658 queue_work(dm->vblank_control_workqueue, &work->work);
6664 static int dm_enable_vblank(struct drm_crtc *crtc)
6666 return dm_set_vblank(crtc, true);
6669 static void dm_disable_vblank(struct drm_crtc *crtc)
6671 dm_set_vblank(crtc, false);
6674 /* Implemented only the options currently availible for the driver */
6675 static const struct drm_crtc_funcs amdgpu_dm_crtc_funcs = {
6676 .reset = dm_crtc_reset_state,
6677 .destroy = amdgpu_dm_crtc_destroy,
6678 .set_config = drm_atomic_helper_set_config,
6679 .page_flip = drm_atomic_helper_page_flip,
6680 .atomic_duplicate_state = dm_crtc_duplicate_state,
6681 .atomic_destroy_state = dm_crtc_destroy_state,
6682 .set_crc_source = amdgpu_dm_crtc_set_crc_source,
6683 .verify_crc_source = amdgpu_dm_crtc_verify_crc_source,
6684 .get_crc_sources = amdgpu_dm_crtc_get_crc_sources,
6685 .get_vblank_counter = amdgpu_get_vblank_counter_kms,
6686 .enable_vblank = dm_enable_vblank,
6687 .disable_vblank = dm_disable_vblank,
6688 .get_vblank_timestamp = drm_crtc_vblank_helper_get_vblank_timestamp,
6689 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
6690 .late_register = amdgpu_dm_crtc_late_register,
6694 static enum drm_connector_status
6695 amdgpu_dm_connector_detect(struct drm_connector *connector, bool force)
6698 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
6702 * 1. This interface is NOT called in context of HPD irq.
6703 * 2. This interface *is called* in context of user-mode ioctl. Which
6704 * makes it a bad place for *any* MST-related activity.
6707 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED &&
6708 !aconnector->fake_enable)
6709 connected = (aconnector->dc_sink != NULL);
6711 connected = (aconnector->base.force == DRM_FORCE_ON);
6713 update_subconnector_property(aconnector);
6715 return (connected ? connector_status_connected :
6716 connector_status_disconnected);
6719 int amdgpu_dm_connector_atomic_set_property(struct drm_connector *connector,
6720 struct drm_connector_state *connector_state,
6721 struct drm_property *property,
6724 struct drm_device *dev = connector->dev;
6725 struct amdgpu_device *adev = drm_to_adev(dev);
6726 struct dm_connector_state *dm_old_state =
6727 to_dm_connector_state(connector->state);
6728 struct dm_connector_state *dm_new_state =
6729 to_dm_connector_state(connector_state);
6733 if (property == dev->mode_config.scaling_mode_property) {
6734 enum amdgpu_rmx_type rmx_type;
6737 case DRM_MODE_SCALE_CENTER:
6738 rmx_type = RMX_CENTER;
6740 case DRM_MODE_SCALE_ASPECT:
6741 rmx_type = RMX_ASPECT;
6743 case DRM_MODE_SCALE_FULLSCREEN:
6744 rmx_type = RMX_FULL;
6746 case DRM_MODE_SCALE_NONE:
6752 if (dm_old_state->scaling == rmx_type)
6755 dm_new_state->scaling = rmx_type;
6757 } else if (property == adev->mode_info.underscan_hborder_property) {
6758 dm_new_state->underscan_hborder = val;
6760 } else if (property == adev->mode_info.underscan_vborder_property) {
6761 dm_new_state->underscan_vborder = val;
6763 } else if (property == adev->mode_info.underscan_property) {
6764 dm_new_state->underscan_enable = val;
6766 } else if (property == adev->mode_info.abm_level_property) {
6767 dm_new_state->abm_level = val;
6774 int amdgpu_dm_connector_atomic_get_property(struct drm_connector *connector,
6775 const struct drm_connector_state *state,
6776 struct drm_property *property,
6779 struct drm_device *dev = connector->dev;
6780 struct amdgpu_device *adev = drm_to_adev(dev);
6781 struct dm_connector_state *dm_state =
6782 to_dm_connector_state(state);
6785 if (property == dev->mode_config.scaling_mode_property) {
6786 switch (dm_state->scaling) {
6788 *val = DRM_MODE_SCALE_CENTER;
6791 *val = DRM_MODE_SCALE_ASPECT;
6794 *val = DRM_MODE_SCALE_FULLSCREEN;
6798 *val = DRM_MODE_SCALE_NONE;
6802 } else if (property == adev->mode_info.underscan_hborder_property) {
6803 *val = dm_state->underscan_hborder;
6805 } else if (property == adev->mode_info.underscan_vborder_property) {
6806 *val = dm_state->underscan_vborder;
6808 } else if (property == adev->mode_info.underscan_property) {
6809 *val = dm_state->underscan_enable;
6811 } else if (property == adev->mode_info.abm_level_property) {
6812 *val = dm_state->abm_level;
6819 static void amdgpu_dm_connector_unregister(struct drm_connector *connector)
6821 struct amdgpu_dm_connector *amdgpu_dm_connector = to_amdgpu_dm_connector(connector);
6823 drm_dp_aux_unregister(&amdgpu_dm_connector->dm_dp_aux.aux);
6826 static void amdgpu_dm_connector_destroy(struct drm_connector *connector)
6828 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
6829 const struct dc_link *link = aconnector->dc_link;
6830 struct amdgpu_device *adev = drm_to_adev(connector->dev);
6831 struct amdgpu_display_manager *dm = &adev->dm;
6835 * Call only if mst_mgr was iniitalized before since it's not done
6836 * for all connector types.
6838 if (aconnector->mst_mgr.dev)
6839 drm_dp_mst_topology_mgr_destroy(&aconnector->mst_mgr);
6841 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
6842 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
6843 for (i = 0; i < dm->num_of_edps; i++) {
6844 if ((link == dm->backlight_link[i]) && dm->backlight_dev[i]) {
6845 backlight_device_unregister(dm->backlight_dev[i]);
6846 dm->backlight_dev[i] = NULL;
6851 if (aconnector->dc_em_sink)
6852 dc_sink_release(aconnector->dc_em_sink);
6853 aconnector->dc_em_sink = NULL;
6854 if (aconnector->dc_sink)
6855 dc_sink_release(aconnector->dc_sink);
6856 aconnector->dc_sink = NULL;
6858 drm_dp_cec_unregister_connector(&aconnector->dm_dp_aux.aux);
6859 drm_connector_unregister(connector);
6860 drm_connector_cleanup(connector);
6861 if (aconnector->i2c) {
6862 i2c_del_adapter(&aconnector->i2c->base);
6863 kfree(aconnector->i2c);
6865 kfree(aconnector->dm_dp_aux.aux.name);
6870 void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector)
6872 struct dm_connector_state *state =
6873 to_dm_connector_state(connector->state);
6875 if (connector->state)
6876 __drm_atomic_helper_connector_destroy_state(connector->state);
6880 state = kzalloc(sizeof(*state), GFP_KERNEL);
6883 state->scaling = RMX_OFF;
6884 state->underscan_enable = false;
6885 state->underscan_hborder = 0;
6886 state->underscan_vborder = 0;
6887 state->base.max_requested_bpc = 8;
6888 state->vcpi_slots = 0;
6890 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
6891 state->abm_level = amdgpu_dm_abm_level;
6893 __drm_atomic_helper_connector_reset(connector, &state->base);
6897 struct drm_connector_state *
6898 amdgpu_dm_connector_atomic_duplicate_state(struct drm_connector *connector)
6900 struct dm_connector_state *state =
6901 to_dm_connector_state(connector->state);
6903 struct dm_connector_state *new_state =
6904 kmemdup(state, sizeof(*state), GFP_KERNEL);
6909 __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base);
6911 new_state->freesync_capable = state->freesync_capable;
6912 new_state->abm_level = state->abm_level;
6913 new_state->scaling = state->scaling;
6914 new_state->underscan_enable = state->underscan_enable;
6915 new_state->underscan_hborder = state->underscan_hborder;
6916 new_state->underscan_vborder = state->underscan_vborder;
6917 new_state->vcpi_slots = state->vcpi_slots;
6918 new_state->pbn = state->pbn;
6919 return &new_state->base;
6923 amdgpu_dm_connector_late_register(struct drm_connector *connector)
6925 struct amdgpu_dm_connector *amdgpu_dm_connector =
6926 to_amdgpu_dm_connector(connector);
6929 if ((connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
6930 (connector->connector_type == DRM_MODE_CONNECTOR_eDP)) {
6931 amdgpu_dm_connector->dm_dp_aux.aux.dev = connector->kdev;
6932 r = drm_dp_aux_register(&amdgpu_dm_connector->dm_dp_aux.aux);
6937 #if defined(CONFIG_DEBUG_FS)
6938 connector_debugfs_init(amdgpu_dm_connector);
6944 static const struct drm_connector_funcs amdgpu_dm_connector_funcs = {
6945 .reset = amdgpu_dm_connector_funcs_reset,
6946 .detect = amdgpu_dm_connector_detect,
6947 .fill_modes = drm_helper_probe_single_connector_modes,
6948 .destroy = amdgpu_dm_connector_destroy,
6949 .atomic_duplicate_state = amdgpu_dm_connector_atomic_duplicate_state,
6950 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
6951 .atomic_set_property = amdgpu_dm_connector_atomic_set_property,
6952 .atomic_get_property = amdgpu_dm_connector_atomic_get_property,
6953 .late_register = amdgpu_dm_connector_late_register,
6954 .early_unregister = amdgpu_dm_connector_unregister
6957 static int get_modes(struct drm_connector *connector)
6959 return amdgpu_dm_connector_get_modes(connector);
6962 static void create_eml_sink(struct amdgpu_dm_connector *aconnector)
6964 struct dc_sink_init_data init_params = {
6965 .link = aconnector->dc_link,
6966 .sink_signal = SIGNAL_TYPE_VIRTUAL
6970 if (!aconnector->base.edid_blob_ptr) {
6971 DRM_ERROR("No EDID firmware found on connector: %s ,forcing to OFF!\n",
6972 aconnector->base.name);
6974 aconnector->base.force = DRM_FORCE_OFF;
6975 aconnector->base.override_edid = false;
6979 edid = (struct edid *) aconnector->base.edid_blob_ptr->data;
6981 aconnector->edid = edid;
6983 aconnector->dc_em_sink = dc_link_add_remote_sink(
6984 aconnector->dc_link,
6986 (edid->extensions + 1) * EDID_LENGTH,
6989 if (aconnector->base.force == DRM_FORCE_ON) {
6990 aconnector->dc_sink = aconnector->dc_link->local_sink ?
6991 aconnector->dc_link->local_sink :
6992 aconnector->dc_em_sink;
6993 dc_sink_retain(aconnector->dc_sink);
6997 static void handle_edid_mgmt(struct amdgpu_dm_connector *aconnector)
6999 struct dc_link *link = (struct dc_link *)aconnector->dc_link;
7002 * In case of headless boot with force on for DP managed connector
7003 * Those settings have to be != 0 to get initial modeset
7005 if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT) {
7006 link->verified_link_cap.lane_count = LANE_COUNT_FOUR;
7007 link->verified_link_cap.link_rate = LINK_RATE_HIGH2;
7011 aconnector->base.override_edid = true;
7012 create_eml_sink(aconnector);
7015 struct dc_stream_state *
7016 create_validate_stream_for_sink(struct amdgpu_dm_connector *aconnector,
7017 const struct drm_display_mode *drm_mode,
7018 const struct dm_connector_state *dm_state,
7019 const struct dc_stream_state *old_stream)
7021 struct drm_connector *connector = &aconnector->base;
7022 struct amdgpu_device *adev = drm_to_adev(connector->dev);
7023 struct dc_stream_state *stream;
7024 const struct drm_connector_state *drm_state = dm_state ? &dm_state->base : NULL;
7025 int requested_bpc = drm_state ? drm_state->max_requested_bpc : 8;
7026 enum dc_status dc_result = DC_OK;
7029 stream = create_stream_for_sink(aconnector, drm_mode,
7030 dm_state, old_stream,
7032 if (stream == NULL) {
7033 DRM_ERROR("Failed to create stream for sink!\n");
7037 dc_result = dc_validate_stream(adev->dm.dc, stream);
7039 if (dc_result != DC_OK) {
7040 DRM_DEBUG_KMS("Mode %dx%d (clk %d) failed DC validation with error %d (%s)\n",
7045 dc_status_to_str(dc_result));
7047 dc_stream_release(stream);
7049 requested_bpc -= 2; /* lower bpc to retry validation */
7052 } while (stream == NULL && requested_bpc >= 6);
7054 if (dc_result == DC_FAIL_ENC_VALIDATE && !aconnector->force_yuv420_output) {
7055 DRM_DEBUG_KMS("Retry forcing YCbCr420 encoding\n");
7057 aconnector->force_yuv420_output = true;
7058 stream = create_validate_stream_for_sink(aconnector, drm_mode,
7059 dm_state, old_stream);
7060 aconnector->force_yuv420_output = false;
7066 enum drm_mode_status amdgpu_dm_connector_mode_valid(struct drm_connector *connector,
7067 struct drm_display_mode *mode)
7069 int result = MODE_ERROR;
7070 struct dc_sink *dc_sink;
7071 /* TODO: Unhardcode stream count */
7072 struct dc_stream_state *stream;
7073 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
7075 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
7076 (mode->flags & DRM_MODE_FLAG_DBLSCAN))
7080 * Only run this the first time mode_valid is called to initilialize
7083 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED &&
7084 !aconnector->dc_em_sink)
7085 handle_edid_mgmt(aconnector);
7087 dc_sink = to_amdgpu_dm_connector(connector)->dc_sink;
7089 if (dc_sink == NULL && aconnector->base.force != DRM_FORCE_ON_DIGITAL &&
7090 aconnector->base.force != DRM_FORCE_ON) {
7091 DRM_ERROR("dc_sink is NULL!\n");
7095 stream = create_validate_stream_for_sink(aconnector, mode, NULL, NULL);
7097 dc_stream_release(stream);
7102 /* TODO: error handling*/
7106 static int fill_hdr_info_packet(const struct drm_connector_state *state,
7107 struct dc_info_packet *out)
7109 struct hdmi_drm_infoframe frame;
7110 unsigned char buf[30]; /* 26 + 4 */
7114 memset(out, 0, sizeof(*out));
7116 if (!state->hdr_output_metadata)
7119 ret = drm_hdmi_infoframe_set_hdr_metadata(&frame, state);
7123 len = hdmi_drm_infoframe_pack_only(&frame, buf, sizeof(buf));
7127 /* Static metadata is a fixed 26 bytes + 4 byte header. */
7131 /* Prepare the infopacket for DC. */
7132 switch (state->connector->connector_type) {
7133 case DRM_MODE_CONNECTOR_HDMIA:
7134 out->hb0 = 0x87; /* type */
7135 out->hb1 = 0x01; /* version */
7136 out->hb2 = 0x1A; /* length */
7137 out->sb[0] = buf[3]; /* checksum */
7141 case DRM_MODE_CONNECTOR_DisplayPort:
7142 case DRM_MODE_CONNECTOR_eDP:
7143 out->hb0 = 0x00; /* sdp id, zero */
7144 out->hb1 = 0x87; /* type */
7145 out->hb2 = 0x1D; /* payload len - 1 */
7146 out->hb3 = (0x13 << 2); /* sdp version */
7147 out->sb[0] = 0x01; /* version */
7148 out->sb[1] = 0x1A; /* length */
7156 memcpy(&out->sb[i], &buf[4], 26);
7159 print_hex_dump(KERN_DEBUG, "HDR SB:", DUMP_PREFIX_NONE, 16, 1, out->sb,
7160 sizeof(out->sb), false);
7166 amdgpu_dm_connector_atomic_check(struct drm_connector *conn,
7167 struct drm_atomic_state *state)
7169 struct drm_connector_state *new_con_state =
7170 drm_atomic_get_new_connector_state(state, conn);
7171 struct drm_connector_state *old_con_state =
7172 drm_atomic_get_old_connector_state(state, conn);
7173 struct drm_crtc *crtc = new_con_state->crtc;
7174 struct drm_crtc_state *new_crtc_state;
7177 trace_amdgpu_dm_connector_atomic_check(new_con_state);
7182 if (!drm_connector_atomic_hdr_metadata_equal(old_con_state, new_con_state)) {
7183 struct dc_info_packet hdr_infopacket;
7185 ret = fill_hdr_info_packet(new_con_state, &hdr_infopacket);
7189 new_crtc_state = drm_atomic_get_crtc_state(state, crtc);
7190 if (IS_ERR(new_crtc_state))
7191 return PTR_ERR(new_crtc_state);
7194 * DC considers the stream backends changed if the
7195 * static metadata changes. Forcing the modeset also
7196 * gives a simple way for userspace to switch from
7197 * 8bpc to 10bpc when setting the metadata to enter
7200 * Changing the static metadata after it's been
7201 * set is permissible, however. So only force a
7202 * modeset if we're entering or exiting HDR.
7204 new_crtc_state->mode_changed =
7205 !old_con_state->hdr_output_metadata ||
7206 !new_con_state->hdr_output_metadata;
7212 static const struct drm_connector_helper_funcs
7213 amdgpu_dm_connector_helper_funcs = {
7215 * If hotplugging a second bigger display in FB Con mode, bigger resolution
7216 * modes will be filtered by drm_mode_validate_size(), and those modes
7217 * are missing after user start lightdm. So we need to renew modes list.
7218 * in get_modes call back, not just return the modes count
7220 .get_modes = get_modes,
7221 .mode_valid = amdgpu_dm_connector_mode_valid,
7222 .atomic_check = amdgpu_dm_connector_atomic_check,
7225 static void dm_crtc_helper_disable(struct drm_crtc *crtc)
7229 static int count_crtc_active_planes(struct drm_crtc_state *new_crtc_state)
7231 struct drm_atomic_state *state = new_crtc_state->state;
7232 struct drm_plane *plane;
7235 drm_for_each_plane_mask(plane, state->dev, new_crtc_state->plane_mask) {
7236 struct drm_plane_state *new_plane_state;
7238 /* Cursor planes are "fake". */
7239 if (plane->type == DRM_PLANE_TYPE_CURSOR)
7242 new_plane_state = drm_atomic_get_new_plane_state(state, plane);
7244 if (!new_plane_state) {
7246 * The plane is enable on the CRTC and hasn't changed
7247 * state. This means that it previously passed
7248 * validation and is therefore enabled.
7254 /* We need a framebuffer to be considered enabled. */
7255 num_active += (new_plane_state->fb != NULL);
7261 static void dm_update_crtc_active_planes(struct drm_crtc *crtc,
7262 struct drm_crtc_state *new_crtc_state)
7264 struct dm_crtc_state *dm_new_crtc_state =
7265 to_dm_crtc_state(new_crtc_state);
7267 dm_new_crtc_state->active_planes = 0;
7269 if (!dm_new_crtc_state->stream)
7272 dm_new_crtc_state->active_planes =
7273 count_crtc_active_planes(new_crtc_state);
7276 static int dm_crtc_helper_atomic_check(struct drm_crtc *crtc,
7277 struct drm_atomic_state *state)
7279 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
7281 struct amdgpu_device *adev = drm_to_adev(crtc->dev);
7282 struct dc *dc = adev->dm.dc;
7283 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(crtc_state);
7286 trace_amdgpu_dm_crtc_atomic_check(crtc_state);
7288 dm_update_crtc_active_planes(crtc, crtc_state);
7290 if (WARN_ON(unlikely(!dm_crtc_state->stream &&
7291 modeset_required(crtc_state, NULL, dm_crtc_state->stream)))) {
7296 * We require the primary plane to be enabled whenever the CRTC is, otherwise
7297 * drm_mode_cursor_universal may end up trying to enable the cursor plane while all other
7298 * planes are disabled, which is not supported by the hardware. And there is legacy
7299 * userspace which stops using the HW cursor altogether in response to the resulting EINVAL.
7301 if (crtc_state->enable &&
7302 !(crtc_state->plane_mask & drm_plane_mask(crtc->primary))) {
7303 DRM_DEBUG_ATOMIC("Can't enable a CRTC without enabling the primary plane\n");
7307 /* In some use cases, like reset, no stream is attached */
7308 if (!dm_crtc_state->stream)
7311 if (dc_validate_stream(dc, dm_crtc_state->stream) == DC_OK)
7314 DRM_DEBUG_ATOMIC("Failed DC stream validation\n");
7318 static bool dm_crtc_helper_mode_fixup(struct drm_crtc *crtc,
7319 const struct drm_display_mode *mode,
7320 struct drm_display_mode *adjusted_mode)
7325 static const struct drm_crtc_helper_funcs amdgpu_dm_crtc_helper_funcs = {
7326 .disable = dm_crtc_helper_disable,
7327 .atomic_check = dm_crtc_helper_atomic_check,
7328 .mode_fixup = dm_crtc_helper_mode_fixup,
7329 .get_scanout_position = amdgpu_crtc_get_scanout_position,
7332 static void dm_encoder_helper_disable(struct drm_encoder *encoder)
7337 static int convert_dc_color_depth_into_bpc (enum dc_color_depth display_color_depth)
7339 switch (display_color_depth) {
7340 case COLOR_DEPTH_666:
7342 case COLOR_DEPTH_888:
7344 case COLOR_DEPTH_101010:
7346 case COLOR_DEPTH_121212:
7348 case COLOR_DEPTH_141414:
7350 case COLOR_DEPTH_161616:
7358 static int dm_encoder_helper_atomic_check(struct drm_encoder *encoder,
7359 struct drm_crtc_state *crtc_state,
7360 struct drm_connector_state *conn_state)
7362 struct drm_atomic_state *state = crtc_state->state;
7363 struct drm_connector *connector = conn_state->connector;
7364 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
7365 struct dm_connector_state *dm_new_connector_state = to_dm_connector_state(conn_state);
7366 const struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
7367 struct drm_dp_mst_topology_mgr *mst_mgr;
7368 struct drm_dp_mst_port *mst_port;
7369 enum dc_color_depth color_depth;
7371 bool is_y420 = false;
7373 if (!aconnector->port || !aconnector->dc_sink)
7376 mst_port = aconnector->port;
7377 mst_mgr = &aconnector->mst_port->mst_mgr;
7379 if (!crtc_state->connectors_changed && !crtc_state->mode_changed)
7382 if (!state->duplicated) {
7383 int max_bpc = conn_state->max_requested_bpc;
7384 is_y420 = drm_mode_is_420_also(&connector->display_info, adjusted_mode) &&
7385 aconnector->force_yuv420_output;
7386 color_depth = convert_color_depth_from_display_info(connector,
7389 bpp = convert_dc_color_depth_into_bpc(color_depth) * 3;
7390 clock = adjusted_mode->clock;
7391 dm_new_connector_state->pbn = drm_dp_calc_pbn_mode(clock, bpp, false);
7393 dm_new_connector_state->vcpi_slots = drm_dp_atomic_find_vcpi_slots(state,
7396 dm_new_connector_state->pbn,
7397 dm_mst_get_pbn_divider(aconnector->dc_link));
7398 if (dm_new_connector_state->vcpi_slots < 0) {
7399 DRM_DEBUG_ATOMIC("failed finding vcpi slots: %d\n", (int)dm_new_connector_state->vcpi_slots);
7400 return dm_new_connector_state->vcpi_slots;
7405 const struct drm_encoder_helper_funcs amdgpu_dm_encoder_helper_funcs = {
7406 .disable = dm_encoder_helper_disable,
7407 .atomic_check = dm_encoder_helper_atomic_check
7410 #if defined(CONFIG_DRM_AMD_DC_DCN)
7411 static int dm_update_mst_vcpi_slots_for_dsc(struct drm_atomic_state *state,
7412 struct dc_state *dc_state,
7413 struct dsc_mst_fairness_vars *vars)
7415 struct dc_stream_state *stream = NULL;
7416 struct drm_connector *connector;
7417 struct drm_connector_state *new_con_state;
7418 struct amdgpu_dm_connector *aconnector;
7419 struct dm_connector_state *dm_conn_state;
7421 int vcpi, pbn_div, pbn, slot_num = 0;
7423 for_each_new_connector_in_state(state, connector, new_con_state, i) {
7425 aconnector = to_amdgpu_dm_connector(connector);
7427 if (!aconnector->port)
7430 if (!new_con_state || !new_con_state->crtc)
7433 dm_conn_state = to_dm_connector_state(new_con_state);
7435 for (j = 0; j < dc_state->stream_count; j++) {
7436 stream = dc_state->streams[j];
7440 if ((struct amdgpu_dm_connector*)stream->dm_stream_context == aconnector)
7449 pbn_div = dm_mst_get_pbn_divider(stream->link);
7450 /* pbn is calculated by compute_mst_dsc_configs_for_state*/
7451 for (j = 0; j < dc_state->stream_count; j++) {
7452 if (vars[j].aconnector == aconnector) {
7458 if (j == dc_state->stream_count)
7461 slot_num = DIV_ROUND_UP(pbn, pbn_div);
7463 if (stream->timing.flags.DSC != 1) {
7464 dm_conn_state->pbn = pbn;
7465 dm_conn_state->vcpi_slots = slot_num;
7467 drm_dp_mst_atomic_enable_dsc(state,
7475 vcpi = drm_dp_mst_atomic_enable_dsc(state,
7482 dm_conn_state->pbn = pbn;
7483 dm_conn_state->vcpi_slots = vcpi;
7489 static void dm_drm_plane_reset(struct drm_plane *plane)
7491 struct dm_plane_state *amdgpu_state = NULL;
7494 plane->funcs->atomic_destroy_state(plane, plane->state);
7496 amdgpu_state = kzalloc(sizeof(*amdgpu_state), GFP_KERNEL);
7497 WARN_ON(amdgpu_state == NULL);
7500 __drm_atomic_helper_plane_reset(plane, &amdgpu_state->base);
7503 static struct drm_plane_state *
7504 dm_drm_plane_duplicate_state(struct drm_plane *plane)
7506 struct dm_plane_state *dm_plane_state, *old_dm_plane_state;
7508 old_dm_plane_state = to_dm_plane_state(plane->state);
7509 dm_plane_state = kzalloc(sizeof(*dm_plane_state), GFP_KERNEL);
7510 if (!dm_plane_state)
7513 __drm_atomic_helper_plane_duplicate_state(plane, &dm_plane_state->base);
7515 if (old_dm_plane_state->dc_state) {
7516 dm_plane_state->dc_state = old_dm_plane_state->dc_state;
7517 dc_plane_state_retain(dm_plane_state->dc_state);
7520 return &dm_plane_state->base;
7523 static void dm_drm_plane_destroy_state(struct drm_plane *plane,
7524 struct drm_plane_state *state)
7526 struct dm_plane_state *dm_plane_state = to_dm_plane_state(state);
7528 if (dm_plane_state->dc_state)
7529 dc_plane_state_release(dm_plane_state->dc_state);
7531 drm_atomic_helper_plane_destroy_state(plane, state);
7534 static const struct drm_plane_funcs dm_plane_funcs = {
7535 .update_plane = drm_atomic_helper_update_plane,
7536 .disable_plane = drm_atomic_helper_disable_plane,
7537 .destroy = drm_primary_helper_destroy,
7538 .reset = dm_drm_plane_reset,
7539 .atomic_duplicate_state = dm_drm_plane_duplicate_state,
7540 .atomic_destroy_state = dm_drm_plane_destroy_state,
7541 .format_mod_supported = dm_plane_format_mod_supported,
7544 static int dm_plane_helper_prepare_fb(struct drm_plane *plane,
7545 struct drm_plane_state *new_state)
7547 struct amdgpu_framebuffer *afb;
7548 struct drm_gem_object *obj;
7549 struct amdgpu_device *adev;
7550 struct amdgpu_bo *rbo;
7551 struct dm_plane_state *dm_plane_state_new, *dm_plane_state_old;
7555 if (!new_state->fb) {
7556 DRM_DEBUG_KMS("No FB bound\n");
7560 afb = to_amdgpu_framebuffer(new_state->fb);
7561 obj = new_state->fb->obj[0];
7562 rbo = gem_to_amdgpu_bo(obj);
7563 adev = amdgpu_ttm_adev(rbo->tbo.bdev);
7565 r = amdgpu_bo_reserve(rbo, true);
7567 dev_err(adev->dev, "fail to reserve bo (%d)\n", r);
7571 r = dma_resv_reserve_fences(rbo->tbo.base.resv, 1);
7573 dev_err(adev->dev, "reserving fence slot failed (%d)\n", r);
7577 if (plane->type != DRM_PLANE_TYPE_CURSOR)
7578 domain = amdgpu_display_supported_domains(adev, rbo->flags);
7580 domain = AMDGPU_GEM_DOMAIN_VRAM;
7582 r = amdgpu_bo_pin(rbo, domain);
7583 if (unlikely(r != 0)) {
7584 if (r != -ERESTARTSYS)
7585 DRM_ERROR("Failed to pin framebuffer with error %d\n", r);
7589 r = amdgpu_ttm_alloc_gart(&rbo->tbo);
7590 if (unlikely(r != 0)) {
7591 DRM_ERROR("%p bind failed\n", rbo);
7595 amdgpu_bo_unreserve(rbo);
7597 afb->address = amdgpu_bo_gpu_offset(rbo);
7602 * We don't do surface updates on planes that have been newly created,
7603 * but we also don't have the afb->address during atomic check.
7605 * Fill in buffer attributes depending on the address here, but only on
7606 * newly created planes since they're not being used by DC yet and this
7607 * won't modify global state.
7609 dm_plane_state_old = to_dm_plane_state(plane->state);
7610 dm_plane_state_new = to_dm_plane_state(new_state);
7612 if (dm_plane_state_new->dc_state &&
7613 dm_plane_state_old->dc_state != dm_plane_state_new->dc_state) {
7614 struct dc_plane_state *plane_state =
7615 dm_plane_state_new->dc_state;
7616 bool force_disable_dcc = !plane_state->dcc.enable;
7618 fill_plane_buffer_attributes(
7619 adev, afb, plane_state->format, plane_state->rotation,
7621 &plane_state->tiling_info, &plane_state->plane_size,
7622 &plane_state->dcc, &plane_state->address,
7623 afb->tmz_surface, force_disable_dcc);
7629 amdgpu_bo_unpin(rbo);
7632 amdgpu_bo_unreserve(rbo);
7636 static void dm_plane_helper_cleanup_fb(struct drm_plane *plane,
7637 struct drm_plane_state *old_state)
7639 struct amdgpu_bo *rbo;
7645 rbo = gem_to_amdgpu_bo(old_state->fb->obj[0]);
7646 r = amdgpu_bo_reserve(rbo, false);
7648 DRM_ERROR("failed to reserve rbo before unpin\n");
7652 amdgpu_bo_unpin(rbo);
7653 amdgpu_bo_unreserve(rbo);
7654 amdgpu_bo_unref(&rbo);
7657 static int dm_plane_helper_check_state(struct drm_plane_state *state,
7658 struct drm_crtc_state *new_crtc_state)
7660 struct drm_framebuffer *fb = state->fb;
7661 int min_downscale, max_upscale;
7663 int max_scale = INT_MAX;
7665 /* Plane enabled? Validate viewport and get scaling factors from plane caps. */
7666 if (fb && state->crtc) {
7667 /* Validate viewport to cover the case when only the position changes */
7668 if (state->plane->type != DRM_PLANE_TYPE_CURSOR) {
7669 int viewport_width = state->crtc_w;
7670 int viewport_height = state->crtc_h;
7672 if (state->crtc_x < 0)
7673 viewport_width += state->crtc_x;
7674 else if (state->crtc_x + state->crtc_w > new_crtc_state->mode.crtc_hdisplay)
7675 viewport_width = new_crtc_state->mode.crtc_hdisplay - state->crtc_x;
7677 if (state->crtc_y < 0)
7678 viewport_height += state->crtc_y;
7679 else if (state->crtc_y + state->crtc_h > new_crtc_state->mode.crtc_vdisplay)
7680 viewport_height = new_crtc_state->mode.crtc_vdisplay - state->crtc_y;
7682 if (viewport_width < 0 || viewport_height < 0) {
7683 DRM_DEBUG_ATOMIC("Plane completely outside of screen\n");
7685 } else if (viewport_width < MIN_VIEWPORT_SIZE*2) { /* x2 for width is because of pipe-split. */
7686 DRM_DEBUG_ATOMIC("Viewport width %d smaller than %d\n", viewport_width, MIN_VIEWPORT_SIZE*2);
7688 } else if (viewport_height < MIN_VIEWPORT_SIZE) {
7689 DRM_DEBUG_ATOMIC("Viewport height %d smaller than %d\n", viewport_height, MIN_VIEWPORT_SIZE);
7695 /* Get min/max allowed scaling factors from plane caps. */
7696 get_min_max_dc_plane_scaling(state->crtc->dev, fb,
7697 &min_downscale, &max_upscale);
7699 * Convert to drm convention: 16.16 fixed point, instead of dc's
7700 * 1.0 == 1000. Also drm scaling is src/dst instead of dc's
7701 * dst/src, so min_scale = 1.0 / max_upscale, etc.
7703 min_scale = (1000 << 16) / max_upscale;
7704 max_scale = (1000 << 16) / min_downscale;
7707 return drm_atomic_helper_check_plane_state(
7708 state, new_crtc_state, min_scale, max_scale, true, true);
7711 static int dm_plane_atomic_check(struct drm_plane *plane,
7712 struct drm_atomic_state *state)
7714 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
7716 struct amdgpu_device *adev = drm_to_adev(plane->dev);
7717 struct dc *dc = adev->dm.dc;
7718 struct dm_plane_state *dm_plane_state;
7719 struct dc_scaling_info scaling_info;
7720 struct drm_crtc_state *new_crtc_state;
7723 trace_amdgpu_dm_plane_atomic_check(new_plane_state);
7725 dm_plane_state = to_dm_plane_state(new_plane_state);
7727 if (!dm_plane_state->dc_state)
7731 drm_atomic_get_new_crtc_state(state,
7732 new_plane_state->crtc);
7733 if (!new_crtc_state)
7736 ret = dm_plane_helper_check_state(new_plane_state, new_crtc_state);
7740 ret = fill_dc_scaling_info(adev, new_plane_state, &scaling_info);
7744 if (dc_validate_plane(dc, dm_plane_state->dc_state) == DC_OK)
7750 static int dm_plane_atomic_async_check(struct drm_plane *plane,
7751 struct drm_atomic_state *state)
7753 /* Only support async updates on cursor planes. */
7754 if (plane->type != DRM_PLANE_TYPE_CURSOR)
7760 static void dm_plane_atomic_async_update(struct drm_plane *plane,
7761 struct drm_atomic_state *state)
7763 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
7765 struct drm_plane_state *old_state =
7766 drm_atomic_get_old_plane_state(state, plane);
7768 trace_amdgpu_dm_atomic_update_cursor(new_state);
7770 swap(plane->state->fb, new_state->fb);
7772 plane->state->src_x = new_state->src_x;
7773 plane->state->src_y = new_state->src_y;
7774 plane->state->src_w = new_state->src_w;
7775 plane->state->src_h = new_state->src_h;
7776 plane->state->crtc_x = new_state->crtc_x;
7777 plane->state->crtc_y = new_state->crtc_y;
7778 plane->state->crtc_w = new_state->crtc_w;
7779 plane->state->crtc_h = new_state->crtc_h;
7781 handle_cursor_update(plane, old_state);
7784 static const struct drm_plane_helper_funcs dm_plane_helper_funcs = {
7785 .prepare_fb = dm_plane_helper_prepare_fb,
7786 .cleanup_fb = dm_plane_helper_cleanup_fb,
7787 .atomic_check = dm_plane_atomic_check,
7788 .atomic_async_check = dm_plane_atomic_async_check,
7789 .atomic_async_update = dm_plane_atomic_async_update
7793 * TODO: these are currently initialized to rgb formats only.
7794 * For future use cases we should either initialize them dynamically based on
7795 * plane capabilities, or initialize this array to all formats, so internal drm
7796 * check will succeed, and let DC implement proper check
7798 static const uint32_t rgb_formats[] = {
7799 DRM_FORMAT_XRGB8888,
7800 DRM_FORMAT_ARGB8888,
7801 DRM_FORMAT_RGBA8888,
7802 DRM_FORMAT_XRGB2101010,
7803 DRM_FORMAT_XBGR2101010,
7804 DRM_FORMAT_ARGB2101010,
7805 DRM_FORMAT_ABGR2101010,
7806 DRM_FORMAT_XRGB16161616,
7807 DRM_FORMAT_XBGR16161616,
7808 DRM_FORMAT_ARGB16161616,
7809 DRM_FORMAT_ABGR16161616,
7810 DRM_FORMAT_XBGR8888,
7811 DRM_FORMAT_ABGR8888,
7815 static const uint32_t overlay_formats[] = {
7816 DRM_FORMAT_XRGB8888,
7817 DRM_FORMAT_ARGB8888,
7818 DRM_FORMAT_RGBA8888,
7819 DRM_FORMAT_XBGR8888,
7820 DRM_FORMAT_ABGR8888,
7824 static const u32 cursor_formats[] = {
7828 static int get_plane_formats(const struct drm_plane *plane,
7829 const struct dc_plane_cap *plane_cap,
7830 uint32_t *formats, int max_formats)
7832 int i, num_formats = 0;
7835 * TODO: Query support for each group of formats directly from
7836 * DC plane caps. This will require adding more formats to the
7840 switch (plane->type) {
7841 case DRM_PLANE_TYPE_PRIMARY:
7842 for (i = 0; i < ARRAY_SIZE(rgb_formats); ++i) {
7843 if (num_formats >= max_formats)
7846 formats[num_formats++] = rgb_formats[i];
7849 if (plane_cap && plane_cap->pixel_format_support.nv12)
7850 formats[num_formats++] = DRM_FORMAT_NV12;
7851 if (plane_cap && plane_cap->pixel_format_support.p010)
7852 formats[num_formats++] = DRM_FORMAT_P010;
7853 if (plane_cap && plane_cap->pixel_format_support.fp16) {
7854 formats[num_formats++] = DRM_FORMAT_XRGB16161616F;
7855 formats[num_formats++] = DRM_FORMAT_ARGB16161616F;
7856 formats[num_formats++] = DRM_FORMAT_XBGR16161616F;
7857 formats[num_formats++] = DRM_FORMAT_ABGR16161616F;
7861 case DRM_PLANE_TYPE_OVERLAY:
7862 for (i = 0; i < ARRAY_SIZE(overlay_formats); ++i) {
7863 if (num_formats >= max_formats)
7866 formats[num_formats++] = overlay_formats[i];
7870 case DRM_PLANE_TYPE_CURSOR:
7871 for (i = 0; i < ARRAY_SIZE(cursor_formats); ++i) {
7872 if (num_formats >= max_formats)
7875 formats[num_formats++] = cursor_formats[i];
7883 static int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
7884 struct drm_plane *plane,
7885 unsigned long possible_crtcs,
7886 const struct dc_plane_cap *plane_cap)
7888 uint32_t formats[32];
7891 unsigned int supported_rotations;
7892 uint64_t *modifiers = NULL;
7894 num_formats = get_plane_formats(plane, plane_cap, formats,
7895 ARRAY_SIZE(formats));
7897 res = get_plane_modifiers(dm->adev, plane->type, &modifiers);
7901 if (modifiers == NULL)
7902 adev_to_drm(dm->adev)->mode_config.fb_modifiers_not_supported = true;
7904 res = drm_universal_plane_init(adev_to_drm(dm->adev), plane, possible_crtcs,
7905 &dm_plane_funcs, formats, num_formats,
7906 modifiers, plane->type, NULL);
7911 if (plane->type == DRM_PLANE_TYPE_OVERLAY &&
7912 plane_cap && plane_cap->per_pixel_alpha) {
7913 unsigned int blend_caps = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
7914 BIT(DRM_MODE_BLEND_PREMULTI);
7916 drm_plane_create_alpha_property(plane);
7917 drm_plane_create_blend_mode_property(plane, blend_caps);
7920 if (plane->type == DRM_PLANE_TYPE_PRIMARY &&
7922 (plane_cap->pixel_format_support.nv12 ||
7923 plane_cap->pixel_format_support.p010)) {
7924 /* This only affects YUV formats. */
7925 drm_plane_create_color_properties(
7927 BIT(DRM_COLOR_YCBCR_BT601) |
7928 BIT(DRM_COLOR_YCBCR_BT709) |
7929 BIT(DRM_COLOR_YCBCR_BT2020),
7930 BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) |
7931 BIT(DRM_COLOR_YCBCR_FULL_RANGE),
7932 DRM_COLOR_YCBCR_BT709, DRM_COLOR_YCBCR_LIMITED_RANGE);
7935 supported_rotations =
7936 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
7937 DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
7939 if (dm->adev->asic_type >= CHIP_BONAIRE &&
7940 plane->type != DRM_PLANE_TYPE_CURSOR)
7941 drm_plane_create_rotation_property(plane, DRM_MODE_ROTATE_0,
7942 supported_rotations);
7944 drm_plane_helper_add(plane, &dm_plane_helper_funcs);
7946 /* Create (reset) the plane state */
7947 if (plane->funcs->reset)
7948 plane->funcs->reset(plane);
7953 static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
7954 struct drm_plane *plane,
7955 uint32_t crtc_index)
7957 struct amdgpu_crtc *acrtc = NULL;
7958 struct drm_plane *cursor_plane;
7962 cursor_plane = kzalloc(sizeof(*cursor_plane), GFP_KERNEL);
7966 cursor_plane->type = DRM_PLANE_TYPE_CURSOR;
7967 res = amdgpu_dm_plane_init(dm, cursor_plane, 0, NULL);
7969 acrtc = kzalloc(sizeof(struct amdgpu_crtc), GFP_KERNEL);
7973 res = drm_crtc_init_with_planes(
7978 &amdgpu_dm_crtc_funcs, NULL);
7983 drm_crtc_helper_add(&acrtc->base, &amdgpu_dm_crtc_helper_funcs);
7985 /* Create (reset) the plane state */
7986 if (acrtc->base.funcs->reset)
7987 acrtc->base.funcs->reset(&acrtc->base);
7989 acrtc->max_cursor_width = dm->adev->dm.dc->caps.max_cursor_size;
7990 acrtc->max_cursor_height = dm->adev->dm.dc->caps.max_cursor_size;
7992 acrtc->crtc_id = crtc_index;
7993 acrtc->base.enabled = false;
7994 acrtc->otg_inst = -1;
7996 dm->adev->mode_info.crtcs[crtc_index] = acrtc;
7997 drm_crtc_enable_color_mgmt(&acrtc->base, MAX_COLOR_LUT_ENTRIES,
7998 true, MAX_COLOR_LUT_ENTRIES);
7999 drm_mode_crtc_set_gamma_size(&acrtc->base, MAX_COLOR_LEGACY_LUT_ENTRIES);
8005 kfree(cursor_plane);
8010 static int to_drm_connector_type(enum signal_type st)
8013 case SIGNAL_TYPE_HDMI_TYPE_A:
8014 return DRM_MODE_CONNECTOR_HDMIA;
8015 case SIGNAL_TYPE_EDP:
8016 return DRM_MODE_CONNECTOR_eDP;
8017 case SIGNAL_TYPE_LVDS:
8018 return DRM_MODE_CONNECTOR_LVDS;
8019 case SIGNAL_TYPE_RGB:
8020 return DRM_MODE_CONNECTOR_VGA;
8021 case SIGNAL_TYPE_DISPLAY_PORT:
8022 case SIGNAL_TYPE_DISPLAY_PORT_MST:
8023 return DRM_MODE_CONNECTOR_DisplayPort;
8024 case SIGNAL_TYPE_DVI_DUAL_LINK:
8025 case SIGNAL_TYPE_DVI_SINGLE_LINK:
8026 return DRM_MODE_CONNECTOR_DVID;
8027 case SIGNAL_TYPE_VIRTUAL:
8028 return DRM_MODE_CONNECTOR_VIRTUAL;
8031 return DRM_MODE_CONNECTOR_Unknown;
8035 static struct drm_encoder *amdgpu_dm_connector_to_encoder(struct drm_connector *connector)
8037 struct drm_encoder *encoder;
8039 /* There is only one encoder per connector */
8040 drm_connector_for_each_possible_encoder(connector, encoder)
8046 static void amdgpu_dm_get_native_mode(struct drm_connector *connector)
8048 struct drm_encoder *encoder;
8049 struct amdgpu_encoder *amdgpu_encoder;
8051 encoder = amdgpu_dm_connector_to_encoder(connector);
8053 if (encoder == NULL)
8056 amdgpu_encoder = to_amdgpu_encoder(encoder);
8058 amdgpu_encoder->native_mode.clock = 0;
8060 if (!list_empty(&connector->probed_modes)) {
8061 struct drm_display_mode *preferred_mode = NULL;
8063 list_for_each_entry(preferred_mode,
8064 &connector->probed_modes,
8066 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED)
8067 amdgpu_encoder->native_mode = *preferred_mode;
8075 static struct drm_display_mode *
8076 amdgpu_dm_create_common_mode(struct drm_encoder *encoder,
8078 int hdisplay, int vdisplay)
8080 struct drm_device *dev = encoder->dev;
8081 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
8082 struct drm_display_mode *mode = NULL;
8083 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
8085 mode = drm_mode_duplicate(dev, native_mode);
8090 mode->hdisplay = hdisplay;
8091 mode->vdisplay = vdisplay;
8092 mode->type &= ~DRM_MODE_TYPE_PREFERRED;
8093 strscpy(mode->name, name, DRM_DISPLAY_MODE_LEN);
8099 static void amdgpu_dm_connector_add_common_modes(struct drm_encoder *encoder,
8100 struct drm_connector *connector)
8102 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
8103 struct drm_display_mode *mode = NULL;
8104 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
8105 struct amdgpu_dm_connector *amdgpu_dm_connector =
8106 to_amdgpu_dm_connector(connector);
8110 char name[DRM_DISPLAY_MODE_LEN];
8113 } common_modes[] = {
8114 { "640x480", 640, 480},
8115 { "800x600", 800, 600},
8116 { "1024x768", 1024, 768},
8117 { "1280x720", 1280, 720},
8118 { "1280x800", 1280, 800},
8119 {"1280x1024", 1280, 1024},
8120 { "1440x900", 1440, 900},
8121 {"1680x1050", 1680, 1050},
8122 {"1600x1200", 1600, 1200},
8123 {"1920x1080", 1920, 1080},
8124 {"1920x1200", 1920, 1200}
8127 n = ARRAY_SIZE(common_modes);
8129 for (i = 0; i < n; i++) {
8130 struct drm_display_mode *curmode = NULL;
8131 bool mode_existed = false;
8133 if (common_modes[i].w > native_mode->hdisplay ||
8134 common_modes[i].h > native_mode->vdisplay ||
8135 (common_modes[i].w == native_mode->hdisplay &&
8136 common_modes[i].h == native_mode->vdisplay))
8139 list_for_each_entry(curmode, &connector->probed_modes, head) {
8140 if (common_modes[i].w == curmode->hdisplay &&
8141 common_modes[i].h == curmode->vdisplay) {
8142 mode_existed = true;
8150 mode = amdgpu_dm_create_common_mode(encoder,
8151 common_modes[i].name, common_modes[i].w,
8156 drm_mode_probed_add(connector, mode);
8157 amdgpu_dm_connector->num_modes++;
8161 static void amdgpu_set_panel_orientation(struct drm_connector *connector)
8163 struct drm_encoder *encoder;
8164 struct amdgpu_encoder *amdgpu_encoder;
8165 const struct drm_display_mode *native_mode;
8167 if (connector->connector_type != DRM_MODE_CONNECTOR_eDP &&
8168 connector->connector_type != DRM_MODE_CONNECTOR_LVDS)
8171 encoder = amdgpu_dm_connector_to_encoder(connector);
8175 amdgpu_encoder = to_amdgpu_encoder(encoder);
8177 native_mode = &amdgpu_encoder->native_mode;
8178 if (native_mode->hdisplay == 0 || native_mode->vdisplay == 0)
8181 drm_connector_set_panel_orientation_with_quirk(connector,
8182 DRM_MODE_PANEL_ORIENTATION_UNKNOWN,
8183 native_mode->hdisplay,
8184 native_mode->vdisplay);
8187 static void amdgpu_dm_connector_ddc_get_modes(struct drm_connector *connector,
8190 struct amdgpu_dm_connector *amdgpu_dm_connector =
8191 to_amdgpu_dm_connector(connector);
8194 /* empty probed_modes */
8195 INIT_LIST_HEAD(&connector->probed_modes);
8196 amdgpu_dm_connector->num_modes =
8197 drm_add_edid_modes(connector, edid);
8199 /* sorting the probed modes before calling function
8200 * amdgpu_dm_get_native_mode() since EDID can have
8201 * more than one preferred mode. The modes that are
8202 * later in the probed mode list could be of higher
8203 * and preferred resolution. For example, 3840x2160
8204 * resolution in base EDID preferred timing and 4096x2160
8205 * preferred resolution in DID extension block later.
8207 drm_mode_sort(&connector->probed_modes);
8208 amdgpu_dm_get_native_mode(connector);
8210 /* Freesync capabilities are reset by calling
8211 * drm_add_edid_modes() and need to be
8214 amdgpu_dm_update_freesync_caps(connector, edid);
8216 amdgpu_set_panel_orientation(connector);
8218 amdgpu_dm_connector->num_modes = 0;
8222 static bool is_duplicate_mode(struct amdgpu_dm_connector *aconnector,
8223 struct drm_display_mode *mode)
8225 struct drm_display_mode *m;
8227 list_for_each_entry (m, &aconnector->base.probed_modes, head) {
8228 if (drm_mode_equal(m, mode))
8235 static uint add_fs_modes(struct amdgpu_dm_connector *aconnector)
8237 const struct drm_display_mode *m;
8238 struct drm_display_mode *new_mode;
8240 uint32_t new_modes_count = 0;
8242 /* Standard FPS values
8251 * 60 - Commonly used
8252 * 48,72,96,120 - Multiples of 24
8254 static const uint32_t common_rates[] = {
8255 23976, 24000, 25000, 29970, 30000,
8256 48000, 50000, 60000, 72000, 96000, 120000
8260 * Find mode with highest refresh rate with the same resolution
8261 * as the preferred mode. Some monitors report a preferred mode
8262 * with lower resolution than the highest refresh rate supported.
8265 m = get_highest_refresh_rate_mode(aconnector, true);
8269 for (i = 0; i < ARRAY_SIZE(common_rates); i++) {
8270 uint64_t target_vtotal, target_vtotal_diff;
8273 if (drm_mode_vrefresh(m) * 1000 < common_rates[i])
8276 if (common_rates[i] < aconnector->min_vfreq * 1000 ||
8277 common_rates[i] > aconnector->max_vfreq * 1000)
8280 num = (unsigned long long)m->clock * 1000 * 1000;
8281 den = common_rates[i] * (unsigned long long)m->htotal;
8282 target_vtotal = div_u64(num, den);
8283 target_vtotal_diff = target_vtotal - m->vtotal;
8285 /* Check for illegal modes */
8286 if (m->vsync_start + target_vtotal_diff < m->vdisplay ||
8287 m->vsync_end + target_vtotal_diff < m->vsync_start ||
8288 m->vtotal + target_vtotal_diff < m->vsync_end)
8291 new_mode = drm_mode_duplicate(aconnector->base.dev, m);
8295 new_mode->vtotal += (u16)target_vtotal_diff;
8296 new_mode->vsync_start += (u16)target_vtotal_diff;
8297 new_mode->vsync_end += (u16)target_vtotal_diff;
8298 new_mode->type &= ~DRM_MODE_TYPE_PREFERRED;
8299 new_mode->type |= DRM_MODE_TYPE_DRIVER;
8301 if (!is_duplicate_mode(aconnector, new_mode)) {
8302 drm_mode_probed_add(&aconnector->base, new_mode);
8303 new_modes_count += 1;
8305 drm_mode_destroy(aconnector->base.dev, new_mode);
8308 return new_modes_count;
8311 static void amdgpu_dm_connector_add_freesync_modes(struct drm_connector *connector,
8314 struct amdgpu_dm_connector *amdgpu_dm_connector =
8315 to_amdgpu_dm_connector(connector);
8320 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10)
8321 amdgpu_dm_connector->num_modes +=
8322 add_fs_modes(amdgpu_dm_connector);
8325 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector)
8327 struct amdgpu_dm_connector *amdgpu_dm_connector =
8328 to_amdgpu_dm_connector(connector);
8329 struct drm_encoder *encoder;
8330 struct edid *edid = amdgpu_dm_connector->edid;
8332 encoder = amdgpu_dm_connector_to_encoder(connector);
8334 if (!drm_edid_is_valid(edid)) {
8335 amdgpu_dm_connector->num_modes =
8336 drm_add_modes_noedid(connector, 640, 480);
8338 amdgpu_dm_connector_ddc_get_modes(connector, edid);
8339 amdgpu_dm_connector_add_common_modes(encoder, connector);
8340 amdgpu_dm_connector_add_freesync_modes(connector, edid);
8342 amdgpu_dm_fbc_init(connector);
8344 return amdgpu_dm_connector->num_modes;
8347 void amdgpu_dm_connector_init_helper(struct amdgpu_display_manager *dm,
8348 struct amdgpu_dm_connector *aconnector,
8350 struct dc_link *link,
8353 struct amdgpu_device *adev = drm_to_adev(dm->ddev);
8356 * Some of the properties below require access to state, like bpc.
8357 * Allocate some default initial connector state with our reset helper.
8359 if (aconnector->base.funcs->reset)
8360 aconnector->base.funcs->reset(&aconnector->base);
8362 aconnector->connector_id = link_index;
8363 aconnector->dc_link = link;
8364 aconnector->base.interlace_allowed = false;
8365 aconnector->base.doublescan_allowed = false;
8366 aconnector->base.stereo_allowed = false;
8367 aconnector->base.dpms = DRM_MODE_DPMS_OFF;
8368 aconnector->hpd.hpd = AMDGPU_HPD_NONE; /* not used */
8369 aconnector->audio_inst = -1;
8370 mutex_init(&aconnector->hpd_lock);
8373 * configure support HPD hot plug connector_>polled default value is 0
8374 * which means HPD hot plug not supported
8376 switch (connector_type) {
8377 case DRM_MODE_CONNECTOR_HDMIA:
8378 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
8379 aconnector->base.ycbcr_420_allowed =
8380 link->link_enc->features.hdmi_ycbcr420_supported ? true : false;
8382 case DRM_MODE_CONNECTOR_DisplayPort:
8383 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
8384 link->link_enc = link_enc_cfg_get_link_enc(link);
8385 ASSERT(link->link_enc);
8387 aconnector->base.ycbcr_420_allowed =
8388 link->link_enc->features.dp_ycbcr420_supported ? true : false;
8390 case DRM_MODE_CONNECTOR_DVID:
8391 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
8397 drm_object_attach_property(&aconnector->base.base,
8398 dm->ddev->mode_config.scaling_mode_property,
8399 DRM_MODE_SCALE_NONE);
8401 drm_object_attach_property(&aconnector->base.base,
8402 adev->mode_info.underscan_property,
8404 drm_object_attach_property(&aconnector->base.base,
8405 adev->mode_info.underscan_hborder_property,
8407 drm_object_attach_property(&aconnector->base.base,
8408 adev->mode_info.underscan_vborder_property,
8411 if (!aconnector->mst_port)
8412 drm_connector_attach_max_bpc_property(&aconnector->base, 8, 16);
8414 /* This defaults to the max in the range, but we want 8bpc for non-edp. */
8415 aconnector->base.state->max_bpc = (connector_type == DRM_MODE_CONNECTOR_eDP) ? 16 : 8;
8416 aconnector->base.state->max_requested_bpc = aconnector->base.state->max_bpc;
8418 if (connector_type == DRM_MODE_CONNECTOR_eDP &&
8419 (dc_is_dmcu_initialized(adev->dm.dc) || adev->dm.dc->ctx->dmub_srv)) {
8420 drm_object_attach_property(&aconnector->base.base,
8421 adev->mode_info.abm_level_property, 0);
8424 if (connector_type == DRM_MODE_CONNECTOR_HDMIA ||
8425 connector_type == DRM_MODE_CONNECTOR_DisplayPort ||
8426 connector_type == DRM_MODE_CONNECTOR_eDP) {
8427 drm_connector_attach_hdr_output_metadata_property(&aconnector->base);
8429 if (!aconnector->mst_port)
8430 drm_connector_attach_vrr_capable_property(&aconnector->base);
8432 #ifdef CONFIG_DRM_AMD_DC_HDCP
8433 if (adev->dm.hdcp_workqueue)
8434 drm_connector_attach_content_protection_property(&aconnector->base, true);
8439 static int amdgpu_dm_i2c_xfer(struct i2c_adapter *i2c_adap,
8440 struct i2c_msg *msgs, int num)
8442 struct amdgpu_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
8443 struct ddc_service *ddc_service = i2c->ddc_service;
8444 struct i2c_command cmd;
8448 cmd.payloads = kcalloc(num, sizeof(struct i2c_payload), GFP_KERNEL);
8453 cmd.number_of_payloads = num;
8454 cmd.engine = I2C_COMMAND_ENGINE_DEFAULT;
8457 for (i = 0; i < num; i++) {
8458 cmd.payloads[i].write = !(msgs[i].flags & I2C_M_RD);
8459 cmd.payloads[i].address = msgs[i].addr;
8460 cmd.payloads[i].length = msgs[i].len;
8461 cmd.payloads[i].data = msgs[i].buf;
8465 ddc_service->ctx->dc,
8466 ddc_service->ddc_pin->hw_info.ddc_channel,
8470 kfree(cmd.payloads);
8474 static u32 amdgpu_dm_i2c_func(struct i2c_adapter *adap)
8476 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
8479 static const struct i2c_algorithm amdgpu_dm_i2c_algo = {
8480 .master_xfer = amdgpu_dm_i2c_xfer,
8481 .functionality = amdgpu_dm_i2c_func,
8484 static struct amdgpu_i2c_adapter *
8485 create_i2c(struct ddc_service *ddc_service,
8489 struct amdgpu_device *adev = ddc_service->ctx->driver_context;
8490 struct amdgpu_i2c_adapter *i2c;
8492 i2c = kzalloc(sizeof(struct amdgpu_i2c_adapter), GFP_KERNEL);
8495 i2c->base.owner = THIS_MODULE;
8496 i2c->base.class = I2C_CLASS_DDC;
8497 i2c->base.dev.parent = &adev->pdev->dev;
8498 i2c->base.algo = &amdgpu_dm_i2c_algo;
8499 snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c hw bus %d", link_index);
8500 i2c_set_adapdata(&i2c->base, i2c);
8501 i2c->ddc_service = ddc_service;
8502 if (i2c->ddc_service->ddc_pin)
8503 i2c->ddc_service->ddc_pin->hw_info.ddc_channel = link_index;
8510 * Note: this function assumes that dc_link_detect() was called for the
8511 * dc_link which will be represented by this aconnector.
8513 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
8514 struct amdgpu_dm_connector *aconnector,
8515 uint32_t link_index,
8516 struct amdgpu_encoder *aencoder)
8520 struct dc *dc = dm->dc;
8521 struct dc_link *link = dc_get_link_at_index(dc, link_index);
8522 struct amdgpu_i2c_adapter *i2c;
8524 link->priv = aconnector;
8526 DRM_DEBUG_DRIVER("%s()\n", __func__);
8528 i2c = create_i2c(link->ddc, link->link_index, &res);
8530 DRM_ERROR("Failed to create i2c adapter data\n");
8534 aconnector->i2c = i2c;
8535 res = i2c_add_adapter(&i2c->base);
8538 DRM_ERROR("Failed to register hw i2c %d\n", link->link_index);
8542 connector_type = to_drm_connector_type(link->connector_signal);
8544 res = drm_connector_init_with_ddc(
8547 &amdgpu_dm_connector_funcs,
8552 DRM_ERROR("connector_init failed\n");
8553 aconnector->connector_id = -1;
8557 drm_connector_helper_add(
8559 &amdgpu_dm_connector_helper_funcs);
8561 amdgpu_dm_connector_init_helper(
8568 drm_connector_attach_encoder(
8569 &aconnector->base, &aencoder->base);
8571 if (connector_type == DRM_MODE_CONNECTOR_DisplayPort
8572 || connector_type == DRM_MODE_CONNECTOR_eDP)
8573 amdgpu_dm_initialize_dp_connector(dm, aconnector, link->link_index);
8578 aconnector->i2c = NULL;
8583 int amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device *adev)
8585 switch (adev->mode_info.num_crtc) {
8602 static int amdgpu_dm_encoder_init(struct drm_device *dev,
8603 struct amdgpu_encoder *aencoder,
8604 uint32_t link_index)
8606 struct amdgpu_device *adev = drm_to_adev(dev);
8608 int res = drm_encoder_init(dev,
8610 &amdgpu_dm_encoder_funcs,
8611 DRM_MODE_ENCODER_TMDS,
8614 aencoder->base.possible_crtcs = amdgpu_dm_get_encoder_crtc_mask(adev);
8617 aencoder->encoder_id = link_index;
8619 aencoder->encoder_id = -1;
8621 drm_encoder_helper_add(&aencoder->base, &amdgpu_dm_encoder_helper_funcs);
8626 static void manage_dm_interrupts(struct amdgpu_device *adev,
8627 struct amdgpu_crtc *acrtc,
8631 * We have no guarantee that the frontend index maps to the same
8632 * backend index - some even map to more than one.
8634 * TODO: Use a different interrupt or check DC itself for the mapping.
8637 amdgpu_display_crtc_idx_to_irq_type(
8642 drm_crtc_vblank_on(&acrtc->base);
8645 &adev->pageflip_irq,
8647 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
8654 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
8662 &adev->pageflip_irq,
8664 drm_crtc_vblank_off(&acrtc->base);
8668 static void dm_update_pflip_irq_state(struct amdgpu_device *adev,
8669 struct amdgpu_crtc *acrtc)
8672 amdgpu_display_crtc_idx_to_irq_type(adev, acrtc->crtc_id);
8675 * This reads the current state for the IRQ and force reapplies
8676 * the setting to hardware.
8678 amdgpu_irq_update(adev, &adev->pageflip_irq, irq_type);
8682 is_scaling_state_different(const struct dm_connector_state *dm_state,
8683 const struct dm_connector_state *old_dm_state)
8685 if (dm_state->scaling != old_dm_state->scaling)
8687 if (!dm_state->underscan_enable && old_dm_state->underscan_enable) {
8688 if (old_dm_state->underscan_hborder != 0 && old_dm_state->underscan_vborder != 0)
8690 } else if (dm_state->underscan_enable && !old_dm_state->underscan_enable) {
8691 if (dm_state->underscan_hborder != 0 && dm_state->underscan_vborder != 0)
8693 } else if (dm_state->underscan_hborder != old_dm_state->underscan_hborder ||
8694 dm_state->underscan_vborder != old_dm_state->underscan_vborder)
8699 #ifdef CONFIG_DRM_AMD_DC_HDCP
8700 static bool is_content_protection_different(struct drm_connector_state *state,
8701 const struct drm_connector_state *old_state,
8702 const struct drm_connector *connector, struct hdcp_workqueue *hdcp_w)
8704 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
8705 struct dm_connector_state *dm_con_state = to_dm_connector_state(connector->state);
8707 /* Handle: Type0/1 change */
8708 if (old_state->hdcp_content_type != state->hdcp_content_type &&
8709 state->content_protection != DRM_MODE_CONTENT_PROTECTION_UNDESIRED) {
8710 state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
8714 /* CP is being re enabled, ignore this
8716 * Handles: ENABLED -> DESIRED
8718 if (old_state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED &&
8719 state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED) {
8720 state->content_protection = DRM_MODE_CONTENT_PROTECTION_ENABLED;
8724 /* S3 resume case, since old state will always be 0 (UNDESIRED) and the restored state will be ENABLED
8726 * Handles: UNDESIRED -> ENABLED
8728 if (old_state->content_protection == DRM_MODE_CONTENT_PROTECTION_UNDESIRED &&
8729 state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED)
8730 state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
8732 /* Stream removed and re-enabled
8734 * Can sometimes overlap with the HPD case,
8735 * thus set update_hdcp to false to avoid
8736 * setting HDCP multiple times.
8738 * Handles: DESIRED -> DESIRED (Special case)
8740 if (!(old_state->crtc && old_state->crtc->enabled) &&
8741 state->crtc && state->crtc->enabled &&
8742 connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED) {
8743 dm_con_state->update_hdcp = false;
8747 /* Hot-plug, headless s3, dpms
8749 * Only start HDCP if the display is connected/enabled.
8750 * update_hdcp flag will be set to false until the next
8753 * Handles: DESIRED -> DESIRED (Special case)
8755 if (dm_con_state->update_hdcp && state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED &&
8756 connector->dpms == DRM_MODE_DPMS_ON && aconnector->dc_sink != NULL) {
8757 dm_con_state->update_hdcp = false;
8762 * Handles: UNDESIRED -> UNDESIRED
8763 * DESIRED -> DESIRED
8764 * ENABLED -> ENABLED
8766 if (old_state->content_protection == state->content_protection)
8770 * Handles: UNDESIRED -> DESIRED
8771 * DESIRED -> UNDESIRED
8772 * ENABLED -> UNDESIRED
8774 if (state->content_protection != DRM_MODE_CONTENT_PROTECTION_ENABLED)
8778 * Handles: DESIRED -> ENABLED
8784 static void remove_stream(struct amdgpu_device *adev,
8785 struct amdgpu_crtc *acrtc,
8786 struct dc_stream_state *stream)
8788 /* this is the update mode case */
8790 acrtc->otg_inst = -1;
8791 acrtc->enabled = false;
8794 static int get_cursor_position(struct drm_plane *plane, struct drm_crtc *crtc,
8795 struct dc_cursor_position *position)
8797 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
8799 int xorigin = 0, yorigin = 0;
8801 if (!crtc || !plane->state->fb)
8804 if ((plane->state->crtc_w > amdgpu_crtc->max_cursor_width) ||
8805 (plane->state->crtc_h > amdgpu_crtc->max_cursor_height)) {
8806 DRM_ERROR("%s: bad cursor width or height %d x %d\n",
8808 plane->state->crtc_w,
8809 plane->state->crtc_h);
8813 x = plane->state->crtc_x;
8814 y = plane->state->crtc_y;
8816 if (x <= -amdgpu_crtc->max_cursor_width ||
8817 y <= -amdgpu_crtc->max_cursor_height)
8821 xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
8825 yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
8828 position->enable = true;
8829 position->translate_by_source = true;
8832 position->x_hotspot = xorigin;
8833 position->y_hotspot = yorigin;
8838 static void handle_cursor_update(struct drm_plane *plane,
8839 struct drm_plane_state *old_plane_state)
8841 struct amdgpu_device *adev = drm_to_adev(plane->dev);
8842 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(plane->state->fb);
8843 struct drm_crtc *crtc = afb ? plane->state->crtc : old_plane_state->crtc;
8844 struct dm_crtc_state *crtc_state = crtc ? to_dm_crtc_state(crtc->state) : NULL;
8845 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
8846 uint64_t address = afb ? afb->address : 0;
8847 struct dc_cursor_position position = {0};
8848 struct dc_cursor_attributes attributes;
8851 if (!plane->state->fb && !old_plane_state->fb)
8854 DC_LOG_CURSOR("%s: crtc_id=%d with size %d to %d\n",
8856 amdgpu_crtc->crtc_id,
8857 plane->state->crtc_w,
8858 plane->state->crtc_h);
8860 ret = get_cursor_position(plane, crtc, &position);
8864 if (!position.enable) {
8865 /* turn off cursor */
8866 if (crtc_state && crtc_state->stream) {
8867 mutex_lock(&adev->dm.dc_lock);
8868 dc_stream_set_cursor_position(crtc_state->stream,
8870 mutex_unlock(&adev->dm.dc_lock);
8875 amdgpu_crtc->cursor_width = plane->state->crtc_w;
8876 amdgpu_crtc->cursor_height = plane->state->crtc_h;
8878 memset(&attributes, 0, sizeof(attributes));
8879 attributes.address.high_part = upper_32_bits(address);
8880 attributes.address.low_part = lower_32_bits(address);
8881 attributes.width = plane->state->crtc_w;
8882 attributes.height = plane->state->crtc_h;
8883 attributes.color_format = CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA;
8884 attributes.rotation_angle = 0;
8885 attributes.attribute_flags.value = 0;
8887 attributes.pitch = afb->base.pitches[0] / afb->base.format->cpp[0];
8889 if (crtc_state->stream) {
8890 mutex_lock(&adev->dm.dc_lock);
8891 if (!dc_stream_set_cursor_attributes(crtc_state->stream,
8893 DRM_ERROR("DC failed to set cursor attributes\n");
8895 if (!dc_stream_set_cursor_position(crtc_state->stream,
8897 DRM_ERROR("DC failed to set cursor position\n");
8898 mutex_unlock(&adev->dm.dc_lock);
8902 static void prepare_flip_isr(struct amdgpu_crtc *acrtc)
8905 assert_spin_locked(&acrtc->base.dev->event_lock);
8906 WARN_ON(acrtc->event);
8908 acrtc->event = acrtc->base.state->event;
8910 /* Set the flip status */
8911 acrtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
8913 /* Mark this event as consumed */
8914 acrtc->base.state->event = NULL;
8916 DC_LOG_PFLIP("crtc:%d, pflip_stat:AMDGPU_FLIP_SUBMITTED\n",
8920 static void update_freesync_state_on_stream(
8921 struct amdgpu_display_manager *dm,
8922 struct dm_crtc_state *new_crtc_state,
8923 struct dc_stream_state *new_stream,
8924 struct dc_plane_state *surface,
8925 u32 flip_timestamp_in_us)
8927 struct mod_vrr_params vrr_params;
8928 struct dc_info_packet vrr_infopacket = {0};
8929 struct amdgpu_device *adev = dm->adev;
8930 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(new_crtc_state->base.crtc);
8931 unsigned long flags;
8932 bool pack_sdp_v1_3 = false;
8938 * TODO: Determine why min/max totals and vrefresh can be 0 here.
8939 * For now it's sufficient to just guard against these conditions.
8942 if (!new_stream->timing.h_total || !new_stream->timing.v_total)
8945 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
8946 vrr_params = acrtc->dm_irq_params.vrr_params;
8949 mod_freesync_handle_preflip(
8950 dm->freesync_module,
8953 flip_timestamp_in_us,
8956 if (adev->family < AMDGPU_FAMILY_AI &&
8957 amdgpu_dm_vrr_active(new_crtc_state)) {
8958 mod_freesync_handle_v_update(dm->freesync_module,
8959 new_stream, &vrr_params);
8961 /* Need to call this before the frame ends. */
8962 dc_stream_adjust_vmin_vmax(dm->dc,
8963 new_crtc_state->stream,
8964 &vrr_params.adjust);
8968 mod_freesync_build_vrr_infopacket(
8969 dm->freesync_module,
8973 TRANSFER_FUNC_UNKNOWN,
8977 new_crtc_state->freesync_timing_changed |=
8978 (memcmp(&acrtc->dm_irq_params.vrr_params.adjust,
8980 sizeof(vrr_params.adjust)) != 0);
8982 new_crtc_state->freesync_vrr_info_changed |=
8983 (memcmp(&new_crtc_state->vrr_infopacket,
8985 sizeof(vrr_infopacket)) != 0);
8987 acrtc->dm_irq_params.vrr_params = vrr_params;
8988 new_crtc_state->vrr_infopacket = vrr_infopacket;
8990 new_stream->adjust = acrtc->dm_irq_params.vrr_params.adjust;
8991 new_stream->vrr_infopacket = vrr_infopacket;
8993 if (new_crtc_state->freesync_vrr_info_changed)
8994 DRM_DEBUG_KMS("VRR packet update: crtc=%u enabled=%d state=%d",
8995 new_crtc_state->base.crtc->base.id,
8996 (int)new_crtc_state->base.vrr_enabled,
8997 (int)vrr_params.state);
8999 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
9002 static void update_stream_irq_parameters(
9003 struct amdgpu_display_manager *dm,
9004 struct dm_crtc_state *new_crtc_state)
9006 struct dc_stream_state *new_stream = new_crtc_state->stream;
9007 struct mod_vrr_params vrr_params;
9008 struct mod_freesync_config config = new_crtc_state->freesync_config;
9009 struct amdgpu_device *adev = dm->adev;
9010 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(new_crtc_state->base.crtc);
9011 unsigned long flags;
9017 * TODO: Determine why min/max totals and vrefresh can be 0 here.
9018 * For now it's sufficient to just guard against these conditions.
9020 if (!new_stream->timing.h_total || !new_stream->timing.v_total)
9023 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
9024 vrr_params = acrtc->dm_irq_params.vrr_params;
9026 if (new_crtc_state->vrr_supported &&
9027 config.min_refresh_in_uhz &&
9028 config.max_refresh_in_uhz) {
9030 * if freesync compatible mode was set, config.state will be set
9033 if (config.state == VRR_STATE_ACTIVE_FIXED && config.fixed_refresh_in_uhz &&
9034 (!drm_atomic_crtc_needs_modeset(&new_crtc_state->base) ||
9035 new_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED)) {
9036 vrr_params.max_refresh_in_uhz = config.max_refresh_in_uhz;
9037 vrr_params.min_refresh_in_uhz = config.min_refresh_in_uhz;
9038 vrr_params.fixed_refresh_in_uhz = config.fixed_refresh_in_uhz;
9039 vrr_params.state = VRR_STATE_ACTIVE_FIXED;
9041 config.state = new_crtc_state->base.vrr_enabled ?
9042 VRR_STATE_ACTIVE_VARIABLE :
9046 config.state = VRR_STATE_UNSUPPORTED;
9049 mod_freesync_build_vrr_params(dm->freesync_module,
9051 &config, &vrr_params);
9053 new_crtc_state->freesync_timing_changed |=
9054 (memcmp(&acrtc->dm_irq_params.vrr_params.adjust,
9055 &vrr_params.adjust, sizeof(vrr_params.adjust)) != 0);
9057 new_crtc_state->freesync_config = config;
9058 /* Copy state for access from DM IRQ handler */
9059 acrtc->dm_irq_params.freesync_config = config;
9060 acrtc->dm_irq_params.active_planes = new_crtc_state->active_planes;
9061 acrtc->dm_irq_params.vrr_params = vrr_params;
9062 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
9065 static void amdgpu_dm_handle_vrr_transition(struct dm_crtc_state *old_state,
9066 struct dm_crtc_state *new_state)
9068 bool old_vrr_active = amdgpu_dm_vrr_active(old_state);
9069 bool new_vrr_active = amdgpu_dm_vrr_active(new_state);
9071 if (!old_vrr_active && new_vrr_active) {
9072 /* Transition VRR inactive -> active:
9073 * While VRR is active, we must not disable vblank irq, as a
9074 * reenable after disable would compute bogus vblank/pflip
9075 * timestamps if it likely happened inside display front-porch.
9077 * We also need vupdate irq for the actual core vblank handling
9080 dm_set_vupdate_irq(new_state->base.crtc, true);
9081 drm_crtc_vblank_get(new_state->base.crtc);
9082 DRM_DEBUG_DRIVER("%s: crtc=%u VRR off->on: Get vblank ref\n",
9083 __func__, new_state->base.crtc->base.id);
9084 } else if (old_vrr_active && !new_vrr_active) {
9085 /* Transition VRR active -> inactive:
9086 * Allow vblank irq disable again for fixed refresh rate.
9088 dm_set_vupdate_irq(new_state->base.crtc, false);
9089 drm_crtc_vblank_put(new_state->base.crtc);
9090 DRM_DEBUG_DRIVER("%s: crtc=%u VRR on->off: Drop vblank ref\n",
9091 __func__, new_state->base.crtc->base.id);
9095 static void amdgpu_dm_commit_cursors(struct drm_atomic_state *state)
9097 struct drm_plane *plane;
9098 struct drm_plane_state *old_plane_state;
9102 * TODO: Make this per-stream so we don't issue redundant updates for
9103 * commits with multiple streams.
9105 for_each_old_plane_in_state(state, plane, old_plane_state, i)
9106 if (plane->type == DRM_PLANE_TYPE_CURSOR)
9107 handle_cursor_update(plane, old_plane_state);
9110 static void amdgpu_dm_commit_planes(struct drm_atomic_state *state,
9111 struct dc_state *dc_state,
9112 struct drm_device *dev,
9113 struct amdgpu_display_manager *dm,
9114 struct drm_crtc *pcrtc,
9115 bool wait_for_vblank)
9118 uint64_t timestamp_ns;
9119 struct drm_plane *plane;
9120 struct drm_plane_state *old_plane_state, *new_plane_state;
9121 struct amdgpu_crtc *acrtc_attach = to_amdgpu_crtc(pcrtc);
9122 struct drm_crtc_state *new_pcrtc_state =
9123 drm_atomic_get_new_crtc_state(state, pcrtc);
9124 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(new_pcrtc_state);
9125 struct dm_crtc_state *dm_old_crtc_state =
9126 to_dm_crtc_state(drm_atomic_get_old_crtc_state(state, pcrtc));
9127 int planes_count = 0, vpos, hpos;
9129 unsigned long flags;
9130 struct amdgpu_bo *abo;
9131 uint32_t target_vblank, last_flip_vblank;
9132 bool vrr_active = amdgpu_dm_vrr_active(acrtc_state);
9133 bool pflip_present = false;
9135 struct dc_surface_update surface_updates[MAX_SURFACES];
9136 struct dc_plane_info plane_infos[MAX_SURFACES];
9137 struct dc_scaling_info scaling_infos[MAX_SURFACES];
9138 struct dc_flip_addrs flip_addrs[MAX_SURFACES];
9139 struct dc_stream_update stream_update;
9142 bundle = kzalloc(sizeof(*bundle), GFP_KERNEL);
9145 dm_error("Failed to allocate update bundle\n");
9150 * Disable the cursor first if we're disabling all the planes.
9151 * It'll remain on the screen after the planes are re-enabled
9154 if (acrtc_state->active_planes == 0)
9155 amdgpu_dm_commit_cursors(state);
9157 /* update planes when needed */
9158 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
9159 struct drm_crtc *crtc = new_plane_state->crtc;
9160 struct drm_crtc_state *new_crtc_state;
9161 struct drm_framebuffer *fb = new_plane_state->fb;
9162 struct amdgpu_framebuffer *afb = (struct amdgpu_framebuffer *)fb;
9163 bool plane_needs_flip;
9164 struct dc_plane_state *dc_plane;
9165 struct dm_plane_state *dm_new_plane_state = to_dm_plane_state(new_plane_state);
9167 /* Cursor plane is handled after stream updates */
9168 if (plane->type == DRM_PLANE_TYPE_CURSOR)
9171 if (!fb || !crtc || pcrtc != crtc)
9174 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
9175 if (!new_crtc_state->active)
9178 dc_plane = dm_new_plane_state->dc_state;
9180 bundle->surface_updates[planes_count].surface = dc_plane;
9181 if (new_pcrtc_state->color_mgmt_changed) {
9182 bundle->surface_updates[planes_count].gamma = dc_plane->gamma_correction;
9183 bundle->surface_updates[planes_count].in_transfer_func = dc_plane->in_transfer_func;
9184 bundle->surface_updates[planes_count].gamut_remap_matrix = &dc_plane->gamut_remap_matrix;
9187 fill_dc_scaling_info(dm->adev, new_plane_state,
9188 &bundle->scaling_infos[planes_count]);
9190 bundle->surface_updates[planes_count].scaling_info =
9191 &bundle->scaling_infos[planes_count];
9193 plane_needs_flip = old_plane_state->fb && new_plane_state->fb;
9195 pflip_present = pflip_present || plane_needs_flip;
9197 if (!plane_needs_flip) {
9202 abo = gem_to_amdgpu_bo(fb->obj[0]);
9205 * Wait for all fences on this FB. Do limited wait to avoid
9206 * deadlock during GPU reset when this fence will not signal
9207 * but we hold reservation lock for the BO.
9209 r = dma_resv_wait_timeout(abo->tbo.base.resv,
9210 DMA_RESV_USAGE_WRITE, false,
9211 msecs_to_jiffies(5000));
9212 if (unlikely(r <= 0))
9213 DRM_ERROR("Waiting for fences timed out!");
9215 fill_dc_plane_info_and_addr(
9216 dm->adev, new_plane_state,
9218 &bundle->plane_infos[planes_count],
9219 &bundle->flip_addrs[planes_count].address,
9220 afb->tmz_surface, false);
9222 drm_dbg_state(state->dev, "plane: id=%d dcc_en=%d\n",
9223 new_plane_state->plane->index,
9224 bundle->plane_infos[planes_count].dcc.enable);
9226 bundle->surface_updates[planes_count].plane_info =
9227 &bundle->plane_infos[planes_count];
9230 * Only allow immediate flips for fast updates that don't
9231 * change FB pitch, DCC state, rotation or mirroing.
9233 bundle->flip_addrs[planes_count].flip_immediate =
9234 crtc->state->async_flip &&
9235 acrtc_state->update_type == UPDATE_TYPE_FAST;
9237 timestamp_ns = ktime_get_ns();
9238 bundle->flip_addrs[planes_count].flip_timestamp_in_us = div_u64(timestamp_ns, 1000);
9239 bundle->surface_updates[planes_count].flip_addr = &bundle->flip_addrs[planes_count];
9240 bundle->surface_updates[planes_count].surface = dc_plane;
9242 if (!bundle->surface_updates[planes_count].surface) {
9243 DRM_ERROR("No surface for CRTC: id=%d\n",
9244 acrtc_attach->crtc_id);
9248 if (plane == pcrtc->primary)
9249 update_freesync_state_on_stream(
9252 acrtc_state->stream,
9254 bundle->flip_addrs[planes_count].flip_timestamp_in_us);
9256 drm_dbg_state(state->dev, "%s Flipping to hi: 0x%x, low: 0x%x\n",
9258 bundle->flip_addrs[planes_count].address.grph.addr.high_part,
9259 bundle->flip_addrs[planes_count].address.grph.addr.low_part);
9265 if (pflip_present) {
9267 /* Use old throttling in non-vrr fixed refresh rate mode
9268 * to keep flip scheduling based on target vblank counts
9269 * working in a backwards compatible way, e.g., for
9270 * clients using the GLX_OML_sync_control extension or
9271 * DRI3/Present extension with defined target_msc.
9273 last_flip_vblank = amdgpu_get_vblank_counter_kms(pcrtc);
9276 /* For variable refresh rate mode only:
9277 * Get vblank of last completed flip to avoid > 1 vrr
9278 * flips per video frame by use of throttling, but allow
9279 * flip programming anywhere in the possibly large
9280 * variable vrr vblank interval for fine-grained flip
9281 * timing control and more opportunity to avoid stutter
9282 * on late submission of flips.
9284 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
9285 last_flip_vblank = acrtc_attach->dm_irq_params.last_flip_vblank;
9286 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
9289 target_vblank = last_flip_vblank + wait_for_vblank;
9292 * Wait until we're out of the vertical blank period before the one
9293 * targeted by the flip
9295 while ((acrtc_attach->enabled &&
9296 (amdgpu_display_get_crtc_scanoutpos(dm->ddev, acrtc_attach->crtc_id,
9297 0, &vpos, &hpos, NULL,
9298 NULL, &pcrtc->hwmode)
9299 & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
9300 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
9301 (int)(target_vblank -
9302 amdgpu_get_vblank_counter_kms(pcrtc)) > 0)) {
9303 usleep_range(1000, 1100);
9307 * Prepare the flip event for the pageflip interrupt to handle.
9309 * This only works in the case where we've already turned on the
9310 * appropriate hardware blocks (eg. HUBP) so in the transition case
9311 * from 0 -> n planes we have to skip a hardware generated event
9312 * and rely on sending it from software.
9314 if (acrtc_attach->base.state->event &&
9315 acrtc_state->active_planes > 0 &&
9316 !acrtc_state->force_dpms_off) {
9317 drm_crtc_vblank_get(pcrtc);
9319 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
9321 WARN_ON(acrtc_attach->pflip_status != AMDGPU_FLIP_NONE);
9322 prepare_flip_isr(acrtc_attach);
9324 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
9327 if (acrtc_state->stream) {
9328 if (acrtc_state->freesync_vrr_info_changed)
9329 bundle->stream_update.vrr_infopacket =
9330 &acrtc_state->stream->vrr_infopacket;
9334 /* Update the planes if changed or disable if we don't have any. */
9335 if ((planes_count || acrtc_state->active_planes == 0) &&
9336 acrtc_state->stream) {
9338 * If PSR or idle optimizations are enabled then flush out
9339 * any pending work before hardware programming.
9341 if (dm->vblank_control_workqueue)
9342 flush_workqueue(dm->vblank_control_workqueue);
9344 bundle->stream_update.stream = acrtc_state->stream;
9345 if (new_pcrtc_state->mode_changed) {
9346 bundle->stream_update.src = acrtc_state->stream->src;
9347 bundle->stream_update.dst = acrtc_state->stream->dst;
9350 if (new_pcrtc_state->color_mgmt_changed) {
9352 * TODO: This isn't fully correct since we've actually
9353 * already modified the stream in place.
9355 bundle->stream_update.gamut_remap =
9356 &acrtc_state->stream->gamut_remap_matrix;
9357 bundle->stream_update.output_csc_transform =
9358 &acrtc_state->stream->csc_color_matrix;
9359 bundle->stream_update.out_transfer_func =
9360 acrtc_state->stream->out_transfer_func;
9363 acrtc_state->stream->abm_level = acrtc_state->abm_level;
9364 if (acrtc_state->abm_level != dm_old_crtc_state->abm_level)
9365 bundle->stream_update.abm_level = &acrtc_state->abm_level;
9368 * If FreeSync state on the stream has changed then we need to
9369 * re-adjust the min/max bounds now that DC doesn't handle this
9370 * as part of commit.
9372 if (is_dc_timing_adjust_needed(dm_old_crtc_state, acrtc_state)) {
9373 spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
9374 dc_stream_adjust_vmin_vmax(
9375 dm->dc, acrtc_state->stream,
9376 &acrtc_attach->dm_irq_params.vrr_params.adjust);
9377 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
9379 mutex_lock(&dm->dc_lock);
9380 if ((acrtc_state->update_type > UPDATE_TYPE_FAST) &&
9381 acrtc_state->stream->link->psr_settings.psr_allow_active)
9382 amdgpu_dm_psr_disable(acrtc_state->stream);
9384 dc_commit_updates_for_stream(dm->dc,
9385 bundle->surface_updates,
9387 acrtc_state->stream,
9388 &bundle->stream_update,
9392 * Enable or disable the interrupts on the backend.
9394 * Most pipes are put into power gating when unused.
9396 * When power gating is enabled on a pipe we lose the
9397 * interrupt enablement state when power gating is disabled.
9399 * So we need to update the IRQ control state in hardware
9400 * whenever the pipe turns on (since it could be previously
9401 * power gated) or off (since some pipes can't be power gated
9404 if (dm_old_crtc_state->active_planes != acrtc_state->active_planes)
9405 dm_update_pflip_irq_state(drm_to_adev(dev),
9408 if ((acrtc_state->update_type > UPDATE_TYPE_FAST) &&
9409 acrtc_state->stream->link->psr_settings.psr_version != DC_PSR_VERSION_UNSUPPORTED &&
9410 !acrtc_state->stream->link->psr_settings.psr_feature_enabled)
9411 amdgpu_dm_link_setup_psr(acrtc_state->stream);
9413 /* Decrement skip count when PSR is enabled and we're doing fast updates. */
9414 if (acrtc_state->update_type == UPDATE_TYPE_FAST &&
9415 acrtc_state->stream->link->psr_settings.psr_feature_enabled) {
9416 struct amdgpu_dm_connector *aconn =
9417 (struct amdgpu_dm_connector *)acrtc_state->stream->dm_stream_context;
9419 if (aconn->psr_skip_count > 0)
9420 aconn->psr_skip_count--;
9422 /* Allow PSR when skip count is 0. */
9423 acrtc_attach->dm_irq_params.allow_psr_entry = !aconn->psr_skip_count;
9425 acrtc_attach->dm_irq_params.allow_psr_entry = false;
9428 mutex_unlock(&dm->dc_lock);
9432 * Update cursor state *after* programming all the planes.
9433 * This avoids redundant programming in the case where we're going
9434 * to be disabling a single plane - those pipes are being disabled.
9436 if (acrtc_state->active_planes)
9437 amdgpu_dm_commit_cursors(state);
9443 static void amdgpu_dm_commit_audio(struct drm_device *dev,
9444 struct drm_atomic_state *state)
9446 struct amdgpu_device *adev = drm_to_adev(dev);
9447 struct amdgpu_dm_connector *aconnector;
9448 struct drm_connector *connector;
9449 struct drm_connector_state *old_con_state, *new_con_state;
9450 struct drm_crtc_state *new_crtc_state;
9451 struct dm_crtc_state *new_dm_crtc_state;
9452 const struct dc_stream_status *status;
9455 /* Notify device removals. */
9456 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
9457 if (old_con_state->crtc != new_con_state->crtc) {
9458 /* CRTC changes require notification. */
9462 if (!new_con_state->crtc)
9465 new_crtc_state = drm_atomic_get_new_crtc_state(
9466 state, new_con_state->crtc);
9468 if (!new_crtc_state)
9471 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
9475 aconnector = to_amdgpu_dm_connector(connector);
9477 mutex_lock(&adev->dm.audio_lock);
9478 inst = aconnector->audio_inst;
9479 aconnector->audio_inst = -1;
9480 mutex_unlock(&adev->dm.audio_lock);
9482 amdgpu_dm_audio_eld_notify(adev, inst);
9485 /* Notify audio device additions. */
9486 for_each_new_connector_in_state(state, connector, new_con_state, i) {
9487 if (!new_con_state->crtc)
9490 new_crtc_state = drm_atomic_get_new_crtc_state(
9491 state, new_con_state->crtc);
9493 if (!new_crtc_state)
9496 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
9499 new_dm_crtc_state = to_dm_crtc_state(new_crtc_state);
9500 if (!new_dm_crtc_state->stream)
9503 status = dc_stream_get_status(new_dm_crtc_state->stream);
9507 aconnector = to_amdgpu_dm_connector(connector);
9509 mutex_lock(&adev->dm.audio_lock);
9510 inst = status->audio_inst;
9511 aconnector->audio_inst = inst;
9512 mutex_unlock(&adev->dm.audio_lock);
9514 amdgpu_dm_audio_eld_notify(adev, inst);
9519 * amdgpu_dm_crtc_copy_transient_flags - copy mirrored flags from DRM to DC
9520 * @crtc_state: the DRM CRTC state
9521 * @stream_state: the DC stream state.
9523 * Copy the mirrored transient state flags from DRM, to DC. It is used to bring
9524 * a dc_stream_state's flags in sync with a drm_crtc_state's flags.
9526 static void amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state *crtc_state,
9527 struct dc_stream_state *stream_state)
9529 stream_state->mode_changed = drm_atomic_crtc_needs_modeset(crtc_state);
9533 * amdgpu_dm_atomic_commit_tail() - AMDgpu DM's commit tail implementation.
9534 * @state: The atomic state to commit
9536 * This will tell DC to commit the constructed DC state from atomic_check,
9537 * programming the hardware. Any failures here implies a hardware failure, since
9538 * atomic check should have filtered anything non-kosher.
9540 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state)
9542 struct drm_device *dev = state->dev;
9543 struct amdgpu_device *adev = drm_to_adev(dev);
9544 struct amdgpu_display_manager *dm = &adev->dm;
9545 struct dm_atomic_state *dm_state;
9546 struct dc_state *dc_state = NULL, *dc_state_temp = NULL;
9548 struct drm_crtc *crtc;
9549 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
9550 unsigned long flags;
9551 bool wait_for_vblank = true;
9552 struct drm_connector *connector;
9553 struct drm_connector_state *old_con_state, *new_con_state;
9554 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
9555 int crtc_disable_count = 0;
9556 bool mode_set_reset_required = false;
9558 trace_amdgpu_dm_atomic_commit_tail_begin(state);
9560 drm_atomic_helper_update_legacy_modeset_state(dev, state);
9562 dm_state = dm_atomic_get_new_state(state);
9563 if (dm_state && dm_state->context) {
9564 dc_state = dm_state->context;
9566 /* No state changes, retain current state. */
9567 dc_state_temp = dc_create_state(dm->dc);
9568 ASSERT(dc_state_temp);
9569 dc_state = dc_state_temp;
9570 dc_resource_state_copy_construct_current(dm->dc, dc_state);
9573 for_each_oldnew_crtc_in_state (state, crtc, old_crtc_state,
9574 new_crtc_state, i) {
9575 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
9577 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
9579 if (old_crtc_state->active &&
9580 (!new_crtc_state->active ||
9581 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
9582 manage_dm_interrupts(adev, acrtc, false);
9583 dc_stream_release(dm_old_crtc_state->stream);
9587 drm_atomic_helper_calc_timestamping_constants(state);
9589 /* update changed items */
9590 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
9591 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
9593 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
9594 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
9596 drm_dbg_state(state->dev,
9597 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
9598 "planes_changed:%d, mode_changed:%d,active_changed:%d,"
9599 "connectors_changed:%d\n",
9601 new_crtc_state->enable,
9602 new_crtc_state->active,
9603 new_crtc_state->planes_changed,
9604 new_crtc_state->mode_changed,
9605 new_crtc_state->active_changed,
9606 new_crtc_state->connectors_changed);
9608 /* Disable cursor if disabling crtc */
9609 if (old_crtc_state->active && !new_crtc_state->active) {
9610 struct dc_cursor_position position;
9612 memset(&position, 0, sizeof(position));
9613 mutex_lock(&dm->dc_lock);
9614 dc_stream_set_cursor_position(dm_old_crtc_state->stream, &position);
9615 mutex_unlock(&dm->dc_lock);
9618 /* Copy all transient state flags into dc state */
9619 if (dm_new_crtc_state->stream) {
9620 amdgpu_dm_crtc_copy_transient_flags(&dm_new_crtc_state->base,
9621 dm_new_crtc_state->stream);
9624 /* handles headless hotplug case, updating new_state and
9625 * aconnector as needed
9628 if (modeset_required(new_crtc_state, dm_new_crtc_state->stream, dm_old_crtc_state->stream)) {
9630 DRM_DEBUG_ATOMIC("Atomic commit: SET crtc id %d: [%p]\n", acrtc->crtc_id, acrtc);
9632 if (!dm_new_crtc_state->stream) {
9634 * this could happen because of issues with
9635 * userspace notifications delivery.
9636 * In this case userspace tries to set mode on
9637 * display which is disconnected in fact.
9638 * dc_sink is NULL in this case on aconnector.
9639 * We expect reset mode will come soon.
9641 * This can also happen when unplug is done
9642 * during resume sequence ended
9644 * In this case, we want to pretend we still
9645 * have a sink to keep the pipe running so that
9646 * hw state is consistent with the sw state
9648 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
9649 __func__, acrtc->base.base.id);
9653 if (dm_old_crtc_state->stream)
9654 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
9656 pm_runtime_get_noresume(dev->dev);
9658 acrtc->enabled = true;
9659 acrtc->hw_mode = new_crtc_state->mode;
9660 crtc->hwmode = new_crtc_state->mode;
9661 mode_set_reset_required = true;
9662 } else if (modereset_required(new_crtc_state)) {
9663 DRM_DEBUG_ATOMIC("Atomic commit: RESET. crtc id %d:[%p]\n", acrtc->crtc_id, acrtc);
9664 /* i.e. reset mode */
9665 if (dm_old_crtc_state->stream)
9666 remove_stream(adev, acrtc, dm_old_crtc_state->stream);
9668 mode_set_reset_required = true;
9670 } /* for_each_crtc_in_state() */
9673 /* if there mode set or reset, disable eDP PSR */
9674 if (mode_set_reset_required) {
9675 if (dm->vblank_control_workqueue)
9676 flush_workqueue(dm->vblank_control_workqueue);
9678 amdgpu_dm_psr_disable_all(dm);
9681 dm_enable_per_frame_crtc_master_sync(dc_state);
9682 mutex_lock(&dm->dc_lock);
9683 WARN_ON(!dc_commit_state(dm->dc, dc_state));
9685 /* Allow idle optimization when vblank count is 0 for display off */
9686 if (dm->active_vblank_irq_count == 0)
9687 dc_allow_idle_optimizations(dm->dc, true);
9688 mutex_unlock(&dm->dc_lock);
9691 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
9692 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
9694 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
9696 if (dm_new_crtc_state->stream != NULL) {
9697 const struct dc_stream_status *status =
9698 dc_stream_get_status(dm_new_crtc_state->stream);
9701 status = dc_stream_get_status_from_state(dc_state,
9702 dm_new_crtc_state->stream);
9704 DC_ERR("got no status for stream %p on acrtc%p\n", dm_new_crtc_state->stream, acrtc);
9706 acrtc->otg_inst = status->primary_otg_inst;
9709 #ifdef CONFIG_DRM_AMD_DC_HDCP
9710 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
9711 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
9712 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
9713 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
9715 new_crtc_state = NULL;
9718 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
9720 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
9722 if (dm_new_crtc_state && dm_new_crtc_state->stream == NULL &&
9723 connector->state->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
9724 hdcp_reset_display(adev->dm.hdcp_workqueue, aconnector->dc_link->link_index);
9725 new_con_state->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED;
9726 dm_new_con_state->update_hdcp = true;
9730 if (is_content_protection_different(new_con_state, old_con_state, connector, adev->dm.hdcp_workqueue))
9731 hdcp_update_display(
9732 adev->dm.hdcp_workqueue, aconnector->dc_link->link_index, aconnector,
9733 new_con_state->hdcp_content_type,
9734 new_con_state->content_protection == DRM_MODE_CONTENT_PROTECTION_DESIRED);
9738 /* Handle connector state changes */
9739 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
9740 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
9741 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
9742 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
9743 struct dc_surface_update dummy_updates[MAX_SURFACES];
9744 struct dc_stream_update stream_update;
9745 struct dc_info_packet hdr_packet;
9746 struct dc_stream_status *status = NULL;
9747 bool abm_changed, hdr_changed, scaling_changed;
9749 memset(&dummy_updates, 0, sizeof(dummy_updates));
9750 memset(&stream_update, 0, sizeof(stream_update));
9753 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
9754 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base);
9757 /* Skip any modesets/resets */
9758 if (!acrtc || drm_atomic_crtc_needs_modeset(new_crtc_state))
9761 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
9762 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
9764 scaling_changed = is_scaling_state_different(dm_new_con_state,
9767 abm_changed = dm_new_crtc_state->abm_level !=
9768 dm_old_crtc_state->abm_level;
9771 !drm_connector_atomic_hdr_metadata_equal(old_con_state, new_con_state);
9773 if (!scaling_changed && !abm_changed && !hdr_changed)
9776 stream_update.stream = dm_new_crtc_state->stream;
9777 if (scaling_changed) {
9778 update_stream_scaling_settings(&dm_new_con_state->base.crtc->mode,
9779 dm_new_con_state, dm_new_crtc_state->stream);
9781 stream_update.src = dm_new_crtc_state->stream->src;
9782 stream_update.dst = dm_new_crtc_state->stream->dst;
9786 dm_new_crtc_state->stream->abm_level = dm_new_crtc_state->abm_level;
9788 stream_update.abm_level = &dm_new_crtc_state->abm_level;
9792 fill_hdr_info_packet(new_con_state, &hdr_packet);
9793 stream_update.hdr_static_metadata = &hdr_packet;
9796 status = dc_stream_get_status(dm_new_crtc_state->stream);
9798 if (WARN_ON(!status))
9801 WARN_ON(!status->plane_count);
9804 * TODO: DC refuses to perform stream updates without a dc_surface_update.
9805 * Here we create an empty update on each plane.
9806 * To fix this, DC should permit updating only stream properties.
9808 for (j = 0; j < status->plane_count; j++)
9809 dummy_updates[j].surface = status->plane_states[0];
9812 mutex_lock(&dm->dc_lock);
9813 dc_commit_updates_for_stream(dm->dc,
9815 status->plane_count,
9816 dm_new_crtc_state->stream,
9819 mutex_unlock(&dm->dc_lock);
9822 /* Count number of newly disabled CRTCs for dropping PM refs later. */
9823 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
9824 new_crtc_state, i) {
9825 if (old_crtc_state->active && !new_crtc_state->active)
9826 crtc_disable_count++;
9828 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
9829 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
9831 /* For freesync config update on crtc state and params for irq */
9832 update_stream_irq_parameters(dm, dm_new_crtc_state);
9834 /* Handle vrr on->off / off->on transitions */
9835 amdgpu_dm_handle_vrr_transition(dm_old_crtc_state,
9840 * Enable interrupts for CRTCs that are newly enabled or went through
9841 * a modeset. It was intentionally deferred until after the front end
9842 * state was modified to wait until the OTG was on and so the IRQ
9843 * handlers didn't access stale or invalid state.
9845 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
9846 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
9847 #ifdef CONFIG_DEBUG_FS
9848 bool configure_crc = false;
9849 enum amdgpu_dm_pipe_crc_source cur_crc_src;
9850 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
9851 struct crc_rd_work *crc_rd_wrk = dm->crc_rd_wrk;
9853 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
9854 cur_crc_src = acrtc->dm_irq_params.crc_src;
9855 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
9857 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
9859 if (new_crtc_state->active &&
9860 (!old_crtc_state->active ||
9861 drm_atomic_crtc_needs_modeset(new_crtc_state))) {
9862 dc_stream_retain(dm_new_crtc_state->stream);
9863 acrtc->dm_irq_params.stream = dm_new_crtc_state->stream;
9864 manage_dm_interrupts(adev, acrtc, true);
9866 #ifdef CONFIG_DEBUG_FS
9868 * Frontend may have changed so reapply the CRC capture
9869 * settings for the stream.
9871 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
9873 if (amdgpu_dm_is_valid_crc_source(cur_crc_src)) {
9874 configure_crc = true;
9875 #if defined(CONFIG_DRM_AMD_SECURE_DISPLAY)
9876 if (amdgpu_dm_crc_window_is_activated(crtc)) {
9877 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
9878 acrtc->dm_irq_params.crc_window.update_win = true;
9879 acrtc->dm_irq_params.crc_window.skip_frame_cnt = 2;
9880 spin_lock_irq(&crc_rd_wrk->crc_rd_work_lock);
9881 crc_rd_wrk->crtc = crtc;
9882 spin_unlock_irq(&crc_rd_wrk->crc_rd_work_lock);
9883 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
9889 if (amdgpu_dm_crtc_configure_crc_source(
9890 crtc, dm_new_crtc_state, cur_crc_src))
9891 DRM_DEBUG_DRIVER("Failed to configure crc source");
9896 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j)
9897 if (new_crtc_state->async_flip)
9898 wait_for_vblank = false;
9900 /* update planes when needed per crtc*/
9901 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j) {
9902 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
9904 if (dm_new_crtc_state->stream)
9905 amdgpu_dm_commit_planes(state, dc_state, dev,
9906 dm, crtc, wait_for_vblank);
9909 /* Update audio instances for each connector. */
9910 amdgpu_dm_commit_audio(dev, state);
9912 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || \
9913 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
9914 /* restore the backlight level */
9915 for (i = 0; i < dm->num_of_edps; i++) {
9916 if (dm->backlight_dev[i] &&
9917 (dm->actual_brightness[i] != dm->brightness[i]))
9918 amdgpu_dm_backlight_set_level(dm, i, dm->brightness[i]);
9922 * send vblank event on all events not handled in flip and
9923 * mark consumed event for drm_atomic_helper_commit_hw_done
9925 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
9926 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
9928 if (new_crtc_state->event)
9929 drm_send_event_locked(dev, &new_crtc_state->event->base);
9931 new_crtc_state->event = NULL;
9933 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
9935 /* Signal HW programming completion */
9936 drm_atomic_helper_commit_hw_done(state);
9938 if (wait_for_vblank)
9939 drm_atomic_helper_wait_for_flip_done(dev, state);
9941 drm_atomic_helper_cleanup_planes(dev, state);
9943 /* return the stolen vga memory back to VRAM */
9944 if (!adev->mman.keep_stolen_vga_memory)
9945 amdgpu_bo_free_kernel(&adev->mman.stolen_vga_memory, NULL, NULL);
9946 amdgpu_bo_free_kernel(&adev->mman.stolen_extended_memory, NULL, NULL);
9949 * Finally, drop a runtime PM reference for each newly disabled CRTC,
9950 * so we can put the GPU into runtime suspend if we're not driving any
9953 for (i = 0; i < crtc_disable_count; i++)
9954 pm_runtime_put_autosuspend(dev->dev);
9955 pm_runtime_mark_last_busy(dev->dev);
9958 dc_release_state(dc_state_temp);
9962 static int dm_force_atomic_commit(struct drm_connector *connector)
9965 struct drm_device *ddev = connector->dev;
9966 struct drm_atomic_state *state = drm_atomic_state_alloc(ddev);
9967 struct amdgpu_crtc *disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
9968 struct drm_plane *plane = disconnected_acrtc->base.primary;
9969 struct drm_connector_state *conn_state;
9970 struct drm_crtc_state *crtc_state;
9971 struct drm_plane_state *plane_state;
9976 state->acquire_ctx = ddev->mode_config.acquire_ctx;
9978 /* Construct an atomic state to restore previous display setting */
9981 * Attach connectors to drm_atomic_state
9983 conn_state = drm_atomic_get_connector_state(state, connector);
9985 ret = PTR_ERR_OR_ZERO(conn_state);
9989 /* Attach crtc to drm_atomic_state*/
9990 crtc_state = drm_atomic_get_crtc_state(state, &disconnected_acrtc->base);
9992 ret = PTR_ERR_OR_ZERO(crtc_state);
9996 /* force a restore */
9997 crtc_state->mode_changed = true;
9999 /* Attach plane to drm_atomic_state */
10000 plane_state = drm_atomic_get_plane_state(state, plane);
10002 ret = PTR_ERR_OR_ZERO(plane_state);
10006 /* Call commit internally with the state we just constructed */
10007 ret = drm_atomic_commit(state);
10010 drm_atomic_state_put(state);
10012 DRM_ERROR("Restoring old state failed with %i\n", ret);
10018 * This function handles all cases when set mode does not come upon hotplug.
10019 * This includes when a display is unplugged then plugged back into the
10020 * same port and when running without usermode desktop manager supprot
10022 void dm_restore_drm_connector_state(struct drm_device *dev,
10023 struct drm_connector *connector)
10025 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
10026 struct amdgpu_crtc *disconnected_acrtc;
10027 struct dm_crtc_state *acrtc_state;
10029 if (!aconnector->dc_sink || !connector->state || !connector->encoder)
10032 disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
10033 if (!disconnected_acrtc)
10036 acrtc_state = to_dm_crtc_state(disconnected_acrtc->base.state);
10037 if (!acrtc_state->stream)
10041 * If the previous sink is not released and different from the current,
10042 * we deduce we are in a state where we can not rely on usermode call
10043 * to turn on the display, so we do it here
10045 if (acrtc_state->stream->sink != aconnector->dc_sink)
10046 dm_force_atomic_commit(&aconnector->base);
10050 * Grabs all modesetting locks to serialize against any blocking commits,
10051 * Waits for completion of all non blocking commits.
10053 static int do_aquire_global_lock(struct drm_device *dev,
10054 struct drm_atomic_state *state)
10056 struct drm_crtc *crtc;
10057 struct drm_crtc_commit *commit;
10061 * Adding all modeset locks to aquire_ctx will
10062 * ensure that when the framework release it the
10063 * extra locks we are locking here will get released to
10065 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
10069 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
10070 spin_lock(&crtc->commit_lock);
10071 commit = list_first_entry_or_null(&crtc->commit_list,
10072 struct drm_crtc_commit, commit_entry);
10074 drm_crtc_commit_get(commit);
10075 spin_unlock(&crtc->commit_lock);
10081 * Make sure all pending HW programming completed and
10084 ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ);
10087 ret = wait_for_completion_interruptible_timeout(
10088 &commit->flip_done, 10*HZ);
10091 DRM_ERROR("[CRTC:%d:%s] hw_done or flip_done "
10092 "timed out\n", crtc->base.id, crtc->name);
10094 drm_crtc_commit_put(commit);
10097 return ret < 0 ? ret : 0;
10100 static void get_freesync_config_for_crtc(
10101 struct dm_crtc_state *new_crtc_state,
10102 struct dm_connector_state *new_con_state)
10104 struct mod_freesync_config config = {0};
10105 struct amdgpu_dm_connector *aconnector =
10106 to_amdgpu_dm_connector(new_con_state->base.connector);
10107 struct drm_display_mode *mode = &new_crtc_state->base.mode;
10108 int vrefresh = drm_mode_vrefresh(mode);
10109 bool fs_vid_mode = false;
10111 new_crtc_state->vrr_supported = new_con_state->freesync_capable &&
10112 vrefresh >= aconnector->min_vfreq &&
10113 vrefresh <= aconnector->max_vfreq;
10115 if (new_crtc_state->vrr_supported) {
10116 new_crtc_state->stream->ignore_msa_timing_param = true;
10117 fs_vid_mode = new_crtc_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED;
10119 config.min_refresh_in_uhz = aconnector->min_vfreq * 1000000;
10120 config.max_refresh_in_uhz = aconnector->max_vfreq * 1000000;
10121 config.vsif_supported = true;
10125 config.state = VRR_STATE_ACTIVE_FIXED;
10126 config.fixed_refresh_in_uhz = new_crtc_state->freesync_config.fixed_refresh_in_uhz;
10128 } else if (new_crtc_state->base.vrr_enabled) {
10129 config.state = VRR_STATE_ACTIVE_VARIABLE;
10131 config.state = VRR_STATE_INACTIVE;
10135 new_crtc_state->freesync_config = config;
10138 static void reset_freesync_config_for_crtc(
10139 struct dm_crtc_state *new_crtc_state)
10141 new_crtc_state->vrr_supported = false;
10143 memset(&new_crtc_state->vrr_infopacket, 0,
10144 sizeof(new_crtc_state->vrr_infopacket));
10148 is_timing_unchanged_for_freesync(struct drm_crtc_state *old_crtc_state,
10149 struct drm_crtc_state *new_crtc_state)
10151 const struct drm_display_mode *old_mode, *new_mode;
10153 if (!old_crtc_state || !new_crtc_state)
10156 old_mode = &old_crtc_state->mode;
10157 new_mode = &new_crtc_state->mode;
10159 if (old_mode->clock == new_mode->clock &&
10160 old_mode->hdisplay == new_mode->hdisplay &&
10161 old_mode->vdisplay == new_mode->vdisplay &&
10162 old_mode->htotal == new_mode->htotal &&
10163 old_mode->vtotal != new_mode->vtotal &&
10164 old_mode->hsync_start == new_mode->hsync_start &&
10165 old_mode->vsync_start != new_mode->vsync_start &&
10166 old_mode->hsync_end == new_mode->hsync_end &&
10167 old_mode->vsync_end != new_mode->vsync_end &&
10168 old_mode->hskew == new_mode->hskew &&
10169 old_mode->vscan == new_mode->vscan &&
10170 (old_mode->vsync_end - old_mode->vsync_start) ==
10171 (new_mode->vsync_end - new_mode->vsync_start))
10177 static void set_freesync_fixed_config(struct dm_crtc_state *dm_new_crtc_state) {
10178 uint64_t num, den, res;
10179 struct drm_crtc_state *new_crtc_state = &dm_new_crtc_state->base;
10181 dm_new_crtc_state->freesync_config.state = VRR_STATE_ACTIVE_FIXED;
10183 num = (unsigned long long)new_crtc_state->mode.clock * 1000 * 1000000;
10184 den = (unsigned long long)new_crtc_state->mode.htotal *
10185 (unsigned long long)new_crtc_state->mode.vtotal;
10187 res = div_u64(num, den);
10188 dm_new_crtc_state->freesync_config.fixed_refresh_in_uhz = res;
10191 static int dm_update_crtc_state(struct amdgpu_display_manager *dm,
10192 struct drm_atomic_state *state,
10193 struct drm_crtc *crtc,
10194 struct drm_crtc_state *old_crtc_state,
10195 struct drm_crtc_state *new_crtc_state,
10197 bool *lock_and_validation_needed)
10199 struct dm_atomic_state *dm_state = NULL;
10200 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
10201 struct dc_stream_state *new_stream;
10205 * TODO Move this code into dm_crtc_atomic_check once we get rid of dc_validation_set
10206 * update changed items
10208 struct amdgpu_crtc *acrtc = NULL;
10209 struct amdgpu_dm_connector *aconnector = NULL;
10210 struct drm_connector_state *drm_new_conn_state = NULL, *drm_old_conn_state = NULL;
10211 struct dm_connector_state *dm_new_conn_state = NULL, *dm_old_conn_state = NULL;
10215 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
10216 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
10217 acrtc = to_amdgpu_crtc(crtc);
10218 aconnector = amdgpu_dm_find_first_crtc_matching_connector(state, crtc);
10220 /* TODO This hack should go away */
10221 if (aconnector && enable) {
10222 /* Make sure fake sink is created in plug-in scenario */
10223 drm_new_conn_state = drm_atomic_get_new_connector_state(state,
10224 &aconnector->base);
10225 drm_old_conn_state = drm_atomic_get_old_connector_state(state,
10226 &aconnector->base);
10228 if (IS_ERR(drm_new_conn_state)) {
10229 ret = PTR_ERR_OR_ZERO(drm_new_conn_state);
10233 dm_new_conn_state = to_dm_connector_state(drm_new_conn_state);
10234 dm_old_conn_state = to_dm_connector_state(drm_old_conn_state);
10236 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
10239 new_stream = create_validate_stream_for_sink(aconnector,
10240 &new_crtc_state->mode,
10242 dm_old_crtc_state->stream);
10245 * we can have no stream on ACTION_SET if a display
10246 * was disconnected during S3, in this case it is not an
10247 * error, the OS will be updated after detection, and
10248 * will do the right thing on next atomic commit
10252 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
10253 __func__, acrtc->base.base.id);
10259 * TODO: Check VSDB bits to decide whether this should
10260 * be enabled or not.
10262 new_stream->triggered_crtc_reset.enabled =
10263 dm->force_timing_sync;
10265 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
10267 ret = fill_hdr_info_packet(drm_new_conn_state,
10268 &new_stream->hdr_static_metadata);
10273 * If we already removed the old stream from the context
10274 * (and set the new stream to NULL) then we can't reuse
10275 * the old stream even if the stream and scaling are unchanged.
10276 * We'll hit the BUG_ON and black screen.
10278 * TODO: Refactor this function to allow this check to work
10279 * in all conditions.
10281 if (dm_new_crtc_state->stream &&
10282 is_timing_unchanged_for_freesync(new_crtc_state, old_crtc_state))
10285 if (dm_new_crtc_state->stream &&
10286 dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) &&
10287 dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream)) {
10288 new_crtc_state->mode_changed = false;
10289 DRM_DEBUG_DRIVER("Mode change not required, setting mode_changed to %d",
10290 new_crtc_state->mode_changed);
10294 /* mode_changed flag may get updated above, need to check again */
10295 if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
10298 drm_dbg_state(state->dev,
10299 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
10300 "planes_changed:%d, mode_changed:%d,active_changed:%d,"
10301 "connectors_changed:%d\n",
10303 new_crtc_state->enable,
10304 new_crtc_state->active,
10305 new_crtc_state->planes_changed,
10306 new_crtc_state->mode_changed,
10307 new_crtc_state->active_changed,
10308 new_crtc_state->connectors_changed);
10310 /* Remove stream for any changed/disabled CRTC */
10313 if (!dm_old_crtc_state->stream)
10316 if (dm_new_crtc_state->stream &&
10317 is_timing_unchanged_for_freesync(new_crtc_state,
10319 new_crtc_state->mode_changed = false;
10321 "Mode change not required for front porch change, "
10322 "setting mode_changed to %d",
10323 new_crtc_state->mode_changed);
10325 set_freesync_fixed_config(dm_new_crtc_state);
10328 } else if (aconnector &&
10329 is_freesync_video_mode(&new_crtc_state->mode,
10331 struct drm_display_mode *high_mode;
10333 high_mode = get_highest_refresh_rate_mode(aconnector, false);
10334 if (!drm_mode_equal(&new_crtc_state->mode, high_mode)) {
10335 set_freesync_fixed_config(dm_new_crtc_state);
10339 ret = dm_atomic_get_state(state, &dm_state);
10343 DRM_DEBUG_DRIVER("Disabling DRM crtc: %d\n",
10346 /* i.e. reset mode */
10347 if (dc_remove_stream_from_ctx(
10350 dm_old_crtc_state->stream) != DC_OK) {
10355 dc_stream_release(dm_old_crtc_state->stream);
10356 dm_new_crtc_state->stream = NULL;
10358 reset_freesync_config_for_crtc(dm_new_crtc_state);
10360 *lock_and_validation_needed = true;
10362 } else {/* Add stream for any updated/enabled CRTC */
10364 * Quick fix to prevent NULL pointer on new_stream when
10365 * added MST connectors not found in existing crtc_state in the chained mode
10366 * TODO: need to dig out the root cause of that
10368 if (!aconnector || (!aconnector->dc_sink && aconnector->mst_port))
10371 if (modereset_required(new_crtc_state))
10374 if (modeset_required(new_crtc_state, new_stream,
10375 dm_old_crtc_state->stream)) {
10377 WARN_ON(dm_new_crtc_state->stream);
10379 ret = dm_atomic_get_state(state, &dm_state);
10383 dm_new_crtc_state->stream = new_stream;
10385 dc_stream_retain(new_stream);
10387 DRM_DEBUG_ATOMIC("Enabling DRM crtc: %d\n",
10390 if (dc_add_stream_to_ctx(
10393 dm_new_crtc_state->stream) != DC_OK) {
10398 *lock_and_validation_needed = true;
10403 /* Release extra reference */
10405 dc_stream_release(new_stream);
10408 * We want to do dc stream updates that do not require a
10409 * full modeset below.
10411 if (!(enable && aconnector && new_crtc_state->active))
10414 * Given above conditions, the dc state cannot be NULL because:
10415 * 1. We're in the process of enabling CRTCs (just been added
10416 * to the dc context, or already is on the context)
10417 * 2. Has a valid connector attached, and
10418 * 3. Is currently active and enabled.
10419 * => The dc stream state currently exists.
10421 BUG_ON(dm_new_crtc_state->stream == NULL);
10423 /* Scaling or underscan settings */
10424 if (is_scaling_state_different(dm_old_conn_state, dm_new_conn_state) ||
10425 drm_atomic_crtc_needs_modeset(new_crtc_state))
10426 update_stream_scaling_settings(
10427 &new_crtc_state->mode, dm_new_conn_state, dm_new_crtc_state->stream);
10430 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level;
10433 * Color management settings. We also update color properties
10434 * when a modeset is needed, to ensure it gets reprogrammed.
10436 if (dm_new_crtc_state->base.color_mgmt_changed ||
10437 drm_atomic_crtc_needs_modeset(new_crtc_state)) {
10438 ret = amdgpu_dm_update_crtc_color_mgmt(dm_new_crtc_state);
10443 /* Update Freesync settings. */
10444 get_freesync_config_for_crtc(dm_new_crtc_state,
10445 dm_new_conn_state);
10451 dc_stream_release(new_stream);
10455 static bool should_reset_plane(struct drm_atomic_state *state,
10456 struct drm_plane *plane,
10457 struct drm_plane_state *old_plane_state,
10458 struct drm_plane_state *new_plane_state)
10460 struct drm_plane *other;
10461 struct drm_plane_state *old_other_state, *new_other_state;
10462 struct drm_crtc_state *new_crtc_state;
10466 * TODO: Remove this hack once the checks below are sufficient
10467 * enough to determine when we need to reset all the planes on
10470 if (state->allow_modeset)
10473 /* Exit early if we know that we're adding or removing the plane. */
10474 if (old_plane_state->crtc != new_plane_state->crtc)
10477 /* old crtc == new_crtc == NULL, plane not in context. */
10478 if (!new_plane_state->crtc)
10482 drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
10484 if (!new_crtc_state)
10487 /* CRTC Degamma changes currently require us to recreate planes. */
10488 if (new_crtc_state->color_mgmt_changed)
10491 if (drm_atomic_crtc_needs_modeset(new_crtc_state))
10495 * If there are any new primary or overlay planes being added or
10496 * removed then the z-order can potentially change. To ensure
10497 * correct z-order and pipe acquisition the current DC architecture
10498 * requires us to remove and recreate all existing planes.
10500 * TODO: Come up with a more elegant solution for this.
10502 for_each_oldnew_plane_in_state(state, other, old_other_state, new_other_state, i) {
10503 struct amdgpu_framebuffer *old_afb, *new_afb;
10504 if (other->type == DRM_PLANE_TYPE_CURSOR)
10507 if (old_other_state->crtc != new_plane_state->crtc &&
10508 new_other_state->crtc != new_plane_state->crtc)
10511 if (old_other_state->crtc != new_other_state->crtc)
10514 /* Src/dst size and scaling updates. */
10515 if (old_other_state->src_w != new_other_state->src_w ||
10516 old_other_state->src_h != new_other_state->src_h ||
10517 old_other_state->crtc_w != new_other_state->crtc_w ||
10518 old_other_state->crtc_h != new_other_state->crtc_h)
10521 /* Rotation / mirroring updates. */
10522 if (old_other_state->rotation != new_other_state->rotation)
10525 /* Blending updates. */
10526 if (old_other_state->pixel_blend_mode !=
10527 new_other_state->pixel_blend_mode)
10530 /* Alpha updates. */
10531 if (old_other_state->alpha != new_other_state->alpha)
10534 /* Colorspace changes. */
10535 if (old_other_state->color_range != new_other_state->color_range ||
10536 old_other_state->color_encoding != new_other_state->color_encoding)
10539 /* Framebuffer checks fall at the end. */
10540 if (!old_other_state->fb || !new_other_state->fb)
10543 /* Pixel format changes can require bandwidth updates. */
10544 if (old_other_state->fb->format != new_other_state->fb->format)
10547 old_afb = (struct amdgpu_framebuffer *)old_other_state->fb;
10548 new_afb = (struct amdgpu_framebuffer *)new_other_state->fb;
10550 /* Tiling and DCC changes also require bandwidth updates. */
10551 if (old_afb->tiling_flags != new_afb->tiling_flags ||
10552 old_afb->base.modifier != new_afb->base.modifier)
10559 static int dm_check_cursor_fb(struct amdgpu_crtc *new_acrtc,
10560 struct drm_plane_state *new_plane_state,
10561 struct drm_framebuffer *fb)
10563 struct amdgpu_device *adev = drm_to_adev(new_acrtc->base.dev);
10564 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(fb);
10565 unsigned int pitch;
10568 if (fb->width > new_acrtc->max_cursor_width ||
10569 fb->height > new_acrtc->max_cursor_height) {
10570 DRM_DEBUG_ATOMIC("Bad cursor FB size %dx%d\n",
10571 new_plane_state->fb->width,
10572 new_plane_state->fb->height);
10575 if (new_plane_state->src_w != fb->width << 16 ||
10576 new_plane_state->src_h != fb->height << 16) {
10577 DRM_DEBUG_ATOMIC("Cropping not supported for cursor plane\n");
10581 /* Pitch in pixels */
10582 pitch = fb->pitches[0] / fb->format->cpp[0];
10584 if (fb->width != pitch) {
10585 DRM_DEBUG_ATOMIC("Cursor FB width %d doesn't match pitch %d",
10594 /* FB pitch is supported by cursor plane */
10597 DRM_DEBUG_ATOMIC("Bad cursor FB pitch %d px\n", pitch);
10601 /* Core DRM takes care of checking FB modifiers, so we only need to
10602 * check tiling flags when the FB doesn't have a modifier. */
10603 if (!(fb->flags & DRM_MODE_FB_MODIFIERS)) {
10604 if (adev->family < AMDGPU_FAMILY_AI) {
10605 linear = AMDGPU_TILING_GET(afb->tiling_flags, ARRAY_MODE) != DC_ARRAY_2D_TILED_THIN1 &&
10606 AMDGPU_TILING_GET(afb->tiling_flags, ARRAY_MODE) != DC_ARRAY_1D_TILED_THIN1 &&
10607 AMDGPU_TILING_GET(afb->tiling_flags, MICRO_TILE_MODE) == 0;
10609 linear = AMDGPU_TILING_GET(afb->tiling_flags, SWIZZLE_MODE) == 0;
10612 DRM_DEBUG_ATOMIC("Cursor FB not linear");
10620 static int dm_update_plane_state(struct dc *dc,
10621 struct drm_atomic_state *state,
10622 struct drm_plane *plane,
10623 struct drm_plane_state *old_plane_state,
10624 struct drm_plane_state *new_plane_state,
10626 bool *lock_and_validation_needed)
10629 struct dm_atomic_state *dm_state = NULL;
10630 struct drm_crtc *new_plane_crtc, *old_plane_crtc;
10631 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
10632 struct dm_crtc_state *dm_new_crtc_state, *dm_old_crtc_state;
10633 struct dm_plane_state *dm_new_plane_state, *dm_old_plane_state;
10634 struct amdgpu_crtc *new_acrtc;
10639 new_plane_crtc = new_plane_state->crtc;
10640 old_plane_crtc = old_plane_state->crtc;
10641 dm_new_plane_state = to_dm_plane_state(new_plane_state);
10642 dm_old_plane_state = to_dm_plane_state(old_plane_state);
10644 if (plane->type == DRM_PLANE_TYPE_CURSOR) {
10645 if (!enable || !new_plane_crtc ||
10646 drm_atomic_plane_disabling(plane->state, new_plane_state))
10649 new_acrtc = to_amdgpu_crtc(new_plane_crtc);
10651 if (new_plane_state->src_x != 0 || new_plane_state->src_y != 0) {
10652 DRM_DEBUG_ATOMIC("Cropping not supported for cursor plane\n");
10656 if (new_plane_state->fb) {
10657 ret = dm_check_cursor_fb(new_acrtc, new_plane_state,
10658 new_plane_state->fb);
10666 needs_reset = should_reset_plane(state, plane, old_plane_state,
10669 /* Remove any changed/removed planes */
10674 if (!old_plane_crtc)
10677 old_crtc_state = drm_atomic_get_old_crtc_state(
10678 state, old_plane_crtc);
10679 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
10681 if (!dm_old_crtc_state->stream)
10684 DRM_DEBUG_ATOMIC("Disabling DRM plane: %d on DRM crtc %d\n",
10685 plane->base.id, old_plane_crtc->base.id);
10687 ret = dm_atomic_get_state(state, &dm_state);
10691 if (!dc_remove_plane_from_context(
10693 dm_old_crtc_state->stream,
10694 dm_old_plane_state->dc_state,
10695 dm_state->context)) {
10701 dc_plane_state_release(dm_old_plane_state->dc_state);
10702 dm_new_plane_state->dc_state = NULL;
10704 *lock_and_validation_needed = true;
10706 } else { /* Add new planes */
10707 struct dc_plane_state *dc_new_plane_state;
10709 if (drm_atomic_plane_disabling(plane->state, new_plane_state))
10712 if (!new_plane_crtc)
10715 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_crtc);
10716 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
10718 if (!dm_new_crtc_state->stream)
10724 ret = dm_plane_helper_check_state(new_plane_state, new_crtc_state);
10728 WARN_ON(dm_new_plane_state->dc_state);
10730 dc_new_plane_state = dc_create_plane_state(dc);
10731 if (!dc_new_plane_state)
10734 DRM_DEBUG_ATOMIC("Enabling DRM plane: %d on DRM crtc %d\n",
10735 plane->base.id, new_plane_crtc->base.id);
10737 ret = fill_dc_plane_attributes(
10738 drm_to_adev(new_plane_crtc->dev),
10739 dc_new_plane_state,
10743 dc_plane_state_release(dc_new_plane_state);
10747 ret = dm_atomic_get_state(state, &dm_state);
10749 dc_plane_state_release(dc_new_plane_state);
10754 * Any atomic check errors that occur after this will
10755 * not need a release. The plane state will be attached
10756 * to the stream, and therefore part of the atomic
10757 * state. It'll be released when the atomic state is
10760 if (!dc_add_plane_to_context(
10762 dm_new_crtc_state->stream,
10763 dc_new_plane_state,
10764 dm_state->context)) {
10766 dc_plane_state_release(dc_new_plane_state);
10770 dm_new_plane_state->dc_state = dc_new_plane_state;
10772 dm_new_crtc_state->mpo_requested |= (plane->type == DRM_PLANE_TYPE_OVERLAY);
10774 /* Tell DC to do a full surface update every time there
10775 * is a plane change. Inefficient, but works for now.
10777 dm_new_plane_state->dc_state->update_flags.bits.full_update = 1;
10779 *lock_and_validation_needed = true;
10786 static void dm_get_oriented_plane_size(struct drm_plane_state *plane_state,
10787 int *src_w, int *src_h)
10789 switch (plane_state->rotation & DRM_MODE_ROTATE_MASK) {
10790 case DRM_MODE_ROTATE_90:
10791 case DRM_MODE_ROTATE_270:
10792 *src_w = plane_state->src_h >> 16;
10793 *src_h = plane_state->src_w >> 16;
10795 case DRM_MODE_ROTATE_0:
10796 case DRM_MODE_ROTATE_180:
10798 *src_w = plane_state->src_w >> 16;
10799 *src_h = plane_state->src_h >> 16;
10804 static int dm_check_crtc_cursor(struct drm_atomic_state *state,
10805 struct drm_crtc *crtc,
10806 struct drm_crtc_state *new_crtc_state)
10808 struct drm_plane *cursor = crtc->cursor, *underlying;
10809 struct drm_plane_state *new_cursor_state, *new_underlying_state;
10811 int cursor_scale_w, cursor_scale_h, underlying_scale_w, underlying_scale_h;
10812 int cursor_src_w, cursor_src_h;
10813 int underlying_src_w, underlying_src_h;
10815 /* On DCE and DCN there is no dedicated hardware cursor plane. We get a
10816 * cursor per pipe but it's going to inherit the scaling and
10817 * positioning from the underlying pipe. Check the cursor plane's
10818 * blending properties match the underlying planes'. */
10820 new_cursor_state = drm_atomic_get_new_plane_state(state, cursor);
10821 if (!new_cursor_state || !new_cursor_state->fb) {
10825 dm_get_oriented_plane_size(new_cursor_state, &cursor_src_w, &cursor_src_h);
10826 cursor_scale_w = new_cursor_state->crtc_w * 1000 / cursor_src_w;
10827 cursor_scale_h = new_cursor_state->crtc_h * 1000 / cursor_src_h;
10829 for_each_new_plane_in_state_reverse(state, underlying, new_underlying_state, i) {
10830 /* Narrow down to non-cursor planes on the same CRTC as the cursor */
10831 if (new_underlying_state->crtc != crtc || underlying == crtc->cursor)
10834 /* Ignore disabled planes */
10835 if (!new_underlying_state->fb)
10838 dm_get_oriented_plane_size(new_underlying_state,
10839 &underlying_src_w, &underlying_src_h);
10840 underlying_scale_w = new_underlying_state->crtc_w * 1000 / underlying_src_w;
10841 underlying_scale_h = new_underlying_state->crtc_h * 1000 / underlying_src_h;
10843 if (cursor_scale_w != underlying_scale_w ||
10844 cursor_scale_h != underlying_scale_h) {
10845 drm_dbg_atomic(crtc->dev,
10846 "Cursor [PLANE:%d:%s] scaling doesn't match underlying [PLANE:%d:%s]\n",
10847 cursor->base.id, cursor->name, underlying->base.id, underlying->name);
10851 /* If this plane covers the whole CRTC, no need to check planes underneath */
10852 if (new_underlying_state->crtc_x <= 0 &&
10853 new_underlying_state->crtc_y <= 0 &&
10854 new_underlying_state->crtc_x + new_underlying_state->crtc_w >= new_crtc_state->mode.hdisplay &&
10855 new_underlying_state->crtc_y + new_underlying_state->crtc_h >= new_crtc_state->mode.vdisplay)
10862 #if defined(CONFIG_DRM_AMD_DC_DCN)
10863 static int add_affected_mst_dsc_crtcs(struct drm_atomic_state *state, struct drm_crtc *crtc)
10865 struct drm_connector *connector;
10866 struct drm_connector_state *conn_state, *old_conn_state;
10867 struct amdgpu_dm_connector *aconnector = NULL;
10869 for_each_oldnew_connector_in_state(state, connector, old_conn_state, conn_state, i) {
10870 if (!conn_state->crtc)
10871 conn_state = old_conn_state;
10873 if (conn_state->crtc != crtc)
10876 aconnector = to_amdgpu_dm_connector(connector);
10877 if (!aconnector->port || !aconnector->mst_port)
10886 return drm_dp_mst_add_affected_dsc_crtcs(state, &aconnector->mst_port->mst_mgr);
10891 * amdgpu_dm_atomic_check() - Atomic check implementation for AMDgpu DM.
10892 * @dev: The DRM device
10893 * @state: The atomic state to commit
10895 * Validate that the given atomic state is programmable by DC into hardware.
10896 * This involves constructing a &struct dc_state reflecting the new hardware
10897 * state we wish to commit, then querying DC to see if it is programmable. It's
10898 * important not to modify the existing DC state. Otherwise, atomic_check
10899 * may unexpectedly commit hardware changes.
10901 * When validating the DC state, it's important that the right locks are
10902 * acquired. For full updates case which removes/adds/updates streams on one
10903 * CRTC while flipping on another CRTC, acquiring global lock will guarantee
10904 * that any such full update commit will wait for completion of any outstanding
10905 * flip using DRMs synchronization events.
10907 * Note that DM adds the affected connectors for all CRTCs in state, when that
10908 * might not seem necessary. This is because DC stream creation requires the
10909 * DC sink, which is tied to the DRM connector state. Cleaning this up should
10910 * be possible but non-trivial - a possible TODO item.
10912 * Return: -Error code if validation failed.
10914 static int amdgpu_dm_atomic_check(struct drm_device *dev,
10915 struct drm_atomic_state *state)
10917 struct amdgpu_device *adev = drm_to_adev(dev);
10918 struct dm_atomic_state *dm_state = NULL;
10919 struct dc *dc = adev->dm.dc;
10920 struct drm_connector *connector;
10921 struct drm_connector_state *old_con_state, *new_con_state;
10922 struct drm_crtc *crtc;
10923 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
10924 struct drm_plane *plane;
10925 struct drm_plane_state *old_plane_state, *new_plane_state;
10926 enum dc_status status;
10928 bool lock_and_validation_needed = false;
10929 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
10930 #if defined(CONFIG_DRM_AMD_DC_DCN)
10931 struct dsc_mst_fairness_vars vars[MAX_PIPES];
10932 struct drm_dp_mst_topology_state *mst_state;
10933 struct drm_dp_mst_topology_mgr *mgr;
10936 trace_amdgpu_dm_atomic_check_begin(state);
10938 ret = drm_atomic_helper_check_modeset(dev, state);
10940 DRM_DEBUG_DRIVER("drm_atomic_helper_check_modeset() failed\n");
10944 /* Check connector changes */
10945 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
10946 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
10947 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
10949 /* Skip connectors that are disabled or part of modeset already. */
10950 if (!old_con_state->crtc && !new_con_state->crtc)
10953 if (!new_con_state->crtc)
10956 new_crtc_state = drm_atomic_get_crtc_state(state, new_con_state->crtc);
10957 if (IS_ERR(new_crtc_state)) {
10958 DRM_DEBUG_DRIVER("drm_atomic_get_crtc_state() failed\n");
10959 ret = PTR_ERR(new_crtc_state);
10963 if (dm_old_con_state->abm_level !=
10964 dm_new_con_state->abm_level)
10965 new_crtc_state->connectors_changed = true;
10968 #if defined(CONFIG_DRM_AMD_DC_DCN)
10969 if (dc_resource_is_dsc_encoding_supported(dc)) {
10970 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
10971 if (drm_atomic_crtc_needs_modeset(new_crtc_state)) {
10972 ret = add_affected_mst_dsc_crtcs(state, crtc);
10974 DRM_DEBUG_DRIVER("add_affected_mst_dsc_crtcs() failed\n");
10979 pre_validate_dsc(state, &dm_state, vars);
10982 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
10983 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
10985 if (!drm_atomic_crtc_needs_modeset(new_crtc_state) &&
10986 !new_crtc_state->color_mgmt_changed &&
10987 old_crtc_state->vrr_enabled == new_crtc_state->vrr_enabled &&
10988 dm_old_crtc_state->dsc_force_changed == false)
10991 ret = amdgpu_dm_verify_lut_sizes(new_crtc_state);
10993 DRM_DEBUG_DRIVER("amdgpu_dm_verify_lut_sizes() failed\n");
10997 if (!new_crtc_state->enable)
11000 ret = drm_atomic_add_affected_connectors(state, crtc);
11002 DRM_DEBUG_DRIVER("drm_atomic_add_affected_connectors() failed\n");
11006 ret = drm_atomic_add_affected_planes(state, crtc);
11008 DRM_DEBUG_DRIVER("drm_atomic_add_affected_planes() failed\n");
11012 if (dm_old_crtc_state->dsc_force_changed)
11013 new_crtc_state->mode_changed = true;
11017 * Add all primary and overlay planes on the CRTC to the state
11018 * whenever a plane is enabled to maintain correct z-ordering
11019 * and to enable fast surface updates.
11021 drm_for_each_crtc(crtc, dev) {
11022 bool modified = false;
11024 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
11025 if (plane->type == DRM_PLANE_TYPE_CURSOR)
11028 if (new_plane_state->crtc == crtc ||
11029 old_plane_state->crtc == crtc) {
11038 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
11039 if (plane->type == DRM_PLANE_TYPE_CURSOR)
11043 drm_atomic_get_plane_state(state, plane);
11045 if (IS_ERR(new_plane_state)) {
11046 ret = PTR_ERR(new_plane_state);
11047 DRM_DEBUG_DRIVER("new_plane_state is BAD\n");
11053 /* Remove exiting planes if they are modified */
11054 for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) {
11055 ret = dm_update_plane_state(dc, state, plane,
11059 &lock_and_validation_needed);
11061 DRM_DEBUG_DRIVER("dm_update_plane_state() failed\n");
11066 /* Disable all crtcs which require disable */
11067 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
11068 ret = dm_update_crtc_state(&adev->dm, state, crtc,
11072 &lock_and_validation_needed);
11074 DRM_DEBUG_DRIVER("DISABLE: dm_update_crtc_state() failed\n");
11079 /* Enable all crtcs which require enable */
11080 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
11081 ret = dm_update_crtc_state(&adev->dm, state, crtc,
11085 &lock_and_validation_needed);
11087 DRM_DEBUG_DRIVER("ENABLE: dm_update_crtc_state() failed\n");
11092 /* Add new/modified planes */
11093 for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) {
11094 ret = dm_update_plane_state(dc, state, plane,
11098 &lock_and_validation_needed);
11100 DRM_DEBUG_DRIVER("dm_update_plane_state() failed\n");
11105 /* Run this here since we want to validate the streams we created */
11106 ret = drm_atomic_helper_check_planes(dev, state);
11108 DRM_DEBUG_DRIVER("drm_atomic_helper_check_planes() failed\n");
11112 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
11113 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
11114 if (dm_new_crtc_state->mpo_requested)
11115 DRM_DEBUG_DRIVER("MPO enablement requested on crtc:[%p]\n", crtc);
11118 /* Check cursor planes scaling */
11119 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
11120 ret = dm_check_crtc_cursor(state, crtc, new_crtc_state);
11122 DRM_DEBUG_DRIVER("dm_check_crtc_cursor() failed\n");
11127 if (state->legacy_cursor_update) {
11129 * This is a fast cursor update coming from the plane update
11130 * helper, check if it can be done asynchronously for better
11133 state->async_update =
11134 !drm_atomic_helper_async_check(dev, state);
11137 * Skip the remaining global validation if this is an async
11138 * update. Cursor updates can be done without affecting
11139 * state or bandwidth calcs and this avoids the performance
11140 * penalty of locking the private state object and
11141 * allocating a new dc_state.
11143 if (state->async_update)
11147 /* Check scaling and underscan changes*/
11148 /* TODO Removed scaling changes validation due to inability to commit
11149 * new stream into context w\o causing full reset. Need to
11150 * decide how to handle.
11152 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
11153 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
11154 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
11155 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
11157 /* Skip any modesets/resets */
11158 if (!acrtc || drm_atomic_crtc_needs_modeset(
11159 drm_atomic_get_new_crtc_state(state, &acrtc->base)))
11162 /* Skip any thing not scale or underscan changes */
11163 if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state))
11166 lock_and_validation_needed = true;
11169 #if defined(CONFIG_DRM_AMD_DC_DCN)
11170 /* set the slot info for each mst_state based on the link encoding format */
11171 for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
11172 struct amdgpu_dm_connector *aconnector;
11173 struct drm_connector *connector;
11174 struct drm_connector_list_iter iter;
11175 u8 link_coding_cap;
11177 if (!mgr->mst_state )
11180 drm_connector_list_iter_begin(dev, &iter);
11181 drm_for_each_connector_iter(connector, &iter) {
11182 int id = connector->index;
11184 if (id == mst_state->mgr->conn_base_id) {
11185 aconnector = to_amdgpu_dm_connector(connector);
11186 link_coding_cap = dc_link_dp_mst_decide_link_encoding_format(aconnector->dc_link);
11187 drm_dp_mst_update_slots(mst_state, link_coding_cap);
11192 drm_connector_list_iter_end(&iter);
11197 * Streams and planes are reset when there are changes that affect
11198 * bandwidth. Anything that affects bandwidth needs to go through
11199 * DC global validation to ensure that the configuration can be applied
11202 * We have to currently stall out here in atomic_check for outstanding
11203 * commits to finish in this case because our IRQ handlers reference
11204 * DRM state directly - we can end up disabling interrupts too early
11207 * TODO: Remove this stall and drop DM state private objects.
11209 if (lock_and_validation_needed) {
11210 ret = dm_atomic_get_state(state, &dm_state);
11212 DRM_DEBUG_DRIVER("dm_atomic_get_state() failed\n");
11216 ret = do_aquire_global_lock(dev, state);
11218 DRM_DEBUG_DRIVER("do_aquire_global_lock() failed\n");
11222 #if defined(CONFIG_DRM_AMD_DC_DCN)
11223 if (!compute_mst_dsc_configs_for_state(state, dm_state->context, vars)) {
11224 DRM_DEBUG_DRIVER("compute_mst_dsc_configs_for_state() failed\n");
11228 ret = dm_update_mst_vcpi_slots_for_dsc(state, dm_state->context, vars);
11230 DRM_DEBUG_DRIVER("dm_update_mst_vcpi_slots_for_dsc() failed\n");
11236 * Perform validation of MST topology in the state:
11237 * We need to perform MST atomic check before calling
11238 * dc_validate_global_state(), or there is a chance
11239 * to get stuck in an infinite loop and hang eventually.
11241 ret = drm_dp_mst_atomic_check(state);
11243 DRM_DEBUG_DRIVER("drm_dp_mst_atomic_check() failed\n");
11246 status = dc_validate_global_state(dc, dm_state->context, true);
11247 if (status != DC_OK) {
11248 DRM_DEBUG_DRIVER("DC global validation failure: %s (%d)",
11249 dc_status_to_str(status), status);
11255 * The commit is a fast update. Fast updates shouldn't change
11256 * the DC context, affect global validation, and can have their
11257 * commit work done in parallel with other commits not touching
11258 * the same resource. If we have a new DC context as part of
11259 * the DM atomic state from validation we need to free it and
11260 * retain the existing one instead.
11262 * Furthermore, since the DM atomic state only contains the DC
11263 * context and can safely be annulled, we can free the state
11264 * and clear the associated private object now to free
11265 * some memory and avoid a possible use-after-free later.
11268 for (i = 0; i < state->num_private_objs; i++) {
11269 struct drm_private_obj *obj = state->private_objs[i].ptr;
11271 if (obj->funcs == adev->dm.atomic_obj.funcs) {
11272 int j = state->num_private_objs-1;
11274 dm_atomic_destroy_state(obj,
11275 state->private_objs[i].state);
11277 /* If i is not at the end of the array then the
11278 * last element needs to be moved to where i was
11279 * before the array can safely be truncated.
11282 state->private_objs[i] =
11283 state->private_objs[j];
11285 state->private_objs[j].ptr = NULL;
11286 state->private_objs[j].state = NULL;
11287 state->private_objs[j].old_state = NULL;
11288 state->private_objs[j].new_state = NULL;
11290 state->num_private_objs = j;
11296 /* Store the overall update type for use later in atomic check. */
11297 for_each_new_crtc_in_state (state, crtc, new_crtc_state, i) {
11298 struct dm_crtc_state *dm_new_crtc_state =
11299 to_dm_crtc_state(new_crtc_state);
11301 dm_new_crtc_state->update_type = lock_and_validation_needed ?
11306 /* Must be success */
11309 trace_amdgpu_dm_atomic_check_finish(state, ret);
11314 if (ret == -EDEADLK)
11315 DRM_DEBUG_DRIVER("Atomic check stopped to avoid deadlock.\n");
11316 else if (ret == -EINTR || ret == -EAGAIN || ret == -ERESTARTSYS)
11317 DRM_DEBUG_DRIVER("Atomic check stopped due to signal.\n");
11319 DRM_DEBUG_DRIVER("Atomic check failed with err: %d \n", ret);
11321 trace_amdgpu_dm_atomic_check_finish(state, ret);
11326 static bool is_dp_capable_without_timing_msa(struct dc *dc,
11327 struct amdgpu_dm_connector *amdgpu_dm_connector)
11330 bool capable = false;
11332 if (amdgpu_dm_connector->dc_link &&
11333 dm_helpers_dp_read_dpcd(
11335 amdgpu_dm_connector->dc_link,
11336 DP_DOWN_STREAM_PORT_COUNT,
11338 sizeof(dpcd_data))) {
11339 capable = (dpcd_data & DP_MSA_TIMING_PAR_IGNORED) ? true:false;
11345 static bool dm_edid_parser_send_cea(struct amdgpu_display_manager *dm,
11346 unsigned int offset,
11347 unsigned int total_length,
11349 unsigned int length,
11350 struct amdgpu_hdmi_vsdb_info *vsdb)
11353 union dmub_rb_cmd cmd;
11354 struct dmub_cmd_send_edid_cea *input;
11355 struct dmub_cmd_edid_cea_output *output;
11357 if (length > DMUB_EDID_CEA_DATA_CHUNK_BYTES)
11360 memset(&cmd, 0, sizeof(cmd));
11362 input = &cmd.edid_cea.data.input;
11364 cmd.edid_cea.header.type = DMUB_CMD__EDID_CEA;
11365 cmd.edid_cea.header.sub_type = 0;
11366 cmd.edid_cea.header.payload_bytes =
11367 sizeof(cmd.edid_cea) - sizeof(cmd.edid_cea.header);
11368 input->offset = offset;
11369 input->length = length;
11370 input->cea_total_length = total_length;
11371 memcpy(input->payload, data, length);
11373 res = dc_dmub_srv_cmd_with_reply_data(dm->dc->ctx->dmub_srv, &cmd);
11375 DRM_ERROR("EDID CEA parser failed\n");
11379 output = &cmd.edid_cea.data.output;
11381 if (output->type == DMUB_CMD__EDID_CEA_ACK) {
11382 if (!output->ack.success) {
11383 DRM_ERROR("EDID CEA ack failed at offset %d\n",
11384 output->ack.offset);
11386 } else if (output->type == DMUB_CMD__EDID_CEA_AMD_VSDB) {
11387 if (!output->amd_vsdb.vsdb_found)
11390 vsdb->freesync_supported = output->amd_vsdb.freesync_supported;
11391 vsdb->amd_vsdb_version = output->amd_vsdb.amd_vsdb_version;
11392 vsdb->min_refresh_rate_hz = output->amd_vsdb.min_frame_rate;
11393 vsdb->max_refresh_rate_hz = output->amd_vsdb.max_frame_rate;
11395 DRM_WARN("Unknown EDID CEA parser results\n");
11402 static bool parse_edid_cea_dmcu(struct amdgpu_display_manager *dm,
11403 uint8_t *edid_ext, int len,
11404 struct amdgpu_hdmi_vsdb_info *vsdb_info)
11408 /* send extension block to DMCU for parsing */
11409 for (i = 0; i < len; i += 8) {
11413 /* send 8 bytes a time */
11414 if (!dc_edid_parser_send_cea(dm->dc, i, len, &edid_ext[i], 8))
11418 /* EDID block sent completed, expect result */
11419 int version, min_rate, max_rate;
11421 res = dc_edid_parser_recv_amd_vsdb(dm->dc, &version, &min_rate, &max_rate);
11423 /* amd vsdb found */
11424 vsdb_info->freesync_supported = 1;
11425 vsdb_info->amd_vsdb_version = version;
11426 vsdb_info->min_refresh_rate_hz = min_rate;
11427 vsdb_info->max_refresh_rate_hz = max_rate;
11435 res = dc_edid_parser_recv_cea_ack(dm->dc, &offset);
11443 static bool parse_edid_cea_dmub(struct amdgpu_display_manager *dm,
11444 uint8_t *edid_ext, int len,
11445 struct amdgpu_hdmi_vsdb_info *vsdb_info)
11449 /* send extension block to DMCU for parsing */
11450 for (i = 0; i < len; i += 8) {
11451 /* send 8 bytes a time */
11452 if (!dm_edid_parser_send_cea(dm, i, len, &edid_ext[i], 8, vsdb_info))
11456 return vsdb_info->freesync_supported;
11459 static bool parse_edid_cea(struct amdgpu_dm_connector *aconnector,
11460 uint8_t *edid_ext, int len,
11461 struct amdgpu_hdmi_vsdb_info *vsdb_info)
11463 struct amdgpu_device *adev = drm_to_adev(aconnector->base.dev);
11465 if (adev->dm.dmub_srv)
11466 return parse_edid_cea_dmub(&adev->dm, edid_ext, len, vsdb_info);
11468 return parse_edid_cea_dmcu(&adev->dm, edid_ext, len, vsdb_info);
11471 static int parse_hdmi_amd_vsdb(struct amdgpu_dm_connector *aconnector,
11472 struct edid *edid, struct amdgpu_hdmi_vsdb_info *vsdb_info)
11474 uint8_t *edid_ext = NULL;
11476 bool valid_vsdb_found = false;
11478 /*----- drm_find_cea_extension() -----*/
11479 /* No EDID or EDID extensions */
11480 if (edid == NULL || edid->extensions == 0)
11483 /* Find CEA extension */
11484 for (i = 0; i < edid->extensions; i++) {
11485 edid_ext = (uint8_t *)edid + EDID_LENGTH * (i + 1);
11486 if (edid_ext[0] == CEA_EXT)
11490 if (i == edid->extensions)
11493 /*----- cea_db_offsets() -----*/
11494 if (edid_ext[0] != CEA_EXT)
11497 valid_vsdb_found = parse_edid_cea(aconnector, edid_ext, EDID_LENGTH, vsdb_info);
11499 return valid_vsdb_found ? i : -ENODEV;
11502 void amdgpu_dm_update_freesync_caps(struct drm_connector *connector,
11506 struct detailed_timing *timing;
11507 struct detailed_non_pixel *data;
11508 struct detailed_data_monitor_range *range;
11509 struct amdgpu_dm_connector *amdgpu_dm_connector =
11510 to_amdgpu_dm_connector(connector);
11511 struct dm_connector_state *dm_con_state = NULL;
11512 struct dc_sink *sink;
11514 struct drm_device *dev = connector->dev;
11515 struct amdgpu_device *adev = drm_to_adev(dev);
11516 bool freesync_capable = false;
11517 struct amdgpu_hdmi_vsdb_info vsdb_info = {0};
11519 if (!connector->state) {
11520 DRM_ERROR("%s - Connector has no state", __func__);
11524 sink = amdgpu_dm_connector->dc_sink ?
11525 amdgpu_dm_connector->dc_sink :
11526 amdgpu_dm_connector->dc_em_sink;
11528 if (!edid || !sink) {
11529 dm_con_state = to_dm_connector_state(connector->state);
11531 amdgpu_dm_connector->min_vfreq = 0;
11532 amdgpu_dm_connector->max_vfreq = 0;
11533 amdgpu_dm_connector->pixel_clock_mhz = 0;
11534 connector->display_info.monitor_range.min_vfreq = 0;
11535 connector->display_info.monitor_range.max_vfreq = 0;
11536 freesync_capable = false;
11541 dm_con_state = to_dm_connector_state(connector->state);
11543 if (!adev->dm.freesync_module)
11547 if (sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT
11548 || sink->sink_signal == SIGNAL_TYPE_EDP) {
11549 bool edid_check_required = false;
11552 edid_check_required = is_dp_capable_without_timing_msa(
11554 amdgpu_dm_connector);
11557 if (edid_check_required == true && (edid->version > 1 ||
11558 (edid->version == 1 && edid->revision > 1))) {
11559 for (i = 0; i < 4; i++) {
11561 timing = &edid->detailed_timings[i];
11562 data = &timing->data.other_data;
11563 range = &data->data.range;
11565 * Check if monitor has continuous frequency mode
11567 if (data->type != EDID_DETAIL_MONITOR_RANGE)
11570 * Check for flag range limits only. If flag == 1 then
11571 * no additional timing information provided.
11572 * Default GTF, GTF Secondary curve and CVT are not
11575 if (range->flags != 1)
11578 amdgpu_dm_connector->min_vfreq = range->min_vfreq;
11579 amdgpu_dm_connector->max_vfreq = range->max_vfreq;
11580 amdgpu_dm_connector->pixel_clock_mhz =
11581 range->pixel_clock_mhz * 10;
11583 connector->display_info.monitor_range.min_vfreq = range->min_vfreq;
11584 connector->display_info.monitor_range.max_vfreq = range->max_vfreq;
11589 if (amdgpu_dm_connector->max_vfreq -
11590 amdgpu_dm_connector->min_vfreq > 10) {
11592 freesync_capable = true;
11595 } else if (edid && sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A) {
11596 i = parse_hdmi_amd_vsdb(amdgpu_dm_connector, edid, &vsdb_info);
11597 if (i >= 0 && vsdb_info.freesync_supported) {
11598 timing = &edid->detailed_timings[i];
11599 data = &timing->data.other_data;
11601 amdgpu_dm_connector->min_vfreq = vsdb_info.min_refresh_rate_hz;
11602 amdgpu_dm_connector->max_vfreq = vsdb_info.max_refresh_rate_hz;
11603 if (amdgpu_dm_connector->max_vfreq - amdgpu_dm_connector->min_vfreq > 10)
11604 freesync_capable = true;
11606 connector->display_info.monitor_range.min_vfreq = vsdb_info.min_refresh_rate_hz;
11607 connector->display_info.monitor_range.max_vfreq = vsdb_info.max_refresh_rate_hz;
11613 dm_con_state->freesync_capable = freesync_capable;
11615 if (connector->vrr_capable_property)
11616 drm_connector_set_vrr_capable_property(connector,
11620 void amdgpu_dm_trigger_timing_sync(struct drm_device *dev)
11622 struct amdgpu_device *adev = drm_to_adev(dev);
11623 struct dc *dc = adev->dm.dc;
11626 mutex_lock(&adev->dm.dc_lock);
11627 if (dc->current_state) {
11628 for (i = 0; i < dc->current_state->stream_count; ++i)
11629 dc->current_state->streams[i]
11630 ->triggered_crtc_reset.enabled =
11631 adev->dm.force_timing_sync;
11633 dm_enable_per_frame_crtc_master_sync(dc->current_state);
11634 dc_trigger_sync(dc, dc->current_state);
11636 mutex_unlock(&adev->dm.dc_lock);
11639 void dm_write_reg_func(const struct dc_context *ctx, uint32_t address,
11640 uint32_t value, const char *func_name)
11642 #ifdef DM_CHECK_ADDR_0
11643 if (address == 0) {
11644 DC_ERR("invalid register write. address = 0");
11648 cgs_write_register(ctx->cgs_device, address, value);
11649 trace_amdgpu_dc_wreg(&ctx->perf_trace->write_count, address, value);
11652 uint32_t dm_read_reg_func(const struct dc_context *ctx, uint32_t address,
11653 const char *func_name)
11656 #ifdef DM_CHECK_ADDR_0
11657 if (address == 0) {
11658 DC_ERR("invalid register read; address = 0\n");
11663 if (ctx->dmub_srv &&
11664 ctx->dmub_srv->reg_helper_offload.gather_in_progress &&
11665 !ctx->dmub_srv->reg_helper_offload.should_burst_write) {
11670 value = cgs_read_register(ctx->cgs_device, address);
11672 trace_amdgpu_dc_rreg(&ctx->perf_trace->read_count, address, value);
11677 static int amdgpu_dm_set_dmub_async_sync_status(bool is_cmd_aux,
11678 struct dc_context *ctx,
11679 uint8_t status_type,
11680 uint32_t *operation_result)
11682 struct amdgpu_device *adev = ctx->driver_context;
11683 int return_status = -1;
11684 struct dmub_notification *p_notify = adev->dm.dmub_notify;
11687 if (status_type == DMUB_ASYNC_TO_SYNC_ACCESS_SUCCESS) {
11688 return_status = p_notify->aux_reply.length;
11689 *operation_result = p_notify->result;
11690 } else if (status_type == DMUB_ASYNC_TO_SYNC_ACCESS_TIMEOUT) {
11691 *operation_result = AUX_RET_ERROR_TIMEOUT;
11692 } else if (status_type == DMUB_ASYNC_TO_SYNC_ACCESS_FAIL) {
11693 *operation_result = AUX_RET_ERROR_ENGINE_ACQUIRE;
11695 *operation_result = AUX_RET_ERROR_UNKNOWN;
11698 if (status_type == DMUB_ASYNC_TO_SYNC_ACCESS_SUCCESS) {
11700 *operation_result = p_notify->sc_status;
11702 *operation_result = SET_CONFIG_UNKNOWN_ERROR;
11706 return return_status;
11709 int amdgpu_dm_process_dmub_aux_transfer_sync(bool is_cmd_aux, struct dc_context *ctx,
11710 unsigned int link_index, void *cmd_payload, void *operation_result)
11712 struct amdgpu_device *adev = ctx->driver_context;
11716 dc_process_dmub_aux_transfer_async(ctx->dc,
11717 link_index, (struct aux_payload *)cmd_payload);
11718 } else if (dc_process_dmub_set_config_async(ctx->dc, link_index,
11719 (struct set_config_cmd_payload *)cmd_payload,
11720 adev->dm.dmub_notify)) {
11721 return amdgpu_dm_set_dmub_async_sync_status(is_cmd_aux,
11722 ctx, DMUB_ASYNC_TO_SYNC_ACCESS_SUCCESS,
11723 (uint32_t *)operation_result);
11726 ret = wait_for_completion_timeout(&adev->dm.dmub_aux_transfer_done, 10 * HZ);
11728 DRM_ERROR("wait_for_completion_timeout timeout!");
11729 return amdgpu_dm_set_dmub_async_sync_status(is_cmd_aux,
11730 ctx, DMUB_ASYNC_TO_SYNC_ACCESS_TIMEOUT,
11731 (uint32_t *)operation_result);
11735 if (adev->dm.dmub_notify->result == AUX_RET_SUCCESS) {
11736 struct aux_payload *payload = (struct aux_payload *)cmd_payload;
11738 payload->reply[0] = adev->dm.dmub_notify->aux_reply.command;
11739 if (!payload->write && adev->dm.dmub_notify->aux_reply.length &&
11740 payload->reply[0] == AUX_TRANSACTION_REPLY_AUX_ACK) {
11741 memcpy(payload->data, adev->dm.dmub_notify->aux_reply.data,
11742 adev->dm.dmub_notify->aux_reply.length);
11747 return amdgpu_dm_set_dmub_async_sync_status(is_cmd_aux,
11748 ctx, DMUB_ASYNC_TO_SYNC_ACCESS_SUCCESS,
11749 (uint32_t *)operation_result);
11753 * Check whether seamless boot is supported.
11755 * So far we only support seamless boot on CHIP_VANGOGH.
11756 * If everything goes well, we may consider expanding
11757 * seamless boot to other ASICs.
11759 bool check_seamless_boot_capability(struct amdgpu_device *adev)
11761 switch (adev->asic_type) {
11763 if (!adev->mman.keep_stolen_vga_memory)